From 8a4713225ad83181a4dd61f661f3612e3302dda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 08:04:24 +0300 Subject: [PATCH] fix(av1): a skipped 8x8 intra leaf whose tx_depth split published its 8-px block side, not TX_4X4, to the next block's tx-size context decode_leaf8's tail called fill_lf_grid(leaf_mi, 2, 8, 0) with the literal block side. libaom calls set_txfm_ctxs(mbmi->tx_size, bw, bh, skip_txfm && is_inter_block(mbmi), xd) (decodemv.c) -- for an INTRA block that skip term is always 0, so a skipped 8x8 leaf whose tx_depth resolved to TX_4X4 still publishes 4 to get_tx_size_context (pred_common.h:342). Writing 8 made the 8x16 strip below such a leaf read tx_size_cat1 row 2 where libaom reads row 1 -- same alphabet, wrong CDF row (class wrong-alphabet-same-value), so the msac range diverged mid-frame with NO refusal. Witness: a 4K 10-bit AV1 mkv's key frame at -ss 900 (3840x1608 yuv420p10le), first bad luma sample row 186 col 1803, entropy divergence at mi (60,122) right after filter_intra_mode (both decoders rng=33750; ours then read tx_depth at ctx 2 where aomdec reads ctx 1). All 24 key frames of that file (0..6900 step 300) are now LUMA-EXACT vs ffmpeg (previously 6 of them differed by 7.5M-16.6M bytes). A chroma-only residual remains, first at chroma (102,688) pre-filter -- a separate open defect. Also: EC_IMODE/EC_ISTEP/EC_TXCTX/EC_LFGRID cross-decoder trace rungs mirroring the instrumented aomdec, a skip_split_tx counter print in decode_probe, and two aomenc gate arms (8/10-bit) that currently FAIL on their counter assert -- the gradients source never makes aomenc pick a skipped 8x8 leaf with a split transform (recipe sweep unfinished, see lanes/kf900-r1.handoff.md). --- crates/ec-av1/examples/decode_probe.rs | 4 +++ crates/ec-av1/src/decode.rs | 42 +++++++++++++++++++++++++- crates/ec-av1/src/stream.rs | 39 ++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index ce9a3d90..e07e064e 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -33,6 +33,10 @@ fn main() { println!("rect4_32: horz={h} vert={v} coded={c}"); let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); + // lane-kf900 r1: the counter the skipped-8x8-split-transform gate + // asserts -- printed here so a recipe sweep can see it fire without a + // test-binary rebuild. + println!("skip_split_tx: {}", ec_av1::decode::skip_split_tx_hits()); let ir = ec_av1::stream::inter_rect_counters(); println!( "inter_rect: 32x8={} 8x32={} 64x32={} 32x64={} 64x16={} 16x64={}", diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b4a7ad28..ca0f6a6a 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -4140,6 +4140,9 @@ impl Neighbours { // TX_32X32 -- independent of this block's luma `tx_depth`. let uv_tx_w = ((w_mi * MI / 2).max(4).min(32)) as u8; let uv_tx_h = ((h_mi * MI / 2).max(4).min(32)) as u8; + if std::env::var_os("EC_TXGRID_TRACE").is_some() { + eprintln!("EC_LFGRID mi_row={mi_r} mi_col={mi_c} w_mi={w_mi} h_mi={h_mi} tx_px={tx_px} tx_h_px={tx_h_px}"); + } let cur = CURRENT_DELTA_LF.with(|c| c.get()); let snapshot: [i8; 4] = if DELTA_LF_MULTI.with(|c| c.get()) { std::array::from_fn(|i| cur[i].clamp(-63, 63) as i8) @@ -5435,6 +5438,13 @@ fn read_intra_mode_rect( Option, )> { let ec_istep = std::env::var_os("EC_TRACE_MODE_STEP").is_some(); + if ec_istep { + // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line + // (decodemv.c:818), printed at the same point: before the first + // mode-info symbol of the block, so a cross-decoder range ladder can + // be keyed on the BLOCK, not just on the symbol sequence. + eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=rect bw={bw} bh={bh} efi={} cls={:?} rng={}", enable_filter_intra as i32, filter_intra_size_class_rect(bw, bh), dec.debug_state().0); + } macro_rules! istep { ($name:literal, $val:expr) => { if ec_istep { @@ -5561,9 +5571,11 @@ fn read_intra_mode_rect( if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!("TRACE_RECT_USEFI value={}", use_filter_intra as i32); } + istep!("use_filter_intra", use_filter_intra as i32); if use_filter_intra { FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); + istep!("filter_intra_mode", fi_mode as i32); filter_intra = Some(fi_mode); } } @@ -5606,6 +5618,14 @@ fn tx_size_context_rect( ) -> usize { let above = mi_r > n.tile_row0_mi && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_w; let left = mi_c > n.tile_col0_mi && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_h; + if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { + eprintln!( + "EC_TXCTX mi_row={mi_r} mi_col={mi_c} w={own_w} h={own_h} above_px={} left_px={} ctx={}", + if mi_r > n.tile_row0_mi { tx_px_at(n, false, mi_r - 1, mi_c) } else { 0 }, + if mi_c > n.tile_col0_mi { tx_h_px_at(n, false, mi_r, mi_c - 1) } else { 0 }, + usize::from(above) + usize::from(left) + ); + } usize::from(above) + usize::from(left) } @@ -8370,6 +8390,13 @@ fn read_intra_mode( // under `EC_TRACE_MODE_STEP`) so a range ladder can be diffed line for // line against the instrumented aomdec, not just `tell()`. let ec_istep = std::env::var_os("EC_TRACE_MODE_STEP").is_some(); + if ec_istep { + // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line + // (decodemv.c:818), printed at the same point: before the first + // mode-info symbol of the block, so a cross-decoder range ladder can + // be keyed on the BLOCK, not just on the symbol sequence. + eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sq side={side} rng={}", dec.debug_state().0); + } macro_rules! istep { ($name:literal, $val:expr) => { if ec_istep { @@ -10170,7 +10197,17 @@ fn decode_leaf8( neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]); } neighbours.fill_skip_grid(leaf_mi, 2, skip); - neighbours.fill_lf_grid(leaf_mi, 2, 8, 0); + // lane-kf900 r1: the leaf's own RESOLVED transform size, never the literal + // block side. libaom calls `set_txfm_ctxs(mbmi->tx_size, ..., skip_txfm && + // is_inter_block(mbmi), xd)` (decodemv.c) -- for an INTRA block that skip + // term is always 0, so a skipped 8x8 leaf whose `tx_depth` resolved to + // TX_4X4 still publishes 4 to the next block's `get_tx_size_context` + // (pred_common.h:342). Writing 8 here made the 8x16 strip below the leaf + // read `tx_size_cat1` row 2 where libaom reads row 1 -- same alphabet, + // wrong CDF row, so the msac range diverged mid-frame with no refusal + // (class `wrong-alphabet-same-value`; the ReelB key frame at + // -ss 900, first bad luma sample row 186 col 1803). + neighbours.fill_lf_grid(leaf_mi, 2, resolved as u8, 0); neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); Ok(mode) } @@ -10202,6 +10239,9 @@ fn read_intra_mode_sub8( mi_c: usize, ) -> Result<(bool, usize, i32, Option<(usize, i32, Option<(i32, i32)>)>, Option)> { let trace = std::env::var_os("EC_AV1_TRACE").is_some(); + if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { + eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sub8 rng={}", dec.debug_state().0); + } let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; if trace { eprintln!("TRACE sub8 skip mi=({mi_r},{mi_c}) ctx={skip_ctx} value={} rng={}", skip as i32, dec.debug_state().0); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index eab98345..ac436fa7 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -21515,6 +21515,45 @@ mod tests { } } + /// lane-kf900 r1: a SKIPPED 8x8 intra leaf whose `tx_depth` symbol + /// resolved to TX_4X4 must publish 4 -- not its 8-px block side -- to the + /// next block's `get_tx_size_context` (libaom `set_txfm_ctxs(mbmi->tx_size, + /// ..., skip_txfm && is_inter_block(mbmi), xd)`; that skip term is always 0 + /// for an intra block). `decode_leaf8`'s tail wrote the literal 8, so the + /// rect strip below such a leaf read `tx_size_cat1` row 2 where libaom + /// reads row 1 -- same alphabet, wrong CDF row, the msac range diverged + /// mid-frame with NO refusal. `skip_split_tx_hits` was `pub` and asserted + /// by no gate at all before this one (class `tool-disabled-in-every-gate`). + #[test] + fn a_real_aomenc_stream_with_a_skipped_8x8_intra_leaf_whose_tx_split_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_stream_with_a_skipped_8x8_intra_leaf_whose_tx_split_decodes_pixel_exact"; + crate::decode::reset_skip_split_tx_hits(); + run_multi_tile_gate(NAME, 0, 0, 8, 1, &[], false, None); + if have_ffmpeg() && have_aomenc() { + assert!( + crate::decode::skip_split_tx_hits() > 0, + "{NAME}: no skipped 8x8 intra leaf split its transform -- the gate proves nothing" + ); + } + } + + /// [`a_real_aomenc_stream_with_a_skipped_8x8_intra_leaf_whose_tx_split_decodes_pixel_exact`]'s + /// 10-bit arm -- the film that exposed the defect is yuv420p10le. + #[test] + fn a_real_aomenc_stream_with_a_skipped_8x8_intra_leaf_whose_tx_split_10bit_decodes_pixel_exact() + { + const NAME: &str = "a_real_aomenc_stream_with_a_skipped_8x8_intra_leaf_whose_tx_split_10bit_decodes_pixel_exact"; + crate::decode::reset_skip_split_tx_hits(); + run_multi_tile_gate(NAME, 0, 0, 10, 1, &[], false, None); + if have_ffmpeg() && have_aomenc() { + assert!( + crate::decode::skip_split_tx_hits() > 0, + "{NAME}: no skipped 8x8 intra leaf split its transform -- the gate proves nothing" + ); + } + } + #[test] fn a_real_aomenc_multi_tile_intra_10bit_stream_decodes_pixel_exact() { run_multi_tile_gate(