forge
Explore
Log in

Don't you dare go Hollow

Merge lane/av1422gather3: the 128-root rect chroma gather takes the 4:2:2 pair rule

Browse files at this commit

cb271bc1c06280ab932580a51c25c4d6d172cde3Ahmet Kaan GümüşParent: a117ce2973f04a
decode_block_128rect was the one of the three named gathers that a 4:2:2
stream can enter. It fired 72 times on each pinned sb128 strip and still
used around_mi_rect, which counts each chroma column twice. It now uses
around_mi_422_chroma at ss (1,0). Output is unchanged on all 21 committed
4:2:2 cells, so the gate is a source scan plus the 72-unit reach counter.
Dropping the guard reds it: the ss (1,0) guard must appear once, found 0.

The other two named sites sit inside chroma_444 guards (ss 0,0). Zero hits
on all 21 committed 4:2:2 cells. Not routed. Comments only.
crates/ec-av1/src/decode.rsmodified+32 -10
@@ -24931,7 +24931,23 @@ fn decode_block_128rect(
24931 );24931 );
24932 continue;24932 continue;
24933 }24933 }
24934- let cu_around = neighbours.around_mi_rect(cu_mi, luma_span, luma_span_h);24934+ // lane-av1422gather3: libaom's `get_txb_ctx_general`
24935+ // reads the unit's above votes over `txb_w_unit` =
24936+ // `chroma_tx / 4` CHROMA cells, and at ss_x 1 one
24937+ // chroma cell spans TWO luma mi columns -- the same
24938+ // pair rule the mu-chunk walks take
24939+ // (`around_mi_422_chroma`). Reachability measured on
24940+ // the pinned `422_intrabc_sb128_strip*.obu` (72 unit
24941+ // gathers each, `sb128rect_chroma_replay_hits().0`);
24942+ // output-inert on all 21 committed 4:2:2 cells, so
24943+ // the source-scan gate
24944+ // `the_422_block_128rect_chroma_chunk_gather_stays_per_axis`
24945+ // is what stops a revert.
24946+ let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
24947+ neighbours.around_mi_422_chroma(cu_mi, luma_span, luma_span_h)
24948+ } else {
24949+ neighbours.around_mi_rect(cu_mi, luma_span, luma_span_h)
24950+ };
24935 let buf: &mut PlaneBuf<'static> =24951 let buf: &mut PlaneBuf<'static> =
24936 if plane_pass == 1 { &mut *u } else { &mut *v };24952 if plane_pass == 1 { &mut *u } else { &mut *v };
24937 let grid = read_plane(24953 let grid = read_plane(
@@ -25008,15 +25024,13 @@ fn decode_block_128rect(
25008 // rows below the block on the last unit of a 128x64/64x128 strip and25024 // rows below the block on the last unit of a 128x64/64x128 strip and
25009 // smearing a neighbour chunk's unit over its neighbour's rows.25025 // smearing a neighbour chunk's unit over its neighbour's rows.
25010 //25026 //
25011- // DEFENSIVE at the decoder's current capability set: both admitted25027+ // 4:2:2 (ss (1,0)) is the one format where the two spans differ, and
25012- // chroma formats have `ss_x == ss_y` (4:2:0 ss (1,1), 4:4:4 ss25028+ // it is ADMITTED since lane-av1422lift: this branch does fire there
25013- // (0,0)), so the two spans are equal and this line is a no-op on25029+ // (`SB128RECT_REPLAY_SPAN_MISMATCH_HITS` reads 9 on each of the two
25014- // every stream `decode_stream` accepts; 4:2:2 (ss (1,0)) -- the one25030+ // pinned `422_intrabc_sb128_strip*.obu` cells, whose five frames are
25015- // format where they differ -- is refused by name at the sequence25031+ // byte-exact against ffmpeg) -- that is the replay's own designed
25016- // header, so the arm is unreachable there. It stays per-axis because25032+ // 4:2:2 case, not a defect signature. Nothing here asserts the
25017- // the walk it replays is per-axis, and a 4:2:2 port must not have to25033+ // mismatch is zero.
25018- // re-derive this. `SB128RECT_REPLAY_SPAN_MISMATCH_HITS` names the
25019- // day that changes.
25020 if luma_span != luma_span_h {25034 if luma_span != luma_span_h {
25021 hit!(SB128RECT_REPLAY_SPAN_MISMATCH_HITS);25035 hit!(SB128RECT_REPLAY_SPAN_MISMATCH_HITS);
25022 }25036 }
@@ -49558,6 +49572,10 @@ fn decode_intra_sub8_leaf(
49558 } else {49572 } else {
49559 // The leaf's own TX_8X4/TX_4X8 chroma unit: it IS the chroma plane49573 // The leaf's own TX_8X4/TX_4X8 chroma unit: it IS the chroma plane
49560 // block, so `get_txb_ctx` fixes the skip context at above+left.49574 // block, so `get_txb_ctx` fixes the skip context at above+left.
49575+ // lane-av1422gather3: 4:4:4-only -- this `ca` arm sits inside the
49576+ // `chroma_444` guard (ss 0/0), so the 4:2:2 pair rule does not
49577+ // apply here. Probed at ss (1,0) over all 21 committed 4:2:2
49578+ // cells: 0 hits.
49561 let ca = neighbours.around_mi_rect(lmi, bw, bh);49579 let ca = neighbours.around_mi_rect(lmi, bw, bh);
49562 let scan_c: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 };49580 let scan_c: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 };
49563 let uv_tx = default_intra_tx_type(uv_predict_mode as u8);49581 let uv_tx = default_intra_tx_type(uv_predict_mode as u8);
@@ -50406,6 +50424,10 @@ fn decode_inter_sub8_rect2(
50406 fctx,50424 fctx,
50407 )?;50425 )?;
50408 } else {50426 } else {
50427+ // lane-av1422gather3: 4:4:4-only -- this `ca` arm sits inside
50428+ // the `chroma_444` guard (ss 0/0); the 4:2:2 per-piece arm
50429+ // below already reads through `around_mi_422_chroma`. Probed
50430+ // at ss (1,0) over all 21 committed 4:2:2 cells: 0 hits.
50409 let ca = neighbours.around_mi_rect(lmi, bw, bh);50431 let ca = neighbours.around_mi_rect(lmi, bw, bh);
50410 let (ug, _) = read_inter_plane_rect(50432 let (ug, _) = read_inter_plane_rect(
50411 dec,50433 dec,
crates/ec-av1/src/stream.rsmodified+107 -0
@@ -3676,6 +3676,113 @@ pub(crate) mod tests {
3676 );3676 );
3677 }3677 }
3678 }3678 }
3679+
3680+ /// lane-av1422gather3: `decode_block_128rect`'s per-chunk CHROMA unit
3681+ /// walk gathered its coefficient context with the plain square-cut
3682+ /// `around_mi_rect`, so at ss (1,0) its above dc vote summed 16 LUMA mi
3683+ /// cells where libaom's `get_txb_ctx_general` reads `txb_w_unit` =
3684+ /// `chroma_tx / 4` = 8 CHROMA cells (one chroma column per PAIR of luma
3685+ /// mi columns; both luma cells of a column carry the covering unit's
3686+ /// whole-unit dc sign). That is the same pair rule the mu-chunk walks
3687+ /// take (`around_mi_422_chroma`), and the gather now routes through it
3688+ /// under the ss guard -- verbatim `around_mi_rect` at every other shape.
3689+ ///
3690+ /// Reachability is MEASURED, not assumed: the walk fires at ss (1,0) on
3691+ /// both pinned `422_intrabc_sb128_strip*.obu` cells, 72 chroma unit
3692+ /// gathers each (`sb128rect_chroma_replay_hits().0`, the same walk's own
3693+ /// replay counter). No committed cell DISCRIMINATES the change: with the
3694+ /// pair rule in place all 21 committed 4:2:2 cells are byte-exact
3695+ /// against ffmpeg, exactly as the pre-change tree was (the gathered
3696+ /// votes cancel at every reached unit), so THIS gate -- not a pixel
3697+ /// compare -- is what stops the routing being reverted.
3698+ #[test]
3699+ fn the_422_block_128rect_chroma_chunk_gather_stays_per_axis() {
3700+ const NAME: &str = "the_422_block_128rect_chroma_chunk_gather_stays_per_axis";
3701+ let src = include_str!("decode.rs");
3702+ let body = src
3703+ .split_once("fn decode_block_128rect(")
3704+ .expect("decode_block_128rect must exist")
3705+ .1
3706+ .split_once("\nfn ")
3707+ .expect("decode_block_128rect must be a top-level fn")
3708+ .0;
3709+ // Scan CODE only: the fix's own comment quotes the guard form, and a
3710+ // prose mention must not read as a reintroduction (nor mask one).
3711+ let stripped: Vec<&str> = body
3712+ .lines()
3713+ .map(|l| l.split_once("//").map_or(l, |(c, _)| c))
3714+ .collect();
3715+ let code = stripped.join("\n");
3716+ let body = code.as_str();
3717+ assert_eq!(
3718+ body.matches("if ss_x(fctx) == 1 && ss_y(fctx) == 0 {")
3719+ .count(),
3720+ 1,
3721+ "{NAME}: the ss (1, 0) guard over the per-chunk chroma gather must \
3722+ appear exactly once in decode_block_128rect, found {}",
3723+ body.matches("if ss_x(fctx) == 1 && ss_y(fctx) == 0 {")
3724+ .count()
3725+ );
3726+ for name in [
3727+ "neighbours.around_mi_422_chroma(cu_mi, luma_span, luma_span_h)",
3728+ "neighbours.around_mi_rect(cu_mi, luma_span, luma_span_h)",
3729+ ] {
3730+ assert_eq!(
3731+ body.matches(name).count(),
3732+ 1,
3733+ "{NAME}: `{name}` must appear exactly once in \
3734+ decode_block_128rect's per-chunk chroma gather, found {} -- one \
3735+ arm has been dropped, so the gather has fallen back to the \
3736+ square per-mi rule at ss (1, 0)",
3737+ body.matches(name).count()
3738+ );
3739+ }
3740+ // Reachability arm: the walk must actually RUN at ss (1, 0) on the
3741+ // pinned strips, or the routing above is a no-op the gate cannot see.
3742+ // A stream that stopped coding 128-axis intra blocks reads 0 and this
3743+ // assert fails instead of passing over a decode that never reached
3744+ // the fixed gather (class gate-blind-to-feature).
3745+ for (file, bytes, fp) in [
3746+ (
3747+ "422_intrabc_sb128_strip.obu",
3748+ 1672usize,
3749+ 0x50f5cfc576e4cd00_u64,
3750+ ),
3751+ (
3752+ "422_intrabc_sb128_strip_notxsearch.obu",
3753+ 1675,
3754+ 0xd4936f252ff8cff0,
3755+ ),
3756+ ] {
3757+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
3758+ .join("fixtures")
3759+ .join(file);
3760+ let data = std::fs::read(&path).unwrap_or_else(|e| {
3761+ panic!(
3762+ "{NAME}: pinned witness {} is missing ({e}) -- the gate cannot run",
3763+ path.display()
3764+ )
3765+ });
3766+ assert_eq!(data.len(), bytes, "{NAME}: {file} size drifted");
3767+ assert_eq!(fnv1a64(&data), fp, "{NAME}: {file} bytes drifted");
3768+ let _guard = lock_gate_counters();
3769+ let before = crate::decode::sb128rect_chroma_replay_hits().0;
3770+ let frames = decode_stream(&data)
3771+ .unwrap_or_else(|e| panic!("{NAME}: {file} no longer decodes cleanly: {e}"));
3772+ let after = crate::decode::sb128rect_chroma_replay_hits().0;
3773+ assert_eq!(
3774+ after - before,
3775+ 72,
3776+ "{NAME}: {file} gathered {} chroma unit context(s) in \
3777+ decode_block_128rect's per-chunk walk; 72 were measured at ss (1, 0) -- \
3778+ a lower number means the gather is no longer REACHED here \
3779+ (class gate-blind-to-feature)",
3780+ after - before
3781+ );
3782+ assert_eq!(frames.len(), 5, "{NAME}: {file} frame count");
3783+ }
3784+ }
3785+
3679 /// lane-av1422lpf: the three 4:2:2 LOSSLESS INTER witness fixtures,3786 /// lane-av1422lpf: the three 4:2:2 LOSSLESS INTER witness fixtures,
3680 /// PINNED, and asserted to refuse at the SEQUENCE HEADER.3787 /// PINNED, and asserted to refuse at the SEQUENCE HEADER.
3681 ///3788 ///
lanes/av1422gather3.report.mdadded+138 -0
@@ -0,0 +1,138 @@
1+# lane-av1422gather3 — the three chroma gathers `av1muchunk422sym` left outside the pair rule
2+
3+Base `main` @ `90778367`. Branch `lane/av1422gather3`.
4+
5+`lanes/av1muchunk422sym.report.md` §6 enumerated three `around_mi_rect`
6+call sites that "gather a CHROMA plane's coefficient context, feed
7+`dc_sign_ctx`, lack the pair rule, and belong to a DIFFERENT walk". Each was
8+to be measured on its own terms. Verdicts, per site:
9+
10+| site (base line) | function | calls `around_mi_422_chroma` at ss (1,0)? | reached at ss (1,0)? | verdict |
11+|---|---|---|---|---|
12+| ~24924 → **24934** | `decode_block_128rect` | **no** (square `around_mi_rect`) | **YES — 72 gathers** on each pinned `422_intrabc_sb128_strip*.obu` | **ROUTED + GATED** |
13+| ~49552 → **49562** | `decode_intra_sub8_leaf` | no (inside `chroma_444`) | **NO — 0 hits** over the whole 4:2:2 corpus | **CLOSED (unreachable)** |
14+| ~50400 → **50410** | `decode_inter_sub8_rect2` | no (inside `chroma_444`) | **NO — 0 hits** over the whole 4:2:2 corpus | **CLOSED (unreachable)** |
15+
16+The first is a real 4:2:2 instance of the class. The other two are stale rows:
17+the mechanical enumeration keyed on "`around_mi_rect` feeding a chroma context"
18+and did not notice that both gathers sit inside an `if chroma_444 { … }` arm
19+(`chroma_444 = ss_x == 0 && ss_y == 0`, `decode.rs` 49359 / 49796), i.e. they
20+only run at 4:4:4, where `ss_x == ss_y == 0` and the pair rule does not apply.
21+`decode_inter_sub8_rect2`'s actual 4:2:2 arm already reads
22+`around_mi_422_chroma(ctx_mi, 8, 4)` (`decode.rs` ~50538).
23+
24+## 1. Site 1 — `decode_block_128rect` (the intra 128-root rect chroma walk)
25+
26+The per-chunk chroma-unit walk gathers its coefficient context from
27+`cu_mi` over the unit's LUMA span (`luma_span = chroma_tx << ss_x = 64`,
28+`luma_span_h = chroma_tx << ss_y = 32`) and feeds `cu_around[plane_pass]` to
29+`read_plane` → `dc_sign_ctx(around.2)`. Same rule as the mu-chunk walks: at
30+ss (1,0) libaom's `get_txb_ctx_general` reads the above votes over
31+`txb_w_unit = chroma_tx / 4 = 8` CHROMA cells, and one chroma cell spans TWO
32+luma mi columns, so the square per-mi sum counts each above column twice.
33+The left extent is unsubsquared at ss_y 0 and already lines up one-to-one.
34+
35+**Reachability, measured not argued.** A temporary env-free probe at the
36+gather (printing when `ss_x == 1 && ss_y == 0`) fired **72 times on each of**
37+`422_intrabc_sb128_strip.obu` and `422_intrabc_sb128_strip_notxsearch.obu`
38+and **0 times on the other 19 committed 4:2:2 cells** (all `422_*` / `s422_*`).
39+The 72 is the same walk's own replay counter
40+(`sb128rect_chroma_replay_hits().0`, whose doc records `replay = 72,
41+mismatch = 9` on exactly those two cells) — two independent instruments, same
42+number.
43+
44+**No committed cell discriminates the change.** With the pair rule in place
45+every committed 4:2:2 cell was decoded and compared plane-by-plane against
46+ffmpeg rawvideo:
47+
48+```text
49+422_allskip_2f EXACT (2 frames) 422_sb128_3f EXACT (3)
50+422_inter_160x128_3f EXACT (3) s422_12bit_160x128 EXACT (3)
51+422_intrabc_sb128_strip EXACT (5) s422_320x246/322x240/322x246 EXACT (16)
52+422_intrabc_sb128_strip_notxsearch EXACT (5) s422_352x242_10b EXACT (16)
53+422_key_64x64 EXACT (1) s422_416x242_10b/416x250_10b EXACT (16)
54+422_muchunk_compound_128root EXACT (6) s422_grain_160x128 EXACT (3)
55+422_muchunk_intra_in_inter_128root EXACT (6) s422_superres_160x128 EXACT (3)
56+422_palette_intra_in_inter_384x240_17f EXACT (16)
57+422_residual_compound_warp_16f EXACT (16)
58+422_residual_compound_warp_nolr_16f EXACT (16)
59+(422_header_edge16_walk refuses by design — not a decode cell)
60+```
61+
62+The pre-change tree was equally exact (every one of these is a pinned
63+byte-exact gate on `main`, and the strip fixture was re-decoded pre-change
64+here: f0..f4 all EXACT). So the gathered votes cancel at every reached unit:
65+the routing is **correct but output-inert on the committed corpus**, exactly
66+the disposition the same file already gives the IntraBC twin
67+(`the_intrabc_128rect_chroma_chunk_walk_stays_per_axis`, "that fixture's own
68+votes do not cancel … no committed cell discriminates").
69+
70+Because no pixel gate can see it, the change is pinned by a **source-scan
71+routing gate**, `the_422_block_128rect_chroma_chunk_gather_stays_per_axis`,
72+which additionally carries a **reachability arm** (decodes both pinned strips
73+and asserts exactly 72 gathered units, so the routing can never be a no-op the
74+scan cannot see — class `gate-blind-to-feature`).
75+
76+### Red-before (mutation), measured
77+
78+Two mutations, each built and run:
79+
80+1. gather reverted to the bare square call (guard removed) →
81+ `the ss (1, 0) guard over the per-chunk chroma gather must appear exactly
82+ once in decode_block_128rect, found 0` — RED.
83+2. guard kept, pair rule replaced by the square gather in the `if` arm →
84+ `neighbours.around_mi_422_chroma(cu_mi, luma_span, luma_span_h) must appear
85+ exactly once … found 0 -- one arm has been dropped, so the gather has
86+ fallen back to the square per-mi rule at ss (1, 0)` — RED.
87+
88+Reverted; green again.
89+
90+## 2/3. Sites 2 & 3 — the `chroma_444`-scoped gathers (closed)
91+
92+- `decode_intra_sub8_leaf` (49582): the `let ca = around_mi_rect(lmi, bw, bh)`
93+ that feeds `read_chroma` sits in the `else` of `if lossless(fctx)` inside
94+ `else if chroma_444 && bw != bh`. `chroma_444` is `ss_x==0 && ss_y==0`
95+ (`decode.rs` 49359).
96+- `decode_inter_sub8_rect2` (50434): the `let ca = around_mi_rect(lmi, bw, bh)`
97+ that feeds `read_inter_plane_rect` for U/V sits in the `else` of
98+ `if lossless(fctx)` inside the `if chroma_444` block (`decode.rs` 49796).
99+
100+Both were probed at ss (1,0) across the entire committed 4:2:2 corpus:
101+**0 hits each** (matching the static guard). They are 4:4:4-only; at 4:4:4 the
102+pair rule is inapplicable (`ss_x == ss_y == 0`, one luma mi per chroma cell).
103+`decode_inter_sub8_rect2`'s 4:2:2 per-piece arm already reads through
104+`around_mi_422_chroma`. Both comments now record the guard so a future
105+mechanical enumeration does not re-list them.
106+
107+## 4. Also corrected (same function, stale prose)
108+
109+`decode_block_128rect`'s `SB128RECT_REPLAY_SPAN_MISMATCH_HITS` comment still
110+read "4:2:2 … is refused by name at the sequence header, so the arm is
111+unreachable there". 4:2:2 is admitted (`lane-av1422lift`) and the arm fires
112+(mismatch 9 on the two strip cells, byte-exact). The comment now says so and
113+states that nothing asserts the mismatch is zero.
114+
115+## 5. Evidence summary
116+
117+- Site 1 fix: `decode.rs` 24946–24950 (guard + `around_mi_422_chroma`).
118+- Site 2/3: comments only (`decode.rs` 49578, 50430) — no code change, closed
119+ as unreachable.
120+- Gate: `stream.rs::the_422_block_128rect_chroma_chunk_gather_stays_per_axis`
121+ (source scan + 72-unit reachability arm), mutation-proven red twice.
122+- Pinned fixtures: `422_intrabc_sb128_strip.obu` (1672 B, sha256 prefix
123+ `f92000db86df7acc`), `422_intrabc_sb128_strip_notxsearch.obu` (1675 B,
124+ `80dd0d4e93fd6c3d`).
125+
126+## 6. Repro
127+
128+```text
129+git worktree add -b lane/av1422gather3 ~/.cache/wt/av1422gather3 90778367
130+cd ~/.cache/wt/av1422gather3
131+CARGO_TARGET_DIR=~/.cache/tgt-av1422gather3 cargo build -p ec-av1 --example dump_yuv
132+B=~/.cache/tgt-av1422gather3/debug/examples
133+$B/dump_yuv crates/ec-av1/fixtures/422_intrabc_sb128_strip.obu /tmp/ours # 384x320 yuv422p
134+ffmpeg -v error -i crates/ec-av1/fixtures/422_intrabc_sb128_strip.obu -pix_fmt yuv422p \
135+ -f rawvideo /tmp/ref.yuv
136+TMPDIR=/dev/shm CARGO_TARGET_DIR=~/.cache/tgt-av1422gather3 \
137+ cargo test -p ec-av1 --lib -- the_422_block_128rect_chroma_chunk_gather_stays_per_axis
138+```