From 226d2c8fdfd5227e8075f47ef35034ef33f26888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 23 Sep 2026 08:35:23 +0300 Subject: [PATCH] ec-av1: rect intrabc r5 -- skipped-strip residual stride is side, not bw*bh The skip arm of decode_intrabc_rect handed push_mc_rect a ZERO_RESIDUAL[..bw*bh] (chroma: cw*ch) slice while reconstruct_mc_rect indexes residual[row*side .. row*side+w] for row in 0..h, needing up to (h-1)*side+w elements. On a skipped VERT 2:1 strip (bh>bw) that overruns the slice: an 8x32 strip panics at row 8 ('range end index 264 out of range for slice of length 256'). Pass side*side / cside*cside, matching the proven inter skip arm. Invisible to every gate because they only counted CODED strips and the wavefront path's Residual::done pointer-compare maps the slice to the empty marker: add INTRABC_RECT_SKIPPED_HITS ([horz, vert]) and two skipped-VERT arms to the unblock gate (testsrc2 512x384 cq30, palette off AND on), each asserted byte-exact vs ffmpeg at 1 AND 4 recon threads. Fail-pre-fix proven: same panic index (264 > 256). --- crates/ec-av1/src/decode.rs | 36 +++++++++-- crates/ec-av1/src/stream.rs | 118 ++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 6 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 8da96577..169aa5f6 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3734,6 +3734,13 @@ thread_local! { /// to an all-skip stream (no residual reader at all) would stay green. static INTRABC_RECT_CODED_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0, 0]) }; + /// lane-av1-intrabc r5: the `skip == 1` twin of [`INTRABC_RECT_CODED_HITS`] + /// -- rect INTRABC blocks reconstructed as a bare prediction copy, split + /// by orientation. The r5 panic (residual stride `bw * bh` underrunning + /// `reconstruct_mc_rect`'s `row * side + w` index on a VERT strip) was + /// invisible to every gate because they only counted CODED strips. + static INTRABC_RECT_SKIPPED_HITS: std::cell::Cell<[usize; 2]> = + const { std::cell::Cell::new([0, 0]) }; } /// lane-av1-intrabc r4: coded (`skip == 0`) rect intrabc blocks reconstructed, @@ -3743,6 +3750,13 @@ pub fn intrabc_rect_coded_hits() -> [usize; 2] { INTRABC_RECT_CODED_HITS.with(std::cell::Cell::get) } +/// lane-av1-intrabc r5: skipped (`skip == 1`) rect intrabc blocks +/// reconstructed, as `[horz, vert]` -- see [`INTRABC_RECT_SKIPPED_HITS`]. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub fn intrabc_rect_skipped_hits() -> [usize; 2] { + INTRABC_RECT_SKIPPED_HITS.with(std::cell::Cell::get) +} + /// Current value of [`INTRABC_RECT_HITS`]. #[allow(dead_code)] // read only from the `#[cfg(test)]` gates pub fn intrabc_rect_hits() -> usize { @@ -3755,6 +3769,7 @@ pub(crate) fn reset_intrabc_rect_hits() { INTRABC_RECT_HITS.with(|c| c.set(0)); INTRABC_RECT_VARTX_HITS.with(|c| c.set(0)); INTRABC_RECT_CODED_HITS.with(|c| c.set([0, 0])); + INTRABC_RECT_SKIPPED_HITS.with(|c| c.set([0, 0])); } /// Current value of [`INTRABC_RECT_VARTX_HITS`]. @@ -10962,9 +10977,15 @@ fn decode_intrabc_rect( fctx: &crate::decode::FrameCtx, ) -> Result<()> { hit!(INTRABC_RECT_HITS); - if !skip { - // [horz, vert]: see `INTRABC_RECT_CODED_HITS`. - let idx = usize::from(bh > bw); + // [horz, vert]: see `INTRABC_RECT_CODED_HITS` / `INTRABC_RECT_SKIPPED_HITS`. + let idx = usize::from(bh > bw); + if skip { + INTRABC_RECT_SKIPPED_HITS.with(|c| { + let mut seen = c.get(); + seen[idx] += 1; + c.set(seen); + }); + } else { INTRABC_RECT_CODED_HITS.with(|c| { let mut seen = c.get(); seen[idx] += 1; @@ -11044,9 +11065,12 @@ fn decode_intrabc_rect( let (luma_grid, u_grid, v_grid); if skip { // No residual syntax at all: the prediction IS the block. - push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..bw * bh], fctx); - push_mc_rect(1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cw * ch], fctx); - push_mc_rect(2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cw * ch], fctx); + // Residual stride is `side` (and `cside`), not `bw`: reconstruct_mc_rect indexes + // `residual[row * side .. row*side + w]` for row in 0..h, so a skipped VERT strip + // (bh > bw) overruns a `bw * bh` slice. Bug-for-bug with the inter skip arm. + push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * side], fctx); + push_mc_rect(1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * cside], fctx); + push_mc_rect(2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * cside], fctx); luma_tx_type = TxType::DctDct; luma_grid = Grid::Zero(side * side); u_grid = Grid::Zero(cside * cside); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index b0adfba2..366ac146 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -6634,6 +6634,124 @@ pub(crate) mod tests { coded_total[1] ); assert!(frames_compared > 0, "{NAME}: no frame was compared pixel-exact"); + + // lane-av1-intrabc r5: the SKIPPED VERT 2:1 strip -- the arm the + // reviewer's sweep panicked on (a skipped 8x32 strip passed a + // `bw * bh`-sized ZERO_RESIDUAL slice at stride `side`, and + // `reconstruct_mc_rect` indexed `row * side + w` past its end). + // Skip-heavy recipes (low cq, palette off AND on), each decoded + // byte-exact vs ffmpeg on BOTH the default single-threaded path + // (`EC_AV1_RECON_THREADS == 1`, the inline ReconPlanes arm that + // panicked pre-fix) AND the wavefront path. The skipped-orientation + // counter is asserted per arm (`gate-blind-to-feature`): a recipe + // that stops producing a skipped VERT strip fails instead of passing + // vacuously. `lock_gate_counters` because `set_recon_threads` is a + // process-wide knob. + let _par_lock = lock_gate_counters(); + let skip_arms: [(&str, &str, usize, usize, &[&str]); 2] = [ + ("vert-skip-512-cq30-pal0-txs0", TS512, 512, 384, &["--cq-level=30", "--enable-palette=0", "--enable-tx-size-search=0"]), + ("vert-skip-512-cq30-pal1-txs0", TS512, 512, 384, &["--cq-level=30", "--enable-palette=1", "--enable-tx-size-search=0"]), + ]; + for (arm, src, width, height, extra) in &skip_arms { + let y4m = { + let out = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", *src, "-t", "0.2", "-pix_fmt", + "yuv420p", "-strict", "-1", "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!(out.status.success(), "{NAME}: ffmpeg failed for {arm}"); + out.stdout + }; + let mut child = Command::new(aomenc_path()) + .args([ + "--codec=av1", "--bit-depth=8", "--input-bit-depth=8", "--passes=1", + "--end-usage=q", "--cpu-used=0", "--lag-in-frames=0", "--kf-max-dist=1", + "--limit=1", "--threads=1", "--tile-columns=0", + "--enable-rect-partitions=1", "--enable-1to4-partitions=1", + "--min-partition-size=8", "--max-partition-size=32", "--sb-size=64", + "--tune-content=screen", "--enable-intrabc=1", + ]) + // aomenc keeps the LAST occurrence of a repeated flag. + .args(*extra) + .args(["--obu", "-o", "-", "-"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m) + .or_else(|e| match e.kind() { + std::io::ErrorKind::BrokenPipe => Ok(()), + _ => Err(e), + }) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "{NAME}: aomenc refused {arm}: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + // Default single-threaded path first (the arm that panicked + // pre-fix), then the wavefront path; both must match ffmpeg. + let mut skipped_vert = 0usize; + for threads in [1usize, 4] { + crate::par::set_recon_threads(threads); + crate::decode::reset_intrabc_rect_hits(); + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => { + crate::par::set_recon_threads(1); + panic!("{NAME}: {arm} refused at {threads} recon thread(s): {e}") + } + }; + let skipped = crate::decode::intrabc_rect_skipped_hits(); + assert!( + !frames.is_empty(), + "{NAME}: {arm} decoded no frame at {threads} recon thread(s)" + ); + let theirs = ffmpeg_decode_sequence(&stream, *width, *height, frames.len()); + assert_eq!( + frames.len(), + theirs.len(), + "{NAME}: {arm} frame count at {threads} recon thread(s)" + ); + for (i, (ours, ref_frame)) in frames.iter().zip(theirs.iter()).enumerate() { + assert!( + ours.y == ref_frame.y && ours.u == ref_frame.u && ours.v == ref_frame.v, + "{NAME}: {arm} frame {i} does not match ffmpeg at {threads} recon thread(s)" + ); + } + if threads == 1 { + skipped_vert = skipped[1]; + } else { + assert_eq!( + skipped[1], skipped_vert, + "{NAME}: {arm} skipped VERT strip count changed across recon paths" + ); + } + eprintln!( + "{NAME}: {arm} {} frame(s) exact at {threads} recon thread(s), skipped rect intrabc horz={} vert={}", + frames.len(), + skipped[0], + skipped[1] + ); + } + crate::par::set_recon_threads(1); + assert!( + skipped_vert > 0, + "{NAME}: {arm} reconstructed no SKIPPED VERT rect intrabc strip -- the r5 stride arm is untested" + ); + } } /// lane-t900 r31: builds one `--tune-content=screen --enable-intrabc=1`