From 916263c6cea628c7fbcd41d87ea576669efeff6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 28 Aug 2026 05:28:44 +0300 Subject: [PATCH] wip(av1): temporal MV projection math (spec 7.9/7.10.2.8), not yet wired New crate::motion_field module: MotionField (per-frame saved 8x8 MV grid + order-hint bookkeeping), setup_motion_field (av1_setup_motion_field's LAST/BWDREF/ALTREF2/ALTREF/LAST2 selection + motion_field_projection's distance-scaled projection into a TplField), and add_tpl_ref_mv (the per-query-block second projection). All ported from libaom av1/common/mvref_common.c, unit-tested against hand-checked distance/ projection arithmetic. mvstack::find_mv_stack_with_sign_bias gains an Option parameter: when Some, folds temporal candidates into the stack at libaom's real insertion point (after the row/col/top-right REF_CAT_LEVEL boost, before the corner probe) and derives zero_mv_ctx (GLOBALMV_OFFSET) from them; None (every existing caller today) reproduces the prior always-0 exact behaviour bit for bit -- all 13 existing mvstack unit tests still pass unmodified. NOT DONE: decode_inter_block's only call site still passes tpl: None (see its own TODO), and nothing in decode_inter_frame_tile_with_cdfs or stream::decode_stream builds a per-DPB-slot MotionField or calls setup_motion_field -- so decode::tmv_hits() reads 0 against any real stream, and no temporal-MV gate exists yet. See ledger DELTA for the precise remaining wiring. --- crates/ec-av1/src/decode.rs | 22 ++ crates/ec-av1/src/lib.rs | 1 + crates/ec-av1/src/motion_field.rs | 477 ++++++++++++++++++++++++++++++ crates/ec-av1/src/mvstack.rs | 135 ++++++++- 4 files changed, 631 insertions(+), 4 deletions(-) create mode 100644 crates/ec-av1/src/motion_field.rs diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 3b5a2d74..101b7bdb 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -131,6 +131,23 @@ static BWDREF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUs static ALTREF2_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); static ALTREF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); +/// How many query blocks folded in at least one temporal MV candidate +/// (spec 7.10.2.8's `add_tpl_ref_mv`) so far in this process -- the +/// temporal-MV gate's own firing counter (lane-av1tmvp). +/// +/// # HANDOFF (lane-av1tmvp): not yet wired to a live stream +/// [`crate::mvstack::find_mv_stack_with_sign_bias`]'s only call site in +/// this file (`decode_inter_block`) still passes `tpl: None` -- this +/// counter compiles and the projection math it counts is unit-tested +/// ([`crate::motion_field`]), but nothing in [`crate::stream::decode_stream`] +/// builds a per-DPB-slot [`crate::motion_field::MotionField`] or calls +/// [`crate::motion_field::setup_motion_field`] yet, so this will read `0` +/// against any real stream today. See the module doc on +/// `crate::motion_field` and this crate's ledger for the remaining wiring. +pub(crate) fn tmv_hits() -> usize { + crate::mvstack::tmv_hits() +} + /// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; /// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). pub(crate) fn ref_hits(ref_frame: i8) -> usize { @@ -3814,6 +3831,11 @@ fn decode_inter_block( mi_cols as usize, mi_rows as usize, sign_bias_table, + // TODO(lane-av1tmvp HANDOFF): thread a real `TplArgs` here once + // `decode_inter_frame_tile_with_cdfs` carries a projected + // `TplField` (see that function's own doc) -- `None` keeps + // today's `use_ref_frame_mvs = 0`-only behaviour exact. + None, ); let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; let mut is_globalmv = false; diff --git a/crates/ec-av1/src/lib.rs b/crates/ec-av1/src/lib.rs index 90ed6061..e749bf2a 100644 --- a/crates/ec-av1/src/lib.rs +++ b/crates/ec-av1/src/lib.rs @@ -23,6 +23,7 @@ pub mod frame; pub mod intra; pub mod mc; pub mod motion; +mod motion_field; pub mod msac; pub mod mvstack; pub mod obu; diff --git a/crates/ec-av1/src/motion_field.rs b/crates/ec-av1/src/motion_field.rs new file mode 100644 index 00000000..bac2f6eb --- /dev/null +++ b/crates/ec-av1/src/motion_field.rs @@ -0,0 +1,477 @@ +//! 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], +} + +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]) -> 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, + } + } + + /// 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. + pub fn set(&mut self, mi_row: usize, mi_col: usize, saved: SavedMv) { + let (r, c) = (mi_row / 2, mi_col / 2); + if r < self.rows && c < self.cols { + self.cells[r * self.cols + c] = Some(saved); + } + } + + fn get(&self, row8: usize, col8: usize) -> Option { + if row8 < self.rows && col8 < self.cols { + self.cells[row8 * self.cols + col8] + } else { + None + } + } +} + +/// The current frame's projected temporal motion field (spec 7.9.2's +/// `MotionFieldMvs`): one projected `(mv, ref_frame_offset)` per 8x8 cell, +/// `None` where no source candidate landed (libaom's `INVALID_MV`). +#[derive(Clone, Debug)] +pub struct TplField { + cols: usize, + rows: usize, + cells: Vec>, +} + +impl TplField { + fn get(&self, row8: usize, col8: usize) -> Option<((i32, i32), i32)> { + if row8 < self.rows && col8 < self.cols { + self.cells[row8 * self.cols + col8] + } else { + None + } + } +} + +/// 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`), the `is_integer == false` half +/// only — every stream this decoder ever reaches has `force_integer_mv == +/// false` on an inter frame (`allow_screen_content_tools`, the only source +/// of a `true` value here, is refused outright at the stream level). +pub fn lower_mv_precision(mv: (i32, i32), allow_high_precision_mv: bool) -> (i32, i32) { + 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 [Option<((i32, i32), i32)>], +) -> bool { + 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], + ) + } + }); + for row8 in 0..start.rows { + for col8 in 0..start.cols { + let Some(saved) = start.get(row8, col8) else { + continue; + }; + if saved.ref_frame <= 0 { + continue; + } + let ref_frame_offset = ref_offset[saved.ref_frame as usize]; + let pos_valid = ref_frame_offset.abs() <= MAX_FRAME_DISTANCE + && ref_frame_offset > 0 + && start_to_current.abs() <= MAX_FRAME_DISTANCE; + if !pos_valid { + 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] = Some((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![None; cols * rows]; + + let slot_of = + |ref_frame: i8| slots[ref_frame_idx[(ref_frame - LAST_FRAME) as usize] as usize].as_ref(); + 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, + ); + } + + 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. +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, +) -> Option { + 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; + if row8 < 0 || col8 < 0 { + return None; + } + let (fwd_mv, ref_frame_offset) = tpl.get(row8 as usize / 2, col8 as usize / 2)?; + let projected = get_mv_projection(fwd_mv, cur_offset_0, ref_frame_offset); + let mv = lower_mv_precision(projected, allow_high_precision_mv); + Some(TplCandidate { mv }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[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]); + mf.set( + 0, + 0, + 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); + assert!(cand.is_some(), "expected a projected temporal candidate"); + } +} diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 2534f077..a9bf3e55 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -78,6 +78,40 @@ //! stack to two entries (spec 7.10.2.12) — this module's stack can be //! shorter than two candidates where libaom's never is. +/// How many query blocks [`find_mv_stack_with_sign_bias`] has folded at +/// least one temporal MV candidate into (spec 7.9/7.10.2.8's +/// `add_tpl_ref_mv`) — the real-aomenc temporal-MV gate's own firing +/// counter (`crate::decode::tmv_hits`), proving the projection path in +/// [`crate::motion_field`] is actually reached, not just compiled. +static TMV_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// The number of query blocks that received at least one temporal MV +/// candidate so far, across every [`find_mv_stack_with_sign_bias`] call in +/// this process. +pub(crate) fn tmv_hits() -> usize { + TMV_HITS.load(std::sync::atomic::Ordering::Relaxed) +} + +/// Everything [`find_mv_stack_with_sign_bias`] needs to fold temporal MV +/// candidates into a block's stack (spec 7.10.2.8, only reached when the +/// frame header's own `use_ref_frame_mvs` is set): the current frame's own +/// projected motion field, the distance (in `get_relative_dist` units) from +/// the current frame to *this query's* single reference frame's own +/// `OrderHint` (`cur_offset_0`, spec 7.9.3 — computed by the caller, since +/// this module has no reason to carry `order_hint_bits`/`OrderHints[]` +/// itself), and `allow_high_precision_mv` (`lower_mv_precision`, spec +/// 7.10.2.8). +pub struct TplArgs<'a> { + /// The current frame's projected temporal motion field + /// ([`crate::motion_field::setup_motion_field`]'s output). + pub field: &'a crate::motion_field::TplField, + /// `get_relative_dist(OrderHint, OrderHints[ref_frame - LAST_FRAME])`, + /// pre-computed by the caller. + pub cur_offset_0: i32, + /// The frame header's own `allow_high_precision_mv`. + pub allow_high_precision_mv: bool, +} + /// 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)] @@ -534,6 +568,7 @@ pub fn find_mv_stack( mi_cols, mi_rows, &NO_SIGN_BIAS, + None, ) } @@ -553,6 +588,7 @@ pub fn find_mv_stack_with_sign_bias( mi_cols: usize, mi_rows: usize, sign_bias_table: &SignBiasTable, + tpl: Option, ) -> MvStack { let mut candidates = Vec::new(); let mut newmv_count = 0u32; @@ -629,6 +665,97 @@ pub fn find_mv_stack_with_sign_bias( entry.weight += REF_CAT_LEVEL; } + // Temporal MV candidates (spec 7.10.2.8's `add_tpl_ref_mv`, only reached + // when the frame header set `use_ref_frame_mvs`): probed over a grid of + // 8x8-unit offsets spanning the block (libaom's own `blk_row_end`/ + // `blk_col_end`/`step_h`/`step_w`, every real block this crate ever + // codes being square and no wider than 32x32 — `BLOCK_64X64`'s own + // 16x16-mi-unit step branch is dead here, ported anyway since nothing + // relies on that), plus three "extension" samples past the block's own + // span (`tpl_sample_pos`) once the block is at least 8x8 and smaller + // than 64x64 (`allow_extension` — true for every block size this crate + // codes). Folded in *after* the row/col/top-right scan's own + // `REF_CAT_LEVEL` boost above and *before* the corner probe, matching + // libaom's call order in `setup_ref_mv_list` exactly (a temporal + // candidate never gets the boost, and the corner probe can still add a + // genuinely new, lower-weight entry after it). + let mut zero_mv_ctx = 0usize; + if let Some(TplArgs { + field, + cur_offset_0, + allow_high_precision_mv, + }) = tpl + { + let mut any_hit = false; + let voffset = bh4.max(2); + let hoffset = bw4.max(2); + let blk_row_end = bh4.min(16); + let blk_col_end = bw4.min(16); + let step_h = if bh4 >= 16 { 4 } else { 2 }; + let step_w = if bw4 >= 16 { 4 } else { 2 }; + let mut first_sample_missing = true; + let mut first_sample_far = false; + let mut blk_row = 0usize; + while blk_row < blk_row_end { + let mut blk_col = 0usize; + while blk_col < blk_col_end { + if let Some(cand) = crate::motion_field::add_tpl_ref_mv( + field, + mi_row, + mi_col, + blk_row as isize, + blk_col as isize, + cur_offset_0, + allow_high_precision_mv, + ) { + any_hit = true; + if blk_row == 0 && blk_col == 0 { + first_sample_missing = false; + first_sample_far = cand.mv.0.abs() >= 16 || cand.mv.1.abs() >= 16; + } + add_candidate(&mut candidates, cand.mv, 2); + } + blk_col += step_w; + } + blk_row += step_h; + } + // Three "extension" samples past the block's own span (libaom + // `tpl_sample_pos`), reachable for every 8x8..32x32 block this crate + // codes (`allow_extension`), gated on staying inside the block's own + // 64x64 superblock (`check_sb_border`). + let allow_extension = (2..16).contains(&bh4) && (2..16).contains(&bw4); + if allow_extension { + let sb_mask = 16isize; + for (row_off, col_off) in [ + (voffset as isize, -2isize), + (voffset as isize, hoffset as isize), + (voffset as isize - 2, hoffset as isize), + ] { + let row = (mi_row as isize & (sb_mask - 1)) + row_off; + let col = (mi_col as isize & (sb_mask - 1)) + col_off; + if row < 0 || row >= sb_mask || col < 0 || col >= sb_mask { + continue; + } + if let Some(cand) = crate::motion_field::add_tpl_ref_mv( + field, + mi_row, + mi_col, + row_off, + col_off, + cur_offset_0, + allow_high_precision_mv, + ) { + any_hit = true; + add_candidate(&mut candidates, cand.mv, 2); + } + } + } + if any_hit { + TMV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + zero_mv_ctx = usize::from(first_sample_missing || first_sample_far); + } + // The diagonal top-left corner probe (module doc): folds into the row // side for `ref_match_count` without moving the already-captured // `nearest_match`, and its own newmv-ness is a dummy count libaom @@ -816,10 +943,10 @@ pub fn find_mv_stack_with_sign_bias( } _ => 5, }; - // GLOBALMV_OFFSET is only ever set by temporal MV projection - // (`cm->features.allow_ref_frame_mvs`), which this reduction never runs - // (see module doc) — exact, not a stand-in. - let zero_mv_ctx = 0; + // `zero_mv_ctx` was already computed above, from the temporal-MV pass + // when `tpl` is `Some` (GLOBALMV_OFFSET, spec 7.10.2.8) — `0` otherwise, + // exactly this reduction's old always-0 behaviour when + // `use_ref_frame_mvs` is unset. // av1_drl_ctx (mvref_common.h): weight was boosted in place above for // the nearest entries only, so this now compares the real stored weight