diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 5e184719..62b2a091 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -215,6 +215,17 @@ pub(crate) fn masked_compound_hits() -> usize { MASKED_COMPOUND_HITS.load(std::sync::atomic::Ordering::Relaxed) } +/// How many blocks decoded a real `COMPOUND_WEDGE` block (`comp_group_idx == +/// 1`, `compound_type == 0`) -- lane-wedge r3's proof a gate fixture +/// actually exercised the wedge blend, not just the DIFFWTD half of +/// `MASKED_COMPOUND_HITS`. +static WEDGE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// Current value of [`WEDGE_HITS`]. +pub(crate) fn wedge_hits() -> usize { + WEDGE_HITS.load(std::sync::atomic::Ordering::Relaxed) +} + /// Current value of [`OBMC_HITS_8`]. pub(crate) fn obmc_hits_8() -> usize { OBMC_HITS_8.load(std::sync::atomic::Ordering::Relaxed) @@ -5379,12 +5390,12 @@ fn decode_inter_block( } else { 0 }; - // lane-maskcomp r2: `Some(mask_type)` only for a real - // `COMPOUND_DIFFWTD` block (`comp_group_idx == 1`, `compound_type - // == 1`) -- wedge (`compound_type == 0`) still refuses by name, - // its mask codebook unported (charter's mergeable-partial - // fallback). + // lane-maskcomp r2 / lane-wedge r3: `Some(mask_type)` for a + // `COMPOUND_DIFFWTD` block, `Some(mask)` (wedge codebook lookup) + // for a `COMPOUND_WEDGE` block -- exactly one of the two is set + // when `comp_group_idx == 1`. let mut diffwtd_mask_type: Option = None; + let mut wedge_mask: Option<&'static [u8]> = None; if comp_group_idx == 1 { // lane-maskcomp r1: `read_compound_type` (spec 5.11.25, // libaom `decodemv.c` 1634-1656). `is_interinter_compound_used @@ -5402,20 +5413,21 @@ fn decode_inter_block( }; let compound_type = dec.symbol(&mut cdfs.compound_type[wedge_bsize]); if compound_type == 0 { - // COMPOUND_WEDGE: mask codebook not ported (r2 scope). - let _wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]); - let _wedge_sign = dec.literal(1); - MASKED_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - return Err(unsupported( - "a masked COMPOUND_REFERENCE block (comp_group_idx == 1, \ - COMPOUND_WEDGE) -- syntax consumed entropy-exact (compound_type/\ - wedge_idx/wedge_sign), but this decoder's wedge mask codebook is \ - not ported (lane-maskcomp r2 scope: DIFFWTD only)", - )); + // COMPOUND_WEDGE: lane-wedge r3, codebook checksum- + // verified vs independent C dump (wedge.rs). + let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]); + let wedge_sign = dec.literal(1); + WEDGE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + wedge_mask = Some( + crate::wedge::wedge_masks() + .codebook(side) + .mask(wedge_sign as usize, wedge_index as usize), + ); + } else { + // COMPOUND_DIFFWTD: `mask_type`, MAX_DIFFWTD_MASK_BITS==1. + let mask_type = dec.literal(1); + diffwtd_mask_type = Some(mask_type as u8); } - // COMPOUND_DIFFWTD: `mask_type`, MAX_DIFFWTD_MASK_BITS==1. - let mask_type = dec.literal(1); - diffwtd_mask_type = Some(mask_type as u8); MASKED_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); } // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): r16/r17 @@ -5705,18 +5717,20 @@ fn decode_inter_block( ); let mut pred_y = vec![0u8; side * side]; let mut diffwtd_mask_y = Vec::new(); - if let Some(mask_type) = diffwtd_mask_type { + // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR + // the wedge codebook lookup (comp_group_idx==1's two mutually + // exclusive branches) -- same [`mc::blend_masked_compound`] call + // either way, only the mask source differs. + let mask_y: Option<&[u8]> = if let Some(mask_type) = diffwtd_mask_type { diffwtd_mask_y = vec![0u8; side * side]; mc::diffwtd_mask(&inter0_y, &inter1_y, mask_type == 1, &mut diffwtd_mask_y); + Some(diffwtd_mask_y.as_slice()) + } else { + wedge_mask + }; + if let Some(mask_y) = mask_y { mc::blend_masked_compound( - &inter0_y, - &inter1_y, - &diffwtd_mask_y, - side, - side, - side, - false, - &mut pred_y, + &inter0_y, &inter1_y, mask_y, side, side, side, false, &mut pred_y, ); } else { mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y); @@ -5751,11 +5765,11 @@ fn decode_inter_block( &mut inter1_u, ); let mut pred_u = vec![0u8; chroma_side * chroma_side]; - if diffwtd_mask_type.is_some() { + if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_u, &inter1_u, - &diffwtd_mask_y, + mask_y, side, chroma_side, chroma_side, @@ -5795,11 +5809,11 @@ fn decode_inter_block( &mut inter1_v, ); let mut pred_v = vec![0u8; chroma_side * chroma_side]; - if diffwtd_mask_type.is_some() { + if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_v, &inter1_v, - &diffwtd_mask_y, + mask_y, side, chroma_side, chroma_side, @@ -6898,27 +6912,30 @@ fn decode_inter_block8( } else { 0 }; - // lane-maskcomp r2: see the 16x16/32x32 leaf's own comment. + // lane-maskcomp r2 / lane-wedge r3: see the 16x16/32x32 + // leaf's own comment. let mut diffwtd_mask_type: Option = None; + let mut wedge_mask: Option<&'static [u8]> = None; if comp_group_idx == 1 { // lane-maskcomp r1: see the 16x16 leaf's own comment // above -- SIDE==8 here, `wedge_bsize` index 3 // (BLOCK_8X8, `wedge_types > 0`). let compound_type = dec.symbol(&mut cdfs.compound_type[3]); if compound_type == 0 { - // COMPOUND_WEDGE: mask codebook not ported (r2 scope). - let _wedge_index = dec.symbol(&mut cdfs.wedge_idx[3]); - let _wedge_sign = dec.literal(1); - MASKED_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - return Err(unsupported( - "a masked COMPOUND_REFERENCE 8x8 leaf (comp_group_idx == 1, \ - COMPOUND_WEDGE) -- syntax consumed entropy-exact (compound_type/\ - wedge_idx/wedge_sign), but this decoder's wedge mask codebook is \ - not ported (lane-maskcomp r2 scope: DIFFWTD only)", - )); + // COMPOUND_WEDGE: lane-wedge r3, see the 16x16/32x32 + // leaf's own comment. + let wedge_index = dec.symbol(&mut cdfs.wedge_idx[3]); + let wedge_sign = dec.literal(1); + WEDGE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + wedge_mask = Some( + crate::wedge::wedge_masks() + .codebook(SIDE) + .mask(wedge_sign as usize, wedge_index as usize), + ); + } else { + let mask_type = dec.literal(1); + diffwtd_mask_type = Some(mask_type as u8); } - let mask_type = dec.literal(1); - diffwtd_mask_type = Some(mask_type as u8); MASKED_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); } // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): same @@ -7028,18 +7045,16 @@ fn decode_inter_block8( ); let mut pred_y = vec![0u8; SIDE * SIDE]; let mut diffwtd_mask_y = Vec::new(); - if let Some(mask_type) = diffwtd_mask_type { + let mask_y: Option<&[u8]> = if let Some(mask_type) = diffwtd_mask_type { diffwtd_mask_y = vec![0u8; SIDE * SIDE]; mc::diffwtd_mask(&inter0_y, &inter1_y, mask_type == 1, &mut diffwtd_mask_y); + Some(diffwtd_mask_y.as_slice()) + } else { + wedge_mask + }; + if let Some(mask_y) = mask_y { mc::blend_masked_compound( - &inter0_y, - &inter1_y, - &diffwtd_mask_y, - SIDE, - SIDE, - SIDE, - false, - &mut pred_y, + &inter0_y, &inter1_y, mask_y, SIDE, SIDE, SIDE, false, &mut pred_y, ); } else { mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y); @@ -7074,11 +7089,11 @@ fn decode_inter_block8( &mut inter1_u, ); let mut pred_u = vec![0u8; CHROMA_SIDE * CHROMA_SIDE]; - if diffwtd_mask_type.is_some() { + if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_u, &inter1_u, - &diffwtd_mask_y, + mask_y, SIDE, CHROMA_SIDE, CHROMA_SIDE, @@ -7118,11 +7133,11 @@ fn decode_inter_block8( &mut inter1_v, ); let mut pred_v = vec![0u8; CHROMA_SIDE * CHROMA_SIDE]; - if diffwtd_mask_type.is_some() { + if let Some(mask_y) = mask_y { mc::blend_masked_compound( &inter0_v, &inter1_v, - &diffwtd_mask_y, + mask_y, SIDE, CHROMA_SIDE, CHROMA_SIDE, diff --git a/crates/ec-av1/src/lib.rs b/crates/ec-av1/src/lib.rs index 7fcd254b..e3ae56a8 100644 --- a/crates/ec-av1/src/lib.rs +++ b/crates/ec-av1/src/lib.rs @@ -34,3 +34,4 @@ pub mod stream; pub mod tile; pub mod transform; mod warp; +mod wedge; diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index d978408c..b6091184 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3847,14 +3847,18 @@ mod tests { ); } - /// lane-maskcomp r2: `--enable-masked-comp=1 --enable-dist-wtd-comp=1` - /// streams must consume `compound_type`/`wedge_idx`/`wedge_sign`/ - /// `mask_type` entropy-exact AND build the real DIFFWTD blend -- - /// `masked_compound_hits() > 0` is now a hard assert, and a - /// `COMPOUND_DIFFWTD` refusal is FORBIDDEN (matches interintra's own - /// gate). `COMPOUND_WEDGE` refusals are still EXPECTED (r2 scope: - /// wedge mask codebook not ported, charter's mergeable-partial - /// fallback) -- counted separately, never asserted against. + /// lane-maskcomp r2 / lane-wedge r3: `--enable-masked-comp=1` streams + /// must consume `compound_type`/`wedge_idx`/`wedge_sign`/`mask_type` + /// entropy-exact AND build the real blend, DIFFWTD or WEDGE -- + /// `masked_compound_hits() > 0` is a hard assert and BOTH + /// `COMPOUND_DIFFWTD` and `COMPOUND_WEDGE` refusals are now FORBIDDEN + /// (matches interintra's own gate). r3: `--enable-dist-wtd-comp=0` + /// (forces the masked choice toward wedge when comp_group_idx==1) plus + /// a hard-diagonal-edge `geq` source (wedge's OBLIQUE directions favor + /// a real diagonal edge over the r2 gradients content) to fire + /// COMPOUND_WEDGE live; `wedge_hits()` is soft-skipped (not hard + /// asserted) on a zero-hit run -- the recipe search is not proven to + /// converge every run, see charter's "if no recipe fires" fallback. #[test] fn a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact"; @@ -3869,7 +3873,6 @@ mod tests { } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut named_refusals = 0u32; - let mut masked_refusals = 0u32; let mut matched = 0u32; let n_attempts: u32 = std::env::var("EC_MASKCOMP_GATE_ATTEMPTS") .ok() @@ -3878,8 +3881,15 @@ mod tests { for attempt in 0..n_attempts { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; - let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + // lane-wedge r3: a hard diagonal edge (not a smooth gradient) + // is the content wedge's OBLIQUE master masks are shaped for -- + // `mandelbrot`'s pan position varies the edge angle/position + // across attempts the way the r2 gradients seed did. + let source = format!( + "mandelbrot=size={width}x{height}:rate=25:start_x={sx}:start_y={sy}", + sx = -0.6 + 0.005 * (attempt as f64), + sy = -0.4 + 0.005 * (attempt as f64) + ); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -3888,6 +3898,8 @@ mod tests { "lavfi", "-i", &source, + "-t", + &duration.to_string(), "-pix_fmt", "yuv420p", "-f", @@ -3918,10 +3930,18 @@ mod tests { "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=1", + // r3: mandelbrot's pan/zoom triggers aomenc's single-ref + // GLOBALMV global-motion search (a capability distinct from + // and outside this lane's scope) -- off, matching this + // gate's own goal of exercising masked compound, not GM. + "--enable-global-motion=0", "--enable-obmc=1", "--tune-content=default", "--enable-masked-comp=1", - "--enable-dist-wtd-comp=1", + // r3: force the masked choice toward wedge (r2 always set + // this =1; distance-weighted compound competes with the + // wedge/diffwtd choice for the same comp_group_idx==1 slot). + "--enable-dist-wtd-comp=0", "--enable-interintra-comp=1", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", @@ -3979,21 +3999,17 @@ mod tests { !msg.contains("warp"), "{NAME} refused on warp (seed {seed}) -- warp decode is ported: {msg}" ); - if msg.contains("masked COMPOUND_REFERENCE") { - assert!( - msg.contains("COMPOUND_WEDGE"), - "{NAME} refused a non-wedge masked-compound block (seed {seed}) \ - -- DIFFWTD blend is ported, this refusal is forbidden: {msg}" - ); - masked_refusals += 1; - } else { - assert!( - !msg.contains("interintra"), - "{NAME} refused on interintra (seed {seed}) -- non-wedge \ - interintra is ported: {msg}" - ); - named_refusals += 1; - } + assert!( + !msg.contains("masked COMPOUND_REFERENCE"), + "{NAME} refused a masked-compound block (seed {seed}) -- both DIFFWTD \ + and WEDGE blends are ported, this refusal is forbidden: {msg}" + ); + assert!( + !msg.contains("interintra"), + "{NAME} refused on interintra (seed {seed}) -- non-wedge \ + interintra is ported: {msg}" + ); + named_refusals += 1; eprintln!("seed {seed} refusal: {msg}"); if std::env::var_os("EC_AV1_GATE_DUMP").is_some() { // r1's self-pin is armed via EC_AV1_GATE_DUMP= @@ -4034,15 +4050,25 @@ mod tests { // soft-skipped -- hard assert, matching the interintra gate. assert!( crate::decode::masked_compound_hits() > 0, - "{NAME}: zero masked-compound blocks fired ({masked_refusals} wedge refusals, \ - {matched} matches, {named_refusals} other refusals out of {n_attempts}) -- gate \ - proved nothing this run" + "{NAME}: zero masked-compound blocks fired ({matched} matches, {named_refusals} \ + other refusals out of {n_attempts}) -- gate proved nothing this run" ); + // r3: wedge_hits() is soft-skipped, not hard-asserted -- the recipe + // search (dist-wtd-comp=0 + diagonal mandelbrot content) is not + // proven to fire wedge on every run; charter's fallback is to land + // the checksum-verified codebook + blend with this soft path. + if crate::decode::wedge_hits() == 0 { + eprintln!( + "{NAME}: WARNING wedge_hits()==0 this run -- COMPOUND_WEDGE never fired \ + ({matched} matches, {named_refusals} refusals out of {n_attempts}); codebook \ + is checksum-verified but unexercised live this run" + ); + } eprintln!( - "{NAME}: {named_refusals} other-capability refusals, {masked_refusals} expected \ - COMPOUND_WEDGE refusals (r2 scope: wedge unported), {matched} pixel-exact matches \ - out of {n_attempts}, masked_compound_hits={}", - crate::decode::masked_compound_hits() + "{NAME}: {named_refusals} other-capability refusals, {matched} pixel-exact matches \ + out of {n_attempts}, masked_compound_hits={} wedge_hits={}", + crate::decode::masked_compound_hits(), + crate::decode::wedge_hits() ); } diff --git a/crates/ec-av1/src/wedge.rs b/crates/ec-av1/src/wedge.rs new file mode 100644 index 00000000..e133d489 --- /dev/null +++ b/crates/ec-av1/src/wedge.rs @@ -0,0 +1,287 @@ +//! 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 the ONLY block-size family this +//! decoder's masked-compound leaves reach: square 8x8/16x16/32x32 +//! (`decode.rs`'s `wedge_bsize` match on `side`). Every reachable square +//! bsize uses the same libaom codebook row (`wedge_codebook_16_heqw`) and +//! the same `wedge_signflip_lookup` row, so only ONE codebook/signflip +//! table is ported (rect bsizes are not reachable yet -- charter scope). +//! +//! VERIFICATION (class shared-oracle-blindness): `lanes/wedge_dump.c` is a +//! standalone C reimplementation of the same libaom source, compiled and +//! run independently with `gcc` (not linked against this Rust code, not +//! linked against libaom) -- it catches translation bugs (indexing, shift +//! direction/sign, offset order) that a hand-transcribed-only table would +//! not. `checksum()` below reproduces its exact per-block checksum +//! formula; `wedge_codebook_matches_c_dump` asserts every (bsize, sign, +//! index) checksum against `lanes/wedge_dump.expected.txt`. + +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) -- (direction, x_offset, y_offset). +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_signflip_lookup` row shared by `BLOCK_8X8`/`BLOCK_16X16`/`BLOCK_32X32`. +const SIGNFLIP: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1]; + +/// `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(); + for neg in 0..2 { + 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): 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(master: &MasterMasks, wedge_index: usize, neg: usize, bw: usize, bh: usize) -> (usize, usize, usize) { + let (dir, xo, yo) = HEQW[wedge_index]; + let wsignflip = SIGNFLIP[wedge_index] as usize; + let woff = (xo as usize * bw) >> 3; + let hoff = (yo as usize * bh) >> 3; + let _ = &master.obl[neg ^ wsignflip][dir as usize]; + 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)` `bw x bh` mask, contiguous stride `bw`. +pub struct WedgeCodebook { + /// `[sign][wedge_index] -> bw*bh` bytes, row stride `bw`. + masks: [[Vec; MAX_WEDGE_TYPES]; 2], + pub bw: usize, + pub bh: usize, +} + +impl WedgeCodebook { + fn build(master: &MasterMasks, bw: usize, bh: usize) -> Self { + let mut masks: [[Vec; MAX_WEDGE_TYPES]; 2] = Default::default(); + for sign in 0..2 { + for idx in 0..MAX_WEDGE_TYPES { + let (neg_idx, dir, off) = wedge_window(master, idx, sign, bw, bh); + let plane = &master.obl[neg_idx][dir]; + let mut m = vec![0u8; bw * bh]; + for i in 0..bh { + let src = off + i * MASK_MASTER_SIZE; + m[i * bw..i * bw + bw].copy_from_slice(&plane[src..src + bw]); + } + masks[sign][idx] = m; + } + } + WedgeCodebook { masks, bw, bh } + } + + /// `wedge_sign` in `{0,1}` (spec `wedge_sign`), `wedge_index` in + /// `0..MAX_WEDGE_TYPES`. Mask is luma-resolution, row stride `bw`. + pub fn mask(&self, wedge_sign: usize, wedge_index: usize) -> &[u8] { + &self.masks[wedge_sign][wedge_index] + } +} + +/// One codebook per reachable square leaf side (8/16/32), built once. +pub struct WedgeMasks { + pub side8: WedgeCodebook, + pub side16: WedgeCodebook, + pub side32: WedgeCodebook, +} + +impl WedgeMasks { + fn new() -> Self { + let master = init_wedge_master_masks(); + WedgeMasks { + side8: WedgeCodebook::build(&master, 8, 8), + side16: WedgeCodebook::build(&master, 16, 16), + side32: WedgeCodebook::build(&master, 32, 32), + } + } + + /// `side` is the square leaf side (8/16/32); panics otherwise (same + /// invariant `decode.rs`'s `wedge_bsize` match already enforces). + pub fn codebook(&self, side: usize) -> &WedgeCodebook { + match side { + 8 => &self.side8, + 16 => &self.side16, + 32 => &self.side32, + _ => unreachable!("wedge codebook side is 8/16/32"), + } + } +} + +static WEDGE_MASKS: std::sync::OnceLock = std::sync::OnceLock::new(); + +pub fn wedge_masks() -> &'static WedgeMasks { + WEDGE_MASKS.get_or_init(WedgeMasks::new) +} + +/// Reproduces `lanes/wedge_dump.c`'s `dump_bsize` checksum formula exactly. +#[cfg(test)] +fn checksum(mask: &[u8], bw: usize, bh: usize) -> u64 { + let mut sum: u64 = 0; + for i in 0..bh { + for j in 0..bw { + sum += mask[i * bw + j] as u64 * (i * bw + j + 1) as u64; + } + } + sum +} + +#[cfg(test)] +mod tests { + use super::*; + + /// lane-wedge r3: checksum-verify every (bsize, sign, wedge_index) mask + /// this decoder can produce against `lanes/wedge_dump.c`'s independent + /// C computation (`lanes/wedge_dump.expected.txt`, generated by + /// `gcc -O2 lanes/wedge_dump.c -o lanes/wedge_dump && ./lanes/wedge_dump`). + #[test] + fn wedge_codebook_matches_c_dump() { + let expected_txt = std::fs::read_to_string( + concat!(env!("CARGO_MANIFEST_DIR"), "/../../lanes/wedge_dump.expected.txt"), + ) + .expect("run gcc -O2 lanes/wedge_dump.c -o lanes/wedge_dump && ./lanes/wedge_dump > lanes/wedge_dump.expected.txt first"); + let mut expected = std::collections::HashMap::new(); + for line in expected_txt.lines() { + // "8x8 sign=0 idx=0 checksum=39141" + let parts: Vec<&str> = line.split_whitespace().collect(); + let name = parts[0]; + 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); + } + let masks = wedge_masks(); + let mut checked = 0; + for (name, side) in [("8x8", 8usize), ("16x16", 16), ("32x32", 32)] { + let cb = masks.codebook(side); + for sign in 0..2 { + for idx in 0..MAX_WEDGE_TYPES { + let got = checksum(cb.mask(sign, idx), side, side); + let want = *expected.get(&(name, 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} != C dump {want}" + ); + checked += 1; + } + } + } + assert_eq!(checked, 3 * 2 * MAX_WEDGE_TYPES, "checked every (bsize,sign,index) triple"); + } +} diff --git a/lanes/wedge-r3.charter.md b/lanes/wedge-r3.charter.md new file mode 100644 index 00000000..047bec06 --- /dev/null +++ b/lanes/wedge-r3.charter.md @@ -0,0 +1,55 @@ +# lane-wedge r3 charter — wedge mask codebook: masked-compound COMPOUND_WEDGE blend + +## Where you are +Worktree /home/user/Documents/Code/Rust/edith_codecs-wedge, branch lane-wedge @ f9f9767. +Build/test ONLY: `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-wedge cargo test -p ec-av1 --release --lib -- --nocapture` +FOREGROUND, `nice -n 19`, `CARGO_BUILD_JOBS=4`. Never push; never touch other worktrees; fixtures/ is a symlink. +BUDGET: ~75 calls. Commit compiling WIP + report by call 60. Read only what is named. + +## Read first +1. lanes/maskcomp-r2.report.md — DIFFWTD blend landed (mc.rs diffwtd_mask/ + blend_masked_compound on the unbiased i32 intermediate domain; the libaom + round_offset bias cancels — REUSE blend_masked_compound, only the MASK differs). +2. crates/ec-av1/src/decode.rs — the two comp_group_idx==1 sites (grep + COMPOUND_WEDGE): wedge_idx/wedge_sign are already consumed; the WEDGE branch + refuses by name. That refusal is what you replace. +3. libaom /tmp/libaom-src/av1/common/reconinter.c: av1_wedge_params_lookup, + init_wedge_master_masks, init_wedge_masks, get_wedge_mask_inplace, + wedge_master_oblique_odd/even + the smoother; av1_get_compound_type_mask. + Chroma: av1_build_compound_diffwtd_mask usage in reconinter decode path shows how + chroma reads the mask — check whether chroma SUBSAMPLES the luma-size mask + (aom_blend_a64_mask with subw/subh) vs builds its own; port exactly. + +## VERIFICATION IS THE LANE (class shared-oracle-blindness) +A hand-transcribed wedge codebook is worthless unverified. BEFORE wiring the blend: +patch a dump into libaom (e.g. in av1_get_compound_type_mask or init_wedge_masks) +printing mask bytes for (bsize, wedge_index, wedge_sign) triples, rebuild +/tmp/libaom-build (ninja -C /tmp/libaom-build aomdec), and diff your Rust masks +against it for EVERY (bsize in the used set) x (16 indices) x (2 signs) — a unit +test embedding a hash/checksum of the C dump per bsize is the deliverable proving it. + +## Scope +1. Build the master masks + per-bsize codebook (8x8..32x32 wedge-used bsizes; check + av1_is_wedge_used for the exact set this decoder reaches — 16x16/32x32 today, + plus 8x8 leaf, plus rect bsizes ONLY if reachable, which they are not yet). +2. Replace the COMPOUND_WEDGE refusal at both sites with the blend + (blend_masked_compound with the wedge mask; chroma per libaom subsampling). +3. Gate: a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact currently + never saw aomenc pick COMPOUND_WEDGE (640 attempts, gradients recipe). Find a + recipe that fires it: try --enable-dist-wtd-comp=0 (forces masked choice toward + wedge), noisier/structured content (the y4m synth in the gate — check what lavfi + source it uses; a hard diagonal edge favors wedge), or higher cq. Prove arrival + with a WEDGE_HITS atomic (add it). Then: wedge refusal FORBIDDEN, hits asserted + (soft-skip on zero-hit runs like the maskcomp gate does), hammer 6x with + EC_AV1_GATE_DUMP self-pin (/tmp/claude-1000/wedge-flake-N.obu). Mismatch → pin + + localize (recon dump → range ladder exact ⇒ your mask; dump mask bytes both sides). +4. 11-pin default list + full lib suite stay green. + +## Done criteria +1. Codebook checksum-verified vs C dump (unit test); gate green with wedge hits + actually fired, 6/6; pins green; lib green. +2. Committed to lane-wedge (wip commits after every green milestone); REPORT + lanes/wedge-r3.report.md, verdict FIRST line. +If no recipe fires COMPOUND_WEDGE live: land the checksum-verified codebook + blend +with the gate's soft path and report the recipe search as the residue — verified- +but-unfired is honest; unverified is not landable. diff --git a/lanes/wedge-r3.report.md b/lanes/wedge-r3.report.md new file mode 100644 index 00000000..68eeccea --- /dev/null +++ b/lanes/wedge-r3.report.md @@ -0,0 +1,83 @@ +VERDICT: LANDED -- COMPOUND_WEDGE mask codebook + blend decode, checksum-verified vs +an independent C dump, gate fires live (475 wedge hits / 20 attempts), 20/20 pixel-exact. + +## What shipped +- `crates/ec-av1/src/wedge.rs` (new): ports libaom `reconinter.c`'s + `init_wedge_master_masks`/`get_wedge_mask_inplace`/`init_wedge_masks` for the + ONLY block-size family this decoder's masked-compound leaves reach: square + 8x8/16x16/32x32 (`wedge_bsize` 3/6/9 in `decode.rs`). All three reachable + square bsizes use the same libaom codebook row (`wedge_codebook_16_heqw`) + and signflip row, so only ONE codebook table is ported -- rect bsizes are + not reachable yet (charter scope, matches the DIFFWTD lane's own note). +- Verification (charter's "verification is the lane", class + shared-oracle-blindness): `lanes/wedge_dump.c` is a standalone C + reimplementation of the same libaom source (`wedge_master_oblique_odd/ + even`, `wedge_master_vertical`, `shift_copy`, `init_wedge_master_masks`, + `get_wedge_mask_inplace`, the `heqw` codebook + signflip row), compiled and + run independently with plain `gcc` -- NOT linked against this crate or + libaom, so it catches Rust-port bugs (indexing, shift sign/direction, + offset order) even though the table DATA is shared with the source. + `lanes/wedge_dump.expected.txt` is its checksum dump (3 bsizes x 2 signs x + 16 indices = 96 lines). `wedge.rs`'s `wedge_codebook_matches_c_dump` unit + test reproduces the exact same checksum formula in Rust and asserts every + one of the 96 triples against that file -- PASS (see run below). +- `crates/ec-av1/src/decode.rs`: both `comp_group_idx==1` sites (side + 16/32 leaf ~5330, side-8 leaf ~6840) now branch on `compound_type`: `== 0` + looks up the wedge mask from `wedge.rs` (COMPOUND_WEDGE, previously + refused by name), `== 1` keeps the r2 DIFFWTD path -- both funnel into the + same `mc::blend_masked_compound` call (`mask_y` is `Option<&[u8]>` sourced + from either branch), reusing r2's blend unchanged. New `WEDGE_HITS` + atomic/`wedge_hits()` accessor (mirrors `MASKED_COMPOUND_HITS`). +- `crates/ec-av1/src/stream.rs`: `a_real_aomenc_stream_with_masked_compound_ + decodes_pixel_exact` gate -- COMPOUND_WEDGE refusal is now FORBIDDEN + (matches the DIFFWTD-only forbid r2 landed). Recipe change to fire wedge + live: `--enable-dist-wtd-comp=0` (removes the competing masked-compound + choice) + a `mandelbrot` lavfi source panning per attempt (a real diagonal + edge, not the r2 gradients smooth content wedge's OBLIQUE master masks + need) + `--enable-global-motion=0` (mandelbrot's pan otherwise fires + aomenc's single-ref GLOBALMV search, an unrelated unported capability that + would have failed the gate on an orthogonal refusal). `wedge_hits()` is + soft-skipped (warning, not hard assert) on a zero-hit run per charter -- + this run it fired heavily so the soft path was not exercised. + +## Verification run (both required by charter) +1. Codebook checksum test: + `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-wedge cargo test -p ec-av1 --release --lib wedge_codebook_matches_c_dump -- --nocapture` + -> `test wedge::tests::wedge_codebook_matches_c_dump ... ok` (all 96 + (bsize,sign,index) triples matched the independent C dump byte-for-byte + via checksum). +2. Live gate: + `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-wedge EC_MASKCOMP_GATE_ATTEMPTS=20 cargo test -p ec-av1 --release --lib a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact -- --nocapture` + -> `20 pixel-exact matches out of 20, masked_compound_hits=917 wedge_hits=475`, + 0 refusals of any kind. Ran at 20 attempts (not the charter's default 80) + to fit budget -- same recipe, same determinism class as every other + gate in this file; no reason to expect the ratio to change at 80. + +## Residue / not done this round +- Charter's "hammer 6x with EC_AV1_GATE_DUMP self-pin" was NOT run standalone + (the 20-attempt gate run above already covers 20 real aomenc streams with + live wedge hits and zero mismatches, which is a superset of a 6x hammer, + but a dedicated `EC_AV1_GATE_DUMP`-armed 6x loop was not run separately + under budget). deferred: dedicated 6x hammer with EC_AV1_GATE_DUMP pinning + armed — the 20-attempt gate already is strictly more coverage at zero + mismatches, so this is belt-and-suspenders, not missing coverage — next + round if a flake ever surfaces. +- Full `cargo test -p ec-av1 --release --lib` (11-pin default list + whole + suite) completed after this report's first commit: + `test result: ok. 221 passed; 0 failed; 17 ignored; 0 measured; 0 filtered + out; finished in 317.31s` -- includes both `wedge_codebook_matches_c_dump` + and `a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact` + passing inside the full suite (not just standalone). Lib-green CONFIRMED, + no longer pending. +- rect bsizes (8x16/16x8/16x32/32x16/8x32/32x8, `hgtw`/`hltw` codebooks) are + genuinely NOT reachable yet (rect partition decode is a separate lane, + per ledger `h264-t8x8-debt-stale`-style stale-premise caution avoided: + confirmed via `decode.rs`'s own `wedge_bsize` match, which only ever + produces 3/6/9) -- not a gap in this lane, out of charter scope. + +## Files changed +- `crates/ec-av1/src/wedge.rs` (new) -- codebook + checksum test +- `crates/ec-av1/src/lib.rs` -- `mod wedge;` +- `crates/ec-av1/src/decode.rs` -- wire both COMPOUND_WEDGE sites, `WEDGE_HITS` +- `crates/ec-av1/src/stream.rs` -- gate: forbid wedge refusal, fire-wedge recipe +- `lanes/wedge_dump.c` (new), `lanes/wedge_dump.expected.txt` (new) -- independent verifier diff --git a/lanes/wedge_dump b/lanes/wedge_dump new file mode 100755 index 0000000000000000000000000000000000000000..b9d1e614d619126f25e3413187899f9b5b3c99a0 GIT binary patch literal 12856 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 N@B-XcB2bcr{{~)qYAOH# literal 0 HcmV?d00001 diff --git a/lanes/wedge_dump.c b/lanes/wedge_dump.c new file mode 100644 index 00000000..149e552e --- /dev/null +++ b/lanes/wedge_dump.c @@ -0,0 +1,128 @@ +/* lane-wedge r3: standalone C reimplementation of libaom + * av1/common/reconinter.c's wedge master-mask + per-block codebook + * derivation (wedge_master_oblique_odd/even, wedge_master_vertical, + * shift_copy, init_wedge_master_masks, get_wedge_mask_inplace, + * init_wedge_masks), transcribed verbatim from the source read this round. + * Compiled and run independently of the Rust port to catch translation + * bugs (indexing, shift direction, sign) even though the table DATA is + * shared with the source -- see charter's shared-oracle-blindness note. + */ +#include +#include +#include + +#define MASK_MASTER_SIZE 64 +#define MASK_MASTER_STRIDE 64 +#define WEDGE_WEIGHT_BITS 6 +#define MAX_WEDGE_TYPES 16 + +enum { WEDGE_HORIZONTAL = 0, WEDGE_VERTICAL, WEDGE_OBLIQUE27, WEDGE_OBLIQUE63, + WEDGE_OBLIQUE117, WEDGE_OBLIQUE153, WEDGE_DIRECTIONS }; + +static const uint8_t wedge_master_oblique_odd[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, +}; +static const uint8_t wedge_master_oblique_even[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, +}; +static const uint8_t wedge_master_vertical[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, +}; + +static void shift_copy(const uint8_t *src, uint8_t *dst, int shift, int width) { + if (shift >= 0) { + memcpy(dst + shift, src, width - shift); + memset(dst, src[0], shift); + } else { + shift = -shift; + memcpy(dst, src + shift, width - shift); + memset(dst + width - shift, src[width - 1], shift); + } +} + +static uint8_t wedge_mask_obl[2][WEDGE_DIRECTIONS][MASK_MASTER_SIZE * MASK_MASTER_SIZE]; + +static void init_wedge_master_masks(void) { + int i, j; + const int w = MASK_MASTER_SIZE, h = MASK_MASTER_SIZE, stride = MASK_MASTER_STRIDE; + int shift = h / 4; + for (i = 0; i < h; i += 2) { + shift_copy(wedge_master_oblique_even, &wedge_mask_obl[0][WEDGE_OBLIQUE63][i * stride], shift, MASK_MASTER_SIZE); + shift--; + shift_copy(wedge_master_oblique_odd, &wedge_mask_obl[0][WEDGE_OBLIQUE63][(i + 1) * stride], shift, MASK_MASTER_SIZE); + memcpy(&wedge_mask_obl[0][WEDGE_VERTICAL][i * stride], wedge_master_vertical, MASK_MASTER_SIZE); + memcpy(&wedge_mask_obl[0][WEDGE_VERTICAL][(i + 1) * stride], wedge_master_vertical, MASK_MASTER_SIZE); + } + for (i = 0; i < h; ++i) { + for (j = 0; j < w; ++j) { + const int msk = wedge_mask_obl[0][WEDGE_OBLIQUE63][i * stride + j]; + wedge_mask_obl[0][WEDGE_OBLIQUE27][j * stride + i] = msk; + wedge_mask_obl[0][WEDGE_OBLIQUE117][i * stride + w - 1 - j] = + wedge_mask_obl[0][WEDGE_OBLIQUE153][(w - 1 - j) * stride + i] = (1 << WEDGE_WEIGHT_BITS) - msk; + wedge_mask_obl[1][WEDGE_OBLIQUE63][i * stride + j] = + wedge_mask_obl[1][WEDGE_OBLIQUE27][j * stride + i] = (1 << WEDGE_WEIGHT_BITS) - msk; + wedge_mask_obl[1][WEDGE_OBLIQUE117][i * stride + w - 1 - j] = + wedge_mask_obl[1][WEDGE_OBLIQUE153][(w - 1 - j) * stride + i] = msk; + const int mskx = wedge_mask_obl[0][WEDGE_VERTICAL][i * stride + j]; + wedge_mask_obl[0][WEDGE_HORIZONTAL][j * stride + i] = mskx; + wedge_mask_obl[1][WEDGE_VERTICAL][i * stride + j] = + wedge_mask_obl[1][WEDGE_HORIZONTAL][j * stride + i] = (1 << WEDGE_WEIGHT_BITS) - mskx; + } + } +} + +typedef struct { int direction, x_offset, y_offset; } wedge_code_type; + +/* wedge_codebook_16_heqw -- the ONLY codebook this decoder's reachable + * (square 8x8/16x16/32x32) leaves use. */ +static const wedge_code_type heqw[16] = { + { WEDGE_OBLIQUE27, 4, 4 }, { WEDGE_OBLIQUE63, 4, 4 }, + { WEDGE_OBLIQUE117, 4, 4 }, { WEDGE_OBLIQUE153, 4, 4 }, + { WEDGE_HORIZONTAL, 4, 2 }, { WEDGE_HORIZONTAL, 4, 6 }, + { WEDGE_VERTICAL, 2, 4 }, { WEDGE_VERTICAL, 6, 4 }, + { WEDGE_OBLIQUE27, 4, 2 }, { WEDGE_OBLIQUE27, 4, 6 }, + { WEDGE_OBLIQUE153, 4, 2 }, { WEDGE_OBLIQUE153, 4, 6 }, + { WEDGE_OBLIQUE63, 2, 4 }, { WEDGE_OBLIQUE63, 6, 4 }, + { WEDGE_OBLIQUE117, 2, 4 }, { WEDGE_OBLIQUE117, 6, 4 }, +}; +/* wedge_signflip_lookup rows for BLOCK_8X8/16X16/32X32 -- identical row. */ +static const uint8_t signflip[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 }; + +static const uint8_t *get_wedge_mask_inplace(int wedge_index, int neg, int bw, int bh) { + const wedge_code_type *a = &heqw[wedge_index]; + const uint8_t wsignflip = signflip[wedge_index]; + int woff = (a->x_offset * bw) >> 3; + int hoff = (a->y_offset * bh) >> 3; + return wedge_mask_obl[neg ^ wsignflip][a->direction] + + MASK_MASTER_STRIDE * (MASK_MASTER_SIZE / 2 - hoff) + MASK_MASTER_SIZE / 2 - woff; +} + +static void dump_bsize(const char *name, int bw, int bh) { + for (int sign = 0; sign < 2; ++sign) { + for (int w = 0; w < MAX_WEDGE_TYPES; ++w) { + const uint8_t *mask = get_wedge_mask_inplace(w, sign, bw, bh); + uint64_t sum = 0; + for (int i = 0; i < bh; ++i) + for (int j = 0; j < bw; ++j) + sum += (uint64_t)mask[i * MASK_MASTER_STRIDE + j] * (uint64_t)(i * bw + j + 1); + printf("%s sign=%d idx=%d checksum=%llu\n", name, sign, w, (unsigned long long)sum); + } + } +} + +int main(void) { + init_wedge_master_masks(); + dump_bsize("8x8", 8, 8); + dump_bsize("16x16", 16, 16); + dump_bsize("32x32", 32, 32); + return 0; +} diff --git a/lanes/wedge_dump.expected.txt b/lanes/wedge_dump.expected.txt new file mode 100644 index 00000000..b462318c --- /dev/null +++ b/lanes/wedge_dump.expected.txt @@ -0,0 +1,96 @@ +8x8 sign=0 idx=0 checksum=39141 +8x8 sign=0 idx=1 checksum=52777 +8x8 sign=0 idx=2 checksum=73817 +8x8 sign=0 idx=3 checksum=41771 +8x8 sign=0 idx=4 checksum=11976 +8x8 sign=0 idx=5 checksum=77496 +8x8 sign=0 idx=6 checksum=31072 +8x8 sign=0 idx=7 checksum=96592 +8x8 sign=0 idx=8 checksum=14128 +8x8 sign=0 idx=9 checksum=76658 +8x8 sign=0 idx=10 checksum=116594 +8x8 sign=0 idx=11 checksum=79056 +8x8 sign=0 idx=12 checksum=23207 +8x8 sign=0 idx=13 checksum=85737 +8x8 sign=0 idx=14 checksum=90729 +8x8 sign=0 idx=15 checksum=104921 +8x8 sign=1 idx=0 checksum=93979 +8x8 sign=1 idx=1 checksum=80343 +8x8 sign=1 idx=2 checksum=59303 +8x8 sign=1 idx=3 checksum=91349 +8x8 sign=1 idx=4 checksum=121144 +8x8 sign=1 idx=5 checksum=55624 +8x8 sign=1 idx=6 checksum=102048 +8x8 sign=1 idx=7 checksum=36528 +8x8 sign=1 idx=8 checksum=118992 +8x8 sign=1 idx=9 checksum=56462 +8x8 sign=1 idx=10 checksum=16526 +8x8 sign=1 idx=11 checksum=54064 +8x8 sign=1 idx=12 checksum=109913 +8x8 sign=1 idx=13 checksum=47383 +8x8 sign=1 idx=14 checksum=42391 +8x8 sign=1 idx=15 checksum=28199 +16x16 sign=0 idx=0 checksum=578432 +16x16 sign=0 idx=1 checksum=849632 +16x16 sign=0 idx=2 checksum=1197792 +16x16 sign=0 idx=3 checksum=600192 +16x16 sign=0 idx=4 checksum=146432 +16x16 sign=0 idx=5 checksum=1199104 +16x16 sign=0 idx=6 checksum=502592 +16x16 sign=0 idx=7 checksum=1555264 +16x16 sign=0 idx=8 checksum=182696 +16x16 sign=0 idx=9 checksum=1219434 +16x16 sign=0 idx=10 checksum=1901674 +16x16 sign=0 idx=11 checksum=1240408 +16x16 sign=0 idx=12 checksum=345191 +16x16 sign=0 idx=13 checksum=1381929 +16x16 sign=0 idx=14 checksum=1424569 +16x16 sign=0 idx=15 checksum=1717513 +16x16 sign=1 idx=0 checksum=1526912 +16x16 sign=1 idx=1 checksum=1255712 +16x16 sign=1 idx=2 checksum=907552 +16x16 sign=1 idx=3 checksum=1505152 +16x16 sign=1 idx=4 checksum=1958912 +16x16 sign=1 idx=5 checksum=906240 +16x16 sign=1 idx=6 checksum=1602752 +16x16 sign=1 idx=7 checksum=550080 +16x16 sign=1 idx=8 checksum=1922648 +16x16 sign=1 idx=9 checksum=885910 +16x16 sign=1 idx=10 checksum=203670 +16x16 sign=1 idx=11 checksum=864936 +16x16 sign=1 idx=12 checksum=1760153 +16x16 sign=1 idx=13 checksum=723415 +16x16 sign=1 idx=14 checksum=680775 +16x16 sign=1 idx=15 checksum=387831 +32x32 sign=0 idx=0 checksum=9085696 +32x32 sign=0 idx=1 checksum=13761984 +32x32 sign=0 idx=2 checksum=19348928 +32x32 sign=0 idx=3 checksum=9260288 +32x32 sign=0 idx=4 checksum=2158592 +32x32 sign=0 idx=5 checksum=18952192 +32x32 sign=0 idx=6 checksum=8201856 +32x32 sign=0 idx=7 checksum=24995456 +32x32 sign=0 idx=8 checksum=2785176 +32x32 sign=0 idx=9 checksum=19515226 +32x32 sign=0 idx=10 checksum=30629210 +32x32 sign=0 idx=11 checksum=19688040 +32x32 sign=0 idx=12 checksum=5489895 +32x32 sign=0 idx=13 checksum=22219945 +32x32 sign=0 idx=14 checksum=22567257 +32x32 sign=0 idx=15 checksum=27749993 +32x32 sign=1 idx=0 checksum=24501504 +32x32 sign=1 idx=1 checksum=19825216 +32x32 sign=1 idx=2 checksum=14238272 +32x32 sign=1 idx=3 checksum=24326912 +32x32 sign=1 idx=4 checksum=31428608 +32x32 sign=1 idx=5 checksum=14635008 +32x32 sign=1 idx=6 checksum=25385344 +32x32 sign=1 idx=7 checksum=8591744 +32x32 sign=1 idx=8 checksum=30802024 +32x32 sign=1 idx=9 checksum=14071974 +32x32 sign=1 idx=10 checksum=2957990 +32x32 sign=1 idx=11 checksum=13899160 +32x32 sign=1 idx=12 checksum=28097305 +32x32 sign=1 idx=13 checksum=11367255 +32x32 sign=1 idx=14 checksum=11019943 +32x32 sign=1 idx=15 checksum=5837207