From 8cda34e7bf2a78b14c47542c3b2659b9de72a1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 23 Aug 2026 18:36:30 +0300 Subject: [PATCH] perf(h264): re-cost the Intra_4x4 mode survivors with real quantisation Ablating the reference showed x264's rate-distortion mode decision is worth 0.80 dB intra of its lead, so the mode decision is where the distance is. This does the first half of it: SATD still ranks the nine Intra_4x4 modes, but the top three are re-costed by actually quantising and reconstructing the block and charging reconstruction SSD plus coded bits. A mode that predicts slightly worse can quantise to far fewer levels, and SATD cannot see that. Measured with bd_psnr_vs_x264 against x264 -preset medium: clip all-intra GOP 10 3840x1608 film -0.749 -> -0.620 -1.557 -> -1.495 2560x1440 screen capture -0.225 -> -0.137 +0.173 -> +0.216 for 3-4% encode time. Three survivors is where it saturates: all nine landed within 0.002 dB of three for 7% more time. The rate term needs the same ZERO_BLOCK_LAMBDA correction the zero-block test uses -- block_bits counts CAVLC-ish widths, and with the full term the search loses 0.065 dB instead of gaining, and loses more the more candidates it sees. The synthetic clip pays 0.157 dB for this (-5.140 -> -5.297) and the floor moves to -5.40. Its smooth ramps quantise unlike anything in a real frame; all four measurements on real content gain. Full ec-h264 suite green. --- crates/ec-h264/src/enc/mb.rs | 66 +++++++++++++++++++++++++++++++++- crates/ec-h264/tests/encode.rs | 13 ++++--- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/crates/ec-h264/src/enc/mb.rs b/crates/ec-h264/src/enc/mb.rs index 4b36ebb0..e3ee281d 100644 --- a/crates/ec-h264/src/enc/mb.rs +++ b/crates/ec-h264/src/enc/mb.rs @@ -1055,6 +1055,10 @@ fn choose_chroma_mode( /// mode against the *reconstructed* neighbours and reconstruct it immediately, /// because the next block predicts from it. Returns the chosen modes and the /// modes they were predicted to be. +/// How many SATD survivors the Intra_4x4 mode decision re-costs with real +/// quantisation before choosing. +const I4_RD_CANDIDATES: usize = 3; + fn code_intra_4x4( pic: &mut Picture, e: &MbEnc<'_>, @@ -1090,6 +1094,7 @@ fn code_intra_4x4( let allowed = modes_allowed(n.have_top, n.have_left, have_tl); let mut best = (i32::MAX, predicted.min(2)); let mut p = [0u8; 16]; + let mut ranked = [(i32::MAX, 2u8); 9]; for mode in 0..9u8 { if !allowed[mode as usize] { continue; @@ -1097,11 +1102,70 @@ fn code_intra_4x4( pred_4x4(mode, &n, &mut p); let bits = if mode == predicted { 1 } else { 4 }; let c = satd4(&e.src.y, e.src.stride, x, y, &p, 4) + motion_rate(e, f64::from(bits)); + ranked[mode as usize] = (c, mode); if c < best.0 { best = (c, mode); } } - let mode = best.1; + let mut mode = best.1; + // SATD ranks the modes; the survivors are re-costed with the + // quantisation actually in force, because a mode that predicts a + // little worse can quantise to far fewer levels. Measured against + // x264 with `bd_psnr_vs_x264` on a 3840x1608 film clip and a 2560x1440 + // screen capture: +0.129 and +0.088 dB all-intra, +0.062 and +0.043 dB + // at GOP 10, for 3-4% encode time. Three survivors is where it + // saturates -- all nine landed within 0.002 dB of three, for 7% more + // encode time. + { + ranked.sort_unstable(); + let keep = I4_RD_CANDIDATES; + // The same over-count [`ZERO_BLOCK_LAMBDA`] corrects for: with the + // full rate term this search loses 0.065 dB instead of gaining, + // and it loses more the more candidates it sees. + let lambda = ZERO_BLOCK_LAMBDA + * if e.lambda_standard { + e.lambda + } else { + lambda_ssd(qp) + }; + let mut best_rd = f64::MAX; + for &(c, cand) in ranked.iter().take(keep) { + if c == i32::MAX { + break; + } + pred_4x4(cand, &n, &mut p); + let mut d = [0i32; 16]; + for ry in 0..4 { + for rx in 0..4 { + d[ry * 4 + rx] = i32::from(e.src.y[(y + ry) * e.src.stride + x + rx]) + - i32::from(p[ry * 4 + rx]); + } + } + let src = d; + forward_4x4(&mut d); + let mut levels = [0i32; 16]; + let nz = quant_4x4(&d, qp, true, false, &mut levels); + let mut resid = [0i32; 16]; + if nz > 0 { + let mut raster = [0i32; 16]; + unzigzag(&levels, &mut raster); + dequant_4x4(&mut raster, &e.ls, qp, false); + inverse_transform_4x4(&raster, &mut resid); + } + let mut ssd = 0i64; + for i in 0..16 { + let err = i64::from(src[i] - resid[i]); + ssd += err * err; + } + let mode_bits = if cand == predicted { 1 } else { 4 }; + let bits = block_bits(&levels) + mode_bits; + let cost = ssd as f64 + lambda * bits as f64; + if cost < best_rd { + best_rd = cost; + mode = cand; + } + } + } modes[blk] = mode; pic.i4_modes[by * w4 + bx] = mode; // Prediction into the plane, then residual and reconstruction. diff --git a/crates/ec-h264/tests/encode.rs b/crates/ec-h264/tests/encode.rs index 9aa3f2ba..01b3174e 100644 --- a/crates/ec-h264/tests/encode.rs +++ b/crates/ec-h264/tests/encode.rs @@ -863,14 +863,17 @@ fn real_clip_t8x8_bd_psnr() { // --------------------------------------------------------------------------- /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured -/// -5.140 dB against x264 there on 2026-08-23 (352x288, 24 pictures of +/// -5.297 dB against x264 there on 2026-08-23 (352x288, 24 pictures of /// `Clip::render_smooth`, both encoders at their own default effort: our /// `Preset::Fast`, x264's `-preset medium`). The floor is that number with /// room for platform noise, so the distance can shrink but not grow -/// unnoticed; it is not a claim that -5.1 dB is fine. On a 3840x1608 clip -/// from the library the same measurement reads -1.557 dB, so this synthetic -/// number is the pessimistic end of the range, not the typical one. -const BD_PSNR_VS_X264_FLOOR: f64 = -5.25; +/// unnoticed; it is not a claim that -5.3 dB is fine. On a 3840x1608 clip +/// from the library the same measurement reads -1.495 dB and on a 2560x1440 +/// screen capture +0.216 dB, so this synthetic number is the pessimistic end +/// of the range, not the typical one -- the Intra_4x4 RD refinement that +/// gained on all four of those measurements cost 0.157 dB here, on a source +/// whose smooth ramps quantise unlike anything in a real frame. +const BD_PSNR_VS_X264_FLOOR: f64 = -5.40; fn have_x264() -> bool { std::process::Command::new("ffmpeg")