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;
63 #if QSV_VERSION_ATLEAST(1, 19)
72 return AVERROR(ENOSYS);
75 int ff_qsv_level_to_mfx(enum AVCodecID codec_id, int level)
77 if (level == FF_LEVEL_UNKNOWN)
78 return MFX_LEVEL_UNKNOWN;
81 case AV_CODEC_ID_HEVC:
91 } qsv_iopatterns[] = {
92 {MFX_IOPATTERN_IN_VIDEO_MEMORY, "input is video memory surface" },
93 {MFX_IOPATTERN_IN_SYSTEM_MEMORY, "input is system memory surface" },
94 {MFX_IOPATTERN_IN_OPAQUE_MEMORY, "input is opaque memory surface" },
95 {MFX_IOPATTERN_OUT_VIDEO_MEMORY, "output is video memory surface" },
96 {MFX_IOPATTERN_OUT_SYSTEM_MEMORY, "output is system memory surface" },
97 {MFX_IOPATTERN_OUT_OPAQUE_MEMORY, "output is opaque memory surface" },
100 int ff_qsv_print_iopattern(void *log_ctx, int mfx_iopattern,
101 const char *extra_string)
103 const char *desc = NULL;
105 for (int i = 0; i < FF_ARRAY_ELEMS(qsv_iopatterns); i++) {
106 if (qsv_iopatterns[i].mfx_iopattern == mfx_iopattern) {
107 desc = qsv_iopatterns[i].desc;
111 desc = "unknown iopattern";
113 av_log(log_ctx, AV_LOG_VERBOSE, "%s: %s\n", extra_string, desc);
117 static const struct {
122 { MFX_ERR_NONE, 0, "success" },
123 { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
124 { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
125 { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
126 { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
127 { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
128 { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
129 { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
130 { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
131 { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
132 /* the following 3 errors should always be handled explicitly, so those "mappings"
133 * are for completeness only */
134 { MFX_ERR_MORE_DATA, AVERROR_UNKNOWN, "expect more data at input" },
135 { MFX_ERR_MORE_SURFACE, AVERROR_UNKNOWN, "expect more surface at output" },
136 { MFX_ERR_MORE_BITSTREAM, AVERROR_UNKNOWN, "expect more bitstream at output" },
137 { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
138 { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
139 { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
140 { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
141 { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
142 { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
143 { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
144 { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
146 { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
147 { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
148 { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
149 { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
150 { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
151 { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
152 { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
153 { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
154 { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
157 int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
160 for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
161 if (qsv_errors[i].mfxerr == mfx_err) {
163 *desc = qsv_errors[i].desc;
164 return qsv_errors[i].averr;
168 *desc = "unknown error";
169 return AVERROR_UNKNOWN;
172 int ff_qsv_print_error(void *log_ctx, mfxStatus err,
173 const char *error_string)
177 ret = ff_qsv_map_error(err, &desc);
178 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
182 int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
183 const char *warning_string)
187 ret = ff_qsv_map_error(err, &desc);
188 av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
192 enum AVPixelFormat ff_qsv_map_fourcc(uint32_t fourcc)
195 case MFX_FOURCC_NV12: return AV_PIX_FMT_NV12;
196 case MFX_FOURCC_P010: return AV_PIX_FMT_P010;
197 case MFX_FOURCC_P8: return AV_PIX_FMT_PAL8;
199 return AV_PIX_FMT_NONE;
202 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
205 case AV_PIX_FMT_YUV420P:
206 case AV_PIX_FMT_YUVJ420P:
207 case AV_PIX_FMT_NV12:
208 *fourcc = MFX_FOURCC_NV12;
209 return AV_PIX_FMT_NV12;
210 case AV_PIX_FMT_YUV420P10:
211 case AV_PIX_FMT_P010:
212 *fourcc = MFX_FOURCC_P010;
213 return AV_PIX_FMT_P010;
215 return AVERROR(ENOSYS);
219 int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
222 for (i = 0; i < ctx->nb_mids; i++) {
223 QSVMid *mid = &ctx->mids[i];
224 if (mid->handle == frame->surface.Data.MemId)
230 enum AVFieldOrder ff_qsv_map_picstruct(int mfx_pic_struct)
232 enum AVFieldOrder field = AV_FIELD_UNKNOWN;
233 switch (mfx_pic_struct & 0xF) {
234 case MFX_PICSTRUCT_PROGRESSIVE:
235 field = AV_FIELD_PROGRESSIVE;
237 case MFX_PICSTRUCT_FIELD_TFF:
240 case MFX_PICSTRUCT_FIELD_BFF:
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 //This code is only required for Linux since a display handle is required.
329 //For Windows the session is complete and ready to use.
331 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
332 static int ff_qsv_set_display_handle(AVCodecContext *avctx, QSVSession *qs)
334 AVDictionary *child_device_opts = NULL;
335 AVVAAPIDeviceContext *hwctx;
338 av_dict_set(&child_device_opts, "kernel_driver", "i915", 0);
339 av_dict_set(&child_device_opts, "driver", "iHD", 0);
341 ret = av_hwdevice_ctx_create(&qs->va_device_ref, AV_HWDEVICE_TYPE_VAAPI, NULL, child_device_opts, 0);
343 av_log(avctx, AV_LOG_ERROR, "Failed to create a VAAPI device.\n");
346 qs->va_device_ctx = (AVHWDeviceContext*)qs->va_device_ref->data;
347 hwctx = qs->va_device_ctx->hwctx;
349 ret = MFXVideoCORE_SetHandle(qs->session,
350 (mfxHandleType)MFX_HANDLE_VA_DISPLAY, (mfxHDL)hwctx->display);
352 return ff_qsv_print_error(avctx, ret, "Error during set display handle\n");
356 av_dict_free(&child_device_opts);
360 #endif //AVCODEC_QSV_LINUX_SESSION_HANDLE
362 int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs,
363 const char *load_plugins, int gpu_copy)
365 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
366 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
367 mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
372 #if QSV_VERSION_ATLEAST(1, 16)
373 init_par.GPUCopy = gpu_copy;
375 init_par.Implementation = impl;
376 init_par.Version = ver;
377 ret = MFXInitEx(init_par, &qs->session);
379 return ff_qsv_print_error(avctx, ret,
380 "Error initializing an internal MFX session");
382 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
383 ret = ff_qsv_set_display_handle(avctx, qs);
388 ret = qsv_load_plugins(qs->session, load_plugins, avctx);
390 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
394 MFXQueryIMPL(qs->session, &impl);
396 switch (MFX_IMPL_BASETYPE(impl)) {
397 case MFX_IMPL_SOFTWARE:
400 case MFX_IMPL_HARDWARE:
401 case MFX_IMPL_HARDWARE2:
402 case MFX_IMPL_HARDWARE3:
403 case MFX_IMPL_HARDWARE4:
404 desc = "hardware accelerated";
410 av_log(avctx, AV_LOG_VERBOSE,
411 "Initialized an internal MFX session using %s implementation\n",
417 static void mids_buf_free(void *opaque, uint8_t *data)
419 AVBufferRef *hw_frames_ref = opaque;
420 av_buffer_unref(&hw_frames_ref);
424 static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
426 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
427 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
428 int nb_surfaces = frames_hwctx->nb_surfaces;
430 AVBufferRef *mids_buf, *hw_frames_ref1;
434 hw_frames_ref1 = av_buffer_ref(hw_frames_ref);
438 mids = av_mallocz_array(nb_surfaces, sizeof(*mids));
440 av_buffer_unref(&hw_frames_ref1);
444 mids_buf = av_buffer_create((uint8_t*)mids, nb_surfaces * sizeof(*mids),
445 mids_buf_free, hw_frames_ref1, 0);
447 av_buffer_unref(&hw_frames_ref1);
452 for (i = 0; i < nb_surfaces; i++) {
453 QSVMid *mid = &mids[i];
454 mid->handle = frames_hwctx->surfaces[i].Data.MemId;
455 mid->hw_frames_ref = hw_frames_ref1;
461 static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_ref,
462 AVBufferRef *mids_buf)
464 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
465 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
466 QSVMid *mids = (QSVMid*)mids_buf->data;
467 int nb_surfaces = frames_hwctx->nb_surfaces;
470 // the allocated size of the array is two larger than the number of
471 // surfaces, we store the references to the frames context and the
472 // QSVMid array there
473 resp->mids = av_mallocz_array(nb_surfaces + 2, sizeof(*resp->mids));
475 return AVERROR(ENOMEM);
477 for (i = 0; i < nb_surfaces; i++)
478 resp->mids[i] = &mids[i];
479 resp->NumFrameActual = nb_surfaces;
481 resp->mids[resp->NumFrameActual] = (mfxMemId)av_buffer_ref(hw_frames_ref);
482 if (!resp->mids[resp->NumFrameActual]) {
483 av_freep(&resp->mids);
484 return AVERROR(ENOMEM);
487 resp->mids[resp->NumFrameActual + 1] = av_buffer_ref(mids_buf);
488 if (!resp->mids[resp->NumFrameActual + 1]) {
489 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
490 av_freep(&resp->mids);
491 return AVERROR(ENOMEM);
497 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
498 mfxFrameAllocResponse *resp)
500 QSVFramesContext *ctx = pthis;
503 /* this should only be called from an encoder or decoder and
504 * only allocates video memory frames */
505 if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
506 MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) ||
507 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)))
508 return MFX_ERR_UNSUPPORTED;
510 if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
511 /* external frames -- fill from the caller-supplied frames context */
512 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
513 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
514 mfxFrameInfo *i = &req->Info;
515 mfxFrameInfo *i1 = &frames_hwctx->surfaces[0].Info;
517 if (i->Width > i1->Width || i->Height > i1->Height ||
518 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
519 av_log(ctx->logctx, AV_LOG_ERROR, "Mismatching surface properties in an "
520 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
521 i->Width, i->Height, i->FourCC, i->ChromaFormat,
522 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
523 return MFX_ERR_UNSUPPORTED;
526 ret = qsv_setup_mids(resp, ctx->hw_frames_ctx, ctx->mids_buf);
528 av_log(ctx->logctx, AV_LOG_ERROR,
529 "Error filling an external frame allocation request\n");
530 return MFX_ERR_MEMORY_ALLOC;
532 } else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
533 /* internal frames -- allocate a new hw frames context */
534 AVHWFramesContext *ext_frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
535 mfxFrameInfo *i = &req->Info;
537 AVBufferRef *frames_ref, *mids_buf;
538 AVHWFramesContext *frames_ctx;
539 AVQSVFramesContext *frames_hwctx;
541 frames_ref = av_hwframe_ctx_alloc(ext_frames_ctx->device_ref);
543 return MFX_ERR_MEMORY_ALLOC;
545 frames_ctx = (AVHWFramesContext*)frames_ref->data;
546 frames_hwctx = frames_ctx->hwctx;
548 frames_ctx->format = AV_PIX_FMT_QSV;
549 frames_ctx->sw_format = ff_qsv_map_fourcc(i->FourCC);
550 frames_ctx->width = i->Width;
551 frames_ctx->height = i->Height;
552 frames_ctx->initial_pool_size = req->NumFrameSuggested;
554 frames_hwctx->frame_type = req->Type;
556 ret = av_hwframe_ctx_init(frames_ref);
558 av_log(ctx->logctx, AV_LOG_ERROR,
559 "Error initializing a frames context for an internal frame "
560 "allocation request\n");
561 av_buffer_unref(&frames_ref);
562 return MFX_ERR_MEMORY_ALLOC;
565 mids_buf = qsv_create_mids(frames_ref);
567 av_buffer_unref(&frames_ref);
568 return MFX_ERR_MEMORY_ALLOC;
571 ret = qsv_setup_mids(resp, frames_ref, mids_buf);
572 av_buffer_unref(&mids_buf);
573 av_buffer_unref(&frames_ref);
575 av_log(ctx->logctx, AV_LOG_ERROR,
576 "Error filling an internal frame allocation request\n");
577 return MFX_ERR_MEMORY_ALLOC;
580 return MFX_ERR_UNSUPPORTED;
586 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
588 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
589 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual + 1]);
590 av_freep(&resp->mids);
594 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
596 QSVMid *qsv_mid = mid;
597 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)qsv_mid->hw_frames_ref->data;
598 AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
601 if (qsv_mid->locked_frame)
602 return MFX_ERR_UNDEFINED_BEHAVIOR;
604 /* Allocate a system memory frame that will hold the mapped data. */
605 qsv_mid->locked_frame = av_frame_alloc();
606 if (!qsv_mid->locked_frame)
607 return MFX_ERR_MEMORY_ALLOC;
608 qsv_mid->locked_frame->format = hw_frames_ctx->sw_format;
610 /* wrap the provided handle in a hwaccel AVFrame */
611 qsv_mid->hw_frame = av_frame_alloc();
612 if (!qsv_mid->hw_frame)
615 qsv_mid->hw_frame->data[3] = (uint8_t*)&qsv_mid->surf;
616 qsv_mid->hw_frame->format = AV_PIX_FMT_QSV;
618 // doesn't really matter what buffer is used here
619 qsv_mid->hw_frame->buf[0] = av_buffer_alloc(1);
620 if (!qsv_mid->hw_frame->buf[0])
623 qsv_mid->hw_frame->width = hw_frames_ctx->width;
624 qsv_mid->hw_frame->height = hw_frames_ctx->height;
626 qsv_mid->hw_frame->hw_frames_ctx = av_buffer_ref(qsv_mid->hw_frames_ref);
627 if (!qsv_mid->hw_frame->hw_frames_ctx)
630 qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
631 qsv_mid->surf.Data.MemId = qsv_mid->handle;
633 /* map the data to the system memory */
634 ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
635 AV_HWFRAME_MAP_DIRECT);
639 ptr->Pitch = qsv_mid->locked_frame->linesize[0];
640 ptr->Y = qsv_mid->locked_frame->data[0];
641 ptr->U = qsv_mid->locked_frame->data[1];
642 ptr->V = qsv_mid->locked_frame->data[1] + 1;
646 av_frame_free(&qsv_mid->hw_frame);
647 av_frame_free(&qsv_mid->locked_frame);
648 return MFX_ERR_MEMORY_ALLOC;
651 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
653 QSVMid *qsv_mid = mid;
655 av_frame_free(&qsv_mid->locked_frame);
656 av_frame_free(&qsv_mid->hw_frame);
661 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
663 QSVMid *qsv_mid = (QSVMid*)mid;
664 *hdl = qsv_mid->handle;
668 int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
669 AVBufferRef *device_ref, const char *load_plugins,
672 static const mfxHandleType handle_types[] = {
673 MFX_HANDLE_VA_DISPLAY,
674 MFX_HANDLE_D3D9_DEVICE_MANAGER,
675 MFX_HANDLE_D3D11_DEVICE,
677 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref->data;
678 AVQSVDeviceContext *device_hwctx = device_ctx->hwctx;
679 mfxSession parent_session = device_hwctx->session;
680 mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
681 mfxHDL handle = NULL;
686 mfxHandleType handle_type;
691 err = MFXQueryIMPL(parent_session, &impl);
692 if (err == MFX_ERR_NONE)
693 err = MFXQueryVersion(parent_session, &ver);
694 if (err != MFX_ERR_NONE)
695 return ff_qsv_print_error(avctx, err,
696 "Error querying the session attributes");
698 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
699 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
700 if (err == MFX_ERR_NONE) {
701 handle_type = handle_types[i];
707 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
708 "from the session\n");
711 #if QSV_VERSION_ATLEAST(1, 16)
712 init_par.GPUCopy = gpu_copy;
714 init_par.Implementation = impl;
715 init_par.Version = ver;
716 err = MFXInitEx(init_par, &session);
717 if (err != MFX_ERR_NONE)
718 return ff_qsv_print_error(avctx, err,
719 "Error initializing a child MFX session");
722 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
723 if (err != MFX_ERR_NONE)
724 return ff_qsv_print_error(avctx, err,
725 "Error setting a HW handle");
728 if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
729 err = MFXJoinSession(parent_session, session);
730 if (err != MFX_ERR_NONE)
731 return ff_qsv_print_error(avctx, err,
732 "Error joining session");
735 ret = qsv_load_plugins(session, load_plugins, avctx);
737 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
745 int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
746 QSVFramesContext *qsv_frames_ctx,
747 const char *load_plugins, int opaque, int gpu_copy)
749 mfxFrameAllocator frame_allocator = {
750 .pthis = qsv_frames_ctx,
751 .Alloc = qsv_frame_alloc,
752 .Lock = qsv_frame_lock,
753 .Unlock = qsv_frame_unlock,
754 .GetHDL = qsv_frame_get_hdl,
755 .Free = qsv_frame_free,
758 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
759 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
766 ret = ff_qsv_init_session_device(avctx, &session,
767 frames_ctx->device_ref, load_plugins, gpu_copy);
772 qsv_frames_ctx->logctx = avctx;
774 /* allocate the memory ids for the external frames */
775 av_buffer_unref(&qsv_frames_ctx->mids_buf);
776 qsv_frames_ctx->mids_buf = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
777 if (!qsv_frames_ctx->mids_buf)
778 return AVERROR(ENOMEM);
779 qsv_frames_ctx->mids = (QSVMid*)qsv_frames_ctx->mids_buf->data;
780 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
782 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
783 if (err != MFX_ERR_NONE)
784 return ff_qsv_print_error(avctx, err,
785 "Error setting a frame allocator");
792 int ff_qsv_close_internal_session(QSVSession *qs)
795 MFXClose(qs->session);
798 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
799 av_buffer_unref(&qs->va_device_ref);