Skip to content

Commit 58253b0

Browse files
authored
perf: cut the processor time of hardware decoding on Windows (#326)
* perf: reuse the buffers of hardware-decoded frames in the media player For every picture the hardware path dropped the system-memory frame the picture was read back into, and allocated a new I420 frame for the conversion. At 4K that is two fresh allocations of 12 MB per picture, and a fresh allocation costs more to fault in than the copy into it. The read-back frame now stays between pictures, with a new buffer only when the size or the pixel format changes (the transfer copies no more than the smaller of the two frames), and the I420 frames come from a buffer pool, so a picture lands in memory that is already mapped. The conversion of other formats in software uses the pool as well. CPU cycles on the decoding thread per picture, on an NVIDIA GeForce RTX 3080 through Direct3D 11: the read-back went from 11.0 to 5.3 million at 4K and from 3.3 to 1.8 million at 1080p, the conversion from 10.4 to 5.7 and from 2.2 to 1.3. * 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 3e539f7 commit 58253b0

5 files changed

Lines changed: 119 additions & 23 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/include/media/VideoDecoder.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ namespace ffmpeg
120120
// reference the caller owns.
121121
int ConvertToI420(const AVFrame * source, AVFrame ** result);
122122

123+
// A new I420 frame of the given size, whose buffer comes from a
124+
// pool, so that the picture lands in memory that is already
125+
// mapped. The caller owns the frame.
126+
int AllocateI420(int width, int height, AVFrame ** result);
127+
123128
// A picture decoded while the packets of a failed hardware decoder
124129
// were decoded again, which the caller has not asked for yet.
125130
struct Pending
@@ -133,6 +138,12 @@ namespace ffmpeg
133138
SwsContext * sws_context_ = nullptr;
134139
AVFrame * decoded_ = nullptr;
135140
AVFrame * transferred_ = nullptr;
141+
// The size of the decoder's surfaces that transferred_ was made for.
142+
int transfer_width_ = 0;
143+
int transfer_height_ = 0;
144+
AVBufferPool * pool_ = nullptr;
145+
int pool_width_ = 0;
146+
int pool_height_ = 0;
136147
AVRational time_base_ = { 0, 1 };
137148

138149
AVPixelFormat hardware_format_ = AV_PIX_FMT_NONE;

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

Lines changed: 89 additions & 17 deletions
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
@@ -232,6 +232,14 @@ namespace ffmpeg
232232

233233
sws_context_ = nullptr;
234234
}
235+
236+
// Frames still out with WebRTC keep their buffers, and the pool goes
237+
// when the last of them comes back.
238+
av_buffer_pool_uninit(&pool_);
239+
240+
pool_width_ = 0;
241+
pool_height_ = 0;
242+
235243
if (codec_context_ != nullptr) {
236244
avcodec_free_context(&codec_context_);
237245
}
@@ -326,18 +334,35 @@ namespace ffmpeg
326334
// The picture is in the memory of the hardware decoder. Bring it
327335
// into system memory, as NV12, and convert it like any other
328336
// format that is not I420.
329-
av_frame_unref(transferred_);
337+
//
338+
// The system-memory frame stays between pictures: a buffer of
339+
// this size is a fresh allocation every time it is dropped, and
340+
// the pages of a fresh allocation cost more to fault in than the
341+
// copy into them. The buffer is made for the size of the decoder's
342+
// surfaces, which is as much as a transfer may copy (Direct3D 12
343+
// copies all of it, padding included), so it is kept only for
344+
// as long as those, the picture and the format stay as they are.
345+
const AVHWFramesContext * frames = reinterpret_cast<const AVHWFramesContext *>(decoded_->hw_frames_ctx->data);
346+
347+
if (transferred_->buf[0] != nullptr && (transferred_->width != decoded_->width
348+
|| transferred_->height != decoded_->height || transferred_->format != frames->sw_format
349+
|| transfer_width_ != frames->width || transfer_height_ != frames->height)) {
350+
av_frame_unref(transferred_);
351+
}
352+
353+
const bool allocated = transferred_->buf[0] == nullptr;
330354

331355
result = av_hwframe_transfer_data(transferred_, decoded_, 0);
332356

333357
if (result >= 0) {
334-
transferred_->width = decoded_->width;
335-
transferred_->height = decoded_->height;
358+
if (allocated) {
359+
transfer_width_ = frames->width;
360+
transfer_height_ = frames->height;
361+
}
336362

337363
result = ConvertToI420(transferred_, frame);
338364
}
339365

340-
av_frame_unref(transferred_);
341366
av_frame_unref(decoded_);
342367
}
343368
else if (IsI420(decoded_->format)) {
@@ -485,21 +510,11 @@ namespace ffmpeg
485510
return AVERROR(EINVAL);
486511
}
487512

488-
AVFrame * converted = av_frame_alloc();
513+
AVFrame * converted = nullptr;
489514

490-
if (converted == nullptr) {
491-
return AVERROR(ENOMEM);
492-
}
493-
494-
converted->format = AV_PIX_FMT_YUV420P;
495-
converted->width = source->width;
496-
converted->height = source->height;
497-
498-
int error = av_frame_get_buffer(converted, 0);
515+
int error = AllocateI420(source->width, source->height, &converted);
499516

500517
if (error < 0) {
501-
av_frame_free(&converted);
502-
503518
return error;
504519
}
505520

@@ -517,6 +532,63 @@ namespace ffmpeg
517532
return 0;
518533
}
519534

535+
int VideoDecoder::AllocateI420(int width, int height, AVFrame ** result)
536+
{
537+
constexpr int kAlign = 32;
538+
539+
if (pool_ == nullptr || pool_width_ != width || pool_height_ != height) {
540+
av_buffer_pool_uninit(&pool_);
541+
542+
const int size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width, height, kAlign);
543+
544+
if (size < 0) {
545+
return size;
546+
}
547+
548+
pool_ = av_buffer_pool_init(size, av_buffer_allocz);
549+
pool_width_ = width;
550+
pool_height_ = height;
551+
552+
if (pool_ == nullptr) {
553+
return AVERROR(ENOMEM);
554+
}
555+
}
556+
557+
AVFrame * frame = av_frame_alloc();
558+
559+
if (frame == nullptr) {
560+
return AVERROR(ENOMEM);
561+
}
562+
563+
// The buffer goes back to the pool when the last reference to the
564+
// frame is dropped, so the memory of a picture is reused for the
565+
// next ones rather than allocated, and faulted in, each time.
566+
frame->buf[0] = av_buffer_pool_get(pool_);
567+
568+
if (frame->buf[0] == nullptr) {
569+
av_frame_free(&frame);
570+
571+
return AVERROR(ENOMEM);
572+
}
573+
574+
frame->format = AV_PIX_FMT_YUV420P;
575+
frame->width = width;
576+
frame->height = height;
577+
578+
int error = av_image_fill_arrays(frame->data, frame->linesize, frame->buf[0]->data,
579+
AV_PIX_FMT_YUV420P, width, height, kAlign);
580+
581+
if (error < 0) {
582+
av_frame_free(&frame);
583+
584+
return error;
585+
}
586+
587+
*result = frame;
588+
589+
return 0;
590+
}
591+
520592
int VideoDecoder::GetWidth() const
521593
{
522594
return codec_context_ != nullptr ? codec_context_->width : 0;

‎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)