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() {
821 // ---------------------------------------------------------------------------821 // ---------------------------------------------------------------------------
822 822
823 /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured823 /// 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 at824+/// -2.640 dB against x264 there on 2026-08-23 (352x288, 24 pictures, both at
825 /// their own default effort: our `Preset::Fast`, x264's `-preset medium`).825 /// their own default effort: our `Preset::Fast`, x264's `-preset medium`).
826 /// The floor is that number with room for platform noise, so the distance can826 /// 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.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.60;828+const BD_PSNR_VS_X264_FLOOR: f64 = -2.80;
829 829
830 fn have_x264() -> bool {830 fn have_x264() -> bool {
831 std::process::Command::new("ffmpeg")831 std::process::Command::new("ffmpeg")
@@ -839,7 +839,7 @@ fn have_x264() -> bool {
839 /// reference, no B pictures, the same GOP, and adaptive quantisation and psy839 /// reference, no B pictures, the same GOP, and adaptive quantisation and psy
840 /// off so both encoders spend bits by the same rule. Returns the Annex B840 /// off so both encoders spend bits by the same rule. Returns the Annex B
841 /// stream.841 /// 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>> {
843 let dir = std::env::temp_dir().join(format!("ec-h264-x264-{}", std::process::id()));843 let dir = std::env::temp_dir().join(format!("ec-h264-x264-{}", std::process::id()));
844 std::fs::create_dir_all(&dir).ok()?;844 std::fs::create_dir_all(&dir).ok()?;
845 let raw = dir.join(format!("in-q{qp}.yuv"));845 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
859 .arg(&raw)859 .arg(&raw)
860 .args(["-c:v", "libx264", "-preset", "medium"])860 .args(["-c:v", "libx264", "-preset", "medium"])
861 .args(["-profile:v", "main", "-qp", &qp.to_string()])861 .args(["-profile:v", "main", "-qp", &qp.to_string()])
862- .args(["-g", "10", "-bf", "0", "-refs", "1"])862+ .args(["-g", &gop.to_string(), "-bf", "0", "-refs", "1"])
863- .args(["-x264-params", "scenecut=0:aq-mode=0:psy=0:8x8dct=0"])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+ ])
864 .args(["-f", "h264"])870 .args(["-f", "h264"])
865 .arg(&out)871 .arg(&out)
866 .output()872 .output()
@@ -880,9 +886,15 @@ fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32) -> Option<Vec
880 /// rot behind an `#[ignore]`; point `EC_H264_CLIP` at a media file to measure886 /// rot behind an `#[ignore]`; point `EC_H264_CLIP` at a media file to measure
881 /// the same thing on real content.887 /// the same thing on real content.
882 ///888 ///
883-/// Both curves are (bits, luma PSNR against the same source pictures). Ours889+/// Both curves are (bits, luma PSNR against the same source pictures), and
884-/// uses the encoder's own reconstruction; x264's stream is decoded back with890+/// both are measured on ffmpeg's decode of the stream rather than on an
885-/// ffmpeg, so its PSNR is measured on what a decoder would actually show.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.
886 ///898 ///
887 /// cargo test --release -p ec-h264 --test encode bd_psnr_vs_x264 -- --nocapture899 /// cargo test --release -p ec-h264 --test encode bd_psnr_vs_x264 -- --nocapture
888 /// EC_H264_CLIP=<file> cargo test --release -p ec-h264 --test encode \900 /// EC_H264_CLIP=<file> cargo test --release -p ec-h264 --test encode \
@@ -912,10 +924,14 @@ fn bd_psnr_vs_x264() {
912 }924 }
913 };925 };
914 926
927+ let gop: u32 = std::env::var("EC_H264_GOP")
928+ .ok()
929+ .and_then(|v| v.parse().ok())
930+ .unwrap_or(10);
915 let ours = |qp: i32| {931 let ours = |qp: i32| {
916 let mut cfg = EncoderConfig::new(w as u32, h as u32);932 let mut cfg = EncoderConfig::new(w as u32, h as u32);
917 cfg.qp = qp;933 cfg.qp = qp;
918- cfg.gop_size = 10;934+ cfg.gop_size = gop;
919 cfg.cabac = true;935 cfg.cabac = true;
920 cfg.framerate = 1.0;936 cfg.framerate = 1.0;
921 cfg.threads = 0;937 cfg.threads = 0;
@@ -937,10 +953,8 @@ fn bd_psnr_vs_x264() {
937 .zip(&decoded)953 .zip(&decoded)
938 .map(|((y, _, _), (dy, _, _))| psnr(y, dy))954 .map(|((y, _, _), (dy, _, _))| psnr(y, dy))
939 .sum();955 .sum();
940- let (decoded_psnr, recon_psnr) = (956+ let (decoded_psnr, recon_psnr) =
941- sum / sources.len() as f64,957+ (sum / sources.len() as f64, sum_recon / sources.len() as f64);
942- sum_recon / sources.len() as f64,
943- );
944 assert!(958 assert!(
945 (decoded_psnr - recon_psnr).abs() < 0.05,959 (decoded_psnr - recon_psnr).abs() < 0.05,
946 "qp {qp}: our reconstruction reads {recon_psnr:.2} dB but our own \960 "qp {qp}: our reconstruction reads {recon_psnr:.2} dB but our own \
@@ -949,7 +963,7 @@ fn bd_psnr_vs_x264() {
949 ((stream.len() * 8) as f64, decoded_psnr)963 ((stream.len() * 8) as f64, decoded_psnr)
950 };964 };
951 let x264 = |qp: i32| {965 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");
953 let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes x264's own stream");967 let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes x264's own stream");
954 assert_eq!(968 assert_eq!(
955 decoded.len(),969 decoded.len(),
@@ -984,7 +998,7 @@ fn bd_psnr_vs_x264() {
984 998
985 // The floor is calibrated on the synthetic clip; a clip of the user's is a999 // The floor is calibrated on the synthetic clip; a clip of the user's is a
986 // measurement, not a gate, because its content is not pinned.1000 // 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 {
988 assert!(1002 assert!(
989 bd > BD_PSNR_VS_X264_FLOOR,1003 bd > BD_PSNR_VS_X264_FLOOR,
990 "BD-PSNR against x264 fell to {bd:+.3} dB, past the \1004 "BD-PSNR against x264 fell to {bd:+.3} dB, past the \