From 338bd3fa5b21b28b4eff2f240762147ff413cfa0 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 02:25:10 +0300 Subject: [PATCH] perf(h264): search every partition, and document the split gate The bit account points at the motion field, so the search's two stopping rules were measured rather than assumed. The early exit that accepted a seed costing under half a level a sample is monotonically harmful -- 0/0.25/0.5/1.0 read -0.586/-0.587/-0.593/-0.617 dB BD-PSNR against x264 on the film clip and -0.466/-0.470/-0.468/-0.476 on the screen capture -- and removing it does not move the suite's wall clock, so it is gone. The gate on searching the 16x8 and 8x16 halves reads identically at four settings on both clips: real content clears it everywhere. It stays as a guard for flat content and is documented as one. --- crates/ec-h264/src/enc/mb.rs | 19 ++++++++++++++----- lanes/h264x264-r2.report.md | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/crates/ec-h264/src/enc/mb.rs b/crates/ec-h264/src/enc/mb.rs index 0a08a993..ae53987a 100644 --- a/crates/ec-h264/src/enc/mb.rs +++ b/crates/ec-h264/src/enc/mb.rs @@ -735,11 +735,15 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter best = (cand, c); } } - // A partition the prediction already fits within half a level a sample - // is not searched further: on real content most of them are, and the - // search is the encoder's hot loop. - let good_enough = (w * h / 2) as i32; - if best.1 > good_enough { + // Every partition is searched. This used to stop at a seed whose cost + // was already under half a level a sample, on the reasoning that the + // prediction fit and the search is the encoder's hot loop. Swept on + // consecutive pictures (BD-PSNR against x264, GOP 10) the threshold is + // monotonically harmful: 0/0.25/0.5/1.0 levels a sample read + // -0.586/-0.587/-0.593/-0.617 dB on the film clip and + // -0.466/-0.470/-0.468/-0.476 on the screen capture. A seed that fits + // is exactly where a little more refinement is cheap. + { let mut step = 4i16 * 4; // four whole samples, in quarter units while step >= 4 { let mut improved = true; @@ -828,6 +832,11 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter // Measured: two more searches per macroblock buy 0.1-0.2 dB on screen // capture and nothing on camera content, for roughly twice the search // time. That is a Balanced trade, not a Fast one. + // The threshold is a floor that real content clears everywhere: swept at + // 0, 0.5, 1 and 2 levels a sample it moves neither clip's BD-PSNR by a + // thousandth of a decibel, so on these sources the halves are always + // searched. It stays as a guard for content flat enough to make the two + // extra searches pure cost. if e.preset == Preset::Balanced && cost16 > 16 * 16 { for shape in [PShape::Horizontal, PShape::Vertical] { let mut total = motion_rate(e, 6.0); // the partition's own signalling diff --git a/lanes/h264x264-r2.report.md b/lanes/h264x264-r2.report.md index 40de5fa1..ff2bc4af 100644 --- a/lanes/h264x264-r2.report.md +++ b/lanes/h264x264-r2.report.md @@ -490,3 +490,33 @@ That is a prediction result, not an entropy-coding one. The residual coder is no priced exactly and quantised against that price; the next thing to look at is what we hand it -- how the motion search chooses, and how the partition and mode decision weighs a more expensive motion field against the residual it saves. + +## The two gates on the motion search, swept + +The bit account says the residual is where we overspend and the motion field is +where we underspend, so the first thing to check is whether the search is being +stopped early. Two constants decided that, and neither had been measured. + +The early exit: a partition whose best seed already cost less than half a level +a sample was accepted without refinement. Swept, in levels a sample: + +| threshold | film | screen capture | +| --- | --- | --- | +| 0 (no exit) | **-0.586** | **-0.466** | +| 0.25 | -0.587 | -0.470 | +| 0.5 (was) | -0.593 | -0.468 | +| 1.0 | -0.617 | -0.476 | + +Monotone in the direction of searching more, and the encode-side suite does not +measure the extra work (71.7 s against 71.8 s), so the exit is gone. A seed that +already fits is exactly the case where one more refinement step is cheap. + +The split gate, which decides whether the 16x8 and 8x16 halves are searched at +all, reads identically at 0, 0.5, 1 and 2 levels a sample on both clips -- real +content clears it everywhere, so the halves are always searched today. It stays +as a guard for flat content, now documented as one rather than as a tuned value. + +Small numbers, and that is the finding: the search's stopping rules are not what +holds the motion field back. What we do not have is the shape -- the encoder +codes 16x16, 16x8 and 8x16 and nothing smaller, while x264 has the 8x8 sub- +partitions. That is the next slice.