dxva2.c 26.4 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * dxva2.c: Video Acceleration helpers
3 4
 *****************************************************************************
 * Copyright (C) 2009 Geoffroy Couprie
5
 * Copyright (C) 2009 Laurent Aimar
6 7 8
 * $Id$
 *
 * Authors: Geoffroy Couprie <geal@videolan.org>
9
 *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
10
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12 13
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
14 15 16 17
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

30 31
#include <assert.h>

32
#include <vlc_common.h>
33
#include <vlc_picture.h>
34
#include <vlc_plugin.h>
35

36 37 38
#define DXVA2API_USE_BITFIELDS
#define COBJMACROS
#include <libavcodec/dxva2.h>
39
#include "../../video_chroma/d3d9_fmt.h"
40

41 42 43 44 45 46
#define D3D_Device          IDirect3DDevice9
#define D3D_DecoderType     IDirectXVideoDecoder
#define D3D_DecoderDevice   IDirectXVideoDecoderService
#define D3D_DecoderSurface  IDirect3DSurface9
#include "directx_va.h"

47
static int Open(vlc_va_t *, AVCodecContext *, enum PixelFormat,
48
                const es_format_t *, picture_sys_t *p_sys);
49
static void Close(vlc_va_t *, void **);
50 51 52

vlc_module_begin()
    set_description(N_("DirectX Video Acceleration (DXVA) 2.0"))
53
    set_capability("hw decoder", 0)
54 55 56 57 58
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_VCODEC)
    set_callbacks(Open, Close)
vlc_module_end()

59 60 61 62 63 64
#include <initguid.h> /* must be last included to not redefine existing GUIDs */

/* dxva2api.h GUIDs: http://msdn.microsoft.com/en-us/library/windows/desktop/ms697067(v=vs100).aspx
 * assume that they are declared in dxva2api.h */
#define MS_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)

65 66
#ifdef __MINGW32__
# include <_mingw.h>
67

68
# if !defined(__MINGW64_VERSION_MAJOR)
69 70 71
#  undef MS_GUID
#  define MS_GUID DEFINE_GUID /* dxva2api.h fails to declare those, redefine as static */
#  define DXVA2_E_NEW_VIDEO_DEVICE MAKE_HRESULT(1, 4, 4097)
Rafaël Carré's avatar
Rafaël Carré committed
72 73
# else
#  include <dxva.h>
74
# endif
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
75

76 77 78 79 80
#endif /* __MINGW32__ */

MS_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
MS_GUID(IID_IDirectXVideoAccelerationService, 0xfc51a550, 0xd5e7, 0x11d9, 0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);

81
DEFINE_GUID(DXVA2_NoEncrypt,                        0x1b81bed0, 0xa0c7, 0x11d3, 0xb9, 0x84, 0x00, 0xc0, 0x4f, 0x2e, 0x73, 0xc5);
82

83
DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo,       0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
84 85 86 87 88 89 90 91 92 93 94 95


/* */
typedef struct {
    const char   *name;
    D3DFORMAT    format;
    vlc_fourcc_t codec;
} d3d_format_t;
/* XXX Prefered format must come first */
static const d3d_format_t d3d_formats[] = {
    { "YV12",   MAKEFOURCC('Y','V','1','2'),    VLC_CODEC_YV12 },
    { "NV12",   MAKEFOURCC('N','V','1','2'),    VLC_CODEC_NV12 },
Rafaël Carré's avatar
Rafaël Carré committed
96
    { "IMC3",   MAKEFOURCC('I','M','C','3'),    VLC_CODEC_YV12 },
97
    { "P010",   MAKEFOURCC('P','0','1','0'),    VLC_CODEC_P010 },
98 99

    { NULL, 0, 0 }
100 101
};

102 103 104 105 106 107 108 109
static const d3d_format_t *D3dFindFormat(D3DFORMAT format)
{
    for (unsigned i = 0; d3d_formats[i].name; i++) {
        if (d3d_formats[i].format == format)
            return &d3d_formats[i];
    }
    return NULL;
}
110

