diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index dfabf5ac..d4e5958d 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -246,13 +246,63 @@ pub const PARTITION_W32: [[u16; 11]; 4] = [ ], ]; -/// `Default_Uv_Mode_Cfl_Allowed_Cdf[0]` (spec 9.4): the fourteen chroma intra -/// modes — the thirteen the CFL-free table has, plus chroma from luma — for a -/// DC-predicted luma block. A block of 32x32 or smaller is offered CFL, so it -/// reads this table where a 64x64 block reads [`UV_MODE_NO_CFL`]. -pub const UV_MODE_CFL_DC: [u16; 15] = [ - 10407, 11208, 12900, 13181, 13823, 14175, 14899, 15656, 15986, 20086, 20995, 22455, 24212, - 32768, 0, +/// `Default_Uv_Mode_Cfl_Allowed_Cdf` (spec 9.4): the fourteen chroma intra +/// modes — the thirteen the CFL-free table has, plus chroma from luma — +/// indexed by the luma mode of the same block. A block of 32x32 or smaller is +/// offered CFL, so it reads this table where a 64x64 block reads +/// [`UV_MODE_NO_CFL`]. +pub const UV_MODE_CFL: [[u16; 15]; 13] = [ + [ + 10407, 11208, 12900, 13181, 13823, 14175, 14899, 15656, 15986, 20086, 20995, 22455, 24212, + 32768, 0, + ], + [ + 4532, 19780, 20057, 20215, 20428, 21071, 21199, 21451, 22099, 24228, 24693, 27032, 29472, + 32768, 0, + ], + [ + 5273, 5379, 20177, 20270, 20385, 20439, 20949, 21695, 21774, 23138, 24256, 24703, 26679, + 32768, 0, + ], + [ + 6740, 7167, 7662, 14152, 14536, 14785, 15034, 16741, 18371, 21520, 22206, 23389, 24182, + 32768, 0, + ], + [ + 4987, 5368, 5928, 6068, 19114, 20315, 21857, 22253, 22411, 24911, 25380, 26027, 26376, + 32768, 0, + ], + [ + 5370, 6889, 7247, 7393, 9498, 21114, 21402, 21753, 21981, 24780, 25386, 26517, 27176, + 32768, 0, + ], + [ + 4816, 4961, 7204, 7326, 8765, 8930, 20169, 20682, 20803, 23188, 23763, 24455, 24940, 32768, + 0, + ], + [ + 6608, 6740, 8529, 9049, 9257, 9356, 9735, 18827, 19059, 22336, 23204, 23964, 24793, 32768, + 0, + ], + [ + 5998, 7419, 7781, 8933, 9255, 9549, 9753, 10417, 18898, 22494, 23139, 24764, 25989, 32768, + 0, + ], + [ + 10660, 11298, 12550, 12957, 13322, 13624, 14040, 15004, 15534, 20714, 21789, 23443, 24861, + 32768, 0, + ], + [ + 10522, 11530, 12552, 12963, 13378, 13779, 14245, 15235, 15902, 20102, 22696, 23774, 25838, + 32768, 0, + ], + [ + 10099, 10691, 12639, 13049, 13386, 13665, 14125, 15163, 15636, 19676, 20474, 23519, 25208, + 32768, 0, + ], + [ + 3144, 5087, 7382, 7504, 7593, 7690, 7801, 8064, 8232, 9248, 9875, 10521, 29048, 32768, 0, + ], ]; /// `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] = [ /// `Default_Dc_Sign_Cdf[2][1]` (spec 9.4): the sign of a chroma DC /// coefficient, indexed the way [`DC_SIGN_LUMA`] is. pub const DC_SIGN_CHROMA: [[u16; 3]; 3] = [[15232, 32768, 0], [12928, 32768, 0], [17280, 32768, 0]]; + +/// `Default_Angle_Delta_Cdf` (spec 9.4): the offset a directional intra mode +/// is steered by, seven symbols from -3 to +3, one row per directional mode in +/// the order they follow `V_PRED`. +pub const ANGLE_DELTA: [[u16; 8]; 8] = [ + [2180, 5032, 7567, 22776, 26989, 30217, 32768, 0], + [2301, 5608, 8801, 23487, 26974, 30330, 32768, 0], + [3780, 11018, 13699, 19354, 23083, 31286, 32768, 0], + [4581, 11226, 15147, 17138, 21834, 28397, 32768, 0], + [1737, 10927, 14509, 19588, 22745, 28823, 32768, 0], + [2664, 10176, 12485, 17650, 21600, 30495, 32768, 0], + [2240, 11096, 15453, 20341, 22561, 28917, 32768, 0], + [3605, 10428, 12459, 17676, 21244, 30655, 32768, 0], +]; diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index f8b01ba7..017b1149 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -20,6 +20,29 @@ const PARTITION_NONE: usize = 0; const PARTITION_SPLIT: usize = 3; /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode. const DC_PRED: usize = 0; + +/// `V_PRED` (spec 6.10.2), the first of the eight directional intra modes and +/// so the first mode that carries an angle delta. +const V_PRED: usize = 1; + +/// `H_PRED`, which predicts every row from the column to the block's left. +#[cfg(test)] +const H_PRED: usize = 2; + +/// The last directional mode, `D67_PRED`. +const D67_PRED: usize = 8; + +/// The number of intra modes a key frame's luma block chooses from, +/// `INTRA_MODES` (spec 3). +const INTRA_MODES: usize = 13; + +/// `Intra_Mode_Context` (spec 9.3): the five-way class each intra mode puts +/// its neighbours in when they pick the CDF for the next block's mode. +const INTRA_MODE_CTX: [usize; INTRA_MODES] = [0, 1, 2, 3, 4, 4, 4, 4, 3, 0, 1, 2, 0]; + +/// The symbol an angle delta of zero codes as: the alphabet runs from -3 to +/// +3, so `MAX_ANGLE_DELTA` is the middle of it. +const ANGLE_DELTA_ZERO: usize = 3; /// Side of a superblock in 4x4 mode-info units when 128x128 superblocks are off. const SB_MI: u32 = 16; @@ -237,7 +260,7 @@ pub fn split_dc_key_frame_tile( enc.symbol_fixed(DC_PRED, &cdf::KF_Y_MODE[0][0]); // Chroma from luma is offered up to 32x32, so the block reads // the wider table even though it does not take the mode. - enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL_DC); + enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL[DC_PRED]); write_dc_coeffs( &mut enc, @@ -285,6 +308,11 @@ pub struct BlockCoeffs { pub u: Vec, /// The V plane's. pub v: Vec, + /// The luma intra mode the block is predicted with, one of the thirteen + /// modes a key frame codes (`DC_PRED` is zero, which is what + /// [`Default`] and a plain coefficient list give). Chroma stays on + /// `DC_PRED`. + pub mode: u8, } impl From> for BlockCoeffs { @@ -397,6 +425,15 @@ pub fn split_coeff_key_frame_tile( "a coefficient key frame needs one coefficient set per 32x32 block", )); } + if let Some(bad) = blocks.iter().find(|b| usize::from(b.mode) >= INTRA_MODES) { + return Err(Error::unsupported( + "AV1 tile", + format!( + "intra mode {} is not one of the thirteen a key frame codes", + bad.mode + ), + )); + } if !Q_CTX_2.contains(&base_q_idx) { return Err(Error::unsupported( "AV1 tile", @@ -424,10 +461,16 @@ pub fn split_coeff_key_frame_tile( // because a tile starts each row with no left neighbour. let mut above = vec![[Neighbour::default(); 3]; cols as usize]; let mut left = vec![[Neighbour::default(); 3]; rows as usize]; + // The luma intra mode of those same neighbours, which picks the CDF the + // next block's mode is coded with. A block outside the tile reads as + // `DC_PRED`, which is what these start and are reset to. + let mut above_mode = vec![DC_PRED; cols as usize]; + let mut left_mode = vec![DC_PRED; rows as usize]; let mut enc = SymbolEncoder::new(); for sb_r in 0..sb_rows { left.iter_mut().for_each(|l| *l = Default::default()); + left_mode.iter_mut().for_each(|m| *m = DC_PRED); for sb_c in 0..sb_cols { let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0); enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]); @@ -436,11 +479,20 @@ pub fn split_coeff_key_frame_tile( let (r, c) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); let (r, c) = (r as usize, c as usize); let block = &grids[r * cols as usize + c]; + let mode = usize::from(blocks[r * cols as usize + c].mode); enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W32[0]); enc.symbol_fixed(0, &cdf::SKIP[0]); - enc.symbol_fixed(DC_PRED, &cdf::KF_Y_MODE[0][0]); - enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL_DC); + enc.symbol_fixed( + mode, + &cdf::KF_Y_MODE[INTRA_MODE_CTX[above_mode[c]]][INTRA_MODE_CTX[left_mode[r]]], + ); + if (V_PRED..=D67_PRED).contains(&mode) { + enc.symbol_fixed(ANGLE_DELTA_ZERO, &cdf::ANGLE_DELTA[mode - V_PRED]); + } + enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL[mode]); + above_mode[c] = mode; + left_mode[r] = mode; for (plane, grid) in block.iter().enumerate() { let coding = planes[plane]; @@ -1628,6 +1680,7 @@ mod tests { luma: Vec::new(), u: dc(level), v: dc(-level), + ..BlockCoeffs::default() }) .collect(); let [luma, u, v] = decode_coeff_planes(&blocks); @@ -1674,6 +1727,100 @@ mod tests { /// The coefficient writer refuses what it cannot code rather than writing a /// stream a decoder walks off the end of. + /// The 32x32 block at the given block row and column of a decoded plane. + fn block_at(rows: &[Vec], block_row: usize, block_col: usize) -> Vec> { + rows[block_row * 32..block_row * 32 + 32] + .iter() + .map(|r| r[block_col * 32..block_col * 32 + 32].to_vec()) + .collect() + } + + /// A directional intra mode says where its prediction comes from, and with + /// no residual of its own a block must be exactly that prediction. The + /// top-left block is given a basis function that is flat across it and + /// falls down it; the block to its right is coded `H_PRED`, so every one of + /// its rows must be the constant its left neighbour's rightmost column + /// hands it, and the block below is coded `V_PRED`, so every one of its + /// columns must be the constant the row above it ends on — a single flat + /// value for the whole block. Nothing here pins the transform's shape or + /// the mode's CDF: a mode written with the wrong context, an angle delta + /// left out or a mode index off by one desyncs the symbol decoder and the + /// two empty blocks stop mirroring their neighbours. + #[test] + fn directional_modes_predict_from_the_neighbours_they_name() { + if !have_ffmpeg() { + eprintln!("SKIP directional_modes_predict_from_the_neighbours_they_name: no ffmpeg"); + return; + } + let gradient = BlockCoeffs { + luma: vec![Coeff { + row: 1, + col: 0, + level: 12, + }], + ..BlockCoeffs::default() + }; + let blocks = [ + gradient, + BlockCoeffs { + mode: H_PRED as u8, + ..BlockCoeffs::default() + }, + BlockCoeffs { + mode: V_PRED as u8, + ..BlockCoeffs::default() + }, + BlockCoeffs::default(), + ]; + let [luma, ..] = decode_coeff_planes(&blocks); + + let source = block_at(&luma, 0, 0); + for (r, row) in source.iter().enumerate() { + assert!( + row.iter().all(|&s| s == row[0]), + "the basis function at row 1, column 0 is flat across the block, row {r} is not: {row:?}" + ); + } + let profile: Vec = source.iter().map(|r| r[0]).collect(); + assert!( + profile.windows(2).all(|w| w[0] >= w[1]) && profile[0] > profile[31], + "the block must fall from top to bottom, got {profile:?}" + ); + + let horizontal = block_at(&luma, 0, 1); + for (r, row) in horizontal.iter().enumerate() { + assert!( + row.iter().all(|&s| s == profile[r]), + "an H_PRED block repeats the column to its left, row {r} should be all {} but is {row:?}", + profile[r] + ); + } + + let vertical = block_at(&luma, 1, 0); + let bottom = profile[31]; + assert!( + vertical.iter().flatten().all(|&s| s == bottom), + "a V_PRED block repeats the row above it, which is all {bottom} here, got rows \ + {:?} and {:?}", + vertical[0], + vertical[31] + ); + } + + /// The mode index is checked before anything is written. + #[test] + fn an_intra_mode_outside_the_key_frame_set_is_refused() { + let mut blocks = vec![BlockCoeffs::default(); 4]; + blocks[2].mode = INTRA_MODES as u8; + let message = split_coeff_key_frame_tile(16, 16, Q_IDX, &blocks) + .unwrap_err() + .to_string(); + assert!( + message.contains("intra mode 13"), + "the refusal must name the mode, got {message}" + ); + } + #[test] fn coefficients_the_writer_cannot_code_are_refused() { let empty = || vec![BlockCoeffs::default(); 4];