forge
Explore
Log in

Don't you dare go Hollow

Commits

Commits on 5591c0e1dfbc7e0137f2c8d4fb224bfcb05d01baFiltered to crates/ec-h265/src/transform.rsClear filter

2026-08-27

perf(h265): SIMD the forward/inverse transform's odd dot productAhmet Kaan Gümüş · b9e8391
perf(h265): SIMD the forward/inverse transform's odd dot product

wide::i32x8 across the 8+ wide chunks of forward_1d's/inverse_1d's
matrix*sample sums, scalar tail for the rest; also hoist inverse_1d's
src[2j+1] gather out of the per-x loop (it didn't depend on x). Bounds
for i32 accumulation documented on `dot`. Correctness gate green
(cargo test -p ec-h265 --release, rdoq_cache_hash_is_pinned unmoved);
instruction count over a 4-frame 1080p single-thread profile loop:
142,512,057,557 -> 142,473,095,572 (-0.03%, most TUs at this content/QP
are 8x8/4x4 where half<8 never enters the vector path).
perf(h265): SIMD quantize_offset with the wide-crate idiomAhmet Kaan Gümüş · 44291e4
perf(h265): SIMD quantize_offset with the wide-crate idiom

Coefficients are bounded to +/-32768 and qp is clamped to 0..=51, so
abs(c)*scale+offset never overflows i32 -- the whole quantiser loop runs
in i32x8 lanes and is the exact same integer as the scalar i64 path,
just batched 8 coefficients at a time with a scalar tail.

Correctness gate: cargo test -p ec-h265 --release green incl.
rdoq_cache_hash_is_pinned (bit-identity pin unaffected).

perf stat -e instructions,cycles -r 3 over a 4-frame 1080p single-thread
encode_idr loop (examples/profile_encode.rs, not committed):
  before: 117,195,791,981 instructions / 23,255,778,501 cycles
  after:  116,465,368,212 instructions / 22,246,309,650 cycles
  (-0.63% instructions, -4.3% cycles)
perf(h265): flatten the transform matrix branch out of the hot butterfly loopsAhmet Kaan Gümüş · c180d55
perf(h265): flatten the transform matrix branch out of the hot butterfly loops

Profiled the encode path first (perf record --call-graph dwarf on a 1080p
single-thread encode_idr loop): forward_1d is 28% of self-time, inverse_1d
9.7%, well ahead of anything in residual.rs/cabac.rs (residual::rdoq is
2.2%, cabac put_bit 0.9%) -- the RDOQ/CABAC bookkeeping levers named in the
charter are not where the time is; the integer transform is.

m32() computed each matrix entry on the fly with a branch (col<16 / row
parity) inside forward_1d's and inverse_1d's innermost multiply-accumulate
loop. Mirrored the same values out to two flat 32x32 tables at compile time
(FULL_MATRIX and its transpose FULL_MATRIX_T, both built by a const fn that
literally calls m32 -- so this is m32 evaluated ahead of time, not a new
derivation) and index them instead, so the hot loop is a branchless
multiply-add over a contiguous array read.

perf stat -e instructions,cycles on the same 4-frame 1080p single-thread
workload, best of 3: 105.7B -> 97.2B instructions (-8.1%), 22.6B -> 17.8B
cycles (-21%, before run noisy at +-10.5%). Short of the 15% instruction
target; intra::predict (12% of self-time) and quantize_offset (6.4%) are
the next levers and are untouched.

Bit-identical: rdoq_cache_hash's 416x240 and 1920x1080 encodes hash the
same (da5d606db0755d97, f08a816c9fa9ca2c) with git-stashed transform.rs
before this change as with it; pinned that equality as an assertion
(rdoq_cache_hash_is_pinned) since the existing test only printed. Full
ec-h265 --release suite green (56 tests across 7 binaries).

2026-08-25

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.
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.

2026-08-21

2026-08-14

feat(h265): full-depth quadtree search, VA-API check, parse-side fuzzAhmet Kaan Gümüş · b19a281
feat(h265): full-depth quadtree search, VA-API check, parse-side fuzz

The search goes down to 8x8 by default because on real pictures that is
both better and no slower: measured on a 1080p camera frame at QP 22,
-27% bits and +1.5 dB against stopping at 16x16, at the same wall time.
The 16x16 floor stays available as a setting.

Two defects the earlier tests could not see, both found by pointing the
encoder at real footage:

- stopping the search early skipped split_cu_flag entirely, while the
  sequence still allowed a split. Every picture decoded to noise. The
  flag is written now whatever the search decides.
- a fuzzed SPS could name a 45-bit POC field and take ec-core's bit
  reader past its documented contract. Picture size, POC width and
  long-term set count are validated where they are parsed.

Also: VA-API decode of the encoder's own output is a test (radeonsi, real
GPU), progress counters are one per cache line - twelve workers polling
eight counters per line spend their time invalidating each other - and
waits back off to a sleep instead of spinning on a core someone needs.
feat(h265): integer transforms, quantiser and the CABAC coderAhmet Kaan Gümüş · 990d3ac
feat(h265): integer transforms, quantiser and the CABAC coder

The inverse transform and the dequantiser are the normative ones, so the
encoder's reconstruction is what a decoder produces. Only the first 16
columns of the 32x32 matrix are typed in; the rest follow from the
matrix's own symmetry, and tests check both the printed second half and
orthogonality, because a mistyped column is the classic defect here.

The CABAC coder can run in counting mode - identical context updates, no
bytes kept - which is how rate-distortion decisions are priced at the
rate they actually cost. A spec-derived decoder in the tests reads back
what the encoder writes.