From 8a6015122b2a8fcfa2c40b4d0207ce12eb037e9e 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 23:01:19 +0300 Subject: [PATCH] test(av1): gate the mi-exact mode neighbour for non-strip readers (two pinned rgbtestsrc arms, mode_mi_override_hits counter) + lane-rectx r5 report --- crates/ec-av1/src/decode.rs | 24 ++++++-- crates/ec-av1/src/stream.rs | 114 ++++++++++++++++++++++++++++++++++++ lanes/rectx-r5.report.md | 84 ++++++++++++++++++++++++++ 3 files changed, 218 insertions(+), 4 deletions(-) create mode 100644 lanes/rectx-r5.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index f47eab8f..e170897c 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -757,6 +757,20 @@ pub(crate) fn rect_leaf_coeff_hits() -> usize { RECT_LEAF_COEFF_HITS.with(|c| c.get()) } +// lane-rectx r5: how many times a NON-strip `kf_y_mode` reader +// ([`decode_block`], [`decode_block_rect`], [`decode_block_rect64`]) took an +// above/left mode from the mi-exact map that DIFFERS from its coarse 16x16 +// slot -- i.e. how often the r5 defect would have picked the wrong CDF row. +thread_local! { + static MODE_MI_OVERRIDE_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`MODE_MI_OVERRIDE_HITS`]. +pub(crate) fn mode_mi_override_hits() -> usize { + MODE_MI_OVERRIDE_HITS.with(|c| c.get()) +} + // lane-sbpart r2: how many superblock-level `PARTITION_HORZ`/`PARTITION_VERT` // blocks (two true 64x32/32x64 strips, [`decode_block_rect64`]) fired -- the // gate's proof this round's arms actually reached a real block, not just @@ -2589,10 +2603,12 @@ impl Neighbours { /// a different CDF for the same mode value). fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) { let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); - ( - self.mode_above_mi(mi_r, mi_c).unwrap_or(self.above_mode[c]), - self.mode_left_mi(mi_r, mi_c).unwrap_or(self.left_mode[r]), - ) + let above = self.mode_above_mi(mi_r, mi_c).unwrap_or(self.above_mode[c]); + let left = self.mode_left_mi(mi_r, mi_c).unwrap_or(self.left_mode[r]); + if above != self.above_mode[c] || left != self.left_mode[r] { + MODE_MI_OVERRIDE_HITS.with(|h| h.set(h.get() + 1)); + } + (above, left) } fn record_rect( diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 8e43d92a..529c82a2 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5458,6 +5458,120 @@ mod tests { eprintln!("{NAME}: pixel-exact, rect_leaf_coeff_hits={fired}"); } + /// lane-rectx r5: the same 16x8/8x16 rect leaves, but this time proving + /// what a LATER, non-strip block reads from them. `decode_leaf_rect` took + /// the mi-exact intra mode map in r4; `decode_block`/`decode_block_rect`/ + /// `decode_block_rect64` did not, so a 32x16 strip whose left neighbour was + /// a 16x8 leaf still read the coarse per-16x16 `left_mode` slot -- which no + /// sub-16x16 block ever writes -- and picked a DIFFERENT `kf_y_mode` CDF row + /// for the same decoded mode value. The stream stayed in sync through mode + /// info (`EC_TRACE_MODE_STEP` ranges match up to that symbol) and then + /// diverged inside the block's own coefficients: the whole bottom-right + /// 32x32 quadrant of this fixture decoded wrong (1650 bytes) while the + /// three earlier quadrants were exact -- wrong pixels returned as success. + /// `mode_mi_override_hits` counts exactly the reads where the mi-exact + /// neighbour disagrees with the coarse slot, so a regression that drops the + /// override again fails the counter as well as the pixels. + #[test] + fn a_real_aomenc_stream_whose_square_block_reads_a_sub16_neighbours_mode_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_stream_whose_square_block_reads_a_sub16_neighbours_mode_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height) = (64usize, 64usize); + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", + &format!("rgbtestsrc=size={width}x{height}"), + "-pix_fmt", "yuv420p", "-t", "1", "-vframes", "1", "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!(y4m.status.success(), "ffmpeg fixture: {}", String::from_utf8_lossy(&y4m.stderr)); + // Two swept cells of the r4 sweep that decoded to completion with + // WRONG pixels (1714 and 1650 bytes): `--reduced-tx-type-set` and + // `--enable-filter-intra` are the two axes that move which block sizes + // aomenc reaches for here; both arms are pinned. + let mut total_overrides = 0usize; + for (rtx, filter_intra) in [("1", "0"), ("0", "1")] { + let encode = || { + let mut child = Command::new(aomenc_path()) + .args([ + "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=32", + "--cpu-used=4", "--threads=1", "--row-mt=0", "--sb-size=64", + "--kf-max-dist=0", "--enable-rect-partitions=1", + "--enable-ab-partitions=0", "--enable-1to4-partitions=0", + "--enable-tx-size-search=0", "--enable-cdef=0", + "--enable-restoration=0", + &format!("--enable-filter-intra={filter_intra}"), + &format!("--reduced-tx-type-set={rtx}"), + "--min-partition-size=8", "--max-partition-size=32", + "--obu", "-o", "-", "-", + ]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + out.stdout + }; + let stream = encode(); + assert_eq!( + stream, + encode(), + "{NAME}: aomenc output is not reproducible (rtx={rtx} filter_intra={filter_intra})" + ); + let before = crate::decode::mode_mi_override_hits(); + let frames = decode_stream(&stream).unwrap_or_else(|e| { + panic!("{NAME}: decode failed (rtx={rtx} filter_intra={filter_intra}): {e}") + }); + // Counted only on an attempt that actually decoded AND is compared + // below -- a refusal never contributes to the firing assert. + let overrides = crate::decode::mode_mi_override_hits() - before; + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frames.len()); + assert_eq!(frames.len(), 1, "{NAME}: expected one key frame"); + assert_eq!(ffmpeg_frames.len(), frames.len(), "{NAME}: ffmpeg frame count"); + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!( + got.y, want.y, + "{NAME} frame {i} luma vs ffmpeg (rtx={rtx} filter_intra={filter_intra}, \ + {overrides} mi-exact mode overrides)" + ); + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (rtx={rtx})"); + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (rtx={rtx})"); + } + total_overrides += overrides; + } + assert!( + total_overrides > 0, + "{NAME}: no non-strip block ever read a mi-exact neighbour that disagreed with its \ + coarse slot -- the gate proves nothing" + ); + eprintln!("{NAME}: pixel-exact, mode_mi_override_hits={total_overrides}"); + } + #[test] #[ignore = "lane-rectx r3 scratch sweep"] fn sweep_rectx_recipes() { diff --git a/lanes/rectx-r5.report.md b/lanes/rectx-r5.report.md new file mode 100644 index 00000000..c7fccfc5 --- /dev/null +++ b/lanes/rectx-r5.report.md @@ -0,0 +1,84 @@ +# lane-rectx r5 -- three "decodes fine, wrong pixels" cells: one root cause, luma now exact in all three + +Branch `lane-rectx`, on top of r4's `fad4d5a`. r4 left three swept cells decoding to +completion with WRONG pixels (the worst class). All three had the SAME root cause; after the +fix two are byte-exact and all three are LUMA-exact, with a chroma-only residue below. + +## Root cause (found this round) + +`decode_leaf_rect` took the mi-exact intra-mode neighbour map in r4 (`mode_above_mi` / +`mode_left_mi`). The three OTHER `kf_y_mode` readers did not: +`decode_block` (square, `decode.rs` ~4960), `decode_block_rect` (32x16/16x32, ~3274) and +`decode_block_rect64` (64x32/32x64, ~3850) each read the COARSE per-16x16 `above_mode[c]` / +`left_mode[r]` slots -- which no sub-16x16 block ever writes (`record_rect` loops +`w / SUB` = 0 cells for a 16x8). So a 32x16 strip whose left neighbour was a 16x8 rect leaf +read an older, larger block's mode, indexed a DIFFERENT `kf_y_mode` CDF row, decoded the same +mode VALUE off a different distribution, and desynced from that block's first coefficient on. +This is the memory class "context read from one cell" / "CDF row held constant", one level up +from r4's own instance. + +Fix (`decode.rs`): one `Neighbours::modes_above_left(r, c)` helper (mi-exact if the map holds +exactly this block's above/left mi, else the coarse slot) used by all three readers, for the +mode CDF row AND for `smooth_neighbor` (libaom's `get_filt_type` reads the same `above_mi`/ +`left_mi`). New `MODE_MI_OVERRIDE_HITS` counter (`decode.rs` ~760) counts the reads where the +two disagree. + +Also applied here, per the coordinator's mid-round correction (defect found by lane-sub8's +verifier in the code r4 cherry-picked from that lane): `mode_above_mi`/`mode_left_mi` guarded +availability with `mi_r > 0`/`mi_c > 0` instead of the tile-relative +`mi_r > tile_row0_mi`/`mi_c > tile_col0_mi`, and the maps were never reset -- `start_tile` now +clears `sub8_mode_col` over the tile's mi columns and `start_row` clears `sub8_mode_row`, +exactly like every other neighbour array beside them. (This lane's sweep is single-tile, so +that half is code-verified here, not gate-proven; lane-sub8 r6 owns the multi-tile gate.) + +## Evidence + +EVIDENCE: /tmp/.../scratchpad/rx/{rgb240.obu,pre_a.f0,pre_o.f0,o.step.txt,a.step.txt} | +ffmpeg lavfi `rgbtestsrc=size=64x64` -> aomenc `--cq-level=24 --reduced-tx-type-set=0 +--enable-rect-partitions=1 --min-partition-size=8 --max-partition-size=32 +--enable-filter-intra=0 --enable-cdef=0 --enable-restoration=0` (sha256 +22113636b7ab950b5d1a175a60b9e01251dcead819f99a9ef14dcd1378ccfad6, reproducible across two +encodes); `EC_TRACE_MODE_STEP` ladder ours vs instrumented aomdec, `EC_AV1_PREFILT_DUMP` both | +first divergent element = `mi_row=8 mi_col=8 name=mode val=2` at range 53452 (oracle) vs 33402 +(ours) -- same value, different CDF row, entry ranges equal (56121) so it is the ROW not the +symbol; that block's left neighbour is the 16x8 leaf at mi(8,4) mode=12. Pre-filter luma diff +1006 -> 0, total 1650 -> 12 bytes (all chroma). + +EVIDENCE: gate `a_real_aomenc_stream_whose_square_block_reads_a_sub16_neighbours_mode_decodes_pixel_exact` +(`stream.rs` ~5460) | `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib -- a_real_aomenc_stream_whose_square_block_reads_a_sub16 --nocapture` | +`pixel-exact, mode_mi_override_hits=2`, two pinned arms (`rgbtestsrc` 64x64 cq32 with +`--reduced-tx-type-set=1 --enable-filter-intra=0` and with `=0 --enable-filter-intra=1`), each +encoded twice and byte-compared before decode; the counter is read only on an attempt that +decoded AND is pixel-compared. + +EVIDENCE: /tmp/.../scratchpad/rx/{rgb240,rgb241,rgb321,mandfi161}.{obu,ours.yuv,ref.yuv} | +`examples/decode_probe ` vs `ffmpeg -i -f rawvideo`, per-plane byte diff | +per cell, before -> after this round: rgbtestsrc cq24 rtx=0 1650 -> 12 (luma 1118 -> 0); +rgbtestsrc cq24 rtx=1 3461 -> 14 (luma 2304 -> 0); rgbtestsrc cq32 rtx=1 1622 -> 0; +rgbtestsrc cq32 `--enable-filter-intra=1` rtx=0 -> 0; mandelbrot start_x=-0.6 cq16 +`--enable-filter-intra=1` rtx=1 126 -> 123 (luma 0, U 40, V 83). + +## Open residue + +- fix-now(next round, needs its own lane -- it is a CHROMA PREDICTION defect, not a rect one): + every remaining mismatch is chroma-only with luma byte-exact and the entropy ladder + IDENTICAL end to end (`diff` of the two `EC_ISTEP` traces on mandfi161 is empty apart from + two `use_filter_intra` lines the oracle prints and we do not -- ranges match, so the symbol + is consumed). Localised per 4x4 chroma block on mandelbrot cq16 fi=1: the three FULLY wrong + blocks (16/16 samples) are mi(10,4) V, mi(10,6) V, mi(12,12) U -- all three `uv_mode=13` + (`UV_CFL_PRED`), one plane wrong while the other is exact, i.e. the per-plane CFL alpha, not + the shared luma AC. The partially wrong blocks (1-13 samples, max delta 9) are all + directional chroma with a NON-ZERO `angle_uv` (mi(8,12) uv=4 delta -3, mi(10,2) uv=4 delta 3, + mi(12,8) uv=6 delta -3, ...), i.e. the chroma intra-edge filter/upsample path. Ruled out this + round by experiment: `smooth_neighbor_uv` is NOT the cause -- forcing it true and false + globally leaves the diff at 123 bytes either way (rgb240: 12/13/12). +- deferred(a lane for AB partitions below 16x16): unchanged from r4, `a HORZ_A/HORZ_B/VERT_A + partition below 16x16` still dominates the refusals in the sweep. +- accepted: `read_coeffs_rect` still refuses `V_DCT`/`H_DCT` on a rect transform + (`--reduced-tx-type-set=0` cells can hit it: rgbtestsrc cq32 rtx=0 fi=0 refuses by name). +- accepted: `filter intra on a HORZ/VERT strip` still refuses (4 of the 6 filter-intra cells + swept above stop there, non-silently). + +## Refusals + +None lifted or added this round; `refusal_inventory` and `gate_coverage` unchanged and green.