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