Improved performance when copying video surface in dxva2.
[vlc.git] / modules / codec / avcodec / dxva2.c
1 /*****************************************************************************
2  * va.c: Video Acceleration helpers
3  *****************************************************************************
4  * Copyright (C) 2009 Geoffroy Couprie
5  * Copyright (C) 2009 Laurent Aimar
6  * $Id$
7  *
8  * Authors: Geoffroy Couprie <geal@videolan.org>
9  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_picture.h>
32 #include <vlc_fourcc.h>
33 #include <vlc_cpu.h>
34 #include <assert.h>
35
36 #define HAVE_AVCODEC_DXVA2
37
38 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
39 #   include <libavcodec/avcodec.h>
40 #   ifdef HAVE_AVCODEC_DXVA2
41 #       include <libavcodec/dxva2.h>
42 #   endif
43 #elif defined(HAVE_FFMPEG_AVCODEC_H)
44 #   include <ffmpeg/avcodec.h>
45 #else
46 #   include <avcodec.h>
47 #endif
48
49 #include "avcodec.h"
50 #include "va.h"
51
52 #ifdef HAVE_AVCODEC_DXVA2
53
54 #include <windows.h>
55 #include <windowsx.h>
56 #include <ole2.h>
57 #include <commctrl.h>
58 #include <shlwapi.h>
59 #include <d3d9.h>
60
61 /* FIXME */
62 #define CoTaskMemFree(x)
63
64 /* */
65 #define DXVA2_E_NOT_INITIALIZED     MAKE_HRESULT(1, 4, 4096)
66 #define DXVA2_E_NEW_VIDEO_DEVICE    MAKE_HRESULT(1, 4, 4097)
67 #define DXVA2_E_VIDEO_DEVICE_LOCKED MAKE_HRESULT(1, 4, 4098)
68 #define DXVA2_E_NOT_AVAILABLE       MAKE_HRESULT(1, 4, 4099)
69
70 static const GUID DXVA2_ModeMPEG2_MoComp = {
71     0xe6a9f44b, 0x61b0, 0x4563, {0x9e,0xa4,0x63,0xd2,0xa3,0xc6,0xfe,0x66}
72 };
73 static const GUID DXVA2_ModeMPEG2_IDCT = {
74   0xbf22ad00, 0x03ea, 0x4690, {0x80,0x77,0x47,0x33,0x46,0x20,0x9b,0x7e}
75 };
76 static const GUID DXVA2_ModeMPEG2_VLD = {
77    0xee27417f, 0x5e28, 0x4e65, {0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9}
78 };
79
80 static const GUID DXVA2_ModeH264_A = {
81     0x1b81be64, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
82 };
83 static const GUID DXVA2_ModeH264_B = {
84     0x1b81be65, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
85 };
86 static const GUID DXVA2_ModeH264_C = {
87     0x1b81be66, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
88 };
89 static const GUID DXVA2_ModeH264_D = {
90     0x1b81be67, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
91 };
92 static const GUID DXVA2_ModeH264_E = {
93     0x1b81be68, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
94 };
95 static const GUID DXVA2_ModeH264_F = {
96     0x1b81be69, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
97 };
98 static const GUID DXVA2_ModeWMV8_A = {
99     0x1b81be80, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
100 };
101 static const GUID DXVA2_ModeWMV8_B = {
102     0x1b81be81, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
103 };
104 static const GUID DXVA2_ModeWMV9_A = {
105     0x1b81be90, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
106 };
107 static const GUID DXVA2_ModeWMV9_B = {
108     0x1b81be91, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
109 };
110 static const GUID DXVA2_ModeWMV9_C = {
111     0x1b81be94, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
112 };
113
114 static const GUID DXVA2_ModeVC1_A = {
115     0x1b81beA0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
116 };
117 static const GUID DXVA2_ModeVC1_B = {
118     0x1b81beA1, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
119 };
120 static const GUID DXVA2_ModeVC1_C = {
121     0x1b81beA2, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
122 };
123 static const GUID DXVA2_ModeVC1_D = {
124     0x1b81beA3, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
125 };
126
127 /* */
128 typedef struct {
129     const char   *name;
130     const GUID   *guid;
131     int          codec;
132 } dxva2_mode_t;
133 /* XXX Prefered modes must come first */
134 static const dxva2_mode_t dxva2_modes[] = {
135     { "DXVA2_ModeMPEG2_VLD",    &DXVA2_ModeMPEG2_VLD,     0 },
136     { "DXVA2_ModeMPEG2_MoComp", &DXVA2_ModeMPEG2_MoComp,  0 },
137     { "DXVA2_ModeMPEG2_IDCT",   &DXVA2_ModeMPEG2_IDCT,    0 },
138
139     { "H.264 variable-length decoder (VLD), FGT",               &DXVA2_ModeH264_F, CODEC_ID_H264 },
140     { "H.264 VLD, no FGT",                                      &DXVA2_ModeH264_E, CODEC_ID_H264 },
141     { "H.264 IDCT, FGT",                                        &DXVA2_ModeH264_D, 0,            },
142     { "H.264 inverse discrete cosine transform (IDCT), no FGT", &DXVA2_ModeH264_C, 0,            },
143     { "H.264 MoComp, FGT",                                      &DXVA2_ModeH264_B, 0,            },
144     { "H.264 motion compensation (MoComp), no FGT",             &DXVA2_ModeH264_A, 0,            },
145
146     { "Windows Media Video 8 MoComp",           &DXVA2_ModeWMV8_B, 0 },
147     { "Windows Media Video 8 post processing",  &DXVA2_ModeWMV8_A, 0 },
148
149     {  "Windows Media Video 9 IDCT",            &DXVA2_ModeWMV9_C, 0 },
150     {  "Windows Media Video 9 MoComp",          &DXVA2_ModeWMV9_B, 0 },
151     {  "Windows Media Video 9 post processing", &DXVA2_ModeWMV9_A, 0 },
152
153     { "VC-1 VLD",             &DXVA2_ModeVC1_D, 0 },
154     { "VC-1 IDCT",            &DXVA2_ModeVC1_C, 0 },
155     { "VC-1 MoComp",          &DXVA2_ModeVC1_B, 0 },
156     { "VC-1 post processing", &DXVA2_ModeVC1_A, 0 },
157
158     { NULL, NULL, 0 }
159 };
160
161 static const dxva2_mode_t *Dxva2FindMode(const GUID *guid)
162 {
163     for (unsigned i = 0; dxva2_modes[i].name; i++) {
164         if (IsEqualGUID(dxva2_modes[i].guid, guid))
165             return &dxva2_modes[i];
166     }
167     return NULL;
168 }
169
170 /* */
171 #define VLC_CODEC_NV12 VLC_FOURCC('N','V','1','2') /* TODO move to vlc_fourcc.h */
172 typedef struct {
173     const char   *name;
174     D3DFORMAT    format;
175     vlc_fourcc_t codec;
176 } d3d_format_t;
177 /* XXX Prefered format must come first */
178 static const d3d_format_t d3d_formats[] = {
179     { "YV12",   MAKEFOURCC('Y','V','1','2'),    VLC_CODEC_YV12 },
180     { "NV12",   MAKEFOURCC('N','V','1','2'),    VLC_CODEC_NV12 },
181
182     { NULL, 0, 0 }
183 };
184
185 static const d3d_format_t *D3dFindFormat(D3DFORMAT format)
186 {
187     for (unsigned i = 0; d3d_formats[i].name; i++) {
188         if (d3d_formats[i].format == format)
189             return &d3d_formats[i];
190     }
191     return NULL;
192 }
193
194 static const GUID IID_IDirectXVideoDecoderService = {
195     0xfc51a551, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
196 };
197 static const GUID IID_IDirectXVideoAccelerationService = {
198     0xfc51a550, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
199 };
200
201 /* */
202 typedef struct {
203     LPDIRECT3DSURFACE9 d3d;
204     int                refcount;
205     unsigned int       order;
206 } vlc_va_surface_t;
207
208 #define VA_DXVA2_MAX_SURFACE_COUNT (64)
209 typedef struct
210 {
211     /* */
212     vlc_va_t va;
213
214     /* */
215     vlc_object_t *log;
216     int          codec_id;
217
218     /* DLL */
219         HINSTANCE             hd3d9_dll;
220     HINSTANCE             hdxva2_dll;
221
222     /* Direct3D */
223         D3DPRESENT_PARAMETERS d3dpp;
224         LPDIRECT3D9           d3dobj;
225         LPDIRECT3DDEVICE9     d3ddev;
226
227     /* Device manager */
228     UINT                     token;
229     LPDIRECT3DDEVICEMANAGER9 devmng;
230     HANDLE                   device;
231
232     /* Video service */
233     LPDIRECTXVIDEODECODERSERVICE vs;
234     GUID                         input;
235     D3DFORMAT                    render;
236
237     /* Video decoder */
238     DXVA2_ConfigPictureDecode    cfg;
239     LPDIRECTXVIDEODECODER        decoder;
240
241     /* Option conversion */
242     D3DFORMAT                    output;
243     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     D3DPRESENT_PARAMETERS *d3dpp = &va->d3dpp;
538     ZeroMemory(d3dpp, sizeof(*d3dpp));
539     d3dpp->Flags                  = D3DPRESENTFLAG_VIDEO;
540     d3dpp->Windowed               = TRUE;
541     d3dpp->hDeviceWindow          = NULL;
542     d3dpp->SwapEffect             = D3DSWAPEFFECT_DISCARD;
543     d3dpp->MultiSampleType        = D3DMULTISAMPLE_NONE;
544     d3dpp->PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
545     d3dpp->BackBufferCount        = 0;                  /* FIXME what to put here */
546     d3dpp->BackBufferFormat       = D3DFMT_X8R8G8B8;    /* FIXME what to put here */
547     d3dpp->BackBufferWidth        = 0;
548     d3dpp->BackBufferHeight       = 0;
549     d3dpp->EnableAutoDepthStencil = FALSE;
550
551     /* Direct3D needs a HWND to create a device, even without using ::Present
552     this HWND is used to alert Direct3D when there's a change of focus window.
553     For now, use GetShellWindow, as it looks harmless */
554     LPDIRECT3DDEVICE9 d3ddev;
555     if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
556                                        D3DDEVTYPE_HAL, GetShellWindow(),
557                                        D3DCREATE_SOFTWARE_VERTEXPROCESSING |
558                                        D3DCREATE_MULTITHREADED,
559                                        d3dpp, &d3ddev))) {
560         msg_Err(va->log, "IDirect3D9_CreateDevice failed");
561         return VLC_EGENERIC;
562     }
563     va->d3ddev = d3ddev;
564
565     return VLC_SUCCESS;
566 }
567 /**
568  * It releases a Direct3D device and its resources.
569  */
570 static void D3dDestroyDevice(vlc_va_dxva2_t *va)
571 {
572     if (va->d3ddev)
573         IDirect3DDevice9_Release(va->d3ddev);
574     if (va->d3dobj)
575         IDirect3D9_Release(va->d3dobj);
576 }
577 /**
578  * It describes our Direct3D object
579  */
580 static char *DxDescribe(vlc_va_dxva2_t *va)
581 {
582     D3DADAPTER_IDENTIFIER9 id;
583     ZeroMemory(&id, sizeof(id));
584
585     if (FAILED(IDirect3D9_GetAdapterIdentifier(va->d3dobj,
586                                                D3DADAPTER_DEFAULT, 0, &id)))
587         return strdup("DXVA2 (unknown)");
588
589     char *description;
590     if (asprintf(&description, "DXVA2 (%.*s, vendor %d, device %d, revision %d)",
591                  sizeof(id.Description), id.Description,
592                  id.VendorId, id.DeviceId, id.Revision) < 0)
593         return NULL;
594     return description;
595 }
596
597 /**
598  * It creates a Direct3D device manager
599  */
600 static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
601 {
602     HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
603                                            IDirect3DDeviceManager9 **);
604     CreateDeviceManager9 =
605       (void *)GetProcAddress(va->hdxva2_dll,
606                              TEXT("DXVA2CreateDirect3DDeviceManager9"));
607
608     if (!CreateDeviceManager9) {
609         msg_Err(va->log, "cannot load function\n");
610         return VLC_EGENERIC;
611     }
612     msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
613
614     UINT token;
615     LPDIRECT3DDEVICEMANAGER9 devmng;
616     if (FAILED(CreateDeviceManager9(&token, &devmng))) {
617         msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
618         return VLC_EGENERIC;
619     }
620     va->token  = token;
621     va->devmng = devmng;
622     msg_Info(va->log, "obtained IDirect3DDeviceManager9");
623
624     HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
625     if (FAILED(hr)) {
626         msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
627         return VLC_EGENERIC;
628     }
629     return VLC_SUCCESS;
630 }
631 /**
632  * It destroys a Direct3D device manager
633  */
634 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
635 {
636     if (va->devmng)
637         IDirect3DDeviceManager9_Release(va->devmng);
638 }
639
640 /**
641  * It creates a DirectX video service
642  */
643 static int DxCreateVideoService(vlc_va_dxva2_t *va)
644 {
645     HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
646                                          REFIID riid,
647                                          void **ppService);
648     CreateVideoService =
649       (void *)GetProcAddress(va->hdxva2_dll,
650                              TEXT("DXVA2CreateVideoService"));
651
652     if (!CreateVideoService) {
653         msg_Err(va->log, "cannot load function\n");
654         return 4;
655     }
656     msg_Info(va->log, "DXVA2CreateVideoService Success!");
657
658     HRESULT hr;
659
660     HANDLE device;
661     hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
662     if (FAILED(hr)) {
663         msg_Err(va->log, "OpenDeviceHandle failed");
664         return VLC_EGENERIC;
665     }
666     va->device = device;
667
668     LPDIRECTXVIDEODECODERSERVICE vs;
669     hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
670                                                  &IID_IDirectXVideoDecoderService,
671                                                  &vs);
672     if (FAILED(hr)) {
673         msg_Err(va->log, "GetVideoService failed");
674         return VLC_EGENERIC;
675     }
676     va->vs = vs;
677
678     return VLC_SUCCESS;
679 }
680 /**
681  * It destroys a DirectX video service
682  */
683 static void DxDestroyVideoService(vlc_va_dxva2_t *va)
684 {
685     if (va->device)
686         IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
687     if (va->vs)
688         IDirectXVideoDecoderService_Release(va->vs);
689 }
690 /**
691  * Find the best suited decoder mode GUID and render format.
692  */
693 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
694 {
695     /* Retreive supported modes from the decoder service */
696     UINT input_count = 0;
697     GUID *input_list = NULL;
698     if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
699                                                                  &input_count,
700                                                                  &input_list))) {
701         msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
702         return VLC_EGENERIC;
703     }
704     for (unsigned i = 0; i < input_count; i++) {
705         const GUID *g = &input_list[i];
706         const dxva2_mode_t *mode = Dxva2FindMode(g);
707         if (mode) {
708             msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
709         } else {
710             msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX\n",
711                      (unsigned)g->Data1, g->Data2, g->Data3);
712         }
713     }
714
715     /* Try all supported mode by our priority */
716     for (unsigned i = 0; dxva2_modes[i].name; i++) {
717         const dxva2_mode_t *mode = &dxva2_modes[i];
718         if (!mode->codec || mode->codec != va->codec_id)
719             continue;
720
721         /* */
722         bool is_suported = false;
723         for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
724             is_suported = IsEqualGUID(mode->guid, g);
725         }
726         if (!is_suported)
727             continue;
728
729         /* */
730         msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
731         UINT      output_count = 0;
732         D3DFORMAT *output_list = NULL;
733         if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
734                                                                        &output_count,
735                                                                        &output_list))) {
736             msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
737             continue;
738         }
739         for (unsigned j = 0; j < output_count; j++) {
740             const D3DFORMAT f = output_list[j];
741             const d3d_format_t *format = D3dFindFormat(f);
742             if (format) {
743                 msg_Dbg(va->log, "%s is supported for output", format->name);
744             } else {
745                 msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
746             }
747         }
748
749         /* */
750         for (unsigned j = 0; d3d_formats[j].name; j++) {
751             const d3d_format_t *format = &d3d_formats[j];
752
753             /* */
754             bool is_suported = false;
755             for (unsigned k = 0; !is_suported && k < output_count; k++) {
756                 is_suported = format->format == output_list[k];
757             }
758             if (!is_suported)
759                 continue;
760
761             /* We have our solution */
762             msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
763             *input  = *mode->guid;
764             *output = format->format;
765             CoTaskMemFree(output_list);
766             CoTaskMemFree(input_list);
767             return VLC_SUCCESS;
768         }
769         CoTaskMemFree(output_list);
770     }
771     CoTaskMemFree(input_list);
772     return VLC_EGENERIC;
773 }
774
775 /**
776  * It creates a DXVA2 decoder using the given video format
777  */
778 static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
779                                 int codec_id, const video_format_t *fmt)
780 {
781     /* */
782     msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
783             codec_id, fmt->i_width, fmt->i_height);
784
785     /* Allocates all surfaces needed for the decoder */
786     switch (codec_id) {
787     case CODEC_ID_H264:
788         va->surface_count = 16 + 1;
789         break;
790     default:
791         va->surface_count = 2 + 1;
792         break;
793     }
794     LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
795     if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
796                                                          fmt->i_width,
797                                                          fmt->i_height,
798                                                          va->surface_count - 1,
799                                                          va->render,
800                                                          D3DPOOL_DEFAULT,
801                                                          0,
802                                                          DXVA2_VideoDecoderRenderTarget,
803                                                          surface_list,
804                                                          NULL))) {
805         msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed\n");
806         va->surface_count = 0;
807         return VLC_EGENERIC;
808     }
809     for (unsigned i = 0; i < va->surface_count; i++) {
810         vlc_va_surface_t *surface = &va->surface[i];
811         surface->d3d = surface_list[i];
812         surface->refcount = 0;
813         surface->order = 0;
814     }
815     va->surface_width  = fmt->i_width;
816     va->surface_height = fmt->i_height;
817     msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
818             va->surface_count, fmt->i_width, fmt->i_height);
819
820     /* */
821     DXVA2_VideoDesc dsc;
822     ZeroMemory(&dsc, sizeof(dsc));
823     dsc.SampleWidth     = fmt->i_width;
824     dsc.SampleHeight    = fmt->i_height;
825     dsc.Format          = va->render;
826     if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
827         dsc.InputSampleFreq.Numerator   = fmt->i_frame_rate;
828         dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
829     } else {
830         dsc.InputSampleFreq.Numerator   = 0;
831         dsc.InputSampleFreq.Denominator = 0;
832     }
833     dsc.OutputFrameFreq = dsc.InputSampleFreq;
834     dsc.UABProtectionLevel = FALSE;
835     dsc.Reserved = 0;
836
837     /* FIXME I am unsure we can let unknown everywhere */
838     DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
839     ext->SampleFormat = 0;//DXVA2_SampleUnknown;
840     ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
841     ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
842     ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
843     ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
844     ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
845     ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
846
847     /* List all configurations available for the decoder */
848     UINT                      cfg_count = 0;
849     DXVA2_ConfigPictureDecode *cfg_list = NULL;
850     if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
851                                                                     &va->input,
852                                                                     &dsc,
853                                                                     NULL,
854                                                                     &cfg_count,
855                                                                     &cfg_list))) {
856         msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed\n");
857         return VLC_EGENERIC;
858     }
859     msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
860
861     /* Select the best decoder configuration */
862     bool has_cfg = false;
863     for (unsigned i = 0; i < cfg_count; i++) {
864         const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
865
866         /* */
867         msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
868                 i, cfg->ConfigBitstreamRaw);
869
870         /* */
871         if ((!has_cfg && cfg->ConfigBitstreamRaw == 1) ||
872             (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)) {
873             va->cfg = *cfg;
874             has_cfg = true;
875         }
876     }
877     CoTaskMemFree(cfg_list);
878     if (!has_cfg) {
879         msg_Err(va->log, "Failed to find a supported decoder configuration");
880         return VLC_EGENERIC;
881     }
882
883     /* Create the decoder */
884     LPDIRECTXVIDEODECODER decoder;
885     if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
886                                                               &va->input,
887                                                               &dsc,
888                                                               &va->cfg,
889                                                               surface_list,
890                                                               va->surface_count,
891                                                               &decoder))) {
892         msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed\n");
893         return VLC_EGENERIC;
894     }
895     va->decoder = decoder;
896     msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
897     return VLC_SUCCESS;
898 }
899 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
900 {
901     if (va->decoder)
902         IDirectXVideoDecoder_Release(va->decoder);
903     va->decoder = NULL;
904
905     for (unsigned i = 0; i < va->surface_count; i++)
906         IDirect3DSurface9_Release(va->surface[i].d3d);
907     va->surface_count = 0;
908 }
909 static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
910 {
911     msg_Err(va->log, "DxResetVideoDecoder unimplemented");
912     return VLC_EGENERIC;
913 }
914
915 static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
916 {
917     switch (va->render) {
918     case MAKEFOURCC('N','V','1','2'):
919         va->output = MAKEFOURCC('Y','V','1','2');
920         break;
921     default:
922         va->output = va->render;
923         break;
924     }
925     va->surface_cache_size = __MAX((va->surface_width + 0x0f) & ~ 0x0f, 4096);
926     va->surface_cache_base = malloc(16 + va->surface_cache_size);
927     va->surface_cache      = &va->surface_cache_base[16 - ((intptr_t)va->surface_cache_base & 0x0f)];
928 }
929 static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
930 {
931     free(va->surface_cache_base);
932     va->surface_cache_base = NULL;
933     va->surface_cache      = NULL;
934     va->surface_cache_size = 0;
935 }
936
937 /* Copy 64 bytes from srcp to dsp loading data with the SSE>=2 instruction load and
938  * storing data with the SSE>=2 instruction store.
939  */
940 #define COPY64(dstp, srcp, load, store) \
941     asm volatile (                      \
942         load "  0(%[src]), %%xmm1\n"    \
943         load " 16(%[src]), %%xmm2\n"    \
944         load " 32(%[src]), %%xmm3\n"    \
945         load " 48(%[src]), %%xmm4\n"    \
946         store " %%xmm1,    0(%[dst])\n" \
947         store " %%xmm2,   16(%[dst])\n" \
948         store " %%xmm3,   32(%[dst])\n" \
949         store " %%xmm4,   48(%[dst])\n" \
950         : : [dst]"r"(dstp), [src]"r"(srcp) : "memory")
951
952 /* Execute the instruction op only if SSE2 is supported. */
953 #ifdef CAN_COMPILE_SSE2
954 #   define ASM_SSE2(cpu, op) do {          \
955         if (cpu & CPU_CAPABILITY_SSE2)  \
956             asm volatile (op);    \
957     } while (0)
958 #else
959 #   define ASM_SSE2(cpu, op)
960 #endif
961
962 /* Optimized copy from "Uncacheable Speculative Write Combining" memory
963  * as used by some video surface.
964  * XXX It is really efficient only when SSE4.1 is available.
965  */
966 static void CopyFromUswc(uint8_t *dst, size_t dst_pitch,
967                          const uint8_t *src, size_t src_pitch,
968                          unsigned unaligned,
969                          unsigned width, unsigned height,
970                          unsigned cpu)
971 {
972     assert(((intptr_t)dst & 0x0f) == 0 && (dst_pitch & 0x0f) == 0);
973
974     ASM_SSE2(cpu, "mfence");
975     for (unsigned y = 0; y < height; y++) {
976         unsigned x;
977
978         for (x = 0; x < unaligned; x++)
979             dst[x] = src[x];
980
981 #ifdef CAN_COMPILE_SSE4_1
982         if (cpu & CPU_CAPABILITY_SSE4_1) {
983             if (!unaligned) {
984                 for (; x+63 < width; x += 64)
985                     COPY64(&dst[x], &src[x], "movntdqa", "movdqa");
986             } else {
987                 for (; x+63 < width; x += 64)
988                     COPY64(&dst[x], &src[x], "movntdqa", "movdqu");
989             }
990         } else
991 #endif
992 #ifdef CAN_COMPILE_SSE2
993         if (cpu & CPU_CAPABILITY_SSE2) {
994             if (!unaligned) {
995                 for (; x+63 < width; x += 64)
996                     COPY64(&dst[x], &src[x], "movdqa", "movdqa");
997             } else {
998                 for (; x+63 < width; x += 64)
999                     COPY64(&dst[x], &src[x], "movdqa", "movdqu");
1000             }
1001         }
1002 #endif
1003
1004         for (; x < width; x++)
1005             dst[x] = src[x];
1006
1007         src += src_pitch;
1008         dst += dst_pitch;
1009     }
1010 }
1011
1012 static void Copy2d(uint8_t *dst, size_t dst_pitch,
1013                    const uint8_t *src, size_t src_pitch,
1014                    unsigned width, unsigned height,
1015                    unsigned cpu)
1016 {
1017     assert(((intptr_t)src & 0x0f) == 0 && (src_pitch & 0x0f) == 0);
1018
1019     ASM_SSE2(cpu, "mfence");
1020
1021     for (unsigned y = 0; y < height; y++) {
1022         unsigned x = 0;
1023         bool unaligned = ((intptr_t)dst & 0x0f) != 0;
1024
1025 #ifdef CAN_COMPILE_SSE2
1026         if (cpu & CPU_CAPABILITY_SSE2) {
1027             if (!unaligned) {
1028                 for (; x+63 < width; x += 64)
1029                     COPY64(&dst[x], &src[x], "movdqa", "movntdq");
1030             } else {
1031                 for (; x+63 < width; x += 64)
1032                     COPY64(&dst[x], &src[x], "movdqa", "movdqu");
1033             }
1034         }
1035 #endif
1036
1037         for (; x < width; x++)
1038             dst[x] = src[x];
1039
1040         src += src_pitch;
1041         dst += dst_pitch;
1042     }
1043 }
1044
1045 static void SplitUV(uint8_t *dstu, size_t dstu_pitch,
1046                     uint8_t *dstv, size_t dstv_pitch,
1047                     const uint8_t *src, size_t src_pitch,
1048                     unsigned width, unsigned height, unsigned cpu)
1049 {
1050     const uint8_t shuffle[] = { 0, 2, 4, 6, 8, 10, 12, 14,
1051                                 1, 3, 5, 7, 9, 11, 13, 15 };
1052     const uint8_t mask[] = { 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
1053                              0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00 };
1054
1055     assert(((intptr_t)src & 0x0f) == 0 && (src_pitch & 0x0f) == 0);
1056
1057     ASM_SSE2(cpu, "mfence");
1058
1059     for (unsigned y = 0; y < height; y++) {
1060         unsigned x = 0;
1061
1062 #define LOAD64 \
1063     "movdqa  0(%[src]), %%xmm0\n" \
1064     "movdqa 16(%[src]), %%xmm1\n" \
1065     "movdqa 32(%[src]), %%xmm2\n" \
1066     "movdqa 48(%[src]), %%xmm3\n"
1067
1068 #define STORE2X32 \
1069     "movq   %%xmm0,   0(%[dst1])\n" \
1070     "movq   %%xmm1,   8(%[dst1])\n" \
1071     "movhpd %%xmm0,   0(%[dst2])\n" \
1072     "movhpd %%xmm1,   8(%[dst2])\n" \
1073     "movq   %%xmm2,  16(%[dst1])\n" \
1074     "movq   %%xmm3,  24(%[dst1])\n" \
1075     "movhpd %%xmm2,  16(%[dst2])\n" \
1076     "movhpd %%xmm3,  24(%[dst2])\n"
1077
1078 #ifdef CAN_COMPILE_SSSE3
1079         if (cpu & CPU_CAPABILITY_SSSE3) {
1080             for (x = 0; x < (width & ~31); x += 32) {
1081                 asm volatile (
1082                     "movdqu (%[shuffle]), %%xmm7\n"
1083                     LOAD64
1084                     "pshufb  %%xmm7, %%xmm0\n"
1085                     "pshufb  %%xmm7, %%xmm1\n"
1086                     "pshufb  %%xmm7, %%xmm2\n"
1087                     "pshufb  %%xmm7, %%xmm3\n"
1088                     STORE2X32
1089                     : : [dst1]"r"(&dstu[x]), [dst2]"r"(&dstv[x]), [src]"r"(&src[2*x]), [shuffle]"r"(shuffle) : "memory");
1090             }
1091         } else
1092 #endif
1093 #ifdef CAN_COMPILE_SSE2
1094         if (cpu & CPU_CAPABILITY_SSE2) {
1095             for (x = 0; x < (width & ~31); x += 32) {
1096                 asm volatile (
1097                     "movdqu (%[mask]), %%xmm7\n"
1098                     LOAD64
1099                     "movdqa   %%xmm0, %%xmm4\n"
1100                     "movdqa   %%xmm1, %%xmm5\n"
1101                     "movdqa   %%xmm2, %%xmm6\n"
1102                     "psrlw    $8,     %%xmm0\n"
1103                     "psrlw    $8,     %%xmm1\n"
1104                     "pand     %%xmm7, %%xmm4\n"
1105                     "pand     %%xmm7, %%xmm5\n"
1106                     "pand     %%xmm7, %%xmm6\n"
1107                     "packuswb %%xmm4, %%xmm0\n"
1108                     "packuswb %%xmm5, %%xmm1\n"
1109                     "pand     %%xmm3, %%xmm7\n"
1110                     "psrlw    $8,     %%xmm2\n"
1111                     "psrlw    $8,     %%xmm3\n"
1112                     "packuswb %%xmm6, %%xmm2\n"
1113                     "packuswb %%xmm7, %%xmm3\n"
1114                     STORE2X32
1115                     : : [dst2]"r"(&dstu[x]), [dst1]"r"(&dstv[x]), [src]"r"(&src[2*x]), [mask]"r"(mask) : "memory");
1116             }
1117         }
1118 #endif
1119 #undef STORE2X32
1120 #undef LOAD64
1121
1122         for (; x < width; x++) {
1123             dstu[x] = src[2*x+0];
1124             dstv[x] = src[2*x+1];
1125         }
1126         src  += src_pitch;
1127         dstu += dstu_pitch;
1128         dstv += dstv_pitch;
1129     }
1130 }
1131
1132 static void CopyPlane(uint8_t *dst, size_t dst_pitch, const uint8_t *src, size_t src_pitch,
1133                       uint8_t *cache, size_t cache_size,
1134                       unsigned width, unsigned height,
1135                       unsigned cpu)
1136 {
1137     const unsigned w16 = (width+15) & ~15;
1138     const unsigned hstep = cache_size / w16;
1139     assert(hstep > 0);
1140
1141     for (unsigned y = 0; y < height; y += hstep) {
1142         const unsigned unaligned = (intptr_t)src & 0x0f;
1143         const unsigned hblock =  __MIN(hstep, height - y);
1144
1145         /* Copy a bunch of line into our cache */
1146         CopyFromUswc(cache, w16,
1147                      src, src_pitch,
1148                      unaligned,
1149                      width, hblock, cpu);
1150
1151         /* Copy from our cache to the destination */
1152         Copy2d(dst, dst_pitch,
1153                cache, w16,
1154                width, hblock, cpu);
1155
1156         /* */
1157         src += src_pitch * hblock;
1158         dst += dst_pitch * hblock;
1159     }
1160
1161     ASM_SSE2(cpu, "mfence");
1162 }
1163 static void SplitPlanes(uint8_t *dstu, size_t dstu_pitch,
1164                         uint8_t *dstv, size_t dstv_pitch,
1165                         const uint8_t *src, size_t src_pitch,
1166                         uint8_t *cache, size_t cache_size,
1167                         unsigned width, unsigned height,
1168                         unsigned cpu)
1169 {
1170     const unsigned w2_16 = (2*width+15) & ~15;
1171     const unsigned hstep = cache_size / w2_16;
1172     assert(hstep > 0);
1173
1174     for (unsigned y = 0; y < height; y += hstep) {
1175         const unsigned unaligned = (intptr_t)src & 0x0f;
1176         const unsigned hblock =  __MIN(hstep, height - y);
1177
1178         /* Copy a bunch of line into our cache */
1179         CopyFromUswc(cache, w2_16,
1180                      src, src_pitch,
1181                      unaligned,
1182                      2*width, hblock, cpu);
1183
1184         /* Copy from our cache to the destination */
1185         SplitUV(dstu, dstu_pitch,
1186                 dstv, dstv_pitch,
1187                 cache, w2_16,
1188                 width, hblock, cpu);
1189
1190         /* */
1191         src  += src_pitch  * hblock;
1192         dstu += dstu_pitch * hblock;
1193         dstv += dstv_pitch * hblock;
1194     }
1195
1196     ASM_SSE2(cpu, "mfence");
1197 }
1198
1199 static void CopyFromNv12(picture_t *dst, const D3DLOCKED_RECT *src,
1200                          uint8_t *cache, size_t cache_size,
1201                          unsigned width, unsigned height)
1202 {
1203     const unsigned cpu = vlc_CPU();
1204
1205     /* */
1206     CopyPlane(dst->p[0].p_pixels, dst->p[0].i_pitch,
1207               src->pBits, src->Pitch,
1208               cache, cache_size,
1209               width, height, cpu);
1210     SplitPlanes(dst->p[2].p_pixels, dst->p[2].i_pitch,
1211                 dst->p[1].p_pixels, dst->p[1].i_pitch,
1212                 (const uint8_t*)src->pBits + src->Pitch * height, src->Pitch,
1213                 cache, cache_size,
1214                 width/2, height/2, cpu);
1215
1216     ASM_SSE2(cpu, "emms");
1217 }
1218 static void CopyFromYv12(picture_t *dst, const D3DLOCKED_RECT *src,
1219                          uint8_t *cache, size_t cache_size,
1220                          unsigned width, unsigned height)
1221 {
1222     const unsigned cpu = vlc_CPU();
1223
1224     /* */
1225     for (unsigned n = 0, offset = 0; n < 3; n++) {
1226         const unsigned d = n > 0 ? 2 : 1;
1227         CopyPlane(dst->p[n].p_pixels, dst->p[n].i_pitch,
1228                   (const uint8_t*)src->pBits + offset, src->Pitch/d,
1229                   cache, cache_size,
1230                   width/d, height/d, cpu);
1231         offset += (src->Pitch/d) * (height/d);
1232     }
1233     ASM_SSE2(cpu, "emms");
1234 }
1235 #undef ASM_SSE2
1236 #undef COPY64
1237
1238 #else
1239 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
1240 {
1241     return NULL;
1242 }
1243 #endif