forge
Explore
Log in

Don't you dare go Hollow

Match x264's quantiser, and split the gap intra vs inter

Browse files at this commit

cd63f57fb218cbcf3f7f1a51f70b53639cc22f3cAhmet Kaan GümüşParent: 5e93348
  • fix
    • match x264's quantiser, and split the gap intra vs inter
Show full message
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.rsmodified+30 -16
@@ -821,11 +821,11 @@ fn real_clip_t8x8_bd_psnr() {
821821 // ---------------------------------------------------------------------------
822822
823823 /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured
824-/// -2.442 dB against x264 there on 2026-08-23 (352x288, 24 pictures, both at
824+/// -2.640 dB against x264 there on 2026-08-23 (352x288, 24 pictures, both at
825825 /// their own default effort: our `Preset::Fast`, x264's `-preset medium`).
826826 /// The floor is that number with room for platform noise, so the distance can
827-/// shrink but not grow unnoticed; it is not a claim that -2.4 dB is fine.
828-const BD_PSNR_VS_X264_FLOOR: f64 = -2.60;
827+/// shrink but not grow unnoticed; it is not a claim that -2.6 dB is fine.
828+const BD_PSNR_VS_X264_FLOOR: f64 = -2.80;
829829
830830 fn have_x264() -> bool {
831831 std::process::Command::new("ffmpeg")
@@ -839,7 +839,7 @@ fn have_x264() -> bool {
839839 /// reference, no B pictures, the same GOP, and adaptive quantisation and psy
840840 /// off so both encoders spend bits by the same rule. Returns the Annex B
841841 /// stream.
842-fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32) -> Option<Vec<u8>> {
842+fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32, gop: u32) -> Option<Vec<u8>> {
843843 let dir = std::env::temp_dir().join(format!("ec-h264-x264-{}", std::process::id()));
844844 std::fs::create_dir_all(&dir).ok()?;
845845 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<Vec
859859 .arg(&raw)
860860 .args(["-c:v", "libx264", "-preset", "medium"])
861861 .args(["-profile:v", "main", "-qp", &qp.to_string()])
862- .args(["-g", "10", "-bf", "0", "-refs", "1"])
863- .args(["-x264-params", "scenecut=0:aq-mode=0:psy=0:8x8dct=0"])
862+ .args(["-g", &gop.to_string(), "-bf", "0", "-refs", "1"])
863+ // ipratio/pbratio 1.0 matter: x264's constant-QP mode otherwise codes
864+ // I pictures at a lower QP than the one asked for, which reads as a
865+ // rate-quality win that is really a different quantiser.
866+ .args([
867+ "-x264-params",
868+ "scenecut=0:aq-mode=0:psy=0:8x8dct=0:ipratio=1.0:pbratio=1.0:qcomp=1.0:chroma-qp-offset=0",
869+ ])
864870 .args(["-f", "h264"])
865871 .arg(&out)
866872 .output()
@@ -880,9 +886,15 @@ fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32) -> Option<Vec
880886 /// rot behind an `#[ignore]`; point `EC_H264_CLIP` at a media file to measure
881887 /// the same thing on real content.
882888 ///
883-/// Both curves are (bits, luma PSNR against the same source pictures). Ours
884-/// uses the encoder's own reconstruction; x264's stream is decoded back with
885-/// ffmpeg, so its PSNR is measured on what a decoder would actually show.
889+/// Both curves are (bits, luma PSNR against the same source pictures), and
890+/// both are measured on ffmpeg's decode of the stream rather than on an
891+/// encoder's own reconstruction, so the two halves are read the same way.
892+///
893+/// `EC_H264_GOP` sets the GOP of both encoders; 1 makes the comparison
894+/// all-intra, which is how the gap was split: on a 3840x1608 clip from the
895+/// library the intra-only distance is -0.749 dB (+2-4% bits) while the same
896+/// clip at GOP 10 is -2.992 dB (+19% bits), so roughly two thirds of the
897+/// distance is in P pictures, not in the intra path.
886898 ///
887899 /// cargo test --release -p ec-h264 --test encode bd_psnr_vs_x264 -- --nocapture
888900 /// EC_H264_CLIP=<file> cargo test --release -p ec-h264 --test encode \
@@ -912,10 +924,14 @@ fn bd_psnr_vs_x264() {
912924 }
913925 };
914926
927+ let gop: u32 = std::env::var("EC_H264_GOP")
928+ .ok()
929+ .and_then(|v| v.parse().ok())
930+ .unwrap_or(10);
915931 let ours = |qp: i32| {
916932 let mut cfg = EncoderConfig::new(w as u32, h as u32);
917933 cfg.qp = qp;
918- cfg.gop_size = 10;
934+ cfg.gop_size = gop;
919935 cfg.cabac = true;
920936 cfg.framerate = 1.0;
921937 cfg.threads = 0;
@@ -937,10 +953,8 @@ fn bd_psnr_vs_x264() {
937953 .zip(&decoded)
938954 .map(|((y, _, _), (dy, _, _))| psnr(y, dy))
939955 .sum();
940- let (decoded_psnr, recon_psnr) = (
941- sum / sources.len() as f64,
942- sum_recon / sources.len() as f64,
943- );
956+ let (decoded_psnr, recon_psnr) =
957+ (sum / sources.len() as f64, sum_recon / sources.len() as f64);
944958 assert!(
945959 (decoded_psnr - recon_psnr).abs() < 0.05,
946960 "qp {qp}: our reconstruction reads {recon_psnr:.2} dB but our own \
@@ -949,7 +963,7 @@ fn bd_psnr_vs_x264() {
949963 ((stream.len() * 8) as f64, decoded_psnr)
950964 };
951965 let x264 = |qp: i32| {
952- let stream = x264_encode_qp(&sources, w, h, qp).expect("x264 encodes");
966+ let stream = x264_encode_qp(&sources, w, h, qp, gop).expect("x264 encodes");
953967 let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes x264's own stream");
954968 assert_eq!(
955969 decoded.len(),
@@ -984,7 +998,7 @@ fn bd_psnr_vs_x264() {
984998
985999 // The floor is calibrated on the synthetic clip; a clip of the user's is a
9861000 // measurement, not a gate, because its content is not pinned.
987- if std::env::var_os("EC_H264_CLIP").is_none() {
1001+ if std::env::var_os("EC_H264_CLIP").is_none() && gop == 10 {
9881002 assert!(
9891003 bd > BD_PSNR_VS_X264_FLOOR,
9901004 "BD-PSNR against x264 fell to {bd:+.3} dB, past the \