Skip to content

Commit 74846be

Browse files
committed
feat: encode VP9 in hardware on Windows, when turned on
Intel GPUs have a Media Foundation VP9 encoder. The hardware encoder factory uses it for VP9 (profile 0), the way it uses the others, when the system property webrtc.hardware.vp9Encoder is true as the factory is created. It is off by default: the encoder has not been tested on an Intel GPU, and once on, it would take over VP9 from libvpx. Without the property, or where there is no VP9 encoder, VP9 is encoded with libvpx, and a hardware encoder that fails to start, or a stream that asks for SVC, falls back to libvpx as well. VP9 is offered by the software encoders anyway, so turning it on changes nothing that is negotiated. EncoderOutputProcessor passes VP9 frames on as they come, with the VP9 information of a single layer that the Java encoders get too (the group of frames with every key frame), parses their QP, and uses the VP9 quality scaling thresholds of the Java encoders. On a machine with an Intel VP9 encoder registered but no Intel GPU, it fails to start and libvpx takes over, which the test of the property saw.
1 parent 14a0dc4 commit 74846be

11 files changed

Lines changed: 262 additions & 16 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ PeerConnectionFactory factory = PeerConnectionFactory.builder()
3232

3333
Hardware encoders produce a single layer. A stream that asks for SVC, such as an AV1 stream with a scalability mode like `L1T3`, is encoded in software.
3434

35+
VP9 can be encoded in hardware too, on Windows with the Media Foundation VP9 encoder of the GPU driver, which Intel GPUs have. It is off by default, since it has not been tested on such a GPU yet; turn it on with a system property before the factory is created:
36+
37+
```java
38+
System.setProperty("webrtc.hardware.vp9Encoder", "true");
39+
```
40+
41+
Where the GPU has no VP9 encoder, or the stream asks for SVC, VP9 is still encoded with libvpx.
42+
3543
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.
3644

3745
The hardware encoders take over H.264 Constrained Baseline and Baseline with packetization mode 1, and AV1 profile 0, formats the software encoders offer too, so encoding these in hardware never changes what is negotiated. H.265 (Main profile) has no software encoder: it is offered after the other codecs, only where the hardware encodes it, so the codec a peer connection prefers stays the same, and a call uses H.265 only if it is preferred (e.g. with `RTCRtpTransceiver.setCodecPreferences()`) and the other end decodes it. On macOS, VideoToolbox is required to encode H.265 in hardware, and never falls back to its software encoder; a Mac without a hardware HEVC encoder does not offer H.265. When a hardware encoder fails to start, for example because the GPU has no encoder sessions left, or fails while encoding, the stream switches to the next encoder in line (e.g. from NVENC to Media Foundation on Windows), and finally to the software encoder, and continues with a key frame. On a machine without a hardware encoder, the factory encodes like `DefaultVideoEncoderFactory`.

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "common_video/h264/h264_bitstream_parser.h"
2525
#include "common_video/h265/h265_bitstream_parser.h"
2626
#include "modules/video_coding/codecs/h264/include/h264_globals.h"
27+
#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
2728
#include "modules/video_coding/include/video_codec_interface.h"
2829
#include "modules/video_coding/svc/scalable_video_controller_no_layering.h"
2930

@@ -43,7 +44,8 @@ namespace jni
4344
// key frame, not necessarily in front of every one, so they are kept and
4445
// put back where missing. The frame dependencies WebRTC needs for AV1 and
4546
// H.265 are those of a stream without layers, which is all hardware
46-
// encoders produce here.
47+
// encoders produce here. VP9 frames go out as they come, with the VP9
48+
// information of a single layer, as the Java encoders get it.
4749
class EncoderOutputProcessor
4850
{
4951
public:
@@ -64,8 +66,9 @@ namespace jni
6466
webrtc::CodecSpecificInfo & info);
6567

6668
// The quality scaling settings that suit the codec: the thresholds
67-
// of WebRTC's H.264 encoder, for H.265 as well, and none for AV1,
68-
// whose QP is not parsed from the bitstream.
69+
// of WebRTC's H.264 encoder, for H.265 as well, those for VP9 of
70+
// the Java encoders, and none for AV1, whose QP is not parsed from
71+
// the bitstream.
6972
webrtc::VideoEncoder::ScalingSettings GetScalingSettings() const;
7073

