From a2a84c73ed4b28f78d38fdfdc47e1a2511faa4dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 24 Sep 2026 18:52:55 +0300 Subject: [PATCH] lane-av1-mixloss: lossless-segment leaves code no tx-depth symbol (read_tx_size's lossless return precedes TX_MODE_SELECT) + env-gated EC_ECDUMP register dump --- crates/ec-av1/src/decode.rs | 34 ++++++++++-- crates/ec-av1/src/msac.rs | 102 ++++++++++++++++++++++++++++++++---- 2 files changed, 123 insertions(+), 13 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 2024215f..709275cc 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -6625,6 +6625,13 @@ fn read_coeffs( let (range, value) = dec.debug_state(); eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); } + if crate::envflags::env_flag!("EC_ECDUMP_IN") { + let (range, value) = dec.debug_state(); + let bit = dec.debug_bitpos(); + eprintln!( + "EC_ECDUMP_IN side={side} ctx={skip_ctx} dcctx={sign_ctx} ECIN=({value},{range},{bit})" + ); + } let dbg_txbskip = if coeff_trace_on() { coding.txb_skip[skip_ctx] } else { @@ -8340,9 +8347,18 @@ impl Neighbours { (0..w_mi).map(|k| self.above[mi_c + k][0].level.to_string()).collect(); let lf: Vec = (0..h_mi).map(|k| self.left[mi_r + k][0].level.to_string()).collect(); + // lane-av1mix: the last few msac register states, so the + // byte-position of the `all_zero` read can be matched against + // aomdec's `EC=(value,rng,bytepos)` dump (decodetxb.c) without a + // second run. + let recent = crate::msac::symtrace::ecdump_recent(4) + .iter() + .map(|(_low, value, rng, bytepos)| format!("({value},{rng},{bytepos})")) + .collect::>() + .join(" "); eprintln!( - "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},]", - ab.join(","), lf.join(",") + "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},] recentEC=[{}]", + ab.join(","), lf.join(","), recent ); } if crate::envflags::env_flag!("EC_AV1_TRACE") { @@ -18240,7 +18256,13 @@ fn decode_leaf_rect8( // Missing this read consumed one symbol fewer than the encoder wrote // and desynced the tile at the very first rect leaf (class // `symbol-consumption-gap`). - let depth = if tx_select { + // lane-av1mix: libaom `read_tx_size`'s FIRST line + // (`decodeframe.c:1203`, `if (xd->lossless[...]) return TX_4X4`) + // fires before the `TX_MODE_SELECT` test, so a lossless-segment leaf + // codes NO depth symbol even in `TxMode::Select` -- the mixed-lossless + // frame's sub8 leaves forked exactly here (ours read the symbol at + // (12477,50864) bit 3011, the oracle's all_zero used it). + let depth = if tx_select && !lossless(fctx) { // lane-av1txbands: the `TXFM_CONTEXT` band read every other rect // reader already does. let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); @@ -33524,7 +33546,11 @@ fn decode_intra_sub8_leaf( // context bands, which on an inter frame are live. // `block_signals_txsize(BLOCK_4X4)` is false (blockd.h:1027): the 4x4 leaf // codes no depth symbol at all, its transform is its own size. - let depth = if tx_select && bw != bh { + // lane-av1mix: the lossless early return (`decodeframe.c:1203`) sits + // before the `TX_MODE_SELECT` test, so a lossless-segment rect leaf codes + // no depth symbol either (sweep of the class fixed at the key-frame pair + // path). + let depth = if tx_select && bw != bh && !lossless(fctx) { let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); let d = dec.symbol(&mut cdfs.tx_size_cat0[ctx]); if step { diff --git a/crates/ec-av1/src/msac.rs b/crates/ec-av1/src/msac.rs index 51193acd..9e7548f8 100644 --- a/crates/ec-av1/src/msac.rs +++ b/crates/ec-av1/src/msac.rs @@ -402,7 +402,11 @@ impl<'a> SymbolDecoder<'a> { // TABLE it read, so every coding tool is accounted without a // per-call-site label. Off (`EC_AV1_BITCENSUS` unset) this is one // relaxed load. - let t0 = if crate::census::armed() { self.tell_bits() } else { 0.0 }; + let t0 = if crate::census::armed() { + self.tell_bits() + } else { + 0.0 + }; let s = self.symbol_fixed(cdf); // lane-av1txr: spec 5.9.2's `disable_cdf_update` -- a frame that sets // it codes every symbol against the tables it started with, so this @@ -410,7 +414,7 @@ impl<'a> SymbolDecoder<'a> { // allow_update_cdf && !cm->features.disable_cdf_update`, // decodeframe.c:2909). The flag is per-reader (see `Self::adapt`). if self.adapt { - update_cdf(cdf, s); + update_cdf(cdf, s); } if crate::census::armed() { crate::census::charge(cdf.as_ptr() as usize, self.tell_bits() - t0); @@ -443,13 +447,16 @@ impl<'a> SymbolDecoder<'a> { #[inline] fn symbol_fixed_raw(&mut self, cdf: &[u16]) -> usize { let nsyms = cdf.len() - 1; + if symtrace::ecdump_armed() { + symtrace::ecdump(0, self.value, self.range, self.bit as i64); + } // Two-symbol alphabets are most of the stream (every flag, every bit of // a literal), and their walk has exactly one boundary: computing it // straight skips the loop and its bounds work. if nsyms == 2 { let f = u32::from(CDF_TOP - cdf[0]); - let cur = (((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) - + EC_MIN_PROB; + let cur = + (((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) + EC_MIN_PROB; return self.bool_cur(cur); } // Loop-invariant: the coder's range does not move until the symbol is @@ -829,13 +836,21 @@ pub(crate) mod tests { (a, b as u32) }; assert_eq!(a, b, "item {i} (pick {pick})"); - assert_eq!(a, if pick == usize::MAX { a } else { arg }, "item {i} vs written"); + assert_eq!( + a, + if pick == usize::MAX { a } else { arg }, + "item {i} vs written" + ); assert_eq!( fast.debug_state(), slow.debug_state(), "coder state after item {i} (pick {pick})" ); - assert_eq!(fast_cdfs[pick.min(14)], slow_cdfs[pick.min(14)], "cdf after item {i}"); + assert_eq!( + fast_cdfs[pick.min(14)], + slow_cdfs[pick.min(14)], + "cdf after item {i}" + ); } } @@ -898,6 +913,7 @@ pub(crate) mod tests { /// Off (the variable unset) this is one relaxed load per symbol. pub(crate) mod symtrace { use std::cell::RefCell; + use std::collections::VecDeque; use std::sync::OnceLock; static DIR: OnceLock> = OnceLock::new(); @@ -906,11 +922,76 @@ pub(crate) mod symtrace { thread_local! { static BUF: RefCell<(String, usize)> = const { RefCell::new((String::new(), 0)) }; + static ECD: RefCell> = RefCell::new(VecDeque::new()); + static ECD_READER: RefCell> = const { RefCell::new(None) }; + static ECD_IDX: RefCell = const { RefCell::new(0) }; + } + + /// lane-av1mix: `(low, value, rng, bitpos)` state register dump, one line + /// per symbol read, byte-for-byte diffable against libaom's `EC_ECDUMP` + /// (`decodetxb.c`, `r->ec.dif` masked to 16 bits / `r->ec.rng` / + /// `od_ec_dec_tell`). Reader-only, `EC_AV1_ECDUMP=`: each reader + /// writes `/ecdump-.txt`. + pub(crate) fn ecdump(low: u32, value: u32, rng: u32, bitpos: i64) { + if !ecdump_armed() { + return; + } + let dir = std::env::var("EC_AV1_ECDUMP").unwrap_or_else(|_| "/tmp".to_string()); + let idx = ECD_IDX.with(|c| { + let mut g = c.borrow_mut(); + let n = *g; + *g += 1; + n + }); + let _ = idx; + let line = format!("{idx} ({low},{value},{rng},{bitpos})\n"); + ECD_READER.with(|f| { + let mut slot = f.borrow_mut(); + if slot.is_none() { + let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let path = format!("{dir}/ecdump-{seq:05}.txt"); + *slot = Some(std::fs::File::create(&path).expect("ecdump file")); + eprintln!("ECDUMP -> {path}"); + } + use std::io::Write; + let _ = slot.as_mut().unwrap().write_all(line.as_bytes()); + }); + ECD.with(|q| { + let mut q = q.borrow_mut(); + q.push_back((low, value, rng, bitpos as u32)); + if q.len() > 512 { + q.pop_front(); + } + }); + } + + /// lane-av1mix: the last `n` dumped register states (most recent last). + #[allow(dead_code)] // diagnostic helper for lane sessions + pub(crate) fn ecdump_recent(n: usize) -> Vec<(u32, u32, u32, u32)> { + if std::env::var_os("EC_AV1_ECDUMP").is_none() { + return Vec::new(); + } + ECD.with(|q| { + let q = q.borrow(); + let skip = q.len().saturating_sub(n); + q.iter().skip(skip).copied().collect() + }) } 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() + DIR.get_or_init(|| { + crate::envflags::var("EC_AV1_SYMTRACE") + .ok() + .filter(|v| !v.is_empty()) + }) + .as_deref() + } + + /// lane-av1mix: `EC_AV1_ECDUMP=` arms the decoder state-register dump. + pub(crate) fn ecdump_armed() -> bool { + static ARMED: std::sync::LazyLock = + std::sync::LazyLock::new(|| std::env::var_os("EC_AV1_ECDUMP").is_some()); + *ARMED } /// `EC_AV1_SYMTRACE_AT=` or `=:` (inclusive). @@ -940,7 +1021,10 @@ pub(crate) mod symtrace { 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()); + eprintln!( + "SYMTRACE at {n}: {}", + std::backtrace::Backtrace::force_capture() + ); } buf.push_str(&format!("{n} {value} {nsyms} {cdf0}\n")); *n += 1;