Skip to content

Add VA-API hardware decoding (--vaapi, --vaapi-device) - #9

Open
bernhardberger wants to merge 1 commit into
fancybits:fancybitsfrom
bernhardberger:vaapi
Open

bernhardberger wants to merge 1 commit into
fancybits:fancybitsfrom
bernhardberger:vaapi

Conversation

@bernhardberger

Copy link
Copy Markdown

Adds --vaapi and --vaapi-device=<path> (implies --vaapi) for Intel and AMD GPUs on Linux. The device is attached to the native decoder rather than selecting a separate decoder, so libavcodec's default get_format uses the VAAPI hwaccel when the GPU supports the codec and profile and decodes in software when it does not (MPEG-2 on recent AMD GPUs, for example). Surfaces are downloaded with their frame properties, so pts, picture type, key frame flag and A53 captions pass through unchanged; 10-bit P010 goes through the existing 8-bit conversion.

The device is not attached when --qsv/--cuvid/--dxva2/--vdpau already chose a decoder (cuvid would read the VAAPI context as CUDA), nor with lowres. An unusable device is logged and decoding falls back to software; 25 consecutive frame download failures abort rather than drop frames. With -v 1 the log says whether VA-API is active for the stream. Only libavutil's generic hwcontext API is used, so builds without VAAPI compile unchanged and fall back at runtime.

Verified on an AMD Radeon 760M (Mesa 25.0 radeonsi, FFmpeg 7.1) against the same binary without --vaapi: cut list, frame CSV and SRT byte-identical on a 720p60 H.264 recording with 608 captions and on a HEVC Main10 clip; cut lists identical on two long 720p50 H.264 sports recordings and a 7 h SD MPEG-2 recording (software fallback); four concurrent --vaapi runs match a single run. Also checked --vaapi-device=/dev/null, a missing device, --cuvid --vaapi, --qsv --vaapi and lowres=1. Builds cleanly against libavcodec 59 (Debian 12), 60 (Ubuntu 24.04) and 61 (Debian 13).

Timings with thread_count=2, capped at two cores:

Recording Software --vaapi
1 h 44 min 720p50 H.264 High, 12.6 Mbit/s 583 s, 985 s CPU 393 s, 358 s CPU
2 h 55 min 720p50 H.264 High, 7.7 Mbit/s 903 s, 1528 s CPU 824 s, 697 s CPU
10 min 720p60 H.264 Main, 2.8 Mbit/s 31 s, 58 s CPU 40 s, 40 s CPU
4 min 720p50 HEVC Main10, 6 Mbit/s 67 s, 103 s CPU 93 s, 94 s CPU
7 h SD MPEG-2 (software fallback) 295 s 290 s

VA-API always saves CPU; wall time only drops when software decoding is the bottleneck. Low-bitrate H.264 decodes about as fast in software as through VA-API plus the download. On Main10 the per-frame P010 to YUV420P swscale conversion dominates (ffmpeg alone decodes and downloads that clip in 13 s); a luma-only conversion would be a separate change.

Attach a VA-API device to the native libavcodec decoder so its default
get_format uses the VAAPI hwaccel when the GPU supports the codec and
profile, and decodes in software when it does not. Decoded surfaces are
downloaded (NV12, or P010 which the existing 8-bit conversion handles)
together with their frame properties, so pts, picture type, key frame
flag and A53 caption side data are unchanged.

The device is not attached when --qsv/--cuvid/--dxva2/--vdpau already
selected a decoder, nor with lowres, which hwaccels do not implement. A
device that cannot be opened is logged and decoding continues in
software; 25 consecutive frame download failures abort instead of
silently dropping frames.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant