Repository navigation
libobs, obs-ffmpeg: Handle an empty packet in the keyframe checks - #218
Merged
Merged
Conversation
zackees
force-pushed
the
fix/216-vaapi-empty-packet
branch
from
October 11, 2026 08:00
ba515bb to
169f308
Compare
Member
|
Verified locally, rebased onto master and stamped.
Merging once CI is green. |
Collaborator
Author
|
FYI the macOS failures here (and on #219) aren't from this change: master Two possible fixes, your call which version the build should carry:
Happy to send a PR for whichever you prefer. |
9 tasks done
obs_avc_keyframe and obs_hevc_keyframe crash on an empty packet whose data is NULL. The VA-API encoder passes one when an encoder's first packet holds only parameter sets: obs_extract_avc_headers moves every unit into the header and leaves no packet data. The start-code search then computes end - 3 from a NULL end and reads address 0. Reported upstream as obsproject/obs-studio#13316 by vicho314. Refs obs-rust#216. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
obs_nal_find_startcode now returns end for an empty range before the search computes end - 3, as the Rust port in obs-codec already does, so obs_avc_keyframe and obs_hevc_keyframe return false for (NULL, 0) instead of reading address 0. The VA-API encoder no longer outputs a packet when the first encoded packet holds only parameter sets: they are kept as the header and the packet is skipped, rather than sent on with NULL data. Refs obsproject/obs-studio#13316, reported by vicho314. Fixes obs-rust#216. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Local-Gate: v1 tree=a2933bdf713491cb123d7a77a223dfc081c570d2 secs=129 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791707416,"gate":"rust/all/fmt","host":"linux-x86_64","key":"753adfb004ec6d5317032fe23f5f9a25e22b1c1a178219c3ce4056947d9855ef","lane":"lint","parents":["ee9d21114da7b4107a7c6145bdfdea594a13fe51"],"secs":54,"stamp":"2d9023ca3eead0d69b12bd5463eaadbe","tree":"a2933bdf713491cb123d7a77a223dfc081c570d2","v":1,"via":"run"} Ci-Attestation: {"at":1791707416,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"753adfb004ec6d5317032fe23f5f9a25e22b1c1a178219c3ce4056947d9855ef","lane":"lint","parents":["ee9d21114da7b4107a7c6145bdfdea594a13fe51"],"secs":54,"stamp":"fecbddbbfceeb6bfabcdb119599d0e57","tree":"a2933bdf713491cb123d7a77a223dfc081c570d2","v":1,"via":"run"} Ci-Attestation: {"at":1791707416,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"d82c61414ebf992a4b813c7ecf06b34a00feb7cd77a39b767e1f6b61a17a8d31","lane":"rust-test","parents":["ee9d21114da7b4107a7c6145bdfdea594a13fe51"],"secs":30,"stamp":"07ac077319c19861edc8cb01db086c23","tree":"a2933bdf713491cb123d7a77a223dfc081c570d2","v":1,"via":"run"} Ci-Attestation: {"at":1791707416,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"762ce104c31dfe5b52996591e421813dcb2f9821f5a8a9b066f4d3ac0bbea08d","lane":"tier2-linux","parents":["ee9d21114da7b4107a7c6145bdfdea594a13fe51"],"secs":44,"stamp":"7da405d76263cbc3954a9236c22c748b","tree":"a2933bdf713491cb123d7a77a223dfc081c570d2","v":1,"via":"run"}
zackees
force-pushed
the
fix/216-vaapi-empty-packet
branch
from
October 11, 2026 08:30
169f308 to
9ab9779
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Recording with the FFmpeg VAAPI encoder (H.264 or HEVC) could crash OBS as soon as the recording started, when the encoder's first packet held only parameter sets (SPS/PPS, plus VPS for HEVC) and no slice. With this change an empty packet is no longer treated as a possible keyframe, and the VAAPI encoder does not output a packet when there is nothing left in it after the headers are taken out.
The cause: on the first packet
vaapi_encode_internal()(plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c:690and:698) moves the parameter sets into the header withobs_extract_avc_headers()/obs_extract_hevc_headers(). For a header-only packet nothing is left, so the packet it outputs has NULL data and size 0, and it then callsobs_avc_keyframe(NULL, 0)/obs_hevc_keyframe(NULL, 0)(obs-ffmpeg-vaapi.c:721,:725).obs_nal_find_startcode()(libobs/obs-nal.c:60) passes the empty range toff_avc_find_startcode_internal(), which computesend -= 3from a NULLend(obs-nal.c:27) and reads address 0. That matches the report's backtrace (p=0x0, end=0xfffffffffffffffd).test/cmocka: Cover keyframe checks on an empty packet:test_avc_keyframe_emptybuilds a header-only H.264 packet (SPS + PPS), checks thatobs_extract_avc_headers()leaves an empty packet, and callsobs_avc_keyframe()on it and on(NULL, 0);test_hevc_keyframe_emptycallsobs_hevc_keyframe(NULL, 0). Both crash with signal 11 on this commit; the othertest_avc_parseandtest_hevccases, andtest_nal/test_avc, pass.libobs, obs-ffmpeg: Handle an empty packet in the keyframe checks:obs_nal_find_startcode()returnsendfor an empty range before the search runs, so both keyframe checks return false for(NULL, 0). This is the same behaviour as the Rust port of the start-code search in this repository. The VAAPI encoder also keeps a header-only first packet as the header and skips outputting it, instead of sending on a packet with no data. All tests pass.Motivation and Context
Fixes #216 (upstream obsproject/obs-studio#13316, reported by vicho314)
Why this one: it's a crash at the start of every recording for the people it affects, the reporter's backtrace points straight at the faulty line, and there is no upstream fix. The
obs_nal_find_startcode()guard also protects every other caller that could pass an empty packet.How Has This Been Tested?
Automated:
test_avc_parseandtest_hevcfail at the RED commit (test_avc_keyframe_emptyandtest_hevc_keyframe_emptycrash with signal 11) and pass at the GREEN commit, after clean rebuilds.test_nalandtest_avcpass at both. clang-format is clean.By hand, headless on a Radeon 680M (radeonsi): the hardware there doesn't produce a header-only first packet by itself, so I forced one in the VAAPI encoder. Before the fix, recording segfaulted in
ff_avc_find_startcode_internalwith the same stack as the report. After the fix, recording continues, andffmpeg_vaapi,ffmpeg_vaapi_tex,hevc_ffmpeg_vaapiandhevc_ffmpeg_vaapi_texall record.Not tested:
Types of changes
Checklist:
🤖 Generated with Claude Code