diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index af9b70ae..0aa4d130 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -12618,6 +12618,16 @@ fn decode_intrabc_128rect( // within the chunk, plane-major inside the chunk. let cu_tx = 32usize; let chunk_chroma = cside * 64 / side; + // lane-av1-128rectspan: SQUARE per-axis geometry -- one value + // for both axes, which holds only while `ss_x == ss_y`. At + // 4:2:2 a TX_32X32 chroma unit's luma footprint is + // (64, 32), a mu chunk's chroma plane block is 32x64 (two + // stacked units), and `units_y`/`cu_y`/`unit_mi.0` below all + // need the same per-axis treatment + // `decode_block_128rect`'s walk already has. 4:2:2 is refused + // by name at the sequence header, so this arm is + // unreachable for it today -- it is a PORT note for that + // format, not a live defect. let unit_luma = cu_tx * (side / cside); let chunk_w_mi = 16.min(max_w_mi.saturating_sub(cc * 16)); let chunk_h_mi = 16.min(max_h_mi.saturating_sub(cr * 16)); @@ -18795,6 +18805,25 @@ thread_local! { /// frame's `tx_mode` is forced `ONLY_4X4`). Keeps the gate that owns this /// shape non-vacuous -- a stream that never codes one reads 0. pub(crate) static INTRA128_LOSSLESS_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// lane-av1-128rectspan: chroma transform units of a 128-root HORZ/VERT + /// intra block re-stamped by the end-of-block replay + /// (`record_split_luma_rect` clobbers the per-unit contexts the chroma + /// walk published, so every unit replays its own state after it). A + /// non-skip 128x64/64x128 block codes `nch_x * cn * 2 + nch_y * cn_h * 2` + /// of them, so this reads 0 on any stream that never codes one -- the + /// non-vacuity anchor for the replay's own gate. + pub(crate) static SB128RECT_CHROMA_REPLAY_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// ... and the number of non-skip 128-root rect BLOCKS whose replay + /// height differs from the unit's own LUMA-MI height -- the signature of the + /// width passed to both axes (`luma_span` where the in-loop stamp used + /// `luma_span_h`). Zero on every stream this decoder admits: both + /// supported chroma formats have `ss_x == ss_y` (4:2:0 and 4:4:4), and + /// 4:2:2 -- the one format whose two spans differ -- is refused by name + /// at the sequence header (`stream::decode_frame`), so the arm is + /// defensive until that format is ported. The counter turns a future + /// 4:2:2 admission (or any hardcoded span) into a named red gate instead + /// of silent mi-state corruption. + pub(crate) static SB128RECT_REPLAY_SPAN_MISMATCH_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// [`SKIP_VARTX_LUMA_RESET_HITS`], for a gate's own before/after delta. @@ -18822,6 +18851,17 @@ pub(crate) fn intra_sb128_hits() -> (usize, usize, [usize; 3]) { ) } +/// [`SB128RECT_CHROMA_REPLAY_HITS`] (chroma units re-stamped by +/// `decode_block_128rect`'s end-of-block replay) and +/// [`SB128RECT_REPLAY_SPAN_MISMATCH_HITS`] (replays whose stamped height was +/// not the unit's own), for a gate's own before/after delta. +pub(crate) fn sb128rect_chroma_replay_hits() -> (usize, usize) { + ( + SB128RECT_CHROMA_REPLAY_HITS.with(std::cell::Cell::get), + SB128RECT_REPLAY_SPAN_MISMATCH_HITS.with(std::cell::Cell::get), + ) +} + /// The 128 root's HORZ/VERT arm on an INTRA frame: ONE 128x64 (HORZ) or /// 64x128 (VERT) intra block (lane-sb128c r8; the inter twin is /// `decode_inter_block`'s own rect path). @@ -19189,8 +19229,29 @@ fn decode_block_128rect( uv_predict_mode, [&last_grids[0], &last_grids[1]], ); + // lane-av1-128rectspan: the replay must re-stamp each unit over the + // SAME cells the walk above stamped it over -- `record_mi_chroma`'s + // height drives `left[]` and its width `above[]`, and the walk used + // the per-axis `luma_span_h`. Passing the width on both axes doubled + // the `left[]` run wherever the two differ (4:2:2), reaching 8 mi + // rows below the block on the last unit of a 128x64/64x128 strip and + // smearing a neighbour chunk's unit over its neighbour's rows. + // + // DEFENSIVE at the decoder's current capability set: both admitted + // chroma formats have `ss_x == ss_y` (4:2:0 ss (1,1), 4:4:4 ss + // (0,0)), so the two spans are equal and this line is a no-op on + // every stream `decode_stream` accepts; 4:2:2 (ss (1,0)) -- the one + // format where they differ -- is refused by name at the sequence + // header, so the arm is unreachable there. It stays per-axis because + // the walk it replays is per-axis, and a 4:2:2 port must not have to + // re-derive this. `SB128RECT_REPLAY_SPAN_MISMATCH_HITS` names the + // day that changes. + if luma_span != luma_span_h { + hit!(SB128RECT_REPLAY_SPAN_MISMATCH_HITS); + } for (plane, cu_mi, grid) in &units { - neighbours.record_mi_chroma(*cu_mi, luma_span, luma_span, *plane, grid); + neighbours.record_mi_chroma(*cu_mi, luma_span, luma_span_h, *plane, grid); + hit!(SB128RECT_CHROMA_REPLAY_HITS); } } // No palette exists at this size; the unconditional stamp clears any diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 2f9d5462..de5cbe2f 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -260,6 +260,15 @@ pub fn intra128_lossless_counters() -> usize { crate::decode::intra128_lossless_hits() } +/// lane-av1-128rectspan: chroma transform units re-stamped by +/// `decode_block_128rect`'s end-of-block replay, and how many of those +/// replays stamped a height that was not the unit's own (the 4:2:2 signature +/// -- zero on every stream this decoder admits, since 4:2:2 is refused by +/// name at the sequence header). +pub fn sb128rect_chroma_replay_counters() -> (usize, usize) { + crate::decode::sb128rect_chroma_replay_hits() +} + /// lane-sb128c r9: the four AB shapes at the 128 root, in `PARTITION_HORZ_A`, /// `_HORZ_B`, `_VERT_A`, `_VERT_B` order (key and inter tile paths share the /// counter -- it is bumped where the root symbol resolves). @@ -5868,6 +5877,7 @@ pub(crate) mod tests { let _guard = lock_gate_counters(); let before = crate::decode::intra_sb128_hits(); + let before_replay = crate::decode::sb128rect_chroma_replay_hits(); let frames = decode_stream(&stream).unwrap_or_else(|e| { panic!("{NAME}: the pinned stream no longer decodes cleanly: {e}") }); @@ -5877,6 +5887,35 @@ pub(crate) mod tests { "{NAME}: this stream coded NO 128-axis intra block -- the fixed body \ never ran (class gate-blind-to-feature)" ); + // lane-av1-128rectspan: the block-level `record_split_luma_rect` + // above clobbers the per-unit chroma contexts, so every coded unit + // replays its own state after it -- the arm the span fix sits on. + // The delta keeps THAT arm non-vacuous (a 128-root rect stream that + // stopped replaying reads 0), and the mismatch half is the 4:2:2 + // signature: this stream is 4:4:4 (ss 0,0), where the unit's luma + // width and height are equal, so a replay that stamped one axis with + // the other's span would still read 0 here -- the counter is what + // names the day 4:2:2 (ss 1,0, refused by name at the sequence + // header) is admitted, rather than letting the 8-row spill below the + // block corrupt the next block's `txb_skip` silently. + let after_replay = crate::decode::sb128rect_chroma_replay_hits(); + assert!( + after_replay.0 > before_replay.0, + "{NAME}: no 128-root rect block replayed a chroma unit's own mi state -- the \ + replay arm never ran (class gate-blind-to-feature)" + ); + assert_eq!( + after_replay.1, + before_replay.1, + "{NAME}: the tail replay stamped {} unit(s) over a height that was not the \ + unit's own luma height (4:2:2 spans leaked into an admitted stream)", + after_replay.1 - before_replay.1 + ); + eprintln!( + "{NAME}: replayed {} chroma unit(s), {} span mismatch(es)", + after_replay.0 - before_replay.0, + after_replay.1 - before_replay.1 + ); assert_eq!(frames.len(), FRAMES, "{NAME}: frame count"); for f in &frames { assert_eq!((f.width, f.height), (W, H), "{NAME}: dimensions"); @@ -6382,9 +6421,12 @@ pub(crate) mod tests { #[test] fn a_lossless_sb128_rect_intra_block_decodes_sample_exact() { const NAME: &str = "a_lossless_sb128_rect_intra_block_decodes_sample_exact"; - if !have_ffmpeg() { - eprintln!("SKIP {NAME}: no ffmpeg"); - return; + // The pixel arm needs ffmpeg; the decode and its counter deltas do + // not, so a box without ffmpeg still proves the shape was reached + // (class `gate-skips-on-its-own-failure`) -- the skip is still named. + let ffmpeg = have_ffmpeg(); + if !ffmpeg { + eprintln!("SKIP {NAME}: no ffmpeg -- the ffmpeg pixel arm only"); } let (w, h, frames) = (320usize, 256usize, 4usize); let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) @@ -6392,6 +6434,7 @@ pub(crate) mod tests { let stream = std::fs::read(&path) .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); let before = crate::decode::intra128_lossless_hits(); + let before_replay = crate::decode::sb128rect_chroma_replay_hits(); let ours = decode_stream(&stream) .unwrap_or_else(|e| panic!("{NAME}: a lossless 128-axis key frame decodes: {e}")); let hits = crate::decode::intra128_lossless_hits() - before; @@ -6400,6 +6443,33 @@ pub(crate) mod tests { "{NAME}: this stream coded NO lossless 128-axis intra block -- the fixed body \ was never reached (class gate-blind-to-feature)" ); + // lane-av1-128rectspan: the 4:2:0 twin of the 4:4:4 gate's replay + // assertion. This stream codes the 64x128 block at mi(0,64) with + // TX_4X4 chroma units, so the end-of-block replay re-stamps each of + // them over its own luma-mi rows -- the arm the span fix sits on, on + // the OTHER admitted ss (1,1). The mismatch half is the 4:2:2 + // signature and reads 0 here because 4:2:2 is refused by name at the + // sequence header; it is the tripwire for the day it is not. + let after_replay = crate::decode::sb128rect_chroma_replay_hits(); + assert!( + after_replay.0 > before_replay.0, + "{NAME}: no 128-axis block replayed a chroma unit's own mi state -- the replay \ + arm never ran (class gate-blind-to-feature)" + ); + assert_eq!( + after_replay.1, before_replay.1, + "{NAME}: the tail replay stamped {} unit(s) over a height that was not the \ + unit's own luma height (4:2:2 spans leaked into an admitted stream)", + after_replay.1 - before_replay.1 + ); + eprintln!( + "{NAME}: replayed {} chroma unit(s), {} span mismatch(es)", + after_replay.0 - before_replay.0, + after_replay.1 - before_replay.1 + ); + if !ffmpeg { + return; + } let refs = ffmpeg_decode_sequence(&stream, w, h, frames); assert_eq!(ours.len(), frames, "{NAME}: decode-order frame count"); for (i, (got, want)) in ours.iter().zip(refs.iter()).enumerate() { diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index b234dcda..622043c5 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -413,6 +413,14 @@ fn write_inter_block_128( // libaom codes every plane of a mu chunk before moving on. The // chroma `txb_skip_ctx` takes `get_txb_ctx`'s offset-10 rows // (+3 here) because the uv plane block is bigger than the unit. + // lane-av1-128rectspan: SQUARE per-axis geometry -- one TX_32X32 + // per 64x64 mu chunk, stamped at span 64 on BOTH axes. That is + // 4:2:0/4:4:4 only: at 4:2:2 the chunk's chroma plane block is + // 32x64 (two stacked units) with a (64, 32) luma footprint per + // unit, so `unit_mi`, the `64`-spans and the whole `CHUNKS` + // raster need the same per-axis treatment before 4:2:2 could be + // written here. Refused by name at the sequence header today, so + // this is a PORT note, not a live defect. let mut pair = [Vec::new(), Vec::new()]; for plane in 1..3usize { let grid = unit_of(plane, cr, cc);