Stop penalising intra macroblocks in P pictures
Browse files at this commit942a61aa058701031e640cc8a8004beb931c4154Parent: cce1dad- perf
- stop penalising intra macroblocks in P pictures
2 changed files +32 -14
crates/ec-h264/src/enc/mb.rsmodified+17 -6
| @@ -355,15 +355,26 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy | |||
| 355 | inter = Some(choose_inter(pic, e, mb_x, mb_y)); | 355 | inter = Some(choose_inter(pic, e, mb_x, mb_y)); |
| 356 | } | 356 | } |
| 357 | 357 | ||
| 358 | - // Intra cost against the inter one: a P macroblock goes intra only when it | 358 | + // Intra cost against the inter one, with no thumb on the scale. An eight-bit |
| 359 | - // is clearly cheaper, because an intra macroblock in a P picture also costs | 359 | + // penalty used to be added to the intra side, on the theory that an intra |
| 360 | - // the next picture's prediction quality. Skipping the intra trial when the | 360 | + // macroblock in a P picture also costs the next picture's prediction |
| 361 | - // inter cost is already low was measured and dropped: it cost 0.7 dB on | 361 | + // quality. Measured, it costs the opposite: removing it is worth 1.17 dB |
| 362 | - // screen capture and bought no measurable time. | 362 | + // BD-PSNR against x264 on a 3840x1608 film clip at GOP 10 (-1.404 -> |
| 363 | + // -0.233 dB) and 0.10 dB on a 2560x1440 screen capture (-0.033 -> +0.068), | ||
| 364 | + // and it is not a rate-quality trade -- at every one of QP 22/26/30/34 the | ||
| 365 | + // unpenalised encoder is both smaller and 0.15-1.2 dB better, because the | ||
| 366 | + // intra macroblocks it now places stop drift accumulating across the GOP. | ||
| 367 | + // Penalties of 1, 2, 4 and 8 bits all measured worse and negative ones | ||
| 368 | + // measured the same, so zero is the flat part of the curve, not a tuned | ||
| 369 | + // value. | ||
| 370 | + // | ||
| 371 | + // Skipping the intra trial when the inter cost is already low was measured | ||
| 372 | + // and dropped: it cost 0.7 dB on screen capture and bought no measurable | ||
| 373 | + // time. | ||
| 363 | let intra_cost = intra_pre_cost(pic, e, &nbr, mb_x, mb_y); | 374 | let intra_cost = intra_pre_cost(pic, e, &nbr, mb_x, mb_y); |
| 364 | let go_intra = match &inter { | 375 | let go_intra = match &inter { |
| 365 | None => true, | 376 | None => true, |
| 366 | - Some(i) => intra_cost.best() + motion_rate(e, 8.0) < i.cost, | 377 | + Some(i) => intra_cost.best() < i.cost, |
| 367 | }; | 378 | }; |
| 368 | 379 | ||
| 369 | if go_intra { | 380 | if go_intra { |
crates/ec-h264/tests/encode.rsmodified+15 -8
| @@ -863,17 +863,24 @@ fn real_clip_t8x8_bd_psnr() { | |||
| 863 | // --------------------------------------------------------------------------- | 863 | // --------------------------------------------------------------------------- |
| 864 | 864 | ||
| 865 | /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured | 865 | /// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured |
| 866 | -/// -5.297 dB against x264 there on 2026-08-23 (352x288, 24 pictures of | 866 | +/// -5.702 dB against x264 there on 2026-08-23 (352x288, 24 pictures of |
| 867 | /// `Clip::render_smooth`, both encoders at their own default effort: our | 867 | /// `Clip::render_smooth`, both encoders at their own default effort: our |
| 868 | /// `Preset::Fast`, x264's `-preset medium`). The floor is that number with | 868 | /// `Preset::Fast`, x264's `-preset medium`). The floor is that number with |
| 869 | /// room for platform noise, so the distance can shrink but not grow | 869 | /// room for platform noise, so the distance can shrink but not grow |
| 870 | -/// unnoticed; it is not a claim that -5.3 dB is fine. On a 3840x1608 clip | 870 | +/// unnoticed; it is not a claim that -5.7 dB is fine. On a 3840x1608 clip |
| 871 | -/// from the library the same measurement reads -1.495 dB and on a 2560x1440 | 871 | +/// from the library the same measurement reads -0.233 dB and on a 2560x1440 |
| 872 | -/// screen capture +0.216 dB, so this synthetic number is the pessimistic end | 872 | +/// screen capture +0.068 dB, so this synthetic number is the pessimistic end |
| 873 | -/// of the range, not the typical one -- the Intra_4x4 RD refinement that | 873 | +/// of the range and not the typical one. |
| 874 | -/// gained on all four of those measurements cost 0.157 dB here, on a source | 874 | +/// |
| 875 | -/// whose smooth ramps quantise unlike anything in a real frame. | 875 | +/// It moved -5.40 -> -5.75 when the intra-versus-inter penalty came off. That |
| 876 | -const BD_PSNR_VS_X264_FLOOR: f64 = -5.40; | 876 | +/// change is worth 1.17 dB on the film clip and takes fewer bits at every QP |
| 877 | +/// while doing it; here it spends 5% more bits for the same quality, because | ||
| 878 | +/// this clip's smooth ramps are what inter prediction is best at and any intra | ||
| 879 | +/// macroblock in a P picture is waste. The two contents disagreeing is the | ||
| 880 | +/// signal that the decision belongs on real coded cost rather than on a SATD | ||
| 881 | +/// comparison at all -- that is the next slice, not a reason to tune the | ||
| 882 | +/// threshold back toward this clip. | ||
| 883 | +const BD_PSNR_VS_X264_FLOOR: f64 = -5.75; | ||
| 877 | 884 | ||
| 878 | fn have_x264() -> bool { | 885 | fn have_x264() -> bool { |
| 879 | std::process::Command::new("ffmpeg") | 886 | std::process::Command::new("ffmpeg") |