forge
Explore
Log in

Don't you dare go Hollow

Commits

Commits on 68cf7a69b80f16fa32ec222cdfd758fa2faff4b7Filtered to crates/ec-h264/src/inter.rsClear filter

2026-08-14

perf(h264): close the decode gap on inter contentAhmet Kaan Gümüş · 4cab1f0
perf(h264): close the decode gap on inter content

Instruction counts, single thread, measured with perf over whole streams:
1080p WEB-DL 100 frames 38.23G -> 30.02G (-21.5%), QCIF B streams
-47% and -29%. The changes, each measured on its own and kept only
where it paid:

- chroma interpolation takes a const-width kernel plus a full-sample
  copy path, so the 2x2 blocks that dominate real motion stop paying
  for eight lanes and four multiplies they discard;
- single-list, default-weight prediction interpolates straight into
  the picture at the picture's pitch, and declares no scratch at all;
- boundary strength is decided once per macroblock for all 24 internal
  edges (intra gives 3, no coefficients plus one motion vector gives 0)
  instead of 24 times from the block tables;
- the luma half-sample filter splits by partition width, because a
  16-lane vector is two SSE2 registers and half of it is thrown away
  for an 8- or 4-wide partition;
- a horizontal chroma edge deblocks as one 8-lane pass;
- the rounded average is written as the plain byte idiom the compiler
  turns into pavgb;
- an output frame is grown row by row rather than allocated zeroed;
- a direct-mode quadrant whose four 4x4 blocks derive the same motion
  compensates once as an 8x8 (interpolation is position-invariant, so
  this is bit-exact).

Reverted, measured worse: combining at the picture pitch (+6 to +9%),
blanket 8-lane luma taps (+2.2%), window slices for the deblock line
filters (+0.8%).

Conformance unchanged: 22 conformance tests and 55 unit tests green,
JVT full sequences and the real library sweep still bit-exact.
feat(h264): software encoder, I and P pictures, CAVLC, slice-parallelAhmet Kaan Gümüş · 6d22228
feat(h264): software encoder, I and P pictures, CAVLC, slice-parallel

The encoder shares the decoder's reconstruction rather than owning one:
prediction (pred, inter), dequantisation and the inverse transforms
(transform), the deblocking filter and the per-macroblock metadata are the
decoder's own code and the decoder's own Picture. What is new here is the
forward direction — forward transforms and quantisation (quant), CAVLC
writing derived at compile time from the decoder's tables (vlc), parameter
sets and slice headers (headers), mode decision and macroblock coding (mb),
and rate control (rc).

Scope: Baseline-profile syntax (8-bit 4:2:0 progressive, CAVLC, one
reference, pic_order_cnt_type 2), intra 16x16 and 4x4 with chroma modes,
P_Skip and P_L0_16x16 with a diamond search to quarter sample, and CBR
rate control with a per-macroblock-row correction. Coding a picture as one
slice per worker gives the parallelism inside the encoder: a slice is
independent by definition, so every worker owns its picture buffer and its
bitstream, and the bands are stitched and filtered once at the end.

B pictures and CABAC are not coded yet; the bframes field is accepted and
coded as zero rather than refused.
perf(h264): vectorise the motion compensation kernelsAhmet Kaan Gümüş · d69235f
perf(h264): vectorise the motion compensation kernels

Motion compensation was 54% of a 1080p decode profile: the 6-tap luma
interpolation of 8.4.2.2.1, the chroma bilinear of 8.4.2.2.2 and the
prediction average of 8.4.2.3.1, all per sample in 32-bit arithmetic.

The unrounded 6-tap result spans -2550..10710 and the chroma bilinear
never exceeds 64 * 255, so both fit 16-bit lanes exactly: one vector is
one row of a partition (sixteen luma lanes, eight chroma). Only the
centre sample j needs 32-bit lanes, because the second pass sums
already-filtered values. Every load is fixed length so the compiler
emits one widening load rather than a byte loop — the open-ended slice
version measured barely faster than scalar.

Bit-identical by construction and by the from-spec test, which walks
every fractional position and partition size at interior and far
outside positions. 1080p library streams: 44.2 to 35.3 Ginstr and 64.2
to 45.5 Ginstr, 18% and 40% less CPU time.
feat(h264): display-order output reaches the packet entry surfaceAhmet Kaan Gümüş · 5a58bb1
feat(h264): display-order output reaches the packet entry surface

A player never touches the NAL-level API, so the reordering has to arrive
at H264Decoder: packets in, frames out, timestamps travelling with their
pictures rather than with the packets that carried them.

packet_entry_surface_reorders_and_carries_timestamps drives exactly that
path — one access unit per packet, frames pulled with receive_frame —
and proves three things at once: every frame is bit-exact against
ffmpeg, the output order is a real permutation of the packet order, and
the tag on each emitted frame identifies the packet whose picture it
actually is (checked against the same stream decoded in decode order).

Also: the crate description now states the scope it reached, the debug
tracing added while chasing the inter defects is gone, and clippy and
rustfmt are clean.
feat(h264): inter prediction, decoded picture buffer, display-order outputAhmet Kaan Gümüş · cd996f4
feat(h264): inter prediction, decoded picture buffer, display-order output

P and B slices under both entropy coders, with the clause 8.2 decoded
picture buffer behind them.

- inter.rs: quarter-sample luma interpolation (8.4.2.2.1), eighth-sample
  chroma (8.4.2.2.2) and the weighted combination of 8.4.2.3. Plane
  borders are replicated once per picture, which turns the per-sample
  Clip3 of Equations 8-239/8-240 into a single clamp of the block origin;
  the unit test checks every fractional position, inside the picture and
  far outside it, against the spec's clipped definition written out
  separately.
- mv.rs: macroblock and sub-macroblock partitioning (Tables 7-13, 7-14,
  7-17, 7-18), neighbouring motion data (8.4.1.3.2) and motion vector
  prediction (8.4.1.3) including the directional segmentation shortcuts.
- dpb.rs: picture order count types 0, 1 and 2, sliding-window and
  adaptive (MMCO 1..6, long-term references included) marking, reference
  list initialisation and modification, frame_num gap handling per
  8.2.5.2, and the output bumping of C.4.5.3.
- decoder.rs: P/B macroblock decode, P_Skip, B_Skip and both direct modes
  (spatial and temporal), constrained intra prediction, the inter scaling
  lists, and boundary strength derivation per 8.7.2.1 so deblocking now
  varies across an edge.
- Output is display order by default; OutputOrder::Decode is the escape
  hatch for a caller that reorders itself. Timestamps travel with the
  picture, not the packet.

The tC0 table rows for bS 1 and 2 were transcribed but unexercised by an
intra-only decoder, and both were wrong: whole-row shifts. Re-derived
from Table 8-17; inter edges reach them now.

CABAC initialisation gains the cabac_init_idc columns of Tables 9-13 to
9-21 (ctxIdx 11..59 and the idc columns of 70..275, 399..401), extracted
from the spec text by column alignment.