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,