A bit more explicit dxva2 describe function.
[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     static const struct {
591         unsigned id;
592         char     name[32];
593     } vendors [] = {
594         { 0x1002, "ATI" },
595         { 0x10DE, "NVIDIA" },
596         { 0x8086, "Intel" },
597         { 0x5333, "S3 Graphics" },
598         { 0, "" }
599     };
600     D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
601
602     const char *vendor = "Unknown";
603     for (int i = 0; vendors[i].id != 0; i++) {
604         if (vendors[i].id == id->VendorId) {
605             vendor = vendors[i].name;
606             break;
607         }
608     }
609
610     char *description;
611     if (asprintf(&description, "DXVA2 (%.*s, vendor %d(%s), device %d, revision %d)",
612                  sizeof(id->Description), id->Description,
613                  id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
614         return NULL;
615     return description;
616 }
617
618 /**
619  * It creates a Direct3D device manager
620  */
621 static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
622 {
623     HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
624                                            IDirect3DDeviceManager9 **);
625     CreateDeviceManager9 =
626       (void *)GetProcAddress(va->hdxva2_dll,
627                              TEXT("DXVA2CreateDirect3DDeviceManager9"));
628
629     if (!CreateDeviceManager9) {
630         msg_Err(va->log, "cannot load function\n");
631         return VLC_EGENERIC;
632     }
633     msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
634
635     UINT token;
636     IDirect3DDeviceManager9 *devmng;
637     if (FAILED(CreateDeviceManager9(&token, &devmng))) {
638         msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
639         return VLC_EGENERIC;
640     }
641     va->token  = token;
642     va->devmng = devmng;
643     msg_Info(va->log, "obtained IDirect3DDeviceManager9");
644
645     HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
646     if (FAILED(hr)) {
647         msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
648         return VLC_EGENERIC;
649     }
650     return VLC_SUCCESS;
651 }
652 /**
653  * It destroys a Direct3D device manager
654  */
655 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
656 {
657     if (va->devmng)
658         IDirect3DDeviceManager9_Release(va->devmng);
659 }
660
661 /**
662  * It creates a DirectX video service
663  */
664 static int DxCreateVideoService(vlc_va_dxva2_t *va)
665 {
666     HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
667                                          REFIID riid,
668                                          void **ppService);
669     CreateVideoService =
670       (void *)GetProcAddress(va->hdxva2_dll,
671                              TEXT("DXVA2CreateVideoService"));
672
673     if (!CreateVideoService) {
674         msg_Err(va->log, "cannot load function\n");
675         return 4;
676     }
677     msg_Info(va->log, "DXVA2CreateVideoService Success!");
678
679     HRESULT hr;
680
681     HANDLE device;
682     hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
683     if (FAILED(hr)) {
684         msg_Err(va->log, "OpenDeviceHandle failed");
685         return VLC_EGENERIC;
686     }
687     va->device = device;
688
689     IDirectXVideoDecoderService *vs;
690     hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
691                                                  &IID_IDirectXVideoDecoderService,
692                                                  &vs);
693     if (FAILED(hr)) {
694         msg_Err(va->log, "GetVideoService failed");
695         return VLC_EGENERIC;
696     }
697     va->vs = vs;
698
699     return VLC_SUCCESS;
700 }
701 /**
702  * It destroys a DirectX video service
703  */
704 static void DxDestroyVideoService(vlc_va_dxva2_t *va)
705 {
706     if (va->device)
707         IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
708     if (va->vs)
709         IDirectXVideoDecoderService_Release(va->vs);
710 }
711 /**
712  * Find the best suited decoder mode GUID and render format.
713  */
714 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
715 {
716     /* Retreive supported modes from the decoder service */
717     UINT input_count = 0;
718     GUID *input_list = NULL;
719     if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
720                                                                  &input_count,
721                                                                  &input_list))) {
722         msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
723         return VLC_EGENERIC;
724     }
725     for (unsigned i = 0; i < input_count; i++) {
726         const GUID *g = &input_list[i];
727         const dxva2_mode_t *mode = Dxva2FindMode(g);
728         if (mode) {
729             msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
730         } else {
731             msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX",
732                      (unsigned)g->Data1, g->Data2, g->Data3);
733         }
734     }
735
736     /* Try all supported mode by our priority */
737     for (unsigned i = 0; dxva2_modes[i].name; i++) {
738         const dxva2_mode_t *mode = &dxva2_modes[i];
739         if (!mode->codec || mode->codec != va->codec_id)
740             continue;
741
742         /* */
743         bool is_suported = false;
744         for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
745             is_suported = IsEqualGUID(mode->guid, g);
746         }
747         if (!is_suported)
748             continue;
749
750         /* */
751         msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
752         UINT      output_count = 0;
753         D3DFORMAT *output_list = NULL;
754         if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
755                                                                        &output_count,
756                                                                        &output_list))) {
757             msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
758             continue;
759         }
760         for (unsigned j = 0; j < output_count; j++) {
761             const D3DFORMAT f = output_list[j];
762             const d3d_format_t *format = D3dFindFormat(f);
763             if (format) {
764                 msg_Dbg(va->log, "%s is supported for output", format->name);
765             } else {
766                 msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
767             }
768         }
769
770         /* */
771         for (unsigned j = 0; d3d_formats[j].name; j++) {
772             const d3d_format_t *format = &d3d_formats[j];
773
774             /* */
775             bool is_suported = false;
776             for (unsigned k = 0; !is_suported && k < output_count; k++) {
777                 is_suported = format->format == output_list[k];
778             }
779             if (!is_suported)
780                 continue;
781
782             /* We have our solution */
783             msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
784             *input  = *mode->guid;
785             *output = format->format;
786             CoTaskMemFree(output_list);
787             CoTaskMemFree(input_list);
788             return VLC_SUCCESS;
789         }
790         CoTaskMemFree(output_list);
791     }
792     CoTaskMemFree(input_list);
793     return VLC_EGENERIC;
794 }
795
796 /**
797  * It creates a DXVA2 decoder using the given video format
798  */
799 static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
800                                 int codec_id, const video_format_t *fmt)
801 {
802     /* */
803     msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
804             codec_id, fmt->i_width, fmt->i_height);
805
806     /* Allocates all surfaces needed for the decoder */
807     switch (codec_id) {
808     case CODEC_ID_H264:
809         va->surface_count = 16 + 1;
810         break;
811     default:
812         va->surface_count = 2 + 1;
813         break;
814     }
815     LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
816     if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
817                                                          fmt->i_width,
818                                                          fmt->i_height,
819                                                          va->surface_count - 1,
820                                                          va->render,
821                                                          D3DPOOL_DEFAULT,
822                                                          0,
823                                                          DXVA2_VideoDecoderRenderTarget,
824                                                          surface_list,
825                                                          NULL))) {
826         msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed\n");
827         va->surface_count = 0;
828         return VLC_EGENERIC;
829     }
830     for (unsigned i = 0; i < va->surface_count; i++) {
831         vlc_va_surface_t *surface = &va->surface[i];
832         surface->d3d = surface_list[i];
833         surface->refcount = 0;
834         surface->order = 0;
835     }
836     va->surface_width  = fmt->i_width;
837     va->surface_height = fmt->i_height;
838     msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
839             va->surface_count, fmt->i_width, fmt->i_height);
840
841     /* */
842     DXVA2_VideoDesc dsc;
843     ZeroMemory(&dsc, sizeof(dsc));
844     dsc.SampleWidth     = fmt->i_width;
845     dsc.SampleHeight    = fmt->i_height;
846     dsc.Format          = va->render;
847     if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
848         dsc.InputSampleFreq.Numerator   = fmt->i_frame_rate;
849         dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
850     } else {
851         dsc.InputSampleFreq.Numerator   = 0;
852         dsc.InputSampleFreq.Denominator = 0;
853     }
854     dsc.OutputFrameFreq = dsc.InputSampleFreq;
855     dsc.UABProtectionLevel = FALSE;
856     dsc.Reserved = 0;
857
858     /* FIXME I am unsure we can let unknown everywhere */
859     DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
860     ext->SampleFormat = 0;//DXVA2_SampleUnknown;
861     ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
862     ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
863     ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
864     ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
865     ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
866     ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
867
868     /* List all configurations available for the decoder */
869     UINT                      cfg_count = 0;
870     DXVA2_ConfigPictureDecode *cfg_list = NULL;
871     if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
872                                                                     &va->input,
873                                                                     &dsc,
874                                                                     NULL,
875                                                                     &cfg_count,
876                                                                     &cfg_list))) {
877         msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed\n");
878         return VLC_EGENERIC;
879     }
880     msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
881
882     /* Select the best decoder configuration */
883     bool has_cfg = false;
884     for (unsigned i = 0; i < cfg_count; i++) {
885         const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
886
887         /* */
888         msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
889                 i, cfg->ConfigBitstreamRaw);
890
891         /* */
892         if ((!has_cfg && cfg->ConfigBitstreamRaw == 1) ||
893             (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)) {
894             va->cfg = *cfg;
895             has_cfg = true;
896         }
897     }
898     CoTaskMemFree(cfg_list);
899     if (!has_cfg) {
900         msg_Err(va->log, "Failed to find a supported decoder configuration");
901         return VLC_EGENERIC;
902     }
903
904     /* Create the decoder */
905     IDirectXVideoDecoder *decoder;
906     if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
907                                                               &va->input,
908                                                               &dsc,
909                                                               &va->cfg,
910                                                               surface_list,
911                                                               va->surface_count,
912                                                               &decoder))) {
913         msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed\n");
914         return VLC_EGENERIC;
915     }
916     va->decoder = decoder;
917     msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
918     return VLC_SUCCESS;
919 }
920 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
921 {
922     if (va->decoder)
923         IDirectXVideoDecoder_Release(va->decoder);
924     va->decoder = NULL;
925
926     for (unsigned i = 0; i < va->surface_count; i++)
927         IDirect3DSurface9_Release(va->surface[i].d3d);
928     va->surface_count = 0;
929 }
930 static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
931 {
932     msg_Err(va->log, "DxResetVideoDecoder unimplemented");
933     return VLC_EGENERIC;
934 }
935
936 static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
937 {
938     switch (va->render) {
939     case MAKEFOURCC('N','V','1','2'):
940         va->output = MAKEFOURCC('Y','V','1','2');
941         break;
942     default:
943         va->output = va->render;
944         break;
945     }
946     va->surface_cache_size = __MAX((va->surface_width + 0x0f) & ~ 0x0f, 4096);
947     va->surface_cache_base = malloc(16 + va->surface_cache_size);
948     va->surface_cache      = &va->surface_cache_base[16 - ((intptr_t)va->surface_cache_base & 0x0f)];
949 }
950 static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
951 {
952     free(va->surface_cache_base);
953     va->surface_cache_base = NULL;
954     va->surface_cache      = NULL;
955     va->surface_cache_size = 0;
956 }
957
958 /* Copy 64 bytes from srcp to dsp loading data with the SSE>=2 instruction load and
959  * storing data with the SSE>=2 instruction store.
960  */
961 #define COPY64(dstp, srcp, load, store) \
962     asm volatile (                      \
963         load "  0(%[src]), %%xmm1\n"    \
964         load " 16(%[src]), %%xmm2\n"    \
965         load " 32(%[src]), %%xmm3\n"    \
966         load " 48(%[src]), %%xmm4\n"    \
967         store " %%xmm1,    0(%[dst])\n" \
968         store " %%xmm2,   16(%[dst])\n" \
969         store " %%xmm3,   32(%[dst])\n" \
970         store " %%xmm4,   48(%[dst])\n" \
971         : : [dst]"r"(dstp), [src]"r"(srcp) : "memory")
972
973 /* Execute the instruction op only if SSE2 is supported. */
974 #ifdef CAN_COMPILE_SSE2
975 #   define ASM_SSE2(cpu, op) do {          \
976         if (cpu & CPU_CAPABILITY_SSE2)  \
977             asm volatile (op);    \
978     } while (0)
979 #else
980 #   define ASM_SSE2(cpu, op)
981 #endif
982
983 /* Optimized copy from "Uncacheable Speculative Write Combining" memory
984  * as used by some video surface.
985  * XXX It is really efficient only when SSE4.1 is available.
986  */
987 static void CopyFromUswc(uint8_t *dst, size_t dst_pitch,
988                          const uint8_t *src, size_t src_pitch,
989                          unsigned unaligned,
990                          unsigned width, unsigned height,
991                          unsigned cpu)
992 {
993     assert(((intptr_t)dst & 0x0f) == 0 && (dst_pitch & 0x0f) == 0);
994
995     ASM_SSE2(cpu, "mfence");
996     for (unsigned y = 0; y < height; y++) {
997         unsigned x;
998
999         for (x = 0; x < unaligned; x++)
1000             dst[x] = src[x];
1001
1002 #ifdef CAN_COMPILE_SSE4_1
1003         if (cpu & CPU_CAPABILITY_SSE4_1) {
1004             if (!unaligned) {
1005                 for (; x+63 < width; x += 64)
1006                     COPY64(&dst[x], &src[x], "movntdqa", "movdqa");
1007             } else {
1008                 for (; x+63 < width; x += 64)
1009                     COPY64(&dst[x], &src[x], "movntdqa", "movdqu");
1010             }
1011         } else
1012 #endif
1013 #ifdef CAN_COMPILE_SSE2
1014         if (cpu & CPU_CAPABILITY_SSE2) {
1015             if (!unaligned) {
1016                 for (; x+63 < width; x += 64)
1017                     COPY64(&dst[x], &src[x], "movdqa", "movdqa");
1018             } else {
1019                 for (; x+63 < width; x += 64)
1020                     COPY64(&dst[x], &src[x], "movdqa", "movdqu");
1021             }
1022         }
1023 #endif
1024
1025         for (; x < width; x++)
1026             dst[x] = src[x];
1027
1028         src += src_pitch;
1029         dst += dst_pitch;
1030     }
1031 }
1032
1033 static void Copy2d(uint8_t *dst, size_t dst_pitch,
1034                    const uint8_t *src, size_t src_pitch,
1035                    unsigned width, unsigned height,
1036                    unsigned cpu)
1037 {
1038     assert(((intptr_t)src & 0x0f) == 0 && (src_pitch & 0x0f) == 0);
1039
1040     ASM_SSE2(cpu, "mfence");
1041
1042     for (unsigned y = 0; y < height; y++) {
1043         unsigned x = 0;
1044         bool unaligned = ((intptr_t)dst & 0x0f) != 0;
1045
1046 #ifdef CAN_COMPILE_SSE2
1047         if (cpu & CPU_CAPABILITY_SSE2) {
1048             if (!unaligned) {
1049                 for (; x+63 < width; x += 64)
1050                     COPY64(&dst[x], &src[x], "movdqa", "movntdq");
1051             } else {
1052                 for (; x+63 < width; x += 64)
1053                     COPY64(&dst[x], &src[x], "movdqa", "movdqu");
1054             }
1055         }
1056 #endif
1057
1058         for (; x < width; x++)
1059             dst[x] = src[x];
1060
1061         src += src_pitch;
1062         dst += dst_pitch;
1063     }
1064 }
1065
1066 static void SplitUV(uint8_t *dstu, size_t dstu_pitch,
1067                     uint8_t *dstv, size_t dstv_pitch,
1068                     const uint8_t *src, size_t src_pitch,
1069                     unsigned width, unsigned height, unsigned cpu)
1070 {
1071     const uint8_t shuffle[] = { 0, 2, 4, 6, 8, 10, 12, 14,
1072                                 1, 3, 5, 7, 9, 11, 13, 15 };
1073     const uint8_t mask[] = { 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
1074                              0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00 };
1075
1076     assert(((intptr_t)src & 0x0f) == 0 && (src_pitch & 0x0f) == 0);
1077
1078     ASM_SSE2(cpu, "mfence");
1079
1080     for (unsigned y = 0; y < height; y++) {
1081         unsigned x = 0;
1082
1083 #define LOAD64 \
1084     "movdqa  0(%[src]), %%xmm0\n" \
1085     "movdqa 16(%[src]), %%xmm1\n" \
1086     "movdqa 32(%[src]), %%xmm2\n" \
1087     "movdqa 48(%[src]), %%xmm3\n"
1088
1089 #define STORE2X32 \
1090     "movq   %%xmm0,   0(%[dst1])\n" \
1091     "movq   %%xmm1,   8(%[dst1])\n" \
1092     "movhpd %%xmm0,   0(%[dst2])\n" \
1093     "movhpd %%xmm1,   8(%[dst2])\n" \
1094     "movq   %%xmm2,  16(%[dst1])\n" \
1095     "movq   %%xmm3,  24(%[dst1])\n" \
1096     "movhpd %%xmm2,  16(%[dst2])\n" \
1097     "movhpd %%xmm3,  24(%[dst2])\n"
1098
1099 #ifdef CAN_COMPILE_SSSE3
1100         if (cpu & CPU_CAPABILITY_SSSE3) {
1101             for (x = 0; x < (width & ~31); x += 32) {
1102                 asm volatile (
1103                     "movdqu (%[shuffle]), %%xmm7\n"
1104                     LOAD64
1105                     "pshufb  %%xmm7, %%xmm0\n"
1106                     "pshufb  %%xmm7, %%xmm1\n"
1107                     "pshufb  %%xmm7, %%xmm2\n"
1108                     "pshufb  %%xmm7, %%xmm3\n"
1109                     STORE2X32
1110                     : : [dst1]"r"(&dstu[x]), [dst2]"r"(&dstv[x]), [src]"r"(&src[2*x]), [shuffle]"r"(shuffle) : "memory");
1111             }
1112         } else
1113 #endif
1114 #ifdef CAN_COMPILE_SSE2
1115         if (cpu & CPU_CAPABILITY_SSE2) {
1116             for (x = 0; x < (width & ~31); x += 32) {
1117                 asm volatile (
1118                     "movdqu (%[mask]), %%xmm7\n"
1119                     LOAD64
1120                     "movdqa   %%xmm0, %%xmm4\n"
1121                     "movdqa   %%xmm1, %%xmm5\n"
1122                     "movdqa   %%xmm2, %%xmm6\n"
1123                     "psrlw    $8,     %%xmm0\n"
1124                     "psrlw    $8,     %%xmm1\n"
1125                     "pand     %%xmm7, %%xmm4\n"
1126                     "pand     %%xmm7, %%xmm5\n"
1127                     "pand     %%xmm7, %%xmm6\n"
1128                     "packuswb %%xmm4, %%xmm0\n"
1129                     "packuswb %%xmm5, %%xmm1\n"
1130                     "pand     %%xmm3, %%xmm7\n"
1131                     "psrlw    $8,     %%xmm2\n"
1132                     "psrlw    $8,     %%xmm3\n"
1133                     "packuswb %%xmm6, %%xmm2\n"
1134                     "packuswb %%xmm7, %%xmm3\n"
1135                     STORE2X32
1136                     : : [dst2]"r"(&dstu[x]), [dst1]"r"(&dstv[x]), [src]"r"(&src[2*x]), [mask]"r"(mask) : "memory");
1137             }
1138         }
1139 #endif
1140 #undef STORE2X32
1141 #undef LOAD64
1142
1143         for (; x < width; x++) {
1144             dstu[x] = src[2*x+0];
1145             dstv[x] = src[2*x+1];
1146         }
1147         src  += src_pitch;
1148         dstu += dstu_pitch;
1149         dstv += dstv_pitch;
1150     }
1151 }
1152
1153 static void CopyPlane(uint8_t *dst, size_t dst_pitch, const uint8_t *src, size_t src_pitch,
1154                       uint8_t *cache, size_t cache_size,
1155                       unsigned width, unsigned height,
1156                       unsigned cpu)
1157 {
1158     const unsigned w16 = (width+15) & ~15;
1159     const unsigned hstep = cache_size / w16;
1160     assert(hstep > 0);
1161
1162     for (unsigned y = 0; y < height; y += hstep) {
1163         const unsigned unaligned = (intptr_t)src & 0x0f;
1164         const unsigned hblock =  __MIN(hstep, height - y);
1165
1166         /* Copy a bunch of line into our cache */
1167         CopyFromUswc(cache, w16,
1168                      src, src_pitch,
1169                      unaligned,
1170                      width, hblock, cpu);
1171
1172         /* Copy from our cache to the destination */
1173         Copy2d(dst, dst_pitch,
1174                cache, w16,
1175                width, hblock, cpu);
1176
1177         /* */
1178         src += src_pitch * hblock;
1179         dst += dst_pitch * hblock;
1180     }
1181
1182     ASM_SSE2(cpu, "mfence");
1183 }
1184 static void SplitPlanes(uint8_t *dstu, size_t dstu_pitch,
1185                         uint8_t *dstv, size_t dstv_pitch,
1186                         const uint8_t *src, size_t src_pitch,
1187                         uint8_t *cache, size_t cache_size,
1188                         unsigned width, unsigned height,
1189                         unsigned cpu)
1190 {
1191     const unsigned w2_16 = (2*width+15) & ~15;
1192     const unsigned hstep = cache_size / w2_16;
1193     assert(hstep > 0);
1194
1195     for (unsigned y = 0; y < height; y += hstep) {
1196         const unsigned unaligned = (intptr_t)src & 0x0f;
1197         const unsigned hblock =  __MIN(hstep, height - y);
1198
1199         /* Copy a bunch of line into our cache */
1200         CopyFromUswc(cache, w2_16,
1201                      src, src_pitch,
1202                      unaligned,
1203                      2*width, hblock, cpu);
1204
1205         /* Copy from our cache to the destination */
1206         SplitUV(dstu, dstu_pitch,
1207                 dstv, dstv_pitch,
1208                 cache, w2_16,
1209                 width, hblock, cpu);
1210
1211         /* */
1212         src  += src_pitch  * hblock;
1213         dstu += dstu_pitch * hblock;
1214         dstv += dstv_pitch * hblock;
1215     }
1216
1217     ASM_SSE2(cpu, "mfence");
1218 }
1219
1220 static void CopyFromNv12(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     CopyPlane(dst->p[0].p_pixels, dst->p[0].i_pitch,
1228               src->pBits, src->Pitch,
1229               cache, cache_size,
1230               width, height, cpu);
1231     SplitPlanes(dst->p[2].p_pixels, dst->p[2].i_pitch,
1232                 dst->p[1].p_pixels, dst->p[1].i_pitch,
1233                 (const uint8_t*)src->pBits + src->Pitch * height, src->Pitch,
1234                 cache, cache_size,
1235                 width/2, height/2, cpu);
1236
1237     ASM_SSE2(cpu, "emms");
1238 }
1239 static void CopyFromYv12(picture_t *dst, const D3DLOCKED_RECT *src,
1240                          uint8_t *cache, size_t cache_size,
1241                          unsigned width, unsigned height)
1242 {
1243     const unsigned cpu = vlc_CPU();
1244
1245     /* */
1246     for (unsigned n = 0, offset = 0; n < 3; n++) {
1247         const unsigned d = n > 0 ? 2 : 1;
1248         CopyPlane(dst->p[n].p_pixels, dst->p[n].i_pitch,
1249                   (const uint8_t*)src->pBits + offset, src->Pitch/d,
1250                   cache, cache_size,
1251                   width/d, height/d, cpu);
1252         offset += (src->Pitch/d) * (height/d);
1253     }
1254     ASM_SSE2(cpu, "emms");
1255 }
1256 #undef ASM_SSE2
1257 #undef COPY64
1258
1259 #else
1260 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
1261 {
1262     (void)log;
1263     (void)codec_id;
1264     return NULL;
1265 }
1266 #endif