input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / avcodec / d3d11va.c
blob7244c29d8855b14f99f3ea18d0d7cc547df468f0
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_DecoderType ID3D11VideoDecoder
54 #define D3D_DecoderDevice ID3D11VideoDevice
55 #define D3D_DecoderSurface ID3D11VideoDecoderOutputView
56 #include "directx_va.h"
58 static int Open(vlc_va_t *, AVCodecContext *, enum PixelFormat,
59 const es_format_t *, picture_sys_t *p_sys);
60 static void Close(vlc_va_t *, void **);
62 vlc_module_begin()
63 set_description(N_("Direct3D11 Video Acceleration"))
64 set_capability("hw decoder", 110)
65 set_category(CAT_INPUT)
66 set_subcategory(SUBCAT_INPUT_VCODEC)
67 set_callbacks(Open, Close)
68 vlc_module_end()
71 * In this mode libavcodec doesn't need the whole array on texture on startup
72 * So we get the surfaces from the decoder pool when needed. We don't need to
73 * extract the decoded surface into the decoder picture anymore.
75 #define D3D11_DIRECT_DECODE LIBAVCODEC_VERSION_CHECK( 57, 30, 3, 72, 101 )
77 #include <initguid.h> /* must be last included to not redefine existing GUIDs */
79 /* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
80 * assume that they are declared in dxva2api.h */
81 #define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
83 #ifdef __MINGW32__
84 # include <_mingw.h>
86 # if !defined(__MINGW64_VERSION_MAJOR)
87 # undef MS_GUID
88 # define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
89 # define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
90 # else
91 # include <dxva.h>
92 # endif
94 #endif /* __MINGW32__ */
96 DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo, 0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
98 DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
100 struct vlc_va_sys_t
102 directx_sys_t dx_sys;
103 UINT totalTextureSlices;
104 unsigned textureWidth;
105 unsigned textureHeight;
107 d3d11_handle_t hd3d;
108 d3d11_device_t d3d_dev;
110 /* Video service */
111 ID3D11VideoContext *d3dvidctx;
112 DXGI_FORMAT render;
114 /* pool */
115 picture_t *extern_pics[MAX_SURFACE_COUNT];
117 /* Video decoder */
118 D3D11_VIDEO_DECODER_CONFIG cfg;
120 /* avcodec internals */
121 struct AVD3D11VAContext hw;
123 ID3D11ShaderResourceView *resourceView[MAX_SURFACE_COUNT * D3D11_MAX_SHADER_VIEW];
126 /* */
127 static int D3dCreateDevice(vlc_va_t *);
128 static void D3dDestroyDevice(vlc_va_t *);
129 static char *DxDescribe(vlc_va_sys_t *);
131 static int DxCreateVideoService(vlc_va_t *);
132 static void DxDestroyVideoService(vlc_va_t *);
133 static int DxGetInputList(vlc_va_t *, input_list_t *);
134 static int DxSetupOutput(vlc_va_t *, const GUID *, const video_format_t *);
136 static int DxCreateDecoderSurfaces(vlc_va_t *, int codec_id,
137 const video_format_t *fmt, unsigned surface_count);
138 static void DxDestroySurfaces(vlc_va_t *);
139 static void SetupAVCodecContext(vlc_va_t *);
141 void SetupAVCodecContext(vlc_va_t *va)
143 vlc_va_sys_t *sys = va->sys;
144 directx_sys_t *dx_sys = &sys->dx_sys;
146 sys->hw.video_context = sys->d3dvidctx;
147 sys->hw.decoder = dx_sys->decoder;
148 sys->hw.cfg = &sys->cfg;
149 sys->hw.surface_count = dx_sys->va_pool.surface_count;
150 sys->hw.surface = dx_sys->hw_surface;
151 sys->hw.context_mutex = sys->d3d_dev.context_mutex;
153 if (IsEqualGUID(&dx_sys->input, &DXVA_Intel_H264_NoFGT_ClearVideo))
154 sys->hw.workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
157 static void d3d11_pic_context_destroy(struct picture_context_t *opaque)
159 struct va_pic_context *pic_ctx = (struct va_pic_context*)opaque;
160 if (pic_ctx->va_surface)
161 va_surface_Release(pic_ctx->va_surface);
162 ReleasePictureSys(&pic_ctx->picsys);
163 free(pic_ctx);
166 static struct va_pic_context *CreatePicContext(ID3D11VideoDecoderOutputView *,
167 ID3D11Resource *,
168 ID3D11DeviceContext *,
169 UINT slice,
170 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW]);
172 static struct picture_context_t *d3d11_pic_context_copy(struct picture_context_t *ctx)
174 struct va_pic_context *src_ctx = (struct va_pic_context*)ctx;
175 struct va_pic_context *pic_ctx = CreatePicContext(src_ctx->picsys.decoder,
176 src_ctx->picsys.resource[0], src_ctx->picsys.context,
177 src_ctx->picsys.slice_index, src_ctx->picsys.resourceView);
178 if (unlikely(pic_ctx==NULL))
179 return NULL;
180 if (src_ctx->va_surface) {
181 pic_ctx->va_surface = src_ctx->va_surface;
182 va_surface_AddRef(pic_ctx->va_surface);
184 return &pic_ctx->s;
187 static struct va_pic_context *CreatePicContext(
188 ID3D11VideoDecoderOutputView *decoderSurface,
189 ID3D11Resource *p_resource,
190 ID3D11DeviceContext *context,
191 UINT slice,
192 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW])
194 struct va_pic_context *pic_ctx = calloc(1, sizeof(*pic_ctx));
195 if (unlikely(pic_ctx==NULL))
196 goto done;
197 pic_ctx->s.destroy = d3d11_pic_context_destroy;
198 pic_ctx->s.copy = d3d11_pic_context_copy;
200 D3D11_TEXTURE2D_DESC txDesc;
201 ID3D11Texture2D_GetDesc((ID3D11Texture2D*)p_resource, &txDesc);
203 pic_ctx->picsys.formatTexture = txDesc.Format;
204 pic_ctx->picsys.context = context;
205 pic_ctx->picsys.slice_index = slice;
206 pic_ctx->picsys.decoder = decoderSurface;
207 for (int i=0;i<D3D11_MAX_SHADER_VIEW; i++)
209 pic_ctx->picsys.resource[i] = p_resource;
210 pic_ctx->picsys.resourceView[i] = resourceView[i];
212 AcquirePictureSys(&pic_ctx->picsys);
213 pic_ctx->picsys.context = context;
214 done:
215 return pic_ctx;
218 static struct va_pic_context* NewSurfacePicContext(vlc_va_t *va, int surface_index)
220 vlc_va_sys_t *sys = va->sys;
221 directx_sys_t *dx_sys = &sys->dx_sys;
222 ID3D11VideoDecoderOutputView *surface = dx_sys->hw_surface[surface_index];
223 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW];
224 ID3D11Resource *p_resource;
225 ID3D11VideoDecoderOutputView_GetResource(surface, &p_resource);
227 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
228 ID3D11VideoDecoderOutputView_GetDesc(surface, &viewDesc);
230 for (int i=0; i<D3D11_MAX_SHADER_VIEW; i++)
231 resourceView[i] = sys->resourceView[viewDesc.Texture2D.ArraySlice*D3D11_MAX_SHADER_VIEW + i];
233 struct va_pic_context *pic_ctx = CreatePicContext(
234 surface,
235 p_resource,
236 sys->d3d_dev.d3dcontext,
237 viewDesc.Texture2D.ArraySlice,
238 resourceView);
239 ID3D11Resource_Release(p_resource);
240 if (unlikely(pic_ctx==NULL))
241 return NULL;
242 /* all the resources are acquired during surfaces init, and a second time in
243 * CreatePicContext(), undo one of them otherwise we need an extra release
244 * when the pool is emptied */
245 ReleasePictureSys(&pic_ctx->picsys);
246 return pic_ctx;
249 static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
251 picture_sys_t *p_sys = pic->p_sys;
252 #if D3D11_DIRECT_DECODE
253 if (va->sys->dx_sys.can_extern_pool)
255 /* copy the original picture_sys_t in the va_pic_context */
256 if (!pic->context)
258 assert(p_sys!=NULL);
259 if (!p_sys->decoder)
261 HRESULT hr;
262 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
263 ZeroMemory(&viewDesc, sizeof(viewDesc));
264 viewDesc.DecodeProfile = va->sys->dx_sys.input;
265 viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
266 viewDesc.Texture2D.ArraySlice = p_sys->slice_index;
268 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( va->sys->dx_sys.d3ddec,
269 p_sys->resource[KNOWN_DXGI_INDEX],
270 &viewDesc,
271 &p_sys->decoder );
272 if (FAILED(hr))
273 return VLC_EGENERIC;
276 pic->context = (picture_context_t*)CreatePicContext(
277 p_sys->decoder,
278 p_sys->resource[KNOWN_DXGI_INDEX],
279 va->sys->d3d_dev.d3dcontext,
280 p_sys->slice_index,
281 p_sys->resourceView );
282 if (pic->context == NULL)
283 return VLC_EGENERIC;
286 else
287 #endif
289 int res = va_pool_Get(&va->sys->dx_sys.va_pool, pic);
290 if (unlikely(res != VLC_SUCCESS))
291 return res;
293 *data = (uint8_t*)((struct va_pic_context *)pic->context)->picsys.decoder;
294 return VLC_SUCCESS;
297 static void Close(vlc_va_t *va, void **ctx)
299 vlc_va_sys_t *sys = va->sys;
301 (void) ctx;
303 directx_va_Close(va, &sys->dx_sys);
305 D3D11_Destroy( &sys->hd3d );
307 free((char *)va->description);
308 free(sys);
311 static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
312 const es_format_t *fmt, picture_sys_t *p_sys)
314 int err = VLC_EGENERIC;
315 directx_sys_t *dx_sys;
317 ctx->hwaccel_context = NULL;
319 if (pix_fmt != AV_PIX_FMT_D3D11VA_VLD)
320 return VLC_EGENERIC;
322 vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
323 if (unlikely(sys == NULL))
324 return VLC_ENOMEM;
326 dx_sys = &sys->dx_sys;
328 dx_sys->va_pool.pf_create_device = D3dCreateDevice;
329 dx_sys->va_pool.pf_destroy_device = D3dDestroyDevice;
330 dx_sys->va_pool.pf_create_video_service = DxCreateVideoService;
331 dx_sys->va_pool.pf_destroy_video_service = DxDestroyVideoService;
332 dx_sys->va_pool.pf_create_decoder_surfaces = DxCreateDecoderSurfaces;
333 dx_sys->va_pool.pf_destroy_surfaces = DxDestroySurfaces;
334 dx_sys->va_pool.pf_setup_avcodec_ctx = SetupAVCodecContext;
335 dx_sys->va_pool.pf_new_surface_context = NewSurfacePicContext;
336 dx_sys->pf_get_input_list = DxGetInputList;
337 dx_sys->pf_setup_output = DxSetupOutput;
339 va->sys = sys;
341 sys->d3d_dev.d3ddevice = NULL;
342 va->sys->render = DXGI_FORMAT_UNKNOWN;
343 if ( p_sys != NULL && p_sys->context != NULL ) {
344 void *d3dvidctx = NULL;
345 HRESULT hr = ID3D11DeviceContext_QueryInterface(p_sys->context, &IID_ID3D11VideoContext, &d3dvidctx);
346 if (FAILED(hr)) {
347 msg_Err(va, "Could not Query ID3D11VideoContext Interface from the picture. (hr=0x%lX)", hr);
348 } else {
349 ID3D11DeviceContext_GetDevice( p_sys->context, &sys->d3d_dev.d3ddevice );
350 HANDLE context_lock = INVALID_HANDLE_VALUE;
351 UINT dataSize = sizeof(context_lock);
352 hr = ID3D11Device_GetPrivateData(sys->d3d_dev.d3ddevice, &GUID_CONTEXT_MUTEX, &dataSize, &context_lock);
353 if (FAILED(hr))
354 msg_Warn(va, "No mutex found to lock the decoder");
355 sys->d3d_dev.context_mutex = context_lock;
357 sys->d3d_dev.d3dcontext = p_sys->context;
358 sys->d3d_dev.owner = false;
359 D3D11_GetDriverVersion(va, &sys->d3d_dev);
360 sys->d3dvidctx = d3dvidctx;
362 assert(p_sys->texture[KNOWN_DXGI_INDEX] != NULL);
363 D3D11_TEXTURE2D_DESC dstDesc;
364 ID3D11Texture2D_GetDesc( p_sys->texture[KNOWN_DXGI_INDEX], &dstDesc);
365 sys->render = dstDesc.Format;
366 va->sys->textureWidth = dstDesc.Width;
367 va->sys->textureHeight = dstDesc.Height;
368 va->sys->totalTextureSlices = dstDesc.ArraySize;
372 err = D3D11_Create( va, &sys->hd3d, false );
373 if (err != VLC_SUCCESS)
374 goto error;
376 err = directx_va_Open(va, &sys->dx_sys);
377 if (err!=VLC_SUCCESS)
378 goto error;
380 err = directx_va_Setup(va, &sys->dx_sys, ctx, fmt, isXboxHardware(sys->d3d_dev.d3ddevice));
381 if (err != VLC_SUCCESS)
382 goto error;
384 ctx->hwaccel_context = &sys->hw;
386 /* TODO print the hardware name/vendor for debugging purposes */
387 va->description = DxDescribe(sys);
388 va->get = Get;
390 return VLC_SUCCESS;
392 error:
393 Close(va, NULL);
394 return err;
398 * It creates a Direct3D device usable for decoding
400 static int D3dCreateDevice(vlc_va_t *va)
402 vlc_va_sys_t *sys = va->sys;
403 HRESULT hr;
405 if (sys->d3d_dev.d3ddevice && sys->d3d_dev.d3dcontext) {
406 msg_Dbg(va, "Reusing Direct3D11 device");
407 ID3D11DeviceContext_AddRef(sys->d3d_dev.d3dcontext);
408 return VLC_SUCCESS;
411 #if VLC_WINSTORE_APP
412 sys->d3d_dev.d3dcontext = var_InheritInteger(va, "winrt-d3dcontext");
413 if (likely(sys->d3d_dev.d3dcontext))
415 ID3D11Device* d3ddevice = NULL;
416 ID3D11DeviceContext_GetDevice(sys->d3d_dev.d3dcontext, &sys->d3d_dev.d3ddevice);
417 ID3D11DeviceContext_AddRef(sys->d3d_dev.d3dcontext);
418 ID3D11Device_Release(sys->d3d_dev.d3ddevice);
420 #endif
422 /* */
423 if (!sys->d3d_dev.d3ddevice)
425 hr = D3D11_CreateDevice(va, &sys->hd3d, true, &sys->d3d_dev);
426 if (FAILED(hr)) {
427 msg_Err(va, "D3D11CreateDevice failed. (hr=0x%lX)", hr);
428 return VLC_EGENERIC;
432 void *d3dvidctx = NULL;
433 hr = ID3D11DeviceContext_QueryInterface(sys->d3d_dev.d3dcontext, &IID_ID3D11VideoContext, &d3dvidctx);
434 if (FAILED(hr)) {
435 msg_Err(va, "Could not Query ID3D11VideoContext Interface. (hr=0x%lX)", hr);
436 ID3D11DeviceContext_Release(sys->d3d_dev.d3dcontext);
437 ID3D11Device_Release(sys->d3d_dev.d3ddevice);
438 return VLC_EGENERIC;
440 sys->d3dvidctx = d3dvidctx;
442 return VLC_SUCCESS;
446 * It releases a Direct3D device and its resources.
448 static void D3dDestroyDevice(vlc_va_t *va)
450 vlc_va_sys_t *sys = va->sys;
451 if (sys->d3dvidctx)
452 ID3D11VideoContext_Release(sys->d3dvidctx);
453 D3D11_ReleaseDevice( &sys->d3d_dev );
457 * It describes our Direct3D object
459 static char *DxDescribe(vlc_va_sys_t *sys)
462 IDXGIAdapter *p_adapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
463 if (!p_adapter) {
464 return NULL;
467 char *description = NULL;
468 DXGI_ADAPTER_DESC adapterDesc;
469 if (SUCCEEDED(IDXGIAdapter_GetDesc(p_adapter, &adapterDesc))) {
470 char *utfdesc = FromWide(adapterDesc.Description);
471 if (likely(utfdesc!=NULL))
473 if (asprintf(&description, "D3D11VA (%s, vendor %x(%s), device %x, revision %x)",
474 utfdesc,
475 adapterDesc.VendorId, DxgiVendorStr(adapterDesc.VendorId), adapterDesc.DeviceId, adapterDesc.Revision) < 0)
476 description = NULL;
477 free(utfdesc);
481 IDXGIAdapter_Release(p_adapter);
482 return description;
486 * It creates a DirectX video service
488 static int DxCreateVideoService(vlc_va_t *va)
490 directx_sys_t *dx_sys = &va->sys->dx_sys;
492 void *d3dviddev = NULL;
493 HRESULT hr = ID3D11Device_QueryInterface(va->sys->d3d_dev.d3ddevice, &IID_ID3D11VideoDevice, &d3dviddev);
494 if (FAILED(hr)) {
495 msg_Err(va, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr);
496 return VLC_EGENERIC;
498 dx_sys->d3ddec = d3dviddev;
500 return VLC_SUCCESS;
504 * It destroys a DirectX video service
506 static void DxDestroyVideoService(vlc_va_t *va)
508 directx_sys_t *dx_sys = &va->sys->dx_sys;
509 if (dx_sys->d3ddec)
510 ID3D11VideoDevice_Release(dx_sys->d3ddec);
513 static void ReleaseInputList(input_list_t *p_list)
515 free(p_list->list);
518 static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
520 directx_sys_t *dx_sys = &va->sys->dx_sys;
521 HRESULT hr;
523 UINT input_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(dx_sys->d3ddec);
525 p_list->count = input_count;
526 p_list->list = calloc(input_count, sizeof(*p_list->list));
527 if (unlikely(p_list->list == NULL)) {
528 return VLC_ENOMEM;
530 p_list->pf_release = ReleaseInputList;
532 for (unsigned i = 0; i < input_count; i++) {
533 hr = ID3D11VideoDevice_GetVideoDecoderProfile(dx_sys->d3ddec, i, &p_list->list[i]);
534 if (FAILED(hr))
536 msg_Err(va, "GetVideoDecoderProfile %d failed. (hr=0x%lX)", i, hr);
537 ReleaseInputList(p_list);
538 return VLC_EGENERIC;
542 return VLC_SUCCESS;
545 extern const GUID DXVA_ModeHEVC_VLD_Main10;
546 extern const GUID DXVA_ModeVP9_VLD_10bit_Profile2;
548 static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
550 vlc_va_sys_t *sys = va->sys;
551 directx_sys_t *dx_sys = &sys->dx_sys;
552 HRESULT hr;
554 #ifndef NDEBUG
555 BOOL bSupported = false;
556 for (int format = 0; format < 188; format++) {
557 hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, format, &bSupported);
558 if (SUCCEEDED(hr) && bSupported)
559 msg_Dbg(va, "format %s is supported for output", DxgiFormatToStr(format));
561 #endif
563 IDXGIAdapter *pAdapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
564 if (!pAdapter)
565 return VLC_EGENERIC;
567 DXGI_ADAPTER_DESC adapterDesc;
568 hr = IDXGIAdapter_GetDesc(pAdapter, &adapterDesc);
569 IDXGIAdapter_Release(pAdapter);
570 if (FAILED(hr))
571 return VLC_EGENERIC;
573 if (!directx_va_canUseDecoder(va, adapterDesc.VendorId, adapterDesc.DeviceId,
574 input, sys->d3d_dev.WDDM.build))
576 msg_Warn(va, "GPU blacklisted for %s codec", directx_va_GetDecoderName(input));
577 return VLC_EGENERIC;
580 DXGI_FORMAT processorInput[5];
581 int idx = 0;
582 if ( sys->render != DXGI_FORMAT_UNKNOWN )
583 processorInput[idx++] = sys->render;
584 if (IsEqualGUID(input, &DXVA_ModeHEVC_VLD_Main10) || IsEqualGUID(input, &DXVA_ModeVP9_VLD_10bit_Profile2))
585 processorInput[idx++] = DXGI_FORMAT_P010;
586 processorInput[idx++] = DXGI_FORMAT_NV12;
587 processorInput[idx++] = DXGI_FORMAT_420_OPAQUE;
588 processorInput[idx++] = DXGI_FORMAT_UNKNOWN;
590 char *psz_decoder_name = directx_va_GetDecoderName(input);
592 /* */
593 for (idx = 0; processorInput[idx] != DXGI_FORMAT_UNKNOWN; ++idx)
595 BOOL is_supported = false;
596 hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, processorInput[idx], &is_supported);
597 if (SUCCEEDED(hr) && is_supported)
598 msg_Dbg(va, "%s output is supported for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
599 else
601 msg_Dbg(va, "Can't get a decoder output format %s for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
602 continue;
605 // check if we can create render texture of that format
606 // check the decoder can output to that format
607 if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
608 D3D11_FORMAT_SUPPORT_SHADER_LOAD) )
610 #ifndef ID3D11VideoContext_VideoProcessorBlt
611 msg_Dbg(va, "Format %s needs a processor but is not supported",
612 DxgiFormatToStr(processorInput[idx]));
613 #else
614 if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
615 D3D11_FORMAT_SUPPORT_VIDEO_PROCESSOR_INPUT) )
617 msg_Dbg(va, "Format %s needs a processor but is not available",
618 DxgiFormatToStr(processorInput[idx]));
619 continue;
621 #endif
624 D3D11_VIDEO_DECODER_DESC decoderDesc;
625 ZeroMemory(&decoderDesc, sizeof(decoderDesc));
626 decoderDesc.Guid = *input;
627 decoderDesc.SampleWidth = fmt->i_width;
628 decoderDesc.SampleHeight = fmt->i_height;
629 decoderDesc.OutputFormat = processorInput[idx];
631 UINT cfg_count = 0;
632 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
633 if (FAILED(hr))
635 msg_Err( va, "Failed to get configuration for decoder %s. (hr=0x%lX)", psz_decoder_name, hr );
636 continue;
638 if (cfg_count == 0) {
639 msg_Err( va, "No decoder configuration possible for %s %dx%d",
640 DxgiFormatToStr(decoderDesc.OutputFormat),
641 decoderDesc.SampleWidth, decoderDesc.SampleHeight );
642 continue;
645 msg_Dbg(va, "Using output format %s for decoder %s", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
646 if ( sys->render == processorInput[idx] )
648 if (CanUseVoutPool(&sys->d3d_dev, sys->totalTextureSlices))
649 dx_sys->can_extern_pool = true;
650 else
651 msg_Warn( va, "use internal pool" );
653 sys->render = processorInput[idx];
654 free(psz_decoder_name);
655 return VLC_SUCCESS;
657 free(psz_decoder_name);
659 msg_Dbg(va, "Output format from picture source not supported.");
660 return VLC_EGENERIC;
663 static bool CanUseDecoderPadding(vlc_va_sys_t *sys)
665 IDXGIAdapter *pAdapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
666 if (!pAdapter)
667 return false;
669 DXGI_ADAPTER_DESC adapterDesc;
670 HRESULT hr = IDXGIAdapter_GetDesc(pAdapter, &adapterDesc);
671 IDXGIAdapter_Release(pAdapter);
672 if (FAILED(hr))
673 return false;
675 /* Qualcomm hardware has issues with textures and pixels that should not be
676 * part of the decoded area */
677 return adapterDesc.VendorId != GPU_MANUFACTURER_QUALCOMM;
681 * It creates a Direct3D11 decoder using the given video format
683 static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id,
684 const video_format_t *fmt, unsigned surface_count)
686 vlc_va_sys_t *sys = va->sys;
687 directx_sys_t *dx_sys = &va->sys->dx_sys;
688 HRESULT hr;
690 ID3D10Multithread *pMultithread;
691 hr = ID3D11Device_QueryInterface( sys->d3d_dev.d3ddevice, &IID_ID3D10Multithread, (void **)&pMultithread);
692 if (SUCCEEDED(hr)) {
693 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
694 ID3D10Multithread_Release(pMultithread);
697 if (!sys->textureWidth || !sys->textureHeight)
699 sys->textureWidth = fmt->i_width;
700 sys->textureHeight = fmt->i_height;
703 assert(sys->textureWidth >= fmt->i_width);
704 assert(sys->textureHeight >= fmt->i_height);
706 if ((sys->textureWidth != fmt->i_width || sys->textureHeight != fmt->i_height) &&
707 !CanUseDecoderPadding(sys))
709 msg_Dbg(va, "mismatching external pool sizes use the internal one %dx%d vs %dx%d",
710 sys->textureWidth, sys->textureHeight, fmt->i_width, fmt->i_height);
711 dx_sys->can_extern_pool = false;
712 sys->textureWidth = fmt->i_width;
713 sys->textureHeight = fmt->i_height;
715 if (sys->totalTextureSlices && sys->totalTextureSlices < surface_count)
717 msg_Warn(va, "not enough decoding slices in the texture (%d/%d)",
718 sys->totalTextureSlices, surface_count);
719 dx_sys->can_extern_pool = false;
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 (sys->textureWidth > 2304 || sys->textureHeight > 2304) &&
726 isXboxHardware(sys->d3d_dev.d3ddevice))
728 msg_Warn(va, "%dx%d resolution not supported by your hardware", fmt->i_width, fmt->i_height);
729 return VLC_EGENERIC;
731 #endif
733 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
734 ZeroMemory(&viewDesc, sizeof(viewDesc));
735 viewDesc.DecodeProfile = dx_sys->input;
736 viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
738 const d3d_format_t *textureFmt = NULL;
739 for (const d3d_format_t *output_format = GetRenderFormatList();
740 output_format->name != NULL; ++output_format)
742 if (output_format->formatTexture == sys->render &&
743 is_d3d11_opaque(output_format->fourcc))
745 textureFmt = output_format;
746 break;
749 if (unlikely(textureFmt==NULL))
751 msg_Dbg(va, "no hardware decoder matching %s", DxgiFormatToStr(sys->render));
752 return VLC_EGENERIC;
755 if (dx_sys->can_extern_pool)
757 #if !D3D11_DIRECT_DECODE
758 size_t surface_idx;
759 for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
760 picture_t *pic = decoder_NewPicture( (decoder_t*) va->obj.parent );
761 sys->extern_pics[surface_idx] = pic;
762 dx_sys->hw_surface[surface_idx] = NULL;
763 if (pic==NULL)
765 msg_Warn(va, "not enough decoder pictures %d out of %d", surface_idx, surface_count);
766 dx_sys->can_extern_pool = false;
767 break;
770 D3D11_TEXTURE2D_DESC texDesc;
771 ID3D11Texture2D_GetDesc(pic->p_sys->texture[KNOWN_DXGI_INDEX], &texDesc);
772 assert(texDesc.Format == sys->render);
773 assert(texDesc.BindFlags & D3D11_BIND_DECODER);
775 #if !LIBAVCODEC_VERSION_CHECK( 57, 27, 2, 61, 102 )
776 if (pic->p_sys->slice_index != surface_idx)
778 msg_Warn(va, "d3d11va requires decoding slices to be the first in the texture (%d/%d)",
779 pic->p_sys->slice_index, surface_idx);
780 dx_sys->can_extern_pool = false;
781 break;
783 #endif
785 viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
786 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
787 pic->p_sys->resource[KNOWN_DXGI_INDEX],
788 &viewDesc,
789 &pic->p_sys->decoder );
790 if (FAILED(hr)) {
791 msg_Warn(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
792 dx_sys->can_extern_pool = false;
793 break;
796 D3D11_AllocateShaderView(va, sys->d3d_dev.d3ddevice, textureFmt, pic->p_sys->texture, pic->p_sys->slice_index, pic->p_sys->resourceView);
798 dx_sys->hw_surface[surface_idx] = pic->p_sys->decoder;
801 if (!dx_sys->can_extern_pool)
803 for (size_t i = 0; i < surface_idx; ++i)
805 if (dx_sys->hw_surface[i])
807 ID3D11VideoDecoderOutputView_Release(dx_sys->hw_surface[i]);
808 dx_sys->hw_surface[i] = NULL;
810 if (sys->extern_pics[i])
812 sys->extern_pics[i]->p_sys->decoder = NULL;
813 picture_Release(sys->extern_pics[i]);
814 sys->extern_pics[i] = NULL;
818 else
819 #endif
820 msg_Dbg(va, "using external surface pool");
823 if (!dx_sys->can_extern_pool)
825 D3D11_TEXTURE2D_DESC texDesc;
826 ZeroMemory(&texDesc, sizeof(texDesc));
827 texDesc.Width = sys->textureWidth;
828 texDesc.Height = sys->textureHeight;
829 texDesc.MipLevels = 1;
830 texDesc.Format = sys->render;
831 texDesc.SampleDesc.Count = 1;
832 texDesc.MiscFlags = 0;
833 texDesc.ArraySize = surface_count;
834 texDesc.Usage = D3D11_USAGE_DEFAULT;
835 texDesc.BindFlags = D3D11_BIND_DECODER;
836 texDesc.CPUAccessFlags = 0;
838 if (DeviceSupportsFormat(sys->d3d_dev.d3ddevice, texDesc.Format, D3D11_FORMAT_SUPPORT_SHADER_LOAD))
839 texDesc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
841 ID3D11Texture2D *p_texture;
842 hr = ID3D11Device_CreateTexture2D( sys->d3d_dev.d3ddevice, &texDesc, NULL, &p_texture );
843 if (FAILED(hr)) {
844 msg_Err(va, "CreateTexture2D %d failed. (hr=0x%0lx)", surface_count, hr);
845 return VLC_EGENERIC;
848 unsigned surface_idx;
849 for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
850 sys->extern_pics[surface_idx] = NULL;
851 viewDesc.Texture2D.ArraySlice = surface_idx;
853 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
854 (ID3D11Resource*) p_texture,
855 &viewDesc,
856 &dx_sys->hw_surface[surface_idx] );
857 if (FAILED(hr)) {
858 msg_Err(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
859 ID3D11Texture2D_Release(p_texture);
860 return VLC_EGENERIC;
863 if (texDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
865 ID3D11Texture2D *textures[D3D11_MAX_SHADER_VIEW] = {p_texture, p_texture, p_texture};
866 D3D11_AllocateShaderView(va, sys->d3d_dev.d3ddevice, textureFmt, textures, surface_idx,
867 &sys->resourceView[surface_idx * D3D11_MAX_SHADER_VIEW]);
871 msg_Dbg(va, "ID3D11VideoDecoderOutputView succeed with %d surfaces (%dx%d)",
872 surface_count, fmt->i_width, fmt->i_height);
874 D3D11_VIDEO_DECODER_DESC decoderDesc;
875 ZeroMemory(&decoderDesc, sizeof(decoderDesc));
876 decoderDesc.Guid = dx_sys->input;
877 decoderDesc.SampleWidth = fmt->i_width;
878 decoderDesc.SampleHeight = fmt->i_height;
879 decoderDesc.OutputFormat = sys->render;
881 UINT cfg_count;
882 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
883 if (FAILED(hr)) {
884 msg_Err(va, "GetVideoDecoderConfigCount failed. (hr=0x%lX)", hr);
885 return VLC_EGENERIC;
888 /* List all configurations available for the decoder */
889 D3D11_VIDEO_DECODER_CONFIG cfg_list[cfg_count];
890 for (unsigned i = 0; i < cfg_count; i++) {
891 hr = ID3D11VideoDevice_GetVideoDecoderConfig( dx_sys->d3ddec, &decoderDesc, i, &cfg_list[i] );
892 if (FAILED(hr)) {
893 msg_Err(va, "GetVideoDecoderConfig failed. (hr=0x%lX)", hr);
894 return VLC_EGENERIC;
898 msg_Dbg(va, "we got %d decoder configurations", cfg_count);
900 /* Select the best decoder configuration */
901 int cfg_score = 0;
902 for (unsigned i = 0; i < cfg_count; i++) {
903 const D3D11_VIDEO_DECODER_CONFIG *cfg = &cfg_list[i];
905 /* */
906 msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
907 i, cfg->ConfigBitstreamRaw);
909 /* */
910 int score;
911 if (cfg->ConfigBitstreamRaw == 1)
912 score = 1;
913 else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
914 score = 2;
915 else
916 continue;
917 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
918 score += 16;
920 if (cfg_score < score) {
921 sys->cfg = *cfg;
922 cfg_score = score;
925 if (cfg_score <= 0) {
926 msg_Err(va, "Failed to find a supported decoder configuration");
927 return VLC_EGENERIC;
930 /* Create the decoder */
931 ID3D11VideoDecoder *decoder;
932 hr = ID3D11VideoDevice_CreateVideoDecoder( dx_sys->d3ddec, &decoderDesc, &sys->cfg, &decoder );
933 if (FAILED(hr)) {
934 msg_Err(va, "ID3D11VideoDevice_CreateVideoDecoder failed. (hr=0x%lX)", hr);
935 dx_sys->decoder = NULL;
936 return VLC_EGENERIC;
938 dx_sys->decoder = decoder;
940 msg_Dbg(va, "DxCreateDecoderSurfaces succeed");
941 return VLC_SUCCESS;
944 static void DxDestroySurfaces(vlc_va_t *va)
946 directx_sys_t *dx_sys = &va->sys->dx_sys;
947 if (dx_sys->va_pool.surface_count && !dx_sys->can_extern_pool) {
948 ID3D11Resource *p_texture;
949 ID3D11VideoDecoderOutputView_GetResource( dx_sys->hw_surface[0], &p_texture );
950 ID3D11Resource_Release(p_texture);
951 ID3D11Resource_Release(p_texture);
953 for (unsigned i = 0; i < dx_sys->va_pool.surface_count; i++)
955 ID3D11VideoDecoderOutputView_Release( dx_sys->hw_surface[i] );
956 for (int j = 0; j < D3D11_MAX_SHADER_VIEW; j++)
958 if (va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j])
959 ID3D11ShaderResourceView_Release(va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j]);
962 if (dx_sys->decoder)
964 ID3D11VideoDecoder_Release(dx_sys->decoder);
965 dx_sys->decoder = NULL;