From 942a61aa058701031e640cc8a8004beb931c4154 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 19:05:16 +0300 Subject: [PATCH] perf(h264): stop penalising intra macroblocks in P pictures The intra-versus-inter comparison added an eight-bit penalty to the intra side, on the theory that an intra macroblock in a P picture also costs the next picture's prediction quality. Measured, it costs the opposite -- the intra macroblocks it suppressed were stopping drift from accumulating across the GOP. BD-PSNR against x264 -preset medium at GOP 10: 3840x1608 film -1.404 -> -0.233 dB 2560x1440 screen capture -0.033 -> +0.068 dB and it is not a rate-quality trade: at every one of QP 22/26/30/34 the unpenalised encoder is both smaller and 0.15-1.2 dB better on the film clip. Penalties of 1, 2, 4 and 8 bits all measured worse and negative ones measured the same, so zero is the flat part of the curve rather than a tuned value. The synthetic gate clip disagrees: it spends 5% more bits for the same quality, because smooth ramps are what inter prediction is best at and any intra macroblock there is waste, so the floor moves -5.40 -> -5.75. The two contents disagreeing in sign is the signal that this decision belongs on real coded cost rather than on a SATD comparison -- that is the next slice, and the floor comment says so. Full ec-h264 suite green. --- crates/ec-h264/src/enc/mb.rs | 23 +++++++++++++++++------ crates/ec-h264/tests/encode.rs | 23 +++++++++++++++-------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/crates/ec-h264/src/enc/mb.rs b/crates/ec-h264/src/enc/mb.rs index a2e4950a..daba4d13 100644 --- a/crates/ec-h264/src/enc/mb.rs +++ b/crates/ec-h264/src/enc/mb.rs @@ -355,15 +355,26 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy inter = Some(choose_inter(pic, e, mb_x, mb_y)); } - // Intra cost against the inter one: a P macroblock goes intra only when it - // is clearly cheaper, because an intra macroblock in a P picture also costs - // the next picture's prediction quality. Skipping the intra trial when the - // inter cost is already low was measured and dropped: it cost 0.7 dB on - // screen capture and bought no measurable time. + // Intra cost against the inter one, with no thumb on the scale. An eight-bit + // penalty used to be added to the intra side, on the theory that an intra + // macroblock in a P picture also costs the next picture's prediction + // quality. Measured, it costs the opposite: removing it is worth 1.17 dB + // BD-PSNR against x264 on a 3840x1608 film clip at GOP 10 (-1.404 -> + // -0.233 dB) and 0.10 dB on a 2560x1440 screen capture (-0.033 -> +0.068), + // and it is not a rate-quality trade -- at every one of QP 22/26/30/34 the + // unpenalised encoder is both smaller and 0.15-1.2 dB better, because the + // intra macroblocks it now places stop drift accumulating across the GOP. + // Penalties of 1, 2, 4 and 8 bits all measured worse and negative ones + // measured the same, so zero is the flat part of the curve, not a tuned + // value. + // + // Skipping the intra trial when the inter cost is already low was measured + // and dropped: it cost 0.7 dB on screen capture and bought no measurable + // time. let intra_cost = intra_pre_cost(pic, e, &nbr, mb_x, mb_y); let go_intra = match &inter { None => true, - Some(i) => intra_cost.best() + motion_rate(e, 8.0) < i.cost, + Some(i) => intra_cost.best() < i.cost, }; if go_intra { diff --git a/crates/ec-h264/tests/encode.rs b/crates/ec-h264/tests/encode.rs index 01b3174e..d3e7ae10 100644 --- a/crates/ec-h264/tests/encode.rs +++ b/crates/ec-h264/tests/encode.rs @@ -863,17 +863,24 @@ fn real_clip_t8x8_bd_psnr() { // --------------------------------------------------------------------------- /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured -/// -5.297 dB against x264 there on 2026-08-23 (352x288, 24 pictures of +/// -5.702 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.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; +/// unnoticed; it is not a claim that -5.7 dB is fine. On a 3840x1608 clip +/// from the library the same measurement reads -0.233 dB and on a 2560x1440 +/// screen capture +0.068 dB, so this synthetic number is the pessimistic end +/// of the range and not the typical one. +/// +/// It moved -5.40 -> -5.75 when the intra-versus-inter penalty came off. That +/// change is worth 1.17 dB on the film clip and takes fewer bits at every QP +/// while doing it; here it spends 5% more bits for the same quality, because +/// this clip's smooth ramps are what inter prediction is best at and any intra +/// macroblock in a P picture is waste. The two contents disagreeing is the +/// signal that the decision belongs on real coded cost rather than on a SATD +/// comparison at all -- that is the next slice, not a reason to tune the +/// threshold back toward this clip. +const BD_PSNR_VS_X264_FLOOR: f64 = -5.75; fn have_x264() -> bool { std::process::Command::new("ffmpeg")