Skip to content

Commit bdcf83a

Browse files
committed
feat: decode H.265 with VA-API on Linux
Adds an H.265 Main decoder for Intel and AMD GPUs, with its own parser for the parameter sets and slice headers. VA-API decoding no longer needs webrtc.hardware.vaapiExperimental, which is removed.
1 parent 2394b9d commit bdcf83a

13 files changed

Lines changed: 2552 additions & 42 deletions

File tree

‎docs/guide/advanced/video-codecs.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ System.setProperty("webrtc.hardware.vp9Encoder", "true");
4040

4141
Where the GPU has no VP9 encoder, or the stream asks for SVC, VP9 is still encoded with libvpx.
4242

43-
On Linux, H.265 is encoded with the VA-API encoder of Intel and AMD drivers where the driver encodes H.265 Main. It is offered only for sending: Intel and AMD GPUs do not decode H.265 here yet, so the other end has to.
43+
On Linux, H.265 is encoded with the VA-API encoder of Intel and AMD drivers where the driver encodes H.265 Main. Receiving it needs a hardware H.265 decoder too, since there is no software one: on Intel and AMD GPUs that is the VA-API decoder (see below).
4444

4545
NVENC needs an NVIDIA driver of version 522 or newer on Windows, 520 or newer on Linux. VA-API needs libva 2 and a driver that encodes H.264, such as Intel's `intel-media-va-driver` (iHD) or Mesa's `mesa-va-drivers` for AMD, and access to a render node in `/dev/dri`. All of them are loaded at run time, so nothing needs to be installed on machines without them.
4646

@@ -62,7 +62,7 @@ PeerConnectionFactory factory = PeerConnectionFactory.builder()
6262
| Platform | Hardware decoding |
6363
|---|---|
6464
| Windows | H.264, H.265 (Main profile), AV1 and VP9 (profile 0), on GPUs that decode them, through the Media Foundation decoders of Windows on Direct3D 11 (DXVA) |
65-
| Linux | H.264, H.265 (Main profile), VP9 and AV1 (profile 0) on NVIDIA GPUs that decode them, through NVDEC; VP9 (profile 0) on Intel and AMD GPUs through VA-API, when turned on with `webrtc.hardware.vaapiExperimental` |
65+
| Linux | H.264, H.265 (Main profile), VP9 and AV1 (profile 0) on NVIDIA GPUs that decode them, through NVDEC; VP9 (profile 0) and H.265 (Main profile) on Intel and AMD GPUs through VA-API |
6666
| macOS | H.264 through VideoToolbox, as with `DefaultVideoDecoderFactory`; VP9 (profile 0) and H.265 (Main profile) through the decoders of VideoToolbox, on Macs that have them, and AV1 (profile 0) on Apple M3 and newer |
6767

6868
On Linux, NVDEC needs an NVIDIA driver, which provides `libcuda.so.1` and `libnvcuvid.so.1`; both are loaded when the factory is made, so a machine without the driver is not affected. Streams with spatial layers are decoded in software.

‎webrtc-jni/src/main/cpp/include/media/video/codec/HardwareCodecOptions.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,10 @@ namespace jni
2424
// tested on one.
2525
constexpr const char * kHardwareVp9EncoderProperty = "webrtc.hardware.vp9Encoder";
2626

27-
// The Java system property that turns on VA-API decoding, which is off by
28-
// default: it has not been tested on an Intel or AMD GPU under Linux yet.
29-
constexpr const char * kExperimentalVaapiProperty = "webrtc.hardware.vaapiExperimental";
30-
3127
// Whether hardware VP9 encoding is turned on: the Java system property
3228
// above is "true", as Boolean.getBoolean has it. Callable from any thread;
3329
// false where Java cannot be asked, for one with an exception pending.
3430
bool IsHardwareVp9EncoderEnabled();
35-
36-
// Whether VA-API decoding is turned on, the same way.
37-
bool IsExperimentalVaapiEnabled();
3831
}
3932

