From cd8ea4672c8acdffbe725a414f59a9f4f74b7420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 22:42:40 +0300 Subject: [PATCH] fix(av1): a warp-capable frame under a scaled reference read the 3-symbol motion_mode_cdf where libaom reads the 2-symbol obmc_cdf libaom motion_mode_allowed (blockd.h:1484) drops WARPED_CAUSAL eligibility when av1_is_scaled(block_ref_scale_factors[0]), so a superres block reads obmc_cdf. We read motion_mode_cdf: the decoded value stayed SIMPLE_TRANSLATION, so nothing errored -- the stream decoded "OK: 24 frames" with quietly wrong pixels from the diverged coder state (frame 1: 1342 luma pixels off, growing to |d|=18 by frame 23). The existing warp refusal could not see it (warp_params is None there). Gate: the superres gate now runs --enable-warped-motion=1 by DEFAULT and hard-asserts scaled_warp_suppressed_hits() > 0 -- 12/12 pixel-exact, 25 blocks took the obmc_cdf arm only because of this fix. decode_probe gains EC_PROBE_OUT (raw yuv420p dump), the instrument that located it. Sibling site in decode_inter_block8 is unreachable (refused earlier) and carries a note. --- crates/ec-av1/examples/decode_probe.rs | 15 ++++- crates/ec-av1/src/decode.rs | 47 ++++++++++++++ crates/ec-av1/src/stream.rs | 44 +++++++++---- lanes/scaledref-r1.report.md | 6 +- lanes/scaledref-r2.report.md | 85 ++++++++++++++++++++++++++ 5 files changed, 182 insertions(+), 15 deletions(-) create mode 100644 lanes/scaledref-r2.report.md diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index a7edca41..e9d17fe2 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -32,7 +32,20 @@ fn main() { Ok(frames) if frames.is_empty() => { println!("OK but EMPTY: no frames -- is {path} an IVF rather than a raw OBU stream?"); } - Ok(frames) => println!("OK: {} frames decoded", frames.len()), + Ok(frames) => { + // EC_PROBE_OUT=: dump the decoded planes as raw yuv420p so a + // stream can be diffed against ffmpeg's own decode pixel by pixel. + if let Ok(out) = std::env::var("EC_PROBE_OUT") { + let mut buf: Vec = Vec::new(); + for f in &frames { + for p in [&f.y, &f.u, &f.v] { + buf.extend(p.iter().map(|&s| s as u8)); + } + } + std::fs::write(&out, &buf).expect("writing EC_PROBE_OUT"); + } + println!("OK: {} frames decoded, {}x{}", frames.len(), frames[0].width, frames[0].height) + } Err(e) => println!("REFUSED: {e}"), } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a6d92e3b..4719863e 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -627,6 +627,22 @@ thread_local! { static SCALED_OBMC_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; static SCALED_INTERINTRA_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; static SCALED_BLOCK8_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static SCALED_WARP_SUPPRESSED_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static MIXED_SCALE_COMPOUND_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`MIXED_SCALE_COMPOUND_HITS`]: compound blocks with one +/// scaled and one unscaled tap. +pub(crate) fn mixed_scale_compound_hits() -> usize { + MIXED_SCALE_COMPOUND_HITS.with(|c| c.get()) +} + +/// Current value of [`SCALED_WARP_SUPPRESSED_HITS`]: blocks that would have +/// read the 3-symbol `motion_mode_cdf` but read the 2-symbol `obmc_cdf` +/// instead because their reference is scaled (libaom `motion_mode_allowed`, +/// `blockd.h:1484`). +pub(crate) fn scaled_warp_suppressed_hits() -> usize { + SCALED_WARP_SUPPRESSED_HITS.with(|c| c.get()) } /// Current value of [`SCALED_COMPOUND_HITS`]. @@ -9507,6 +9523,12 @@ fn decode_inter_block( if scale0 != mc::REF_NO_SCALE || scale1 != mc::REF_NO_SCALE { SCALED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); } + // lane-scaledref r2: the MIXED case -- one tap scaled, the other + // not -- is the one an all-frames-scaled recipe never produces, + // and the one a single shared scale factor would get wrong. + if (scale0 == mc::REF_NO_SCALE) != (scale1 == mc::REF_NO_SCALE) { + MIXED_SCALE_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); + } let mut inter0_y = vec![0i32; side * side]; mc::predict_compound_intermediate( @@ -10016,8 +10038,27 @@ fn decode_inter_block( // `motion_mode_allowed` reads the 3-symbol `motion_mode_cdf` // instead of the 2-symbol `obmc_cdf` exactly when // `num_proj_ref >= 1` under `allow_warped_motion`. + // lane-scaledref r2: libaom `motion_mode_allowed` + // (`blockd.h:1484`) requires + // `!av1_is_scaled(block_ref_scale_factors[0])` for + // WARPED_CAUSAL -- under a scaled reference (superres) the + // block reads the 2-symbol `obmc_cdf`, never the 3-symbol + // `motion_mode_cdf`. Reading the wrong alphabet here narrows + // the arithmetic coder by the wrong amount and predicts the + // rest of the tile off a diverged state: it was silently + // wrong pixels (r1's `--enable-warped-motion=1` superres + // mismatch), not a desync error. + let ref_is_scaled = + mc::scale_factor(py_ref.width, frame_width) != mc::REF_NO_SCALE; let warp_eligible = allow_warped_motion + && !ref_is_scaled && num_proj_ref(grid, mi_row, mi_col, bw4, bh4, mi_cols as usize, mi_rows as usize, ref_frame) >= 1; + if allow_warped_motion + && ref_is_scaled + && num_proj_ref(grid, mi_row, mi_col, bw4, bh4, mi_cols as usize, mi_rows as usize, ref_frame) >= 1 + { + SCALED_WARP_SUPPRESSED_HITS.with(|c| c.set(c.get() + 1)); + } if warp_eligible { let mode = dec.symbol(&mut cdfs.motion_mode[bsize_idx]); match mode { @@ -11409,6 +11450,12 @@ fn decode_inter_block8( // lane-warp round 1: same 3-vs-2-symbol split as the 16x16+ leaf // (see its own doc) -- this leaf is always `LAST_FRAME`-only // (grid.set below hardcodes it). + // lane-scaledref r2 sibling of the 16x16+ leaf's fix: libaom + // drops WARPED_CAUSAL eligibility (so this reads `obmc_cdf`) + // when the reference is scaled. Not gated here because an 8x8 + // leaf under a scaled reference is refused before this function + // runs (see the block8 refusal below, ~12500) -- whoever lifts + // that refusal must add `&& !ref_is_scaled` here too. let warp_eligible = allow_warped_motion && num_proj_ref(grid, mi_row, mi_col, 2, 2, mi_cols as usize, mi_rows as usize, LAST_FRAME) >= 1; if warp_eligible { diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 344560de..6fcc0356 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5079,12 +5079,21 @@ mod tests { let on = |name: &str| { if std::env::var(name).as_deref() == Ok("0") { "0" } else { "1" } }; - // warp stays OFF: warp under a scaled reference is still refused - // (its own fallback mismatches, see decode.rs) -- set - // EC_SCALEDREF_WARP=1 to reproduce that residue. - let warp = format!( - "--enable-warped-motion={}", - if std::env::var("EC_SCALEDREF_WARP").as_deref() == Ok("1") { "1" } else { "0" } + // lane-scaledref r2: warp is ON. A warp-capable frame header + // under a scaled reference is exactly the case r1 got wrong + // (the block must read the 2-symbol `obmc_cdf`, not the + // 3-symbol `motion_mode_cdf` -- libaom `motion_mode_allowed`, + // blockd.h:1484), and it decoded silently wrong pixels rather + // than refusing. `EC_SCALEDREF_WARP=0` turns it off for + // bisecting only. + let warp = format!("--enable-warped-motion={}", on("EC_SCALEDREF_WARP")); + // lane-scaledref r2: `EC_SCALEDREF_MODE=3` (qthresh) lets + // superres switch on and off per frame, so a compound block can + // reference one scaled and one unscaled reference -- the mixed + // case mode 1 (every frame scaled) never produces. + let superres_mode = format!( + "--superres-mode={}", + std::env::var("EC_SCALEDREF_MODE").unwrap_or_else(|_| "1".into()) ); let obmc = format!("--enable-obmc={}", on("EC_SCALEDREF_OBMC")); let interintra = format!("--enable-interintra-comp={}", on("EC_SCALEDREF_II")); @@ -5111,7 +5120,7 @@ mod tests { // still refuses on the aligned path ("an inter partition // below 16x16"). Denominator 12 codes 43 columns and every // seed refused there. - "--superres-mode=1", + &superres_mode, "--superres-denominator=16", "--superres-kf-denominator=16", // the four combinations this round lifts (each switchable @@ -5211,7 +5220,7 @@ mod tests { eprintln!( "{NAME}: {named_refusals} other-capability refusals, {matched}/{n_attempts} pixel-exact, \ superres_hits={} predict_scaled_hits={} scaled_compound={} scaled_warp_fallback={} \ - scaled_obmc={} scaled_interintra={} scaled_block8={}", + scaled_obmc={} scaled_interintra={} scaled_block8={} scaled_warp_suppressed={} mixed_scale_compound={}", crate::superres::superres_hits(), crate::mc::predict_scaled_hits(), crate::decode::scaled_compound_hits(), @@ -5219,6 +5228,8 @@ mod tests { crate::decode::scaled_obmc_hits(), crate::decode::scaled_interintra_hits(), crate::decode::scaled_block8_hits(), + crate::decode::scaled_warp_suppressed_hits(), + crate::decode::mixed_scale_compound_hits(), ); assert!( matched > 0, @@ -5228,10 +5239,19 @@ mod tests { crate::mc::predict_scaled_hits() > 0, "{NAME}: matched but zero predict_scaled_hits -- no block took the scaled MC path" ); - // Warp and the 8x8 leaf are NOT asserted here: both stay refused this - // round (warp's own fallback mismatches at seed 47, and no recipe - // produced an 8x8 inter leaf at all), so their counters are - // diagnostics only -- see this gate's own doc and decode.rs. + // The 8x8 leaf is NOT asserted here: it stays refused (no recipe + // produced an 8x8 inter leaf at all), so its counter is a + // diagnostic only -- see this gate's own doc and decode.rs. + // `scaled_warp_suppressed` IS asserted: it counts the blocks whose + // WARPED_CAUSAL eligibility libaom drops because the reference is + // scaled, i.e. the blocks that must read `obmc_cdf`. Zero of them + // would mean this gate's `--enable-warped-motion=1` never reached a + // warp-eligible block and proves nothing about r2's fix. + assert!( + crate::decode::scaled_warp_suppressed_hits() > 0, + "{NAME}: {matched} matches but no block had its warp eligibility \ + suppressed by a scaled reference -- the warp arm proves nothing" + ); for (hits, what) in [ (crate::decode::scaled_compound_hits(), "compound with a scaled reference"), (crate::decode::scaled_obmc_hits(), "OBMC with a scaled reference"), diff --git a/lanes/scaledref-r1.report.md b/lanes/scaledref-r1.report.md index 95f06d61..e9eb5ccc 100644 --- a/lanes/scaledref-r1.report.md +++ b/lanes/scaledref-r1.report.md @@ -23,8 +23,10 @@ verified, gated and kept; everything below r1 adds is on top of it. 1. `stream::tests::a_real_aomenc_superres_stream_with_compound_obmc_and_interintra_decodes_pixel_exact` — real `aomenc --superres-mode=1 --superres-denominator=16 --superres-kf-denominator=16 --auto-alt-ref=1 --lag-in-frames=25 --enable-obmc=1 --enable-interintra-comp=1 - --enable-smooth-interintra=1`, 12 seeds x 24 frames of 64x64 moving gradients, every frame - (hidden alt-refs included, `decode_stream` decodes the whole OBU sequence) compared to ffmpeg. + --enable-smooth-interintra=1`, 12 seeds x 24 frames of 64x64 moving gradients, every SHOWN frame + compared to ffmpeg (corrected in r2: `decode_stream` pushes a picture only under + `show_frame`, stream.rs:711, and `ffmpeg -f obu` emits shown frames only -- a hidden + alt-ref is checked only through propagation into the frames that reference it). Hard-asserts `mc::predict_scaled_hits() > 0` and the per-case counters `scaled_compound/scaled_obmc/scaled_interintra > 0`. diff --git a/lanes/scaledref-r2.report.md b/lanes/scaledref-r2.report.md new file mode 100644 index 00000000..22dd69e6 --- /dev/null +++ b/lanes/scaledref-r2.report.md @@ -0,0 +1,85 @@ +# lane-scaledref r2 — warp-capable frame under a scaled reference read the wrong motion_mode alphabet + +Branch `lane-scaledref` (HEAD back on the branch: the verifier left the worktree detached at +a8111dd; `git checkout lane-scaledref` restored it, both were the same commit, no work lost). + +## Verifier finding 1 — ROOT CAUSE FOUND AND FIXED (no refusal needed) + +The verifier's premise was that "motion_mode symbol reading is unaffected; only the prediction +falls back". That is **false**, and it is the whole defect. libaom +`motion_mode_allowed` (oracle source `av1/common/blockd.h:1484-1487`): + +```c + if (mbmi->num_proj_ref >= 1 && allow_warped_motion && + !xd->cur_frame_force_integer_mv && + !av1_is_scaled(xd->block_ref_scale_factors[0])) { + return WARPED_CAUSAL; + } + return OBMC_CAUSAL; +``` + +Under a scaled reference the block reads the **2-symbol `obmc_cdf`**, never the 3-symbol +`motion_mode_cdf`. We read the 3-symbol one, which narrows the arithmetic decoder by the wrong +amount: the decoded *value* happened to stay SIMPLE_TRANSLATION, so nothing ever errored — the +stream decoded "OK: 24 frames" with quietly wrong pixels, exactly the silent-wrong-pixels class +the verifier caught. The warp guard at decode.rs ~10197 could never see it: `warp_params` is +`None` for those blocks (`warped=0` in EC_AV1_TRACE), so no fallback and no refusal ran. + +| path:line | why | +| --- | --- | +| `crates/ec-av1/src/decode.rs` ~10060 (`warp_eligible`) | `&& !ref_is_scaled` added (`mc::scale_factor(py_ref.width, frame_width) != REF_NO_SCALE`), libaom-exact; new `SCALED_WARP_SUPPRESSED_HITS` counts the blocks that take the `obmc_cdf` arm because of it | +| `crates/ec-av1/src/decode.rs` ~630 | the counter + `scaled_warp_suppressed_hits()` / `mixed_scale_compound_hits()` accessors | +| `crates/ec-av1/src/decode.rs` ~9515 | `MIXED_SCALE_COMPOUND_HITS`: compound blocks with exactly one scaled tap (diagnostic for finding 3) | +| `crates/ec-av1/src/decode.rs` ~11455 (`decode_inter_block8`) | sibling site of the same shape, comment only: an 8x8 leaf under a scaled reference is refused before this code runs, so the same `&& !ref_is_scaled` is owed by whoever lifts that refusal | +| `crates/ec-av1/src/stream.rs` (superres gate) | `--enable-warped-motion=1` is now the DEFAULT arm (`EC_SCALEDREF_WARP=0` only for bisecting), `--superres-mode` made settable (`EC_SCALEDREF_MODE`), and `scaled_warp_suppressed_hits() > 0` is hard-asserted so the warp arm cannot pass vacuously | +| `crates/ec-av1/examples/decode_probe.rs` | `EC_PROBE_OUT=` dumps the decoded planes as raw yuv420p — that is the instrument that located this defect (diff vs `ffmpeg -f obu`) | +| `lanes/scaledref-r1.report.md` | finding 2, wording corrected (below) | + +Not changed, reported instead of silently fixed: libaom also requires `!cur_frame_force_integer_mv` +in that same condition and our `warp_eligible` still omits it. Left alone this round — no gate +covers `force_integer_mv` (screen content), and changing an alphabet choice without a gate is the +mistake this round is fixing. deferred: force_integer_mv warp eligibility — unblocked by a +screen-content gate with `cur_frame_force_integer_mv=1`. + +Refusals: **none lifted, none added**, 45 unchanged. `refusal_inventory` / `gate_coverage` +untouched and green (see totals). The "warp prediction with a scaled reference" refusal stays: it +now only guards a *global*-warp block under a scaled reference, which this recipe never produced +(`scaled_warp_fallback=0`). + +EVIDENCE: /tmp/.../scratchpad/warp_superres.obu + ours2.yuv vs ff.yuv | EC_PROBE_OUT decode of the verifier's pinned stream, byte-compared to `ffmpeg -f obu -pix_fmt yuv420p -f rawvideo` | before: frames 1..23 differ (frame 1: 1342 luma + 370 chroma pixels, first at (2,26), max |d| 3, growing to 18 by frame 23); after the fix: `cmp` identical, 147456/147456 bytes + +EVIDENCE: gate run, this report | `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib a_real_aomenc_superres_stream_with_compound -- --nocapture` (warp now on by default) | `0 other-capability refusals, 12/12 pixel-exact, superres_hits=291 predict_scaled_hits=3198 scaled_compound=500 scaled_warp_fallback=0 scaled_obmc=9 scaled_interintra=1 scaled_block8=0 scaled_warp_suppressed=25 mixed_scale_compound=0` — 25 blocks took the `obmc_cdf` arm only because of this fix + +## Verifier finding 2 — report correction (done) + +`lanes/scaledref-r1.report.md` line ~27 now reads "every SHOWN frame compared to ffmpeg ... a +hidden alt-ref is checked only through propagation" (ledger already records +`decode_stream` pushes only under `show_frame`, stream.rs:711). + +## Verifier finding 3 — mixed scaled/unscaled compound taps: DEFERRED, measured not assumed + +Counter `mixed_scale_compound` added and wired into the gate's print; both alternative recipes were +run, neither yields a gateable stream: + +- `EC_SCALEDREF_MODE=3` (qthresh): aomenc never turns superres on for this content/q — + `superres_hits=0 predict_scaled_hits=0`, i.e. the whole gate goes vacuous (it fails its own + `predict_scaled_hits > 0` assert, which is the assert working). +- `EC_SCALEDREF_MODE=2` (random denominator per frame): produces coded widths that are not + multiples of 32, so the frame edge forces sub-16 partitions and the stream stops at the still-open + named refusal `an 8x8 partition leaf under a scaled reference (superres, unimplemented)` at + seed 42. + +deferred: mixed one-scaled-one-unscaled compound tap — unblocked by the leaf8 desync (lane-sub8), +which is what mode-2 recipes hit; the counter is in place so any future recipe proves the case +immediately. The per-tap code itself is r1's (`scale0`/`scale1` are already independent). + +## Test totals + +`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` (whole crate lib suite): **268 passed, 0 failed, 23 ignored**, 968s (same totals as r1, now with the warp arm of the superres gate ON). Run at the final tree. + +## Residue carried from r1 (unchanged) + +- deferred: 8x8 partition leaf under a scaled reference — unblocked by the lane-sub8 leaf8 desync. +- deferred: reference scaling in BOTH axes (`--resize-mode`) — unblocked by a lane sized for it. +- accepted: 10-bit is refused upstream (`Picture` planes are `Vec`). +- accepted: the Golomb-tail cap stays where r1 left it (it masks an intra-rect64 defect).