WIP(av1): compound mvstack candidate machinery, not yet wired
Browse files at this commit368413c3a9b189fd655fe51d5b6f9cd44cb41b0bParent: cfa1e234 changed files +875 -0
- crates/ec-av1/src/decode.rs +5 -0
- crates/ec-av1/src/encode.rs +6 -0
- crates/ec-av1/src/mvstack.rs +856 -0
- crates/ec-av1/src/tile.rs +8 -0
crates/ec-av1/src/decode.rsmodified+5 -0
| @@ -4020,6 +4020,7 @@ fn decode_inter_block( | ||
| 4020 | 4020 | is_inter: true, |
| 4021 | 4021 | ref_frame, |
| 4022 | 4022 | ref_frame1: None, |
| 4023 | + mv1: None, | |
| 4023 | 4024 | mv, |
| 4024 | 4025 | is_new_mv, |
| 4025 | 4026 | size: bw4, |
| @@ -4176,6 +4177,7 @@ fn decode_inter_block( | ||
| 4176 | 4177 | is_inter: false, |
| 4177 | 4178 | ref_frame: -1, |
| 4178 | 4179 | ref_frame1: None, |
| 4180 | + mv1: None, | |
| 4179 | 4181 | mv: (0, 0), |
| 4180 | 4182 | is_new_mv: false, |
| 4181 | 4183 | size: side / 4, |
| @@ -4455,6 +4457,7 @@ fn decode_inter_block8( | ||
| 4455 | 4457 | is_inter: true, |
| 4456 | 4458 | ref_frame: LAST_FRAME, |
| 4457 | 4459 | ref_frame1: None, |
| 4460 | + mv1: None, | |
| 4458 | 4461 | mv, |
| 4459 | 4462 | is_new_mv, |
| 4460 | 4463 | size: 2, |
| @@ -4600,6 +4603,7 @@ fn decode_inter_block8( | ||
| 4600 | 4603 | is_inter: false, |
| 4601 | 4604 | ref_frame: -1, |
| 4602 | 4605 | ref_frame1: None, |
| 4606 | + mv1: None, | |
| 4603 | 4607 | mv: (0, 0), |
| 4604 | 4608 | is_new_mv: false, |
| 4605 | 4609 | size: 2, |
| @@ -5680,6 +5684,7 @@ mod tests { | ||
| 5680 | 5684 | is_inter: true, |
| 5681 | 5685 | ref_frame: LAST_FRAME, |
| 5682 | 5686 | ref_frame1: None, |
| 5687 | + mv1: None, | |
| 5683 | 5688 | mv, |
| 5684 | 5689 | is_new_mv: false, |
| 5685 | 5690 | size: 1, |
crates/ec-av1/src/encode.rsmodified+6 -0
| @@ -2613,6 +2613,7 @@ pub(crate) fn encode_inter_frame( | ||
| 2613 | 2613 | is_inter: true, |
| 2614 | 2614 | ref_frame: LAST_FRAME, |
| 2615 | 2615 | ref_frame1: None, |
| 2616 | + mv1: None, | |
| 2616 | 2617 | mv: info.mv, |
| 2617 | 2618 | is_new_mv: matches!(info.mode, InterMode::NewMv), |
| 2618 | 2619 | size: 8, |
| @@ -2625,6 +2626,7 @@ pub(crate) fn encode_inter_frame( | ||
| 2625 | 2626 | is_inter: false, |
| 2626 | 2627 | ref_frame: -1, |
| 2627 | 2628 | ref_frame1: None, |
| 2629 | + mv1: None, | |
| 2628 | 2630 | mv: (0, 0), |
| 2629 | 2631 | is_new_mv: false, |
| 2630 | 2632 | size: 8, |
| @@ -2703,6 +2705,7 @@ pub(crate) fn encode_inter_frame( | ||
| 2703 | 2705 | is_inter: true, |
| 2704 | 2706 | ref_frame: LAST_FRAME, |
| 2705 | 2707 | ref_frame1: None, |
| 2708 | + mv1: None, | |
| 2706 | 2709 | mv: info.mv, |
| 2707 | 2710 | is_new_mv: matches!(info.mode, InterMode::NewMv), |
| 2708 | 2711 | size: 4, |
| @@ -2711,6 +2714,7 @@ pub(crate) fn encode_inter_frame( | ||
| 2711 | 2714 | is_inter: false, |
| 2712 | 2715 | ref_frame: -1, |
| 2713 | 2716 | ref_frame1: None, |
| 2717 | + mv1: None, | |
| 2714 | 2718 | mv: (0, 0), |
| 2715 | 2719 | is_new_mv: false, |
| 2716 | 2720 | size: 4, |
| @@ -2755,6 +2759,7 @@ pub(crate) fn encode_inter_frame( | ||
| 2755 | 2759 | is_inter: true, |
| 2756 | 2760 | ref_frame: LAST_FRAME, |
| 2757 | 2761 | ref_frame1: None, |
| 2762 | + mv1: None, | |
| 2758 | 2763 | mv: info.mv, |
| 2759 | 2764 | is_new_mv: matches!( |
| 2760 | 2765 | info.mode, |
| @@ -2766,6 +2771,7 @@ pub(crate) fn encode_inter_frame( | ||
| 2766 | 2771 | is_inter: false, |
| 2767 | 2772 | ref_frame: -1, |
| 2768 | 2773 | ref_frame1: None, |
| 2774 | + mv1: None, | |
| 2769 | 2775 | mv: (0, 0), |
| 2770 | 2776 | is_new_mv: false, |
| 2771 | 2777 | size: 2, |
crates/ec-av1/src/mvstack.rsmodified+856 -0
| @@ -131,6 +131,12 @@ pub struct MiInfo { | ||
| 131 | 131 | pub ref_frame1: Option<i8>, |
| 132 | 132 | /// The unit's motion vector, `(row, col)`, in the spec's 1/8-pel units. |
| 133 | 133 | pub mv: (i32, i32), |
| 134 | + /// The unit's second motion vector, matching `ref_frame1` (spec | |
| 135 | + /// `Mvs[1]`) — `Some` only for a compound-coded unit, `None` otherwise. | |
| 136 | + /// lane-av1comp step 4: exists so the compound mvstack scan below can | |
| 137 | + /// read a compound neighbour's second MV; no caller sets it yet (same | |
| 138 | + /// unwired state `ref_frame1` landed in for r3). | |
| 139 | + pub mv1: Option<(i32, i32)>, | |
| 134 | 140 | /// Whether this unit's own mode was `NEWMV` (spec's |
| 135 | 141 | /// `have_newmv_in_inter_mode`, compound modes excluded since this module |
| 136 | 142 | /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a |
| @@ -985,6 +991,661 @@ pub fn find_mv_stack_with_sign_bias( | ||
| 985 | 991 | } |
| 986 | 992 | } |
| 987 | 993 | |
| 994 | +/// One neighbour's vote for a compound-reference (spec `RefFrame[0]` *and* | |
| 995 | +/// `RefFrame[1]` both active) candidate: two full motion vectors and the | |
| 996 | +/// combined weight — spec 7.10.2's compound half of `add_ref_mv_candidate` | |
| 997 | +/// (libaom `mvref_common.c`, the `is_compound` branch of `scan_row_mbmi`/ | |
| 998 | +/// `scan_col_mbmi`/`scan_blk_mbmi`). | |
| 999 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 1000 | +pub struct CompoundStackEntry { | |
| 1001 | + /// The candidate's vote for `RefFrame[0]`'s motion vector. | |
| 1002 | + pub mv0: (i32, i32), | |
| 1003 | + /// The candidate's vote for `RefFrame[1]`'s motion vector. | |
| 1004 | + pub mv1: (i32, i32), | |
| 1005 | + /// The summed weight of every neighbour cell that voted for this pair. | |
| 1006 | + pub weight: u32, | |
| 1007 | +} | |
| 1008 | + | |
| 1009 | +/// [`find_mv_stack_compound`]'s result — [`MvStack`]'s shape, doubled for | |
| 1010 | +/// the reference pair. | |
| 1011 | +#[derive(Clone, Debug, PartialEq, Eq)] | |
| 1012 | +pub struct CompoundMvStack { | |
| 1013 | + /// The candidates, highest weight first. | |
| 1014 | + pub entries: Vec<CompoundStackEntry>, | |
| 1015 | + /// `RefStackMv[0]` for both refs, or `((0,0),(0,0))` when the stack is | |
| 1016 | + /// empty. | |
| 1017 | + pub nearest_mv: ((i32, i32), (i32, i32)), | |
| 1018 | + /// `RefStackMv[1]` for both refs, or `((0,0),(0,0))` when the stack has | |
| 1019 | + /// fewer than two entries. | |
| 1020 | + pub near_mv: ((i32, i32), (i32, i32)), | |
| 1021 | + /// The predictor pair a `NEW_NEWMV`-family block's MV differences are | |
| 1022 | + /// coded against. | |
| 1023 | + pub pred_mv: ((i32, i32), (i32, i32)), | |
| 1024 | + /// Context for the compound `new_mv` symbol. | |
| 1025 | + pub new_mv_ctx: usize, | |
| 1026 | + /// Context for the compound `ref_mv` symbol. | |
| 1027 | + pub ref_mv_ctx: usize, | |
| 1028 | + /// Context for the compound `zero_mv` symbol. | |
| 1029 | + pub zero_mv_ctx: usize, | |
| 1030 | + /// Context for each `drl_mode` symbol between consecutive stack entries. | |
| 1031 | + pub drl_ctx: Vec<usize>, | |
| 1032 | +} | |
| 1033 | + | |
| 1034 | +/// [`add_candidate`], doubled: dedupes on the whole `(mv0, mv1)` pair. | |
| 1035 | +fn add_compound_candidate( | |
| 1036 | + candidates: &mut Vec<CompoundStackEntry>, | |
| 1037 | + mv0: (i32, i32), | |
| 1038 | + mv1: (i32, i32), | |
| 1039 | + weight: u32, | |
| 1040 | +) { | |
| 1041 | + if let Some(entry) = candidates.iter_mut().find(|e| e.mv0 == mv0 && e.mv1 == mv1) { | |
| 1042 | + entry.weight += weight; | |
| 1043 | + } else { | |
| 1044 | + candidates.push(CompoundStackEntry { mv0, mv1, weight }); | |
| 1045 | + } | |
| 1046 | +} | |
| 1047 | + | |
| 1048 | +/// The compound row scan (spec 7.10.2.2's compound branch, libaom | |
| 1049 | +/// `scan_row_mbmi`'s `is_compound` path): [`scan_row`]'s weight/run-length | |
| 1050 | +/// math unchanged, but a candidate only votes when *both* its `ref_frame` | |
| 1051 | +/// and `ref_frame1` equal the query's pair exactly, in that order — a | |
| 1052 | +/// compound pair is always encoded forward-then-backward | |
| 1053 | +/// (`RefFrame[0]`/`RefFrame[1]`), so unlike [`process_single_ref_mv_candidate`]'s | |
| 1054 | +/// extension pass, no sign-bias flip ever applies to an exact-pair scan | |
| 1055 | +/// match (libaom's own `scan_row_mbmi` compound branch doesn't flip either). | |
| 1056 | +#[allow(clippy::too_many_arguments)] | |
| 1057 | +fn scan_row_compound( | |
| 1058 | + grid: &MiGrid, | |
| 1059 | + mi_row: usize, | |
| 1060 | + mi_col: usize, | |
| 1061 | + bw4: usize, | |
| 1062 | + row_offset: isize, | |
| 1063 | + max_row_offset: isize, | |
| 1064 | + ref_frame: (i8, i8), | |
| 1065 | + candidates: &mut Vec<CompoundStackEntry>, | |
| 1066 | + newmv_count: &mut u32, | |
| 1067 | + processed_rows: &mut usize, | |
| 1068 | +) -> bool { | |
| 1069 | + let Some(row) = mi_row.checked_add_signed(row_offset) else { | |
| 1070 | + return false; | |
| 1071 | + }; | |
| 1072 | + let col_shift: usize = if row_offset.unsigned_abs() > 1 { 1 } else { 0 }; | |
| 1073 | + let use_step_16 = bw4 >= 16; | |
| 1074 | + let end_mi = bw4.min(16); | |
| 1075 | + let mut found = false; | |
| 1076 | + let mut i = 0usize; | |
| 1077 | + while i < end_mi { | |
| 1078 | + let Some(info) = grid.get(row, mi_col + col_shift + i) else { | |
| 1079 | + i += 1; | |
| 1080 | + continue; | |
| 1081 | + }; | |
| 1082 | + let n4 = info.size; | |
| 1083 | + let mut len = bw4.min(n4); | |
| 1084 | + if use_step_16 { | |
| 1085 | + len = len.max(4); | |
| 1086 | + } else if row_offset.unsigned_abs() > 1 { | |
| 1087 | + len = len.max(2); | |
| 1088 | + } | |
| 1089 | + let mut weight = ROW_COL_WEIGHT_FLOOR; | |
| 1090 | + if bw4 <= n4 { | |
| 1091 | + let inc = ((-max_row_offset + row_offset + 1) as usize).min(n4); | |
| 1092 | + weight = weight.max(inc as u32); | |
| 1093 | + *processed_rows = (inc as isize - row_offset - 1).max(0) as usize; | |
| 1094 | + } | |
| 1095 | + if info.is_inter && info.ref_frame == ref_frame.0 && info.ref_frame1 == Some(ref_frame.1) { | |
| 1096 | + found = true; | |
| 1097 | + add_compound_candidate( | |
| 1098 | + candidates, | |
| 1099 | + info.mv, | |
| 1100 | + info.mv1.unwrap_or((0, 0)), | |
| 1101 | + len as u32 * weight, | |
| 1102 | + ); | |
| 1103 | + *newmv_count += u32::from(info.is_new_mv); | |
| 1104 | + } | |
| 1105 | + i += len.max(1); | |
| 1106 | + } | |
| 1107 | + found | |
| 1108 | +} | |
| 1109 | + | |
| 1110 | +/// The compound col scan (spec 7.10.2.3's compound branch): [`scan_row_compound`], | |
| 1111 | +/// transposed — mirrors [`scan_col`]/[`scan_row`]'s own relationship. | |
| 1112 | +#[allow(clippy::too_many_arguments)] | |
| 1113 | +fn scan_col_compound( | |
| 1114 | + grid: &MiGrid, | |
| 1115 | + mi_row: usize, | |
| 1116 | + mi_col: usize, | |
| 1117 | + bh4: usize, | |
| 1118 | + col_offset: isize, | |
| 1119 | + max_col_offset: isize, | |
| 1120 | + ref_frame: (i8, i8), | |
| 1121 | + candidates: &mut Vec<CompoundStackEntry>, | |
| 1122 | + newmv_count: &mut u32, | |
| 1123 | + processed_cols: &mut usize, | |
| 1124 | +) -> bool { | |
| 1125 | + let Some(col) = mi_col.checked_add_signed(col_offset) else { | |
| 1126 | + return false; | |
| 1127 | + }; | |
| 1128 | + let row_shift: usize = if col_offset.unsigned_abs() > 1 { 1 } else { 0 }; | |
| 1129 | + let use_step_16 = bh4 >= 16; | |
| 1130 | + let end_mi = bh4.min(16); | |
| 1131 | + let mut found = false; | |
| 1132 | + let mut i = 0usize; | |
| 1133 | + while i < end_mi { | |
| 1134 | + let Some(info) = grid.get(mi_row + row_shift + i, col) else { | |
| 1135 | + i += 1; | |
| 1136 | + continue; | |
| 1137 | + }; | |
| 1138 | + let n4 = info.size; | |
| 1139 | + let mut len = bh4.min(n4); | |
| 1140 | + if use_step_16 { | |
| 1141 | + len = len.max(4); | |
| 1142 | + } else if col_offset.unsigned_abs() > 1 { | |
| 1143 | + len = len.max(2); | |
| 1144 | + } | |
| 1145 | + let mut weight = ROW_COL_WEIGHT_FLOOR; | |
| 1146 | + if bh4 <= n4 { | |
| 1147 | + let inc = ((-max_col_offset + col_offset + 1) as usize).min(n4); | |
| 1148 | + weight = weight.max(inc as u32); | |
| 1149 | + *processed_cols = (inc as isize - col_offset - 1).max(0) as usize; | |
| 1150 | + } | |
| 1151 | + if info.is_inter && info.ref_frame == ref_frame.0 && info.ref_frame1 == Some(ref_frame.1) { | |
| 1152 | + found = true; | |
| 1153 | + add_compound_candidate( | |
| 1154 | + candidates, | |
| 1155 | + info.mv, | |
| 1156 | + info.mv1.unwrap_or((0, 0)), | |
| 1157 | + len as u32 * weight, | |
| 1158 | + ); | |
| 1159 | + *newmv_count += u32::from(info.is_new_mv); | |
| 1160 | + } | |
| 1161 | + i += len.max(1); | |
| 1162 | + } | |
| 1163 | + found | |
| 1164 | +} | |
| 1165 | + | |
| 1166 | +/// The compound top-right probe ([`scan_top_right`]'s pair-matched twin). | |
| 1167 | +fn scan_top_right_compound( | |
| 1168 | + grid: &MiGrid, | |
| 1169 | + mi_row: usize, | |
| 1170 | + mi_col: usize, | |
| 1171 | + bw4: usize, | |
| 1172 | + ref_frame: (i8, i8), | |
| 1173 | + candidates: &mut Vec<CompoundStackEntry>, | |
| 1174 | + newmv_count: &mut u32, | |
| 1175 | +) -> bool { | |
| 1176 | + let Some(row) = mi_row.checked_sub(1) else { | |
| 1177 | + return false; | |
| 1178 | + }; | |
| 1179 | + let col = mi_col + bw4; | |
| 1180 | + if let Some(info) = grid.get(row, col) | |
| 1181 | + && info.is_inter | |
| 1182 | + && info.ref_frame == ref_frame.0 | |
| 1183 | + && info.ref_frame1 == Some(ref_frame.1) | |
| 1184 | + { | |
| 1185 | + add_compound_candidate( | |
| 1186 | + candidates, | |
| 1187 | + info.mv, | |
| 1188 | + info.mv1.unwrap_or((0, 0)), | |
| 1189 | + CORNER_WEIGHT, | |
| 1190 | + ); | |
| 1191 | + *newmv_count += u32::from(info.is_new_mv); | |
| 1192 | + return true; | |
| 1193 | + } | |
| 1194 | + false | |
| 1195 | +} | |
| 1196 | + | |
| 1197 | +/// The compound diagonal corner probe ([`scan_corner`]'s pair-matched twin). | |
| 1198 | +fn scan_corner_compound( | |
| 1199 | + grid: &MiGrid, | |
| 1200 | + mi_row: usize, | |
| 1201 | + mi_col: usize, | |
| 1202 | + ref_frame: (i8, i8), | |
| 1203 | + candidates: &mut Vec<CompoundStackEntry>, | |
| 1204 | + newmv_count: &mut u32, | |
| 1205 | +) -> bool { | |
| 1206 | + let (Some(row), Some(col)) = (mi_row.checked_sub(1), mi_col.checked_sub(1)) else { | |
| 1207 | + return false; | |
| 1208 | + }; | |
| 1209 | + if let Some(info) = grid.get(row, col) | |
| 1210 | + && info.is_inter | |
| 1211 | + && info.ref_frame == ref_frame.0 | |
| 1212 | + && info.ref_frame1 == Some(ref_frame.1) | |
| 1213 | + { | |
| 1214 | + add_compound_candidate( | |
| 1215 | + candidates, | |
| 1216 | + info.mv, | |
| 1217 | + info.mv1.unwrap_or((0, 0)), | |
| 1218 | + CORNER_WEIGHT, | |
| 1219 | + ); | |
| 1220 | + *newmv_count += u32::from(info.is_new_mv); | |
| 1221 | + return true; | |
| 1222 | + } | |
| 1223 | + false | |
| 1224 | +} | |
| 1225 | + | |
| 1226 | +/// Per-side unresolved-MV accumulator [`process_compound_ref_mv_candidate`] | |
| 1227 | +/// fills across every candidate the compound extension pass walks: up to two | |
| 1228 | +/// motion vectors that matched `ref_frame.0`/`.1` exactly (`ref_id`), and up | |
| 1229 | +/// to two more borrowed from a differently-referenced but still-inter | |
| 1230 | +/// neighbour under the sign-bias flip [`process_single_ref_mv_candidate`] | |
| 1231 | +/// also applies (`ref_diff`). Ported from libaom's `setup_ref_mv_list` | |
| 1232 | +/// (`mvref_common.c`, `process_compound_ref_mv_candidate` ~line 380-430 plus | |
| 1233 | +/// the combine step at ~696-762): the extension walk fills this once across | |
| 1234 | +/// the whole row-then-col scan, and [`combine_compound_candidates`] below | |
| 1235 | +/// zips the two independently-filled sides back into full pair candidates. | |
| 1236 | +#[derive(Default)] | |
| 1237 | +struct CompoundRefLists { | |
| 1238 | + ref_id: [Vec<(i32, i32)>; 2], | |
| 1239 | + ref_diff: [Vec<(i32, i32)>; 2], | |
| 1240 | +} | |
| 1241 | + | |
| 1242 | +/// libaom's `process_compound_ref_mv_candidate`: folds one already-coded | |
| 1243 | +/// neighbour's up to two reference slots into `lists`, split by which side | |
| 1244 | +/// of `ref_frame` (the query block's own compound pair) each slot's own | |
| 1245 | +/// reference frame matches — an exact match goes to that side's `ref_id` | |
| 1246 | +/// list, anything else (still inter, wrong reference) goes to `ref_diff` | |
| 1247 | +/// under the usual sign-bias flip. | |
| 1248 | +fn process_compound_ref_mv_candidate( | |
| 1249 | + candidate: &MiInfo, | |
| 1250 | + ref_frame: (i8, i8), | |
| 1251 | + sign_bias_table: &SignBiasTable, | |
| 1252 | + lists: &mut CompoundRefLists, | |
| 1253 | +) { | |
| 1254 | + if !candidate.is_inter { | |
| 1255 | + return; | |
| 1256 | + } | |
| 1257 | + let slots = [ | |
| 1258 | + Some((candidate.ref_frame, candidate.mv)), | |
| 1259 | + candidate | |
| 1260 | + .ref_frame1 | |
| 1261 | + .map(|rf1| (rf1, candidate.mv1.unwrap_or((0, 0)))), | |
| 1262 | + ]; | |
| 1263 | + for (candidate_ref, candidate_mv) in slots.into_iter().flatten() { | |
| 1264 | + for (i, side_ref) in [ref_frame.0, ref_frame.1].into_iter().enumerate() { | |
| 1265 | + if candidate_ref == side_ref { | |
| 1266 | + if lists.ref_id[i].len() < 2 { | |
| 1267 | + lists.ref_id[i].push(candidate_mv); | |
| 1268 | + } | |
| 1269 | + } else if lists.ref_diff[i].len() < 2 { | |
| 1270 | + let mut mv = candidate_mv; | |
| 1271 | + if sign_bias(sign_bias_table, candidate_ref) != sign_bias(sign_bias_table, side_ref) | |
| 1272 | + { | |
| 1273 | + mv = (-mv.0, -mv.1); | |
| 1274 | + } | |
| 1275 | + lists.ref_diff[i].push(mv); | |
| 1276 | + } | |
| 1277 | + } | |
| 1278 | + } | |
| 1279 | +} | |
| 1280 | + | |
| 1281 | +/// libaom's combine step (`setup_ref_mv_list`, `mvref_common.c` ~696-720): | |
| 1282 | +/// zips each side's `ref_id` (exact matches) then `ref_diff` (borrowed, | |
| 1283 | +/// sign-flipped) entries, position-wise and independently per side, into up | |
| 1284 | +/// to two full `(mv0, mv1)` pair candidates — missing slots default to | |
| 1285 | +/// `(0, 0)`, matching libaom's zero-initialized `combined_mvs`. | |
| 1286 | +fn combine_compound_candidates(lists: &CompoundRefLists) -> [((i32, i32), (i32, i32)); 2] { | |
| 1287 | + let side_slots = |side: usize| -> [(i32, i32); 2] { | |
| 1288 | + let merged: Vec<(i32, i32)> = lists.ref_id[side] | |
| 1289 | + .iter() | |
| 1290 | + .chain(lists.ref_diff[side].iter()) | |
| 1291 | + .take(2) | |
| 1292 | + .copied() | |
| 1293 | + .collect(); | |
| 1294 | + std::array::from_fn(|i| merged.get(i).copied().unwrap_or((0, 0))) | |
| 1295 | + }; | |
| 1296 | + let side0 = side_slots(0); | |
| 1297 | + let side1 = side_slots(1); | |
| 1298 | + [(side0[0], side1[0]), (side0[1], side1[1])] | |
| 1299 | +} | |
| 1300 | + | |
| 1301 | +/// [`find_mv_stack_with_sign_bias`]'s compound-reference twin (spec | |
| 1302 | +/// 7.10.2's `is_compound` path throughout): builds the reference MV stack | |
| 1303 | +/// for a block predicted against *two* simultaneously-active reference | |
| 1304 | +/// frames (`ref_frame.0`, `ref_frame.1`), keeping every existing | |
| 1305 | +/// single-reference caller of [`find_mv_stack`]/[`find_mv_stack_with_sign_bias`] | |
| 1306 | +/// untouched — this is a new, parallel entry point, not an extension of the | |
| 1307 | +/// single-ref one. Same reduction scope as the single-ref module doc | |
| 1308 | +/// (single tile, square blocks, `IDENTITY` global motion only); the | |
| 1309 | +/// mode-context switch below reuses the identical `(nearest_match, | |
| 1310 | +/// ref_match_count)` derivation the single-ref path uses, since libaom's own | |
| 1311 | +/// switch statement (`mvref_common.c` ~619-643) is ref-count-agnostic — it | |
| 1312 | +/// only ever reads those two counts, regardless of which `MODE_CTX_REF_FRAMES` | |
| 1313 | +/// slot `mode_context` it's indexing. | |
| 1314 | +/// | |
| 1315 | +/// Deviation from libaom's real compound extension combine step | |
| 1316 | +/// (`setup_ref_mv_list`, ~line 715): when the stack already has exactly one | |
| 1317 | +/// entry, libaom compares the first combined candidate against that entry | |
| 1318 | +/// and *skips* adding it on a match (`is_dup`, no weight change) rather than | |
| 1319 | +/// folding it in; this port instead runs every combined candidate through | |
| 1320 | +/// [`add_compound_candidate`]'s ordinary dedup, which bumps the existing | |
| 1321 | +/// entry's weight on a match. Weight-only, never mv/rank -- affects `drl_ctx` | |
| 1322 | +/// (a `REF_CAT_LEVEL`-boosted entry crosses that threshold slightly earlier | |
| 1323 | +/// than real libaom would in this exact corner) but not `nearest_mv`/`near_mv`. | |
| 1324 | +/// Documented rather than fixed this round for budget; r5 or later should | |
| 1325 | +/// port the real `is_dup` check if a live compound gate's `drl_ctx` disagrees. | |
| 1326 | +#[allow(clippy::too_many_arguments)] | |
| 1327 | +pub fn find_mv_stack_compound( | |
| 1328 | + grid: &MiGrid, | |
| 1329 | + mi_row: usize, | |
| 1330 | + mi_col: usize, | |
| 1331 | + bw4: usize, | |
| 1332 | + bh4: usize, | |
| 1333 | + ref_frame: (i8, i8), | |
| 1334 | + mi_cols: usize, | |
| 1335 | + mi_rows: usize, | |
| 1336 | + sign_bias_table: &SignBiasTable, | |
| 1337 | + tpl: Option<CompoundTplArgs>, | |
| 1338 | +) -> CompoundMvStack { | |
| 1339 | + let mut candidates: Vec<CompoundStackEntry> = Vec::new(); | |
| 1340 | + let mut newmv_count = 0u32; | |
| 1341 | + | |
| 1342 | + let row_adj = bh4 < 2 && mi_row % 2 == 1; | |
| 1343 | + let col_adj = bw4 < 2 && mi_col % 2 == 1; | |
| 1344 | + let max_row_offset: isize = if mi_row > 0 { | |
| 1345 | + let reach = if bh4 < 2 { -4 } else { -6 } + isize::from(row_adj); | |
| 1346 | + reach.max(-(mi_row as isize)) | |
| 1347 | + } else { | |
| 1348 | + 0 | |
| 1349 | + }; | |
| 1350 | + let max_col_offset: isize = if mi_col > 0 { | |
| 1351 | + let reach = if bw4 < 2 { -4 } else { -6 } + isize::from(col_adj); | |
| 1352 | + reach.max(-(mi_col as isize)) | |
| 1353 | + } else { | |
| 1354 | + 0 | |
| 1355 | + }; | |
| 1356 | + | |
| 1357 | + let mut processed_rows = 0usize; | |
| 1358 | + let mut processed_cols = 0usize; | |
| 1359 | + let found_above = mi_row > 0 | |
| 1360 | + && scan_row_compound( | |
| 1361 | + grid, | |
| 1362 | + mi_row, | |
| 1363 | + mi_col, | |
| 1364 | + bw4, | |
| 1365 | + -1, | |
| 1366 | + max_row_offset, | |
| 1367 | + ref_frame, | |
| 1368 | + &mut candidates, | |
| 1369 | + &mut newmv_count, | |
| 1370 | + &mut processed_rows, | |
| 1371 | + ); | |
| 1372 | + let found_left = mi_col > 0 | |
| 1373 | + && scan_col_compound( | |
| 1374 | + grid, | |
| 1375 | + mi_row, | |
| 1376 | + mi_col, | |
| 1377 | + bh4, | |
| 1378 | + -1, | |
| 1379 | + max_col_offset, | |
| 1380 | + ref_frame, | |
| 1381 | + &mut candidates, | |
| 1382 | + &mut newmv_count, | |
| 1383 | + &mut processed_cols, | |
| 1384 | + ); | |
| 1385 | + let found_top_right = scan_top_right_compound( | |
| 1386 | + grid, | |
| 1387 | + mi_row, | |
| 1388 | + mi_col, | |
| 1389 | + bw4, | |
| 1390 | + ref_frame, | |
| 1391 | + &mut candidates, | |
| 1392 | + &mut newmv_count, | |
| 1393 | + ); | |
| 1394 | + | |
| 1395 | + let row_matched = found_above || found_top_right; | |
| 1396 | + let nearest_match = usize::from(row_matched) + usize::from(found_left); | |
| 1397 | + | |
| 1398 | + for entry in &mut candidates { | |
| 1399 | + entry.weight += REF_CAT_LEVEL; | |
| 1400 | + } | |
| 1401 | + | |
| 1402 | + // Temporal compound candidates (spec 7.10.2.8's compound | |
| 1403 | + // `add_tpl_ref_mv`): the same projected field, read once per side of the | |
| 1404 | + // pair through the existing single-ref [`crate::motion_field::add_tpl_ref_mv`] | |
| 1405 | + // — each side's own `cur_offset` scales the *same* stored candidate to | |
| 1406 | + // that side's reference frame. A combined pair candidate is only added | |
| 1407 | + // when *both* sides land a projection (libaom folds a missing side to | |
| 1408 | + // `(0,0)` rather than dropping the candidate entirely; this reduction | |
| 1409 | + // drops it instead — a documented, conservative deviation: fewer | |
| 1410 | + // candidates than the real bitstream, never more). | |
| 1411 | + let mut zero_mv_ctx = 0usize; | |
| 1412 | + if let Some(CompoundTplArgs { | |
| 1413 | + field, | |
| 1414 | + cur_offset_0, | |
| 1415 | + cur_offset_1, | |
| 1416 | + allow_high_precision_mv, | |
| 1417 | + }) = tpl | |
| 1418 | + { | |
| 1419 | + let mut any_hit = false; | |
| 1420 | + let blk_row_end = bh4.min(16); | |
| 1421 | + let blk_col_end = bw4.min(16); | |
| 1422 | + let step_h = if bh4 >= 16 { 4 } else { 2 }; | |
| 1423 | + let step_w = if bw4 >= 16 { 4 } else { 2 }; | |
| 1424 | + let mut first_sample_missing = true; | |
| 1425 | + let mut first_sample_far = false; | |
| 1426 | + let mut blk_row = 0usize; | |
| 1427 | + while blk_row < blk_row_end { | |
| 1428 | + let mut blk_col = 0usize; | |
| 1429 | + while blk_col < blk_col_end { | |
| 1430 | + let cand0 = crate::motion_field::add_tpl_ref_mv( | |
| 1431 | + field, | |
| 1432 | + mi_row, | |
| 1433 | + mi_col, | |
| 1434 | + blk_row as isize, | |
| 1435 | + blk_col as isize, | |
| 1436 | + cur_offset_0, | |
| 1437 | + allow_high_precision_mv, | |
| 1438 | + ); | |
| 1439 | + let cand1 = crate::motion_field::add_tpl_ref_mv( | |
| 1440 | + field, | |
| 1441 | + mi_row, | |
| 1442 | + mi_col, | |
| 1443 | + blk_row as isize, | |
| 1444 | + blk_col as isize, | |
| 1445 | + cur_offset_1, | |
| 1446 | + allow_high_precision_mv, | |
| 1447 | + ); | |
| 1448 | + if let (Some(c0), Some(c1)) = (cand0, cand1) { | |
| 1449 | + any_hit = true; | |
| 1450 | + if blk_row == 0 && blk_col == 0 { | |
| 1451 | + first_sample_missing = false; | |
| 1452 | + first_sample_far = c0.mv.0.abs() >= 16 | |
| 1453 | + || c0.mv.1.abs() >= 16 | |
| 1454 | + || c1.mv.0.abs() >= 16 | |
| 1455 | + || c1.mv.1.abs() >= 16; | |
| 1456 | + } | |
| 1457 | + add_compound_candidate(&mut candidates, c0.mv, c1.mv, 2); | |
| 1458 | + } | |
| 1459 | + blk_col += step_w; | |
| 1460 | + } | |
| 1461 | + blk_row += step_h; | |
| 1462 | + } | |
| 1463 | + if any_hit { | |
| 1464 | + TMV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 1465 | + } | |
| 1466 | + zero_mv_ctx = usize::from(first_sample_missing || first_sample_far); | |
| 1467 | + } | |
| 1468 | + | |
| 1469 | + let mut dummy_newmv_count = 0u32; | |
| 1470 | + let corner_matched = scan_corner_compound( | |
| 1471 | + grid, | |
| 1472 | + mi_row, | |
| 1473 | + mi_col, | |
| 1474 | + ref_frame, | |
| 1475 | + &mut candidates, | |
| 1476 | + &mut dummy_newmv_count, | |
| 1477 | + ); | |
| 1478 | + let mut row_matched_ext = false; | |
| 1479 | + let mut col_matched_ext = false; | |
| 1480 | + for idx in 2..=MVREF_ROW_COLS { | |
| 1481 | + let row_offset = -((idx as isize) * 2) + 1 + isize::from(row_adj); | |
| 1482 | + let col_offset = -((idx as isize) * 2) + 1 + isize::from(col_adj); | |
| 1483 | + if row_offset.unsigned_abs() <= max_row_offset.unsigned_abs() | |
| 1484 | + && row_offset.unsigned_abs() > processed_rows | |
| 1485 | + && scan_row_compound( | |
| 1486 | + grid, | |
| 1487 | + mi_row, | |
| 1488 | + mi_col, | |
| 1489 | + bw4, | |
| 1490 | + row_offset, | |
| 1491 | + max_row_offset, | |
| 1492 | + ref_frame, | |
| 1493 | + &mut candidates, | |
| 1494 | + &mut dummy_newmv_count, | |
| 1495 | + &mut processed_rows, | |
| 1496 | + ) | |
| 1497 | + { | |
| 1498 | + row_matched_ext = true; | |
| 1499 | + } | |
| 1500 | + if col_offset.unsigned_abs() <= max_col_offset.unsigned_abs() | |
| 1501 | + && col_offset.unsigned_abs() > processed_cols | |
| 1502 | + && scan_col_compound( | |
| 1503 | + grid, | |
| 1504 | + mi_row, | |
| 1505 | + mi_col, | |
| 1506 | + bh4, | |
| 1507 | + col_offset, | |
| 1508 | + max_col_offset, | |
| 1509 | + ref_frame, | |
| 1510 | + &mut candidates, | |
| 1511 | + &mut dummy_newmv_count, | |
| 1512 | + &mut processed_cols, | |
| 1513 | + ) | |
| 1514 | + { | |
| 1515 | + col_matched_ext = true; | |
| 1516 | + } | |
| 1517 | + } | |
| 1518 | + let ref_match_count = usize::from(row_matched || corner_matched || row_matched_ext) | |
| 1519 | + + usize::from(found_left || col_matched_ext); | |
| 1520 | + | |
| 1521 | + candidates.sort_by_key(|e| std::cmp::Reverse(e.weight)); | |
| 1522 | + candidates.truncate(MAX_STACK_SIZE); | |
| 1523 | + | |
| 1524 | + // The compound extension pass (libaom's `setup_ref_mv_list`, "Handle | |
| 1525 | + // compound reference frame extension"): gather across the row directly | |
| 1526 | + // above and the column directly left (same `mi_size` reach as the | |
| 1527 | + // single-ref pass), then combine once. | |
| 1528 | + let mi_width = (16usize).min(bw4).min(mi_cols.saturating_sub(mi_col)); | |
| 1529 | + let mi_height = (16usize).min(bh4).min(mi_rows.saturating_sub(mi_row)); | |
| 1530 | + let mi_size = mi_width.min(mi_height); | |
| 1531 | + if candidates.len() < MAX_MV_REF_CANDIDATES { | |
| 1532 | + let mut lists = CompoundRefLists::default(); | |
| 1533 | + if max_row_offset.unsigned_abs() >= 1 { | |
| 1534 | + let mut idx = 0usize; | |
| 1535 | + while idx < mi_size { | |
| 1536 | + let cand = grid.get(mi_row - 1, mi_col + idx); | |
| 1537 | + let step = cand.map_or(1, |c| c.size).max(1); | |
| 1538 | + if let Some(c) = cand { | |
| 1539 | + process_compound_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut lists); | |
| 1540 | + } | |
| 1541 | + idx += step; | |
| 1542 | + } | |
| 1543 | + } | |
| 1544 | + if max_col_offset.unsigned_abs() >= 1 { | |
| 1545 | + let mut idx = 0usize; | |
| 1546 | + while idx < mi_size { | |
| 1547 | + let cand = grid.get(mi_row + idx, mi_col - 1); | |
| 1548 | + let step = cand.map_or(1, |c| c.size).max(1); | |
| 1549 | + if let Some(c) = cand { | |
| 1550 | + process_compound_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut lists); | |
| 1551 | + } | |
| 1552 | + idx += step; | |
| 1553 | + } | |
| 1554 | + } | |
| 1555 | + for (mv0, mv1) in combine_compound_candidates(&lists) { | |
| 1556 | + if candidates.len() < MAX_MV_REF_CANDIDATES { | |
| 1557 | + add_compound_candidate(&mut candidates, mv0, mv1, 2); | |
| 1558 | + } | |
| 1559 | + } | |
| 1560 | + } | |
| 1561 | + | |
| 1562 | + let clamp = |mv| clamp_mv_ref(mv, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows); | |
| 1563 | + let nearest_mv = candidates | |
| 1564 | + .first() | |
| 1565 | + .map_or(((0, 0), (0, 0)), |e| (clamp(e.mv0), clamp(e.mv1))); | |
| 1566 | + let near_mv = candidates | |
| 1567 | + .get(1) | |
| 1568 | + .map_or(((0, 0), (0, 0)), |e| (clamp(e.mv0), clamp(e.mv1))); | |
| 1569 | + let pred_mv = if candidates.is_empty() { | |
| 1570 | + ((0, 0), (0, 0)) | |
| 1571 | + } else { | |
| 1572 | + nearest_mv | |
| 1573 | + }; | |
| 1574 | + | |
| 1575 | + let new_mv_ctx = match nearest_match { | |
| 1576 | + 0 => usize::from(ref_match_count >= 1), | |
| 1577 | + 1 => { | |
| 1578 | + if newmv_count > 0 { | |
| 1579 | + 2 | |
| 1580 | + } else { | |
| 1581 | + 3 | |
| 1582 | + } | |
| 1583 | + } | |
| 1584 | + _ => { | |
| 1585 | + if newmv_count >= 1 { | |
| 1586 | + 4 | |
| 1587 | + } else { | |
| 1588 | + 5 | |
| 1589 | + } | |
| 1590 | + } | |
| 1591 | + }; | |
| 1592 | + let ref_mv_ctx = match nearest_match { | |
| 1593 | + 0 => match ref_match_count { | |
| 1594 | + 0 => 0, | |
| 1595 | + 1 => 1, | |
| 1596 | + _ => 2, | |
| 1597 | + }, | |
| 1598 | + 1 => { | |
| 1599 | + if ref_match_count >= 2 { | |
| 1600 | + 4 | |
| 1601 | + } else { | |
| 1602 | + 3 | |
| 1603 | + } | |
| 1604 | + } | |
| 1605 | + _ => 5, | |
| 1606 | + }; | |
| 1607 | + | |
| 1608 | + let drl_ctx = candidates | |
| 1609 | + .windows(2) | |
| 1610 | + .map(|w| { | |
| 1611 | + let (a, b) = (w[0].weight, w[1].weight); | |
| 1612 | + if a >= REF_CAT_LEVEL && b >= REF_CAT_LEVEL { | |
| 1613 | + 0 | |
| 1614 | + } else if a >= REF_CAT_LEVEL && b < REF_CAT_LEVEL { | |
| 1615 | + 1 | |
| 1616 | + } else { | |
| 1617 | + 2 | |
| 1618 | + } | |
| 1619 | + }) | |
| 1620 | + .collect(); | |
| 1621 | + | |
| 1622 | + CompoundMvStack { | |
| 1623 | + entries: candidates, | |
| 1624 | + nearest_mv, | |
| 1625 | + near_mv, | |
| 1626 | + pred_mv, | |
| 1627 | + new_mv_ctx, | |
| 1628 | + ref_mv_ctx, | |
| 1629 | + zero_mv_ctx, | |
| 1630 | + drl_ctx, | |
| 1631 | + } | |
| 1632 | +} | |
| 1633 | + | |
| 1634 | +/// [`find_mv_stack_compound`]'s temporal-MV inputs — [`TplArgs`], doubled: | |
| 1635 | +/// one `cur_offset` per side of the pair (`get_relative_dist(OrderHint, | |
| 1636 | +/// OrderHints[ref_frame.N - LAST_FRAME])`, spec 7.9.3), since a compound | |
| 1637 | +/// query projects the same stored field twice, once per reference. | |
| 1638 | +pub struct CompoundTplArgs<'a> { | |
| 1639 | + /// The current frame's projected temporal motion field. | |
| 1640 | + pub field: &'a crate::motion_field::TplField, | |
| 1641 | + /// `get_relative_dist` for `ref_frame.0`. | |
| 1642 | + pub cur_offset_0: i32, | |
| 1643 | + /// `get_relative_dist` for `ref_frame.1`. | |
| 1644 | + pub cur_offset_1: i32, | |
| 1645 | + /// The frame header's own `allow_high_precision_mv`. | |
| 1646 | + pub allow_high_precision_mv: bool, | |
| 1647 | +} | |
| 1648 | + | |
| 988 | 1649 | /// The single-reference context shared by the `single_ref_p1`/`p3`/`p4` |
| 989 | 1650 | /// binary decisions this crate's LAST-only reference chain codes (spec |
| 990 | 1651 | /// 5.11.25; libaom's `av1_get_pred_context_single_ref_p1/p3/p4`, |
| @@ -1188,6 +1849,7 @@ mod tests { | ||
| 1188 | 1849 | is_inter: true, |
| 1189 | 1850 | ref_frame: 1, |
| 1190 | 1851 | ref_frame1: None, |
| 1852 | + mv1: None, | |
| 1191 | 1853 | mv, |
| 1192 | 1854 | is_new_mv: false, |
| 1193 | 1855 | // 1: smaller than every `bw4`/`bh4` these small-grid tests use, |
| @@ -1340,6 +2002,7 @@ mod tests { | ||
| 1340 | 2002 | is_inter: true, |
| 1341 | 2003 | ref_frame: 1, |
| 1342 | 2004 | ref_frame1: None, |
| 2005 | + mv1: None, | |
| 1343 | 2006 | mv: (4, 4), |
| 1344 | 2007 | is_new_mv: true, |
| 1345 | 2008 | size: 1, |
| @@ -1419,6 +2082,7 @@ mod tests { | ||
| 1419 | 2082 | is_inter: true, |
| 1420 | 2083 | ref_frame: 1, |
| 1421 | 2084 | ref_frame1: None, |
| 2085 | + mv1: None, | |
| 1422 | 2086 | mv, |
| 1423 | 2087 | is_new_mv: false, |
| 1424 | 2088 | size: 8, |
| @@ -1447,6 +2111,7 @@ mod tests { | ||
| 1447 | 2111 | is_inter: false, |
| 1448 | 2112 | ref_frame: -1, |
| 1449 | 2113 | ref_frame1: None, |
| 2114 | + mv1: None, | |
| 1450 | 2115 | mv: (0, 0), |
| 1451 | 2116 | is_new_mv: false, |
| 1452 | 2117 | size: 8, |
| @@ -1462,6 +2127,7 @@ mod tests { | ||
| 1462 | 2127 | is_inter: true, |
| 1463 | 2128 | ref_frame: 1, |
| 1464 | 2129 | ref_frame1: None, |
| 2130 | + mv1: None, | |
| 1465 | 2131 | mv, |
| 1466 | 2132 | is_new_mv: false, |
| 1467 | 2133 | size: 8, |
| @@ -1688,4 +2354,194 @@ mod tests { | ||
| 1688 | 2354 | assert_eq!(uni_comp_ref_p1_ctx(Some(LAST3_FRAME), None), 0); |
| 1689 | 2355 | assert_eq!(uni_comp_ref_p1_ctx(None, None), 1); |
| 1690 | 2356 | } |
| 2357 | + | |
| 2358 | + // --- lane-av1comp step 4: compound mvstack (find_mv_stack_compound) --- | |
| 2359 | + | |
| 2360 | + const COMP_PAIR: (i8, i8) = (LAST_FRAME, ALTREF_FRAME); | |
| 2361 | + | |
| 2362 | + fn comp_inter(mv0: (i32, i32), mv1: (i32, i32)) -> MiInfo { | |
| 2363 | + MiInfo { | |
| 2364 | + is_inter: true, | |
| 2365 | + ref_frame: COMP_PAIR.0, | |
| 2366 | + ref_frame1: Some(COMP_PAIR.1), | |
| 2367 | + mv: mv0, | |
| 2368 | + mv1: Some(mv1), | |
| 2369 | + is_new_mv: false, | |
| 2370 | + size: 1, | |
| 2371 | + } | |
| 2372 | + } | |
| 2373 | + | |
| 2374 | + #[test] | |
| 2375 | + fn compound_scan_only_matches_the_exact_pair() { | |
| 2376 | + let mut grid = MiGrid::new(8, 8); | |
| 2377 | + // Same-side single-ref neighbour (LAST only) must not vote. | |
| 2378 | + grid.set(1, 2, inter((4, 4))); | |
| 2379 | + // Wrong second ref (GOLDEN, not ALTREF) must not vote either. | |
| 2380 | + grid.set( | |
| 2381 | + 1, | |
| 2382 | + 3, | |
| 2383 | + MiInfo { | |
| 2384 | + is_inter: true, | |
| 2385 | + ref_frame: LAST_FRAME, | |
| 2386 | + ref_frame1: Some(GOLDEN_FRAME), | |
| 2387 | + mv: (9, 9), | |
| 2388 | + mv1: Some((9, 9)), | |
| 2389 | + is_new_mv: false, | |
| 2390 | + size: 1, | |
| 2391 | + }, | |
| 2392 | + ); | |
| 2393 | + // Exact pair match. | |
| 2394 | + let (mv0, mv1) = ((2, 2), (-6, 3)); | |
| 2395 | + grid.set(2, 1, comp_inter(mv0, mv1)); | |
| 2396 | + | |
| 2397 | + let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None); | |
| 2398 | + | |
| 2399 | + // The exact-pair immediate scan lands one boosted entry; the | |
| 2400 | + // compound extension pass (short stack, len < MAX_MV_REF_CANDIDATES) | |
| 2401 | + // separately tops it up with a low-weight combo built from the two | |
| 2402 | + // near-miss neighbours it's allowed to gather over regardless of | |
| 2403 | + // reference frame -- a real second entry, not spurious. | |
| 2404 | + assert_eq!(stack.entries.len(), 2); | |
| 2405 | + assert_eq!(stack.entries[0].mv0, mv0); | |
| 2406 | + assert_eq!(stack.entries[0].mv1, mv1); | |
| 2407 | + assert_eq!(stack.entries[0].weight, 2 + REF_CAT_LEVEL); | |
| 2408 | + assert_eq!(stack.nearest_mv, (mv0, mv1)); | |
| 2409 | + } | |
| 2410 | + | |
| 2411 | + #[test] | |
| 2412 | + fn compound_dedupes_same_pair_and_sums_weight() { | |
| 2413 | + let mut grid = MiGrid::new(8, 8); | |
| 2414 | + let (mv0, mv1) = ((4, 4), (-4, -4)); | |
| 2415 | + // Block at (2, 2), 2x2 mi units: above spans cols 2..4, left rows 2..4. | |
| 2416 | + grid.set(1, 2, comp_inter(mv0, mv1)); | |
| 2417 | + grid.set(1, 3, comp_inter(mv0, mv1)); | |
| 2418 | + grid.set(2, 1, comp_inter(mv0, mv1)); | |
| 2419 | + grid.set(3, 1, comp_inter(mv0, mv1)); | |
| 2420 | + | |
| 2421 | + let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None); | |
| 2422 | + | |
| 2423 | + // Every extension-pass combo the four identical-pair neighbours can | |
| 2424 | + // build also matches this same (mv0, mv1) pair, so | |
| 2425 | + // [`add_compound_candidate`]'s dedup keeps it a single entry -- | |
| 2426 | + // this reduction's own deviation from libaom's real `is_dup` | |
| 2427 | + // early-exit (which would skip the redundant combos rather than | |
| 2428 | + // folding their weight in, see [`find_mv_stack_compound`]'s doc): | |
| 2429 | + // both combined candidates the extension pass builds here land on | |
| 2430 | + // this dedup path and bump the weight instead of no-opping. | |
| 2431 | + assert_eq!(stack.entries.len(), 1); | |
| 2432 | + // 2 above + 2 left cells, weight 2 each (size:1 floor), the | |
| 2433 | + // REF_CAT_LEVEL boost every immediate-scan entry gets, plus the two | |
| 2434 | + // extension-pass combos folded in above. | |
| 2435 | + assert_eq!(stack.entries[0].weight, 8 + REF_CAT_LEVEL + 2 + 2); | |
| 2436 | + } | |
| 2437 | + | |
| 2438 | + #[test] | |
| 2439 | + fn compound_two_distinct_pairs_sort_by_weight() { | |
| 2440 | + let mut grid = MiGrid::new(8, 8); | |
| 2441 | + let above = ((4, 4), (1, 1)); | |
| 2442 | + let left = ((8, 8), (2, 2)); | |
| 2443 | + grid.set(2, 3, comp_inter(above.0, above.1)); | |
| 2444 | + grid.set(2, 4, comp_inter(above.0, above.1)); | |
| 2445 | + grid.set(3, 2, comp_inter(left.0, left.1)); | |
| 2446 | + grid.set(4, 2, comp_inter(left.0, left.1)); | |
| 2447 | + grid.set(5, 2, comp_inter(left.0, left.1)); | |
| 2448 | + | |
| 2449 | + let stack = find_mv_stack_compound(&grid, 3, 3, 2, 3, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None); | |
| 2450 | + | |
| 2451 | + assert_eq!(stack.entries.len(), 2); | |
| 2452 | + assert_eq!(stack.entries[0].mv0, left.0); | |
| 2453 | + assert_eq!(stack.entries[0].mv1, left.1); | |
| 2454 | + assert_eq!(stack.entries[1].mv0, above.0); | |
| 2455 | + assert_eq!(stack.entries[1].mv1, above.1); | |
| 2456 | + assert_eq!(stack.nearest_mv, left); | |
| 2457 | + assert_eq!(stack.near_mv, above); | |
| 2458 | + } | |
| 2459 | + | |
| 2460 | + #[test] | |
| 2461 | + fn combine_compound_candidates_zips_ref_id_then_ref_diff_per_side() { | |
| 2462 | + let mut lists = CompoundRefLists::default(); | |
| 2463 | + lists.ref_id[0].push((1, 1)); | |
| 2464 | + lists.ref_diff[0].push((2, 2)); | |
| 2465 | + lists.ref_id[1].push((3, 3)); | |
| 2466 | + // side 1 has only one entry -> comp_idx 1 falls back to ref_diff. | |
| 2467 | + lists.ref_diff[1].push((4, 4)); | |
| 2468 | + | |
| 2469 | + let combined = combine_compound_candidates(&lists); | |
| 2470 | + assert_eq!(combined[0], ((1, 1), (3, 3))); | |
| 2471 | + assert_eq!(combined[1], ((2, 2), (4, 4))); | |
| 2472 | + } | |
| 2473 | + | |
| 2474 | + #[test] | |
| 2475 | + fn process_compound_ref_mv_candidate_splits_exact_vs_borrowed() { | |
| 2476 | + let mut lists = CompoundRefLists::default(); | |
| 2477 | + // Exact match on side 0 (LAST_FRAME), wrong ref on side 1 -> borrowed | |
| 2478 | + // into ref_diff[1] with no sign flip (NO_SIGN_BIAS). | |
| 2479 | + let candidate = MiInfo { | |
| 2480 | + is_inter: true, | |
| 2481 | + ref_frame: LAST_FRAME, | |
| 2482 | + ref_frame1: None, | |
| 2483 | + mv: (5, 5), | |
| 2484 | + mv1: None, | |
| 2485 | + is_new_mv: false, | |
| 2486 | + size: 1, | |
| 2487 | + }; | |
| 2488 | + process_compound_ref_mv_candidate(&candidate, COMP_PAIR, &NO_SIGN_BIAS, &mut lists); | |
| 2489 | + assert_eq!(lists.ref_id[0], vec![(5, 5)]); | |
| 2490 | + assert_eq!(lists.ref_diff[1], vec![(5, 5)]); | |
| 2491 | + assert!(lists.ref_id[1].is_empty()); | |
| 2492 | + assert!(lists.ref_diff[0].is_empty()); | |
| 2493 | + } | |
| 2494 | + | |
| 2495 | + #[test] | |
| 2496 | + fn compound_extension_pass_tops_up_a_short_stack() { | |
| 2497 | + // No exact-pair neighbour at all, but a LAST-only neighbour above and | |
| 2498 | + // an ALTREF-only neighbour left: the extension pass should combine | |
| 2499 | + // them into one topped-up candidate rather than leaving the stack | |
| 2500 | + // empty. | |
| 2501 | + let mut grid = MiGrid::new(8, 8); | |
| 2502 | + grid.set( | |
| 2503 | + 1, | |
| 2504 | + 2, | |
| 2505 | + MiInfo { | |
| 2506 | + is_inter: true, | |
| 2507 | + ref_frame: LAST_FRAME, | |
| 2508 | + ref_frame1: None, | |
| 2509 | + mv: (7, 7), | |
| 2510 | + mv1: None, | |
| 2511 | + is_new_mv: false, | |
| 2512 | + size: 8, | |
| 2513 | + }, | |
| 2514 | + ); | |
| 2515 | + grid.set( | |
| 2516 | + 2, | |
| 2517 | + 1, | |
| 2518 | + MiInfo { | |
| 2519 | + is_inter: true, | |
| 2520 | + ref_frame: ALTREF_FRAME, | |
| 2521 | + ref_frame1: None, | |
| 2522 | + mv: (-3, -3), | |
| 2523 | + mv1: None, | |
| 2524 | + is_new_mv: false, | |
| 2525 | + size: 8, | |
| 2526 | + }, | |
| 2527 | + ); | |
| 2528 | + | |
| 2529 | + let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None); | |
| 2530 | + | |
| 2531 | + // The combine step (libaom's real "Handle compound reference frame | |
| 2532 | + // extension") zips each side's ref_id-then-ref_diff lists | |
| 2533 | + // independently, producing *two* distinct combos here (the exact | |
| 2534 | + // match and the borrowed one land on opposite sides for each combo | |
| 2535 | + // index) — both get added, `MAX_MV_REF_CANDIDATES` (2) is exactly | |
| 2536 | + // this stack's cap. | |
| 2537 | + assert_eq!(stack.entries.len(), 2); | |
| 2538 | + assert_eq!(stack.entries[0].weight, 2); | |
| 2539 | + // combo 0: side 0's own exact ref_id match, side 1's borrowed one. | |
| 2540 | + assert_eq!(stack.entries[0].mv0, (7, 7)); | |
| 2541 | + assert_eq!(stack.entries[0].mv1, (-3, -3)); | |
| 2542 | + // combo 1: the mirror -- side 0's borrowed entry, side 1's own exact | |
| 2543 | + // ref_id match. | |
| 2544 | + assert_eq!(stack.entries[1].mv0, (-3, -3)); | |
| 2545 | + assert_eq!(stack.entries[1].mv1, (7, 7)); | |
| 2546 | + } | |
| 1691 | 2547 | } |
crates/ec-av1/src/tile.rsmodified+8 -0
| @@ -2435,6 +2435,7 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 2435 | 2435 | is_inter: true, |
| 2436 | 2436 | ref_frame: LAST_FRAME, |
| 2437 | 2437 | ref_frame1: None, |
| 2438 | + mv1: None, | |
| 2438 | 2439 | mv, |
| 2439 | 2440 | is_new_mv, |
| 2440 | 2441 | size: 8, |
| @@ -2452,6 +2453,7 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 2452 | 2453 | is_inter: true, |
| 2453 | 2454 | ref_frame: LAST_FRAME, |
| 2454 | 2455 | ref_frame1: None, |
| 2456 | + mv1: None, | |
| 2455 | 2457 | mv, |
| 2456 | 2458 | is_new_mv, |
| 2457 | 2459 | size: 8, |
| @@ -2480,6 +2482,7 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 2480 | 2482 | is_inter: false, |
| 2481 | 2483 | ref_frame: -1, |
| 2482 | 2484 | ref_frame1: None, |
| 2485 | + mv1: None, | |
| 2483 | 2486 | mv: (0, 0), |
| 2484 | 2487 | is_new_mv: false, |
| 2485 | 2488 | size: 8, |
| @@ -2619,6 +2622,7 @@ fn write_inter_frame_leaf( | ||
| 2619 | 2622 | is_inter: true, |
| 2620 | 2623 | ref_frame: LAST_FRAME, |
| 2621 | 2624 | ref_frame1: None, |
| 2625 | + mv1: None, | |
| 2622 | 2626 | mv, |
| 2623 | 2627 | is_new_mv, |
| 2624 | 2628 | size: SUB_MI as usize, |
| @@ -2647,6 +2651,7 @@ fn write_inter_frame_leaf( | ||
| 2647 | 2651 | is_inter: false, |
| 2648 | 2652 | ref_frame: -1, |
| 2649 | 2653 | ref_frame1: None, |
| 2654 | + mv1: None, | |
| 2650 | 2655 | mv: (0, 0), |
| 2651 | 2656 | is_new_mv: false, |
| 2652 | 2657 | size: SUB_MI as usize, |
| @@ -2809,6 +2814,7 @@ fn write_inter_frame_leaf8( | ||
| 2809 | 2814 | is_inter: true, |
| 2810 | 2815 | ref_frame: LAST_FRAME, |
| 2811 | 2816 | ref_frame1: None, |
| 2817 | + mv1: None, | |
| 2812 | 2818 | mv, |
| 2813 | 2819 | is_new_mv, |
| 2814 | 2820 | size: 2, |
| @@ -2837,6 +2843,7 @@ fn write_inter_frame_leaf8( | ||
| 2837 | 2843 | is_inter: false, |
| 2838 | 2844 | ref_frame: -1, |
| 2839 | 2845 | ref_frame1: None, |
| 2846 | + mv1: None, | |
| 2840 | 2847 | mv: (0, 0), |
| 2841 | 2848 | is_new_mv: false, |
| 2842 | 2849 | size: 2, |
| @@ -5090,6 +5097,7 @@ mod tests { | ||
| 5090 | 5097 | is_inter: true, |
| 5091 | 5098 | ref_frame: 1, |
| 5092 | 5099 | ref_frame1: None, |
| 5100 | + mv1: None, | |
| 5093 | 5101 | mv, |
| 5094 | 5102 | is_new_mv, |
| 5095 | 5103 | size: 8, |