forge
Explore
Log in

Don't you dare go Hollow

Choose the Intra_8x8 luma mode by rate-distortion

Browse files at this commit

ccc465055a391e2af3f4e978dc532261a6ffaa44Ahmet Kaan GümüşParent: addbfd0
  • perf
    • choose the Intra_8x8 luma mode by rate-distortion
Show full message
perf(h264): choose the Intra_8x8 luma mode by rate-distortion

The Intra_8x8 chooser scored the nine prediction modes on SATD of the
prediction residual plus a mode-bit term. That is a decision taken before the
quantiser has a say: it never sees which mode's residual survives
quantisation cheaply. Same class as the intra chroma chooser fixed in
98d01b7, and the last SATD-only intra site.

Every allowed mode is now predicted, coded at the real QP and scored as
SSD + lambda * bits, with the rate read from the CABAC coder's own 8x8 price
(residual_block_8x8_cost — Luma8x8 has no coded_block_flag of its own, the
CBP carries it, so the cbf-indexed cost path panics on it).

BD-PSNR against x264 at matched features, 8x8 transform on, GOP 10, 24
consecutive pictures, luma / all-plane YUV:

  3840x1608 film     +0.475 / -0.068  ->  +0.512 / -0.035
  1138x640 web clip  +0.040 / -0.031  ->  +0.076 / +0.003
  854x480 web clip   +0.070 / +0.047  ->  +0.132 / +0.108
  2560x1440 screen   -0.206 / -0.199  ->  -0.215 / -0.207

