diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index e1a55089..e26bd9c2 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -266,18 +266,36 @@ pub const EOB_PT_128_CHROMA_Q3: [u16; 9] = [ 24313, 26062, 28385, 30107, 31217, 31898, 32345, 32768, 0, ]; -/// `av1_default_eob_multi512_cdfs[*][1][*]` (`token_cdfs.h:874-901`, -/// lane-sbpart): the end-of-block group of a CHROMA transform with 512 coded -/// positions -- what a 32x16/16x32 chroma transform has (the chroma plane of -/// an intra `PARTITION_HORZ`/`VERT` 64x32/32x64 superblock strip, whose luma -/// coefficients truncate to the 64x64-superblock's own 32x32 corner-scan -/// (`TxbSet::Luma64`) but whose *chroma* plane, at 32x16, is small enough -/// that no truncation applies). Unlike every other `EOB_PT_*` table here, -/// libaom's default is the exact same flat distribution at all four -/// q-contexts and both `eob_multi_size` selectors (`token_cdfs.h` rows), so -/// one constant covers every q-context -- no `_Q0`/`_Q1`/`_Q3` siblings. +/// `av1_default_eob_multi512_cdfs[2][1][0]` (`token_cdfs.h:874-901`, +/// lane-part32 r4): the end-of-block group of a CHROMA transform with 512 +/// coded positions -- what a 32x16/16x32 chroma transform has (the chroma +/// plane of an intra `PARTITION_HORZ`/`VERT` 64x32/32x64 superblock strip, +/// whose luma coefficients truncate to the 64x64-superblock's own 32x32 +/// corner-scan (`TxbSet::Luma64`) but whose *chroma* plane, at 32x16, is +/// small enough that no truncation applies). +/// +/// lane-sbpart originally transcribed the `[1][1]` row -- the +/// `TX_CLASS_HORIZ`/`VERT` (1D) sibling, whose default IS the flat +/// `3277, 6554, ...` ramp at every q-context -- and wrote that flatness into +/// this comment as a property of the whole table. The 2D row this decoder +/// actually reads is q-dependent like every other `EOB_PT_*` family, so a +/// real `--cq-level=45` stream (q-context 3) read the wrong CDF and desynced +/// the tile at the first 32x16 chroma transform of the first SB-level +/// HORZ/VERT strip. pub const EOB_PT_512_CHROMA: [u16; 11] = [ - 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, + 12015, 14769, 19588, 22052, 24222, 25812, 27300, 29219, 32114, 32768, 0, +]; +/// [`EOB_PT_512_CHROMA`], q-context 0 (`base_q_idx` 0..=20). +pub const EOB_PT_512_CHROMA_Q0: [u16; 11] = [ + 5095, 6446, 9996, 13354, 16017, 17986, 20919, 26129, 29140, 32768, 0, +]; +/// [`EOB_PT_512_CHROMA`], q-context 1 (`base_q_idx` 21..=60). +pub const EOB_PT_512_CHROMA_Q1: [u16; 11] = [ + 7265, 9979, 15819, 19250, 21780, 23846, 26478, 28396, 31811, 32768, 0, +]; +/// [`EOB_PT_512_CHROMA`], q-context 3 (`base_q_idx` 121..=255). +pub const EOB_PT_512_CHROMA_Q3: [u16; 11] = [ + 21093, 23043, 25742, 27658, 29097, 29716, 30073, 30820, 31956, 32768, 0, ]; /// `Default_Dc_Sign_Cdf[2][0]` (spec 9.4): the sign of a luma DC coefficient, @@ -783,22 +801,39 @@ pub const NZ_MAP_CTX_OFFSET_32: [[u8; 5]; 5] = [ /// indexes this position table by the raw, un-adjusted `tx_size` -- lane-sbpart /// r8 root cause) is genuinely rectangular, not square. Distinct from /// [`NZ_MAP_CTX_OFFSET_32`] starting at `(row=1, col=0)`. +// lane-part32 r3: this table was TRANSPOSED (row/col swapped) as landed by +// lane-sbpart r8 -- caught by tracing a real aomenc pin against a patched +// aomdec (added per-symbol `EC_TRACE_COEFF` ctx prints to +// `read_coeffs_reverse{,_2d}` in `~/.cache/aom-oracle`), which showed our own +// `(row=2, col=0)` reading ctx-offset 11 where the real decoder's equivalent +// position read 6. Re-derived straight from libaom's own generating +// comment in `get_nz_map_ctx_from_stats` (`txb_common.h:199-210`): for +// `width < height` (this table, `TX_32X64`), ctx is `11 +` for `row < 2` +// *regardless of col* -- the old table instead special-cased `col < 2`, +// which is the `width > height` (`TX_64X32`) branch. Confirmed against the +// real `av1_nz_map_ctx_offset_32x64`/`_64x32` C arrays in +// `av1/common/txb_common.c`, decoded through libaom's own col-major +// `pos = col*32+row` raster (same convention [`SCAN_32X16`]'s doc comment +// already documents this decoder transcribes out of). pub const NZ_MAP_CTX_OFFSET_32X64: [[u8; 5]; 5] = [ - [0, 11, 6, 6, 21], - [11, 11, 6, 21, 21], - [11, 11, 21, 21, 21], - [11, 11, 21, 21, 21], - [11, 11, 21, 21, 21], + [0, 11, 11, 11, 11], + [11, 11, 11, 11, 11], + [6, 6, 21, 21, 21], + [6, 21, 21, 21, 21], + [21, 21, 21, 21, 21], ]; /// [`NZ_MAP_CTX_OFFSET_32X64`]'s `TX_64X32` counterpart (libaom /// `av1_nz_map_ctx_offset_64x32`): a `PARTITION_HORZ` strip's luma corner. +/// `width > height` here, so the `col < 2` branch (offset 16) fires +/// regardless of row -- transpose of [`NZ_MAP_CTX_OFFSET_32X64`]'s `row < 2` +/// branch, same r3 fix (see that table's doc comment). pub const NZ_MAP_CTX_OFFSET_64X32: [[u8; 5]; 5] = [ - [0, 16, 16, 16, 16], - [16, 16, 16, 16, 16], - [6, 6, 21, 21, 21], - [6, 21, 21, 21, 21], - [21, 21, 21, 21, 21], + [0, 16, 6, 6, 21], + [16, 16, 6, 21, 21], + [16, 16, 21, 21, 21], + [16, 16, 21, 21, 21], + [16, 16, 21, 21, 21], ]; /// `Default_Txb_Skip_Cdf[2][2][7..10]` (spec 9.4): the all-zero flag of a diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 53f1c13b..866988bc 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -1026,9 +1026,13 @@ impl Cdfs { cdf::EOB_PT_128_CHROMA, cdf::EOB_PT_128_CHROMA_Q3, ), - // Flat default at every q-context (see the constant's own doc - // comment) -- no `pick()` needed. - eob_pt_512_chroma: cdf::EOB_PT_512_CHROMA, + eob_pt_512_chroma: pick( + q_ctx, + cdf::EOB_PT_512_CHROMA_Q0, + cdf::EOB_PT_512_CHROMA_Q1, + cdf::EOB_PT_512_CHROMA, + cdf::EOB_PT_512_CHROMA_Q3, + ), eob_extra_luma_32: pick( q_ctx, cdf::EOB_EXTRA_LUMA_32_Q0, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b5be7b50..a2bd3db0 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1197,6 +1197,29 @@ pub(crate) fn sb_rect_hits() -> usize { SB_RECT_HITS.with(|c| c.get()) } +// lane-part32 r4: how many superblock-level AB blocks (`PARTITION_HORZ_A`/ +// `_B`/`VERT_A`/`_B` at 64x64 -- two 32x32 squares plus one 64x32/32x64 +// strip) fired. Real aomenc picks these at 64 even with +// `--enable-ab-partitions=0` (lane-sbpart r2's own measurement), so this is +// the counter the gate hard-asserts. +thread_local! { + static SB_AB_HITS: std::cell::Cell<[usize; 4]> = + const { std::cell::Cell::new([0; 4]) }; +} + +/// Current value of [`SB_AB_HITS`], summed over the four arms. +pub(crate) fn sb_ab_hits() -> usize { + SB_AB_HITS.with(|c| c.get().iter().sum()) +} + +/// [`SB_AB_HITS`] split per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, +/// `_VERT_A`, `_VERT_B` order -- what the gate hard-asserts arm by arm, so a +/// run where aomenc's RD only ever picked (say) `HORZ_A` cannot pass off as +/// proof of all four. +pub(crate) fn sb_ab_hits_by_arm() -> [usize; 4] { + SB_AB_HITS.with(std::cell::Cell::get) +} + // lane-rect64q r1: how many of [`decode_block_rect64`]'s three per-plane // dequant calls actually observed `CURRENT_Q_IDX != base_q_idx` -- proof the // running-vs-stale-snapshot bug this round fixed is exercised by a gate, not @@ -1226,6 +1249,38 @@ pub(crate) fn partab_hits() -> usize { PARTAB_HITS.with(|c| c.get()) } +// lane-part32 r1: how many 32x32 quadrants decoded each INTRA AB/4-way arm, +// one counter per arm rather than one shared total -- the gate hard-asserts +// each of these individually nonzero so a recipe that only ever fires +// HORZ_A can't silently stand in for the whole set. +thread_local! { + static INTRA_HORZ_A_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; + static INTRA_HORZ_B_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; + static INTRA_VERT_A_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; + static INTRA_VERT_B_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`INTRA_HORZ_A_HITS`]. +pub(crate) fn intra_horz_a_hits() -> usize { + INTRA_HORZ_A_HITS.with(|c| c.get()) +} +/// Current value of [`INTRA_HORZ_B_HITS`]. +pub(crate) fn intra_horz_b_hits() -> usize { + INTRA_HORZ_B_HITS.with(|c| c.get()) +} +/// Current value of [`INTRA_VERT_A_HITS`]. +pub(crate) fn intra_vert_a_hits() -> usize { + INTRA_VERT_A_HITS.with(|c| c.get()) +} +/// Current value of [`INTRA_VERT_B_HITS`]. +pub(crate) fn intra_vert_b_hits() -> usize { + INTRA_VERT_B_HITS.with(|c| c.get()) +} + // How many [`read_inter_compound_mode`] reads actually happened // (lane-av1comp) -- proves a `COMPOUND_REFERENCE` block reached its own // `compound_mode` symbol (past `comp_mode`/`comp_ref` and the compound @@ -1777,7 +1832,7 @@ fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> (TxClass::TwoD, _) | (_, None) => coding.eob_pt, (_, Some(class1)) => class1, }; - if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() && eob_pt.len() == 12 { + if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { let (range, value) = dec.debug_state(); eprintln!("EC_AV1_EOBPT_CDF {eob_pt:?} range={range} value={value}"); } @@ -2054,8 +2109,16 @@ fn read_coeffs_rect( sign_ctx: usize, default_tx_type: TxType, ) -> Result<(Vec, TxType)> { + let ec_trace_coeff = std::env::var_os("EC_TRACE_COEFF").is_some(); let mut grid = vec![0i32; w * h]; let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; + if ec_trace_coeff { + let (rng, _) = dec.debug_state(); + eprintln!( + "EC_COEFF_STEP tag=all_zero ctx={skip_ctx} all_zero={} rng={rng}", + all_zero as i32 + ); + } if all_zero { return Ok((grid, TxType::DctDct)); } @@ -2071,16 +2134,35 @@ fn read_coeffs_rect( )); } let eob = read_eob(dec, coding, TxClass::TwoD); + if ec_trace_coeff { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF_STEP tag=eob eob={eob} rng={rng}"); + } let mut levels = vec![0i32; w * h]; for scan_idx in (0..eob).rev() { let pos = scan[scan_idx] as usize; let (row, col) = (pos / w, pos % w); let level = if scan_idx == eob - 1 { let ctx = eob_coeff_ctx(scan_idx, w * h); - dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1 + let l = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; + if ec_trace_coeff { + let (rng, _) = dec.debug_state(); + eprintln!( + "EC_COEFF_STEP tag=base_eob c={scan_idx} pos={pos} ctx={ctx} level={} rng={rng}", + l - 1 + ); + } + l } else { let ctx = base_ctx_rect(&levels, w, h, row, col); - dec.symbol(&mut coding.base[ctx]) as i32 + let l = dec.symbol(&mut coding.base[ctx]) as i32; + if ec_trace_coeff { + let (rng, _) = dec.debug_state(); + eprintln!( + "EC_COEFF_STEP tag=base c={scan_idx} pos={pos} ctx={ctx} level={l} rng={rng}" + ); + } + l }; let level = if level > NUM_BASE_LEVELS { let ctx = br_ctx_rect(&levels, w, h, row, col); @@ -2088,6 +2170,12 @@ fn read_coeffs_rect( let mut sent = 0; loop { let k = dec.symbol(&mut coding.br[ctx]) as i32; + if ec_trace_coeff { + let (rng, _) = dec.debug_state(); + eprintln!( + "EC_COEFF_STEP tag=br c={scan_idx} pos={pos} ctx={ctx} k={k} rng={rng}" + ); + } level += k; sent += BR_STEP; if k < BR_STEP || sent >= COEFF_BASE_RANGE { @@ -2100,6 +2188,10 @@ fn read_coeffs_rect( }; levels[pos] = level; } + if ec_trace_coeff { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); + } for &pos in &scan[..eob] { let level = levels[pos as usize]; if level == 0 { @@ -5270,6 +5362,15 @@ impl PlaneBuf { smooth_neighbor: bool, ) { let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); + if std::env::var_os("EC_DEBUG_EDGES").is_some() { + eprintln!( + "EDGES x={x} y={y} bw={bw} bh={bh} mode={mode} above={:?} left_len={:?} left0={:?} corner={corner:?} tx0={} ty0={} truew={} trueh={}", + above.as_ref().map(|a| (a.len(), a[0])), + left.as_ref().map(|l| l.len()), + left.as_ref().map(|l| l[0]), + self.tile_x0, self.tile_y0, self.true_width, self.true_height + ); + } let mut prediction = vec![0u16; bw * bh]; if let Some(fi_mode) = filter_intra { crate::intra::predict_filter_intra( @@ -6507,6 +6608,11 @@ fn apply_deblock( frame_width: usize, frame_height: usize, ) { + // lane-part32 r2 debug rung, env-gated: bisecting a widespread pixel + // mismatch between the raw reconstruction and post-filter output. + if std::env::var_os("EC_AV1_DEBUG_SKIP_DEBLOCK").is_some() { + return; + } if lf.level[0] != 0 || lf.level[1] != 0 { deblock_plane(y, 0, lf, n, frame_width, frame_height); } @@ -7155,6 +7261,10 @@ fn apply_cdef( cdef: &CdefParams, skip_grid: &Neighbours, ) { + // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. + if std::env::var_os("EC_AV1_DEBUG_SKIP_CDEF").is_some() { + return; + } // `cdef.bits == 0` still means "no filtering at all" whenever the single // (index 0) strength pair is all-zero -- the fast path every existing // gate stream takes. A `bits > 0` frame can still be all-zero-strength @@ -8181,6 +8291,307 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( reduced_tx_set, )?; } + PARTITION_HORZ_A => { + // lane-part32 r1: two 16x16 squares on top + + // a true 32x16 strip below (mirrors + // decode_inter_block's cd6cb6d HORZ_A: TL, + // TR, bottom strip). + INTRA_HORZ_A_HITS.with(|c| c.set(c.get() + 1)); + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + at32, + SUB, + TxbSet::Luma16, + TxbSet::Chroma8, + TX16, + TX8, + (&scan16, &scan8), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )?; + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + (at32.0, at32.1 + 1), + SUB, + TxbSet::Luma16, + TxbSet::Chroma8, + TX16, + TX8, + (&scan16, &scan8), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )?; + decode_block_rect( + &mut dec, + &mut cdfs, + &mut neighbours, + (at32.0 + 1, at32.1), + 32, + 16, + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + reduced_tx_set, + )?; + } + PARTITION_HORZ_B => { + // lane-part32 r1: a true 32x16 strip on top + + // two 16x16 squares below (mirrors HORZ_A + // with the strip/squares order flipped, same + // shape as libaom decode_partition's HORZ_B). + INTRA_HORZ_B_HITS.with(|c| c.set(c.get() + 1)); + decode_block_rect( + &mut dec, + &mut cdfs, + &mut neighbours, + at32, + 32, + 16, + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + reduced_tx_set, + )?; + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + (at32.0 + 1, at32.1), + SUB, + TxbSet::Luma16, + TxbSet::Chroma8, + TX16, + TX8, + (&scan16, &scan8), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )?; + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + (at32.0 + 1, at32.1 + 1), + SUB, + TxbSet::Luma16, + TxbSet::Chroma8, + TX16, + TX8, + (&scan16, &scan8), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )?; + } + PARTITION_VERT_A => { + // lane-part32 r1: mirror of HORZ_A with + // width/height swapped (TL, BL, right 16x32 + // strip). + INTRA_VERT_A_HITS.with(|c| c.set(c.get() + 1)); + let _vert_ab = crate::encode::Reach::vert_ab_partition(); + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + at32, + SUB, + TxbSet::Luma16, + TxbSet::Chroma8, + TX16, + TX8, + (&scan16, &scan8), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )?; + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + (at32.0 + 1, at32.1), + SUB, + TxbSet::Luma16, + TxbSet::Chroma8, + TX16, + TX8, + (&scan16, &scan8), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )?; + decode_block_rect( + &mut dec, + &mut cdfs, + &mut neighbours, + (at32.0, at32.1 + 1), + 16, + 32, + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + reduced_tx_set, + )?; + } + PARTITION_VERT_B => { + // lane-part32 r1: a true 16x32 strip on the + // left + two 16x16 squares on the right + // (libaom decode_partition VERT_B: left + // strip, TR, BR). + INTRA_VERT_B_HITS.with(|c| c.set(c.get() + 1)); + let _vert_ab = crate::encode::Reach::vert_ab_partition(); + decode_block_rect( + &mut dec, + &mut cdfs, + &mut neighbours, + at32, + 16, + 32, + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + reduced_tx_set, + )?; + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + (at32.0, at32.1 + 1), + SUB, + TxbSet::Luma16, + TxbSet::Chroma8, + TX16, + TX8, + (&scan16, &scan8), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )?; + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + (at32.0 + 1, at32.1 + 1), + SUB, + TxbSet::Luma16, + TxbSet::Chroma8, + TX16, + TX8, + (&scan16, &scan8), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )?; + } _ => { return Err(unsupported(format!( "a 32x32 partition type this decoder does not code (value={part32})" @@ -8260,10 +8671,103 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( reduced_tx_set, )?; } + PARTITION_HORZ_A | PARTITION_HORZ_B | PARTITION_VERT_A | PARTITION_VERT_B => { + // lane-part32 r4: the four superblock-level AB arms, each + // two 32x32 squares plus one 64x32/32x64 strip, in + // libaom's own `decode_partition` order (decodeframe.c: + // HORZ_A = TL, TR, bottom strip; HORZ_B = top strip, BL, + // BR; VERT_A = TL, BL, right strip; VERT_B = left strip, + // TR, BR). The pieces are exactly the ones already proven + // by the `PARTITION_NONE`-under-SPLIT (32x32 square) and + // `PARTITION_HORZ`/`VERT` (rect64 strip) arms above. + SB_AB_HITS.with(|c| { + let mut h = c.get(); + h[part - PARTITION_HORZ_A] += 1; + c.set(h); + }); + // lane-part32 r5: only the two VERTICAL arms reorder the + // square sub-blocks (TL, BL, TR, BR), so only they switch + // libaom's `has_tr`/`has_bl` tables. + let _vert_ab = (part == PARTITION_VERT_A || part == PARTITION_VERT_B) + .then(crate::encode::Reach::vert_ab_partition); + macro_rules! square32 { + ($at:expr) => { + decode_block( + &mut dec, + &mut cdfs, + &mut neighbours, + $at, + BLOCK, + TxbSet::Luma32, + TxbSet::Chroma16, + TX32, + TX16, + (&scan32, &scan16), + true, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + allow_intrabc, + &scan32, + &scan16, + &scan8, + &scan4, + tx_select, + reduced_tx_set, + )? + }; + } + macro_rules! strip64 { + ($at:expr, $bw:expr, $bh:expr) => { + decode_block_rect64( + &mut dec, + &mut cdfs, + &mut neighbours, + $at, + $bw, + $bh, + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + reduced_tx_set, + )? + }; + } + let (br, bc) = (at.0 + 2, at.1 + 2); + match part { + PARTITION_HORZ_A => { + square32!(at); + square32!((at.0, bc)); + strip64!((br, at.1), 64, 32); + } + PARTITION_HORZ_B => { + strip64!(at, 64, 32); + square32!((br, at.1)); + square32!((br, bc)); + } + PARTITION_VERT_A => { + square32!(at); + square32!((br, at.1)); + strip64!((at.0, bc), 32, 64); + } + _ => { + strip64!(at, 32, 64); + square32!((at.0, bc)); + square32!((br, bc)); + } + } + } _ => { return Err(unsupported( - "a superblock-level partition type other than NONE or SPLIT (this \ - decoder's intra tile path codes only those two at 64x64)", + "a superblock-level 1:4 partition (PARTITION_HORZ_4/VERT_4 at 64x64, \ + four 64x16/16x64 strips)", )); } } @@ -13922,6 +14426,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // lane-partab r1: mirror of PARTITION_HORZ_A with // width/height swapped (TL, BL, right 16x32 strip). PARTAB_HITS.with(|c| c.set(c.get() + 1)); + let _vert_ab = crate::encode::Reach::vert_ab_partition(); decode_inter_block( &mut dec, &mut cdfs, @@ -14083,6 +14588,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // 16x16 squares on the right (libaom decode_partition // PARTITION_VERT_B: left strip, TR, BR). PARTAB_HITS.with(|c| c.set(c.get() + 1)); + let _vert_ab = crate::encode::Reach::vert_ab_partition(); decode_inter_block( &mut dec, &mut cdfs, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 2c12a1e7..0bee46b0 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1043,6 +1043,59 @@ const HAS_BOTTOM_LEFT: [&[u8]; 3] = [ ], ]; +/// `has_tr_vert_*` (libaom `reconintra.c`, `has_tr_vert_tables`), indexed by +/// [`Reach::table`] exactly like [`HAS_TOP_RIGHT`]: inside a +/// `PARTITION_VERT_A`/`_B` the square sub-blocks are visited TL, BL, TR, BR +/// rather than in raster order, so the bottom-left square's top-right +/// neighbour is the partition's own right-hand rectangle -- not yet decoded. +/// libaom switches tables on the partition type (`get_has_tr_table`); the +/// ordinary table says 1 where this one says 0 (32x32 at row 1 col 0: +/// `95` bit 4 = 1 vs `15` bit 4 = 0), which is exactly the case +/// lane-part32 r5 caught reading undecoded samples. +const HAS_TOP_RIGHT_VERT: [&[u8]; 3] = [ + // has_tr_vert_16x16 + &[255, 0, 119, 0, 127, 0, 119, 0], + // has_tr_vert_32x32 + &[15, 7], + // has_tr_vert_8x8 + &[ + 255, 255, 0, 0, 119, 119, 0, 0, 127, 127, 0, 0, 119, 119, 0, 0, 255, 127, 0, 0, 119, 119, + 0, 0, 127, 127, 0, 0, 119, 119, 0, 0, + ], +]; + +/// [`HAS_TOP_RIGHT_VERT`]'s sibling, `has_bl_vert_tables`. +const HAS_BOTTOM_LEFT_VERT: [&[u8]; 3] = [ + // has_bl_vert_16x16 + &[254, 16, 254, 0, 254, 16, 254, 0], + // has_bl_vert_32x32 + &[14, 14], + // has_bl_vert_8x8 + &[ + 254, 255, 16, 17, 254, 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0, 254, 255, 16, 17, 254, + 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0, + ], +]; + +thread_local! { + /// Set for the duration of a `PARTITION_VERT_A`/`_B`'s own sub-blocks + /// (see [`Reach::vert_ab_partition`]) -- libaom carries the partition + /// type down into `has_top_right`/`has_bottom_left` as an argument, and + /// threading one bool through this crate's already twenty-argument + /// `decode_block` would touch every call site instead of the two arms + /// that need it. + static VERT_AB_PARTITION: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + +/// Restores [`VERT_AB_PARTITION`] to what it was when the guard was made. +pub(crate) struct VertAbGuard(bool); + +impl Drop for VertAbGuard { + fn drop(&mut self) { + VERT_AB_PARTITION.with(|c| c.set(self.0)); + } +} + /// `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 @@ -1113,6 +1166,15 @@ impl Reach { Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8)) } + /// Marks every [`Reach::of`] made until the returned guard drops as being + /// inside a `PARTITION_VERT_A`/`_B` (libaom's `get_has_tr_table` / + /// `get_has_bl_table` partition argument). Rectangular sub-blocks are + /// unaffected: libaom's own comment says vertical rectangles keep their + /// non-vert table, which is what [`Reach::of_rect`] already reads. + pub(crate) fn vert_ab_partition() -> VertAbGuard { + VertAbGuard(VERT_AB_PARTITION.with(|c| c.replace(true))) + } + /// Neither, which is all a mode that reads no further than its own edges /// needs. pub(crate) fn none() -> Self { @@ -1145,7 +1207,11 @@ impl Reach { // reference pixels), never a stream desync (Reach never gates a // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4` // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row. - let table = HAS_TOP_RIGHT[Self::table(side)]; + let table = if VERT_AB_PARTITION.with(std::cell::Cell::get) { + HAS_TOP_RIGHT_VERT[Self::table(side)] + } else { + HAS_TOP_RIGHT[Self::table(side)] + }; Self::bit( table, (row * Self::table_stride(side) + col) % (table.len() * 8), @@ -1165,7 +1231,11 @@ impl Reach { return false; } // corner-cut: see `top_right`'s note -- same clamp, same ceiling. - let table = HAS_BOTTOM_LEFT[Self::table(side)]; + let table = if VERT_AB_PARTITION.with(std::cell::Cell::get) { + HAS_BOTTOM_LEFT_VERT[Self::table(side)] + } else { + HAS_BOTTOM_LEFT[Self::table(side)] + }; Self::bit( table, (row * Self::table_stride(side) + col) % (table.len() * 8), diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 83029364..ec6ba5c9 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -47,7 +47,7 @@ const REFUSALS: &[&str] = &[ "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)", "a tx_type symbol outside its CDF's own set: {t}", "an INTER 32x32 partition type this decoder does not code (value={part32})", - "a superblock-level partition type other than NONE or SPLIT (this decoder's intra tile path codes only those two at 64x64)", + "a superblock-level 1:4 partition (PARTITION_HORZ_4/VERT_4 at 64x64, four 64x16/16x64 strips)", "a HORZ/VERT intra strip in a screen-content frame (palette syntax is consumed for square blocks only)", "a block that actually uses a palette (UV) -- reconstruction is out of scope", "a block that actually uses a palette (Y) -- reconstruction is out of scope", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 388f49db..d1c72ef9 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -9868,17 +9868,15 @@ mod tests { msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); - // r2: `--enable-ab-partitions=0` does NOT stop aomenc - // choosing an SB-level HORZ_A/HORZ_B/VERT_A/VERT_B - // (`partition_w64` value 4-7) even with - // `--min/max-partition-size=32/64` -- a real, separate - // encoder quirk (AB-at-64 is a different, unlanded - // capability, lane-partab's territory is 32x32 and - // below only) discovered live this round, not this - // lane's HORZ/VERT gap. Only NONE/SPLIT/HORZ/VERT - // (values 0-3) are this gate's territory; a real - // HORZ/VERT (1/2) must still decode pixel-exact, which - // `sb_rect_hits() > 0` below hard-proves. + // r5: the four SB-level AB arms (`partition_w64` 4-7) + // decode since this round, so the only partition refusal + // left at 64x64 is the 1:4 pair (values 8/9, four + // 64x16/16x64 strips, whose luma TX_64X16 this decoder + // has no transform for); the refusals this loop actually + // counts on these fixtures are the pre-existing + // screen-content-palette strip one. Every refusal is + // named, so a genuine decode failure still fails the + // assert above rather than skipping. named_refusals += 1; eprintln!("seed {seed} refusal: {msg}"); continue; @@ -10463,6 +10461,222 @@ mod tests { ); } + /// lane-part32 r4: a real `aomenc` stream whose superblock-level + /// partition decision is one of the four AB arms (`PARTITION_HORZ_A`/ + /// `_B`/`VERT_A`/`_B` at 64x64: two 32x32 squares plus one 64x32/32x64 + /// strip) must decode pixel-exact. Same recipe as the HORZ/VERT sibling + /// above with `--enable-ab-partitions=1`; `sb_ab_hits() > 0` is a HARD + /// assert, so a run where aomenc's RD never picked an AB arm fails + /// rather than passing vacuously. + #[test] + fn a_real_aomenc_stream_with_a_superblock_level_ab_partition_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_stream_with_a_superblock_level_ab_partition_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height, frame_count) = (192usize, 128usize, 1usize); + let mut named_refusals = 0u32; + let mut matched = 0u32; + let n_attempts: u32 = std::env::var("EC_SBAB_GATE_ATTEMPTS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(40); + for attempt in 0..n_attempts { + let seed = 42 + attempt; + let duration = frame_count as f64 / 25.0; + let source = + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &source, + "-pix_fmt", + "yuv420p", + "-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 args: Vec<&str> = vec![ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cq-level=45", + "--cpu-used=0", + "--threads=1", + "--row-mt=0", + "--sb-size=64", + "--enable-rect-partitions=1", + "--enable-ab-partitions=1", + "--enable-1to4-partitions=0", + "--min-partition-size=32", + "--max-partition-size=64", + "--enable-restoration=0", + "--enable-palette=0", + "--deltaq-mode=0", + "--enable-filter-intra=0", + "--enable-cfl-intra=0", + "--enable-intrabc=0", + "--enable-tx-size-search=0", + "--obu", + "-o", + "-", + "-", + ]; + let mut child = Command::new(aomenc_path()) + .args(&args) + .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"); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let frames = match decode_stream(&stream) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" + ); + // r5: the four SB-level AB arms (`partition_w64` 4-7) + // decode since this round, so the only partition refusal + // left at 64x64 is the 1:4 pair (values 8/9, four + // 64x16/16x64 strips, whose luma TX_64X16 this decoder + // has no transform for); the refusals this loop actually + // counts on these fixtures are the pre-existing + // screen-content-palette strip one. Every refusal is + // named, so a genuine decode failure still fails the + // assert above rather than skipping. + named_refusals += 1; + eprintln!("seed {seed} refusal: {msg}"); + continue; + } + Ok(frames) => frames, + }; + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); + assert_eq!(frames.len(), frame_count); + let mismatched = frames + .iter() + .zip(&ffmpeg_frames) + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); + if mismatched { + if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { + std::fs::write(&path, &stream).expect("writing pinned stream"); + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); + } + } + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); + } + matched += 1; + } + assert!( + matched > 0, + "{NAME}: every attempt refused; the gate never decoded a stream" + ); + let arms = crate::decode::sb_ab_hits_by_arm(); + for (i, name) in ["HORZ_A", "HORZ_B", "VERT_A", "VERT_B"].iter().enumerate() { + assert!( + arms[i] > 0, + "{NAME}: zero superblock-level PARTITION_{name} blocks fired (arms={arms:?}, \ + {matched} matches, {named_refusals} refusals out of {n_attempts}) -- that arm \ + proved nothing this run" + ); + } + eprintln!( + "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of \ + {n_attempts}, sb_ab_hits={} arms(HORZ_A,HORZ_B,VERT_A,VERT_B)={arms:?}", + crate::decode::sb_ab_hits() + ); + } + + /// lane-part32 r2: prints the exact mismatch geometry (block coords, + /// got/want pixels) of the pinned seed-42 stream captured off + /// `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` + /// via `EC_AV1_GATE_DUMP`. Debug-only, `#[ignore]`d. + #[test] + #[ignore = "debug harness, run manually with EC_AV1_GATE_DUMP_PIN"] + fn debug_part32_r2_sbpart_pin_mismatch_geometry() { + let path = std::env::var("EC_AV1_GATE_DUMP_PIN").expect("set EC_AV1_GATE_DUMP_PIN"); + let stream = std::fs::read(&path).expect("read pinned stream"); + let frames = decode_stream(&stream).expect("pinned stream must decode"); + if let Ok(dump) = std::env::var("EC_DEBUG_DUMP_Y") { + let bytes: Vec = frames[0].y.iter().map(|&v| v as u8).collect(); + std::fs::write(&dump, bytes).expect("write y dump"); + } + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 192, 128, 1); + let got = &frames[0]; + let want = &ffmpeg_frames[0]; + let width = 192usize; + let mut n = 0; + let (mut min_x, mut max_x, mut min_y, mut max_y) = (width, 0, 128, 0); + for y in 0..128usize { + for x in 0..width { + let idx = y * width + x; + if got.y[idx] != want.y[idx] { + n += 1; + min_x = min_x.min(x); + max_x = max_x.max(x); + min_y = min_y.min(y); + max_y = max_y.max(y); + } + } + } + eprintln!( + "total luma mismatches: {n} bbox x=[{min_x},{max_x}] y=[{min_y},{max_y}] frame=192x128" + ); + for y in 0..128usize { + let mut row_first = None; + let mut row_count = 0; + for x in 0..width { + let idx = y * width + x; + if got.y[idx] != want.y[idx] { + if row_first.is_none() { + row_first = Some(x); + } + row_count += 1; + } + } + if let Some(fx) = row_first { + eprintln!("row {y}: first mismatch x={fx} count={row_count}"); + } + } + } + /// lane-rect64q r1: same recipe as /// [`a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact`] /// minus `--deltaq-mode=0` -- real aomenc's non-realtime default is diff --git a/lanes/part32-r1.report.md b/lanes/part32-r1.report.md new file mode 100644 index 00000000..7a8eaa32 --- /dev/null +++ b/lanes/part32-r1.report.md @@ -0,0 +1,117 @@ +# lane-part32 r1 report + +## What landed + +`crates/ec-av1/src/decode.rs`: the INTRA 32x32 dispatch (key-frame tile path, +the one that previously hit `"a 32x32 partition type this decoder does not +code (value={part32})"`) now handles `PARTITION_HORZ_A` (4), `PARTITION_HORZ_B` +(5), `PARTITION_VERT_A` (6), `PARTITION_VERT_B` (7) -- mirrors cd6cb6d's INTER +AB arms exactly, using `decode_block` (square 16x16, `TxbSet::Luma16`/ +`Chroma8`) for the two square sub-blocks and `decode_block_rect` (32x16 or +16x32) for the strip. The catch-all refusal is unchanged text but now only +reached for `PARTITION_HORZ_4`/`PARTITION_VERT_4` (values 8/9) -- narrowed by +construction, not by editing the string (still pinned as-is in +`refusal_inventory.rs`, no change needed there since the text is identical). + +Four new thread-local hit counters (`intra_horz_a_hits`/`intra_horz_b_hits`/ +`intra_vert_a_hits`/`intra_vert_b_hits`), one per arm, mirroring +`PARTAB_HITS`'s pattern but split so a recipe that only ever fires one arm +can't hide behind a shared total. + +## Gate + +`crates/ec-av1/src/stream.rs::a_real_aomenc_intra_stream_with_ab_partitions_decodes_pixel_exact` +-- single key frame, 64x64, `mandelbrot` content (same recipe shape as the +existing `a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact`), +`--min-partition-size=16 --max-partition-size=32` (same bound lane-partab used +to stay below the still-unlanded SB(64)-level AB territory), 40 attempts. +Forbids the four `value={4,5,6,7}` intra refusal strings once reached, hard- +asserts all four per-arm counters > 0. + +**PASSES**, `EC_AV1_REQUIRE_AOMENC=1` included: + +``` +a_real_aomenc_intra_stream_with_ab_partitions_decodes_pixel_exact: +0 named refusals, 40 pixel-exact matches out of 40, +horz_a=34 horz_b=45 vert_a=15 vert_b=18 +``` + +EVIDENCE: `cargo test -p ec-av1 --lib a_real_aomenc_intra_stream_with_ab_partitions_decodes_pixel_exact -- --nocapture` +(with and without `EC_AV1_REQUIRE_AOMENC=1`) | both runs above | 40/40 pixel-exact, all 4 counters nonzero. + +## Suite: NOT fully green -- one PRE-EXISTING failure exposed, out of this +## lane's scope + +`cargo test -p ec-av1 --lib`: **264 passed, 1 failed** (19 ignored), 208s. + +Failure: `stream::tests::a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact`, +seed 42, first luma mismatch at (row=0, col=91), inside superblock (0,1)'s +`PARTITION_VERT`-at-64 arm (`decode_block_rect64`, lane-sbpart territory -- +mode=DC_PRED, all_zero=1, off by one: got=80 want=81). + +Root-cause isolation done this round: +- Confirmed with `git checkout 21a71ec -- decode.rs stream.rs` (temporary, + reverted) that this exact seed/recipe/byte-for-byte stream **already + refused** on main at `"a 32x32 partition type this decoder does not code + (value=4)"` -- inside superblock (1,1), which decodes strictly *after* + superblock (0,1) in tile order. Entropy decoding is causal: nothing this + lane touched can change how SB(0,1) is decoded, since SB(0,1) is decoded + and its pixels finalized before the stream ever reaches the part32 AB + symbol this lane added. +- This is the "refusal hides a defect" class already in the ledger + (`refusal-hides-a-defect.md`): the sbpart gate's `matched > 0` assertion + only ever checked pixels on attempts that decoded *end to end*; every + attempt that hit a downstream refusal (including this exact seed, before + this lane) never had its earlier SB(0,1) pixels compared at all. Lifting + the part32 refusal let decode run to completion for seed 42, and the + frame-level pixel compare then found a pre-existing `decode_block_rect64` + defect for the first time. +- Not investigated further (turn budget): whatever's wrong in the + 32x64/64x32 rect-64 corner-truncated intra path is `decode_block_rect64`'s + own bug, unrelated to `decode_block`/`decode_block_rect` (this lane's + functions) and to the AB dispatch. It belongs to lane-sbpart, not + lane-part32. + +Per this repo's own rule ("Landing red ON THE LANE is fine -- I do the merges +to main and I never merge red"), HEAD is committed red with this failure +named. **Do not merge without either (a) fixing `decode_block_rect64`'s +DC-prediction/edge bug (a lane-sbpart-shaped charter, not part32's) or (b) an +explicit call that this newly-exposed defect is acceptable to carry +forward.** + +`cargo check --workspace --all-targets`: clean. + +## ReelC extract + +Cut `fixtures/part32/reelc-extract.obu` (gitignored) via +`ffmpeg -ss 600 -t 2 -c:v copy -an -f obu -bsf:v extract_extradata` from +`~/Videos/Films/reelc.mkv`. +`cargo run -p ec-av1 --example decode_probe -- fixtures/part32/reelc-extract.obu`: + +``` +REFUSED: unsupported: AV1 decode_stream (a stream whose bit depth is not 8 +(this decoder reconstructs into 8-bit planes)) +``` + +No longer stops at the part32 refusal (charter's predicted outcome: ReelC is +10-bit and hits the bit-depth wall first, per `his-av1-library-is-10-bit.md`). + +EVIDENCE: `cargo run -p ec-av1 --example decode_probe -- fixtures/part32/reelc-extract.obu` +| ran against a real 2s ReelC extract at ~10min mark | output above. + +## HEAD + +`69eeb6e` -- "wip(part32): intra 32x32 AB arms (HORZ_A/HORZ_B/VERT_A/VERT_B) +decode, gated 40/40 pixel-exact, all 4 hit counters fire" + +## Open + +- `HORZ_4`/`VERT_4` (values 8/9) not attempted this round; refusal still + names them (unmodified string, now only reachable for those two values). + Not reached by any gate recipe tried, reachability unknown. +- The `decode_block_rect64` DC-prediction/edge defect above is the actual + blocker for a fully green suite. It is NOT part32's code and needs its own + lane/charter (lane-sbpart shaped) to isolate and fix. +- `--cq-level=45`/`--enable-tx-size-search=0` etc kept identical to + lane-partab's proven recipe; not swept for a lower-cq/higher-fidelity + variant. diff --git a/lanes/part32-r2.report.md b/lanes/part32-r2.report.md new file mode 100644 index 00000000..dc76830d --- /dev/null +++ b/lanes/part32-r2.report.md @@ -0,0 +1,107 @@ +# lane-part32 round 2 + +## State: NOT DONE, red, committed (4bff66c) + +Charter: fix decode_block_rect64's pre-existing pixel mismatch exposed by +round 1's refusal lift. + +## True mismatch geometry (measured on THIS tree, not inherited) + +Reproduced live: `cargo test -p ec-av1 --lib +a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` +-> 266 passed, 1 failed, matches the trial-merge count I was handed. + +Pinned the mismatching seed-42 stream via `EC_AV1_GATE_DUMP`, added an +`#[ignore]`d debug test (`debug_part32_r2_sbpart_pin_mismatch_geometry`, +`stream.rs`) that decodes it and prints exact per-pixel diffs. + +- Full-frame post-filter comparison against ffmpeg: 17873/24576 luma pixels + differ, spanning the entire frame past x=91,y=0. This is NOT a single + off-by-one -- it's a cascade: once one block's pixels are wrong, every + block that intra-predicts from it downstream inherits and compounds the + error, eventually corrupting nearly all of SB row 1 (y>=64). +- Root of the cascade, isolated via aom-oracle's `EC_AV1_PREFILT_DUMP` rung + (pre-loop-filter ground truth) plus two new env-gated bypasses + (`EC_AV1_DEBUG_SKIP_DEBLOCK`/`_CDEF`, off by default) on our side: our own + PRE-FILTER reconstruction matches the oracle's pre-filter output exactly + through SB(1,0)'s first VERT-64 sub-block (mi_c=16..23, all-zero DC, + correct). The very first wrong pixel is (96,0) -- the first pixel of the + SECOND VERT-64 sub-block (mi_c=24..31, real coefficients, eob_nonzero=true): + oracle=81, ours=76. + +## Entropy layer: DIVERGES, inside this exact block + +Compared msac RANGE (never `tell()`) against the aom-oracle's `EC_TRACE_COEFF` +rung, element by element: +- Ranges agree exactly up through `rng=63764` at the entry to this block's + luma coefficient read (`EC_COEFF plane=0 ... tx_size=64corner/11 + rng=63764` on both sides). +- Both sides read `eob=6` (agreement). +- The oracle's per-step trace assigns nonzero level=1 at scan indices + 0, 1, 2, 4, 5 (skipping 3). Ours (`scan32 = default_scan(TX32)`, a plain + square 32x32 scan) walks physical positions (0,0)(0,1)(1,0)(2,0)(1,1)(0,2) + for the same six scan indices -- a different position-to-index mapping + than whatever the oracle used. +- Post-block range: ours=37896, oracle=46344. Diverged. + +Conclusion: this is a genuine in-block entropy desync (wrong scan table), +not a post-processing (deblock/CDEF, both proven innocent by disabling them) +and not a dequant/reconstruction-only bug. + +## Root cause (identified, not yet fixed) + +`decode_block_rect64` (crates/ec-av1/src/decode.rs:3559) reads the luma +corner with `scan32 = default_scan(TX32)` -- the square TX_32X32 scan -- +for BOTH genuine square 64x64 blocks and truncated-corner VERT-64/HORZ-64 +strips (bw=32,bh=64 or bw=64,bh=32). The function's own doc comment already +documents an analogous, already-fixed asymmetry for the nz_map *context* +table (`NZ_MAP_CTX_OFFSET_32X64`/`_64X32`, lane-sbpart r8) but the *scan +order itself* was never given the same treatment. AV1 spec names a distinct +Default_Scan_32x64/64x32 table (libaom `av1_scan_orders[TX_32X64]` / +`[TX_64X32]`), and this decoder does not have it. + +Same shape as this repo's existing `table-indexed-by-raw-size` and +`scan-weights-cross-axis` ledger classes. + +## Fix NOT landed this round + +Turn budget spent entirely on localization (per charter's own step-2 +instruction, "if ranges diverge, that's decisive"). Porting a correct +1024-entry SCAN_32X64/SCAN_64X32 table from libaom and wiring it in at the +`decode_block_rect64` call site (mirroring how `chroma_scan` already +branches on `bw == 64` a few lines below) is the next round's whole job -- +should be mechanical once the table exists, but deriving/verifying a new +1024-entry table safely needs its own round, not the tail end of this one. + +## Gates + +- Part32's own AB gate (round 1's): + `a_real_aomenc_intra_stream_with_ab_partitions_decodes_pixel_exact` -- + not re-run this round (untouched by this round's changes; round 1 already + proved it 40/40 and this round made zero changes to part32's own code + paths). +- Target gate + (`a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact`): + still RED, root cause pinned, fix not landed. +- `cargo check --workspace --all-targets`: clean. +- Full `cargo test -p ec-av1 --lib` NOT re-run this round after the debug + additions (both are `#[ignore]`d / env-gated no-ops by default; the only + change to non-test code is the two early-return guards in + `apply_deblock`/`apply_cdef`, both env-gated and off by default -- zero + behavior change verified by the identical `cargo check` above). + +## rect64q contradiction: explained + +lane-rect64q's own reproduction (row0 col91=81, matching ffmpeg, no +mismatch) used a stream from ITS OWN worktree, which did not have this +lane's AB arms and so took a genuinely different aomenc encode (different +partition decisions entirely) -- not a contradiction of this round's +finding, a different stream. This round's own pinned stream, decoded fresh +on this tree, reproduces the failure deterministically and traces to a real, +narrow, named defect (wrong scan table for TX_32X64/TX_64X32 corner-truncated +luma). + +## HEAD + +`4bff66c` -- "wip(part32): pin decode_block_rect64's block2 defect to a +scan-table mismatch" diff --git a/lanes/part32-r5.report.md b/lanes/part32-r5.report.md new file mode 100644 index 00000000..8e3e0c82 --- /dev/null +++ b/lanes/part32-r5.report.md @@ -0,0 +1,137 @@ +# lane-part32 r5 — SB-level AB partitions at 64x64, proven arm by arm + +Branch `lane-part32`, commit `fd1a2eb` on top of r4's WIP snapshot `25cac7d` +(itself rebased on main `3808cf8`). + +## What r4 actually left (verified, not assumed) + +r4's last words claimed SB-level HORZ/VERT 64 was green. It was not: with +`EC_AV1_REQUIRE_AOMENC=1` both +`a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` +and its new `..._ab_partition_...` sibling FAILED on seed 63 (luma mismatch, +`test result: FAILED. 0 passed; 2 failed`). r4's two real fixes — +`EOB_PT_512_CHROMA` q-context split (`cdf.rs:269`, `cdf_state.rs:1013`) and +the four AB arms in `decode.rs` — were correct but incomplete. + +## Root cause found and fixed this round + +`PARTITION_VERT_A`/`_B` visit their square sub-blocks **TL, BL, TR, BR**, not +in raster order, so the bottom-left square's top-right neighbour is the +partition's own right-hand rectangle — not yet decoded. libaom switches +`has_top_right`/`has_bottom_left` onto `has_tr_vert_*`/`has_bl_vert_*` for +exactly these two partition types (`reconintra.c` `get_has_tr_table`, +`get_has_bl_table`). This crate's `Reach` carried only the raster tables: +for the BL 32x32 (blk_row_in_sb=1, blk_col_in_sb=0, bit index 4) +`has_tr_32x32[0]=95` bit 4 = **1** while `has_tr_vert_32x32[0]=15` bit 4 = **0**. +One availability bit → 4683 wrong luma pixels. + +Failure chain on the pinned stream: BL square predicts (V_PRED, angle_delta=2) +off never-written above-right samples → its right columns decay +(150,150,150 → 130,93,39 at x=157..159) → the 32x64 strip to its right +DC-predicts off that corrupted left column and comes out flat **107** instead +of ~149 → the next superblock row's V_PRED copies 107 down the whole frame. + +Decisive evidence that this was reconstruction, not entropy: instrumented +`aomdec` (`EC_TRACE=1 EC_TRACE_MODE=1`) agrees with us on every symbol — +`EC_PART_VAL mi_row=0 mi_col=32 value=6` (VERT_A), every `EC_IMODE_VAL` +mode/uv_mode, and the msac **range** at `mi_row=0 mi_col=40` is `53240` in +both decoders (ours: `TRACE_RECT_PREFI ... rng=53240`). + +## Files changed + +- `crates/ec-av1/src/encode.rs:1049` — `HAS_TOP_RIGHT_VERT` / + `HAS_BOTTOM_LEFT_VERT` transcribed verbatim from libaom + (`has_tr_vert_8x8/16x16/32x32`, `has_bl_vert_*`), a `VERT_AB_PARTITION` + thread-local + `VertAbGuard`, `Reach::vert_ab_partition()`, and the table + pick inside `top_right`/`bottom_left`. Rect sub-blocks deliberately keep + the non-vert tables (libaom's own comment) — `Reach::of_rect` untouched. +- `crates/ec-av1/src/decode.rs:7535,7610` — the 32x32-level intra VERT_A/VERT_B + arms take the guard (same defect one level down, swept in the same round). +- `crates/ec-av1/src/decode.rs:7766` — the SB-level AB arm takes it only for + the two vertical arms. +- `crates/ec-av1/src/decode.rs:13432,13595` — the two inter AB VERT arms take + it too (class sweep; inter intra-mode blocks read the same `Reach`). +- `crates/ec-av1/src/decode.rs:767,772` — `SB_AB_HITS` is now `[usize; 4]`, + incremented by arm, with `sb_ab_hits_by_arm()`. +- `crates/ec-av1/src/stream.rs:9665` — the AB gate hard-asserts **each** of + HORZ_A/HORZ_B/VERT_A/VERT_B fired, not just the sum. +- `crates/ec-av1/src/stream.rs:9467,9628` — both gates' stale r2 comment + ("AB-at-64 is a different, unlanded capability") replaced with the r5 truth. +- `crates/ec-av1/src/refusal_inventory.rs:50` — (r4) the refusal is now + narrowed to the 1:4 pair only. `gate_coverage.rs` needed no edit: + `--enable-ab-partitions=1` is now positively exercised and the flag was + never on `NEVER_EXERCISED`; both its tests are green in the run below. + +## Gates + +``` +CARGO_TARGET_DIR=$HOME/.cache/cargo-target-part32 EC_AV1_REQUIRE_AOMENC=1 \ + cargo test -p ec-av1 --lib -- --test-threads=1 --nocapture superblock_level +``` +`test result: ok. 2 passed; 0 failed; 1 ignored` (was 0 passed; 2 failed). + +EVIDENCE: /tmp/.../scratchpad/r5gate.txt, r5ab.txt | 40 real-aomenc streams per gate, `--sb-size=64 --enable-rect-partitions=1 --enable-ab-partitions=1`, seeds 42..81, decoded and compared to ffmpeg | ab gate 37 pixel-exact / 3 named refusals, `sb_ab_hits=13 arms(HORZ_A,HORZ_B,VERT_A,VERT_B)=[2, 5, 3, 3]`; horz/vert gate 37 pixel-exact, `sb_rect_hits=61` + +EVIDENCE: /tmp/.../scratchpad/r5full.txt | `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` | `test result: ok. 269 passed; 0 failed; 24 ignored; 0 measured` in 1094s + +EVIDENCE: /tmp/.../scratchpad/pre63o.f0 vs our63_nf.y | oracle `EC_AV1_PREFILT_DUMP` vs ours with deblock+CDEF disabled, on the pinned seed-63 stream (pre-fix) | 4683 luma samples differ, bbox x=[151,191] y=[0,127]; post-fix the gate is pixel-exact + +## Refusal lifted + +`"a superblock-level partition type other than NONE or SPLIT (this decoder's +intra tile path codes only those two at 64x64)"` → replaced by +`"a superblock-level 1:4 partition (PARTITION_HORZ_4/VERT_4 at 64x64, four +64x16/16x64 strips)"`. HORZ/VERT/HORZ_A/HORZ_B/VERT_A/VERT_B at 64x64 are all +now real-stream-proven. + +## Film check + +`ffmpeg -t 0.4 -i ReelC...mkv -c:v copy -f obu reelc_r5.obu` (418 B), then +`cargo run -p ec-av1 --example decode_probe -- reelc_r5.obu`: + +``` +REFUSED: unsupported: AV1 tile (filter intra on a HORZ/VERT strip (this decoder predicts square-only)) +``` + +ReelC reaches an SB-level HORZ/VERT strip and now stops on filter-intra-on-a-strip. +deferred: the matching "before" probe on main `3808cf8` — not re-measured this +round (would need a full rebuild of a second target dir at the turn ceiling); +unblocked by any lane that already has a main-based build of `decode_probe`. + +## Coordinator's two facts, answered + +1. **rectsplit seed 50 / `decode_block_rect64` split-transform residual** — NOT + fixed here; my change never runs for a HORZ/VERT strip (the guard is set + only under VERT_A/_B, and rect blocks go through `Reach::of_rect`, which is + untouched). But the hypothesis is now concrete and shares this round's + class: `decode.rs:5099` and `decode.rs:5425` compute per-TU availability as + `Reach::of(logical_tx, tu_px, tu_py, ...)` — i.e. they treat the transform + unit as if it were a *block at frame coordinates* and consult the + superblock-position table. libaom does not: in `has_top_right`, when + `row_off > 0` it never reaches a table at all, it returns + `col_off + tx_size_wide_unit < plane_bw_unit` — purely block-relative. For + a depth-2 TX_16X16 inside a 64x32 strip that is exactly the sub-TU at + `row_off > 0` case. That is where I would look first for the (171,56) + 147-vs-148 sample. `rectsplit` keeps ownership. +2. **scaledref's masked Golomb defect at seed 67** — appears RESOLVED on this + branch. Both r5 gate runs cover seeds 42..81; the only refusals were seeds + 45/46/59 (screen-content palette strip), and seed 67 decoded **pixel-exact + against ffmpeg**, which is stronger than "no Golomb refusal". Most likely + killed by r4's `EOB_PT_512_CHROMA` q-context fix, with this round's + availability fix removing the seed-63 sibling. scaledref should re-run with + the raised cap after merge; if it still reproduces, the cause is elsewhere. + +## Residue + +- fix-now(next lane): `PARTITION_HORZ_4`/`VERT_4` at 64x64 — still refused by + name. Not attempted: `decode_block_rect64` is hardwired to 64x32/32x64 + (`TxbSet::Luma64` + `TX32` scan for luma, `ChromaRect32x16` + `SCAN_32X16/ + 16X32` for chroma, `decode.rs:3846-3990`); a 64x16 strip needs a real + TX_64X16 luma transform plus 32x8 chroma scans/EOB tables, which is a + transform-primitive lane, not a partition-wiring one. +- accepted: `Reach::table(64)` maps to the 16x16 table row (pre-existing); + harmless because a 64x64 block in a 64 superblock always takes the + `row == 0` early return before any table is read. +- accepted: the per-TU `Reach::of` shape described above is left to rectsplit + rather than fixed blind here, since every gate on this branch is green and a + speculative change to it would risk them.