dxva2.c 23.3 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
#define D3D_DecoderType     IDirectXVideoDecoder
#define D3D_DecoderDevice   IDirectXVideoDecoderService
#define D3D_DecoderSurface  IDirect3DSurface9
#include "directx_va.h"

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

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

58 59 60 61 62 63
#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)

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

67
# if !defined(__MINGW64_VERSION_MAJOR)
68 69 70
#  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
71 72
# else
#  include <dxva.h>
73
# endif
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
74

75 76 77 78 79
#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);

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

82
DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo,       0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
83 84 85 86 87 88 89


/* */
typedef struct {
    const char   *name;
    D3DFORMAT    format;
    vlc_fourcc_t codec;
Steve Lhomme's avatar
Steve Lhomme committed
90
} d3d9_format_t;
91
/* XXX Prefered format must come first */
Steve Lhomme's avatar
Steve Lhomme committed
92
static const d3d9_format_t d3d_formats[] = {
93 94
    { "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
95
    { "IMC3",   MAKEFOURCC('I','M','C','3'),    VLC_CODEC_YV12 },
96
    { "P010",   MAKEFOURCC('P','0','1','0'),    VLC_CODEC_P010 },
97 98

    { NULL, 0, 0 }
99 100
};

Steve Lhomme's avatar
Steve Lhomme committed
101
static const d3d9_format_t *D3dFindFormat(D3DFORMAT format)
102 103 104 105 106 107 108
{
    for (unsigned i = 0; d3d_formats[i].name; i++) {
        if (d3d_formats[i].format == format)
            return &d3d_formats[i];
    }
    return NULL;
}
109

110
struct vlc_va_sys_t
111
{
112
    directx_sys_t         dx_sys;
113

114
    /* Direct3D */
115 116
    d3d9_handle_t          hd3d;
    d3d9_device_t          d3d_dev;
117

118 119 120
    /* DLL */
    HINSTANCE              dxva2_dll;

121
    /* Device manager */
122
    IDirect3DDeviceManager9  *devmng;
123 124 125 126 127 128 129 130
    HANDLE                   device;

    /* Video service */
    D3DFORMAT                    render;

    /* Video decoder */
    DXVA2_ConfigPictureDecode    cfg;

131
    /* avcodec internals */
132
    struct dxva_context hw;
133
};
134

135

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

141
static int D3dCreateDeviceManager(vlc_va_t *);
142
static void D3dDestroyDeviceManager(vlc_va_t *);
143

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

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

155
void SetupAVCodecContext(vlc_va_t *va)
156
{
157
    vlc_va_sys_t *sys = va->sys;
158
    directx_sys_t *dx_sys = &sys->dx_sys;
159

160
    sys->hw.decoder = dx_sys->decoder;
161
    sys->hw.cfg = &sys->cfg;
162
    sys->hw.surface_count = dx_sys->va_pool.surface_count;
163
    sys->hw.surface = dx_sys->hw_surface;
164

165 166 167
    if (IsEqualGUID(&dx_sys->input, &DXVA_Intel_H264_NoFGT_ClearVideo))
        sys->hw.workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
}
168

169 170 171 172 173 174
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);
175
        va_surface_Release(pic_ctx->va_surface);
176 177 178 179
        free(pic_ctx);
    }
}

180
static struct va_pic_context *CreatePicContext(IDirect3DSurface9 *);
181 182 183 184

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;
185 186 187 188 189 190
    struct va_pic_context *pic_ctx = CreatePicContext(src_ctx->picsys.surface);
    if (unlikely(pic_ctx==NULL))
        return NULL;
    pic_ctx->va_surface = src_ctx->va_surface;
    va_surface_AddRef(pic_ctx->va_surface);
    return &pic_ctx->s;
191 192
}

193
static struct va_pic_context *CreatePicContext(IDirect3DSurface9 *surface)
194 195 196 197 198 199
{
    struct va_pic_context *pic_ctx = calloc(1, sizeof(*pic_ctx));
    if (unlikely(pic_ctx==NULL))
        return NULL;
    pic_ctx->s.destroy = d3d9_pic_context_destroy;
    pic_ctx->s.copy    = d3d9_pic_context_copy;
200
    pic_ctx->picsys.surface = surface;
201
    AcquirePictureSys(&pic_ctx->picsys);
202 203 204
    return pic_ctx;
}

