Skip to content

libobs, obs-ffmpeg: Handle an empty packet in the keyframe checks - #218

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/216-vaapi-empty-packet
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/216-vaapi-empty-packet

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

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:690 and :698) moves the parameter sets into the header with obs_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 calls obs_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 to ff_avc_find_startcode_internal(), which computes end -= 3 from a NULL end (obs-nal.c:27) and reads address 0. That matches the report's backtrace (p=0x0, end=0xfffffffffffffffd).

  1. test/cmocka: Cover keyframe checks on an empty packet: test_avc_keyframe_empty builds a header-only H.264 packet (SPS + PPS), checks that obs_extract_avc_headers() leaves an empty packet, and calls obs_avc_keyframe() on it and on (NULL, 0); test_hevc_keyframe_empty calls obs_hevc_keyframe(NULL, 0). Both crash with signal 11 on this commit; the other test_avc_parse and test_hevc cases, and test_nal / test_avc, pass.
  2. libobs, obs-ffmpeg: Handle an empty packet in the keyframe checks: obs_nal_find_startcode() returns end for 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_parse and test_hevc fail at the RED commit (test_avc_keyframe_empty and test_hevc_keyframe_empty crash with signal 11) and pass at the GREEN commit, after clean rebuilds. test_nal and test_avc pass 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_internal with the same stack as the report. After the fix, recording continues, and ffmpeg_vaapi, ffmpeg_vaapi_tex, hevc_ffmpeg_vaapi and hevc_ffmpeg_vaapi_tex all record.

Not tested:

  • The reporter's own trigger: their driver produces a header-only first packet naturally, and I couldn't get one without forcing it.
  • AV1 VAAPI (its keyframe check doesn't use the start-code search), and Intel VAAPI.
  • The Rust and Docker lanes of the local gate.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

@rdoupe
rdoupe requested a review from zackees as a code owner October 11, 2026 07:48
@zackees
zackees force-pushed the fix/216-vaapi-empty-packet branch from ba515bb to 169f308 Compare October 11, 2026 08:00
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped.

  • RED (5c5d297b0): Tier 2 OFF passed 68 of 70. test_avc_keyframe_empty and test_hevc_keyframe_empty failed.
  • GREEN (ba515bb6d): Tier 2 passed 70 of 70 with ENABLE_RUST_LIBOBS OFF and 70 of 70 with it ON. With ON, obs-nal.c is replaced by the Rust obs-codec port, and that port already treats an empty packet as not a keyframe, so the C fix brings the C in line with it.
  • Local gate: lint, rust-test and tier2-linux all passed.

Merging once CI is green.

@rdoupe

rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator Author

FYI the macOS failures here (and on #219) aren't from this change: master e226c3d25 fails the same three macOS jobs. Since the obs-studio-rust-v0.1.0-beta.20261011 tag, git describe --always --tags returns obs-studio-rust-v0.1.0-beta.20261011-7-ge226c3d25, and cmake/common/versionconfig.cmake:24's REGEX REPLACE leaves the obs-studio-rust-v prefix in place, so project(... VERSION ...) gets obs-studio-rust-v0;1;0 and macOS configure stops with VERSION "obs-studio-rust-v0.1.0" format invalid.

Two possible fixes, your call which version the build should carry:

  • git describe --always --tags --match "[0-9]*" keeps libobs versioned from the upstream OBS tags (33.0.0-beta6 today), so plugin/API version checks are unchanged.
  • or take the version from the obs-rust tag with an anchored match (e.g. string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" ...)), which would make libobs report 0.1.0.

Happy to send a PR for whichever you prefer.

rdoupe and others added 2 commits October 11, 2026 01:28
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
zackees force-pushed the fix/216-vaapi-empty-packet branch from 169f308 to 9ab9779 Compare October 11, 2026 08:30
@zackees
zackees merged commit 1699a3a into obs-rust:master Oct 11, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libobs/obs-ffmpeg: VAAPI crashes when the first encoded packet holds only headers (upstream #13316)

2 participants