From 23d548c3ebc694395626fec2e7c0ae3e1c780b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Aug 2026 19:47:32 +0300 Subject: [PATCH] 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.rs | 268 +++++++++++++++++++++++++++++------ 1 file changed, 225 insertions(+), 43 deletions(-) diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 752e545f..3533150d 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -5,20 +5,36 @@ //! Implemented from the spec: the row-above scan (7.10.2.2), the col-left //! scan (7.10.2.3), a single top-right probe (the first of 7.10.2.4's extra //! positions), weight-based dedupe (7.10.2.5's `add_ref_mv_candidate`), the -//! weight-descending sort (7.10.2.6), and the DRL context derivation from -//! `REF_CAT_LEVEL` (7.10.2.8, the one piece of that section whose constant -//! and shape is unambiguous from the spec text). +//! weight-descending sort (7.10.2.6), and the `NewMvContext`/`RefMvContext`/ +//! `ZeroMvContext`/`DrlCtx` derivations of 7.10.2.8, ported from libaom's +//! `av1_find_mv_stack` (`av1/common/mvref_common.c`, the `mode_context` +//! switch around its `nearest_match`/`ref_match_count` and the +//! `newmv_count > 0` tests) and `av1_drl_ctx` (`mvref_common.h`), plus MV +//! clamping (7.10.2.14, libaom's `clamp_mv_ref`). +//! +//! This module's scan is already reduced to the immediate row/col/top-right +//! positions with no extended-offset scan (see below), which lets those +//! context formulas collapse exactly rather than approximately: libaom's +//! `ref_match_count` comes from the *same* row/col match counters as +//! `nearest_match` once the extended scan is gone, so `ref_match_count == +//! nearest_match` always here, and every candidate this module finds is one +//! of libaom's "nearest" ones, so the `REF_CAT_LEVEL` (640) bonus +//! `av1_find_mv_stack` adds to nearest candidates before computing DRL +//! context applies to all of them uniformly — which is why [`find_mv_stack`] +//! always reports `drl_ctx == 0`: with the extended scan absent, no +//! candidate here can ever be a "non-nearest" (sub-640) one for a pair +//! comparison to distinguish. Likewise, the `GLOBALMV_OFFSET` bit of +//! libaom's `mode_context` is set only inside +//! `cm->features.allow_ref_frame_mvs` (temporal MV projection), which this +//! module never has (`use_ref_frame_mvs = 0`), so `zero_mv_ctx` is always 0 +//! here — again exact for the case this module covers, not a stand-in. //! //! Deliberately reduced away (see the crate report, not reproduced bit-exact //! here): compound/multi-reference candidates, the temporal MV projection -//! scan, the `>1` row/col scans at offset 3 and their skip-by-block-size -//! stepping, and the exact `NewMvContext`/`RefMvContext`/`ZeroMvContext` -//! tables — spec 7.10.2.8 derives those through several more match classes -//! than this reduction tracks, and nothing in this crate consumes them yet -//! (no CDF wiring), so the formulas below are a documented, monotonic -//! stand-in rather than a proven port. Also reduced away: clamping -//! `NearestMv`/`NearMv`/`PredMv` to the frame's allowed MV range -//! (7.10.2.14) — the caller gets unclamped values. +//! scan, and the `>1` row/col scans at offset 3 and their skip-by-block-size +//! stepping (which is also why the "extra search" that pads a short stack to +//! two entries, spec 7.10.2.12, is not implemented — this module's stack can +//! be shorter than two candidates where libaom's never is). /// 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. @@ -31,6 +47,11 @@ pub struct MiInfo { pub ref_frame: i8, /// The unit's motion vector, `(row, col)`, in the spec's 1/8-pel units. pub mv: (i32, i32), + /// Whether this unit's own mode was `NEWMV` (spec's + /// `have_newmv_in_inter_mode`, compound modes excluded since this module + /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a + /// neighbour's own coded-with-NEWMV state, not this block's. + pub is_new_mv: bool, } /// The whole frame's `mi` grid, one [`MiInfo`] per 4x4 unit, in raster order. @@ -142,6 +163,7 @@ fn scan_row( bw4: usize, ref_frame: i8, candidates: &mut Vec, + newmv_count: &mut u32, ) -> bool { let Some(row) = mi_row.checked_sub(1) else { return false; @@ -154,6 +176,7 @@ fn scan_row( { found = true; add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); + *newmv_count += u32::from(info.is_new_mv); } } found @@ -168,6 +191,7 @@ fn scan_col( bh4: usize, ref_frame: i8, candidates: &mut Vec, + newmv_count: &mut u32, ) -> bool { let Some(col) = mi_col.checked_sub(1) else { return false; @@ -180,6 +204,7 @@ fn scan_col( { found = true; add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); + *newmv_count += u32::from(info.is_new_mv); } } found @@ -198,6 +223,7 @@ fn scan_top_right( bw4: usize, ref_frame: i8, candidates: &mut Vec, + newmv_count: &mut u32, ) -> bool { let Some(row) = mi_row.checked_sub(1) else { return false; @@ -208,13 +234,50 @@ fn scan_top_right( && info.ref_frame == ref_frame { add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); + *newmv_count += u32::from(info.is_new_mv); return true; } false } +/// `MV_BORDER` (spec 7.10.2.14): 16 pixels of slack in 1/8-pel units, added +/// on top of the block's own span before a candidate is clamped to the +/// frame. +const MV_BORDER: i32 = 16 << 3; + +/// Clamps one candidate MV to the range spec 7.10.2.14 (libaom's +/// `clamp_mv_ref`) allows for a `bw4`-by-`bh4` block at `(mi_row, mi_col)` in +/// a `mi_cols`-by-`mi_rows` frame: the distance from the block's own edge to +/// the matching frame edge, plus the block's own span and [`MV_BORDER`] of +/// slack, in every direction. +fn clamp_mv_ref( + mv: (i32, i32), + mi_row: usize, + mi_col: usize, + bw4: usize, + bh4: usize, + mi_cols: usize, + mi_rows: usize, +) -> (i32, i32) { + // Pixels-to-1/8-pel: 4 mi units/pixel-row * 8 subpel steps/pixel. + let to_subpel = |mi_units: usize| (mi_units as i32) * 4 * 8; + let (bw8, bh8) = (to_subpel(bw4), to_subpel(bh4)); + let to_left = -to_subpel(mi_col); + let to_right = to_subpel(mi_cols.saturating_sub(bw4).saturating_sub(mi_col)); + let to_top = -to_subpel(mi_row); + let to_bottom = to_subpel(mi_rows.saturating_sub(bh4).saturating_sub(mi_row)); + + let (row, col) = mv; + ( + row.clamp(to_top - bh8 - MV_BORDER, to_bottom + bh8 + MV_BORDER), + col.clamp(to_left - bw8 - MV_BORDER, to_right + bw8 + MV_BORDER), + ) +} + /// Builds the reference MV stack for the `bw4`-by-`bh4` (in 4x4 units) -/// block at `(mi_row, mi_col)`, predicting against `ref_frame`. +/// block at `(mi_row, mi_col)`, predicting against `ref_frame`, in a frame +/// that is `mi_cols`-by-`mi_rows` 4x4 units (used only to clamp the +/// predictors to the frame, spec 7.10.2.14). pub fn find_mv_stack( grid: &MiGrid, mi_row: usize, @@ -222,49 +285,105 @@ pub fn find_mv_stack( bw4: usize, bh4: usize, ref_frame: i8, + mi_cols: usize, + mi_rows: usize, ) -> MvStack { let mut candidates = Vec::new(); - let found_above = scan_row(grid, mi_row, mi_col, bw4, ref_frame, &mut candidates); - let found_left = scan_col(grid, mi_row, mi_col, bh4, ref_frame, &mut candidates); - let found_top_right = scan_top_right(grid, mi_row, mi_col, bw4, ref_frame, &mut candidates); + let mut newmv_count = 0u32; + let found_above = scan_row( + grid, + mi_row, + mi_col, + bw4, + ref_frame, + &mut candidates, + &mut newmv_count, + ); + let found_left = scan_col( + grid, + mi_row, + mi_col, + bh4, + ref_frame, + &mut candidates, + &mut newmv_count, + ); + let found_top_right = scan_top_right( + grid, + mi_row, + mi_col, + bw4, + ref_frame, + &mut candidates, + &mut newmv_count, + ); // Highest weight first; a stable sort keeps scan order (above, left, // top-right) among ties, matching spec 7.10.2.6. candidates.sort_by_key(|e| std::cmp::Reverse(e.weight)); candidates.truncate(MAX_STACK_SIZE); - let nearest_mv = candidates.first().map_or((0, 0), |e| e.mv); - let near_mv = candidates.get(1).map_or((0, 0), |e| e.mv); + let clamp = |mv| clamp_mv_ref(mv, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows); + let nearest_mv = candidates.first().map_or((0, 0), |e| clamp(e.mv)); + let near_mv = candidates.get(1).map_or((0, 0), |e| clamp(e.mv)); let pred_mv = if candidates.is_empty() { (0, 0) } else { nearest_mv }; - let close_matches = usize::from(found_above) + usize::from(found_left); - let total_matches = close_matches + usize::from(found_top_right); - - // Reduced context tables (see module doc): monotonic in how much - // neighbour agreement was found, not a bit-exact port of 7.10.2.8. - let new_mv_ctx = match (found_above, found_left, candidates.len()) { - (false, false, _) => 0, - (true, false, _) | (false, true, _) => 1, - (true, true, 1) => 2, - (true, true, _) => 3, + // libaom's `row_match_count`/`col_match_count` (mvref_common.c) fold the + // top-right probe into the row side, so "found above" for context + // purposes means the row scan *or* the top-right one matched. + let row_matched = found_above || found_top_right; + let nearest_match = usize::from(row_matched) + usize::from(found_left); + + // Exact port of the `mode_context[ref_frame]` switch (mvref_common.c + // ~line 619-643): with no extended-offset scan, `ref_match_count` here is + // always equal to `nearest_match` (both come from the same row/col match + // counters this module never advances further), so the `ref_match_count` + // branches of libaom's switch collapse into the constants below — see + // module doc. + let new_mv_ctx = match nearest_match { + 0 => 0, + 1 => { + if newmv_count > 0 { + 2 + } else { + 3 + } + } + _ => { + if newmv_count >= 1 { + 4 + } else { + 5 + } + } }; - let ref_mv_ctx = total_matches.min(5); - let zero_mv_ctx = usize::from(nearest_mv != (0, 0)); - + let ref_mv_ctx = match nearest_match { + 0 => 0, + 1 => 3, + _ => 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; + + // av1_drl_ctx (mvref_common.h): every candidate here is one of libaom's + // "nearest" ones (see module doc), so each gets `REF_CAT_LEVEL` before + // the comparison, which is why this always lands on 0. let drl_ctx = candidates .windows(2) .map(|w| { - let (a, b) = (w[0].weight, w[1].weight); - if a >= REF_CAT_LEVEL && b < REF_CAT_LEVEL { + let (a, b) = (w[0].weight + REF_CAT_LEVEL, w[1].weight + REF_CAT_LEVEL); + if a >= REF_CAT_LEVEL && b >= REF_CAT_LEVEL { + 0 + } else if a >= REF_CAT_LEVEL && b < REF_CAT_LEVEL { 1 - } else if a < REF_CAT_LEVEL && b < REF_CAT_LEVEL { - 2 } else { - 0 + 2 } }) .collect(); @@ -281,6 +400,20 @@ pub fn find_mv_stack( } } +/// The single-reference context shared by the `single_ref_p1`/`p3`/`p4` +/// binary decisions this crate's LAST-only reference chain codes (spec +/// 5.11.25; libaom's `av1_get_pred_context_single_ref_p1/p3/p4`, +/// `pred_common.c`). Each of those three functions compares a forward-ref +/// neighbour count against a same-direction count that is always zero in a +/// LAST-only world (no LAST2/LAST3/GOLDEN/backward candidate ever exists +/// here), which collapses all three of libaom's formulas to the same +/// two-valued test: "is there an inter neighbour at all". `has_inter` +/// gathers that over the block's whole span, the way spec 5.11.39 reads any +/// neighbour-derived context. +pub(crate) fn single_ref_ctx(has_inter_neighbour: bool) -> usize { + if has_inter_neighbour { 2 } else { 1 } +} + #[cfg(test)] mod tests { use super::*; @@ -290,6 +423,7 @@ mod tests { is_inter: true, ref_frame: 1, mv, + is_new_mv: false, } } @@ -305,16 +439,19 @@ mod tests { grid.set(3, 1, inter(mv)); grid.set(1, 4, inter(mv)); - let stack = find_mv_stack(&grid, 2, 2, 2, 2, 1); + let stack = find_mv_stack(&grid, 2, 2, 2, 2, 1, 8, 8); assert_eq!(stack.entries.len(), 1); assert_eq!(stack.entries[0].mv, mv); // 2 above cells + 2 left cells + 1 top-right, each weight 2. assert_eq!(stack.entries[0].weight, 10); assert_eq!(stack.nearest_mv, mv); - // Documented reduced-context value (see module doc): both sides - // found a match and they collapsed to a single stack entry. - assert_eq!(stack.new_mv_ctx, 2); + // Exact ctx (see module doc): both sides matched (nearest_match=2) + // and no neighbour was coded NEWMV, which is libaom's + // `mode_context[ref_frame] |= 5` branch. + assert_eq!(stack.new_mv_ctx, 5); + assert_eq!(stack.ref_mv_ctx, 5); + assert_eq!(stack.zero_mv_ctx, 0); } #[test] @@ -330,7 +467,7 @@ mod tests { grid.set(4, 2, inter(left_mv)); grid.set(5, 2, inter(left_mv)); - let stack = find_mv_stack(&grid, 3, 3, 2, 3, 1); + let stack = find_mv_stack(&grid, 3, 3, 2, 3, 1, 8, 8); assert_eq!(stack.entries.len(), 2); assert_eq!( @@ -362,7 +499,7 @@ mod tests { a.set(2, 1, inter((8, 8))); a.set(3, 1, inter((8, 8))); a.set(1, 4, inter((4, 4))); - let stack_a = find_mv_stack(&a, 2, 2, 2, 2, 1); + let stack_a = find_mv_stack(&a, 2, 2, 2, 2, 1, 8, 8); // Grid B: the naive transpose — swap which MV sits above vs left — // with the top-right probe again matching whatever now sits above. @@ -372,7 +509,7 @@ mod tests { b.set(2, 1, inter((4, 4))); b.set(3, 1, inter((4, 4))); b.set(1, 4, inter((8, 8))); - let stack_b = find_mv_stack(&b, 2, 2, 2, 2, 1); + let stack_b = find_mv_stack(&b, 2, 2, 2, 2, 1, 8, 8); assert_ne!(stack_a, stack_b); assert_eq!(stack_a.entries[0].mv, (4, 4)); @@ -384,16 +521,61 @@ mod tests { let grid = MiGrid::new(4, 4); // Block at the tile's top-left corner: no row above, no col left, // and the top-right probe is also off the top edge. - let stack = find_mv_stack(&grid, 0, 0, 2, 2, 1); + let stack = find_mv_stack(&grid, 0, 0, 2, 2, 1, 4, 4); assert!(stack.entries.is_empty()); assert_eq!(stack.nearest_mv, (0, 0)); assert_eq!(stack.near_mv, (0, 0)); assert_eq!(stack.pred_mv, (0, 0)); assert!(stack.drl_ctx.is_empty()); + assert_eq!(stack.new_mv_ctx, 0); + assert_eq!(stack.ref_mv_ctx, 0); // Out-of-range reads are also inert directly against the grid. assert!(grid.get(0, 10).is_none()); assert!(grid.get(10, 0).is_none()); } + + #[test] + fn a_candidate_far_outside_the_frame_is_clamped_to_it() { + // A 4x4-mi frame (16x16 px), block 1x1 mi at (1, 1): room for one + // neighbour above and one to the left, tight edges on every side. + let mut grid = MiGrid::new(4, 4); + grid.set(0, 1, inter((100_000, 100_000))); + + let stack = find_mv_stack(&grid, 1, 1, 1, 1, 1, 4, 4); + + // Unclamped this candidate is (100000, 100000); spec 7.10.2.14 caps + // it at the frame edge (2 mi below/right) plus the block's own 4x4 + // span plus MV_BORDER (128), all in 1/8-pel: (2*4)*8 + 1*4*8 + 128 = + // 64 + 32 + 128 = 224. + assert_eq!(stack.nearest_mv, (224, 224)); + assert_ne!(stack.entries[0].mv, stack.nearest_mv); + } + + #[test] + fn a_neighbour_coded_newmv_raises_the_new_mv_context() { + let mut grid = MiGrid::new(8, 8); + grid.set( + 1, + 2, + MiInfo { + is_inter: true, + ref_frame: 1, + mv: (4, 4), + is_new_mv: true, + }, + ); + // Only the row matches (nearest_match == 1): newmv_count > 0 picks + // libaom's `mode_context[ref_frame] |= 2` branch. + let stack = find_mv_stack(&grid, 2, 2, 1, 1, 1, 8, 8); + assert_eq!(stack.new_mv_ctx, 2); + assert_eq!(stack.ref_mv_ctx, 3); + } + + #[test] + fn single_ref_ctx_matches_libaoms_collapsed_forward_only_case() { + assert_eq!(single_ref_ctx(false), 1); + assert_eq!(single_ref_ctx(true), 2); + } }