205
static struct va_pic_context* NewSurfacePicContext(vlc_va_t *va, int surface_index)
206
{
207
    directx_sys_t *dx_sys = &va->sys->dx_sys;
208 209 210 211 212 213 214 215
    struct va_pic_context *pic_ctx = CreatePicContext(dx_sys->hw_surface[surface_index]);
    if (unlikely(pic_ctx==NULL))
        return NULL;
    /* all the resources are acquired during surfaces init, and a second time in
     * CreatePicContext(), undo one of them otherwise we need an extra release
     * when the pool is emptied */
    ReleasePictureSys(&pic_ctx->picsys);
    return pic_ctx;
216 217
}

218
static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
219
{
220 221 222 223 224
    vlc_va_sys_t *sys = va->sys;

    /* Check the device */
    HRESULT hr = IDirect3DDeviceManager9_TestDevice(sys->devmng, sys->device);
    if (hr == DXVA2_E_NEW_VIDEO_DEVICE) {
225
        msg_Warn(va, "New video device detected.");
226 227 228 229 230 231 232
        if (DxResetVideoDecoder(va))
            return VLC_EGENERIC;
    } else if (FAILED(hr)) {
        msg_Err(va, "IDirect3DDeviceManager9_TestDevice %u", (unsigned)hr);
        return VLC_EGENERIC;
    }

233
    int res = va_pool_Get(&sys->dx_sys.va_pool, pic);
234 235 236
    if (likely(res==VLC_SUCCESS))
        *data = (uint8_t*)((struct va_pic_context*)pic->context)->picsys.surface;
    return res;
237
}
238

239
static void Close(vlc_va_t *va, void **ctx)
240
{
241
    vlc_va_sys_t *sys = va->sys;
242 243
    if ( sys == NULL )
        return;
244

245
    (void) ctx;
246 247 248

    directx_va_Close(va, &sys->dx_sys);

249 250 251
    if (sys->dxva2_dll)
        FreeLibrary(sys->dxva2_dll);

252
    free((char *)va->description);
253
    free(sys);
254 255
}

256
static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
257
                const es_format_t *fmt, picture_sys_t *p_sys)
258
{
259 260 261
    int err = VLC_EGENERIC;
    directx_sys_t *dx_sys;

262 263 264
    if (pix_fmt != AV_PIX_FMT_DXVA2_VLD)
        return VLC_EGENERIC;

265 266
    ctx->hwaccel_context = NULL;

267 268 269
    vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;
270 271

    /* Load dll*/
272
    if (D3D9_Create(va, &sys->hd3d) != VLC_SUCCESS) {
273
        msg_Warn(va, "cannot load d3d9.dll");
274
        free( sys );
275 276
        goto error;
    }
277

278 279 280 281
    /* Load dll*/
    sys->dxva2_dll = LoadLibrary(TEXT("DXVA2.DLL"));
    if (!sys->dxva2_dll) {
        msg_Warn(va, "cannot load DXVA2 decoder DLL");
282 283
        D3D9_Destroy( &sys->hd3d );
        free( sys );
284 285 286
        goto error;
    }

287
    dx_sys = &sys->dx_sys;
288

289 290 291 292 293 294 295 296 297
    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;
298
    dx_sys->va_pool.pf_new_surface_context     = NewSurfacePicContext;
299 300
    dx_sys->pf_get_input_list          = DxGetInputList;
    dx_sys->pf_setup_output            = DxSetupOutput;
301

302
    va->sys = sys;
303

304
    if (p_sys!=NULL)
305 306 307 308
    {
        D3DSURFACE_DESC src;
        if (SUCCEEDED(IDirect3DSurface9_GetDesc(p_sys->surface, &src)))
            sys->render = src.Format;
309 310
        IDirect3DSurface9_GetDevice(p_sys->surface, &sys->d3d_dev.dev );
        sys->d3d_dev.owner = false;
311
    }
312

313
    err = directx_va_Open(va, &sys->dx_sys);
314
    if (err!=VLC_SUCCESS)
315
        goto error;
316

317
    err = directx_va_Setup(va, &sys->dx_sys, ctx, fmt);
318 319 320 321 322
    if (err != VLC_SUCCESS)
        goto error;

    ctx->hwaccel_context = &sys->hw;

323
    /* TODO print the hardware name/vendor for debugging purposes */
324 325
    va->description = DxDescribe(sys);
    va->get     = Get;
326
    return VLC_SUCCESS;
327 328

error:
Steve Lhomme's avatar
Steve Lhomme committed
329
    Close(va, NULL);
330
    return VLC_EGENERIC;
331 332
}
/* */
333

