diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index dc004ccf..a016066f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -63,6 +63,46 @@ const BLOCK_MI: u32 = 8; // *quadrant* size a 128 superblock is decoded in, which is also what the CDEF // grid is indexed by (libaom `read_cdef` reads one `cdef_idx` per 64x64 // inside a 128 superblock). +thread_local! { + /// lane-frame80: `EC_TRACE_COEFF_FRAME=` narrows the + /// `EC_TRACE_COEFF` rungs to a single frame in DECODE order (a 4K stream + /// prints gigabytes otherwise). Unset = every frame, as before. + static COEFF_TRACE_FRAME: std::cell::Cell> = const { std::cell::Cell::new(None) }; + static COEFF_TRACE_CUR: std::cell::Cell = const { std::cell::Cell::new(usize::MAX) }; +} + +/// Called by [`crate::stream::decode_stream`] before each frame's tiles with +/// that frame's DECODE-order index. +pub fn set_coeff_trace_frame(idx: usize) { + COEFF_TRACE_FRAME.with(|c| { + if c.get().is_none() { + let want = match std::env::var("EC_TRACE_COEFF_FRAME") { + Ok(v) => v.parse::().unwrap_or(usize::MAX), + // unset: keep tracing every frame, as before this rung existed + Err(_) => usize::MAX - 1, + }; + c.set(Some(want)); + } + }); + COEFF_TRACE_CUR.with(|c| c.set(idx)); + if std::env::var_os("EC_TRACE_COEFF").is_some() { + eprintln!("EC_COEFF_FRAME decode_idx={idx}"); + } +} + +pub(crate) fn coeff_trace_on() -> bool { + if std::env::var_os("EC_TRACE_COEFF").is_none() { + return false; + } + match COEFF_TRACE_FRAME.with(|c| c.get()) { + // no `set_coeff_trace_frame` caller (direct tile-decode tests), or the + // env var unset: trace every frame. + None => true, + Some(w) if w >= usize::MAX - 1 => true, + Some(w) => COEFF_TRACE_CUR.with(|c| c.get()) == w, + } +} + thread_local! { static SB128: std::cell::Cell = const { std::cell::Cell::new(false) }; } @@ -4122,13 +4162,13 @@ fn read_coeffs( let (range, value) = dec.debug_state(); eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); } - let dbg_txbskip = if std::env::var_os("EC_TRACE_COEFF").is_some() { + let dbg_txbskip = if coeff_trace_on() { coding.txb_skip[skip_ctx] } else { [0; 3] }; let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; - if std::env::var_os("EC_TRACE_COEFF").is_some() { + if coeff_trace_on() { let (rng, _) = dec.debug_state(); eprintln!( "EC_COEFF_STEP tag=all_zero side={} ctx={skip_ctx} cdf={dbg_txbskip:?} all_zero={} rng={rng}", @@ -4167,7 +4207,7 @@ fn read_coeffs( eprintln!("EC_AV1_TXTYPE32_CDF {tx_type_cdf:?}"); } let t = dec.symbol(tx_type_cdf); - if std::env::var_os("EC_TRACE_COEFF").is_some() { + if coeff_trace_on() { let (rng, _) = dec.debug_state(); eprintln!("EC_COEFF_STEP tag=tx_type len={len} rng={rng}"); } @@ -4196,7 +4236,7 @@ fn read_coeffs( if trace { eprintln!("TRACE eob value={eob}"); } - let ec_trace_coeff = std::env::var_os("EC_TRACE_COEFF").is_some(); + let ec_trace_coeff = coeff_trace_on(); if ec_trace_coeff { let (rng, _) = dec.debug_state(); let cls = match class { TxClass::TwoD => "2d", TxClass::Horiz => "horiz", TxClass::Vert => "vert" }; @@ -4338,7 +4378,7 @@ fn read_coeffs_rect( sign_ctx: usize, default_tx_type: TxType, ) -> Result<(Vec, TxType)> { - let rect_trace = std::env::var_os("EC_TRACE_COEFF").is_some(); + let rect_trace = coeff_trace_on(); let mut grid = vec![0i32; w * h]; let entry_rng = dec.debug_state().0; let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; @@ -10067,7 +10107,7 @@ fn decode_block_rect64( // 32x16/16x32 under a 64x16/16x64 1:4 strip (lane-tx64x16). let (luma_cw, luma_ch) = (bw.min(32), bh.min(32)); let scan32 = default_scan(TX32); - if std::env::var_os("EC_TRACE_COEFF").is_some() { + if coeff_trace_on() { let (rng, _) = dec.debug_state(); eprintln!("EC_COEFF plane=0 row={mi_r} col={mi_c} tx_size={luma_cw}x{luma_ch} rng={rng}"); } @@ -10109,7 +10149,7 @@ fn decode_block_rect64( TxType::DctDct, )? }; - if std::env::var_os("EC_TRACE_COEFF").is_some() { + if coeff_trace_on() { let (rng, _) = dec.debug_state(); eprintln!("EC_COEFF_VAL plane=0 row={mi_r} col={mi_c} rng={rng}"); } @@ -10183,7 +10223,7 @@ fn decode_block_rect64( }; let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); let mut u_coding = cdfs.txb(chroma_set, uv_predict_mode); - if std::env::var_os("EC_TRACE_COEFF").is_some() { + if coeff_trace_on() { let (rng, _) = dec.debug_state(); eprintln!("EC_COEFF plane=1 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); } @@ -10197,7 +10237,7 @@ fn decode_block_rect64( dc_sign_ctx(around[1].2), TxType::DctDct, )?; - if std::env::var_os("EC_TRACE_COEFF").is_some() { + if coeff_trace_on() { let (rng, _) = dec.debug_state(); eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); } @@ -10237,7 +10277,7 @@ fn decode_block_rect64( ); let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); let mut v_coding = cdfs.txb(chroma_set, uv_predict_mode); - if std::env::var_os("EC_TRACE_COEFF").is_some() { + if coeff_trace_on() { let (rng, _) = dec.debug_state(); eprintln!("EC_COEFF plane=2 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); } @@ -10251,7 +10291,7 @@ fn decode_block_rect64( dc_sign_ctx(around[2].2), TxType::DctDct, )?; - if std::env::var_os("EC_TRACE_COEFF").is_some() { + if coeff_trace_on() { let (rng, _) = dec.debug_state(); eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); } @@ -16131,6 +16171,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // function is ever called. delta: DeltaParams, ) -> Result<(Picture, Cdfs)> { + // lane-frame80: [`TX_SELECT_INTER`] is set by the INTER tile decoder + // only, so a key frame decoded after an inter frame on this thread would + // otherwise inherit it and read its `tx_depth` context off the inter + // frame's stale `TXFM_CONTEXT` bands. + TX_SELECT_INTER.with(|c| c.set(false)); if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 42e062c8..cd9fe14b 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -688,6 +688,8 @@ pub fn decode_stream(data: &[u8]) -> Result> { })?) }; let started_from = initial_cdfs.clone(); + // lane-frame80: narrow the EC_TRACE_COEFF rungs to one DECODE-order frame. + decode::set_coeff_trace_frame(pictures_decoded); let order_hint_bits = parser .sequence_header() diff --git a/lanes/frame80-r1.handoff.md b/lanes/frame80-r1.handoff.md new file mode 100644 index 00000000..fd953a61 --- /dev/null +++ b/lanes/frame80-r1.handoff.md @@ -0,0 +1,65 @@ +# lane-frame80 r1 HANDOFF — the rect intra `tx_depth` context used the key frame's grid inside inter frames + +## Stream / repro +- Cut: `~/.cache/frame80-tmp/t120.obu` = `~/.cache/hg-0-10s.obu` (10-bit 3840x1608 10 s head) + truncated to the first **120 frame-carrying OBUs** with + `python3 /census4/trunc.py 120 t120.obu` (445416 B, + sha256 `045d81a9691540220292663cecbc9a2eeb38db6624a714d3d0150544590a9128`, hashed twice, equal). + 120 OBUs = DECODE-order frames 0..80 (the OBU count is NOT the decode index: `show_existing_frame` + headers count as frame OBUs; n=81 only reaches decode frame ~57). +- Every probe on this stream needs `EC_INTRA16X4_DECODE=1 EC_INTRA128_IN_INTER=1`; without them the + full 10 s cut stops at decode frame 62 on the 16x4-in-inter refusal (main 243f125 re-added it), + which is NOT the golomb wall. + +## New instrument (committed) +- `decode.rs` `set_coeff_trace_frame` / `coeff_trace_on`: `EC_TRACE_COEFF_FRAME=` narrows + every `EC_TRACE_COEFF` rung to one DECODE-order frame; unset = every frame (old behaviour). + Also prints `EC_COEFF_FRAME decode_idx=N` per frame under `EC_TRACE_COEFF`, which is what makes the + our-side trace splittable. Wired from `stream.rs` (`decode::set_coeff_trace_frame(pictures_decoded)`). +- Cross-decoder coefficient ladder recipe (`~/.cache/frame80-tmp/lad3.py`): aomdec's and our tag order + differ inside a TU (ours prints `br` before `base_eob`, aomdec after; ours splits `sign`/`sign_rect`; + aomdec prints `tag=tx_type` unconditionally for luma even when no symbol is read). Ladder ONLY on + tags `{all_zero, eob, after_bases}` + rng. On that key our frames 1..62 align element-exactly inside + aomdec's whole-stream trace by substring search of the frame's first 30 elements. + +## First diverging element (this is NOT in frame 80 — the charter premise was stale) +DECODE frame **62** (not 80), frame-local ladder element 3981: +- Block **mi_row=108 mi_col=528**, an INTRA block inside an inter frame, luma 32x8 (BLOCK_32X8), + luma TU TX_32X8 (aomdec `tx_size=16`), chroma TX_16X4; its above neighbour mi(106,528) is an INTER + 32x8 block whose var-tx tree split (txfm ctx 16 px wide). +- Symbol: the block's `tx_depth` (`read_selected_tx_size`, cat=2). + aomdec `EC_ISTEP mi_row=108 mi_col=528 name=tx_depth val=0 ctx=2 cat=2 rng=60142`; + ours computed **ctx=1** (`EC_TXCTX ... w=32 h=8 above_px=16 left_px=64 ctx=1`) and left the range at + 43516 where aomdec has 60142; the next luma `all_zero` then reads ctx=5/rng=41484 vs aomdec ctx=0/rng=60138. +- Entry ranges agree (both 49400 at the end of the previous block's chroma TU), so the divergence is + exactly this one CDF-row choice. + +## Root cause (fixed, committed) +`get_tx_size_context` (libaom `av1/common/pred_common.h:342`) overrides the `TXFM_CONTEXT` bands when a +neighbour is an INTER block: `above = block_size_wide[above_mbmi->bsize] >= max_tx_wide` +(mirror for left/height). Our `tx_size_context_rect` (the KEY frame's deblock-grid approximation) has no +such override and was being used for rect intra strips inside INTER frames; `tx_size_context_txfm_rect` +(which has the override) was reachable only from `decode_intra_rect_in_inter`. +Fix, at the two single points every caller routes through (`crates/ec-av1/src/decode.rs`): +- `tx_size_context_rect` → delegates to `tx_size_context_txfm_rect` when `TX_SELECT_INTER` is set + (covers `decode_block_rect`, `decode_leaf_rect`, `decode_block_rect4`, `decode_rect4_16_strip`). +- `tx_size_context` (square, used by `read_tx_size(.., None)`) → delegates to `tx_size_context_txfm`. +- `decode_key_frame_tile_with_cdfs` now clears `TX_SELECT_INTER` (it was set by the inter tile decoder + only, so a key frame after an inter frame on the same thread inherited it). + +## State after the fix (measured) +`EC_INTRA16X4_DECODE=1 EC_INTRA128_IN_INTER=1 EC_FRAMES=1 EC_GOLOMB=1 decode_probe ~/.cache/hg-0-10s.obu` +→ decode-order frames 0..**84** decode (was 0..79); the wall moves to decode frame 85, still the +"a Golomb tail longer than this decoder reads" refusal (`EC_GOLOMB_LONG w=32 h=32 pos=353 base=15 length=21`), +i.e. still our own desync, now 5 frames later. + +## Exact next step +1. Re-cut for decode frame 85 (bump the OBU count until `EC_FRAME_OK` shows 85 frames, ~n=127) and + re-run the ladder: `python3 ~/.cache/frame80-tmp/lad3.py ` after locating the + frame's start with the first-30-elements substring search (see lad.py/lad2.py in the same dir). + Sweep frames 63..85 the same way — several of them failed head-alignment before the fix; re-check + which still do. +2. NOT DONE (owner: next round): pixel compare of the fixed frames vs ffmpeg with + `/census7/streamcmp.py`; frame 57's two tx_depth divergences; the witness fixture + `crates/ec-av1/fixtures/reelb_head_frame80_witness.obu` + its gate; `cargo test -p ec-av1 --lib` suite. + The fix is entropy-verified on the ladder and by the 5-frame wall move only.