Skip to content

obs-ffmpeg: Make a keyframe when an output joins a running encoder - #598

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/keyframe-request-more-encoders
Oct 13, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/keyframe-request-more-encoders

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Description

Follow-up to #594: a recording started while the replay buffer runs now also starts right away with the VA-API H.264 and HEVC encoders and with AOM AV1 and SVT-AV1, not only with x264. Before, at 20 fps with VA-API's default keyframe interval, a 6 s recording held 3 s, and one stopped before the encoder's next keyframe took over 3 s to stop and left an unreadable file.

#594 added the optional request_keyframe to obs_encoder_info; libobs calls it when an output starts on a video encoder that already runs, because the new output gets no packet before a keyframe. This implements it in obs-ffmpeg: the next frame goes to FFmpeg with pict_type = AV_PICTURE_TYPE_I, which FFmpeg's VA-API encoders turn into an IDR frame, libaom into a forced keyframe (AOM_EFLAG_FORCE_KF) and SVT-AV1 into a key picture. The flag is set atomically by the request and taken by the next encode, as in obs-x264. No API change.

  1. test/cmocka: Add failing join cases for the encoders OBS ships on Linux: test_encoder_join_keyframe gets cases that load the built obs-x264 and obs-ffmpeg modules and run the join with the real encoders, each set to a 10 s keyframe interval: x264, VA-API H.264, VA-API HEVC, AOM AV1, SVT-AV1. The joining output's first packet must be a keyframe made from the frame the encoder got when the output joined (within 3 frames; a scheduled keyframe is 300 frames away). Each case skips when its encoder is not available (no VA-API device, FFmpeg without the library), so CI runs what its FFmpeg has. Core video grows to 320x240 for the hardware encoders' minimum size.
  2. obs-ffmpeg: Make a keyframe when an output joins a running encoder: the fix.

Encoders covered and not covered:

