forge
Explore
Log in

Don't you dare go Hollow

Commits

Commits on mainFiltered to crates/ec-h265/src/residual.rsClear filter

2026-10-23

Restore edith_codecs after local data loss (2026-10-23)Ahmet Kaan Gümüş · b7434a2
Restore edith_codecs after local data loss (2026-10-23)

On 2026-10-23 an automated agent ran `rm -rf $HOME` and deleted the local
repository with all of its history. This commit restores the working tree
from the newest surviving copy, the host-c server lane mirror. That mirror
matches local main as of the testcut Phase 2 reports, including the tool
cache, the nextest fast profile and changed-crates.sh. The history before
this commit was not recovered.

2026-09-06

feat(h265): decide transform skip per 4x4 luma block by rate-distortionAhmet Kaan Gümüş · a9d41bf
feat(h265): decide transform skip per 4x4 luma block by rate-distortion

Unconditional 4x4 transform skip is worth +1.8 dB BD-PSNR-YUV on one
screen-capture clip and -0.34 dB on another, so the tool wants a per-block
decision, not a switch. TransformSkip::Rd codes the committed 4x4 luma block
both ways and keeps the cheaper one.

The writer emitted the transform_skip_flag only when it was set, which was
consistent only because every 4x4 skipped; the decoder reads a bin for every
4x4 once the PPS enables the tool. encode_residual now takes Option<bool>:
None when the PPS left the tool off, Some(v) for the flag to write.

2026-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.
perf(h265): hoist scan-table lookups out of encode_subblock's hot RDOQ loopAhmet Kaan Gümüş · b54343a
perf(h265): hoist scan-table lookups out of encode_subblock's hot RDOQ loop

profile-first (perf record on ec-bench's 640x360x10f h265 encode row):
encode_subblock is 26% self time, the single hottest symbol, driven by
RDOQ's per-trial tail re-encode (encode_subblock_range walks every
sub-block from the trial position down to sub-block 0 on every candidate,
since a real adaptive-CABAC bit count cannot be decomposed into a local
delta -- range/low state and context adaptation both carry forward). That
replay cost is structural to exact-CABAC RDOQ and not safely reducible
without changing which candidate wins (disallowed: this lane's gate is
bit-exact output before/after).

What's still free: log2_size/sub_wide/the two scan tables were re-derived
from `n` on every single encode_subblock call, even though they are loop
invariants for the whole encode_subblock_range walk (and, in rdoq's own
direct-call site, invariant for the whole rdoq() call). Hoisted to the
callers and threaded through as parameters -- same values, fewer
recomputations, zero behavior change.

Verified bit-exact: sha256 of ec-bench's h265 AU output identical
before/after (temporary EC_BENCH_DUMP_H265_AUS dump hook, reverted).
cargo test -p ec-h265 --release: 57 passed, 0 failed, including the
pinned rdoq_cache_hash tests. No measurable wall-clock change at the
640x360x10f bench's scale (~2610-2650ms both sides, noise-dominated) --
this hoist's savings are real but too small relative to the required
CABAC replay volume to show above that noise floor.

2026-08-27

perf(h265): hoist sig_ctx_inc's sub-block-invariant part out of the position loopAhmet Kaan Gümüş · 07800cd
perf(h265): hoist sig_ctx_inc's sub-block-invariant part out of the position loop

prev_csbf and the additive context base in sig_ctx_inc depend only on the
sub-block's own coordinates (xs/ys), never on the scan position within it,
but were recomputed on every one of up to 15 significant-flag positions per
sub-block. Split into sig_ctx_base (once per sub-block) + sig_ctx_inc (per
position), shared by encode_subblock and decode_residual so both stay in
sync. Correctness gate green (cargo test -p ec-h265 --release,
rdoq_cache_hash_is_pinned unmoved). Instructions over the 4-frame 1080p
single-thread profile loop, stacked on the transform SIMD commit:
142,473,095,572 -> 141,277,415,462 (-0.84%).
perf(h265): sub-block frozen-prefix CABAC cache for RDOQ trialsAhmet Kaan Gümüş · 4c5b62b
perf(h265): sub-block frozen-prefix CABAC cache for RDOQ trials

rdoq() re-encoded every sub-block from last_sub down to 0 on every trial,
even though a trial only ever changes one coefficient. Sub-blocks above the
one being trialed never change (csbf-neighbour lookups and the greater1_ctx
carry both flow from a higher scan sub-block to a lower one, never the
reverse), so a cursor now caches the CABAC state right before the sub-block
under trial and is advanced one committed sub-block at a time as trials move
to lower ones; only a trial that moves the block's last significant position
(the header itself changing) still falls back to a full re-encode from the
block's start.

