From 2f8409b5baed949b0fdf4dc7d4d37e1b848b3b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 13:31:11 +0300 Subject: [PATCH] fix(av1): refuse lossless frames by name; the 128-intra-in-inter chroma context gate --- crates/ec-av1/src/stream.rs | 103 ++++++++++++++++++++++++++++++++++++ lanes/dpm1.report.md | 49 +++++++++++++++-- 2 files changed, 149 insertions(+), 3 deletions(-) diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 784abd6e..9180ab34 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -1591,6 +1591,24 @@ fn decode_frame( "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)", )); } + // lane-dpm1: a LOSSLESS frame (`qindex == 0` with zero deltas, spec 5.9.12) + // is a different tile syntax, and this decoder has none of it: libaom + // forces TX_4X4 with the WALSH-HADAMARD transform over every block (so no + // `tx_depth` symbol is coded at all), and `is_cfl_allowed` narrows to + // `plane_bsize == BLOCK_4X4` instead of the 32x32 rule, which changes the + // `uv_mode` ALPHABET. Measured on his own screen-capture row at libaom + // `-crf 5` (base_q_idx 0): the very first block, mi(0,0) BLOCK_32X32, + // reads `uv_mode` off the CfL alphabet where libaom reads the plain one + // (ours 12 = UV_CFL_PRED, aomdec 1 = V_PRED) and the tile desyncs at + // symbol four of the frame. Refuse by name rather than return a picture + // that is silently wrong from luma sample 0 (class `refusal-hides-a-defect` + // read the other way: a silent wrong decode is worse than a named gap). + if header.lossless.iter().any(|&l| l) { + return Err(Error::unsupported( + "AV1 decode_stream", + "a lossless frame (qindex 0): the TX_4X4 Walsh-Hadamard tile syntax and the lossless CfL rule are unimplemented", + )); + } crate::decode::set_bit_depth(bit_depth, fctx); // spec 7.16: this frame's superres state, for the filter chain -- // `decode.rs` upscales between CDEF and loop restoration and sizes @@ -3882,6 +3900,91 @@ pub(crate) mod tests { } } + /// lane-dpm1: the 128x128-INTRA-inside-an-INTER-frame chroma context gate. + /// A 128x128 block codes its chroma as four TX_32X32 units, one per 64x64 + /// mu chunk, and libaom stamps the coefficient context PER UNIT. The two + /// INTER mu-chunk sites set `mu_chroma` so the block tail re-stamps the + /// four units in mu order; the INTRA-in-inter site did not, so the + /// whole-block record left every unit carrying the TOP-LEFT unit's level. + /// The next block then read its `txb_skip` off ctx 11 where libaom reads + /// ctx 10 (class `override-slot-on-one-arm`) -- one symbol on the wrong + /// CDF row, tile desynced, and because the loop-restoration coefficients + /// live in the same tile data the damage showed up as a +-1 luma sample at + /// the TOP of the frame (measured: frame 1, luma sample 66, ours 146 / + /// ffmpeg 145, with 1.7M samples wrong behind it). + /// + /// Source: the repository's own 2160p colour-bar fixture at the native + /// gate's crop (1920x1024 out of the middle), 12 frames, libaom + /// `-cpu-used 6 -crf 45 -g 12` -- the first reference point of + /// `bd_rate_screen_native`'s "bars 2160p" row, which is where the assertion + /// caught it. The gate asserts the tool fired (class + /// `gate-blind-to-feature`) and that every shown frame decodes sample-exact + /// against ffmpeg's own decode. + #[test] + fn a_libaom_stream_with_128_intra_blocks_in_inter_frames_decodes_exact() { + const NAME: &str = "a_libaom_stream_with_128_intra_blocks_in_inter_frames_decodes_exact"; + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + let clip = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../fixtures/video/h264-2160p-23.976-8bit.mp4"); + if !clip.exists() { + eprintln!("SKIP {NAME}: the 2160p bars fixture is missing"); + return; + } + let (w, h, frames) = (1920usize, 1024usize, 12usize); + // [[pid-keyed-temp-path]]: parallel test binaries must not share a name. + let src = std::env::temp_dir().join(format!("ec-av1-i128-{}.yuv", std::process::id())); + let load = Command::new("ffmpeg") + .args(["-v", "error", "-y", "-i"]) + .arg(&clip) + .args(["-frames:v", &frames.to_string(), "-vf", "crop=1920:1024:960:568"]) + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p"]) + .arg(&src) + .output() + .expect("ffmpeg failed to run"); + assert!(load.status.success(), "{NAME}: {}", String::from_utf8_lossy(&load.stderr)); + let out = Command::new("ffmpeg") + .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"]) + .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"]) + .arg(&src) + .args(["-an", "-threads", "1", "-g", "12", "-c:v", "libaom-av1"]) + .args(["-cpu-used", "6", "-b:v", "0", "-crf", "45", "-f", "obu", "-"]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + let _ = std::fs::remove_file(&src); + if !out.status.success() { + eprintln!( + "SKIP {NAME}: ffmpeg has no libaom-av1 encoder ({})", + String::from_utf8_lossy(&out.stderr) + ); + return; + } + let stream = out.stdout; + let before = crate::decode::intra_128_in_inter_hits(); + let ours = decode_stream(&stream).expect("libaom bars stream decodes"); + assert!( + crate::decode::intra_128_in_inter_hits() > before, + "{NAME} went blind: no 128-rooted intra block inside an inter frame" + ); + let theirs = ffmpeg_decode_sequence(&stream, w, h, frames); + assert_eq!(ours.len(), frames, "shown frame count"); + for (i, (a, b)) in ours.iter().zip(theirs.iter()).enumerate() { + for (plane, l, r) in [("Y", &a.y, &b.y), ("U", &a.u, &b.u), ("V", &a.v, &b.v)] { + let first = l.iter().zip(r.iter()).position(|(x, y)| x != y); + assert!( + first.is_none(), + "frame {i} plane {plane} differs from ffmpeg at sample {}", + first.unwrap() + ); + } + } + } + /// 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/dpm1.report.md b/lanes/dpm1.report.md index d0f3928c..54b66834 100644 --- a/lanes/dpm1.report.md +++ b/lanes/dpm1.report.md @@ -32,7 +32,50 @@ Instrumented finding: a +64 perturbation of rounded candidates DID change the output on those streams, so the rounded candidates were consumed but only in slots whose ±4 eighth-pel difference never reached a coded block's mv. +## (B) bars 2160p libaom crf 45 — FIXED (root cause, class, sweep, fixture) +First divergence (two instrumented decoders, `EC_TRACE_MODE_STEP` + +`EC_TRACE_COEFF`): INTER frame 1, block **mi(0,288)**, a 128x128 INTRA block +inside an inter frame, its FIRST chroma (U) transform unit -- aomdec reads +`txb_skip` on ctx 10, ours on ctx 11 (`get_txb_ctx`'s offset-10 rows, base 1 vs +0). The luma ladder and every symbol before it matched exactly. + +Root cause, `crates/ec-av1/src/decode.rs`: a 128x128 block codes its chroma as +four TX_32X32 units, one per 64x64 mu chunk, and libaom stamps the coefficient +context PER UNIT. The block tail re-stamps those four units only when +`mu_chroma` is set -- the two INTER mu-chunk sites (27255, 28552) set it, the +INTRA-in-inter site did not, so the whole-block record left all four units +carrying the TOP-LEFT unit's level. Class **`override-slot-on-one-arm`** (a fix +installed on one arm of a set of twins). Sweep: all `CHROMA_SPLIT_TX_HITS` +sites -- the two key-frame ones (14364, 14703) have their own local re-stamp, +the three inter-frame ones now all set `mu_chroma`. No other site. + +Why it read as a `+-1` at sample 66: the U-plane symbol desynced the tile, and +the loop-restoration coefficients live in the same tile data, so the whole +frame shifted by one level while 1.7M samples behind it went wrong. + +Fixture: `stream.rs a_libaom_stream_with_128_intra_blocks_in_inter_frames_decodes_exact` -- the repo's own 2160p bars fixture at the native crop, libaom +`-cpu-used 6 -crf 45 -g 12`, asserts the tool fired and every frame is exact. +With `mu_chroma = false` on that arm it FAILS with "frame 1 plane Y differs +from ffmpeg at sample 66" (verified). + +## (A) screen capture libaom crf 5 — root cause found, feature DEFERRED +That stream's key frame is coded **LOSSLESS** (`base_q_idx 0`, `lossless[seg]` +true; aomdec's own `EC_IMODE_VAL ... tx=0` shows TX_4X4 on a BLOCK_32X32). +libaom then (a) forces TX_4X4 + the Walsh-Hadamard transform everywhere and +codes NO `tx_depth` symbol, and (b) narrows `is_cfl_allowed` to +`plane_bsize == BLOCK_4X4`, which changes the `uv_mode` alphabet. Our tile +reader ignores `header.lossless` entirely: the FIRST block, mi(0,0), reads +`uv_mode` = 12 (UV_CFL_PRED) where aomdec reads 1 (V_PRED) -- symbol four of +the frame. Class `branch-dropped-as-unreachable` (the header computes +`lossless`/`coded_lossless` and nothing consumes it). + +Shipped now: a NAMED REFUSAL in `stream.rs` (`a lossless frame (qindex 0) ...`) +so the decoder stops returning a picture that is wrong from luma sample 0. +`deferred: the lossless decode path (WHT 4x4, forced tx size, lossless CfL +rule, filter bypass) — a feature lane, not a fix — unblocks: implement +`inverse WHT + the TX_4X4 forcing + `is_cfl_allowed`'s lossless branch, gate +with this same screen row at crf 5.` + ## OPEN -* bars 2160p libaom crf 45 frame 1 Y sample 66 (+-1 reconstruction). -* screen capture (OBS .mkv row) libaom crf 5 frame 0 (KEY) Y sample 0: 126 vs - 33 — a key-frame desync at the first sample. +* The native gate's screen row now reports the refusal instead of a silent + wrong decode; it is RED until the lossless lane lands.