forge
Explore
Log in

Don't you dare go Hollow

Code a real intra mode per block

Browse files at this commit

6cacc2bf2df8676a7f553307e1fd16cd43a151feAhmet Kaan GümüşParent: b99ade1
  • feat
    • code a real intra mode per block
Show full message
feat(av1): code a real intra mode per block

The tile writer coded DC_PRED for every block. It now takes the luma
intra mode with the block's coefficients and codes it against the
key-frame CDF the block's neighbours select, with the angle delta a
directional mode carries.

The chroma mode stays DC_PRED, but the table it is coded with is
indexed by the luma mode — holding the DC row while the luma mode moved
desynchronised every stream, which is what the new decode test caught
first.

A dav1d decode gates the result on what the modes promise rather than on
pinned bytes: the top-left block carries a basis function that is flat
across it and falls down it, the block to its right is H_PRED and must
repeat its left neighbour's column, and the block below is V_PRED and
must repeat the row above it. Dropping the angle delta, pinning the mode
context or pinning the chroma table each fail it.
crates/ec-av1/src/cdf.rsmodified+69 -5
@@ -246,13 +246,63 @@ pub const PARTITION_W32: [[u16; 11]; 4] = [
246 ],246 ],
247 ];247 ];
248 248
249-/// `Default_Uv_Mode_Cfl_Allowed_Cdf[0]` (spec 9.4): the fourteen chroma intra249+/// `Default_Uv_Mode_Cfl_Allowed_Cdf` (spec 9.4): the fourteen chroma intra
250-/// modes — the thirteen the CFL-free table has, plus chroma from luma — for a250+/// modes — the thirteen the CFL-free table has, plus chroma from luma —
251-/// DC-predicted luma block. A block of 32x32 or smaller is offered CFL, so it251+/// indexed by the luma mode of the same block. A block of 32x32 or smaller is
252-/// reads this table where a 64x64 block reads [`UV_MODE_NO_CFL`].252+/// offered CFL, so it reads this table where a 64x64 block reads
253-pub const UV_MODE_CFL_DC: [u16; 15] = [253+/// [`UV_MODE_NO_CFL`].
254+pub const UV_MODE_CFL: [[u16; 15]; 13] = [
255+ [
254 10407, 11208, 12900, 13181, 13823, 14175, 14899, 15656, 15986, 20086, 20995, 22455, 24212,256 10407, 11208, 12900, 13181, 13823, 14175, 14899, 15656, 15986, 20086, 20995, 22455, 24212,
255 32768, 0,257 32768, 0,
258+ ],
259+ [
260+ 4532, 19780, 20057, 20215, 20428, 21071, 21199, 21451, 22099, 24228, 24693, 27032, 29472,
261+ 32768, 0,
262+ ],
263+ [
264+ 5273, 5379, 20177, 20270, 20385, 20439, 20949, 21695, 21774, 23138, 24256, 24703, 26679,
265+ 32768, 0,
266+ ],
267+ [
268+ 6740, 7167, 7662, 14152, 14536, 14785, 15034, 16741, 18371, 21520, 22206, 23389, 24182,
269+ 32768, 0,
270+ ],
271+ [
272+ 4987, 5368, 5928, 6068, 19114, 20315, 21857, 22253, 22411, 24911, 25380, 26027, 26376,
273+ 32768, 0,
274+ ],
275+ [
276+ 5370, 6889, 7247, 7393, 9498, 21114, 21402, 21753, 21981, 24780, 25386, 26517, 27176,
277+ 32768, 0,
278+ ],
279+ [
280+ 4816, 4961, 7204, 7326, 8765, 8930, 20169, 20682, 20803, 23188, 23763, 24455, 24940, 32768,
281+ 0,
282+ ],
283+ [
284+ 6608, 6740, 8529, 9049, 9257, 9356, 9735, 18827, 19059, 22336, 23204, 23964, 24793, 32768,
285+ 0,
286+ ],
287+ [
288+ 5998, 7419, 7781, 8933, 9255, 9549, 9753, 10417, 18898, 22494, 23139, 24764, 25989, 32768,
289+ 0,
290+ ],
291+ [
292+ 10660, 11298, 12550, 12957, 13322, 13624, 14040, 15004, 15534, 20714, 21789, 23443, 24861,
293+ 32768, 0,
294+ ],
295+ [
296+ 10522, 11530, 12552, 12963, 13378, 13779, 14245, 15235, 15902, 20102, 22696, 23774, 25838,
297+ 32768, 0,
298+ ],
299+ [
300+ 10099, 10691, 12639, 13049, 13386, 13665, 14125, 15163, 15636, 19676, 20474, 23519, 25208,
301+ 32768, 0,
302+ ],
303+ [
304+ 3144, 5087, 7382, 7504, 7593, 7690, 7801, 8064, 8232, 9248, 9875, 10521, 29048, 32768, 0,
305+ ],
256 ];306 ];
257 307
258 /// `Default_Txb_Skip_Cdf[2][3][0]` (spec 9.4): the all-zero flag of a 32x32308 /// `Default_Txb_Skip_Cdf[2][3][0]` (spec 9.4): the all-zero flag of a 32x32
@@ -480,3 +530,17 @@ pub const EOB_PT_256_CHROMA: [u16; 10] = [
480 /// `Default_Dc_Sign_Cdf[2][1]` (spec 9.4): the sign of a chroma DC530 /// `Default_Dc_Sign_Cdf[2][1]` (spec 9.4): the sign of a chroma DC
481 /// coefficient, indexed the way [`DC_SIGN_LUMA`] is.531 /// coefficient, indexed the way [`DC_SIGN_LUMA`] is.
482 pub const DC_SIGN_CHROMA: [[u16; 3]; 3] = [[15232, 32768, 0], [12928, 32768, 0], [17280, 32768, 0]];532 pub const DC_SIGN_CHROMA: [[u16; 3]; 3] = [[15232, 32768, 0], [12928, 32768, 0], [17280, 32768, 0]];
533+
534+/// `Default_Angle_Delta_Cdf` (spec 9.4): the offset a directional intra mode
535+/// is steered by, seven symbols from -3 to +3, one row per directional mode in
536+/// the order they follow `V_PRED`.
537+pub const ANGLE_DELTA: [[u16; 8]; 8] = [
538+ [2180, 5032, 7567, 22776, 26989, 30217, 32768, 0],
539+ [2301, 5608, 8801, 23487, 26974, 30330, 32768, 0],
540+ [3780, 11018, 13699, 19354, 23083, 31286, 32768, 0],
541+ [4581, 11226, 15147, 17138, 21834, 28397, 32768, 0],
542+ [1737, 10927, 14509, 19588, 22745, 28823, 32768, 0],
543+ [2664, 10176, 12485, 17650, 21600, 30495, 32768, 0],
544+ [2240, 11096, 15453, 20341, 22561, 28917, 32768, 0],
545+ [3605, 10428, 12459, 17676, 21244, 30655, 32768, 0],
546+];
crates/ec-av1/src/tile.rsmodified+150 -3
@@ -20,6 +20,29 @@ const PARTITION_NONE: usize = 0;
20 const PARTITION_SPLIT: usize = 3;20 const PARTITION_SPLIT: usize = 3;
21 /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode.21 /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode.
22 const DC_PRED: usize = 0;22 const DC_PRED: usize = 0;
23+
24+/// `V_PRED` (spec 6.10.2), the first of the eight directional intra modes and
25+/// so the first mode that carries an angle delta.
26+const V_PRED: usize = 1;
27+
28+/// `H_PRED`, which predicts every row from the column to the block's left.
29+#[cfg(test)]
30+const H_PRED: usize = 2;
31+
32+/// The last directional mode, `D67_PRED`.
33+const D67_PRED: usize = 8;
34+
35+/// The number of intra modes a key frame's luma block chooses from,
36+/// `INTRA_MODES` (spec 3).
37+const INTRA_MODES: usize = 13;
38+
39+/// `Intra_Mode_Context` (spec 9.3): the five-way class each intra mode puts
40+/// its neighbours in when they pick the CDF for the next block's mode.
41+const INTRA_MODE_CTX: [usize; INTRA_MODES] = [0, 1, 2, 3, 4, 4, 4, 4, 3, 0, 1, 2, 0];
42+
43+/// The symbol an angle delta of zero codes as: the alphabet runs from -3 to
44+/// +3, so `MAX_ANGLE_DELTA` is the middle of it.
45+const ANGLE_DELTA_ZERO: usize = 3;
23 /// Side of a superblock in 4x4 mode-info units when 128x128 superblocks are off.46 /// Side of a superblock in 4x4 mode-info units when 128x128 superblocks are off.
24 const SB_MI: u32 = 16;47 const SB_MI: u32 = 16;
25 48
@@ -237,7 +260,7 @@ pub fn split_dc_key_frame_tile(
237 enc.symbol_fixed(DC_PRED, &cdf::KF_Y_MODE[0][0]);260 enc.symbol_fixed(DC_PRED, &cdf::KF_Y_MODE[0][0]);
238 // Chroma from luma is offered up to 32x32, so the block reads261 // Chroma from luma is offered up to 32x32, so the block reads
239 // the wider table even though it does not take the mode.262 // the wider table even though it does not take the mode.
240- enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL_DC);263+ enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL[DC_PRED]);
241 264
242 write_dc_coeffs(265 write_dc_coeffs(
243 &mut enc,266 &mut enc,
@@ -285,6 +308,11 @@ pub struct BlockCoeffs {
285 pub u: Vec<Coeff>,308 pub u: Vec<Coeff>,
286 /// The V plane's.309 /// The V plane's.
287 pub v: Vec<Coeff>,310 pub v: Vec<Coeff>,
311+ /// The luma intra mode the block is predicted with, one of the thirteen
312+ /// modes a key frame codes (`DC_PRED` is zero, which is what
313+ /// [`Default`] and a plain coefficient list give). Chroma stays on
314+ /// `DC_PRED`.
315+ pub mode: u8,
288 }316 }
289 317
290 impl From<Vec<Coeff>> for BlockCoeffs {318 impl From<Vec<Coeff>> for BlockCoeffs {
@@ -397,6 +425,15 @@ pub fn split_coeff_key_frame_tile(
397 "a coefficient key frame needs one coefficient set per 32x32 block",425 "a coefficient key frame needs one coefficient set per 32x32 block",
398 ));426 ));
399 }427 }
428+ if let Some(bad) = blocks.iter().find(|b| usize::from(b.mode) >= INTRA_MODES) {
429+ return Err(Error::unsupported(
430+ "AV1 tile",
431+ format!(
432+ "intra mode {} is not one of the thirteen a key frame codes",
433+ bad.mode
434+ ),
435+ ));
436+ }
400 if !Q_CTX_2.contains(&base_q_idx) {437 if !Q_CTX_2.contains(&base_q_idx) {
401 return Err(Error::unsupported(438 return Err(Error::unsupported(
402 "AV1 tile",439 "AV1 tile",
@@ -424,10 +461,16 @@ pub fn split_coeff_key_frame_tile(
424 // because a tile starts each row with no left neighbour.461 // because a tile starts each row with no left neighbour.
425 let mut above = vec![[Neighbour::default(); 3]; cols as usize];462 let mut above = vec![[Neighbour::default(); 3]; cols as usize];
426 let mut left = vec![[Neighbour::default(); 3]; rows as usize];463 let mut left = vec![[Neighbour::default(); 3]; rows as usize];
464+ // The luma intra mode of those same neighbours, which picks the CDF the
465+ // next block's mode is coded with. A block outside the tile reads as
466+ // `DC_PRED`, which is what these start and are reset to.
467+ let mut above_mode = vec![DC_PRED; cols as usize];
468+ let mut left_mode = vec![DC_PRED; rows as usize];
427 469
428 let mut enc = SymbolEncoder::new();470 let mut enc = SymbolEncoder::new();
429 for sb_r in 0..sb_rows {471 for sb_r in 0..sb_rows {
430 left.iter_mut().for_each(|l| *l = Default::default());472 left.iter_mut().for_each(|l| *l = Default::default());
473+ left_mode.iter_mut().for_each(|m| *m = DC_PRED);
431 for sb_c in 0..sb_cols {474 for sb_c in 0..sb_cols {
432 let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0);475 let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0);
433 enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]);476 enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]);
@@ -436,11 +479,20 @@ pub fn split_coeff_key_frame_tile(
436 let (r, c) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);479 let (r, c) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);
437 let (r, c) = (r as usize, c as usize);480 let (r, c) = (r as usize, c as usize);
438 let block = &grids[r * cols as usize + c];481 let block = &grids[r * cols as usize + c];
482+ let mode = usize::from(blocks[r * cols as usize + c].mode);
439 483
440 enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W32[0]);484 enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W32[0]);
441 enc.symbol_fixed(0, &cdf::SKIP[0]);485 enc.symbol_fixed(0, &cdf::SKIP[0]);
442- enc.symbol_fixed(DC_PRED, &cdf::KF_Y_MODE[0][0]);486+ enc.symbol_fixed(
443- enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL_DC);487+ mode,
488+ &cdf::KF_Y_MODE[INTRA_MODE_CTX[above_mode[c]]][INTRA_MODE_CTX[left_mode[r]]],
489+ );
490+ if (V_PRED..=D67_PRED).contains(&mode) {
491+ enc.symbol_fixed(ANGLE_DELTA_ZERO, &cdf::ANGLE_DELTA[mode - V_PRED]);
492+ }
493+ enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL[mode]);
494+ above_mode[c] = mode;
495+ left_mode[r] = mode;
444 496
445 for (plane, grid) in block.iter().enumerate() {497 for (plane, grid) in block.iter().enumerate() {
446 let coding = planes[plane];498 let coding = planes[plane];
@@ -1628,6 +1680,7 @@ mod tests {
1628 luma: Vec::new(),1680 luma: Vec::new(),
1629 u: dc(level),1681 u: dc(level),
1630 v: dc(-level),1682 v: dc(-level),
1683+ ..BlockCoeffs::default()
1631 })1684 })
1632 .collect();1685 .collect();
1633 let [luma, u, v] = decode_coeff_planes(&blocks);1686 let [luma, u, v] = decode_coeff_planes(&blocks);
@@ -1674,6 +1727,100 @@ mod tests {
1674 1727
1675 /// The coefficient writer refuses what it cannot code rather than writing a1728 /// The coefficient writer refuses what it cannot code rather than writing a
1676 /// stream a decoder walks off the end of.1729 /// stream a decoder walks off the end of.
1730+ /// The 32x32 block at the given block row and column of a decoded plane.
1731+ fn block_at(rows: &[Vec<u8>], block_row: usize, block_col: usize) -> Vec<Vec<u8>> {
1732+ rows[block_row * 32..block_row * 32 + 32]
1733+ .iter()
1734+ .map(|r| r[block_col * 32..block_col * 32 + 32].to_vec())
1735+ .collect()
1736+ }
1737+
1738+ /// A directional intra mode says where its prediction comes from, and with
1739+ /// no residual of its own a block must be exactly that prediction. The
1740+ /// top-left block is given a basis function that is flat across it and
1741+ /// falls down it; the block to its right is coded `H_PRED`, so every one of
1742+ /// its rows must be the constant its left neighbour's rightmost column
1743+ /// hands it, and the block below is coded `V_PRED`, so every one of its
1744+ /// columns must be the constant the row above it ends on — a single flat
1745+ /// value for the whole block. Nothing here pins the transform's shape or
1746+ /// the mode's CDF: a mode written with the wrong context, an angle delta
1747+ /// left out or a mode index off by one desyncs the symbol decoder and the
1748+ /// two empty blocks stop mirroring their neighbours.
1749+ #[test]
1750+ fn directional_modes_predict_from_the_neighbours_they_name() {
1751+ if !have_ffmpeg() {
1752+ eprintln!("SKIP directional_modes_predict_from_the_neighbours_they_name: no ffmpeg");
1753+ return;
1754+ }
1755+ let gradient = BlockCoeffs {
1756+ luma: vec![Coeff {
1757+ row: 1,
1758+ col: 0,
1759+ level: 12,
1760+ }],
1761+ ..BlockCoeffs::default()
1762+ };
1763+ let blocks = [
1764+ gradient,
1765+ BlockCoeffs {
1766+ mode: H_PRED as u8,
1767+ ..BlockCoeffs::default()
1768+ },
1769+ BlockCoeffs {
1770+ mode: V_PRED as u8,
1771+ ..BlockCoeffs::default()
1772+ },
1773+ BlockCoeffs::default(),
1774+ ];
1775+ let [luma, ..] = decode_coeff_planes(&blocks);
1776+
1777+ let source = block_at(&luma, 0, 0);
1778+ for (r, row) in source.iter().enumerate() {
1779+ assert!(
1780+ row.iter().all(|&s| s == row[0]),
1781+ "the basis function at row 1, column 0 is flat across the block, row {r} is not: {row:?}"
1782+ );
1783+ }
1784+ let profile: Vec<u8> = source.iter().map(|r| r[0]).collect();
1785+ assert!(
1786+ profile.windows(2).all(|w| w[0] >= w[1]) && profile[0] > profile[31],
1787+ "the block must fall from top to bottom, got {profile:?}"
1788+ );
1789+
1790+ let horizontal = block_at(&luma, 0, 1);
1791+ for (r, row) in horizontal.iter().enumerate() {
1792+ assert!(
1793+ row.iter().all(|&s| s == profile[r]),
1794+ "an H_PRED block repeats the column to its left, row {r} should be all {} but is {row:?}",
1795+ profile[r]
1796+ );
1797+ }
1798+
1799+ let vertical = block_at(&luma, 1, 0);
1800+ let bottom = profile[31];
1801+ assert!(
1802+ vertical.iter().flatten().all(|&s| s == bottom),
1803+ "a V_PRED block repeats the row above it, which is all {bottom} here, got rows \
1804+ {:?} and {:?}",
1805+ vertical[0],
1806+ vertical[31]
1807+ );
1808+ }
1809+
1810+ /// The mode index is checked before anything is written.
1811+ #[test]
1812+ fn an_intra_mode_outside_the_key_frame_set_is_refused() {
1813+ let mut blocks = vec![BlockCoeffs::default(); 4];
1814+ blocks[2].mode = INTRA_MODES as u8;
1815+ let message = split_coeff_key_frame_tile(16, 16, Q_IDX, &blocks)
1816+ .unwrap_err()
1817+ .to_string();
1818+ assert!(
1819+ message.contains("intra mode 13"),
1820+ "the refusal must name the mode, got {message}"
1821+ );
1822+ }
1823+
1677 #[test]1824 #[test]
1678 fn coefficients_the_writer_cannot_code_are_refused() {1825 fn coefficients_the_writer_cannot_code_are_refused() {
1679 let empty = || vec![BlockCoeffs::default(); 4];1826 let empty = || vec![BlockCoeffs::default(); 4];