d3d11va: when forcing the internal pool size, use the internal dimensions
[vlc.git] / modules / codec / avcodec / d3d11va.c
blob133f29d8ee0088c5f5d9e4ba0315fe4ca462b287
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 HANDLE context_mutex;
116 /* pool */
117 picture_t *extern_pics[MAX_SURFACE_COUNT];
119 /* Video decoder */
120 D3D11_VIDEO_DECODER_CONFIG cfg;
122 /* avcodec internals */
123 struct AVD3D11VAContext hw;
125 ID3D11ShaderResourceView *resourceView[MAX_SURFACE_COUNT * D3D11_MAX_SHADER_VIEW];
128 /* */
129 static int D3dCreateDevice(vlc_va_t *);
130 static void D3dDestroyDevice(vlc_va_t *);
131 static char *DxDescribe(vlc_va_sys_t *);
133 static int DxCreateVideoService(vlc_va_t *);
134 static void DxDestroyVideoService(vlc_va_t *);
135 static int DxGetInputList(vlc_va_t *, input_list_t *);
136 static int DxSetupOutput(vlc_va_t *, const GUID *, const video_format_t *);
138 static int DxCreateDecoderSurfaces(vlc_va_t *, int codec_id,
139 const video_format_t *fmt, unsigned surface_count);
140 static void DxDestroySurfaces(vlc_va_t *);
141 static void SetupAVCodecContext(vlc_va_t *);
143 void SetupAVCodecContext(vlc_va_t *va)
145 vlc_va_sys_t *sys = va->sys;
146 directx_sys_t *dx_sys = &sys->dx_sys;
148 sys->hw.video_context = sys->d3dvidctx;
149 sys->hw.decoder = dx_sys->decoder;
150 sys->hw.cfg = &sys->cfg;
151 sys->hw.surface_count = dx_sys->va_pool.surface_count;
152 sys->hw.surface = dx_sys->hw_surface;
153 sys->hw.context_mutex = sys->context_mutex;
155 if (IsEqualGUID(&dx_sys->input, &DXVA_Intel_H264_NoFGT_ClearVideo))
156 sys->hw.workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
159 static void d3d11_pic_context_destroy(struct picture_context_t *opaque)
161 struct va_pic_context *pic_ctx = (struct va_pic_context*)opaque;
162 if (pic_ctx->va_surface)
163 va_surface_Release(pic_ctx->va_surface);
164 ReleasePictureSys(&pic_ctx->picsys);
165 free(pic_ctx);
168 static struct va_pic_context *CreatePicContext(ID3D11VideoDecoderOutputView *,
169 ID3D11Resource *,
170 ID3D11DeviceContext *,
171 UINT slice,
172 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW]);
174 static struct picture_context_t *d3d11_pic_context_copy(struct picture_context_t *ctx)
176 struct va_pic_context *src_ctx = (struct va_pic_context*)ctx;
177 struct va_pic_context *pic_ctx = CreatePicContext(src_ctx->picsys.decoder,
178 src_ctx->picsys.resource[0], src_ctx->picsys.context,
179 src_ctx->picsys.slice_index, src_ctx->picsys.resourceView);
180 if (unlikely(pic_ctx==NULL))
181 return NULL;
182 if (src_ctx->va_surface) {
183 pic_ctx->va_surface = src_ctx->va_surface;
184 va_surface_AddRef(pic_ctx->va_surface);
186 return &pic_ctx->s;
189 static struct va_pic_context *CreatePicContext(
190 ID3D11VideoDecoderOutputView *decoderSurface,
191 ID3D11Resource *p_resource,
192 ID3D11DeviceContext *context,
193 UINT slice,
194 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW])
196 struct va_pic_context *pic_ctx = calloc(1, sizeof(*pic_ctx));
197 if (unlikely(pic_ctx==NULL))
198 goto done;
199 pic_ctx->s.destroy = d3d11_pic_context_destroy;
200 pic_ctx->s.copy = d3d11_pic_context_copy;
202 D3D11_TEXTURE2D_DESC txDesc;
203 ID3D11Texture2D_GetDesc((ID3D11Texture2D*)p_resource, &txDesc);
205 pic_ctx->picsys.formatTexture = txDesc.Format;
206 pic_ctx->picsys.context = context;
207 pic_ctx->picsys.slice_index = slice;
208 pic_ctx->picsys.decoder = decoderSurface;
209 for (int i=0;i<D3D11_MAX_SHADER_VIEW; i++)
211 pic_ctx->picsys.resource[i] = p_resource;
212 pic_ctx->picsys.resourceView[i] = resourceView[i];
214 AcquirePictureSys(&pic_ctx->picsys);
215 pic_ctx->picsys.context = context;
216 done:
217 return pic_ctx;
220 static struct va_pic_context* NewSurfacePicContext(vlc_va_t *va, int surface_index)
222 vlc_va_sys_t *sys = va->sys;
223 directx_sys_t *dx_sys = &sys->dx_sys;
224 ID3D11VideoDecoderOutputView *surface = dx_sys->hw_surface[surface_index];
225 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW];
226 ID3D11Resource *p_resource;
227 ID3D11VideoDecoderOutputView_GetResource(surface, &p_resource);
229 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
230 ID3D11VideoDecoderOutputView_GetDesc(surface, &viewDesc);
232 for (int i=0; i<D3D11_MAX_SHADER_VIEW; i++)
233 resourceView[i] = sys->resourceView[viewDesc.Texture2D.ArraySlice*D3D11_MAX_SHADER_VIEW + i];
235 struct va_pic_context *pic_ctx = CreatePicContext(
236 surface,
237 p_resource,
238 sys->d3d_dev.d3dcontext,
239 viewDesc.Texture2D.ArraySlice,
240 resourceView);
241 ID3D11Resource_Release(p_resource);
242 if (unlikely(pic_ctx==NULL))
243 return NULL;
244 /* all the resources are acquired during surfaces init, and a second time in
245 * CreatePicContext(), undo one of them otherwise we need an extra release
246 * when the pool is emptied */
247 ReleasePictureSys(&pic_ctx->picsys);
248 return pic_ctx;
251 static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
253 #if D3D11_DIRECT_DECODE
254 if (va->sys->dx_sys.can_extern_pool)
256 /* copy the original picture_sys_t in the va_pic_context */
257 if (!pic->context)
259 assert(pic->p_sys!=NULL);
260 if (!pic->p_sys->decoder)
262 HRESULT hr;
263 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
264 ZeroMemory(&viewDesc, sizeof(viewDesc));
265 viewDesc.DecodeProfile = va->sys->dx_sys.input;
266 viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
267 viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
269 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( va->sys->dx_sys.d3ddec,
270 pic->p_sys->resource[KNOWN_DXGI_INDEX],
271 &viewDesc,
272 &pic->p_sys->decoder );
273 if (FAILED(hr))
274 return VLC_EGENERIC;
277 pic->context = (picture_context_t*)CreatePicContext(
278 pic->p_sys->decoder,
279 pic->p_sys->resource[KNOWN_DXGI_INDEX],
280 va->sys->d3d_dev.d3dcontext,
281 pic->p_sys->slice_index,
282 pic->p_sys->resourceView );
283 if (pic->context == NULL)
284 return VLC_EGENERIC;
287 else
288 #endif
290 int res = va_pool_Get(&va->sys->dx_sys.va_pool, pic);
291 if (unlikely(res != VLC_SUCCESS))
292 return res;
294 *data = (uint8_t*)((struct va_pic_context *)pic->context)->picsys.decoder;
295 return VLC_SUCCESS;
298 static void Close(vlc_va_t *va, void **ctx)
300 vlc_va_sys_t *sys = va->sys;
302 (void) ctx;
304 directx_va_Close(va, &sys->dx_sys);
306 D3D11_Destroy( &sys->hd3d );
308 free((char *)va->description);
309 free(sys);
312 static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
313 const es_format_t *fmt, picture_sys_t *p_sys)
315 int err = VLC_EGENERIC;
316 directx_sys_t *dx_sys;
318 ctx->hwaccel_context = NULL;
320 if (pix_fmt != AV_PIX_FMT_D3D11VA_VLD)
321 return VLC_EGENERIC;
323 vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
324 if (unlikely(sys == NULL))
325 return VLC_ENOMEM;
327 dx_sys = &sys->dx_sys;
329 dx_sys->va_pool.pf_create_device = D3dCreateDevice;
330 dx_sys->va_pool.pf_destroy_device = D3dDestroyDevice;
331 dx_sys->va_pool.pf_create_video_service = DxCreateVideoService;
332 dx_sys->va_pool.pf_destroy_video_service = DxDestroyVideoService;
333 dx_sys->va_pool.pf_create_decoder_surfaces = DxCreateDecoderSurfaces;
334 dx_sys->va_pool.pf_destroy_surfaces = DxDestroySurfaces;
335 dx_sys->va_pool.pf_setup_avcodec_ctx = SetupAVCodecContext;
336 dx_sys->va_pool.pf_new_surface_context = NewSurfacePicContext;
337 dx_sys->pf_get_input_list = DxGetInputList;
338 dx_sys->pf_setup_output = DxSetupOutput;
340 va->sys = sys;
342 sys->d3d_dev.d3ddevice = NULL;
343 va->sys->render = DXGI_FORMAT_UNKNOWN;
344 if ( p_sys != NULL && p_sys->context != NULL ) {
345 void *d3dvidctx = NULL;
346 HRESULT hr = ID3D11DeviceContext_QueryInterface(p_sys->context, &IID_ID3D11VideoContext, &d3dvidctx);
347 if (FAILED(hr)) {
348 msg_Err(va, "Could not Query ID3D11VideoContext Interface from the picture. (hr=0x%lX)", hr);
349 } else {
350 ID3D11DeviceContext_GetDevice( p_sys->context, &sys->d3d_dev.d3ddevice );
351 HANDLE context_lock = INVALID_HANDLE_VALUE;
352 UINT dataSize = sizeof(context_lock);
353 hr = ID3D11Device_GetPrivateData(sys->d3d_dev.d3ddevice, &GUID_CONTEXT_MUTEX, &dataSize, &context_lock);
354 if (FAILED(hr))
355 msg_Warn(va, "No mutex found to lock the decoder");
356 sys->context_mutex = context_lock;
358 sys->d3d_dev.d3dcontext = p_sys->context;
359 sys->d3d_dev.owner = false;
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 );
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);
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 /* */
412 hr = D3D11_CreateDevice(va, &sys->hd3d, true, &sys->d3d_dev);
413 if (FAILED(hr)) {
414 msg_Err(va, "D3D11CreateDevice failed. (hr=0x%lX)", hr);
415 return VLC_EGENERIC;
418 void *d3dvidctx = NULL;
419 hr = ID3D11DeviceContext_QueryInterface(sys->d3d_dev.d3dcontext, &IID_ID3D11VideoContext, &d3dvidctx);
420 if (FAILED(hr)) {
421 msg_Err(va, "Could not Query ID3D11VideoContext Interface. (hr=0x%lX)", hr);
422 return VLC_EGENERIC;
424 sys->d3dvidctx = d3dvidctx;
426 return VLC_SUCCESS;
430 * It releases a Direct3D device and its resources.
432 static void D3dDestroyDevice(vlc_va_t *va)
434 vlc_va_sys_t *sys = va->sys;
435 if (sys->d3dvidctx)
436 ID3D11VideoContext_Release(sys->d3dvidctx);
437 D3D11_ReleaseDevice( &sys->d3d_dev );
441 * It describes our Direct3D object
443 static char *DxDescribe(vlc_va_sys_t *sys)
446 IDXGIAdapter *p_adapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
447 if (!p_adapter) {
448 return NULL;
451 char *description = NULL;
452 DXGI_ADAPTER_DESC adapterDesc;
453 if (SUCCEEDED(IDXGIAdapter_GetDesc(p_adapter, &adapterDesc))) {
454 char *utfdesc = FromWide(adapterDesc.Description);
455 if (likely(utfdesc!=NULL))
457 if (asprintf(&description, "D3D11VA (%s, vendor %u(%s), device %u, revision %u)",
458 utfdesc,
459 adapterDesc.VendorId, DxgiVendorStr(adapterDesc.VendorId), adapterDesc.DeviceId, adapterDesc.Revision) < 0)
460 description = NULL;
461 free(utfdesc);
465 IDXGIAdapter_Release(p_adapter);
466 return description;
470 * It creates a DirectX video service
472 static int DxCreateVideoService(vlc_va_t *va)
474 directx_sys_t *dx_sys = &va->sys->dx_sys;
476 void *d3dviddev = NULL;
477 HRESULT hr = ID3D11Device_QueryInterface(va->sys->d3d_dev.d3ddevice, &IID_ID3D11VideoDevice, &d3dviddev);
478 if (FAILED(hr)) {
479 msg_Err(va, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr);
480 return VLC_EGENERIC;
482 dx_sys->d3ddec = d3dviddev;
484 return VLC_SUCCESS;
488 * It destroys a DirectX video service
490 static void DxDestroyVideoService(vlc_va_t *va)
492 directx_sys_t *dx_sys = &va->sys->dx_sys;
493 if (dx_sys->d3ddec)
494 ID3D11VideoDevice_Release(dx_sys->d3ddec);
497 static void ReleaseInputList(input_list_t *p_list)
499 free(p_list->list);
502 static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
504 directx_sys_t *dx_sys = &va->sys->dx_sys;
505 HRESULT hr;
507 UINT input_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(dx_sys->d3ddec);
509 p_list->count = input_count;
510 p_list->list = calloc(input_count, sizeof(*p_list->list));
511 if (unlikely(p_list->list == NULL)) {
512 return VLC_ENOMEM;
514 p_list->pf_release = ReleaseInputList;
516 for (unsigned i = 0; i < input_count; i++) {
517 hr = ID3D11VideoDevice_GetVideoDecoderProfile(dx_sys->d3ddec, i, &p_list->list[i]);
518 if (FAILED(hr))
520 msg_Err(va, "GetVideoDecoderProfile %d failed. (hr=0x%lX)", i, hr);
521 ReleaseInputList(p_list);
522 return VLC_EGENERIC;
526 return VLC_SUCCESS;
529 extern const GUID DXVA_ModeHEVC_VLD_Main10;
530 static bool CanUseIntelHEVC(vlc_va_t *va)
532 vlc_va_sys_t *sys = va->sys;
533 /* it should be OK starting after driver 20.19.15.4835 */
534 struct wdmm_version WDMM = {
535 .wddm = 20,
536 .d3d_features = 19,
537 .revision = 15,
538 .build = 4836,
540 if (D3D11CheckDriverVersion(sys->d3d_dev.d3ddevice, GPU_MANUFACTURER_INTEL, &WDMM) == VLC_SUCCESS)
541 return true;
543 msg_Dbg(va, "HEVC not supported with these drivers");
544 return false;
547 static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
549 vlc_va_sys_t *sys = va->sys;
550 directx_sys_t *dx_sys = &sys->dx_sys;
551 HRESULT hr;
553 #ifndef NDEBUG
554 BOOL bSupported = false;
555 for (int format = 0; format < 188; format++) {
556 hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, format, &bSupported);
557 if (SUCCEEDED(hr) && bSupported)
558 msg_Dbg(va, "format %s is supported for output", DxgiFormatToStr(format));
560 #endif
562 if (IsEqualGUID(input,&DXVA_ModeHEVC_VLD_Main10) && !CanUseIntelHEVC(va))
563 return VLC_EGENERIC;
565 DXGI_FORMAT processorInput[4];
566 int idx = 0;
567 if ( sys->render != DXGI_FORMAT_UNKNOWN )
568 processorInput[idx++] = sys->render;
569 processorInput[idx++] = DXGI_FORMAT_NV12;
570 processorInput[idx++] = DXGI_FORMAT_420_OPAQUE;
571 processorInput[idx++] = DXGI_FORMAT_UNKNOWN;
573 char *psz_decoder_name = directx_va_GetDecoderName(input);
575 /* */
576 for (idx = 0; processorInput[idx] != DXGI_FORMAT_UNKNOWN; ++idx)
578 BOOL is_supported = false;
579 hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, processorInput[idx], &is_supported);
580 if (SUCCEEDED(hr) && is_supported)
581 msg_Dbg(va, "%s output is supported for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
582 else
584 msg_Dbg(va, "Can't get a decoder output format %s for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
585 continue;
588 // check if we can create render texture of that format
589 // check the decoder can output to that format
590 if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
591 D3D11_FORMAT_SUPPORT_SHADER_LOAD) )
593 #ifndef ID3D11VideoContext_VideoProcessorBlt
594 msg_Dbg(va, "Format %s needs a processor but is not supported",
595 DxgiFormatToStr(processorInput[idx]));
596 #else
597 if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
598 D3D11_FORMAT_SUPPORT_VIDEO_PROCESSOR_INPUT) )
600 msg_Dbg(va, "Format %s needs a processor but is not available",
601 DxgiFormatToStr(processorInput[idx]));
602 continue;
604 #endif
607 D3D11_VIDEO_DECODER_DESC decoderDesc;
608 ZeroMemory(&decoderDesc, sizeof(decoderDesc));
609 decoderDesc.Guid = *input;
610 decoderDesc.SampleWidth = fmt->i_width;
611 decoderDesc.SampleHeight = fmt->i_height;
612 decoderDesc.OutputFormat = processorInput[idx];
614 UINT cfg_count = 0;
615 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
616 if (FAILED(hr))
618 msg_Err( va, "Failed to get configuration for decoder %s. (hr=0x%lX)", psz_decoder_name, hr );
619 continue;
621 if (cfg_count == 0) {
622 msg_Err( va, "No decoder configuration possible for %s %dx%d",
623 DxgiFormatToStr(decoderDesc.OutputFormat),
624 decoderDesc.SampleWidth, decoderDesc.SampleHeight );
625 continue;
628 msg_Dbg(va, "Using output format %s for decoder %s", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
629 if ( sys->render == processorInput[idx] )
631 /* NVIDIA cards crash when calling CreateVideoDecoderOutputView
632 * on more than 30 slices */
633 if (sys->totalTextureSlices <= 30 || !isNvidiaHardware(sys->d3d_dev.d3ddevice))
634 dx_sys->can_extern_pool = true;
635 else
636 msg_Warn( va, "NVIDIA GPU with too many slices (%d) detected, use internal pool",
637 sys->totalTextureSlices );
639 sys->render = processorInput[idx];
640 free(psz_decoder_name);
641 return VLC_SUCCESS;
643 free(psz_decoder_name);
645 msg_Dbg(va, "Output format from picture source not supported.");
646 return VLC_EGENERIC;
649 static bool CanUseDecoderPadding(vlc_va_sys_t *sys)
651 IDXGIAdapter *pAdapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
652 if (!pAdapter)
653 return false;
655 DXGI_ADAPTER_DESC adapterDesc;
656 HRESULT hr = IDXGIAdapter_GetDesc(pAdapter, &adapterDesc);
657 IDXGIAdapter_Release(pAdapter);
658 if (FAILED(hr))
659 return false;
661 /* Qualcomm hardware has issues with textures and pixels that should not be
662 * part of the decoded area */
663 return adapterDesc.VendorId != GPU_MANUFACTURER_QUALCOMM;
667 * It creates a Direct3D11 decoder using the given video format
669 static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id,
670 const video_format_t *fmt, unsigned surface_count)
672 vlc_va_sys_t *sys = va->sys;
673 directx_sys_t *dx_sys = &va->sys->dx_sys;
674 HRESULT hr;
676 ID3D10Multithread *pMultithread;
677 hr = ID3D11Device_QueryInterface( sys->d3d_dev.d3ddevice, &IID_ID3D10Multithread, (void **)&pMultithread);
678 if (SUCCEEDED(hr)) {
679 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
680 ID3D10Multithread_Release(pMultithread);
683 if (!sys->textureWidth || !sys->textureHeight)
685 sys->textureWidth = fmt->i_width;
686 sys->textureHeight = fmt->i_height;
689 #if VLC_WINSTORE_APP
690 /* On the Xbox 1/S, any decoding of H264 with one dimension over 2304
691 * crashes totally the device */
692 if (codec_id == AV_CODEC_ID_H264 &&
693 (fmt->i_width > 2304 || fmt->i_height > 2304) &&
694 isXboxHardware(sys->d3d_dev.d3ddevice))
696 msg_Warn(va, "%dx%d resolution not supported by your hardware", fmt->i_width, fmt->i_height);
697 return VLC_EGENERIC;
699 #endif
700 if ((sys->textureWidth != fmt->i_width || sys->textureHeight != fmt->i_height) &&
701 !CanUseDecoderPadding(sys))
703 msg_Dbg(va, "mismatching external pool sizes use the internal one %dx%d vs %dx%d",
704 sys->textureWidth, sys->textureHeight, fmt->i_width, fmt->i_height);
705 dx_sys->can_extern_pool = false;
706 sys->textureWidth = fmt->i_width;
707 sys->textureHeight = fmt->i_height;
710 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
711 ZeroMemory(&viewDesc, sizeof(viewDesc));
712 viewDesc.DecodeProfile = dx_sys->input;
713 viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
715 const d3d_format_t *textureFmt = NULL;
716 for (const d3d_format_t *output_format = GetRenderFormatList();
717 output_format->name != NULL; ++output_format)
719 if (output_format->formatTexture == sys->render &&
720 is_d3d11_opaque(output_format->fourcc))
722 textureFmt = output_format;
723 break;
726 if (unlikely(textureFmt==NULL))
728 msg_Dbg(va, "no hardware decoder matching %s", DxgiFormatToStr(sys->render));
729 return VLC_EGENERIC;
732 if (dx_sys->can_extern_pool)
734 #if !D3D11_DIRECT_DECODE
735 size_t surface_idx;
736 for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
737 picture_t *pic = decoder_NewPicture( (decoder_t*) va->obj.parent );
738 sys->extern_pics[surface_idx] = pic;
739 dx_sys->hw_surface[surface_idx] = NULL;
740 if (pic==NULL)
742 msg_Warn(va, "not enough decoder pictures %d out of %d", surface_idx, surface_count);
743 dx_sys->can_extern_pool = false;
744 break;
747 D3D11_TEXTURE2D_DESC texDesc;
748 ID3D11Texture2D_GetDesc(pic->p_sys->texture[KNOWN_DXGI_INDEX], &texDesc);
749 if (texDesc.ArraySize < surface_count)
751 msg_Warn(va, "not enough decoding slices in the texture (%d/%d)",
752 texDesc.ArraySize, surface_count);
753 dx_sys->can_extern_pool = false;
754 break;
756 assert(texDesc.Format == sys->render);
757 assert(texDesc.BindFlags & D3D11_BIND_DECODER);
759 #if !LIBAVCODEC_VERSION_CHECK( 57, 27, 2, 61, 102 )
760 if (pic->p_sys->slice_index != surface_idx)
762 msg_Warn(va, "d3d11va requires decoding slices to be the first in the texture (%d/%d)",
763 pic->p_sys->slice_index, surface_idx);
764 dx_sys->can_extern_pool = false;
765 break;
767 #endif
769 viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
770 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
771 pic->p_sys->resource[KNOWN_DXGI_INDEX],
772 &viewDesc,
773 &pic->p_sys->decoder );
774 if (FAILED(hr)) {
775 msg_Warn(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
776 dx_sys->can_extern_pool = false;
777 break;
780 AllocateShaderView(VLC_OBJECT(va), sys->d3d_dev.d3ddevice, textureFmt, pic->p_sys->texture, pic->p_sys->slice_index, pic->p_sys->resourceView);
782 dx_sys->hw_surface[surface_idx] = pic->p_sys->decoder;
785 if (!dx_sys->can_extern_pool)
787 for (size_t i = 0; i < surface_idx; ++i)
789 if (dx_sys->hw_surface[i])
791 ID3D11VideoDecoderOutputView_Release(dx_sys->hw_surface[i]);
792 dx_sys->hw_surface[i] = NULL;
794 if (sys->extern_pics[i])
796 sys->extern_pics[i]->p_sys->decoder = NULL;
797 picture_Release(sys->extern_pics[i]);
798 sys->extern_pics[i] = NULL;
802 else
803 #endif
804 msg_Dbg(va, "using external surface pool");
807 if (!dx_sys->can_extern_pool)
809 D3D11_TEXTURE2D_DESC texDesc;
810 ZeroMemory(&texDesc, sizeof(texDesc));
811 texDesc.Width = sys->textureWidth;
812 texDesc.Height = sys->textureHeight;
813 texDesc.MipLevels = 1;
814 texDesc.Format = sys->render;
815 texDesc.SampleDesc.Count = 1;
816 texDesc.MiscFlags = 0;
817 texDesc.ArraySize = surface_count;
818 texDesc.Usage = D3D11_USAGE_DEFAULT;
819 texDesc.BindFlags = D3D11_BIND_DECODER;
820 texDesc.CPUAccessFlags = 0;
822 if (DeviceSupportsFormat(sys->d3d_dev.d3ddevice, texDesc.Format, D3D11_FORMAT_SUPPORT_SHADER_LOAD))
823 texDesc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
825 ID3D11Texture2D *p_texture;
826 hr = ID3D11Device_CreateTexture2D( sys->d3d_dev.d3ddevice, &texDesc, NULL, &p_texture );
827 if (FAILED(hr)) {
828 msg_Err(va, "CreateTexture2D %d failed. (hr=0x%0lx)", surface_count, hr);
829 return VLC_EGENERIC;
832 unsigned surface_idx;
833 for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
834 sys->extern_pics[surface_idx] = NULL;
835 viewDesc.Texture2D.ArraySlice = surface_idx;
837 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
838 (ID3D11Resource*) p_texture,
839 &viewDesc,
840 &dx_sys->hw_surface[surface_idx] );
841 if (FAILED(hr)) {
842 msg_Err(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
843 ID3D11Texture2D_Release(p_texture);
844 return VLC_EGENERIC;
847 if (texDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
849 ID3D11Texture2D *textures[D3D11_MAX_SHADER_VIEW] = {p_texture, p_texture};
850 AllocateShaderView(VLC_OBJECT(va), sys->d3d_dev.d3ddevice, textureFmt, textures, surface_idx,
851 &sys->resourceView[surface_idx * D3D11_MAX_SHADER_VIEW]);
855 msg_Dbg(va, "ID3D11VideoDecoderOutputView succeed with %d surfaces (%dx%d)",
856 surface_count, fmt->i_width, fmt->i_height);
858 D3D11_VIDEO_DECODER_DESC decoderDesc;
859 ZeroMemory(&decoderDesc, sizeof(decoderDesc));
860 decoderDesc.Guid = dx_sys->input;
861 decoderDesc.SampleWidth = fmt->i_width;
862 decoderDesc.SampleHeight = fmt->i_height;
863 decoderDesc.OutputFormat = sys->render;
865 UINT cfg_count;
866 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
867 if (FAILED(hr)) {
868 msg_Err(va, "GetVideoDecoderConfigCount failed. (hr=0x%lX)", hr);
869 return VLC_EGENERIC;
872 /* List all configurations available for the decoder */
873 D3D11_VIDEO_DECODER_CONFIG cfg_list[cfg_count];
874 for (unsigned i = 0; i < cfg_count; i++) {
875 hr = ID3D11VideoDevice_GetVideoDecoderConfig( dx_sys->d3ddec, &decoderDesc, i, &cfg_list[i] );
876 if (FAILED(hr)) {
877 msg_Err(va, "GetVideoDecoderConfig failed. (hr=0x%lX)", hr);
878 return VLC_EGENERIC;
882 msg_Dbg(va, "we got %d decoder configurations", cfg_count);
884 /* Select the best decoder configuration */
885 int cfg_score = 0;
886 for (unsigned i = 0; i < cfg_count; i++) {
887 const D3D11_VIDEO_DECODER_CONFIG *cfg = &cfg_list[i];
889 /* */
890 msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
891 i, cfg->ConfigBitstreamRaw);
893 /* */
894 int score;
895 if (cfg->ConfigBitstreamRaw == 1)
896 score = 1;
897 else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
898 score = 2;
899 else
900 continue;
901 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
902 score += 16;
904 if (cfg_score < score) {
905 sys->cfg = *cfg;
906 cfg_score = score;
909 if (cfg_score <= 0) {
910 msg_Err(va, "Failed to find a supported decoder configuration");
911 return VLC_EGENERIC;
914 /* Create the decoder */
915 ID3D11VideoDecoder *decoder;
916 hr = ID3D11VideoDevice_CreateVideoDecoder( dx_sys->d3ddec, &decoderDesc, &sys->cfg, &decoder );
917 if (FAILED(hr)) {
918 msg_Err(va, "ID3D11VideoDevice_CreateVideoDecoder failed. (hr=0x%lX)", hr);
919 dx_sys->decoder = NULL;
920 return VLC_EGENERIC;
922 dx_sys->decoder = decoder;
924 msg_Dbg(va, "DxCreateDecoderSurfaces succeed");
925 return VLC_SUCCESS;
928 static void DxDestroySurfaces(vlc_va_t *va)
930 directx_sys_t *dx_sys = &va->sys->dx_sys;
931 if (dx_sys->va_pool.surface_count && !dx_sys->can_extern_pool) {
932 ID3D11Resource *p_texture;
933 ID3D11VideoDecoderOutputView_GetResource( dx_sys->hw_surface[0], &p_texture );
934 ID3D11Resource_Release(p_texture);
935 ID3D11Resource_Release(p_texture);
937 for (unsigned i = 0; i < dx_sys->va_pool.surface_count; i++)
939 ID3D11VideoDecoderOutputView_Release( dx_sys->hw_surface[i] );
940 for (int j = 0; j < D3D11_MAX_SHADER_VIEW; j++)
942 if (va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j])
943 ID3D11ShaderResourceView_Release(va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j]);
946 if (dx_sys->decoder)
948 ID3D11VideoDecoder_Release(dx_sys->decoder);
949 dx_sys->decoder = NULL;