diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 4eba7286..3d7e3130 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -949,6 +949,14 @@ thread_local! { /// counter from [`TX_DEPTH_HITS`] (which every square path also bumps) so /// the gate can prove THIS path fired. static RECT8_SPLIT_TX_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// 16x8/8x16 intra strips whose prediction actually read past the block's + /// own width (above-right) or height (below-left) -- the samples + /// `Reach::of_rect` decides on, and the only ones a wrong `has_tr_*`/ + /// `has_bl_*` table row can corrupt (lane-tx4x8 r3). + static RECT_STRIP_REACH_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// Every 16x8/8x16 intra strip predicted, reaching or not -- the + /// denominator [`RECT_STRIP_REACH_HITS`] is a fraction of. + static RECT_STRIP_PRED_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// Current value of [`RECT8_SPLIT_TX_HITS`]. @@ -956,6 +964,16 @@ pub(crate) fn rect8_split_tx_hits() -> usize { RECT8_SPLIT_TX_HITS.with(|c| c.get()) } +/// Current value of [`RECT_STRIP_PRED_HITS`]. +pub(crate) fn rect_strip_pred_hits() -> usize { + RECT_STRIP_PRED_HITS.with(|c| c.get()) +} + +/// Current value of [`RECT_STRIP_REACH_HITS`]. +pub(crate) fn rect_strip_reach_hits() -> usize { + RECT_STRIP_REACH_HITS.with(|c| c.get()) +} + /// Current value of [`TX4X8_CODED_HITS`]. pub(crate) fn tx4x8_coded_hits() -> usize { TX4X8_CODED_HITS.with(|c| c.get()) @@ -4766,6 +4784,12 @@ impl PlaneBuf { }); let corner = (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); + if matches!((bw, bh), (16, 8) | (8, 16)) { + RECT_STRIP_PRED_HITS.with(|h| h.set(h.get() + 1)); + if across > own_across || down > own_down { + RECT_STRIP_REACH_HITS.with(|h| h.set(h.get() + 1)); + } + } (above, left, corner) } diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index a1817eab..e36c60ee 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1070,50 +1070,94 @@ const HAS_BOTTOM_LEFT: [&[u8]; 4] = [ ], ]; -/// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp -/// r1: the two rect strips PARTITION_HORZ/VERT ever produce at the 32x32 -/// level, transcribed verbatim (not derived): `[0]` is 32-wide, `[1]` is -/// 16-wide. -const HAS_TOP_RIGHT_RECT: [&[u8]; 2] = [&[15, 5, 7, 5], &[255, 119, 127, 119]]; -/// `has_bl_32x16`/`has_bl_16x32`, same source, same order. -const HAS_BOTTOM_LEFT_RECT: [&[u8]; 2] = [&[78, 14, 78, 14], &[16, 0, 16, 0]]; - -/// `has_tr_4x8` (libaom `reconintra.c`), transcribed verbatim -- lane-tx4x8's -/// `PARTITION_VERT` leaf of an 8x8 block. Row stride is -/// [`Reach::table_stride`]`(4)` = 32 bits, as for every other entry of -/// libaom's own `has_tr_tables`. -const HAS_TR_4X8: [u8; 64] = [ - 255, 255, 255, 255, 119, 119, 119, 119, 127, 127, 127, 127, 119, 119, 119, 119, 255, 127, 255, - 127, 119, 119, 119, 119, 127, 127, 127, 127, 119, 119, 119, 119, 255, 255, 255, 127, 119, 119, - 119, 119, 127, 127, 127, 127, 119, 119, 119, 119, 255, 127, 255, 127, 119, 119, 119, 119, 127, - 127, 127, 127, 119, 119, 119, 119, -]; -/// `has_tr_8x4`, same source -- the `PARTITION_HORZ` leaf. -const HAS_TR_8X4: [u8; 64] = [ - 255, 255, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0, 127, 127, 0, 0, 85, 85, 0, 0, 119, - 119, 0, 0, 85, 85, 0, 0, 255, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0, 127, 127, - 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0, -]; -/// `has_bl_4x8`, same source. -const HAS_BL_4X8: [u8; 64] = [ - 16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 1, 0, 16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, - 17, 0, 0, 0, 0, 16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 1, 0, 16, 17, 17, 17, 0, 1, - 1, 1, 16, 17, 17, 17, 0, 0, 0, 0, -]; -/// `has_bl_8x4`, same source. -const HAS_BL_8X4: [u8; 64] = [ - 254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 1, 254, 255, 84, 85, 254, - 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 0, 254, 255, 84, 85, 254, 255, 16, 17, 254, 255, - 84, 85, 254, 255, 0, 1, 254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 0, -]; +/// The `has_tr_*` / `has_bl_*` tables for every rectangular shape this +/// decoder codes (libaom `reconintra.c`, generated from the oracle's source +/// so no byte is retyped). Each is indexed by +/// `(blk_row_in_sb << (5 - log2(bw / 4))) + blk_col_in_sb`, i.e. row stride +/// `128 / bw` bits ([`Reach::table_stride`]) on libaom's 128-pixel +/// superblock grid; on this crate's 64-pixel superblock the highest index a +/// shape can reach stays inside its own table, so no wrap is involved. +/// +/// lane-tx4x8 r3: the previous two-row `HAS_TOP_RIGHT_RECT`/ +/// `HAS_BOTTOM_LEFT_RECT` pair held 32x16/16x32 only and a `_` arm routed +/// every other shape -- 16x8 strips among them -- to the 32x16 row, wrong at +/// 10 of the 21 reachable superblock positions for above-right and 7 for +/// below-left. +/// `has_tr_4x8` (`reconintra.c`), transcribed verbatim: 64 bytes, row stride 32 bits (`128 / 4`). +const HAS_TR_4X8: [u8; 64] = [255, 255, 255, 255, 119, 119, 119, 119, 127, 127, 127, 127, 119, 119, 119, 119, 255, 127, 255, 127, 119, 119, 119, 119, 127, 127, 127, 127, 119, 119, 119, 119, 255, 255, 255, 127, 119, 119, 119, 119, 127, 127, 127, 127, 119, 119, 119, 119, 255, 127, 255, 127, 119, 119, 119, 119, 127, 127, 127, 127, 119, 119, 119, 119]; +/// `has_bl_4x8` (`reconintra.c`), transcribed verbatim: 64 bytes, row stride 32 bits (`128 / 4`). +const HAS_BL_4X8: [u8; 64] = [16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 1, 0, 16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 0, 0, 16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 1, 0, 16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 0, 0]; +/// `has_tr_8x4` (`reconintra.c`), transcribed verbatim: 64 bytes, row stride 16 bits (`128 / 8`). +const HAS_TR_8X4: [u8; 64] = [255, 255, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0, 127, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0, 255, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0, 127, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0]; +/// `has_bl_8x4` (`reconintra.c`), transcribed verbatim: 64 bytes, row stride 16 bits (`128 / 8`). +const HAS_BL_8X4: [u8; 64] = [254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 1, 254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 0, 254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 1, 254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 0]; +/// `has_tr_8x16` (`reconintra.c`), transcribed verbatim: 16 bytes, row stride 16 bits (`128 / 8`). +const HAS_TR_8X16: [u8; 16] = [255, 255, 119, 119, 127, 127, 119, 119, 255, 127, 119, 119, 127, 127, 119, 119]; +/// `has_bl_8x16` (`reconintra.c`), transcribed verbatim: 16 bytes, row stride 16 bits (`128 / 8`). +const HAS_BL_8X16: [u8; 16] = [16, 17, 0, 1, 16, 17, 0, 0, 16, 17, 0, 1, 16, 17, 0, 0]; +/// `has_tr_16x8` (`reconintra.c`), transcribed verbatim: 16 bytes, row stride 8 bits (`128 / 16`). +const HAS_TR_16X8: [u8; 16] = [255, 0, 85, 0, 119, 0, 85, 0, 127, 0, 85, 0, 119, 0, 85, 0]; +/// `has_bl_16x8` (`reconintra.c`), transcribed verbatim: 16 bytes, row stride 8 bits (`128 / 16`). +const HAS_BL_16X8: [u8; 16] = [254, 84, 254, 16, 254, 84, 254, 0, 254, 84, 254, 16, 254, 84, 254, 0]; +/// `has_tr_16x32` (`reconintra.c`), transcribed verbatim: 4 bytes, row stride 8 bits (`128 / 16`). +const HAS_TR_16X32: [u8; 4] = [255, 119, 127, 119]; +/// `has_bl_16x32` (`reconintra.c`), transcribed verbatim: 4 bytes, row stride 8 bits (`128 / 16`). +const HAS_BL_16X32: [u8; 4] = [16, 0, 16, 0]; +/// `has_tr_32x16` (`reconintra.c`), transcribed verbatim: 4 bytes, row stride 4 bits (`128 / 32`). +const HAS_TR_32X16: [u8; 4] = [15, 5, 7, 5]; +/// `has_bl_32x16` (`reconintra.c`), transcribed verbatim: 4 bytes, row stride 4 bits (`128 / 32`). +const HAS_BL_32X16: [u8; 4] = [78, 14, 78, 14]; +/// `has_tr_32x64` (`reconintra.c`), transcribed verbatim: 1 bytes, row stride 4 bits (`128 / 32`). +const HAS_TR_32X64: [u8; 1] = [127]; +/// `has_bl_32x64` (`reconintra.c`), transcribed verbatim: 1 bytes, row stride 4 bits (`128 / 32`). +const HAS_BL_32X64: [u8; 1] = [0]; +/// `has_tr_64x32` (`reconintra.c`), transcribed verbatim: 1 bytes, row stride 2 bits (`128 / 64`). +const HAS_TR_64X32: [u8; 1] = [19]; +/// `has_bl_64x32` (`reconintra.c`), transcribed verbatim: 1 bytes, row stride 2 bits (`128 / 64`). +const HAS_BL_64X32: [u8; 1] = [34]; +/// `has_tr_4x16` (`reconintra.c`), transcribed verbatim: 32 bytes, row stride 32 bits (`128 / 4`). +const HAS_TR_4X16: [u8; 32] = [255, 255, 255, 255, 127, 127, 127, 127, 255, 127, 255, 127, 127, 127, 127, 127, 255, 255, 255, 127, 127, 127, 127, 127, 255, 127, 255, 127, 127, 127, 127, 127]; +/// `has_bl_4x16` (`reconintra.c`), transcribed verbatim: 32 bytes, row stride 32 bits (`128 / 4`). +const HAS_BL_4X16: [u8; 32] = [0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0]; +/// `has_tr_16x4` (`reconintra.c`), transcribed verbatim: 32 bytes, row stride 8 bits (`128 / 16`). +const HAS_TR_16X4: [u8; 32] = [255, 0, 0, 0, 85, 0, 0, 0, 119, 0, 0, 0, 85, 0, 0, 0, 127, 0, 0, 0, 85, 0, 0, 0, 119, 0, 0, 0, 85, 0, 0, 0]; +/// `has_bl_16x4` (`reconintra.c`), transcribed verbatim: 32 bytes, row stride 8 bits (`128 / 16`). +const HAS_BL_16X4: [u8; 32] = [254, 254, 254, 84, 254, 254, 254, 16, 254, 254, 254, 84, 254, 254, 254, 0, 254, 254, 254, 84, 254, 254, 254, 16, 254, 254, 254, 84, 254, 254, 254, 0]; +/// `has_tr_8x32` (`reconintra.c`), transcribed verbatim: 8 bytes, row stride 16 bits (`128 / 8`). +const HAS_TR_8X32: [u8; 8] = [255, 255, 127, 127, 255, 127, 127, 127]; +/// `has_bl_8x32` (`reconintra.c`), transcribed verbatim: 8 bytes, row stride 16 bits (`128 / 8`). +const HAS_BL_8X32: [u8; 8] = [0, 1, 0, 0, 0, 1, 0, 0]; +/// `has_tr_32x8` (`reconintra.c`), transcribed verbatim: 8 bytes, row stride 4 bits (`128 / 32`). +const HAS_TR_32X8: [u8; 8] = [15, 0, 5, 0, 7, 0, 5, 0]; +/// `has_bl_32x8` (`reconintra.c`), transcribed verbatim: 8 bytes, row stride 4 bits (`128 / 32`). +const HAS_BL_32X8: [u8; 8] = [238, 78, 238, 14, 238, 78, 238, 14]; +/// `has_tr_16x64` (`reconintra.c`), transcribed verbatim: 2 bytes, row stride 8 bits (`128 / 16`). +const HAS_TR_16X64: [u8; 2] = [255, 127]; +/// `has_bl_16x64` (`reconintra.c`), transcribed verbatim: 2 bytes, row stride 8 bits (`128 / 16`). +const HAS_BL_16X64: [u8; 2] = [0, 0]; +/// `has_tr_64x16` (`reconintra.c`), transcribed verbatim: 2 bytes, row stride 2 bits (`128 / 64`). +const HAS_TR_64X16: [u8; 2] = [3, 1]; +/// `has_bl_64x16` (`reconintra.c`), transcribed verbatim: 2 bytes, row stride 2 bits (`128 / 64`). +const HAS_BL_64X16: [u8; 2] = [42, 42]; /// The `has_tr_*`/`has_bl_*` row for one rectangular block shape. fn rect_reach_tables(bw: usize, bh: usize) -> (&'static [u8], &'static [u8]) { match (bw, bh) { (4, 8) => (&HAS_TR_4X8, &HAS_BL_4X8), (8, 4) => (&HAS_TR_8X4, &HAS_BL_8X4), - (16, 32) => (HAS_TOP_RIGHT_RECT[1], HAS_BOTTOM_LEFT_RECT[1]), - _ => (HAS_TOP_RIGHT_RECT[0], HAS_BOTTOM_LEFT_RECT[0]), + (8, 16) => (&HAS_TR_8X16, &HAS_BL_8X16), + (16, 8) => (&HAS_TR_16X8, &HAS_BL_16X8), + (16, 32) => (&HAS_TR_16X32, &HAS_BL_16X32), + (32, 16) => (&HAS_TR_32X16, &HAS_BL_32X16), + (32, 64) => (&HAS_TR_32X64, &HAS_BL_32X64), + (64, 32) => (&HAS_TR_64X32, &HAS_BL_64X32), + (4, 16) => (&HAS_TR_4X16, &HAS_BL_4X16), + (16, 4) => (&HAS_TR_16X4, &HAS_BL_16X4), + (8, 32) => (&HAS_TR_8X32, &HAS_BL_8X32), + (32, 8) => (&HAS_TR_32X8, &HAS_BL_32X8), + (16, 64) => (&HAS_TR_16X64, &HAS_BL_16X64), + (64, 16) => (&HAS_TR_64X16, &HAS_BL_64X16), + _ => unreachable!("no libaom has_tr/has_bl table for a {bw}x{bh} block"), } } @@ -1164,7 +1208,7 @@ impl Reach { return false; } let table = rect_reach_tables(bw, bh).0; - Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8)) + Self::bit(table, row * Self::table_stride(bw) + col) } fn bottom_left_rect(bw: usize, bh: usize, x: usize, y: usize) -> bool { @@ -1176,7 +1220,7 @@ impl Reach { return false; } let table = rect_reach_tables(bw, bh).1; - Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8)) + Self::bit(table, row * Self::table_stride(bw) + col) } /// Neither, which is all a mode that reads no further than its own edges @@ -3118,11 +3162,124 @@ pub fn encode_sequence( #[cfg(test)] mod tests { + use super::*; 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}; + /// Every rectangular shape this decoder codes, at every position a block + /// of that shape can sit inside a 64x64 superblock: [`Reach::of_rect`] + /// must answer exactly what libaom's `has_top_right`/`has_bottom_left` + /// (`reconintra.c`) answer, whose bodies are ported below. + /// + /// lane-tx4x8 r3 ([[enumerate-table-domain]], [[tool-disabled-in-every-gate]]): + /// `rect_reach_tables` used to route every shape but 4x8/8x4/16x32 to the + /// 32x16 row, so live 16x8 strips read the wrong bit at 10 (above-right) + /// and 7 (below-left) of the 21 reachable positions with no gate seeing + /// it. The per-shape `(len, byte sum)` fingerprints below come from the + /// oracle's own arrays, so a re-routed or truncated table fails here even + /// where the two rows happen to agree. + /// + /// Not covered (no parameter for either on `of_rect`): chroma `ss_x`/ + /// `ss_y`, and sub-block transforms (`row_off`/`col_off` > 0), which the + /// callers handle by predicting each transform unit as its own block. + /// `PARTITION_VERT_A`/`VERT_B` need no arm either: libaom's + /// `has_tr_vert_tables`/`has_bl_vert_tables` entry for every RECT bsize is + /// either the plain table itself (4x8, 8x16, 16x32, 32x64) or NULL + /// (the horizontal shapes, which those partitions never produce). + #[test] + fn every_rect_shape_reaches_what_libaom_says_over_the_whole_superblock() { + // (bw, bh, has_tr len, has_tr byte sum, has_bl len, has_bl byte sum). + const SHAPES: [(usize, usize, usize, u32, usize, u32); 14] = [ + (4, 8, 64, 9280, 64, 550), + (8, 4, 64, 3712, 64, 9630), + (8, 16, 16, 2352, 16, 134), + (16, 8, 16, 960, 16, 2400), + (16, 32, 4, 620, 4, 32), + (32, 16, 4, 32, 4, 184), + (32, 64, 1, 127, 1, 0), + (64, 32, 1, 19, 1, 34), + (4, 16, 32, 5472, 32, 14), + (16, 4, 32, 960, 32, 6464), + (8, 32, 8, 1400, 8, 2), + (32, 8, 8, 32, 8, 1136), + (16, 64, 2, 382, 2, 0), + (64, 16, 2, 4, 2, 84), + ]; + /// libaom `reconintra.c` `has_top_right`/`has_bottom_left`, ported for + /// a block whose transform covers it whole (`row_off == col_off == 0`, + /// `tx_size == bsize`) and luma (`ss_x == ss_y == 0`) on a 64-pixel + /// superblock (`sb_mi_size == 16`). + fn libaom_reach( + bw: usize, + bh: usize, + x: usize, + y: usize, + width: usize, + height: usize, + ) -> (bool, bool) { + let (bw_log2, bh_log2) = ((bw / 4).ilog2() as usize, (bh / 4).ilog2() as usize); + let (_bw_unit, bh_unit) = (bw / 4, bh / 4); + let sb_mi_size = 16usize; + let blk_row_in_sb = ((y / 4) & (sb_mi_size - 1)) >> bh_log2; + let blk_col_in_sb = ((x / 4) & (sb_mi_size - 1)) >> bw_log2; + let this_blk_index = (blk_row_in_sb << (5 - bw_log2)) + blk_col_in_sb; + let (tr_table, bl_table) = rect_reach_tables(bw, bh); + let bit = |t: &[u8]| (t[this_blk_index / 8] >> (this_blk_index % 8)) & 1 != 0; + // has_top_right: top_available && right_available, then row_off == 0. + let top_right = if y == 0 || x + bw >= width { + false + } else if blk_row_in_sb == 0 { + // (`col_off + tx_wide_unit < plane_bw_unit` above this is + // `bw_unit < bw_unit`, false for a whole-block transform.) + true + } else if ((blk_col_in_sb + 1) << bw_log2) >= sb_mi_size { + false + } else { + bit(tr_table) + }; + // has_bottom_left: bottom_available && left_available, col_off == 0. + let bottom_left = if x == 0 || y + bh >= height { + false + } else if blk_col_in_sb == 0 { + // (`row_off + tx_high_unit < plane_bh_unit` is likewise false.) + (blk_row_in_sb << bh_log2) + bh_unit < sb_mi_size + } else if ((blk_row_in_sb + 1) << bh_log2) >= sb_mi_size { + false + } else { + bit(bl_table) + }; + (top_right, bottom_left) + } + for (bw, bh, tr_len, tr_sum, bl_len, bl_sum) in SHAPES { + let (tr, bl) = rect_reach_tables(bw, bh); + let sum = |t: &[u8]| t.iter().map(|&b| u32::from(b)).sum::(); + assert_eq!( + (tr.len(), sum(tr), bl.len(), sum(bl)), + (tr_len, tr_sum, bl_len, bl_sum), + "rect_reach_tables({bw}, {bh}) is not libaom's has_tr_{bw}x{bh}/has_bl_{bw}x{bh}" + ); + // Two superblocks across and down, so the superblock-relative + // position (and not the frame position) is what decides. + for sb_y in [0usize, 64] { + for sb_x in [0usize, 64] { + for y in (sb_y..sb_y + 64).step_by(bh) { + for x in (sb_x..sb_x + 64).step_by(bw) { + let got = Reach::of_rect(bw, bh, x, y, 256, 256); + let want = libaom_reach(bw, bh, x, y, 256, 256); + assert_eq!( + (got.above_right, got.below_left), + want, + "{bw}x{bh} at ({x}, {y})" + ); + } + } + } + } + } + } + fn have_ffmpeg() -> bool { Command::new("ffmpeg") .arg("-version") diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 4931dd18..c285c639 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -7423,8 +7423,9 @@ mod tests { /// hard-asserted per firing attempt so neither orientation can ride along /// on the other ([[gate-blind-to-feature]]), and only leaves that coded /// REAL coefficients count -- a skipped leaf never runs the transform. - /// Arms: 8-bit 64x64, an 8-bit `--tile-columns=1` 128x64 (the mi-granular - /// mode maps are per tile), and a 10-bit 64x64. + /// Arms: 8-bit 16x16, an 8-bit `--tile-columns=1` 128x16 (the mi-granular + /// mode maps are per tile), and a 10-bit 16x16 -- 16-pixel fixtures, for + /// the reason the comment inside the loop gives. #[test] fn a_real_aomenc_stream_with_a_sub8_rect_leaf_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_a_sub8_rect_leaf_decodes_pixel_exact"; @@ -7627,6 +7628,234 @@ mod tests { } } + /// lane-tx4x8 r3 ([[enumerate-table-domain]], [[tool-disabled-in-every-gate]]): + /// a 16x8/8x16 intra strip whose DIRECTIONAL mode reads above-right or + /// below-left samples. Which of those the decoder may read is + /// `Reach::of_rect`'s answer, and until this round `rect_reach_tables` + /// had no 16x8 row at all -- every 16x8 strip read libaom's `has_tr_32x16` + /// row instead of `has_tr_16x8`, wrong at 10 of the 21 reachable + /// superblock positions for above-right and 7 for below-left. No gate saw + /// it: the sibling strip gate (`coded_rect_strip_below_16x16`) runs with + /// smooth/paeth/DC modes, which never read past the block, so the wrong + /// bit changed no pixel there. + /// + /// So this gate turns every non-directional intra tool OFF + /// (`--enable-smooth-intra=0 --enable-paeth-intra=0 + /// --enable-filter-intra=0`) at `--min-partition-size=8 + /// --max-partition-size=16 --enable-rect-partitions=1`, and counts, per + /// attempt, the 16x8/8x16 strips whose prediction actually reached past + /// their own width or height ([`decode::rect_strip_reach_hits`], recorded + /// in the predictor's edge fetch). An attempt only counts when that + /// counter moved AND the frame is pixel-exact against ffmpeg. + /// + /// BLOCKED, and `#[ignore]`d for it, not skipped from inside + /// ([[gate-skips-on-its-own-failure]]): the 16x16-level strip decoder + /// refuses every NON-SKIP 16x8/8x16 strip ("a coded (non-skip) HORZ/VERT + /// rect strip below 16x16", `decode.rs`), and that is what real streams + /// contain -- measured on this exact recipe, 38 of 40 attempts died on + /// that refusal and 2 on a sub-16x16 AB partition, at cq 30/45/58 and at + /// `--min-partition-size=16` (which yields no 16x8 at all, aomenc bounds a + /// rect shape by its smaller side). What unblocks it: the lane that ports + /// the coded 16x16-level strip; then drop this attribute. Until then the + /// table routing is pinned by the domain test + /// `encode::tests::every_rect_shape_reaches_what_libaom_says_over_the_whole_superblock` + /// against libaom's own `has_tr_*`/`has_bl_*` arrays. + #[test] + #[ignore = "the coded 16x8/8x16 strip is a live refusal; see the doc above"] + fn a_directional_16x8_strip_reads_the_right_above_right_samples() { + const NAME: &str = "a_directional_16x8_strip_reads_the_right_above_right_samples"; + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + let msg = format!("no aomenc at {}", aomenc_path().display()); + assert!( + std::env::var_os("EC_AV1_REQUIRE_AOMENC").is_none(), + "EC_AV1_REQUIRE_AOMENC is set but {msg}" + ); + eprintln!("SKIP {NAME}: {msg}"); + return; + } + // 64x16, not 64x64: a 16-tall frame is a single row of 16x16 blocks, + // so a HORZ arm is a 16x8 strip at column 0..3 of the superblock -- + // exactly the positions whose `has_tr_16x8`/`has_bl_16x8` bit the old + // routing got wrong -- while the frame is too short for aomenc to + // build the deep sub-16x16 partitions other lanes still refuse. It + // must also be WIDER than one block: at width 16 the block is the + // frame, and `x + bw < width` kills above-right before any table. + for (width, height, bit_depth) in [(64usize, 16usize, 8u8), (64, 16, 10u8)] { + let mut refusals = Vec::new(); + let mut fired_runs = 0u32; + let mut reach_hits = 0usize; + for attempt in 0..40u32 { + let seed = 700 + attempt; + // Coarse quantisers on lightly-dithered content: fine detail + // at low cq makes aomenc build the sub-16x16 partitions other + // lanes refuse, and 40/40 attempts died there. + let cq = 45 + (attempt % 4); + let half = height / 2; + let pix_fmt = if bit_depth == 10 { + "yuv420p10le" + } else { + "yuv420p" + }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &format!("testsrc2=size={width}x{half}:rate=25"), + "-f", + "lavfi", + "-i", + &format!("color=c=gray:s={width}x{half}:rate=25"), + "-filter_complex", + &format!( + "[0]noise=alls=8:allf=t:all_seed={seed}[t];[t][1]vstack,\ + format={pix_fmt}" + ), + "-strict", + "-1", + "-t", + "0.04", + "-f", + "yuv4mpegpipe", + "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let mut child = Command::new(aomenc_path()) + .args([ + "--codec=av1", + "--passes=1", + "--end-usage=q", + &format!("--cq-level={cq}"), + "--cpu-used=0", + "--threads=1", + "--row-mt=0", + "--sb-size=64", + // The tool under test: 16x8/8x16 strips, predicted by + // a mode that reads beyond the block. + // aomenc bounds a RECT shape by its smaller side, so + // `--min-partition-size=16` yields no 16x8 at all + // (measured: 0 strips in 40 attempts); 8 is the + // smallest value that lets a 16x16 split HORZ/VERT. + "--min-partition-size=8", + "--max-partition-size=16", + // Largest transform per block: a split (depth-1) rect + // strip is a named refusal of another lane, and it + // ate every attempt while it was on. + "--enable-tx-size-search=0", + "--enable-rect-partitions=1", + "--enable-directional-intra=1", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-filter-intra=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-restoration=0", + "--enable-cdef=0", + "--loopfilter-control=0", + "--enable-ref-frame-mvs=0", + "--limit=1", + "--obu", + "-o", + "-", + "-", + ]) + .args(if bit_depth == 10 { + &["--input-bit-depth=10", "--bit-depth=10"][..] + } else { + &[][..] + }) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + if !out.status.success() { + refusals.push(format!( + "seed={seed} cq={cq}: aomenc itself refused the fixture" + )); + continue; + } + let stream = out.stdout; + let reach_before = decode::rect_strip_reach_hits(); + let pred_before = decode::rect_strip_pred_hits(); + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => { + let msg = e.to_string(); + assert!( + msg.starts_with("unsupported: "), + "decode error that is not a named refusal \ + (seed={seed} cq={cq} depth={bit_depth}): {msg}" + ); + refusals.push(format!("seed={seed} cq={cq}: {msg}")); + continue; + } + }; + let reached = decode::rect_strip_reach_hits() - reach_before; + let predicted = decode::rect_strip_pred_hits() - pred_before; + if predicted == 0 { + refusals.push(format!( + "seed={seed} cq={cq}: decoded, but no 16x8/8x16 strip in the frame" + )); + continue; + } + let ffmpeg_frames = if bit_depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, 1) + } else { + ffmpeg_decode_sequence(&stream, width, height, 1) + }; + assert_eq!( + frames[0].y, ffmpeg_frames[0].y, + "luma vs ffmpeg (seed={seed} cq={cq} depth={bit_depth}, {predicted} strips, \ + {reached} of them reaching)" + ); + assert_eq!( + frames[0].u, ffmpeg_frames[0].u, + "U vs ffmpeg (seed={seed} cq={cq} depth={bit_depth})" + ); + assert_eq!( + frames[0].v, ffmpeg_frames[0].v, + "V vs ffmpeg (seed={seed} cq={cq} depth={bit_depth})" + ); + fired_runs += 1; + reach_hits += reached; + if fired_runs >= 4 { + break; + } + } + assert!( + fired_runs >= 4, + "fewer than 4 firing+pixel-exact runs out of 40 attempts \ + ({width}x{height} depth={bit_depth}, {reach_hits} reaching strips):\n{}", + refusals.join("\n") + ); + } + } + /// lane-av1golden7 r9's decisive fixture: a real aomenc stream (seed 59, /// `--tune-content=film`, GOLDEN_FRAME firing downstream) whose frame-0 /// *intra keyframe* mismatched ffmpeg on 3855/4096 luma pixels -- traced diff --git a/lanes/COMMON-20260901.md b/lanes/COMMON-20260901.md new file mode 100644 index 00000000..c34ef78b --- /dev/null +++ b/lanes/COMMON-20260901.md @@ -0,0 +1,87 @@ +# COMMON CHARTER PREAMBLE — batch 2026-09-01 (ec-av1 decode, his real films) + +GOAL (why): ec-av1 must decode the user's two real AV1 films bit-exact +(`/home/user/Videos/Films/reelc.mkv`, +`/home/user/Downloads/reelb.mkv`, +both yuv420p10le). 47 refusal strings remain in crates/ec-av1/src/refusal_inventory.rs. +Each lane lifts its cluster of refusals WITH a real-aomenc gate proving it. + +WORKING RULES (binding): +- Work ONLY in your worktree; branch named in your charter. Never touch main, never + merge into main, never `git checkout ` to revert (copy to scratch first). +- `export CARGO_TARGET_DIR=$HOME/.cache/cargo-target-` (named in charter) for EVERY cargo + command. sccache is the global rustc wrapper. Use `nice -n 10 cargo ... -j3` — 10 lanes + run concurrently on 12 cores / 30G RAM. Long/lavfi runs under + `systemd-run --user --scope -p MemoryMax=8G` and bound ffmpeg with `-t`. +- Test scope: `cargo test -p ec-av1 --lib` only (plus `cargo check -p ec-av1`). Never the + workspace suite. Set `EC_AV1_REQUIRE_AOMENC=1` so an absent oracle FAILS, never SKIPs. +- Oracle: `~/.cache/aom-oracle/build/aomenc` and instrumented `aomdec` (scripts/build-aom-oracle.sh, + scripts/instrument-aom-oracle.sh; rungs EC_TRACE=1 / EC_TRACE_COEFF=1 / EC_TRACE_MODE=1 / + EC_AV1_PREFILT_DUMP). Ours: EC_TRACE_COEFF / EC_TRACE_MODE / EC_TRACE_MODE_STEP in decode.rs. + Cross-decoder bisection compares msac RANGE element by element, never tell(). + The reference is at the oracle source tree (find via scripts/build-aom-oracle.sh). +- FIRST STEP, always: `git log --oneline -8 main` and rebase your branch onto main + (`git rebase main`; resolve conflicts, keeping main's fixes). Then RE-MEASURE your lane's + premise before building — charters may carry stale premises. Cite the command that + produced every premise you act on. +- Fixtures: `fixtures` is a symlink to main's fixtures dir (shared). Pin any new stream + fixture under crates/ec-av1/fixtures/ or fixtures/ with a hash; hash an aomenc output + TWICE before trusting a seed (lavfi gradients ignores its seed). +- A refusal is lifted ONLY together with a gate: a real-aomenc stream (tool explicitly + enabled on the aomenc command line) decoded pixel-exact vs ffmpeg/aomdec, that + HARD-ASSERTS a hit counter proving the feature fired, and never turns a decode error or + pixel mismatch into SKIP. Update refusal_inventory.rs + gate_coverage.rs in the same + commit. `refusal_inventory` and `gate_coverage` tests must stay green. +- Defect classes to check first (memory-proven, each cost rounds): CDF row indexed by a + field that moves; table indexed by RAW vs adjusted tx_size; scan/ctx tables transposed + vs spec (libaom layout is not the spec's — gate on asymmetric behaviour); neighbour ctx + read from one cell instead of gathered over the span; a symbol we read that the + reference never wrote (reference range unchanged = unread); a header field parsed then + never consumed; counter-reset list missing a table; call-site arg order; a probe loop + reusing last run's output file (rm -f first, check exit status). +- Tool-call ceiling ~70. If you hit it: commit everything on your branch, write + `lanes/-r.handoff.md` with exact facts (ranges, stream, element) so a fresh + agent continues without re-reading. +- DONE = commit(s) on your branch + `lanes/-r.report.md` in your worktree with: + what changed (path:line), gate name + command + result, refusals lifted, test totals + (`cargo test -p ec-av1 --lib` passed/failed/ignored), and EVIDENCE lines of the form + `EVIDENCE: | | `. State open residue with + fix-now | deferred() | accepted. Never claim "done" beyond what you ran. +- Film check, when your lane targets a film blocker: extract 0.4s with + `ffmpeg -t 0.4 -i -c:v copy -f obu .obu` (bounded), run + `cargo run -p ec-av1 --example decode_probe -- .obu` before and after; report + the refusal string each film now stops at. + +VERIFIERS (added 2026-09-01 22:40 after a verifier left a lane worktree on a detached HEAD +while its builder was mid-round): a verifier NEVER runs `git checkout`, `git switch`, +`git stash` or any tree-changing git command inside a lane worktree. To test a specific +commit, `git worktree add --detach /tmp/claude-1000/.../scratchpad/verify- ` with its +own CARGO_TARGET_DIR, and remove it when done. + +NEIGHBOUR MAPS (added 2026-09-02 00:50, class new-map-ignores-tile-edge): any per-mi side band +you add or inherit (mode, uv_mode, skip, ref, filter, txfm ctx, palette) MUST (1) guard +availability with `mi_r > tile_row0_mi` / `mi_c > tile_col0_mi`, never `> 0`; (2) be reset in +start_tile AND start_row; (3) have a `--tile-columns=1` arm in the lane's gate. Gate Err arms +tolerate ONLY messages containing "unsupported"; lavfi sources carry an explicit seed +(noise=all_seed=N) and the report shows two identical hashes. + +SIBLING GATES (added 2026-09-02 03:00, rectsplit r3): a root-cause fix that turns YOUR gate green +is not done until every gate that exercises the same tables/paths (grep the table name and the +function in stream.rs) is re-run — rectsplit r2 shipped a transposition that fixed its gate and +broke the merged SB HORZ/VERT gate at seed 43. Run the siblings before writing "root cause". + +FORMAT + LOGS (added 2026-09-02 03:30): never run a repo-wide `cargo fmt` in a lane (txselect +carried 5000 lines of churn that the merge had to strip; format only files you touched, or +not at all). Suite logs go to a PRIVATE path (`$CARGO_TARGET_DIR/../-suite.log`), never +a shared scratchpad name — another session overwrote a verifier's log mid-run. + +## AOMENC FLAG PRECEDENCE (measured 2026-09-02 08:45, md5 of 4 orderings) +aomenc keeps the LAST occurrence of a repeated `--enable-*` flag. Per-arm overrides go AFTER the +base recipe. Every gate proves its flag arrived through a decoder-side counter or a header field; +a counter at 0 means the flag did not arrive or the encoder declined — never weaken the assert. + +## SUITE RUNS (3 self-kills on 2026-09-02) +Start the full suite detached: `setsid -f nohup env ... cargo test ... > $HOME/.cache/-suite.log 2>&1` +or via the Bash tool's run_in_background. NEVER wait on it with a foreground `until/while ... sleep` loop +or `tail --pid`: the 10-minute foreground cap SIGTERMs the whole process group (rc 144, no `test result` +line). Check the log with single instantaneous `grep` calls between other work. diff --git a/lanes/tx4x8-r3.report.md b/lanes/tx4x8-r3.report.md new file mode 100644 index 00000000..5391e29c --- /dev/null +++ b/lanes/tx4x8-r3.report.md @@ -0,0 +1,116 @@ +# lane-tx4x8 r3 -- every rect shape gets its own has_tr/has_bl row + +## Root cause (verifier's finding, confirmed) + +`encode.rs` `rect_reach_tables(bw, bh)` had rows for 4x8, 8x4 and 16x32 only and +a `_` arm that routed **every other rectangular shape** to libaom's +`has_tr_32x16`/`has_bl_32x16`. 16x8 strips are live (`decode_leaf_rect`, +`decode.rs`), and neither the old `HAS_TOP_RIGHT_RECT[bw == 16]` selection nor +the r2 `_` arm matches `has_tr_16x8`/`has_bl_16x8` (`reconintra.c:96` / `:282`, +16 bytes each): over the 21 reachable superblock positions, has_tr was wrong at +14 (old) / 10 (r2) and has_bl at 14 / 7. Class: +[[enumerate-table-domain]] + [[tool-disabled-in-every-gate]]. + +## Fix + +* `encode.rs`: all 14 rect shapes this decoder codes or will code (4x8, 8x4, + 8x16, 16x8, 16x32, 32x16, 32x64, 64x32, 4x16, 16x4, 8x32, 32x8, 16x64, 64x16) + now have their own `HAS_TR_*`/`HAS_BL_*` const, **generated** from + `~/.cache/aom-oracle/src/av1/common/reconintra.c` (no byte retyped), and + `rect_reach_tables` is an exhaustive match ending in + `unreachable!("no libaom has_tr/has_bl table for a {bw}x{bh} block")`. +* Index rule verified per shape: libaom's + `(blk_row_in_sb << (5 - bw_in_mi_log2)) + blk_col_in_sb` == row stride + `128 / bw` == the existing `Reach::table_stride(bw)`. For a 64-pixel + superblock the largest index any shape can produce is + `(64/bh - 1) * (128/bw) + (64/bw - 1)`, which is inside every one of the 14 + oracle tables -- so the `% (table.len() * 8)` wrap the rect path used to + apply is not just unnecessary, it was masking exactly this kind of + mis-routing; it is gone (the square path keeps its own). + +## Check 1 -- domain test (runnable, green) + +`encode::tests::every_rect_shape_reaches_what_libaom_says_over_the_whole_superblock`: +for all 14 shapes, at every position a block of that shape can sit in a 64x64 +superblock (and in a second superblock at (64,64), so SB-relative position is +what decides), `Reach::of_rect` == a port of libaom's `has_top_right` / +`has_bottom_left` bodies incl. every early exit (top/right/bottom/left +availability, `blk_row_in_sb == 0`, rightmost column, leftmost column's +`row_off_in_sb + count < sb_height_unit`, bottom row). Each shape's table is +additionally pinned by a `(len, byte sum)` fingerprint taken from the oracle's +arrays, so a re-routed or truncated row fails even where two rows agree. + +Not covered, stated rather than implied: chroma `ss_x`/`ss_y` and sub-block +transforms (`row_off`/`col_off` > 0) -- `of_rect` takes neither parameter +(callers predict each transform unit as its own block); `PARTITION_VERT_A/B` +needs no arm because libaom's `has_tr_vert_tables` entry for every *rect* bsize +is either the plain table (4x8, 8x16, 16x32, 32x64) or NULL. + + cd /home/user/Documents/Code/Rust/edith_codecs-tx4x8 + CARGO_TARGET_DIR=$HOME/.cache/cargo-target-tx4x8 EC_NOMEMGUARD=1 \ + cargo test -p ec-av1 --lib -j3 every_rect_shape + +EVIDENCE: $HOME/.cache/tx4x8-suite-r3.log | cargo test -p ec-av1 --lib every_rect_shape | 1 passed, 0 failed + +MUTATION (the check that says the test has teeth): restoring r2's routing for +one shape -- `(16, 8) => (&HAS_TR_32X16, &HAS_BL_32X16)` -- fails it: + + assertion `left == right` failed: rect_reach_tables(16, 8) is not libaom's has_tr_16x8/has_bl_16x8 + left: (4, 32, 4, 184) + right: (16, 960, 16, 2400) + +## Check 2 -- the asymmetric strip stream gate: BLOCKED, `#[ignore]`d + +`stream::tests::a_directional_16x8_strip_reads_the_right_above_right_samples` +is written in full (directional-only intra: `--enable-smooth-intra=0 +--enable-paeth-intra=0 --enable-filter-intra=0 --enable-directional-intra=1 +--enable-rect-partitions=1 --enable-ab-partitions=0`, 64x16 fixture whose +bottom half is flat so a HORZ arm can be a skip strip, 8-bit and 10-bit arms, +per-attempt counters `decode::rect_strip_pred_hits` / `rect_strip_reach_hits` +recorded in the predictor's edge fetch, pixel-exact vs ffmpeg) but it cannot +pass today and is `#[ignore]`d with the reason in its doc rather than skipping +from inside ([[gate-skips-on-its-own-failure]]). + +Why: the 16x16-level strip decoder refuses every **non-skip** 16x8/8x16 strip +("a coded (non-skip) HORZ/VERT rect strip below 16x16"), which is what real +streams contain. Measured on this recipe, per 40 attempts: + +| recipe | outcome | +| --- | --- | +| `--min-partition-size=16 --max-partition-size=16` | 40/40 decode, **0** 16x8/8x16 strips (aomenc bounds a rect shape by its smaller side, so 16x8 is never searched) | +| `--min-partition-size=8`, cq 8..20, noise 30, 64x64 | 40/40 refused (split-transform strip / non-skip strip / sub-16x16 AB) | +| `--min-partition-size=8 --enable-tx-size-search=0`, cq 30/45/55/62, noise 4 and 15, 64x16 | 0 firing; at cq 45 with the half-flat fixture: **38/40 "coded (non-skip) rect strip below 16x16"**, 2/40 sub-16x16 AB | + +MUTATION for this gate is therefore not reportable at stream level: with the +old `_` routing the gate fails for the same reason it fails with the fix (no +attempt gets past the refusal). The mutation evidence for the fix is the +domain-test one above. + +deferred: the stream-level pixel gate for 16x8/8x16 reach -- blocked by the +live "coded (non-skip) HORZ/VERT rect strip below 16x16" refusal -- unblocked +by the lane that ports the coded 16x16-level strip; then delete the +`#[ignore]` attribute (nothing else in the test changes). + +## Also + +* `stream.rs` doc of the sub-8x8 rect gate said "Arms: 8-bit 64x64, ... 128x64, + ... 10-bit 64x64"; the arms are 16x16 / 128x16 / 16x16. Fixed. +* New counters `rect_strip_pred_hits` / `rect_strip_reach_hits` in `decode.rs` + (bumped in `Plane::edges_rect`) are the instrument the blocked gate needs the + day the refusal lifts. + +## Suite + + CARGO_TARGET_DIR=$HOME/.cache/cargo-target-tx4x8 EC_NOMEMGUARD=1 \ + EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 -j3 + +275 passed, 0 failed, 23 ignored (r2: 274 / 0 / 22 -- +1 = the domain test, ++1 ignored = the blocked gate). Log: `$HOME/.cache/tx4x8-suite-r3.log`. + +## Merge note + +This round deletes `HAS_TOP_RIGHT_RECT` / `HAS_BOTTOM_LEFT_RECT` (the 2-row +arrays lane-tx64x16 r3 extends to 4 rows with its own 32x8/8x32 entries). On a +merge, keep this branch's per-shape consts -- `HAS_TR_32X8` / `HAS_BL_32X8` / +`HAS_TR_8X32` / `HAS_BL_8X32` are already here, byte-identical to the oracle -- +and drop the array form together with `Reach::rect_table`.