diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 6da8d8ca..ea09d254 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -196,3 +196,40 @@ pub const UV_MODE_NO_CFL: [[u16; 14]; 13] = [ 0, ], ]; + +// The coefficient CDFs below are the q-context-2 tables (`base_q_idx` 61..=120, +// spec 8.3.2's `Default_..._Cdf[2]`), which is the context the frames this +// crate writes today sit in. The other three sets arrive with the rate control +// that can leave that band. + +/// `Default_Txb_Skip_Cdf[2][4][0]` (spec 9.4): the all-zero flag of a 64x64 +/// luma transform block whose block size is the transform size, so its +/// context is 0. +pub const TXB_SKIP_LUMA_64: [u16; 3] = [28573, 32768, 0]; + +/// `Default_Txb_Skip_Cdf[2][3][7]` (spec 9.4): the all-zero flag of a 32x32 +/// chroma transform block that covers its whole plane block with no coded +/// neighbour, which is context 7. +pub const TXB_SKIP_CHROMA_32: [u16; 3] = [5124, 32768, 0]; + +/// `Default_Eob_Pt_1024_Cdf[2][0][0]` (spec 9.4): the end-of-block position +/// token of a luma transform block with 1024 coded coefficient positions — +/// what a 64x64 transform has, since only its top-left 32x32 is coded. +pub const EOB_PT_1024_LUMA: [u16; 12] = [ + 2784, 3831, 7041, 10521, 14847, 18844, 23155, 26682, 29229, 31045, 32768, 0, +]; + +/// `Default_Coeff_Base_Eob_Cdf[2][4][0][0]` (spec 9.4): the level of the last +/// coefficient of a 64x64 luma transform block, less one, when that +/// coefficient is the DC. +pub const COEFF_BASE_EOB_LUMA_64: [u16; 4] = [935, 3382, 32768, 0]; + +/// `Default_Dc_Sign_Cdf[2][0]` (spec 9.4): the sign of a luma DC coefficient, +/// indexed by whether the neighbouring blocks' DC signs lean negative (1), +/// positive (2) or neither (0). +pub const DC_SIGN_LUMA: [[u16; 3]; 3] = [[16000, 32768, 0], [13056, 32768, 0], [18816, 32768, 0]]; + +/// `Default_Coeff_Br_Cdf[2][3][0][0]` (spec 9.4): a base-range increment of a +/// coefficient whose neighbours are all zero, in a transform of 32x32 or +/// larger (the table is shared from 32x32 up). +pub const COEFF_BR_LUMA: [u16; 5] = [9040, 14786, 18360, 32768, 0]; diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 3f9e5f2c..62069434 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -21,6 +21,36 @@ const DC_PRED: usize = 0; /// Side of a superblock in 4x4 mode-info units when 128x128 superblocks are off. const SB_MI: u32 = 16; +/// `NUM_BASE_LEVELS` (spec 3): levels above this carry a base-range tail. +const NUM_BASE_LEVELS: i32 = 2; +/// `COEFF_BASE_RANGE` (spec 3): how far the base-range tail reaches before the +/// Golomb tail takes over. +const COEFF_BASE_RANGE: i32 = 12; +/// `BR_CDF_SIZE - 1` (spec 3): the largest increment one base-range symbol +/// carries. +const BR_STEP: i32 = 3; +/// The largest level the base and base-range syntax carry between them. +const MAX_LEVEL: i32 = NUM_BASE_LEVELS + COEFF_BASE_RANGE; +/// The q-context band whose default CDFs [`crate::cdf`] carries. +const Q_CTX_2: std::ops::RangeInclusive = 61..=120; + +/// Both writers here code whole superblocks only: a partial one forces the +/// partition syntax down the block tree, which they do not code yet. +fn check_superblocks(mi_cols: u32, mi_rows: u32) -> Result<()> { + if mi_cols == 0 + || mi_rows == 0 + || !mi_cols.is_multiple_of(SB_MI) + || !mi_rows.is_multiple_of(SB_MI) + { + return Err(Error::unsupported( + "AV1 tile", + "a key frame is written only for frames that are a whole number \ + of 64x64 superblocks", + )); + } + Ok(()) +} + /// Writes the payload of a one-tile key frame in which every superblock is a /// skipped 64x64 DC-predicted block. /// @@ -32,17 +62,7 @@ const SB_MI: u32 = 16; /// a partial superblock forces the partition syntax down the block tree, which /// this writer does not code yet. pub fn flat_key_frame_tile(mi_cols: u32, mi_rows: u32) -> Result> { - if mi_cols == 0 - || mi_rows == 0 - || !mi_cols.is_multiple_of(SB_MI) - || !mi_rows.is_multiple_of(SB_MI) - { - return Err(Error::unsupported( - "AV1 tile", - "a flat key frame is written only for frames that are a whole \ - number of 64x64 superblocks", - )); - } + check_superblocks(mi_cols, mi_rows)?; let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI); let mut enc = SymbolEncoder::new(); @@ -74,6 +94,114 @@ pub fn flat_key_frame_tile(mi_cols: u32, mi_rows: u32) -> Result> { Ok(enc.finish()) } +/// Writes the payload of a one-tile key frame in which every superblock is a +/// 64x64 DC-predicted block carrying one luma DC coefficient of `dc_level` and +/// no chroma residual. +/// +/// `dc_level` is a quantised level, not a sample value: the decoder multiplies +/// it by the frame's DC quantiser and inverse-transforms it over the whole +/// block, so the picture it makes is a flat grey some distance either side of +/// the mid-grey a zero level gives. `base_q_idx` is the frame header's, and +/// picks the coefficient CDFs. +/// +/// # Errors +/// Returns an error when the frame is not a whole number of 64x64 superblocks, +/// when `dc_level` is outside the range the base and base-range syntax carry +/// (`-14..=14` without zero), or when `base_q_idx` is outside the q-context +/// band whose default CDFs this crate carries. +pub fn dc_key_frame_tile( + mi_cols: u32, + mi_rows: u32, + base_q_idx: u8, + dc_level: i32, +) -> Result> { + check_superblocks(mi_cols, mi_rows)?; + if dc_level == 0 || dc_level.abs() > MAX_LEVEL { + return Err(Error::unsupported( + "AV1 tile", + "a DC-only key frame is written for levels -14..=14 without zero; \ + wider levels need the Golomb tail", + )); + } + if !Q_CTX_2.contains(&base_q_idx) { + return Err(Error::unsupported( + "AV1 tile", + "the coefficient CDFs of only one q context are known, so \ + base_q_idx must be 61..=120", + )); + } + let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI); + let level = dc_level.abs(); + let negative = dc_level < 0; + + let mut enc = SymbolEncoder::new(); + for r in 0..sb_rows { + for c in 0..sb_cols { + enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W64[0]); + + // Nothing is skipped now, so every neighbour's skip flag is 0 and + // the skip context stays 0 across the frame. + 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_NO_CFL[DC_PRED]); + + // coeffs() for the luma plane (spec 5.11.39). The block size is the + // transform size, so the all-zero flag's context is 0; a 64x64 + // transform codes only its top-left 32x32, which is the 1024- + // position end-of-block alphabet; an end-of-block of one is token 0 + // with no extra bits; and a 64x64 transform's type set is DCT-only, + // so no transform type is coded either. + enc.symbol_fixed(0, &cdf::TXB_SKIP_LUMA_64); + enc.symbol_fixed(0, &cdf::EOB_PT_1024_LUMA); + enc.symbol_fixed( + (level.min(NUM_BASE_LEVELS + 1) - 1) as usize, + &cdf::COEFF_BASE_EOB_LUMA_64, + ); + // The base-range tail. Every neighbour of the DC is zero, so its + // magnitude context is 0 throughout. + if level > NUM_BASE_LEVELS { + let mut left = level - (NUM_BASE_LEVELS + 1); + let mut sent = 0; + while sent < COEFF_BASE_RANGE { + let k = left.min(BR_STEP); + enc.symbol_fixed(k as usize, &cdf::COEFF_BR_LUMA); + if k < BR_STEP { + break; + } + left -= k; + sent += BR_STEP; + } + } + // The signs come after the levels, DC first (spec 5.11.39). + enc.symbol_fixed( + usize::from(negative), + &cdf::DC_SIGN_LUMA[dc_sign_ctx(r, c, negative)], + ); + + // Both chroma transform blocks are all-zero. Their planes carry no + // coded coefficient anywhere in the frame, so the neighbour halves + // of their context stay 0 and only the offset for a transform block + // that covers its whole plane block is left: context 7. + enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_32); + enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_32); + } + } + Ok(enc.finish()) +} + +/// `Dc_Sign_Contexts` (spec 8.3.2) for a frame every block of which carries a +/// DC of the same sign: each of the sixteen 4x4 units above and left of the +/// block votes its neighbour's sign, so the count is 0, +-16 or +-32 and the +/// table's three-way split reduces to "no coded neighbour, or which way they +/// lean". The left column is cleared at the start of every superblock row. +fn dc_sign_ctx(sb_row: u32, sb_col: u32, negative: bool) -> usize { + let neighbours = i32::from(sb_row > 0) + i32::from(sb_col > 0); + if neighbours == 0 { + return 0; + } + if negative { 1 } else { 2 } +} + #[cfg(test)] mod tests { use super::*; @@ -215,6 +343,120 @@ mod tests { } } + /// The one q-context whose CDFs this crate carries; the decoded table + /// below is pinned at this quantiser and nowhere else. + const Q_IDX: u8 = 100; + + /// Encode a 64x64 key frame carrying `dc_level` and hand back its planes. + fn decode_dc_frame(dc_level: i32) -> Vec { + let seq = sequence_64(); + let mut header = flat_key_frame(); + header.quantization.base_q_idx = Q_IDX; + let tile = dc_key_frame_tile(header.mi_cols, header.mi_rows, Q_IDX, dc_level).unwrap(); + let mut stream = temporal_delimiter(); + stream.extend_from_slice(&sequence_header_obu(&seq).unwrap()); + stream.extend_from_slice(&frame_obu(&seq, &header, &tile).unwrap()); + ffmpeg_decode(&stream, 64, 64) + } + + /// What a reference decoder makes of each DC level at `base_q_idx` 100: + /// the level times the DC quantiser, inverse-transformed over a whole + /// 64x64 block — which spreads one coefficient over 4096 samples, so a + /// level moves the picture by a sample or two, not by tens. These numbers + /// are pinned from the decoder rather than derived: this crate has no + /// inverse transform of its own yet to derive them with. What the test + /// asserts around them — flat planes, untouched chroma, monotone in the + /// level, and the sign going the right way — is derived, and it is what a + /// desync in the coefficient syntax breaks first. + const DECODED_AT_Q100: [(i32, u8); 28] = [ + (1, 128), + (2, 128), + (3, 129), + (4, 129), + (5, 129), + (6, 129), + (7, 129), + (8, 129), + (9, 130), + (10, 130), + (11, 130), + (12, 130), + (13, 130), + (14, 131), + (-1, 128), + (-2, 128), + (-3, 128), + (-4, 127), + (-5, 127), + (-6, 127), + (-7, 127), + (-8, 127), + (-9, 126), + (-10, 126), + (-11, 126), + (-12, 126), + (-13, 126), + (-14, 126), + ]; + + #[test] + fn a_dc_coefficient_moves_the_whole_block_off_mid_grey() { + if !have_ffmpeg() { + eprintln!("SKIP a_dc_coefficient_moves_the_whole_block_off_mid_grey: no ffmpeg"); + return; + } + let mut seen = Vec::new(); + for (level, want) in DECODED_AT_Q100 { + let planes = decode_dc_frame(level); + let (luma, chroma) = planes.split_at(64 * 64); + for (i, &sample) in luma.iter().enumerate() { + assert_eq!(sample, want, "luma sample {i} at dc level {level}"); + } + // The chroma transform blocks are all-zero, so both planes stay at + // the prediction. A desync in the luma coefficient syntax lands + // here first: the chroma flags are the next symbols after it. + for (i, &sample) in chroma.iter().enumerate() { + assert_eq!(sample, 128, "chroma sample {i} at dc level {level}"); + } + seen.push((level, want)); + } + // The picture moves off mid-grey the way the level says: up for a + // positive level, down for a negative one, and never back towards it + // as the level grows. + for w in seen.windows(2) { + let ((prev_level, prev), (level, value)) = (w[0], w[1]); + if prev_level.signum() != level.signum() { + continue; + } + if level > 0 { + assert!( + value >= prev, + "level {level} decoded below level {prev_level}" + ); + assert!(value >= 128, "a positive level darkened the picture"); + } else { + assert!( + value <= prev, + "level {level} decoded above level {prev_level}" + ); + assert!(value <= 128, "a negative level brightened the picture"); + } + } + assert!( + DECODED_AT_Q100.iter().any(|&(_, v)| v > 128) + && DECODED_AT_Q100.iter().any(|&(_, v)| v < 128), + "the pinned table has to move the picture both ways" + ); + } + + #[test] + fn dc_levels_the_base_syntax_cannot_carry_are_refused() { + assert!(dc_key_frame_tile(16, 16, 100, 0).is_err()); + assert!(dc_key_frame_tile(16, 16, 100, 15).is_err()); + assert!(dc_key_frame_tile(16, 16, 100, -15).is_err()); + assert!(dc_key_frame_tile(16, 16, 40, 3).is_err()); + } + #[test] fn partial_superblocks_are_refused() { assert!(flat_key_frame_tile(16, 20).is_err());