Skip to content

Demote scheduled B-frames without an L1 reference to P#2

Open
mprammer wants to merge 1 commit into
masterfrom
mp/fix-crf-bframe-l1-ratecontrol
Open

Demote scheduled B-frames without an L1 reference to P#2
mprammer wants to merge 1 commit into
masterfrom
mp/fix-crf-bframe-l1-ratecontrol

Conversation

@mprammer

Copy link
Copy Markdown

A Spiral exact-cadence schedule is computed per full keyint GOP and applied modulo keyint, so in a partial final GOP a scheduled B-frame can reference a forward anchor whose display position is past end-of-stream. That anchor is never submitted, never decoded, and never enters the DPB, so the B reaches CRF ratecontrol with an empty L1 list — and rate_estimate_qscale() unconditionally dereferences fref_nearest[1] / fref[1][i_ref[1]-1] for B slices, SIGSEGV'ing on flush. (Fixed-QP frames skip rate_estimate_qscale, which is why only CRF transcodes crashed.) This demotes a scheduled B/B-ref with no schedule-allowed future reference in the DPB to P before the slice type is derived, mirroring x264's normal end-of-stream handling; its past/L0 anchor is always present, so P ratecontrol is well-defined.

Verified on x86 Linux: an exact-cadence CRF encode of a clip whose length isn't a multiple of keyint SIGSEGV'd before and exits cleanly after, and removing just the demote call reproduces the crash.

🤖 Generated with Claude Code

A Spiral exact-cadence schedule is computed per full keyint GOP and applied
via frame_index % keyint, with no knowledge of stream length. In a partial
final GOP a scheduled B frame can reference a forward anchor whose display
position lies past end-of-stream; that anchor is never submitted, never
decoded, and never enters the DPB. The B then reaches ratecontrol with an
empty L1 list, and rate_estimate_qscale() unconditionally dereferences
h->fref_nearest[1] / h->fref[1][h->i_ref[1]-1] for B slices -> SIGSEGV on
flush. CRF only; fixed-QP frames skip rate_estimate_qscale, which is why the
--x264-layer-qps / frame-IR path was unaffected.

Mirror x264's normal end-of-stream behaviour: before deriving the slice type,
demote a scheduled B/B-ref with no schedule-allowed future reference in the
DPB to P. Its past/L0 anchor is always present, so P ratecontrol is defined.

Verified on x86 Linux: the exact-cadence CRF encode of a clip with a partial
final GOP SIGSEGV'd before and exits cleanly after; removing just the demote
call reproduces the crash (A/B).

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: mprammer <martin@spiraldb.com>
@mprammer mprammer marked this pull request as ready for review June 15, 2026 18:09
@mprammer mprammer requested a review from gatesn June 15, 2026 18:09
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.

1 participant