Merge branch 'lane-av1-desync': tx_type for inter residuals, real RD skip
Browse files at this commit4 changed files +163 -35
- crates/ec-av1/src/cdf.rs +14 -3
- crates/ec-av1/src/cdf_state.rs +32 -4
- crates/ec-av1/src/encode.rs +106 -26
- crates/ec-av1/src/tile.rs +11 -2
crates/ec-av1/src/cdf.rsmodified+14 -3
| @@ -939,9 +939,12 @@ pub const PARTITION_W16: [[u16; 11]; 4] = [ | ||
| 939 | 939 | /// The transform type of a 16x16 intra luma transform, as an index into the |
| 940 | 940 | /// spec's `Tx_Type_Intra_Inv_Set2`, indexed by the block's luma mode. |
| 941 | 941 | /// |
| 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. | |
| 945 | 948 | pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ |
| 946 | 949 | [1127, 12814, 22772, 27483, 32768, 0], |
| 947 | 950 | [145, 6761, 11980, 26667, 32768, 0], |
| @@ -958,6 +961,14 @@ pub const INTRA_TX_TYPE_SET2_16: [[u16; 6]; 13] = [ | ||
| 958 | 961 | [1968, 5556, 12023, 18547, 32768, 0], |
| 959 | 962 | ]; |
| 960 | 963 | |
| 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 | + | |
| 961 | 972 | // The inter-frame tables below (spec 9.4) support the syntax an inter block |
| 962 | 973 | // needs: whether it is coded as intra at all, which reference frame and |
| 963 | 974 | // 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; | ||
| 16 | 16 | /// Which of the four coefficient table sets a transform block is coded with. |
| 17 | 17 | #[derive(Clone, Copy)] |
| 18 | 18 | 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. | |
| 20 | 23 | 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, | |
| 21 | 30 | /// The 64x64 luma transform of a whole superblock, scanned as a 32x32. |
| 22 | 31 | Luma64, |
| 23 | 32 | /// The 16x16 luma transform of a 16x16 block. |
| @@ -49,8 +58,11 @@ pub(crate) struct TxbTables<'a> { | ||
| 49 | 58 | pub br: &'a mut [[u16; 5]; 21], |
| 50 | 59 | /// The sign of the DC. |
| 51 | 60 | 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]>, | |
| 54 | 66 | } |
| 55 | 67 | |
| 56 | 68 | /// Every table a key frame's tile writer adapts. |
| @@ -144,6 +156,10 @@ pub(crate) struct Cdfs { | ||
| 144 | 156 | pub dc_sign_luma: [[u16; 3]; 3], |
| 145 | 157 | /// The transform type of a 16x16 intra luma transform, by luma mode. |
| 146 | 158 | 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], | |
| 147 | 163 | /// The DC sign of a chroma coefficient, shared by both chroma sets. |
| 148 | 164 | pub dc_sign_chroma: [[u16; 3]; 3], |
| 149 | 165 | /// Whether an inter frame's block is coded as intra. |
| @@ -472,6 +488,7 @@ impl Cdfs { | ||
| 472 | 488 | ), |
| 473 | 489 | dc_sign_luma: cdf::DC_SIGN_LUMA, |
| 474 | 490 | intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, |
| 491 | + inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, | |
| 475 | 492 | dc_sign_chroma: cdf::DC_SIGN_CHROMA, |
| 476 | 493 | intra_inter: cdf::INTRA_INTER, |
| 477 | 494 | single_ref: cdf::SINGLE_REF, |
| @@ -499,6 +516,17 @@ impl Cdfs { | ||
| 499 | 516 | dc_sign: &mut self.dc_sign_luma, |
| 500 | 517 | tx_type: None, |
| 501 | 518 | }, |
| 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 | + }, | |
| 502 | 530 | TxbSet::Luma64 => TxbTables { |
| 503 | 531 | side: 32, |
| 504 | 532 | txb_skip: &mut self.txb_skip_luma_64, |
| @@ -519,7 +547,7 @@ impl Cdfs { | ||
| 519 | 547 | base_eob: &mut self.base_eob_luma_16, |
| 520 | 548 | br: &mut self.br_luma_16, |
| 521 | 549 | 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()), | |
| 523 | 551 | }, |
| 524 | 552 | TxbSet::Chroma8 => TxbTables { |
| 525 | 553 | side: 8, |
crates/ec-av1/src/encode.rsmodified+106 -26
| @@ -1176,8 +1176,11 @@ fn mc_trial( | ||
| 1176 | 1176 | /// intra mode [`Search::modes`] offers (predicted exactly as a key frame's |
| 1177 | 1177 | /// block is), `NEARESTMV`, and `NEWMV` searched from `reference` by |
| 1178 | 1178 | /// [`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. | |
| 1181 | 1184 | /// |
| 1182 | 1185 | /// The symbol costs this ranks candidates by use fixed contexts (0) for |
| 1183 | 1186 | /// `skip`, `intra_inter` and `single_ref` — the tile writer's actual |
| @@ -1289,19 +1292,9 @@ fn search_inter_block( | ||
| 1289 | 1292 | + symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1) // not zero |
| 1290 | 1293 | + symbol_bits(&cdf::REF_MV[stack.ref_mv_ctx], 0); // NEARESTMV |
| 1291 | 1294 | |
| 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. | |
| 1305 | 1298 | { |
| 1306 | 1299 | let mv = stack.nearest_mv; |
| 1307 | 1300 | let luma_trial = mc_trial( |
| @@ -1314,7 +1307,7 @@ fn search_inter_block( | ||
| 1314 | 1307 | ref_luma.0, |
| 1315 | 1308 | ref_luma.1, |
| 1316 | 1309 | ref_luma.2, |
| 1317 | - true, | |
| 1310 | + false, | |
| 1318 | 1311 | search.base_q_idx, |
| 1319 | 1312 | search.deadzone, |
| 1320 | 1313 | luma_set, |
| @@ -1329,7 +1322,7 @@ fn search_inter_block( | ||
| 1329 | 1322 | ref_u.0, |
| 1330 | 1323 | ref_u.1, |
| 1331 | 1324 | ref_u.2, |
| 1332 | - true, | |
| 1325 | + false, | |
| 1333 | 1326 | search.base_q_idx, |
| 1334 | 1327 | search.deadzone, |
| 1335 | 1328 | chroma_set, |
| @@ -1344,23 +1337,34 @@ fn search_inter_block( | ||
| 1344 | 1337 | ref_v.0, |
| 1345 | 1338 | ref_v.1, |
| 1346 | 1339 | ref_v.2, |
| 1347 | - true, | |
| 1340 | + false, | |
| 1348 | 1341 | search.base_q_idx, |
| 1349 | 1342 | search.deadzone, |
| 1350 | 1343 | chroma_set, |
| 1351 | 1344 | ); |
| 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); | |
| 1352 | 1348 | let cost = luma_trial.sse |
| 1353 | 1349 | + u.sse |
| 1354 | 1350 | + v.sse |
| 1355 | 1351 | + 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 | + }); | |
| 1357 | 1361 | consider(Candidate { |
| 1358 | 1362 | cost, |
| 1359 | 1363 | luma: luma_trial, |
| 1360 | 1364 | u, |
| 1361 | 1365 | v, |
| 1362 | 1366 | mode: DC_PRED, |
| 1363 | - skip: true, | |
| 1367 | + skip, | |
| 1364 | 1368 | inter: Some(InterInfo { |
| 1365 | 1369 | mode: InterMode::NearestMv, |
| 1366 | 1370 | mv, |
| @@ -1393,7 +1397,7 @@ fn search_inter_block( | ||
| 1393 | 1397 | ref_luma.0, |
| 1394 | 1398 | ref_luma.1, |
| 1395 | 1399 | ref_luma.2, |
| 1396 | - true, | |
| 1400 | + false, | |
| 1397 | 1401 | search.base_q_idx, |
| 1398 | 1402 | search.deadzone, |
| 1399 | 1403 | luma_set, |
| @@ -1408,7 +1412,7 @@ fn search_inter_block( | ||
| 1408 | 1412 | ref_u.0, |
| 1409 | 1413 | ref_u.1, |
| 1410 | 1414 | ref_u.2, |
| 1411 | - true, | |
| 1415 | + false, | |
| 1412 | 1416 | search.base_q_idx, |
| 1413 | 1417 | search.deadzone, |
| 1414 | 1418 | chroma_set, |
| @@ -1423,7 +1427,7 @@ fn search_inter_block( | ||
| 1423 | 1427 | ref_v.0, |
| 1424 | 1428 | ref_v.1, |
| 1425 | 1429 | ref_v.2, |
| 1426 | - true, | |
| 1430 | + false, | |
| 1427 | 1431 | search.base_q_idx, |
| 1428 | 1432 | search.deadzone, |
| 1429 | 1433 | chroma_set, |
| @@ -1433,23 +1437,31 @@ fn search_inter_block( | ||
| 1433 | 1437 | } else { |
| 1434 | 1438 | 0.0 |
| 1435 | 1439 | }; |
| 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); | |
| 1436 | 1443 | let cost = luma_trial.sse |
| 1437 | 1444 | + u.sse |
| 1438 | 1445 | + v.sse |
| 1439 | 1446 | + search.lambda |
| 1440 | - * (skip_bits(true) | |
| 1447 | + * (skip_bits(skip) | |
| 1441 | 1448 | + intra_inter_bits(true) |
| 1442 | 1449 | + single_ref_bits |
| 1443 | 1450 | + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) |
| 1444 | 1451 | + 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 | + }); | |
| 1446 | 1458 | consider(Candidate { |
| 1447 | 1459 | cost, |
| 1448 | 1460 | luma: luma_trial, |
| 1449 | 1461 | u, |
| 1450 | 1462 | v, |
| 1451 | 1463 | mode: DC_PRED, |
| 1452 | - skip: true, | |
| 1464 | + skip, | |
| 1453 | 1465 | inter: Some(InterInfo { |
| 1454 | 1466 | mode: InterMode::NewMv, |
| 1455 | 1467 | mv, |
| @@ -1793,6 +1805,74 @@ mod tests { | ||
| 1793 | 1805 | } |
| 1794 | 1806 | } |
| 1795 | 1807 | |
| 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 | + | |
| 1796 | 1876 | /// One frame of a real clip, scaled to a whole number of 32x32 blocks. |
| 1797 | 1877 | fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture { |
| 1798 | 1878 | let out = Command::new("ffmpeg") |
crates/ec-av1/src/tile.rsmodified+11 -2
| @@ -1577,7 +1577,12 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 1577 | 1577 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 1578 | 1578 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); |
| 1579 | 1579 | 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]; | |
| 1581 | 1586 | let scan32 = default_scan(TX32); |
| 1582 | 1587 | let scan16 = default_scan(TX16); |
| 1583 | 1588 | let zero_grids = [ |
| @@ -1707,7 +1712,11 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 1707 | 1712 | write_block_planes( |
| 1708 | 1713 | &mut enc, |
| 1709 | 1714 | &mut cdfs, |
| 1710 | - &split_planes, | |
| 1715 | + if is_inter { | |
| 1716 | + &inter_planes | |
| 1717 | + } else { | |
| 1718 | + &intra_planes | |
| 1719 | + }, | |
| 1711 | 1720 | &grids, |
| 1712 | 1721 | &[&scan32, &scan16, &scan16], |
| 1713 | 1722 | &neighbours.around(at, BLOCK), |