Match x264's quantiser, and split the gap intra vs inter
Browse files at this commitcd63f57fb218cbcf3f7f1a51f70b53639cc22f3cParent: 5e93348- fix
- match x264's quantiser, and split the gap intra vs inter
1 changed file +30 -16
- crates/ec-h264/tests/encode.rs +30 -16
crates/ec-h264/tests/encode.rsmodified+30 -16
| @@ -821,11 +821,11 @@ fn real_clip_t8x8_bd_psnr() { | ||
| 821 | 821 | // --------------------------------------------------------------------------- |
| 822 | 822 | |
| 823 | 823 | /// 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 | |
| 825 | 825 | /// their own default effort: our `Preset::Fast`, x264's `-preset medium`). |
| 826 | 826 | /// 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; | |
| 829 | 829 | |
| 830 | 830 | fn have_x264() -> bool { |
| 831 | 831 | std::process::Command::new("ffmpeg") |
| @@ -839,7 +839,7 @@ fn have_x264() -> bool { | ||
| 839 | 839 | /// reference, no B pictures, the same GOP, and adaptive quantisation and psy |
| 840 | 840 | /// off so both encoders spend bits by the same rule. Returns the Annex B |
| 841 | 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 | 843 | let dir = std::env::temp_dir().join(format!("ec-h264-x264-{}", std::process::id())); |
| 844 | 844 | std::fs::create_dir_all(&dir).ok()?; |
| 845 | 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 | 859 | .arg(&raw) |
| 860 | 860 | .args(["-c:v", "libx264", "-preset", "medium"]) |
| 861 | 861 | .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 | + ]) | |
| 864 | 870 | .args(["-f", "h264"]) |
| 865 | 871 | .arg(&out) |
| 866 | 872 | .output() |
| @@ -880,9 +886,15 @@ fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32) -> Option<Vec | ||
| 880 | 886 | /// rot behind an `#[ignore]`; point `EC_H264_CLIP` at a media file to measure |
| 881 | 887 | /// the same thing on real content. |
| 882 | 888 | /// |
| 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. | |
| 886 | 898 | /// |
| 887 | 899 | /// cargo test --release -p ec-h264 --test encode bd_psnr_vs_x264 -- --nocapture |
| 888 | 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 | 931 | let ours = |qp: i32| { |
| 916 | 932 | let mut cfg = EncoderConfig::new(w as u32, h as u32); |
| 917 | 933 | cfg.qp = qp; |
| 918 | - cfg.gop_size = 10; | |
| 934 | + cfg.gop_size = gop; | |
| 919 | 935 | cfg.cabac = true; |
| 920 | 936 | cfg.framerate = 1.0; |
| 921 | 937 | cfg.threads = 0; |
| @@ -937,10 +953,8 @@ fn bd_psnr_vs_x264() { | ||
| 937 | 953 | .zip(&decoded) |
| 938 | 954 | .map(|((y, _, _), (dy, _, _))| psnr(y, dy)) |
| 939 | 955 | .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); | |
| 944 | 958 | assert!( |
| 945 | 959 | (decoded_psnr - recon_psnr).abs() < 0.05, |
| 946 | 960 | "qp {qp}: our reconstruction reads {recon_psnr:.2} dB but our own \ |
| @@ -949,7 +963,7 @@ fn bd_psnr_vs_x264() { | ||
| 949 | 963 | ((stream.len() * 8) as f64, decoded_psnr) |
| 950 | 964 | }; |
| 951 | 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 | 967 | let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes x264's own stream"); |
| 954 | 968 | assert_eq!( |
| 955 | 969 | decoded.len(), |
| @@ -984,7 +998,7 @@ fn bd_psnr_vs_x264() { | ||
| 984 | 998 | |
| 985 | 999 | // The floor is calibrated on the synthetic clip; a clip of the user's is a |
| 986 | 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 | 1002 | assert!( |
| 989 | 1003 | bd > BD_PSNR_VS_X264_FLOOR, |
| 990 | 1004 | "BD-PSNR against x264 fell to {bd:+.3} dB, past the \ |