diff --git a/core/server/videos.cpp b/core/server/videos.cpp index a982d128..dc905151 100644 --- a/core/server/videos.cpp +++ b/core/server/videos.cpp @@ -377,7 +377,7 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) { static const char* kKnown[] = { "model", "prompt", "seconds", "size", "input_reference", "seed", "num_inference_steps", "task", "target", "conditions", "user", "latent_upscale", "upscale_refine_steps", - "sampler", "shift", "audio_shift", + "sampler", "shift", "audio_shift", "first_frame_mode", "negative_prompt", "decoder", "loras", "lora", /* RadVideoGenBatch::guidance_h's order */ "guidance_scale", "stg_scale", "modality_scale", "guidance_rescale", @@ -522,6 +522,25 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) { "last_frame part"); } } + /* How the FIRST keyframe is fitted to the canvas. The reference stretches it ("disabled"): + * every pixel survives, but an anchor whose aspect differs from the canvas's is distorted -- + * a portrait photo pinned into a landscape video comes out squashed. "center" instead keeps + * the anchor's aspect and centre-crops the excess: true geometry, edges fall off the canvas. + * The last frame always centre-crops ("center"), as the reference does. Ignored without a + * first_frame part. */ + bool first_center = false; + if (has(body, "first_frame_mode")) { + const std::string fm = body["first_frame_mode"].is_string() + ? body["first_frame_mode"].get() : ""; + if (fm.empty() || fm == "stretch") + ; + else if (fm == "center") + first_center = true; + else + return bad("first_frame_mode", "expected \"stretch\" (the reference's own resize) " + "or \"center\" (keep the anchor's aspect, crop the " + "canvas)"); + } json target = json::object(); if (has(body, "target")) { if (!body["target"].is_object()) return bad("target", "expected an object"); @@ -914,13 +933,15 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) { const size_t n = m.data.size(); if (m.kind == gen::VideoRefMedia::FIRST_FRAME || m.kind == gen::VideoRefMedia::LAST_FRAME) { - /* A keyframe anchor resized to the EXACT output canvas: first a plain stretch - * ("disabled"), last a centre crop ("center") -- the reference's own resize. Its - * latent grid is then the target's, so the pack's pinned rows (frame_rows wide) - * match what the encoder writes into the image slot. */ + /* A keyframe anchor resized to the EXACT output canvas: the first a plain stretch + * ("disabled" -- the reference's own resize) unless the request asked + * first_frame_mode=center, the last always a centre crop ("center"). Its latent + * grid is then the target's, so the pack's pinned rows (frame_rows wide) match + * what the encoder writes into the image slot. */ mm::Frame fr; if (deps_.mm->encoder_image_canvas(p, n, (int32_t)W, (int32_t)H, - m.kind == gen::VideoRefMedia::LAST_FRAME, + m.kind == gen::VideoRefMedia::LAST_FRAME + || first_center, &fr, &why) < 0) return bad(m.name.c_str(), "'" + m.name + "' could not be encoded: " + why); m.enc_frames = 1; diff --git a/tests/server_fakes.h b/tests/server_fakes.h index 5e3433cb..1c76c721 100644 --- a/tests/server_fakes.h +++ b/tests/server_fakes.h @@ -411,9 +411,11 @@ struct FakeMultimodal : IMultimodal { int32_t enc_frames = 5, enc_side = 16; int64_t enc_n = 850; int n_ienc = 0, n_venc = 0, n_aenc = 0, n_cenc = 0; - /* The canvas an anchor was resized to, and whether the last frame's cover-crop was asked for. */ + /* The canvas an anchor was resized to, and the cover-crop asks -- cenc_cover is the LAST + * call's flag, cenc_covers records every call in order (first frame, then last). */ int32_t cenc_w = 0, cenc_h = 0; bool cenc_cover = false; + std::vector cenc_covers; int encoder_image(const uint8_t*, size_t, int32_t, int32_t, mm::Frame* out, std::string*) override { ++n_ienc; @@ -425,6 +427,7 @@ struct FakeMultimodal : IMultimodal { mm::Frame* out, std::string*) override { ++n_cenc; cenc_w = w; cenc_h = h; cenc_cover = cover; + cenc_covers.push_back(cover); out->w = w; out->h = h; out->rgb.assign((size_t)w * h * 3, 100); return RAD_OK; diff --git a/tests/videos_test.cpp b/tests/videos_test.cpp index 43904751..6727f178 100644 --- a/tests/videos_test.cpp +++ b/tests/videos_test.cpp @@ -654,6 +654,9 @@ TEST(fl2va_keyframes_feed_the_tower_and_pin_the_output_ends) { CHECK_EQ(mm.cenc_w, 256); CHECK_EQ(mm.cenc_h, 256); CHECK(mm.cenc_cover); /* the last call was the last frame's centre crop */ + REQUIRE_EQ(mm.cenc_covers.size(), (size_t)2); + CHECK(!mm.cenc_covers[0]); /* default first frame: the reference's stretch */ + CHECK(mm.cenc_covers[1]); /* the last frame: always the centre crop */ const gen::VideoRequest& g = gen.last(); REQUIRE_EQ(g.refs.size(), (size_t)2); CHECK_EQ((int)g.refs[0].kind, (int)gen::VideoRefMedia::FIRST_FRAME); @@ -679,6 +682,83 @@ TEST(fl2va_keyframes_feed_the_tower_and_pin_the_output_ends) { CHECK(g.tokens == want); } +/* first_frame_mode=center: the FIRST anchor is cover-cropped to the canvas instead of stretched -- + * the no-distortion answer for a portrait photo pinned into a landscape output. The flag reaches + * encoder_image_canvas on the first call; the last frame keeps its centre crop either way. */ +TEST(first_frame_mode_center_cover_crops_the_first_anchor) { + FakeMultimodal mm; + mm.enc_frames = 5; mm.enc_side = 16; mm.enc_n = 850; + FakeVideoGen gen; + std::unique_ptr f(new Fixture(Fixture::Tweak{ [&](ServerOptions& o, Deps& d) { + d.videos = &gen; + d.mm = &mm; + o.video_model = true; + gen.inf.encodes = 1; + gen.inf.enc_ratio = 16; + gen.inf.max_enc_side = 512; + } })); + HttpRequest q; + q.method = "POST"; + q.path = "/v1/videos"; + q.form = { FormPart{ "prompt", "", "", "fox" }, + FormPart{ "size", "", "", "256x256" }, + FormPart{ "task", "", "", "fl2va" }, + FormPart{ "first_frame", "f.png", "image/png", "PF" }, + FormPart{ "last_frame", "l.png", "image/png", "PL" }, + FormPart{ "first_frame_mode", "", "", "center" } }; + HttpResponse r = f->srv->router().dispatch(q); + REQUIRE_EQ(r.status, 200); + REQUIRE_EQ(mm.cenc_covers.size(), (size_t)2); + CHECK(mm.cenc_covers[0]); /* the first anchor: centre crop, not stretch */ + CHECK(mm.cenc_covers[1]); /* the last: unchanged */ + CHECK_EQ(mm.cenc_w, 256); + CHECK_EQ(mm.cenc_h, 256); /* still the EXACT canvas: pins need its grid */ +} + +/* first_frame_mode takes two values; anything else is refused naming the field, and an explicit + * "stretch" (or empty form field) keeps the reference's default. */ +TEST(first_frame_mode_refuses_a_mode_it_does_not_know) { + FakeMultimodal mm; + mm.enc_frames = 5; mm.enc_side = 16; mm.enc_n = 850; + FakeVideoGen gen; + std::unique_ptr f(new Fixture(Fixture::Tweak{ [&](ServerOptions& o, Deps& d) { + d.videos = &gen; + d.mm = &mm; + o.video_model = true; + gen.inf.encodes = 1; + gen.inf.enc_ratio = 16; + gen.inf.max_enc_side = 512; + } })); + auto post = [&](const char* mode) { + HttpRequest q; + q.method = "POST"; + q.path = "/v1/videos"; + q.form = { FormPart{ "prompt", "", "", "fox" }, FormPart{ "size", "", "", "256x256" }, + FormPart{ "first_frame", "f.png", "image/png", "PF" } }; + if (mode) q.form.push_back(FormPart{ "first_frame_mode", "", "", mode }); + return f->srv->router().dispatch(q); + }; + { + HttpResponse r = post("warp"); + CHECK(r.status >= 400); + CHECK(r.body.find("first_frame_mode") != std::string::npos); + } + mm.cenc_covers.clear(); + { + HttpResponse r = post("stretch"); + REQUIRE_EQ(r.status, 200); + REQUIRE_EQ(mm.cenc_covers.size(), (size_t)1); + CHECK(!mm.cenc_covers[0]); + } + mm.cenc_covers.clear(); + { + HttpResponse r = post(""); /* an empty form field is "not set" */ + REQUIRE_EQ(r.status, 200); + REQUIRE_EQ(mm.cenc_covers.size(), (size_t)1); + CHECK(!mm.cenc_covers[0]); + } +} + /* Without a vision tower the keyframe request still renders: the anchors keep their VAE pin but carry * no text-encoder media (the deliberate pin-only fallback, not a silent weakening). */ TEST(fl2va_without_a_tower_pins_only) {