334 335 336
/**
 * It creates a Direct3D device usable for DXVA 2
 */
337
static int D3dCreateDevice(vlc_va_t *va)
338
{
339 340
    vlc_va_sys_t *sys = va->sys;

341
    if (sys->d3d_dev.dev) {
342 343 344 345
        msg_Dbg(va, "Reusing Direct3D9 device");
        return VLC_SUCCESS;
    }

346 347 348 349
    video_format_t fmt = { 0 };
    HRESULT hr = D3D9_CreateDevice(va, &sys->hd3d, GetDesktopWindow(), &fmt, &sys->d3d_dev);
    if (FAILED(hr))
    {
350
        msg_Err(va, "IDirect3D9_CreateDevice failed");
351 352 353 354 355
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}
356

357 358 359
/**
 * It releases a Direct3D device and its resources.
 */
360
static void D3dDestroyDevice(vlc_va_t *va)
361
{
362 363 364
    vlc_va_sys_t *sys = va->sys;
    D3D9_ReleaseDevice(&sys->d3d_dev);
    D3D9_Destroy( &sys->hd3d );
365 366 367 368
}
/**
 * It describes our Direct3D object
 */
369
static char *DxDescribe(vlc_va_sys_t *sys)
370
{
371 372 373 374 375
    D3DADAPTER_IDENTIFIER9 d3dai;
    if (FAILED(IDirect3D9_GetAdapterIdentifier(sys->hd3d.obj,
                                               sys->d3d_dev.adapterId, 0, &d3dai))) {
        return NULL;
    }
376

377
    char *description;
378
    if (asprintf(&description, "DXVA2 (%.*s, vendor %s(%lu), device %lu, revision %lu)",
379
                 (int)sizeof(d3dai.Description), d3dai.Description,
380
                 DxgiVendorStr(d3dai.VendorId), d3dai.VendorId, d3dai.DeviceId, d3dai.Revision) < 0)
381 382 383 384 385 386 387
        return NULL;
    return description;
}

/**
 * It creates a Direct3D device manager
 */
388
static int D3dCreateDeviceManager(vlc_va_t *va)
389
{
390 391
    vlc_va_sys_t *sys = va->sys;

392 393 394
    HRESULT (WINAPI *CreateDeviceManager9)(UINT *pResetToken,
                                           IDirect3DDeviceManager9 **);
    CreateDeviceManager9 =
395
      (void *)GetProcAddress(sys->dxva2_dll,
396
                             "DXVA2CreateDirect3DDeviceManager9");
397 398

    if (!CreateDeviceManager9) {
399
        msg_Err(va, "cannot load function");
400 401
        return VLC_EGENERIC;
    }
402
    msg_Dbg(va, "OurDirect3DCreateDeviceManager9 Success!");
403 404

    UINT token;
405
    IDirect3DDeviceManager9 *devmng;
406
    if (FAILED(CreateDeviceManager9(&token, &devmng))) {
407
        msg_Err(va, " OurDirect3DCreateDeviceManager9 failed");
408 409
        return VLC_EGENERIC;
    }
410
    sys->devmng = devmng;
Steve Lhomme's avatar
Steve Lhomme committed
411
    msg_Dbg(va, "obtained IDirect3DDeviceManager9");
412

413
    HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, sys->d3d_dev.dev, token);
414
    if (FAILED(hr)) {
415
        msg_Err(va, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
416 417 418 419 420 421 422
        return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
/**
 * It destroys a Direct3D device manager
 */
423
static void D3dDestroyDeviceManager(vlc_va_t *va)
424
{
425 426
    if (va->sys->devmng)
        IDirect3DDeviceManager9_Release(va->sys->devmng);
427 428 429 430 431
}

/**
 * It creates a DirectX video service
 */
432
static int DxCreateVideoService(vlc_va_t *va)
433
{
434
    vlc_va_sys_t *sys = va->sys;
435
    directx_sys_t *dx_sys = &va->sys->dx_sys;
436 437 438
    HRESULT hr;

    HANDLE device;
439
    hr = IDirect3DDeviceManager9_OpenDeviceHandle(sys->devmng, &device);
440
    if (FAILED(hr)) {
441
        msg_Err(va, "OpenDeviceHandle failed");
442 443
        return VLC_EGENERIC;
    }
444
    sys->device = device;
445

446 447 448
    void *pv;
    hr = IDirect3DDeviceManager9_GetVideoService(sys->devmng, device,
                                        &IID_IDirectXVideoDecoderService, &pv);
449
    if (FAILED(hr)) {
450
        msg_Err(va, "GetVideoService failed");
451 452
        return VLC_EGENERIC;
    }
453
    dx_sys->d3ddec = pv;
454 455 456

    return VLC_SUCCESS;
}
457

458 459 460
/**
 * It destroys a DirectX video service
 */
461
static void DxDestroyVideoService(vlc_va_t *va)
462
{
463
    directx_sys_t *dx_sys = &va->sys->dx_sys;
464
    if (va->sys->device)
465 466 467 468 469
    {
        HRESULT hr = IDirect3DDeviceManager9_CloseDeviceHandle(va->sys->devmng, va->sys->device);
        if (FAILED(hr))
            msg_Warn(va, "Failed to release device handle %x. (hr=0x%lX)", va->sys->device, hr);
    }
470 471
    if (dx_sys->d3ddec)
        IDirectXVideoDecoderService_Release(dx_sys->d3ddec);
472
}
473

474
static void ReleaseInputList(input_list_t *p_list)
475
{
476
    CoTaskMemFree(p_list->list);
477 478
}

479
static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
480
{
481
    directx_sys_t *dx_sys = &va->sys->dx_sys;
482 483
    UINT input_count = 0;
    GUID *input_list = NULL;
484
    if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(dx_sys->d3ddec,
485 486
                                                                 &input_count,
                                                                 &input_list))) {
487
        msg_Err(va, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
488 489
        return VLC_EGENERIC;
    }
490 491 492 493 494 495 496

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

497
static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
498
{
499
    VLC_UNUSED(fmt);
500 501 502
    int err = VLC_EGENERIC;
    UINT      output_count = 0;
    D3DFORMAT *output_list = NULL;
503
    if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(va->sys->dx_sys.d3ddec,
504 505 506 507 508 509 510 511 512
                                                                   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];
Steve Lhomme's avatar
Steve Lhomme committed
513
        const d3d9_format_t *format = D3dFindFormat(f);
514 515
        if (format) {
            msg_Dbg(va, "%s is supported for output", format->name);
516
        } else {
517
            msg_Dbg(va, "%d is supported for output (%4.4s)", f, (const char*)&f);
518
        }
519 520
    }

521
    /* */
522 523 524
    for (unsigned pass = 0; pass < 2 && err != VLC_SUCCESS; ++pass)
    {
        for (unsigned j = 0; d3d_formats[j].name; j++) {
Steve Lhomme's avatar
Steve Lhomme committed
525
            const d3d9_format_t *format = &d3d_formats[j];
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

            /* */
            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;
542
        }
543
    }
544 545
    CoTaskMemFree(output_list);
    return err;
546 547
}

548 549 550
/**
 * It creates a DXVA2 decoder using the given video format
 */
551 552
static int DxCreateVideoDecoder(vlc_va_t *va, int codec_id,
                                const video_format_t *fmt, unsigned surface_count)
553
{
554 555
    vlc_va_sys_t *p_sys = va->sys;
    directx_sys_t *sys = &va->sys->dx_sys;
556
    HRESULT hr;
557

558
    hr = IDirectXVideoDecoderService_CreateSurface(sys->d3ddec,
559
                                                         fmt->i_width,
560
                                                         fmt->i_height,
561
                                                         surface_count - 1,
562
                                                         p_sys->render,
563 564 565
                                                         D3DPOOL_DEFAULT,
                                                         0,
                                                         DXVA2_VideoDecoderRenderTarget,
566
                                                         sys->hw_surface,
567 568
                                                         NULL);
    if (FAILED(hr)) {
569
        msg_Err(va, "IDirectXVideoAccelerationService_CreateSurface %d failed (hr=0x%0lx)", surface_count - 1, hr);
570 571
        return VLC_EGENERIC;
    }
572
    msg_Dbg(va, "IDirectXVideoAccelerationService_CreateSurface succeed with %d surfaces (%dx%d)",
573
            surface_count, fmt->i_width, fmt->i_height);
574

575
    IDirect3DSurface9 *tstCrash;
576
    hr = IDirectXVideoDecoderService_CreateSurface(sys->d3ddec,
577
                                                         fmt->i_width,
578
                                                         fmt->i_height,
579 580 581 582 583 584 585 586 587
                                                         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);
588
        for (unsigned i = 0; i < surface_count; i++)
589
            IDirect3DSurface9_Release( sys->hw_surface[i] );
590 591 592 593
        return VLC_EGENERIC;
    }
    IDirect3DSurface9_Release(tstCrash);

594 595 596 597 598
    /* */
    DXVA2_VideoDesc dsc;
    ZeroMemory(&dsc, sizeof(dsc));
    dsc.SampleWidth     = fmt->i_width;
    dsc.SampleHeight    = fmt->i_height;
599
    dsc.Format          = p_sys->render;
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    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;
624
    if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(sys->d3ddec,
625
                                                                    &sys->input,
626 627 628 629
                                                                    &dsc,
                                                                    NULL,
                                                                    &cfg_count,
                                                                    &cfg_list))) {
630
        msg_Err(va, "IDirectXVideoDecoderService_GetDecoderConfigurations failed");
631
        for (unsigned i = 0; i < surface_count; i++)
632
            IDirect3DSurface9_Release( sys->hw_surface[i] );
633 634
        return VLC_EGENERIC;
    }