7174
private:
@@ -87,6 +90,11 @@ namespace jni
8790
webrtc::H264BitstreamParser h264Parser;
8891
webrtc::H265BitstreamParser h265Parser;
8992
webrtc::ScalableVideoControllerNoLayering svcController;
93+
94+
// The group of frames VP9 describes: one layer, every frame
95+
// referring to the one before.
96+
webrtc::GofInfoVP9 gof;
97+
size_t gofIndex = 0;
9098
};
9199
}
92100

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_HARDWARE_CODEC_OPTIONS_H_
18+
#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_HARDWARE_CODEC_OPTIONS_H_
19+
20+
namespace jni
21+
{
22+
// The Java system property that turns on hardware VP9 encoding, which is
23+
// off by default: only Intel GPUs encode VP9, and the encoder has not been
24+
// tested on one.
25+
constexpr const char * kHardwareVp9EncoderProperty = "webrtc.hardware.vp9Encoder";
26+
27+
// Whether hardware VP9 encoding is turned on: the Java system property
28+
// above is "true", as Boolean.getBoolean has it. Callable from any thread;
29+
// false where Java cannot be asked, for one with an exception pending.
30+
bool IsHardwareVp9EncoderEnabled();
31+
}
32+
33+
#endif

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ namespace jni
3333
// packetization mode 1, since mode 0 needs each NAL unit to fit a packet,
3434
// which hardware encoders cannot be relied on to keep to, and AV1 in
3535
// profile 0. H.265, which WebRTC has no encoder of its own for, is offered
36-
// in Main profile.
36+
// in Main profile. VP9 (profile 0), which only Intel GPUs encode, only if
37+
// asked for.
3738
class MFVideoEncoderFactory : public webrtc::VideoEncoderFactory
3839
{
3940
public:
4041
// Returns a factory, or null if there is no hardware H.264, H.265
41-
// or AV1 encoder on this system.
42-
static std::unique_ptr<MFVideoEncoderFactory> Create();
42+
// or AV1 encoder on this system, nor a VP9 one where that is
43+
// enabled.
44+
static std::unique_ptr<MFVideoEncoderFactory> Create(bool vp9Enabled);
4345

4446
~MFVideoEncoderFactory() override = default;
4547

@@ -48,12 +50,13 @@ namespace jni
4850
const webrtc::SdpVideoFormat & format) override;
4951

5052
private:
51-
MFVideoEncoderFactory(bool h264, bool h265, bool av1);
53+
MFVideoEncoderFactory(bool h264, bool h265, bool av1, bool vp9);
5254

5355
private:
5456
const bool h264;
5557
const bool h265;
5658
const bool av1;
59+
const bool vp9;
5760
};
5861
}
5962

‎webrtc-jni/src/main/cpp/src/media/video/codec/EncoderOutputProcessor.cpp‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "common_video/h264/h264_common.h"
2222
#include "common_video/h265/h265_common.h"
2323
#include "modules/video_coding/codecs/interface/common_constants.h"
24+
#include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
2425
#include "rtc_base/logging.h"
2526

2627
namespace jni
@@ -31,6 +32,10 @@ namespace jni
3132
constexpr int kLowH264QpThreshold = 24;
3233
constexpr int kHighH264QpThreshold = 37;
3334

35+
// Those of the Java VP9 encoders, for a QP of 0 to 255.
36+
constexpr int kLowVp9QpThreshold = 96;
37+
constexpr int kHighVp9QpThreshold = 185;
38+
3439
// AV1 OBU types, section 6.2.2 of the AV1 specification.
3540
constexpr uint8_t kObuSequenceHeader = 1;
3641
constexpr uint8_t kObuTemporalDelimiter = 2;
@@ -121,11 +126,14 @@ namespace jni
121126
if (mode == format.parameters.end() || mode->second != "1") {
122127
packetizationMode = webrtc::H264PacketizationMode::SingleNalUnit;
123128
}
129+
130+
gof.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1);
124131
}
125132

126133
void EncoderOutputProcessor::Reset()
127134
{
128135
parameterSets.clear();
136+
gofIndex = 0;
129137
}
130138

131139
bool EncoderOutputProcessor::Process(std::span<const uint8_t> bitstream, bool keyFrame,
@@ -141,6 +149,11 @@ namespace jni
141149
case webrtc::kVideoCodecH265:
142150
processed = ProcessH265(bitstream, keyFrame, output);
143151
break;
152+
case webrtc::kVideoCodecVP9:
153+
// No parameter sets: every key frame carries what it needs.
154+
output.assign(bitstream.begin(), bitstream.end());
155+
processed = !output.empty();
156+
break;
144157
default:
145158
processed = ProcessH264(bitstream, keyFrame, output);
146159
break;
@@ -165,6 +178,10 @@ namespace jni
165178
h265Parser.ParseBitstream(encoded);
166179
image.qp_ = h265Parser.GetLastSliceQp().value_or(-1);
167180
}
181+
else if (codec == webrtc::kVideoCodecVP9) {
182+
int qp = -1;
183+
image.qp_ = webrtc::vp9::GetQp(output.data(), output.size(), &qp) ? qp : -1;
184+
}
168185
else {
169186
image.qp_ = -1;
170187
}
@@ -328,7 +345,8 @@ namespace jni
328345
}
329346

