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 *****************************************************************************/
29 * See https://msdn.microsoft.com/en-us/library/windows/desktop/hh162912%28v=vs.85%29.aspx
36 # define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
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>
49 #include <libavcodec/d3d11va.h>
51 #include "../../video_chroma/d3d11_fmt.h"
53 #define D3D_Device ID3D11Device
54 #define D3D_DecoderType ID3D11VideoDecoder
55 #define D3D_DecoderDevice ID3D11VideoDevice
56 #define D3D_DecoderSurface ID3D11VideoDecoderOutputView
57 #include "directx_va.h"
59 static int Open(vlc_va_t
*, AVCodecContext
*, enum PixelFormat
,
60 const es_format_t
*, picture_sys_t
*p_sys
);
61 static void Close(vlc_va_t
*, void **);
64 set_description(N_("Direct3D11 Video Acceleration"))
65 set_capability("hw decoder", 110)
66 set_category(CAT_INPUT
)
67 set_subcategory(SUBCAT_INPUT_VCODEC
)
68 set_callbacks(Open
, Close
)
72 * In this mode libavcodec doesn't need the whole array on texture on startup
73 * So we get the surfaces from the decoder pool when needed. We don't need to
74 * extract the decoded surface into the decoder picture anymore.
76 #define D3D11_DIRECT_DECODE LIBAVCODEC_VERSION_CHECK( 57, 30, 3, 72, 101 )
78 #include <initguid.h> /* must be last included to not redefine existing GUIDs */
80 /* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
81 * assume that they are declared in dxva2api.h */
82 #define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
87 # if !defined(__MINGW64_VERSION_MAJOR)
89 # define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
90 # define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
95 #endif /* __MINGW32__ */
97 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
98 # include <dxgidebug.h>
101 DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo
, 0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
103 DEFINE_GUID(DXVA2_NoEncrypt
, 0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
107 directx_sys_t dx_sys
;
108 UINT totalTextureSlices
;
109 unsigned textureWidth
;
110 unsigned textureHeight
;
112 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
113 HINSTANCE dxgidebug_dll
;
117 ID3D11VideoContext
*d3dvidctx
;
120 ID3D11DeviceContext
*d3dctx
;
121 HANDLE context_mutex
;
124 picture_t
*extern_pics
[MAX_SURFACE_COUNT
];
127 D3D11_VIDEO_DECODER_CONFIG cfg
;
129 /* avcodec internals */
130 struct AVD3D11VAContext hw
;
132 ID3D11ShaderResourceView
*resourceView
[MAX_SURFACE_COUNT
* D3D11_MAX_SHADER_VIEW
];
136 static int D3dCreateDevice(vlc_va_t
*);
137 static void D3dDestroyDevice(vlc_va_t
*);
138 static char *DxDescribe(directx_sys_t
*);
140 static int DxCreateVideoService(vlc_va_t
*);
141 static void DxDestroyVideoService(vlc_va_t
*);
142 static int DxGetInputList(vlc_va_t
*, input_list_t
*);
143 static int DxSetupOutput(vlc_va_t
*, const GUID
*, const video_format_t
*);
145 static int DxCreateDecoderSurfaces(vlc_va_t
*, int codec_id
,
146 const video_format_t
*fmt
, unsigned surface_count
);
147 static void DxDestroySurfaces(vlc_va_t
*);
148 static void SetupAVCodecContext(vlc_va_t
*);
150 void SetupAVCodecContext(vlc_va_t
*va
)
152 vlc_va_sys_t
*sys
= va
->sys
;
153 directx_sys_t
*dx_sys
= &sys
->dx_sys
;
155 sys
->hw
.video_context
= sys
->d3dvidctx
;
156 sys
->hw
.decoder
= dx_sys
->decoder
;
157 sys
->hw
.cfg
= &sys
->cfg
;
158 sys
->hw
.surface_count
= dx_sys
->va_pool
.surface_count
;
159 sys
->hw
.surface
= dx_sys
->hw_surface
;
160 sys
->hw
.context_mutex
= sys
->context_mutex
;
162 if (IsEqualGUID(&dx_sys
->input
, &DXVA_Intel_H264_NoFGT_ClearVideo
))
163 sys
->hw
.workaround
|= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO
;
166 static void d3d11_pic_context_destroy(struct picture_context_t
*opaque
)
168 struct va_pic_context
*pic_ctx
= (struct va_pic_context
*)opaque
;
169 if (pic_ctx
->va_surface
)
170 va_surface_Release(pic_ctx
->va_surface
);
171 ReleasePictureSys(&pic_ctx
->picsys
);
175 static struct va_pic_context
*CreatePicContext(ID3D11VideoDecoderOutputView
*,
177 ID3D11DeviceContext
*,
179 ID3D11ShaderResourceView
*resourceView
[D3D11_MAX_SHADER_VIEW
]);
181 static struct picture_context_t
*d3d11_pic_context_copy(struct picture_context_t
*ctx
)
183 struct va_pic_context
*src_ctx
= (struct va_pic_context
*)ctx
;
184 struct va_pic_context
*pic_ctx
= CreatePicContext(src_ctx
->picsys
.decoder
,
185 src_ctx
->picsys
.resource
[0], src_ctx
->picsys
.context
,
186 src_ctx
->picsys
.slice_index
, src_ctx
->picsys
.resourceView
);
187 if (unlikely(pic_ctx
==NULL
))
189 if (src_ctx
->va_surface
) {
190 pic_ctx
->va_surface
= src_ctx
->va_surface
;
191 va_surface_AddRef(pic_ctx
->va_surface
);
196 static struct va_pic_context
*CreatePicContext(
197 ID3D11VideoDecoderOutputView
*decoderSurface
,
198 ID3D11Resource
*p_resource
,
199 ID3D11DeviceContext
*context
,
201 ID3D11ShaderResourceView
*resourceView
[D3D11_MAX_SHADER_VIEW
])
203 struct va_pic_context
*pic_ctx
= calloc(1, sizeof(*pic_ctx
));
204 if (unlikely(pic_ctx
==NULL
))
206 pic_ctx
->s
.destroy
= d3d11_pic_context_destroy
;
207 pic_ctx
->s
.copy
= d3d11_pic_context_copy
;
209 D3D11_TEXTURE2D_DESC txDesc
;
210 ID3D11Texture2D_GetDesc((ID3D11Texture2D
*)p_resource
, &txDesc
);
212 pic_ctx
->picsys
.formatTexture
= txDesc
.Format
;
213 pic_ctx
->picsys
.context
= context
;
214 pic_ctx
->picsys
.slice_index
= slice
;
215 pic_ctx
->picsys
.decoder
= decoderSurface
;
216 for (int i
=0;i
<D3D11_MAX_SHADER_VIEW
; i
++)
218 pic_ctx
->picsys
.resource
[i
] = p_resource
;
219 pic_ctx
->picsys
.resourceView
[i
] = resourceView
[i
];
221 AcquirePictureSys(&pic_ctx
->picsys
);
222 pic_ctx
->picsys
.context
= context
;
227 static struct va_pic_context
* NewSurfacePicContext(vlc_va_t
*va
, int surface_index
)
229 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
230 ID3D11VideoDecoderOutputView
*surface
= dx_sys
->hw_surface
[surface_index
];
231 ID3D11ShaderResourceView
*resourceView
[D3D11_MAX_SHADER_VIEW
];
232 ID3D11Resource
*p_resource
;
233 ID3D11VideoDecoderOutputView_GetResource(surface
, &p_resource
);
235 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc
;
236 ID3D11VideoDecoderOutputView_GetDesc(surface
, &viewDesc
);
238 for (int i
=0; i
<D3D11_MAX_SHADER_VIEW
; i
++)
239 resourceView
[i
] = va
->sys
->resourceView
[viewDesc
.Texture2D
.ArraySlice
*D3D11_MAX_SHADER_VIEW
+ i
];
241 struct va_pic_context
*pic_ctx
= CreatePicContext(
245 viewDesc
.Texture2D
.ArraySlice
,
247 ID3D11Resource_Release(p_resource
);
248 if (unlikely(pic_ctx
==NULL
))
250 /* all the resources are acquired during surfaces init, and a second time in
251 * CreatePicContext(), undo one of them otherwise we need an extra release
252 * when the pool is emptied */
253 ReleasePictureSys(&pic_ctx
->picsys
);
257 static int Get(vlc_va_t
*va
, picture_t
*pic
, uint8_t **data
)
259 #if D3D11_DIRECT_DECODE
260 if (va
->sys
->dx_sys
.can_extern_pool
)
262 /* copy the original picture_sys_t in the va_pic_context */
265 assert(pic
->p_sys
!=NULL
);
266 if (!pic
->p_sys
->decoder
)
269 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc
;
270 ZeroMemory(&viewDesc
, sizeof(viewDesc
));
271 viewDesc
.DecodeProfile
= va
->sys
->dx_sys
.input
;
272 viewDesc
.ViewDimension
= D3D11_VDOV_DIMENSION_TEXTURE2D
;
273 viewDesc
.Texture2D
.ArraySlice
= pic
->p_sys
->slice_index
;
275 hr
= ID3D11VideoDevice_CreateVideoDecoderOutputView( va
->sys
->dx_sys
.d3ddec
,
276 pic
->p_sys
->resource
[KNOWN_DXGI_INDEX
],
278 &pic
->p_sys
->decoder
);
283 pic
->context
= (picture_context_t
*)CreatePicContext(
285 pic
->p_sys
->resource
[KNOWN_DXGI_INDEX
],
287 pic
->p_sys
->slice_index
,
288 pic
->p_sys
->resourceView
);
289 if (pic
->context
== NULL
)
296 int res
= va_pool_Get(&va
->sys
->dx_sys
.va_pool
, pic
);
297 if (unlikely(res
!= VLC_SUCCESS
))
300 *data
= (uint8_t*)((struct va_pic_context
*)pic
->context
)->picsys
.decoder
;
304 static void Close(vlc_va_t
*va
, void **ctx
)
306 vlc_va_sys_t
*sys
= va
->sys
;
310 directx_va_Close(va
, &sys
->dx_sys
);
312 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
313 if (sys
->dxgidebug_dll
)
314 FreeLibrary(sys
->dxgidebug_dll
);
317 free((char *)va
->description
);
321 static int Open(vlc_va_t
*va
, AVCodecContext
*ctx
, enum PixelFormat pix_fmt
,
322 const es_format_t
*fmt
, picture_sys_t
*p_sys
)
324 int err
= VLC_EGENERIC
;
325 directx_sys_t
*dx_sys
;
327 ctx
->hwaccel_context
= NULL
;
329 if (pix_fmt
!= AV_PIX_FMT_D3D11VA_VLD
)
332 vlc_va_sys_t
*sys
= calloc(1, sizeof (*sys
));
333 if (unlikely(sys
== NULL
))
336 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
337 sys
->dxgidebug_dll
= LoadLibrary(TEXT("DXGIDEBUG.DLL"));
340 dx_sys
= &sys
->dx_sys
;
342 dx_sys
->va_pool
.pf_create_device
= D3dCreateDevice
;
343 dx_sys
->va_pool
.pf_destroy_device
= D3dDestroyDevice
;
344 dx_sys
->va_pool
.pf_create_video_service
= DxCreateVideoService
;
345 dx_sys
->va_pool
.pf_destroy_video_service
= DxDestroyVideoService
;
346 dx_sys
->va_pool
.pf_create_decoder_surfaces
= DxCreateDecoderSurfaces
;
347 dx_sys
->va_pool
.pf_destroy_surfaces
= DxDestroySurfaces
;
348 dx_sys
->va_pool
.pf_setup_avcodec_ctx
= SetupAVCodecContext
;
349 dx_sys
->va_pool
.pf_new_surface_context
= NewSurfacePicContext
;
350 dx_sys
->pf_get_input_list
= DxGetInputList
;
351 dx_sys
->pf_setup_output
= DxSetupOutput
;
352 dx_sys
->psz_decoder_dll
= TEXT("D3D11.DLL");
356 dx_sys
->d3ddev
= NULL
;
357 va
->sys
->render
= DXGI_FORMAT_UNKNOWN
;
358 if ( p_sys
!= NULL
&& p_sys
->context
!= NULL
) {
359 void *d3dvidctx
= NULL
;
360 HRESULT hr
= ID3D11DeviceContext_QueryInterface(p_sys
->context
, &IID_ID3D11VideoContext
, &d3dvidctx
);
362 msg_Err(va
, "Could not Query ID3D11VideoContext Interface from the picture. (hr=0x%lX)", hr
);
364 ID3D11DeviceContext_GetDevice( p_sys
->context
, &dx_sys
->d3ddev
);
365 HANDLE context_lock
= INVALID_HANDLE_VALUE
;
366 UINT dataSize
= sizeof(context_lock
);
367 hr
= ID3D11Device_GetPrivateData(dx_sys
->d3ddev
, &GUID_CONTEXT_MUTEX
, &dataSize
, &context_lock
);
369 msg_Warn(va
, "No mutex found to lock the decoder");
370 sys
->context_mutex
= context_lock
;
372 sys
->d3dctx
= p_sys
->context
;
373 sys
->d3dvidctx
= d3dvidctx
;
375 assert(p_sys
->texture
[KNOWN_DXGI_INDEX
] != NULL
);
376 D3D11_TEXTURE2D_DESC dstDesc
;
377 ID3D11Texture2D_GetDesc( p_sys
->texture
[KNOWN_DXGI_INDEX
], &dstDesc
);
378 sys
->render
= dstDesc
.Format
;
379 va
->sys
->textureWidth
= dstDesc
.Width
;
380 va
->sys
->textureHeight
= dstDesc
.Height
;
381 va
->sys
->totalTextureSlices
= dstDesc
.ArraySize
;
384 if (!va
->sys
->textureWidth
|| !va
->sys
->textureHeight
)
386 va
->sys
->textureWidth
= fmt
->video
.i_width
;
387 va
->sys
->textureHeight
= fmt
->video
.i_height
;
391 err
= directx_va_Open(va
, &sys
->dx_sys
, false);
393 err
= directx_va_Open(va
, &sys
->dx_sys
, true);
395 if (err
!=VLC_SUCCESS
)
398 err
= directx_va_Setup(va
, &sys
->dx_sys
, ctx
, fmt
);
399 if (err
!= VLC_SUCCESS
)
402 ctx
->hwaccel_context
= &sys
->hw
;
404 /* TODO print the hardware name/vendor for debugging purposes */
405 va
->description
= DxDescribe(dx_sys
);
416 * It creates a Direct3D device usable for decoding
418 static int D3dCreateDevice(vlc_va_t
*va
)
420 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
423 if (dx_sys
->d3ddev
&& va
->sys
->d3dctx
) {
424 msg_Dbg(va
, "Reusing Direct3D11 device");
425 ID3D11DeviceContext_AddRef(va
->sys
->d3dctx
);
430 d3d11_handle_t hd3d11
;
431 hr
= D3D11_CreateDevice(VLC_OBJECT(va
), dx_sys
->hdecoder_dll
, true, &hd3d11
);
433 msg_Err(va
, "D3D11CreateDevice failed. (hr=0x%lX)", hr
);
436 dx_sys
->d3ddev
= hd3d11
.d3ddevice
;
437 va
->sys
->d3dctx
= hd3d11
.d3dcontext
;
439 void *d3dvidctx
= NULL
;
440 hr
= ID3D11DeviceContext_QueryInterface(dx_sys
->d3ddev
, &IID_ID3D11VideoContext
, &d3dvidctx
);
442 msg_Err(va
, "Could not Query ID3D11VideoContext Interface. (hr=0x%lX)", hr
);
445 va
->sys
->d3dvidctx
= d3dvidctx
;
447 #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
448 HRESULT (WINAPI
* pf_DXGIGetDebugInterface
)(const GUID
*riid
, void **ppDebug
);
449 if (va
->sys
->dxgidebug_dll
) {
450 pf_DXGIGetDebugInterface
= (void *)GetProcAddress(va
->sys
->dxgidebug_dll
, "DXGIGetDebugInterface");
451 if (pf_DXGIGetDebugInterface
) {
452 IDXGIDebug
*pDXGIDebug
= NULL
;
453 hr
= pf_DXGIGetDebugInterface(&IID_IDXGIDebug
, (void**)&pDXGIDebug
);
454 if (SUCCEEDED(hr
) && pDXGIDebug
) {
455 hr
= IDXGIDebug_ReportLiveObjects(pDXGIDebug
, DXGI_DEBUG_ALL
, DXGI_DEBUG_RLO_ALL
);
465 * It releases a Direct3D device and its resources.
467 static void D3dDestroyDevice(vlc_va_t
*va
)
469 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
470 if (va
->sys
->d3dvidctx
)
471 ID3D11VideoContext_Release(va
->sys
->d3dvidctx
);
473 ID3D11DeviceContext_Release(va
->sys
->d3dctx
);
475 ID3D11Device_Release(dx_sys
->d3ddev
);
478 * It describes our Direct3D object
480 static char *DxDescribe(directx_sys_t
*dx_sys
)
482 static const struct {
487 { 0x10DE, "NVIDIA" },
490 { 0x5333, "S3 Graphics" },
491 { 0x4D4F4351, "Qualcomm" },
495 IDXGIAdapter
*p_adapter
= D3D11DeviceAdapter(dx_sys
->d3ddev
);
500 char *description
= NULL
;
501 DXGI_ADAPTER_DESC adapterDesc
;
502 if (SUCCEEDED(IDXGIAdapter_GetDesc(p_adapter
, &adapterDesc
))) {
503 const char *vendor
= "Unknown";
504 for (int i
= 0; vendors
[i
].id
!= 0; i
++) {
505 if (vendors
[i
].id
== adapterDesc
.VendorId
) {
506 vendor
= vendors
[i
].name
;
511 char *utfdesc
= FromWide(adapterDesc
.Description
);
512 if (likely(utfdesc
!=NULL
))
514 if (asprintf(&description
, "D3D11VA (%s, vendor %u(%s), device %u, revision %u)",
516 adapterDesc
.VendorId
, vendor
, adapterDesc
.DeviceId
, adapterDesc
.Revision
) < 0)
522 IDXGIAdapter_Release(p_adapter
);
527 * It creates a DirectX video service
529 static int DxCreateVideoService(vlc_va_t
*va
)
531 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
533 void *d3dviddev
= NULL
;
534 HRESULT hr
= ID3D11Device_QueryInterface(dx_sys
->d3ddev
, &IID_ID3D11VideoDevice
, &d3dviddev
);
536 msg_Err(va
, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr
);
539 dx_sys
->d3ddec
= d3dviddev
;
545 * It destroys a DirectX video service
547 static void DxDestroyVideoService(vlc_va_t
*va
)
549 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
551 ID3D11VideoDevice_Release(dx_sys
->d3ddec
);
554 static void ReleaseInputList(input_list_t
*p_list
)
559 static int DxGetInputList(vlc_va_t
*va
, input_list_t
*p_list
)
561 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
564 UINT input_count
= ID3D11VideoDevice_GetVideoDecoderProfileCount(dx_sys
->d3ddec
);
566 p_list
->count
= input_count
;
567 p_list
->list
= calloc(input_count
, sizeof(*p_list
->list
));
568 if (unlikely(p_list
->list
== NULL
)) {
571 p_list
->pf_release
= ReleaseInputList
;
573 for (unsigned i
= 0; i
< input_count
; i
++) {
574 hr
= ID3D11VideoDevice_GetVideoDecoderProfile(dx_sys
->d3ddec
, i
, &p_list
->list
[i
]);
577 msg_Err(va
, "GetVideoDecoderProfile %d failed. (hr=0x%lX)", i
, hr
);
578 ReleaseInputList(p_list
);
586 static int DxSetupOutput(vlc_va_t
*va
, const GUID
*input
, const video_format_t
*fmt
)
588 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
592 BOOL bSupported
= false;
593 for (int format
= 0; format
< 188; format
++) {
594 hr
= ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys
->d3ddec
, input
, format
, &bSupported
);
595 if (SUCCEEDED(hr
) && bSupported
)
596 msg_Dbg(va
, "format %s is supported for output", DxgiFormatToStr(format
));
600 DXGI_FORMAT processorInput
[4];
602 if ( va
->sys
->render
!= DXGI_FORMAT_UNKNOWN
)
603 processorInput
[idx
++] = va
->sys
->render
;
604 processorInput
[idx
++] = DXGI_FORMAT_NV12
;
605 processorInput
[idx
++] = DXGI_FORMAT_420_OPAQUE
;
606 processorInput
[idx
++] = DXGI_FORMAT_UNKNOWN
;
608 char *psz_decoder_name
= directx_va_GetDecoderName(input
);
611 for (idx
= 0; processorInput
[idx
] != DXGI_FORMAT_UNKNOWN
; ++idx
)
613 BOOL is_supported
= false;
614 hr
= ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys
->d3ddec
, input
, processorInput
[idx
], &is_supported
);
615 if (SUCCEEDED(hr
) && is_supported
)
616 msg_Dbg(va
, "%s output is supported for decoder %s.", DxgiFormatToStr(processorInput
[idx
]), psz_decoder_name
);
619 msg_Dbg(va
, "Can't get a decoder output format %s for decoder %s.", DxgiFormatToStr(processorInput
[idx
]), psz_decoder_name
);
623 // check if we can create render texture of that format
624 // check the decoder can output to that format
625 if ( !DeviceSupportsFormat(dx_sys
->d3ddev
, processorInput
[idx
],
626 D3D11_FORMAT_SUPPORT_SHADER_LOAD
) )
628 #ifndef ID3D11VideoContext_VideoProcessorBlt
629 msg_Dbg(va
, "Format %s needs a processor but is not supported",
630 DxgiFormatToStr(processorInput
[idx
]));
632 if ( !DeviceSupportsFormat(dx_sys
->d3ddev
, processorInput
[idx
],
633 D3D11_FORMAT_SUPPORT_VIDEO_PROCESSOR_INPUT
) )
635 msg_Dbg(va
, "Format %s needs a processor but is not available",
636 DxgiFormatToStr(processorInput
[idx
]));
642 D3D11_VIDEO_DECODER_DESC decoderDesc
;
643 ZeroMemory(&decoderDesc
, sizeof(decoderDesc
));
644 decoderDesc
.Guid
= *input
;
645 decoderDesc
.SampleWidth
= fmt
->i_width
;
646 decoderDesc
.SampleHeight
= fmt
->i_height
;
647 decoderDesc
.OutputFormat
= processorInput
[idx
];
650 hr
= ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys
->d3ddec
, &decoderDesc
, &cfg_count
);
653 msg_Err( va
, "Failed to get configuration for decoder %s. (hr=0x%lX)", psz_decoder_name
, hr
);
656 if (cfg_count
== 0) {
657 msg_Err( va
, "No decoder configuration possible for %s %dx%d",
658 DxgiFormatToStr(decoderDesc
.OutputFormat
),
659 decoderDesc
.SampleWidth
, decoderDesc
.SampleHeight
);
663 msg_Dbg(va
, "Using output format %s for decoder %s", DxgiFormatToStr(processorInput
[idx
]), psz_decoder_name
);
664 if ( va
->sys
->render
== processorInput
[idx
] )
666 /* NVIDIA cards crash when calling CreateVideoDecoderOutputView
667 * on more than 30 slices */
668 if (va
->sys
->totalTextureSlices
<= 30 || !isNvidiaHardware(dx_sys
->d3ddev
))
669 dx_sys
->can_extern_pool
= true;
671 msg_Warn( va
, "NVIDIA GPU with too many slices (%d) detected, use internal pool",
672 va
->sys
->totalTextureSlices
);
674 va
->sys
->render
= processorInput
[idx
];
675 free(psz_decoder_name
);
678 free(psz_decoder_name
);
680 msg_Dbg(va
, "Output format from picture source not supported.");
684 static bool CanUseDecoderPadding(directx_sys_t
*dx_sys
)
686 IDXGIAdapter
*pAdapter
= D3D11DeviceAdapter(dx_sys
->d3ddev
);
690 DXGI_ADAPTER_DESC adapterDesc
;
691 HRESULT hr
= IDXGIAdapter_GetDesc(pAdapter
, &adapterDesc
);
692 IDXGIAdapter_Release(pAdapter
);
696 /* Qualcomm hardware has issues with textures and pixels that should not be
697 * part of the decoded area */
698 return adapterDesc
.VendorId
!= 0x4D4F4351;
702 * It creates a Direct3D11 decoder using the given video format
704 static int DxCreateDecoderSurfaces(vlc_va_t
*va
, int codec_id
,
705 const video_format_t
*fmt
, unsigned surface_count
)
707 vlc_va_sys_t
*sys
= va
->sys
;
708 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
711 ID3D10Multithread
*pMultithread
;
712 hr
= ID3D11Device_QueryInterface( dx_sys
->d3ddev
, &IID_ID3D10Multithread
, (void **)&pMultithread
);
714 ID3D10Multithread_SetMultithreadProtected(pMultithread
, TRUE
);
715 ID3D10Multithread_Release(pMultithread
);
719 /* On the Xbox 1/S, any decoding of H264 with one dimension over 2304
720 * crashes totally the device */
721 if (codec_id
== AV_CODEC_ID_H264
&&
722 (fmt
->i_width
> 2304 || fmt
->i_height
> 2304) &&
723 isXboxHardware(dx_sys
->d3ddev
))
725 msg_Warn(va
, "%dx%d resolution not supported by your hardware", fmt
->i_width
, fmt
->i_height
);
729 if ((sys
->textureWidth
!= fmt
->i_width
|| sys
->textureHeight
!= fmt
->i_height
) &&
730 !CanUseDecoderPadding(dx_sys
))
732 msg_Dbg(va
, "mismatching external pool sizes use the internal one %dx%d vs %dx%d",
733 sys
->textureWidth
, sys
->textureHeight
, fmt
->i_width
, fmt
->i_height
);
734 dx_sys
->can_extern_pool
= false;
737 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc
;
738 ZeroMemory(&viewDesc
, sizeof(viewDesc
));
739 viewDesc
.DecodeProfile
= dx_sys
->input
;
740 viewDesc
.ViewDimension
= D3D11_VDOV_DIMENSION_TEXTURE2D
;
742 const d3d_format_t
*textureFmt
= NULL
;
743 for (const d3d_format_t
*output_format
= GetRenderFormatList();
744 output_format
->name
!= NULL
; ++output_format
)
746 if (output_format
->formatTexture
== sys
->render
&&
747 (output_format
->fourcc
== VLC_CODEC_D3D11_OPAQUE
|| output_format
->fourcc
== VLC_CODEC_D3D11_OPAQUE_10B
))
749 textureFmt
= output_format
;
753 if (unlikely(textureFmt
==NULL
))
755 msg_Dbg(va
, "no hardware decoder matching %s", DxgiFormatToStr(sys
->render
));
759 if (dx_sys
->can_extern_pool
)
761 #if !D3D11_DIRECT_DECODE
763 for (surface_idx
= 0; surface_idx
< surface_count
; surface_idx
++) {
764 picture_t
*pic
= decoder_NewPicture( (decoder_t
*) va
->obj
.parent
);
765 sys
->extern_pics
[surface_idx
] = pic
;
766 dx_sys
->hw_surface
[surface_idx
] = NULL
;
769 msg_Warn(va
, "not enough decoder pictures %d out of %d", surface_idx
, surface_count
);
770 dx_sys
->can_extern_pool
= false;
774 D3D11_TEXTURE2D_DESC texDesc
;
775 ID3D11Texture2D_GetDesc(pic
->p_sys
->texture
[KNOWN_DXGI_INDEX
], &texDesc
);
776 if (texDesc
.ArraySize
< surface_count
)
778 msg_Warn(va
, "not enough decoding slices in the texture (%d/%d)",
779 texDesc
.ArraySize
, surface_count
);
780 dx_sys
->can_extern_pool
= false;
783 assert(texDesc
.Format
== sys
->render
);
784 assert(texDesc
.BindFlags
& D3D11_BIND_DECODER
);
786 #if !LIBAVCODEC_VERSION_CHECK( 57, 27, 2, 61, 102 )
787 if (pic
->p_sys
->slice_index
!= surface_idx
)
789 msg_Warn(va
, "d3d11va requires decoding slices to be the first in the texture (%d/%d)",
790 pic
->p_sys
->slice_index
, surface_idx
);
791 dx_sys
->can_extern_pool
= false;
796 viewDesc
.Texture2D
.ArraySlice
= pic
->p_sys
->slice_index
;
797 hr
= ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys
->d3ddec
,
798 pic
->p_sys
->resource
[KNOWN_DXGI_INDEX
],
800 &pic
->p_sys
->decoder
);
802 msg_Warn(va
, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx
, hr
);
803 dx_sys
->can_extern_pool
= false;
807 AllocateShaderView(VLC_OBJECT(va
), dx_sys
->d3ddev
, textureFmt
, pic
->p_sys
->texture
, pic
->p_sys
->slice_index
, pic
->p_sys
->resourceView
);
809 dx_sys
->hw_surface
[surface_idx
] = pic
->p_sys
->decoder
;
812 if (!dx_sys
->can_extern_pool
)
814 for (size_t i
= 0; i
< surface_idx
; ++i
)
816 if (dx_sys
->hw_surface
[i
])
818 ID3D11VideoDecoderOutputView_Release(dx_sys
->hw_surface
[i
]);
819 dx_sys
->hw_surface
[i
] = NULL
;
821 if (sys
->extern_pics
[i
])
823 sys
->extern_pics
[i
]->p_sys
->decoder
= NULL
;
824 picture_Release(sys
->extern_pics
[i
]);
825 sys
->extern_pics
[i
] = NULL
;
831 msg_Dbg(va
, "using external surface pool");
834 if (!dx_sys
->can_extern_pool
)
836 D3D11_TEXTURE2D_DESC texDesc
;
837 ZeroMemory(&texDesc
, sizeof(texDesc
));
838 texDesc
.Width
= sys
->textureWidth
;
839 texDesc
.Height
= sys
->textureHeight
;
840 texDesc
.MipLevels
= 1;
841 texDesc
.Format
= sys
->render
;
842 texDesc
.SampleDesc
.Count
= 1;
843 texDesc
.MiscFlags
= 0;
844 texDesc
.ArraySize
= surface_count
;
845 texDesc
.Usage
= D3D11_USAGE_DEFAULT
;
846 texDesc
.BindFlags
= D3D11_BIND_DECODER
;
847 texDesc
.CPUAccessFlags
= 0;
849 if (DeviceSupportsFormat(dx_sys
->d3ddev
, texDesc
.Format
, D3D11_FORMAT_SUPPORT_SHADER_LOAD
))
850 texDesc
.BindFlags
|= D3D11_BIND_SHADER_RESOURCE
;
852 ID3D11Texture2D
*p_texture
;
853 hr
= ID3D11Device_CreateTexture2D( dx_sys
->d3ddev
, &texDesc
, NULL
, &p_texture
);
855 msg_Err(va
, "CreateTexture2D %d failed. (hr=0x%0lx)", surface_count
, hr
);
859 unsigned surface_idx
;
860 for (surface_idx
= 0; surface_idx
< surface_count
; surface_idx
++) {
861 sys
->extern_pics
[surface_idx
] = NULL
;
862 viewDesc
.Texture2D
.ArraySlice
= surface_idx
;
864 hr
= ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys
->d3ddec
,
865 (ID3D11Resource
*) p_texture
,
867 &dx_sys
->hw_surface
[surface_idx
] );
869 msg_Err(va
, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx
, hr
);
870 ID3D11Texture2D_Release(p_texture
);
874 if (texDesc
.BindFlags
& D3D11_BIND_SHADER_RESOURCE
)
876 ID3D11Texture2D
*textures
[D3D11_MAX_SHADER_VIEW
] = {p_texture
, p_texture
};
877 AllocateShaderView(VLC_OBJECT(va
), dx_sys
->d3ddev
, textureFmt
, textures
, surface_idx
,
878 &sys
->resourceView
[surface_idx
* D3D11_MAX_SHADER_VIEW
]);
882 msg_Dbg(va
, "ID3D11VideoDecoderOutputView succeed with %d surfaces (%dx%d)",
883 surface_count
, fmt
->i_width
, fmt
->i_height
);
885 D3D11_VIDEO_DECODER_DESC decoderDesc
;
886 ZeroMemory(&decoderDesc
, sizeof(decoderDesc
));
887 decoderDesc
.Guid
= dx_sys
->input
;
888 decoderDesc
.SampleWidth
= fmt
->i_width
;
889 decoderDesc
.SampleHeight
= fmt
->i_height
;
890 decoderDesc
.OutputFormat
= sys
->render
;
893 hr
= ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys
->d3ddec
, &decoderDesc
, &cfg_count
);
895 msg_Err(va
, "GetVideoDecoderConfigCount failed. (hr=0x%lX)", hr
);
899 /* List all configurations available for the decoder */
900 D3D11_VIDEO_DECODER_CONFIG cfg_list
[cfg_count
];
901 for (unsigned i
= 0; i
< cfg_count
; i
++) {
902 hr
= ID3D11VideoDevice_GetVideoDecoderConfig( dx_sys
->d3ddec
, &decoderDesc
, i
, &cfg_list
[i
] );
904 msg_Err(va
, "GetVideoDecoderConfig failed. (hr=0x%lX)", hr
);
909 msg_Dbg(va
, "we got %d decoder configurations", cfg_count
);
911 /* Select the best decoder configuration */
913 for (unsigned i
= 0; i
< cfg_count
; i
++) {
914 const D3D11_VIDEO_DECODER_CONFIG
*cfg
= &cfg_list
[i
];
917 msg_Dbg(va
, "configuration[%d] ConfigBitstreamRaw %d",
918 i
, cfg
->ConfigBitstreamRaw
);
922 if (cfg
->ConfigBitstreamRaw
== 1)
924 else if (codec_id
== AV_CODEC_ID_H264
&& cfg
->ConfigBitstreamRaw
== 2)
928 if (IsEqualGUID(&cfg
->guidConfigBitstreamEncryption
, &DXVA2_NoEncrypt
))
931 if (cfg_score
< score
) {
936 if (cfg_score
<= 0) {
937 msg_Err(va
, "Failed to find a supported decoder configuration");
941 /* Create the decoder */
942 ID3D11VideoDecoder
*decoder
;
943 hr
= ID3D11VideoDevice_CreateVideoDecoder( dx_sys
->d3ddec
, &decoderDesc
, &sys
->cfg
, &decoder
);
945 msg_Err(va
, "ID3D11VideoDevice_CreateVideoDecoder failed. (hr=0x%lX)", hr
);
946 dx_sys
->decoder
= NULL
;
949 dx_sys
->decoder
= decoder
;
951 msg_Dbg(va
, "DxCreateDecoderSurfaces succeed");
955 static void DxDestroySurfaces(vlc_va_t
*va
)
957 directx_sys_t
*dx_sys
= &va
->sys
->dx_sys
;
958 if (dx_sys
->va_pool
.surface_count
&& !dx_sys
->can_extern_pool
) {
959 ID3D11Resource
*p_texture
;
960 ID3D11VideoDecoderOutputView_GetResource( dx_sys
->hw_surface
[0], &p_texture
);
961 ID3D11Resource_Release(p_texture
);
962 ID3D11Resource_Release(p_texture
);
964 for (unsigned i
= 0; i
< dx_sys
->va_pool
.surface_count
; i
++)
966 ID3D11VideoDecoderOutputView_Release( dx_sys
->hw_surface
[i
] );
967 for (int j
= 0; j
< D3D11_MAX_SHADER_VIEW
; j
++)
969 if (va
->sys
->resourceView
[i
*D3D11_MAX_SHADER_VIEW
+ j
])
970 ID3D11ShaderResourceView_Release(va
->sys
->resourceView
[i
*D3D11_MAX_SHADER_VIEW
+ j
]);
975 ID3D11VideoDecoder_Release(dx_sys
->decoder
);
976 dx_sys
->decoder
= NULL
;