EC_TRACE_COEFF_FRAME rung, and the key-frame tile decoder clears TX_SELECT_INTER
Browse files at this commit42ede61c870fddf63368c55b4792289827d2a96bParent: f5530c9- feat
- EC_TRACE_COEFF_FRAME rung, and the key-frame tile decoder clears TX_SELECT_INTER
3 changed files +123 -11
- crates/ec-av1/src/decode.rs +56 -11
- crates/ec-av1/src/stream.rs +2 -0
- lanes/frame80-r1.handoff.md +65 -0
crates/ec-av1/src/decode.rsmodified+56 -11
| @@ -63,6 +63,46 @@ const BLOCK_MI: u32 = 8; | ||
| 63 | 63 | // *quadrant* size a 128 superblock is decoded in, which is also what the CDEF |
| 64 | 64 | // grid is indexed by (libaom `read_cdef` reads one `cdef_idx` per 64x64 |
| 65 | 65 | // inside a 128 superblock). |
| 66 | +thread_local! { | |
| 67 | + /// lane-frame80: `EC_TRACE_COEFF_FRAME=<decode idx>` narrows the | |
| 68 | + /// `EC_TRACE_COEFF` rungs to a single frame in DECODE order (a 4K stream | |
| 69 | + /// prints gigabytes otherwise). Unset = every frame, as before. | |
| 70 | + static COEFF_TRACE_FRAME: std::cell::Cell<Option<usize>> = const { std::cell::Cell::new(None) }; | |
| 71 | + static COEFF_TRACE_CUR: std::cell::Cell<usize> = const { std::cell::Cell::new(usize::MAX) }; | |
| 72 | +} | |
| 73 | + | |
| 74 | +/// Called by [`crate::stream::decode_stream`] before each frame's tiles with | |
| 75 | +/// that frame's DECODE-order index. | |
| 76 | +pub fn set_coeff_trace_frame(idx: usize) { | |
| 77 | + COEFF_TRACE_FRAME.with(|c| { | |
| 78 | + if c.get().is_none() { | |
| 79 | + let want = match std::env::var("EC_TRACE_COEFF_FRAME") { | |
| 80 | + Ok(v) => v.parse::<usize>().unwrap_or(usize::MAX), | |
| 81 | + // unset: keep tracing every frame, as before this rung existed | |
| 82 | + Err(_) => usize::MAX - 1, | |
| 83 | + }; | |
| 84 | + c.set(Some(want)); | |
| 85 | + } | |
| 86 | + }); | |
| 87 | + COEFF_TRACE_CUR.with(|c| c.set(idx)); | |
| 88 | + if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 89 | + eprintln!("EC_COEFF_FRAME decode_idx={idx}"); | |
| 90 | + } | |
| 91 | +} | |
| 92 | + | |
| 93 | +pub(crate) fn coeff_trace_on() -> bool { | |
| 94 | + if std::env::var_os("EC_TRACE_COEFF").is_none() { | |
| 95 | + return false; | |
| 96 | + } | |
| 97 | + match COEFF_TRACE_FRAME.with(|c| c.get()) { | |
| 98 | + // no `set_coeff_trace_frame` caller (direct tile-decode tests), or the | |
| 99 | + // env var unset: trace every frame. | |
| 100 | + None => true, | |
| 101 | + Some(w) if w >= usize::MAX - 1 => true, | |
| 102 | + Some(w) => COEFF_TRACE_CUR.with(|c| c.get()) == w, | |
| 103 | + } | |
| 104 | +} | |
| 105 | + | |
| 66 | 106 | thread_local! { |
| 67 | 107 | static SB128: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 68 | 108 | } |
| @@ -4122,13 +4162,13 @@ fn read_coeffs( | ||
| 4122 | 4162 | let (range, value) = dec.debug_state(); |
| 4123 | 4163 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); |
| 4124 | 4164 | } |
| 4125 | - let dbg_txbskip = if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 4165 | + let dbg_txbskip = if coeff_trace_on() { | |
| 4126 | 4166 | coding.txb_skip[skip_ctx] |
| 4127 | 4167 | } else { |
| 4128 | 4168 | [0; 3] |
| 4129 | 4169 | }; |
| 4130 | 4170 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 4131 | - if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 4171 | + if coeff_trace_on() { | |
| 4132 | 4172 | let (rng, _) = dec.debug_state(); |
| 4133 | 4173 | eprintln!( |
| 4134 | 4174 | "EC_COEFF_STEP tag=all_zero side={} ctx={skip_ctx} cdf={dbg_txbskip:?} all_zero={} rng={rng}", |
| @@ -4167,7 +4207,7 @@ fn read_coeffs( | ||
| 4167 | 4207 | eprintln!("EC_AV1_TXTYPE32_CDF {tx_type_cdf:?}"); |
| 4168 | 4208 | } |
| 4169 | 4209 | let t = dec.symbol(tx_type_cdf); |
| 4170 | - if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 4210 | + if coeff_trace_on() { | |
| 4171 | 4211 | let (rng, _) = dec.debug_state(); |
| 4172 | 4212 | eprintln!("EC_COEFF_STEP tag=tx_type len={len} rng={rng}"); |
| 4173 | 4213 | } |
| @@ -4196,7 +4236,7 @@ fn read_coeffs( | ||
| 4196 | 4236 | if trace { |
| 4197 | 4237 | eprintln!("TRACE eob value={eob}"); |
| 4198 | 4238 | } |
| 4199 | - let ec_trace_coeff = std::env::var_os("EC_TRACE_COEFF").is_some(); | |
| 4239 | + let ec_trace_coeff = coeff_trace_on(); | |
| 4200 | 4240 | if ec_trace_coeff { |
| 4201 | 4241 | let (rng, _) = dec.debug_state(); |
| 4202 | 4242 | let cls = match class { TxClass::TwoD => "2d", TxClass::Horiz => "horiz", TxClass::Vert => "vert" }; |
| @@ -4338,7 +4378,7 @@ fn read_coeffs_rect( | ||
| 4338 | 4378 | sign_ctx: usize, |
| 4339 | 4379 | default_tx_type: TxType, |
| 4340 | 4380 | ) -> Result<(Vec<i32>, TxType)> { |
| 4341 | - let rect_trace = std::env::var_os("EC_TRACE_COEFF").is_some(); | |
| 4381 | + let rect_trace = coeff_trace_on(); | |
| 4342 | 4382 | let mut grid = vec![0i32; w * h]; |
| 4343 | 4383 | let entry_rng = dec.debug_state().0; |
| 4344 | 4384 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| @@ -10067,7 +10107,7 @@ fn decode_block_rect64( | ||
| 10067 | 10107 | // 32x16/16x32 under a 64x16/16x64 1:4 strip (lane-tx64x16). |
| 10068 | 10108 | let (luma_cw, luma_ch) = (bw.min(32), bh.min(32)); |
| 10069 | 10109 | let scan32 = default_scan(TX32); |
| 10070 | - if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 10110 | + if coeff_trace_on() { | |
| 10071 | 10111 | let (rng, _) = dec.debug_state(); |
| 10072 | 10112 | eprintln!("EC_COEFF plane=0 row={mi_r} col={mi_c} tx_size={luma_cw}x{luma_ch} rng={rng}"); |
| 10073 | 10113 | } |
| @@ -10109,7 +10149,7 @@ fn decode_block_rect64( | ||
| 10109 | 10149 | TxType::DctDct, |
| 10110 | 10150 | )? |
| 10111 | 10151 | }; |
| 10112 | - if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 10152 | + if coeff_trace_on() { | |
| 10113 | 10153 | let (rng, _) = dec.debug_state(); |
| 10114 | 10154 | eprintln!("EC_COEFF_VAL plane=0 row={mi_r} col={mi_c} rng={rng}"); |
| 10115 | 10155 | } |
| @@ -10183,7 +10223,7 @@ fn decode_block_rect64( | ||
| 10183 | 10223 | }; |
| 10184 | 10224 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 10185 | 10225 | let mut u_coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 10186 | - if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 10226 | + if coeff_trace_on() { | |
| 10187 | 10227 | let (rng, _) = dec.debug_state(); |
| 10188 | 10228 | eprintln!("EC_COEFF plane=1 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); |
| 10189 | 10229 | } |
| @@ -10197,7 +10237,7 @@ fn decode_block_rect64( | ||
| 10197 | 10237 | dc_sign_ctx(around[1].2), |
| 10198 | 10238 | TxType::DctDct, |
| 10199 | 10239 | )?; |
| 10200 | - if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 10240 | + if coeff_trace_on() { | |
| 10201 | 10241 | let (rng, _) = dec.debug_state(); |
| 10202 | 10242 | eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); |
| 10203 | 10243 | } |
| @@ -10237,7 +10277,7 @@ fn decode_block_rect64( | ||
| 10237 | 10277 | ); |
| 10238 | 10278 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 10239 | 10279 | let mut v_coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 10240 | - if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 10280 | + if coeff_trace_on() { | |
| 10241 | 10281 | let (rng, _) = dec.debug_state(); |
| 10242 | 10282 | eprintln!("EC_COEFF plane=2 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); |
| 10243 | 10283 | } |
| @@ -10251,7 +10291,7 @@ fn decode_block_rect64( | ||
| 10251 | 10291 | dc_sign_ctx(around[2].2), |
| 10252 | 10292 | TxType::DctDct, |
| 10253 | 10293 | )?; |
| 10254 | - if std::env::var_os("EC_TRACE_COEFF").is_some() { | |
| 10294 | + if coeff_trace_on() { | |
| 10255 | 10295 | let (rng, _) = dec.debug_state(); |
| 10256 | 10296 | eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); |
| 10257 | 10297 | } |
| @@ -16131,6 +16171,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 16131 | 16171 | // function is ever called. |
| 16132 | 16172 | delta: DeltaParams, |
| 16133 | 16173 | ) -> Result<(Picture, Cdfs)> { |
| 16174 | + // lane-frame80: [`TX_SELECT_INTER`] is set by the INTER tile decoder | |
| 16175 | + // only, so a key frame decoded after an inter frame on this thread would | |
| 16176 | + // otherwise inherit it and read its `tx_depth` context off the inter | |
| 16177 | + // frame's stale `TXFM_CONTEXT` bands. | |
| 16178 | + TX_SELECT_INTER.with(|c| c.set(false)); | |
| 16134 | 16179 | if mi_cols == 0 || mi_rows == 0 { |
| 16135 | 16180 | return Err(unsupported("a frame with no mode-info grid")); |
| 16136 | 16181 | } |
crates/ec-av1/src/stream.rsmodified+2 -0
| @@ -688,6 +688,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 688 | 688 | })?) |
| 689 | 689 | }; |
| 690 | 690 | let started_from = initial_cdfs.clone(); |
| 691 | + // lane-frame80: narrow the EC_TRACE_COEFF rungs to one DECODE-order frame. | |
| 692 | + decode::set_coeff_trace_frame(pictures_decoded); | |
| 691 | 693 | |
| 692 | 694 | let order_hint_bits = parser |
| 693 | 695 | .sequence_header() |
lanes/frame80-r1.handoff.mdadded+65 -0
| @@ -0,0 +1,65 @@ | ||
| 1 | +# lane-frame80 r1 HANDOFF — the rect intra `tx_depth` context used the key frame's grid inside inter frames | |
| 2 | + | |
| 3 | +## Stream / repro | |
| 4 | +- Cut: `~/.cache/frame80-tmp/t120.obu` = `~/.cache/hg-0-10s.obu` (10-bit 3840x1608 10 s head) | |
| 5 | + truncated to the first **120 frame-carrying OBUs** with | |
| 6 | + `python3 <scratchpad>/census4/trunc.py <in> 120 t120.obu` (445416 B, | |
| 7 | + sha256 `045d81a9691540220292663cecbc9a2eeb38db6624a714d3d0150544590a9128`, hashed twice, equal). | |
| 8 | + 120 OBUs = DECODE-order frames 0..80 (the OBU count is NOT the decode index: `show_existing_frame` | |
| 9 | + headers count as frame OBUs; n=81 only reaches decode frame ~57). | |
| 10 | +- Every probe on this stream needs `EC_INTRA16X4_DECODE=1 EC_INTRA128_IN_INTER=1`; without them the | |
| 11 | + full 10 s cut stops at decode frame 62 on the 16x4-in-inter refusal (main 243f125 re-added it), | |
| 12 | + which is NOT the golomb wall. | |
| 13 | + | |
| 14 | +## New instrument (committed) | |
| 15 | +- `decode.rs` `set_coeff_trace_frame` / `coeff_trace_on`: `EC_TRACE_COEFF_FRAME=<decode idx>` narrows | |
| 16 | + every `EC_TRACE_COEFF` rung to one DECODE-order frame; unset = every frame (old behaviour). | |
| 17 | + Also prints `EC_COEFF_FRAME decode_idx=N` per frame under `EC_TRACE_COEFF`, which is what makes the | |
| 18 | + our-side trace splittable. Wired from `stream.rs` (`decode::set_coeff_trace_frame(pictures_decoded)`). | |
| 19 | +- Cross-decoder coefficient ladder recipe (`~/.cache/frame80-tmp/lad3.py`): aomdec's and our tag order | |
| 20 | + differ inside a TU (ours prints `br` before `base_eob`, aomdec after; ours splits `sign`/`sign_rect`; | |
| 21 | + aomdec prints `tag=tx_type` unconditionally for luma even when no symbol is read). Ladder ONLY on | |
| 22 | + tags `{all_zero, eob, after_bases}` + rng. On that key our frames 1..62 align element-exactly inside | |
| 23 | + aomdec's whole-stream trace by substring search of the frame's first 30 elements. | |
| 24 | + | |
| 25 | +## First diverging element (this is NOT in frame 80 — the charter premise was stale) | |
| 26 | +DECODE frame **62** (not 80), frame-local ladder element 3981: | |
| 27 | +- Block **mi_row=108 mi_col=528**, an INTRA block inside an inter frame, luma 32x8 (BLOCK_32X8), | |
| 28 | + luma TU TX_32X8 (aomdec `tx_size=16`), chroma TX_16X4; its above neighbour mi(106,528) is an INTER | |
| 29 | + 32x8 block whose var-tx tree split (txfm ctx 16 px wide). | |
| 30 | +- Symbol: the block's `tx_depth` (`read_selected_tx_size`, cat=2). | |
| 31 | + aomdec `EC_ISTEP mi_row=108 mi_col=528 name=tx_depth val=0 ctx=2 cat=2 rng=60142`; | |
| 32 | + ours computed **ctx=1** (`EC_TXCTX ... w=32 h=8 above_px=16 left_px=64 ctx=1`) and left the range at | |
| 33 | + 43516 where aomdec has 60142; the next luma `all_zero` then reads ctx=5/rng=41484 vs aomdec ctx=0/rng=60138. | |
| 34 | +- Entry ranges agree (both 49400 at the end of the previous block's chroma TU), so the divergence is | |
| 35 | + exactly this one CDF-row choice. | |
| 36 | + | |
| 37 | +## Root cause (fixed, committed) | |
| 38 | +`get_tx_size_context` (libaom `av1/common/pred_common.h:342`) overrides the `TXFM_CONTEXT` bands when a | |
| 39 | +neighbour is an INTER block: `above = block_size_wide[above_mbmi->bsize] >= max_tx_wide` | |
| 40 | +(mirror for left/height). Our `tx_size_context_rect` (the KEY frame's deblock-grid approximation) has no | |
| 41 | +such override and was being used for rect intra strips inside INTER frames; `tx_size_context_txfm_rect` | |
| 42 | +(which has the override) was reachable only from `decode_intra_rect_in_inter`. | |
| 43 | +Fix, at the two single points every caller routes through (`crates/ec-av1/src/decode.rs`): | |
| 44 | +- `tx_size_context_rect` → delegates to `tx_size_context_txfm_rect` when `TX_SELECT_INTER` is set | |
| 45 | + (covers `decode_block_rect`, `decode_leaf_rect`, `decode_block_rect4`, `decode_rect4_16_strip`). | |
| 46 | +- `tx_size_context` (square, used by `read_tx_size(.., None)`) → delegates to `tx_size_context_txfm`. | |
| 47 | +- `decode_key_frame_tile_with_cdfs` now clears `TX_SELECT_INTER` (it was set by the inter tile decoder | |
| 48 | + only, so a key frame after an inter frame on the same thread inherited it). | |
| 49 | + | |
| 50 | +## State after the fix (measured) | |
| 51 | +`EC_INTRA16X4_DECODE=1 EC_INTRA128_IN_INTER=1 EC_FRAMES=1 EC_GOLOMB=1 decode_probe ~/.cache/hg-0-10s.obu` | |
| 52 | +→ decode-order frames 0..**84** decode (was 0..79); the wall moves to decode frame 85, still the | |
| 53 | +"a Golomb tail longer than this decoder reads" refusal (`EC_GOLOMB_LONG w=32 h=32 pos=353 base=15 length=21`), | |
| 54 | +i.e. still our own desync, now 5 frames later. | |
| 55 | + | |
| 56 | +## Exact next step | |
| 57 | +1. Re-cut for decode frame 85 (bump the OBU count until `EC_FRAME_OK` shows 85 frames, ~n=127) and | |
| 58 | + re-run the ladder: `python3 ~/.cache/frame80-tmp/lad3.py <aom_start_idx> <frame>` after locating the | |
| 59 | + frame's start with the first-30-elements substring search (see lad.py/lad2.py in the same dir). | |
| 60 | + Sweep frames 63..85 the same way — several of them failed head-alignment before the fix; re-check | |
| 61 | + which still do. | |
| 62 | +2. NOT DONE (owner: next round): pixel compare of the fixed frames vs ffmpeg with | |
| 63 | + `<scratchpad>/census7/streamcmp.py`; frame 57's two tx_depth divergences; the witness fixture | |
| 64 | + `crates/ec-av1/fixtures/reelb_head_frame80_witness.obu` + its gate; `cargo test -p ec-av1 --lib` suite. | |
| 65 | + The fix is entropy-verified on the ladder and by the 5-frame wall move only. |