diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index fa42e872..a17c3b27 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -477,7 +477,7 @@ pub const CFL_SIGN: [u16; 9] = [1418, 2123, 13340, 18405, 26972, 28343, 32294, 3 /// `av1_filter_intra_allowed_bsize`'s "both sides <= 32" bound, so libaom /// reads `use_filter_intra` there too and the missing rows dropped the /// symbol on every DC_PRED 1:4 strip. -pub const FILTER_INTRA: [[u16; 3]; 12] = [ +pub const FILTER_INTRA: [[u16; 3]; 14] = [ [4621, 32768, 0], [7866, 32768, 0], [12408, 32768, 0], @@ -490,6 +490,12 @@ pub const FILTER_INTRA: [[u16; 3]; 12] = [ [5893, 32768, 0], [18101, 32768, 0], [20229, 32768, 0], + // lane-r14: the 16x16-level 1:4 pair (`default_filter_intra_cdfs` index + // 17/16 -- BLOCK_16X4/BLOCK_4X16, generated by + // `scripts/extract-filter-intra-cdfs.py`), classes 12/13. Both sides <= 32, + // so libaom writes `use_filter_intra` on every DC_PRED strip of this shape. + [10368, 32768, 0], + [12770, 32768, 0], ]; /// `default_filter_intra_mode_cdf` (entropymode.c): which of the five /// `FILTER_INTRA_MODES` a `use_filter_intra` block picks. diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index f4d4dd64..acd19522 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -186,6 +186,22 @@ pub(crate) enum TxbSet { /// `tx_size_sqr == TX_4X4`, intra returns `EXT_TX_SET_DTT4_IDTX_1DDCT`, /// the same seven-symbol set (and same `TX_4X4` row) `Luma4Set1` reads. LumaRect4x8Set1, + /// The luma 16x4/4x16 transform of a 16x16-level `PARTITION_HORZ_4`/ + /// `PARTITION_VERT_4` strip (lane-r14). `get_txsize_entropy_ctx` + /// ((`txsize_sqr_map` + `txsize_sqr_up_map` + 1) >> 1) resolves `TX_16X4` + /// to `TX_8X8`, and the transform has 64 coded positions, so EVERY table + /// here is [`Luma8`](TxbSet::Luma8)'s (`eob_pt_64` included) except + /// `tx_type`: `read_tx_type` indexes `intra_ext_tx_cdf[eset] + /// [txsize_sqr_map[TX_16X4]]` = the `TX_4X4` row, and + /// `av1_get_ext_tx_set_type` at `tx_size_sqr_up == TX_16X16` with + /// `tx_size_sqr == TX_4X4` returns `EXT_TX_SET_DTT4_IDTX` under + /// `reduced_tx_set` -- the same pair [`LumaRect4x8`](TxbSet::LumaRect4x8) + /// reads, which differs from this variant only in its 32-position `eob_pt`. + LumaRect16x4, + /// [`LumaRect16x4`](TxbSet::LumaRect16x4)'s `reduced_tx_set: false` + /// counterpart: `EXT_TX_SET_DTT4_IDTX_1DDCT`, the seven-symbol `TX_4X4` + /// row [`LumaRect4x8Set1`](TxbSet::LumaRect4x8Set1) reads. + LumaRect16x4Set1, } /// The tables one transform block is coded with, borrowed from the state for @@ -541,7 +557,7 @@ pub(crate) struct Cdfs { /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32, `[4]`=32x16, `[5]`=16x32, /// `[6]`=16x8, `[7]`=8x16, `[8]`=4x8, `[9]`=8x4, `[10]`=32x8, `[11]`=8x32) -- /// see [`cdf::FILTER_INTRA`]. - pub filter_intra: [[u16; 3]; 12], + pub filter_intra: [[u16; 3]; 14], /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks. pub filter_intra_mode: [u16; 6], /// `TxMode::Select`'s `tx_depth` flag at an 8x8 block, by `tx_size_context` @@ -1767,6 +1783,30 @@ impl Cdfs { tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()), eob_pt_class1: Some(&mut self.eob_pt_128_luma_class1), }, + TxbSet::LumaRect16x4 => TxbTables { + side: 8, + txb_skip: &mut self.txb_skip_luma_8, + eob_pt: &mut self.eob_pt_64_luma, + eob_extra: &mut self.eob_extra_luma_8, + base: &mut self.base_luma_8, + base_eob: &mut self.base_eob_luma_8, + br: &mut self.br_luma_8, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1), + }, + TxbSet::LumaRect16x4Set1 => TxbTables { + side: 8, + txb_skip: &mut self.txb_skip_luma_8, + eob_pt: &mut self.eob_pt_64_luma, + eob_extra: &mut self.eob_extra_luma_8, + base: &mut self.base_luma_8, + base_eob: &mut self.base_eob_luma_8, + br: &mut self.br_luma_8, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.intra_tx_type_4_set1[mode].as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1), + }, TxbSet::ChromaRect8x4 => TxbTables { side: 8, txb_skip: &mut self.txb_skip_chroma_8, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 415ad419..b7d95cff 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1952,6 +1952,27 @@ pub(crate) fn rect4_coeff_hits() -> usize { RECT4_COEFF_HITS.with(|c| c.get()) } +// lane-r14: the 16x16-level 1:4 pair -- four 16x4 (`PARTITION_HORZ_4`) or +// 4x16 (`PARTITION_VERT_4`) strips, `CHROMA` counting the odd strips that +// close a 4:2:0 chroma pair and `COEFF` the strips with a coded residual. +thread_local! { + static RECT4_16_HORZ_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static RECT4_16_VERT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static RECT4_16_CHROMA_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static RECT4_16_COEFF_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current values of the 16x16-level 1:4 counters: `(16x4 strips, 4x16 +/// strips, chroma-reference strips, strips with a coded residual)`. +pub fn rect4_16_counters() -> (usize, usize, usize, usize) { + ( + RECT4_16_HORZ_HITS.with(|c| c.get()), + RECT4_16_VERT_HITS.with(|c| c.get()), + RECT4_16_CHROMA_HITS.with(|c| c.get()), + RECT4_16_COEFF_HITS.with(|c| c.get()), + ) +} + /// Current value of `RECT4_32_HORZ_HITS` (32x8 strips decoded). pub(crate) fn rect4_32_horz_hits() -> usize { RECT4_32_HORZ_HITS.with(|c| c.get()) @@ -4667,6 +4688,10 @@ fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option { // tile (class symbol-consumption-gap). (32, 8) => Some(10), (8, 32) => Some(11), + // lane-r14: the 16x16-level 1:4 pair (`decode_rect4_16`'s 16x4/4x16 + // strips), rows 17/16 of `default_filter_intra_cdfs`. + (16, 4) => Some(12), + (4, 16) => Some(13), _ if bw == bh => filter_intra_size_class(bw), _ => None, } @@ -4692,7 +4717,13 @@ fn palette_bsize_ctx(side: usize) -> Option { /// `trailing_zeros` is `log2` exactly. `None` past `av1_allow_palette`'s own /// bound (`block_size_wide/high <= 64`, `sb_type >= BLOCK_8X8`). fn palette_bsize_ctx_wh(bw: usize, bh: usize) -> Option { - if !(8..=64).contains(&bw) || !(8..=64).contains(&bh) { + // lane-r14: `av1_allow_palette`'s bound is `sb_type >= BLOCK_8X8` by the + // BLOCK_SIZES_ALL enum INDEX, which the 1:4 shapes BLOCK_4X16 (16) and + // BLOCK_16X4 (17) clear -- a 64-pixel block, so `av1_get_palette_bsize_ctx` + // (`num_pels_log2 - num_pels_log2[BLOCK_8X8]`) is 0 for them, exactly as + // for an 8x8. Reading the dimension bound as "both sides >= 8" dropped + // `palette_y_mode` on every 16x4/4x16 strip of a screen-content frame. + if bw > 64 || bh > 64 || bw * bh < 64 { return None; } Some(((bw * bh) as u32).trailing_zeros() as usize - 6) @@ -5014,6 +5045,12 @@ fn read_intra_mode_rect( above_mode: usize, left_mode: usize, cfl: bool, + // lane-r14: `is_chroma_reference` -- false only for a strip one mi tall + // (or wide) that is NOT the odd one of its 4:2:0 pair, where libaom reads + // no `uv_mode`/`cfl_alpha`/`angle_delta_uv`/`palette_uv` at all (the pair's + // chroma is coded once, on the closing strip). Every other caller is a + // block big enough to own its chroma and passes `true`. + has_chroma: bool, bw: usize, bh: usize, enable_filter_intra: bool, @@ -5077,21 +5114,23 @@ fn read_intra_mode_rect( 0 }; istep!("angle_y", angle_delta_y); - let uv_mode = if cfl { + let uv_mode = if !has_chroma { + DC_PRED + } else if cfl { dec.symbol(&mut cdfs.uv_mode_cfl[mode]) } else { dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) }; istep!("uv_mode", uv_mode as i32); - let alpha = if cfl && uv_mode == UV_CFL_PRED { + let alpha = if has_chroma && cfl && uv_mode == UV_CFL_PRED { Some(read_cfl_alphas(dec, cdfs)) } else { None }; - if (9..=12).contains(&uv_mode) { + if has_chroma && (9..=12).contains(&uv_mode) { SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); } - let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { + let angle_delta_uv = if has_chroma && (V_PRED..=D67_PRED).contains(&uv_mode) { DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) } else { @@ -5131,7 +5170,11 @@ fn read_intra_mode_rect( palette_y_pending = Some((n, colors)); } let palette_uv_mode_ctx = usize::from(use_palette_y); - if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { + // `read_palette_mode_info`'s own `xd->is_chroma_ref` guard. + if has_chroma + && uv_mode == DC_PRED + && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 + { let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache); palette_uv_pending = Some((n, u_colors, v_colors)); @@ -5687,6 +5730,7 @@ fn decode_block_rect( nb_above_mode, nb_left_mode, true, + true, bw, bh, enable_filter_intra, @@ -6161,6 +6205,7 @@ fn decode_leaf_rect( above_mode, left_mode, true, + true, bw, bh, enable_filter_intra, @@ -6440,6 +6485,7 @@ fn decode_block_rect4( above_mode, left_mode, true, + true, bw, bh, enable_filter_intra, @@ -6741,6 +6787,332 @@ fn decode_block_rect4( Ok((mode, uv_predict_mode)) } +/// Decodes the four 16x4 / 4x16 strips of a 16x16-level `PARTITION_HORZ_4` / +/// `PARTITION_VERT_4` (lane-r14) -- [`decode_block_rect4`]'s pair one level +/// down, and the first shape here whose luma block is a single mi row (or +/// column). +/// +/// What is new at this size is CHROMA. `is_chroma_reference` +/// (`av1_common_int.h`) is false for a 4:2:0 block one mi tall (wide) on an +/// EVEN mi row (column), so strips 0 and 2 read no `uv_mode`/`cfl_alpha`/ +/// `angle_delta_uv` at all and code no chroma residual; strips 1 and 3 close +/// the pair and carry the chroma of the whole 16x8 (8x16) luma pair -- +/// `setup_pred_plane`'s own `if (subsampling_y && (mi_row & 1) && +/// mi_size_high[bsize] == 1) mi_row -= 1` offset, i.e. an 8x4 (4x8) chroma +/// transform at the PAIR's origin, with CfL averaging over the pair's luma. +/// +/// Tables: LUMA `TX_16X4`/`TX_4X16` averages to the 8x8 entropy context +/// ((0 + 2 + 1) >> 1 = 1) with 64 coded positions, so its CDF set is +/// [`TxbSet::LumaRect16x4`] (`Luma8`'s tables, the `TX_4X4` `tx_type` row); +/// scans are [`SCAN_16X4`]/[`SCAN_4X16`]. CHROMA `TX_8X4`/`TX_4X8` is +/// [`TxbSet::ChromaRect8x4`] with [`SCAN_8X4`]/[`SCAN_4X8`]. Angle deltas ARE +/// coded (`av1_use_angle_delta` compares the BLOCK_SIZES_ALL enum index, and +/// BLOCK_16X4 is 17), and so is `use_filter_intra` (both sides <= 32) off this +/// shape's own CDF row. +/// +/// A split transform (`tx_depth != 0`, `sub_tx_size_map[TX_16X4] == TX_8X4`) +/// refuses by name rather than guess-decode. +#[allow(clippy::too_many_arguments)] +fn decode_rect4_16( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + at16: (usize, usize), + horz: bool, + y: &mut PlaneBuf, + u: &mut PlaneBuf, + v: &mut PlaneBuf, + enable_filter_intra: bool, + allow_screen_content_tools: bool, + base_q_idx: u8, + tx_select: bool, + reduced_tx_set: bool, +) -> Result<()> { + let _ = base_q_idx; + let (r, c) = at16; + let (mi_row0, mi_col0) = (r * (SUB / MI), c * (SUB / MI)); + let (bw, bh) = if horz { (16, 4) } else { (4, 16) }; + let (mi_w, mi_h) = (bw / MI, bh / MI); + let scan: &[u16] = if horz { &SCAN_16X4 } else { &SCAN_4X16 }; + let chroma_scan: &[u16] = if horz { &SCAN_8X4 } else { &SCAN_4X8 }; + let mut prev: Option<((usize, usize), usize)> = None; + let mut last_mode = DC_PRED; + let mut last_uv_mode = DC_PRED; + for i in 0..4usize { + let lmi = if horz { + (mi_row0 + i, mi_col0) + } else { + (mi_row0, mi_col0 + i) + }; + let has_chroma = i % 2 == 1; + // `decode_block_rect4`'s own neighbour rule: the coarse 16-px cell + // aliases all four strips onto one entry, so the mi-exact map wins and + // the previous strip stands in on the split axis. + let mut above_mode = neighbours.above_mode[c]; + let mut left_mode = neighbours.left_mode[r]; + if let Some(((pr, pc), pmode)) = prev { + if pc == lmi.1 && lmi.0 == pr + mi_h { + above_mode = pmode; + } else if pr == lmi.0 && lmi.1 == pc + mi_w { + left_mode = pmode; + } + } + let (above_mode, left_mode) = + neighbours.modes_above_left_mi(lmi.0, lmi.1, (above_mode, left_mode)); + let ( + skip, + mode, + angle_delta_y, + uv_mode, + angle_delta_uv, + alpha, + filter_intra, + palette_y, + palette_uv, + ) = read_intra_mode_rect( + dec, + cdfs, + above_mode, + left_mode, + true, + has_chroma, + bw, + bh, + enable_filter_intra, + neighbours.skip_txfm_ctx(lmi.0, lmi.1), + allow_screen_content_tools, + None, + &[], + lmi.0, + lmi.1, + )?; + if palette_y.is_some() || palette_uv.is_some() { + return Err(unsupported( + "a 1:4 rect strip that actually uses a palette (reconstruction is not ported at this shape)", + )); + } + let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); + if smooth_neighbor { + SMOOTH_LUMA_HITS.with(|h| h.set(h.get() + 1)); + } + if filter_intra.is_some() { + FILTER_INTRA_RECT_HITS.with(|h| h.set(h.get() + 1)); + } + // `bsize_to_tx_size_cat(BLOCK_16X4)`: `max_txsize_rect_lookup` is + // TX_16X4 and `sub_tx_size_map` reaches TX_4X4 in two steps, so the + // depth symbol is the 3-symbol `tx_size_cat1` -- not the 2-symbol + // `cat0` a 4x8 leaf reads nor the `cat2` a 32x8 strip does. + let depth = if tx_select { + let ctx = tx_size_context_rect(neighbours, lmi, bw, bh); + dec.symbol(&mut cdfs.tx_size_cat1[ctx]) + } else { + 0 + }; + if depth != 0 { + TX_DEPTH_HITS.with(|h| h.set(h.get() + 1)); + return Err(unsupported( + "a 16x4/4x16 strip whose transform is split (sub_tx_size_map[TX_16X4] == TX_8X4; the per-unit walk is not ported at this shape)", + )); + } + let (px, py) = (lmi.1 * MI, lmi.0 * MI); + let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); + let levels = if skip { + let zeros = vec![0i32; bw * bh]; + y.reconstruct_rect( + px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, + smooth_neighbor, + ); + zeros + } else { + // The strip's luma TU *is* the strip (`plane_bsize == + // txsize_to_bsize[tx_size]`), so `get_txb_ctx` fixes + // `txb_skip_ctx` at 0. + let around = neighbours.around_mi_rect(lmi, bw, bh)[0]; + let set = if reduced_tx_set { + TxbSet::LumaRect16x4 + } else { + TxbSet::LumaRect16x4Set1 + }; + let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); + let (levels, tx_type) = read_coeffs_rect( + dec, + &mut coding, + scan, + bw, + bh, + 0, + dc_sign_ctx(around.2), + TxType::DctDct, + )?; + let residual = dequant_and_inverse_typed_wh( + &levels, + bw, + bh, + crate::decode::bit_depth(), + block_q_idx(), + plane_q_delta(0).0, + plane_q_delta(0).1, + tx_type, + ); + y.reconstruct_rect( + px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, + smooth_neighbor, + ); + RECT4_16_COEFF_HITS.with(|h| h.set(h.get() + 1)); + levels + }; + neighbours.record_mode_mi(lmi.0, lmi.1, mi_w, mi_h, mode); + neighbours.record_mi_luma_rect(lmi, bw, bh, &levels); + neighbours.fill_skip_grid_rect(lmi, mi_w, mi_h, skip); + neighbours.fill_lf_grid_rect(lmi, mi_w, mi_h, bw as u8, bh as u8, 0); + // `update_partition_context` writes `partition_context_lookup[ + // BLOCK_16X4]` over the whole 16x16: full width above, a 4-px side on + // the left (mirrored under VERT_4). + for cell in 0..mi_w { + if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) { + *slot = bw; + } + } + for cell in 0..mi_h { + if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) { + *slot = bh; + } + } + if has_chroma { + RECT4_16_CHROMA_HITS.with(|h| h.set(h.get() + 1)); + let pair_mi = if horz { + (lmi.0 - 1, lmi.1) + } else { + (lmi.0, lmi.1 - 1) + }; + 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 uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; + let pair_reach = Reach::of_rect(pw, ph, ppx, ppy, y.width, y.height); + let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c); + let (u_levels, v_levels) = if skip { + let zeros = vec![0i32; cw * ch]; + for (buf, _plane) in [(&mut *u, 1usize), (&mut *v, 2)] { + buf.reconstruct_rect( + cpx, + cpy, + cw, + ch, + uv_predict_mode, + angle_delta_uv, + pair_reach, + &zeros, + None, + None, + smooth_neighbor_uv, + ); + } + (zeros.clone(), zeros) + } else { + let around = neighbours.around_mi_rect(pair_mi, pw, ph); + let mut planes = Vec::with_capacity(2); + for plane in 1..3 { + let default_tx = default_intra_tx_type(uv_predict_mode as u8); + let skip_ctx = usize::from(around[plane].0) + usize::from(around[plane].1); + let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); + let (levels, tx_type) = read_coeffs_rect( + dec, + &mut coding, + chroma_scan, + cw, + ch, + skip_ctx, + dc_sign_ctx(around[plane].2), + default_tx, + )?; + planes.push((levels, tx_type)); + } + let (v_levels, v_tx_type) = planes.pop().expect("plane 2"); + let (u_levels, u_tx_type) = planes.pop().expect("plane 1"); + // CfL averages the luma of the WHOLE pair, the region this one + // chroma transform covers. + let ac = alpha.map(|_| cfl_ac_q3_rect(y, ppx, ppy, pw, ph)); + for (plane, buf, levels, tx_type) in [ + (1usize, &mut *u, &u_levels, u_tx_type), + (2, &mut *v, &v_levels, v_tx_type), + ] { + let residual = dequant_and_inverse_typed_wh( + levels, + cw, + ch, + crate::decode::bit_depth(), + block_q_idx(), + plane_q_delta(plane).0, + plane_q_delta(plane).1, + tx_type, + ); + buf.reconstruct_rect( + cpx, + cpy, + cw, + ch, + uv_predict_mode, + angle_delta_uv, + pair_reach, + &residual, + alpha + .zip(ac.as_deref()) + .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), + None, + smooth_neighbor_uv, + ); + } + (u_levels, v_levels) + }; + neighbours.record_uv_mode_mi(pair_mi.0, pair_mi.1, pw / MI, ph / MI, uv_predict_mode); + // The chroma planes' coefficient context, over the PAIR's mi span + // (`decode_leaf_rect8`'s own rule: a subsampled plane's bound is + // the luma one rounded up to its 4x4 unit). + let u_state = neighbour_state(&u_levels); + let v_state = neighbour_state(&v_levels); + let round_up_even = |n: usize| n.div_ceil(2) * 2; + let bound_h = round_up_even(neighbours.mi_rows); + let bound_w = round_up_even(neighbours.mi_cols); + for cell in 0..(ph / MI) { + if pair_mi.0 + cell < bound_h + && let Some(slot) = neighbours.left.get_mut(pair_mi.0 + cell) + { + slot[1] = u_state; + slot[2] = v_state; + } + } + for cell in 0..(pw / MI) { + if pair_mi.1 + cell < bound_w + && let Some(slot) = neighbours.above.get_mut(pair_mi.1 + cell) + { + slot[1] = u_state; + slot[2] = v_state; + } + } + last_uv_mode = uv_predict_mode; + } + if horz { + RECT4_16_HORZ_HITS.with(|h| h.set(h.get() + 1)); + } else { + RECT4_16_VERT_HITS.with(|h| h.set(h.get() + 1)); + } + last_mode = mode; + prev = Some((lmi, mode)); + } + // The coarse 16-px cell holds one entry for the whole 16x16: the last + // strip is what a block below (right) sees through it, the mi-exact maps + // above answering everything finer. + neighbours.above_mode[c] = last_mode; + neighbours.left_mode[r] = last_mode; + neighbours.above_uv_mode[c] = last_uv_mode; + neighbours.left_uv_mode[r] = last_uv_mode; + neighbours.above_side[c] = bw; + neighbours.left_side[r] = bh; + Ok(()) +} + /// Decodes one true `bw`x`bh` superblock-level `PARTITION_HORZ`/ /// `PARTITION_VERT` strip (lane-sbpart r2, `bw, bh` one of `(64, 32)` / /// `(32, 64)` -- [`decode_block_rect`]'s own sibling one level up). Unlike @@ -6794,6 +7166,7 @@ fn decode_block_rect64( // the real 13-symbol `uv_mode_no_cfl` one and desynced the tile // from this block's very first symbol (lane-sbpart r3 bisect). false, + true, bw, bh, enable_filter_intra, @@ -11705,9 +12078,29 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( neighbours.left_mode[sr] = mode_right; continue; } + if part16 == PARTITION_HORZ_4 + || part16 == PARTITION_VERT_4 + { + decode_rect4_16( + &mut dec, + &mut cdfs, + &mut neighbours, + at16, + part16 == PARTITION_HORZ_4, + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + reduced_tx_set, + )?; + continue; + } if part16 != PARTITION_SPLIT { return Err(unsupported( - "a 1:4 partition below 16x16 (PARTITION_HORZ_4/VERT_4, four 16x4/4x16 strips -- this decoder codes NONE, HORZ, VERT, the four AB arms and a clean split at 16x16)", + "a 16x16-level partition value outside PARTITION_NONE..PARTITION_VERT_4", )); } // A real (non-straddle) SPLIT of a @@ -19852,8 +20245,6 @@ mod tests { // string that blocks the shape otherwise. Either way the row is // pinned HERE, so the round that lifts one of those refusals cannot // land a wrong CDF row for the shape it unlocks. - const PART16: &str = "a HORZ_A/HORZ_B/VERT_A partition below 16x16 (this decoder codes \ - only the square arms, HORZ, VERT, VERT_B, and a clean split below 16x16)"; let libaom: &[(usize, usize, u16, Option<&str>)] = &[ (4, 4, 4621, None), (4, 8, 6743, None), @@ -19868,8 +20259,10 @@ mod tests { // The 1:4 strips: a 16x16-level PARTITION_HORZ_4/VERT_4 gives // 4x16/16x4 (still refused by name before any leaf is coded), // a 32x32-level one 8x32/32x8. - (4, 16, 12770, Some(PART16)), - (16, 4, 10368, Some(PART16)), + // lane-r14: the 16-level pair is coded now (`decode_rect4_16`), + // owning classes 13/12. + (4, 16, 12770, None), + (16, 4, 10368, None), // lane-fi32x8 r1: the 32-level pair is coded (four 32x8/8x32 // strips per `PARTITION_HORZ_4`/`VERT_4`) and now owns classes // 10/11; only the 16-level pair is still refused before any leaf. diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 958dd46d..e88a3edf 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -34,7 +34,8 @@ const REFUSALS: &[&str] = &[ "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", "a split intra strip whose transform unit is {tx_w}x{tx_h} (no luma coefficient tables for that shape here)", "an inter partition below 8x8 (this decoder codes no inter leaf smaller than 8x8; lane-sub8 scoped to intra)", - "a 1:4 partition below 16x16 (PARTITION_HORZ_4/VERT_4, four 16x4/4x16 strips -- this decoder codes NONE, HORZ, VERT, the four AB arms and a clean split at 16x16)", + "a 16x16-level partition value outside PARTITION_NONE..PARTITION_VERT_4", + "a 16x4/4x16 strip whose transform is split (sub_tx_size_map[TX_16X4] == TX_8X4; the per-unit walk is not ported at this shape)", "a 1:4 rect strip that actually uses a palette (reconstruction is not ported at this shape)", "a 16x16 block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 070b728a..e86ad583 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -18180,6 +18180,228 @@ mod tests { /// then compared pixel-exact are counted (class /// gate-skips-on-its-own-failure); the 10-bit arm is what retires /// `enable-1to4-partitions` from `NEVER_EXERCISED_10BIT`. + /// lane-r14: the 16x16-level 1:4 pair -- four 16x4 (`PARTITION_HORZ_4`) or + /// 4x16 (`PARTITION_VERT_4`) strips, the shape the wall + /// "a 1:4 partition below 16x16" refused. Fixture: 4-PIXEL bands (the + /// 8-pixel ones of the 32-level gate above, one size down -- a 16x4 strip + /// is flat where a 16x8 one is not), transposed per attempt so a + /// transposed scan or nz-map table cannot pass (class + /// scan-weights-cross-axis), plus an explicit-seed noise layer that keeps + /// aomenc out of screen-content mode. `--enable-filter-intra=1` and + /// `--enable-cfl-intra=1` are ON: this shape reads `use_filter_intra` off + /// its own CDF rows (classes 12/13) and its CfL averages the whole 16x8 + /// (8x16) luma PAIR, not the strip. `--enable-tx-size-search=0`: a split + /// transform at this shape is still refused by name. + /// + /// Counters: 16x4 strips, 4x16 strips, chroma-reference strips (the odd + /// strip of each 4:2:0 pair, the only one that codes chroma) and strips + /// with a coded residual must all be nonzero over the compared attempts, + /// and only attempts that DECODED and were compared pixel-exact count + /// (class gate-skips-on-its-own-failure). + #[test] + fn a_real_aomenc_stream_with_a_16_level_1to4_partition_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_stream_with_a_16_level_1to4_partition_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height, frame_count) = (192usize, 128usize, 1usize); + let n_attempts: u32 = std::env::var("EC_R14_GATE_ATTEMPTS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(8); + for bit_depth in [8u32, 10u32] { + let mut named_refusals = 0u32; + let mut matched = 0u32; + let (mut horz_proved, mut vert_proved) = (0usize, 0usize); + let (mut chroma_proved, mut coeff_proved) = (0usize, 0usize); + let (mut out_of_scope, mut out_of_scope_mismatch) = (0u32, 0u32); + for attempt in 0..n_attempts { + let seed = 42 + attempt; + let duration = frame_count as f64 / 25.0; + let axis = if attempt % 2 == 0 { "Y" } else { "X" }; + let step = 67 + (attempt / 2) * 12; + let noise = 6 + (attempt % 4) * 3; + let source = format!( + "color=c=gray:s={width}x{height}:d={duration}:r=25,format=gray,\ + geq=lum='mod(floor({axis}/4)*{step},256)',\ + noise=alls={noise}:all_seed={seed},format=yuv420p" + ); + let cq_level = format!("--cq-level={}", 24 + (attempt % 4) * 8); + let pix_fmt = if bit_depth == 10 { "yuv420p10le" } else { "yuv420p" }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", &source, "-t", &duration.to_string(), + "-pix_fmt", pix_fmt, "-strict", "-1", "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "{NAME}: ffmpeg refused the {bit_depth}-bit fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let depth_arg = format!("--bit-depth={bit_depth}"); + let input_depth_arg = format!("--input-bit-depth={bit_depth}"); + let args: Vec<&str> = vec![ + "--codec=av1", + "--passes=1", + "--end-usage=q", + &cq_level, + "--cpu-used=0", + "--threads=1", + "--row-mt=0", + "--sb-size=64", + &depth_arg, + &input_depth_arg, + "--enable-rect-partitions=1", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=1", + "--min-partition-size=4", + "--max-partition-size=16", + "--enable-restoration=0", + "--enable-palette=0", + "--deltaq-mode=0", + "--enable-filter-intra=1", + "--enable-cfl-intra=1", + "--enable-intrabc=0", + "--enable-tx-size-search=0", + "--obu", + "-o", + "-", + "-", + ]; + let mut child = Command::new(aomenc_path()) + .args(&args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let before = crate::decode::rect4_16_counters(); + let frames = match decode_stream(&stream) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal ({bit_depth}-bit, seed \ + {seed}): {msg}" + ); + named_refusals += 1; + eprintln!("{bit_depth}-bit seed {seed} refusal: {msg}"); + continue; + } + Ok(frames) => frames, + }; + let now = crate::decode::rect4_16_counters(); + let (horz, vert, chroma, coeff) = ( + now.0 - before.0, + now.1 - before.1, + now.2 - before.2, + now.3 - before.3, + ); + let ffmpeg_frames = if bit_depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) + } else { + ffmpeg_decode_sequence(&stream, width, height, frame_count) + }; + assert_eq!(frames.len(), frame_count); + let mismatched = frames + .iter() + .zip(&ffmpeg_frames) + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); + if horz + vert == 0 { + out_of_scope += 1; + if mismatched { + out_of_scope_mismatch += 1; + eprintln!( + "{NAME}: {bit_depth}-bit seed {seed} MISMATCHES with zero 16-level \ + 1:4 strips -- a pre-existing defect of another shape" + ); + } + continue; + } + if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { + std::fs::write(&path, &stream).expect("writing pinned stream"); + eprintln!( + "EC_AV1_GATE_DUMP: wrote mismatching stream ({bit_depth}-bit, seed \ + {seed}) to {path}" + ); + } + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!( + got.y, want.y, + "{NAME} frame {i} luma vs ffmpeg ({bit_depth}-bit, seed {seed})" + ); + assert_eq!( + got.u, want.u, + "{NAME} frame {i} U vs ffmpeg ({bit_depth}-bit, seed {seed})" + ); + assert_eq!( + got.v, want.v, + "{NAME} frame {i} V vs ffmpeg ({bit_depth}-bit, seed {seed})" + ); + } + horz_proved += horz; + vert_proved += vert; + chroma_proved += chroma; + coeff_proved += coeff; + matched += 1; + } + assert!( + matched > 0, + "{NAME}: every {bit_depth}-bit attempt refused or fell out of scope ({named_refusals} \ + refusals, {out_of_scope} out of scope); the gate proved nothing" + ); + assert_eq!( + out_of_scope_mismatch, 0, + "{NAME}: {out_of_scope_mismatch} {bit_depth}-bit attempt(s) mismatch with no \ + 16-level 1:4 strip at all" + ); + assert!( + horz_proved > 0 && vert_proved > 0, + "{NAME}: {bit_depth}-bit compared attempts decoded {horz_proved} 16x4 and \ + {vert_proved} 4x16 strips -- both orientations must fire or a transposed table \ + passes" + ); + assert!( + chroma_proved > 0, + "{NAME}: {bit_depth}-bit compared attempts closed {chroma_proved} chroma pairs" + ); + assert!( + coeff_proved > 0, + "{NAME}: {bit_depth}-bit compared attempts coded no residual on any strip" + ); + eprintln!( + "{NAME}: {bit_depth}-bit {matched} compared attempts, {horz_proved} 16x4 / \ + {vert_proved} 4x16 strips, {chroma_proved} chroma pairs, {coeff_proved} coded \ + strips, {named_refusals} refusals, {out_of_scope} out of scope" + ); + } + } + #[test] // lane-tx64x16 r4: the fixture r3 could not find. `gradients` never wins // a 32-level 1:4 shape; 8-pixel BANDS do -- each 32x8 strip is flat, so