From b24f633ab66a169b1c2d6f31b5f0f8d66c461785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 25 Aug 2026 03:59:09 +0300 Subject: [PATCH] perf(h265): give five intra candidates the rate-distortion trial The luma mode search screened 35 intra modes with SATD and handed only two survivors to the full rate-distortion trial. That count was never measured; it was the number the search was written with. Swept against x265 at matched features on four real clips (BD-PSNR luma / all-plane, 24 pictures after 1500, ladders auto-aligned, 0% extrapolation): | k | 4K film | 1138x640 web | 1440p screen capture | 1080p phone | |---|---------|--------------|----------------------|-------------| | 1 | -0.840 / -0.815 | -0.728 / -0.733 | | | | 2 | -0.731 / -0.730 | -0.603 / -0.632 | -2.521 / -2.420 | -0.435 / -0.444 | | 3 | -0.703 / -0.709 | -0.556 / -0.596 | -2.493 / -2.402 | -0.401 / -0.416 | | 4 | -0.693 / -0.705 | -0.530 / -0.574 | | | | 5 | -0.685 / -0.703 | -0.507 / -0.556 | -2.465 / -2.360 | -0.366 / -0.387 | | 8 | -0.684 / -0.710 | -0.471 / -0.525 | -2.453 / -2.368 | -0.340 / -0.361 | Every clip improves monotonically to five. Eight buys +0.001 dB on the film clip and takes its all-plane figure backwards, for another 20% of wall time, so the knee is where the default now sits: film 38s -> 49s for the whole four-QP ladder, web clip 8s -> 11s. The synthetic floor moves with it (+2.456 -> +2.507 luma, +3.158 -> +3.238 all-plane) and EC_H265_RDO_CANDIDATES in the gate test re-runs the sweep. --- crates/ec-h265/src/encoder.rs | 20 +++++++++++++++++++- crates/ec-h265/tests/encode.rs | 15 +++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/crates/ec-h265/src/encoder.rs b/crates/ec-h265/src/encoder.rs index a7ac1251..f15fb466 100644 --- a/crates/ec-h265/src/encoder.rs +++ b/crates/ec-h265/src/encoder.rs @@ -75,6 +75,24 @@ pub struct EncoderConfig { pub ctb_size: usize, /// How many of the 35 intra modes get a full rate-distortion trial, on top /// of the most probable mode, which always gets one. + /// + /// Swept against x265 at matched features on four real clips (BD-PSNR + /// luma / all-plane, wall time for the whole four-QP ladder): + /// + /// | k | film 3840x1608 | web 1138x640 | screen 2560x1440 | phone 1080p | + /// |---|----------------|-------------------|------------------|-------------| + /// | 1 | -0.840 / -0.815 38s | -0.728 / -0.733 8s | | | + /// | 2 | -0.731 / -0.730 40s | -0.603 / -0.632 9s | -2.521 / -2.420 29s | -0.435 / -0.444 11s | + /// | 3 | -0.703 / -0.709 43s | -0.556 / -0.596 10s | -2.493 / -2.402 31s | -0.401 / -0.416 11s | + /// | 4 | -0.693 / -0.705 46s | -0.530 / -0.574 10s | | | + /// | 5 | -0.685 / -0.703 49s | -0.507 / -0.556 11s | -2.465 / -2.360 35s | -0.366 / -0.387 13s | + /// | 8 | -0.684 / -0.710 59s | -0.471 / -0.525 14s | -2.453 / -2.368 41s | -0.340 / -0.361 16s | + /// + /// Every clip improves monotonically up to 5, which is why 5 is the + /// default. Eight buys nothing on the film clip (+0.001 luma, and its + /// all-plane figure goes *backwards*) for another 20% of wall time, so the + /// knee is where the default sits. `EC_H265_RDO_CANDIDATES` in the gate + /// test re-runs the sweep. pub rdo_candidates: usize, /// Smallest coding unit the search will produce, 8 or 16. Sixteen is a /// *coarser* search that is not always faster: on real pictures the 8x8 @@ -106,7 +124,7 @@ impl EncoderConfig { rate_control: RateControl::ConstantQp(27), threads: 0, ctb_size: 32, - rdo_candidates: 2, + rdo_candidates: 5, min_cu_size: 8, video_signal_type: None, sample_aspect_ratio: None, diff --git a/crates/ec-h265/tests/encode.rs b/crates/ec-h265/tests/encode.rs index c00c16ea..5c7b0707 100644 --- a/crates/ec-h265/tests/encode.rs +++ b/crates/ec-h265/tests/encode.rs @@ -16,7 +16,7 @@ type Planes = (Vec, Vec, Vec); /// measured value with 0.15 dB of room for platform noise, so the advantage can /// shrink but not grow unnoticed; it is not a claim that the number is fine. /// -/// Measured +2.456 dB luma / +3.158 dB YUV on the recalibrated run (352x288, +/// Measured +2.507 dB luma / +3.238 dB YUV at `rdo_candidates: 5` (352x288, /// 24 pictures of `Clip::render_smooth`, x265 at QP 22/26/30/34, our ladder /// auto-shifted from a QP-26 probe to 16/20/24/28, both sides decoded through /// ffmpeg, union-range BD-PSNR, 0% extrapolation both sides). This is a @@ -24,8 +24,10 @@ type Planes = (Vec, Vec, Vec); /// encoder is no leaner than x265 at matched QP (the probe measures a shift of /// 0) and the aligned BD is negative. Real-clip numbers, 24 pictures after /// 1500, ladders auto-aligned to 22/26/30/34, 0% extrapolation both sides: -/// • 3840x1608 film: -0.731 dB luma / -0.730 dB YUV -/// • 1138x640 web clip: -0.603 dB luma / -0.632 dB YUV +/// • 3840x1608 film: -0.685 dB luma / -0.703 dB YUV +/// • 1138x640 web clip: -0.507 dB luma / -0.556 dB YUV +/// • 2560x1440 screen capture: -2.465 dB luma / -2.360 dB YUV +/// • 1080p phone clip: -0.366 dB luma / -0.387 dB YUV /// Those two are the real gap to close; the synthetic number only guards /// against regression on a clip whose content is pinned. /// @@ -36,7 +38,7 @@ type Planes = (Vec, Vec, Vec); /// all-intra rate-quality slope is genuine content behaviour (x264 forced /// all-intra shows the same shape); the fix is to auto-align the ladders from /// a probe encode, not to change x265's rate control. -const BD_PSNR_VS_X265_FLOOR: f64 = 2.306; +const BD_PSNR_VS_X265_FLOOR: f64 = 2.357; /// A synthetic source with real structure: smooth gradients, a band of /// sinusoidal texture and a translating box, so prediction matters — the half @@ -425,6 +427,11 @@ fn bd_psnr_vs_x265() { cfg.rate_control = RateControl::ConstantQp(qp); cfg.keep_recon = true; cfg.threads = 0; + // Ablation knob: how many SATD survivors reach the full rate-distortion + // trial in the luma mode search. The shipped default is 5. + if let Ok(k) = std::env::var("EC_H265_RDO_CANDIDATES") { + cfg.rdo_candidates = k.parse().expect("EC_H265_RDO_CANDIDATES must be a number"); + } let enc = Encoder::new(cfg).expect("encoder"); let (mut stream, mut sum_recon) = (Vec::new(), 0.0); for (y, u, v) in &sources {