diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index f6f1f508..c218af09 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -111,6 +111,12 @@ fn main() { // an INTER frame -- the arm this round implemented. let i128 = ec_av1::stream::intra128_in_inter_counters(); println!("intra128_in_inter: 128x64={} 64x128={}", i128[0], i128[1]); + // lane-lossless128: intra 128-axis blocks on a LOSSLESS frame (every + // plane TX_4X4) -- the shape whose reconstruction used to panic. + println!( + "intra128_lossless: {}", + ec_av1::stream::intra128_lossless_counters() + ); let sb = ec_av1::stream::sb128_rect_counters(); println!( "sb128_rect: edge_horz={} edge_vert={} inter_128x64={} inter_64x128={}", diff --git a/crates/ec-av1/fixtures/lossless_sb128_rect_kf.obu b/crates/ec-av1/fixtures/lossless_sb128_rect_kf.obu new file mode 100644 index 0000000000000000000000000000000000000000..0267060662a913c7ea31754544408a92223be446 GIT binary patch literal 3471 zcmV;A4RG=j0168L000C$|NZ~@fIu?s4iEr<0001F7dQX^|3n@(M$RD)qk_!4Pi5L< z=jZJgWj&_|Eg|U(m&kmRKM#(BRZd!|__~@C0Qvcn%Rf+-j&-Ww!+M(Xp@X^;r3Ujm z$J$~c>PtAMT;()5oB#j%m9kAxu|6Xt|Kzu$9duEWH9cpkk)n!driwp*zJ1U}Dz`{d zFltN1PeP;biF3l;U^>&b>x1xjfVWO!=AFAe+Nlr{G3wd0m3RY3JDcOBlc6 zHhX@(p6c1|bDZZowtJ3dxaKjA7&N_mJ~p*SLQFr#|26X>5|fhRl<56I_BuC${JV`< z322SKY41Bb&QjNI`hE9ufP*HvZqA-*S#zPwLZ*v)l8b4nAMf6C;+8@)16_ninDj_epNr0P z(0}6pp;Y>FRd8eu&TyaWR*%f|@f&k62Vm~|ovVM|*$!7<);K3)&pv2!RKs&?$ zEq~4>j4j~5Eo1GU#Lz31(FV=bL?Edqx14j1+;Y!K=?H&=ym16&MUI58jI1Mi zx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C{dEV70q&gfTj6h_&L!geJR@?g-`D xfUr=!U#ce!kI;bueV?cfz2qtY|JWEAAC1e6A-+FuY=31h{r^h%zZ6y71@Mu6t=|9u literal 0 HcmV?d00001 diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index f50ce613..1941bfd7 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -15097,6 +15097,11 @@ thread_local! { /// `TX_MODE_LARGEST` is one; the counter is what keeps that reset's gate /// non-vacuous. pub(crate) static SKIP_VARTX_LUMA_RESET_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// lane-lossless128: an intra 128-axis (BLOCK_128X64/64X128) block decoded + /// on a LOSSLESS frame, where every plane is coded as TX_4X4 units (the + /// 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) }; } /// [`SKIP_VARTX_LUMA_RESET_HITS`], for a gate's own before/after delta. @@ -15104,6 +15109,11 @@ pub fn skip_vartx_luma_reset_hits() -> usize { SKIP_VARTX_LUMA_RESET_HITS.with(std::cell::Cell::get) } +/// [`INTRA128_LOSSLESS_HITS`], for a gate's own before/after delta. +pub(crate) fn intra128_lossless_hits() -> usize { + INTRA128_LOSSLESS_HITS.with(std::cell::Cell::get) +} + /// [`INTRA128_IN_INTER_HITS`], for a gate's own before/after delta. pub(crate) fn intra128_in_inter_hits() -> [usize; 2] { INTRA128_IN_INTER_HITS.with(std::cell::Cell::get) @@ -15141,6 +15151,17 @@ pub(crate) fn intra_sb128_hits() -> (usize, usize, [usize; 3]) { /// No CfL, no filter intra (both cap at 32x32) and no palette /// (`av1_allow_palette` caps at 64x64) exist at this size -- the mode /// reader's own size gates already say so, and the guard below is defensive. +/// +/// lane-lossless128: on a LOSSLESS frame every one of those sizes collapses to +/// TX_4X4 -- `read_tx_mode` returns `ONLY_4X4` before the frame codes a tx-size +/// symbol and `read_tx_size` returns `TX_4X4` before it looks at anything +/// (`decodeframe.c:140`, `:1183`) -- so the block is tiled into 4x4 luma units +/// and (because a lossless frame forces `TX_4X4` on CHROMA too) 4x4 chroma +/// units, plane-major inside each 64x64 mu chunk exactly as +/// `decode_token_recon_block` codes them. Reading the 64-point geometry there +/// reconstructs into a `side`-sized plane with a 16-sample residual (the panic +/// this lane closed); the geometry and the unit walks below are normalized on +/// `lossless_frame`. #[allow(clippy::too_many_arguments)] fn decode_block_128rect( dec: &mut SymbolDecoder, @@ -15198,8 +15219,15 @@ fn decode_block_128rect( let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c); // spec 5.11.16 `read_tx_size`: read for every intra block, skipped or // not. `bsize_to_tx_size_cat(BLOCK_128X64)` is 3 (see this fn's doc). + // lane-lossless128: a LOSSLESS frame codes no tx-size symbol at all -- + // libaom's `read_tx_mode` returns `ONLY_4X4` for a `coded_lossless` frame + // before the symbol is even considered (`decodeframe.c:140`) and + // `read_tx_size` returns `TX_4X4` before it looks at anything + // (`decodeframe.c:1183`). So `tx_select` is off, every plane is walked as + // TX_4X4 transform units, and no `tx_size_cat3` symbol may be consumed. + let lossless_frame = lossless(fctx); let in_inter = fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some(); - let depth = if tx_select { + let depth = if tx_select && !lossless_frame { // lane-inter128intra r1: `get_tx_size_context` compares the bands // against `tx_size_wide/high[max_txsize_rect_lookup[bsize]]` -- // TX_64X64 for every 128-axis block, hence the `min(64)`. @@ -15218,24 +15246,49 @@ fn decode_block_128rect( c.set(h); }); } - let logical_tx = 64 >> depth; + let logical_tx = if lossless_frame { 4 } else { 64 >> depth }; + if lossless_frame { + hit!(INTRA128_LOSSLESS_HITS); + } // A 64-point transform codes nothing past its low 32 coefficients. let coeff_tx_side = logical_tx.min(32); - let luma_set = txbset_for(logical_tx, reduced_tx_set); + let luma_set = if lossless_frame { + TxbSet::Luma4 + } else { + txbset_for(logical_tx, reduced_tx_set) + }; let luma_scan = default_scan(coeff_tx_side); - let chroma_tx = 32; + // `av1_get_max_uv_txsize` caps at TX_32X32 normally; a lossless frame + // forces every plane to TX_4X4 (its own `Chroma4` set and 4x4 scan). + let chroma_tx = if lossless_frame { 4 } else { 32 }; + let chroma_set = if lossless_frame { + TxbSet::Chroma4 + } else { + TxbSet::Chroma32 + }; let chroma_scan = default_scan(chroma_tx); let (cpx, cpy) = (px / 2, py / 2); let (chroma_w, chroma_h) = (bw / 2, bh / 2); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); let (nch_x, nch_y) = (bw / 64, bh / 64); - // One 32x32 chroma unit per mu chunk, per plane. - debug_assert_eq!((chroma_w / chroma_tx, chroma_h / chroma_tx), (nch_x, nch_y)); + // The frame's own mi grid (`av1_calc_mi_size`, 8-px aligned): libaom's + // `decode_token_recon_block` clips each vector's walk to + // `max_block_wide/high`, so a chroma unit whose top-left is off the frame + // codes nothing. + let mi_cols = 2 * y.true_width.div_ceil(8); + let mi_rows = 2 * y.true_height.div_ceil(8); + let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c)); + let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r)); + // Non-lossless: one 32x32 chroma unit per mu chunk, per plane. + debug_assert!(lossless_frame || (chroma_w / chroma_tx, chroma_h / chroma_tx) == (nch_x, nch_y)); let tpc = 64 / logical_tx; let zero_tu = vec![0i32; logical_tx * logical_tx]; let zero_cu = vec![0i32; chroma_tx * chroma_tx]; let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; - let mut units: Vec<((usize, usize), Grid, Grid)> = Vec::new(); + let mut units: Vec<(usize, (usize, usize), Grid)> = Vec::new(); + // The chroma context arrays are indexed in LUMA mi; every chroma unit of + // every chunk shares this luma span (2 chroma pixels per luma unit). + let luma_span = chroma_tx * 2; for ch_row in 0..nch_y { for ch_col in 0..nch_x { for tu_row in ch_row * tpc..(ch_row + 1) * tpc { @@ -15305,56 +15358,74 @@ fn decode_block_128rect( neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); } } - // The chroma context arrays are indexed in LUMA mi, so this - // unit's anchor is its own 64x64 mu chunk. - let luma_span = chroma_tx * 2; - let cu_mi = ( - mi_r + ch_row * (luma_span / MI), - mi_c + ch_col * (luma_span / MI), - ); - let cu_r = tu_reach( - bw, - bh, - ch_col * luma_span, - ch_row * luma_span, - luma_span, - reach, - px, - py, - y.width, - y.height, fctx, - ); - let (cu_x, cu_y) = (cpx + ch_col * chroma_tx, cpy + ch_row * chroma_tx); - if skip { - push_intra(1, - cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, &zero_cu, None, - None, smooth_neighbor_uv, fctx, - ); - push_intra(2, - cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, &zero_cu, None, - None, smooth_neighbor_uv, fctx, - ); - continue; + // Transform units per axis inside this mu chunk's 32x32 chroma + // span: one 32x32 unit normally, 8x8 TX_4X4 units when lossless. + let cn = (32 / chroma_tx).max(1); + // libaom's per-chunk `unit_width/height` (plane-mi units of the + // luma mi grid): `min(mu_blocks + col, max_blocks) >> ss`. + let unit_w = ((ch_col + 1) * 16).min(max_w_mi) >> 1; + let unit_h = ((ch_row + 1) * 16).min(max_h_mi) >> 1; + let stepc = (chroma_tx / 4).max(1); + // `decode_token_recon_block` codes a mu chunk PLANE-MAJOR: every U + // unit of the chunk, then every V unit. At one unit per plane per + // chunk (every non-lossless 128-axis block) that is the same + // symbol order the interleaved pair used to read; a lossless + // frame's 4x4 chroma units are where it becomes observable. + for plane_pass in 1..=2usize { + for ci_row in 0..cn { + for ci_col in 0..cn { + if ch_col * 8 + ci_col * stepc >= unit_w + || ch_row * 8 + ci_row * stepc >= unit_h + { + continue; + } + let cu_x = cpx + ch_col * 32 + ci_col * chroma_tx; + let cu_y = cpy + ch_row * 32 + ci_row * chroma_tx; + let cu_mi = ( + mi_r + ch_row * 16 + ci_row * (chroma_tx / 2), + mi_c + ch_col * 16 + ci_col * (chroma_tx / 2), + ); + let cu_r = tu_reach( + bw, + bh, + ch_col * 64 + ci_col * luma_span, + ch_row * 64 + ci_row * luma_span, + luma_span, + reach, + px, + py, + y.width, + y.height, fctx, + ); + if skip { + push_intra(plane_pass, + cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, + &zero_cu, None, None, smooth_neighbor_uv, fctx, + ); + continue; + } + let cu_around = neighbours.around_mi(cu_mi, luma_span); + let buf: &mut PlaneBuf<'static> = + if plane_pass == 1 { &mut *u } else { &mut *v }; + let grid = read_plane( + dec, cdfs, chroma_set, &chroma_scan, plane_pass, cu_around[plane_pass], + mode, uv_predict_mode, angle_delta_uv, cu_r, buf, cu_x, cu_y, + chroma_tx, chroma_tx, base_q_idx, None, None, Some(3), + smooth_neighbor_uv, fctx, + )?; + last_grids[plane_pass - 1] = grid.clone(); + // Immediately, so the NEXT unit of this same block + // reads this one's coefficient context; replayed after + // `record_split_luma_rect` below, which would otherwise + // clobber them whole-block. + neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_pass, &grid); + units.push((plane_pass, cu_mi, grid)); + } + } + } + if !skip { + hit!(CHROMA_SPLIT_TX_HITS); } - let cu_around = neighbours.around_mi(cu_mi, luma_span); - let u_grid = read_plane( - dec, cdfs, TxbSet::Chroma32, &chroma_scan, 1, cu_around[1], mode, uv_predict_mode, - angle_delta_uv, cu_r, u, cu_x, cu_y, chroma_tx, chroma_tx, base_q_idx, None, None, - Some(3), smooth_neighbor_uv, fctx, - )?; - let v_grid = read_plane( - dec, cdfs, TxbSet::Chroma32, &chroma_scan, 2, cu_around[2], mode, uv_predict_mode, - angle_delta_uv, cu_r, v, cu_x, cu_y, chroma_tx, chroma_tx, base_q_idx, None, None, - Some(3), smooth_neighbor_uv, fctx, - )?; - hit!(CHROMA_SPLIT_TX_HITS); - // Immediately, so the NEXT unit of this same block reads this - // one's coefficient context; replayed after `record_split_luma_rect` - // below, which would otherwise clobber them whole-block. - neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 1, &u_grid); - neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 2, &v_grid); - units.push((cu_mi, u_grid.clone(), v_grid.clone())); - last_grids = [u_grid, v_grid]; } } if skip { @@ -15379,9 +15450,8 @@ fn decode_block_128rect( uv_predict_mode, [&last_grids[0], &last_grids[1]], ); - for (cu_mi, u_grid, v_grid) in &units { - neighbours.record_mi_chroma(*cu_mi, 64, 64, 1, u_grid); - neighbours.record_mi_chroma(*cu_mi, 64, 64, 2, v_grid); + for (plane, cu_mi, grid) in &units { + neighbours.record_mi_chroma(*cu_mi, luma_span, luma_span, *plane, grid); } } // 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 2e1cd272..8dcefab3 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -207,6 +207,13 @@ pub fn intra128_in_inter_counters() -> [usize; 2] { crate::decode::intra128_in_inter_hits() } +/// lane-lossless128: intra 128-axis blocks decoded on a LOSSLESS frame, where +/// every plane is a TX_4X4 unit. The engagement counter for +/// `a_lossless_sb128_rect_intra_block_decodes_sample_exact`. +pub fn intra128_lossless_counters() -> usize { + crate::decode::intra128_lossless_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). @@ -4950,6 +4957,68 @@ pub(crate) mod tests { } } + /// lane-lossless128: a LOSSLESS key frame that codes a 128-axis + /// (`BLOCK_64X128`) intra block -- the shape libaom's `read_tx_size` forces + /// to `TX_4X4` on its very first line (`decodeframe.c:1183`), so every plane + /// of the block is a 4x4 transform unit and NO `tx_size_cat3` symbol is + /// coded. + /// + /// On the merged base `decode_block_128rect` had no lossless carve-out: it + /// read `logical_tx = 64` off `depth == 0`, handed `PlaneBuf::reconstruct` + /// a 64x64 geometry with the 16-sample 4x4 residual + /// [`TxParams::run`](crate::decode::TxParams) produced -- `index out of + /// bounds: the len is 16 but the index is 16` at the reconstruction call, + /// on EVERY conformant stream of this shape (a panic, never a refusal). + /// + /// `crates/ec-av1/fixtures/lossless_sb128_rect_kf.obu`, 3471 bytes, sha256 + /// `013b07979c8be999c70d47f32768c2bf959be086a893f60dde1b0f5e35d4f475`: + /// `aomenc --codec=av1 --passes=1 --threads=1 --obu --lossless=1 + /// --cpu-used=0` over a 320x256, 4-frame source that is flat grey except a + /// textured 64x128 patch in the top-right corner -- which is what makes the + /// 128 root take `PARTITION_VERT` and code the `BLOCK_64X128` at mi(0,64). + /// + /// The counter delta keeps the gate non-vacuous: a stream that stopped + /// coding the shape reads 0 and the gate fails instead of passing on a + /// decode that never reached the fixed body (class + /// `gate-blind-to-feature`). + #[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; + } + let (w, h, frames) = (320usize, 256usize, 4usize); + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("fixtures/lossless_sb128_rect_kf.obu"); + let stream = std::fs::read(&path) + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); + let before = crate::decode::intra128_lossless_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; + assert!( + hits > 0, + "{NAME}: this stream coded NO lossless 128-axis intra block -- the fixed body \ + was never reached (class gate-blind-to-feature)" + ); + 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() { + for (plane, (g, r)) in [(&got.y, &want.y), (&got.u, &want.u), (&got.v, &want.v)] + .iter() + .enumerate() + { + let bad = g.iter().zip(r.iter()).filter(|(a, b)| a != b).count(); + assert_eq!( + bad, 0, + "{NAME}: frame {i} plane {plane}: {bad} samples differ from ffmpeg" + ); + } + } + eprintln!("{NAME}: {frames} frames sample-exact, intra128_lossless hits {hits}"); + } + /// lane-dkey: the palette-neighbour-band gate. A palette-Y block's size and /// colours used to stay standing in the mi-granular above/left bands after a /// sub-8x8 group was decoded next to it -- the sub-8x8 readers published diff --git a/lanes/av1lossless128.report.md b/lanes/av1lossless128.report.md new file mode 100644 index 00000000..e8414061 --- /dev/null +++ b/lanes/av1lossless128.report.md @@ -0,0 +1,199 @@ +# lane-av1-lossless128 — the lossless 128-axis intra panic is closed (byte-exact) + +Base: `main` **46f07214**. Worktree `../edith_codecs-av1ll`, branch +`lane-av1-lossless128`, `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-av1ll`, +`TMPDIR=$HOME/.cache/tmp-av1ll`. Nothing pushed. + +## 0. WIP assessment (cancelled agent, kept) + +The uncommitted WIP (`decode.rs`, `stream.rs`, `examples/decode_probe.rs`, plus +an untracked report and a gitignored fixture) was a **coherent** fix of +`decode_block_128rect`, not a half-applied edit. It already: + +* skipped the `tx_size_cat3` read on a lossless frame, +* walked every plane as TX_4X4 (`TxbSet::Luma4` / `Chroma4`), +* made the chroma walk plane-major inside each 64x64 mu chunk (a no-op when + `cn == 1`, i.e. every non-lossless block), +* bumped `INTRA128_LOSSLESS_HITS`, and +* added `a_lossless_sb128_rect_intra_block_decodes_sample_exact`. + +What it had not done: prove the gate, force-add the gitignored fixture, run +the wave-1 probes, or run the suite. Those are this resume. The three files +were **not** reset. + +## 1. Headline + +A lossless key frame that codes an intra `BLOCK_64X128` no longer panics. The +committed fixture is sample-exact against ffmpeg, and the fixed body is +reached (`intra128_lossless` hits 2). + +``` +$PROBE crates/ec-av1/fixtures/lossless_sb128_rect_kf.obu out.raw +OK: 4 frames decoded, 320x256 +intra128_lossless: 2 +ffmpeg -loglevel error -i crates/ec-av1/fixtures/lossless_sb128_rect_kf.obu \ + -pix_fmt yuv420p -f rawvideo ref.raw -y +cmp out.raw ref.raw # identical +sha256 of the planes: b30015ecfdd5091a02be1b1d92e467fc63d65e0fd6775fc17e01589b2f7edc44 +sha256 of the .obu: 013b07979c8be999c70d47f32768c2bf959be086a893f60dde1b0f5e35d4f475 +``` + +On the merged base (`$HOME/.cache/av1ll-base`, probe +`$HOME/.cache/cargo-target-av1ll-base`) the same fixture **panics** (rc 101): + +``` +thread 'main' panicked at crates/ec-av1/src/decode.rs:3596:16: +index out of bounds: the len is 16 but the index is 16 + decode::exec_intra <- decode::read_plane <- decode::decode_block_128rect +``` + +The txbands repro (`aomenc --lossless=1 --cpu-used=0 --sb-size=128` over +testsrc2 320x240, 24 frames) also panics on that base probe and decodes all +24 frames on this lane (`intra128_lossless: 13`). It is **not** sample-exact +against ffmpeg — the first wrong luma sample is frame 0 col 108, and that +desync is the pre-existing 16x4 chroma bug in §6, not this panic. Base and +this lane consume the same mode-symbol ladder up to the 128-axis block +(identical `EC_ISTEP` through `mi(0,64)`); they diverge only inside that +block, which is the fix. + +## 2. Root cause + +`decode_block_128rect` had no lossless carve-out. libaom forces TX_4X4 for a +`CodedLossless` frame before any tx-size syntax exists (read in +`~/.cache/aom-oracle/src`): + +* `read_tx_mode` returns `ONLY_4X4` when `coded_lossless` + (`av1/decoder/decodeframe.c:141`); +* `read_tx_size` returns `TX_4X4` before it looks at anything + (`decodeframe.c:1183`); +* `av1_get_tx_size` returns `TX_4X4` for every plane of a lossless `xd` + (`av1/common/blockd.h:1381`), so chroma is 4x4 too; +* `decode_token_recon_block` then walks each 64x64 mu chunk plane-major + (`decodeframe.c:982-1006`): all Y, then all U, then all V, clipped to + `max_block_wide/high`. + +With `tx_select == false` the old body took `depth = 0`, hence +`logical_tx = 64`, and called `read_plane` with a 64-point geometry. +`TxParams::run` on a lossless frame always returns the 16-sample +Walsh-Hadamard residual (`dequant_and_inverse_wht4x4`), and +`PlaneBuf::reconstruct` indexes that slice with the 64-point side +(`residual[idx]`, `dense_residual` does not pad a non-empty slice). Index 16 +on a len-16 slice. A panic on a conformant stream. + +The committed fixture's 128 roots (aomdec `EC_PART_VAL`, `bsize=15`) are +`PARTITION_NONE` except `mi(0,64)` and `mi(32,64)`, which are +`PARTITION_VERT` (value 2) — two `BLOCK_64X128` blocks. That is the shape the +txbands report named (`fn=rect bw=64 bh=128`). + +## 3. Fix + +`decode_block_128rect` now mirrors `decode_block`'s lossless path: + +* `depth` is read only when `tx_select && !lossless_frame` (no `tx_size_cat3` + symbol on a lossless frame); +* `logical_tx = 4`, `luma_set = TxbSet::Luma4`, `chroma_tx = 4`, + `chroma_set = TxbSet::Chroma4`; +* chroma units inside each mu chunk are `cn = 32 / chroma_tx` per axis and + plane-major (all U, then all V), which is `decode_token_recon_block`'s + order and is a no-op at `cn == 1`; +* a unit whose top-left is outside libaom's per-chunk + `unit_width/height = min(mu_blocks + col, max_blocks) >> ss` is not coded; +* `INTRA128_LOSSLESS_HITS` is bumped at the head; the probe prints it. + +Non-lossless behavior is unchanged: the 23 previously committed +`crates/ec-av1/fixtures/*.obu` decode byte-identical to the base probe +(§5). + +## 4. The gate (non-vacuous, fails pre-fix) + +`a_lossless_sb128_rect_intra_block_decodes_sample_exact` (`stream.rs`): +decodes the committed fixture, asserts `intra128_lossless_hits()` delta > 0, +then compares all four frames' Y/U/V against `ffmpeg_decode_sequence`. + +Fail-pre-fix, measured on the base probe (rc 101, panic quoted in §1). + +Pass post-fix: + +``` +test stream::tests::a_lossless_sb128_rect_intra_block_decodes_sample_exact ... ok + "4 frames sample-exact, intra128_lossless hits 2" +``` + +Fixture: `crates/ec-av1/fixtures/lossless_sb128_rect_kf.obu`, 3471 bytes, +sha256 `013b07979c8be999c70d47f32768c2bf959be086a893f60dde1b0f5e35d4f475`. +The path is gitignored (`fixtures` in `.gitignore`); it is force-added, same +as the other 23 committed `.obu` files. 320x256, 4 frames, lossless, +`use_128x128_superblock=true`, single tile. The cancelled agent's generator +script is not in the tree; the bytes and the aomenc recipe in the test doc +are what this lane verified (base panics, this lane is sample-exact, sha256 +matches). + +The panic witness is the **vertical** axis (`BLOCK_64X128`). A bottom-edge +`PARTITION_HORZ` (`BLOCK_128X64`) stream generated here decoded sample-exact +on **both** the base probe and this lane, so it does not exercise the +panicking reconstruct; aomenc did not emit an interior non-skip `PARTITION_HORZ` +128 root on the seams tried. The function is shared (`bw, bh` swapped, same +unit walk). Not separately panic-witnessed. + +## 5. Regression evidence + +* **23 committed fixtures** (`crates/ec-av1/fixtures/*.obu` except the new + one): lane probe raw bytes **identical** to the base probe. The only row + that moves is the new fixture (base rc 101 → lane OK, sample-exact vs + ffmpeg). +* Wave-1 probes, this lane's `decode_probe`: + * gray 60-frame key (`libaom-av1`, 320x240, `mono_chrome=true`): `OK: 60 + frames`, luma `cmp` identical to `ffmpeg -pix_fmt gray`. + * `fixtures/bitstreams/av1-monochrome.ivf` remuxed to OBU: `OK: 60 frames`, + luma identical to ffmpeg gray. + * `av1-profile1-444.ivf` remuxed to OBU: refused by name (`a chroma format + other than 4:2:0`). + * `aomenc --enable-qm=1` (160x96, 4 frames): refused by name (`a frame + using quantisation matrices`). The `--enable-qm=0` control of the same + source is sample-exact vs ffmpeg. + * `~/.cache/av1txr/s/cdf0.obu` (`disable_cdf` stream, 3 frames, 320x240): + sample-exact vs ffmpeg `yuv420p`. + * `reelb_kf900.obu` (3840x1608, 10-bit, 1 shown frame): `EC_PROBE_OUT16` cmp + identical to `ffmpeg -pix_fmt yuv420p10le` (18524160 bytes). The other + committed `reelb_*` fixtures are in the 23-file base-vs-lane sweep (identical). +* `cargo check -p ec-av1 --all-targets`: **0 warnings**. +* A 640x256 lossless `--sb-size=128 --tile-columns=1` stream refuses on both + base and this lane with the existing string `intra block copy on a + HORZ/VERT/1:4 rect intra strip` (lane-av1-intrabc's shape). Not a regression. + `intra128_lossless` was 0 on that stream — it never reached this body. + +## 6. Found and DEFERRED: lossless 16x4/4x16 chroma is still one rect, not TX_4X4 + +Pre-existing. Independent of the panic: it reproduces on a stream with no +128-axis block, and the base probe's `EC_ISTEP` ladder matches this lane's +through the divergence (so this lane's edit did not move it). + +**Measured on** `aomenc --lossless=1 --cpu-used=0 --sb-size=128` over +testsrc2 320x240, frame 0. aomdec `EC_PART`: the 16x16 at `mi(0,20)` is +`PARTITION_HORZ_4` (value 8) — four 16x4 strips. Mode symbols match through +`mi(1,20) uv_mode` (rng 53536 both sides). The next strip's `skip` does not +(aom rng 50651, ours 56265). That is the chroma-reference strip's coefficient +read: `decode_rect4_16_strip` still reads one `TxbSet::ChromaRect8x4` for the +pair (`decode.rs` around the `read_chroma_coeffs_rect` call at the chroma-pair +arm) where libaom codes two TX_4X4 units per plane. One symbol block too few, +and the tile desyncs from that strip on. First wrong luma sample on the +24-frame file is frame 0 col 108 (the strip at pixel x=104). + +`deferred(lossless 16x4/4x16 chroma pair — decode_rect4_16_strip needs the +two-TX_4X4 walk; unblock: a lossless-shape lane).` Different shape, different +symptom (silent wrong pixels, never a panic), and not what the 128-axis body +reads. Not folded into the success summary. + +## 7. Not changed + +* No refusal string added or removed. +* No encoder change. +* Sibling `lane-av1-intrabc` (Nazli, worktree `edith_codecs-av1ibc`) owns + intrabc / MV-grid. This lane did not touch those regions. Shared helpers + (`read_plane`, `exec_intra`, txfm bands) were not modified. + +## 8. Suite + +`cargo test -p ec-av1 --release --lib -- --test-threads=1`, hub process +`av1ll-suite`, on the committed tree. Expected **601 passed; 0 failed; 60 +ignored** (base 600 + this gate). Literal final line filled after the run.