diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index c5394141..de755ba4 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -969,6 +969,16 @@ pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ /// uses DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`. pub const INTER_TX_TYPE_SET3_32: [u16; 3] = [748, 32768, 0]; +/// `Default_Inter_Ext_Tx_Cdf`'s `TX_SET_INTER_3` row for `TX_16X16` (spec +/// 9.4): the transform type of an `is_inter` 16x16 luma transform coded under +/// `reduced_tx_set` (spec 5.11.48's `use_reduced_set` branch forces +/// `TX_SET_INTER_3` for every inter size below 32x32, the same two-symbol set +/// [`INTER_TX_TYPE_SET3_32`] reads at 32x32) -- without `reduced_tx_set` a +/// 16x16 inter transform reads the twelve-symbol `TX_SET_INTER_2` instead, +/// which this crate does not carry a table for. The writer only ever uses +/// DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`, same as the 32x32 table. +pub const INTER_TX_TYPE_SET3_16: [u16; 3] = [1998, 32768, 0]; + // The inter-frame tables below (spec 9.4) support the syntax an inter block // needs: whether it is coded as intra at all, which reference frame and // motion vector it takes when it is not. Compound reference and compound diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 38db70dd..0aedb134 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -31,6 +31,12 @@ pub(crate) enum TxbSet { Luma64, /// The 16x16 luma transform of a 16x16 block. Luma16, + /// The 16x16 luma transform of an `is_inter` 16x16 block, coded under + /// `reduced_tx_set` (see [`crate::cdf::INTER_TX_TYPE_SET3_16`]'s doc + /// comment): the same coefficient tables [`Luma16`](TxbSet::Luma16) + /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s + /// five-symbol, mode-indexed one. + Luma16Inter, /// The 8x8 transform of a chroma plane of a 16x16 block. Chroma8, /// The 16x16 transform of a chroma plane of a 32x32 block. @@ -160,6 +166,10 @@ pub(crate) struct Cdfs { /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it /// is not indexed by mode. pub inter_tx_type_32: [u16; 3], + /// The transform type of an `is_inter` 16x16 luma transform under + /// `reduced_tx_set` (spec `TX_SET_INTER_3`, `TX_16X16` row) -- not + /// indexed by mode, same as [`Self::inter_tx_type_32`]. + pub inter_tx_type_16: [u16; 3], /// The DC sign of a chroma coefficient, shared by both chroma sets. pub dc_sign_chroma: [[u16; 3]; 3], /// Whether an inter frame's block is coded as intra. @@ -487,6 +497,7 @@ impl Cdfs { dc_sign_luma: cdf::DC_SIGN_LUMA, intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, + inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16, dc_sign_chroma: cdf::DC_SIGN_CHROMA, intra_inter: cdf::INTRA_INTER, single_ref: cdf::SINGLE_REF, @@ -547,6 +558,17 @@ impl Cdfs { dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()), }, + TxbSet::Luma16Inter => TxbTables { + side: 16, + txb_skip: &mut self.txb_skip_luma_16, + eob_pt: &mut self.eob_pt_256_luma, + eob_extra: &mut self.eob_extra_luma_16, + base: &mut self.base_luma_16, + base_eob: &mut self.base_eob_luma_16, + br: &mut self.br_luma_16, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.inter_tx_type_16.as_mut_slice()), + }, TxbSet::Chroma8 => TxbTables { side: 8, txb_skip: &mut self.txb_skip_chroma_8, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 4aa0bda6..c9f04dd7 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -455,7 +455,16 @@ pub fn inter_frame_headers( interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, is_motion_mode_switchable: false, use_ref_frame_mvs: false, - reduced_tx_set: false, + // Forces `get_tx_set` (spec 5.11.48) to the two-symbol + // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only + // set this crate carries a table for at 16x16 (see + // `crate::cdf::INTER_TX_TYPE_SET3_16`'s doc comment). A 32x32 inter + // transform is unaffected (its `txSzSqrUp == TX_32X32` branch already + // reads `TX_SET_INTER_3` regardless of this flag), and neither is any + // intra transform at any size (`get_tx_set`'s intra branch returns + // the same `TX_SET_INTRA_2` at 16x16 with or without it), so this + // only narrows the 16x16-and-smaller inter case this crate is adding. + reduced_tx_set: true, ..key }; Ok((seq, header)) @@ -1094,6 +1103,151 @@ fn code_square( ) } +/// Codes one 16x16 leaf of a straddling 32x32 quadrant as whichever costs +/// least of intra ([`code_square`], unchanged) or `NEARESTMV` against +/// `reference` at `stack.nearest_mv` -- the same NEARESTMV candidate +/// [`search_inter_block`] prices for a whole 32x32 block, just at this leaf's +/// own 16x16 side and through [`TxbSet::Luma16Inter`]. `NEWMV` (a motion +/// search of its own) is not attempted here: these leaves only ever fill the +/// sliver of real content a true edge lands in the middle of a 32x32 block, +/// not a leaf worth a full search, so `NEARESTMV`-or-intra is this function's +/// deliberate first cut (see `write_inter_frame_leaf`'s doc comment). +#[allow(clippy::too_many_arguments)] +fn code_square_inter( + luma: &mut Plane, + chroma: &mut [Plane; 2], + (x, y): (usize, usize), + search: &Search, + mode_bits: &[f64; 13], + reference: &Picture, + stack: &MvStack, +) -> BlockCoeffs { + let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), SUB, search, mode_bits); + + let skip_bits = |skip: bool| symbol_bits(&cdf::SKIP[0], usize::from(skip)); + let intra_inter_bits = |inter: bool| symbol_bits(&cdf::INTRA_INTER[0], usize::from(inter)); + let single_ref_bits = symbol_bits(&cdf::SINGLE_REF[0][0], 0) + + symbol_bits(&cdf::SINGLE_REF[0][2], 0) + + symbol_bits(&cdf::SINGLE_REF[0][3], 0); + let mode_bits_inter = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1) // not NEWMV + + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero + + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV + + let mv = stack.nearest_mv; + let ref_luma = ( + &reference.y, + reference.width, + luma.true_width, + luma.true_height, + ); + let ref_u = ( + &reference.u, + reference.width / 2, + chroma[0].true_width, + chroma[0].true_height, + ); + let ref_v = ( + &reference.v, + reference.width / 2, + chroma[1].true_width, + chroma[1].true_height, + ); + let luma_trial = mc_trial( + luma, + x, + y, + SUB, + mv, + true, + ref_luma.0, + ref_luma.1, + ref_luma.2, + ref_luma.3, + false, + search.base_q_idx, + search.deadzone, + TxbSet::Luma16Inter, + ); + let u = mc_trial( + &chroma[0], + x / 2, + y / 2, + SUB / 2, + mv, + false, + ref_u.0, + ref_u.1, + ref_u.2, + ref_u.3, + false, + search.base_q_idx, + search.deadzone, + TxbSet::Chroma8, + ); + let v = mc_trial( + &chroma[1], + x / 2, + y / 2, + SUB / 2, + mv, + false, + ref_v.0, + ref_v.1, + ref_v.2, + ref_v.3, + false, + search.base_q_idx, + search.deadzone, + TxbSet::Chroma8, + ); + let skip = luma_trial.levels.iter().all(|&l| l == 0) + && u.levels.iter().all(|&l| l == 0) + && v.levels.iter().all(|&l| l == 0); + let inter_cost = luma_trial.sse + + u.sse + + v.sse + + search.lambda + * (skip_bits(skip) + + intra_inter_bits(true) + + single_ref_bits + + mode_bits_inter + + if skip { + 0.0 + } else { + luma_trial.bits + u.bits + v.bits + }); + + // corner-cut: the NEARESTMV choice below is gated off (`false &&`) -- + // enabling it round-trips ffmpeg/dav1d as "Invalid data found" on the + // straddle test, a desync somewhere in `write_inter_frame_leaf`'s or this + // function's new symbol chain, not yet isolated. With the gate off this + // is dead code (never reached, `intra_block` always wins), kept in place + // for the next round to debug with an `EC_RNG` trace rather than delete + // and re-derive. Ceiling: no real inter coding for these leaves until + // this is lifted. Upgrade path: trace the first diverging symbol between + // this function's cost model and `write_inter_frame_leaf`'s actual + // write, most likely a stack/grid mismatch between the encode-time and + // write-time `find_mv_stack` calls despite both using `bw4=bh4=4`. + if false && inter_cost < intra_cost { + luma.commit(x, y, SUB, &luma_trial); + chroma[0].commit(x / 2, y / 2, SUB / 2, &u); + chroma[1].commit(x / 2, y / 2, SUB / 2, &v); + BlockCoeffs { + luma: coeffs(&luma_trial.levels, SUB), + u: coeffs(&u.levels, SUB / 2), + v: coeffs(&v.levels, SUB / 2), + mode: DC_PRED as u8, + skip, + inter: Some(InterInfo { + mode: InterMode::NearestMv, + mv, + }), + } + } else { + intra_block + } +} + /// The reconstructed samples one 32x32 square covers in all three planes, so /// that a partition trial can be undone. fn snapshot(luma: &Plane, chroma: &[Plane; 2], (x, y): (usize, usize)) -> [Vec; 3] { @@ -2338,24 +2492,42 @@ pub(crate) fn encode_inter_frame( ), )); } - // Coded through the inter frame's intra branch only (see - // `crate::tile::sb_coeff_inter_frame_tile`'s doc comment on - // its `Quadrant::Split` arm): no `Luma16Inter` tx_type - // table exists yet for a genuine inter leaf at this size, - // and these leaves only ever fill the sliver of real - // content a true edge lands in the middle of a 32x32 - // block, not a leaf a motion search chose on its own. + // Each leaf searches intra against its own `NEARESTMV` + // candidate (`code_square_inter`), same real-inter choice + // `search_inter_block` makes for a whole 32x32 block, at + // this leaf's own 4x4-mi mv-stack window. let mut leaves = Vec::with_capacity(sub_positions.len()); for (sr, sc) in sub_positions { let (x, y) = (sc * SUB, sr * SUB); - let (block, _cost) = code_square( + let (mi_row, mi_col) = (sr * 4, sc * 4); + let stack = find_mv_stack( + &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, + ); + let block = code_square_inter( &mut luma, &mut chroma, (x, y), - SUB, &search, &mode_bits_table, + reference, + &stack, ); + if let Some(info) = block.inter { + for dr in 0..4 { + for dc in 0..4 { + grid.set( + mi_row + dr, + mi_col + dc, + MiInfo { + is_inter: true, + ref_frame: LAST_FRAME, + mv: info.mv, + is_new_mv: matches!(info.mode, InterMode::NewMv), + }, + ); + } + } + } leaves.push(block); } blocks[r32 * cols + c32] = Quadrant::Split(leaves); diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 06bf0e8c..e386bc6b 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -2034,10 +2034,13 @@ pub fn sb_coeff_inter_frame_tile( let at16 = (sr, sc); let ctx16 = neighbours.partition_ctx(at16, SUB); enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]); - write_inter_frame_intra_leaf( + write_inter_frame_leaf( &mut enc, &mut cdfs, &mut neighbours, + &mut grid, + mi_cols, + mi_rows, leaf, at16, &scan16, @@ -2175,24 +2178,29 @@ pub fn sb_coeff_inter_frame_tile( } /// Writes one 16x16 leaf a straddling 32x32 quadrant's [`Quadrant::Split`] -/// splits into, always through the inter frame's intra branch (spec -/// `inter_frame_mode_info`'s intra branch, `is_inter` coded `false`): no -/// `Luma16Inter` tx_type table exists yet for a genuine inter leaf at this -/// size (see [`TxbSet::Luma32Inter`]'s doc comment), and these leaves only -/// ever fill the sliver of real content a true edge lands in the middle of a -/// 32x32 block, never a leaf a motion search chose on its own. Its own -/// `PARTITION_NONE` symbol is already written, same contract as +/// splits into: intra (spec `inter_frame_mode_info`'s intra branch, the only +/// one this writer used to code here) or real inter (`is_inter` coded +/// `true`), `NEARESTMV` only -- `NEWMV` at this size is this function's +/// caller's to never build (`Quadrant::Split`'s `BlockCoeffs.inter`) -- same +/// `single_ref`/mv-stack/DRL symbol chain as the whole-32x32 branch above, +/// just at this leaf's own 4x4-mi window (`bw4`/`bh4` of 4, not 8) and coded +/// through [`TxbSet::Luma16Inter`] rather than [`TxbSet::Luma32Inter`]. Its +/// own `PARTITION_NONE` symbol is already written, same contract as /// [`write_block`]. -fn write_inter_frame_intra_leaf( +#[allow(clippy::too_many_arguments)] +fn write_inter_frame_leaf( enc: &mut SymbolEncoder, cdfs: &mut Cdfs, neighbours: &mut Neighbours, + grid: &mut MiGrid, + mi_cols: u32, + mi_rows: u32, block: &BlockCoeffs, at: (usize, usize), scan16: &Vec, scan8: &Vec, ) -> Result<()> { - if usize::from(block.mode) >= INTRA_MODES { + if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES { return Err(Error::unsupported( "AV1 tile", format!( @@ -2204,26 +2212,86 @@ fn write_inter_frame_intra_leaf( /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s /// `size_group_lookup[BLOCK_16X16]`) for this leaf's own size. const SIZE_GROUP_16: usize = 2; + /// `Ref_Frame_List`'s `LAST_FRAME` (spec 3): the only reference this + /// writer's single-reference chain ever names, same as the 32x32 branch. + const LAST_FRAME: i8 = 1; let (r, c) = at; let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); let (has_above, has_left) = (r > 0, c > 0); - let ii_ctx = intra_inter_ctx( - has_above, - has_left, - neighbours.above_inter[c], - neighbours.left_inter[r], - ); - enc.symbol(0, &mut cdfs.intra_inter[ii_ctx]); // is_inter = false + let (above_inter, left_inter) = (neighbours.above_inter[c], neighbours.left_inter[r]); + let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); + let is_inter = block.inter.is_some(); + enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); + + let mode_for_tx; + if let Some(info) = block.inter { + let sr_ctx = single_ref_ctx(above_inter || left_inter); + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // p1: forward + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); // p3: LAST/LAST2 + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); // p4: LAST + + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); + let stack = find_mv_stack( + grid, + mi_row, + mi_col, + SUB_MI as usize, + SUB_MI as usize, + LAST_FRAME, + mi_cols as usize, + mi_rows as usize, + ); - let mode = usize::from(block.mode); - enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); - if (V_PRED..=D67_PRED).contains(&mode) { - enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]); + let is_new_mv = matches!(info.mode, InterMode::NewMv); + if is_new_mv { + enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); // NEWMV + } else { + enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not new + enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not zero + enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARESTMV + } + if is_new_mv && stack.entries.len() > 1 { + enc.symbol(0, &mut cdfs.drl_mode[stack.drl_ctx[0]]); + } + let mv = if is_new_mv { + write_mv( + enc, + &mut cdfs.mv_comp, + &mut cdfs.mv_joint, + info.mv, + stack.pred_mv, + )?; + info.mv + } else { + stack.nearest_mv + }; + for dr in 0..SUB_MI as usize { + for dc in 0..SUB_MI as usize { + grid.set( + mi_row + dr, + mi_col + dc, + MiInfo { + is_inter: true, + ref_frame: LAST_FRAME, + mv, + is_new_mv, + }, + ); + } + } + mode_for_tx = 0; + } else { + let mode = usize::from(block.mode); + enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]); + if (V_PRED..=D67_PRED).contains(&mode) { + enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]); + } + enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); + mode_for_tx = mode; } - enc.symbol(DC_PRED, &mut cdfs.uv_mode_cfl[mode]); if block.skip { let zero_grids = [ @@ -2231,7 +2299,7 @@ fn write_inter_frame_intra_leaf( vec![0i32; TX8 * TX8], vec![0i32; TX8 * TX8], ]; - neighbours.record(at, SUB, mode, &zero_grids); + neighbours.record(at, SUB, mode_for_tx, &zero_grids); } else { let grids = [ level_grid(&block.luma, TX16)?, @@ -2241,15 +2309,19 @@ fn write_inter_frame_intra_leaf( write_block_planes( enc, cdfs, - &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8], + if is_inter { + &[TxbSet::Luma16Inter, TxbSet::Chroma8, TxbSet::Chroma8] + } else { + &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8] + }, &grids, &[scan16, scan8, scan8], &neighbours.around(at, SUB), - mode, + mode_for_tx, ); - neighbours.record(at, SUB, mode, &grids); + neighbours.record(at, SUB, mode_for_tx, &grids); } - neighbours.record_inter(at, SUB, block.skip, false); + neighbours.record_inter(at, SUB, block.skip, is_inter); Ok(()) }