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
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.
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.
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 *****************************************************************************/
31 #if defined(HAVE_LIBAVCODEC_AVCODEC_H) && defined(HAVE_AVCODEC_DXVA2)
32 # if _WIN32_WINNT < 0x600
33 /* dxva2 needs Vista support */
35 # define _WIN32_WINNT 0x600
39 #include <vlc_common.h>
40 #include <vlc_picture.h>
41 #include <vlc_fourcc.h>
45 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
46 # include <libavcodec/avcodec.h>
47 # ifdef HAVE_AVCODEC_DXVA2
48 # define DXVA2API_USE_BITFIELDS
50 # include <libavcodec/dxva2.h>
60 #ifdef HAVE_AVCODEC_DXVA2
71 # if defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 3
72 # undef IDirect3DDeviceManager9_Release
73 # define IDirect3DDeviceManager9_Release(This) (This)->lpVtbl->Release(This)
78 #define DXVA2_E_NOT_INITIALIZED MAKE_HRESULT(1, 4, 4096)
79 #define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
80 #define DXVA2_E_VIDEO_DEVICE_LOCKED MAKE_HRESULT(1, 4, 4098)
81 #define DXVA2_E_NOT_AVAILABLE MAKE_HRESULT(1, 4, 4099)
83 static const GUID DXVA2_ModeMPEG2_MoComp = {
84 0xe6a9f44b, 0x61b0,0x4563, {0x9e,0xa4,0x63,0xd2,0xa3,0xc6,0xfe,0x66}
86 static const GUID DXVA2_ModeMPEG2_IDCT = {
87 0xbf22ad00, 0x03ea,0x4690, {0x80,0x77,0x47,0x33,0x46,0x20,0x9b,0x7e}
89 static const GUID DXVA2_ModeMPEG2_VLD = {
90 0xee27417f, 0x5e28,0x4e65, {0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9}
92 static const GUID DXVA2_ModeMPEG2and1_VLD = {
93 0x86695f12, 0x340e,0x4f04, {0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60}
95 static const GUID DXVA2_ModeMPEG1_VLD = {
96 0x6f3ec719, 0x3735,0x42cc, {0x80,0x63,0x65,0xcc,0x3c,0xb3,0x66,0x16}
99 static const GUID DXVA2_ModeH264_A = {
100 0x1b81be64, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
102 static const GUID DXVA2_ModeH264_B = {
103 0x1b81be65, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
105 static const GUID DXVA2_ModeH264_C = {
106 0x1b81be66, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
108 static const GUID DXVA2_ModeH264_D = {
109 0x1b81be67, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
111 static const GUID DXVA2_ModeH264_E = {
112 0x1b81be68, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
114 static const GUID DXVA2_ModeH264_F = {
115 0x1b81be69, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
117 static const GUID DXVA_ModeH264_VLD_WithFMOASO_NoFGT = {
118 0xd5f04ff9, 0x3418,0x45d8, {0x95,0x61,0x32,0xa7,0x6a,0xae,0x2d,0xdd}
120 static const GUID DXVADDI_Intel_ModeH264_A = {
121 0x604F8E64, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
123 static const GUID DXVADDI_Intel_ModeH264_C = {
124 0x604F8E66, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
126 static const GUID DXVADDI_Intel_ModeH264_E = { // DXVA_Intel_H264_NoFGT_ClearVideo
127 0x604F8E68, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
129 static const GUID DXVA_ModeH264_VLD_NoFGT_Flash = {
130 0x4245F676, 0x2BBC,0x4166, {0xa0,0xBB,0x54,0xE7,0xB8,0x49,0xC3,0x80}
133 static const GUID DXVA2_ModeWMV8_A = {
134 0x1b81be80, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
136 static const GUID DXVA2_ModeWMV8_B = {
137 0x1b81be81, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
139 static const GUID DXVA2_ModeWMV9_A = {
140 0x1b81be90, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
142 static const GUID DXVA2_ModeWMV9_B = {
143 0x1b81be91, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
145 static const GUID DXVA2_ModeWMV9_C = {
146 0x1b81be94, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
149 static const GUID DXVA2_ModeVC1_A = {
150 0x1b81beA0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
152 static const GUID DXVA2_ModeVC1_B = {
153 0x1b81beA1, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
155 static const GUID DXVA2_ModeVC1_C = {
156 0x1b81beA2, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
158 static const GUID DXVA2_ModeVC1_D = {
159 0x1b81beA3, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
161 /* Conformity to the August 2010 update of the specification, ModeVC1_VLD2010 */
162 static const GUID DXVA2_ModeVC1_D2010 = {
163 0x1b81beA4, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
166 static const GUID DXVA_NoEncrypt = {
167 0x1b81bed0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
170 static const GUID DXVA_Intel_VC1_ClearVideo = {
171 0xBCC5DB6D, 0xA2B6,0x4AF0, {0xAC,0xE4,0xAD,0xB1,0xF7,0x87,0xBC,0x89}
173 static const GUID DXVA_Intel_VC1_ClearVideo_2 = {
174 0xE07EC519, 0xE651,0x4CD6, {0xAC,0x84,0x13,0x70,0xCC,0xEE,0xC8,0x51}
178 static const GUID DXVA_nVidia_MPEG4_ASP = {
179 0x9947EC6F, 0x689B,0x11DC, {0xA3,0x20,0x00,0x19,0xDB,0xBC,0x41,0x84}
181 static const GUID DXVA_ModeMPEG4pt2_VLD_Simple = {
182 0xefd64d74, 0xc9e8,0x41d7, {0xa5,0xe9,0xe9,0xb0,0xe3,0x9f,0xa3,0x19}
184 static const GUID DXVA_ModeMPEG4pt2_VLD_AdvSimple_NoGMC = {
185 0xed418a9f, 0x10d,0x4eda, {0x9a,0xe3,0x9a,0x65,0x35,0x8d,0x8d,0x2e}
187 static const GUID DXVA_ModeMPEG4pt2_VLD_AdvSimple_GMC = {
188 0xab998b5b, 0x4258,0x44a9, {0x9f,0xeb,0x94,0xe5,0x97,0xa6,0xba,0xae}
190 static const GUID DXVA_ModeMPEG4pt2_VLD_AdvSimple_Avivo = {
191 0x7C74ADC6, 0xe2ba,0x4ade, {0x86,0xde,0x30,0xbe,0xab,0xb4,0x0c,0xc1}
195 static const GUID DXVA_ModeH264_VLD_Multiview = {
196 0x9901CCD3, 0xca12,0x4b7e, {0x86,0x7a,0xe2,0x22,0x3d,0x92,0x55,0xc3}
205 /* XXX Prefered modes must come first */
206 static const dxva2_mode_t dxva2_modes[] = {
208 { "MPEG-2 variable-length decoder", &DXVA2_ModeMPEG2_VLD, CODEC_ID_MPEG2VIDEO },
209 { "MPEG-2 & MPEG-1 variable-length decoder", &DXVA2_ModeMPEG2and1_VLD, CODEC_ID_MPEG2VIDEO },
210 { "MPEG-2 motion compensation", &DXVA2_ModeMPEG2_MoComp, 0 },
211 { "MPEG-2 inverse discrete cosine transform", &DXVA2_ModeMPEG2_IDCT, 0 },
213 { "MPEG-1 variable-length decoder", &DXVA2_ModeMPEG1_VLD, 0 },
216 { "H.264 variable-length decoder, film grain technology", &DXVA2_ModeH264_F, CODEC_ID_H264 },
217 { "H.264 variable-length decoder, no film grain technology", &DXVA2_ModeH264_E, CODEC_ID_H264 },
218 { "H.264 variable-length decoder, no film grain technology (Intel ClearVideo)", &DXVADDI_Intel_ModeH264_E, CODEC_ID_H264 },
219 { "H.264 variable-length decoder, no film grain technology, FMO/ASO", &DXVA_ModeH264_VLD_WithFMOASO_NoFGT, CODEC_ID_H264 },
220 { "H.264 variable-length decoder, no film grain technology, Flash", &DXVA_ModeH264_VLD_NoFGT_Flash, CODEC_ID_H264 },
222 { "H.264 inverse discrete cosine transform, film grain technology", &DXVA2_ModeH264_D, 0 },
223 { "H.264 inverse discrete cosine transform, no film grain technology", &DXVA2_ModeH264_C, 0 },
224 { "H.264 inverse discrete cosine transform, no film grain technology (Intel)", &DXVADDI_Intel_ModeH264_C, 0 },
226 { "H.264 motion compensation, film grain technology", &DXVA2_ModeH264_B, 0 },
227 { "H.264 motion compensation, no film grain technology", &DXVA2_ModeH264_A, 0 },
228 { "H.264 motion compensation, no film grain technology (Intel)", &DXVADDI_Intel_ModeH264_A, 0 },
231 { "Windows Media Video 8 motion compensation", &DXVA2_ModeWMV8_B, 0 },
232 { "Windows Media Video 8 post processing", &DXVA2_ModeWMV8_A, 0 },
234 { "Windows Media Video 9 IDCT", &DXVA2_ModeWMV9_C, 0 },
235 { "Windows Media Video 9 motion compensation", &DXVA2_ModeWMV9_B, 0 },
236 { "Windows Media Video 9 post processing", &DXVA2_ModeWMV9_A, 0 },
239 { "VC-1 variable-length decoder", &DXVA2_ModeVC1_D, CODEC_ID_VC1 },
240 { "VC-1 variable-length decoder", &DXVA2_ModeVC1_D, CODEC_ID_WMV3 },
241 { "VC-1 variable-length decoder", &DXVA2_ModeVC1_D2010, CODEC_ID_VC1 },
242 { "VC-1 variable-length decoder", &DXVA2_ModeVC1_D2010, CODEC_ID_WMV3 },
243 { "VC-1 variable-length decoder 2 (Intel)", &DXVA_Intel_VC1_ClearVideo_2, 0 },
244 { "VC-1 variable-length decoder (Intel)", &DXVA_Intel_VC1_ClearVideo, 0 },
246 { "VC-1 inverse discrete cosine transform", &DXVA2_ModeVC1_C, 0 },
247 { "VC-1 motion compensation", &DXVA2_ModeVC1_B, 0 },
248 { "VC-1 post processing", &DXVA2_ModeVC1_A, 0 },
250 /* Xvid/Divx: TODO */
251 { "MPEG-4 Part 2 nVidia bitstream decoder", &DXVA_nVidia_MPEG4_ASP, 0 },
252 { "MPEG-4 Part 2 variable-length decoder, Simple Profile", &DXVA_ModeMPEG4pt2_VLD_Simple, 0 },
253 { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, no GMC", &DXVA_ModeMPEG4pt2_VLD_AdvSimple_NoGMC, 0 },
254 { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, GMC", &DXVA_ModeMPEG4pt2_VLD_AdvSimple_GMC, 0 },
255 { "MPEG-4 Part 2 variable-length decoder, Simple&Advanced Profile, Avivo", &DXVA_ModeMPEG4pt2_VLD_AdvSimple_Avivo, 0 },
260 static const dxva2_mode_t *Dxva2FindMode(const GUID *guid)
262 for (unsigned i = 0; dxva2_modes[i].name; i++) {
263 if (IsEqualGUID(dxva2_modes[i].guid, guid))
264 return &dxva2_modes[i];
275 /* XXX Prefered format must come first */
276 static const d3d_format_t d3d_formats[] = {
277 { "YV12", MAKEFOURCC('Y','V','1','2'), VLC_CODEC_YV12 },
278 { "NV12", MAKEFOURCC('N','V','1','2'), VLC_CODEC_NV12 },
283 static const d3d_format_t *D3dFindFormat(D3DFORMAT format)
285 for (unsigned i = 0; d3d_formats[i].name; i++) {
286 if (d3d_formats[i].format == format)
287 return &d3d_formats[i];
292 static const GUID IID_IDirectXVideoDecoderService = {
293 0xfc51a551, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
295 static const GUID IID_IDirectXVideoAccelerationService = {
296 0xfc51a550, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
301 LPDIRECT3DSURFACE9 d3d;
306 #define VA_DXVA2_MAX_SURFACE_COUNT (64)
320 HINSTANCE hdxva2_dll;
323 D3DPRESENT_PARAMETERS d3dpp;
325 D3DADAPTER_IDENTIFIER9 d3dai;
326 LPDIRECT3DDEVICE9 d3ddev;
330 IDirect3DDeviceManager9 *devmng;
334 IDirectXVideoDecoderService *vs;
339 DXVA2_ConfigPictureDecode cfg;
340 IDirectXVideoDecoder *decoder;
342 /* Option conversion */
344 copy_cache_t surface_cache;
347 struct dxva_context hw;
350 unsigned surface_count;
351 unsigned surface_order;
354 vlc_fourcc_t surface_chroma;
356 vlc_va_surface_t surface[VA_DXVA2_MAX_SURFACE_COUNT];
357 LPDIRECT3DSURFACE9 hw_surface[VA_DXVA2_MAX_SURFACE_COUNT];
361 static vlc_va_dxva2_t *vlc_va_dxva2_Get(void *external)
363 assert(external == (void*)(&((vlc_va_dxva2_t*)external)->va));
368 static int D3dCreateDevice(vlc_va_dxva2_t *);
369 static void D3dDestroyDevice(vlc_va_dxva2_t *);
370 static char *DxDescribe(vlc_va_dxva2_t *);
372 static int D3dCreateDeviceManager(vlc_va_dxva2_t *);
373 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *);
375 static int DxCreateVideoService(vlc_va_dxva2_t *);
376 static void DxDestroyVideoService(vlc_va_dxva2_t *);
377 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *, GUID *input, D3DFORMAT *output);
379 static int DxCreateVideoDecoder(vlc_va_dxva2_t *,
380 int codec_id, const video_format_t *);
381 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *);
382 static int DxResetVideoDecoder(vlc_va_dxva2_t *);
384 static void DxCreateVideoConversion(vlc_va_dxva2_t *);
385 static void DxDestroyVideoConversion(vlc_va_dxva2_t *);
388 static int Setup(vlc_va_t *external, void **hw, vlc_fourcc_t *chroma,
389 int width, int height)
391 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
393 if (va->width == width && va->height == height && va->decoder)
397 DxDestroyVideoConversion(va);
398 DxDestroyVideoDecoder(va);
402 if (width <= 0 || height <= 0)
405 /* FIXME transmit a video_format_t by VaSetup directly */
407 memset(&fmt, 0, sizeof(fmt));
409 fmt.i_height = height;
411 if (DxCreateVideoDecoder(va, va->codec_id, &fmt))
414 va->hw.decoder = va->decoder;
415 va->hw.cfg = &va->cfg;
416 va->hw.surface_count = va->surface_count;
417 va->hw.surface = va->hw_surface;
418 for (unsigned i = 0; i < va->surface_count; i++)
419 va->hw.surface[i] = va->surface[i].d3d;
422 DxCreateVideoConversion(va);
427 const d3d_format_t *output = D3dFindFormat(va->output);
428 *chroma = output->codec;
433 static int Extract(vlc_va_t *external, picture_t *picture, AVFrame *ff)
435 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
436 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
438 if (!va->surface_cache.buffer)
442 assert(va->output == MAKEFOURCC('Y','V','1','2'));
446 if (FAILED(IDirect3DSurface9_LockRect(d3d, &lock, NULL, D3DLOCK_READONLY))) {
447 msg_Err(va->log, "Failed to lock surface");
451 if (va->render == MAKEFOURCC('Y','V','1','2')) {
452 uint8_t *plane[3] = {
454 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height,
455 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
456 + (lock.Pitch/2) * (va->surface_height/2)
463 CopyFromYv12(picture, plane, pitch,
464 va->width, va->height,
467 assert(va->render == MAKEFOURCC('N','V','1','2'));
468 uint8_t *plane[2] = {
470 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
476 CopyFromNv12(picture, plane, pitch,
477 va->width, va->height,
482 IDirect3DSurface9_UnlockRect(d3d);
485 /* FIXME it is nearly common with VAAPI */
486 static int Get(vlc_va_t *external, AVFrame *ff)
488 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
490 /* Check the device */
491 HRESULT hr = IDirect3DDeviceManager9_TestDevice(va->devmng, va->device);
492 if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
493 if (DxResetVideoDecoder(va))
495 } else if (FAILED(hr)) {
496 msg_Err(va->log, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
500 /* Grab an unused surface, in case none are, try the oldest
501 * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
503 for (i = 0, old = 0; i < va->surface_count; i++) {
504 vlc_va_surface_t *surface = &va->surface[i];
506 if (!surface->refcount)
509 if (surface->order < va->surface[old].order)
512 if (i >= va->surface_count)
515 vlc_va_surface_t *surface = &va->surface[i];
517 surface->refcount = 1;
518 surface->order = va->surface_order++;
521 for (int i = 0; i < 4; i++) {
525 if (i == 0 || i == 3)
526 ff->data[i] = (void*)surface->d3d;/* Yummie */
530 static void Release(vlc_va_t *external, AVFrame *ff)
532 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
533 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
535 for (unsigned i = 0; i < va->surface_count; i++) {
536 vlc_va_surface_t *surface = &va->surface[i];
538 if (surface->d3d == d3d)
542 static void Close(vlc_va_t *external)
544 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
546 DxDestroyVideoConversion(va);
547 DxDestroyVideoDecoder(va);
548 DxDestroyVideoService(va);
549 D3dDestroyDeviceManager(va);
550 D3dDestroyDevice(va);
553 FreeLibrary(va->hdxva2_dll);
555 FreeLibrary(va->hd3d9_dll);
557 free(va->va.description);
561 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
563 vlc_va_dxva2_t *va = calloc(1, sizeof(*va));
569 va->codec_id = codec_id;
572 va->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
573 if (!va->hd3d9_dll) {
574 msg_Warn(va->log, "cannot load d3d9.dll");
577 va->hdxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
578 if (!va->hdxva2_dll) {
579 msg_Warn(va->log, "cannot load dxva2.dll");
582 msg_Dbg(va->log, "DLLs loaded");
585 if (D3dCreateDevice(va)) {
586 msg_Err(va->log, "Failed to create Direct3D device");
589 msg_Dbg(va->log, "D3dCreateDevice succeed");
591 if (D3dCreateDeviceManager(va)) {
592 msg_Err(va->log, "D3dCreateDeviceManager failed");
596 if (DxCreateVideoService(va)) {
597 msg_Err(va->log, "DxCreateVideoService failed");
602 if (DxFindVideoServiceConversion(va, &va->input, &va->render)) {
603 msg_Err(va->log, "DxFindVideoServiceConversion failed");
607 /* TODO print the hardware name/vendor for debugging purposes */
608 va->va.description = DxDescribe(va);
609 va->va.setup = Setup;
611 va->va.release = Release;
612 va->va.extract = Extract;
613 va->va.close = Close;
623 * It creates a Direct3D device usable for DXVA 2
625 static int D3dCreateDevice(vlc_va_dxva2_t *va)
628 LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
629 Create9 = (void *)GetProcAddress(va->hd3d9_dll,
630 TEXT("Direct3DCreate9"));
632 msg_Err(va->log, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
638 d3dobj = Create9(D3D_SDK_VERSION);
640 msg_Err(va->log, "Direct3DCreate9 failed");
646 D3DADAPTER_IDENTIFIER9 *d3dai = &va->d3dai;
647 if (FAILED(IDirect3D9_GetAdapterIdentifier(va->d3dobj,
648 D3DADAPTER_DEFAULT, 0, d3dai))) {
649 msg_Warn(va->log, "IDirect3D9_GetAdapterIdentifier failed");
650 ZeroMemory(d3dai, sizeof(*d3dai));
654 D3DPRESENT_PARAMETERS *d3dpp = &va->d3dpp;
655 ZeroMemory(d3dpp, sizeof(*d3dpp));
656 d3dpp->Flags = D3DPRESENTFLAG_VIDEO;
657 d3dpp->Windowed = TRUE;
658 d3dpp->hDeviceWindow = NULL;
659 d3dpp->SwapEffect = D3DSWAPEFFECT_DISCARD;
660 d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
661 d3dpp->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
662 d3dpp->BackBufferCount = 0; /* FIXME what to put here */
663 d3dpp->BackBufferFormat = D3DFMT_X8R8G8B8; /* FIXME what to put here */
664 d3dpp->BackBufferWidth = 0;
665 d3dpp->BackBufferHeight = 0;
666 d3dpp->EnableAutoDepthStencil = FALSE;
668 /* Direct3D needs a HWND to create a device, even without using ::Present
669 this HWND is used to alert Direct3D when there's a change of focus window.
670 For now, use GetShellWindow, as it looks harmless */
671 LPDIRECT3DDEVICE9 d3ddev;
672 if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
673 D3DDEVTYPE_HAL, GetShellWindow(),
674 D3DCREATE_SOFTWARE_VERTEXPROCESSING |
675 D3DCREATE_MULTITHREADED,
677 msg_Err(va->log, "IDirect3D9_CreateDevice failed");
685 * It releases a Direct3D device and its resources.
687 static void D3dDestroyDevice(vlc_va_dxva2_t *va)
690 IDirect3DDevice9_Release(va->d3ddev);
692 IDirect3D9_Release(va->d3dobj);
695 * It describes our Direct3D object
697 static char *DxDescribe(vlc_va_dxva2_t *va)
699 static const struct {
704 { 0x10DE, "NVIDIA" },
706 { 0x5333, "S3 Graphics" },
709 D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
711 const char *vendor = "Unknown";
712 for (int i = 0; vendors[i].id != 0; i++) {
713 if (vendors[i].id == id->VendorId) {
714 vendor = vendors[i].name;
720 if (asprintf(&description, "DXVA2 (%.*s, vendor %lu(%s), device %lu, revision %lu)",
721 sizeof(id->Description), id->Description,
722 id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
728 * It creates a Direct3D device manager
730 static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
732 HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
733 IDirect3DDeviceManager9 **);
734 CreateDeviceManager9 =
735 (void *)GetProcAddress(va->hdxva2_dll,
736 TEXT("DXVA2CreateDirect3DDeviceManager9"));
738 if (!CreateDeviceManager9) {
739 msg_Err(va->log, "cannot load function");
742 msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
745 IDirect3DDeviceManager9 *devmng;
746 if (FAILED(CreateDeviceManager9(&token, &devmng))) {
747 msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
752 msg_Info(va->log, "obtained IDirect3DDeviceManager9");
754 HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
756 msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
762 * It destroys a Direct3D device manager
764 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
767 IDirect3DDeviceManager9_Release(va->devmng);
771 * It creates a DirectX video service
773 static int DxCreateVideoService(vlc_va_dxva2_t *va)
775 HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
779 (void *)GetProcAddress(va->hdxva2_dll,
780 TEXT("DXVA2CreateVideoService"));
782 if (!CreateVideoService) {
783 msg_Err(va->log, "cannot load function");
786 msg_Info(va->log, "DXVA2CreateVideoService Success!");
791 hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
793 msg_Err(va->log, "OpenDeviceHandle failed");
798 IDirectXVideoDecoderService *vs;
799 hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
800 &IID_IDirectXVideoDecoderService,
803 msg_Err(va->log, "GetVideoService failed");
811 * It destroys a DirectX video service
813 static void DxDestroyVideoService(vlc_va_dxva2_t *va)
816 IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
818 IDirectXVideoDecoderService_Release(va->vs);
821 * Find the best suited decoder mode GUID and render format.
823 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
825 /* Retreive supported modes from the decoder service */
826 UINT input_count = 0;
827 GUID *input_list = NULL;
828 if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
831 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
834 for (unsigned i = 0; i < input_count; i++) {
835 const GUID *g = &input_list[i];
836 const dxva2_mode_t *mode = Dxva2FindMode(g);
838 msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
840 msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX",
841 (unsigned)g->Data1, g->Data2, g->Data3);
845 /* Try all supported mode by our priority */
846 for (unsigned i = 0; dxva2_modes[i].name; i++) {
847 const dxva2_mode_t *mode = &dxva2_modes[i];
848 if (!mode->codec || mode->codec != va->codec_id)
852 bool is_suported = false;
853 for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
854 is_suported = IsEqualGUID(mode->guid, g);
860 msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
861 UINT output_count = 0;
862 D3DFORMAT *output_list = NULL;
863 if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
866 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
869 for (unsigned j = 0; j < output_count; j++) {
870 const D3DFORMAT f = output_list[j];
871 const d3d_format_t *format = D3dFindFormat(f);
873 msg_Dbg(va->log, "%s is supported for output", format->name);
875 msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
880 for (unsigned j = 0; d3d_formats[j].name; j++) {
881 const d3d_format_t *format = &d3d_formats[j];
884 bool is_suported = false;
885 for (unsigned k = 0; !is_suported && k < output_count; k++) {
886 is_suported = format->format == output_list[k];
891 /* We have our solution */
892 msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
893 *input = *mode->guid;
894 *output = format->format;
895 CoTaskMemFree(output_list);
896 CoTaskMemFree(input_list);
899 CoTaskMemFree(output_list);
901 CoTaskMemFree(input_list);
906 * It creates a DXVA2 decoder using the given video format
908 static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
909 int codec_id, const video_format_t *fmt)
912 msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
913 codec_id, fmt->i_width, fmt->i_height);
915 va->width = fmt->i_width;
916 va->height = fmt->i_height;
918 /* Allocates all surfaces needed for the decoder */
919 va->surface_width = (fmt->i_width + 15) & ~15;
920 va->surface_height = (fmt->i_height + 15) & ~15;
923 va->surface_count = 16 + 1;
926 va->surface_count = 2 + 1;
929 LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
930 if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
933 va->surface_count - 1,
937 DXVA2_VideoDecoderRenderTarget,
940 msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed");
941 va->surface_count = 0;
944 for (unsigned i = 0; i < va->surface_count; i++) {
945 vlc_va_surface_t *surface = &va->surface[i];
946 surface->d3d = surface_list[i];
947 surface->refcount = 0;
950 msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
951 va->surface_count, fmt->i_width, fmt->i_height);
955 ZeroMemory(&dsc, sizeof(dsc));
956 dsc.SampleWidth = fmt->i_width;
957 dsc.SampleHeight = fmt->i_height;
958 dsc.Format = va->render;
959 if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
960 dsc.InputSampleFreq.Numerator = fmt->i_frame_rate;
961 dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
963 dsc.InputSampleFreq.Numerator = 0;
964 dsc.InputSampleFreq.Denominator = 0;
966 dsc.OutputFrameFreq = dsc.InputSampleFreq;
967 dsc.UABProtectionLevel = FALSE;
970 /* FIXME I am unsure we can let unknown everywhere */
971 DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
972 ext->SampleFormat = 0;//DXVA2_SampleUnknown;
973 ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
974 ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
975 ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
976 ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
977 ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
978 ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
980 /* List all configurations available for the decoder */
982 DXVA2_ConfigPictureDecode *cfg_list = NULL;
983 if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
989 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed");
992 msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
994 /* Select the best decoder configuration */
996 for (unsigned i = 0; i < cfg_count; i++) {
997 const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
1000 msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
1001 i, cfg->ConfigBitstreamRaw);
1005 if (cfg->ConfigBitstreamRaw == 1)
1007 else if (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
1011 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA_NoEncrypt))
1014 if (cfg_score < score) {
1019 CoTaskMemFree(cfg_list);
1020 if (cfg_score <= 0) {
1021 msg_Err(va->log, "Failed to find a supported decoder configuration");
1022 return VLC_EGENERIC;
1025 /* Create the decoder */
1026 IDirectXVideoDecoder *decoder;
1027 if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
1034 msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed");
1035 return VLC_EGENERIC;
1037 va->decoder = decoder;
1038 msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
1041 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
1044 IDirectXVideoDecoder_Release(va->decoder);
1047 for (unsigned i = 0; i < va->surface_count; i++)
1048 IDirect3DSurface9_Release(va->surface[i].d3d);
1049 va->surface_count = 0;
1051 static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
1053 msg_Err(va->log, "DxResetVideoDecoder unimplemented");
1054 return VLC_EGENERIC;
1057 static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
1059 switch (va->render) {
1060 case MAKEFOURCC('N','V','1','2'):
1061 va->output = MAKEFOURCC('Y','V','1','2');
1064 va->output = va->render;
1067 CopyInitCache(&va->surface_cache, va->surface_width);
1069 static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
1071 CopyCleanCache(&va->surface_cache);