forge
Explore
Log in

Don't you dare go Hollow

Wire NEARESTMV inter coding for 16x16 straddle leaves, gated off

Browse files at this commit

cac7aab1405af43c442a126418f7a682344a45faAhmet Kaan GümüşParent: f9d5fb9
  • feat
    • wire NEARESTMV inter coding for 16x16 straddle leaves, gated off
Show full message
feat(av1): wire NEARESTMV inter coding for 16x16 straddle leaves, gated off

Adds the full machinery for real inter coding of the 16x16 leaves a
half-straddling 32x32 inter-frame quadrant splits into: a Luma16Inter
TxbSet/CDF table (reduced_tx_set forces the same two-symbol DCT_IDTX set
16x16 already shares the shape of with Luma32Inter), a leaf writer
(write_inter_frame_leaf) that codes is_inter/single_ref/mv-stack/DRL/mv at
this leaf's own 4x4-mi window, and an encode-side NEARESTMV-vs-intra cost
comparison (code_square_inter) with matching per-leaf MvStack/grid updates.

The NEARESTMV selection itself is left gated off (`false &&`, corner-cut
comment at the site) -- enabling it desyncs the decoder ("Invalid data
found") on the existing straddle round-trip test, not yet isolated within
this round's budget. Confirmed via a false/true toggle that the desync is
in the new is_inter leaf path, not in the reduced_tx_set frame-header flag
(that alone round-trips clean). Gate: cargo test -p ec-av1 --release ==
130 passed, 9 ignored, 0 failed (same as baseline).
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 block982 // 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 and983 // 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 compound984 // 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, it166 /// `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+182 -10
@@ -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 ..key468 ..key
460 };469 };
461 Ok((seq, header))470 Ok((seq, header))
@@ -1094,6 +1103,151 @@ 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+ // corner-cut: the NEARESTMV choice below is gated off (`false &&`) --
1221+ // enabling it round-trips ffmpeg/dav1d as "Invalid data found" on the
1222+ // straddle test, a desync somewhere in `write_inter_frame_leaf`'s or this
1223+ // function's new symbol chain, not yet isolated. With the gate off this
1224+ // is dead code (never reached, `intra_block` always wins), kept in place
1225+ // for the next round to debug with an `EC_RNG` trace rather than delete
1226+ // and re-derive. Ceiling: no real inter coding for these leaves until
1227+ // this is lifted. Upgrade path: trace the first diverging symbol between
1228+ // this function's cost model and `write_inter_frame_leaf`'s actual
1229+ // write, most likely a stack/grid mismatch between the encode-time and
1230+ // write-time `find_mv_stack` calls despite both using `bw4=bh4=4`.
1231+ if false && inter_cost < intra_cost {
1232+ luma.commit(x, y, SUB, &luma_trial);
1233+ chroma[0].commit(x / 2, y / 2, SUB / 2, &u);
1234+ chroma[1].commit(x / 2, y / 2, SUB / 2, &v);
1235+ BlockCoeffs {
1236+ luma: coeffs(&luma_trial.levels, SUB),
1237+ u: coeffs(&u.levels, SUB / 2),
1238+ v: coeffs(&v.levels, SUB / 2),
1239+ mode: DC_PRED as u8,
1240+ skip,
1241+ inter: Some(InterInfo {
1242+ mode: InterMode::NearestMv,
1243+ mv,
1244+ }),
1245+ }
1246+ } else {
1247+ intra_block
1248+ }
1249+}
1250+
1097 /// The reconstructed samples one 32x32 square covers in all three planes, so1251 /// The reconstructed samples one 32x32 square covers in all three planes, so
1098 /// that a partition trial can be undone.1252 /// that a partition trial can be undone.
1099 fn snapshot(luma: &Plane, chroma: &[Plane; 2], (x, y): (usize, usize)) -> [Vec<u8>; 3] {1253 fn snapshot(luma: &Plane, chroma: &[Plane; 2], (x, y): (usize, usize)) -> [Vec<u8>; 3] {
@@ -2338,24 +2492,42 @@ pub(crate) fn encode_inter_frame(
2338 ),2492 ),
2339 ));2493 ));
2340 }2494 }
2341- // Coded through the inter frame's intra branch only (see2495+ // Each leaf searches intra against its own `NEARESTMV`
2342- // `crate::tile::sb_coeff_inter_frame_tile`'s doc comment on2496+ // candidate (`code_square_inter`), same real-inter choice
2343- // its `Quadrant::Split` arm): no `Luma16Inter` tx_type2497+ // `search_inter_block` makes for a whole 32x32 block, at
2344- // table exists yet for a genuine inter leaf at this size,2498+ // 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());2499 let mut leaves = Vec::with_capacity(sub_positions.len());
2349 for (sr, sc) in sub_positions {2500 for (sr, sc) in sub_positions {
2350 let (x, y) = (sc * SUB, sr * SUB);2501 let (x, y) = (sc * SUB, sr * SUB);
2351- let (block, _cost) = code_square(2502+ let (mi_row, mi_col) = (sr * 4, sc * 4);
2503+ let stack = find_mv_stack(
2504+ &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows,
2505+ );
2506+ let block = code_square_inter(
2352 &mut luma,2507 &mut luma,
2353 &mut chroma,2508 &mut chroma,
2354 (x, y),2509 (x, y),
2355- SUB,
2356 &search,2510 &search,
2357 &mode_bits_table,2511 &mode_bits_table,
2512+ reference,
2513+ &stack,
2358 );2514 );
2515+ if let Some(info) = block.inter {
2516+ for dr in 0..4 {
2517+ for dc in 0..4 {
2518+ grid.set(
2519+ mi_row + dr,
2520+ mi_col + dc,
2521+ MiInfo {
2522+ is_inter: true,
2523+ ref_frame: LAST_FRAME,
2524+ mv: info.mv,
2525+ is_new_mv: matches!(info.mode, InterMode::NewMv),
2526+ },
2527+ );
2528+ }
2529+ }
2530+ }
2359 leaves.push(block);2531 leaves.push(block);
2360 }2532 }
2361 blocks[r32 * cols + c32] = Quadrant::Split(leaves);2533 blocks[r32 * cols + c32] = Quadrant::Split(leaves);
crates/ec-av1/src/tile.rsmodified+99 -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,
@@ -2175,24 +2178,29 @@ pub fn sb_coeff_inter_frame_tile(
2175 }2178 }
2176 2179
2177 /// Writes one 16x16 leaf a straddling 32x32 quadrant's [`Quadrant::Split`]2180 /// Writes one 16x16 leaf a straddling 32x32 quadrant's [`Quadrant::Split`]
2178-/// splits into, always through the inter frame's intra branch (spec2181+/// 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`): no2182+/// 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 this2183+/// `true`), `NEARESTMV` only -- `NEWMV` at this size is this function's
2181-/// size (see [`TxbSet::Luma32Inter`]'s doc comment), and these leaves only2184+/// 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 a2185+/// `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 own2186+/// 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 as2187+/// through [`TxbSet::Luma16Inter`] rather than [`TxbSet::Luma32Inter`]. Its
2188+/// own `PARTITION_NONE` symbol is already written, same contract as
2185 /// [`write_block`].2189 /// [`write_block`].
2186-fn write_inter_frame_intra_leaf(2190+#[allow(clippy::too_many_arguments)]
2191+fn write_inter_frame_leaf(
2187 enc: &mut SymbolEncoder,2192 enc: &mut SymbolEncoder,
2188 cdfs: &mut Cdfs,2193 cdfs: &mut Cdfs,
2189 neighbours: &mut Neighbours,2194 neighbours: &mut Neighbours,
2195+ grid: &mut MiGrid,
2196+ mi_cols: u32,
2197+ mi_rows: u32,
2190 block: &BlockCoeffs,2198 block: &BlockCoeffs,
2191 at: (usize, usize),2199 at: (usize, usize),
2192 scan16: &Vec<u16>,2200 scan16: &Vec<u16>,
2193 scan8: &Vec<u16>,2201 scan8: &Vec<u16>,
2194 ) -> Result<()> {2202 ) -> Result<()> {
2195- if usize::from(block.mode) >= INTRA_MODES {2203+ if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES {
2196 return Err(Error::unsupported(2204 return Err(Error::unsupported(
2197 "AV1 tile",2205 "AV1 tile",
2198 format!(2206 format!(
@@ -2204,26 +2212,86 @@ fn write_inter_frame_intra_leaf(
2204 /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s2212 /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s
2205 /// `size_group_lookup[BLOCK_16X16]`) for this leaf's own size.2213 /// `size_group_lookup[BLOCK_16X16]`) for this leaf's own size.
2206 const SIZE_GROUP_16: usize = 2;2214 const SIZE_GROUP_16: usize = 2;
2215+ /// `Ref_Frame_List`'s `LAST_FRAME` (spec 3): the only reference this
2216+ /// writer's single-reference chain ever names, same as the 32x32 branch.
2217+ const LAST_FRAME: i8 = 1;
2207 2218
2208 let (r, c) = at;2219 let (r, c) = at;
2209 let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]);2220 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]);2221 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
2211 2222
2212 let (has_above, has_left) = (r > 0, c > 0);2223 let (has_above, has_left) = (r > 0, c > 0);
2213- let ii_ctx = intra_inter_ctx(2224+ let (above_inter, left_inter) = (neighbours.above_inter[c], neighbours.left_inter[r]);
2214- has_above,2225+ let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
2215- has_left,2226+ let is_inter = block.inter.is_some();
2216- neighbours.above_inter[c],2227+ enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]);
2217- neighbours.left_inter[r],2228+
2218- );2229+ let mode_for_tx;
2219- enc.symbol(0, &mut cdfs.intra_inter[ii_ctx]); // is_inter = false2230+ if let Some(info) = block.inter {
2231+ let sr_ctx = single_ref_ctx(above_inter || left_inter);
2232+ enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // p1: forward
2233+ enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); // p3: LAST/LAST2
2234+ enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); // p4: LAST
2235+
2236+ let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
2237+ let stack = find_mv_stack(
2238+ grid,
2239+ mi_row,
2240+ mi_col,
2241+ SUB_MI as usize,
2242+ SUB_MI as usize,
2243+ LAST_FRAME,
2244+ mi_cols as usize,
2245+ mi_rows as usize,
2246+ );
2220 2247
2221- let mode = usize::from(block.mode);2248+ let is_new_mv = matches!(info.mode, InterMode::NewMv);
2222- enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]);2249+ if is_new_mv {
2223- if (V_PRED..=D67_PRED).contains(&mode) {2250+ 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]);2251+ } else {
2252+ enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not new
2253+ enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not zero
2254+ enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV
2255+ }
2256+ if is_new_mv && stack.entries.len() > 1 {
2257+ enc.symbol(0, &mut cdfs.drl_mode[stack.drl_ctx[0]]);
2258+ }
2259+ let mv = if is_new_mv {
2260+ write_mv(
2261+ enc,
2262+ &mut cdfs.mv_comp,
2263+ &mut cdfs.mv_joint,
2264+ info.mv,
2265+ stack.pred_mv,
2266+ )?;
2267+ info.mv
2268+ } else {
2269+ stack.nearest_mv
2270+ };
2271+ for dr in 0..SUB_MI as usize {
2272+ for dc in 0..SUB_MI as usize {
2273+ grid.set(
2274+ mi_row + dr,
2275+ mi_col + dc,
2276+ MiInfo {
2277+ is_inter: true,
2278+ ref_frame: LAST_FRAME,
2279+ mv,
2280+ is_new_mv,
2281+ },
2282+ );
2283+ }
2284+ }
2285+ mode_for_tx = 0;
2286+ } else {
2287+ let mode = usize::from(block.mode);
2288+ enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]);
2289+ if (V_PRED..=D67_PRED).contains(&mode) {
2290+ enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]);
2291+ }
2292+ enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]);
2293+ mode_for_tx = mode;
2225 }2294 }
2226- enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]);
2227 2295
2228 if block.skip {2296 if block.skip {
2229 let zero_grids = [2297 let zero_grids = [
@@ -2231,7 +2299,7 @@ fn write_inter_frame_intra_leaf(
2231 vec![0i32; TX8 * TX8],2299 vec![0i32; TX8 * TX8],
2232 vec![0i32; TX8 * TX8],2300 vec![0i32; TX8 * TX8],
2233 ];2301 ];
2234- neighbours.record(at, SUB, mode, &zero_grids);2302+ neighbours.record(at, SUB, mode_for_tx, &zero_grids);
2235 } else {2303 } else {
2236 let grids = [2304 let grids = [
2237 level_grid(&block.luma, TX16)?,2305 level_grid(&block.luma, TX16)?,
@@ -2241,15 +2309,19 @@ fn write_inter_frame_intra_leaf(
2241 write_block_planes(2309 write_block_planes(
2242 enc,2310 enc,
2243 cdfs,2311 cdfs,
2244- &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8],2312+ if is_inter {
2313+ &[TxbSet::Luma16Inter, TxbSet::Chroma8, TxbSet::Chroma8]
2314+ } else {
2315+ &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8]
2316+ },
2245 &grids,2317 &grids,
2246 &[scan16, scan8, scan8],2318 &[scan16, scan8, scan8],
2247 &neighbours.around(at, SUB),2319 &neighbours.around(at, SUB),
2248- mode,2320+ mode_for_tx,
2249 );2321 );
2250- neighbours.record(at, SUB, mode, &grids);2322+ neighbours.record(at, SUB, mode_for_tx, &grids);
2251 }2323 }
2252- neighbours.record_inter(at, SUB, block.skip, false);2324+ neighbours.record_inter(at, SUB, block.skip, is_inter);
2253 Ok(())2325 Ok(())
2254 }2326 }
2255 2327