From d2974e01951c714746db061f206fdf3231ae53ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 8 Sep 2026 10:28:34 +0300 Subject: [PATCH] wip(av1): sb128b lane snapshot at the turn cap (orchestrator) --- crates/ec-av1/src/decode.rs | 3 ++ crates/ec-av1/src/msac.rs | 101 ++++++++++++++++++++++++++++++++++- crates/ec-av1/src/mvstack.rs | 18 +++++++ 3 files changed, 121 insertions(+), 1 deletion(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index ac0a542a..2320cada 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -28177,6 +28177,9 @@ fn decode_inter_block( }; (mv, false) }; + crate::msac::symtrace::note(&format!( + " MODE mi=({mi_row},{mi_col}) dbg={dbg_mode} mv={mv:?}" + )); if crate::envflags::env_flag!("EC_TRACE_MODE") { for (i, e) in stack.entries.iter().enumerate() { eprintln!( diff --git a/crates/ec-av1/src/msac.rs b/crates/ec-av1/src/msac.rs index ab975d5b..636c7956 100644 --- a/crates/ec-av1/src/msac.rs +++ b/crates/ec-av1/src/msac.rs @@ -144,6 +144,9 @@ impl SymbolEncoder { /// shorter than a two-symbol alphabet's three entries. pub fn symbol_fixed(&mut self, symbol: usize, cdf: &[u16]) { assert!(cdf.len() >= 3, "a CDF covers at least two symbols"); + if !self.pricing && symtrace::dir().is_some() { + symtrace::push(symbol, cdf.len() - 1, cdf[0]); + } let nsyms = cdf.len() - 1; assert!(symbol < nsyms, "symbol {symbol} is outside the alphabet"); @@ -201,6 +204,7 @@ impl SymbolEncoder { /// payload end on a byte boundary with no terminator. pub fn finish(mut self) -> Vec { assert!(!self.pricing, "a pricer holds no bytes to flush"); + symtrace::flush('w'); // Round the interval's low end up to a value with 14 low zero bits, // then set bit 14: the result is inside the interval (the interval is // at least 2^15 wide before normalisation) and its tail bits are all @@ -303,6 +307,12 @@ pub struct SymbolDecoder<'a> { max_bits: i32, } +impl Drop for SymbolDecoder<'_> { + fn drop(&mut self) { + symtrace::flush('r'); + } +} + impl<'a> SymbolDecoder<'a> { /// `init_symbol` (spec 8.2.2). #[must_use] @@ -397,6 +407,16 @@ impl<'a> SymbolDecoder<'a> { /// [`SymbolEncoder::symbol_fixed`]. #[inline] pub fn symbol_fixed(&mut self, cdf: &[u16]) -> usize { + if symtrace::dir().is_some() { + let s = self.symbol_fixed_raw(cdf); + symtrace::push(s, cdf.len() - 1, cdf[0]); + return s; + } + self.symbol_fixed_raw(cdf) + } + + #[inline] + fn symbol_fixed_raw(&mut self, cdf: &[u16]) -> usize { let nsyms = cdf.len() - 1; // Two-symbol alphabets are most of the stream (every flag, every bit of // a literal), and their walk has exactly one boundary: computing it @@ -493,7 +513,11 @@ impl<'a> SymbolDecoder<'a> { // `EQUIPROBABLE`'s single boundary: `f >> EC_PROB_SHIFT` is 256, so // the range multiply is a shift. let cur = ((self.range >> 8) << 7) + EC_MIN_PROB; - v = (v << 1) | self.bool_cur(cur) as u32; + let bit = self.bool_cur(cur); + if symtrace::dir().is_some() { + symtrace::push(bit, 2, EQUIPROBABLE[0]); + } + v = (v << 1) | bit as u32; } v } @@ -790,3 +814,78 @@ pub(crate) mod tests { } } } + +/// Symbol-by-symbol writer/reader trace (`EC_AV1_SYMTRACE=`), the +/// instrument a desync between OUR writer and OUR reader is bisected with: +/// every non-pricing [`SymbolEncoder`] and every [`SymbolDecoder`] dumps one +/// line per symbol (`index value nsyms cdf0`) into `/w-.txt` / +/// `/r-.txt`, and the first line where the two differ names the +/// divergent symbol. `EC_AV1_SYMTRACE_AT=` additionally prints a +/// backtrace at that index on both sides, which names the syntax element. +/// Off (the variable unset) this is one relaxed load per symbol. +pub(crate) mod symtrace { + use std::cell::RefCell; + use std::sync::OnceLock; + + static DIR: OnceLock> = OnceLock::new(); + static AT: OnceLock> = OnceLock::new(); + static SEQ: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + + thread_local! { + static BUF: RefCell<(String, usize)> = const { RefCell::new((String::new(), 0)) }; + } + + pub(crate) fn dir() -> Option<&'static str> { + DIR.get_or_init(|| crate::envflags::var("EC_AV1_SYMTRACE").ok().filter(|v| !v.is_empty())) + .as_deref() + } + + /// `EC_AV1_SYMTRACE_AT=` or `=:` (inclusive). + fn at() -> Option<(usize, usize)> { + *AT.get_or_init(|| { + let v = crate::envflags::var("EC_AV1_SYMTRACE_AT").ok()?; + let (a, b) = v.split_once(':').unwrap_or((v.as_str(), v.as_str())); + Some((a.parse().ok()?, b.parse().ok()?)) + }) + } + + /// A caller's own annotation, interleaved with this coder's symbol lines + /// (`# ...`) so the writer/reader diff shows the state around a desync. + pub(crate) fn note(line: &str) { + if dir().is_none() { + return; + } + BUF.with(|b| { + let (buf, _) = &mut *b.borrow_mut(); + buf.push_str("# "); + buf.push_str(line); + buf.push('\n'); + }); + } + + pub(crate) fn push(value: usize, nsyms: usize, cdf0: u16) { + BUF.with(|b| { + let (buf, n) = &mut *b.borrow_mut(); + if at().is_some_and(|(a, b)| (a..=b).contains(n)) { + eprintln!("SYMTRACE at {n}: {}", std::backtrace::Backtrace::force_capture()); + } + buf.push_str(&format!("{n} {value} {nsyms} {cdf0}\n")); + *n += 1; + }); + } + + /// Flushes this coder's lines as `/-.txt` and starts the + /// next coder's trace. + pub(crate) fn flush(side: char) { + let Some(dir) = dir() else { return }; + BUF.with(|b| { + let (buf, n) = &mut *b.borrow_mut(); + if !buf.is_empty() { + let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let _ = std::fs::write(format!("{dir}/{side}-{seq:05}.txt"), buf.as_bytes()); + } + buf.clear(); + *n = 0; + }); + } +} diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index f6ac1631..342c7935 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -1742,6 +1742,24 @@ pub fn find_mv_stack_with_sign_bias( .checked_sub(1) .is_some_and(|c| grid.skip_at(mi_row, c)); + // lane-sb128b: the writer/reader stack diff instrument (`EC_AV1_MVDBG=1`) + // -- one line per built stack, in build order, so the two sides' logs + // diff to the first block whose candidate list differs. + if crate::msac::symtrace::dir().is_some() { + crate::msac::symtrace::note(&format!( + "MVDBG mi=({mi_row},{mi_col}) b=({bw4}x{bh4}) ref={ref_frame} n={} mv={:?}", + candidates.len(), + candidates.iter().map(|e| (e.mv, e.weight)).collect::>(), + )); + let row = mi_row.checked_sub(1).map(|r| { + (mi_col..mi_col + bw4).map(|c| grid.get(r, c).map(|m| (m.ref_frame, m.mv))).collect::>() + }); + let col = mi_col.checked_sub(1).map(|c| { + (mi_row..mi_row + bh4).map(|r| grid.get(r, c).map(|m| (m.ref_frame, m.mv))).collect::>() + }); + crate::msac::symtrace::note(&format!(" above={row:?}")); + crate::msac::symtrace::note(&format!(" left={col:?}")); + } MvStack { entries: candidates, nearest_mv,