2 * Intel MediaSDK QSV encoder/decoder shared code
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <mfx/mfxvideo.h>
22 #include <mfx/mfxplugin.h>
23 #include <mfx/mfxjpeg.h>
28 #include "libavutil/avstring.h"
29 #include "libavutil/common.h"
30 #include "libavutil/error.h"
31 #include "libavutil/hwcontext.h"
32 #include "libavutil/hwcontext_qsv.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/avassert.h"
37 #include "qsv_internal.h"
39 #if QSV_VERSION_ATLEAST(1, 12)
40 #include "mfx/mfxvp8.h"
43 int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
46 case AV_CODEC_ID_H264:
48 #if QSV_VERSION_ATLEAST(1, 8)
49 case AV_CODEC_ID_HEVC:
50 return MFX_CODEC_HEVC;
52 case AV_CODEC_ID_MPEG1VIDEO:
53 case AV_CODEC_ID_MPEG2VIDEO:
54 return MFX_CODEC_MPEG2;
57 #if QSV_VERSION_ATLEAST(1, 12)
61 case AV_CODEC_ID_MJPEG:
62 return MFX_CODEC_JPEG;
67 return AVERROR(ENOSYS);
72 enum AVCodecID codec_id;
75 } qsv_profile_map[] = {
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 ),
106 int ff_qsv_profile_to_mfx(enum AVCodecID codec_id, int profile)
109 if (profile == FF_PROFILE_UNKNOWN)
110 return MFX_PROFILE_UNKNOWN;
112 for (i = 0; i < FF_ARRAY_ELEMS(qsv_profile_map); i++) {
113 if (qsv_profile_map[i].codec_id != codec_id)
115 if (qsv_profile_map[i].codec_profile == profile)
116 return qsv_profile_map[i].mfx_profile;
119 return MFX_PROFILE_UNKNOWN;
122 int ff_qsv_level_to_mfx(enum AVCodecID codec_id, int level)
124 if (level == FF_LEVEL_UNKNOWN)
125 return MFX_LEVEL_UNKNOWN;
128 case AV_CODEC_ID_HEVC:
135 static const struct {
140 { MFX_ERR_NONE, 0, "success" },
141 { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
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" },
150 /* the following 3 errors should always be handled explicitly, so those "mappings"
151 * are for completeness only */
152 { MFX_ERR_MORE_DATA, AVERROR_UNKNOWN, "expect more data at input" },
153 { MFX_ERR_MORE_SURFACE, AVERROR_UNKNOWN, "expect more surface at output" },
154 { MFX_ERR_MORE_BITSTREAM, AVERROR_UNKNOWN, "expect more bitstream at output" },
155 { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
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" },
175 int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
178 for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
179 if (qsv_errors[i].mfxerr == mfx_err) {
181 *desc = qsv_errors[i].desc;
182 return qsv_errors[i].averr;
186 *desc = "unknown error";
187 return AVERROR_UNKNOWN;
190 int ff_qsv_print_error(void *log_ctx, mfxStatus err,
191 const char *error_string)
195 ret = ff_qsv_map_error(err, &desc);
196 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
200 int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
201 const char *warning_string)
205 ret = ff_qsv_map_error(err, &desc);
206 av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
210 static enum AVPixelFormat qsv_map_fourcc(uint32_t fourcc)
213 case MFX_FOURCC_NV12: return AV_PIX_FMT_NV12;
214 case MFX_FOURCC_P010: return AV_PIX_FMT_P010;
215 case MFX_FOURCC_P8: return AV_PIX_FMT_PAL8;
217 return AV_PIX_FMT_NONE;
220 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
223 case AV_PIX_FMT_YUV420P:
224 case AV_PIX_FMT_YUVJ420P:
225 case AV_PIX_FMT_NV12:
226 *fourcc = MFX_FOURCC_NV12;
227 return AV_PIX_FMT_NV12;
228 case AV_PIX_FMT_YUV420P10:
229 case AV_PIX_FMT_P010:
230 *fourcc = MFX_FOURCC_P010;
231 return AV_PIX_FMT_P010;
233 return AVERROR(ENOSYS);
237 int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
240 for (i = 0; i < ctx->nb_mids; i++) {
241 QSVMid *mid = &ctx->mids[i];
242 if (mid->handle == frame->surface.Data.MemId)
248 enum AVPictureType ff_qsv_map_pictype(int mfx_pic_type)
250 enum AVPictureType type;
251 switch (mfx_pic_type & 0x7) {
252 case MFX_FRAMETYPE_I:
253 if (mfx_pic_type & MFX_FRAMETYPE_S)
254 type = AV_PICTURE_TYPE_SI;
256 type = AV_PICTURE_TYPE_I;
258 case MFX_FRAMETYPE_B:
259 type = AV_PICTURE_TYPE_B;
261 case MFX_FRAMETYPE_P:
262 if (mfx_pic_type & MFX_FRAMETYPE_S)
263 type = AV_PICTURE_TYPE_SP;
265 type = AV_PICTURE_TYPE_P;
267 case MFX_FRAMETYPE_UNKNOWN:
268 type = AV_PICTURE_TYPE_NONE;
277 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
280 if (!load_plugins || !*load_plugins)
283 while (*load_plugins) {
288 char *plugin = av_get_token(&load_plugins, ":");
290 return AVERROR(ENOMEM);
291 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
292 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
293 err = AVERROR(EINVAL);
294 goto load_plugin_fail;
297 for (i = 0; i < sizeof(uid.Data); i++) {
298 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
300 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
301 err = AVERROR(EINVAL);
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);
312 err = ff_qsv_print_error(logctx, ret, errorbuf);
313 goto load_plugin_fail;
328 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
329 const char *load_plugins)
331 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
332 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
337 ret = MFXInit(impl, &ver, session);
339 return ff_qsv_print_error(avctx, ret,
340 "Error initializing an internal MFX session");
342 ret = qsv_load_plugins(*session, load_plugins, avctx);
344 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
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";
364 av_log(avctx, AV_LOG_VERBOSE,
365 "Initialized an internal MFX session using %s implementation\n",
371 static void mids_buf_free(void *opaque, uint8_t *data)
373 AVBufferRef *hw_frames_ref = opaque;
374 av_buffer_unref(&hw_frames_ref);
378 static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
380 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
381 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
382 int nb_surfaces = frames_hwctx->nb_surfaces;
384 AVBufferRef *mids_buf, *hw_frames_ref1;
388 hw_frames_ref1 = av_buffer_ref(hw_frames_ref);
392 mids = av_mallocz_array(nb_surfaces, sizeof(*mids));
394 av_buffer_unref(&hw_frames_ref1);
398 mids_buf = av_buffer_create((uint8_t*)mids, nb_surfaces * sizeof(*mids),
399 mids_buf_free, hw_frames_ref1, 0);
401 av_buffer_unref(&hw_frames_ref1);
406 for (i = 0; i < nb_surfaces; i++) {
407 QSVMid *mid = &mids[i];
408 mid->handle = frames_hwctx->surfaces[i].Data.MemId;
409 mid->hw_frames_ref = hw_frames_ref1;
415 static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_ref,
416 AVBufferRef *mids_buf)
418 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
419 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
420 QSVMid *mids = (QSVMid*)mids_buf->data;
421 int nb_surfaces = frames_hwctx->nb_surfaces;
424 // the allocated size of the array is two larger than the number of
425 // surfaces, we store the references to the frames context and the
426 // QSVMid array there
427 resp->mids = av_mallocz_array(nb_surfaces + 2, sizeof(*resp->mids));
429 return AVERROR(ENOMEM);
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]) {
437 av_freep(&resp->mids);
438 return AVERROR(ENOMEM);
441 resp->mids[resp->NumFrameActual + 1] = av_buffer_ref(mids_buf);
442 if (!resp->mids[resp->NumFrameActual + 1]) {
443 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
444 av_freep(&resp->mids);
445 return AVERROR(ENOMEM);
451 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
452 mfxFrameAllocResponse *resp)
454 QSVFramesContext *ctx = pthis;
457 /* this should only be called from an encoder or decoder and
458 * only allocates video memory frames */
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) {
465 /* external frames -- fill from the caller-supplied frames context */
466 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
467 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
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) {
473 av_log(ctx->logctx, AV_LOG_ERROR, "Mismatching surface properties in an "
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;
480 ret = qsv_setup_mids(resp, ctx->hw_frames_ctx, ctx->mids_buf);
482 av_log(ctx->logctx, AV_LOG_ERROR,
483 "Error filling an external frame allocation request\n");
484 return MFX_ERR_MEMORY_ALLOC;
486 } else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
487 /* internal frames -- allocate a new hw frames context */
488 AVHWFramesContext *ext_frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
489 mfxFrameInfo *i = &req->Info;
491 AVBufferRef *frames_ref, *mids_buf;
492 AVHWFramesContext *frames_ctx;
493 AVQSVFramesContext *frames_hwctx;
495 frames_ref = av_hwframe_ctx_alloc(ext_frames_ctx->device_ref);
497 return MFX_ERR_MEMORY_ALLOC;
499 frames_ctx = (AVHWFramesContext*)frames_ref->data;
500 frames_hwctx = frames_ctx->hwctx;
502 frames_ctx->format = AV_PIX_FMT_QSV;
503 frames_ctx->sw_format = qsv_map_fourcc(i->FourCC);
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;
510 ret = av_hwframe_ctx_init(frames_ref);
512 av_log(ctx->logctx, AV_LOG_ERROR,
513 "Error initializing a frames context for an internal frame "
514 "allocation request\n");
515 av_buffer_unref(&frames_ref);
516 return MFX_ERR_MEMORY_ALLOC;
519 mids_buf = qsv_create_mids(frames_ref);
521 av_buffer_unref(&frames_ref);
522 return MFX_ERR_MEMORY_ALLOC;
525 ret = qsv_setup_mids(resp, frames_ref, mids_buf);
526 av_buffer_unref(&mids_buf);
527 av_buffer_unref(&frames_ref);
529 av_log(ctx->logctx, AV_LOG_ERROR,
530 "Error filling an internal frame allocation request\n");
531 return MFX_ERR_MEMORY_ALLOC;
534 return MFX_ERR_UNSUPPORTED;
540 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
542 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
543 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual + 1]);
544 av_freep(&resp->mids);
548 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
550 QSVMid *qsv_mid = mid;
551 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)qsv_mid->hw_frames_ref->data;
552 AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
555 if (qsv_mid->locked_frame)
556 return MFX_ERR_UNDEFINED_BEHAVIOR;
558 /* Allocate a system memory frame that will hold the mapped data. */
559 qsv_mid->locked_frame = av_frame_alloc();
560 if (!qsv_mid->locked_frame)
561 return MFX_ERR_MEMORY_ALLOC;
562 qsv_mid->locked_frame->format = hw_frames_ctx->sw_format;
564 /* wrap the provided handle in a hwaccel AVFrame */
565 qsv_mid->hw_frame = av_frame_alloc();
566 if (!qsv_mid->hw_frame)
569 qsv_mid->hw_frame->data[3] = (uint8_t*)&qsv_mid->surf;
570 qsv_mid->hw_frame->format = AV_PIX_FMT_QSV;
572 // doesn't really matter what buffer is used here
573 qsv_mid->hw_frame->buf[0] = av_buffer_alloc(1);
574 if (!qsv_mid->hw_frame->buf[0])
577 qsv_mid->hw_frame->width = hw_frames_ctx->width;
578 qsv_mid->hw_frame->height = hw_frames_ctx->height;
580 qsv_mid->hw_frame->hw_frames_ctx = av_buffer_ref(qsv_mid->hw_frames_ref);
581 if (!qsv_mid->hw_frame->hw_frames_ctx)
584 qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
585 qsv_mid->surf.Data.MemId = qsv_mid->handle;
587 /* map the data to the system memory */
588 ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
589 AV_HWFRAME_MAP_DIRECT);
593 ptr->Pitch = qsv_mid->locked_frame->linesize[0];
594 ptr->Y = qsv_mid->locked_frame->data[0];
595 ptr->U = qsv_mid->locked_frame->data[1];
596 ptr->V = qsv_mid->locked_frame->data[1] + 1;
600 av_frame_free(&qsv_mid->hw_frame);
601 av_frame_free(&qsv_mid->locked_frame);
602 return MFX_ERR_MEMORY_ALLOC;
605 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
607 QSVMid *qsv_mid = mid;
609 av_frame_free(&qsv_mid->locked_frame);
610 av_frame_free(&qsv_mid->hw_frame);
615 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
617 QSVMid *qsv_mid = (QSVMid*)mid;
618 *hdl = qsv_mid->handle;
622 int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
623 AVBufferRef *device_ref, const char *load_plugins)
625 static const mfxHandleType handle_types[] = {
626 MFX_HANDLE_VA_DISPLAY,
627 MFX_HANDLE_D3D9_DEVICE_MANAGER,
628 MFX_HANDLE_D3D11_DEVICE,
630 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref->data;
631 AVQSVDeviceContext *device_hwctx = device_ctx->hwctx;
632 mfxSession parent_session = device_hwctx->session;
637 mfxHDL handle = NULL;
638 mfxHandleType handle_type;
643 err = MFXQueryIMPL(parent_session, &impl);
644 if (err == MFX_ERR_NONE)
645 err = MFXQueryVersion(parent_session, &ver);
646 if (err != MFX_ERR_NONE)
647 return ff_qsv_print_error(avctx, err,
648 "Error querying the session attributes");
650 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
651 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
652 if (err == MFX_ERR_NONE) {
653 handle_type = handle_types[i];
659 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
660 "from the session\n");
663 err = MFXInit(impl, &ver, &session);
664 if (err != MFX_ERR_NONE)
665 return ff_qsv_print_error(avctx, err,
666 "Error initializing a child MFX session");
669 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
670 if (err != MFX_ERR_NONE)
671 return ff_qsv_print_error(avctx, err,
672 "Error setting a HW handle");
675 if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
676 err = MFXJoinSession(parent_session, session);
677 if (err != MFX_ERR_NONE)
678 return ff_qsv_print_error(avctx, err,
679 "Error joining session");
682 ret = qsv_load_plugins(session, load_plugins, avctx);
684 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
692 int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
693 QSVFramesContext *qsv_frames_ctx,
694 const char *load_plugins, int opaque)
696 mfxFrameAllocator frame_allocator = {
697 .pthis = qsv_frames_ctx,
698 .Alloc = qsv_frame_alloc,
699 .Lock = qsv_frame_lock,
700 .Unlock = qsv_frame_unlock,
701 .GetHDL = qsv_frame_get_hdl,
702 .Free = qsv_frame_free,
705 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
706 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
713 ret = ff_qsv_init_session_device(avctx, &session,
714 frames_ctx->device_ref, load_plugins);
719 qsv_frames_ctx->logctx = avctx;
721 /* allocate the memory ids for the external frames */
722 av_buffer_unref(&qsv_frames_ctx->mids_buf);
723 qsv_frames_ctx->mids_buf = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
724 if (!qsv_frames_ctx->mids_buf)
725 return AVERROR(ENOMEM);
726 qsv_frames_ctx->mids = (QSVMid*)qsv_frames_ctx->mids_buf->data;
727 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
729 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
730 if (err != MFX_ERR_NONE)
731 return ff_qsv_print_error(avctx, err,
732 "Error setting a frame allocator");