From f7bce7deb24bfae0122aed8bd9738ef4c653e276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 24 Aug 2026 00:12:04 +0300 Subject: [PATCH] perf(h264): iterate sub-sample refinement to convergence at every preset The refinement stopped after a single pass at Fast. Running it to convergence is worth 0.111 dB BD-PSNR against x264 on the film clip and 0.035 on the screen capture, for 2% of the comparison's wall clock; the synthetic floor tightens to -4.5. Attribution of what is left, measured by disabling x264's features one at a time: its whole-sample search pattern is worth 0.04 dB, its rate-distortion quantisation 0.20, and its sub-sample and mode refinement 0.83 -- so the rate-distortion mode decision is where the remaining distance is. --- crates/ec-h264/src/enc/mb.rs | 8 +++++--- crates/ec-h264/tests/encode.rs | 2 +- lanes/h264x264-r2.report.md | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/ec-h264/src/enc/mb.rs b/crates/ec-h264/src/enc/mb.rs index ad4025d0..7c97cc62 100644 --- a/crates/ec-h264/src/enc/mb.rs +++ b/crates/ec-h264/src/enc/mb.rs @@ -642,6 +642,11 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter // capture. The whole-sample winner is re-costed in the same domain // first so the two halves of the search compare. best.1 = cost_of(best.0, true); + // Both sub-sample steps iterate to convergence at every preset. + // Stopping the refinement after one pass at Fast, which is what + // this used to do, costs 0.111 dB BD-PSNR on the film clip and + // 0.035 on the screen capture for 2% of the comparison's wall + // clock. for step in [2i16, 1] { let mut improved = true; while improved { @@ -663,9 +668,6 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter improved = true; } } - if e.preset == Preset::Fast { - break; - } } } } diff --git a/crates/ec-h264/tests/encode.rs b/crates/ec-h264/tests/encode.rs index 14765b06..41cc4dd7 100644 --- a/crates/ec-h264/tests/encode.rs +++ b/crates/ec-h264/tests/encode.rs @@ -896,7 +896,7 @@ fn real_clip_t8x8_bd_psnr() { /// signal that the decision belongs on real coded cost rather than on a SATD /// comparison at all -- that is the next slice, not a reason to tune the /// threshold back toward this clip. -const BD_PSNR_VS_X264_FLOOR: f64 = -4.6; +const BD_PSNR_VS_X264_FLOOR: f64 = -4.5; fn have_x264() -> bool { std::process::Command::new("ffmpeg") diff --git a/lanes/h264x264-r2.report.md b/lanes/h264x264-r2.report.md index fb24beee..5b661975 100644 --- a/lanes/h264x264-r2.report.md +++ b/lanes/h264x264-r2.report.md @@ -117,3 +117,26 @@ screen capture**, at an 8x8 macroblock share of 17.3% and 14.2%. It is now a win on both contents; what is still owed is the decision to flip the default, because that changes every default stream byte and re-pins the byte-identity tests. + +## Addendum: where the rest of the gap is + +With x264's own features disabled one at a time, on consecutive pictures +(ours reads -1.154 film / -0.696 screen capture): + +| x264 running | film | screen | +| ---------------- | -----: | ------: | +| as configured | -1.154 | -0.696 | +| `trellis=0` | -0.959 | -0.594 | +| `me=dia` | -1.115 | -0.696 | +| `subme=4` | -0.905 | -0.579 | +| `subme=1` | -0.324 | -0.244 | + +The whole-sample search pattern is worth 0.04 dB and 0.00; rate-distortion +quantisation is worth 0.20 and 0.10; **sub-sample and mode refinement is worth +0.83 and 0.45** -- almost the entire remaining gap. x264's `subme` 6 and 7 are +its rate-distortion mode decision, which is the named next lane. + +Iterating our own sub-sample refinement to convergence at `Fast` (it stopped +after one pass) is the first 0.111 dB of that, for 2% of the comparison's wall +clock: film -1.154 -> -1.043, screen capture -0.696 -> -0.661, synthetic +-4.542 -> -4.449.