111
struct vlc_va_sys_t
112
{
113
    directx_sys_t         dx_sys;
114
    vlc_fourcc_t          i_chroma;
115

116
    /* DLL */
117
    HINSTANCE             hd3d9_dll;
118 119

    /* Direct3D */
120 121
    LPDIRECT3D9            d3dobj;
    D3DADAPTER_IDENTIFIER9 d3dai;
122 123

    /* Device manager */
124
    IDirect3DDeviceManager9  *devmng;
125 126 127 128 129 130 131 132
    HANDLE                   device;

    /* Video service */
    D3DFORMAT                    render;

    /* Video decoder */
    DXVA2_ConfigPictureDecode    cfg;

133
    /* avcodec internals */
134
    struct dxva_context hw;
135
};
136

137

138
/* */
139
static int D3dCreateDevice(vlc_va_t *);
140
static void D3dDestroyDevice(vlc_va_t *);
141
static char *DxDescribe(vlc_va_sys_t *);
142

143
static int D3dCreateDeviceManager(vlc_va_t *);
144
static void D3dDestroyDeviceManager(vlc_va_t *);
145

146
static int DxCreateVideoService(vlc_va_t *);
147 148
static void DxDestroyVideoService(vlc_va_t *);
static int DxGetInputList(vlc_va_t *, input_list_t *);
149
static int DxSetupOutput(vlc_va_t *, const GUID *, const video_format_t *);
150

151 152
static int DxCreateVideoDecoder(vlc_va_t *, int codec_id,
                                const video_format_t *, unsigned surface_count);
153
static void DxDestroyVideoDecoder(vlc_va_t *);
154
static int DxResetVideoDecoder(vlc_va_t *);
155
static void SetupAVCodecContext(vlc_va_t *);
156 157

/* */
158
static void Setup(vlc_va_t *va, vlc_fourcc_t *chroma)
159
{
160
    *chroma = va->sys->i_chroma;
161 162
}

163
void SetupAVCodecContext(vlc_va_t *va)
164
{
165
    vlc_va_sys_t *sys = va->sys;
166
    directx_sys_t *dx_sys = &sys->dx_sys;
167

168
    sys->hw.decoder = dx_sys->decoder;
169
    sys->hw.cfg = &sys->cfg;
170 171
    sys->hw.surface_count = dx_sys->va_pool.surface_count;
    sys->hw.surface = dx_sys->va_pool.hw_surface;
172

173 174 175
    if (IsEqualGUID(&dx_sys->input, &DXVA_Intel_H264_NoFGT_ClearVideo))
        sys->hw.workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
}
176

177 178
static int Extract(vlc_va_t *va, picture_t *picture, uint8_t *data)
{
179 180
    VLC_UNUSED(va); VLC_UNUSED(data);
    struct va_pic_context *pic_ctx = (struct va_pic_context*)picture->context;
181
    va_surface_AddRef(pic_ctx->va_surface);
182 183
    return VLC_SUCCESS;
}
184

185
static int CheckDevice(vlc_va_t *va)
186
{
187
    vlc_va_sys_t *sys = va->sys;
188 189

    /* Check the device */
190
    HRESULT hr = IDirect3DDeviceManager9_TestDevice(sys->devmng, sys->device);
191 192 193 194
    if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
        if (DxResetVideoDecoder(va))
            return VLC_EGENERIC;
    } else if (FAILED(hr)) {
195
        msg_Err(va, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
196 197
        return VLC_EGENERIC;
    }
198 199
    return VLC_SUCCESS;
}
200

201 202 203 204 205 206
static void d3d9_pic_context_destroy(struct picture_context_t *opaque)
{
    struct va_pic_context *pic_ctx = (struct va_pic_context*)opaque;
    if (pic_ctx->va_surface)
    {
        ReleasePictureSys(&pic_ctx->picsys);
207
        va_surface_Release(pic_ctx->va_surface);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        free(pic_ctx);
    }
}

static struct picture_context_t *CreatePicContext(vlc_va_surface_t *);

