edith_codecs
Clone
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
ec-h264 against x264, round 2: the comparison was sampling one picture a second
What was wrong with the measurement
bd_psnr_vs_x264 takes 24 pictures out of a real file. It took them one second
apart -- the sampler's stride was the clip's frame rate. At that spacing a P
picture has nothing to predict from, so the encoder was being measured on
content it never sees in a real encode. The give-away is the macroblock mix:
on the film clip our P slices went 93.2% intra, 6.3% skip, 0.5% coded inter.
Every P-path constant in the crate had been swept against that.
The sampler now takes consecutive pictures. EC_H264_CLIP_STRIDE restores a
wider spacing for the deliberately-uncorrelated case, and EC_H264_CLIP_SKIP
passes over a film's titles and fades before sampling.
Baselines move a long way once the pictures are correlated:
| clip | one a second | consecutive |
|---|---|---|
| film 3840x1608 | -0.110 | -1.521 |
| screen capture 2560x1440 | -0.977 | -1.904 |
The honest number was worse than the one that had been reported.
What the corrected measurement then showed
At every QP we were spending 20-42% more bits than x264 for a similar PSNR, and the premium grew with QP. Attribution, by disabling x264's own features on the screen capture:
| x264 running | BD-PSNR ours vs x264 |
|---|---|
| as configured | -0.977 |
trellis=0 |
-0.773 |
trellis=0:subme=1:me=dia |
-0.203 |
So roughly 0.2 dB of x264's lead is its rate-distortion quantisation and 0.57 dB is its motion search. (Those three rows are on the old sampler; they were what pointed at the search.)
Two fixes
The intra side of the mode decision signalled nothing. The inter side pays for its motion vector; going intra pays for a macroblock type and sixteen prediction modes, and the SATD of an intra prediction says nothing about them. A penalty had been swept before and landed at zero -- on the one-a-second content, where it had nothing to suppress. Re-swept on consecutive pictures:
| bits | film | screen | synthetic |
|---|---|---|---|
| 0 | -1.521 | -1.904 | -5.718 |
| 4 | -1.305 | -1.290 | -5.400 |
| 8 | -1.223 | -0.920 | -5.290 |
| 12 | -1.211 | -0.733 | -5.286 |
| 16 | -1.267 | -0.650 | -5.262 |
| 32 | -1.649 | -0.586 | -- |
Screen capture keeps improving to 48 and film falls off after 16, so the landed value is 12 -- the overlap of the two flat parts, not either optimum.
Sub-sample refinement ran on SAD. Interpolation smooths, and a sum of absolute differences reads the smoothing as a better match. The refinement now costs candidates on SATD, with the whole-sample winner re-costed in the same domain first: +0.057 dB on film, +0.037 on screen capture, +0.74 on the synthetic clip.
Result
| clip | before | after |
|---|---|---|
| film | -1.521 | -1.154 |
| screen capture | -1.904 | -0.696 |
| synthetic | -5.718 | -4.542 |
The bit premium is also flat in QP again rather than growing: film +14-16% at every QP, screen capture +17-18%, where before it ran +23% at QP 22 to +42% at QP 34.
The synthetic floor moves with it, -5.75 -> -4.6.
Constants re-swept under the corrected sampler
The two contents are quoted film / screen capture.
- Intra_4x4 in P pictures: on reads -1.154 / -0.696, off reads -1.989 / -0.564. The contents disagree in sign; film's margin is three times screen capture's, so it stays on.
- P_Skip rebate: 0 and 2 read -1.102 / -0.741 and -1.154 / -0.696 -- the contents disagree by less than the step, so 2 stays. Six costs 0.14 dB, twelve costs 0.42.
- Intra_4x4 discount on the 16x16 pre-cost: 0.65/0.75/0.85/1.0 read -1.216/-1.154/-1.182/-1.373 on film and -0.970/-0.696/-0.591/-0.562 on screen capture. Three quarters is film's optimum and inside screen capture's flat part, so it stays.
What is still open
x264's rate-distortion quantisation (trellis) is worth about 0.2 dB and we have none. The remaining bit premium is flat in QP, which is the shape of a coder that codes the same decisions less cheaply rather than one making worse decisions.
Addendum: two parked decisions rested on the old sampler
Both were re-measured on consecutive pictures.
Preset::Balanced. It had read -1.587 dB where Fast read -1.557 -- worse,
for 40% more time, which is why dropping it was on the table. On correlated
pictures it wins on both contents: film -0.875 against Fast's -1.154, screen
capture -0.506 against -0.696, for 11% more wall clock on the whole comparison.
Its wider search range and half-macroblock partitions had nothing to find when
consecutive pictures were a second apart. The question is withdrawn.
transform_8x8. It had read +0.827 dB on natural 720p and 0.000 on the
screen capture. Re-measured: +0.800 dB on the film clip and +0.537 on the
screen capture, at an 8x8 macroblock share of 17.3% and 14.2%. It is now a
win on both contents; what is still owed is the decision to flip the default,
because that changes every default stream byte and re-pins the byte-identity
tests.
Addendum: where the rest of the gap is
With x264's own features disabled one at a time, on consecutive pictures (ours reads -1.154 film / -0.696 screen capture):
| x264 running | film | screen |
|---|---|---|
| as configured | -1.154 | -0.696 |
trellis=0 |
-0.959 | -0.594 |
me=dia |
-1.115 | -0.696 |
subme=4 |
-0.905 | -0.579 |
subme=1 |
-0.324 | -0.244 |
The whole-sample search pattern is worth 0.04 dB and 0.00; rate-distortion
quantisation is worth 0.20 and 0.10; sub-sample and mode refinement is worth
0.83 and 0.45 -- almost the entire remaining gap. x264's subme 6 and 7 are
its rate-distortion mode decision, which is the named next lane.
Iterating our own sub-sample refinement to convergence at Fast (it stopped
after one pass) is the first 0.111 dB of that, for 2% of the comparison's wall
clock: film -1.154 -> -1.043, screen capture -0.696 -> -0.661, synthetic
-4.542 -> -4.449.
The search's motion-vector weight was swept with it and held at a half:
0.25/0.5/1.0/2.0 read -1.074/-1.043/-1.225/-1.618 on film and
-0.676/-0.661/-0.795/-1.201 on screen capture. Charging a vector's whole
signalling cost is worse than charging half on both contents; it now says so in
a named constant rather than a bare / 2.0.
Addendum: the intra-versus-inter decision on real coded cost
The attribution in this report put 0.83 dB (film) and 0.45 dB (screen capture)
behind x264's sub-sample and mode refinement, and the sub-sample half landed in
the previous slice. This is the mode half: the intra-versus-inter decision now
codes both branches for real and keeps the one with the smaller squared error
plus lambda times its actual bits, which is what x264 does from subme 6 up.
What it replaced compared an Intra_16x16 SATD, discounted by a fixed 3/4 for the Intra_4x4 that usually follows, against a coded inter cost, and paid for the intra side's signalling with a guessed twelve bits. Three approximations where the encoder can measure instead.
BD-PSNR against x264 at GOP 10 over QP 22/26/30/34, both real clips sampled consecutively from picture 1500:
| film 3840x1608 | screen 2560x1440 | synthetic 352x288 | |
|---|---|---|---|
| SATD and a guess | -1.043 | -1.064 | -4.449 |
| real coded cost | -0.728 | -0.500 | -4.523 |
The bit premium over x264 falls with the distance rather than being traded for it: the screen capture went from +34.4% to +17.2% bits at QP 22 and from +15.9% to +9.7% at QP 34; the film clip from +12.8% to +12.2% at QP 22.
Cost: each P macroblock is coded twice and the loser's reconstruction and entropy writer are thrown away. The screen-capture measurement run went 28.8 s to 30.8 s wall, x264 and the ffmpeg decodes included.
Lambda in that decision was swept, not assumed
| lambda scale | film | screen | synthetic |
|---|---|---|---|
| 0.5 | -- | -- | -5.398 |
| 0.7 | -- | -- | -4.872 |
| 1.0 | -0.728 | -0.500 | -4.523 |
| 1.4 | -0.744 | -0.477 | -4.310 |
| 2.0 | -0.778 | -0.483 | -4.300 |
| 3.0 | -0.835 | -0.494 | -- |
The two real clips are flat between 1.0 and 1.4 and fall off above it; the
synthetic clip keeps improving to 2.0. Unscaled is the film optimum, within
0.023 dB of the screen capture's, and it puts no unmeasured constant in front
of the standard multiplier. The synthetic clip's 0.074 dB is the price, and the
floor in bd_psnr_vs_x264 moved -4.5 to -4.6 to hold the new number.
Constants re-swept under the new decision
allow_i4 in P pictures: on everywhere still. Restricting Intra_4x4 to I
pictures reads -1.613 (film) and -0.508 (screen capture) against -0.728 and
-0.500 with it on. Under the old decision the screen capture preferred the
restriction by a third of a dB; coding both branches for real removed that
disagreement, which is the same class of finding as the sampler: a constant
tuned against a proxy measurement inverts when the proxy goes away.
Two constants of the old decision are gone rather than re-swept, because nothing reads them any more: the twelve-bit intra signalling estimate and the 3/4 Intra_4x4 discount.
What is left
Rate-distortion quantisation (x264's trellis) is worth 0.20 dB on film and 0.10 on the screen capture by the ablation in this report, and we still have none. That is the next slice.
Addendum: rate-distortion quantisation, attempted and not landed
x264's trellis is still worth what the ablation said after the mode decision
changed -- re-measured, because a stale premise is the failure mode this lane
has already hit twice. Ours against x264 with trellis=0 reads -0.535 dB on
the film clip and -0.270 on the screen capture, against -0.728 and -0.478 with
x264 at its default: 0.193 and 0.208 dB of real headroom.
A coefficient-level walk-back was built and measured against it. For each inter luma 4x4 block, levels are stepped toward zero from the end of the scan while squared error plus lambda times bits falls; the bit estimate was made scan-position aware, since a significance flag is only sent up to the last non-zero level and that saving is the whole reason the technique pays. Then the whole macroblock was coded once per candidate strength and the winner picked on exact coded bits, using the trial machinery the mode decision already has.
It buys about 0.02 dB, an order of magnitude short:
| candidates (multiples of the zero-block lambda) | synthetic | film | screen |
|---|---|---|---|
| none (landed) | -4.523 | -0.728 | -0.478 |
| 0, 0.5 | -4.523 | -- | -- |
| 0, 1 | -4.512 | -0.728 | -0.480 |
| 0, 2 | -4.520 | -- | -- |
| 0, 1, 2, 4 | -4.563 | -- | -- |
| 0, 4 | -4.538 | -- | -- |
| 0, 8 | -4.564 | -- | -- |
Two findings, both of which the next attempt has to answer:
- A superset of candidates chosen on exact cost measured worse. Adding 2
and 4 to the candidate set can only lower the local cost of every
macroblock, and it lost 0.05 dB against the pair. So the local cost is not
the quantity the BD-PSNR measures, and searching it harder finds a worse
answer. The obvious explanation -- that a reference picture's quality is
worth more than the local metric can see, GOP 10 making every P picture a
reference for nine others -- does not survive a check: x264 reports
mbtree=0 rc=cqpin the streams this test compares against, so it wins these 0.2 dB with no propagation term either. What is left is that the candidates are generated against an estimate and only chosen on exact bits, and that choosing greedily per macroblock changes the CABAC contexts the next macroblock is measured in. Which of those two it is has not been measured. - The candidate generator is rate-blind where it matters. The levels are walked using an estimate; exact bits only pick between finished macroblocks. x264 decides each coefficient against its actual CABAC context cost, which is the difference the numbers above are consistent with.
So the machinery is not landed -- it doubles the inter coding work for 0.02 dB. The next attempt should decide levels where the contexts exist, at write time, which inter luma allows because its reconstruction does not feed the next block's prediction the way intra's does; and it needs a term for what a reference picture's quality is worth to the pictures that predict from it, without which more search measures worse.
Rate-distortion quantisation, landed on the coder's own price
The attempt above failed on an estimate. This one prices bins with the CABAC engine itself and lands 0.100 dB on the film clip, 0.029 on the screen capture and 0.450 on the synthetic gate, for no measurable encode time.
The price is the thing itself
CabacEnc::residual_block_cost runs the bins residual_block would write --
the same code, through a BinSink the two share -- against a copy of the
context state, charging each bin -log2(p) for its state from a 128-entry
table and moving the state exactly as the coder would. So a level dropped is
priced with the significance map that follows from it, and the adaptation
inside the block is priced too.
Its accuracy is asserted, not assumed: over 400 random blocks it predicts
2 649 288/256 bits where the arithmetic coder then spends 2 650 112/256, an
error of 0.03% (residual_cost_predicts_the_bits_spent). A wrong price would
be invisible in the stream -- it would only show up as lost dB -- which is why
it is a test and not a comment.
What actually moved the numbers
Three changes, measured one at a time against film -0.728, screen -0.500, synthetic -4.523:
| change | synthetic | film | screen |
|---|---|---|---|
| levels walked down on exact cost | -4.530 | -0.732 | -0.449 |
| + dead zone 1/6 -> 1/2 for the walked path | -4.229 | -0.630 | -0.496 |
| + whole-block zero decided on exact cost | -4.087 | -0.628 | -0.469 |
| + search bounded to levels <= 2 | -4.073 | -0.628 | -0.471 |
The first row is the honest answer to the previous attempt's question: an exact rate on its own buys 0.05 dB on screen content and nothing on film. The dead zone was the missing half. A search that can only walk levels down cannot see a level the quantiser's 1/6 dead zone already threw away, so it was choosing among the survivors of a decision it was meant to replace. Quantising the searched path at 1/2 -- round to nearest, x264's own arrangement when trellis is on -- and letting the search take the levels back down is worth 0.102 dB on film and 0.301 on the synthetic clip.
The class: a search bounded by the heuristic it replaces measures the
heuristic. The dead zone constant was swept and defended (enc/quant.rs
says the inter offset is inert across 6/64 to 32/64), and that was true --
of a path with no search after it.
The constants
| RDOQ_LAMBDA (x lambda_ssd), dead zone | synthetic | film | screen |
|---|---|---|---|
| 1.0, 1/6 | -4.530 | -0.732 | -0.449 |
| 0.5, 1/2 | -4.203 | -0.607 | -0.620 |
| 1.0, 1/2 | -4.229 | -0.630 | -0.496 |
| 1.25, 1/2 | -4.103 | -0.657 | -0.433 |
| 1.5, 1/2 | -4.362 | -0.680 | -0.444 |
| 2.0, 1/2 | -4.526 | -- | -- |
| 1.0, 1/3 | -4.295 | -0.647 | -0.465 |
| 1.0, 1/4 | -4.416 | -0.677 | -0.465 |
The two real clips disagree about the rate term -- film wants it lighter, screen capture heavier -- and 1.0 is the crossing point that costs neither. The 1.25 and 1.5 rows are what a screen-only tuning would take, at 0.03 to 0.05 dB of the film clip.
Bounding the search to levels of 2 or less (RDOQ_MAX_LEVEL) costs nothing and
is what makes it free: unbounded, the search tripled our encode time on the
synthetic clip (1.08 s -> 3.53 s); bounded, it is 1.09 s. A large level is
carrying real signal and never wins by shrinking. 1, 2 and 3 read
-4.062/-0.623/-0.496, -4.073/-0.628/-0.471 and -4.065/-0.629/-0.477; 2 is the
best of the three on the clip that gains least.
What is not covered
Intra blocks stay out: their reconstruction is what the next block in the macroblock predicts from, so a level changed there moves a prediction the decision cannot see. Inter chroma AC and the 8x8 transform path are untouched and are the next place to look -- x264's trellis covers both. The coded_block_flag context the price is taken against assumes both neighbours carry coefficients rather than reading them, which is a constant shift across candidates except for the whole-block-zero one.
x264's own trellis is worth 0.193 dB on the film clip and 0.208 on the screen capture (measured by ablation, r2 above). We now take 0.100 and 0.029 of that.
The same price, applied to inter chroma AC
The luma decision was extended to the chroma AC blocks: they are quantised,
searched against the CABAC coder's own price for their levels, and given the
same exact whole-block-zero test, through the one rdoq_4x4 the luma path
uses (it now takes the block category and the DC-skipping flag).
The dead zone had to be re-swept there, because the class the luma work named says a one-directional search measures whatever rounding feeds it. Chroma answered the opposite way. BD-PSNR against x264, at the settled luma constants:
| chroma rounding under the search | film 3840x1608 | screen capture 2560x1440 |
|---|---|---|
| no chroma search (baseline) | -0.628 | -0.471 |
| 1/2 (what luma wanted) | -0.653 | -0.482 |
| 1/3 | -0.644 | -0.481 |
| 1/4 | -0.623 | -0.472 |
| 1/6 (the inter dead zone it had) | -0.593 | -0.468 |
So chroma keeps its dead zone and takes only the pruning half of the search. Chroma AC is sparse at these quantisers: offering more levels spends more bits than the search wins back, and every widening is monotonically worse. The synthetic gate does not move (-4.073 either way) -- its chroma planes are flat.
Landed: film -0.628 -> -0.593 (0.035 dB), screen capture -0.471 -> -0.468. x264's trellis is worth 0.193 dB on the film clip and 0.208 on the screen capture, so the two slices together take 0.135 and 0.032 of it.
The 8x8 luma transform path is still untouched.
The same price, applied to the 8x8 transform path
code_block_8x8 now quantises, searches and zero-tests its inter blocks the
way the 4x4 luma path does, on a residual_block_8x8_cost that shares its
bins with the writer (asserted to 1% over 200 random 64-coefficient blocks,
residual_8x8_cost_predicts_the_bits_spent).
The x264 comparison cannot see this path -- both sides run with 8x8dct=0 at
matched features -- so it is measured as BD-PSNR of the opt-in transform_8x8
flag against flag-off, which isolates it: the flag-off curve does not go
through an 8x8 block.
| rounding under the search | film 3840x1608 | screen capture 2560x1440 |
|---|---|---|
| no search (baseline) | +0.535 | +0.042 |
| 1/6 | +0.551 | -- |
| 1/3 | +0.605 | +0.075 |
| 1/2 | +0.619 | +0.067 |
8x8 answers the way 4x4 luma did and chroma did not: its blocks carry enough coefficients for the wider rounding to pay for itself. Landed at 1/2 -- film +0.535 -> +0.619 (0.084 dB), screen capture +0.042 -> +0.067. The average 8x8 macroblock share rises with it (19.0% -> 20.2% film), which is the flag being chosen more often now that the blocks it picks are cheaper.
transform_8x8 is still opt-in, so this changes no default stream byte; the
open decision to flip that default is worth 0.084 dB more than it was.
The mode-decision multiplier, swept and left alone
lambda_for is 2^((qp-12)/6) in the bits domain, the same ladder x264's
lambda_tab uses, and it had never been swept as a scale. It is now, on both
clips, at the settled quantisation constants -- BD-PSNR against x264:
| lambda scale | film 3840x1608 | screen capture 2560x1440 | sum |
|---|---|---|---|
| 0.5 | -0.577 | -0.635 | -1.212 |
| 0.7 | -0.571 | -0.523 | -1.094 |
| 0.85 | -0.576 | -0.494 | -1.070 |
| 1.0 (kept) | -0.593 | -0.468 | -1.061 |
| 1.4 | -0.647 | -0.490 | -1.137 |
| 2.0 | -0.767 | -0.565 | -1.332 |
The two contents want opposite directions and the ladder sits at the crossing: film would take 0.022 dB from a 0.7 scale and screen capture would lose 0.055 for it. Nothing landed. The synthetic gate agrees the tilt is small (-4.073 -> -4.055 at 0.7), and it is not the film clip's shape.
Recorded so the constant is not re-swept a third time: the remaining gap is not an operating-point tilt. It is bits -- we spend 13% more than x264 at 0.45 dB higher PSNR on the film clip -- so the places left to look are the ones that decide what to spend them on.
Where the bits actually go
The BD-PSNR gate says how much we overspend. It cannot say on what. So the decoder
now counts, per syntax class, the CABAC bits a stream spends: BitAccount marks the
arithmetic decoder's position (bit_position() * 256 + range.leading_zeros()) before
and after each syntax element and attributes the difference to one of eight classes.
Because the counting lives in the decoder, our stream and x264's stream are measured
by the same instrument on the same sources at the same QP -- bit_account_vs_x264
in tests/encode.rs encodes both, decodes both, and prints the table.
Twelve consecutive pictures, GOP 10, CABAC, our defaults against the matched x264 feature set. Percentages are ours relative to x264.
3840x1608 film clip:
| class | qp 26 | qp 34 |
|---|---|---|
| skip | -1.3% | +6.1% |
| mb_type | -11.2% | +3.7% |
| mv | -14.7% | +14.0% |
| cbp+qp | +6.6% | +25.5% |
| intra modes | +11.2% | +12.5% |
| residual luma | +19.8% | +23.9% |
| residual chroma | +7.8% | +6.9% |
| total accounted | +10.9% | +13.4% |
2560x1440 screen capture:
| class | qp 26 | qp 34 |
|---|---|---|
| skip | -21.9% | -17.4% |
| mb_type | -12.2% | +0.8% |
| mv | -24.8% | -31.2% |
| cbp+qp | +8.4% | +13.0% |
| intra modes | +28.9% | +12.6% |
| residual luma | +15.4% | +11.0% |
| residual chroma | +3.8% | -14.1% |
| total accounted | +10.9% | +6.6% |
352x288 synthetic, where the gap is largest, reads the same way only louder: residual luma +64.9% at qp 26 and +82.1% at qp 34, mv -20.0% and -25.7%.
One shape in all three sources. We spend less than x264 on describing motion -- fewer skip bits, fewer mb_type bits, fewer mvd bits -- and more on everything that follows from the prediction those choices produce: residual luma first, then the coded-block-pattern and QP deltas that carry it, then the intra modes we fall back to when inter prediction is not good enough. x264 buys a cheaper residual by paying more for the motion field, and comes out ahead on the sum.
That is a prediction result, not an entropy-coding one. The residual coder is now 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.
P_8x8, and the preset that hid it
The encoder coded 16x16, 16x8 and 8x16 and nothing smaller. It now codes P_8x8
with four P_L0_8x8 sub-partitions: PShape::Quad, a sub_mb_type writer on
both entropy coders, and four searches seeded from the whole-macroblock winner.
Landing it exposed a defect in the comparison itself. The first measurement of
P_8x8 moved neither clip by a thousandth of a decibel, and neither had the split
gate swept just before it. A temporary shape histogram said why: over 1,200,000
macroblocks of the screen capture, whole=1200000 horiz=0 vert=0 quad=0. The
partition search runs only at Preset::Balanced, and bd_psnr_vs_x264 runs
Preset::Fast unless told otherwise -- so every partition number taken through
the default gate was flat by construction. The histogram stays in the encoder
behind EC_H264_SHAPE_STATS, because a flat sweep and a dead code path read
the same from a rate curve and only a count separates them.
Measured where the shapes are actually searched (EC_H264_PRESET_BALANCED=1,
consecutive pictures, GOP 10, BD-PSNR against x264):
| P_8x8 charged | film | screen capture |
|---|---|---|
| not coded | -0.382 | -0.369 |
| 6 bits | -0.379 | -0.364 |
| 12 bits | -0.377 | -0.353 |
| 20 bits | -0.378 | -0.356 |
Both contents put the optimum at twelve bits, which is about what P_8x8 signals: an mb_type one bin longer than 16x8's plus four sub_mb_type bins. Quarters are worth 0.005 dB on camera content and 0.016 on screen capture.
balanced_partitions_decode_bit_exactly now encodes at Balanced under both
entropy coders and requires ffmpeg to reproduce our reconstruction sample for
sample, and requires the sweep to have coded at least one of every shape -- the
existing bit-exactness sweeps run the default preset, so no sub_mb_type had
ever been put in front of another decoder before this.
The decoder counts shapes as well as bits now, so the mix is comparable too. Screen capture at qp 26, twelve pictures, both encoders at their fullest search:
| skip | 16x16 | 16x8 | 8x16 | 8x8 | intra | |
|---|---|---|---|---|---|---|
| ours | 134847 | 4792 | 488 | 226 | 103 | 32344 |
| x264 | 132159 | 6410 | 920 | 355 | 294 | 32662 |
We still split about half as often as x264 at every size, and residual luma is still +10.9%. The gain from charging P_8x8 more rather than less says where that comes from: each sub-partition is searched against the whole macroblock's motion predictor, not its own, so the search misjudges what a split costs to signal and picks the wrong ones. Deriving the real predictor inside the search is the next slice.
Each partition searched against its own predictor
Charging P_8x8 more rather than less was the tell. If the search wanted the quarters talked out of, the thing it was mispricing was not the mb_type but the mvd behind it -- and it was, because every partition of a split macroblock was searched against the whole macroblock's motion predictor. 8.4.1.3 gives 16x8 and 8x16 partitions directional shortcuts and 8x8 sub-partitions the median of their own neighbourhood, so the mvd the search costed was not the mvd the stream would carry. Partitions whose true predictor was close by looked expensive; the search declined them.
The predictor is now derived inside the search loop, per partition. The neighbourhood comes from the picture as before, then the entries that fall inside this macroblock on partitions already chosen are overwritten from the motion vectors this loop has picked -- which is exactly what the encoder will do when it codes them. The chosen predictor also seeds the search.
BD-PSNR against x264 at matched features, consecutive pictures, Preset::Balanced:
| clip | before | after |
|---|---|---|
| 3840x1608 film | -0.377 dB | -0.329 dB |
| 2560x1440 screen capture | -0.353 dB | -0.310 dB |
Both shape-signalling charges were re-swept on the new path, because a change to what a split costs to code changes what it is worth to charge for. The quarters' twelve bits survive (6/12/20 read -0.342/-0.343/-0.346 film, -0.322/-0.314/-0.324 screen). The halves' six do not: at 3/6/10 bits they read -0.329/-0.343/-0.358 film and -0.310/-0.314/-0.326 screen, so the charge is now three. With each half searched against its own predictor its mvd is genuinely cheaper, and the search no longer needs to be argued out of splitting.
The coded_block_pattern group, and a count that wanted two answers
With the motion field closer to x264's, the bit account still puts our overspend in residual luma (+17.1% film, +9.8% screen at qp 26) while our PSNR runs above x264's at every quantiser -- an encoder coding blocks x264 declines to code. The per-block zero test already drops a block whose levels buy less squared error than they cost, but it decides one block at a time, and a block that survives alone in its 8x8 group also drags a coded_block_pattern bit behind it.
x264's answer is decimate: score how tightly a block's levels cluster at the
front of the scan, drop the 8x8 group under four and the macroblock's luma under
six. Ported exactly, it reads:
| group / macroblock threshold | film | screen capture |
|---|---|---|
| off | -0.329 dB | -0.310 dB |
| 2 / 3 | -0.328 | -0.313 |
| 3 / 4 | -0.335 | -0.283 |
| 4 / 6 (x264's) | -0.346 | -0.242 |
| 6 / 9 | -0.374 | -0.227 |
Monotone in opposite directions: camera grain wants the count off, screen capture wants it harder than x264 sets it, and no single threshold serves both. That is a heuristic standing in for a test the encoder can just run, so it runs it: a group is coded only when its blocks buy more squared error than their levels plus the pattern bit are worth.
The first attempt at that test kept the per-block test's rate estimate -- a CAVLC-shaped count with a 0.4 correction factor -- and reproduced the same split, film wanting the charge at zero and screen wanting it unbounded. The estimate was the thing with two answers. CABAC's own cost for the same levels is already in the encoder for RDOQ, in 256ths of a bit; against that exact rate the screen capture develops an interior optimum where before it had none:
| lambda scale | film | screen capture |
|---|---|---|
| 0.25 | -0.315 dB | -0.297 dB |
| 0.5 | -0.314 | -0.267 |
| 0.6 | -0.321 | -0.252 |
| 0.75 | -0.335 | -0.212 |
| 0.9 | -0.354 | -0.202 |
| 1.0 | -0.372 | -0.193 |
| 2.0 | -0.575 | -0.277 |
Screen capture would still take more; camera grain would take less, because a grain block scoring as noise is carrying signal the metric is measuring. Six tenths is the most the film clip takes without losing ground against where it started, so both contents gain and neither pays: film -0.329 -> -0.321, screen capture -0.310 -> -0.252.
The same test was swept on the 8x8 transform path, where it is worth nothing:
0, 0.3 and 0.6 read +0.620/+0.624/+0.608 dB on the film clip's transform_8x8
gate and +0.007/+0.007/+0.009 on the screen capture, all inside its noise. One
8x8 block is a whole pattern group, so its own zero test already decides what
this one would. It is not kept.
eight_by_eight_gains_on_flat_and_text pins what the flag buys, and both its
arms move when the paths under them change: the flag-off arm is the 4x4 path
that just improved, and the flag-on arm is not. Its delta drops from 0.255 to
0.230 dB while the absolute synthetic comparison against x264 improves from
-4.060 to -3.969 dB, and the gate is re-pinned with that written next to it.
The gate could not see chroma, so chroma decisions kept winning by starving it
cbp_chroma has the same group structure the luma coded_block_pattern test
just fixed: it is 0 (nothing), 1 (DC only) or 2 (DC and AC), so a single
surviving AC block takes the pattern to 2 and makes every other AC block spend
a coded_block_flag saying it is empty. The same group RD test applies -- pool
the eight AC blocks, compare coding them against zeroing them all, and charge
the pattern step.
Swept against the gate as it stood, the charge looked like free money: the film
clip went -0.310, -0.292, -0.247, -0.178, -0.069, +0.079 dB BD at 0, 4, 8, 16,
32 and 64 bits, crossing x264 and still climbing at the top of the grid. A
result that good from one constant is a reason to check the instrument, and the
instrument was wrong: bd_psnr_vs_x264 measured PSNR on the luma plane alone.
Every bit the encoder stopped spending on chroma came back as luma quality, and
the metric counted only the second half of that trade. A chroma decision read
against a luma-only metric can only ever conclude "code less chroma".
The gate now also reports an all-plane PSNR, pooled by sample count the way
x264 reports it -- for 4:2:0 that weights luma 6 and each chroma plane 1 -- and
prints it as BD-PSNR-YUV next to the luma number. On that metric the same
sweep reads -0.688, -0.681, -0.659, -0.653, -0.670, -0.742 dB on the film clip
and -0.537, -0.527, -0.530, -0.517, -0.524, -0.522 on the screen capture. Both
contents turn over at sixteen, and past the turn the luma number keeps rising
while the real one falls -- the shape the luma-only gate was blind to.
CBP_CHROMA_BIT is baked at 16.
The larger finding is the metric itself. At the settings this lane has been tuning, we are -0.18 dB luma on the film clip but -0.65 dB all-plane, and -0.23 against -0.52 on the screen capture. Chroma is the bigger half of the remaining gap to x264 and no measurement in this lane had ever seen it. Every earlier constant in this report was chosen on the luma-only number; the ones that can move chroma bits are owed a re-sweep.