//! lane-wedge r3: `COMPOUND_WEDGE` mask codebook -- libaom //! `av1/common/reconinter.c`'s `init_wedge_master_masks`/`init_wedge_masks`/ //! `get_wedge_mask_inplace`, ported for EVERY bsize libaom gives a wedge //! codebook (`av1_wedge_params_lookup`, reconinter.c:236-267): the three //! square sizes 8x8/16x16/32x32 (`wedge_codebook_16_heqw`) and the six //! rectangular ones 8x16/16x32/8x32 (`wedge_codebook_16_hgtw`, h > w) and //! 16x8/32x16/32x8 (`wedge_codebook_16_hltw`, h < w), each with its own //! `wedge_signflip_lookup` row (reconinter.c:160-183). //! //! lane-inter16ab r5: masks are stored PADDED to a `side x side` plane //! (`side = max(bw, bh)`, row stride `side`), because `decode.rs` predicts //! a rectangular strip into a square `side x side` buffer and writes only //! its true `write_w x write_h` footprint -- so a padded mask drops //! straight into the existing square blend call with no stride plumbing. //! Rows/columns outside `bw x bh` are zero and never reach the frame. //! //! VERIFICATION (class shared-oracle-blindness): `lanes/wedge_libaom.expected.txt` //! is generated by a C harness LINKED AGAINST libaom itself //! (`~/.cache/inter16ab-tmp/wedge_h.c`, `av1_init_wedge_masks()` then //! `av1_get_contiguous_soft_mask(idx, sign, bsize)`) -- a genuinely foreign //! implementation, not a transcription. Its 96 square-size lines are //! byte-identical to the older independent transcription //! `lanes/wedge_dump.expected.txt` (`lanes/wedge_dump.c`), so the two //! oracles agree where they overlap. `checksum()` below reproduces the //! harness's per-block checksum formula (true `bw x bh` window, padded //! stride skipped); `wedge_codebook_matches_libaom_dump` asserts all //! 9 x 2 x 16 = 288 entries. const MASK_MASTER_SIZE: usize = 64; const WEDGE_WEIGHT_BITS: u32 = 6; pub const MAX_WEDGE_TYPES: usize = 16; #[derive(Clone, Copy)] enum Dir { Horizontal = 0, Vertical = 1, Oblique27 = 2, Oblique63 = 3, Oblique117 = 4, Oblique153 = 5, } const N_DIR: usize = 6; const WEDGE_MASTER_OBLIQUE_ODD: [u8; MASK_MASTER_SIZE] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 6, 18, 37, 53, 60, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, ]; const WEDGE_MASTER_OBLIQUE_EVEN: [u8; MASK_MASTER_SIZE] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 11, 27, 46, 58, 62, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, ]; const WEDGE_MASTER_VERTICAL: [u8; MASK_MASTER_SIZE] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 7, 21, 43, 57, 62, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, ]; /// `wedge_codebook_16_heqw` (reconinter.c:225) -- (direction, x_offset, /// y_offset); the codebook for the square sizes (h == w). const HEQW: [(Dir, i32, i32); MAX_WEDGE_TYPES] = [ (Dir::Oblique27, 4, 4), (Dir::Oblique63, 4, 4), (Dir::Oblique117, 4, 4), (Dir::Oblique153, 4, 4), (Dir::Horizontal, 4, 2), (Dir::Horizontal, 4, 6), (Dir::Vertical, 2, 4), (Dir::Vertical, 6, 4), (Dir::Oblique27, 4, 2), (Dir::Oblique27, 4, 6), (Dir::Oblique153, 4, 2), (Dir::Oblique153, 4, 6), (Dir::Oblique63, 2, 4), (Dir::Oblique63, 6, 4), (Dir::Oblique117, 2, 4), (Dir::Oblique117, 6, 4), ]; /// `wedge_codebook_16_hgtw` (reconinter.c:203) -- h > w (8x16/16x32/8x32). /// Differs from `heqw` only in entries 4..7: three HORIZONTAL splits plus a /// single centred VERTICAL one. const HGTW: [(Dir, i32, i32); MAX_WEDGE_TYPES] = [ (Dir::Oblique27, 4, 4), (Dir::Oblique63, 4, 4), (Dir::Oblique117, 4, 4), (Dir::Oblique153, 4, 4), (Dir::Horizontal, 4, 2), (Dir::Horizontal, 4, 4), (Dir::Horizontal, 4, 6), (Dir::Vertical, 4, 4), (Dir::Oblique27, 4, 2), (Dir::Oblique27, 4, 6), (Dir::Oblique153, 4, 2), (Dir::Oblique153, 4, 6), (Dir::Oblique63, 2, 4), (Dir::Oblique63, 6, 4), (Dir::Oblique117, 2, 4), (Dir::Oblique117, 6, 4), ]; /// `wedge_codebook_16_hltw` (reconinter.c:214) -- h < w (16x8/32x16/32x8): /// the transpose of `hgtw`'s 4..7 (three VERTICAL splits, one centred /// HORIZONTAL). const HLTW: [(Dir, i32, i32); MAX_WEDGE_TYPES] = [ (Dir::Oblique27, 4, 4), (Dir::Oblique63, 4, 4), (Dir::Oblique117, 4, 4), (Dir::Oblique153, 4, 4), (Dir::Vertical, 2, 4), (Dir::Vertical, 4, 4), (Dir::Vertical, 6, 4), (Dir::Horizontal, 4, 4), (Dir::Oblique27, 4, 2), (Dir::Oblique27, 4, 6), (Dir::Oblique153, 4, 2), (Dir::Oblique153, 4, 6), (Dir::Oblique63, 2, 4), (Dir::Oblique63, 6, 4), (Dir::Oblique117, 2, 4), (Dir::Oblique117, 6, 4), ]; /// `wedge_signflip_lookup` (reconinter.c:160-183), one row per bsize. /// `BLOCK_8X8`/`16X16`/`32X32` share this row. const SF_SQUARE: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1]; /// `BLOCK_8X16`/`16X8`/`16X32`/`32X16` share this row (index 4 flips to 0). const SF_RECT: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1]; /// `BLOCK_8X32` (reconinter.c:173) -- index 8 also 0. const SF_8X32: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1]; /// `BLOCK_32X8` (reconinter.c:174) -- index 12 also 0. const SF_32X8: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1]; /// `av1_wedge_params_lookup[bsize]`'s `(codebook, signflip)` pair for a /// block footprint, `None` where `wedge_types == 0`. fn wedge_params(bw: usize, bh: usize) -> Option<(&'static [(Dir, i32, i32); MAX_WEDGE_TYPES], &'static [u8; MAX_WEDGE_TYPES])> { Some(match (bw, bh) { (8, 8) | (16, 16) | (32, 32) => (&HEQW, &SF_SQUARE), (8, 16) | (16, 32) => (&HGTW, &SF_RECT), (16, 8) | (32, 16) => (&HLTW, &SF_RECT), (8, 32) => (&HGTW, &SF_8X32), (32, 8) => (&HLTW, &SF_32X8), _ => return None, }) } /// Every `(bw, bh)` with a wedge codebook, in `av1_wedge_params_lookup` order. pub const WEDGE_BSIZES: [(usize, usize); 9] = [ (8, 8), (8, 16), (16, 8), (16, 16), (16, 32), (32, 16), (32, 32), (8, 32), (32, 8), ]; /// `shift_copy` (reconinter.c) -- shift a `MASK_MASTER_SIZE`-wide row right /// (`shift >= 0`) or left, replicating the edge sample into the gap. fn shift_copy(src: &[u8; MASK_MASTER_SIZE], dst: &mut [u8], shift: i32) { let w = MASK_MASTER_SIZE; if shift >= 0 { let shift = shift as usize; dst[shift..w].copy_from_slice(&src[..w - shift]); dst[..shift].fill(src[0]); } else { let shift = (-shift) as usize; dst[..w - shift].copy_from_slice(&src[shift..]); dst[w - shift..].fill(src[w - 1]); } } /// `wedge_mask_obl[neg][direction]`, a flat `MASK_MASTER_SIZE`-square plane /// per (neg, direction). Built once by [`init_wedge_master_masks`]. struct MasterMasks { /// `[neg][direction] -> MASK_MASTER_SIZE * MASK_MASTER_SIZE` bytes. obl: [[Vec; N_DIR]; 2], } /// `init_wedge_master_masks` (reconinter.c) ported verbatim. fn init_wedge_master_masks() -> MasterMasks { let w = MASK_MASTER_SIZE; let stride = MASK_MASTER_SIZE; let mut obl: [[Vec; N_DIR]; 2] = Default::default(); // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it. #[allow(clippy::needless_range_loop)] for neg in 0..2 { // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it. #[allow(clippy::needless_range_loop)] for d in 0..N_DIR { obl[neg][d] = vec![0u8; w * w]; } } let mut shift = (w / 4) as i32; for i in (0..w).step_by(2) { { let row_start = i * stride; shift_copy( &WEDGE_MASTER_OBLIQUE_EVEN, &mut obl[0][Dir::Oblique63 as usize][row_start..row_start + w], shift, ); } shift -= 1; { let row_start = (i + 1) * stride; shift_copy( &WEDGE_MASTER_OBLIQUE_ODD, &mut obl[0][Dir::Oblique63 as usize][row_start..row_start + w], shift, ); } obl[0][Dir::Vertical as usize][i * stride..i * stride + w] .copy_from_slice(&WEDGE_MASTER_VERTICAL); obl[0][Dir::Vertical as usize][(i + 1) * stride..(i + 1) * stride + w] .copy_from_slice(&WEDGE_MASTER_VERTICAL); } for i in 0..w { for j in 0..w { let msk = obl[0][Dir::Oblique63 as usize][i * stride + j] as i32; obl[0][Dir::Oblique27 as usize][j * stride + i] = msk as u8; let flip = ((1i32 << WEDGE_WEIGHT_BITS) - msk) as u8; obl[0][Dir::Oblique117 as usize][i * stride + w - 1 - j] = flip; obl[0][Dir::Oblique153 as usize][(w - 1 - j) * stride + i] = flip; obl[1][Dir::Oblique63 as usize][i * stride + j] = flip; obl[1][Dir::Oblique27 as usize][j * stride + i] = flip; obl[1][Dir::Oblique117 as usize][i * stride + w - 1 - j] = msk as u8; obl[1][Dir::Oblique153 as usize][(w - 1 - j) * stride + i] = msk as u8; let mskx = obl[0][Dir::Vertical as usize][i * stride + j] as i32; obl[0][Dir::Horizontal as usize][j * stride + i] = mskx as u8; let flipx = ((1i32 << WEDGE_WEIGHT_BITS) - mskx) as u8; obl[1][Dir::Vertical as usize][i * stride + j] = flipx; obl[1][Dir::Horizontal as usize][j * stride + i] = flipx; } } MasterMasks { obl } } /// `get_wedge_mask_inplace` (reconinter.c:270-289): the `bw x bh` window of /// the master plane for this `(wedge_index, neg, bw, bh)`, still in /// `MASK_MASTER_SIZE`-stride coordinates (caller extracts the window). fn wedge_window(wedge_index: usize, neg: usize, bw: usize, bh: usize) -> (usize, usize, usize) { let (codebook, signflip) = wedge_params(bw, bh).expect("wedge_window on a wedge-capable bsize"); let (dir, xo, yo) = codebook[wedge_index]; let wsignflip = signflip[wedge_index] as usize; let woff = (xo as usize * bw) >> 3; let hoff = (yo as usize * bh) >> 3; let row0 = MASK_MASTER_SIZE / 2 - hoff; let col0 = MASK_MASTER_SIZE / 2 - woff; (neg ^ wsignflip, dir as usize, row0 * MASK_MASTER_SIZE + col0) } /// Per-`(sign, wedge_index)` mask for one bsize, stored PADDED into a /// `stride x stride` plane (`stride = max(bw, bh)`) so a rectangular mask /// indexes exactly like the square prediction buffer `decode.rs` blends /// into. Only rows `0..bh` / columns `0..bw` are meaningful. #[allow(dead_code)] // read only from the `#[cfg(test)]` gates pub struct WedgeCodebook { /// `[sign][wedge_index] -> stride*stride` bytes, row stride `stride`. masks: [[Vec; MAX_WEDGE_TYPES]; 2], #[allow(dead_code)] // read only from the `#[cfg(test)]` gates pub bw: usize, #[allow(dead_code)] // read only from the `#[cfg(test)]` gates pub bh: usize, /// `max(bw, bh)` -- the row stride of every mask returned by [`Self::mask`]. #[allow(dead_code)] // read only from the `#[cfg(test)]` gates pub stride: usize, } impl WedgeCodebook { fn build(master: &MasterMasks, bw: usize, bh: usize) -> Self { let stride = bw.max(bh); let mut masks: [[Vec; MAX_WEDGE_TYPES]; 2] = Default::default(); // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it. #[allow(clippy::needless_range_loop)] for sign in 0..2 { // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it. #[allow(clippy::needless_range_loop)] for idx in 0..MAX_WEDGE_TYPES { let (neg_idx, dir, off) = wedge_window(idx, sign, bw, bh); let plane = &master.obl[neg_idx][dir]; let mut m = vec![0u8; stride * stride]; for i in 0..bh { let src = off + i * MASK_MASTER_SIZE; m[i * stride..i * stride + bw].copy_from_slice(&plane[src..src + bw]); } masks[sign][idx] = m; } } WedgeCodebook { masks, bw, bh, stride } } /// `wedge_sign` in `{0,1}` (spec `wedge_sign`), `wedge_index` in /// `0..MAX_WEDGE_TYPES`. Mask is luma-resolution, row stride /// [`Self::stride`] (`max(bw, bh)`), length `stride * stride`. pub fn mask(&self, wedge_sign: usize, wedge_index: usize) -> &[u8] { &self.masks[wedge_sign][wedge_index] } } /// One codebook per wedge-capable bsize, built once. pub struct WedgeMasks { /// Parallel to [`WEDGE_BSIZES`]. books: Vec, } impl WedgeMasks { fn new() -> Self { let master = init_wedge_master_masks(); WedgeMasks { books: WEDGE_BSIZES .iter() .map(|&(bw, bh)| WedgeCodebook::build(&master, bw, bh)) .collect(), } } /// `(bw, bh)` is the block's TRUE footprint; panics on a size libaom /// gives no codebook (the same invariant `decode.rs`'s `wedge_used_bsize` /// already enforces before any wedge symbol is read). pub fn codebook(&self, bw: usize, bh: usize) -> &WedgeCodebook { let i = WEDGE_BSIZES .iter() .position(|&s| s == (bw, bh)) .unwrap_or_else(|| unreachable!("no wedge codebook for {bw}x{bh}")); &self.books[i] } } static WEDGE_MASKS: std::sync::OnceLock = std::sync::OnceLock::new(); pub fn wedge_masks() -> &'static WedgeMasks { WEDGE_MASKS.get_or_init(WedgeMasks::new) } /// Reproduces the libaom-linked harness's per-block checksum formula /// (`~/.cache/inter16ab-tmp/wedge_h.c`): the TRUE `bw x bh` window read out /// of the padded plane, weighted by its contiguous position + 1. #[cfg(test)] fn checksum(mask: &[u8], bw: usize, bh: usize, stride: usize) -> u64 { let mut sum: u64 = 0; for i in 0..bh { for j in 0..bw { sum += mask[i * stride + j] as u64 * (i * bw + j + 1) as u64; } } sum } #[cfg(test)] mod tests { use super::*; /// lane-inter16ab r5 (class enumerate-table-domain): checksum-verify /// EVERY (bsize, sign, wedge_index) mask this decoder can produce -- /// all 9 wedge-capable bsizes, both signs, all 16 indices = 288 entries /// -- against `av1_get_contiguous_soft_mask()` read straight out of /// libaom by the linked C harness (`lanes/wedge_libaom.expected.txt`, /// regenerate with `gcc -O2 -o wedge_h wedge_h.c -I -I /// /libaom.a -lm -lpthread && ./wedge_h`). #[test] fn wedge_codebook_matches_libaom_dump() { let expected_txt = std::fs::read_to_string(concat!( env!("CARGO_MANIFEST_DIR"), "/../../lanes/wedge_libaom.expected.txt" )) .expect("lanes/wedge_libaom.expected.txt (libaom-linked harness dump)"); let mut expected = std::collections::HashMap::new(); for line in expected_txt.lines() { // "16x8 sign=0 idx=0 checksum=39141" let parts: Vec<&str> = line.split_whitespace().collect(); let name = parts[0].to_string(); let sign: usize = parts[1]["sign=".len()..].parse().unwrap(); let idx: usize = parts[2]["idx=".len()..].parse().unwrap(); let cs: u64 = parts[3]["checksum=".len()..].parse().unwrap(); expected.insert((name, sign, idx), cs); } assert_eq!(expected.len(), 9 * 2 * MAX_WEDGE_TYPES, "the dump covers every entry"); let masks = wedge_masks(); let mut checked = 0; for (bw, bh) in WEDGE_BSIZES { let name = format!("{bw}x{bh}"); let cb = masks.codebook(bw, bh); for sign in 0..2 { for idx in 0..MAX_WEDGE_TYPES { let got = checksum(cb.mask(sign, idx), bw, bh, cb.stride); let want = *expected.get(&(name.clone(), sign, idx)).unwrap_or_else(|| { panic!("missing expected entry for {name} sign={sign} idx={idx}") }); assert_eq!( got, want, "{name} sign={sign} idx={idx}: rust checksum {got} != libaom {want}" ); checked += 1; } } } assert_eq!(checked, 9 * 2 * MAX_WEDGE_TYPES, "checked every (bsize,sign,index) triple"); } }