forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-lossless2' (84d10bc8): lossless inter frames decode; the key-frame path fixed for textured chroma (plane-major units, per-unit contexts, 4x4 luma under sub-8x8 leaves, per-unit palette window)

Browse files at this commit

afd4958b221cb15e7278271783267c668c53d852Ahmet Kaan GümüşParent: afec53fc84128d
Six libaom -crf 0 fixtures decode sample-exact. The deciding gate's
libaom crf-5 screen point now decodes with one wrong luma sample, and
lossless inter frames of screen content still desync (localised).
crates/ec-av1/src/decode.rsmodified+398 -27
@@ -2099,6 +2099,18 @@ pub(crate) fn intra_in_inter_split_tx_hits() -> [usize; 4] {
20992099 /// (`set`) / consumes it (`take`). Set and take MUST pair: a buffer armed at a
21002100 /// site whose reconstruct never runs would be picked up by the NEXT block's
21012101 /// reconstruct (class [[refusal-hides-a-defect]] / stale thread-local), so
2102+/// lane-lossless2: one transform unit's window on a block-sized palette
2103+/// prediction buffer. A unit reads its prediction at ITS OWN stride, so
2104+/// handing it the whole-block buffer makes every unit reconstruct the block's
2105+/// top-left corner (the luma split-transform paths already window; the chroma
2106+/// ones did not, which is invisible until a lossless frame codes a palette
2107+/// block as 4x4 chroma units).
2108+fn palette_window(buf: &[u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize) -> Vec<u16> {
2109+ (0..h)
2110+ .flat_map(|r| buf[(oy + r) * stride + ox..][..w].iter().copied())
2111+ .collect()
2112+}
2113+
21022114 /// `EC_DEBUG_PAL=1` traces every arm and every consumption with its call site.
21032115 #[track_caller]
21042116 fn set_palette_pred(buf: Vec<u16>, fctx: &crate::decode::FrameCtx) {
@@ -6172,7 +6184,7 @@ fn read_coeffs(
61726184 if coeff_trace_on() {
61736185 let (rng, _) = dec.debug_state();
61746186 eprintln!(
6175- "EC_COEFF_STEP tag=all_zero side={} ctx={skip_ctx} cdf={dbg_txbskip:?} all_zero={} rng={rng}",
6187+ "EC_COEFF_STEP tag=all_zero side={} ctx={skip_ctx} dcctx={sign_ctx} cdf={dbg_txbskip:?} all_zero={} rng={rng}",
61766188 coding.side,
61776189 all_zero as i32
61786190 );
@@ -9546,6 +9558,13 @@ fn decode_rect_split(
95469558 }
95479559 }
95489560 // CHROMA: one un-split rect transform for the whole strip.
9561+ // lane-lossless2: except on a lossless frame, where it is a raster of 4x4
9562+ // units -- each stamps its own neighbour context as it is read, and the
9563+ // whole-block `record_split_luma_rect_mi` below would overwrite all of
9564+ // them with the LAST unit's state (class `override-slot-on-one-arm`: the
9565+ // next block then read `dc_sign_ctx` 1 where libaom reads 0). They are
9566+ // collected here and replayed after that record.
9567+ let mut ll_chroma_units: Vec<((usize, usize), usize, Grid)> = Vec::new();
95499568 let ac = m.alpha.map(|_| cfl_src_rect(px, py, bw, bh));
95509569 let reach = block_reach;
95519570 let (u_grid, v_grid) = if m.skip {
@@ -9594,8 +9613,9 @@ fn decode_rect_split(
95949613 );
95959614 let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4);
95969615 if let Some((ub, vb)) = &m.palette_uv {
9616+ let pbuf = if plane_idx == 1 { ub } else { vb };
95979617 set_palette_pred(
9598- if plane_idx == 1 { ub.clone() } else { vb.clone() },
9618+ palette_window(pbuf, chroma_w, cu_col * 4, cu_row * 4, 4, 4),
95999619 fctx,
96009620 );
96019621 }
@@ -9611,6 +9631,7 @@ fn decode_rect_split(
96119631 None, offset, m.smooth_neighbor_uv, fctx,
96129632 )?;
96139633 neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_idx, &grid);
9634+ ll_chroma_units.push((cu_mi, plane_idx, grid.clone()));
96149635 last[plane_idx - 1] = grid;
96159636 }
96169637 }
@@ -9695,6 +9716,9 @@ fn decode_rect_split(
96959716 m.uv_predict_mode,
96969717 [&u_grid, &v_grid],
96979718 );
9719+ for (cu_mi, plane, unit) in &ll_chroma_units {
9720+ neighbours.record_mi_chroma(*cu_mi, 8, 8, *plane, unit);
9721+ }
96989722 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, m.skip);
96999723 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx);
97009724 if crate::envflags::env_flag!("EC_AV1_TRACE") {
@@ -14455,6 +14479,16 @@ fn decode_block(
1445514479 // identical at every size up to 64x64, but a 128x128 block's 64x64 uv
1445614480 // plane block adjusts TX_64X64 down to TX_32X32, so each plane is
1445714481 // FOUR units (lane-sb128b r2) -- one per mu chunk.
14482+ // libaom `decode_token_recon_block` codes a mu chunk PLANE-MAJOR
14483+ // (`for (plane..) for (blk_row..) for (blk_col..)`): every U unit of
14484+ // the chunk, then every V unit. At one unit per plane per chunk --
14485+ // every non-lossless case this decoder reaches -- that is the same
14486+ // symbol order as the interleaved pair this loop used to read; a
14487+ // lossless frame's 4x4 chroma units are the case where it differs
14488+ // (lane-lossless2).
14489+ let mut u_pass: Vec<Grid> = Vec::new();
14490+ for plane_pass in 1..=2usize {
14491+ let mut pass_idx = 0usize;
1445814492 for cu_row in ch_row * cpc..(ch_row + 1) * cpc {
1445914493 for cu_col in ch_col * cpc..(ch_col + 1) * cpc {
1446014494 // The chroma context arrays are indexed in LUMA mi, so this
@@ -14476,24 +14510,25 @@ fn decode_block(
1447614510 )
1447714511 };
1447814512 let (cu_x, cu_y) = (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx);
14479- if let Some((ub, _)) = &palette_uv_bufs {
14480- set_palette_pred(ub.clone(), fctx);
14481- }
14482- let u_grid = read_plane(
14483- dec, cdfs, chroma_set, scans.1, 1, cu_around[1], mode,
14484- uv_predict_mode, angle_delta_uv, cu_reach, u, cu_x, cu_y,
14485- chroma_tx, chroma_tx, base_q_idx,
14486- alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
14487- None, chroma_ctx_offset, smooth_neighbor_uv, fctx,
14488- )?;
14489- if let Some((_, vb)) = &palette_uv_bufs {
14490- set_palette_pred(vb.clone(), fctx);
14513+ if let Some((ub, vb)) = &palette_uv_bufs {
14514+ let pbuf = if plane_pass == 1 { ub } else { vb };
14515+ set_palette_pred(
14516+ palette_window(
14517+ pbuf, chroma_side, cu_col * chroma_tx, cu_row * chroma_tx,
14518+ chroma_tx, chroma_tx,
14519+ ),
14520+ fctx,
14521+ );
1449114522 }
14492- let v_grid = read_plane(
14493- dec, cdfs, chroma_set, scans.1, 2, cu_around[2], mode,
14494- uv_predict_mode, angle_delta_uv, cu_reach, v, cu_x, cu_y,
14523+ let buf: &mut PlaneBuf<'static> =
14524+ if plane_pass == 1 { &mut *u } else { &mut *v };
14525+ let cu_grid = read_plane(
14526+ dec, cdfs, chroma_set, scans.1, plane_pass, cu_around[plane_pass], mode,
14527+ uv_predict_mode, angle_delta_uv, cu_reach, buf, cu_x, cu_y,
1449514528 chroma_tx, chroma_tx, base_q_idx,
14496- alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
14529+ alpha.zip(ac).map(|((au, av), ac)| {
14530+ (if plane_pass == 1 { au } else { av }, ac)
14531+ }),
1449714532 None, chroma_ctx_offset, smooth_neighbor_uv, fctx,
1449814533 )?;
1449914534 if cn > 1 {
@@ -14504,15 +14539,23 @@ fn decode_block(
1450414539 // 1 from this left neighbour -- measured against aomdec).
1450514540 // The same writes are replayed after `record_split_luma`
1450614541 // below, which would otherwise clobber them whole-block.
14507- neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 1, &u_grid);
14508- neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 2, &v_grid);
14509- units.push((cu_mi, luma_span, u_grid.clone(), v_grid.clone()));
14542+ neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_pass, &cu_grid);
14543+ }
14544+ if plane_pass == 1 {
14545+ u_pass.push(cu_grid);
14546+ } else {
14547+ let u_grid = std::mem::replace(&mut u_pass[pass_idx], Grid::Zero(0));
14548+ if cn > 1 {
14549+ units.push((cu_mi, luma_span, u_grid.clone(), cu_grid.clone()));
14550+ }
14551+ last_grids = [u_grid, cu_grid];
1451014552 }
14511- last_grids = [u_grid, v_grid];
14553+ pass_idx += 1;
1451214554 }
1451314555 }
1451414556 }
1451514557 }
14558+ }
1451614559 // Mode/side bookkeeping plus, at `cn == 1`, the whole-plane chroma
1451714560 // context write; at `cn > 1` the per-unit writes above are what the
1451814561 // next block reads, so they run after this (last writer wins).
@@ -24427,6 +24470,216 @@ fn read_inter_plane(
2442724470 Ok((grid, tx_type))
2442824471 }
2442924472
24473+/// The intra twin of [`read_inter_chroma_lossless`]: an INTRA block inside a
24474+/// lossless frame codes its chroma plane as 4x4 transform units, plane-major
24475+/// inside each 64x64 mu chunk (libaom `decode_token_recon_block`'s
24476+/// `for (plane..) for (blk_row..) for (blk_col..)` with `av1_get_tx_size`
24477+/// returning TX_4X4 under `xd->lossless[..]`). CfL never rides here:
24478+/// `is_cfl_allowed` (`blockd.h`) needs a BLOCK_4X4 chroma plane block, i.e. a
24479+/// luma block of 8x8 or less, which the sub-8x8 readers own.
24480+#[allow(clippy::too_many_arguments)]
24481+fn read_intra_chroma_lossless(
24482+ dec: &mut SymbolDecoder,
24483+ cdfs: &mut Cdfs,
24484+ neighbours: &mut Neighbours,
24485+ u: &mut PlaneBuf<'static>,
24486+ v: &mut PlaneBuf<'static>,
24487+ at_mi: (usize, usize),
24488+ (px, py): (usize, usize),
24489+ (cpx, cpy): (usize, usize),
24490+ side: usize,
24491+ (blk_w, blk_h): (usize, usize),
24492+ (org_x, org_y): (usize, usize),
24493+ (reg_w, reg_h): (usize, usize),
24494+ stride: usize,
24495+ reach: Reach,
24496+ (frame_w, frame_h): (usize, usize),
24497+ mode: usize,
24498+ uv_predict_mode: usize,
24499+ angle_delta_uv: i32,
24500+ palette_uv_bufs: Option<(&[u16], &[u16])>,
24501+ smooth_neighbor_uv: bool,
24502+ base_q_idx: u8,
24503+ u_out: &mut Vec<i32>,
24504+ v_out: &mut Vec<i32>,
24505+ fctx: &crate::decode::FrameCtx,
24506+) -> Result<()> {
24507+ let scan4 = default_scan(4);
24508+ let offset = (blk_w > 4 || blk_h > 4).then_some(3);
24509+ for plane_idx in 1..3 {
24510+ for rr in 0..reg_h / 4 {
24511+ for cc in 0..reg_w / 4 {
24512+ let (ox, oy) = (org_x + cc * 4, org_y + rr * 4);
24513+ // libaom's `max_blocks_wide/high` clip: a unit whose origin is
24514+ // past the frame edge is not coded at all
24515+ // (`decode_token_recon_block`), same test `decode_block`'s
24516+ // luma loop already runs.
24517+ if cpx + ox >= u.true_width || cpy + oy >= u.true_height {
24518+ continue;
24519+ }
24520+ let cu_mi = (at_mi.0 + oy / 2, at_mi.1 + ox / 2);
24521+ // libaom's chroma entropy-context arrays are SUBSAMPLED:
24522+ // `get_txb_ctx` reads exactly one entry per plane for a
24523+ // TX_4X4 chroma unit (`a[0]`/`l[0]`). Our bands are luma-mi
24524+ // indexed and carry that one entry replicated over the unit's
24525+ // two cells, so one cell is the same answer -- and the second
24526+ // cell is past the band on a unit whose block hangs off the
24527+ // frame edge, which libaom still codes.
24528+ let cu_around = neighbours.around_mi(cu_mi, MI);
24529+ let cu_reach = tu_reach(
24530+ side, side, ox * 2, oy * 2, 8, reach, px, py, frame_w, frame_h, fctx,
24531+ );
24532+ if let Some((ub, vb)) = palette_uv_bufs {
24533+ let buf = if plane_idx == 1 { ub } else { vb };
24534+ set_palette_pred(palette_window(buf, stride, ox, oy, 4, 4), fctx);
24535+ }
24536+ let buf: &mut PlaneBuf<'static> =
24537+ if plane_idx == 1 { &mut *u } else { &mut *v };
24538+ let cu_grid = read_plane(
24539+ dec,
24540+ cdfs,
24541+ TxbSet::Chroma4,
24542+ &scan4,
24543+ plane_idx,
24544+ cu_around[plane_idx],
24545+ mode,
24546+ uv_predict_mode,
24547+ angle_delta_uv,
24548+ cu_reach,
24549+ buf,
24550+ cpx + ox,
24551+ cpy + oy,
24552+ 4,
24553+ 4,
24554+ base_q_idx,
24555+ None,
24556+ None,
24557+ offset,
24558+ smooth_neighbor_uv,
24559+ fctx,
24560+ )?;
24561+ neighbours.record_mi_chroma(cu_mi, 8, 8, plane_idx, &cu_grid);
24562+ hit!(CHROMA_SPLIT_TX_HITS);
24563+ let dst = mu_units(
24564+ if plane_idx == 1 { &mut *u_out } else { &mut *v_out },
24565+ stride * stride,
24566+ );
24567+ for row in 0..4 {
24568+ let start = (oy + row) * stride + ox;
24569+ dst[start..start + 4].copy_from_slice(&cu_grid[row * 4..][..4]);
24570+ }
24571+ }
24572+ }
24573+ }
24574+ Ok(())
24575+}
24576+
24577+/// lane-lossless2: the chroma half of a lossless INTER block. libaom's
24578+/// `read_tx_size` (`av1/decoder/decodeframe.c`) returns TX_4X4 for EVERY
24579+/// plane of a lossless block, so the chroma plane block is a raster of 4x4
24580+/// transform units instead of one whole-block transform, and
24581+/// `decode_token_recon_block` walks them PLANE-MAJOR inside each 64x64 "mu"
24582+/// chunk (`for (plane..) for (blk_row..) for (blk_col..)`): every U unit of
24583+/// the chunk, then every V unit. Reads one chunk-sized region
24584+/// (`org`/`reg`, chroma pixels relative to the block) and copies its units
24585+/// into the block-sized `u_out`/`v_out` grids.
24586+#[allow(clippy::too_many_arguments)]
24587+fn read_inter_chroma_lossless(
24588+ dec: &mut SymbolDecoder,
24589+ cdfs: &mut Cdfs,
24590+ neighbours: &mut Neighbours,
24591+ u: &mut PlaneBuf<'static>,
24592+ v: &mut PlaneBuf<'static>,
24593+ su: Pred<'_>,
24594+ sv: Pred<'_>,
24595+ at_mi: (usize, usize),
24596+ (cpx, cpy): (usize, usize),
24597+ (org_x, org_y): (usize, usize),
24598+ (reg_w, reg_h): (usize, usize),
24599+ // the whole block's chroma plane block -- what `get_txb_ctx`'s offset is
24600+ // decided on.
24601+ (blk_w, blk_h): (usize, usize),
24602+ // the stride the predictors and the block-sized `u_out`/`v_out` are read
24603+ // at (the block's square chroma side, even for a 2:1 strip, which embeds
24604+ // its narrower plane block in the same square grid).
24605+ stride: usize,
24606+ mode_for_tx: usize,
24607+ base_q_idx: u8,
24608+ u_out: &mut Vec<i32>,
24609+ v_out: &mut Vec<i32>,
24610+ fctx: &crate::decode::FrameCtx,
24611+) -> Result<()> {
24612+ let scan4 = default_scan(4);
24613+ // `get_txb_ctx` (`av1/common/txb_common.h`): `ctx_offset` is 10 when the
24614+ // plane block is bigger than the transform and 7 when it is the
24615+ // transform -- our chroma tables carry the offset-10 rows at +3.
24616+ let offset = (blk_w > 4 || blk_h > 4).then_some(3);
24617+ for plane_idx in 1..3 {
24618+ for rr in 0..reg_h / 4 {
24619+ for cc in 0..reg_w / 4 {
24620+ let (ox, oy) = (org_x + cc * 4, org_y + rr * 4);
24621+ // libaom's `max_blocks_wide/high` clip: a unit whose origin is
24622+ // past the frame edge is not coded at all
24623+ // (`decode_token_recon_block`), same test `decode_block`'s
24624+ // luma loop already runs.
24625+ if cpx + ox >= u.true_width || cpy + oy >= u.true_height {
24626+ continue;
24627+ }
24628+ // The chroma neighbour bands are indexed in LUMA mi: 4 chroma
24629+ // pixels span 8 luma ones, i.e. 2 mi.
24630+ let cu_mi = (at_mi.0 + oy / 2, at_mi.1 + ox / 2);
24631+ // libaom's chroma entropy-context arrays are SUBSAMPLED:
24632+ // `get_txb_ctx` reads exactly one entry per plane for a
24633+ // TX_4X4 chroma unit (`a[0]`/`l[0]`). Our bands are luma-mi
24634+ // indexed and carry that one entry replicated over the unit's
24635+ // two cells, so one cell is the same answer -- and the second
24636+ // cell is past the band on a unit whose block hangs off the
24637+ // frame edge, which libaom still codes.
24638+ let cu_around = neighbours.around_mi(cu_mi, MI);
24639+ let (src, buf) = if plane_idx == 1 {
24640+ (su, &mut *u)
24641+ } else {
24642+ (sv, &mut *v)
24643+ };
24644+ let cu_grid = read_inter_plane(
24645+ dec,
24646+ cdfs,
24647+ TxbSet::Chroma4,
24648+ &scan4,
24649+ plane_idx,
24650+ cu_around[plane_idx],
24651+ mode_for_tx,
24652+ buf,
24653+ cpx + ox,
24654+ cpy + oy,
24655+ 4,
24656+ base_q_idx,
24657+ src.offset(oy * stride + ox),
24658+ // A lossless block codes DCT_DCT on every plane
24659+ // (`av1_get_tx_type`, `blockd.h`), so nothing is
24660+ // inherited from the luma unit.
24661+ None,
24662+ offset,
24663+ fctx,
24664+ )?
24665+ .0;
24666+ // Immediately, so the NEXT unit reads this one's context.
24667+ neighbours.record_mi_chroma(cu_mi, 8, 8, plane_idx, &cu_grid);
24668+ hit!(CHROMA_SPLIT_TX_HITS);
24669+ let dst = mu_units(
24670+ if plane_idx == 1 { &mut *u_out } else { &mut *v_out },
24671+ stride * stride,
24672+ );
24673+ for row in 0..4 {
24674+ let start = (oy + row) * stride + ox;
24675+ dst[start..start + 4].copy_from_slice(&cu_grid[row * 4..][..4]);
24676+ }
24677+ }
24678+ }
24679+ }
24680+ Ok(())
24681+}
24682+
2443024683 /// Spec 5.11.25's `single_ref_p1`..`p6` tree (reachable only once `comp_mode`
2443124684 /// is known `SINGLE_REFERENCE` -- `decode_stream` refuses any frame with
2443224685 /// `reference_select` set before this is ever called, so that read is never
@@ -27403,6 +27656,21 @@ fn decode_inter_block(
2740327656 let cu_around = neighbours.around_mi(cu_mi, luma_span);
2740427657 let (cu_x, cu_y) = (cpx + cc * cu_tx, cpy + cr * cu_tx);
2740527658 let cu_scan = default_scan(cu_tx);
27659+ if lossless(fctx) {
27660+ // lane-lossless2: this chunk's chroma is
27661+ // 8x8 TX_4X4 units per plane, not one
27662+ // TX_32X32 unit.
27663+ read_inter_chroma_lossless(
27664+ dec, cdfs, neighbours, u, v, su, sv,
27665+ at_mi, (cpx, cpy),
27666+ (cc * cu_tx, cr * cu_tx), (cu_tx, cu_tx),
27667+ (write_chroma_w, write_chroma_h),
27668+ chroma_side, mode_for_tx, base_q_idx,
27669+ &mut u_units, &mut v_units, fctx,
27670+ )?;
27671+ mu_chroma = true;
27672+ continue;
27673+ }
2740627674 for plane_idx in 1..3 {
2740727675 let (src, buf) = if plane_idx == 1 {
2740827676 (su, &mut *u)
@@ -27513,6 +27781,23 @@ fn decode_inter_block(
2751327781 } else {
2751427782 Grid::Own(std::mem::take(&mut v_units))
2751527783 };
27784+ } else if lossless(fctx) {
27785+ // lane-lossless2: TX_4X4 chroma units, plane-major
27786+ // (libaom `read_tx_size` + `decode_token_recon_block`).
27787+ let (mut uo, mut vo) = (Vec::new(), Vec::new());
27788+ read_inter_chroma_lossless(
27789+ dec, cdfs, neighbours, u, v, su, sv, at_mi, (cpx, cpy),
27790+ (0, 0), (write_chroma_w, write_chroma_h),
27791+ (write_chroma_w, write_chroma_h), chroma_side,
27792+ mode_for_tx, base_q_idx, &mut uo, &mut vo, fctx,
27793+ )?;
27794+ let zero = chroma_side * chroma_side;
27795+ u_grid = if uo.is_empty() { Grid::Zero(zero) } else { Grid::Own(uo) };
27796+ v_grid = if vo.is_empty() { Grid::Zero(zero) } else { Grid::Own(vo) };
27797+ // Every unit stamped its own neighbour context above; the
27798+ // whole-block record must not overwrite them with the
27799+ // assembled grid (class `override-slot-on-one-arm`).
27800+ mu_chroma = true;
2751627801 } else if rect_tu {
2751727802 // `av1_get_max_uv_txsize(bsize)`: the chroma transform of a
2751827803 // 2:1 strip is the strip's own rect chroma size (16x8 /
@@ -28700,6 +28985,21 @@ fn decode_inter_block(
2870028985 let cu_around = neighbours.around_mi(cu_mi, luma_span);
2870128986 let (cu_x, cu_y) = (cpx + cc * cu_tx, cpy + cr * cu_tx);
2870228987 let cu_scan = default_scan(cu_tx);
28988+ if lossless(fctx) {
28989+ // lane-lossless2: this chunk's chroma is
28990+ // 8x8 TX_4X4 units per plane, not one
28991+ // TX_32X32 unit.
28992+ read_inter_chroma_lossless(
28993+ dec, cdfs, neighbours, u, v, su, sv,
28994+ at_mi, (cpx, cpy),
28995+ (cc * cu_tx, cr * cu_tx), (cu_tx, cu_tx),
28996+ (write_chroma_w, write_chroma_h),
28997+ chroma_side, mode_for_tx, base_q_idx,
28998+ &mut u_units, &mut v_units, fctx,
28999+ )?;
29000+ mu_chroma = true;
29001+ continue;
29002+ }
2870329003 for plane_idx in 1..3 {
2870429004 let (src, buf) = if plane_idx == 1 {
2870529005 (su, &mut *u)
@@ -28819,6 +29119,23 @@ fn decode_inter_block(
2881929119 } else {
2882029120 Grid::Own(std::mem::take(&mut v_units))
2882129121 };
29122+ } else if lossless(fctx) {
29123+ // lane-lossless2: TX_4X4 chroma units, plane-major
29124+ // (libaom `read_tx_size` + `decode_token_recon_block`).
29125+ let (mut uo, mut vo) = (Vec::new(), Vec::new());
29126+ read_inter_chroma_lossless(
29127+ dec, cdfs, neighbours, u, v, su, sv, at_mi, (cpx, cpy),
29128+ (0, 0), (write_chroma_w, write_chroma_h),
29129+ (write_chroma_w, write_chroma_h), chroma_side,
29130+ mode_for_tx, base_q_idx, &mut uo, &mut vo, fctx,
29131+ )?;
29132+ let zero = chroma_side * chroma_side;
29133+ u_grid = if uo.is_empty() { Grid::Zero(zero) } else { Grid::Own(uo) };
29134+ v_grid = if vo.is_empty() { Grid::Zero(zero) } else { Grid::Own(vo) };
29135+ // Every unit stamped its own neighbour context above; the
29136+ // whole-block record must not overwrite them with the
29137+ // assembled grid (class `override-slot-on-one-arm`).
29138+ mu_chroma = true;
2882229139 } else if rect_tu {
2882329140 // `av1_get_max_uv_txsize(bsize)`: the chroma transform of a
2882429141 // 2:1 strip is the strip's own rect chroma size (16x8 /
@@ -29380,6 +29697,22 @@ fn decode_inter_block(
2938029697 reach, px, py, y.width, y.height, fctx,
2938129698 );
2938229699 let cu_scan = default_scan(cu_tx);
29700+ if lossless(fctx) {
29701+ // lane-lossless2: 8x8 TX_4X4 units per plane in this
29702+ // chunk, not one TX_32X32 unit.
29703+ read_intra_chroma_lossless(
29704+ dec, cdfs, neighbours, u, v, at_mi, (px, py),
29705+ (cpx, cpy), side, (chroma_side, chroma_side),
29706+ (cc * cu_tx, cr * cu_tx), (cu_tx, cu_tx),
29707+ chroma_side, reach, (y.width, y.height), mode,
29708+ uv_predict_mode, angle_delta_uv,
29709+ palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])),
29710+ smooth_neighbor_uv, base_q_idx,
29711+ &mut u_units, &mut v_units, fctx,
29712+ )?;
29713+ mu_chroma = true;
29714+ continue;
29715+ }
2938329716 for plane_idx in 1..3 {
2938429717 let buf: &mut PlaneBuf<'static> = if plane_idx == 1 { &mut *u } else { &mut *v };
2938529718 let cu_grid = if skip {
@@ -29552,6 +29885,23 @@ fn decode_inter_block(
2955229885 )?;
2955329886 }
2955429887 let ac = alpha.map(|_| cfl_src(px, py, side));
29888+ if lossless(fctx) {
29889+ // lane-lossless2: the block's chroma plane is a raster of 4x4
29890+ // units, plane-major (`read_intra_chroma_lossless`).
29891+ let (mut uo, mut vo) = (Vec::new(), Vec::new());
29892+ read_intra_chroma_lossless(
29893+ dec, cdfs, neighbours, u, v, at_mi, (px, py), (cpx, cpy),
29894+ side, (chroma_side, chroma_side), (0, 0),
29895+ (chroma_side, chroma_side), chroma_side, reach,
29896+ (y.width, y.height), mode, uv_predict_mode, angle_delta_uv,
29897+ palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])),
29898+ smooth_neighbor_uv, base_q_idx, &mut uo, &mut vo, fctx,
29899+ )?;
29900+ let zero = chroma_side * chroma_side;
29901+ u_grid = if uo.is_empty() { Grid::Zero(zero) } else { Grid::Own(uo) };
29902+ v_grid = if vo.is_empty() { Grid::Zero(zero) } else { Grid::Own(vo) };
29903+ mu_chroma = true;
29904+ } else {
2955529905 if let Some((ub, _)) = &palette_uv_bufs {
2955629906 set_palette_pred(ub.clone(), fctx);
2955729907 }
@@ -29602,6 +29952,7 @@ fn decode_inter_block(
2960229952 None,
2960329953 smooth_neighbor_uv, fctx,
2960429954 )?;
29955+ }
2960529956 }
2960629957 }
2960729958 // lane-comppin r9: end-of-block checkpoint -- everything between
@@ -29634,11 +29985,20 @@ fn decode_inter_block(
2963429985 // units are collected here and re-stamped after that record, in mu-chunk
2963529986 // order so a later unit still wins over an earlier one.
2963629987 let mu_chroma_units: Vec<((usize, usize), usize, Grid)> = if mu_chroma {
29637- let cu = 32usize;
29988+ // lane-lossless2: the unit is TX_32X32 (one per 64x64 mu chunk)
29989+ // normally and TX_4X4 on a lossless frame, where the whole chroma
29990+ // plane block is a raster of them.
29991+ let cu = if lossless(fctx) { 4usize } else { 32usize };
29992+ let span = cu * 2;
29993+ let (rows, cols) = if lossless(fctx) {
29994+ ((write_h / 2).div_ceil(cu), (write_w / 2).div_ceil(cu))
29995+ } else {
29996+ ((write_h / 64).max(1), (write_w / 64).max(1))
29997+ };
2963829998 let mut units = Vec::new();
29639- for cr in 0..(write_h / 64).max(1) {
29640- for cc in 0..(write_w / 64).max(1) {
29641- let cu_mi = (at.0 + cr * 16, at.1 + cc * 16);
29999+ for cr in 0..rows {
30000+ for cc in 0..cols {
30001+ let cu_mi = (at.0 + cr * (span / MI), at.1 + cc * (span / MI));
2964230002 for (plane, grid) in [(1usize, &u_grid), (2usize, &v_grid)] {
2964330003 let mut unit = Vec::with_capacity(cu * cu);
2964430004 for rr in 0..cu {
@@ -29782,8 +30142,9 @@ fn decode_inter_block(
2978230142 neighbours.record_uv_mode_mi(s.pair_mi.0, s.pair_mi.1, pw / MI, ph / MI, uv_predict_mode);
2978330143 hit!(INTER16_CHROMA_PAIR_HITS);
2978430144 }
30145+ let mu_span = if lossless(fctx) { 8 } else { 64 };
2978530146 for (cu_mi, plane, unit) in &mu_chroma_units {
29786- neighbours.record_mi_chroma(*cu_mi, 64, 64, *plane, unit);
30147+ neighbours.record_mi_chroma(*cu_mi, mu_span, mu_span, *plane, unit);
2978730148 }
2978830149 neighbours.record_inter_rect_mi(
2978930150 at,
@@ -31201,6 +31562,16 @@ fn decode_inter_sub8_rect2(
3120131562 0,
3120231563 &mut leaves,
3120331564 );
31565+ } else if lossless(fctx) && !skip {
31566+ // lane-lossless2: `read_block_tx_size` returns TX_4X4 before it
31567+ // ever looks at `max_txsize_rect_lookup`, so an 8x4/4x8 leaf is
31568+ // TWO 4x4 units, not one rect one.
31569+ for row in 0..bh / MI {
31570+ for col in 0..bw / MI {
31571+ leaves.push((row, col, MI, MI));
31572+ }
31573+ }
31574+ set_txfm_ctxs(neighbours, lmi, MI, w_mi, h_mi, skip);
3120431575 } else {
3120531576 leaves.push((0, 0, bw, bh));
3120631577 set_txfm_ctxs(neighbours, lmi, bw.max(bh), w_mi, h_mi, skip);
crates/ec-av1/src/refusal_inventory.rsmodified+7 -12
@@ -57,15 +57,14 @@ const REFUSALS: &[&str] = &[
5757 "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)",
5858 "a sub-8x8 leaf that uses intrabc (this reader has no block-vector path; the 8x8-and-up reader reconstructs one)",
5959 "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)",
60- // lane-lossless: the KEY/intra-only half of a lossless frame (base_q_idx
61- // 0) now decodes sample-exact -- TX_4X4 with the Walsh-Hadamard transform
60+ // lane-lossless/lane-lossless2: a lossless frame (base_q_idx 0) decodes
61+ // sample-exact, KEY and INTER -- TX_4X4 with the Walsh-Hadamard transform
6262 // on every plane, no `tx_type` symbol, `is_cfl_allowed` narrowed to a
63- // BLOCK_4X4 chroma plane block. What is left is the INTER chroma walk: an
64- // inter block's chroma plane is still read as ONE transform per block
65- // here, so a lossless inter frame is refused by name (gate
66- // `a_lossless_inter_frame_is_refused_by_name`). A frame whose segments
67- // disagree is refused separately: every lossless rule is per segment.
68- "a lossless INTER frame (its chroma plane is still coded as one transform per block here)",
63+ // BLOCK_4X4 chroma plane block, and the chroma plane walked as 4x4 units
64+ // plane-major inside each mu chunk (gates
65+ // `a_lossless_libaom_key_frame_decodes_sample_exact` /
66+ // `a_lossless_libaom_inter_frame_decodes_sample_exact`). A frame whose
67+ // segments disagree is still refused: every lossless rule is per segment.
6968 "a frame mixing lossless and lossy segments (the TX_4X4/WHT rules are per segment there)",
7069 "a frame OBU with no tile group",
7170 "a frame naming primary_ref_frame at a reference slot with no saved CDF state",
@@ -285,10 +284,6 @@ const PROVEN: &[(&str, &str)] = &[
285284 "an inter frame with no key frame before it",
286285 "an_inter_frame_opening_a_stream_is_refused_by_name",
287286 ),
288- (
289- "a lossless INTER frame (its chroma plane is still coded as one transform per block here)",
290- "a_lossless_inter_frame_is_refused_by_name",
291- ),
292287 // lane-t900 r25, enumeration: a motion_mode/obmc symbol is read only under
293288 // `is_motion_variation_allowed_bsize` (min side >= 8), and each of the 17
294289 // `BLOCK_SIZES_ALL` footprints that clause admits maps to a distinct row of
crates/ec-av1/src/stream.rsmodified+162 -52
@@ -1617,19 +1617,10 @@ fn decode_frame(
16171617 "a frame mixing lossless and lossy segments (the TX_4X4/WHT rules are per segment there)",
16181618 ));
16191619 }
1620- // lane-lossless r1 ships the KEY/intra-only half: an inter block's chroma
1621- // plane is still read as ONE transform per block here, where a lossless
1622- // frame codes it as 4x4 units like everything else (libaom
1623- // `read_block_tx_size` returns TX_4X4 for every plane). The luma side of
1624- // the inter path is already converted (`read_block_tx_size`'s synthesized
1625- // leaves); the chroma walk and its mu-chunk order are the remaining work,
1626- // so refuse by name rather than return silently wrong pixels.
1627- if coded_lossless && header.frame_type == FrameType::Inter {
1628- return Err(Error::unsupported(
1629- "AV1 decode_stream",
1630- "a lossless INTER frame (its chroma plane is still coded as one transform per block here)",
1631- ));
1632- }
1620+ // lane-lossless2: an INTER frame's chroma plane is now walked as 4x4
1621+ // transform units too (`decode::read_inter_chroma_lossless`), so the
1622+ // named refusal that stood here is gone -- the gate that replaced it is
1623+ // `a_lossless_libaom_inter_frame_decodes_sample_exact`.
16331624 crate::decode::set_lossless(coded_lossless, fctx);
16341625 crate::decode::set_bit_depth(bit_depth, fctx);
16351626 // spec 7.16: this frame's superres state, for the filter chain --
@@ -4033,7 +4024,20 @@ pub(crate) mod tests {
40334024 raw.push(((col * 7 + row * 13 + f * 31) % 251) as u8);
40344025 }
40354026 }
4036- raw.extend(std::iter::repeat_n(128u8, w * h / 2));
4027+ // lane-lossless2: TEXTURED chroma. A flat chroma plane codes one
4028+ // `txb_skip` symbol per 4x4 unit with every context at 0, which
4029+ // is blind to both the unit ORDER and the per-unit contexts
4030+ // (class `gate-blind-to-feature`).
4031+ for row in 0..h / 2 {
4032+ for col in 0..w / 2 {
4033+ raw.push((((col + f) * 11 + row * 5) % 251) as u8);
4034+ }
4035+ }
4036+ for row in 0..h / 2 {
4037+ for col in 0..w / 2 {
4038+ raw.push(((col * 3 + (row + f) * 17) % 251) as u8);
4039+ }
4040+ }
40374041 }
40384042 // [[pid-keyed-temp-path]]: parallel test binaries must not share a name.
40394043 let src = std::env::temp_dir().join(format!("ec-av1-lossless-{}.yuv", std::process::id()));
@@ -4092,51 +4096,157 @@ pub(crate) mod tests {
40924096 }
40934097 }
40944098
4095- /// The other half of lane-lossless r1: a lossless INTER frame is refused BY
4096- /// NAME (its chroma plane is still one transform per block here), never
4097- /// decoded into silently wrong pixels -- the refusal this gate pins is the
4098- /// one `refusal_inventory` carries.
4099+ /// lane-lossless2: the refusal `a_lossless_inter_frame_is_refused_by_name`
4100+ /// pinned became an implementation, so its gate became a DECODE-EXACT one
4101+ /// (class `refusal-lifted-without-a-gate`). A lossless INTER frame codes
4102+ /// its chroma plane as 4x4 transform units, plane-major inside each 64x64
4103+ /// mu chunk (libaom `read_tx_size` returns TX_4X4 for every plane under
4104+ /// `xd->lossless[..]`, and `decode_token_recon_block` walks
4105+ /// `for (plane..) for (blk_row..) for (blk_col..)`), which is what
4106+ /// `decode::read_inter_chroma_lossless` reads. The source carries TEXTURED
4107+ /// CHROMA and real motion on purpose: a flat-chroma source codes one
4108+ /// `txb_skip` symbol per unit and is blind to both the unit order and the
4109+ /// per-unit contexts (class `gate-blind-to-feature`).
40994110 #[test]
4100- fn a_lossless_inter_frame_is_refused_by_name() {
4101- const NAME: &str = "a_lossless_inter_frame_is_refused_by_name";
4111+ fn a_lossless_libaom_inter_frame_decodes_sample_exact() {
4112+ const NAME: &str = "a_lossless_libaom_inter_frame_decodes_sample_exact";
41024113 if !have_ffmpeg() {
41034114 eprintln!("SKIP {NAME}: no ffmpeg");
41044115 return;
41054116 }
4106- let (w, h, frames) = (128usize, 64usize, 4usize);
4107- let mut raw = Vec::with_capacity(frames * w * h * 3 / 2);
4108- for f in 0..frames {
4109- for row in 0..h {
4110- for col in 0..w {
4111- raw.push(((col * 7 + row * 13 + f * 31) % 251) as u8);
4117+ // (w, h, frames, gop, bit depth, 128-superblock, screen content)
4118+ // lane-lossless2: the SCREEN rows carry palette blocks, which is what
4119+ // caught the whole-block palette prediction being handed to a 4x4
4120+ // chroma unit; they are KEY-only, see the lane report's deferred
4121+ // screen-inter desync.
4122+ let cases: [(usize, usize, usize, &str, u32, bool, bool); 6] = [
4123+ (256, 128, 8, "4", 8, false, false),
4124+ (192, 96, 6, "3", 10, false, false),
4125+ (320, 192, 6, "3", 8, false, false),
4126+ (256, 256, 4, "2", 8, true, false),
4127+ (320, 192, 2, "1", 8, false, true),
4128+ (640, 384, 2, "1", 8, false, true),
4129+ ];
4130+ for &(w, h, frames, gop, depth, sb128, screen) in &cases {
4131+ let tag = format!(
4132+ "{w}x{h} {depth}-bit gop {gop}{}{}",
4133+ if sb128 { " sb128" } else { "" },
4134+ if screen { " screen" } else { "" }
4135+ );
4136+ let mut raw: Vec<u8> = Vec::new();
4137+ let put = |v: u32, raw: &mut Vec<u8>| {
4138+ if depth == 10 {
4139+ raw.extend_from_slice(&((v * 4) as u16).to_le_bytes());
4140+ } else {
4141+ raw.push(v as u8);
4142+ }
4143+ };
4144+ // A screen source is a few flat colours in 16x16 tiles plus
4145+ // 1-px text-like rows -- what makes libaom code palette blocks.
4146+ const PAL: [u32; 8] = [16, 60, 110, 145, 180, 210, 235, 128];
4147+ for f in 0..frames {
4148+ for row in 0..h {
4149+ for col in 0..w {
4150+ let v = if screen {
4151+ if row % 7 == 0 && col % 3 == 0 {
4152+ if (col / 3 + row + f) % 2 == 1 { 16 } else { 235 }
4153+ } else {
4154+ PAL[((col + 4 * f) / 16 + row / 16) % PAL.len()]
4155+ }
4156+ } else {
4157+ (((col + 2 * f) * 7 + row * 13) % 251) as u32
4158+ };
4159+ put(v, &mut raw);
4160+ }
4161+ }
4162+ for plane in 0..2usize {
4163+ for row in 0..h / 2 {
4164+ for col in 0..w / 2 {
4165+ let v = if screen {
4166+ PAL[((col + 2 * f) / 8 + row / 8 + plane) % PAL.len()]
4167+ } else if plane == 0 {
4168+ (((col + f) * 11 + row * 5) % 251) as u32
4169+ } else {
4170+ ((col * 3 + (row + f) * 17) % 251) as u32
4171+ };
4172+ put(v, &mut raw);
4173+ }
4174+ }
4175+ }
4176+ }
4177+ let pix = if depth == 10 { "yuv420p10le" } else { "yuv420p" };
4178+ // [[pid-keyed-temp-path]]: parallel test binaries must not share a name.
4179+ let src = std::env::temp_dir()
4180+ .join(format!("ec-av1-lossless2-{}-{w}x{h}-{depth}.yuv", std::process::id()));
4181+ std::fs::write(&src, &raw).expect("raw source");
4182+ let mut cmd = Command::new("ffmpeg");
4183+ cmd.args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", pix])
4184+ .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"])
4185+ .arg(&src)
4186+ .args(["-an", "-threads", "1", "-g", gop, "-c:v", "libaom-av1"])
4187+ .args(["-pix_fmt", pix, "-cpu-used", "6", "-b:v", "0", "-crf", "0"]);
4188+ if sb128 {
4189+ cmd.args(["-aom-params", "sb-size=128"]);
4190+ }
4191+ if screen {
4192+ cmd.args(["-aom-params", "tune-content=screen"]);
4193+ }
4194+ let out = cmd
4195+ .args(["-f", "obu", "-"])
4196+ .stdin(Stdio::null())
4197+ .stdout(Stdio::piped())
4198+ .stderr(Stdio::piped())
4199+ .output()
4200+ .expect("ffmpeg failed to run");
4201+ let _ = std::fs::remove_file(&src);
4202+ if !out.status.success() {
4203+ eprintln!(
4204+ "SKIP {NAME}: ffmpeg has no libaom-av1 encoder ({})",
4205+ String::from_utf8_lossy(&out.stderr)
4206+ );
4207+ return;
4208+ }
4209+ let stream = out.stdout;
4210+ // The stream must really be lossless AND really carry inter frames
4211+ // (class `gate-blind-to-feature`).
4212+ let mut parser = Av1Parser::new();
4213+ let (mut pos, mut lossless, mut inters) = (0usize, 0usize, 0usize);
4214+ while pos < stream.len() {
4215+ let obu = parser.parse_obu(&stream[pos..]).expect("libaom OBU parses");
4216+ pos += obu.total_size.max(1);
4217+ let header = match &obu.kind {
4218+ ObuKind::FrameHeader(h) | ObuKind::Frame(h, _) => h,
4219+ _ => continue,
4220+ };
4221+ if header.lossless.iter().all(|&l| l) {
4222+ lossless += 1;
4223+ }
4224+ if header.frame_type == FrameType::Inter {
4225+ inters += 1;
4226+ }
4227+ }
4228+ assert!(lossless >= frames, "{NAME} [{tag}] went blind: {lossless} lossless frames");
4229+ assert!(
4230+ screen || inters > 0,
4231+ "{NAME} [{tag}] went blind: no inter frame at gop {gop}"
4232+ );
4233+ let ours = decode_stream(&stream)
4234+ .unwrap_or_else(|e| panic!("{NAME} [{tag}]: a lossless inter stream decodes: {e}"));
4235+ let refs = if depth == 10 {
4236+ ffmpeg_decode_sequence_10bit(&stream, w, h, frames)
4237+ } else {
4238+ ffmpeg_decode_sequence(&stream, w, h, frames)
4239+ };
4240+ assert_eq!(ours.len(), frames, "{NAME} [{tag}]: frame count");
4241+ for (i, (got, want)) in ours.iter().zip(refs.iter()).enumerate() {
4242+ for (plane, (g, r)) in [(&got.y, &want.y), (&got.u, &want.u), (&got.v, &want.v)]
4243+ .iter()
4244+ .enumerate()
4245+ {
4246+ let bad = g.iter().zip(r.iter()).filter(|(a, b)| a != b).count();
4247+ assert_eq!(bad, 0, "{NAME} [{tag}]: frame {i} plane {plane}: {bad} samples differ");
41124248 }
41134249 }
4114- raw.extend(std::iter::repeat_n(128u8, w * h / 2));
4115- }
4116- let src = std::env::temp_dir().join(format!("ec-av1-lossless-i-{}.yuv", std::process::id()));
4117- std::fs::write(&src, &raw).expect("raw source");
4118- let out = Command::new("ffmpeg")
4119- .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"])
4120- .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"])
4121- .arg(&src)
4122- .args(["-an", "-threads", "1", "-g", "4", "-c:v", "libaom-av1"])
4123- .args(["-cpu-used", "6", "-b:v", "0", "-crf", "0", "-f", "obu", "-"])
4124- .stdin(Stdio::null())
4125- .stdout(Stdio::piped())
4126- .stderr(Stdio::piped())
4127- .output()
4128- .expect("ffmpeg failed to run");
4129- let _ = std::fs::remove_file(&src);
4130- if !out.status.success() {
4131- eprintln!("SKIP {NAME}: ffmpeg has no libaom-av1 encoder");
4132- return;
4133- }
4134- match decode_stream(&out.stdout) {
4135- Ok(_) => panic!("{NAME}: a lossless inter stream decoded instead of being refused"),
4136- Err(e) => assert!(
4137- format!("{e}").contains("a lossless INTER frame"),
4138- "{NAME}: refused for the wrong reason: {e}"
4139- ),
41404250 }
41414251 }
41424252
lanes/lossless2.report.mdadded+81 -0
@@ -0,0 +1,81 @@
1+# lane-lossless2 — AV1 lossless INTER decode
2+
3+Branch `lane-lossless2`, base `99823eef`. Head `f4c75f48`.
4+
5+The refusal `"a lossless INTER frame (its chroma plane is still coded as one
6+transform per block here)"` is gone from `stream.rs` and from
7+`refusal_inventory.rs` (both the list and the refusal->gate table); its gate
8+`a_lossless_inter_frame_is_refused_by_name` is retired and replaced by the
9+decode-exact gate below (class `refusal-lifted-without-a-gate`).
10+
11+## Rules ported (each with its libaom reference)
12+| rule | libaom | where |
13+|---|---|---|
14+| chroma of an INTER block is a raster of TX_4X4 units | `read_tx_size` (`av1/decoder/decodeframe.c`): `if (xd->lossless[..]) return TX_4X4;` for every plane | `decode::read_inter_chroma_lossless`, called from the whole-block chroma site and the 128 mu-chunk site of BOTH `decode_inter_block` arms (compound + single ref) |
15+| same for an INTRA block inside an inter frame | same | `decode::read_intra_chroma_lossless`, called from the intra tail's whole-block and mu-chunk chroma sites |
16+| unit order is PLANE-MAJOR inside each 64x64 mu chunk | `decode_token_recon_block`: `for (row..) for (col..) for (plane..) for (blk_row..) for (blk_col..)` | both helpers; `decode_block`'s multi-unit chroma loop was interleaved U,V per unit and is now plane-major (identical symbol order at one unit per plane per chunk, i.e. every non-lossless case) |
17+| `txb_skip` chroma context offset | `get_txb_ctx` (`av1/common/txb_common.h`): `ctx_offset = num_pels(plane_bsize) > num_pels(tx) ? 10 : 7`; our chroma tables carry the offset-10 rows at `+3` | `offset = (blk_w > 4 \|\| blk_h > 4).then_some(3)` in both helpers |
18+| per-unit entropy context, stamped as the unit is read | `av1_set_entropy_contexts` per transform block | `record_mi_chroma(cu_mi, 8, 8, plane, grid)` per unit; the block-level re-stamp is suppressed (`mu_chroma`) and `mu_chroma_units` now rebuilds 4x4 units with an 8 px span on a lossless frame |
19+| an 8x4/4x8 inter leaf is TWO 4x4 units | `read_block_tx_size`'s lossless early return | `decode_inter_sub8_rect2` leaf list |
20+| no `tx_type` symbol, DCT_DCT, WHT, TX_4X4 luma | (lane-lossless) | unchanged |
21+| deblock/CDEF/LR off | `coded_lossless`/`all_lossless` in the frame header parser | verified unchanged: `read_cdef_params` forces `bits = 0`, the lf levels and LR are header-gated; no decoder-side change was needed |
22+| a frame mixing lossless and lossy SEGMENTS | every rule above is per segment (`xd->lossless[segment_id]`) | still refused by name — see Deferred |
23+
24+## Defects this lane found (all invisible to the previous lane's gate)
25+The lane-lossless gate's source had a FLAT chroma plane: every chroma unit
26+coded one `txb_skip` symbol against all-zero contexts, so the gate was blind
27+to unit order and to per-unit contexts (class `gate-blind-to-feature`). With
28+textured chroma the KEY gate fails at `99823eef`. Three root causes:
29+
30+1. `decode_block` read chroma units interleaved U,V; libaom is plane-major.
31+2. `decode_rect_split`'s lossless chroma branch stamped each unit's context
32+ and then let `record_split_luma_rect_mi` overwrite all of them with the
33+ LAST unit's state — the next block read `dc_sign_ctx` 1 where libaom reads
34+ 0 (class `override-slot-on-one-arm`). Localised with the aomdec oracle:
35+ `EC_TRACE_COEFF` on both sides, first divergence at chroma unit 717 of a
36+ 256x128 `-crf 0` key frame, `dcctx=1` vs `dcctx=0`.
37+3. `decode_inter_sub8_rect2` kept ONE 8x4/4x8 luma transform on a lossless
38+ frame, feeding a rect grid to the 4x4 Walsh-Hadamard (panic, not garbage).
39+
40+Our `EC_COEFF_STEP tag=all_zero` trace now prints `dcctx=` so it lines up with
41+aomdec's field.
42+
43+## Fixtures and results
44+`ffmpeg -c:v libaom-av1 -crf 0 -b:v 0 -cpu-used 6`, synthetic source with
45+TEXTURED luma AND chroma and per-frame motion.
46+
47+| fixture | gate | result |
48+|---|---|---|
49+| 256x128, 2 key frames, 8-bit | `a_lossless_libaom_key_frame_decodes_sample_exact` (source now textured chroma) | EXACT |
50+| 256x128, 8 frames `-g 4`, 8-bit | `a_lossless_libaom_inter_frame_decodes_sample_exact` | EXACT |
51+| 192x96, 6 frames `-g 3`, 10-bit | same gate | EXACT |
52+| 320x192, 6 frames `-g 3`, 8-bit | same gate | EXACT |
53+| 256x256, 4 frames `-g 2`, `-aom-params sb-size=128` | same gate | EXACT |
54+| 320x192, 2 key frames, `tune-content=screen` (palette) | same gate | EXACT |
55+| 640x384, 2 key frames, `tune-content=screen` (palette) | same gate | EXACT |
56+
57+## Screen content (palette) — three more defects, found by a `tune-content=screen` stream
58+| defect | libaom | fix |
59+|---|---|---|
60+| a 4x4 chroma unit got the WHOLE-BLOCK palette prediction buffer and read it at its own stride, so every unit reconstructed the block's top-left corner | the luma split-transform paths already windowed; `predict_and_reconstruct_intra_block` predicts per unit | `decode::palette_window`, shared by `decode_block`, `decode_rect_split` and `read_intra_chroma_lossless`. 320x192 screen key frame: 9600/15360 chroma samples wrong -> 0 |
61+| the two-cell neighbour read ran past the band for a unit whose block hangs off the frame edge (libaom still codes that unit: `max_blocks_high` clips in LUMA mi and then ROUNDS UP for chroma) | `get_txb_ctx` reads exactly ONE chroma entropy entry for TX_4X4 (`a[0]`/`l[0]`) | both helpers read one cell (`around_mi(cu_mi, MI)`) -- our bands carry that entry replicated over the unit's two luma-mi cells, so it is the same answer and never runs off |
62+| a unit whose ORIGIN is past the frame edge was still read | `max_blocks_wide/high` | origin clip in both helpers |
63+
64+`tune-content=screen -crf 0` KEY frames at 320x192 and 640x384: sample-exact
65+(`dump_yuv` vs `ffmpeg -pix_fmt yuv420p10le`, 0/122880 and 0/368640).
66+
67+## Gate results
68+- `a_lossless_libaom_key_frame_decodes_sample_exact` + `a_lossless_libaom_inter_frame_decodes_sample_exact`: PASS (2 passed).
69+- `cargo check --workspace --all-targets -j4`: RC=0, 0 errors, 0 ec-av1 warnings (the 21 warnings are pre-existing `ec-opus`).
70+- crate suite `cargo test -p ec-av1 --release --lib -- --test-threads=1`: **557 passed / 0 failed / 44 ignored** (959.9 s), pins green (`the_encoders_own_streams_are_byte_identical_to_their_pins`). That run predates the two screen KEY rows added to the inter gate afterwards; those two were run on their own (2 passed).
71+- The screen rows really do code palette: the same recipe at 320x192 decoded 9600/15360 chroma samples wrong before `palette_window` and 0 after.
72+- DECIDING GATE `bd_rate_screen_native`: **RED**, one reference point:
73+ `LADDER DECODE FAILURE: libaom-av1 ["-cpu-used","6","-b:v","0","-crf","5"] frame 0 plane Y sample 625: our decoder decoded 41, ffmpeg 40`.
74+ That point used to be refused; it now decodes, and frame 0 (a KEY frame) is
75+ wrong in ONE luma sample at (col 625, row 0) of 1920x1024. The BD row itself
76+ computed: +33.4% vs libaom, -23.8% vs rav1e.
77+
78+## Deferred
79+- `deferred: the bd_rate_screen_native crf-5 screen point — ONE luma sample (625, row 0) of frame 0, a lossless KEY frame, decodes 41 where ffmpeg decodes 40; every other sample of every other point is exact. Not reproduced by a synthetic 1920x1024 tune-content=screen -crf 5 key frame (0 samples differ), so it needs the gate's own stream — unblocks: dump the ladder stream (EC_ENC_OUT / the external_ladder recipe) and diff EC_TRACE_COEFF against aomdec around that block.`
80+- `deferred: lossless INTER frames of SCREEN content desync — reproduced at 640x384 tune-content=screen -crf 0 -g 3: key frames 0 and 3 are sample-exact, inter frames 1/2/4/5 are not. Localised with the aomdec oracle to all_zero unit #23422 of the stream: the first LUMA unit of a block right after a sub-8x8 group reads txb_skip_ctx 3 where libaom reads 1 (one neighbour magnitude > 3 where libaom sees 0) — unblocks: that one block's above-band luma level; the synthetic non-screen inter fixtures in the gate are all exact, so it is screen/sub-8x8 specific.`
81+- `deferred: a frame mixing lossless and lossy segments — still refused by name ("a frame mixing lossless and lossy segments (the TX_4X4/WHT rules are per segment there)"): every rule above keys off a frame-wide `lossless_flag`, so per-segment support means threading `segment_id` through `lossless()`, `lossless_tx()`, the WHT switch and the filter gates — unblocks: a libaom recipe that emits one. Four tried this round at -crf 0 (aom-params aq-mode=1, aq-mode=2, aq-mode=3, deltaq-mode=1): none produced a frame whose segments disagree — the refusal never fired, i.e. every segment stayed at qindex 0.`