From 21c832090b62b7b73239fcf7d59cda4a6a386507 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 17:32:02 +0300 Subject: [PATCH] wip(av1): msac symbol_fixed hand-verified correct; real desync is a switchable_interp read aomdec never performs for this block. Round 6, capped at budget. COMPILES. No behaviour change: new EC_AV1_EOBPT_CDF-gated `debug_state()`/eprintln! diagnostics only (msac.rs's new `pub(crate) fn debug_state`, decode.rs's three new state prints in `read_coeffs` and `read_eob`), same pattern as every prior round's instrumentation. R6'S OWN PLAN'S STEP 1/2 (msac.rs correctness) IS NOW CLOSED, NEGATIVE: hand-derived `symbol_fixed` (msac.rs:301-329) against libaom's actual `od_ec_decode_cdf_q15` source (`/tmp/libaom-src/aom_dsp/entdec.c:204`, read fresh this round, not from memory) term-by-term for nsyms=11 specifically -- `N = nsyms - 1`, loop increments `ret` before indexing `icdf[++ret]`, `v = ((r>>8)*(icdf[ret]>>EC_PROB_SHIFT))>>(7-EC_PROB_SHIFT) + EC_MIN_PROB*(N-ret)`, loop continues `while (c < v)`. This is algebraically IDENTICAL to our `cur = (((range>>8)*(f>>EC_PROB_SHIFT)) >>(7-EC_PROB_SHIFT)) + EC_MIN_PROB*(nsyms-1-symbol)`, loop `while value < cur`. nsyms=11 is not special-cased anywhere in either implementation. r5's suspicion (1) is FALSIFIED -- do not re-open without new evidence. DECISIVE NEW EVIDENCE (this round's actual find): added a `debug_state()` accessor to `SymbolDecoder` and printed `(range, value)` before/after the `all_zero` (txb_skip) read, after the `tx_type` read, and entering `eob_pt` -- then added the SAME state to aomdec's `decodetxb.c` (`EC_AV1_EOBPT_STATE_IN`, plus `rng_before` on the existing `EC_PLANE` trace). Result: **the coder state ALREADY diverges before the `txb_skip` read even starts** -- ours enters at `range=46855`, aomdec enters at `rng_before=50720`. So r5's "CDF + symbol byte-identical at tx_type/eob_pt" was real but downstream of an earlier, silent divergence; walking back from there: Between `EC_MV` (motion vector, matches exactly: `mv=(-2,-8)` both sides) and `EC_TXBCTX`/`EC_PLANE` (first plane's coeff decode), the ONLY symbol read possible on this path is the switchable interpolation filter (`resolve_interp_filter`, decode.rs:3726 / libaom's `read_mb_interp_filter`, decodemv.c:1122). Ours takes the *real-symbol* branch (`interp_fixed == None`, i.e. the frame header says `interpolation_filter == Switchable`) and decodes `filter=[3, 3]` (BILINEAR both directions) -- a real, adapting 3-symbol CDF read. aomdec's own trace for this exact block (`mi_row=0 mi_col=0`, frame=14, ref=1, mode=NEWMV=16) shows `interp=(0,0)` (REGULAR both directions) with **no `IFILTER_DBG` line at all** -- confirmed by instrumenting `av1_is_interp_needed()`'s own three sub-conditions directly (`IFILTER_NEEDED ... skip_mode=0 motion_mode=0 mode=16 needed=1`): the function returns TRUE (the read is not skipped by the gate), yet `read_mb_interp_filter`'s OWN symbol-read branch never fires for this block, which by its source (decodemv.c:1134, `if (interp_filter != SWITCHABLE)`) is only possible if **libaom's own per-frame `features->interp_filter` is a FIXED value for frame 14, not `SWITCHABLE`** -- i.e. aomdec's `interpolation_filter` header field for THIS frame differs from what our decoder computed. CONCLUSION / R7 HANDOFF, IN ORDER: 1. This is very likely NOT an msac bug at all (closed, see above). The live suspect is `read_interpolation_filter()` (`ec-av1-syntax/src/frame.rs:1165`) or, more likely given the header bit-parsing there matches spec 5.9.10 exactly on inspection, a **frame-header/frame-index MISMATCH**: this pin's "decode-order frame 1" may not be resolving to the same underlying temporal-unit frame aomdec calls its own "frame=14" -- ties back to f952bb4's already-flagged "hidden frame" suspicion from round 3, never fully closed. Confirm by dumping `header.interpolation_filter` (and ideally `show_frame`/`frame_type`/`order_hint`) for EVERY frame header our decoder parses in this stream, aligned against aomdec's own `cm->current_frame.frame_number` sequence (add an `EC_AV1_FRAME_NUMBER`-style eprintln! to `av1_read_coeffs_txb` or the frame-header decode point on aomdec's side, matching the counters already in place) -- find the first frame where the two disagree on `interpolation_filter`, and from there whether it's a genuine off-by-one in which OBU/frame our pin harness treats as "decode-order frame 1", or a real per-frame header field ours reads as `Switchable` when it should not. 2. If frame alignment IS correct and headers genuinely disagree on this field for the SAME frame: re-derive `read_interpolation_filter` against spec 5.9.10 word-for-word once more (looks correct on this round's read, but re-check the surrounding `allow_high_precision_mv` /`force_integer_mv` order just above it, frame.rs:963-968 -- an earlier misread bit could shift every subsequent header field by one bit position without desyncing the *tile* data at all, which is exactly the shape of "everything downstream of one header field looks locally consistent but globally wrong"). 3. Once the true cause is found and fixed, re-run the pin (`EC_AV1_PIN_N=24`) for exactness, THEN full `cargo test -p ec-av1 --release` (not just `--lib`) per existing done-criteria -- a header-parsing fix is even more likely than an msac fix to touch every stream. 4. If the pin goes exact: run the three flaking gates 15x each (`compound_references`, `reference_select`, `deblocking`) and report pass counts honestly. Instrumentation added this round, env-gated (no behaviour change when unset): `SymbolDecoder::debug_state()` (msac.rs, `pub(crate)`, mirrors the existing `debug_bitpos()`), three new `EC_AV1_EOBPT_CDF`-gated state prints in decode.rs (`EC_AV1_STATE_BEFORE_TXBSKIP`, `EC_AV1_STATE_AFTER_TXBSKIP`, `EC_AV1_STATE_AFTER_TXTYPE`). External, not committed to this repo: `/tmp/libaom-src`'s `av1/decoder/decodetxb.c` (`EC_AV1_EOBPT_STATE_IN`, `rng_before` on `EC_PLANE`) and `av1/decoder/decodemv.c` (`IFILTER_NEEDED`, gated on the pre-existing `EC_IFILTER_DBG`) -- rebuilt into `/tmp/libaom-src/build/decoder-debug/aomdec`; this source tree is outside the repo and NOT reproducible from a fresh checkout, note for whoever runs r7 without the same `/tmp` state. class: sibling of [[stale-premise-lanes]] -- r4 and r5 both suspected mechanisms one level too deep (a table's forwarding, then a coder primitive) because both were proven correct in isolation without first walking the state itself backward to its true entry point; the actual divergence sits one whole syntax element earlier (a per-block symbol read that should not have fired at all), in territory ([[hidden frame]] frame-index resolution) an even earlier round (f952bb4) already named and left open. --- crates/ec-av1/src/decode.rs | 15 ++++++++++++++- crates/ec-av1/src/msac.rs | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 61eb91c6..c17c7e72 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -551,7 +551,8 @@ fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> (_, Some(class1)) => class1, }; if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() && eob_pt.len() == 12 { - eprintln!("EC_AV1_EOBPT_CDF {eob_pt:?}"); + let (range, value) = dec.debug_state(); + eprintln!("EC_AV1_EOBPT_CDF {eob_pt:?} range={range} value={value}"); } let group = dec.symbol(eob_pt) + 1; if trace { @@ -622,10 +623,18 @@ fn read_coeffs( let trace = std::env::var_os("EC_AV1_TRACE").is_some(); let side = coding.side; let mut grid = vec![0i32; side * side]; + if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { + let (range, value) = dec.debug_state(); + eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); + } let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; if trace { eprintln!("TRACE all_zero ctx={skip_ctx} value={}", all_zero as i32); } + if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { + let (range, value) = dec.debug_state(); + eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}"); + } if all_zero { return Ok((grid, TxType::DctDct)); } @@ -644,6 +653,10 @@ fn read_coeffs( eprintln!("EC_AV1_TXTYPE32_CDF {tx_type_cdf:?}"); } let t = dec.symbol(tx_type_cdf); + if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { + let (range, value) = dec.debug_state(); + eprintln!("EC_AV1_STATE_AFTER_TXTYPE range={range} value={value}"); + } if trace { eprintln!("TRACE tx_type value={t} len={len}"); } diff --git a/crates/ec-av1/src/msac.rs b/crates/ec-av1/src/msac.rs index 53eb3c1d..b9be77e2 100644 --- a/crates/ec-av1/src/msac.rs +++ b/crates/ec-av1/src/msac.rs @@ -335,6 +335,14 @@ impl<'a> SymbolDecoder<'a> { self.bit } + /// Debug-only: the raw coder state (`range`, `value`) entering the next + /// read, for a byte-for-byte diff against a reference decoder's own + /// `r->ec.rng`/`r->ec.dif` while chasing a desync (see + /// `EC_AV1_EOBPT_CDF`). Not spec output, just the two live fields. + pub(crate) fn debug_state(&self) -> (u32, u32) { + (self.range, self.value) + } + /// Reads `bits` raw bits, most significant first — the `L(n)` descriptor /// of spec 4.10.4. pub fn literal(&mut self, bits: u32) -> u32 {