forge
Explore
Log in

Don't you dare go Hollow

Refuse lossless frames by name

Browse files at this commit

2f8409b5baed949b0fdf4dc7d4d37e1b848b3b4bAhmet Kaan GümüşParent: bdab11e
  • fix
    • refuse lossless frames by name
    • the 128-intra-in-inter chroma context gate
Show full message
fix(av1): refuse lossless frames by name; the 128-intra-in-inter chroma context gate
crates/ec-av1/src/stream.rsmodified+103 -0
@@ -1591,6 +1591,24 @@ fn decode_frame(
1591 "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)",1591 "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)",
1592 ));1592 ));
1593 }1593 }
1594+ // lane-dpm1: a LOSSLESS frame (`qindex == 0` with zero deltas, spec 5.9.12)
1595+ // is a different tile syntax, and this decoder has none of it: libaom
1596+ // forces TX_4X4 with the WALSH-HADAMARD transform over every block (so no
1597+ // `tx_depth` symbol is coded at all), and `is_cfl_allowed` narrows to
1598+ // `plane_bsize == BLOCK_4X4` instead of the 32x32 rule, which changes the
1599+ // `uv_mode` ALPHABET. Measured on his own screen-capture row at libaom
1600+ // `-crf 5` (base_q_idx 0): the very first block, mi(0,0) BLOCK_32X32,
1601+ // reads `uv_mode` off the CfL alphabet where libaom reads the plain one
1602+ // (ours 12 = UV_CFL_PRED, aomdec 1 = V_PRED) and the tile desyncs at
1603+ // symbol four of the frame. Refuse by name rather than return a picture
1604+ // that is silently wrong from luma sample 0 (class `refusal-hides-a-defect`
1605+ // read the other way: a silent wrong decode is worse than a named gap).
1606+ if header.lossless.iter().any(|&l| l) {
1607+ return Err(Error::unsupported(
1608+ "AV1 decode_stream",
1609+ "a lossless frame (qindex 0): the TX_4X4 Walsh-Hadamard tile syntax and the lossless CfL rule are unimplemented",
1610+ ));
1611+ }
1594 crate::decode::set_bit_depth(bit_depth, fctx);1612 crate::decode::set_bit_depth(bit_depth, fctx);
1595 // spec 7.16: this frame's superres state, for the filter chain --1613 // spec 7.16: this frame's superres state, for the filter chain --
1596 // `decode.rs` upscales between CDEF and loop restoration and sizes1614 // `decode.rs` upscales between CDEF and loop restoration and sizes
@@ -3882,6 +3900,91 @@ pub(crate) mod tests {
3882 }3900 }
3883 }3901 }
3884 3902
3903+ /// lane-dpm1: the 128x128-INTRA-inside-an-INTER-frame chroma context gate.
3904+ /// A 128x128 block codes its chroma as four TX_32X32 units, one per 64x64
3905+ /// mu chunk, and libaom stamps the coefficient context PER UNIT. The two
3906+ /// INTER mu-chunk sites set `mu_chroma` so the block tail re-stamps the
3907+ /// four units in mu order; the INTRA-in-inter site did not, so the
3908+ /// whole-block record left every unit carrying the TOP-LEFT unit's level.
3909+ /// The next block then read its `txb_skip` off ctx 11 where libaom reads
3910+ /// ctx 10 (class `override-slot-on-one-arm`) -- one symbol on the wrong
3911+ /// CDF row, tile desynced, and because the loop-restoration coefficients
3912+ /// live in the same tile data the damage showed up as a +-1 luma sample at
3913+ /// the TOP of the frame (measured: frame 1, luma sample 66, ours 146 /
3914+ /// ffmpeg 145, with 1.7M samples wrong behind it).
3915+ ///
3916+ /// Source: the repository's own 2160p colour-bar fixture at the native
3917+ /// gate's crop (1920x1024 out of the middle), 12 frames, libaom
3918+ /// `-cpu-used 6 -crf 45 -g 12` -- the first reference point of
3919+ /// `bd_rate_screen_native`'s "bars 2160p" row, which is where the assertion
3920+ /// caught it. The gate asserts the tool fired (class
3921+ /// `gate-blind-to-feature`) and that every shown frame decodes sample-exact
3922+ /// against ffmpeg's own decode.
3923+ #[test]
3924+ fn a_libaom_stream_with_128_intra_blocks_in_inter_frames_decodes_exact() {
3925+ const NAME: &str = "a_libaom_stream_with_128_intra_blocks_in_inter_frames_decodes_exact";
3926+ if !have_ffmpeg() {
3927+ eprintln!("SKIP {NAME}: no ffmpeg");
3928+ return;
3929+ }
3930+ let clip = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
3931+ .join("../../fixtures/video/h264-2160p-23.976-8bit.mp4");
3932+ if !clip.exists() {
3933+ eprintln!("SKIP {NAME}: the 2160p bars fixture is missing");
3934+ return;
3935+ }
3936+ let (w, h, frames) = (1920usize, 1024usize, 12usize);
3937+ // [[pid-keyed-temp-path]]: parallel test binaries must not share a name.
3938+ let src = std::env::temp_dir().join(format!("ec-av1-i128-{}.yuv", std::process::id()));
3939+ let load = Command::new("ffmpeg")
3940+ .args(["-v", "error", "-y", "-i"])
3941+ .arg(&clip)
3942+ .args(["-frames:v", &frames.to_string(), "-vf", "crop=1920:1024:960:568"])
3943+ .args(["-f", "rawvideo", "-pix_fmt", "yuv420p"])
3944+ .arg(&src)
3945+ .output()
3946+ .expect("ffmpeg failed to run");
3947+ assert!(load.status.success(), "{NAME}: {}", String::from_utf8_lossy(&load.stderr));
3948+ let out = Command::new("ffmpeg")
3949+ .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"])
3950+ .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"])
3951+ .arg(&src)
3952+ .args(["-an", "-threads", "1", "-g", "12", "-c:v", "libaom-av1"])
3953+ .args(["-cpu-used", "6", "-b:v", "0", "-crf", "45", "-f", "obu", "-"])
3954+ .stdin(Stdio::null())
3955+ .stdout(Stdio::piped())
3956+ .stderr(Stdio::piped())
3957+ .output()
3958+ .expect("ffmpeg failed to run");
3959+ let _ = std::fs::remove_file(&src);
3960+ if !out.status.success() {
3961+ eprintln!(
3962+ "SKIP {NAME}: ffmpeg has no libaom-av1 encoder ({})",
3963+ String::from_utf8_lossy(&out.stderr)
3964+ );
3965+ return;
3966+ }
3967+ let stream = out.stdout;
3968+ let before = crate::decode::intra_128_in_inter_hits();
3969+ let ours = decode_stream(&stream).expect("libaom bars stream decodes");
3970+ assert!(
3971+ crate::decode::intra_128_in_inter_hits() > before,
3972+ "{NAME} went blind: no 128-rooted intra block inside an inter frame"
3973+ );
3974+ let theirs = ffmpeg_decode_sequence(&stream, w, h, frames);
3975+ assert_eq!(ours.len(), frames, "shown frame count");
3976+ for (i, (a, b)) in ours.iter().zip(theirs.iter()).enumerate() {
3977+ for (plane, l, r) in [("Y", &a.y, &b.y), ("U", &a.u, &b.u), ("V", &a.v, &b.v)] {
3978+ let first = l.iter().zip(r.iter()).position(|(x, y)| x != y);
3979+ assert!(
3980+ first.is_none(),
3981+ "frame {i} plane {plane} differs from ffmpeg at sample {}",
3982+ first.unwrap()
3983+ );
3984+ }
3985+ }
3986+ }
3987+
3885 /// lane-dkey: the palette-neighbour-band gate. A palette-Y block's size and3988 /// lane-dkey: the palette-neighbour-band gate. A palette-Y block's size and
3886 /// colours used to stay standing in the mi-granular above/left bands after a3989 /// colours used to stay standing in the mi-granular above/left bands after a
3887 /// sub-8x8 group was decoded next to it -- the sub-8x8 readers published3990 /// sub-8x8 group was decoded next to it -- the sub-8x8 readers published
lanes/dpm1.report.mdmodified+46 -3
@@ -32,7 +32,50 @@ Instrumented finding: a +64 perturbation of rounded candidates DID change the
32 output on those streams, so the rounded candidates were consumed but only in32 output on those streams, so the rounded candidates were consumed but only in
33 slots whose ±4 eighth-pel difference never reached a coded block's mv.33 slots whose ±4 eighth-pel difference never reached a coded block's mv.
34 34
35+## (B) bars 2160p libaom crf 45 — FIXED (root cause, class, sweep, fixture)
36+First divergence (two instrumented decoders, `EC_TRACE_MODE_STEP` +
37+`EC_TRACE_COEFF`): INTER frame 1, block **mi(0,288)**, a 128x128 INTRA block
38+inside an inter frame, its FIRST chroma (U) transform unit -- aomdec reads
39+`txb_skip` on ctx 10, ours on ctx 11 (`get_txb_ctx`'s offset-10 rows, base 1 vs
40+0). The luma ladder and every symbol before it matched exactly.
41+
42+Root cause, `crates/ec-av1/src/decode.rs`: a 128x128 block codes its chroma as
43+four TX_32X32 units, one per 64x64 mu chunk, and libaom stamps the coefficient
44+context PER UNIT. The block tail re-stamps those four units only when
45+`mu_chroma` is set -- the two INTER mu-chunk sites (27255, 28552) set it, the
46+INTRA-in-inter site did not, so the whole-block record left all four units
47+carrying the TOP-LEFT unit's level. Class **`override-slot-on-one-arm`** (a fix
48+installed on one arm of a set of twins). Sweep: all `CHROMA_SPLIT_TX_HITS`
49+sites -- the two key-frame ones (14364, 14703) have their own local re-stamp,
50+the three inter-frame ones now all set `mu_chroma`. No other site.
51+
52+Why it read as a `+-1` at sample 66: the U-plane symbol desynced the tile, and
53+the loop-restoration coefficients live in the same tile data, so the whole
54+frame shifted by one level while 1.7M samples behind it went wrong.
55+
56+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
57+`-cpu-used 6 -crf 45 -g 12`, asserts the tool fired and every frame is exact.
58+With `mu_chroma = false` on that arm it FAILS with "frame 1 plane Y differs
59+from ffmpeg at sample 66" (verified).
60+
61+## (A) screen capture libaom crf 5 — root cause found, feature DEFERRED
62+That stream's key frame is coded **LOSSLESS** (`base_q_idx 0`, `lossless[seg]`
63+true; aomdec's own `EC_IMODE_VAL ... tx=0` shows TX_4X4 on a BLOCK_32X32).
64+libaom then (a) forces TX_4X4 + the Walsh-Hadamard transform everywhere and
65+codes NO `tx_depth` symbol, and (b) narrows `is_cfl_allowed` to
66+`plane_bsize == BLOCK_4X4`, which changes the `uv_mode` alphabet. Our tile
67+reader ignores `header.lossless` entirely: the FIRST block, mi(0,0), reads
68+`uv_mode` = 12 (UV_CFL_PRED) where aomdec reads 1 (V_PRED) -- symbol four of
69+the frame. Class `branch-dropped-as-unreachable` (the header computes
70+`lossless`/`coded_lossless` and nothing consumes it).
71+
72+Shipped now: a NAMED REFUSAL in `stream.rs` (`a lossless frame (qindex 0) ...`)
73+so the decoder stops returning a picture that is wrong from luma sample 0.
74+`deferred: the lossless decode path (WHT 4x4, forced tx size, lossless CfL
75+rule, filter bypass) — a feature lane, not a fix — unblocks: implement
76+`inverse WHT + the TX_4X4 forcing + `is_cfl_allowed`'s lossless branch, gate
77+with this same screen row at crf 5.`
78+
35 ## OPEN79 ## OPEN
36-* bars 2160p libaom crf 45 frame 1 Y sample 66 (+-1 reconstruction).80+* The native gate's screen row now reports the refusal instead of a silent
37-* screen capture (OBS .mkv row) libaom crf 5 frame 0 (KEY) Y sample 0: 126 vs81+ wrong decode; it is RED until the lossless lane lands.
38- 33 — a key-frame desync at the first sample.