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> {
732 [(cy + row) * self.width + cx..(cy + row) * self.width + cx + HALF],732 [(cy + row) * self.width + cx..(cy + row) * self.width + cx + HALF],
733 );733 );
734 }734 }
735- let (_, cbf) = self.transform_luma(HALF, mode, &source, &refs);735+ let (_, cbf) = self.transform_luma(HALF, mode, &source, &refs, true);
736 self.scratch.source = source;736 self.scratch.source = source;
737 child_levels[part][..HALF * HALF]737 child_levels[part][..HALF * HALF]
738 .copy_from_slice(&self.scratch.levels[..HALF * HALF]);738 .copy_from_slice(&self.scratch.levels[..HALF * HALF]);
@@ -869,7 +869,7 @@ impl<'a> CtuEncoder<'a> {
869 [(cy + row) * self.width + cx..(cy + row) * self.width + cx + half],869 [(cy + row) * self.width + cx..(cy + row) * self.width + cx + half],
870 );870 );
871 }871 }
872- let (_, cbf) = self.transform_luma(half, mode, &source, &refs);872+ let (_, cbf) = self.transform_luma(half, mode, &source, &refs, true);
873 child_levels[part][..half * half]873 child_levels[part][..half * half]
874 .copy_from_slice(&self.scratch.levels[..half * half]);874 .copy_from_slice(&self.scratch.levels[..half * half]);
875 child_cbf[part] = cbf;875 child_cbf[part] = cbf;
@@ -1362,7 +1362,13 @@ impl<'a> CtuEncoder<'a> {
1362 for &mode in &candidates {1362 for &mode in &candidates {
1363 let bits_before = enc.bit_count();1363 let bits_before = enc.bit_count();
1364 self.write_mode_syntax(enc, mode, mpm);1364 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);
1366 enc.encode_bin(ctx::CBF_LUMA + 1, u32::from(cbf));1372 enc.encode_bin(ctx::CBF_LUMA + 1, u32::from(cbf));
1367 if cbf {1373 if cbf {
1368 let scan = intra::scan_index(mode, log2, true);1374 let scan = intra::scan_index(mode, log2, true);
@@ -1388,6 +1394,14 @@ impl<'a> CtuEncoder<'a> {
1388 }1394 }
1389 }1395 }
1390 enc.set_counting(false);1396 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+ }
1391 // Commit the winner's reconstruction and levels.1405 // Commit the winner's reconstruction and levels.
1392 for row in 0..n {1406 for row in 0..n {
1393 for col in 0..n {1407 for col in 0..n {
@@ -1402,7 +1416,19 @@ impl<'a> CtuEncoder<'a> {
1402 1416
1403 /// Predict, transform, quantise and reconstruct one luma block into1417 /// Predict, transform, quantise and reconstruct one luma block into
1404 /// `scratch.recon` / `scratch.levels`; returns `(SSD, cbf)`.1418 /// `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) {
1406 intra::predict(1432 intra::predict(
1407 refs,1433 refs,
1408 mode,1434 mode,
@@ -1432,7 +1458,7 @@ impl<'a> CtuEncoder<'a> {
1432 } else {1458 } else {
1433 quantize(&self.scratch.coeffs, &mut self.scratch.levels, n, self.qp)1459 quantize(&self.scratch.coeffs, &mut self.scratch.levels, n, self.qp)
1434 };1460 };
1435- if self.rdoq && nonzero > 0 {1461+ if self.rdoq && refine && nonzero > 0 {
1436 nonzero = residual::rdoq(1462 nonzero = residual::rdoq(
1437 &self.scratch.coeffs,1463 &self.scratch.coeffs,
1438 &mut self.scratch.levels,1464 &mut self.scratch.levels,