diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 87a84a10..c057efaa 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -220,7 +220,7 @@ use crate::mc; use crate::msac::SymbolDecoder; use crate::mvstack::{ ALTREF_FRAME, ALTREF2_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST_FRAME, LAST2_FRAME, LAST3_FRAME, - MiGrid, MiInfo, NO_SIGN_BIAS, NeighbourRef, SignBiasTable, comp_reference_type_ctx, + MiGrid, MiInfo, NO_REF1, NO_SIGN_BIAS, NeighbourRef, SignBiasTable, comp_reference_type_ctx, find_mv_stack_with_sign_bias, reference_mode_ctx, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, uni_comp_ref_p1_ctx, @@ -3316,9 +3316,9 @@ fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)> let info = crate::mvstack::MiInfo { is_inter: dv.is_some(), ref_frame: 0, - ref_frame1: None, + ref_frame1: NO_REF1, mv: dv.unwrap_or((0, 0)), - mv1: None, + mv1: (0, 0), is_new_mv: dv.is_some(), is_global_mv0: false, is_global_mv1: false, @@ -4792,7 +4792,7 @@ pub(crate) fn build_motion_field( let mut saved: Option = None; for (rf, mv) in [ (info.ref_frame, Some(info.mv)), - (info.ref_frame1.unwrap_or(0), info.mv1), + (info.ref_frame1.max(0), Some(info.mv1)), ] { let (Some(mv), true) = (mv, rf > 0) else { continue; @@ -22623,7 +22623,7 @@ fn find_samples( && crate::envflags::var("EC_PROJ_MI") .is_ok_and(|v| v == format!("{mi_row},{mi_col}")); let single_ref_match = |info: &MiInfo| { - let m = info.ref_frame == ref_frame && info.ref_frame1.is_none(); + let m = info.ref_frame == ref_frame && info.ref_frame1 == NO_REF1; if proj_dbg { eprintln!( "EC_PROJ cand ref={} ref1={:?} size={} size_h={} is_inter={} want={ref_frame} match={m}", @@ -24256,31 +24256,29 @@ fn decode_inter_block( // too -- a square `bw4`x`bw4` stamp here claims a rect strip's // NEXT strip's own rows/cols before it decodes, corrupting // every later mvstack scan that reads this cell back. - for dr in 0..bh4 { - for dc in 0..bw4 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { - is_inter: true, - ref_frame: ref0, - ref_frame1: Some(ref1), - mv1: Some(mv1), - mv: mv0, - is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), - size: bw4 as u8, - size_h: bh4 as u8, - // libaom `is_global_mv_block`: per-ref-slot, mode - // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's - // own gm model > TRANSLATION (IDENTITY excluded - // too -- the two-predicate trap, distinct from - // `gm_nontrans` above which allows IDENTITY). - is_global_mv0, - is_global_mv1, - }, - ); - } - } + grid.fill_rect( + mi_row, + mi_col, + bh4, + bw4, + MiInfo { + is_inter: true, + ref_frame: ref0, + ref_frame1: ref1, + mv1, + mv: mv0, + is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), + size: bw4 as u8, + size_h: bh4 as u8, + // libaom `is_global_mv_block`: per-ref-slot, mode + // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's + // own gm model > TRANSLATION (IDENTITY excluded + // too -- the two-predicate trap, distinct from + // `gm_nontrans` above which allows IDENTITY). + is_global_mv0, + is_global_mv1, + }, + ); // spec 5.11.25's `comp_group_idx`/`compound_idx`: only read when // masked compound / distance-weighted compound are actually // enabled for this stream -- `is_any_masked_compound_used_here` @@ -25812,32 +25810,30 @@ fn decode_inter_block( // lane-rect r2: see the compound path's matching comment above -- // `grid` must be stamped with the block's true bh4/bw4 span, not // a square `bw4`x`bw4` guess. - for dr in 0..bh4 { - for dc in 0..bw4 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { - is_inter: true, - ref_frame, - // An interintra block records ref_frame[1] == - // INTRA_FRAME (0): warp-sample gathering - // (libaom av1_findSamples, mvref_common.c:1155) - // requires ref_frame[1] == NONE_FRAME, so such a - // neighbour must not donate samples or count in - // num_proj_ref. - ref_frame1: interintra_mode.map(|_| 0), - mv1: None, - mv, - is_new_mv, - size: bw4 as u8, - size_h: bh4 as u8, - is_global_mv0: is_global_mv_block, - is_global_mv1: false, - }, - ); - } - } + grid.fill_rect( + mi_row, + mi_col, + bh4, + bw4, + MiInfo { + is_inter: true, + ref_frame, + // An interintra block records ref_frame[1] == + // INTRA_FRAME (0): warp-sample gathering + // (libaom av1_findSamples, mvref_common.c:1155) + // requires ref_frame[1] == NONE_FRAME, so such a + // neighbour must not donate samples or count in + // num_proj_ref. + ref_frame1: if interintra_mode.is_some() { 0 } else { NO_REF1 }, + mv1: (0, 0), + mv, + is_new_mv, + size: bw4 as u8, + size_h: bh4 as u8, + is_global_mv0: is_global_mv_block, + is_global_mv1: false, + }, + ); mode_for_tx = 0; uv_predict_mode = DC_PRED; @@ -26644,26 +26640,24 @@ fn decode_inter_block( // lane's `r * SUB_MI` re-derivation would snap a sub-SUB strip to // its enclosing 16-px cell. let (mi_r0, mi_c0) = (rmi, cmi); - for dr in 0..write_h / MI { - for dc in 0..write_w / MI { - grid.set( - mi_r0 + dr, - mi_c0 + dc, - MiInfo { - is_inter: false, - ref_frame: -1, - ref_frame1: None, - mv1: None, - mv: (0, 0), - is_new_mv: false, - size: (write_w / MI) as u8, - size_h: (write_h / MI) as u8, - is_global_mv0: false, - is_global_mv1: false, - }, - ); - } - } + grid.fill_rect( + mi_r0, + mi_c0, + write_h / MI, + write_w / MI, + MiInfo { + is_inter: false, + ref_frame: -1, + ref_frame1: NO_REF1, + mv1: (0, 0), + mv: (0, 0), + is_new_mv: false, + size: (write_w / MI) as u8, + size_h: (write_h / MI) as u8, + is_global_mv0: false, + is_global_mv1: false, + }, + ); neighbours.record_inter_rect_mi( at, write_w / MI, @@ -26868,26 +26862,24 @@ fn decode_inter_block( ref_frame_for_lf = 0; globalmv_for_lf = false; let (mi_row, mi_col) = (rmi, cmi); - for dr in 0..side / 4 { - for dc in 0..side / 4 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { - is_inter: false, - ref_frame: -1, - ref_frame1: None, - mv1: None, - mv: (0, 0), - is_new_mv: false, - size: (side / 4) as u8, - size_h: (side / 4) as u8, - is_global_mv0: false, - is_global_mv1: false, - }, - ); - } - } + grid.fill_rect( + mi_row, + mi_col, + side / 4, + side / 4, + MiInfo { + is_inter: false, + ref_frame: -1, + ref_frame1: NO_REF1, + mv1: (0, 0), + mv: (0, 0), + is_new_mv: false, + size: (side / 4) as u8, + size_h: (side / 4) as u8, + is_global_mv0: false, + is_global_mv1: false, + }, + ); // lane-intrainter r1: an intra block in an inter frame reads the same // `tx_depth` symbol a key-frame block does, and when it splits, the @@ -27787,8 +27779,8 @@ fn decode_inter_sub8_split4( MiInfo { is_inter: true, ref_frame, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv, size: 1, @@ -28319,26 +28311,24 @@ fn decode_intra_sub8_leaf( // intra-in-inter block writes them: `is_inter = 0`, `INTRA_FRAME` ref, no // interpolation filter, no skip mode. neighbours.record_inter_rect_mi(lmi, w_mi, h_mi, skip, false, 0, [3, 3], false); - for dr in 0..h_mi { - for dc in 0..w_mi { - grid.set( - lmi.0 + dr, - lmi.1 + dc, - MiInfo { - is_inter: false, - ref_frame: -1, - ref_frame1: None, - mv1: None, - mv: (0, 0), - is_new_mv: false, - size: w_mi as u8, - size_h: h_mi as u8, - is_global_mv0: false, - is_global_mv1: false, - }, - ); - } - } + grid.fill_rect( + lmi.0, + lmi.1, + h_mi, + w_mi, + MiInfo { + is_inter: false, + ref_frame: -1, + ref_frame1: NO_REF1, + mv1: (0, 0), + mv: (0, 0), + is_new_mv: false, + size: w_mi as u8, + size_h: h_mi as u8, + is_global_mv0: false, + is_global_mv1: false, + }, + ); SUB8_INTRA_RECT_HITS.with(|h| { let mut s = h.get(); s[usize::from(vert)] += 1; @@ -29024,26 +29014,24 @@ fn grid_stamp_rect( 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 as u8, - size_h: h_mi as u8, - is_global_mv0: false, - is_global_mv1: false, - }, - ); - } - } + grid.fill_rect( + mi_row, + mi_col, + h_mi, + w_mi, + MiInfo { + is_inter: true, + ref_frame, + ref_frame1: NO_REF1, + mv1: (0, 0), + mv, + is_new_mv, + size: w_mi as u8, + size_h: h_mi as u8, + is_global_mv0: false, + is_global_mv1: false, + }, + ); } /// Decodes one 8x8 leaf of a straddling 16x16 inter-frame block @@ -29605,26 +29593,24 @@ fn decode_inter_block8( COMPOUND_WARP_HITS.with(|c| c.set(c.get() + 1)); COMPOUND_WARP_HITS_8.with(|c| c.set(c.get() + 1)); } - for dr in 0..2 { - for dc in 0..2 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { - is_inter: true, - ref_frame: ref0, - ref_frame1: Some(ref1), - mv1: Some(mv1), - mv: mv0, - is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), - size: 2, - size_h: 2, - is_global_mv0: is_global_mv0_c, - is_global_mv1: is_global_mv1_c, - }, - ); - } - } + grid.fill_rect( + mi_row, + mi_col, + 2, + 2, + MiInfo { + is_inter: true, + ref_frame: ref0, + ref_frame1: ref1, + mv1, + mv: mv0, + is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), + size: 2, + size_h: 2, + is_global_mv0: is_global_mv0_c, + is_global_mv1: is_global_mv1_c, + }, + ); mode_for_tx = 0; let mut inter0_y = vec![0i32; SIDE * SIDE]; @@ -30282,28 +30268,26 @@ fn decode_inter_block8( warp_params.is_some(), ); } - for dr in 0..2 { - for dc in 0..2 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { - is_inter: true, - ref_frame, - // INTRA_FRAME marker for interintra blocks -- keeps - // them out of warp-sample gathering (see 16/32 site). - ref_frame1: interintra_mode.map(|_| 0), - mv1: None, - mv, - is_new_mv, - size: 2, - size_h: 2, - is_global_mv0: is_global_mv_block, - is_global_mv1: false, - }, - ); - } - } + grid.fill_rect( + mi_row, + mi_col, + 2, + 2, + MiInfo { + is_inter: true, + ref_frame, + // INTRA_FRAME marker for interintra blocks -- keeps + // them out of warp-sample gathering (see 16/32 site). + ref_frame1: if interintra_mode.is_some() { 0 } else { NO_REF1 }, + mv1: (0, 0), + mv, + is_new_mv, + size: 2, + size_h: 2, + is_global_mv0: is_global_mv_block, + is_global_mv1: false, + }, + ); mode_for_tx = 0; // lane-scaledref r1: this leaf's own prediction is always `Regular` @@ -30746,26 +30730,24 @@ fn decode_inter_block8( }); mode_for_tx = mode; let (mi_row, mi_col) = leaf_mi; - for dr in 0..2 { - for dc in 0..2 { - grid.set( - mi_row + dr, - mi_col + dc, - MiInfo { - is_inter: false, - ref_frame: -1, - ref_frame1: None, - mv1: None, - mv: (0, 0), - is_new_mv: false, - size: 2, - size_h: 2, - is_global_mv0: false, - is_global_mv1: false, - }, - ); - } - } + grid.fill_rect( + mi_row, + mi_col, + 2, + 2, + MiInfo { + is_inter: false, + ref_frame: -1, + ref_frame1: NO_REF1, + mv1: (0, 0), + mv: (0, 0), + is_new_mv: false, + size: 2, + size_h: 2, + is_global_mv0: false, + is_global_mv1: false, + }, + ); // lane-leaf8tx r1: the 8x8 leaf's own intra `tx_depth` (libaom // `read_tx_size` -> `read_selected_tx_size`, max depth 1 at @@ -35541,8 +35523,8 @@ mod tests { let neighbour = |mv| MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv: false, size: 1, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 1898fdbf..d645f3d5 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -28,7 +28,7 @@ use crate::frame::frame_obu; use crate::intra::{D67_PRED, DC_PRED, KEY_FRAME_MODES, V_PRED}; use crate::mc; use crate::motion; -use crate::mvstack::{MiGrid, MiInfo, MvStack, find_mv_stack}; +use crate::mvstack::{MiGrid, MiInfo, MvStack, NO_REF1, find_mv_stack}; use crate::obu::temporal_delimiter; use crate::quant::ac_q; use crate::sequence::sequence_header_obu; @@ -3097,8 +3097,8 @@ pub(crate) fn encode_inter_frame( Some(info) => MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: info.mv, is_new_mv: matches!(info.mode, InterMode::NewMv), size: 8, @@ -3113,8 +3113,8 @@ pub(crate) fn encode_inter_frame( None => MiInfo { is_inter: false, ref_frame: -1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: (0, 0), is_new_mv: false, size: 8, @@ -3195,8 +3195,8 @@ pub(crate) fn encode_inter_frame( Some(info) => MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: info.mv, is_new_mv: matches!(info.mode, InterMode::NewMv), size: 4, @@ -3207,8 +3207,8 @@ pub(crate) fn encode_inter_frame( None => MiInfo { is_inter: false, ref_frame: -1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: (0, 0), is_new_mv: false, size: 4, @@ -3255,8 +3255,8 @@ pub(crate) fn encode_inter_frame( Some(info) => MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: info.mv, is_new_mv: matches!( info.mode, @@ -3270,8 +3270,8 @@ pub(crate) fn encode_inter_frame( None => MiInfo { is_inter: false, ref_frame: -1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: (0, 0), is_new_mv: false, size: 2, diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 3a57194b..a6ce6f80 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -155,6 +155,12 @@ pub struct TplArgs<'a> { pub allow_high_precision_mv: bool, } +/// [`MiInfo::ref_frame1`]'s "this unit was not coded compound" value. Slot 1 +/// only ever holds a real reference (`0`..=`7`, `0` being `INTRA_FRAME` for +/// an interintra unit), so `-1` tags the slot -- and with it [`MiInfo::mv1`] +/// -- as absent without an `Option` tag word per grid cell. +pub const NO_REF1: i8 = -1; + /// One 4x4 `mi` unit's motion state, as the encode loop will have filled it /// in by the time it asks for a block's MV stack. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -173,12 +179,12 @@ pub struct MiInfo { /// desync (a `new_mv_ctx` fed by a `newmv_count`/`nearest_match` that /// silently dropped a compound neighbour's vote whenever its match sat /// in this slot). - pub ref_frame1: Option, + pub ref_frame1: i8, /// The unit's motion vector, `(row, col)`, in the spec's 1/8-pel units. pub mv: (i32, i32), /// The unit's second motion vector, matching `ref_frame1` (spec /// `Mvs[1]`) — `Some` only for a compound-coded unit, `None` otherwise. - pub mv1: Option<(i32, i32)>, + pub mv1: (i32, i32), /// Whether this unit's own mode was `NEWMV` (spec's /// `have_newmv_in_inter_mode`, compound modes excluded since this module /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a @@ -272,6 +278,23 @@ impl MiGrid { } } + /// Records one block's whole `bh4` x `bw4` footprint at once. Every + /// caller published a block cell-by-cell through [`Self::set`]; the rows + /// are contiguous, so this fills each of them with one `slice::fill` of + /// the (`Copy`) cell instead, the way the neighbour grids' own span fills + /// do. + pub fn fill_rect(&mut self, row: usize, col: usize, h: usize, w: usize, info: MiInfo) { + if col >= self.cols || row >= self.rows { + return; + } + let w = w.min(self.cols - col); + let cell = Some(info); + for r in row..(row + h).min(self.rows) { + let base = r * self.cols + col; + self.cells[base..base + w].fill(cell); + } + } + /// The unit at `(row, col)`, or `None` when it is outside the grid, hasn't /// been coded, or sits outside the current tile's own bounds ([`Self::set_tile_bounds`]). pub fn get(&self, row: usize, col: usize) -> Option<&MiInfo> { @@ -307,7 +330,7 @@ impl MiGrid { /// One entry of the reference MV stack: a candidate motion vector and the /// total weight the neighbours that agreed on it contributed. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct StackEntry { /// The candidate `(row, col)` motion vector. pub mv: (i32, i32), @@ -315,6 +338,85 @@ pub struct StackEntry { pub weight: u32, } +/// A fixed-capacity, heap-free candidate list. The MV stack is rebuilt for +/// EVERY inter block, so the `Vec` this replaces was one malloc/free pair per +/// block (plus one for `drl_ctx`); capacity is a spec constant +/// ([`MAX_STACK_SIZE`]), so the storage is inline and `Deref` hands every +/// consumer the same slice API a `Vec` did. +#[derive(Clone, Copy, Debug)] +pub struct SmallVec { + len: u8, + buf: [T; N], +} + +impl Default for SmallVec { + fn default() -> Self { + Self { + len: 0, + buf: [T::default(); N], + } + } +} + +impl SmallVec { + /// Appends `v`. Callers gate on [`MAX_STACK_SIZE`] before pushing (the + /// spec drops the overflowing candidate at insertion), so a full list + /// here is a bug, not a stream property. + pub fn push(&mut self, v: T) { + debug_assert!((self.len as usize) < N); + self.buf[self.len as usize] = v; + self.len += 1; + } + + /// `Vec::truncate`. + pub fn truncate(&mut self, n: usize) { + if n < self.len as usize { + self.len = n as u8; + } + } +} + +impl FromIterator for SmallVec { + fn from_iter>(iter: I) -> Self { + let mut out = Self::default(); + for v in iter { + out.push(v); + } + out + } +} + +impl std::ops::Deref for SmallVec { + type Target = [T]; + fn deref(&self) -> &[T] { + &self.buf[..self.len as usize] + } +} + +impl std::ops::DerefMut for SmallVec { + fn deref_mut(&mut self) -> &mut [T] { + &mut self.buf[..self.len as usize] + } +} + +impl PartialEq for SmallVec { + fn eq(&self, other: &Self) -> bool { + **self == **other + } +} + +impl Eq for SmallVec {} + +/// The single-reference MV stack's own list type. +pub type StackVec = SmallVec; + +/// The compound MV stack's own list type. +pub type CompoundStackVec = SmallVec; + +/// `drl_ctx`: one context per adjacent stack pair, so at most +/// `MAX_STACK_SIZE - 1` of them. +pub type DrlVec = SmallVec; + /// The result of [`find_mv_stack`]: the sorted candidate list, the two /// predictors a `NEARESTMV`/`NEARMV` block reads from it, the predictor a /// `NEWMV` residual is coded against, and the symbol contexts the block's @@ -323,7 +425,7 @@ pub struct StackEntry { pub struct MvStack { /// The candidates, highest weight first (ties keep scan order: above, /// then left, then top-right). - pub entries: Vec, + pub entries: StackVec, /// `RefStackMv[0]`, or `(0, 0)` when the stack is empty. pub nearest_mv: (i32, i32), /// `RefStackMv[1]`, or `(0, 0)` when the stack has fewer than two @@ -340,7 +442,7 @@ pub struct MvStack { pub zero_mv_ctx: usize, /// Context for each `drl_mode` symbol between consecutive stack /// entries, `entries.len().saturating_sub(1)` long. - pub drl_ctx: Vec, + pub drl_ctx: DrlVec, } /// Spec 7.10.2.8's threshold a candidate's weight is compared against to @@ -377,7 +479,7 @@ 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). -fn add_candidate(candidates: &mut Vec, mv: (i32, i32), weight: u32) { +fn add_candidate(candidates: &mut StackVec, mv: (i32, i32), weight: u32) { if let Some(entry) = candidates.iter_mut().find(|e| e.mv == mv) { entry.weight += weight; } else if candidates.len() >= MAX_STACK_SIZE { @@ -468,7 +570,7 @@ fn process_single_ref_mv_candidate( candidate: &MiInfo, ref_frame: i8, sign_bias_table: &SignBiasTable, - candidates: &mut Vec, + candidates: &mut StackVec, ) { if !candidate.is_inter { return; @@ -479,8 +581,11 @@ fn process_single_ref_mv_candidate( // never checks `is_global_mv_block`/substitutes `gm_mv_candidates`. let mut slots = [(candidate.ref_frame, candidate.mv); 2]; let mut n_slots = 1; - if let (Some(rf1), Some(mv1)) = (candidate.ref_frame1, candidate.mv1) { - slots[1] = (rf1, mv1); + // `mv1` is meaningful only for a compound neighbour (`ref_frame1 > 0`); + // an interintra one stores `INTRA_FRAME` (0) with no second MV, and used + // to be excluded here by its `mv1` being `None`. + if candidate.ref_frame1 > 0 { + slots[1] = (candidate.ref_frame1, candidate.mv1); n_slots = 2; } for &(rf, mv) in &slots[..n_slots] { @@ -538,7 +643,7 @@ fn scan_row( max_row_offset: isize, ref_frame: i8, gm: &GmMvTable, - candidates: &mut Vec, + candidates: &mut StackVec, newmv_count: &mut u32, processed_rows: &mut usize, ) -> bool { @@ -603,11 +708,11 @@ fn single_ref_match(info: &MiInfo, ref_frame: i8, gm: &GmMvTable) -> Option<(i32 } else { info.mv }) - } else if info.ref_frame1 == Some(ref_frame) { + } else if info.ref_frame1 > 0 && info.ref_frame1 == ref_frame { if info.is_global_mv1 { Some(gm_mv(gm, ref_frame)) } else { - info.mv1 + Some(info.mv1) } } else { None @@ -626,7 +731,7 @@ fn scan_col( max_col_offset: isize, ref_frame: i8, gm: &GmMvTable, - candidates: &mut Vec, + candidates: &mut StackVec, newmv_count: &mut u32, processed_cols: &mut usize, ) -> bool { @@ -762,7 +867,7 @@ fn scan_top_right( bw4: usize, ref_frame: i8, gm: &GmMvTable, - candidates: &mut Vec, + candidates: &mut StackVec, newmv_count: &mut u32, ) -> bool { let Some(row) = mi_row.checked_sub(1) else { @@ -791,7 +896,7 @@ fn scan_corner( mi_col: usize, ref_frame: i8, gm: &GmMvTable, - candidates: &mut Vec, + candidates: &mut StackVec, newmv_count: &mut u32, ) -> bool { let (Some(row), Some(col)) = (mi_row.checked_sub(1), mi_col.checked_sub(1)) else { @@ -919,7 +1024,7 @@ pub fn find_mv_stack_with_sign_bias( ) -> MvStack { // lane-alloc: the stack is capped at MAX_STACK_SIZE entries, so the pushes // below grew (and memcpy'd) a fresh allocation up to 4 times per block. - let mut candidates = Vec::with_capacity(MAX_STACK_SIZE); + let mut candidates = StackVec::default(); let mut newmv_count = 0u32; // libaom's `max_row_offset`/`max_col_offset` (`setup_ref_mv_list`): how @@ -1011,7 +1116,7 @@ pub fn find_mv_stack_with_sign_bias( // av1_find_mv_stack boosts only the entries the immediate scan found — // exactly `candidates` at this point, before the corner probe (below) // can add anything else (mvref_common.c ~line 544-545). - for entry in &mut candidates { + for entry in candidates.iter_mut() { entry.weight += REF_CAT_LEVEL; } @@ -1371,7 +1476,7 @@ pub fn find_mv_stack_with_sign_bias( /// combined weight — spec 7.10.2's compound half of `add_ref_mv_candidate` /// (libaom `mvref_common.c`, the `is_compound` branch of `scan_row_mbmi`/ /// `scan_col_mbmi`/`scan_blk_mbmi`). -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct CompoundStackEntry { /// The candidate's vote for `RefFrame[0]`'s motion vector. pub mv0: (i32, i32), @@ -1386,7 +1491,7 @@ pub struct CompoundStackEntry { #[derive(Clone, Debug, PartialEq, Eq)] pub struct CompoundMvStack { /// The candidates, highest weight first. - pub entries: Vec, + pub entries: CompoundStackVec, /// `RefStackMv[0]` for both refs, or `((0,0),(0,0))` when the stack is /// empty. pub nearest_mv: ((i32, i32), (i32, i32)), @@ -1403,12 +1508,12 @@ pub struct CompoundMvStack { /// Context for the compound `zero_mv` symbol. pub zero_mv_ctx: usize, /// Context for each `drl_mode` symbol between consecutive stack entries. - pub drl_ctx: Vec, + pub drl_ctx: DrlVec, } /// [`add_candidate`], doubled: dedupes on the whole `(mv0, mv1)` pair. fn add_compound_candidate( - candidates: &mut Vec, + candidates: &mut CompoundStackVec, mv0: (i32, i32), mv1: (i32, i32), weight: u32, @@ -1443,7 +1548,7 @@ fn scan_row_compound( max_row_offset: isize, ref_frame: (i8, i8), gm: &GmMvTable, - candidates: &mut Vec, + candidates: &mut CompoundStackVec, newmv_count: &mut u32, processed_rows: &mut usize, ) -> bool { @@ -1479,12 +1584,12 @@ fn scan_row_compound( weight = weight.max(inc as u32); *processed_rows = (inc as isize - row_offset - 1).max(0) as usize; } - if info.is_inter && info.ref_frame == ref_frame.0 && info.ref_frame1 == Some(ref_frame.1) { + if info.is_inter && info.ref_frame == ref_frame.0 && info.ref_frame1 > 0 && info.ref_frame1 == ref_frame.1 { found = true; add_compound_candidate( candidates, if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, - if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1 }, len as u32 * weight, ); *newmv_count += u32::from(info.is_new_mv); @@ -1506,7 +1611,7 @@ fn scan_col_compound( max_col_offset: isize, ref_frame: (i8, i8), gm: &GmMvTable, - candidates: &mut Vec, + candidates: &mut CompoundStackVec, newmv_count: &mut u32, processed_cols: &mut usize, ) -> bool { @@ -1542,12 +1647,12 @@ fn scan_col_compound( weight = weight.max(inc as u32); *processed_cols = (inc as isize - col_offset - 1).max(0) as usize; } - if info.is_inter && info.ref_frame == ref_frame.0 && info.ref_frame1 == Some(ref_frame.1) { + if info.is_inter && info.ref_frame == ref_frame.0 && info.ref_frame1 > 0 && info.ref_frame1 == ref_frame.1 { found = true; add_compound_candidate( candidates, if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, - if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1 }, len as u32 * weight, ); *newmv_count += u32::from(info.is_new_mv); @@ -1566,7 +1671,7 @@ fn scan_top_right_compound( bw4: usize, ref_frame: (i8, i8), gm: &GmMvTable, - candidates: &mut Vec, + candidates: &mut CompoundStackVec, newmv_count: &mut u32, ) -> bool { let Some(row) = mi_row.checked_sub(1) else { @@ -1576,12 +1681,12 @@ fn scan_top_right_compound( if let Some(info) = grid.get(row, col) && info.is_inter && info.ref_frame == ref_frame.0 - && info.ref_frame1 == Some(ref_frame.1) + && info.ref_frame1 > 0 && info.ref_frame1 == ref_frame.1 { add_compound_candidate( candidates, if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, - if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1 }, CORNER_WEIGHT, ); *newmv_count += u32::from(info.is_new_mv); @@ -1597,7 +1702,7 @@ fn scan_corner_compound( mi_col: usize, ref_frame: (i8, i8), gm: &GmMvTable, - candidates: &mut Vec, + candidates: &mut CompoundStackVec, newmv_count: &mut u32, ) -> bool { let (Some(row), Some(col)) = (mi_row.checked_sub(1), mi_col.checked_sub(1)) else { @@ -1606,12 +1711,12 @@ fn scan_corner_compound( if let Some(info) = grid.get(row, col) && info.is_inter && info.ref_frame == ref_frame.0 - && info.ref_frame1 == Some(ref_frame.1) + && info.ref_frame1 > 0 && info.ref_frame1 == ref_frame.1 { add_compound_candidate( candidates, if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, - if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1 }, CORNER_WEIGHT, ); *newmv_count += u32::from(info.is_new_mv); @@ -1653,13 +1758,10 @@ fn process_compound_ref_mv_candidate( } let slots = [ Some((candidate.ref_frame, candidate.mv)), - candidate - .ref_frame1 - // An interintra neighbour stores INTRA_FRAME (0) here; libaom's - // ref-diff scan requires `can_rf > INTRA_FRAME` - // (process_compound_ref_mv_candidate), so it contributes nothing. - .filter(|&rf1| rf1 > 0) - .map(|rf1| (rf1, candidate.mv1.unwrap_or((0, 0)))), + // An interintra neighbour stores INTRA_FRAME (0) here; libaom's + // ref-diff scan requires `can_rf > INTRA_FRAME` + // (process_compound_ref_mv_candidate), so it contributes nothing. + (candidate.ref_frame1 > 0).then_some((candidate.ref_frame1, candidate.mv1)), ]; for (candidate_ref, candidate_mv) in slots.into_iter().flatten() { for (i, side_ref) in [ref_frame.0, ref_frame.1].into_iter().enumerate() { @@ -1748,7 +1850,7 @@ pub fn find_mv_stack_compound( gm: &GmMvTable, tpl: Option, ) -> CompoundMvStack { - let mut candidates: Vec = Vec::with_capacity(MAX_STACK_SIZE); + let mut candidates = CompoundStackVec::default(); let mut newmv_count = 0u32; let row_adj = bh4 < 2 && mi_row % 2 == 1; @@ -1828,7 +1930,7 @@ pub fn find_mv_stack_compound( let row_matched = found_above || found_top_right; let nearest_match = usize::from(row_matched) + usize::from(found_left); - for entry in &mut candidates { + for entry in candidates.iter_mut() { entry.weight += REF_CAT_LEVEL; } @@ -2432,12 +2534,28 @@ mod tests { use super::*; + + /// lane-mvstack2: the mi grid is one cell per 4x4 unit of the frame and + /// `find_mv_stack` walks it for every inter block, so the cell's SIZE is + /// the scan's memory traffic. `ref_frame1`/`mv1` are validity-tagged by + /// `ref_frame1 == NO_REF1` rather than by two `Option` tags (32 bytes -> + /// 24), and `is_inter` keeps a niche so the grid's `Option` costs + /// no extra word. + #[test] + fn mi_info_cell_stays_compact() { + assert_eq!(std::mem::size_of::(), 24); + assert_eq!( + std::mem::size_of::>(), + std::mem::size_of::() + ); + } + fn inter(mv: (i32, i32)) -> MiInfo { MiInfo { is_inter: true, ref_frame: 1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv: false, // 1: smaller than every `bw4`/`bh4` these small-grid tests use, @@ -2595,8 +2713,8 @@ mod tests { MiInfo { is_inter: true, ref_frame: 1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: (4, 4), is_new_mv: true, size: 1, @@ -2635,7 +2753,7 @@ mod tests { assert_eq!(stack.entries.len(), 2); assert_eq!(stack.entries[0].mv, (4, 4)); assert_eq!(stack.entries[1].mv, (8, 8)); - assert_eq!(stack.drl_ctx, vec![1]); + assert_eq!(&stack.drl_ctx[..], &[1]); } #[test] @@ -2678,8 +2796,8 @@ mod tests { MiInfo { is_inter: true, ref_frame: 1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv: false, size: 8, @@ -2710,8 +2828,8 @@ mod tests { MiInfo { is_inter: false, ref_frame: -1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: (0, 0), is_new_mv: false, size: 8, @@ -2729,8 +2847,8 @@ mod tests { MiInfo { is_inter: true, ref_frame: 1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv: false, size: 8, @@ -2969,9 +3087,9 @@ mod tests { MiInfo { is_inter: true, ref_frame: COMP_PAIR.0, - ref_frame1: Some(COMP_PAIR.1), + ref_frame1: COMP_PAIR.1, mv: mv0, - mv1: Some(mv1), + mv1, is_new_mv: false, size: 1, size_h: 1, @@ -2992,9 +3110,9 @@ mod tests { MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: Some(GOLDEN_FRAME), + ref_frame1: GOLDEN_FRAME, mv: (9, 9), - mv1: Some((9, 9)), + mv1: (9, 9), is_new_mv: false, size: 1, size_h: 1, @@ -3095,9 +3213,9 @@ mod tests { let candidate = MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, + ref_frame1: NO_REF1, mv: (5, 5), - mv1: None, + mv1: (0, 0), is_new_mv: false, size: 1, size_h: 1, @@ -3124,9 +3242,9 @@ mod tests { MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, + ref_frame1: NO_REF1, mv: (7, 7), - mv1: None, + mv1: (0, 0), is_new_mv: false, size: 8, size_h: 8, @@ -3140,9 +3258,9 @@ mod tests { MiInfo { is_inter: true, ref_frame: ALTREF_FRAME, - ref_frame1: None, + ref_frame1: NO_REF1, mv: (-3, -3), - mv1: None, + mv1: (0, 0), is_new_mv: false, size: 8, size_h: 8, @@ -3197,7 +3315,7 @@ mod tests { /// candidate would survive and evict a scanned one. #[test] fn the_ninth_distinct_candidate_is_dropped_at_insertion_not_after_sorting() { - let mut candidates = Vec::new(); + let mut candidates = StackVec::default(); for i in 0..MAX_STACK_SIZE as i32 { add_candidate(&mut candidates, (i, i), 2); } @@ -3211,7 +3329,7 @@ mod tests { add_candidate(&mut candidates, (0, 0), 6); assert_eq!(candidates[0].weight, 8); - let mut comp = Vec::new(); + let mut comp = CompoundStackVec::default(); for i in 0..MAX_STACK_SIZE as i32 { add_compound_candidate(&mut comp, (i, i), (i, -i), 2); } diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 63ea3ecc..7b6a05fa 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -16,7 +16,7 @@ use ec_core::{Error, Result}; use crate::cdf; use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; use crate::msac::SymbolEncoder; -use crate::mvstack::{MiGrid, MiInfo, find_mv_stack, single_ref_ctx}; +use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, single_ref_ctx}; /// round-4 av1-truesize debugging aid: prints `msg()` to stderr when the /// `EC_RNG` environment variable is set, mirroring the `EC_PART`/`EC_TOK` @@ -2434,8 +2434,8 @@ pub fn sb_coeff_inter_frame_tile( MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv, size: 8, @@ -2455,8 +2455,8 @@ pub fn sb_coeff_inter_frame_tile( MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv, size: 8, @@ -2487,8 +2487,8 @@ pub fn sb_coeff_inter_frame_tile( MiInfo { is_inter: false, ref_frame: -1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: (0, 0), is_new_mv: false, size: 8, @@ -2630,8 +2630,8 @@ fn write_inter_frame_leaf( MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv, size: SUB_MI as usize as u8, @@ -2662,8 +2662,8 @@ fn write_inter_frame_leaf( MiInfo { is_inter: false, ref_frame: -1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: (0, 0), is_new_mv: false, size: SUB_MI as usize as u8, @@ -2828,8 +2828,8 @@ fn write_inter_frame_leaf8( MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv, size: 2, @@ -2860,8 +2860,8 @@ fn write_inter_frame_leaf8( MiInfo { is_inter: false, ref_frame: -1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv: (0, 0), is_new_mv: false, size: 2, @@ -5138,8 +5138,8 @@ mod tests { MiInfo { is_inter: true, ref_frame: 1, - ref_frame1: None, - mv1: None, + ref_frame1: NO_REF1, + mv1: (0, 0), mv, is_new_mv, size: 8,