From f3e2fd4c52c8b016fb0557a655aedeb91d4cf9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 25 Sep 2026 11:51:26 +0300 Subject: [PATCH] lane-av1-llband2: port 01a34bc8, 444 lossless rect-strip chroma unit span decode_rect_split's lossless chroma arm here is the same writer the reviewed sibling commit 01a34bc8 (edith_codecs-av1llband) fixed for the av1llsub8 stop at aligned step 19228 (16x8 leaf, U bc=2 br=0, txb_skip base 1 vs oracle base 0 off a polluted above-band cell). Same base tree 6cf92769, same defect site (hardcoded luma_span=8 at decode.rs:10458). Ported that fix instead of authoring a second one: unit luma span now derived from the plane subsampling (span_x=4< 0 differing bytes (sample-exact all planes, counter 160 units on the fixture); 4:2:0 lossless key + inter gates green; cargo check -p ec-av1 --all-targets 0 warnings. decode_rect4_16_strip, read_inter_rect_chroma, restoration.rs and the intra write-back untouched. Details in lanes/av1llband2.report.md. --- crates/ec-av1/examples/decode_probe.rs | 4 ++ crates/ec-av1/src/decode.rs | 45 ++++++++++--- crates/ec-av1/src/stream.rs | 77 ++++++++++++++++++++++ lanes/av1llband2.report.md | 90 ++++++++++++++++++++++++++ 4 files changed, 206 insertions(+), 10 deletions(-) create mode 100644 lanes/av1llband2.report.md diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index ab64eea2..7a27aee2 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -216,6 +216,10 @@ fn main() { ec_av1::decode::chroma_edge_tu_clip_hits(), ec_av1::decode::skip_lossless_band_reset_hits(), ); + println!( + "llband: rect_split_lossless_chroma444={}", + ec_av1::decode::rect_split_lossless_chroma444_hits() + ); // lane-t900 r12: chroma edge-filter neighbour answered from the mi-granular // uv_mode grid instead of the coarse one-slot-per-column map. println!( diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index cfe2e990..bcb8d9d9 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -5321,6 +5321,10 @@ thread_local! { /// entropy bands were zeroed per unit (libaom `av1_reset_entropy_context`, /// decodeframe.c:1262) instead of leaking the previous occupant's. static SKIP_LOSSLESS_BAND_RESET_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// lane-av1-llband: 4x4 chroma transform units a lossless HORZ/VERT strip + /// walked at subsampling 0/0 (one unit per mi cell -- the geometry the + /// hardcoded 4:2:0 span mis-stepped). + static RECT_SPLIT_LOSSLESS_CHROMA444_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// How many skipped sub-8x8 chroma blocks carried a CfL alpha (the shape @@ -5343,6 +5347,14 @@ pub fn skip_lossless_band_reset_hits() -> usize { SKIP_LOSSLESS_BAND_RESET_HITS.with(|c| c.get()) } +/// How many 4x4 chroma transform units a lossless HORZ/VERT strip walked at +/// subsampling 0/0 (lane-av1-llband). Zero means a stream never decoded the +/// 4:4:4 lossless unit walk this counter proves -- a gate asserting the fix +/// must see this nonzero. +pub fn rect_split_lossless_chroma444_hits() -> usize { + RECT_SPLIT_LOSSLESS_CHROMA444_HITS.with(|c| c.get()) +} + /// Gate-side reset for the two lane-av1-loss64 counters above. pub fn reset_loss64_hits() { CHROMA_EDGE_TU_CLIP_HITS.with(|c| c.set(0)); @@ -10403,7 +10415,7 @@ fn decode_rect_split( // them with the LAST unit's state (class `override-slot-on-one-arm`: the // next block then read `dc_sign_ctx` 1 where libaom reads 0). They are // collected here and replayed after that record. - let mut ll_chroma_units: Vec<((usize, usize), usize, Grid)> = Vec::new(); + let mut ll_chroma_units: Vec<((usize, usize), usize, Grid, usize, usize)> = Vec::new(); // The chroma_tiled arm below stamps each unit's coefficient context // immediately (so the NEXT unit reads it), then the whole-strip // `record_split_luma_rect_mi` rewrites every chroma cell with ONE @@ -10451,19 +10463,29 @@ fn decode_rect_split( let scan4 = default_scan(4); let offset = (chroma_w > 4 || chroma_h > 4).then_some(3); let (cw_n, ch_n) = (chroma_w / 4, chroma_h / 4); + // lane-av1-llband: one 4x4 chroma unit's extent in LUMA pixels -- 4 + // chroma px scaled by the plane's own subsampling. The hardcoded 8 + // was the 4:2:0 unit's span; at 4:4:4 a unit is 4x4 LUMA px (one mi + // cell), and the 8-px step made every unit past the first column/row + // read its `txb_skip` context at the WRONG mi (two cells over) and + // stamp a 2-cell span with one unit's state -- the polluted above-band + // cell behind a 16x8 leaf's U bc=2 unit of the 444 lossless key frame + // (measured: oracle `EC_DBGCTX` reads 1-cell spans at 1-mi steps; + // ours read `above[28..29]` where the oracle read `above[26]`). + let span_x = 4 << ss_x(fctx); + let span_y = 4 << ss_y(fctx); let mut last = [Grid::Zero(16), Grid::Zero(16)]; for plane_idx in 1..=2 { for cu_row in 0..ch_n { for cu_col in 0..cw_n { - let luma_span = 8; let cu_mi = ( - mi_r + cu_row * (luma_span / MI), - mi_c + cu_col * (luma_span / MI), + mi_r + cu_row * (span_y / MI), + mi_c + cu_col * (span_x / MI), ); - let cu_around = neighbours.around_mi(cu_mi, luma_span); + let cu_around = neighbours.around_mi_rect(cu_mi, span_x, span_y); let cu_reach = tu_reach( bw, bh, - cu_col * luma_span, cu_row * luma_span, luma_span, + cu_col * span_x, cu_row * span_y, span_x, block_reach, px, py, y.width, y.height, fctx, ); let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4); @@ -10485,8 +10507,11 @@ fn decode_rect_split( }), None, offset, m.smooth_neighbor_uv, fctx, )?; - neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_idx, &grid); - ll_chroma_units.push((cu_mi, plane_idx, grid.clone())); + if span_x == 4 && span_y == 4 { + hit!(RECT_SPLIT_LOSSLESS_CHROMA444_HITS); + } + neighbours.record_mi_chroma(cu_mi, span_x, span_y, plane_idx, &grid); + ll_chroma_units.push((cu_mi, plane_idx, grid.clone(), span_x, span_y)); last[plane_idx - 1] = grid; } } @@ -10658,8 +10683,8 @@ fn decode_rect_split( m.uv_predict_mode, [&u_grid, &v_grid], ); - for (cu_mi, plane, unit) in &ll_chroma_units { - neighbours.record_mi_chroma(*cu_mi, 8, 8, *plane, unit); + for (cu_mi, plane, unit, span_w, span_h) in &ll_chroma_units { + neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); } for (cu_mi, plane, unit, span_w, span_h) in &tiled_chroma_units { neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index fc5f3f9f..7a32f409 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5399,6 +5399,83 @@ pub(crate) mod tests { } } + /// lane-av1-llband: the pinned 4:4:4 LOSSLESS key frame + /// (`fixtures/ll444-lossless-key.obu`, the `ll444` recipe's frame 0 -- + /// `testsrc2 128x96` yuv444p cut to its key frame) decodes sample-exact, + /// and its HORZ/VERT strips really walked their chroma as 4x4 units at + /// subsampling 0/0 (one unit per mi cell). That unit walk used the 4:2:0 + /// span (8 luma px per 4x4 chroma unit) unconditionally, so every unit + /// past the first column/row read its `txb_skip` context two mi cells + /// over and stamped a 2-cell smear -- the 178-byte key-frame miss, first + /// divergence at the 16x8 leaf mi (16,24)'s U bc=2 unit (oracle + /// `txb_skip` base 0 off `above[26]`, ours base 1 off the polluted + /// `above[29]`). The counter assertion is the non-vacuity half (class + /// `gate-blind-to-feature`): a stream that never reaches the 4:4:4 unit + /// walk must not pass. + #[test] + fn a_real_aomenc_lossless_444_key_frame_decodes_sample_exact() { + const NAME: &str = "a_real_aomenc_lossless_444_key_frame_decodes_sample_exact"; + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + let Ok(stream) = std::fs::read(pin_dir().join("ll444-lossless-key.obu")) else { + eprintln!( + "SKIP {NAME}: no pinned bytes at {} -- regenerate with \ + `testsrc2 128x96` yuv444p 6 frames, aomenc --profile=1 --lossless=1 \ + --enable-palette=0 --enable-intrabc=0, cut to the key frame's OBUs", + pin_dir().join("ll444-lossless-key.obu").display() + ); + return; + }; + // Blindness guard: the pinned fixture must really be a lossless frame. + let mut parser = Av1Parser::new(); + let mut pos = 0usize; + let mut lossless = false; + while pos < stream.len() { + let obu = parser.parse_obu(&stream[pos..]).expect("aomenc OBU parses"); + pos += obu.total_size.max(1); + if let ObuKind::FrameHeader(h) | ObuKind::Frame(h, _) = obu.kind { + lossless = h.lossless.iter().all(|&l| l); + } + } + assert!(lossless, "{NAME} went blind: the pinned frame is not lossless"); + let before = crate::decode::rect_split_lossless_chroma444_hits(); + let ours = decode_stream(&stream).expect("a 444 lossless key-frame stream decodes"); + assert_eq!(ours.len(), 1, "{NAME}: frame count"); + assert!( + crate::decode::rect_split_lossless_chroma444_hits() > before, + "{NAME} went blind: no lossless HORZ/VERT strip walked its chroma \ + as 4x4 units at subsampling 0/0" + ); + let refs = Command::new("ffmpeg") + .args(["-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv444p", "-"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .and_then(|mut child| { + use std::io::Write as _; + child.stdin.as_mut().expect("stdin").write_all(&stream)?; + child.wait_with_output() + }) + .expect("ffmpeg 444 decode failed"); + assert!(refs.status.success(), "{}", String::from_utf8_lossy(&refs.stderr)); + let frame = &ours[0]; + let planes = [ + (&frame.y, frame.width * frame.height), + (&frame.u, frame.width * frame.height), + (&frame.v, frame.width * frame.height), + ]; + let mut off = 0usize; + for (plane, (g, len)) in planes.iter().enumerate() { + let raw = &refs.stdout[off..off + len]; + let bad = g.iter().zip(raw.iter()).filter(|&(&a, &b)| a != u16::from(b)).count(); + assert_eq!(bad, 0, "{NAME}: plane {plane}: {bad} samples differ"); + off += len; + } + } + /// 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 diff --git a/lanes/av1llband2.report.md b/lanes/av1llband2.report.md new file mode 100644 index 00000000..e0c2a8bc --- /dev/null +++ b/lanes/av1llband2.report.md @@ -0,0 +1,90 @@ +# lane-av1-llband2: 4:4:4 lossless above-band cell — sibling fix ported (r1) + +## Charter + +Resolve the `av1llsub8` stop (aligned step 19228: 16x8 leaf, U unit bc=2 +br=0, `txb_skip` base 1 vs oracle base 0 — an above-band cell). Port the +already-reviewed sibling fix if it is the same writer; never author a +second fix for the same defect. 4:2:0 lossless stays exact. Work in +`edith_codecs-av1llband2` @ 6cf92769. + +## Writer identification (done before any edit) + +Read `edith_codecs-av1llband` r1 (`lanes/av1llband.report.md`) and commit +`01a34bc8` before writing anything. That sibling took the exact same stop +(178-byte key-frame miss, first divergence at the 16x8 leaf mi (16,24)'s +U bc=2 unit) to **0 differing bytes**, and was reviewed. + +Same-writer proof: + +- This branch HEAD is the sibling's parent commit `6cf92769` byte-for-byte + (both continue `av1llsub8`). +- The defect site in THIS tree is the identical code the sibling named: + `decode_rect_split`'s lossless chroma arm hardcoding the 4:2:0 unit span + — `let luma_span = 8;` at `decode.rs:10458` here, feeding the `cu_mi` + step, `around_mi`, `tu_reach` and both `record_mi_chroma` stamps. +- The stop signature matches the sibling's reproduced defect exactly + (step 19228, 16x8 leaf, U bc=2 br=0, base 1 / row `[0][11]` vs oracle + base 0 / row `[0][10]`). + +⇒ `01a34bc8` IS that writer's fix. Ported it; no second fix invented. + +## Port (faithful, byte-identical) + +`git cherry-pick -n 01a34bc8`, dropping the sibling's lane report file +(`lanes/av1llband.report.md` — this lane writes its own). Post-images of +all three source files `cmp`-verified IDENTICAL against the sibling +worktree at `01a34bc8`: + +- `crates/ec-av1/src/decode.rs` (+45/−10): `span_x = 4 << ss_x(fctx)`, + `span_y = 4 << ss_y(fctx)` replaces the hardcoded 8 for the `cu_mi` + step, `around_mi_rect`, `tu_reach`, the immediate and replayed + `record_mi_chroma` stamps (`ll_chroma_units` carries per-unit spans); + non-vacuity counter `RECT_SPLIT_LOSSLESS_CHROMA444_HITS` + + `rect_split_lossless_chroma444_hits()`. +- `crates/ec-av1/src/stream.rs` (+77): gate + `a_real_aomenc_lossless_444_key_frame_decodes_sample_exact` over the + pinned fixture, with the lossless-frame blindness guard and the + counter-moved non-vacuity assert. +- `crates/ec-av1/examples/decode_probe.rs` (+4): `llband:` counter line. + +Off-limits files untouched: `decode_rect4_16_strip`, +`read_inter_rect_chroma`, `restoration.rs`, intra write-back. No 422 +model, no luma path, no `sub8_leaf_chroma444` (innocent: the 16x8 leaf +never reaches it). 4:2:0 arithmetic is unchanged (`4 << 1 = 8`). + +Fixture provenance: `fixtures/ll444-lossless-key.obu` (untracked, as in +the sibling), copied from the sibling worktree, sha256 +`9fc9ce1f5f6475a1d483f404b44acb90f72a80be2e5c51dedf0aeb1da9a3f5dc` +(4927 B; cut of the `ll444` recipe's key-frame OBUs: `testsrc2 128x96` +yuv444p, `aomenc --profile=1 --lossless=1 --enable-palette=0 +--enable-intrabc=0`, oracle build `~/.cache/aom-oracle`). + +## Verification (measured, this tree) + +- New gate `a_real_aomenc_lossless_444_key_frame_decodes_sample_exact`: + **green** — key frame decodes **0 differing bytes** (sample-exact, + all 3 planes vs ffmpeg `yuv444p` rawvideo, 36864 samples compared) and + the counter moved (non-vacuity). +- `decode_probe` on the pinned fixture: `llband: + rect_split_lossless_chroma444=160` (matches the sibling's 160 units), + no panic. +- 4:2:0 lossless stays green: + `a_lossless_libaom_key_frame_decodes_sample_exact`, + `a_lossless_libaom_inter_frame_decodes_sample_exact` — both ok + (3 passed, 0 failed in the gate run). +- `cargo check -p ec-av1 --all-targets`: 0 warnings, 0 errors. + +## Deferred (spoken, not hidden) + +- **1:4 strips at lossless 4:4:4** (`decode_rect4_16_strip`): same span + defect class (per the sibling's r1), but the file is owned by a live + lane and off-limits to this branch — the fix is the same span + derivation measured here. deferred(fix-now in the owning lane). +- Lossless INTER frames panic at `reconstruct_mc_rect` (decode.rs:2589): + `read_inter_rect_chroma`'s branch, untouched, as chartered. + deferred(fix-now in the owning lane). +- Intrabc rect leaves at lossless 444 still route the rect arm's + `read_coeffs_rect` path: unreachable with `--enable-intrabc=0` + fixtures. deferred(fix-now after the inter side is green, with an + intrabc 4:4:4 fixture), matching `av1llsub8`/`av1llband`.