d3d11va.c 34.3 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
#define D3D_DecoderType     ID3D11VideoDecoder
#define D3D_DecoderDevice   ID3D11VideoDevice
#define D3D_DecoderSurface  ID3D11VideoDecoderOutputView
#include "directx_va.h"

58 59
static int Open(vlc_va_t *, AVCodecContext *, enum PixelFormat,
                const es_format_t *, picture_sys_t *p_sys);
60
static void Close(vlc_va_t *, void **);
61 62 63

vlc_module_begin()
    set_description(N_("Direct3D11 Video Acceleration"))
64
    set_capability("hw decoder", 110)
65 66 67 68 69
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_VCODEC)
    set_callbacks(Open, Close)
vlc_module_end()

70 71 72 73 74 75 76
/*
 * 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 )

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#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__ */

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

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

100 101 102
struct vlc_va_sys_t
{
    directx_sys_t                dx_sys;
103
    UINT                         totalTextureSlices;
104 105
    unsigned                     textureWidth;
    unsigned                     textureHeight;
106

107
    d3d11_handle_t               hd3d;
108 109
    d3d11_device_t               d3d_dev;

110 111 112 113
    /* Video service */
    ID3D11VideoContext           *d3dvidctx;
    DXGI_FORMAT                  render;

114 115 116
    /* pool */
    picture_t                    *extern_pics[MAX_SURFACE_COUNT];

117 118 119 120 121
    /* Video decoder */
    D3D11_VIDEO_DECODER_CONFIG   cfg;

    /* avcodec internals */
    struct AVD3D11VAContext      hw;
122 123

    ID3D11ShaderResourceView     *resourceView[MAX_SURFACE_COUNT * D3D11_MAX_SHADER_VIEW];
124 125 126 127 128
};

/* */
static int D3dCreateDevice(vlc_va_t *);
static void D3dDestroyDevice(vlc_va_t *);
129
static char *DxDescribe(vlc_va_sys_t *);
130 131 132 133

static int DxCreateVideoService(vlc_va_t *);
static void DxDestroyVideoService(vlc_va_t *);
static int DxGetInputList(vlc_va_t *, input_list_t *);
134
static int DxSetupOutput(vlc_va_t *, const GUID *, const video_format_t *);
135

136 137
static int DxCreateDecoderSurfaces(vlc_va_t *, int codec_id,
                                   const video_format_t *fmt, unsigned surface_count);
138 139 140 141 142 143 144 145 146
static void DxDestroySurfaces(vlc_va_t *);
static void SetupAVCodecContext(vlc_va_t *);

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;
147
    sys->hw.decoder = dx_sys->decoder;
148
    sys->hw.cfg = &sys->cfg;
149
    sys->hw.surface_count = dx_sys->va_pool.surface_count;
150
    sys->hw.surface = dx_sys->hw_surface;
151
    sys->hw.context_mutex = sys->d3d_dev.context_mutex;
152 153 154 155 156

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

157 158 159 160
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)
161
        va_surface_Release(pic_ctx->va_surface);
162 163 164 165
    ReleasePictureSys(&pic_ctx->picsys);
    free(pic_ctx);
}

166
static struct va_pic_context *CreatePicContext(ID3D11VideoDecoderOutputView *,
Steve Lhomme's avatar
Steve Lhomme committed
167 168 169 170 171 172 173 174
                                               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;
175
    struct va_pic_context *pic_ctx = CreatePicContext(src_ctx->picsys.decoder,
Steve Lhomme's avatar
Steve Lhomme committed
176 177 178 179
                                                      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;
180 181 182 183
    if (src_ctx->va_surface) {
        pic_ctx->va_surface = src_ctx->va_surface;
        va_surface_AddRef(pic_ctx->va_surface);
    }
Steve Lhomme's avatar
Steve Lhomme committed
184 185 186
    return &pic_ctx->s;
}

187
static struct va_pic_context *CreatePicContext(
188 189 190 191 192 193 194 195 196 197
                                                  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
198
    pic_ctx->s.copy    = d3d11_pic_context_copy;
199 200 201 202 203

    D3D11_TEXTURE2D_DESC txDesc;
    ID3D11Texture2D_GetDesc((ID3D11Texture2D*)p_resource, &txDesc);

    pic_ctx->picsys.formatTexture = txDesc.Format;
204 205 206 207 208 209 210 211
    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];
    }