Three clips of four gain on both metrics; the screen capture loses 0.008 dB.
Encode time on the 854x480 clip goes from 12 s to 13 s. The path only runs
when the 8x8 transform is enabled, which is still opt-in, so no default
stream byte moves; the byte-identity conformance tests confirm it.
crates/ec-h264/src/enc/mb.rsmodified+83 -24
@@ -2098,38 +2098,25 @@ fn code_intra_8x8(
2098 pred_modes[blk8] = predicted;2098 pred_modes[blk8] = predicted;
2099 let n = filter_nbr8(&gather_nbr8(pic, nbr, blk8, x, y));2099 let n = filter_nbr8(&gather_nbr8(pic, nbr, blk8, x, y));
2100 let allowed = modes_allowed(n.have_top, n.have_left, n.have_tl);2100 let allowed = modes_allowed(n.have_top, n.have_left, n.have_tl);
2101- let mut best = (i32::MAX, predicted.min(2));
2102 let mut p = [0u8; 64];2101 let mut p = [0u8; 64];
2103- for mode in 0..9u8 {2102+ {
2104- if !allowed[mode as usize] {2103+ let (mode, coded, recon) =
2105- continue;2104+ i8_mode_by_rd(pic, e, w, x, y, qp, &n, &allowed, predicted, &mut p);
2106- }2105+ let stride = pic.y.stride;
2107- pred_8x8(mode, &n, &mut p);2106+ let origin = pic.y.at(x, y);
2108- let bits = if mode == predicted { 1 } else { 4 };2107+ for ry in 0..8 {
2109- let mut c = motion_rate(e, f64::from(bits));2108+ let row = origin + ry * stride;
2110- for q in 0..4 {2109+ pic.y.data[row..row + 8].copy_from_slice(&recon[ry * 8..ry * 8 + 8]);
2111- let (ox, oy) = ((q % 2) * 4, (q / 2) * 4);
2112- c += satd4(&e.src.y, e.src.stride, x + ox, y + oy, &p[oy * 8 + ox..], 8);
2113- }
2114- if c < best.0 {
2115- best = (c, mode);
2116 }2110 }
2111+ lv.luma8[blk8] = coded.0;
2112+ if coded.1 {
2113+ lv.cbp_luma |= 1 << blk8;
2117 }2114 }
2118- let mode = best.1;
2119 modes[blk8] = mode;2115 modes[blk8] = mode;
2120 for dy in 0..2 {2116 for dy in 0..2 {
2121 let base = (by + dy) * w4 + bx;2117 let base = (by + dy) * w4 + bx;
2122 pic.i4_modes[base..base + 2].fill(mode);2118 pic.i4_modes[base..base + 2].fill(mode);
2123 }2119 }
2124- pred_8x8(mode, &n, &mut p);
2125- let stride = pic.y.stride;
2126- let origin = pic.y.at(x, y);
2127- for ry in 0..8 {
2128- let row = origin + ry * stride;
2129- pic.y.data[row..row + 8].copy_from_slice(&p[ry * 8..ry * 8 + 8]);
2130- }
2131- if code_block_8x8(pic, e, w, x, y, qp, true, &mut lv.luma8[blk8]) > 0 {
2132- lv.cbp_luma |= 1 << blk8;
2133 }2120 }
2134 }2121 }
2135 (modes, pred_modes)2122 (modes, pred_modes)
@@ -3445,3 +3432,75 @@ fn choose_chroma_mode_rd(
3445 *out = lv;3432 *out = lv;
3446 mode3433 mode
3447 }3434 }
3435+
3436+/// The mode decision of [`code_intra_8x8`]: for every allowed mode, predict,
3437+/// code the 8x8 residual at the real QP and score `SSD + lambda * bits`, then
3438+/// keep the cheapest. The SATD chooser this replaced scored the prediction
3439+/// residual before the quantiser had a say, so it optimised a proxy.
3440+///
3441+/// Returns the winning mode, its coded levels with whether the block has any,
3442+/// and the reconstructed 8x8 to write back. Measured on BD-PSNR against x264
3443+/// with the 8x8 transform on, GOP 10, 24 consecutive pictures, luma / YUV:
3444+/// • 3840x1608 film: +0.475 / -0.068 -> +0.512 / -0.035
3445+/// • 1138x640 web: +0.040 / -0.031 -> +0.076 / +0.003
3446+/// • 854x480 web: +0.070 / +0.047 -> +0.132 / +0.108
3447+/// • 2560x1440 screen: -0.206 / -0.199 -> -0.215 / -0.207
3448+/// Three clips of four gain; the screen capture loses 0.008 dB. Encode time
3449+/// moves from 12 s to 13 s on the 854x480 clip.
3450+#[allow(clippy::too_many_arguments)]
3451+fn i8_mode_by_rd(
3452+ pic: &mut Picture,
3453+ e: &MbEnc<'_>,
3454+ w: &EncEntropy,
3455+ x: usize,
3456+ y: usize,
3457+ qp: i32,
3458+ n: &crate::pred::Nbr8,
3459+ allowed: &[bool; 9],
3460+ predicted: u8,
3461+ p: &mut [u8; 64],
3462+) -> (u8, ([i32; 64], bool), [u8; 64]) {
3463+ let lambda = lambda_ssd(qp);
3464+ let stride = pic.y.stride;
3465+ let origin = pic.y.at(x, y);
3466+ let mut best: Option<(f64, u8, [i32; 64], bool, [u8; 64])> = None;
3467+ for mode in 0..9u8 {
3468+ if !allowed[mode as usize] {
3469+ continue;
3470+ }
3471+ pred_8x8(mode, n, p);
3472+ for ry in 0..8 {
3473+ let row = origin + ry * stride;
3474+ pic.y.data[row..row + 8].copy_from_slice(&p[ry * 8..ry * 8 + 8]);
3475+ }
3476+ let mut levels = [0i32; 64];
3477+ let nz = code_block_8x8(pic, e, w, x, y, qp, true, &mut levels);
3478+ let mut ssd = 0i64;
3479+ let mut recon = [0u8; 64];
3480+ for ry in 0..8 {
3481+ let row = origin + ry * stride;
3482+ for rx in 0..8 {
3483+ let r = pic.y.data[row + rx];
3484+ recon[ry * 8 + rx] = r;
3485+ let d = i64::from(e.src.y[(y + ry) * e.src.stride + x + rx]) - i64::from(r);
3486+ ssd += d * d;
3487+ }
3488+ }
3489+ let mut bits = if mode == predicted { 1.0 } else { 4.0 };
3490+ if nz > 0 {
3491+ // `Luma8x8` has no coded_block_flag of its own — the CBP carries it
3492+ // — so it needs the 8x8 cost path, not the cbf-indexed one.
3493+ bits += match w.residual_block_8x8_cost(&levels) {
3494+ Some(b) => f64::from(b) / 256.0,
3495+ None => block_bits(&levels) as f64,
3496+ };
3497+ }
3498+ let cost = ssd as f64 + lambda * bits;
3499+ if best.as_ref().is_none_or(|b| cost < b.0) {
3500+ best = Some((cost, mode, levels, nz > 0, recon));
3501+ }
3502+ }
3503+ let (_, mode, levels, coded, recon) =
3504+ best.expect("Intra_8x8 always has at least the DC mode available");
3505+ (mode, (levels, coded), recon)
3506+}