From 7642b3514445b0100a7bd929d5040a8948fdc0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 25 Aug 2026 07:33:57 +0300 Subject: [PATCH] test(bd): refuse a sample window with nothing in it A clip-driven BD run whose sample window is a film's black head codes to almost no bits and decodes back to the source exactly, so every point on both curves sits at the 99 dB PSNR cap, the two cubics coincide, and the gate prints +0.000 dB. That is not a measurement, and it read as one: an ablation re-measure reported the same +0.000 for both arms of the knob. Both BD gates now refuse such a window by name and say which variable samples past it. The h265 gate also fails with "ladder collapsed" when its QP shift clamps two ladder points onto the same QP, instead of the "singular BD-PSNR fit" assert deep inside the cubic solver. --- crates/ec-h264/tests/encode.rs | 17 +++++++++++++++++ crates/ec-h265/tests/encode.rs | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/crates/ec-h264/tests/encode.rs b/crates/ec-h264/tests/encode.rs index 70f6d10c..a7f760c6 100644 --- a/crates/ec-h264/tests/encode.rs +++ b/crates/ec-h264/tests/encode.rs @@ -228,6 +228,21 @@ fn cubic_integral(c: [f64; 4], x: f64) -> f64 { c[0] * x + c[1] * x * x / 2.0 + c[2] * x * x * x / 3.0 + c[3] * x * x * x * x / 4.0 } +/// A sample window with nothing in it (a film's black head, a fade) codes to +/// almost no bits and decodes back to the source exactly, so every point on +/// both curves sits at the PSNR cap and the BD number is a flat +0.000 that +/// reads like a measurement. Refuse such a window by name. +fn refuse_flat_window(psnrs: impl IntoIterator, skip_var: &str) { + const FLAT_WINDOW_DB: f64 = 90.0; + let ps: Vec = psnrs.into_iter().collect(); + assert!( + ps.iter().any(|&p| p < FLAT_WINDOW_DB), + "the sample window is flat — every point reaches the PSNR cap ({ps:?}); \ + this is a black head or a fade, not content: pass {skip_var} to sample \ + past it" + ); +} + fn bd_psnr_delta(candidate: &[(f64, f64)], anchor: &[(f64, f64)]) -> f64 { let xs_candidate = std::array::from_fn(|i| candidate[i].0.ln()); let ys_candidate = std::array::from_fn(|i| candidate[i].1); @@ -539,6 +554,7 @@ fn eight_by_eight_gains_on_flat_and_text() { let qps = [22, 26, 30, 34]; let on: Vec<_> = qps.iter().map(|&q| run(true, q, true)).collect(); let off: Vec<_> = qps.iter().map(|&q| run(false, q, true)).collect(); + refuse_flat_window(off.iter().map(|&(_, p, _)| p), "EC_H264_CLIP_SKIP"); let on_curve: Vec<_> = on.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect(); let off_curve: Vec<_> = off.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect(); let bd = bd_psnr_delta(&on_curve, &off_curve); @@ -1223,6 +1239,7 @@ fn bd_psnr_vs_x264() { oy - xy ); } + refuse_flat_window(theirs.iter().map(|&(_, p, _)| p), "EC_H264_CLIP_SKIP"); let curve = |v: &[(f64, f64, f64)], luma: bool| -> Vec<(f64, f64)> { v.iter() .map(|&(b, p, y)| (b, if luma { p } else { y })) diff --git a/crates/ec-h265/tests/encode.rs b/crates/ec-h265/tests/encode.rs index 51e0cc4c..e469d759 100644 --- a/crates/ec-h265/tests/encode.rs +++ b/crates/ec-h265/tests/encode.rs @@ -556,6 +556,19 @@ fn bd_psnr_vs_x265() { // Probe both encoders at QP 26 to measure the bit ratio. let probe_ours = ours(probe_qp); let probe_x265 = x265(probe_qp); + // A sample window with nothing in it (a film's black head, a fade) codes + // to a handful of bits and decodes back to the source exactly, so every + // point on both curves sits at the PSNR cap and the BD number is a flat + // +0.000 that looks like a measurement. Refuse it by name. + const FLAT_WINDOW_DB: f64 = 90.0; + assert!( + probe_x265.1 < FLAT_WINDOW_DB && probe_ours.1 < FLAT_WINDOW_DB, + "the sample window is flat — both encoders reach the PSNR cap on it \ + (ours {:.2} dB, x265 {:.2} dB at qp {probe_qp}); this is a black head \ + or a fade, not content: pass EC_H265_CLIP_SKIP to sample past it", + probe_ours.1, + probe_x265.1, + ); let mut shift = (6.0 * (probe_ours.0 / probe_x265.0).log2()).round() as i32; eprintln!( "ladder shift {shift:+} (probe q{probe_qp}: ours {:.1} Mbit vs x265 {:.1} Mbit)", @@ -605,6 +618,15 @@ fn bd_psnr_vs_x265() { let offset = f64::from(q - probe_qp) * spread; ((centre + offset).round() as i32).clamp(1, 45) }); + let mut distinct = our_qps; + distinct.sort_unstable(); + let collapsed = distinct.windows(2).any(|w| w[0] == w[1]); + assert!( + !collapsed, + "our QP ladder collapsed to {:?}: the shift has pushed it into the \ + qp floor, so two points share an x and the cubic has no unique fit", + our_qps, + ); mine = our_qps .iter() .map(|&q| if q == probe_qp { probe_ours } else { ours(q) })