Temporal MV candidates ignored force_integer_mv
Browse files at this commit0c17efcbf667c824c71390ee7122339bba8962ccParent: 5c50b7f- fix
- temporal MV candidates ignored force_integer_mv
3 changed files +111 -10
- crates/ec-av1/src/decode.rs +6 -0
- crates/ec-av1/src/motion_field.rs +70 -8
- crates/ec-av1/src/mvstack.rs +35 -2
crates/ec-av1/src/decode.rsmodified+6 -0
| @@ -26220,6 +26220,7 @@ fn decode_inter_block( | |||
| 26220 | t.ref_order_hints[(ref1 - LAST_FRAME) as usize], | 26220 | t.ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 26221 | ), | 26221 | ), |
| 26222 | allow_high_precision_mv, | 26222 | allow_high_precision_mv, |
| 26223 | + force_integer_mv, | ||
| 26223 | }); | 26224 | }); |
| 26224 | let gm_table = build_gm_mv_table( | 26225 | let gm_table = build_gm_mv_table( |
| 26225 | global_motion, | 26226 | global_motion, |
| @@ -27444,6 +27445,7 @@ fn decode_inter_block( | |||
| 27444 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], | 27445 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], |
| 27445 | ), | 27446 | ), |
| 27446 | allow_high_precision_mv, | 27447 | allow_high_precision_mv, |
| 27448 | + force_integer_mv, | ||
| 27447 | }); | 27449 | }); |
| 27448 | let gm_table = build_gm_mv_table( | 27450 | let gm_table = build_gm_mv_table( |
| 27449 | global_motion, | 27451 | global_motion, |
| @@ -29829,6 +29831,7 @@ fn decode_inter_sub8_split4( | |||
| 29829 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], | 29831 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], |
| 29830 | ), | 29832 | ), |
| 29831 | allow_high_precision_mv, | 29833 | allow_high_precision_mv, |
| 29834 | + force_integer_mv, | ||
| 29832 | }); | 29835 | }); |
| 29833 | let stack = crate::mvstack::find_mv_stack_with_sign_bias( | 29836 | let stack = crate::mvstack::find_mv_stack_with_sign_bias( |
| 29834 | grid, | 29837 | grid, |
| @@ -30857,6 +30860,7 @@ fn decode_inter_sub8_rect2( | |||
| 30857 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], | 30860 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], |
| 30858 | ), | 30861 | ), |
| 30859 | allow_high_precision_mv, | 30862 | allow_high_precision_mv, |
| 30863 | + force_integer_mv, | ||
| 30860 | }); | 30864 | }); |
| 30861 | let stack = crate::mvstack::find_mv_stack_with_sign_bias( | 30865 | let stack = crate::mvstack::find_mv_stack_with_sign_bias( |
| 30862 | grid, | 30866 | grid, |
| @@ -31603,6 +31607,7 @@ fn decode_inter_block8( | |||
| 31603 | t.ref_order_hints[(ref1 - LAST_FRAME) as usize], | 31607 | t.ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 31604 | ), | 31608 | ), |
| 31605 | allow_high_precision_mv, | 31609 | allow_high_precision_mv, |
| 31610 | + force_integer_mv, | ||
| 31606 | }); | 31611 | }); |
| 31607 | // lane-av1comp: this leaf path has neither a real sign-bias | 31612 | // lane-av1comp: this leaf path has neither a real sign-bias |
| 31608 | // table nor `tpl_frame` (see this function's own doc on the | 31613 | // table nor `tpl_frame` (see this function's own doc on the |
| @@ -32293,6 +32298,7 @@ fn decode_inter_block8( | |||
| 32293 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], | 32298 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], |
| 32294 | ), | 32299 | ), |
| 32295 | allow_high_precision_mv, | 32300 | allow_high_precision_mv, |
| 32301 | + force_integer_mv, | ||
| 32296 | }); | 32302 | }); |
| 32297 | let stack = crate::mvstack::find_mv_stack_with_sign_bias( | 32303 | let stack = crate::mvstack::find_mv_stack_with_sign_bias( |
| 32298 | grid, | 32304 | 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) { | |||
| 174 | (scale(mv.0), scale(mv.1)) | 174 | (scale(mv.0), scale(mv.1)) |
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | -/// `lower_mv_precision` (`mvref_common.h`), the `is_integer == false` half | 177 | +/// `lower_mv_precision` (`mvref_common.h`), both halves. lane-dgolomb: the |
| 178 | -/// only — every stream this decoder ever reaches has `force_integer_mv == | 178 | +/// `is_integer` half used to be dropped as unreachable on the claim that |
| 179 | -/// false` on an inter frame (`allow_screen_content_tools`, the only source | 179 | +/// `allow_screen_content_tools` is refused at the stream level -- it is not, |
| 180 | -/// of a `true` value here, is refused outright at the stream level). | 180 | +/// and libaom turns screen-content tools on for dark, smooth FILM (its |
| 181 | -pub fn lower_mv_precision(mv: (i32, i32), allow_high_precision_mv: bool) -> (i32, i32) { | 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 | + } | ||
| 182 | if allow_high_precision_mv { | 206 | if allow_high_precision_mv { |
| 183 | return mv; | 207 | return mv; |
| 184 | } | 208 | } |
| @@ -526,6 +550,7 @@ pub fn add_tpl_ref_mv( | |||
| 526 | blk_col: isize, | 550 | blk_col: isize, |
| 527 | cur_offset_0: i32, | 551 | cur_offset_0: i32, |
| 528 | allow_high_precision_mv: bool, | 552 | allow_high_precision_mv: bool, |
| 553 | + force_integer_mv: bool, | ||
| 529 | ) -> Option<TplCandidate> { | 554 | ) -> Option<TplCandidate> { |
| 530 | let probe = tpl.probe(mi_row, mi_col).cell(blk_row, blk_col); | 555 | let probe = tpl.probe(mi_row, mi_col).cell(blk_row, blk_col); |
| 531 | if crate::envflags::env_flag!("EC_TRACE_TPL") { | 556 | if crate::envflags::env_flag!("EC_TRACE_TPL") { |
| @@ -539,7 +564,13 @@ pub fn add_tpl_ref_mv( | |||
| 539 | } | 564 | } |
| 540 | let (fwd_mv, ref_frame_offset) = probe?; | 565 | let (fwd_mv, ref_frame_offset) = probe?; |
| 541 | Some(TplCandidate { | 566 | 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 | + ), | ||
| 543 | }) | 574 | }) |
| 544 | } | 575 | } |
| 545 | 576 | ||
| @@ -552,9 +583,10 @@ pub fn project_tpl_mv( | |||
| 552 | cur_offset: i32, | 583 | cur_offset: i32, |
| 553 | ref_frame_offset: i32, | 584 | ref_frame_offset: i32, |
| 554 | allow_high_precision_mv: bool, | 585 | allow_high_precision_mv: bool, |
| 586 | + force_integer_mv: bool, | ||
| 555 | ) -> (i32, i32) { | 587 | ) -> (i32, i32) { |
| 556 | let projected = get_mv_projection(fwd_mv, cur_offset, ref_frame_offset); | 588 | 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) |
| 558 | } | 590 | } |
| 559 | 591 | ||
| 560 | /// One query block's view of [`TplField`] (lane-tpl). `add_tpl_ref_mv`'s | 592 | /// One query block's view of [`TplField`] (lane-tpl). `add_tpl_ref_mv`'s |
| @@ -709,7 +741,37 @@ mod tests { | |||
| 709 | // ref_frame_offset = get_relative_dist(2,1) = 1, scaling (-64,0) by | 741 | // ref_frame_offset = get_relative_dist(2,1) = 1, scaling (-64,0) by |
| 710 | // 2/1 -> (-128,0); get_block_position's sign_bias (dir=2) subtracts | 742 | // 2/1 -> (-128,0); get_block_position's sign_bias (dir=2) subtracts |
| 711 | // the offset, landing at 8x8 cell (2, 0). | 743 | // 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); | ||
| 713 | assert!(cand.is_some(), "expected a projected temporal candidate"); | 746 | assert!(cand.is_some(), "expected a projected temporal candidate"); |
| 714 | } | 747 | } |
| 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 | + } | ||
| 715 | } | 777 | } |
crates/ec-av1/src/mvstack.rsmodified+35 -2
| @@ -154,6 +154,10 @@ pub struct TplArgs<'a> { | |||
| 154 | pub cur_offset_0: i32, | 154 | pub cur_offset_0: i32, |
| 155 | /// The frame header's own `allow_high_precision_mv`. | 155 | /// The frame header's own `allow_high_precision_mv`. |
| 156 | pub allow_high_precision_mv: bool, | 156 | 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, | ||
| 157 | } | 161 | } |
| 158 | 162 | ||
| 159 | /// [`MiInfo::ref_frame1`]'s "this unit was not coded compound" value. Slot 1 | 163 | /// [`MiInfo::ref_frame1`]'s "this unit was not coded compound" value. Slot 1 |
| @@ -1036,6 +1040,7 @@ fn tpl_trace_sample( | |||
| 1036 | blk_col: isize, | 1040 | blk_col: isize, |
| 1037 | cur_offset: i32, | 1041 | cur_offset: i32, |
| 1038 | allow_high_precision_mv: bool, | 1042 | allow_high_precision_mv: bool, |
| 1043 | + force_integer_mv: bool, | ||
| 1039 | ) -> Option<(i32, i32)> { | 1044 | ) -> Option<(i32, i32)> { |
| 1040 | crate::motion_field::add_tpl_ref_mv( | 1045 | crate::motion_field::add_tpl_ref_mv( |
| 1041 | field, | 1046 | field, |
| @@ -1045,6 +1050,7 @@ fn tpl_trace_sample( | |||
| 1045 | blk_col, | 1050 | blk_col, |
| 1046 | cur_offset, | 1051 | cur_offset, |
| 1047 | allow_high_precision_mv, | 1052 | allow_high_precision_mv, |
| 1053 | + force_integer_mv, | ||
| 1048 | ) | 1054 | ) |
| 1049 | .map(|c| c.mv) | 1055 | .map(|c| c.mv) |
| 1050 | } | 1056 | } |
| @@ -1061,6 +1067,7 @@ fn tpl_trace_sample_compound( | |||
| 1061 | blk_col: isize, | 1067 | blk_col: isize, |
| 1062 | cur_offsets: (i32, i32), | 1068 | cur_offsets: (i32, i32), |
| 1063 | allow_high_precision_mv: bool, | 1069 | allow_high_precision_mv: bool, |
| 1070 | + force_integer_mv: bool, | ||
| 1064 | ) { | 1071 | ) { |
| 1065 | for off in [cur_offsets.0, cur_offsets.1] { | 1072 | for off in [cur_offsets.0, cur_offsets.1] { |
| 1066 | let _ = crate::motion_field::add_tpl_ref_mv( | 1073 | let _ = crate::motion_field::add_tpl_ref_mv( |
| @@ -1071,6 +1078,7 @@ fn tpl_trace_sample_compound( | |||
| 1071 | blk_col, | 1078 | blk_col, |
| 1072 | off, | 1079 | off, |
| 1073 | allow_high_precision_mv, | 1080 | allow_high_precision_mv, |
| 1081 | + force_integer_mv, | ||
| 1074 | ); | 1082 | ); |
| 1075 | } | 1083 | } |
| 1076 | } | 1084 | } |
| @@ -1093,6 +1101,7 @@ fn tpl_sample( | |||
| 1093 | blk_col: isize, | 1101 | blk_col: isize, |
| 1094 | cur_offset: i32, | 1102 | cur_offset: i32, |
| 1095 | allow_high_precision_mv: bool, | 1103 | allow_high_precision_mv: bool, |
| 1104 | + force_integer_mv: bool, | ||
| 1096 | ) -> Option<(i32, i32)> { | 1105 | ) -> Option<(i32, i32)> { |
| 1097 | if trace_tpl { | 1106 | if trace_tpl { |
| 1098 | return tpl_trace_sample( | 1107 | return tpl_trace_sample( |
| @@ -1103,6 +1112,7 @@ fn tpl_sample( | |||
| 1103 | blk_col, | 1112 | blk_col, |
| 1104 | cur_offset, | 1113 | cur_offset, |
| 1105 | allow_high_precision_mv, | 1114 | allow_high_precision_mv, |
| 1115 | + force_integer_mv, | ||
| 1106 | ); | 1116 | ); |
| 1107 | } | 1117 | } |
| 1108 | let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?; | 1118 | let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?; |
| @@ -1111,6 +1121,7 @@ fn tpl_sample( | |||
| 1111 | cur_offset, | 1121 | cur_offset, |
| 1112 | rfo, | 1122 | rfo, |
| 1113 | allow_high_precision_mv, | 1123 | allow_high_precision_mv, |
| 1124 | + force_integer_mv, | ||
| 1114 | )) | 1125 | )) |
| 1115 | } | 1126 | } |
| 1116 | 1127 | ||
| @@ -1128,6 +1139,7 @@ fn tpl_sample_compound( | |||
| 1128 | blk_col: isize, | 1139 | blk_col: isize, |
| 1129 | cur_offsets: (i32, i32), | 1140 | cur_offsets: (i32, i32), |
| 1130 | allow_high_precision_mv: bool, | 1141 | allow_high_precision_mv: bool, |
| 1142 | + force_integer_mv: bool, | ||
| 1131 | ) -> Option<((i32, i32), (i32, i32))> { | 1143 | ) -> Option<((i32, i32), (i32, i32))> { |
| 1132 | if trace_tpl { | 1144 | if trace_tpl { |
| 1133 | tpl_trace_sample_compound( | 1145 | tpl_trace_sample_compound( |
| @@ -1138,12 +1150,25 @@ fn tpl_sample_compound( | |||
| 1138 | blk_col, | 1150 | blk_col, |
| 1139 | cur_offsets, | 1151 | cur_offsets, |
| 1140 | allow_high_precision_mv, | 1152 | allow_high_precision_mv, |
| 1153 | + force_integer_mv, | ||
| 1141 | ); | 1154 | ); |
| 1142 | } | 1155 | } |
| 1143 | let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?; | 1156 | let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?; |
| 1144 | Some(( | 1157 | Some(( |
| 1145 | - crate::motion_field::project_tpl_mv(fwd_mv, cur_offsets.0, rfo, allow_high_precision_mv), | 1158 | + crate::motion_field::project_tpl_mv( |
| 1146 | - crate::motion_field::project_tpl_mv(fwd_mv, cur_offsets.1, rfo, allow_high_precision_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 | + ), | ||
| 1147 | )) | 1172 | )) |
| 1148 | } | 1173 | } |
| 1149 | 1174 | ||
| @@ -1312,6 +1337,7 @@ pub fn find_mv_stack_with_sign_bias( | |||
| 1312 | field, | 1337 | field, |
| 1313 | cur_offset_0, | 1338 | cur_offset_0, |
| 1314 | allow_high_precision_mv, | 1339 | allow_high_precision_mv, |
| 1340 | + force_integer_mv, | ||
| 1315 | }) = tpl | 1341 | }) = tpl |
| 1316 | { | 1342 | { |
| 1317 | let mut any_hit = false; | 1343 | let mut any_hit = false; |
| @@ -1342,6 +1368,7 @@ pub fn find_mv_stack_with_sign_bias( | |||
| 1342 | blk_col, | 1368 | blk_col, |
| 1343 | cur_offset_0, | 1369 | cur_offset_0, |
| 1344 | allow_high_precision_mv, | 1370 | allow_high_precision_mv, |
| 1371 | + force_integer_mv, | ||
| 1345 | ) | 1372 | ) |
| 1346 | }; | 1373 | }; |
| 1347 | let mut blk_row = 0usize; | 1374 | let mut blk_row = 0usize; |
| @@ -2116,6 +2143,7 @@ pub fn find_mv_stack_compound( | |||
| 2116 | cur_offset_0, | 2143 | cur_offset_0, |
| 2117 | cur_offset_1, | 2144 | cur_offset_1, |
| 2118 | allow_high_precision_mv, | 2145 | allow_high_precision_mv, |
| 2146 | + force_integer_mv, | ||
| 2119 | }) = tpl | 2147 | }) = tpl |
| 2120 | { | 2148 | { |
| 2121 | let mut any_hit = false; | 2149 | let mut any_hit = false; |
| @@ -2144,6 +2172,7 @@ pub fn find_mv_stack_compound( | |||
| 2144 | blk_col, | 2172 | blk_col, |
| 2145 | (cur_offset_0, cur_offset_1), | 2173 | (cur_offset_0, cur_offset_1), |
| 2146 | allow_high_precision_mv, | 2174 | allow_high_precision_mv, |
| 2175 | + force_integer_mv, | ||
| 2147 | ) | 2176 | ) |
| 2148 | }; | 2177 | }; |
| 2149 | let mut blk_row = 0usize; | 2178 | let mut blk_row = 0usize; |
| @@ -2411,6 +2440,10 @@ pub struct CompoundTplArgs<'a> { | |||
| 2411 | pub cur_offset_1: i32, | 2440 | pub cur_offset_1: i32, |
| 2412 | /// The frame header's own `allow_high_precision_mv`. | 2441 | /// The frame header's own `allow_high_precision_mv`. |
| 2413 | pub allow_high_precision_mv: bool, | 2442 | 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, | ||
| 2414 | } | 2447 | } |
| 2415 | 2448 | ||
| 2416 | /// The single-reference context shared by the `single_ref_p1`/`p3`/`p4` | 2449 | /// The single-reference context shared by the `single_ref_p1`/`p3`/`p4` |