Skip to content

obs-ffmpeg: Fall back to an alpha format the encoder opens - #311

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/13785-vp9-alpha
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/13785-vp9-alpha

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

A custom FFmpeg recording to WebM/VP9 with the BGRA (alpha) video format now starts and keeps its alpha, instead of failing with "Failed to open video codec: Invalid argument".

For a BGRA output create_video_stream() picks the encoder's least-lossy listed format with avcodec_find_best_pix_fmt_of_list(..., has_alpha=1). FFmpeg 8 and later list gbrap for libvpx-vp9, so gbrap is chosen, but libvpx-vp9 only opens it with strict experimental compliance ("not widely supported ... use 'yuva420p'"), so avcodec_open2 returns EINVAL. The ffmpeg CLI fails the same way for BGRA input to libvpx-vp9; it shows up in OBS as a regression with the newer bundled FFmpeg.

  1. test: Record WebM/VP9 from BGRA through ffmpeg_output: test_ffmpeg_output_alpha builds the real obs-ffmpeg-output.c into the test (without loading the module or its hardware probes), starts core video at 128x128 BGRA, records about 1 s through ffmpeg_output and reads the file back with libavformat:

    • WebM with libvpx-vp9 + libopus: must start, be VP9, contain video packets and carry alpha_mode=1 — fails today with "Failed to open video codec: Invalid argument";
    • guard: FFV1 in Matroska still records bgra;
    • guard: libvpx (VP8) with its default auto-alt-ref still fails rather than dropping alpha, and records with alpha with auto-alt-ref=0.

    1 of 3 fails. Each case skips if FFmpeg lacks the encoder.

  2. obs-ffmpeg: Fall back to an alpha format the encoder opens: when the chosen format has alpha and opening fails with EINVAL, that format is dropped from the candidates and the next-best one is used if it still has alpha, with a fresh context. libvpx-vp9 goes gbrap → … → yuva420p; one INFO line names the rejected and the chosen format, each step is logged at DEBUG. Encoders that open their first choice, non-alpha formats, other errors and rejected non-alpha formats are unchanged; with no alpha format left it fails with the same message as before, so alpha is never dropped silently. Context setup moved into create_video_context(), open_video_codec() now returns the avcodec_open2 error, new next_alpha_format() helper. All 3 pass.

A plain "retry with any next-best format" was rejected: VP8 with its default auto-alt-ref rejects yuva420p, so a blind fallback would quietly record yuv420p without alpha, and hardware encoders would be reopened once per format.

Motivation and Context

Fixes #310 (upstream obsproject/obs-studio#13785, reported by PickKali; also nilankadesilva)

Why this one: WebM/VP9 with alpha is the standard way to record transparent overlays and alerts from OBS, and it stopped working outright with the newer FFmpeg.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), rebased on current master, with FFmpeg n9.0.1 and libvpx 1.17:

  • test_ffmpeg_output_alpha: 1 of 3 fails at the RED commit with the reported error, 3 of 3 pass at GREEN.
  • Full ctest at GREEN: 99 of 99 in each lane under a private Xvfb; without a display the new test's cases skip, like the other core tests.
  • clang-format, gersemi and ci/test_registration.py are clean; the test is registered for Linux only, with its working directory inside that guard.

Not tested: other FFmpeg/libvpx versions, hardware encoders, the OBS UI and the reporter's Windows build, and decoding the alpha back (the test checks the WebM alpha_mode tag, not decoded alpha pixels).

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 and others added 2 commits October 11, 2026 23:56
Builds the real ffmpeg_output (obs-ffmpeg-output.c) into a cmocka test,
records about one second from a 128x128 BGRA, sRGB, full range core video
mix through it, and reads the file back with libavformat.

On master with FFmpeg n9.0.1 and libvpx 1.17, WebM with libvpx-vp9 does not
start: the output picks gbrap for BGRA, libvpx-vp9 refuses it ("Pixel
format 'gbrap' is not widely supported. Use -strict experimental ..."), and
the output stops with "Failed to open video codec: Invalid argument" (stop
code -2, connect failed), the error in the report. 1 of 3 cases fails.

The guards pass on master and must keep passing: Matroska/FFV1 still
records bgra, and VP8 with alpha still fails with libvpx's default
auto-alt-ref (instead of recording without alpha) and records alpha with
auto-alt-ref=0. The cases skip when core video cannot start.

Reported upstream in obsproject/obs-studio#13785 by PickKali.

Refs obs-rust#310.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The Custom FFmpeg Output picks the encoder pixel format closest to the
output format from the formats the encoder lists. For BGRA it keeps alpha,
and with FFmpeg 8 or later libvpx-vp9 lists gbrap, which wins. libvpx-vp9
refuses gbrap unless strict experimental compliance is on ("not widely
supported ... use 'yuva420p'"), so WebM/VP9 recordings with alpha failed
with "Failed to open video codec: Invalid argument".

When the encoder rejects the chosen alpha format with EINVAL, drop it from
the candidates and open a fresh context with the next-best format, as long
as that one still has alpha. libvpx-vp9 now records yuva420p, as it did
before FFmpeg listed gbrap, and the log says which format was rejected.

Encoders that open the first choice are unchanged. A failure with a format
without alpha, any other error, or no alpha format left still fails as
before with the same message, so alpha is never dropped silently (VP8 with
its default auto-alt-ref still asks for auto-alt-ref=0).

The new test passes in both lanes: VP9 records a WebM with alpha_mode 1,
FFV1 in Matroska still records bgra, and the VP8 guard is unchanged.

Fixes obs-rust#310.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 04:02
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

I couldn't reproduce the RED here. In the Tier 2 container (Ubuntu's FFmpeg 8.0.1-3ubuntu2 and its libvpx), test_ffmpeg_output_alpha passes 3 of 3 at the RED commit 7dd962c4f as well as at GREEN, so libvpx-vp9 there either doesn't list gbrap first or opens it. CI uses the same kind of distro FFmpeg, so it can't show the failure either. Your RED evidence is from FFmpeg n9.0.1 and libvpx 1.17 only.

The fix itself looks right and conservative: it only acts after an alpha format fails with EINVAL, never drops alpha silently, and the VP8 guard is a good call. Before merging I'd like a RED that doesn't depend on the host's FFmpeg version. One option: unit-test next_alpha_format() and the retry loop with a fixed candidate list (gbrap, yuva420p, yuv420p) and a stub open that returns EINVAL for gbrap, so the selection logic fails on master and passes with the fix everywhere. The end-to-end WebM test can stay as an extra check that skips when it can't reproduce.

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: obs-ffmpeg: WebM/VP9 recording with BGRA fails to start (upstream #13785)

2 participants