forge
Explore
Log in

Don't you dare go Hollow

lane-av1-llintra: 444 lossless intra-side fixes, key-frame exact at 64x64, sub8 chroma stop named

Browse files at this commit

25a6b5f6cdfd3cc4da72012cab6f02e38258446dAhmet Kaan GümüşParent: 38b6919
- decode_leaf8: 444 lossless leaf chroma is a plane-major 2x2 raster of
  TX_4X4 units per plane (oracle av1_get_tx_size forces TX_4X4 on every
  lossless plane), not one Chroma8 unit whose lossless WHT is 4x4-only
  and panicked reconstruct (index 16 len 16); skip arm predicts the four
  units per-unit; per-unit record_mi_chroma replaces the whole-leaf
  chroma band stamp for that shape.
- read_intra_chroma_lossless: cu_mi divisor, tu_reach scaling and
  record_mi_chroma spans made subsampling-aware (byte-identical at 420).
- decode_block whole-block chroma unit clip: plane span and overhang
  made subsampling-aware; the hardcoded 420 span clipped three quarters
  of a 444 block's coded units and desynced the tile (the 64x64 key
  frame luma diff).
- cfl_allowed_px: lossless arm now mirrors is_cfl_allowed's
  plane_bsize == 4x4 rule, so a 444 lossless leaf reads the 13-symbol
  uv_mode CDF (wrong-alphabet-same-value).

