Skip to content

Commit ef1ba32

Browse files
authored
feat: decode VP9 on the GPU with Media Foundation on Windows (#319)
HardwareVideoDecoderFactory now decodes VP9 profile 0 on Windows, through the VP9 decoder Media Foundation has on Direct3D 11, the way it does H.264 and AV1. The factory offers VP9 where the GPU has the DXVA decoder profile (D3D11_DECODER_PROFILE_VP9_VLD_PROFILE0, with NV12 output) and Windows has a VP9 decoder that uses Direct3D 11: the VP9 Video Extensions of the Microsoft Store. Without either, VP9 stays with libvpx, and negotiation does not change. MFVideoDecoder takes VP9 as a third codec. Two things are particular to it: - A frame with spatial layers goes to libvpx. Its layers reach a decoder back to back without a superframe index; VideoToolbox does not decode that, and a Media Foundation decoder is not known to. - A VP9 stream states its size in the key frames only, so the decoder may have no output type to offer before it has seen one. That is no longer a failure to configure; the output type is set when the transform asks for it, as it is for a change of size. The VP9 tests of the decoder test class run on Windows too, not on macOS only. A VP9 decoder is required with -Dwebrtc.test.hardwareVp9Decoder=true on Windows, as an AV1 decoder is with its own property; on macOS the existing property applies. On an AMD Radeon RX 9070 XT with the VP9 Video Extensions, the decoder tests pass with the hardware decoders required, VP9 decoded by MediaFoundation (VP9VideoExtensionDecoder).
1 parent d2aa2df commit ef1ba32

7 files changed

Lines changed: 112 additions & 52 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ PeerConnectionFactory factory = PeerConnectionFactory.builder()
5151

5252
| Platform | Hardware decoding |
5353
|---|---|
54-
| Windows | H.264 and AV1, on GPUs that decode them, through the Media Foundation decoders of Windows on Direct3D 11 (DXVA) |
54+
| Windows | H.264, AV1 and VP9 (profile 0), on GPUs that decode them, through the Media Foundation decoders of Windows on Direct3D 11 (DXVA) |
5555
| Linux | Not yet; decoding is in software |
5656
| macOS | H.264 through VideoToolbox, as with `DefaultVideoDecoderFactory`, and VP9 (profile 0) through VideoToolbox's VP9 decoder, on Macs that have one |
5757

58-
AV1 on Windows needs the *AV1 Video Extension*, which Windows 11 includes. Decoded frames are copied from GPU memory back to system memory, where WebRTC's frames are, so hardware decoding pays off mostly at high resolutions and with many streams; at low resolutions WebRTC's software decoders are about as cheap. A hardware decoder that fails, or turns out to decode in software, is replaced by the software decoder, which starts with the next key frame.
58+
AV1 on Windows needs the *AV1 Video Extension*, which Windows 11 includes, and VP9 the *VP9 Video Extensions*, a free component of the Microsoft Store that a system may not have; without it, VP9 is decoded in software. Streams with spatial layers (SVC) are decoded in software too, since their layers reach a decoder without the index that says where each ends. Decoded frames are copied from GPU memory back to system memory, where WebRTC's frames are, so hardware decoding pays off mostly at high resolutions and with many streams; at low resolutions WebRTC's software decoders are about as cheap. A hardware decoder that fails, or turns out to decode in software, is replaced by the software decoder, which starts with the next key frame.
5959

6060
On macOS, VP9 decoding in hardware saves CPU: in a measurement on an Apple M2 it took a fraction of the processor time libvpx needs, but multi-threaded libvpx was about as fast on the clock. VideoToolbox decodes VP9 in hardware only where the Mac has a decoder for it, which Apple silicon does; a Mac without one decodes VP9 with libvpx as before. Profile 2 (10 bit), sizes below 64x64 or above 4096x4096, and streams with spatial layers (SVC) are decoded with libvpx too.
6161

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
namespace jni
4040
{
41-
// Decodes H.264 or AV1 on the GPU, with the decoder transform of Windows
41+
// Decodes H.264, AV1 or VP9 on the GPU, with the decoder transform of Windows
4242
// for the codec, which decodes through DXVA on the Direct3D 11 device it
4343
// is given.
4444
//
@@ -52,7 +52,7 @@ namespace jni
5252
class MFVideoDecoder : public webrtc::VideoDecoder
5353
{
5454
public:
55-
// The codec is H.264 or AV1.
55+
// The codec is H.264, AV1 or VP9.
5656
explicit MFVideoDecoder(webrtc::VideoCodecType codec);
5757
~MFVideoDecoder() override;
5858

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727

2828
namespace jni
2929
{
30-
// Creates the Media Foundation decoders that decode on the GPU, for H.264
31-
// and AV1, whichever the GPU decodes and Windows has a decoder for that
30+
// Creates the Media Foundation decoders that decode on the GPU, for H.264,
31+
// AV1 and VP9, whichever the GPU decodes and Windows has a decoder for that
3232
// uses Direct3D 11. It offers them in the formats WebRTC's software
33-
// decoders offer too: H.264 in all its profiles, and AV1 in profile 0.
33+
// decoders offer too: H.264 in all its profiles, and AV1 and VP9 in
34+
// profile 0.
3435
class MFVideoDecoderFactory : public webrtc::VideoDecoderFactory
3536
{
3637
public:
37-
// Returns a factory, or null if the GPU decodes neither codec.
38+
// Returns a factory, or null if the GPU decodes none of the codecs.
3839
static std::unique_ptr<MFVideoDecoderFactory> Create();
3940

4041
~MFVideoDecoderFactory() override = default;
@@ -44,11 +45,12 @@ namespace jni
4445
const webrtc::SdpVideoFormat & format) override;
4546

4647
private:
47-
MFVideoDecoderFactory(bool h264, bool av1);
48+
MFVideoDecoderFactory(bool h264, bool av1, bool vp9);
4849

4950
private:
5051
const bool h264;
5152
const bool av1;
53+
const bool vp9;
5254
};
5355
}
5456

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ namespace jni
5656

5757
const GUID & InputFormat(webrtc::VideoCodecType codec)
5858
{
59-
return codec == webrtc::kVideoCodecAV1 ? MFVideoFormat_AV1 : MFVideoFormat_H264;
59+
switch (codec) {
60+
case webrtc::kVideoCodecAV1:
61+
return MFVideoFormat_AV1;
62+
case webrtc::kVideoCodecVP9:
63+
return MFVideoFormat_VP90;
64+
default:
65+
return MFVideoFormat_H264;
66+
}
6067
}
6168
}
6269

@@ -174,6 +181,16 @@ namespace jni
174181

175182
if (SUCCEEDED(hr)) {
176183
hr = SetOutputType();
184+
185+
// A VP9 stream states its size in the key frames only, and its
186+
// decoder may offer no output type before it has seen one. It then
187+
// asks for the type again with MF_E_TRANSFORM_STREAM_CHANGE, which
188+
// DrainOutput answers, and a decoder that cannot give one at all
189+
// fails there, and the software decoder takes over.
190+
if (FAILED(hr) && codec == webrtc::kVideoCodecVP9 && !resolution.Valid()) {
191+
RTC_LOG(LS_INFO) << implementationName << " offers no output type yet, hr=" << hr;
192+
hr = S_OK;
193+
}
177194
}
178195
if (SUCCEEDED(hr)) {
179196
hr = transform->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
@@ -241,6 +258,15 @@ namespace jni
241258
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
242259
}
243260

261+
// The layers of a VP9 frame with spatial layers reach a decoder back to
262+
// back, without the superframe index that tells where one ends. libvpx
263+
// takes them that way; a hardware decoder is not known to.
264+
if (codec == webrtc::kVideoCodecVP9
265+
&& (image.SpatialIndex().value_or(0) > 0 || image.SpatialLayerFrameSize(1).has_value()))
266+
{
267+
return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
268+
}
269+
244270
ComPtr<IMFMediaBuffer> buffer;
245271
HRESULT hr = MFCreateMemoryBuffer(static_cast<DWORD>(image.size()), &buffer);
246272

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace jni
5858
{
5959
bool h264 = false;
6060
bool av1 = false;
61+
bool vp9 = false;
6162

6263
try {
6364
ComInitializer comInitializer;
@@ -86,23 +87,27 @@ namespace jni
8687
&& HasDirect3DDecoder(MFVideoFormat_H264, manager.Get());
8788
av1 = SupportsDecoderProfile(device.Get(), D3D11_DECODER_PROFILE_AV1_VLD_PROFILE0)
8889
&& HasDirect3DDecoder(MFVideoFormat_AV1, manager.Get());
90+
vp9 = SupportsDecoderProfile(device.Get(), D3D11_DECODER_PROFILE_VP9_VLD_PROFILE0)
91+
&& HasDirect3DDecoder(MFVideoFormat_VP90, manager.Get());
8992
}
9093
catch (...) {
9194
return nullptr;
9295
}
9396

94-
RTC_LOG(LS_INFO) << "Media Foundation hardware decoders, H.264: " << h264 << ", AV1: " << av1;
97+
RTC_LOG(LS_INFO) << "Media Foundation hardware decoders, H.264: " << h264 << ", AV1: " << av1
98+
<< ", VP9: " << vp9;
9599

96-
if (!h264 && !av1) {
100+
if (!h264 && !av1 && !vp9) {
97101
return nullptr;
98102
}
99103

100-
return std::unique_ptr<MFVideoDecoderFactory>(new MFVideoDecoderFactory(h264, av1));
104+
return std::unique_ptr<MFVideoDecoderFactory>(new MFVideoDecoderFactory(h264, av1, vp9));
101105
}
102106

103-
MFVideoDecoderFactory::MFVideoDecoderFactory(bool h264, bool av1) :
107+
MFVideoDecoderFactory::MFVideoDecoderFactory(bool h264, bool av1, bool vp9) :
104108
h264(h264),
105-
av1(av1)
109+
av1(av1),
110+
vp9(vp9)
106111
{
107112
}
108113

@@ -116,6 +121,9 @@ namespace jni
116121
if (av1) {
117122
formats.push_back(webrtc::SdpVideoFormat::AV1Profile0());
118123
}
124+
if (vp9) {
125+
formats.push_back(webrtc::SdpVideoFormat::VP9Profile0());
126+
}
119127

120128
return formats;
121129
}

‎webrtc/src/main/java/dev/onvoid/webrtc/media/video/codec/HardwareVideoDecoderFactory.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
* .build();
3535
* }</pre>
3636
* <p>
37-
* On Windows, H.264 and AV1 are decoded on the GPU through Direct3D 11 by the
38-
* Media Foundation decoders of Windows, where the GPU decodes the codec.
37+
* On Windows, H.264, AV1 and VP9 (profile 0) are decoded on the GPU through
38+
* Direct3D 11 by the Media Foundation decoders of Windows, where the GPU
39+
* decodes the codec and Windows has a decoder for it; VP9 needs the VP9 Video
40+
* Extensions of the Microsoft Store. VP9 streams with spatial layers go to
41+
* libvpx.
3942
* Decoded frames are copied back to system memory, which WebRTC's frames are
4043
* in, so hardware decoding saves CPU mostly at high resolutions. A hardware
4144
* decoder that fails to start, or fails while decoding, is replaced by the

