d3d: add support for new libavcodec hwaccel API

Unfortunately quite a mess, in particular due to the need to have some
compatibility with the old API. (The old API will be supported only in
short term.)
This commit is contained in:
wm4 2017-06-08 21:16:11 +02:00
parent 79dc1834f5
commit 0754cbc83e
12 changed files with 399 additions and 13 deletions

View File

@ -19,6 +19,8 @@
#include <libavcodec/avcodec.h>
#include "config.h"
#include "lavc.h"
#include "common/common.h"
#include "common/av_common.h"
@ -29,6 +31,8 @@
#include "d3d.h"
#if !HAVE_D3D_HWACCEL_NEW
// define all the GUIDs used directly here, to avoid problems with inconsistent
// dxva2api.h versions in mingw-w64 and different MSVC version
#include <guiddef.h>
@ -100,6 +104,8 @@ static const struct d3dva_mode d3dva_modes[] = {
#undef MODE
#undef MODE2
#endif
HMODULE d3d11_dll, d3d9_dll, dxva2_dll;
static pthread_once_t d3d_load_once = PTHREAD_ONCE_INIT;
@ -116,6 +122,21 @@ void d3d_load_dlls(void)
pthread_once(&d3d_load_once, d3d_do_load);
}
// Test if Direct3D11 can be used by us. Basically, this prevents trying to use
// D3D11 on Win7, and then failing somewhere in the process.
bool d3d11_check_decoding(ID3D11Device *dev)
{
HRESULT hr;
// We assume that NV12 is always supported, if hw decoding is supported at
// all.
UINT supported = 0;
hr = ID3D11Device_CheckFormatSupport(dev, DXGI_FORMAT_NV12, &supported);
return !FAILED(hr) && (supported & D3D11_BIND_DECODER);
}
#if !HAVE_D3D_HWACCEL_NEW
int d3d_probe_codec(const char *codec)
{
enum AVCodecID codecid = mp_codec_to_av_codec_id(codec);
@ -269,18 +290,6 @@ void copy_nv12(struct mp_image *dest, uint8_t *src_bits,
mp_image_copy_gpu(dest, &buf);
}
// Test if Direct3D11 can be used by us. Basically, this prevents trying to use
// D3D11 on Win7, and then failing somewhere in the process.
bool d3d11_check_decoding(ID3D11Device *dev)
{
HRESULT hr;
// We assume that NV12 is always supported, if hw decoding is supported at
// all.
UINT supported = 0;
hr = ID3D11Device_CheckFormatSupport(dev, DXGI_FORMAT_NV12, &supported);
return !FAILED(hr) && (supported & D3D11_BIND_DECODER);
}
static int get_dxgi_mpfmt(DWORD dxgi_fmt)
{
switch (dxgi_fmt) {
@ -360,3 +369,74 @@ done:
mp_image_unrefp(&sw_img);
return sw_img;
}
// Dummies for simpler compat.
AVBufferRef *d3d11_wrap_device_ref(ID3D11Device *device) { return NULL; }
AVBufferRef *d3d9_wrap_device_ref(struct IDirect3DDevice9 *device) { return NULL; }
#else /* !HAVE_D3D_HWACCEL_NEW */
#include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_dxva2.h>
#include <libavutil/hwcontext_d3d11va.h>
void d3d_hwframes_refine(struct lavc_ctx *ctx, AVBufferRef *hw_frames_ctx)
{
AVHWFramesContext *fctx = (void *)hw_frames_ctx->data;
int alignment = 16;
switch (ctx->avctx->codec_id) {
// decoding MPEG-2 requires additional alignment on some Intel GPUs, but it
// causes issues for H.264 on certain AMD GPUs.....
case AV_CODEC_ID_MPEG2VIDEO:
alignment = 32;
break;
// the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
// all coding features have enough room to work with
case AV_CODEC_ID_HEVC:
alignment = 128;
break;
}
fctx->width = FFALIGN(fctx->width, alignment);
fctx->height = FFALIGN(fctx->height, alignment);
if (fctx->format == AV_PIX_FMT_DXVA2_VLD) {
AVDXVA2FramesContext *hwctx = fctx->hwctx;
hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
}
if (fctx->format == AV_PIX_FMT_D3D11) {
AVD3D11VAFramesContext *hwctx = fctx->hwctx;
hwctx->BindFlags |= D3D11_BIND_DECODER | D3D11_BIND_SHADER_RESOURCE;
}
}
AVBufferRef *d3d11_wrap_device_ref(ID3D11Device *device)
{
AVBufferRef *device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_D3D11VA);
if (!device_ref)
return NULL;
AVHWDeviceContext *ctx = (void *)device_ref->data;
AVD3D11VADeviceContext *hwctx = ctx->hwctx;
ID3D11Device_AddRef(device);
hwctx->device = device;
if (av_hwdevice_ctx_init(device_ref) < 0)
av_buffer_unref(&device_ref);
return device_ref;
}
// Dummy for simpler compat.
struct mp_image *d3d11_download_image(struct mp_hwdec_ctx *ctx,
struct mp_image *mpi,
struct mp_image_pool *swpool)
{
return NULL;
}
#endif /* else !HAVE_D3D_HWACCEL_NEW */

View File

@ -71,4 +71,12 @@ struct mp_image *d3d11_download_image(struct mp_hwdec_ctx *ctx,
struct mp_image *mpi,
struct mp_image_pool *swpool);
struct AVBufferRef;
struct IDirect3DDevice9;
void d3d_hwframes_refine(struct lavc_ctx *ctx, struct AVBufferRef *hw_frames_ctx);
struct AVBufferRef *d3d11_wrap_device_ref(ID3D11Device *device);
struct AVBufferRef *d3d9_wrap_device_ref(struct IDirect3DDevice9 *device);
#endif

View File

@ -18,6 +18,8 @@
#include <libavcodec/d3d11va.h>
#include <libavutil/mem.h>
#include "config.h"
#include "lavc.h"
#include "common/common.h"
#include "common/av_common.h"
@ -28,6 +30,8 @@
#include "d3d.h"
#if !HAVE_D3D_HWACCEL_NEW
#define ADDITIONAL_SURFACES HWDEC_EXTRA_SURFACES
struct d3d11va_decoder {
@ -583,3 +587,104 @@ const struct vd_lavc_hwdec mp_vd_lavc_d3d11va_copy = {
.process_image = d3d11va_retrieve_image,
.delay_queue = HWDEC_DELAY_QUEUE_COUNT,
};
#else /* !HAVE_D3D_HWACCEL_NEW */
#include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_d3d11va.h>
static void d3d11_destroy_dev(struct mp_hwdec_ctx *ctx)
{
av_buffer_unref(&ctx->av_device_ref);
ID3D11Device_Release((ID3D11Device *)ctx->ctx);
talloc_free(ctx);
}
static struct mp_hwdec_ctx *d3d11_create_dev(struct mpv_global *global,
struct mp_log *plog, bool probing)
{
ID3D11Device *device = NULL;
HRESULT hr;
d3d_load_dlls();
if (!d3d11_dll) {
mp_err(plog, "Failed to load D3D11 library\n");
return NULL;
}
PFN_D3D11_CREATE_DEVICE CreateDevice =
(void *)GetProcAddress(d3d11_dll, "D3D11CreateDevice");
if (!CreateDevice) {
mp_err(plog, "Failed to get D3D11CreateDevice symbol from DLL: %s\n",
mp_LastError_to_str());
return NULL;
}
hr = CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
D3D11_CREATE_DEVICE_VIDEO_SUPPORT, NULL, 0,
D3D11_SDK_VERSION, &device, NULL, NULL);
if (FAILED(hr)) {
mp_err(plog, "Failed to create D3D11 Device: %s\n",
mp_HRESULT_to_str(hr));
return NULL;
}
struct mp_hwdec_ctx *ctx = talloc_ptrtype(NULL, ctx);
*ctx = (struct mp_hwdec_ctx) {
.type = HWDEC_D3D11VA_COPY,
.ctx = device,
.destroy = d3d11_destroy_dev,
.av_device_ref = d3d11_wrap_device_ref(device),
};
if (!ctx->av_device_ref) {
mp_err(plog, "Failed to allocate AVHWDeviceContext.\n");
d3d11_destroy_dev(ctx);
return NULL;
}
return ctx;
}
static struct mp_image *d3d11_update_image_attribs(struct lavc_ctx *s,
struct mp_image *img)
{
if (img->params.hw_subfmt == IMGFMT_NV12)
mp_image_setfmt(img, IMGFMT_D3D11NV12);
return img;
}
const struct vd_lavc_hwdec mp_vd_lavc_d3d11va = {
.type = HWDEC_D3D11VA,
.image_format = IMGFMT_D3D11VA,
.generic_hwaccel = true,
.set_hwframes = true,
.static_pool = true,
.hwframes_refine = d3d_hwframes_refine,
.process_image = d3d11_update_image_attribs,
.pixfmt_map = (const enum AVPixelFormat[][2]) {
{AV_PIX_FMT_YUV420P10, AV_PIX_FMT_P010},
{AV_PIX_FMT_YUV420P, AV_PIX_FMT_NV12},
{AV_PIX_FMT_NONE}
},
};
const struct vd_lavc_hwdec mp_vd_lavc_d3d11va_copy = {
.type = HWDEC_D3D11VA_COPY,
.copying = true,
.image_format = IMGFMT_D3D11VA,
.generic_hwaccel = true,
.create_dev = d3d11_create_dev,
.set_hwframes = true,
.static_pool = true,
.hwframes_refine = d3d_hwframes_refine,
.pixfmt_map = (const enum AVPixelFormat[][2]) {
{AV_PIX_FMT_YUV420P10, AV_PIX_FMT_P010},
{AV_PIX_FMT_YUV420P, AV_PIX_FMT_NV12},
{AV_PIX_FMT_NONE}
},
.delay_queue = HWDEC_DELAY_QUEUE_COUNT,
};
#endif /* else !HAVE_D3D_HWACCEL_NEW */

View File

@ -23,6 +23,8 @@
#include <libavcodec/dxva2.h>
#include <libavutil/common.h>
#include "config.h"
#include "lavc.h"
#include "common/common.h"
#include "common/av_common.h"
@ -33,6 +35,8 @@
#include "d3d.h"
#if !HAVE_D3D_HWACCEL_NEW
#define ADDITIONAL_SURFACES HWDEC_EXTRA_SURFACES
struct priv {
@ -550,3 +554,169 @@ const struct vd_lavc_hwdec mp_vd_lavc_dxva2_copy = {
.process_image = dxva2_retrieve_image,
.delay_queue = HWDEC_DELAY_QUEUE_COUNT,
};
#else /* !HAVE_D3D_HWACCEL_NEW */
#include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_dxva2.h>
static void d3d9_free_av_device_ref(AVHWDeviceContext *ctx)
{
AVDXVA2DeviceContext *hwctx = ctx->hwctx;
if (hwctx->devmgr)
IDirect3DDeviceManager9_Release(hwctx->devmgr);
}
AVBufferRef *d3d9_wrap_device_ref(IDirect3DDevice9 *device)
{
HRESULT hr;
d3d_load_dlls();
if (!dxva2_dll)
return NULL;
HRESULT (WINAPI *DXVA2CreateDirect3DDeviceManager9)(UINT *, IDirect3DDeviceManager9 **) =
(void *)GetProcAddress(dxva2_dll, "DXVA2CreateDirect3DDeviceManager9");
if (!DXVA2CreateDirect3DDeviceManager9)
return NULL;
AVBufferRef *device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DXVA2);
if (!device_ref)
return NULL;
AVHWDeviceContext *ctx = (void *)device_ref->data;
AVDXVA2DeviceContext *hwctx = ctx->hwctx;
UINT reset_token = 0;
hr = DXVA2CreateDirect3DDeviceManager9(&reset_token, &hwctx->devmgr);
if (FAILED(hr))
goto fail;
IDirect3DDeviceManager9_ResetDevice(hwctx->devmgr, device, reset_token);
if (FAILED(hr))
goto fail;
ctx->free = d3d9_free_av_device_ref;
if (av_hwdevice_ctx_init(device_ref) < 0)
goto fail;
return device_ref;
fail:
d3d9_free_av_device_ref(ctx);
av_buffer_unref(&device_ref);
return NULL;
}
static void d3d9_destroy_dev(struct mp_hwdec_ctx *ctx)
{
av_buffer_unref(&ctx->av_device_ref);
IDirect3DDevice9_Release((IDirect3DDevice9 *)ctx->ctx);
talloc_free(ctx);
}
static struct mp_hwdec_ctx *d3d9_create_dev(struct mpv_global *global,
struct mp_log *plog, bool probing)
{
d3d_load_dlls();
if (!d3d9_dll || !dxva2_dll) {
mp_err(plog, "Failed to load D3D9 library\n");
return NULL;
}
HRESULT (WINAPI *Direct3DCreate9Ex)(UINT, IDirect3D9Ex **) =
(void *)GetProcAddress(d3d9_dll, "Direct3DCreate9Ex");
if (!Direct3DCreate9Ex) {
mp_err(plog, "Failed to locate Direct3DCreate9Ex\n");
return NULL;
}
IDirect3D9Ex *d3d9ex = NULL;
HRESULT hr = Direct3DCreate9Ex(D3D_SDK_VERSION, &d3d9ex);
if (FAILED(hr)) {
mp_err(plog, "Failed to create IDirect3D9Ex object\n");
return NULL;
}
UINT adapter = D3DADAPTER_DEFAULT;
D3DDISPLAYMODEEX modeex = {0};
IDirect3D9Ex_GetAdapterDisplayModeEx(d3d9ex, adapter, &modeex, NULL);
D3DPRESENT_PARAMETERS present_params = {
.Windowed = TRUE,
.BackBufferWidth = 640,
.BackBufferHeight = 480,
.BackBufferCount = 0,
.BackBufferFormat = modeex.Format,
.SwapEffect = D3DSWAPEFFECT_DISCARD,
.Flags = D3DPRESENTFLAG_VIDEO,
};
IDirect3DDevice9Ex *exdev = NULL;
hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, adapter,
D3DDEVTYPE_HAL,
GetShellWindow(),
D3DCREATE_SOFTWARE_VERTEXPROCESSING |
D3DCREATE_MULTITHREADED |
D3DCREATE_FPU_PRESERVE,
&present_params,
NULL,
&exdev);
IDirect3D9_Release(d3d9ex);
if (FAILED(hr)) {
mp_err(plog, "Failed to create Direct3D device: %s\n",
mp_HRESULT_to_str(hr));
return NULL;
}
struct mp_hwdec_ctx *ctx = talloc_ptrtype(NULL, ctx);
*ctx = (struct mp_hwdec_ctx) {
.type = HWDEC_D3D11VA_COPY,
.ctx = exdev,
.destroy = d3d9_destroy_dev,
.av_device_ref = d3d9_wrap_device_ref((IDirect3DDevice9 *)exdev),
};
if (!ctx->av_device_ref) {
mp_err(plog, "Failed to allocate AVHWDeviceContext.\n");
d3d9_destroy_dev(ctx);
return NULL;
}
return ctx;
}
const struct vd_lavc_hwdec mp_vd_lavc_dxva2 = {
.type = HWDEC_DXVA2,
.image_format = IMGFMT_DXVA2,
.generic_hwaccel = true,
.set_hwframes = true,
.static_pool = true,
.hwframes_refine = d3d_hwframes_refine,
.pixfmt_map = (const enum AVPixelFormat[][2]) {
{AV_PIX_FMT_YUV420P10, AV_PIX_FMT_P010},
{AV_PIX_FMT_YUV420P, AV_PIX_FMT_NV12},
{AV_PIX_FMT_NONE}
},
};
const struct vd_lavc_hwdec mp_vd_lavc_dxva2_copy = {
.type = HWDEC_DXVA2_COPY,
.copying = true,
.image_format = IMGFMT_DXVA2,
.generic_hwaccel = true,
.create_dev = d3d9_create_dev,
.set_hwframes = true,
.static_pool = true,
.hwframes_refine = d3d_hwframes_refine,
.pixfmt_map = (const enum AVPixelFormat[][2]) {
{AV_PIX_FMT_YUV420P10, AV_PIX_FMT_P010},
{AV_PIX_FMT_YUV420P, AV_PIX_FMT_NV12},
{AV_PIX_FMT_NONE}
},
.delay_queue = HWDEC_DELAY_QUEUE_COUNT,
};
#endif /* else #if !HAVE_D3D_HWACCEL_NEW */

