From 0754cbc83eb030ed3c0f0666e8b7c2481631e513 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 8 Jun 2017 21:16:11 +0200 Subject: 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.) --- video/decode/d3d.c | 104 ++++++++++++++++++++++++---- video/decode/d3d.h | 8 +++ video/decode/hw_d3d11va.c | 105 ++++++++++++++++++++++++++++ video/decode/hw_dxva2.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++ video/decode/lavc.h | 2 + video/decode/vd_lavc.c | 5 +- 6 files changed, 381 insertions(+), 13 deletions(-) (limited to 'video/decode') diff --git a/video/decode/d3d.c b/video/decode/d3d.c index abe8034b60..d81f481e7c 100644 --- a/video/decode/d3d.c +++ b/video/decode/d3d.c @@ -19,6 +19,8 @@ #include +#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 @@ -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 +#include +#include + +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 */ diff --git a/video/decode/d3d.h b/video/decode/d3d.h index 6caeb2dc03..334f12151e 100644 --- a/video/decode/d3d.h +++ b/video/decode/d3d.h @@ -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 diff --git a/video/decode/hw_d3d11va.c b/video/decode/hw_d3d11va.c index 78d81c2acb..c8255a36db 100644 --- a/video/decode/hw_d3d11va.c +++ b/video/decode/hw_d3d11va.c @@ -18,6 +18,8 @@ #include #include +#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 +#include + +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 */ diff --git a/video/decode/hw_dxva2.c b/video/decode/hw_dxva2.c index b2ba562cea..65b76c6b9a 100644 --- a/video/decode/hw_dxva2.c +++ b/video/decode/hw_dxva2.c @@ -23,6 +23,8 @@ #include #include +#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 +#include + +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 */ diff --git a/video/decode/lavc.h b/video/decode/lavc.h index cc098b998e..f58ee8bc1e 100644 --- a/video/decode/lavc.h +++ b/video/decode/lavc.h @@ -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. diff --git a/video/decode/vd_lavc.c b/video/decode/vd_lavc.c index 6dda6302f3..581f50979b 100644 --- a/video/decode/vd_lavc.c +++ b/video/decode/vd_lavc.c @@ -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"); -- cgit v1.2.3