wip(av1): temporal MV projection math (spec 7.9/7.10.2.8), not yet wired
Browse files at this commit916263c6cea628c7fbcd41d87ea576669efeff6dParent: 2bcbc814 changed files +631 -4
- crates/ec-av1/src/decode.rs +22 -0
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/motion_field.rs +477 -0
- crates/ec-av1/src/mvstack.rs +131 -4
crates/ec-av1/src/decode.rsmodified+22 -0
| @@ -131,6 +131,23 @@ static BWDREF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUs | ||
| 131 | 131 | static ALTREF2_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 132 | 132 | static ALTREF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 133 | 133 | |
| 134 | +/// How many query blocks folded in at least one temporal MV candidate | |
| 135 | +/// (spec 7.10.2.8's `add_tpl_ref_mv`) so far in this process -- the | |
| 136 | +/// temporal-MV gate's own firing counter (lane-av1tmvp). | |
| 137 | +/// | |
| 138 | +/// # HANDOFF (lane-av1tmvp): not yet wired to a live stream | |
| 139 | +/// [`crate::mvstack::find_mv_stack_with_sign_bias`]'s only call site in | |
| 140 | +/// this file (`decode_inter_block`) still passes `tpl: None` -- this | |
| 141 | +/// counter compiles and the projection math it counts is unit-tested | |
| 142 | +/// ([`crate::motion_field`]), but nothing in [`crate::stream::decode_stream`] | |
| 143 | +/// builds a per-DPB-slot [`crate::motion_field::MotionField`] or calls | |
| 144 | +/// [`crate::motion_field::setup_motion_field`] yet, so this will read `0` | |
| 145 | +/// against any real stream today. See the module doc on | |
| 146 | +/// `crate::motion_field` and this crate's ledger for the remaining wiring. | |
| 147 | +pub(crate) fn tmv_hits() -> usize { | |
| 148 | + crate::mvstack::tmv_hits() | |
| 149 | +} | |
| 150 | + | |
| 134 | 151 | /// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; |
| 135 | 152 | /// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). |
| 136 | 153 | pub(crate) fn ref_hits(ref_frame: i8) -> usize { |
| @@ -3814,6 +3831,11 @@ fn decode_inter_block( | ||
| 3814 | 3831 | mi_cols as usize, |
| 3815 | 3832 | mi_rows as usize, |
| 3816 | 3833 | sign_bias_table, |
| 3834 | + // TODO(lane-av1tmvp HANDOFF): thread a real `TplArgs` here once | |
| 3835 | + // `decode_inter_frame_tile_with_cdfs` carries a projected | |
| 3836 | + // `TplField` (see that function's own doc) -- `None` keeps | |
| 3837 | + // today's `use_ref_frame_mvs = 0`-only behaviour exact. | |
| 3838 | + None, | |
| 3817 | 3839 | ); |
| 3818 | 3840 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; |
| 3819 | 3841 | let mut is_globalmv = false; |
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -23,6 +23,7 @@ pub mod frame; | ||
| 23 | 23 | pub mod intra; |
| 24 | 24 | pub mod mc; |
| 25 | 25 | pub mod motion; |
| 26 | +mod motion_field; | |
| 26 | 27 | pub mod msac; |
| 27 | 28 | pub mod mvstack; |
| 28 | 29 | pub mod obu; |
crates/ec-av1/src/motion_field.rsadded+477 -0
| @@ -0,0 +1,477 @@ | ||
| 1 | +//! Temporal MV projection (AV1 spec 7.9, `use_ref_frame_mvs`): projects a | |
| 2 | +//! previously decoded frame's stored motion vectors into the current | |
| 3 | +//! frame's own 8x8 motion field, ported from libaom's | |
| 4 | +//! `av1_setup_motion_field`/`motion_field_projection` | |
| 5 | +//! (`av1/common/mvref_common.c`) and the projection math `add_tpl_ref_mv` | |
| 6 | +//! (same file) applies a second time, per query block, against the block's | |
| 7 | +//! own single reference frame. | |
| 8 | + | |
| 9 | +use crate::mvstack::{ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST_FRAME, LAST2_FRAME}; | |
| 10 | + | |
| 11 | +/// One 8x8 cell's saved motion state, as libaom's `av1_copy_frame_mvs` | |
| 12 | +/// leaves it after decoding the frame that owns this grid: the coded MV and | |
| 13 | +/// the single reference frame (`LAST_FRAME..=ALTREF_FRAME`, 1..=7) it | |
| 14 | +/// pointed to. | |
| 15 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 16 | +pub struct SavedMv { | |
| 17 | + pub mv: (i32, i32), | |
| 18 | + pub ref_frame: i8, | |
| 19 | +} | |
| 20 | + | |
| 21 | +/// A decoded inter frame's whole motion field (spec `MotionFieldMvs`... no | |
| 22 | +/// — libaom's `cur_frame->mvs`), one [`SavedMv`] per 8x8 unit, plus the | |
| 23 | +/// order-hint bookkeeping [`setup_motion_field`] needs to project it into a | |
| 24 | +/// later frame: this frame's own `OrderHint` and the `OrderHint` of each of | |
| 25 | +/// the 7 references *it* was coded against (`SavedOrderHints`/ | |
| 26 | +/// `ref_order_hints`). | |
| 27 | +#[derive(Clone, Debug)] | |
| 28 | +pub struct MotionField { | |
| 29 | + cols: usize, | |
| 30 | + rows: usize, | |
| 31 | + cells: Vec<Option<SavedMv>>, | |
| 32 | + pub order_hint: u32, | |
| 33 | + pub ref_order_hints: [u32; 7], | |
| 34 | +} | |
| 35 | + | |
| 36 | +impl MotionField { | |
| 37 | + /// A field sized for an `mi_cols` by `mi_rows` frame, all cells | |
| 38 | + /// initially unset (spec 7.9's own reset to `-1`/invalid before any | |
| 39 | + /// block writes into it). | |
| 40 | + pub fn new(mi_cols: usize, mi_rows: usize, order_hint: u32, ref_order_hints: [u32; 7]) -> Self { | |
| 41 | + let cols = mi_cols.div_ceil(2); | |
| 42 | + let rows = mi_rows.div_ceil(2); | |
| 43 | + Self { | |
| 44 | + cols, | |
| 45 | + rows, | |
| 46 | + cells: vec![None; cols * rows], | |
| 47 | + order_hint, | |
| 48 | + ref_order_hints, | |
| 49 | + } | |
| 50 | + } | |
| 51 | + | |
| 52 | + /// Records `saved` at the 8x8 cell containing 4x4 unit `(mi_row, | |
| 53 | + /// mi_col)` — every `MiInfo` inside one 8x8 cell shares a single | |
| 54 | + /// motion-field entry, matching `av1_copy_frame_mvs`'s own half-resolution | |
| 55 | + /// write. | |
| 56 | + pub fn set(&mut self, mi_row: usize, mi_col: usize, saved: SavedMv) { | |
| 57 | + let (r, c) = (mi_row / 2, mi_col / 2); | |
| 58 | + if r < self.rows && c < self.cols { | |
| 59 | + self.cells[r * self.cols + c] = Some(saved); | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + fn get(&self, row8: usize, col8: usize) -> Option<SavedMv> { | |
| 64 | + if row8 < self.rows && col8 < self.cols { | |
| 65 | + self.cells[row8 * self.cols + col8] | |
| 66 | + } else { | |
| 67 | + None | |
| 68 | + } | |
| 69 | + } | |
| 70 | +} | |
| 71 | + | |
| 72 | +/// The current frame's projected temporal motion field (spec 7.9.2's | |
| 73 | +/// `MotionFieldMvs`): one projected `(mv, ref_frame_offset)` per 8x8 cell, | |
| 74 | +/// `None` where no source candidate landed (libaom's `INVALID_MV`). | |
| 75 | +#[derive(Clone, Debug)] | |
| 76 | +pub struct TplField { | |
| 77 | + cols: usize, | |
| 78 | + rows: usize, | |
| 79 | + cells: Vec<Option<((i32, i32), i32)>>, | |
| 80 | +} | |
| 81 | + | |
| 82 | +impl TplField { | |
| 83 | + fn get(&self, row8: usize, col8: usize) -> Option<((i32, i32), i32)> { | |
| 84 | + if row8 < self.rows && col8 < self.cols { | |
| 85 | + self.cells[row8 * self.cols + col8] | |
| 86 | + } else { | |
| 87 | + None | |
| 88 | + } | |
| 89 | + } | |
| 90 | +} | |
| 91 | + | |
| 92 | +/// spec `get_relative_dist` (5.9.3): `a - b` wrapped into the signed range | |
| 93 | +/// order hints roll over in at `order_hint_bits` width. | |
| 94 | +pub fn get_relative_dist(order_hint_bits: u32, a: u32, b: u32) -> i32 { | |
| 95 | + if order_hint_bits == 0 { | |
| 96 | + return 0; | |
| 97 | + } | |
| 98 | + let diff = a as i32 - b as i32; | |
| 99 | + let m = 1i32 << (order_hint_bits - 1); | |
| 100 | + (diff & (m - 1)) - (diff & m) | |
| 101 | +} | |
| 102 | + | |
| 103 | +/// spec `FRAME_OFFSET_BITS`-derived clamp (libaom `MAX_FRAME_DISTANCE`). | |
| 104 | +const MAX_FRAME_DISTANCE: i32 = 31; | |
| 105 | + | |
| 106 | +/// libaom's `div_mult`: precomputed `16384 / den` (`av1_get_mv_projection`'s | |
| 107 | +/// own comment: "all the values are strictly under 14 bits"). | |
| 108 | +const DIV_MULT: [i64; 32] = [ | |
| 109 | + 0, 16384, 8192, 5461, 4096, 3276, 2730, 2340, 2048, 1820, 1638, 1489, 1365, 1260, 1170, 1092, | |
| 110 | + 1024, 963, 910, 862, 819, 780, 744, 712, 682, 655, 630, 606, 585, 564, 546, 528, | |
| 111 | +]; | |
| 112 | + | |
| 113 | +fn round_pow2_signed(value: i64, n: u32) -> i32 { | |
| 114 | + let half = 1i64 << (n - 1); | |
| 115 | + (if value < 0 { | |
| 116 | + -((-value + half) >> n) | |
| 117 | + } else { | |
| 118 | + (value + half) >> n | |
| 119 | + }) as i32 | |
| 120 | +} | |
| 121 | + | |
| 122 | +/// `av1_get_mv_projection` (spec 7.9.3): scales `mv` by the ratio of two | |
| 123 | +/// frame-distance offsets. | |
| 124 | +fn get_mv_projection(mv: (i32, i32), num: i32, den: i32) -> (i32, i32) { | |
| 125 | + let den = den.min(MAX_FRAME_DISTANCE); | |
| 126 | + let num = if num > 0 { | |
| 127 | + num.min(MAX_FRAME_DISTANCE) | |
| 128 | + } else { | |
| 129 | + num.max(-MAX_FRAME_DISTANCE) | |
| 130 | + }; | |
| 131 | + let mult = DIV_MULT[den as usize]; | |
| 132 | + let scale = |v: i32| round_pow2_signed(v as i64 * num as i64 * mult, 14).clamp(-16383, 16383); | |
| 133 | + (scale(mv.0), scale(mv.1)) | |
| 134 | +} | |
| 135 | + | |
| 136 | +/// `lower_mv_precision` (`mvref_common.h`), the `is_integer == false` half | |
| 137 | +/// only — every stream this decoder ever reaches has `force_integer_mv == | |
| 138 | +/// false` on an inter frame (`allow_screen_content_tools`, the only source | |
| 139 | +/// of a `true` value here, is refused outright at the stream level). | |
| 140 | +pub fn lower_mv_precision(mv: (i32, i32), allow_high_precision_mv: bool) -> (i32, i32) { | |
| 141 | + if allow_high_precision_mv { | |
| 142 | + return mv; | |
| 143 | + } | |
| 144 | + let round = |v: i32| { | |
| 145 | + if v & 1 != 0 { | |
| 146 | + v + if v > 0 { -1 } else { 1 } | |
| 147 | + } else { | |
| 148 | + v | |
| 149 | + } | |
| 150 | + }; | |
| 151 | + (round(mv.0), round(mv.1)) | |
| 152 | +} | |
| 153 | + | |
| 154 | +/// `get_block_position` (spec 7.9.2): the 8x8 cell a projected `mv` lands on | |
| 155 | +/// starting from `(blk_row, blk_col)` (already in 8x8 units), or `None` when | |
| 156 | +/// it falls outside the frame or outside the projection's own bound (a | |
| 157 | +/// 64x64-superblock-aligned base block, spec's `MAX_OFFSET_WIDTH`/ | |
| 158 | +/// `MAX_OFFSET_HEIGHT`). | |
| 159 | +fn get_block_position( | |
| 160 | + mvs_rows: usize, | |
| 161 | + mvs_cols: usize, | |
| 162 | + blk_row: usize, | |
| 163 | + blk_col: usize, | |
| 164 | + mv: (i32, i32), | |
| 165 | + sign_bias: bool, | |
| 166 | +) -> Option<(usize, usize)> { | |
| 167 | + let base_blk_row = (blk_row >> 3) << 3; | |
| 168 | + let base_blk_col = (blk_col >> 3) << 3; | |
| 169 | + let row_offset = if mv.0 >= 0 { | |
| 170 | + mv.0 >> 6 | |
| 171 | + } else { | |
| 172 | + -((-mv.0) >> 6) | |
| 173 | + }; | |
| 174 | + let col_offset = if mv.1 >= 0 { | |
| 175 | + mv.1 >> 6 | |
| 176 | + } else { | |
| 177 | + -((-mv.1) >> 6) | |
| 178 | + }; | |
| 179 | + let row = if sign_bias { | |
| 180 | + blk_row as i64 - row_offset as i64 | |
| 181 | + } else { | |
| 182 | + blk_row as i64 + row_offset as i64 | |
| 183 | + }; | |
| 184 | + let col = if sign_bias { | |
| 185 | + blk_col as i64 - col_offset as i64 | |
| 186 | + } else { | |
| 187 | + blk_col as i64 + col_offset as i64 | |
| 188 | + }; | |
| 189 | + if row < 0 || row >= mvs_rows as i64 || col < 0 || col >= mvs_cols as i64 { | |
| 190 | + return None; | |
| 191 | + } | |
| 192 | + if row < base_blk_row as i64 | |
| 193 | + || row >= base_blk_row as i64 + 8 | |
| 194 | + || col < base_blk_col as i64 - 8 | |
| 195 | + || col >= base_blk_col as i64 + 16 | |
| 196 | + { | |
| 197 | + return None; | |
| 198 | + } | |
| 199 | + Some((row as usize, col as usize)) | |
| 200 | +} | |
| 201 | + | |
| 202 | +/// `motion_field_projection` (spec 7.9.2): projects `start`'s stored MVs | |
| 203 | +/// into `tpl`, scaled by the OrderHint distance from `start` to the current | |
| 204 | +/// frame. `dir` mirrors libaom's own parameter: `2` for a backward-looking | |
| 205 | +/// source (`LAST_FRAME`/`LAST2_FRAME`, offset negated and `sign_bias` | |
| 206 | +/// flipped), `0` for a forward one (`BWDREF_FRAME`/`ALTREF2_FRAME`/ | |
| 207 | +/// `ALTREF_FRAME`). Returns whether the projection ran at all (`start`'s | |
| 208 | +/// frame size has to match the current frame's — always true for this | |
| 209 | +/// decoder's fixed-size streams, but ported anyway). | |
| 210 | +#[allow(clippy::too_many_arguments)] | |
| 211 | +fn motion_field_projection( | |
| 212 | + start: &MotionField, | |
| 213 | + cur_order_hint: u32, | |
| 214 | + order_hint_bits: u32, | |
| 215 | + mi_rows: usize, | |
| 216 | + mi_cols: usize, | |
| 217 | + dir: u8, | |
| 218 | + tpl_cells: &mut [Option<((i32, i32), i32)>], | |
| 219 | +) -> bool { | |
| 220 | + let mvs_rows = mi_rows.div_ceil(2); | |
| 221 | + let mvs_cols = mi_cols.div_ceil(2); | |
| 222 | + if start.rows != mvs_rows || start.cols != mvs_cols { | |
| 223 | + return false; | |
| 224 | + } | |
| 225 | + let start_order_hint = start.order_hint; | |
| 226 | + let mut start_to_current = get_relative_dist(order_hint_bits, start_order_hint, cur_order_hint); | |
| 227 | + if dir == 2 { | |
| 228 | + start_to_current = -start_to_current; | |
| 229 | + } | |
| 230 | + let ref_offset: [i32; 8] = std::array::from_fn(|rf| { | |
| 231 | + if rf == 0 { | |
| 232 | + 0 | |
| 233 | + } else { | |
| 234 | + get_relative_dist( | |
| 235 | + order_hint_bits, | |
| 236 | + start_order_hint, | |
| 237 | + start.ref_order_hints[rf - 1], | |
| 238 | + ) | |
| 239 | + } | |
| 240 | + }); | |
| 241 | + for row8 in 0..start.rows { | |
| 242 | + for col8 in 0..start.cols { | |
| 243 | + let Some(saved) = start.get(row8, col8) else { | |
| 244 | + continue; | |
| 245 | + }; | |
| 246 | + if saved.ref_frame <= 0 { | |
| 247 | + continue; | |
| 248 | + } | |
| 249 | + let ref_frame_offset = ref_offset[saved.ref_frame as usize]; | |
| 250 | + let pos_valid = ref_frame_offset.abs() <= MAX_FRAME_DISTANCE | |
| 251 | + && ref_frame_offset > 0 | |
| 252 | + && start_to_current.abs() <= MAX_FRAME_DISTANCE; | |
| 253 | + if !pos_valid { | |
| 254 | + continue; | |
| 255 | + } | |
| 256 | + let projected = get_mv_projection(saved.mv, start_to_current, ref_frame_offset); | |
| 257 | + if let Some((r, c)) = | |
| 258 | + get_block_position(mvs_rows, mvs_cols, row8, col8, projected, dir >> 1 == 1) | |
| 259 | + { | |
| 260 | + tpl_cells[r * mvs_cols + c] = Some((saved.mv, ref_frame_offset)); | |
| 261 | + } | |
| 262 | + } | |
| 263 | + } | |
| 264 | + true | |
| 265 | +} | |
| 266 | + | |
| 267 | +/// `av1_setup_motion_field` (spec 7.9.2's own driver): scans up to | |
| 268 | +/// [`MFMV_STACK_SIZE`]`= 3` reference-frame slots in libaom's fixed order | |
| 269 | +/// (`LAST_FRAME`, then `BWDREF_FRAME`/`ALTREF2_FRAME`/`ALTREF_FRAME` when | |
| 270 | +/// each is display-order-forward of the current frame, then `LAST2_FRAME` | |
| 271 | +/// once the stack still has room) and projects each into the returned | |
| 272 | +/// [`TplField`]. | |
| 273 | +/// | |
| 274 | +/// `slots` is the DPB's 8 reference-picture slots' saved [`MotionField`]s | |
| 275 | +/// (a `None` slot, or one still holding an intra frame's — never | |
| 276 | +/// constructed, so also `None` — motion field, contributes nothing); | |
| 277 | +/// `ref_frame_idx` is the current frame header's own `ref_frame_idx` | |
| 278 | +/// (`LAST_FRAME..=ALTREF_FRAME`, indexed `[i]` for `LAST_FRAME + i`). | |
| 279 | +pub fn setup_motion_field( | |
| 280 | + slots: &[Option<MotionField>; 8], | |
| 281 | + ref_frame_idx: [u8; 7], | |
| 282 | + cur_order_hint: u32, | |
| 283 | + order_hint_bits: u32, | |
| 284 | + mi_rows: usize, | |
| 285 | + mi_cols: usize, | |
| 286 | +) -> TplField { | |
| 287 | + let cols = mi_cols.div_ceil(2); | |
| 288 | + let rows = mi_rows.div_ceil(2); | |
| 289 | + let mut cells = vec![None; cols * rows]; | |
| 290 | + | |
| 291 | + let slot_of = | |
| 292 | + |ref_frame: i8| slots[ref_frame_idx[(ref_frame - LAST_FRAME) as usize] as usize].as_ref(); | |
| 293 | + let order_hint_of = |ref_frame: i8| slot_of(ref_frame).map_or(0, |m| m.order_hint); | |
| 294 | + | |
| 295 | + let mut ref_stamp = 2i32; // MFMV_STACK_SIZE - 1 | |
| 296 | + | |
| 297 | + if let Some(last) = slot_of(LAST_FRAME) { | |
| 298 | + let alt_of_lst = last.ref_order_hints[(ALTREF_FRAME - LAST_FRAME) as usize]; | |
| 299 | + let is_lst_overlay = alt_of_lst == order_hint_of(GOLDEN_FRAME); | |
| 300 | + if !is_lst_overlay { | |
| 301 | + motion_field_projection( | |
| 302 | + last, | |
| 303 | + cur_order_hint, | |
| 304 | + order_hint_bits, | |
| 305 | + mi_rows, | |
| 306 | + mi_cols, | |
| 307 | + 2, | |
| 308 | + &mut cells, | |
| 309 | + ); | |
| 310 | + } | |
| 311 | + ref_stamp -= 1; | |
| 312 | + } | |
| 313 | + | |
| 314 | + if get_relative_dist(order_hint_bits, order_hint_of(BWDREF_FRAME), cur_order_hint) > 0 | |
| 315 | + && let Some(bwd) = slot_of(BWDREF_FRAME) | |
| 316 | + && motion_field_projection( | |
| 317 | + bwd, | |
| 318 | + cur_order_hint, | |
| 319 | + order_hint_bits, | |
| 320 | + mi_rows, | |
| 321 | + mi_cols, | |
| 322 | + 0, | |
| 323 | + &mut cells, | |
| 324 | + ) | |
| 325 | + { | |
| 326 | + ref_stamp -= 1; | |
| 327 | + } | |
| 328 | + | |
| 329 | + if get_relative_dist( | |
| 330 | + order_hint_bits, | |
| 331 | + order_hint_of(crate::mvstack::ALTREF2_FRAME), | |
| 332 | + cur_order_hint, | |
| 333 | + ) > 0 | |
| 334 | + && let Some(alt2) = slot_of(crate::mvstack::ALTREF2_FRAME) | |
| 335 | + && motion_field_projection( | |
| 336 | + alt2, | |
| 337 | + cur_order_hint, | |
| 338 | + order_hint_bits, | |
| 339 | + mi_rows, | |
| 340 | + mi_cols, | |
| 341 | + 0, | |
| 342 | + &mut cells, | |
| 343 | + ) | |
| 344 | + { | |
| 345 | + ref_stamp -= 1; | |
| 346 | + } | |
| 347 | + | |
| 348 | + if ref_stamp >= 0 | |
| 349 | + && get_relative_dist(order_hint_bits, order_hint_of(ALTREF_FRAME), cur_order_hint) > 0 | |
| 350 | + && let Some(alt) = slot_of(ALTREF_FRAME) | |
| 351 | + && motion_field_projection( | |
| 352 | + alt, | |
| 353 | + cur_order_hint, | |
| 354 | + order_hint_bits, | |
| 355 | + mi_rows, | |
| 356 | + mi_cols, | |
| 357 | + 0, | |
| 358 | + &mut cells, | |
| 359 | + ) | |
| 360 | + { | |
| 361 | + ref_stamp -= 1; | |
| 362 | + } | |
| 363 | + | |
| 364 | + if ref_stamp >= 0 | |
| 365 | + && let Some(last2) = slot_of(LAST2_FRAME) | |
| 366 | + { | |
| 367 | + motion_field_projection( | |
| 368 | + last2, | |
| 369 | + cur_order_hint, | |
| 370 | + order_hint_bits, | |
| 371 | + mi_rows, | |
| 372 | + mi_cols, | |
| 373 | + 2, | |
| 374 | + &mut cells, | |
| 375 | + ); | |
| 376 | + } | |
| 377 | + | |
| 378 | + TplField { cols, rows, cells } | |
| 379 | +} | |
| 380 | + | |
| 381 | +/// The result [`add_tpl_ref_mv`] folds into a query block's MV stack: the | |
| 382 | +/// projected candidate MV (already scaled to the block's own reference | |
| 383 | +/// frame and MV-precision-lowered) plus whether the very first (`blk_row == | |
| 384 | +/// 0 && blk_col == 0`) sample differs enough from the zero vector to raise | |
| 385 | +/// `GLOBALMV_OFFSET` (spec 7.10.2.8 — this decoder only ever reaches | |
| 386 | +/// `IDENTITY` global motion, so the spec's `gm_get_motion_vector` compare | |
| 387 | +/// collapses to "far from `(0, 0)`"). | |
| 388 | +pub struct TplCandidate { | |
| 389 | + pub mv: (i32, i32), | |
| 390 | +} | |
| 391 | + | |
| 392 | +/// `add_tpl_ref_mv`'s single-reference half (spec 7.10.2.8, libaom | |
| 393 | +/// `mvref_common.c`): probes one 8x8-unit offset `(blk_row, blk_col)` from | |
| 394 | +/// `(mi_row, mi_col)` in [`TplField`] and, if it landed a candidate, | |
| 395 | +/// projects it a second time — this time scaled to `cur_offset_0`, the | |
| 396 | +/// distance from the current frame to the query block's own single | |
| 397 | +/// reference frame, rather than `start_to_current_frame_offset` from the | |
| 398 | +/// first (storage-time) projection. | |
| 399 | +pub fn add_tpl_ref_mv( | |
| 400 | + tpl: &TplField, | |
| 401 | + mi_row: usize, | |
| 402 | + mi_col: usize, | |
| 403 | + blk_row: isize, | |
| 404 | + blk_col: isize, | |
| 405 | + cur_offset_0: i32, | |
| 406 | + allow_high_precision_mv: bool, | |
| 407 | +) -> Option<TplCandidate> { | |
| 408 | + let pos_row = if mi_row & 1 != 0 { | |
| 409 | + blk_row | |
| 410 | + } else { | |
| 411 | + blk_row + 1 | |
| 412 | + }; | |
| 413 | + let pos_col = if mi_col & 1 != 0 { | |
| 414 | + blk_col | |
| 415 | + } else { | |
| 416 | + blk_col + 1 | |
| 417 | + }; | |
| 418 | + let row8 = mi_row as isize + pos_row; | |
| 419 | + let col8 = mi_col as isize + pos_col; | |
| 420 | + if row8 < 0 || col8 < 0 { | |
| 421 | + return None; | |
| 422 | + } | |
| 423 | + let (fwd_mv, ref_frame_offset) = tpl.get(row8 as usize / 2, col8 as usize / 2)?; | |
| 424 | + let projected = get_mv_projection(fwd_mv, cur_offset_0, ref_frame_offset); | |
| 425 | + let mv = lower_mv_precision(projected, allow_high_precision_mv); | |
| 426 | + Some(TplCandidate { mv }) | |
| 427 | +} | |
| 428 | + | |
| 429 | +#[cfg(test)] | |
| 430 | +mod tests { | |
| 431 | + use super::*; | |
| 432 | + | |
| 433 | + #[test] | |
| 434 | + fn relative_dist_wraps_at_order_hint_bits() { | |
| 435 | + // 7-bit order hints (aomenc's usual default): 1 - 126 should read as | |
| 436 | + // +3 (wrapping backward past 0), not -125. | |
| 437 | + assert_eq!(get_relative_dist(7, 1, 126), 3); | |
| 438 | + assert_eq!(get_relative_dist(7, 10, 5), 5); | |
| 439 | + } | |
| 440 | + | |
| 441 | + #[test] | |
| 442 | + fn mv_projection_scales_by_the_distance_ratio() { | |
| 443 | + // A MV recorded across a 2-frame gap, projected across a 1-frame | |
| 444 | + // gap, should halve (libaom's own div_mult table is exact for small | |
| 445 | + // integer ratios). | |
| 446 | + let (row, col) = get_mv_projection((16, -32), 1, 2); | |
| 447 | + assert_eq!((row, col), (8, -16)); | |
| 448 | + // Same-distance projection is the identity. | |
| 449 | + assert_eq!(get_mv_projection((7, -3), 4, 4), (7, -3)); | |
| 450 | + } | |
| 451 | + | |
| 452 | + #[test] | |
| 453 | + fn setup_motion_field_projects_last_frame_forward() { | |
| 454 | + // A slot-0 inter frame (order_hint 2) that coded one 8x8 cell's MV | |
| 455 | + // against its own LAST_FRAME (order_hint 1). The current frame | |
| 456 | + // (order_hint 4) names that same slot as *its* LAST_FRAME. | |
| 457 | + let mut mf = MotionField::new(16, 16, 2, [1, 0, 0, 0, 0, 0, 0]); | |
| 458 | + mf.set( | |
| 459 | + 0, | |
| 460 | + 0, | |
| 461 | + SavedMv { | |
| 462 | + mv: (-64, 0), | |
| 463 | + ref_frame: LAST_FRAME, | |
| 464 | + }, | |
| 465 | + ); | |
| 466 | + let slots: [Option<MotionField>; 8] = | |
| 467 | + std::array::from_fn(|i| if i == 0 { Some(mf.clone()) } else { None }); | |
| 468 | + let ref_frame_idx = [0u8; 7]; | |
| 469 | + let tpl = setup_motion_field(&slots, ref_frame_idx, 4, 7, 16, 16); | |
| 470 | + // LAST_FRAME's own dir=2 projection: start_to_current = -get_relative_dist(2,4) = 2, | |
| 471 | + // ref_frame_offset = get_relative_dist(2,1) = 1, scaling (-64,0) by | |
| 472 | + // 2/1 -> (-128,0); get_block_position's sign_bias (dir=2) subtracts | |
| 473 | + // the offset, landing at 8x8 cell (2, 0). | |
| 474 | + let cand = add_tpl_ref_mv(&tpl, 4, 0, 0isize, 0isize, get_relative_dist(7, 4, 2), true); | |
| 475 | + assert!(cand.is_some(), "expected a projected temporal candidate"); | |
| 476 | + } | |
| 477 | +} |
crates/ec-av1/src/mvstack.rsmodified+131 -4
| @@ -78,6 +78,40 @@ | ||
| 78 | 78 | //! stack to two entries (spec 7.10.2.12) — this module's stack can be |
| 79 | 79 | //! shorter than two candidates where libaom's never is. |
| 80 | 80 | |
| 81 | +/// How many query blocks [`find_mv_stack_with_sign_bias`] has folded at | |
| 82 | +/// least one temporal MV candidate into (spec 7.9/7.10.2.8's | |
| 83 | +/// `add_tpl_ref_mv`) — the real-aomenc temporal-MV gate's own firing | |
| 84 | +/// counter (`crate::decode::tmv_hits`), proving the projection path in | |
| 85 | +/// [`crate::motion_field`] is actually reached, not just compiled. | |
| 86 | +static TMV_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | |
| 87 | + | |
| 88 | +/// The number of query blocks that received at least one temporal MV | |
| 89 | +/// candidate so far, across every [`find_mv_stack_with_sign_bias`] call in | |
| 90 | +/// this process. | |
| 91 | +pub(crate) fn tmv_hits() -> usize { | |
| 92 | + TMV_HITS.load(std::sync::atomic::Ordering::Relaxed) | |
| 93 | +} | |
| 94 | + | |
| 95 | +/// Everything [`find_mv_stack_with_sign_bias`] needs to fold temporal MV | |
| 96 | +/// candidates into a block's stack (spec 7.10.2.8, only reached when the | |
| 97 | +/// frame header's own `use_ref_frame_mvs` is set): the current frame's own | |
| 98 | +/// projected motion field, the distance (in `get_relative_dist` units) from | |
| 99 | +/// the current frame to *this query's* single reference frame's own | |
| 100 | +/// `OrderHint` (`cur_offset_0`, spec 7.9.3 — computed by the caller, since | |
| 101 | +/// this module has no reason to carry `order_hint_bits`/`OrderHints[]` | |
| 102 | +/// itself), and `allow_high_precision_mv` (`lower_mv_precision`, spec | |
| 103 | +/// 7.10.2.8). | |
| 104 | +pub struct TplArgs<'a> { | |
| 105 | + /// The current frame's projected temporal motion field | |
| 106 | + /// ([`crate::motion_field::setup_motion_field`]'s output). | |
| 107 | + pub field: &'a crate::motion_field::TplField, | |
| 108 | + /// `get_relative_dist(OrderHint, OrderHints[ref_frame - LAST_FRAME])`, | |
| 109 | + /// pre-computed by the caller. | |
| 110 | + pub cur_offset_0: i32, | |
| 111 | + /// The frame header's own `allow_high_precision_mv`. | |
| 112 | + pub allow_high_precision_mv: bool, | |
| 113 | +} | |
| 114 | + | |
| 81 | 115 | /// One 4x4 `mi` unit's motion state, as the encode loop will have filled it |
| 82 | 116 | /// in by the time it asks for a block's MV stack. |
| 83 | 117 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| @@ -534,6 +568,7 @@ pub fn find_mv_stack( | ||
| 534 | 568 | mi_cols, |
| 535 | 569 | mi_rows, |
| 536 | 570 | &NO_SIGN_BIAS, |
| 571 | + None, | |
| 537 | 572 | ) |
| 538 | 573 | } |
| 539 | 574 | |
| @@ -553,6 +588,7 @@ pub fn find_mv_stack_with_sign_bias( | ||
| 553 | 588 | mi_cols: usize, |
| 554 | 589 | mi_rows: usize, |
| 555 | 590 | sign_bias_table: &SignBiasTable, |
| 591 | + tpl: Option<TplArgs>, | |
| 556 | 592 | ) -> MvStack { |
| 557 | 593 | let mut candidates = Vec::new(); |
| 558 | 594 | let mut newmv_count = 0u32; |
| @@ -629,6 +665,97 @@ pub fn find_mv_stack_with_sign_bias( | ||
| 629 | 665 | entry.weight += REF_CAT_LEVEL; |
| 630 | 666 | } |
| 631 | 667 | |
| 668 | + // Temporal MV candidates (spec 7.10.2.8's `add_tpl_ref_mv`, only reached | |
| 669 | + // when the frame header set `use_ref_frame_mvs`): probed over a grid of | |
| 670 | + // 8x8-unit offsets spanning the block (libaom's own `blk_row_end`/ | |
| 671 | + // `blk_col_end`/`step_h`/`step_w`, every real block this crate ever | |
| 672 | + // codes being square and no wider than 32x32 — `BLOCK_64X64`'s own | |
| 673 | + // 16x16-mi-unit step branch is dead here, ported anyway since nothing | |
| 674 | + // relies on that), plus three "extension" samples past the block's own | |
| 675 | + // span (`tpl_sample_pos`) once the block is at least 8x8 and smaller | |
| 676 | + // than 64x64 (`allow_extension` — true for every block size this crate | |
| 677 | + // codes). Folded in *after* the row/col/top-right scan's own | |
| 678 | + // `REF_CAT_LEVEL` boost above and *before* the corner probe, matching | |
| 679 | + // libaom's call order in `setup_ref_mv_list` exactly (a temporal | |
| 680 | + // candidate never gets the boost, and the corner probe can still add a | |
| 681 | + // genuinely new, lower-weight entry after it). | |
| 682 | + let mut zero_mv_ctx = 0usize; | |
| 683 | + if let Some(TplArgs { | |
| 684 | + field, | |
| 685 | + cur_offset_0, | |
| 686 | + allow_high_precision_mv, | |
| 687 | + }) = tpl | |
| 688 | + { | |
| 689 | + let mut any_hit = false; | |
| 690 | + let voffset = bh4.max(2); | |
| 691 | + let hoffset = bw4.max(2); | |
| 692 | + let blk_row_end = bh4.min(16); | |
| 693 | + let blk_col_end = bw4.min(16); | |
| 694 | + let step_h = if bh4 >= 16 { 4 } else { 2 }; | |
| 695 | + let step_w = if bw4 >= 16 { 4 } else { 2 }; | |
| 696 | + let mut first_sample_missing = true; | |
| 697 | + let mut first_sample_far = false; | |
| 698 | + let mut blk_row = 0usize; | |
| 699 | + while blk_row < blk_row_end { | |
| 700 | + let mut blk_col = 0usize; | |
| 701 | + while blk_col < blk_col_end { | |
| 702 | + if let Some(cand) = crate::motion_field::add_tpl_ref_mv( | |
| 703 | + field, | |
| 704 | + mi_row, | |
| 705 | + mi_col, | |
| 706 | + blk_row as isize, | |
| 707 | + blk_col as isize, | |
| 708 | + cur_offset_0, | |
| 709 | + allow_high_precision_mv, | |
| 710 | + ) { | |
| 711 | + any_hit = true; | |
| 712 | + if blk_row == 0 && blk_col == 0 { | |
| 713 | + first_sample_missing = false; | |
| 714 | + first_sample_far = cand.mv.0.abs() >= 16 || cand.mv.1.abs() >= 16; | |
| 715 | + } | |
| 716 | + add_candidate(&mut candidates, cand.mv, 2); | |
| 717 | + } | |
| 718 | + blk_col += step_w; | |
| 719 | + } | |
| 720 | + blk_row += step_h; | |
| 721 | + } | |
| 722 | + // Three "extension" samples past the block's own span (libaom | |
| 723 | + // `tpl_sample_pos`), reachable for every 8x8..32x32 block this crate | |
| 724 | + // codes (`allow_extension`), gated on staying inside the block's own | |
| 725 | + // 64x64 superblock (`check_sb_border`). | |
| 726 | + let allow_extension = (2..16).contains(&bh4) && (2..16).contains(&bw4); | |
| 727 | + if allow_extension { | |
| 728 | + let sb_mask = 16isize; | |
| 729 | + for (row_off, col_off) in [ | |
| 730 | + (voffset as isize, -2isize), | |
| 731 | + (voffset as isize, hoffset as isize), | |
| 732 | + (voffset as isize - 2, hoffset as isize), | |
| 733 | + ] { | |
| 734 | + let row = (mi_row as isize & (sb_mask - 1)) + row_off; | |
| 735 | + let col = (mi_col as isize & (sb_mask - 1)) + col_off; | |
| 736 | + if row < 0 || row >= sb_mask || col < 0 || col >= sb_mask { | |
| 737 | + continue; | |
| 738 | + } | |
| 739 | + if let Some(cand) = crate::motion_field::add_tpl_ref_mv( | |
| 740 | + field, | |
| 741 | + mi_row, | |
| 742 | + mi_col, | |
| 743 | + row_off, | |
| 744 | + col_off, | |
| 745 | + cur_offset_0, | |
| 746 | + allow_high_precision_mv, | |
| 747 | + ) { | |
| 748 | + any_hit = true; | |
| 749 | + add_candidate(&mut candidates, cand.mv, 2); | |
| 750 | + } | |
| 751 | + } | |
| 752 | + } | |
| 753 | + if any_hit { | |
| 754 | + TMV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 755 | + } | |
| 756 | + zero_mv_ctx = usize::from(first_sample_missing || first_sample_far); | |
| 757 | + } | |
| 758 | + | |
| 632 | 759 | // The diagonal top-left corner probe (module doc): folds into the row |
| 633 | 760 | // side for `ref_match_count` without moving the already-captured |
| 634 | 761 | // `nearest_match`, and its own newmv-ness is a dummy count libaom |
| @@ -816,10 +943,10 @@ pub fn find_mv_stack_with_sign_bias( | ||
| 816 | 943 | } |
| 817 | 944 | _ => 5, |
| 818 | 945 | }; |
| 819 | - // GLOBALMV_OFFSET is only ever set by temporal MV projection | |
| 820 | - // (`cm->features.allow_ref_frame_mvs`), which this reduction never runs | |
| 821 | - // (see module doc) — exact, not a stand-in. | |
| 822 | - let zero_mv_ctx = 0; | |
| 946 | + // `zero_mv_ctx` was already computed above, from the temporal-MV pass | |
| 947 | + // when `tpl` is `Some` (GLOBALMV_OFFSET, spec 7.10.2.8) — `0` otherwise, | |
| 948 | + // exactly this reduction's old always-0 behaviour when | |
| 949 | + // `use_ref_frame_mvs` is unset. | |
| 823 | 950 | |
| 824 | 951 | // av1_drl_ctx (mvref_common.h): weight was boosted in place above for |
| 825 | 952 | // the nearest entries only, so this now compares the real stored weight |