forge
Explore
Log in

Don't you dare go Hollow

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

Browse files at this commit

a2a84c73ed4b28f78d38fdfdc47e1a2511faa4ddAhmet Kaan GümüşParent: 322c515
crates/ec-av1/src/decode.rsmodified+30 -4
@@ -6625,6 +6625,13 @@ fn read_coeffs(
66256625 let (range, value) = dec.debug_state();
66266626 eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}");
66276627 }
6628+ if crate::envflags::env_flag!("EC_ECDUMP_IN") {
6629+ let (range, value) = dec.debug_state();
6630+ let bit = dec.debug_bitpos();
6631+ eprintln!(
6632+ "EC_ECDUMP_IN side={side} ctx={skip_ctx} dcctx={sign_ctx} ECIN=({value},{range},{bit})"
6633+ );
6634+ }
66286635 let dbg_txbskip = if coeff_trace_on() {
66296636 coding.txb_skip[skip_ctx]
66306637 } else {
@@ -8340,9 +8347,18 @@ impl Neighbours {
83408347 (0..w_mi).map(|k| self.above[mi_c + k][0].level.to_string()).collect();
83418348 let lf: Vec<String> =
83428349 (0..h_mi).map(|k| self.left[mi_r + k][0].level.to_string()).collect();
8350+ // lane-av1mix: the last few msac register states, so the
8351+ // byte-position of the `all_zero` read can be matched against
8352+ // aomdec's `EC=(value,rng,bytepos)` dump (decodetxb.c) without a
8353+ // second run.
8354+ let recent = crate::msac::symtrace::ecdump_recent(4)
8355+ .iter()
8356+ .map(|(_low, value, rng, bytepos)| format!("({value},{rng},{bytepos})"))
8357+ .collect::<Vec<_>>()
8358+ .join(" ");
83438359 eprintln!(
8344- "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},]",
8345- ab.join(","), lf.join(",")
8360+ "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},] recentEC=[{}]",
8361+ ab.join(","), lf.join(","), recent
83468362 );
83478363 }
83488364 if crate::envflags::env_flag!("EC_AV1_TRACE") {
@@ -18240,7 +18256,13 @@ fn decode_leaf_rect8(
1824018256 // Missing this read consumed one symbol fewer than the encoder wrote
1824118257 // and desynced the tile at the very first rect leaf (class
1824218258 // `symbol-consumption-gap`).
18243- let depth = if tx_select {
18259+ // lane-av1mix: libaom `read_tx_size`'s FIRST line
18260+ // (`decodeframe.c:1203`, `if (xd->lossless[...]) return TX_4X4`)
18261+ // fires before the `TX_MODE_SELECT` test, so a lossless-segment leaf
18262+ // codes NO depth symbol even in `TxMode::Select` -- the mixed-lossless
18263+ // frame's sub8 leaves forked exactly here (ours read the symbol at
18264+ // (12477,50864) bit 3011, the oracle's all_zero used it).
18265+ let depth = if tx_select && !lossless(fctx) {
1824418266 // lane-av1txbands: the `TXFM_CONTEXT` band read every other rect
1824518267 // reader already does.
1824618268 let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx);
@@ -33524,7 +33546,11 @@ fn decode_intra_sub8_leaf(
3352433546 // context bands, which on an inter frame are live.
3352533547 // `block_signals_txsize(BLOCK_4X4)` is false (blockd.h:1027): the 4x4 leaf
3352633548 // codes no depth symbol at all, its transform is its own size.
33527- let depth = if tx_select && bw != bh {
33549+ // lane-av1mix: the lossless early return (`decodeframe.c:1203`) sits
33550+ // before the `TX_MODE_SELECT` test, so a lossless-segment rect leaf codes
33551+ // no depth symbol either (sweep of the class fixed at the key-frame pair
33552+ // path).
33553+ let depth = if tx_select && bw != bh && !lossless(fctx) {
3352833554 let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx);
3352933555 let d = dec.symbol(&mut cdfs.tx_size_cat0[ctx]);
3353033556 if step {
crates/ec-av1/src/msac.rsmodified+93 -9
@@ -402,7 +402,11 @@ impl<'a> SymbolDecoder<'a> {
402402 // TABLE it read, so every coding tool is accounted without a
403403 // per-call-site label. Off (`EC_AV1_BITCENSUS` unset) this is one
404404 // relaxed load.
405- let t0 = if crate::census::armed() { self.tell_bits() } else { 0.0 };
405+ let t0 = if crate::census::armed() {
406+ self.tell_bits()
407+ } else {
408+ 0.0
409+ };
406410 let s = self.symbol_fixed(cdf);
407411 // lane-av1txr: spec 5.9.2's `disable_cdf_update` -- a frame that sets
408412 // it codes every symbol against the tables it started with, so this
@@ -410,7 +414,7 @@ impl<'a> SymbolDecoder<'a> {
410414 // allow_update_cdf && !cm->features.disable_cdf_update`,
411415 // decodeframe.c:2909). The flag is per-reader (see `Self::adapt`).
412416 if self.adapt {
413- update_cdf(cdf, s);
417+ update_cdf(cdf, s);
414418 }
415419 if crate::census::armed() {
416420 crate::census::charge(cdf.as_ptr() as usize, self.tell_bits() - t0);
@@ -443,13 +447,16 @@ impl<'a> SymbolDecoder<'a> {
443447 #[inline]
444448 fn symbol_fixed_raw(&mut self, cdf: &[u16]) -> usize {
445449 let nsyms = cdf.len() - 1;
450+ if symtrace::ecdump_armed() {
451+ symtrace::ecdump(0, self.value, self.range, self.bit as i64);
452+ }
446453 // Two-symbol alphabets are most of the stream (every flag, every bit of
447454 // a literal), and their walk has exactly one boundary: computing it
448455 // straight skips the loop and its bounds work.
449456 if nsyms == 2 {
450457 let f = u32::from(CDF_TOP - cdf[0]);
451- let cur = (((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT))
452- + EC_MIN_PROB;
458+ let cur =
459+ (((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) + EC_MIN_PROB;
453460 return self.bool_cur(cur);
454461 }
455462 // Loop-invariant: the coder's range does not move until the symbol is
@@ -829,13 +836,21 @@ pub(crate) mod tests {
829836 (a, b as u32)
830837 };
831838 assert_eq!(a, b, "item {i} (pick {pick})");
832- assert_eq!(a, if pick == usize::MAX { a } else { arg }, "item {i} vs written");
839+ assert_eq!(
840+ a,
841+ if pick == usize::MAX { a } else { arg },
842+ "item {i} vs written"
843+ );
833844 assert_eq!(
834845 fast.debug_state(),
835846 slow.debug_state(),
836847 "coder state after item {i} (pick {pick})"
837848 );
838- assert_eq!(fast_cdfs[pick.min(14)], slow_cdfs[pick.min(14)], "cdf after item {i}");
849+ assert_eq!(
850+ fast_cdfs[pick.min(14)],
851+ slow_cdfs[pick.min(14)],
852+ "cdf after item {i}"
853+ );
839854 }
840855 }
841856
@@ -898,6 +913,7 @@ pub(crate) mod tests {
898913 /// Off (the variable unset) this is one relaxed load per symbol.
899914 pub(crate) mod symtrace {
900915 use std::cell::RefCell;
916+ use std::collections::VecDeque;
901917 use std::sync::OnceLock;
902918
903919 static DIR: OnceLock<Option<String>> = OnceLock::new();
@@ -906,11 +922,76 @@ pub(crate) mod symtrace {
906922
907923 thread_local! {
908924 static BUF: RefCell<(String, usize)> = const { RefCell::new((String::new(), 0)) };
925+ static ECD: RefCell<VecDeque<(u32, u32, u32, u32)>> = RefCell::new(VecDeque::new());
926+ static ECD_READER: RefCell<Option<std::fs::File>> = const { RefCell::new(None) };
927+ static ECD_IDX: RefCell<usize> = const { RefCell::new(0) };
928+ }
929+
930+ /// lane-av1mix: `(low, value, rng, bitpos)` state register dump, one line
931+ /// per symbol read, byte-for-byte diffable against libaom's `EC_ECDUMP`
932+ /// (`decodetxb.c`, `r->ec.dif` masked to 16 bits / `r->ec.rng` /
933+ /// `od_ec_dec_tell`). Reader-only, `EC_AV1_ECDUMP=<dir>`: each reader
934+ /// writes `<dir>/ecdump-<n>.txt`.
935+ pub(crate) fn ecdump(low: u32, value: u32, rng: u32, bitpos: i64) {
936+ if !ecdump_armed() {
937+ return;
938+ }
939+ let dir = std::env::var("EC_AV1_ECDUMP").unwrap_or_else(|_| "/tmp".to_string());
940+ let idx = ECD_IDX.with(|c| {
941+ let mut g = c.borrow_mut();
942+ let n = *g;
943+ *g += 1;
944+ n
945+ });
946+ let _ = idx;
947+ let line = format!("{idx} ({low},{value},{rng},{bitpos})\n");
948+ ECD_READER.with(|f| {
949+ let mut slot = f.borrow_mut();
950+ if slot.is_none() {
951+ let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
952+ let path = format!("{dir}/ecdump-{seq:05}.txt");
953+ *slot = Some(std::fs::File::create(&path).expect("ecdump file"));
954+ eprintln!("ECDUMP -> {path}");
955+ }
956+ use std::io::Write;
957+ let _ = slot.as_mut().unwrap().write_all(line.as_bytes());
958+ });
959+ ECD.with(|q| {
960+ let mut q = q.borrow_mut();
961+ q.push_back((low, value, rng, bitpos as u32));
962+ if q.len() > 512 {
963+ q.pop_front();
964+ }
965+ });
966+ }
967+
968+ /// lane-av1mix: the last `n` dumped register states (most recent last).
969+ #[allow(dead_code)] // diagnostic helper for lane sessions
970+ pub(crate) fn ecdump_recent(n: usize) -> Vec<(u32, u32, u32, u32)> {
971+ if std::env::var_os("EC_AV1_ECDUMP").is_none() {
972+ return Vec::new();
973+ }
974+ ECD.with(|q| {
975+ let q = q.borrow();
976+ let skip = q.len().saturating_sub(n);
977+ q.iter().skip(skip).copied().collect()
978+ })
909979 }
910980
911981 pub(crate) fn dir() -> Option<&'static str> {
912- DIR.get_or_init(|| crate::envflags::var("EC_AV1_SYMTRACE").ok().filter(|v| !v.is_empty()))
913- .as_deref()
982+ DIR.get_or_init(|| {
983+ crate::envflags::var("EC_AV1_SYMTRACE")
984+ .ok()
985+ .filter(|v| !v.is_empty())
986+ })
987+ .as_deref()
988+ }
989+
990+ /// lane-av1mix: `EC_AV1_ECDUMP=<dir>` arms the decoder state-register dump.
991+ pub(crate) fn ecdump_armed() -> bool {
992+ static ARMED: std::sync::LazyLock<bool> =
993+ std::sync::LazyLock::new(|| std::env::var_os("EC_AV1_ECDUMP").is_some());
994+ *ARMED
914995 }
915996
916997 /// `EC_AV1_SYMTRACE_AT=<i>` or `=<from>:<to>` (inclusive).
@@ -940,7 +1021,10 @@ pub(crate) mod symtrace {
9401021 BUF.with(|b| {
9411022 let (buf, n) = &mut *b.borrow_mut();
9421023 if at().is_some_and(|(a, b)| (a..=b).contains(n)) {
943- eprintln!("SYMTRACE at {n}: {}", std::backtrace::Backtrace::force_capture());
1024+ eprintln!(
1025+ "SYMTRACE at {n}: {}",
1026+ std::backtrace::Backtrace::force_capture()
1027+ );
9441028 }
9451029 buf.push_str(&format!("{n} {value} {nsyms} {cdf0}\n"));
9461030 *n += 1;