static struct picture_context_t *d3d9_pic_context_copy(struct picture_context_t *ctx)
{
    struct va_pic_context *src_ctx = (struct va_pic_context*)ctx;
    return CreatePicContext(src_ctx->va_surface);
}

static struct picture_context_t *CreatePicContext(vlc_va_surface_t *va_surface)
{
    struct va_pic_context *pic_ctx = calloc(1, sizeof(*pic_ctx));
    if (unlikely(pic_ctx==NULL))
        return NULL;
    pic_ctx->va_surface = va_surface;
226
    va_surface_AddRef(pic_ctx->va_surface);
227 228 229
    pic_ctx->s.destroy = d3d9_pic_context_destroy;
    pic_ctx->s.copy    = d3d9_pic_context_copy;
    pic_ctx->picsys.surface = va_surface->decoderSurface;
230
    AcquirePictureSys(&pic_ctx->picsys);
231 232 233
    return &pic_ctx->s;
}

234
static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
235
{
236
    vlc_va_surface_t *va_surface = va_pool_Get(va, &va->sys->dx_sys.va_pool);
237 238
    if (unlikely(va_surface==NULL))
        return VLC_EGENERIC;
239
    pic->context = CreatePicContext(va_surface);
240
    va_surface_Release(va_surface);
241 242
    if (unlikely(pic->context==NULL))
        return VLC_EGENERIC;
243 244
    *data = (uint8_t*)va_surface->decoderSurface;
    return VLC_SUCCESS;
245
}
246

247
static void Close(vlc_va_t *va, void **ctx)
248
{
249
    vlc_va_sys_t *sys = va->sys;
250

251
    (void) ctx;
252 253 254

    directx_va_Close(va, &sys->dx_sys);

255 256
    if (sys->hd3d9_dll)
        FreeLibrary(sys->hd3d9_dll);
257

258
    free((char *)va->description);
259
    free(sys);
260 261
}

Steve Lhomme's avatar
Steve Lhomme committed
262
static vlc_fourcc_t d3d9va_fourcc(enum PixelFormat swfmt)
263 264 265 266 267 268 269 270 271 272 273 274 275
{
    switch (swfmt)
    {
        case AV_PIX_FMT_YUV420P10LE:
            return VLC_CODEC_D3D9_OPAQUE_10B;
        case AV_PIX_FMT_YUVJ420P:
        case AV_PIX_FMT_YUV420P:
            return VLC_CODEC_D3D9_OPAQUE;
        default:
            return VLC_CODEC_D3D9_OPAQUE;
    }
}

276 277 278 279
static void ReleasePic(void *opaque, uint8_t *data)
{
    (void)data;
    picture_t *pic = opaque;
280
    struct va_pic_context *pic_ctx = (struct va_pic_context*)pic->context;
281
    va_surface_Release(pic_ctx->va_surface);
282 283 284
    picture_Release(pic);
}

285
static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
286
                const es_format_t *fmt, picture_sys_t *p_sys)
