diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index cf2f181e..5eef3725 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -486,7 +486,15 @@ pub(crate) fn lossless_tx(px: usize, fctx: &crate::decode::FrameCtx) -> usize { /// picks the 13-symbol one (class `wrong-alphabet-same-value`). pub(crate) fn cfl_allowed_px(bw: usize, bh: usize, fctx: &crate::decode::FrameCtx) -> bool { if lossless(fctx) { - bw <= 8 && bh <= 8 + // `is_cfl_allowed`: at lossless CfL is available only when the + // partition size equals the transform size -- the CHROMA plane block + // must be a single TX_4X4. True for an 8x8 partition at 4:2:0 (its + // plane block is 4x4) and for 4x4 partitions; FALSE for an 8x8 + // partition at 4:4:4, whose plane block is 8x8 -- that partition + // reads the 13-symbol uv_mode CDF, and reading the CFL alphabet + // there desynced the tile from the leaf's own uv_mode symbol (class + // `wrong-alphabet-same-value`). + (bw >> ss_x(fctx)).max(4) == 4 && (bh >> ss_y(fctx)).max(4) == 4 } else { bw <= 32 && bh <= 32 } @@ -17279,9 +17287,17 @@ fn decode_block( (2 * y.true_width.div_ceil(8)) as i32 - mi_c as i32 - span_mi as i32; let over_h_mi = (2 * y.true_height.div_ceil(8)) as i32 - mi_r as i32 - span_mi as i32; - let plane_px = ((span_mi * 2).max(4)) as i32; - let max_units_w = ((plane_px + 2 * over_w_mi.min(0)) >> 2).max(0) as usize; - let max_units_h = ((plane_px + 2 * over_h_mi.min(0)) >> 2).max(0) as usize; + // `max_block_wide/high` in chroma pixels: the plane block's + // own span (`side` at 4:4:4, `side / 2` at 4:2:0 -- the old + // hardcoded `span_mi * 2` clipped three quarters of a 4:4:4 + // block's coded units and desynced the tile) plus the + // negative overhang (`mb_to_*_edge >> (3 + ss)`), whose + // chroma-px scale is `MI >> ss` per mi. + let plane_px = (((span_mi * MI) >> ss_x(fctx)).max(4)) as i32; + let over_w_px = over_w_mi.min(0) * (MI >> ss_x(fctx)) as i32; + let over_h_px = over_h_mi.min(0) * (MI >> ss_y(fctx)) as i32; + let max_units_w = ((plane_px + over_w_px) >> 2).max(0) as usize; + let max_units_h = ((plane_px + over_h_px) >> 2).max(0) as usize; let unit = chroma_tx / 4; // TU side in 4x4 chroma units if cu_col * unit >= max_units_w || cu_row * unit >= max_units_h { hit!(CHROMA_EDGE_TU_CLIP_HITS); @@ -18169,12 +18185,45 @@ fn decode_leaf8( ); } let ac = alpha.map(|_| cfl_src(px, py, 8)); + // 4:4:4 lossless: the leaf's chroma plane block is a 2x2 raster of + // TX_4X4 units (`av1_get_tx_size`: lossless forces TX_4X4 on EVERY + // plane), so a skipped leaf predicts per unit -- each from its own + // edges -- like the luma `SKIP_SPLIT_TX` loop above. CfL never fires + // here (`is_cfl_allowed` at lossless needs plane_bsize == 4x4; this + // chroma plane block is 8x8), and intrabc/palette keep the + // whole-plane-block prediction they already run. + let chroma_units_444 = chroma_444 + && lossless(fctx) + && intrabc_bufs.is_none() + && palette_uv_bufs.is_none(); if let Some((_, ub, _)) = &intrabc_bufs { set_palette_pred(ub.clone(), fctx); } else if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - push_intra(1, + if chroma_units_444 { + for cu_row in 0..2 { + for cu_col in 0..2 { + let cu_reach = tu_reach( + 8, 8, cu_col * 4, cu_row * 4, 4, + reach, px, py, y.width, y.height, fctx, + ); + push_intra(1, + cpx + cu_col * 4, + cpy + cu_row * 4, + 4, + uv_predict_mode, + angle_delta_uv, + cu_reach, + &ZERO_RESIDUAL[..16], + None, + None, + smooth_neighbor_uv, fctx, + ); + } + } + } else { + push_intra(1, cpx, cpy, csz, @@ -18186,12 +18235,35 @@ fn decode_leaf8( None, smooth_neighbor_uv, fctx, ); + } if let Some((_, _, vb)) = &intrabc_bufs { set_palette_pred(vb.clone(), fctx); } else if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - push_intra(2, + if chroma_units_444 { + for cu_row in 0..2 { + for cu_col in 0..2 { + let cu_reach = tu_reach( + 8, 8, cu_col * 4, cu_row * 4, 4, + reach, px, py, y.width, y.height, fctx, + ); + push_intra(2, + cpx + cu_col * 4, + cpy + cu_row * 4, + 4, + uv_predict_mode, + angle_delta_uv, + cu_reach, + &ZERO_RESIDUAL[..16], + None, + None, + smooth_neighbor_uv, fctx, + ); + } + } + } else { + push_intra(2, cpx, cpy, csz, @@ -18203,6 +18275,7 @@ fn decode_leaf8( None, smooth_neighbor_uv, fctx, ); + } luma_grid = Grid::Zero(64); u_grid = Grid::Zero(csz * csz); v_grid = Grid::Zero(csz * csz); @@ -18416,6 +18489,28 @@ fn decode_leaf8( if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { hit!(INTRABC_TX4_CHROMA_COPY_HITS); } + if chroma_444 && lossless(fctx) { + // 4:4:4 lossless: the leaf's chroma plane block is a 2x2 raster + // of TX_4X4 units per plane, PLANE-MAJOR (`av1_get_tx_size`: + // lossless forces TX_4X4 on EVERY plane) -- the same walk + // `read_intra_chroma_lossless` runs for whole blocks. Reading one + // TX_8X8 unit here panicked before: the lossless inverse is the + // 4x4-only WHT (`TxParams::run`'s own `(w, h) == (4, 4)` + // invariant), and the unit count desyncs the tile either way. + // Per-unit `record_mi_chroma` calls stamp the neighbour bands + // inside, which is why the tail's whole-leaf chroma stamp below + // is skipped for this shape. + let (mut uo, mut vo) = (Vec::new(), Vec::new()); + read_intra_chroma_lossless( + dec, cdfs, neighbours, u, v, leaf_mi, (px, py), (cpx, cpy), + 8, (8, 8), (0, 0), (8, 8), 8, reach, (y.width, y.height), mode, + uv_predict_mode, angle_delta_uv, + palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])), + smooth_neighbor_uv, base_q_idx, &mut uo, &mut vo, fctx, + )?; + u_grid = if uo.is_empty() { Grid::Zero(64) } else { Grid::Own(uo) }; + v_grid = if vo.is_empty() { Grid::Zero(64) } else { Grid::Own(vo) }; + } else { if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } @@ -18466,6 +18561,7 @@ fn decode_leaf8( None, smooth_neighbor_uv, fctx, )?; + } // The two chroma reads above are the only consulters of this leaf's // armed [`INTRABC_CHROMA_TX`]; leaving it set would make the NEXT // leaf's chroma inherit this leaf's luma type instead of its own @@ -18493,14 +18589,21 @@ fn decode_leaf8( let bound_w = round_up_even(neighbours.mi_cols); let u_state = neighbour_state(&u_grid); let v_state = neighbour_state(&v_grid); - for cell in 0..side_mi { - if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { - neighbours.left[leaf_mi.0 + cell][1] = u_state; - neighbours.left[leaf_mi.0 + cell][2] = v_state; - } - if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { - neighbours.above[leaf_mi.1 + cell][1] = u_state; - neighbours.above[leaf_mi.1 + cell][2] = v_state; + // The 4:4:4 lossless walk above already stamped these bands PER UNIT + // (its raster-order last writer per cell is exactly libaom's + // per-entry last update); this whole-leaf aggregate would clobber + // four distinct unit states with one, so it runs only for the shapes + // whose chroma really is one unit. + if !(chroma_444 && lossless(fctx)) { + for cell in 0..side_mi { + if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { + neighbours.left[leaf_mi.0 + cell][1] = u_state; + neighbours.left[leaf_mi.0 + cell][2] = v_state; + } + if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { + neighbours.above[leaf_mi.1 + cell][1] = u_state; + neighbours.above[leaf_mi.1 + cell][2] = v_state; + } } } // This leaf's own palette state into the mi-granular above/left bands @@ -28390,7 +28493,13 @@ fn read_intra_chroma_lossless( if cpx + ox >= u.true_width || cpy + oy >= u.true_height { continue; } - let cu_mi = (at_mi.0 + oy / 2, at_mi.1 + ox / 2); + // Chroma pixels per mi cell: 2 at 4:2:0, 4 at 4:4:4. At + // 4:2:0 a TX_4X4 chroma unit spans two mi cells and anchors + // at the even one; at 4:4:4 it IS one mi cell. + let cu_mi = ( + at_mi.0 + oy / (MI >> ss_y(fctx)), + at_mi.1 + ox / (MI >> ss_x(fctx)), + ); // libaom's chroma entropy-context arrays are SUBSAMPLED: // `get_txb_ctx` reads exactly one entry per plane for a // TX_4X4 chroma unit (`a[0]`/`l[0]`). Our bands are luma-mi @@ -28399,8 +28508,12 @@ fn read_intra_chroma_lossless( // cell is past the band on a unit whose block hangs off the // frame edge, which libaom still codes. let cu_around = neighbours.around_mi(cu_mi, MI); + // The unit's origin and extent in LUMA pixels: `<< ss` at + // 4:2:0 (8x8 luma for a 4x4 chroma unit), identity at 4:4:4. let cu_reach = tu_reach( - side, side, ox * 2, oy * 2, 8, reach, px, py, frame_w, frame_h, fctx, + side, side, + ox << ss_x(fctx), oy << ss_y(fctx), 4 << ss_x(fctx), + reach, px, py, frame_w, frame_h, fctx, ); if let Some((ub, vb)) = palette_uv_bufs { let buf = if plane_idx == 1 { ub } else { vb }; @@ -28431,7 +28544,11 @@ fn read_intra_chroma_lossless( smooth_neighbor_uv, fctx, )?; - neighbours.record_mi_chroma(cu_mi, 8, 8, plane_idx, &cu_grid); + // The unit's own footprint on the LUMA mi grid: 8 luma px + // (two cells) at 4:2:0, 4 (one cell) at 4:4:4. + neighbours.record_mi_chroma( + cu_mi, 4 << ss_x(fctx), 4 << ss_y(fctx), plane_idx, &cu_grid, + ); hit!(CHROMA_SPLIT_TX_HITS); let dst = mu_units( if plane_idx == 1 { &mut *u_out } else { &mut *v_out }, diff --git a/lanes/av1llintra.report.md b/lanes/av1llintra.report.md new file mode 100644 index 00000000..c2b73837 --- /dev/null +++ b/lanes/av1llintra.report.md @@ -0,0 +1,93 @@ +# lane-av1-llintra: 4:4:4 lossless intra side (class 444-lossless-intra-side) + +## Charter + +Fix the 4:4:4 lossless intra-side panic (decode.rs:3766, `index 16 len 16`, via +`decode_leaf8 -> read_plane -> exec_intra`) and the same-class 64x64 lossless +intra luma mis-decode, or stop at the first site that cannot be finished. + +## Repro (fail-before) + +`aomenc profile 1 --lossless=1`, testsrc2 128x96 yuv444p, 6 frames, palette and +intrabc off, cdef/restoration off (`ll444.obu` = default partitions, +`dodge2.obu` = `--sb-size=64 --max/min-partition-size=64`): + +- `ll444.obu`: PANIC decode.rs:3766 (`reconstruct`, index 16 len 16) on the KEY + frame, `decode_leaf8 -> read_plane -> exec_intra` (measured, RUST_BACKTRACE). +- `dodge2.obu`: key frame decoded but luma pixel-diffed the oracle from byte 65 + (31729 of 36864). + +## Root causes and fixes (all in decode.rs) + +1. **Leaf8 chroma is one TX_8X8 unit at 4:4:4 lossless** (the panic). Oracle + `av1_get_tx_size`: `if (xd->lossless[..]) return TX_4X4;` for EVERY plane, so + the leaf's 8x8 chroma plane block is a plane-major 2x2 raster of TX_4X4 + units. Our `TxbSet::Chroma8` single unit hit `TxParams::run`'s lossless WHT, + which is 4x4-only (`debug_assert_eq!((w, h), (4, 4))`), silently producing a + 16-coeff residual for a side-8 reconstruct. Fixed: in `decode_leaf8`'s TX4 + arm, `chroma_444 && lossless` routes through `read_intra_chroma_lossless` + (per-unit reads + per-unit `record_mi_chroma`); the tail's whole-leaf chroma + band stamp is skipped for that shape (raster-order last writer per band cell + is exactly libaom's per-entry last update). In the skip arm, the 4:4:4 + lossless leaf predicts four TX_4X4 chroma units (per-unit edges), like the + luma `SKIP_SPLIT_TX` loop; intrabc/palette keep the whole-plane-block + prediction. +2. **`read_intra_chroma_lossless` carried 4:2:0 math**: `cu_mi = at_mi + oy/2`, + `tu_reach(.., ox * 2, oy * 2, 8, ..)`, `record_mi_chroma(.., 8, 8, ..)`. + Made subsampling-aware (`MI >> ss` divisors, `<< ss` scaling, `4 << ss` + spans) -- byte-identical at 4:2:0, correct at 4:4:4 (one mi cell per unit). +3. **Whole-block chroma unit clip was a 4:2:0-sized raster** (`plane_px = + span_mi * 2`, overhang `2 * over_mi`): at 4:4:4 it clipped three quarters of + each 64x64 block's coded units and desynced the tile -- this was the dodge2 + key frame's luma diff from byte 65 (the desynced chroma garbage-fed the + following superblocks' luma). Fixed to `span_mi * MI >> ss` and `over_mi * + (MI >> ss)`, libaom `max_block_wide/high`. +4. **Lossless CFL alphabet gate**: oracle `is_cfl_allowed` at lossless requires + `plane_bsize == BLOCK_4X4` (TRUE for an 8x8 partition at 4:2:0, FALSE at + 4:4:4). Our `cfl_allowed_px` lossless arm accepted any <=8x8, so a 4:4:4 + lossless leaf read the 14-symbol `uv_mode_cfl` CDF where the stream carries + the 13-symbol one (class `wrong-alphabet-same-value`). Fixed to + `(bw >> ss_x).max(4) == 4 && (bh >> ss_y).max(4) == 4`; 4:2:0 identities + unchanged. + +## Verification (measured) + +- `ll444.obu` no longer panics on the key frame; the key frame decodes. +- `dodge2.obu` KEY FRAME: byte-exact vs `aomdec --rawvideo` (first diff at byte + 37493, i.e. inside inter frame 1). +- 4:2:0 regression gate `a_lossless_libaom_inter_frame_decodes_sample_exact`: + green. +- `cargo check -p ec-av1 --all-targets`: 0 warnings, 0 errors. + +## STOP -- named, with the measured site + +`ll444.obu`'s key frame is NOT yet pixel-exact: 36254 of 36864 bytes differ +from byte 19. Localized with the trace pair (ours `EC_AV1_TRACE`, oracle +`EC_TRACE_MODE_STEP` + `EC_DQCOEFF`): the structural read order matches the +oracle exactly (per-4x4-leaf plane-interleaved luma,U,V; luma unit 0 of the +first sub8 group is byte-exact), but the FIRST chroma unit (`plane 1 @(0,0)`, +`TxbSet::Chroma4`, TX_4X4) decodes near-miss coefficients from scan position 4 +(e.g. ours `2:-8` vs oracle `2:-4`, ours `6:4` vs oracle `9:4`) with every +classical context input equal (bands all zero at frame start, `txb_skip_ctx` +base 7 both sides, DCT_DCT forced, same scan). Site: `sub8_leaf_chroma444`'s +square branch (decode.rs read_plane calls around 18935) -- the divergence is a +symbol-level CDF-row input unique to lossless chroma at 4x4 that the current +ctx arguments do not capture. Next step: oracle-paired token trace +(`EC_TRACE_MODE_STEP`'s `EC_COEFF` rng stream vs an rng/tell dump inside +`read_coeffs`) to name the first divergent symbol, then diff its CDF row +selection. + +Per charter, stopped there. Frames 1+ of BOTH streams panic in +`reconstruct_mc_rect` (decode.rs:28328 post-insert, was 28233) via the +`decode_inter_block8` inter path -- a different function from anything this +lane changed; that is the charter's named stop for the inter side, and +`read_inter_chroma_lossless` remains untouched (another report owns its /2 +design). + +## Deferred (spoken, not hidden) + +- `decode_leaf8`'s intrabc chroma prediction buffers are hardcoded 4x4 + (18074-18085), so an intrabc 8x8 leaf at 4:4:4 predicts 16 samples for a + 64-sample reconstruct. Unreachable with `--enable-intrabc=0` fixtures; + deferred(fix-now after the inter side is green, with an intrabc 4:4:4 + fixture).