diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 1d34c35c..6dc2b60e 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -21,12 +21,13 @@ use ec_av1_syntax::{ }; use ec_core::{Error, Result}; +use crate::cdf; use crate::frame::frame_obu; -use crate::intra::{DC_PRED, NON_DIRECTIONAL}; +use crate::intra::{D67_PRED, DC_PRED, KEY_FRAME_MODES, V_PRED}; use crate::obu::temporal_delimiter; use crate::quant::ac_q; use crate::sequence::sequence_header_obu; -use crate::tile::{BlockCoeffs, Coeff, Superblock, sb_coeff_key_frame_tile}; +use crate::tile::{BlockCoeffs, Coeff, INTRA_MODE_CTX, Superblock, sb_coeff_key_frame_tile}; use crate::transform::{dequant_and_inverse, forward_and_quantize}; /// The side of the luma blocks this encoder codes, in samples. @@ -35,13 +36,14 @@ const BLOCK: usize = 32; /// How heavily the mode search weighs rate against squared error, in units of /// the quantizer's reconstruction step squared per bit. /// -/// Swept over three clips and two synthetic pictures at 0, 0.05, 0.1, 0.2, 0.4 -/// and 0.8 (`probe_lambda`, and the table in the lane report): 0 leaves half the -/// saving on the table on a picture that runs one way (-35.6% against -59.5% on -/// stripes), and everything from 0.1 up gives a little back on screen capture -/// (-10.7% against -11.5%) and on a hand-held clip. 0.05 is the best point on -/// every clip measured. -const LAMBDA_SCALE: f64 = 0.05; +/// Swept twice over three of his clips and three synthetic pictures +/// (`probe_lambda` and `probe_directional`, and the tables in the two lane +/// reports). The first sweep, before the mode symbol was costed, put the best +/// point at 0.05; costing it moved the point to 0.1, where the directional +/// modes stop costing rate on the pictures that do not want them (+0.05% on +/// screen capture against +0.19% at 0.05) without giving up any of what they +/// save on the pictures that do (-53.0% on a diagonal). +const LAMBDA_SCALE: f64 = 0.1; /// One 8-bit planar 4:2:0 picture. #[derive(Clone, Debug, PartialEq, Eq)] @@ -235,17 +237,26 @@ impl Plane<'_> { /// The reconstructed samples a block at `(x, y)` predicts from: the row /// above it, the column to its left and the sample between them, each /// missing where the block sits against an edge of the frame. + /// + /// `reach` says whether the samples above the block's right and below its + /// left are decoded, which is what a directional mode reads into; where + /// they are not, or where the frame ends first, the edge is shorter and + /// the predictor repeats its last sample, exactly as the decoder's clamp + /// to `aboveLimit` and `leftLimit` does (spec 7.11.2.2). fn edges( &self, x: usize, y: usize, side: usize, + reach: Reach, ) -> (Option>, Option>, Option) { + let across = (x + if reach.above_right { 2 * side } else { side }).min(self.width); + let down = (y + if reach.below_left { 2 * side } else { side }).min(self.height); let above = - (y > 0).then(|| self.reconstruction[(y - 1) * self.width + x..][..side].to_vec()); + (y > 0).then(|| self.reconstruction[(y - 1) * self.width + x..][..across - x].to_vec()); let left = (x > 0).then(|| { - (0..side) - .map(|i| self.reconstruction[(y + i) * self.width + x - 1]) + (y..down) + .map(|row| self.reconstruction[row * self.width + x - 1]) .collect::>() }); let corner = (x > 0 && y > 0).then(|| self.reconstruction[(y - 1) * self.width + x - 1]); @@ -255,16 +266,9 @@ impl Plane<'_> { /// Codes one block under one mode without committing it: hands back the /// levels, the block the decoder would reconstruct, the squared error /// against the source and an estimate of what the levels cost in bits. - fn trial( - &self, - x: usize, - y: usize, - side: usize, - mode: u8, - base_q_idx: u8, - deadzone: f64, - ) -> Trial { - let (above, left, corner) = self.edges(x, y, side); + fn trial(&self, at: At, mode: u8, base_q_idx: u8, deadzone: f64) -> Trial { + let At { x, y, side, reach } = at; + let (above, left, corner) = self.edges(x, y, side, reach); let mut prediction = vec![0u8; side * side]; crate::intra::predict( mode, @@ -323,33 +327,19 @@ impl Plane<'_> { } /// Codes one block under a fixed mode, committing it. - fn code_block( - &mut self, - x: usize, - y: usize, - side: usize, - mode: u8, - base_q_idx: u8, - deadzone: f64, - ) -> Vec { - let trial = self.trial(x, y, side, mode, base_q_idx, deadzone); - self.commit(x, y, side, &trial); - coeffs(&trial.levels, side) + fn code_block(&mut self, at: At, mode: u8, base_q_idx: u8, deadzone: f64) -> Vec { + let trial = self.trial(at, mode, base_q_idx, deadzone); + self.commit(at.x, at.y, at.side, &trial); + coeffs(&trial.levels, at.side) } /// Codes one block under every mode the search offers and commits the one /// whose squared error and estimated rate come out cheapest. - fn search_block( - &mut self, - x: usize, - y: usize, - side: usize, - search: &Search, - ) -> (Vec, u8) { + fn search_block(&mut self, at: At, search: &Search, mode_bits: &[f64; 13]) -> (Vec, u8) { let mut best: Option<(f64, u8, Trial)> = None; for &mode in search.modes { - let trial = self.trial(x, y, side, mode, search.base_q_idx, search.deadzone); - let cost = trial.sse + search.lambda * trial.bits; + let trial = self.trial(at, mode, search.base_q_idx, search.deadzone); + let cost = trial.sse + search.lambda * (trial.bits + mode_bits[usize::from(mode)]); if best .as_ref() .is_none_or(|(best_cost, _, _)| cost < *best_cost) @@ -358,11 +348,89 @@ impl Plane<'_> { } } let (_, mode, trial) = best.expect("the search offers at least one mode"); - self.commit(x, y, side, &trial); - (coeffs(&trial.levels, side), mode) + self.commit(at.x, at.y, at.side, &trial); + (coeffs(&trial.levels, at.side), mode) + } +} + +/// Where one block sits in its plane, and how far past its own edges its +/// prediction may read. +#[derive(Clone, Copy)] +struct At { + x: usize, + y: usize, + side: usize, + reach: Reach, +} + +/// Which of the samples past a block's own edges the decoder has decoded by +/// the time it predicts the block, and so which a directional mode may read. +/// +/// The decoder derives these from its `BlockDecoded` flags, which are cleared +/// per superblock (`clear_block_decoded_flags`, spec 7.4): the row above the +/// superblock counts as decoded out to the end of the *tile*, while the column +/// to its left counts only as far as the superblock's own height, and every +/// position inside the superblock starts undecoded. For the four 32x32 +/// quadrants of a 64x64 superblock that comes to: the samples above-right are +/// decoded for all but the last quadrant -- the one whose above-right lies +/// inside the superblock to the right, which raster order has not reached -- +/// and the samples below-left only for the first, the only one whose left +/// column continues into the superblock beside it. +#[derive(Clone, Copy)] +struct Reach { + above_right: bool, + below_left: bool, +} + +impl Reach { + /// The reach of the quadrant coded `quadrant`-th in a superblock, counting + /// in raster order among the four. + fn quadrant(quadrant: usize) -> Self { + Self { + above_right: quadrant != 3, + below_left: quadrant == 0, + } + } + + /// Neither, which is all a mode that reads no further than its own edges + /// needs. + fn none() -> Self { + Self { + above_right: false, + below_left: false, + } } } +/// What one symbol costs against a CDF, in bits. +fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 { + let low = if symbol == 0 { 0 } else { cdf[symbol - 1] }; + let probability = f64::from(cdf[symbol] - low) / 32768.0; + -probability.log2() +} + +/// What the tile writer spends to say a block is coded in each of the thirteen +/// modes, given the modes of the blocks above it and to its left: the luma mode +/// symbol against the CDF those two neighbours pick, the angle delta a +/// directional mode carries, and the chroma mode symbol, whose CDF the luma +/// mode itself indexes. +/// +/// Without this the search is blind to what a mode costs to name, which is not +/// a rounding error: a directional mode on a picture that does not run that way +/// is a rate loss the squared error never sees. +fn mode_bits(above_mode: u8, left_mode: u8) -> [f64; 13] { + let luma = &cdf::KF_Y_MODE[INTRA_MODE_CTX[usize::from(above_mode)]] + [INTRA_MODE_CTX[usize::from(left_mode)]]; + std::array::from_fn(|mode| { + let angle = if (usize::from(V_PRED)..=usize::from(D67_PRED)).contains(&mode) { + symbol_bits(&cdf::ANGLE_DELTA[mode - usize::from(V_PRED)], 3) + } else { + 0.0 + }; + symbol_bits(luma, mode) + angle + symbol_bits(&cdf::UV_MODE_CFL[mode], usize::from(DC_PRED)) + }) +} + /// What the luma mode search picks between, and under what terms. struct Search<'a> { base_q_idx: u8, @@ -405,15 +473,16 @@ fn coeffs(levels: &[i32], side: usize) -> Vec { /// when its planes are not 4:2:0 of that size, or when the tile writer refuses /// the quantizer index. pub fn encode_key_frame(picture: &Picture, base_q_idx: u8, deadzone: f64) -> Result { - encode_key_frame_with_modes(picture, base_q_idx, deadzone, &NON_DIRECTIONAL) + encode_key_frame_with_modes(picture, base_q_idx, deadzone, &KEY_FRAME_MODES) } /// Encodes one picture as a key frame, choosing each block's luma mode from /// `modes` alone. /// /// This is what an ablation measures against: `&[DC_PRED]` is the encoder -/// before the mode search, and [`NON_DIRECTIONAL`] is what -/// [`encode_key_frame`] uses. +/// before the mode search, [`crate::intra::NON_DIRECTIONAL`] is it before the +/// directional modes, and [`KEY_FRAME_MODES`] is what [`encode_key_frame`] +/// uses. /// /// # Errors /// The same as [`encode_key_frame`], and additionally when `modes` is empty or @@ -431,7 +500,7 @@ pub fn encode_key_frame_with_modes( "a mode search needs at least one mode to choose from", )); } - if let Some(bad) = modes.iter().find(|m| !NON_DIRECTIONAL.contains(m)) { + if let Some(bad) = modes.iter().find(|m| !KEY_FRAME_MODES.contains(m)) { return Err(Error::unsupported( "AV1 encode", format!("intra mode {bad} is not one the encoder predicts"), @@ -471,6 +540,12 @@ pub fn encode_key_frame_with_modes( }; let (cols, rows) = (picture.width / BLOCK, picture.height / BLOCK); + // The luma mode of the block above and of the block to the left, which is + // what picks the CDF the next block's mode is coded against -- the same + // bookkeeping the tile writer keeps, so that the search is costing the + // symbol the writer will actually write. + let mut above_mode = vec![DC_PRED; cols]; + let mut left_mode = vec![DC_PRED; rows]; let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); let mut superblocks = Vec::with_capacity(sb_cols * sb_rows); let mut modes = Vec::with_capacity(cols * rows); @@ -486,13 +561,44 @@ pub fn encode_key_frame_with_modes( continue; } let (x, y) = (col * BLOCK, row * BLOCK); - let (luma_coeffs, mode) = luma.search_block(x, y, BLOCK, &search); + let (luma_coeffs, mode) = luma.search_block( + At { + x, + y, + side: BLOCK, + reach: Reach::quadrant(quadrant), + }, + &search, + &mode_bits(above_mode[col], left_mode[row]), + ); + above_mode[col] = mode; + left_mode[row] = mode; modes.push(mode); blocks.push(BlockCoeffs { // Chroma is predicted DC because that is the mode the tile // writer codes for it. - u: chroma[0].code_block(x / 2, y / 2, BLOCK / 2, DC_PRED, base_q_idx, deadzone), - v: chroma[1].code_block(x / 2, y / 2, BLOCK / 2, DC_PRED, base_q_idx, deadzone), + u: chroma[0].code_block( + At { + x: x / 2, + y: y / 2, + side: BLOCK / 2, + reach: Reach::none(), + }, + DC_PRED, + base_q_idx, + deadzone, + ), + v: chroma[1].code_block( + At { + x: x / 2, + y: y / 2, + side: BLOCK / 2, + reach: Reach::none(), + }, + DC_PRED, + base_q_idx, + deadzone, + ), luma: luma_coeffs, mode, }); @@ -523,7 +629,7 @@ pub fn encode_key_frame_with_modes( #[cfg(test)] mod tests { use super::*; - use crate::intra::{H_PRED, V_PRED}; + use crate::intra::{D45_PRED, D135_PRED, H_PRED, KEY_FRAME_MODES, NON_DIRECTIONAL, V_PRED}; use std::io::Write; use std::process::{Command, Stdio}; @@ -718,6 +824,21 @@ mod tests { picture } + /// Where two planes first disagree, and by how much: a mismatch reported as + /// a position says which block and which sample of it went wrong, which a + /// pair of thousand-sample arrays does not. + fn first_difference(ours: &[u8], theirs: &[u8], width: usize) -> Option { + let i = ours.iter().zip(theirs).position(|(a, b)| a != b)?; + let differ = ours.iter().zip(theirs).filter(|(a, b)| a != b).count(); + Some(format!( + "{differ} samples differ, first at ({}, {}): ours {} theirs {}", + i % width, + i / width, + ours[i], + theirs[i] + )) + } + /// Every mode the encoder offers has to predict what the decoder predicts, /// not just the ones a particular picture happens to choose: each is forced /// over a whole picture and the reconstruction gated against ffmpeg. @@ -727,17 +848,30 @@ mod tests { eprintln!("SKIP every_mode_decodes_to_what_the_encoder_predicted: no ffmpeg"); return; } - let picture = test_card(128, 96); - for mode in NON_DIRECTIONAL { - let encoded = encode_key_frame_with_modes(&picture, 100, 0.5, &[mode]).unwrap(); - let decoded = ffmpeg_decode(&encoded.stream, 128, 96); - assert_eq!(decoded.y, encoded.reconstruction.y, "mode {mode}: luma"); - assert_eq!(decoded.u, encoded.reconstruction.u, "mode {mode}: U"); - assert_eq!(decoded.v, encoded.reconstruction.v, "mode {mode}: V"); - assert!( - encoded.modes.iter().all(|&m| m == mode), - "mode {mode}: the encoder coded something else" - ); + // 128 wide is a whole number of superblocks and 160 is not: the last + // superblock of a 160-wide row is half a one, whose blocks have no + // above-right samples inside the frame at all. + for (width, height) in [(128, 96), (160, 96)] { + let picture = test_card(width, height); + for mode in KEY_FRAME_MODES { + let encoded = encode_key_frame_with_modes(&picture, 100, 0.5, &[mode]).unwrap(); + let decoded = ffmpeg_decode(&encoded.stream, width, height); + for (plane, ours, theirs, stride) in [ + ("luma", &encoded.reconstruction.y, &decoded.y, width), + ("U", &encoded.reconstruction.u, &decoded.u, width / 2), + ("V", &encoded.reconstruction.v, &decoded.v, width / 2), + ] { + assert!( + first_difference(ours, theirs, stride).is_none(), + "{width}x{height} mode {mode}, {plane}: {}", + first_difference(ours, theirs, stride).unwrap() + ); + } + assert!( + encoded.modes.iter().all(|&m| m == mode), + "mode {mode}: the encoder coded something else" + ); + } } } @@ -749,7 +883,13 @@ mod tests { fn the_search_picks_the_direction_the_picture_runs() { for (vertical, want) in [(true, V_PRED), (false, H_PRED)] { let picture = stripes(128, 96, vertical); - let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); + // Only the pair, because on stripes a third mode predicts exactly + // what the right one of the pair does -- PAETH reads the corner and + // the left column, which a striped picture makes equal -- and the + // tie then goes to whichever is cheaper to name, which is not what + // this gate is about. + let encoded = + encode_key_frame_with_modes(&picture, 100, 0.5, &[V_PRED, H_PRED]).unwrap(); // The first block of the picture has neither neighbour, so it // cannot tell the modes apart; every other one can. let picked = encoded.modes[1..].iter().filter(|&&m| m == want).count(); @@ -832,16 +972,145 @@ mod tests { } } + /// The pictures a sweep is measured over: two synthetic ones, plus a frame + /// from each clip named in `EC_AV1_CLIPS` as `path@skip`, colon separated. + fn sweep_pictures() -> Vec<(String, Picture)> { + let mut pictures = vec![ + ("test card".to_string(), test_card(160, 96)), + ("stripes".to_string(), stripes(160, 96, true)), + ("diagonal".to_string(), diagonal(160, 96, true)), + ]; + if let Ok(clips) = std::env::var("EC_AV1_CLIPS") { + for entry in clips.split(':').filter(|e| !e.is_empty()) { + let (path, skip) = entry.split_once('@').unwrap_or((entry, "0")); + let name = path.rsplit('/').next().unwrap_or(path).to_string(); + pictures.push((name, clip_frame(path, skip, 640, 352))); + } + } + pictures + } + + /// What the six directional modes are worth, per picture, at whatever + /// `LAMBDA_SCALE` currently is: the rate they save over the seven that read + /// no further, and how often the search picks one. + #[test] + #[ignore = "a sweep, not a gate"] + fn probe_directional() { + for (name, picture) in sweep_pictures() { + let dc = ladder(&picture, &[DC_PRED]); + let flat = ladder(&picture, &NON_DIRECTIONAL); + let all = ladder(&picture, &KEY_FRAME_MODES); + let encoded = encode_key_frame(&picture, 90, 0.5).unwrap(); + let directional = encoded + .modes + .iter() + .filter(|&&m| (3..=8).contains(&m)) + .count(); + // A picture whose ladders sit at different fidelities altogether + // has no BD-rate to report -- see the flat-sample-window class. + let overlap = |a: &[(f64, f64)], b: &[(f64, f64)]| { + a[0].0.max(b[0].0) < a[a.len() - 1].0.min(b[b.len() - 1].0) + }; + let against = |a: &[(f64, f64)], b: &[(f64, f64)]| { + if overlap(a, b) { + format!("{:+.2}%", bd_rate(a, b) * 100.0) + } else { + "no overlap".to_string() + } + }; + println!( + "{name}: {} against the seven, {} against DC alone, {directional} of {} blocks directional", + against(&flat, &all), + against(&dc, &all), + encoded.modes.len(), + ); + } + } + + /// A picture whose stripes run along a diagonal, one way or the other. + /// This is to the directional modes what [`stripes`] is to the vertical and + /// horizontal pair: a predictor that walked the edge in the wrong direction + /// would answer the two pictures the same way round. + fn diagonal(width: usize, height: usize, down_right: bool) -> Picture { + let mut picture = Picture::grey(width, height); + for y in 0..height { + for x in 0..width { + let along = if down_right { x + height - y } else { x + y }; + picture.y[y * width + x] = if (along / 6) % 2 == 0 { 40 } else { 210 }; + } + } + picture + } + + /// The mode picked by the most blocks of a picture, ignoring the first + /// block, which has no neighbours to tell the modes apart with. + fn favourite_mode(picture: &Picture) -> (u8, usize, usize) { + let encoded = encode_key_frame(picture, 100, 0.5).unwrap(); + let blocks = &encoded.modes[1..]; + let mut counts = [0usize; 13]; + for &mode in blocks { + counts[usize::from(mode)] += 1; + } + let (mode, count) = counts + .iter() + .enumerate() + .max_by_key(|&(_, count)| *count) + .expect("thirteen modes"); + (mode as u8, *count, blocks.len()) + } + + /// The search has to follow a diagonal the way it runs: stripes down and to + /// the right are cheapest predicted at 135 degrees, stripes down and to the + /// left at 45. A walk that stepped the wrong way along the edge, or read + /// the above row where it should read the left column, would swap these. + #[test] + fn the_search_picks_the_diagonal_the_picture_runs() { + for (down_right, want) in [(true, D135_PRED), (false, D45_PRED)] { + let picture = diagonal(160, 96, down_right); + let (mode, count, blocks) = favourite_mode(&picture); + assert_eq!( + mode, want, + "down_right={down_right}: {count} of {blocks} blocks picked mode {mode}" + ); + } + } + + /// What the directional modes are for: a picture that runs along a diagonal + /// costs far less to code with them than the seven that read only their own + /// edges can manage, and a picture that runs no particular way costs no + /// more. The second half is the one that bites: a mode set the search + /// cannot price is a mode set that loses rate on content that does not want + /// it, which is what costing the mode symbol is for. + #[test] + fn the_diagonals_beat_the_modes_that_read_no_further() { + for (name, picture, want) in [ + ("down-right", diagonal(160, 96, true), -0.20), + ("down-left", diagonal(160, 96, false), -0.20), + ("test card", test_card(160, 96), 0.01), + ] { + let saved = bd_rate( + &ladder(&picture, &NON_DIRECTIONAL), + &ladder(&picture, &KEY_FRAME_MODES), + ); + assert!( + saved < want, + "{name}: the directional modes saved {:.1}% of the rate, wanted better than {:.1}%", + saved * 100.0, + -want * 100.0 + ); + } + } + /// A mode the encoder cannot predict must be refused rather than coded as /// something else, and a search with nothing to choose from likewise. #[test] fn a_mode_the_encoder_cannot_predict_is_refused() { let picture = test_card(64, 64); - let message = encode_key_frame_with_modes(&picture, 100, 0.5, &[3]) + let message = encode_key_frame_with_modes(&picture, 100, 0.5, &[13]) .unwrap_err() .to_string(); assert!( - message.contains("intra mode 3"), + message.contains("intra mode 13"), "the refusal must name the mode, got {message}" ); assert!(encode_key_frame_with_modes(&picture, 100, 0.5, &[]).is_err()); diff --git a/crates/ec-av1/src/intra.rs b/crates/ec-av1/src/intra.rs index 494db082..f3056b89 100644 --- a/crates/ec-av1/src/intra.rs +++ b/crates/ec-av1/src/intra.rs @@ -1,9 +1,12 @@ //! Intra prediction, the half of spec 7.11.2 a key frame needs. //! -//! The seven modes here are the ones that read no further than the row above -//! and the column to the left of the block: the eight directional modes reach -//! past a block's own width into samples whose availability the decoder tracks -//! in its own bookkeeping, and are not written yet. +//! All thirteen key-frame intra modes: the seven that read no further than the +//! row above and the column to the left of the block, and the six that steer +//! along an angle and reach past the block's own width into the samples above +//! its right and below its left. Whether those further samples are decoded is +//! not this module's business -- the caller passes the samples it has, and the +//! edge is extended by repeating the last one, which is what the decoder does +//! with its own `BlockDecoded` bookkeeping (spec 7.11.2.2). /// The average of the neighbours (spec 7.11.2.5). pub const DC_PRED: u8 = 0; @@ -19,8 +22,40 @@ pub const SMOOTH_V_PRED: u8 = 10; pub const SMOOTH_H_PRED: u8 = 11; /// Whichever of above, left and the corner is nearest their gradient. pub const PAETH_PRED: u8 = 12; +/// Down-left along 45 degrees. +pub const D45_PRED: u8 = 3; +/// Down-right along 135 degrees. +pub const D135_PRED: u8 = 4; +/// Down-right, steeper: 113 degrees. +pub const D113_PRED: u8 = 5; +/// Down-right, shallower: 157 degrees. +pub const D157_PRED: u8 = 6; +/// Up-right along 203 degrees. +pub const D203_PRED: u8 = 7; +/// Down-left, steeper: 67 degrees. +pub const D67_PRED: u8 = 8; -/// The modes this module predicts, in the order a search should try them. +/// Every mode a key frame's luma block can be coded with, in the order a +/// search should try them: the cheap ones first, so an early one is likely to +/// be the one that survives. +pub const KEY_FRAME_MODES: [u8; 13] = [ + DC_PRED, + V_PRED, + H_PRED, + SMOOTH_PRED, + SMOOTH_V_PRED, + SMOOTH_H_PRED, + PAETH_PRED, + D45_PRED, + D67_PRED, + D113_PRED, + D135_PRED, + D157_PRED, + D203_PRED, +]; + +/// The modes that read no further than the row above and the column to the +/// left, in the order a search should try them. pub const NON_DIRECTIONAL: [u8; 7] = [ DC_PRED, V_PRED, @@ -51,26 +86,75 @@ fn round2(value: i32, shift: u32) -> i32 { (value + (1 << (shift - 1))) >> shift } +/// `Dr_Intra_Derivative` (spec 9.3): how far along the edge one row of the +/// block moves, in 64ths of a sample, for the angles a delta can reach. +fn dr_intra_derivative(angle: u16) -> i32 { + match angle { + 3 => 1023, + 6 => 547, + 9 => 372, + 14 => 273, + 17 => 215, + 20 => 178, + 23 => 151, + 26 => 132, + 29 => 116, + 32 => 102, + 36 => 90, + 39 => 80, + 42 => 71, + 45 => 64, + 48 => 57, + 51 => 51, + 54 => 45, + 58 => 40, + 61 => 35, + 64 => 31, + 67 => 27, + 70 => 23, + 73 => 19, + 76 => 15, + 81 => 11, + 84 => 7, + 87 => 3, + other => panic!("no derivative is tabulated for {other} degrees"), + } +} + +/// `Mode_To_Angle` (spec 9.3), zero for the modes that have no angle. +const MODE_TO_ANGLE: [u16; 13] = [0, 90, 180, 45, 135, 113, 157, 203, 67, 0, 0, 0, 0]; + /// The edges a block predicts from, as the decoder builds them (spec 7.11.2.2): -/// a side that does not exist is filled from the side that does, and the corner -/// falls back the same way. +/// a side that does not exist is filled from the side that does, a side that +/// runs out is extended by repeating its last sample, and the corner falls back +/// the same way. +/// +/// Both arrays hold the corner first, so the spec's index `-1` is index 0 here +/// and they run out to the spec's `w + h - 1`. struct Edges { above: Vec, left: Vec, - corner: i32, } impl Edges { fn build(above: Option<&[u8]>, left: Option<&[u8]>, corner: Option, side: usize) -> Self { + // A directional mode reads out to w + h - 1 either way. + let want = side * 2; + let extend = |samples: &[u8]| { + let mut row: Vec = samples.iter().map(|&s| i32::from(s)).collect(); + let last = *row.last().expect("an edge that exists has samples"); + row.resize(want, last); + row + }; let above_row = match (above, left) { - (Some(a), _) => a.iter().map(|&s| i32::from(s)).collect(), - (None, Some(l)) => vec![i32::from(l[0]); side], - (None, None) => vec![127; side], + (Some(a), _) => extend(a), + (None, Some(l)) => vec![i32::from(l[0]); want], + (None, None) => vec![127; want], }; let left_col = match (left, above) { - (Some(l), _) => l.iter().map(|&s| i32::from(s)).collect(), - (None, Some(a)) => vec![i32::from(a[0]); side], - (None, None) => vec![129; side], + (Some(l), _) => extend(l), + (None, Some(a)) => vec![i32::from(a[0]); want], + (None, None) => vec![129; want], }; let corner = match (corner, above, left) { (Some(c), Some(_), Some(_)) => i32::from(c), @@ -78,20 +162,38 @@ impl Edges { (_, None, Some(l)) => i32::from(l[0]), (_, None, None) => 128, }; + let with_corner = |edge: Vec| { + let mut v = Vec::with_capacity(want + 1); + v.push(corner); + v.extend(edge); + v + }; Self { - above: above_row, - left: left_col, - corner, + above: with_corner(above_row), + left: with_corner(left_col), } } + + /// `AboveRow[i]`, where `i` may be the spec's `-1`. + fn above(&self, i: i32) -> i32 { + self.above[usize::try_from(i + 1).expect("an edge is read no further back than the corner")] + } + + /// `LeftCol[i]`, where `i` may be the spec's `-1`. + fn left(&self, i: i32) -> i32 { + self.left[usize::try_from(i + 1).expect("an edge is read no further back than the corner")] + } } /// Predicts one square block into `dst`, row-major and `side * side` long. /// /// `above` is the reconstructed row above the block and `left` its -/// reconstructed left column, each `side` samples long, and `corner` the -/// sample diagonally above-left; `None` where the block sits against an edge -/// of the frame. +/// reconstructed left column, each at least `side` samples long, and `corner` +/// the sample diagonally above-left; `None` where the block sits against an +/// edge of the frame. A directional mode reads out to `2 * side`: pass the +/// samples above-right and below-left where the decoder has them decoded, and +/// the edge is extended by repetition where it does not, which is what the +/// decoder's own clamp to `aboveLimit` and `leftLimit` comes to. /// /// # Panics /// Panics on a mode this module does not predict, or when `dst` is not @@ -106,32 +208,41 @@ pub fn predict( ) { assert_eq!(dst.len(), side * side, "the destination is the block"); let edges = Edges::build(above, left, corner, side); - let (a, l) = (&edges.above, &edges.left); + let angle = MODE_TO_ANGLE[usize::from(mode)]; + if matches!( + mode, + D45_PRED | D67_PRED | D113_PRED | D135_PRED | D157_PRED | D203_PRED + ) { + directional(angle, &edges, side, dst); + return; + } let weights = &SM_WEIGHTS[side..side * 2]; for row in 0..side { for col in 0..side { + let (r, c) = (row as i32, col as i32); + let last = side as i32 - 1; let value = match mode { - DC_PRED => dc(above, left), - V_PRED => a[col], - H_PRED => l[row], + DC_PRED => dc(above, left, side), + V_PRED => edges.above(c), + H_PRED => edges.left(r), SMOOTH_PRED => round2( - i32::from(weights[row]) * a[col] - + (256 - i32::from(weights[row])) * l[side - 1] - + i32::from(weights[col]) * l[row] - + (256 - i32::from(weights[col])) * a[side - 1], + i32::from(weights[row]) * edges.above(c) + + (256 - i32::from(weights[row])) * edges.left(last) + + i32::from(weights[col]) * edges.left(r) + + (256 - i32::from(weights[col])) * edges.above(last), 9, ), SMOOTH_V_PRED => round2( - i32::from(weights[row]) * a[col] - + (256 - i32::from(weights[row])) * l[side - 1], + i32::from(weights[row]) * edges.above(c) + + (256 - i32::from(weights[row])) * edges.left(last), 8, ), SMOOTH_H_PRED => round2( - i32::from(weights[col]) * l[row] - + (256 - i32::from(weights[col])) * a[side - 1], + i32::from(weights[col]) * edges.left(r) + + (256 - i32::from(weights[col])) * edges.above(last), 8, ), - PAETH_PRED => paeth(a[col], l[row], edges.corner), + PAETH_PRED => paeth(edges.above(c), edges.left(r), edges.above(-1)), other => panic!("intra mode {other} is not one this module predicts"), }; dst[row * side + col] = value.clamp(0, 255) as u8; @@ -139,8 +250,66 @@ pub fn predict( } } +/// Directional intra prediction (spec 7.11.2.4), with the intra edge filter off +/// -- which is what our sequence header says -- so nothing here upsamples. +fn directional(angle: u16, edges: &Edges, side: usize, dst: &mut [u8]) { + let n = side as i32; + // The furthest sample the walk can reach, past which the edge is flat. + let max_base = n + n - 1; + let blend = |edge: &dyn Fn(i32) -> i32, base: i32, shift: i32| { + round2(edge(base) * (32 - shift) + edge(base + 1) * shift, 5) + }; + let above = |i: i32| edges.above(i); + let left = |i: i32| edges.left(i); + for row in 0..n { + for col in 0..n { + let value = if angle < 90 { + let dx = dr_intra_derivative(angle); + let idx = (row + 1) * dx; + let base = (idx >> 6) + col; + let shift = (idx >> 1) & 0x1F; + if base < max_base { + blend(&above, base, shift) + } else { + above(max_base) + } + } else if angle > 180 { + let dy = dr_intra_derivative(270 - angle); + let idx = (col + 1) * dy; + let base = (idx >> 6) + row; + let shift = (idx >> 1) & 0x1F; + if base < max_base { + blend(&left, base, shift) + } else { + left(max_base) + } + } else { + // The two zones meet here: a ray that leaves through the row + // above is read there, and one that leaves through the column + // to the left is read there instead. + let dx = dr_intra_derivative(180 - angle); + let idx = (col << 6) - (row + 1) * dx; + let base = idx >> 6; + if base >= -1 { + blend(&above, base, (idx >> 1) & 0x1F) + } else { + let dy = dr_intra_derivative(angle - 90); + let idx = (row << 6) - (col + 1) * dy; + blend(&left, idx >> 6, (idx >> 1) & 0x1F) + } + }; + dst[(row * n + col) as usize] = value.clamp(0, 255) as u8; + } + } +} + /// `dc_predict` (spec 7.11.2.5): the average of whichever neighbours exist. -fn dc(above: Option<&[u8]>, left: Option<&[u8]>) -> i32 { +/// +/// Only the block's own `side` samples of each edge count: what a directional +/// mode reaches past them is no part of the average. +fn dc(above: Option<&[u8]>, left: Option<&[u8]>, side: usize) -> i32 { + let above = above.map(|a| &a[..side.min(a.len())]); + let left = left.map(|l| &l[..side.min(l.len())]); let average = |samples: &[u8]| { let sum: u32 = samples.iter().map(|&s| u32::from(s)).sum(); ((sum + (samples.len() as u32 >> 1)) / samples.len() as u32) as i32 diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 8f9c0d45..d83cb0d1 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -51,7 +51,7 @@ 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]; +pub(crate) 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. diff --git a/lanes/av1-directional-r1.report.md b/lanes/av1-directional-r1.report.md new file mode 100644 index 00000000..8bf29353 --- /dev/null +++ b/lanes/av1-directional-r1.report.md @@ -0,0 +1,85 @@ +# lane-av1-directional r1 — the six directional intra modes + +Adds AV1's six directional intra predictors (D45, D67, D113, D135, D157, D203) +to `ec-av1`, with the availability derivation the decoder actually uses, and +puts the cost of *naming* a mode into the mode search — which was the honest +finding left open by `av1-predict-r1`. + +## What landed + +- `crates/ec-av1/src/intra.rs`: `directional()` per spec 7.11.2.4, all three + zones (`pAngle < 90` walks the above row, `> 180` walks the left column, + `90..180` walks the above row while `base >= -1` and falls to the left + column below that), `Dr_Intra_Derivative` and `Mode_To_Angle` tabulated. + Edges now carry the spec's index −1 (the corner) at slot 0 and are extended + to `2 * side` by repeating the last sample. `enable_intra_edge_filter` is + false in our sequence header, so no filtering and no upsampling apply. +- `crates/ec-av1/src/encode.rs`: `Reach` — which of the above-right and + below-left samples a block may read, derived from `clear_block_decoded_flags` + (spec 7.4). For the four 32x32 quadrants of a 64x64 superblock: above-right + available for quadrants 0, 1, 2 (not 3), below-left only for quadrant 0. +- `mode_bits()` — what the tile writer spends to name each of the thirteen + modes, scored through the writer's own CDFs: the KF_Y_MODE symbol under the + context the two neighbouring modes pick, the ANGLE_DELTA zero symbol a + directional mode carries, and the UV_MODE_CFL DC symbol, whose row the luma + mode indexes. The encoder tracks neighbour modes exactly as the writer does. +- `LAMBDA_SCALE` re-swept with the mode cost in: 0.05 → 0.1. + +## Gates + +| Gate | Result | +|---|---| +| `ffmpeg` decodes our stream to the encoder's own prediction, all 13 modes, 128x96 and 160x96 | bit-exact, 0 differing samples | +| the search picks the diagonal the picture runs (D135 / D45 test cards) | pass | +| directional modes cost no more than 1% rate on a picture that does not want them | +0.41% | +| full suite `cargo test --release -p ec-av1` | 75 passed, 0 failed, 2 ignored | +| `cargo clippy --release -p ec-av1 --all-targets` | 0 warnings | + +The 160x96 width is in the shape gate on purpose: it makes the last superblock +of a row partial, which is where a wrong above-right derivation shows up. + +## Mutations killed + +| Mutation | Caught by | +|---|---| +| above-right always available | ffmpeg mismatch, mode 3, first at (63, 64) | +| below-left always available | ffmpeg mismatch, mode 7 | +| 45° derivative off by one (`1023` → `1022`) | ffmpeg mismatch, mode 3 | +| zone 2 reads the left column instead of the above row | ffmpeg mismatch, modes 4/5/6 | + +Restore run after the four: 12 passed, 0 failed. + +## His data — this branch against `main`, same frame each row + +One key frame per clip, 640x352 crop, `base_q_idx` 100. The skip is recorded +here because `av1-predict-r1` did not record its own, and its screen-capture +row (4808 B / 46.06 dB) is a *different frame* of the same file, not a +regression — that clip is 6.5 s long and its frames differ by 8 dB across it. + +| clip | skip | main bytes | main PSNR | this branch | PSNR | Δ rate | +|---|---|---|---|---|---|---| +| ReelC (1080p film) | 1920 s | 19125 | 40.61 dB | 19134 | 40.59 dB | +0.05% | +| OBS screen capture | 5 s | 9144 | 43.52 dB | 9112 | 43.52 dB | −0.35% | +| reela | 5 s | 7535 | 44.76 dB | 7518 | 44.76 dB | −0.23% | + +On synthetic pictures that genuinely run diagonally the same code is worth +−53.0% rate. The difference is block size: at 32x32, directional modes fire on +8 of 220 blocks in the film frame and 5 of 220 in the screen capture. + +## Dispositions + +- **accepted** — directional modes move real 640x352 frames by ≤0.35%. They + are correct, bit-exact and default-on; the win they are capable of waits on + block sizes below 32x32 and on the partition decision, which is its own lane. +- **accepted** — ReelC is +0.05% rate / −0.02 dB, inside the noise of a single + frame and the price of the lambda move that pays for the other two clips. +- **fix-now, done in this lane** — the search's blindness to mode signalling + cost, left open by `av1-predict-r1`. Costed through the writer's CDFs above. + +## Not in this lane + +Angle deltas other than zero; block sizes other than 32x32 and the partition +decision; a coefficient rate term scored through the writer's CDFs (the mode +half is now done, the level half is still the magnitude estimate); transform +types beyond DCT_DCT; CDF adaptation; inter frames; pictures whose dimensions +are not a multiple of 32.