ref2va: wire video and audio reference conditioning end-to-end (all three ref kinds live on device)

Follows the merged-cell pin (c725a2d). That commit proved reference IMAGE
conditioning consumed on device; reference VIDEO and AUDIO were plumbed
through the pack and step but still refused at submit — only their decode
and encoder-enqueue were missing. This lands them.

Server (videos.cpp): `task ref2va` now decodes `ref_video_N` and
`ref_audio_N` (and a video's paired `ref_video_audio_N`) into encoder form
via the mm encoders and enqueues them as Vencode/Aencode passes, instead of
the old image-only decode loop that rejected them. A soundtrack is folded
into its video's row (the `audio` word), matching how the pack lays that
pair. Caps stay <=9 images / <=3 videos / <=3 audios.

Driver (video_driver.{h,cpp}): the encode queue `enc_q` now carries VIDEO,
VAUDIO and AUDIO items in presentation order; `ref_tab` stamps each
reference's latent counts straight from the encoder geometry (rad_arch_encode_geom:
a video's frame count eg[24]=its total latent frames, its soundtrack's eg[12]),
so the pack's ref segments are sized by the encoder, not hand arithmetic.
processor.{h,cpp}/iface.h/adapters.h gained the media decode entry points.

Arch (minimaxh3.cpp): step_dit scatters reference rows into the packed step —
picture/video via op_vin from b_evc, audio via op_ain from b_enca, at each
segment start, targets last. A bounds audit confirms the scatter reads exactly
the slot region the encode pass wrote this job (eg[24]=lat = the pass's
written row count = the pack's segment size; no stale-neighbor read).

DEVICE-VERIFIED on dual gfx1201 / TP2, warm server, fixed seed, comparing
DECODED stream md5 (never raw mp4 bytes — the muxer stamps a field): all three
reference kinds are DETERMINISTIC (a rerun of one reference is bit-identical,
including raw bytes) AND CONTENT-LIVE (a different reference at the same seed
gives different output video AND audio). Image (content tokens), audio
(tone/noise/880 distinct) and video (refv_a vs refv_b: both streams differ)
each steered the generation. An earlier "the video step is nondeterministic"
note was a measurement artifact — it compared raw mp4 bytes and straddled the
one-off cold-start outlier; the warm decoded-stream probe is bit-reproducible.

Debug scaffolding (R4D_ENC_DEBUG, 4 trailing RadVideoGenDecl fields + a pinned
driver slab + an FNV digest in done()) is entangled in these files. It is
env-gated and zero-touch when unset; STRIP IT BEFORE ANY PR.

tests: video_gen_test/videos_test/server_fakes cover the video/audio enqueue
paths; abi_layout_rows adds the new trailing rows; ref_audio_sensitivity.py is
a standalone host golden harness (audio reference separates 4/4), not a ctest
case. Host tier green (only mm_test's pre-existing webp demuxer gap fails, and
the GPU card/kbench tests fail solely on VRAM contention with a live server).

NOTE-REF2VA.md updated: continuity tail (#8) remains unimplemented; strict
numeric parity vs the ComfyUI CPU reference remains open.

PLACEHOLDER AUTHOR — repo has no git identity; re-author before any push.
DO NOT PUSH this branch.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-19 15:25:32 -05:00
1 parent 72624973ad
commit 658f7b7214
16 files changed
+1055 -162

No files matched your search

+63
View File
@@ -1041,6 +1041,69 @@ int Processor::encoder_image(const uint8_t* bytes, size_t n, int32_t ratio, int3
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
* reference's trim (`n % 17 == 5`), so the temporal encoder's 17-frame clips all land with the
* short final clip padded from five real frames. A stream too short to reach five yields what it
* has; the encode pass pads the final clip either way. */
struct VaeSampleArgs { int32_t max_frames; };
std::vector<int64_t> vae_sample_cb(const VideoInfo& info, const void* user) {
const VaeSampleArgs* a = static_cast<const VaeSampleArgs*>(user);
const int64_t avail = info.n_frames > 0 ? info.n_frames : 1;
const int64_t cap = a->max_frames > 0 ? std::min<int64_t>(a->max_frames, avail) : avail;
int64_t want = cap - (((cap - 5) % 17) + 17) % 17; /* largest v <= cap with v % 17 == 5 */
if (want < 5) want = std::min<int64_t>(cap, 5);
const int64_t take = std::max<int64_t>(1, std::min(want, avail));
std::vector<int64_t> idx((size_t)take);
for (int64_t i = 0; i < take; ++i)
idx[(size_t)i] = take > 1 ? (i * (avail - 1)) / (take - 1) : 0;
return idx;
}
} /* namespace */
int Processor::encoder_video(const uint8_t* bytes, size_t n, int32_t ratio, int32_t max_side,
int32_t max_frames, std::vector<Frame>* out, std::string* why) const {
if (ratio < 1 || max_side < ratio) {
if (why) *why = "the encoder's side ratio and maximum are not usable";
return RAD_E_INVAL;
}
/* Same decode guard as a still: a reference the text encoder accepts the VAE accepts too. */
Video v;
const VaeSampleArgs a{ max_frames };
RAD_TRY(decode_video(bytes, n, vae_sample_cb, &a, cfg_.max_source_pixels, &v, why));
if (v.frames.empty()) {
if (why) *why = "the video holds no frames";
return RAD_E_INVAL;
}
/* One grid for every frame, the snap encoder_image applies to a still, taken from the first
* frame (a clip is one aspect throughout). */
auto snap = [&](int64_t s) {
int64_t v = (s / ratio) * ratio;
if (v < ratio) v = ratio;
if (v > max_side) v = max_side;
return (int32_t)v;
};
const Frame& f0 = v.frames.front();
const int32_t longest = std::max(f0.w, f0.h);
const double scale = longest > max_side ? (double)max_side / (double)longest : 1.0;
const int32_t w = snap((int64_t)std::floor(f0.w * scale));
const int32_t h = snap((int64_t)std::floor(f0.h * scale));
out->clear();
out->reserve(v.frames.size());
for (const Frame& f : v.frames) {
Frame r;
resize_lanczos_rgb(f, w, h, &r);
out->push_back(std::move(r));
}
return RAD_OK;
}
int Processor::encoder_audio(const uint8_t* bytes, size_t n, int32_t rate, int64_t max_samples,
std::vector<float>* out, std::string* why) const {
return decode_audio(bytes, n, rate, max_samples, out, why);
}
namespace {
struct SampleArgs { double fps; int32_t lo, hi; bool gemma4; };
std::vector<int64_t> sample_cb(const VideoInfo& info, const void* user) {