635
    msg_Dbg(va, "we got %d decoder configurations", cfg_count);
636 637

    /* Select the best decoder configuration */
638
    int cfg_score = 0;
639 640 641 642
    for (unsigned i = 0; i < cfg_count; i++) {
        const DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];

        /* */
643
        msg_Dbg(va, "configuration[%d] ConfigBitstreamRaw %d",
644 645 646
                i, cfg->ConfigBitstreamRaw);

        /* */
647 648 649
        int score;
        if (cfg->ConfigBitstreamRaw == 1)
            score = 1;
650
        else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
651 652 653
            score = 2;
        else
            continue;
654
        if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
655 656 657
            score += 16;

        if (cfg_score < score) {
658
            p_sys->cfg = *cfg;
659
            cfg_score = score;
660 661 662
        }
    }
    CoTaskMemFree(cfg_list);
663
    if (cfg_score <= 0) {
664
        msg_Err(va, "Failed to find a supported decoder configuration");
665 666
        return VLC_EGENERIC;
    }
667

668
    /* Create the decoder */
669
    IDirectXVideoDecoder *decoder;
670
    if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(sys->d3ddec,
671
                                                              &sys->input,
672
                                                              &dsc,
673
                                                              &p_sys->cfg,
674
                                                              sys->hw_surface,
675
                                                              surface_count,
676
                                                              &decoder))) {
677
        msg_Err(va, "IDirectXVideoDecoderService_CreateVideoDecoder failed");
678
        for (unsigned i = 0; i < surface_count; i++)
679
            IDirect3DSurface9_Release( sys->hw_surface[i] );
680 681
        return VLC_EGENERIC;
    }
682
    sys->decoder = decoder;
683

684
    msg_Dbg(va, "IDirectXVideoDecoderService_CreateVideoDecoder succeed");
685 686
    return VLC_SUCCESS;
}
687

688
static void DxDestroyVideoDecoder(vlc_va_t *va)
689
{
690 691 692 693 694
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    if (dx_sys->decoder)
    {
        IDirectXVideoDecoder_Release(dx_sys->decoder);
        dx_sys->decoder = NULL;
695 696
        for (unsigned i = 0; i < dx_sys->va_pool.surface_count; i++)
            IDirect3DSurface9_Release(dx_sys->hw_surface[i]);
697
    }
698
}
699 700

static int DxResetVideoDecoder(vlc_va_t *va)
701
{
702
    msg_Err(va, "DxResetVideoDecoder unimplemented");
703
    return VLC_EGENERIC;
704
}