212
    AcquirePictureSys(&pic_ctx->picsys);
213 214 215 216 217
    pic_ctx->picsys.context = context;
done:
    return pic_ctx;
}

218
static struct va_pic_context* NewSurfacePicContext(vlc_va_t *va, int surface_index)
219
{
220 221
    vlc_va_sys_t *sys = va->sys;
    directx_sys_t *dx_sys = &sys->dx_sys;
222
    ID3D11VideoDecoderOutputView *surface = dx_sys->hw_surface[surface_index];
223 224 225 226 227 228 229 230
    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++)
231
        resourceView[i] = sys->resourceView[viewDesc.Texture2D.ArraySlice*D3D11_MAX_SHADER_VIEW + i];
232

233
    struct va_pic_context *pic_ctx = CreatePicContext(
234 235
                                                  surface,
                                                  p_resource,
236
                                                  sys->d3d_dev.d3dcontext,
237 238 239 240 241 242 243 244 245
                                                  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);
246
    return pic_ctx;
247 248
}

249 250
static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
{
251
    picture_sys_t *p_sys = pic->p_sys;
252
#if D3D11_DIRECT_DECODE
253
    if (va->sys->dx_sys.can_extern_pool)
254
    {
255 256 257
        /* copy the original picture_sys_t in the va_pic_context */
        if (!pic->context)
        {
258 259
            assert(p_sys!=NULL);
            if (!p_sys->decoder)
260 261 262 263 264 265
            {
                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;
266
                viewDesc.Texture2D.ArraySlice = p_sys->slice_index;
267 268

                hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( va->sys->dx_sys.d3ddec,
269
                                                                     p_sys->resource[KNOWN_DXGI_INDEX],
270
                                                                     &viewDesc,
271
                                                                     &p_sys->decoder );
272 273 274
                if (FAILED(hr))
                    return VLC_EGENERIC;
            }
275

276
            pic->context = (picture_context_t*)CreatePicContext(
277 278
                                             p_sys->decoder,
                                             p_sys->resource[KNOWN_DXGI_INDEX],
279
                                             va->sys->d3d_dev.d3dcontext,
280 281
                                             p_sys->slice_index,
                                             p_sys->resourceView );
282 283 284 285 286 287
            if (pic->context == NULL)
                return VLC_EGENERIC;
        }
    }
    else
#endif
288
    {
289
        int res = va_pool_Get(&va->sys->dx_sys.va_pool, pic);
290 291
        if (unlikely(res != VLC_SUCCESS))
            return res;
292
    }
293
    *data = (uint8_t*)((struct va_pic_context *)pic->context)->picsys.decoder;
294
    return VLC_SUCCESS;
295 296
}

297
static void Close(vlc_va_t *va, void **ctx)
298 299 300 301 302 303 304
{
    vlc_va_sys_t *sys = va->sys;

    (void) ctx;

    directx_va_Close(va, &sys->dx_sys);

305 306
    D3D11_Destroy( &sys->hd3d );

307 308 309 310 311 312 313 314 315 316
    free((char *)va->description);
    free(sys);
}

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;

317 318
    ctx->hwaccel_context = NULL;

319 320 321 322 323 324 325 326 327
    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;

    dx_sys = &sys->dx_sys;

328 329 330 331 332 333 334
    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;
335
    dx_sys->va_pool.pf_new_surface_context     = NewSurfacePicContext;
336 337 338 339 340
    dx_sys->pf_get_input_list          = DxGetInputList;
    dx_sys->pf_setup_output            = DxSetupOutput;

    va->sys = sys;

341
    sys->d3d_dev.d3ddevice = NULL;
