From dcd3febfee102a5bedf87d8542be7a14c0573543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 28 Aug 2026 12:23:21 +0300 Subject: [PATCH] wip(av1): root-cause cdfflake-stream-seed45 pin -- reduced_tx_set=false inter tx_type gap Bisected the pinned frame-2 mismatch (cdfflake-stream-seed45.bin, 359/4096 luma px, worst delta 6, right-edge block). Added scratch instrumentation (env-gated, zero behavior change): scratch_dump_prefilt in decode.rs (EC_AV1_PREFILT_DUMP_OURS, mirrors the instrumented aomdec's own EC_AV1_PREFILT_DUMP) and an EC_RNG per-symbol trace in msac.rs (SymbolDecoder::symbol_fixed), matching libaom's aom_dsp/entdec.c format exactly (rng_before/rng_after/nsyms/symbol, shared global counter). Verdict: PIXEL-PATH, not entropy garbage-cascade in the usual sense -- pre-deblock buffers already differ (497/4096 luma mismatches at frame 2, rows 48-63 cols 32-63, exactly the mi(12,8)/mi(12,12) 16x16x2 region), proving the defect is in reconstruction, but the root symbol-level cause IS a real entropy divergence: gdb-bisected via `EC_RNG` + a conditional breakpoint on od_ec_decode_cdf_q15 in the instrumented aomdec build to n=787, which resolves (backtrace) to av1_read_tx_type -> decodetxb.c:read_coeffs_txb, nsyms=12 -- libaom's EXT_TX_SET_DTT4_IDTX_1DDCT, selected by av1_get_ext_tx_set_type(TX_16X16, is_inter=1, use_reduced_set=0) (blockd.h:1097, av1_ext_tx_set_lookup[1][1]). Our decoder read a 2-symbol table (nsyms=2) at the same point instead. Root cause: cdf_state.rs's TxbSet::Luma16Inter (and Luma8Inter) are hardcoded to the *reduced_tx_set=true* 2-symbol EXT_TX_SET_DCT_IDTX table only (doc comments there already say so explicitly); this crate's own writer always emits reduced_tx_set=true so the gap never showed up on a round-trip test. A real aomenc stream with reduced_tx_set=false (this pin) hits an inter 16x16 block whose tx_type this decoder cannot read at all -- it silently misreads a binary CDF where the real bitstream encoded a 12-way EXT_TX_SET_DTT4_IDTX_1DDCT symbol, desyncing the rest of the frame's own coefficient reads (491 more luma reads through the tile) and poisoning the CDFs forwarded into frame 3. Same-shape sibling gaps found by reading av1_get_ext_tx_set_type (NOT yet fixed, name only): 8x8 inter + reduced_tx_set=false wants EXT_TX_SET_ALL16 (16-symbol), also unimplemented (Luma8Inter is likewise reduced-only per its own doc comment). 32x32+/64x64 inter are fine (EXT_TX_SET_DCT_IDTX unconditionally regardless of reduced_tx_set, per blockd.h:1101-1102) -- Luma32Inter's existing 2-symbol table is already correct there. NOT fixed this round (budget): needs a new TxbSet::Luma16InterSet1 (12-symbol CDF, transcribed from libaom entropymode.c default_inter_ext_tx_cdf[EXT_TX_SET_DTT4_IDTX_1DDCT][TX_16X16], + the EXT_TX_SET_DTT4_IDTX_1DDCT symbol->TxType mapping from av1_ext_tx_used/inter table), wired into decode_inter_block's TxbSet selection (currently hardcoded to TxbSet::Luma16Inter at every call site, e.g. decode.rs:6484) gated on `!reduced_tx_set`, and the same for TxbSet::Luma8InterSet1 (16-symbol EXT_TX_SET_ALL16). Dying words: pull default_inter_ext_tx_cdf's 16x16-DTT4_IDTX_1DDCT row (12 probs) and the matching tx_type index table from /tmp/libaom-src/av1/common/av1_entropymode.c + av1/common/enums.h(EXT_TX_SET_DTT4_IDTX_1DDCT symbol order), add the two TxbSet variants + a reduced_tx_set-gated select at every `16 =>` / `8 if reduced_tx_set` dispatch site feeding an inter block, then rerun scratch_isolate_pinned_mismatch on cdfflake-stream-seed45.bin to confirm 4/4 frames byte-exact, then remove scratch_dump_prefilt/EC_RNG (or leave as documented scratch, orchestrator's call), then the cdf-forwarding gate 5x + full ec-av1 --lib suite 3x. --- crates/ec-av1/src/decode.rs | 26 ++++++++++++++++++++++++++ crates/ec-av1/src/msac.rs | 12 ++++++++++++ 2 files changed, 38 insertions(+) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index bf973a8b..571adc37 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2485,6 +2485,30 @@ fn cdef_filter_block( /// size. `cdef_bits == 0` is this crate's only supported case so far (see the /// refusal in `stream.rs`), so every 64x64 filter block always uses index 0's /// strengths -- no per-block `cdef_idx` selection is implemented yet. +/// bisect scratch (lane-cdffwd2): dump this frame's Y/U/V exactly the way +/// the instrumented `aomdec` build's `EC_AV1_PREFILT_DUMP` does (raw +/// `true_width`x`true_height` bytes per plane, decode order, one file per +/// frame index), gated on env so it is zero-cost/zero-behavior off. Called +/// from both the key-frame and inter-frame tile decoders so the frame index +/// lines up with aomdec's own (which dumps unconditionally every frame). +fn scratch_dump_prefilt(y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf) { + let Some(base) = std::env::var_os("EC_AV1_PREFILT_DUMP_OURS") else { + return; + }; + use std::sync::atomic::{AtomicUsize, Ordering}; + static IDX: AtomicUsize = AtomicUsize::new(0); + let idx = IDX.fetch_add(1, Ordering::SeqCst); + let path = format!("{}.f{}", base.to_string_lossy(), idx); + let mut out = Vec::new(); + for plane in [y, u, v] { + for row in 0..plane.true_height { + let start = row * plane.width; + out.extend_from_slice(&plane.data[start..start + plane.true_width]); + } + } + let _ = std::fs::write(path, out); +} + /// Spec 7.14: the in-loop deblocking filter, applied once per frame after /// tile reconstruction and before [`apply_cdef`]. Uniform-level path only -- /// `stream.rs` already refuses `delta_lf_present`/segmentation upstream, so @@ -3588,6 +3612,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( } } + scratch_dump_prefilt(&y, &u, &v); apply_deblock(&mut y, &mut u, &mut v, loop_filter, &neighbours); apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); @@ -6589,6 +6614,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( } } + scratch_dump_prefilt(&y, &u, &v); apply_deblock(&mut y, &mut u, &mut v, loop_filter, &neighbours); apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); diff --git a/crates/ec-av1/src/msac.rs b/crates/ec-av1/src/msac.rs index 53eb3c1d..dbb25df8 100644 --- a/crates/ec-av1/src/msac.rs +++ b/crates/ec-av1/src/msac.rs @@ -303,6 +303,9 @@ impl<'a> SymbolDecoder<'a> { let mut cur = self.range; let mut prev; let mut symbol = 0; + // bisect scratch (lane-cdffwd2): rng_before, matching libaom + // entdec.c's own EC_RNG trace field of the same name. + let rng_before = self.range; loop { prev = cur; let f = u32::from(CDF_TOP - cdf[symbol]); @@ -315,6 +318,15 @@ impl<'a> SymbolDecoder<'a> { } self.range = prev - cur; self.value -= cur; + if std::env::var_os("EC_RNG").is_some() { + use std::sync::atomic::{AtomicU64, Ordering}; + static N: AtomicU64 = AtomicU64::new(0); + let n = N.fetch_add(1, Ordering::SeqCst); + eprintln!( + "EC_RNG n={n} rng_before={rng_before} rng_after={} nsyms={nsyms} symbol={symbol}", + self.range + ); + } // Renormalise back into `[2^15, 2^16)`, the same shift the encoder // applied when it narrowed to this symbol.