From 29e03fefad2bf453776b0ad0b15cb5f1fd08028f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 8 Sep 2026 05:58:19 +0300 Subject: [PATCH] test(av1): the straddle gate's SEG_LVL_ALT_Q rows are generated by recipe, not two uncommittable film crops The two pinned .obu files were crops of a real film in the gitignored shared fixtures dir, so the half of the gate that actually saw the rect dequant defect SKIPped on every fresh clone. Replaced by a synthetic lavfi source -- half flat grey, half heavy noise -- encoded by libaom -aq-mode 1, whose activity segmentation gives segments with distinct quantizers; the new decode::block_q_span() counter asserts that property so the row cannot go blind. Sensitivity re-measured: with e4cab216's six sites reverted the recipe row fails at 384x152 crf 20, frame 0 plane U (160,64), 126 vs 123. --- crates/ec-av1/src/decode.rs | 18 ++++++ crates/ec-av1/src/stream.rs | 124 +++++++++++++++++++++++------------- lanes/d792.report.md | 33 ++++++---- 3 files changed, 120 insertions(+), 55 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b074f631..5d9d3054 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -857,6 +857,12 @@ thread_local! { /// ids any block ended up with -- the gate's proof the feature fired. static SEG_ID_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; static SEG_IDS_SEEN: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// lane-golomb2: the (lowest, highest) quantizer index any block has been + /// dequantized with, i.e. `block_q_idx`'s own span. A gate that means to + /// exercise `SEG_LVL_ALT_Q` needs a stream where this span is WIDER than + /// zero (class `gate-blind-to-feature`): segmentation being enabled says + /// nothing about the segments carrying distinct quantizers. + static BLOCK_Q_SPAN: std::cell::Cell<(i32, i32)> = const { std::cell::Cell::new((i32::MAX, i32::MIN)) }; /// How many `seg_id_predicted` symbols were read (temporal update). static SEG_PRED_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } @@ -906,6 +912,13 @@ pub(crate) fn segment_ids_seen() -> usize { SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) } +/// The lowest and highest quantizer index any decoded block was dequantized +/// with (`(i32::MAX, i32::MIN)` when nothing decoded). +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn block_q_span() -> (i32, i32) { + BLOCK_Q_SPAN.with(|c| c.get()) +} + /// How many `seg_id_predicted` symbols have been read (temporal update). #[allow(dead_code)] // read only from the `#[cfg(test)]` gates pub(crate) fn segment_pred_hits() -> usize { @@ -918,6 +931,7 @@ pub(crate) fn reset_segment_hits() { SEG_ID_HITS.with(|c| c.set(0)); SEG_IDS_SEEN.with(|c| c.set(0)); SEG_PRED_HITS.with(|c| c.set(0)); + BLOCK_Q_SPAN.with(|c| c.set((i32::MAX, i32::MIN))); } /// `seg_feature_active_idx(segment_id, feature)` for the frame currently @@ -947,6 +961,10 @@ fn block_q_idx(fctx: &crate::decode::FrameCtx) -> i32 { if crate::envflags::env_flag!("EC_DQCOEFF") { eprintln!("OUR_Q base={base} seg={seg_id} q={q}"); } + hit_do!(BLOCK_Q_SPAN.with(|c| { + let (lo, hi) = c.get(); + c.set((lo.min(q), hi.max(q))); + });); q } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index a3afba31..3334cb24 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -35201,55 +35201,91 @@ pub(crate) mod tests { } const FRAMES: usize = 4; let mut checked = 0usize; - // The PINNED half of this gate. The generated rows below are the - // general regression; these two files are rav1e's own answer to a - // 384x152 crop of a REAL film (`speed=6`, quantizer 100 and 150, - // `-g 4`), which is the only recipe measured to turn on the - // activity segmentation whose `SEG_LVL_ALT_Q` the rect dequant path - // dropped -- a `testsrc2` or repo-fixture source codes one segment - // and passes this gate with the defect still in (both verified by - // reverting the fix under it). Bytes live in the shared `fixtures` - // directory, which is gitignored, so a missing file SKIPs. - for name in [ - "d792_straddle_rav1e_384x152_q100.obu", - "d792_straddle_rav1e_384x152_q150.obu", - ] { - let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../../fixtures") - .join(name); - let Ok(stream) = std::fs::read(&path) else { - eprintln!("SKIP {NAME}: {name} missing"); - continue; - }; - let (w, h) = (384usize, 152usize); - let want = ffmpeg_decode_sequence(&stream, w, h, FRAMES); - let got = decode_stream(&stream) - .unwrap_or_else(|e| panic!("{NAME}: {name} refused: {e}")); - assert_eq!(got.len(), want.len(), "{NAME}: {name} frame count"); - for (f, (o, r)) in got.iter().zip(&want).enumerate() { - for (plane, a, b) in [("Y", &o.y, &r.y), ("U", &o.u, &r.u), ("V", &o.v, &r.v)] { - if let Some(i) = a.iter().zip(b.iter()).position(|(x, y)| x != y) { - let stride = if plane == "Y" { w } else { w / 2 }; - panic!( - "{NAME}: {name}, frame {f} plane {plane} sample ({}, {}): ours {} \ - vs ffmpeg {}", - i % stride, - i / stride, - a[i], - b[i], - ); + // The SEGMENTED half of this gate, built BY RECIPE. + // + // The defect this gate exists for only shows on a stream whose + // segments carry distinct `SEG_LVL_ALT_Q`, and neither `testsrc2` nor + // the repo's own clip turns activity segmentation on at all (ledger + // dead-end: 16 ladder points all pass with the defect still in). The + // contrast is therefore built here -- half flat grey, half heavy + // noise -- and libaom's `-aq-mode 1` answers it with segments whose + // quantizer indices differ; `block_q_span()` asserts exactly that, so + // the gate cannot go quietly blind (class `gate-blind-to-feature`). + // This replaces two crops of a real film that could never be + // committed and so SKIPped on every fresh clone. + for (w, h) in [(384usize, 152usize), (384, 184)] { + for crf in ["20", "35"] { + let half = (w / 2).to_string(); + let flat = format!("color=c=gray:s={half}x{h}:r=25:d=1"); + let busy = format!("{flat},noise=alls=80:all_seed=7:allf=t+u"); + let args: Vec = [ + "-v", "error", "-f", "lavfi", "-i", &flat, "-f", "lavfi", "-i", &busy, + "-filter_complex", "[0:v][1:v]hstack=inputs=2,format=yuv420p", + "-frames:v", &FRAMES.to_string(), "-pix_fmt", "yuv420p", "-an", + "-threads", "1", "-g", &FRAMES.to_string(), "-c:v", "libaom-av1", + "-cpu-used", "6", "-b:v", "0", "-crf", crf, + // libaom's activity segmentation, i.e. `SEG_LVL_ALT_Q`. + "-aq-mode", "1", "-f", "obu", "-", + ] + .iter() + .map(|s| (*s).to_string()) + .collect(); + let out = Command::new("ffmpeg") + .args(&args) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + if !out.status.success() { + eprintln!( + "SKIP {NAME}: ffmpeg cannot encode the segmented {w}x{h} recipe ({})", + String::from_utf8_lossy(&out.stderr).lines().last().unwrap_or("") + ); + continue; + } + let stream = out.stdout; + let want = ffmpeg_decode_sequence(&stream, w, h, FRAMES); + crate::decode::reset_segment_hits(); + let got = decode_stream(&stream).unwrap_or_else(|e| { + panic!("{NAME}: the segmented {w}x{h} crf {crf} recipe refused: {e}") + }); + let (lo, hi) = crate::decode::block_q_span(); + assert!( + hi > lo, + "{NAME}: the segmented {w}x{h} crf {crf} recipe dequantized every block \ + with q {lo} -- no segment carried a distinct SEG_LVL_ALT_Q, so this row \ + would pass with the rect dequant defect back in ({} segment ids seen)", + crate::decode::segment_ids_seen() + ); + assert_eq!(got.len(), want.len(), "{NAME}: segmented {w}x{h} frame count"); + for (f, (o, r)) in got.iter().zip(&want).enumerate() { + for (plane, a, b) in [("Y", &o.y, &r.y), ("U", &o.u, &r.u), ("V", &o.v, &r.v)] { + if let Some(i) = a.iter().zip(b.iter()).position(|(x, y)| x != y) { + let stride = if plane == "Y" { w } else { w / 2 }; + panic!( + "{NAME}: segmented {w}x{h} crf {crf}, frame {f} plane {plane} \ + sample ({}, {}): ours {} vs ffmpeg {} (block q span {lo}..{hi})", + i % stride, + i / stride, + a[i], + b[i], + ); + } } } + eprintln!( + "{NAME}: segmented {w}x{h} crf {crf} exact, block q span {lo}..{hi}" + ); + checked += 1; } - checked += 1; } for (w, h) in [(384usize, 152usize), (384, 184)] { - // REAL content, not colour bars (class `gate film rows are colour - // bars`): both encoders only turn segmentation on -- the feature - // this gate is about -- when the picture's activity varies, and - // `testsrc2` codes one segment, so a bars source passes this gate - // with the defect still in (verified by reverting the fix under - // it). + // The ENCODER-LADDER half: both reference encoders, every + // ladder point, on real (non-bars) content. These rows code ONE + // segment, so they do not gate `SEG_LVL_ALT_Q` -- that is the + // recipe rows' job above -- but they are the rectangular + // partition coverage rav1e's straddle answer adds. let clip = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) .join("../../fixtures/video/h264-1080p-23.976-8bit.mp4"); if !clip.exists() { diff --git a/lanes/d792.report.md b/lanes/d792.report.md index 98fd3776..40495436 100644 --- a/lanes/d792.report.md +++ b/lanes/d792.report.md @@ -176,17 +176,28 @@ counter. (quantizer 50/100/150/200) and libaom (`-cpu-used 6 -crf 5/20/35/45 -aq-mode 1`), each decoded by ffmpeg and by us and compared sample by sample. -* **Pinned rows** -- `fixtures/d792_straddle_rav1e_384x152_q100.obu` and - `..._q150.obu`, rav1e's own answer to a 384x152 crop of a real film. - -That split is not decoration. **The generated rows alone do NOT catch this -defect**: with the fix reverted under them, first a `testsrc2` source and then -the repo's own `h264-1080p-23.976-8bit.mp4` fixture (which is colour bars, -per the standing note) both PASSED at all 16 points, because neither codes -more than one segment, and the defect only exists where `SEG_LVL_ALT_Q` does -(class `gate-blind-to-feature`). The pinned rows fail pre-fix at -`frame 0 plane Y sample (17, 140): ours 41 vs ffmpeg 42` and pass after, so -the gate's defect-catching power is measured, not assumed. +* **Recipe rows (lane-golomb2)** -- a SYNTHETIC source built for the feature: + half flat grey, half heavy `noise` (hstacked in lavfi), encoded by libaom + `-cpu-used 6 -crf 20/35 -aq-mode 1` at both straddling sizes. libaom's + activity segmentation answers that contrast with segments whose quantizers + differ, which the row asserts through the decoder's own + `decode::block_q_span()` (measured: q 33..106 at crf 20, 89..160 at crf 35). + +That split is not decoration. **The generated ladder rows alone do NOT catch +this defect**: with the fix reverted under them, first a `testsrc2` source and +then the repo's own `h264-1080p-23.976-8bit.mp4` fixture (which is colour +bars, per the standing note) both PASSED at all 16 points, because neither +codes more than one segment, and the defect only exists where `SEG_LVL_ALT_Q` +does (class `gate-blind-to-feature`). The recipe rows fail pre-fix at +`segmented 384x152 crf 20, frame 0 plane U sample (160, 64): ours 126 vs +ffmpeg 123` (the six sites of `e4cab216` reverted by hand, gate re-run) and +pass after, so the gate's defect-catching power is measured, not assumed. + +The two pinned streams this gate used to carry +(`fixtures/d792_straddle_rav1e_384x152_q{100,150}.obu`) were crops of a real +film: untracked, uncommittable, and SKIPped on any fresh clone, i.e. blind +where it mattered most (class `gate-skips-on-its-own-failure`). They are +deleted; the recipe rows replace them. ## Verification