path: root/
diff options
authorPhilip Langdale <>2016-09-04 15:23:55 -0700
committerwm4 <wm4@nowhere>2016-09-08 16:06:12 +0200
commit2048ad2b8af1becddb70cad2a7adc0361b779443 (patch)
treebd3743b17ca85c9d0080f2ecd0f20209a3f98b5c /
parent622f9cf1aac453da6f9c161e3fce4352b8697c90 (diff)
hwdec/opengl: Add support for CUDA and cuvid/NvDecode
Nvidia's "NvDecode" API (up until recently called "cuvid" is a cross platform, but nvidia proprietary API that exposes their hardware video decoding capabilities. It is analogous to their DXVA or VDPAU support on Windows or Linux but without using platform specific API calls. As a rule, you'd rather use DXVA or VDPAU as these are more mature and well supported APIs, but on Linux, VDPAU is falling behind the hardware capabilities, and there's no sign that nvidia are making the investments to update it. Most concretely, this means that there is no VP8/9 or HEVC Main10 support in VDPAU. On the other hand, NvDecode does export vp8/9 and partial support for HEVC Main10 (more on that below). ffmpeg already has support in the form of the "cuvid" family of decoders. Due to the design of the API, it is best exposed as a full decoder rather than an hwaccel. As such, there are decoders like h264_cuvid, hevc_cuvid, etc. These decoders support two output paths today - in both cases, NV12 frames are returned, either in CUDA device memory or regular system memory. In the case of the system memory path, the decoders can be used as-is in mpv today with a command line like: mpv --vd=lavc:h264_cuvid foobar.mp4 Doing this will take advantage of hardware decoding, but the cost of the memcpy to system memory adds up, especially for high resolution video (4K etc). To avoid that, we need an hwdec that takes advantage of CUDA's OpenGL interop to copy from device memory into OpenGL textures. That is what this change implements. The process is relatively simple as only basic device context aquisition needs to be done by us - the CUDA buffer pool is managed by the decoder - thankfully. The hwdec looks a bit like the vdpau interop one - the hwdec maintains a single set of plane textures and each output frame is repeatedly mapped into these textures to pass on. The frames are always in NV12 format, at least until 10bit output supports emerges. The only slightly interesting part of the copying process is that CUDA works by associating PBOs, so we need to define these for each of the textures. TODO Items: * I need to add a download_image function for screenshots. This would do the same copy to system memory that the decoder's system memory output does. * There are items to investigate on the ffmpeg side. There appears to be a problem with timestamps for some content. Final note: I mentioned HEVC Main10. While there is no 10bit output support, NvDecode can return dithered 8bit NV12 so you can take advantage of the hardware acceleration. This particular mode requires compiling ffmpeg with a modified header (or possibly the CUDA 8 RC) and is not upstream in ffmpeg yet. Usage: You will need to specify vo=opengl and hwdec=cuda. Note that hwdec=auto will probably not work as it will try to use vdpau first. mpv --hwdec=cuda --vo=opengl foobar.mp4 If you want to use filters that require frames in system memory, just use the decoder directly without the hwdec, as documented above.
Diffstat (limited to '')
1 files changed, 2 insertions, 0 deletions
diff --git a/ b/
index f246ae4a33..23f61c0d2d 100644
--- a/
+++ b/
@@ -286,6 +286,7 @@ def build(ctx):
( "video/vdpau.c", "vdpau" ),
( "video/vdpau_mixer.c", "vdpau" ),
( "video/decode/dec_video.c"),
+ ( "video/decode/cuda.c", "cuda-gl" ),
( "video/decode/dxva2.c", "d3d-hwaccel" ),
( "video/decode/d3d11va.c", "d3d-hwaccel" ),
( "video/decode/d3d.c", "win32" ),
@@ -341,6 +342,7 @@ def build(ctx):
( "video/out/opengl/egl_helpers.c", "egl-helpers" ),
( "video/out/opengl/formats.c", "gl" ),
( "video/out/opengl/hwdec.c", "gl" ),
+ ( "video/out/opengl/hwdec_cuda.c", "cuda-gl" ),
( "video/out/opengl/hwdec_d3d11egl.c", "egl-angle" ),
( "video/out/opengl/hwdec_d3d11eglrgb.c","egl-angle" ),
( "video/out/opengl/hwdec_dxva2.c", "gl-win32" ),