330347
// AV1 and H.265 have no codec specific information; WebRTC sends
331-
// their frame dependencies from these, in the dependency descriptor.
348+
// their frame dependencies, and those of VP9, from these, in the
349+
// dependency descriptor.
332350
auto layerFrames = svcController.NextFrameConfig(/*restart=*/keyFrame);
333351
info.generic_frame_info = svcController.OnEncodeDone(layerFrames[0]);
334352
info.scalability_mode = webrtc::ScalabilityMode::kL1T1;
@@ -339,6 +357,32 @@ namespace jni
339357
webrtc::RenderResolution(image._encodedWidth, image._encodedHeight)
340358
};
341359
}
360+
361+
if (codec == webrtc::kVideoCodecVP9) {
362+
// VP9 has its own RTP header, which WebRTC fills from these: one
363+
// spatial and one temporal layer, the group of frames sent with
364+
// every key frame.
365+
if (keyFrame) {
366+
gofIndex = 0;
367+
}
368+
369+
info.codecSpecific.VP9.inter_pic_predicted = !keyFrame;
370+
info.codecSpecific.VP9.flexible_mode = false;
371+
info.codecSpecific.VP9.ss_data_available = keyFrame;
372+
info.codecSpecific.VP9.temporal_idx = webrtc::kNoTemporalIdx;
373+
info.codecSpecific.VP9.temporal_up_switch = true;
374+
info.codecSpecific.VP9.inter_layer_predicted = false;
375+
info.codecSpecific.VP9.gof_idx = static_cast<uint8_t>(gofIndex++ % gof.num_frames_in_gof);
376+
info.codecSpecific.VP9.num_spatial_layers = 1;
377+
info.codecSpecific.VP9.first_frame_in_picture = true;
378+
info.codecSpecific.VP9.spatial_layer_resolution_present = keyFrame;
379+
380+
if (keyFrame) {
381+
info.codecSpecific.VP9.width[0] = image._encodedWidth;
382+
info.codecSpecific.VP9.height[0] = image._encodedHeight;
383+
info.codecSpecific.VP9.gof.CopyGofInfoVP9(gof);
384+
}
385+
}
342386
}
343387

344388
webrtc::VideoEncoder::ScalingSettings EncoderOutputProcessor::GetScalingSettings() const
@@ -348,6 +392,9 @@ namespace jni
348392
if (codec == webrtc::kVideoCodecH264 || codec == webrtc::kVideoCodecH265) {
349393
return webrtc::VideoEncoder::ScalingSettings(kLowH264QpThreshold, kHighH264QpThreshold);
350394
}
395+
if (codec == webrtc::kVideoCodecVP9) {
396+
return webrtc::VideoEncoder::ScalingSettings(kLowVp9QpThreshold, kHighVp9QpThreshold);
397+
}
351398

352399
return webrtc::VideoEncoder::ScalingSettings::kOff;
353400
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
#include "media/video/codec/HardwareCodecOptions.h"
18+
#include "JavaUtils.h"
19+
20+
#include <jni.h>
21+
22+
namespace jni
23+
{
24+
bool IsHardwareVp9EncoderEnabled()
25+
{
26+
JNIEnv * env = AttachCurrentThread();
27+
28+
// An exception pending is someone else's to handle, and no JNI call
29+
// may be made meanwhile.
30+
if (env == nullptr || env->ExceptionCheck()) {
31+
return false;
32+
}
33+
if (env->PushLocalFrame(4) != JNI_OK) {
34+
env->ExceptionClear();
35+
return false;
36+
}
37+
38+
bool enabled = false;
39+
40+
// java.lang.Boolean comes from the bootstrap class loader, which any
41+
// thread reaches, one without Java frames as well.
42+
jclass booleanClass = env->FindClass("java/lang/Boolean");
43+
44+
if (booleanClass != nullptr) {
45+
jmethodID getBoolean = env->GetStaticMethodID(booleanClass, "getBoolean", "(Ljava/lang/String;)Z");
46+
jstring name = getBoolean != nullptr ? env->NewStringUTF(kHardwareVp9EncoderProperty) : nullptr;
47+
48+
if (name != nullptr) {
49+
enabled = env->CallStaticBooleanMethod(booleanClass, getBoolean, name) == JNI_TRUE;
50+
}
51+
}
52+
53+
// Raised here, so cleared here.
54+
if (env->ExceptionCheck()) {
55+
env->ExceptionClear();
56+
enabled = false;
57+
}
58+
59+
env->PopLocalFrame(nullptr);
60+
61+
return enabled;
62+
}
63+
}

