From 36c2f06ac474c1498ee5afd9175db94f5c488673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 18:33:39 +0300 Subject: [PATCH] diag(av1): sbpart r7 -- per-symbol coeff range ladder localizes block2's true divergence Rung 3's entry/exit EC_TRACE_COEFF proved block2's luma coefficient read (TxbSet::Luma64 corner, decode.rs:3214) is where the desync actually starts: entry rng=63764 matches the oracle exactly (confirming r6's mode-info prefix finding held all the way through skip/depth), but the luma read's own exit range (46984 ours vs 46344 oracle) diverges -- overturning r6's suspicion that around_rect's neighbour gather was the culprit; the gather only feeds skip_ctx/dc_sign_ctx, and both of those read identically (all_zero=0 both sides, dc_sign both agree). Added a new oracle rung (11, scripts/instrument-aom-oracle.sh, same EC_TRACE_COEFF flag) printing per-symbol rng inside read_coeffs_txb (eob/base_eob/after_bases/sign/post_golomb), plus matching entry/exit EC_COEFF/EC_COEFF_VAL markers at all 3 of decode_block_rect64's own read_coeffs/read_coeffs_rect call sites and rng on read_eob's own TRACE eob_pt/eob_extra lines. Range-laddered block2's luma block byte-for-byte against the oracle: eob (6) and base_eob (level=1) both match. First value divergence is the "base" symbol at scan position row=1,col=0 (pos=32) -- ours decodes value=3 (triggering an extra "br" read, final level 3), the oracle's equivalent position decodes level=1 with no br read at all. This is a NEW, sharper suspect than r6's around_rect/context-gather framing: the coefficient magnitude symbol itself (base_ctx's context computation, or the CDF row it indexes) for this specific scan position inside the 32x32 corner scan. Diagnostics only, no behavior change to the live gate (still red, same as r6 left it) -- sb_rect_hits()>0 unproven this round, not attempted since the fix itself wasn't reached within budget. cargo check -p ec-av1 --tests: clean, only pre-existing missing-doc warnings. Oracle rung env-gated, applied to the shared ~/.cache/aom-oracle tree and rebuilt (ninja aomdec clean), also captured in scripts/instrument-aom-oracle.sh so it reproduces from a fresh clone. --- crates/ec-av1/src/decode.rs | 26 ++++++++++- scripts/instrument-aom-oracle.sh | 78 ++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index d825f43b..ae93f329 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1180,7 +1180,7 @@ fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> } let group = dec.symbol(eob_pt) + 1; if trace { - eprintln!("TRACE eob_pt value={group}"); + eprintln!("TRACE eob_pt value={group} rng={}", dec.debug_state().0); } let bits = OFFSET_BITS[group]; let mut offset = 0u32; @@ -3216,6 +3216,10 @@ fn decode_block_rect64( // grid (see this function's own doc comment). let scan32 = default_scan(TX32); let mut luma_coding = cdfs.txb(TxbSet::Luma64, mode); + if std::env::var_os("EC_TRACE_COEFF").is_some() { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF plane=0 row={mi_r} col={mi_c} tx_size=64corner rng={rng}"); + } let (luma_corner, luma_tx_type) = read_coeffs( dec, &mut luma_coding, @@ -3224,6 +3228,10 @@ fn decode_block_rect64( dc_sign_ctx(around[0].2), TxType::DctDct, )?; + if std::env::var_os("EC_TRACE_COEFF").is_some() { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF_VAL plane=0 row={mi_r} col={mi_c} rng={rng}"); + } let mut luma_levels = vec![0i32; bw * bh]; for row in 0..32 { luma_levels[row * bw..][..32].copy_from_slice(&luma_corner[row * 32..][..32]); @@ -3255,6 +3263,10 @@ fn decode_block_rect64( let chroma_scan: &[u16] = if bw == 64 { &SCAN_32X16 } else { &SCAN_16X32 }; let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); let mut u_coding = cdfs.txb(TxbSet::ChromaRect32x16, uv_predict_mode); + if std::env::var_os("EC_TRACE_COEFF").is_some() { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF plane=1 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); + } let (u_levels, u_tx_type) = read_coeffs_rect( dec, &mut u_coding, @@ -3265,6 +3277,10 @@ fn decode_block_rect64( dc_sign_ctx(around[1].2), TxType::DctDct, )?; + if std::env::var_os("EC_TRACE_COEFF").is_some() { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); + } let u_residual = dequant_and_inverse_typed_wh( &u_levels, chroma_w, @@ -3288,6 +3304,10 @@ fn decode_block_rect64( ); let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); let mut v_coding = cdfs.txb(TxbSet::ChromaRect32x16, uv_predict_mode); + if std::env::var_os("EC_TRACE_COEFF").is_some() { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF plane=2 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); + } let (v_levels, v_tx_type) = read_coeffs_rect( dec, &mut v_coding, @@ -3298,6 +3318,10 @@ fn decode_block_rect64( dc_sign_ctx(around[2].2), TxType::DctDct, )?; + if std::env::var_os("EC_TRACE_COEFF").is_some() { + let (rng, _) = dec.debug_state(); + eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); + } let v_residual = dequant_and_inverse_typed_wh( &v_levels, chroma_w, diff --git a/scripts/instrument-aom-oracle.sh b/scripts/instrument-aom-oracle.sh index 0231c009..ebbb3651 100755 --- a/scripts/instrument-aom-oracle.sh +++ b/scripts/instrument-aom-oracle.sh @@ -563,3 +563,81 @@ s = s.replace(marker_old, "/* EC_INSTRUMENTED_INTRA_STEP */\n" + marker_old, 1) open(path, "w").write(s) print("intra mode-info per-symbol ladder instrumented") PYS + +# --- rung 11: per-symbol range ladder inside read_coeffs_txb (decodetxb.c) -- +# lane-sbpart r7: rung 3's EC_TRACE_COEFF only bracketed a whole coefficient +# block (entry/exit rng) -- too coarse to find WHICH symbol inside a Luma64 +# corner-scan block diverges. This adds EC_COEFF_STEP tag=eob/base_eob/ +# after_bases/sign/post_golomb lines, rng after each, under the same +# EC_TRACE_COEFF flag (no new env var). Localized r7's own bisect to the +# `base` symbol at scan position (row=1,col=0) inside block2's luma corner: +# entry rng and eob/base_eob both match ours byte-for-byte, then our `base` +# read at that position decodes value=3 (triggering an extra `br` read) +# where the oracle's equivalent position decodes level=1 -- see +# lanes/sbpart-r7.report.md. +G="$SRC/av1/decoder/decodetxb.c" +[ -f "$G" ] || { echo "no oracle source at $G" >&2; exit 1; } + +python3 - "$G" <<'PYC11' +import sys +path = sys.argv[1] +s = open(path).read() +if "EC_COEFF_STEP" in s: + print("decodetxb rung 11 already applied (no-op)") + sys.exit(0) + +old1 = " *eob = rec_eob_pos(eob_pt, eob_extra);\n" +new1 = old1 + """ { + const int ec_trace2 = getenv("EC_TRACE_COEFF") != NULL; + if (ec_trace2) { + fprintf(stderr, "EC_COEFF_STEP tag=eob eob=%d rng=%u\\n", *eob, (unsigned)r->ec.rng); + } + } +""" +assert old1 in s, "eob assign not found" +s = s.replace(old1, new1, 1) + +old2 = " levels[get_padded_idx(pos, bhl)] = level;\n }\n if (*eob > 1) {" +new2 = """ levels[get_padded_idx(pos, bhl)] = level; + if (getenv("EC_TRACE_COEFF") != NULL) { + fprintf(stderr, "EC_COEFF_STEP tag=base_eob level=%d rng=%u\\n", level, (unsigned)r->ec.rng); + } + } + if (*eob > 1) {""" +assert old2 in s, "base_eob block not found" +s = s.replace(old2, new2, 1) + +old3 = """ read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bhl, + levels, base_cdf, br_cdf); + } + } +""" +new3 = old3 + """ if (getenv("EC_TRACE_COEFF") != NULL) { + fprintf(stderr, "EC_COEFF_STEP tag=after_bases rng=%u\\n", (unsigned)r->ec.rng); + } +""" +assert old3 in s, "after_bases site not found" +s = s.replace(old3, new3, 1) + +old4 = """ if (level >= MAX_BASE_BR_RANGE) { + level += read_golomb(xd, r); + } + + if (c == 0) dc_val = sign ? -level : level;""" +new4 = """ if (getenv("EC_TRACE_COEFF") != NULL) { + fprintf(stderr, "EC_COEFF_STEP tag=sign c=%d sign=%d rng=%u\\n", c, sign, (unsigned)r->ec.rng); + } + if (level >= MAX_BASE_BR_RANGE) { + level += read_golomb(xd, r); + } + if (getenv("EC_TRACE_COEFF") != NULL) { + fprintf(stderr, "EC_COEFF_STEP tag=post_golomb c=%d level=%d rng=%u\\n", c, level, (unsigned)r->ec.rng); + } + + if (c == 0) dc_val = sign ? -level : level;""" +assert old4 in s, "sign/golomb site not found" +s = s.replace(old4, new4, 1) + +open(path, "w").write(s) +print("decodetxb rung 11 (per-symbol coeff ladder) instrumented") +PYC11