342 343
    va->sys->render = DXGI_FORMAT_UNKNOWN;
    if ( p_sys != NULL && p_sys->context != NULL ) {
344 345
        void *d3dvidctx = NULL;
        HRESULT hr = ID3D11DeviceContext_QueryInterface(p_sys->context, &IID_ID3D11VideoContext, &d3dvidctx);
346
        if (FAILED(hr)) {
Steve Lhomme's avatar
Steve Lhomme committed
347
           msg_Err(va, "Could not Query ID3D11VideoContext Interface from the picture. (hr=0x%lX)", hr);
348
        } else {
349
            ID3D11DeviceContext_GetDevice( p_sys->context, &sys->d3d_dev.d3ddevice );
350 351
            HANDLE context_lock = INVALID_HANDLE_VALUE;
            UINT dataSize = sizeof(context_lock);
352
            hr = ID3D11Device_GetPrivateData(sys->d3d_dev.d3ddevice, &GUID_CONTEXT_MUTEX, &dataSize, &context_lock);
353
            if (FAILED(hr))
354
                msg_Warn(va, "No mutex found to lock the decoder");
355
            sys->d3d_dev.context_mutex = context_lock;
356

357
            sys->d3d_dev.d3dcontext = p_sys->context;
358
            sys->d3d_dev.owner = false;
359
            D3D11_GetDriverVersion(va, &sys->d3d_dev);
360 361
            sys->d3dvidctx = d3dvidctx;

362
            assert(p_sys->texture[KNOWN_DXGI_INDEX] != NULL);
363
            D3D11_TEXTURE2D_DESC dstDesc;
364
            ID3D11Texture2D_GetDesc( p_sys->texture[KNOWN_DXGI_INDEX], &dstDesc);
365
            sys->render = dstDesc.Format;
366 367
            va->sys->textureWidth = dstDesc.Width;
            va->sys->textureHeight = dstDesc.Height;
368
            va->sys->totalTextureSlices = dstDesc.ArraySize;
369 370 371
        }
    }

372
    err = D3D11_Create( va, &sys->hd3d, false );
373 374 375
    if (err != VLC_SUCCESS)
        goto error;

376
    err = directx_va_Open(va, &sys->dx_sys);
377 378 379
    if (err!=VLC_SUCCESS)
        goto error;

380
    err = directx_va_Setup(va, &sys->dx_sys, ctx, fmt, isXboxHardware(sys->d3d_dev.d3ddevice));
381 382 383 384 385
    if (err != VLC_SUCCESS)
        goto error;

    ctx->hwaccel_context = &sys->hw;

386
    /* TODO print the hardware name/vendor for debugging purposes */
387
    va->description = DxDescribe(sys);
388 389 390 391 392
    va->get     = Get;

    return VLC_SUCCESS;

error:
Steve Lhomme's avatar
Steve Lhomme committed
393
    Close(va, NULL);
394 395 396 397 398 399 400 401
    return err;
}

/**
 * It creates a Direct3D device usable for decoding
 */
