From f0a6733bb7622f9416fb8d25e5a46ccd9226faef 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 18:07:53 +0300 Subject: [PATCH] perf(h264): try Intra_4x4 in P pictures at every preset The BD-PSNR gate against x264 put us 2.992 dB behind on a 3840x1608 clip from the library at Preset::Fast, and 1.587 dB behind at Preset::Balanced. Ablating the four places the two presets differ shows the whole difference sits in one of them: allowing Intra_4x4 in P pictures reads -1.557 dB on its own, while a 48-sample search range (-3.039 dB), the half-macroblock partitions (-3.009 dB) and the subpel refinement (-3.035 dB) each land inside the noise of the -2.992 dB baseline. So Intra_4x4 is now tried in P pictures at every preset. The cost is about 5% encode time (21.75 s against 20.67 s for the 24-picture measurement), which is what a 1.4 dB rate-quality move is worth. Against x264 on that clip we now spend 5-10% more bits instead of 19%, at 0.3-1.3 dB lower PSNR instead of 1.0-1.7 dB. The synthetic floor moves with the default: -5.202 dB to -5.140 dB, and the constant is retightened to -5.25. No test in the repo pins encoder bytes, so nothing else re-pins. --- crates/ec-h264/src/enc/mb.rs | 14 +++++++++++--- crates/ec-h264/tests/encode.rs | 8 +++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/ec-h264/src/enc/mb.rs b/crates/ec-h264/src/enc/mb.rs index 724168be..4b36ebb0 100644 --- a/crates/ec-h264/src/enc/mb.rs +++ b/crates/ec-h264/src/enc/mb.rs @@ -50,10 +50,10 @@ use super::{ /// the two edith actually picks between. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum Preset { - /// Diamond motion search, 16x16 partitions, intra 4x4 in I pictures only. + /// Diamond motion search, 16x16 partitions. #[default] Fast, - /// Wider search with a hexagon refinement and intra 4x4 everywhere. + /// Wider search with a hexagon refinement and half-macroblock partitions. Balanced, } @@ -434,7 +434,15 @@ fn intra_pre_cost( IntraCost { i16_mode: best.1, i16_cost: best.0, - allow_i4: e.slice_type == SliceType::I || e.preset == Preset::Balanced, + // Intra_4x4 is tried in P pictures too, at every preset. Measured + // against x264 on a 3840x1608 clip from the library (BD-PSNR over QP + // 22/26/30/34, matched features): restricting it to I pictures cost + // -2.992 dB against x264 where allowing it everywhere reads -1.557 dB, + // for 5% more encode time. Nothing else in the Fast/Balanced split + // came close -- a 48-sample search range read -3.039 dB and the + // half-macroblock partitions -3.009 dB, both inside the noise of the + // -2.992 dB baseline. + allow_i4: true, } } diff --git a/crates/ec-h264/tests/encode.rs b/crates/ec-h264/tests/encode.rs index 3bd099be..1160b090 100644 --- a/crates/ec-h264/tests/encode.rs +++ b/crates/ec-h264/tests/encode.rs @@ -863,12 +863,14 @@ fn real_clip_t8x8_bd_psnr() { // --------------------------------------------------------------------------- /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured -/// -5.202 dB against x264 there on 2026-08-23 (352x288, 24 pictures of +/// -5.140 dB against x264 there on 2026-08-23 (352x288, 24 pictures of /// `Clip::render_smooth`, both encoders 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 -5.2 dB is fine. -const BD_PSNR_VS_X264_FLOOR: f64 = -5.40; +/// unnoticed; it is not a claim that -5.1 dB is fine. On a 3840x1608 clip +/// from the library the same measurement reads -1.557 dB, so this synthetic +/// number is the pessimistic end of the range, not the typical one. +const BD_PSNR_VS_X264_FLOOR: f64 = -5.25; fn have_x264() -> bool { std::process::Command::new("ffmpeg")