1 /*****************************************************************************
2 * va.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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_picture.h>
32 #include <vlc_fourcc.h>
36 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
37 # include <libavcodec/avcodec.h>
38 # ifdef HAVE_AVCODEC_DXVA2
39 # define DXVA2API_USE_BITFIELDS
40 # include <libavcodec/dxva2.h>
42 #elif defined(HAVE_FFMPEG_AVCODEC_H)
43 # include <ffmpeg/avcodec.h>
52 #ifdef HAVE_AVCODEC_DXVA2
62 #define DXVA2_E_NOT_INITIALIZED MAKE_HRESULT(1, 4, 4096)
63 #define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
64 #define DXVA2_E_VIDEO_DEVICE_LOCKED MAKE_HRESULT(1, 4, 4098)
65 #define DXVA2_E_NOT_AVAILABLE MAKE_HRESULT(1, 4, 4099)
67 static const GUID DXVA2_ModeMPEG2_MoComp = {
68 0xe6a9f44b, 0x61b0,0x4563, {0x9e,0xa4,0x63,0xd2,0xa3,0xc6,0xfe,0x66}
70 static const GUID DXVA2_ModeMPEG2_IDCT = {
71 0xbf22ad00, 0x03ea,0x4690, {0x80,0x77,0x47,0x33,0x46,0x20,0x9b,0x7e}
73 static const GUID DXVA2_ModeMPEG2_VLD = {
74 0xee27417f, 0x5e28,0x4e65, {0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9}
77 static const GUID DXVA2_ModeH264_A = {
78 0x1b81be64, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
80 static const GUID DXVA2_ModeH264_B = {
81 0x1b81be65, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
83 static const GUID DXVA2_ModeH264_C = {
84 0x1b81be66, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
86 static const GUID DXVA2_ModeH264_D = {
87 0x1b81be67, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
89 static const GUID DXVA2_ModeH264_E = {
90 0x1b81be68, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
92 static const GUID DXVA2_ModeH264_F = {
93 0x1b81be69, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
95 static const GUID DXVADDI_Intel_ModeH264_A = {
96 0x604F8E64, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
98 static const GUID DXVADDI_Intel_ModeH264_C = {
99 0x604F8E66, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
101 static const GUID DXVADDI_Intel_ModeH264_E = {
102 0x604F8E68, 0x4951,0x4c54, {0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6}
104 static const GUID DXVA2_ModeWMV8_A = {
105 0x1b81be80, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
107 static const GUID DXVA2_ModeWMV8_B = {
108 0x1b81be81, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
110 static const GUID DXVA2_ModeWMV9_A = {
111 0x1b81be90, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
113 static const GUID DXVA2_ModeWMV9_B = {
114 0x1b81be91, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
116 static const GUID DXVA2_ModeWMV9_C = {
117 0x1b81be94, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
120 static const GUID DXVA2_ModeVC1_A = {
121 0x1b81beA0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
123 static const GUID DXVA2_ModeVC1_B = {
124 0x1b81beA1, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
126 static const GUID DXVA2_ModeVC1_C = {
127 0x1b81beA2, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
129 static const GUID DXVA2_ModeVC1_D = {
130 0x1b81beA3, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
133 static const GUID DXVA_NoEncrypt = {
134 0x1b81bed0, 0xa0c7,0x11d3, {0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5}
143 /* XXX Prefered modes must come first */
144 static const dxva2_mode_t dxva2_modes[] = {
145 { "DXVA2_ModeMPEG2_VLD", &DXVA2_ModeMPEG2_VLD, CODEC_ID_MPEG2VIDEO },
146 { "DXVA2_ModeMPEG2_MoComp", &DXVA2_ModeMPEG2_MoComp, 0 },
147 { "DXVA2_ModeMPEG2_IDCT", &DXVA2_ModeMPEG2_IDCT, 0 },
149 { "H.264 variable-length decoder (VLD), FGT", &DXVA2_ModeH264_F, CODEC_ID_H264 },
150 { "H.264 VLD, no FGT", &DXVA2_ModeH264_E, CODEC_ID_H264 },
151 { "H.264 VLD, no FGT (Intel)", &DXVADDI_Intel_ModeH264_E, CODEC_ID_H264 },
152 { "H.264 IDCT, FGT", &DXVA2_ModeH264_D, 0 },
153 { "H.264 inverse discrete cosine transform (IDCT), no FGT", &DXVA2_ModeH264_C, 0 },
154 { "H.264 inverse discrete cosine transform (IDCT), no FGT (Intel)", &DXVADDI_Intel_ModeH264_C, 0 },
155 { "H.264 MoComp, FGT", &DXVA2_ModeH264_B, 0 },
156 { "H.264 motion compensation (MoComp), no FGT", &DXVA2_ModeH264_A, 0 },
157 { "H.264 motion compensation (MoComp), no FGT (Intel)", &DXVADDI_Intel_ModeH264_A, 0 },
159 { "Windows Media Video 8 MoComp", &DXVA2_ModeWMV8_B, 0 },
160 { "Windows Media Video 8 post processing", &DXVA2_ModeWMV8_A, 0 },
162 { "Windows Media Video 9 IDCT", &DXVA2_ModeWMV9_C, 0 },
163 { "Windows Media Video 9 MoComp", &DXVA2_ModeWMV9_B, 0 },
164 { "Windows Media Video 9 post processing", &DXVA2_ModeWMV9_A, 0 },
166 { "VC-1 VLD", &DXVA2_ModeVC1_D, CODEC_ID_VC1 },
167 { "VC-1 VLD", &DXVA2_ModeVC1_D, CODEC_ID_WMV3 },
168 { "VC-1 IDCT", &DXVA2_ModeVC1_C, 0 },
169 { "VC-1 MoComp", &DXVA2_ModeVC1_B, 0 },
170 { "VC-1 post processing", &DXVA2_ModeVC1_A, 0 },
175 static const dxva2_mode_t *Dxva2FindMode(const GUID *guid)
177 for (unsigned i = 0; dxva2_modes[i].name; i++) {
178 if (IsEqualGUID(dxva2_modes[i].guid, guid))
179 return &dxva2_modes[i];
190 /* XXX Prefered format must come first */
191 static const d3d_format_t d3d_formats[] = {
192 { "YV12", MAKEFOURCC('Y','V','1','2'), VLC_CODEC_YV12 },
193 { "NV12", MAKEFOURCC('N','V','1','2'), VLC_CODEC_NV12 },
198 static const d3d_format_t *D3dFindFormat(D3DFORMAT format)
200 for (unsigned i = 0; d3d_formats[i].name; i++) {
201 if (d3d_formats[i].format == format)
202 return &d3d_formats[i];
207 static const GUID IID_IDirectXVideoDecoderService = {
208 0xfc51a551, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
210 static const GUID IID_IDirectXVideoAccelerationService = {
211 0xfc51a550, 0xd5e7, 0x11d9, {0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02}
216 LPDIRECT3DSURFACE9 d3d;
221 #define VA_DXVA2_MAX_SURFACE_COUNT (64)
235 HINSTANCE hdxva2_dll;
238 D3DPRESENT_PARAMETERS d3dpp;
240 D3DADAPTER_IDENTIFIER9 d3dai;
241 LPDIRECT3DDEVICE9 d3ddev;
245 IDirect3DDeviceManager9 *devmng;
249 IDirectXVideoDecoderService *vs;
254 DXVA2_ConfigPictureDecode cfg;
255 IDirectXVideoDecoder *decoder;
257 /* Option conversion */
259 copy_cache_t surface_cache;
262 struct dxva_context hw;
265 unsigned surface_count;
266 unsigned surface_order;
269 vlc_fourcc_t surface_chroma;
271 vlc_va_surface_t surface[VA_DXVA2_MAX_SURFACE_COUNT];
272 LPDIRECT3DSURFACE9 hw_surface[VA_DXVA2_MAX_SURFACE_COUNT];
276 static vlc_va_dxva2_t *vlc_va_dxva2_Get(void *external)
278 assert(external == (void*)(&((vlc_va_dxva2_t*)external)->va));
283 static int D3dCreateDevice(vlc_va_dxva2_t *);
284 static void D3dDestroyDevice(vlc_va_dxva2_t *);
285 static char *DxDescribe(vlc_va_dxva2_t *);
287 static int D3dCreateDeviceManager(vlc_va_dxva2_t *);
288 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *);
290 static int DxCreateVideoService(vlc_va_dxva2_t *);
291 static void DxDestroyVideoService(vlc_va_dxva2_t *);
292 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *, GUID *input, D3DFORMAT *output);
294 static int DxCreateVideoDecoder(vlc_va_dxva2_t *,
295 int codec_id, const video_format_t *);
296 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *);
297 static int DxResetVideoDecoder(vlc_va_dxva2_t *);
299 static void DxCreateVideoConversion(vlc_va_dxva2_t *);
300 static void DxDestroyVideoConversion(vlc_va_dxva2_t *);
303 static int Setup(vlc_va_t *external, void **hw, vlc_fourcc_t *chroma,
304 int width, int height)
306 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
308 if (va->width == width && va->height == height && va->decoder)
312 DxDestroyVideoConversion(va);
313 DxDestroyVideoDecoder(va);
317 if (width <= 0 || height <= 0)
320 /* FIXME transmit a video_format_t by VaSetup directly */
322 memset(&fmt, 0, sizeof(fmt));
324 fmt.i_height = height;
326 if (DxCreateVideoDecoder(va, va->codec_id, &fmt))
329 va->hw.decoder = va->decoder;
330 va->hw.cfg = &va->cfg;
331 va->hw.surface_count = va->surface_count;
332 va->hw.surface = va->hw_surface;
333 for (unsigned i = 0; i < va->surface_count; i++)
334 va->hw.surface[i] = va->surface[i].d3d;
337 DxCreateVideoConversion(va);
342 const d3d_format_t *output = D3dFindFormat(va->output);
343 *chroma = output->codec;
348 static int Extract(vlc_va_t *external, picture_t *picture, AVFrame *ff)
350 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
351 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
353 if (!va->surface_cache.buffer)
357 assert(va->output == MAKEFOURCC('Y','V','1','2'));
361 if (FAILED(IDirect3DSurface9_LockRect(d3d, &lock, NULL, D3DLOCK_READONLY))) {
362 msg_Err(va->log, "Failed to lock surface");
366 if (va->render == MAKEFOURCC('Y','V','1','2')) {
367 uint8_t *plane[3] = {
369 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height,
370 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
371 + (lock.Pitch/2) * (va->surface_height/2)
378 CopyFromYv12(picture, plane, pitch,
379 va->width, va->height,
382 assert(va->render == MAKEFOURCC('N','V','1','2'));
383 uint8_t *plane[2] = {
385 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
391 CopyFromNv12(picture, plane, pitch,
392 va->width, va->height,
397 IDirect3DSurface9_UnlockRect(d3d);
400 /* FIXME it is nearly common with VAAPI */
401 static int Get(vlc_va_t *external, AVFrame *ff)
403 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
405 /* Check the device */
406 HRESULT hr = IDirect3DDeviceManager9_TestDevice(va->devmng, va->device);
407 if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
408 if (DxResetVideoDecoder(va))
410 } else if (FAILED(hr)) {
411 msg_Err(va->log, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
415 /* Grab an unused surface, in case none are, try the oldest
416 * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
418 for (i = 0, old = 0; i < va->surface_count; i++) {
419 vlc_va_surface_t *surface = &va->surface[i];
421 if (!surface->refcount)
424 if (surface->order < va->surface[old].order)
427 if (i >= va->surface_count)
430 vlc_va_surface_t *surface = &va->surface[i];
432 surface->refcount = 1;
433 surface->order = va->surface_order++;
436 for (int i = 0; i < 4; i++) {
440 if (i == 0 || i == 3)
441 ff->data[i] = (void*)surface->d3d;/* Yummie */
445 static void Release(vlc_va_t *external, AVFrame *ff)
447 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
448 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
450 for (unsigned i = 0; i < va->surface_count; i++) {
451 vlc_va_surface_t *surface = &va->surface[i];
453 if (surface->d3d == d3d)
457 static void Close(vlc_va_t *external)
459 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
461 DxDestroyVideoConversion(va);
462 DxDestroyVideoDecoder(va);
463 DxDestroyVideoService(va);
464 D3dDestroyDeviceManager(va);
465 D3dDestroyDevice(va);
468 FreeLibrary(va->hdxva2_dll);
470 FreeLibrary(va->hd3d9_dll);
472 free(va->va.description);
476 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
478 vlc_va_dxva2_t *va = calloc(1, sizeof(*va));
484 va->codec_id = codec_id;
487 va->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
488 if (!va->hd3d9_dll) {
489 msg_Warn(va->log, "cannot load d3d9.dll");
492 va->hdxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
493 if (!va->hdxva2_dll) {
494 msg_Warn(va->log, "cannot load dxva2.dll");
497 msg_Dbg(va->log, "DLLs loaded");
500 if (D3dCreateDevice(va)) {
501 msg_Err(va->log, "Failed to create Direct3D device");
504 msg_Dbg(va->log, "D3dCreateDevice succeed");
506 if (D3dCreateDeviceManager(va)) {
507 msg_Err(va->log, "D3dCreateDeviceManager failed");
511 if (DxCreateVideoService(va)) {
512 msg_Err(va->log, "DxCreateVideoService failed");
517 if (DxFindVideoServiceConversion(va, &va->input, &va->render)) {
518 msg_Err(va->log, "DxFindVideoServiceConversion failed");
522 /* TODO print the hardware name/vendor for debugging purposes */
523 va->va.description = DxDescribe(va);
524 va->va.setup = Setup;
526 va->va.release = Release;
527 va->va.extract = Extract;
528 va->va.close = Close;
538 * It creates a Direct3D device usable for DXVA 2
540 static int D3dCreateDevice(vlc_va_dxva2_t *va)
543 LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
544 Create9 = (void *)GetProcAddress(va->hd3d9_dll,
545 TEXT("Direct3DCreate9"));
547 msg_Err(va->log, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
553 d3dobj = Create9(D3D_SDK_VERSION);
555 msg_Err(va->log, "Direct3DCreate9 failed");
561 D3DADAPTER_IDENTIFIER9 *d3dai = &va->d3dai;
562 if (FAILED(IDirect3D9_GetAdapterIdentifier(va->d3dobj,
563 D3DADAPTER_DEFAULT, 0, d3dai))) {
564 msg_Warn(va->log, "IDirect3D9_GetAdapterIdentifier failed");
565 ZeroMemory(d3dai, sizeof(*d3dai));
569 D3DPRESENT_PARAMETERS *d3dpp = &va->d3dpp;
570 ZeroMemory(d3dpp, sizeof(*d3dpp));
571 d3dpp->Flags = D3DPRESENTFLAG_VIDEO;
572 d3dpp->Windowed = TRUE;
573 d3dpp->hDeviceWindow = NULL;
574 d3dpp->SwapEffect = D3DSWAPEFFECT_DISCARD;
575 d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
576 d3dpp->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
577 d3dpp->BackBufferCount = 0; /* FIXME what to put here */
578 d3dpp->BackBufferFormat = D3DFMT_X8R8G8B8; /* FIXME what to put here */
579 d3dpp->BackBufferWidth = 0;
580 d3dpp->BackBufferHeight = 0;
581 d3dpp->EnableAutoDepthStencil = FALSE;
583 /* Direct3D needs a HWND to create a device, even without using ::Present
584 this HWND is used to alert Direct3D when there's a change of focus window.
585 For now, use GetShellWindow, as it looks harmless */
586 LPDIRECT3DDEVICE9 d3ddev;
587 if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
588 D3DDEVTYPE_HAL, GetShellWindow(),
589 D3DCREATE_SOFTWARE_VERTEXPROCESSING |
590 D3DCREATE_MULTITHREADED,
592 msg_Err(va->log, "IDirect3D9_CreateDevice failed");
600 * It releases a Direct3D device and its resources.
602 static void D3dDestroyDevice(vlc_va_dxva2_t *va)
605 IDirect3DDevice9_Release(va->d3ddev);
607 IDirect3D9_Release(va->d3dobj);
610 * It describes our Direct3D object
612 static char *DxDescribe(vlc_va_dxva2_t *va)
614 static const struct {
619 { 0x10DE, "NVIDIA" },
621 { 0x5333, "S3 Graphics" },
624 D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
626 const char *vendor = "Unknown";
627 for (int i = 0; vendors[i].id != 0; i++) {
628 if (vendors[i].id == id->VendorId) {
629 vendor = vendors[i].name;
635 if (asprintf(&description, "DXVA2 (%.*s, vendor %d(%s), device %d, revision %d)",
636 sizeof(id->Description), id->Description,
637 id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
643 * It creates a Direct3D device manager
645 static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
647 HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
648 IDirect3DDeviceManager9 **);
649 CreateDeviceManager9 =
650 (void *)GetProcAddress(va->hdxva2_dll,
651 TEXT("DXVA2CreateDirect3DDeviceManager9"));
653 if (!CreateDeviceManager9) {
654 msg_Err(va->log, "cannot load function\n");
657 msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
660 IDirect3DDeviceManager9 *devmng;
661 if (FAILED(CreateDeviceManager9(&token, &devmng))) {
662 msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
667 msg_Info(va->log, "obtained IDirect3DDeviceManager9");
669 HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
671 msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
677 * It destroys a Direct3D device manager
679 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
682 IDirect3DDeviceManager9_Release(va->devmng);
686 * It creates a DirectX video service
688 static int DxCreateVideoService(vlc_va_dxva2_t *va)
690 HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
694 (void *)GetProcAddress(va->hdxva2_dll,
695 TEXT("DXVA2CreateVideoService"));
697 if (!CreateVideoService) {
698 msg_Err(va->log, "cannot load function\n");
701 msg_Info(va->log, "DXVA2CreateVideoService Success!");
706 hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
708 msg_Err(va->log, "OpenDeviceHandle failed");
713 IDirectXVideoDecoderService *vs;
714 hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
715 &IID_IDirectXVideoDecoderService,
718 msg_Err(va->log, "GetVideoService failed");
726 * It destroys a DirectX video service
728 static void DxDestroyVideoService(vlc_va_dxva2_t *va)
731 IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
733 IDirectXVideoDecoderService_Release(va->vs);
736 * Find the best suited decoder mode GUID and render format.
738 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
740 /* Retreive supported modes from the decoder service */
741 UINT input_count = 0;
742 GUID *input_list = NULL;
743 if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
746 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
749 for (unsigned i = 0; i < input_count; i++) {
750 const GUID *g = &input_list[i];
751 const dxva2_mode_t *mode = Dxva2FindMode(g);
753 msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
755 msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX",
756 (unsigned)g->Data1, g->Data2, g->Data3);
760 /* Try all supported mode by our priority */
761 for (unsigned i = 0; dxva2_modes[i].name; i++) {
762 const dxva2_mode_t *mode = &dxva2_modes[i];
763 if (!mode->codec || mode->codec != va->codec_id)
767 bool is_suported = false;
768 for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
769 is_suported = IsEqualGUID(mode->guid, g);
775 msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
776 UINT output_count = 0;
777 D3DFORMAT *output_list = NULL;
778 if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
781 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
784 for (unsigned j = 0; j < output_count; j++) {
785 const D3DFORMAT f = output_list[j];
786 const d3d_format_t *format = D3dFindFormat(f);
788 msg_Dbg(va->log, "%s is supported for output", format->name);
790 msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
795 for (unsigned j = 0; d3d_formats[j].name; j++) {
796 const d3d_format_t *format = &d3d_formats[j];
799 bool is_suported = false;
800 for (unsigned k = 0; !is_suported && k < output_count; k++) {
801 is_suported = format->format == output_list[k];
806 /* We have our solution */
807 msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
808 *input = *mode->guid;
809 *output = format->format;
810 CoTaskMemFree(output_list);
811 CoTaskMemFree(input_list);
814 CoTaskMemFree(output_list);
816 CoTaskMemFree(input_list);
821 * It creates a DXVA2 decoder using the given video format
823 static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
824 int codec_id, const video_format_t *fmt)
827 msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
828 codec_id, fmt->i_width, fmt->i_height);
830 va->width = fmt->i_width;
831 va->height = fmt->i_height;
833 /* Allocates all surfaces needed for the decoder */
834 va->surface_width = (fmt->i_width + 15) & ~15;
835 va->surface_height = (fmt->i_height + 15) & ~15;
838 va->surface_count = 16 + 1;
841 va->surface_count = 2 + 1;
844 LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
845 if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
848 va->surface_count - 1,
852 DXVA2_VideoDecoderRenderTarget,
855 msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed\n");
856 va->surface_count = 0;
859 for (unsigned i = 0; i < va->surface_count; i++) {
860 vlc_va_surface_t *surface = &va->surface[i];
861 surface->d3d = surface_list[i];
862 surface->refcount = 0;
865 msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
866 va->surface_count, fmt->i_width, fmt->i_height);
870 ZeroMemory(&dsc, sizeof(dsc));
871 dsc.SampleWidth = fmt->i_width;
872 dsc.SampleHeight = fmt->i_height;
873 dsc.Format = va->render;
874 if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
875 dsc.InputSampleFreq.Numerator = fmt->i_frame_rate;
876 dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
878 dsc.InputSampleFreq.Numerator = 0;
879 dsc.InputSampleFreq.Denominator = 0;
881 dsc.OutputFrameFreq = dsc.InputSampleFreq;
882 dsc.UABProtectionLevel = FALSE;
885 /* FIXME I am unsure we can let unknown everywhere */
886 DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
887 ext->SampleFormat = 0;//DXVA2_SampleUnknown;
888 ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
889 ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
890 ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
891 ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
892 ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
893 ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
895 /* List all configurations available for the decoder */
897 DXVA2_ConfigPictureDecode *cfg_list = NULL;
898 if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
904 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed\n");
907 msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
909 /* Select the best decoder configuration */
911 for (unsigned i = 0; i < cfg_count; i++) {
912 const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
915 msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
916 i, cfg->ConfigBitstreamRaw);
920 if (cfg->ConfigBitstreamRaw == 1)
922 else if (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
926 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA_NoEncrypt))
929 if (cfg_score < score) {
934 CoTaskMemFree(cfg_list);
935 if (cfg_score <= 0) {
936 msg_Err(va->log, "Failed to find a supported decoder configuration");
940 /* Create the decoder */
941 IDirectXVideoDecoder *decoder;
942 if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
949 msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed\n");
952 va->decoder = decoder;
953 msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
956 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
959 IDirectXVideoDecoder_Release(va->decoder);
962 for (unsigned i = 0; i < va->surface_count; i++)
963 IDirect3DSurface9_Release(va->surface[i].d3d);
964 va->surface_count = 0;
966 static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
968 msg_Err(va->log, "DxResetVideoDecoder unimplemented");
972 static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
974 switch (va->render) {
975 case MAKEFOURCC('N','V','1','2'):
976 va->output = MAKEFOURCC('Y','V','1','2');
979 va->output = va->render;
982 CopyInitCache(&va->surface_cache, va->surface_width);
984 static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
986 CopyCleanCache(&va->surface_cache);
989 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)