From 0bdf223f5e9a3e79992ec0f8fc137e3ae7b06403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 09:48:09 +0300 Subject: [PATCH] fix(av1): rect intra strips never read use_intrabc -- every screen-content key frame desynced at its first symbol av1_allow_intrabc is a FRAME-level test (libaom decodemv.c:892, read_intra_frame_mode_info): on an intra frame whose header sets allow_intrabc, every intra block reads a use_intrabc symbol right after skip/cdef/delta_q and before mbmi->mode. There is no block-shape term, but read_intra_mode_rect -- the reader for every HORZ/VERT/1:4 rect strip -- never read it, so the arithmetic decoder desynced at the FIRST mode symbol of the first rect block of any --tune-content=screen key frame. Measured on an 8-bit 256x192 aomenc key frame (cq59, cpu-used 0, rect + 1:4 partitions, tune-content=screen): the decode reported OK and all 49152 luma samples differed from ffmpeg's. Cross-decoder range ladder: entry rng 32874 identical, our y-mode read gave 62696, the oracle 58628 -- exactly the value obtained by consuming one default intrabc symbol (rng 30694, normalised 61388) before the kf_y_mode read. The flag is now consumed in read_intra_mode_rect, taking allow_intrabc from the per-frame INTRABC_MI_GRID (Some only when the header set it), so no new plumbing. Reconstruction of an intrabc block at a rect shape is not ported, so a firing symbol refuses by name (inventory 45 -> 46). Gate: a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips (3 real-aomenc 8-bit arms) -- 147 rect-strip use_intrabc symbols read, 1 named refusal, 1 frame compared pixel-exact. --- crates/ec-av1/examples/decode_probe.rs | 1 + crates/ec-av1/src/decode.rs | 45 ++++++ crates/ec-av1/src/refusal_inventory.rs | 1 + crates/ec-av1/src/stream.rs | 194 +++++++++++++++++++++++++ 4 files changed, 241 insertions(+) diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index 1a4e1417..57b53f21 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -33,6 +33,7 @@ fn main() { println!("reelc_chroma: skip_cfl={sc} dir_1to4_pairs={dp}"); let (h, v, c) = ec_av1::stream::rect4_32_counters(); println!("rect4_32: horz={h} vert={v} coded={c}"); + println!("rect_intrabc_reads: {}", ec_av1::stream::rect_intrabc_reads()); let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); // lane-kf900 r1: the counter the skipped-8x8-split-transform gate diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 3975b3cf..16e092fe 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1571,6 +1571,30 @@ thread_local! { static INTRABC_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } +/// Whether this frame's header set `allow_intrabc` -- [`INTRABC_MI_GRID`] is +/// `Some` exactly then (set once per frame in `decode_frame`), so the flag +/// needs no separate plumbing to reach the rect-strip mode reader. +fn allow_intrabc_frame() -> bool { + INTRABC_MI_GRID.with(|g| g.borrow().is_some()) +} + +thread_local! { + /// How many rect intra strips READ the `use_intrabc` symbol (i.e. how many + /// times the fix below was exercised) -- the rect-strip gate's hit counter, + /// distinct from [`INTRABC_HITS`] which counts blocks that USE intrabc. + static RECT_INTRABC_READS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`RECT_INTRABC_READS`]. +pub(crate) fn rect_intrabc_reads() -> usize { + RECT_INTRABC_READS.with(|c| c.get()) +} + +/// Zeroes [`RECT_INTRABC_READS`] (gate tests call this before a decode). +pub(crate) fn reset_rect_intrabc_reads() { + RECT_INTRABC_READS.with(|c| c.set(0)); +} + /// Current value of [`INTRABC_HITS`]. pub(crate) fn intrabc_hits() -> usize { INTRABC_HITS.with(|c| c.get()) @@ -5580,6 +5604,27 @@ fn read_intra_mode_rect( maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip); maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip); istep!("dq", 0); + // `read_intrabc_info` (spec 5.11.13, libaom `decodemv.c:892`, right after + // `skip`/`segment_id`/`cdef`/`delta_q` and before `mbmi->mode`): the + // `use_intrabc` symbol is read for EVERY intra block of an `allow_intrabc` + // frame -- `av1_allow_intrabc` is a frame-level test with no shape term, so + // rect strips read it exactly like square leaves ([`read_intra_mode`], + // [`read_intra_mode_sub8`]). Missing it here desynced the arithmetic + // decoder at the FIRST symbol of the first rect block of every + // `--tune-content=screen` key frame. Reconstruction of a real intrabc + // block at this shape is out of scope, so the flag is consumed and + // refused by name when it fires. + if allow_intrabc_frame() { + let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; + RECT_INTRABC_READS.with(|c| c.set(c.get() + 1)); + istep!("intrabc", use_intrabc as i32); + if use_intrabc { + INTRABC_HITS.with(|c| c.set(c.get() + 1)); + return Err(unsupported( + "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)", + )); + } + } let above_ctx = INTRA_MODE_CTX[above_mode]; let left_ctx = INTRA_MODE_CTX[left_mode]; let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 5b269c10..fa26c2dd 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -47,6 +47,7 @@ const REFUSALS: &[&str] = &[ "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)", "a block that actually uses a palette (UV) -- reconstruction is out of scope", "a block that actually uses a palette (Y) -- reconstruction is out of scope", + "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)", "a sub-8x8 leaf that uses intrabc (this reader has no block-vector path; the 8x8-and-up reader reconstructs one)", "an intrabc block under TxMode::Select (its transform size is coded by the inter var-tx partition tree, which this decoder never reads)", "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 3312974a..0f4ad695 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -74,6 +74,11 @@ pub fn rect64_inter_tu_hits() -> usize { crate::decode::rect64_inter_tu_hits() } +/// How many rect intra strips read the `use_intrabc` symbol on the last decode. +pub fn rect_intrabc_reads() -> usize { + crate::decode::rect_intrabc_reads() +} + pub fn rect4_32_counters() -> (usize, usize, usize) { ( crate::decode::rect4_32_horz_hits(), @@ -2713,6 +2718,195 @@ mod tests { } } + /// lane-kf900 r5, the gate for the rect-strip `use_intrabc` read. + /// + /// `av1_allow_intrabc` is a FRAME-level test (`decodemv.c:892`, + /// `read_intra_frame_mode_info`): on an intra frame whose header set + /// `allow_intrabc`, EVERY intra block reads a `use_intrabc` symbol right + /// after `skip`/`cdef`/`delta_q` and before `mbmi->mode`. There is no + /// block-shape term, so rect strips read it exactly like square leaves -- + /// `read_intra_mode_rect` never did, and the arithmetic decoder desynced + /// at the FIRST symbol of the first rect block of every + /// `--tune-content=screen` key frame (measured: `testsrc2` 256x192 cq59 + /// 8-bit decoded "OK" with all 49152 luma samples wrong). + /// + /// Arm 0 is that recipe at cq50 without palette: its rect strips really do + /// use intrabc, which this decoder cannot reconstruct, so it must stop on + /// the NAMED refusal instead of emitting a wrong frame -- and it must have + /// read the symbol to get there. + /// Arm 1 is the same recipe without palette and without tx-size search -- + /// it decodes the whole frame, reading the symbol dozens of times. + /// Arm 2 is a screen-tools key frame whose header does NOT set + /// `allow_intrabc` (cq63): it stays pixel-exact, proving the new read is + /// not taken when libaom does not write it. + /// + /// PIXEL RESIDUE (open, lane-kf900 r5): arm 1's frame is entropy-exact + /// against the instrumented aomdec for 429 mode-ladder elements and then + /// diverges inside the DV read of a SQUARE intrabc block at mi(32,44), so + /// its pixels are not compared here. That is a separate defect in + /// `read_intrabc_dv`, not in the read this gate covers. + #[test] + fn a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips() { + const NAME: &str = "a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips"; + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height) = (256usize, 192usize); + // (arm, source, per-arm overrides AFTER the base recipe, expect_refusal, + // must_read_use_intrabc) + let arms: [(&str, &str, &[&str], bool, bool); 3] = [ + ( + "testsrc2-screen-nopalette-cq50", + "testsrc2=s=256x192:r=25", + &["--cq-level=50", "--enable-palette=0", "--enable-tx-size-search=0"], + true, + true, + ), + ( + "smptebars-screen-nopalette-cq45", + "smptebars=size=256x192:rate=25", + &["--cq-level=45", "--enable-palette=0", "--enable-tx-size-search=0"], + false, + true, + ), + ( + "testsrc2-screen-nopalette-cq63", + "testsrc2=s=256x192:r=25", + &["--cq-level=63", "--enable-palette=0", "--enable-tx-size-search=1"], + false, + false, + ), + ]; + let (mut reads_total, mut refused, mut frames_compared) = (0usize, 0u32, 0usize); + for (arm, src, extra, expect_refusal, must_read) in &arms { + let render = || { + 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 y4m_a = render(); + let y4m_b = render(); + assert_eq!(y4m_a, y4m_b, "{NAME}: {arm} must render byte-identical across two runs"); + let y4m = y4m_a; + 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", + // `allow_screen_content_tools` (and with it the frame + // header's `allow_intrabc` bit) comes from here. + "--tune-content=screen", + ]) + // 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; + decode::reset_rect_intrabc_reads(); + let decoded = decode_stream(&stream); + let reads = decode::rect_intrabc_reads(); + reads_total += reads; + if *must_read { + assert!( + reads > 0, + "{NAME}: {arm} read no use_intrabc symbol on a rect strip -- the \ + arm no longer exercises the fix (allow_intrabc not set?)" + ); + } + match decoded { + Ok(frames) => { + assert!( + !*expect_refusal, + "{NAME}: {arm} decoded a frame whose rect strips use intrabc -- \ + reconstruction is not ported, it must refuse by name" + ); + assert!(!frames.is_empty(), "{NAME}: {arm} decoded no frame"); + if *must_read { + // Arm 1: entropy coverage only (see PIXEL RESIDUE above). + eprintln!("{NAME}: {arm} decoded {} frame(s), {reads} rect use_intrabc reads", frames.len()); + continue; + } + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frames.len()); + assert_eq!(frames.len(), ffmpeg_frames.len(), "{NAME}: {arm} frame count"); + for (i, (ours, theirs)) in frames.iter().zip(ffmpeg_frames.iter()).enumerate() { + assert_eq!(ours.y, theirs.y, "{NAME}: {arm} luma, frame {i}"); + assert_eq!(ours.u, theirs.u, "{NAME}: {arm} U, frame {i}"); + assert_eq!(ours.v, theirs.v, "{NAME}: {arm} V, frame {i}"); + frames_compared += 1; + } + } + Err(e) => { + let msg = e.to_string(); + assert!(msg.contains("unsupported"), "{NAME}: {arm} decode failed: {e}"); + assert!( + *expect_refusal, + "{NAME}: {arm} was expected to decode, it refused: {e}" + ); + assert!( + msg.contains("intra block copy") || msg.contains("intrabc"), + "{NAME}: {arm} refused for an unrelated reason: {e}" + ); + eprintln!("{NAME}: {arm} REFUSED (expected): {e}"); + refused += 1; + } + } + } + eprintln!( + "{NAME}: {reads_total} rect-strip use_intrabc symbol(s) read, \ + {refused} refused by name, {frames_compared} frames compared pixel-exact" + ); + assert!(reads_total > 0, "{NAME}: gate is vacuous -- no arm read the symbol"); + assert_eq!(refused, 1, "{NAME}: expected exactly one named intrabc refusal"); + assert!(frames_compared > 0, "{NAME}: no frame was compared pixel-exact"); + } + /// lane-kf900 r4, the gate for the two lifted palette refusals ("a 1:4 /// rect strip that actually uses a palette (reconstruction is not ported /// at this shape)" and "a palette block on a HORZ/VERT intra strip below