1 /*****************************************************************************
2 * dxva2.c: Video Acceleration helpers
3 *****************************************************************************
4 * Copyright (C) 2009 Geoffroy Couprie
5 * Copyright (C) 2009 Laurent Aimar
6 * $Id: 2b6a1fc26aee7508c4f99dc17a9a7dcb11a170af $
7 *
8 * Authors: Geoffroy Couprie <geal@videolan.org>
9 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
10 *
11 * This program is free software; you can redistribute it and/or modify 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.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
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 *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <assert.h>
31
32 #include <vlc_common.h>
33 #include <vlc_picture.h>
34 #include <vlc_filter.h> /* need for the DXA9 to YV12 conversion */
35 #include <vlc_modules.h>
36 #include <vlc_plugin.h>
37
38 #include "directx_va.h"
39
40 #define DXVA2API_USE_BITFIELDS
41 #define COBJMACROS
42 #include <libavcodec/dxva2.h>
43
44 static int Open(vlc_va_t *, AVCodecContext *, enum PixelFormat,
45 const es_format_t *, picture_sys_t *p_sys);
46 static void Close(vlc_va_t *, AVCodecContext *);
47
48 vlc_module_begin()
49 set_description(N_("DirectX Video Acceleration (DXVA) 2.0"))
50 set_capability("hw decoder", 0)
51 set_category(CAT_INPUT)
52 set_subcategory(SUBCAT_INPUT_VCODEC)
53 set_callbacks(Open, Close)
54 vlc_module_end()
55
56 #include <initguid.h> /* must be last included to not redefine existing GUIDs */
57
58 /* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
59 * assume that they are declared in dxva2api.h */
60 #define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
61
62 #ifdef __MINGW32__
63 # include <_mingw.h>
64
65 # if !defined(__MINGW64_VERSION_MAJOR)
66 # undef MS_GUID
67 # define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
68 # define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
69 # else
70 # include <dxva.h>
71 # endif
72
73 #endif /* __MINGW32__ */
74
75 MS_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
76 MS_GUID(IID_IDirectXVideoAccelerationService, 0xfc51a550, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
77
78 DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
79
80 DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo, 0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
81
82
83 /* */
84 typedef struct {
85 const char *name;
86 D3DFORMAT format;
87 vlc_fourcc_t codec;
88 } d3d_format_t;
89 /* XXX Prefered format must come first */
90 static const d3d_format_t d3d_formats[] = {
91 { "YV12", MAKEFOURCC('Y','V','1','2'), VLC_CODEC_YV12 },
92 { "NV12", MAKEFOURCC('N','V','1','2'), VLC_CODEC_NV12 },
93 { "IMC3", MAKEFOURCC('I','M','C','3'), VLC_CODEC_YV12 },
94 { "P010", MAKEFOURCC('P','0','1','0'), VLC_CODEC_P010 },
95
96 { NULL, 0, 0 }
97 };
98
D3dFindFormat(D3DFORMAT format)99 static const d3d_format_t *D3dFindFormat(D3DFORMAT format)
100 {
101 for (unsigned i = 0; d3d_formats[i].name; i++) {
102 if (d3d_formats[i].format == format)
103 return &d3d_formats[i];
104 }
105 return NULL;
106 }
107
108 struct vlc_va_sys_t
109 {
110 directx_sys_t dx_sys;
111 vlc_fourcc_t i_chroma;
112
113 /* DLL */
114 HINSTANCE hd3d9_dll;
115
116 /* Direct3D */
117 LPDIRECT3D9 d3dobj;
118 D3DADAPTER_IDENTIFIER9 d3dai;
119
120 /* Device manager */
121 IDirect3DDeviceManager9 *devmng;
122 HANDLE device;
123
124 /* Video service */
125 D3DFORMAT render;
126
127 /* Video decoder */
128 DXVA2_ConfigPictureDecode cfg;
129
130 /* Option conversion */
131 filter_t *filter;
132
133 /* avcodec internals */
134 struct dxva_context hw;
135 };
136
137 struct picture_sys_t
138 {
139 LPDIRECT3DSURFACE9 surface;
140 };
141
142 static picture_t *DxAllocPicture(vlc_va_t *, const video_format_t *, unsigned index);
143
144
145 /* */
146 static int D3dCreateDevice(vlc_va_t *);
147 static void D3dDestroyDevice(vlc_va_t *);
148 static char *DxDescribe(vlc_va_sys_t *);
149
150 static int D3dCreateDeviceManager(vlc_va_t *);
151 static void D3dDestroyDeviceManager(vlc_va_t *);
152
153 static int DxCreateVideoService(vlc_va_t *);
154 static void DxDestroyVideoService(vlc_va_t *);
155 static int DxGetInputList(vlc_va_t *, input_list_t *);
156 static int DxSetupOutput(vlc_va_t *, const GUID *, const video_format_t *);
157
158 static int DxCreateVideoDecoder(vlc_va_t *,
159 int codec_id, const video_format_t *);
160 static void DxDestroyVideoDecoder(vlc_va_t *);
161 static int DxResetVideoDecoder(vlc_va_t *);
162 static void SetupAVCodecContext(vlc_va_t *);
163
DeleteFilter(filter_t * p_filter)164 static void DeleteFilter( filter_t * p_filter )
165 {
166 if( p_filter->p_module )
167 module_unneed( p_filter, p_filter->p_module );
168
169 es_format_Clean( &p_filter->fmt_in );
170 es_format_Clean( &p_filter->fmt_out );
171
172 vlc_object_release( p_filter );
173 }
174
video_new_buffer(filter_t * p_filter)175 static picture_t *video_new_buffer(filter_t *p_filter)
176 {
177 return p_filter->owner.sys;
178 }
179
CreateFilter(vlc_object_t * p_this,const es_format_t * p_fmt_in,vlc_fourcc_t src_chroma)180 static filter_t *CreateFilter( vlc_object_t *p_this, const es_format_t *p_fmt_in,
181 vlc_fourcc_t src_chroma )
182 {
183 filter_t *p_filter;
184
185 p_filter = vlc_object_create( p_this, sizeof(filter_t) );
186 if (unlikely(p_filter == NULL))
187 return NULL;
188
189 p_filter->owner.video.buffer_new = (picture_t *(*)(filter_t *))video_new_buffer;
190
191 vlc_fourcc_t fmt_out;
192 switch (src_chroma)
193 {
194 case VLC_CODEC_D3D9_OPAQUE_10B:
195 fmt_out = VLC_CODEC_P010;
196 break;
197 case VLC_CODEC_D3D9_OPAQUE:
198 fmt_out = VLC_CODEC_YV12;
199 break;
200 }
201
202 es_format_InitFromVideo( &p_filter->fmt_in, &p_fmt_in->video );
203 es_format_InitFromVideo( &p_filter->fmt_out, &p_fmt_in->video );
204 p_filter->fmt_in.i_codec = p_filter->fmt_in.video.i_chroma = src_chroma;
205 p_filter->fmt_out.i_codec = p_filter->fmt_out.video.i_chroma = fmt_out;
206 p_filter->p_module = module_need( p_filter, "video filter", NULL, false );
207
208 if( !p_filter->p_module )
209 {
210 msg_Dbg( p_filter, "no video filter found" );
211 DeleteFilter( p_filter );
212 return NULL;
213 }
214
215 return p_filter;
216 }
217
218 /* */
Setup(vlc_va_t * va,vlc_fourcc_t * chroma)219 static void Setup(vlc_va_t *va, vlc_fourcc_t *chroma)
220 {
221 vlc_va_sys_t *sys = va->sys;
222
223 *chroma = sys->filter == NULL ? sys->i_chroma : VLC_CODEC_YV12;
224 }
225
SetupAVCodecContext(vlc_va_t * va)226 void SetupAVCodecContext(vlc_va_t *va)
227 {
228 vlc_va_sys_t *sys = va->sys;
229 directx_sys_t *dx_sys = &sys->dx_sys;
230
231 sys->hw.decoder = (IDirectXVideoDecoder*) dx_sys->decoder;
232 sys->hw.cfg = &sys->cfg;
233 sys->hw.surface_count = dx_sys->surface_count;
234 sys->hw.surface = (LPDIRECT3DSURFACE9*) dx_sys->hw_surface;
235
236 if (IsEqualGUID(&dx_sys->input, &DXVA_Intel_H264_NoFGT_ClearVideo))
237 sys->hw.workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
238 }
239
Extract(vlc_va_t * va,picture_t * picture,uint8_t * data)240 static int Extract(vlc_va_t *va, picture_t *picture, uint8_t *data)
241 {
242 directx_sys_t *dx_sys = &va->sys->dx_sys;
243 LPDIRECT3DSURFACE9 d3d = (LPDIRECT3DSURFACE9)(uintptr_t)data;
244 if ( picture->format.i_chroma == VLC_CODEC_D3D9_OPAQUE ||
245 picture->format.i_chroma == VLC_CODEC_D3D9_OPAQUE_10B )
246 {
247 picture_sys_t *p_sys = picture->p_sys;
248 LPDIRECT3DSURFACE9 output = p_sys->surface;
249
250 assert(d3d != output);
251 #ifndef NDEBUG
252 LPDIRECT3DDEVICE9 srcDevice, dstDevice;
253 IDirect3DSurface9_GetDevice(d3d, &srcDevice);
254 IDirect3DSurface9_GetDevice(output, &dstDevice);
255 assert(srcDevice == dstDevice);
256 #endif
257
258 HRESULT hr;
259 RECT visibleSource;
260 visibleSource.left = 0;
261 visibleSource.top = 0;
262 visibleSource.right = picture->format.i_visible_width;
263 visibleSource.bottom = picture->format.i_visible_height;
264 hr = IDirect3DDevice9_StretchRect( (IDirect3DDevice9*) dx_sys->d3ddev, d3d, &visibleSource, output, &visibleSource, D3DTEXF_NONE);
265 if (FAILED(hr)) {
266 msg_Err(va, "Failed to copy the hw surface to the decoder surface (hr=0x%0lx)", hr );
267 return VLC_EGENERIC;
268 }
269 }
270 else if (va->sys->filter != NULL) {
271 va->sys->filter->owner.sys = picture;
272 vlc_va_surface_t *surface = picture->context;
273 picture_Hold( surface->p_pic );
274 va->sys->filter->pf_video_filter( va->sys->filter, surface->p_pic );
275 } else {
276 msg_Err(va, "Unsupported output picture format %08X", picture->format.i_chroma );
277 return VLC_EGENERIC;
278 }
279
280 return VLC_SUCCESS;
281 }
282
CheckDevice(vlc_va_t * va)283 static int CheckDevice(vlc_va_t *va)
284 {
285 vlc_va_sys_t *sys = va->sys;
286
287 /* Check the device */
288 HRESULT hr = IDirect3DDeviceManager9_TestDevice(sys->devmng, sys->device);
289 if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
290 if (DxResetVideoDecoder(va))
291 return VLC_EGENERIC;
292 } else if (FAILED(hr)) {
293 msg_Err(va, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
294 return VLC_EGENERIC;
295 }
296 return VLC_SUCCESS;
297 }
298
Get(vlc_va_t * va,picture_t * pic,uint8_t ** data)299 static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
300 {
301 return directx_va_Get(va, &va->sys->dx_sys, pic, data);
302 }
303
Close(vlc_va_t * va,AVCodecContext * ctx)304 static void Close(vlc_va_t *va, AVCodecContext *ctx)
305 {
306 vlc_va_sys_t *sys = va->sys;
307
308 (void) ctx;
309
310 if (sys->filter)
311 {
312 DeleteFilter( sys->filter );
313 sys->filter = NULL;
314 }
315
316 directx_va_Close(va, &sys->dx_sys);
317
318 if (sys->hd3d9_dll)
319 FreeLibrary(sys->hd3d9_dll);
320
321 free((char *)va->description);
322 free(sys);
323 }
324
d3d9va_fourcc(enum PixelFormat swfmt)325 vlc_fourcc_t d3d9va_fourcc(enum PixelFormat swfmt)
326 {
327 switch (swfmt)
328 {
329 case AV_PIX_FMT_YUV420P10LE:
330 return VLC_CODEC_D3D9_OPAQUE_10B;
331 case AV_PIX_FMT_YUVJ420P:
332 case AV_PIX_FMT_YUV420P:
333 return VLC_CODEC_D3D9_OPAQUE;
334 default:
335 return VLC_CODEC_D3D9_OPAQUE;
336 }
337 }
338
Open(vlc_va_t * va,AVCodecContext * ctx,enum PixelFormat pix_fmt,const es_format_t * fmt,picture_sys_t * p_sys)339 static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
340 const es_format_t *fmt, picture_sys_t *p_sys)
341 {
342 int err = VLC_EGENERIC;
343 directx_sys_t *dx_sys;
344
345 if (pix_fmt != AV_PIX_FMT_DXVA2_VLD)
346 return VLC_EGENERIC;
347
348 vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
349 if (unlikely(sys == NULL))
350 return VLC_ENOMEM;
351
352 /* Load dll*/
353 sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
354 if (!sys->hd3d9_dll) {
355 msg_Warn(va, "cannot load d3d9.dll");
356 goto error;
357 }
358
359 dx_sys = &sys->dx_sys;
360
361 dx_sys->pf_check_device = CheckDevice;
362 dx_sys->pf_create_device = D3dCreateDevice;
363 dx_sys->pf_destroy_device = D3dDestroyDevice;
364 dx_sys->pf_create_device_manager = D3dCreateDeviceManager;
365 dx_sys->pf_destroy_device_manager = D3dDestroyDeviceManager;
366 dx_sys->pf_create_video_service = DxCreateVideoService;
367 dx_sys->pf_destroy_video_service = DxDestroyVideoService;
368 dx_sys->pf_create_decoder_surfaces = DxCreateVideoDecoder;
369 dx_sys->pf_destroy_surfaces = DxDestroyVideoDecoder;
370 dx_sys->pf_setup_avcodec_ctx = SetupAVCodecContext;
371 dx_sys->pf_get_input_list = DxGetInputList;
372 dx_sys->pf_setup_output = DxSetupOutput;
373 dx_sys->pf_alloc_surface_pic = DxAllocPicture;
374 dx_sys->psz_decoder_dll = TEXT("DXVA2.DLL");
375
376 va->sys = sys;
377
378 dx_sys->d3ddev = NULL;
379 if (p_sys!=NULL)
380 IDirect3DSurface9_GetDevice(p_sys->surface, (IDirect3DDevice9**) &dx_sys->d3ddev );
381
382 sys->i_chroma = d3d9va_fourcc(ctx->sw_pix_fmt);
383
384 err = directx_va_Open(va, &sys->dx_sys, ctx, fmt, true);
385 if (err!=VLC_SUCCESS)
386 goto error;
387
388 if (p_sys == NULL)
389 {
390 sys->filter = CreateFilter( VLC_OBJECT(va), fmt, sys->i_chroma);
391 if (sys->filter == NULL)
392 goto error;
393 }
394
395 err = directx_va_Setup(va, &sys->dx_sys, ctx);
396 if (err != VLC_SUCCESS)
397 goto error;
398
399 ctx->hwaccel_context = &sys->hw;
400
401 /* TODO print the hardware name/vendor for debugging purposes */
402 va->description = DxDescribe(sys);
403 va->setup = Setup;
404 va->get = Get;
405 va->release = directx_va_Release;
406 va->extract = Extract;
407 return VLC_SUCCESS;
408
409 error:
410 Close(va, ctx);
411 return VLC_EGENERIC;
412 }
413 /* */
414
415 /**
416 * It creates a Direct3D device usable for DXVA 2
417 */
D3dCreateDevice(vlc_va_t * va)418 static int D3dCreateDevice(vlc_va_t *va)
419 {
420 vlc_va_sys_t *sys = va->sys;
421
422 if (sys->dx_sys.d3ddev) {
423 msg_Dbg(va, "Reusing Direct3D9 device");
424 IDirect3DDevice9_AddRef(sys->dx_sys.d3ddev);
425 return VLC_SUCCESS;
426 }
427
428 /* */
429 LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
430 Create9 = (void *)GetProcAddress(sys->hd3d9_dll, "Direct3DCreate9");
431 if (!Create9) {
432 msg_Err(va, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
433 return VLC_EGENERIC;
434 }
435
436 /* */
437 LPDIRECT3D9 d3dobj;
438 d3dobj = Create9(D3D_SDK_VERSION);
439 if (!d3dobj) {
440 msg_Err(va, "Direct3DCreate9 failed");
441 return VLC_EGENERIC;
442 }
443 sys->d3dobj = d3dobj;
444
445 /* */
446 D3DADAPTER_IDENTIFIER9 *d3dai = &sys->d3dai;
447 if (FAILED(IDirect3D9_GetAdapterIdentifier(sys->d3dobj,
448 D3DADAPTER_DEFAULT, 0, d3dai))) {
449 msg_Warn(va, "IDirect3D9_GetAdapterIdentifier failed");
450 ZeroMemory(d3dai, sizeof(*d3dai));
451 }
452
453 /* */
454 D3DPRESENT_PARAMETERS d3dpp;
455 ZeroMemory(&d3dpp, sizeof(d3dpp));
456 d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
457 d3dpp.Windowed = TRUE;
458 d3dpp.hDeviceWindow = NULL;
459 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
460 d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
461 d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
462 d3dpp.BackBufferCount = 0; /* FIXME what to put here */
463 d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; /* FIXME what to put here */
464 d3dpp.BackBufferWidth = 0;
465 d3dpp.BackBufferHeight = 0;
466 d3dpp.EnableAutoDepthStencil = FALSE;
467
468 /* Direct3D needs a HWND to create a device, even without using ::Present
469 this HWND is used to alert Direct3D when there's a change of focus window.
470 For now, use GetDesktopWindow, as it looks harmless */
471 LPDIRECT3DDEVICE9 d3ddev;
472 if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
473 D3DDEVTYPE_HAL, GetDesktopWindow(),
474 D3DCREATE_SOFTWARE_VERTEXPROCESSING |
475 D3DCREATE_MULTITHREADED,
476 &d3dpp, &d3ddev))) {
477 msg_Err(va, "IDirect3D9_CreateDevice failed");
478 return VLC_EGENERIC;
479 }
480 sys->dx_sys.d3ddev = (IUnknown*) d3ddev;
481
482 return VLC_SUCCESS;
483 }
484
485 /**
486 * It releases a Direct3D device and its resources.
487 */
D3dDestroyDevice(vlc_va_t * va)488 static void D3dDestroyDevice(vlc_va_t *va)
489 {
490 if (va->sys->d3dobj)
491 IDirect3D9_Release(va->sys->d3dobj);
492 }
493 /**
494 * It describes our Direct3D object
495 */
DxDescribe(vlc_va_sys_t * va)496 static char *DxDescribe(vlc_va_sys_t *va)
497 {
498 static const struct {
499 unsigned id;
500 char name[32];
501 } vendors [] = {
502 { 0x1002, "ATI" },
503 { 0x10DE, "NVIDIA" },
504 { 0x1106, "VIA" },
505 { 0x8086, "Intel" },
506 { 0x5333, "S3 Graphics" },
507 { 0, "" }
508 };
509 D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
510
511 const char *vendor = "Unknown";
512 for (int i = 0; vendors[i].id != 0; i++) {
513 if (vendors[i].id == id->VendorId) {
514 vendor = vendors[i].name;
515 break;
516 }
517 }
518
519 char *description;
520 if (asprintf(&description, "DXVA2 (%.*s, vendor %lu(%s), device %lu, revision %lu)",
521 (int)sizeof(id->Description), id->Description,
522 id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
523 return NULL;
524 return description;
525 }
526
527 /**
528 * It creates a Direct3D device manager
529 */
D3dCreateDeviceManager(vlc_va_t * va)530 static int D3dCreateDeviceManager(vlc_va_t *va)
531 {
532 vlc_va_sys_t *sys = va->sys;
533 directx_sys_t *dx_sys = &va->sys->dx_sys;
534
535 HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
536 IDirect3DDeviceManager9 **);
537 CreateDeviceManager9 =
538 (void *)GetProcAddress(dx_sys->hdecoder_dll,
539 "DXVA2CreateDirect3DDeviceManager9");
540
541 if (!CreateDeviceManager9) {
542 msg_Err(va, "cannot load function");
543 return VLC_EGENERIC;
544 }
545 msg_Dbg(va, "OurDirect3DCreateDeviceManager9 Success!");
546
547 UINT token;
548 IDirect3DDeviceManager9 *devmng;
549 if (FAILED(CreateDeviceManager9(&token, &devmng))) {
550 msg_Err(va, " OurDirect3DCreateDeviceManager9 failed");
551 return VLC_EGENERIC;
552 }
553 sys->devmng = devmng;
554 msg_Info(va, "obtained IDirect3DDeviceManager9");
555
556 HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, (IDirect3DDevice9*) dx_sys->d3ddev, token);
557 if (FAILED(hr)) {
558 msg_Err(va, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
559 return VLC_EGENERIC;
560 }
561 return VLC_SUCCESS;
562 }
563 /**
564 * It destroys a Direct3D device manager
565 */
D3dDestroyDeviceManager(vlc_va_t * va)566 static void D3dDestroyDeviceManager(vlc_va_t *va)
567 {
568 if (va->sys->devmng)
569 IDirect3DDeviceManager9_Release(va->sys->devmng);
570 }
571
572 /**
573 * It creates a DirectX video service
574 */
DxCreateVideoService(vlc_va_t * va)575 static int DxCreateVideoService(vlc_va_t *va)
576 {
577 vlc_va_sys_t *sys = va->sys;
578 directx_sys_t *dx_sys = &va->sys->dx_sys;
579
580 HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
581 REFIID riid,
582 void **ppService);
583 CreateVideoService =
584 (void *)GetProcAddress(dx_sys->hdecoder_dll, "DXVA2CreateVideoService");
585
586 if (!CreateVideoService) {
587 msg_Err(va, "cannot load function");
588 return 4;
589 }
590 msg_Info(va, "DXVA2CreateVideoService Success!");
591
592 HRESULT hr;
593
594 HANDLE device;
595 hr = IDirect3DDeviceManager9_OpenDeviceHandle(sys->devmng, &device);
596 if (FAILED(hr)) {
597 msg_Err(va, "OpenDeviceHandle failed");
598 return VLC_EGENERIC;
599 }
600 sys->device = device;
601
602 void *pv;
603 hr = IDirect3DDeviceManager9_GetVideoService(sys->devmng, device,
604 &IID_IDirectXVideoDecoderService, &pv);
605 if (FAILED(hr)) {
606 msg_Err(va, "GetVideoService failed");
607 return VLC_EGENERIC;
608 }
609 dx_sys->d3ddec = pv;
610
611 return VLC_SUCCESS;
612 }
613
614 /**
615 * It destroys a DirectX video service
616 */
DxDestroyVideoService(vlc_va_t * va)617 static void DxDestroyVideoService(vlc_va_t *va)
618 {
619 if (va->sys->device)
620 IDirect3DDeviceManager9_CloseDeviceHandle(va->sys->devmng, va->sys->device);
621 }
622
ReleaseInputList(input_list_t * p_list)623 static void ReleaseInputList(input_list_t *p_list)
624 {
625 CoTaskMemFree(p_list->list);
626 }
627
DxGetInputList(vlc_va_t * va,input_list_t * p_list)628 static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
629 {
630 directx_sys_t *dx_sys = &va->sys->dx_sys;
631 UINT input_count = 0;
632 GUID *input_list = NULL;
633 if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids((IDirectXVideoDecoderService*) dx_sys->d3ddec,
634 &input_count,
635 &input_list))) {
636 msg_Err(va, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
637 return VLC_EGENERIC;
638 }
639
640 p_list->count = input_count;
641 p_list->list = input_list;
642 p_list->pf_release = ReleaseInputList;
643 return VLC_SUCCESS;
644 }
645
DxSetupOutput(vlc_va_t * va,const GUID * input,const video_format_t * fmt)646 static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
647 {
648 VLC_UNUSED(fmt);
649 int err = VLC_EGENERIC;
650 UINT output_count = 0;
651 D3DFORMAT *output_list = NULL;
652 if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets((IDirectXVideoDecoderService*) va->sys->dx_sys.d3ddec,
653 input,
654 &output_count,
655 &output_list))) {
656 msg_Err(va, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
657 return VLC_EGENERIC;
658 }
659
660 for (unsigned j = 0; j < output_count; j++) {
661 const D3DFORMAT f = output_list[j];
662 const d3d_format_t *format = D3dFindFormat(f);
663 if (format) {
664 msg_Dbg(va, "%s is supported for output", format->name);
665 } else {
666 msg_Dbg(va, "%d is supported for output (%4.4s)", f, (const char*)&f);
667 }
668 }
669
670 /* */
671 for (unsigned j = 0; d3d_formats[j].name; j++) {
672 const d3d_format_t *format = &d3d_formats[j];
673
674 /* */
675 bool is_supported = false;
676 for (unsigned k = 0; !is_supported && k < output_count; k++) {
677 is_supported = format->format == output_list[k];
678 }
679 if (!is_supported)
680 continue;
681
682 /* We have our solution */
683 msg_Dbg(va, "Using decoder output '%s'", format->name);
684 va->sys->render = format->format;
685 err = VLC_SUCCESS;
686 break;
687 }
688 CoTaskMemFree(output_list);
689 return err;
690 }
691
692 /**
693 * It creates a DXVA2 decoder using the given video format
694 */
DxCreateVideoDecoder(vlc_va_t * va,int codec_id,const video_format_t * fmt)695 static int DxCreateVideoDecoder(vlc_va_t *va, int codec_id, const video_format_t *fmt)
696 {
697 vlc_va_sys_t *p_sys = va->sys;
698 directx_sys_t *sys = &va->sys->dx_sys;
699 HRESULT hr;
700
701 hr = IDirectXVideoDecoderService_CreateSurface((IDirectXVideoDecoderService*) sys->d3ddec,
702 sys->surface_width,
703 sys->surface_height,
704 sys->surface_count - 1,
705 p_sys->render,
706 D3DPOOL_DEFAULT,
707 0,
708 DXVA2_VideoDecoderRenderTarget,
709 (LPDIRECT3DSURFACE9*) sys->hw_surface,
710 NULL);
711 if (FAILED(hr)) {
712 msg_Err(va, "IDirectXVideoAccelerationService_CreateSurface %d failed (hr=0x%0lx)", sys->surface_count - 1, hr);
713 sys->surface_count = 0;
714 return VLC_EGENERIC;
715 }
716 msg_Dbg(va, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
717 sys->surface_count, sys->surface_width, sys->surface_height);
718
719 IDirect3DSurface9 *tstCrash;
720 hr = IDirectXVideoDecoderService_CreateSurface((IDirectXVideoDecoderService*) sys->d3ddec,
721 sys->surface_width,
722 sys->surface_height,
723 0,
724 p_sys->render,
725 D3DPOOL_DEFAULT,
726 0,
727 DXVA2_VideoDecoderRenderTarget,
728 &tstCrash,
729 NULL);
730 if (FAILED(hr)) {
731 msg_Err(va, "extra buffer impossible, avoid a crash (hr=0x%0lx)", hr);
732 for (int i = 0; i < sys->surface_count; i++)
733 IDirect3DSurface9_Release( (IDirect3DSurface9*) sys->hw_surface[i] );
734 sys->surface_count = 0;
735 return VLC_EGENERIC;
736 }
737 IDirect3DSurface9_Release(tstCrash);
738
739 /* */
740 DXVA2_VideoDesc dsc;
741 ZeroMemory(&dsc, sizeof(dsc));
742 dsc.SampleWidth = fmt->i_width;
743 dsc.SampleHeight = fmt->i_height;
744 dsc.Format = p_sys->render;
745 if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
746 dsc.InputSampleFreq.Numerator = fmt->i_frame_rate;
747 dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
748 } else {
749 dsc.InputSampleFreq.Numerator = 0;
750 dsc.InputSampleFreq.Denominator = 0;
751 }
752 dsc.OutputFrameFreq = dsc.InputSampleFreq;
753 dsc.UABProtectionLevel = FALSE;
754 dsc.Reserved = 0;
755
756 /* FIXME I am unsure we can let unknown everywhere */
757 DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
758 ext->SampleFormat = 0;//DXVA2_SampleUnknown;
759 ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
760 ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
761 ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
762 ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
763 ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
764 ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;
765
766 /* List all configurations available for the decoder */
767 UINT cfg_count = 0;
768 DXVA2_ConfigPictureDecode *cfg_list = NULL;
769 if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations((IDirectXVideoDecoderService*) sys->d3ddec,
770 &sys->input,
771 &dsc,
772 NULL,
773 &cfg_count,
774 &cfg_list))) {
775 msg_Err(va, "IDirectXVideoDecoderService_GetDecoderConfigurations failed");
776 for (int i = 0; i < sys->surface_count; i++)
777 IDirect3DSurface9_Release( (IDirect3DSurface9*) sys->hw_surface[i] );
778 sys->surface_count = 0;
779 return VLC_EGENERIC;
780 }
781 msg_Dbg(va, "we got %d decoder configurations", cfg_count);
782
783 /* Select the best decoder configuration */
784 int cfg_score = 0;
785 for (unsigned i = 0; i < cfg_count; i++) {
786 const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
787
788 /* */
789 msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
790 i, cfg->ConfigBitstreamRaw);
791
792 /* */
793 int score;
794 if (cfg->ConfigBitstreamRaw == 1)
795 score = 1;
796 else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
797 score = 2;
798 else
799 continue;
800 if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
801 score += 16;
802
803 if (cfg_score < score) {
804 p_sys->cfg = *cfg;
805 cfg_score = score;
806 }
807 }
808 CoTaskMemFree(cfg_list);
809 if (cfg_score <= 0) {
810 msg_Err(va, "Failed to find a supported decoder configuration");
811 return VLC_EGENERIC;
812 }
813
814 /* Create the decoder */
815 IDirectXVideoDecoder *decoder;
816 if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder((IDirectXVideoDecoderService*) sys->d3ddec,
817 &sys->input,
818 &dsc,
819 &p_sys->cfg,
820 (LPDIRECT3DSURFACE9*) sys->hw_surface,
821 sys->surface_count,
822 &decoder))) {
823 msg_Err(va, "IDirectXVideoDecoderService_CreateVideoDecoder failed");
824 for (int i = 0; i < sys->surface_count; i++)
825 IDirect3DSurface9_Release( (IDirect3DSurface9*) sys->hw_surface[i] );
826 sys->surface_count = 0;
827 return VLC_EGENERIC;
828 }
829 sys->decoder = (IUnknown*) decoder;
830
831 msg_Dbg(va, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
832 return VLC_SUCCESS;
833 }
834
DxDestroyVideoDecoder(vlc_va_t * va)835 static void DxDestroyVideoDecoder(vlc_va_t *va)
836 {
837 VLC_UNUSED(va);
838 }
839
DxResetVideoDecoder(vlc_va_t * va)840 static int DxResetVideoDecoder(vlc_va_t *va)
841 {
842 msg_Err(va, "DxResetVideoDecoder unimplemented");
843 return VLC_EGENERIC;
844 }
845
DxAllocPicture(vlc_va_t * va,const video_format_t * fmt,unsigned index)846 static picture_t *DxAllocPicture(vlc_va_t *va, const video_format_t *fmt, unsigned index)
847 {
848 video_format_t src_fmt = *fmt;
849 src_fmt.i_chroma = va->sys->i_chroma;
850 picture_sys_t *pic_sys = calloc(1, sizeof(*pic_sys));
851 if (unlikely(pic_sys == NULL))
852 return NULL;
853 pic_sys->surface = (LPDIRECT3DSURFACE9) va->sys->dx_sys.hw_surface[index];
854
855 picture_resource_t res = {
856 .p_sys = pic_sys,
857 };
858 picture_t *pic = picture_NewFromResource(&src_fmt, &res);
859 if (unlikely(pic == NULL))
860 {
861 free(pic_sys);
862 return NULL;
863 }
864 return pic;
865 }
866