1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-28 10:02:17 +00:00
mpv/video/img_format.h
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

256 lines
8.8 KiB
C

/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MPLAYER_IMG_FORMAT_H
#define MPLAYER_IMG_FORMAT_H
#include <inttypes.h>
#include "osdep/endian.h"
#include "misc/bstr.h"
#if BYTE_ORDER == BIG_ENDIAN
#define MP_SELECT_LE_BE(LE, BE) BE
#else
#define MP_SELECT_LE_BE(LE, BE) LE
#endif
#define MP_MAX_PLANES 4
// All pixels start in byte boundaries
#define MP_IMGFLAG_BYTE_ALIGNED 0x1
// set if (possibly) alpha is included (might be not definitive for packed RGB)
#define MP_IMGFLAG_ALPHA 0x80
// Uses one component per plane (set even if it's just one plane)
#define MP_IMGFLAG_PLANAR 0x100
// set if it's YUV colorspace
#define MP_IMGFLAG_YUV 0x200
// set if it's RGB colorspace
#define MP_IMGFLAG_RGB 0x400
// set if it's XYZ colorspace
#define MP_IMGFLAG_XYZ 0x800
// set if the format is in a standard YUV format:
// - planar and yuv colorspace
// - chroma shift 0-2
// - 1-4 planes (1: gray, 2: gray/alpha, 3: yuv, 4: yuv/alpha)
// - 8-16 bit per pixel/plane, all planes have same depth,
// each plane has exactly one component
#define MP_IMGFLAG_YUV_P 0x1000
// set if in little endian, or endian independent
#define MP_IMGFLAG_LE 0x2000
// set if in big endian, or endian independent
#define MP_IMGFLAG_BE 0x4000
// set if in native (host) endian, or endian independent
#define MP_IMGFLAG_NE MP_SELECT_LE_BE(MP_IMGFLAG_LE, MP_IMGFLAG_BE)
// Carries a palette in plane[1] (see IMGFMT_PAL8 for format of the palette).
// Note that some non-paletted formats have this flag set, because FFmpeg
// mysteriously expects some formats to carry a palette plane for no apparent
// reason. FFmpeg developer braindeath?
// The only real paletted format we support is IMGFMT_PAL8, so check for that
// format directly if you want an actual paletted format.
#define MP_IMGFLAG_PAL 0x8000
// planes don't contain real data; planes[3] contains an API-specific pointer
#define MP_IMGFLAG_HWACCEL 0x10000
// Set if the chroma resolution is lower than luma resolution. Unset for non-YUV.
#define MP_IMGFLAG_SUBSAMPLED 0x20000
// Like MP_IMGFLAG_YUV_P, but RGB. This can be e.g. AV_PIX_FMT_GBRP. The planes
// are always shuffled (G - B - R [- A]).
#define MP_IMGFLAG_RGB_P 0x40000
// Semi-planar YUV formats, like AV_PIX_FMT_NV12.
// The flag MP_IMGFLAG_YUV_NV_SWAP is set for AV_PIX_FMT_NV21.
#define MP_IMGFLAG_YUV_NV 0x80000
#define MP_IMGFLAG_YUV_NV_SWAP 0x100000
// Exactly one of these bits is set in mp_imgfmt_desc.flags
#define MP_IMGFLAG_COLOR_CLASS_MASK \
(MP_IMGFLAG_YUV | MP_IMGFLAG_RGB | MP_IMGFLAG_XYZ)
struct mp_imgfmt_desc {
int id; // IMGFMT_*
int avformat; // AV_PIX_FMT_* (or AV_PIX_FMT_NONE)
int flags; // MP_IMGFLAG_* bitfield
int8_t num_planes;
int8_t chroma_xs, chroma_ys; // chroma shift (i.e. log2 of chroma pixel size)
int8_t align_x, align_y; // pixel size to get byte alignment and to get
// to a pixel pos where luma & chroma aligns
int8_t bytes[MP_MAX_PLANES]; // bytes per pixel (MP_IMGFLAG_BYTE_ALIGNED)
int8_t bpp[MP_MAX_PLANES]; // bits per pixel
int8_t plane_bits; // number of bits in use for plane 0
int8_t component_bits; // number of bits per component (0 if uneven)
int8_t component_full_bits; // number of bits per component including
// internal padding (0 if uneven)
int8_t components[MP_MAX_PLANES]; // number of components for each plane
// chroma shifts per plane (provided for convenience with planar formats)
int8_t xs[MP_MAX_PLANES];
int8_t ys[MP_MAX_PLANES];
};
struct mp_imgfmt_desc mp_imgfmt_get_desc(int imgfmt);
enum mp_imgfmt {
IMGFMT_NONE = 0,
// Offset to make confusing with ffmpeg formats harder
IMGFMT_START = 1000,
// Planar YUV formats
IMGFMT_444P, // 1x1
IMGFMT_422P, // 2x1
IMGFMT_440P, // 1x2
IMGFMT_420P, // 2x2
IMGFMT_411P, // 4x1
IMGFMT_410P, // 4x4
// YUV formats with 2 bytes per plane-pixel. Formats with 9-15 bits pad the
// most significant bits with 0 (use shifts to expand them to 16 bits).
IMGFMT_444P16,
IMGFMT_444P14,
IMGFMT_444P12,
IMGFMT_444P10,
IMGFMT_444P9,
IMGFMT_422P16,
IMGFMT_422P14,
IMGFMT_422P12,
IMGFMT_422P10,
IMGFMT_422P9,
IMGFMT_420P16,
IMGFMT_420P14,
IMGFMT_420P12,
IMGFMT_420P10,
IMGFMT_420P9,
// Planar YUV with alpha (4th plane)
IMGFMT_444AP,
IMGFMT_422AP,
IMGFMT_420AP,
// Gray
IMGFMT_Y8,
IMGFMT_Y16,
// Gray with alpha (packed)
IMGFMT_YA8,
IMGFMT_YA16,
// Packed YUV formats (components are byte-accessed)
IMGFMT_YUYV, // Y0 U Y1 V
IMGFMT_UYVY, // U Y0 V Y1
// Y plane + packed plane for chroma
IMGFMT_NV12,
IMGFMT_NV21,
// Like IMGFMT_NV12, but with 16 bits per component
IMGFMT_P010,
// RGB/BGR Formats
// Byte accessed (low address to high address)
IMGFMT_ARGB,
IMGFMT_BGRA,
IMGFMT_ABGR,
IMGFMT_RGBA,
IMGFMT_BGR24, // 3 bytes per pixel
IMGFMT_RGB24,
IMGFMT_RGB48, // 6 bytes per pixel, uint16_t channels
IMGFMT_RGBA64, // 8 bytes per pixel, uint16_t channels
IMGFMT_BGRA64,
// Like e.g. IMGFMT_ARGB, but has a padding byte instead of alpha
IMGFMT_0RGB,
IMGFMT_BGR0,
IMGFMT_0BGR,
IMGFMT_RGB0,
IMGFMT_RGB0_START = IMGFMT_0RGB,
IMGFMT_RGB0_END = IMGFMT_RGB0,
// Accessed with bit-shifts after endian-swapping the uint16_t pixel
IMGFMT_RGB565, // 5r 6g 5b (MSB to LSB)
// The first plane has 1 byte per pixel. The second plane is a palette with
// 256 entries, with each entry encoded like in IMGFMT_BGR32.
IMGFMT_PAL8,
// XYZ colorspace, similar organization to RGB48. Even though it says "12",
// the components are stored as 16 bit, with lower 4 bits set to 0.
IMGFMT_XYZ12,
// Hardware accelerated formats. Plane data points to special data
// structures, instead of pixel data.
IMGFMT_VDPAU, // VdpVideoSurface
IMGFMT_VDPAU_OUTPUT, // VdpOutputSurface
IMGFMT_VAAPI,
// NV12/P010/P016
// plane 1: ID3D11Texture2D
// plane 2: slice index casted to pointer
// plane 3: ID3D11VideoDecoderOutputView (can be absent in filters/VO)
IMGFMT_D3D11VA,
// Like IMGFMT_D3D11VA, but format is restricted to NV12.
IMGFMT_D3D11NV12,
// Like IMGFMT_D3D11VA, but format is restricted to a certain RGB format.
// Also, it must have a share handle, have been flushed, and not be a
// texture array slice.
IMGFMT_D3D11RGB,
IMGFMT_DXVA2, // IDirect3DSurface9 (NV12/P010/P016)
IMGFMT_MMAL, // MMAL_BUFFER_HEADER_T
IMGFMT_VIDEOTOOLBOX, // CVPixelBufferRef
IMGFMT_CUDA, // CUDA Buffer
// Generic pass-through of AV_PIX_FMT_*. Used for formats which don't have
// a corresponding IMGFMT_ value.
IMGFMT_AVPIXFMT_START,
IMGFMT_AVPIXFMT_END = IMGFMT_AVPIXFMT_START + 500,
IMGFMT_END,
// Redundant format aliases for native endian access
// The IMGFMT_RGB32 and IMGFMT_BGR32 formats provide bit-shift access to
// normally byte-accessed formats:
// IMGFMT_RGB32 = r | (g << 8) | (b << 16) | (a << 24)
// IMGFMT_BGR32 = b | (g << 8) | (r << 16) | (a << 24)
IMGFMT_RGB32 = MP_SELECT_LE_BE(IMGFMT_RGBA, IMGFMT_ABGR),
IMGFMT_BGR32 = MP_SELECT_LE_BE(IMGFMT_BGRA, IMGFMT_ARGB),
};
static inline bool IMGFMT_IS_RGB(int fmt)
{
struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(fmt);
return (desc.flags & MP_IMGFLAG_RGB) && desc.num_planes == 1;
}
#define IMGFMT_RGB_DEPTH(fmt) (mp_imgfmt_get_desc(fmt).plane_bits)
#define IMGFMT_IS_HWACCEL(fmt) (mp_imgfmt_get_desc(fmt).flags & MP_IMGFLAG_HWACCEL)
int mp_imgfmt_from_name(bstr name, bool allow_hwaccel);
char *mp_imgfmt_to_name_buf(char *buf, size_t buf_size, int fmt);
#define mp_imgfmt_to_name(fmt) mp_imgfmt_to_name_buf((char[16]){0}, 16, (fmt))
char **mp_imgfmt_name_list(void);
#define vo_format_name mp_imgfmt_to_name
int mp_imgfmt_find(int xs, int ys, int planes, int component_bits, int flags);
int mp_imgfmt_select_best(int dst1, int dst2, int src);
#endif /* MPLAYER_IMG_FORMAT_H */