From d6b7cc75bf1a2565bc3fe06442e70f467362c1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 23 Sep 2026 07:13:26 +0300 Subject: [PATCH] ec-av1: 4:4:4 luma byte-exact, chroma WIP (lane-av1-444) Sequence header refuses only 4:2:2 now (ss_x != ss_y); 4:4:4 and 4:2:0 proceed. Chroma plane size, tx size, CFL window, loop-filter chroma step and the 8x8 leaf's chroma unit follow ss_x/ss_y; an 8x8 leaf at 4:4:4 reads 8x8 chroma. Restoration chroma unit size follows ss_x. Square profile-1 recipe: luma IDENTICAL vs aomdec+ffmpeg both frames (0/16384 diffs); chroma 37389/98304 diffs, first at U (112,0) inside the 64x64 block at (64,0) which takes the single-TU arm but codes four TX_32X32 chroma units at 4:4:4 (av1_get_max_uv_txsize). 420 square arm still IDENTICAL. 4:2:2 stays a named refusal. Full suite not run. --- crates/ec-av1/src/decode.rs | 904 +++++++++++++++---------- crates/ec-av1/src/refusal_inventory.rs | 147 +++- crates/ec-av1/src/restoration.rs | 291 ++++++-- crates/ec-av1/src/stream.rs | 10 +- lanes/av1444.report.md | 99 +++ 5 files changed, 978 insertions(+), 473 deletions(-) create mode 100644 lanes/av1444.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index d73769ba..7d58078b 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -102,6 +102,9 @@ pub(crate) struct FrameCtx { /// (like [`FrameCtx::bit_depth`]), so a mono frame decoded on a thread /// that last saw a 4:2:0 one never inherits the wrong plane count. pub(crate) mono_chrome: std::cell::Cell, + /// lane-av1-444: sequence subsampling_x/y. 1,1 is 4:2:0; 0,0 is 4:4:4. + pub(crate) subsampling_x: std::cell::Cell, + pub(crate) subsampling_y: std::cell::Cell, pub(crate) superres: std::cell::Cell<(u32, u32)>, pub(crate) cdef_transmitted: std::cell::Cell, pub(crate) cdef_sb_cols: std::cell::Cell, @@ -200,6 +203,8 @@ impl FrameCtx { cdef_bits: std::cell::Cell::new(0), bit_depth: std::cell::Cell::new(8), mono_chrome: std::cell::Cell::new(false), + subsampling_x: std::cell::Cell::new(1), + subsampling_y: std::cell::Cell::new(1), superres: std::cell::Cell::new((0, 8)), cdef_transmitted: std::cell::Cell::new(false), cdef_sb_cols: std::cell::Cell::new(0), @@ -296,6 +301,8 @@ pub(crate) fn filter_ctx_copy(fctx: &FrameCtx) -> FrameCtx { let out = FrameCtx::new(); out.bit_depth.set(fctx.bit_depth.get()); out.mono_chrome.set(fctx.mono_chrome.get()); + out.subsampling_x.set(fctx.subsampling_x.get()); + out.subsampling_y.set(fctx.subsampling_y.get()); out.interp_filter.set(fctx.interp_filter.get()); out.grain_bit_depth.set(fctx.grain_bit_depth.get()); out.superres.set(fctx.superres.get()); @@ -618,6 +625,29 @@ pub(crate) fn set_mono(mono_chrome: bool, fctx: &crate::decode::FrameCtx) { fctx.mono_chrome.with(|c| c.set(mono_chrome)); } +/// Sequence subsampling. Set once per frame from stream.rs, like set_mono. +pub(crate) fn set_subsampling(ss_x: u8, ss_y: u8, fctx: &crate::decode::FrameCtx) { + fctx.subsampling_x.set(ss_x); + fctx.subsampling_y.set(ss_y); +} + +#[inline] +pub(crate) fn ss_x(fctx: &crate::decode::FrameCtx) -> usize { + fctx.subsampling_x.get() as usize +} + +#[inline] +pub(crate) fn ss_y(fctx: &crate::decode::FrameCtx) -> usize { + fctx.subsampling_y.get() as usize +} + +/// libaom ROUND_POWER_OF_TWO. At ss=1 equals div_ceil(dim, 2); at ss=0 is dim. +#[inline] +pub(crate) fn round_ss(dim: usize, ss: usize) -> usize { + (dim + ((1usize << ss) >> 1)) >> ss +} + + /// This frame's superres state: `(upscaled_width, superres_denom)`, or /// `(0, 8)` when the frame is not scaled. Spec 7.16's upscale runs /// between CDEF and loop restoration, so LOOP RESTORATION -- unit grid, @@ -2333,17 +2363,14 @@ pub(crate) struct CflSrc { py: usize, bw: usize, bh: usize, - /// Which averager the parse site asked for -- [`cfl_ac_q3_rect`] and - /// [`cfl_ac_q3`] are not the same function at `bw == bh`. - rect: bool, } fn cfl_src(px: usize, py: usize, side: usize) -> CflSrc { - CflSrc { px, py, bw: side, bh: side, rect: false } + CflSrc { px, py, bw: side, bh: side } } fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc { - CflSrc { px, py, bw, bh, rect: true } + CflSrc { px, py, bw, bh } } /// lane-wave1 step (i): the pixel-writing half of block decoding, recorded @@ -3578,11 +3605,7 @@ fn exec_intra( // written for this block -- at parse time it would have been the previous // frame's samples. let ac = cfl.map(|(alpha, src)| { - let sig = if src.rect { - cfl_ac_q3_rect(&*y, src.px, src.py, src.bw, src.bh) - } else { - cfl_ac_q3(&*y, src.px, src.py, src.bw) - }; + let sig = cfl_ac_ss(&*y, src.px, src.py, src.bw, src.bh, ss_x(fctx), ss_y(fctx)); (alpha, sig) }); let cfl = ac.as_ref().map(|(a, sig)| (*a, sig.as_slice())); @@ -6757,7 +6780,9 @@ fn read_coeffs_rect( // mask rather than a hardware divide. let stride = w + 4; let n = stride * (h + 4); - debug_assert!(w.is_power_of_two() && n <= 36 * 36); + if !(w.is_power_of_two() && n <= 36 * 36) { + panic!("rect coeff scratch w={w} h={h} n={n}"); + } let (mut b96, mut b256, mut b768); // Byte levels, same `0..=15` bound [`read_coeffs`] states. let levels: &mut [u8] = if n <= 96 { @@ -7653,8 +7678,8 @@ impl Neighbours { // `av1_get_max_uv_txsize` under 4:2:0: the block's chroma extent is // half its luma one (min 4 px, the smallest transform), capped at // TX_32X32 -- independent of this block's luma `tx_depth`. - let uv_tx_w = ((w_mi * MI / 2).max(4).min(32)) as u8; - let uv_tx_h = ((h_mi * MI / 2).max(4).min(32)) as u8; + let uv_tx_w = (((w_mi * MI) >> ss_x(fctx)).max(4).min(32)) as u8; + let uv_tx_h = (((h_mi * MI) >> ss_y(fctx)).max(4).min(32)) as u8; if crate::envflags::env_flag!("EC_TXGRID_TRACE") { eprintln!("EC_LFGRID mi_row={mi_r} mi_col={mi_c} w_mi={w_mi} h_mi={h_mi} tx_px={tx_px} tx_h_px={tx_h_px}"); } @@ -9453,7 +9478,7 @@ fn read_intra_mode_rect( // BLOCK_4X8, both floored at the 4-px transform. Halving the short // axis to 2 read HALF the colour-index symbols of a 1:4 strip's // chroma palette and desynced the tile from there. - let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); + let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4)); let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch)); let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); hit!(PALETTE_UV_HITS); @@ -9640,8 +9665,8 @@ fn decode_rect_split( ) -> Result<()> { let (mi_r, mi_c) = at_mi; let (px, py) = (mi_c * MI, mi_r * MI); - let (cpx, cpy) = (px / 2, py / 2); - let (chroma_w, chroma_h) = (bw / 2, bh / 2); + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); + let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); // The chroma transform is picked before any symbol is read, so an // unsupported chroma shape refuses before this strip desyncs the tile. let chroma: Option<(TxbSet, &[u16])> = match (chroma_w, chroma_h) { @@ -9694,7 +9719,11 @@ fn decode_rect_split( // caller -- a new strip size would otherwise hit the `expect` below and // panic instead of refusing by name -- and so stays in // `refusal_inventory::REFUSALS`. - if !m.skip && chroma.is_none() { + // A 4:4:4 64-axis strip's chroma plane block is larger than one adjusted + // UV transform (TX_64X32 adjusts to TX_32X32). Those sizes are tiled + // below; everything else still needs a single-unit table. + let chroma_tiled = matches!((chroma_w, chroma_h), (64, 32) | (32, 64) | (64, 16) | (16, 64)); + if !m.skip && chroma.is_none() && !chroma_tiled { return Err(unsupported( "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", )); @@ -10051,6 +10080,80 @@ fn decode_rect_split( } let [ug, vg] = last; (ug, vg) + } else if chroma_tiled { + eprintln!("EC_TILE chroma={chroma_w}x{chroma_h}"); + let (uw, uh, set, scan): (usize, usize, TxbSet, &[u16]) = match (chroma_w, chroma_h) { + (64, 32) | (32, 64) => (32, 32, TxbSet::Chroma32, default_scan(32)), + (64, 16) => (32, 16, TxbSet::ChromaRect32x16, &SCAN_32X16), + (16, 64) => (16, 32, TxbSet::ChromaRect32x16, &SCAN_16X32), + _ => unreachable!("chroma_tiled"), + }; + let (nw, nh) = (chroma_w / uw, chroma_h / uh); + let bigger = chroma_w * chroma_h > uw * uh; + let default_tx = TxType::DctDct; + let mut last = [Grid::Zero(uw * uh), Grid::Zero(uw * uh)]; + for plane_idx in 1..=2 { + for cu_row in 0..nh { + for cu_col in 0..nw { + let luma_span_x = uw << ss_x(fctx); + let luma_span_y = uh << ss_y(fctx); + let cu_mi = ( + mi_r + cu_row * (luma_span_y / MI), + mi_c + cu_col * (luma_span_x / MI), + ); + let cu_around = neighbours.around_mi_rect(cu_mi, luma_span_x, luma_span_y); + let (cu_x, cu_y) = (cpx + cu_col * uw, cpy + cu_row * uh); + let cfl = m.alpha.map(|(au, av)| { + let alpha = if plane_idx == 1 { au } else { av }; + ( + alpha, + cfl_src_rect( + px + cu_col * luma_span_x, + py + cu_row * luma_span_y, + luma_span_x, + luma_span_y, + ), + ) + }); + // 32x32 does not fit read_coeffs_rect's scratch (768). The + // square reader is the path every other 32x32 chroma unit uses. + let levels = if uw == uh { + let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; + read_plane( + dec, cdfs, set, scan, plane_idx, cu_around[plane_idx], + m.mode, m.uv_predict_mode, m.angle_delta_uv, reach, + plane, cu_x, cu_y, uw, uw, base_q_idx, cfl, None, + bigger.then_some(3), m.smooth_neighbor_uv, fctx, + )? + } else { + let skip_ctx = usize::from(cu_around[plane_idx].0) + + usize::from(cu_around[plane_idx].1) + + usize::from(bigger) * 3; + let mut coding = cdfs.txb(set, m.uv_predict_mode); + let (levels, tx_type) = read_coeffs_rect( + dec, &mut coding, scan, uw, uh, skip_ctx, + dc_sign_ctx(cu_around[plane_idx].2), default_tx, + )?; + let residual = dequant_and_inverse_typed_wh( + &levels, uw, uh, crate::decode::bit_depth(fctx), + block_q_idx(fctx), + plane_q_delta(plane_idx, fctx).0, + plane_q_delta(plane_idx, fctx).1, + tx_type, + ); + push_intra_rect( + plane_idx, cu_x, cu_y, uw, uh, + m.uv_predict_mode, m.angle_delta_uv, reach, &residual, + cfl, None, m.smooth_neighbor_uv, fctx, + ); + levels + }; + neighbours.record_mi_chroma(cu_mi, luma_span_x, luma_span_y, plane_idx, &levels); + last[plane_idx - 1] = levels; + } + } + } + (last[0].clone(), last[1].clone()) } else { let (chroma_set, chroma_scan) = chroma.expect("refused above when None"); // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size @@ -10741,7 +10844,7 @@ fn decode_intra_rect_in_inter( PaletteY { size: n, colors, map } }); let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { - let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); + let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4)); let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch)); let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); hit!(PALETTE_UV_HITS); @@ -11063,8 +11166,8 @@ fn decode_block_rect( return Ok(()); } let (tx_w, tx_h) = (bw, bh); - let (cpx, cpy) = (px / 2, py / 2); - let (chroma_w, chroma_h) = (bw / 2, bh / 2); + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); + let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); if crate::envflags::env_flag!("EC_AV1_TRACE") { let (rng, _) = dec.debug_state(); @@ -11152,10 +11255,25 @@ fn decode_block_rect( // (`TxbSet::LumaRect32x16`/`ChromaRect16x8`, see those variants' own // doc comments); the scan order and 2D context tables are the real // rect ones (`SCAN_32X16`/etc, `base_ctx_rect`/`br_ctx_rect`). - let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw == 32 { - (&SCAN_32X16, &SCAN_16X8) + let luma_scan: &[u16] = if bw > bh { &SCAN_32X16 } else { &SCAN_16X32 }; + // Chroma tx follows the plane block, not a hardcoded half. At 4:2:0 a + // 32x16 strip's chroma is 16x8 (ChromaRect16x8); at 4:4:4 it is 32x16 + // (ChromaRect32x16, DCT-only — ADST is undefined on a 32-point axis). + let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (chroma_w, chroma_h) { + (32, 16) => (TxbSet::ChromaRect32x16, &SCAN_32X16), + (16, 32) => (TxbSet::ChromaRect32x16, &SCAN_16X32), + (16, 8) => (TxbSet::ChromaRect16x8, &SCAN_16X8), + (8, 16) => (TxbSet::ChromaRect16x8, &SCAN_8X16), + _ => { + return Err(unsupported( + "a rectangular chroma transform whose size has no coefficient table", + )) + } + }; + let chroma_default_tx = if chroma_w.max(chroma_h) >= 32 { + TxType::DctDct } else { - (&SCAN_16X32, &SCAN_8X16) + default_intra_tx_type(uv_predict_mode as u8) }; // lane-rectwire r3: real coefficients desync a pixel-exact gate // (rectwire-flake-1.obu, seed 55) -- both 32x16 strips of frame 0's @@ -11235,9 +11353,9 @@ fn decode_block_rect( if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); + let u_default_tx = chroma_default_tx; let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); - let mut u_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode); + let mut u_coding = cdfs.txb(chroma_set, uv_predict_mode); let (u_levels, u_tx_type) = read_chroma_coeffs_rect( dec, &mut u_coding, @@ -11279,9 +11397,9 @@ fn decode_block_rect( if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); + let v_default_tx = chroma_default_tx; let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); - let mut v_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode); + let mut v_coding = cdfs.txb(chroma_set, uv_predict_mode); let (v_levels, v_tx_type) = read_chroma_coeffs_rect( dec, &mut v_coding, @@ -11551,8 +11669,8 @@ fn decode_leaf_rect( } let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); - let (cpx, cpy) = (px / 2, py / 2); - let (chroma_w, chroma_h) = (bw / 2, bh / 2); + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); + let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let (luma_levels, u_levels, v_levels); if skip { luma_levels = Grid::Zero(bw * bh); @@ -11587,11 +11705,14 @@ fn decode_leaf_rect( // `use_reduced_set` branch returns `EXT_TX_SET_DTT4_IDTX` at // `tx_size_sqr_up == TX_16X16` regardless of the true, non-square // `tx_size`) and its chroma half (8x4/4x8, `TxbSet::ChromaRect8x4`). - let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw == 16 { + let (luma_scan, chroma_scan): (&[u16], &[u16]) = if ss_x(fctx) == 0 { + if bw == 16 { (&SCAN_16X8, &SCAN_16X8) } else { (&SCAN_8X16, &SCAN_8X16) } + } else if bw == 16 { (&SCAN_16X8, &SCAN_8X4) } else { (&SCAN_8X16, &SCAN_4X8) }; + let chroma_rect_set = if ss_x(fctx) == 0 { TxbSet::ChromaRect16x8 } else { TxbSet::ChromaRect8x4 }; let around = neighbours.around_mi_rect(leaf_mi, bw, bh); let luma_set = if reduced_tx_set { TxbSet::LumaRect16x8 @@ -11621,7 +11742,7 @@ fn decode_leaf_rect( let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); - let mut u_coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); + let mut u_coding = cdfs.txb(chroma_rect_set, uv_predict_mode); let (u_l, u_tx_type) = read_chroma_coeffs_rect( dec, &mut u_coding, chroma_scan, chroma_w, chroma_h, u_skip_ctx, dc_sign_ctx(around[1].2), u_default_tx, fctx, @@ -11640,7 +11761,7 @@ fn decode_leaf_rect( ); let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); - let mut v_coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); + let mut v_coding = cdfs.txb(chroma_rect_set, uv_predict_mode); let (v_l, v_tx_type) = read_chroma_coeffs_rect( dec, &mut v_coding, chroma_scan, chroma_w, chroma_h, v_skip_ctx, dc_sign_ctx(around[2].2), v_default_tx, fctx, @@ -11936,8 +12057,8 @@ fn decode_block_rect4( neighbours.record_mode_mi(mi_r, mi_c, mi_w, mi_h, mode); neighbours.record_uv_mode_mi(mi_r, mi_c, mi_w, mi_h, uv_predict_mode); let (px, py) = (mi_c * MI, mi_r * MI); - let (cpx, cpy) = (px / 2, py / 2); - let (chroma_w, chroma_h) = (bw / 2, bh / 2); + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); + let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw > bh { (&SCAN_32X8, &SCAN_16X4) @@ -12549,8 +12670,8 @@ fn decode_rect4_16_strip( }; let (pw, ph) = if horz { (16, 8) } else { (8, 16) }; let (ppx, ppy) = (pair_mi.1 * MI, pair_mi.0 * MI); - let (cpx, cpy) = (ppx / 2, ppy / 2); - let (cw, ch) = (pw / 2, ph / 2); + let (cpx, cpy) = (ppx >> ss_x(fctx), ppy >> ss_y(fctx)); + let (cw, ch) = (pw >> ss_x(fctx), ph >> ss_y(fctx)); let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; if (V_PRED..=D67_PRED).contains(&uv_predict_mode) { hit!(RECT4_16_CHROMA_DIR_HITS); @@ -12932,8 +13053,8 @@ fn decode_block_rect64( return Ok(()); } let (tx_w, tx_h) = (bw, bh); - let (cpx, cpy) = (px / 2, py / 2); - let (chroma_w, chroma_h) = (bw / 2, bh / 2); + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); + let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); if let Some(ref pyv) = palette_y { let buf: Vec = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); @@ -13112,145 +13233,99 @@ fn decode_block_rect64( leftcol, ); } - // CHROMA: a real, untruncated chroma_w x chroma_h transform -- no - // corner crop needed (see doc comment). - let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); - // The chroma plane is never truncated (both axes <= 32 after - // subsampling). Under a 1:4 strip it is a true 32x8/8x32: - // `get_txsize_entropy_ctx(TX_32X8)` = (TX_8X8 + TX_32X32 + 1) >> 1 = - // TX_16X16 and `txsize_log2_minus4[TX_32X8]` = 4 (256 positions), - // both identical to TX_16X16 -- so [`TxbSet::Chroma16`] is the exact - // set, and only the scan is new (lane-tx64x16). - let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (bw, bh) { - (64, 16) => (TxbSet::Chroma16, &SCAN_32X8), - (16, 64) => (TxbSet::Chroma16, &SCAN_8X32), - (64, _) => (TxbSet::ChromaRect32x16, &SCAN_32X16), - _ => (TxbSet::ChromaRect32x16, &SCAN_16X32), - }; - let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); - let mut u_coding = cdfs.txb(chroma_set, uv_predict_mode); - if coeff_trace_on() { - let (rng, _) = dec.debug_state(); - eprintln!("EC_COEFF plane=1 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); - } - let (u_levels, u_tx_type) = read_chroma_coeffs_rect( - dec, - &mut u_coding, - chroma_scan, - chroma_w, - chroma_h, - u_skip_ctx, - dc_sign_ctx(around[1].2), - TxType::DctDct, - fctx, - )?; - if coeff_trace_on() { - let (rng, _) = dec.debug_state(); - eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); - } - { - let cur = block_q_idx(fctx); - if cur != i32::from(base_q_idx) { - hit!(RECT64_QIDX_DRIFT_HITS); - } - if crate::envflags::env_flag!("EC_AV1_TRACE") { - eprintln!( - "TRACE rect64_dequant plane=1 base_q_idx={base_q_idx} current_q_idx={cur}" - ); - } - } - let u_residual = dequant_and_inverse_typed_wh( - &u_levels, - chroma_w, - chroma_h, - crate::decode::bit_depth(fctx), - block_q_idx(fctx), - plane_q_delta(1, fctx).0, - plane_q_delta(1, fctx).1, - u_tx_type, - ); - // lane-t900 r34: the UV-palette prediction override, exactly as the - // `skip` arm above installs it. The [`PALETTE_PRED`] slot is a - // set-then-take, so it must be filled again right before each plane's - // own reconstruct; leaving this out of the non-skip arm predicted a - // palette block's chroma off its block edges (measured: testsrc2 - // 128x128 10-bit cq55 `--tile-columns=1`, the 32x64 strip at px=96 -- - // luma exact, both chroma planes wrong over its whole 16x32). - if let Some((ub, _)) = &palette_uv_bufs { - set_palette_pred(ub.clone(), fctx); - } - push_intra_rect(1, - cpx, - cpy, - chroma_w, - chroma_h, - uv_predict_mode, - angle_delta_uv, - reach, - &u_residual, - alpha.zip(ac).map(|((au, _), ac)| (au, ac)), - None, - smooth_neighbor_uv, fctx, - ); - let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); - let mut v_coding = cdfs.txb(chroma_set, uv_predict_mode); - if coeff_trace_on() { - let (rng, _) = dec.debug_state(); - eprintln!("EC_COEFF plane=2 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); - } - let (v_levels, v_tx_type) = read_chroma_coeffs_rect( - dec, - &mut v_coding, - chroma_scan, - chroma_w, - chroma_h, - v_skip_ctx, - dc_sign_ctx(around[2].2), - TxType::DctDct, - fctx, - )?; - if coeff_trace_on() { - let (rng, _) = dec.debug_state(); - eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); - } - { - let cur = block_q_idx(fctx); - if cur != i32::from(base_q_idx) { - hit!(RECT64_QIDX_DRIFT_HITS); + // CHROMA. At 4:2:0 the plane block is already one adjusted UV + // transform (<=32 on each axis). At 4:4:4 a 64-axis strip's chroma + // is larger than that, so it is tiled into 32-capped units. + let _ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); + let (uw, uh) = (chroma_w.min(32), chroma_h.min(32)); + let (nw, nh) = (chroma_w / uw, chroma_h / uh); + let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (uw, uh) { + (32, 32) => (TxbSet::Chroma32, default_scan(32)), + (32, 16) => (TxbSet::ChromaRect32x16, &SCAN_32X16), + (16, 32) => (TxbSet::ChromaRect32x16, &SCAN_16X32), + (32, 8) => (TxbSet::Chroma16, &SCAN_32X8), + (8, 32) => (TxbSet::Chroma16, &SCAN_8X32), + _ => { + return Err(unsupported( + "a 64-axis strip whose chroma unit has no coefficient table", + )) } - if crate::envflags::env_flag!("EC_AV1_TRACE") { - eprintln!( - "TRACE rect64_dequant plane=2 base_q_idx={base_q_idx} current_q_idx={cur}" - ); + }; + let bigger = nw * nh > 1; + let mut u_levels = vec![0i32; chroma_w * chroma_h]; + let mut v_levels = vec![0i32; chroma_w * chroma_h]; + for plane_idx in 1..=2 { + for cu_row in 0..nh { + for cu_col in 0..nw { + let cu_around = if nw == 1 && nh == 1 { + around + } else { + let lsx = uw << ss_x(fctx); + let lsy = uh << ss_y(fctx); + let cu_mi = ( + mi_r + cu_row * (lsy / MI), + mi_c + cu_col * (lsx / MI), + ); + neighbours.around_mi_rect(cu_mi, lsx, lsy) + }; + let lsx = uw << ss_x(fctx); + let lsy = uh << ss_y(fctx); + let cfl = alpha.map(|(au, av)| { + let a = if plane_idx == 1 { au } else { av }; + (a, cfl_src_rect(px + cu_col * lsx, py + cu_row * lsy, lsx, lsy)) + }); + let levels = if uw == uh { + let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; + if let Some((ub, vb)) = &palette_uv_bufs { + set_palette_pred((if plane_idx == 1 { ub } else { vb }).clone(), fctx); + } + read_plane( + dec, cdfs, chroma_set, chroma_scan, plane_idx, cu_around[plane_idx], + mode, uv_predict_mode, angle_delta_uv, reach, plane, + cpx + cu_col * uw, cpy + cu_row * uh, uw, uw, base_q_idx, + cfl, None, bigger.then_some(3), smooth_neighbor_uv, fctx, + )? + } else { + let skip_ctx = usize::from(cu_around[plane_idx].0) + + usize::from(cu_around[plane_idx].1) + + usize::from(bigger) * 3; + let mut coding = cdfs.txb(chroma_set, uv_predict_mode); + let (levels, tx_type) = read_chroma_coeffs_rect( + dec, &mut coding, chroma_scan, uw, uh, skip_ctx, + dc_sign_ctx(cu_around[plane_idx].2), TxType::DctDct, fctx, + )?; + let residual = dequant_and_inverse_typed_wh( + &levels, uw, uh, crate::decode::bit_depth(fctx), + block_q_idx(fctx), + plane_q_delta(plane_idx, fctx).0, + plane_q_delta(plane_idx, fctx).1, + tx_type, + ); + if let Some((ub, vb)) = &palette_uv_bufs { + set_palette_pred((if plane_idx == 1 { ub } else { vb }).clone(), fctx); + } + push_intra_rect( + plane_idx, cpx + cu_col * uw, cpy + cu_row * uh, uw, uh, + uv_predict_mode, angle_delta_uv, reach, &residual, + cfl, None, smooth_neighbor_uv, fctx, + ); + levels + }; + let dst = if plane_idx == 1 { &mut u_levels } else { &mut v_levels }; + for row in 0..uh { + let d = (cu_row * uh + row) * chroma_w + cu_col * uw; + dst[d..d + uw].copy_from_slice(&levels[row * uw..][..uw]); + } + if bigger { + neighbours.record_mi_chroma( + (mi_r + cu_row * (lsy / MI), mi_c + cu_col * (lsx / MI)), + lsx, lsy, plane_idx, &levels, + ); + } + } } } - let v_residual = dequant_and_inverse_typed_wh( - &v_levels, - chroma_w, - chroma_h, - crate::decode::bit_depth(fctx), - block_q_idx(fctx), - plane_q_delta(2, fctx).0, - plane_q_delta(2, fctx).1, - v_tx_type, - ); - if let Some((_, vb)) = &palette_uv_bufs { - set_palette_pred(vb.clone(), fctx); - } - push_intra_rect(2, - cpx, - cpy, - chroma_w, - chroma_h, - uv_predict_mode, - angle_delta_uv, - reach, - &v_residual, - alpha.zip(ac).map(|((_, av), ac)| (av, ac)), - None, - smooth_neighbor_uv, fctx, - ); - neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); + neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); hit!(RECT_COEFF_HITS); } let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); @@ -13619,9 +13694,9 @@ fn read_intra_mode( let on = palette_onscreen_uv( (side, side), palette_onscreen(mi_r, mi_c, side, side, fctx), - (side / 2, side / 2), + (side >> ss_x(fctx), side >> ss_y(fctx)), ); - let map = decode_color_index_map(dec, cdfs, n, side / 2, on, true); + let map = decode_color_index_map(dec, cdfs, n, side >> ss_x(fctx), on, true); hit!(PALETTE_UV_HITS); if trace { eprintln!( @@ -13692,6 +13767,38 @@ fn cfl_ac_q3(y: &PlaneBuf<'_>, px: usize, py: usize, side: usize) -> Vec { /// (lane-av1cfl) builds a CfL candidate's AC signal from its own `u8` /// reconstruction through THIS function rather than a second transcription of /// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c`. +/// CfL AC at the frame's subsampling. 4:2:0 keeps the 2x2 average; 4:4:4 +/// copies each luma sample shifted to Q3 (`cfl_luma_subsampling_444_lbd`). +fn cfl_ac_ss( + y: &PlaneBuf<'_>, + px: usize, + py: usize, + bw: usize, + bh: usize, + ss_x: usize, + ss_y: usize, +) -> Vec { + if ss_x == 0 && ss_y == 0 { + let mut ac = vec![0i32; bw * bh]; + let mut sum = 0i32; + for row in 0..bh { + for col in 0..bw { + let q3 = i32::from(y.data[(py + row) * y.width + px + col]) << 3; + ac[row * bw + col] = q3; + sum += q3; + } + } + let num_pel = (bw * bh) as i32; + let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); + ac.iter_mut().for_each(|v| *v -= avg); + ac + } else if bw == bh { + cfl_ac_q3(y, px, py, bw) + } else { + cfl_ac_q3_rect(y, px, py, bw, bh) + } +} + pub(crate) fn cfl_ac_q3_at( px: usize, py: usize, @@ -14377,6 +14484,15 @@ fn decode_block( // multi-transform-unit path below. let (luma_set, chroma_set, luma_tx, chroma_tx, scans) = if lossless(fctx) { (TxbSet::Luma4, TxbSet::Chroma4, 4, 4, (scan4, scan4)) + } else if ss_x(fctx) == 0 && ss_y(fctx) == 0 { + let tx = if side >= 64 { 32 } else { side }; + let (set, scan): (TxbSet, &[u16]) = match tx { + 32 => (TxbSet::Chroma32, scan32), + 16 => (TxbSet::Chroma16, scan16), + 8 => (TxbSet::Chroma8, scan8), + _ => (TxbSet::Chroma4, scan4), + }; + (luma_set, set, luma_tx, tx, (scans.0, scan)) } else { (luma_set, chroma_set, luma_tx, chroma_tx, scans) }; @@ -14511,8 +14627,8 @@ fn decode_block( }; let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8, scan4); let reach = Reach::of(side, px, py, y.width, y.height, fctx); - let (cpx, cpy) = (px / 2, py / 2); - let chroma_side = side / 2; + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); + let chroma_side = side >> ss_x(fctx); // spec 7.11.3.4 with `use_intrabc`: the reference is the *current* // frame's own pre-loop-filter reconstruction (all loop filters are off // whenever `allow_intrabc`, frame.rs:194/210/252/272) at the full-pel @@ -14524,19 +14640,19 @@ fn decode_block( let mut yb = vec![0u16; side * side]; mc::predict_with_filter( &y.data, y.width, y.true_width, y.true_height, - mv_to_q4(px, dv_col, true), mv_to_q4(py, dv_row, true), + mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), side, side, mc::InterpFilterKind::Bilinear, &mut yb, fctx, ); let mut ub = vec![0u16; chroma_side * chroma_side]; mc::predict_with_filter( &u.data, u.width, u.true_width, u.true_height, - mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), + mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_x(fctx)), chroma_side, chroma_side, mc::InterpFilterKind::Bilinear, &mut ub, fctx, ); let mut vb = vec![0u16; chroma_side * chroma_side]; mc::predict_with_filter( &v.data, v.width, v.true_width, v.true_height, - mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), + mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_x(fctx)), chroma_side, chroma_side, mc::InterpFilterKind::Bilinear, &mut vb, fctx, ); (yb, ub, vb) @@ -14685,7 +14801,7 @@ fn decode_block( let cn = (chroma_side / chroma_tx).max(1); for cu_row in 0..cn { for cu_col in 0..cn { - let luma_span = chroma_tx * 2; + let luma_span = chroma_tx << ss_x(fctx); let cu_r = if cn == 1 { reach } else { @@ -14774,6 +14890,10 @@ fn decode_block( if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } + // lane-av1-444: a 64x64 block takes this arm (`logical_tx == side`). + // At 4:4:4 its chroma plane is 64x64 and libaom codes four TX_32X32 + // units. This read is still one unit (`chroma_side` x `chroma_tx`). + // Luma of that block matches; chroma does not. See lanes/av1444.report.md. let u_grid = read_plane( dec, cdfs, @@ -14964,7 +15084,7 @@ fn decode_block( // The chroma context arrays are indexed in LUMA mi, so this // unit's anchor is its own luma quadrant: `chroma_tx` chroma // pixels span `2 * chroma_tx` luma ones. - let luma_span = chroma_tx * 2; + let luma_span = chroma_tx << ss_x(fctx); let cu_mi = ( mi_r + cu_row * (luma_span / MI), mi_c + cu_col * (luma_span / MI), @@ -15267,8 +15387,8 @@ fn decode_block_128rect( TxbSet::Chroma32 }; let chroma_scan = default_scan(chroma_tx); - let (cpx, cpy) = (px / 2, py / 2); - let (chroma_w, chroma_h) = (bw / 2, bh / 2); + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); + let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); let (nch_x, nch_y) = (bw / 64, bh / 64); // The frame's own mi grid (`av1_calc_mi_size`, 8-px aligned): libaom's @@ -15288,7 +15408,7 @@ fn decode_block_128rect( let mut units: Vec<(usize, (usize, usize), Grid)> = Vec::new(); // The chroma context arrays are indexed in LUMA mi; every chroma unit of // every chunk shares this luma span (2 chroma pixels per luma unit). - let luma_span = chroma_tx * 2; + let luma_span = chroma_tx << ss_x(fctx); for ch_row in 0..nch_y { for ch_col in 0..nch_x { for tu_row in ch_row * tpc..(ch_row + 1) * tpc { @@ -15363,8 +15483,8 @@ fn decode_block_128rect( let cn = (32 / chroma_tx).max(1); // libaom's per-chunk `unit_width/height` (plane-mi units of the // luma mi grid): `min(mu_blocks + col, max_blocks) >> ss`. - let unit_w = ((ch_col + 1) * 16).min(max_w_mi) >> 1; - let unit_h = ((ch_row + 1) * 16).min(max_h_mi) >> 1; + let unit_w = ((ch_col + 1) * 16).min(max_w_mi) >> ss_x(fctx); + let unit_h = ((ch_row + 1) * 16).min(max_h_mi) >> ss_y(fctx); let stepc = (chroma_tx / 4).max(1); // `decode_token_recon_block` codes a mu chunk PLANE-MAJOR: every U // unit of the chunk, then every V unit. At one unit per plane per @@ -15382,8 +15502,8 @@ fn decode_block_128rect( let cu_x = cpx + ch_col * 32 + ci_col * chroma_tx; let cu_y = cpy + ch_row * 32 + ci_row * chroma_tx; let cu_mi = ( - mi_r + ch_row * 16 + ci_row * (chroma_tx / 2), - mi_c + ch_col * 16 + ci_col * (chroma_tx / 2), + mi_r + ch_row * 16 + ci_row * ((chroma_tx << ss_y(fctx)) / 4), + mi_c + ch_col * 16 + ci_col * ((chroma_tx << ss_x(fctx)) / 4), ); let cu_r = tu_reach( bw, @@ -15695,7 +15815,15 @@ fn decode_leaf8( }; let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let reach = Reach::of(8, px, py, y.width, y.height, fctx); - let (cpx, cpy) = (px / 2, py / 2); + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); + // 4:4:4: this 8x8 block's chroma plane block is 8x8 (same scan as luma, + // `scans.0`). 4:2:0 stays the 4x4 unit this leaf was written for. + let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0; + let (csz, cset, cscan) = if chroma_444 { + (8usize, TxbSet::Chroma8, scans.0) + } else { + (4usize, TxbSet::Chroma4, scans.1) + }; // spec 7.11.3.4 with `use_intrabc`, same as `decode_block`: predict from // the CURRENT frame's own pre-loop-filter reconstruction (every loop // filter is off whenever `allow_intrabc`, frame.rs:194/210/252/272) at @@ -15705,19 +15833,19 @@ fn decode_leaf8( let mut yb = vec![0u16; 8 * 8]; mc::predict_with_filter( &y.data, y.width, y.true_width, y.true_height, - mv_to_q4(px, dv_col, true), mv_to_q4(py, dv_row, true), + mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), 8, 8, mc::InterpFilterKind::Bilinear, &mut yb, fctx, ); let mut ub = vec![0u16; 4 * 4]; mc::predict_with_filter( &u.data, u.width, u.true_width, u.true_height, - mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), + mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_x(fctx)), 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, ); let mut vb = vec![0u16; 4 * 4]; mc::predict_with_filter( &v.data, v.width, v.true_width, v.true_height, - mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), + mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_x(fctx)), 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, ); (yb, ub, vb) @@ -15803,11 +15931,11 @@ fn decode_leaf8( push_intra(1, cpx, cpy, - 4, + csz, uv_predict_mode, angle_delta_uv, reach, - &ZERO_RESIDUAL[..16], + &ZERO_RESIDUAL[..csz * csz], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, @@ -15820,18 +15948,18 @@ fn decode_leaf8( push_intra(2, cpx, cpy, - 4, + csz, uv_predict_mode, angle_delta_uv, reach, - &ZERO_RESIDUAL[..16], + &ZERO_RESIDUAL[..csz * csz], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); luma_grid = Grid::Zero(64); - u_grid = Grid::Zero(16); - v_grid = Grid::Zero(16); + u_grid = Grid::Zero(csz * csz); + v_grid = Grid::Zero(csz * csz); } else if resolved != 4 { let around = neighbours.around_mi(leaf_mi, 8); if let Some(buf) = &palette_y_buf { @@ -15880,8 +16008,8 @@ fn decode_leaf8( u_grid = read_plane( dec, cdfs, - TxbSet::Chroma4, - scans.1, + cset, + cscan, 1, around[1], mode, @@ -15891,8 +16019,8 @@ fn decode_leaf8( u, cpx, cpy, - 4, - TX4, + csz, + csz, base_q_idx, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, @@ -15907,8 +16035,8 @@ fn decode_leaf8( v_grid = read_plane( dec, cdfs, - TxbSet::Chroma4, - scans.1, + cset, + cscan, 2, around[2], mode, @@ -15918,8 +16046,8 @@ fn decode_leaf8( v, cpx, cpy, - 4, - TX4, + csz, + csz, base_q_idx, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, @@ -16019,8 +16147,8 @@ fn decode_leaf8( u_grid = read_plane( dec, cdfs, - TxbSet::Chroma4, - scans.1, + cset, + cscan, 1, chroma_around[1], mode, @@ -16030,8 +16158,8 @@ fn decode_leaf8( u, cpx, cpy, - 4, - TX4, + csz, + csz, base_q_idx, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, @@ -16044,8 +16172,8 @@ fn decode_leaf8( v_grid = read_plane( dec, cdfs, - TxbSet::Chroma4, - scans.1, + cset, + cscan, 2, chroma_around[2], mode, @@ -16055,8 +16183,8 @@ fn decode_leaf8( v, cpx, cpy, - 4, - TX4, + csz, + csz, base_q_idx, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, @@ -16398,7 +16526,7 @@ fn decode_leaf_split4( let mut yb = vec![0u16; 16]; mc::predict_with_filter( &y.data, y.width, y.true_width, y.true_height, - mv_to_q4(px, dv.1, true), mv_to_q4(py, dv.0, true), + mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), 4, 4, mc::InterpFilterKind::Bilinear, &mut yb, fctx, ); set_palette_pred(yb.clone(), fctx); @@ -16502,7 +16630,7 @@ fn decode_leaf_split4( (Grid::Zero(16), Grid::Zero(16)) } else { let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); - let (cpx, cpy) = (gpx / 2, gpy / 2); + let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); let (uv_mode, angle_delta_uv, alpha) = last_uv.expect("i==3 always sets has_chroma, so chroma is always Some"); @@ -16547,13 +16675,13 @@ fn decode_leaf_split4( let mut ub = vec![0u16; 16]; mc::predict_with_filter( &u.data, u.width, u.true_width, u.true_height, - mv_to_q4(cpx, dv.1, false), mv_to_q4(cpy, dv.0, false), + mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_x(fctx)), 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, ); let mut vb = vec![0u16; 16]; mc::predict_with_filter( &v.data, v.width, v.true_width, v.true_height, - mv_to_q4(cpx, dv.1, false), mv_to_q4(cpy, dv.0, false), + mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_x(fctx)), 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, ); let chroma_around = neighbours.around_mi(leaf_mi, 8); @@ -16799,7 +16927,7 @@ fn decode_leaf_rect8( let mut yb = vec![0u16; tw * th]; mc::predict_with_filter( &y.data, y.width, y.true_width, y.true_height, - mv_to_q4(tu_px, dv.1, true), mv_to_q4(tu_py, dv.0, true), + mv_to_q4(tu_px, dv.1, 0), mv_to_q4(tu_py, dv.0, 0), tw, th, mc::InterpFilterKind::Bilinear, &mut yb, fctx, ); set_palette_pred(yb, fctx); @@ -17118,7 +17246,7 @@ fn decode_leaf_rect8( (Grid::Zero(16), Grid::Zero(16)) } else { let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); - let (cpx, cpy) = (gpx / 2, gpy / 2); + let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); let (uv_mode, angle_delta_uv, alpha) = last_uv.expect("i==1 always sets has_chroma, so chroma is always Some"); @@ -17163,13 +17291,13 @@ fn decode_leaf_rect8( let mut ub = vec![0u16; 16]; mc::predict_with_filter( &u.data, u.width, u.true_width, u.true_height, - mv_to_q4(cpx, dv.1, false), mv_to_q4(cpy, dv.0, false), + mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_x(fctx)), 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, ); let mut vb = vec![0u16; 16]; mc::predict_with_filter( &v.data, v.width, v.true_width, v.true_height, - mv_to_q4(cpx, dv.1, false), mv_to_q4(cpy, dv.0, false), + mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_x(fctx)), 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, ); let chroma_around = neighbours.around_mi(leaf_mi, 8); @@ -17937,10 +18065,10 @@ fn deblock_span( deblock_plane_span(y, 0, lf, n, frame_width, frame_height, y_lo, y_hi, horz, fctx); } if lf.level[2] != 0 { - deblock_plane_span(u, 1, lf, n, frame_width, frame_height, y_lo / 2, y_hi / 2, horz, fctx); + deblock_plane_span(u, 1, lf, n, frame_width, frame_height, y_lo >> ss_y(fctx), y_hi >> ss_y(fctx), horz, fctx); } if lf.level[3] != 0 { - deblock_plane_span(v, 2, lf, n, frame_width, frame_height, y_lo / 2, y_hi / 2, horz, fctx); + deblock_plane_span(v, 2, lf, n, frame_width, frame_height, y_lo >> ss_y(fctx), y_hi >> ss_y(fctx), horz, fctx); } } @@ -17959,8 +18087,9 @@ fn deblock_span( /// block (different tx size, ref frame, skip flag and delta_lf) as soon as /// the group is split into 4x4 luma blocks (class /// `context-read-from-one-cell`). -fn plane_to_mi(chroma: bool, coord: usize) -> usize { - if chroma { (coord / 2) | 1 } else { coord / 4 } +fn plane_to_mi(chroma: bool, coord: usize, ss: usize) -> usize { + // scale | ((coord << scale) >> 2). ss=1 is (coord/2)|1; ss=0 is coord/4. + if chroma { ss | ((coord << ss) >> 2) } else { coord / 4 } } /// This plane's own transform width in pixels at the covering block, spec @@ -19630,7 +19759,7 @@ impl<'a> EdgeCtx<'a> { chroma, seg_lf_active, delta_idx: if plane_idx == 0 { dir } else { plane_idx + 1 }, - step: if chroma { 2 } else { 1 }, + step: if chroma { 1 << (if dir == 0 { ss_x(fctx) } else { ss_y(fctx) }) } else { 1 }, base0, lvl, } @@ -19659,7 +19788,10 @@ impl<'a> EdgeCtx<'a> { /// entirely. Returns `None` when this 4-pixel edge group is not filtered. #[inline(always)] fn edge_params(c: &EdgeCtx, x0: usize, y0: usize) -> Option<(u8, i32)> { - let (mi_r, mi_c) = (plane_to_mi(c.chroma, y0), plane_to_mi(c.chroma, x0)); + let (mi_r, mi_c) = ( + plane_to_mi(c.chroma, y0, if c.chroma { ss_y(c.fctx) } else { 0 }), + plane_to_mi(c.chroma, x0, if c.chroma { ss_x(c.fctx) } else { 0 }), + ); let cur_tx = if c.dir == 0 { tx_px_at(c.n, c.chroma, mi_r, mi_c) } else { @@ -19731,10 +19863,11 @@ fn edge_params_body( (usize::from(cur_org[0]), usize::from(cur_dim[1])) }; let pu_edge = if c.chroma { - let plane_dim = (dim_mi * MI / 2).max(4); + let ss = if c.dir == 0 { ss_x(c.fctx) } else { ss_y(c.fctx) }; + let plane_dim = ((dim_mi * MI) >> ss).max(4); (if c.dir == 0 { x0 } else { y0 }) % plane_dim == 0 } else { - plane_to_mi(false, if c.dir == 0 { x0 } else { y0 }) == org_px + plane_to_mi(false, if c.dir == 0 { x0 } else { y0 }, 0) == org_px }; if !pu_edge && cur_ref != 0 @@ -21383,16 +21516,16 @@ fn replay_inner( width: tw, height: th, y: crop(&y, tw, th), - u: crop(&u, tw.div_ceil(2), th.div_ceil(2)), - v: crop(&v, tw.div_ceil(2), th.div_ceil(2)), + u: crop(&u, round_ss(tw, ss_x(fctx)), round_ss(th, ss_y(fctx))), + v: crop(&v, round_ss(tw, ss_x(fctx)), round_ss(th, ss_y(fctx))), }); }); let out = Picture { width: fw, height: fh, y: crop(&y, fw, fh), - u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)), - v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)), + u: crop(&u, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))), + v: crop(&v, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))), }; // The working planes outlive the crop: either they ARE the // deblocked planes this candidate's successors reuse, or they go @@ -21440,7 +21573,7 @@ pub(crate) fn replay_restoration( let r = slot.as_mut()?; let (cdefed, deblocked) = r.final_luma.as_ref()?; let (fw, fh) = (r.frame_width, r.frame_height); - let mut grid = crate::restoration::RestorationGrid::new(lr, fw as u32, fh as u32); + let mut grid = crate::restoration::RestorationGrid::with_ss(lr, fw as u32, fh as u32, fctx.subsampling_x.get(), fctx.subsampling_y.get()); let (horz, vert) = (grid.horz_units[0], grid.vert_units[0]); if horz == 0 || units.len() != horz * vert { return None; @@ -21651,18 +21784,19 @@ fn cdef_band( w[(mi_r / 2) * unit_cols + mi_c / 2] = mark; } if enable_primary_uv || enable_secondary_uv { - let (cox, coy) = (ox / 2, oy / 2); + let (cox, coy) = (ox >> ss_x(fctx), oy >> ss_y(fctx)); + let (csize_x, csize_y) = (8 >> ss_x(fctx), 8 >> ss_y(fctx)); for (plane, src_p) in [(&mut *u, src_u), (&mut *v, src_v)] { let (tw, th) = (plane.true_width, plane.true_height); let stride = plane.width; - cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); + cdef_gather(src_p, stride, tw, th, cox, coy, csize_x, csize_y, &mut win_uv); cdef_filter_block( &win_uv, plane, cox, coy, - 4, - 4, + csize_x, + csize_y, t_uv, uv_sec_strength, uv_dir, @@ -22376,9 +22510,9 @@ fn apply_loop_restoration( &u.data, &deblocked_u.data, u.width, - frame_w.div_ceil(2), - frame_h.div_ceil(2), - 1, + round_ss(frame_w, ss_x(fctx)), + round_ss(frame_h, ss_y(fctx)), + ss_y(fctx) as u32, lr.frame_restoration_type[1], lr.loop_restoration_size[1], grid, @@ -22391,9 +22525,9 @@ fn apply_loop_restoration( &v.data, &deblocked_v.data, v.width, - frame_w.div_ceil(2), - frame_h.div_ceil(2), - 1, + round_ss(frame_w, ss_x(fctx)), + round_ss(frame_h, ss_y(fctx)), + ss_y(fctx) as u32, lr.frame_restoration_type[2], lr.loop_restoration_size[2], grid, @@ -22857,27 +22991,29 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( tile_x1: width, tile_y1: height, }; + let (cw, ch) = (width >> ss_x(fctx), height >> ss_y(fctx)); + let (ctw, cth) = (true_width >> ss_x(fctx), true_height >> ss_y(fctx)); let mut u = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane(width * height / 4)), - width: width / 2, - height: height / 2, - true_width: true_width / 2, - true_height: true_height / 2, + data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), + width: cw, + height: ch, + true_width: ctw, + true_height: cth, tile_x0: 0, tile_y0: 0, - tile_x1: width / 2, - tile_y1: height / 2, + tile_x1: cw, + tile_y1: ch, }; let mut v = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane(width * height / 4)), - width: width / 2, - height: height / 2, - true_width: true_width / 2, - true_height: true_height / 2, + data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), + width: cw, + height: ch, + true_width: ctw, + true_height: cth, tile_x0: 0, tile_y0: 0, - tile_x1: width / 2, - tile_y1: height / 2, + tile_x1: cw, + tile_y1: ch, }; let scan32 = default_scan(TX32); @@ -22896,10 +23032,12 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // spec 7.16/libaom `av1_alloc_restoration_struct`: the unit grid is // sized from `av1_get_upsampled_plane_size`, i.e. the UPSCALED width -- // loop restoration runs after the superres upscale. - let mut lr_grid = crate::restoration::RestorationGrid::new( + let mut lr_grid = crate::restoration::RestorationGrid::with_ss( lr, superres(fctx).map_or(frame_width, |(w, _)| w), frame_height, + fctx.subsampling_x.get(), + fctx.subsampling_y.get(), ); // lane-tiles r2: each tile gets its own fresh `SymbolDecoder` over its @@ -24903,7 +25041,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( tile_x1: out_w, tile_y1: in_h, }; - let (cw, ch, cuw) = (fw.div_ceil(2), fh.div_ceil(2), uw.div_ceil(2)); + let (cw, ch, cuw) = (round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx)), round_ss(uw, ss_x(fctx))); y = up(&y, fw, fh, uw); u = up(&u, cw, ch, cuw); v = up(&v, cw, ch, cuw); @@ -24990,8 +25128,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( width: true_width, height: true_height, y: yc, - u: crop(&u, true_width.div_ceil(2), true_height.div_ceil(2)), - v: crop(&v, true_width.div_ceil(2), true_height.div_ceil(2)), + u: crop(&u, round_ss(true_width, ss_x(fctx)), round_ss(true_height, ss_y(fctx))), + v: crop(&v, round_ss(true_width, ss_x(fctx)), round_ss(true_height, ss_y(fctx))), }) } else { None @@ -25002,8 +25140,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( width: fw, height: fh, y: crop_owned(y, fw, fh), - u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)), - v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)), + u: crop_owned(u, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))), + v: crop_owned(v, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))), }, result_cdfs, )) @@ -25276,8 +25414,9 @@ fn read_intrabc_dv( /// A 1/8-pel motion vector component converted to the 1/16-pel offset /// [`mc::predict`] takes, duplicating [`crate::encode`]'s private `mv_to_q4` /// (its own doc comment explains the `*2`/`*1` luma/chroma split). -fn mv_to_q4(pos: usize, mv_component: i32, luma: bool) -> i32 { - (pos as i32) * 16 + mv_component * if luma { 2 } else { 1 } +fn mv_to_q4(pos: usize, mv_component: i32, ss: usize) -> i32 { + // orig_pos += mv * (1 << (1 - ss)). Luma always passes 0. + (pos as i32) * 16 + mv_component * (1i32 << (1 - ss as i32)) } impl PlaneBuf<'_> { @@ -25825,8 +25964,18 @@ impl<'p> RefPix<'p> { dims[i].and(pics[i]).map(|g| { ( whole_plane(&g.y, g.width, g.height), - whole_plane(&g.u, g.width / 2, g.height / 2), - whole_plane(&g.v, g.width / 2, g.height / 2), + { + // Reference pictures carry no subsampling field. A + // 4:4:4 chroma plane is luma-sized; 4:2:0 is the quarter. + let full = g.u.len() == g.width * g.height; + let (cw, ch) = if full { (g.width, g.height) } else { (g.width / 2, g.height / 2) }; + whole_plane(&g.u, cw, ch) + }, + { + let full = g.v.len() == g.width * g.height; + let (cw, ch) = if full { (g.width, g.height) } else { (g.width / 2, g.height / 2) }; + whole_plane(&g.v, cw, ch) + }, ) }) }); @@ -26384,8 +26533,8 @@ fn obmc_neighbour_pred( refplane.width, refplane.true_width, refplane.true_height, - mv_to_q4(x, mv.1, luma), - mv_to_q4(y, mv.0, luma), + 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) }), w, h, h_kind, @@ -26398,8 +26547,8 @@ fn obmc_neighbour_pred( refplane.width, refplane.true_width, refplane.true_height, - mv_to_q4(x, mv.1, luma), - mv_to_q4(y, mv.0, luma), + 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) }), x_scale_fp, w, h, @@ -27440,10 +27589,10 @@ fn decode_inter_block( fctx.inter_last_mc.with(|c| c.set(None)); let has_chroma = strip_chroma.is_none_or(|s| s.has_chroma); let (cpx, cpy) = match strip_chroma { - Some(s) if s.has_chroma => (s.pair_mi.1 * MI / 2, s.pair_mi.0 * MI / 2), - _ => (px / 2, py / 2), + Some(s) if s.has_chroma => (s.pair_mi.1 * MI >> ss_x(fctx), s.pair_mi.0 * MI >> ss_y(fctx)), + _ => (px >> ss_x(fctx), py >> ss_y(fctx)), }; - let chroma_side = side / 2; + let chroma_side = side >> ss_x(fctx); let (write_chroma_w, write_chroma_h) = match strip_chroma { Some(s) if s.has_chroma => { if s.horz { @@ -27452,8 +27601,21 @@ fn decode_inter_block( (4, 8) } } - _ => (write_w / 2, write_h / 2), + _ => (write_w >> ss_x(fctx), write_h >> ss_y(fctx)), }; + let (chroma_set, chroma_tx, scan_chroma) = + if ss_x(fctx) == 0 && ss_y(fctx) == 0 && strip_chroma.is_none() { + let tx = if side >= 64 { 32 } else { side }; + let set = match tx { + 32 => TxbSet::Chroma32, + 16 => TxbSet::Chroma16, + 8 => TxbSet::Chroma8, + _ => TxbSet::Chroma4, + }; + (set, tx, scan_luma) + } else { + (chroma_set, chroma_tx, scan_chroma) + }; // lane-midcut r3 (same rule as lane-t900 r5's `warp_plane_allowed`, one // copy kept at the merge): `av1_init_warp_params` (libaom reconinter.c) // bails per PLANE at `block_width < 8 || block_height < 8` -- on the @@ -28242,8 +28404,8 @@ fn decode_inter_block( py0.width, py0.true_width, py0.true_height, - mv_to_q4(px, mv0.1, true), - mv_to_q4(py, mv0.0, true), + mv_to_q4(px, mv0.1, 0), + mv_to_q4(py, mv0.0, 0), scale0, side, side, @@ -28269,8 +28431,8 @@ fn decode_inter_block( py1.width, py1.true_width, py1.true_height, - mv_to_q4(px, mv1.1, true), - mv_to_q4(py, mv1.0, true), + mv_to_q4(px, mv1.1, 0), + mv_to_q4(py, mv1.0, 0), scale1, side, side, @@ -28318,8 +28480,8 @@ fn decode_inter_block( pu0.width, pu0.true_width, pu0.true_height, - mv_to_q4(cpx, mv0.1, false), - mv_to_q4(cpy, mv0.0, false), + mv_to_q4(cpx, mv0.1, ss_x(fctx)), + mv_to_q4(cpy, mv0.0, ss_x(fctx)), scale0, chroma_side, chroma_side, @@ -28337,7 +28499,7 @@ fn decode_inter_block( crate::warp::warp_affine_compound( wp, &pu0.data, pu0.true_width as i32, pu0.true_height as i32, pu0.width as i32, &mut inter0_u, cpx as i32, cpy as i32, - chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, fctx, + chroma_side as i32, chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); } } @@ -28347,8 +28509,8 @@ fn decode_inter_block( pu1.width, pu1.true_width, pu1.true_height, - mv_to_q4(cpx, mv1.1, false), - mv_to_q4(cpy, mv1.0, false), + mv_to_q4(cpx, mv1.1, ss_x(fctx)), + mv_to_q4(cpy, mv1.0, ss_x(fctx)), scale1, chroma_side, chroma_side, @@ -28366,7 +28528,7 @@ fn decode_inter_block( crate::warp::warp_affine_compound( wp, &pu1.data, pu1.true_width as i32, pu1.true_height as i32, pu1.width as i32, &mut inter1_u, cpx as i32, cpy as i32, - chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, fctx, + chroma_side as i32, chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); } } @@ -28379,7 +28541,7 @@ fn decode_inter_block( side, chroma_side, chroma_side, - true, + ss_x(fctx) == 1 && ss_y(fctx) == 1, &mut pred_u, fctx, ); } else { @@ -28392,8 +28554,8 @@ fn decode_inter_block( pv0.width, pv0.true_width, pv0.true_height, - mv_to_q4(cpx, mv0.1, false), - mv_to_q4(cpy, mv0.0, false), + mv_to_q4(cpx, mv0.1, ss_x(fctx)), + mv_to_q4(cpy, mv0.0, ss_x(fctx)), scale0, chroma_side, chroma_side, @@ -28411,7 +28573,7 @@ fn decode_inter_block( crate::warp::warp_affine_compound( wp, &pv0.data, pv0.true_width as i32, pv0.true_height as i32, pv0.width as i32, &mut inter0_v, cpx as i32, cpy as i32, - chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, fctx, + chroma_side as i32, chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); } } @@ -28421,8 +28583,8 @@ fn decode_inter_block( pv1.width, pv1.true_width, pv1.true_height, - mv_to_q4(cpx, mv1.1, false), - mv_to_q4(cpy, mv1.0, false), + mv_to_q4(cpx, mv1.1, ss_x(fctx)), + mv_to_q4(cpy, mv1.0, ss_x(fctx)), scale1, chroma_side, chroma_side, @@ -28440,7 +28602,7 @@ fn decode_inter_block( crate::warp::warp_affine_compound( wp, &pv1.data, pv1.true_width as i32, pv1.true_height as i32, pv1.width as i32, &mut inter1_v, cpx as i32, cpy as i32, - chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, fctx, + chroma_side as i32, chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); } } @@ -28453,7 +28615,7 @@ fn decode_inter_block( side, chroma_side, chroma_side, - true, + ss_x(fctx) == 1 && ss_y(fctx) == 1, &mut pred_v, fctx, ); } else { @@ -29561,8 +29723,8 @@ fn decode_inter_block( py_ref.width, py_ref.true_width, py_ref.true_height, - mv_to_q4(px, mv.1, true), - mv_to_q4(py, mv.0, true), + mv_to_q4(px, mv.1, 0), + mv_to_q4(py, mv.0, 0), side, side, write_w, @@ -29576,8 +29738,8 @@ fn decode_inter_block( pu_ref.width, pu_ref.true_width, pu_ref.true_height, - mv_to_q4(cpx, mv.1, false), - mv_to_q4(cpy, mv.0, false), + mv_to_q4(cpx, mv.1, ss_x(fctx)), + mv_to_q4(cpy, mv.0, ss_x(fctx)), chroma_side, chroma_side, write_chroma_w, @@ -29591,8 +29753,8 @@ fn decode_inter_block( pv_ref.width, pv_ref.true_width, pv_ref.true_height, - mv_to_q4(cpx, mv.1, false), - mv_to_q4(cpy, mv.0, false), + mv_to_q4(cpx, mv.1, ss_x(fctx)), + mv_to_q4(cpy, mv.0, ss_x(fctx)), chroma_side, chroma_side, write_chroma_w, @@ -29607,8 +29769,8 @@ fn decode_inter_block( py_ref.width, py_ref.true_width, py_ref.true_height, - mv_to_q4(px, mv.1, true), - mv_to_q4(py, mv.0, true), + mv_to_q4(px, mv.1, 0), + mv_to_q4(py, mv.0, 0), luma_scale, side, side, @@ -29623,8 +29785,8 @@ fn decode_inter_block( pu_ref.width, pu_ref.true_width, pu_ref.true_height, - mv_to_q4(cpx, mv.1, false), - mv_to_q4(cpy, mv.0, false), + mv_to_q4(cpx, mv.1, ss_x(fctx)), + mv_to_q4(cpy, mv.0, ss_x(fctx)), luma_scale, chroma_side, chroma_side, @@ -29639,8 +29801,8 @@ fn decode_inter_block( pv_ref.width, pv_ref.true_width, pv_ref.true_height, - mv_to_q4(cpx, mv.1, false), - mv_to_q4(cpy, mv.0, false), + mv_to_q4(cpx, mv.1, ss_x(fctx)), + mv_to_q4(cpy, mv.0, ss_x(fctx)), luma_scale, chroma_side, chroma_side, @@ -29684,12 +29846,12 @@ fn decode_inter_block( crate::warp::warp_affine( params, &pu_ref.data, pu_ref.true_width as i32, pu_ref.true_height as i32, pu_ref.width as i32, &mut pred_u, cpx as i32, cpy as i32, chroma_side as i32, - chroma_side as i32, chroma_side as i32, 1, 1, fctx, + chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); crate::warp::warp_affine( params, &pv_ref.data, pv_ref.true_width as i32, pv_ref.true_height as i32, pv_ref.width as i32, &mut pred_v, cpx as i32, cpy as i32, chroma_side as i32, - chroma_side as i32, chroma_side as i32, 1, 1, fctx, + chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); } } @@ -29736,8 +29898,8 @@ fn decode_inter_block( src.width, src.true_width, src.true_height, - mv_to_q4(cpx, prev_mv.1, false), - mv_to_q4(cpy, prev_mv.0, false), + mv_to_q4(cpx, prev_mv.1, ss_x(fctx)), + mv_to_q4(cpy, prev_mv.0, ss_x(fctx)), prev_scale, piece_w, piece_h, @@ -31284,7 +31446,7 @@ fn decode_inter_sub8_split4( hit!(SUB8_INTER_SPLIT_HITS); let (gr, gc) = group_mi; let (gpx, gpy) = (gc * MI, gr * MI); - let (cpx, cpy) = (gpx / 2, gpy / 2); + let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); // Per-sub-block state the group's chroma prediction needs (spec // 7.11.3.1's candRow/candCol loop reads each sub-block's OWN mv/ref/ // filters), indexed `dr * 2 + dc`. @@ -31577,8 +31739,8 @@ fn decode_inter_sub8_split4( sref_y.width, sref_y.true_width, sref_y.true_height, - mv_to_q4(px, mv.1, true), - mv_to_q4(py, mv.0, true), + mv_to_q4(px, mv.1, 0), + mv_to_q4(py, mv.0, 0), luma_scale, B4, B4, @@ -31713,8 +31875,8 @@ fn decode_inter_sub8_split4( plane.width, plane.true_width, plane.true_height, - mv_to_q4(ox, mv.1, false), - mv_to_q4(oy, mv.0, false), + mv_to_q4(ox, mv.1, ss_x(fctx)), + mv_to_q4(oy, mv.0, ss_x(fctx)), piece_scale, pw, ph, @@ -32194,7 +32356,7 @@ fn decode_intra_sub8_leaf( // chroma-reference block's own type. Same body as [`decode_leaf_rect8`]'s // post-loop chroma. let (gpx, gpy) = (gc * MI, gr * MI); - let (cpx, cpy) = (gpx / 2, gpy / 2); + let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(gr, gc, r, c); let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; @@ -32324,7 +32486,7 @@ fn decode_inter_sub8_rect2( } let (gr, gc) = group_mi; let (gpx, gpy) = (gc * MI, gr * MI); - let (cpx, cpy) = (gpx / 2, gpy / 2); + let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); // Per-sub-block state the group's one chroma unit predicts from. let mut piece: [Option<(i8, (i32, i32), mc::InterpFilterKind, mc::InterpFilterKind)>; 2] = [None; 2]; @@ -32632,8 +32794,8 @@ fn decode_inter_sub8_rect2( sref_y.width, sref_y.true_width, sref_y.true_height, - mv_to_q4(px, mv.1, true), - mv_to_q4(py, mv.0, true), + mv_to_q4(px, mv.1, 0), + mv_to_q4(py, mv.0, 0), luma_scale, bw, bh, @@ -32819,8 +32981,8 @@ fn decode_inter_sub8_rect2( plane.width, plane.true_width, plane.true_height, - mv_to_q4(ox, mv.1, false), - mv_to_q4(oy, mv.0, false), + mv_to_q4(ox, mv.1, ss_x(fctx)), + mv_to_q4(oy, mv.0, ss_x(fctx)), piece_scale, piece_w, piece_h, @@ -33074,7 +33236,7 @@ fn decode_inter_block8( const SIZE_GROUP_8: usize = 1; let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); - let (cpx, cpy) = (px / 2, py / 2); + let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); // lane-txselect: whether this leaf's `TxMode::Select` var-tx tree took // the one split a `BLOCK_8X8` can take (`read_block_tx_size` below) -- // read back by the luma residual read and the tail's neighbour/loop-filter @@ -33534,8 +33696,8 @@ fn decode_inter_block8( py0.width, py0.true_width, py0.true_height, - mv_to_q4(px, mv0.1, true), - mv_to_q4(py, mv0.0, true), + mv_to_q4(px, mv0.1, 0), + mv_to_q4(py, mv0.0, 0), scale0, SIDE, SIDE, @@ -33556,8 +33718,8 @@ fn decode_inter_block8( py1.width, py1.true_width, py1.true_height, - mv_to_q4(px, mv1.1, true), - mv_to_q4(py, mv1.0, true), + mv_to_q4(px, mv1.1, 0), + mv_to_q4(py, mv1.0, 0), scale1, SIDE, SIDE, @@ -33596,8 +33758,8 @@ fn decode_inter_block8( pu0.width, pu0.true_width, pu0.true_height, - mv_to_q4(cpx, mv0.1, false), - mv_to_q4(cpy, mv0.0, false), + mv_to_q4(cpx, mv0.1, ss_x(fctx)), + mv_to_q4(cpy, mv0.0, ss_x(fctx)), scale0, CHROMA_SIDE, CHROMA_SIDE, @@ -33611,8 +33773,8 @@ fn decode_inter_block8( pu1.width, pu1.true_width, pu1.true_height, - mv_to_q4(cpx, mv1.1, false), - mv_to_q4(cpy, mv1.0, false), + mv_to_q4(cpx, mv1.1, ss_x(fctx)), + mv_to_q4(cpy, mv1.0, ss_x(fctx)), scale1, CHROMA_SIDE, CHROMA_SIDE, @@ -33629,7 +33791,7 @@ fn decode_inter_block8( SIDE, CHROMA_SIDE, CHROMA_SIDE, - true, + ss_x(fctx) == 1 && ss_y(fctx) == 1, &mut pred_u, fctx, ); } else { @@ -33642,8 +33804,8 @@ fn decode_inter_block8( pv0.width, pv0.true_width, pv0.true_height, - mv_to_q4(cpx, mv0.1, false), - mv_to_q4(cpy, mv0.0, false), + mv_to_q4(cpx, mv0.1, ss_x(fctx)), + mv_to_q4(cpy, mv0.0, ss_x(fctx)), scale0, CHROMA_SIDE, CHROMA_SIDE, @@ -33657,8 +33819,8 @@ fn decode_inter_block8( pv1.width, pv1.true_width, pv1.true_height, - mv_to_q4(cpx, mv1.1, false), - mv_to_q4(cpy, mv1.0, false), + mv_to_q4(cpx, mv1.1, ss_x(fctx)), + mv_to_q4(cpy, mv1.0, ss_x(fctx)), scale1, CHROMA_SIDE, CHROMA_SIDE, @@ -33675,7 +33837,7 @@ fn decode_inter_block8( SIDE, CHROMA_SIDE, CHROMA_SIDE, - true, + ss_x(fctx) == 1 && ss_y(fctx) == 1, &mut pred_v, fctx, ); } else { @@ -34311,8 +34473,8 @@ fn decode_inter_block8( plane.width, plane.true_width, plane.true_height, - mv_to_q4(x, mv.1, luma), - mv_to_q4(y, mv.0, luma), + 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, h_filter, @@ -34325,8 +34487,8 @@ fn decode_inter_block8( plane.width, plane.true_width, plane.true_height, - mv_to_q4(x, mv.1, luma), - mv_to_q4(y, mv.0, luma), + 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, @@ -34358,12 +34520,12 @@ fn decode_inter_block8( 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, 1, 1, fctx, + CHROMA_SIDE as i32, CHROMA_SIDE 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, 1, 1, fctx, + CHROMA_SIDE as i32, CHROMA_SIDE as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx, ); } } @@ -35515,27 +35677,29 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_x1: width, tile_y1: height, }; + let (cw, ch) = (width >> ss_x(fctx), height >> ss_y(fctx)); + let (ctw, cth) = (true_width >> ss_x(fctx), true_height >> ss_y(fctx)); let mut u = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane(width * height / 4)), - width: width / 2, - height: height / 2, - true_width: true_width / 2, - true_height: true_height / 2, + data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), + width: cw, + height: ch, + true_width: ctw, + true_height: cth, tile_x0: 0, tile_y0: 0, - tile_x1: width / 2, - tile_y1: height / 2, + tile_x1: cw, + tile_y1: ch, }; let mut v = PlaneBuf { - data: std::borrow::Cow::Owned(fresh_plane(width * height / 4)), - width: width / 2, - height: height / 2, - true_width: true_width / 2, - true_height: true_height / 2, + data: std::borrow::Cow::Owned(fresh_plane(cw * ch)), + width: cw, + height: ch, + true_width: ctw, + true_height: cth, tile_x0: 0, tile_y0: 0, - tile_x1: width / 2, - tile_y1: height / 2, + tile_x1: cw, + tile_y1: ch, }; let scan32 = default_scan(TX32); @@ -35565,10 +35729,12 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // spec 7.16/libaom `av1_alloc_restoration_struct`: the unit grid is // sized from `av1_get_upsampled_plane_size`, i.e. the UPSCALED width -- // loop restoration runs after the superres upscale. - let mut lr_grid = crate::restoration::RestorationGrid::new( + let mut lr_grid = crate::restoration::RestorationGrid::with_ss( lr, superres(fctx).map_or(frame_width, |(w, _)| w), frame_height, + fctx.subsampling_x.get(), + fctx.subsampling_y.get(), ); // lane-pipefilt: the loop filters of superblock row r run as soon as row @@ -38226,7 +38392,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( tile_x1: out_w, tile_y1: in_h, }; - let (cw, ch, cuw) = (fw.div_ceil(2), fh.div_ceil(2), uw.div_ceil(2)); + let (cw, ch, cuw) = (round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx)), round_ss(uw, ss_x(fctx))); y = up(&y, fw, fh, uw); u = up(&u, cw, ch, cuw); v = up(&v, cw, ch, cuw); @@ -38304,8 +38470,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( width: true_width, height: true_height, y: crop(&y, true_width, true_height), - u: crop(&u, true_width.div_ceil(2), true_height.div_ceil(2)), - v: crop(&v, true_width.div_ceil(2), true_height.div_ceil(2)), + u: crop(&u, round_ss(true_width, ss_x(fctx)), round_ss(true_height, ss_y(fctx))), + v: crop(&v, round_ss(true_width, ss_x(fctx)), round_ss(true_height, ss_y(fctx))), }) } else { None @@ -38316,8 +38482,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( width: fw, height: fh, y: crop_owned(y, fw, fh), - u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)), - v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)), + u: crop_owned(u, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))), + v: crop_owned(v, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))), }, result_cdfs, motion_field, @@ -40358,8 +40524,8 @@ mod tests { ref_y.width, ref_y.true_width, ref_y.true_height, - mv_to_q4(px, left_mv.1, true), - mv_to_q4(py, left_mv.0, true), + mv_to_q4(px, left_mv.1, 0), + mv_to_q4(py, left_mv.0, 0), side, side, &mut want_near, fctx, @@ -40370,8 +40536,8 @@ mod tests { ref_y.width, ref_y.true_width, ref_y.true_height, - mv_to_q4(px, above_mv.1, true), - mv_to_q4(py, above_mv.0, true), + mv_to_q4(px, above_mv.1, 0), + mv_to_q4(py, above_mv.0, 0), side, side, &mut want_nearest, fctx, diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index c3f09376..df769188 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -78,7 +78,7 @@ const REFUSALS: &[&str] = &[ // hardcoded 4:2:0, the probe emits 4:2:0 planes only); it is now refused // at the sequence header. Gate: // `a_non_420_subsampled_sequence_header_is_refused_by_name`. - "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels", + "a chroma format of 4:2:2 (subsampling_x != subsampling_y): this decoder decodes 4:2:0 and 4:4:4; 4:2:2 is not ported", // lane-av1txr-r2: the third member of the same silent-garbage family. // `using_qmatrix`/`qm_y`/`qm_u`/`qm_v` are parsed by `ec-av1-syntax` but // read by NOTHING on the decode path -- dequantisation is `base_q_idx` + @@ -356,7 +356,7 @@ const PROVEN: &[(&str, &str)] = &[ // profile-0 CONTROL still decodes -- the refusal is exercised, not merely // present. ( - "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels", + "a chroma format of 4:2:2 (subsampling_x != subsampling_y): this decoder decodes 4:2:0 and 4:4:4; 4:2:2 is not ported", "a_non_420_subsampled_sequence_header_is_refused_by_name", ), // lane-av1txr-r2: the gate encodes the same source twice with real aomenc @@ -584,7 +584,11 @@ mod tests { .chain(REFUSALS) .map(|&s| s.to_owned()) .collect(); - assert!(found.len() >= 30, "the refusal scan found only {} reasons -- it is broken, not the decoder", found.len()); + assert!( + found.len() >= 30, + "the refusal scan found only {} reasons -- it is broken, not the decoder", + found.len() + ); let added: Vec<&String> = found.difference(&listed).collect(); assert!( @@ -630,9 +634,14 @@ mod tests { let mut found: BTreeSet = BTreeSet::new(); for (i, _) in src.match_indices("\"SKIP ") { let rest = &src[i + "\"SKIP ".len()..]; - let Some(colon) = rest.find(':') else { continue }; + let Some(colon) = rest.find(':') else { + continue; + }; let name = &rest[..colon]; - if !name.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') { + if !name + .chars() + .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') + { continue; } // The tail of the message says what was skipped. Only "{e}" -- the @@ -645,7 +654,10 @@ mod tests { } } let listed: BTreeSet<&str> = GATES_THAT_SKIP_ON_A_DECODE_ERROR.iter().copied().collect(); - let added: Vec<&String> = found.iter().filter(|n| !listed.contains(n.as_str())).collect(); + let added: Vec<&String> = found + .iter() + .filter(|n| !listed.contains(n.as_str())) + .collect(); assert!( added.is_empty(), "these gates turn a decode error into a printed SKIP and are not declared: \ @@ -718,9 +730,18 @@ mod tests { fn every_partition_value_of_an_enumerated_alphabet_has_an_arm() { // (the refusal in the fallback arm, the alphabet its CDF codes) let blocks: [(&str, usize); 4] = [ - ("a 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1), - ("an INTER 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1), - ("a 128x128 superblock partition value outside the 8-symbol alphabet", crate::cdf::PARTITION_W128[0].len() - 1), + ( + "a 32x32 partition type this decoder does not code", + crate::cdf::PARTITION_W32[0].len() - 1, + ), + ( + "an INTER 32x32 partition type this decoder does not code", + crate::cdf::PARTITION_W32[0].len() - 1, + ), + ( + "a 128x128 superblock partition value outside the 8-symbol alphabet", + crate::cdf::PARTITION_W128[0].len() - 1, + ), // lane-t900 r24: the key-frame superblock root. Its `part` is // either a `partition_w64` symbol or, at a frame edge, one of the // three gathered outcomes (PARTITION_HORZ, PARTITION_VERT, @@ -774,7 +795,10 @@ mod tests { "the `match` at decode.rs:{} refuses {reason:?} but has no arm for {:?} -- that \ is a real gap in its {alphabet}-value alphabet, not a dead refusal", i + 1, - missing.iter().map(|v| PARTITION_NAMES[*v]).collect::>() + missing + .iter() + .map(|v| PARTITION_NAMES[*v]) + .collect::>() ); } assert_eq!( @@ -828,7 +852,11 @@ mod tests { .next() .unwrap_or("") .to_owned(); - assert!(!var.is_empty(), "the chain at decode.rs:{} tests nothing", head + 1); + assert!( + !var.is_empty(), + "the chain at decode.rs:{} tests nothing", + head + 1 + ); // Walk up collecting every branch head of this chain, each read to // the line that opens its body. @@ -838,7 +866,8 @@ mod tests { loop { i -= 1; let t = lines[i].trim_start(); - if indent(lines[i]) != outer || !(t.starts_with("} else if ") || t.starts_with("if ")) + if indent(lines[i]) != outer + || !(t.starts_with("} else if ") || t.starts_with("if ")) { if i == 0 { break; @@ -888,7 +917,10 @@ mod tests { "the chain at decode.rs:{} refuses {reason:?} but no branch names {:?} -- that \ is a real gap in its {alphabet}-value alphabet, not a dead refusal", head + 1, - missing.iter().map(|v| PARTITION_NAMES[*v]).collect::>() + missing + .iter() + .map(|v| PARTITION_NAMES[*v]) + .collect::>() ); } assert_eq!(found, chains.len()); @@ -991,11 +1023,18 @@ mod tests { values.insert(((1u64 << length) - 2) as u32); } let max = *values.iter().max().unwrap(); - assert_eq!(max, (1u32 << 20) - 2, "the enumeration misses the spec's own ceiling"); + assert_eq!( + max, + (1u32 << 20) - 2, + "the enumeration misses the spec's own ceiling" + ); // A coefficient is masked to 20 bits (spec 5.11.39), and the base and // base-range syntax contribute at most 15 of that before the tail, so // this is the largest tail a legal coefficient can need. - assert!(max >= 0xF_FFFF - 15, "the ceiling is below the largest legal tail"); + assert!( + max >= 0xF_FFFF - 15, + "the ceiling is below the largest legal tail" + ); for value in values { match roundtrip(value) { Ok(read) => assert_eq!(read, value, "the Golomb tail {value} read back as {read}"), @@ -1051,7 +1090,11 @@ mod tests { ]; assert_eq!(alphabets[0].1, 13, "the y_mode alphabet changed size"); for group in &crate::cdf::Y_MODE { - assert_eq!(group.len() - 1, alphabets[0].1, "the y_mode rows disagree on width"); + assert_eq!( + group.len() - 1, + alphabets[0].1, + "the y_mode rows disagree on width" + ); } let src = include_str!("decode.rs"); @@ -1059,12 +1102,17 @@ mod tests { let mut sites = 0usize; for (i, line) in lines.iter().enumerate() { let t = line.trim(); - let Some(rest) = t.strip_prefix("if mode >= ") else { continue }; + let Some(rest) = t.strip_prefix("if mode >= ") else { + continue; + }; let Some(threshold) = rest.trim_end_matches(" {").parse::().ok() else { continue; }; // Only the guards carrying THIS refusal. - if !lines[i..(i + 4).min(lines.len())].join("\n").contains(REFUSAL) { + if !lines[i..(i + 4).min(lines.len())] + .join("\n") + .contains(REFUSAL) + { continue; } sites += 1; @@ -1094,7 +1142,10 @@ mod tests { i + 1 ); } - assert_eq!(sites, 3, "the intra-mode guard is at three sites, found {sites}"); + assert_eq!( + sites, 3, + "the intra-mode guard is at three sites, found {sites}" + ); } /// lane-t900 r31, ENUMERATION for "a HORZ/VERT intra strip in a @@ -1154,7 +1205,10 @@ mod tests { } } } - assert!(with_128 > 0, "enumeration is vacuous -- no 128-sided footprint was tried"); + assert!( + with_128 > 0, + "enumeration is vacuous -- no 128-sided footprint was tried" + ); assert_eq!( allowed_at_128, 0, "a footprint with a 128-pixel side passed av1_allow_palette's bound -- the \ @@ -1266,11 +1320,17 @@ mod tests { // (3) The two tables, read out of decode_rect_split's own text: every // `(w, h) =>` / `(w, h) |` match arm head between the table's `let` // and the guard that follows it. - let body_at = src.find("fn decode_rect_split(").expect("decode_rect_split is gone"); + let body_at = src + .find("fn decode_rect_split(") + .expect("decode_rect_split is gone"); let body = &src[body_at..]; let slice = |from: &str, to: &str| -> &str { - let a = body.find(from).unwrap_or_else(|| panic!("{from:?} is gone from decode_rect_split")); - let b = body[a..].find(to).unwrap_or_else(|| panic!("{to:?} is gone from decode_rect_split")); + let a = body + .find(from) + .unwrap_or_else(|| panic!("{from:?} is gone from decode_rect_split")); + let b = body[a..] + .find(to) + .unwrap_or_else(|| panic!("{to:?} is gone from decode_rect_split")); &body[a..a + b] }; let arm_shapes = |text: &str| -> BTreeSet<(usize, usize)> { @@ -1343,7 +1403,10 @@ mod tests { } } } - assert_eq!(checked, 40, "the strip domain is not the ten rect shapes x four depths"); + assert_eq!( + checked, 40, + "the strip domain is not the ten rect shapes x four depths" + ); } /// lane-t900 r33, ENUMERATION for "CfL, filter intra or a palette on a @@ -1453,9 +1516,15 @@ mod tests { } continue; } - let Some(rest) = line.strip_prefix('(') else { continue }; - let Some((pair, _)) = rest.split_once(") =>") else { continue }; - let Some((a, b)) = pair.split_once(',') else { continue }; + let Some(rest) = line.strip_prefix('(') else { + continue; + }; + let Some((pair, _)) = rest.split_once(") =>") else { + continue; + }; + let Some((a, b)) = pair.split_once(',') else { + continue; + }; let bw: usize = a.trim().parse().expect("arm bw"); let bh: usize = b.trim().parse().expect("arm bh"); assert!( @@ -1467,9 +1536,18 @@ mod tests { seen.push((bw, bh)); arms += 1; } - assert!(arms >= 10, "only {arms} arms parsed -- the table's shape changed"); - assert!(saw_none_fallback, "filter_intra_size_class_rect lost its `_ => None` fallback"); - assert!(saw_square_delegate, "filter_intra_size_class_rect lost its square-delegate arm"); + assert!( + arms >= 10, + "only {arms} arms parsed -- the table's shape changed" + ); + assert!( + saw_none_fallback, + "filter_intra_size_class_rect lost its `_ => None` fallback" + ); + assert!( + saw_square_delegate, + "filter_intra_size_class_rect lost its square-delegate arm" + ); // The guard arm delegates square strips to `filter_intra_size_class`; // drive the delegate directly, so a widening there turns THIS test red // (the numeric walk never reaches the guard arm). @@ -1519,7 +1597,10 @@ mod tests { ); } } - assert_eq!(sites, 8, "the call-site audit found {sites} sites, not the 8 it enumerated"); + assert_eq!( + sites, 8, + "the call-site audit found {sites} sites, not the 8 it enumerated" + ); } /// Every entry of [`PROVEN`] must still name a live refusal and a test @@ -1538,7 +1619,9 @@ mod tests { "{reason:?} is listed as proven but is no longer a decode-path refusal -- drop it from PROVEN (the capability landed) or fix the string" ); assert!( - sources.iter().any(|src| src.contains(&format!("fn {gate}("))), + sources + .iter() + .any(|src| src.contains(&format!("fn {gate}("))), "{reason:?} names the proving test {gate}, which exists in neither stream.rs \ nor decode.rs" ); diff --git a/crates/ec-av1/src/restoration.rs b/crates/ec-av1/src/restoration.rs index 959a992f..a31dbaca 100644 --- a/crates/ec-av1/src/restoration.rs +++ b/crates/ec-av1/src/restoration.rs @@ -63,7 +63,12 @@ fn inverse_recenter(r: u32, v: u32) -> u32 { /// `aom_read_primitive_refsubexpfin` (`inv_recenter_finite_nonneg` composed /// with `read_primitive_subexpfin`): a value in `[0, n)` recentred around /// `reference`. -fn decode_unsigned_subexp_with_ref_msac(dec: &mut SymbolDecoder, n: u32, k: u32, reference: u32) -> u32 { +fn decode_unsigned_subexp_with_ref_msac( + dec: &mut SymbolDecoder, + n: u32, + k: u32, + reference: u32, +) -> u32 { let v = decode_subexp_msac(dec, n, k); if (reference << 1) <= n { inverse_recenter(reference, v) @@ -74,7 +79,13 @@ fn decode_unsigned_subexp_with_ref_msac(dec: &mut SymbolDecoder, n: u32, k: u32, /// Signed wrapper, matching the way `read_wiener_filter`/`read_sgrproj_filter` /// call `aom_read_primitive_refsubexpfin(rb, max-min+1, k, ref-min)+min`. -fn decode_signed_subexp_with_ref_msac(dec: &mut SymbolDecoder, low: i32, high: i32, k: u32, reference: i32) -> i32 { +fn decode_signed_subexp_with_ref_msac( + dec: &mut SymbolDecoder, + low: i32, + high: i32, + k: u32, + reference: i32, +) -> i32 { let n = (high - low) as u32; let r = (reference - low).clamp(0, n as i32) as u32; decode_unsigned_subexp_with_ref_msac(dec, n, k, r) as i32 + low @@ -274,7 +285,11 @@ fn read_wiener_direction(dec: &mut SymbolDecoder, chroma: bool, reference: &[i32 /// `read_wiener_filter` (decodeframe.c ~1595): both directions, updating /// `reference` in place (the running per-plane, per-tile reference state). -fn read_wiener_filter(dec: &mut SymbolDecoder, chroma: bool, reference: &mut WienerInfo) -> WienerInfo { +fn read_wiener_filter( + dec: &mut SymbolDecoder, + chroma: bool, + reference: &mut WienerInfo, +) -> WienerInfo { let vfilter = read_wiener_direction(dec, chroma, &reference.vfilter); let hfilter = read_wiener_direction(dec, chroma, &reference.hfilter); let info = WienerInfo { vfilter, hfilter }; @@ -400,6 +415,16 @@ pub(crate) struct RestorationGrid { impl RestorationGrid { pub(crate) fn new(lr: &LoopRestorationParams, frame_width: u32, frame_height: u32) -> Self { + Self::with_ss(lr, frame_width, frame_height, 1, 1) + } + + pub(crate) fn with_ss( + lr: &LoopRestorationParams, + frame_width: u32, + frame_height: u32, + ss_x: u8, + ss_y: u8, + ) -> Self { let mut horz_units = [1usize; 3]; let mut vert_units = [1usize; 3]; let mut units: [Vec; 3] = [Vec::new(), Vec::new(), Vec::new()]; @@ -410,7 +435,10 @@ impl RestorationGrid { let (pw, ph) = if plane == 0 { (frame_width, frame_height) } else { - ((frame_width + 1) / 2, (frame_height + 1) / 2) + ( + (frame_width + u32::from(ss_x)) >> ss_x, + (frame_height + u32::from(ss_y)) >> ss_y, + ) }; let unit_size = lr.loop_restoration_size[plane]; horz_units[plane] = count_units(pw, unit_size); @@ -462,7 +490,8 @@ pub(crate) fn read_lr( reference: &mut [(WienerInfo, SgrprojInfo); 3], mi_row: u32, mi_col: u32, - sb_mi: u32, fctx: &crate::decode::FrameCtx, + sb_mi: u32, + fctx: &crate::decode::FrameCtx, ) { if !lr.uses_lr { return; @@ -472,7 +501,11 @@ pub(crate) fn read_lr( if ftype == RestorationType::None { continue; } - let mi_size = if plane == 0 { 4u32 } else { 2u32 }; + let mi_size = if plane == 0 { + 4u32 + } else { + 4u32 >> (crate::decode::ss_x(fctx) as u32) + }; let unit_size = lr.loop_restoration_size[plane]; let horz_units = grid.horz_units[plane] as u32; let vert_units = grid.vert_units[plane] as u32; @@ -636,8 +669,16 @@ fn lr_sample( ) -> i32 { let col_c = col.clamp(0, plane_w as i64 - 1) as usize; i32::from( - lr_src_row(cdef, deblocked, stride, plane_w, plane_h, stripe_v_start, stripe_v_end, row) - [col_c], + lr_src_row( + cdef, + deblocked, + stride, + plane_w, + plane_h, + stripe_v_start, + stripe_v_end, + row, + )[col_c], ) } @@ -671,7 +712,11 @@ fn lr_src_row<'a>( let dist = stripe_v_start as i64 - 1 - row; ( deblocked, - if dist == 0 { stripe_v_start - 1 } else { stripe_v_start.saturating_sub(2) }, + if dist == 0 { + stripe_v_start - 1 + } else { + stripe_v_start.saturating_sub(2) + }, ) } } else if stripe_v_end == plane_h { @@ -712,7 +757,8 @@ fn apply_wiener_stripe( h_end: usize, v_start: usize, v_end: usize, - info: &WienerInfo, fctx: &crate::decode::FrameCtx, + info: &WienerInfo, + fctx: &crate::decode::FrameCtx, ) { let w = h_end - h_start; let h = v_end - v_start; @@ -738,8 +784,16 @@ fn apply_wiener_stripe( // stripe substitution is resolved once per row by `lr_src_row`, and only // a unit touching the plane's left or right edge needs the clamped copy. let hf = &info.hfilter; - let t16: [i16; 8] = - [hf[0] as i16, hf[1] as i16, hf[2] as i16, hf[3] as i16, hf[4] as i16, hf[5] as i16, hf[6] as i16, 0]; + let t16: [i16; 8] = [ + hf[0] as i16, + hf[1] as i16, + hf[2] as i16, + hf[3] as i16, + hf[4] as i16, + hf[5] as i16, + hf[6] as i16, + 0, + ]; // lane-mc: the intermediate is `i16` now. Both bounds are inside that // range at 8- and 10-bit (12-bit is refused in stream.rs), so the // kernel's saturating store composes with this clamp instead of changing @@ -748,7 +802,9 @@ fn apply_wiener_stripe( let mut hbuf = WIENER_ROW_SCRATCH.with(|c| std::mem::take(&mut *c.borrow_mut())); for r in 0..rows { let row = v_start as i64 - 3 + r as i64; - let src = lr_src_row(cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row); + let src = lr_src_row( + cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, + ); let lo = h_start as i64 - 3; let out = &mut inter[r * w..r * w + w]; if lo >= 0 && lo as usize + w + 7 <= plane_w { @@ -815,31 +871,27 @@ thread_local! { // `~/.cache/aom-oracle/src/av1/common/restoration.c` to rule out any other // transcription slip. const SGR_X_BY_XPLUS1: [i32; 256] = [ - 1, 128, 171, 192, 205, 213, 219, 224, 228, 230, 233, 235, 236, 238, 239, - 240, 241, 242, 243, 243, 244, 244, 245, 245, 246, 246, 247, 247, 247, 247, - 248, 248, 248, 248, 249, 249, 249, 249, 249, 250, 250, 250, 250, 250, 250, - 250, 251, 251, 251, 251, 251, 251, 251, 251, 251, 251, 252, 252, 252, 252, - 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 253, 253, - 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, - 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 254, 254, 254, - 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, - 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, - 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, - 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, - 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 256, + 1, 128, 171, 192, 205, 213, 219, 224, 228, 230, 233, 235, 236, 238, 239, 240, 241, 242, 243, + 243, 244, 244, 245, 245, 246, 246, 247, 247, 247, 247, 248, 248, 248, 248, 249, 249, 249, 249, + 249, 250, 250, 250, 250, 250, 250, 250, 251, 251, 251, 251, 251, 251, 251, 251, 251, 251, 252, + 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 253, 253, 253, + 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, + 253, 253, 253, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 256, ]; /// `av1_one_by_x` (`restoration.c`): `round(4096/n)` for `n` in `1..=25` /// (`MAX_NELEM`, `(2*MAX_RADIUS+1)^2`), indexed `[n-1]`. const SGR_ONE_BY_X: [i32; 25] = [ - 4096, 2048, 1365, 1024, 819, 683, 585, 512, 455, 410, 372, 341, 315, 293, 273, 256, 241, 228, 216, 205, 195, 186, - 178, 171, 164, + 4096, 2048, 1365, 1024, 819, 683, 585, 512, 455, 410, 372, 341, 315, 293, 273, 256, 241, 228, + 216, 205, 195, 186, 178, 171, 164, ]; const SGRPROJ_MTABLE_BITS: u32 = 20; @@ -877,7 +929,8 @@ fn compute_ab( // stripe, ~70 KB each at a 64-wide unit), i.e. an allocation whose every // page then faulted in on first touch. Every entry is written below, so // the resize's fill value is never read. - ab: &mut Vec<(i32, i64)>, fctx: &crate::decode::FrameCtx, + ab: &mut Vec<(i32, i64)>, + fctx: &crate::decode::FrameCtx, ) { let gw = w + 2; let gh = h + 2; @@ -927,8 +980,9 @@ fn compute_ab( let i = gi as i64 - 1; { let mut accumulate = |row: i64, add: bool| { - let src = - lr_src_row(cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row); + let src = lr_src_row( + cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, + ); fill_clamped_row(src, plane_w, base_col, &mut samp); if add { for ((c_a, c_b), &px) in col_a.iter_mut().zip(col_b.iter_mut()).zip(samp.iter()) @@ -967,7 +1021,15 @@ fn compute_ab( a_row[gj] = a; b_row[gj] = b; } - ab_row(a_row, b_row, n, s, bd_shift, one_by_x, &mut ab[gi * gw..gi * gw + gw]); + ab_row( + a_row, + b_row, + n, + s, + bd_shift, + one_by_x, + &mut ab[gi * gw..gi * gw + gw], + ); let dump_gj = 6i64 - h_start as i64 + 1; if dump && v_start as i64 + i == 60 && r == 1 && (0..gw as i64).contains(&dump_gj) { let gj = dump_gj as usize; @@ -1006,14 +1068,22 @@ fn ab_point(a: i32, b: i32, n: i32, s: i32, bd_shift: u32, one_by_x: i64) -> (i3 let b_s = round2_u31(b, bd_shift); let p = (a_s * n - b_s * b_s).max(0); let a_val = SGR_X_BY_XPLUS1[z_index(p, s)]; - let b_val = round2((256 - a_val) as i64 * i64::from(b) * one_by_x, SGRPROJ_RECIP_BITS); + let b_val = round2( + (256 - a_val) as i64 * i64::from(b) * one_by_x, + SGRPROJ_RECIP_BITS, + ); (a_val, b_val) } /// One grid row's `(A, B)` pairs: [`ab_point`] per element, eight at a time /// under AVX2 (the kernel is that arithmetic lane-wise, same widths). fn ab_row( - a_row: &[i32], b_row: &[i32], n: i32, s: i32, bd_shift: u32, one_by_x: i64, + a_row: &[i32], + b_row: &[i32], + n: i32, + s: i32, + bd_shift: u32, + one_by_x: i64, out: &mut [(i32, i64)], ) { let mut done = 0usize; @@ -1054,7 +1124,12 @@ mod sgr_simd { #[target_feature(enable = "avx2")] #[allow(clippy::too_many_arguments)] pub unsafe fn ab_row_avx2( - a_row: &[i32], b_row: &[i32], n: i32, s: i32, bd_shift: u32, one_by_x: i32, + a_row: &[i32], + b_row: &[i32], + n: i32, + s: i32, + bd_shift: u32, + one_by_x: i32, out: &mut [(i32, i64)], ) -> usize { let len = out.len().min(a_row.len()).min(b_row.len()); @@ -1070,8 +1145,16 @@ mod sgr_simd { let sh_b = _mm_cvtsi32_si128(SGRPROJ_RECIP_BITS as i32); let sh_a = _mm_cvtsi32_si128(2 * bd_shift as i32); let sh_bd = _mm_cvtsi32_si128(bd_shift as i32); - let rnd_a = _mm256_set1_epi32(if bd_shift == 0 { 0 } else { 1 << (2 * bd_shift - 1) }); - let rnd_bd = _mm256_set1_epi32(if bd_shift == 0 { 0 } else { 1 << (bd_shift - 1) }); + let rnd_a = _mm256_set1_epi32(if bd_shift == 0 { + 0 + } else { + 1 << (2 * bd_shift - 1) + }); + let rnd_bd = _mm256_set1_epi32(if bd_shift == 0 { + 0 + } else { + 1 << (bd_shift - 1) + }); let mut av = [0i32; 8]; let mut bv = [0i32; 8]; let mut gj = 0usize; @@ -1115,7 +1198,11 @@ mod sgr_simd { /// width narrower). #[inline] fn round2_u31(value: i32, shift: u32) -> i32 { - if shift == 0 { value } else { (value + (1 << (shift - 1))) >> shift } + if shift == 0 { + value + } else { + (value + (1 << (shift - 1))) >> shift + } } /// Copies one source row's `out.len()` samples starting at `base_col` into a @@ -1150,7 +1237,8 @@ fn apply_sgrproj_stripe( h_end: usize, v_start: usize, v_end: usize, - info: &SgrprojInfo, fctx: &crate::decode::FrameCtx, + info: &SgrprojInfo, + fctx: &crate::decode::FrameCtx, ) { let w = h_end - h_start; let h = v_end - v_start; @@ -1166,10 +1254,16 @@ fn apply_sgrproj_stripe( (std::mem::take(&mut m.0), std::mem::take(&mut m.1)) }); if r0 > 0 { - compute_ab(cdef, deblocked, stride, plane_w, plane_h, h_start, v_start, v_end, w, h, r0, s0, &mut buf0, fctx); + compute_ab( + cdef, deblocked, stride, plane_w, plane_h, h_start, v_start, v_end, w, h, r0, s0, + &mut buf0, fctx, + ); } if r1 > 0 { - compute_ab(cdef, deblocked, stride, plane_w, plane_h, h_start, v_start, v_end, w, h, r1, s1, &mut buf1, fctx); + compute_ab( + cdef, deblocked, stride, plane_w, plane_h, h_start, v_start, v_end, w, h, r1, s1, + &mut buf1, fctx, + ); } let ab0 = (r0 > 0).then_some(&buf0); let ab1 = (r1 > 0).then_some(&buf1); @@ -1185,7 +1279,9 @@ fn apply_sgrproj_stripe( let bytes: Vec = (59..=61) .flat_map(|row| { (5..=7).map(move |col| { - lr_sample(cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, col) + lr_sample( + cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, col, + ) }) }) .collect(); @@ -1265,7 +1361,8 @@ fn apply_sgrproj_stripe( let (a_ur, b_ur) = ab[idx(i - 1, j + 1)]; let (a_dl, b_dl) = ab[idx(i + 1, j - 1)]; let (a_dr, b_dr) = ab[idx(i + 1, j + 1)]; - let a = (a_c + a_u + a_d + a_l + a_r) as i64 * 4 + (a_ul + a_ur + a_dl + a_dr) as i64 * 3; + let a = (a_c + a_u + a_d + a_l + a_r) as i64 * 4 + + (a_ul + a_ur + a_dl + a_dr) as i64 * 3; let b = (b_c + b_u + b_d + b_l + b_r) * 4 + (b_ul + b_ur + b_dl + b_dr) * 3; let flt1 = round2(a * dgd + b, SGRPROJ_SGR_BITS + 5 - SGRPROJ_RST_BITS); v += xq[1] as i64 * (flt1 - u); @@ -1278,9 +1375,15 @@ fn apply_sgrproj_stripe( "EC_LR_CALL_DUMP combine: c={:?} u_tap(row60)={:?} d={:?} l={:?} r={:?} \ ul={:?} ur={:?} dl={:?} dr={:?} dense a={a} b={b} dgd={dgd} \ u={u} flt1={flt1} xq={xq:?} v_before_final_round={v}", - ab[idx(i, j)], ab[idx(i - 1, j)], ab[idx(i + 1, j)], ab[idx(i, j - 1)], - ab[idx(i, j + 1)], ab[idx(i - 1, j - 1)], ab[idx(i - 1, j + 1)], - ab[idx(i + 1, j - 1)], ab[idx(i + 1, j + 1)] + ab[idx(i, j)], + ab[idx(i - 1, j)], + ab[idx(i + 1, j)], + ab[idx(i, j - 1)], + ab[idx(i, j + 1)], + ab[idx(i - 1, j - 1)], + ab[idx(i - 1, j + 1)], + ab[idx(i + 1, j - 1)], + ab[idx(i + 1, j + 1)] ); } } @@ -1311,7 +1414,8 @@ fn filter_restoration_unit( v_start: usize, v_end: usize, ss_y: u32, - filter: UnitFilter, fctx: &crate::decode::FrameCtx, + filter: UnitFilter, + fctx: &crate::decode::FrameCtx, ) { let full_stripe_height = 64usize >> ss_y; let runit_offset = 8usize >> ss_y; @@ -1333,10 +1437,32 @@ fn filter_restoration_unit( } match filter { UnitFilter::Wiener(info) => apply_wiener_stripe( - out, cdef, deblocked, stride, plane_w, plane_h, h_start, h_end, stripe_v_start, stripe_v_end, &info, fctx, + out, + cdef, + deblocked, + stride, + plane_w, + plane_h, + h_start, + h_end, + stripe_v_start, + stripe_v_end, + &info, + fctx, ), UnitFilter::Sgrproj(info) => apply_sgrproj_stripe( - out, cdef, deblocked, stride, plane_w, plane_h, h_start, h_end, stripe_v_start, stripe_v_end, &info, fctx, + out, + cdef, + deblocked, + stride, + plane_w, + plane_h, + h_start, + h_end, + stripe_v_start, + stripe_v_end, + &info, + fctx, ), UnitFilter::None => {} } @@ -1395,12 +1521,16 @@ pub(crate) fn apply_loop_restoration_plane( let out = unsafe { so.get() }; for rrow in r_lo..r_hi { lr_unit_row( - out, cdef, deblocked, stride, plane_w, plane_h, ss_y, unit_size, grid, - plane, rrow, &rows, fctx, + out, cdef, deblocked, stride, plane_w, plane_h, ss_y, unit_size, grid, plane, + rrow, &rows, fctx, ); } }); - debug_assert_eq!(rows.len(), grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units"); + debug_assert_eq!( + rows.len(), + grid.vert_units[plane], + "RU row walk must match RestorationGrid::new's count_units" + ); return; }; for rrow in r_lo..r_hi.min(rows.len()) { @@ -1435,7 +1565,11 @@ pub(crate) fn lr_unit_rows( let mut y0 = 0u32; while y0 < plane_h as u32 { let remaining_h = plane_h as u32 - y0; - let uh = if remaining_h < ext_size { remaining_h } else { unit_size }; + let uh = if remaining_h < ext_size { + remaining_h + } else { + unit_size + }; let v_start = (y0 as i64 - voffset as i64).max(0) as u32; let mut v_end = y0 + uh; if v_end < plane_h as u32 { @@ -1471,7 +1605,11 @@ fn lr_unit_row( let mut rcol = 0usize; while x0 < plane_w as u32 { let remaining_w = plane_w as u32 - x0; - let uw = if remaining_w < ext_size { remaining_w } else { unit_size }; + let uw = if remaining_w < ext_size { + remaining_w + } else { + unit_size + }; let filter = grid.get(plane, rrow, rcol); if filter != UnitFilter::None { filter_restoration_unit( @@ -1486,7 +1624,8 @@ fn lr_unit_row( v_start as usize, v_end as usize, ss_y, - filter, fctx, + filter, + fctx, ); } x0 += uw; @@ -1510,7 +1649,15 @@ mod tests { /// `SGRPROJ_PRJ_MAX-MIN+1`. #[test] fn subexp_roundtrips_every_value() { - for &(num_syms, k) in &[(16u32, 1u32), (32, 2), (64, 3), (96, 4), (128, 4), (3, 1), (1, 3)] { + for &(num_syms, k) in &[ + (16u32, 1u32), + (32, 2), + (64, 3), + (96, 4), + (128, 4), + (3, 1), + (1, 3), + ] { for v in 0..num_syms { let mut enc = SymbolEncoder::new(); write_subexp(&mut enc, num_syms, k, v); @@ -1548,10 +1695,10 @@ mod tests { #[test] fn signed_subexp_with_ref_roundtrips() { let cases: [(i32, i32, u32); 4] = [ - (-5, 11, 1), // Wiener tap0 - (-23, 9, 2), // Wiener tap1 - (-17, 47, 3), // Wiener tap2 - (-96, 32, 4), // SGR xqd0 + (-5, 11, 1), // Wiener tap0 + (-23, 9, 2), // Wiener tap1 + (-17, 47, 3), // Wiener tap2 + (-96, 32, 4), // SGR xqd0 ]; for (low, high, k) in cases { let n = (high - low) as u32; @@ -1563,8 +1710,12 @@ mod tests { write_unsigned_subexp_with_ref(&mut enc, n, k, r, coded_v); let payload = enc.finish(); let mut dec = SymbolDecoder::new(&payload); - let got = decode_signed_subexp_with_ref_msac(&mut dec, low, high, k, reference_v); - assert_eq!(got, v, "low={low} high={high} k={k} reference={reference_v} v={v}"); + let got = + decode_signed_subexp_with_ref_msac(&mut dec, low, high, k, reference_v); + assert_eq!( + got, v, + "low={low} high={high} k={k} reference={reference_v} v={v}" + ); } } } @@ -1595,7 +1746,11 @@ mod tests { let mut dec = SymbolDecoder::new(&payload); let mut rref = WienerInfo::default(); for u in &units { - assert_eq!(read_wiener_filter(&mut dec, chroma, &mut rref), *u, "chroma={chroma}"); + assert_eq!( + read_wiener_filter(&mut dec, chroma, &mut rref), + *u, + "chroma={chroma}" + ); } } } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 8dcefab3..a8535af3 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -1662,10 +1662,11 @@ fn decode_frame( // unaffected. Gates: `a_non_420_subsampled_sequence_header_is_refused_by_ // name` below (hand-built profile 1/2 headers plus a real aomenc 4:4:4 // stream). - if seq.subsampling_x != 1 || seq.subsampling_y != 1 { + // lane-av1-444: 4:4:4 (ss 0,0) is decoded. 4:2:2 (ss_x != ss_y) is not. + if seq.subsampling_x != seq.subsampling_y { return Err(Error::unsupported( "AV1 decode_stream", - "a chroma format other than 4:2:0 (subsampling_x/y != 1/1): every chroma extent in this decoder is hardcoded to 4:2:0 and the probe emits 4:2:0 planes only, so a 4:4:4/4:2:2 stream would decode silently wrong pixels", + "a chroma format of 4:2:2 (subsampling_x != subsampling_y): this decoder decodes 4:2:0 and 4:4:4; 4:2:2 is not ported", )); } // lane-av1txr-r2: `quantization_params.using_qmatrix` (spec 5.9.12) is @@ -1739,6 +1740,7 @@ fn decode_frame( // the bit depth so a mono frame decoded on a thread that last saw a // 4:2:0 one never inherits the wrong plane count. crate::decode::set_mono(seq.mono_chrome, fctx); + crate::decode::set_subsampling(seq.subsampling_x, seq.subsampling_y, fctx); // lane-av1txr: spec 5.9.2 `disable_cdf_update` -- a frame that sets it // codes every tile symbol against the CDFs it started with, so the tile // readers must not adapt (libaom `decodeframe.c:2909`: @@ -2153,11 +2155,11 @@ pub(crate) mod tests { #[test] fn a_non_420_subsampled_sequence_header_is_refused_by_name() { const NAME: &str = "a_non_420_subsampled_sequence_header_is_refused_by_name"; - const REFUSAL: &str = "a chroma format other than 4:2:0"; + const REFUSAL: &str = "a chroma format of 4:2:2"; let fctx = &crate::decode::FrameCtx::new(); let picture = test_card(64, 64); let encoded = encode_key_frame_with_ctx(&picture, 100, 0.5, fctx).unwrap(); - for (profile, label, sx, sy) in [(1u8, "4:4:4", 0u8, 0u8), (2, "4:2:2", 1, 0)] { + for (profile, label, sx, sy) in [(2u8, "4:2:2", 1u8, 0u8)] { let color = ec_av1_syntax::ColorConfig { num_planes: 3, subsampling_x: sx, diff --git a/lanes/av1444.report.md b/lanes/av1444.report.md new file mode 100644 index 00000000..f07c703f --- /dev/null +++ b/lanes/av1444.report.md @@ -0,0 +1,99 @@ +# Lane av1444: AV1 4:4:4 (WIP) + +Branch `lane-av1-444`, base `7f8d6cf6`. Worktree +`/home/user/Documents/Code/Rust/edith_codecs-av1444`. Private +`CARGO_TARGET_DIR=$HOME/.cache/cargo-target-av1444`, +`TMPDIR=$HOME/.cache/av1444-tmp`. Not pushed. + +Status: **WIP**. 4:4:4 is no longer refused. Luma of a square profile-1 +keyframe is byte-exact vs aomdec and ffmpeg. Chroma is not. 4:2:2 stays a +named refusal. Full suite was not run. + +Commit: blocked by the global pre-commit hook +(`format-staged.py` runs `rustfmt ` with no `--edition`). That rustfmt +rejects the pre-existing `let` chains in `decode.rs` (`edition = "2024"`). +`rustfmt --edition 2024 --check` wants a wholesale reformat of `decode.rs`, +so the hook was not bypassed and the reformat was not applied. Changes are +staged, not committed. `git status` in the worktree is the source of truth. + +## What changed + +- `stream.rs`: the sequence-header refusal now fires only when + `subsampling_x != subsampling_y` (4:2:2). 4:4:4 (`0,0`) and 4:2:0 (`1,1`) + proceed. `set_subsampling` publishes the header values onto `FrameCtx` + once per frame, same slot as `set_mono`. +- `decode.rs`: chroma plane size, tx size, CFL window, loop-filter chroma + step, and the 8x8 leaf's chroma unit follow `ss_x`/`ss_y`. At 4:4:4 an + 8x8 leaf's chroma is 8x8 (`TxbSet::Chroma8`, `scans.0`), not the hardcoded + 4x4. A square block below 64 uses chroma tx = block side. A 64 block still + takes the single-TU arm (see the divergence below). +- `restoration.rs`: chroma restoration unit size is no longer hardcoded + half-res. Not witnessed (the proof stream disables restoration). +- `refusal_inventory.rs`: the old "any non-4:2:0" string is replaced by the + 4:2:2 string. Gate `a_non_420_subsampled_sequence_header_is_refused_by_name` + asserts that 4:2:2 string only. The gate was not re-run in this wrap-up. + +## Byte-exactness + +Oracle: `~/.cache/aom-oracle/build/aomenc` + `aomdec`, cross-checked with +ffmpeg 8.x raw `yuv444p` / `yuv420p`. aomdec `--rawvideo` and ffmpeg +`-pix_fmt yuv444p` were identical on the 444 witness, so the mismatch below +is ours. + +Recipe (both profiles): `testsrc2=128x128`, 2 frames, `--cpu-used=8 +--usage=2 --end-usage=q --cq-level=40 --min-partition-size=16 +--enable-rect-partitions=0 --enable-cdef=0 --enable-restoration=0 +--loopfilter-control=0 --threads=1 --obu`. Profile 1 adds `--profile=1` and +yuv444p input. Probe: `decode_probe` 8-bit dump (low byte of each plane). + +| stream | vs | result | +| --- | --- | --- | +| profile 0, filters off, square | ffmpeg `yuv420p` | IDENTICAL (98304 B, 2 frames) | +| profile 1, filters off, square | aomdec and ffmpeg `yuv444p` | luma IDENTICAL both frames (0 diffs / 16384). chroma NOT. 37389 diffs / 98304. First chroma mismatch: frame 0 U (112, 0), ours 192 vs oracle 167 | +| profile 1 with rect partitions (earlier `sq8.obu`) | — | panics in `decode_rect4_16_strip` → `reconstruct_rect` (`len 64, index 64`). Not a witness | + +`av1-profile1-444.ivf` was not re-decoded. It contains the rect/1:4 shapes +this tree still mishandles, so it will not be byte-exact. + +## Exact divergence + +First profile-1 block is an 8x8 leaf at mi (0, 0). Before the leaf fix that +leaf read 4x4 chroma, under-consumed symbols, and luma diverged at pixel 8. +After the fix, luma of both frames matches and U/V match until x=112. + +x=112 sits inside the 64x64 block at pixel (64, 0) (`EC_BLOCK side=64 +at=(0, 4)`, `chroma_in=32`). That block takes `decode_block`'s +`logical_tx == side` arm (side 64, one luma TU whose coefficient grid is +32). The arm then reads **one** chroma unit with reconstruct side +`chroma_side` (64 at 4:4:4) and coeff tx `chroma_tx` (32). libaom codes +**four** TX_32X32 chroma units for a 64x64 uv plane block at 4:4:4 +(`av1_get_max_uv_txsize` of a 64x64 uv bsize is TX_32X32, 2x2). The +multi-TU arm already loops `cn = chroma_side / chroma_tx` units, but a +64 block never enters it because `logical_tx == side`. Comment is on the +U `read_plane` in that arm. + +Quadrant diffs inside that 64x64, U plane, frame 0: (0,0) 764/1024, +(1,0) 896/1024, (0,1) 973/1024, (1,1) 1024/1024. Row 0 of the block matches +for the first 48 samples, then goes flat-wrong. That is a wrong chroma +unit, not a luma desync. + +## Still refused or unproven + +| shape | disposition | +| --- | --- | +| 4:2:2 (`ss_x != ss_y`) | refused by name. Gate re-scoped, not re-run | +| 4:4:4 64x64 chroma (and any block that takes the single-TU arm with `chroma_side != chroma_tx`) | decodes, not byte-exact. Next edit: when `ss==0` and `chroma_side > chroma_tx`, use the existing `cn` loop instead of one `read_plane` | +| 1:4 strips (`decode_rect4_16`) | panic, not a named refusal. Not in the square witness | +| sub-8 group chroma (`Chroma4` / size 4 in the 4x4-group tails) | still hardcoded 4:2:0. Square witness did not hit this first | +| loop filter, CDEF, restoration on | threaded with `ss_*`, not witnessed (proof stream disables them) | +| 10-bit 4:4:4 | not encoded, not compared | +| `av1-profile1-444.ivf` | not re-run | +| full `cargo test -p ec-av1` | not run. Do not claim green | + +## Next + +1. In `decode_block`, when `logical_tx == side` but `chroma_side > chroma_tx`, code chroma as `cn` x `cn` units (the multi-TU loop already does this). Re-run the square profile-1 recipe. Luma must stay identical; chroma must go to 0 diffs. +2. Then turn filters back on one at a time. +3. Then a 10-bit profile-2 444 stream (`yuv444p10le`). +4. Then the existing `av1-profile1-444.ivf`, after the 1:4 path no longer panics. +5. Full suite, `--test-threads=1`, `TMPDIR` on `$HOME`.