From cd63f57fb218cbcf3f7f1a51f70b53639cc22f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 23 Aug 2026 17:45:11 +0300 Subject: [PATCH] fix(h264/test): match x264's quantiser, and split the gap intra vs inter The gate as committed asked x264 for a constant QP and did not get one: x264's CQP mode scales the I-picture quantiser by `ipratio` (1.4 by default), so its I pictures were coded finer than the QP under test. That flattered x264 on the intra side and made the whole comparison a different-quantiser comparison. `ipratio=1.0:pbratio=1.0:qcomp=1.0: chroma-qp-offset=0` pins it; the synthetic BD-PSNR moves from -2.442 dB to -2.640 dB, and the floor with it. With the quantiser actually matched, `EC_H264_GOP` splits the distance. On the 3840x1608 clip from the library: GOP 10 : -2.992 dB, ours +19% bits at 1.1-1.7 dB lower PSNR GOP 1 : -0.749 dB, ours +2-4% bits at 0.4-0.8 dB lower PSNR So the intra path is close and roughly two thirds of the distance is in P pictures: our inter path searches 16x16 only at `Preset::Fast` (halves at Balanced, never 8x8 sub-partitions) with no rate-distortion subpel refinement, and a worse prediction costs both bits and PSNR at the same quantiser. That is the next lane's target, now measured instead of assumed. Ours is also read off ffmpeg's decode of our own stream rather than off the encoder's reconstruction, matching how x264 is read; the two are asserted equal within 0.05 dB. --- crates/ec-h264/tests/encode.rs | 46 ++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/crates/ec-h264/tests/encode.rs b/crates/ec-h264/tests/encode.rs index 311b5b4e..337ca7fe 100644 --- a/crates/ec-h264/tests/encode.rs +++ b/crates/ec-h264/tests/encode.rs @@ -821,11 +821,11 @@ fn real_clip_t8x8_bd_psnr() { // --------------------------------------------------------------------------- /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured -/// -2.442 dB against x264 there on 2026-08-23 (352x288, 24 pictures, both at +/// -2.640 dB against x264 there on 2026-08-23 (352x288, 24 pictures, both at /// their own default effort: our `Preset::Fast`, x264's `-preset medium`). /// The floor is that number with room for platform noise, so the distance can -/// shrink but not grow unnoticed; it is not a claim that -2.4 dB is fine. -const BD_PSNR_VS_X264_FLOOR: f64 = -2.60; +/// shrink but not grow unnoticed; it is not a claim that -2.6 dB is fine. +const BD_PSNR_VS_X264_FLOOR: f64 = -2.80; fn have_x264() -> bool { std::process::Command::new("ffmpeg") @@ -839,7 +839,7 @@ fn have_x264() -> bool { /// reference, no B pictures, the same GOP, and adaptive quantisation and psy /// off so both encoders spend bits by the same rule. Returns the Annex B /// stream. -fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32) -> Option> { +fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32, gop: u32) -> Option> { let dir = std::env::temp_dir().join(format!("ec-h264-x264-{}", std::process::id())); std::fs::create_dir_all(&dir).ok()?; let raw = dir.join(format!("in-q{qp}.yuv")); @@ -859,8 +859,14 @@ fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32) -> Option Option cargo test --release -p ec-h264 --test encode \ @@ -912,10 +924,14 @@ fn bd_psnr_vs_x264() { } }; + let gop: u32 = std::env::var("EC_H264_GOP") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(10); let ours = |qp: i32| { let mut cfg = EncoderConfig::new(w as u32, h as u32); cfg.qp = qp; - cfg.gop_size = 10; + cfg.gop_size = gop; cfg.cabac = true; cfg.framerate = 1.0; cfg.threads = 0; @@ -937,10 +953,8 @@ fn bd_psnr_vs_x264() { .zip(&decoded) .map(|((y, _, _), (dy, _, _))| psnr(y, dy)) .sum(); - let (decoded_psnr, recon_psnr) = ( - sum / sources.len() as f64, - sum_recon / sources.len() as f64, - ); + let (decoded_psnr, recon_psnr) = + (sum / sources.len() as f64, sum_recon / sources.len() as f64); assert!( (decoded_psnr - recon_psnr).abs() < 0.05, "qp {qp}: our reconstruction reads {recon_psnr:.2} dB but our own \ @@ -949,7 +963,7 @@ fn bd_psnr_vs_x264() { ((stream.len() * 8) as f64, decoded_psnr) }; let x264 = |qp: i32| { - let stream = x264_encode_qp(&sources, w, h, qp).expect("x264 encodes"); + let stream = x264_encode_qp(&sources, w, h, qp, gop).expect("x264 encodes"); let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes x264's own stream"); assert_eq!( decoded.len(), @@ -984,7 +998,7 @@ fn bd_psnr_vs_x264() { // The floor is calibrated on the synthetic clip; a clip of the user's is a // measurement, not a gate, because its content is not pinned. - if std::env::var_os("EC_H264_CLIP").is_none() { + if std::env::var_os("EC_H264_CLIP").is_none() && gop == 10 { assert!( bd > BD_PSNR_VS_X264_FLOOR, "BD-PSNR against x264 fell to {bd:+.3} dB, past the \