From 6897ed71473c3c33bb88f0578eeccaa7b2c64540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 24 Aug 2026 06:17:15 +0300 Subject: [PATCH] perf(h264): decide the P shape and skip flag on coded cost The motion search ranked its shapes by an SATD total plus a guessed signalling charge, and the macroblock took that ranking as final. That total is a prediction-domain estimate: it never sees the residual the shape causes, so a split that looks cheaper to predict can cost more to code than the 16x16 it displaced, and the encoder had no way to notice. Intra against inter was already decided by coding both branches and comparing squared error plus lambda times real bits. This extends the same treatment to the inter side: choose_inter now returns every shape it costed, most promising first, with P_Skip appended when the search did not already prefer it, and the macroblock codes the list and keeps the smallest coded cost. BD-PSNR-YUV against x264 at GOP 10 over QP 22/26/30/34, Balanced: film 3840x1608 screen 2560x1440 1 candidate -0.580 -0.474 2 -0.521 -0.459 3 -0.513 -0.447 5 -0.504 -0.406 monotone with no turnover, so the whole list is coded. Luma-only moves -0.121 to -0.079 and -0.190 to -0.128. The film clip's comparison run goes from 46s to 64s: a P macroblock is now coded up to five times and every loser's reconstruction and entropy writer is thrown away, which is why the list is Balanced-only and Fast still codes the SATD winner alone. This is the step the x264 feature ladder localised: subme 4 reads -0.290 and subme 6 reads -0.581 on the film clip, the difference being exactly that x264 stops estimating the mode decision and codes it. --- crates/ec-h264/src/enc/mb.rs | 83 +++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 11 deletions(-) diff --git a/crates/ec-h264/src/enc/mb.rs b/crates/ec-h264/src/enc/mb.rs index b9d325b0..1e13afd8 100644 --- a/crates/ec-h264/src/enc/mb.rs +++ b/crates/ec-h264/src/enc/mb.rs @@ -471,7 +471,7 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy } } - let mut inter: Option = None; + let mut inter: Option> = None; if e.slice_type == SliceType::P { inter = Some(choose_inter(pic, e, mb_x, mb_y)); } @@ -499,7 +499,7 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy // P macroblock is coded twice, and the loser's reconstruction and entropy // writer are thrown away. let intra_cost = intra_pre_cost(pic, e, &nbr, mb_x, mb_y); - let Some(choice) = inter else { + let Some(choices) = inter else { encode_intra_mb(pic, e, w, mb_addr, nbr, intra_cost); return; }; @@ -510,14 +510,29 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy lambda_ssd(e.qp) }; let before = save_mb_state(pic, e, mb_addr); - let mut w_inter = w.clone(); - let inter_bits = { - let start = w_inter.bit_len(); - encode_inter_mb(pic, e, &mut w_inter, mb_addr, nbr, choice); - (w_inter.bit_len() - start) as f64 + // The shape and the skip flag are ranked the same way intra and inter are: + // by coding each candidate and measuring it. The SATD total that ordered + // them inside the search is a prediction-domain estimate that never sees + // the residual it causes, which is exactly the step x264 takes between + // subme 4 and subme 6 -- the 0.29 dB the feature ladder localised there. + let rd_cands = if e.preset == Preset::Balanced { + choices.len().min(SHAPE_RD_CANDIDATES) + } else { + 1 }; - let inter_rd = ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda * inter_bits; - let after_inter = save_mb_state(pic, e, mb_addr); + let mut best_inter: Option<(f64, MbState, EncEntropy)> = None; + for &choice in &choices[..rd_cands.max(1)] { + load_mb_state(pic, e, mb_addr, &before); + let mut w_c = w.clone(); + let start = w_c.bit_len(); + encode_inter_mb(pic, e, &mut w_c, mb_addr, nbr, choice); + let bits = (w_c.bit_len() - start) as f64; + let rd = ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda * bits; + if best_inter.as_ref().is_none_or(|b| rd < b.0) { + best_inter = Some((rd, save_mb_state(pic, e, mb_addr), w_c)); + } + } + let (inter_rd, after_inter, w_inter) = best_inter.expect("at least one inter candidate"); load_mb_state(pic, e, mb_addr, &before); let mut w_intra = w.clone(); @@ -742,7 +757,26 @@ impl PShape { /// half the bits it spends cost more than the chroma it buys. const CHROMA_ME_WEIGHT: f64 = 0.5; +/// How many inter candidates the macroblock decision codes before choosing, +/// most promising first. One is the SATD winner alone, which is what this used +/// to do; five is every shape the search costed plus P_Skip. BD-PSNR-YUV +/// against x264 at GOP 10 over QP 22/26/30/34, at `Preset::Balanced`: +/// +/// ```text +/// film 3840x1608 screen 2560x1440 +/// 1 candidate -0.580 -0.474 +/// 2 -0.521 -0.459 +/// 3 -0.513 -0.447 +/// 5 -0.504 -0.406 +/// ``` +/// +/// monotone in the count with no turnover, so the decision is taken at the +/// full list. The SATD total that used to rank the shapes is a +/// prediction-domain estimate that never sees the residual it causes. +const SHAPE_RD_CANDIDATES: usize = 5; + /// The inter decision for a P macroblock. +#[derive(Clone, Copy)] struct InterChoice { shape: PShape, /// One motion vector per partition. @@ -754,7 +788,7 @@ struct InterChoice { } /// Motion search plus the skip candidate. -fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> InterChoice { +fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Vec { let reference = e.reference.expect("a P slice has a reference"); let ctx = MvCtx { mb_x, @@ -976,6 +1010,9 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter }; let mut best = (PShape::Whole, [mv16; 4], cost16); + // Every shape the search costed, kept so the macroblock decision can rank + // them on coded cost instead of on this SATD total (see `encode_mb`). + let mut evaluated = vec![(PShape::Whole, [mv16; 4])]; // Halves, seeded with the whole-macroblock winner. The predictor used here // is the whole-macroblock one rather than each half's own: the real // predictors are derived in coding order when the choice is made, and this @@ -1035,6 +1072,7 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter mvs[part] = mv; total += cost + chroma_satd(bx * 4, by * 4, w, h, mv); } + evaluated.push((shape, mvs)); if total < best.2 { best = (shape, mvs, total); } @@ -1074,12 +1112,35 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter ); } } - InterChoice { + let winner = InterChoice { shape: best.0, mv: best.1, skip_mv, prefer_skip: best.0 == PShape::Whole && skip_cost <= best.2, + }; + // The SATD winner first, then the shapes it beat, then the skip candidate + // when the winner is not already it. The caller codes as many of these as + // its preset allows and keeps the one with the smallest coded cost. + let mut cands = vec![winner]; + for (shape, mv) in evaluated { + if shape != winner.shape || winner.prefer_skip { + cands.push(InterChoice { + shape, + mv, + skip_mv, + prefer_skip: false, + }); + } + } + if !winner.prefer_skip { + cands.push(InterChoice { + shape: PShape::Whole, + mv: [skip_mv; 4], + skip_mv, + prefer_skip: true, + }); } + cands } /// Quantise and reconstruct the luma residual of a non-Intra_16x16 macroblock,