From fabb8ceed7e2b954a57ece3b387c58c448b88321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 12:02:32 +0300 Subject: [PATCH] test(av1): a synthetic 1920x1024 force_integer_mv gate, plus the lane report The gate builds its own source -- near-flat dark background (2..=4 luma values per 16x16 block, libaom's own screen-content counting rule) with a textured patch stepping 9 px every second frame, so half the frames are exact duplicates of their predecessor, which is the only condition under which `av1_is_integer_mv` returns 1. It asserts that libaom really coded an inter frame with `force_integer_mv` and `use_ref_frame_mvs` before comparing all 12 shown frames against ffmpeg's own decode. Reported honestly in lanes/dgolomb.report.md: this gate is currently BLIND to the defect it was written for (stubbing the integer branch out still passes), so the regression cover is the motion_field unit test whose values come from the instrumented libaom oracle. The film-B ladder decodes sample-exact at crf 20/35/45; crf 5 still desyncs on the key frame at mi(184,344). --- crates/ec-av1/src/stream.rs | 109 ++++++++++++++++++++++++++++++++++++ lanes/dgolomb.report.md | 89 +++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 lanes/dgolomb.report.md diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 8af6ab4e..608967a8 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3758,6 +3758,115 @@ pub(crate) mod tests { } } + /// lane-dgolomb: the `force_integer_mv` gate. libaom turns screen-content + /// tools on for DARK, SMOOTH content -- its `av1_set_screen_content_options` + /// counts 16x16 blocks with 2..=4 distinct luma values -- and then sets + /// `force_integer_mv` on any inter frame whose source is pixel-identical to + /// the previously coded source (`av1_is_integer_mv`: `cs_rate >= 0.8` and + /// `C == T`, every collocated 8x8 block matching, is the only path that + /// returns 1). Both hold on his real AV1 films, and this decoder used to + /// drop `lower_mv_precision`'s `is_integer` half as unreachable, so every + /// temporal MV candidate on such a frame came out at eighth-pel: the frame + /// decoded without any error into a whole-frame prediction mismatch, and + /// the desync surfaced blocks later as "a Golomb tail longer than this + /// decoder reads". + /// + /// The source is SYNTHETIC (no film sample lives in `fixtures`): a + /// near-flat dark background at the film-B crop size, 1920x1024, with one + /// textured patch stepping 9 px every SECOND frame, so half the frames are + /// exact duplicates of their predecessor (the `C == T` trigger) while the + /// motion field still holds non-zero MVs whose projections are not + /// multiples of 8. The gate asserts both halves: at least one INTER frame + /// really carries `force_integer_mv` (class `gate-blind-to-feature`), and + /// every shown frame decodes sample-exact against ffmpeg's own decode. + #[test] + fn a_libaom_force_integer_mv_stream_decodes_exact() { + const NAME: &str = "a_libaom_force_integer_mv_stream_decodes_exact"; + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + let (w, h, frames) = (1920usize, 1024usize, 12usize); + let mut raw = Vec::with_capacity(frames * w * h * 3 / 2); + for f in 0..frames { + let mut y = vec![0u8; w * h]; + for row in 0..h { + for col in 0..w { + // 2..=4 distinct values per 16x16 block, all dark: the + // screen-content detector's own counting rule. + y[row * w + col] = + 16 + ((col / 32 + row / 32) % 5) as u8 + ((col / 4 + row / 4) % 3) as u8; + } + } + let x0 = 200 + 9 * (f / 2); + for row in 300..428 { + for col in x0..x0 + 192 { + y[row * w + col] = + ((((col - x0) / 3) * 7 + ((row - 300) / 5) * 13) % 200 + 30) as u8; + } + } + raw.extend_from_slice(&y); + raw.extend(std::iter::repeat_n(128u8, w * h / 2)); + } + // [[pid-keyed-temp-path]]: parallel test binaries must not share a name. + let src = std::env::temp_dir().join(format!("ec-av1-intmv-{}.yuv", std::process::id())); + std::fs::write(&src, &raw).expect("raw source"); + let out = Command::new("ffmpeg") + .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"]) + .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"]) + .arg(&src) + .args(["-an", "-threads", "1", "-g", "12", "-c:v", "libaom-av1"]) + .args(["-cpu-used", "6", "-b:v", "0", "-crf", "35", "-f", "obu", "-"]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + let _ = std::fs::remove_file(&src); + if !out.status.success() { + eprintln!( + "SKIP {NAME}: ffmpeg has no libaom-av1 encoder ({})", + String::from_utf8_lossy(&out.stderr) + ); + return; + } + let stream = out.stdout; + + // Half of the gate: the stream must actually carry the feature. + let mut parser = Av1Parser::new(); + let (mut pos, mut int_mv_inter) = (0usize, 0usize); + while pos < stream.len() { + let obu = parser.parse_obu(&stream[pos..]).expect("libaom OBU parses"); + pos += obu.total_size.max(1); + let header = match &obu.kind { + ObuKind::FrameHeader(h) | ObuKind::Frame(h, _) => h, + _ => continue, + }; + if header.force_integer_mv && !header.frame_is_intra && header.use_ref_frame_mvs { + int_mv_inter += 1; + } + } + assert!( + int_mv_inter > 0, + "{NAME} went blind: libaom coded no inter frame with force_integer_mv" + ); + + let ours = decode_stream(&stream).expect("libaom screen-content stream decodes"); + let theirs = ffmpeg_decode_sequence(&stream, w, h, frames); + assert_eq!(ours.len(), frames, "shown frame count"); + for (i, (a, b)) in ours.iter().zip(theirs.iter()).enumerate() { + for (plane, l, r) in [("Y", &a.y, &b.y), ("U", &a.u, &b.u), ("V", &a.v, &b.v)] { + let first = l.iter().zip(r.iter()).position(|(x, y)| x != y); + assert!( + first.is_none(), + "frame {i} plane {plane} differs from ffmpeg at sample {} \ + ({int_mv_inter} force_integer_mv inter frames in this stream)", + first.unwrap() + ); + } + } + } + /// Hardened past the report-only test above: a real libaom-av1 gradients /// stream, pinned deterministic (`gradients`' own `seed=`, plus /// `-threads 1`/`-row-mt 0`/no tiling so libaom's `cpu-used 8` RD search diff --git a/lanes/dgolomb.report.md b/lanes/dgolomb.report.md new file mode 100644 index 00000000..e2e22b48 --- /dev/null +++ b/lanes/dgolomb.report.md @@ -0,0 +1,89 @@ +# lane-dgolomb — libaom film-B decode (the "Golomb tail" refusal) + +## Root cause (closed) +`crates/ec-av1/src/motion_field.rs` `lower_mv_precision` implemented only the +`is_integer == false` half of libaom's function, on a comment claiming +`force_integer_mv` is unreachable because `allow_screen_content_tools` is +"refused outright at the stream level". Both halves of that claim are false: + +* libaom's `av1_set_screen_content_options` counts 16x16 blocks with 2..=4 + distinct luma values, so DARK, SMOOTH FILM reads as screen content — every + frame of the film-B gate clip has `allow_screen_content_tools = 1`; +* `av1_is_integer_mv` (encoder_utils.c:1357) returns 1 exactly when + `cs_rate >= 0.8` **and** `C == T`, i.e. when the source frame is + pixel-identical to the previously coded source. Film B has such frames. + +So a real libaom stream carries `force_integer_mv = 1` on an inter frame, and +every temporal MV candidate must be rounded to full pel. Ours stayed at +eighth-pel → the mv stack held values libaom never produced → whole-frame +prediction mismatch behind a clean parse, and the entropy desync surfaced +blocks later as `a Golomb tail longer than this decoder reads` +(class `refusal-names-a-correlate`). + +**Class: `branch-dropped-as-unreachable`** — a spec branch deleted on an +unreachability note that a later capability change silently invalidated. + +## First divergence (measured, not reasoned) +Film-B gate clip (1920x1024 crop, `-cpu-used 6 -crf 35 -g 12`), decode-order +frame 3 = order_hint 1, the ONE inter frame with `force_integer_mv = 1`, +block mi(0,0) (128x128, NEWMV, ref LAST): + +* `EC_TPL` field cells identical in both decoders (16 probes), range identical + (`rng=49952`) — so the field and the entropy state were right; +* instrumented aomdec stack: `(16,0)w16 (16,-8)w12 (8,-40)w4` +* ours before the fix: `(14,-4)w16 (12,-2)w8 (12,-38)w4 (16,-4)w4` +* `(74,-20)/5 → 15,-4 → integer → (16,0)` reproduces libaom exactly. + +## Fix +`lower_mv_precision(mv, allow_high_precision_mv, force_integer_mv)` with +libaom's `integer_mv_precision` (ties away from zero on `|mod| > 4`), threaded +through `project_tpl_mv`/`add_tpl_ref_mv`, `TplArgs`/`CompoundTplArgs` and the +six `decode.rs` construction sites. + +## Sweep (class + siblings) +* `grep` for unreachability claims over `ec-av1`/`ec-av1-syntax`: no other + "spec branch dropped because a header flag is refused" site. +* The other decoder-side `is_integer` consumers in libaom — + `av1_find_best_ref_mvs` (mvref_common.c:847) and the compound nearest/near + lowering (decodemv.c:1519) — are provably no-ops for us once the temporal + path is fixed: under `force_integer_mv` every other candidate source is + already integer (`read_mv_component` honours the flag, `warp.rs:128` honours + it for global MVs, spatial candidates are decoded MVs). Not implemented; if + a future defect lands here, this is the place. + +## Verification +* `crates/ec-av1/src/motion_field.rs` `force_integer_mv_rounds_a_projected_candidate_to_full_pel` + — the five (mfmv0, rfo) pairs of that first block, eighth-pel vs full-pel, + values taken from the oracle. Fails without the fix. PASSES. +* Film-B ladder, 48 frames, `-cpu-used 6 -g 48`, our `decode_stream` vs + `ffmpeg -c:v libdav1d`, sample-exact over all 48 shown frames: + crf 20 EXACT, crf 35 EXACT (was the Golomb refusal), crf 45 EXACT. + Same at `-g 12`. Before the fix: crf 20/45 25–28 dB garbage, crf 35 refused. +* `crates/ec-av1/src/stream.rs` `a_libaom_force_integer_mv_stream_decodes_exact` + — synthetic 1920x1024 source (no film sample in `fixtures`): near-flat dark + background (2..=4 luma values per 16x16 block, the detector's own rule) with + a textured patch stepping 9 px every SECOND frame, so half the frames are + exact duplicates (the `C == T` trigger). Asserts (a) libaom really coded an + inter frame with `force_integer_mv` + `use_ref_frame_mvs` (anti-blindness), + (b) all 12 shown frames decode sample-exact vs ffmpeg. PASSES, 2.1 s. + +## OPEN / deferred +1. **The synthetic gate is BLIND to this defect.** Re-run with the integer + branch stubbed out (`if false {`) and it still passes: on that source the + temporal candidates are already full-pel (or unused), so only the unit test + above actually regresses the fix. Fixing this needs a synthetic clip whose + duplicate frame still reads NON-multiple-of-8 projected temporal MVs — + e.g. motion of an odd number of half-pels between the earlier frames, with + the duplicate pair placed so the ARF's field survives into it. Unblocks: + one more recipe iteration against `EC_TPL`/`EC_STACK`. +2. **crf 5 still fails**: `-cpu-used 6 -crf 5 -g 48` desyncs on the KEY frame + (base_q 9, `delta_q_present`, `cdef_bits 3`), first divergent intra block + mi(184,344) — aomdec `EC_IMODE ... rng=49148` vs ours `rng=52010`, the + previous traced block being mi(188,340). Unrelated to temporal MVs; needs + its own bisect from that block's coefficient ladder (`EC_TRACE_COEFF`). +3. **Not done in this lane** (budget): the "reference streams decode exact + through our decoder" assertion inside `external_ladder`/the native gates; + the full `cargo test -p ec-av1 --release` run (started detached, was still + running at hand-off, log + `/tmp/claude-1000/.../scratchpad/suite.log`); `cargo check --workspace + --all-targets`; the byte pins (8590 / 28535) were not re-read.