diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a4498ee4..415ad419 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1784,6 +1784,24 @@ pub(crate) fn mode_mi_override_hits() -> usize { MODE_MI_OVERRIDE_HITS.with(|c| c.get()) } +// lane-modectx r1: how many times a `kf_y_mode`/`y_mode` neighbour read at a +// TILE edge found its coarse fallback band holding a NON-DC mode -- i.e. a +// mode from the previous tile, where libaom's `up_available`/`left_available` +// make the neighbour DC_PRED. Measured 0 over 20 two-tile-column streams: +// `above_mode` is cleared per tile in `Neighbours::start_tile` and `left_mode` +// per superblock row in `start_row`, so the luma fallback cannot leak the way +// lane-mtfix's `above_uv_mode` one did. The gate asserts it stays 0 -- this is +// the invariant that makes the coarse fallback safe. +thread_local! { + static MODE_TILE_EDGE_COARSE_LEAKS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`MODE_TILE_EDGE_COARSE_LEAKS`]. +pub(crate) fn mode_tile_edge_coarse_leaks() -> usize { + MODE_TILE_EDGE_COARSE_LEAKS.with(|c| c.get()) +} + // lane-cfl r1: how many times a chroma intra-edge filter-type read // (`get_filt_type`, libaom reconintra.c) took an above/left UV mode from the // mi-exact map that DIFFERS from its coarse 16x16 slot -- i.e. how often the @@ -4346,13 +4364,29 @@ impl Neighbours { (above, left) } + /// lane-modectx r1: the coarse-[`SUB`]-grid entry point, now availability + /// correct. The mi-exact lookups already refuse to cross a tile edge, but + /// their FALLBACK (`above_mode`/`left_mode`) did not: `left_mode` is reset + /// per superblock row and never per tile, so the first block column of + /// tile 2..N read the previous tile's mode as its left neighbour, and the + /// above band survives across a tile ROW boundary the same way lane-mtfix's + /// `above_uv_mode` did. libaom reads `above_mbmi`/`left_mbmi` only when + /// `up_available`/`left_available` and uses DC_PRED otherwise + /// ([`Self::modes_above_left_mi`]). fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) { let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); - 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]); + let (above, left) = + self.modes_above_left_mi(mi_r, mi_c, (self.above_mode[c], 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)); } + // A neighbour the coarse band offered where the tile edge makes it + // unavailable -- must never happen (see `MODE_TILE_EDGE_COARSE_LEAKS`). + if (mi_r <= self.tile_row0_mi && self.above_mode[c] != DC_PRED) + || (mi_c <= self.tile_col0_mi && self.left_mode[r] != DC_PRED) + { + MODE_TILE_EDGE_COARSE_LEAKS.with(|h| h.set(h.get() + 1)); + } (above, left) } @@ -8581,8 +8615,10 @@ fn decode_leaf8( reduced_tx_set: bool, ) -> Result { let (r, c) = outer_at; - let mut above_mode = neighbours.above_mode[c]; - let mut left_mode = neighbours.left_mode[r]; + // lane-modectx r1: the group's own availability-correct fallback pair, not + // the raw coarse bands (which leak across a tile edge -- see + // `modes_above_left`). + let (mut above_mode, mut left_mode) = neighbours.modes_above_left(r, c); if let Some(((pr, pc), pmode)) = prev_leaf { if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { above_mode = pmode; @@ -9263,8 +9299,9 @@ fn decode_leaf_rect8( reduced_tx_set: bool, ) -> Result<(usize, usize)> { let (r, c) = outer_at; - let above_mode = neighbours.above_mode[c]; - let left_mode = neighbours.left_mode[r]; + // lane-modectx r1: availability-correct fallback pair (see + // `modes_above_left`), not the raw coarse bands. + let (above_mode, left_mode) = neighbours.modes_above_left(r, c); let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c); let (bw, bh) = if vert { (4, 8) } else { (8, 4) }; let (w_mi, h_mi) = (bw / MI, bh / MI); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 74e19ba9..070b728a 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -13859,6 +13859,13 @@ mod tests { let (width, height) = (128usize, 64usize); let mut matched = 0u32; let mut named_refusals = 0u32; + // lane-modectx r1: luma `kf_y_mode` neighbour reads whose coarse + // fallback band held a previous tile's mode at a tile edge. Measured 0 + // -- `above_mode` is cleared per tile and `left_mode` per superblock + // row -- and that is exactly why the coarse fallback is safe where + // lane-mtfix's chroma one was not. Accumulated only for attempts that + // then compared pixel-exact (class counter-from-refused-stream). + let mut mode_edge_leaks = 0usize; for attempt in 0..20u32 { let seed = 42 + attempt; let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); @@ -14001,6 +14008,7 @@ mod tests { .is_some_and(|seq| seq.enable_intra_edge_filter); let before = crate::decode::tile_hits(); + let before_edge = crate::decode::mode_tile_edge_coarse_leaks(); let picture = match decode_key_frame_tile_with_cdfs( &tile_bufs, &header.tile_info, @@ -14051,6 +14059,7 @@ mod tests { assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); + mode_edge_leaks += crate::decode::mode_tile_edge_coarse_leaks() - before_edge; matched += 1; } assert!( @@ -14058,8 +14067,15 @@ mod tests { "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \ a two-tile-column stream" ); + assert_eq!( + mode_edge_leaks, 0, + "{NAME}: a luma mode neighbour read at a tile edge saw a NON-DC coarse band -- the \ + per-tile `above_mode` / per-superblock-row `left_mode` reset that makes the coarse \ + fallback safe has regressed" + ); eprintln!( - "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" + "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20, \ + {mode_edge_leaks} tile-edge coarse leaks" ); } diff --git a/lanes/modectx-r1.report.md b/lanes/modectx-r1.report.md new file mode 100644 index 00000000..d354c942 --- /dev/null +++ b/lanes/modectx-r1.report.md @@ -0,0 +1,93 @@ +# lane-modectx r1 — the luma mode-context readers, and the charter's premise + +Base: `lane-rectsplitx` cbf8ffd (NOT rebased onto main — `git rebase main` conflicts in +`crates/ec-av1/src/decode.rs` + `refusal_inventory.rs` because lane-rectsplitx is not merged +yet; aborted, work stays on cbf8ffd as chartered. Deviation from COMMON's "rebase first"). + +## 1. The pinned defect does NOT reproduce on this base (charter premise stale) + +Stream pinned by the charter (192x128, 8-bit, seed 48, axis Y, step 103, noise 12, cq 32, +32x32_level_1to4 recipe), regenerated twice: + +``` +36cd6fae68d91d4b1fdbb77d7a7854b46a535f727a8b12094411ab0185d07673 a.obu +36cd6fae68d91d4b1fdbb77d7a7854b46a535f727a8b12094411ab0185d07673 b.obu +``` + +There is no `mode` divergence at mi(24,8). Our `EC_TRACE_MODE_STEP` ladder is IDENTICAL to the +instrumented aomdec's (`name=dq` excluded: our `val` field carries a different quantity there, +range identical) up to the last element we read, and we then stop on a REFUSAL, not a desync: + +* ours: 48 non-dq ladder lines, last `mi_row=6 mi_col=8 name=angle_uv val=0 rng=37000` + == aomdec line 48 exactly; then + `REFUSED: unsupported: AV1 tile (a HORZ_A/HORZ_B/VERT_A partition below 16x16 ...)`. +* aomdec `EC_TRACE=1` confirms the stream really does code that partition: + `EC_PART mi_row=8 mi_col=0 bsize=6 ctx=4 ... EC_PART_VAL value=4` (= `PARTITION_HORZ_A` at + 16x16) — aomenc emits an AB partition at 16x16 despite `--enable-ab-partitions=0`. + So the cq-32 arm of this fixture is blocked by a real, named capability gap (16x16 AB + partitions), not by a mode-context defect. + +At the gate's own cq for this attempt (attempt 6 uses `--cq-level=45`, seed 48 axis Y step 103 +noise 12 — i.e. the charter's stream IS already in the gate window) the frame decodes and the +whole ladder matches, before and after this lane's change. + +Conclusion: the mi(24,8) `kf_y_mode` divergence lane-band63 r1 pinned was fixed by +lane-rectsplitx ec0b3b5 ("a 1:4 strip read its neighbour MODE from a 16px-coarse band"), which +is in this lane's base. Item (4) of the charter (add seed 48 to the gate window) needs no new +pin: it is attempt 6 of the existing window and passes. + +EVIDENCE: /tmp/.../scratchpad/{a.obu,ours.log,ref.log,ours45.log,ref45.log} | regenerate the +pinned stream twice (identical sha256), decode with EC_TRACE_MODE_STEP under ours + instrumented +aomdec, diff ladders | cq32: 48/48 ladder lines identical then a named refusal (aomdec EC_PART +value=4 = HORZ_A at 16x16); cq45: 0 ladder differences, `OK: 1 frames decoded`. + +## 2. What did change: availability-correct coarse fallback (class new-map-ignores-tile-edge) + +`Neighbours::modes_above_left` (decode.rs:4005) — the entry point of every remaining coarse +reader (`decode_block_rect` :5182, `decode_block_rect64` :6169, `decode_block` :7442) — took the +mi-exact override first but then fell back to the raw `above_mode`/`left_mode` band with NO +tile-relative availability. It now routes through `modes_above_left_mi`, which returns `DC_PRED` +where libaom's `up_available`/`left_available` are false. Same for the two raw-band leaf sites, +`decode_leaf_split4` (:8348) and `decode_leaf_rect8` (:8547), which read +`neighbours.above_mode[c]`/`left_mode[r]` directly as their per-leaf fallback. + +Measured result of the fix: **zero behavioural change**, and that is now asserted. New counter +`MODE_TILE_EDGE_COARSE_LEAKS` counts every coarse fallback at a tile edge that held a non-DC +mode; over the 20 attempts of +`a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact` it is 0, because `above_mode` +is cleared per tile in `start_tile` and `left_mode` per superblock row in `start_row`. The gate +now asserts `== 0` — the invariant that makes the coarse fallback safe, and the thing that +would regress if either reset were dropped (that is exactly how lane-mtfix's chroma +`above_uv_mode` twin broke). + +Honest statement: this is hardening plus a regression guard, not a defect fix. No stream in the +suite changes. + +## 3. Band granularity sweep (charter item 3) + +| band | granularity | availability guard | can an 8/4-px strip alias it? | +|---|---|---|---| +| `above_mode`/`left_mode` | SUB (16px) | now tile-relative via `modes_above_left_mi` | yes, but only as a fallback — `sub8_mode_col/row` (mi) is primary and always written by `record_mode_mi` | +| `above_uv_mode`/`left_uv_mode` | SUB | tile-relative in `smooth_uv_neighbour` (mi primary, `uv_mode_col/row`) | same shape as luma; primary is mi-exact | +| `above_side`/`left_side` | SUB | — | WRITE-ONLY: no reader left (partition ctx uses `above_side_mi`/`left_side_mi`, mi-granular). Dead state, left alone this round | +| `above_side_mi`/`left_side_mi` | mi | reset in `start_tile`/`start_row` | no | +| `above_skip`/`skip_mode`/`inter`/`ref`/`ref1`/`comp_group_idx`/`compound_idx`/`filter`/`txfm` | mi | reset per tile/row | no | +| `skip_grid`/`tx_grid`/`tx_h_grid`/`uv_tx_grid`/`ref_grid`/`delta_lf_grid` | mi, 2-D | absolute mi coords | no | +| `above_palette_size/colors`, `left_palette_size/colors` (+ `_uv_`) | **SUB, square-only writer** | above: `r % 4 != 0`; left: tile-relative | **YES** — `record_palette_y` loops `0..side/SUB`, so a 32x8 / 8x32 palette strip writes NOTHING on its short axis and leaves an older block's palette cache in the cell; four 32x8 strips also share one left cell | + +The palette row is the one live instance of the charter's "last strip wins" class that remains. +It is NOT fixed here: every 1:4 / rect-strip gate runs `--enable-palette=0`, so there is no +stream in the repo that can prove or disprove it, and a blind fix would be an unverified change +to lane-palette2's territory. +Disposition: deferred(needs a real aomenc stream with `--enable-palette=1` that codes a 32x8 or +8x32 palette strip — same fixture family, palette enabled, screen-content source). + +## 4. Suite + +`cargo test -p ec-av1 --lib` (systemd unit, EC_AV1_REQUIRE_AOMENC=1): **335 passed; 0 failed; 29 ignored** (768.77s), log `$HOME/.cache/modectx-suite.log`. + +EVIDENCE: $HOME/.cache/modectx-suite.log | `systemd-run --user --unit=modectx-suite-1788316736 ... cargo test -p ec-av1 --lib -j3` on 4339963 | test result: ok. 335 passed; 0 failed; 29 ignored + +Gate re-run of the charter siblings is inside that suite run (32x32_level_1to4, superblock_level_1to4, +split_transform_*, coded_rect_strip_below_16x16, sub8_*, filter_intra_*, multi_tile/two+four tile +columns and tile rows, tiny sweep, refusal_inventory, gate_coverage — all ok).