static int D3dCreateDevice(vlc_va_t *va)
{
402
    vlc_va_sys_t *sys = va->sys;
403 404
    HRESULT hr;

405
    if (sys->d3d_dev.d3ddevice && sys->d3d_dev.d3dcontext) {
406
        msg_Dbg(va, "Reusing Direct3D11 device");
407
        ID3D11DeviceContext_AddRef(sys->d3d_dev.d3dcontext);
408 409 410
        return VLC_SUCCESS;
    }

411 412 413 414 415 416 417 418
#if VLC_WINSTORE_APP
    sys->d3d_dev.d3dcontext = var_InheritInteger(va, "winrt-d3dcontext");
    if (likely(sys->d3d_dev.d3dcontext))
    {
        ID3D11Device* d3ddevice = NULL;
        ID3D11DeviceContext_GetDevice(sys->d3d_dev.d3dcontext, &sys->d3d_dev.d3ddevice);
        ID3D11DeviceContext_AddRef(sys->d3d_dev.d3dcontext);
        ID3D11Device_Release(sys->d3d_dev.d3ddevice);
419
    }
420 421 422 423 424 425 426 427 428 429 430
#endif

    /* */
    if (!sys->d3d_dev.d3ddevice)
    {
        hr = D3D11_CreateDevice(va, &sys->hd3d, true, &sys->d3d_dev);
        if (FAILED(hr)) {
            msg_Err(va, "D3D11CreateDevice failed. (hr=0x%lX)", hr);
            return VLC_EGENERIC;
        }
	}
431

432
    void *d3dvidctx = NULL;
433
    hr = ID3D11DeviceContext_QueryInterface(sys->d3d_dev.d3dcontext, &IID_ID3D11VideoContext, &d3dvidctx);
434
    if (FAILED(hr)) {
Steve Lhomme's avatar
Steve Lhomme committed
435
       msg_Err(va, "Could not Query ID3D11VideoContext Interface. (hr=0x%lX)", hr);
Steve Lhomme's avatar
Steve Lhomme committed
436 437
       ID3D11DeviceContext_Release(sys->d3d_dev.d3dcontext);
       ID3D11Device_Release(sys->d3d_dev.d3ddevice);
438 439
       return VLC_EGENERIC;
    }
440
    sys->d3dvidctx = d3dvidctx;
441 442 443 444 445 446 447 448 449

    return VLC_SUCCESS;
}

/**
 * It releases a Direct3D device and its resources.
 */
static void D3dDestroyDevice(vlc_va_t *va)
{
450 451 452 453
    vlc_va_sys_t *sys = va->sys;
    if (sys->d3dvidctx)
        ID3D11VideoContext_Release(sys->d3dvidctx);
    D3D11_ReleaseDevice( &sys->d3d_dev );
454
}
455

456 457 458
/**
 * It describes our Direct3D object
 */
459
static char *DxDescribe(vlc_va_sys_t *sys)
460 461
{

462
    IDXGIAdapter *p_adapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
463
    if (!p_adapter) {
464 465 466
       return NULL;
    }

467
    char *description = NULL;
468 469
    DXGI_ADAPTER_DESC adapterDesc;
    if (SUCCEEDED(IDXGIAdapter_GetDesc(p_adapter, &adapterDesc))) {
470 471 472
        char *utfdesc = FromWide(adapterDesc.Description);
        if (likely(utfdesc!=NULL))
        {
473
            if (asprintf(&description, "D3D11VA (%s, vendor %x(%s), device %x, revision %x)",
474
                         utfdesc,
475
                         adapterDesc.VendorId, DxgiVendorStr(adapterDesc.VendorId), adapterDesc.DeviceId, adapterDesc.Revision) < 0)
476 477 478
                description = NULL;
            free(utfdesc);
        }
479 480 481
    }

    IDXGIAdapter_Release(p_adapter);
482
    return description;
483 484 485 486 487 488 489 490 491
}

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

492
    void *d3dviddev = NULL;
493
    HRESULT hr = ID3D11Device_QueryInterface(va->sys->d3d_dev.d3ddevice, &IID_ID3D11VideoDevice, &d3dviddev);
494 495 496 497
    if (FAILED(hr)) {
       msg_Err(va, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr);
       return VLC_EGENERIC;
    }
498
    dx_sys->d3ddec = d3dviddev;
499 500 501 502 503 504 505 506 507

    return VLC_SUCCESS;
}

/**
 * It destroys a DirectX video service
 */
static void DxDestroyVideoService(vlc_va_t *va)
{
508 509 510
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    if (dx_sys->d3ddec)
        ID3D11VideoDevice_Release(dx_sys->d3ddec);
511 512 513 514 515 516 517 518 519 520 521 522
}

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;

523
    UINT input_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(dx_sys->d3ddec);
