diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 227ca52d..2f989080 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2069,6 +2069,22 @@ pub fn chroma422_sub8_hits() -> usize { CHROMA422_SUB8_HITS.with(|c| c.get()) } +// lane-av1-422sub8rect: how many PER-PIECE TX_4X4 chroma coefficient units +// the INTER sub-8x8 split4 path read on a 4:2:2 frame (ss 1,0) -- one per +// non-skipped odd-column piece per plane (`is_chroma_reference`'s +// `mi_col & 0x01` at ss_x 1). 4:2:0 goes through the shared group unit and +// 4:4:4 through [`sub8_leaf_chroma444`]'s own per-piece twin, so a witness +// gate reading 0 here proves the inter 4:2:2 per-piece arm never ran. +thread_local! { + static CHROMA422_INTER_SUB8_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`CHROMA422_INTER_SUB8_HITS`]. +pub fn chroma422_inter_sub8_hits() -> usize { + CHROMA422_INTER_SUB8_HITS.with(|c| c.get()) +} + // lane-av1-422m: how many TX_4X4 chunk units the invalid-ss-size chroma arm // read (the 8x32 luma block whose 4:2:2 plane has no `ss_size_lookup` block). // A 4:2:0 or 4:4:4 frame cannot increment it, so a non-zero read proves the @@ -2668,7 +2684,10 @@ impl TxParams { if self.stride == 0 { return residual; } - let mut strided = vec![0i32; self.stride * self.stride]; + // lane-av1-422sub8rect: `h` rows of `stride` -- a rect unit whose + // height exceeds its (narrow) prediction stride needs the full + // stride * h; every square caller has h == stride and is unchanged. + let mut strided = vec![0i32; self.stride * self.h]; for row in 0..self.h { strided[row * self.stride..][..self.w] .copy_from_slice(&residual[row * self.w..][..self.w]); @@ -30739,6 +30758,8 @@ fn interintra_blend( bw: usize, bh: usize, stride: usize, + subw: usize, + subh: usize, ii_mode: u8, wedge: Option<(&'static [u8], usize)>, pred: &mut [u16], fctx: &crate::decode::FrameCtx, @@ -30785,25 +30806,38 @@ fn interintra_blend( let m = u32::from(match wedge { // lane-wii r2: wedge-interintra mask (aom_blend_a64_mask // over the wedge codebook, intra as src0, fixed sign 0). - // Luma plane: mask rows are luma-resolution, ms == side. - // 4:2:0 chroma: 2x2 box average (reconinter.c sub8x8 -- - // subw == subh == 1). - Some((mask, ms)) if ms == stride => mask[i * stride + j], - Some((mask, ms)) => { - let t = 2 * i * ms + 2 * j; - ((u32::from(mask[t]) - + u32::from(mask[t + 1]) - + u32::from(mask[t + ms]) - + u32::from(mask[t + ms + 1]) - + 2) - >> 2) as u8 - } - None => match ii_mode { - 0 => 32, + // `(subw, subh)` are the plane's own subsampling shifts; + // the four branches mirror aom_dsp/blend-a64-mask.c's + // `subw`/`subh` pair handling exactly: luma (0, 0) reads + // the direct sample, (1, 1) the 2x2 box average + // (reconinter.c sub8x8), and 4:2:2's (1, 0) the + // `AOM_BLEND_AVG` column pair. + Some((mask, ms)) => match (subw, subh) { + (0, 0) => u32::from(mask[i * ms + j]), + (1, 1) => { + let t = 2 * i * ms + 2 * j; + (u32::from(mask[t]) + + u32::from(mask[t + 1]) + + u32::from(mask[t + ms]) + + u32::from(mask[t + ms + 1]) + + 2) + >> 2 + } + (1, 0) => { + let t = i * ms + 2 * j; + (u32::from(mask[t]) + u32::from(mask[t + 1]) + 1) >> 1 + } + (_sw, _sh) => { + let t = 2 * i * ms + j; + (u32::from(mask[t]) + u32::from(mask[t + ms]) + 1) >> 1 + } + }, + None => u32::from(match ii_mode { + 0 => 32u8, 1 => II_WEIGHTS_1D[i * scale], 2 => II_WEIGHTS_1D[j * scale], _ => II_WEIGHTS_1D[i.min(j) * scale], - }, + }), }); let idx = i * stride + j; pred[idx] = ((m * u32::from(intra[i * bw + j]) + (64 - m) * u32::from(pred[idx]) + 32) @@ -30953,6 +30987,12 @@ pub(crate) fn obmc_plan( // `av1_setup_build_prediction_by_above_pred` passing that ref's own // `scale_factors`). frame_width: usize, + // lane-av1-422sub8rect: the plane's own subsampling shifts -- every + // chroma extent below derives from the luma rule by `>> ss` on the + // subsampled axis (`build_obmc_inter_pred_above`'s + // `overlap >> pd->subsampling_y` and the left pass's matching `>> ss_x`), + // never a blind half. 4:2:0 and 4:4:4 reduce to the old /2s and 0s. + ss: (usize, usize), ) -> Result { // lane-rect r2 (libaom av1_build_obmc_inter_prediction): each pass has // its OWN neighbour cap (width-log2 for above, height-log2 for left) and @@ -30984,10 +31024,16 @@ pub(crate) fn obmc_plan( let overlap_above = write_h.min(64) / 2; let overlap_left = write_w.min(64) / 2; // lane-inter8 r1 / lane-scaledref r1: `av1_skip_u4x4_pred_in_obmc` - // (`reconinter.c` 820) returns `dir == 0` when the chroma plane's own - // block is BLOCK_4X4/8X4/4X8 (an 8x8, 16x8 or 8x16 luma block at 4:2:0): - // the ABOVE pass skips chroma entirely, the LEFT pass still blends it. - let skip_chroma_above = matches!((write_w, write_h), (8, 8) | (16, 8) | (8, 16)); + // (reconinter.c:829) keys on the CHROMA plane block + // (`get_plane_block_size(bsize, ss_x, ss_y)`): BLOCK_4X4/BLOCK_8X4/ + // BLOCK_4X8 return `dir == 0`, so the ABOVE pass skips chroma for those + // and the LEFT pass never does. lane-av1-422sub8rect: keyed on the + // per-axis-shifted plane shape now -- an 8x8 leaf is (4, 4) at 4:2:0 + // (skip) and (4, 8) at 4:2:2 (still skip, BLOCK_4X8 is in the list), + // while 8x4/4x8 luma leaves are (4, 4) or (8, 4) at 4:2:2. + let (ss_x, ss_y) = ss; + let skip_chroma_above = + matches!((write_w >> ss_x, write_h >> ss_y), (4, 4) | (8, 4) | (4, 8)); let mut above = Vec::new(); for (off4, span4, nb, src4) in overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, max_nb(bw4)) { @@ -31055,6 +31101,7 @@ pub(crate) fn obmc_plan( cpy, overlap_above, overlap_left, + ss, skip_chroma_above, }) } @@ -31088,6 +31135,8 @@ pub(crate) struct ObmcPlan { cpy: usize, overlap_above: usize, overlap_left: usize, + /// The plane's own subsampling shifts ([`obmc_plan`]). + ss: (usize, usize), skip_chroma_above: bool, } @@ -31115,9 +31164,18 @@ pub(crate) fn obmc_run( cpy, overlap_above, overlap_left, + ss, skip_chroma_above, .. } = plan; + // lane-av1-422sub8rect: every chroma extent is the luma rule shifted by + // the plane's own subsampling (`build_obmc_inter_pred_above`'s + // `overlap >> pd->subsampling_y`, the left pass's `>> ss_x`, the + // neighbour-span `>> ss` per axis) -- 4:2:0 and 4:4:4 reduce to the old + // halves, 4:2:2 keeps the unhalved height. + let (ss_x, ss_y) = ss; + let chroma_w = write_w >> ss_x; + let chroma_h = write_h >> ss_y; // lane-alloc: one neighbour-prediction buffer per plane for the whole // block, reused by every neighbour of the above and left passes. let (mut tmp_y, mut tmp_u, mut tmp_v) = (Vec::new(), Vec::new(), Vec::new()); @@ -31136,7 +31194,8 @@ pub(crate) fn obmc_run( let (x, y) = (f[1].parse::().ok()?, f[2].parse::().ok()?); let fr = f.get(3).and_then(|s| s.parse::().ok()).unwrap_or(-1); let idx = PREFILT_PICTURE_IDX.load(std::sync::atomic::Ordering::SeqCst) as i64; - if x >= cpx && x < cpx + write_w / 2 && y >= cpy && y < cpy + write_h / 2 + let (mcb_cw, mcb_ch) = (write_w >> ss_x, write_h >> ss_y); + if x >= cpx && x < cpx + mcb_cw && y >= cpy && y < cpy + mcb_ch && (fr < 0 || fr == idx) { Some((x, y, idx)) @@ -31147,11 +31206,11 @@ pub(crate) fn obmc_run( if let Some((_, _, idx)) = ec_mcb { eprintln!( "OUR_MCB pre f={idx} cpx={cpx} cpy={cpy} cw={} ch={}", - write_w / 2, - write_h / 2 + write_w >> ss_x, + write_h >> ss_y ); - for r in 0..write_h / 2 { - let row: Vec = (0..write_w / 2).map(|c| pred_u[r * chroma_side + c]).collect(); + for r in 0..(write_h >> ss_y) { + let row: Vec = (0..(write_w >> ss_x)).map(|c| pred_u[r * chroma_side + c]).collect(); eprintln!("OUR_MCB prerow{r}: {row:?}"); } } @@ -31167,9 +31226,10 @@ pub(crate) fn obmc_run( if !skip_chroma_above { // lane-t900 r13 (measured, NOT a defect): libaom's min-4 clamp // sizes only the OBMC PREDICTION buffer; the blend length is the - // plain half (`build_obmc_inter_pred_above`: `overlap >> ss_y`). - let (cbw, cbh, cox) = (bw / 2, overlap_above / 2, ox / 2); - let cw = cbw.min(write_w / 2 - cox); + // plain `overlap >> ss_y` (`build_obmc_inter_pred_above`: + // `bh = overlap >> pd->subsampling_y`). + let (cbw, cbh, cox) = (bw >> ss_x, overlap_above >> ss_y, ox >> ss_x); + let cw = cbw.min(chroma_w - cox); obmc_neighbour_pred(nu, cpx + cox, cpy, nb.mv, cbw, cbh, false, nb.h_kind, nb.v_kind, nb.scale, &mut tmp_u, fctx); obmc_blend_v(pred_u, chroma_side, cox, 0, cw, cbh, cbw, &tmp_u); obmc_neighbour_pred(nv, cpx + cox, cpy, nb.mv, cbw, cbh, false, nb.h_kind, nb.v_kind, nb.scale, &mut tmp_v, fctx); @@ -31184,7 +31244,7 @@ pub(crate) fn obmc_run( let (bw, bh, oy) = (overlap_left, nb.span4 * 4, nb.off4 * 4); obmc_neighbour_pred(ny, px, py + oy, nb.mv, bw, bh, true, nb.h_kind, nb.v_kind, nb.scale, &mut tmp_y, fctx); obmc_blend_h(pred_y, side, 0, oy, bw, bh.min(write_h - oy), &tmp_y); - let (cbw, cbh, coy) = (overlap_left / 2, bh / 2, oy / 2); + let (cbw, cbh, coy) = (overlap_left >> ss_x, bh >> ss_y, oy >> ss_y); obmc_neighbour_pred(nu, cpx, cpy + coy, nb.mv, cbw, cbh, false, nb.h_kind, nb.v_kind, nb.scale, &mut tmp_u, fctx); if let Some((_, _, idx)) = ec_mcb { eprintln!( @@ -31196,14 +31256,14 @@ pub(crate) fn obmc_run( eprintln!("OUR_MCB lrow{r}: {row:?}"); } } - let ch = cbh.min(write_h / 2 - coy); + let ch = cbh.min(chroma_h - coy); obmc_blend_h(pred_u, chroma_side, 0, coy, cbw, ch, &tmp_u); obmc_neighbour_pred(nv, cpx, cpy + coy, nb.mv, cbw, cbh, false, nb.h_kind, nb.v_kind, nb.scale, &mut tmp_v, fctx); obmc_blend_h(pred_v, chroma_side, 0, coy, cbw, ch, &tmp_v); } if let Some((_, _, idx)) = ec_mcb { - for r in 0..write_h / 2 { - let row: Vec = (0..write_w / 2).map(|c| pred_u[r * chroma_side + c]).collect(); + for r in 0..chroma_h { + let row: Vec = (0..chroma_w).map(|c| pred_u[r * chroma_side + c]).collect(); eprintln!("OUR_MCB post f={idx} row{r}: {row:?}"); } } @@ -32642,7 +32702,7 @@ fn decode_inter_block( }; if let Some(mask_y) = mask_y { mc::blend_masked_compound( - &inter0_y, &inter1_y, mask_y, side, side, side, false, &mut pred_y, fctx, + &inter0_y, &inter1_y, mask_y, side, side, side, 0, 0, &mut pred_y, fctx, ); } else { mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y, fctx); @@ -32706,6 +32766,13 @@ fn decode_inter_block( } let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_side * chroma_side); if let Some(mask_y) = mask_y { + // lane-av1-422sub8rect stop-note: this fn's 4:2:2 chroma + // prediction is still the square-cut model (the owning + // parent-lane surface) -- its buffer is `chroma_side`-square + // and cannot be indexed by the subsampled-axis mask walks, + // so at ss_x != ss_y the blend keeps the direct + // luma-resolution read (the pre-port behaviour, known + // divergent there) until that surface itself is ported. mc::blend_masked_compound( &inter0_u, &inter1_u, @@ -32713,7 +32780,8 @@ fn decode_inter_block( side, chroma_side, chroma_side, - ss_x(fctx) == 1 && ss_y(fctx) == 1, + if ss_x(fctx) == ss_y(fctx) { ss_x(fctx) } else { 0 }, + if ss_x(fctx) == ss_y(fctx) { ss_y(fctx) } else { 0 }, &mut pred_u, fctx, ); } else { @@ -32778,6 +32846,13 @@ fn decode_inter_block( } let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_side * chroma_side); if let Some(mask_y) = mask_y { + // lane-av1-422sub8rect stop-note: this fn's 4:2:2 chroma + // prediction is still the square-cut model (the owning + // parent-lane surface) -- its buffer is `chroma_side`-square + // and cannot be indexed by the subsampled-axis mask walks, + // so at ss_x != ss_y the blend keeps the direct + // luma-resolution read (the pre-port behaviour, known + // divergent there) until that surface itself is ported. mc::blend_masked_compound( &inter0_v, &inter1_v, @@ -32785,7 +32860,8 @@ fn decode_inter_block( side, chroma_side, chroma_side, - ss_x(fctx) == 1 && ss_y(fctx) == 1, + if ss_x(fctx) == ss_y(fctx) { ss_x(fctx) } else { 0 }, + if ss_x(fctx) == ss_y(fctx) { ss_y(fctx) } else { 0 }, &mut pred_v, fctx, ); } else { @@ -34039,6 +34115,7 @@ fn decode_inter_block( refpix, interp_fixed, frame_width, + (ss_x(fctx), ss_y(fctx)), )?) } else { None @@ -34200,14 +34277,14 @@ fn decode_inter_block( if write_w != write_h { hit!(INTERINTRA_RECT_HITS); } - interintra_blend(y, px, py, write_w, write_h, side, ii, wedge_mask, &mut pred_y, fctx); + interintra_blend(y, px, py, write_w, write_h, side, 0, 0, ii, wedge_mask, &mut pred_y, fctx); interintra_blend( - u, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ii, wedge_mask, - &mut pred_u, fctx, + u, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ss_x(fctx), + ss_y(fctx), ii, wedge_mask, &mut pred_u, fctx, ); interintra_blend( - v, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ii, wedge_mask, - &mut pred_v, fctx, + v, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ss_x(fctx), + ss_y(fctx), ii, wedge_mask, &mut pred_v, fctx, ); } if let Some(plan) = &obmc { @@ -36201,6 +36278,17 @@ fn decode_inter_sub8_split4( // frame 1: plane 0,1,2 per leaf in leaf order) -- there is no shared // group unit and no 2x2-piece group prediction. let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0; + // 4:2:2 (lane-av1-422sub8rect): `is_chroma_reference` for a BLOCK_4X4 + // piece at ss (1, 0) is `(mi_col & 0x01) || !(bw & 0x01) || !ss_x` + // == ODD mi_col (av1_common_int.h:1459) -- pieces 1 and 3 of the group + // are their plane's own chroma references and each codes its own + // TX_4X4 unit right after its luma, exactly the key-frame split-group + // behaviour the pinned t422 fixture's frame 0 traces (groups (18,54) + // and (30,42): U+V read on BOTH odd-column leaves) and frame 1's + // inter group at mi(12,2) repeats (U+V TX_4X4 ctx 8 on leaf (12,3) + // alone -- the bottom-right leaf was skipped, so it codes nothing). + // The 4:2:0 shared group unit never exists at 4:2:2. + let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; for i in 0..4usize { let (dr, dc) = (i / 2, i % 2); let lmi = (gr + dr, gc + dc); @@ -36253,7 +36341,9 @@ fn decode_inter_sub8_split4( } // lane-sub8intra: the `BLOCK_4X4` twin of the HORZ/VERT arm -- // same [`decode_intra_sub8_leaf`], no tx-depth symbol, chroma on - // the LAST sub-block (`is_chroma_reference` under 4:2:0). + // the chroma-reference sub-blocks (`is_chroma_reference`: the + // LAST one under 4:2:0, BOTH odd-column ones at 4:2:2, every + // one at 4:4:4). decode_intra_sub8_leaf( dec, cdfs, @@ -36262,7 +36352,7 @@ fn decode_inter_sub8_split4( group_mi, lmi, (B4, B4), - chroma_444 || i == 3, + chroma_444 || (cmi & 1 == 1 && chroma_422) || i == 3, skip, scan4, y, @@ -36634,6 +36724,150 @@ fn decode_inter_sub8_split4( neighbours.record_mi_chroma(lmi, B4, B4, 1, &ug); neighbours.record_mi_chroma(lmi, B4, B4, 2, &vg); } + // lane-av1-422sub8rect: the 4:2:2 per-piece arm. `is_chroma_reference` + // for a BLOCK_4X4 at ss (1, 0) is the odd mi_col (`av1_common_int.h` + // :1459), so THIS piece (right column of the group) is a chroma + // reference and codes its own U and V TX_4X4 units right after its + // luma -- measured on the pinned t422 fixture: frame 0 key-frame + // groups (18,54)/(30,42) read U+V on both odd-column leaves, and + // frame 1's inter group at mi(12,2) reads U+V TX_4X4 ctx 8 on leaf + // (12,3) alone (its bottom-right twin was skipped). + // + // The unit anchors at the FLOORED chroma cell + // (`av1_setup_dst_planes`' `(mi_col & !1) * MI >> ss_x`, the + // lane-av1-422kf2 rule), so the 4-px-wide square covers the group's + // whole chroma width, and the prediction walks + // `build_inter_predictors_sub8x8` (reconinter_template.inc): the + // left 2x4 half from the LEFT piece's own ref/mv/filters, the right + // 2x4 half from this piece's -- unless the left piece is intra (or + // off-frame), which makes `is_sub8x8_inter` false and predicts the + // whole 4x4 from THIS piece's own mv. A skipped reference piece + // still predicts (spec 7.11.3: `predict_inter_block_visit` runs + // before the `skip_txfm` guard) but codes no coefficients. + } else if chroma_422 && (cmi & 1) == 1 { + let (pc_ref, pc_mv, pc_hf, pc_vf) = piece[i].expect("inter piece recorded above"); + let unit_x = ((cmi & !1) * MI) >> ss_x(fctx); + let unit_y = py >> ss_y(fctx); + // `is_sub8x8_inter`'s left-neighbour probe (`mi[-1]`): the piece + // one mi to the LEFT, when it exists and is inter. + let piece_left = piece[i - 1]; + let build_c = move |_y: &mut PlaneBuf<'static>, + _u: &mut PlaneBuf<'static>, + _v: &mut PlaneBuf<'static>, + fctx: &crate::decode::FrameCtx, + out: &mut [Vec; 3]| { + let Some((pc_ref_y, sref_u, sref_v)) = ref_planes_deferred(pc_ref, refpix) else { + return; + }; + let pc_scale = mc::scale_factor(pc_ref_y.width, frame_width); + // The left piece's own reference planes + mv/filters, when it + // is an inter piece (`is_sub8x8_inter`'s `mi[-1]` probe). + let left = piece_left + .as_ref() + .map(|&(l_ref, l_mv, l_hf, l_vf)| { + (l_ref, l_mv, l_hf, l_vf) + }) + .and_then(|(l_ref, l_mv, l_hf, l_vf)| { + ref_planes_deferred(l_ref, refpix).map(|planes| { + let (lp_y, _, _) = planes; + let l_scale = mc::scale_factor(lp_y.width, frame_width); + (l_mv, l_hf, l_vf, planes, l_scale) + }) + }); + let mut pred_u = refill(std::mem::take(&mut out[1]), B4 * B4); + let mut pred_v = refill(std::mem::take(&mut out[2]), B4 * B4); + for (plane, dst) in [(sref_u, &mut pred_u), (sref_v, &mut pred_v)] { + let mut buf = vec![0u16; B4 * B4]; + // Right 2x4 half: this piece's own ref/mv/filters. + let mut half = vec![0u16; 2 * 4]; + mc::predict_maybe_scaled( + &plane.data, + plane.width, + plane.true_width, + plane.true_height, + mv_to_q4(unit_x + 2, pc_mv.1, ss_x(fctx)), + mv_to_q4(unit_y, pc_mv.0, ss_y(fctx)), + pc_scale, + 2, + 4, + pc_hf, + pc_vf, + &mut half, fctx, + ); + for row in 0..4 { + buf[row * B4 + 2..row * B4 + 4] + .copy_from_slice(&half[row * 2..row * 2 + 2]); + } + // Left 2x4 half: the LEFT piece's own mv/filters + // (sub8x8-inter); a mixed group (left piece intra or + // off-frame) predicts it from THIS piece too, which + // reduces to the whole-4x4-from-own rule. + let (l_mv, l_hf, l_vf, l_planes, l_scale) = match left { + Some((l_mv, l_hf, l_vf, l_planes, l_scale)) => { + (l_mv, l_hf, l_vf, l_planes, l_scale) + } + None => (pc_mv, pc_hf, pc_vf, (pc_ref_y, sref_u, sref_v), pc_scale), + }; + let (_, l_ref_u, _) = l_planes; + mc::predict_maybe_scaled( + &l_ref_u.data, + l_ref_u.width, + l_ref_u.true_width, + l_ref_u.true_height, + mv_to_q4(unit_x, l_mv.1, ss_x(fctx)), + mv_to_q4(unit_y, l_mv.0, ss_y(fctx)), + l_scale, + 2, + 4, + l_hf, + l_vf, + &mut half, fctx, + ); + for row in 0..4 { + buf[row * B4..row * B4 + 2].copy_from_slice(&half[row * 2..row * 2 + 2]); + } + dst.copy_from_slice(&buf); + } + out[1] = pred_u; + out[2] = pred_v; + }; + let (pred_cu, pred_cv): (Vec, Vec); + let (su, sv) = if recon_deferred(fctx) { + push_pred(fctx, build_c); + ( + Pred::Cur { plane: 1, stride: B4, off: 0 }, + Pred::Cur { plane: 2, stride: B4, off: 0 }, + ) + } else { + let mut built: [Vec; 3] = Default::default(); + build_c(y, u, v, fctx, &mut built); + let [_, bu, bv] = built; + (pred_cu, pred_cv) = (bu, bv); + (Pred::Inline(&pred_cu, B4), Pred::Inline(&pred_cv, B4)) + }; + // The unit's entropy context: one chroma cell wide, floored -- + // [`Neighbours::around_mi_422_chroma`] at the unit's own 8x4 + // luma span (the [`sub8_leaf_chroma422`] rule). + let ctx_mi = (rmi, cmi & !1); + let ca = neighbours.around_mi_422_chroma(ctx_mi, 8, 4); + if skip { + push_mc(1, unit_x, unit_y, B4, su, &ZERO_RESIDUAL[..B4 * B4], fctx); + push_mc(2, unit_x, unit_y, B4, sv, &ZERO_RESIDUAL[..B4 * B4], fctx); + neighbours.record_mi_chroma(ctx_mi, 8, 4, 1, &ZERO_RESIDUAL[..B4 * B4]); + neighbours.record_mi_chroma(ctx_mi, 8, 4, 2, &ZERO_RESIDUAL[..B4 * B4]); + } else { + hit!(CHROMA422_INTER_SUB8_HITS); + let (ug, _) = read_inter_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 1, ca[1], 0, u, unit_x, unit_y, B4, + base_q_idx, su, Some(piece_tx_type), None, fctx, + )?; + let (vg, _) = read_inter_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 2, ca[2], 0, v, unit_x, unit_y, B4, + base_q_idx, sv, Some(piece_tx_type), None, fctx, + )?; + neighbours.record_mi_chroma(ctx_mi, 8, 4, 1, &ug); + neighbours.record_mi_chroma(ctx_mi, 8, 4, 2, &vg); + } } neighbours.record_mode_mi(rmi, cmi, 1, 1, DC_PRED); neighbours.record_inter_rect_mi(lmi, 1, 1, skip, true, ref_frame, resolved_filter, false); @@ -36673,7 +36907,7 @@ fn decode_inter_sub8_split4( // frames mismatched ffmpeg by +-1..7 (class // `override-slot-on-one-arm`, this lane's own 10-bit film gate // regression at 1ad7e0fc; port of lane-av1-cfl's 7c6dad53). - if !chroma_444 && !intra_chroma { + if !chroma_444 && !chroma_422 && !intra_chroma { neighbours.record_uv_mode_mi(gr, gc, 2, 2, DC_PRED); } // lane-t900 r23 (class [[new-map-ignores-tile-edge]]: a neighbour map @@ -36685,10 +36919,12 @@ fn decode_inter_sub8_split4( // 0). No sub-8x8 piece can itself be a palette (`av1_allow_palette` // needs `bsize >= BLOCK_8X8`), so the group always clears. record_strip_palette(neighbours, (gr, gc), 8, 8, 0, [0u16; 8], 0, [0u16; 8]); - if !chroma_444 && !intra_chroma { + if !chroma_444 && !chroma_422 && !intra_chroma { // Chroma, once for the whole group, on the last sub-block's own `skip` // flag -- prediction from all four sub-blocks (2x2 pieces), residual as - // one TX_4X4 unit per plane. + // one TX_4X4 unit per plane. lane-av1-422sub8rect: 4:2:2 never takes + // this arm -- the shared group unit does not exist at ss (1, 0); each + // odd-column piece coded its own chroma inside the loop above. // `is_sub8x8_inter` (reconinter_template.inc:54) is false as soon as ANY // mi of the group is intra: the group's 4x4 chroma is then predicted WHOLE // from the chroma-reference (last) sub-block's own mv, not in 2x2 pieces. @@ -37213,6 +37449,29 @@ fn decode_intra_sub8_leaf( // ours ctx 2 where libaom gathers 1). record_strip_palette(neighbours, (gr, gc), 8, 8, 0, [0u16; 8], 0, [0u16; 8]); if !has_chroma { + // lane-av1-422sub8rect: 4:2:2 has no shared group unit whose tail + // would stamp the mi map (`is_chroma_reference` runs per piece), so + // a non-referenced piece publishes its own cell -- libaom leaves + // `uv_mode` unset on it, `is_smooth` reads 0, i.e. DC_PRED. + if ss_x(fctx) == 1 && ss_y(fctx) == 0 { + neighbours.record_uv_mode_mi(lmi.0, lmi.1, 1, 1, DC_PRED); + } + return Ok(mode); + } + // lane-av1-422sub8rect: at 4:2:2 this (odd-column) piece is its plane's + // OWN chroma reference -- the key-frame split group's per-leaf TX_4X4 + // model, floored anchor and per-chroma-cell context included + // ([`sub8_leaf_chroma422`], measured on the pinned t422 fixture frame 0: + // groups (18,54)/(30,42) code U+V on both odd-column leaves), never a + // shared group unit. Inter frames code no intrabc, so `intrabc_dv` is + // `None` here. + if ss_x(fctx) == 1 && ss_y(fctx) == 0 { + sub8_leaf_chroma422( + dec, cdfs, neighbours, lmi, uv_mode, 0, alpha, skip, + None, y, u, v, scan4, base_q_idx, + neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), + (bw, bh), fctx, + )?; return Ok(mode); } // The group's single 4x4 chroma unit, intra-predicted from THIS @@ -38292,18 +38551,30 @@ fn decode_inter_block8( // bookkeeping. Always `false` outside a `TxMode::Select` inter frame. let split8; const SIDE: usize = 8; - // lane-av1-444: this 8x8 leaf's chroma plane block is `8 >> ss` a side -- - // 4 at 4:2:0 (one TX_4X4 unit) and the full 8 at 4:4:4, where libaom - // codes ONE TX_8X8 unit (`av1_get_max_uv_txsize(BLOCK_8X8)` at ss 0/0, - // measured against aomdec's EC_COEFF on `av1-profile1-444.ivf` frame 1: - // plane=1 tx=TX_8X8 per 8x8 block, not a 4:2:0 4x4). - let chroma_side: usize = SIDE >> ss_x(fctx); - let (chroma_set, chroma_scan): (TxbSet, &[u16]) = if chroma_side == 8 { + // lane-av1-444 / lane-av1-422sub8rect: this 8x8 leaf's chroma plane block + // is `ss_size_lookup[BLOCK_8X8][ss_x][ss_y]` -- 4:2:0 BLOCK_4X4 (one + // TX_4X4 unit), 4:4:4 BLOCK_8X8 (ONE TX_8X8 unit, `av1_get_max_uv_txsize` + // at ss 0/0, measured against aomdec's EC_COEFF on `av1-profile1-444.ivf` + // frame 1), and 4:2:2 BLOCK_4X8 (ONE TX_4X8 RECT unit per plane, measured + // on the pinned t422 fixture: frame 1 mi(0,0) reads plane 1 tx_size 5 == + // TX_4X8 at rng 55168 right after the agreed luma TX_8X8 txb_skip -- the + // old `SIDE >> ss_x` square read was the first-divergence defect this + // port fixes). + let chroma_w: usize = SIDE >> ss_x(fctx); + let chroma_h: usize = SIDE >> ss_y(fctx); + let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; + let (chroma_set, chroma_scan): (TxbSet, &[u16]) = if chroma_422 { + // `get_txsize_entropy_ctx(TX_4X8)`: the rect fills its plane block, + // so the tables are the rect set and the SCAN_4X8 walk, exactly the + // pairing the key-frame BLOCK_4X8 arm (`decode_block`'s lane-av1-422f + // table) reads byte-exact. + (TxbSet::ChromaRect8x4, &SCAN_4X8[..]) + } else if chroma_w == 8 { (TxbSet::Chroma8, scan8) } else { (TxbSet::Chroma4, scan4) }; - let chroma_tx = if chroma_side == 8 { TX8 } else { TX4 }; + let chroma_tx = if chroma_w == 8 { TX8 } else { TX4 }; let (r, c) = outer_at; // lane-inter8 r2: this leaf's OWN mi cell, not the enclosing 16x16's -- @@ -38804,13 +39075,13 @@ fn decode_inter_block8( }; if let Some(mask_y) = mask_y { mc::blend_masked_compound( - &inter0_y, &inter1_y, mask_y, SIDE, SIDE, SIDE, false, &mut pred_y, fctx, + &inter0_y, &inter1_y, mask_y, SIDE, SIDE, SIDE, 0, 0, &mut pred_y, fctx, ); } else { mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y, fctx); } - let mut inter0_u = vec![0i32; chroma_side * chroma_side]; + let mut inter0_u = vec![0i32; chroma_w * chroma_h]; mc::predict_compound_intermediate( &pu0.data, pu0.width, @@ -38819,13 +39090,13 @@ fn decode_inter_block8( mv_to_q4(cpx, mv0.1, ss_x(fctx)), mv_to_q4(cpy, mv0.0, ss_x(fctx)), scale0, - chroma_side, - chroma_side, + chroma_w, + chroma_h, h_filter, v_filter, &mut inter0_u, fctx, ); - let mut inter1_u = vec![0i32; chroma_side * chroma_side]; + let mut inter1_u = vec![0i32; chroma_w * chroma_h]; mc::predict_compound_intermediate( &pu1.data, pu1.width, @@ -38834,29 +39105,30 @@ fn decode_inter_block8( mv_to_q4(cpx, mv1.1, ss_x(fctx)), mv_to_q4(cpy, mv1.0, ss_x(fctx)), scale1, - chroma_side, - chroma_side, + chroma_w, + chroma_h, h_filter, v_filter, &mut inter1_u, fctx, ); - let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_side * chroma_side); + let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_w * chroma_h); if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_u, &inter1_u, mask_y, SIDE, - chroma_side, - chroma_side, - ss_x(fctx) == 1 && ss_y(fctx) == 1, + chroma_w, + chroma_h, + ss_x(fctx), +ss_y(fctx), &mut pred_u, fctx, ); } else { mc::combine_compound(&inter0_u, &inter1_u, fwd_offset, bck_offset, &mut pred_u, fctx); } - let mut inter0_v = vec![0i32; chroma_side * chroma_side]; + let mut inter0_v = vec![0i32; chroma_w * chroma_h]; mc::predict_compound_intermediate( &pv0.data, pv0.width, @@ -38865,13 +39137,13 @@ fn decode_inter_block8( mv_to_q4(cpx, mv0.1, ss_x(fctx)), mv_to_q4(cpy, mv0.0, ss_x(fctx)), scale0, - chroma_side, - chroma_side, + chroma_w, + chroma_h, h_filter, v_filter, &mut inter0_v, fctx, ); - let mut inter1_v = vec![0i32; chroma_side * chroma_side]; + let mut inter1_v = vec![0i32; chroma_w * chroma_h]; mc::predict_compound_intermediate( &pv1.data, pv1.width, @@ -38880,22 +39152,23 @@ fn decode_inter_block8( mv_to_q4(cpx, mv1.1, ss_x(fctx)), mv_to_q4(cpy, mv1.0, ss_x(fctx)), scale1, - chroma_side, - chroma_side, + chroma_w, + chroma_h, h_filter, v_filter, &mut inter1_v, fctx, ); - let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_side * chroma_side); + let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_w * chroma_h); if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_v, &inter1_v, mask_y, SIDE, - chroma_side, - chroma_side, - ss_x(fctx) == 1 && ss_y(fctx) == 1, + chroma_w, + chroma_h, + ss_x(fctx), +ss_y(fctx), &mut pred_v, fctx, ); } else { @@ -38910,8 +39183,8 @@ fn decode_inter_block8( push_pred(fctx, build); ( Pred::Cur { plane: 0, stride: SIDE, off: 0 }, - Pred::Cur { plane: 1, stride: chroma_side, off: 0 }, - Pred::Cur { plane: 2, stride: chroma_side, off: 0 }, + Pred::Cur { plane: 1, stride: chroma_w, off: 0 }, + Pred::Cur { plane: 2, stride: chroma_w, off: 0 }, ) } else { let mut built: [Vec; 3] = Default::default(); @@ -38920,8 +39193,8 @@ fn decode_inter_block8( (pred_y, pred_u, pred_v) = (by, bu, bv); ( Pred::Inline(&pred_y, SIDE), - Pred::Inline(&pred_u, chroma_side), - Pred::Inline(&pred_v, chroma_side), + Pred::Inline(&pred_u, chroma_w), + Pred::Inline(&pred_v, chroma_w), ) }; split8 = read_block_tx_size( @@ -38938,25 +39211,43 @@ fn decode_inter_block8( .is_some(); if skip { push_mc(0, px, py, SIDE, sy, &ZERO_RESIDUAL[..SIDE * SIDE], fctx); - push_mc(1, + // lane-av1-422sub8rect: the plane block is the (chroma_w, + // chroma_h) rect at 4:2:2 -- the recorded write is + // stride `chroma_w`, full `chroma_h` rows. + push_mc_rect(1, cpx, cpy, - chroma_side, + chroma_w, + chroma_w, + chroma_h, su, - &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx, + &ZERO_RESIDUAL[..chroma_w * chroma_h], fctx, ); - push_mc(2, + push_mc_rect(2, cpx, cpy, - chroma_side, + chroma_w, + chroma_w, + chroma_h, sv, - &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx, + &ZERO_RESIDUAL[..chroma_w * chroma_h], fctx, ); luma_grid = Grid::Zero(SIDE * SIDE); - u_grid = Grid::Zero(chroma_side * chroma_side); - v_grid = Grid::Zero(chroma_side * chroma_side); + u_grid = Grid::Zero(chroma_w * chroma_h); + v_grid = Grid::Zero(chroma_w * chroma_h); } else { let around = neighbours.around_mi(leaf_mi, 8); + // lane-av1-422sub8rect: the 4:2:2 chroma unit is the + // TX_4X8 rect -- its `txb_skip_ctx` reads per chroma + // 4-px cell ([`Neighbours::around_mi_422_chroma`], the + // key-frame BLOCK_4X8 arm's own context walk), and the + // coefficients go through the inter rect reader with the + // inherited luma tx type. + let chroma_around = if chroma_422 { + neighbours.around_mi_422_chroma(leaf_mi, 8, 8) + } else { + around + }; let (grid8, luma_tx_type) = read_inter_luma8( dec, cdfs, @@ -38974,42 +39265,82 @@ fn decode_inter_block8( split8, fctx, )?; luma_grid = grid8; - u_grid = read_inter_plane( - dec, - cdfs, - chroma_set, - chroma_scan, - 1, - around[1], - mode_for_tx, - u, - cpx, - cpy, - chroma_side, - base_q_idx, - su, - Some(luma_tx_type), - None, fctx, - )? - .0; - v_grid = read_inter_plane( - dec, - cdfs, - chroma_set, - chroma_scan, - 2, - around[2], - mode_for_tx, - v, - cpx, - cpy, - chroma_side, - base_q_idx, - sv, - Some(luma_tx_type), - None, fctx, - )? - .0; + u_grid = if chroma_422 { + read_inter_plane_rect( + dec, + cdfs, + chroma_set, + (chroma_w, chroma_h), + chroma_w, + 1, + chroma_around[1], + mode_for_tx, + u, + cpx, + cpy, + su, + Some(luma_tx_type), + None, fctx, + )? + .0 + } else { + read_inter_plane( + dec, + cdfs, + chroma_set, + chroma_scan, + 1, + chroma_around[1], + mode_for_tx, + u, + cpx, + cpy, + chroma_w, + base_q_idx, + su, + Some(luma_tx_type), + None, fctx, + )? + .0 + }; + v_grid = if chroma_422 { + read_inter_plane_rect( + dec, + cdfs, + chroma_set, + (chroma_w, chroma_h), + chroma_w, + 2, + chroma_around[2], + mode_for_tx, + v, + cpx, + cpy, + sv, + Some(luma_tx_type), + None, fctx, + )? + .0 + } else { + read_inter_plane( + dec, + cdfs, + chroma_set, + chroma_scan, + 2, + chroma_around[2], + mode_for_tx, + v, + cpx, + cpy, + chroma_w, + base_q_idx, + sv, + Some(luma_tx_type), + None, fctx, + )? + .0 + }; } // lane-inter8 r2: the COMPOUND leaf used to return here // WITHOUT the end-of-function `record_mi`/`fill_lf_grid` the @@ -39495,7 +39826,7 @@ fn decode_inter_block8( SIDE, SIDE, SIDE, - chroma_side, + chroma_w, px, py, cpx, @@ -39503,6 +39834,7 @@ fn decode_inter_block8( refpix, interp_fixed, frame_width, + (ss_x(fctx), ss_y(fctx)), )?) } else { None @@ -39519,15 +39851,15 @@ fn decode_inter_block8( return; }; let mut pred_y = refill(std::mem::take(&mut out[0]), SIDE * SIDE); - let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_side * chroma_side); - let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_side * chroma_side); + let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_w * chroma_h); + let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_w * chroma_h); // Merge of lane-gmaffine (this leaf's own switchable filters) with // lane-scaledref (spec 7.11.3.3's scaled walk): same kernels either // way, only the sample walk differs. - for (plane, x, y, dim, dst) in [ - (sref_y, px, py, SIDE, &mut pred_y), - (sref_u, cpx, cpy, chroma_side, &mut pred_u), - (sref_v, cpx, cpy, chroma_side, &mut pred_v), + for (plane, x, y, (dim_w, dim_h), dst) in [ + (sref_y, px, py, (SIDE, SIDE), &mut pred_y), + (sref_u, cpx, cpy, (chroma_w, chroma_h), &mut pred_u), + (sref_v, cpx, cpy, (chroma_w, chroma_h), &mut pred_v), ] { let luma = std::ptr::eq(plane, sref_y); if luma_scale == mc::REF_NO_SCALE { @@ -39538,8 +39870,8 @@ fn decode_inter_block8( plane.true_height, mv_to_q4(x, mv.1, if luma { 0 } else { ss_x(fctx) }), mv_to_q4(y, mv.0, if luma { 0 } else { ss_y(fctx) }), - dim, - dim, + dim_w, + dim_h, h_filter, v_filter, dst, fctx, @@ -39553,8 +39885,8 @@ fn decode_inter_block8( mv_to_q4(x, mv.1, if luma { 0 } else { ss_x(fctx) }), mv_to_q4(y, mv.0, if luma { 0 } else { ss_y(fctx) }), luma_scale, - dim, - dim, + dim_w, + dim_h, h_filter, v_filter, dst, fctx, @@ -39576,19 +39908,21 @@ fn decode_inter_block8( SIDE as i32, SIDE as i32, 0, 0, fctx, ); // `av1_init_warp_params`'s per-plane `block_width/height < 8` - // bail-out: an 8x8 leaf's chroma plane is 4x4 in 420, so it is - // predicted translationally even when the luma warps. + // bail-out: an 8x8 leaf's chroma plane block is narrower than 8 + // at any subsampled format (4:2:0 4x4, 4:2:2 4x8), so it is + // predicted translationally even when the luma warps; 4:4:4's + // 8x8 chroma warps with the luma. #[allow(clippy::absurd_extreme_comparisons)] - if chroma_side >= 8 { + if chroma_w >= 8 { crate::warp::warp_affine( params, &sref_u.data, sref_u.true_width as i32, sref_u.true_height as i32, - sref_u.width as i32, &mut pred_u, cpx as i32, cpy as i32, chroma_side as i32, - chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, + sref_u.width as i32, &mut pred_u, cpx as i32, cpy as i32, chroma_w as i32, + chroma_h as i32, chroma_w as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); crate::warp::warp_affine( params, &sref_v.data, sref_v.true_width as i32, sref_v.true_height as i32, - sref_v.width as i32, &mut pred_v, cpx as i32, cpy as i32, chroma_side as i32, - chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, + sref_v.width as i32, &mut pred_v, cpx as i32, cpy as i32, chroma_w as i32, + chroma_h as i32, chroma_w as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); } } @@ -39600,12 +39934,14 @@ fn decode_inter_block8( // Non-wedge interintra blend, mutually exclusive with OBMC (the // motion_mode symbol is not read for an interintra block). if let Some(ii) = interintra_mode { - interintra_blend(y, px, py, SIDE, SIDE, SIDE, ii, wedge_mask, &mut pred_y, fctx); + interintra_blend(y, px, py, SIDE, SIDE, SIDE, 0, 0, ii, wedge_mask, &mut pred_y, fctx); interintra_blend( - u, cpx, cpy, chroma_side, chroma_side, chroma_side, ii, wedge_mask, &mut pred_u, fctx, + u, cpx, cpy, chroma_w, chroma_h, chroma_w, ss_x(fctx), ss_y(fctx), ii, wedge_mask, + &mut pred_u, fctx, ); interintra_blend( - v, cpx, cpy, chroma_side, chroma_side, chroma_side, ii, wedge_mask, &mut pred_v, fctx, + v, cpx, cpy, chroma_w, chroma_h, chroma_w, ss_x(fctx), ss_y(fctx), ii, wedge_mask, + &mut pred_v, fctx, ); } @@ -39617,8 +39953,8 @@ fn decode_inter_block8( push_pred(fctx, build); ( Pred::Cur { plane: 0, stride: SIDE, off: 0 }, - Pred::Cur { plane: 1, stride: chroma_side, off: 0 }, - Pred::Cur { plane: 2, stride: chroma_side, off: 0 }, + Pred::Cur { plane: 1, stride: chroma_w, off: 0 }, + Pred::Cur { plane: 2, stride: chroma_w, off: 0 }, ) } else { // The interintra predictor reads this block's own above/left @@ -39633,8 +39969,8 @@ fn decode_inter_block8( (pred_y, pred_u, pred_v) = (by, bu, bv); ( Pred::Inline(&pred_y, SIDE), - Pred::Inline(&pred_u, chroma_side), - Pred::Inline(&pred_v, chroma_side), + Pred::Inline(&pred_u, chroma_w), + Pred::Inline(&pred_v, chroma_w), ) }; split8 = read_block_tx_size( @@ -39651,25 +39987,43 @@ fn decode_inter_block8( .is_some(); if skip { push_mc(0, px, py, SIDE, sy, &ZERO_RESIDUAL[..SIDE * SIDE], fctx); - push_mc(1, + // lane-av1-422sub8rect: the plane block is the (chroma_w, + // chroma_h) rect at 4:2:2 -- the recorded write is stride + // `chroma_w`, full `chroma_h` rows. + push_mc_rect(1, cpx, cpy, - chroma_side, + chroma_w, + chroma_w, + chroma_h, su, - &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx, + &ZERO_RESIDUAL[..chroma_w * chroma_h], fctx, ); - push_mc(2, + push_mc_rect(2, cpx, cpy, - chroma_side, + chroma_w, + chroma_w, + chroma_h, sv, - &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx, + &ZERO_RESIDUAL[..chroma_w * chroma_h], fctx, ); luma_grid = Grid::Zero(SIDE * SIDE); - u_grid = Grid::Zero(chroma_side * chroma_side); - v_grid = Grid::Zero(chroma_side * chroma_side); + u_grid = Grid::Zero(chroma_w * chroma_h); + v_grid = Grid::Zero(chroma_w * chroma_h); } else { let around = neighbours.around_mi(leaf_mi, 8); + // lane-av1-422sub8rect: the 4:2:2 chroma unit is the TX_4X8 + // rect -- its `txb_skip_ctx` reads per chroma 4-px cell + // ([`Neighbours::around_mi_422_chroma`], the key-frame + // BLOCK_4X8 arm's own context walk), and the coefficients go + // through the inter rect reader with the inherited luma tx + // type. + let chroma_around = if chroma_422 { + neighbours.around_mi_422_chroma(leaf_mi, 8, 8) + } else { + around + }; let (grid8, luma_tx_type) = read_inter_luma8( dec, cdfs, @@ -39687,42 +40041,82 @@ fn decode_inter_block8( split8, fctx, )?; luma_grid = grid8; - u_grid = read_inter_plane( - dec, - cdfs, - chroma_set, - chroma_scan, - 1, - around[1], - mode_for_tx, - u, - cpx, - cpy, - chroma_side, - base_q_idx, - su, - Some(luma_tx_type), - None, fctx, - )? - .0; - v_grid = read_inter_plane( - dec, - cdfs, - chroma_set, - chroma_scan, - 2, - around[2], - mode_for_tx, - v, - cpx, - cpy, - chroma_side, - base_q_idx, - sv, - Some(luma_tx_type), - None, fctx, - )? - .0; + u_grid = if chroma_422 { + read_inter_plane_rect( + dec, + cdfs, + chroma_set, + (chroma_w, chroma_h), + chroma_w, + 1, + chroma_around[1], + mode_for_tx, + u, + cpx, + cpy, + su, + Some(luma_tx_type), + None, fctx, + )? + .0 + } else { + read_inter_plane( + dec, + cdfs, + chroma_set, + chroma_scan, + 1, + chroma_around[1], + mode_for_tx, + u, + cpx, + cpy, + chroma_w, + base_q_idx, + su, + Some(luma_tx_type), + None, fctx, + )? + .0 + }; + v_grid = if chroma_422 { + read_inter_plane_rect( + dec, + cdfs, + chroma_set, + (chroma_w, chroma_h), + chroma_w, + 2, + chroma_around[2], + mode_for_tx, + v, + cpx, + cpy, + sv, + Some(luma_tx_type), + None, fctx, + )? + .0 + } else { + read_inter_plane( + dec, + cdfs, + chroma_set, + chroma_scan, + 2, + chroma_around[2], + mode_for_tx, + v, + cpx, + cpy, + chroma_w, + base_q_idx, + sv, + Some(luma_tx_type), + None, fctx, + )? + .0 + }; } } else { let mode = dec.symbol(&mut cdfs.y_mode[SIZE_GROUP_8]); @@ -39932,17 +40326,17 @@ fn decode_inter_block8( PaletteY { size: n, colors, map } }); let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { - // `av1_get_plane_block_size(BLOCK_8X8, 1, 1)` at the sequence's - // own subsampling: BLOCK_4X4 at 4:2:0, BLOCK_8X8 at 4:4:4 - // (`chroma_side`, lane-av1-444). + // `av1_get_plane_block_size(BLOCK_8X8, ss)` at the sequence's own + // subsampling: BLOCK_4X4 at 4:2:0, BLOCK_8X8 at 4:4:4, and at + // 4:2:2 the BLOCK_4X8 plane block (lane-av1-422sub8rect). let on = palette_onscreen_uv( (SIDE, SIDE), palette_onscreen(rmi, cmi, SIDE, SIDE, fctx), - (chroma_side, chroma_side), + (chroma_w, chroma_h), (ss_x(fctx), ss_y(fctx)), ); let map = - decode_color_index_map_wh(dec, cdfs, n, chroma_side, chroma_side, on, true); + decode_color_index_map_wh(dec, cdfs, n, chroma_w, chroma_h, on, true); hit!(PALETTE_UV_HITS); hit!(INTRA_IN_INTER_PALETTE_UV_HITS); hit_do! { @@ -40113,14 +40507,18 @@ fn decode_inter_block8( if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - push_intra(1, + // lane-av1-422sub8rect: the plane block is the (chroma_w, + // chroma_h) rect at 4:2:2 -- the recorded prediction/write uses + // the rect push. + push_intra_rect(1, cpx, cpy, - chroma_side, + chroma_w, + chroma_h, uv_predict_mode, angle_delta_uv, reach, - &ZERO_RESIDUAL[..chroma_side * chroma_side], + &ZERO_RESIDUAL[..chroma_w * chroma_h], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, @@ -40128,21 +40526,22 @@ fn decode_inter_block8( if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - push_intra(2, + push_intra_rect(2, cpx, cpy, - chroma_side, + chroma_w, + chroma_h, uv_predict_mode, angle_delta_uv, reach, - &ZERO_RESIDUAL[..chroma_side * chroma_side], + &ZERO_RESIDUAL[..chroma_w * chroma_h], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); luma_grid = Grid::Zero(SIDE * SIDE); - u_grid = Grid::Zero(chroma_side * chroma_side); - v_grid = Grid::Zero(chroma_side * chroma_side); + u_grid = Grid::Zero(chroma_w * chroma_h); + v_grid = Grid::Zero(chroma_w * chroma_h); } else { let around = neighbours.around_mi(leaf_mi, 8); luma_grid = if split8 { @@ -40186,56 +40585,89 @@ fn decode_inter_block8( )? }; let ac = alpha.map(|_| cfl_src(px, py, SIDE)); + // lane-av1-422sub8rect: at 4:2:2 the plane block is the BLOCK_4X8 + // rect and the key-frame arm's own reader applies -- one + // TX_4X8 `ChromaRect8x4` unit per plane through + // [`read_rect_chroma_unit`] with the per-chroma-cell context + // walk ([`Neighbours::around_mi_422_chroma`], the + // lane-av1-422f model this leaf's key-frame twin proves + // byte-exact). 4:2:0/4:4:4 keep the square [`read_plane`] read. + let (chroma_around_u, chroma_around_v) = if chroma_422 { + let ca = neighbours.around_mi_422_chroma(leaf_mi, 8, 8); + (ca[1], ca[2]) + } else { + (around[1], around[2]) + }; if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u_grid = read_plane( - dec, - cdfs, - chroma_set, - chroma_scan, - 1, - around[1], - mode, - uv_predict_mode, - angle_delta_uv, - reach, - u, - cpx, - cpy, - chroma_side, - chroma_tx, - base_q_idx, - alpha.zip(ac).map(|((au, _), ac)| (au, ac)), - None, - None, - smooth_neighbor_uv, fctx, - )?; + u_grid = if chroma_422 { + read_rect_chroma_unit( + dec, cdfs, chroma_set, chroma_scan, chroma_w, chroma_h, 1, + chroma_around_u, uv_predict_mode, angle_delta_uv, reach, u, + cpx, cpy, base_q_idx, None, + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), + smooth_neighbor_uv, fctx, + )? + } else { + read_plane( + dec, + cdfs, + chroma_set, + chroma_scan, + 1, + chroma_around_u, + mode, + uv_predict_mode, + angle_delta_uv, + reach, + u, + cpx, + cpy, + chroma_w, + chroma_tx, + base_q_idx, + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), + None, + None, + smooth_neighbor_uv, fctx, + )? + }; if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v_grid = read_plane( - dec, - cdfs, - chroma_set, - chroma_scan, - 2, - around[2], - mode, - uv_predict_mode, - angle_delta_uv, - reach, - v, - cpx, - cpy, - chroma_side, - chroma_tx, - base_q_idx, - alpha.zip(ac).map(|((_, av), ac)| (av, ac)), - None, - None, - smooth_neighbor_uv, fctx, - )?; + v_grid = if chroma_422 { + read_rect_chroma_unit( + dec, cdfs, chroma_set, chroma_scan, chroma_w, chroma_h, 2, + chroma_around_v, uv_predict_mode, angle_delta_uv, reach, v, + cpx, cpy, base_q_idx, None, + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), + smooth_neighbor_uv, fctx, + )? + } else { + read_plane( + dec, + cdfs, + chroma_set, + chroma_scan, + 2, + chroma_around_v, + mode, + uv_predict_mode, + angle_delta_uv, + reach, + v, + cpx, + cpy, + chroma_w, + chroma_tx, + base_q_idx, + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), + None, + None, + smooth_neighbor_uv, fctx, + )? + }; } } // lane-txselect: a split leaf's plane-0 neighbour context is per 4x4 diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index eb48b255..2ae94b7c 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -6809,6 +6809,7 @@ fn obmc_prediction( &refpix, Some(fctx.interp_filter.get()), frame_width, + (crate::decode::ss_x(fctx), crate::decode::ss_y(fctx)), ) .ok()?; let mut pred = [ diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index bbffbfc4..a9c96c6c 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -2142,8 +2142,11 @@ pub(crate) fn diffwtd_mask( /// (64-m)*pred1) >> 6` (a plain, unrounded shift, matching the C `>>` on /// `int32_t`) then one final [`round2`] by [`INTER_POST_ROUND`]. `mask` is /// always the LUMA-resolution mask (`mask_stride` == luma block width); -/// `subsampled` selects the 2x2-average chroma read (spec 7.11.3.14 / -/// libaom's `subw == 1 && subh == 1` branch) vs. the direct luma read. +/// `(subw, subh)` are the PLANE's own subsampling shifts and pick the mask +/// read exactly as aom_dsp/blend-a64-mask.c's four `subw`/`subh` branches do: +/// `(0, 0)` the direct luma sample, `(1, 1)` the 2x2 box average, +/// `(1, 0)`/`(0, 1)` the `AOM_BLEND_AVG` pair average on the subsampled axis +/// alone (4:2:2). #[allow(clippy::too_many_arguments)] pub(crate) fn blend_masked_compound( pred0: &[i32], @@ -2152,7 +2155,8 @@ pub(crate) fn blend_masked_compound( mask_stride: usize, w: usize, h: usize, - subsampled: bool, + subw: usize, + subh: usize, dst: &mut [u16], fctx: &crate::decode::FrameCtx, ) { @@ -2162,17 +2166,30 @@ pub(crate) fn blend_masked_compound( let post_round = INTER_POST_ROUND - round_delta(u32::from(crate::decode::bit_depth(fctx))); for i in 0..h { for j in 0..w { - let m = if subsampled { - let idx = (2 * i) * mask_stride + 2 * j; - round2( - i32::from(mask[idx]) - + i32::from(mask[idx + 1]) - + i32::from(mask[idx + mask_stride]) - + i32::from(mask[idx + mask_stride + 1]), - 2, - ) - } else { - i32::from(mask[i * mask_stride + j]) + let m: i32 = match (subw, subh) { + (0, 0) => i32::from(mask[i * mask_stride + j]), + (1, 1) => { + let idx = (2 * i) * mask_stride + 2 * j; + round2( + i32::from(mask[idx]) + + i32::from(mask[idx + 1]) + + i32::from(mask[idx + mask_stride]) + + i32::from(mask[idx + mask_stride + 1]), + 2, + ) + } + // aom_blend_a64_mask_c's `subw == 1 && subh == 0` branch: + // `AOM_BLEND_AVG(mask[2 * j], mask[2 * j + 1])` per row. + (1, 0) => { + let idx = i * mask_stride + 2 * j; + (i32::from(mask[idx]) + i32::from(mask[idx + 1]) + 1) >> 1 + } + // the `subw == 0 && subh == 1` branch: averaged down the + // columns instead. + (_sw, _sh) => { + let idx = (2 * i) * mask_stride + j; + (i32::from(mask[idx]) + i32::from(mask[idx + mask_stride]) + 1) >> 1 + } }; let res = (m * pred0[i * w + j] + (64 - m) * pred1[i * w + j]) >> 6; // `post_round` above: `INTER_POST_ROUND - round_delta` -- see diff --git a/lanes/av1422sub8rect.report.md b/lanes/av1422sub8rect.report.md new file mode 100644 index 00000000..36d96d1e --- /dev/null +++ b/lanes/av1422sub8rect.report.md @@ -0,0 +1,132 @@ +# lane-av1-422sub8rect — the sub-8x8 inter 4:2:2 chroma ported to its real +# shapes (`decode_inter_block8`'s whole chroma half to the BLOCK_4X8 rect; +# `decode_inter_sub8_split4`'s group unit replaced by the per-piece +# odd-column chroma-reference model the oracle traces actually code); frame +# 0 stays byte-exact at all four stages, the 4:2:0/4:4:4 identity holds by +# ss-keying, and the inter-frame divergence is REMEASURED (reduced ~15% on +# frames 1..10, exactness NOT claimed -- other 4:2:2 classes remain) + +## Outcome + +1. ROOT-CAUSE CONFIRMATION FIRST (pinned fixture `/tmp/t422.obu` sha256 + `d3fa4beeb1309033e4a03f5ae5c431c8fa8386ba1ada088684710423c7938129`, + parent-lane instruments in `/tmp/i422/`, local `EC_AV1_ALLOW_422_PROBE` + bypass applied and reverted byte-identical after — `git diff` carries + only decode.rs/mc.rs/encode.rs changes): the oracle's frame-1 first + block (`AOMMB mi=(0,0) ... skip=0`) reads luma TX_8X8 then + `plane=1 tx_size=5 mi_row=0 mi_col=0 rng=55168` — tx_size 5 IS TX_4X8, + confirming the charter: the 8x8 inter leaf's chroma plane block is + BLOCK_4X8, one TX_4X8 rect unit per plane (`ss_size_lookup[BLOCK_8X8][1][0]`), + while ours read a TX_4X4 square. +2. SPLIT4 MODEL CORRECTED AGAINST THE ORACLE, NOT THE CHARTER'S GUESS: the + parent report assumed the split4 group codes ONE rect group unit; the + traces prove otherwise. At ss (1,0) `is_chroma_reference` for a BLOCK_4X4 + piece is `(mi_col & 0x01)` — ODD columns only (av1_common_int.h:1459) — + and every non-skipped odd-column piece codes its OWN U then V TX_4X4 + square unit right after its luma, anchored at the FLOORED chroma cell + (`(mi_col & !1) * MI >> ss_x`, the lane-av1-422kf2 rule) with + `around_mi_422_chroma`-style per-chroma-cell ctx (measured ctx 8 = + above+left+7 at frame 1's mi(12,3)): + - frame 0 key-frame groups (18,54) and (30,42): U+V read on BOTH + odd-column leaves ((18,55)+(19,55), (30,43)+(31,43)); + - frame 1 inter group at mi(12,2): U+V on leaf (12,3) ALONE — its + bottom-right twin was skipped, so it codes nothing. + The 4:2:0 shared group unit simply never exists at 4:2:2. +3. PORT (all keyed on ss, 4:2:0/4:4:4 byte-identical by construction): + - `decode_inter_block8` chroma half to per-axis shapes: `chroma_w/chroma_h`, + `(TxbSet::ChromaRect8x4, SCAN_4X8)` at 4:2:2; inter coefficient reads + through `read_inter_plane_rect` (offset-7 ctx — the unit IS the plane + block — inherited luma tx type) with `around_mi_422_chroma`; MC + prediction (single-ref, scaled, warp, compound-intermediate), the + wedge-masked blend, interintra blend, OBMC pushes and skip pushes all + (4, 8)-shaped; the intra-in-inter arm reads the key-frame arm's own + rect reader (`read_rect_chroma_unit`, ctx offset None, CfL carried); + palette UV map dims per axis. + - `decode_inter_sub8_split4`: per-piece arm added after each inter + piece's luma (odd column, ss 1,0): prediction as + `build_inter_predictors_sub8x8` — left 2x4 half from the LEFT piece's + own ref/mv/filters, right 2x4 half from this piece's; a mixed group + (left piece intra/off-frame, `is_sub8x8_inter` false) predicts the + whole 4x4 from this piece; skip pieces still predict but record zeros. + Coefficients via square `TxbSet::Chroma4` reads at the floored anchor, + per-unit `record_mi_chroma` (8x4 luma span). Intra pieces gate + `has_chroma` on the piece's own column parity and route through + [`sub8_leaf_chroma422`]; even-column intra pieces publish DC to the + uv-mode map. The 4:2:0 group tail (build, read, band stamps, DC map + stamp) is gated OFF at 4:2:2. + - Shared helpers made ss-exact (4:2:0/4:4:4 reduce to the old behaviour): + `blend_masked_compound`'s `subsampled: bool` → per-axis `(subw, subh)` + mirroring aom_dsp/blend-a64-mask.c's four branches; `interintra_blend`'s + wedge mask read likewise; `obmc_plan`/`obmc_run` take the plane ss — + chroma extents are the luma rules `>> ss` (`av1_skip_u4x4_pred_in_obmc` + keyed on the plane block, above-pass skip and left-pass blend extents), + `TxParams::strided` sized `stride * h` (identical when h == stride, as + every pre-existing caller has). The encoder's `obmc_plan` call passes + the fctx ss. + - STOP-NOTE (owning parent-lane surface, named not fixed): + `decode_inter_block`'s 4:2:2 chroma is still the square-cut model; its + chroma masked-blend keeps the pre-port direct mask read there (guarded + by `ss_x == ss_y`), because its `chroma_side`-square buffers cannot be + indexed by the subsampled-axis walks. Known-divergent, unchanged + behaviour, named for the owning lane. +4. MEASURED after the port (17-frame probe run under the reverted bypass; + comparator: ours padded 256x160/128x160 u8 cropped vs the instrumented + `$HOME/.cache/aom-oracle/build` aomdec): + - frame 0: **byte-exact at PREFILT, POSTCDEF (and FINAL by the u16 + comparator) on Y, U and V — 0 diffs everywhere**, so the rect read, + the split4 per-piece model and every shared-helper change are exact on + everything frame 0 exercises (including the split groups (18,54), + (30,42) that motivated the per-piece model). + - inter frames 1..10 decode and were measured: PREFILT diffs dropped + ~15% vs the parent lane's ladder (e.g. frame 1: 30825/12624/12796 → + 22248/11733/11974; frame 8: 35014/11020/12043 → 32855/11020/12043; + per-frame values in `/tmp/i422/sub8rect/`). **Frame exactness is NOT + claimed** — frames 1..10 remain divergent: the named sub-8 sites are + fixed, but the stream carries further 4:2:2 classes the decoder still + square-cuts or gaps (probe trace: `EC_RECTCHROMA_GAP luma=16x8 + tx=16x8 chroma=8x8`, `luma=8x32 ... chroma=4x32`, i.e. `decode_inter_block`'s + own 4:2:2 chroma half), so the entropy walk desyncs again after the + first uncoded-for block. + - frames 11..16 were not reached by the probe's frame accounting this + round (11 dumped frames, decode exits 0 — the dump-index vs + show-existing accounting needs the parent-lane harness's + 1-frame-per-OBU walk); the ladder for 1..16 is the owning lane's + re-measurement once the next class lands. +5. Deferred items: (a) frames 11..16 ladder entries — unblock: rerun the + parent-lane per-OBU cut harness (`/tmp/i422/cut_frames.py`) after the + next class; (b) `decode_inter_sub8_rect2`'s 4:2:2 has_chroma gating (its + inter strips pass `i == 1` where BLOCK_4X8 needs odd-column parity) — + not one of this charter's named sites, unmeasured on the fixture so far. + +## Decision + +- The header refusal STAYS unconditional in `stream.rs`; the bypass was + local-only, applied and reverted byte-identical (0 occurrences left; + `stream::tests` green after the revert). +- 4:2:0/4:4:4 untouched by construction (ss-keyed arms); the 4:2:0 + group-tail SKIP arm untouched; `read_coeffs_rect` untouched. + +## Verification + +- `cargo check -p ec-av1` (target `~/.cache/cargo-target-av1422sub8rect`): + 0 warnings, 0 errors. +- `stream::tests::a_non_420_subsampled_sequence_header_is_refused_by_name`: + 1 passed — the refusal fires after the bypass revert. +- `stream::tests::a_real_aomenc_stream_with_a_skipped_8x8_intra_leaf_ + whose_tx_split_decodes_pixel_exact` under `EC_AV1_REQUIRE_AOMENC=1` + (oracle aomenc): 1 passed — the 4:2:0 inter path, including the + shared-helper ss-ifications, stays pixel-exact. +- Frame-0 stage dumps vs the instrumented aomdec: PREFILT/POSTCDEF + Y=U=V=0 (POSTDEB comparator run this round used the wrong dump-env name; + the post-deblock state is bracketed by the two exact stages). +- Hit counter added: `CHROMA422_INTER_SUB8_HITS` (per-piece inter 4:2:2 + chroma units) — reads non-zero on the fixture's frames 1+, so the new arm + provably fires. + +## State + +- Commit on lane-av1-422sub8rect (no push): the sub-8 rect port + + `TxParams::strided` sizing fix + shared ss-exact helpers + this report. +- Probe instruments regenerated under `/tmp/i422/sub8rect/` (scratch, not + committed); the oracle build's rungs already cover everything needed to + re-measure after the next class.