Merge branch 'lane-modectx': every kf_y_mode context reader uses the mi-exact, tile-relative map; coarse band leak invariant asserted
Browse files at this commit3 changed files +153 -7
- crates/ec-av1/src/decode.rs +43 -6
- crates/ec-av1/src/stream.rs +17 -1
- lanes/modectx-r1.report.md +93 -0
crates/ec-av1/src/decode.rsmodified+43 -6
| @@ -1784,6 +1784,24 @@ pub(crate) fn mode_mi_override_hits() -> usize { | |||
| 1784 | MODE_MI_OVERRIDE_HITS.with(|c| c.get()) | 1784 | MODE_MI_OVERRIDE_HITS.with(|c| c.get()) |
| 1785 | } | 1785 | } |
| 1786 | 1786 | ||
| 1787 | +// lane-modectx r1: how many times a `kf_y_mode`/`y_mode` neighbour read at a | ||
| 1788 | +// TILE edge found its coarse fallback band holding a NON-DC mode -- i.e. a | ||
| 1789 | +// mode from the previous tile, where libaom's `up_available`/`left_available` | ||
| 1790 | +// make the neighbour DC_PRED. Measured 0 over 20 two-tile-column streams: | ||
| 1791 | +// `above_mode` is cleared per tile in `Neighbours::start_tile` and `left_mode` | ||
| 1792 | +// per superblock row in `start_row`, so the luma fallback cannot leak the way | ||
| 1793 | +// lane-mtfix's `above_uv_mode` one did. The gate asserts it stays 0 -- this is | ||
| 1794 | +// the invariant that makes the coarse fallback safe. | ||
| 1795 | +thread_local! { | ||
| 1796 | + static MODE_TILE_EDGE_COARSE_LEAKS: std::cell::Cell<usize> = | ||
| 1797 | + const { std::cell::Cell::new(0) }; | ||
| 1798 | +} | ||
| 1799 | + | ||
| 1800 | +/// Current value of [`MODE_TILE_EDGE_COARSE_LEAKS`]. | ||
| 1801 | +pub(crate) fn mode_tile_edge_coarse_leaks() -> usize { | ||
| 1802 | + MODE_TILE_EDGE_COARSE_LEAKS.with(|c| c.get()) | ||
| 1803 | +} | ||
| 1804 | + | ||
| 1787 | // lane-cfl r1: how many times a chroma intra-edge filter-type read | 1805 | // lane-cfl r1: how many times a chroma intra-edge filter-type read |
| 1788 | // (`get_filt_type`, libaom reconintra.c) took an above/left UV mode from the | 1806 | // (`get_filt_type`, libaom reconintra.c) took an above/left UV mode from the |
| 1789 | // mi-exact map that DIFFERS from its coarse 16x16 slot -- i.e. how often the | 1807 | // mi-exact map that DIFFERS from its coarse 16x16 slot -- i.e. how often the |
| @@ -4346,13 +4364,29 @@ impl Neighbours { | |||
| 4346 | (above, left) | 4364 | (above, left) |
| 4347 | } | 4365 | } |
| 4348 | 4366 | ||
| 4367 | + /// lane-modectx r1: the coarse-[`SUB`]-grid entry point, now availability | ||
| 4368 | + /// correct. The mi-exact lookups already refuse to cross a tile edge, but | ||
| 4369 | + /// their FALLBACK (`above_mode`/`left_mode`) did not: `left_mode` is reset | ||
| 4370 | + /// per superblock row and never per tile, so the first block column of | ||
| 4371 | + /// tile 2..N read the previous tile's mode as its left neighbour, and the | ||
| 4372 | + /// above band survives across a tile ROW boundary the same way lane-mtfix's | ||
| 4373 | + /// `above_uv_mode` did. libaom reads `above_mbmi`/`left_mbmi` only when | ||
| 4374 | + /// `up_available`/`left_available` and uses DC_PRED otherwise | ||
| 4375 | + /// ([`Self::modes_above_left_mi`]). | ||
| 4349 | fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) { | 4376 | fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) { |
| 4350 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); | 4377 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 4351 | - let above = self.mode_above_mi(mi_r, mi_c).unwrap_or(self.above_mode[c]); | 4378 | + let (above, left) = |
| 4352 | - let left = self.mode_left_mi(mi_r, mi_c).unwrap_or(self.left_mode[r]); | 4379 | + self.modes_above_left_mi(mi_r, mi_c, (self.above_mode[c], self.left_mode[r])); |
| 4353 | if above != self.above_mode[c] || left != self.left_mode[r] { | 4380 | if above != self.above_mode[c] || left != self.left_mode[r] { |
| 4354 | MODE_MI_OVERRIDE_HITS.with(|h| h.set(h.get() + 1)); | 4381 | MODE_MI_OVERRIDE_HITS.with(|h| h.set(h.get() + 1)); |
| 4355 | } | 4382 | } |
| 4383 | + // A neighbour the coarse band offered where the tile edge makes it | ||
| 4384 | + // unavailable -- must never happen (see `MODE_TILE_EDGE_COARSE_LEAKS`). | ||
| 4385 | + if (mi_r <= self.tile_row0_mi && self.above_mode[c] != DC_PRED) | ||
| 4386 | + || (mi_c <= self.tile_col0_mi && self.left_mode[r] != DC_PRED) | ||
| 4387 | + { | ||
| 4388 | + MODE_TILE_EDGE_COARSE_LEAKS.with(|h| h.set(h.get() + 1)); | ||
| 4389 | + } | ||
| 4356 | (above, left) | 4390 | (above, left) |
| 4357 | } | 4391 | } |
| 4358 | 4392 | ||
| @@ -8581,8 +8615,10 @@ fn decode_leaf8( | |||
| 8581 | reduced_tx_set: bool, | 8615 | reduced_tx_set: bool, |
| 8582 | ) -> Result<usize> { | 8616 | ) -> Result<usize> { |
| 8583 | let (r, c) = outer_at; | 8617 | let (r, c) = outer_at; |
| 8584 | - let mut above_mode = neighbours.above_mode[c]; | 8618 | + // lane-modectx r1: the group's own availability-correct fallback pair, not |
| 8585 | - let mut left_mode = neighbours.left_mode[r]; | 8619 | + // the raw coarse bands (which leak across a tile edge -- see |
| 8620 | + // `modes_above_left`). | ||
| 8621 | + let (mut above_mode, mut left_mode) = neighbours.modes_above_left(r, c); | ||
| 8586 | if let Some(((pr, pc), pmode)) = prev_leaf { | 8622 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 8587 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { | 8623 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { |
| 8588 | above_mode = pmode; | 8624 | above_mode = pmode; |
| @@ -9263,8 +9299,9 @@ fn decode_leaf_rect8( | |||
| 9263 | reduced_tx_set: bool, | 9299 | reduced_tx_set: bool, |
| 9264 | ) -> Result<(usize, usize)> { | 9300 | ) -> Result<(usize, usize)> { |
| 9265 | let (r, c) = outer_at; | 9301 | let (r, c) = outer_at; |
| 9266 | - let above_mode = neighbours.above_mode[c]; | 9302 | + // lane-modectx r1: availability-correct fallback pair (see |
| 9267 | - let left_mode = neighbours.left_mode[r]; | 9303 | + // `modes_above_left`), not the raw coarse bands. |
| 9304 | + let (above_mode, left_mode) = neighbours.modes_above_left(r, c); | ||
| 9268 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c); | 9305 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c); |
| 9269 | let (bw, bh) = if vert { (4, 8) } else { (8, 4) }; | 9306 | let (bw, bh) = if vert { (4, 8) } else { (8, 4) }; |
| 9270 | let (w_mi, h_mi) = (bw / MI, bh / MI); | 9307 | let (w_mi, h_mi) = (bw / MI, bh / MI); |
crates/ec-av1/src/stream.rsmodified+17 -1
| @@ -13859,6 +13859,13 @@ mod tests { | |||
| 13859 | let (width, height) = (128usize, 64usize); | 13859 | let (width, height) = (128usize, 64usize); |
| 13860 | let mut matched = 0u32; | 13860 | let mut matched = 0u32; |
| 13861 | let mut named_refusals = 0u32; | 13861 | let mut named_refusals = 0u32; |
| 13862 | + // lane-modectx r1: luma `kf_y_mode` neighbour reads whose coarse | ||
| 13863 | + // fallback band held a previous tile's mode at a tile edge. Measured 0 | ||
| 13864 | + // -- `above_mode` is cleared per tile and `left_mode` per superblock | ||
| 13865 | + // row -- and that is exactly why the coarse fallback is safe where | ||
| 13866 | + // lane-mtfix's chroma one was not. Accumulated only for attempts that | ||
| 13867 | + // then compared pixel-exact (class counter-from-refused-stream). | ||
| 13868 | + let mut mode_edge_leaks = 0usize; | ||
| 13862 | for attempt in 0..20u32 { | 13869 | for attempt in 0..20u32 { |
| 13863 | let seed = 42 + attempt; | 13870 | let seed = 42 + attempt; |
| 13864 | let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); | 13871 | let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); |
| @@ -14001,6 +14008,7 @@ mod tests { | |||
| 14001 | .is_some_and(|seq| seq.enable_intra_edge_filter); | 14008 | .is_some_and(|seq| seq.enable_intra_edge_filter); |
| 14002 | 14009 | ||
| 14003 | let before = crate::decode::tile_hits(); | 14010 | let before = crate::decode::tile_hits(); |
| 14011 | + let before_edge = crate::decode::mode_tile_edge_coarse_leaks(); | ||
| 14004 | let picture = match decode_key_frame_tile_with_cdfs( | 14012 | let picture = match decode_key_frame_tile_with_cdfs( |
| 14005 | &tile_bufs, | 14013 | &tile_bufs, |
| 14006 | &header.tile_info, | 14014 | &header.tile_info, |
| @@ -14051,6 +14059,7 @@ mod tests { | |||
| 14051 | assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); | 14059 | assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); |
| 14052 | assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); | 14060 | assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); |
| 14053 | assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); | 14061 | assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); |
| 14062 | + mode_edge_leaks += crate::decode::mode_tile_edge_coarse_leaks() - before_edge; | ||
| 14054 | matched += 1; | 14063 | matched += 1; |
| 14055 | } | 14064 | } |
| 14056 | assert!( | 14065 | assert!( |
| @@ -14058,8 +14067,15 @@ mod tests { | |||
| 14058 | "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \ | 14067 | "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \ |
| 14059 | a two-tile-column stream" | 14068 | a two-tile-column stream" |
| 14060 | ); | 14069 | ); |
| 14070 | + assert_eq!( | ||
| 14071 | + mode_edge_leaks, 0, | ||
| 14072 | + "{NAME}: a luma mode neighbour read at a tile edge saw a NON-DC coarse band -- the \ | ||
| 14073 | + per-tile `above_mode` / per-superblock-row `left_mode` reset that makes the coarse \ | ||
| 14074 | + fallback safe has regressed" | ||
| 14075 | + ); | ||
| 14061 | eprintln!( | 14076 | eprintln!( |
| 14062 | - "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" | 14077 | + "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20, \ |
| 14078 | + {mode_edge_leaks} tile-edge coarse leaks" | ||
| 14063 | ); | 14079 | ); |
| 14064 | } | 14080 | } |
| 14065 | 14081 | ||
lanes/modectx-r1.report.mdadded+93 -0
| @@ -0,0 +1,93 @@ | |||
| 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): **335 passed; 0 failed; 29 ignored** (768.77s), log `$HOME/.cache/modectx-suite.log`. | ||
| 88 | + | ||
| 89 | +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 | ||
| 90 | + | ||
| 91 | +Gate re-run of the charter siblings is inside that suite run (32x32_level_1to4, superblock_level_1to4, | ||
| 92 | +split_transform_*, coded_rect_strip_below_16x16, sub8_*, filter_intra_*, multi_tile/two+four tile | ||
| 93 | +columns and tile rows, tiny sweep, refusal_inventory, gate_coverage — all ok). | ||