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)
131 static const struct {
141 MFX_FOURCC_YUY2, 0 },
144 MFX_FOURCC_UYVY, 0 },
147 MFX_FOURCC_Y210, 1 },
151 MFX_FOURCC_AYUV, 0 },
155 MFX_FOURCC_Y410, 0 },
156 #if QSV_VERSION_ATLEAST(1, 31)
160 MFX_FOURCC_P016, 1 },
164 MFX_FOURCC_Y216, 1 },
168 MFX_FOURCC_Y416, 1 },
185 mfxHDLPair *handle_pair;
186 handle_pair = surf->Data.MemId;
187 switch (base_dev_type) {
190 base_handle[0] = handle_pair->first;
195 base_handle[0] = handle_pair->first;
196 base_handle[1] = handle_pair->second;
201 base_handle[0] = handle_pair->first;
229 static uint32_t qsv_get_d3d11va_bind_flags(
int mem_type)
231 uint32_t bind_flags = 0;
233 if ((mem_type & MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) && (mem_type & MFX_MEMTYPE_INTERNAL_FRAME))
234 bind_flags = D3D11_BIND_DECODER | D3D11_BIND_VIDEO_ENCODER;
236 bind_flags = D3D11_BIND_DECODER;
238 if ((MFX_MEMTYPE_FROM_VPPOUT & mem_type) || (MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET & mem_type))
239 bind_flags = D3D11_BIND_RENDER_TARGET;
248 int i, planes_nb = 0;
249 if (
dst->format !=
src->format)
254 for (
i = 0;
i <
desc->nb_components;
i++)
255 planes_nb =
FFMAX(planes_nb,
desc->comp[
i].plane + 1);
257 for (
i = 0;
i < planes_nb;
i++) {
258 int sheight, dheight, y;
266 if (swidth < 0 || dwidth < 0) {
270 sheight =
src->height;
271 dheight =
dst->height;
277 for (y = 0; y < sheight; y++) {
278 void *line_ptr =
dst->data[
i] + y*
dst->linesize[
i] + swidth;
280 comp.depth > 8 ? 2 : 1,
284 for (y = sheight; y < dheight; y++) {
285 memcpy(
dst->data[
i]+y*
dst->linesize[
i],
286 dst->data[
i]+(sheight-1)*
dst->linesize[
i],
297 int hw_handle_supported = 0;
298 mfxHandleType handle_type;
303 err = MFXQueryIMPL(hwctx->
session, &
s->impl);
304 if (err == MFX_ERR_NONE)
305 err = MFXQueryVersion(hwctx->
session, &
s->ver);
306 if (err != MFX_ERR_NONE) {
313 handle_type = MFX_HANDLE_VA_DISPLAY;
316 hw_handle_supported = 1;
320 handle_type = MFX_HANDLE_D3D11_DEVICE;
323 hw_handle_supported = 1;
327 handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
330 hw_handle_supported = 1;
334 if (hw_handle_supported) {
335 err = MFXVideoCORE_GetHandle(hwctx->
session, handle_type, &
s->handle);
336 if (err == MFX_ERR_NONE) {
337 s->handle_type = handle_type;
338 s->child_device_type = device_type;
344 "from the session\n");
353 if (
s->session_download) {
354 MFXVideoVPP_Close(
s->session_download);
355 MFXClose(
s->session_download);
357 s->session_download =
NULL;
358 s->session_download_init = 0;
360 if (
s->session_upload) {
361 MFXVideoVPP_Close(
s->session_upload);
362 MFXClose(
s->session_upload);
364 s->session_upload =
NULL;
365 s->session_upload_init = 0;
391 mfxHDLPair *hdl_pair = (mfxHDLPair *)qsv_surface->
mfx_surface.Data.MemId;
394 if (!
s->child_frames_ref)
398 if (!child_frames_ctx->device_ctx)
418 s->nb_surfaces_used++;
432 mfxHDLPair *handle_pairs_internal =
NULL;
435 if (!
s->child_frames_ref)
445 "QSV on dxva2 requires a fixed frame pool size\n");
450 qsv_surface =
av_calloc(1,
sizeof(*qsv_surface));
462 handle_pairs_internal =
av_calloc(1,
sizeof(*handle_pairs_internal));
463 if (!handle_pairs_internal)
472 VASurfaceID *surface_id_internal;
474 surface_id_internal =
av_calloc(1,
sizeof(*surface_id_internal));
475 if (!surface_id_internal)
478 *surface_id_internal = (VASurfaceID)(uintptr_t)qsv_surface->
child_frame->
data[3];
479 handle_pairs_internal->first = (mfxHDL)surface_id_internal;
480 handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
487 handle_pairs_internal->first = (mfxMemId)qsv_surface->
child_frame->
data[0];
489 if (child_frames_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET)
490 handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
492 handle_pairs_internal->second = (mfxMemId)qsv_surface->
child_frame->
data[1];
497 qsv_surface->
mfx_surface.Data.MemId = (mfxMemId)handle_pairs_internal;
538 if (!device_priv->
handle) {
540 "Cannot create a non-opaque internal surface pool without "
541 "a hardware handle\n");
546 if (!child_device_ref)
553 child_device_hwctx->
display = (VADisplay)device_priv->
handle;
566 child_device_hwctx->
devmgr = (IDirect3DDeviceManager9*)device_priv->
handle;
577 if (!child_frames_ref) {
593 hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
594 if (hwctx->
frame_type & MFX_MEMTYPE_SHARED_RESOURCE)
595 child_frames_hwctx->
MiscFlags = D3D11_RESOURCE_MISC_SHARED;
602 if (hwctx->
frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
603 child_frames_hwctx->
surface_type = DXVA2_VideoProcessorRenderTarget;
605 child_frames_hwctx->
surface_type = DXVA2_VideoDecoderRenderTarget;
618 for (
i = 0;
i <
ctx->initial_pool_size;
i++) {
619 s->handle_pairs_internal[
i].first = child_frames_hwctx->
surface_ids +
i;
620 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
621 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
623 hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
629 for (
i = 0;
i <
ctx->initial_pool_size;
i++) {
631 if(child_frames_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
632 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
636 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
638 if (child_frames_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
639 hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
641 hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
648 for (
i = 0;
i <
ctx->initial_pool_size;
i++) {
649 s->handle_pairs_internal[
i].first = (mfxMemId)child_frames_hwctx->
surfaces[
i];
650 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
651 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
653 if (child_frames_hwctx->
surface_type == DXVA2_VideoProcessorRenderTarget)
654 hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
656 hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
660 s->child_frames_ref = child_frames_ref;
661 child_frames_ref =
NULL;
682 surf->Info.BitDepthLuma =
desc->comp[0].depth;
683 surf->Info.BitDepthChroma =
desc->comp[0].depth;
686 if (
desc->log2_chroma_w &&
desc->log2_chroma_h)
687 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
688 else if (
desc->log2_chroma_w)
689 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
691 surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
693 surf->Info.FourCC =
fourcc;
695 surf->Info.CropW =
ctx->width;
697 surf->Info.CropH =
ctx->height;
698 surf->Info.FrameRateExtN = 25;
699 surf->Info.FrameRateExtD = 1;
700 surf->Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
712 if (
ctx->initial_pool_size < 0) {
715 }
else if (
ctx->initial_pool_size == 0) {
716 mfxFrameSurface1 mfx_surf1;
727 memset(&mfx_surf1, 0,
sizeof(mfx_surf1));
729 s->frame_info = mfx_surf1.Info;
730 frames_hwctx->
info = &
s->frame_info;
737 sizeof(*
s->handle_pairs_internal));
738 if (!
s->handle_pairs_internal)
742 sizeof(*
s->surfaces_internal));
743 if (!
s->surfaces_internal)
746 for (
i = 0;
i <
ctx->initial_pool_size;
i++) {
753 if (!(frames_hwctx->
frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
769 frames_hwctx->
surfaces =
s->surfaces_internal;
775 static mfxStatus
frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
776 mfxFrameAllocResponse *resp)
781 mfxFrameInfo *
i = &req->Info;
784 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
785 !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
786 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
787 return MFX_ERR_UNSUPPORTED;
788 if (
i->Width > i1->Width ||
i->Height > i1->Height ||
789 i->FourCC != i1->FourCC ||
i->ChromaFormat != i1->ChromaFormat) {
791 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
792 i->Width,
i->Height,
i->FourCC,
i->ChromaFormat,
793 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
794 return MFX_ERR_UNSUPPORTED;
797 resp->mids =
s->mem_ids;
803 static mfxStatus
frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
808 static mfxStatus
frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
810 return MFX_ERR_UNSUPPORTED;
813 static mfxStatus
frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
815 return MFX_ERR_UNSUPPORTED;
820 mfxHDLPair *pair_dst = (mfxHDLPair*)hdl;
821 mfxHDLPair *pair_src = (mfxHDLPair*)mid;
823 pair_dst->first = pair_src->first;
825 if (pair_src->second != (mfxMemId)MFX_INFINITE)
826 pair_dst->second = pair_src->second;
832 static int qsv_d3d11_update_config(
void *
ctx, mfxHDL handle, mfxConfig cfg)
837 IDXGIAdapter *pDXGIAdapter;
838 DXGI_ADAPTER_DESC adapterDesc;
839 IDXGIDevice *pDXGIDevice =
NULL;
842 mfxVariant impl_value = {0};
844 hr = ID3D11Device_QueryInterface(device, &IID_IDXGIDevice, (
void**)&pDXGIDevice);
846 hr = IDXGIDevice_GetAdapter(pDXGIDevice, &pDXGIAdapter);
849 IDXGIDevice_Release(pDXGIDevice);
853 hr = IDXGIAdapter_GetDesc(pDXGIAdapter, &adapterDesc);
863 impl_value.Type = MFX_VARIANT_TYPE_U16;
864 impl_value.Data.U16 = adapterDesc.DeviceId;
865 sts = MFXSetConfigFilterProperty(cfg,
866 (
const mfxU8 *)
"mfxExtendedDeviceId.DeviceID", impl_value);
867 if (sts != MFX_ERR_NONE) {
869 "DeviceID property: %d.\n", sts);
873 impl_value.Type = MFX_VARIANT_TYPE_PTR;
874 impl_value.Data.Ptr = &adapterDesc.AdapterLuid;
875 sts = MFXSetConfigFilterProperty(cfg,
876 (
const mfxU8 *)
"mfxExtendedDeviceId.DeviceLUID", impl_value);
877 if (sts != MFX_ERR_NONE) {
879 "DeviceLUID property: %d.\n", sts);
883 impl_value.Type = MFX_VARIANT_TYPE_U32;
884 impl_value.Data.U32 = 0x0001;
885 sts = MFXSetConfigFilterProperty(cfg,
886 (
const mfxU8 *)
"mfxExtendedDeviceId.LUIDDeviceNodeMask", impl_value);
887 if (sts != MFX_ERR_NONE) {
889 "LUIDDeviceNodeMask property: %d.\n", sts);
896 IDXGIAdapter_Release(pDXGIAdapter);
897 IDXGIDevice_Release(pDXGIDevice);
902 static int qsv_d3d9_update_config(
void *
ctx, mfxHDL handle, mfxConfig cfg)
907 IDirect3DDeviceManager9* devmgr = handle;
908 IDirect3DDevice9 *device =
NULL;
909 IDirect3DDevice9Ex *device_ex =
NULL;
910 HANDLE device_handle = 0;
911 IDirect3D9Ex *d3d9ex =
NULL;
912 IDirect3D9 *d3d9 =
NULL;
914 D3DDEVICE_CREATION_PARAMETERS params;
916 mfxVariant impl_value = {0};
918 hr = IDirect3DDeviceManager9_OpenDeviceHandle(devmgr, &device_handle);
924 hr = IDirect3DDeviceManager9_LockDevice(devmgr, device_handle, &device, TRUE);
927 IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
930 hr = IDirect3DDevice9_QueryInterface(device, &IID_IDirect3DDevice9Ex, (
void **)&device_ex);
931 IDirect3DDevice9_Release(device);
937 hr = IDirect3DDevice9Ex_GetCreationParameters(device_ex, ¶ms);
940 IDirect3DDevice9Ex_Release(device_ex);
944 hr = IDirect3DDevice9Ex_GetDirect3D(device_ex, &d3d9);
947 IDirect3DDevice9Ex_Release(device_ex);
950 hr = IDirect3D9_QueryInterface(d3d9, &IID_IDirect3D9Ex, (
void **)&d3d9ex);
951 IDirect3D9_Release(d3d9);
954 IDirect3DDevice9Ex_Release(device_ex);
958 hr = IDirect3D9Ex_GetAdapterLUID(d3d9ex, params.AdapterOrdinal, &luid);
964 impl_value.Type = MFX_VARIANT_TYPE_PTR;
965 impl_value.Data.Ptr = &luid;
966 sts = MFXSetConfigFilterProperty(cfg,
967 (
const mfxU8 *)
"mfxExtendedDeviceId.DeviceLUID", impl_value);
968 if (sts != MFX_ERR_NONE) {
970 "DeviceLUID property: %d.\n", sts);
977 IDirect3D9Ex_Release(d3d9ex);
978 IDirect3DDevice9Ex_Release(device_ex);
981 IDirect3DDeviceManager9_UnlockDevice(devmgr, device_handle, FALSE);
982 IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
988 static int qsv_va_update_config(
void *
ctx, mfxHDL handle, mfxConfig cfg)
991 #if VA_CHECK_VERSION(1, 15, 0)
993 VADisplay dpy = handle;
995 VADisplayAttribute attr = {
996 .type = VADisplayPCIID,
998 mfxVariant impl_value = {0};
1000 vas = vaGetDisplayAttributes(dpy, &attr, 1);
1001 if (vas == VA_STATUS_SUCCESS && attr.flags != VA_DISPLAY_ATTRIB_NOT_SUPPORTED) {
1002 impl_value.Type = MFX_VARIANT_TYPE_U16;
1003 impl_value.Data.U16 = (attr.value & 0xFFFF);
1004 sts = MFXSetConfigFilterProperty(cfg,
1005 (
const mfxU8 *)
"mfxImplDescription.mfxDeviceDescription.DeviceID", impl_value);
1006 if (sts != MFX_ERR_NONE) {
1008 "DeviceID property: %d.\n", sts);
1013 "consider to upgrade the driver to support VA-API 1.15.0\n");
1022 "the device information from the driver. Please consider to upgrade libva to "
1023 "support VA-API 1.15.0\n");
1029 static int qsv_new_mfx_loader(
void *
ctx,
1031 mfxHandleType handle_type,
1032 mfxIMPL implementation,
1037 mfxLoader loader =
NULL;
1039 mfxVariant impl_value = {0};
1049 cfg = MFXCreateConfig(loader);
1055 impl_value.Type = MFX_VARIANT_TYPE_U32;
1056 impl_value.Data.U32 = (implementation == MFX_IMPL_SOFTWARE) ?
1057 MFX_IMPL_TYPE_SOFTWARE : MFX_IMPL_TYPE_HARDWARE;
1058 sts = MFXSetConfigFilterProperty(cfg,
1059 (
const mfxU8 *)
"mfxImplDescription.Impl", impl_value);
1060 if (sts != MFX_ERR_NONE) {
1062 "property: %d.\n", sts);
1066 impl_value.Type = MFX_VARIANT_TYPE_U32;
1067 impl_value.Data.U32 = pver->Version;
1068 sts = MFXSetConfigFilterProperty(cfg,
1069 (
const mfxU8 *)
"mfxImplDescription.ApiVersion.Version",
1071 if (sts != MFX_ERR_NONE) {
1073 "property: %d.\n", sts);
1077 impl_value.Type = MFX_VARIANT_TYPE_U32;
1078 impl_value.Data.U32 = 0x8086;
1079 sts = MFXSetConfigFilterProperty(cfg,
1080 (
const mfxU8 *)
"mfxImplDescription.VendorID", impl_value);
1081 if (sts != MFX_ERR_NONE) {
1083 "VendorID property: %d.\n", sts);
1087 if (MFX_HANDLE_VA_DISPLAY == handle_type) {
1088 if (handle && qsv_va_update_config(
ctx, handle, cfg))
1091 impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_VAAPI;
1092 }
else if (MFX_HANDLE_D3D9_DEVICE_MANAGER == handle_type) {
1093 if (handle && qsv_d3d9_update_config(
ctx, handle, cfg))
1096 impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_D3D9;
1098 if (handle && qsv_d3d11_update_config(
ctx, handle, cfg))
1101 impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_D3D11;
1104 impl_value.Type = MFX_VARIANT_TYPE_U32;
1105 sts = MFXSetConfigFilterProperty(cfg,
1106 (
const mfxU8 *)
"mfxImplDescription.AccelerationMode", impl_value);
1107 if (sts != MFX_ERR_NONE) {
1109 "AccelerationMode property: %d.\n", sts);
1124 static int qsv_create_mfx_session_from_loader(
void *
ctx, mfxLoader loader, mfxSession *psession)
1127 mfxSession session =
NULL;
1128 uint32_t impl_idx = 0;
1133 mfxImplDescription *impl_desc;
1135 sts = MFXEnumImplementations(loader, impl_idx,
1136 MFX_IMPLCAPS_IMPLDESCSTRUCTURE,
1137 (mfxHDL *)&impl_desc);
1139 if (sts == MFX_ERR_NOT_FOUND)
1141 else if (sts != MFX_ERR_NONE) {
1146 sts = MFXCreateSession(loader, impl_idx, &session);
1147 MFXDispReleaseImplDescription(loader, impl_desc);
1148 if (sts == MFX_ERR_NONE)
1154 if (sts != MFX_ERR_NONE) {
1159 sts = MFXQueryVersion(session, &ver);
1160 if (sts != MFX_ERR_NONE) {
1166 "version is %d.%d\n", ver.Major, ver.Minor);
1168 *psession = session;
1181 mfxHandleType handle_type,
1182 mfxIMPL implementation,
1184 mfxSession *psession,
1187 mfxLoader loader =
NULL;
1190 "Use Intel(R) oneVPL to create MFX session, API version is "
1191 "%d.%d, the required implementation version is %d.%d\n",
1192 MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
1194 if (handle_type != MFX_HANDLE_VA_DISPLAY &&
1195 handle_type != MFX_HANDLE_D3D9_DEVICE_MANAGER &&
1196 handle_type != MFX_HANDLE_D3D11_DEVICE) {
1198 "Invalid MFX device handle type\n");
1205 if (qsv_new_mfx_loader(
ctx, handle, handle_type, implementation, pver, (
void **)&loader))
1212 if (qsv_create_mfx_session_from_loader(
ctx, loader, psession))
1221 if (!*ploader && loader)
1231 mfxHandleType handle_type,
1232 mfxIMPL implementation,
1234 mfxSession *psession,
1239 mfxSession session =
NULL;
1242 "Use Intel(R) Media SDK to create MFX session, API version is "
1243 "%d.%d, the required implementation version is %d.%d\n",
1244 MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
1249 sts = MFXInit(implementation, &ver, &session);
1250 if (sts != MFX_ERR_NONE) {
1256 sts = MFXQueryVersion(session, &ver);
1257 if (sts != MFX_ERR_NONE) {
1264 "version is %d.%d\n", ver.Major, ver.Minor);
1268 sts = MFXInit(implementation, &ver, &session);
1269 if (sts != MFX_ERR_NONE) {
1275 *psession = session;
1289 mfxSession *session,
int upload)
1297 mfxFrameAllocator frame_allocator = {
1310 void **loader = &hwctx->
loader;
1311 mfxSession parent_session = hwctx->
session;
1315 err = MFXQueryIMPL(parent_session, &impl);
1316 if (err == MFX_ERR_NONE)
1317 err = MFXQueryVersion(parent_session, &ver);
1318 if (err != MFX_ERR_NONE) {
1324 opaque = !!(frames_hwctx->
frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
1328 device_priv->
impl, &device_priv->
ver, session, loader);
1332 if (device_priv->
handle) {
1333 err = MFXVideoCORE_SetHandle(*session, device_priv->
handle_type,
1335 if (err != MFX_ERR_NONE) {
1342 err = MFXJoinSession(parent_session, *session);
1343 if (err != MFX_ERR_NONE) {
1351 err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
1352 if (err != MFX_ERR_NONE) {
1358 memset(&par, 0,
sizeof(par));
1361 par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
1362 MFX_IOPATTERN_IN_VIDEO_MEMORY;
1366 par.ExtParam =
s->ext_buffers;
1368 par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
1369 MFX_IOPATTERN_IN_OPAQUE_MEMORY;
1373 par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
1374 MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
1383 par.vpp.In.FrameRateExtN = 25;
1384 par.vpp.In.FrameRateExtD = 1;
1385 par.vpp.Out = par.vpp.In;
1387 err = MFXVideoVPP_Init(*session, &par);
1388 if (err != MFX_ERR_NONE) {
1390 "Surface upload/download will not be possible\n");
1418 opaque = !!(frames_hwctx->
frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
1441 s->mem_ids[
i] = frames_hwctx->
surfaces[
i].Data.MemId;
1446 sizeof(*
s->surface_ptrs));
1447 if (!
s->surface_ptrs)
1453 s->opaque_alloc.In.Surfaces =
s->surface_ptrs;
1454 s->opaque_alloc.In.NumSurface = frames_hwctx->
nb_surfaces;
1455 s->opaque_alloc.In.Type = frames_hwctx->
frame_type;
1457 s->opaque_alloc.Out =
s->opaque_alloc.In;
1459 s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
1460 s->opaque_alloc.Header.BufferSz =
sizeof(
s->opaque_alloc);
1462 s->ext_buffers[0] = (mfxExtBuffer*)&
s->opaque_alloc;
1466 s->session_download =
NULL;
1467 s->session_upload =
NULL;
1469 s->session_download_init = 0;
1470 s->session_upload_init = 0;
1503 fmts[0] =
ctx->sw_format;
1527 mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->
surfaces[
i].Data.MemId;
1528 dst_hwctx->
surface_ids[
i] = *(VASurfaceID*)pair->first;
1537 D3D11_TEXTURE2D_DESC texDesc;
1540 dst_hwctx = dst_ctx->
hwctx;
1545 if (src_hwctx->
frame_type & MFX_MEMTYPE_SHARED_RESOURCE)
1546 dst_hwctx->
MiscFlags = D3D11_RESOURCE_MISC_SHARED;
1548 mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->
surfaces[
i].Data.MemId;
1550 dst_hwctx->
texture_infos[
i].
index = pair->second == (mfxMemId)MFX_INFINITE ? (intptr_t)0 : (intptr_t)pair->second;
1554 dst_hwctx->
BindFlags = texDesc.BindFlags;
1569 mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->
surfaces[
i].Data.MemId;
1570 dst_hwctx->
surfaces[
i] = (IDirect3DSurface9*)pair->first;
1573 if (src_hwctx->
frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
1574 dst_hwctx->
surface_type = DXVA2_VideoDecoderRenderTarget;
1576 dst_hwctx->
surface_type = DXVA2_VideoProcessorRenderTarget;
1591 mfxFrameSurface1 *surf = (mfxFrameSurface1*)
src->data[3];
1594 uint8_t *child_data;
1598 if (!
s->child_frames_ref)
1602 switch (child_frames_ctx->device_ctx->type) {
1606 mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
1611 child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)pair->first;
1618 mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
1619 child_data = pair->first;
1626 mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
1627 child_data = pair->first;
1635 if (
dst->format == child_frames_ctx->format) {
1642 dst->height =
src->height;
1645 mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
1646 dst->data[0] = pair->first;
1647 dst->data[1] = pair->second == (mfxMemId)MFX_INFINITE ? (uint8_t *)0 : pair->second;
1649 dst->data[3] = child_data;
1670 dummy->format = child_frames_ctx->format;
1675 mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
1676 dummy->data[0] = pair->first;
1677 dummy->data[1] = pair->second == (mfxMemId)MFX_INFINITE ? (uint8_t *)0 : pair->second;
1679 dummy->data[3] = child_data;
1695 int download = !!
src->hw_frames_ctx;
1696 mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ?
src->data[3] :
dst->data[3]);
1708 dummy->buf[0] = download ?
src->buf[0] :
dst->buf[0];
1709 dummy->data[3] = surf->Data.MemId;
1710 dummy->hw_frames_ctx =
s->child_frames_ref;
1726 switch (
frame->format) {
1730 surface->Data.Y =
frame->data[0];
1731 surface->Data.UV =
frame->data[1];
1735 surface->Data.Y =
frame->data[0];
1736 surface->Data.U =
frame->data[1];
1737 surface->Data.V =
frame->data[2];
1741 surface->Data.B =
frame->data[0];
1742 surface->Data.G =
frame->data[0] + 1;
1743 surface->Data.R =
frame->data[0] + 2;
1744 surface->Data.A =
frame->data[0] + 3;
1747 surface->Data.Y =
frame->data[0];
1748 surface->Data.U =
frame->data[0] + 1;
1749 surface->Data.V =
frame->data[0] + 3;
1754 surface->Data.Y16 = (mfxU16 *)
frame->data[0];
1755 surface->Data.U16 = (mfxU16 *)
frame->data[0] + 1;
1756 surface->Data.V16 = (mfxU16 *)
frame->data[0] + 3;
1759 surface->Data.V =
frame->data[0];
1760 surface->Data.U =
frame->data[0] + 1;
1761 surface->Data.Y =
frame->data[0] + 2;
1764 surface->Data.A =
frame->data[0] + 3;
1767 surface->Data.U =
frame->data[0];
1770 surface->Data.U =
frame->data[0];
1771 surface->Data.Y =
frame->data[0] + 2;
1772 surface->Data.V =
frame->data[0] + 4;
1775 surface->Data.A =
frame->data[0] + 6;
1779 surface->Data.Y =
frame->data[0] + 1;
1780 surface->Data.U =
frame->data[0];
1781 surface->Data.V =
frame->data[0] + 2;
1785 return MFX_ERR_UNSUPPORTED;
1787 surface->Data.Pitch =
frame->linesize[0];
1788 surface->Data.TimeStamp =
frame->pts;
1796 atomic_int *inited = upload ? &
s->session_upload_init : &
s->session_download_init;
1797 mfxSession *session = upload ? &
s->session_upload : &
s->session_download;
1823 mfxFrameSurface1
out = {{ 0 }};
1824 mfxFrameSurface1 *in = (mfxFrameSurface1*)
src->data[3];
1826 mfxSyncPoint sync =
NULL;
1830 AVFrame *tmp_frame = &
s->realigned_download_frame;
1841 if (
dst->height & 15 ||
dst->linesize[0] & 15) {
1843 if (tmp_frame->format !=
dst->format ||
1844 tmp_frame->width !=
FFALIGN(
dst->linesize[0], 16) ||
1845 tmp_frame->height !=
FFALIGN(
dst->height, 16)) {
1848 tmp_frame->format =
dst->format;
1849 tmp_frame->width =
FFALIGN(
dst->linesize[0], 16);
1850 tmp_frame->height =
FFALIGN(
dst->height, 16);
1857 dst_frame = realigned ? tmp_frame :
dst;
1859 if (!
s->session_download) {
1860 if (
s->child_frames_ref)
1867 out.Info = in->Info;
1871 err = MFXVideoVPP_RunFrameVPPAsync(
s->session_download, in, &
out,
NULL, &sync);
1872 if (err == MFX_WRN_DEVICE_BUSY)
1874 }
while (err == MFX_WRN_DEVICE_BUSY);
1876 if (err < 0 || !sync) {
1882 err = MFXVideoCORE_SyncOperation(
s->session_download, sync, 1000);
1883 }
while (err == MFX_WRN_IN_EXECUTION);
1890 tmp_frame->width =
dst->width;
1891 tmp_frame->height =
dst->height;
1893 tmp_frame->width =
FFALIGN(
dst->linesize[0], 16);
1894 tmp_frame->height =
FFALIGN(
dst->height, 16);
1906 mfxFrameSurface1 in = {{ 0 }};
1907 mfxFrameSurface1 *
out = (mfxFrameSurface1*)
dst->data[3];
1908 mfxFrameInfo tmp_info;
1910 mfxSyncPoint sync =
NULL;
1914 AVFrame *tmp_frame = &
s->realigned_upload_frame;
1925 if (
src->height & 15 ||
src->linesize[0] & 15) {
1927 if (tmp_frame->format !=
src->format ||
1928 tmp_frame->width !=
FFALIGN(
src->width, 16) ||
1929 tmp_frame->height !=
FFALIGN(
src->height, 16)) {
1932 tmp_frame->format =
src->format;
1934 tmp_frame->height =
FFALIGN(
src->height, 16);
1950 tmp_info =
out->Info;
1951 out->Info.CropW =
FFMIN(
out->Info.Width, tmp_frame->width);
1952 out->Info.CropH =
FFMIN(
out->Info.Height, tmp_frame->height);
1955 src_frame = realigned ? tmp_frame :
src;
1957 if (!
s->session_upload) {
1958 if (
s->child_frames_ref)
1965 in.Info =
out->Info;
1969 err = MFXVideoVPP_RunFrameVPPAsync(
s->session_upload, &in,
out,
NULL, &sync);
1970 if (err == MFX_WRN_DEVICE_BUSY)
1972 }
while (err == MFX_WRN_DEVICE_BUSY);
1974 if (err < 0 || !sync) {
1980 err = MFXVideoCORE_SyncOperation(
s->session_upload, sync, 1000);
1981 }
while (err == MFX_WRN_IN_EXECUTION);
1988 out->Info.CropW = tmp_info.CropW;
1989 out->Info.CropH = tmp_info.CropH;
2000 mfxFrameSurface1 mfx_surf1;
2005 dst_hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
2014 if (src_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
2015 dst_hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
2017 dst_hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
2027 memset(&mfx_surf1, 0,
sizeof(mfx_surf1));
2029 s->frame_info = mfx_surf1.Info;
2030 dst_hwctx->
info = &
s->frame_info;
2048 sizeof(*
s->handle_pairs_internal));
2049 if (!
s->handle_pairs_internal)
2052 sizeof(*
s->surfaces_internal));
2053 if (!
s->surfaces_internal)
2058 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
2059 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
2062 dst_hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
2071 sizeof(*
s->handle_pairs_internal));
2072 if (!
s->handle_pairs_internal)
2075 sizeof(*
s->surfaces_internal));
2076 if (!
s->surfaces_internal)
2081 if (src_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
2082 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
2086 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
2089 if (src_hwctx->
BindFlags & D3D11_BIND_RENDER_TARGET) {
2090 dst_hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
2092 dst_hwctx->
frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
2102 sizeof(*
s->handle_pairs_internal));
2103 if (!
s->handle_pairs_internal)
2106 sizeof(*
s->surfaces_internal));
2107 if (!
s->surfaces_internal)
2111 s->handle_pairs_internal[
i].first = (mfxMemId)src_hwctx->
surfaces[
i];
2112 s->handle_pairs_internal[
i].second = (mfxMemId)MFX_INFINITE;
2113 s->surfaces_internal[
i].Data.MemId = (mfxMemId)&
s->handle_pairs_internal[
i];
2116 if (src_hwctx->
surface_type == DXVA2_VideoProcessorRenderTarget)
2117 dst_hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
2119 dst_hwctx->
frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
2127 dst_hwctx->
surfaces =
s->surfaces_internal;
2152 switch(
src->format) {
2156 mfxHDLPair *pair = (mfxHDLPair*)hwctx->
surfaces[
i].Data.MemId;
2157 if (*(VASurfaceID*)pair->first == (VASurfaceID)(uintptr_t)
src->data[3]) {
2166 mfxHDLPair *pair = (mfxHDLPair*)hwctx->
surfaces[
i].Data.MemId;
2167 if (pair->first ==
src->data[0]
2168 && (pair->second ==
src->data[1]
2169 || (pair->second == (mfxMemId)MFX_INFINITE &&
src->data[1] == (uint8_t *)0))) {
2178 mfxHDLPair *pair = (mfxHDLPair*)hwctx->
surfaces[
i].Data.MemId;
2179 if (pair->first ==
src->data[3]) {
2189 "is not in the mapped frames context.\n");
2199 dst->height =
src->height;
2207 mfxFrameSurface1 *surfaces_internal = (mfxFrameSurface1 *)hwmap->
priv;
2208 mfxHDLPair *handle_pairs_internal = (mfxHDLPair *)surfaces_internal->Data.MemId;
2211 switch (src_ctx->
format) {
2215 av_freep(&handle_pairs_internal->first);
2240 mfxFrameSurface1 *surfaces_internal =
NULL;
2241 mfxHDLPair *handle_pairs_internal =
NULL;
2244 surfaces_internal =
av_calloc(1,
sizeof(*surfaces_internal));
2245 if (!surfaces_internal) {
2250 handle_pairs_internal =
av_calloc(1,
sizeof(*handle_pairs_internal));
2251 if (!handle_pairs_internal) {
2260 switch (
src->format) {
2264 VASurfaceID *surface_id_internal;
2266 surface_id_internal =
av_calloc(1,
sizeof(*surface_id_internal));
2267 if (!surface_id_internal) {
2272 *surface_id_internal = (VASurfaceID)(uintptr_t)
src->data[3];
2273 handle_pairs_internal->first = (mfxHDL)surface_id_internal;
2274 handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
2286 handle_pairs_internal->first = (mfxMemId)
src->data[0];
2288 if (src_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
2289 handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
2291 handle_pairs_internal->second = (mfxMemId)
src->data[1];
2302 surfaces_internal->Data.MemId = (mfxMemId)handle_pairs_internal;
2310 dst->height =
src->height;
2311 dst->data[3] = (uint8_t*)surfaces_internal;
2333 const void *hwconfig,
2373 static const struct {
2377 {
"auto", MFX_IMPL_AUTO },
2378 {
"sw", MFX_IMPL_SOFTWARE },
2379 {
"hw", MFX_IMPL_HARDWARE },
2380 {
"auto_any", MFX_IMPL_AUTO_ANY },
2381 {
"hw_any", MFX_IMPL_HARDWARE_ANY },
2382 {
"hw2", MFX_IMPL_HARDWARE2 },
2383 {
"hw3", MFX_IMPL_HARDWARE3 },
2384 {
"hw4", MFX_IMPL_HARDWARE4 },
2387 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
2392 if (!strcmp(device, impl_map[
i].
name)) {
2393 impl = impl_map[
i].impl;
2397 impl = strtol(device,
NULL, 0);
2400 if (impl != MFX_IMPL_SOFTWARE) {
2402 impl |= MFX_IMPL_VIA_D3D11;
2404 impl |= MFX_IMPL_VIA_D3D9;
2411 mfxIMPL implementation,
2417 mfxVersion ver = { { 3, 1 } };
2419 mfxHandleType handle_type;
2423 switch (child_device_ctx->
type) {
2428 handle_type = MFX_HANDLE_VA_DISPLAY;
2429 handle = (mfxHDL)child_device_hwctx->
display;
2437 handle_type = MFX_HANDLE_D3D11_DEVICE;
2438 handle = (mfxHDL)child_device_hwctx->
device;
2446 handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
2447 handle = (mfxHDL)child_device_hwctx->
devmgr;
2461 err = MFXVideoCORE_SetHandle(hwctx->
session, handle_type, handle);
2462 if (err != MFX_ERR_NONE) {
2494 ctx->user_opaque = priv;
2499 child_device_ctx,
flags);
2518 ctx->user_opaque = priv;
2526 "\"%s\".\n", e->
value);
2530 }
else if (CONFIG_D3D11VA) {
2532 "Defaulting child_device_type to AV_HWDEVICE_TYPE_D3D11VA for oneVPL."
2533 "Please explicitly set child device type via \"-init_hw_device\" "
2534 "option if needed.\n");
2536 }
else if (CONFIG_DXVA2) {
2539 }
else if (CONFIG_DXVA2) {
2541 "WARNING: defaulting child_device_type to AV_HWDEVICE_TYPE_DXVA2 for compatibility "
2542 "with old commandlines. This behaviour will be removed "
2543 "in the future. Please explicitly set device type via \"-init_hw_device\" option.\n");
2545 }
else if (CONFIG_D3D11VA) {
2548 }
else if (CONFIG_VAAPI) {
2555 #if CONFIG_VAAPI && defined(_WIN32)
2560 "\"%s\".\n", e->
value);
2565 child_device_opts =
NULL;
2566 switch (child_device_type) {
2575 av_dict_set(&child_device_opts,
"vendor_id",
"0x8086", 0);
2576 av_dict_set(&child_device_opts,
"driver",
"iHD", 0);
2586 av_dict_set(&child_device_opts,
"vendor_id",
"0x8086", 0);
2595 "d3d11va is not available or child device type is set to dxva2 "
2596 "explicitly for oneVPL.\n");
2611 e ? e->
value :
NULL, child_device_opts, 0);