19 #include <stdatomic.h> 
   55 #define QSV_VERSION_ATLEAST(MAJOR, MINOR)   \ 
   56     (MFX_VERSION_MAJOR > (MAJOR) ||         \ 
   57      MFX_VERSION_MAJOR == (MAJOR) && MFX_VERSION_MINOR >= (MINOR)) 
   59 #define QSV_RUNTIME_VERSION_ATLEAST(MFX_VERSION, MAJOR, MINOR)          \ 
   60     ((MFX_VERSION.Major > (MAJOR)) ||                                   \ 
   61      (MFX_VERSION.Major == (MAJOR) && MFX_VERSION.Minor >= (MINOR))) 
   63 #define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl)) 
   64 #define QSV_ONEVPL       QSV_VERSION_ATLEAST(2, 0) 
   65 #define QSV_HAVE_OPAQUE  !QSV_ONEVPL 
   68 #include <mfxdispatcher.h> 
   70 #define MFXUnload(a) do { } while(0) 
  124 static const struct {
 
  134                        MFX_FOURCC_YUY2, 0 },
 
  137                        MFX_FOURCC_UYVY, 0 },
 
  140                        MFX_FOURCC_Y210, 1 },
 
  144                        MFX_FOURCC_AYUV, 0 },
 
  148                        MFX_FOURCC_Y410, 0 },
 
  149 #if QSV_VERSION_ATLEAST(1, 31) 
  153                        MFX_FOURCC_P016, 1 },
 
  157                        MFX_FOURCC_Y216, 1 },
 
  161                        MFX_FOURCC_Y416, 1 },
 
  176     mfxHDLPair *handle_pair;
 
  177     handle_pair = surf->Data.MemId;
 
  178     switch (base_dev_type) {
 
  181         base_handle[0] = handle_pair->first;
 
  186         base_handle[0] = handle_pair->first;
 
  187         base_handle[1] = handle_pair->second;
 
  192         base_handle[0] = handle_pair->first;
 
  220 static uint32_t qsv_get_d3d11va_bind_flags(
int mem_type)
 
  222     uint32_t bind_flags = 0;
 
  224     if ((mem_type & MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) && (mem_type & MFX_MEMTYPE_INTERNAL_FRAME))
 
  225         bind_flags = D3D11_BIND_DECODER | D3D11_BIND_VIDEO_ENCODER;
 
  227         bind_flags = D3D11_BIND_DECODER;
 
  229     if ((MFX_MEMTYPE_FROM_VPPOUT & mem_type) || (MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET & mem_type))
 
  230         bind_flags = D3D11_BIND_RENDER_TARGET;
 
  239     int i, planes_nb = 0;
 
  245     for (
i = 0; 
i < 
desc->nb_components; 
i++)
 
  246         planes_nb = 
FFMAX(planes_nb, 
desc->comp[
i].plane + 1);
 
  248     for (
i = 0; 
i < planes_nb; 
i++) {
 
  249         int sheight, dheight, y;
 
  257         if (swidth < 0 || dwidth < 0) {
 
  261         sheight = 
src->height;
 
  268         for (y = 0; y < sheight; y++) {
 
  271                            comp.depth > 8 ? 2 : 1,
 
  275         for (y = sheight; y < dheight; y++) {
 
  288     int   hw_handle_supported = 0;
 
  289     mfxHandleType handle_type;
 
  294     err = MFXQueryIMPL(hwctx->
session, &
s->impl);
 
  295     if (err == MFX_ERR_NONE)
 
  296         err = MFXQueryVersion(hwctx->
session, &
s->ver);
 
  297     if (err != MFX_ERR_NONE) {
 
  304         handle_type = MFX_HANDLE_VA_DISPLAY;
 
  307         hw_handle_supported = 1;
 
  311         handle_type = MFX_HANDLE_D3D11_DEVICE;
 
  314         hw_handle_supported = 1;
 
  318         handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
 
  321         hw_handle_supported = 1;
 
  325     if (hw_handle_supported) {
 
  326         err = MFXVideoCORE_GetHandle(hwctx->
session, handle_type, &
s->handle);
 
  327         if (err == MFX_ERR_NONE) {
 
  328             s->handle_type       = handle_type;
 
  329             s->child_device_type = device_type;
 
  335                "from the session\n");
 
  344     if (
s->session_download) {
 
  345         MFXVideoVPP_Close(
s->session_download);
 
  346         MFXClose(
s->session_download);
 
  348     s->session_download = 
NULL;
 
  349     s->session_download_init = 0;
 
  351     if (
s->session_upload) {
 
  352         MFXVideoVPP_Close(
s->session_upload);
 
  353         MFXClose(
s->session_upload);
 
  355     s->session_upload = 
NULL;
 
  356     s->session_upload_init = 0;
 
  384         s->nb_surfaces_used++;
 
  406     if (!device_priv->
handle) {
 
  408                "Cannot create a non-opaque internal surface pool without " 
  409                "a hardware handle\n");
 
  414     if (!child_device_ref)
 
  421         child_device_hwctx->
display = (VADisplay)device_priv->
handle;
 
  434         child_device_hwctx->
devmgr = (IDirect3DDeviceManager9*)device_priv->
handle;
 
  445     if (!child_frames_ref) {
 
  461             hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
 
  462         if (hwctx->
frame_type & MFX_MEMTYPE_SHARED_RESOURCE)
 
  463             child_frames_hwctx->
MiscFlags = D3D11_RESOURCE_MISC_SHARED;
 
  470         if (hwctx->
frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
 
  471             child_frames_hwctx->
surface_type = DXVA2_VideoProcessorRenderTarget;
 
  473             child_frames_hwctx->
surface_type = DXVA2_VideoDecoderRenderTarget;
 
  486         for (
i = 0; 
i < 
ctx->initial_pool_size; 
i++) {
 
  487             s->handle_pairs_internal[
i].first = child_frames_hwctx->
surface_ids + 
i;
 
  488             s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
 
  489             s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
 
  491         hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
 
  497         for (
i = 0; 
i < 
ctx->initial_pool_size; 
i++) {
 
  499             if(child_frames_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
 
  500                 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
 
  504             s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
 
  506         if (child_frames_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
 
  507             hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
 
  509             hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
 
  516         for (
i = 0; 
i < 
ctx->initial_pool_size; 
i++) {
 
  517             s->handle_pairs_internal[
i].first = (mfxMemId)child_frames_hwctx->
surfaces[
i];
 
  518             s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
 
  519             s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
 
  521         if (child_frames_hwctx->
surface_type == DXVA2_VideoProcessorRenderTarget)
 
  522             hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
 
  524             hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
 
  528     s->child_frames_ref       = child_frames_ref;
 
  529     child_frames_ref          = 
NULL;
 
  550     surf->Info.BitDepthLuma   = 
desc->comp[0].depth;
 
  551     surf->Info.BitDepthChroma = 
desc->comp[0].depth;
 
  554     if (
desc->log2_chroma_w && 
desc->log2_chroma_h)
 
  555         surf->Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
 
  556     else if (
desc->log2_chroma_w)
 
  557         surf->Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV422;
 
  559         surf->Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV444;
 
  561     surf->Info.FourCC         = 
fourcc;
 
  563     surf->Info.CropW          = 
ctx->width;
 
  565     surf->Info.CropH          = 
ctx->height;
 
  566     surf->Info.FrameRateExtN  = 25;
 
  567     surf->Info.FrameRateExtD  = 1;
 
  568     surf->Info.PicStruct      = MFX_PICSTRUCT_PROGRESSIVE;
 
  580     if (
ctx->initial_pool_size <= 0) {
 
  586                                          sizeof(*
s->handle_pairs_internal));
 
  587     if (!
s->handle_pairs_internal)
 
  591                                      sizeof(*
s->surfaces_internal));
 
  592     if (!
s->surfaces_internal)
 
  595     for (
i = 0; 
i < 
ctx->initial_pool_size; 
i++) {
 
  602     if (!(frames_hwctx->
frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
 
  618     frames_hwctx->
surfaces    = 
s->surfaces_internal;
 
  624 static mfxStatus 
frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
 
  625                              mfxFrameAllocResponse *resp)
 
  630     mfxFrameInfo *
i  = &req->Info;
 
  631     mfxFrameInfo *i1 = &hwctx->
surfaces[0].Info;
 
  633     if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
 
  634         !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
 
  635         !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
 
  636         return MFX_ERR_UNSUPPORTED;
 
  637     if (
i->Width  > i1->Width || 
i->Height > i1->Height ||
 
  638         i->FourCC != i1->FourCC || 
i->ChromaFormat != i1->ChromaFormat) {
 
  640                "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
 
  641                i->Width,  
i->Height,  
i->FourCC,  
i->ChromaFormat,
 
  642                i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
 
  643         return MFX_ERR_UNSUPPORTED;
 
  646     resp->mids           = 
s->mem_ids;
 
  652 static mfxStatus 
frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
 
  657 static mfxStatus 
frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
 
  659     return MFX_ERR_UNSUPPORTED;
 
  662 static mfxStatus 
frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
 
  664     return MFX_ERR_UNSUPPORTED;
 
  669     mfxHDLPair *pair_dst = (mfxHDLPair*)hdl;
 
  670     mfxHDLPair *pair_src = (mfxHDLPair*)mid;
 
  672     pair_dst->first = pair_src->first;
 
  674     if (pair_src->second != (mfxMemId)MFX_INFINITE)
 
  675         pair_dst->second = pair_src->second;
 
  681 static int qsv_d3d11_update_config(
void *
ctx, mfxHDL handle, mfxConfig cfg)
 
  686     IDXGIAdapter *pDXGIAdapter;
 
  687     DXGI_ADAPTER_DESC adapterDesc;
 
  688     IDXGIDevice *pDXGIDevice = 
NULL;
 
  691     mfxVariant impl_value = {0};
 
  693     hr = ID3D11Device_QueryInterface(device, &IID_IDXGIDevice, (
void**)&pDXGIDevice);
 
  695         hr = IDXGIDevice_GetAdapter(pDXGIDevice, &pDXGIAdapter);
 
  698             IDXGIDevice_Release(pDXGIDevice);
 
  702         hr = IDXGIAdapter_GetDesc(pDXGIAdapter, &adapterDesc);
 
  712     impl_value.Type = MFX_VARIANT_TYPE_U16;
 
  713     impl_value.Data.U16 = adapterDesc.DeviceId;
 
  714     sts = MFXSetConfigFilterProperty(cfg,
 
  715                                      (
const mfxU8 *)
"mfxExtendedDeviceId.DeviceID", impl_value);
 
  716     if (sts != MFX_ERR_NONE) {
 
  718                "DeviceID property: %d.\n", sts);
 
  722     impl_value.Type = MFX_VARIANT_TYPE_PTR;
 
  723     impl_value.Data.Ptr = &adapterDesc.AdapterLuid;
 
  724     sts = MFXSetConfigFilterProperty(cfg,
 
  725                                      (
const mfxU8 *)
"mfxExtendedDeviceId.DeviceLUID", impl_value);
 
  726     if (sts != MFX_ERR_NONE) {
 
  728                "DeviceLUID property: %d.\n", sts);
 
  732     impl_value.Type = MFX_VARIANT_TYPE_U32;
 
  733     impl_value.Data.U32 = 0x0001;
 
  734     sts = MFXSetConfigFilterProperty(cfg,
 
  735                                      (
const mfxU8 *)
"mfxExtendedDeviceId.LUIDDeviceNodeMask", impl_value);
 
  736     if (sts != MFX_ERR_NONE) {
 
  738                "LUIDDeviceNodeMask property: %d.\n", sts);
 
  745     IDXGIAdapter_Release(pDXGIAdapter);
 
  746     IDXGIDevice_Release(pDXGIDevice);
 
  751 static int qsv_d3d9_update_config(
void *
ctx, mfxHDL handle, mfxConfig cfg)
 
  756     IDirect3DDeviceManager9* devmgr = handle;
 
  757     IDirect3DDevice9 *device = 
NULL;
 
  758     IDirect3DDevice9Ex *device_ex = 
NULL;
 
  759     HANDLE device_handle = 0;
 
  760     IDirect3D9Ex *d3d9ex = 
NULL;
 
  761     IDirect3D9 *d3d9 = 
NULL;
 
  763     D3DDEVICE_CREATION_PARAMETERS params;
 
  765     mfxVariant impl_value = {0};
 
  767     hr = IDirect3DDeviceManager9_OpenDeviceHandle(devmgr, &device_handle);
 
  773     hr = IDirect3DDeviceManager9_LockDevice(devmgr, device_handle, &device, TRUE);
 
  776         IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
 
  779     hr = IDirect3DDevice9_QueryInterface(device, &IID_IDirect3DDevice9Ex, (
void **)&device_ex);
 
  780     IDirect3DDevice9_Release(device);
 
  786     hr = IDirect3DDevice9Ex_GetCreationParameters(device_ex, ¶ms);
 
  789         IDirect3DDevice9Ex_Release(device_ex);
 
  793     hr = IDirect3DDevice9Ex_GetDirect3D(device_ex, &d3d9);
 
  796         IDirect3DDevice9Ex_Release(device_ex);
 
  799     hr = IDirect3D9_QueryInterface(d3d9, &IID_IDirect3D9Ex, (
void **)&d3d9ex);
 
  800     IDirect3D9_Release(d3d9);
 
  803         IDirect3DDevice9Ex_Release(device_ex);
 
  807     hr = IDirect3D9Ex_GetAdapterLUID(d3d9ex, params.AdapterOrdinal, &luid);
 
  813     impl_value.Type = MFX_VARIANT_TYPE_PTR;
 
  814     impl_value.Data.Ptr = &luid;
 
  815     sts = MFXSetConfigFilterProperty(cfg,
 
  816                                      (
const mfxU8 *)
"mfxExtendedDeviceId.DeviceLUID", impl_value);
 
  817     if (sts != MFX_ERR_NONE) {
 
  819                "DeviceLUID property: %d.\n", sts);
 
  826     IDirect3D9Ex_Release(d3d9ex);
 
  827     IDirect3DDevice9Ex_Release(device_ex);
 
  830     IDirect3DDeviceManager9_UnlockDevice(devmgr, device_handle, FALSE);
 
  831     IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
 
  837 static int qsv_va_update_config(
void *
ctx, mfxHDL handle, mfxConfig cfg)
 
  840 #if VA_CHECK_VERSION(1, 15, 0) 
  842     VADisplay dpy = handle;
 
  844     VADisplayAttribute attr = {
 
  845         .type = VADisplayPCIID,
 
  847     mfxVariant impl_value = {0};
 
  849     vas = vaGetDisplayAttributes(dpy, &attr, 1);
 
  850     if (vas == VA_STATUS_SUCCESS && attr.flags != VA_DISPLAY_ATTRIB_NOT_SUPPORTED) {
 
  851         impl_value.Type = MFX_VARIANT_TYPE_U16;
 
  852         impl_value.Data.U16 = (attr.value & 0xFFFF);
 
  853         sts = MFXSetConfigFilterProperty(cfg,
 
  854                                          (
const mfxU8 *)
"mfxImplDescription.mfxDeviceDescription.DeviceID", impl_value);
 
  855         if (sts != MFX_ERR_NONE) {
 
  857                    "DeviceID property: %d.\n", sts);
 
  862                "consider to upgrade the driver to support VA-API 1.15.0\n");
 
  871            "the device information from the driver. Please consider to upgrade libva to " 
  872            "support VA-API 1.15.0\n");
 
  878 static int qsv_new_mfx_loader(
void *
ctx,
 
  880                               mfxHandleType handle_type,
 
  881                               mfxIMPL implementation,
 
  886     mfxLoader loader = 
NULL;
 
  888     mfxVariant impl_value = {0};
 
  898     cfg = MFXCreateConfig(loader);
 
  904     impl_value.Type = MFX_VARIANT_TYPE_U32;
 
  905     impl_value.Data.U32 = (implementation == MFX_IMPL_SOFTWARE) ?
 
  906         MFX_IMPL_TYPE_SOFTWARE : MFX_IMPL_TYPE_HARDWARE;
 
  907     sts = MFXSetConfigFilterProperty(cfg,
 
  908                                      (
const mfxU8 *)
"mfxImplDescription.Impl", impl_value);
 
  909     if (sts != MFX_ERR_NONE) {
 
  911                "property: %d.\n", sts);
 
  915     impl_value.Type = MFX_VARIANT_TYPE_U32;
 
  916     impl_value.Data.U32 = pver->Version;
 
  917     sts = MFXSetConfigFilterProperty(cfg,
 
  918                                      (
const mfxU8 *)
"mfxImplDescription.ApiVersion.Version",
 
  920     if (sts != MFX_ERR_NONE) {
 
  922                "property: %d.\n", sts);
 
  926     impl_value.Type = MFX_VARIANT_TYPE_U32;
 
  927     impl_value.Data.U32 = 0x8086; 
 
  928     sts = MFXSetConfigFilterProperty(cfg,
 
  929                                      (
const mfxU8 *)
"mfxImplDescription.VendorID", impl_value);
 
  930     if (sts != MFX_ERR_NONE) {
 
  932                "VendorID property: %d.\n", sts);
 
  936     if (MFX_HANDLE_VA_DISPLAY == handle_type) {
 
  937         if (handle && qsv_va_update_config(
ctx, handle, cfg))
 
  940         impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_VAAPI;
 
  941     } 
else if (MFX_HANDLE_D3D9_DEVICE_MANAGER == handle_type) {
 
  942         if (handle && qsv_d3d9_update_config(
ctx, handle, cfg))
 
  945         impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_D3D9;
 
  947         if (handle && qsv_d3d11_update_config(
ctx, handle, cfg))
 
  950         impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_D3D11;
 
  953     impl_value.Type = MFX_VARIANT_TYPE_U32;
 
  954     sts = MFXSetConfigFilterProperty(cfg,
 
  955                                      (
const mfxU8 *)
"mfxImplDescription.AccelerationMode", impl_value);
 
  956     if (sts != MFX_ERR_NONE) {
 
  958                "AccelerationMode property: %d.\n", sts);
 
  973 static int qsv_create_mfx_session_from_loader(
void *
ctx, mfxLoader loader, mfxSession *psession)
 
  976     mfxSession session = 
NULL;
 
  977     uint32_t impl_idx = 0;
 
  982         mfxImplDescription *impl_desc;
 
  984         sts = MFXEnumImplementations(loader, impl_idx,
 
  985                                      MFX_IMPLCAPS_IMPLDESCSTRUCTURE,
 
  986                                      (mfxHDL *)&impl_desc);
 
  988         if (sts == MFX_ERR_NOT_FOUND)
 
  990         else if (sts != MFX_ERR_NONE) {
 
  995         sts = MFXCreateSession(loader, impl_idx, &session);
 
  996         MFXDispReleaseImplDescription(loader, impl_desc);
 
  997         if (sts == MFX_ERR_NONE)
 
 1003     if (sts != MFX_ERR_NONE) {
 
 1008     sts = MFXQueryVersion(session, &ver);
 
 1009     if (sts != MFX_ERR_NONE) {
 
 1015            "version is %d.%d\n", ver.Major, ver.Minor);
 
 1017     *psession = session;
 
 1030                                   mfxHandleType handle_type,
 
 1031                                   mfxIMPL implementation,
 
 1033                                   mfxSession *psession,
 
 1036     mfxLoader loader = 
NULL;
 
 1039            "Use Intel(R) oneVPL to create MFX session, API version is " 
 1040            "%d.%d, the required implementation version is %d.%d\n",
 
 1041            MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
 
 1043     if (handle_type != MFX_HANDLE_VA_DISPLAY &&
 
 1044         handle_type != MFX_HANDLE_D3D9_DEVICE_MANAGER &&
 
 1045         handle_type != MFX_HANDLE_D3D11_DEVICE) {
 
 1047                "Invalid MFX device handle type\n");
 
 1054         if (qsv_new_mfx_loader(
ctx, handle, handle_type, implementation, pver, (
void **)&loader))
 
 1061     if (qsv_create_mfx_session_from_loader(
ctx, loader, psession))
 
 1070     if (!*ploader && loader)
 
 1080                                   mfxHandleType handle_type,
 
 1081                                   mfxIMPL implementation,
 
 1083                                   mfxSession *psession,
 
 1088     mfxSession session = 
NULL;
 
 1091            "Use Intel(R) Media SDK to create MFX session, API version is " 
 1092            "%d.%d, the required implementation version is %d.%d\n",
 
 1093            MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
 
 1098     sts = MFXInit(implementation, &ver, &session);
 
 1099     if (sts != MFX_ERR_NONE) {
 
 1105     sts = MFXQueryVersion(session, &ver);
 
 1106     if (sts != MFX_ERR_NONE) {
 
 1113            "version is %d.%d\n", ver.Major, ver.Minor);
 
 1117     sts = MFXInit(implementation, &ver, &session);
 
 1118     if (sts != MFX_ERR_NONE) {
 
 1124     *psession = session;
 
 1138                                      mfxSession *session, 
int upload)
 
 1146     mfxFrameAllocator frame_allocator = {
 
 1159     void             **loader = &hwctx->
loader;
 
 1160     mfxSession parent_session = hwctx->
session;
 
 1164     err = MFXQueryIMPL(parent_session, &impl);
 
 1165     if (err == MFX_ERR_NONE)
 
 1166         err = MFXQueryVersion(parent_session, &ver);
 
 1167     if (err != MFX_ERR_NONE) {
 
 1173     opaque = !!(frames_hwctx->
frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
 
 1177                                  device_priv->
impl, &device_priv->
ver, session, loader);
 
 1181     if (device_priv->
handle) {
 
 1182         err = MFXVideoCORE_SetHandle(*session, device_priv->
handle_type,
 
 1184         if (err != MFX_ERR_NONE) {
 
 1191         err = MFXJoinSession(parent_session, *session);
 
 1192         if (err != MFX_ERR_NONE) {
 
 1200         err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
 
 1201         if (err != MFX_ERR_NONE) {
 
 1207     memset(&par, 0, 
sizeof(par));
 
 1210         par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
 
 1211                                  MFX_IOPATTERN_IN_VIDEO_MEMORY;
 
 1215         par.ExtParam    = 
s->ext_buffers;
 
 1217         par.IOPattern   = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
 
 1218                                    MFX_IOPATTERN_IN_OPAQUE_MEMORY;
 
 1222     par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
 
 1223                               MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
 
 1226     par.vpp.In = frames_hwctx->
surfaces[0].Info;
 
 1232     par.vpp.In.FrameRateExtN = 25;
 
 1233     par.vpp.In.FrameRateExtD = 1;
 
 1234     par.vpp.Out = par.vpp.In;
 
 1236     err = MFXVideoVPP_Init(*session, &par);
 
 1237     if (err != MFX_ERR_NONE) {
 
 1239                "Surface upload/download will not be possible\n");
 
 1267     opaque = !!(frames_hwctx->
frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
 
 1290             s->mem_ids[
i] = frames_hwctx->
surfaces[
i].Data.MemId;
 
 1295                                     sizeof(*
s->surface_ptrs));
 
 1296         if (!
s->surface_ptrs)
 
 1302         s->opaque_alloc.In.Surfaces   = 
s->surface_ptrs;
 
 1303         s->opaque_alloc.In.NumSurface = frames_hwctx->
nb_surfaces;
 
 1304         s->opaque_alloc.In.Type       = frames_hwctx->
frame_type;
 
 1306         s->opaque_alloc.Out = 
s->opaque_alloc.In;
 
 1308         s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
 
 1309         s->opaque_alloc.Header.BufferSz = 
sizeof(
s->opaque_alloc);
 
 1311         s->ext_buffers[0] = (mfxExtBuffer*)&
s->opaque_alloc;
 
 1315     s->session_download = 
NULL;
 
 1316     s->session_upload   = 
NULL;
 
 1318     s->session_download_init = 0;
 
 1319     s->session_upload_init   = 0;
 
 1352     fmts[0] = 
ctx->sw_format;
 
 1376                 mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->
surfaces[
i].Data.MemId;
 
 1377                 dst_hwctx->
surface_ids[
i] = *(VASurfaceID*)pair->first;
 
 1386             D3D11_TEXTURE2D_DESC texDesc;
 
 1389             dst_hwctx = dst_ctx->
hwctx;
 
 1394             if (src_hwctx->
frame_type & MFX_MEMTYPE_SHARED_RESOURCE)
 
 1395                 dst_hwctx->
MiscFlags = D3D11_RESOURCE_MISC_SHARED;
 
 1397                 mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->
surfaces[
i].Data.MemId;
 
 1399                 dst_hwctx->
texture_infos[
i].
index = pair->second == (mfxMemId)MFX_INFINITE ? (intptr_t)0 : (intptr_t)pair->second;
 
 1402             dst_hwctx->
BindFlags = texDesc.BindFlags;
 
 1415                 mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->
surfaces[
i].Data.MemId;
 
 1416                 dst_hwctx->
surfaces[
i] = (IDirect3DSurface9*)pair->first;
 
 1419             if (src_hwctx->
frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
 
 1420                 dst_hwctx->
surface_type = DXVA2_VideoDecoderRenderTarget;
 
 1422                 dst_hwctx->
surface_type = DXVA2_VideoProcessorRenderTarget;
 
 1437     mfxFrameSurface1 *surf = (mfxFrameSurface1*)
src->data[3];
 
 1440     uint8_t *child_data;
 
 1444     if (!
s->child_frames_ref)
 
 1448     switch (child_frames_ctx->device_ctx->type) {
 
 1452         mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
 
 1457         child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)pair->first;
 
 1464         mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
 
 1465         child_data = pair->first;
 
 1472         mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
 
 1473         child_data = pair->first;
 
 1481     if (dst->
format == child_frames_ctx->format) {
 
 1491             mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
 
 1492             dst->
data[0] = pair->first;
 
 1493             dst->
data[1] = pair->second == (mfxMemId)MFX_INFINITE ? (uint8_t *)0 : pair->second;
 
 1495             dst->
data[3] = child_data;
 
 1516     dummy->format        = child_frames_ctx->format;
 
 1521         mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
 
 1522         dummy->data[0] = pair->first;
 
 1523         dummy->data[1] = pair->second == (mfxMemId)MFX_INFINITE ? (uint8_t *)0 : pair->second;
 
 1525         dummy->data[3] = child_data;
 
 1541     int download = !!
src->hw_frames_ctx;
 
 1542     mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? 
src->data[3] : dst->
data[3]);
 
 1554     dummy->buf[0]        = download ? 
src->buf[0] : dst->
buf[0];
 
 1555     dummy->data[3]       = surf->Data.MemId;
 
 1556     dummy->hw_frames_ctx = 
s->child_frames_ref;
 
 1600         surface->Data.Y16 = (mfxU16 *)
frame->
data[0];
 
 1601         surface->Data.U16 = (mfxU16 *)
frame->
data[0] + 1;
 
 1602         surface->Data.V16 = (mfxU16 *)
frame->
data[0] + 3;
 
 1631         return MFX_ERR_UNSUPPORTED;
 
 1634     surface->Data.TimeStamp = 
frame->
pts;
 
 1642     atomic_int *inited  = upload ? &
s->session_upload_init : &
s->session_download_init;
 
 1643     mfxSession *session = upload ? &
s->session_upload      : &
s->session_download;
 
 1669     mfxFrameSurface1 
out = {{ 0 }};
 
 1670     mfxFrameSurface1 *in = (mfxFrameSurface1*)
src->data[3];
 
 1672     mfxSyncPoint sync = 
NULL;
 
 1676     AVFrame *tmp_frame = &
s->realigned_download_frame;
 
 1689         if (tmp_frame->format != dst->
format ||
 
 1694             tmp_frame->format = dst->
format;
 
 1703     dst_frame = realigned ? tmp_frame : dst;
 
 1705     if (!
s->session_download) {
 
 1706         if (
s->child_frames_ref)
 
 1713     out.Info = in->Info;
 
 1717         err = MFXVideoVPP_RunFrameVPPAsync(
s->session_download, in, &
out, 
NULL, &sync);
 
 1718         if (err == MFX_WRN_DEVICE_BUSY)
 
 1720     } 
while (err == MFX_WRN_DEVICE_BUSY);
 
 1722     if (err < 0 || !sync) {
 
 1728         err = MFXVideoCORE_SyncOperation(
s->session_download, sync, 1000);
 
 1729     } 
while (err == MFX_WRN_IN_EXECUTION);
 
 1736         tmp_frame->width  = dst->
width;
 
 1737         tmp_frame->height = dst->
height;
 
 1752     mfxFrameSurface1   in = {{ 0 }};
 
 1753     mfxFrameSurface1 *
out = (mfxFrameSurface1*)dst->
data[3];
 
 1754     mfxFrameInfo tmp_info;
 
 1756     mfxSyncPoint sync = 
NULL;
 
 1760     AVFrame *tmp_frame = &
s->realigned_upload_frame;
 
 1771     if (
src->height & 15 || 
src->linesize[0] & 15) {
 
 1773         if (tmp_frame->format != 
src->format ||
 
 1774             tmp_frame->width  != 
FFALIGN(
src->width, 16) ||
 
 1775             tmp_frame->height != 
FFALIGN(
src->height, 16)) {
 
 1778             tmp_frame->format = 
src->format;
 
 1780             tmp_frame->height = 
FFALIGN(
src->height, 16);
 
 1796         tmp_info = 
out->Info;
 
 1797         out->Info.CropW = 
FFMIN(
out->Info.Width,  tmp_frame->width);
 
 1798         out->Info.CropH = 
FFMIN(
out->Info.Height, tmp_frame->height);
 
 1801     src_frame = realigned ? tmp_frame : 
src;
 
 1803     if (!
s->session_upload) {
 
 1804         if (
s->child_frames_ref)
 
 1811     in.Info = 
out->Info;
 
 1815         err = MFXVideoVPP_RunFrameVPPAsync(
s->session_upload, &in, 
out, 
NULL, &sync);
 
 1816         if (err == MFX_WRN_DEVICE_BUSY)
 
 1818     } 
while (err == MFX_WRN_DEVICE_BUSY);
 
 1820     if (err < 0 || !sync) {
 
 1826         err = MFXVideoCORE_SyncOperation(
s->session_upload, sync, 1000);
 
 1827     } 
while (err == MFX_WRN_IN_EXECUTION);
 
 1834         out->Info.CropW = tmp_info.CropW;
 
 1835         out->Info.CropH = tmp_info.CropH;
 
 1850             "mapped to QSV frames.\n");
 
 1860                                                  sizeof(*
s->handle_pairs_internal));
 
 1861             if (!
s->handle_pairs_internal)
 
 1864                                              sizeof(*
s->surfaces_internal));
 
 1865             if (!
s->surfaces_internal)
 
 1870                 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
 
 1871                 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
 
 1874             dst_hwctx->
frame_type  = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
 
 1883                                                  sizeof(*
s->handle_pairs_internal));
 
 1884             if (!
s->handle_pairs_internal)
 
 1887                                              sizeof(*
s->surfaces_internal));
 
 1888             if (!
s->surfaces_internal)
 
 1893                 if (src_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
 
 1894                     s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
 
 1898                 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
 
 1901             if (src_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
 
 1902                 dst_hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
 
 1904                 dst_hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
 
 1914                                                  sizeof(*
s->handle_pairs_internal));
 
 1915             if (!
s->handle_pairs_internal)
 
 1918                                              sizeof(*
s->surfaces_internal));
 
 1919             if (!
s->surfaces_internal)
 
 1923                 s->handle_pairs_internal[
i].first = (mfxMemId)src_hwctx->
surfaces[
i];
 
 1924                 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
 
 1925                 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
 
 1928             if (src_hwctx->
surface_type == DXVA2_VideoProcessorRenderTarget)
 
 1929                 dst_hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
 
 1931                 dst_hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
 
 1939     dst_hwctx->
surfaces = 
s->surfaces_internal;
 
 1951         switch(
src->format) {
 
 1955             mfxHDLPair *pair = (mfxHDLPair*)hwctx->
surfaces[
i].Data.MemId;
 
 1956             if (*(VASurfaceID*)pair->first == (VASurfaceID)(uintptr_t)
src->data[3]) {
 
 1965             mfxHDLPair *pair = (mfxHDLPair*)hwctx->
surfaces[
i].Data.MemId;
 
 1966             if (pair->first == 
src->data[0]
 
 1967                 && (pair->second == 
src->data[1]
 
 1968                     || (pair->second == (mfxMemId)MFX_INFINITE && 
src->data[1] == (uint8_t *)0))) {
 
 1977             mfxHDLPair *pair = (mfxHDLPair*)hwctx->
surfaces[
i].Data.MemId;
 
 1978             if (pair->first == 
src->data[3]) {
 
 1988                "is not in the mapped frames context.\n");
 
 2005                                       const void *hwconfig,
 
 2045     static const struct {
 
 2049         { 
"auto",     MFX_IMPL_AUTO         },
 
 2050         { 
"sw",       MFX_IMPL_SOFTWARE     },
 
 2051         { 
"hw",       MFX_IMPL_HARDWARE     },
 
 2052         { 
"auto_any", MFX_IMPL_AUTO_ANY     },
 
 2053         { 
"hw_any",   MFX_IMPL_HARDWARE_ANY },
 
 2054         { 
"hw2",      MFX_IMPL_HARDWARE2    },
 
 2055         { 
"hw3",      MFX_IMPL_HARDWARE3    },
 
 2056         { 
"hw4",      MFX_IMPL_HARDWARE4    },
 
 2059     mfxIMPL impl = MFX_IMPL_AUTO_ANY;
 
 2064             if (!strcmp(device, impl_map[
i].
name)) {
 
 2065                 impl = impl_map[
i].impl;
 
 2069             impl = strtol(device, 
NULL, 0);
 
 2072     if (impl != MFX_IMPL_SOFTWARE) {
 
 2074             impl |= MFX_IMPL_VIA_D3D11;
 
 2076             impl |= MFX_IMPL_VIA_D3D9;
 
 2083                                         mfxIMPL implementation,
 
 2089     mfxVersion    ver = { { 3, 1 } };
 
 2091     mfxHandleType handle_type;
 
 2095     switch (child_device_ctx->
type) {
 
 2100             handle_type = MFX_HANDLE_VA_DISPLAY;
 
 2101             handle = (mfxHDL)child_device_hwctx->
display;
 
 2109             handle_type = MFX_HANDLE_D3D11_DEVICE;
 
 2110             handle = (mfxHDL)child_device_hwctx->
device;
 
 2118             handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
 
 2119             handle = (mfxHDL)child_device_hwctx->
devmgr;
 
 2133     err = MFXVideoCORE_SetHandle(hwctx->
session, handle_type, handle);
 
 2134     if (err != MFX_ERR_NONE) {
 
 2166     ctx->user_opaque = priv;
 
 2171                                         child_device_ctx, 
flags);
 
 2190     ctx->user_opaque = priv;
 
 2198                    "\"%s\".\n", e->
value);
 
 2202     } 
else if (CONFIG_D3D11VA) {  
 
 2204                "Defaulting child_device_type to AV_HWDEVICE_TYPE_D3D11VA for oneVPL." 
 2205                "Please explicitly set child device type via \"-init_hw_device\" " 
 2206                "option if needed.\n");
 
 2208     } 
else if (CONFIG_DXVA2) {
 
 2211     } 
else if (CONFIG_DXVA2) {
 
 2213                 "WARNING: defaulting child_device_type to AV_HWDEVICE_TYPE_DXVA2 for compatibility " 
 2214                 "with old commandlines. This behaviour will be removed " 
 2215                 "in the future. Please explicitly set device type via \"-init_hw_device\" option.\n");
 
 2217     } 
else if (CONFIG_D3D11VA) {
 
 2220     } 
else if (CONFIG_VAAPI) {
 
 2227 #if CONFIG_VAAPI && defined(_WIN32) 
 2232                 "\"%s\".\n", e->
value);
 
 2237     child_device_opts = 
NULL;
 
 2238     switch (child_device_type) {
 
 2247             av_dict_set(&child_device_opts, 
"kernel_driver", 
"i915", 0);
 
 2248             av_dict_set(&child_device_opts, 
"driver",        
"iHD",  0);
 
 2258             av_dict_set(&child_device_opts, 
"vendor_id",        
"0x8086",  0);
 
 2267                    "d3d11va is not available or child device type is set to dxva2 " 
 2268                    "explicitly for oneVPL.\n");
 
 2283                                  e ? e->
value : 
NULL, child_device_opts, 0);