forge
Explore
Log in

Don't you dare go Hollow

Score mode candidates without RDOQ, refine only the winner

Browse files at this commit

32fb6a829742c149a11515bbdca5fa47078a6dc9Ahmet Kaan GümüşParent: 9fab3bf
  • perf
    • score mode candidates without RDOQ, refine only the winner
Show full message
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.rsmodified+31 -5
@@ -732,7 +732,7 @@ impl<'a> CtuEncoder<'a> {
732732 [(cy + row) * self.width + cx..(cy + row) * self.width + cx + HALF],
733733 );
734734 }
735- let (_, cbf) = self.transform_luma(HALF, mode, &source, &refs);
735+ let (_, cbf) = self.transform_luma(HALF, mode, &source, &refs, true);
736736 self.scratch.source = source;
737737 child_levels[part][..HALF * HALF]
738738 .copy_from_slice(&self.scratch.levels[..HALF * HALF]);
@@ -869,7 +869,7 @@ impl<'a> CtuEncoder<'a> {
869869 [(cy + row) * self.width + cx..(cy + row) * self.width + cx + half],
870870 );
871871 }
872- let (_, cbf) = self.transform_luma(half, mode, &source, &refs);
872+ let (_, cbf) = self.transform_luma(half, mode, &source, &refs, true);
873873 child_levels[part][..half * half]
874874 .copy_from_slice(&self.scratch.levels[..half * half]);
875875 child_cbf[part] = cbf;
@@ -1362,7 +1362,13 @@ impl<'a> CtuEncoder<'a> {
13621362 for &mode in &candidates {
13631363 let bits_before = enc.bit_count();
13641364 self.write_mode_syntax(enc, mode, mpm);
1365- let (ssd, cbf) = self.transform_luma(n, mode, &source, &refs);
1365+ // Mode ranking runs the cheap (dead-zone-only) quantiser: RDOQ's
1366+ // per-coefficient CABAC-costed search is the hottest bucket in the
1367+ // encoder (measured: ~73% of encode wall time) and re-running it
1368+ // for every candidate mode only to throw away all but the winner
1369+ // is exactly the redundant recomputation this is worth skipping.
1370+ // The winner gets the full RDOQ refinement below, once.
1371+ let (ssd, cbf) = self.transform_luma(n, mode, &source, &refs, false);
13661372 enc.encode_bin(ctx::CBF_LUMA + 1, u32::from(cbf));
13671373 if cbf {
13681374 let scan = intra::scan_index(mode, log2, true);
@@ -1388,6 +1394,14 @@ impl<'a> CtuEncoder<'a> {
13881394 }
13891395 }
13901396 enc.set_counting(false);
1397+ // The trial above scored every candidate without RDOQ's refinement
1398+ // loop; now that the mode is decided, pay for that loop exactly once,
1399+ // on the winner, so the committed levels are the ones RDOQ can reach.
1400+ if self.rdoq {
1401+ self.transform_luma(n, best.1, &source, &refs, true);
1402+ best_levels[..n * n].copy_from_slice(&self.scratch.levels[..n * n]);
1403+ self.scratch.best_recon[..n * n].copy_from_slice(&self.scratch.recon[..n * n]);
1404+ }
13911405 // Commit the winner's reconstruction and levels.
13921406 for row in 0..n {
13931407 for col in 0..n {
@@ -1402,7 +1416,19 @@ impl<'a> CtuEncoder<'a> {
14021416
14031417 /// Predict, transform, quantise and reconstruct one luma block into
14041418 /// `scratch.recon` / `scratch.levels`; returns `(SSD, cbf)`.
1405- fn transform_luma(&mut self, n: usize, mode: u8, source: &[u8], refs: &Refs) -> (f64, bool) {
1419+ ///
1420+ /// `refine` gates RDOQ's per-coefficient CABAC-costed search on top of the
1421+ /// dead-zone quantiser: callers scoring several mode candidates pass
1422+ /// `false` and re-run the winner with `true`, since the refinement is
1423+ /// worthless work on every candidate but the one that gets kept.
1424+ fn transform_luma(
1425+ &mut self,
1426+ n: usize,
1427+ mode: u8,
1428+ source: &[u8],
1429+ refs: &Refs,
1430+ refine: bool,
1431+ ) -> (f64, bool) {
14061432 intra::predict(
14071433 refs,
14081434 mode,
@@ -1432,7 +1458,7 @@ impl<'a> CtuEncoder<'a> {
14321458 } else {
14331459 quantize(&self.scratch.coeffs, &mut self.scratch.levels, n, self.qp)
14341460 };
1435- if self.rdoq && nonzero > 0 {
1461+ if self.rdoq && refine && nonzero > 0 {
14361462 nonzero = residual::rdoq(
14371463 &self.scratch.coeffs,
14381464 &mut self.scratch.levels,