Skip to content

Commit d37a15e

Browse files
committed
perf: decode through Direct3D 12 first in the media player on Windows
FFmpeg 8.1 has Direct3D 12 hwaccels for H.264 and VP9. The Windows build now enables them (h264_d3d12va, vp9_d3d12va), and the player tries a Direct3D 12 device before the Direct3D 11 one it used. A machine without Windows 10 2004 or a driver that decodes through Direct3D 12 has no such device, and goes on with Direct3D 11 as before. The pictures are the same: every test of the hardware path compares them with software decoding. Reading a picture back is what costs processor time on this path, and Direct3D 12 does it for about half of what Direct3D 11 does. Process CPU per frame in the player on an NVIDIA GeForce RTX 3080, median of four runs of steady playback, one machine and synthetic media: software Direct3D 11 Direct3D 12 1080p H.264 3.6 ms 4.2 ms 2.1 ms 4K H.264 6.6 ms 12.4 ms 5.4 ms Direct3D 11 used more processor time than software at 4K, Direct3D 12 less. The component list changed, so the next build rebuilds FFmpeg on Windows.
1 parent 7ff8353 commit d37a15e

4 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎docs/guide/media/media-files.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ boolean hardware = source.getPlayer().isHardwareDecoding();
222222

223223
The pictures are the same ones software decoding gives; in the tests, every frame of H.264 and VP9 media is compared with its software counterpart. Hardware decoding is off unless asked for.
224224

225-
- **Platforms:** macOS through VideoToolbox, Windows through Direct3D 11, and Linux through NVDEC on NVIDIA GPUs (x86-64 and ARM64; the driver provides `libcuda.so.1` and `libnvcuvid.so.1`, which are loaded when a player asks for hardware). The flag is accepted everywhere; where the platform has no hardware decoder, or the FFmpeg build has none for it (32-bit ARM, VA-API on Intel and AMD GPUs), the video is decoded in software, and `isHardwareDecoding()` says so.
225+
- **Platforms:** macOS through VideoToolbox, Windows through Direct3D 12 (Windows 10 version 2004 or later, with a driver that decodes video through it) and otherwise Direct3D 11, and Linux through NVDEC on NVIDIA GPUs (x86-64 and ARM64; the driver provides `libcuda.so.1` and `libnvcuvid.so.1`, which are loaded when a player asks for hardware). The flag is accepted everywhere; where the platform has no hardware decoder, or the FFmpeg build has none for it (32-bit ARM, VA-API on Intel and AMD GPUs), the video is decoded in software, and `isHardwareDecoding()` says so.
226226
- **Codecs:** H.264 and VP9. VP8, MPEG-4, MJPEG and H.265/HEVC are decoded in software.
227227
- **Fallback:** a stream the hardware does not take, such as a profile it cannot decode, is decoded in software without the player noticing more than `isHardwareDecoding()` turning `false`. If the hardware fails after it has produced a picture, decoding goes on in software from the next key frame.
228-
- **What it saves** is processor time, not the copy: a decoded picture is read back from the media engine into system memory, and converted to I420 as WebRTC wants it. On an Apple M2, a 1080p H.264 stream took 11.6 ms of processor time per frame in software and 1.4 ms in hardware, and a 4K stream 35.7 ms against 4.2 ms, about seven to eight times less. Per frame, software on all cores is faster on the clock, which does not matter at playback speed. These are figures for one machine and for synthetic media, not a promise.
228+
- **What it saves** is processor time, not the copy: a decoded picture is read back from the media engine into system memory, and converted to I420 as WebRTC wants it. On an Apple M2, a 1080p H.264 stream took 11.6 ms of processor time per frame in software and 1.4 ms in hardware, and a 4K stream 35.7 ms against 4.2 ms, about seven to eight times less. Per frame, software on all cores is faster on the clock, which does not matter at playback speed. Windows gains less: on an NVIDIA GeForce RTX 3080 through Direct3D 12, a 1080p H.264 stream took about 2.1 ms of processor time per frame against 3.6 ms in software, and a 4K stream 5.4 ms against 6.6 ms. Direct3D 11 reads the picture back more slowly and took more than software at 4K (12.4 ms), which is why Direct3D 12 comes first. These are figures for one machine each and for synthetic media, not a promise; try it on your own streams before relying on it.
229229

