From 7f6f585b7b5bfb3783e92b2f74935114b3c73192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 22:13:03 +0300 Subject: [PATCH] fix(av1): 16x8/8x16 intra strips never read use_filter_intra -- one dropped symbol per DC_PRED rect leaf desynced the tile av1_filter_intra_allowed_bsize is 'both sides <= 32', so libaom reads the use_filter_intra flag for every DC_PRED 16x8/8x16 block; filter_intra_size_class_rect returned None at those two sizes, so decode_leaf_rect skipped a symbol the encoder wrote and every following block decoded from a desynced entropy stream -- silently, with no error and a whole-frame pixel mismatch. Adds default_filter_intra_cdfs rows 5 (BLOCK_16X8, 9394) and 4 (BLOCK_8X16, 12551) and an asymmetric pin for the pair. Root-caused against the instrumented aomdec EC_TRACE_COEFF ladder: at leaf mi=(2,4) ours and the oracle both decode mode=0 uv_mode=9 skip=0, our range 34808 vs the oracle's 40668; after the fix both are 40668 and the next transform block's all_zero reads 40269 in both. The same ladder proves r2's rect coefficient path (scans, eob alphabets, txb sets, nz-map/br/DC-sign contexts) bit-exact for a real non-skip TX_16X8 leaf and both its TX_8X4 chroma halves. Lane gate stays RED and no refusal is lifted: a second, independent defect (the rect leaf's kf_y_mode context comes from the coarse 16x16 neighbour grid, not the real mi grid) is located to the element in lanes/rectx-r3.handoff.md. cargo test -p ec-av1 --lib: 269 passed, 1 failed (this lane's own gate), 24 ignored. --- crates/ec-av1/src/cdf.rs | 10 +++- crates/ec-av1/src/cdf_state.rs | 5 +- crates/ec-av1/src/decode.rs | 54 ++++++++++++++++++++- crates/ec-av1/src/stream.rs | 3 +- lanes/rectx-r3.handoff.md | 54 +++++++++++++++++++++ lanes/rectx-r3.report.md | 88 ++++++++++++++++++++++++++++++++++ 6 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 lanes/rectx-r3.handoff.md create mode 100644 lanes/rectx-r3.report.md diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 1929f374..466f5513 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -412,13 +412,21 @@ pub const CFL_SIGN: [u16; 9] = [1418, 2123, 13340, 18405, 26972, 28343, 32294, 3 /// (12756/14301, `default_filter_intra_cdfs` index 8/7) -- the two rect /// strips `PARTITION_HORZ`/`PARTITION_VERT` decode, each with its own /// distinct default probability from every square class above. -pub const FILTER_INTRA: [[u16; 3]; 6] = [ +/// `[6]`/`[7]` (lane-rectx r3) add `BLOCK_16X8`/`BLOCK_8X16`'s own rows +/// (9394/12551, `default_filter_intra_cdfs` index 5/4) -- the 16x8/8x16 +/// strips [`crate::decode`]'s `decode_leaf_rect` codes. Missing them meant +/// the flag was never read at that size at all (libaom reads it for every +/// DC_PRED block whose sides are both <= 32), so every DC_PRED 16x8 strip +/// dropped one symbol and desynced the tile from that block on. +pub const FILTER_INTRA: [[u16; 3]; 8] = [ [4621, 32768, 0], [7866, 32768, 0], [12408, 32768, 0], [22343, 32768, 0], [12756, 32768, 0], [14301, 32768, 0], + [9394, 32768, 0], + [12551, 32768, 0], ]; /// `default_filter_intra_mode_cdf` (entropymode.c): which of the five /// `FILTER_INTRA_MODES` a `use_filter_intra` block picks. diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index a230b324..1f4a4196 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -462,9 +462,10 @@ pub(crate) struct Cdfs { /// since spec 8.3.2 adapts one component without touching the other. pub mv_comp: [MvComponentCdfs; 2], /// The `use_filter_intra` flag, indexed by block-size class (`[0]`=4x4, - /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32, `[4]`=32x16, `[5]`=16x32) -- + /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32, `[4]`=32x16, `[5]`=16x32, + /// `[6]`=16x8, `[7]`=8x16) -- /// see [`cdf::FILTER_INTRA`]. - pub filter_intra: [[u16; 3]; 6], + pub filter_intra: [[u16; 3]; 8], /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks. pub filter_intra_mode: [u16; 6], /// `TxMode::Select`'s `tx_depth` flag at an 8x8 block, by `tx_size_context` diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 8759a6cb..cc4fa41e 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1651,7 +1651,15 @@ fn read_coeffs_rect( default_tx_type: TxType, ) -> Result<(Vec, TxType)> { let mut grid = vec![0i32; w * h]; + let entry_rng = dec.debug_state().0; let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; + if std::env::var_os("EC_TRACE_COEFF").is_some() { + let (rng, _) = dec.debug_state(); + eprintln!( + "EC_COEFF_STEP tag=all_zero_rect w={w} h={h} ctx={skip_ctx} all_zero={} entry={entry_rng} rng={rng}", + u8::from(all_zero) + ); + } if all_zero { return Ok((grid, TxType::DctDct)); } @@ -1732,6 +1740,10 @@ fn read_coeffs_rect( } else { dec.literal(1) == 1 }; + if trace { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF_STEP tag=sign_rect pos={pos} sign={} rng={rng}", u8::from(negative)); + } let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { let g = read_golomb(dec)?; level + g as i32 @@ -2684,6 +2696,13 @@ fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option { match (bw, bh) { (32, 16) => Some(4), (16, 32) => Some(5), + // lane-rectx r3: `av1_filter_intra_allowed_bsize` is "both sides <= + // 32", so a 16x8/8x16 strip reads the flag too -- returning `None` + // here dropped that symbol on every DC_PRED strip of that size and + // desynced the tile (caught against an aomdec EC_TRACE_COEFF ladder: + // identical mode/uv_mode values, range 34808 vs the oracle's 40668). + (16, 8) => Some(6), + (8, 16) => Some(7), _ if bw == bh => filter_intra_size_class(bw), _ => None, } @@ -3555,7 +3574,10 @@ fn decode_leaf_rect( ) -> Result { let _ = base_q_idx; if std::env::var_os("EC_AV1_RECTX_TRACE").is_some() { - eprintln!("decode_leaf_rect: leaf_mi={leaf_mi:?} bw={bw} bh={bh} outer_at={outer_at:?}"); + eprintln!( + "decode_leaf_rect: leaf_mi={leaf_mi:?} bw={bw} bh={bh} outer_at={outer_at:?} rng={}", + dec.debug_state().0 + ); } let (r, c) = outer_at; let mut above_mode = neighbours.above_mode[c]; @@ -13572,6 +13594,36 @@ mod tests { assert_eq!(SCAN_4X8, LIBAOM_DEFAULT_SCAN_8X4, "SCAN_4X8 vs libaom default_scan_8x4"); } + /// lane-rectx r3: `av1_filter_intra_allowed_bsize` is "both sides <= 32", + /// so a DC_PRED 16x8/8x16 strip reads a `use_filter_intra` flag exactly + /// like a square 16x16 does. [`filter_intra_size_class_rect`] returned + /// `None` at those two sizes, so the symbol was never read and the tile + /// desynced from that block on (root-caused against an instrumented + /// aomdec `EC_TRACE_COEFF` ladder on a real stream: identical `mode=0`/ + /// `uv_mode=9` values, our range 34808 vs the oracle's 40668 at the very + /// next transform block). + /// + /// The two rows are ASYMMETRIC (9394 for 16x8, 12551 for 8x16), so this + /// pin fails if the pair is ever transposed -- the shape the repo's + /// scan tables already carry (`SCAN_8X4` == libaom `default_scan_4x8`). + #[test] + fn a_rect_strip_below_16x16_reads_its_own_filter_intra_cdf_row() { + // `default_filter_intra_cdfs` (entropymode.c), indexed by BLOCK_SIZES_ALL: + // [4] = BLOCK_8X16, [5] = BLOCK_16X8, [7] = BLOCK_16X32, [8] = BLOCK_32X16. + const LIBAOM_8X16: u16 = 12551; + const LIBAOM_16X8: u16 = 9394; + let c16x8 = filter_intra_size_class_rect(16, 8).expect("16x8 reads use_filter_intra"); + let c8x16 = filter_intra_size_class_rect(8, 16).expect("8x16 reads use_filter_intra"); + assert_ne!(c16x8, c8x16, "the two orientations must not share a CDF row"); + assert_eq!(cdf::FILTER_INTRA[c16x8][0], LIBAOM_16X8, "16x8 filter_intra row"); + assert_eq!(cdf::FILTER_INTRA[c8x16][0], LIBAOM_8X16, "8x16 filter_intra row"); + // The sizes one class up keep their own distinct rows (lane-intradisp). + assert_eq!(cdf::FILTER_INTRA[filter_intra_size_class_rect(32, 16).unwrap()][0], 12756); + assert_eq!(cdf::FILTER_INTRA[filter_intra_size_class_rect(16, 32).unwrap()][0], 14301); + // A size with a side over 32 is genuinely not allowed the flag. + assert_eq!(filter_intra_size_class_rect(64, 32), None); + } + /// `is_uni_comp_ref` (lane-av1comp): a pair is unidirectional exactly /// when both references sit on the same temporal side -- the three /// `uni_comp_ref` bitstream pairs (LAST/LAST2, LAST/LAST3, LAST/GOLDEN) diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index a6143468..96b71d59 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5480,7 +5480,7 @@ mod tests { assert!(y4m.status.success()); let (width, height) = if start_x.contains("128x128") { (128usize, 128usize) } else { (64usize, 64usize) }; for cq in [16u32, 20, 24, 28, 32, 40, 50] { - for rtx in ["0"] { + for rtx in ["0", "1"] { let mut child = Command::new(aomenc_path()) .args([ "--codec=av1", "--passes=1", "--end-usage=q", @@ -5509,6 +5509,7 @@ mod tests { let bad = frames.iter().zip(&want) .filter(|(g, w)| g.y != w.y || g.u != w.u || g.v != w.v).count(); eprintln!("x={start_x} cq={cq} rtx={rtx} fired={fired} frames={} mismatched={bad}", frames.len()); + if bad == 0 { continue; } let (g, w) = (&frames[0], &want[0]); for br in 0..height / 8 { let mut line = String::new(); diff --git a/lanes/rectx-r3.handoff.md b/lanes/rectx-r3.handoff.md new file mode 100644 index 00000000..61a97ccc --- /dev/null +++ b/lanes/rectx-r3.handoff.md @@ -0,0 +1,54 @@ +# lane-rectx r3 handoff — one defect fixed, one precisely located + +TOTALS `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` (CARGO_TARGET_DIR=~/.cache/cargo-target-rectx): +**269 passed, 1 failed, 24 ignored** (1042s). The single failure is this lane's own gate, +`stream::tests::a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16_decodes_pixel_exact`; +it now stops on an unrelated third blocker ("a 32x32 partition type this decoder does not code +(value=7)" = PARTITION_VERT_B at 32x32) with the recipe as written. No other test regressed +(268 -> 269 passed is this round's new pin). + +## Do next, in this order + +1. **Per-mi y-mode neighbour grid.** `Neighbours::above_mode` (decode.rs:1806) / `left_mode` + are per-16x16 cell; libaom reads `above_mi`/`left_mi` off the real mi grid. Reproduce in + one command: + - stream: `ffmpeg -v error -f lavfi -i rgbtestsrc=size=64x64 -pix_fmt yuv420p -t 1 -vframes 1 + -f yuv4mpegpipe g.y4m` then `aomenc --codec=av1 --passes=1 --end-usage=q --cq-level=50 + --cpu-used=4 --threads=1 --row-mt=0 --sb-size=64 --kf-max-dist=0 --enable-rect-partitions=1 + --enable-ab-partitions=0 --enable-1to4-partitions=0 --enable-tx-size-search=0 + --enable-cdef=0 --enable-restoration=0 --reduced-tx-type-set=1 --min-partition-size=8 + --max-partition-size=32 --obu -o t.obu g.y4m` + - ours: `EC_TRACE_COEFF=1 EC_AV1_RECTX_TRACE=1 EC_TRACE_MODE_STEP=1 + target/debug/examples/decode_probe t.obu` + - oracle: `EC_TRACE_COEFF=1 EC_TRACE_MODE=1 ~/.cache/aom-oracle/build/aomdec --rawvideo + -o /dev/null t.obu` + - THE ELEMENT: leaf `mi=(4,12)`, bw=16 bh=8, outer_at=(1,3). Both enter at range **59792**. + Oracle `EC_IMODE_VAL mode=2 uv_mode=2 skip=0 -> rng=49036`; ours `mode=0 uv_mode=13 -> + rng=47236`. First 15 transform blocks of the frame are bit-exact before it. + - Fix shape: a per-mi mode grid written wherever `fill_skip_grid`/`fill_skip_grid_rect` + (13 sites) is written, read by `read_intra_mode*`'s `above_ctx`/`left_ctx`. +2. **Then re-pick the gate recipe.** `stream::tests::sweep_rectx_recipes` (`#[ignore]`, + `--nocapture`) sweeps 9 lavfi sources x 7 cq x reduced-tx-type-set 0/1 and prints + `fired=`/refusal/`mismatched=` per cell plus an 8x8 mismatch map. After the filter_intra fix + exactly one cell decodes to completion (rgbtestsrc cq=50 rtx=1, fired=4) and it mismatches + on defect (1). Every other cell refuses; the dominant refusal is + "a HORZ_A/HORZ_B/VERT_A partition below 16x16" — this aomenc build writes AB partitions + below 16x16 even with `--enable-ab-partitions=0` (126/126 cells swept; same quirk the ledger + records at SB level). Wiring HORZ_A/HORZ_B/VERT_A below 16x16 may be the cheaper route to a + green gate than recipe hunting. +3. Only then lift "a coded (non-skip) HORZ/VERT rect strip below 16x16" and "a non-skip + rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding" in + `refusal_inventory.rs` + `gate_coverage.rs`. NOT lifted this round. + +## Do NOT re-do + +- The r2 coefficient path (scans, `EOB_PT_128_LUMA`/`EOB_PT_32_CHROMA`, txb sets, nz-map/br + ctx, DC-sign ctx) is range-exact against aomdec for a real non-skip TX_16X8 leaf + both + TX_8X4 chroma halves. Don't re-derive it. +- `base_ctx_rect`'s 11/16 rect nz-map offsets are CORRECT as written (display-orientation + convention: our (row,col) are display row/col, libaom's flat `av1_nz_map_ctx_offset` tables + are column-major so their INDEX packing, not their rule, is what differs). Swapping the two + constants was tried this round: the 126-cell sweep went from 2 full decodes to 0 (every cell + refused). Dead end. +- `SCAN_8X4`/`SCAN_4X8` were re-checked verbatim against + `~/.cache/aom-oracle/src/av1/common/scan.c` this round. They are right. diff --git a/lanes/rectx-r3.report.md b/lanes/rectx-r3.report.md new file mode 100644 index 00000000..c3c83a5f --- /dev/null +++ b/lanes/rectx-r3.report.md @@ -0,0 +1,88 @@ +# lane-rectx r3 — the rect leaf was bit-exact; the block AFTER it was not + +Branch `lane-rectx`, on top of `main` 3808cf8 (`git merge-base --is-ancestor main HEAD` = OK, +no rebase needed). Prior state: r1/r2 landed non-skip TX_16X8/TX_8X16 rect-leaf coefficient +decode (`decode_leaf_rect`'s lane-rectx arm, `read_coeffs_rect`, `TxbSet::LumaRect16x8{,Set1}` +/`ChromaRect8x4`, `EOB_PT_128_LUMA`/`EOB_PT_32_CHROMA`, `SCAN_8X4`/`SCAN_4X8`) UNPROVEN, with +the lane's own gate red. + +## What this round proved + +1. **The r2 coefficient path is right.** Against the instrumented oracle + (`~/.cache/aom-oracle/build/aomdec`, `EC_TRACE_COEFF=1`) on a real aomenc stream, a + non-skip `TX_16X8` luma leaf plus both its `TX_8X4` chroma halves decode with an + IDENTICAL msac range at every element (`all_zero` 37445, `tx_type` 55512, `eob`=1 41024, + `base_eob` pos 0 level 1 64544, DC sign 37072, chroma `all_zero`=1 40760 / 47390). + Scans, eob CDF alphabet, txb sets, nz-map/br contexts and the DC-sign context at this size + are all confirmed by that ladder, not by argument. +2. **`SCAN_8X4`/`SCAN_4X8` re-checked against the real `scan.c`** (not a memory of it): + `~/.cache/aom-oracle/src/av1/common/scan.c` `default_scan_4x8` == our `SCAN_8X4` and + `default_scan_8x4` == our `SCAN_4X8`, verbatim, which is what + `decode.rs::tests::the_rect_scan_tables_match_libaom...` already pins (asymmetric pair, a + swap fails it). +3. **Root cause of the desync (FIXED): the `use_filter_intra` flag was never read at + 16x8/8x16.** `av1_filter_intra_allowed_bsize` is "both sides <= 32", so libaom reads the + flag for every DC_PRED 16x8/8x16 block; `filter_intra_size_class_rect` returned `None` + there, so we dropped one symbol per DC_PRED strip and desynced the tile from that block on + — silently, with a full-frame pixel mismatch and no error. Ladder evidence: leaf mi=(2,4), + we and the oracle both decode `mode=0 uv_mode=9 skip=0`, but our post-mode range is 34808 + where the oracle's is 40668 (value-equal narrowing drift). After the fix our range at that + point is 40668 and the next transform block's `all_zero` reads 40269 — bit-identical to the + oracle. + +## Files changed + +- `crates/ec-av1/src/cdf.rs:415` — `FILTER_INTRA` 6 -> 8 rows, adding `default_filter_intra_cdfs` + index 5 (BLOCK_16X8, 9394) and 4 (BLOCK_8X16, 12551). +- `crates/ec-av1/src/cdf_state.rs:467` — the field's type/doc follow. +- `crates/ec-av1/src/decode.rs` `filter_intra_size_class_rect` — `(16,8) => Some(6)`, + `(8,16) => Some(7)`; the actual fix. +- `crates/ec-av1/src/decode.rs` `read_coeffs_rect` — three env-gated trace prints + (`tag=all_zero_rect` with the pre-symbol range, `tag=sign_rect`) under the existing + `EC_TRACE_COEFF`, plus the entry range on the `EC_AV1_RECTX_TRACE` line; these are what made + the cross-decoder bisection possible and are kept for the successor. +- `crates/ec-av1/src/decode.rs` tests — new + `a_rect_strip_below_16x16_reads_its_own_filter_intra_cdf_row`. +- `crates/ec-av1/src/stream.rs` `sweep_rectx_recipes` (scratch, `#[ignore]`) — also sweeps + `--reduced-tx-type-set=1`, and only dumps the 8x8 mismatch map when there IS a mismatch. + +## Checks + +- `cargo test -p ec-av1 --lib -- filter_intra scan_` -> 8 passed, 0 failed (includes the new pin + and the existing `a_real_aomenc_filter_intra_stream_decodes_pixel_exact`). +- `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` -> see TOTALS below. +- EVIDENCE: /tmp/claude-1000/.../scratchpad/{s.obu,aom.log,ours.log} | ffmpeg mandelbrot 64x64 + start_x=-0.6 -> aomenc cq16 --enable-rect-partitions=1 --min-partition-size=8 + --max-partition-size=32; instrumented aomdec `EC_TRACE_COEFF=1` vs + `examples/decode_probe` `EC_TRACE_COEFF=1` | first divergence moved from the transform block + after leaf mi=(2,4) (ours 34467 vs oracle 40269) to none at that point (both 40269) after the + fix. + +## Open residue + +- **fix-now (next round, out of this round's tool budget): the rect leaf's `kf_y_mode` + context is read from the coarse 16x16 neighbour grid.** Second, independent defect, found + by the same ladder on `rgbtestsrc=size=64x64` cq=50 `--reduced-tx-type-set=1`: at leaf + mi=(4,12) (`decode_leaf_rect`, bw=16 bh=8, outer_at=(1,3)) we and the oracle ENTER the block + at the same range 59792, and then we decode `mode=0` where the oracle decodes `mode=2` + (H_PRED) — a wrong CDF row, not a wrong value. `Neighbours::above_mode`/`left_mode` + (decode.rs:1806) are per-16x16-cell; libaom takes `above_mi`/`left_mi` from the real mi grid, + so when the cell above was split into 8x8 leaves our slot holds the last-written leaf's mode + (bottom-RIGHT) instead of the true above neighbour (bottom-LEFT at mi col 12). Known class: + "context read from one cell". The fix is a per-mi mode grid alongside the existing per-mi + `skip_grid`/`lf_grid` — 13 `fill_skip_grid*` call sites would gain a mode write. It affects + every intra path, not just rect leaves. +- **deferred(the mode-grid fix above): the lane gate + `a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16_decodes_pixel_exact` is still RED**, + and its two refusals are NOT lifted. Its current recipe fails on a third, unrelated blocker + ("a HORZ_A/HORZ_B/VERT_A partition below 16x16"): a 126-cell sweep (9 lavfi sources x 7 + cq x reduced-tx-type-set 0/1, `sweep_rectx_recipes`) found aomenc writes AB partitions below + 16x16 despite `--enable-ab-partitions=0` in most cells — same aomenc quirk the ledger already + records one level up. After the filter_intra fix the sweep has exactly ONE cell that decodes + to completion (rgbtestsrc cq=50 rtx=1, 4 rect leaves fired) and it mismatches on the mode-ctx + defect above. So there is currently no recipe that can turn the gate green; a green gate + needs the mode-grid fix first. +- accepted: `read_coeffs_rect` still refuses `V_DCT`/`H_DCT` ("a non-2D tx class on a + rectangular transform"), which is why the gate recipe pins `--reduced-tx-type-set=1`. + +TOTALS: see lanes/rectx-r3.handoff.md.