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