524 525 526 527 528 529 530 531 532

    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++) {
533
        hr = ID3D11VideoDevice_GetVideoDecoderProfile(dx_sys->d3ddec, i, &p_list->list[i]);
534 535 536 537 538 539 540 541 542 543 544
        if (FAILED(hr))
        {
            msg_Err(va, "GetVideoDecoderProfile %d failed. (hr=0x%lX)", i, hr);
            ReleaseInputList(p_list);
            return VLC_EGENERIC;
        }
    }

    return VLC_SUCCESS;
}

545
extern const GUID DXVA_ModeHEVC_VLD_Main10;
546
extern const GUID DXVA_ModeVP9_VLD_10bit_Profile2;
547

548
static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *fmt)
549
{
550 551
    vlc_va_sys_t *sys = va->sys;
    directx_sys_t *dx_sys = &sys->dx_sys;
552 553
    HRESULT hr;

554 555 556
#ifndef NDEBUG
    BOOL bSupported = false;
    for (int format = 0; format < 188; format++) {
557
        hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, format, &bSupported);
558 559 560 561
        if (SUCCEEDED(hr) && bSupported)
            msg_Dbg(va, "format %s is supported for output", DxgiFormatToStr(format));
    }
#endif
562

563 564 565 566 567 568 569 570
    IDXGIAdapter *pAdapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
    if (!pAdapter)
        return VLC_EGENERIC;

    DXGI_ADAPTER_DESC adapterDesc;
    hr = IDXGIAdapter_GetDesc(pAdapter, &adapterDesc);
    IDXGIAdapter_Release(pAdapter);
    if (FAILED(hr))
571 572
        return VLC_EGENERIC;

573 574
    if (!directx_va_canUseDecoder(va, adapterDesc.VendorId, adapterDesc.DeviceId,
                                  input, sys->d3d_dev.WDDM.build))
575 576 577 578 579
    {
        msg_Warn(va, "GPU blacklisted for %s codec", directx_va_GetDecoderName(input));
        return VLC_EGENERIC;
    }

Steve Lhomme's avatar
Steve Lhomme committed
580
    DXGI_FORMAT processorInput[5];
581
    int idx = 0;
582 583
    if ( sys->render != DXGI_FORMAT_UNKNOWN )
        processorInput[idx++] = sys->render;
584 585
    if (IsEqualGUID(input, &DXVA_ModeHEVC_VLD_Main10) || IsEqualGUID(input, &DXVA_ModeVP9_VLD_10bit_Profile2))
        processorInput[idx++] = DXGI_FORMAT_P010;
586
    processorInput[idx++] = DXGI_FORMAT_NV12;
587
    processorInput[idx++] = DXGI_FORMAT_420_OPAQUE;
588 589
    processorInput[idx++] = DXGI_FORMAT_UNKNOWN;

590 591
    char *psz_decoder_name = directx_va_GetDecoderName(input);

592 593
    /* */
    for (idx = 0; processorInput[idx] != DXGI_FORMAT_UNKNOWN; ++idx)
594
    {
595
        BOOL is_supported = false;
596
        hr = ID3D11VideoDevice_CheckVideoDecoderFormat(dx_sys->d3ddec, input, processorInput[idx], &is_supported);
597
        if (SUCCEEDED(hr) && is_supported)
598
            msg_Dbg(va, "%s output is supported for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
599
        else
600
        {
601
            msg_Dbg(va, "Can't get a decoder output format %s for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
602
            continue;
603
        }
604

605 606
       // check if we can create render texture of that format
       // check the decoder can output to that format
607
       if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
608 609
                                  D3D11_FORMAT_SUPPORT_SHADER_LOAD) )
       {
610 611 612
#ifndef ID3D11VideoContext_VideoProcessorBlt
           msg_Dbg(va, "Format %s needs a processor but is not supported",
                   DxgiFormatToStr(processorInput[idx]));
613
#else
614
           if ( !DeviceSupportsFormat(sys->d3d_dev.d3ddevice, processorInput[idx],
615
                                      D3D11_FORMAT_SUPPORT_VIDEO_PROCESSOR_INPUT) )
616 617 618 619 620
           {
               msg_Dbg(va, "Format %s needs a processor but is not available",
                       DxgiFormatToStr(processorInput[idx]));
               continue;
           }
621
#endif
622
        }
623

624 625 626 627 628 629 630 631
        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;
632
        hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
633 634 635 636 637 638 639 640 641 642 643 644
        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;
        }

