21 #include <mfx/mfxvideo.h>
22 #include <mfx/mfxplugin.h>
23 #include <mfx/mfxjpeg.h>
39 #if QSV_VERSION_ATLEAST(1, 12)
40 #include "mfx/mfxvp8.h"
48 #if QSV_VERSION_ATLEAST(1, 8)
50 return MFX_CODEC_HEVC;
54 return MFX_CODEC_MPEG2;
57 #if QSV_VERSION_ATLEAST(1, 12)
62 return MFX_CODEC_JPEG;
76 #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, MFX_PROFILE_ ## v }
77 MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2_SIMPLE ),
78 MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2_MAIN ),
79 MAP(MPEG2VIDEO, MPEG2_HIGH, MPEG2_HIGH ),
81 MAP(H264, H264_BASELINE, AVC_BASELINE ),
82 MAP(H264, H264_CONSTRAINED_BASELINE, AVC_BASELINE),
83 #if QSV_VERSION_ATLEAST(1, 3)
84 MAP(H264, H264_EXTENDED, AVC_EXTENDED ),
86 MAP(H264, H264_MAIN, AVC_MAIN ),
87 MAP(H264, H264_HIGH, AVC_HIGH ),
88 MAP(H264, H264_HIGH_422, AVC_HIGH_422 ),
90 #if QSV_VERSION_ATLEAST(1, 8)
91 MAP(HEVC, HEVC_MAIN, HEVC_MAIN ),
92 MAP(HEVC, HEVC_MAIN_10, HEVC_MAIN10 ),
93 MAP(HEVC, HEVC_MAIN_STILL_PICTURE, HEVC_MAINSP ),
95 #if QSV_VERSION_ATLEAST(1, 16)
96 MAP(HEVC, HEVC_REXT, HEVC_REXT ),
99 MAP(VC1, VC1_SIMPLE, VC1_SIMPLE ),
100 MAP(VC1, VC1_MAIN, VC1_MAIN ),
101 MAP(VC1, VC1_COMPLEX, VC1_ADVANCED ),
102 MAP(VC1, VC1_ADVANCED, VC1_ADVANCED ),
110 return MFX_PROFILE_UNKNOWN;
119 return MFX_PROFILE_UNKNOWN;
125 return MFX_LEVEL_UNKNOWN;
135 static const struct {
140 { MFX_ERR_NONE, 0,
"success" },
142 { MFX_ERR_NULL_PTR,
AVERROR(EINVAL),
"NULL pointer" },
143 { MFX_ERR_UNSUPPORTED,
AVERROR(ENOSYS),
"unsupported" },
144 { MFX_ERR_MEMORY_ALLOC,
AVERROR(ENOMEM),
"failed to allocate memory" },
145 { MFX_ERR_NOT_ENOUGH_BUFFER,
AVERROR(ENOMEM),
"insufficient input/output buffer" },
146 { MFX_ERR_INVALID_HANDLE,
AVERROR(EINVAL),
"invalid handle" },
147 { MFX_ERR_LOCK_MEMORY,
AVERROR(EIO),
"failed to lock the memory block" },
148 { MFX_ERR_NOT_INITIALIZED,
AVERROR_BUG,
"not initialized" },
149 { MFX_ERR_NOT_FOUND,
AVERROR(ENOSYS),
"specified object was not found" },
153 { MFX_ERR_MORE_SURFACE,
AVERROR_UNKNOWN,
"expect more surface at output" },
154 { MFX_ERR_MORE_BITSTREAM,
AVERROR_UNKNOWN,
"expect more bitstream at output" },
156 { MFX_ERR_DEVICE_LOST,
AVERROR(EIO),
"device lost" },
157 { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM,
AVERROR(EINVAL),
"incompatible video parameters" },
158 { MFX_ERR_INVALID_VIDEO_PARAM,
AVERROR(EINVAL),
"invalid video parameters" },
159 { MFX_ERR_UNDEFINED_BEHAVIOR,
AVERROR_BUG,
"undefined behavior" },
160 { MFX_ERR_DEVICE_FAILED,
AVERROR(EIO),
"device failed" },
161 { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM,
AVERROR(EINVAL),
"incompatible audio parameters" },
162 { MFX_ERR_INVALID_AUDIO_PARAM,
AVERROR(EINVAL),
"invalid audio parameters" },
164 { MFX_WRN_IN_EXECUTION, 0,
"operation in execution" },
165 { MFX_WRN_DEVICE_BUSY, 0,
"device busy" },
166 { MFX_WRN_VIDEO_PARAM_CHANGED, 0,
"video parameters changed" },
167 { MFX_WRN_PARTIAL_ACCELERATION, 0,
"partial acceleration" },
168 { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0,
"incompatible video parameters" },
169 { MFX_WRN_VALUE_NOT_CHANGED, 0,
"value is saturated" },
170 { MFX_WRN_OUT_OF_RANGE, 0,
"value out of range" },
171 { MFX_WRN_FILTER_SKIPPED, 0,
"filter skipped" },
172 { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0,
"incompatible audio parameters" },
186 *
desc =
"unknown error";
191 const char *error_string)
201 const char *warning_string)
226 *
fourcc = MFX_FOURCC_NV12;
230 *
fourcc = MFX_FOURCC_P010;
240 for (
i = 0;
i <
ctx->nb_mids;
i++) {
251 switch (mfx_pic_type & 0x7) {
252 case MFX_FRAMETYPE_I:
253 if (mfx_pic_type & MFX_FRAMETYPE_S)
258 case MFX_FRAMETYPE_B:
261 case MFX_FRAMETYPE_P:
262 if (mfx_pic_type & MFX_FRAMETYPE_S)
267 case MFX_FRAMETYPE_UNKNOWN:
280 if (!load_plugins || !*load_plugins)
283 while (*load_plugins) {
291 if (strlen(plugin) != 2 *
sizeof(
uid.Data)) {
294 goto load_plugin_fail;
297 for (
i = 0;
i <
sizeof(
uid.Data);
i++) {
298 err = sscanf(plugin + 2 *
i,
"%2hhx",
uid.Data +
i);
302 goto load_plugin_fail;
307 ret = MFXVideoUSER_Load(session, &
uid, 1);
310 snprintf(errorbuf,
sizeof(errorbuf),
311 "Could not load the requested plugin '%s'", plugin);
313 goto load_plugin_fail;
329 const char *load_plugins)
331 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
337 ret = MFXInit(impl, &ver, session);
340 "Error initializing an internal MFX session");
348 MFXQueryIMPL(*session, &impl);
350 switch (MFX_IMPL_BASETYPE(impl)) {
351 case MFX_IMPL_SOFTWARE:
354 case MFX_IMPL_HARDWARE:
355 case MFX_IMPL_HARDWARE2:
356 case MFX_IMPL_HARDWARE3:
357 case MFX_IMPL_HARDWARE4:
358 desc =
"hardware accelerated";
365 "Initialized an internal MFX session using %s implementation\n",
382 int nb_surfaces = frames_hwctx->nb_surfaces;
406 for (
i = 0;
i < nb_surfaces;
i++) {
408 mid->
handle = frames_hwctx->surfaces[
i].Data.MemId;
421 int nb_surfaces = frames_hwctx->nb_surfaces;
431 for (
i = 0;
i < nb_surfaces;
i++)
432 resp->mids[
i] = &mids[
i];
433 resp->NumFrameActual = nb_surfaces;
435 resp->mids[resp->NumFrameActual] = (mfxMemId)
av_buffer_ref(hw_frames_ref);
436 if (!resp->mids[resp->NumFrameActual]) {
441 resp->mids[resp->NumFrameActual + 1] =
av_buffer_ref(mids_buf);
442 if (!resp->mids[resp->NumFrameActual + 1]) {
452 mfxFrameAllocResponse *resp)
459 if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
460 MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) ||
461 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)))
462 return MFX_ERR_UNSUPPORTED;
464 if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
468 mfxFrameInfo *
i = &req->Info;
469 mfxFrameInfo *i1 = &frames_hwctx->surfaces[0].Info;
471 if (
i->Width > i1->Width ||
i->Height > i1->Height ||
472 i->FourCC != i1->FourCC ||
i->ChromaFormat != i1->ChromaFormat) {
474 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
475 i->Width,
i->Height,
i->FourCC,
i->ChromaFormat,
476 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
477 return MFX_ERR_UNSUPPORTED;
483 "Error filling an external frame allocation request\n");
484 return MFX_ERR_MEMORY_ALLOC;
486 }
else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
489 mfxFrameInfo *
i = &req->Info;
497 return MFX_ERR_MEMORY_ALLOC;
500 frames_hwctx = frames_ctx->hwctx;
504 frames_ctx->width =
i->Width;
505 frames_ctx->height =
i->Height;
506 frames_ctx->initial_pool_size = req->NumFrameSuggested;
508 frames_hwctx->frame_type = req->Type;
513 "Error initializing a frames context for an internal frame "
514 "allocation request\n");
516 return MFX_ERR_MEMORY_ALLOC;
522 return MFX_ERR_MEMORY_ALLOC;
530 "Error filling an internal frame allocation request\n");
531 return MFX_ERR_MEMORY_ALLOC;
534 return MFX_ERR_UNSUPPORTED;
556 return MFX_ERR_UNDEFINED_BEHAVIOR;
561 return MFX_ERR_MEMORY_ALLOC;
584 qsv_mid->
surf.Info = hw_frames_hwctx->surfaces[0].Info;
602 return MFX_ERR_MEMORY_ALLOC;
626 MFX_HANDLE_VA_DISPLAY,
627 MFX_HANDLE_D3D9_DEVICE_MANAGER,
628 MFX_HANDLE_D3D11_DEVICE,
632 mfxSession parent_session = device_hwctx->session;
637 mfxHDL handle =
NULL;
643 err = MFXQueryIMPL(parent_session, &impl);
644 if (err == MFX_ERR_NONE)
645 err = MFXQueryVersion(parent_session, &ver);
646 if (err != MFX_ERR_NONE)
648 "Error querying the session attributes");
651 err = MFXVideoCORE_GetHandle(parent_session,
handle_types[
i], &handle);
652 if (err == MFX_ERR_NONE) {
660 "from the session\n");
663 err = MFXInit(impl, &ver, &session);
664 if (err != MFX_ERR_NONE)
666 "Error initializing a child MFX session");
669 err = MFXVideoCORE_SetHandle(session,
handle_type, handle);
670 if (err != MFX_ERR_NONE)
672 "Error setting a HW handle");
676 err = MFXJoinSession(parent_session, session);
677 if (err != MFX_ERR_NONE)
679 "Error joining session");
694 const char *load_plugins,
int opaque)
696 mfxFrameAllocator frame_allocator = {
697 .pthis = qsv_frames_ctx,
719 qsv_frames_ctx->
logctx = avctx;
727 qsv_frames_ctx->
nb_mids = frames_hwctx->nb_surfaces;
729 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
730 if (err != MFX_ERR_NONE)
732 "Error setting a frame allocator");