View File

@ -102,6 +102,8 @@ struct vd_lavc_hwdec {
// The returned device will be freed with mp_hwdec_ctx->destroy.
struct mp_hwdec_ctx *(*create_dev)(struct mpv_global *global,
struct mp_log *log, bool probing);
// Optional. Fill in special hwaccel- and codec-specific requirements.
void (*hwframes_refine)(struct lavc_ctx *ctx, AVBufferRef *hw_frames_ctx);
// Suffix for libavcodec decoder. If non-NULL, the codec is overridden
// with hwdec_find_decoder.
// Intuitively, this will force the corresponding wrapper decoder.

View File

@ -399,7 +399,7 @@ static int hwdec_probe(struct dec_video *vd, struct vd_lavc_hwdec *hwdec,
r = hwdec->probe(ctx, hwdec, codec);
if (hwdec->generic_hwaccel) {
assert(!hwdec->probe && !hwdec->init && !hwdec->init_decoder &&
!hwdec->uninit && !hwdec->allocate_image && !hwdec->process_image);
!hwdec->uninit && !hwdec->allocate_image);
struct mp_hwdec_ctx *dev = hwdec_create_dev(vd, hwdec, autoprobe);
if (!dev)
return hwdec->copying ? -1 : HWDEC_ERR_NO_CTX;
@ -779,6 +779,9 @@ int hwdec_setup_hw_frames_ctx(struct lavc_ctx *ctx, AVBufferRef *device_ctx,
fctx->initial_pool_size = initial_pool_size;
if (ctx->hwdec->hwframes_refine)
ctx->hwdec->hwframes_refine(ctx, ctx->cached_hw_frames_ctx);
int res = av_hwframe_ctx_init(ctx->cached_hw_frames_ctx);
if (res < 0) {
MP_ERR(ctx, "Failed to allocate hw frames.\n");

View File

@ -97,7 +97,12 @@ static const struct {
{IMGFMT_VAAPI, AV_PIX_FMT_VAAPI_VLD},
{IMGFMT_DXVA2, AV_PIX_FMT_DXVA2_VLD},
#if HAVE_D3D_HWACCEL
#if HAVE_D3D_HWACCEL_NEW
{IMGFMT_D3D11VA, AV_PIX_FMT_D3D11},
{IMGFMT_D3D11NV12, AV_PIX_FMT_D3D11},
#else
{IMGFMT_D3D11VA, AV_PIX_FMT_D3D11VA_VLD},
#endif
#endif
{IMGFMT_MMAL, AV_PIX_FMT_MMAL},
#if HAVE_CUDA_HWACCEL

View File

@ -195,6 +195,7 @@ static int create(struct gl_hwdec *hw)
.driver_name = hw->driver->name,
.ctx = p->d3d11_device,
.download_image = d3d11_download_image,
.av_device_ref = d3d11_wrap_device_ref(p->d3d11_device),
};
hwdec_devices_add(hw->devs, &p->hwctx);

View File

@ -164,6 +164,7 @@ static int create(struct gl_hwdec *hw)
.type = HWDEC_D3D11VA,
.driver_name = hw->driver->name,
.ctx = p->d3d11_device,
.av_device_ref = d3d11_wrap_device_ref(p->d3d11_device),
};
hwdec_devices_add(hw->devs, &p->hwctx);

View File

@ -211,6 +211,7 @@ static int create(struct gl_hwdec *hw)
.type = HWDEC_DXVA2,
.driver_name = hw->driver->name,
.ctx = (IDirect3DDevice9 *)p->device9ex,
.av_device_ref = d3d9_wrap_device_ref((IDirect3DDevice9 *)p->device9ex),
};
hwdec_devices_add(hw->devs, &p->hwctx);

View File

@ -22,6 +22,7 @@
#include "osdep/windows_utils.h"
#include "hwdec.h"
#include "video/hwdec.h"
#include "video/decode/d3d.h"
// for WGL_ACCESS_READ_ONLY_NV
#include <GL/wglext.h>
@ -104,6 +105,7 @@ static int create(struct gl_hwdec *hw)
.type = HWDEC_DXVA2,
.driver_name = hw->driver->name,
.ctx = (IDirect3DDevice9 *)p->device,
.av_device_ref = d3d9_wrap_device_ref((IDirect3DDevice9 *)p->device),
};
hwdec_devices_add(hw->devs, &p->hwctx);
return 0;

View File

@ -813,6 +813,14 @@ hwaccel_features = [
'desc': 'DXVA2 and D3D11VA hwaccel',
'deps': [ 'win32' ],
'func': check_true,
}, {
'name': '--d3d-hwaccel-new',
'desc': 'DXVA2 and D3D11VA hwaccel (new API)',
'func': check_statement('libavcodec/version.h',
'int x[(LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 4, 0) && '
' LIBAVCODEC_VERSION_MICRO < 100)'
' ? 1 : -1]',
use='libav'),
}, {
'name': '--cuda-hwaccel',
'desc': 'CUDA hwaccel',