diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index c01faeb5..dd29bfc8 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -27780,9 +27780,37 @@ fn read_block_tx_size( // `read_block_tx_size` (`decodeframe.c`) sends a lossless block down the // `read_tx_size` arm, which returns TX_4X4 before it reads anything. if lossless(fctx) { - let mut leaves = Vec::new(); - for row in 0..side / MI { - for col in 0..side / MI { + // lane-av1lm444loss: libaom CLIPS the transform-unit grid at the frame + // edge -- `max_block_high`/`max_block_wide` (`av1_common_int.h:1565`, + // `:1579`) fold `mb_to_bottom_edge`/`mb_to_right_edge` into + // `block_size_high/wide` before `>> MI_SIZE_LOG2`, so a block that + // hangs off the bottom/right codes only the units INSIDE the frame. + // `decode_token_recon_block`'s mu walk then bounds every chunk with + // `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + row, + // max_blocks_high), ss_y)`, so the out-of-frame units are never read. + // + // The unclipped `side / MI` grid is correct arithmetic for a block that + // FITS, which is every block of a frame whose height and width are + // multiples of the superblock -- so this reads identically on all of + // those, and only differs on the partial edge block. Measured on the + // 128x96 4:4:4 lossless pin (one 128 root per inter frame, frame 32 + // mi = 24 tx rows tall): the unclipped grid read 32 rows where the + // oracle read 24 -- 256 extra luma units per inter frame (1024 vs + // the oracle's 768), the `EC_SYMR` ladder's first fork landing exactly + // at that row-24 boundary (`plane=0` luma where the oracle had moved + // to `plane=1`), and the pixel diff growing 5596 -> 33561 wrong + // samples over the six frames. + let side_mi = side / MI; + let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1)); + let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0)); + if max_w_mi != side_mi || max_h_mi != side_mi { + LOSSLESS_EDGE_CLIPPED.with(|c| c.set(c.get() + 1)); + LOSSLESS_EDGE_CLIP_UNITS + .with(|c| c.set(c.get() + (side_mi * side_mi - max_w_mi * max_h_mi))); + } + let mut leaves = Vec::with_capacity(max_w_mi * max_h_mi); + for row in 0..max_h_mi { + for col in 0..max_w_mi { leaves.push((row, col, 4, 4)); } } @@ -28262,9 +28290,24 @@ fn read_block_tx_size_rect( fctx: &crate::decode::FrameCtx, ) -> Result>> { if lossless(fctx) { - let mut leaves = Vec::new(); - for row in 0..bh / MI { - for col in 0..bw / MI { + // lane-av1lm444loss: the same frame-edge clip as the square builder + // above -- `max_block_high`/`max_block_wide` bound a rect block's + // transform grid by the part of it inside the frame, so a 1:4 strip + // hanging off the bottom codes only its in-frame rows. Identical + // arithmetic for a block that fits. + let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(at_mi.1)); + let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(at_mi.0)); + let full_w_mi = bw / MI; + let full_h_mi = bh / MI; + if max_w_mi != full_w_mi || max_h_mi != full_h_mi { + LOSSLESS_EDGE_CLIPPED.with(|c| c.set(c.get() + 1)); + LOSSLESS_EDGE_CLIP_UNITS.with(|c| { + c.set(c.get() + (full_w_mi * full_h_mi - max_w_mi * max_h_mi)) + }); + } + let mut leaves = Vec::with_capacity(max_w_mi * max_h_mi); + for row in 0..max_h_mi { + for col in 0..max_w_mi { leaves.push((row, col, 4, 4)); } } @@ -32551,6 +32594,19 @@ thread_local! { /// 128x96 4:4:4 lossless stream, 1536 per inter frame (768 per plane), /// against the oracle's 1536. pub(crate) static MU_CHUNK_WALK_UNITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// lane-av1lm444loss: lossless blocks whose transform-unit grid was + /// CLIPPED at the frame edge -- `max_block_high`/`max_block_wide` cut the + /// grid short of the block's own `side / MI`, so libaom codes fewer TX_4X4 + /// units than the block is wide/tall. Zero on any frame whose height and + /// width are multiples of the superblock, so a gate cannot look armed by + /// merely decoding. + pub(crate) static LOSSLESS_EDGE_CLIPPED: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// lane-av1lm444loss: the transform units that clip REMOVED -- the + /// `(side / MI - clipped) * (side / MI)` overflow the pre-fix walk read + /// past the frame bottom/right edge. On the 128x96 4:4:4 lossless pin this + /// is 256 per inter frame (8 unclipped luma rows x 32 columns), and 0 on a + /// frame that fits. + pub(crate) static LOSSLESS_EDGE_CLIP_UNITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// [`MU_CHUNK_WALKS`], for a gate's own before/after delta. @@ -32570,6 +32626,19 @@ pub(crate) fn mu_chunk_walk_units() -> usize { pub(crate) fn mu_chunk_order_hits() -> usize { MU_CHUNK_ORDER_HITS.with(std::cell::Cell::get) } +/// [`LOSSLESS_EDGE_CLIPPED`] -- lossless blocks whose TX grid the frame-edge +/// clip shortened, for a gate's own before/after delta. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn lossless_edge_clipped() -> usize { + LOSSLESS_EDGE_CLIPPED.with(std::cell::Cell::get) +} + +/// [`LOSSLESS_EDGE_CLIP_UNITS`] -- transform units the clip removed, i.e. the +/// ones the unclipped pre-fix walk read past the frame edge. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn lossless_edge_clip_units() -> usize { + LOSSLESS_EDGE_CLIP_UNITS.with(std::cell::Cell::get) +} /// [`CHROMA_SPLIT_TX_HITS`], for a gate's own before/after delta. #[allow(dead_code)] // read only from the `#[cfg(test)]` gates diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index a5188571..934dc4ea 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -6883,6 +6883,128 @@ pub(crate) mod tests { } } + /// lane-av1lm444loss: a LOSSLESS block's transform-unit grid is clipped at + /// the FRAME edge, exactly as libaom's `max_block_high`/`max_block_wide` + /// clip it (`av1_common_int.h:1565`, `:1579` fold `mb_to_bottom_edge` / + /// `mb_to_right_edge` into `block_size_high/wide` before `>> + /// MI_SIZE_LOG2`), so a block hanging off the bottom/right codes only the + /// units inside the frame. `decode_token_recon_block` then bounds every + /// mu chunk with `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + /// + row, max_blocks_high), ss_y)`, so the overflow units are never read + /// there either. + /// + /// Fixture is the sibling [`a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major`] + /// pin -- 128x96 yuv444p 4:4:4 lossless, 6 frames, 63429 bytes -- because + /// 96 is NOT a multiple of the 128 superblock: its five inter frames each + /// carry one unsplit 128 root that overhangs the bottom by 32 px. That is + /// the whole reason this stream is the witness. The frame is 24 mi tall, + /// so `max_blocks_high >> 2` is 24 transform rows where the block's own + /// `side / MI` is 32. + /// + /// The three assertions are all non-vacuous and all EXACT: + /// + /// 1. `lossless_edge_clipped()` fired -- 5, one per inter frame's 128 + /// root. It is 0 on any frame whose height and width are multiples of + /// the superblock, so a stream that merely decodes cannot satisfy it. + /// 2. `lossless_edge_clip_units()` is 1280 = 5 x 256: the 8 transform rows + /// x 32 columns the unclipped walk read past the frame bottom, per + /// inter frame. The pre-fix walk read 2560 units per inter frame + /// against the oracle's 2304, and this is that 256. + /// 3. every sample of every frame matches the oracle `aomdec + /// --rawvideo` in decode order -- all six frames, hidden alt-ref + /// included, which the pre-fix tree failed from frame 1 on + /// (5596 wrong samples there, growing to 33561 by frame 5). + /// + /// The control arm pins the other direction of the invariant: the + /// `ll444_minp64_128root_control` stream has no overhanging root, so its + /// clip counters must both read 0 -- the clip cannot fire on a block that + /// fits, which is what keeps every other lossless pin byte-identical. + #[test] + fn a_lossless_block_clips_its_transform_grid_at_the_frame_edge() { + const NAME: &str = "a_lossless_block_clips_its_transform_grid_at_the_frame_edge"; + const W: usize = 128; + const H: usize = 96; + const FRAMES: usize = 6; + /// 5 inter frames, one unsplit 128 root each; frame 0 splits. + const ROOTS: usize = 5; + /// `max_blocks_high >> 2` = 24 rows (96 px / 4), against the block's + /// own `side / MI` = 32 -- so 8 rows x 32 columns = 256 units per root + /// were read past the frame bottom before the clip. + const CLIP_UNITS_PER_ROOT: usize = 256; + + let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("fixtures/ll444_128root_lossless.obu"); + let stream = read_pin(&obu, 63429, 0xf07d_47fc_fd51_2658, NAME); + let _guard = lock_gate_counters(); + let (c0, u0) = ( + crate::decode::lossless_edge_clipped(), + crate::decode::lossless_edge_clip_units(), + ); + let frames = decode_stream(&stream) + .unwrap_or_else(|e| panic!("{NAME}: the pinned stream no longer decodes cleanly: {e}")); + let (c1, u1) = ( + crate::decode::lossless_edge_clipped(), + crate::decode::lossless_edge_clip_units(), + ); + assert_eq!( + c1 - c0, + ROOTS, + "{NAME}: the frame-edge clip fired on {}/{} of the 128 roots -- it must be 0 \ + on a frame that fits, so this is what makes the gate non-vacuous", + c1 - c0, + ROOTS + ); + assert_eq!( + u1 - u0, + ROOTS * CLIP_UNITS_PER_ROOT, + "{NAME}: the clip removed {}/{} transform units; the unclipped walk read {} \ + per inter frame past the 96 px frame bottom (8 rows x 32 columns)", + u1 - u0, + ROOTS * CLIP_UNITS_PER_ROOT, + CLIP_UNITS_PER_ROOT + ); + assert_eq!(frames.len(), FRAMES, "{NAME}: frame count"); + for f in &frames { + assert_eq!((f.width, f.height), (W, H), "{NAME}: dimensions"); + } + if aomdec_path().is_file() { + assert_rawvideo_matches(&obu, &stream, NAME, FRAMES); + } else { + eprintln!( + "SKIP {NAME} aomdec arm: no oracle aomdec at {}", + aomdec_path().display() + ); + } + + // Control: a stream whose roots all fit must not clip at all. + let ctrl = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("fixtures/ll444_minp64_128root_control.obu"); + let ctrl_stream = read_pin( + &ctrl, + 61494, + 0xf6f5_a3cb_efc7_eb10, + &format!("{NAME} control"), + ); + let (c2, u2) = ( + crate::decode::lossless_edge_clipped(), + crate::decode::lossless_edge_clip_units(), + ); + let _ = decode_stream(&ctrl_stream) + .unwrap_or_else(|e| panic!("{NAME}: the control arm no longer decodes cleanly: {e}")); + assert_eq!( + crate::decode::lossless_edge_clipped() - c2, + 0, + "{NAME} control: the clip fired on a stream whose blocks all fit -- the clip \ + must be a no-op there, which is what keeps every other lossless pin \ + byte-identical" + ); + assert_eq!( + crate::decode::lossless_edge_clip_units() - u2, + 0, + "{NAME} control: the clip removed units from a stream whose blocks all fit" + ); + } + /// lane-av1llpredgate2: the debug-exact pin for the ss-aware /// `obmc_skip_chroma_above` decision (lane-av1-llpred2). Fixture is the /// llpred/llpred2 reports' stream pinned into `fixtures/`: testsrc2 diff --git a/lanes/av1lm444loss.report.md b/lanes/av1lm444loss.report.md new file mode 100644 index 00000000..20d1659b --- /dev/null +++ b/lanes/av1lm444loss.report.md @@ -0,0 +1,357 @@ +# lane-av1lm444loss — H1: 4:4:4 LOSSLESS byte-exactness + +**Verdict.** Lead (a) **FIXED**: the pinned `ll444_128root_lossless.obu` is now +byte-exact on all six frames and its `EC_SYMR` ladder is bit-identical to the +oracle (235,940 reads, zero fork; it forked at read 71,322 before). The defect +was **not** in the coefficient reader — it was the transform-unit **walk**: a +lossless block's TX grid was never clipped at the frame edge, so the decoder +read 256 units per inter frame that libaom never codes, and the fork lands on a +PLANE change (`plane=0` luma where the oracle had moved to `plane=1` chroma). + +Lead (b) is **blocked by a separate, pre-existing defect**, diagnosed to a +one-line cause in §5. Not this lane's class; not fixed here. + +**Tree.** `lane-av1lm444loss` off `a21f3680`, worktree `~/.cache/wt/av1lm444loss`. +One commit, `7b9f46fa`. Not pushed (Main merges). + +## 1. Lead (a): reproduction and the first fork + +`fixtures/ll444_128root_lossless.obu` (63429 bytes, 128x96 yuv444p, 6 frames). +Decoded-order dump vs the oracle's `EC_AV1_FINAL_DUMP`, at the base tree: + +| frame | wrong samples | first differing byte | +|---|---|---| +| 0 | 0 | — | +| 1 | 5596 | 16544 | +| 2 | 25693 | 0 | +| 3 | 28492 | 0 | +| 4 | 32289 | 0 | +| 5 | 33561 | 0 | + +`EC_SYMR` on both sides, one line per `symbol()` call, aligned on +`(value, range, symbol, post_rng)`: + +``` +index 71321 both mi=(0,0) decodetxb.c:158 (txb_skip) s=1 pre=(35131,51766) post=51766 +index 71322 ORACLE mi=(0,0) decodetxb.c:158 s=1 pre=(35131,51110) post=49256 cdf0=31697 +index 71322 OURS mi=(0,0) (luma walk) s=1 pre=(35131,51110) post=50351 icdf0=377 +``` + +Both sides read the same site and the same 2-class `txb_skip` CDF. The CDF row +content diverges (`32768 - 377 = 32391` vs the oracle's pre-adapt `32391` at +71321, `31697` at 71322), so this is the two sides being on **different units**, +not the same unit read two ways. + +`EC_COEFF_STEP tag=all_zero` names them: + +``` +[3967] ORACLE plane=0 bc=15 br=23 ctx=1 (luma, block row 23 = y 92..95) +[3968] ORACLE plane=1 bc=0 br=16 ctx=10 (chroma — the walk moved planes) +[3968] OURS side=4 ctx=1 (still luma) +``` + +That is the whole story in one line: the oracle finished the luma plane at +transform row 24 and moved to chroma; we were still reading luma. + +## 2. Mechanism: the missing frame-edge clip + +libaom bounds a block's transform grid by the part of it **inside the frame**: + +- `max_block_high` / `max_block_wide` (`av1_common_int.h:1579`, `:1565`) start + from `block_size_high/wide[bsize]` and, when `mb_to_bottom_edge` / + `mb_to_right_edge` is negative, add `>> (3 + ss)`, then `>> MI_SIZE_LOG2`. +- `mb_to_bottom_edge` is `GET_MV_SUBPEL((mi_rows - bh - mi_row) * MI_SIZE)` + (`set_mi_row_col`, `av1_common_int.h:1360`), so the clip reduces exactly to + the block's extent within the frame, in transform units. +- `decode_token_recon_block` then caps every mu chunk with + `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + row, + max_blocks_high), ss_y)` — the out-of-frame units are never read. + +Our decoder already had this exact arithmetic. `read_block_tx_size`'s **var-tx** +branch computes it, twenty lines below the lossless branch: + +```rust +let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1)); +let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0)); +``` + +and `read_var_tx_size` bails on it (`if blk_row >= max_h_mi || blk_col >= +max_w_mi { return; }`). The **lossless** branch returned a plain +`0..side/MI` square, and so did `read_block_tx_size_rect`'s lossless branch. + +**The arithmetic, exactly.** Frame 128x96. `side = 128`, so `side / MI = 32`; +`mi_rows = 2 * ceil(96/8) = 24`. `max_blocks_high = 128 + (-32 >> 3) = 96`, +`>> 2 = 24`. Clipped rows 24, columns 32. + +| | luma units / frame | chroma units / frame | total | +|---|---|---|---| +| oracle | 24 x 32 = **768** | 2 x 24 x 32 = **1536** | **2304** | +| ours, before | 32 x 32 = **1024** | 1536 | **2560** | + +The 256-unit excess is exactly the 8 unclipped transform rows x 32 columns past +the frame bottom, per inter frame. Chroma was already clipped — the +`read_inter_chroma_lossless` loop carries its own +`if cpx + ox >= u.true_width || cpy + oy >= u.true_height { continue; }` — which +is why only the LUMA side of the walk diverged. + +## 3. The fix + +Both lossless leaf builders now clip. `decode.rs`, `read_block_tx_size`: + +```rust +let side_mi = side / MI; +let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1)); +let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0)); +``` + +and `read_block_tx_size_rect` the same over `(bw, bh)`. Two hit counters ride +along: `lossless_edge_clipped()` (blocks whose grid the clip shortened) and +`lossless_edge_clip_units()` (units removed). + +**The clip is inert when the block fits** — for any block whose `at_mi + side/MI` +is inside the frame, `min` returns `side_mi` and the leaf vector is +byte-identical. That is the structural reason 4:2:0 cannot move, and the gate's +control arm pins it. + +### After + +| measurement | before | after | +|---|---|---| +| frames byte-exact vs oracle aomdec (decode order) | 1 of 6 | **6 of 6** | +| worst frame wrong samples | 33561 | **0** | +| `EC_SYMR` reads, ours | 258035 | **235940** | +| `EC_SYMR` reads, oracle | 235945 | 235945 | +| first `EC_SYMR` fork | read 71322 | **none, all 235940 paired** | + +## 4. Gate + +`stream::tests::a_lossless_block_clips_its_transform_grid_at_the_frame_edge`, +same fixture as its sibling +`a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major` (96 is not a +multiple of the 128 superblock — that is the entire reason this stream is the +witness). + +| # | assertion | value | +|---|---|---| +| 1 | `lossless_edge_clipped()` delta | **5** = one 128 root per inter frame. Reads 0 on a frame that fits, so a stream that merely decodes cannot satisfy it | +| 2 | `lossless_edge_clip_units()` delta | **1280** = 5 x 256 (8 rows x 32 cols removed per root) | +| 3 | every sample of all 6 frames vs oracle `aomdec --rawvideo`, decode order | exact | +| 4 | control arm (`ll444_minp64_128root_control`): both clip counters | **0 / 0** — the clip is a no-op where blocks fit | + +### Mutation proof (run, not asserted) + +Reverting only the two clip expressions to the unclipped grid, keeping the gate +and the counters: + +``` +=== MUT clip === +test stream::tests::a_lossless_block_clips_its_transform_grid_at_the_frame_edge ... FAILED +panicked at crates/ec-av1/src/stream.rs:6924: + the frame-edge clip fired on 0/5 of the 128 roots -- it must be 0 on a frame + that fits, so this is what makes the gate non-vacuous + left: 0 right: 5 +test result: FAILED. 0 passed; 1 failed +SUITE EXIT: 101 +``` + +Same reverted build, decoded-order dumps vs the oracle — assertion 3's failure +mode, captured: + +``` +f0: 0 wrong f3: 28492 wrong, first at byte 0 +f1: 5596 wrong, first at byte 16544 f4: 32289 wrong +f2: 25693 wrong, first at byte 0 f5: 33561 wrong +``` + +File restored from a saved copy (not `git checkout --`, per +`skill://stash-red-before-proof`), re-verified with `diff -q`, then: + +``` +=== LIVE === +test ... a_lossless_block_clips_its_transform_grid_at_the_frame_edge ... ok +test result: ok. 1 passed; 0 failed +EXIT: 0 +``` + +## 5. Lead (b): blocked by a different defect (not fixed here) + +`testsrc2 512x128 yuv444p --lossless=1 --cpu-used=2 --lag-in-frames=0 +--kf-max-dist=100 --limit=6` does **not** reach the entropy fork the ticket +describes. It dies first, on the base tree, before and after this fix: + +``` +thread 'main' panicked at crates/ec-av1/src/decode.rs:2787: +assertion `left == right` failed left: (4, 8) right: (4, 4) + 4: ::run + 5: ec_av1::decode::push_mc_rect_tx + 6: ec_av1::decode::read_inter_plane_rect + 7: ec_av1::decode::decode_intrabc_rect + 8: ec_av1::decode::decode_leaf_rect + 9: ec_av1::decode::decode_key_frame_tile_with_cdfs +``` + +(release builds wrap the assert and panic one frame later at +`decode.rs:2823`, `range end index 4 out of range for slice of length 0`). + +**Cause, measured.** A temporary env-gated probe at `decode_intrabc_rect`'s +residual split: + +``` +EC_PROBE_IBC mi=(28,106) bw=8 bh=16 lossless=true leaves=Some(8) +``` + +The luma leaves are all TX_4X4 (8 of them, 2 x 4) — the lossless branch is +correct. The `(4, 8)` comes from the **chroma** call in the same arm: +`decode_intrabc_rect` derives the chroma footprint as +`let (cw, ch) = (bw / 2, bh / 2); let (cpx, cpy) = (px / 2, py / 2);` — a +hardcoded 4:2:0 halving. At 4:4:4 that block's chroma plane block is 8x16, not +4x8, so a **lossless** 4x8 chroma rect transform reaches `TxParams::run`, whose +lossless arm requires 4x4 (the Walsh-Hadamard path). Same class the +`av1tilerows` lane landed in the lossless 16x4/4x16 chroma-pair walk: a +hardcoded 4:2:0-shaped chroma reach at 4:4:4. + +Confirming it is the whole blocker: the same stream re-encoded with +`--enable-intrabc=0` (118365 bytes) decodes on this tree **byte-exact on all six +frames** against the oracle aomdec. + +**Not fixed in this lane.** It is a different defect class from H1, in +`decode_intrabc_rect`'s chroma geometry rather than the coefficient reader or +the TX-4X4 path this lane owns, and that function carries other lanes' gates +(`a_lossless_444_intrabc_rect_leaf_walks_per_4x4_units`, +`a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`). The shape +correction is `bw >> ss_x(fctx)` / `bh >> ss_y(fctx)` on `cw`/`ch`/`cpx`/`cpy`; +dispatching it to the intrabc-rect owner is cheaper than widening this lane. + +Also measured, so nobody re-derives it: the cached `g_512x128_notile.obu` +under `~/.cache/h4/` is byte-exact on all 7 decode-order frames on this tree — +its recipe carries `--enable-rect-partitions=0`, so it never reaches the +intrabc rect route. + +## 6. Invariants — all green on this tree + +| group | result | +|---|---| +| new gate `a_lossless_block_clips_its_transform_grid_at_the_frame_edge` | 1 passed, 0 failed (mutation red, §4) | +| all `lossless` + `444` gates (`cargo test -- lossless 444`) | **24 passed / 0 failed** | +| 4:2:0 lossless + frame-edge gates (17 scoped: `frame_edge`, `partial`, `edge`, `odd`, `lossless`, non-444) | **17 passed / 0 failed** | +| sibling `a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major` | ok — its parity assertions 2/3 (4 walks, 1536 chroma units per inter frame) are unchanged and still exact | +| `cargo check -p ec-av1` | 0 warnings, 0 errors | +| source-scan guards | untouched — no `include_str!` anchor was edited; the only test hunks are in `stream.rs`, the only non-test hunks in the two leaf builders plus the two counters | + +Full-suite validation is Main's; the runs above are the scoped groups this +lane's invariant names. + +## 7. Refutation notes for the reviewer + +- The reserved 4:2:0 group-tail chroma SKIP arm and `last_intrabc` tails are + outside every hunk — the diff is the two lossless leaf builders, two counters, + two accessors, one gate. +- 4:2:0 unreachability is not the argument here and is not claimed: the clip + changes behaviour for **any** subsampling when a lossless block overhangs the + frame. The 4:2:0 argument is the `min` no-op above, pinned by the gate's + control arm and by the 17 scoped 4:2:0 gates. +- The clip reuses the var-tx branch's own variables and its own + `mi_cols`/`mi_rows` definition, so it introduces no new notion of the frame + grid. Its only approximation is that `mi_rows` is `2 * ceil(h / 8)` rather + than libaom's floor `h >> 3`; the two agree on every multiple-of-8 height, + which is the fixture's 96, and differ by at most one 4-px transform row + otherwise — the same approximation the shipped var-tx branch already carries. + +--- + +# Appendix r2 — lead (b): the blocking defect, diagnosed in full + +Written BEFORE the fix, so the diagnosis survives even if the work stops here. + +## The line + +`crates/ec-av1/src/decode.rs`, `decode_intrabc_rect`, line 13065 and line 13070 +at the base tree: + +```rust +let (cpx, cpy) = (px / 2, py / 2); // line 13065 +let (cw, ch) = (bw / 2, bh / 2); // line 13070 +``` + +Both are a **hardcoded 4:2:0 halving**. The chroma plane block's true geometry +is `bw >> ss_x(fctx)` x `bh >> ss_y(fctx)` at `(px >> ss_x(fctx), +py >> ss_y(fctx))`. At `ss (0,0)` (4:4:4) `ss_x = ss_y = 0`, so every one of +those four values is halved when it must not be. + +## Mechanism + +`decode_intrabc_rect` serves a 1:4 rect partition inside an IntraBC block. Its +chroma arm calls + +```rust +read_inter_plane_rect(dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), + cside, plane, around[plane], ...) +``` + +with the halved `(cw, ch)`. On a **lossless** block that shape reaches +`TxParams::run`, whose lossless arm is the 4x4 Walsh-Hadamard path and carries + +```rust +debug_assert_eq!((self.w, self.h), (4, 4)); +``` + +So a lossless 4:4:4 rect block whose halved chroma shape is rect (any 1:4 +strip: 8x16 -> 4x8, 4x32 -> 2x16, ...) asserts. Release builds compile the +`debug_assert` out, `dequant_and_inverse_wht4x4` returns a buffer shorter than +`w * h`, and the failure surfaces one frame later in the strided residual copy: + +``` +thread 'main' panicked at crates/ec-av1/src/decode.rs:2823:59: +range end index 4 out of range for slice of length 0 +``` + +which is the release-mode face of the same defect. + +## Evidence + +Recipe `testsrc2 512x128 yuv444p --lossless=1 --cpu-used=2 --lag-in-frames=0 +--kf-max-dist=100 --limit=6` (121844 bytes), base tree, debug build: + +``` +thread 'main' panicked at crates/ec-av1/src/decode.rs:2787:13: +assertion `left == right` failed + left: (4, 8) + right: (4, 4) + 4: ::run + 5: ec_av1::decode::push_mc_rect_tx + 6: ec_av1::decode::read_inter_plane_rect + 7: ec_av1::decode::decode_intrabc_rect + 8: ec_av1::decode::decode_leaf_rect + 9: ec_av1::decode::decode_key_frame_tile_with_cdfs +``` + +A temporary env-gated probe at `decode_intrabc_rect`'s residual split isolates +which side is wrong: + +``` +EC_PROBE_IBC mi=(28,106) bw=8 bh=16 lossless=true leaves=Some(8) +``` + +`leaves=Some(8)` with `bw=8, bh=16` is **2 x 4 = 8 TX_4X4 units** — the lossless +leaf builder is CORRECT and produced no rect luma unit at all. The `(4, 8)` in +the assert therefore cannot be luma. At 4:4:4 this block's chroma plane block is +8x16; the hardcoded halving turns it into 4x8, which is exactly the `left` +value in the assert. `ss_x = ss_y = 0`, so `(bw >> ss_x, bh >> ss_y)` is a +no-op there and `(bw / 2, bh / 2)` is a pure error. + +Confirming it is the ONLY blocker on this stream: the identical recipe re-encoded +with `--enable-intrabc=0` (118365 bytes, which never routes through +`decode_intrabc_rect`) decodes on this tree **byte-exact against the oracle +aomdec on all six frames**. So the 18779 -> 130708 damage and the 459866 vs +369136 `EC_SYMR` asymmetry the sweep recorded are behind this line, not +independent of it. + +## Class and ownership + +Same class `av1tilerows` landed in the lossless 16x4/4x16 chroma-pair walk: a +hardcoded 4:2:0-shaped chroma reach at 4:4:4. `decode_intrabc_rect` is a +different function from the one that lane fixed and carries its own gates +(`a_lossless_444_intrabc_rect_leaf_walks_per_4x4_units`, +`a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`), which is why +r1 reported rather than fixed it. Appendix r3 takes it on, on this branch, with +those gates as the blast-radius check.