forge
Explore
Log in

Don't you dare go Hollow

Exact spec 7.10.2.8 mvstack contexts, 7.10.2.14 MV clamping

Browse files at this commit

23d548c3ebc694395626fec2e7c0ae3e1c780b32Ahmet Kaan GümüşParent: 995148f
  • fix
    • exact spec 7.10.2.8 mvstack contexts, 7.10.2.14 MV clamping
Show full message
fix(av1): exact spec 7.10.2.8 mvstack contexts, 7.10.2.14 MV clamping

Replaces the documented monotonic stand-in NewMvContext/RefMvContext/
ZeroMvContext/DrlCtx with an exact port of libaom's av1_find_mv_stack
mode_context switch and av1_drl_ctx (mvref_common.c/.h), which collapse
to closed forms given this module's already-reduced scan (no extended
row/col offsets, no temporal MV projection): ref_match_count ==
nearest_match always, so drl_ctx is always 0 (every candidate gets the
REF_CAT_LEVEL nearest-bonus), and zero_mv_ctx is always 0 (GLOBALMV_OFFSET
is only ever set by temporal MV projection). Adds MiInfo::is_new_mv so
new_mv_ctx can read whether a neighbour was itself coded NEWMV, matching
libaom's newmv_count. Adds clamp_mv_ref (7.10.2.14) applied to
nearest_mv/near_mv/pred_mv, requiring find_mv_stack's new mi_cols/mi_rows
params. Adds single_ref_ctx: the LAST-only reduction collapses libaom's
three single_ref_p1/p3/p4 context functions to one two-valued test since
LAST2/LAST3/GOLDEN/backward counts are always zero here.

