From 76c34660473f190bed1a97cabfb7a4a501f33b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 26 Sep 2026 03:54:28 +0300 Subject: [PATCH] ec-av1: lane-av1422ctxgather -- the 4:2:2 strip chroma entropy context fixed at source: decode_inter_block's pair write and around_c gather kept the 4:2:0 pair span (16,8) for a HORZ strip that at ss (1,0) is its OWN chroma reference, so the strip at mi(8,4) zeroed left[9][1] under the mi(8,2) 8x8 leaf's level 5 and the mi(9,4) strip's U all_zero read one ctx row low (t422 frame 1, the parent lane's anchor; the write span is (16,4) at ss (1,0)); the strip chroma gathers (decode_rect4_16_strip's else arm, decode_inter_block's around_c, read_inter_rect_chroma's chunked units) route through around_mi_422_chroma at ss (1,0) -- one chroma column spans two luma mi at ss_x 1 and the per-mi gather double-counts each column's dc sign; 4:2:0/4:4:4 take the original expressions verbatim (all edits gated ss_x==1 && ss_y==0), lossless 4:4:4 stream gates green, frame 0 of the pinned t422 fixture exact at all ladder stages, the inter divergence drops 10x on frame 1 (Y 23948 -> 1739) with no frame regressed and the residual attributed downstream (first diffs Y (96,240), U (2,44), V (0,44)); the 4:2:2 header refusal stays unconditional --- crates/ec-av1/src/decode.rs | 75 ++++++++++++++++++++++++--- lanes/av1422ctxgather.report.md | 89 +++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 8 deletions(-) create mode 100644 lanes/av1422ctxgather.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 2bd42aa6..81432d1d 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -15328,7 +15328,18 @@ fn decode_rect4_16_strip( } (Grid::Zero(cw * ch), Grid::Zero(cw * ch)) } else { - let around = neighbours.around_mi_rect(pair_mi, pw, ph); + // lane-av1422ctxgather: 4:2:2's chroma above votes sample + // every second luma-mi cell + // ([`Neighbours::around_mi_422_chroma`]: at ss_x 1 one + // chroma 4-px column spans two luma mi columns, and the + // plain per-mi gather counts each column's whole-unit dc + // sign twice). The left extent is unsubsampled at ss_y 0, + // so its per-mi cells already line up one-to-one. + let around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { + neighbours.around_mi_422_chroma(pair_mi, pw, ph) + } else { + neighbours.around_mi_rect(pair_mi, pw, ph) + }; // lane-av1-444: the strip's own 16x4 / 4x16 chroma at ss 0/0 // (`TxbSet::Chroma8`, `get_txsize_entropy_ctx(TX_16X4)` = // the 8x8 set); 4:2:0 keeps the pair's 8x4 / 4x8. @@ -23347,7 +23358,19 @@ fn read_inter_rect_chroma( at_mi.1 + cu_col * (span_x / MI), ); let cu_around = if multi { - neighbours.around_mi_rect(cu_mi, span_x, span_y) + // lane-av1422ctxgather: at ss (1,0) the unit's above + // span covers `uw` chroma px = `2*uw` luma mi columns, + // so the plain per-mi gather counts every chroma + // column's whole-unit dc sign twice + // ([`Neighbours::around_mi_422_chroma`]; the left + // extent is 1:1 at ss_y 0). 4:2:0 keeps the per-mi + // gather (both extents duplicated there, measured + // elsewhere). + if ss_x(fctx) == 1 && ss_y(fctx) == 0 { + neighbours.around_mi_422_chroma(cu_mi, span_x, span_y) + } else { + neighbours.around_mi_rect(cu_mi, span_x, span_y) + } } else { around }; @@ -34557,12 +34580,31 @@ fn decode_inter_block( // EVEN strip's mi origin and spans both strips, so its // above/left coefficient context is the pair's, not this // strip's row. + // lane-av1422ctxgather: at 4:2:2 (ss 1,0) a HORZ strip is + // its own chroma reference (`is_chroma_reference`'s + // `!(subsampling_y)` row clause) -- pair_mi is the strip + // itself and the context reads the strip's own 8x4, ONE + // luma-mi row tall, with the 4:2:2 above sampling + // ([`Neighbours::around_mi_422_chroma`]). The 4:2:0 pair + // merge keeps the (16, 8) span and the plain per-mi gather + // (as do 4:4:4 and every VERT shape). let around_c = match strip_chroma { - Some(s) if s.has_chroma => neighbours.around_mi_rect( - s.pair_mi, - if s.horz { 16 } else { 8 }, - if s.horz { 8 } else { 16 }, - ), + Some(s) if s.has_chroma => { + let (pw, ph) = if s.horz { + if ss_x(fctx) == 1 && ss_y(fctx) == 0 { + (16, 4) + } else { + (16, 8) + } + } else { + (8, 16) + }; + if ss_x(fctx) == 1 && ss_y(fctx) == 0 { + neighbours.around_mi_422_chroma(s.pair_mi, pw, ph) + } else { + neighbours.around_mi_rect(s.pair_mi, pw, ph) + } + } _ => around, }; let luma_tx_type; @@ -36199,7 +36241,24 @@ fn decode_inter_block( } } if let Some((s, u_state, v_state)) = pair_chroma { - let (pw, ph) = if s.horz { (16usize, 8usize) } else { (8, 16) }; + // lane-av1422ctxgather: at 4:2:2 (ss 1,0) a horz strip is its own + // chroma reference -- no pair merge ever happened, so the write + // covers the strip's own 8x4: ONE luma-mi left row (libaom's + // `av1_set_contexts` for the TX_8X4 at ss_y 0 stamps + // `tx_size_high_unit` = 1 cell). The 4:2:0 pair's two-row (16, 8) + // span would overwrite the NEXT row's leaf-stamped level state + // (measured: t422 frame 1, the strip at mi(8,4) zeroed + // left[9][1] under the mi(8,2) 8x8's level 5 before the mi(9,4) + // strip's own all_zero read it one ctx row low). + let (pw, ph) = if s.horz { + if ss_x(fctx) == 1 && ss_y(fctx) == 0 { + (16usize, 4usize) + } else { + (16usize, 8usize) + } + } else { + (8, 16) + }; let round_up_even = |n: usize| n.div_ceil(2) * 2; let (bound_h, bound_w) = ( round_up_even(neighbours.mi_rows), diff --git a/lanes/av1422ctxgather.report.md b/lanes/av1422ctxgather.report.md new file mode 100644 index 00000000..bae9f22f --- /dev/null +++ b/lanes/av1422ctxgather.report.md @@ -0,0 +1,89 @@ +# lane-av1422ctxgather — the 4:2:2 strip chroma context gather and the +# clobbering pair write fixed at source; 4:2:0/4:4:4 byte-identical; the +# 4:2:2 header refusal STAYS + +## Outcome + +1. **The clobber (the anchor's root cause).** `decode_inter_block`'s + `pair_chroma` write hardcoded the 4:2:0 pair span `(pw, ph) = (16, 8)` + for every horz strip. At 4:2:2 (ss 1,0) a horz 16x4 strip is its OWN + chroma reference (`is_chroma_reference`'s `!(subsampling_y)` row + clause) — no pair merge ever happened, `pair_mi` is the strip itself, + and the two-row span overwrote one row it does not own. Measured + (t422 frame 1, EC422WATCH probe, since removed): the inter strip at + mi(8,4) zeroed `left[9][1]` under the mi(8,2) 8x8 leaf's level 5 + before the mi(9,4) strip's own U `all_zero` read it one ctx row low + (ours base 0 + 7, oracle base 1 + 7 — the parent lane's anchor + facts, reproduced). The span is now `(16, 4)` at ss (1,0); 4:2:0 + keeps `(16, 8)`; VERT keeps `(8, 16)` in every format. +2. **The inter strip's chroma gather.** `decode_inter_block`'s + `around_c` gathered the horz strip's context with + `around_mi_rect(pair_mi, 16, 8)` — the pair span AND the plain + per-mi above sampling (one chroma 4-px column spans two luma mi + columns at ss_x 1, so every column's whole-unit dc sign is counted + twice). At ss (1,0) it now reads `(16, 4)` through + [`Neighbours::around_mi_422_chroma`]; the 4:2:0 pair merge keeps the + (16, 8) span and the per-mi gather, as do 4:4:4 and every VERT shape. +3. **The intra strip's chroma gather** (the charter's named site, + `decode_rect4_16_strip` ~15331): the else arm's + `around_mi_rect(pair_mi, pw, ph)` is routed through + [`around_mi_422_chroma`] at ss (1,0) — the same measured rule as the + other chroma gathers (10754/13774/15297/17862/18061). Shapes at other + subsamplings keep the original gather. +4. **Class sweep found one more instance, fixed:** + `read_inter_rect_chroma`'s chunked units (`multi` arm) gathered each + unit with `around_mi_rect(cu_mi, span_x, span_y)`; at ss (1,0) the + unit's above span covers `2*uw` luma mi columns and the per-mi gather + double-counts every column. Now routed through [`around_mi_422_chroma`] + at ss (1,0) only; the `else` (4:2:0/4:4:4) arm is the original + expression verbatim. +5. Untouched, byte-identical: the intra strip's own chroma write (its + pair is already `(16, 4)` at 4:2:2), `decode_rect4_16_intrabc`'s pair + write (intrabc counters are 0 on the pinned fixture and intrabc has + its own inventory discipline), the 4:2:0 group-tail chroma SKIP arm + (reserved for its owning lane), `stream.rs`, and the 4:2:2 header + refusal. The `EC_AV1_ALLOW_422_PROBE` bypass was applied to stream.rs + for the probe runs and reverted; `git status` names only decode.rs. + +## Verification + +- **The anchor, before/after (probed, EC422WATCH + EC_DCDUMP):** before, + `inter_pair` wrote `left[9][1] 5 -> 0` and the mi(9,4) strip's U gather + read `left=[None/0]` -> base 0; after, nothing writes row 9 between the + leaf's stamp and the gather, the gather reads `left=[Some(true)/5]` -> + base 1 = the oracle's row. First U divergence at the parent's anchor + (pre-all_zero rng 49672) is gone. +- **t422 inter ladder re-measured** (decode_probe + EC_AV1_{PREFILT, + POSTDEBLOCK,POSTCDEF}_DUMP vs the parent's oracle dumps, + /tmp/i422/ladder/oracle, our dumps /tmp/i422/av1422ctxgather/final): + frame 0 is **exact at all three stages (0 diffs per plane per frame)**. + Frame 1 pre-fix Y=23948 U=13513 V=13544 -> post-fix Y=1739 U=4346 + V=4974 (first Y diff moved from the anchor's ctx row to y=96 x=240; + first chroma diffs at chroma (0,44)/(2,44)). Frames 2-15 improved on + every plane (no frame regressed) but remain nonzero. +- **Frame exactness claimed ONLY where measured 0:** frame 0 at all + stages. Frames 1+ are NOT claimed. The residual is downstream of this + lane's class: frame 1's first Y diff sits at (96, 240) (mi(24,60)) and + the first U/V diffs at chroma (2,44)/(0,44) — neither is a strip + gather nor a pair write; attributing them is the next lane's first + diff. +- **4:2:0/4:4:4 identity:** all four edits are gated + `ss_x(fctx) == 1 && ss_y(fctx) == 0` with the non-4:2:2 arm the + original expression verbatim; the lossless 4:4:4 stream gates stay + green — `a_lossless_libaom_key_frame_decodes_sample_exact`, + `a_lossless_libaom_inter_frame_decodes_sample_exact`, + `a_real_aomenc_mixed_lossless_segment_frame_decodes_sample_exact`: + 3 passed, 0 failed. +- `cargo check -p ec-av1` (target dir `~/.cache/cargo-target-av1422ctxgather`): + **0 warnings, 0 errors**. +- Probes: the temporary EC422WATCH write-watch was removed before the + final build and commit; the final ladder was re-run on the clean tree + and matches the probed run exactly (the watch was eprintln-only). + +## State + +- Commit on lane-av1422ctxgather (no push): the four gated edits in + `crates/ec-av1/src/decode.rs` + this report. +- Next stop named for the owning lane: the frame-1 Y diff at mi(24,60) + and the chroma diffs at chroma (0,44)/(2,44) — re-run the stage ladder + and the EC_ISTEP/EC_COEFF pairing from there.