4033
#endif
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef JNI_WEBRTC_MEDIA_VIDEO_CODEC_H265_PARSER_H_
18+
#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_H265_PARSER_H_
19+
20+
#include <array>
21+
#include <cstdint>
22+
#include <map>
23+
#include <optional>
24+
#include <span>
25+
#include <vector>
26+
27+
namespace jni
28+
{
29+
// A short-term reference picture set (7.3.7), as the POC differences of
30+
// its pictures to the current one.
31+
struct H265ShortTermRps
32+
{
33+
static constexpr int kMaxPictures = 16;
34+
35+
int numNegative = 0;
36+
int numPositive = 0;
37+
std::array<int32_t, kMaxPictures> deltaPocS0 = {};
38+
std::array<bool, kMaxPictures> usedS0 = {};
39+
std::array<int32_t, kMaxPictures> deltaPocS1 = {};
40+
std::array<bool, kMaxPictures> usedS1 = {};
41+
};
42+
43+
// What the decoder needs of a sequence parameter set (7.3.2.2), up to the
44+
// VUI, which decoding does not depend on.
45+
struct H265Sps
46+
{
47+
uint32_t generalProfileIdc = 0;
48+
uint32_t generalProfileCompatibility = 0;
49+
uint32_t chromaFormatIdc = 0;
50+
bool separateColourPlane = false;
51+
uint32_t width = 0;
52+
uint32_t height = 0;
53+
// The conformance window, in luma samples.
54+
uint32_t cropLeft = 0;
55+
uint32_t cropRight = 0;
56+
uint32_t cropTop = 0;
57+
uint32_t cropBottom = 0;
58+
uint32_t bitDepthLumaMinus8 = 0;
59+
uint32_t bitDepthChromaMinus8 = 0;
60+
uint32_t log2MaxPocLsbMinus4 = 0;
61+
// Of the highest sub-layer.
62+
uint32_t maxDecPicBufferingMinus1 = 0;
63+
uint32_t maxNumReorderPics = 0;
64+
uint32_t log2MinCbMinus3 = 0;
65+
uint32_t log2DiffMaxMinCb = 0;
66+
uint32_t log2MinTbMinus2 = 0;
67+
uint32_t log2DiffMaxMinTb = 0;
68+
uint32_t maxTransformDepthInter = 0;
69+
uint32_t maxTransformDepthIntra = 0;
70+
bool scalingListEnabled = false;
71+
bool amp = false;
72+
bool sao = false;
73+
bool pcm = false;
74+
uint32_t pcmBitDepthLumaMinus1 = 0;
75+
uint32_t pcmBitDepthChromaMinus1 = 0;
76+
uint32_t log2MinPcmCbMinus3 = 0;
77+
uint32_t log2DiffMaxMinPcmCb = 0;
78+
bool pcmLoopFilterDisabled = false;
79+
std::vector<H265ShortTermRps> shortTermRps;
80+
bool longTermRefsPresent = false;
81+
std::vector<uint32_t> ltRefPicPocLsb;
82+
std::vector<bool> ltUsedByCurrPic;
83+
bool temporalMvp = false;
84+
bool strongIntraSmoothing = false;
85+
86+
uint32_t Log2CtbSize() const;
87+
uint32_t WidthInCtbs() const;
88+
uint32_t HeightInCtbs() const;
89+
};
90+
91+
// A picture parameter set (7.3.2.3).
92+
struct H265Pps
93+
{
94+
uint32_t spsId = 0;
95+
bool dependentSliceSegments = false;
96+
bool outputFlagPresent = false;
97+
uint32_t numExtraSliceHeaderBits = 0;
98+
bool signDataHiding = false;
99+
bool cabacInitPresent = false;
100+
uint32_t numRefIdxL0DefaultMinus1 = 0;
101+
uint32_t numRefIdxL1DefaultMinus1 = 0;
102+
int32_t initQpMinus26 = 0;
103+
bool constrainedIntraPred = false;
104+
bool transformSkip = false;
105+
bool cuQpDelta = false;
106+
uint32_t diffCuQpDeltaDepth = 0;
107+
int32_t cbQpOffset = 0;
108+
int32_t crQpOffset = 0;
109+
bool sliceChromaQpOffsetsPresent = false;
110+
bool weightedPred = false;
111+
bool weightedBipred = false;
112+
bool transquantBypass = false;
113+
bool tiles = false;
114+
bool entropyCodingSync = false;
115+
uint32_t numTileColumnsMinus1 = 0;
116+
uint32_t numTileRowsMinus1 = 0;
117+
bool uniformSpacing = true;
118+
// Of all columns and rows but the last, where the spacing is not
119+
// uniform.
120+
std::vector<uint32_t> columnWidthMinus1;
121+
std::vector<uint32_t> rowHeightMinus1;
122+
bool loopFilterAcrossTiles = false;
123+
bool loopFilterAcrossSlices = false;
124+
bool deblockingFilterOverrideEnabled = false;
125+
bool deblockingFilterDisabled = false;
126+
int32_t betaOffsetDiv2 = 0;
127+
int32_t tcOffsetDiv2 = 0;
128+
bool scalingListDataPresent = false;
129+
bool listsModificationPresent = false;
130+
uint32_t log2ParallelMergeLevelMinus2 = 0;
131+
bool sliceHeaderExtensionPresent = false;
132+
bool extensionPresent = false;
133+
};
134+
135+
// A slice segment header (7.3.6.1). A dependent slice segment carries the
136+
// fields of the independent one before it, which the parser fills in.
137+
struct H265SliceHeader
138+
{
139+
static constexpr int kMaxRefs = 16;
140+
141+
uint8_t nalType = 0;
142+
uint8_t temporalId = 0;
143+
bool firstSliceInPicture = false;
144+
bool noOutputOfPriorPics = false;
145+
uint32_t ppsId = 0;
146+
bool dependent = false;
147+
uint32_t segmentAddress = 0;
148+
uint32_t sliceType = 0;
149+
bool picOutput = true;
150+
uint32_t pocLsb = 0;
151+
// The short-term set of the picture, and how many bits it took in the
152+
// header, 0 when it is one of the SPS.
153+
H265ShortTermRps shortTermRps;
154+
uint32_t shortTermRpsBits = 0;
155+
// The long-term pictures: the POC (LSBs, or all of it where the MSBs
156+
// are given), whether the MSBs are given and whether the current
157+
// picture uses them.
158+
std::vector<int32_t> longTermPoc;
159+
std::vector<bool> longTermMsbPresent;
160+
std::vector<bool> longTermUsed;
161+
bool temporalMvp = false;
162+
bool saoLuma = false;
163+
bool saoChroma = false;
164+
uint32_t numRefIdxL0Minus1 = 0;
165+
uint32_t numRefIdxL1Minus1 = 0;
166+
bool listModificationL0 = false;
167+
bool listModificationL1 = false;
168+
std::array<uint32_t, kMaxRefs> listEntryL0 = {};
169+
std::array<uint32_t, kMaxRefs> listEntryL1 = {};
170+
bool mvdL1Zero = false;
171+
bool cabacInit = false;
172+
bool collocatedFromL0 = true;
173+
uint32_t collocatedRefIdx = 0;
174+
uint32_t lumaLog2WeightDenom = 0;
175+
int32_t deltaChromaLog2WeightDenom = 0;
176+
std::array<std::array<int32_t, kMaxRefs>, 2> deltaLumaWeight = {};
177+
std::array<std::array<int32_t, kMaxRefs>, 2> lumaOffset = {};
178+
std::array<std::array<std::array<int32_t, 2>, kMaxRefs>, 2> deltaChromaWeight = {};
179+
std::array<std::array<std::array<int32_t, 2>, kMaxRefs>, 2> chromaOffset = {};
180+
uint32_t fiveMinusMaxNumMergeCand = 0;
181+
int32_t qpDelta = 0;
182+
int32_t cbQpOffset = 0;
183+
int32_t crQpOffset = 0;
184+
bool deblockingFilterDisabled = false;
185+
int32_t betaOffsetDiv2 = 0;
186+
int32_t tcOffsetDiv2 = 0;
187+
bool loopFilterAcrossSlices = false;
188+
uint32_t numEntryPoints = 0;
189+
// Where the slice data begins, in bytes from the NAL unit header, with
190+
// emulation prevention bytes counted.
191+
uint32_t dataByteOffset = 0;
192+
193+
bool IsIntra() const;
194+
bool IsB() const;
195+
// NumPicTotalCurr (7-55).
196+
uint32_t NumPicTotalCurr() const;
197+
};
198+
199+
// Parses the parameter sets and slice segment headers of an H.265 stream,
200+
// as a VA-API decoder, which leaves that to the application, needs them.
201+
// It takes 8 bit 4:2:0 Main and Main Still Picture streams; anything else,
202+
// including scaling lists and extensions, is not parsed.
203+
class H265Parser
204+
{
205+
public:
206+
// NAL unit types (table 7-1).
207+
static constexpr uint8_t kNalRaslN = 8;
208+
static constexpr uint8_t kNalRaslR = 9;
209+
static constexpr uint8_t kNalBlaWLp = 16;
210+
static constexpr uint8_t kNalIdrWRadl = 19;
211+
static constexpr uint8_t kNalIdrNLp = 20;
212+
static constexpr uint8_t kNalCraNut = 21;
213+
static constexpr uint8_t kNalReservedIrap23 = 23;
214+
static constexpr uint8_t kNalVps = 32;
215+
static constexpr uint8_t kNalSps = 33;
216+
static constexpr uint8_t kNalPps = 34;
217+
static constexpr uint8_t kNalEos = 36;
218+
219+
// Slice types (7.4.7.1).
220+
static constexpr uint32_t kSliceB = 0;
221+
static constexpr uint32_t kSliceP = 1;
222+
static constexpr uint32_t kSliceI = 2;
223+
224+
// Each takes a NAL unit from its header on, with emulation
225+
// prevention bytes, and returns false if it is broken or not one
226+
// the parser takes.
227+
bool ParseSps(std::span<const uint8_t> nal);
228+
bool ParsePps(std::span<const uint8_t> nal);
229+
std::optional<H265SliceHeader> ParseSliceHeader(std::span<const uint8_t> nal);
230+
231+
const H265Sps * Sps(uint32_t id) const;
232+
const H265Pps * Pps(uint32_t id) const;
233+
234+
void Reset();
235+
236+
private:
237+
std::map<uint32_t, H265Sps> spss;
238+
std::map<uint32_t, H265Pps> ppss;
239+
// The last independent slice segment, whose fields a dependent one
240+
// takes.
241+
std::optional<H265SliceHeader> lastIndependent;
242+
};
243+
}
244+
245+
#endif

0 commit comments

Comments
 (0)