From f0141f93013eec7b0a977f485e5d962966a4d75d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 25 Sep 2026 05:48:07 +0300 Subject: [PATCH] lane-av1-llsub8: 444 lossless rect-leaf chroma reads per-4x4 units Two symbol-level fixes behind the 4:4:4 lossless sub8 chroma miss, both named against the oracle's EC_COEFF ladder on the regenerated ll444 fixture: - read_intra_mode_sub8 read uv_mode off the 14-symbol CFL alphabet unconditionally; a 4:4:4 lossless BLOCK_8X4/4X8 leaf's plane block is the rect itself, so is_cfl_allowed is FALSE and the stream carries the 13-symbol uv_mode_no_cfl row (wrong-alphabet-same-value). - the rect leaf's lossless chroma is (bw/4)*(bh/4) TX_4X4 units with get_txb_ctx's +10 txb_skip rows, not one TX_8X4 rect unit off ChromaRect8x4's [1][0..2] rows; the lossless arm now walks plane-major per-4x4 units through read_plane(TxbSet::Chroma4, +3) with immediate per-unit entropy stamps (tail stamps per unit too). Key frame 15001 -> 178 differing bytes, first divergence pushed from aligned step 12136 to 19228 (named stop in lanes/av1llsub8.report.md: above-band cell state behind a 16x8 leaf's U bc=2 unit). 4:2:0 lossless gates stay green; cargo check 0 warnings. --- crates/ec-av1/src/decode.rs | 133 ++++++++++++++++++++++++++++++------ lanes/av1llsub8.report.md | 84 +++++++++++++++++++++++ 2 files changed, 197 insertions(+), 20 deletions(-) create mode 100644 lanes/av1llsub8.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 5eef3725..cfe2e990 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -18771,7 +18771,21 @@ fn read_intra_mode_sub8( // desynced the tile at the very next element. let angle_delta_y = 0; let chroma = if has_chroma { - let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); + // `is_cfl_allowed` (blockd.h): at lossless CfL needs the CHROMA plane + // block to be a single TX_4X4 (`cfl_allowed_px`). True for this leaf + // at 4:2:0 (the group's plane block is 4x4) and for a 4:4:4 + // `BLOCK_4X4` split leaf, but FALSE for a 4:4:4 `BLOCK_8X4`/`BLOCK_4X8` + // rect leaf, whose plane block is the rect itself -- that shape reads + // the 13-symbol `uv_mode_no_cfl` CDF, and reading the CFL alphabet + // there desynced the tile at the rect leaf's own uv_mode symbol (measured: + // ours consumed the 14-symbol interval where the oracle read 13, first + // divergence at the rect leaf mi (8,16) of `ll444.obu`; class + // `wrong-alphabet-same-value`, lane-av1-llsub8). + let uv_mode = if cfl_allowed_px(seg_w_mi * MI, seg_h_mi * MI, fctx) { + dec.symbol(&mut cdfs.uv_mode_cfl[mode]) + } else { + dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) + }; if trace { eprintln!("TRACE sub8 uv_mode value={uv_mode} rng={}", dec.debug_state().0); } @@ -18839,7 +18853,7 @@ fn sub8_leaf_chroma444( base_q_idx: u8, smooth_neighbor_uv: bool, fctx: &crate::decode::FrameCtx, -) -> Result<(Grid, Grid)> { +) -> Result<(Grid, Grid, usize, usize)> { let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; let (w_mi, h_mi) = (bw / MI, bh / MI); neighbours.record_uv_mode_mi(lmi.0, lmi.1, w_mi, h_mi, uv_predict_mode); @@ -18867,7 +18881,7 @@ fn sub8_leaf_chroma444( hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); fctx.intrabc_chroma_tx.with(|c| c.set(None)); } - (Grid::Zero(bw * bh), Grid::Zero(bw * bh)) + (Grid::Zero(bw * bh), Grid::Zero(bw * bh), 4, 4) } else if let Some(dv) = intrabc_dv { // Frame copy at the DV; the residual still reads the ordinary chroma // tables with the inherited luma `tx_type` (see the 4:2:0 tails). @@ -18897,12 +18911,12 @@ fn sub8_leaf_chroma444( 0, reach, v, px, py, 4, TX4, base_q_idx, None, None, None, smooth_neighbor_uv, fctx, )?; - (ug, vg) + (ug, vg, 4, 4) } else { let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 }; let default_tx = fctx.intrabc_chroma_tx.with(std::cell::Cell::get).unwrap_or(TxType::DctDct); - let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh)); + let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh), bw, bh); for (plane_idx, buf) in [(1usize, ub), (2usize, vb)] { let skip_ctx = usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); @@ -18942,14 +18956,62 @@ fn sub8_leaf_chroma444( 0, reach, v, px, py, 4, TX4, base_q_idx, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, )?; - (ug, vg) + (ug, vg, 4, 4) + } else if lossless(fctx) { + // lane-av1-llsub8: `read_tx_size` answers TX_4X4 for EVERY plane of + // a lossless block, so this rect leaf's chroma is (bw/4)*(bh/4) + // TX_4X4 units at their own 4x4 origins -- not the single + // TX_8X4/TX_4X8 rect unit the non-lossless arm below reads (that + // one is `av1_get_max_uv_txsize(BLOCK_8X4)`'s rect transform, which + // lossless never reaches). Measured on `ll444.obu`'s HORZ leaf at + // mi (8,16): the oracle codes U units at bc 0,1 with + // `get_txb_ctx`'s +10 rows (`txb_skip_cdf[0][10..12]`, + // plane pels 32 > tx pels 16) where the rect-unit read consumed + // `txb_skip_cdf[1][0]` -- the tile desynced at that U unit's + // all_zero symbol. Walk is plane-major like libaom + // `decode_token_recon_block`'s plane/row/col loop; each unit adds + // 3 to the chroma context (Chroma4's `big` rows, the same fold + // [`read_plane`]'s 128x128 caller uses); CFL is never allowed at + // lossless, so no alpha rides along. + let mut out_u = Grid::Own(vec![0i32; bw * bh]); + let mut out_v = Grid::Own(vec![0i32; bw * bh]); + for (plane_idx, buf) in [(1usize, u), (2usize, v)] { + for ur in 0..bh / 4usize { + for uc in 0..bw / 4usize { + let (umi_r, umi_c) = (lmi.0 + ur, lmi.1 + uc); + let (upx, upy) = (px + uc * 4, py + ur * 4); + let unit_around = neighbours.around_mi_rect((umi_r, umi_c), 4, 4); + let unit_reach = Reach::of(4, upx, upy, y.width, y.height, fctx); + let grid = read_plane( + dec, cdfs, TxbSet::Chroma4, scan4, plane_idx, unit_around[plane_idx], + uv_mode, uv_predict_mode, 0, unit_reach, buf, upx, upy, 4, + TX4, base_q_idx, None, None, Some(3), smooth_neighbor_uv, fctx, + )?; + let cell = grid_unit_state(&grid, 4, 0, 0, 4, 4); + if umi_r < neighbours.mi_rows { + neighbours.left[umi_r][plane_idx] = cell; + } + if umi_c < neighbours.mi_cols { + neighbours.above[umi_c][plane_idx] = cell; + } + let out = if plane_idx == 1 { &mut out_u } else { &mut out_v }; + if let Grid::Own(v) = out { + for r in 0..4usize { + v[(ur * 4 + r) * bw + uc * 4..][..4] + .copy_from_slice(&grid[r * 4..][..4]); + } + } + } + } + } + (out_u, out_v, 4, 4) } else { // `av1_get_max_uv_txsize(BLOCK_4X8/8X4)` at ss 0/0 is the rect // transform itself; its `tx_type` is mode-derived with the // <32 set (`decode_rect_split`'s own rule for this shape). let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 }; let default_tx = default_intra_tx_type(uv_predict_mode as u8); - let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh)); + let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh), bw, bh); for plane_idx in 1..=2usize { let skip_ctx = usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); @@ -18977,23 +19039,54 @@ fn sub8_leaf_chroma444( } }; // libaom `av1_set_entropy_contexts` stamps above/left PER TRANSFORM UNIT: - // the leaf's own chroma span, not the 8x8 group's. - let (u_grid, v_grid) = grids; - let u_state = neighbour_state(&u_grid); - let v_state = neighbour_state(&v_grid); - for cell in 0..h_mi { - if lmi.0 + cell < neighbours.mi_rows { - neighbours.left[lmi.0 + cell][1] = u_state; - neighbours.left[lmi.0 + cell][2] = v_state; + // the leaf's own chroma span, not the 8x8 group's. A rect leaf's lossless + // units (4x4) each carry their own state -- a 4:2:0-style whole-leaf smear + // gave one unit's state to its neighbour's cells (lane-av1-llsub8). + let (u_grid, v_grid, unit_w, unit_h) = grids; + // One unit covers `unit_w/MI` mi columns and `unit_h/MI` mi rows (a 4x4 + // unit: one cell each; the non-lossless rect unit: the whole leaf). + let (uw_cells, uh_cells) = ((unit_w / MI).max(1), (unit_h / MI).max(1)); + for ur in 0..h_mi / uh_cells { + for uc in 0..w_mi / uw_cells { + let (x0, y0) = (uc * unit_w, ur * unit_h); + let us = grid_unit_state(&u_grid, bw, x0, y0, unit_w, unit_h); + let vs = grid_unit_state(&v_grid, bw, x0, y0, unit_w, unit_h); + for cell in 0..uh_cells { + if lmi.0 + ur * uh_cells + cell < neighbours.mi_rows { + neighbours.left[lmi.0 + ur * uh_cells + cell][1] = us; + neighbours.left[lmi.0 + ur * uh_cells + cell][2] = vs; + } + } + for cell in 0..uw_cells { + if lmi.1 + uc * uw_cells + cell < neighbours.mi_cols { + neighbours.above[lmi.1 + uc * uw_cells + cell][1] = us; + neighbours.above[lmi.1 + uc * uw_cells + cell][2] = vs; + } + } } } - for cell in 0..w_mi { - if lmi.1 + cell < neighbours.mi_cols { - neighbours.above[lmi.1 + cell][1] = u_state; - neighbours.above[lmi.1 + cell][2] = v_state; + Ok((u_grid, v_grid, unit_w, unit_h)) +} + +/// [`neighbour_state`] over one `(unit_w x unit_h)` transform unit at offset +/// `(x0, y0)` inside a `bw`-wide assembled chroma grid -- the per-unit stamp +/// the rect arms need when a leaf carries more than one unit. +fn grid_unit_state(grid: &Grid, bw: usize, x0: usize, y0: usize, unit_w: usize, unit_h: usize) -> Neighbour { + let mut cul = 0u32; + let mut dc = None; + for r in 0..unit_h { + for c in 0..unit_w { + let v = grid[(y0 + r) * bw + x0 + c]; + cul += v.unsigned_abs(); + if r == 0 && c == 0 && v != 0 { + dc = Some(v < 0); + } } } - Ok((u_grid, v_grid)) + if cul == 0 { + return Neighbour::default(); + } + Neighbour { level: cul.min(7) as u8, dc } } diff --git a/lanes/av1llsub8.report.md b/lanes/av1llsub8.report.md new file mode 100644 index 00000000..abf6af9b --- /dev/null +++ b/lanes/av1llsub8.report.md @@ -0,0 +1,84 @@ +# lane-av1-llsub8: 4:4:4 lossless sub8 chroma coefficient miss (r1) + +## Charter + +Localize the 4:4:4 lossless sub8 chroma coefficient miss. Work in +`edith_codecs-av1llsub8` @ 38393554; own `sub8_leaf_chroma444` and the +coefficient read it calls. 4:2:0 lossless stays exact. + +## Repro + +Fixture regenerated per the llintra report's recipe (raw `--i444` input, same +encoder): `testsrc2 128x96` yuv444p, 6 frames, `aomenc --profile=1 +--lossless=1 --enable-palette=0 --enable-intrabc=0` (`~/ll444.obu`, 26511 B; +key-frame cut `~/ll444kf.obu`). Pre-fix key frame: 15001/36864 bytes differ +from `aomdec --rawvideo` (first diff byte 4181). Probe exits 101 at frame 1 +(`reconstruct_mc_rect`, decode.rs:2589 path) — the charter's named inter side, +untouched. + +## Ladder (EC_TRACE_COEFF, symbol-anchored, key frame only) + +12136 aligned `(tag,rng)` steps, then: + +1. **uv_mode alphabet (FIXED).** First divergence is *not* a coefficient: at + the first 8x8 `PARTITION_HORZ` leaf (mi (8,16)) the leaf's own `uv_mode` + symbol desyncs. `read_intra_mode_sub8` read `cdfs.uv_mode_cfl[mode]` + (14-symbol) unconditionally; a 4:4:4 lossless `BLOCK_8X4`/`BLOCK_4X8` + leaf's plane block is the rect itself, so `is_cfl_allowed` is FALSE and the + stream carries the 13-symbol `uv_mode_no_cfl` row (class + `wrong-alphabet-same-value`; oracle post-read rng 33082 vs ours 38358 off + the same entry 56906). Fix: gate on `cfl_allowed_px(seg_w_mi*MI, + seg_h_mi*MI)` — true for the 4:2:0 group tail (plane block 4x4) and 4:4:4 + split leaves, false for 4:4:4 rect leaves; 4:2:0 unchanged. +2. **Rect-leaf lossless chroma unit structure (FIXED).** Next divergence + (step 12138): the rect leaf's chroma. `read_tx_size` answers TX_4X4 for + EVERY plane of a lossless block, so the oracle codes the 8x4 leaf's chroma + as TWO plane-major TX_4X4 units (U bc 0, U bc 1, then V) with + `get_txb_ctx`'s **+10** rows (`plane pels 32 > tx pels 16`, + `txb_skip_cdf[0][10..12]`), while ours read ONE TX_8X4 rect unit off + `ChromaRect8x4` (rows `[1][0..2]`, only reached correctly non-losslessly). + Fix: lossless rect arm reads per-4x4 units via `read_plane(TxbSet::Chroma4, + luma_skip_ctx=Some(3))` (Chroma4's `big` rows = `[0][10..12]`), plane-major + per `decode_token_recon_block`, per-unit ctx from the unit's own + `around_mi_rect`, immediate per-unit `av1_set_entropy_contexts` stamps. + The non-lossless arm (single rect transform, rows `[1][7..9]`) is kept. + Tail now stamps per-unit (`grid_unit_state`) instead of one whole-leaf + smear; 4x4 leaves and the single-unit rect arm are bit-identical to before. + +After both fixes: divergence moves to step 19228, key frame 178/36864 bytes +off (first diff byte 8320). + +## STOP — named, not fixed (budget) + +Step 19228: inside a **16x8 leaf** (`PARTITION_HORZ` of a 16x16; U units +bc 0..3, br 0..1) the U unit at **bc=2, br=0** reads `txb_skip` base **1** +(ours, row `[0][11]` — the same `+3` fold, cdf `[8570,...]`) where the oracle +reads base **0** (row `[0][10]`, cdf0=32705): one of ours' above/left entropy +cells for that unit is stamped nonzero where the oracle's is clean. bc=0/1 of +the same leaf aligned (rng 40879/40628), and ours' `dcctx=2` on the diverging +read (vs 0 on bc=1) says the polluted cell carries a DC-sign vote — i.e. an +above-band cell written by the block above with a state the oracle doesn't +hold. Next step: dump the above-band cell contents (both sides, +`EC_DCDUMP`/oracle equivalent) for the units directly above this leaf's +bc 2/3 columns and diff the writer — suspect the 16x8-path's per-unit stamp +map (which unit owns which above/left cell when a leaf spans >1 mi row) in +`decode_leaf_rect`'s chroma loop, NOT `sub8_leaf_chroma444` (16x8 leaves are +read by `decode_leaf_rect`, which already had per-unit +3 reads before this +lane). + +## Verification (measured) + +- `a_lossless_libaom_key_frame_decodes_sample_exact` + `a_lossless_libaom_ + inter_frame_decodes_sample_exact` (4:2:0): green. +- `cargo check -p ec-av1 --all-targets`: 0 warnings, 0 errors. +- Key frame: 15001 → 178 differing bytes; first diff byte 4181 → 8320. +- Consumed-count ladder: 12136 → 12138 → 19228 aligned steps. + +## Deferred (spoken, not hidden) + +- 16x8 above-band stamp diff above (fix-now; named cell + writer). +- Intrabc rect leaves at lossless 444 still route the rect arm's + `read_coeffs_rect` path with the wrong unit structure — unreachable with + `--enable-intrabc=0` fixtures; same fix shape as the non-intrabc arm + (deferred(fix-now after the inter side is green, with an intrabc 4:4:4 + fixture), matching the llintra report's deferral).