230230
## Playing a Live Stream
231231

‎webrtc-java-media/src/main/cpp/dependencies/ffmpeg/CMakeLists.txt‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ if(APPLE)
132132
--enable-hwaccel=vp9_videotoolbox
133133
)
134134
elseif(WIN32)
135-
# Direct3D 11 only. DXVA2, on Direct3D 9, would add nothing on a GPU of
136-
# the last decade, which decodes H.264 and VP9 through Direct3D 11.
135+
# Direct3D 11, and 12 further down. DXVA2, on Direct3D 9, would add
136+
# nothing on a GPU of the last decade, which decodes H.264 and VP9
137+
# through Direct3D 11.
137138
# FFmpeg loads d3d11.dll and dxgi.dll when a device is created, so a
138139
# machine without a GPU decoder loads the libraries all the same.
139140
# The decoders offer the D3D11 pixel format, which the *_d3d11va2
@@ -146,6 +147,17 @@ elseif(WIN32)
146147
--enable-hwaccel=h264_d3d11va2
147148
--enable-hwaccel=vp9_d3d11va2
148149
)
150+
# Direct3D 12 as well, which the player tries first: reading a picture
151+
# back costs about half the processor time it does through Direct3D 11 at
152+
# 4K. It needs Windows 10 2004 or later and a driver with Direct3D 12
153+
# video decoding; a machine without them has a Direct3D 11 decoder to
154+
# fall back to, as it always had. Like Direct3D 11 it is loaded when a
155+
# device is created.
156+
list(APPEND FFMPEG_COMPONENTS
157+
--enable-d3d12va
158+
--enable-hwaccel=h264_d3d12va
159+
--enable-hwaccel=vp9_d3d12va
160+
)
149161
elseif(LINUX AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64|aarch64|arm64|ARM64)$")
150162
# NVDEC, on NVIDIA GPUs. FFmpeg loads the CUDA driver and libnvcuvid with
151163
# dlopen when a device is created, so the libraries load on a machine with

‎webrtc-java-media/src/main/cpp/src/media/VideoDecoder.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace
4141
#if defined(__APPLE__)
4242
return { AV_HWDEVICE_TYPE_VIDEOTOOLBOX };
4343
#elif defined(_WIN32)
44-
return { AV_HWDEVICE_TYPE_D3D11VA };
44+
return { AV_HWDEVICE_TYPE_D3D12VA, AV_HWDEVICE_TYPE_D3D11VA };
4545
#else
4646
return { AV_HWDEVICE_TYPE_CUDA };
4747
#endif

‎webrtc-java-media/src/main/java/dev/onvoid/webrtc/media/player/MediaPlayer.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ public MediaPlayer(MediaReader reader, CustomVideoSource videoSource,
106106
* <p>
107107
* With {@code hardwareDecoding} the player decodes H.264 and VP9 video on
108108
* the media engine or GPU of the machine where there is one the platform
109-
* offers (VideoToolbox on macOS, Direct3D 11 on Windows, NVDEC on NVIDIA
110-
* GPUs on Linux), which takes a fraction of the processor time. The
109+
* offers (VideoToolbox on macOS, Direct3D 12 or 11 on Windows, NVDEC on
110+
* NVIDIA GPUs on Linux), which takes less processor time, by an amount
111+
* that depends on the platform and the stream. The
111112
* pictures are the same as software decoding gives. A stream the
112113
* hardware does not take, or a codec it has no decoder for, is decoded in
113114
* software; {@link #isHardwareDecoding()} tells which it is.

0 commit comments

Comments
 (0)