Verified: dodge2 key frame byte-exact vs aomdec --rawvideo; ll444 key
frame no longer panics; a_lossless_libaom_inter_frame_decodes_sample_exact
green; cargo check -p ec-av1 --all-targets 0 warnings. Stop: ll444 key
frame still desyncs at the first sub8 leaf's chroma unit (sub8_leaf_
chroma444 square branch, symbol-level, site named in the report);
inter frames panic in reconstruct_mc_rect via decode_inter_block8
(different function, out of lane).
crates/ec-av1/src/decode.rsmodified+134 -17
@@ -486,7 +486,15 @@ pub(crate) fn lossless_tx(px: usize, fctx: &crate::decode::FrameCtx) -> usize {
486486 /// picks the 13-symbol one (class `wrong-alphabet-same-value`).
487487 pub(crate) fn cfl_allowed_px(bw: usize, bh: usize, fctx: &crate::decode::FrameCtx) -> bool {
488488 if lossless(fctx) {
489- bw <= 8 && bh <= 8
489+ // `is_cfl_allowed`: at lossless CfL is available only when the
490+ // partition size equals the transform size -- the CHROMA plane block
491+ // must be a single TX_4X4. True for an 8x8 partition at 4:2:0 (its
492+ // plane block is 4x4) and for 4x4 partitions; FALSE for an 8x8
493+ // partition at 4:4:4, whose plane block is 8x8 -- that partition
494+ // reads the 13-symbol uv_mode CDF, and reading the CFL alphabet
495+ // there desynced the tile from the leaf's own uv_mode symbol (class
496+ // `wrong-alphabet-same-value`).
497+ (bw >> ss_x(fctx)).max(4) == 4 && (bh >> ss_y(fctx)).max(4) == 4
490498 } else {
491499 bw <= 32 && bh <= 32
492500 }
@@ -17279,9 +17287,17 @@ fn decode_block(
1727917287 (2 * y.true_width.div_ceil(8)) as i32 - mi_c as i32 - span_mi as i32;
1728017288 let over_h_mi =
1728117289 (2 * y.true_height.div_ceil(8)) as i32 - mi_r as i32 - span_mi as i32;
17282- let plane_px = ((span_mi * 2).max(4)) as i32;
17283- let max_units_w = ((plane_px + 2 * over_w_mi.min(0)) >> 2).max(0) as usize;
17284- let max_units_h = ((plane_px + 2 * over_h_mi.min(0)) >> 2).max(0) as usize;
17290+ // `max_block_wide/high` in chroma pixels: the plane block's
17291+ // own span (`side` at 4:4:4, `side / 2` at 4:2:0 -- the old
17292+ // hardcoded `span_mi * 2` clipped three quarters of a 4:4:4
17293+ // block's coded units and desynced the tile) plus the
17294+ // negative overhang (`mb_to_*_edge >> (3 + ss)`), whose
17295+ // chroma-px scale is `MI >> ss` per mi.
17296+ let plane_px = (((span_mi * MI) >> ss_x(fctx)).max(4)) as i32;
17297+ let over_w_px = over_w_mi.min(0) * (MI >> ss_x(fctx)) as i32;
17298+ let over_h_px = over_h_mi.min(0) * (MI >> ss_y(fctx)) as i32;
17299+ let max_units_w = ((plane_px + over_w_px) >> 2).max(0) as usize;
17300+ let max_units_h = ((plane_px + over_h_px) >> 2).max(0) as usize;
1728517301 let unit = chroma_tx / 4; // TU side in 4x4 chroma units
1728617302 if cu_col * unit >= max_units_w || cu_row * unit >= max_units_h {
1728717303 hit!(CHROMA_EDGE_TU_CLIP_HITS);
@@ -18169,12 +18185,45 @@ fn decode_leaf8(
1816918185 );
1817018186 }
1817118187 let ac = alpha.map(|_| cfl_src(px, py, 8));
18188+ // 4:4:4 lossless: the leaf's chroma plane block is a 2x2 raster of
18189+ // TX_4X4 units (`av1_get_tx_size`: lossless forces TX_4X4 on EVERY
18190+ // plane), so a skipped leaf predicts per unit -- each from its own
18191+ // edges -- like the luma `SKIP_SPLIT_TX` loop above. CfL never fires
18192+ // here (`is_cfl_allowed` at lossless needs plane_bsize == 4x4; this
18193+ // chroma plane block is 8x8), and intrabc/palette keep the
18194+ // whole-plane-block prediction they already run.
18195+ let chroma_units_444 = chroma_444
18196+ && lossless(fctx)
18197+ && intrabc_bufs.is_none()
18198+ && palette_uv_bufs.is_none();
1817218199 if let Some((_, ub, _)) = &intrabc_bufs {
1817318200 set_palette_pred(ub.clone(), fctx);
1817418201 } else if let Some((ub, _)) = &palette_uv_bufs {
1817518202 set_palette_pred(ub.clone(), fctx);
1817618203 }
18177- push_intra(1,
18204+ if chroma_units_444 {
18205+ for cu_row in 0..2 {
18206+ for cu_col in 0..2 {
18207+ let cu_reach = tu_reach(
18208+ 8, 8, cu_col * 4, cu_row * 4, 4,
18209+ reach, px, py, y.width, y.height, fctx,
18210+ );
18211+ push_intra(1,
18212+ cpx + cu_col * 4,
18213+ cpy + cu_row * 4,
18214+ 4,
18215+ uv_predict_mode,
18216+ angle_delta_uv,
18217+ cu_reach,
18218+ &ZERO_RESIDUAL[..16],
18219+ None,
18220+ None,
18221+ smooth_neighbor_uv, fctx,
18222+ );
18223+ }
18224+ }
18225+ } else {
18226+ push_intra(1,
1817818227 cpx,
1817918228 cpy,
1818018229 csz,
@@ -18186,12 +18235,35 @@ fn decode_leaf8(
1818618235 None,
1818718236 smooth_neighbor_uv, fctx,
1818818237 );
18238+ }
1818918239 if let Some((_, _, vb)) = &intrabc_bufs {
1819018240 set_palette_pred(vb.clone(), fctx);
1819118241 } else if let Some((_, vb)) = &palette_uv_bufs {
1819218242 set_palette_pred(vb.clone(), fctx);
1819318243 }
18194- push_intra(2,
18244+ if chroma_units_444 {
18245+ for cu_row in 0..2 {
18246+ for cu_col in 0..2 {
18247+ let cu_reach = tu_reach(
18248+ 8, 8, cu_col * 4, cu_row * 4, 4,
18249+ reach, px, py, y.width, y.height, fctx,
18250+ );
18251+ push_intra(2,
18252+ cpx + cu_col * 4,
18253+ cpy + cu_row * 4,
18254+ 4,
18255+ uv_predict_mode,
18256+ angle_delta_uv,
18257+ cu_reach,
18258+ &ZERO_RESIDUAL[..16],
18259+ None,
18260+ None,
18261+ smooth_neighbor_uv, fctx,
18262+ );
18263+ }
18264+ }
18265+ } else {
18266+ push_intra(2,
1819518267 cpx,
1819618268 cpy,
1819718269 csz,
@@ -18203,6 +18275,7 @@ fn decode_leaf8(
1820318275 None,
1820418276 smooth_neighbor_uv, fctx,
1820518277 );
18278+ }
1820618279 luma_grid = Grid::Zero(64);
1820718280 u_grid = Grid::Zero(csz * csz);
1820818281 v_grid = Grid::Zero(csz * csz);
@@ -18416,6 +18489,28 @@ fn decode_leaf8(
1841618489 if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() {
1841718490 hit!(INTRABC_TX4_CHROMA_COPY_HITS);
1841818491 }
18492+ if chroma_444 && lossless(fctx) {
18493+ // 4:4:4 lossless: the leaf's chroma plane block is a 2x2 raster
18494+ // of TX_4X4 units per plane, PLANE-MAJOR (`av1_get_tx_size`:
18495+ // lossless forces TX_4X4 on EVERY plane) -- the same walk
18496+ // `read_intra_chroma_lossless` runs for whole blocks. Reading one
18497+ // TX_8X8 unit here panicked before: the lossless inverse is the
18498+ // 4x4-only WHT (`TxParams::run`'s own `(w, h) == (4, 4)`
18499+ // invariant), and the unit count desyncs the tile either way.
18500+ // Per-unit `record_mi_chroma` calls stamp the neighbour bands
18501+ // inside, which is why the tail's whole-leaf chroma stamp below
18502+ // is skipped for this shape.
18503+ let (mut uo, mut vo) = (Vec::new(), Vec::new());
18504+ read_intra_chroma_lossless(
18505+ dec, cdfs, neighbours, u, v, leaf_mi, (px, py), (cpx, cpy),
18506+ 8, (8, 8), (0, 0), (8, 8), 8, reach, (y.width, y.height), mode,
18507+ uv_predict_mode, angle_delta_uv,
18508+ palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])),
18509+ smooth_neighbor_uv, base_q_idx, &mut uo, &mut vo, fctx,
18510+ )?;
18511+ u_grid = if uo.is_empty() { Grid::Zero(64) } else { Grid::Own(uo) };
18512+ v_grid = if vo.is_empty() { Grid::Zero(64) } else { Grid::Own(vo) };
18513+ } else {
1841918514 if let Some((ub, _)) = &palette_uv_bufs {
1842018515 set_palette_pred(ub.clone(), fctx);
1842118516 }
@@ -18466,6 +18561,7 @@ fn decode_leaf8(
1846618561 None,
1846718562 smooth_neighbor_uv, fctx,
1846818563 )?;
18564+ }
1846918565 // The two chroma reads above are the only consulters of this leaf's
1847018566 // armed [`INTRABC_CHROMA_TX`]; leaving it set would make the NEXT
1847118567 // leaf's chroma inherit this leaf's luma type instead of its own
@@ -18493,14 +18589,21 @@ fn decode_leaf8(
1849318589 let bound_w = round_up_even(neighbours.mi_cols);
1849418590 let u_state = neighbour_state(&u_grid);
1849518591 let v_state = neighbour_state(&v_grid);
18496- for cell in 0..side_mi {
18497- if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {
18498- neighbours.left[leaf_mi.0 + cell][1] = u_state;
18499- neighbours.left[leaf_mi.0 + cell][2] = v_state;
18500- }
18501- if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {
18502- neighbours.above[leaf_mi.1 + cell][1] = u_state;
18503- neighbours.above[leaf_mi.1 + cell][2] = v_state;
18592+ // The 4:4:4 lossless walk above already stamped these bands PER UNIT
18593+ // (its raster-order last writer per cell is exactly libaom's
18594+ // per-entry last update); this whole-leaf aggregate would clobber
18595+ // four distinct unit states with one, so it runs only for the shapes
18596+ // whose chroma really is one unit.
18597+ if !(chroma_444 && lossless(fctx)) {
18598+ for cell in 0..side_mi {
18599+ if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {
18600+ neighbours.left[leaf_mi.0 + cell][1] = u_state;
18601+ neighbours.left[leaf_mi.0 + cell][2] = v_state;
18602+ }
18603+ if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {
18604+ neighbours.above[leaf_mi.1 + cell][1] = u_state;
18605+ neighbours.above[leaf_mi.1 + cell][2] = v_state;
18606+ }
1850418607 }
1850518608 }
1850618609 // This leaf's own palette state into the mi-granular above/left bands
@@ -28390,7 +28493,13 @@ fn read_intra_chroma_lossless(
2839028493 if cpx + ox >= u.true_width || cpy + oy >= u.true_height {
2839128494 continue;
2839228495 }
28393- let cu_mi = (at_mi.0 + oy / 2, at_mi.1 + ox / 2);
28496+ // Chroma pixels per mi cell: 2 at 4:2:0, 4 at 4:4:4. At
28497+ // 4:2:0 a TX_4X4 chroma unit spans two mi cells and anchors
28498+ // at the even one; at 4:4:4 it IS one mi cell.
28499+ let cu_mi = (
28500+ at_mi.0 + oy / (MI >> ss_y(fctx)),
28501+ at_mi.1 + ox / (MI >> ss_x(fctx)),
28502+ );
2839428503 // libaom's chroma entropy-context arrays are SUBSAMPLED:
2839528504 // `get_txb_ctx` reads exactly one entry per plane for a
2839628505 // TX_4X4 chroma unit (`a[0]`/`l[0]`). Our bands are luma-mi
@@ -28399,8 +28508,12 @@ fn read_intra_chroma_lossless(
2839928508 // cell is past the band on a unit whose block hangs off the
2840028509 // frame edge, which libaom still codes.
2840128510 let cu_around = neighbours.around_mi(cu_mi, MI);
28511+ // The unit's origin and extent in LUMA pixels: `<< ss` at
28512+ // 4:2:0 (8x8 luma for a 4x4 chroma unit), identity at 4:4:4.
2840228513 let cu_reach = tu_reach(
28403- side, side, ox * 2, oy * 2, 8, reach, px, py, frame_w, frame_h, fctx,
28514+ side, side,
28515+ ox << ss_x(fctx), oy << ss_y(fctx), 4 << ss_x(fctx),
28516+ reach, px, py, frame_w, frame_h, fctx,
2840428517 );
2840528518 if let Some((ub, vb)) = palette_uv_bufs {
2840628519 let buf = if plane_idx == 1 { ub } else { vb };
@@ -28431,7 +28544,11 @@ fn read_intra_chroma_lossless(
2843128544 smooth_neighbor_uv,
2843228545 fctx,
2843328546 )?;
28434- neighbours.record_mi_chroma(cu_mi, 8, 8, plane_idx, &cu_grid);
28547+ // The unit's own footprint on the LUMA mi grid: 8 luma px
28548+ // (two cells) at 4:2:0, 4 (one cell) at 4:4:4.
28549+ neighbours.record_mi_chroma(
28550+ cu_mi, 4 << ss_x(fctx), 4 << ss_y(fctx), plane_idx, &cu_grid,
28551+ );
2843528552 hit!(CHROMA_SPLIT_TX_HITS);
2843628553 let dst = mu_units(
2843728554 if plane_idx == 1 { &mut *u_out } else { &mut *v_out },
lanes/av1llintra.report.mdadded+93 -0
@@ -0,0 +1,93 @@
1+# lane-av1-llintra: 4:4:4 lossless intra side (class 444-lossless-intra-side)
2+
3+## Charter
4+
5+Fix the 4:4:4 lossless intra-side panic (decode.rs:3766, `index 16 len 16`, via
6+`decode_leaf8 -> read_plane -> exec_intra`) and the same-class 64x64 lossless
7+intra luma mis-decode, or stop at the first site that cannot be finished.
8+
9+## Repro (fail-before)
10+
11+`aomenc profile 1 --lossless=1`, testsrc2 128x96 yuv444p, 6 frames, palette and
12+intrabc off, cdef/restoration off (`ll444.obu` = default partitions,
13+`dodge2.obu` = `--sb-size=64 --max/min-partition-size=64`):
14+
15+- `ll444.obu`: PANIC decode.rs:3766 (`reconstruct`, index 16 len 16) on the KEY
16+ frame, `decode_leaf8 -> read_plane -> exec_intra` (measured, RUST_BACKTRACE).
17+- `dodge2.obu`: key frame decoded but luma pixel-diffed the oracle from byte 65
18+ (31729 of 36864).
19+
20+## Root causes and fixes (all in decode.rs)
21+
22+1. **Leaf8 chroma is one TX_8X8 unit at 4:4:4 lossless** (the panic). Oracle
23+ `av1_get_tx_size`: `if (xd->lossless[..]) return TX_4X4;` for EVERY plane, so
24+ the leaf's 8x8 chroma plane block is a plane-major 2x2 raster of TX_4X4
25+ units. Our `TxbSet::Chroma8` single unit hit `TxParams::run`'s lossless WHT,
26+ which is 4x4-only (`debug_assert_eq!((w, h), (4, 4))`), silently producing a
27+ 16-coeff residual for a side-8 reconstruct. Fixed: in `decode_leaf8`'s TX4
28+ arm, `chroma_444 && lossless` routes through `read_intra_chroma_lossless`
29+ (per-unit reads + per-unit `record_mi_chroma`); the tail's whole-leaf chroma
30+ band stamp is skipped for that shape (raster-order last writer per band cell
31+ is exactly libaom's per-entry last update). In the skip arm, the 4:4:4
32+ lossless leaf predicts four TX_4X4 chroma units (per-unit edges), like the
33+ luma `SKIP_SPLIT_TX` loop; intrabc/palette keep the whole-plane-block
34+ prediction.
35+2. **`read_intra_chroma_lossless` carried 4:2:0 math**: `cu_mi = at_mi + oy/2`,
36+ `tu_reach(.., ox * 2, oy * 2, 8, ..)`, `record_mi_chroma(.., 8, 8, ..)`.
37+ Made subsampling-aware (`MI >> ss` divisors, `<< ss` scaling, `4 << ss`
38+ spans) -- byte-identical at 4:2:0, correct at 4:4:4 (one mi cell per unit).
39+3. **Whole-block chroma unit clip was a 4:2:0-sized raster** (`plane_px =
40+ span_mi * 2`, overhang `2 * over_mi`): at 4:4:4 it clipped three quarters of
41+ each 64x64 block's coded units and desynced the tile -- this was the dodge2
42+ key frame's luma diff from byte 65 (the desynced chroma garbage-fed the
43+ following superblocks' luma). Fixed to `span_mi * MI >> ss` and `over_mi *
44+ (MI >> ss)`, libaom `max_block_wide/high`.
45+4. **Lossless CFL alphabet gate**: oracle `is_cfl_allowed` at lossless requires
46+ `plane_bsize == BLOCK_4X4` (TRUE for an 8x8 partition at 4:2:0, FALSE at
47+ 4:4:4). Our `cfl_allowed_px` lossless arm accepted any <=8x8, so a 4:4:4
48+ lossless leaf read the 14-symbol `uv_mode_cfl` CDF where the stream carries
49+ the 13-symbol one (class `wrong-alphabet-same-value`). Fixed to
50+ `(bw >> ss_x).max(4) == 4 && (bh >> ss_y).max(4) == 4`; 4:2:0 identities
51+ unchanged.
52+
53+## Verification (measured)
54+
55+- `ll444.obu` no longer panics on the key frame; the key frame decodes.
56+- `dodge2.obu` KEY FRAME: byte-exact vs `aomdec --rawvideo` (first diff at byte
57+ 37493, i.e. inside inter frame 1).
58+- 4:2:0 regression gate `a_lossless_libaom_inter_frame_decodes_sample_exact`:
59+ green.
60+- `cargo check -p ec-av1 --all-targets`: 0 warnings, 0 errors.
61+
62+## STOP -- named, with the measured site
63+
64+`ll444.obu`'s key frame is NOT yet pixel-exact: 36254 of 36864 bytes differ
65+from byte 19. Localized with the trace pair (ours `EC_AV1_TRACE`, oracle
66+`EC_TRACE_MODE_STEP` + `EC_DQCOEFF`): the structural read order matches the
67+oracle exactly (per-4x4-leaf plane-interleaved luma,U,V; luma unit 0 of the
68+first sub8 group is byte-exact), but the FIRST chroma unit (`plane 1 @(0,0)`,
69+`TxbSet::Chroma4`, TX_4X4) decodes near-miss coefficients from scan position 4
70+(e.g. ours `2:-8` vs oracle `2:-4`, ours `6:4` vs oracle `9:4`) with every
71+classical context input equal (bands all zero at frame start, `txb_skip_ctx`
72+base 7 both sides, DCT_DCT forced, same scan). Site: `sub8_leaf_chroma444`'s
73+square branch (decode.rs read_plane calls around 18935) -- the divergence is a
74+symbol-level CDF-row input unique to lossless chroma at 4x4 that the current
75+ctx arguments do not capture. Next step: oracle-paired token trace
76+(`EC_TRACE_MODE_STEP`'s `EC_COEFF` rng stream vs an rng/tell dump inside
77+`read_coeffs`) to name the first divergent symbol, then diff its CDF row
78+selection.
79+
80+Per charter, stopped there. Frames 1+ of BOTH streams panic in
81+`reconstruct_mc_rect` (decode.rs:28328 post-insert, was 28233) via the
82+`decode_inter_block8` inter path -- a different function from anything this
83+lane changed; that is the charter's named stop for the inter side, and
84+`read_inter_chroma_lossless` remains untouched (another report owns its /2
85+design).
86+
87+## Deferred (spoken, not hidden)
88+
89+- `decode_leaf8`'s intrabc chroma prediction buffers are hardcoded 4x4
90+ (18074-18085), so an intrabc 8x8 leaf at 4:4:4 predicts 16 samples for a
91+ 64-sample reconstruct. Unreachable with `--enable-intrabc=0` fixtures;
92+ deferred(fix-now after the inter side is green, with an intrabc 4:4:4
93+ fixture).