From fc77857b528702167c06a8933627f3c64c5bc9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 11:22:50 +0300 Subject: [PATCH] debug(av1): r8 clears tile-offset and mode_info as the pinned CfL stream's bug Independent from-spec OBU/frame-header parse in Python derives the tile group's start at byte 23 (obu_header -> leb128 -> full sequence_header -> full uncompressed_header bit-by-bit); EC_AV1_TRACE's new key_tile_bytes line proves decode_stream's obu_offset + tile.offset feeds decode_key_frame_tile that same byte 23, and r7's python literal agrees -- three-way match. Extended EC_AV1_TRACE to partition/skip/y_mode/uv_mode (decode.rs read_intra_mode + both partition sites) and cross-checked against an independent Python MSAC decoder whose partition_w32/kf_y_mode/uv_mode_cfl CDF literals this round re-verified byte-for-byte against /tmp/libaom-src/av1/common/entropymode.c (not inherited from a prior round): partition=NONE, skip=0, y_mode=DC_PRED, uv_mode=DC_PRED, matching ours exactly. Both r8 hypotheses from r7's handoff are falsified -- the bitstream genuinely codes DC_PRED with a near-zero residual for this block, and our decode of that is bit-exact. ffmpeg's real output (a smooth diagonal gradient) cannot come from that symbol sequence under any correct DC_PRED reconstruction, so the desync is downstream of symbol decode entirely: intra prediction or the inverse-transform/dequant path. Traces kept (not fixed yet); ignore reason on the pinned CFL test points r9 at crate::intra::predict and dequant_and_inverse_typed. --- crates/ec-av1/src/decode.rs | 37 +++++++++++++++++++++++++++++++++---- crates/ec-av1/src/stream.rs | 2 +- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 503e735b..58ebcefc 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -564,11 +564,22 @@ fn read_intra_mode( left_mode: usize, cfl: bool, ) -> Result<(bool, usize, Option<(i32, i32)>)> { + let trace = std::env::var_os("EC_AV1_TRACE").is_some(); let skip = dec.symbol(&mut cdfs.skip[0]) != 0; - let mode = - dec.symbol(&mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]]); + if trace { + eprintln!("TRACE skip value={}", skip as i32); + } + let above_ctx = INTRA_MODE_CTX[above_mode]; + let left_ctx = INTRA_MODE_CTX[left_mode]; + let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); + if trace { + eprintln!("TRACE y_mode ctx=({above_ctx},{left_ctx}) value={mode}"); + } if (V_PRED..=D67_PRED).contains(&mode) { let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]); + if trace { + eprintln!("TRACE angle_delta value={angle}"); + } if angle != ANGLE_DELTA_ZERO { return Err(unsupported( "a nonzero angle delta (this encoder never writes one)", @@ -580,6 +591,9 @@ fn read_intra_mode( } else { dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) }; + if trace { + eprintln!("TRACE uv_mode cfl={cfl} y_mode={mode} value={uv_mode}"); + } let alpha = if uv_mode == DC_PRED { None } else if cfl && uv_mode == UV_CFL_PRED { @@ -1119,6 +1133,13 @@ pub fn decode_key_frame_tile( let scan4 = default_scan(TX4); let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!( + "TRACE key_tile_bytes len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}", + data.len(), + &data[..data.len().min(8)] + ); + } let mut dec = SymbolDecoder::new(data); let mut neighbours = Neighbours::new( cols32 as usize * 2, @@ -1144,7 +1165,11 @@ pub fn decode_key_frame_tile( // mirroring [`crate::tile::sb_coeff_key_frame_tile`]'s own // three-way write exactly. let part = if has_cols && has_rows { - dec.symbol(&mut cdfs.partition_w64[ctx]) + let p = dec.symbol(&mut cdfs.partition_w64[ctx]); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!("TRACE partition_w64 ctx={ctx} value={p}"); + } + p } else { match (has_cols, has_rows) { (true, false) => { @@ -1190,7 +1215,11 @@ pub fn decode_key_frame_tile( has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), ); let part32 = if has_cols32 && has_rows32 { - dec.symbol(&mut cdfs.partition_w32[ctx32]) + let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!("TRACE partition_w32 ctx={ctx32} value={p}"); + } + p } else { match (has_cols32, has_rows32) { (true, false) => { diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 88cdb534..0821d7c9 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -513,7 +513,7 @@ mod tests { /// decode: pixel-exact against ffmpeg's own decode, not just "decoded". /// #[test] - #[ignore = "r7: the symbol-level cross-check this note asked for is done and CLEARS the coefficient decode. An EC_AV1_TRACE-gated trace (decode.rs read_coeffs/read_eob) was diffed against an independent Python MSAC decoder (real libaom default CDFs pulled from token_cdfs.h, context arithmetic ported from txb_common.h's get_nz_mag/get_nz_map_ctx_from_stats/get_br_ctx -- not from this crate). Every symbol matches ours bit-for-bit through the full Luma32 base loop: eob_pt=5, eob_extra(ctx=2)=1, eob=15, base_eob(ctx=1)=1, then base ctx/value pairs 21/0,21/0,21/0,21/0,6/0,6/0,6/0,7/0,7/2,6/0,6/0,1/0,2/2,0/2, dc_sign(ctx=0)=negative -- three nonzero levels (magnitude 2 each) at raster positions 0/1/2. The scan table, CDF tables, and every context function are proven correct for this stream; r6's transposition hypothesis stays reverted and should not be revisited. Feeding those exact levels through dequant_and_inverse_typed (base_q_idx=60) gives a near-flat residual (row 0 barely varies, rows 1-31 replicate it -- correct behaviour for energy confined to row 0) landing luma at a uniform ~126 against ffmpeg's real ~50-and-varying output: three magnitude-2 coefficients cannot close a gap that size even transformed exactly right, so the desync is BEFORE this trace starts -- mode_info (partition/skip/y_mode/uv_mode) or the tile-group byte offset itself. r8: extend EC_AV1_TRACE (already added, off by default) to mode_info's own symbols and cross-check them the same way, and separately confirm the tile data's starting byte offset against the OBU/frame-header parse (ec-av1-syntax) rather than trusting a byte slice pulled from a prior round's own decoder run."] + #[ignore = "r8: BOTH remaining hypotheses from r7 are now falsified. (1) Tile-data byte offset: an independent from-spec OBU parse in Python (obu_header/leb128/full sequence_header/full uncompressed_header bit-by-bit) derives the tile group starting at absolute byte 23 in the pinned stream; `EC_AV1_TRACE`'s new `key_tile_bytes` line shows our own `decode_stream` (stream.rs `obu_offset + tile.offset`) feeds `decode_key_frame_tile` that exact same byte 23 (first8 04 c8 d9 35 3d a6 de 5e); the r7 python scripts' hardcoded literal agrees too -- three-way match, offset is not the bug. (2) mode_info symbols: `EC_AV1_TRACE` extended to partition/skip/y_mode/uv_mode (decode.rs read_intra_mode + the two partition sites); an independent Python MSAC decoder over the SAME CDF literals -- this round re-verified partition_w32_cdf[0], kf_y_mode_cdf[0][0] and uv_mode_cdf[cfl=1][DC_PRED] byte-for-byte against /tmp/libaom-src/av1/common/entropymode.c source, not trusted from a prior round -- reads partition=NONE, skip=0, y_mode=DC_PRED(0), uv_mode=DC_PRED(0), matching our decoder exactly. So the bitstream genuinely codes DC_PRED with a near-zero residual (three magnitude-2 AC coefficients) for this block, and our decode of that is bit-exact; ffmpeg's real output (a smooth 46..61 diagonal gradient) cannot come from that symbol sequence under any correct DC_PRED reconstruction. The desync is downstream of symbol decode entirely -- intra prediction (`crate::intra::predict`) or the inverse-transform/dequant path (`dequant_and_inverse_typed`) miscomputing the reconstructed pixels from a genuinely-correct DC_PRED + tiny-residual decode, not a wrong symbol. r9: trace/diff `crate::intra::predict`'s DC output and the post-dequant residual grid against an independent implementation of both, the same way r6-r8 did for the entropy layer."] fn a_real_libaom_cfl_stream_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!("SKIP a_real_libaom_cfl_stream_decodes_pixel_exact: no ffmpeg");