Repository navigation
obs-ffmpeg: Fall back to an alpha format the encoder opens - #311
Conversation
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]>
|
I couldn't reproduce the RED here. In the Tier 2 container (Ubuntu's FFmpeg 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 |
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 withavcodec_find_best_pix_fmt_of_list(..., has_alpha=1). FFmpeg 8 and later listgbrapfor libvpx-vp9, sogbrapis chosen, but libvpx-vp9 only opens it with strict experimental compliance ("not widely supported ... use 'yuva420p'"), soavcodec_open2returns EINVAL. TheffmpegCLI fails the same way for BGRA input to libvpx-vp9; it shows up in OBS as a regression with the newer bundled FFmpeg.test: Record WebM/VP9 from BGRA through ffmpeg_output:test_ffmpeg_output_alphabuilds the realobs-ffmpeg-output.cinto the test (without loading the module or its hardware probes), starts core video at 128x128 BGRA, records about 1 s throughffmpeg_outputand reads the file back with libavformat:alpha_mode=1— fails today with "Failed to open video codec: Invalid argument";bgra;auto-alt-ref=0.1 of 3 fails. Each case skips if FFmpeg lacks the encoder.
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 goesgbrap→ … →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 intocreate_video_context(),open_video_codec()now returns theavcodec_open2error, newnext_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 recordyuv420pwithout 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_LIBOBSOFF 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.ci/test_registration.pyare 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_modetag, not decoded alpha pixels).Types of changes
Checklist:
🤖 Generated with Claude Code