forge
Explore
Log in

Don't you dare go Hollow

Temporal MV candidates ignored force_integer_mv

Browse files at this commit

0c17efcbf667c824c71390ee7122339bba8962ccAhmet Kaan GümüşParent: 5c50b7f
  • fix
    • temporal MV candidates ignored force_integer_mv
Show full message
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.rsmodified+6 -0
@@ -26220,6 +26220,7 @@ fn decode_inter_block(
2622026220 t.ref_order_hints[(ref1 - LAST_FRAME) as usize],
2622126221 ),
2622226222 allow_high_precision_mv,
26223+ force_integer_mv,
2622326224 });
2622426225 let gm_table = build_gm_mv_table(
2622526226 global_motion,
@@ -27444,6 +27445,7 @@ fn decode_inter_block(
2744427445 t.ref_order_hints[(ref_frame - LAST_FRAME) as usize],
2744527446 ),
2744627447 allow_high_precision_mv,
27448+ force_integer_mv,
2744727449 });
2744827450 let gm_table = build_gm_mv_table(
2744927451 global_motion,
@@ -29829,6 +29831,7 @@ fn decode_inter_sub8_split4(
2982929831 t.ref_order_hints[(ref_frame - LAST_FRAME) as usize],
2983029832 ),
2983129833 allow_high_precision_mv,
29834+ force_integer_mv,
2983229835 });
2983329836 let stack = crate::mvstack::find_mv_stack_with_sign_bias(
2983429837 grid,
@@ -30857,6 +30860,7 @@ fn decode_inter_sub8_rect2(
3085730860 t.ref_order_hints[(ref_frame - LAST_FRAME) as usize],
3085830861 ),
3085930862 allow_high_precision_mv,
30863+ force_integer_mv,
3086030864 });
3086130865 let stack = crate::mvstack::find_mv_stack_with_sign_bias(
3086230866 grid,
@@ -31603,6 +31607,7 @@ fn decode_inter_block8(
3160331607 t.ref_order_hints[(ref1 - LAST_FRAME) as usize],
3160431608 ),
3160531609 allow_high_precision_mv,
31610+ force_integer_mv,
3160631611 });
3160731612 // lane-av1comp: this leaf path has neither a real sign-bias
3160831613 // table nor `tpl_frame` (see this function's own doc on the
@@ -32293,6 +32298,7 @@ fn decode_inter_block8(
3229332298 t.ref_order_hints[(ref_frame - LAST_FRAME) as usize],
3229432299 ),
3229532300 allow_high_precision_mv,
32301+ force_integer_mv,
3229632302 });
3229732303 let stack = crate::mvstack::find_mv_stack_with_sign_bias(
3229832304 grid,
crates/ec-av1/src/motion_field.rsmodified+70 -8
@@ -174,11 +174,35 @@ fn get_mv_projection(mv: (i32, i32), num: i32, den: i32) -> (i32, i32) {
174174 (scale(mv.0), scale(mv.1))
175175 }
176176
177-/// `lower_mv_precision` (`mvref_common.h`), the `is_integer == false` half
178-/// only — every stream this decoder ever reaches has `force_integer_mv ==
179-/// false` on an inter frame (`allow_screen_content_tools`, the only source
180-/// of a `true` value here, is refused outright at the stream level).
181-pub fn lower_mv_precision(mv: (i32, i32), allow_high_precision_mv: bool) -> (i32, i32) {
177+/// `lower_mv_precision` (`mvref_common.h`), both halves. lane-dgolomb: the
178+/// `is_integer` half used to be dropped as unreachable on the claim that
179+/// `allow_screen_content_tools` is refused at the stream level -- it is not,
180+/// and libaom turns screen-content tools on for dark, smooth FILM (its
181+/// `av1_set_screen_content_options` counts near-flat blocks), so a real
182+/// libaom stream carries `force_integer_mv == 1` on an inter frame and every
183+/// temporal MV candidate has to be rounded to full pel. Without it the
184+/// candidates were 1/8-pel values libaom never produces: a whole-frame
185+/// prediction mismatch behind a clean parse (class
186+/// `branch-dropped-as-unreachable`).
187+pub fn lower_mv_precision(
188+ mv: (i32, i32),
189+ allow_high_precision_mv: bool,
190+ force_integer_mv: bool,
191+) -> (i32, i32) {
192+ if force_integer_mv {
193+ // `integer_mv_precision`: drop to the nearest full pel, ties away
194+ // from zero (`abs(mod) > 4`).
195+ let round = |v: i32| {
196+ let m = v % 8;
197+ match m {
198+ 0 => v,
199+ _ if m > 4 => v - m + 8,
200+ _ if m < -4 => v - m - 8,
201+ _ => v - m,
202+ }
203+ };
204+ return (round(mv.0), round(mv.1));
205+ }
182206 if allow_high_precision_mv {
183207 return mv;
184208 }
@@ -526,6 +550,7 @@ pub fn add_tpl_ref_mv(
526550 blk_col: isize,
527551 cur_offset_0: i32,
528552 allow_high_precision_mv: bool,
553+ force_integer_mv: bool,
529554 ) -> Option<TplCandidate> {
530555 let probe = tpl.probe(mi_row, mi_col).cell(blk_row, blk_col);
531556 if crate::envflags::env_flag!("EC_TRACE_TPL") {
@@ -539,7 +564,13 @@ pub fn add_tpl_ref_mv(
539564 }
540565 let (fwd_mv, ref_frame_offset) = probe?;
541566 Some(TplCandidate {
542- mv: project_tpl_mv(fwd_mv, cur_offset_0, ref_frame_offset, allow_high_precision_mv),
567+ mv: project_tpl_mv(
568+ fwd_mv,
569+ cur_offset_0,
570+ ref_frame_offset,
571+ allow_high_precision_mv,
572+ force_integer_mv,
573+ ),
543574 })
544575 }
545576
@@ -552,9 +583,10 @@ pub fn project_tpl_mv(
552583 cur_offset: i32,
553584 ref_frame_offset: i32,
554585 allow_high_precision_mv: bool,
586+ force_integer_mv: bool,
555587 ) -> (i32, i32) {
556588 let projected = get_mv_projection(fwd_mv, cur_offset, ref_frame_offset);
557- lower_mv_precision(projected, allow_high_precision_mv)
589+ lower_mv_precision(projected, allow_high_precision_mv, force_integer_mv)
558590 }
559591
560592 /// One query block's view of [`TplField`] (lane-tpl). `add_tpl_ref_mv`'s
@@ -709,7 +741,37 @@ mod tests {
709741 // ref_frame_offset = get_relative_dist(2,1) = 1, scaling (-64,0) by
710742 // 2/1 -> (-128,0); get_block_position's sign_bias (dir=2) subtracts
711743 // the offset, landing at 8x8 cell (2, 0).
712- let cand = add_tpl_ref_mv(&tpl, 4, 0, 0isize, 0isize, get_relative_dist(7, 4, 2), true);
744+ let cand =
745+ add_tpl_ref_mv(&tpl, 4, 0, 0isize, 0isize, get_relative_dist(7, 4, 2), true, false);
713746 assert!(cand.is_some(), "expected a projected temporal candidate");
714747 }
748+
749+ /// lane-dgolomb: `lower_mv_precision`'s `force_integer_mv` half, which
750+ /// this decoder used to drop as unreachable. The values are the ones a
751+ /// real libaom stream (film B, `-cpu-used 6 -crf 35`, the one inter frame
752+ /// whose header carries `force_integer_mv == 1`) produces at its first
753+ /// 128x128 block: the same 16 temporal probes, projected and then rounded
754+ /// to full pel, are what put `(16,0)`/`(16,-8)`/`(8,-40)` in libaom's own
755+ /// traced mv stack where the eighth-pel values were `(14,-4)`/`(12,-2)`/
756+ /// `(12,-38)`/`(16,-4)`.
757+ #[test]
758+ fn force_integer_mv_rounds_a_projected_candidate_to_full_pel() {
759+ // (mfmv0, ref_frame_offset) -> (eighth-pel value, full-pel value),
760+ // all at cur_offset_0 == 1.
761+ for (mv, rfo, eighth, integer) in [
762+ ((74, -20), 5, (14, -4), (16, 0)),
763+ ((74, -24), 5, (14, -4), (16, -8)),
764+ ((82, -24), 5, (16, -4), (16, -8)),
765+ ((66, -16), 5, (12, -2), (16, 0)),
766+ ((24, -76), 2, (12, -38), (8, -40)),
767+ ] {
768+ assert_eq!(project_tpl_mv(mv, 1, rfo, false, false), eighth, "{mv:?}");
769+ assert_eq!(project_tpl_mv(mv, 1, rfo, false, true), integer, "{mv:?}");
770+ }
771+ // `integer_mv_precision`'s tie rule is away from zero on |mod| > 4
772+ // only, and it is signed-symmetric.
773+ assert_eq!(lower_mv_precision((4, -4), false, true), (0, 0));
774+ assert_eq!(lower_mv_precision((5, -5), false, true), (8, -8));
775+ assert_eq!(lower_mv_precision((-12, 8), false, true), (-8, 8));
776+ }
715777 }
crates/ec-av1/src/mvstack.rsmodified+35 -2
@@ -154,6 +154,10 @@ pub struct TplArgs<'a> {
154154 pub cur_offset_0: i32,
155155 /// The frame header's own `allow_high_precision_mv`.
156156 pub allow_high_precision_mv: bool,
157+ /// The frame header's own `force_integer_mv` (`lower_mv_precision`'s
158+ /// `is_integer`): a screen-content frame rounds every projected temporal
159+ /// candidate to full pel.
160+ pub force_integer_mv: bool,
157161 }
158162
159163 /// [`MiInfo::ref_frame1`]'s "this unit was not coded compound" value. Slot 1
@@ -1036,6 +1040,7 @@ fn tpl_trace_sample(
10361040 blk_col: isize,
10371041 cur_offset: i32,
10381042 allow_high_precision_mv: bool,
1043+ force_integer_mv: bool,
10391044 ) -> Option<(i32, i32)> {
10401045 crate::motion_field::add_tpl_ref_mv(
10411046 field,
@@ -1045,6 +1050,7 @@ fn tpl_trace_sample(
10451050 blk_col,
10461051 cur_offset,
10471052 allow_high_precision_mv,
1053+ force_integer_mv,
10481054 )
10491055 .map(|c| c.mv)
10501056 }
@@ -1061,6 +1067,7 @@ fn tpl_trace_sample_compound(
10611067 blk_col: isize,
10621068 cur_offsets: (i32, i32),
10631069 allow_high_precision_mv: bool,
1070+ force_integer_mv: bool,
10641071 ) {
10651072 for off in [cur_offsets.0, cur_offsets.1] {
10661073 let _ = crate::motion_field::add_tpl_ref_mv(
@@ -1071,6 +1078,7 @@ fn tpl_trace_sample_compound(
10711078 blk_col,
10721079 off,
10731080 allow_high_precision_mv,
1081+ force_integer_mv,
10741082 );
10751083 }
10761084 }
@@ -1093,6 +1101,7 @@ fn tpl_sample(
10931101 blk_col: isize,
10941102 cur_offset: i32,
10951103 allow_high_precision_mv: bool,
1104+ force_integer_mv: bool,
10961105 ) -> Option<(i32, i32)> {
10971106 if trace_tpl {
10981107 return tpl_trace_sample(
@@ -1103,6 +1112,7 @@ fn tpl_sample(
11031112 blk_col,
11041113 cur_offset,
11051114 allow_high_precision_mv,
1115+ force_integer_mv,
11061116 );
11071117 }
11081118 let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?;
@@ -1111,6 +1121,7 @@ fn tpl_sample(
11111121 cur_offset,
11121122 rfo,
11131123 allow_high_precision_mv,
1124+ force_integer_mv,
11141125 ))
11151126 }
11161127
@@ -1128,6 +1139,7 @@ fn tpl_sample_compound(
11281139 blk_col: isize,
11291140 cur_offsets: (i32, i32),
11301141 allow_high_precision_mv: bool,
1142+ force_integer_mv: bool,
11311143 ) -> Option<((i32, i32), (i32, i32))> {
11321144 if trace_tpl {
11331145 tpl_trace_sample_compound(
@@ -1138,12 +1150,25 @@ fn tpl_sample_compound(
11381150 blk_col,
11391151 cur_offsets,
11401152 allow_high_precision_mv,
1153+ force_integer_mv,
11411154 );
11421155 }
11431156 let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?;
11441157 Some((
1145- crate::motion_field::project_tpl_mv(fwd_mv, cur_offsets.0, rfo, allow_high_precision_mv),
1146- crate::motion_field::project_tpl_mv(fwd_mv, cur_offsets.1, rfo, allow_high_precision_mv),
1158+ crate::motion_field::project_tpl_mv(
1159+ fwd_mv,
1160+ cur_offsets.0,
1161+ rfo,
1162+ allow_high_precision_mv,
1163+ force_integer_mv,
1164+ ),
1165+ crate::motion_field::project_tpl_mv(
1166+ fwd_mv,
1167+ cur_offsets.1,
1168+ rfo,
1169+ allow_high_precision_mv,
1170+ force_integer_mv,
1171+ ),
11471172 ))
11481173 }
11491174
@@ -1312,6 +1337,7 @@ pub fn find_mv_stack_with_sign_bias(
13121337 field,
13131338 cur_offset_0,
13141339 allow_high_precision_mv,
1340+ force_integer_mv,
13151341 }) = tpl
13161342 {
13171343 let mut any_hit = false;
@@ -1342,6 +1368,7 @@ pub fn find_mv_stack_with_sign_bias(
13421368 blk_col,
13431369 cur_offset_0,
13441370 allow_high_precision_mv,
1371+ force_integer_mv,
13451372 )
13461373 };
13471374 let mut blk_row = 0usize;
@@ -2116,6 +2143,7 @@ pub fn find_mv_stack_compound(
21162143 cur_offset_0,
21172144 cur_offset_1,
21182145 allow_high_precision_mv,
2146+ force_integer_mv,
21192147 }) = tpl
21202148 {
21212149 let mut any_hit = false;
@@ -2144,6 +2172,7 @@ pub fn find_mv_stack_compound(
21442172 blk_col,
21452173 (cur_offset_0, cur_offset_1),
21462174 allow_high_precision_mv,
2175+ force_integer_mv,
21472176 )
21482177 };
21492178 let mut blk_row = 0usize;
@@ -2411,6 +2440,10 @@ pub struct CompoundTplArgs<'a> {
24112440 pub cur_offset_1: i32,
24122441 /// The frame header's own `allow_high_precision_mv`.
24132442 pub allow_high_precision_mv: bool,
2443+ /// The frame header's own `force_integer_mv` (`lower_mv_precision`'s
2444+ /// `is_integer`): a screen-content frame rounds every projected temporal
2445+ /// candidate to full pel.
2446+ pub force_integer_mv: bool,
24142447 }
24152448
24162449 /// The single-reference context shared by the `single_ref_p1`/`p3`/`p4`