‎webrtc-jni/src/main/cpp/src/media/video/codec/windows/MFVideoEncoder.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ namespace jni
6161
return MFVideoFormat_AV1;
6262
case webrtc::kVideoCodecH265:
6363
return MFVideoFormat_HEVC;
64+
case webrtc::kVideoCodecVP9:
65+
return MFVideoFormat_VP90;
6466
default:
6567
return MFVideoFormat_H264;
6668
}
@@ -311,6 +313,12 @@ namespace jni
311313

312314
hr = transform->SetOutputType(outputStreamId, outputType.Get(), 0);
313315
}
316+
else if (codec == webrtc::kVideoCodecVP9) {
317+
// 8-bit 4:2:0: VP9 profile 0, what WebRTC offers.
318+
outputType->SetUINT32(MF_MT_VIDEO_PROFILE, eAVEncVP9VProfile_420_8);
319+
320+
hr = transform->SetOutputType(outputStreamId, outputType.Get(), 0);
321+
}
314322
else {
315323
// Constrained Baseline is what peers expect, and is a subset of
316324
// Baseline too. Older encoders only know Baseline, which peers

‎webrtc-jni/src/main/cpp/src/media/video/codec/windows/MFVideoEncoderFactory.cpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ namespace jni
4747
}
4848
}
4949

50-
std::unique_ptr<MFVideoEncoderFactory> MFVideoEncoderFactory::Create()
50+
std::unique_ptr<MFVideoEncoderFactory> MFVideoEncoderFactory::Create(bool vp9Enabled)
5151
{
5252
bool h264 = false;
5353
bool h265 = false;
5454
bool av1 = false;
55+
bool vp9 = false;
5556

5657
try {
5758
ComInitializer comInitializer;
@@ -60,23 +61,29 @@ namespace jni
6061
h264 = HasHardwareEncoder(MFVideoFormat_H264);
6162
h265 = HasHardwareEncoder(MFVideoFormat_HEVC);
6263
av1 = HasHardwareEncoder(MFVideoFormat_AV1);
64+
vp9 = vp9Enabled && HasHardwareEncoder(MFVideoFormat_VP90);
6365
}
6466
catch (...) {
6567
return nullptr;
6668
}
6769

68-
if (!h264 && !h265 && !av1) {
70+
if (vp9Enabled && !vp9) {
71+
RTC_LOG(LS_INFO) << "Hardware VP9 encoding is enabled, but there is no Media Foundation VP9 encoder";
72+
}
73+
74+
if (!h264 && !h265 && !av1 && !vp9) {
6975
RTC_LOG(LS_INFO) << "No Media Foundation hardware encoder found";
7076
return nullptr;
7177
}
7278

73-
return std::unique_ptr<MFVideoEncoderFactory>(new MFVideoEncoderFactory(h264, h265, av1));
79+
return std::unique_ptr<MFVideoEncoderFactory>(new MFVideoEncoderFactory(h264, h265, av1, vp9));
7480
}
7581

76-
MFVideoEncoderFactory::MFVideoEncoderFactory(bool h264, bool h265, bool av1) :
82+
MFVideoEncoderFactory::MFVideoEncoderFactory(bool h264, bool h265, bool av1, bool vp9) :
7783
h264(h264),
7884
h265(h265),
79-
av1(av1)
85+
av1(av1),
86+
vp9(vp9)
8087
{
8188
}
8289

@@ -96,6 +103,9 @@ namespace jni
96103
if (av1) {
97104
formats.push_back(webrtc::SdpVideoFormat::AV1Profile0());
98105
}
106+
if (vp9) {
107+
formats.push_back(webrtc::SdpVideoFormat::VP9Profile0());
108+
}
99109

100110
return formats;
101111
}

‎webrtc-jni/src/main/cpp/src/media/video/codec/windows/WindowsHardwareVideoCodecFactories.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "media/video/codec/HardwareCodecOptions.h"
1718
#include "media/video/codec/HardwareVideoDecoderFactory.h"
1819
#include "media/video/codec/HardwareVideoEncoderFactory.h"
1920
#include "media/video/codec/nvenc/NvencVideoEncoderFactory.h"
@@ -32,7 +33,9 @@ namespace jni
3233
if (auto nvenc = NvencVideoEncoderFactory::Create()) {
3334
factories.push_back(std::move(nvenc));
3435
}
35-
if (auto mediaFoundation = MFVideoEncoderFactory::Create()) {
36+
// VP9 only if asked for: only Intel GPUs encode it, and the encoder
37+
// has not been tested on one.
38+
if (auto mediaFoundation = MFVideoEncoderFactory::Create(IsHardwareVp9EncoderEnabled())) {
3639
factories.push_back(std::move(mediaFoundation));
3740
}
3841

0 commit comments

Comments
 (0)