645
        msg_Dbg(va, "Using output format %s for decoder %s", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
646
        if ( sys->render == processorInput[idx] )
647
        {
648
            if (CanUseVoutPool(&sys->d3d_dev, sys->totalTextureSlices))
649
                dx_sys->can_extern_pool = true;
650
            else
Steve Lhomme's avatar
Steve Lhomme committed
651
                msg_Warn( va, "use internal pool" );
652
        }
653
        sys->render = processorInput[idx];
654 655
        free(psz_decoder_name);
        return VLC_SUCCESS;
656
    }
657
    free(psz_decoder_name);
658 659

    msg_Dbg(va, "Output format from picture source not supported.");
660 661 662
    return VLC_EGENERIC;
}

663
static bool CanUseDecoderPadding(vlc_va_sys_t *sys)
664
{
665
    IDXGIAdapter *pAdapter = D3D11DeviceAdapter(sys->d3d_dev.d3ddevice);
666 667 668 669 670 671 672 673 674 675 676
    if (!pAdapter)
        return false;

    DXGI_ADAPTER_DESC adapterDesc;
    HRESULT hr = IDXGIAdapter_GetDesc(pAdapter, &adapterDesc);
    IDXGIAdapter_Release(pAdapter);
    if (FAILED(hr))
        return false;

    /* Qualcomm hardware has issues with textures and pixels that should not be
    * part of the decoded area */
677
    return adapterDesc.VendorId != GPU_MANUFACTURER_QUALCOMM;
678 679
}

680 681 682
/**
 * It creates a Direct3D11 decoder using the given video format
 */
683 684
static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id,
                                   const video_format_t *fmt, unsigned surface_count)
685 686 687 688 689 690
{
    vlc_va_sys_t *sys = va->sys;
    directx_sys_t *dx_sys = &va->sys->dx_sys;
    HRESULT hr;

    ID3D10Multithread *pMultithread;
691
    hr = ID3D11Device_QueryInterface( sys->d3d_dev.d3ddevice, &IID_ID3D10Multithread, (void **)&pMultithread);
692
    if (SUCCEEDED(hr)) {
693
        ID3D10Multithread_SetMultithreadProtected(pMultithread, TRUE);
694 695 696
        ID3D10Multithread_Release(pMultithread);
    }

697 698 699 700 701 702
    if (!sys->textureWidth || !sys->textureHeight)
    {
        sys->textureWidth  = fmt->i_width;
        sys->textureHeight = fmt->i_height;
    }

703 704 705
    assert(sys->textureWidth  >= fmt->i_width);
    assert(sys->textureHeight >= fmt->i_height);

706 707 708 709 710 711 712 713 714
    if ((sys->textureWidth != fmt->i_width || sys->textureHeight != fmt->i_height) &&
        !CanUseDecoderPadding(sys))
    {
        msg_Dbg(va, "mismatching external pool sizes use the internal one %dx%d vs %dx%d",
                sys->textureWidth, sys->textureHeight, fmt->i_width, fmt->i_height);
        dx_sys->can_extern_pool = false;
        sys->textureWidth  = fmt->i_width;
        sys->textureHeight = fmt->i_height;
    }
715 716 717 718 719 720
    if (sys->totalTextureSlices && sys->totalTextureSlices < surface_count)
    {
        msg_Warn(va, "not enough decoding slices in the texture (%d/%d)",
                 sys->totalTextureSlices, surface_count);
        dx_sys->can_extern_pool = false;
    }
721 722 723 724
#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 &&
725
        (sys->textureWidth > 2304 || sys->textureHeight > 2304) &&
726
        isXboxHardware(sys->d3d_dev.d3ddevice))
