]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/dxva2.c
Prefered DXVA2_ConfigPictureDecode with guidConfigBitstreamEncryption equals to DXVA_...
[vlc] / modules / codec / avcodec / dxva2.c
1 /*****************************************************************************
2  * va.c: Video Acceleration helpers
3  *****************************************************************************
4  * Copyright (C) 2009 Geoffroy Couprie
5  * Copyright (C) 2009 Laurent Aimar
6  * $Id$
7  *
8  * Authors: Geoffroy Couprie <geal@videolan.org>
9  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_picture.h>
32 #include <vlc_fourcc.h>
33 #include <vlc_cpu.h>
34 #include <assert.h>
35
36 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
37 #   include <libavcodec/avcodec.h>
38 #   ifdef HAVE_AVCODEC_DXVA2
39 #       define DXVA2API_USE_BITFIELDS
40 #       include <libavcodec/dxva2.h>
41 #   endif
42 #elif defined(HAVE_FFMPEG_AVCODEC_H)
43 #   include <ffmpeg/avcodec.h>
44 #else
45 #   include <avcodec.h>
46 #endif
47
48 #include "avcodec.h"
49 #include "va.h"
50 #include "copy.h"
51
52 #ifdef HAVE_AVCODEC_DXVA2
53
54 #include <windows.h>
55 #include <windowsx.h>
56 #include <ole2.h>
57 #include <commctrl.h>
58 #include <shlwapi.h>
59 #include <d3d9.h>
60
61 /* */
62 #define DXVA2_E_NOT_INITIALIZED     MAKE_HRESULT(1, 4, 4096)
63 #define DXVA2_E_NEW_VIDEO_DEVICE    MAKE_HRESULT(1, 4, 4097)
64 #define DXVA2_E_VIDEO_DEVICE_LOCKED MAKE_HRESULT(1, 4, 4098)
65 #define DXVA2_E_NOT_AVAILABLE       MAKE_HRESULT(1, 4, 4099)
66
67 static const GUID DXVA2_ModeMPEG2_MoComp = {
68     0xe6a9f44b, 0x61b0, 0x4563, {0x9e,0xa4,0x63,0xd2,0xa3,0xc6,0xfe,0x66}
69 };
70 static const GUID DXVA2_ModeMPEG2_IDCT = {
71   0xbf22ad00, 0x03ea, 0x4690, {0x80,0x77,0x47,0x33,0x46,0x20,0x9b,0x7e}
72 };
73 static const GUID DXVA2_ModeMPEG2_VLD = {
74    0xee27417f, 0x5e28, 0x4e65, {0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9}
75 };
76
77 static const GUID DXVA2_ModeH264_A = {
78     0x1b81be64, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
79 };
80 static const GUID DXVA2_ModeH264_B = {
81     0x1b81be65, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
82 };
83 static const GUID DXVA2_ModeH264_C = {
84     0x1b81be66, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
85 };
86 static const GUID DXVA2_ModeH264_D = {
87     0x1b81be67, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
88 };
89 static const GUID DXVA2_ModeH264_E = {
90     0x1b81be68, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
91 };
92 static const GUID DXVA2_ModeH264_F = {
93     0x1b81be69, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
94 };
95 static const GUID DXVADDI_Intel_ModeH264_A = {
96     0x604F8E64, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
97 };
98 static const GUID DXVADDI_Intel_ModeH264_C = {
99     0x604F8E66,0x4951, 0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
100 };
101 static const GUID DXVADDI_Intel_ModeH264_E = {
102     0x604F8E68,0x4951, 0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
103 };
104 static const GUID DXVA2_ModeWMV8_A = {
105     0x1b81be80, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
106 };
107 static const GUID DXVA2_ModeWMV8_B = {
108     0x1b81be81, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
109 };
110 static const GUID DXVA2_ModeWMV9_A = {
111     0x1b81be90, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
112 };
113 static const GUID DXVA2_ModeWMV9_B = {
114     0x1b81be91, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
115 };
116 static const GUID DXVA2_ModeWMV9_C = {
117     0x1b81be94, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
118 };
119
120 static const GUID DXVA2_ModeVC1_A = {
121     0x1b81beA0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
122 };
123 static const GUID DXVA2_ModeVC1_B = {
124     0x1b81beA1, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
125 };
126 static const GUID DXVA2_ModeVC1_C = {
127     0x1b81beA2, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
128 };
129 static const GUID DXVA2_ModeVC1_D = {
130     0x1b81beA3, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
131 };
132
133 static const GUID DXVA_NoEncrypt = {
134     0x1b81bed0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
135 };
136
137 /* */
138 typedef struct {
139     const char   *name;
140     const GUID   *guid;
141     int          codec;
142 } dxva2_mode_t;
143 /* XXX Prefered modes must come first */
144 static const dxva2_mode_t dxva2_modes[] = {
145     { "DXVA2_ModeMPEG2_VLD",    &DXVA2_ModeMPEG2_VLD,     CODEC_ID_MPEG2VIDEO },
146     { "DXVA2_ModeMPEG2_MoComp", &DXVA2_ModeMPEG2_MoComp,  0 },
147     { "DXVA2_ModeMPEG2_IDCT",   &DXVA2_ModeMPEG2_IDCT,    0 },
148
149     { "H.264 variable-length decoder (VLD), FGT",               &DXVA2_ModeH264_F,         CODEC_ID_H264 },
150     { "H.264 VLD, no FGT",                                      &DXVA2_ModeH264_E,         CODEC_ID_H264 },
151     { "H.264 VLD, no FGT (Intel)",                              &DXVADDI_Intel_ModeH264_E, CODEC_ID_H264 },
152     { "H.264 IDCT, FGT",                                        &DXVA2_ModeH264_D,         0             },
153     { "H.264 inverse discrete cosine transform (IDCT), no FGT", &DXVA2_ModeH264_C,         0             },
154     { "H.264 inverse discrete cosine transform (IDCT), no FGT (Intel)", &DXVADDI_Intel_ModeH264_C, 0     },
155     { "H.264 MoComp, FGT",                                      &DXVA2_ModeH264_B,         0             },
156     { "H.264 motion compensation (MoComp), no FGT",             &DXVA2_ModeH264_A,         0             },
157     { "H.264 motion compensation (MoComp), no FGT (Intel)",     &DXVADDI_Intel_ModeH264_A, 0             },
158
159     { "Windows Media Video 8 MoComp",           &DXVA2_ModeWMV8_B, 0 },
160     { "Windows Media Video 8 post processing",  &DXVA2_ModeWMV8_A, 0 },
161
162     {  "Windows Media Video 9 IDCT",            &DXVA2_ModeWMV9_C, 0 },
163     {  "Windows Media Video 9 MoComp",          &DXVA2_ModeWMV9_B, 0 },
164     {  "Windows Media Video 9 post processing", &DXVA2_ModeWMV9_A, 0 },
165
166     { "VC-1 VLD",             &DXVA2_ModeVC1_D, CODEC_ID_VC1 },
167     { "VC-1 VLD",             &DXVA2_ModeVC1_D, CODEC_ID_WMV3 },
168     { "VC-1 IDCT",            &DXVA2_ModeVC1_C, 0 },
169     { "VC-1 MoComp",          &DXVA2_ModeVC1_B, 0 },
170     { "VC-1 post processing", &DXVA2_ModeVC1_A, 0 },
171
172     { NULL, NULL, 0 }
173 };
174
175 static const dxva2_mode_t *Dxva2FindMode(const GUID *guid)
176 {
177     for (unsigned i = 0; dxva2_modes[i].name; i++) {
178         if (IsEqualGUID(dxva2_modes[i].guid, guid))
179             return &dxva2_modes[i];
180     }
181     return NULL;
182 }
183
184 /* */
185 typedef struct {
186     const char   *name;
187     D3DFORMAT    format;
188     vlc_fourcc_t codec;
189 } d3d_format_t;
190 /* XXX Prefered format must come first */
191 static const d3d_format_t d3d_formats[] = {
192     { "YV12",   MAKEFOURCC('Y','V','1','2'),    VLC_CODEC_YV12 },
193     { "NV12",   MAKEFOURCC('N','V','1','2'),    VLC_CODEC_NV12 },
194
195     { NULL, 0, 0 }
196 };
197
198 static const d3d_format_t *D3dFindFormat(D3DFORMAT format)
199 {
200     for (unsigned i = 0; d3d_formats[i].name; i++) {
201         if (d3d_formats[i].format == format)
202             return &d3d_formats[i];
203     }
204     return NULL;
205 }
206
207 static const GUID IID_IDirectXVideoDecoderService = {
208     0xfc51a551, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
209 };
210 static const GUID IID_IDirectXVideoAccelerationService = {
211     0xfc51a550, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
212 };
213
214 /* */
215 typedef struct {
216     LPDIRECT3DSURFACE9 d3d;
217     int                refcount;
218     unsigned int       order;
219 } vlc_va_surface_t;
220
221 #define VA_DXVA2_MAX_SURFACE_COUNT (64)
222 typedef struct
223 {
224     /* */
225     vlc_va_t va;
226
227     /* */
228     vlc_object_t *log;
229     int          codec_id;
230
231     /* DLL */
232         HINSTANCE             hd3d9_dll;
233     HINSTANCE             hdxva2_dll;
234
235     /* Direct3D */
236     D3DPRESENT_PARAMETERS  d3dpp;
237     LPDIRECT3D9            d3dobj;
238     D3DADAPTER_IDENTIFIER9 d3dai;
239     LPDIRECT3DDEVICE9      d3ddev;
240
241     /* Device manager */
242     UINT                     token;
243     IDirect3DDeviceManager9  *devmng;
244     HANDLE                   device;
245
246     /* Video service */
247     IDirectXVideoDecoderService  *vs;
248     GUID                         input;
249     D3DFORMAT                    render;
250
251     /* Video decoder */
252     DXVA2_ConfigPictureDecode    cfg;
253     IDirectXVideoDecoder         *decoder;
254
255     /* Option conversion */
256     D3DFORMAT                    output;
257     copy_cache_t                 surface_cache;
258
259     /* */
260     struct dxva_context hw;
261
262     /* */
263     unsigned     surface_count;
264     unsigned     surface_order;
265     int          surface_width;
266     int          surface_height;
267     vlc_fourcc_t surface_chroma;
268
269     vlc_va_surface_t surface[VA_DXVA2_MAX_SURFACE_COUNT];
270     LPDIRECT3DSURFACE9 hw_surface[VA_DXVA2_MAX_SURFACE_COUNT];
271 } vlc_va_dxva2_t;
272
273 /* */
274 static vlc_va_dxva2_t *vlc_va_dxva2_Get(void *external)
275 {
276     assert(external == (void*)(&((vlc_va_dxva2_t*)external)->va));
277     return external;
278 }
279
280 /* */
281 static int D3dCreateDevice(vlc_va_dxva2_t *);
282 static void D3dDestroyDevice(vlc_va_dxva2_t *);
283 static char *DxDescribe(vlc_va_dxva2_t *);
284
285 static int D3dCreateDeviceManager(vlc_va_dxva2_t *);
286 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *);
287
288 static int DxCreateVideoService(vlc_va_dxva2_t *);
289 static void DxDestroyVideoService(vlc_va_dxva2_t *);
290 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *, GUID *input, D3DFORMAT *output);
291
292 static int DxCreateVideoDecoder(vlc_va_dxva2_t *,
293                                 int codec_id, const video_format_t *);
294 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *);
295 static int DxResetVideoDecoder(vlc_va_dxva2_t *);
296
297 static void DxCreateVideoConversion(vlc_va_dxva2_t *);
298 static void DxDestroyVideoConversion(vlc_va_dxva2_t *);
299
300 /* */
301 static int Setup(vlc_va_t *external, void **hw, vlc_fourcc_t *chroma,
302                  int width, int height)
303 {
304     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
305
306     if (va->surface_width  == width &&
307         va->surface_height == height)
308         goto ok;
309
310     /* */
311     DxDestroyVideoConversion(va);
312     DxDestroyVideoDecoder(va);
313
314     *hw = NULL;
315     *chroma = 0;
316     if (width <= 0 || height <= 0)
317         return VLC_EGENERIC;
318
319     /* FIXME transmit a video_format_t by VaSetup directly */
320     video_format_t fmt;
321     memset(&fmt, 0, sizeof(fmt));
322     fmt.i_width = width;
323     fmt.i_height = height;
324
325     if (DxCreateVideoDecoder(va, va->codec_id, &fmt))
326         return VLC_EGENERIC;
327     /* */
328     va->hw.decoder = va->decoder;
329     va->hw.cfg = &va->cfg;
330     va->hw.surface_count = va->surface_count;
331     va->hw.surface = va->hw_surface;
332     for (unsigned i = 0; i < va->surface_count; i++)
333         va->hw.surface[i] = va->surface[i].d3d;
334
335     /* */
336     DxCreateVideoConversion(va);
337
338     /* */
339 ok:
340     *hw = &va->hw;
341     const d3d_format_t *output = D3dFindFormat(va->output);
342     *chroma = output->codec;
343
344     return VLC_SUCCESS;
345 }
346
347 static int Extract(vlc_va_t *external, picture_t *picture, AVFrame *ff)
348 {
349     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
350     LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
351
352     if (!va->surface_cache.buffer)
353         return VLC_EGENERIC;
354
355     /* */
356     assert(va->output == MAKEFOURCC('Y','V','1','2'));
357
358     /* */
359     D3DLOCKED_RECT lock;
360     if (FAILED(IDirect3DSurface9_LockRect(d3d, &lock, NULL, D3DLOCK_READONLY))) {
361         msg_Err(va->log, "Failed to lock surface");
362         return VLC_EGENERIC;
363     }
364
365     if (va->render == MAKEFOURCC('Y','V','1','2')) {
366         uint8_t *plane[3] = {
367             lock.pBits,
368             (uint8_t*)lock.pBits + lock.Pitch * va->surface_height,
369             (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
370                                  + (lock.Pitch/2) * (va->surface_height/2)
371         };
372         size_t  pitch[3] = {
373             lock.Pitch,
374             lock.Pitch / 2,
375             lock.Pitch / 2,
376         };
377         CopyFromYv12(picture, plane, pitch,
378                      va->surface_width, va->surface_height,
379                      &va->surface_cache);
380     } else {
381         assert(va->render == MAKEFOURCC('N','V','1','2'));
382         uint8_t *plane[2] = {
383             lock.pBits,
384             (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
385         };
386         size_t  pitch[2] = {
387             lock.Pitch,
388             lock.Pitch,
389         };
390         CopyFromNv12(picture, plane, pitch,
391                      va->surface_width, va->surface_height,
392                      &va->surface_cache);
393     }
394
395     /* */
396     IDirect3DSurface9_UnlockRect(d3d);
397     return VLC_SUCCESS;
398 }
399 /* FIXME it is nearly common with VAAPI */
400 static int Get(vlc_va_t *external, AVFrame *ff)
401 {
402     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
403
404     /* Check the device */
405     HRESULT hr = IDirect3DDeviceManager9_TestDevice(va->devmng, va->device);
406     if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
407         if (DxResetVideoDecoder(va))
408             return VLC_EGENERIC;
409     } else if (FAILED(hr)) {
410         msg_Err(va->log, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
411         return VLC_EGENERIC;
412     }
413
414     /* Grab an unused surface, in case none are, try the oldest
415      * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
416     unsigned i, old;
417     for (i = 0, old = 0; i < va->surface_count; i++) {
418         vlc_va_surface_t *surface = &va->surface[i];
419
420         if (!surface->refcount)
421             break;
422
423         if (surface->order < va->surface[old].order)
424             old = i;
425     }
426     if (i >= va->surface_count)
427         i = old;
428
429     vlc_va_surface_t *surface = &va->surface[i];
430
431     surface->refcount = 1;
432     surface->order = va->surface_order++;
433
434     /* */
435     for (int i = 0; i < 4; i++) {
436         ff->data[i] = NULL;
437         ff->linesize[i] = 0;
438
439         if (i == 0 || i == 3)
440             ff->data[i] = (void*)surface->d3d;/* Yummie */
441     }
442     return VLC_SUCCESS;
443 }
444 static void Release(vlc_va_t *external, AVFrame *ff)
445 {
446     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
447     LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
448
449     for (unsigned i = 0; i < va->surface_count; i++) {
450         vlc_va_surface_t *surface = &va->surface[i];
451
452         if (surface->d3d == d3d)
453             surface->refcount--;
454     }
455 }
456 static void Close(vlc_va_t *external)
457 {
458     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
459
460     DxDestroyVideoConversion(va);
461     DxDestroyVideoDecoder(va);
462     DxDestroyVideoService(va);
463     D3dDestroyDeviceManager(va);
464     D3dDestroyDevice(va);
465
466     if (va->hdxva2_dll)
467         FreeLibrary(va->hdxva2_dll);
468     if (va->hd3d9_dll)
469         FreeLibrary(va->hd3d9_dll);
470
471     free(va->va.description);
472     free(va);
473 }
474
475 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
476 {
477     vlc_va_dxva2_t *va = calloc(1, sizeof(*va));
478     if (!va)
479         return NULL;
480
481     /* */
482     va->log = log;
483     va->codec_id = codec_id;
484
485     /* Load dll*/
486     va->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
487     if (!va->hd3d9_dll) {
488         msg_Warn(va->log, "cannot load d3d9.dll");
489         goto error;
490     }
491     va->hdxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
492     if (!va->hdxva2_dll) {
493         msg_Warn(va->log, "cannot load dxva2.dll");
494         goto error;
495     }
496     msg_Dbg(va->log, "DLLs loaded");
497
498     /* */
499     if (D3dCreateDevice(va)) {
500         msg_Err(va->log, "Failed to create Direct3D device");
501         goto error;
502     }
503     msg_Dbg(va->log, "D3dCreateDevice succeed");
504
505     if (D3dCreateDeviceManager(va)) {
506         msg_Err(va->log, "D3dCreateDeviceManager failed");
507         goto error;
508     }
509
510     if (DxCreateVideoService(va)) {
511         msg_Err(va->log, "DxCreateVideoService failed");
512         goto error;
513     }
514
515     /* */
516     if (DxFindVideoServiceConversion(va, &va->input, &va->render)) {
517         msg_Err(va->log, "DxFindVideoServiceConversion failed");
518         goto error;
519     }
520
521     /* TODO print the hardware name/vendor for debugging purposes */
522     va->va.description = DxDescribe(va);
523     va->va.setup   = Setup;
524     va->va.get     = Get;
525     va->va.release = Release;
526     va->va.extract = Extract;
527     va->va.close   = Close;
528     return &va->va;
529
530 error:
531     Close(&va->va);
532     return NULL;
533 }
534 /* */
535
536 /**
537  * It creates a Direct3D device usable for DXVA 2
538  */
539 static int D3dCreateDevice(vlc_va_dxva2_t *va)
540 {
541     /* */
542     LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
543     Create9 = (void *)GetProcAddress(va->hd3d9_dll,
544                                      TEXT("Direct3DCreate9"));
545     if (!Create9) {
546         msg_Err(va->log, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
547         return VLC_EGENERIC;
548     }
549
550     /* */
551     LPDIRECT3D9 d3dobj;
552     d3dobj = Create9(D3D_SDK_VERSION);
553     if (!d3dobj) {
554         msg_Err(va->log, "Direct3DCreate9 failed");
555         return VLC_EGENERIC;
556     }
557     va->d3dobj = d3dobj;
558
559     /* */
560     D3DADAPTER_IDENTIFIER9 *d3dai = &va->d3dai;
561     if (FAILED(IDirect3D9_GetAdapterIdentifier(va->d3dobj,
562                                                D3DADAPTER_DEFAULT, 0, d3dai))) {
563         msg_Warn(va->log, "IDirect3D9_GetAdapterIdentifier failed");
564         ZeroMemory(d3dai, sizeof(*d3dai));
565     }
566
567     /* */
568     D3DPRESENT_PARAMETERS *d3dpp = &va->d3dpp;
569     ZeroMemory(d3dpp, sizeof(*d3dpp));
570     d3dpp->Flags                  = D3DPRESENTFLAG_VIDEO;
571     d3dpp->Windowed               = TRUE;
572     d3dpp->hDeviceWindow          = NULL;
573     d3dpp->SwapEffect             = D3DSWAPEFFECT_DISCARD;
574     d3dpp->MultiSampleType        = D3DMULTISAMPLE_NONE;
575     d3dpp->PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
576     d3dpp->BackBufferCount        = 0;                  /* FIXME what to put here */
577     d3dpp->BackBufferFormat       = D3DFMT_X8R8G8B8;    /* FIXME what to put here */
578     d3dpp->BackBufferWidth        = 0;
579     d3dpp->BackBufferHeight       = 0;
580     d3dpp->EnableAutoDepthStencil = FALSE;
581
582     /* Direct3D needs a HWND to create a device, even without using ::Present
583     this HWND is used to alert Direct3D when there's a change of focus window.
584     For now, use GetShellWindow, as it looks harmless */
585     LPDIRECT3DDEVICE9 d3ddev;
586     if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
587                                        D3DDEVTYPE_HAL, GetShellWindow(),
588                                        D3DCREATE_SOFTWARE_VERTEXPROCESSING |
589                                        D3DCREATE_MULTITHREADED,
590                                        d3dpp, &d3ddev))) {
591         msg_Err(va->log, "IDirect3D9_CreateDevice failed");
592         return VLC_EGENERIC;
593     }
594     va->d3ddev = d3ddev;
595
596     return VLC_SUCCESS;
597 }
598 /**
599  * It releases a Direct3D device and its resources.
600  */
601 static void D3dDestroyDevice(vlc_va_dxva2_t *va)
602 {
603     if (va->d3ddev)
604         IDirect3DDevice9_Release(va->d3ddev);
605     if (va->d3dobj)
606         IDirect3D9_Release(va->d3dobj);
607 }
608 /**
609  * It describes our Direct3D object
610  */
611 static char *DxDescribe(vlc_va_dxva2_t *va)
612 {
613     static const struct {
614         unsigned id;
615         char     name[32];
616     } vendors [] = {
617         { 0x1002, "ATI" },
618         { 0x10DE, "NVIDIA" },
619         { 0x8086, "Intel" },
620         { 0x5333, "S3 Graphics" },
621         { 0, "" }
622     };
623     D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
624
625     const char *vendor = "Unknown";
626     for (int i = 0; vendors[i].id != 0; i++) {
627         if (vendors[i].id == id->VendorId) {
628             vendor = vendors[i].name;
629             break;
630         }
631     }
632
633     char *description;
634     if (asprintf(&description, "DXVA2 (%.*s, vendor %d(%s), device %d, revision %d)",
635                  sizeof(id->Description), id->Description,
636                  id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
637         return NULL;
638     return description;
639 }
640
641 /**
642  * It creates a Direct3D device manager
643  */
644 static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
645 {
646     HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
647                                            IDirect3DDeviceManager9 **);
648     CreateDeviceManager9 =
649       (void *)GetProcAddress(va->hdxva2_dll,
650                              TEXT("DXVA2CreateDirect3DDeviceManager9"));
651
652     if (!CreateDeviceManager9) {
653         msg_Err(va->log, "cannot load function\n");
654         return VLC_EGENERIC;
655     }
656     msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
657
658     UINT token;
659     IDirect3DDeviceManager9 *devmng;
660     if (FAILED(CreateDeviceManager9(&token, &devmng))) {
661         msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
662         return VLC_EGENERIC;
663     }
664     va->token  = token;
665     va->devmng = devmng;
666     msg_Info(va->log, "obtained IDirect3DDeviceManager9");
667
668     HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
669     if (FAILED(hr)) {
670         msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
671         return VLC_EGENERIC;
672     }
673     return VLC_SUCCESS;
674 }
675 /**
676  * It destroys a Direct3D device manager
677  */
678 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
679 {
680     if (va->devmng)
681         IDirect3DDeviceManager9_Release(va->devmng);
682 }
683
684 /**
685  * It creates a DirectX video service
686  */
687 static int DxCreateVideoService(vlc_va_dxva2_t *va)
688 {
689     HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
690                                          REFIID riid,
691                                          void **ppService);
692     CreateVideoService =
693       (void *)GetProcAddress(va->hdxva2_dll,
694                              TEXT("DXVA2CreateVideoService"));
695
696     if (!CreateVideoService) {
697         msg_Err(va->log, "cannot load function\n");
698         return 4;
699     }
700     msg_Info(va->log, "DXVA2CreateVideoService Success!");
701
702     HRESULT hr;
703
704     HANDLE device;
705     hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
706     if (FAILED(hr)) {
707         msg_Err(va->log, "OpenDeviceHandle failed");
708         return VLC_EGENERIC;
709     }
710     va->device = device;
711
712     IDirectXVideoDecoderService *vs;
713     hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
714                                                  &IID_IDirectXVideoDecoderService,
715                                                  &vs);
716     if (FAILED(hr)) {
717         msg_Err(va->log, "GetVideoService failed");
718         return VLC_EGENERIC;
719     }
720     va->vs = vs;
721
722     return VLC_SUCCESS;
723 }
724 /**
725  * It destroys a DirectX video service
726  */
727 static void DxDestroyVideoService(vlc_va_dxva2_t *va)
728 {
729     if (va->device)
730         IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
731     if (va->vs)
732         IDirectXVideoDecoderService_Release(va->vs);
733 }
734 /**
735  * Find the best suited decoder mode GUID and render format.
736  */
737 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
738 {
739     /* Retreive supported modes from the decoder service */
740     UINT input_count = 0;
741     GUID *input_list = NULL;
742     if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
743                                                                  &input_count,
744                                                                  &input_list))) {
745         msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
746         return VLC_EGENERIC;
747     }
748     for (unsigned i = 0; i < input_count; i++) {
749         const GUID *g = &input_list[i];
750         const dxva2_mode_t *mode = Dxva2FindMode(g);
751         if (mode) {
752             msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
753         } else {
754             msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX",
755                      (unsigned)g->Data1, g->Data2, g->Data3);
756         }
757     }
758
759     /* Try all supported mode by our priority */
760     for (unsigned i = 0; dxva2_modes[i].name; i++) {
761         const dxva2_mode_t *mode = &dxva2_modes[i];
762         if (!mode->codec || mode->codec != va->codec_id)
763             continue;
764
765         /* */
766         bool is_suported = false;
767         for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
768             is_suported = IsEqualGUID(mode->guid, g);
769         }
770         if (!is_suported)
771             continue;
772
773         /* */
774         msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
775         UINT      output_count = 0;
776         D3DFORMAT *output_list = NULL;
777         if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
778                                                                        &output_count,
779                                                                        &output_list))) {
780             msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
781             continue;
782         }
783         for (unsigned j = 0; j < output_count; j++) {
784             const D3DFORMAT f = output_list[j];
785             const d3d_format_t *format = D3dFindFormat(f);
786             if (format) {
787                 msg_Dbg(va->log, "%s is supported for output", format->name);
788             } else {
789                 msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
790             }
791         }
792
793         /* */
794         for (unsigned j = 0; d3d_formats[j].name; j++) {
795             const d3d_format_t *format = &d3d_formats[j];
796
797             /* */
798             bool is_suported = false;
799             for (unsigned k = 0; !is_suported && k < output_count; k++) {
800                 is_suported = format->format == output_list[k];
801             }
802             if (!is_suported)
803                 continue;
804
805             /* We have our solution */
806             msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
807             *input  = *mode->guid;
808             *output = format->format;
809             CoTaskMemFree(output_list);
810             CoTaskMemFree(input_list);
811             return VLC_SUCCESS;
812         }
813         CoTaskMemFree(output_list);
814     }
815     CoTaskMemFree(input_list);
816     return VLC_EGENERIC;
817 }
818
819 /**
820  * It creates a DXVA2 decoder using the given video format
821  */
822 static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
823                                 int codec_id, const video_format_t *fmt)
824 {
825     /* */
826     msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
827             codec_id, fmt->i_width, fmt->i_height);
828
829     /* Allocates all surfaces needed for the decoder */
830     switch (codec_id) {
831     case CODEC_ID_H264:
832         va->surface_count = 16 + 1;
833         break;
834     default:
835         va->surface_count = 2 + 1;
836         break;
837     }
838     LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
839     if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
840                                                          fmt->i_width,
841                                                          fmt->i_height,
842                                                          va->surface_count - 1,
843                                                          va->render,
844                                                          D3DPOOL_DEFAULT,
845                                                          0,
846                                                          DXVA2_VideoDecoderRenderTarget,
847                                                          surface_list,
848                                                          NULL))) {
849         msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed\n");
850         va->surface_count = 0;
851         return VLC_EGENERIC;
852     }
853     for (unsigned i = 0; i < va->surface_count; i++) {
854         vlc_va_surface_t *surface = &va->surface[i];
855         surface->d3d = surface_list[i];
856         surface->refcount = 0;
857         surface->order = 0;
858     }
859     va->surface_width  = fmt->i_width;
860     va->surface_height = fmt->i_height;
861     msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
862             va->surface_count, fmt->i_width, fmt->i_height);
863
864     /* */
865     DXVA2_VideoDesc dsc;
866     ZeroMemory(&dsc, sizeof(dsc));
867     dsc.SampleWidth     = fmt->i_width;
868     dsc.SampleHeight    = fmt->i_height;
869     dsc.Format          = va->render;
870     if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
871         dsc.InputSampleFreq.Numerator   = fmt->i_frame_rate;
872         dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
873     } else {
874         dsc.InputSampleFreq.Numerator   = 0;
875         dsc.InputSampleFreq.Denominator = 0;
876     }
877     dsc.OutputFrameFreq = dsc.InputSampleFreq;
878     dsc.UABProtectionLevel = FALSE;
879     dsc.Reserved = 0;
880
881     /* FIXME I am unsure we can let unknown everywhere */
882     DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
883     ext->SampleFormat = 0;//DXVA2_SampleUnknown;
884     ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
885     ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
886     ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
887     ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
888     ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
889     ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
890
891     /* List all configurations available for the decoder */
892     UINT                      cfg_count = 0;
893     DXVA2_ConfigPictureDecode *cfg_list = NULL;
894     if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
895                                                                     &va->input,
896                                                                     &dsc,
897                                                                     NULL,
898                                                                     &cfg_count,
899                                                                     &cfg_list))) {
900         msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed\n");
901         return VLC_EGENERIC;
902     }
903     msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
904
905     /* Select the best decoder configuration */
906     int cfg_score = 0;
907     for (unsigned i = 0; i < cfg_count; i++) {
908         const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
909
910         /* */
911         msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
912                 i, cfg->ConfigBitstreamRaw);
913
914         /* */
915         int score;
916         if (cfg->ConfigBitstreamRaw == 1)
917             score = 1;
918         else if (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
919             score = 2;
920         else
921             continue;
922         if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA_NoEncrypt))
923             score += 16;
924
925         if (cfg_score < score) {
926             va->cfg = *cfg;
927             cfg_score = score;
928         }
929     }
930     CoTaskMemFree(cfg_list);
931     if (cfg_score <= 0) {
932         msg_Err(va->log, "Failed to find a supported decoder configuration");
933         return VLC_EGENERIC;
934     }
935
936     /* Create the decoder */
937     IDirectXVideoDecoder *decoder;
938     if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
939                                                               &va->input,
940                                                               &dsc,
941                                                               &va->cfg,
942                                                               surface_list,
943                                                               va->surface_count,
944                                                               &decoder))) {
945         msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed\n");
946         return VLC_EGENERIC;
947     }
948     va->decoder = decoder;
949     msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
950     return VLC_SUCCESS;
951 }
952 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
953 {
954     if (va->decoder)
955         IDirectXVideoDecoder_Release(va->decoder);
956     va->decoder = NULL;
957
958     for (unsigned i = 0; i < va->surface_count; i++)
959         IDirect3DSurface9_Release(va->surface[i].d3d);
960     va->surface_count = 0;
961 }
962 static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
963 {
964     msg_Err(va->log, "DxResetVideoDecoder unimplemented");
965     return VLC_EGENERIC;
966 }
967
968 static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
969 {
970     switch (va->render) {
971     case MAKEFOURCC('N','V','1','2'):
972         va->output = MAKEFOURCC('Y','V','1','2');
973         break;
974     default:
975         va->output = va->render;
976         break;
977     }
978     CopyInitCache(&va->surface_cache, va->surface_width);
979 }
980 static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
981 {
982     CopyCleanCache(&va->surface_cache);
983 }
984 #else
985 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
986 {
987     (void)log;
988     (void)codec_id;
989     return NULL;
990 }
991 #endif