//! Temporal MV projection (AV1 spec 7.9, `use_ref_frame_mvs`): projects a //! previously decoded frame's stored motion vectors into the current //! frame's own 8x8 motion field, ported from libaom's //! `av1_setup_motion_field`/`motion_field_projection` //! (`av1/common/mvref_common.c`) and the projection math `add_tpl_ref_mv` //! (same file) applies a second time, per query block, against the block's //! own single reference frame. use crate::mvstack::{ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST_FRAME, LAST2_FRAME}; /// One 8x8 cell's saved motion state, as libaom's `av1_copy_frame_mvs` /// leaves it after decoding the frame that owns this grid: the coded MV and /// the single reference frame (`LAST_FRAME..=ALTREF_FRAME`, 1..=7) it /// pointed to. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct SavedMv { pub mv: (i32, i32), pub ref_frame: i8, } /// A decoded inter frame's whole motion field (spec `MotionFieldMvs`... no /// — libaom's `cur_frame->mvs`), one [`SavedMv`] per 8x8 unit, plus the /// order-hint bookkeeping [`setup_motion_field`] needs to project it into a /// later frame: this frame's own `OrderHint` and the `OrderHint` of each of /// the 7 references *it* was coded against (`SavedOrderHints`/ /// `ref_order_hints`). #[derive(Clone, Debug)] pub struct MotionField { cols: usize, rows: usize, cells: Vec>, pub order_hint: u32, pub ref_order_hints: [u32; 7], /// libaom `motion_field_projection`'s own first test: a start frame whose /// `frame_type` is `KEY_FRAME`/`INTRA_ONLY_FRAME` is never projected at /// all (`return 0`), so it also never spends a `ref_stamp` slot. pub is_intra: bool, } impl MotionField { /// A field sized for an `mi_cols` by `mi_rows` frame, all cells /// initially unset (spec 7.9's own reset to `-1`/invalid before any /// block writes into it). pub fn new( mi_cols: usize, mi_rows: usize, order_hint: u32, ref_order_hints: [u32; 7], is_intra: bool, ) -> Self { let cols = mi_cols.div_ceil(2); let rows = mi_rows.div_ceil(2); Self { cols, rows, cells: vec![None; cols * rows], order_hint, ref_order_hints, is_intra, } } /// Records `saved` at the 8x8 cell containing 4x4 unit `(mi_row, /// mi_col)` — every `MiInfo` inside one 8x8 cell shares a single /// motion-field entry, matching `av1_copy_frame_mvs`'s own half-resolution /// write. /// `saved == None` is `av1_copy_frame_mvs`'s own leading clear /// (`mv->ref_frame = NONE_FRAME; mv->mv.as_int = 0;`), which every block /// of an inter frame performs -- an intra (or nothing-to-store) block /// sharing an 8x8 cell with an earlier inter one wipes that cell. #[allow(dead_code)] // lane-mf: the frame build now fills cell rows directly; kept for the module's tests pub fn set(&mut self, mi_row: usize, mi_col: usize, saved: Option) { let (r, c) = (mi_row / 2, mi_col / 2); if r < self.rows && c < self.cols { self.cells[r * self.cols + c] = saved; } } /// lane-mf: the cell grid itself (`cols`, `rows`, cells in row-major /// order), so [`crate::decode::build_motion_field`] can fill disjoint /// row bands of it in parallel. Cell row `r` owns `cells[r * cols..][..cols]`. pub(crate) fn cells_mut(&mut self) -> (usize, usize, &mut [Option]) { (self.cols, self.rows, &mut self.cells) } } /// lane-mvstack3: one 8x8 temporal-MV cell -- the projected `(mv, /// ref_frame_offset)` pair. `motion_field_projection` writes a cell only /// with a STRICTLY POSITIVE `ref_frame_offset` (its own `pos_valid` test), /// so `0` is exactly libaom's own "this `tpl_mvs` entry was never projected" /// marker and the `Option` tag this used to carry was redundant: it cost 8 /// bytes per cell (20 vs 12) over a field that is 2.6 MB at 4K, read up to /// 19 times per inter block. pub(crate) type TplCell = ((i32, i32), i32); /// An unprojected [`TplCell`]. const TPL_EMPTY: TplCell = ((0, 0), 0); /// The current frame's projected temporal motion field (spec 7.9.2's /// `MotionFieldMvs`): one projected [`TplCell`] per 8x8 cell, its /// `ref_frame_offset` left at `0` where no source candidate landed /// (libaom's `INVALID_MV`). #[derive(Clone, Debug)] pub struct TplField { cols: usize, rows: usize, cells: Vec, } impl TplField { /// lane-mergefix r5 rung (`EC_TPL`): how many 8x8 cells the projection /// actually filled -- an empty field means every temporal candidate is /// silently absent while the entropy stream still matches. pub fn filled_cells(&self) -> usize { self.cells.iter().filter(|c| c.1 != 0).count() } } /// spec `get_relative_dist` (5.9.3): `a - b` wrapped into the signed range /// order hints roll over in at `order_hint_bits` width. pub fn get_relative_dist(order_hint_bits: u32, a: u32, b: u32) -> i32 { if order_hint_bits == 0 { return 0; } let diff = a as i32 - b as i32; let m = 1i32 << (order_hint_bits - 1); (diff & (m - 1)) - (diff & m) } /// spec `FRAME_OFFSET_BITS`-derived clamp (libaom `MAX_FRAME_DISTANCE`). const MAX_FRAME_DISTANCE: i32 = 31; /// libaom's `div_mult`: precomputed `16384 / den` (`av1_get_mv_projection`'s /// own comment: "all the values are strictly under 14 bits"). const DIV_MULT: [i64; 32] = [ 0, 16384, 8192, 5461, 4096, 3276, 2730, 2340, 2048, 1820, 1638, 1489, 1365, 1260, 1170, 1092, 1024, 963, 910, 862, 819, 780, 744, 712, 682, 655, 630, 606, 585, 564, 546, 528, ]; fn round_pow2_signed(value: i64, n: u32) -> i32 { let half = 1i64 << (n - 1); (if value < 0 { -((-value + half) >> n) } else { (value + half) >> n }) as i32 } /// `av1_get_mv_projection` (spec 7.9.3): scales `mv` by the ratio of two /// frame-distance offsets. fn get_mv_projection(mv: (i32, i32), num: i32, den: i32) -> (i32, i32) { let den = den.min(MAX_FRAME_DISTANCE); let num = if num > 0 { num.min(MAX_FRAME_DISTANCE) } else { num.max(-MAX_FRAME_DISTANCE) }; let mult = DIV_MULT[den as usize]; let scale = |v: i32| round_pow2_signed(v as i64 * num as i64 * mult, 14).clamp(-16383, 16383); (scale(mv.0), scale(mv.1)) } /// `lower_mv_precision` (`mvref_common.h`), both halves. lane-dgolomb: the /// `is_integer` half used to be dropped as unreachable on the claim that /// `allow_screen_content_tools` is refused at the stream level -- it is not, /// and libaom turns screen-content tools on for dark, smooth FILM (its /// `av1_set_screen_content_options` counts near-flat blocks), so a real /// libaom stream carries `force_integer_mv == 1` on an inter frame and every /// temporal MV candidate has to be rounded to full pel. Without it the /// candidates were 1/8-pel values libaom never produces: a whole-frame /// prediction mismatch behind a clean parse (class /// `branch-dropped-as-unreachable`). pub fn lower_mv_precision( mv: (i32, i32), allow_high_precision_mv: bool, force_integer_mv: bool, ) -> (i32, i32) { if force_integer_mv { // `integer_mv_precision`: drop to the nearest full pel, ties away // from zero (`abs(mod) > 4`). let round = |v: i32| { let m = v % 8; match m { 0 => v, _ if m > 4 => v - m + 8, _ if m < -4 => v - m - 8, _ => v - m, } }; let out = (round(mv.0), round(mv.1)); if out != mv && crate::envflags::env_flag!("EC_TRACE_TPL") { eprintln!("EC_INTMV rounded {mv:?} -> {out:?}"); } return out; } if allow_high_precision_mv { return mv; } let round = |v: i32| { if v & 1 != 0 { v + if v > 0 { -1 } else { 1 } } else { v } }; (round(mv.0), round(mv.1)) } /// `get_block_position` (spec 7.9.2): the 8x8 cell a projected `mv` lands on /// starting from `(blk_row, blk_col)` (already in 8x8 units), or `None` when /// it falls outside the frame or outside the projection's own bound (a /// 64x64-superblock-aligned base block, spec's `MAX_OFFSET_WIDTH`/ /// `MAX_OFFSET_HEIGHT`). fn get_block_position( mvs_rows: usize, mvs_cols: usize, blk_row: usize, blk_col: usize, mv: (i32, i32), sign_bias: bool, ) -> Option<(usize, usize)> { let base_blk_row = (blk_row >> 3) << 3; let base_blk_col = (blk_col >> 3) << 3; let row_offset = if mv.0 >= 0 { mv.0 >> 6 } else { -((-mv.0) >> 6) }; let col_offset = if mv.1 >= 0 { mv.1 >> 6 } else { -((-mv.1) >> 6) }; let row = if sign_bias { blk_row as i64 - row_offset as i64 } else { blk_row as i64 + row_offset as i64 }; let col = if sign_bias { blk_col as i64 - col_offset as i64 } else { blk_col as i64 + col_offset as i64 }; if row < 0 || row >= mvs_rows as i64 || col < 0 || col >= mvs_cols as i64 { return None; } if row < base_blk_row as i64 || row >= base_blk_row as i64 + 8 || col < base_blk_col as i64 - 8 || col >= base_blk_col as i64 + 16 { return None; } Some((row as usize, col as usize)) } /// `motion_field_projection` (spec 7.9.2): projects `start`'s stored MVs /// into `tpl`, scaled by the OrderHint distance from `start` to the current /// frame. `dir` mirrors libaom's own parameter: `2` for a backward-looking /// source (`LAST_FRAME`/`LAST2_FRAME`, offset negated and `sign_bias` /// flipped), `0` for a forward one (`BWDREF_FRAME`/`ALTREF2_FRAME`/ /// `ALTREF_FRAME`). Returns whether the projection ran at all (`start`'s /// frame size has to match the current frame's — always true for this /// decoder's fixed-size streams, but ported anyway). #[allow(clippy::too_many_arguments)] fn motion_field_projection( start: &MotionField, cur_order_hint: u32, order_hint_bits: u32, mi_rows: usize, mi_cols: usize, dir: u8, tpl_cells: &mut [TplCell], ) -> bool { // libaom's own first two `return 0`s, in order: a key/intra-only start // frame is never projected (and so never spends a `ref_stamp` slot -- // returning `true` here made a forward key frame, `--enable-fwd-kf=1`, // consume the slot ALTREF/LAST2 should have had), then the frame-size // test. if start.is_intra { return false; } let mvs_rows = mi_rows.div_ceil(2); let mvs_cols = mi_cols.div_ceil(2); if start.rows != mvs_rows || start.cols != mvs_cols { return false; } let start_order_hint = start.order_hint; let mut start_to_current = get_relative_dist(order_hint_bits, start_order_hint, cur_order_hint); if dir == 2 { start_to_current = -start_to_current; } let ref_offset: [i32; 8] = std::array::from_fn(|rf| { if rf == 0 { 0 } else { get_relative_dist( order_hint_bits, start_order_hint, start.ref_order_hints[rf - 1], ) } }); // lane-mvstack3: two of libaom's three `pos_valid` terms do not depend on // the cell at all -- `start_to_current` is one value for the whole field, // and a reference's own offset is one value per `ref_frame` -- so fold // them into a per-ref table read once instead of re-deriving them for // every 8x8 cell (a 4K field is 129600 of them, per direction, per // frame). Same terms, same order of rejection. if start_to_current.abs() > MAX_FRAME_DISTANCE { return true; } let ref_valid: [bool; 8] = std::array::from_fn(|rf| ref_offset[rf] > 0 && ref_offset[rf] <= MAX_FRAME_DISTANCE); let cols = start.cols; for row8 in 0..start.rows { // The row is contiguous, so walk it as a slice: `MotionField::get`'s // per-cell bounds test is the same test the loop bounds already make. let row = &start.cells[row8 * cols..][..cols]; for (col8, cell) in row.iter().enumerate() { let Some(saved) = *cell else { continue; }; if saved.ref_frame <= 0 { continue; } let ref_frame_offset = ref_offset[saved.ref_frame as usize]; if !ref_valid[saved.ref_frame as usize] { continue; } let projected = get_mv_projection(saved.mv, start_to_current, ref_frame_offset); if let Some((r, c)) = get_block_position(mvs_rows, mvs_cols, row8, col8, projected, dir >> 1 == 1) { tpl_cells[r * mvs_cols + c] = (saved.mv, ref_frame_offset); } } } true } /// `av1_setup_motion_field` (spec 7.9.2's own driver): scans up to /// [`MFMV_STACK_SIZE`]`= 3` reference-frame slots in libaom's fixed order /// (`LAST_FRAME`, then `BWDREF_FRAME`/`ALTREF2_FRAME`/`ALTREF_FRAME` when /// each is display-order-forward of the current frame, then `LAST2_FRAME` /// once the stack still has room) and projects each into the returned /// [`TplField`]. /// /// `slots` is the DPB's 8 reference-picture slots' saved [`MotionField`]s /// (a `None` slot, or one still holding an intra frame's — never /// constructed, so also `None` — motion field, contributes nothing); /// `ref_frame_idx` is the current frame header's own `ref_frame_idx` /// (`LAST_FRAME..=ALTREF_FRAME`, indexed `[i]` for `LAST_FRAME + i`). pub fn setup_motion_field>( slots: &[Option; 8], ref_frame_idx: [u8; 7], cur_order_hint: u32, order_hint_bits: u32, mi_rows: usize, mi_cols: usize, ) -> TplField { let cols = mi_cols.div_ceil(2); let rows = mi_rows.div_ceil(2); let mut cells = vec![TPL_EMPTY; cols * rows]; // `av1_setup_motion_field`'s own head: `if (!enable_order_hint) return;` // leaves every `tpl_mvs` cell INVALID. if order_hint_bits == 0 { return TplField { cols, rows, cells }; } let slot_of = // lane-thread3: `M` is an owned `MotionField` on the single-threaded // path and a `&MotionField` borrowed out of a resolved reference-slot // promise on a worker; the projection reads the same field either way. |ref_frame: i8| { slots[ref_frame_idx[(ref_frame - LAST_FRAME) as usize] as usize] .as_ref() .map(M::borrow) }; let order_hint_of = |ref_frame: i8| slot_of(ref_frame).map_or(0, |m| m.order_hint); let mut ref_stamp = 2i32; // MFMV_STACK_SIZE - 1 if let Some(last) = slot_of(LAST_FRAME) { let alt_of_lst = last.ref_order_hints[(ALTREF_FRAME - LAST_FRAME) as usize]; let is_lst_overlay = alt_of_lst == order_hint_of(GOLDEN_FRAME); if !is_lst_overlay { motion_field_projection( last, cur_order_hint, order_hint_bits, mi_rows, mi_cols, 2, &mut cells, ); } ref_stamp -= 1; } if get_relative_dist(order_hint_bits, order_hint_of(BWDREF_FRAME), cur_order_hint) > 0 && let Some(bwd) = slot_of(BWDREF_FRAME) && motion_field_projection( bwd, cur_order_hint, order_hint_bits, mi_rows, mi_cols, 0, &mut cells, ) { ref_stamp -= 1; } if get_relative_dist( order_hint_bits, order_hint_of(crate::mvstack::ALTREF2_FRAME), cur_order_hint, ) > 0 && let Some(alt2) = slot_of(crate::mvstack::ALTREF2_FRAME) && motion_field_projection( alt2, cur_order_hint, order_hint_bits, mi_rows, mi_cols, 0, &mut cells, ) { ref_stamp -= 1; } if ref_stamp >= 0 && get_relative_dist(order_hint_bits, order_hint_of(ALTREF_FRAME), cur_order_hint) > 0 && let Some(alt) = slot_of(ALTREF_FRAME) && motion_field_projection( alt, cur_order_hint, order_hint_bits, mi_rows, mi_cols, 0, &mut cells, ) { ref_stamp -= 1; } if ref_stamp >= 0 && let Some(last2) = slot_of(LAST2_FRAME) { motion_field_projection( last2, cur_order_hint, order_hint_bits, mi_rows, mi_cols, 2, &mut cells, ); } if crate::envflags::env_flag!("EC_TRACE_TPL") { eprintln!("EC_TPL_FIELD oh={cur_order_hint} rows={rows} cols={cols}"); for r in 0..rows { let row: String = (0..cols) .map(|c| if cells[r * cols + c].1 != 0 { '#' } else { '.' }) .collect(); eprintln!("EC_TPL_FIELD r{r} {row}"); } } TplField { cols, rows, cells } } /// The result [`add_tpl_ref_mv`] folds into a query block's MV stack: the /// projected candidate MV (already scaled to the block's own reference /// frame and MV-precision-lowered) plus whether the very first (`blk_row == /// 0 && blk_col == 0`) sample differs enough from the zero vector to raise /// `GLOBALMV_OFFSET` (spec 7.10.2.8 — this decoder only ever reaches /// `IDENTITY` global motion, so the spec's `gm_get_motion_vector` compare /// collapses to "far from `(0, 0)`"). pub struct TplCandidate { pub mv: (i32, i32), } /// `add_tpl_ref_mv`'s single-reference half (spec 7.10.2.8, libaom /// `mvref_common.c`): probes one 8x8-unit offset `(blk_row, blk_col)` from /// `(mi_row, mi_col)` in [`TplField`] and, if it landed a candidate, /// projects it a second time — this time scaled to `cur_offset_0`, the /// distance from the current frame to the query block's own single /// reference frame, rather than `start_to_current_frame_offset` from the /// first (storage-time) projection. // Signature mirrors the spec's per-call inputs; a parameter struct would only move them. #[allow(clippy::too_many_arguments)] pub fn add_tpl_ref_mv( tpl: &TplField, mi_row: usize, mi_col: usize, blk_row: isize, blk_col: isize, cur_offset_0: i32, allow_high_precision_mv: bool, force_integer_mv: bool, ) -> Option { let probe = tpl.probe(mi_row, mi_col).cell(blk_row, blk_col); if crate::envflags::env_flag!("EC_TRACE_TPL") { match probe { None => eprintln!( "EC_TPL mi_row={mi_row} mi_col={mi_col} blk=({blk_row},{blk_col}) INVALID" ), Some((m, rfo)) => eprintln!( "EC_TPL mi_row={mi_row} mi_col={mi_col} blk=({blk_row},{blk_col}) mfmv0=({},{}) rfo={rfo}", m.0, m.1 ), } } let (fwd_mv, ref_frame_offset) = probe?; Some(TplCandidate { mv: project_tpl_mv( fwd_mv, cur_offset_0, ref_frame_offset, allow_high_precision_mv, force_integer_mv, ), }) } /// The projection half of [`add_tpl_ref_mv`], split out so a caller holding a /// [`TplProbe`] cell can project it to more than one reference offset without /// reading the field twice (lane-tpl: the compound stack does exactly that). #[inline] pub fn project_tpl_mv( fwd_mv: (i32, i32), cur_offset: i32, ref_frame_offset: i32, allow_high_precision_mv: bool, force_integer_mv: bool, ) -> (i32, i32) { let projected = get_mv_projection(fwd_mv, cur_offset, ref_frame_offset); lower_mv_precision(projected, allow_high_precision_mv, force_integer_mv) } /// One query block's view of [`TplField`] (lane-tpl). `add_tpl_ref_mv`'s /// parity derivation and row base are the same for every `(blk_row, /// blk_col)` the mv stack probes, so they are hoisted here and the per-sample /// cost drops to an add, a shift and the bounds check: `row8 = mi_row + /// pos_row` with `pos_row = blk_row + (mi_row even)` means the cell index is /// `(mi_row + 1 - (mi_row & 1) + blk_row) >> 1`, and an arithmetic shift /// keeps the spec's `row8 < 0` rejection exact (`row8 < 0` iff the shifted /// index is negative). pub struct TplProbe<'a> { cells: &'a [TplCell], cols: isize, rows: isize, base_row: isize, base_col: isize, } impl TplField { /// [`TplProbe`] for the query block at `(mi_row, mi_col)`. #[inline] pub fn probe(&self, mi_row: usize, mi_col: usize) -> TplProbe<'_> { TplProbe { cells: &self.cells, cols: self.cols as isize, rows: self.rows as isize, base_row: mi_row as isize + 1 - (mi_row & 1) as isize, base_col: mi_col as isize + 1 - (mi_col & 1) as isize, } } } impl TplProbe<'_> { /// The stored `(mv, ref_frame_offset)` at the 8x8-unit offset /// `(blk_row, blk_col)` from the query block, or `None` where the spec's /// probe falls outside the field. #[inline] pub fn cell(&self, blk_row: isize, blk_col: isize) -> Option<((i32, i32), i32)> { let row = (self.base_row + blk_row) >> 1; let col = (self.base_col + blk_col) >> 1; if row < 0 || col < 0 || row >= self.rows || col >= self.cols { return None; } let cell = self.cells[(row * self.cols + col) as usize]; if cell.1 == 0 { None } else { Some(cell) } } } #[cfg(test)] mod tests { use super::*; /// lane-tpl: `TplProbe::cell` replaced `add_tpl_ref_mv`'s per-sample /// parity derivation with one hoisted base; this pins it against that /// derivation, spelled out, over every parity and every offset the mv /// stack (and its extension samples) can probe with. #[test] fn probe_cell_matches_the_spec_parity_derivation() { let (rows, cols) = (5usize, 6usize); let cells: Vec = (0..rows * cols) .map(|i| ((i as i32, -(i as i32)), 1)) .collect(); let tpl = TplField { cols, rows, cells }; for mi_row in 0..2 * rows { for mi_col in 0..2 * cols { let probe = tpl.probe(mi_row, mi_col); for blk_row in -4isize..=16 { for blk_col in -4isize..=16 { // the spec's own derivation (7.10.2.8) let pos_row = if mi_row & 1 != 0 { blk_row } else { blk_row + 1 }; let pos_col = if mi_col & 1 != 0 { blk_col } else { blk_col + 1 }; let row8 = mi_row as isize + pos_row; let col8 = mi_col as isize + pos_col; let want = if row8 < 0 || col8 < 0 { None } else { let (r, c) = (row8 as usize / 2, col8 as usize / 2); if r < rows && c < cols { Some(tpl.cells[r * cols + c]) } else { None } }; assert_eq!( probe.cell(blk_row, blk_col), want, "mi=({mi_row},{mi_col}) blk=({blk_row},{blk_col})" ); } } } } } /// lane-mvstack3: [`TplCell`] dropped its `Option` tag for the /// `ref_frame_offset == 0` sentinel `motion_field_projection` already /// guarantees (it only ever stores a strictly positive offset). This pins /// that sentinel: an unprojected cell must still read as `None` through /// [`TplProbe::cell`], and a projected one with a zero MV must not. #[test] fn an_unprojected_tpl_cell_reads_as_none_but_a_zero_mv_one_does_not() { let (rows, cols) = (2usize, 2usize); let cells: Vec = vec![TPL_EMPTY, ((0, 0), 3), ((7, -7), 1), TPL_EMPTY]; let tpl = TplField { cols, rows, cells }; // mi=(0,0) probes cell (0,0) at blk (-1,-1) and (0,1) at blk (-1,1). let probe = tpl.probe(0, 0); assert_eq!(probe.cell(-1, -1), None, "unprojected cell"); assert_eq!(probe.cell(-1, 1), Some(((0, 0), 3)), "projected zero mv"); assert_eq!(probe.cell(1, -1), Some(((7, -7), 1))); assert_eq!(probe.cell(1, 1), None, "unprojected cell"); assert_eq!(tpl.filled_cells(), 2); } #[test] fn relative_dist_wraps_at_order_hint_bits() { // 7-bit order hints (aomenc's usual default): 1 - 126 should read as // +3 (wrapping backward past 0), not -125. assert_eq!(get_relative_dist(7, 1, 126), 3); assert_eq!(get_relative_dist(7, 10, 5), 5); } #[test] fn mv_projection_scales_by_the_distance_ratio() { // A MV recorded across a 2-frame gap, projected across a 1-frame // gap, should halve (libaom's own div_mult table is exact for small // integer ratios). let (row, col) = get_mv_projection((16, -32), 1, 2); assert_eq!((row, col), (8, -16)); // Same-distance projection is the identity. assert_eq!(get_mv_projection((7, -3), 4, 4), (7, -3)); } #[test] fn setup_motion_field_projects_last_frame_forward() { // A slot-0 inter frame (order_hint 2) that coded one 8x8 cell's MV // against its own LAST_FRAME (order_hint 1). The current frame // (order_hint 4) names that same slot as *its* LAST_FRAME. let mut mf = MotionField::new(16, 16, 2, [1, 0, 0, 0, 0, 0, 0], false); mf.set( 0, 0, Some(SavedMv { mv: (-64, 0), ref_frame: LAST_FRAME, }), ); let slots: [Option; 8] = std::array::from_fn(|i| if i == 0 { Some(mf.clone()) } else { None }); let ref_frame_idx = [0u8; 7]; let tpl = setup_motion_field(&slots, ref_frame_idx, 4, 7, 16, 16); // LAST_FRAME's own dir=2 projection: start_to_current = -get_relative_dist(2,4) = 2, // ref_frame_offset = get_relative_dist(2,1) = 1, scaling (-64,0) by // 2/1 -> (-128,0); get_block_position's sign_bias (dir=2) subtracts // the offset, landing at 8x8 cell (2, 0). let cand = add_tpl_ref_mv( &tpl, 4, 0, 0isize, 0isize, get_relative_dist(7, 4, 2), true, false, ); assert!(cand.is_some(), "expected a projected temporal candidate"); } /// lane-dgolomb: `lower_mv_precision`'s `force_integer_mv` half, which /// this decoder used to drop as unreachable. The values are the ones a /// real libaom stream (film B, `-cpu-used 6 -crf 35`, the one inter frame /// whose header carries `force_integer_mv == 1`) produces at its first /// 128x128 block: the same 16 temporal probes, projected and then rounded /// to full pel, are what put `(16,0)`/`(16,-8)`/`(8,-40)` in libaom's own /// traced mv stack where the eighth-pel values were `(14,-4)`/`(12,-2)`/ /// `(12,-38)`/`(16,-4)`. #[test] fn force_integer_mv_rounds_a_projected_candidate_to_full_pel() { // (mfmv0, ref_frame_offset) -> (eighth-pel value, full-pel value), // all at cur_offset_0 == 1. for (mv, rfo, eighth, integer) in [ ((74, -20), 5, (14, -4), (16, 0)), ((74, -24), 5, (14, -4), (16, -8)), ((82, -24), 5, (16, -4), (16, -8)), ((66, -16), 5, (12, -2), (16, 0)), ((24, -76), 2, (12, -38), (8, -40)), ] { assert_eq!(project_tpl_mv(mv, 1, rfo, false, false), eighth, "{mv:?}"); assert_eq!(project_tpl_mv(mv, 1, rfo, false, true), integer, "{mv:?}"); } // `integer_mv_precision`'s tie rule is away from zero on |mod| > 4 // only, and it is signed-symmetric. assert_eq!(lower_mv_precision((4, -4), false, true), (0, 0)); assert_eq!(lower_mv_precision((5, -5), false, true), (8, -8)); assert_eq!(lower_mv_precision((-12, 8), false, true), (-8, 8)); } }