Commits
Commits on
ccc465055a391e2af3f4e978dc532261a6ffaa442026-08-25
perf(h264): choose the Intra_8x8 luma mode by rate-distortionAhmet Kaan Gümüş · ccc4650
perf(h264): choose the Intra_8x8 luma mode by rate-distortion The Intra_8x8 chooser scored the nine prediction modes on SATD of the prediction residual plus a mode-bit term. That is a decision taken before the quantiser has a say: it never sees which mode's residual survives quantisation cheaply. Same class as the intra chroma chooser fixed in 98d01b7, and the last SATD-only intra site. Every allowed mode is now predicted, coded at the real QP and scored as SSD + lambda * bits, with the rate read from the CABAC coder's own 8x8 price (residual_block_8x8_cost — Luma8x8 has no coded_block_flag of its own, the CBP carries it, so the cbf-indexed cost path panics on it). BD-PSNR against x264 at matched features, 8x8 transform on, GOP 10, 24 consecutive pictures, luma / all-plane YUV: 3840x1608 film +0.475 / -0.068 -> +0.512 / -0.035 1138x640 web clip +0.040 / -0.031 -> +0.076 / +0.003 854x480 web clip +0.070 / +0.047 -> +0.132 / +0.108 2560x1440 screen -0.206 / -0.199 -> -0.215 / -0.207 Three clips of four gain on both metrics; the screen capture loses 0.008 dB. Encode time on the 854x480 clip goes from 12 s to 13 s. The path only runs when the 8x8 transform is enabled, which is still opt-in, so no default stream byte moves; the byte-identity conformance tests confirm it.
2026-08-24
Merge branch 'lane-h265-gate': BD-PSNR against x265 with self-aligning laddersAhmet Kaan Gümüş · addbfd0
Merge branch 'lane-h265-gate': BD-PSNR against x265 with self-aligning ladders Adds the ec-h265 gate: BD-PSNR and BD-PSNR-YUV against x265 at matched features, both sides decoded through ffmpeg, with the QP ladders aligned from a probe encode so the Bjontegaard integral interpolates. The extrapolation share of each curve is measured and a run above 20% fails with a named message instead of reporting a cubic guess.
test(h265-gate): align the QP ladders from a probe encodeAhmet Kaan Gümüş · 8b6026d
test(h265-gate): align the QP ladders from a probe encode A fixed QP offset assumed our encoder is always leaner than x265 at matched QP. That holds on the smooth synthetic clip (probe shift -6, reproducing the hand-picked 16/20/24/28) but not on real content: on a 3840x1608 film and a 1138x640 web clip the probe measures a shift of 0, and the hand-picked ladder put 51% and 72% of the integration range outside the measured bitrate span, so the guard fired and the BD number would have been a cubic guess. The ladder is now derived: both sides encode once at QP 26, the bit ratio becomes a QP shift at ~6 QP per factor of two in bits, and x265's 22/26/30/34 plus that shift is our sweep. If the extrapolation share still exceeds 20%, the shift is re-estimated from the geometric-mean bit ratio of the two full curves, at most twice, before the existing panic fires. The probe encodes are reused as the QP-26 rungs, so the alignment costs no extra encode. With the ladders aligned, the real gap is visible: -0.731 dB luma / -0.730 dB YUV on the film, -0.603 dB / -0.632 dB on the web clip, 0% extrapolation on every curve. The synthetic floor stays at +2.306 dB and gates only the pinned clip; a media file is measured, not gated. Two comments corrected while here: ffmpeg's `-qp` does reach libx265 (`qp=N` and `rc=cqp:qp=N` are byte-identical, so the prefix is dropped), and the shallow all-intra rate-quality slope is content behaviour, not a rate-control misconfiguration.
perf(h264): choose the intra chroma mode by rate-distortionAhmet Kaan Gümüş · 495e450
perf(h264): choose the intra chroma mode by rate-distortion The chooser scored the four chroma prediction modes on SATD of the prediction plus the bits naming the mode, deciding before the quantiser had a say. Every available mode now goes through the real chroma coding path and is scored on reconstructed SSD plus what its levels and its name actually cost, which is the same measurement the rest of the macroblock decisions already make. Measured with bd_psnr_vs_x264 against x264 -preset medium, BD-PSNR-YUV, GOP 10, 24 consecutive pictures: 3840x1608 film -0.500 -> -0.443 dB, 2560x1440 screen capture -0.220 -> -0.146 dB, its second scene -0.571 -> -0.538 dB, a 1080p phone clip -1.122 -> -1.084 dB, a 720p web clip -0.606 -> -0.572 dB. Five clips of five improve; luma is unmoved (film -0.065 -> -0.070 dB) and encode time does not change measurably, since only intra macroblocks pay and only four chroma trials each.
fix(h265-gate): offset QP ladders so BD-PSNR is a measurement, not a guessAhmet Kaan Gümüş · 4ae6e08
fix(h265-gate): offset QP ladders so BD-PSNR is a measurement, not a guess The gate reported BD-PSNR -38.258 dB with 50% extrapolation on our side — the two rate-quality curves did not overlap. Root cause was misdiagnosed as ffmpeg -qp not reaching x265 rate control; in fact -qp and rc=cqp:qp=N produce byte-identical output. x265 at CQP on this smooth synthetic clip genuinely has a shallow rate-quality slope (29% bitrate drop across 12 QP). The fix: pass rc=cqp:qp=N inside the x265 param string (explicit, not relying on ffmpeg flag translation) and offset the QP ladders so the bitrate ranges overlap. Our sweep drops to 16/20/24/28; x265 stays at 22/26/30/34. At matched bitrate our PSNR is higher on this clip. Measured table (352x288, 24 pictures, Clip::render_smooth): qp 16: ours 809632 bits 53.63 dB qp 20: ours 577872 bits 51.26 dB qp 24: ours 433584 bits 48.84 dB qp 28: ours 316664 bits 45.86 dB qp 22: x265 835424 bits 51.50 dB qp 26: x265 745104 bits 49.19 dB qp 30: x265 674088 bits 46.37 dB qp 34: x265 596808 bits 42.97 dB cubic extrapolation: candidate 0% anchor 0% BD-PSNR: +2.456 dB luma / +3.158 dB YUV Guard: bd_psnr_delta now returns (bd, cand_extrap, anc_extrap); the test panics with a named message when either curve is extrapolated over more than 20% of the integration range. Floor recalibrated: BD_PSNR_VS_X265_FLOOR = 2.306 (measured 2.456 - 0.15).
perf(h264): give the chroma DC block a zero testAhmet Kaan Gümüş · 59070a1
perf(h264): give the chroma DC block a zero test Every other block in this encoder is asked whether coding it pays: luma 4x4 and 8x8, the chroma AC group, the macroblock's shape and skip flag. The chroma DC block was the exception — whatever the quantiser produced was coded, however little it bought. It gets the same test now. With only DC in a block the inverse transform gives every one of its sixteen samples the same (dc + 32) >> 6, so the squared error of zeroing the block is exact from the AC residual that is already reconstructed at that point: one sum per block, no second trial. BD-PSNR-YUV against x264 (GOP 10, QP 22/26/30/34, Preset::Balanced), test off -> on: film -0.504 -> -0.500, screen capture -0.227 -> -0.220, phone -0.300 -> -0.297, handheld -0.886 -> -0.884. The multiplier's ladder is in the comment on CHROMA_DC_LAMBDA; the peak is flat between 0.5 and 0.75 and turns sharply worse above 1.
ec-h265: add bd_psnr_vs_x265 gate against x265Ahmet Kaan Gümüş · 15b6d35
ec-h265: add bd_psnr_vs_x265 gate against x265 BD-PSNR of this encoder against x265 at matched features (intra-only, no B frames, in-loop filters off, AQ/psy/RDOQ off, QP 22/26/30/34) on the synthetic 352x288 24-picture clip, both sides decoded through ffmpeg. Measured: BD-PSNR luma -38.258 dB, BD-PSNR-YUV -31.656 dB. Floor calibrated at measured - 0.15 = -38.41 dB. The gap is wide because this encoder spends 40-70% fewer bits than x265 at every QP, so its rate-quality curve sits below x265's and the union-range cubic integral extrapolates; the convention is documented in bd_psnr_delta.
Merge branch 'lane-mkv-gate': the gate opens Matroska tooAhmet Kaan Gümüş · 832b640
Merge branch 'lane-mkv-gate': the gate opens Matroska too The BD-PSNR gate demuxed only through ec-mp4, so every .mkv in the user's library was refused before a single picture was measured. Probe the container by its bytes instead — ftyp at offset 4 for MP4, EBML magic for Matroska — and refuse a non-H.264 video track by naming its codec.
perf(h264): choose chroma_qp_index_offset from the sourceAhmet Kaan Gümüş · eb83b6f
perf(h264): choose chroma_qp_index_offset from the source The offset was a hardcoded 0 in the PPS. Sweeping it showed the optimum is not a constant: a film clip is best at 0 and loses 0.16 dB at -2, while every screen capture and phone clip gains, one of them by 0.23 dB. What separates them is how textured the chroma planes are relative to luma — flat chroma is what wants the extra chroma bits, because its few edges are all the eye has to read there, and film's chroma carries as much detail as its luma, where the same bits buy more. So measure it: mean absolute sample gradient of the chroma planes over that of luma, taken on the first source picture, and code at -2 below half. The offset now reaches the PPS, the encoder's own chroma QP derivation and the deblocking parameters of every slice it writes, so reconstruction still matches a decode exactly. BD-PSNR-YUV against x264 (GOP 10, QP 22/26/30/34, Preset::Balanced) over nine clips, before -> after: -0.504 -> -0.504 (film, keeps 0), -0.406 -> -0.227, -0.495 -> -0.268, -0.601 -> -0.570, -0.639 -> -0.575, -0.931 -> -0.886, -0.510 -> -0.491, -0.352 -> -0.340, -0.376 -> -0.300. The rule reproduces each clip's own swept optimum.
ec-h264: probe container by bytes, accept Matroska alongside MP4Ahmet Kaan Gümüş · d5e9b8f
ec-h264: probe container by bytes, accept Matroska alongside MP4 Factor both demux call sites in tests/encode.rs onto one helper, open_h264_source, that reads 12 bytes from the file head and dispatches to ec_mp4::Mp4Demuxer (ftyp at offset 4) or ec_matroska::MatroskaDemuxer (EBML magic 1A 45 DF A3) — never by extension. A non-H.264 video track (AV1, HEVC) is refused with a message naming the codec, not a demuxer panic. Add ec-matroska as a dev-dependency. BD-PSNR ours vs x264 over q22/26/30/34: -0.732 dB (FMA 01, 1280x720, H.264-in-Matroska, 24 pictures). AV1 .mkv refusal confirmed: 'video track is Av1, not H.264 — this gate measures H.264 only'.
perf(h264): decide the P shape and skip flag on coded costAhmet Kaan Gümüş · 6897ed7
perf(h264): decide the P shape and skip flag on coded cost
The motion search ranked its shapes by an SATD total plus a guessed
signalling charge, and the macroblock took that ranking as final. That
total is a prediction-domain estimate: it never sees the residual the
shape causes, so a split that looks cheaper to predict can cost more to
code than the 16x16 it displaced, and the encoder had no way to notice.
Intra against inter was already decided by coding both branches and
comparing squared error plus lambda times real bits. This extends the
same treatment to the inter side: choose_inter now returns every shape
it costed, most promising first, with P_Skip appended when the search
did not already prefer it, and the macroblock codes the list and keeps
the smallest coded cost.
BD-PSNR-YUV against x264 at GOP 10 over QP 22/26/30/34, Balanced:
film 3840x1608 screen 2560x1440
1 candidate -0.580 -0.474
2 -0.521 -0.459
3 -0.513 -0.447
5 -0.504 -0.406
monotone with no turnover, so the whole list is coded. Luma-only moves
-0.121 to -0.079 and -0.190 to -0.128. The film clip's comparison run
goes from 46s to 64s: a P macroblock is now coded up to five times and
every loser's reconstruction and entropy writer is thrown away, which
is why the list is Balanced-only and Fast still codes the SATD winner
alone.
This is the step the x264 feature ladder localised: subme 4 reads
-0.290 and subme 6 reads -0.581 on the film clip, the difference being
exactly that x264 stops estimating the mode decision and codes it.perf(h264): search the diagonals tooAhmet Kaan Gümüş · cdfa038
perf(h264): search the diagonals too The whole-sample motion search stepped on a cross, so it could only walk along an axis. A macroblock moving diagonally has to be reached in two steps through a point that is worse than either -- and a step search stops at the first point it cannot improve on, so it never took the first step. Step on a square instead. Worth 0.032 dB BD-PSNR-YUV on the film clip and 0.041 on the screen capture, for twice the candidates in the stage that reads the reference in place and never interpolates. The opening step size was swept alongside at 4, 8 and 16 whole samples and stays at four; wider is not a wider search, it only starts the walk further from the seeds, which are already the answer most of the time.
feat(h264): let the inter decision see chromaAhmet Kaan Gümüş · 5f9d4c9
feat(h264): let the inter decision see chroma Every decision in the P path was made on luma alone: the motion search, the partition shape, the skip flag. Chroma was then coded against whatever vector came out, having never been asked about. Add the two chroma planes to the cost of the candidates that survive to the shape decision -- one motion compensation and one SATD per candidate, which the search proper could not afford but a handful of finalists can. Weight swept at 0, 0.25, 0.5, 1 and 2 on two contents, read on BD-PSNR-YUV; half is the optimum. Worth 0.041 dB on the film clip and nothing on the screen capture, which is small enough to be the useful part of the result: our chroma trails by 0.5-0.9 dB per plane at every QP, and this rules out the vector as the reason. Weighting chroma higher does lift chroma PSNR, monotonically, and past a half it spends more bits than the chroma is worth.
test(h264): report chroma per plane, and stop dropping the first x264 paramAhmet Kaan Gümüş · 8adad23
test(h264): report chroma per plane, and stop dropping the first x264 param The gate compared two encoders on a pooled all-plane number without ever saying which plane either one was losing. Print U and V separately per QP for both sides; on the film clip they say our chroma trails x264 by 0.48-0.90 dB uniformly across QP while we spend more bits, which is a prediction deficit rather than a starved one. EC_H264_X264_PARAMS was spliced in with a colon of its own, so a value written with a leading colon produced "::subme=1" -- and ffmpeg drops the token that follows an empty one. The parameter silently did not apply and the sweep read as "this x264 feature is worth nothing". Trim the colons off the value instead. Corrected attribution of x264's lead on the film clip, as BD-PSNR-YUV: its RD mode decision is worth 0.739 dB, its trellis quantisation 0.206, and the two are additive -- with both stripped we lead by 0.278 dB.
feat(h264): measure all three planes, and a chroma coded_block_pattern testAhmet Kaan Gümüş · 49e08f5
feat(h264): measure all three planes, and a chroma coded_block_pattern test The rate-quality gate measured PSNR on the luma plane alone, so any decision that could trade chroma bits for luma bits read as a pure gain. It now also reports an all-plane PSNR, pooled by sample count the way x264 reports it -- luma 6, each chroma plane 1 for 4:2:0 -- as BD-PSNR-YUV beside the luma number. Chroma gets the group test luma already has: cbp_chroma is 0, 1 (DC) or 2 (DC and AC), so one surviving AC block takes the pattern to 2 and makes the other seven each spend a coded_block_flag saying they are empty. The eight AC blocks are pooled, coding them is compared against zeroing them all, and the pattern step is charged CBP_CHROMA_BIT. Swept at 0, 4, 8, 16, 32 and 64 bits the charge reads -0.688/-0.681/-0.659/ -0.653/-0.670/-0.742 dB all-plane BD on a 3840x1608 film clip and -0.537/ -0.527/-0.530/-0.517/-0.524/-0.522 on a 2560x1440 screen capture; both turn over at sixteen. Read on luma alone the same sweep climbs monotonically to +0.079 dB at 64, which is the encoder buying luma by throwing chroma away. On the all-plane number the gap to x264 is -0.65 dB on the film clip against -0.18 luma, and -0.52 against -0.23 on the screen capture: chroma is the larger half of what is left, and nothing in this lane had been able to see it.
feat(h264): decide the coded_block_pattern group by rate-distortionAhmet Kaan Gümüş · 835b128
feat(h264): decide the coded_block_pattern group by rate-distortion Our residual luma still ran 10 to 17 per cent over x264's at a PSNR above x264's: blocks coded that x264 declines to code. The per-block zero test decides one block at a time, so a block surviving alone in its 8x8 group drags a coded_block_pattern bit behind it uncounted. x264 settles that with `decimate`, a count of how tightly the levels cluster at the front of the scan. Ported exactly, its optimum runs monotonically opposite on the two real clips -- camera grain wants it off, screen capture wants it harder than x264 sets it -- so it is a heuristic standing in for a test the encoder can run. Run the test instead: code the group only when its blocks buy more squared error than their levels plus the pattern bit are worth. That test needs the rate to be right. On the per-block estimate it reproduced the same content split; against CABAC's own cost for the same levels -- already in the encoder for RDOQ, in 256ths of a bit -- screen capture develops an interior optimum it never had. Both lambda and the pattern charge are swept, tables in their doc comments. BD-PSNR against x264 at matched features, consecutive pictures, at the balanced preset: 3840x1608 film -0.329 -> -0.321 dB, 2560x1440 screen capture -0.310 -> -0.252 dB. The same test on the 8x8 transform path is worth nothing across its sweep and is not kept; the negative result is recorded where the constant lives. The transform_8x8 flag gate pins the delta between two arms and drops from 0.255 to 0.230 dB because the flag-off arm is the path that improved -- the absolute synthetic comparison against x264 moves from -4.060 to -3.969 dB -- so it is re-pinned with that written beside it.
feat(h264): search each inter partition against its own predictorAhmet Kaan Gümüş · 1db6da0
feat(h264): search each inter partition against its own predictor A split macroblock's partitions were all searched against the whole macroblock's motion predictor, so the mvd the search costed was not the mvd the stream would carry: 8.4.1.3 gives 16x8 and 8x16 partitions directional shortcuts and 8x8 sub-partitions the median of their own neighbourhood. Partitions whose real predictor sat close by looked expensive and the search declined them. Derive the predictor inside the search loop instead. The neighbourhood comes from the picture, then the entries falling inside this macroblock on already-chosen partitions are filled from the vectors this loop has picked -- what the encoder will do when it codes them. That predictor also seeds the search. Both shape-signalling charges are re-swept on the new path. The quarters' twelve bits survive; the halves' six do not, and become three, with the sweep tables in their doc comments. BD-PSNR against x264 at matched features, consecutive pictures, at the balanced preset: 3840x1608 film -0.377 -> -0.329 dB, 2560x1440 screen capture -0.353 -> -0.310 dB.
2026-08-23
feat(h264): code P_8x8, and count shapes as well as bitsAhmet Kaan Gümüş · 2b1c4a3
feat(h264): code P_8x8, and count shapes as well as bits The encoder had no partition smaller than 8x16. It now codes P_8x8 with four P_L0_8x8 sub-partitions, including the sub_mb_type writer both entropy coders were missing. The first measurement of it read flat, as had the split-gate sweep before it, and a shape histogram said why: the partition search runs only at Preset::Balanced, and the x264 comparison runs Preset::Fast by default, so 1,200,000 macroblocks were coded whole and nothing was ever split. Measured where the shapes are searched, P_8x8 is worth 0.005 dB BD-PSNR on the film clip and 0.016 on the screen capture, with the optimum at twelve bits of signalling on both. The bit-exactness sweeps ran the default preset too, so no sub_mb_type had ever faced another decoder: balanced_partitions_decode_bit_exactly encodes at Balanced under CABAC and CAVLC, requires ffmpeg to reproduce the reconstruction sample for sample, and requires every shape to appear. The decoder now reports MbShapes alongside BitAccount, which puts our partition mix next to x264's: we still split about half as often.
perf(h264): search every partition, and document the split gateAhmet Kaan Gümüş · 338bd3f
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.
feat(h264): count CABAC bits by syntax class in the decoderAhmet Kaan Gümüş · 0723a49
feat(h264): count CABAC bits by syntax class in the decoder The rate gate says how much we overspend against x264; it cannot say on what. BitAccount marks the arithmetic decoder's position before and after each syntax element and attributes the difference to one of eight classes, so any conforming stream -- ours or x264's -- is measured by one instrument. bit_account_vs_x264 encodes both, decodes both, prints the table. The answer is the same on all three sources: we spend fewer bits than x264 on skip, mb_type and mvd, and more on residual luma, cbp+qp and intra modes. x264 buys a cheaper residual by paying more for the motion field. That points the next lane at prediction, not at entropy coding.
h264: record the mode-decision lambda sweep, which changes nothingAhmet Kaan Gümüş · 6deb39c
h264: record the mode-decision lambda sweep, which changes nothing The RD multiplier ladder had never been swept as a scale. It is now, on both real clips at the settled quantisation constants, and it sits on the crossing: film wants 0.7 (0.022 dB), screen capture wants 1.0 and loses 0.055 for the same move. Nothing landed; the table is in the lane report so the constant is not swept a third time.
h264: price the 8x8 inter blocks on their own bins tooAhmet Kaan Gümüş · fad4297
h264: price the 8x8 inter blocks on their own bins too residual_block_8x8_cost costs the cat-5 bins the writer writes, and the 8x8 path quantises, searches and zero-tests through it the way the 4x4 luma path does. quant_8x8 grew the same offset-naming sibling the 4x4 quantiser has, so the search picks the rounding that feeds it. Swept again rather than inherited: 8x8 answers like 4x4 luma, not like chroma, and keeps round-to-nearest. Measured as BD-PSNR of the opt-in transform_8x8 flag against flag-off, which is what isolates this path: film +0.535 -> +0.619, screen capture +0.042 -> +0.067. No default stream byte moves -- the flag is still off by default.
h264: price inter chroma AC levels the same wayAhmet Kaan Gümüş · e21a764
h264: price inter chroma AC levels the same way The RDOQ decision built for inter luma now also runs on the chroma AC blocks: rdoq_4x4 takes the block category and the DC-skipping flag, and block_ssd reconstructs through the AC-only unzigzag and dequant when it is set. The rounding that feeds the search had to be swept again rather than inherited. Luma wanted the wider 1/2 rounding once a search could take levels back down; chroma is monotonically worse for every widening and keeps the 1/6 inter dead zone it already had, taking only the pruning half. BD-PSNR against x264 on two real clips: film -0.628 -> -0.593, screen capture -0.471 -> -0.468; the synthetic gate is unchanged at -4.073 because its chroma planes are flat.
h264: choose inter luma levels against the CABAC coder's own priceAhmet Kaan Gümüş · 47083d3
h264: choose inter luma levels against the CABAC coder's own price Rate-distortion quantisation for inter luma 4x4: each level is offered the cheaper magnitudes below it, and takes one when the squared error it gives up costs less than the bits it saves. The rate is exact. `CabacEnc::residual_block_cost` runs the same bins `residual_block` writes -- one body behind a `BinSink` the writer and the price share -- against a copy of the context state, charging each bin -log2(p) for its state and moving the state as the coder would. Over 400 random blocks it predicts 2649288/256 bits where the coder then spends 2650112/256, an error of 0.03%, which is asserted rather than assumed: a wrong price is invisible in the stream and shows up only as lost dB. An exact rate on its own was worth almost nothing (film -0.728 -> -0.732, screen capture -0.500 -> -0.449). The half that pays is the dead zone: 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 the decision it was meant to replace. Quantising the searched path at 1/2 and letting the search take the levels back down is worth 0.102 dB on the film clip and 0.301 on the synthetic gate. Deciding the whole-block zero on exact bits too adds 0.142 dB there. BD-PSNR against x264 at matched features: film 3840x1608 -0.728 -> -0.628, screen capture 2560x1440 -0.500 -> -0.471, synthetic -4.523 -> -4.073, the floor moved to -4.15. Bounding the search to levels of 2 or less keeps it free (synthetic run 1.08 s before, 1.09 s after; unbounded it was 3.53 s). Intra stays out: its reconstruction is what the next block in the macroblock predicts from. Inter chroma AC and the 8x8 path are not covered yet.
h264: withdraw the propagation explanation from the RD quantisation noteAhmet Kaan Gümüş · c844bb5
h264: withdraw the propagation explanation from the RD quantisation note The addendum blamed the superset-measures-worse result on a reference picture's quality being worth more than the local metric can see. x264 reports mbtree=0 rc=cqp in the streams this test compares against, so it takes its 0.2 dB with no propagation term either, and the explanation does not hold. The measurement stands; the cause is named as unsettled, with the two remaining suspects and the experiment that separates them.
h264: record what coefficient-level RD quantisation measuredAhmet Kaan Gümüş · cf13a82
h264: record what coefficient-level RD quantisation measured x264's trellis is worth 0.193 dB on the film clip and 0.208 on the screen capture, re-measured against `trellis=0` now that the mode decision has changed. A coefficient-level walk-back, with a scan-position-aware bit estimate and the finished macroblocks picked on exact coded bits, buys about 0.02 of that, so it is not landed: it doubles the inter coding work for a fiftieth of the distance. Two results worth keeping. A superset of candidates chosen on exact local cost measured 0.05 dB worse than a pair of them, which is local rate-distortion being myopic about a reference picture the next nine predict from. And the generator walks levels on an estimate rather than against the CABAC context that will actually code them, which is where x264's version gets its order of magnitude.
h264: decide intra against inter on real coded costAhmet Kaan Gümüş · dbbd3cc
h264: decide intra against inter on real coded cost The comparison was a prediction-domain SATD, discounted by a fixed 3/4 for the Intra_4x4 that usually follows, against a coded inter cost, with the intra side's signalling paid for by a guessed twelve bits. Both branches are now coded for real and the one with the smaller squared error plus lambda times its actual bits is kept -- the decision x264 makes from subme 6 up, and the mode half of the 0.83 dB the ablation in the lane report put behind its refinement. BD-PSNR against x264 at GOP 10 over QP 22/26/30/34: the film clip goes -1.043 to -0.728, the screen capture -1.064 to -0.500, and the bit premium falls with the distance rather than being traded for it (the screen capture spends +17.2% over x264 at QP 22 where it spent +34.4%). The synthetic clip loses 0.074 dB; it prefers a heavier rate term in that decision, which the two real clips do not, so the standard multiplier goes in unscaled and its floor moves -4.5 to -4.6. Each P macroblock is coded twice and the loser's reconstruction and entropy writer are thrown away: one measurement run went 28.8 s to 30.8 s wall. Intra_4x4 in P pictures was re-swept under the new decision and stays on everywhere; the two constants of the old decision are deleted rather than re-swept, because nothing reads them any more.
docs(h264): name and sweep the search's motion-vector weightAhmet Kaan Gümüş · a5aa64e
docs(h264): name and sweep the search's motion-vector weight The motion search halved a vector's signalling cost with a bare `/ 2.0`. Swept on consecutive pictures it holds: 0.25/0.5/1.0/2.0 read -1.074/-1.043/-1.225/ -1.618 dB BD-PSNR against x264 on the film clip and -0.676/-0.661/-0.795/-1.201 on the screen capture, so a half is the flat part and charging the whole rate is worse on both contents.
perf(h264): iterate sub-sample refinement to convergence at every presetAhmet Kaan Gümüş · f7bce7d
perf(h264): iterate sub-sample refinement to convergence at every preset The refinement stopped after a single pass at Fast. Running it to convergence is worth 0.111 dB BD-PSNR against x264 on the film clip and 0.035 on the screen capture, for 2% of the comparison's wall clock; the synthetic floor tightens to -4.5. Attribution of what is left, measured by disabling x264's features one at a time: its whole-sample search pattern is worth 0.04 dB, its rate-distortion quantisation 0.20, and its sub-sample and mode refinement 0.83 -- so the rate-distortion mode decision is where the remaining distance is.
docs(h264): re-measure the two parked decisions on the corrected samplerAhmet Kaan Gümüş · 463b2bc
docs(h264): re-measure the two parked decisions on the corrected sampler `Preset::Balanced` and `transform_8x8` were both parked on numbers taken one picture a second. On consecutive pictures Balanced wins on both contents (-0.875 vs Fast's -1.154 on film, -0.506 vs -0.696 on screen capture) for 11% more wall clock, and transform_8x8 reads +0.800 dB on film and +0.537 on screen capture where it used to read 0.000 there.
fix(h264): measure the encoder on consecutive pictures, and pay for going intraAhmet Kaan Gümüş · ba061b6
fix(h264): measure the encoder on consecutive pictures, and pay for going intra The real-clip comparison against x264 sampled one picture a second, so P pictures had nothing to predict from and the P path was never really under test: on a film clip our P slices ran 93.2% intra, 6.3% skip, 0.5% coded inter. The sampler now takes consecutive pictures, with EC_H264_CLIP_STRIDE to restore a wider spacing and EC_H264_CLIP_SKIP to pass over a film's titles first. Corrected, the encoder read -1.521 dB BD-PSNR on film and -1.904 on screen capture, spending 20-42% more bits than x264 with the premium growing with QP. Two defects the old sampler had hidden: - The intra side of the mode decision carried no rate term at all, while the inter side paid for its motion vector. Twelve bits for the macroblock type and the prediction modes is the overlap of the two contents' flat parts; a previous sweep of this same constant landed zero on the uncorrelated content. - Sub-sample refinement chose on SAD, which reads interpolation's smoothing as a better match. It now costs candidates on SATD. Together: film -1.521 -> -1.154 dB, screen capture -1.904 -> -0.696, synthetic -5.718 -> -4.542, and the bit premium is flat in QP again (+14-18%) instead of growing to +42%. The synthetic floor tightens to -4.6. The other P-path constants swept under the old sampler were re-swept and all held: Intra_4x4 in P pictures, the P_Skip rebate, and the Intra_4x4 discount.
mp3: gate VBR against the reference ladder, and split its frames tooAhmet Kaan Gümüş · 806ee38
mp3: gate VBR against the reference ladder, and split its frames too VBR had no reference gate, which is why the demand-weighted frame split landed in the CBR arm only. The gate encodes every rung of libmp3lame's VBR ladder on each source and compares against the rung that actually spent what we spent -- nominal rates are no guide, since V6 is nominally 115 kbit/s and lands near 60 on speech. Measured that way the shipped VBR lost almost everywhere: 11 of 14 rows behind on correlation and up to 8 dB behind on the worst 20 ms window at the same rate. The demand split takes that to 10 rows ahead, and recalibrating the quality bar takes it to 13. The recalibration is the second half of the split: reaching the same masking bar for fewer bits has to be spent, not pocketed, because the quality knob promises a mean rate. The bar now tightens by 1.6 - 12q decibels, fitted so quality 0.3 lands at 162 kbit/s against its promised 163 and quality 0.6 at 232 against 230. Floors are set from the measurement: correlation -0.0005, worst-window excess 3.5 dB, and 8% of the matched rung's rate, with top-rung rows exempt from the rate check because the reference cannot spend more there.
mp3: split the frame budget by each channel's bit demandAhmet Kaan Gümüş · e90e593
mp3: split the frame budget by each channel's bit demand CBR handed every granule-channel the same share of the frame, so the quiet channel of a mid/side pair held bits it had no way to spend while the loud one was starved. The quantiser codes |xr|^(3/4), so the sum of that over a granule's lines is the shape of its demand; the frame is now split by it, blended seven parts demand to three parts even. The blend is where the two gate metrics disagree. Correlation keeps improving to a pure demand split, but the worst 20 ms window turns around before it: at full demand the loud granules take so much of the frame that a quiet one cannot code its own transient, and one row falls 1.8 dB behind the reference. Seven tenths takes nearly all the correlation and leaves every row's worst window ahead. All fourteen sweep rows now beat libmp3lame on both metrics. The correlation floor becomes zero -- falling behind the reference at all is the regression -- and the worst-window excess tightens from 5.0 dB to 1.0. The split is CBR only. VBR chooses its bitrate from the noise-to-mask ratio a candidate reaches, and that mapping is calibrated to the even split: quality 0.5 settles at 154.6 kbit/s rather than 208 with the demand split in. Both want to move together, against a VBR reference gate that does not exist yet.
mp3: code correlated stereo as mid/sideAhmet Kaan Gümüş · 21447bf
mp3: code correlated stereo as mid/side The encoder declared joint stereo in every frame header and then wrote mode_extension zero, which is plain left/right. The decoder had implemented mid/side all along, so the two spoken-word sources in the library sweep were coded twice over -- once per channel -- at half the bits each, and sat 12.6 dB behind the reference in the worst 20 ms window. A frame now takes mid/side when the difference of its channels is quieter than their sum, which is every correlated mix and no opposed one. Both channels of a frame take the same window, because adding a long block's lines to a short block's describes two different time slices. A mid/side pair is held to whichever channel masked less, since an error in the side channel lands in both ears. All fourteen sweep rows improve or hold. Five now beat the reference where three did; the worst window on the speech rows closes to a tie at 192 kbit/s. Both gate floors tighten onto the new worst rows: correlation to -0.0012, and worst-window excess from 13.5 dB to 5.0 dB.
perf(mp3): stop amplifying at 85% of the frame budget, not 95%Ahmet Kaan Gümüş · 5912f29
perf(mp3): stop amplifying at 85% of the frame budget, not 95% The worst-window metric from the last commit left a question: on the speech sources the worst 20 ms barely improves when the bitrate rises by half, so something other than the bit supply was capping it. The distortion loop has three stop conditions and none of them had been swept. Two are not live. Raising the round cap from 20 to 60 gives bit-identical output, so the loop never reaches it, and amplifying one band per round instead of three is a wash. The third is the budget break, and it is the whole story. Stopping at 85% instead of 95% improves every one of the fourteen library rows in correlation and none of them regresses in the worst window; reelm and hein at 192 kbit/s gain 2.8 and 1.4 dB. The mean gap to the reference at 128 kbit/s halves, from -0.00058 to -0.00033. The other direction shows why. Letting the loop amplify to 100% of the budget costs reelm 0.0048 correlation: past the budget an amplified band buys nothing and the rate loop coarsens every other band to pay for it. Below 70% the condition stops binding at all, since the first round already spends more. Both gate floors tighten onto the new worst rows.
fix(mp3): wake the block switcher, and measure what it is forAhmet Kaan Gümüş · 4ca0311
fix(mp3): wake the block switcher, and measure what it is for The switcher decided a granule was a transient when the next granule's total energy grew eightfold. A granule is 13 ms, and music meets that about once a minute: counting block types in our own coded streams found one short granule in 9196, against libmp3lame's 88 over the same audio. It has been dead code since it was written. Sweeping the constant over 2 through 32 moved whole-file correlation by 0.00004, which is the second half of the finding: the crate's quality gate cannot see pre-echo at all. A few milliseconds of noise smeared backwards before an attack disappears into a minute of correlation, and a gate like that would have accepted the switcher being deleted. So the sweep now also reports the worst 20 ms error-to-signal ratio, for ours and for the reference, skipping windows under -60 dBFS. Against that metric the constant is not flat: at the shipped 8.0 one window of a real file carries more error energy than signal (+1.1 dB), the curve is flat from 1.6 to 4, and the cliff sits between 4 and 8. The shipped value was just past the edge of it. 4.0 lands, at the cheap end of the flat part. The new gate's floor is set from the measurement and the measurement is unflattering: we lead the reference in the worst window on all four music sources and trail it on all three speech ones, worst by 14 dB. On those, the worst window barely improves when the bitrate rises by half, so the limit is the masking model rather than the bits. That is the next round.
test(mp3): measure the encoder against LAME on real sourcesAhmet Kaan Gümüş · e722375
test(mp3): measure the encoder against LAME on real sources The crate's only quality gate compared correlations against a table frozen from the encoder this one replaces, measured on synthetic fixtures. A frozen number cannot notice the reference moving, and its thinnest row passed by 0.00014. This adds a live one. `real_library_sweep_vs_lame` encodes the same sixty seconds of the same file twice at matched constant bitrate -- once with `Mp3Encoder`, once with libmp3lame -- decodes both through the same decoder, aligns each against the samples that went in, and correlates. Matched CBR puts the two sizes within 0.2% of each other, so what is left is quality. Fourteen rows across seven sources land between -0.00137 and +0.00115, and the floor is set just under the worst of them. The gap halves from 128 to 192 kbit/s on every source and is positive on the sparsest material, which points at bit allocation on dense content rather than at the filterbank.
docs(lane): mark the screen-capture rows that are not comparableAhmet Kaan Gümüş · 5900f75
docs(lane): mark the screen-capture rows that are not comparable Rounds 1-2 and rounds 3+ measured different captures, so only the film clip and the fixture span the lane.
docs(lane): close the x264 rank-1 lane with its before-and-after tableAhmet Kaan Gümüş · 82c74f8
docs(lane): close the x264 rank-1 lane with its before-and-after table Also records the rejected Intra_16x16 rate-distortion mode search and why it does nothing where the Intra_4x4 one did.
docs(lane): record the class sweep of the remaining decision constantsAhmet Kaan Gümüş · d72bafc
docs(lane): record the class sweep of the remaining decision constants Four more swept the same way; three are content-split and one is inert, so none of them lands. Written down so the sweep is not repeated.
docs(lane): record the three swept decision constantsAhmet Kaan Gümüş · b939e05
docs(lane): record the three swept decision constants Chroma cost, the intra penalty in P pictures and the P_Skip rebate, with the per-QP rows showing the intra penalty was not a rate-quality trade, and why the synthetic fixture now disagrees in sign.
perf(h264): size the P_Skip rebate to what skipping actually savesAhmet Kaan Gümüş · 1bfd070
perf(h264): size the P_Skip rebate to what skipping actually saves The skip candidate was handed a four-bit rebate. Sweeping it against x264 on three clips puts the flat part of the curve at 0-2 bits: on a 3840x1608 film clip at GOP 10, 0 and 2 read -0.113 and -0.110 dB where 4 reads -0.233 and 8 reads -0.708. The screen capture and the synthetic clip move by 0.01 dB across the same range. Two rather than zero because skipping does save signalling, and two is the top of the flat region.
perf(h264): stop penalising intra macroblocks in P picturesAhmet Kaan Gümüş · 942a61a
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.
perf(h264): choose the chroma mode on SATD plus its signalling costAhmet Kaan Gümüş · cce1dad
perf(h264): choose the chroma mode on SATD plus its signalling cost The chroma prediction mode was picked on SAD with no rate term, while everything else in the mode decision costs on SATD. The residual goes through the transform, and naming the mode costs between one and five bits (intra_chroma_pred_mode is ue(v)), so both belong in the cost. Measured with bd_psnr_vs_x264 against x264 -preset medium: clip all-intra GOP 10 3840x1608 film -0.620 -> -0.535 -1.495 -> -1.404 2560x1440 screen capture -0.231 -> -0.197 -0.062 -> -0.033 352x288 synthetic ramp - -5.297 -> -5.291 at no measurable encode time (20.4 s and 22.7 s on the film clip, both before and after). SATD alone is about two thirds of the gain and the rate term the rest. Full ec-h264 suite green.
docs(lane): record the Intra_4x4 rate-distortion refinement roundAhmet Kaan Gümüş · 947e714
docs(lane): record the Intra_4x4 rate-distortion refinement round Adds the cross-content table, the survivor-count saturation point, the rate-term calibration that made the search work at all, and what the next mode-decision slices are.
perf(h264): re-cost the Intra_4x4 mode survivors with real quantisationAhmet Kaan Gümüş · 8cda34e
perf(h264): re-cost the Intra_4x4 mode survivors with real quantisation Ablating the reference showed x264's rate-distortion mode decision is worth 0.80 dB intra of its lead, so the mode decision is where the distance is. This does the first half of it: SATD still ranks the nine Intra_4x4 modes, but the top three are re-costed by actually quantising and reconstructing the block and charging reconstruction SSD plus coded bits. A mode that predicts slightly worse can quantise to far fewer levels, and SATD cannot see that. Measured with bd_psnr_vs_x264 against x264 -preset medium: 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 for 3-4% encode time. Three survivors is where it saturates: all nine landed within 0.002 dB of three for 7% more time. The rate term needs the same ZERO_BLOCK_LAMBDA correction the zero-block test uses -- block_bits counts CAVLC-ish widths, and with the full term the search loses 0.065 dB instead of gaining, and loses more the more candidates it sees. The synthetic clip pays 0.157 dB for this (-5.140 -> -5.297) and the floor moves to -5.40. Its smooth ramps quantise unlike anything in a real frame; all four measurements on real content gain. Full ec-h264 suite green.
test(h264): attribute x264's lead to its mode decision, not trellisAhmet Kaan Gümüş · b36a1af
test(h264): attribute x264's lead to its mode decision, not trellis EC_H264_X264_PARAMS appends to the parameter list the gate hands libx264, so a single reference feature can be switched off and the distance re-measured. On the 3840x1608 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 0.10 dB inter and 0.16 dB intra, so RDOQ is not the lever it looked like. x264's rate-distortion mode decision is worth 0.80 dB intra and a further 0.58 dB inter -- with it off we are ahead of x264 all-intra. We pick macroblock and prediction modes on SATD plus an estimated rate; x264 picks the survivors on true coded bits against reconstruction SSD. That is where the remaining 1.4 dB is.
docs(h264): the dead zone is not where the distance to x264 isAhmet Kaan Gümüş · b25750f
docs(h264): the dead zone is not where the distance to x264 is Swept both rounding-offset numerators against x264 with bd_psnr_vs_x264 on a 3840x1608 clip, spelling them as sixty-fourths the way x264 spells --deadzone-intra / --deadzone-inter. Two results, both recorded in round_offset's doc comment and the lane report: The intra offset is the one that matters, and ours is the better of the two. x264's 11/64 reads -1.667 dB all-intra where ours (1/3, i.e. 21/64) reads -0.745 dB, with the inter value making no difference to that. The inter offset is inert across the useful range: 6/64, 16/64, 21/64 and 32/64 span -1.571 to -1.532 dB, a 0.04 dB spread, while the bit counts move 0.8% -- so the path is reached and simply does not matter. That leaves the remaining -1.557 dB (0.75 dB intra, 0.81 dB P pictures) for trellis/RDOQ, P_8x8 sub-partitions and the intra mode decision.
perf(h264): try Intra_4x4 in P pictures at every presetAhmet Kaan Gümüş · f0a6733
perf(h264): try Intra_4x4 in P pictures at every preset The BD-PSNR gate against x264 put us 2.992 dB behind on a 3840x1608 clip from the library at Preset::Fast, and 1.587 dB behind at Preset::Balanced. Ablating the four places the two presets differ shows the whole difference sits in one of them: allowing Intra_4x4 in P pictures reads -1.557 dB on its own, while a 48-sample search range (-3.039 dB), the half-macroblock partitions (-3.009 dB) and the subpel refinement (-3.035 dB) each land inside the noise of the -2.992 dB baseline. So Intra_4x4 is now tried in P pictures at every preset. The cost is about 5% encode time (21.75 s against 20.67 s for the 24-picture measurement), which is what a 1.4 dB rate-quality move is worth. Against x264 on that clip we now spend 5-10% more bits instead of 19%, at 0.3-1.3 dB lower PSNR instead of 1.0-1.7 dB. The synthetic floor moves with the default: -5.202 dB to -5.140 dB, and the constant is retightened to -5.25. No test in the repo pins encoder bytes, so nothing else re-pins.