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