# lane h264x264-r1 — first rate-quality reference for ec-h264 Rank 1 of `lanes/survey-r1.report.md` ("ec-h264 has no rate-quality reference at all"). Closed 2026-08-23. Commits `1dee93e`, `aa1cd35`, `d994e4c`, `7252dbe` on `main`. ## What landed `bd_psnr_vs_x264` in `crates/ec-h264/tests/encode.rs` — a run-by-default gate (skips loudly, with the exact runnable command, when ffmpeg/libx264 is absent). It encodes the same pictures at QP 22/26/30/34 with both encoders under matched features, decodes our own stream with ffmpeg to prove our reported PSNR is the stream's and not just our reconstruction's (`|recon - decoded| < 0.05 dB`), and reports BD-PSNR. Default source is a synthetic clip; `EC_H264_CLIP=` sweeps a real one, `EC_H264_GOP` and `EC_H264_PRESET_BALANCED` vary the configuration. ## Two instrument defects found before any number was trusted 1. **x264's `-qp` is not constant QP.** `ipratio` (1.4) and `pbratio` scale the I/B quantiser off the requested value; `qcomp`, `aq-mode`, `psy`, `chroma-qp-offset` perturb it further. All-intra diagnosis: x264 read +37% bits at +4.5 dB — a different quantiser reading as a rate-quality win. Pinned with `scenecut=0:aq-mode=0:psy=0:8x8dct=0:ipratio=1.0:pbratio=1.0:qcomp=1.0:chroma-qp-offset=0`. 2. **A noisy synthetic source measures residual abandonment, not coding gain.** On an incompressible noise field x264 spent 224k bits at QP 30 where we spent 672k and landed 0.5 dB *below* us, yet scored a large BD win. The tell was `Preset::Balanced` scoring *worse* than `Preset::Fast` there while scoring far better on real content. Replaced with `Clip::render_smooth`. Both are recorded as a class in memory (`x264-cqp-and-noise-gates`): an encoder-vs-reference gate must first prove the two encoders sit at the same operating point. ## Where the gap was 3840x1608 clip from the library, 24 pictures 1 s apart, GOP 10, matched features: | configuration | BD-PSNR vs x264 | encode time | |---|---|---| | `Preset::Fast` (baseline) | **-2.992 dB** | 20.67 s | | all-intra (`EC_H264_GOP=1`) | -0.749 dB | 19.27 s | | `Preset::Balanced` | -1.587 dB | 29.24 s | All-intra costs only 0.7 dB, so about two thirds of the distance was in P pictures. Ablation of the four places `Fast` and `Balanced` differ, one at a time, same clip: | ablation | BD-PSNR | encode time | |---|---|---| | baseline | -2.992 dB | 20.67 s | | half-macroblock partitions | -3.009 dB | 26.38 s | | + subpel refinement | -3.035 dB | 29.28 s | | 48-sample search range | -3.039 dB | 20.86 s | | **Intra_4x4 in P** | **-1.557 dB** | 21.75 s | | Intra_4x4 + range | -1.574 dB | 21.88 s | | all four (= `Balanced`) | -1.587 dB | 29.24 s | The whole `Balanced` advantage is Intra_4x4 in P pictures, for 5% encode time. The other three buy nothing measurable — range and the half-partitions are marginally negative, i.e. inside the noise, and the half-partitions cost 28% encode time for it. ## Fix `allow_i4: true` unconditionally in `crates/ec-h264/src/enc/mb.rs` (`7252dbe`). After it, on the same clip: -1.557 dB, +5-10% bits at 0.3-1.3 dB lower PSNR (was +19% bits at 1.0-1.7 dB). Synthetic default -5.202 -> -5.140 dB, floor retightened to -5.25. Full `ec-h264` suite green (109 tests, release). No test in the repo pins encoder bytes. ## Open, not fixed here - **`Preset::Balanced` no longer earns its 40% encode time.** With Intra_4x4 on everywhere it reads -1.587 dB against `Fast`'s -1.557 dB — slower and marginally worse. Either its search changes need to be replaced with something that measures, or the preset should go. Needs the user's call because it is public API. - **The remaining -1.557 dB is still unattributed.** All-intra re-measures at -0.749 dB after the fix (unchanged -- I pictures always had Intra_4x4), so the split is about 0.75 dB intra and 0.81 dB P-picture coding. The dead-zone hypothesis on record -- that ours is inverted against x264's -- was tested and is **dead**. Sweeping both numerators as sixty-fourths on the same clip: | intra, inter (/64) | GOP 10 | all-intra | |---|---|---| | ours (21.3, 10.7 equivalent) | -1.557 dB | -0.749 dB | | 11, 21 (x264's own) | -2.132 dB | -1.667 dB | | 21, 21 | -1.535 dB | -0.745 dB | | 11, 11 | -2.128 dB | -1.667 dB | | 21, 6 | -1.557 dB | -0.745 dB | | 21, 16 | -1.532 dB | - | | 21, 32 | -1.571 dB | - | The intra offset is the only one that moves the number, and ours is the better of the two by 0.92 dB all-intra. The inter offset is inert: 6/64 through 32/64 span 0.04 dB while the bit counts move 0.8%, so the path is reached and simply does not matter. Recorded in `round_offset`'s doc comment so the sweep is not repeated. Attributing the rest by ablating **the reference** (`EC_H264_X264_PARAMS`, same clip): | x264 configuration | GOP 10 | all-intra | |---|---|---| | `-preset medium` (the gate's) | -1.557 dB | -0.749 dB | | `trellis=0` | -1.456 dB | -0.587 dB | | `trellis=2` | -1.642 dB | -0.811 dB | | `trellis=0:subme=1:me=dia` | -0.873 dB | **+0.216 dB** | Trellis is worth only 0.10 dB inter and 0.16 dB intra of x264's lead, so RDOQ is not the lever it looked like. Turning off x264's rate-distortion **mode decision** (`subme=1`, which drops the RD refinement of macroblock types) is worth 0.80 dB intra and a further 0.58 dB inter -- and with it off we are *ahead* of x264 on all-intra by 0.216 dB. So the distance is the mode decision: we choose macroblock and prediction modes on SATD-plus-estimated-rate, x264 chooses the survivors on true coded bits against reconstruction SSD. That is the next build slice, and it is worth about 1.4 dB. P_8x8 sub-partitions and the search itself stay unmeasured but are bounded above by what is left after that. ## Round 2 -- the first mode-decision slice (main 931f413) Acting on that attribution: SATD still ranks the nine Intra_4x4 modes, but the top `I4_RD_CANDIDATES = 3` are now re-costed by actually quantising, dequantising and reconstructing the block, and charging reconstruction SSD plus coded bits. A mode that predicts a little worse can quantise to far fewer levels, and SATD cannot see that. | clip | all-intra | GOP 10 | |---|---|---| | 3840x1608 film | -0.749 -> **-0.620** | -1.557 -> **-1.495** | | 2560x1440 screen capture | -0.225 -> **-0.137** | +0.173 -> **+0.216** | | 352x288 synthetic ramp (the gate's) | - | -5.140 -> -5.297 | Cost 3-4% encode time. Survivor count saturates at three: all nine landed within 0.002 dB of three for 7% more time. **The rate term had to be calibrated before the search worked at all.** With `block_bits` charged in full the search got monotonically *worse* the more candidates it saw (K=1 -0.749, K=9 -0.830 all-intra) -- which reads as "RD does not help here" and is wrong. `block_bits` counts CAVLC-ish widths that over-state what the coder in force spends, and the repo already corrects for that elsewhere with `ZERO_BLOCK_LAMBDA = 0.4`. Scaling the rate term by the same constant turns every real-content measurement from a loss into a gain: | rate-term scale | all-intra, film | |---|---| | 1.0, K=3 | -0.814 dB | | 0.5, K=3 | -0.644 dB | | **0.4, K=3** | **-0.620 dB** | | 0.4, K=9 | -0.618 dB | The synthetic clip pays 0.157 dB for the change and `BD_PSNR_VS_X264_FLOOR` moved -5.25 -> -5.40. Accepted deliberately: its smooth ramps quantise unlike anything in a real frame, all four real-content measurements gain, and the floor's doc comment says so rather than claiming -5.3 dB is fine. ### Still open after round 2 The same RD treatment has not been applied to Intra_8x8 mode selection, `choose_chroma_mode`, or `choose_inter` (still SATD + `motion_rate`). Those are the rest of the ~1.4 dB the `subme=1` ablation priced. P_8x8 sub-partitions remain unmeasured. ## Round 3 -- the thumbs on the scale (main 81d18a3, 9268484, ff0baca) Three constants in the macroblock decision were checked by sweeping them. Two were wrong, and one of them was worth more than everything in rounds 1 and 2 put together. **Chroma mode (81d18a3).** Picked on SAD with no rate term while everything else costs on SATD. SATD plus the mode's ue(v) signalling, at no measurable time: | clip | all-intra | GOP 10 | |---|---|---| | 3840x1608 film | -0.620 -> -0.535 dB | -1.495 -> -1.404 dB | | 2560x1440 screen capture | -0.231 -> -0.197 dB | -0.062 -> -0.033 dB | **The intra penalty in P pictures (9268484).** The comparison added eight bits to the intra side, for "an intra macroblock also costs the next picture's prediction quality". Sweeping it: | penalty (bits) | film GOP 10 | |---|---| | 32 | -3.065 dB | | 16 | -1.940 dB | | 8 (what it was) | -1.404 dB | | 4 | -0.704 dB | | 0 | **-0.233 dB** | | -1 / -2 / -4 / -8 | -0.140 / -0.113 / -0.106 / -0.115 dB | Zero is the flat part of the curve, not a tuned value. Per-QP, it is not a rate-quality trade at all -- the unpenalised encoder is *smaller and better at every QP*: | QP | penalty 8 | penalty 0 | |---|---|---| | 22 | 23313152 b, 55.48 dB | 23253064 b, 55.63 dB | | 26 | 16041512 b, 51.94 dB | 15986488 b, 52.93 dB | | 30 | 11411616 b, 48.26 dB | 11360328 b, 49.73 dB | | 34 | 8086824 b, 46.05 dB | 8035984 b, 47.28 dB | The suppressed intra macroblocks were what stopped drift accumulating across the GOP. **The P_Skip rebate (ff0baca).** Four bits, swept to 0/2/4/8: film reads -0.113 / -0.110 / -0.233 / -0.708 dB. Set to two, the top of the flat region, since skipping does save signalling. Where the film clip stands after the three: **-0.110 dB at GOP 10** (from -2.992 dB when the lane opened) and the screen capture is **+0.068 dB**, i.e. ahead of x264. ### The synthetic gate clip now disagrees in sign Removing the intra penalty costs the 352x288 `render_smooth` fixture 0.41 dB (-5.291 -> -5.702), and the floor moved -5.40 -> -5.75. It spends 5% more bits for the same quality there: smooth ramps are exactly what inter prediction is best at, so any intra macroblock in a P picture is waste, and a SATD comparison cannot see that. Two contents disagreeing in sign is the signal that this decision belongs on real coded cost -- both branches already compute SSD-plus-real-bits internally (`intra_mb_bits`, `inter_mb_bits`), so the next slice is to compare them there instead of tuning the threshold back toward the fixture. ### Class sweep: the rest of the encoder's magic constants Same shape as the three above, swept on the film clip at GOP 10 and then cross-checked on the screen capture and the fixture. None of them is a fix. | constant | film | screen capture | fixture | |---|---|---|---| | `IntraCost::best` fudge 0.75 -> 0.5 | -0.110 -> -0.066 | +0.068 -> +0.147 | -5.718 -> -6.231 | | `ZERO_BLOCK_LAMBDA` 0.4 -> 0.8 | -0.110 -> -0.020 | +0.068 -> +0.028 | -5.718 -> -5.897 | | `lambda_ssd` scale 0.85 -> 1.3 | -0.110 -> -0.060 | +0.068 -> -0.028 | -5.718 -> -5.798 | | search early-out `w*h/2` -> 0 or w*h | -0.110 (all three) | - | - | The first three are content-split: the film clip wants more rate weight, the screen capture and the fixture want less. That is the same disagreement-in-sign as the intra penalty, and the same answer applies -- it is a criterion problem, not a tuning problem, so the values stay where they are. The search early-out is simply inert on this content at any setting. ### Rejected: rate-distortion selection of the Intra_16x16 mode The same treatment the Intra_4x4 modes got, over the four Intra_16x16 modes (code each with real quantisation, cost SSD plus real bits, keep the best): | clip | all-intra | GOP 10 | |---|---|---| | 3840x1608 film | -0.535 -> -0.527 dB | -0.110 -> **-0.145** dB | | 2560x1440 screen capture | -0.197 -> -0.188 dB | +0.068 -> +0.098 dB | | 352x288 fixture | - | -5.718 -> -5.661 dB | Noise, and negative on the one row that matters most, for 4% more encode time. Not landed. The reason it does nothing where the Intra_4x4 version did something: an Intra_16x16 macroblock is chosen only where the picture is flat enough that all four modes predict nearly the same thing, and everywhere else Intra_4x4 or inter wins the outer decision anyway. ## Where the lane stands | measurement | lane open (7252dbe~) | now (c674993) | |---|---|---| | 3840x1608 film, GOP 10 | -2.992 dB | **-0.110 dB** | | 3840x1608 film, all-intra | -0.749 dB | **-0.535 dB** | | screen capture, GOP 10 | not comparable | **+0.068 dB** | | 352x288 fixture (the gate) | -5.140 dB | -5.718 dB | The screen-capture rows in rounds 1 and 2 were measured on a different capture from the one rounds 3 onward use (`~/Videos/ab/obs-2026-08-17.fixed.mp4`), so only the film clip and the fixture span the whole lane. Round 3's own before-and-after pairs are same-capture and do compare. Rank 1 of the survey is closed: the gate exists (`bd_psnr_vs_x264`, floored, in the default suite), the gap was measured, localised by ablating the reference, and closed to roughly parity on both real clips. What is left is the fixture, and the next slice for it is the intra-versus-inter decision on real coded cost rather than on SATD.