727
    {
728
        msg_Warn(va, "%dx%d resolution not supported by your hardware", fmt->i_width, fmt->i_height);
729 730 731 732
        return VLC_EGENERIC;
    }
#endif

733 734 735 736 737
    D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC viewDesc;
    ZeroMemory(&viewDesc, sizeof(viewDesc));
    viewDesc.DecodeProfile = dx_sys->input;
    viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;

738 739 740 741 742
    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 &&
743
                is_d3d11_opaque(output_format->fourcc))
744 745 746 747 748
        {
            textureFmt = output_format;
            break;
        }
    }
749 750 751 752 753
    if (unlikely(textureFmt==NULL))
    {
        msg_Dbg(va, "no hardware decoder matching %s", DxgiFormatToStr(sys->render));
        return VLC_EGENERIC;
    }
754

755
    if (dx_sys->can_extern_pool)
756
    {
757
#if !D3D11_DIRECT_DECODE
758
        size_t surface_idx;
759
        for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
760 761
            picture_t *pic = decoder_NewPicture( (decoder_t*) va->obj.parent );
            sys->extern_pics[surface_idx] = pic;
762
            dx_sys->hw_surface[surface_idx] = NULL;
763 764
            if (pic==NULL)
            {
765
                msg_Warn(va, "not enough decoder pictures %d out of %d", surface_idx, surface_count);
766
                dx_sys->can_extern_pool = false;
767 768
                break;
            }
769

770
            D3D11_TEXTURE2D_DESC texDesc;
771
            ID3D11Texture2D_GetDesc(pic->p_sys->texture[KNOWN_DXGI_INDEX], &texDesc);
772 773 774
            assert(texDesc.Format == sys->render);
            assert(texDesc.BindFlags & D3D11_BIND_DECODER);

775
#if !LIBAVCODEC_VERSION_CHECK( 57, 27, 2, 61, 102 )
776 777 778 779
            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);
780
                dx_sys->can_extern_pool = false;
781 782
                break;
            }
783
#endif
784 785

            viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
786
            hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
787
                                                                 pic->p_sys->resource[KNOWN_DXGI_INDEX],
788 789 790 791
                                                                 &viewDesc,
                                                                 &pic->p_sys->decoder );
            if (FAILED(hr)) {
                msg_Warn(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
792
                dx_sys->can_extern_pool = false;
793 794
                break;
            }
795

796
            D3D11_AllocateShaderView(va, sys->d3d_dev.d3ddevice, textureFmt, pic->p_sys->texture, pic->p_sys->slice_index, pic->p_sys->resourceView);
797

798
            dx_sys->hw_surface[surface_idx] = pic->p_sys->decoder;
799 800
        }

801
        if (!dx_sys->can_extern_pool)