Entry-gate test (sub_block_cursor_matches_full_recompute) checks the new
cursor path against always-full-recompute bit-for-bit across sizes 8/16/32,
every scan, 8 seeds, both sign-hiding states, including trials that drop the
last significant coefficient, empty a whole sub-block, and cross the
greater1/greater2 escape boundary. Output verified bit-identical before/after
by FNV-1a hash of 1920x1080 and 416x240 natural-content encodes
(rdoq_cache_hash.rs). perf stat on the 1080p hash encode: -2.45%
instructions, -1.29% cycles — real but modest, since most of encode_residual's
cost is the CABAC bin coding itself, not the header/context bookkeeping this
round targeted.
perf(h265): hoist rdoq's per-trial csbf/last_full rescan into incremental stateAhmet Kaan Gümüş · 3a8dac3
perf(h265): hoist rdoq's per-trial csbf/last_full rescan into incremental state

encode_residual rescanned the whole block for its coded-sub-block flags and
last significant position on every rate-distortion trial, even though a
trial only ever changes one coefficient. rdoq() now maintains that header
state (a per-sub-block nonzero count plus a lookahead into the still-
untouched lower scan positions for the one case where the last position
itself moves) and feeds it straight to a new encode_residual_with, instead
of re-deriving it from levels each call. Bit-identical output verified by
hashing two natural-content streams before/after (1920x1080 and 416x240);
measured -1.1% cycles / -1.4% instructions on a 1080p bench frame under
perf stat — real but modest, since encode_residual's own CABAC coding
(not this header scan) is the dominant cost per r1's structure notes.
Merge branch 'lane-h265perf2': closed-form bit proxy for h265 mode rankingAhmet Kaan Gümüş · 555bbde
Merge branch 'lane-h265perf2': closed-form bit proxy for h265 mode ranking

estimate_residual_bits replaces per-candidate encode_residual in
choose_luma_mode; BD-PSNR within 0.05dB on both ladders (winner still
gets real RDOQ). Real remaining lever is RDOQ's own inner trial loop.
feat(h265): cheap coefficient-statistics proxy for mode-ranking bitsAhmet Kaan Gümüş · c8c466c
feat(h265): cheap coefficient-statistics proxy for mode-ranking bits

choose_luma_mode's per-candidate cost used the real CABAC-costed
encode_residual to rank ~6 intra mode survivors; replace it with a
closed-form proxy (last-position binarization + a log2 magnitude
estimate per level, no context updates) so ranking no longer pays for
a full residual_coding() walk per candidate. The winner still gets the
real encode_residual/RDOQ below. Magnitude scale swept against
bd_psnr_vs_x265 on the synthetic clip and a real screen capture;
1.5 lands within 0.05 dB of the real-bits baseline on both.
feat(h265): begin a software HEVC decoder — CABAC decode + CTU quadtreeAhmet Kaan Gümüş · d55730b
feat(h265): begin a software HEVC decoder — CABAC decode + CTU quadtree

Module map (crates/ec-h265):
- cabac.rs: CabacDecoder, the arithmetic-decode mirror of CabacEncoder,
  sharing RANGE_TAB_LPS/TRANS_IDX_LPS/TRANS_IDX_MPS/Contexts. Promoted from
  a test-only RefDecoder that already existed and matched the spec
  flowcharts; added decode_bin/decode_bypass/decode_terminate and a WPP
  byte_position_aligned() helper.
- residual.rs: decode_residual, a bin-for-bin mirror of encode_residual.
  Round-trip tested against encode_residual across every block
  size/scan/plane/sign-hiding combination.
- decode.rs (new): the coding-tree/transform-tree walk ec-h265 had no
  decode-shaped twin for. Reuses crate::intra and crate::transform as-is.
  decode_idr_au() parses VPS/SPS/PPS + one I slice and refuses inter
  slices with a stated reason.

Gates: decoder_inverts_the_encoder_bin_by_bin and
decode_residual_inverts_encode_residual green; tests/decode.rs round-trip
green at 32x32 and 96x64 (WPP), 70x50 checkpoint-ignored (same coded dims
as the passing 96x64 case — content-triggered desync, documented for the
next round). Pre-existing cargo test -p ec-h265 --release: 15/15 green.

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.