1 /*****************************************************************************
2 * dxva2.c: Video Acceleration helpers
3 *****************************************************************************
4 * Copyright (C) 2009 Geoffroy Couprie
5 * Copyright (C) 2009 Laurent Aimar
8 * Authors: Geoffroy Couprie <geal@videolan.org>
9 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
31 # if _WIN32_WINNT < 0x600
32 /* dxva2 needs Vista support */
34 # define _WIN32_WINNT 0x600
39 #include <vlc_common.h>
40 #include <vlc_picture.h>
41 #include <vlc_fourcc.h>
43 #include <vlc_plugin.h>
45 #include <libavcodec/avcodec.h>
46 # define DXVA2API_USE_BITFIELDS
48 # include <libavcodec/dxva2.h>
54 static int Open(vlc_va_t *, int, const es_format_t *);
55 static void Close(vlc_va_t *);
58 set_description(N_("DirectX Video Acceleration (DXVA) 2.0"))
59 set_capability("hw decoder", 50)
60 set_category(CAT_INPUT)
61 set_subcategory(SUBCAT_INPUT_VCODEC)
62 set_callbacks(Open, Close)
73 #include <initguid.h> /* must be last included to not redefine existing GUIDs */
75 /* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
76 * assume that they are declared in dxva2api.h */
77 #define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
82 # if !defined(__MINGW64_VERSION_MAJOR)
84 # define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
85 # define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
90 #endif /* __MINGW32__ */
92 MS_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
93 MS_GUID(IID_IDirectXVideoAccelerationService, 0xfc51a550, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
95 MS_GUID (DXVA_NoEncrypt, 0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
97 /* Codec capabilities GUID, sorted by codec */
98 MS_GUID (DXVA2_ModeMPEG2_MoComp, 0xe6a9f44b, 0x61b0, 0x4563, 0x9e, 0xa4, 0x63, 0xd2, 0xa3, 0xc6, 0xfe, 0x66);
99 MS_GUID (DXVA2_ModeMPEG2_IDCT, 0xbf22ad00, 0x03ea, 0x4690, 0x80, 0x77, 0x47, 0x33, 0x46, 0x20, 0x9b, 0x7e);
100 MS_GUID (DXVA2_ModeMPEG2_VLD, 0xee27417f, 0x5e28, 0x4e65, 0xbe, 0xea, 0x1d, 0x26, 0xb5, 0x08, 0xad, 0xc9);
101 DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e, 0x4f04, 0x9f, 0xd3, 0x92, 0x53, 0xdd, 0x32, 0x74, 0x60);
102 DEFINE_GUID(DXVA2_ModeMPEG1_VLD, 0x6f3ec719, 0x3735, 0x42cc, 0x80, 0x63, 0x65, 0xcc, 0x3c, 0xb3, 0x66, 0x16);
104 MS_GUID (DXVA2_ModeH264_A, 0x1b81be64, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
105 MS_GUID (DXVA2_ModeH264_B, 0x1b81be65, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
106 MS_GUID (DXVA2_ModeH264_C, 0x1b81be66, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
107 MS_GUID (DXVA2_ModeH264_D, 0x1b81be67, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
108 MS_GUID (DXVA2_ModeH264_E, 0x1b81be68, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
109 MS_GUID (DXVA2_ModeH264_F, 0x1b81be69, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
110 DEFINE_GUID(DXVA_ModeH264_VLD_Multiview, 0x9901CCD3, 0xca12, 0x4b7e, 0x86, 0x7a, 0xe2, 0x22, 0x3d, 0x92, 0x55, 0xc3); // MVC
111 DEFINE_GUID(DXVA_ModeH264_VLD_WithFMOASO_NoFGT, 0xd5f04ff9, 0x3418, 0x45d8, 0x95, 0x61, 0x32, 0xa7, 0x6a, 0xae, 0x2d, 0xdd);
112 DEFINE_GUID(DXVADDI_Intel_ModeH264_A, 0x604F8E64, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
113 DEFINE_GUID(DXVADDI_Intel_ModeH264_C, 0x604F8E66, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
114 DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6); // DXVA_Intel_H264_NoFGT_ClearVideo
115 DEFINE_GUID(DXVA_ModeH264_VLD_NoFGT_Flash, 0x4245F676, 0x2BBC, 0x4166, 0xa0, 0xBB, 0x54, 0xE7, 0xB8, 0x49, 0xC3, 0x80);
117 MS_GUID (DXVA2_ModeWMV8_A, 0x1b81be80, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
118 MS_GUID (DXVA2_ModeWMV8_B, 0x1b81be81, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
120 MS_GUID (DXVA2_ModeWMV9_A, 0x1b81be90, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
121 MS_GUID (DXVA2_ModeWMV9_B, 0x1b81be91, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
122 MS_GUID (DXVA2_ModeWMV9_C, 0x1b81be94, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
124 MS_GUID (DXVA2_ModeVC1_A, 0x1b81beA0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
125 MS_GUID (DXVA2_ModeVC1_B, 0x1b81beA1, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
126 MS_GUID (DXVA2_ModeVC1_C, 0x1b81beA2, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
127 MS_GUID (DXVA2_ModeVC1_D, 0x1b81beA3, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
128 DEFINE_GUID(DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5); // August 2010 update
129 DEFINE_GUID(DXVA_Intel_VC1_ClearVideo, 0xBCC5DB6D, 0xA2B6, 0x4AF0, 0xAC, 0xE4, 0xAD, 0xB1, 0xF7, 0x87, 0xBC, 0x89);
130 DEFINE_GUID(DXVA_Intel_VC1_ClearVideo_2, 0xE07EC519, 0xE651, 0x4CD6, 0xAC, 0x84, 0x13, 0x70, 0xCC, 0xEE, 0xC8, 0x51);
132 DEFINE_GUID(DXVA_nVidia_MPEG4_ASP, 0x9947EC6F, 0x689B, 0x11DC, 0xA3, 0x20, 0x00, 0x19, 0xDB, 0xBC, 0x41, 0x84);
133 DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_Simple, 0xefd64d74, 0xc9e8, 0x41d7, 0xa5, 0xe9, 0xe9, 0xb0, 0xe3, 0x9f, 0xa3, 0x19);
134 DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_NoGMC, 0xed418a9f, 0x010d, 0x4eda, 0x9a, 0xe3, 0x9a, 0x65, 0x35, 0x8d, 0x8d, 0x2e);
135 DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_GMC, 0xab998b5b, 0x4258, 0x44a9, 0x9f, 0xeb, 0x94, 0xe5, 0x97, 0xa6, 0xba, 0xae);
136 DEFINE_GUID(DXVA_ModeMPEG4pt2_VLD_AdvSimple_Avivo, 0x7C74ADC6, 0xe2ba, 0x4ade, 0x86, 0xde, 0x30, 0xbe, 0xab, 0xb4, 0x0c, 0xc1);
145 /* XXX Prefered modes must come first */
146 static const dxva2_mode_t dxva2_modes[] = {
148 { "MPEG-2 variable-length decoder", &DXVA2_ModeMPEG2_VLD, CODEC_ID_MPEG2VIDEO },
149 { "MPEG-2 & MPEG-1 variable-length decoder", &DXVA2_ModeMPEG2and1_VLD, CODEC_ID_MPEG2VIDEO },
150 { "MPEG-2 motion compensation", &DXVA2_ModeMPEG2_MoComp, 0 },
151 { "MPEG-2 inverse discrete cosine transform", &DXVA2_ModeMPEG2_IDCT, 0 },
153 { "MPEG-1 variable-length decoder", &DXVA2_ModeMPEG1_VLD, 0 },
156 { "H.264 variable-length decoder, film grain technology", &DXVA2_ModeH264_F, CODEC_ID_H264 },
157 { "H.264 variable-length decoder, no film grain technology", &DXVA2_ModeH264_E, CODEC_ID_H264 },
158 { "H.264 variable-length decoder, no film grain technology (Intel ClearVideo)", &DXVADDI_Intel_ModeH264_E, CODEC_ID_H264 },
159 { "H.264 variable-length decoder, no film grain technology, FMO/ASO", &DXVA_ModeH264_VLD_WithFMOASO_NoFGT, CODEC_ID_H264 },
160 { "H.264 variable-length decoder, no film grain technology, Flash", &DXVA_ModeH264_VLD_NoFGT_Flash, CODEC_ID_H264 },
162 { "H.264 inverse discrete cosine transform, film grain technology", &DXVA2_ModeH264_D, 0 },
163 { "H.264 inverse discrete cosine transform, no film grain technology", &DXVA2_ModeH264_C, 0 },
164 { "H.264 inverse discrete cosine transform, no film grain technology (Intel)", &DXVADDI_Intel_ModeH264_C, 0 },
166 { "H.264 motion compensation, film grain technology", &DXVA2_ModeH264_B, 0 },
167 { "H.264 motion compensation, no film grain technology", &DXVA2_ModeH264_A, 0 },
168 { "H.264 motion compensation, no film grain technology (Intel)", &DXVADDI_Intel_ModeH264_A, 0 },
171 { "Windows Media Video 8 motion compensation", &DXVA2_ModeWMV8_B, 0 },
172 { "Windows Media Video 8 post processing", &DXVA2_ModeWMV8_A, 0 },
174 { "Windows Media Video 9 IDCT", &DXVA2_ModeWMV9_C, 0 },
175 { "Windows Media Video 9 motion compensation", &DXVA2_ModeWMV9_B, 0 },
176 { "Windows Media Video 9 post processing", &DXVA2_ModeWMV9_A, 0 },
179 { "VC-1 variable-length decoder", &DXVA2_ModeVC1_D, CODEC_ID_VC1 },
180 { "VC-1 variable-length decoder", &DXVA2_ModeVC1_D, CODEC_ID_WMV3 },
181 { "VC-1 variable-length decoder", &DXVA2_ModeVC1_D2010, CODEC_ID_VC1 },
182 { "VC-1 variable-length decoder", &DXVA2_ModeVC1_D2010, CODEC_ID_WMV3 },
183 { "VC-1 variable-length decoder 2 (Intel)", &DXVA_Intel_VC1_ClearVideo_2, 0 },
184 { "VC-1 variable-length decoder (Intel)", &DXVA_Intel_VC1_ClearVideo, 0 },
186 { "VC-1 inverse discrete cosine transform", &DXVA2_ModeVC1_C, 0 },
187 { "VC-1 motion compensation", &DXVA2_ModeVC1_B, 0 },
188 { "VC-1 post processing", &DXVA2_ModeVC1_A, 0 },
190 /* Xvid/Divx: TODO */
191 { "MPEG-4 Part 2 nVidia bitstream decoder", &DXVA_nVidia_MPEG4_ASP, 0 },
192 { "MPEG-4 Part 2 variable-length decoder, Simple Profile", &DXVA_ModeMPEG4pt2_VLD_Simple, 0 },
193 { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, no GMC", &DXVA_ModeMPEG4pt2_VLD_AdvSimple_NoGMC, 0 },
194 { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, GMC", &DXVA_ModeMPEG4pt2_VLD_AdvSimple_GMC, 0 },
195 { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, Avivo", &DXVA_ModeMPEG4pt2_VLD_AdvSimple_Avivo, 0 },
200 static const dxva2_mode_t *Dxva2FindMode(const GUID *guid)
202 for (unsigned i = 0; dxva2_modes[i].name; i++) {
203 if (IsEqualGUID(dxva2_modes[i].guid, guid))
204 return &dxva2_modes[i];
215 /* XXX Prefered format must come first */
216 static const d3d_format_t d3d_formats[] = {
217 { "YV12", MAKEFOURCC('Y','V','1','2'), VLC_CODEC_YV12 },
218 { "NV12", MAKEFOURCC('N','V','1','2'), VLC_CODEC_NV12 },
219 { "IMC3", MAKEFOURCC('I','M','C','3'), VLC_CODEC_YV12 },
224 static const d3d_format_t *D3dFindFormat(D3DFORMAT format)
226 for (unsigned i = 0; d3d_formats[i].name; i++) {
227 if (d3d_formats[i].format == format)
228 return &d3d_formats[i];
235 LPDIRECT3DSURFACE9 d3d;
240 #define VA_DXVA2_MAX_SURFACE_COUNT (64)
250 HINSTANCE hdxva2_dll;
253 D3DPRESENT_PARAMETERS d3dpp;
255 D3DADAPTER_IDENTIFIER9 d3dai;
256 LPDIRECT3DDEVICE9 d3ddev;
260 IDirect3DDeviceManager9 *devmng;
264 IDirectXVideoDecoderService *vs;
269 DXVA2_ConfigPictureDecode cfg;
270 IDirectXVideoDecoder *decoder;
272 /* Option conversion */
274 copy_cache_t surface_cache;
277 struct dxva_context hw;
280 unsigned surface_count;
281 unsigned surface_order;
284 vlc_fourcc_t surface_chroma;
286 vlc_va_surface_t surface[VA_DXVA2_MAX_SURFACE_COUNT];
287 LPDIRECT3DSURFACE9 hw_surface[VA_DXVA2_MAX_SURFACE_COUNT];
289 typedef struct vlc_va_sys_t vlc_va_dxva2_t;
292 static vlc_va_dxva2_t *vlc_va_dxva2_Get(vlc_va_t *external)
294 vlc_va_dxva2_t *va = external->sys;
295 assert(VLC_OBJECT(external) == va->log);
300 static int D3dCreateDevice(vlc_va_dxva2_t *);
301 static void D3dDestroyDevice(vlc_va_dxva2_t *);
302 static char *DxDescribe(vlc_va_dxva2_t *);
304 static int D3dCreateDeviceManager(vlc_va_dxva2_t *);
305 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *);
307 static int DxCreateVideoService(vlc_va_dxva2_t *);
308 static void DxDestroyVideoService(vlc_va_dxva2_t *);
309 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *, GUID *input, D3DFORMAT *output);
311 static int DxCreateVideoDecoder(vlc_va_dxva2_t *,
312 int codec_id, const video_format_t *);
313 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *);
314 static int DxResetVideoDecoder(vlc_va_dxva2_t *);
316 static void DxCreateVideoConversion(vlc_va_dxva2_t *);
317 static void DxDestroyVideoConversion(vlc_va_dxva2_t *);
320 static int Setup(vlc_va_t *external, void **hw, vlc_fourcc_t *chroma,
321 int width, int height)
323 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
325 if (va->width == width && va->height == height && va->decoder)
329 DxDestroyVideoConversion(va);
330 DxDestroyVideoDecoder(va);
334 if (width <= 0 || height <= 0)
337 /* FIXME transmit a video_format_t by VaSetup directly */
339 memset(&fmt, 0, sizeof(fmt));
341 fmt.i_height = height;
343 if (DxCreateVideoDecoder(va, va->codec_id, &fmt))
346 va->hw.decoder = va->decoder;
347 va->hw.cfg = &va->cfg;
348 va->hw.surface_count = va->surface_count;
349 va->hw.surface = va->hw_surface;
350 for (unsigned i = 0; i < va->surface_count; i++)
351 va->hw.surface[i] = va->surface[i].d3d;
354 DxCreateVideoConversion(va);
359 const d3d_format_t *output = D3dFindFormat(va->output);
360 *chroma = output->codec;
365 static int Extract(vlc_va_t *external, picture_t *picture, AVFrame *ff)
367 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
368 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
370 if (!va->surface_cache.buffer)
374 assert(va->output == MAKEFOURCC('Y','V','1','2'));
378 if (FAILED(IDirect3DSurface9_LockRect(d3d, &lock, NULL, D3DLOCK_READONLY))) {
379 msg_Err(va->log, "Failed to lock surface");
383 if (va->render == MAKEFOURCC('Y','V','1','2') ||
384 va->render == MAKEFOURCC('I','M','C','3')) {
385 bool imc3 = va->render == MAKEFOURCC('I','M','C','3');
386 size_t chroma_pitch = imc3 ? lock.Pitch : (lock.Pitch / 2);
394 uint8_t *plane[3] = {
395 (uint8_t*)lock.pBits,
396 (uint8_t*)lock.pBits + pitch[0] * va->surface_height,
397 (uint8_t*)lock.pBits + pitch[0] * va->surface_height
398 + pitch[1] * va->surface_height / 2,
402 uint8_t *V = plane[1];
406 CopyFromYv12(picture, plane, pitch,
407 va->width, va->height,
410 assert(va->render == MAKEFOURCC('N','V','1','2'));
411 uint8_t *plane[2] = {
413 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
419 CopyFromNv12(picture, plane, pitch,
420 va->width, va->height,
425 IDirect3DSurface9_UnlockRect(d3d);
428 /* FIXME it is nearly common with VAAPI */
429 static int Get(vlc_va_t *external, AVFrame *ff)
431 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
433 /* Check the device */
434 HRESULT hr = IDirect3DDeviceManager9_TestDevice(va->devmng, va->device);
435 if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
436 if (DxResetVideoDecoder(va))
438 } else if (FAILED(hr)) {
439 msg_Err(va->log, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
443 /* Grab an unused surface, in case none are, try the oldest
444 * XXX using the oldest is a workaround in case a problem happens with libavcodec */
446 for (i = 0, old = 0; i < va->surface_count; i++) {
447 vlc_va_surface_t *surface = &va->surface[i];
449 if (!surface->refcount)
452 if (surface->order < va->surface[old].order)
455 if (i >= va->surface_count)
458 vlc_va_surface_t *surface = &va->surface[i];
460 surface->refcount = 1;
461 surface->order = va->surface_order++;
464 for (int i = 0; i < 4; i++) {
468 if (i == 0 || i == 3)
469 ff->data[i] = (void*)surface->d3d;/* Yummie */
473 static void Release(vlc_va_t *external, AVFrame *ff)
475 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
476 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
478 for (unsigned i = 0; i < va->surface_count; i++) {
479 vlc_va_surface_t *surface = &va->surface[i];
481 if (surface->d3d == d3d)
485 static void Close(vlc_va_t *external)
487 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
489 DxDestroyVideoConversion(va);
490 DxDestroyVideoDecoder(va);
491 DxDestroyVideoService(va);
492 D3dDestroyDeviceManager(va);
493 D3dDestroyDevice(va);
496 FreeLibrary(va->hdxva2_dll);
498 FreeLibrary(va->hd3d9_dll);
500 free(external->description);
504 static int Open(vlc_va_t *external, int codec_id, const es_format_t *fmt)
506 vlc_va_dxva2_t *va = calloc(1, sizeof(*va));
512 va->log = VLC_OBJECT(external);
513 va->codec_id = codec_id;
517 va->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
518 if (!va->hd3d9_dll) {
519 msg_Warn(va->log, "cannot load d3d9.dll");
522 va->hdxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
523 if (!va->hdxva2_dll) {
524 msg_Warn(va->log, "cannot load dxva2.dll");
527 msg_Dbg(va->log, "DLLs loaded");
530 if (D3dCreateDevice(va)) {
531 msg_Err(va->log, "Failed to create Direct3D device");
534 msg_Dbg(va->log, "D3dCreateDevice succeed");
536 if (D3dCreateDeviceManager(va)) {
537 msg_Err(va->log, "D3dCreateDeviceManager failed");
541 if (DxCreateVideoService(va)) {
542 msg_Err(va->log, "DxCreateVideoService failed");
547 if (DxFindVideoServiceConversion(va, &va->input, &va->render)) {
548 msg_Err(va->log, "DxFindVideoServiceConversion failed");
552 /* TODO print the hardware name/vendor for debugging purposes */
553 external->description = DxDescribe(va);
554 external->pix_fmt = PIX_FMT_DXVA2_VLD;
555 external->setup = Setup;
557 external->release = Release;
558 external->extract = Extract;
568 * It creates a Direct3D device usable for DXVA 2
570 static int D3dCreateDevice(vlc_va_dxva2_t *va)
573 LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
574 Create9 = (void *)GetProcAddress(va->hd3d9_dll,
575 TEXT("Direct3DCreate9"));
577 msg_Err(va->log, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
583 d3dobj = Create9(D3D_SDK_VERSION);
585 msg_Err(va->log, "Direct3DCreate9 failed");
591 D3DADAPTER_IDENTIFIER9 *d3dai = &va->d3dai;
592 if (FAILED(IDirect3D9_GetAdapterIdentifier(va->d3dobj,
593 D3DADAPTER_DEFAULT, 0, d3dai))) {
594 msg_Warn(va->log, "IDirect3D9_GetAdapterIdentifier failed");
595 ZeroMemory(d3dai, sizeof(*d3dai));
599 D3DPRESENT_PARAMETERS *d3dpp = &va->d3dpp;
600 ZeroMemory(d3dpp, sizeof(*d3dpp));
601 d3dpp->Flags = D3DPRESENTFLAG_VIDEO;
602 d3dpp->Windowed = TRUE;
603 d3dpp->hDeviceWindow = NULL;
604 d3dpp->SwapEffect = D3DSWAPEFFECT_DISCARD;
605 d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
606 d3dpp->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
607 d3dpp->BackBufferCount = 0; /* FIXME what to put here */
608 d3dpp->BackBufferFormat = D3DFMT_X8R8G8B8; /* FIXME what to put here */
609 d3dpp->BackBufferWidth = 0;
610 d3dpp->BackBufferHeight = 0;
611 d3dpp->EnableAutoDepthStencil = FALSE;
613 /* Direct3D needs a HWND to create a device, even without using ::Present
614 this HWND is used to alert Direct3D when there's a change of focus window.
615 For now, use GetDesktopWindow, as it looks harmless */
616 LPDIRECT3DDEVICE9 d3ddev;
617 if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
618 D3DDEVTYPE_HAL, GetDesktopWindow(),
619 D3DCREATE_SOFTWARE_VERTEXPROCESSING |
620 D3DCREATE_MULTITHREADED,
622 msg_Err(va->log, "IDirect3D9_CreateDevice failed");
630 * It releases a Direct3D device and its resources.
632 static void D3dDestroyDevice(vlc_va_dxva2_t *va)
635 IDirect3DDevice9_Release(va->d3ddev);
637 IDirect3D9_Release(va->d3dobj);
640 * It describes our Direct3D object
642 static char *DxDescribe(vlc_va_dxva2_t *va)
644 static const struct {
649 { 0x10DE, "NVIDIA" },
652 { 0x5333, "S3 Graphics" },
655 D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
657 const char *vendor = "Unknown";
658 for (int i = 0; vendors[i].id != 0; i++) {
659 if (vendors[i].id == id->VendorId) {
660 vendor = vendors[i].name;
666 if (asprintf(&description, "DXVA2 (%.*s, vendor %lu(%s), device %lu, revision %lu)",
667 sizeof(id->Description), id->Description,
668 id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
674 * It creates a Direct3D device manager
676 static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
678 HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
679 IDirect3DDeviceManager9 **);
680 CreateDeviceManager9 =
681 (void *)GetProcAddress(va->hdxva2_dll,
682 TEXT("DXVA2CreateDirect3DDeviceManager9"));
684 if (!CreateDeviceManager9) {
685 msg_Err(va->log, "cannot load function");
688 msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
691 IDirect3DDeviceManager9 *devmng;
692 if (FAILED(CreateDeviceManager9(&token, &devmng))) {
693 msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
698 msg_Info(va->log, "obtained IDirect3DDeviceManager9");
700 HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
702 msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
708 * It destroys a Direct3D device manager
710 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
713 IDirect3DDeviceManager9_Release(va->devmng);
717 * It creates a DirectX video service
719 static int DxCreateVideoService(vlc_va_dxva2_t *va)
721 HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
725 (void *)GetProcAddress(va->hdxva2_dll,
726 TEXT("DXVA2CreateVideoService"));
728 if (!CreateVideoService) {
729 msg_Err(va->log, "cannot load function");
732 msg_Info(va->log, "DXVA2CreateVideoService Success!");
737 hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
739 msg_Err(va->log, "OpenDeviceHandle failed");
744 IDirectXVideoDecoderService *vs;
745 hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
746 &IID_IDirectXVideoDecoderService,
749 msg_Err(va->log, "GetVideoService failed");
757 * It destroys a DirectX video service
759 static void DxDestroyVideoService(vlc_va_dxva2_t *va)
762 IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
764 IDirectXVideoDecoderService_Release(va->vs);
767 * Find the best suited decoder mode GUID and render format.
769 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
771 /* Retreive supported modes from the decoder service */
772 UINT input_count = 0;
773 GUID *input_list = NULL;
774 if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
777 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
780 for (unsigned i = 0; i < input_count; i++) {
781 const GUID *g = &input_list[i];
782 const dxva2_mode_t *mode = Dxva2FindMode(g);
784 msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
786 msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX",
787 (unsigned)g->Data1, g->Data2, g->Data3);
791 /* Try all supported mode by our priority */
792 for (unsigned i = 0; dxva2_modes[i].name; i++) {
793 const dxva2_mode_t *mode = &dxva2_modes[i];
794 if (!mode->codec || mode->codec != va->codec_id)
798 bool is_suported = false;
799 for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
800 is_suported = IsEqualGUID(mode->guid, g);
806 msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
807 UINT output_count = 0;
808 D3DFORMAT *output_list = NULL;
809 if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
812 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
815 for (unsigned j = 0; j < output_count; j++) {
816 const D3DFORMAT f = output_list[j];
817 const d3d_format_t *format = D3dFindFormat(f);
819 msg_Dbg(va->log, "%s is supported for output", format->name);
821 msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
826 for (unsigned j = 0; d3d_formats[j].name; j++) {
827 const d3d_format_t *format = &d3d_formats[j];
830 bool is_suported = false;
831 for (unsigned k = 0; !is_suported && k < output_count; k++) {
832 is_suported = format->format == output_list[k];
837 /* We have our solution */
838 msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
839 *input = *mode->guid;
840 *output = format->format;
841 CoTaskMemFree(output_list);
842 CoTaskMemFree(input_list);
845 CoTaskMemFree(output_list);
847 CoTaskMemFree(input_list);
852 * It creates a DXVA2 decoder using the given video format
854 static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
855 int codec_id, const video_format_t *fmt)
858 msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
859 codec_id, fmt->i_width, fmt->i_height);
861 va->width = fmt->i_width;
862 va->height = fmt->i_height;
864 /* Allocates all surfaces needed for the decoder */
865 va->surface_width = (fmt->i_width + 15) & ~15;
866 va->surface_height = (fmt->i_height + 15) & ~15;
869 va->surface_count = 16 + 1;
872 va->surface_count = 2 + 1;
875 LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
876 if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
879 va->surface_count - 1,
883 DXVA2_VideoDecoderRenderTarget,
886 msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed");
887 va->surface_count = 0;
890 for (unsigned i = 0; i < va->surface_count; i++) {
891 vlc_va_surface_t *surface = &va->surface[i];
892 surface->d3d = surface_list[i];
893 surface->refcount = 0;
896 msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
897 va->surface_count, fmt->i_width, fmt->i_height);
901 ZeroMemory(&dsc, sizeof(dsc));
902 dsc.SampleWidth = fmt->i_width;
903 dsc.SampleHeight = fmt->i_height;
904 dsc.Format = va->render;
905 if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
906 dsc.InputSampleFreq.Numerator = fmt->i_frame_rate;
907 dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
909 dsc.InputSampleFreq.Numerator = 0;
910 dsc.InputSampleFreq.Denominator = 0;
912 dsc.OutputFrameFreq = dsc.InputSampleFreq;
913 dsc.UABProtectionLevel = FALSE;
916 /* FIXME I am unsure we can let unknown everywhere */
917 DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
918 ext->SampleFormat = 0;//DXVA2_SampleUnknown;
919 ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
920 ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
921 ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
922 ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
923 ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
924 ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
926 /* List all configurations available for the decoder */
928 DXVA2_ConfigPictureDecode *cfg_list = NULL;
929 if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
935 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed");
938 msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
940 /* Select the best decoder configuration */
942 for (unsigned i = 0; i < cfg_count; i++) {
943 const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
946 msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
947 i, cfg->ConfigBitstreamRaw);
951 if (cfg->ConfigBitstreamRaw == 1)
953 else if (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
957 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA_NoEncrypt))
960 if (cfg_score < score) {
965 CoTaskMemFree(cfg_list);
966 if (cfg_score <= 0) {
967 msg_Err(va->log, "Failed to find a supported decoder configuration");
971 /* Create the decoder */
972 IDirectXVideoDecoder *decoder;
973 if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
980 msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed");
983 va->decoder = decoder;
984 msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
987 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
990 IDirectXVideoDecoder_Release(va->decoder);
993 for (unsigned i = 0; i < va->surface_count; i++)
994 IDirect3DSurface9_Release(va->surface[i].d3d);
995 va->surface_count = 0;
997 static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
999 msg_Err(va->log, "DxResetVideoDecoder unimplemented");
1000 return VLC_EGENERIC;
1003 static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
1005 switch (va->render) {
1006 case MAKEFOURCC('N','V','1','2'):
1007 case MAKEFOURCC('I','M','C','3'):
1008 va->output = MAKEFOURCC('Y','V','1','2');
1011 va->output = va->render;
1014 CopyInitCache(&va->surface_cache, va->surface_width);
1016 static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
1018 CopyCleanCache(&va->surface_cache);