d3d11va.c 35.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*****************************************************************************
 * d3d11va.c: Direct3D11 Video Acceleration decoder
 *****************************************************************************
 * Copyright © 2009 Geoffroy Couprie
 * Copyright © 2009 Laurent Aimar
 * Copyright © 2015 Steve Lhomme
 * Copyright © 2015 VideoLabs
 *
 * Authors: Geoffroy Couprie <geal@videolan.org>
 *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *          Steve Lhomme <robux4@gmail.com>
 *
 * 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
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/

/**
  * See https://msdn.microsoft.com/en-us/library/windows/desktop/hh162912%28v=vs.85%29.aspx
  **/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

Steve Lhomme's avatar
Steve Lhomme committed
35 36 37
# undef WINAPI_FAMILY
# define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP

38 39 40 41 42 43
#include <assert.h>

#include <vlc_common.h>
#include <vlc_picture.h>
#include <vlc_plugin.h>
#include <vlc_charset.h>
44
#include <vlc_codec.h>
45 46

#define COBJMACROS
47
#include <initguid.h>
48
#include <d3d11.h>
49 50
#include <libavcodec/d3d11va.h>

51
#include "../../video_chroma/d3d11_fmt.h"
52

53 54 55 56 57 58
#define D3D_Device          ID3D11Device
#define D3D_DecoderType     ID3D11VideoDecoder
#define D3D_DecoderDevice   ID3D11VideoDevice
#define D3D_DecoderSurface  ID3D11VideoDecoderOutputView
#include "directx_va.h"

59 60
static int Open(vlc_va_t *, AVCodecContext *, enum PixelFormat,
                const es_format_t *, picture_sys_t *p_sys);
61
static void Close(vlc_va_t *, void **);
62 63 64 65 66 67 68 69 70

vlc_module_begin()
    set_description(N_("Direct3D11 Video Acceleration"))
    set_capability("hw decoder", 0)
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_VCODEC)
    set_callbacks(Open, Close)
vlc_module_end()

Steve Lhomme's avatar
Steve Lhomme committed
71 72 73 74
#if VLC_WINSTORE_APP
#define pf_CreateDevice                 D3D11CreateDevice
#endif

75 76 77 78 79 80 81
/*
 * In this mode libavcodec doesn't need the whole array on texture on startup
 * So we get the surfaces from the decoder pool when needed. We don't need to
 * extract the decoded surface into the decoder picture anymore.
 */
#define D3D11_DIRECT_DECODE  LIBAVCODEC_VERSION_CHECK( 57, 30, 3, 72, 101 )

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#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)

#ifdef __MINGW32__
# include <_mingw.h>

# if !defined(__MINGW64_VERSION_MAJOR)
#  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)
# else
#  include <dxva.h>
# endif

#endif /* __MINGW32__ */

101 102
#if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
# include <dxgidebug.h>
103 104 105 106
#endif

DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo,       0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);

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

109 110 111
struct vlc_va_sys_t
{
    directx_sys_t                dx_sys;
112
    vlc_fourcc_t                 i_chroma;
113
    UINT                         totalTextureSlices;
114

115
#if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
116 117 118 119 120 121 122 123
    HINSTANCE                    dxgidebug_dll;
#endif

    /* Video service */
    ID3D11VideoContext           *d3dvidctx;
    DXGI_FORMAT                  render;

    ID3D11DeviceContext          *d3dctx;
124
    HANDLE                       context_mutex;
125

126 127 128 129
    /* pool */
    bool                         b_extern_pool;
    picture_t                    *extern_pics[MAX_SURFACE_COUNT];

130 131 132 133 134
    /* Video decoder */
    D3D11_VIDEO_DECODER_CONFIG   cfg;

    /* avcodec internals */
    struct AVD3D11VAContext      hw;
135 136

    ID3D11ShaderResourceView     *resourceView[MAX_SURFACE_COUNT * D3D11_MAX_SHADER_VIEW];
137 138 139 140 141 142 143 144 145 146
};

/* */
static int D3dCreateDevice(vlc_va_t *);
static void D3dDestroyDevice(vlc_va_t *);
static char *DxDescribe(directx_sys_t *);

static int DxCreateVideoService(vlc_va_t *);
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 DxCreateDecoderSurfaces(vlc_va_t *, int codec_id,
                                   const video_format_t *fmt, unsigned surface_count);
151 152 153 154
static void DxDestroySurfaces(vlc_va_t *);
static void SetupAVCodecContext(vlc_va_t *);

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

