1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-23 15:22:09 +00:00
mpv/video/decode/cuda.c
Philip Langdale 2048ad2b8a 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.
2016-09-08 16:06:12 +02:00

127 lines
3.3 KiB
C

/*
* This file is part of mpv.
*
* Copyright (c) 2016 Philip Langdale <philipl@overt.org>
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_cuda.h>
#include "common/av_common.h"
#include "video/decode/lavc.h"
typedef struct CUVIDContext {
CUcontext cuda_ctx;
} CUVIDContext;
static void cuvid_ctx_free(AVHWDeviceContext *ctx)
{
AVCUDADeviceContext *hwctx = ctx->hwctx;
cuCtxDestroy(hwctx->cuda_ctx);
}
static int probe(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec,
const char *codec)
{
if (!hwdec_devices_load(ctx->hwdec_devs, HWDEC_CUDA))
return HWDEC_ERR_NO_CTX;
return 0;
}
static int init(struct lavc_ctx *ctx)
{
struct CUVIDContext *p = talloc_ptrtype(NULL, p);
*p = (struct CUVIDContext) {
.cuda_ctx = hwdec_devices_get(ctx->hwdec_devs, HWDEC_CUDA)->ctx,
};
ctx->hwdec_priv = p;
return 0;
}
static int init_decoder(struct lavc_ctx *ctx, int w, int h)
{
AVCodecContext *avctx = ctx->avctx;
AVCUDADeviceContext *device_hwctx;
AVHWDeviceContext *device_ctx;
CUVIDContext *priv = ctx->hwdec_priv;
int ret = 0;
if (avctx->hw_frames_ctx) {
MP_ERR(ctx, "hw_frames_ctx already initialised!\n");
return -1;
}
AVBufferRef *hw_device_ctx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
if (!hw_device_ctx) {
MP_WARN(ctx, "av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA) failed\n");
goto error;
}
device_ctx = (AVHWDeviceContext*)hw_device_ctx->data;
device_ctx->free = cuvid_ctx_free;
device_hwctx = device_ctx->hwctx;
device_hwctx->cuda_ctx = priv->cuda_ctx;
ret = av_hwdevice_ctx_init(hw_device_ctx);
if (ret < 0) {
MP_ERR(ctx, "av_hwdevice_ctx_init failed\n");
goto error;
}
avctx->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
if (!avctx->hw_frames_ctx) {
MP_ERR(ctx, "av_hwframe_ctx_alloc failed\n");
goto error;
}
return 0;
error:
av_buffer_unref(&avctx->hw_frames_ctx);
av_buffer_unref(&hw_device_ctx);
return -1;
}
static void uninit(struct lavc_ctx *ctx)
{
struct CUVIDContext *p = ctx->hwdec_priv;
if (!p)
return;
talloc_free(p);
ctx->hwdec_priv = NULL;
}
static struct mp_image *process_image(struct lavc_ctx *ctx, struct mp_image *img)
{
if (img->imgfmt == IMGFMT_CUDA)
img->params.hw_subfmt = IMGFMT_NV12;
return img;
}
const struct vd_lavc_hwdec mp_vd_lavc_cuda = {
.type = HWDEC_CUDA,
.image_format = IMGFMT_CUDA,
.lavc_suffix = "_cuvid",
.probe = probe,
.init = init,
.uninit = uninit,
.init_decoder = init_decoder,
.process_image = process_image,
};