From 973f04aef64e524dba8421c06fe058bd542e70a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 4 Oct 2026 13:48:49 +0300 Subject: [PATCH] av1: the 128-root rect chroma gather was not taking the 4:2:2 pair rule decode_block_128rect's per-chunk chroma unit walk gathered its coefficient context with the square-cut around_mi_rect, so at ss (1, 0) its above dc vote summed 16 luma mi cells where libaom's get_txb_ctx_general reads txb_w_unit = chroma_tx / 4 = 8 chroma cells (one chroma column per pair of luma mi columns -- both luma cells of a column carry the covering unit's whole-unit dc sign). The gather now routes through around_mi_422_chroma under the ss guard, verbatim around_mi_rect at every other shape, the same rule the mu-chunk walks take. Reachability measured: the walk fires at ss (1, 0) on both pinned 422_intrabc_sb128_strip*.obu cells, 72 chroma unit gathers each (sb128rect_chroma_replay_hits().0). No committed cell discriminates the change -- all 21 committed 4:2:2 cells are byte-exact against ffmpeg with the pair rule, exactly as the pre-change tree was -- so the_422_block_128rect_chroma_chunk_gather_stays_per_axis pins the routing by source scan and carries a 72-unit reachability arm. Both mutations (guard removed; guard kept with the square gather) red it. lanes/av1muchunk422sym.report.md's other two rows are closed as unreachable at ss (1, 0): decode_intra_sub8_leaf and decode_inter_sub8_rect2 gather those chroma contexts only inside their if chroma_444 arms (ss 0/0), 0 probe hits over the whole 4:2:2 corpus. Also corrects decode_block_128rect's stale prose that called 4:2:2 refused by name (it is admitted; SB128RECT_REPLAY_SPAN_MISMATCH_HITS fires there on byte-exact cells). --- crates/ec-av1/src/decode.rs | 42 ++++++++--- crates/ec-av1/src/stream.rs | 107 ++++++++++++++++++++++++++ lanes/av1422gather3.report.md | 138 ++++++++++++++++++++++++++++++++++ 3 files changed, 277 insertions(+), 10 deletions(-) create mode 100644 lanes/av1422gather3.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b790da3a..104ae632 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -24931,7 +24931,23 @@ fn decode_block_128rect( ); continue; } - let cu_around = neighbours.around_mi_rect(cu_mi, luma_span, luma_span_h); + // lane-av1422gather3: libaom's `get_txb_ctx_general` + // reads the unit's above votes over `txb_w_unit` = + // `chroma_tx / 4` CHROMA cells, and at ss_x 1 one + // chroma cell spans TWO luma mi columns -- the same + // pair rule the mu-chunk walks take + // (`around_mi_422_chroma`). Reachability measured on + // the pinned `422_intrabc_sb128_strip*.obu` (72 unit + // gathers each, `sb128rect_chroma_replay_hits().0`); + // output-inert on all 21 committed 4:2:2 cells, so + // the source-scan gate + // `the_422_block_128rect_chroma_chunk_gather_stays_per_axis` + // is what stops a revert. + let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { + neighbours.around_mi_422_chroma(cu_mi, luma_span, luma_span_h) + } else { + neighbours.around_mi_rect(cu_mi, luma_span, luma_span_h) + }; let buf: &mut PlaneBuf<'static> = if plane_pass == 1 { &mut *u } else { &mut *v }; let grid = read_plane( @@ -25008,15 +25024,13 @@ fn decode_block_128rect( // rows below the block on the last unit of a 128x64/64x128 strip and // smearing a neighbour chunk's unit over its neighbour's rows. // - // DEFENSIVE at the decoder's current capability set: both admitted - // chroma formats have `ss_x == ss_y` (4:2:0 ss (1,1), 4:4:4 ss - // (0,0)), so the two spans are equal and this line is a no-op on - // every stream `decode_stream` accepts; 4:2:2 (ss (1,0)) -- the one - // format where they differ -- is refused by name at the sequence - // header, so the arm is unreachable there. It stays per-axis because - // the walk it replays is per-axis, and a 4:2:2 port must not have to - // re-derive this. `SB128RECT_REPLAY_SPAN_MISMATCH_HITS` names the - // day that changes. + // 4:2:2 (ss (1,0)) is the one format where the two spans differ, and + // it is ADMITTED since lane-av1422lift: this branch does fire there + // (`SB128RECT_REPLAY_SPAN_MISMATCH_HITS` reads 9 on each of the two + // pinned `422_intrabc_sb128_strip*.obu` cells, whose five frames are + // byte-exact against ffmpeg) -- that is the replay's own designed + // 4:2:2 case, not a defect signature. Nothing here asserts the + // mismatch is zero. if luma_span != luma_span_h { hit!(SB128RECT_REPLAY_SPAN_MISMATCH_HITS); } @@ -49558,6 +49572,10 @@ fn decode_intra_sub8_leaf( } else { // The leaf's own TX_8X4/TX_4X8 chroma unit: it IS the chroma plane // block, so `get_txb_ctx` fixes the skip context at above+left. + // lane-av1422gather3: 4:4:4-only -- this `ca` arm sits inside the + // `chroma_444` guard (ss 0/0), so the 4:2:2 pair rule does not + // apply here. Probed at ss (1,0) over all 21 committed 4:2:2 + // cells: 0 hits. let ca = neighbours.around_mi_rect(lmi, bw, bh); let scan_c: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 }; let uv_tx = default_intra_tx_type(uv_predict_mode as u8); @@ -50406,6 +50424,10 @@ fn decode_inter_sub8_rect2( fctx, )?; } else { + // lane-av1422gather3: 4:4:4-only -- this `ca` arm sits inside + // the `chroma_444` guard (ss 0/0); the 4:2:2 per-piece arm + // below already reads through `around_mi_422_chroma`. Probed + // at ss (1,0) over all 21 committed 4:2:2 cells: 0 hits. let ca = neighbours.around_mi_rect(lmi, bw, bh); let (ug, _) = read_inter_plane_rect( dec, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 6e1a9775..a2b78d82 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3676,6 +3676,113 @@ pub(crate) mod tests { ); } } + + /// lane-av1422gather3: `decode_block_128rect`'s per-chunk CHROMA unit + /// walk gathered its coefficient context with the plain square-cut + /// `around_mi_rect`, so at ss (1,0) its above dc vote summed 16 LUMA mi + /// cells where libaom's `get_txb_ctx_general` reads `txb_w_unit` = + /// `chroma_tx / 4` = 8 CHROMA cells (one chroma column per PAIR of luma + /// mi columns; both luma cells of a column carry the covering unit's + /// whole-unit dc sign). That is the same pair rule the mu-chunk walks + /// take (`around_mi_422_chroma`), and the gather now routes through it + /// under the ss guard -- verbatim `around_mi_rect` at every other shape. + /// + /// Reachability is MEASURED, not assumed: the walk fires at ss (1,0) on + /// both pinned `422_intrabc_sb128_strip*.obu` cells, 72 chroma unit + /// gathers each (`sb128rect_chroma_replay_hits().0`, the same walk's own + /// replay counter). No committed cell DISCRIMINATES the change: with the + /// pair rule in place all 21 committed 4:2:2 cells are byte-exact + /// against ffmpeg, exactly as the pre-change tree was (the gathered + /// votes cancel at every reached unit), so THIS gate -- not a pixel + /// compare -- is what stops the routing being reverted. + #[test] + fn the_422_block_128rect_chroma_chunk_gather_stays_per_axis() { + const NAME: &str = "the_422_block_128rect_chroma_chunk_gather_stays_per_axis"; + let src = include_str!("decode.rs"); + let body = src + .split_once("fn decode_block_128rect(") + .expect("decode_block_128rect must exist") + .1 + .split_once("\nfn ") + .expect("decode_block_128rect must be a top-level fn") + .0; + // Scan CODE only: the fix's own comment quotes the guard form, and a + // prose mention must not read as a reintroduction (nor mask one). + let stripped: Vec<&str> = body + .lines() + .map(|l| l.split_once("//").map_or(l, |(c, _)| c)) + .collect(); + let code = stripped.join("\n"); + let body = code.as_str(); + assert_eq!( + body.matches("if ss_x(fctx) == 1 && ss_y(fctx) == 0 {") + .count(), + 1, + "{NAME}: the ss (1, 0) guard over the per-chunk chroma gather must \ + appear exactly once in decode_block_128rect, found {}", + body.matches("if ss_x(fctx) == 1 && ss_y(fctx) == 0 {") + .count() + ); + for name in [ + "neighbours.around_mi_422_chroma(cu_mi, luma_span, luma_span_h)", + "neighbours.around_mi_rect(cu_mi, luma_span, luma_span_h)", + ] { + assert_eq!( + body.matches(name).count(), + 1, + "{NAME}: `{name}` must appear exactly once in \ + decode_block_128rect's per-chunk chroma gather, found {} -- one \ + arm has been dropped, so the gather has fallen back to the \ + square per-mi rule at ss (1, 0)", + body.matches(name).count() + ); + } + // Reachability arm: the walk must actually RUN at ss (1, 0) on the + // pinned strips, or the routing above is a no-op the gate cannot see. + // A stream that stopped coding 128-axis intra blocks reads 0 and this + // assert fails instead of passing over a decode that never reached + // the fixed gather (class gate-blind-to-feature). + for (file, bytes, fp) in [ + ( + "422_intrabc_sb128_strip.obu", + 1672usize, + 0x50f5cfc576e4cd00_u64, + ), + ( + "422_intrabc_sb128_strip_notxsearch.obu", + 1675, + 0xd4936f252ff8cff0, + ), + ] { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("fixtures") + .join(file); + let data = std::fs::read(&path).unwrap_or_else(|e| { + panic!( + "{NAME}: pinned witness {} is missing ({e}) -- the gate cannot run", + path.display() + ) + }); + assert_eq!(data.len(), bytes, "{NAME}: {file} size drifted"); + assert_eq!(fnv1a64(&data), fp, "{NAME}: {file} bytes drifted"); + let _guard = lock_gate_counters(); + let before = crate::decode::sb128rect_chroma_replay_hits().0; + let frames = decode_stream(&data) + .unwrap_or_else(|e| panic!("{NAME}: {file} no longer decodes cleanly: {e}")); + let after = crate::decode::sb128rect_chroma_replay_hits().0; + assert_eq!( + after - before, + 72, + "{NAME}: {file} gathered {} chroma unit context(s) in \ + decode_block_128rect's per-chunk walk; 72 were measured at ss (1, 0) -- \ + a lower number means the gather is no longer REACHED here \ + (class gate-blind-to-feature)", + after - before + ); + assert_eq!(frames.len(), 5, "{NAME}: {file} frame count"); + } + } + /// lane-av1422lpf: the three 4:2:2 LOSSLESS INTER witness fixtures, /// PINNED, and asserted to refuse at the SEQUENCE HEADER. /// diff --git a/lanes/av1422gather3.report.md b/lanes/av1422gather3.report.md new file mode 100644 index 00000000..af55f4b8 --- /dev/null +++ b/lanes/av1422gather3.report.md @@ -0,0 +1,138 @@ +# lane-av1422gather3 — the three chroma gathers `av1muchunk422sym` left outside the pair rule + +Base `main` @ `90778367`. Branch `lane/av1422gather3`. + +`lanes/av1muchunk422sym.report.md` §6 enumerated three `around_mi_rect` +call sites that "gather a CHROMA plane's coefficient context, feed +`dc_sign_ctx`, lack the pair rule, and belong to a DIFFERENT walk". Each was +to be measured on its own terms. Verdicts, per site: + +| site (base line) | function | calls `around_mi_422_chroma` at ss (1,0)? | reached at ss (1,0)? | verdict | +|---|---|---|---|---| +| ~24924 → **24934** | `decode_block_128rect` | **no** (square `around_mi_rect`) | **YES — 72 gathers** on each pinned `422_intrabc_sb128_strip*.obu` | **ROUTED + GATED** | +| ~49552 → **49562** | `decode_intra_sub8_leaf` | no (inside `chroma_444`) | **NO — 0 hits** over the whole 4:2:2 corpus | **CLOSED (unreachable)** | +| ~50400 → **50410** | `decode_inter_sub8_rect2` | no (inside `chroma_444`) | **NO — 0 hits** over the whole 4:2:2 corpus | **CLOSED (unreachable)** | + +The first is a real 4:2:2 instance of the class. The other two are stale rows: +the mechanical enumeration keyed on "`around_mi_rect` feeding a chroma context" +and did not notice that both gathers sit inside an `if chroma_444 { … }` arm +(`chroma_444 = ss_x == 0 && ss_y == 0`, `decode.rs` 49359 / 49796), i.e. they +only run at 4:4:4, where `ss_x == ss_y == 0` and the pair rule does not apply. +`decode_inter_sub8_rect2`'s actual 4:2:2 arm already reads +`around_mi_422_chroma(ctx_mi, 8, 4)` (`decode.rs` ~50538). + +## 1. Site 1 — `decode_block_128rect` (the intra 128-root rect chroma walk) + +The per-chunk chroma-unit walk gathers its coefficient context from +`cu_mi` over the unit's LUMA span (`luma_span = chroma_tx << ss_x = 64`, +`luma_span_h = chroma_tx << ss_y = 32`) and feeds `cu_around[plane_pass]` to +`read_plane` → `dc_sign_ctx(around.2)`. Same rule as the mu-chunk walks: at +ss (1,0) libaom's `get_txb_ctx_general` reads the above votes over +`txb_w_unit = chroma_tx / 4 = 8` CHROMA cells, and one chroma cell spans TWO +luma mi columns, so the square per-mi sum counts each above column twice. +The left extent is unsubsquared at ss_y 0 and already lines up one-to-one. + +**Reachability, measured not argued.** A temporary env-free probe at the +gather (printing when `ss_x == 1 && ss_y == 0`) fired **72 times on each of** +`422_intrabc_sb128_strip.obu` and `422_intrabc_sb128_strip_notxsearch.obu` +and **0 times on the other 19 committed 4:2:2 cells** (all `422_*` / `s422_*`). +The 72 is the same walk's own replay counter +(`sb128rect_chroma_replay_hits().0`, whose doc records `replay = 72, +mismatch = 9` on exactly those two cells) — two independent instruments, same +number. + +**No committed cell discriminates the change.** With the pair rule in place +every committed 4:2:2 cell was decoded and compared plane-by-plane against +ffmpeg rawvideo: + +```text +422_allskip_2f EXACT (2 frames) 422_sb128_3f EXACT (3) +422_inter_160x128_3f EXACT (3) s422_12bit_160x128 EXACT (3) +422_intrabc_sb128_strip EXACT (5) s422_320x246/322x240/322x246 EXACT (16) +422_intrabc_sb128_strip_notxsearch EXACT (5) s422_352x242_10b EXACT (16) +422_key_64x64 EXACT (1) s422_416x242_10b/416x250_10b EXACT (16) +422_muchunk_compound_128root EXACT (6) s422_grain_160x128 EXACT (3) +422_muchunk_intra_in_inter_128root EXACT (6) s422_superres_160x128 EXACT (3) +422_palette_intra_in_inter_384x240_17f EXACT (16) +422_residual_compound_warp_16f EXACT (16) +422_residual_compound_warp_nolr_16f EXACT (16) +(422_header_edge16_walk refuses by design — not a decode cell) +``` + +The pre-change tree was equally exact (every one of these is a pinned +byte-exact gate on `main`, and the strip fixture was re-decoded pre-change +here: f0..f4 all EXACT). So the gathered votes cancel at every reached unit: +the routing is **correct but output-inert on the committed corpus**, exactly +the disposition the same file already gives the IntraBC twin +(`the_intrabc_128rect_chroma_chunk_walk_stays_per_axis`, "that fixture's own +votes do not cancel … no committed cell discriminates"). + +Because no pixel gate can see it, the change is pinned by a **source-scan +routing gate**, `the_422_block_128rect_chroma_chunk_gather_stays_per_axis`, +which additionally carries a **reachability arm** (decodes both pinned strips +and asserts exactly 72 gathered units, so the routing can never be a no-op the +scan cannot see — class `gate-blind-to-feature`). + +### Red-before (mutation), measured + +Two mutations, each built and run: + +1. gather reverted to the bare square call (guard removed) → + `the ss (1, 0) guard over the per-chunk chroma gather must appear exactly + once in decode_block_128rect, found 0` — RED. +2. guard kept, pair rule replaced by the square gather in the `if` arm → + `neighbours.around_mi_422_chroma(cu_mi, luma_span, luma_span_h) must appear + exactly once … found 0 -- one arm has been dropped, so the gather has + fallen back to the square per-mi rule at ss (1, 0)` — RED. + +Reverted; green again. + +## 2/3. Sites 2 & 3 — the `chroma_444`-scoped gathers (closed) + +- `decode_intra_sub8_leaf` (49582): the `let ca = around_mi_rect(lmi, bw, bh)` + that feeds `read_chroma` sits in the `else` of `if lossless(fctx)` inside + `else if chroma_444 && bw != bh`. `chroma_444` is `ss_x==0 && ss_y==0` + (`decode.rs` 49359). +- `decode_inter_sub8_rect2` (50434): the `let ca = around_mi_rect(lmi, bw, bh)` + that feeds `read_inter_plane_rect` for U/V sits in the `else` of + `if lossless(fctx)` inside the `if chroma_444` block (`decode.rs` 49796). + +Both were probed at ss (1,0) across the entire committed 4:2:2 corpus: +**0 hits each** (matching the static guard). They are 4:4:4-only; at 4:4:4 the +pair rule is inapplicable (`ss_x == ss_y == 0`, one luma mi per chroma cell). +`decode_inter_sub8_rect2`'s 4:2:2 per-piece arm already reads through +`around_mi_422_chroma`. Both comments now record the guard so a future +mechanical enumeration does not re-list them. + +## 4. Also corrected (same function, stale prose) + +`decode_block_128rect`'s `SB128RECT_REPLAY_SPAN_MISMATCH_HITS` comment still +read "4:2:2 … is refused by name at the sequence header, so the arm is +unreachable there". 4:2:2 is admitted (`lane-av1422lift`) and the arm fires +(mismatch 9 on the two strip cells, byte-exact). The comment now says so and +states that nothing asserts the mismatch is zero. + +## 5. Evidence summary + +- Site 1 fix: `decode.rs` 24946–24950 (guard + `around_mi_422_chroma`). +- Site 2/3: comments only (`decode.rs` 49578, 50430) — no code change, closed + as unreachable. +- Gate: `stream.rs::the_422_block_128rect_chroma_chunk_gather_stays_per_axis` + (source scan + 72-unit reachability arm), mutation-proven red twice. +- Pinned fixtures: `422_intrabc_sb128_strip.obu` (1672 B, sha256 prefix + `f92000db86df7acc`), `422_intrabc_sb128_strip_notxsearch.obu` (1675 B, + `80dd0d4e93fd6c3d`). + +## 6. Repro + +```text +git worktree add -b lane/av1422gather3 ~/.cache/wt/av1422gather3 90778367 +cd ~/.cache/wt/av1422gather3 +CARGO_TARGET_DIR=~/.cache/tgt-av1422gather3 cargo build -p ec-av1 --example dump_yuv +B=~/.cache/tgt-av1422gather3/debug/examples +$B/dump_yuv crates/ec-av1/fixtures/422_intrabc_sb128_strip.obu /tmp/ours # 384x320 yuv422p +ffmpeg -v error -i crates/ec-av1/fixtures/422_intrabc_sb128_strip.obu -pix_fmt yuv422p \ + -f rawvideo /tmp/ref.yuv +TMPDIR=/dev/shm CARGO_TARGET_DIR=~/.cache/tgt-av1422gather3 \ + cargo test -p ec-av1 --lib -- the_422_block_128rect_chroma_chunk_gather_stays_per_axis +```