Gate on content prediction can win, not on noise
Browse files at this commit5370748fe84b27001b864fda4d53aa81c9c115beParent: cd63f57- test
- gate on content prediction can win, not on noise
1 changed file +57 -6
crates/ec-h264/tests/encode.rsmodified+57 -6
| @@ -58,6 +58,48 @@ impl Clip { | |||
| 58 | } | 58 | } |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | + /// Frame `t` without the per-sample noise field: smooth gradients, a | ||
| 62 | + /// band of sinusoidal texture and a box translating by whole and | ||
| 63 | + /// sub-sample amounts. `render`'s noise is incompressible, which makes a | ||
| 64 | + /// rate-quality comparison read mostly as how willing each encoder is to | ||
| 65 | + /// throw residual away; this one is content where prediction matters, the | ||
| 66 | + /// half a rate-quality comparison is about. | ||
| 67 | + fn render_smooth(&mut self, t: usize) { | ||
| 68 | + let (w, h) = (self.w, self.h); | ||
| 69 | + let ft = t as f64; | ||
| 70 | + for y in 0..h { | ||
| 71 | + for x in 0..w { | ||
| 72 | + let (fx, fy) = (x as f64, y as f64); | ||
| 73 | + // A slow diagonal ramp that drifts by 1.5 samples a frame, so | ||
| 74 | + // motion is not always on the sample grid. | ||
| 75 | + let ramp = (fx + fy * 0.5 - ft * 1.5) * 0.35; | ||
| 76 | + // A textured band, the part a search has to actually find. | ||
| 77 | + let texture = if y > h / 3 && y < 2 * h / 3 { | ||
| 78 | + 18.0 * ((fx * 0.19 + ft * 0.9).sin() + (fy * 0.11).cos()) | ||
| 79 | + } else { | ||
| 80 | + 0.0 | ||
| 81 | + }; | ||
| 82 | + let box_x = ((ft * 2.7) as usize) % (w.saturating_sub(48).max(1)); | ||
| 83 | + let inside = x >= box_x && x < box_x + 48 && y >= h / 5 && y < h / 5 + 36; | ||
| 84 | + let v = if inside { | ||
| 85 | + 196.0 - texture * 0.5 | ||
| 86 | + } else { | ||
| 87 | + 40.0 + (ramp % 90.0).abs() + texture | ||
| 88 | + }; | ||
| 89 | + self.y[y * w + x] = v.clamp(0.0, 255.0) as u8; | ||
| 90 | + } | ||
| 91 | + } | ||
| 92 | + for y in 0..h / 2 { | ||
| 93 | + for x in 0..w / 2 { | ||
| 94 | + let (fx, fy) = (x as f64, y as f64); | ||
| 95 | + self.u[y * (w / 2) + x] = | ||
| 96 | + (118.0 + 12.0 * ((fx * 0.05 + ft * 0.3).sin())).clamp(0.0, 255.0) as u8; | ||
| 97 | + self.v[y * (w / 2) + x] = | ||
| 98 | + (134.0 + 10.0 * ((fy * 0.04 - ft * 0.25).cos())).clamp(0.0, 255.0) as u8; | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + | ||
| 61 | fn view(&self) -> PictureView<'_> { | 103 | fn view(&self) -> PictureView<'_> { |
| 62 | PictureView::i420(self.w as u32, self.h as u32, &self.y, &self.u, &self.v) | 104 | PictureView::i420(self.w as u32, self.h as u32, &self.y, &self.u, &self.v) |
| 63 | } | 105 | } |
| @@ -821,11 +863,12 @@ fn real_clip_t8x8_bd_psnr() { | |||
| 821 | // --------------------------------------------------------------------------- | 863 | // --------------------------------------------------------------------------- |
| 822 | 864 | ||
| 823 | /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured | 865 | /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured |
| 824 | -/// -2.640 dB against x264 there on 2026-08-23 (352x288, 24 pictures, both at | 866 | +/// -5.202 dB against x264 there on 2026-08-23 (352x288, 24 pictures of |
| 825 | -/// their own default effort: our `Preset::Fast`, x264's `-preset medium`). | 867 | +/// `Clip::render_smooth`, both encoders at their own default effort: our |
| 826 | -/// The floor is that number with room for platform noise, so the distance can | 868 | +/// `Preset::Fast`, x264's `-preset medium`). The floor is that number with |
| 827 | -/// shrink but not grow unnoticed; it is not a claim that -2.6 dB is fine. | 869 | +/// room for platform noise, so the distance can shrink but not grow |
| 828 | -const BD_PSNR_VS_X264_FLOOR: f64 = -2.80; | 870 | +/// unnoticed; it is not a claim that -5.2 dB is fine. |
| 871 | +const BD_PSNR_VS_X264_FLOOR: f64 = -5.40; | ||
| 829 | 872 | ||
| 830 | fn have_x264() -> bool { | 873 | fn have_x264() -> bool { |
| 831 | std::process::Command::new("ffmpeg") | 874 | std::process::Command::new("ffmpeg") |
| @@ -896,6 +939,11 @@ fn x264_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32, gop: u32) -> | |||
| 896 | /// clip at GOP 10 is -2.992 dB (+19% bits), so roughly two thirds of the | 939 | /// 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. | 940 | /// distance is in P pictures, not in the intra path. |
| 898 | /// | 941 | /// |
| 942 | +/// `EC_H264_PRESET_BALANCED` runs our half of the comparison at | ||
| 943 | +/// `Preset::Balanced` instead of the default `Preset::Fast`, which separates | ||
| 944 | +/// what search effort buys from what the coder itself costs: on the same | ||
| 945 | +/// library clip it moves -2.992 dB to -1.587 dB for 40% more encode time. | ||
| 946 | +/// | ||
| 899 | /// cargo test --release -p ec-h264 --test encode bd_psnr_vs_x264 -- --nocapture | 947 | /// cargo test --release -p ec-h264 --test encode bd_psnr_vs_x264 -- --nocapture |
| 900 | /// EC_H264_CLIP=<file> cargo test --release -p ec-h264 --test encode \ | 948 | /// EC_H264_CLIP=<file> cargo test --release -p ec-h264 --test encode \ |
| 901 | /// bd_psnr_vs_x264 -- --nocapture | 949 | /// bd_psnr_vs_x264 -- --nocapture |
| @@ -916,7 +964,7 @@ fn bd_psnr_vs_x264() { | |||
| 916 | let mut clip = Clip::new(w, h); | 964 | let mut clip = Clip::new(w, h); |
| 917 | let mut sources = Vec::with_capacity(frames); | 965 | let mut sources = Vec::with_capacity(frames); |
| 918 | for t in 0..frames { | 966 | for t in 0..frames { |
| 919 | - clip.render(t); | 967 | + clip.render_smooth(t); |
| 920 | sources.push((clip.y.clone(), clip.u.clone(), clip.v.clone())); | 968 | sources.push((clip.y.clone(), clip.u.clone(), clip.v.clone())); |
| 921 | } | 969 | } |
| 922 | eprintln!("synthetic clip: {w}x{h}, {frames} pictures"); | 970 | eprintln!("synthetic clip: {w}x{h}, {frames} pictures"); |
| @@ -932,6 +980,9 @@ fn bd_psnr_vs_x264() { | |||
| 932 | let mut cfg = EncoderConfig::new(w as u32, h as u32); | 980 | let mut cfg = EncoderConfig::new(w as u32, h as u32); |
| 933 | cfg.qp = qp; | 981 | cfg.qp = qp; |
| 934 | cfg.gop_size = gop; | 982 | cfg.gop_size = gop; |
| 983 | + if std::env::var_os("EC_H264_PRESET_BALANCED").is_some() { | ||
| 984 | + cfg.preset = ec_h264::Preset::Balanced; | ||
| 985 | + } | ||
| 935 | cfg.cabac = true; | 986 | cfg.cabac = true; |
| 936 | cfg.framerate = 1.0; | 987 | cfg.framerate = 1.0; |
| 937 | cfg.threads = 0; | 988 | cfg.threads = 0; |