287
{
288 289 290
    int err = VLC_EGENERIC;
    directx_sys_t *dx_sys;

291 292 293
    if (pix_fmt != AV_PIX_FMT_DXVA2_VLD)
        return VLC_EGENERIC;

294 295 296
    vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;
297 298

    /* Load dll*/
299 300 301
    sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
    if (!sys->hd3d9_dll) {
        msg_Warn(va, "cannot load d3d9.dll");
302 303
        goto error;
    }
304

305
    dx_sys = &sys->dx_sys;
306

307 308 309 310 311 312 313 314 315 316
    dx_sys->va_pool.pf_check_device            = CheckDevice;
    dx_sys->va_pool.pf_create_device           = D3dCreateDevice;
    dx_sys->va_pool.pf_destroy_device          = D3dDestroyDevice;
    dx_sys->va_pool.pf_create_device_manager   = D3dCreateDeviceManager;
    dx_sys->va_pool.pf_destroy_device_manager  = D3dDestroyDeviceManager;
    dx_sys->va_pool.pf_create_video_service    = DxCreateVideoService;
    dx_sys->va_pool.pf_destroy_video_service   = DxDestroyVideoService;
    dx_sys->va_pool.pf_create_decoder_surfaces = DxCreateVideoDecoder;
    dx_sys->va_pool.pf_destroy_surfaces        = DxDestroyVideoDecoder;
    dx_sys->va_pool.pf_setup_avcodec_ctx       = SetupAVCodecContext;
317 318 319
    dx_sys->pf_get_input_list          = DxGetInputList;
    dx_sys->pf_setup_output            = DxSetupOutput;
    dx_sys->psz_decoder_dll            = TEXT("DXVA2.DLL");
320

321
    va->sys = sys;
322

323 324
    dx_sys->d3ddev = NULL;
    if (p_sys!=NULL)
325 326 327 328
    {
        D3DSURFACE_DESC src;
        if (SUCCEEDED(IDirect3DSurface9_GetDesc(p_sys->surface, &src)))
            sys->render = src.Format;
329
        IDirect3DSurface9_GetDevice(p_sys->surface, &dx_sys->d3ddev );
330
    }
331

332 333
    sys->i_chroma = d3d9va_fourcc(ctx->sw_pix_fmt);

334
    err = directx_va_Open(va, &sys->dx_sys, ctx, fmt, true);
335
    if (err!=VLC_SUCCESS)
336
        goto error;
337

338 339 340 341 342 343
    err = directx_va_Setup(va, &sys->dx_sys, ctx);
    if (err != VLC_SUCCESS)
        goto error;

    ctx->hwaccel_context = &sys->hw;

344
    /* TODO print the hardware name/vendor for debugging purposes */
345 346 347
    va->description = DxDescribe(sys);
    va->setup   = Setup;
    va->get     = Get;
348
    va->release = ReleasePic;
349
    va->extract = Extract;
350
    return VLC_SUCCESS;
351 352

error:
Steve Lhomme's avatar
Steve Lhomme committed
353
    Close(va, NULL);
354
    return VLC_EGENERIC;
355 356
}
/* */
357

358 359 360
/**
 * It creates a Direct3D device usable for DXVA 2
 */
361
static int D3dCreateDevice(vlc_va_t *va)
362
{
363 364
    vlc_va_sys_t *sys = va->sys;

365 366 367 368 369 370
    if (sys->dx_sys.d3ddev) {
        msg_Dbg(va, "Reusing Direct3D9 device");
        IDirect3DDevice9_AddRef(sys->dx_sys.d3ddev);
        return VLC_SUCCESS;
    }

371 372
    /* */
    LPDIRECT3D9 (WINAPI *Create9)(UINT SDKVersion);
373
    Create9 = (void *)GetProcAddress(sys->hd3d9_dll, "Direct3DCreate9");
374
    if (!Create9) {
375
        msg_Err(va, "Cannot locate reference to Direct3DCreate9 ABI in DLL");
376
        return VLC_EGENERIC;
377 378
    }

379 380 381 382
    /* */
    LPDIRECT3D9 d3dobj;
    d3dobj = Create9(D3D_SDK_VERSION);
    if (!d3dobj) {
383
        msg_Err(va, "Direct3DCreate9 failed");
384
        return VLC_EGENERIC;
385
    }
386
    sys->d3dobj = d3dobj;
387

388
    /* */
389 390
    D3DADAPTER_IDENTIFIER9 *d3dai = &sys->d3dai;
    if (FAILED(IDirect3D9_GetAdapterIdentifier(sys->d3dobj,
391
                                               D3DADAPTER_DEFAULT, 0, d3dai))) {
392
        msg_Warn(va, "IDirect3D9_GetAdapterIdentifier failed");
393 394 395
        ZeroMemory(d3dai, sizeof(*d3dai));
    }

396
    /* */
397 398 399 400 401 402 403 404 405 406 407 408 409
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Flags                  = D3DPRESENTFLAG_VIDEO;
    d3dpp.Windowed               = TRUE;
    d3dpp.hDeviceWindow          = NULL;
    d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    d3dpp.MultiSampleType        = D3DMULTISAMPLE_NONE;
    d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
    d3dpp.BackBufferCount        = 0;                  /* FIXME what to put here */
    d3dpp.BackBufferFormat       = D3DFMT_X8R8G8B8;    /* FIXME what to put here */
    d3dpp.BackBufferWidth        = 0;
    d3dpp.BackBufferHeight       = 0;
    d3dpp.EnableAutoDepthStencil = FALSE;
410

411 412
    /* Direct3D needs a HWND to create a device, even without using ::Present
    this HWND is used to alert Direct3D when there's a change of focus window.
413
    For now, use GetDesktopWindow, as it looks harmless */
414 415
    LPDIRECT3DDEVICE9 d3ddev;
    if (FAILED(IDirect3D9_CreateDevice(d3dobj, D3DADAPTER_DEFAULT,
416
                                       D3DDEVTYPE_HAL, GetDesktopWindow(),
417 418
                                       D3DCREATE_SOFTWARE_VERTEXPROCESSING |
                                       D3DCREATE_MULTITHREADED,
419
                                       &d3dpp, &d3ddev))) {
420
        msg_Err(va, "IDirect3D9_CreateDevice failed");
421 422
        return VLC_EGENERIC;
    }
423
    sys->dx_sys.d3ddev = d3ddev;
424 425 426

    return VLC_SUCCESS;
}
427

