diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index d4e361ff..514d7b81 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1627,6 +1627,23 @@ pub(crate) fn uv_mode_mi_override_hits() -> usize { UV_MODE_MI_OVERRIDE_HITS.with(|c| c.get()) } +// lane-mtfix r1: how many times a chroma intra-edge filter-type read was +// suppressed at a TILE edge that is not a frame edge -- the above/left +// `uv_mode` neighbour of a block on a tile's first mi row/column lives in +// another tile, so libaom's `above_mbmi`/`left_mbmi` are NULL there. The +// coarse [`SUB`] maps are frame-sized and (`above_uv_mode`) not cleared per +// tile, so without the guard in [`Neighbours::smooth_uv_neighbour`] the tile +// above would decide this tile's chroma edge filter strength. +thread_local! { + static UV_TILE_EDGE_SUPPRESSED_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`UV_TILE_EDGE_SUPPRESSED_HITS`]. +pub(crate) fn uv_tile_edge_suppressed_hits() -> usize { + UV_TILE_EDGE_SUPPRESSED_HITS.with(|c| c.get()) +} + // lane-cfl r1 gate counters: `UV_CFL_PRED` blocks (every `cfl_alpha_signs` // read) and chroma blocks with a NONZERO `angle_delta_uv` -- the two block // shapes the r1 chroma defect showed up on. @@ -4048,13 +4065,29 @@ impl Neighbours { /// planes 1/2. Falls back to the coarse [`SUB`] slot on whichever axis no /// block has recorded the exact neighbouring mi. fn smooth_uv_neighbour(&self, mi_r: usize, mi_c: usize, r: usize, c: usize) -> bool { + // lane-mtfix r1: availability is TILE-relative on BOTH axes and on the + // coarse fallback too. `above_uv_mode` is a frame-wide [`SUB`]-grid + // array that `start_tile` does not clear, so a block on a tile's first + // mi row used to read the uv_mode of the block above it in the tile + // ABOVE and filter its chroma edge at that neighbour's strength + // (2x2-tile intra gate, seed 46: chroma-only +-1..2 over the 8x8 block + // at chroma (120, 32), the top-right tile's first block). + let have_above = mi_r > self.tile_row0_mi; + let have_left = mi_c > self.tile_col0_mi; + if self.tile_row0_mi > 0 && !have_above && self.above_uv_mode[c] != DC_PRED + || self.tile_col0_mi > 0 && !have_left && self.left_uv_mode[r] != DC_PRED + { + UV_TILE_EDGE_SUPPRESSED_HITS.with(|h| h.set(h.get() + 1)); + } let above = match self.uv_mode_col.get(mi_c) { - Some(&(row, m)) if mi_r > self.tile_row0_mi && row == mi_r - 1 => m, - _ => self.above_uv_mode[c], + Some(&(row, m)) if have_above && row == mi_r - 1 => m, + _ if have_above => self.above_uv_mode[c], + _ => DC_PRED, }; let left = match self.uv_mode_row.get(mi_r) { - Some(&(col, m)) if mi_c > self.tile_col0_mi && col == mi_c - 1 => m, - _ => self.left_uv_mode[r], + Some(&(col, m)) if have_left && col == mi_c - 1 => m, + _ if have_left => self.left_uv_mode[r], + _ => DC_PRED, }; // lane-cfl r1 counter: how often the mi-exact chroma neighbour differs // from the coarse [`SUB`] slot -- i.e. how often a chroma edge would diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index d27020fa..85e72945 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -16978,6 +16978,7 @@ mod tests { let expected_tiles = (1usize << tile_cols_log2) * (1usize << tile_rows_log2); let mut matched = 0u32; let mut named_refusals = 0u32; + let mut uv_edge_suppressed = 0usize; let n_attempts: u32 = std::env::var("EC_TILES_GATE_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) @@ -17129,6 +17130,7 @@ mod tests { let before = crate::decode::tile_hits(); let pal_before = crate::decode::palette_tile_left_hits(); + let uv_edge_before = crate::decode::uv_tile_edge_suppressed_hits(); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); @@ -17176,6 +17178,13 @@ mod tests { (seed {seed})" ); } + // lane-mtfix r1: the chroma edge-filter neighbour of a block on a + // tile's first mi row/column lives in the tile above/left, where + // libaom has no neighbour at all. This counts the reads suppressed + // there whose frame-wide coarse slot held a non-DC mode -- the + // reads that would otherwise pick the wrong chroma edge filter + // strength (they did, before this fix: seed 46 of this gate). + uv_edge_suppressed += crate::decode::uv_tile_edge_suppressed_hits() - uv_edge_before; matched += 1; } assert!( @@ -17183,21 +17192,29 @@ mod tests { "{name}: every attempt refused ({named_refusals}); the gate never decoded a \ multi-tile stream with the coding tools on" ); - eprintln!("{name}: {matched} pixel-exact matches, {named_refusals} named refusals"); + eprintln!( + "{name}: {matched} pixel-exact matches, {named_refusals} named refusals, \ + {uv_edge_suppressed} chroma edge reads suppressed at a tile edge" + ); } #[test] fn a_real_aomenc_multi_tile_intra_stream_decodes_pixel_exact() { - run_multi_tile_gate( - "a_real_aomenc_multi_tile_intra_stream_decodes_pixel_exact", - 1, - 1, - 8, - 1, - &[], - false, - None, - ); + const NAME: &str = "a_real_aomenc_multi_tile_intra_stream_decodes_pixel_exact"; + let before = crate::decode::uv_tile_edge_suppressed_hits(); + run_multi_tile_gate(NAME, 1, 1, 8, 1, &[], false, None); + // lane-mtfix r1: this is the only arm with a tile ROW boundary, so it + // is the one that exercises the tile-relative guard on the chroma + // edge-filter neighbour. Without the guard, seed 46's first block of + // the bottom-right tile read the tile ABOVE it and mis-filtered its + // chroma edge (U/V off by 1..2 over chroma x[120..127] y[32..37]). + if have_ffmpeg() && have_aomenc() { + assert!( + crate::decode::uv_tile_edge_suppressed_hits() > before, + "{NAME}: no chroma edge-filter neighbour read was suppressed at a tile edge -- \ + the tile-relative guard is untested by this gate" + ); + } } #[test] diff --git a/lanes/mtfix-r1.report.md b/lanes/mtfix-r1.report.md new file mode 100644 index 00000000..768f8bd9 --- /dev/null +++ b/lanes/mtfix-r1.report.md @@ -0,0 +1,80 @@ +# lane-mtfix r1 — the multi-tile intra gate's U-plane failure on main 11633d7 + +## What was wrong + +`Neighbours::smooth_uv_neighbour` (`crates/ec-av1/src/decode.rs`) decides the chroma +intra-edge filter strength from the above/left `uv_mode` neighbour (libaom +`get_intra_edge_filter_type`, reconintra.c). Its mi-exact maps (`uv_mode_col`/`uv_mode_row`) +were tile-guarded, but the COARSE fallbacks were not: on a miss it read +`above_uv_mode[c]` / `left_uv_mode[r]` unconditionally. `above_uv_mode` is a frame-wide +`SUB`-grid array and is the ONE `above_*` band `start_tile` never clears (checked by +enumerating the struct's `above_*` fields against `start_tile`'s reset list: +`above_txfm` and `above_palette_*` are missing from the reset too, but every reader of +those is already guarded by `mi_r > tile_row0_mi` / `mi_c > tile_col0_mi`). + +So the first block of a tile ROW read the `uv_mode` of the block above it **in the tile +above**, where libaom has `above_mbmi == NULL`, and filtered its chroma edge at the wrong +strength — luma untouched. + +This is class `new-map-ignores-tile-edge`. It was not a regression of working code: at +56ea250 seed 46 REFUSED (`palette syntax is consumed for square blocks only`); the +palette2 merge lifted that refusal and the defect behind it became visible +(class `refusal-hides-a-defect`). + +## Bisect + +| commit | gate `a_real_aomenc_multi_tile_intra_stream_decodes_pixel_exact` | +|---|---| +| 56ea250 (lane-ab16 merge) | ok — 5 pixel-exact matches, **1 named refusal at seed 46** | +| 11633d7 (lane-palette2 merge, main) | FAILED — frame 0 U vs ffmpeg, seed 46 | +| this branch | ok — 6 pixel-exact matches, 0 refusals, 4 tile-edge suppressions | + +EVIDENCE: $HOME/.cache/mtfix-ab16.log, $HOME/.cache/mtfix-gate1.log, $HOME/.cache/mtfix-gate2.log | ran the one gate in a detached worktree at 56ea250 and in the lane worktree at 11633d7 | pass w/ seed-46 refusal vs FAILED U plane + +## First wrong block + +`EC_AV1_GATE_DUMP` pinned the seed-46 stream (166 bytes, 256x128, `cols=2 rows=2`); ours +vs the oracle's `EC_AV1_PREFILT_DUMP` frame 0: + +* Y: 0 differing samples. +* U: 14 samples, V: 17 samples, all inside chroma x[120..127] y[32..37], magnitude 1..2. +* That is the 8x8 chroma / 16x16 luma block at luma (240, 64) — the FIRST block of the + bottom-right tile (tile row boundary y=64, tile column boundary x=128), i.e. exactly a + tile's top edge. Reconstruction was already wrong pre-loop-filter. + +EVIDENCE: /tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/b6d8a07f-96a4-4bbb-b378-af9ae25cf7c9/scratchpad/mtfix/{ours,ref}.f0 | decode_probe + aomdec, both under EC_AV1_PREFILT_DUMP, on the pinned seed-46 stream | Y diff 0, U diff 14 / V diff 17, bbox chroma x[120..127] y[32..37] + +## Fix + +`crates/ec-av1/src/decode.rs` `smooth_uv_neighbour`: availability is tile-relative on both +axes and on the coarse fallback too (`have_above = mi_r > tile_row0_mi`, +`have_left = mi_c > tile_col0_mi`); no neighbour ⇒ `DC_PRED` (not smooth), which is what +libaom's NULL `above_mbmi`/`left_mbmi` gives. Guarding the READ rather than adding +`above_uv_mode` to `start_tile`'s reset also covers the left axis and leaves the leak +observable, which is what the new counter counts. + +`crates/ec-av1/src/decode.rs` `uv_tile_edge_suppressed_hits()`: counts reads suppressed at +a tile edge that is not a frame edge AND whose coarse slot held a non-DC mode — i.e. reads +that would have picked the wrong filter strength. + +`crates/ec-av1/src/stream.rs` `run_multi_tile_gate` reports the per-gate delta, and +`a_real_aomenc_multi_tile_intra_stream_decodes_pixel_exact` (the only arm with a tile ROW +boundary) HARD-ASSERTS it is ≥ 1. + +Mutation: the un-guarded code IS main 11633d7, and that tree fails this gate at seed 46 +(row 2 of the bisect table above). + +## Gates + +* `cargo test -p ec-av1 --lib multi_tile cfl chroma_edge palette ab_partitions filter_intra refusal_inventory gate_coverage`: 31 passed, 0 failed, 3 ignored. + Counters: intra 2x2 gate 6/6 matches, 4 tile-edge suppressions; 10-bit (4x1 tiles) and + palette (2x1) arms report 0 — they have no tile row boundary, and the left band resets + every SB row, so there is nothing to suppress there. +* Full suite (`systemd-run` unit, log `$HOME/.cache/mtfix-suite.log`): **339 passed, 0 failed, 31 ignored**. + +## Residue + +* `above_txfm`, `above_palette_size`, `above_palette_colors`, `above_palette_uv_*` are still + absent from `start_tile`'s reset list — accepted: every reader of them is tile-guarded + (decode.rs:3411/3422/3505/4837/4838/9149/9150), so the arrays are dead across a tile edge. + COMMON's "reset in start_tile" rule would add 4 more loops for no behavioural change.