forge
Explore
Log in

Don't you dare go Hollow

The luma mode-context coarse fallback ignored tile availability

Browse files at this commit

353ada15875bc7088143cf70061d8ddb1d0ab11dAhmet Kaan GümüşParent: 928732c
  • fix
    • the luma mode-context coarse fallback ignored tile availability
Show full message
fix(av1): the luma mode-context coarse fallback ignored tile availability

`Neighbours::modes_above_left` -- the entry point of every remaining coarse
`kf_y_mode`/`y_mode` neighbour reader (`decode_block_rect`,
`decode_block_rect64`, `decode_block`) -- took the mi-exact override first but
then fell back to the raw `above_mode`/`left_mode` band with no tile-relative
availability, and `decode_leaf_split4` / `decode_leaf_rect8` read those bands
directly. All five now go through `modes_above_left_mi`, which yields DC_PRED
where libaom's `up_available`/`left_available` are false.

Measured: zero behavioural change. The new `MODE_TILE_EDGE_COARSE_LEAKS`
counter -- coarse fallbacks at a tile edge holding a non-DC mode -- is 0 across
the 20 attempts of the two-tile-column gate, because `above_mode` is cleared
per tile in `start_tile` and `left_mode` per superblock row in `start_row`.
That gate now asserts it stays 0: it is the invariant the coarse fallback rests
on, and dropping either reset is exactly how lane-mtfix's chroma
`above_uv_mode` twin broke.