428 429 430
/**
 * It releases a Direct3D device and its resources.
 */
431
static void D3dDestroyDevice(vlc_va_t *va)
432
{
433
    directx_sys_t *dx_sys = &va->sys->dx_sys;
434 435
    if (va->sys->d3dobj)
        IDirect3D9_Release(va->sys->d3dobj);
436 437
    if (dx_sys->d3ddev)
        IDirect3DDevice9_Release(dx_sys->d3ddev);
438 439 440 441
}
/**
 * It describes our Direct3D object
 */
442
static char *DxDescribe(vlc_va_sys_t *va)
443
{
444 445 446 447 448 449
    static const struct {
        unsigned id;
        char     name[32];
    } vendors [] = {
        { 0x1002, "ATI" },
        { 0x10DE, "NVIDIA" },
450
        { 0x1106, "VIA" },
451 452 453 454
        { 0x8086, "Intel" },
        { 0x5333, "S3 Graphics" },
        { 0, "" }
    };
455
    D3DADAPTER_IDENTIFIER9 *id = &va->d3dai;
456 457 458 459 460 461 462 463 464

    const char *vendor = "Unknown";
    for (int i = 0; vendors[i].id != 0; i++) {
        if (vendors[i].id == id->VendorId) {
            vendor = vendors[i].name;
            break;
        }
    }

465
    char *description;
Rafaël Carré's avatar
Rafaël Carré committed
466
    if (asprintf(&description, "DXVA2 (%.*s, vendor %lu(%s), device %lu, revision %lu)",
467
                 (int)sizeof(id->Description), id->Description,
468
                 id->VendorId, vendor, id->DeviceId, id->Revision) < 0)
469 470 471 472 473 474 475
        return NULL;
    return description;
}

/**
 * It creates a Direct3D device manager
 */
476
static int D3dCreateDeviceManager(vlc_va_t *va)
477
{
478
    vlc_va_sys_t *sys = va->sys;
479
    directx_sys_t *dx_sys = &va->sys->dx_sys;
480

481 482 483
    HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
                                           IDirect3DDeviceManager9 **);
    CreateDeviceManager9 =
484
      (void *)GetProcAddress(dx_sys->hdecoder_dll,
485
                             "DXVA2CreateDirect3DDeviceManager9");
486 487

    if (!CreateDeviceManager9) {
488
        msg_Err(va, "cannot load function");
489 490
        return VLC_EGENERIC;
    }
491
    msg_Dbg(va, "OurDirect3DCreateDeviceManager9 Success!");
492 493

    UINT token;
494
    IDirect3DDeviceManager9 *devmng;
