Archived
video: endpoint keyframe completion (I2VA / L2VA / FL2VA)
One keyframe path serves three DaSiWa modes: first_frame pins output frame 0,
last_frame pins the last frame, and the gap is diffused. A keyframe is pure
attention conditioning -- its VAE latent rides as a pinned (update=0) cond row
at its frame's time position, faithful to comfy/ldm/minimax/model.py where
keyframe latents are img_update==False rows. The sampler is untouched; the step
merely widens to carry the extra cond rows.
- server (videos.cpp/iface.h/adapters.h): first_frame/last_frame form parts ->
VideoRefMedia::FIRST_FRAME/LAST_FRAME; kf_task validation (i2va/fl2va need
first, l2va/fl2va need last); keyframes are mutually exclusive with refs;
encoder_image_canvas(cover=LAST) sizes the anchor.
- driver (video_driver.cpp): keyframe pass issues a RAD_ENC_IMAGE Vencode per
anchor (slot = ++ref_tab[1], ordered), appends ref_tab key triples
{frame(0|frames-1), video_t=1, audio_t=0}.
- arch (minimaxh3.cpp): ref_parts emits keyframe cond parts FIRST; the step
scatter admits kR2VCond/kR2VCondAudio rows.
- tests: video_gen_test keyframe-completes-widens-the-step-and-pins-the-ends
(2x Vencode at slot 1,2; widened step; seg_cu/lat_cu/ref_tab pinned); fake_pack_ref
lays keyframe rows (nk=0 byte-identical to before); videos_test keyframe API.
Device-verified (TP2, seed 9999): FL2VA completes a valid 73-frame MP4+AAC; pins
are consumed (fl2va != same-seed t2va) and force the endpoints (frame0 vs anchor
0.687 pinned vs 0.460 un-pinned; last 0.719 vs 0.521). Fidelity follow-up (not
function): anchors ride as pins only; the reference also feeds them through the
TE as vision tokens, so a prompt that contradicts the anchor lets text win --
full endpoint parity vs a golden needs TE-anchor tokens (#10/P6 bucket).
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
8ddcbbf01f
commit
60e920cd4d
11 files changed
+410
-37
No files matched your search
+8
-1
@@ -79,7 +79,14 @@ private:
|
||||
* its kind, which fixes its slot in the conditioning; VIDEO_AUDIO #i is the soundtrack of
|
||||
* VIDEO #i. */
|
||||
struct VideoRefMedia {
|
||||
enum Kind : int32_t { IMAGE = 0, VIDEO = 1, VIDEO_AUDIO = 2, AUDIO = 3 };
|
||||
/* FIRST_FRAME / LAST_FRAME are endpoint KEYFRAMES, not references: a keyframe completion pins
|
||||
* one output frame (first to frame 0, last to the final frame) and diffuses the gap between.
|
||||
* They are never conditioned on as identities, so they take no reference row -- the driver folds
|
||||
* each into the pack's keyframe table and encodes it into an image slot (a keyframe request
|
||||
* carries no references, so those slots are free). The server resizes each to the target canvas
|
||||
* (first a plain stretch, last a centre crop) and sets enc_pixels to one frame at that size, so
|
||||
* its latent grid is the target's exactly and its pinned rows are frame_rows wide. */
|
||||
enum Kind : int32_t { IMAGE = 0, VIDEO = 1, VIDEO_AUDIO = 2, AUDIO = 3, FIRST_FRAME = 4, LAST_FRAME = 5 };
|
||||
Kind kind = IMAGE;
|
||||
int32_t index = 0;
|
||||
std::string name, filename, content_type; /* the part's name and what the client called the file */
|
||||
|
||||
@@ -417,12 +417,14 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
* frames and its soundtrack's are the Vencode/Aencode latent counts rad_arch_encode_geom gives
|
||||
* (a video's are the same before and after the encode pass clip-pads its short final clip). The
|
||||
* table must exist before the step layout is decided, because when the denoiser consumes it the
|
||||
* reference rows are part of that layout. The keyframe count stays 0 (continuation is separate
|
||||
* work), so the table holds only reference rows. */
|
||||
* reference rows are part of that layout. Endpoint keyframes (a first and/or last frame pinned
|
||||
* at the output's ends) are appended after the reference rows as {frame, video_t, audio_t}
|
||||
* triples; a request carries either references or keyframes, never both. */
|
||||
if (d.encodes) {
|
||||
bool need_geom = false;
|
||||
for (const VideoRefMedia& m : r->refs)
|
||||
if (m.kind != VideoRefMedia::IMAGE) need_geom = true;
|
||||
if (m.kind != VideoRefMedia::IMAGE && m.kind != VideoRefMedia::FIRST_FRAME &&
|
||||
m.kind != VideoRefMedia::LAST_FRAME) need_geom = true;
|
||||
if (need_geom && !enc_geom_)
|
||||
return no("the request carries a reference video or audio, but this plugin exports no "
|
||||
"rad_arch_encode_geom, so their encoder geometry cannot be sized");
|
||||
@@ -507,6 +509,33 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
}
|
||||
/* VIDEO_AUDIO rows are folded into their video above, so they take no row here. */
|
||||
}
|
||||
/* Endpoint keyframes: each FIRST_FRAME/LAST_FRAME the server resized to the target canvas
|
||||
* and staged in enc_pixels folds into the pack's KEYFRAME table -- not a reference row. The
|
||||
* DiT reads its latent rows PINNED at that frame's moment on the target timeline (denoised
|
||||
* never): the first pins output frame 0, the last the final frame, and the gap between is
|
||||
* diffused. The key triple is {pixel frame, video_t, audio_t}; a picture anchor is video-only
|
||||
* (video_t = one latent frame, audio_t = 0), and its pixel frame is 0 or frames-1 -- the same
|
||||
* resolved frame index the reference's MiniMaxH3ImageToVideo assigns. Its latents ride the
|
||||
* image slot its keyframe ORDER names (1, then 2): a keyframe request carries no references,
|
||||
* so those slots are free, and ref_parts -- and the scatter -- read them back by the same
|
||||
* order. Keyframe rows are appended after every reference row, which is where ref_pack_of
|
||||
* reads the table's key block. */
|
||||
for (size_t i = 0; i < r->refs.size(); ++i) {
|
||||
const VideoRefMedia& m = r->refs[i];
|
||||
const bool first = m.kind == VideoRefMedia::FIRST_FRAME;
|
||||
if (!first && m.kind != VideoRefMedia::LAST_FRAME) continue;
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (bad_side(m))
|
||||
return no("'" + m.name + "' has encoder sides (" + std::to_string(m.enc_h) +
|
||||
"x" + std::to_string(m.enc_w) + ") the VAE grid of " +
|
||||
std::to_string(d.enc_ratio) + " does not divide");
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0); /* [n_refs, n_keys] */
|
||||
const int32_t slot = ++j->ref_tab[1]; /* keyframe order -> image slot 1, 2 */
|
||||
j->enc_q.push_back({RAD_ENC_IMAGE, slot, (int32_t)i});
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{first ? 0 : s.frames - 1, 1 /*one latent frame*/, 0 /*no soundtrack*/});
|
||||
}
|
||||
}
|
||||
/* The step's packed sequence. When the denoiser consumes the reference table the pack is the
|
||||
* only source of its rows, segment boundaries and positions -- the reference rows sit between
|
||||
@@ -640,8 +669,8 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
* chosen. Each reference the server decoded into encoder form rides one encode pass at the slot
|
||||
* its index numbers within its kind -- a picture or video a Vencode pass, a standalone audio and
|
||||
* every video's soundtrack an Aencode pass (a soundtrack at its video's slot, since the encoder
|
||||
* and the scatter both number it by its video). The keyframe count stays 0: continuation is
|
||||
* separate work. */
|
||||
* and the scatter both number it by its video). Endpoint keyframes ride the same queue as
|
||||
* picture anchors -- a Vencode pass at the image slot their keyframe order names. */
|
||||
/* The soundtrack's length, exactly the samples the decode will hand the encoder — the
|
||||
* number a caller that chains clips against an audio floor checks. A build without
|
||||
* libavcodec never gets the muxed file to read it back from, so it is logged here. */
|
||||
|
||||
@@ -1041,6 +1041,44 @@ int Processor::encoder_image(const uint8_t* bytes, size_t n, int32_t ratio, int3
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Processor::encoder_image_canvas(const uint8_t* bytes, size_t n, int32_t w, int32_t h,
|
||||
bool cover, Frame* out, std::string* why) const {
|
||||
if (w < 1 || h < 1) {
|
||||
if (why) *why = "the keyframe canvas is empty";
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
/* The same decode guard as encoder_image(): a keyframe the client sends is held to the size a
|
||||
* reference would be. */
|
||||
Frame f;
|
||||
RAD_TRY(decode_image(bytes, n, cfg_.max_source_pixels, &f, why));
|
||||
if (!cover) {
|
||||
/* "disabled": a plain stretch to the canvas, exactly the reference's first-frame resize. */
|
||||
if (w == f.w && h == f.h) { *out = std::move(f); return RAD_OK; }
|
||||
resize_lanczos_rgb(f, w, h, out);
|
||||
return RAD_OK;
|
||||
}
|
||||
/* "center": scale up to COVER the canvas keeping aspect (both axes at least the target), then
|
||||
* cut the exact canvas from the centre -- the reference's last-frame resize. The scale is the
|
||||
* larger of the two axis ratios so neither side falls short; each is rounded up to at least the
|
||||
* target so the crop always has enough to take from. */
|
||||
const double s = std::max((double)w / (double)f.w, (double)h / (double)f.h);
|
||||
int32_t cw = (int32_t)std::llround((double)f.w * s);
|
||||
int32_t ch = (int32_t)std::llround((double)f.h * s);
|
||||
if (cw < w) cw = w;
|
||||
if (ch < h) ch = h;
|
||||
Frame big;
|
||||
resize_lanczos_rgb(f, cw, ch, &big);
|
||||
const int32_t x0 = (cw - w) / 2, y0 = (ch - h) / 2;
|
||||
out->w = w;
|
||||
out->h = h;
|
||||
out->rgb.resize((size_t)w * h * 3);
|
||||
for (int32_t y = 0; y < h; ++y)
|
||||
for (int32_t x = 0; x < w * 3; ++x)
|
||||
out->rgb[(size_t)y * w * 3 + x] =
|
||||
big.rgb[(size_t)(y0 + y) * cw * 3 + (size_t)(x0 * 3 + x)];
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
namespace {
|
||||
/* Which frames of a source video a reference clip keeps: spread uniformly over the stream, the
|
||||
* largest count no more than both the stream's length and `max_frames` that is 5 modulo 17 — the
|
||||
|
||||
@@ -327,6 +327,17 @@ public:
|
||||
int encoder_image(const uint8_t* bytes, size_t n, int32_t ratio, int32_t max_side, Frame* out,
|
||||
std::string* why) const;
|
||||
|
||||
/* A keyframe anchor in the video VAE's own form: decoded under the same `max_source_pixels`
|
||||
* guard, then resized to the EXACT output canvas (`w` x `h`), unlike `encoder_image`'s
|
||||
* aspect-kept snap. A keyframe's pinned rows must cover the target's latent grid exactly -- it
|
||||
* is frame_rows wide, not the anchor's own grid -- so the anchor cannot keep its aspect: the
|
||||
* reference stretches the first frame to the canvas ("disabled") and cover-crops the last
|
||||
* ("center"). `cover` picks between them: false stretches both axes to fill the canvas (may
|
||||
* distort); true instead scales up to COVER the canvas keeping aspect, then centre-crops
|
||||
* the excess. `w`/`h` are the request's target size, already a multiple of the VAE ratio. */
|
||||
int encoder_image_canvas(const uint8_t* bytes, size_t n, int32_t w, int32_t h, bool cover,
|
||||
Frame* out, std::string* why) const;
|
||||
|
||||
/* A moving picture in the video VAE's own form: decoded under the same `max_source_pixels`
|
||||
* guard, then every frame resized to the one grid `encoder_image` picks. The frames are
|
||||
* SAMPLED first: uniformly across the stream, to the largest count no more than `max_frames`
|
||||
|
||||
@@ -719,6 +719,15 @@ public:
|
||||
return p_->encoder_image(bytes, n, ratio, max_side, out, why);
|
||||
}
|
||||
|
||||
int encoder_image_canvas(const uint8_t* bytes, size_t n, int32_t w, int32_t h, bool cover,
|
||||
mm::Frame* out, std::string* why) override {
|
||||
if (!p_ || !mm::decoder_available()) {
|
||||
if (why) *why = "no picture decoder is available";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
return p_->encoder_image_canvas(bytes, n, w, h, cover, out, why);
|
||||
}
|
||||
|
||||
int encoder_video(const uint8_t* bytes, size_t n, int32_t ratio, int32_t max_side,
|
||||
int32_t max_frames, std::vector<mm::Frame>* out, std::string* why) override {
|
||||
if (!p_ || !mm::decoder_available()) {
|
||||
|
||||
@@ -544,6 +544,17 @@ public:
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* A keyframe anchor in a video VAE's encoder-ready form: decoded under the same guard, then
|
||||
* resized to the EXACT `w` x `h` output canvas (`cover` selects a plain stretch or a centre
|
||||
* crop), not an aspect-kept snap -- a pinned keyframe's latent grid must be the target's. The
|
||||
* video generator's server uses it for first_frame/last_frame parts. */
|
||||
virtual int encoder_image_canvas(const uint8_t* bytes, size_t n, int32_t w, int32_t h,
|
||||
bool cover, mm::Frame* out, std::string* why) {
|
||||
(void)bytes; (void)n; (void)w; (void)h; (void)cover; (void)out;
|
||||
if (why) *why = "this server prepares no reference media for a video encoder";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* A moving picture in a video VAE's encoder-ready form: decoded under the same guard, sampled to
|
||||
* a clip-friendly frame count (at most `max_frames`) and resized to `encoder_image`'s grid. The
|
||||
* frames are the trimmed, sampled count; the caller clip-pads to a whole clip. */
|
||||
|
||||
+89
-7
@@ -272,6 +272,11 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
* since it may carry a file). A form may carry the reference media of a ref2va request as
|
||||
* named file parts; they are collected here and checked once the task is known. */
|
||||
std::vector<gen::VideoRefMedia> refs;
|
||||
/* Endpoint keyframes: the "first_frame"/"last_frame" file parts, kept apart from the reference
|
||||
* media because they are conditioned on differently (pinned rows, not reference rows) and are
|
||||
* validated and encoded on their own path below. At most one of each; together they are FL2VA,
|
||||
* alone they are I2VA (first) or L2VA (last). */
|
||||
std::vector<gen::VideoRefMedia> keys;
|
||||
json body = json::object();
|
||||
if (!req.form.empty()) {
|
||||
for (const FormPart& p : req.form) {
|
||||
@@ -281,6 +286,21 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
"ref_image_N file parts"
|
||||
: "this server makes video from text "
|
||||
"only"), req.peer);
|
||||
if (p.name == "first_frame" || p.name == "last_frame") {
|
||||
if (p.filename.empty())
|
||||
return error_response(err_bad(p.name, "'" + p.name + "' must carry an image "
|
||||
"file"), req.peer);
|
||||
gen::VideoRefMedia m;
|
||||
m.kind = p.name == "first_frame" ? gen::VideoRefMedia::FIRST_FRAME
|
||||
: gen::VideoRefMedia::LAST_FRAME;
|
||||
m.index = 1;
|
||||
m.name = p.name;
|
||||
m.filename = p.filename;
|
||||
m.content_type = p.content_type;
|
||||
m.data = p.data;
|
||||
keys.push_back(std::move(m));
|
||||
continue;
|
||||
}
|
||||
if (!p.filename.empty()) {
|
||||
gen::VideoRefMedia m;
|
||||
if (!parse_ref_name(p.name, &m.kind, &m.index))
|
||||
@@ -341,16 +361,26 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
req.peer);
|
||||
/* The task: text always; reference conditioning for a model that takes reference media. */
|
||||
bool ref2va = false;
|
||||
std::string kf_task; /* an explicitly named keyframe task (i2va / l2va / fl2va), if any */
|
||||
if (has(body, "task")) {
|
||||
const std::string task = body["task"].is_string() ? body["task"].get<std::string>() : "";
|
||||
if (task == "ref2va" && takes_refs) ref2va = true;
|
||||
/* The keyframe-completion tasks: a first and/or last frame pinned at the output's ends.
|
||||
* They are recognised whatever a model declares (any model with a video encoder can pin a
|
||||
* keyframe); which frames are actually present is decided by the file parts, cross-checked
|
||||
* against this name below. t2va with a first_frame part is I2VA; a named task missing its
|
||||
* frame is refused there. */
|
||||
else if (task == "i2va" || task == "l2va" || task == "fl2va") kf_task = task;
|
||||
else if (task != "t2va") {
|
||||
ApiError e = err_unsupported("task", takes_refs
|
||||
? "this server makes video from text or from reference "
|
||||
"media: t2va, ref2va"
|
||||
: "this server makes video from text: t2va");
|
||||
e.supported = takes_refs ? std::vector<std::string>{ "t2va", "ref2va" }
|
||||
: std::vector<std::string>{ "t2va" };
|
||||
? "this server makes video from text, from reference "
|
||||
"media, or between keyframes: t2va, ref2va, i2va, l2va, "
|
||||
"fl2va"
|
||||
: "this server makes video from text or between keyframes: "
|
||||
"t2va, i2va, l2va, fl2va");
|
||||
e.supported = takes_refs
|
||||
? std::vector<std::string>{ "t2va", "ref2va", "i2va", "l2va", "fl2va" }
|
||||
: std::vector<std::string>{ "t2va", "i2va", "l2va", "fl2va" };
|
||||
return error_response(e, req.peer);
|
||||
}
|
||||
}
|
||||
@@ -369,6 +399,36 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
ApiError e;
|
||||
if (validate_refs(refs, info, &e) < 0) return error_response(e, req.peer);
|
||||
}
|
||||
/* Endpoint keyframes: at most one of each, not alongside reference media (a request either
|
||||
* completes between two pinned frames or conditions on references -- the two conditioning
|
||||
* paths are different rows), and only on a model that encodes (a keyframe reaches the DiT as a
|
||||
* pinned VAE latent). */
|
||||
{
|
||||
int n_first = 0, n_last = 0;
|
||||
for (const gen::VideoRefMedia& m : keys)
|
||||
if (m.kind == gen::VideoRefMedia::FIRST_FRAME) ++n_first; else ++n_last;
|
||||
if (n_first > 1) return bad("first_frame", "at most one first_frame part");
|
||||
if (n_last > 1) return bad("last_frame", "at most one last_frame part");
|
||||
if (!keys.empty()) {
|
||||
if (!info.encodes)
|
||||
return bad("first_frame", "this model has no video encoder, so it cannot pin a "
|
||||
"keyframe");
|
||||
if (!refs.empty())
|
||||
return bad("first_frame", "a keyframe completion conditions on its pinned frames, "
|
||||
"not on reference media: give first_frame/last_frame or "
|
||||
"the ref_* parts, not both");
|
||||
}
|
||||
/* A named keyframe task must carry the frame(s) it promises; t2va with parts is fine, the
|
||||
* parts alone decide the mode. */
|
||||
if (!kf_task.empty()) {
|
||||
if ((kf_task == "i2va" || kf_task == "fl2va") && n_first < 1)
|
||||
return bad("first_frame", "task '" + kf_task + "' pins a first frame: give a "
|
||||
"first_frame part");
|
||||
if ((kf_task == "l2va" || kf_task == "fl2va") && n_last < 1)
|
||||
return bad("last_frame", "task '" + kf_task + "' pins a last frame: give a "
|
||||
"last_frame part");
|
||||
}
|
||||
}
|
||||
json target = json::object();
|
||||
if (has(body, "target")) {
|
||||
if (!body["target"].is_object()) return bad("target", "expected an object");
|
||||
@@ -495,6 +555,11 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
r->seed = (((uint64_t)rd() << 32) ^ rd()) & (((uint64_t)1 << 53) - 1);
|
||||
}
|
||||
r->refs = std::move(refs);
|
||||
/* Keyframes ride the reference vector for the driver's sake (its encode queue indexes it), but
|
||||
* always AFTER every reference: the driver lays the reference rows down first, then folds each
|
||||
* FIRST_FRAME/LAST_FRAME into the pack's keyframe table. first before last, so they keyframe-order
|
||||
* 0 and 1 and take image slots 1 and 2. */
|
||||
r->refs.insert(r->refs.end(), keys.begin(), keys.end());
|
||||
|
||||
/* ---- the prompt: the template's text with special tokens, the reference media in its
|
||||
* presentation order, the prompt last (docs/PORT-REF2VA.md §3: images, then videos each
|
||||
@@ -595,12 +660,29 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
* geometry, clip-pads a video's frames and zero-pads an audio's samples to what it reports. The
|
||||
* processor decodes under the same guard it used for the text encoder, so a reference that got
|
||||
* this far decodes here too. */
|
||||
if (ref2va && info.encodes && deps_.mm) {
|
||||
if ((ref2va || !keys.empty()) && info.encodes && deps_.mm) {
|
||||
for (gen::VideoRefMedia& m : r->refs) {
|
||||
std::string why;
|
||||
const uint8_t* p = (const uint8_t*)m.data.data();
|
||||
const size_t n = m.data.size();
|
||||
if (m.kind == gen::VideoRefMedia::IMAGE) {
|
||||
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. */
|
||||
mm::Frame fr;
|
||||
if (deps_.mm->encoder_image_canvas(p, n, (int32_t)W, (int32_t)H,
|
||||
m.kind == gen::VideoRefMedia::LAST_FRAME,
|
||||
&fr, &why) < 0)
|
||||
return bad(m.name.c_str(), "'" + m.name + "' could not be encoded: " + why);
|
||||
m.enc_frames = 1;
|
||||
m.enc_h = fr.h;
|
||||
m.enc_w = fr.w;
|
||||
m.enc_pixels.resize((size_t)fr.w * fr.h * 3);
|
||||
for (size_t i = 0; i < m.enc_pixels.size(); ++i)
|
||||
m.enc_pixels[i] = (float)fr.rgb[i];
|
||||
} else if (m.kind == gen::VideoRefMedia::IMAGE) {
|
||||
mm::Frame fr;
|
||||
if (deps_.mm->encoder_image(p, n, (int32_t)info.enc_ratio,
|
||||
(int32_t)info.max_enc_side, &fr, &why) < 0)
|
||||
|
||||
Reference in new issue
Block a user