diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index bc36ce1f..8dd17bdb 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -109,6 +109,10 @@ fn main() { "llintra8_chroma_walk: {}", ec_av1::decode::llintra8_chroma_walk_hits() ); + println!( + "sub8rect444_chroma_walk: {}", + ec_av1::decode::llsub8rect444_chroma_walk_hits() + ); let si = ec_av1::stream::sub8_intra_rect_hits(); println!( "sub8_intra_rect: horz8x4={} vert4x8={} chroma_ref={} mixed={} split4x4={}", diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index f8911b8c..e22ffa7b 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -8454,7 +8454,7 @@ impl Neighbours { o.2 += dc_vote(left[plane].dc); } } - out + out } /// [`Self::around`] with independent above (`w`)/left (`h`) extents, in @@ -25841,6 +25841,22 @@ pub fn llintra8_chroma_walk_hits() -> usize { LLINTRA8_CHROMA_WALK_HITS.with(|c| c.get()) } +thread_local! { + /// lane-av1sub8rect444: how many of `decode_intra_sub8_leaf`'s 8x4/4x8 + /// INTRA leaves ran the lossless 4:4:4 per-unit chroma walk (two TX_4X4 + /// units per plane, plane-major) instead of one TX_8X4/TX_4X8 rect unit + /// -- the shape whose unit count AND skip ctx the rect read got wrong. + /// A gate proving the arm fired; CHROMA_SPLIT_TX_HITS cannot attribute + /// its bumps to THIS leaf. + pub(crate) static LLSUB8RECT444_CHROMA_WALK_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// [`LLSUB8RECT444_CHROMA_WALK_HITS`]: lossless 4:4:4 chroma walks through a +/// sub-8 8x4/4x8 INTRA leaf. +pub fn llsub8rect444_chroma_walk_hits() -> usize { + LLSUB8RECT444_CHROMA_WALK_HITS.with(|c| c.get()) +} + fn read_sb128_root( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, @@ -36085,6 +36101,7 @@ fn decode_intra_sub8_leaf( } let ac = alpha.map(|_| if chroma_444 { cfl_src_rect(px, py, bw, bh) } else { cfl_src(gpx, gpy, 8) }); + let mut chroma_stamped_per_unit = false; let (u_grid, v_grid): (Grid, Grid) = if skip { if alpha.is_some() { hit!(CHROMA_SKIP_CFL_HITS); @@ -36101,6 +36118,95 @@ fn decode_intra_sub8_leaf( (Grid::Zero(16), Grid::Zero(16)) } } else if chroma_444 && bw != bh { + if lossless(fctx) { + // lane-av1sub8rect444: lossless codes TX_4X4 on EVERY plane + // (`av1_get_tx_size`, blockd.h:1383), so this leaf's 8x4/4x8 + // chroma plane block is a 1x2/2x1 raster of TX_4X4 units per + // plane, PLANE-MAJOR -- the same per-unit walk + // [`read_intra_chroma_lossless`] runs for whole blocks and this + // leaf's own luma `split` runs above, not one TX_8X4/TX_4X8 + // rect unit. The rect read was wrong twice over: its unit count + // left the second unit's coefficients in the bitstream, and its + // skip ctx fixed at above+left missed `get_txb_ctx`'s + // plane-block-bigger-than-transform term (libaom ctx 10+above + // +left, our tables' +3), where the oracle read ctx 11≡4. + let (mut uo, mut vo) = (Vec::new(), Vec::new()); + for plane_idx in 1..3 { + for tu in 0..2usize { + let (ox, oy) = if vert { (0, tu * 4) } else { (tu * 4, 0) }; + // libaom's `max_blocks_wide/high` clip: a unit whose + // origin is past the frame edge is not coded at all + // (`decode_token_recon_block`). At 4:4:4 the unit's + // chroma origin is the leaf's own (px, py). + if px + ox >= u.true_width || py + oy >= u.true_height { + continue; + } + // At 4:4:4 a TX_4X4 chroma unit IS one mi cell. + let tu_mi = (lmi.0 + oy / MI, lmi.1 + ox / MI); + let tu_around = neighbours.around_mi(tu_mi, MI); + let tu_r = tu_reach( + bw, + bh, + ox, + oy, + 4, + reach_c, + px, + py, + y.width, + y.height, fctx, + ); + let buf: &mut PlaneBuf<'static> = + if plane_idx == 1 { &mut *u } else { &mut *v }; + // The plane block (8x4/4x8) is bigger than the TX_4X4 + // transform, so `get_txb_ctx`'s chroma ctx_offset is 10, + // not 7 -- our chroma tables carry the offset-10 rows at + // +3, the same `Some(3)` [`read_intra_chroma_lossless`] + // derives from `blk_w > 4 || blk_h > 4`. + let tu_grid = read_plane( + dec, + cdfs, + TxbSet::Chroma4, + scan4, + plane_idx, + tu_around[plane_idx], + mode, + uv_predict_mode, + angle_delta_uv, + tu_r, + buf, + px + ox, + py + oy, + 4, + TX4, + base_q_idx, + None, + None, + Some(3), + smooth_neighbor_uv, fctx, + )?; + // The unit's own coefficient context, stamped per unit + // (`av1_set_contexts`); the tail's whole-leaf chroma + // stamp below is skipped for this shape. + neighbours.record_mi_chroma(tu_mi, 4, 4, plane_idx, &tu_grid); + hit!(CHROMA_SPLIT_TX_HITS); + let dst = mu_units( + if plane_idx == 1 { &mut uo } else { &mut vo }, + bw * bh, + ); + for row in 0..4 { + let start = (oy + row) * bw + ox; + dst[start..start + 4].copy_from_slice(&tu_grid[row * 4..][..4]); + } + } + } + chroma_stamped_per_unit = true; + hit!(LLSUB8RECT444_CHROMA_WALK_HITS); + ( + if uo.is_empty() { Grid::Zero(bw * bh) } else { Grid::Own(uo) }, + if vo.is_empty() { Grid::Zero(bw * bh) } else { Grid::Own(vo) }, + ) + } 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. let ca = neighbours.around_mi_rect(lmi, bw, bh); @@ -36139,6 +36245,7 @@ fn decode_intra_sub8_leaf( let ug = read_chroma(1, ca[1], alpha.zip(ac).map(|((au, _), ac)| (au, ac)))?; let vg = read_chroma(2, ca[2], alpha.zip(ac).map(|((_, av), ac)| (av, ac)))?; (ug, vg) + } } else { let chroma_around = neighbours.around_mi(if chroma_444 { lmi } else { group_mi }, if chroma_444 { 4 } else { 8 }); let ug = read_plane( @@ -36153,9 +36260,19 @@ fn decode_intra_sub8_leaf( )?; (ug, vg) }; + // The 4:4:4 lossless walk above already stamped these bands PER UNIT + // (raster-order last writer per cell, exactly libaom's per-entry last + // update); the whole-leaf aggregate would clobber the two units' distinct + // states with one, so it runs only for the one-unit shapes (the same rule + // [`decode_leaf8`]'s intrabc arm and the inter 8x8 leaf obey). The walk + // must skip the 4:2:0 group-tail below too: it spans the GROUP's two mi + // rows/cols with one composed state, which would overwrite the walk's + // own stamps with the whole-leaf aggregate. if chroma_444 { - neighbours.record_mi_chroma(lmi, bw, bh, 1, &u_grid); - neighbours.record_mi_chroma(lmi, bw, bh, 2, &v_grid); + if !chroma_stamped_per_unit { + neighbours.record_mi_chroma(lmi, bw, bh, 1, &u_grid); + neighbours.record_mi_chroma(lmi, bw, bh, 2, &v_grid); + } } else { let round_up_even = |n: usize| n.div_ceil(2) * 2; let bound_h = round_up_even(neighbours.mi_rows); diff --git a/lanes/av1sub8rect444.report.md b/lanes/av1sub8rect444.report.md new file mode 100644 index 00000000..771643df --- /dev/null +++ b/lanes/av1sub8rect444.report.md @@ -0,0 +1,95 @@ +# lane-av1sub8rect444: the lossless sub-8 rect leaf's 4:4:4 chroma per-unit walk + +## Ticket + +`lanes/av1llintercdf.report.md` "Named, not chased" item 1: at the shared state +rng=51429 after the 8x4 leaf's luma, the oracle reads the leaf's chroma as +FOUR TX_4X4 units (plane 1 bc=0,1 ctx=11 then plane 2 bc=0,1) while +`decode_intra_sub8_leaf`'s `chroma_444 && bw != bh` arm read the chroma plane +as ONE `TX_8X4`/`TX_4X8` rect unit per plane (`read_coeffs_rect`, skip ctx +fixed at above+left where the oracle derives 11≡4). Wrong unit count AND wrong +skip ctx. Template: b3f815c8's per-unit 8x8-leaf walk. + +## Change (decode.rs, one arm + one tail guard + counter) + +1. `decode_intra_sub8_leaf`'s rect arm now branches on `lossless(fctx)`: the + walk reads the leaf's 8x4/4x8 chroma plane block as a 1x2/2x1 raster of + `TX_4X4` units per plane, PLANE-MAJOR (`av1_get_tx_size`, blockd.h:1383), + via `read_plane` with `TxbSet::Chroma4`, per-unit `around_mi` (per-unit + `get_txb_ctx`), the plane-block-bigger-than-transform `+3` (libaom's +10 + rows, so above+left=1 reads row 4 ↔ libaom ctx 11 — the named 11≡4), and + the lossless tx_type drop (`read_plane`'s own `coding.tx_type = None` for + lossless + DCT_DCT force). Units anchor at the leaf's own (px, py) at + 4:4:4, clip past the frame edge like `read_intra_chroma_lossless`, stamp + each unit's own coefficient context with per-unit `record_mi_chroma` + (`av1_set_contexts` per unit), and assemble the leaf grid at stride bw. + CFL is impossible here (the uv read upstream already takes the + 13-symbol `uv_mode_no_cfl` row for this shape at lossless), so no alpha + rides along — the same `None` the template walks pass. +2. Tail guard: the whole-leaf `record_mi_chroma(lmi, bw, bh, ...)` is skipped + when the walk stamped per unit — NESTED inside the `chroma_444` branch. + The first cut wrote `if chroma_444 && !stamped { record } else { <4:2:0 + group tail> }`, which let a walked leaf fall into the 4:2:0 group tail: + it spans the GROUP's two mi rows/cols with one composed state and + clobbered the walk's own stamps (measured: `above[3][1]` flipped pos→neg + between the walk's stamp and the next leaf's read — the oracle wants the + per-unit value). The nested form skips BOTH branches for the walk. +3. Non-vacuity counter `LLSUB8RECT444_CHROMA_WALK_HITS` + + `llsub8rect444_chroma_walk_hits()` + a `decode_probe` line, per the lane + convention (`CHROMA_SPLIT_TX_HITS` cannot attribute its bumps to this + leaf). 4:2:0 is arithmetically identical: the branch lives inside + `chroma_444`, and the non-lossless rect path and the 4:2:0 group tail are + byte-preserved. + +## Verification (measured, this tree @ HEAD + this change) + +- Entropy witness, `/tmp/llkf/t6.obu` vs fresh `aomdec` + (`$HOME/.cache/aom-oracle/build`), `EC_ECDUMP_IN` (value,rng) pairing: + pre-change the first mismatching read is #2326 — inside the named leaf's + chroma at mi(0,2). Post-change #2316–#2488 pair 1:1: the named leaf's FOUR + chroma units read exactly the oracle's symbols + ((30281,51429)/(20612,34312)/(33129,58376)/(30377,36104) against oracle + ctx 11≡4), the sibling INTER leaf mi(1,2) and everything after pairs, and + the walk counter fires (15 walks stream-wide, `decode_probe`). + Fail-before: with the rect read in place the leaf desynced at its own + chroma (t6 frame-1 first differing pixel byte 65, 16032 differing bytes). +- The stream still diverges — later and at a DIFFERENT block: first + mismatching read #2489, the U unit (2,8) of the 16x4 INTER strip at + mi(2,8): ours reads `txb_skip` ctx 3 (above[8][1]=0, left[2][1]=0) where + the oracle reads ctx 11 with raw cells `above=[0,] left=[23,]` + (`EC_ECDUMP`, the patched oracle's cell dump) — libaom's left[2][1] holds + level 7 / dc 0 from an earlier row-2 writer, ours holds 0. That cell is + written by the 16x16 intra block (0,4)'s chroma band stamping and the + (1,8) 16x4 strip's own tail — `decode_rect4_16_strip` is llcpred's lane. + With the desync now downstream, our decoder refuses honestly at the strip + (`a Golomb tail longer than this decoder reads`) instead of decoding + garbage, so per-frame byte counts are not claimable this round: **t6 + frames 1..5 are NOT exact — no exactness claimed.** Frame 0 is untouched + by construction (both callers, `decode_inter_sub8_split4` and + `decode_inter_sub8_rect2`, are inter-frame paths; the key frame never + reaches this arm) and was byte-exact before. +- Non-regression: `cargo test -p ec-av1 --lib a_lossless` → 7 passed, 0 + failed (both 4:2:0 lossless libaom gates, the 444 min-partition-64 and + min-partition-8 inter gates, the sb128 pair, the 16x4 pair). +- `cargo check -p ec-av1 --all-targets` (target dir + `$HOME/.cache/cargo-target-av1sub8rect444`): 0 warnings, 0 errors. + `rustfmt --check` reports no hunks inside the edited ranges (the crate's + pre-existing drift is untouched). +- Fixtures are scratch in /tmp/llkf (t6.obu, regenerable per + av1llkf.report.md's recipe) and /tmp/llsub8rect444; nothing committed + from them. + +## Named, not chased (spoken) + +1. The t6 next-site: the 16x4 INTER strip's chroma coefficient-context + stamping at 4:4:4 lossless. Measured above (ctx 3 vs 11 on U (2,8); + oracle cells `above=[0] left=[23]`). The row-2 U band state our tree + drops is written by the 16x16-intra/(0,4) stamping and consumed one + superblock later; `decode_rect4_16_strip` is llcpred's lane — per the + ticket this lane stops at the boundary. + deferred(the 16x4 strip's per-unit chroma context stamping at 4:4:4 + lossless; unblock: llcpred's decode_rect4_16_strip lane, t6.obu as the + ready-made fixture, first divergent read #2489 = U unit (2,8)). +2. Instrument parity: `read_coeffs_rect` still has no `EC_ECDUMP_IN` rung + (av1llintercdf item 2) — this lane's walk bypasses the rect reader + entirely, so the rung was not needed here.