Commits
Commits on
552edc4cede0dfd9bebbc4e344242802802d15ccFiltered to crates/ec-h265/tests/encode.rsClear filter2026-08-28
perf(h265): estimated per-context fracbits table replaces exact CABAC replay in RDOQ trial costingAhmet Kaan Gümüş · 50e18de
perf(h265): estimated per-context fracbits table replaces exact CABAC replay in RDOQ trial costing RDOQ's per-candidate rate was priced by re-simulating the exact adaptive CABAC coder for every trial (encode_subblock measured 26% self time, cost structural to exact counting since range/low/context state carries forward). Adds an x265/HM-`entropyBits`-style estimated fractional-bit table (ENTROPY_BITS[64], entropy of the current CDF/probability state via the CABAC probability-state geometric model, alpha=0.9493) and a CabacEncoder `estimating` mode that looks it up instead of replaying range/renorm arithmetic. Only RDOQ's dedicated counting instrument (`rdoq_enc`) ever estimates; the committing encoder always writes exact bits, so streams stay conformant. Measured: ec-bench h265 640x360x10f encode ~2953ms -> ~1833ms (1.61x, 4-run median each). bd_psnr_vs_x265 (synthetic clip): BD-PSNR-YUV +7.880 -> +7.948 dB vs x265 (a gain, not a loss), wall time 28.85s -> 20.70s. Both comfortably clear the repo's adopt bar (speedup >=1.15x, BD loss <=0.05 dB) with room to spare, so `EncoderConfig::rdoq_estimate` defaults on; `false` keeps the exact-replay path for A/B. rdoq_cache_hash's pinned bytes re-pinned to the new default's output.
2026-08-26
feat(h265): in-loop deblocking filter, on by defaultAhmet Kaan Gümüş · 96b7b12
feat(h265): in-loop deblocking filter, on by default Spec 8.7.2, written from the text. An intra-only stream simplifies it twice over: intra prediction reads unfiltered reconstruction, so the filter is a post-pass over the finished picture and the picture-hash SEI stays honest; and every edge has bS = 2, so the whole strength derivation reduces to finding the transform-block boundaries. TuMap carries one byte per 4x4 unit -- log2 of the transform covering it. Transform blocks of side S are aligned to multiples of S, so an edge at x is a boundary exactly when x % S == 0, and no edge list is needed. Each wavefront worker publishes its band; the encoder stitches them, filters every vertical edge, then every horizontal one over the result. Writing the filter exposed an encoder bug: the RQT split trial marks four child transform blocks, and when the nosplit trial won nothing rewrote those cells, so the map reported a boundary inside a 32x32 transform. Reconstruction and coefficients were restored between trials; the side map was not. The nosplit branch now marks its own transform, and tu_log2 rides the CU-depth meta snapshot. ffmpeg decodes every shape bit-exactly with the filter on. BD-PSNR against a deblocking x265, our filter off then on: film -0.197 -> +0.063 dB, screen capture +0.606 -> +0.693, web clip +0.567 -> +0.686, phone clip +0.113 -> +0.162, synthetic +8.781 -> +8.796. Every clip improves, so it defaults on.
2026-08-25
feat(h265): a 64x64 coding tree block may stay one coding unitAhmet Kaan Gümüş · 826a8f8
feat(h265): a 64x64 coding tree block may stay one coding unit HEVC's largest transform is 32x32, so a 64x64 intra coding unit splits its transform tree without a split_transform_flag -- the split is inferred from log2TrafoSize > MaxTbLog2SizeY -- and predicts per 32x32 transform block in z-order, each quadrant marked coded before the next reads its neighbours. What the coding unit buys is three split flags and three mode signallings, against one luma direction for the whole 64x64. On by default; only reachable with ctb_size 64, which is not the default. Against always splitting a 64x64 tree block it is worth +0.002 dB BD-PSNR on a film frame and +0.098 dB on a screen capture.
feat(h265): residual quadtree, one split level, on by defaultAhmet Kaan Gümüş · 11904c0
feat(h265): residual quadtree, one split level, on by default A 2Nx2N intra coding unit may now split its luma transform once into four half-size children, chosen against the unsplit block by J = SSD + lambda*bits. Chroma splits with the tree while its own blocks stay 4x4 or larger; at an 8x8 luma tree chroma stays at the parent size and is coded with the last child (7.3.8.8). The parent luma cbf is the OR of the children's. Measured against x265 over the four-point ladder it is worth +0.010 dB BD-PSNR on film and +0.080 dB on screen capture, so it is on by default. Flipping the default surfaced a transform-skip desync: transform_skip_flag is read for every 4x4 residual block once the picture parameter set enables skip, and the encoder decided it from the coding unit's size, so the 4x4 chroma children of a 16x16 unit were coded with a real transform and no flag. The decision now travels by the transform block's own size.
Merge branch 'lane-h265-tskip': transform skip, measured and left offAhmet Kaan Gümüş · d563e2c
Merge branch 'lane-h265-tskip': transform skip, measured and left off Transform skip is a content-dependent win: on a 2560x1440 screen capture it is worth +0.243 dB BD-PSNR (+0.030 to +0.273) and +0.158 BD-PSNR-YUV, while on a 3840x1608 film clip it costs -0.008 dB. It stays opt-in until the encoder picks it per clip.
test(h265): take the clip's dimensions by positionAhmet Kaan Gümüş · d04b63e
test(h265): take the clip's dimensions by position ffprobe prints one comma-separated line per stream and recent builds end it with a trailing separator, so splitting the line in two handed the parse a field with a comma still on it and every clip-driven gate panicked before it measured anything.
feat(h265): transform skip on 4x4 transform unitsAhmet Kaan Gümüş · f5dbd0b
feat(h265): transform skip on 4x4 transform units A 4x4 transform unit may now code its residual samples directly instead of transforming them (transform_skip_flag, 7.3.8.11), which is what screen content wants: sharp edges spread over every basis function, and the transform costs bits to undo them. The PPS carries transform_skip_enabled_flag, the flag itself is coded per 4x4 TU on its own pair of contexts (luma and chroma, init value 139 in Table 9-25), and the residual path swaps the integer transform for a pair of shifts. Those shifts are the whole feature: the decoder scales what it reads by tsShift = 5 + Log2(nTbS) and takes the bit-depth shift of 12 back off, so a coefficient carries the sample scaled by the difference. Writing the encoder's own shift as tsShift instead was caught by the decode: 3117 samples of a 64x64 picture differed. Off by default, so every existing stream stays byte-identical; the BD gate reaches it through EC_H265_TSKIP. Gated by ffmpeg decoding three picture shapes sample for sample against the encoder's reconstruction, and by a unit round trip over every 8-bit residual value. Three mutations were checked to fail those: tsShift one short, the flag never written, and one wrong context init value.
test(bd): refuse a sample window with nothing in itAhmet Kaan Gümüş · 7642b35
test(bd): refuse a sample window with nothing in it A clip-driven BD run whose sample window is a film's black head codes to almost no bits and decodes back to the source exactly, so every point on both curves sits at the 99 dB PSNR cap, the two cubics coincide, and the gate prints +0.000 dB. That is not a measurement, and it read as one: an ablation re-measure reported the same +0.000 for both arms of the knob. Both BD gates now refuse such a window by name and say which variable samples past it. The h265 gate also fails with "ladder collapsed" when its QP shift clamps two ladder points onto the same QP, instead of the "singular BD-PSNR fit" assert deep inside the cubic solver.
feat(h265): rate-distortion quantisationAhmet Kaan Gümüş · 21ef192
feat(h265): rate-distortion quantisation
Every non-zero level up to 2 is offered its lower magnitudes and zero, and
the alternative is kept when the rate the CABAC coder actually charges for
it, priced against the transform-domain error the change costs, comes out
cheaper. The rate comes from the coder itself: a counting CabacEncoder is
restored to a fixed base state per trial and asked to code the block, so
the price includes the significance map and the greater-than-one flags
rather than a model of them. Distortion is converted from the coefficient
domain to sample squared error by (n * n) / 128^2, which is what this
transform's scaling leaves for a block of side n. A whole-block-zero trial
follows, since the cbf flag alone can be the cheaper coding.
With a downward-only search the quantiser feeding it must not already
round down, so the RDOQ path quantises with a round-to-nearest offset
(dead zone 2) instead of the intra default of 3; the search then decides
what to drop. All four constants were swept against the clip gate:
luma dead zone 1/2 +0.360, 1/3 +0.315, 1/4 +0.118 (film BD-PSNR-YUV)
chroma dead zone kept at 1/2: 1/3 and 1/4 climb on luma (+0.568/+0.585)
while all-plane BD-PSNR falls (+0.169/+0.128)
max level 1 -> +0.244, 2 -> +0.276, 3 -> +0.277
lambda 1.0 -> +0.276, 1.25 -> +0.294, 1.5 -> +0.293 on film,
but -0.392/-0.408/-0.441 on screen capture
Measured on the real clips, default on:
1080p film +0.246 -> +0.360 dB BD-PSNR vs x265
2560x1440 screen capture -0.479 -> -0.357 dB
Bit exactness is gated separately: rdoq_decodes_bit_exactly encodes three
frame shapes at QP 27, asserts the bitstream differs from the plain path,
and decodes each with ffmpeg against the kept reconstruction with zero
sample mismatches.
The synthetic fallback clip in the BD gate needed its ladder alignment
extended for this: our curve now spans a much wider bitrate range than
x265's over the same QP spacing, so after the geometric-mean rates match,
the ladder is drawn in towards its own centre until both curves cover the
same range. Single-QP shift corrections are ignored, which only made the
alignment oscillate between neighbouring shifts.feat(h265): sign data hiding, correct and shipped offAhmet Kaan Gümüş · 0877a36
feat(h265): sign data hiding, correct and shipped off Implements sign_data_hiding_enabled_flag per 7.3.8.11: for each 4x4 sub-block whose first and last significant scan positions are more than three apart, the sign at the first position is not coded and the decoder infers it from the parity of the sub-block's summed absolute levels. The encoder nudges one already-non-zero level by one when the parity comes out wrong, picking the coefficient whose ideal (pre-rounding) level sits closest to the nudge. Proven bit-exact: sign_hiding_decodes_bit_exactly encodes 64x64, 130x66 and 352x288 with hiding on and off, asserts the streams differ, and decodes both through ffmpeg with zero sample mismatches. Shipped off. On the clip gate it loses: -0.033 dB BD-PSNR on a 2560x1440 screen capture (-0.479 -> -0.512 vs x265) and -0.043 dB on 1080p film (+0.246 -> +0.203). Hiding is mandatory for a qualifying sub-block, so every one of them pays whether its cheapest nudge is cheap or not, and levels straight off the quantiser's rounding make that nudge expensive. A rate bias on the nudge picker was swept at +-0.25 and +-0.5 units of squared level error per bit and lost in both directions, so the picker is distortion-only. Worth re-measuring once RDOQ lands, which is where this pays for other encoders.
fix(h265): bound the clip gate's decode to the pictures it samplesAhmet Kaan Gümüş · 6e90b9d
fix(h265): bound the clip gate's decode to the pictures it samples `clip_sources` asked ffmpeg for the whole file as raw I420 and sliced the result afterwards. A 1080p feature is hundreds of gigabytes that way: a run on a full film grew the test binary to 29 GB of anon RSS and was killed by the kernel OOM killer, with the machine swapping hard. Short captures fit, so the gate had only ever been run on content that hid it. Trim inside ffmpeg instead -- `select=gte(n,SKIP)` drops the skipped pictures and `-frames:v WANT` stops the decode once enough have come out, so the process holds exactly the frames it measures. The same film run now finishes in 10 s inside an 8 GB cap, and the numbers on the short capture are unchanged.
feat(h265): chroma prediction mode search, measured and left offAhmet Kaan Gümüş · fea923b
feat(h265): chroma prediction mode search, measured and left off Chroma was always coded in the derived mode: `intra_chroma_pred_mode` was a hardcoded 0 bin. The syntax offers five modes — planar, vertical, horizontal and DC, each swapped for angular 34 when it collides with the luma mode, plus derived — and nothing had ever measured what the other four are worth. `choose_chroma_mode` now codes all five into a counting CABAC snapshot, both planes and their residuals, and keeps the lowest `SSD + lambda * bits`. It is off by default, because measured against x265 on four real clips it is a wash (BD-PSNR luma / all-plane, derived -> search): | clip | derived | search | |------|---------|--------| | 4K film | -0.685 / -0.703 | -0.757 / -0.606 | | 1138x640 web | -0.507 / -0.556 | -0.584 / -0.505 | | 1440p screen capture | -2.465 / -2.360 | -2.617 / -2.460 | | 1080p phone | -0.366 / -0.387 | -0.477 / -0.384 | Luma falls on every clip — the extra chroma bits come out of the same budget at a fixed QP — while all-plane gains 0.10 dB and 0.05 dB on the two camera clips, is flat on the third and loses 0.10 dB on the screen capture, for 10-15% more encode time. Average +0.01 dB all-plane is not a default. The lambda is not the cause: `chroma_rd_weight` swept at 0.25/0.5/1.0/2.0 leaves the screen capture losing at every weight, and 1.0 is the best weight wherever the search wins at all. Correctness is gated on its own: `chroma_mode_search_decodes_bit_exactly` asserts the stream differs from the derived-only one (so the explicit modes really fired) and that ffmpeg decodes it sample-for-sample. EC_H265_CHROMA_RD and EC_H265_CHROMA_W in the BD gate re-run both sweeps.
perf(h265): give five intra candidates the rate-distortion trialAhmet Kaan Gümüş · b24f633
perf(h265): give five intra candidates the rate-distortion trial The luma mode search screened 35 intra modes with SATD and handed only two survivors to the full rate-distortion trial. That count was never measured; it was the number the search was written with. Swept against x265 at matched features on four real clips (BD-PSNR luma / all-plane, 24 pictures after 1500, ladders auto-aligned, 0% extrapolation): | k | 4K film | 1138x640 web | 1440p screen capture | 1080p phone | |---|---------|--------------|----------------------|-------------| | 1 | -0.840 / -0.815 | -0.728 / -0.733 | | | | 2 | -0.731 / -0.730 | -0.603 / -0.632 | -2.521 / -2.420 | -0.435 / -0.444 | | 3 | -0.703 / -0.709 | -0.556 / -0.596 | -2.493 / -2.402 | -0.401 / -0.416 | | 4 | -0.693 / -0.705 | -0.530 / -0.574 | | | | 5 | -0.685 / -0.703 | -0.507 / -0.556 | -2.465 / -2.360 | -0.366 / -0.387 | | 8 | -0.684 / -0.710 | -0.471 / -0.525 | -2.453 / -2.368 | -0.340 / -0.361 | Every clip improves monotonically to five. Eight buys +0.001 dB on the film clip and takes its all-plane figure backwards, for another 20% of wall time, so the knee is where the default now sits: film 38s -> 49s for the whole four-QP ladder, web clip 8s -> 11s. The synthetic floor moves with it (+2.456 -> +2.507 luma, +3.158 -> +3.238 all-plane) and EC_H265_RDO_CANDIDATES in the gate test re-runs the sweep.
2026-08-24
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.
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).
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.