diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index a639062d..c5394141 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -939,9 +939,12 @@ pub const PARTITION_W16: [[u16; 11]; 4] = [ /// The transform type of a 16x16 intra luma transform, as an index into the /// spec's `Tx_Type_Intra_Inv_Set2`, indexed by the block's luma mode. /// -/// A 16x16 transform is the only size this writer codes whose transform-type -/// set holds more than one type, so it is the only one that codes the symbol: -/// 32x32 and 64x64 are DCT-only by spec 5.11.40's `get_tx_set`. +/// A 16x16 intra transform is the only size this writer's intra path codes +/// whose transform-type set holds more than one type: 32x32 and 64x64 are +/// DCT-only for an *intra* block by spec 5.11.48's `get_tx_set` (its +/// `txSzSqrUp == TX_32X32` case returns `TX_SET_DCTONLY` only in the +/// `!is_inter` branch). An `is_inter` 32x32 transform takes the other branch +/// and reads [`INTER_TX_TYPE_SET3_32`] instead. pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ [1127, 12814, 22772, 27483, 32768, 0], [145, 6761, 11980, 26667, 32768, 0], @@ -958,6 +961,14 @@ pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ [1968, 5556, 12023, 18547, 32768, 0], ]; +/// `Default_Inter_Ext_Tx_Cdf`'s `TX_SET_INTER_3` row for `TX_32X32` (spec +/// 9.4): the transform type of an `is_inter` 32x32 luma transform, whose +/// `get_tx_set` (5.11.48) returns `TX_SET_INTER_3` -- `{IDTX, DCT_DCT}` -- +/// because `txSzSqrUp == TX_32X32` takes the `is_inter` branch there, unlike +/// an intra block's, which is DCT-only at that size. The writer only ever +/// uses DCT_DCT, index one of `Tx_Type_Inter_Inv_Set3`. +pub const INTER_TX_TYPE_SET3_32: [u16; 3] = [748, 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 aa443b01..818e9d91 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -16,8 +16,17 @@ use crate::cdf; /// Which of the four coefficient table sets a transform block is coded with. #[derive(Clone, Copy)] pub(crate) enum TxbSet { - /// The 32x32 luma transform of a 32x32 block. + /// The 32x32 luma transform of an intra 32x32 block. `get_tx_set` is + /// DCT-only at this size for an intra block, so no `tx_type` symbol is + /// coded; an `is_inter` 32x32 block reads a different set and uses + /// [`Luma32Inter`](TxbSet::Luma32Inter) instead. Luma32, + /// The 32x32 luma transform of an `is_inter` 32x32 block: the same + /// coefficient tables [`Luma32`](TxbSet::Luma32) reads, but `get_tx_set` + /// (spec 5.11.48) returns `TX_SET_INTER_3` rather than `TX_SET_DCTONLY` + /// at this size for an inter block, so this variant carries the + /// `tx_type` symbol `Luma32` does not. + Luma32Inter, /// The 64x64 luma transform of a whole superblock, scanned as a 32x32. Luma64, /// The 16x16 luma transform of a 16x16 block. @@ -49,8 +58,11 @@ pub(crate) struct TxbTables<'a> { pub br: &'a mut [[u16; 5]; 21], /// The sign of the DC. pub dc_sign: &'a mut [[u16; 3]; 3], - /// The transform type, for the one size whose set holds more than one. - pub tx_type: Option<&'a mut [u16; 6]>, + /// The transform type, for the one size whose set holds more than one -- + /// a plain slice because the intra 16x16 table's alphabet (five types) + /// and the inter 32x32 table's (two, `TX_SET_INTER_3`) are different + /// sizes. + pub tx_type: Option<&'a mut [u16]>, } /// Every table a key frame's tile writer adapts. @@ -144,6 +156,10 @@ pub(crate) struct Cdfs { pub dc_sign_luma: [[u16; 3]; 3], /// The transform type of a 16x16 intra luma transform, by luma mode. pub intra_tx_type_16: [[u16; 6]; 13], + /// The transform type of an `is_inter` 32x32 luma transform (spec + /// `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 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. @@ -472,6 +488,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, dc_sign_chroma: cdf::DC_SIGN_CHROMA, intra_inter: cdf::INTRA_INTER, single_ref: cdf::SINGLE_REF, @@ -499,6 +516,17 @@ impl Cdfs { dc_sign: &mut self.dc_sign_luma, tx_type: None, }, + TxbSet::Luma32Inter => TxbTables { + side: 32, + txb_skip: &mut self.txb_skip_luma_32, + eob_pt: &mut self.eob_pt_1024_luma, + eob_extra: &mut self.eob_extra_luma_32, + base: &mut self.base_luma_32, + base_eob: &mut self.base_eob_luma_32, + br: &mut self.br_luma_32, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.inter_tx_type_32.as_mut_slice()), + }, TxbSet::Luma64 => TxbTables { side: 32, txb_skip: &mut self.txb_skip_luma_64, @@ -519,7 +547,7 @@ impl Cdfs { base_eob: &mut self.base_eob_luma_16, br: &mut self.br_luma_16, dc_sign: &mut self.dc_sign_luma, - tx_type: Some(&mut self.intra_tx_type_16[mode]), + tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()), }, TxbSet::Chroma8 => TxbTables { side: 8, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 5607f138..b70d3ef2 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1176,8 +1176,11 @@ fn mc_trial( /// intra mode [`Search::modes`] offers (predicted exactly as a key frame's /// block is), `NEARESTMV`, and `NEWMV` searched from `reference` by /// [`motion::search`] and seeded by `stack.pred_mv` — both inter candidates -/// coded `skip: true`; see the corner-cut comment where they build their -/// [`Trial`]s. +/// coding a real residual (`skip: false`) whenever one prices out cheaper +/// than the all-skip candidate, now that `sb_coeff_inter_frame_tile`'s +/// missing inter `tx_type` symbol (the desync this function's candidates +/// used to route around; see `crate::cdf_state::TxbSet::Luma32Inter`) is +/// fixed. /// /// The symbol costs this ranks candidates by use fixed contexts (0) for /// `skip`, `intra_inter` and `single_ref` — the tile writer's actual @@ -1289,19 +1292,9 @@ fn search_inter_block( + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV - // corner-cut: every inter candidate below codes `skip: true` -- no - // residual -- even where a coded residual would cost less. A coded - // residual on an `is_inter` block (`skip: false`) desyncs - // `sb_coeff_inter_frame_tile`'s output past what dav1d will accept - // ("Invalid data found when processing input"), isolated by a minimal - // repro (`EC_AV1_DEBUG_ONLY_SKIP` in this lane's history): an intra-only - // inter frame decodes clean, an all-skip inter frame decodes clean, but - // one `NEARESTMV` block coded with any residual breaks the stream even - // at `mv == (0, 0)`. That is inside `sb_coeff_inter_frame_tile` - // (`crate::tile`), which this lane's charter forbids editing — ceiling: - // an inter frame never codes a residual until that tile-writer defect is - // found and fixed; ping-pong candidate is `NEARESTMV`/`NEWMV` "coded" - // above. + // The NEARESTMV candidate: `skip` is decided below from whether its + // trial actually found a nonzero level, once its residual is priced + // against the same CDFs the tile writer codes it with. { let mv = stack.nearest_mv; let luma_trial = mc_trial( @@ -1314,7 +1307,7 @@ fn search_inter_block( ref_luma.0, ref_luma.1, ref_luma.2, - true, + false, search.base_q_idx, search.deadzone, luma_set, @@ -1329,7 +1322,7 @@ fn search_inter_block( ref_u.0, ref_u.1, ref_u.2, - true, + false, search.base_q_idx, search.deadzone, chroma_set, @@ -1344,23 +1337,34 @@ fn search_inter_block( ref_v.0, ref_v.1, ref_v.2, - true, + false, search.base_q_idx, search.deadzone, chroma_set, ); + 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 cost = luma_trial.sse + u.sse + v.sse + search.lambda - * (skip_bits(true) + intra_inter_bits(true) + single_ref_bits + mode_bits_inter); + * (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 + }); consider(Candidate { cost, luma: luma_trial, u, v, mode: DC_PRED, - skip: true, + skip, inter: Some(InterInfo { mode: InterMode::NearestMv, mv, @@ -1393,7 +1397,7 @@ fn search_inter_block( ref_luma.0, ref_luma.1, ref_luma.2, - true, + false, search.base_q_idx, search.deadzone, luma_set, @@ -1408,7 +1412,7 @@ fn search_inter_block( ref_u.0, ref_u.1, ref_u.2, - true, + false, search.base_q_idx, search.deadzone, chroma_set, @@ -1423,7 +1427,7 @@ fn search_inter_block( ref_v.0, ref_v.1, ref_v.2, - true, + false, search.base_q_idx, search.deadzone, chroma_set, @@ -1433,23 +1437,31 @@ fn search_inter_block( } else { 0.0 }; + 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 cost = luma_trial.sse + u.sse + v.sse + search.lambda - * (skip_bits(true) + * (skip_bits(skip) + intra_inter_bits(true) + single_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + drl_bits - + mv_bits); + + mv_bits + + if skip { + 0.0 + } else { + luma_trial.bits + u.bits + v.bits + }); consider(Candidate { cost, luma: luma_trial, u, v, mode: DC_PRED, - skip: true, + skip, inter: Some(InterInfo { mode: InterMode::NewMv, mv, @@ -1793,6 +1805,74 @@ mod tests { } } + /// The minimal repro that isolated the inter-residual desync: a key frame + /// followed by one inter frame whose single superblock carries one + /// `NEARESTMV` block (`mv == (0, 0)`) with exactly one nonzero luma + /// coefficient, its three sibling blocks all `skip: true`. Before the + /// `is_inter` transform-block fix this broke ffmpeg/dav1d ("Invalid data + /// found when processing input") even though every symbol either block + /// codes is otherwise proven (all-skip inter blocks decode clean, and the + /// same coefficient syntax decodes clean on every intra key-frame gate). + #[test] + fn a_nearestmv_block_with_one_nonzero_coefficient_decodes_clean() { + if !have_ffmpeg() { + eprintln!( + "SKIP a_nearestmv_block_with_one_nonzero_coefficient_decodes_clean: no ffmpeg" + ); + return; + } + let (width, height) = (64usize, 64usize); + let picture = Picture::grey(width, height); + let key = encode_key_frame(&picture, 100, 0.5).unwrap(); + + let (seq, _) = key_frame_headers(width, height, 100).unwrap(); + let (_, inter_header) = inter_frame_headers(width, height, 100, 1, 0).unwrap(); + + let residual_block = BlockCoeffs { + luma: vec![Coeff { + row: 0, + col: 0, + level: 4, + }], + u: Vec::new(), + v: Vec::new(), + mode: 0, + skip: false, + inter: Some(InterInfo { + mode: InterMode::NearestMv, + mv: (0, 0), + }), + }; + let skipped_block = BlockCoeffs { + skip: true, + inter: Some(InterInfo { + mode: InterMode::NearestMv, + mv: (0, 0), + }), + ..BlockCoeffs::default() + }; + let blocks = vec![ + residual_block, + skipped_block.clone(), + skipped_block.clone(), + skipped_block, + ]; + let tile = + sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks) + .unwrap(); + + // `key.stream` is already a temporal delimiter, the sequence header + // and the key frame's own OBU (`encode_key_frame` built it from the + // same `key_frame_headers(width, height, 100)` this test calls), so + // the inter frame's OBU is appended straight onto it rather than + // re-deriving the key frame's tile bytes. + let mut stream = key.stream.clone(); + stream.extend_from_slice(&temporal_delimiter()); + stream.extend_from_slice(&frame_obu(&seq, &inter_header, &tile).unwrap()); + + ffmpeg_decode_sequence(&stream, width, height, 2); + } + /// One frame of a real clip, scaled to a whole number of 32x32 blocks. fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture { let out = Command::new("ffmpeg") diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index ae88875e..66ef5d71 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -1577,7 +1577,12 @@ pub fn sb_coeff_inter_frame_tile( let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); let mut enc = SymbolEncoder::new(); - let split_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16]; + // An `is_inter` block's 32x32 luma transform reads a different + // `tx_type` set than an intra block's (`get_tx_set`, spec 5.11.48; see + // `TxbSet::Luma32Inter`'s doc comment); chroma's transform type is + // derived from luma's, not coded, so it never differs between the two. + let intra_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16]; + let inter_planes = [TxbSet::Luma32Inter, TxbSet::Chroma16, TxbSet::Chroma16]; let scan32 = default_scan(TX32); let scan16 = default_scan(TX16); let zero_grids = [ @@ -1707,7 +1712,11 @@ pub fn sb_coeff_inter_frame_tile( write_block_planes( &mut enc, &mut cdfs, - &split_planes, + if is_inter { + &inter_planes + } else { + &intra_planes + }, &grids, &[&scan32, &scan16, &scan16], &neighbours.around(at, BLOCK),