forge
Explore
Log in

Don't you dare go Hollow

Merge lane-av1lm444loss @ 9249442f — the lossless frame-edge TX-grid clip fix + gate

Browse files at this commit

cb2465d9e46aa8a9eccb2156cb85e47eeeb4ba3cAhmet Kaan GümüşParent: 4c293dadd7df0c
- crates/ec-av1/src/decode.rs: libaom clips the transform-unit grid at the
  frame edge (max_block_high/max_block_wide fold mb_to_bottom_edge /
  mb_to_right_edge in before the shift), so decode_token_recon_block now
  bounds the square AND the rect mu walks by the part of the block inside the
  frame, with the LOSSLESS_EDGE_CLIPPED / LOSSLESS_EDGE_CLIP_UNITS counters as
  the non-vacuity evidence. Identical arithmetic for a block that fits.
- crates/ec-av1/src/stream.rs: the H1 gate.
- lanes/av1lm444loss.report.md (appendix r2 diagnoses the remaining lead (b)
  blocking defect).

No conflict: decode.rs auto-merged — the region is disjoint from
lane-av1chrtx's per-unit leaf tx_type work (~7.8k/~28.2k) and from
lane-av1distwtd's compound counter.

Tip note: the branch head 5519aaf8 is one commit past this tip
("ec-av1: ss-aware chroma geometry + lossless TX_4X4 raster in
decode_intrabc_rect") and is NOT merged here; the dispatched tip is 9249442f.
That delta is the follow-on lead (b) work the report's appendix names as
still open.
crates/ec-av1/src/decode.rsmodified+75 -6
@@ -27780,9 +27780,37 @@ fn read_block_tx_size(
2778027780 // `read_block_tx_size` (`decodeframe.c`) sends a lossless block down the
2778127781 // `read_tx_size` arm, which returns TX_4X4 before it reads anything.
2778227782 if lossless(fctx) {
27783- let mut leaves = Vec::new();
27784- for row in 0..side / MI {
27785- for col in 0..side / MI {
27783+ // lane-av1lm444loss: libaom CLIPS the transform-unit grid at the frame
27784+ // edge -- `max_block_high`/`max_block_wide` (`av1_common_int.h:1565`,
27785+ // `:1579`) fold `mb_to_bottom_edge`/`mb_to_right_edge` into
27786+ // `block_size_high/wide` before `>> MI_SIZE_LOG2`, so a block that
27787+ // hangs off the bottom/right codes only the units INSIDE the frame.
27788+ // `decode_token_recon_block`'s mu walk then bounds every chunk with
27789+ // `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + row,
27790+ // max_blocks_high), ss_y)`, so the out-of-frame units are never read.
27791+ //
27792+ // The unclipped `side / MI` grid is correct arithmetic for a block that
27793+ // FITS, which is every block of a frame whose height and width are
27794+ // multiples of the superblock -- so this reads identically on all of
27795+ // those, and only differs on the partial edge block. Measured on the
27796+ // 128x96 4:4:4 lossless pin (one 128 root per inter frame, frame 32
27797+ // mi = 24 tx rows tall): the unclipped grid read 32 rows where the
27798+ // oracle read 24 -- 256 extra luma units per inter frame (1024 vs
27799+ // the oracle's 768), the `EC_SYMR` ladder's first fork landing exactly
27800+ // at that row-24 boundary (`plane=0` luma where the oracle had moved
27801+ // to `plane=1`), and the pixel diff growing 5596 -> 33561 wrong
27802+ // samples over the six frames.
27803+ let side_mi = side / MI;
27804+ let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1));
27805+ let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0));
27806+ if max_w_mi != side_mi || max_h_mi != side_mi {
27807+ LOSSLESS_EDGE_CLIPPED.with(|c| c.set(c.get() + 1));
27808+ LOSSLESS_EDGE_CLIP_UNITS
27809+ .with(|c| c.set(c.get() + (side_mi * side_mi - max_w_mi * max_h_mi)));
27810+ }
27811+ let mut leaves = Vec::with_capacity(max_w_mi * max_h_mi);
27812+ for row in 0..max_h_mi {
27813+ for col in 0..max_w_mi {
2778627814 leaves.push((row, col, 4, 4));
2778727815 }
2778827816 }
@@ -28262,9 +28290,24 @@ fn read_block_tx_size_rect(
2826228290 fctx: &crate::decode::FrameCtx,
2826328291 ) -> Result<Option<Vec<(usize, usize, usize, usize)>>> {
2826428292 if lossless(fctx) {
28265- let mut leaves = Vec::new();
28266- for row in 0..bh / MI {
28267- for col in 0..bw / MI {
28293+ // lane-av1lm444loss: the same frame-edge clip as the square builder
28294+ // above -- `max_block_high`/`max_block_wide` bound a rect block's
28295+ // transform grid by the part of it inside the frame, so a 1:4 strip
28296+ // hanging off the bottom codes only its in-frame rows. Identical
28297+ // arithmetic for a block that fits.
28298+ let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(at_mi.1));
28299+ let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(at_mi.0));
28300+ let full_w_mi = bw / MI;
28301+ let full_h_mi = bh / MI;
28302+ if max_w_mi != full_w_mi || max_h_mi != full_h_mi {
28303+ LOSSLESS_EDGE_CLIPPED.with(|c| c.set(c.get() + 1));
28304+ LOSSLESS_EDGE_CLIP_UNITS.with(|c| {
28305+ c.set(c.get() + (full_w_mi * full_h_mi - max_w_mi * max_h_mi))
28306+ });
28307+ }
28308+ let mut leaves = Vec::with_capacity(max_w_mi * max_h_mi);
28309+ for row in 0..max_h_mi {
28310+ for col in 0..max_w_mi {
2826828311 leaves.push((row, col, 4, 4));
2826928312 }
2827028313 }
@@ -32551,6 +32594,19 @@ thread_local! {
3255132594 /// 128x96 4:4:4 lossless stream, 1536 per inter frame (768 per plane),
3255232595 /// against the oracle's 1536.
3255332596 pub(crate) static MU_CHUNK_WALK_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
32597+ /// lane-av1lm444loss: lossless blocks whose transform-unit grid was
32598+ /// CLIPPED at the frame edge -- `max_block_high`/`max_block_wide` cut the
32599+ /// grid short of the block's own `side / MI`, so libaom codes fewer TX_4X4
32600+ /// units than the block is wide/tall. Zero on any frame whose height and
32601+ /// width are multiples of the superblock, so a gate cannot look armed by
32602+ /// merely decoding.
32603+ pub(crate) static LOSSLESS_EDGE_CLIPPED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
32604+ /// lane-av1lm444loss: the transform units that clip REMOVED -- the
32605+ /// `(side / MI - clipped) * (side / MI)` overflow the pre-fix walk read
32606+ /// past the frame bottom/right edge. On the 128x96 4:4:4 lossless pin this
32607+ /// is 256 per inter frame (8 unclipped luma rows x 32 columns), and 0 on a
32608+ /// frame that fits.
32609+ pub(crate) static LOSSLESS_EDGE_CLIP_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
3255432610 }
3255532611
3255632612 /// [`MU_CHUNK_WALKS`], for a gate's own before/after delta.
@@ -32570,6 +32626,19 @@ pub(crate) fn mu_chunk_walk_units() -> usize {
3257032626 pub(crate) fn mu_chunk_order_hits() -> usize {
3257132627 MU_CHUNK_ORDER_HITS.with(std::cell::Cell::get)
3257232628 }
32629+/// [`LOSSLESS_EDGE_CLIPPED`] -- lossless blocks whose TX grid the frame-edge
32630+/// clip shortened, for a gate's own before/after delta.
32631+#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
32632+pub(crate) fn lossless_edge_clipped() -> usize {
32633+ LOSSLESS_EDGE_CLIPPED.with(std::cell::Cell::get)
32634+}
32635+
32636+/// [`LOSSLESS_EDGE_CLIP_UNITS`] -- transform units the clip removed, i.e. the
32637+/// ones the unclipped pre-fix walk read past the frame edge.
32638+#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
32639+pub(crate) fn lossless_edge_clip_units() -> usize {
32640+ LOSSLESS_EDGE_CLIP_UNITS.with(std::cell::Cell::get)
32641+}
3257332642
3257432643 /// [`CHROMA_SPLIT_TX_HITS`], for a gate's own before/after delta.
3257532644 #[allow(dead_code)] // read only from the `#[cfg(test)]` gates
crates/ec-av1/src/stream.rsmodified+122 -0
@@ -6883,6 +6883,128 @@ pub(crate) mod tests {
68836883 }
68846884 }
68856885
6886+ /// lane-av1lm444loss: a LOSSLESS block's transform-unit grid is clipped at
6887+ /// the FRAME edge, exactly as libaom's `max_block_high`/`max_block_wide`
6888+ /// clip it (`av1_common_int.h:1565`, `:1579` fold `mb_to_bottom_edge` /
6889+ /// `mb_to_right_edge` into `block_size_high/wide` before `>>
6890+ /// MI_SIZE_LOG2`), so a block hanging off the bottom/right codes only the
6891+ /// units inside the frame. `decode_token_recon_block` then bounds every
6892+ /// mu chunk with `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high
6893+ /// + row, max_blocks_high), ss_y)`, so the overflow units are never read
6894+ /// there either.
6895+ ///
6896+ /// Fixture is the sibling [`a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major`]
6897+ /// pin -- 128x96 yuv444p 4:4:4 lossless, 6 frames, 63429 bytes -- because
6898+ /// 96 is NOT a multiple of the 128 superblock: its five inter frames each
6899+ /// carry one unsplit 128 root that overhangs the bottom by 32 px. That is
6900+ /// the whole reason this stream is the witness. The frame is 24 mi tall,
6901+ /// so `max_blocks_high >> 2` is 24 transform rows where the block's own
6902+ /// `side / MI` is 32.
6903+ ///
6904+ /// The three assertions are all non-vacuous and all EXACT:
6905+ ///
6906+ /// 1. `lossless_edge_clipped()` fired -- 5, one per inter frame's 128
6907+ /// root. It is 0 on any frame whose height and width are multiples of
6908+ /// the superblock, so a stream that merely decodes cannot satisfy it.
6909+ /// 2. `lossless_edge_clip_units()` is 1280 = 5 x 256: the 8 transform rows
6910+ /// x 32 columns the unclipped walk read past the frame bottom, per
6911+ /// inter frame. The pre-fix walk read 2560 units per inter frame
6912+ /// against the oracle's 2304, and this is that 256.
6913+ /// 3. every sample of every frame matches the oracle `aomdec
6914+ /// --rawvideo` in decode order -- all six frames, hidden alt-ref
6915+ /// included, which the pre-fix tree failed from frame 1 on
6916+ /// (5596 wrong samples there, growing to 33561 by frame 5).
6917+ ///
6918+ /// The control arm pins the other direction of the invariant: the
6919+ /// `ll444_minp64_128root_control` stream has no overhanging root, so its
6920+ /// clip counters must both read 0 -- the clip cannot fire on a block that
6921+ /// fits, which is what keeps every other lossless pin byte-identical.
6922+ #[test]
6923+ fn a_lossless_block_clips_its_transform_grid_at_the_frame_edge() {
6924+ const NAME: &str = "a_lossless_block_clips_its_transform_grid_at_the_frame_edge";
6925+ const W: usize = 128;
6926+ const H: usize = 96;
6927+ const FRAMES: usize = 6;
6928+ /// 5 inter frames, one unsplit 128 root each; frame 0 splits.
6929+ const ROOTS: usize = 5;
6930+ /// `max_blocks_high >> 2` = 24 rows (96 px / 4), against the block's
6931+ /// own `side / MI` = 32 -- so 8 rows x 32 columns = 256 units per root
6932+ /// were read past the frame bottom before the clip.
6933+ const CLIP_UNITS_PER_ROOT: usize = 256;
6934+
6935+ let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
6936+ .join("fixtures/ll444_128root_lossless.obu");
6937+ let stream = read_pin(&obu, 63429, 0xf07d_47fc_fd51_2658, NAME);
6938+ let _guard = lock_gate_counters();
6939+ let (c0, u0) = (
6940+ crate::decode::lossless_edge_clipped(),
6941+ crate::decode::lossless_edge_clip_units(),
6942+ );
6943+ let frames = decode_stream(&stream)
6944+ .unwrap_or_else(|e| panic!("{NAME}: the pinned stream no longer decodes cleanly: {e}"));
6945+ let (c1, u1) = (
6946+ crate::decode::lossless_edge_clipped(),
6947+ crate::decode::lossless_edge_clip_units(),
6948+ );
6949+ assert_eq!(
6950+ c1 - c0,
6951+ ROOTS,
6952+ "{NAME}: the frame-edge clip fired on {}/{} of the 128 roots -- it must be 0 \
6953+ on a frame that fits, so this is what makes the gate non-vacuous",
6954+ c1 - c0,
6955+ ROOTS
6956+ );
6957+ assert_eq!(
6958+ u1 - u0,
6959+ ROOTS * CLIP_UNITS_PER_ROOT,
6960+ "{NAME}: the clip removed {}/{} transform units; the unclipped walk read {} \
6961+ per inter frame past the 96 px frame bottom (8 rows x 32 columns)",
6962+ u1 - u0,
6963+ ROOTS * CLIP_UNITS_PER_ROOT,
6964+ CLIP_UNITS_PER_ROOT
6965+ );
6966+ assert_eq!(frames.len(), FRAMES, "{NAME}: frame count");
6967+ for f in &frames {
6968+ assert_eq!((f.width, f.height), (W, H), "{NAME}: dimensions");
6969+ }
6970+ if aomdec_path().is_file() {
6971+ assert_rawvideo_matches(&obu, &stream, NAME, FRAMES);
6972+ } else {
6973+ eprintln!(
6974+ "SKIP {NAME} aomdec arm: no oracle aomdec at {}",
6975+ aomdec_path().display()
6976+ );
6977+ }
6978+
6979+ // Control: a stream whose roots all fit must not clip at all.
6980+ let ctrl = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
6981+ .join("fixtures/ll444_minp64_128root_control.obu");
6982+ let ctrl_stream = read_pin(
6983+ &ctrl,
6984+ 61494,
6985+ 0xf6f5_a3cb_efc7_eb10,
6986+ &format!("{NAME} control"),
6987+ );
6988+ let (c2, u2) = (
6989+ crate::decode::lossless_edge_clipped(),
6990+ crate::decode::lossless_edge_clip_units(),
6991+ );
6992+ let _ = decode_stream(&ctrl_stream)
6993+ .unwrap_or_else(|e| panic!("{NAME}: the control arm no longer decodes cleanly: {e}"));
6994+ assert_eq!(
6995+ crate::decode::lossless_edge_clipped() - c2,
6996+ 0,
6997+ "{NAME} control: the clip fired on a stream whose blocks all fit -- the clip \
6998+ must be a no-op there, which is what keeps every other lossless pin \
6999+ byte-identical"
7000+ );
7001+ assert_eq!(
7002+ crate::decode::lossless_edge_clip_units() - u2,
7003+ 0,
7004+ "{NAME} control: the clip removed units from a stream whose blocks all fit"
7005+ );
7006+ }
7007+
68867008 /// lane-av1llpredgate2: the debug-exact pin for the ss-aware
68877009 /// `obmc_skip_chroma_above` decision (lane-av1-llpred2). Fixture is the
68887010 /// llpred/llpred2 reports' stream pinned into `fixtures/`: testsrc2
lanes/av1lm444loss.report.mdadded+357 -0
@@ -0,0 +1,357 @@
1+# lane-av1lm444loss — H1: 4:4:4 LOSSLESS byte-exactness
2+
3+**Verdict.** Lead (a) **FIXED**: the pinned `ll444_128root_lossless.obu` is now
4+byte-exact on all six frames and its `EC_SYMR` ladder is bit-identical to the
5+oracle (235,940 reads, zero fork; it forked at read 71,322 before). The defect
6+was **not** in the coefficient reader — it was the transform-unit **walk**: a
7+lossless block's TX grid was never clipped at the frame edge, so the decoder
8+read 256 units per inter frame that libaom never codes, and the fork lands on a
9+PLANE change (`plane=0` luma where the oracle had moved to `plane=1` chroma).
10+
11+Lead (b) is **blocked by a separate, pre-existing defect**, diagnosed to a
12+one-line cause in §5. Not this lane's class; not fixed here.
13+
14+**Tree.** `lane-av1lm444loss` off `a21f3680`, worktree `~/.cache/wt/av1lm444loss`.
15+One commit, `7b9f46fa`. Not pushed (Main merges).
16+
17+## 1. Lead (a): reproduction and the first fork
18+
19+`fixtures/ll444_128root_lossless.obu` (63429 bytes, 128x96 yuv444p, 6 frames).
20+Decoded-order dump vs the oracle's `EC_AV1_FINAL_DUMP`, at the base tree:
21+
22+| frame | wrong samples | first differing byte |
23+|---|---|---|
24+| 0 | 0 | — |
25+| 1 | 5596 | 16544 |
26+| 2 | 25693 | 0 |
27+| 3 | 28492 | 0 |
28+| 4 | 32289 | 0 |
29+| 5 | 33561 | 0 |
30+
31+`EC_SYMR` on both sides, one line per `symbol()` call, aligned on
32+`(value, range, symbol, post_rng)`:
33+
34+```
35+index 71321 both mi=(0,0) decodetxb.c:158 (txb_skip) s=1 pre=(35131,51766) post=51766
36+index 71322 ORACLE mi=(0,0) decodetxb.c:158 s=1 pre=(35131,51110) post=49256 cdf0=31697
37+index 71322 OURS mi=(0,0) (luma walk) s=1 pre=(35131,51110) post=50351 icdf0=377
38+```
39+
40+Both sides read the same site and the same 2-class `txb_skip` CDF. The CDF row
41+content diverges (`32768 - 377 = 32391` vs the oracle's pre-adapt `32391` at
42+71321, `31697` at 71322), so this is the two sides being on **different units**,
43+not the same unit read two ways.
44+
45+`EC_COEFF_STEP tag=all_zero` names them:
46+
47+```
48+[3967] ORACLE plane=0 bc=15 br=23 ctx=1 (luma, block row 23 = y 92..95)
49+[3968] ORACLE plane=1 bc=0 br=16 ctx=10 (chroma — the walk moved planes)
50+[3968] OURS side=4 ctx=1 (still luma)
51+```
52+
53+That is the whole story in one line: the oracle finished the luma plane at
54+transform row 24 and moved to chroma; we were still reading luma.
55+
56+## 2. Mechanism: the missing frame-edge clip
57+
58+libaom bounds a block's transform grid by the part of it **inside the frame**:
59+
60+- `max_block_high` / `max_block_wide` (`av1_common_int.h:1579`, `:1565`) start
61+ from `block_size_high/wide[bsize]` and, when `mb_to_bottom_edge` /
62+ `mb_to_right_edge` is negative, add `>> (3 + ss)`, then `>> MI_SIZE_LOG2`.
63+- `mb_to_bottom_edge` is `GET_MV_SUBPEL((mi_rows - bh - mi_row) * MI_SIZE)`
64+ (`set_mi_row_col`, `av1_common_int.h:1360`), so the clip reduces exactly to
65+ the block's extent within the frame, in transform units.
66+- `decode_token_recon_block` then caps every mu chunk with
67+ `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + row,
68+ max_blocks_high), ss_y)` — the out-of-frame units are never read.
69+
70+Our decoder already had this exact arithmetic. `read_block_tx_size`'s **var-tx**
71+branch computes it, twenty lines below the lossless branch:
72+
73+```rust
74+let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1));
75+let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0));
76+```
77+
78+and `read_var_tx_size` bails on it (`if blk_row >= max_h_mi || blk_col >=
79+max_w_mi { return; }`). The **lossless** branch returned a plain
80+`0..side/MI` square, and so did `read_block_tx_size_rect`'s lossless branch.
81+
82+**The arithmetic, exactly.** Frame 128x96. `side = 128`, so `side / MI = 32`;
83+`mi_rows = 2 * ceil(96/8) = 24`. `max_blocks_high = 128 + (-32 >> 3) = 96`,
84+`>> 2 = 24`. Clipped rows 24, columns 32.
85+
86+| | luma units / frame | chroma units / frame | total |
87+|---|---|---|---|
88+| oracle | 24 x 32 = **768** | 2 x 24 x 32 = **1536** | **2304** |
89+| ours, before | 32 x 32 = **1024** | 1536 | **2560** |
90+
91+The 256-unit excess is exactly the 8 unclipped transform rows x 32 columns past
92+the frame bottom, per inter frame. Chroma was already clipped — the
93+`read_inter_chroma_lossless` loop carries its own
94+`if cpx + ox >= u.true_width || cpy + oy >= u.true_height { continue; }` — which
95+is why only the LUMA side of the walk diverged.
96+
97+## 3. The fix
98+
99+Both lossless leaf builders now clip. `decode.rs`, `read_block_tx_size`:
100+
101+```rust
102+let side_mi = side / MI;
103+let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1));
104+let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0));
105+```
106+
107+and `read_block_tx_size_rect` the same over `(bw, bh)`. Two hit counters ride
108+along: `lossless_edge_clipped()` (blocks whose grid the clip shortened) and
109+`lossless_edge_clip_units()` (units removed).
110+
111+**The clip is inert when the block fits** — for any block whose `at_mi + side/MI`
112+is inside the frame, `min` returns `side_mi` and the leaf vector is
113+byte-identical. That is the structural reason 4:2:0 cannot move, and the gate's
114+control arm pins it.
115+
116+### After
117+
118+| measurement | before | after |
119+|---|---|---|
120+| frames byte-exact vs oracle aomdec (decode order) | 1 of 6 | **6 of 6** |
121+| worst frame wrong samples | 33561 | **0** |
122+| `EC_SYMR` reads, ours | 258035 | **235940** |
123+| `EC_SYMR` reads, oracle | 235945 | 235945 |
124+| first `EC_SYMR` fork | read 71322 | **none, all 235940 paired** |
125+
126+## 4. Gate
127+
128+`stream::tests::a_lossless_block_clips_its_transform_grid_at_the_frame_edge`,
129+same fixture as its sibling
130+`a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major` (96 is not a
131+multiple of the 128 superblock — that is the entire reason this stream is the
132+witness).
133+
134+| # | assertion | value |
135+|---|---|---|
136+| 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 |
137+| 2 | `lossless_edge_clip_units()` delta | **1280** = 5 x 256 (8 rows x 32 cols removed per root) |
138+| 3 | every sample of all 6 frames vs oracle `aomdec --rawvideo`, decode order | exact |
139+| 4 | control arm (`ll444_minp64_128root_control`): both clip counters | **0 / 0** — the clip is a no-op where blocks fit |
140+
141+### Mutation proof (run, not asserted)
142+
143+Reverting only the two clip expressions to the unclipped grid, keeping the gate
144+and the counters:
145+
146+```
147+=== MUT clip ===
148+test stream::tests::a_lossless_block_clips_its_transform_grid_at_the_frame_edge ... FAILED
149+panicked at crates/ec-av1/src/stream.rs:6924:
150+ the frame-edge clip fired on 0/5 of the 128 roots -- it must be 0 on a frame
151+ that fits, so this is what makes the gate non-vacuous
152+ left: 0 right: 5
153+test result: FAILED. 0 passed; 1 failed
154+SUITE EXIT: 101
155+```
156+
157+Same reverted build, decoded-order dumps vs the oracle — assertion 3's failure
158+mode, captured:
159+
160+```
161+f0: 0 wrong f3: 28492 wrong, first at byte 0
162+f1: 5596 wrong, first at byte 16544 f4: 32289 wrong
163+f2: 25693 wrong, first at byte 0 f5: 33561 wrong
164+```
165+
166+File restored from a saved copy (not `git checkout --`, per
167+`skill://stash-red-before-proof`), re-verified with `diff -q`, then:
168+
169+```
170+=== LIVE ===
171+test ... a_lossless_block_clips_its_transform_grid_at_the_frame_edge ... ok
172+test result: ok. 1 passed; 0 failed
173+EXIT: 0
174+```
175+
176+## 5. Lead (b): blocked by a different defect (not fixed here)
177+
178+`testsrc2 512x128 yuv444p --lossless=1 --cpu-used=2 --lag-in-frames=0
179+--kf-max-dist=100 --limit=6` does **not** reach the entropy fork the ticket
180+describes. It dies first, on the base tree, before and after this fix:
181+
182+```
183+thread 'main' panicked at crates/ec-av1/src/decode.rs:2787:
184+assertion `left == right` failed left: (4, 8) right: (4, 4)
185+ 4: <ec_av1::decode::TxParams>::run
186+ 5: ec_av1::decode::push_mc_rect_tx
187+ 6: ec_av1::decode::read_inter_plane_rect
188+ 7: ec_av1::decode::decode_intrabc_rect
189+ 8: ec_av1::decode::decode_leaf_rect
190+ 9: ec_av1::decode::decode_key_frame_tile_with_cdfs
191+```
192+
193+(release builds wrap the assert and panic one frame later at
194+`decode.rs:2823`, `range end index 4 out of range for slice of length 0`).
195+
196+**Cause, measured.** A temporary env-gated probe at `decode_intrabc_rect`'s
197+residual split:
198+
199+```
200+EC_PROBE_IBC mi=(28,106) bw=8 bh=16 lossless=true leaves=Some(8)
201+```
202+
203+The luma leaves are all TX_4X4 (8 of them, 2 x 4) — the lossless branch is
204+correct. The `(4, 8)` comes from the **chroma** call in the same arm:
205+`decode_intrabc_rect` derives the chroma footprint as
206+`let (cw, ch) = (bw / 2, bh / 2); let (cpx, cpy) = (px / 2, py / 2);` — a
207+hardcoded 4:2:0 halving. At 4:4:4 that block's chroma plane block is 8x16, not
208+4x8, so a **lossless** 4x8 chroma rect transform reaches `TxParams::run`, whose
209+lossless arm requires 4x4 (the Walsh-Hadamard path). Same class the
210+`av1tilerows` lane landed in the lossless 16x4/4x16 chroma-pair walk: a
211+hardcoded 4:2:0-shaped chroma reach at 4:4:4.
212+
213+Confirming it is the whole blocker: the same stream re-encoded with
214+`--enable-intrabc=0` (118365 bytes) decodes on this tree **byte-exact on all six
215+frames** against the oracle aomdec.
216+
217+**Not fixed in this lane.** It is a different defect class from H1, in
218+`decode_intrabc_rect`'s chroma geometry rather than the coefficient reader or
219+the TX-4X4 path this lane owns, and that function carries other lanes' gates
220+(`a_lossless_444_intrabc_rect_leaf_walks_per_4x4_units`,
221+`a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`). The shape
222+correction is `bw >> ss_x(fctx)` / `bh >> ss_y(fctx)` on `cw`/`ch`/`cpx`/`cpy`;
223+dispatching it to the intrabc-rect owner is cheaper than widening this lane.
224+
225+Also measured, so nobody re-derives it: the cached `g_512x128_notile.obu`
226+under `~/.cache/h4/` is byte-exact on all 7 decode-order frames on this tree —
227+its recipe carries `--enable-rect-partitions=0`, so it never reaches the
228+intrabc rect route.
229+
230+## 6. Invariants — all green on this tree
231+
232+| group | result |
233+|---|---|
234+| new gate `a_lossless_block_clips_its_transform_grid_at_the_frame_edge` | 1 passed, 0 failed (mutation red, §4) |
235+| all `lossless` + `444` gates (`cargo test -- lossless 444`) | **24 passed / 0 failed** |
236+| 4:2:0 lossless + frame-edge gates (17 scoped: `frame_edge`, `partial`, `edge`, `odd`, `lossless`, non-444) | **17 passed / 0 failed** |
237+| 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 |
238+| `cargo check -p ec-av1` | 0 warnings, 0 errors |
239+| 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 |
240+
241+Full-suite validation is Main's; the runs above are the scoped groups this
242+lane's invariant names.
243+
244+## 7. Refutation notes for the reviewer
245+
246+- The reserved 4:2:0 group-tail chroma SKIP arm and `last_intrabc` tails are
247+ outside every hunk — the diff is the two lossless leaf builders, two counters,
248+ two accessors, one gate.
249+- 4:2:0 unreachability is not the argument here and is not claimed: the clip
250+ changes behaviour for **any** subsampling when a lossless block overhangs the
251+ frame. The 4:2:0 argument is the `min` no-op above, pinned by the gate's
252+ control arm and by the 17 scoped 4:2:0 gates.
253+- The clip reuses the var-tx branch's own variables and its own
254+ `mi_cols`/`mi_rows` definition, so it introduces no new notion of the frame
255+ grid. Its only approximation is that `mi_rows` is `2 * ceil(h / 8)` rather
256+ than libaom's floor `h >> 3`; the two agree on every multiple-of-8 height,
257+ which is the fixture's 96, and differ by at most one 4-px transform row
258+ otherwise — the same approximation the shipped var-tx branch already carries.
259+
260+---
261+
262+# Appendix r2 — lead (b): the blocking defect, diagnosed in full
263+
264+Written BEFORE the fix, so the diagnosis survives even if the work stops here.
265+
266+## The line
267+
268+`crates/ec-av1/src/decode.rs`, `decode_intrabc_rect`, line 13065 and line 13070
269+at the base tree:
270+
271+```rust
272+let (cpx, cpy) = (px / 2, py / 2); // line 13065
273+let (cw, ch) = (bw / 2, bh / 2); // line 13070
274+```
275+
276+Both are a **hardcoded 4:2:0 halving**. The chroma plane block's true geometry
277+is `bw >> ss_x(fctx)` x `bh >> ss_y(fctx)` at `(px >> ss_x(fctx),
278+py >> ss_y(fctx))`. At `ss (0,0)` (4:4:4) `ss_x = ss_y = 0`, so every one of
279+those four values is halved when it must not be.
280+
281+## Mechanism
282+
283+`decode_intrabc_rect` serves a 1:4 rect partition inside an IntraBC block. Its
284+chroma arm calls
285+
286+```rust
287+read_inter_plane_rect(dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch),
288+ cside, plane, around[plane], ...)
289+```
290+
291+with the halved `(cw, ch)`. On a **lossless** block that shape reaches
292+`TxParams::run`, whose lossless arm is the 4x4 Walsh-Hadamard path and carries
293+
294+```rust
295+debug_assert_eq!((self.w, self.h), (4, 4));
296+```
297+
298+So a lossless 4:4:4 rect block whose halved chroma shape is rect (any 1:4
299+strip: 8x16 -> 4x8, 4x32 -> 2x16, ...) asserts. Release builds compile the
300+`debug_assert` out, `dequant_and_inverse_wht4x4` returns a buffer shorter than
301+`w * h`, and the failure surfaces one frame later in the strided residual copy:
302+
303+```
304+thread 'main' panicked at crates/ec-av1/src/decode.rs:2823:59:
305+range end index 4 out of range for slice of length 0
306+```
307+
308+which is the release-mode face of the same defect.
309+
310+## Evidence
311+
312+Recipe `testsrc2 512x128 yuv444p --lossless=1 --cpu-used=2 --lag-in-frames=0
313+--kf-max-dist=100 --limit=6` (121844 bytes), base tree, debug build:
314+
315+```
316+thread 'main' panicked at crates/ec-av1/src/decode.rs:2787:13:
317+assertion `left == right` failed
318+ left: (4, 8)
319+ right: (4, 4)
320+ 4: <ec_av1::decode::TxParams>::run
321+ 5: ec_av1::decode::push_mc_rect_tx
322+ 6: ec_av1::decode::read_inter_plane_rect
323+ 7: ec_av1::decode::decode_intrabc_rect
324+ 8: ec_av1::decode::decode_leaf_rect
325+ 9: ec_av1::decode::decode_key_frame_tile_with_cdfs
326+```
327+
328+A temporary env-gated probe at `decode_intrabc_rect`'s residual split isolates
329+which side is wrong:
330+
331+```
332+EC_PROBE_IBC mi=(28,106) bw=8 bh=16 lossless=true leaves=Some(8)
333+```
334+
335+`leaves=Some(8)` with `bw=8, bh=16` is **2 x 4 = 8 TX_4X4 units** — the lossless
336+leaf builder is CORRECT and produced no rect luma unit at all. The `(4, 8)` in
337+the assert therefore cannot be luma. At 4:4:4 this block's chroma plane block is
338+8x16; the hardcoded halving turns it into 4x8, which is exactly the `left`
339+value in the assert. `ss_x = ss_y = 0`, so `(bw >> ss_x, bh >> ss_y)` is a
340+no-op there and `(bw / 2, bh / 2)` is a pure error.
341+
342+Confirming it is the ONLY blocker on this stream: the identical recipe re-encoded
343+with `--enable-intrabc=0` (118365 bytes, which never routes through
344+`decode_intrabc_rect`) decodes on this tree **byte-exact against the oracle
345+aomdec on all six frames**. So the 18779 -> 130708 damage and the 459866 vs
346+369136 `EC_SYMR` asymmetry the sweep recorded are behind this line, not
347+independent of it.
348+
349+## Class and ownership
350+
351+Same class `av1tilerows` landed in the lossless 16x4/4x16 chroma-pair walk: a
352+hardcoded 4:2:0-shaped chroma reach at 4:4:4. `decode_intrabc_rect` is a
353+different function from the one that lane fixed and carries its own gates
354+(`a_lossless_444_intrabc_rect_leaf_walks_per_4x4_units`,
355+`a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`), which is why
356+r1 reported rather than fixed it. Appendix r3 takes it on, on this branch, with
357+those gates as the blast-radius check.