feat: encode H.264 with VA-API on Intel and AMD GPUs on Linux - #313
Merged
Merged
Conversation
HardwareVideoEncoderFactory now uses the VA-API encoder of the GPU driver on Linux, after NVENC: Intel's iHD driver and Mesa's radeonsi for AMD. libva is loaded at run time, so building needs only its headers, vendored from libva 2.17.0 (MIT, its license goes into the platform jar), and machines without libva or an encoding driver are unaffected. The first DRM render node whose driver encodes H.264 Constrained Baseline with CBR is used, with either the full or the low-power encode entrypoint. The encoder is synchronous, with one reference frame, P-frames only, picture order count type 2 and CAVLC; it fills the sequence, picture and slice parameters and the rate control, frame rate and HRD parameters, and leaves writing the parameter sets and slice headers to the driver. A key frame without SPS and PPS, which a driver that expects packed headers would produce, makes the encoder give up, so that the software encoder takes over rather than sending a stream nothing can decode. Frames are written into the surface as NV12, derived where the driver allows it.
This was referenced Sep 30, 2026
devopvoid
added this pull request to stack #311
October 1, 2026 22:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #312 (then #310, #303): this PR targets its branch and should be merged after it.
Important
Not yet run on hardware. It was developed without a VA-API device: the WSL environment available has Mesa's d3d12 VA driver, but no
/dev/drirender node, and the driver fails to initialize through WSLg's display as well. The code has only been syntax-checked. With VA-API, far more of the encoder's behavior is ours than with NVENC or Media Foundation. See Needs testing below.On Linux,
HardwareVideoEncoderFactorynow uses the VA-API encoder of the GPU driver after NVENC: Intel's iHD driver, and Mesa's radeonsi for AMD. The order on Linux becomes NVENC, then VA-API, then OpenH264. The Java API is unchanged.Loading
libva.so.2andlibva-drm.so.2are loaded at run time. Machines without libva, or without a driver that can encode, are unaffected.va_version.h, which the libva build generates.dependencies/libva/README.mdrecords the details. This makes the cross-compiled ARM targets independent of what their sysroots contain. The license goes into the platform jar underMETA-INF/licenses/libva/./dev/dri/renderD*node whose driver can encode H.264 Constrained Baseline with CBR. It takes the full encode entrypoint where there is one, otherwise the low-power one. The display is shared by all encoders.Encoder
VaapiH264Encoderencodes synchronously on the encoder thread:frame_numcounting modulo 256, andidr_pic_idincremented per IDR framevaDeriveImage, otherwise throughvaCreateImage/vaPutImageHeaders: the driver writes the parameter sets and slice headers; no packed headers are passed. If a key frame comes out without both SPS and PPS (as it would from a driver that expects packed headers), the encoder gives up, so the stream falls back to software instead of sending something nothing can decode.
Testing
g++ -std=c++20 -Wall -Wextra -fsyntax-onlyin WSL (Debian 12) against the vendored headers. The only warning comes from WebRTC's ownScalingSettingsclass.HardwareVideoEncoderIntegrationTestandVideoCodecFactoryTestsstill pass.HardwareVideoEncoderIntegrationTestnow also acceptsVA-API (...)as a hardware encoder.Needs testing
On an Intel iGPU and an AMD GPU, on Linux:
This fails unless H.264 is actually encoded on the GPU. A call's
encoderImplementationstat should readVA-API (<driver>). Most likely to need fixing:OpenH264. The fix would be to write SPS/PPS as packed headers ourselves.