From 0c17efcbf667c824c71390ee7122339bba8962cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 11:51:35 +0300 Subject: [PATCH] fix(av1): temporal MV candidates ignored force_integer_mv `lower_mv_precision` implemented only its `is_integer == false` half, on a comment claiming no reachable stream sets `force_integer_mv` because `allow_screen_content_tools` is refused at the stream level. Neither half of that claim holds: libaom turns screen-content tools on for dark, smooth FILM (`av1_set_screen_content_options` counts near-flat 16x16 blocks) and then sets `force_integer_mv` on the frames its hash test calls static, and this decoder parses such streams without refusing anything. The consequence was invisible to the parse: every temporal MV candidate came out at eighth-pel where libaom rounds it to full pel, so the mv stack held values libaom never produced. The stack still had the right shape for a while, so the frame decoded "successfully" into a whole-frame prediction mismatch, and the entropy desync that followed surfaced as the unrelated refusal "a Golomb tail longer than this decoder reads" (class `refusal-names-a-correlate`). First divergence, measured against the instrumented libaom oracle: film-B gate clip, `-cpu-used 6 -crf 35 -g 12`, decode-order frame 3 (order_hint 1, the one inter frame with `force_integer_mv == 1`), block mi(0,0), the NEWMV predictor -- identical `EC_TPL` field cells and identical range in both decoders, aomdec's stack `(16,0)w16 (16,-8)w12 (8,-40)w4` against ours `(14,-4)w16 (12,-2)w8 (12,-38)w4 (16,-4)w4`. Class: `branch-dropped-as-unreachable` -- a spec branch deleted on an unreachability note that a capability change (screen-content streams no longer refused) silently invalidated. All four libaom film-B ladder points at 48 frames now decode sample-exact against ffmpeg/libdav1d except crf 5, which desyncs on the KEY frame at mi(184,344) (base_q 9) -- a separate, still-open defect. --- crates/ec-av1/src/decode.rs | 6 +++ crates/ec-av1/src/motion_field.rs | 78 +++++++++++++++++++++++++++---- crates/ec-av1/src/mvstack.rs | 37 ++++++++++++++- 3 files changed, 111 insertions(+), 10 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index ff96665e..bd60274c 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -26220,6 +26220,7 @@ fn decode_inter_block( t.ref_order_hints[(ref1 - LAST_FRAME) as usize], ), allow_high_precision_mv, + force_integer_mv, }); let gm_table = build_gm_mv_table( global_motion, @@ -27444,6 +27445,7 @@ fn decode_inter_block( t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], ), allow_high_precision_mv, + force_integer_mv, }); let gm_table = build_gm_mv_table( global_motion, @@ -29829,6 +29831,7 @@ fn decode_inter_sub8_split4( t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], ), allow_high_precision_mv, + force_integer_mv, }); let stack = crate::mvstack::find_mv_stack_with_sign_bias( grid, @@ -30857,6 +30860,7 @@ fn decode_inter_sub8_rect2( t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], ), allow_high_precision_mv, + force_integer_mv, }); let stack = crate::mvstack::find_mv_stack_with_sign_bias( grid, @@ -31603,6 +31607,7 @@ fn decode_inter_block8( t.ref_order_hints[(ref1 - LAST_FRAME) as usize], ), allow_high_precision_mv, + force_integer_mv, }); // lane-av1comp: this leaf path has neither a real sign-bias // table nor `tpl_frame` (see this function's own doc on the @@ -32293,6 +32298,7 @@ fn decode_inter_block8( t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], ), allow_high_precision_mv, + force_integer_mv, }); let stack = crate::mvstack::find_mv_stack_with_sign_bias( grid, diff --git a/crates/ec-av1/src/motion_field.rs b/crates/ec-av1/src/motion_field.rs index e444e910..6a565113 100644 --- a/crates/ec-av1/src/motion_field.rs +++ b/crates/ec-av1/src/motion_field.rs @@ -174,11 +174,35 @@ fn get_mv_projection(mv: (i32, i32), num: i32, den: i32) -> (i32, i32) { (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) { +/// `lower_mv_precision` (`mvref_common.h`), both halves. lane-dgolomb: the +/// `is_integer` half used to be dropped as unreachable on the claim that +/// `allow_screen_content_tools` is refused at the stream level -- it is not, +/// and libaom turns screen-content tools on for dark, smooth FILM (its +/// `av1_set_screen_content_options` counts near-flat blocks), so a real +/// libaom stream carries `force_integer_mv == 1` on an inter frame and every +/// temporal MV candidate has to be rounded to full pel. Without it the +/// candidates were 1/8-pel values libaom never produces: a whole-frame +/// prediction mismatch behind a clean parse (class +/// `branch-dropped-as-unreachable`). +pub fn lower_mv_precision( + mv: (i32, i32), + allow_high_precision_mv: bool, + force_integer_mv: bool, +) -> (i32, i32) { + if force_integer_mv { + // `integer_mv_precision`: drop to the nearest full pel, ties away + // from zero (`abs(mod) > 4`). + let round = |v: i32| { + let m = v % 8; + match m { + 0 => v, + _ if m > 4 => v - m + 8, + _ if m < -4 => v - m - 8, + _ => v - m, + } + }; + return (round(mv.0), round(mv.1)); + } if allow_high_precision_mv { return mv; } @@ -526,6 +550,7 @@ pub fn add_tpl_ref_mv( blk_col: isize, cur_offset_0: i32, allow_high_precision_mv: bool, + force_integer_mv: bool, ) -> Option { let probe = tpl.probe(mi_row, mi_col).cell(blk_row, blk_col); if crate::envflags::env_flag!("EC_TRACE_TPL") { @@ -539,7 +564,13 @@ pub fn add_tpl_ref_mv( } let (fwd_mv, ref_frame_offset) = probe?; Some(TplCandidate { - mv: project_tpl_mv(fwd_mv, cur_offset_0, ref_frame_offset, allow_high_precision_mv), + mv: project_tpl_mv( + fwd_mv, + cur_offset_0, + ref_frame_offset, + allow_high_precision_mv, + force_integer_mv, + ), }) } @@ -552,9 +583,10 @@ pub fn project_tpl_mv( cur_offset: i32, ref_frame_offset: i32, allow_high_precision_mv: bool, + force_integer_mv: bool, ) -> (i32, i32) { let projected = get_mv_projection(fwd_mv, cur_offset, ref_frame_offset); - lower_mv_precision(projected, allow_high_precision_mv) + lower_mv_precision(projected, allow_high_precision_mv, force_integer_mv) } /// One query block's view of [`TplField`] (lane-tpl). `add_tpl_ref_mv`'s @@ -709,7 +741,37 @@ mod tests { // 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); + let cand = + add_tpl_ref_mv(&tpl, 4, 0, 0isize, 0isize, get_relative_dist(7, 4, 2), true, false); assert!(cand.is_some(), "expected a projected temporal candidate"); } + + /// lane-dgolomb: `lower_mv_precision`'s `force_integer_mv` half, which + /// this decoder used to drop as unreachable. The values are the ones a + /// real libaom stream (film B, `-cpu-used 6 -crf 35`, the one inter frame + /// whose header carries `force_integer_mv == 1`) produces at its first + /// 128x128 block: the same 16 temporal probes, projected and then rounded + /// to full pel, are what put `(16,0)`/`(16,-8)`/`(8,-40)` in libaom's own + /// traced mv stack where the eighth-pel values were `(14,-4)`/`(12,-2)`/ + /// `(12,-38)`/`(16,-4)`. + #[test] + fn force_integer_mv_rounds_a_projected_candidate_to_full_pel() { + // (mfmv0, ref_frame_offset) -> (eighth-pel value, full-pel value), + // all at cur_offset_0 == 1. + for (mv, rfo, eighth, integer) in [ + ((74, -20), 5, (14, -4), (16, 0)), + ((74, -24), 5, (14, -4), (16, -8)), + ((82, -24), 5, (16, -4), (16, -8)), + ((66, -16), 5, (12, -2), (16, 0)), + ((24, -76), 2, (12, -38), (8, -40)), + ] { + assert_eq!(project_tpl_mv(mv, 1, rfo, false, false), eighth, "{mv:?}"); + assert_eq!(project_tpl_mv(mv, 1, rfo, false, true), integer, "{mv:?}"); + } + // `integer_mv_precision`'s tie rule is away from zero on |mod| > 4 + // only, and it is signed-symmetric. + assert_eq!(lower_mv_precision((4, -4), false, true), (0, 0)); + assert_eq!(lower_mv_precision((5, -5), false, true), (8, -8)); + assert_eq!(lower_mv_precision((-12, 8), false, true), (-8, 8)); + } } diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index f061fe3e..ddf1a5b8 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -154,6 +154,10 @@ pub struct TplArgs<'a> { pub cur_offset_0: i32, /// The frame header's own `allow_high_precision_mv`. pub allow_high_precision_mv: bool, + /// The frame header's own `force_integer_mv` (`lower_mv_precision`'s + /// `is_integer`): a screen-content frame rounds every projected temporal + /// candidate to full pel. + pub force_integer_mv: bool, } /// [`MiInfo::ref_frame1`]'s "this unit was not coded compound" value. Slot 1 @@ -1036,6 +1040,7 @@ fn tpl_trace_sample( blk_col: isize, cur_offset: i32, allow_high_precision_mv: bool, + force_integer_mv: bool, ) -> Option<(i32, i32)> { crate::motion_field::add_tpl_ref_mv( field, @@ -1045,6 +1050,7 @@ fn tpl_trace_sample( blk_col, cur_offset, allow_high_precision_mv, + force_integer_mv, ) .map(|c| c.mv) } @@ -1061,6 +1067,7 @@ fn tpl_trace_sample_compound( blk_col: isize, cur_offsets: (i32, i32), allow_high_precision_mv: bool, + force_integer_mv: bool, ) { for off in [cur_offsets.0, cur_offsets.1] { let _ = crate::motion_field::add_tpl_ref_mv( @@ -1071,6 +1078,7 @@ fn tpl_trace_sample_compound( blk_col, off, allow_high_precision_mv, + force_integer_mv, ); } } @@ -1093,6 +1101,7 @@ fn tpl_sample( blk_col: isize, cur_offset: i32, allow_high_precision_mv: bool, + force_integer_mv: bool, ) -> Option<(i32, i32)> { if trace_tpl { return tpl_trace_sample( @@ -1103,6 +1112,7 @@ fn tpl_sample( blk_col, cur_offset, allow_high_precision_mv, + force_integer_mv, ); } let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?; @@ -1111,6 +1121,7 @@ fn tpl_sample( cur_offset, rfo, allow_high_precision_mv, + force_integer_mv, )) } @@ -1128,6 +1139,7 @@ fn tpl_sample_compound( blk_col: isize, cur_offsets: (i32, i32), allow_high_precision_mv: bool, + force_integer_mv: bool, ) -> Option<((i32, i32), (i32, i32))> { if trace_tpl { tpl_trace_sample_compound( @@ -1138,12 +1150,25 @@ fn tpl_sample_compound( blk_col, cur_offsets, allow_high_precision_mv, + force_integer_mv, ); } let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?; Some(( - crate::motion_field::project_tpl_mv(fwd_mv, cur_offsets.0, rfo, allow_high_precision_mv), - crate::motion_field::project_tpl_mv(fwd_mv, cur_offsets.1, rfo, allow_high_precision_mv), + crate::motion_field::project_tpl_mv( + fwd_mv, + cur_offsets.0, + rfo, + allow_high_precision_mv, + force_integer_mv, + ), + crate::motion_field::project_tpl_mv( + fwd_mv, + cur_offsets.1, + rfo, + allow_high_precision_mv, + force_integer_mv, + ), )) } @@ -1312,6 +1337,7 @@ pub fn find_mv_stack_with_sign_bias( field, cur_offset_0, allow_high_precision_mv, + force_integer_mv, }) = tpl { let mut any_hit = false; @@ -1342,6 +1368,7 @@ pub fn find_mv_stack_with_sign_bias( blk_col, cur_offset_0, allow_high_precision_mv, + force_integer_mv, ) }; let mut blk_row = 0usize; @@ -2116,6 +2143,7 @@ pub fn find_mv_stack_compound( cur_offset_0, cur_offset_1, allow_high_precision_mv, + force_integer_mv, }) = tpl { let mut any_hit = false; @@ -2144,6 +2172,7 @@ pub fn find_mv_stack_compound( blk_col, (cur_offset_0, cur_offset_1), allow_high_precision_mv, + force_integer_mv, ) }; let mut blk_row = 0usize; @@ -2411,6 +2440,10 @@ pub struct CompoundTplArgs<'a> { pub cur_offset_1: i32, /// The frame header's own `allow_high_precision_mv`. pub allow_high_precision_mv: bool, + /// The frame header's own `force_integer_mv` (`lower_mv_precision`'s + /// `is_integer`): a screen-content frame rounds every projected temporal + /// candidate to full pel. + pub force_integer_mv: bool, } /// The single-reference context shared by the `single_ref_p1`/`p3`/`p4`