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)
233 HINSTANCE hdxva2_dll;
236 D3DPRESENT_PARAMETERS d3dpp;
238 D3DADAPTER_IDENTIFIER9 d3dai;
239 LPDIRECT3DDEVICE9 d3ddev;
243 IDirect3DDeviceManager9 *devmng;
247 IDirectXVideoDecoderService *vs;
252 DXVA2_ConfigPictureDecode cfg;
253 IDirectXVideoDecoder *decoder;
255 /* Option conversion */
257 copy_cache_t surface_cache;
260 struct dxva_context hw;
263 unsigned surface_count;
264 unsigned surface_order;
267 vlc_fourcc_t surface_chroma;
269 vlc_va_surface_t surface[VA_DXVA2_MAX_SURFACE_COUNT];
270 LPDIRECT3DSURFACE9 hw_surface[VA_DXVA2_MAX_SURFACE_COUNT];
274 static vlc_va_dxva2_t *vlc_va_dxva2_Get(void *external)
276 assert(external == (void*)(&((vlc_va_dxva2_t*)external)->va));
281 static int D3dCreateDevice(vlc_va_dxva2_t *);
282 static void D3dDestroyDevice(vlc_va_dxva2_t *);
283 static char *DxDescribe(vlc_va_dxva2_t *);
285 static int D3dCreateDeviceManager(vlc_va_dxva2_t *);
286 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *);
288 static int DxCreateVideoService(vlc_va_dxva2_t *);
289 static void DxDestroyVideoService(vlc_va_dxva2_t *);
290 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *, GUID *input, D3DFORMAT *output);
292 static int DxCreateVideoDecoder(vlc_va_dxva2_t *,
293 int codec_id, const video_format_t *);
294 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *);
295 static int DxResetVideoDecoder(vlc_va_dxva2_t *);
297 static void DxCreateVideoConversion(vlc_va_dxva2_t *);
298 static void DxDestroyVideoConversion(vlc_va_dxva2_t *);
301 static int Setup(vlc_va_t *external, void **hw, vlc_fourcc_t *chroma,
302 int width, int height)
304 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
306 if (va->surface_width == width &&
307 va->surface_height == height)
311 DxDestroyVideoConversion(va);
312 DxDestroyVideoDecoder(va);
316 if (width <= 0 || height <= 0)
319 /* FIXME transmit a video_format_t by VaSetup directly */
321 memset(&fmt, 0, sizeof(fmt));
323 fmt.i_height = height;
325 if (DxCreateVideoDecoder(va, va->codec_id, &fmt))
328 va->hw.decoder = va->decoder;
329 va->hw.cfg = &va->cfg;
330 va->hw.surface_count = va->surface_count;
331 va->hw.surface = va->hw_surface;
332 for (unsigned i = 0; i < va->surface_count; i++)
333 va->hw.surface[i] = va->surface[i].d3d;
336 DxCreateVideoConversion(va);
341 const d3d_format_t *output = D3dFindFormat(va->output);
342 *chroma = output->codec;
347 static int Extract(vlc_va_t *external, picture_t *picture, AVFrame *ff)
349 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
350 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
352 if (!va->surface_cache.buffer)
356 assert(va->output == MAKEFOURCC('Y','V','1','2'));
360 if (FAILED(IDirect3DSurface9_LockRect(d3d, &lock, NULL, D3DLOCK_READONLY))) {
361 msg_Err(va->log, "Failed to lock surface");
365 if (va->render == MAKEFOURCC('Y','V','1','2')) {
366 uint8_t *plane[3] = {
368 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height,
369 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
370 + (lock.Pitch/2) * (va->surface_height/2)
377 CopyFromYv12(picture, plane, pitch,
378 va->surface_width, va->surface_height,
381 assert(va->render == MAKEFOURCC('N','V','1','2'));
382 uint8_t *plane[2] = {
384 (uint8_t*)lock.pBits + lock.Pitch * va->surface_height
390 CopyFromNv12(picture, plane, pitch,
391 va->surface_width, va->surface_height,
396 IDirect3DSurface9_UnlockRect(d3d);
399 /* FIXME it is nearly common with VAAPI */
400 static int Get(vlc_va_t *external, AVFrame *ff)
402 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
404 /* Check the device */
405 HRESULT hr = IDirect3DDeviceManager9_TestDevice(va->devmng, va->device);
406 if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
407 if (DxResetVideoDecoder(va))
409 } else if (FAILED(hr)) {
410 msg_Err(va->log, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
414 /* Grab an unused surface, in case none are, try the oldest
415 * XXX using the oldest is a workaround in case a problem happens with ffmpeg */
417 for (i = 0, old = 0; i < va->surface_count; i++) {
418 vlc_va_surface_t *surface = &va->surface[i];
420 if (!surface->refcount)
423 if (surface->order < va->surface[old].order)
426 if (i >= va->surface_count)
429 vlc_va_surface_t *surface = &va->surface[i];
431 surface->refcount = 1;
432 surface->order = va->surface_order++;
435 for (int i = 0; i < 4; i++) {
439 if (i == 0 || i == 3)
440 ff->data[i] = (void*)surface->d3d;/* Yummie */
444 static void Release(vlc_va_t *external, AVFrame *ff)
446 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
447 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)ff->data[3];
449 for (unsigned i = 0; i < va->surface_count; i++) {
450 vlc_va_surface_t *surface = &va->surface[i];
452 if (surface->d3d == d3d)
456 static void Close(vlc_va_t *external)
458 vlc_va_dxva2_t *va = vlc_va_dxva2_Get(external);
460 DxDestroyVideoConversion(va);
461 DxDestroyVideoDecoder(va);
462 DxDestroyVideoService(va);
463 D3dDestroyDeviceManager(va);
464 D3dDestroyDevice(va);
467 FreeLibrary(va->hdxva2_dll);
469 FreeLibrary(va->hd3d9_dll);
471 free(va->va.description);
475 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)
477 vlc_va_dxva2_t *va = calloc(1, sizeof(*va));
483 va->codec_id = codec_id;
486 va->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
487 if (!va->hd3d9_dll) {
488 msg_Warn(va->log, "cannot load d3d9.dll");
491 va->hdxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
492 if (!va->hdxva2_dll) {
493 msg_Warn(va->log, "cannot load dxva2.dll");
496 msg_Dbg(va->log, "DLLs loaded");
499 if (D3dCreateDevice(va)) {
500 msg_Err(va->log, "Failed to create Direct3D device");
503 msg_Dbg(va->log, "D3dCreateDevice succeed");
505 if (D3dCreateDeviceManager(va)) {
506 msg_Err(va->log, "D3dCreateDeviceManager failed");
510 if (DxCreateVideoService(va)) {
511 msg_Err(va->log, "DxCreateVideoService failed");
516 if (DxFindVideoServiceConversion(va, &va->input, &va->render)) {
517 msg_Err(va->log, "DxFindVideoServiceConversion failed");
521 /* TODO print the hardware name/vendor for debugging purposes */
522 va->va.description = DxDescribe(va);
523 va->va.setup = Setup;
525 va->va.release = Release;
526 va->va.extract = Extract;
527 va->va.close = Close;
537 * It creates a Direct3D device usable for DXVA 2
539 static int D3dCreateDevice(vlc_va_dxva2_t *va)
542 LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
543 Create9 = (void *)GetProcAddress(va->hd3d9_dll,
544 TEXT("Direct3DCreate9"));
546 msg_Err(va->log, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
552 d3dobj = Create9(D3D_SDK_VERSION);
554 msg_Err(va->log, "Direct3DCreate9 failed");
560 D3DADAPTER_IDENTIFIER9 *d3dai = &va->d3dai;
561 if (FAILED(IDirect3D9_GetAdapterIdentifier(va->d3dobj,
562 D3DADAPTER_DEFAULT, 0, d3dai))) {
563 msg_Warn(va->log, "IDirect3D9_GetAdapterIdentifier failed");
564 ZeroMemory(d3dai, sizeof(*d3dai));
568 D3DPRESENT_PARAMETERS *d3dpp = &va->d3dpp;
569 ZeroMemory(d3dpp, sizeof(*d3dpp));
570 d3dpp->Flags = D3DPRESENTFLAG_VIDEO;
571 d3dpp->Windowed = TRUE;
572 d3dpp->hDeviceWindow = NULL;
573 d3dpp->SwapEffect = D3DSWAPEFFECT_DISCARD;
574 d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
575 d3dpp->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
576 d3dpp->BackBufferCount = 0; /* FIXME what to put here */
577 d3dpp->BackBufferFormat = D3DFMT_X8R8G8B8; /* FIXME what to put here */
578 d3dpp->BackBufferWidth = 0;
579 d3dpp->BackBufferHeight = 0;
580 d3dpp->EnableAutoDepthStencil = FALSE;
582 /* Direct3D needs a HWND to create a device, even without using ::Present
583 this HWND is used to alert Direct3D when there's a change of focus window.
584 For now, use GetShellWindow, as it looks harmless */
585 LPDIRECT3DDEVICE9 d3ddev;
586 if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
587 D3DDEVTYPE_HAL, GetShellWindow(),
588 D3DCREATE_SOFTWARE_VERTEXPROCESSING |
589 D3DCREATE_MULTITHREADED,
591 msg_Err(va->log, "IDirect3D9_CreateDevice failed");
599 * It releases a Direct3D device and its resources.
601 static void D3dDestroyDevice(vlc_va_dxva2_t *va)
604 IDirect3DDevice9_Release(va->d3ddev);
606 IDirect3D9_Release(va->d3dobj);
609 * It describes our Direct3D object
611 static char *DxDescribe(vlc_va_dxva2_t *va)
613 static const struct {
618 { 0x10DE, "NVIDIA" },
620 { 0x5333, "S3 Graphics" },
623 D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
625 const char *vendor = "Unknown";
626 for (int i = 0; vendors[i].id != 0; i++) {
627 if (vendors[i].id == id->VendorId) {
628 vendor = vendors[i].name;
634 if (asprintf(&description, "DXVA2 (%.*s, vendor %d(%s), device %d, revision %d)",
635 sizeof(id->Description), id->Description,
636 id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
642 * It creates a Direct3D device manager
644 static int D3dCreateDeviceManager(vlc_va_dxva2_t *va)
646 HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
647 IDirect3DDeviceManager9 **);
648 CreateDeviceManager9 =
649 (void *)GetProcAddress(va->hdxva2_dll,
650 TEXT("DXVA2CreateDirect3DDeviceManager9"));
652 if (!CreateDeviceManager9) {
653 msg_Err(va->log, "cannot load function\n");
656 msg_Dbg(va->log, "OurDirect3DCreateDeviceManager9 Success!");
659 IDirect3DDeviceManager9 *devmng;
660 if (FAILED(CreateDeviceManager9(&token, &devmng))) {
661 msg_Err(va->log, " OurDirect3DCreateDeviceManager9 failed");
666 msg_Info(va->log, "obtained IDirect3DDeviceManager9");
668 HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, va->d3ddev, token);
670 msg_Err(va->log, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
676 * It destroys a Direct3D device manager
678 static void D3dDestroyDeviceManager(vlc_va_dxva2_t *va)
681 IDirect3DDeviceManager9_Release(va->devmng);
685 * It creates a DirectX video service
687 static int DxCreateVideoService(vlc_va_dxva2_t *va)
689 HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
693 (void *)GetProcAddress(va->hdxva2_dll,
694 TEXT("DXVA2CreateVideoService"));
696 if (!CreateVideoService) {
697 msg_Err(va->log, "cannot load function\n");
700 msg_Info(va->log, "DXVA2CreateVideoService Success!");
705 hr = IDirect3DDeviceManager9_OpenDeviceHandle(va->devmng, &device);
707 msg_Err(va->log, "OpenDeviceHandle failed");
712 IDirectXVideoDecoderService *vs;
713 hr = IDirect3DDeviceManager9_GetVideoService(va->devmng, device,
714 &IID_IDirectXVideoDecoderService,
717 msg_Err(va->log, "GetVideoService failed");
725 * It destroys a DirectX video service
727 static void DxDestroyVideoService(vlc_va_dxva2_t *va)
730 IDirect3DDeviceManager9_CloseDeviceHandle(va->devmng, va->device);
732 IDirectXVideoDecoderService_Release(va->vs);
735 * Find the best suited decoder mode GUID and render format.
737 static int DxFindVideoServiceConversion(vlc_va_dxva2_t *va, GUID *input, D3DFORMAT *output)
739 /* Retreive supported modes from the decoder service */
740 UINT input_count = 0;
741 GUID *input_list = NULL;
742 if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(va->vs,
745 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
748 for (unsigned i = 0; i < input_count; i++) {
749 const GUID *g = &input_list[i];
750 const dxva2_mode_t *mode = Dxva2FindMode(g);
752 msg_Dbg(va->log, "- '%s' is supported by hardware", mode->name);
754 msg_Warn(va->log, "- Unknown GUID = %08X-%04x-%04x-XXXX",
755 (unsigned)g->Data1, g->Data2, g->Data3);
759 /* Try all supported mode by our priority */
760 for (unsigned i = 0; dxva2_modes[i].name; i++) {
761 const dxva2_mode_t *mode = &dxva2_modes[i];
762 if (!mode->codec || mode->codec != va->codec_id)
766 bool is_suported = false;
767 for (const GUID *g = &input_list[0]; !is_suported && g < &input_list[input_count]; g++) {
768 is_suported = IsEqualGUID(mode->guid, g);
774 msg_Dbg(va->log, "Trying to use '%s' as input", mode->name);
775 UINT output_count = 0;
776 D3DFORMAT *output_list = NULL;
777 if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->vs, mode->guid,
780 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
783 for (unsigned j = 0; j < output_count; j++) {
784 const D3DFORMAT f = output_list[j];
785 const d3d_format_t *format = D3dFindFormat(f);
787 msg_Dbg(va->log, "%s is supported for output", format->name);
789 msg_Dbg(va->log, "%d is supported for output (%4.4s)", f, (const char*)&f);
794 for (unsigned j = 0; d3d_formats[j].name; j++) {
795 const d3d_format_t *format = &d3d_formats[j];
798 bool is_suported = false;
799 for (unsigned k = 0; !is_suported && k < output_count; k++) {
800 is_suported = format->format == output_list[k];
805 /* We have our solution */
806 msg_Dbg(va->log, "Using '%s' to decode to '%s'", mode->name, format->name);
807 *input = *mode->guid;
808 *output = format->format;
809 CoTaskMemFree(output_list);
810 CoTaskMemFree(input_list);
813 CoTaskMemFree(output_list);
815 CoTaskMemFree(input_list);
820 * It creates a DXVA2 decoder using the given video format
822 static int DxCreateVideoDecoder(vlc_va_dxva2_t *va,
823 int codec_id, const video_format_t *fmt)
826 msg_Dbg(va->log, "DxCreateVideoDecoder id %d %dx%d",
827 codec_id, fmt->i_width, fmt->i_height);
829 /* Allocates all surfaces needed for the decoder */
832 va->surface_count = 16 + 1;
835 va->surface_count = 2 + 1;
838 LPDIRECT3DSURFACE9 surface_list[VA_DXVA2_MAX_SURFACE_COUNT];
839 if (FAILED(IDirectXVideoDecoderService_CreateSurface(va->vs,
842 va->surface_count - 1,
846 DXVA2_VideoDecoderRenderTarget,
849 msg_Err(va->log, "IDirectXVideoAccelerationService_CreateSurface failed\n");
850 va->surface_count = 0;
853 for (unsigned i = 0; i < va->surface_count; i++) {
854 vlc_va_surface_t *surface = &va->surface[i];
855 surface->d3d = surface_list[i];
856 surface->refcount = 0;
859 va->surface_width = fmt->i_width;
860 va->surface_height = fmt->i_height;
861 msg_Dbg(va->log, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
862 va->surface_count, fmt->i_width, fmt->i_height);
866 ZeroMemory(&dsc, sizeof(dsc));
867 dsc.SampleWidth = fmt->i_width;
868 dsc.SampleHeight = fmt->i_height;
869 dsc.Format = va->render;
870 if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
871 dsc.InputSampleFreq.Numerator = fmt->i_frame_rate;
872 dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
874 dsc.InputSampleFreq.Numerator = 0;
875 dsc.InputSampleFreq.Denominator = 0;
877 dsc.OutputFrameFreq = dsc.InputSampleFreq;
878 dsc.UABProtectionLevel = FALSE;
881 /* FIXME I am unsure we can let unknown everywhere */
882 DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
883 ext->SampleFormat = 0;//DXVA2_SampleUnknown;
884 ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
885 ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
886 ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
887 ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
888 ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
889 ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
891 /* List all configurations available for the decoder */
893 DXVA2_ConfigPictureDecode *cfg_list = NULL;
894 if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(va->vs,
900 msg_Err(va->log, "IDirectXVideoDecoderService_GetDecoderConfigurations failed\n");
903 msg_Dbg(va->log, "we got %d decoder configurations", cfg_count);
905 /* Select the best decoder configuration */
907 for (unsigned i = 0; i < cfg_count; i++) {
908 const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
911 msg_Dbg(va->log, "configuration[%d] ConfigBitstreamRaw %d",
912 i, cfg->ConfigBitstreamRaw);
916 if (cfg->ConfigBitstreamRaw == 1)
918 else if (codec_id == CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
922 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA_NoEncrypt))
925 if (cfg_score < score) {
930 CoTaskMemFree(cfg_list);
931 if (cfg_score <= 0) {
932 msg_Err(va->log, "Failed to find a supported decoder configuration");
936 /* Create the decoder */
937 IDirectXVideoDecoder *decoder;
938 if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(va->vs,
945 msg_Err(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder failed\n");
948 va->decoder = decoder;
949 msg_Dbg(va->log, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
952 static void DxDestroyVideoDecoder(vlc_va_dxva2_t *va)
955 IDirectXVideoDecoder_Release(va->decoder);
958 for (unsigned i = 0; i < va->surface_count; i++)
959 IDirect3DSurface9_Release(va->surface[i].d3d);
960 va->surface_count = 0;
962 static int DxResetVideoDecoder(vlc_va_dxva2_t *va)
964 msg_Err(va->log, "DxResetVideoDecoder unimplemented");
968 static void DxCreateVideoConversion(vlc_va_dxva2_t *va)
970 switch (va->render) {
971 case MAKEFOURCC('N','V','1','2'):
972 va->output = MAKEFOURCC('Y','V','1','2');
975 va->output = va->render;
978 CopyInitCache(&va->surface_cache, va->surface_width);
980 static void DxDestroyVideoConversion(vlc_va_dxva2_t *va)
982 CopyCleanCache(&va->surface_cache);
985 vlc_va_t *vlc_va_NewDxva2(vlc_object_t *log, int codec_id)