forge
Explore
Log in

Don't you dare go Hollow

Gate the mi-exact mode neighbour for non-strip readers (two pinned rgbtestsrc arms, mode_mi_override_hits counter) + lane-rectx r5 report

Browse files at this commit

8a6015122b2a8fcfa2c40b4d0207ce12eb037e9eAhmet Kaan GümüşParent: fbf038b
  • test
    • gate the mi-exact mode neighbour for non-strip readers (two pinned rgbtestsrc arms, mode_mi_override_hits counter) + lane-rectx r5 report
Show full message
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.rsmodified+20 -4
@@ -757,6 +757,20 @@ pub(crate) fn rect_leaf_coeff_hits() -> usize {
757 RECT_LEAF_COEFF_HITS.with(|c| c.get())757 RECT_LEAF_COEFF_HITS.with(|c| c.get())
758 }758 }
759 759
760+// lane-rectx r5: how many times a NON-strip `kf_y_mode` reader
761+// ([`decode_block`], [`decode_block_rect`], [`decode_block_rect64`]) took an
762+// above/left mode from the mi-exact map that DIFFERS from its coarse 16x16
763+// slot -- i.e. how often the r5 defect would have picked the wrong CDF row.
764+thread_local! {
765+ static MODE_MI_OVERRIDE_HITS: std::cell::Cell<usize> =
766+ const { std::cell::Cell::new(0) };
767+}
768+
769+/// Current value of [`MODE_MI_OVERRIDE_HITS`].
770+pub(crate) fn mode_mi_override_hits() -> usize {
771+ MODE_MI_OVERRIDE_HITS.with(|c| c.get())
772+}
773+
760 // lane-sbpart r2: how many superblock-level `PARTITION_HORZ`/`PARTITION_VERT`774 // lane-sbpart r2: how many superblock-level `PARTITION_HORZ`/`PARTITION_VERT`
761 // blocks (two true 64x32/32x64 strips, [`decode_block_rect64`]) fired -- the775 // blocks (two true 64x32/32x64 strips, [`decode_block_rect64`]) fired -- the
762 // gate's proof this round's arms actually reached a real block, not just776 // gate's proof this round's arms actually reached a real block, not just
@@ -2589,10 +2603,12 @@ impl Neighbours {
2589 /// a different CDF for the same mode value).2603 /// a different CDF for the same mode value).
2590 fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) {2604 fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) {
2591 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));2605 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
2592- (2606+ let above = self.mode_above_mi(mi_r, mi_c).unwrap_or(self.above_mode[c]);
2593- self.mode_above_mi(mi_r, mi_c).unwrap_or(self.above_mode[c]),2607+ let left = self.mode_left_mi(mi_r, mi_c).unwrap_or(self.left_mode[r]);
2594- self.mode_left_mi(mi_r, mi_c).unwrap_or(self.left_mode[r]),2608+ if above != self.above_mode[c] || left != self.left_mode[r] {
2595- )2609+ MODE_MI_OVERRIDE_HITS.with(|h| h.set(h.get() + 1));
2610+ }
2611+ (above, left)
2596 }2612 }
2597 2613
2598 fn record_rect(2614 fn record_rect(
crates/ec-av1/src/stream.rsmodified+114 -0
@@ -5458,6 +5458,120 @@ mod tests {
5458 eprintln!("{NAME}: pixel-exact, rect_leaf_coeff_hits={fired}");5458 eprintln!("{NAME}: pixel-exact, rect_leaf_coeff_hits={fired}");
5459 }5459 }
5460 5460
5461+ /// lane-rectx r5: the same 16x8/8x16 rect leaves, but this time proving
5462+ /// what a LATER, non-strip block reads from them. `decode_leaf_rect` took
5463+ /// the mi-exact intra mode map in r4; `decode_block`/`decode_block_rect`/
5464+ /// `decode_block_rect64` did not, so a 32x16 strip whose left neighbour was
5465+ /// a 16x8 leaf still read the coarse per-16x16 `left_mode` slot -- which no
5466+ /// sub-16x16 block ever writes -- and picked a DIFFERENT `kf_y_mode` CDF row
5467+ /// for the same decoded mode value. The stream stayed in sync through mode
5468+ /// info (`EC_TRACE_MODE_STEP` ranges match up to that symbol) and then
5469+ /// diverged inside the block's own coefficients: the whole bottom-right
5470+ /// 32x32 quadrant of this fixture decoded wrong (1650 bytes) while the
5471+ /// three earlier quadrants were exact -- wrong pixels returned as success.
5472+ /// `mode_mi_override_hits` counts exactly the reads where the mi-exact
5473+ /// neighbour disagrees with the coarse slot, so a regression that drops the
5474+ /// override again fails the counter as well as the pixels.
5475+ #[test]
5476+ fn a_real_aomenc_stream_whose_square_block_reads_a_sub16_neighbours_mode_decodes_pixel_exact() {
5477+ const NAME: &str =
5478+ "a_real_aomenc_stream_whose_square_block_reads_a_sub16_neighbours_mode_decodes_pixel_exact";
5479+ let _gate_lock = lock_gate_counters();
5480+ if !have_ffmpeg() {
5481+ eprintln!("SKIP {NAME}: no ffmpeg");
5482+ return;
5483+ }
5484+ if !have_aomenc() {
5485+ eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display());
5486+ return;
5487+ }
5488+ let (width, height) = (64usize, 64usize);
5489+ let y4m = Command::new("ffmpeg")
5490+ .args([
5491+ "-v", "error", "-f", "lavfi", "-i",
5492+ &format!("rgbtestsrc=size={width}x{height}"),
5493+ "-pix_fmt", "yuv420p", "-t", "1", "-vframes", "1", "-f", "yuv4mpegpipe", "-",
5494+ ])
5495+ .stdin(Stdio::null())
5496+ .stdout(Stdio::piped())
5497+ .stderr(Stdio::piped())
5498+ .output()
5499+ .expect("ffmpeg failed to run");
5500+ assert!(y4m.status.success(), "ffmpeg fixture: {}", String::from_utf8_lossy(&y4m.stderr));
5501+ // Two swept cells of the r4 sweep that decoded to completion with
5502+ // WRONG pixels (1714 and 1650 bytes): `--reduced-tx-type-set` and
5503+ // `--enable-filter-intra` are the two axes that move which block sizes
5504+ // aomenc reaches for here; both arms are pinned.
5505+ let mut total_overrides = 0usize;
5506+ for (rtx, filter_intra) in [("1", "0"), ("0", "1")] {
5507+ let encode = || {
5508+ let mut child = Command::new(aomenc_path())
5509+ .args([
5510+ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=32",
5511+ "--cpu-used=4", "--threads=1", "--row-mt=0", "--sb-size=64",
5512+ "--kf-max-dist=0", "--enable-rect-partitions=1",
5513+ "--enable-ab-partitions=0", "--enable-1to4-partitions=0",
5514+ "--enable-tx-size-search=0", "--enable-cdef=0",
5515+ "--enable-restoration=0",
5516+ &format!("--enable-filter-intra={filter_intra}"),
5517+ &format!("--reduced-tx-type-set={rtx}"),
5518+ "--min-partition-size=8", "--max-partition-size=32",
5519+ "--obu", "-o", "-", "-",
5520+ ])
5521+ .stdin(Stdio::piped())
5522+ .stdout(Stdio::piped())
5523+ .stderr(Stdio::piped())
5524+ .spawn()
5525+ .expect("aomenc failed to start");
5526+ child
5527+ .stdin
5528+ .take()
5529+ .expect("aomenc stdin")
5530+ .write_all(&y4m.stdout)
5531+ .expect("writing y4m to aomenc");
5532+ let out = child.wait_with_output().expect("aomenc failed to run");
5533+ assert!(
5534+ out.status.success(),
5535+ "aomenc refused the fixture: {}",
5536+ String::from_utf8_lossy(&out.stderr)
5537+ );
5538+ out.stdout
5539+ };
5540+ let stream = encode();
5541+ assert_eq!(
5542+ stream,
5543+ encode(),
5544+ "{NAME}: aomenc output is not reproducible (rtx={rtx} filter_intra={filter_intra})"
5545+ );
5546+ let before = crate::decode::mode_mi_override_hits();
5547+ let frames = decode_stream(&stream).unwrap_or_else(|e| {
5548+ panic!("{NAME}: decode failed (rtx={rtx} filter_intra={filter_intra}): {e}")
5549+ });
5550+ // Counted only on an attempt that actually decoded AND is compared
5551+ // below -- a refusal never contributes to the firing assert.
5552+ let overrides = crate::decode::mode_mi_override_hits() - before;
5553+ let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frames.len());
5554+ assert_eq!(frames.len(), 1, "{NAME}: expected one key frame");
5555+ assert_eq!(ffmpeg_frames.len(), frames.len(), "{NAME}: ffmpeg frame count");
5556+ for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() {
5557+ assert_eq!(
5558+ got.y, want.y,
5559+ "{NAME} frame {i} luma vs ffmpeg (rtx={rtx} filter_intra={filter_intra}, \
5560+ {overrides} mi-exact mode overrides)"
5561+ );
5562+ assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (rtx={rtx})");
5563+ assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (rtx={rtx})");
5564+ }
5565+ total_overrides += overrides;
5566+ }
5567+ assert!(
5568+ total_overrides > 0,
5569+ "{NAME}: no non-strip block ever read a mi-exact neighbour that disagreed with its \
5570+ coarse slot -- the gate proves nothing"
5571+ );
5572+ eprintln!("{NAME}: pixel-exact, mode_mi_override_hits={total_overrides}");
5573+ }
5574+
5461 #[test]5575 #[test]
5462 #[ignore = "lane-rectx r3 scratch sweep"]5576 #[ignore = "lane-rectx r3 scratch sweep"]
5463 fn sweep_rectx_recipes() {5577 fn sweep_rectx_recipes() {
lanes/rectx-r5.report.mdadded+84 -0
@@ -0,0 +1,84 @@
1+# lane-rectx r5 -- three "decodes fine, wrong pixels" cells: one root cause, luma now exact in all three
2+
3+Branch `lane-rectx`, on top of r4's `fad4d5a`. r4 left three swept cells decoding to
4+completion with WRONG pixels (the worst class). All three had the SAME root cause; after the
5+fix two are byte-exact and all three are LUMA-exact, with a chroma-only residue below.
6+
7+## Root cause (found this round)
8+
9+`decode_leaf_rect` took the mi-exact intra-mode neighbour map in r4 (`mode_above_mi` /
10+`mode_left_mi`). The three OTHER `kf_y_mode` readers did not:
11+`decode_block` (square, `decode.rs` ~4960), `decode_block_rect` (32x16/16x32, ~3274) and
12+`decode_block_rect64` (64x32/32x64, ~3850) each read the COARSE per-16x16 `above_mode[c]` /
13+`left_mode[r]` slots -- which no sub-16x16 block ever writes (`record_rect` loops
14+`w / SUB` = 0 cells for a 16x8). So a 32x16 strip whose left neighbour was a 16x8 rect leaf
15+read an older, larger block's mode, indexed a DIFFERENT `kf_y_mode` CDF row, decoded the same
16+mode VALUE off a different distribution, and desynced from that block's first coefficient on.
17+This is the memory class "context read from one cell" / "CDF row held constant", one level up
18+from r4's own instance.
19+
20+Fix (`decode.rs`): one `Neighbours::modes_above_left(r, c)` helper (mi-exact if the map holds
21+exactly this block's above/left mi, else the coarse slot) used by all three readers, for the
22+mode CDF row AND for `smooth_neighbor` (libaom's `get_filt_type` reads the same `above_mi`/
23+`left_mi`). New `MODE_MI_OVERRIDE_HITS` counter (`decode.rs` ~760) counts the reads where the
24+two disagree.
25+
26+Also applied here, per the coordinator's mid-round correction (defect found by lane-sub8's
27+verifier in the code r4 cherry-picked from that lane): `mode_above_mi`/`mode_left_mi` guarded
28+availability with `mi_r > 0`/`mi_c > 0` instead of the tile-relative
29+`mi_r > tile_row0_mi`/`mi_c > tile_col0_mi`, and the maps were never reset -- `start_tile` now
30+clears `sub8_mode_col` over the tile's mi columns and `start_row` clears `sub8_mode_row`,
31+exactly like every other neighbour array beside them. (This lane's sweep is single-tile, so
32+that half is code-verified here, not gate-proven; lane-sub8 r6 owns the multi-tile gate.)
33+
34+## Evidence
35+
36+EVIDENCE: /tmp/.../scratchpad/rx/{rgb240.obu,pre_a.f0,pre_o.f0,o.step.txt,a.step.txt} |
37+ffmpeg lavfi `rgbtestsrc=size=64x64` -> aomenc `--cq-level=24 --reduced-tx-type-set=0
38+--enable-rect-partitions=1 --min-partition-size=8 --max-partition-size=32
39+--enable-filter-intra=0 --enable-cdef=0 --enable-restoration=0` (sha256
40+22113636b7ab950b5d1a175a60b9e01251dcead819f99a9ef14dcd1378ccfad6, reproducible across two
41+encodes); `EC_TRACE_MODE_STEP` ladder ours vs instrumented aomdec, `EC_AV1_PREFILT_DUMP` both |
42+first divergent element = `mi_row=8 mi_col=8 name=mode val=2` at range 53452 (oracle) vs 33402
43+(ours) -- same value, different CDF row, entry ranges equal (56121) so it is the ROW not the
44+symbol; that block's left neighbour is the 16x8 leaf at mi(8,4) mode=12. Pre-filter luma diff
45+1006 -> 0, total 1650 -> 12 bytes (all chroma).
46+
47+EVIDENCE: gate `a_real_aomenc_stream_whose_square_block_reads_a_sub16_neighbours_mode_decodes_pixel_exact`
48+(`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` |
49+`pixel-exact, mode_mi_override_hits=2`, two pinned arms (`rgbtestsrc` 64x64 cq32 with
50+`--reduced-tx-type-set=1 --enable-filter-intra=0` and with `=0 --enable-filter-intra=1`), each
51+encoded twice and byte-compared before decode; the counter is read only on an attempt that
52+decoded AND is pixel-compared.
53+
54+EVIDENCE: /tmp/.../scratchpad/rx/{rgb240,rgb241,rgb321,mandfi161}.{obu,ours.yuv,ref.yuv} |
55+`examples/decode_probe <obu> <yuv>` vs `ffmpeg -i <obu> -f rawvideo`, per-plane byte diff |
56+per cell, before -> after this round: rgbtestsrc cq24 rtx=0 1650 -> 12 (luma 1118 -> 0);
57+rgbtestsrc cq24 rtx=1 3461 -> 14 (luma 2304 -> 0); rgbtestsrc cq32 rtx=1 1622 -> 0;
58+rgbtestsrc cq32 `--enable-filter-intra=1` rtx=0 -> 0; mandelbrot start_x=-0.6 cq16
59+`--enable-filter-intra=1` rtx=1 126 -> 123 (luma 0, U 40, V 83).
60+
61+## Open residue
62+
63+- fix-now(next round, needs its own lane -- it is a CHROMA PREDICTION defect, not a rect one):
64+ every remaining mismatch is chroma-only with luma byte-exact and the entropy ladder
65+ IDENTICAL end to end (`diff` of the two `EC_ISTEP` traces on mandfi161 is empty apart from
66+ two `use_filter_intra` lines the oracle prints and we do not -- ranges match, so the symbol
67+ is consumed). Localised per 4x4 chroma block on mandelbrot cq16 fi=1: the three FULLY wrong
68+ blocks (16/16 samples) are mi(10,4) V, mi(10,6) V, mi(12,12) U -- all three `uv_mode=13`
69+ (`UV_CFL_PRED`), one plane wrong while the other is exact, i.e. the per-plane CFL alpha, not
70+ the shared luma AC. The partially wrong blocks (1-13 samples, max delta 9) are all
71+ directional chroma with a NON-ZERO `angle_uv` (mi(8,12) uv=4 delta -3, mi(10,2) uv=4 delta 3,
72+ mi(12,8) uv=6 delta -3, ...), i.e. the chroma intra-edge filter/upsample path. Ruled out this
73+ round by experiment: `smooth_neighbor_uv` is NOT the cause -- forcing it true and false
74+ globally leaves the diff at 123 bytes either way (rgb240: 12/13/12).
75+- deferred(a lane for AB partitions below 16x16): unchanged from r4, `a HORZ_A/HORZ_B/VERT_A
76+ partition below 16x16` still dominates the refusals in the sweep.
77+- accepted: `read_coeffs_rect` still refuses `V_DCT`/`H_DCT` on a rect transform
78+ (`--reduced-tx-type-set=0` cells can hit it: rgbtestsrc cq32 rtx=0 fi=0 refuses by name).
79+- accepted: `filter intra on a HORZ/VERT strip` still refuses (4 of the 6 filter-intra cells
80+ swept above stop there, non-silently).
81+
82+## Refusals
83+
84+None lifted or added this round; `refusal_inventory` and `gate_coverage` unchanged and green.