From 60b3ba2487ba23117f55c73fa4c6338409a99c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 09:37:32 +0300 Subject: [PATCH] feat(av1): sub-8x8 inter PARTITION_VERT (two BLOCK_4X8 leaves); HORZ twin implemented but refused decode_inter_sub8_rect2 codes both rectangular sub-8x8 inter shapes: per-sub-block mode info in decodemv.c order (single ref only -- is_comp_ref_allowed(BLOCK_8X4) false, so no skip_mode and no comp_mode; no interintra/motion_mode/OBMC/warp below min dim 8), one txfm_partition symbol at max TX_8X4/TX_4X8 through r14's general read_var_tx_size, chroma coded once on the chroma-reference (second) sub-block as one TX_4X4 per plane with sub8x8 prediction from BOTH sub-blocks' mvs (reconinter.c build_inter_predictors_sub8x8), and per-mi neighbour/mv-grid writes over the true w_mi x h_mi span. New luma coefficient sets TxbSet::LumaRect8x4Inter{,Set1}: LumaRect4x8's tables (8x8 coefficient class, 32-position eob_pt) with the inter TX_4X4 tx_type row (EXT_TX_SET_ALL16). These also close the panic risk lane-inter16ab r3 noted, where a TX_16X4 -> TX_8X4 var-tx split reached rect_inter_luma_set's unreachable!(). Gate: a_real_aomenc_inter_sequence_with_a_sub8x8_inter_split_decodes_pixel_exact now runs 16 attempts per bit depth -- 0..7 with --enable-rect-partitions=0 (SPLIT, unchanged) and 8..15 with =1 (VERT) -- and asserts a new rect_fired >= 2 alongside fired >= 2 and out_of_scope_mismatch == 0, comparing every decode-order frame Y/U/V at 8 and 10 bit. The HORZ (two BLOCK_8X4) arm is refused by name rather than shipped: a 28-arm sweep on a transposed-structure source mismatches on 4 arms at cq 12/14/16/32, outside the known CDEF band. Range ladder on one of them localizes the divergence to the mode info of the 16x8 inter block immediately to the group's right -- matching through its EC_MODE entry, differing by its first coefficient TU -- i.e. a per-row neighbour band the group leaves behind. Details in lanes/intersub8-r3.handoff.md. --- crates/ec-av1/examples/decode_probe.rs | 2 + crates/ec-av1/src/cdf_state.rs | 40 ++ crates/ec-av1/src/decode.rs | 590 ++++++++++++++++++++++++- crates/ec-av1/src/refusal_inventory.rs | 3 +- crates/ec-av1/src/stream.rs | 41 +- lanes/intersub8-r3.handoff.md | 79 ++++ 6 files changed, 743 insertions(+), 12 deletions(-) create mode 100644 lanes/intersub8-r3.handoff.md diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index 737c8aa9..37be4130 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -36,6 +36,8 @@ fn main() { let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); println!("sub8_inter_split: groups={}", ec_av1::decode::sub8_inter_split_hits()); + let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits(); + println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}"); let leaf = ec_av1::stream::vartx_rect_leaf_hits(); println!("vartx_rect_leaf: 32x16={} 16x32={}", leaf[0], leaf[1]); let ir = ec_av1::stream::inter_rect_counters(); diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 89334535..19c9b910 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -230,6 +230,22 @@ pub(crate) enum TxbSet { /// counterpart: `EXT_TX_SET_DTT4_IDTX_1DDCT`, the seven-symbol `TX_4X4` /// row [`LumaRect4x8Set1`](TxbSet::LumaRect4x8Set1) reads. LumaRect16x4Set1, + /// The luma 8x4/4x8 transform of an INTER `PARTITION_HORZ`/`PARTITION_VERT` + /// leaf under an 8x8 block (lane-intersub8 r3): every coefficient table is + /// [`LumaRect4x8`](TxbSet::LumaRect4x8)'s (`get_txsize_entropy_ctx` squares + /// `TX_8X4` up to the 8x8 class, 32-position `eob_pt`), with the INTER + /// `tx_type` alphabet in its place. Under `reduced_tx_set` + /// `av1_get_ext_tx_set_type` (`tx_size_sqr_up == TX_8X8`, `is_inter`) + /// returns `EXT_TX_SET_DCT_IDTX`, whose row is + /// `default_inter_ext_tx_cdf[?][txsize_sqr_map[TX_8X4] == TX_4X4]` -- the + /// same `TX_4X4` inter row [`Luma4Inter`](TxbSet::Luma4Inter) reads. + LumaRect8x4Inter, + /// [`LumaRect8x4Inter`](TxbSet::LumaRect8x4Inter)'s `reduced_tx_set: false` + /// counterpart: `tx_size_sqr == TX_4X4` with `tx_size_sqr_up == TX_8X8`, so + /// `av1_ext_tx_set_lookup[is_inter=1][...]` is `EXT_TX_SET_ALL16` -- sixteen + /// symbols at the `TX_4X4` inter row, the table + /// [`Luma4InterSet1`](TxbSet::Luma4InterSet1) also reads. + LumaRect8x4InterSet1, } /// The tables one transform block is coded with, borrowed from the state for @@ -1792,6 +1808,30 @@ impl Cdfs { tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()), eob_pt_class1: Some(&mut self.eob_pt_32_luma_class1), }, + TxbSet::LumaRect8x4Inter => TxbTables { + side: 8, + txb_skip: &mut self.txb_skip_luma_8, + eob_pt: &mut self.eob_pt_32_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.inter_tx_type_4.as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_32_luma_class1), + }, + TxbSet::LumaRect8x4InterSet1 => TxbTables { + side: 8, + txb_skip: &mut self.txb_skip_luma_8, + eob_pt: &mut self.eob_pt_32_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.inter_tx_type_4_set1.as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_32_luma_class1), + }, TxbSet::LumaRect4x8Set1 => TxbTables { side: 8, txb_skip: &mut self.txb_skip_luma_8, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 33edaa35..a40e710e 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2541,6 +2541,18 @@ thread_local! { /// How many 8x8 groups an INTER frame split into four BLOCK_4X4 inter /// sub-blocks (lane-intersub8). static SUB8_INTER_SPLIT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// How many 8x8 groups an INTER frame split into two rectangular sub-8x8 + /// inter leaves (lane-intersub8 r3), indexed `[0] = HORZ` (two BLOCK_8X4), + /// `[1] = VERT` (two BLOCK_4X8). + static SUB8_INTER_RECT_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; +} + +/// Current value of [`SUB8_INTER_RECT_HITS`]: `(8x4 groups, 4x8 groups)`. +pub fn sub8_inter_rect_hits() -> (usize, usize) { + SUB8_INTER_RECT_HITS.with(|c| { + let h = c.get(); + (h[0], h[1]) + }) } /// Current value of [`SUB8_SPLIT_HITS`]. @@ -19478,6 +19490,523 @@ fn decode_inter_sub8_split4( Ok(()) } +/// Decodes a `PARTITION_HORZ`/`PARTITION_VERT` of one 8x8 INTER block into two +/// `BLOCK_8X4`/`BLOCK_4X8` inter leaves (lane-intersub8 r3) -- the inter twin of +/// [`decode_leaf_rect8`] and the rectangular sibling of +/// [`decode_inter_sub8_split4`], whose element order it follows exactly. +/// Per sub-block, in libaom `decodemv.c` `read_inter_frame_mode_info` order: +/// +/// * `segment_id`, `skip`, `cdef`/`delta_q`/`delta_lf`, `intra_inter`; +/// * NO `skip_mode` symbol: `is_comp_ref_allowed(BLOCK_8X4)` is false +/// (`av1_is_comp_ref_allowed` needs both sides >= 8), so +/// `read_skip_mode` returns 0 without reading and `comp_mode` is never read +/// either (`read_ref_frames`' `is_comp_ref_allowed` guard) -- single +/// reference only; +/// * NO `interintra` and NO `motion_mode`/OBMC/warp symbol: every one of those +/// gates needs `block_size_wide >= 8 && block_size_high >= 8` +/// (`is_interintra_allowed_bsize`, `motion_mode_allowed`), and one side is 4; +/// * `read_mb_interp_filter` DOES run for both sub-blocks. +/// +/// The shape's own differences from the four-way split: +/// +/// * `block_signals_txsize(BLOCK_8X4)` is true (`bsize > BLOCK_4X4`, +/// blockd.h:1027), so a non-skipped sub-block reads ONE `txfm_partition` +/// symbol under `TX_MODE_SELECT` at `max_txsize_rect_lookup[BLOCK_8X4] == +/// TX_8X4` ([`read_var_tx_size`]); split resolves to two `TX_4X4` units +/// (`sub_tx_size_map[TX_8X4] == TX_4X4`, libaom's early return), no split to +/// the whole-leaf `TX_8X4`; +/// * the luma coefficient set is [`TxbSet::LumaRect8x4Inter`] (the 8x8 +/// coefficient class with a 32-position `eob_pt` and the INTER `TX_4X4` +/// `tx_type` row) for the whole-leaf unit, and the square +/// [`TxbSet::Luma4Inter`] for a split `TX_4X4` unit; +/// * chroma is coded ONCE, on the sub-block that is the chroma reference: +/// `is_chroma_reference` (`av1_common_int.h`) is +/// `((mi_row & 1) || !(bh4 & 1) || !ss_y) && ((mi_col & 1) || !(bw4 & 1) || +/// !ss_x)`, i.e. under 4:2:0 the BOTTOM 8x4 (`bh4 == 1`, odd) and the RIGHT +/// 4x8 (`bw4 == 1`) -- always the second sub-block here; +/// * that chroma unit's prediction is the sub8x8 one (`reconinter.c` +/// `build_inter_predictors_sub8x8`: `row_start = -(block_size_high == 4 && +/// ss_y)`, `col_start = -(block_size_wide == 4 && ss_x)`), two 4x2 (HORZ) or +/// 2x4 (VERT) chroma pieces, each from its OWN sub-block's reference, mv and +/// interpolation filters. +#[allow(clippy::too_many_arguments)] +fn decode_inter_sub8_rect2( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + grid: &mut MiGrid, + group_mi: (usize, usize), + vert: bool, + mi_cols: u32, + mi_rows: u32, + y: &mut PlaneBuf, + u: &mut PlaneBuf, + v: &mut PlaneBuf, + ref_y: &PlaneBuf, + ref_u: &PlaneBuf, + ref_v: &PlaneBuf, + other_refs: &RefSlots, + sign_bias_table: &SignBiasTable, + global_motion: &[ec_av1_syntax::WarpParams; 7], + base_q_idx: u8, + scan4: &[u16], + allow_high_precision_mv: bool, + force_integer_mv: bool, + interp_fixed: Option, + enable_dual_filter: bool, + tpl_frame: Option<&TplFrameArgs>, + reduced_tx_set: bool, + frame_width: usize, +) -> Result<()> { + const LAST_FRAME: i8 = 1; + // The 8x8 group's own square stride: both leaves' prediction buffers are + // laid out at it, as every other rect inter unit here is. + const SIDE: usize = 8; + let (bw, bh) = if vert { (4usize, 8usize) } else { (8usize, 4usize) }; + let (w_mi, h_mi) = (bw / MI, bh / MI); + SUB8_INTER_RECT_HITS.with(|c| { + let mut h = c.get(); + h[usize::from(vert)] += 1; + c.set(h); + }); + let (gr, gc) = group_mi; + let (gpx, gpy) = (gc * MI, gr * MI); + let (cpx, cpy) = (gpx / 2, gpy / 2); + // 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]; + let mut first_tx_type = TxType::DctDct; + let mut last_skip = true; + for i in 0..2usize { + let lmi = if vert { (gr, gc + i) } else { (gr + i, gc) }; + if lmi.0 >= mi_rows as usize || lmi.1 >= mi_cols as usize { + continue; + } + let (rmi, cmi) = lmi; + let (px, py) = (cmi * MI, rmi * MI); + inter_segment_id(dec, cdfs, rmi, cmi, w_mi, h_mi, false, true); + let skip_ctx = + usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); + let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; + inter_segment_id(dec, cdfs, rmi, cmi, w_mi, h_mi, skip, false); + maybe_read_cdef_idx(dec, rmi, cmi, skip); + maybe_read_delta_q(dec, cdfs, rmi, cmi, false, skip); + maybe_read_delta_lf(dec, cdfs, rmi, cmi, false, skip); + let (has_above, has_left) = ( + rmi > neighbours.tile_row0_mi, + cmi > neighbours.tile_col0_mi, + ); + let ii_ctx = intra_inter_ctx( + has_above, + has_left, + neighbours.above_inter[cmi], + neighbours.left_inter[rmi], + ); + let is_inter = dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; + if !is_inter { + return Err(unsupported( + "an intra 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition \ + (this decoder codes only inter sub-blocks there)", + )); + } + if std::env::var_os("EC_TRACE_MODE").is_some() { + eprintln!( + "EC_MODE mi_row={rmi} mi_col={cmi} rng={}", + dec.debug_state().0 + ); + } + let ref_frame = read_single_ref( + dec, + cdfs, + neighbours.above_ref[cmi], + neighbours.above_ref1[cmi], + neighbours.left_ref[rmi], + neighbours.left_ref1[rmi], + ); + let (sref_y, _, _) = ref_planes(ref_frame, ref_y, ref_u, ref_v, other_refs)?; + if mc::scale_factor(sref_y.width, frame_width) != mc::REF_NO_SCALE { + return Err(unsupported( + "a sub-8x8 inter block under a scaled reference (superres, unimplemented)", + )); + } + let gm_table = build_gm_mv_table( + global_motion, + rmi, + cmi, + w_mi, + h_mi, + allow_high_precision_mv, + force_integer_mv, + ); + let tpl = tpl_frame.map(|t| crate::mvstack::TplArgs { + field: t.field, + cur_offset_0: crate::motion_field::get_relative_dist( + t.order_hint_bits, + t.order_hint, + t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], + ), + allow_high_precision_mv, + }); + let stack = crate::mvstack::find_mv_stack_with_sign_bias( + grid, + rmi, + cmi, + w_mi, + h_mi, + ref_frame, + mi_cols as usize, + mi_rows as usize, + sign_bias_table, + &gm_table, + tpl, + ); + let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; + let mut is_globalmv = false; + let (mv, is_new_mv) = if !not_new { + let mut idx = 0usize; + while idx < 2 && stack.entries.len() > idx + 1 { + if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { + break; + } + idx += 1; + } + let base_mv = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); + ( + read_mv( + dec, + &mut cdfs.mv_comp, + &mut cdfs.mv_joint, + base_mv, + allow_high_precision_mv, + force_integer_mv, + ), + true, + ) + } else { + let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; + is_globalmv = !not_zero; + let mv = if !not_zero { + gm_table[(ref_frame - LAST_FRAME) as usize] + } else { + let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; + if nearest { + stack.nearest_mv + } else { + let mut idx = 1usize; + while idx < 3 && stack.entries.len() > idx + 1 { + if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { + break; + } + idx += 1; + } + stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) + } + }; + (mv, false) + }; + if std::env::var_os("EC_TRACE_MODE").is_some() { + eprintln!( + "EC_MODE_MV mi_row={rmi} mi_col={cmi} mv0=({},{}) rng={}", + mv.0, + mv.1, + dec.debug_state().0 + ); + } + let gm_ref = &global_motion[(ref_frame - LAST_FRAME) as usize]; + let above_filter_ctx = if neighbours.above_ref[cmi] == ref_frame + || neighbours.above_ref1[cmi] == Some(ref_frame) + { + neighbours.above_filter[cmi] + } else { + [3, 3] + }; + let left_filter_ctx = if neighbours.left_ref[rmi] == ref_frame + || neighbours.left_ref1[rmi] == Some(ref_frame) + { + neighbours.left_filter[rmi] + } else { + [3, 3] + }; + let gm_nontrans = is_globalmv && gm_ref.model != ec_av1_syntax::WarpModel::Translation; + let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( + dec, + cdfs, + interp_fixed, + enable_dual_filter, + gm_nontrans, + above_filter_ctx, + left_filter_ctx, + false, + ); + // `read_block_tx_size` (libaom `decodeframe.c`): unlike a `BLOCK_4X4`, + // this shape signals its transform size. A non-skipped inter block + // walks the var-tx tree from `max_txsize_rect_lookup[BLOCK_8X4]`; a + // skipped one codes nothing and records its BLOCK size in both txfm + // contexts (`set_txfm_ctxs`' `skip_inter` arm). + let mut leaves: Vec<(usize, usize, usize, usize)> = Vec::new(); + if TX_SELECT_INTER.with(std::cell::Cell::get) && !skip { + let max_w_mi = w_mi.min((mi_cols as usize).saturating_sub(cmi)); + let max_h_mi = h_mi.min((mi_rows as usize).saturating_sub(rmi)); + read_var_tx_size( + dec, + cdfs, + neighbours, + lmi, + SIDE, + (max_w_mi, max_h_mi), + (bw, bh), + 0, + 0, + 0, + &mut leaves, + ); + } else { + leaves.push((0, 0, bw, bh)); + set_txfm_ctxs(neighbours, lmi, bw.max(bh), w_mi, h_mi, skip); + } + if std::env::var_os("EC_TRACE_MODE").is_some() { + eprintln!( + "EC_MODE_VALR mi_row={rmi} mi_col={cmi} newmv={is_new_mv} globalmv={is_globalmv} \ + ref0={ref_frame} mv0=({},{}) stack={} leaves={} rng={}", + mv.0, + mv.1, + stack.entries.len(), + leaves.len(), + dec.debug_state().0 + ); + } + grid_stamp_rect(grid, rmi, cmi, w_mi, h_mi, ref_frame, mv, is_new_mv); + piece[i] = Some((ref_frame, mv, h_filter, v_filter)); + // Prediction over the leaf's true footprint, laid out at the group's + // square stride (what `reconstruct_mc_rect` and `read_inter_plane_rect` + // index with). + let mut pred_y = vec![0u16; SIDE * SIDE]; + { + let mut dense = vec![0u16; bw * bh]; + mc::predict_with_filters( + &sref_y.data, + 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), + bw, + bh, + h_filter, + v_filter, + &mut dense, + ); + for row in 0..bh { + pred_y[row * SIDE..][..bw].copy_from_slice(&dense[row * bw..][..bw]); + } + } + if skip { + y.reconstruct_mc_rect(px, py, SIDE, bw, bh, &pred_y, &vec![0i32; SIDE * SIDE]); + neighbours.record_mi_luma_rect(lmi, bw, bh, &vec![0i32; bw * bh]); + neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, bw as u8, bh as u8, ref_frame.max(LAST_FRAME)); + } else { + let (mut last_tw, mut last_th) = (bw, bh); + for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { + let tu_mi = (rmi + row, cmi + col); + let (tu_px, tu_py) = (px + col * MI, py + row * MI); + let (tu_grid, tu_tx_type) = if tw == th { + // A split `TX_4X4` unit: square reader, its own + // `get_txb_ctx_general` neighbour context (the unit is + // smaller than its plane block, so the lone-TU 0 does not + // apply). + let mut tu_pred = Vec::with_capacity(tw * th); + for rr in 0..th { + let start = (row * MI + rr) * SIDE + col * MI; + tu_pred.extend_from_slice(&pred_y[start..start + tw]); + } + let tu_skip_ctx = neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI); + read_inter_plane( + dec, + cdfs, + if reduced_tx_set { + TxbSet::Luma4Inter + } else { + TxbSet::Luma4InterSet1 + }, + scan4, + 0, + neighbours.around_mi(tu_mi, tw)[0], + 0, + y, + tu_px, + tu_py, + tw, + base_q_idx, + &tu_pred, + None, + Some(tu_skip_ctx), + )? + } else { + // The whole leaf as one `TX_8X4`/`TX_4X8` unit: it IS the + // plane block, so `get_txb_ctx_general` fixes `txb_skip_ctx` + // at 0 (`luma_skip_ctx: None`). + let start = row * MI * SIDE + col * MI; + read_inter_plane_rect( + dec, + cdfs, + if reduced_tx_set { + TxbSet::LumaRect8x4Inter + } else { + TxbSet::LumaRect8x4InterSet1 + }, + (tw, th), + SIDE, + 0, + neighbours.around_mi_rect(tu_mi, tw, th)[0], + 0, + y, + tu_px, + tu_py, + &pred_y[start..], + None, + None, + )? + }; + if idx == 0 { + // `av1_get_tx_type`: the group's one chroma unit scales + // back to luma (0, 0), i.e. the FIRST sub-block's first + // transform unit. + first_tx_type = tu_tx_type; + } + neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); + (last_tw, last_th) = (tw, th); + } + neighbours.fill_lf_grid_rect( + lmi, + w_mi, + h_mi, + last_tw as u8, + last_th as u8, + ref_frame.max(LAST_FRAME), + ); + } + neighbours.record_mode_mi(rmi, cmi, w_mi, h_mi, DC_PRED); + neighbours.record_inter_rect_mi(lmi, w_mi, h_mi, skip, true, ref_frame, resolved_filter, false); + neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); + last_skip = skip; + } + // The group's single 4x4 chroma unit per plane, on the chroma-reference + // (second) sub-block's own skip flag. + const B4: usize = 4; + let mut pred_u = vec![0u16; B4 * B4]; + let mut pred_v = vec![0u16; B4 * B4]; + let (piece_w, piece_h) = if vert { (2usize, 4usize) } else { (4usize, 2usize) }; + for i in 0..2usize { + let Some((ref_frame, mv, h_filter, v_filter)) = piece[i] else { + continue; + }; + let (_, sref_u, sref_v) = ref_planes(ref_frame, ref_y, ref_u, ref_v, other_refs)?; + let (ox, oy) = if vert { + (cpx + i * piece_w, cpy) + } else { + (cpx, cpy + i * piece_h) + }; + let (dc, dr) = if vert { (i * piece_w, 0) } else { (0, i * piece_h) }; + for (plane, dst) in [(sref_u, &mut pred_u), (sref_v, &mut pred_v)] { + let mut buf = vec![0u16; piece_w * piece_h]; + mc::predict_with_filters( + &plane.data, + plane.width, + plane.true_width, + plane.true_height, + mv_to_q4(ox, mv.1, false), + mv_to_q4(oy, mv.0, false), + piece_w, + piece_h, + h_filter, + v_filter, + &mut buf, + ); + for row in 0..piece_h { + for col in 0..piece_w { + dst[(dr + row) * B4 + dc + col] = buf[row * piece_w + col]; + } + } + } + } + let (u_grid, v_grid) = if last_skip { + u.reconstruct_mc(cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4]); + v.reconstruct_mc(cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4]); + (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) + } else { + let around = neighbours.around_mi(group_mi, 8); + let ug = read_inter_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], 0, u, cpx, cpy, B4, base_q_idx, + &pred_u, Some(first_tx_type), None, + )? + .0; + let vg = read_inter_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], 0, v, cpx, cpy, B4, base_q_idx, + &pred_v, Some(first_tx_type), None, + )? + .0; + (ug, vg) + }; + neighbours.record_uv_mode_mi(gr, gc, 2, 2, DC_PRED); + // `update_partition_context` writes `partition_context_lookup[subsize]` + // over the whole 8x8 span -- BLOCK_4X8's width above and height left, the + // same values [`decode_leaf_rect8`] writes for the intra shape. + for cell in 0..2usize { + neighbours.above_side_mi[gc + cell] = bw; + neighbours.left_side_mi[gr + cell] = bh; + } + 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); + let u_state = neighbour_state(&u_grid); + let v_state = neighbour_state(&v_grid); + for cell in 0..2usize { + if cell < 2usize.min(bound_h.saturating_sub(gr)) { + neighbours.left[gr + cell][1] = u_state; + neighbours.left[gr + cell][2] = v_state; + } + if cell < 2usize.min(bound_w.saturating_sub(gc)) { + neighbours.above[gc + cell][1] = u_state; + neighbours.above[gc + cell][2] = v_state; + } + } + Ok(()) +} + +/// Stamps one sub-8x8 inter leaf into every mi cell of its true `w_mi`x`h_mi` +/// footprint (class: a rect block's grid span is its own, never a square guess). +fn grid_stamp_rect( + grid: &mut MiGrid, + mi_row: usize, + mi_col: usize, + w_mi: usize, + h_mi: usize, + ref_frame: i8, + mv: (i32, i32), + is_new_mv: bool, +) { + for dr in 0..h_mi { + for dc in 0..w_mi { + grid.set( + mi_row + dr, + mi_col + dc, + MiInfo { + is_inter: true, + ref_frame, + ref_frame1: None, + mv1: None, + mv, + is_new_mv, + size: w_mi, + size_h: h_mi, + is_global_mv0: false, + is_global_mv1: false, + }, + ); + } + } +} + /// Decodes one 8x8 leaf of a straddling 16x16 inter-frame block /// (lane-av1inter8), mirroring [`crate::tile::write_inter_frame_leaf8`]'s /// exact symbol order: its own skip flag, intra/inter choice and (when @@ -21566,6 +22095,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // instead of silently decoding as SPLIT and desyncing. let part64 = match (has_cols, has_rows) { (true, true) => { + if std::env::var_os("EC_TRACE_PART").is_some() { eprintln!("EC_PART mi_row={} mi_col={} bsize=12 ctx={} rng={}", sb_r as usize * SB_MI as usize, sb_c as usize * SB_MI as usize, sb_ctx, dec.debug_state().0); } let p = dec.symbol(&mut cdfs.partition_w64[sb_ctx]); if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!( @@ -21750,6 +22280,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( dec.debug_state().0 ); } + if std::env::var_os("EC_TRACE_PART").is_some() { eprintln!("EC_PART mi_row={} mi_col={} bsize=9 ctx={} rng={}", (r32 as usize), (c32 as usize), ctx32, dec.debug_state().0); } let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!( @@ -21862,6 +22393,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // inter leaves through the very same loop the // straddling case has always used. let part16 = if has_cols16 && has_rows16 { + if std::env::var_os("EC_TRACE_PART").is_some() { eprintln!("EC_PART mi_row={} mi_col={} bsize=6 ctx={} rng={}", (sr as usize*SUB_MI as usize), (sc as usize*SUB_MI as usize), ctx16, dec.debug_state().0); } let p = dec.symbol(&mut cdfs.partition_w16[ctx16]); if std::env::var_os("EC_AV1_TRACE").is_some() { let (rng, _) = dec.debug_state(); @@ -22077,6 +22609,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( for (mr, mc) in leaf_positions { let leaf_mi = (mr as usize, mc as usize); let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); + if std::env::var_os("EC_TRACE_PART").is_some() { eprintln!("EC_PART mi_row={} mi_col={} bsize=3 ctx={} rng={}", leaf_mi.0, leaf_mi.1, leaf_ctx, dec.debug_state().0); } let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); if part8 == PARTITION_SPLIT { // lane-intersub8: four BLOCK_4X4 inter @@ -22110,11 +22643,64 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( )?; continue; } - if part8 != PARTITION_NONE { + if part8 == PARTITION_HORZ { + // lane-intersub8 r3: the two-BLOCK_8X4 + // shape IS implemented below + // ([`decode_inter_sub8_rect2`], same + // function as the 4x8 one), and it + // decodes most streams exact -- but a + // measured 4-of-28-attempt sweep + // (transposed-structure source, cq + // 12..32, both depths) desyncs in the + // MODE INFO of the 16x8 inter block + // immediately to its right: entry + // ranges match at that block's + // `EC_MODE` print and differ by its + // first coefficient TU, so some + // per-row neighbour band this group + // leaves behind is wrong. Refused by + // name rather than shipped as silent + // pixel drift; the 4x8 twin is gated. return Err(unsupported( - "an inter 8x8 HORZ/VERT partition (8x4/4x8 inter leaves; the 4x4 SPLIT arm decodes, but a rectangular sub-8x8 inter transform is unimplemented)", + "an inter 8x8 HORZ partition (two BLOCK_8X4 inter leaves; decoded, but a measured sweep desyncs the block to its right)", )); } + if part8 == PARTITION_VERT { + // lane-intersub8 r3: two BLOCK_8X4 / + // BLOCK_4X8 inter leaves, chroma once + // per 8x8 on the second of them. + decode_inter_sub8_rect2( + &mut dec, + &mut cdfs, + &mut neighbours, + &mut grid, + leaf_mi, + true, + mi_cols, + mi_rows, + &mut y, + &mut u, + &mut v, + &ref_y, + &ref_u, + &ref_v, + &ref_slots, + &sign_bias_table, + &global_motion, + base_q_idx, + &scan4, + allow_high_precision_mv, + force_integer_mv, + interp_fixed, + enable_dual_filter, + tpl_frame.as_ref(), + reduced_tx_set, + frame_width as usize, + )?; + continue; + } + // `partition_w8` is a 4-symbol alphabet + // (NONE/HORZ/VERT/SPLIT), all four handled. let ( skip, is_inter, diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 51ea438a..3edcf0fe 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -33,9 +33,10 @@ const REFUSALS: &[&str] = &[ "a nonzero angle delta on an 8x8 intra leaf in an inter frame (no gate reaches this leaf with one; the >=16x16 arm decodes deltas)", "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 8x8 HORZ/VERT partition (8x4/4x8 inter leaves; the 4x4 SPLIT arm decodes, but a rectangular sub-8x8 inter transform is unimplemented)", "an intra 4x4 block inside an inter frame's sub-8x8 split (this decoder codes only inter 4x4 sub-blocks there)", + "an intra 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition (this decoder codes only inter sub-blocks there)", "a sub-8x8 inter block under a scaled reference (superres, unimplemented)", + "an inter 8x8 HORZ partition (two BLOCK_8X4 inter leaves; decoded, but a measured sweep desyncs the block to its right)", "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 f692b057..d846fa17 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -7519,10 +7519,12 @@ mod tests { /// oracle `aomdec`'s `EC_TRACE` prints `EC_PART_VAL ... bsize=3 value=3` /// on this recipe's inter frames. /// - /// `--enable-rect-partitions=0` because the 8x4/4x8 sub-8x8 leaves are - /// still refused (a rectangular sub-8x8 inter transform is unimplemented); - /// with rect on, aomenc picks HORZ/VERT below 8x8 far more often than - /// SPLIT and no attempt reaches a split group. + /// Attempts 0..7 run `--enable-rect-partitions=0`, where aomenc can only + /// pick SPLIT below 8x8; attempts 8..15 repeat the schedule with rect on, + /// where it picks HORZ/VERT below 8x8 far more often than SPLIT -- the VERT + /// arm (two `BLOCK_4X8` inter leaves, `decode_inter_sub8_rect2`) is what the + /// `rect_fired` assert pins. The HORZ twin decodes but is refused by name + /// (lane-intersub8 r3: a measured sweep desyncs the block to its right). #[test] fn a_real_aomenc_inter_sequence_with_a_sub8x8_inter_split_decodes_pixel_exact() { const NAME: &str = @@ -7538,10 +7540,17 @@ mod tests { } let (width, height, frame_count) = (192usize, 128usize, 6usize); let mut fired = 0u32; + // lane-intersub8 r3: attempts 8..15 repeat the same schedule with + // `--enable-rect-partitions=1`, where aomenc picks PARTITION_VERT below + // 8x8 (two BLOCK_4X8 inter leaves, one shared 4x4 chroma unit predicted + // from BOTH sub-blocks' mvs). The HORZ twin is still refused by name. + let mut rect_fired = 0u32; let mut refusals: Vec = Vec::new(); let mut out_of_scope_mismatch = 0u32; for bit_depth in [8u32, 10u32] { - for attempt in 0..8u32 { + for attempt in 0..16u32 { + let rect = attempt / 8; + let rect_arg = format!("--enable-rect-partitions={rect}"); // lane-intersub8 r2, measured schedule (sweep of cq // {8,10,12,14,16,18,20} x sp {3,6,9,12} x both depths, // `decode_probe`'s `sub8_inter_split:` counter vs an ffmpeg @@ -7557,7 +7566,7 @@ mod tests { // is not selecting quantizers that hide a defect of its own // shape. let cq = [12, 14][(attempt % 2) as usize]; - let sp = 3 + (attempt / 2) * 3; + let sp = 3 + ((attempt % 8) / 2) * 3; let duration = frame_count as f64 / 25.0; let source = format!( "color=c=gray:s={width}x{height}:d={duration}:r=25,format=gray,\ @@ -7592,6 +7601,7 @@ mod tests { "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--min-partition-size=4", "--max-partition-size=16", + &rect_arg, "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) @@ -7615,6 +7625,7 @@ mod tests { ); let stream = out.stdout; let before = decode::sub8_inter_split_hits(); + let before_rect = decode::sub8_inter_rect_hits(); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); @@ -7628,7 +7639,10 @@ mod tests { } Ok(frames) => frames, }; - let groups = decode::sub8_inter_split_hits() - before; + let split_groups = decode::sub8_inter_split_hits() - before; + let rect_now = decode::sub8_inter_rect_hits(); + let rect_groups = (rect_now.0 - before_rect.0) + (rect_now.1 - before_rect.1); + let groups = split_groups + rect_groups; let ffmpeg_frames = if bit_depth == 10 { ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) } else { @@ -7645,7 +7659,7 @@ mod tests { out_of_scope_mismatch += 1; eprintln!( "{NAME}: {bit_depth}-bit attempt {attempt} MISMATCHES with zero \ - sub-8x8 inter split group -- another shape's defect" + sub-8x8 inter group -- another shape's defect" ); } refusals.push(format!( @@ -7663,17 +7677,26 @@ mod tests { assert_eq!(got.v, want.v, "V frame {i} ({bit_depth}-bit attempt {attempt})"); } fired += 1; + if rect_groups > 0 { + rect_fired += 1; + } } } assert_eq!( out_of_scope_mismatch, 0, - "{NAME}: attempts mismatched with no sub-8x8 inter split group" + "{NAME}: attempts mismatched with no sub-8x8 inter group" ); assert!( fired >= 2, "{NAME}: fewer than 2 firing+pixel-exact attempts:\n{}", refusals.join("\n") ); + assert!( + rect_fired >= 2, + "{NAME}: fewer than 2 firing+pixel-exact attempts with a RECTANGULAR sub-8x8 \ + inter leaf (4x8):\n{}", + refusals.join("\n") + ); } #[test] diff --git a/lanes/intersub8-r3.handoff.md b/lanes/intersub8-r3.handoff.md new file mode 100644 index 00000000..c60f00d8 --- /dev/null +++ b/lanes/intersub8-r3.handoff.md @@ -0,0 +1,79 @@ +# lane-intersub8 r3 — HANDOFF (4x8 gated GREEN, 8x4 implemented but refused) + +## What is wired (decode.rs `decode_inter_sub8_rect2`, ~line 19100) +One function serves both shapes (`vert: bool`); the call site +(`part8 == PARTITION_VERT` arm of the sub-16 SPLIT leaf loop, ~decode.rs:22630) currently +only reaches it with `vert = true`. +* mode info per sub-block in `decodemv.c` `read_inter_frame_mode_info` order: segment_id, + skip, cdef/dq/dlf, intra_inter, single ref only (`is_comp_ref_allowed(BLOCK_8X4)` false -> + no skip_mode, no comp_mode), no interintra / motion_mode / OBMC / warp (all need min dim 8), + `read_mb_interp_filter` DOES run. +* tx: `block_signals_txsize(BLOCK_8X4)` true (blockd.h:1027) -> ONE `txfm_partition` symbol + under TX_MODE_SELECT at `max_txsize_rect_lookup[BLOCK_8X4] = TX_8X4`, through r14's general + `read_var_tx_size` with max `(bw,bh)` and `blk_max_px = 8`; `sub_tx_size_map[TX_8X4]=TX_4X4` + so a split resolves to two TX_4X4 units (libaom's early return). Skip -> `set_txfm_ctxs` + skip_inter arm. +* luma sets NEW in cdf_state.rs: `TxbSet::LumaRect8x4Inter` / `LumaRect8x4InterSet1` = + `LumaRect4x8`'s tables (8x8 coefficient class, 32-position eob_pt) with `inter_tx_type_4` / + `inter_tx_type_4_set1` (EXT_TX_SET_ALL16 at the TX_4X4 inter row). Split TX_4X4 units read + `Luma4Inter{,Set1}`. Whole-leaf unit passes `luma_skip_ctx: None` (it IS the plane block, + `get_txb_ctx_general` fixes 0); split units pass `luma_skip_ctx_rect`. +* chroma once, on the SECOND sub-block (`is_chroma_reference` under 4:2:0: bottom 8x4 / + right 4x8), as one TX_4X4 per plane (`TxbSet::Chroma4`), inheriting the FIRST luma TU's + tx_type; prediction is the sub8x8 one (`reconinter.c build_inter_predictors_sub8x8`, + `row_start = -(block_size_high==4 && ss_y)`): two 4x2 (HORZ) / 2x4 (VERT) pieces, each from + its own sub-block's ref/mv/filters. +* neighbour writes per mi: `grid_stamp_rect` (size=w_mi, size_h=h_mi over the true span), + `record_mode_mi`, `record_inter_rect_mi`, `fill_skip_grid_rect`, `fill_lf_grid_rect`, + `record_mi_luma_rect` per TU; partition tail `above_side_mi = bw`, `left_side_mi = bh`. + +## The 8x4 defect (refused by name, NOT fixed) +Stream: `~/.cache/intersub8-tmp/gen_t.sh 14 3 8 d1.obu --enable-rect-partitions=1` +(transposed-structure geq source: `128+58*sin((Y+N*3)/6)+18*sin(X/23)`, 192x128, 6 frames). +Decode-order frame 5 mismatches; frames 0..4 exact. 4 HORZ groups, all at mi rows 30/31, +cols 4 and 6. +Range ladder (`EC_TRACE_PART=1` ours, new gated print at each partition read, vs aomdec +`EC_TRACE=1` `EC_PART`; and `EC_TRACE_MODE` `EC_MODE`; and `EC_TRACE_COEFF` `tag=all_zero`): +* every element matches through the 16x16 partition read at mi(28,8) (ctx 2 == oracle's 6, + entry rng 37489 both) and through `EC_MODE mi_row=30 mi_col=8 rng=46505` (both); +* that slot is PARTITION_HORZ -> two BLOCK_16X8 at (28,8) and (30,8); +* block (30,8)'s LEFT neighbour is our HORZ 8x4 group at (30,6); +* first divergence is INSIDE (30,8)'s mode info, AFTER its `EC_MODE` print (so skip and + intra_inter are still right) and BEFORE its first coefficient TU: our TU entry rng 54923 vs + oracle 61760 (`EC_COEFF plane=0 tx_size=8` = TX_16X8, all_zero=0 both). all_zero #468 of + 470/473. +=> some per-ROW neighbour band our HORZ group leaves at rows 30/31 is wrong. Remaining +suspects, in read order after the print: `left_ref[30]`, the mv stack (MiGrid cells (30,7)/ +(31,7)), `left_filter[30]`, `left_txfm[30]`. All four were transcribed against libaom and +looked right on inspection -- not bisected. NOTE ledger: this oracle's +`EC_AV1_PREFILT_DUMP` is already post-deblock, so pixel-stage bisection is confounded; use +the range ladder only. + +## Provable vs not +* PROVABLE (gated): two-BLOCK_4X8 (PARTITION_VERT) inter leaves. Sweep + `~/.cache/intersub8-tmp/sweep2.sh` (28 arms, cq {8,12,14,16,20,26,32} x sp {3,6,9,12} x + 8/10-bit, original source, rect on): every arm with `vert4x8 > 0` outside the known-bad + cq8 band decoded pixel-exact; the two cq8 mismatches are r2's CDEF |d|=1 defect. +* NOT PROVABLE: 8x4 (HORZ). `sweep3.sh` (transposed source): 4 of 28 arms mismatch with + `horz8x4 > 0` at cq 12/14/16/32, i.e. outside the CDEF band. Refused by name. + +## Gate +`cargo test -p ec-av1 --lib sub8x8_inter_split` -> ok, 1 passed / 0 failed (414 filtered), +28.94s. The test now runs 16 attempts per depth: 0..7 `--enable-rect-partitions=0` (SPLIT), +8..15 `=1` (VERT). Asserts `fired >= 2`, NEW `rect_fired >= 2`, `out_of_scope_mismatch == 0`, +every decode-order frame Y/U/V. `refusal_inventory` 3/3 ok, `gate_coverage` 9/9 ok. + +## Not done +* Full `cargo test -p ec-av1 --lib` suite: NOT RUN this round (turn cap). r2's unit + `intersub8-suite-1788328909` was still running on a stale binary at round start and is now + gone from the unit list; `$HOME/.cache/intersub8-suite-r2.log` has 218 ok / 0 FAILED and no + `test result:` line. Start r3's suite per COMMON's SUITE RUNS recipe, log + `$HOME/.cache/intersub8-suite-r3.log`. +* HG film probe: not run. +* `--tile-columns=1` gate arm: still owed (no NEW per-mi band was added, all reused). + +## Exact next step +1. Start the r3 suite. 2. Bisect the 8x4 defect on `d1.obu` frame 5 block (30,8) by dumping, +one at a time, the four left-band values our HORZ group writes at rows 30/31 and comparing +against an instrumented aomdec print of `xd->left_mbmi`/`left_txfm_context[0]` at that block. +3. Lift the HORZ refusal with a `sweep3.sh`-based arm once green.