From 66fb25724d76487c7450b929e0773a795f62295b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 3 Oct 2026 10:25:27 +0300 Subject: [PATCH] lane-av1muchroma: size decode_block's skip-arm chroma grid per axis The `chroma_side * chroma_side` mu-chroma sites split in two once measured. Under decode_inter_block `chroma_side` is `(side >> ss_x).max(side >> ss_y)` -- the ENCLOSING square lane-av1-422j introduced -- so at 4:2:2 those eleven sites allocate 2.0x their per-axis extent: oversized, never short. The block-tail mu_chroma_units replay strides the assembled grid by chroma_side, so the square IS the writer/reader contract there; shrinking sites 11/12 to chroma_w * chroma_h reds two pinned gates. Left alone. decode_block has its own binding, `side >> ss_x`, which at 4:2:2 is half the plane block: site 1 sized a 64x64 block's chroma at 32x32 = 1024 where the plane block is 32x64 = 2048. Latent, not live -- the only consumer reduces the plane through neighbour_state, which sums the slice and reads grid[0], and a skip block is all zeros -- but an allocation half the block it names traps the next per-row consumer. Sized per axis. Gate: s422_322x240.obu, the smallest 4:2:2 fixture that reaches the site, with two paired counters. skip_arm_chroma_rect_shapes is the non-vacuity arm (it fires only where per-axis and square differ); skip_arm_chroma_short_samples reads the allocation's real length and must stay 0. Restoring the square reds it by 64 samples. A first draft keyed the counter on `chroma_side != chroma_height` and did not bite -- that proxy was replaced by the length measurement. Reachability, from an env-gated probe over the pinned 4:2:2 corpus (10 fixtures) and the 51 4:4:4 gates: sites 4/7/10 are guarded ss_x == 0 && ss_y == 0, so 4:4:4 only -- unreachable at 4:2:2 structurally. Sites 2/8 are the side > 64 mu-chunk walks: live at 4:2:0 and 4:4:4, unreached at 4:2:2, and sound where they sit, so no fix was invented for the unwitnessed case. Sites 3/5/6/9/11/12 are reached and already correct. palette_uv_side and the floor-> chroma plane allocators are untouched and named still-open in the report. --- crates/ec-av1/src/decode.rs | 69 ++++++++++++- crates/ec-av1/src/stream.rs | 72 ++++++++++++++ lanes/av1muchroma.report.md | 186 ++++++++++++++++++++++++++++++++++++ 3 files changed, 325 insertions(+), 2 deletions(-) create mode 100644 lanes/av1muchroma.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index f8cffc8b..87f21bb8 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2125,6 +2125,43 @@ thread_local! { const { std::cell::Cell::new(0) }; } +// lane-av1muchroma site 1: the total number of samples by which +// `decode_block`'s SKIP arm chroma bookkeeping grids fell SHORT of their own +// plane block, summed over every skipped block decoded. It reads the +// allocation's real length, so it measures the shape rather than proxying a +// condition: the square form this lane removed is `side >> ss_x` squared, +// which at 4:2:2 (ss (1,0)) is exactly HALF the `side >> ss_x` x `side >> ss_y` +// plane block, and restoring it drives this counter strictly positive on +// every 4:2:2 skip block. Zero on 4:2:0 and 4:4:4, where the two forms are +// the same number. +thread_local! { + static SKIP_ARM_CHROMA_SHORT_SAMPLES: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`SKIP_ARM_CHROMA_SHORT_SAMPLES`]. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn skip_arm_chroma_short_samples() -> usize { + SKIP_ARM_CHROMA_SHORT_SAMPLES.with(|c| c.get()) +} + +// Number of times `decode_block`'s SKIP arm ran at a shape where the +// per-axis and square chroma allocations DIFFER -- i.e. at 4:2:2, where +// the square is half the plane block. Zero on 4:2:0 and 4:4:4, where the +// two forms are the same number, so a gate that wants to prove it actually +// exercised the shape under test (rather than passing vacuously) asserts +// this moved. +thread_local! { + static SKIP_ARM_CHROMA_RECT_SHAPES: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`SKIP_ARM_CHROMA_RECT_SHAPES`]. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn skip_arm_chroma_rect_shapes() -> usize { + SKIP_ARM_CHROMA_RECT_SHAPES.with(|c| c.get()) +} + /// Current value of [`CHROMA422_SQUARE_HITS`]. // lane-av1-422b: `pub` so `examples/decode_probe.rs` can census it the way // it does every other capability counter. @@ -23545,8 +23582,36 @@ fn decode_block( } } let luma_grid = vec![0i32; side * side]; - let u_grid = vec![0i32; chroma_side * chroma_side]; - let v_grid = vec![0i32; chroma_side * chroma_side]; + // lane-av1muchroma site 1: the skip arm's chroma grids are the + // block's PLANE BLOCK per axis (`chroma_side` x `chroma_height`), + // not a square. `chroma_side` here is `side >> ss_x` -- the key-frame + // path's own binding, which at 4:2:2 (ss (1,0)) is `side / 2`, so the + // old `chroma_side * chroma_side` allocated HALF the block's chroma: + // a 64x64 block's plane block is 32x64 = 2048 samples and the square + // held 1024, a 2x under-read for any per-row consumer. No current + // consumer indexes it -- `record_rect` -> `record_mi_rect` reduces + // each plane through `neighbour_state`, which sums the slice and + // reads `grid[0]` only, and every sample is zero on a skip block -- + // so the shape is latent, not live (mutation-verified: sizing this + // per-axis leaves every pinned 4:2:2 gate green, shrinking sites + // 11/12 to per-axis reds two). Fixed here so the allocation cannot + // go short if a per-row consumer ever lands. At 4:2:0 the two are + // the same number and at 4:4:4 `chroma_side == chroma_height == + // side`, so only 4:2:2 changes. + let u_grid = vec![0i32; chroma_side * chroma_height]; + let v_grid = vec![0i32; chroma_side * chroma_height]; + // The measured arm, not a proxy: how many samples this block's + // chroma bookkeeping grid is SHORT of its own plane block. The + // square form this lane removed is `chroma_side * chroma_side`, + // which at 4:2:2 is half the plane block; restoring it makes this + // non-zero on every 4:2:2 skip block, and the witness gate asserts + // the total stayed 0. + let plane_block = chroma_side * chroma_height; + let short = plane_block.saturating_sub(u_grid.len()); + hit_do!(SKIP_ARM_CHROMA_SHORT_SAMPLES.with(|c| c.set(c.get() + short))); + if chroma_side != chroma_height { + hit!(SKIP_ARM_CHROMA_RECT_SHAPES); + } neighbours.record( at, side, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index de0c660a..5e91468e 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3136,6 +3136,78 @@ pub(crate) mod tests { } } + /// lane-av1muchroma: the `chroma_side * chroma_side` mu-chroma site + /// census, over the whole committed 4:2:2 corpus, as a per-site + /// REACHABILITY measurement with a non-vacuity arm. + /// + /// `lanes/av1subsizesweep.report.md` named "mu-chroma sites using + /// `chroma_side * chroma_side`" as unprobed residue. This lane's sweep + /// (env-gated probe at each of the 12 sites, run over every pinned 4:2:2 + /// gate) measured them: + /// + /// - sites 4, 7, 10 are guarded `ss_x == 0 && ss_y == 0`, i.e. 4:4:4 + /// ONLY -- structurally unreachable at 4:2:2, not merely unwitnessed. + /// - sites 2, 8 (`side > 64` mu-chunk walks) were not reached by any + /// committed 4:2:2 fixture; their single-reference twin, site 5, is + /// (side 128, plane 64x128), and its square allocation is the ENCLOSING + /// square, so it is 2x oversized, never short. + /// - sites 1, 3, 5, 6, 9, 11, 12 ARE reached. For 3/5/6/9/11/12 + /// `chroma_side` is `max(side >> ss_x, side >> ss_y)` (decode.rs), the + /// ENCLOSING square of the plane rect, so at 4:2:2 the square is 2x + /// the per-axis extent: oversized, never short. + /// - site 1 alone still used `side >> ss_x` and was therefore sized at + /// HALF the plane block at 4:2:2. Fixed per-axis; this gate pins it. + /// + /// Non-vacuity: [`crate::decode::skip_arm_chroma_plane_block_hits`] + /// fires only where the per-axis and square forms differ, so a green run + /// that never reached the site would be vacuous and this asserts > 0. + #[test] + fn the_422_skip_arm_chroma_grid_is_sized_per_axis_not_square() { + const NAME: &str = "the_422_skip_arm_chroma_grid_is_sized_per_axis_not_square"; + const FILE: &str = "s422_322x240.obu"; + const BYTES: usize = 21096; + const FP: u64 = 0x9a96dda2a19d47a6; + const W: usize = 322; + const H: usize = 240; + const FRAMES: usize = 16; + let _gate_lock = lock_gate_counters(); + let data = std::fs::read(crate_pin(FILE)).unwrap_or_else(|e| { + panic!("{NAME}: the committed pin {FILE} is missing ({e}) -- a failure, not a skip") + }); + assert_eq!(data.len(), BYTES, "{NAME}: {FILE} size drifted"); + assert_eq!(fnv1a64(&data), FP, "{NAME}: {FILE} bytes drifted"); + let rect_before = crate::decode::skip_arm_chroma_rect_shapes(); + let short_before = crate::decode::skip_arm_chroma_short_samples(); + let ours = decode_stream(&data) + .unwrap_or_else(|e| panic!("{NAME}: decode_stream refused a real 4:2:2 stream: {e}")); + assert_eq!(ours.len(), FRAMES, "{NAME}: decoded frame count"); + for (i, f) in ours.iter().enumerate() { + assert_eq!((f.width, f.height), (W, H), "{NAME}: frame {i} dimensions"); + assert_eq!( + (f.u.len(), f.v.len()), + (W.div_ceil(2) * H, W.div_ceil(2) * H), + "{NAME}: frame {i} chroma planes are {}/{} samples -- 4:2:2 is HALF WIDTH and \ + FULL HEIGHT; a quarter-height plane means the decode kept a 4:2:0 allocation", + f.u.len(), + f.v.len() + ); + } + let rect_after = crate::decode::skip_arm_chroma_rect_shapes(); + assert!( + rect_after > rect_before, + "{NAME}: no skipped block decoded at a rect chroma shape ({rect_before} -> \ + {rect_after}) -- this gate is then VACUOUS: per-axis and square sizing are the \ + same number at 4:2:0 and 4:4:4, so it proves nothing about the site it claims" + ); + let short_after = crate::decode::skip_arm_chroma_short_samples(); + assert_eq!( + short_after, short_before, + "{NAME}: decode_block's SKIP arm chroma grids fell {short_after} samples SHORT of \ + their own plane block -- the square `side >> ss_x` form this lane removed allocates \ + exactly half the plane block at 4:2:2 (ss (1,0))" + ); + } + /// lane-av1422tailskip: the WITNESS cell for the 4:2:2 intra-in-inter /// PALETTE chroma per-unit window, and its byte pin. /// diff --git a/lanes/av1muchroma.report.md b/lanes/av1muchroma.report.md new file mode 100644 index 00000000..a30cce6a --- /dev/null +++ b/lanes/av1muchroma.report.md @@ -0,0 +1,186 @@ +# lane-av1muchroma — the `chroma_side * chroma_side` mu-chroma sites + +Base: `main` @ `820eeb68` (post `lane/av1paletteuv`). Branch: `lane/av1muchroma`. +Scope: **only** the mu-chroma sites that size a chroma region as +`chroma_side * chroma_side`. `palette_uv_side` and the floor-`>>` chroma plane +allocators are **untouched** — named still-open at the end. + +`lanes/av1subsizesweep.report.md:241` named this residue "not probed; needs an +owning lane with a 128-root 4:2:2 mu-chunk witness". This lane probed it. The +headline is that **the report's premise had gone stale**: line numbers drifted +AND `chroma_side` itself was redefined by `lane-av1-422j` after that row was +written, which changes most of the verdict. + +## The two `chroma_side` bindings (this is the whole story) + +There are two, and they are NOT the same expression: + +| binding | line | expression | 4:2:2 value for a `side`-square block | +|---|---|---|---| +| `decode_block` (key frames) | `decode.rs:23261` | `side >> ss_x` | `side / 2` | +| `decode_inter_block` (inter) | `decode.rs:42130` | `(side >> ss_x).max(side >> ss_y)` | `side` | + +`lane-av1-422j` changed the second to the **enclosing square** of the chroma +rect. So at 4:2:2 (ss `(1,0)`), where the true plane block is `(side/2, side)`: + +- every site under `decode_inter_block` allocates `side * side` — the + **enclosing** square, i.e. **2x the per-axis extent: oversized, never short**; +- site 1, under `decode_block`, allocates `(side/2) * (side/2)` — exactly + **half** the plane block: **short**. + +The `subsizesweep` row asserted "a square `side/2` is too short on rows +`>= side/2`". That is true of site 1 and false of the other eleven, and the +row did not distinguish them. + +## Reachability, measured + +Method: an env-gated `eprintln` probe at each of the 12 sites printing +`(site, ss_x, ss_y, side, chroma_side, plane_w, plane_h)`, run over every +pinned 4:2:2 gate (`the_pinned_422_*`, 7 tests) and the 4:4:4 sweep (51 tests). +The probe was temporary and is **not** in the commit. Counts are probe hits: + +| site | arm | 4:2:2 hits | 4:4:4 hits | 4:2:0 hits | +|---|---|---|---|---| +| 1 | `decode_block` skip arm | **16** | 344 | 5064 | +| 2 | compound `side>64` mu-chunk | 0 | 0 | 2700 | +| 3 | compound lossless whole-block | **258** | 2559 | 89 | +| 4 | compound 4:4:4 quadrant | 0 | 24 | 0 | +| 5 | single-ref `side>64` mu-chunk | **16** | 32 | 2224 | +| 6 | single-ref lossless whole-block | **457** | 3892 | 3952 | +| 7 | single-ref 4:4:4 quadrant | 0 | 648 | 0 | +| 8 | intra-in-inter `side>64` vartx | 0 | 96 | 0 | +| 9 | intra lossless whole-block | **160** | 283 | 549 | +| 10 | intra 4:4:4 quadrant | 0 | 31 | 0 | +| 11 | intra 4:2:2 `side==64` U | **14** | 0 | 0 | +| 12 | intra 4:2:2 `side==64` V | **14** | 0 | 0 | + +Corpus: **10 committed 4:2:2 fixtures** across the 7 pinned gates, plus 51 +4:4:4 gates. 4:2:2 fixture inventory is 11 `.obu` files; `422_residual_compound_warp_nolr_16f.obu` +is the lr-off arm of another gate, so 10 distinct witnesses fed these numbers. + +## Per-site verdict + +**Sites 4, 7, 10 — unreachable at 4:2:2, structurally.** Each is guarded +`ss_x(fctx) == 0 && ss_y(fctx) == 0` (`decode.rs:43983`, `45806`, `46866`), +which is 4:4:4 exactly. Not merely unwitnessed: no 4:2:2 stream can enter them. +At 4:4:4 the two forms coincide anyway. + +**Sites 2, 8 — unreachable at 4:2:2 by the committed corpus.** Both are +`side > 64` mu-chunk walks inside the compound and intra-in-inter paths. No +committed 4:2:2 fixture reaches them (2 is hit 2700x at 4:2:0, 8 is hit 96x at +4:4:4 — so both are live, just not at this subsampling). Their allocation is the +enclosing square, and their twin **site 5** *is* reached at 4:2:2 (side 128, +plane 64x128) with an in-bounds max index of 16319 against a 16384 allocation. +**No fix invented for the unwitnessed case**, per the standing rule. + +**Sites 3, 5, 6, 9, 11, 12 — already correct.** All reached at 4:2:2. All sit +under the enclosing-square `chroma_side`, so each allocates 2.0x its per-axis +extent. Arithmetic check at every observed shape, e.g.: + +``` +3/6/9 lossless zero side=16 cs=16 plane=(8,16) alloc=256 need=128 maxidx=247 OK 2.0x +3/6/9 lossless zero side=32 cs=32 plane=(16,32) alloc=1024 need=512 maxidx=1007 OK 2.0x +3/6/9 lossless zero side=64 cs=64 plane=(32,64) alloc=4096 need=2048 maxidx=4063 OK 2.0x +5 single-ref mu-chunk side=128 cs=128 plane=(64,128) alloc=16384 need=8192 maxidx=16319 OK 2.0x +11/12 422 side==64 side=64 cs=64 plane=(32,64) alloc=4096 need=2048 maxidx=4063 OK 2.0x +``` + +Mutation-verified in the other direction too: **shrinking sites 11/12 to +`chroma_w * chroma_h` reds** `the_pinned_422_corpus_cells` and +`the_pinned_422_palette_intra_in_inter`. The block-tail `mu_chroma_units` +replay (`decode.rs:47387`) strides the assembled grid by `chroma_side`, so the +square **is** the writer/reader contract here. These are not defects and must +not be "fixed" per-axis. + +**Site 1 — FIXED and gated.** The only genuine under-allocation. +`decode.rs:23587`: + +```rust +// before +let u_grid = vec![0i32; chroma_side * chroma_side]; // (side/2)^2 at 4:2:2 +// after +let u_grid = vec![0i32; chroma_side * chroma_height]; // the plane block +``` + +**Was it live?** No — the shape was latent, and this lane says so rather than +overclaiming. The sole consumer is `record_rect` → `record_mi_rect`, which +reduces each plane through `neighbour_state` (`decode.rs:9032`): it sums the +slice and reads `grid[0]`, nothing per-row, and on a skip block every sample is +zero. Mutation-confirmed both ways: sizing site 1 per-axis leaves all 7 pinned +4:2:2 gates green (no live pixel path), so this is a correctness fix for a +consumer that does not exist yet, not a red-to-green rescue. Fixed anyway: an +allocation half the block it names is a trap for the next per-row consumer. + +## The gate + +`stream.rs::the_422_skip_arm_chroma_grid_is_sized_per_axis_not_square`, witness +`s422_322x240.obu` (21096 B, fnv1a64 `0x9a96dda2a19d47a6`, 322x240, 8-bit, +16 decode frames) — chosen because it is the smallest 4:2:2 fixture that +actually reaches site 1 (six do: `s422_320x246` +4, `s422_322x240` +1, +`s422_322x246` +3, `s422_352x242_10b` +2, `s422_416x242_10b` +3, +`s422_416x250_10b` +3). + +Two paired counters in `decode.rs`, deliberately **not** one: + +- `skip_arm_chroma_rect_shapes` — non-vacuity. Fires only where the per-axis and + square forms differ (4:2:2). The gate asserts it moved, so a witness that + stopped reaching the site cannot fake a green. +- `skip_arm_chroma_short_samples` — the claim. Reads + `chroma_side * chroma_height - u_grid.len()`, i.e. the allocation's **real + length**, not a proxy condition. The gate asserts it stayed 0. + +### Mutation proof (restore the square) + +``` +let u_grid = vec![0i32; chroma_side * chroma_side]; // restored +``` +→ +``` +panicked at stream.rs:3204: +decode_block's SKIP arm chroma grids fell 64 samples SHORT of their own +plane block -- the square `side >> ss_x` form this lane removed allocates +exactly half the plane block at 4:2:2 (ss (1,0)) +test result: FAILED. 0 passed; 1 failed +``` + +The first gate draft used a `chroma_side != chroma_height` proxy counter and did +**not** bite on that mutation — a vacuous gate, caught and replaced with the +length-measuring counter above. + +## Tests + +| suite | result | +|---|---| +| 7 pinned `the_pinned_422_*` byte-exact gates | 7/7 ok (unchanged from base) | +| new `the_422_skip_arm_chroma_grid_...` | ok | +| 51 `444` gates | 51/51 ok, no regression | +| full `cargo test -p ec-av1 --lib` | timed out at 3600 s on live `aom`-encode tests (`a_real_aomenc_inter_128x128_none_root_...`); not a failure, an unfinished sweep. All 4:2:2 and 4:4:4 subsets ran green. | + +Feature note: the gate's counters are behind the existing `gate-counters` +feature like every other counter in this crate; it was run with +`--features gate-counters`. + +## Still open, not touched by this lane + +1. **Floor-`>>` chroma plane allocators** — `decode.rs` frame-edge allocators + named at `av1subsizesweep.report.md:242`. Out of charter; untouched. +2. **`palette_uv_side`** (`tile.rs`) — gated by `lane/av1paletteuv`, untouched. +3. **Sites 2 and 8 at 4:2:2** — allocation is sound (enclosing square), but no + committed 4:2:2 fixture reaches them. A 128-root 4:2:2 compound or + intra-in-inter witness would close the reachability question; **no fix is + warranted** and none was written. +4. The two stride conventions in the block-tail replay — `chroma_stride` at + `decode.rs:47347` (lossless) vs `chroma_side` at `47387` (non-lossless). + Both correct for their own writer; noted as a convention split, not a defect. + +## Reproduction + +``` +git worktree add ~/.cache/wt/av1muchroma -b lane/av1muchroma 820eeb68 +cd ~/.cache/wt/av1muchroma +CARGO_TARGET_DIR=~/.cache/muchroma-target \ + cargo test -p ec-av1 --lib --features gate-counters -- \ + the_pinned_422 the_422_skip +``` + +`ffmpeg` is on `PATH`; the byte-exact gates run live against it. \ No newline at end of file