forge
Explore
Log in

Don't you dare go Hollow

Refuse a sample window with nothing in it

Browse files at this commit

7642b3514445b0100a7bd929d5040a8948fdc0f6Ahmet Kaan GümüşParent: ab106d6
  • test
    • refuse a sample window with nothing in it
Show full message
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.rsmodified+17 -0
@@ -228,6 +228,21 @@ fn cubic_integral(c: [f64; 4], x: f64) -> f64 {
228 c[0] * x + c[1] * x * x / 2.0 + c[2] * x * x * x / 3.0 + c[3] * x * x * x * x / 4.0228 c[0] * x + c[1] * x * x / 2.0 + c[2] * x * x * x / 3.0 + c[3] * x * x * x * x / 4.0
229 }229 }
230 230
231+/// A sample window with nothing in it (a film's black head, a fade) codes to
232+/// almost no bits and decodes back to the source exactly, so every point on
233+/// both curves sits at the PSNR cap and the BD number is a flat +0.000 that
234+/// reads like a measurement. Refuse such a window by name.
235+fn refuse_flat_window(psnrs: impl IntoIterator<Item = f64>, skip_var: &str) {
236+ const FLAT_WINDOW_DB: f64 = 90.0;
237+ let ps: Vec<f64> = psnrs.into_iter().collect();
238+ assert!(
239+ ps.iter().any(|&p| p < FLAT_WINDOW_DB),
240+ "the sample window is flat — every point reaches the PSNR cap ({ps:?}); \
241+ this is a black head or a fade, not content: pass {skip_var} to sample \
242+ past it"
243+ );
244+}
245+
231 fn bd_psnr_delta(candidate: &[(f64, f64)], anchor: &[(f64, f64)]) -> f64 {246 fn bd_psnr_delta(candidate: &[(f64, f64)], anchor: &[(f64, f64)]) -> f64 {
232 let xs_candidate = std::array::from_fn(|i| candidate[i].0.ln());247 let xs_candidate = std::array::from_fn(|i| candidate[i].0.ln());
233 let ys_candidate = std::array::from_fn(|i| candidate[i].1);248 let ys_candidate = std::array::from_fn(|i| candidate[i].1);
@@ -539,6 +554,7 @@ fn eight_by_eight_gains_on_flat_and_text() {
539 let qps = [22, 26, 30, 34];554 let qps = [22, 26, 30, 34];
540 let on: Vec<_> = qps.iter().map(|&q| run(true, q, true)).collect();555 let on: Vec<_> = qps.iter().map(|&q| run(true, q, true)).collect();
541 let off: Vec<_> = qps.iter().map(|&q| run(false, q, true)).collect();556 let off: Vec<_> = qps.iter().map(|&q| run(false, q, true)).collect();
557+ refuse_flat_window(off.iter().map(|&(_, p, _)| p), "EC_H264_CLIP_SKIP");
542 let on_curve: Vec<_> = on.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect();558 let on_curve: Vec<_> = on.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect();
543 let off_curve: Vec<_> = off.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect();559 let off_curve: Vec<_> = off.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect();
544 let bd = bd_psnr_delta(&on_curve, &off_curve);560 let bd = bd_psnr_delta(&on_curve, &off_curve);
@@ -1223,6 +1239,7 @@ fn bd_psnr_vs_x264() {
1223 oy - xy1239 oy - xy
1224 );1240 );
1225 }1241 }
1242+ refuse_flat_window(theirs.iter().map(|&(_, p, _)| p), "EC_H264_CLIP_SKIP");
1226 let curve = |v: &[(f64, f64, f64)], luma: bool| -> Vec<(f64, f64)> {1243 let curve = |v: &[(f64, f64, f64)], luma: bool| -> Vec<(f64, f64)> {
1227 v.iter()1244 v.iter()
1228 .map(|&(b, p, y)| (b, if luma { p } else { y }))1245 .map(|&(b, p, y)| (b, if luma { p } else { y }))
crates/ec-h265/tests/encode.rsmodified+22 -0
@@ -556,6 +556,19 @@ fn bd_psnr_vs_x265() {
556 // Probe both encoders at QP 26 to measure the bit ratio.556 // Probe both encoders at QP 26 to measure the bit ratio.
557 let probe_ours = ours(probe_qp);557 let probe_ours = ours(probe_qp);
558 let probe_x265 = x265(probe_qp);558 let probe_x265 = x265(probe_qp);
559+ // A sample window with nothing in it (a film's black head, a fade) codes
560+ // to a handful of bits and decodes back to the source exactly, so every
561+ // point on both curves sits at the PSNR cap and the BD number is a flat
562+ // +0.000 that looks like a measurement. Refuse it by name.
563+ const FLAT_WINDOW_DB: f64 = 90.0;
564+ assert!(
565+ probe_x265.1 < FLAT_WINDOW_DB && probe_ours.1 < FLAT_WINDOW_DB,
566+ "the sample window is flat — both encoders reach the PSNR cap on it \
567+ (ours {:.2} dB, x265 {:.2} dB at qp {probe_qp}); this is a black head \
568+ or a fade, not content: pass EC_H265_CLIP_SKIP to sample past it",
569+ probe_ours.1,
570+ probe_x265.1,
571+ );
559 let mut shift = (6.0 * (probe_ours.0 / probe_x265.0).log2()).round() as i32;572 let mut shift = (6.0 * (probe_ours.0 / probe_x265.0).log2()).round() as i32;
560 eprintln!(573 eprintln!(
561 "ladder shift {shift:+} (probe q{probe_qp}: ours {:.1} Mbit vs x265 {:.1} Mbit)",574 "ladder shift {shift:+} (probe q{probe_qp}: ours {:.1} Mbit vs x265 {:.1} Mbit)",
@@ -605,6 +618,15 @@ fn bd_psnr_vs_x265() {
605 let offset = f64::from(q - probe_qp) * spread;618 let offset = f64::from(q - probe_qp) * spread;
606 ((centre + offset).round() as i32).clamp(1, 45)619 ((centre + offset).round() as i32).clamp(1, 45)
607 });620 });
621+ let mut distinct = our_qps;
622+ distinct.sort_unstable();
623+ let collapsed = distinct.windows(2).any(|w| w[0] == w[1]);
624+ assert!(
625+ !collapsed,
626+ "our QP ladder collapsed to {:?}: the shift has pushed it into the \
627+ qp floor, so two points share an x and the cubic has no unique fit",
628+ our_qps,
629+ );
608 mine = our_qps630 mine = our_qps
609 .iter()631 .iter()
610 .map(|&q| if q == probe_qp { probe_ours } else { ours(q) })632 .map(|&q| if q == probe_qp { probe_ours } else { ours(q) })