void SetupAVCodecContext(vlc_va_t *va)
{
    vlc_va_sys_t *sys = va->sys;
    directx_sys_t *dx_sys = &sys->dx_sys;

    sys->hw.video_context = sys->d3dvidctx;
166
    sys->hw.decoder = dx_sys->decoder;
167
    sys->hw.cfg = &sys->cfg;
168 169
    sys->hw.surface_count = dx_sys->va_pool.surface_count;
    sys->hw.surface = dx_sys->va_pool.hw_surface;
170
    sys->hw.context_mutex = sys->context_mutex;
171 172 173 174 175 176 177

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

static int Extract(vlc_va_t *va, picture_t *output, uint8_t *data)
{
178
    VLC_UNUSED(va); VLC_UNUSED(data);
179 180
    struct va_pic_context *pic_ctx = (struct va_pic_context*)output->context;
    if (!va->sys->b_extern_pool)
181
        va_surface_AddRef(pic_ctx->va_surface);
182
    assert(data == (void*)pic_ctx->picsys.decoder);
183
    return VLC_SUCCESS;
184 185
}

186 187 188 189
static void d3d11_pic_context_destroy(struct picture_context_t *opaque)
{
    struct va_pic_context *pic_ctx = (struct va_pic_context*)opaque;
    if (pic_ctx->va_surface)
190
        va_surface_Release(pic_ctx->va_surface);
191 192 193 194
    ReleasePictureSys(&pic_ctx->picsys);
    free(pic_ctx);
}

Steve Lhomme's avatar
Steve Lhomme committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
static struct va_pic_context *CreatePicContext(vlc_va_surface_t *,
                                               ID3D11VideoDecoderOutputView *,
                                               ID3D11Resource *,
                                               ID3D11DeviceContext *,
                                               UINT slice,
                                               ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW]);

static struct picture_context_t *d3d11_pic_context_copy(struct picture_context_t *ctx)
{
    struct va_pic_context *src_ctx = (struct va_pic_context*)ctx;
    struct va_pic_context *pic_ctx = CreatePicContext(src_ctx->va_surface, src_ctx->picsys.decoder,
                                                      src_ctx->picsys.resource[0], src_ctx->picsys.context,
                                                      src_ctx->picsys.slice_index, src_ctx->picsys.resourceView);
    if (unlikely(pic_ctx==NULL))
        return NULL;
    return &pic_ctx->s;
}