‎webrtc/src/test/java/dev/onvoid/webrtc/HardwareVideoDecoderIntegrationTest.java‎

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
* A machine without a hardware decoder skips the tests that need one, as CI
4747
* runners do. Set the system property {@code webrtc.test.hardwareDecoder} to
4848
* {@code true} on a machine that has one, to make those tests fail instead,
49-
* and {@code webrtc.test.hardwareAv1Decoder} for a GPU that decodes AV1.
49+
* and {@code webrtc.test.hardwareAv1Decoder} for a GPU that decodes AV1, and
50+
* {@code webrtc.test.hardwareVp9Decoder} for one that decodes VP9 on Windows.
51+
* On macOS the VP9 tests follow {@code webrtc.test.hardwareDecoder}.
5052
*/
5153
@Execution(ExecutionMode.SAME_THREAD)
5254
class HardwareVideoDecoderIntegrationTest extends TestBase {
@@ -57,6 +59,8 @@ class HardwareVideoDecoderIntegrationTest extends TestBase {
5759

5860
private static final boolean HARDWARE_AV1_REQUIRED = Boolean.getBoolean("webrtc.test.hardwareAv1Decoder");
5961

62+
private static final boolean HARDWARE_VP9_REQUIRED = Boolean.getBoolean("webrtc.test.hardwareVp9Decoder");
63+
6064
private static final String OS = System.getProperty("os.name").toLowerCase(Locale.ROOT);
6165

6266
private static final Predicate<RTCRtpCodecCapability> H264 = codec ->
@@ -138,14 +142,10 @@ void macDecodesH264WithVideoToolbox() throws Exception {
138142
}
139143

140144
@Test
141-
void macDecodesVp9WithVideoToolbox() throws Exception {
142-
assumeTrue(OS.contains("mac"), "VideoToolbox is available on macOS only");
143-
144-
PeerConnectionFactory hardware = PeerConnectionFactory.builder()
145-
.setAudioDeviceModule(audioDevModule)
146-
.setVideoDecoderFactory(new HardwareVideoDecoderFactory())
147-
.build();
145+
void hardwareDecodesVp9() throws Exception {
146+
assumeVp9Platform();
148147

148+
PeerConnectionFactory hardware = hardwareFactory();
149149
String implementation;
150150

151151
try {
@@ -155,29 +155,22 @@ void macDecodesVp9WithVideoToolbox() throws Exception {
155155
hardware.dispose();
156156
}
157157

158-
boolean hardwareUsed = implementation.contains("VideoToolbox");
159-
160-
if (HARDWARE_REQUIRED) {
161-
assertTrue(hardwareUsed, implementation);
162-
}
163-
else {
164-
assumeTrue(hardwareUsed, "no hardware decoder: " + implementation);
165-
}
158+
expectVp9Hardware(implementation);
166159
}
167160

168161
@Test
169-
void macDecodesVp9InSoftwareByDefault() throws Exception {
170-
assumeTrue(OS.contains("mac"), "VideoToolbox is available on macOS only");
162+
void defaultDecodesVp9InSoftware() throws Exception {
163+
assumeVp9Platform();
171164

172165
// Hardware decoding is opt-in; the shared factory decodes VP9 with libvpx.
173166
String implementation = decoderImplementation(factory, VP9);
174167

175-
assertFalse(implementation.contains("VideoToolbox"), implementation);
168+
assertFalse(isHardware(implementation), implementation);
176169
}
177170

178171
@Test
179-
void macFollowsResolutionChange() throws Exception {
180-
assumeTrue(OS.contains("mac"), "VideoToolbox is available on macOS only");
172+
void hardwareFollowsResolutionChange() throws Exception {
173+
assumeVp9Platform();
181174

182175
PeerConnectionFactory hardware = PeerConnectionFactory.builder()
183176
.setAudioDeviceModule(audioDevModule)
@@ -213,7 +206,7 @@ else if (width == 160 && frame.buffer.getHeight() == 120) {
213206

214207
String implementation = decoderImplementationOf(call);
215208

216-
assumeTrue(implementation.contains("VideoToolbox"), "no hardware decoder: " + implementation);
209+
expectVp9Hardware(implementation);
217210

218211
// The sender restarts at half the size, with a key frame; the
219212
// decoder has to start a session for it.
@@ -229,7 +222,7 @@ else if (width == 160 && frame.buffer.getHeight() == 120) {
229222
assertTrue(half.await(TIMEOUT_SECONDS, TimeUnit.SECONDS), "no frames at the new size");
230223

231224
// Still the hardware decoder, not a fallback.
232-
assertTrue(decoderImplementationOf(call).contains("VideoToolbox"));
225+
assertTrue(isHardware(decoderImplementationOf(call)));
233226

234227
track.removeSink(sink);
235228
receiver.dispose();
@@ -240,12 +233,12 @@ else if (width == 160 && frame.buffer.getHeight() == 120) {
240233
}
241234

242235
@Test
243-
void macVp9NeedsNoKeyFrames() throws Exception {
244-
assumeTrue(OS.contains("mac"), "VideoToolbox is available on macOS only");
236+
void vp9NeedsNoKeyFrames() throws Exception {
237+
assumeVp9Platform();
245238

246239
CallResult result = receiveVideo(hardwareFactory(), VP9, 320, 240, null, 60);
247240

248-
assumeTrue(result.decoder.contains("VideoToolbox"), "no hardware decoder: " + result.decoder);
241+
expectVp9Hardware(result.decoder);
249242

250243
// An inter frame that fails to decode makes the receiver ask for a key
251244
// frame, and every key frame is then the only frame that decodes. A
@@ -255,30 +248,31 @@ void macVp9NeedsNoKeyFrames() throws Exception {
255248
}
256249

257250
@Test
258-
void macVp9TemporalLayers() throws Exception {
259-
assumeTrue(OS.contains("mac"), "VideoToolbox is available on macOS only");
251+
void vp9TemporalLayers() throws Exception {
252+
assumeVp9Platform();
260253

261-
// Temporal layers are one spatial layer, which VideoToolbox decodes.
254+
// Temporal layers are one spatial layer, which the hardware decodes.
262255
CallResult result = receiveVideo(hardwareFactory(), VP9, 320, 240, "L1T3", 60);
263256

264-
assumeTrue(result.decoder.contains("VideoToolbox"), "no hardware decoder: " + result.decoder);
257+
expectVp9Hardware(result.decoder);
265258

266259
assertTrue(result.pliCount <= 1, "key frames requested: " + result.pliCount);
267260
}
268261

269262
@Test
270-
void macVp9SpatialLayers() throws Exception {
271-
assumeTrue(OS.contains("mac"), "VideoToolbox is available on macOS only");
263+
void vp9SpatialLayers() throws Exception {
264+
assumeVp9Platform();
272265

273266
// WebRTC encodes spatial layers only above a size, and drops them
274267
// below it.
275268
CallResult result = receiveVideo(hardwareFactory(), VP9, 640, 480, "L2T2", 60);
276269

277270
assumeTrue(result.scalability.startsWith("L2"), "no spatial layers were encoded: " + result.scalability);
278271

279-
// The frames arrive, from libvpx: VideoToolbox does not decode a
280-
// frame whose layers come without a superframe index.
281-
assertFalse(result.decoder.contains("VideoToolbox"), result.decoder);
272+
// The frames arrive, from libvpx: the layers of a frame come without a
273+
// superframe index, which VideoToolbox does not decode, and which a
274+
// Media Foundation decoder is not known to.
275+
assertFalse(isHardware(result.decoder), result.decoder);
282276
}
283277

284278
private PeerConnectionFactory hardwareFactory() {
@@ -288,6 +282,33 @@ private PeerConnectionFactory hardwareFactory() {
288282
.build();
289283
}
290284

285+
/**
286+
* Skips the test where VP9 is not decoded in hardware: macOS and Windows.
287+
*/
288+
private static void assumeVp9Platform() {
289+
assumeTrue(OS.contains("mac") || OS.contains("win"),
290+
"VP9 is decoded in hardware on macOS and Windows only");
291+
}
292+
293+
private static boolean isHardware(String implementation) {
294+
return implementation.contains("VideoToolbox") || implementation.contains("MediaFoundation");
295+
}
296+
297+
/**
298+
* Where a hardware decoder is required, the test fails without one;
299+
* elsewhere it is skipped.
300+
*/
301+
private static void expectVp9Hardware(String implementation) {
302+
boolean required = OS.contains("mac") ? HARDWARE_REQUIRED : HARDWARE_VP9_REQUIRED;
303+
304+
if (required) {
305+
assertTrue(isHardware(implementation), implementation);
306+
}
307+
else {
308+
assumeTrue(isHardware(implementation), "no hardware decoder: " + implementation);
309+
}
310+
}
311+
291312
/**
292313
* What a call that received video tells about its decoder.
293314
*/

0 commit comments

Comments
 (0)