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