213 214 215 216 217 218 219 220 221 222 223
static struct va_pic_context *CreatePicContext(vlc_va_surface_t *va_surface,
                                                  ID3D11VideoDecoderOutputView *decoderSurface,
                                                  ID3D11Resource *p_resource,
                                                  ID3D11DeviceContext *context,
                                                  UINT slice,
                                                  ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW])
{
    struct va_pic_context *pic_ctx = calloc(1, sizeof(*pic_ctx));
    if (unlikely(pic_ctx==NULL))
        goto done;
    pic_ctx->s.destroy = d3d11_pic_context_destroy;
Steve Lhomme's avatar
Steve Lhomme committed
224
    pic_ctx->s.copy    = d3d11_pic_context_copy;
225 226 227 228 229 230 231 232 233
    pic_ctx->va_surface = va_surface;
    pic_ctx->picsys.context = context;
    pic_ctx->picsys.slice_index = slice;
    pic_ctx->picsys.decoder = decoderSurface;
    for (int i=0;i<D3D11_MAX_SHADER_VIEW; i++)
    {
        pic_ctx->picsys.resource[i] = p_resource;
        pic_ctx->picsys.resourceView[i] = resourceView[i];
    }
234
    AcquirePictureSys(&pic_ctx->picsys);
235 236 237 238 239
    pic_ctx->picsys.context = context;
done:
    return pic_ctx;
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
static picture_context_t* NewSurfacePicContext(vlc_va_t *va, vlc_va_surface_t *va_surface, ID3D11VideoDecoderOutputView *surface)
{
    ID3D11ShaderResourceView *resourceView[D3D11_MAX_SHADER_VIEW];
    ID3D11Resource *p_resource;
    ID3D11VideoDecoderOutputView_GetResource(surface, &p_resource);

    D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
    ID3D11VideoDecoderOutputView_GetDesc(surface, &viewDesc);

    for (int i=0; i<D3D11_MAX_SHADER_VIEW; i++)
        resourceView[i] = va->sys->resourceView[viewDesc.Texture2D.ArraySlice*D3D11_MAX_SHADER_VIEW + i];

    struct va_pic_context *pic_ctx = CreatePicContext(va_surface,
                                                  surface,
                                                  p_resource,
                                                  va->sys->d3dctx,
                                                  viewDesc.Texture2D.ArraySlice,
                                                  resourceView);
    ID3D11Resource_Release(p_resource);
    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->s;
}

268 269
static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
{
270
#if D3D11_DIRECT_DECODE
271
    if (va->sys->b_extern_pool)
272
    {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        /* copy the original picture_sys_t in the va_pic_context */
        if (!pic->context)
        {
            assert(pic->p_sys!=NULL);
            if (!pic->p_sys->decoder)
            {
                HRESULT hr;
                D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
                ZeroMemory(&viewDesc, sizeof(viewDesc));
                viewDesc.DecodeProfile = va->sys->dx_sys.input;
                viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
                viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;

                hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( va->sys->dx_sys.d3ddec,
                                                                     pic->p_sys->resource[KNOWN_DXGI_INDEX],
                                                                     &viewDesc,
                                                                     &pic->p_sys->decoder );
                if (FAILED(hr))
                    return VLC_EGENERIC;
            }
293

294 295 296 297 298 299 300 301 302 303 304 305
            pic->context = (picture_context_t*)CreatePicContext( NULL,
                                             pic->p_sys->decoder,
                                             pic->p_sys->resource[KNOWN_DXGI_INDEX],
                                             va->sys->d3dctx,
                                             pic->p_sys->slice_index,
                                             pic->p_sys->resourceView );
            if (pic->context == NULL)
                return VLC_EGENERIC;
        }
    }
    else
#endif
306
    {
307 308 309
        int res = va_pool_Get(va, pic, &va->sys->dx_sys.va_pool);
        if (unlikely(res != VLC_SUCCESS))
            return res;
310
    }
311
    *data = (uint8_t*)((struct va_pic_context *)pic->context)->picsys.decoder;
312
    return VLC_SUCCESS;
313 314
}

315
static void Close(vlc_va_t *va, void **ctx)
316 317 318 319 320 321 322
{
    vlc_va_sys_t *sys = va->sys;

    (void) ctx;

    directx_va_Close(va, &sys->dx_sys);

323
#if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
324 325 326 327 328 329 330 331
    if (sys->dxgidebug_dll)
        FreeLibrary(sys->dxgidebug_dll);
#endif

    free((char *)va->description);
    free(sys);
}

332
static vlc_fourcc_t d3d11va_fourcc(enum PixelFormat swfmt)
333 334 335 336 337 338 339 340 341 342
{
    switch (swfmt)
    {
        case AV_PIX_FMT_YUV420P10LE:
            return VLC_CODEC_D3D11_OPAQUE_10B;
        default:
            return VLC_CODEC_D3D11_OPAQUE;
    }
}

343 344 345 346
static void ReleasePic(void *opaque, uint8_t *data)
{
    (void)data;
    picture_t *pic = opaque;
347
    struct va_pic_context *pic_ctx = (struct va_pic_context*)pic->context;
348
    va_surface_Release(pic_ctx->va_surface);
349 350
    picture_Release(pic);
}
351

352 353 354 355 356 357
static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
                const es_format_t *fmt, picture_sys_t *p_sys)
{
    int err = VLC_EGENERIC;
    directx_sys_t *dx_sys;

358 359
    ctx->hwaccel_context = NULL;

360 361 362 363 364 365 366
    if (pix_fmt != AV_PIX_FMT_D3D11VA_VLD)
        return VLC_EGENERIC;

    vlc_va_sys_t *sys = calloc(1, sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

367
#if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
368 369 370 371 372
    sys->dxgidebug_dll = LoadLibrary(TEXT("DXGIDEBUG.DLL"));
#endif

    dx_sys = &sys->dx_sys;

373 374 375 376 377 378 379
    dx_sys->va_pool.pf_create_device           = D3dCreateDevice;
    dx_sys->va_pool.pf_destroy_device          = D3dDestroyDevice;
    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 = DxCreateDecoderSurfaces;
    dx_sys->va_pool.pf_destroy_surfaces        = DxDestroySurfaces;
    dx_sys->va_pool.pf_setup_avcodec_ctx       = SetupAVCodecContext;
380
    dx_sys->va_pool.pf_new_surface_context     = NewSurfacePicContext;
381 382 383 384 385 386 387 388 389
    dx_sys->pf_get_input_list          = DxGetInputList;
    dx_sys->pf_setup_output            = DxSetupOutput;
    dx_sys->psz_decoder_dll            = TEXT("D3D11.DLL");

    va->sys = sys;

    dx_sys->d3ddev = NULL;
    va->sys->render = DXGI_FORMAT_UNKNOWN;
    if ( p_sys != NULL && p_sys->context != NULL ) {
390 391
        void *d3dvidctx = NULL;
        HRESULT hr = ID3D11DeviceContext_QueryInterface(p_sys->context, &IID_ID3D11VideoContext, &d3dvidctx);
392
        if (FAILED(hr)) {
Steve Lhomme's avatar
Steve Lhomme committed
393
           msg_Err(va, "Could not Query ID3D11VideoContext Interface from the picture. (hr=0x%lX)", hr);
394
        } else {
395
            ID3D11DeviceContext_GetDevice( p_sys->context, &dx_sys->d3ddev );
396 397
            HANDLE context_lock = INVALID_HANDLE_VALUE;
            UINT dataSize = sizeof(context_lock);
398
            hr = ID3D11Device_GetPrivateData(dx_sys->d3ddev, &GUID_CONTEXT_MUTEX, &dataSize, &context_lock);
399
            if (FAILED(hr))
400
                msg_Warn(va, "No mutex found to lock the decoder");
401
            sys->context_mutex = context_lock;
402

403 404 405
            sys->d3dctx = p_sys->context;
            sys->d3dvidctx = d3dvidctx;

406
            assert(p_sys->texture[KNOWN_DXGI_INDEX] != NULL);
407
            D3D11_TEXTURE2D_DESC dstDesc;
408
            ID3D11Texture2D_GetDesc( p_sys->texture[KNOWN_DXGI_INDEX], &dstDesc);
409
            sys->render = dstDesc.Format;
410
            va->sys->totalTextureSlices = dstDesc.ArraySize;
411 412 413
        }
    }

414 415
    sys->i_chroma = d3d11va_fourcc(ctx->sw_pix_fmt);

Steve Lhomme's avatar
Steve Lhomme committed
416
#if VLC_WINSTORE_APP
417
    err = directx_va_Open(va, &sys->dx_sys, false);
Steve Lhomme's avatar
Steve Lhomme committed
418
#else
419
    err = directx_va_Open(va, &sys->dx_sys, dx_sys->d3ddev == NULL || va->sys->d3dctx == NULL);
Steve Lhomme's avatar
Steve Lhomme committed
420
#endif
421 422 423
    if (err!=VLC_SUCCESS)
        goto error;

424
    err = directx_va_Setup(va, &sys->dx_sys, ctx, fmt);
425 426 427 428 429
    if (err != VLC_SUCCESS)
        goto error;

    ctx->hwaccel_context = &sys->hw;

430 431 432 433
    /* TODO print the hardware name/vendor for debugging purposes */
    va->description = DxDescribe(dx_sys);
    va->setup   = Setup;
    va->get     = Get;
434
#if D3D11_DIRECT_DECODE
435
    va->release = sys->b_extern_pool ? NULL : ReleasePic;
436
#else
437
    va->release = ReleasePic;
438
#endif
439 440 441 442 443
    va->extract = Extract;

    return VLC_SUCCESS;

error:
Steve Lhomme's avatar
Steve Lhomme committed
444
    Close(va, NULL);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    return err;
}

/**
 * It creates a Direct3D device usable for decoding
 */
static int D3dCreateDevice(vlc_va_t *va)
{
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    HRESULT hr;

    if (dx_sys->d3ddev && va->sys->d3dctx) {
        msg_Dbg(va, "Reusing Direct3D11 device");
        ID3D11DeviceContext_AddRef(va->sys->d3dctx);
        return VLC_SUCCESS;
    }

Steve Lhomme's avatar
Steve Lhomme committed
462
#if !VLC_WINSTORE_APP
463 464 465 466 467 468 469
    /* */
    PFN_D3D11_CREATE_DEVICE pf_CreateDevice;
    pf_CreateDevice = (void *)GetProcAddress(dx_sys->hdecoder_dll, "D3D11CreateDevice");
    if (!pf_CreateDevice) {
        msg_Err(va, "Cannot locate reference to D3D11CreateDevice ABI in DLL");
        return VLC_EGENERIC;
    }
Steve Lhomme's avatar
Steve Lhomme committed
470
#endif
471 472

    UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
473
#if !defined(NDEBUG) //&& defined(_MSC_VER)
474
    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
475
#endif
476 477 478 479 480 481 482 483 484 485 486

    /* */
    ID3D11Device *d3ddev;
    ID3D11DeviceContext *d3dctx;
    hr = pf_CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
                                 creationFlags, NULL, 0,
                                 D3D11_SDK_VERSION, &d3ddev, NULL, &d3dctx);
    if (FAILED(hr)) {
        msg_Err(va, "D3D11CreateDevice failed. (hr=0x%lX)", hr);
        return VLC_EGENERIC;
    }
487
    dx_sys->d3ddev = d3ddev;
488 489
    va->sys->d3dctx = d3dctx;

490 491
    void *d3dvidctx = NULL;
    hr = ID3D11DeviceContext_QueryInterface(d3dctx, &IID_ID3D11VideoContext, &d3dvidctx);
492
    if (FAILED(hr)) {
Steve Lhomme's avatar
Steve Lhomme committed
493
       msg_Err(va, "Could not Query ID3D11VideoContext Interface. (hr=0x%lX)", hr);
494 495 496 497
       return VLC_EGENERIC;
    }
    va->sys->d3dvidctx = d3dvidctx;

498
#if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H)
499
    HRESULT (WINAPI  * pf_DXGIGetDebugInterface)(const GUID *riid, void **ppDebug);
500 501
    if (va->sys->dxgidebug_dll) {
        pf_DXGIGetDebugInterface = (void *)GetProcAddress(va->sys->dxgidebug_dll, "DXGIGetDebugInterface");
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        if (pf_DXGIGetDebugInterface) {
            IDXGIDebug *pDXGIDebug = NULL;
            hr = pf_DXGIGetDebugInterface(&IID_IDXGIDebug, (void**)&pDXGIDebug);
            if (SUCCEEDED(hr) && pDXGIDebug) {
                hr = IDXGIDebug_ReportLiveObjects(pDXGIDebug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
            }
        }
    }
#endif

    return VLC_SUCCESS;
}

/**
 * It releases a Direct3D device and its resources.
 */
static void D3dDestroyDevice(vlc_va_t *va)
{
520
    directx_sys_t *dx_sys = &va->sys->dx_sys;
521 522 523 524
    if (va->sys->d3dvidctx)
        ID3D11VideoContext_Release(va->sys->d3dvidctx);
    if (va->sys->d3dctx)
        ID3D11DeviceContext_Release(va->sys->d3dctx);
525 526
    if (dx_sys->d3ddev)
        ID3D11Device_Release(dx_sys->d3ddev);
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
}
/**
 * It describes our Direct3D object
 */
static char *DxDescribe(directx_sys_t *dx_sys)
{
    static const struct {
        unsigned id;
        char     name[32];
    } vendors [] = {
        { 0x1002, "ATI" },
        { 0x10DE, "NVIDIA" },
        { 0x1106, "VIA" },
        { 0x8086, "Intel" },
        { 0x5333, "S3 Graphics" },
542
        { 0x4D4F4351, "Qualcomm" },
543 544 545
        { 0, "" }
    };

546
    IDXGIAdapter *p_adapter = D3D11DeviceAdapter(dx_sys->d3ddev);
547
    if (!p_adapter) {
548 549 550
       return NULL;
    }

551
    char *description = NULL;
552 553 554 555 556 557 558 559 560 561
    DXGI_ADAPTER_DESC adapterDesc;
    if (SUCCEEDED(IDXGIAdapter_GetDesc(p_adapter, &adapterDesc))) {
        const char *vendor = "Unknown";
        for (int i = 0; vendors[i].id != 0; i++) {
            if (vendors[i].id == adapterDesc.VendorId) {
                vendor = vendors[i].name;
                break;
            }
        }

562 563 564 565 566 567 568 569 570
        char *utfdesc = FromWide(adapterDesc.Description);
        if (likely(utfdesc!=NULL))
        {
            if (asprintf(&description, "D3D11VA (%s, vendor %u(%s), device %u, revision %u)",
                         utfdesc,
                         adapterDesc.VendorId, vendor, adapterDesc.DeviceId, adapterDesc.Revision) < 0)
                description = NULL;
            free(utfdesc);
        }
571 572 573
    }

    IDXGIAdapter_Release(p_adapter);
574
    return description;
575 576 577 578 579 580 581 582 583
}

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

584 585
    void *d3dviddev = NULL;
    HRESULT hr = ID3D11Device_QueryInterface(dx_sys->d3ddev, &IID_ID3D11VideoDevice, &d3dviddev);
586 587 588 589
    if (FAILED(hr)) {
       msg_Err(va, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }
590
    dx_sys->d3ddec = d3dviddev;
591 592 593 594 595 596 597 598 599

    return VLC_SUCCESS;
}

/**
 * It destroys a DirectX video service
 */
static void DxDestroyVideoService(vlc_va_t *va)
{
600 601 602
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    if (dx_sys->d3ddec)
        ID3D11VideoDevice_Release(dx_sys->d3ddec);
603 604 605 606 607 608 609 610 611 612 613 614
}

static void ReleaseInputList(input_list_t *p_list)
{
    free(p_list->list);
}

static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
{
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    HRESULT hr;

615
    UINT input_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(dx_sys->d3ddec);
616 617 618 619 620 621 622 623 624

    p_list->count = input_count;
    p_list->list = calloc(input_count, sizeof(*p_list->list));
    if (unlikely(p_list->list == NULL)) {
        return VLC_ENOMEM;
    }
    p_list->pf_release = ReleaseInputList;

    for (unsigned i = 0; i < input_count; i++) {
625
        hr = ID3D11VideoDevice_GetVideoDecoderProfile(dx_sys->d3ddec, i, &p_list->list[i]);
626 627 628 629 630 631 632 633 634 635 636
        if (FAILED(hr))
        {
            msg_Err(va, "GetVideoDecoderProfile %d failed. (hr=0x%lX)", i, hr);
            ReleaseInputList(p_list);
            return VLC_EGENERIC;
        }
    }

    return VLC_SUCCESS;
}

637
static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
638 639 640 641
{
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    HRESULT hr;

642 643 644
#ifndef NDEBUG
    BOOL bSupported = false;
    for (int format = 0; format < 188; format++) {
645
        hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, format, &bSupported);
646 647 648 649
        if (SUCCEEDED(hr) && bSupported)
            msg_Dbg(va, "format %s is supported for output", DxgiFormatToStr(format));
    }
#endif
650

651 652
    DXGI_FORMAT processorInput[4];
    int idx = 0;
653
    if ( va->sys->render != DXGI_FORMAT_UNKNOWN )
654 655
        processorInput[idx++] = va->sys->render;
    processorInput[idx++] = DXGI_FORMAT_NV12;
656
    processorInput[idx++] = DXGI_FORMAT_420_OPAQUE;
657 658
    processorInput[idx++] = DXGI_FORMAT_UNKNOWN;

659 660
    char *psz_decoder_name = directx_va_GetDecoderName(input);

661 662
    /* */
    for (idx = 0; processorInput[idx] != DXGI_FORMAT_UNKNOWN; ++idx)
663
    {
664
        BOOL is_supported = false;
665
        hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, processorInput[idx], &is_supported);
666
        if (SUCCEEDED(hr) && is_supported)
667
            msg_Dbg(va, "%s output is supported for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
668
        else
669
        {
670
            msg_Dbg(va, "Can't get a decoder output format %s for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
671
            continue;
672
        }
673

674 675
       // check if we can create render texture of that format
       // check the decoder can output to that format
676
       if ( !DeviceSupportsFormat(dx_sys->d3ddev, processorInput[idx],
677 678
                                  D3D11_FORMAT_SUPPORT_SHADER_LOAD) )
       {
679 680 681
#ifndef ID3D11VideoContext_VideoProcessorBlt
           msg_Dbg(va, "Format %s needs a processor but is not supported",
                   DxgiFormatToStr(processorInput[idx]));
682
#else
683 684 685 686 687 688 689
           if ( !DeviceSupportsFormat(dx_sys->d3ddev, processorInput[idx],
                                      D3D11_FORMAT_SUPPORT_VIDEO_PROCESSOR_OUTPUT) )
           {
               msg_Dbg(va, "Format %s needs a processor but is not available",
                       DxgiFormatToStr(processorInput[idx]));
               continue;
           }
690
#endif
691
        }
692

693 694 695 696 697 698 699 700
        D3D11_VIDEO_DECODER_DESC decoderDesc;
        ZeroMemory(&decoderDesc, sizeof(decoderDesc));
        decoderDesc.Guid = *input;
        decoderDesc.SampleWidth = fmt->i_width;
        decoderDesc.SampleHeight = fmt->i_height;
        decoderDesc.OutputFormat = processorInput[idx];

        UINT cfg_count = 0;
701
        hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
702 703 704 705 706 707 708 709 710 711 712 713
        if (FAILED(hr))
        {
            msg_Err( va, "Failed to get configuration for decoder %s. (hr=0x%lX)", psz_decoder_name, hr );
            continue;
        }
        if (cfg_count == 0) {
            msg_Err( va, "No decoder configuration possible for %s %dx%d",
                     DxgiFormatToStr(decoderDesc.OutputFormat),
                     decoderDesc.SampleWidth, decoderDesc.SampleHeight );
            continue;
        }

714
        msg_Dbg(va, "Using output format %s for decoder %s", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
715 716 717 718 719 720 721 722 723 724
        if ( va->sys->render == processorInput[idx] )
        {
            /* NVIDIA cards crash when calling CreateVideoDecoderOutputView
             * on more than 30 slices */
            if (va->sys->totalTextureSlices <= 30 || !isNvidiaHardware(dx_sys->d3ddev))
                va->sys->b_extern_pool = true;
            else
                msg_Warn( va, "NVIDIA GPU with too many slices (%d) detected, use internal pool",
                          va->sys->totalTextureSlices );
        }
725 726 727
        va->sys->render = processorInput[idx];
        free(psz_decoder_name);
        return VLC_SUCCESS;
728
    }
729
    free(psz_decoder_name);
730 731

    msg_Dbg(va, "Output format from picture source not supported.");
732 733 734 735 736 737
    return VLC_EGENERIC;
}

/**
 * It creates a Direct3D11 decoder using the given video format
 */
738 739
static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id,
                                   const video_format_t *fmt, unsigned surface_count)
740 741 742 743 744 745
{
    vlc_va_sys_t *sys = va->sys;
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    HRESULT hr;

    ID3D10Multithread *pMultithread;
746
    hr = ID3D11Device_QueryInterface( dx_sys->d3ddev, &IID_ID3D10Multithread, (void **)&pMultithread);
747
    if (SUCCEEDED(hr)) {
748
        ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
749 750 751
        ID3D10Multithread_Release(pMultithread);
    }

752 753 754 755 756
#if VLC_WINSTORE_APP
    /* On the Xbox 1/S, any decoding of H264 with one dimension over 2304
     * crashes totally the device */
    if (codec_id == AV_CODEC_ID_H264 &&
        (dx_sys->surface_width > 2304 || dx_sys->surface_height > 2304) &&
757
        isXboxHardware(dx_sys->d3ddev))
758 759 760 761 762 763
    {
        msg_Warn(va, "%dx%d resolution not supported by your hardware", dx_sys->surface_width, dx_sys->surface_height);
        return VLC_EGENERIC;
    }
#endif

764 765 766 767 768
    D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
    ZeroMemory(&viewDesc, sizeof(viewDesc));
    viewDesc.DecodeProfile = dx_sys->input;
    viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;

769 770 771 772 773 774 775 776 777 778 779
    const d3d_format_t *textureFmt = NULL;
    for (const d3d_format_t *output_format = GetRenderFormatList();
         output_format->name != NULL; ++output_format)
    {
        if (output_format->formatTexture == sys->render &&
            (output_format->fourcc == VLC_CODEC_D3D11_OPAQUE || output_format->fourcc == VLC_CODEC_D3D11_OPAQUE_10B))
        {
            textureFmt = output_format;
            break;
        }
    }
780 781 782 783 784
    if (unlikely(textureFmt==NULL))
    {
        msg_Dbg(va, "no hardware decoder matching %s", DxgiFormatToStr(sys->render));
        return VLC_EGENERIC;
    }
785

786 787
    if (sys->b_extern_pool)
    {
788
#if !D3D11_DIRECT_DECODE
789
        size_t surface_idx;
790
        for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
791 792
            picture_t *pic = decoder_NewPicture( (decoder_t*) va->obj.parent );
            sys->extern_pics[surface_idx] = pic;
793
            dx_sys->va_pool.hw_surface[surface_idx] = NULL;
794 795
            if (pic==NULL)
            {
796
                msg_Warn(va, "not enough decoder pictures %d out of %d", surface_idx, surface_count);
797 798 799
                sys->b_extern_pool = false;
                break;
            }
800

801
            D3D11_TEXTURE2D_DESC texDesc;
802
            ID3D11Texture2D_GetDesc(pic->p_sys->texture[KNOWN_DXGI_INDEX], &texDesc);
803
            if (texDesc.ArraySize < surface_count)
804 805
            {
                msg_Warn(va, "not enough decoding slices in the texture (%d/%d)",
806
                         texDesc.ArraySize, surface_count);
807 808 809 810 811 812
                sys->b_extern_pool = false;
                break;
            }
            assert(texDesc.Format == sys->render);
            assert(texDesc.BindFlags & D3D11_BIND_DECODER);

813
#if !LIBAVCODEC_VERSION_CHECK( 57, 27, 2, 61, 102 )
814 815 816 817 818 819 820
            if (pic->p_sys->slice_index != surface_idx)
            {
                msg_Warn(va, "d3d11va requires decoding slices to be the first in the texture (%d/%d)",
                         pic->p_sys->slice_index, surface_idx);
                sys->b_extern_pool = false;
                break;
            }
821
#endif
822 823

            viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
824
            hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
825
                                                                 pic->p_sys->resource[KNOWN_DXGI_INDEX],
826 827 828 829 830 831 832
                                                                 &viewDesc,
                                                                 &pic->p_sys->decoder );
            if (FAILED(hr)) {
                msg_Warn(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
                sys->b_extern_pool = false;
                break;
            }
833

834
            AllocateShaderView(VLC_OBJECT(va), dx_sys->d3ddev, textureFmt, pic->p_sys->texture, pic->p_sys->slice_index, pic->p_sys->resourceView);
835

836
            dx_sys->va_pool.hw_surface[surface_idx] = pic->p_sys->decoder;
837 838 839 840 841 842
        }

        if (!sys->b_extern_pool)
        {
            for (size_t i = 0; i < surface_idx; ++i)
            {
843
                if (dx_sys->va_pool.hw_surface[i])
844
                {
845 846
                    ID3D11VideoDecoderOutputView_Release(dx_sys->va_pool.hw_surface[i]);
                    dx_sys->va_pool.hw_surface[i] = NULL;
847 848 849 850 851 852 853 854 855 856
                }
                if (sys->extern_pics[i])
                {
                    sys->extern_pics[i]->p_sys->decoder = NULL;
                    picture_Release(sys->extern_pics[i]);
                    sys->extern_pics[i] = NULL;
                }
            }
        }
        else
857
#endif
858 859 860 861 862 863 864
            msg_Dbg(va, "using external surface pool");
    }

    if (!sys->b_extern_pool)
    {
        D3D11_TEXTURE2D_DESC texDesc;
        ZeroMemory(&texDesc, sizeof(texDesc));
865 866
        texDesc.Width = fmt->i_width;
        texDesc.Height = fmt->i_height;
867 868 869 870
        texDesc.MipLevels = 1;
        texDesc.Format = sys->render;
        texDesc.SampleDesc.Count = 1;
        texDesc.MiscFlags = 0;
871
        texDesc.ArraySize = surface_count;
872 873 874 875
        texDesc.Usage = D3D11_USAGE_DEFAULT;
        texDesc.BindFlags = D3D11_BIND_DECODER;
        texDesc.CPUAccessFlags = 0;

876 877 878
        if (DeviceSupportsFormat(dx_sys->d3ddev, texDesc.Format, D3D11_FORMAT_SUPPORT_SHADER_LOAD))
            texDesc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;

879
        ID3D11Texture2D *p_texture;
880
        hr = ID3D11Device_CreateTexture2D( dx_sys->d3ddev, &texDesc, NULL, &p_texture );
881
        if (FAILED(hr)) {
882
            msg_Err(va, "CreateTexture2D %d failed. (hr=0x%0lx)", surface_count, hr);
883 884
            return VLC_EGENERIC;
        }
885

886 887 888 889
        unsigned surface_idx;
        for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
            sys->extern_pics[surface_idx] = NULL;
            viewDesc.Texture2D.ArraySlice = surface_idx;
890

891
            hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
892 893
                                                                 (ID3D11Resource*) p_texture,
                                                                 &viewDesc,
894
                                                                 &dx_sys->va_pool.hw_surface[surface_idx] );
895
            if (FAILED(hr)) {
896
                msg_Err(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
897 898 899
                ID3D11Texture2D_Release(p_texture);
                return VLC_EGENERIC;
            }
900 901

            if (texDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
902
            {
903
                ID3D11Texture2D *textures[D3D11_MAX_SHADER_VIEW] = {p_texture, p_texture};
904 905
                AllocateShaderView(VLC_OBJECT(va), dx_sys->d3ddev, textureFmt, textures, surface_idx,
                                   &sys->resourceView[surface_idx * D3D11_MAX_SHADER_VIEW]);
906
            }
907
        }
908 909
    }
    msg_Dbg(va, "ID3D11VideoDecoderOutputView succeed with %d surfaces (%dx%d)",
910
            surface_count, fmt->i_width, fmt->i_height);
911 912 913 914 915 916 917 918 919

    D3D11_VIDEO_DECODER_DESC decoderDesc;
    ZeroMemory(&decoderDesc, sizeof(decoderDesc));
    decoderDesc.Guid = dx_sys->input;
    decoderDesc.SampleWidth = fmt->i_width;
    decoderDesc.SampleHeight = fmt->i_height;
    decoderDesc.OutputFormat = sys->render;

    UINT cfg_count;
920
    hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
921 922 923 924 925 926 927 928
    if (FAILED(hr)) {
        msg_Err(va, "GetVideoDecoderConfigCount failed. (hr=0x%lX)", hr);
        return VLC_EGENERIC;
    }

    /* List all configurations available for the decoder */
    D3D11_VIDEO_DECODER_CONFIG cfg_list[cfg_count];
    for (unsigned i = 0; i < cfg_count; i++) {
929
        hr = ID3D11VideoDevice_GetVideoDecoderConfig( dx_sys->d3ddec, &decoderDesc, i, &cfg_list[i] );
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
        if (FAILED(hr)) {
            msg_Err(va, "GetVideoDecoderConfig failed. (hr=0x%lX)", hr);
            return VLC_EGENERIC;
        }
    }

    msg_Dbg(va, "we got %d decoder configurations", cfg_count);

    /* Select the best decoder configuration */
    int cfg_score = 0;
    for (unsigned i = 0; i < cfg_count; i++) {
        const D3D11_VIDEO_DECODER_CONFIG *cfg = &cfg_list[i];

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

        /* */
        int score;
        if (cfg->ConfigBitstreamRaw == 1)
            score = 1;
        else if (codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
            score = 2;
        else
            continue;
955
        if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
956 957 958 959 960 961 962 963 964 965 966 967 968 969
            score += 16;

        if (cfg_score < score) {
            sys->cfg = *cfg;
            cfg_score = score;
        }
    }
    if (cfg_score <= 0) {
        msg_Err(va, "Failed to find a supported decoder configuration");
        return VLC_EGENERIC;
    }

    /* Create the decoder */
    ID3D11VideoDecoder *decoder;
970
    hr = ID3D11VideoDevice_CreateVideoDecoder( dx_sys->d3ddec, &decoderDesc, &sys->cfg, &decoder );
971 972 973 974 975
    if (FAILED(hr)) {
        msg_Err(va, "ID3D11VideoDevice_CreateVideoDecoder failed. (hr=0x%lX)", hr);
        dx_sys->decoder = NULL;
        return VLC_EGENERIC;
    }
976
    dx_sys->decoder = decoder;
977 978 979 980 981 982 983 984

    msg_Dbg(va, "DxCreateDecoderSurfaces succeed");
    return VLC_SUCCESS;
}

static void DxDestroySurfaces(vlc_va_t *va)
{
    directx_sys_t *dx_sys = &va->sys->dx_sys;
985
    if (dx_sys->va_pool.surface_count && !va->sys->b_extern_pool) {
986
        ID3D11Resource *p_texture;
987
        ID3D11VideoDecoderOutputView_GetResource( dx_sys->va_pool.hw_surface[0], &p_texture );
988 989 990
        ID3D11Resource_Release(p_texture);
        ID3D11Resource_Release(p_texture);
    }
991
    for (unsigned i = 0; i < dx_sys->va_pool.surface_count; i++)
992
    {
993
        ID3D11VideoDecoderOutputView_Release( dx_sys->va_pool.hw_surface[i] );
994 995 996 997 998 999
        for (int j = 0; j < D3D11_MAX_SHADER_VIEW; j++)
        {
            if (va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j])
                ID3D11ShaderResourceView_Release(va->sys->resourceView[i*D3D11_MAX_SHADER_VIEW + j]);
        }
    }
1000 1001 1002 1003 1004
    if (dx_sys->decoder)
    {
        ID3D11VideoDecoder_Release(dx_sys->decoder);
        dx_sys->decoder = NULL;
    }
1005
}