Merge branch 'lane-av1-ileaf': real inter coding for 16x16 straddle leaves
Browse files at this commit5 changed files +641 -64
- crates/ec-av1/src/cdf.rs +10 -0
- crates/ec-av1/src/cdf_state.rs +22 -0
- crates/ec-av1/src/encode.rs +214 -19
- crates/ec-av1/src/mvstack.rs +256 -18
- crates/ec-av1/src/tile.rs +139 -27
crates/ec-av1/src/cdf.rsmodified+10 -0
| @@ -969,6 +969,16 @@ pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ | |||
| 969 | /// uses DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`. | 969 | /// uses DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`. |
| 970 | pub const INTER_TX_TYPE_SET3_32: [u16; 3] = [748, 32768, 0]; | 970 | pub const INTER_TX_TYPE_SET3_32: [u16; 3] = [748, 32768, 0]; |
| 971 | 971 | ||
| 972 | +/// `Default_Inter_Ext_Tx_Cdf`'s `TX_SET_INTER_3` row for `TX_16X16` (spec | ||
| 973 | +/// 9.4): the transform type of an `is_inter` 16x16 luma transform coded under | ||
| 974 | +/// `reduced_tx_set` (spec 5.11.48's `use_reduced_set` branch forces | ||
| 975 | +/// `TX_SET_INTER_3` for every inter size below 32x32, the same two-symbol set | ||
| 976 | +/// [`INTER_TX_TYPE_SET3_32`] reads at 32x32) -- without `reduced_tx_set` a | ||
| 977 | +/// 16x16 inter transform reads the twelve-symbol `TX_SET_INTER_2` instead, | ||
| 978 | +/// which this crate does not carry a table for. The writer only ever uses | ||
| 979 | +/// DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`, same as the 32x32 table. | ||
| 980 | +pub const INTER_TX_TYPE_SET3_16: [u16; 3] = [1998, 32768, 0]; | ||
| 981 | + | ||
| 972 | // The inter-frame tables below (spec 9.4) support the syntax an inter block | 982 | // The inter-frame tables below (spec 9.4) support the syntax an inter block |
| 973 | // needs: whether it is coded as intra at all, which reference frame and | 983 | // needs: whether it is coded as intra at all, which reference frame and |
| 974 | // motion vector it takes when it is not. Compound reference and compound | 984 | // motion vector it takes when it is not. Compound reference and compound |
crates/ec-av1/src/cdf_state.rsmodified+22 -0
| @@ -31,6 +31,12 @@ pub(crate) enum TxbSet { | |||
| 31 | Luma64, | 31 | Luma64, |
| 32 | /// The 16x16 luma transform of a 16x16 block. | 32 | /// The 16x16 luma transform of a 16x16 block. |
| 33 | Luma16, | 33 | Luma16, |
| 34 | + /// The 16x16 luma transform of an `is_inter` 16x16 block, coded under | ||
| 35 | + /// `reduced_tx_set` (see [`crate::cdf::INTER_TX_TYPE_SET3_16`]'s doc | ||
| 36 | + /// comment): the same coefficient tables [`Luma16`](TxbSet::Luma16) | ||
| 37 | + /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s | ||
| 38 | + /// five-symbol, mode-indexed one. | ||
| 39 | + Luma16Inter, | ||
| 34 | /// The 8x8 transform of a chroma plane of a 16x16 block. | 40 | /// The 8x8 transform of a chroma plane of a 16x16 block. |
| 35 | Chroma8, | 41 | Chroma8, |
| 36 | /// The 16x16 transform of a chroma plane of a 32x32 block. | 42 | /// The 16x16 transform of a chroma plane of a 32x32 block. |
| @@ -160,6 +166,10 @@ pub(crate) struct Cdfs { | |||
| 160 | /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it | 166 | /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it |
| 161 | /// is not indexed by mode. | 167 | /// is not indexed by mode. |
| 162 | pub inter_tx_type_32: [u16; 3], | 168 | pub inter_tx_type_32: [u16; 3], |
| 169 | + /// The transform type of an `is_inter` 16x16 luma transform under | ||
| 170 | + /// `reduced_tx_set` (spec `TX_SET_INTER_3`, `TX_16X16` row) -- not | ||
| 171 | + /// indexed by mode, same as [`Self::inter_tx_type_32`]. | ||
| 172 | + pub inter_tx_type_16: [u16; 3], | ||
| 163 | /// The DC sign of a chroma coefficient, shared by both chroma sets. | 173 | /// The DC sign of a chroma coefficient, shared by both chroma sets. |
| 164 | pub dc_sign_chroma: [[u16; 3]; 3], | 174 | pub dc_sign_chroma: [[u16; 3]; 3], |
| 165 | /// Whether an inter frame's block is coded as intra. | 175 | /// Whether an inter frame's block is coded as intra. |
| @@ -487,6 +497,7 @@ impl Cdfs { | |||
| 487 | dc_sign_luma: cdf::DC_SIGN_LUMA, | 497 | dc_sign_luma: cdf::DC_SIGN_LUMA, |
| 488 | intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, | 498 | intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, |
| 489 | inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, | 499 | inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, |
| 500 | + inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16, | ||
| 490 | dc_sign_chroma: cdf::DC_SIGN_CHROMA, | 501 | dc_sign_chroma: cdf::DC_SIGN_CHROMA, |
| 491 | intra_inter: cdf::INTRA_INTER, | 502 | intra_inter: cdf::INTRA_INTER, |
| 492 | single_ref: cdf::SINGLE_REF, | 503 | single_ref: cdf::SINGLE_REF, |
| @@ -547,6 +558,17 @@ impl Cdfs { | |||
| 547 | dc_sign: &mut self.dc_sign_luma, | 558 | dc_sign: &mut self.dc_sign_luma, |
| 548 | tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()), | 559 | tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()), |
| 549 | }, | 560 | }, |
| 561 | + TxbSet::Luma16Inter => TxbTables { | ||
| 562 | + side: 16, | ||
| 563 | + txb_skip: &mut self.txb_skip_luma_16, | ||
| 564 | + eob_pt: &mut self.eob_pt_256_luma, | ||
| 565 | + eob_extra: &mut self.eob_extra_luma_16, | ||
| 566 | + base: &mut self.base_luma_16, | ||
| 567 | + base_eob: &mut self.base_eob_luma_16, | ||
| 568 | + br: &mut self.br_luma_16, | ||
| 569 | + dc_sign: &mut self.dc_sign_luma, | ||
| 570 | + tx_type: Some(self.inter_tx_type_16.as_mut_slice()), | ||
| 571 | + }, | ||
| 550 | TxbSet::Chroma8 => TxbTables { | 572 | TxbSet::Chroma8 => TxbTables { |
| 551 | side: 8, | 573 | side: 8, |
| 552 | txb_skip: &mut self.txb_skip_chroma_8, | 574 | txb_skip: &mut self.txb_skip_chroma_8, |
crates/ec-av1/src/encode.rsmodified+214 -19
| @@ -455,7 +455,16 @@ pub fn inter_frame_headers( | |||
| 455 | interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, | 455 | interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, |
| 456 | is_motion_mode_switchable: false, | 456 | is_motion_mode_switchable: false, |
| 457 | use_ref_frame_mvs: false, | 457 | use_ref_frame_mvs: false, |
| 458 | - reduced_tx_set: false, | 458 | + // Forces `get_tx_set` (spec 5.11.48) to the two-symbol |
| 459 | + // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only | ||
| 460 | + // set this crate carries a table for at 16x16 (see | ||
| 461 | + // `crate::cdf::INTER_TX_TYPE_SET3_16`'s doc comment). A 32x32 inter | ||
| 462 | + // transform is unaffected (its `txSzSqrUp == TX_32X32` branch already | ||
| 463 | + // reads `TX_SET_INTER_3` regardless of this flag), and neither is any | ||
| 464 | + // intra transform at any size (`get_tx_set`'s intra branch returns | ||
| 465 | + // the same `TX_SET_INTRA_2` at 16x16 with or without it), so this | ||
| 466 | + // only narrows the 16x16-and-smaller inter case this crate is adding. | ||
| 467 | + reduced_tx_set: true, | ||
| 459 | ..key | 468 | ..key |
| 460 | }; | 469 | }; |
| 461 | Ok((seq, header)) | 470 | Ok((seq, header)) |
| @@ -1094,6 +1103,154 @@ fn code_square( | |||
| 1094 | ) | 1103 | ) |
| 1095 | } | 1104 | } |
| 1096 | 1105 | ||
| 1106 | +/// Codes one 16x16 leaf of a straddling 32x32 quadrant as whichever costs | ||
| 1107 | +/// least of intra ([`code_square`], unchanged) or `NEARESTMV` against | ||
| 1108 | +/// `reference` at `stack.nearest_mv` -- the same NEARESTMV candidate | ||
| 1109 | +/// [`search_inter_block`] prices for a whole 32x32 block, just at this leaf's | ||
| 1110 | +/// own 16x16 side and through [`TxbSet::Luma16Inter`]. `NEWMV` (a motion | ||
| 1111 | +/// search of its own) is not attempted here: these leaves only ever fill the | ||
| 1112 | +/// sliver of real content a true edge lands in the middle of a 32x32 block, | ||
| 1113 | +/// not a leaf worth a full search, so `NEARESTMV`-or-intra is this function's | ||
| 1114 | +/// deliberate first cut (see `write_inter_frame_leaf`'s doc comment). | ||
| 1115 | +#[allow(clippy::too_many_arguments)] | ||
| 1116 | +fn code_square_inter( | ||
| 1117 | + luma: &mut Plane, | ||
| 1118 | + chroma: &mut [Plane; 2], | ||
| 1119 | + (x, y): (usize, usize), | ||
| 1120 | + search: &Search, | ||
| 1121 | + mode_bits: &[f64; 13], | ||
| 1122 | + reference: &Picture, | ||
| 1123 | + stack: &MvStack, | ||
| 1124 | +) -> BlockCoeffs { | ||
| 1125 | + let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), SUB, search, mode_bits); | ||
| 1126 | + | ||
| 1127 | + let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); | ||
| 1128 | + let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); | ||
| 1129 | + let single_ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0) | ||
| 1130 | + + symbol_bits(&cdf::SINGLE_REF[0][2], 0) | ||
| 1131 | + + symbol_bits(&cdf::SINGLE_REF[0][3], 0); | ||
| 1132 | + let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV | ||
| 1133 | + + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero | ||
| 1134 | + + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV | ||
| 1135 | + | ||
| 1136 | + let mv = stack.nearest_mv; | ||
| 1137 | + let ref_luma = ( | ||
| 1138 | + &reference.y, | ||
| 1139 | + reference.width, | ||
| 1140 | + luma.true_width, | ||
| 1141 | + luma.true_height, | ||
| 1142 | + ); | ||
| 1143 | + let ref_u = ( | ||
| 1144 | + &reference.u, | ||
| 1145 | + reference.width / 2, | ||
| 1146 | + chroma[0].true_width, | ||
| 1147 | + chroma[0].true_height, | ||
| 1148 | + ); | ||
| 1149 | + let ref_v = ( | ||
| 1150 | + &reference.v, | ||
| 1151 | + reference.width / 2, | ||
| 1152 | + chroma[1].true_width, | ||
| 1153 | + chroma[1].true_height, | ||
| 1154 | + ); | ||
| 1155 | + let luma_trial = mc_trial( | ||
| 1156 | + luma, | ||
| 1157 | + x, | ||
| 1158 | + y, | ||
| 1159 | + SUB, | ||
| 1160 | + mv, | ||
| 1161 | + true, | ||
| 1162 | + ref_luma.0, | ||
| 1163 | + ref_luma.1, | ||
| 1164 | + ref_luma.2, | ||
| 1165 | + ref_luma.3, | ||
| 1166 | + false, | ||
| 1167 | + search.base_q_idx, | ||
| 1168 | + search.deadzone, | ||
| 1169 | + TxbSet::Luma16Inter, | ||
| 1170 | + ); | ||
| 1171 | + let u = mc_trial( | ||
| 1172 | + &chroma[0], | ||
| 1173 | + x / 2, | ||
| 1174 | + y / 2, | ||
| 1175 | + SUB / 2, | ||
| 1176 | + mv, | ||
| 1177 | + false, | ||
| 1178 | + ref_u.0, | ||
| 1179 | + ref_u.1, | ||
| 1180 | + ref_u.2, | ||
| 1181 | + ref_u.3, | ||
| 1182 | + false, | ||
| 1183 | + search.base_q_idx, | ||
| 1184 | + search.deadzone, | ||
| 1185 | + TxbSet::Chroma8, | ||
| 1186 | + ); | ||
| 1187 | + let v = mc_trial( | ||
| 1188 | + &chroma[1], | ||
| 1189 | + x / 2, | ||
| 1190 | + y / 2, | ||
| 1191 | + SUB / 2, | ||
| 1192 | + mv, | ||
| 1193 | + false, | ||
| 1194 | + ref_v.0, | ||
| 1195 | + ref_v.1, | ||
| 1196 | + ref_v.2, | ||
| 1197 | + ref_v.3, | ||
| 1198 | + false, | ||
| 1199 | + search.base_q_idx, | ||
| 1200 | + search.deadzone, | ||
| 1201 | + TxbSet::Chroma8, | ||
| 1202 | + ); | ||
| 1203 | + let skip = luma_trial.levels.iter().all(|&l| l == 0) | ||
| 1204 | + && u.levels.iter().all(|&l| l == 0) | ||
| 1205 | + && v.levels.iter().all(|&l| l == 0); | ||
| 1206 | + let inter_cost = luma_trial.sse | ||
| 1207 | + + u.sse | ||
| 1208 | + + v.sse | ||
| 1209 | + + search.lambda | ||
| 1210 | + * (skip_bits(skip) | ||
| 1211 | + + intra_inter_bits(true) | ||
| 1212 | + + single_ref_bits | ||
| 1213 | + + mode_bits_inter | ||
| 1214 | + + if skip { | ||
| 1215 | + 0.0 | ||
| 1216 | + } else { | ||
| 1217 | + luma_trial.bits + u.bits + v.bits | ||
| 1218 | + }); | ||
| 1219 | + | ||
| 1220 | + // NEARESTMV for 16x16 leaves, gated on real cost. r1/r2 found the gate's | ||
| 1221 | + // desync in `mvstack.rs`'s `find_mv_stack`: it lacked spec 7.10.2.4's | ||
| 1222 | + // extended row/col scan, invisible at 8-mi-only geometry (the immediate | ||
| 1223 | + // scan's own coverage already reached as far) but a real gap at 4-mi | ||
| 1224 | + // leaf geometry. r3 ported the extended scan and, while proving it a | ||
| 1225 | + // no-op at 8-mi (`extended_row_scan_is_a_no_op_at_8mi_geometry_*`, | ||
| 1226 | + // `mvstack.rs`), found and fixed a second bug the extended scan's | ||
| 1227 | + // coverage bookkeeping (`processed_rows`/`processed_cols`) exposed: an | ||
| 1228 | + // intra neighbour is a coded cell too (libaom's `scan_row_mbmi`/ | ||
| 1229 | + // `scan_col_mbmi` advance coverage from any candidate's `bsize`, | ||
| 1230 | + // regardless of whether it also casts a ref-frame-matching vote) -- | ||
| 1231 | + // `MiGrid` now records intra cells (`tile.rs`/`encode.rs`'s `else` | ||
| 1232 | + // branches), which is what made 8-mi geometry a true no-op instead of | ||
| 1233 | + // one that only held when every neighbour happened to be inter. | ||
| 1234 | + if inter_cost < intra_cost { | ||
| 1235 | + luma.commit(x, y, SUB, &luma_trial); | ||
| 1236 | + chroma[0].commit(x / 2, y / 2, SUB / 2, &u); | ||
| 1237 | + chroma[1].commit(x / 2, y / 2, SUB / 2, &v); | ||
| 1238 | + BlockCoeffs { | ||
| 1239 | + luma: coeffs(&luma_trial.levels, SUB), | ||
| 1240 | + u: coeffs(&u.levels, SUB / 2), | ||
| 1241 | + v: coeffs(&v.levels, SUB / 2), | ||
| 1242 | + mode: DC_PRED as u8, | ||
| 1243 | + skip, | ||
| 1244 | + inter: Some(InterInfo { | ||
| 1245 | + mode: InterMode::NearestMv, | ||
| 1246 | + mv, | ||
| 1247 | + }), | ||
| 1248 | + } | ||
| 1249 | + } else { | ||
| 1250 | + intra_block | ||
| 1251 | + } | ||
| 1252 | +} | ||
| 1253 | + | ||
| 1097 | /// The reconstructed samples one 32x32 square covers in all three planes, so | 1254 | /// The reconstructed samples one 32x32 square covers in all three planes, so |
| 1098 | /// that a partition trial can be undone. | 1255 | /// that a partition trial can be undone. |
| 1099 | fn snapshot(luma: &Plane, chroma: &[Plane; 2], (x, y): (usize, usize)) -> [Vec<u8>; 3] { | 1256 | fn snapshot(luma: &Plane, chroma: &[Plane; 2], (x, y): (usize, usize)) -> [Vec<u8>; 3] { |
| @@ -2287,20 +2444,32 @@ pub(crate) fn encode_inter_frame( | |||
| 2287 | &stack, | 2444 | &stack, |
| 2288 | ); | 2445 | ); |
| 2289 | 2446 | ||
| 2290 | - if let Some(info) = block.inter { | 2447 | + for dr in 0..8 { |
| 2291 | - for dr in 0..8 { | 2448 | + for dc in 0..8 { |
| 2292 | - for dc in 0..8 { | 2449 | + grid.set( |
| 2293 | - grid.set( | 2450 | + mi_row + dr, |
| 2294 | - mi_row + dr, | 2451 | + mi_col + dc, |
| 2295 | - mi_col + dc, | 2452 | + match block.inter { |
| 2296 | - MiInfo { | 2453 | + Some(info) => MiInfo { |
| 2297 | is_inter: true, | 2454 | is_inter: true, |
| 2298 | ref_frame: LAST_FRAME, | 2455 | ref_frame: LAST_FRAME, |
| 2299 | mv: info.mv, | 2456 | mv: info.mv, |
| 2300 | is_new_mv: matches!(info.mode, InterMode::NewMv), | 2457 | is_new_mv: matches!(info.mode, InterMode::NewMv), |
| 2458 | + size: 8, | ||
| 2301 | }, | 2459 | }, |
| 2302 | - ); | 2460 | + // Intra: no vote, but still a coded cell |
| 2303 | - } | 2461 | + // -- mvstack's extended-scan coverage |
| 2462 | + // (`processed_rows`/`processed_cols`) | ||
| 2463 | + // must see it (module doc). | ||
| 2464 | + None => MiInfo { | ||
| 2465 | + is_inter: false, | ||
| 2466 | + ref_frame: -1, | ||
| 2467 | + mv: (0, 0), | ||
| 2468 | + is_new_mv: false, | ||
| 2469 | + size: 8, | ||
| 2470 | + }, | ||
| 2471 | + }, | ||
| 2472 | + ); | ||
| 2304 | } | 2473 | } |
| 2305 | } | 2474 | } |
| 2306 | blocks[r32 * cols + c32] = Quadrant::Whole(block); | 2475 | blocks[r32 * cols + c32] = Quadrant::Whole(block); |
| @@ -2338,24 +2507,50 @@ pub(crate) fn encode_inter_frame( | |||
| 2338 | ), | 2507 | ), |
| 2339 | )); | 2508 | )); |
| 2340 | } | 2509 | } |
| 2341 | - // Coded through the inter frame's intra branch only (see | 2510 | + // Each leaf searches intra against its own `NEARESTMV` |
| 2342 | - // `crate::tile::sb_coeff_inter_frame_tile`'s doc comment on | 2511 | + // candidate (`code_square_inter`), same real-inter choice |
| 2343 | - // its `Quadrant::Split` arm): no `Luma16Inter` tx_type | 2512 | + // `search_inter_block` makes for a whole 32x32 block, at |
| 2344 | - // table exists yet for a genuine inter leaf at this size, | 2513 | + // this leaf's own 4x4-mi mv-stack window. |
| 2345 | - // and these leaves only ever fill the sliver of real | ||
| 2346 | - // content a true edge lands in the middle of a 32x32 | ||
| 2347 | - // block, not a leaf a motion search chose on its own. | ||
| 2348 | let mut leaves = Vec::with_capacity(sub_positions.len()); | 2514 | let mut leaves = Vec::with_capacity(sub_positions.len()); |
| 2349 | for (sr, sc) in sub_positions { | 2515 | for (sr, sc) in sub_positions { |
| 2350 | let (x, y) = (sc * SUB, sr * SUB); | 2516 | let (x, y) = (sc * SUB, sr * SUB); |
| 2351 | - let (block, _cost) = code_square( | 2517 | + let (mi_row, mi_col) = (sr * 4, sc * 4); |
| 2518 | + let stack = find_mv_stack( | ||
| 2519 | + &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, | ||
| 2520 | + ); | ||
| 2521 | + let block = code_square_inter( | ||
| 2352 | &mut luma, | 2522 | &mut luma, |
| 2353 | &mut chroma, | 2523 | &mut chroma, |
| 2354 | (x, y), | 2524 | (x, y), |
| 2355 | - SUB, | ||
| 2356 | &search, | 2525 | &search, |
| 2357 | &mode_bits_table, | 2526 | &mode_bits_table, |
| 2527 | + reference, | ||
| 2528 | + &stack, | ||
| 2358 | ); | 2529 | ); |
| 2530 | + for dr in 0..4 { | ||
| 2531 | + for dc in 0..4 { | ||
| 2532 | + grid.set( | ||
| 2533 | + mi_row + dr, | ||
| 2534 | + mi_col + dc, | ||
| 2535 | + match block.inter { | ||
| 2536 | + Some(info) => MiInfo { | ||
| 2537 | + is_inter: true, | ||
| 2538 | + ref_frame: LAST_FRAME, | ||
| 2539 | + mv: info.mv, | ||
| 2540 | + is_new_mv: matches!(info.mode, InterMode::NewMv), | ||
| 2541 | + size: 4, | ||
| 2542 | + }, | ||
| 2543 | + None => MiInfo { | ||
| 2544 | + is_inter: false, | ||
| 2545 | + ref_frame: -1, | ||
| 2546 | + mv: (0, 0), | ||
| 2547 | + is_new_mv: false, | ||
| 2548 | + size: 4, | ||
| 2549 | + }, | ||
| 2550 | + }, | ||
| 2551 | + ); | ||
| 2552 | + } | ||
| 2553 | + } | ||
| 2359 | leaves.push(block); | 2554 | leaves.push(block); |
| 2360 | } | 2555 | } |
| 2361 | blocks[r32 * cols + c32] = Quadrant::Split(leaves); | 2556 | blocks[r32 * cols + c32] = Quadrant::Split(leaves); |
crates/ec-av1/src/mvstack.rsmodified+256 -18
| @@ -97,6 +97,18 @@ pub struct MiInfo { | |||
| 97 | /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a | 97 | /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a |
| 98 | /// neighbour's own coded-with-NEWMV state, not this block's. | 98 | /// neighbour's own coded-with-NEWMV state, not this block's. |
| 99 | pub is_new_mv: bool, | 99 | pub is_new_mv: bool, |
| 100 | + /// The side, in 4x4 units, of the square block this unit belongs to (8 | ||
| 101 | + /// for a whole 32x32 block, 4 for a 16x16 leaf -- every block this crate | ||
| 102 | + /// ever codes is square, so one dimension is enough). Feeds the extended | ||
| 103 | + /// row/col scan's `processed_rows`/`processed_cols` (spec 7.10.2.2/.3, | ||
| 104 | + /// libaom `scan_row_mbmi`/`scan_col_mbmi`'s `inc` term): a query block no | ||
| 105 | + /// wider than its immediate neighbour has that neighbour's whole span | ||
| 106 | + /// already covered by the immediate (-1) scan, so the extended scan | ||
| 107 | + /// below only has to run past that. Set for *every* coded unit, intra or | ||
| 108 | + /// inter -- libaom's `scan_row_mbmi` advances `processed_rows` from any | ||
| 109 | + /// candidate's `bsize` regardless of whether it also casts a vote (that | ||
| 110 | + /// requires a ref-frame match, checked separately in `add_ref_mv_candidate`). | ||
| 111 | + pub size: usize, | ||
| 100 | } | 112 | } |
| 101 | 113 | ||
| 102 | /// The whole frame's `mi` grid, one [`MiInfo`] per 4x4 unit, in raster order. | 114 | /// The whole frame's `mi` grid, one [`MiInfo`] per 4x4 unit, in raster order. |
| @@ -188,6 +200,10 @@ const MAX_STACK_SIZE: usize = 8; | |||
| 188 | /// this module makes — see the module doc). | 200 | /// this module makes — see the module doc). |
| 189 | const IMMEDIATE_WEIGHT: u32 = 2; | 201 | const IMMEDIATE_WEIGHT: u32 = 2; |
| 190 | 202 | ||
| 203 | +/// Spec `MVREF_ROW_COLS`: the extended row/col scan runs at offsets `-3` and | ||
| 204 | +/// `-5` (`idx = 2..=3`), one past `IMMEDIATE_WEIGHT`'s own `-1`. | ||
| 205 | +const MVREF_ROW_COLS: usize = 3; | ||
| 206 | + | ||
| 191 | /// Adds one candidate MV with `weight` to `candidates`, merging it into an | 207 | /// Adds one candidate MV with `weight` to `candidates`, merging it into an |
| 192 | /// existing entry with the same MV rather than duplicating it (spec | 208 | /// existing entry with the same MV rather than duplicating it (spec |
| 193 | /// `add_ref_mv_candidate`, 7.10.2.5). | 209 | /// `add_ref_mv_candidate`, 7.10.2.5). |
| @@ -200,7 +216,14 @@ fn add_candidate(candidates: &mut Vec<StackEntry>, mv: (i32, i32), weight: u32) | |||
| 200 | } | 216 | } |
| 201 | 217 | ||
| 202 | /// The row-above scan (spec 7.10.2.2): every 4x4 unit directly above the | 218 | /// The row-above scan (spec 7.10.2.2): every 4x4 unit directly above the |
| 203 | -/// block's span. Returns whether any unit voted. | 219 | +/// block's span. Returns whether any unit voted, and `processed_rows` |
| 220 | +/// (libaom `scan_row_mbmi`'s output): how many rows past the immediate one | ||
| 221 | +/// this span already covers by *geometry* alone (0 unless a same-or-wider | ||
| 222 | +/// neighbour occupies it, in which case that neighbour's own `size` is the | ||
| 223 | +/// coverage, spec 7.10.2.2's `inc` term reduced to this crate's square, | ||
| 224 | +/// fixed-weight candidates) -- tracked for *every* coded cell, intra or | ||
| 225 | +/// inter, since libaom's `processed_rows` update sits outside the | ||
| 226 | +/// ref-frame-matching `add_ref_mv_candidate` call. | ||
| 204 | fn scan_row( | 227 | fn scan_row( |
| 205 | grid: &MiGrid, | 228 | grid: &MiGrid, |
| 206 | mi_row: usize, | 229 | mi_row: usize, |
| @@ -209,26 +232,31 @@ fn scan_row( | |||
| 209 | ref_frame: i8, | 232 | ref_frame: i8, |
| 210 | candidates: &mut Vec<StackEntry>, | 233 | candidates: &mut Vec<StackEntry>, |
| 211 | newmv_count: &mut u32, | 234 | newmv_count: &mut u32, |
| 212 | -) -> bool { | 235 | +) -> (bool, usize) { |
| 213 | let Some(row) = mi_row.checked_sub(1) else { | 236 | let Some(row) = mi_row.checked_sub(1) else { |
| 214 | - return false; | 237 | + return (false, 0); |
| 215 | }; | 238 | }; |
| 216 | let mut found = false; | 239 | let mut found = false; |
| 240 | + let mut processed_rows = 0; | ||
| 217 | for col in mi_col..mi_col + bw4 { | 241 | for col in mi_col..mi_col + bw4 { |
| 218 | - if let Some(info) = grid.get(row, col) | 242 | + let Some(info) = grid.get(row, col) else { |
| 219 | - && info.is_inter | 243 | + continue; |
| 220 | - && info.ref_frame == ref_frame | 244 | + }; |
| 221 | - { | 245 | + if bw4 <= info.size { |
| 246 | + processed_rows = processed_rows.max(info.size); | ||
| 247 | + } | ||
| 248 | + if info.is_inter && info.ref_frame == ref_frame { | ||
| 222 | found = true; | 249 | found = true; |
| 223 | add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | 250 | add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); |
| 224 | *newmv_count += u32::from(info.is_new_mv); | 251 | *newmv_count += u32::from(info.is_new_mv); |
| 225 | } | 252 | } |
| 226 | } | 253 | } |
| 227 | - found | 254 | + (found, processed_rows) |
| 228 | } | 255 | } |
| 229 | 256 | ||
| 230 | /// The col-left scan (spec 7.10.2.3): every 4x4 unit directly left of the | 257 | /// The col-left scan (spec 7.10.2.3): every 4x4 unit directly left of the |
| 231 | -/// block's span. Returns whether any unit voted. | 258 | +/// block's span. Returns whether any unit voted, and `processed_cols` |
| 259 | +/// (`scan_row`'s doc, transposed). | ||
| 232 | fn scan_col( | 260 | fn scan_col( |
| 233 | grid: &MiGrid, | 261 | grid: &MiGrid, |
| 234 | mi_row: usize, | 262 | mi_row: usize, |
| @@ -237,21 +265,99 @@ fn scan_col( | |||
| 237 | ref_frame: i8, | 265 | ref_frame: i8, |
| 238 | candidates: &mut Vec<StackEntry>, | 266 | candidates: &mut Vec<StackEntry>, |
| 239 | newmv_count: &mut u32, | 267 | newmv_count: &mut u32, |
| 240 | -) -> bool { | 268 | +) -> (bool, usize) { |
| 241 | let Some(col) = mi_col.checked_sub(1) else { | 269 | let Some(col) = mi_col.checked_sub(1) else { |
| 242 | - return false; | 270 | + return (false, 0); |
| 243 | }; | 271 | }; |
| 244 | let mut found = false; | 272 | let mut found = false; |
| 273 | + let mut processed_cols = 0; | ||
| 245 | for row in mi_row..mi_row + bh4 { | 274 | for row in mi_row..mi_row + bh4 { |
| 246 | - if let Some(info) = grid.get(row, col) | 275 | + let Some(info) = grid.get(row, col) else { |
| 247 | - && info.is_inter | 276 | + continue; |
| 248 | - && info.ref_frame == ref_frame | 277 | + }; |
| 249 | - { | 278 | + if bh4 <= info.size { |
| 279 | + processed_cols = processed_cols.max(info.size); | ||
| 280 | + } | ||
| 281 | + if info.is_inter && info.ref_frame == ref_frame { | ||
| 250 | found = true; | 282 | found = true; |
| 251 | add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | 283 | add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); |
| 252 | *newmv_count += u32::from(info.is_new_mv); | 284 | *newmv_count += u32::from(info.is_new_mv); |
| 253 | } | 285 | } |
| 254 | } | 286 | } |
| 287 | + (found, processed_cols) | ||
| 288 | +} | ||
| 289 | + | ||
| 290 | +/// The extended row scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS` row | ||
| 291 | +/// positions, libaom `mvref_common.c`'s second loop): probes rows `-3` and | ||
| 292 | +/// `-5`, shifted one column right (`col_offset = 1`, libaom's `col_adj` | ||
| 293 | +/// collapses to that for every block size this crate ever queries), each | ||
| 294 | +/// only run when the immediate scan's own `processed_rows` hasn't already | ||
| 295 | +/// covered it and the tile's top edge doesn't clip it first. Its match folds | ||
| 296 | +/// into the row side (`ref_match_count`, never `nearest_match`), same as the | ||
| 297 | +/// corner probe. | ||
| 298 | +fn scan_row_extended( | ||
| 299 | + grid: &MiGrid, | ||
| 300 | + mi_row: usize, | ||
| 301 | + mi_col: usize, | ||
| 302 | + bw4: usize, | ||
| 303 | + ref_frame: i8, | ||
| 304 | + candidates: &mut Vec<StackEntry>, | ||
| 305 | + processed_rows: usize, | ||
| 306 | +) -> bool { | ||
| 307 | + let max_row_offset = mi_row.min(MVREF_ROW_COLS * 2); | ||
| 308 | + let mut found = false; | ||
| 309 | + for idx in 2..=MVREF_ROW_COLS { | ||
| 310 | + let offset = idx * 2 - 1; | ||
| 311 | + if offset > max_row_offset || offset <= processed_rows { | ||
| 312 | + continue; | ||
| 313 | + } | ||
| 314 | + let Some(row) = mi_row.checked_sub(offset) else { | ||
| 315 | + continue; | ||
| 316 | + }; | ||
| 317 | + for col in mi_col + 1..mi_col + 1 + bw4 { | ||
| 318 | + if let Some(info) = grid.get(row, col) | ||
| 319 | + && info.is_inter | ||
| 320 | + && info.ref_frame == ref_frame | ||
| 321 | + { | ||
| 322 | + found = true; | ||
| 323 | + add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | ||
| 324 | + } | ||
| 325 | + } | ||
| 326 | + } | ||
| 327 | + found | ||
| 328 | +} | ||
| 329 | + | ||
| 330 | +/// The extended col scan: `scan_row_extended`, transposed (probes cols `-3` | ||
| 331 | +/// and `-5`, shifted one row down). | ||
| 332 | +fn scan_col_extended( | ||
| 333 | + grid: &MiGrid, | ||
| 334 | + mi_row: usize, | ||
| 335 | + mi_col: usize, | ||
| 336 | + bh4: usize, | ||
| 337 | + ref_frame: i8, | ||
| 338 | + candidates: &mut Vec<StackEntry>, | ||
| 339 | + processed_cols: usize, | ||
| 340 | +) -> bool { | ||
| 341 | + let max_col_offset = mi_col.min(MVREF_ROW_COLS * 2); | ||
| 342 | + let mut found = false; | ||
| 343 | + for idx in 2..=MVREF_ROW_COLS { | ||
| 344 | + let offset = idx * 2 - 1; | ||
| 345 | + if offset > max_col_offset || offset <= processed_cols { | ||
| 346 | + continue; | ||
| 347 | + } | ||
| 348 | + let Some(col) = mi_col.checked_sub(offset) else { | ||
| 349 | + continue; | ||
| 350 | + }; | ||
| 351 | + for row in mi_row + 1..mi_row + 1 + bh4 { | ||
| 352 | + if let Some(info) = grid.get(row, col) | ||
| 353 | + && info.is_inter | ||
| 354 | + && info.ref_frame == ref_frame | ||
| 355 | + { | ||
| 356 | + found = true; | ||
| 357 | + add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | ||
| 358 | + } | ||
| 359 | + } | ||
| 360 | + } | ||
| 255 | found | 361 | found |
| 256 | } | 362 | } |
| 257 | 363 | ||
| @@ -363,7 +469,7 @@ pub fn find_mv_stack( | |||
| 363 | ) -> MvStack { | 469 | ) -> MvStack { |
| 364 | let mut candidates = Vec::new(); | 470 | let mut candidates = Vec::new(); |
| 365 | let mut newmv_count = 0u32; | 471 | let mut newmv_count = 0u32; |
| 366 | - let found_above = scan_row( | 472 | + let (found_above, processed_rows) = scan_row( |
| 367 | grid, | 473 | grid, |
| 368 | mi_row, | 474 | mi_row, |
| 369 | mi_col, | 475 | mi_col, |
| @@ -372,7 +478,7 @@ pub fn find_mv_stack( | |||
| 372 | &mut candidates, | 478 | &mut candidates, |
| 373 | &mut newmv_count, | 479 | &mut newmv_count, |
| 374 | ); | 480 | ); |
| 375 | - let found_left = scan_col( | 481 | + let (found_left, processed_cols) = scan_col( |
| 376 | grid, | 482 | grid, |
| 377 | mi_row, | 483 | mi_row, |
| 378 | mi_col, | 484 | mi_col, |
| @@ -417,7 +523,34 @@ pub fn find_mv_stack( | |||
| 417 | &mut candidates, | 523 | &mut candidates, |
| 418 | &mut dummy_newmv_count, | 524 | &mut dummy_newmv_count, |
| 419 | ); | 525 | ); |
| 420 | - let ref_match_count = usize::from(row_matched || corner_matched) + usize::from(found_left); | 526 | + // The extended row/col scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS`): |
| 527 | + // only reachable once the immediate scan's own reach (`processed_rows`/ | ||
| 528 | + // `processed_cols`) leaves a gap the tile's edge doesn't already clip. | ||
| 529 | + // At this module's fixed 8-mi block geometry with every coded cell | ||
| 530 | + // (intra or inter) now feeding coverage, that reach always hits | ||
| 531 | + // `MVREF_ROW_COLS`'s farthest offset first -- a true no-op there (see | ||
| 532 | + // module doc and `extended_scan_is_a_no_op_at_8mi_geometry` below). Folds | ||
| 533 | + // into the row/col match booleans, never `nearest_match`. | ||
| 534 | + let row_matched_ext = scan_row_extended( | ||
| 535 | + grid, | ||
| 536 | + mi_row, | ||
| 537 | + mi_col, | ||
| 538 | + bw4, | ||
| 539 | + ref_frame, | ||
| 540 | + &mut candidates, | ||
| 541 | + processed_rows, | ||
| 542 | + ); | ||
| 543 | + let col_matched_ext = scan_col_extended( | ||
| 544 | + grid, | ||
| 545 | + mi_row, | ||
| 546 | + mi_col, | ||
| 547 | + bh4, | ||
| 548 | + ref_frame, | ||
| 549 | + &mut candidates, | ||
| 550 | + processed_cols, | ||
| 551 | + ); | ||
| 552 | + let ref_match_count = usize::from(row_matched || corner_matched || row_matched_ext) | ||
| 553 | + + usize::from(found_left || col_matched_ext); | ||
| 421 | 554 | ||
| 422 | // Highest weight first; a stable sort keeps scan order (above, left, | 555 | // Highest weight first; a stable sort keeps scan order (above, left, |
| 423 | // top-right, corner) among ties, matching spec 7.10.2.6. The corner | 556 | // top-right, corner) among ties, matching spec 7.10.2.6. The corner |
| @@ -537,6 +670,10 @@ mod tests { | |||
| 537 | ref_frame: 1, | 670 | ref_frame: 1, |
| 538 | mv, | 671 | mv, |
| 539 | is_new_mv: false, | 672 | is_new_mv: false, |
| 673 | + // 1: smaller than every `bw4`/`bh4` these small-grid tests use, | ||
| 674 | + // so it never trips the extended-scan coverage boost -- these | ||
| 675 | + // tests only exercise the immediate/corner/top-right scans. | ||
| 676 | + size: 1, | ||
| 540 | } | 677 | } |
| 541 | } | 678 | } |
| 542 | 679 | ||
| @@ -682,6 +819,7 @@ mod tests { | |||
| 682 | ref_frame: 1, | 819 | ref_frame: 1, |
| 683 | mv: (4, 4), | 820 | mv: (4, 4), |
| 684 | is_new_mv: true, | 821 | is_new_mv: true, |
| 822 | + size: 1, | ||
| 685 | }, | 823 | }, |
| 686 | ); | 824 | ); |
| 687 | // Only the row matches (nearest_match == 1): newmv_count > 0 picks | 825 | // Only the row matches (nearest_match == 1): newmv_count > 0 picks |
| @@ -742,4 +880,104 @@ mod tests { | |||
| 742 | assert_eq!(single_ref_ctx(false), 1); | 880 | assert_eq!(single_ref_ctx(false), 1); |
| 743 | assert_eq!(single_ref_ctx(true), 2); | 881 | assert_eq!(single_ref_ctx(true), 2); |
| 744 | } | 882 | } |
| 883 | + | ||
| 884 | + /// An 8-mi-sized (`size: 8`) coded-but-intra neighbour: contributes no | ||
| 885 | + /// vote (`is_inter: false`) but must still count for extended-scan | ||
| 886 | + /// coverage, the way libaom's `scan_row_mbmi`/`scan_col_mbmi` advance | ||
| 887 | + /// `processed_rows`/`processed_cols` from any candidate's `bsize` alone. | ||
| 888 | + fn intra8() -> MiInfo { | ||
| 889 | + MiInfo { | ||
| 890 | + is_inter: false, | ||
| 891 | + ref_frame: -1, | ||
| 892 | + mv: (0, 0), | ||
| 893 | + is_new_mv: false, | ||
| 894 | + size: 8, | ||
| 895 | + } | ||
| 896 | + } | ||
| 897 | + | ||
| 898 | + /// An 8-mi-sized inter neighbour with an arbitrary, distinctive `mv` -- | ||
| 899 | + /// used below as a "would this get picked up" tracer at extended-scan | ||
| 900 | + /// offsets that a correct implementation must never reach at 8-mi | ||
| 901 | + /// geometry. | ||
| 902 | + fn inter8(mv: (i32, i32)) -> MiInfo { | ||
| 903 | + MiInfo { | ||
| 904 | + is_inter: true, | ||
| 905 | + ref_frame: 1, | ||
| 906 | + mv, | ||
| 907 | + is_new_mv: false, | ||
| 908 | + size: 8, | ||
| 909 | + } | ||
| 910 | + } | ||
| 911 | + | ||
| 912 | + /// The class of bug this round fixed: at 8-mi geometry, an intra | ||
| 913 | + /// neighbour directly above the query block must suppress the extended | ||
| 914 | + /// row scan exactly as an inter one would (spec 7.10.2.2's | ||
| 915 | + /// `processed_rows` tracks *coded* neighbours, not merely *matching* | ||
| 916 | + /// ones). Two tracer candidates sit at the row's extended-scan offsets | ||
| 917 | + /// (-3 and -5, spec 7.10.2.4) with MVs no other scan could produce; if | ||
| 918 | + /// the coverage bug regresses, they show up in `stack.entries`. | ||
| 919 | + #[test] | ||
| 920 | + fn extended_row_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour() { | ||
| 921 | + let mut grid = MiGrid::new(32, 32); | ||
| 922 | + let (mi_row, mi_col) = (10, 10); | ||
| 923 | + for col in mi_col..mi_col + 8 { | ||
| 924 | + grid.set(mi_row - 1, col, intra8()); | ||
| 925 | + } | ||
| 926 | + for col in mi_col + 1..mi_col + 1 + 8 { | ||
| 927 | + grid.set(mi_row - 3, col, inter8((999, 999))); | ||
| 928 | + grid.set(mi_row - 5, col, inter8((888, 888))); | ||
| 929 | + } | ||
| 930 | + | ||
| 931 | + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); | ||
| 932 | + | ||
| 933 | + assert!( | ||
| 934 | + stack.entries.is_empty(), | ||
| 935 | + "extended row scan fired at 8-mi geometry: {:?}", | ||
| 936 | + stack.entries | ||
| 937 | + ); | ||
| 938 | + } | ||
| 939 | + | ||
| 940 | + /// `extended_row_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour`, | ||
| 941 | + /// transposed to the col-left scan. | ||
| 942 | + #[test] | ||
| 943 | + fn extended_col_scan_is_a_no_op_at_8mi_geometry_even_behind_an_intra_neighbour() { | ||
| 944 | + let mut grid = MiGrid::new(32, 32); | ||
| 945 | + let (mi_row, mi_col) = (10, 10); | ||
| 946 | + for row in mi_row..mi_row + 8 { | ||
| 947 | + grid.set(row, mi_col - 1, intra8()); | ||
| 948 | + } | ||
| 949 | + for row in mi_row + 1..mi_row + 1 + 8 { | ||
| 950 | + grid.set(row, mi_col - 3, inter8((999, 999))); | ||
| 951 | + grid.set(row, mi_col - 5, inter8((888, 888))); | ||
| 952 | + } | ||
| 953 | + | ||
| 954 | + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); | ||
| 955 | + | ||
| 956 | + assert!( | ||
| 957 | + stack.entries.is_empty(), | ||
| 958 | + "extended col scan fired at 8-mi geometry: {:?}", | ||
| 959 | + stack.entries | ||
| 960 | + ); | ||
| 961 | + } | ||
| 962 | + | ||
| 963 | + /// Same shape, but the immediate neighbour is itself inter (a same-size | ||
| 964 | + /// vote) rather than intra -- the geometry-only coverage path must not | ||
| 965 | + /// have broken the ordinary matching one. | ||
| 966 | + #[test] | ||
| 967 | + fn extended_row_scan_is_a_no_op_at_8mi_geometry_behind_an_inter_neighbour() { | ||
| 968 | + let mut grid = MiGrid::new(32, 32); | ||
| 969 | + let (mi_row, mi_col) = (10, 10); | ||
| 970 | + for col in mi_col..mi_col + 8 { | ||
| 971 | + grid.set(mi_row - 1, col, inter8((4, 4))); | ||
| 972 | + } | ||
| 973 | + for col in mi_col + 1..mi_col + 1 + 8 { | ||
| 974 | + grid.set(mi_row - 3, col, inter8((999, 999))); | ||
| 975 | + grid.set(mi_row - 5, col, inter8((888, 888))); | ||
| 976 | + } | ||
| 977 | + | ||
| 978 | + let stack = find_mv_stack(&grid, mi_row, mi_col, 8, 8, 1, 32, 32); | ||
| 979 | + | ||
| 980 | + assert_eq!(stack.entries.len(), 1); | ||
| 981 | + assert_eq!(stack.entries[0].mv, (4, 4)); | ||
| 982 | + } | ||
| 745 | } | 983 | } |
crates/ec-av1/src/tile.rsmodified+139 -27
| @@ -2034,10 +2034,13 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2034 | let at16 = (sr, sc); | 2034 | let at16 = (sr, sc); |
| 2035 | let ctx16 = neighbours.partition_ctx(at16, SUB); | 2035 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| 2036 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]); | 2036 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]); |
| 2037 | - write_inter_frame_intra_leaf( | 2037 | + write_inter_frame_leaf( |
| 2038 | &mut enc, | 2038 | &mut enc, |
| 2039 | &mut cdfs, | 2039 | &mut cdfs, |
| 2040 | &mut neighbours, | 2040 | &mut neighbours, |
| 2041 | + &mut grid, | ||
| 2042 | + mi_cols, | ||
| 2043 | + mi_rows, | ||
| 2041 | leaf, | 2044 | leaf, |
| 2042 | at16, | 2045 | at16, |
| 2043 | &scan16, | 2046 | &scan16, |
| @@ -2114,6 +2117,7 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2114 | ref_frame: LAST_FRAME, | 2117 | ref_frame: LAST_FRAME, |
| 2115 | mv, | 2118 | mv, |
| 2116 | is_new_mv, | 2119 | is_new_mv, |
| 2120 | + size: 8, | ||
| 2117 | }, | 2121 | }, |
| 2118 | ); | 2122 | ); |
| 2119 | for dr in 0..8 { | 2123 | for dr in 0..8 { |
| @@ -2129,6 +2133,7 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2129 | ref_frame: LAST_FRAME, | 2133 | ref_frame: LAST_FRAME, |
| 2130 | mv, | 2134 | mv, |
| 2131 | is_new_mv, | 2135 | is_new_mv, |
| 2136 | + size: 8, | ||
| 2132 | }, | 2137 | }, |
| 2133 | ); | 2138 | ); |
| 2134 | } | 2139 | } |
| @@ -2142,6 +2147,24 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2142 | } | 2147 | } |
| 2143 | enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); | 2148 | enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); |
| 2144 | mode_for_tx = mode; | 2149 | mode_for_tx = mode; |
| 2150 | + // Intra: no vote, but still a coded cell -- mvstack's | ||
| 2151 | + // extended-scan coverage must see it (module doc). | ||
| 2152 | + let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8); | ||
| 2153 | + for dr in 0..8 { | ||
| 2154 | + for dc in 0..8 { | ||
| 2155 | + grid.set( | ||
| 2156 | + mi_row + dr, | ||
| 2157 | + mi_col + dc, | ||
| 2158 | + MiInfo { | ||
| 2159 | + is_inter: false, | ||
| 2160 | + ref_frame: -1, | ||
| 2161 | + mv: (0, 0), | ||
| 2162 | + is_new_mv: false, | ||
| 2163 | + size: 8, | ||
| 2164 | + }, | ||
| 2165 | + ); | ||
| 2166 | + } | ||
| 2167 | + } | ||
| 2145 | } | 2168 | } |
| 2146 | 2169 | ||
| 2147 | if block.skip { | 2170 | if block.skip { |
| @@ -2175,24 +2198,29 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2175 | } | 2198 | } |
| 2176 | 2199 | ||
| 2177 | /// Writes one 16x16 leaf a straddling 32x32 quadrant's [`Quadrant::Split`] | 2200 | /// Writes one 16x16 leaf a straddling 32x32 quadrant's [`Quadrant::Split`] |
| 2178 | -/// splits into, always through the inter frame's intra branch (spec | 2201 | +/// splits into: intra (spec `inter_frame_mode_info`'s intra branch, the only |
| 2179 | -/// `inter_frame_mode_info`'s intra branch, `is_inter` coded `false`): no | 2202 | +/// one this writer used to code here) or real inter (`is_inter` coded |
| 2180 | -/// `Luma16Inter` tx_type table exists yet for a genuine inter leaf at this | 2203 | +/// `true`), `NEARESTMV` only -- `NEWMV` at this size is this function's |
| 2181 | -/// size (see [`TxbSet::Luma32Inter`]'s doc comment), and these leaves only | 2204 | +/// caller's to never build (`Quadrant::Split`'s `BlockCoeffs.inter`) -- same |
| 2182 | -/// ever fill the sliver of real content a true edge lands in the middle of a | 2205 | +/// `single_ref`/mv-stack/DRL symbol chain as the whole-32x32 branch above, |
| 2183 | -/// 32x32 block, never a leaf a motion search chose on its own. Its own | 2206 | +/// just at this leaf's own 4x4-mi window (`bw4`/`bh4` of 4, not 8) and coded |
| 2184 | -/// `PARTITION_NONE` symbol is already written, same contract as | 2207 | +/// through [`TxbSet::Luma16Inter`] rather than [`TxbSet::Luma32Inter`]. Its |
| 2208 | +/// own `PARTITION_NONE` symbol is already written, same contract as | ||
| 2185 | /// [`write_block`]. | 2209 | /// [`write_block`]. |
| 2186 | -fn write_inter_frame_intra_leaf( | 2210 | +#[allow(clippy::too_many_arguments)] |
| 2211 | +fn write_inter_frame_leaf( | ||
| 2187 | enc: &mut SymbolEncoder, | 2212 | enc: &mut SymbolEncoder, |
| 2188 | cdfs: &mut Cdfs, | 2213 | cdfs: &mut Cdfs, |
| 2189 | neighbours: &mut Neighbours, | 2214 | neighbours: &mut Neighbours, |
| 2215 | + grid: &mut MiGrid, | ||
| 2216 | + mi_cols: u32, | ||
| 2217 | + mi_rows: u32, | ||
| 2190 | block: &BlockCoeffs, | 2218 | block: &BlockCoeffs, |
| 2191 | at: (usize, usize), | 2219 | at: (usize, usize), |
| 2192 | scan16: &Vec<u16>, | 2220 | scan16: &Vec<u16>, |
| 2193 | scan8: &Vec<u16>, | 2221 | scan8: &Vec<u16>, |
| 2194 | ) -> Result<()> { | 2222 | ) -> Result<()> { |
| 2195 | - if usize::from(block.mode) >= INTRA_MODES { | 2223 | + if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES { |
| 2196 | return Err(Error::unsupported( | 2224 | return Err(Error::unsupported( |
| 2197 | "AV1 tile", | 2225 | "AV1 tile", |
| 2198 | format!( | 2226 | format!( |
| @@ -2204,26 +2232,105 @@ fn write_inter_frame_intra_leaf( | |||
| 2204 | /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s | 2232 | /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s |
| 2205 | /// `size_group_lookup[BLOCK_16X16]`) for this leaf's own size. | 2233 | /// `size_group_lookup[BLOCK_16X16]`) for this leaf's own size. |
| 2206 | const SIZE_GROUP_16: usize = 2; | 2234 | const SIZE_GROUP_16: usize = 2; |
| 2235 | + /// `Ref_Frame_List`'s `LAST_FRAME` (spec 3): the only reference this | ||
| 2236 | + /// writer's single-reference chain ever names, same as the 32x32 branch. | ||
| 2237 | + const LAST_FRAME: i8 = 1; | ||
| 2207 | 2238 | ||
| 2208 | let (r, c) = at; | 2239 | let (r, c) = at; |
| 2209 | let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); | 2240 | let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); |
| 2210 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); | 2241 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); |
| 2211 | 2242 | ||
| 2212 | let (has_above, has_left) = (r > 0, c > 0); | 2243 | let (has_above, has_left) = (r > 0, c > 0); |
| 2213 | - let ii_ctx = intra_inter_ctx( | 2244 | + let (above_inter, left_inter) = (neighbours.above_inter[c], neighbours.left_inter[r]); |
| 2214 | - has_above, | 2245 | + let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 2215 | - has_left, | 2246 | + let is_inter = block.inter.is_some(); |
| 2216 | - neighbours.above_inter[c], | 2247 | + enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); |
| 2217 | - neighbours.left_inter[r], | 2248 | + |
| 2218 | - ); | 2249 | + let mode_for_tx; |
| 2219 | - enc.symbol(0, &mut cdfs.intra_inter[ii_ctx]); // is_inter = false | 2250 | + if let Some(info) = block.inter { |
| 2251 | + let sr_ctx = single_ref_ctx(above_inter || left_inter); | ||
| 2252 | + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // p1: forward | ||
| 2253 | + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); // p3: LAST/LAST2 | ||
| 2254 | + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); // p4: LAST | ||
| 2255 | + | ||
| 2256 | + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); | ||
| 2257 | + let stack = find_mv_stack( | ||
| 2258 | + grid, | ||
| 2259 | + mi_row, | ||
| 2260 | + mi_col, | ||
| 2261 | + SUB_MI as usize, | ||
| 2262 | + SUB_MI as usize, | ||
| 2263 | + LAST_FRAME, | ||
| 2264 | + mi_cols as usize, | ||
| 2265 | + mi_rows as usize, | ||
| 2266 | + ); | ||
| 2220 | 2267 | ||
| 2221 | - let mode = usize::from(block.mode); | 2268 | + let is_new_mv = matches!(info.mode, InterMode::NewMv); |
| 2222 | - enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); | 2269 | + if is_new_mv { |
| 2223 | - if (V_PRED..=D67_PRED).contains(&mode) { | 2270 | + enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); // NEWMV |
| 2224 | - enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]); | 2271 | + } else { |
| 2272 | + enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not new | ||
| 2273 | + enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not zero | ||
| 2274 | + enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV | ||
| 2275 | + } | ||
| 2276 | + if is_new_mv && stack.entries.len() > 1 { | ||
| 2277 | + enc.symbol(0, &mut cdfs.drl_mode[stack.drl_ctx[0]]); | ||
| 2278 | + } | ||
| 2279 | + let mv = if is_new_mv { | ||
| 2280 | + write_mv( | ||
| 2281 | + enc, | ||
| 2282 | + &mut cdfs.mv_comp, | ||
| 2283 | + &mut cdfs.mv_joint, | ||
| 2284 | + info.mv, | ||
| 2285 | + stack.pred_mv, | ||
| 2286 | + )?; | ||
| 2287 | + info.mv | ||
| 2288 | + } else { | ||
| 2289 | + stack.nearest_mv | ||
| 2290 | + }; | ||
| 2291 | + for dr in 0..SUB_MI as usize { | ||
| 2292 | + for dc in 0..SUB_MI as usize { | ||
| 2293 | + grid.set( | ||
| 2294 | + mi_row + dr, | ||
| 2295 | + mi_col + dc, | ||
| 2296 | + MiInfo { | ||
| 2297 | + is_inter: true, | ||
| 2298 | + ref_frame: LAST_FRAME, | ||
| 2299 | + mv, | ||
| 2300 | + is_new_mv, | ||
| 2301 | + size: SUB_MI as usize, | ||
| 2302 | + }, | ||
| 2303 | + ); | ||
| 2304 | + } | ||
| 2305 | + } | ||
| 2306 | + mode_for_tx = 0; | ||
| 2307 | + } else { | ||
| 2308 | + let mode = usize::from(block.mode); | ||
| 2309 | + enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); | ||
| 2310 | + if (V_PRED..=D67_PRED).contains(&mode) { | ||
| 2311 | + enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]); | ||
| 2312 | + } | ||
| 2313 | + enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); | ||
| 2314 | + mode_for_tx = mode; | ||
| 2315 | + // Intra: no vote, but still a coded cell -- mvstack's extended-scan | ||
| 2316 | + // coverage must see it (module doc). | ||
| 2317 | + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); | ||
| 2318 | + for dr in 0..SUB_MI as usize { | ||
| 2319 | + for dc in 0..SUB_MI as usize { | ||
| 2320 | + grid.set( | ||
| 2321 | + mi_row + dr, | ||
| 2322 | + mi_col + dc, | ||
| 2323 | + MiInfo { | ||
| 2324 | + is_inter: false, | ||
| 2325 | + ref_frame: -1, | ||
| 2326 | + mv: (0, 0), | ||
| 2327 | + is_new_mv: false, | ||
| 2328 | + size: SUB_MI as usize, | ||
| 2329 | + }, | ||
| 2330 | + ); | ||
| 2331 | + } | ||
| 2332 | + } | ||
| 2225 | } | 2333 | } |
| 2226 | - enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); | ||
| 2227 | 2334 | ||
| 2228 | if block.skip { | 2335 | if block.skip { |
| 2229 | let zero_grids = [ | 2336 | let zero_grids = [ |
| @@ -2231,7 +2338,7 @@ fn write_inter_frame_intra_leaf( | |||
| 2231 | vec![0i32; TX8 * TX8], | 2338 | vec![0i32; TX8 * TX8], |
| 2232 | vec![0i32; TX8 * TX8], | 2339 | vec![0i32; TX8 * TX8], |
| 2233 | ]; | 2340 | ]; |
| 2234 | - neighbours.record(at, SUB, mode, &zero_grids); | 2341 | + neighbours.record(at, SUB, mode_for_tx, &zero_grids); |
| 2235 | } else { | 2342 | } else { |
| 2236 | let grids = [ | 2343 | let grids = [ |
| 2237 | level_grid(&block.luma, TX16)?, | 2344 | level_grid(&block.luma, TX16)?, |
| @@ -2241,15 +2348,19 @@ fn write_inter_frame_intra_leaf( | |||
| 2241 | write_block_planes( | 2348 | write_block_planes( |
| 2242 | enc, | 2349 | enc, |
| 2243 | cdfs, | 2350 | cdfs, |
| 2244 | - &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8], | 2351 | + if is_inter { |
| 2352 | + &[TxbSet::Luma16Inter, TxbSet::Chroma8, TxbSet::Chroma8] | ||
| 2353 | + } else { | ||
| 2354 | + &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8] | ||
| 2355 | + }, | ||
| 2245 | &grids, | 2356 | &grids, |
| 2246 | &[scan16, scan8, scan8], | 2357 | &[scan16, scan8, scan8], |
| 2247 | &neighbours.around(at, SUB), | 2358 | &neighbours.around(at, SUB), |
| 2248 | - mode, | 2359 | + mode_for_tx, |
| 2249 | ); | 2360 | ); |
| 2250 | - neighbours.record(at, SUB, mode, &grids); | 2361 | + neighbours.record(at, SUB, mode_for_tx, &grids); |
| 2251 | } | 2362 | } |
| 2252 | - neighbours.record_inter(at, SUB, block.skip, false); | 2363 | + neighbours.record_inter(at, SUB, block.skip, is_inter); |
| 2253 | Ok(()) | 2364 | Ok(()) |
| 2254 | } | 2365 | } |
| 2255 | 2366 | ||
| @@ -4466,6 +4577,7 @@ mod tests { | |||
| 4466 | ref_frame: 1, | 4577 | ref_frame: 1, |
| 4467 | mv, | 4578 | mv, |
| 4468 | is_new_mv, | 4579 | is_new_mv, |
| 4580 | + size: 8, | ||
| 4469 | }, | 4581 | }, |
| 4470 | ); | 4582 | ); |
| 4471 | } | 4583 | } |