Skip to content

fix(hevc): derive inter-predicted short-term reference picture sets - #705

Merged
tobbee merged 3 commits into
Eyevinn:masterfrom
perfloop-oss:perfloop-pr-open-yqdd5wtxyv
Nov 12, 2026
Merged

tobbee merged 3 commits into
Eyevinn:masterfrom
perfloop-oss:perfloop-pr-open-yqdd5wtxyv

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

Derive inter-predicted HEVC short-term reference-picture lists and used flags instead of keeping just their count. On master, conformance slice headers exercised by hevc/rps_test.go fail to parse or return wrong reference picture sets and reference-list entries. The corrected picture state propagates through SPS prediction chains and slice-local prediction, allowing reference-list modifications and subsequent header fields to consume the right bits.

Reach: streams that predict a short-term reference picture set from another set, as HM-style encoders do (x265 does not). The reference-list-modification header-size fix, and so its cbcs protection-range correction, also requires PPS lists_modification_present_flag.

Derivation shares backing arrays across predicted sets within an SPS, with capped sub-slices; a slice-local prediction uses its own pair. A new pair is allocated if later sets outgrow the reserve. Fixed per-set work (copying the reference set, clearing scratch arrays and the selection passes) also runs for empty predicted sets, without extra allocations. Slice parsing additionally reads reference-list modifications enabled by the corrected picture count.

Paired parsing-time medians:

Input Before (ns/op) After (ns/op) Pairs
One conformance SPS with 9 predicted sets (LTRPSPS_A) 1675.5 2232.5 16
One slice header with slice-local prediction (LTRPSPS_A) 306.15 459 16
SPS with an empty predicted set 649.65 709.1 16
Slice header with an empty slice-local predicted set 225.9 278.3 16

SPS allocations rise 9→11 (1808→2000 B/op); slice allocations rise 2→6 (400→480 B/op): two allocations for the derived set, and two for the reference-list modification entries that master skipped. Correct picture lists and header boundaries outweigh these costs. Empty-SPS timing is inconclusive, with paired improvement bounds of −192.4 to 54.8 ns/op.

Slice parsing now rejects a hand-built SPS with inconsistent reference sets.

Regression coverage and validation

The two tests in hevc/rps_test.go check conformance header excerpts and prediction chains, both shift signs, retained-but-unused pictures, zero cancellation and empty selection. They check picture state, reference-list modifications, QP deltas and aligned header sizes; fixtures omit slice payloads. No benchmark is added to the contribution.

The public unsigned-distance representation and intra-coded RPS parsing are unchanged. Unrepresentable derived distances return an error.

Go tests for HEVC SPS/slice parsing and HEVC cbcs protection ranges pass. make build, make test and make check pass.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

@tobbee

tobbee commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

@perfloop-agent Why is this an issue that is worth fixing? This is not a proper HEVC parser. What is your use case?

@tsenart

tsenart commented Nov 11, 2026

Copy link
Copy Markdown

A real producer: Intel Quick Sync (hevc_qsv/oneVPL) writes inter-RPS prediction in the SPS and in slice headers and sets lists_modification_present_flag=1. MediaMTX hit the missing derivation with QSV streams (bluenviron/mediamtx#3285, fixed in bluenviron/mediacommon#128). On those QSV headers, master derives empty used-picture lists and skips the list-modification syntax; with this PR both are correct. In the QSV samples we checked, header size and cbcs clear ranges did not change, so we have not shown a cbcs failure. If correct RPS state is out of scope for mp4ff, please close the PR.

@tobbee

tobbee commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

@tsenart Thanks for the context about where such streams appear. I think it is worth adding this.

@tobbee

tobbee commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

@tsenart Thanks for this fix, and for the context about the Quick Sync streams.

I checked the derivation against the JCT-VC conformance streams and FFmpeg's trace_headers:

  • Slice header sizes and slice_qp_delta agree with FFmpeg on all 182 streams it can trace. LTRPSPS_A, RPLM_A and RPLM_B were wrong before this PR, and no other stream changes.
  • The derived pictures and used flags match a separate implementation of equations 7-61 and 7-62 on 185 streams, 139 of which use inter-predicted sets. That implementation took its input from FFmpeg's traced syntax elements.
  • All loops are bounded by the 32-entry limit, and distances that don't fit in a uint32 are rejected. The shared storage allocates at most about 10 KB per SPS. A fuzz run of SPS, PPS and slice-header parsing, seeded from the conformance streams, found no problems.

The first commit adds tests that fail until the fix commit, so I'm squashing the PR into a single commit on master.

@tobbee
tobbee merged commit d98f0d6 into Eyevinn:master Nov 12, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants