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