forge
Explore
Log in

Don't you dare go Hollow

New_mv_ctx case-0 takes the corner-derived ref_match_count bit

Browse files at this commit

d5bf53be343c59f50077f5a52b0cc3bfe8ae0b84Ahmet Kaan GümüşParent: ec4c3f6
  • fix
    • new_mv_ctx case-0 takes the corner-derived ref_match_count bit
Show full message
fix(av1): new_mv_ctx case-0 takes the corner-derived ref_match_count bit

Real-motion content desynced from inter frame 3-4 at aligned sizes: a block
whose only neighbour match is the diagonal corner has nearest_match == 0
but ref_match_count == 1, and libaom sets mode_context |= 1 there
(mvref_common.c ~621) while our arm hardcoded 0 — wrong NEWMV CDF row,
rng drift, decoder rejects the tile. Sibling of the ref_mv_ctx corner fix;
zero_mv_ctx checked, no such dependency. New test pins the corner-only-match
shape. Both I8 failing clips re-encode to 10/10 clean aomdec+ffmpeg decodes,
byte-exact vs reconstruction.
crates/ec-av1/src/mvstack.rsmodified+46 -15
@@ -13,23 +13,29 @@
1313 //! clamping (7.10.2.14, libaom's `clamp_mv_ref`).
1414 //!
1515 //! This module's immediate row/col/top-right scan alone is *not* enough to
16-//! get `RefMvContext` (`ref_mv_ctx`) right, even though it *is* enough for
17-//! `NewMvContext`/`ZeroMvContext`. Ported straight from libaom's
16+//! get `RefMvContext` (`ref_mv_ctx`) right, *nor* `NewMvContext` at
17+//! `nearest_match == 0` (see below). Ported straight from libaom's
1818 //! `av1_find_mv_stack` (`av1/common/mvref_common.c`, `setup_ref_mv_list`,
1919 //! ~line 480 on): after the immediate row (-1), col (-1) and top-right
2020 //! probes settle `nearest_match` (and `mode_context`'s `new_mv` bits, which
21-//! depend on `nearest_match` alone), libaom runs one more probe *before*
22-//! computing `ref_match_count` — `scan_blk_mbmi` at the diagonal top-left
23-//! corner `(-1, -1)` — whose match folds into `row_match_count` (and hence
24-//! can flip `ref_match_count`'s row term true) without ever touching the
25-//! already-captured `nearest_match`. So `ref_match_count >= nearest_match`
26-//! always, sometimes strictly greater, and `ref_mv_ctx` (the `REFMV_OFFSET`
27-//! half of libaom's `mode_context` switch, mvref_common.c ~line 619-643)
28-//! genuinely depends on both counts, not `nearest_match` alone — that
29-//! collapse was the bug this module shipped with for several rounds (see
30-//! the crate report). [`find_mv_stack`] now runs that corner probe and
31-//! computes `ref_mv_ctx` from the true `(nearest_match, ref_match_count)`
32-//! pair.
21+//! for `nearest_match >= 1` depend on `nearest_match` alone), libaom runs one
22+//! more probe *before* computing `ref_match_count` — `scan_blk_mbmi` at the
23+//! diagonal top-left corner `(-1, -1)` — whose match folds into
24+//! `row_match_count` (and hence can flip `ref_match_count`'s row term true)
25+//! without ever touching the already-captured `nearest_match`. So
26+//! `ref_match_count >= nearest_match` always, sometimes strictly greater, and
27+//! `ref_mv_ctx` (the `REFMV_OFFSET` half of libaom's `mode_context` switch,
28+//! mvref_common.c ~line 619-643) genuinely depends on both counts, not
29+//! `nearest_match` alone — that collapse was the bug this module shipped
30+//! with for several rounds (see the crate report). The `case 0` arm of that
31+//! same switch *also* sets `new_mv_ctx`'s own bit from `ref_match_count`
32+//! (`mode_context[ref_frame] |= 1` when `ref_match_count >= 1`) — a second
33+//! corner-probe dependency this module missed for several more rounds after
34+//! the first fix, until real-content inter streams (I8 CLASS B) caught it:
35+//! synthetic fixtures never produced a block with `nearest_match == 0` and a
36+//! genuine corner match. [`find_mv_stack`] now runs that corner probe and
37+//! computes both `ref_mv_ctx` and `new_mv_ctx` from the true
38+//! `(nearest_match, ref_match_count)` pair.
3339 //!
3440 //! The corner probe's own candidate is added to the stack too (matching
3541 //! libaom: a genuinely new MV there becomes a real, if low-weight, entry),
@@ -434,7 +440,12 @@ pub fn find_mv_stack(
434440 // `newmv_count` alone — libaom's switch never reads `ref_match_count`
435441 // for those bits, so this half was already exact before this round.
436442 let new_mv_ctx = match nearest_match {
437- 0 => 0,
443+ // libaom's `case 0` also sets this bit from `ref_match_count`
444+ // (`mode_context[ref_frame] |= 1` when `ref_match_count >= 1`,
445+ // mvref_common.c ~line 621) — the corner-probe-derived count, not
446+ // `nearest_match` alone, despite this module's doc comment above
447+ // having claimed the opposite for several rounds.
448+ 0 => usize::from(ref_match_count >= 1),
438449 1 => {
439450 if newmv_count > 0 {
440451 2
@@ -706,6 +717,26 @@ mod tests {
706717 assert_eq!(stack.drl_ctx, vec![1]);
707718 }
708719
720+ #[test]
721+ fn a_corner_only_match_raises_new_mv_ctx_even_when_nearest_match_is_zero() {
722+ // Block at (2, 2), 1x1 mi unit: row above (1, 2), col left (2, 1)
723+ // and top-right (1, 3) all empty — nearest_match == 0 — but the
724+ // diagonal top-left corner (1, 1) matches. libaom's `case 0` arm
725+ // (mvref_common.c ~line 621) sets `new_mv_ctx`'s own bit from
726+ // `ref_match_count` here, not just `ref_mv_ctx`'s: this is I8 CLASS
727+ // B's real-content bitstream desync (real clips hit blocks whose
728+ // *only* neighbour match is diagonal; the repo's synthetic tests
729+ // never had one until this case).
730+ let mut grid = MiGrid::new(8, 8);
731+ grid.set(1, 1, inter((4, 4)));
732+
733+ let stack = find_mv_stack(&grid, 2, 2, 1, 1, 1, 8, 8);
734+
735+ assert_eq!(stack.entries.len(), 1);
736+ assert_eq!(stack.new_mv_ctx, 1); // ref_match_count == 1, not the old hard-0
737+ assert_eq!(stack.ref_mv_ctx, 1); // nearest_match=0, ref_match_count=1
738+ }
739+
709740 #[test]
710741 fn single_ref_ctx_matches_libaoms_collapsed_forward_only_case() {
711742 assert_eq!(single_ref_ctx(false), 1);