Encoder Here Why
obs-x264 already (#594) now also tested with the real encoder
FFmpeg VA-API H.264, HEVC (ffmpeg_vaapi, hevc_ffmpeg_vaapi) yes tested on an AMD GPU (Mesa VA-API)
FFmpeg VA-API H.264, HEVC texture variants (*_tex) yes same encode function (vaapi_encode_internal); headless, they fall back to the copy encoders, so the GPU-texture path itself was not exercised
AOM AV1, SVT-AV1 yes tested
FFmpeg VA-API AV1 no the test GPU cannot encode AV1, so it could not be run; same code, one line once someone can test it
OpenH264 no the FFmpeg here is built without libopenh264, so it could not be run; uses the same ffmpeg_video_encode, one line once tested
NVENC (obs-nvenc and FFmpeg NVENC), QSV, AMF no no such hardware to test on; each needs its own encoder-specific forced-IDR call

Motivation and Context

Fixes #597 (follow-up to #594; upstream obsproject/obs-studio#12069).

Reproduced headless, private config, over obs-websocket: Advanced output (the replay buffer shares the recording encoder), mkv, 20 fps, replay buffer 20 s, a Media Source looping a clip. Rounds: start the replay buffer, record from 3 s later for 6 s; and record from 1 s later for 2 s; twice each.

  • Current release (AppImage 33.0.0-beta6-799), SVT-AV1: 6 s recordings hold 2.97 s; the 2 s recordings take 3.17 s to stop and are 674-byte unreadable files. (VA-API does not load inside the AppImage on the test machine, so VA-API was reproduced with a master build.)
  • Current master, VA-API H.264: 6 s recordings hold 3.0 s; the 2 s recordings take 3.18 s to stop and are 735-byte unreadable files. SVT-AV1: 2.7 s; 3.4 s stops, unreadable.
  • This branch, VA-API H.264, VA-API HEVC, SVT-AV1 and AOM AV1, two rounds each: every 6 s recording holds 6.06-6.08 s (121 video packets), every 2 s recording 2.05-2.07 s (41 video packets), each stops in 0.15-0.20 s and decodes from its first frame without errors.

Why this one: our own half-fix: #594 fixed the reported data loss only for x264, and the same scenario still loses data with the hardware and AV1 encoders Linux users pick.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON):

  • test_encoder_join_keyframe: 4 of 8 cases fail at the RED commit in each lane (VA-API H.264, VA-API HEVC, AOM AV1, SVT-AV1: no packet within 3 s); 8 of 8 pass at GREEN, 8 runs in each lane, the joining output's first packet a keyframe after 23-64 ms, within one frame of the join (x264: 0.7 s, its lookahead). All cases ran (no skips) under Xvfb, and also with the system graphics module masked as in CI; without a display every case skips, and with VA-API unavailable only the two VA-API cases skip.
  • Full ctest at GREEN, rebased on current master: 176 of 176 in each lane, every PipeWire and PulseAudio test actually run (TMPDIR unset; only the known open_crashes_without_obs_core skips).
  • clang-format and gersemi are clean; no new compiler warnings.

By hand: the end-to-end runs above with a frontend build of this branch (Rust libobs, obs-websocket). Not tested on Windows or macOS (the changed encoders are Linux-only except AOM/SVT AV1, whose change is the same FFmpeg call).

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

test_encoder_join_keyframe checked the request_keyframe path with a stand-in
encoder only. Add cases that run the same scenario, a replay buffer running
on a video encoder with a 10 s keyframe interval and a recording that joins
it, with the real encoders, loaded from the built obs-x264 and obs-ffmpeg
modules: x264, VA-API H.264 and HEVC, AOM AV1 and SVT-AV1. The joining
output's first packet must be a keyframe made from the frame the encoder got
when it joined, give or take a few frames.

x264 passes; it implements request_keyframe. The four obs-ffmpeg encoders
do not, and their joining output gets nothing within 3 s: it waits for the
next scheduled keyframe. Each case skips when its encoder is not available
(no VA-API device, FFmpeg built without the library). The core video size
grows to 320x240 for the hardware encoders' minimum size.

Refs obs-rust#597.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 13, 2026 16:18
A recording started while the replay buffer runs joins the video encoder
the replay buffer already runs, and gets no packet before a keyframe. libobs
asks the encoder for one through request_keyframe, but only obs-x264
implemented it: with the VA-API, AOM AV1 and SVT-AV1 encoders the recording
still waited for the next scheduled keyframe (VA-API's default interval is
120 frames, 6 s at 20 fps), so it missed its first seconds, and one stopped
before that keyframe came out empty after a slow stop.

Implement request_keyframe for the VA-API H.264 and HEVC encoders and for
AOM AV1 and SVT-AV1: the next frame goes to FFmpeg with pict_type
AV_PICTURE_TYPE_I, which FFmpeg's VA-API encoders turn into an IDR frame,
libaom into a forced keyframe and SVT-AV1 into a key picture. The joining
output's first packet is now a keyframe made from the frame the encoder got
when it joined, within a frame either way.

Fixes obs-rust#597.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa secs=457 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run
Ci-Attestation: {"at":1791908926,"gate":"rust/all/fmt","host":"linux-x86_64","key":"af20fb1baf63482c50ae11f2c5dd4a971494d9e9b8bcc875d0dfde2f7bb06483","lane":"lint","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":36,"stamp":"86ebec723f1221d2c596efa087386d09","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"af20fb1baf63482c50ae11f2c5dd4a971494d9e9b8bcc875d0dfde2f7bb06483","lane":"lint","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":36,"stamp":"cdf79df460b44bcf0ef07b9b6bf4a160","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"b5ffc7156f5cabe6c8c9936bf0eeaf65ee623ac9455faffb7501396544aa7f66","lane":"rust-test","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":118,"stamp":"1a1e4ebc14fec5ecec979279d6a12f57","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"6431330132a19677a0deab32b8e17d5098a4add0d51c70289e589018cdf10199","lane":"cross","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":112,"stamp":"0410f2183f65cc571486558e49dac07e","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"6431330132a19677a0deab32b8e17d5098a4add0d51c70289e589018cdf10199","lane":"cross","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":112,"stamp":"20ce29cbd541e274c2cb3d85354528a7","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"6431330132a19677a0deab32b8e17d5098a4add0d51c70289e589018cdf10199","lane":"cross","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":112,"stamp":"44250b48177e804f97ec76f8ea0b37f7","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"6431330132a19677a0deab32b8e17d5098a4add0d51c70289e589018cdf10199","lane":"cross","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":112,"stamp":"4bc13733b542482410c53535ad8aa0f9","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"rust/x86_64-apple-darwin/clippy","host":"linux-x86_64","key":"6431330132a19677a0deab32b8e17d5098a4add0d51c70289e589018cdf10199","lane":"cross","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":112,"stamp":"4a126c73a3fd2c17c46e6ab2a25b82c1","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"rust/x86_64-apple-darwin/dylint","host":"linux-x86_64","key":"6431330132a19677a0deab32b8e17d5098a4add0d51c70289e589018cdf10199","lane":"cross","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":112,"stamp":"e2e03f783385a1dbaeb861a1151c59ad","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
Ci-Attestation: {"at":1791908926,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"9ba0af34b22c0e234ad29e8bf970298f153cc818333da15a9c05d33ce81f489a","lane":"tier2-linux","parents":["a503d6bd4d832aa17b93d0f144d4004dca8d55d9"],"secs":191,"stamp":"aa67c4c8d78a4db655c6e4f1ed170f9e","tree":"327ccadef8c1c1d5fbd37bd9149ba57169e0a1aa","v":1,"via":"run"}
@zackees

zackees commented Oct 13, 2026

Copy link
Copy Markdown
Member

Verified locally under xvfb-run (Tier 2 image + mesa):

  • RED a503d6b: test_encoder_join_keyframe fails real_aom_av1 and real_svt_av1
  • GREEN 00038db: those pass (6 passed). real_vaapi_h264/hevc skip here (no VAAPI device), so the VAAPI change isn't exercised locally.
  • Rebased on master, local gate passed; pushed as 44170a4.

@zackees
zackees force-pushed the fix/keyframe-request-more-encoders branch from 00038db to 44170a4 Compare October 13, 2026 16:28
@zackees
zackees merged commit c553fd9 into obs-rust:master Oct 13, 2026
19 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

2 participants