495
    if (FAILED(CreateDeviceManager9(&token, &devmng))) {
496
        msg_Err(va, " OurDirect3DCreateDeviceManager9 failed");
497 498
        return VLC_EGENERIC;
    }
499 500
    sys->devmng = devmng;
    msg_Info(va, "obtained IDirect3DDeviceManager9");
501

502
    HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, dx_sys->d3ddev, token);
503
    if (FAILED(hr)) {
504
        msg_Err(va, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
505 506 507 508 509 510 511
        return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
/**
 * It destroys a Direct3D device manager
 */
512
static void D3dDestroyDeviceManager(vlc_va_t *va)
513
{
514 515
    if (va->sys->devmng)
        IDirect3DDeviceManager9_Release(va->sys->devmng);
516 517 518 519 520
}

/**
 * It creates a DirectX video service
 */
521
static int DxCreateVideoService(vlc_va_t *va)
522
{
523
    vlc_va_sys_t *sys = va->sys;
524
    directx_sys_t *dx_sys = &va->sys->dx_sys;
525

526 527 528 529
    HRESULT (WINAPI *CreateVideoService)(IDirect3DDevice9 *,
                                         REFIID riid,
                                         void **ppService);
    CreateVideoService =
530
      (void *)GetProcAddress(dx_sys->hdecoder_dll, "DXVA2CreateVideoService");
531 532

    if (!CreateVideoService) {
533
        msg_Err(va, "cannot load function");
534 535
        return 4;
    }
536
    msg_Info(va, "DXVA2CreateVideoService Success!");
537 538 539 540

    HRESULT hr;

    HANDLE device;
541
    hr = IDirect3DDeviceManager9_OpenDeviceHandle(sys->devmng, &device);
542
    if (FAILED(hr)) {
543
        msg_Err(va, "OpenDeviceHandle failed");
544 545
        return VLC_EGENERIC;
    }
546
    sys->device = device;
547

548 549 550
    void *pv;
    hr = IDirect3DDeviceManager9_GetVideoService(sys->devmng, device,
                                        &IID_IDirectXVideoDecoderService, &pv);
551
    if (FAILED(hr)) {
552
        msg_Err(va, "GetVideoService failed");
553 554
        return VLC_EGENERIC;
    }
555
    dx_sys->d3ddec = pv;
556 557 558

    return VLC_SUCCESS;
}
559

560 561 562
/**
 * It destroys a DirectX video service
 */
563
static void DxDestroyVideoService(vlc_va_t *va)
564
{
565
    directx_sys_t *dx_sys = &va->sys->dx_sys;
566 567
    if (va->sys->device)
        IDirect3DDeviceManager9_CloseDeviceHandle(va->sys->devmng, va->sys->device);
568 569
    if (dx_sys->d3ddec)
        IDirectXVideoDecoderService_Release(dx_sys->d3ddec);
570
}
571

572
static void ReleaseInputList(input_list_t *p_list)
573
{
574
    CoTaskMemFree(p_list->list);
575 576
}

577
static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
578
{
579
    directx_sys_t *dx_sys = &va->sys->dx_sys;
580 581
    UINT input_count = 0;
    GUID *input_list = NULL;
582
    if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(dx_sys->d3ddec,
583 584
                                                                 &input_count,
                                                                 &input_list))) {
585
        msg_Err(va, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
586 587
        return VLC_EGENERIC;
    }
588 589 590 591 592 593 594

    p_list->count = input_count;
    p_list->list = input_list;
    p_list->pf_release = ReleaseInputList;
    return VLC_SUCCESS;
}

595
static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
596
{
597
    VLC_UNUSED(fmt);
598 599 600
    int err = VLC_EGENERIC;
    UINT      output_count = 0;
    D3DFORMAT *output_list = NULL;
601
    if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->sys->dx_sys.d3ddec,
602 603 604 605 606 607 608 609 610 611 612 613
                                                                   input,
                                                                   &output_count,
                                                                   &output_list))) {
        msg_Err(va, "IDirectXVideoDecoderService_GetDecoderRenderTargets failed");
        return VLC_EGENERIC;
    }

    for (unsigned j = 0; j < output_count; j++) {
        const D3DFORMAT f = output_list[j];
        const d3d_format_t *format = D3dFindFormat(f);
        if (format) {
            msg_Dbg(va, "%s is supported for output", format->name);
614
        } else {
615
            msg_Dbg(va, "%d is supported for output (%4.4s)", f, (const char*)&f);
616
        }
617 618
    }