The charter's pinned mi(24,8) divergence does not reproduce on this base --
lane-rectsplitx ec0b3b5 fixed it; at cq 32 that fixture now stops on a real
16x16 AB-partition refusal (aomdec EC_PART value=4). See lanes/modectx-r1.report.md.
crates/ec-av1/src/decode.rsmodified+43 -6
@@ -1525,6 +1525,24 @@ pub(crate) fn mode_mi_override_hits() -> usize {
1525 MODE_MI_OVERRIDE_HITS.with(|c| c.get())1525 MODE_MI_OVERRIDE_HITS.with(|c| c.get())
1526 }1526 }
1527 1527
1528+// lane-modectx r1: how many times a `kf_y_mode`/`y_mode` neighbour read at a
1529+// TILE edge found its coarse fallback band holding a NON-DC mode -- i.e. a
1530+// mode from the previous tile, where libaom's `up_available`/`left_available`
1531+// make the neighbour DC_PRED. Measured 0 over 20 two-tile-column streams:
1532+// `above_mode` is cleared per tile in `Neighbours::start_tile` and `left_mode`
1533+// per superblock row in `start_row`, so the luma fallback cannot leak the way
1534+// lane-mtfix's `above_uv_mode` one did. The gate asserts it stays 0 -- this is
1535+// the invariant that makes the coarse fallback safe.
1536+thread_local! {
1537+ static MODE_TILE_EDGE_COARSE_LEAKS: std::cell::Cell<usize> =
1538+ const { std::cell::Cell::new(0) };
1539+}
1540+
1541+/// Current value of [`MODE_TILE_EDGE_COARSE_LEAKS`].
1542+pub(crate) fn mode_tile_edge_coarse_leaks() -> usize {
1543+ MODE_TILE_EDGE_COARSE_LEAKS.with(|c| c.get())
1544+}
1545+
1528 // lane-cfl r1: how many times a chroma intra-edge filter-type read1546 // lane-cfl r1: how many times a chroma intra-edge filter-type read
1529 // (`get_filt_type`, libaom reconintra.c) took an above/left UV mode from the1547 // (`get_filt_type`, libaom reconintra.c) took an above/left UV mode from the
1530 // mi-exact map that DIFFERS from its coarse 16x16 slot -- i.e. how often the1548 // mi-exact map that DIFFERS from its coarse 16x16 slot -- i.e. how often the
@@ -4002,13 +4020,29 @@ impl Neighbours {
4002 (above, left)4020 (above, left)
4003 }4021 }
4004 4022
4023+ /// lane-modectx r1: the coarse-[`SUB`]-grid entry point, now availability
4024+ /// correct. The mi-exact lookups already refuse to cross a tile edge, but
4025+ /// their FALLBACK (`above_mode`/`left_mode`) did not: `left_mode` is reset
4026+ /// per superblock row and never per tile, so the first block column of
4027+ /// tile 2..N read the previous tile's mode as its left neighbour, and the
4028+ /// above band survives across a tile ROW boundary the same way lane-mtfix's
4029+ /// `above_uv_mode` did. libaom reads `above_mbmi`/`left_mbmi` only when
4030+ /// `up_available`/`left_available` and uses DC_PRED otherwise
4031+ /// ([`Self::modes_above_left_mi`]).
4005 fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) {4032 fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) {
4006 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));4033 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
4007- let above = self.mode_above_mi(mi_r, mi_c).unwrap_or(self.above_mode[c]);4034+ let (above, left) =
4008- let left = self.mode_left_mi(mi_r, mi_c).unwrap_or(self.left_mode[r]);4035+ self.modes_above_left_mi(mi_r, mi_c, (self.above_mode[c], self.left_mode[r]));
4009 if above != self.above_mode[c] || left != self.left_mode[r] {4036 if above != self.above_mode[c] || left != self.left_mode[r] {
4010 MODE_MI_OVERRIDE_HITS.with(|h| h.set(h.get() + 1));4037 MODE_MI_OVERRIDE_HITS.with(|h| h.set(h.get() + 1));
4011 }4038 }
4039+ // A neighbour the coarse band offered where the tile edge makes it
4040+ // unavailable -- must never happen (see `MODE_TILE_EDGE_COARSE_LEAKS`).
4041+ if (mi_r <= self.tile_row0_mi && self.above_mode[c] != DC_PRED)
4042+ || (mi_c <= self.tile_col0_mi && self.left_mode[r] != DC_PRED)
4043+ {
4044+ MODE_TILE_EDGE_COARSE_LEAKS.with(|h| h.set(h.get() + 1));
4045+ }
4012 (above, left)4046 (above, left)
4013 }4047 }
4014 4048
@@ -7899,8 +7933,10 @@ fn decode_leaf8(
7899 reduced_tx_set: bool,7933 reduced_tx_set: bool,
7900 ) -> Result<usize> {7934 ) -> Result<usize> {
7901 let (r, c) = outer_at;7935 let (r, c) = outer_at;
7902- let mut above_mode = neighbours.above_mode[c];7936+ // lane-modectx r1: the group's own availability-correct fallback pair, not
7903- let mut left_mode = neighbours.left_mode[r];7937+ // the raw coarse bands (which leak across a tile edge -- see
7938+ // `modes_above_left`).
7939+ let (mut above_mode, mut left_mode) = neighbours.modes_above_left(r, c);
7904 if let Some(((pr, pc), pmode)) = prev_leaf {7940 if let Some(((pr, pc), pmode)) = prev_leaf {
7905 if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {7941 if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
7906 above_mode = pmode;7942 above_mode = pmode;
@@ -8544,8 +8580,9 @@ fn decode_leaf_rect8(
8544 reduced_tx_set: bool,8580 reduced_tx_set: bool,
8545 ) -> Result<(usize, usize)> {8581 ) -> Result<(usize, usize)> {
8546 let (r, c) = outer_at;8582 let (r, c) = outer_at;
8547- let above_mode = neighbours.above_mode[c];8583+ // lane-modectx r1: availability-correct fallback pair (see
8548- let left_mode = neighbours.left_mode[r];8584+ // `modes_above_left`), not the raw coarse bands.
8585+ let (above_mode, left_mode) = neighbours.modes_above_left(r, c);
8549 let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c);8586 let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c);
8550 let (bw, bh) = if vert { (4, 8) } else { (8, 4) };8587 let (bw, bh) = if vert { (4, 8) } else { (8, 4) };
8551 let (w_mi, h_mi) = (bw / MI, bh / MI);8588 let (w_mi, h_mi) = (bw / MI, bh / MI);
crates/ec-av1/src/stream.rsmodified+17 -1
@@ -12119,6 +12119,13 @@ mod tests {
12119 let (width, height) = (128usize, 64usize);12119 let (width, height) = (128usize, 64usize);
12120 let mut matched = 0u32;12120 let mut matched = 0u32;
12121 let mut named_refusals = 0u32;12121 let mut named_refusals = 0u32;
12122+ // lane-modectx r1: luma `kf_y_mode` neighbour reads whose coarse
12123+ // fallback band held a previous tile's mode at a tile edge. Measured 0
12124+ // -- `above_mode` is cleared per tile and `left_mode` per superblock
12125+ // row -- and that is exactly why the coarse fallback is safe where
12126+ // lane-mtfix's chroma one was not. Accumulated only for attempts that
12127+ // then compared pixel-exact (class counter-from-refused-stream).
12128+ let mut mode_edge_leaks = 0usize;
12122 for attempt in 0..20u32 {12129 for attempt in 0..20u32 {
12123 let seed = 42 + attempt;12130 let seed = 42 + attempt;
12124 let source = gradients_source(seed, width, height, "duration=0.04:rate=25");12131 let source = gradients_source(seed, width, height, "duration=0.04:rate=25");
@@ -12261,6 +12268,7 @@ mod tests {
12261 .is_some_and(|seq| seq.enable_intra_edge_filter);12268 .is_some_and(|seq| seq.enable_intra_edge_filter);
12262 12269
12263 let before = crate::decode::tile_hits();12270 let before = crate::decode::tile_hits();
12271+ let before_edge = crate::decode::mode_tile_edge_coarse_leaks();
12264 let picture = match decode_key_frame_tile_with_cdfs(12272 let picture = match decode_key_frame_tile_with_cdfs(
12265 &tile_bufs,12273 &tile_bufs,
12266 &header.tile_info,12274 &header.tile_info,
@@ -12311,6 +12319,7 @@ mod tests {
12311 assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})");12319 assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})");
12312 assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})");12320 assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})");
12313 assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})");12321 assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})");
12322+ mode_edge_leaks += crate::decode::mode_tile_edge_coarse_leaks() - before_edge;
12314 matched += 1;12323 matched += 1;
12315 }12324 }
12316 assert!(12325 assert!(
@@ -12318,8 +12327,15 @@ mod tests {
12318 "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \12327 "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \
12319 a two-tile-column stream"12328 a two-tile-column stream"
12320 );12329 );
12330+ assert_eq!(
12331+ mode_edge_leaks, 0,
12332+ "{NAME}: a luma mode neighbour read at a tile edge saw a NON-DC coarse band -- the \
12333+ per-tile `above_mode` / per-superblock-row `left_mode` reset that makes the coarse \
12334+ fallback safe has regressed"
12335+ );
12321 eprintln!(12336 eprintln!(
12322- "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20"12337+ "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20, \
12338+ {mode_edge_leaks} tile-edge coarse leaks"
12323 );12339 );
12324 }12340 }
12325 12341
lanes/modectx-r1.report.mdadded+87 -0
@@ -0,0 +1,87 @@
1+# lane-modectx r1 — the luma mode-context readers, and the charter's premise
2+
3+Base: `lane-rectsplitx` cbf8ffd (NOT rebased onto main — `git rebase main` conflicts in
4+`crates/ec-av1/src/decode.rs` + `refusal_inventory.rs` because lane-rectsplitx is not merged
5+yet; aborted, work stays on cbf8ffd as chartered. Deviation from COMMON's "rebase first").
6+
7+## 1. The pinned defect does NOT reproduce on this base (charter premise stale)
8+
9+Stream pinned by the charter (192x128, 8-bit, seed 48, axis Y, step 103, noise 12, cq 32,
10+32x32_level_1to4 recipe), regenerated twice:
11+
12+```
13+36cd6fae68d91d4b1fdbb77d7a7854b46a535f727a8b12094411ab0185d07673 a.obu
14+36cd6fae68d91d4b1fdbb77d7a7854b46a535f727a8b12094411ab0185d07673 b.obu
15+```
16+
17+There is no `mode` divergence at mi(24,8). Our `EC_TRACE_MODE_STEP` ladder is IDENTICAL to the
18+instrumented aomdec's (`name=dq` excluded: our `val` field carries a different quantity there,
19+range identical) up to the last element we read, and we then stop on a REFUSAL, not a desync:
20+
21+* ours: 48 non-dq ladder lines, last `mi_row=6 mi_col=8 name=angle_uv val=0 rng=37000`
22+ == aomdec line 48 exactly; then
23+ `REFUSED: unsupported: AV1 tile (a HORZ_A/HORZ_B/VERT_A partition below 16x16 ...)`.
24+* aomdec `EC_TRACE=1` confirms the stream really does code that partition:
25+ `EC_PART mi_row=8 mi_col=0 bsize=6 ctx=4 ... EC_PART_VAL value=4` (= `PARTITION_HORZ_A` at
26+ 16x16) — aomenc emits an AB partition at 16x16 despite `--enable-ab-partitions=0`.
27+ So the cq-32 arm of this fixture is blocked by a real, named capability gap (16x16 AB
28+ partitions), not by a mode-context defect.
29+
30+At the gate's own cq for this attempt (attempt 6 uses `--cq-level=45`, seed 48 axis Y step 103
31+noise 12 — i.e. the charter's stream IS already in the gate window) the frame decodes and the
32+whole ladder matches, before and after this lane's change.
33+
34+Conclusion: the mi(24,8) `kf_y_mode` divergence lane-band63 r1 pinned was fixed by
35+lane-rectsplitx ec0b3b5 ("a 1:4 strip read its neighbour MODE from a 16px-coarse band"), which
36+is in this lane's base. Item (4) of the charter (add seed 48 to the gate window) needs no new
37+pin: it is attempt 6 of the existing window and passes.
38+
39+EVIDENCE: /tmp/.../scratchpad/{a.obu,ours.log,ref.log,ours45.log,ref45.log} | regenerate the
40+pinned stream twice (identical sha256), decode with EC_TRACE_MODE_STEP under ours + instrumented
41+aomdec, diff ladders | cq32: 48/48 ladder lines identical then a named refusal (aomdec EC_PART
42+value=4 = HORZ_A at 16x16); cq45: 0 ladder differences, `OK: 1 frames decoded`.
43+
44+## 2. What did change: availability-correct coarse fallback (class new-map-ignores-tile-edge)
45+
46+`Neighbours::modes_above_left` (decode.rs:4005) — the entry point of every remaining coarse
47+reader (`decode_block_rect` :5182, `decode_block_rect64` :6169, `decode_block` :7442) — took the
48+mi-exact override first but then fell back to the raw `above_mode`/`left_mode` band with NO
49+tile-relative availability. It now routes through `modes_above_left_mi`, which returns `DC_PRED`
50+where libaom's `up_available`/`left_available` are false. Same for the two raw-band leaf sites,
51+`decode_leaf_split4` (:8348) and `decode_leaf_rect8` (:8547), which read
52+`neighbours.above_mode[c]`/`left_mode[r]` directly as their per-leaf fallback.
53+
54+Measured result of the fix: **zero behavioural change**, and that is now asserted. New counter
55+`MODE_TILE_EDGE_COARSE_LEAKS` counts every coarse fallback at a tile edge that held a non-DC
56+mode; over the 20 attempts of
57+`a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact` it is 0, because `above_mode`
58+is cleared per tile in `start_tile` and `left_mode` per superblock row in `start_row`. The gate
59+now asserts `== 0` — the invariant that makes the coarse fallback safe, and the thing that
60+would regress if either reset were dropped (that is exactly how lane-mtfix's chroma
61+`above_uv_mode` twin broke).
62+
63+Honest statement: this is hardening plus a regression guard, not a defect fix. No stream in the
64+suite changes.
65+
66+## 3. Band granularity sweep (charter item 3)
67+
68+| band | granularity | availability guard | can an 8/4-px strip alias it? |
69+|---|---|---|---|
70+| `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` |
71+| `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 |
72+| `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 |
73+| `above_side_mi`/`left_side_mi` | mi | reset in `start_tile`/`start_row` | no |
74+| `above_skip`/`skip_mode`/`inter`/`ref`/`ref1`/`comp_group_idx`/`compound_idx`/`filter`/`txfm` | mi | reset per tile/row | no |
75+| `skip_grid`/`tx_grid`/`tx_h_grid`/`uv_tx_grid`/`ref_grid`/`delta_lf_grid` | mi, 2-D | absolute mi coords | no |
76+| `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 |
77+
78+The palette row is the one live instance of the charter's "last strip wins" class that remains.
79+It is NOT fixed here: every 1:4 / rect-strip gate runs `--enable-palette=0`, so there is no
80+stream in the repo that can prove or disprove it, and a blind fix would be an unverified change
81+to lane-palette2's territory.
82+Disposition: deferred(needs a real aomenc stream with `--enable-palette=1` that codes a 32x8 or
83+8x32 palette strip — same fixture family, palette enabled, screen-content source).
84+
85+## 4. Suite
86+
87+`cargo test -p ec-av1 --lib` (systemd unit, EC_AV1_REQUIRE_AOMENC=1): SUITE_RESULT_PLACEHOLDER