Archived
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:
1 parent
72624973ad
commit
658f7b7214
16 files changed
+1055
-162
No files matched your search
+11
-7
@@ -86,16 +86,20 @@ struct VideoRefMedia {
|
||||
std::string data; /* the file's bytes */
|
||||
|
||||
/* The reference in the encoder's own form, prepared by the server (which owns the decoder) so
|
||||
* the driver stages bytes without ever touching a codec. The VAE encode pass reads exactly
|
||||
* these; they are empty until the server decodes the part. IMAGE/VIDEO fill the pixels,
|
||||
* AUDIO/VIDEO_AUDIO the samples.
|
||||
* the driver stages bytes without ever touching a codec; they are empty until the server decodes
|
||||
* the part. These are the NATURAL decoded shapes — the driver's encode pass, which owns the
|
||||
* encoder's geometry, does the padding the encoder kernels demand (clip-padding a video's
|
||||
* frames, zero-padding an audio's samples). IMAGE/VIDEO fill the pixels, AUDIO/VIDEO_AUDIO the
|
||||
* samples.
|
||||
* enc_pixels: f32 [enc_frames][enc_h][enc_w][3] row-major RGB in 0..255, UN-normalised —
|
||||
* the video VAE applies ImageNet mean/std itself. Each side is snapped down to a multiple
|
||||
* of the VAE's spatial ratio and capped at the declaration's max_enc_side, aspect kept,
|
||||
* because our encoder runs one volume and cannot tile the way the reference does. For a
|
||||
* still image enc_frames is 1.
|
||||
* enc_samples: f32 [enc_channels][enc_n] channel-major, each channel right-padded with
|
||||
* zeros out to a whole number of hops (geom entry 0), at the model's audio rate. */
|
||||
* because our encoder runs one volume and cannot tile the way the reference does. Frames are
|
||||
* the reference's sampled, trimmed count (for a still image, 1); the final short clip is
|
||||
* padded by the driver, not here.
|
||||
* enc_samples: f32 [enc_channels][enc_n] channel-major at the model's audio rate. A mono
|
||||
* source is spread across the channels (the audio VAE encodes one channel at a time); the
|
||||
* tail is right-padded with zeros to a whole hop by the driver. */
|
||||
std::vector<float> enc_pixels;
|
||||
int32_t enc_frames = 0, enc_h = 0, enc_w = 0;
|
||||
std::vector<float> enc_samples;
|
||||
|
||||
+210
-58
@@ -6,6 +6,8 @@
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
namespace rad {
|
||||
@@ -282,6 +284,18 @@ int VideoDriver::init(const Program& prog, PositionsFn positions,
|
||||
vout_.assign(ranks_.size(), RadVideoGenBatch{});
|
||||
free_lanes_.clear();
|
||||
for (int64_t i = decl_.max_lanes - 1; i >= 0; --i) free_lanes_.push_back((int32_t)i);
|
||||
/* Debug-only readback of the encoder outputs (R4D_ENC_DEBUG). Only when the environment names
|
||||
* it AND the container carries encoders; a normal run leaves enc_dbg_ false and never reads
|
||||
* decl_.dbg_* or touches enc_dbg_host_. */
|
||||
if (std::getenv("R4D_ENC_DEBUG") &&
|
||||
(decl_.dbg_enca_bytes > 0 || decl_.dbg_evc_bytes > 0)) {
|
||||
enc_dbg_bytes_ = std::max(decl_.dbg_enca_bytes, decl_.dbg_evc_bytes);
|
||||
enc_dbg_host_ = static_cast<char*>(rad_dev_alloc(enc_dbg_bytes_, RAD_MEM_HOST_PINNED));
|
||||
enc_dbg_ = enc_dbg_host_ != nullptr;
|
||||
if (!enc_dbg_)
|
||||
RAD_ERR("video generator: R4D_ENC_DEBUG set but no pinned host for the %lld-byte "
|
||||
"encoder readback", (long long)enc_dbg_bytes_);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -296,9 +310,11 @@ void VideoDriver::fini() {
|
||||
if (host_) rad_dev_free(host_, RAD_MEM_HOST_PINNED);
|
||||
if (frames_host_) rad_dev_free(frames_host_, RAD_MEM_HOST_PINNED);
|
||||
if (audio_host_) rad_dev_free(audio_host_, RAD_MEM_HOST_PINNED);
|
||||
if (enc_dbg_host_) rad_dev_free(enc_dbg_host_, RAD_MEM_HOST_PINNED);
|
||||
host_ = nullptr;
|
||||
frames_host_ = nullptr;
|
||||
audio_host_ = nullptr;
|
||||
enc_dbg_host_ = nullptr;
|
||||
}
|
||||
|
||||
/* ================================================================== requests */
|
||||
@@ -313,20 +329,37 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
if (T < 1 || T > d.max_text)
|
||||
return no("the prompt is " + std::to_string(T) + " tokens; this model takes 1.." +
|
||||
std::to_string(d.max_text));
|
||||
/* Reference conditioning: the images reach the language model through `media`, which the
|
||||
* server built from the parts (gen/video.h) and build_cond stages into the conditioning
|
||||
* pass. What this generator cannot consume yet is anything whose bytes reach the model
|
||||
* through a VAE — a reference video's frames, a soundtrack, a standalone audio. Those are
|
||||
* refused naming the part, rather than silently made into a weaker video. */
|
||||
/* Reference conditioning reaches the model two ways. Reference images enter the language model
|
||||
* through `media` (the server built it from the parts, gen/video.h; build_cond stages it into
|
||||
* the conditioning pass) AND contribute VAE rows. A reference video, its soundtrack, and a
|
||||
* standalone audio reach the model only as VAE rows: the server decodes them into the encoder's
|
||||
* own form, one encode pass each folds their latents into the lane's fixed slots, and the step's
|
||||
* pack lays those slots between the text and the target streams (RadVideoGenBatch::ref_tab).
|
||||
* Every kind the declaration allows is consumed; the guard below is only about a container
|
||||
* lacking its vision tower, which could encode nothing. */
|
||||
if (!r->refs.empty() && max_enc_ == 0)
|
||||
return no("this container was converted without its vision tower, so no request can "
|
||||
"carry reference media");
|
||||
/* A reference video, soundtrack or standalone audio reaches the model only through the VAE
|
||||
* encoders (the video/audio Vencode/Aencode passes), which a container carries exactly when it
|
||||
* declares `encodes`. Converted without them there is nothing to condition on, and the reference
|
||||
* would drop silently into a text-only video — the very silent weakening this guard exists to
|
||||
* prevent — so refuse it, naming the part. Images need only the tower and are checked below. */
|
||||
if (!d.encodes)
|
||||
for (const VideoRefMedia& m : r->refs)
|
||||
if (m.kind != VideoRefMedia::IMAGE)
|
||||
return no("'" + m.name + "' is a reference " +
|
||||
(m.kind == VideoRefMedia::VIDEO ? "video"
|
||||
: m.kind == VideoRefMedia::VIDEO_AUDIO ? "soundtrack" : "audio") +
|
||||
", but this container was converted without its VAE encoders, so nothing "
|
||||
"can condition on it");
|
||||
/* Only reference images add language-model soft-token rows, so only they need processed media.
|
||||
* A reference video or audio reaches the model purely through the VAE encode rows the pack lays
|
||||
* from the reference table, so a request of those alone carries no `media` and needs none. */
|
||||
bool has_image = false;
|
||||
for (const VideoRefMedia& m : r->refs)
|
||||
if (m.kind != VideoRefMedia::IMAGE)
|
||||
return no("'" + m.name + "' is not consumed yet: reference videos and audios reach "
|
||||
"the model through the video and audio VAEs, which the video path does not "
|
||||
"run in this build; only ref_image_N parts condition a generation today");
|
||||
if (!r->refs.empty() && !r->media)
|
||||
if (m.kind == VideoRefMedia::IMAGE) has_image = true;
|
||||
if (has_image && !r->media)
|
||||
return no("the request carries reference images but no processed media to condition on");
|
||||
if (r->media) {
|
||||
int64_t n_patch = 0;
|
||||
@@ -369,33 +402,105 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
j->geom.n_audio = s.n_audio;
|
||||
/* The pack's reference table (RadVideoGenBatch::ref_tab): every reference the DiT conditions
|
||||
* on, in presentation order, in ITS OWN latent grid -- {kind, slot, h, w, frames, audio} six
|
||||
* words after the reference and keyframe counts. A picture's grid is the encoder's sides over
|
||||
* the VAE ratio, one latent frame, no soundtrack; its slot is the one its Vencode pass folds
|
||||
* its latents into. It must exist before the step layout is decided, because when the denoiser
|
||||
* consumes it the reference rows are part of that layout. Only pictures queue today (videos and
|
||||
* audios are refused at submit above until their decode paths land), so the keyframe count
|
||||
* stays 0 and the table holds only picture rows. */
|
||||
if (d.encodes)
|
||||
* words after the reference and keyframe counts. `kind` is a pack Ref2VARefKind (picture 0,
|
||||
* standalone audio 1, video 2), the server's VIDEO_AUDIO being folded into its video's `audio`
|
||||
* word rather than a row of its own. Each reference's slot is the one its encode pass folds its
|
||||
* latents into, and its latent counts come from the encoder's own geometry, not hand arithmetic:
|
||||
* a picture is one latent frame (its grid the encoder's sides over the VAE ratio); a video's
|
||||
* 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. */
|
||||
if (d.encodes) {
|
||||
bool need_geom = false;
|
||||
for (const VideoRefMedia& m : r->refs)
|
||||
if (m.kind != VideoRefMedia::IMAGE) 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");
|
||||
int32_t eg[25];
|
||||
/* A reference's latent frame counts, straight from the encoder's geometry. */
|
||||
auto video_lat = [&](const VideoRefMedia& m) -> int64_t {
|
||||
int64_t in[4] = { m.enc_frames, m.enc_h, m.enc_w, 3 };
|
||||
return enc_geom_(RAD_GEN_PASS_VENCODE, in, eg, 25) < 0 ? -1 : (int64_t)eg[24];
|
||||
};
|
||||
auto audio_lat = [&](const VideoRefMedia& m) -> int64_t {
|
||||
int64_t in[4] = { m.enc_n, 0, 0, m.enc_channels };
|
||||
return enc_geom_(RAD_GEN_PASS_AENCODE, in, eg, 25) < 0 ? -1 : (int64_t)eg[12];
|
||||
};
|
||||
/* Pictures and videos share a side-grid check against the VAE ratio. */
|
||||
auto bad_side = [&](const VideoRefMedia& m) {
|
||||
return d.enc_ratio < 1 || m.enc_h < d.enc_ratio || m.enc_w < d.enc_ratio ||
|
||||
m.enc_h % d.enc_ratio || m.enc_w % d.enc_ratio;
|
||||
};
|
||||
for (size_t i = 0; i < r->refs.size(); ++i) {
|
||||
const VideoRefMedia& m = r->refs[i];
|
||||
if (m.kind != VideoRefMedia::IMAGE) continue;
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (m.index < 1)
|
||||
return no("'" + m.name + "' has no slot in the conditioning");
|
||||
if (d.enc_ratio < 1 || m.enc_h < d.enc_ratio || m.enc_w < d.enc_ratio ||
|
||||
m.enc_h % d.enc_ratio || m.enc_w % d.enc_ratio)
|
||||
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");
|
||||
j->enc_q.push_back({RAD_ENC_IMAGE, m.index, (int32_t)i});
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0); /* [n_refs, n_keys] */
|
||||
++j->ref_tab[0];
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{0 /*a picture*/, m.index, (int32_t)(m.enc_h / d.enc_ratio),
|
||||
(int32_t)(m.enc_w / d.enc_ratio), 1 /*one latent frame*/,
|
||||
0 /*no soundtrack*/});
|
||||
if (m.kind == VideoRefMedia::IMAGE) {
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (m.index < 1)
|
||||
return no("'" + m.name + "' has no slot in the conditioning");
|
||||
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");
|
||||
j->enc_q.push_back({RAD_ENC_IMAGE, m.index, (int32_t)i});
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0); /* [n_refs, n_keys] */
|
||||
++j->ref_tab[0];
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{0 /*a picture*/, m.index, (int32_t)(m.enc_h / d.enc_ratio),
|
||||
(int32_t)(m.enc_w / d.enc_ratio), 1 /*one latent frame*/,
|
||||
0 /*no soundtrack*/});
|
||||
} else if (m.kind == VideoRefMedia::VIDEO) {
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (m.index < 1 || m.index > 3)
|
||||
return no("'" + m.name + "' has no video slot (1..3)");
|
||||
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");
|
||||
/* A soundtrack given for this video (ref_video_audio_N) is folded into THIS row's
|
||||
* `audio` word, not a row of its own; the pack lays its block right before the
|
||||
* video's and the scatter reads the two back as the pair they are. It is encoded at
|
||||
* the VIDEO's slot, since aenc_slot_row numbers a soundtrack by its video. */
|
||||
int64_t a_t = 0;
|
||||
for (size_t k = 0; k < r->refs.size(); ++k) {
|
||||
const VideoRefMedia& a = r->refs[k];
|
||||
if (a.kind != VideoRefMedia::VIDEO_AUDIO || a.index != m.index) continue;
|
||||
if (a.enc_samples.empty() || a.enc_n < 1 || a.enc_channels < 1)
|
||||
return no("'" + a.name + "' (the soundtrack of '" + m.name +
|
||||
"') was not prepared for the audio encoder");
|
||||
a_t = audio_lat(a);
|
||||
if (a_t < 1)
|
||||
return no("the audio encoder refuses the soundtrack '" + a.name + "'");
|
||||
j->enc_q.push_back({RAD_ENC_VAUDIO, m.index, (int32_t)k});
|
||||
}
|
||||
const int64_t v_t = video_lat(m);
|
||||
if (v_t < 1) return no("the video encoder refuses '" + m.name + "'");
|
||||
j->enc_q.push_back({RAD_ENC_VIDEO, m.index, (int32_t)i});
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0);
|
||||
++j->ref_tab[0];
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{2 /*a video*/, m.index, (int32_t)(m.enc_h / d.enc_ratio),
|
||||
(int32_t)(m.enc_w / d.enc_ratio), (int32_t)v_t, (int32_t)a_t});
|
||||
} else if (m.kind == VideoRefMedia::AUDIO) {
|
||||
if (m.enc_samples.empty() || m.enc_n < 1 || m.enc_channels < 1)
|
||||
return no("'" + m.name + "' was not prepared for the audio encoder");
|
||||
if (m.index < 1 || m.index > 3)
|
||||
return no("'" + m.name + "' has no audio slot (1..3)");
|
||||
const int64_t a_t = audio_lat(m);
|
||||
if (a_t < 1) return no("the audio encoder refuses '" + m.name + "'");
|
||||
j->enc_q.push_back({RAD_ENC_AUDIO, m.index, (int32_t)i});
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0);
|
||||
++j->ref_tab[0];
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{1 /*a standalone audio*/, m.index, 0, 0, 0, (int32_t)a_t});
|
||||
}
|
||||
/* VIDEO_AUDIO rows are folded into their video above, so they take no row here. */
|
||||
}
|
||||
}
|
||||
/* 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
|
||||
* the text and the target streams, so the plain `text + audio + video` arithmetic and the plain
|
||||
@@ -525,10 +630,11 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
j->n_chunks = r->chunks;
|
||||
j->n_samples = (int64_t)s.n_audio * (d.sample_rate / d.audio_rate);
|
||||
/* The reference table and the VAE encode queue were built above, before the step layout was
|
||||
* chosen: each reference picture the server decoded into encoder form rides one Vencode pass,
|
||||
* at the slot its index numbers within pictures -- the slot the encoder folds its latents into
|
||||
* and the step's pack scatters them from. Reference videos and audios are refused above until
|
||||
* their decode paths land, so only pictures are queued and the keyframe count stays 0. */
|
||||
* 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. */
|
||||
/* 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. */
|
||||
@@ -1012,10 +1118,10 @@ const RadBatch* VideoDriver::build_chunk(Job& j, int step) {
|
||||
|
||||
/* One media item through its VAE encoder: stage its encoder-ready bytes and the encode geometry
|
||||
* the plugin's rad_arch_encode_geom derives from them, then name the pass's kind and slot so the
|
||||
* encoder folds the latents into the lane's fixed slot rows. The bytes the server decoded must
|
||||
* already be padded to what the geom reports (a video's frames clip-padded, an audio's samples to
|
||||
* its hop) — the encoder reads that many and no more, so a disagreement is failed here rather than
|
||||
* run past the staged volume. */
|
||||
* encoder folds the latents into the lane's fixed slot rows. The server sends the NATURAL decoded
|
||||
* media; the padding the geom demands (a video's frames to a whole clip, an audio's samples to a
|
||||
* whole hop) is applied here, since only the driver has the geom, and the encoder is handed exactly
|
||||
* that padded volume. */
|
||||
const RadBatch* VideoDriver::build_encode(Job& j, int step) {
|
||||
for (int g = 0; g < R_N; ++g) reg_[g].used = 0;
|
||||
const Job::EncItem& it = j.enc_q[j.enc_next];
|
||||
@@ -1028,34 +1134,50 @@ const RadBatch* VideoDriver::build_encode(Job& j, int step) {
|
||||
if (!enc_geom_)
|
||||
return fail("the request carries reference media to encode, but this plugin exports no "
|
||||
"rad_arch_encode_geom");
|
||||
/* The encoder kernels recompute their geometry from the batch's frame / sample counts and
|
||||
* require them to be the padded figures rad_arch_encode_geom reports (a video's frames out to a
|
||||
* whole clip, an audio's samples out to a whole hop). The server sends the NATURAL decoded
|
||||
* media, so the padding lives here, where the geometry is known: a video's short final clip is
|
||||
* filled by repeating its last frame, an audio's tail by zeros. A single picture is already
|
||||
* clip- and hop-complete (one frame, no hop), so its bytes and staging are unchanged. */
|
||||
int32_t* gd = hptr<int32_t>(R_ENCG);
|
||||
int64_t stage_frames = 0, stage_samples = 0;
|
||||
if (audio) {
|
||||
const int64_t n = m.enc_n;
|
||||
const int32_t ch = m.enc_channels;
|
||||
if (n <= 0 || ch <= 0 || (int64_t)n * ch * 4 > reg_[R_ENCSM].bytes)
|
||||
return fail("a reference audio does not fit one encode pass");
|
||||
if (n <= 0 || ch <= 0) return fail("a reference audio has no samples or no channels");
|
||||
int64_t in[4] = { n, 0, 0, ch };
|
||||
if (enc_geom_(RAD_GEN_PASS_AENCODE, in, gd, 25) < 0)
|
||||
return fail("the audio encoder refuses " + std::to_string(n) + " samples over " +
|
||||
std::to_string(ch) + " channels");
|
||||
if ((int64_t)gd[0] != n)
|
||||
return fail("a reference audio is not padded to the length rad_arch_encode_geom "
|
||||
"reports");
|
||||
std::memcpy(hptr<float>(R_ENCSM), m.enc_samples.data(), (size_t)n * ch * 4);
|
||||
reg_[R_ENCSM].used = n * ch * 4;
|
||||
stage_samples = gd[0]; /* n rounded up to a whole hop; the encoder reads exactly this */
|
||||
if (stage_samples * ch * 4 > reg_[R_ENCSM].bytes)
|
||||
return fail("a reference audio does not fit one encode pass");
|
||||
float* sm = hptr<float>(R_ENCSM);
|
||||
std::memset(sm, 0, (size_t)stage_samples * ch * 4); /* right-pad each channel with zeros */
|
||||
for (int32_t c = 0; c < ch; ++c)
|
||||
std::memcpy(sm + (size_t)c * stage_samples, m.enc_samples.data() + (size_t)c * n,
|
||||
(size_t)n * 4);
|
||||
reg_[R_ENCSM].used = stage_samples * ch * 4;
|
||||
} else {
|
||||
const int64_t nf = m.enc_frames, H = m.enc_h, W = m.enc_w;
|
||||
if (nf <= 0 || H <= 0 || W <= 0 || nf * H * W * 3 * 4 > reg_[R_ENCPX].bytes)
|
||||
return fail("a reference picture or video does not fit one encode pass");
|
||||
if (nf <= 0 || H <= 0 || W <= 0)
|
||||
return fail("a reference picture or video has no frames or no sides");
|
||||
int64_t in[4] = { nf, H, W, 3 };
|
||||
if (enc_geom_(RAD_GEN_PASS_VENCODE, in, gd, 25) < 0)
|
||||
return fail("the video encoder refuses a reference at " + std::to_string(H) + "x" +
|
||||
std::to_string(W) + " over " + std::to_string(nf) + " frames");
|
||||
if ((int64_t)gd[23] != nf)
|
||||
return fail("a reference is not padded to the clip count rad_arch_encode_geom "
|
||||
"reports");
|
||||
std::memcpy(hptr<float>(R_ENCPX), m.enc_pixels.data(), (size_t)nf * H * W * 3 * 4);
|
||||
reg_[R_ENCPX].used = nf * H * W * 3 * 4;
|
||||
const int64_t area = H * W * 3;
|
||||
stage_frames = gd[23]; /* nf clip-padded to a whole number of 17-frame clips */
|
||||
if (stage_frames * area * 4 > reg_[R_ENCPX].bytes)
|
||||
return fail("a reference picture or video does not fit one encode pass");
|
||||
float* px = hptr<float>(R_ENCPX);
|
||||
std::memcpy(px, m.enc_pixels.data(), (size_t)nf * area * 4);
|
||||
/* Repeat the final frame out to the padded clip count: the causal encoder wants every clip
|
||||
* full, and the reference pads the short last clip exactly this way. */
|
||||
for (int64_t f = nf; f < stage_frames; ++f)
|
||||
std::memcpy(px + f * area, px + (nf - 1) * area, (size_t)area * 4);
|
||||
reg_[R_ENCPX].used = stage_frames * area * 4;
|
||||
}
|
||||
reg_[R_ENCG].used = 25 * 4;
|
||||
stage_geom(j, j.geom, j.rows, 0);
|
||||
@@ -1069,11 +1191,11 @@ const RadBatch* VideoDriver::build_encode(Job& j, int step) {
|
||||
V.enc_slot = it.slot;
|
||||
if (audio) {
|
||||
V.enc_samples = dptr<float>(ri, R_ENCSM);
|
||||
V.enc_n_samples = m.enc_n;
|
||||
V.enc_n_samples = stage_samples;
|
||||
V.enc_channels = m.enc_channels;
|
||||
} else {
|
||||
V.enc_pixels = dptr<float>(ri, R_ENCPX);
|
||||
V.enc_frames = m.enc_frames;
|
||||
V.enc_frames = stage_frames;
|
||||
V.enc_height = m.enc_h;
|
||||
V.enc_width = m.enc_w;
|
||||
}
|
||||
@@ -1082,6 +1204,15 @@ const RadBatch* VideoDriver::build_encode(Job& j, int step) {
|
||||
for (auto& B : out_) B.gen = kind;
|
||||
pass_.kind = kind;
|
||||
pass_.job = j.id;
|
||||
if (enc_dbg_) {
|
||||
const rad_buf h = audio ? decl_.dbg_enca : decl_.dbg_evc;
|
||||
const int64_t n = audio ? decl_.dbg_enca_bytes : decl_.dbg_evc_bytes;
|
||||
enc_dbg_used_ = 0;
|
||||
if (h && n > 0) {
|
||||
rb_.assign(1, Readback{h, n, enc_dbg_host_});
|
||||
enc_dbg_used_ = n;
|
||||
}
|
||||
}
|
||||
return &out_[0];
|
||||
}
|
||||
|
||||
@@ -1097,6 +1228,27 @@ int VideoDriver::done() {
|
||||
case RAD_GEN_PASS_VENCODE:
|
||||
case RAD_GEN_PASS_AENCODE:
|
||||
/* The media's latents are now in the lane's slot rows; take the next reference, if any. */
|
||||
if (enc_dbg_ && enc_dbg_used_ > 0) {
|
||||
const int64_t nf = enc_dbg_used_ / 4;
|
||||
const float* z = reinterpret_cast<const float*>(enc_dbg_host_);
|
||||
uint64_t h = 1469598103934665603ULL; /* FNV-1a over the readback bytes */
|
||||
for (int64_t i = 0; i < enc_dbg_used_; ++i)
|
||||
h = (h ^ (uint64_t)(uint8_t)enc_dbg_host_[i]) * 1099511628211ULL;
|
||||
int64_t nz = 0;
|
||||
float mx = 0.0f;
|
||||
double sum = 0.0;
|
||||
for (int64_t i = 0; i < nf; ++i) {
|
||||
if (z[i] != 0.0f) ++nz;
|
||||
const float a = z[i] < 0 ? -z[i] : z[i];
|
||||
if (a > mx) mx = a;
|
||||
sum += z[i];
|
||||
}
|
||||
RAD_ERR("enc_dbg %s slot %d: fnv %016llx floats %lld nonzero %lld maxabs %.6g "
|
||||
"sum %.6g head[%.6g %.6g %.6g %.6g]",
|
||||
pass_.kind == RAD_GEN_PASS_AENCODE ? "AENCA(b_enca)" : "VENCV(b_evc)",
|
||||
(int)enc_dbg_used_, (unsigned long long)h, (long long)nf, (long long)nz,
|
||||
(double)mx, sum, (double)z[0], (double)z[1], (double)z[2], (double)z[3]);
|
||||
}
|
||||
++j->enc_next;
|
||||
break;
|
||||
case RAD_GEN_PASS_VSTEP: {
|
||||
|
||||
@@ -216,6 +216,15 @@ private:
|
||||
int64_t audio_bytes_ = 0;
|
||||
std::vector<Readback> rb_;
|
||||
|
||||
/* Debug-only (R4D_ENC_DEBUG): a pinned slab the encoder's audio / video latent buffer is read
|
||||
* back into after a Vencode / Aencode pass, so done() can log a digest. Allocated and used
|
||||
* only when the environment is set and the generator named its encoders; otherwise enc_dbg_
|
||||
* is false and these stay null / zero, so a normal request never touches them. */
|
||||
bool enc_dbg_ = false;
|
||||
char* enc_dbg_host_ = nullptr;
|
||||
int64_t enc_dbg_bytes_ = 0; /* the slab's capacity */
|
||||
int64_t enc_dbg_used_ = 0; /* bytes the last encode readback filled */
|
||||
|
||||
std::vector<RadBatch> out_; /* one per rank */
|
||||
std::vector<RadVideoGenBatch> vout_;
|
||||
Pass pass_;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -327,6 +327,25 @@ 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 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`
|
||||
* (the declaration's max_enc_frames) that leaves the short final clip at 5 frames — the
|
||||
* reference trims a reference video to `n % 17 == 5` so its 17-frame clips each contribute a
|
||||
* full five latents. The returned count is that trimmed length, NOT clip-padded; the caller
|
||||
* (the driver's encode pass) repeats the final frame out to a whole clip. `out` is empty only
|
||||
* on error. */
|
||||
int 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;
|
||||
|
||||
/* A clip in the audio VAE's own form: its first stream as MONO float at `rate` Hz, truncated
|
||||
* to `max_samples`. The caller duplicates it across the encoder's channels and zero-pads each
|
||||
* to a whole hop (the encoder runs one channel at a time). This is a thin wrapper on the media
|
||||
* decoder, kept here so every reference the server hands the VAEs is decoded under the
|
||||
* processor. */
|
||||
int encoder_audio(const uint8_t* bytes, size_t n, int32_t rate, int64_t max_samples,
|
||||
std::vector<float>* out, std::string* why) const;
|
||||
|
||||
/* Audio, when the program declares an audio encoder; `merge` and `max_frames` are the
|
||||
* encoder's. */
|
||||
int init_audio(const AudioConfig& cfg, std::string* why = nullptr);
|
||||
|
||||
@@ -719,6 +719,24 @@ public:
|
||||
return p_->encoder_image(bytes, n, ratio, max_side, 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()) {
|
||||
if (why) *why = "no video decoder is available";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
return p_->encoder_video(bytes, n, ratio, max_side, max_frames, out, why);
|
||||
}
|
||||
|
||||
int encoder_audio(const uint8_t* bytes, size_t n, int32_t rate, int64_t max_samples,
|
||||
std::vector<float>* out, std::string* why) override {
|
||||
if (!p_ || !mm::decoder_available()) {
|
||||
if (why) *why = "no audio decoder is available";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
return p_->encoder_audio(bytes, n, rate, max_samples, out, why);
|
||||
}
|
||||
|
||||
int32_t audio_rate() const override {
|
||||
return accepts(MediaKind::Audio) ? p_->audio_config().rate : 0;
|
||||
}
|
||||
|
||||
@@ -544,6 +544,26 @@ public:
|
||||
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. */
|
||||
virtual 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) {
|
||||
(void)bytes; (void)n; (void)ratio; (void)max_side; (void)max_frames; (void)out;
|
||||
if (why) *why = "this server prepares no reference media for a video encoder";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* A clip in an audio VAE's encoder-ready form: its first stream as MONO float at `rate` Hz,
|
||||
* truncated to `max_samples`. The caller spreads it across the encoder's channels and zero-pads
|
||||
* to a hop. */
|
||||
virtual int encoder_audio(const uint8_t* bytes, size_t n, int32_t rate, int64_t max_samples,
|
||||
std::vector<float>* out, std::string* why) {
|
||||
(void)bytes; (void)n; (void)rate; (void)max_samples; (void)out;
|
||||
if (why) *why = "this server prepares no reference media for an audio encoder";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* Audio in two halves, for a route that cuts a long recording into clips itself
|
||||
* (/v1/audio/transcriptions): the file decoded to mono samples at audio_rate(), at most
|
||||
* `max_samples` of them (RAD_E_INVAL past that, never a silent cut), and a clip of at most
|
||||
|
||||
+101
-46
@@ -511,14 +511,19 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
r->tokens.insert(r->tokens.end(), ids.begin(), ids.end());
|
||||
};
|
||||
/* The reference media are presented between the template's head and the prompt. Only the
|
||||
* images reach the language model, so only they are decoded and processed here; a
|
||||
* reference video's frames and every audio ride the video and audio VAE encoders, which
|
||||
* the video path does not run in this build yet, so their parts pass through as bytes for
|
||||
* the generator to refuse at submit naming the part — not silently dropped. (When videos
|
||||
* land, a soundtrack's `<Audio j>` label is emitted right before its `<Video k>` label,
|
||||
* the audio ordinal counting soundtracks and standalone audios together, per type.) */
|
||||
* images reach the language model as soft-token rows, so only they are decoded and placed
|
||||
* here; a reference video and every audio reach the model through the VAEs (decoded into the
|
||||
* encoder's form below), so in the prompt they contribute only their spoken label. That
|
||||
* label still rides ahead of the prompt in the reference's presentation order, because the
|
||||
* text encoder sees `<Video k>` / `<Audio j>` markers even for media it only names: a
|
||||
* soundtrack's `<Audio j>` label is emitted right before its video's `<Video k>` label, and
|
||||
* `audio_ord` counts soundtracks and standalone audios together, in the order they are
|
||||
* spoken (docs/PORT-REF2VA.md §3). */
|
||||
if (ref2va && deps_.mm && deps_.mm->accepts(MediaKind::Image)) {
|
||||
/* Every picture is decoded and processed before any token is laid down. */
|
||||
/* Every picture is decoded and processed before any token is laid down. A reference
|
||||
* video or audio needs no vision item here — it reaches the model through the VAEs, so
|
||||
* it contributes only its presentation label to the prompt (the 2 fps timestamped
|
||||
* frames the reference also shows Qwen are deferred; see docs/NOTE-REF2VA.md). */
|
||||
std::vector<std::shared_ptr<mm::Item>> pics;
|
||||
MediaOpts mo;
|
||||
mo.max_pixels = (int64_t)W * (int64_t)H; /* `match`: scaled to the output's area */
|
||||
@@ -531,27 +536,42 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
return bad(m.name.c_str(), "'" + m.name + "' could not be used: " + why);
|
||||
pics.push_back(std::move(it));
|
||||
}
|
||||
/* validate_refs numbered each kind 1..n without gaps, so a part's number is its
|
||||
* presentation ordinal, and paired every soundtrack to a video of the same number. The
|
||||
* reference presentation order (docs/PORT-REF2VA.md §3) is: every picture, then each
|
||||
* video behind its soundtrack's label, then the standalone audios. `<Audio j>` counts
|
||||
* soundtracks and standalone audios together, in the order they are spoken. */
|
||||
auto has_soundtrack = [&](int32_t idx) {
|
||||
for (const gen::VideoRefMedia& m : r->refs)
|
||||
if (m.kind == gen::VideoRefMedia::VIDEO_AUDIO && m.index == idx) return true;
|
||||
return false;
|
||||
};
|
||||
put(t.substr(0, p), true);
|
||||
if (!pics.empty()) {
|
||||
auto media = std::make_shared<mm::PromptMedia>();
|
||||
size_t i = 0;
|
||||
for (const gen::VideoRefMedia& m : r->refs) {
|
||||
if (m.kind != gen::VideoRefMedia::IMAGE) continue;
|
||||
/* validate_refs numbered each kind 1..n without gaps, so the part's number is
|
||||
* its presentation ordinal: `<Picture k>: ` then the picture's token run. The
|
||||
* label is encoded as its own string, as the reference tokenizer's output was
|
||||
* built. */
|
||||
put("<Picture " + std::to_string(m.index) + ">: ", false);
|
||||
mm::Placed pl;
|
||||
pl.item = pics[i++];
|
||||
pl.tok = (int32_t)r->tokens.size();
|
||||
r->tokens.insert(r->tokens.end(), pl.item->expand.begin(),
|
||||
pl.item->expand.end());
|
||||
media->items.push_back(std::move(pl));
|
||||
}
|
||||
media->layout();
|
||||
r->media = std::move(media);
|
||||
std::shared_ptr<mm::PromptMedia> media;
|
||||
if (!pics.empty()) media = std::make_shared<mm::PromptMedia>();
|
||||
size_t i = 0;
|
||||
for (const gen::VideoRefMedia& m : r->refs) { /* sorted by (kind, index): images first */
|
||||
if (m.kind != gen::VideoRefMedia::IMAGE) continue;
|
||||
put("<Picture " + std::to_string(m.index) + ">: ", false);
|
||||
mm::Placed pl;
|
||||
pl.item = pics[i++];
|
||||
pl.tok = (int32_t)r->tokens.size();
|
||||
r->tokens.insert(r->tokens.end(), pl.item->expand.begin(),
|
||||
pl.item->expand.end());
|
||||
media->items.push_back(std::move(pl));
|
||||
}
|
||||
int audio_ord = 0;
|
||||
for (const gen::VideoRefMedia& m : r->refs) {
|
||||
if (m.kind != gen::VideoRefMedia::VIDEO) continue;
|
||||
if (has_soundtrack(m.index))
|
||||
put("<Audio " + std::to_string(++audio_ord) + ">: ", false);
|
||||
put("<Video " + std::to_string(m.index) + ">: ", false);
|
||||
}
|
||||
for (const gen::VideoRefMedia& m : r->refs) {
|
||||
if (m.kind != gen::VideoRefMedia::AUDIO) continue;
|
||||
put("<Audio " + std::to_string(++audio_ord) + ">: ", false);
|
||||
}
|
||||
if (media) { media->layout(); r->media = std::move(media); }
|
||||
put(prompt, false);
|
||||
put(t.substr(p + kSlot.size()), true);
|
||||
} else {
|
||||
@@ -567,29 +587,64 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
std::to_string(info.max_text));
|
||||
}
|
||||
|
||||
/* ---- the encoder's own view of every reference the VAE consumes. The text encoder saw the
|
||||
* pictures above for identity; now each is also decoded and shrunk to the encoder's grid so the
|
||||
* video VAE can turn it into conditioning rows. The driver stages these bytes into a Vencode
|
||||
* pass; reference videos and their soundtracks, and the standalone audios, ride the same two
|
||||
* encoders but are still refused at submit until those decode paths land, so only pictures are
|
||||
* prepared here. The processor decodes under the same guard it used for the text encoder, so a
|
||||
* reference that got this far decodes here too. */
|
||||
/* ---- the encoder's own view of every reference the VAEs consume: pictures and videos through
|
||||
* the video VAE (enc_pixels), soundtracks and standalone audios through the audio VAE
|
||||
* (enc_samples). The text encoder saw the pictures above for identity; now each reference is
|
||||
* decoded to the encoder's own grid so the driver can stage it into a Vencode or Aencode pass.
|
||||
* These are the NATURAL decoded shapes — the driver's encode pass, which owns the encoder's
|
||||
* 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) {
|
||||
for (gen::VideoRefMedia& m : r->refs) {
|
||||
if (m.kind != gen::VideoRefMedia::IMAGE) continue;
|
||||
mm::Frame fr;
|
||||
std::string why;
|
||||
if (deps_.mm->encoder_image((const uint8_t*)m.data.data(), m.data.size(),
|
||||
(int32_t)info.enc_ratio, (int32_t)info.max_enc_side, &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;
|
||||
/* un-normalised RGB 0..255, row-major; the video VAE applies ImageNet mean/std itself */
|
||||
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];
|
||||
const uint8_t* p = (const uint8_t*)m.data.data();
|
||||
const size_t n = m.data.size();
|
||||
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)
|
||||
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;
|
||||
/* un-normalised RGB 0..255, row-major; the video VAE applies ImageNet mean/std itself */
|
||||
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::VIDEO) {
|
||||
/* The frames the temporal VAE sees, sampled and resized to the encoder grid. The
|
||||
* count is the reference's trimmed length (n % 17 == 5); the driver's encode pass
|
||||
* repeats the final frame out to a whole clip, so this stays the natural count. */
|
||||
std::vector<mm::Frame> fr;
|
||||
if (deps_.mm->encoder_video(p, n, (int32_t)info.enc_ratio,
|
||||
(int32_t)info.max_enc_side, (int32_t)info.max_enc_frames,
|
||||
&fr, &why) < 0 || fr.empty())
|
||||
return bad(m.name.c_str(), "'" + m.name + "' could not be encoded: " + why);
|
||||
m.enc_frames = (int32_t)fr.size();
|
||||
m.enc_h = fr.front().h;
|
||||
m.enc_w = fr.front().w;
|
||||
const size_t area = (size_t)m.enc_w * m.enc_h * 3;
|
||||
m.enc_pixels.resize(area * fr.size());
|
||||
for (size_t f = 0; f < fr.size(); ++f)
|
||||
for (size_t i = 0; i < area; ++i)
|
||||
m.enc_pixels[f * area + i] = (float)fr[f].rgb[i];
|
||||
} else {
|
||||
/* A reference audio, or a reference video's soundtrack: mono at the model's audio
|
||||
* rate, then spread across the encoder's channels (the audio VAE encodes one
|
||||
* channel a time; a mono source becomes both). The driver zero-pads to a hop. */
|
||||
std::vector<float> mono;
|
||||
if (deps_.mm->encoder_audio(p, n, (int32_t)info.sample_rate, info.max_enc_samples,
|
||||
&mono, &why) < 0 || mono.empty())
|
||||
return bad(m.name.c_str(), "'" + m.name + "' could not be decoded: " + why);
|
||||
const int32_t ch = (int32_t)info.channels;
|
||||
m.enc_channels = ch;
|
||||
m.enc_n = (int64_t)mono.size();
|
||||
m.enc_samples.assign((size_t)ch * mono.size(), 0.0f);
|
||||
for (int32_t c = 0; c < ch; ++c)
|
||||
for (size_t i = 0; i < mono.size(); ++i)
|
||||
m.enc_samples[(size_t)c * mono.size() + i] = mono[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user