619
    /* */
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    for (unsigned pass = 0; pass < 2 && err != VLC_SUCCESS; ++pass)
    {
        for (unsigned j = 0; d3d_formats[j].name; j++) {
            const d3d_format_t *format = &d3d_formats[j];

            /* */
            bool is_supported = false;
            for (unsigned k = 0; !is_supported && k < output_count; k++) {
                is_supported = format->format == output_list[k];
            }
            if (!is_supported)
                continue;
            if (pass == 0 && format->format != va->sys->render)
                continue;

            /* We have our solution */
            msg_Dbg(va, "Using decoder output '%s'", format->name);
            va->sys->render = format->format;
            err = VLC_SUCCESS;
            break;
640
        }
641
    }
642 643
    CoTaskMemFree(output_list);
    return err;
644 645
}

646 647 648
/**
 * It creates a DXVA2 decoder using the given video format
 */
649 650
static int DxCreateVideoDecoder(vlc_va_t *va, int codec_id,
                                const video_format_t *fmt, unsigned surface_count)
651
{
652 653
    vlc_va_sys_t *p_sys = va->sys;
    directx_sys_t *sys = &va->sys->dx_sys;
654
    HRESULT hr;
655

656
    hr = IDirectXVideoDecoderService_CreateSurface(sys->d3ddec,
657 658
                                                         sys->va_pool.surface_width,
                                                         sys->va_pool.surface_height,
659
                                                         surface_count - 1,
660
                                                         p_sys->render,
661 662 663
                                                         D3DPOOL_DEFAULT,
                                                         0,
                                                         DXVA2_VideoDecoderRenderTarget,
664
                                                         sys->va_pool.hw_surface,
665 666
                                                         NULL);
    if (FAILED(hr)) {
667
        msg_Err(va, "IDirectXVideoAccelerationService_CreateSurface %d failed (hr=0x%0lx)", surface_count - 1, hr);
668 669
        return VLC_EGENERIC;
    }
670
    msg_Dbg(va, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
671
            surface_count, sys->va_pool.surface_width, sys->va_pool.surface_height);
672

673
    IDirect3DSurface9 *tstCrash;
674
    hr = IDirectXVideoDecoderService_CreateSurface(sys->d3ddec,
675 676
                                                         sys->va_pool.surface_width,
                                                         sys->va_pool.surface_height,
677 678 679 680 681 682 683 684 685
                                                         0,
                                                         p_sys->render,
                                                         D3DPOOL_DEFAULT,
                                                         0,
                                                         DXVA2_VideoDecoderRenderTarget,
                                                         &tstCrash,
                                                         NULL);
    if (FAILED(hr)) {
        msg_Err(va, "extra buffer impossible, avoid a crash (hr=0x%0lx)", hr);
686
        for (unsigned i = 0; i < surface_count; i++)
687
            IDirect3DSurface9_Release( sys->va_pool.hw_surface[i] );
688 689 690 691
        return VLC_EGENERIC;
    }
    IDirect3DSurface9_Release(tstCrash);

692 693 694 695 696
    /* */
    DXVA2_VideoDesc dsc;
    ZeroMemory(&dsc, sizeof(dsc));
    dsc.SampleWidth     = fmt->i_width;
    dsc.SampleHeight    = fmt->i_height;
697
    dsc.Format          = p_sys->render;
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    if (fmt->i_frame_rate > 0 && fmt->i_frame_rate_base > 0) {
        dsc.InputSampleFreq.Numerator   = fmt->i_frame_rate;
        dsc.InputSampleFreq.Denominator = fmt->i_frame_rate_base;
    } else {
        dsc.InputSampleFreq.Numerator   = 0;
        dsc.InputSampleFreq.Denominator = 0;
    }
    dsc.OutputFrameFreq = dsc.InputSampleFreq;
    dsc.UABProtectionLevel = FALSE;
    dsc.Reserved = 0;

    /* FIXME I am unsure we can let unknown everywhere */
    DXVA2_ExtendedFormat *ext = &dsc.SampleFormat;
    ext->SampleFormat = 0;//DXVA2_SampleUnknown;
    ext->VideoChromaSubsampling = 0;//DXVA2_VideoChromaSubsampling_Unknown;
    ext->NominalRange = 0;//DXVA2_NominalRange_Unknown;
    ext->VideoTransferMatrix = 0;//DXVA2_VideoTransferMatrix_Unknown;
    ext->VideoLighting = 0;//DXVA2_VideoLighting_Unknown;
    ext->VideoPrimaries = 0;//DXVA2_VideoPrimaries_Unknown;
    ext->VideoTransferFunction = 0;//DXVA2_VideoTransFunc_Unknown;

    /* List all configurations available for the decoder */
    UINT                      cfg_count = 0;
    DXVA2_ConfigPictureDecode *cfg_list = NULL;
722
    if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(sys->d3ddec,
723
                                                                    &sys->input,
724 725 726 727
                                                                    &dsc,
                                                                    NULL,
                                                                    &cfg_count,
                                                                    &cfg_list))) {
728
        msg_Err(va, "IDirectXVideoDecoderService_GetDecoderConfigurations failed");
729
        for (unsigned i = 0; i < surface_count; i++)
730
            IDirect3DSurface9_Release( sys->va_pool.hw_surface[i] );
731 732
        return VLC_EGENERIC;
    }
