]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/dxva2.c
1054e1e6893f35421982159c0bca8b08652b5b79
[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     { "MPEG-2 variable-length decoder",            &DXVA2_ModeMPEG2_VLD,     CODEC_ID_MPEG2VIDEO },
146     { "MPEG-2 motion compensation",                &DXVA2_ModeMPEG2_MoComp,  0 },
147     { "MPEG-2 inverse discrete cosine transform",  &DXVA2_ModeMPEG2_IDCT,    0 },
148
149     { "H.264 variable-length decoder, film grain technology",                      &DXVA2_ModeH264_F,         CODEC_ID_H264 },
150     { "H.264 variable-length decoder, no film grain technology",                   &DXVA2_ModeH264_E,         CODEC_ID_H264 },
151     { "H.264 variable-length decoder, no film grain technology (Intel)",           &DXVADDI_Intel_ModeH264_E, CODEC_ID_H264 },
152     { "H.264 inverse discrete cosine transform, film grain technology",            &DXVA2_ModeH264_D,         0             },
153     { "H.264 inverse discrete cosine transform, no film grain technology",         &DXVA2_ModeH264_C,         0             },
154     { "H.264 inverse discrete cosine transform, no film grain technology (Intel)", &DXVADDI_Intel_ModeH264_C, 0             },
155     { "H.264 motion compensation, film grain technology",                          &DXVA2_ModeH264_B,         0             },
156     { "H.264 motion compensation, no film grain technology",                       &DXVA2_ModeH264_A,         0             },
157     { "H.264 motion compensation, no film grain technology (Intel)",               &DXVADDI_Intel_ModeH264_A, 0             },
158
159     { "Windows Media Video 8 motion compensation", &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 motion compensation", &DXVA2_ModeWMV9_B, 0 },
164     { "Windows Media Video 9 post processing",     &DXVA2_ModeWMV9_A, 0 },
165
166     { "VC-1 variable-length decoder",              &DXVA2_ModeVC1_D, CODEC_ID_VC1 },
167     { "VC-1 variable-length decoder",              &DXVA2_ModeVC1_D, CODEC_ID_WMV3 },
168     { "VC-1 inverse discrete cosine transform",    &DXVA2_ModeVC1_C, 0 },
169     { "VC-1 motion compensation",                  &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     int          width;
231     int          height;
232
233     /* DLL */
234         HINSTANCE             hd3d9_dll;
235     HINSTANCE             hdxva2_dll;
236
237     /* Direct3D */
238     D3DPRESENT_PARAMETERS  d3dpp;
239     LPDIRECT3D9            d3dobj;
240     D3DADAPTER_IDENTIFIER9 d3dai;
241     LPDIRECT3DDEVICE9      d3ddev;
242
243     /* Device manager */
244     UINT                     token;
245     IDirect3DDeviceManager9  *devmng;
246     HANDLE                   device;
247
248     /* Video service */
249     IDirectXVideoDecoderService  *vs;
250     GUID                         input;
251     D3DFORMAT                    render;
252
253     /* Video decoder */
254     DXVA2_ConfigPictureDecode    cfg;
255     IDirectXVideoDecoder         *decoder;
256
257     /* Option conversion */
258     D3DFORMAT                    output;
259     copy_cache_t                 surface_cache;
260
261     /* */
262     struct dxva_context hw;
263
264     /* */
265     unsigned     surface_count;
266     unsigned     surface_order;
267     int          surface_width;
268     int          surface_height;
269     vlc_fourcc_t surface_chroma;
270
271     vlc_va_surface_t surface[VA_DXVA2_MAX_SURFACE_COUNT];
272     LPDIRECT3DSURFACE9 hw_surface[VA_DXVA2_MAX_SURFACE_COUNT];
273 } vlc_va_dxva2_t;
274
275 /* */
276 static vlc_va_dxva2_t *vlc_va_dxva2_Get(void *external)
277 {
278     assert(external == (void*)(&((vlc_va_dxva2_t*)external)->va));
279     return external;
280 }
281
282 /* */
283 static int D3dCreateDevice(vlc_va_dxva2_t *);
284 static void D3dDestroyDevice(vlc_va_dxva2_t *);
285 static char *DxDescribe(vlc_va_dxva2_t *);
286
287 static int D3dCreateDeviceManager(vlc_va_dxva2_t *);
288 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *);
289
290 static int DxCreateVideoService(vlc_va_dxva2_t *);
291 static void DxDestroyVideoService(vlc_va_dxva2_t *);
292 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *, GUID *input, D3DFORMAT *output);
293
294 static int DxCreateVideoDecoder(vlc_va_dxva2_t *,
295                                 int codec_id, const video_format_t *);
296 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *);
297 static int DxResetVideoDecoder(vlc_va_dxva2_t *);
298
299 static void DxCreateVideoConversion(vlc_va_dxva2_t *);
300 static void DxDestroyVideoConversion(vlc_va_dxva2_t *);
301
302 /* */
303 static int Setup(vlc_va_t *external, void **hw, vlc_fourcc_t *chroma,
304                  int width, int height)
305 {
306     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
307
308     if (va->width == width && va->height == height && va->decoder)
309         goto ok;
310
311     /* */
312     DxDestroyVideoConversion(va);
313     DxDestroyVideoDecoder(va);
314
315     *hw = NULL;
316     *chroma = 0;
317     if (width <= 0 || height <= 0)
318         return VLC_EGENERIC;
319
320     /* FIXME transmit a video_format_t by VaSetup directly */
321     video_format_t fmt;
322     memset(&fmt, 0, sizeof(fmt));
323     fmt.i_width = width;
324     fmt.i_height = height;
325
326     if (DxCreateVideoDecoder(va, va->codec_id, &fmt))
327         return VLC_EGENERIC;
328     /* */
329     va->hw.decoder = va->decoder;
330     va->hw.cfg = &va->cfg;
331     va->hw.surface_count = va->surface_count;
332     va->hw.surface = va->hw_surface;
333     for (unsigned i = 0; i < va->surface_count; i++)
334         va->hw.surface[i] = va->surface[i].d3d;
335
336     /* */
337     DxCreateVideoConversion(va);
338
339     /* */
340 ok:
341     *hw = &va->hw;
342     const d3d_format_t *output = D3dFindFormat(va->output);
343     *chroma = output->codec;
344
345     return VLC_SUCCESS;
346 }
347
348 static int Extract(vlc_va_t *external, picture_t *picture, AVFrame *ff)
349 {
350     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
351     LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
352
353     if (!va->surface_cache.buffer)
354         return VLC_EGENERIC;
355
356     /* */
357     assert(va->output == MAKEFOURCC('Y','V','1','2'));
358
359     /* */
360     D3DLOCKED_RECT lock;
361     if (FAILED(IDirect3DSurface9_LockRect(d3d, &lock, NULL, D3DLOCK_READONLY))) {
362         msg_Err(va->log, "Failed to lock surface");
363         return VLC_EGENERIC;
364     }
365
366     if (va->render == MAKEFOURCC('Y','V','1','2')) {
367         uint8_t *plane[3] = {
368             lock.pBits,
369             (uint8_t*)lock.pBits + lock.Pitch * va->surface_height,
370             (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
371                                  + (lock.Pitch/2) * (va->surface_height/2)
372         };
373         size_t  pitch[3] = {
374             lock.Pitch,
375             lock.Pitch / 2,
376             lock.Pitch / 2,
377         };
378         CopyFromYv12(picture, plane, pitch,
379                      va->width, va->height,
380                      &va->surface_cache);
381     } else {
382         assert(va->render == MAKEFOURCC('N','V','1','2'));
383         uint8_t *plane[2] = {
384             lock.pBits,
385             (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
386         };
387         size_t  pitch[2] = {
388             lock.Pitch,
389             lock.Pitch,
390         };
391         CopyFromNv12(picture, plane, pitch,
392                      va->width, va->height,
393                      &va->surface_cache);
394     }
395
396     /* */
397     IDirect3DSurface9_UnlockRect(d3d);
398     return VLC_SUCCESS;
399 }
400 /* FIXME it is nearly common with VAAPI */
401 static int Get(vlc_va_t *external, AVFrame *ff)
402 {
403     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
404
405     /* Check the device */
406     HRESULT hr = IDirect3DDeviceManager9_TestDevice(va->devmng, va->device);
407     if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
408         if (DxResetVideoDecoder(va))
409             return VLC_EGENERIC;
410     } else if (FAILED(hr)) {
411         msg_Err(va->log, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
412         return VLC_EGENERIC;
413     }
414
415     /* Grab an unused surface, in case none are, try the oldest
416      * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
417     unsigned i, old;
418     for (i = 0, old = 0; i < va->surface_count; i++) {
419         vlc_va_surface_t *surface = &va->surface[i];
420
421         if (!surface->refcount)
422             break;
423
424         if (surface->order < va->surface[old].order)
425             old = i;
426     }
427     if (i >= va->surface_count)
428         i = old;
429
430     vlc_va_surface_t *surface = &va->surface[i];
431
432     surface->refcount = 1;
433     surface->order = va->surface_order++;
434
435     /* */
436     for (int i = 0; i < 4; i++) {
437         ff->data[i] = NULL;
438         ff->linesize[i] = 0;
439
440         if (i == 0 || i == 3)
441             ff->data[i] = (void*)surface->d3d;/* Yummie */
442     }
443     return VLC_SUCCESS;
444 }
445 static void Release(vlc_va_t *external, AVFrame *ff)
446 {
447     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
448     LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
449
450     for (unsigned i = 0; i < va->surface_count; i++) {
451         vlc_va_surface_t *surface = &va->surface[i];
452
453         if (surface->d3d == d3d)
454             surface->refcount--;
455     }
456 }
457 static void Close(vlc_va_t *external)
458 {
459     vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
460
461     DxDestroyVideoConversion(va);
462     DxDestroyVideoDecoder(va);
463     DxDestroyVideoService(va);
464     D3dDestroyDeviceManager(va);
465     D3dDestroyDevice(va);
466
467     if (va->hdxva2_dll)
468         FreeLibrary(va->hdxva2_dll);
469     if (va->hd3d9_dll)
470         FreeLibrary(va->hd3d9_dll);
471
472     free(va->va.description);
473     free(va);
474 }
475
476 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
477 {
478     vlc_va_dxva2_t *va = calloc(1, sizeof(*va));
479     if (!va)
480         return NULL;
481
482     /* */
483     va->log = log;
484     va->codec_id = codec_id;
485
486     /* Load dll*/
487     va->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
488     if (!va->hd3d9_dll) {
489         msg_Warn(va->log, "cannot load d3d9.dll");
490         goto error;
491     }
492     va->hdxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
493     if (!va->hdxva2_dll) {
494         msg_Warn(va->log, "cannot load dxva2.dll");
495         goto error;
496     }
497     msg_Dbg(va->log, "DLLs loaded");
498
499     /* */
500     if (D3dCreateDevice(va)) {
501         msg_Err(va->log, "Failed to create Direct3D device");
502         goto error;
503     }
504     msg_Dbg(va->log, "D3dCreateDevice succeed");
505
506     if (D3dCreateDeviceManager(va)) {
507         msg_Err(va->log, "D3dCreateDeviceManager failed");
508         goto error;
509     }
510
511     if (DxCreateVideoService(va)) {
512         msg_Err(va->log, "DxCreateVideoService failed");
513         goto error;
514     }
515
516     /* */
517     if (DxFindVideoServiceConversion(va, &va->input, &va->render)) {
518         msg_Err(va->log, "DxFindVideoServiceConversion failed");
519         goto error;
520     }
521
522     /* TODO print the hardware name/vendor for debugging purposes */
523     va->va.description = DxDescribe(va);
524     va->va.setup   = Setup;
525     va->va.get     = Get;
526     va->va.release = Release;
527     va->va.extract = Extract;
528     va->va.close   = Close;
529     return &va->va;
530
531 error:
532     Close(&va->va);
533     return NULL;
534 }
535 /* */
536
537 /**
538  * It creates a Direct3D device usable for DXVA 2
539  */
540 static int D3dCreateDevice(vlc_va_dxva2_t *va)
541 {
542     /* */
543     LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
544     Create9 = (void *)GetProcAddress(va->hd3d9_dll,
545                                      TEXT("Direct3DCreate9"));
546     if (!Create9) {
547         msg_Err(va->log, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
548         return VLC_EGENERIC;
549     }
550
551     /* */
552     LPDIRECT3D9 d3dobj;
553     d3dobj = Create9(D3D_SDK_VERSION);
554     if (!d3dobj) {
555         msg_Err(va->log, "Direct3DCreate9 failed");
556         return VLC_EGENERIC;
557     }
558     va->d3dobj = d3dobj;
559
560     /* */
561     D3DADAPTER_IDENTIFIER9 *d3dai = &va->d3dai;
562     if (FAILED(IDirect3D9_GetAdapterIdentifier(va->d3dobj,
563                                                D3DADAPTER_DEFAULT, 0, d3dai))) {
564         msg_Warn(va->log, "IDirect3D9_GetAdapterIdentifier failed");
565         ZeroMemory(d3dai, sizeof(*d3dai));
566     }
567
568     /* */
569     D3DPRESENT_PARAMETERS *d3dpp = &va->d3dpp;
570     ZeroMemory(d3dpp, sizeof(*d3dpp));
571     d3dpp->Flags                  = D3DPRESENTFLAG_VIDEO;
572     d3dpp->Windowed               = TRUE;
573     d3dpp->hDeviceWindow          = NULL;
574     d3dpp->SwapEffect             = D3DSWAPEFFECT_DISCARD;
575     d3dpp->MultiSampleType        = D3DMULTISAMPLE_NONE;
576     d3dpp->PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
577     d3dpp->BackBufferCount        = 0;                  /* FIXME what to put here */
578     d3dpp->BackBufferFormat       = D3DFMT_X8R8G8B8;    /* FIXME what to put here */
579     d3dpp->BackBufferWidth        = 0;
580     d3dpp->BackBufferHeight       = 0;
581     d3dpp->EnableAutoDepthStencil = FALSE;
582
583     /* Direct3D needs a HWND to create a device, even without using ::Present
584     this HWND is used to alert Direct3D when there's a change of focus window.
585     For now, use GetShellWindow, as it looks harmless */
586     LPDIRECT3DDEVICE9 d3ddev;
587     if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
588                                        D3DDEVTYPE_HAL, GetShellWindow(),
589                                        D3DCREATE_SOFTWARE_VERTEXPROCESSING |
590                                        D3DCREATE_MULTITHREADED,
591                                        d3dpp, &d3ddev))) {
592         msg_Err(va->log, "IDirect3D9_CreateDevice failed");
593         return VLC_EGENERIC;
594     }
595     va->d3ddev = d3ddev;
596
597     return VLC_SUCCESS;
598 }
599 /**
600  * It releases a Direct3D device and its resources.
601  */
602 static void D3dDestroyDevice(vlc_va_dxva2_t *va)
603 {
604     if (va->d3ddev)
605         IDirect3DDevice9_Release(va->d3ddev);
606     if (va->d3dobj)
607         IDirect3D9_Release(va->d3dobj);
608 }
609 /**
610  * It describes our Direct3D object
611  */
612 static char *DxDescribe(vlc_va_dxva2_t *va)
613 {
614     static const struct {
615         unsigned id;
616         char     name[32];
617     } vendors [] = {
618         { 0x1002, "ATI" },
619         { 0x10DE, "NVIDIA" },
620         { 0x8086, "Intel" },
621         { 0x5333, "S3 Graphics" },
622         { 0, "" }
623     };
624     D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
625
626     const char *vendor = "Unknown";
627     for (int i = 0; vendors[i].id != 0; i++) {
628         if (vendors[i].id == id->VendorId) {
629             vendor = vendors[i].name;
630             break;
631         }
632     }
633
634     char *description;
635     if (asprintf(&description, "DXVA2 (%.*s, vendor %d(%s), device %d, revision %d)",
636                  sizeof(id->Description), id->Description,
637                  id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
638         return NULL;
639     return description;
640 }
641
642 /**
643  * It creates a Direct3D device manager
644  */
645 static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
646 {
647     HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
648                                            IDirect3DDeviceManager9 **);
649     CreateDeviceManager9 =
650       (void *)GetProcAddress(va->hdxva2_dll,
651                              TEXT("DXVA2CreateDirect3DDeviceManager9"));
652
653     if (!CreateDeviceManager9) {
654         msg_Err(va->log, "cannot load function\n");
655         return VLC_EGENERIC;
656     }
657     msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
658
659     UINT token;
660     IDirect3DDeviceManager9 *devmng;
661     if (FAILED(CreateDeviceManager9(&token, &devmng))) {
662         msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
663         return VLC_EGENERIC;
664     }
665     va->token  = token;
666     va->devmng = devmng;
667     msg_Info(va->log, "obtained IDirect3DDeviceManager9");
668
669     HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
670     if (FAILED(hr)) {
671         msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
672         return VLC_EGENERIC;
673     }
674     return VLC_SUCCESS;
675 }
676 /**
677  * It destroys a Direct3D device manager
678  */
679 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
680 {
681     if (va->devmng)
682         IDirect3DDeviceManager9_Release(va->devmng);
683 }
684
685 /**
686  * It creates a DirectX video service
687  */
688 static int DxCreateVideoService(vlc_va_dxva2_t *va)
689 {
690     HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
691                                          REFIID riid,
692                                          void **ppService);
693     CreateVideoService =
694       (void *)GetProcAddress(va->hdxva2_dll,
695                              TEXT("DXVA2CreateVideoService"));
696
697     if (!CreateVideoService) {
698         msg_Err(va->log, "cannot load function\n");
699         return 4;
700     }
701     msg_Info(va->log, "DXVA2CreateVideoService Success!");
702
703     HRESULT hr;
704
705     HANDLE device;
706     hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
707     if (FAILED(hr)) {
708         msg_Err(va->log, "OpenDeviceHandle failed");
709         return VLC_EGENERIC;
710     }
711     va->device = device;
712
713     IDirectXVideoDecoderService *vs;
714     hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
715                                                  &IID_IDirectXVideoDecoderService,
716                                                  &vs);
717     if (FAILED(hr)) {
718         msg_Err(va->log, "GetVideoService failed");
719         return VLC_EGENERIC;
720     }
721     va->vs = vs;
722
723     return VLC_SUCCESS;
724 }
725 /**
726  * It destroys a DirectX video service
727  */
728 static void DxDestroyVideoService(vlc_va_dxva2_t *va)
729 {
730     if (va->device)
731         IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
732     if (va->vs)
733         IDirectXVideoDecoderService_Release(va->vs);
734 }
735 /**
736  * Find the best suited decoder mode GUID and render format.
737  */
738 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
739 {
740     /* Retreive supported modes from the decoder service */
741     UINT input_count = 0;
742     GUID *input_list = NULL;
743     if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
744                                                                  &input_count,
745                                                                  &input_list))) {
746         msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
747         return VLC_EGENERIC;
748     }
749     for (unsigned i = 0; i < input_count; i++) {
750         const GUID *g = &input_list[i];
751         const dxva2_mode_t *mode = Dxva2FindMode(g);
752         if (mode) {
753             msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
754         } else {
755             msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX",
756                      (unsigned)g->Data1, g->Data2, g->Data3);
757         }
758     }
759
760     /* Try all supported mode by our priority */
761     for (unsigned i = 0; dxva2_modes[i].name; i++) {
762         const dxva2_mode_t *mode = &dxva2_modes[i];
763         if (!mode->codec || mode->codec != va->codec_id)
764             continue;
765
766         /* */
767         bool is_suported = false;
768         for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
769             is_suported = IsEqualGUID(mode->guid, g);
770         }
771         if (!is_suported)
772             continue;
773
774         /* */
775         msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
776         UINT      output_count = 0;
777         D3DFORMAT *output_list = NULL;
778         if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
779                                                                        &output_count,
780                                                                        &output_list))) {
781             msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
782             continue;
783         }
784         for (unsigned j = 0; j < output_count; j++) {
785             const D3DFORMAT f = output_list[j];
786             const d3d_format_t *format = D3dFindFormat(f);
787             if (format) {
788                 msg_Dbg(va->log, "%s is supported for output", format->name);
789             } else {
790                 msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
791             }
792         }
793
794         /* */
795         for (unsigned j = 0; d3d_formats[j].name; j++) {
796             const d3d_format_t *format = &d3d_formats[j];
797
798             /* */
799             bool is_suported = false;
800             for (unsigned k = 0; !is_suported && k < output_count; k++) {
801                 is_suported = format->format == output_list[k];
802             }
803             if (!is_suported)
804                 continue;
805
806             /* We have our solution */
807             msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
808             *input  = *mode->guid;
809             *output = format->format;
810             CoTaskMemFree(output_list);
811             CoTaskMemFree(input_list);
812             return VLC_SUCCESS;
813         }
814         CoTaskMemFree(output_list);
815     }
816     CoTaskMemFree(input_list);
817     return VLC_EGENERIC;
818 }
819
820 /**
821  * It creates a DXVA2 decoder using the given video format
822  */
823 static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
824                                 int codec_id, const video_format_t *fmt)
825 {
826     /* */
827     msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
828             codec_id, fmt->i_width, fmt->i_height);
829
830     va->width  = fmt->i_width;
831     va->height = fmt->i_height;
832
833     /* Allocates all surfaces needed for the decoder */
834     va->surface_width  = (fmt->i_width  + 15) & ~15;
835     va->surface_height = (fmt->i_height + 15) & ~15;
836     switch (codec_id) {
837     case CODEC_ID_H264:
838         va->surface_count = 16 + 1;
839         break;
840     default:
841         va->surface_count = 2 + 1;
842         break;
843     }
844     LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
845     if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
846                                                          va->surface_width,
847                                                          va->surface_height,
848                                                          va->surface_count - 1,
849                                                          va->render,
850                                                          D3DPOOL_DEFAULT,
851                                                          0,
852                                                          DXVA2_VideoDecoderRenderTarget,
853                                                          surface_list,
854                                                          NULL))) {
855         msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed\n");
856         va->surface_count = 0;
857         return VLC_EGENERIC;
858     }
859     for (unsigned i = 0; i < va->surface_count; i++) {
860         vlc_va_surface_t *surface = &va->surface[i];
861         surface->d3d = surface_list[i];
862         surface->refcount = 0;
863         surface->order = 0;
864     }
865     msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
866             va->surface_count, fmt->i_width, fmt->i_height);
867
868     /* */
869     DXVA2_VideoDesc dsc;
870     ZeroMemory(&dsc, sizeof(dsc));
871     dsc.SampleWidth     = fmt->i_width;
872     dsc.SampleHeight    = fmt->i_height;
873     dsc.Format          = va->render;
874     if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
875         dsc.InputSampleFreq.Numerator   = fmt->i_frame_rate;
876         dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
877     } else {
878         dsc.InputSampleFreq.Numerator   = 0;
879         dsc.InputSampleFreq.Denominator = 0;
880     }
881     dsc.OutputFrameFreq = dsc.InputSampleFreq;
882     dsc.UABProtectionLevel = FALSE;
883     dsc.Reserved = 0;
884
885     /* FIXME I am unsure we can let unknown everywhere */
886     DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
887     ext->SampleFormat = 0;//DXVA2_SampleUnknown;
888     ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
889     ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
890     ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
891     ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
892     ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
893     ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
894
895     /* List all configurations available for the decoder */
896     UINT                      cfg_count = 0;
897     DXVA2_ConfigPictureDecode *cfg_list = NULL;
898     if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
899                                                                     &va->input,
900                                                                     &dsc,
901                                                                     NULL,
902                                                                     &cfg_count,
903                                                                     &cfg_list))) {
904         msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed\n");
905         return VLC_EGENERIC;
906     }
907     msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
908
909     /* Select the best decoder configuration */
910     int cfg_score = 0;
911     for (unsigned i = 0; i < cfg_count; i++) {
912         const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
913
914         /* */
915         msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
916                 i, cfg->ConfigBitstreamRaw);
917
918         /* */
919         int score;
920         if (cfg->ConfigBitstreamRaw == 1)
921             score = 1;
922         else if (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
923             score = 2;
924         else
925             continue;
926         if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA_NoEncrypt))
927             score += 16;
928
929         if (cfg_score < score) {
930             va->cfg = *cfg;
931             cfg_score = score;
932         }
933     }
934     CoTaskMemFree(cfg_list);
935     if (cfg_score <= 0) {
936         msg_Err(va->log, "Failed to find a supported decoder configuration");
937         return VLC_EGENERIC;
938     }
939
940     /* Create the decoder */
941     IDirectXVideoDecoder *decoder;
942     if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
943                                                               &va->input,
944                                                               &dsc,
945                                                               &va->cfg,
946                                                               surface_list,
947                                                               va->surface_count,
948                                                               &decoder))) {
949         msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed\n");
950         return VLC_EGENERIC;
951     }
952     va->decoder = decoder;
953     msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
954     return VLC_SUCCESS;
955 }
956 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
957 {
958     if (va->decoder)
959         IDirectXVideoDecoder_Release(va->decoder);
960     va->decoder = NULL;
961
962     for (unsigned i = 0; i < va->surface_count; i++)
963         IDirect3DSurface9_Release(va->surface[i].d3d);
964     va->surface_count = 0;
965 }
966 static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
967 {
968     msg_Err(va->log, "DxResetVideoDecoder unimplemented");
969     return VLC_EGENERIC;
970 }
971
972 static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
973 {
974     switch (va->render) {
975     case MAKEFOURCC('N','V','1','2'):
976         va->output = MAKEFOURCC('Y','V','1','2');
977         break;
978     default:
979         va->output = va->render;
980         break;
981     }
982     CopyInitCache(&va->surface_cache, va->surface_width);
983 }
984 static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
985 {
986     CopyCleanCache(&va->surface_cache);
987 }
988 #else
989 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
990 {
991     (void)log;
992     (void)codec_id;
993     return NULL;
994 }
995 #endif