Ledger: does not include the inter tile writer (ec-av1 tile.rs) this
lane's charter also scoped -- see HANDOFF for the exact block-writer
design (symbol order, size groups, MV component coding) worked out but
not yet landed.
crates/ec-av1/src/mvstack.rsmodified+225 -43
@@ -5,20 +5,36 @@
5 //! Implemented from the spec: the row-above scan (7.10.2.2), the col-left5 //! Implemented from the spec: the row-above scan (7.10.2.2), the col-left
6 //! scan (7.10.2.3), a single top-right probe (the first of 7.10.2.4's extra6 //! scan (7.10.2.3), a single top-right probe (the first of 7.10.2.4's extra
7 //! positions), weight-based dedupe (7.10.2.5's `add_ref_mv_candidate`), the7 //! positions), weight-based dedupe (7.10.2.5's `add_ref_mv_candidate`), the
8-//! weight-descending sort (7.10.2.6), and the DRL context derivation from8+//! weight-descending sort (7.10.2.6), and the `NewMvContext`/`RefMvContext`/
9-//! `REF_CAT_LEVEL` (7.10.2.8, the one piece of that section whose constant9+//! `ZeroMvContext`/`DrlCtx` derivations of 7.10.2.8, ported from libaom's
10-//! and shape is unambiguous from the spec text).10+//! `av1_find_mv_stack` (`av1/common/mvref_common.c`, the `mode_context`
11+//! switch around its `nearest_match`/`ref_match_count` and the
12+//! `newmv_count > 0` tests) and `av1_drl_ctx` (`mvref_common.h`), plus MV
13+//! clamping (7.10.2.14, libaom's `clamp_mv_ref`).
14+//!
15+//! This module's scan is already reduced to the immediate row/col/top-right
16+//! positions with no extended-offset scan (see below), which lets those
17+//! context formulas collapse exactly rather than approximately: libaom's
18+//! `ref_match_count` comes from the *same* row/col match counters as
19+//! `nearest_match` once the extended scan is gone, so `ref_match_count ==
20+//! nearest_match` always here, and every candidate this module finds is one
21+//! of libaom's "nearest" ones, so the `REF_CAT_LEVEL` (640) bonus
22+//! `av1_find_mv_stack` adds to nearest candidates before computing DRL
23+//! context applies to all of them uniformly — which is why [`find_mv_stack`]
24+//! always reports `drl_ctx == 0`: with the extended scan absent, no
25+//! candidate here can ever be a "non-nearest" (sub-640) one for a pair
26+//! comparison to distinguish. Likewise, the `GLOBALMV_OFFSET` bit of
27+//! libaom's `mode_context` is set only inside
28+//! `cm->features.allow_ref_frame_mvs` (temporal MV projection), which this
29+//! module never has (`use_ref_frame_mvs = 0`), so `zero_mv_ctx` is always 0
30+//! here — again exact for the case this module covers, not a stand-in.
11 //!31 //!
12 //! Deliberately reduced away (see the crate report, not reproduced bit-exact32 //! Deliberately reduced away (see the crate report, not reproduced bit-exact
13 //! here): compound/multi-reference candidates, the temporal MV projection33 //! here): compound/multi-reference candidates, the temporal MV projection
14-//! scan, the `>1` row/col scans at offset 3 and their skip-by-block-size34+//! scan, and the `>1` row/col scans at offset 3 and their skip-by-block-size
15-//! stepping, and the exact `NewMvContext`/`RefMvContext`/`ZeroMvContext`35+//! stepping (which is also why the "extra search" that pads a short stack to
16-//! tables — spec 7.10.2.8 derives those through several more match classes36+//! two entries, spec 7.10.2.12, is not implemented — this module's stack can
17-//! than this reduction tracks, and nothing in this crate consumes them yet37+//! be shorter than two candidates where libaom's never is).
18-//! (no CDF wiring), so the formulas below are a documented, monotonic
19-//! stand-in rather than a proven port. Also reduced away: clamping
20-//! `NearestMv`/`NearMv`/`PredMv` to the frame's allowed MV range
21-//! (7.10.2.14) — the caller gets unclamped values.
22 38
23 /// One 4x4 `mi` unit's motion state, as the encode loop will have filled it39 /// One 4x4 `mi` unit's motion state, as the encode loop will have filled it
24 /// in by the time it asks for a block's MV stack.40 /// in by the time it asks for a block's MV stack.
@@ -31,6 +47,11 @@ pub struct MiInfo {
31 pub ref_frame: i8,47 pub ref_frame: i8,
32 /// The unit's motion vector, `(row, col)`, in the spec's 1/8-pel units.48 /// The unit's motion vector, `(row, col)`, in the spec's 1/8-pel units.
33 pub mv: (i32, i32),49 pub mv: (i32, i32),
50+ /// Whether this unit's own mode was `NEWMV` (spec's
51+ /// `have_newmv_in_inter_mode`, compound modes excluded since this module
52+ /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a
53+ /// neighbour's own coded-with-NEWMV state, not this block's.
54+ pub is_new_mv: bool,
34 }55 }
35 56
36 /// The whole frame's `mi` grid, one [`MiInfo`] per 4x4 unit, in raster order.57 /// The whole frame's `mi` grid, one [`MiInfo`] per 4x4 unit, in raster order.
@@ -142,6 +163,7 @@ fn scan_row(
142 bw4: usize,163 bw4: usize,
143 ref_frame: i8,164 ref_frame: i8,
144 candidates: &mut Vec<StackEntry>,165 candidates: &mut Vec<StackEntry>,
166+ newmv_count: &mut u32,
145 ) -> bool {167 ) -> bool {
146 let Some(row) = mi_row.checked_sub(1) else {168 let Some(row) = mi_row.checked_sub(1) else {
147 return false;169 return false;
@@ -154,6 +176,7 @@ fn scan_row(
154 {176 {
155 found = true;177 found = true;
156 add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT);178 add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT);
179+ *newmv_count += u32::from(info.is_new_mv);
157 }180 }
158 }181 }
159 found182 found
@@ -168,6 +191,7 @@ fn scan_col(
168 bh4: usize,191 bh4: usize,
169 ref_frame: i8,192 ref_frame: i8,
170 candidates: &mut Vec<StackEntry>,193 candidates: &mut Vec<StackEntry>,
194+ newmv_count: &mut u32,
171 ) -> bool {195 ) -> bool {
172 let Some(col) = mi_col.checked_sub(1) else {196 let Some(col) = mi_col.checked_sub(1) else {
173 return false;197 return false;
@@ -180,6 +204,7 @@ fn scan_col(
180 {204 {
181 found = true;205 found = true;
182 add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT);206 add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT);
207+ *newmv_count += u32::from(info.is_new_mv);
183 }208 }
184 }209 }
185 found210 found
@@ -198,6 +223,7 @@ fn scan_top_right(
198 bw4: usize,223 bw4: usize,
199 ref_frame: i8,224 ref_frame: i8,
200 candidates: &mut Vec<StackEntry>,225 candidates: &mut Vec<StackEntry>,
226+ newmv_count: &mut u32,
201 ) -> bool {227 ) -> bool {
202 let Some(row) = mi_row.checked_sub(1) else {228 let Some(row) = mi_row.checked_sub(1) else {
203 return false;229 return false;
@@ -208,13 +234,50 @@ fn scan_top_right(
208 && info.ref_frame == ref_frame234 && info.ref_frame == ref_frame
209 {235 {
210 add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT);236 add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT);
237+ *newmv_count += u32::from(info.is_new_mv);
211 return true;238 return true;
212 }239 }
213 false240 false
214 }241 }
215 242
243+/// `MV_BORDER` (spec 7.10.2.14): 16 pixels of slack in 1/8-pel units, added
244+/// on top of the block's own span before a candidate is clamped to the
245+/// frame.
246+const MV_BORDER: i32 = 16 << 3;
247+
248+/// Clamps one candidate MV to the range spec 7.10.2.14 (libaom's
249+/// `clamp_mv_ref`) allows for a `bw4`-by-`bh4` block at `(mi_row, mi_col)` in
250+/// a `mi_cols`-by-`mi_rows` frame: the distance from the block's own edge to
251+/// the matching frame edge, plus the block's own span and [`MV_BORDER`] of
252+/// slack, in every direction.
253+fn clamp_mv_ref(
254+ mv: (i32, i32),
255+ mi_row: usize,
256+ mi_col: usize,
257+ bw4: usize,
258+ bh4: usize,
259+ mi_cols: usize,
260+ mi_rows: usize,
261+) -> (i32, i32) {
262+ // Pixels-to-1/8-pel: 4 mi units/pixel-row * 8 subpel steps/pixel.
263+ let to_subpel = |mi_units: usize| (mi_units as i32) * 4 * 8;
264+ let (bw8, bh8) = (to_subpel(bw4), to_subpel(bh4));
265+ let to_left = -to_subpel(mi_col);
266+ let to_right = to_subpel(mi_cols.saturating_sub(bw4).saturating_sub(mi_col));
267+ let to_top = -to_subpel(mi_row);
268+ let to_bottom = to_subpel(mi_rows.saturating_sub(bh4).saturating_sub(mi_row));
269+
270+ let (row, col) = mv;
271+ (
272+ row.clamp(to_top - bh8 - MV_BORDER, to_bottom + bh8 + MV_BORDER),
273+ col.clamp(to_left - bw8 - MV_BORDER, to_right + bw8 + MV_BORDER),
274+ )
275+}
276+
216 /// Builds the reference MV stack for the `bw4`-by-`bh4` (in 4x4 units)277 /// Builds the reference MV stack for the `bw4`-by-`bh4` (in 4x4 units)
217-/// block at `(mi_row, mi_col)`, predicting against `ref_frame`.278+/// block at `(mi_row, mi_col)`, predicting against `ref_frame`, in a frame
279+/// that is `mi_cols`-by-`mi_rows` 4x4 units (used only to clamp the
280+/// predictors to the frame, spec 7.10.2.14).
218 pub fn find_mv_stack(281 pub fn find_mv_stack(
219 grid: &MiGrid,282 grid: &MiGrid,
220 mi_row: usize,283 mi_row: usize,
@@ -222,49 +285,105 @@ pub fn find_mv_stack(
222 bw4: usize,285 bw4: usize,
223 bh4: usize,286 bh4: usize,
224 ref_frame: i8,287 ref_frame: i8,
288+ mi_cols: usize,
289+ mi_rows: usize,
225 ) -> MvStack {290 ) -> MvStack {
226 let mut candidates = Vec::new();291 let mut candidates = Vec::new();
227- let found_above = scan_row(grid, mi_row, mi_col, bw4, ref_frame, &mut candidates);292+ let mut newmv_count = 0u32;
228- let found_left = scan_col(grid, mi_row, mi_col, bh4, ref_frame, &mut candidates);293+ let found_above = scan_row(
229- let found_top_right = scan_top_right(grid, mi_row, mi_col, bw4, ref_frame, &mut candidates);294+ grid,
295+ mi_row,
296+ mi_col,
297+ bw4,
298+ ref_frame,
299+ &mut candidates,
300+ &mut newmv_count,
301+ );
302+ let found_left = scan_col(
303+ grid,
304+ mi_row,
305+ mi_col,
306+ bh4,
307+ ref_frame,
308+ &mut candidates,
309+ &mut newmv_count,
310+ );
311+ let found_top_right = scan_top_right(
312+ grid,
313+ mi_row,
314+ mi_col,
315+ bw4,
316+ ref_frame,
317+ &mut candidates,
318+ &mut newmv_count,
319+ );
230 320
231 // Highest weight first; a stable sort keeps scan order (above, left,321 // Highest weight first; a stable sort keeps scan order (above, left,
232 // top-right) among ties, matching spec 7.10.2.6.322 // top-right) among ties, matching spec 7.10.2.6.
233 candidates.sort_by_key(|e| std::cmp::Reverse(e.weight));323 candidates.sort_by_key(|e| std::cmp::Reverse(e.weight));
234 candidates.truncate(MAX_STACK_SIZE);324 candidates.truncate(MAX_STACK_SIZE);
235 325
236- let nearest_mv = candidates.first().map_or((0, 0), |e| e.mv);326+ let clamp = |mv| clamp_mv_ref(mv, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows);
237- let near_mv = candidates.get(1).map_or((0, 0), |e| e.mv);327+ let nearest_mv = candidates.first().map_or((0, 0), |e| clamp(e.mv));
328+ let near_mv = candidates.get(1).map_or((0, 0), |e| clamp(e.mv));
238 let pred_mv = if candidates.is_empty() {329 let pred_mv = if candidates.is_empty() {
239 (0, 0)330 (0, 0)
240 } else {331 } else {
241 nearest_mv332 nearest_mv
242 };333 };
243 334
244- let close_matches = usize::from(found_above) + usize::from(found_left);335+ // libaom's `row_match_count`/`col_match_count` (mvref_common.c) fold the
245- let total_matches = close_matches + usize::from(found_top_right);336+ // top-right probe into the row side, so "found above" for context
246-337+ // purposes means the row scan *or* the top-right one matched.
247- // Reduced context tables (see module doc): monotonic in how much338+ let row_matched = found_above || found_top_right;
248- // neighbour agreement was found, not a bit-exact port of 7.10.2.8.339+ let nearest_match = usize::from(row_matched) + usize::from(found_left);
249- let new_mv_ctx = match (found_above, found_left, candidates.len()) {340+
250- (false, false, _) => 0,341+ // Exact port of the `mode_context[ref_frame]` switch (mvref_common.c
251- (true, false, _) | (false, true, _) => 1,342+ // ~line 619-643): with no extended-offset scan, `ref_match_count` here is
252- (true, true, 1) => 2,343+ // always equal to `nearest_match` (both come from the same row/col match
253- (true, true, _) => 3,344+ // counters this module never advances further), so the `ref_match_count`
345+ // branches of libaom's switch collapse into the constants below — see
346+ // module doc.
347+ let new_mv_ctx = match nearest_match {
348+ 0 => 0,
349+ 1 => {
350+ if newmv_count > 0 {
351+ 2
352+ } else {
353+ 3
354+ }
355+ }
356+ _ => {
357+ if newmv_count >= 1 {
358+ 4
359+ } else {
360+ 5
361+ }
362+ }
254 };363 };
255- let ref_mv_ctx = total_matches.min(5);364+ let ref_mv_ctx = match nearest_match {
256- let zero_mv_ctx = usize::from(nearest_mv != (0, 0));365+ 0 => 0,
257-366+ 1 => 3,
367+ _ => 5,
368+ };
369+ // GLOBALMV_OFFSET is only ever set by temporal MV projection
370+ // (`cm->features.allow_ref_frame_mvs`), which this reduction never runs
371+ // (see module doc) — exact, not a stand-in.
372+ let zero_mv_ctx = 0;
373+
374+ // av1_drl_ctx (mvref_common.h): every candidate here is one of libaom's
375+ // "nearest" ones (see module doc), so each gets `REF_CAT_LEVEL` before
376+ // the comparison, which is why this always lands on 0.
258 let drl_ctx = candidates377 let drl_ctx = candidates
259 .windows(2)378 .windows(2)
260 .map(|w| {379 .map(|w| {
261- let (a, b) = (w[0].weight, w[1].weight);380+ let (a, b) = (w[0].weight + REF_CAT_LEVEL, w[1].weight + REF_CAT_LEVEL);
262- if a >= REF_CAT_LEVEL && b < REF_CAT_LEVEL {381+ if a >= REF_CAT_LEVEL && b >= REF_CAT_LEVEL {
382+ 0
383+ } else if a >= REF_CAT_LEVEL && b < REF_CAT_LEVEL {
263 1384 1
264- } else if a < REF_CAT_LEVEL && b < REF_CAT_LEVEL {
265- 2
266 } else {385 } else {
267- 0386+ 2
268 }387 }
269 })388 })
270 .collect();389 .collect();
@@ -281,6 +400,20 @@ pub fn find_mv_stack(
281 }400 }
282 }401 }
283 402
403+/// The single-reference context shared by the `single_ref_p1`/`p3`/`p4`
404+/// binary decisions this crate's LAST-only reference chain codes (spec
405+/// 5.11.25; libaom's `av1_get_pred_context_single_ref_p1/p3/p4`,
406+/// `pred_common.c`). Each of those three functions compares a forward-ref
407+/// neighbour count against a same-direction count that is always zero in a
408+/// LAST-only world (no LAST2/LAST3/GOLDEN/backward candidate ever exists
409+/// here), which collapses all three of libaom's formulas to the same
410+/// two-valued test: "is there an inter neighbour at all". `has_inter`
411+/// gathers that over the block's whole span, the way spec 5.11.39 reads any
412+/// neighbour-derived context.
413+pub(crate) fn single_ref_ctx(has_inter_neighbour: bool) -> usize {
414+ if has_inter_neighbour { 2 } else { 1 }
415+}
416+
284 #[cfg(test)]417 #[cfg(test)]
285 mod tests {418 mod tests {
286 use super::*;419 use super::*;
@@ -290,6 +423,7 @@ mod tests {
290 is_inter: true,423 is_inter: true,
291 ref_frame: 1,424 ref_frame: 1,
292 mv,425 mv,
426+ is_new_mv: false,
293 }427 }
294 }428 }
295 429
@@ -305,16 +439,19 @@ mod tests {
305 grid.set(3, 1, inter(mv));439 grid.set(3, 1, inter(mv));
306 grid.set(1, 4, inter(mv));440 grid.set(1, 4, inter(mv));
307 441
308- let stack = find_mv_stack(&grid, 2, 2, 2, 2, 1);442+ let stack = find_mv_stack(&grid, 2, 2, 2, 2, 1, 8, 8);
309 443
310 assert_eq!(stack.entries.len(), 1);444 assert_eq!(stack.entries.len(), 1);
311 assert_eq!(stack.entries[0].mv, mv);445 assert_eq!(stack.entries[0].mv, mv);
312 // 2 above cells + 2 left cells + 1 top-right, each weight 2.446 // 2 above cells + 2 left cells + 1 top-right, each weight 2.
313 assert_eq!(stack.entries[0].weight, 10);447 assert_eq!(stack.entries[0].weight, 10);
314 assert_eq!(stack.nearest_mv, mv);448 assert_eq!(stack.nearest_mv, mv);
315- // Documented reduced-context value (see module doc): both sides449+ // Exact ctx (see module doc): both sides matched (nearest_match=2)
316- // found a match and they collapsed to a single stack entry.450+ // and no neighbour was coded NEWMV, which is libaom's
317- assert_eq!(stack.new_mv_ctx, 2);451+ // `mode_context[ref_frame] |= 5` branch.
452+ assert_eq!(stack.new_mv_ctx, 5);
453+ assert_eq!(stack.ref_mv_ctx, 5);
454+ assert_eq!(stack.zero_mv_ctx, 0);
318 }455 }
319 456
320 #[test]457 #[test]
@@ -330,7 +467,7 @@ mod tests {
330 grid.set(4, 2, inter(left_mv));467 grid.set(4, 2, inter(left_mv));
331 grid.set(5, 2, inter(left_mv));468 grid.set(5, 2, inter(left_mv));
332 469
333- let stack = find_mv_stack(&grid, 3, 3, 2, 3, 1);470+ let stack = find_mv_stack(&grid, 3, 3, 2, 3, 1, 8, 8);
334 471
335 assert_eq!(stack.entries.len(), 2);472 assert_eq!(stack.entries.len(), 2);
336 assert_eq!(473 assert_eq!(
@@ -362,7 +499,7 @@ mod tests {
362 a.set(2, 1, inter((8, 8)));499 a.set(2, 1, inter((8, 8)));
363 a.set(3, 1, inter((8, 8)));500 a.set(3, 1, inter((8, 8)));
364 a.set(1, 4, inter((4, 4)));501 a.set(1, 4, inter((4, 4)));
365- let stack_a = find_mv_stack(&a, 2, 2, 2, 2, 1);502+ let stack_a = find_mv_stack(&a, 2, 2, 2, 2, 1, 8, 8);
366 503
367 // Grid B: the naive transpose — swap which MV sits above vs left —504 // Grid B: the naive transpose — swap which MV sits above vs left —
368 // with the top-right probe again matching whatever now sits above.505 // with the top-right probe again matching whatever now sits above.
@@ -372,7 +509,7 @@ mod tests {
372 b.set(2, 1, inter((4, 4)));509 b.set(2, 1, inter((4, 4)));
373 b.set(3, 1, inter((4, 4)));510 b.set(3, 1, inter((4, 4)));
374 b.set(1, 4, inter((8, 8)));511 b.set(1, 4, inter((8, 8)));
375- let stack_b = find_mv_stack(&b, 2, 2, 2, 2, 1);512+ let stack_b = find_mv_stack(&b, 2, 2, 2, 2, 1, 8, 8);
376 513
377 assert_ne!(stack_a, stack_b);514 assert_ne!(stack_a, stack_b);
378 assert_eq!(stack_a.entries[0].mv, (4, 4));515 assert_eq!(stack_a.entries[0].mv, (4, 4));
@@ -384,16 +521,61 @@ mod tests {
384 let grid = MiGrid::new(4, 4);521 let grid = MiGrid::new(4, 4);
385 // Block at the tile's top-left corner: no row above, no col left,522 // Block at the tile's top-left corner: no row above, no col left,
386 // and the top-right probe is also off the top edge.523 // and the top-right probe is also off the top edge.
387- let stack = find_mv_stack(&grid, 0, 0, 2, 2, 1);524+ let stack = find_mv_stack(&grid, 0, 0, 2, 2, 1, 4, 4);
388 525
389 assert!(stack.entries.is_empty());526 assert!(stack.entries.is_empty());
390 assert_eq!(stack.nearest_mv, (0, 0));527 assert_eq!(stack.nearest_mv, (0, 0));
391 assert_eq!(stack.near_mv, (0, 0));528 assert_eq!(stack.near_mv, (0, 0));
392 assert_eq!(stack.pred_mv, (0, 0));529 assert_eq!(stack.pred_mv, (0, 0));
393 assert!(stack.drl_ctx.is_empty());530 assert!(stack.drl_ctx.is_empty());
531+ assert_eq!(stack.new_mv_ctx, 0);
532+ assert_eq!(stack.ref_mv_ctx, 0);
394 533
395 // Out-of-range reads are also inert directly against the grid.534 // Out-of-range reads are also inert directly against the grid.
396 assert!(grid.get(0, 10).is_none());535 assert!(grid.get(0, 10).is_none());
397 assert!(grid.get(10, 0).is_none());536 assert!(grid.get(10, 0).is_none());
398 }537 }
538+
539+ #[test]
540+ fn a_candidate_far_outside_the_frame_is_clamped_to_it() {
541+ // A 4x4-mi frame (16x16 px), block 1x1 mi at (1, 1): room for one
542+ // neighbour above and one to the left, tight edges on every side.
543+ let mut grid = MiGrid::new(4, 4);
544+ grid.set(0, 1, inter((100_000, 100_000)));
545+
546+ let stack = find_mv_stack(&grid, 1, 1, 1, 1, 1, 4, 4);
547+
548+ // Unclamped this candidate is (100000, 100000); spec 7.10.2.14 caps
549+ // it at the frame edge (2 mi below/right) plus the block's own 4x4
550+ // span plus MV_BORDER (128), all in 1/8-pel: (2*4)*8 + 1*4*8 + 128 =
551+ // 64 + 32 + 128 = 224.
552+ assert_eq!(stack.nearest_mv, (224, 224));
553+ assert_ne!(stack.entries[0].mv, stack.nearest_mv);
554+ }
555+
556+ #[test]
557+ fn a_neighbour_coded_newmv_raises_the_new_mv_context() {
558+ let mut grid = MiGrid::new(8, 8);
559+ grid.set(
560+ 1,
561+ 2,
562+ MiInfo {
563+ is_inter: true,
564+ ref_frame: 1,
565+ mv: (4, 4),
566+ is_new_mv: true,
567+ },
568+ );
569+ // Only the row matches (nearest_match == 1): newmv_count > 0 picks
570+ // libaom's `mode_context[ref_frame] |= 2` branch.
571+ let stack = find_mv_stack(&grid, 2, 2, 1, 1, 1, 8, 8);
572+ assert_eq!(stack.new_mv_ctx, 2);
573+ assert_eq!(stack.ref_mv_ctx, 3);
574+ }
575+
576+ #[test]
577+ fn single_ref_ctx_matches_libaoms_collapsed_forward_only_case() {
578+ assert_eq!(single_ref_ctx(false), 1);
579+ assert_eq!(single_ref_ctx(true), 2);
580+ }
399 }581 }