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
8 *
9 * Authors: Geoffroy Couprie <geal@videolan.org>
10 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
11 * Steve Lhomme <robux4@gmail.com>
12 *
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.
17 *
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.
22 *
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 *****************************************************************************/
27
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
34
35 # undef WINAPI_FAMILY
36 # define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
37
38 #include <assert.h>
39
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>
45
46 #define COBJMACROS
47 #include <initguid.h>
48 #include <d3d11.h>
49 #include <libavcodec/d3d11va.h>
50
51 #include "../../video_chroma/d3d11_fmt.h"
52
53 #define D3D_DecoderType ID3D11VideoDecoder
54 #define D3D_DecoderDevice ID3D11VideoDevice
55 #define D3D_DecoderSurface ID3D11VideoDecoderOutputView
56 #include "directx_va.h"
57
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 **);
61
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()
69
70 /*
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.
74 */
75 #define D3D11_DIRECT_DECODE LIBAVCODEC_VERSION_CHECK( 57, 30, 3, 72, 101 )
76
77 #include <initguid.h> /* must be last included to not redefine existing GUIDs */
78
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)
82
83 #ifdef __MINGW32__
84 # include <_mingw.h>
85
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
93
94 #endif /* __MINGW32__ */
95
96 DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo, 0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
97
98 DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
99
100 struct vlc_va_sys_t
101 {
102 directx_sys_t dx_sys;
103 UINT totalTextureSlices;
104 unsigned textureWidth;
105 unsigned textureHeight;
106
107 d3d11_handle_t hd3d;
108 d3d11_device_t d3d_dev;
109
110 /* Video service */
111 ID3D11VideoContext *d3dvidctx;
112 DXGI_FORMAT render;
113
114 /* pool */
115 picture_t *extern_pics[MAX_SURFACE_COUNT];
116
117 /* Video decoder */
118 D3D11_VIDEO_DECODER_CONFIG cfg;
119
120 /* avcodec internals */
121 struct AVD3D11VAContext hw;
122
123 ID3D11ShaderResourceView *resourceView[MAX_SURFACE_COUNT * D3D11_MAX_SHADER_VIEW];
124 };
125
126 /* */
127 static int D3dCreateDevice(vlc_va_t *);
128 static void D3dDestroyDevice(vlc_va_t *);
129 static char *DxDescribe(vlc_va_sys_t *);
130
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 *);
135
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 *);
140
SetupAVCodecContext(vlc_va_t * va)141 void SetupAVCodecContext(vlc_va_t *va)
142 {
143 vlc_va_sys_t *sys = va->sys;
144 directx_sys_t *dx_sys = &sys->dx_sys;
145
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;
152
153 if (IsEqualGUID(&dx_sys->input, &DXVA_Intel_H264_NoFGT_ClearVideo))
154 sys->hw.workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
155 }
156
d3d11_pic_context_destroy(struct picture_context_t * opaque)157 static void d3d11_pic_context_destroy(struct picture_context_t *opaque)
158 {
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);
164 }
165
166 static struct va_pic_context *CreatePicContext(ID3D11VideoDecoderOutputView *,
167 ID3D11Resource *,
168 ID3D11DeviceContext *,
169 UINT slice,
170 ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW]);
171
d3d11_pic_context_copy(struct picture_context_t * ctx)172 static struct picture_context_t *d3d11_pic_context_copy(struct picture_context_t *ctx)
173 {
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);
183 }
184 return &pic_ctx->s;
185 }
186
CreatePicContext(ID3D11VideoDecoderOutputView * decoderSurface,ID3D11Resource * p_resource,ID3D11DeviceContext * context,UINT slice,ID3D11ShaderResourceView * resourceView[D3D11_MAX_SHADER_VIEW])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])
193 {
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;
199
200 D3D11_TEXTURE2D_DESC txDesc;
201 ID3D11Texture2D_GetDesc((ID3D11Texture2D*)p_resource, &txDesc);
202
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++)
208 {
209 pic_ctx->picsys.resource[i] = p_resource;
210 pic_ctx->picsys.resourceView[i] = resourceView[i];
211 }
212 AcquirePictureSys(&pic_ctx->picsys);
213 pic_ctx->picsys.context = context;
214 done:
215 return pic_ctx;
216 }
217
NewSurfacePicContext(vlc_va_t * va,int surface_index)218 static struct va_pic_context* NewSurfacePicContext(vlc_va_t *va, int surface_index)
219 {
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);
226
227 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
228 ID3D11VideoDecoderOutputView_GetDesc(surface, &viewDesc);
229
230 for (int i=0; i<D3D11_MAX_SHADER_VIEW; i++)
231 resourceView[i] = sys->resourceView[viewDesc.Texture2D.ArraySlice*D3D11_MAX_SHADER_VIEW + i];
232
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;
247 }
248
Get(vlc_va_t * va,picture_t * pic,uint8_t ** data)249 static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
250 {
251 #if D3D11_DIRECT_DECODE
252 if (va->sys->dx_sys.can_extern_pool)
253 {
254 /* copy the original picture_sys_t in the va_pic_context */
255 if (!pic->context)
256 {
257 assert(pic->p_sys!=NULL);
258 if (!pic->p_sys->decoder)
259 {
260 HRESULT hr;
261 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
262 ZeroMemory(&viewDesc, sizeof(viewDesc));
263 viewDesc.DecodeProfile = va->sys->dx_sys.input;
264 viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
265 viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
266
267 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( va->sys->dx_sys.d3ddec,
268 pic->p_sys->resource[KNOWN_DXGI_INDEX],
269 &viewDesc,
270 &pic->p_sys->decoder );
271 if (FAILED(hr))
272 return VLC_EGENERIC;
273 }
274
275 pic->context = (picture_context_t*)CreatePicContext(
276 pic->p_sys->decoder,
277 pic->p_sys->resource[KNOWN_DXGI_INDEX],
278 va->sys->d3d_dev.d3dcontext,
279 pic->p_sys->slice_index,
280 pic->p_sys->resourceView );
281 if (pic->context == NULL)
282 return VLC_EGENERIC;
283 }
284 }
285 else
286 #endif
287 {
288 int res = va_pool_Get(&va->sys->dx_sys.va_pool, pic);
289 if (unlikely(res != VLC_SUCCESS))
290 return res;
291 }
292 *data = (uint8_t*)((struct va_pic_context *)pic->context)->picsys.decoder;
293 return VLC_SUCCESS;
294 }
295
Close(vlc_va_t * va,void ** ctx)296 static void Close(vlc_va_t *va, void **ctx)
297 {
298 vlc_va_sys_t *sys = va->sys;
299
300 (void) ctx;
301
302 directx_va_Close(va, &sys->dx_sys);
303
304 D3D11_Destroy( &sys->hd3d );
305
306 free((char *)va->description);
307 free(sys);
308 }
309
Open(vlc_va_t * va,AVCodecContext * ctx,enum PixelFormat pix_fmt,const es_format_t * fmt,picture_sys_t * p_sys)310 static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
311 const es_format_t *fmt, picture_sys_t *p_sys)
312 {
313 int err = VLC_EGENERIC;
314 directx_sys_t *dx_sys;
315
316 ctx->hwaccel_context = NULL;
317
318 if (pix_fmt != AV_PIX_FMT_D3D11VA_VLD)
319 return VLC_EGENERIC;
320
321 #if !VLC_WINSTORE_APP
322 /* Allow using D3D11VA automatically starting from Windows 8.1 */
323 if (!va->obj.force)
324 {
325 bool isWin81OrGreater = false;
326 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32.dll"));
327 if (likely(hKernel32 != NULL))
328 isWin81OrGreater = GetProcAddress(hKernel32, "IsProcessCritical") != NULL;
329 if (!isWin81OrGreater)
330 return VLC_EGENERIC;
331 }
332 #endif
333
334 vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
335 if (unlikely(sys == NULL))
336 return VLC_ENOMEM;
337
338 dx_sys = &sys->dx_sys;
339
340 dx_sys->va_pool.pf_create_device = D3dCreateDevice;
341 dx_sys->va_pool.pf_destroy_device = D3dDestroyDevice;
342 dx_sys->va_pool.pf_create_video_service = DxCreateVideoService;
343 dx_sys->va_pool.pf_destroy_video_service = DxDestroyVideoService;
344 dx_sys->va_pool.pf_create_decoder_surfaces = DxCreateDecoderSurfaces;
345 dx_sys->va_pool.pf_destroy_surfaces = DxDestroySurfaces;
346 dx_sys->va_pool.pf_setup_avcodec_ctx = SetupAVCodecContext;
347 dx_sys->va_pool.pf_new_surface_context = NewSurfacePicContext;
348 dx_sys->pf_get_input_list = DxGetInputList;
349 dx_sys->pf_setup_output = DxSetupOutput;
350
351 va->sys = sys;
352
353 sys->d3d_dev.d3ddevice = NULL;
354 va->sys->render = DXGI_FORMAT_UNKNOWN;
355 if ( p_sys != NULL && p_sys->context != NULL ) {
356 void *d3dvidctx = NULL;
357 HRESULT hr = ID3D11DeviceContext_QueryInterface(p_sys->context, &IID_ID3D11VideoContext, &d3dvidctx);
358 if (FAILED(hr)) {
359 msg_Err(va, "Could not Query ID3D11VideoContext Interface from the picture. (hr=0x%lX)", hr);
360 } else {
361 ID3D11DeviceContext_GetDevice( p_sys->context, &sys->d3d_dev.d3ddevice );
362 HANDLE context_lock = INVALID_HANDLE_VALUE;
363 UINT dataSize = sizeof(context_lock);
364 hr = ID3D11DeviceContext_GetPrivateData(p_sys->context, &GUID_CONTEXT_MUTEX, &dataSize, &context_lock);
365 if (FAILED(hr))
366 msg_Warn(va, "No mutex found to lock the decoder");
367 sys->d3d_dev.context_mutex = context_lock;
368
369 sys->d3d_dev.d3dcontext = p_sys->context;
370 sys->d3d_dev.owner = false;
371 D3D11_GetDriverVersion(va, &sys->d3d_dev);
372 sys->d3dvidctx = d3dvidctx;
373
374 assert(p_sys->texture[KNOWN_DXGI_INDEX] != NULL);
375 D3D11_TEXTURE2D_DESC dstDesc;
376 ID3D11Texture2D_GetDesc( p_sys->texture[KNOWN_DXGI_INDEX], &dstDesc);
377 sys->render = dstDesc.Format;
378 if (dstDesc.BindFlags & D3D11_BIND_DECODER)
379 {
380 va->sys->textureWidth = dstDesc.Width;
381 va->sys->textureHeight = dstDesc.Height;
382 va->sys->totalTextureSlices = dstDesc.ArraySize;
383 }
384 }
385 }
386
387 err = D3D11_Create( va, &sys->hd3d, false );
388 if (err != VLC_SUCCESS)
389 goto error;
390
391 err = directx_va_Open(va, &sys->dx_sys);
392 if (err!=VLC_SUCCESS)
393 goto error;
394
395 err = directx_va_Setup(va, &sys->dx_sys, ctx, fmt, isXboxHardware(sys->d3d_dev.d3ddevice));
396 if (err != VLC_SUCCESS)
397 goto error;
398
399 ctx->hwaccel_context = &sys->hw;
400
401 /* TODO print the hardware name/vendor for debugging purposes */
402 va->description = DxDescribe(sys);
403 va->get = Get;
404
405 return VLC_SUCCESS;
406
407 error:
408 Close(va, NULL);
409 return err;
410 }
411
412 /**
413 * It creates a Direct3D device usable for decoding
414 */
D3dCreateDevice(vlc_va_t * va)415 static int D3dCreateDevice(vlc_va_t *va)
416 {
417 vlc_va_sys_t *sys = va->sys;
418 HRESULT hr;
419
420 if (sys->d3d_dev.d3ddevice && sys->d3d_dev.d3dcontext) {
421 msg_Dbg(va, "Reusing Direct3D11 device");
422 ID3D11DeviceContext_AddRef(sys->d3d_dev.d3dcontext);
423 return VLC_SUCCESS;
424 }
425
426 #if VLC_WINSTORE_APP
427 sys->d3d_dev.d3dcontext = var_InheritInteger(va, "winrt-d3dcontext");
428 if (likely(sys->d3d_dev.d3dcontext))
429 {
430 ID3D11Device* d3ddevice = NULL;
431 ID3D11DeviceContext_GetDevice(sys->d3d_dev.d3dcontext, &sys->d3d_dev.d3ddevice);
432 ID3D11DeviceContext_AddRef(sys->d3d_dev.d3dcontext);
433 ID3D11Device_Release(sys->d3d_dev.d3ddevice);
434 }
435 #endif
436
437 /* */
438 if (!sys->d3d_dev.d3ddevice)
439 {
440 hr = D3D11_CreateDevice(va, &sys->hd3d, true, &sys->d3d_dev);
441 if (FAILED(hr)) {
442 msg_Err(va, "D3D11CreateDevice failed. (hr=0x%lX)", hr);
443 return VLC_EGENERIC;
444 }
445 }
446
447 void *d3dvidctx = NULL;
448 hr = ID3D11DeviceContext_QueryInterface(sys->d3d_dev.d3dcontext, &IID_ID3D11VideoContext, &d3dvidctx);
449 if (FAILED(hr)) {
450 msg_Err(va, "Could not Query ID3D11VideoContext Interface. (hr=0x%lX)", hr);
451 ID3D11DeviceContext_Release(sys->d3d_dev.d3dcontext);
452 ID3D11Device_Release(sys->d3d_dev.d3ddevice);
453 return VLC_EGENERIC;
454 }
455 sys->d3dvidctx = d3dvidctx;
456
457 return VLC_SUCCESS;
458 }
459
460 /**
461 * It releases a Direct3D device and its resources.
462 */
D3dDestroyDevice(vlc_va_t * va)463 static void D3dDestroyDevice(vlc_va_t *va)
464 {
465 vlc_va_sys_t *sys = va->sys;
466 if (sys->d3dvidctx)
467 ID3D11VideoContext_Release(sys->d3dvidctx);
468 D3D11_ReleaseDevice( &sys->d3d_dev );
469 }
470
471 /**
472 * It describes our Direct3D object
473 */
DxDescribe(vlc_va_sys_t * sys)474 static char *DxDescribe(vlc_va_sys_t *sys)
475 {
476
477 IDXGIAdapter *p_adapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
478 if (!p_adapter) {
479 return NULL;
480 }
481
482 char *description = NULL;
483 DXGI_ADAPTER_DESC adapterDesc;
484 if (SUCCEEDED(IDXGIAdapter_GetDesc(p_adapter, &adapterDesc))) {
485 char *utfdesc = FromWide(adapterDesc.Description);
486 if (likely(utfdesc!=NULL))
487 {
488 if (asprintf(&description, "D3D11VA (%s, vendor %x(%s), device %x, revision %x)",
489 utfdesc,
490 adapterDesc.VendorId, DxgiVendorStr(adapterDesc.VendorId), adapterDesc.DeviceId, adapterDesc.Revision) < 0)
491 description = NULL;
492 free(utfdesc);
493 }
494 }
495
496 IDXGIAdapter_Release(p_adapter);
497 return description;
498 }
499
500 /**
501 * It creates a DirectX video service
502 */
DxCreateVideoService(vlc_va_t * va)503 static int DxCreateVideoService(vlc_va_t *va)
504 {
505 directx_sys_t *dx_sys = &va->sys->dx_sys;
506
507 void *d3dviddev = NULL;
508 HRESULT hr = ID3D11Device_QueryInterface(va->sys->d3d_dev.d3ddevice, &IID_ID3D11VideoDevice, &d3dviddev);
509 if (FAILED(hr)) {
510 msg_Err(va, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr);
511 return VLC_EGENERIC;
512 }
513 dx_sys->d3ddec = d3dviddev;
514
515 return VLC_SUCCESS;
516 }
517
518 /**
519 * It destroys a DirectX video service
520 */
DxDestroyVideoService(vlc_va_t * va)521 static void DxDestroyVideoService(vlc_va_t *va)
522 {
523 directx_sys_t *dx_sys = &va->sys->dx_sys;
524 if (dx_sys->d3ddec)
525 ID3D11VideoDevice_Release(dx_sys->d3ddec);
526 }
527
ReleaseInputList(input_list_t * p_list)528 static void ReleaseInputList(input_list_t *p_list)
529 {
530 free(p_list->list);
531 }
532
DxGetInputList(vlc_va_t * va,input_list_t * p_list)533 static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
534 {
535 directx_sys_t *dx_sys = &va->sys->dx_sys;
536 HRESULT hr;
537
538 UINT input_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(dx_sys->d3ddec);
539
540 p_list->count = input_count;
541 p_list->list = calloc(input_count, sizeof(*p_list->list));
542 if (unlikely(p_list->list == NULL)) {
543 return VLC_ENOMEM;
544 }
545 p_list->pf_release = ReleaseInputList;
546
547 for (unsigned i = 0; i < input_count; i++) {
548 hr = ID3D11VideoDevice_GetVideoDecoderProfile(dx_sys->d3ddec, i, &p_list->list[i]);
549 if (FAILED(hr))
550 {
551 msg_Err(va, "GetVideoDecoderProfile %d failed. (hr=0x%lX)", i, hr);
552 ReleaseInputList(p_list);
553 return VLC_EGENERIC;
554 }
555 }
556
557 return VLC_SUCCESS;
558 }
559
560 extern const GUID DXVA_ModeHEVC_VLD_Main10;
561 extern const GUID DXVA_ModeVP9_VLD_10bit_Profile2;
562
DxSetupOutput(vlc_va_t * va,const GUID * input,const video_format_t * fmt)563 static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
564 {
565 vlc_va_sys_t *sys = va->sys;
566 directx_sys_t *dx_sys = &sys->dx_sys;
567 HRESULT hr;
568
569 #ifndef NDEBUG
570 BOOL bSupported = false;
571 for (int format = 0; format < 188; format++) {
572 hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, format, &bSupported);
573 if (SUCCEEDED(hr) && bSupported)
574 msg_Dbg(va, "format %s is supported for output", DxgiFormatToStr(format));
575 }
576 #endif
577
578 IDXGIAdapter *pAdapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
579 if (!pAdapter)
580 return VLC_EGENERIC;
581
582 DXGI_ADAPTER_DESC adapterDesc;
583 hr = IDXGIAdapter_GetDesc(pAdapter, &adapterDesc);
584 IDXGIAdapter_Release(pAdapter);
585 if (FAILED(hr))
586 return VLC_EGENERIC;
587
588 char *psz_decoder_name = directx_va_GetDecoderName(input);
589
590 if (!directx_va_canUseDecoder(va, adapterDesc.VendorId, adapterDesc.DeviceId,
591 input, sys->d3d_dev.WDDM.build))
592 {
593 msg_Warn(va, "GPU blacklisted for %s codec", psz_decoder_name);
594 free(psz_decoder_name);
595 return VLC_EGENERIC;
596 }
597
598 DXGI_FORMAT processorInput[5];
599 int idx = 0;
600 if ( sys->render != DXGI_FORMAT_UNKNOWN )
601 processorInput[idx++] = sys->render;
602 if (IsEqualGUID(input, &DXVA_ModeHEVC_VLD_Main10) || IsEqualGUID(input, &DXVA_ModeVP9_VLD_10bit_Profile2))
603 processorInput[idx++] = DXGI_FORMAT_P010;
604 processorInput[idx++] = DXGI_FORMAT_NV12;
605 processorInput[idx++] = DXGI_FORMAT_420_OPAQUE;
606 processorInput[idx++] = DXGI_FORMAT_UNKNOWN;
607
608 /* */
609 for (idx = 0; processorInput[idx] != DXGI_FORMAT_UNKNOWN; ++idx)
610 {
611 BOOL is_supported = false;
612 hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, processorInput[idx], &is_supported);
613 if (SUCCEEDED(hr) && is_supported)
614 msg_Dbg(va, "%s output is supported for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
615 else
616 {
617 msg_Dbg(va, "Can't get a decoder output format %s for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
618 continue;
619 }
620
621 // check if we can create render texture of that format
622 // check the decoder can output to that format
623 if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
624 D3D11_FORMAT_SUPPORT_SHADER_LOAD) )
625 {
626 #ifndef ID3D11VideoContext_VideoProcessorBlt
627 msg_Dbg(va, "Format %s needs a processor but is not supported",
628 DxgiFormatToStr(processorInput[idx]));
629 #else
630 if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
631 D3D11_FORMAT_SUPPORT_VIDEO_PROCESSOR_INPUT) )
632 {
633 msg_Dbg(va, "Format %s needs a processor but is not available",
634 DxgiFormatToStr(processorInput[idx]));
635 continue;
636 }
637 #endif
638 }
639
640 D3D11_VIDEO_DECODER_DESC decoderDesc;
641 ZeroMemory(&decoderDesc, sizeof(decoderDesc));
642 decoderDesc.Guid = *input;
643 decoderDesc.SampleWidth = fmt->i_width;
644 decoderDesc.SampleHeight = fmt->i_height;
645 decoderDesc.OutputFormat = processorInput[idx];
646
647 UINT cfg_count = 0;
648 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
649 if (FAILED(hr))
650 {
651 msg_Err( va, "Failed to get configuration for decoder %s. (hr=0x%lX)", psz_decoder_name, hr );
652 continue;
653 }
654 if (cfg_count == 0) {
655 msg_Err( va, "No decoder configuration possible for %s %dx%d",
656 DxgiFormatToStr(decoderDesc.OutputFormat),
657 decoderDesc.SampleWidth, decoderDesc.SampleHeight );
658 continue;
659 }
660
661 msg_Dbg(va, "Using output format %s for decoder %s", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
662 if ( sys->render == processorInput[idx] && sys->totalTextureSlices > 4)
663 {
664 if (CanUseVoutPool(&sys->d3d_dev, sys->totalTextureSlices))
665 dx_sys->can_extern_pool = true;
666 else
667 msg_Warn( va, "use internal pool" );
668 }
669 sys->render = processorInput[idx];
670 free(psz_decoder_name);
671 return VLC_SUCCESS;
672 }
673 free(psz_decoder_name);
674
675 msg_Dbg(va, "Output format from picture source not supported.");
676 return VLC_EGENERIC;
677 }
678
CanUseDecoderPadding(vlc_va_sys_t * sys)679 static bool CanUseDecoderPadding(vlc_va_sys_t *sys)
680 {
681 IDXGIAdapter *pAdapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
682 if (!pAdapter)
683 return false;
684
685 DXGI_ADAPTER_DESC adapterDesc;
686 HRESULT hr = IDXGIAdapter_GetDesc(pAdapter, &adapterDesc);
687 IDXGIAdapter_Release(pAdapter);
688 if (FAILED(hr))
689 return false;
690
691 /* Qualcomm hardware has issues with textures and pixels that should not be
692 * part of the decoded area */
693 return adapterDesc.VendorId != GPU_MANUFACTURER_QUALCOMM;
694 }
695
696 /**
697 * It creates a Direct3D11 decoder using the given video format
698 */
DxCreateDecoderSurfaces(vlc_va_t * va,int codec_id,const video_format_t * fmt,unsigned surface_count)699 static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id,
700 const video_format_t *fmt, unsigned surface_count)
701 {
702 vlc_va_sys_t *sys = va->sys;
703 directx_sys_t *dx_sys = &va->sys->dx_sys;
704 HRESULT hr;
705
706 ID3D10Multithread *pMultithread;
707 hr = ID3D11Device_QueryInterface( sys->d3d_dev.d3ddevice, &IID_ID3D10Multithread, (void **)&pMultithread);
708 if (SUCCEEDED(hr)) {
709 ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
710 ID3D10Multithread_Release(pMultithread);
711 }
712
713 if (!sys->textureWidth || !sys->textureHeight)
714 {
715 sys->textureWidth = fmt->i_width;
716 sys->textureHeight = fmt->i_height;
717 }
718
719 if ((sys->textureWidth != fmt->i_width || sys->textureHeight != fmt->i_height) &&
720 !CanUseDecoderPadding(sys))
721 {
722 msg_Dbg(va, "mismatching external pool sizes use the internal one %dx%d vs %dx%d",
723 sys->textureWidth, sys->textureHeight, fmt->i_width, fmt->i_height);
724 dx_sys->can_extern_pool = false;
725 sys->textureWidth = fmt->i_width;
726 sys->textureHeight = fmt->i_height;
727 }
728 if (sys->totalTextureSlices && sys->totalTextureSlices < surface_count)
729 {
730 msg_Warn(va, "not enough decoding slices in the texture (%d/%d)",
731 sys->totalTextureSlices, surface_count);
732 dx_sys->can_extern_pool = false;
733 }
734 #if VLC_WINSTORE_APP
735 /* On the Xbox 1/S, any decoding of H264 with one dimension over 2304
736 * crashes totally the device */
737 if (codec_id == AV_CODEC_ID_H264 &&
738 (sys->textureWidth > 2304 || sys->textureHeight > 2304) &&
739 isXboxHardware(sys->d3d_dev.d3ddevice))
740 {
741 msg_Warn(va, "%dx%d resolution not supported by your hardware", fmt->i_width, fmt->i_height);
742 return VLC_EGENERIC;
743 }
744 #endif
745
746 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
747 ZeroMemory(&viewDesc, sizeof(viewDesc));
748 viewDesc.DecodeProfile = dx_sys->input;
749 viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
750
751 const d3d_format_t *textureFmt = NULL;
752 for (const d3d_format_t *output_format = GetRenderFormatList();
753 output_format->name != NULL; ++output_format)
754 {
755 if (output_format->formatTexture == sys->render &&
756 is_d3d11_opaque(output_format->fourcc))
757 {
758 textureFmt = output_format;
759 break;
760 }
761 }
762 if (unlikely(textureFmt==NULL))
763 {
764 msg_Dbg(va, "no hardware decoder matching %s", DxgiFormatToStr(sys->render));
765 return VLC_EGENERIC;
766 }
767
768 if (dx_sys->can_extern_pool)
769 {
770 #if !D3D11_DIRECT_DECODE
771 size_t surface_idx;
772 for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
773 picture_t *pic = decoder_NewPicture( (decoder_t*) va->obj.parent );
774 sys->extern_pics[surface_idx] = pic;
775 dx_sys->hw_surface[surface_idx] = NULL;
776 if (pic==NULL)
777 {
778 msg_Warn(va, "not enough decoder pictures %d out of %d", surface_idx, surface_count);
779 dx_sys->can_extern_pool = false;
780 break;
781 }
782
783 D3D11_TEXTURE2D_DESC texDesc;
784 ID3D11Texture2D_GetDesc(pic->p_sys->texture[KNOWN_DXGI_INDEX], &texDesc);
785 assert(texDesc.Format == sys->render);
786 assert(texDesc.BindFlags & D3D11_BIND_DECODER);
787
788 #if !LIBAVCODEC_VERSION_CHECK( 57, 27, 2, 61, 102 )
789 if (pic->p_sys->slice_index != surface_idx)
790 {
791 msg_Warn(va, "d3d11va requires decoding slices to be the first in the texture (%d/%d)",
792 pic->p_sys->slice_index, surface_idx);
793 dx_sys->can_extern_pool = false;
794 break;
795 }
796 #endif
797
798 viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
799 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
800 pic->p_sys->resource[KNOWN_DXGI_INDEX],
801 &viewDesc,
802 &pic->p_sys->decoder );
803 if (FAILED(hr)) {
804 msg_Warn(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
805 dx_sys->can_extern_pool = false;
806 break;
807 }
808
809 D3D11_AllocateShaderView(va, sys->d3d_dev.d3ddevice, textureFmt, pic->p_sys->texture, pic->p_sys->slice_index, pic->p_sys->resourceView);
810
811 dx_sys->hw_surface[surface_idx] = pic->p_sys->decoder;
812 }
813
814 if (!dx_sys->can_extern_pool)
815 {
816 for (size_t i = 0; i < surface_idx; ++i)
817 {
818 if (dx_sys->hw_surface[i])
819 {
820 ID3D11VideoDecoderOutputView_Release(dx_sys->hw_surface[i]);
821 dx_sys->hw_surface[i] = NULL;
822 }
823 if (sys->extern_pics[i])
824 {
825 sys->extern_pics[i]->p_sys->decoder = NULL;
826 picture_Release(sys->extern_pics[i]);
827 sys->extern_pics[i] = NULL;
828 }
829 }
830 }
831 else
832 #endif
833 msg_Dbg(va, "using external surface pool");
834 }
835
836 if (!dx_sys->can_extern_pool)
837 {
838 D3D11_TEXTURE2D_DESC texDesc;
839 ZeroMemory(&texDesc, sizeof(texDesc));
840 texDesc.Width = sys->textureWidth;
841 texDesc.Height = sys->textureHeight;
842 texDesc.MipLevels = 1;
843 texDesc.Format = sys->render;
844 texDesc.SampleDesc.Count = 1;
845 texDesc.MiscFlags = 0;
846 texDesc.ArraySize = surface_count;
847 texDesc.Usage = D3D11_USAGE_DEFAULT;
848 texDesc.BindFlags = D3D11_BIND_DECODER;
849 texDesc.CPUAccessFlags = 0;
850
851 if (DeviceSupportsFormat(sys->d3d_dev.d3ddevice, texDesc.Format, D3D11_FORMAT_SUPPORT_SHADER_LOAD))
852 texDesc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
853
854 ID3D11Texture2D *p_texture;
855 hr = ID3D11Device_CreateTexture2D( sys->d3d_dev.d3ddevice, &texDesc, NULL, &p_texture );
856 if (FAILED(hr)) {
857 msg_Err(va, "CreateTexture2D %d failed. (hr=0x%0lx)", surface_count, hr);
858 return VLC_EGENERIC;
859 }
860
861 unsigned surface_idx;
862 for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
863 sys->extern_pics[surface_idx] = NULL;
864 viewDesc.Texture2D.ArraySlice = surface_idx;
865
866 hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
867 (ID3D11Resource*) p_texture,
868 &viewDesc,
869 &dx_sys->hw_surface[surface_idx] );
870 if (FAILED(hr)) {
871 msg_Err(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
872 ID3D11Texture2D_Release(p_texture);
873 return VLC_EGENERIC;
874 }
875
876 if (texDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
877 {
878 ID3D11Texture2D *textures[D3D11_MAX_SHADER_VIEW] = {p_texture, p_texture, p_texture};
879 D3D11_AllocateShaderView(va, sys->d3d_dev.d3ddevice, textureFmt, textures, surface_idx,
880 &sys->resourceView[surface_idx * D3D11_MAX_SHADER_VIEW]);
881 }
882 }
883 }
884 msg_Dbg(va, "ID3D11VideoDecoderOutputView succeed with %d surfaces (%dx%d)",
885 surface_count, fmt->i_width, fmt->i_height);
886
887 D3D11_VIDEO_DECODER_DESC decoderDesc;
888 ZeroMemory(&decoderDesc, sizeof(decoderDesc));
889 decoderDesc.Guid = dx_sys->input;
890 decoderDesc.SampleWidth = fmt->i_width;
891 decoderDesc.SampleHeight = fmt->i_height;
892 decoderDesc.OutputFormat = sys->render;
893
894 UINT cfg_count;
895 hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
896 if (FAILED(hr)) {
897 msg_Err(va, "GetVideoDecoderConfigCount failed. (hr=0x%lX)", hr);
898 return VLC_EGENERIC;
899 }
900
901 /* List all configurations available for the decoder */
902 D3D11_VIDEO_DECODER_CONFIG cfg_list[cfg_count];
903 for (unsigned i = 0; i < cfg_count; i++) {
904 hr = ID3D11VideoDevice_GetVideoDecoderConfig( dx_sys->d3ddec, &decoderDesc, i, &cfg_list[i] );
905 if (FAILED(hr)) {
906 msg_Err(va, "GetVideoDecoderConfig failed. (hr=0x%lX)", hr);
907 return VLC_EGENERIC;
908 }
909 }
910
911 msg_Dbg(va, "we got %d decoder configurations", cfg_count);
912
913 /* Select the best decoder configuration */
914 int cfg_score = 0;
915 for (unsigned i = 0; i < cfg_count; i++) {
916 const D3D11_VIDEO_DECODER_CONFIG *cfg = &cfg_list[i];
917
918 /* */
919 msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
920 i, cfg->ConfigBitstreamRaw);
921
922 /* */
923 int score;
924 if (cfg->ConfigBitstreamRaw == 1)
925 score = 1;
926 else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
927 score = 2;
928 else
929 continue;
930 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
931 score += 16;
932
933 if (cfg_score < score) {
934 sys->cfg = *cfg;
935 cfg_score = score;
936 }
937 }
938 if (cfg_score <= 0) {
939 msg_Err(va, "Failed to find a supported decoder configuration");
940 return VLC_EGENERIC;
941 }
942
943 /* Create the decoder */
944 ID3D11VideoDecoder *decoder;
945 hr = ID3D11VideoDevice_CreateVideoDecoder( dx_sys->d3ddec, &decoderDesc, &sys->cfg, &decoder );
946 if (FAILED(hr)) {
947 msg_Err(va, "ID3D11VideoDevice_CreateVideoDecoder failed. (hr=0x%lX)", hr);
948 dx_sys->decoder = NULL;
949 return VLC_EGENERIC;
950 }
951 dx_sys->decoder = decoder;
952
953 msg_Dbg(va, "DxCreateDecoderSurfaces succeed");
954 return VLC_SUCCESS;
955 }
956
DxDestroySurfaces(vlc_va_t * va)957 static void DxDestroySurfaces(vlc_va_t *va)
958 {
959 directx_sys_t *dx_sys = &va->sys->dx_sys;
960 if (dx_sys->va_pool.surface_count && !dx_sys->can_extern_pool) {
961 ID3D11Resource *p_texture;
962 ID3D11VideoDecoderOutputView_GetResource( dx_sys->hw_surface[0], &p_texture );
963 ID3D11Resource_Release(p_texture);
964 ID3D11Resource_Release(p_texture);
965 }
966 for (unsigned i = 0; i < dx_sys->va_pool.surface_count; i++)
967 {
968 ID3D11VideoDecoderOutputView_Release( dx_sys->hw_surface[i] );
969 for (int j = 0; j < D3D11_MAX_SHADER_VIEW; j++)
970 {
971 if (va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j])
972 ID3D11ShaderResourceView_Release(va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j]);
973 }
974 }
975 if (dx_sys->decoder)
976 {
977 ID3D11VideoDecoder_Release(dx_sys->decoder);
978 dx_sys->decoder = NULL;
979 }
980 }
981