forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1-desync': tx_type for inter residuals, real RD skip

Browse files at this commit

f58832759b19643647e6a707ecf8b8114488c71fAhmet Kaan GümüşParent: 4a44e40ac63d1d
crates/ec-av1/src/cdf.rsmodified+14 -3
@@ -939,9 +939,12 @@ pub const PARTITION_W16: [[u16; 11]; 4] = [
939939 /// The transform type of a 16x16 intra luma transform, as an index into the
940940 /// spec's `Tx_Type_Intra_Inv_Set2`, indexed by the block's luma mode.
941941 ///
942-/// A 16x16 transform is the only size this writer codes whose transform-type
943-/// set holds more than one type, so it is the only one that codes the symbol:
944-/// 32x32 and 64x64 are DCT-only by spec 5.11.40's `get_tx_set`.
942+/// A 16x16 intra transform is the only size this writer's intra path codes
943+/// whose transform-type set holds more than one type: 32x32 and 64x64 are
944+/// DCT-only for an *intra* block by spec 5.11.48's `get_tx_set` (its
945+/// `txSzSqrUp == TX_32X32` case returns `TX_SET_DCTONLY` only in the
946+/// `!is_inter` branch). An `is_inter` 32x32 transform takes the other branch
947+/// and reads [`INTER_TX_TYPE_SET3_32`] instead.
945948 pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [
946949 [1127, 12814, 22772, 27483, 32768, 0],
947950 [145, 6761, 11980, 26667, 32768, 0],
@@ -958,6 +961,14 @@ pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [
958961 [1968, 5556, 12023, 18547, 32768, 0],
959962 ];
960963
964+/// `Default_Inter_Ext_Tx_Cdf`'s `TX_SET_INTER_3` row for `TX_32X32` (spec
965+/// 9.4): the transform type of an `is_inter` 32x32 luma transform, whose
966+/// `get_tx_set` (5.11.48) returns `TX_SET_INTER_3` -- `{IDTX, DCT_DCT}` --
967+/// because `txSzSqrUp == TX_32X32` takes the `is_inter` branch there, unlike
968+/// an intra block's, which is DCT-only at that size. The writer only ever
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];
971+
961972 // The inter-frame tables below (spec 9.4) support the syntax an inter block
962973 // needs: whether it is coded as intra at all, which reference frame and
963974 // motion vector it takes when it is not. Compound reference and compound
crates/ec-av1/src/cdf_state.rsmodified+32 -4
@@ -16,8 +16,17 @@ use crate::cdf;
1616 /// Which of the four coefficient table sets a transform block is coded with.
1717 #[derive(Clone, Copy)]
1818 pub(crate) enum TxbSet {
19- /// The 32x32 luma transform of a 32x32 block.
19+ /// The 32x32 luma transform of an intra 32x32 block. `get_tx_set` is
20+ /// DCT-only at this size for an intra block, so no `tx_type` symbol is
21+ /// coded; an `is_inter` 32x32 block reads a different set and uses
22+ /// [`Luma32Inter`](TxbSet::Luma32Inter) instead.
2023 Luma32,
24+ /// The 32x32 luma transform of an `is_inter` 32x32 block: the same
25+ /// coefficient tables [`Luma32`](TxbSet::Luma32) reads, but `get_tx_set`
26+ /// (spec 5.11.48) returns `TX_SET_INTER_3` rather than `TX_SET_DCTONLY`
27+ /// at this size for an inter block, so this variant carries the
28+ /// `tx_type` symbol `Luma32` does not.
29+ Luma32Inter,
2130 /// The 64x64 luma transform of a whole superblock, scanned as a 32x32.
2231 Luma64,
2332 /// The 16x16 luma transform of a 16x16 block.
@@ -49,8 +58,11 @@ pub(crate) struct TxbTables<'a> {
4958 pub br: &'a mut [[u16; 5]; 21],
5059 /// The sign of the DC.
5160 pub dc_sign: &'a mut [[u16; 3]; 3],
52- /// The transform type, for the one size whose set holds more than one.
53- pub tx_type: Option<&'a mut [u16; 6]>,
61+ /// The transform type, for the one size whose set holds more than one --
62+ /// a plain slice because the intra 16x16 table's alphabet (five types)
63+ /// and the inter 32x32 table's (two, `TX_SET_INTER_3`) are different
64+ /// sizes.
65+ pub tx_type: Option<&'a mut [u16]>,
5466 }
5567
5668 /// Every table a key frame's tile writer adapts.
@@ -144,6 +156,10 @@ pub(crate) struct Cdfs {
144156 pub dc_sign_luma: [[u16; 3]; 3],
145157 /// The transform type of a 16x16 intra luma transform, by luma mode.
146158 pub intra_tx_type_16: [[u16; 6]; 13],
159+ /// The transform type of an `is_inter` 32x32 luma transform (spec
160+ /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it
161+ /// is not indexed by mode.
162+ pub inter_tx_type_32: [u16; 3],
147163 /// The DC sign of a chroma coefficient, shared by both chroma sets.
148164 pub dc_sign_chroma: [[u16; 3]; 3],
149165 /// Whether an inter frame's block is coded as intra.
@@ -472,6 +488,7 @@ impl Cdfs {
472488 ),
473489 dc_sign_luma: cdf::DC_SIGN_LUMA,
474490 intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16,
491+ inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32,
475492 dc_sign_chroma: cdf::DC_SIGN_CHROMA,
476493 intra_inter: cdf::INTRA_INTER,
477494 single_ref: cdf::SINGLE_REF,
@@ -499,6 +516,17 @@ impl Cdfs {
499516 dc_sign: &mut self.dc_sign_luma,
500517 tx_type: None,
501518 },
519+ TxbSet::Luma32Inter => TxbTables {
520+ side: 32,
521+ txb_skip: &mut self.txb_skip_luma_32,
522+ eob_pt: &mut self.eob_pt_1024_luma,
523+ eob_extra: &mut self.eob_extra_luma_32,
524+ base: &mut self.base_luma_32,
525+ base_eob: &mut self.base_eob_luma_32,
526+ br: &mut self.br_luma_32,
527+ dc_sign: &mut self.dc_sign_luma,
528+ tx_type: Some(self.inter_tx_type_32.as_mut_slice()),
529+ },
502530 TxbSet::Luma64 => TxbTables {
503531 side: 32,
504532 txb_skip: &mut self.txb_skip_luma_64,
@@ -519,7 +547,7 @@ impl Cdfs {
519547 base_eob: &mut self.base_eob_luma_16,
520548 br: &mut self.br_luma_16,
521549 dc_sign: &mut self.dc_sign_luma,
522- tx_type: Some(&mut self.intra_tx_type_16[mode]),
550+ tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()),
523551 },
524552 TxbSet::Chroma8 => TxbTables {
525553 side: 8,
crates/ec-av1/src/encode.rsmodified+106 -26
@@ -1176,8 +1176,11 @@ fn mc_trial(
11761176 /// intra mode [`Search::modes`] offers (predicted exactly as a key frame's
11771177 /// block is), `NEARESTMV`, and `NEWMV` searched from `reference` by
11781178 /// [`motion::search`] and seeded by `stack.pred_mv` — both inter candidates
1179-/// coded `skip: true`; see the corner-cut comment where they build their
1180-/// [`Trial`]s.
1179+/// coding a real residual (`skip: false`) whenever one prices out cheaper
1180+/// than the all-skip candidate, now that `sb_coeff_inter_frame_tile`'s
1181+/// missing inter `tx_type` symbol (the desync this function's candidates
1182+/// used to route around; see `crate::cdf_state::TxbSet::Luma32Inter`) is
1183+/// fixed.
11811184 ///
11821185 /// The symbol costs this ranks candidates by use fixed contexts (0) for
11831186 /// `skip`, `intra_inter` and `single_ref` — the tile writer's actual
@@ -1289,19 +1292,9 @@ fn search_inter_block(
12891292 + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero
12901293 + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV
12911294
1292- // corner-cut: every inter candidate below codes `skip: true` -- no
1293- // residual -- even where a coded residual would cost less. A coded
1294- // residual on an `is_inter` block (`skip: false`) desyncs
1295- // `sb_coeff_inter_frame_tile`'s output past what dav1d will accept
1296- // ("Invalid data found when processing input"), isolated by a minimal
1297- // repro (`EC_AV1_DEBUG_ONLY_SKIP` in this lane's history): an intra-only
1298- // inter frame decodes clean, an all-skip inter frame decodes clean, but
1299- // one `NEARESTMV` block coded with any residual breaks the stream even
1300- // at `mv == (0, 0)`. That is inside `sb_coeff_inter_frame_tile`
1301- // (`crate::tile`), which this lane's charter forbids editing — ceiling:
1302- // an inter frame never codes a residual until that tile-writer defect is
1303- // found and fixed; ping-pong candidate is `NEARESTMV`/`NEWMV` "coded"
1304- // above.
1295+ // The NEARESTMV candidate: `skip` is decided below from whether its
1296+ // trial actually found a nonzero level, once its residual is priced
1297+ // against the same CDFs the tile writer codes it with.
13051298 {
13061299 let mv = stack.nearest_mv;
13071300 let luma_trial = mc_trial(
@@ -1314,7 +1307,7 @@ fn search_inter_block(
13141307 ref_luma.0,
13151308 ref_luma.1,
13161309 ref_luma.2,
1317- true,
1310+ false,
13181311 search.base_q_idx,
13191312 search.deadzone,
13201313 luma_set,
@@ -1329,7 +1322,7 @@ fn search_inter_block(
13291322 ref_u.0,
13301323 ref_u.1,
13311324 ref_u.2,
1332- true,
1325+ false,
13331326 search.base_q_idx,
13341327 search.deadzone,
13351328 chroma_set,
@@ -1344,23 +1337,34 @@ fn search_inter_block(
13441337 ref_v.0,
13451338 ref_v.1,
13461339 ref_v.2,
1347- true,
1340+ false,
13481341 search.base_q_idx,
13491342 search.deadzone,
13501343 chroma_set,
13511344 );
1345+ let skip = luma_trial.levels.iter().all(|&l| l == 0)
1346+ && u.levels.iter().all(|&l| l == 0)
1347+ && v.levels.iter().all(|&l| l == 0);
13521348 let cost = luma_trial.sse
13531349 + u.sse
13541350 + v.sse
13551351 + search.lambda
1356- * (skip_bits(true) + intra_inter_bits(true) + single_ref_bits + mode_bits_inter);
1352+ * (skip_bits(skip)
1353+ + intra_inter_bits(true)
1354+ + single_ref_bits
1355+ + mode_bits_inter
1356+ + if skip {
1357+ 0.0
1358+ } else {
1359+ luma_trial.bits + u.bits + v.bits
1360+ });
13571361 consider(Candidate {
13581362 cost,
13591363 luma: luma_trial,
13601364 u,
13611365 v,
13621366 mode: DC_PRED,
1363- skip: true,
1367+ skip,
13641368 inter: Some(InterInfo {
13651369 mode: InterMode::NearestMv,
13661370 mv,
@@ -1393,7 +1397,7 @@ fn search_inter_block(
13931397 ref_luma.0,
13941398 ref_luma.1,
13951399 ref_luma.2,
1396- true,
1400+ false,
13971401 search.base_q_idx,
13981402 search.deadzone,
13991403 luma_set,
@@ -1408,7 +1412,7 @@ fn search_inter_block(
14081412 ref_u.0,
14091413 ref_u.1,
14101414 ref_u.2,
1411- true,
1415+ false,
14121416 search.base_q_idx,
14131417 search.deadzone,
14141418 chroma_set,
@@ -1423,7 +1427,7 @@ fn search_inter_block(
14231427 ref_v.0,
14241428 ref_v.1,
14251429 ref_v.2,
1426- true,
1430+ false,
14271431 search.base_q_idx,
14281432 search.deadzone,
14291433 chroma_set,
@@ -1433,23 +1437,31 @@ fn search_inter_block(
14331437 } else {
14341438 0.0
14351439 };
1440+ let skip = luma_trial.levels.iter().all(|&l| l == 0)
1441+ && u.levels.iter().all(|&l| l == 0)
1442+ && v.levels.iter().all(|&l| l == 0);
14361443 let cost = luma_trial.sse
14371444 + u.sse
14381445 + v.sse
14391446 + search.lambda
1440- * (skip_bits(true)
1447+ * (skip_bits(skip)
14411448 + intra_inter_bits(true)
14421449 + single_ref_bits
14431450 + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0)
14441451 + drl_bits
1445- + mv_bits);
1452+ + mv_bits
1453+ + if skip {
1454+ 0.0
1455+ } else {
1456+ luma_trial.bits + u.bits + v.bits
1457+ });
14461458 consider(Candidate {
14471459 cost,
14481460 luma: luma_trial,
14491461 u,
14501462 v,
14511463 mode: DC_PRED,
1452- skip: true,
1464+ skip,
14531465 inter: Some(InterInfo {
14541466 mode: InterMode::NewMv,
14551467 mv,
@@ -1793,6 +1805,74 @@ mod tests {
17931805 }
17941806 }
17951807
1808+ /// The minimal repro that isolated the inter-residual desync: a key frame
1809+ /// followed by one inter frame whose single superblock carries one
1810+ /// `NEARESTMV` block (`mv == (0, 0)`) with exactly one nonzero luma
1811+ /// coefficient, its three sibling blocks all `skip: true`. Before the
1812+ /// `is_inter` transform-block fix this broke ffmpeg/dav1d ("Invalid data
1813+ /// found when processing input") even though every symbol either block
1814+ /// codes is otherwise proven (all-skip inter blocks decode clean, and the
1815+ /// same coefficient syntax decodes clean on every intra key-frame gate).
1816+ #[test]
1817+ fn a_nearestmv_block_with_one_nonzero_coefficient_decodes_clean() {
1818+ if !have_ffmpeg() {
1819+ eprintln!(
1820+ "SKIP a_nearestmv_block_with_one_nonzero_coefficient_decodes_clean: no ffmpeg"
1821+ );
1822+ return;
1823+ }
1824+ let (width, height) = (64usize, 64usize);
1825+ let picture = Picture::grey(width, height);
1826+ let key = encode_key_frame(&picture, 100, 0.5).unwrap();
1827+
1828+ let (seq, _) = key_frame_headers(width, height, 100).unwrap();
1829+ let (_, inter_header) = inter_frame_headers(width, height, 100, 1, 0).unwrap();
1830+
1831+ let residual_block = BlockCoeffs {
1832+ luma: vec![Coeff {
1833+ row: 0,
1834+ col: 0,
1835+ level: 4,
1836+ }],
1837+ u: Vec::new(),
1838+ v: Vec::new(),
1839+ mode: 0,
1840+ skip: false,
1841+ inter: Some(InterInfo {
1842+ mode: InterMode::NearestMv,
1843+ mv: (0, 0),
1844+ }),
1845+ };
1846+ let skipped_block = BlockCoeffs {
1847+ skip: true,
1848+ inter: Some(InterInfo {
1849+ mode: InterMode::NearestMv,
1850+ mv: (0, 0),
1851+ }),
1852+ ..BlockCoeffs::default()
1853+ };
1854+ let blocks = vec![
1855+ residual_block,
1856+ skipped_block.clone(),
1857+ skipped_block.clone(),
1858+ skipped_block,
1859+ ];
1860+ let tile =
1861+ sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks)
1862+ .unwrap();
1863+
1864+ // `key.stream` is already a temporal delimiter, the sequence header
1865+ // and the key frame's own OBU (`encode_key_frame` built it from the
1866+ // same `key_frame_headers(width, height, 100)` this test calls), so
1867+ // the inter frame's OBU is appended straight onto it rather than
1868+ // re-deriving the key frame's tile bytes.
1869+ let mut stream = key.stream.clone();
1870+ stream.extend_from_slice(&temporal_delimiter());
1871+ stream.extend_from_slice(&frame_obu(&seq, &inter_header, &tile).unwrap());
1872+
1873+ ffmpeg_decode_sequence(&stream, width, height, 2);
1874+ }
1875+
17961876 /// One frame of a real clip, scaled to a whole number of 32x32 blocks.
17971877 fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture {
17981878 let out = Command::new("ffmpeg")
crates/ec-av1/src/tile.rsmodified+11 -2
@@ -1577,7 +1577,12 @@ pub fn sb_coeff_inter_frame_tile(
15771577 let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize);
15781578 let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx));
15791579 let mut enc = SymbolEncoder::new();
1580- let split_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16];
1580+ // An `is_inter` block's 32x32 luma transform reads a different
1581+ // `tx_type` set than an intra block's (`get_tx_set`, spec 5.11.48; see
1582+ // `TxbSet::Luma32Inter`'s doc comment); chroma's transform type is
1583+ // derived from luma's, not coded, so it never differs between the two.
1584+ let intra_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16];
1585+ let inter_planes = [TxbSet::Luma32Inter, TxbSet::Chroma16, TxbSet::Chroma16];
15811586 let scan32 = default_scan(TX32);
15821587 let scan16 = default_scan(TX16);
15831588 let zero_grids = [
@@ -1707,7 +1712,11 @@ pub fn sb_coeff_inter_frame_tile(
17071712 write_block_planes(
17081713 &mut enc,
17091714 &mut cdfs,
1710- &split_planes,
1715+ if is_inter {
1716+ &inter_planes
1717+ } else {
1718+ &intra_planes
1719+ },
17111720 &grids,
17121721 &[&scan32, &scan16, &scan16],
17131722 &neighbours.around(at, BLOCK),