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