From 32fb6a829742c149a11515bbdca5fa47078a6dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 06:17:31 +0300 Subject: [PATCH] perf(h265): score mode candidates without RDOQ, refine only the winner encode_residual was 73% of encode wall time (perf record, ec-bench h265 row). choose_luma_mode ran full RDOQ (its per-coefficient CABAC-costed search) for every SATD-prescreened mode candidate just to rank them, then discarded all but the winner's levels. RDOQ's expensive refinement loop now runs once, after the winning mode is picked, instead of once per candidate (~6x fewer calls into the hot loop). ec-bench h265 row: 15621ms -> 4008ms wall (0.02x -> 0.08x RTF), ~3.9x. cargo test -p ec-h265 --release: 27+15+1+3 green, including bd_psnr_vs_x265 (+7.98/+8.44 dB, comfortably above its floor) and the RDOQ/RQT/cu64/threads bit-exactness conformance tests. --- crates/ec-h265/src/ctu.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/crates/ec-h265/src/ctu.rs b/crates/ec-h265/src/ctu.rs index 27f991ef..2893eb85 100644 --- a/crates/ec-h265/src/ctu.rs +++ b/crates/ec-h265/src/ctu.rs @@ -732,7 +732,7 @@ impl<'a> CtuEncoder<'a> { [(cy + row) * self.width + cx..(cy + row) * self.width + cx + HALF], ); } - let (_, cbf) = self.transform_luma(HALF, mode, &source, &refs); + let (_, cbf) = self.transform_luma(HALF, mode, &source, &refs, true); self.scratch.source = source; child_levels[part][..HALF * HALF] .copy_from_slice(&self.scratch.levels[..HALF * HALF]); @@ -869,7 +869,7 @@ impl<'a> CtuEncoder<'a> { [(cy + row) * self.width + cx..(cy + row) * self.width + cx + half], ); } - let (_, cbf) = self.transform_luma(half, mode, &source, &refs); + let (_, cbf) = self.transform_luma(half, mode, &source, &refs, true); child_levels[part][..half * half] .copy_from_slice(&self.scratch.levels[..half * half]); child_cbf[part] = cbf; @@ -1362,7 +1362,13 @@ impl<'a> CtuEncoder<'a> { for &mode in &candidates { let bits_before = enc.bit_count(); self.write_mode_syntax(enc, mode, mpm); - let (ssd, cbf) = self.transform_luma(n, mode, &source, &refs); + // Mode ranking runs the cheap (dead-zone-only) quantiser: RDOQ's + // per-coefficient CABAC-costed search is the hottest bucket in the + // encoder (measured: ~73% of encode wall time) and re-running it + // for every candidate mode only to throw away all but the winner + // is exactly the redundant recomputation this is worth skipping. + // The winner gets the full RDOQ refinement below, once. + let (ssd, cbf) = self.transform_luma(n, mode, &source, &refs, false); enc.encode_bin(ctx::CBF_LUMA + 1, u32::from(cbf)); if cbf { let scan = intra::scan_index(mode, log2, true); @@ -1388,6 +1394,14 @@ impl<'a> CtuEncoder<'a> { } } enc.set_counting(false); + // The trial above scored every candidate without RDOQ's refinement + // loop; now that the mode is decided, pay for that loop exactly once, + // on the winner, so the committed levels are the ones RDOQ can reach. + if self.rdoq { + self.transform_luma(n, best.1, &source, &refs, true); + best_levels[..n * n].copy_from_slice(&self.scratch.levels[..n * n]); + self.scratch.best_recon[..n * n].copy_from_slice(&self.scratch.recon[..n * n]); + } // Commit the winner's reconstruction and levels. for row in 0..n { for col in 0..n { @@ -1402,7 +1416,19 @@ impl<'a> CtuEncoder<'a> { /// Predict, transform, quantise and reconstruct one luma block into /// `scratch.recon` / `scratch.levels`; returns `(SSD, cbf)`. - fn transform_luma(&mut self, n: usize, mode: u8, source: &[u8], refs: &Refs) -> (f64, bool) { + /// + /// `refine` gates RDOQ's per-coefficient CABAC-costed search on top of the + /// dead-zone quantiser: callers scoring several mode candidates pass + /// `false` and re-run the winner with `true`, since the refinement is + /// worthless work on every candidate but the one that gets kept. + fn transform_luma( + &mut self, + n: usize, + mode: u8, + source: &[u8], + refs: &Refs, + refine: bool, + ) -> (f64, bool) { intra::predict( refs, mode, @@ -1432,7 +1458,7 @@ impl<'a> CtuEncoder<'a> { } else { quantize(&self.scratch.coeffs, &mut self.scratch.levels, n, self.qp) }; - if self.rdoq && nonzero > 0 { + if self.rdoq && refine && nonzero > 0 { nonzero = residual::rdoq( &self.scratch.coeffs, &mut self.scratch.levels,