802 803 804
        {
            for (size_t i = 0; i < surface_idx; ++i)
            {
805
                if (dx_sys->hw_surface[i])
806
                {
807 808
                    ID3D11VideoDecoderOutputView_Release(dx_sys->hw_surface[i]);
                    dx_sys->hw_surface[i] = NULL;
809 810 811 812 813 814 815 816 817 818
                }
                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
819
#endif
820 821 822
            msg_Dbg(va, "using external surface pool");
    }

823
    if (!dx_sys->can_extern_pool)
824 825 826
    {
        D3D11_TEXTURE2D_DESC texDesc;
        ZeroMemory(&texDesc, sizeof(texDesc));
827 828
        texDesc.Width = sys->textureWidth;
        texDesc.Height = sys->textureHeight;
829 830 831 832
        texDesc.MipLevels = 1;
        texDesc.Format = sys->render;
        texDesc.SampleDesc.Count = 1;
        texDesc.MiscFlags = 0;
833
        texDesc.ArraySize = surface_count;
834 835 836 837
        texDesc.Usage = D3D11_USAGE_DEFAULT;
        texDesc.BindFlags = D3D11_BIND_DECODER;
        texDesc.CPUAccessFlags = 0;

838
        if (DeviceSupportsFormat(sys->d3d_dev.d3ddevice, texDesc.Format, D3D11_FORMAT_SUPPORT_SHADER_LOAD))
839 840
            texDesc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;

841
        ID3D11Texture2D *p_texture;
842
        hr = ID3D11Device_CreateTexture2D( sys->d3d_dev.d3ddevice, &texDesc, NULL, &p_texture );
843
        if (FAILED(hr)) {
844
            msg_Err(va, "CreateTexture2D %d failed. (hr=0x%0lx)", surface_count, hr);
845 846
            return VLC_EGENERIC;
        }
847

848 849 850 851
        unsigned surface_idx;
        for (surface_idx = 0; surface_idx < surface_count; surface_idx++) {
            sys->extern_pics[surface_idx] = NULL;
            viewDesc.Texture2D.ArraySlice = surface_idx;
852

853
            hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( dx_sys->d3ddec,
854 855
                                                                 (ID3D11Resource*) p_texture,
                                                                 &viewDesc,
856
                                                                 &dx_sys->hw_surface[surface_idx] );
857
            if (FAILED(hr)) {
858
                msg_Err(va, "CreateVideoDecoderOutputView %d failed. (hr=0x%0lx)", surface_idx, hr);
859 860 861
                ID3D11Texture2D_Release(p_texture);
                return VLC_EGENERIC;
            }
862 863

            if (texDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
864
            {
Steve Lhomme's avatar
Steve Lhomme committed
865
                ID3D11Texture2D *textures[D3D11_MAX_SHADER_VIEW] = {p_texture, p_texture, p_texture};
866
                D3D11_AllocateShaderView(va, sys->d3d_dev.d3ddevice, textureFmt, textures, surface_idx,
867
                                   &sys->resourceView[surface_idx * D3D11_MAX_SHADER_VIEW]);
868
            }
869
        }
870 871
    }
    msg_Dbg(va, "ID3D11VideoDecoderOutputView succeed with %d surfaces (%dx%d)",
872
            surface_count, fmt->i_width, fmt->i_height);
873 874 875 876 877 878 879 880 881

    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;
882
    hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( dx_sys->d3ddec, &decoderDesc, &cfg_count );
883 884 885 886 887 888 889 890
    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++) {
891
        hr = ID3D11VideoDevice_GetVideoDecoderConfig( dx_sys->d3ddec, &decoderDesc, i, &cfg_list[i] );
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
        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;
917
        if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
918 919 920 921 922 923 924 925 926 927 928 929 930 931
            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;
932
    hr = ID3D11VideoDevice_CreateVideoDecoder( dx_sys->d3ddec, &decoderDesc, &sys->cfg, &decoder );
933 934 935 936 937
    if (FAILED(hr)) {
        msg_Err(va, "ID3D11VideoDevice_CreateVideoDecoder failed. (hr=0x%lX)", hr);
        dx_sys->decoder = NULL;
        return VLC_EGENERIC;
    }
938
    dx_sys->decoder = decoder;
939 940 941 942 943 944 945 946

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

static void DxDestroySurfaces(vlc_va_t *va)
{
    directx_sys_t *dx_sys = &va->sys->dx_sys;
947
    if (dx_sys->va_pool.surface_count && !dx_sys->can_extern_pool) {
948
        ID3D11Resource *p_texture;
949
        ID3D11VideoDecoderOutputView_GetResource( dx_sys->hw_surface[0], &p_texture );
950 951 952
        ID3D11Resource_Release(p_texture);
        ID3D11Resource_Release(p_texture);
    }
953
    for (unsigned i = 0; i < dx_sys->va_pool.surface_count; i++)
954
    {
955
        ID3D11VideoDecoderOutputView_Release( dx_sys->hw_surface[i] );
956 957 958 959 960 961
        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]);
        }
    }
962 963 964 965 966
    if (dx_sys->decoder)
    {
        ID3D11VideoDecoder_Release(dx_sys->decoder);
        dx_sys->decoder = NULL;
    }
967
}