directx_va: do not load the DLL if it's already loaded
[vlc.git] / modules / codec / avcodec / d3d11va.c
blob8e8afe9858085d665eed4c215dae0a283fd8fd6a
1 /*****************************************************************************
2 * d3d11va.c: Direct3D11 Video Acceleration decoder
3 *****************************************************************************
4 * Copyright © 2009 Geoffroy Couprie
5 * Copyright © 2009 Laurent Aimar
6 * Copyright © 2015 Steve Lhomme
7 * Copyright © 2015 VideoLabs
9 * Authors: Geoffroy Couprie <geal@videolan.org>
10 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
11 * Steve Lhomme <robux4@gmail.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /**
29 * See https://msdn.microsoft.com/en-us/library/windows/desktop/hh162912%28v=vs.85%29.aspx
30 **/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 # undef WINAPI_FAMILY
36 # define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
38 #include <assert.h>
40 #include <vlc_common.h>
41 #include <vlc_picture.h>
42 #include <vlc_plugin.h>
43 #include <vlc_charset.h>
44 #include <vlc_codec.h>
46 #define COBJMACROS
47 #include <initguid.h>
48 #include <d3d11.h>
49 #include <libavcodec/d3d11va.h>
51 #include "../../video_chroma/d3d11_fmt.h"
53 #define D3D_Device ID3D11Device
54 #define D3D_DecoderType ID3D11VideoDecoder
55 #define D3D_DecoderDevice ID3D11VideoDevice
56 #define D3D_DecoderSurface ID3D11VideoDecoderOutputView
57 #include "directx_va.h"
59 static int Open(vlc_va_t *, AVCodecContext *, enum PixelFormat,
60 const es_format_t *, picture_sys_t *p_sys);
61 static void Close(vlc_va_t *, void **);
63 vlc_module_begin()
64 set_description(N_("Direct3D11 Video Acceleration"))
65 set_capability("hw decoder", 110)
66 set_category(CAT_INPUT)
67 set_subcategory(SUBCAT_INPUT_VCODEC)
68 set_callbacks(Open, Close)
69 vlc_module_end()
72 * In this mode libavcodec doesn't need the whole array on texture on startup
73 * So we get the surfaces from the decoder pool when needed. We don't need to
74 * extract the decoded surface into the decoder picture anymore.
76 #define D3D11_DIRECT_DECODE LIBAVCODEC_VERSION_CHECK( 57, 30, 3, 72, 101 )
78 #include <initguid.h> /* must be last included to not redefine existing GUIDs */
80 /* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
81 * assume that they are declared in dxva2api.h */
82 #define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
84 #ifdef __MINGW32__
85 # include <_mingw.h>
87 # if !defined(__MINGW64_VERSION_MAJOR)
88 # undef MS_GUID
89 # define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
90 # define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
91 # else
92 # include <dxva.h>
93 # endif
95 #endif /* __MINGW32__ */
97 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
98 # include <dxgidebug.h>
99 #endif
101 DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo, 0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
103 DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
105 struct vlc_va_sys_t
107 directx_sys_t dx_sys;
108 UINT totalTextureSlices;
109 unsigned textureWidth;
110 unsigned textureHeight;
112 d3d11_handle_t hd3d;
113 d3d11_device_t d3d_dev;
115 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
116 HINSTANCE dxgidebug_dll;
117 #endif
119 /* Video service */
120 ID3D11VideoContext *d3dvidctx;
121 DXGI_FORMAT render;
123 HANDLE context_mutex;
125 /* pool */
126 picture_t *extern_pics[MAX_SURFACE_COUNT];
128 /* Video decoder */
129 D3D11_VIDEO_DECODER_CONFIG cfg;
131 /* avcodec internals */
132 struct AVD3D11VAContext hw;
134 ID3D11ShaderResourceView *resourceView[MAX_SURFACE_COUNT * D3D11_MAX_SHADER_VIEW];
137 /* */
138 static int D3dCreateDevice(vlc_va_t *);
139 static void D3dDestroyDevice(vlc_va_t *);
140 static char *DxDescribe(vlc_va_sys_t *);
142 static int DxCreateVideoService(vlc_va_t *);
143 static void DxDestroyVideoService(vlc_va_t *);
144 static int DxGetInputList(vlc_va_t *, input_list_t *);
145 static int DxSetupOutput(vlc_va_t *, const GUID *, const video_format_t *);
147 static int DxCreateDecoderSurfaces(vlc_va_t *, int codec_id,
148 const video_format_t *fmt, unsigned surface_count);
149 static void DxDestroySurfaces(vlc_va_t *);
150 static void SetupAVCodecContext(vlc_va_t *);
152 void SetupAVCodecContext(vlc_va_t *va)
154 vlc_va_sys_t *sys = va->sys;
155 directx_sys_t *dx_sys = &sys->dx_sys;
157 sys->hw.video_context = sys->d3dvidctx;
158 sys->hw.decoder = dx_sys->decoder;
159 sys->hw.cfg = &sys->cfg;
160 sys->hw.surface_count = dx_sys->va_pool.surface_count;
161 sys->hw.surface = dx_sys->hw_surface;
162 sys->hw.context_mutex = sys->context_mutex;
164 if (IsEqualGUID(&dx_sys->input, &DXVA_Intel_H264_NoFGT_ClearVideo))
165 sys->hw.workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
168 static void d3d11_pic_context_destroy(struct picture_context_t *opaque)
170 struct va_pic_context *pic_ctx = (struct va_pic_context*)opaque;
171 if (pic_ctx->va_surface)
172 va_surface_Release(pic_ctx->va_surface);
173 ReleasePictureSys(&pic_ctx->picsys);
174 free(pic_ctx);
177 static struct va_pic_context *CreatePicContext(ID3D11VideoDecoderOutputView *,
178 ID3D11Resource *,
179 ID3D11DeviceContext *,
180 UINT slice,
181 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW]);
183 static struct picture_context_t *d3d11_pic_context_copy(struct picture_context_t *ctx)
185 struct va_pic_context *src_ctx = (struct va_pic_context*)ctx;
186 struct va_pic_context *pic_ctx = CreatePicContext(src_ctx->picsys.decoder,
187 src_ctx->picsys.resource[0], src_ctx->picsys.context,
188 src_ctx->picsys.slice_index, src_ctx->picsys.resourceView);
189 if (unlikely(pic_ctx==NULL))
190 return NULL;
191 if (src_ctx->va_surface) {
192 pic_ctx->va_surface = src_ctx->va_surface;
193 va_surface_AddRef(pic_ctx->va_surface);
195 return &pic_ctx->s;
198 static struct va_pic_context *CreatePicContext(
199 ID3D11VideoDecoderOutputView *decoderSurface,
200 ID3D11Resource *p_resource,
201 ID3D11DeviceContext *context,
202 UINT slice,
203 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW])
205 struct va_pic_context *pic_ctx = calloc(1, sizeof(*pic_ctx));
206 if (unlikely(pic_ctx==NULL))
207 goto done;
208 pic_ctx->s.destroy = d3d11_pic_context_destroy;
209 pic_ctx->s.copy = d3d11_pic_context_copy;
211 D3D11_TEXTURE2D_DESC txDesc;
212 ID3D11Texture2D_GetDesc((ID3D11Texture2D*)p_resource, &txDesc);
214 pic_ctx->picsys.formatTexture = txDesc.Format;
215 pic_ctx->picsys.context = context;
216 pic_ctx->picsys.slice_index = slice;
217 pic_ctx->picsys.decoder = decoderSurface;
218 for (int i=0;i<D3D11_MAX_SHADER_VIEW; i++)
220 pic_ctx->picsys.resource[i] = p_resource;
221 pic_ctx->picsys.resourceView[i] = resourceView[i];
223 AcquirePictureSys(&pic_ctx->picsys);
224 pic_ctx->picsys.context = context;
225 done:
226 return pic_ctx;
229 static struct va_pic_context* NewSurfacePicContext(vlc_va_t *va, int surface_index)
231 vlc_va_sys_t *sys = va->sys;
232 directx_sys_t *dx_sys = &sys->dx_sys;
233 ID3D11VideoDecoderOutputView *surface = dx_sys->hw_surface[surface_index];
234 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW];
235 ID3D11Resource *p_resource;
236 ID3D11VideoDecoderOutputView_GetResource(surface, &p_resource);
238 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
239 ID3D11VideoDecoderOutputView_GetDesc(surface, &viewDesc);
241 for (int i=0; i<D3D11_MAX_SHADER_VIEW; i++)
242 resourceView[i] = sys->resourceView[viewDesc.Texture2D.ArraySlice*D3D11_MAX_SHADER_VIEW + i];
244 struct va_pic_context *pic_ctx = CreatePicContext(
245 surface,
246 p_resource,
247 sys->d3d_dev.d3dcontext,
248 viewDesc.Texture2D.ArraySlice,
249 resourceView);
250 ID3D11Resource_Release(p_resource);
251 if (unlikely(pic_ctx==NULL))
252 return NULL;
253 /* all the resources are acquired during surfaces init, and a second time in
254 * CreatePicContext(), undo one of them otherwise we need an extra release
255 * when the pool is emptied */
256 ReleasePictureSys(&pic_ctx->picsys);
257 return pic_ctx;
260 static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
262 #if D3D11_DIRECT_DECODE
263 if (va->sys->dx_sys.can_extern_pool)
265 /* copy the original picture_sys_t in the va_pic_context */
266 if (!pic->context)
268 assert(pic->p_sys!=NULL);
269 if (!pic->p_sys->decoder)
271 HRESULT hr;
272 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
273 ZeroMemory(&viewDesc, sizeof(viewDesc));
274 viewDesc.DecodeProfile = va->sys->dx_sys.input;
275 viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
276 viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
278 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( va->sys->dx_sys.d3ddec,
279 pic->p_sys->resource[KNOWN_DXGI_INDEX],
280 &viewDesc,
281 &pic->p_sys->decoder );
282 if (FAILED(hr))
283 return VLC_EGENERIC;
286 pic->context = (picture_context_t*)CreatePicContext(
287 pic->p_sys->decoder,
288 pic->p_sys->resource[KNOWN_DXGI_INDEX],
289 va->sys->d3d_dev.d3dcontext,
290 pic->p_sys->slice_index,
291 pic->p_sys->resourceView );
292 if (pic->context == NULL)
293 return VLC_EGENERIC;
296 else
297 #endif
299 int res = va_pool_Get(&va->sys->dx_sys.va_pool, pic);
300 if (unlikely(res != VLC_SUCCESS))
301 return res;
303 *data = (uint8_t*)((struct va_pic_context *)pic->context)->picsys.decoder;
304 return VLC_SUCCESS;
307 static void Close(vlc_va_t *va, void **ctx)
309 vlc_va_sys_t *sys = va->sys;
311 (void) ctx;
313 directx_va_Close(va, &sys->dx_sys);
315 D3D11_Destroy( &sys->hd3d );
317 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
318 if (sys->dxgidebug_dll)
319 FreeLibrary(sys->dxgidebug_dll);
320 #endif
322 free((char *)va->description);
323 free(sys);
326 static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
327 const es_format_t *fmt, picture_sys_t *p_sys)
329 int err = VLC_EGENERIC;
330 directx_sys_t *dx_sys;
332 ctx->hwaccel_context = NULL;
334 if (pix_fmt != AV_PIX_FMT_D3D11VA_VLD)
335 return VLC_EGENERIC;
337 vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
338 if (unlikely(sys == NULL))
339 return VLC_ENOMEM;
341 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
342 sys->dxgidebug_dll = LoadLibrary(TEXT("DXGIDEBUG.DLL"));
343 #endif
345 dx_sys = &sys->dx_sys;
347 dx_sys->va_pool.pf_create_device = D3dCreateDevice;
348 dx_sys->va_pool.pf_destroy_device = D3dDestroyDevice;
349 dx_sys->va_pool.pf_create_video_service = DxCreateVideoService;
350 dx_sys->va_pool.pf_destroy_video_service = DxDestroyVideoService;
351 dx_sys->va_pool.pf_create_decoder_surfaces = DxCreateDecoderSurfaces;
352 dx_sys->va_pool.pf_destroy_surfaces = DxDestroySurfaces;
353 dx_sys->va_pool.pf_setup_avcodec_ctx = SetupAVCodecContext;
354 dx_sys->va_pool.pf_new_surface_context = NewSurfacePicContext;
355 dx_sys->pf_get_input_list = DxGetInputList;
356 dx_sys->pf_setup_output = DxSetupOutput;
358 va->sys = sys;
360 sys->d3d_dev.d3ddevice = NULL;
361 va->sys->render = DXGI_FORMAT_UNKNOWN;
362 if ( p_sys != NULL && p_sys->context != NULL ) {
363 void *d3dvidctx = NULL;
364 HRESULT hr = ID3D11DeviceContext_QueryInterface(p_sys->context, &IID_ID3D11VideoContext, &d3dvidctx);
365 if (FAILED(hr)) {
366 msg_Err(va, "Could not Query ID3D11VideoContext Interface from the picture. (hr=0x%lX)", hr);
367 } else {
368 ID3D11DeviceContext_GetDevice( p_sys->context, &sys->d3d_dev.d3ddevice );
369 HANDLE context_lock = INVALID_HANDLE_VALUE;
370 UINT dataSize = sizeof(context_lock);
371 hr = ID3D11Device_GetPrivateData(sys->d3d_dev.d3ddevice, &GUID_CONTEXT_MUTEX, &dataSize, &context_lock);
372 if (FAILED(hr))
373 msg_Warn(va, "No mutex found to lock the decoder");
374 sys->context_mutex = context_lock;
376 dx_sys->d3ddev = sys->d3d_dev.d3ddevice;
377 sys->d3d_dev.d3dcontext = p_sys->context;
378 sys->d3d_dev.owner = false;
379 sys->d3dvidctx = d3dvidctx;
381 assert(p_sys->texture[KNOWN_DXGI_INDEX] != NULL);
382 D3D11_TEXTURE2D_DESC dstDesc;
383 ID3D11Texture2D_GetDesc( p_sys->texture[KNOWN_DXGI_INDEX], &dstDesc);
384 sys->render = dstDesc.Format;
385 va->sys->textureWidth = dstDesc.Width;
386 va->sys->textureHeight = dstDesc.Height;
387 va->sys->totalTextureSlices = dstDesc.ArraySize;
390 if (!va->sys->textureWidth || !va->sys->textureHeight)
392 va->sys->textureWidth = fmt->video.i_width;
393 va->sys->textureHeight = fmt->video.i_height;
396 err = D3D11_Create( va, &sys->hd3d );
397 if (err != VLC_SUCCESS)
398 goto error;
400 err = directx_va_Open(va, &sys->dx_sys);
401 if (err!=VLC_SUCCESS)
402 goto error;
404 err = directx_va_Setup(va, &sys->dx_sys, ctx, fmt);
405 if (err != VLC_SUCCESS)
406 goto error;
408 ctx->hwaccel_context = &sys->hw;
410 /* TODO print the hardware name/vendor for debugging purposes */
411 va->description = DxDescribe(sys);
412 va->get = Get;
414 return VLC_SUCCESS;
416 error:
417 Close(va, NULL);
418 return err;
422 * It creates a Direct3D device usable for decoding
424 static int D3dCreateDevice(vlc_va_t *va)
426 vlc_va_sys_t *sys = va->sys;
427 directx_sys_t *dx_sys = &sys->dx_sys;
428 HRESULT hr;
430 if (sys->d3d_dev.d3ddevice && sys->d3d_dev.d3dcontext) {
431 msg_Dbg(va, "Reusing Direct3D11 device");
432 ID3D11DeviceContext_AddRef(sys->d3d_dev.d3dcontext);
433 return VLC_SUCCESS;
436 /* */
437 hr = D3D11_CreateDevice(va, &sys->hd3d, true, &sys->d3d_dev);
438 if (FAILED(hr)) {
439 msg_Err(va, "D3D11CreateDevice failed. (hr=0x%lX)", hr);
440 return VLC_EGENERIC;
442 dx_sys->d3ddev = sys->d3d_dev.d3ddevice;
444 void *d3dvidctx = NULL;
445 hr = ID3D11DeviceContext_QueryInterface(sys->d3d_dev.d3dcontext, &IID_ID3D11VideoContext, &d3dvidctx);
446 if (FAILED(hr)) {
447 msg_Err(va, "Could not Query ID3D11VideoContext Interface. (hr=0x%lX)", hr);
448 return VLC_EGENERIC;
450 sys->d3dvidctx = d3dvidctx;
452 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
453 HRESULT (WINAPI * pf_DXGIGetDebugInterface)(const GUID *riid, void **ppDebug);
454 if (sys->dxgidebug_dll) {
455 pf_DXGIGetDebugInterface = (void *)GetProcAddress(sys->dxgidebug_dll, "DXGIGetDebugInterface");
456 if (pf_DXGIGetDebugInterface) {
457 IDXGIDebug *pDXGIDebug = NULL;
458 hr = pf_DXGIGetDebugInterface(&IID_IDXGIDebug, (void**)&pDXGIDebug);
459 if (SUCCEEDED(hr) && pDXGIDebug) {
460 hr = IDXGIDebug_ReportLiveObjects(pDXGIDebug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
464 #endif
466 return VLC_SUCCESS;
470 * It releases a Direct3D device and its resources.
472 static void D3dDestroyDevice(vlc_va_t *va)
474 vlc_va_sys_t *sys = va->sys;
475 if (sys->d3dvidctx)
476 ID3D11VideoContext_Release(sys->d3dvidctx);
477 D3D11_ReleaseDevice( &sys->d3d_dev );
480 * It describes our Direct3D object
482 static char *DxDescribe(vlc_va_sys_t *sys)
484 static const struct {
485 unsigned id;
486 char name[32];
487 } vendors [] = {
488 { 0x1002, "ATI" },
489 { 0x10DE, "NVIDIA" },
490 { 0x1106, "VIA" },
491 { 0x8086, "Intel" },
492 { 0x5333, "S3 Graphics" },
493 { 0x4D4F4351, "Qualcomm" },
494 { 0, "" }
497 IDXGIAdapter *p_adapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
498 if (!p_adapter) {
499 return NULL;
502 char *description = NULL;
503 DXGI_ADAPTER_DESC adapterDesc;
504 if (SUCCEEDED(IDXGIAdapter_GetDesc(p_adapter, &adapterDesc))) {
505 const char *vendor = "Unknown";
506 for (int i = 0; vendors[i].id != 0; i++) {
507 if (vendors[i].id == adapterDesc.VendorId) {
508 vendor = vendors[i].name;
509 break;
513 char *utfdesc = FromWide(adapterDesc.Description);
514 if (likely(utfdesc!=NULL))
516 if (asprintf(&description, "D3D11VA (%s, vendor %u(%s), device %u, revision %u)",
517 utfdesc,
518 adapterDesc.VendorId, vendor, adapterDesc.DeviceId, adapterDesc.Revision) < 0)
519 description = NULL;
520 free(utfdesc);
524 IDXGIAdapter_Release(p_adapter);
525 return description;
529 * It creates a DirectX video service
531 static int DxCreateVideoService(vlc_va_t *va)
533 directx_sys_t *dx_sys = &va->sys->dx_sys;
535 void *d3dviddev = NULL;
536 HRESULT hr = ID3D11Device_QueryInterface(va->sys->d3d_dev.d3ddevice, &IID_ID3D11VideoDevice, &d3dviddev);
537 if (FAILED(hr)) {
538 msg_Err(va, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr);
539 return VLC_EGENERIC;
541 dx_sys->d3ddec = d3dviddev;
543 return VLC_SUCCESS;
547 * It destroys a DirectX video service
549 static void DxDestroyVideoService(vlc_va_t *va)
551 directx_sys_t *dx_sys = &va->sys->dx_sys;
552 if (dx_sys->d3ddec)
553 ID3D11VideoDevice_Release(dx_sys->d3ddec);
556 static void ReleaseInputList(input_list_t *p_list)
558 free(p_list->list);
561 static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
563 directx_sys_t *dx_sys = &va->sys->dx_sys;
564 HRESULT hr;
566 UINT input_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(dx_sys->d3ddec);
568 p_list->count = input_count;
569 p_list->list = calloc(input_count, sizeof(*p_list->list));
570 if (unlikely(p_list->list == NULL)) {
571 return VLC_ENOMEM;
573 p_list->pf_release = ReleaseInputList;
575 for (unsigned i = 0; i < input_count; i++) {
576 hr = ID3D11VideoDevice_GetVideoDecoderProfile(dx_sys->d3ddec, i, &p_list->list[i]);
577 if (FAILED(hr))
579 msg_Err(va, "GetVideoDecoderProfile %d failed. (hr=0x%lX)", i, hr);
580 ReleaseInputList(p_list);
581 return VLC_EGENERIC;
585 return VLC_SUCCESS;
588 static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
590 vlc_va_sys_t *sys = va->sys;
591 directx_sys_t *dx_sys = &sys->dx_sys;
592 HRESULT hr;
594 #ifndef NDEBUG
595 BOOL bSupported = false;
596 for (int format = 0; format < 188; format++) {
597 hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, format, &bSupported);
598 if (SUCCEEDED(hr) && bSupported)
599 msg_Dbg(va, "format %s is supported for output", DxgiFormatToStr(format));
601 #endif
603 DXGI_FORMAT processorInput[4];
604 int idx = 0;
605 if ( sys->render != DXGI_FORMAT_UNKNOWN )
606 processorInput[idx++] = sys->render;
607 processorInput[idx++] = DXGI_FORMAT_NV12;
608 processorInput[idx++] = DXGI_FORMAT_420_OPAQUE;
609 processorInput[idx++] = DXGI_FORMAT_UNKNOWN;
611 char *psz_decoder_name = directx_va_GetDecoderName(input);
613 /* */
614 for (idx = 0; processorInput[idx] != DXGI_FORMAT_UNKNOWN; ++idx)
616 BOOL is_supported = false;
617 hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, processorInput[idx], &is_supported);
618 if (SUCCEEDED(hr) && is_supported)
619 msg_Dbg(va, "%s output is supported for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
620 else
622 msg_Dbg(va, "Can't get a decoder output format %s for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
623 continue;
626 // check if we can create render texture of that format
627 // check the decoder can output to that format
628 if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
629 D3D11_FORMAT_SUPPORT_SHADER_LOAD) )
631 #ifndef ID3D11VideoContext_VideoProcessorBlt
632 msg_Dbg(va, "Format %s needs a processor but is not supported",
633 DxgiFormatToStr(processorInput[idx]));
634 #else
635 if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
636 D3D11_FORMAT_SUPPORT_VIDEO_PROCESSOR_INPUT) )
638 msg_Dbg(va, "Format %s needs a processor but is not available",
639 DxgiFormatToStr(processorInput[idx]));
640 continue;
642 #endif
645 D3D11_VIDEO_DECODER_DESC decoderDesc;
646 ZeroMemory(&decoderDesc, sizeof(decoderDesc));
647 decoderDesc.Guid = *input;
648 decoderDesc.SampleWidth = fmt->i_width;
649 decoderDesc.SampleHeight = fmt->i_height;
650 decoderDesc.OutputFormat = processorInput[idx];
652 UINT cfg_count = 0;
653 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
654 if (FAILED(hr))
656 msg_Err( va, "Failed to get configuration for decoder %s. (hr=0x%lX)", psz_decoder_name, hr );
657 continue;
659 if (cfg_count == 0) {
660 msg_Err( va, "No decoder configuration possible for %s %dx%d",
661 DxgiFormatToStr(decoderDesc.OutputFormat),
662 decoderDesc.SampleWidth, decoderDesc.SampleHeight );
663 continue;
666 msg_Dbg(va, "Using output format %s for decoder %s", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
667 if ( sys->render == processorInput[idx] )
669 /* NVIDIA cards crash when calling CreateVideoDecoderOutputView
670 * on more than 30 slices */
671 if (sys->totalTextureSlices <= 30 || !isNvidiaHardware(sys->d3d_dev.d3ddevice))
672 dx_sys->can_extern_pool = true;
673 else
674 msg_Warn( va, "NVIDIA GPU with too many slices (%d) detected, use internal pool",
675 sys->totalTextureSlices );
677 sys->render = processorInput[idx];
678 free(psz_decoder_name);
679 return VLC_SUCCESS;
681 free(psz_decoder_name);
683 msg_Dbg(va, "Output format from picture source not supported.");
684 return VLC_EGENERIC;
687 static bool CanUseDecoderPadding(vlc_va_sys_t *sys)
689 IDXGIAdapter *pAdapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
690 if (!pAdapter)
691 return false;
693 DXGI_ADAPTER_DESC adapterDesc;
694 HRESULT hr = IDXGIAdapter_GetDesc(pAdapter, &adapterDesc);
695 IDXGIAdapter_Release(pAdapter);
696 if (FAILED(hr))
697 return false;
699 /* Qualcomm hardware has issues with textures and pixels that should not be
700 * part of the decoded area */
701 return adapterDesc.VendorId != 0x4D4F4351;
705 * It creates a Direct3D11 decoder using the given video format
707 static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id,
708 const video_format_t *fmt, unsigned surface_count)
710 vlc_va_sys_t *sys = va->sys;
711 directx_sys_t *dx_sys = &va->sys->dx_sys;
712 HRESULT hr;
714 ID3D10Multithread *pMultithread;
715 hr = ID3D11Device_QueryInterface( sys->d3d_dev.d3ddevice, &IID_ID3D10Multithread, (void **)&pMultithread);
716 if (SUCCEEDED(hr)) {
717 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
718 ID3D10Multithread_Release(pMultithread);
721 #if VLC_WINSTORE_APP
722 /* On the Xbox 1/S, any decoding of H264 with one dimension over 2304
723 * crashes totally the device */
724 if (codec_id == AV_CODEC_ID_H264 &&
725 (fmt->i_width > 2304 || fmt->i_height > 2304) &&
726 isXboxHardware(sys->d3d_dev.dev))
728 msg_Warn(va, "%dx%d resolution not supported by your hardware", fmt->i_width, fmt->i_height);
729 return VLC_EGENERIC;
731 #endif
732 if ((sys->textureWidth != fmt->i_width || sys->textureHeight != fmt->i_height) &&
733 !CanUseDecoderPadding(sys))
735 msg_Dbg(va, "mismatching external pool sizes use the internal one %dx%d vs %dx%d",
736 sys->textureWidth, sys->textureHeight, fmt->i_width, fmt->i_height);
737 dx_sys->can_extern_pool = false;
740 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
741 ZeroMemory(&viewDesc, sizeof(viewDesc));
742 viewDesc.DecodeProfile = dx_sys->input;
743 viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
745 const d3d_format_t *textureFmt = NULL;
746 for (const d3d_format_t *output_format = GetRenderFormatList();
747 output_format->name != NULL; ++output_format)
749 if (output_format->formatTexture == sys->render &&
750 (output_format->fourcc == VLC_CODEC_D3D11_OPAQUE || output_format->fourcc == VLC_CODEC_D3D11_OPAQUE_10B))
752 textureFmt = output_format;
753 break;
756 if (unlikely(textureFmt==NULL))
758 msg_Dbg(va, "no hardware decoder matching %s", DxgiFormatToStr(sys->render));
759 return VLC_EGENERIC;
762 if (dx_sys->can_extern_pool)
764 #if !D3D11_DIRECT_DECODE
765 size_t surface_idx;
766 for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
767 picture_t *pic = decoder_NewPicture( (decoder_t*) va->obj.parent );
768 sys->extern_pics[surface_idx] = pic;
769 dx_sys->hw_surface[surface_idx] = NULL;
770 if (pic==NULL)
772 msg_Warn(va, "not enough decoder pictures %d out of %d", surface_idx, surface_count);
773 dx_sys->can_extern_pool = false;
774 break;
777 D3D11_TEXTURE2D_DESC texDesc;
778 ID3D11Texture2D_GetDesc(pic->p_sys->texture[KNOWN_DXGI_INDEX], &texDesc);
779 if (texDesc.ArraySize < surface_count)
781 msg_Warn(va, "not enough decoding slices in the texture (%d/%d)",
782 texDesc.ArraySize, surface_count);
783 dx_sys->can_extern_pool = false;
784 break;
786 assert(texDesc.Format == sys->render);
787 assert(texDesc.BindFlags & D3D11_BIND_DECODER);
789 #if !LIBAVCODEC_VERSION_CHECK( 57, 27, 2, 61, 102 )
790 if (pic->p_sys->slice_index != surface_idx)
792 msg_Warn(va, "d3d11va requires decoding slices to be the first in the texture (%d/%d)",
793 pic->p_sys->slice_index, surface_idx);
794 dx_sys->can_extern_pool = false;
795 break;
797 #endif
799 viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
800 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
801 pic->p_sys->resource[KNOWN_DXGI_INDEX],
802 &viewDesc,
803 &pic->p_sys->decoder );
804 if (FAILED(hr)) {
805 msg_Warn(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
806 dx_sys->can_extern_pool = false;
807 break;
810 AllocateShaderView(VLC_OBJECT(va), sys->d3d_dev.d3ddevice, textureFmt, pic->p_sys->texture, pic->p_sys->slice_index, pic->p_sys->resourceView);
812 dx_sys->hw_surface[surface_idx] = pic->p_sys->decoder;
815 if (!dx_sys->can_extern_pool)
817 for (size_t i = 0; i < surface_idx; ++i)
819 if (dx_sys->hw_surface[i])
821 ID3D11VideoDecoderOutputView_Release(dx_sys->hw_surface[i]);
822 dx_sys->hw_surface[i] = NULL;
824 if (sys->extern_pics[i])
826 sys->extern_pics[i]->p_sys->decoder = NULL;
827 picture_Release(sys->extern_pics[i]);
828 sys->extern_pics[i] = NULL;
832 else
833 #endif
834 msg_Dbg(va, "using external surface pool");
837 if (!dx_sys->can_extern_pool)
839 D3D11_TEXTURE2D_DESC texDesc;
840 ZeroMemory(&texDesc, sizeof(texDesc));
841 texDesc.Width = sys->textureWidth;
842 texDesc.Height = sys->textureHeight;
843 texDesc.MipLevels = 1;
844 texDesc.Format = sys->render;
845 texDesc.SampleDesc.Count = 1;
846 texDesc.MiscFlags = 0;
847 texDesc.ArraySize = surface_count;
848 texDesc.Usage = D3D11_USAGE_DEFAULT;
849 texDesc.BindFlags = D3D11_BIND_DECODER;
850 texDesc.CPUAccessFlags = 0;
852 if (DeviceSupportsFormat(sys->d3d_dev.d3ddevice, texDesc.Format, D3D11_FORMAT_SUPPORT_SHADER_LOAD))
853 texDesc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
855 ID3D11Texture2D *p_texture;
856 hr = ID3D11Device_CreateTexture2D( sys->d3d_dev.d3ddevice, &texDesc, NULL, &p_texture );
857 if (FAILED(hr)) {
858 msg_Err(va, "CreateTexture2D %d failed. (hr=0x%0lx)", surface_count, hr);
859 return VLC_EGENERIC;
862 unsigned surface_idx;
863 for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
864 sys->extern_pics[surface_idx] = NULL;
865 viewDesc.Texture2D.ArraySlice = surface_idx;
867 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
868 (ID3D11Resource*) p_texture,
869 &viewDesc,
870 &dx_sys->hw_surface[surface_idx] );
871 if (FAILED(hr)) {
872 msg_Err(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
873 ID3D11Texture2D_Release(p_texture);
874 return VLC_EGENERIC;
877 if (texDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
879 ID3D11Texture2D *textures[D3D11_MAX_SHADER_VIEW] = {p_texture, p_texture};
880 AllocateShaderView(VLC_OBJECT(va), sys->d3d_dev.d3ddevice, textureFmt, textures, surface_idx,
881 &sys->resourceView[surface_idx * D3D11_MAX_SHADER_VIEW]);
885 msg_Dbg(va, "ID3D11VideoDecoderOutputView succeed with %d surfaces (%dx%d)",
886 surface_count, fmt->i_width, fmt->i_height);
888 D3D11_VIDEO_DECODER_DESC decoderDesc;
889 ZeroMemory(&decoderDesc, sizeof(decoderDesc));
890 decoderDesc.Guid = dx_sys->input;
891 decoderDesc.SampleWidth = fmt->i_width;
892 decoderDesc.SampleHeight = fmt->i_height;
893 decoderDesc.OutputFormat = sys->render;
895 UINT cfg_count;
896 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
897 if (FAILED(hr)) {
898 msg_Err(va, "GetVideoDecoderConfigCount failed. (hr=0x%lX)", hr);
899 return VLC_EGENERIC;
902 /* List all configurations available for the decoder */
903 D3D11_VIDEO_DECODER_CONFIG cfg_list[cfg_count];
904 for (unsigned i = 0; i < cfg_count; i++) {
905 hr = ID3D11VideoDevice_GetVideoDecoderConfig( dx_sys->d3ddec, &decoderDesc, i, &cfg_list[i] );
906 if (FAILED(hr)) {
907 msg_Err(va, "GetVideoDecoderConfig failed. (hr=0x%lX)", hr);
908 return VLC_EGENERIC;
912 msg_Dbg(va, "we got %d decoder configurations", cfg_count);
914 /* Select the best decoder configuration */
915 int cfg_score = 0;
916 for (unsigned i = 0; i < cfg_count; i++) {
917 const D3D11_VIDEO_DECODER_CONFIG *cfg = &cfg_list[i];
919 /* */
920 msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
921 i, cfg->ConfigBitstreamRaw);
923 /* */
924 int score;
925 if (cfg->ConfigBitstreamRaw == 1)
926 score = 1;
927 else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
928 score = 2;
929 else
930 continue;
931 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
932 score += 16;
934 if (cfg_score < score) {
935 sys->cfg = *cfg;
936 cfg_score = score;
939 if (cfg_score <= 0) {
940 msg_Err(va, "Failed to find a supported decoder configuration");
941 return VLC_EGENERIC;
944 /* Create the decoder */
945 ID3D11VideoDecoder *decoder;
946 hr = ID3D11VideoDevice_CreateVideoDecoder( dx_sys->d3ddec, &decoderDesc, &sys->cfg, &decoder );
947 if (FAILED(hr)) {
948 msg_Err(va, "ID3D11VideoDevice_CreateVideoDecoder failed. (hr=0x%lX)", hr);
949 dx_sys->decoder = NULL;
950 return VLC_EGENERIC;
952 dx_sys->decoder = decoder;
954 msg_Dbg(va, "DxCreateDecoderSurfaces succeed");
955 return VLC_SUCCESS;
958 static void DxDestroySurfaces(vlc_va_t *va)
960 directx_sys_t *dx_sys = &va->sys->dx_sys;
961 if (dx_sys->va_pool.surface_count && !dx_sys->can_extern_pool) {
962 ID3D11Resource *p_texture;
963 ID3D11VideoDecoderOutputView_GetResource( dx_sys->hw_surface[0], &p_texture );
964 ID3D11Resource_Release(p_texture);
965 ID3D11Resource_Release(p_texture);
967 for (unsigned i = 0; i < dx_sys->va_pool.surface_count; i++)
969 ID3D11VideoDecoderOutputView_Release( dx_sys->hw_surface[i] );
970 for (int j = 0; j < D3D11_MAX_SHADER_VIEW; j++)
972 if (va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j])
973 ID3D11ShaderResourceView_Release(va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j]);
976 if (dx_sys->decoder)
978 ID3D11VideoDecoder_Release(dx_sys->decoder);
979 dx_sys->decoder = NULL;