dxva2: add more known pixel format
[vlc.git] / modules / codec / avcodec / dxva2.c
1 /*****************************************************************************
2  * dxva2.c: Video Acceleration helpers
3  *****************************************************************************
4  * Copyright (C) 2009 Geoffroy Couprie
5  * Copyright (C) 2009 Laurent Aimar
6  *
7  * Authors: Geoffroy Couprie <geal@videolan.org>
8  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30
31 #include <vlc_common.h>
32 #include <vlc_picture.h>
33 #include <vlc_plugin.h>
34
35 #define DXVA2API_USE_BITFIELDS
36 #define COBJMACROS
37 #include <libavcodec/dxva2.h>
38 #include "../../video_chroma/d3d9_fmt.h"
39
40 struct dxva2_pic_context
41 {
42     struct d3d9_pic_context ctx;
43     struct vlc_va_surface_t *va_surface;
44     HINSTANCE               dxva2_dll;
45 };
46
47 #define DXVA2_PICCONTEXT_FROM_PICCTX(pic_ctx)  \
48     container_of((pic_ctx), struct dxva2_pic_context, ctx.s)
49
50 #include "directx_va.h"
51
52 static int Open(vlc_va_t *, AVCodecContext *, enum PixelFormat hwfmt, const AVPixFmtDescriptor *,
53                 const es_format_t *, vlc_decoder_device *, video_format_t *, vlc_video_context **);
54
55 vlc_module_begin()
56     set_description(N_("DirectX Video Acceleration (DXVA) 2.0"))
57     set_category(CAT_INPUT)
58     set_subcategory(SUBCAT_INPUT_VCODEC)
59     set_va_callback(Open, 110)
60 vlc_module_end()
61
62 #include <initguid.h> /* must be last included to not redefine existing GUIDs */
63
64 /* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
65  * assume that they are declared in dxva2api.h */
66 #define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
67
68 #ifdef __MINGW32__
69 # include <_mingw.h>
70
71 # if !defined(__MINGW64_VERSION_MAJOR)
72 #  undef MS_GUID
73 #  define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
74 #  define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
75 # else
76 #  include <dxva.h>
77 # endif
78
79 #endif /* __MINGW32__ */
80
81 MS_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
82 MS_GUID(IID_IDirectXVideoAccelerationService, 0xfc51a550, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
83
84 DEFINE_GUID(DXVA2_NoEncrypt,                        0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
85
86 DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo,       0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
87
88
89 /* */
90 typedef struct {
91     const char   *name;
92     D3DFORMAT    format;
93     vlc_fourcc_t codec;
94 } d3d9_format_t;
95 /* XXX Prefered format must come first */
96 static const d3d9_format_t d3d_formats[] = {
97     { "YV12",   MAKEFOURCC('Y','V','1','2'),    VLC_CODEC_YV12 },
98     { "NV12",   MAKEFOURCC('N','V','1','2'),    VLC_CODEC_NV12 },
99     //{ "IMC3",   MAKEFOURCC('I','M','C','3'),    VLC_CODEC_YV12 },
100     { "P010",   MAKEFOURCC('P','0','1','0'),    VLC_CODEC_P010 },
101     { "AYUV",   MAKEFOURCC('A','Y','U','V'),    VLC_CODEC_YUVA },
102     { "YUY2",   MAKEFOURCC('Y','U','Y','2'),    VLC_CODEC_YUYV },
103     { "Y410",   MAKEFOURCC('Y','4','1','0'),    VLC_CODEC_Y410 },
104     { "Y210",   MAKEFOURCC('Y','2','1','0'),    VLC_CODEC_Y210 },
105
106     { NULL, 0, 0 }
107 };
108
109 static const d3d9_format_t *D3dFindFormat(D3DFORMAT format)
110 {
111     for (unsigned i = 0; d3d_formats[i].name; i++) {
112         if (d3d_formats[i].format == format)
113             return &d3d_formats[i];
114     }
115     return NULL;
116 }
117
118 struct vlc_va_sys_t
119 {
120     /* Direct3D */
121     vlc_video_context      *vctx;
122
123     /* DLL */
124     HINSTANCE              dxva2_dll;
125
126     /* Device manager */
127     IDirect3DDeviceManager9  *devmng;
128     HANDLE                   device;
129
130     /* Video service */
131     D3DFORMAT                    render;
132
133     /* Video decoder */
134     DXVA2_ConfigPictureDecode    cfg;
135     const directx_va_mode_t       *selected_decoder;
136     IDirectXVideoDecoderService  *d3ddec;
137
138     /* pool */
139     va_pool_t           *va_pool;
140     IDirect3DSurface9   *hw_surface[MAX_SURFACE_COUNT];
141
142     /* avcodec internals */
143     struct dxva_context hw;
144 };
145
146
147 /* */
148 static int D3dCreateDevice(vlc_va_t *);
149
150 static int DxGetInputList(vlc_va_t *, input_list_t *);
151 static int DxSetupOutput(vlc_va_t *, const directx_va_mode_t *, const video_format_t *);
152
153 static int DxCreateVideoDecoder(vlc_va_t *, int codec_id,
154                                 const video_format_t *, size_t surface_count);
155 static void DxDestroyVideoDecoder(void *);
156
157 static void SetupAVCodecContext(void *opaque, AVCodecContext *avctx)
158 {
159     vlc_va_sys_t *sys = opaque;
160     sys->hw.cfg = &sys->cfg;
161     sys->hw.surface = sys->hw_surface;
162     sys->hw.workaround = sys->selected_decoder->workaround;
163     avctx->hwaccel_context = &sys->hw;
164 }
165
166 static void dxva2_pic_context_destroy(picture_context_t *ctx)
167 {
168     struct dxva2_pic_context *pic_ctx = DXVA2_PICCONTEXT_FROM_PICCTX(ctx);
169     struct vlc_va_surface_t *va_surface = pic_ctx->va_surface;
170     HINSTANCE dxva2_dll = pic_ctx->dxva2_dll;
171     static_assert(offsetof(struct dxva2_pic_context, ctx.s) == 0,
172         "Cast assumption failure");
173     d3d9_pic_context_destroy(ctx);
174     va_surface_Release(va_surface);
175     FreeLibrary(dxva2_dll);
176 }
177
178 static picture_context_t *dxva2_pic_context_copy(picture_context_t *ctx)
179 {
180     struct dxva2_pic_context *src_ctx = DXVA2_PICCONTEXT_FROM_PICCTX(ctx);
181     struct dxva2_pic_context *pic_ctx = malloc(sizeof(*pic_ctx));
182     if (unlikely(pic_ctx==NULL))
183         return NULL;
184     *pic_ctx = *src_ctx;
185     pic_ctx->dxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
186     vlc_video_context_Hold(pic_ctx->ctx.s.vctx);
187     va_surface_AddRef(pic_ctx->va_surface);
188     AcquireD3D9PictureSys(&pic_ctx->ctx.picsys);
189     return &pic_ctx->ctx.s;
190 }
191
192 static struct dxva2_pic_context *CreatePicContext(IDirect3DSurface9 *surface, vlc_video_context *vctx)
193 {
194     struct dxva2_pic_context *pic_ctx = calloc(1, sizeof(*pic_ctx));
195     if (unlikely(pic_ctx==NULL))
196         return NULL;
197     pic_ctx->ctx.s = (picture_context_t) {
198         dxva2_pic_context_destroy, dxva2_pic_context_copy,
199         vlc_video_context_Hold(vctx),
200     };
201     pic_ctx->ctx.picsys.surface = surface;
202     AcquireD3D9PictureSys(&pic_ctx->ctx.picsys);
203     return pic_ctx;
204 }
205
206 static picture_context_t* NewSurfacePicContext(vlc_va_t *va, vlc_va_surface_t *va_surface)
207 {
208     vlc_va_sys_t *sys = va->sys;
209     struct dxva2_pic_context *pic_ctx = CreatePicContext(sys->hw_surface[va_surface_GetIndex(va_surface)], sys->vctx);
210     if (unlikely(pic_ctx==NULL))
211         return NULL;
212     pic_ctx->va_surface = va_surface;
213     pic_ctx->dxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
214     return &pic_ctx->ctx.s;
215 }
216
217 static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
218 {
219     vlc_va_sys_t *sys = va->sys;
220
221     /* Check the device */
222     HRESULT hr = IDirect3DDeviceManager9_TestDevice(sys->devmng, sys->device);
223     if (FAILED(hr)) {
224         if (hr == DXVA2_E_NEW_VIDEO_DEVICE)
225             msg_Warn(va, "New video device detected.");
226         else
227             msg_Err(va, "device not usable. (hr=0x%lX)", hr);
228         return VLC_EGENERIC;
229     }
230
231     vlc_va_surface_t *va_surface = va_pool_Get(sys->va_pool);
232     if (unlikely(va_surface==NULL))
233         return VLC_ENOITEM;
234
235     pic->context = NewSurfacePicContext(va, va_surface);
236     if (unlikely(pic->context == NULL))
237     {
238         va_surface_Release(va_surface);
239         return VLC_ENOITEM;
240     }
241     *data = (uint8_t*)DXVA2_PICCONTEXT_FROM_PICCTX(pic->context)->ctx.picsys.surface;
242     return VLC_SUCCESS;
243 }
244
245 static void Close(vlc_va_t *va)
246 {
247     vlc_va_sys_t *sys = va->sys;
248
249     if (sys->vctx)
250         vlc_video_context_Release(sys->vctx);
251
252     if (sys->va_pool)
253         va_pool_Close(sys->va_pool);
254 }
255
256 static const struct vlc_va_operations ops = { Get, Close, };
257
258 static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat hwfmt, const AVPixFmtDescriptor *desc,
259                 const es_format_t *fmt_in, vlc_decoder_device *dec_device,
260                 video_format_t *fmt_out, vlc_video_context **vtcx_out)
261 {
262     int err = VLC_EGENERIC;
263
264     if ( hwfmt != AV_PIX_FMT_DXVA2_VLD )
265         return VLC_EGENERIC;
266
267     d3d9_decoder_device_t *d3d9_decoder = GetD3D9OpaqueDevice( dec_device );
268     if ( d3d9_decoder == NULL )
269         return VLC_EGENERIC;
270     assert(d3d9_decoder->d3ddev.dev != NULL); // coming from the decoder device
271
272     ctx->hwaccel_context = NULL;
273
274     vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
275     if (unlikely(sys == NULL))
276         return VLC_ENOMEM;
277
278     sys->vctx = vlc_video_context_Create( dec_device, VLC_VIDEO_CONTEXT_DXVA2,
279                                           sizeof(d3d9_video_context_t), &d3d9_vctx_ops );
280     if (likely(sys->vctx == NULL))
281     {
282         free( sys );
283         return VLC_EGENERIC;
284     }
285
286     va->sys = sys;
287
288     /* Load dll*/
289     sys->dxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
290     if (!sys->dxva2_dll) {
291         msg_Warn(va, "cannot load DXVA2 decoder DLL");
292         if (sys->vctx)
293             vlc_video_context_Release(sys->vctx);
294         free( sys );
295         return VLC_EGENERIC;
296     }
297
298     struct va_pool_cfg pool_cfg = {
299         D3dCreateDevice,
300         DxDestroyVideoDecoder,
301         DxCreateVideoDecoder,
302         SetupAVCodecContext,
303         sys,
304     };
305
306     sys->va_pool = va_pool_Create(va, &pool_cfg);
307     if (sys->va_pool == NULL)
308     {
309         err = VLC_EGENERIC;
310         goto error;
311     }
312
313     video_format_t final_fmt = *fmt_out;
314     static const directx_sys_t dx_sys = { DxGetInputList, DxSetupOutput };
315     sys->selected_decoder = directx_va_Setup(va, &dx_sys, ctx, desc, fmt_in, 0,
316                                              &final_fmt, &sys->hw.surface_count);
317     if (sys->selected_decoder == NULL)
318     {
319         err = VLC_EGENERIC;
320         goto error;
321     }
322
323     if (sys->render == MAKEFOURCC('P','0','1','0'))
324         final_fmt.i_chroma = VLC_CODEC_D3D9_OPAQUE_10B;
325     else
326         final_fmt.i_chroma = VLC_CODEC_D3D9_OPAQUE;
327     err = va_pool_SetupDecoder(va, sys->va_pool, ctx, &final_fmt, sys->hw.surface_count);
328     if (err != VLC_SUCCESS)
329         goto error;
330
331     const D3DADAPTER_IDENTIFIER9 *identifier = &d3d9_decoder->d3ddev.identifier;
332     msg_Info(va, "Using DXVA2 (%.*s, vendor %s(%lx), device %lx, revision %lx)",
333                 (int)sizeof(identifier->Description), identifier->Description,
334                 DxgiVendorStr(identifier->VendorId), identifier->VendorId,
335                 identifier->DeviceId, identifier->Revision);
336
337     d3d9_video_context_t *octx = GetD3D9ContextPrivate(sys->vctx);
338     octx->format = sys->render;
339
340     va->ops = &ops;
341     *fmt_out = final_fmt;
342     *vtcx_out = sys->vctx;
343     return VLC_SUCCESS;
344
345 error:
346     Close(va);
347     return VLC_EGENERIC;
348 }
349 /* */
350
351 /**
352  * It creates a Direct3D device usable for DXVA 2
353  */
354 static int D3dCreateDevice(vlc_va_t *va)
355 {
356     vlc_va_sys_t *sys = va->sys;
357
358     d3d9_decoder_device_t *d3d9_decoder = GetD3D9OpaqueContext(sys->vctx);
359
360     HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
361                                            IDirect3DDeviceManager9 **);
362     CreateDeviceManager9 =
363       (void *)GetProcAddress(sys->dxva2_dll,
364                              "DXVA2CreateDirect3DDeviceManager9");
365
366     if (!CreateDeviceManager9) {
367         msg_Err(va, "cannot load function");
368         return VLC_EGENERIC;
369     }
370     msg_Dbg(va, "got CreateDeviceManager9");
371
372     UINT token;
373     if (FAILED(CreateDeviceManager9(&token, &sys->devmng))) {
374         msg_Err(va, " OurDirect3DCreateDeviceManager9 failed");
375         return VLC_EGENERIC;
376     }
377     msg_Dbg(va, "obtained IDirect3DDeviceManager9");
378
379     HRESULT hr = IDirect3DDeviceManager9_ResetDevice(sys->devmng, d3d9_decoder->d3ddev.dev, token);
380     if (FAILED(hr)) {
381         msg_Err(va, "IDirect3DDeviceManager9_ResetDevice failed: 0x%lX)", hr);
382         IDirect3DDeviceManager9_Release(sys->devmng);
383         return VLC_EGENERIC;
384     }
385
386     hr = IDirect3DDeviceManager9_OpenDeviceHandle(sys->devmng, &sys->device);
387     if (FAILED(hr)) {
388         msg_Err(va, "OpenDeviceHandle failed");
389         IDirect3DDeviceManager9_Release(sys->devmng);
390         return VLC_EGENERIC;
391     }
392
393     void *pv;
394     hr = IDirect3DDeviceManager9_GetVideoService(sys->devmng, sys->device,
395                                         &IID_IDirectXVideoDecoderService, &pv);
396     if (FAILED(hr)) {
397         msg_Err(va, "GetVideoService failed");
398         IDirect3DDeviceManager9_CloseDeviceHandle(sys->devmng, sys->device);
399         IDirect3DDeviceManager9_Release(sys->devmng);
400         return VLC_EGENERIC;
401     }
402     sys->d3ddec = pv;
403
404     return VLC_SUCCESS;
405 }
406
407 static void ReleaseInputList(input_list_t *p_list)
408 {
409     CoTaskMemFree(p_list->list);
410 }
411
412 static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
413 {
414     vlc_va_sys_t *sys = va->sys;
415     UINT input_count = 0;
416     GUID *input_list = NULL;
417     if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(sys->d3ddec,
418                                                                  &input_count,
419                                                                  &input_list))) {
420         msg_Err(va, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
421         return VLC_EGENERIC;
422     }
423
424     p_list->count = input_count;
425     p_list->list = input_list;
426     p_list->pf_release = ReleaseInputList;
427     return VLC_SUCCESS;
428 }
429
430 static int DxSetupOutput(vlc_va_t *va, const directx_va_mode_t *mode, const video_format_t *fmt)
431 {
432     VLC_UNUSED(fmt);
433     vlc_va_sys_t *sys = va->sys;
434
435     d3d9_decoder_device_t *d3d9_decoder = GetD3D9OpaqueContext(sys->vctx);
436
437     const D3DADAPTER_IDENTIFIER9 *identifier = &d3d9_decoder->d3ddev.identifier;
438     UINT driverBuild = identifier->DriverVersion.LowPart & 0xFFFF;
439     if (identifier->VendorId == GPU_MANUFACTURER_INTEL && (identifier->DriverVersion.LowPart >> 16) >= 100)
440     {
441         /* new Intel driver format */
442         driverBuild += ((identifier->DriverVersion.LowPart >> 16) - 100) * 1000;
443     }
444     if (!directx_va_canUseDecoder(va, identifier->VendorId, identifier->DeviceId,
445                                   mode->guid, driverBuild))
446     {
447         msg_Warn(va, "GPU blacklisted for %s codec", mode->name);
448         return VLC_EGENERIC;
449     }
450
451     int err = VLC_EGENERIC;
452     UINT      output_count = 0;
453     D3DFORMAT *output_list = NULL;
454     if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(sys->d3ddec,
455                                                                    mode->guid,
456                                                                    &output_count,
457                                                                    &output_list))) {
458         msg_Err(va, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
459         return VLC_EGENERIC;
460     }
461
462     for (unsigned j = 0; j < output_count; j++) {
463         const D3DFORMAT f = output_list[j];
464         const d3d9_format_t *format = D3dFindFormat(f);
465         if (format) {
466             msg_Dbg(va, "%s is supported for output", format->name);
467         } else {
468             msg_Dbg(va, "%d is supported for output (%4.4s)", f, (const char*)&f);
469         }
470     }
471
472     D3DFORMAT preferredOutput;
473     if (mode->bit_depth > 8)
474         preferredOutput = MAKEFOURCC('P','0','1','0');
475     else
476         preferredOutput =  MAKEFOURCC('N','V','1','2');
477     msg_Dbg(va, "favor decoder format %4.4s (for 4:%d:%d %d bits)", (const char*)&preferredOutput,
478             (2-mode->log2_chroma_w)*2, (2-mode->log2_chroma_w-mode->log2_chroma_h)*2, mode->bit_depth);
479
480     /* */
481     for (unsigned pass = 0; pass < 2 && err != VLC_SUCCESS; ++pass)
482     {
483         for (unsigned j = 0; d3d_formats[j].name; j++) {
484             const d3d9_format_t *format = &d3d_formats[j];
485
486             /* */
487             bool is_supported = false;
488             for (unsigned k = 0; !is_supported && k < output_count; k++) {
489                 is_supported = format->format == output_list[k];
490             }
491             if (!is_supported)
492                 continue;
493             if (pass == 0 && format->format != preferredOutput)
494                 continue;
495
496             /* We have our solution */
497             msg_Dbg(va, "Using decoder output '%s'", format->name);
498             sys->render = format->format;
499             err = VLC_SUCCESS;
500             break;
501         }
502     }
503     CoTaskMemFree(output_list);
504     return err;
505 }
506
507 /**
508  * It creates a DXVA2 decoder using the given video format
509  */
510 static int DxCreateVideoDecoder(vlc_va_t *va, int codec_id,
511                                 const video_format_t *fmt, size_t surface_count)
512 {
513     vlc_va_sys_t *sys = va->sys;
514     HRESULT hr;
515
516     hr = IDirectXVideoDecoderService_CreateSurface(sys->d3ddec,
517                                                          fmt->i_width,
518                                                          fmt->i_height,
519                                                          surface_count - 1,
520                                                          sys->render,
521                                                          D3DPOOL_DEFAULT,
522                                                          0,
523                                                          DXVA2_VideoDecoderRenderTarget,
524                                                          sys->hw_surface,
525                                                          NULL);
526     if (FAILED(hr)) {
527         msg_Err(va, "IDirectXVideoAccelerationService_CreateSurface %zu failed (hr=0x%lX)", surface_count - 1, hr);
528         return VLC_EGENERIC;
529     }
530     msg_Dbg(va, "IDirectXVideoAccelerationService_CreateSurface succeed with %zu surfaces (%dx%d)",
531             surface_count, fmt->i_width, fmt->i_height);
532
533     IDirect3DSurface9 *tstCrash;
534     hr = IDirectXVideoDecoderService_CreateSurface(sys->d3ddec,
535                                                          fmt->i_width,
536                                                          fmt->i_height,
537                                                          0,
538                                                          sys->render,
539                                                          D3DPOOL_DEFAULT,
540                                                          0,
541                                                          DXVA2_VideoDecoderRenderTarget,
542                                                          &tstCrash,
543                                                          NULL);
544     if (FAILED(hr)) {
545         msg_Err(va, "extra buffer impossible, avoid a crash (hr=0x%lX)", hr);
546         goto error;
547     }
548     IDirect3DSurface9_Release(tstCrash);
549
550     /* */
551     DXVA2_VideoDesc dsc;
552     ZeroMemory(&dsc, sizeof(dsc));
553     dsc.SampleWidth     = fmt->i_width;
554     dsc.SampleHeight    = fmt->i_height;
555     dsc.Format          = sys->render;
556     if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
557         dsc.InputSampleFreq.Numerator   = fmt->i_frame_rate;
558         dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
559     } else {
560         dsc.InputSampleFreq.Numerator   = 0;
561         dsc.InputSampleFreq.Denominator = 0;
562     }
563     dsc.OutputFrameFreq = dsc.InputSampleFreq;
564     dsc.UABProtectionLevel = FALSE;
565     dsc.Reserved = 0;
566
567     /* FIXME I am unsure we can let unknown everywhere */
568     DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
569     ext->SampleFormat = 0;//DXVA2_SampleUnknown;
570     ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
571     ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
572     ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
573     ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
574     ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
575     ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
576
577     /* List all configurations available for the decoder */
578     UINT                      cfg_count = 0;
579     DXVA2_ConfigPictureDecode *cfg_list = NULL;
580     hr = IDirectXVideoDecoderService_GetDecoderConfigurations(sys->d3ddec,
581                                                               sys->selected_decoder->guid,
582                                                               &dsc,
583                                                               NULL,
584                                                               &cfg_count,
585                                                               &cfg_list);
586     if (FAILED(hr)) {
587         msg_Err(va, "IDirectXVideoDecoderService_GetDecoderConfigurations failed. (hr=0x%lX)", hr);
588         goto error;
589     }
590     msg_Dbg(va, "we got %d decoder configurations", cfg_count);
591
592     /* Select the best decoder configuration */
593     int cfg_score = 0;
594     for (unsigned i = 0; i < cfg_count; i++) {
595         const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
596
597         /* */
598         msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
599                 i, cfg->ConfigBitstreamRaw);
600
601         /* */
602         int score;
603         if (cfg->ConfigBitstreamRaw == 1)
604             score = 1;
605         else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
606             score = 2;
607         else
608             continue;
609         if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
610             score += 16;
611
612         if (cfg_score < score) {
613             sys->cfg = *cfg;
614             cfg_score = score;
615         }
616     }
617     CoTaskMemFree(cfg_list);
618     if (cfg_score <= 0) {
619         msg_Err(va, "Failed to find a supported decoder configuration");
620         goto error;
621     }
622
623     /* Create the decoder */
624     /* adds a reference on each decoder surface */
625     if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(sys->d3ddec,
626                                                               sys->selected_decoder->guid,
627                                                               &dsc,
628                                                               &sys->cfg,
629                                                               sys->hw_surface,
630                                                               surface_count,
631                                                               &sys->hw.decoder))) {
632         msg_Err(va, "IDirectXVideoDecoderService_CreateVideoDecoder failed");
633         goto error;
634     }
635
636     msg_Dbg(va, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
637     return VLC_SUCCESS;
638 error:
639     for (size_t i = 0; i < surface_count; i++)
640         IDirect3DSurface9_Release( sys->hw_surface[i] );
641     return VLC_EGENERIC;
642 }
643
644 static void DxDestroyVideoDecoder(void *opaque)
645 {
646     vlc_va_sys_t *sys = opaque;
647     /* releases a reference on each decoder surface */
648     if (sys->hw.decoder)
649         IDirectXVideoDecoder_Release(sys->hw.decoder);
650     if (sys->hw_surface[0])
651     {
652         for (unsigned i = 0; i < sys->hw.surface_count; i++)
653             IDirect3DSurface9_Release(sys->hw_surface[i]);
654     }
655     IDirect3DDeviceManager9_CloseDeviceHandle(sys->devmng, sys->device);
656     IDirectXVideoDecoderService_Release(sys->d3ddec);
657     IDirect3DDeviceManager9_Release(sys->devmng);
658     if (sys->dxva2_dll)
659         FreeLibrary(sys->dxva2_dll);
660
661     free(sys);
662 }