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 commita2a84c73ed4b28f78d38fdfdc47e1a2511faa4ddParent: 322c5152 changed files +123 -13
- crates/ec-av1/src/decode.rs +30 -4
- crates/ec-av1/src/msac.rs +93 -9
crates/ec-av1/src/decode.rsmodified+30 -4
| @@ -6625,6 +6625,13 @@ fn read_coeffs( | |||
| 6625 | let (range, value) = dec.debug_state(); | 6625 | let (range, value) = dec.debug_state(); |
| 6626 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); | 6626 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); |
| 6627 | } | 6627 | } |
| 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 | + } | ||
| 6628 | let dbg_txbskip = if coeff_trace_on() { | 6635 | let dbg_txbskip = if coeff_trace_on() { |
| 6629 | coding.txb_skip[skip_ctx] | 6636 | coding.txb_skip[skip_ctx] |
| 6630 | } else { | 6637 | } else { |
| @@ -8340,9 +8347,18 @@ impl Neighbours { | |||
| 8340 | (0..w_mi).map(|k| self.above[mi_c + k][0].level.to_string()).collect(); | 8347 | (0..w_mi).map(|k| self.above[mi_c + k][0].level.to_string()).collect(); |
| 8341 | let lf: Vec<String> = | 8348 | let lf: Vec<String> = |
| 8342 | (0..h_mi).map(|k| self.left[mi_r + k][0].level.to_string()).collect(); | 8349 | (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(" "); | ||
| 8343 | eprintln!( | 8359 | eprintln!( |
| 8344 | - "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},]", | 8360 | + "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},] recentEC=[{}]", |
| 8345 | - ab.join(","), lf.join(",") | 8361 | + ab.join(","), lf.join(","), recent |
| 8346 | ); | 8362 | ); |
| 8347 | } | 8363 | } |
| 8348 | if crate::envflags::env_flag!("EC_AV1_TRACE") { | 8364 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| @@ -18240,7 +18256,13 @@ fn decode_leaf_rect8( | |||
| 18240 | // Missing this read consumed one symbol fewer than the encoder wrote | 18256 | // Missing this read consumed one symbol fewer than the encoder wrote |
| 18241 | // and desynced the tile at the very first rect leaf (class | 18257 | // and desynced the tile at the very first rect leaf (class |
| 18242 | // `symbol-consumption-gap`). | 18258 | // `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) { | ||
| 18244 | // lane-av1txbands: the `TXFM_CONTEXT` band read every other rect | 18266 | // lane-av1txbands: the `TXFM_CONTEXT` band read every other rect |
| 18245 | // reader already does. | 18267 | // reader already does. |
| 18246 | let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); | 18268 | let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); |
| @@ -33524,7 +33546,11 @@ fn decode_intra_sub8_leaf( | |||
| 33524 | // context bands, which on an inter frame are live. | 33546 | // context bands, which on an inter frame are live. |
| 33525 | // `block_signals_txsize(BLOCK_4X4)` is false (blockd.h:1027): the 4x4 leaf | 33547 | // `block_signals_txsize(BLOCK_4X4)` is false (blockd.h:1027): the 4x4 leaf |
| 33526 | // codes no depth symbol at all, its transform is its own size. | 33548 | // 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) { | ||
| 33528 | let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); | 33554 | let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); |
| 33529 | let d = dec.symbol(&mut cdfs.tx_size_cat0[ctx]); | 33555 | let d = dec.symbol(&mut cdfs.tx_size_cat0[ctx]); |
| 33530 | if step { | 33556 | if step { |
crates/ec-av1/src/msac.rsmodified+93 -9
| @@ -402,7 +402,11 @@ impl<'a> SymbolDecoder<'a> { | |||
| 402 | // TABLE it read, so every coding tool is accounted without a | 402 | // TABLE it read, so every coding tool is accounted without a |
| 403 | // per-call-site label. Off (`EC_AV1_BITCENSUS` unset) this is one | 403 | // per-call-site label. Off (`EC_AV1_BITCENSUS` unset) this is one |
| 404 | // relaxed load. | 404 | // 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 | + }; | ||
| 406 | let s = self.symbol_fixed(cdf); | 410 | let s = self.symbol_fixed(cdf); |
| 407 | // lane-av1txr: spec 5.9.2's `disable_cdf_update` -- a frame that sets | 411 | // lane-av1txr: spec 5.9.2's `disable_cdf_update` -- a frame that sets |
| 408 | // it codes every symbol against the tables it started with, so this | 412 | // it codes every symbol against the tables it started with, so this |
| @@ -410,7 +414,7 @@ impl<'a> SymbolDecoder<'a> { | |||
| 410 | // allow_update_cdf && !cm->features.disable_cdf_update`, | 414 | // allow_update_cdf && !cm->features.disable_cdf_update`, |
| 411 | // decodeframe.c:2909). The flag is per-reader (see `Self::adapt`). | 415 | // decodeframe.c:2909). The flag is per-reader (see `Self::adapt`). |
| 412 | if self.adapt { | 416 | if self.adapt { |
| 413 | - update_cdf(cdf, s); | 417 | + update_cdf(cdf, s); |
| 414 | } | 418 | } |
| 415 | if crate::census::armed() { | 419 | if crate::census::armed() { |
| 416 | crate::census::charge(cdf.as_ptr() as usize, self.tell_bits() - t0); | 420 | crate::census::charge(cdf.as_ptr() as usize, self.tell_bits() - t0); |
| @@ -443,13 +447,16 @@ impl<'a> SymbolDecoder<'a> { | |||
| 443 | #[inline] | 447 | #[inline] |
| 444 | fn symbol_fixed_raw(&mut self, cdf: &[u16]) -> usize { | 448 | fn symbol_fixed_raw(&mut self, cdf: &[u16]) -> usize { |
| 445 | let nsyms = cdf.len() - 1; | 449 | let nsyms = cdf.len() - 1; |
| 450 | + if symtrace::ecdump_armed() { | ||
| 451 | + symtrace::ecdump(0, self.value, self.range, self.bit as i64); | ||
| 452 | + } | ||
| 446 | // Two-symbol alphabets are most of the stream (every flag, every bit of | 453 | // Two-symbol alphabets are most of the stream (every flag, every bit of |
| 447 | // a literal), and their walk has exactly one boundary: computing it | 454 | // a literal), and their walk has exactly one boundary: computing it |
| 448 | // straight skips the loop and its bounds work. | 455 | // straight skips the loop and its bounds work. |
| 449 | if nsyms == 2 { | 456 | if nsyms == 2 { |
| 450 | let f = u32::from(CDF_TOP - cdf[0]); | 457 | let f = u32::from(CDF_TOP - cdf[0]); |
| 451 | - let cur = (((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) | 458 | + let cur = |
| 452 | - + EC_MIN_PROB; | 459 | + (((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT)) + EC_MIN_PROB; |
| 453 | return self.bool_cur(cur); | 460 | return self.bool_cur(cur); |
| 454 | } | 461 | } |
| 455 | // Loop-invariant: the coder's range does not move until the symbol is | 462 | // Loop-invariant: the coder's range does not move until the symbol is |
| @@ -829,13 +836,21 @@ pub(crate) mod tests { | |||
| 829 | (a, b as u32) | 836 | (a, b as u32) |
| 830 | }; | 837 | }; |
| 831 | assert_eq!(a, b, "item {i} (pick {pick})"); | 838 | 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 | + ); | ||
| 833 | assert_eq!( | 844 | assert_eq!( |
| 834 | fast.debug_state(), | 845 | fast.debug_state(), |
| 835 | slow.debug_state(), | 846 | slow.debug_state(), |
| 836 | "coder state after item {i} (pick {pick})" | 847 | "coder state after item {i} (pick {pick})" |
| 837 | ); | 848 | ); |
| 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 | + ); | ||
| 839 | } | 854 | } |
| 840 | } | 855 | } |
| 841 | 856 | ||
| @@ -898,6 +913,7 @@ pub(crate) mod tests { | |||
| 898 | /// Off (the variable unset) this is one relaxed load per symbol. | 913 | /// Off (the variable unset) this is one relaxed load per symbol. |
| 899 | pub(crate) mod symtrace { | 914 | pub(crate) mod symtrace { |
| 900 | use std::cell::RefCell; | 915 | use std::cell::RefCell; |
| 916 | + use std::collections::VecDeque; | ||
| 901 | use std::sync::OnceLock; | 917 | use std::sync::OnceLock; |
| 902 | 918 | ||
| 903 | static DIR: OnceLock<Option<String>> = OnceLock::new(); | 919 | static DIR: OnceLock<Option<String>> = OnceLock::new(); |
| @@ -906,11 +922,76 @@ pub(crate) mod symtrace { | |||
| 906 | 922 | ||
| 907 | thread_local! { | 923 | thread_local! { |
| 908 | static BUF: RefCell<(String, usize)> = const { RefCell::new((String::new(), 0)) }; | 924 | 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 | + }) | ||
| 909 | } | 979 | } |
| 910 | 980 | ||
| 911 | pub(crate) fn dir() -> Option<&'static str> { | 981 | pub(crate) fn dir() -> Option<&'static str> { |
| 912 | - DIR.get_or_init(|| crate::envflags::var("EC_AV1_SYMTRACE").ok().filter(|v| !v.is_empty())) | 982 | + DIR.get_or_init(|| { |
| 913 | - .as_deref() | 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 | ||
| 914 | } | 995 | } |
| 915 | 996 | ||
| 916 | /// `EC_AV1_SYMTRACE_AT=<i>` or `=<from>:<to>` (inclusive). | 997 | /// `EC_AV1_SYMTRACE_AT=<i>` or `=<from>:<to>` (inclusive). |
| @@ -940,7 +1021,10 @@ pub(crate) mod symtrace { | |||
| 940 | BUF.with(|b| { | 1021 | BUF.with(|b| { |
| 941 | let (buf, n) = &mut *b.borrow_mut(); | 1022 | let (buf, n) = &mut *b.borrow_mut(); |
| 942 | if at().is_some_and(|(a, b)| (a..=b).contains(n)) { | 1023 | 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 | + ); | ||
| 944 | } | 1028 | } |
| 945 | buf.push_str(&format!("{n} {value} {nsyms} {cdf0}\n")); | 1029 | buf.push_str(&format!("{n} {value} {nsyms} {cdf0}\n")); |
| 946 | *n += 1; | 1030 | *n += 1; |