733
    msg_Dbg(va, "we got %d decoder configurations", cfg_count);
734 735

    /* Select the best decoder configuration */
736
    int cfg_score = 0;
737 738 739 740
    for (unsigned i = 0; i < cfg_count; i++) {
        const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];

        /* */
741
        msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
742 743 744
                i, cfg->ConfigBitstreamRaw);

        /* */
745 746 747
        int score;
        if (cfg->ConfigBitstreamRaw == 1)
            score = 1;
748
        else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
749 750 751
            score = 2;
        else
            continue;
752
        if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
753 754 755
            score += 16;

        if (cfg_score < score) {
756
            p_sys->cfg = *cfg;
757
            cfg_score = score;
758 759 760
        }
    }
    CoTaskMemFree(cfg_list);
761
    if (cfg_score <= 0) {
762
        msg_Err(va, "Failed to find a supported decoder configuration");
763 764
        return VLC_EGENERIC;
    }
765

766
    /* Create the decoder */
767
    IDirectXVideoDecoder *decoder;
768
    if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(sys->d3ddec,
769
                                                              &sys->input,
770
                                                              &dsc,
771
                                                              &p_sys->cfg,
772
                                                              sys->va_pool.hw_surface,
773
                                                              surface_count,
774
                                                              &decoder))) {
775
        msg_Err(va, "IDirectXVideoDecoderService_CreateVideoDecoder failed");
776
        for (unsigned i = 0; i < surface_count; i++)
777
            IDirect3DSurface9_Release( sys->va_pool.hw_surface[i] );
778 779
        return VLC_EGENERIC;
    }
780
    sys->decoder = decoder;
781

782
    msg_Dbg(va, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
783 784
    return VLC_SUCCESS;
}
785

786
static void DxDestroyVideoDecoder(vlc_va_t *va)
787
{
788 789 790 791 792 793
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    if (dx_sys->decoder)
    {
        IDirectXVideoDecoder_Release(dx_sys->decoder);
        dx_sys->decoder = NULL;
    }
794
}
795 796

static int DxResetVideoDecoder(vlc_va_t *va)
797
{
798
    msg_Err(va, "DxResetVideoDecoder unimplemented");
799
    return VLC_EGENERIC;
800
}