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