diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index c5394141..de755ba4 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -969,6 +969,16 @@ pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ /// uses DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`. pub const INTER_TX_TYPE_SET3_32: [u16; 3] = [748, 32768, 0]; +/// `Default_Inter_Ext_Tx_Cdf`'s `TX_SET_INTER_3` row for `TX_16X16` (spec +/// 9.4): the transform type of an `is_inter` 16x16 luma transform coded under +/// `reduced_tx_set` (spec 5.11.48's `use_reduced_set` branch forces +/// `TX_SET_INTER_3` for every inter size below 32x32, the same two-symbol set +/// [`INTER_TX_TYPE_SET3_32`] reads at 32x32) -- without `reduced_tx_set` a +/// 16x16 inter transform reads the twelve-symbol `TX_SET_INTER_2` instead, +/// which this crate does not carry a table for. The writer only ever uses +/// DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`, same as the 32x32 table. +pub const INTER_TX_TYPE_SET3_16: [u16; 3] = [1998, 32768, 0]; + // The inter-frame tables below (spec 9.4) support the syntax an inter block // needs: whether it is coded as intra at all, which reference frame and // motion vector it takes when it is not. Compound reference and compound diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 38db70dd..0aedb134 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -31,6 +31,12 @@ pub(crate) enum TxbSet { Luma64, /// The 16x16 luma transform of a 16x16 block. Luma16, + /// The 16x16 luma transform of an `is_inter` 16x16 block, coded under + /// `reduced_tx_set` (see [`crate::cdf::INTER_TX_TYPE_SET3_16`]'s doc + /// comment): the same coefficient tables [`Luma16`](TxbSet::Luma16) + /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s + /// five-symbol, mode-indexed one. + Luma16Inter, /// The 8x8 transform of a chroma plane of a 16x16 block. Chroma8, /// The 16x16 transform of a chroma plane of a 32x32 block. @@ -160,6 +166,10 @@ pub(crate) struct Cdfs { /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it /// is not indexed by mode. pub inter_tx_type_32: [u16; 3], + /// The transform type of an `is_inter` 16x16 luma transform under + /// `reduced_tx_set` (spec `TX_SET_INTER_3`, `TX_16X16` row) -- not + /// indexed by mode, same as [`Self::inter_tx_type_32`]. + pub inter_tx_type_16: [u16; 3], /// The DC sign of a chroma coefficient, shared by both chroma sets. pub dc_sign_chroma: [[u16; 3]; 3], /// Whether an inter frame's block is coded as intra. @@ -487,6 +497,7 @@ impl Cdfs { dc_sign_luma: cdf::DC_SIGN_LUMA, intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, + inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16, dc_sign_chroma: cdf::DC_SIGN_CHROMA, intra_inter: cdf::INTRA_INTER, single_ref: cdf::SINGLE_REF, @@ -547,6 +558,17 @@ impl Cdfs { dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()), }, + TxbSet::Luma16Inter => TxbTables { + side: 16, + txb_skip: &mut self.txb_skip_luma_16, + eob_pt: &mut self.eob_pt_256_luma, + eob_extra: &mut self.eob_extra_luma_16, + base: &mut self.base_luma_16, + base_eob: &mut self.base_eob_luma_16, + br: &mut self.br_luma_16, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.inter_tx_type_16.as_mut_slice()), + }, TxbSet::Chroma8 => TxbTables { side: 8, txb_skip: &mut self.txb_skip_chroma_8, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 4aa0bda6..ac37c3d2 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -455,7 +455,16 @@ pub fn inter_frame_headers( interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, is_motion_mode_switchable: false, use_ref_frame_mvs: false, - reduced_tx_set: false, + // Forces `get_tx_set` (spec 5.11.48) to the two-symbol + // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only + // set this crate carries a table for at 16x16 (see + // `crate::cdf::INTER_TX_TYPE_SET3_16`'s doc comment). A 32x32 inter + // transform is unaffected (its `txSzSqrUp == TX_32X32` branch already + // reads `TX_SET_INTER_3` regardless of this flag), and neither is any + // intra transform at any size (`get_tx_set`'s intra branch returns + // the same `TX_SET_INTRA_2` at 16x16 with or without it), so this + // only narrows the 16x16-and-smaller inter case this crate is adding. + reduced_tx_set: true, ..key }; Ok((seq, header)) @@ -1094,6 +1103,154 @@ fn code_square( ) } +/// Codes one 16x16 leaf of a straddling 32x32 quadrant as whichever costs +/// least of intra ([`code_square`], unchanged) or `NEARESTMV` against +/// `reference` at `stack.nearest_mv` -- the same NEARESTMV candidate +/// [`search_inter_block`] prices for a whole 32x32 block, just at this leaf's +/// own 16x16 side and through [`TxbSet::Luma16Inter`]. `NEWMV` (a motion +/// search of its own) is not attempted here: these leaves only ever fill the +/// sliver of real content a true edge lands in the middle of a 32x32 block, +/// not a leaf worth a full search, so `NEARESTMV`-or-intra is this function's +/// deliberate first cut (see `write_inter_frame_leaf`'s doc comment). +#[allow(clippy::too_many_arguments)] +fn code_square_inter( + luma: &mut Plane, + chroma: &mut [Plane; 2], + (x, y): (usize, usize), + search: &Search, + mode_bits: &[f64; 13], + reference: &Picture, + stack: &MvStack, +) -> BlockCoeffs { + let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), SUB, search, mode_bits); + + let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); + let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); + let single_ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0) + + symbol_bits(&cdf::SINGLE_REF[0][2], 0) + + symbol_bits(&cdf::SINGLE_REF[0][3], 0); + let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV + + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero + + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV + + let mv = stack.nearest_mv; + let ref_luma = ( + &reference.y, + reference.width, + luma.true_width, + luma.true_height, + ); + let ref_u = ( + &reference.u, + reference.width / 2, + chroma[0].true_width, + chroma[0].true_height, + ); + let ref_v = ( + &reference.v, + reference.width / 2, + chroma[1].true_width, + chroma[1].true_height, + ); + let luma_trial = mc_trial( + luma, + x, + y, + SUB, + mv, + true, + ref_luma.0, + ref_luma.1, + ref_luma.2, + ref_luma.3, + false, + search.base_q_idx, + search.deadzone, + TxbSet::Luma16Inter, + ); + let u = mc_trial( + &chroma[0], + x / 2, + y / 2, + SUB / 2, + mv, + false, + ref_u.0, + ref_u.1, + ref_u.2, + ref_u.3, + false, + search.base_q_idx, + search.deadzone, + TxbSet::Chroma8, + ); + let v = mc_trial( + &chroma[1], + x / 2, + y / 2, + SUB / 2, + mv, + false, + ref_v.0, + ref_v.1, + ref_v.2, + ref_v.3, + false, + search.base_q_idx, + search.deadzone, + TxbSet::Chroma8, + ); + let skip = luma_trial.levels.iter().all(|&l| l == 0) + && u.levels.iter().all(|&l| l == 0) + && v.levels.iter().all(|&l| l == 0); + let inter_cost = luma_trial.sse + + u.sse + + v.sse + + search.lambda + * (skip_bits(skip) + + intra_inter_bits(true) + + single_ref_bits + + mode_bits_inter + + if skip { + 0.0 + } else { + luma_trial.bits + u.bits + v.bits + }); + + // NEARESTMV for 16x16 leaves, gated on real cost. r1/r2 found the gate's + // desync in `mvstack.rs`'s `find_mv_stack`: it lacked spec 7.10.2.4's + // extended row/col scan, invisible at 8-mi-only geometry (the immediate + // scan's own coverage already reached as far) but a real gap at 4-mi + // leaf geometry. r3 ported the extended scan and, while proving it a + // no-op at 8-mi (`extended_row_scan_is_a_no_op_at_8mi_geometry_*`, + // `mvstack.rs`), found and fixed a second bug the extended scan's + // coverage bookkeeping (`processed_rows`/`processed_cols`) exposed: an + // intra neighbour is a coded cell too (libaom's `scan_row_mbmi`/ + // `scan_col_mbmi` advance coverage from any candidate's `bsize`, + // regardless of whether it also casts a ref-frame-matching vote) -- + // `MiGrid` now records intra cells (`tile.rs`/`encode.rs`'s `else` + // branches), which is what made 8-mi geometry a true no-op instead of + // one that only held when every neighbour happened to be inter. + if inter_cost < intra_cost { + luma.commit(x, y, SUB, &luma_trial); + chroma[0].commit(x / 2, y / 2, SUB / 2, &u); + chroma[1].commit(x / 2, y / 2, SUB / 2, &v); + BlockCoeffs { + luma: coeffs(&luma_trial.levels, SUB), + u: coeffs(&u.levels, SUB / 2), + v: coeffs(&v.levels, SUB / 2), + mode: DC_PRED as u8, + skip, + inter: Some(InterInfo { + mode: InterMode::NearestMv, + mv, + }), + } + } else { + intra_block + } +} + /// The reconstructed samples one 32x32 square covers in all three planes, so /// that a partition trial can be undone. fn snapshot(luma: &Plane, chroma: &[Plane; 2], (x, y): (usize, usize)) -> [Vec; 3] { @@ -2287,20 +2444,32 @@ pub(crate) fn encode_inter_frame( &stack, ); - if let Some(info) = block.inter { - for dr in 0..8 { - for dc in 0..8 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { + for dr in 0..8 { + for dc in 0..8 { + grid.set( + mi_row + dr, + mi_col + dc, + match block.inter { + Some(info) => MiInfo { is_inter: true, ref_frame: LAST_FRAME, mv: info.mv, is_new_mv: matches!(info.mode, InterMode::NewMv), + size: 8, }, - ); - } + // Intra: no vote, but still a coded cell + // -- mvstack's extended-scan coverage + // (`processed_rows`/`processed_cols`) + // must see it (module doc). + None => MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: 8, + }, + }, + ); } } blocks[r32 * cols + c32] = Quadrant::Whole(block); @@ -2338,24 +2507,50 @@ pub(crate) fn encode_inter_frame( ), )); } - // Coded through the inter frame's intra branch only (see - // `crate::tile::sb_coeff_inter_frame_tile`'s doc comment on - // its `Quadrant::Split` arm): no `Luma16Inter` tx_type - // table exists yet for a genuine inter leaf at this size, - // and these leaves only ever fill the sliver of real - // content a true edge lands in the middle of a 32x32 - // block, not a leaf a motion search chose on its own. + // Each leaf searches intra against its own `NEARESTMV` + // candidate (`code_square_inter`), same real-inter choice + // `search_inter_block` makes for a whole 32x32 block, at + // this leaf's own 4x4-mi mv-stack window. let mut leaves = Vec::with_capacity(sub_positions.len()); for (sr, sc) in sub_positions { let (x, y) = (sc * SUB, sr * SUB); - let (block, _cost) = code_square( + let (mi_row, mi_col) = (sr * 4, sc * 4); + let stack = find_mv_stack( + &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, + ); + let block = code_square_inter( &mut luma, &mut chroma, (x, y), - SUB, &search, &mode_bits_table, + reference, + &stack, ); + for dr in 0..4 { + for dc in 0..4 { + grid.set( + mi_row + dr, + mi_col + dc, + match block.inter { + Some(info) => MiInfo { + is_inter: true, + ref_frame: LAST_FRAME, + mv: info.mv, + is_new_mv: matches!(info.mode, InterMode::NewMv), + size: 4, + }, + None => MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: 4, + }, + }, + ); + } + } leaves.push(block); } blocks[r32 * cols + c32] = Quadrant::Split(leaves); diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index c255c8de..ce291c63 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -97,6 +97,18 @@ pub struct MiInfo { /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a /// neighbour's own coded-with-NEWMV state, not this block's. pub is_new_mv: bool, + /// The side, in 4x4 units, of the square block this unit belongs to (8 + /// for a whole 32x32 block, 4 for a 16x16 leaf -- every block this crate + /// ever codes is square, so one dimension is enough). Feeds the extended + /// row/col scan's `processed_rows`/`processed_cols` (spec 7.10.2.2/.3, + /// libaom `scan_row_mbmi`/`scan_col_mbmi`'s `inc` term): a query block no + /// wider than its immediate neighbour has that neighbour's whole span + /// already covered by the immediate (-1) scan, so the extended scan + /// below only has to run past that. Set for *every* coded unit, intra or + /// inter -- libaom's `scan_row_mbmi` advances `processed_rows` from any + /// candidate's `bsize` regardless of whether it also casts a vote (that + /// requires a ref-frame match, checked separately in `add_ref_mv_candidate`). + pub size: usize, } /// The whole frame's `mi` grid, one [`MiInfo`] per 4x4 unit, in raster order. @@ -188,6 +200,10 @@ const MAX_STACK_SIZE: usize = 8; /// this module makes — see the module doc). const IMMEDIATE_WEIGHT: u32 = 2; +/// Spec `MVREF_ROW_COLS`: the extended row/col scan runs at offsets `-3` and +/// `-5` (`idx = 2..=3`), one past `IMMEDIATE_WEIGHT`'s own `-1`. +const MVREF_ROW_COLS: usize = 3; + /// Adds one candidate MV with `weight` to `candidates`, merging it into an /// existing entry with the same MV rather than duplicating it (spec /// `add_ref_mv_candidate`, 7.10.2.5). @@ -200,7 +216,14 @@ fn add_candidate(candidates: &mut Vec, mv: (i32, i32), weight: u32) } /// The row-above scan (spec 7.10.2.2): every 4x4 unit directly above the -/// block's span. Returns whether any unit voted. +/// block's span. Returns whether any unit voted, and `processed_rows` +/// (libaom `scan_row_mbmi`'s output): how many rows past the immediate one +/// this span already covers by *geometry* alone (0 unless a same-or-wider +/// neighbour occupies it, in which case that neighbour's own `size` is the +/// coverage, spec 7.10.2.2's `inc` term reduced to this crate's square, +/// fixed-weight candidates) -- tracked for *every* coded cell, intra or +/// inter, since libaom's `processed_rows` update sits outside the +/// ref-frame-matching `add_ref_mv_candidate` call. fn scan_row( grid: &MiGrid, mi_row: usize, @@ -209,26 +232,31 @@ fn scan_row( ref_frame: i8, candidates: &mut Vec, newmv_count: &mut u32, -) -> bool { +) -> (bool, usize) { let Some(row) = mi_row.checked_sub(1) else { - return false; + return (false, 0); }; let mut found = false; + let mut processed_rows = 0; for col in mi_col..mi_col + bw4 { - if let Some(info) = grid.get(row, col) - && info.is_inter - && info.ref_frame == ref_frame - { + let Some(info) = grid.get(row, col) else { + continue; + }; + if bw4 <= info.size { + processed_rows = processed_rows.max(info.size); + } + if info.is_inter && info.ref_frame == ref_frame { found = true; add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); *newmv_count += u32::from(info.is_new_mv); } } - found + (found, processed_rows) } /// The col-left scan (spec 7.10.2.3): every 4x4 unit directly left of the -/// block's span. Returns whether any unit voted. +/// block's span. Returns whether any unit voted, and `processed_cols` +/// (`scan_row`'s doc, transposed). fn scan_col( grid: &MiGrid, mi_row: usize, @@ -237,21 +265,99 @@ fn scan_col( ref_frame: i8, candidates: &mut Vec, newmv_count: &mut u32, -) -> bool { +) -> (bool, usize) { let Some(col) = mi_col.checked_sub(1) else { - return false; + return (false, 0); }; let mut found = false; + let mut processed_cols = 0; for row in mi_row..mi_row + bh4 { - if let Some(info) = grid.get(row, col) - && info.is_inter - && info.ref_frame == ref_frame - { + let Some(info) = grid.get(row, col) else { + continue; + }; + if bh4 <= info.size { + processed_cols = processed_cols.max(info.size); + } + if info.is_inter && info.ref_frame == ref_frame { found = true; add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); *newmv_count += u32::from(info.is_new_mv); } } + (found, processed_cols) +} + +/// The extended row scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS` row +/// positions, libaom `mvref_common.c`'s second loop): probes rows `-3` and +/// `-5`, shifted one column right (`col_offset = 1`, libaom's `col_adj` +/// collapses to that for every block size this crate ever queries), each +/// only run when the immediate scan's own `processed_rows` hasn't already +/// covered it and the tile's top edge doesn't clip it first. Its match folds +/// into the row side (`ref_match_count`, never `nearest_match`), same as the +/// corner probe. +fn scan_row_extended( + grid: &MiGrid, + mi_row: usize, + mi_col: usize, + bw4: usize, + ref_frame: i8, + candidates: &mut Vec, + processed_rows: usize, +) -> bool { + let max_row_offset = mi_row.min(MVREF_ROW_COLS * 2); + let mut found = false; + for idx in 2..=MVREF_ROW_COLS { + let offset = idx * 2 - 1; + if offset > max_row_offset || offset <= processed_rows { + continue; + } + let Some(row) = mi_row.checked_sub(offset) else { + continue; + }; + for col in mi_col + 1..mi_col + 1 + bw4 { + if let Some(info) = grid.get(row, col) + && info.is_inter + && info.ref_frame == ref_frame + { + found = true; + add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); + } + } + } + found +} + +/// The extended col scan: `scan_row_extended`, transposed (probes cols `-3` +/// and `-5`, shifted one row down). +fn scan_col_extended( + grid: &MiGrid, + mi_row: usize, + mi_col: usize, + bh4: usize, + ref_frame: i8, + candidates: &mut Vec, + processed_cols: usize, +) -> bool { + let max_col_offset = mi_col.min(MVREF_ROW_COLS * 2); + let mut found = false; + for idx in 2..=MVREF_ROW_COLS { + let offset = idx * 2 - 1; + if offset > max_col_offset || offset <= processed_cols { + continue; + } + let Some(col) = mi_col.checked_sub(offset) else { + continue; + }; + for row in mi_row + 1..mi_row + 1 + bh4 { + if let Some(info) = grid.get(row, col) + && info.is_inter + && info.ref_frame == ref_frame + { + found = true; + add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); + } + } + } found } @@ -363,7 +469,7 @@ pub fn find_mv_stack( ) -> MvStack { let mut candidates = Vec::new(); let mut newmv_count = 0u32; - let found_above = scan_row( + let (found_above, processed_rows) = scan_row( grid, mi_row, mi_col, @@ -372,7 +478,7 @@ pub fn find_mv_stack( &mut candidates, &mut newmv_count, ); - let found_left = scan_col( + let (found_left, processed_cols) = scan_col( grid, mi_row, mi_col, @@ -417,7 +523,34 @@ pub fn find_mv_stack( &mut candidates, &mut dummy_newmv_count, ); - let ref_match_count = usize::from(row_matched || corner_matched) + usize::from(found_left); + // The extended row/col scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS`): + // only reachable once the immediate scan's own reach (`processed_rows`/ + // `processed_cols`) leaves a gap the tile's edge doesn't already clip. + // At this module's fixed 8-mi block geometry with every coded cell + // (intra or inter) now feeding coverage, that reach always hits + // `MVREF_ROW_COLS`'s farthest offset first -- a true no-op there (see + // module doc and `extended_scan_is_a_no_op_at_8mi_geometry` below). Folds + // into the row/col match booleans, never `nearest_match`. + let row_matched_ext = scan_row_extended( + grid, + mi_row, + mi_col, + bw4, + ref_frame, + &mut candidates, + processed_rows, + ); + let col_matched_ext = scan_col_extended( + grid, + mi_row, + mi_col, + bh4, + ref_frame, + &mut candidates, + processed_cols, + ); + let ref_match_count = usize::from(row_matched || corner_matched || row_matched_ext) + + usize::from(found_left || col_matched_ext); // Highest weight first; a stable sort keeps scan order (above, left, // top-right, corner) among ties, matching spec 7.10.2.6. The corner @@ -537,6 +670,10 @@ mod tests { ref_frame: 1, mv, is_new_mv: false, + // 1: smaller than every `bw4`/`bh4` these small-grid tests use, + // so it never trips the extended-scan coverage boost -- these + // tests only exercise the immediate/corner/top-right scans. + size: 1, } } @@ -682,6 +819,7 @@ mod tests { ref_frame: 1, mv: (4, 4), is_new_mv: true, + size: 1, }, ); // Only the row matches (nearest_match == 1): newmv_count > 0 picks @@ -742,4 +880,104 @@ mod tests { assert_eq!(single_ref_ctx(false), 1); assert_eq!(single_ref_ctx(true), 2); } + + /// An 8-mi-sized (`size: 8`) coded-but-intra neighbour: contributes no + /// vote (`is_inter: false`) but must still count for extended-scan + /// coverage, the way libaom's `scan_row_mbmi`/`scan_col_mbmi` advance + /// `processed_rows`/`processed_cols` from any candidate's `bsize` alone. + fn intra8() -> MiInfo { + MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: 8, + } + } + + /// An 8-mi-sized inter neighbour with an arbitrary, distinctive `mv` -- + /// used below as a "would this get picked up" tracer at extended-scan + /// offsets that a correct implementation must never reach at 8-mi + /// geometry. + fn inter8(mv: (i32, i32)) -> MiInfo { + MiInfo { + is_inter: true, + ref_frame: 1, + mv, + is_new_mv: false, + size: 8, + } + } + + /// The class of bug this round fixed: at 8-mi geometry, an intra + /// neighbour directly above the query block must suppress the extended + /// row scan exactly as an inter one would (spec 7.10.2.2's + /// `processed_rows` tracks *coded* neighbours, not merely *matching* + /// ones). Two tracer candidates sit at the row's extended-scan offsets + /// (-3 and -5, spec 7.10.2.4) with MVs no other scan could produce; if + /// the coverage bug regresses, they show up in `stack.entries`. + #[test] + fn extended_row_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour() { + let mut grid = MiGrid::new(32, 32); + let (mi_row, mi_col) = (10, 10); + for col in mi_col..mi_col + 8 { + grid.set(mi_row - 1, col, intra8()); + } + for col in mi_col + 1..mi_col + 1 + 8 { + grid.set(mi_row - 3, col, inter8((999, 999))); + grid.set(mi_row - 5, col, inter8((888, 888))); + } + + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); + + assert!( + stack.entries.is_empty(), + "extended row scan fired at 8-mi geometry: {:?}", + stack.entries + ); + } + + /// `extended_row_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour`, + /// transposed to the col-left scan. + #[test] + fn extended_col_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour() { + let mut grid = MiGrid::new(32, 32); + let (mi_row, mi_col) = (10, 10); + for row in mi_row..mi_row + 8 { + grid.set(row, mi_col - 1, intra8()); + } + for row in mi_row + 1..mi_row + 1 + 8 { + grid.set(row, mi_col - 3, inter8((999, 999))); + grid.set(row, mi_col - 5, inter8((888, 888))); + } + + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); + + assert!( + stack.entries.is_empty(), + "extended col scan fired at 8-mi geometry: {:?}", + stack.entries + ); + } + + /// Same shape, but the immediate neighbour is itself inter (a same-size + /// vote) rather than intra -- the geometry-only coverage path must not + /// have broken the ordinary matching one. + #[test] + fn extended_row_scan_is_a_no_op_at_8mi_geometry_behind_an_inter_neighbour() { + let mut grid = MiGrid::new(32, 32); + let (mi_row, mi_col) = (10, 10); + for col in mi_col..mi_col + 8 { + grid.set(mi_row - 1, col, inter8((4, 4))); + } + for col in mi_col + 1..mi_col + 1 + 8 { + grid.set(mi_row - 3, col, inter8((999, 999))); + grid.set(mi_row - 5, col, inter8((888, 888))); + } + + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); + + assert_eq!(stack.entries.len(), 1); + assert_eq!(stack.entries[0].mv, (4, 4)); + } } diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 06bf0e8c..454107a1 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -2034,10 +2034,13 @@ pub fn sb_coeff_inter_frame_tile( let at16 = (sr, sc); let ctx16 = neighbours.partition_ctx(at16, SUB); enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]); - write_inter_frame_intra_leaf( + write_inter_frame_leaf( &mut enc, &mut cdfs, &mut neighbours, + &mut grid, + mi_cols, + mi_rows, leaf, at16, &scan16, @@ -2114,6 +2117,7 @@ pub fn sb_coeff_inter_frame_tile( ref_frame: LAST_FRAME, mv, is_new_mv, + size: 8, }, ); for dr in 0..8 { @@ -2129,6 +2133,7 @@ pub fn sb_coeff_inter_frame_tile( ref_frame: LAST_FRAME, mv, is_new_mv, + size: 8, }, ); } @@ -2142,6 +2147,24 @@ pub fn sb_coeff_inter_frame_tile( } enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); mode_for_tx = mode; + // Intra: no vote, but still a coded cell -- mvstack's + // extended-scan coverage must see it (module doc). + let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); + for dr in 0..8 { + for dc in 0..8 { + grid.set( + mi_row + dr, + mi_col + dc, + MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: 8, + }, + ); + } + } } if block.skip { @@ -2175,24 +2198,29 @@ pub fn sb_coeff_inter_frame_tile( } /// Writes one 16x16 leaf a straddling 32x32 quadrant's [`Quadrant::Split`] -/// splits into, always through the inter frame's intra branch (spec -/// `inter_frame_mode_info`'s intra branch, `is_inter` coded `false`): no -/// `Luma16Inter` tx_type table exists yet for a genuine inter leaf at this -/// size (see [`TxbSet::Luma32Inter`]'s doc comment), and these leaves only -/// ever fill the sliver of real content a true edge lands in the middle of a -/// 32x32 block, never a leaf a motion search chose on its own. Its own -/// `PARTITION_NONE` symbol is already written, same contract as +/// splits into: intra (spec `inter_frame_mode_info`'s intra branch, the only +/// one this writer used to code here) or real inter (`is_inter` coded +/// `true`), `NEARESTMV` only -- `NEWMV` at this size is this function's +/// caller's to never build (`Quadrant::Split`'s `BlockCoeffs.inter`) -- same +/// `single_ref`/mv-stack/DRL symbol chain as the whole-32x32 branch above, +/// just at this leaf's own 4x4-mi window (`bw4`/`bh4` of 4, not 8) and coded +/// through [`TxbSet::Luma16Inter`] rather than [`TxbSet::Luma32Inter`]. Its +/// own `PARTITION_NONE` symbol is already written, same contract as /// [`write_block`]. -fn write_inter_frame_intra_leaf( +#[allow(clippy::too_many_arguments)] +fn write_inter_frame_leaf( enc: &mut SymbolEncoder, cdfs: &mut Cdfs, neighbours: &mut Neighbours, + grid: &mut MiGrid, + mi_cols: u32, + mi_rows: u32, block: &BlockCoeffs, at: (usize, usize), scan16: &Vec, scan8: &Vec, ) -> Result<()> { - if usize::from(block.mode) >= INTRA_MODES { + if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES { return Err(Error::unsupported( "AV1 tile", format!( @@ -2204,26 +2232,105 @@ fn write_inter_frame_intra_leaf( /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s /// `size_group_lookup[BLOCK_16X16]`) for this leaf's own size. const SIZE_GROUP_16: usize = 2; + /// `Ref_Frame_List`'s `LAST_FRAME` (spec 3): the only reference this + /// writer's single-reference chain ever names, same as the 32x32 branch. + const LAST_FRAME: i8 = 1; let (r, c) = at; let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); let (has_above, has_left) = (r > 0, c > 0); - let ii_ctx = intra_inter_ctx( - has_above, - has_left, - neighbours.above_inter[c], - neighbours.left_inter[r], - ); - enc.symbol(0, &mut cdfs.intra_inter[ii_ctx]); // is_inter = false + let (above_inter, left_inter) = (neighbours.above_inter[c], neighbours.left_inter[r]); + let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); + let is_inter = block.inter.is_some(); + enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); + + let mode_for_tx; + if let Some(info) = block.inter { + let sr_ctx = single_ref_ctx(above_inter || left_inter); + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // p1: forward + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); // p3: LAST/LAST2 + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); // p4: LAST + + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); + let stack = find_mv_stack( + grid, + mi_row, + mi_col, + SUB_MI as usize, + SUB_MI as usize, + LAST_FRAME, + mi_cols as usize, + mi_rows as usize, + ); - let mode = usize::from(block.mode); - enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); - if (V_PRED..=D67_PRED).contains(&mode) { - enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]); + let is_new_mv = matches!(info.mode, InterMode::NewMv); + if is_new_mv { + enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); // NEWMV + } else { + enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not new + enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not zero + enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV + } + if is_new_mv && stack.entries.len() > 1 { + enc.symbol(0, &mut cdfs.drl_mode[stack.drl_ctx[0]]); + } + let mv = if is_new_mv { + write_mv( + enc, + &mut cdfs.mv_comp, + &mut cdfs.mv_joint, + info.mv, + stack.pred_mv, + )?; + info.mv + } else { + stack.nearest_mv + }; + for dr in 0..SUB_MI as usize { + for dc in 0..SUB_MI as usize { + grid.set( + mi_row + dr, + mi_col + dc, + MiInfo { + is_inter: true, + ref_frame: LAST_FRAME, + mv, + is_new_mv, + size: SUB_MI as usize, + }, + ); + } + } + mode_for_tx = 0; + } else { + let mode = usize::from(block.mode); + enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); + if (V_PRED..=D67_PRED).contains(&mode) { + enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]); + } + enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); + mode_for_tx = mode; + // Intra: no vote, but still a coded cell -- mvstack's extended-scan + // coverage must see it (module doc). + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); + for dr in 0..SUB_MI as usize { + for dc in 0..SUB_MI as usize { + grid.set( + mi_row + dr, + mi_col + dc, + MiInfo { + is_inter: false, + ref_frame: -1, + mv: (0, 0), + is_new_mv: false, + size: SUB_MI as usize, + }, + ); + } + } } - enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); if block.skip { let zero_grids = [ @@ -2231,7 +2338,7 @@ fn write_inter_frame_intra_leaf( vec![0i32; TX8 * TX8], vec![0i32; TX8 * TX8], ]; - neighbours.record(at, SUB, mode, &zero_grids); + neighbours.record(at, SUB, mode_for_tx, &zero_grids); } else { let grids = [ level_grid(&block.luma, TX16)?, @@ -2241,15 +2348,19 @@ fn write_inter_frame_intra_leaf( write_block_planes( enc, cdfs, - &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8], + if is_inter { + &[TxbSet::Luma16Inter, TxbSet::Chroma8, TxbSet::Chroma8] + } else { + &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8] + }, &grids, &[scan16, scan8, scan8], &neighbours.around(at, SUB), - mode, + mode_for_tx, ); - neighbours.record(at, SUB, mode, &grids); + neighbours.record(at, SUB, mode_for_tx, &grids); } - neighbours.record_inter(at, SUB, block.skip, false); + neighbours.record_inter(at, SUB, block.skip, is_inter); Ok(()) } @@ -4466,6 +4577,7 @@ mod tests { ref_frame: 1, mv, is_new_mv, + size: 8, }, ); }