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 case MFX_FOURCC_YUY2: return AV_PIX_FMT_YUYV422;
200 #if QSV_VERSION_ATLEAST(1, 27)
201 case MFX_FOURCC_Y210: return AV_PIX_FMT_Y210;
205 return AV_PIX_FMT_NONE;
208 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
211 case AV_PIX_FMT_YUV420P:
212 case AV_PIX_FMT_YUVJ420P:
213 case AV_PIX_FMT_NV12:
214 *fourcc = MFX_FOURCC_NV12;
215 return AV_PIX_FMT_NV12;
216 case AV_PIX_FMT_YUV420P10:
217 case AV_PIX_FMT_P010:
218 *fourcc = MFX_FOURCC_P010;
219 return AV_PIX_FMT_P010;
221 case AV_PIX_FMT_YUV422P:
222 case AV_PIX_FMT_YUYV422:
223 *fourcc = MFX_FOURCC_YUY2;
224 return AV_PIX_FMT_YUYV422;
225 #if QSV_VERSION_ATLEAST(1, 27)
226 case AV_PIX_FMT_YUV422P10:
227 case AV_PIX_FMT_Y210:
228 *fourcc = MFX_FOURCC_Y210;
229 return AV_PIX_FMT_Y210;
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 AVFieldOrder ff_qsv_map_picstruct(int mfx_pic_struct)
250 enum AVFieldOrder field = AV_FIELD_UNKNOWN;
251 switch (mfx_pic_struct & 0xF) {
252 case MFX_PICSTRUCT_PROGRESSIVE:
253 field = AV_FIELD_PROGRESSIVE;
255 case MFX_PICSTRUCT_FIELD_TFF:
258 case MFX_PICSTRUCT_FIELD_BFF:
266 enum AVPictureType ff_qsv_map_pictype(int mfx_pic_type)
268 enum AVPictureType type;
269 switch (mfx_pic_type & 0x7) {
270 case MFX_FRAMETYPE_I:
271 if (mfx_pic_type & MFX_FRAMETYPE_S)
272 type = AV_PICTURE_TYPE_SI;
274 type = AV_PICTURE_TYPE_I;
276 case MFX_FRAMETYPE_B:
277 type = AV_PICTURE_TYPE_B;
279 case MFX_FRAMETYPE_P:
280 if (mfx_pic_type & MFX_FRAMETYPE_S)
281 type = AV_PICTURE_TYPE_SP;
283 type = AV_PICTURE_TYPE_P;
285 case MFX_FRAMETYPE_UNKNOWN:
286 type = AV_PICTURE_TYPE_NONE;
295 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
298 if (!load_plugins || !*load_plugins)
301 while (*load_plugins) {
306 char *plugin = av_get_token(&load_plugins, ":");
308 return AVERROR(ENOMEM);
309 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
310 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
311 err = AVERROR(EINVAL);
312 goto load_plugin_fail;
315 for (i = 0; i < sizeof(uid.Data); i++) {
316 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
318 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
319 err = AVERROR(EINVAL);
320 goto load_plugin_fail;
325 ret = MFXVideoUSER_Load(session, &uid, 1);
328 snprintf(errorbuf, sizeof(errorbuf),
329 "Could not load the requested plugin '%s'", plugin);
330 err = ff_qsv_print_error(logctx, ret, errorbuf);
331 goto load_plugin_fail;
346 //This code is only required for Linux since a display handle is required.
347 //For Windows the session is complete and ready to use.
349 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
350 static int ff_qsv_set_display_handle(AVCodecContext *avctx, QSVSession *qs)
352 AVDictionary *child_device_opts = NULL;
353 AVVAAPIDeviceContext *hwctx;
356 av_dict_set(&child_device_opts, "kernel_driver", "i915", 0);
357 av_dict_set(&child_device_opts, "driver", "iHD", 0);
359 ret = av_hwdevice_ctx_create(&qs->va_device_ref, AV_HWDEVICE_TYPE_VAAPI, NULL, child_device_opts, 0);
361 av_log(avctx, AV_LOG_ERROR, "Failed to create a VAAPI device.\n");
364 qs->va_device_ctx = (AVHWDeviceContext*)qs->va_device_ref->data;
365 hwctx = qs->va_device_ctx->hwctx;
367 ret = MFXVideoCORE_SetHandle(qs->session,
368 (mfxHandleType)MFX_HANDLE_VA_DISPLAY, (mfxHDL)hwctx->display);
370 return ff_qsv_print_error(avctx, ret, "Error during set display handle\n");
374 av_dict_free(&child_device_opts);
378 #endif //AVCODEC_QSV_LINUX_SESSION_HANDLE
380 int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs,
381 const char *load_plugins, int gpu_copy)
383 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
384 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
385 mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
390 #if QSV_VERSION_ATLEAST(1, 16)
391 init_par.GPUCopy = gpu_copy;
393 init_par.Implementation = impl;
394 init_par.Version = ver;
395 ret = MFXInitEx(init_par, &qs->session);
397 return ff_qsv_print_error(avctx, ret,
398 "Error initializing an internal MFX session");
400 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
401 ret = ff_qsv_set_display_handle(avctx, qs);
406 ret = qsv_load_plugins(qs->session, load_plugins, avctx);
408 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
412 MFXQueryIMPL(qs->session, &impl);
414 switch (MFX_IMPL_BASETYPE(impl)) {
415 case MFX_IMPL_SOFTWARE:
418 case MFX_IMPL_HARDWARE:
419 case MFX_IMPL_HARDWARE2:
420 case MFX_IMPL_HARDWARE3:
421 case MFX_IMPL_HARDWARE4:
422 desc = "hardware accelerated";
428 av_log(avctx, AV_LOG_VERBOSE,
429 "Initialized an internal MFX session using %s implementation\n",
435 static void mids_buf_free(void *opaque, uint8_t *data)
437 AVBufferRef *hw_frames_ref = opaque;
438 av_buffer_unref(&hw_frames_ref);
442 static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
444 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
445 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
446 int nb_surfaces = frames_hwctx->nb_surfaces;
448 AVBufferRef *mids_buf, *hw_frames_ref1;
452 hw_frames_ref1 = av_buffer_ref(hw_frames_ref);
456 mids = av_mallocz_array(nb_surfaces, sizeof(*mids));
458 av_buffer_unref(&hw_frames_ref1);
462 mids_buf = av_buffer_create((uint8_t*)mids, nb_surfaces * sizeof(*mids),
463 mids_buf_free, hw_frames_ref1, 0);
465 av_buffer_unref(&hw_frames_ref1);
470 for (i = 0; i < nb_surfaces; i++) {
471 QSVMid *mid = &mids[i];
472 mid->handle = frames_hwctx->surfaces[i].Data.MemId;
473 mid->hw_frames_ref = hw_frames_ref1;
479 static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_ref,
480 AVBufferRef *mids_buf)
482 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
483 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
484 QSVMid *mids = (QSVMid*)mids_buf->data;
485 int nb_surfaces = frames_hwctx->nb_surfaces;
488 // the allocated size of the array is two larger than the number of
489 // surfaces, we store the references to the frames context and the
490 // QSVMid array there
491 resp->mids = av_mallocz_array(nb_surfaces + 2, sizeof(*resp->mids));
493 return AVERROR(ENOMEM);
495 for (i = 0; i < nb_surfaces; i++)
496 resp->mids[i] = &mids[i];
497 resp->NumFrameActual = nb_surfaces;
499 resp->mids[resp->NumFrameActual] = (mfxMemId)av_buffer_ref(hw_frames_ref);
500 if (!resp->mids[resp->NumFrameActual]) {
501 av_freep(&resp->mids);
502 return AVERROR(ENOMEM);
505 resp->mids[resp->NumFrameActual + 1] = av_buffer_ref(mids_buf);
506 if (!resp->mids[resp->NumFrameActual + 1]) {
507 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
508 av_freep(&resp->mids);
509 return AVERROR(ENOMEM);
515 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
516 mfxFrameAllocResponse *resp)
518 QSVFramesContext *ctx = pthis;
521 /* this should only be called from an encoder or decoder and
522 * only allocates video memory frames */
523 if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
524 MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) ||
525 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)))
526 return MFX_ERR_UNSUPPORTED;
528 if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
529 /* external frames -- fill from the caller-supplied frames context */
530 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
531 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
532 mfxFrameInfo *i = &req->Info;
533 mfxFrameInfo *i1 = &frames_hwctx->surfaces[0].Info;
535 if (i->Width > i1->Width || i->Height > i1->Height ||
536 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
537 av_log(ctx->logctx, AV_LOG_ERROR, "Mismatching surface properties in an "
538 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
539 i->Width, i->Height, i->FourCC, i->ChromaFormat,
540 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
541 return MFX_ERR_UNSUPPORTED;
544 ret = qsv_setup_mids(resp, ctx->hw_frames_ctx, ctx->mids_buf);
546 av_log(ctx->logctx, AV_LOG_ERROR,
547 "Error filling an external frame allocation request\n");
548 return MFX_ERR_MEMORY_ALLOC;
550 } else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
551 /* internal frames -- allocate a new hw frames context */
552 AVHWFramesContext *ext_frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
553 mfxFrameInfo *i = &req->Info;
555 AVBufferRef *frames_ref, *mids_buf;
556 AVHWFramesContext *frames_ctx;
557 AVQSVFramesContext *frames_hwctx;
559 frames_ref = av_hwframe_ctx_alloc(ext_frames_ctx->device_ref);
561 return MFX_ERR_MEMORY_ALLOC;
563 frames_ctx = (AVHWFramesContext*)frames_ref->data;
564 frames_hwctx = frames_ctx->hwctx;
566 frames_ctx->format = AV_PIX_FMT_QSV;
567 frames_ctx->sw_format = ff_qsv_map_fourcc(i->FourCC);
568 frames_ctx->width = i->Width;
569 frames_ctx->height = i->Height;
570 frames_ctx->initial_pool_size = req->NumFrameSuggested;
572 frames_hwctx->frame_type = req->Type;
574 ret = av_hwframe_ctx_init(frames_ref);
576 av_log(ctx->logctx, AV_LOG_ERROR,
577 "Error initializing a frames context for an internal frame "
578 "allocation request\n");
579 av_buffer_unref(&frames_ref);
580 return MFX_ERR_MEMORY_ALLOC;
583 mids_buf = qsv_create_mids(frames_ref);
585 av_buffer_unref(&frames_ref);
586 return MFX_ERR_MEMORY_ALLOC;
589 ret = qsv_setup_mids(resp, frames_ref, mids_buf);
590 av_buffer_unref(&mids_buf);
591 av_buffer_unref(&frames_ref);
593 av_log(ctx->logctx, AV_LOG_ERROR,
594 "Error filling an internal frame allocation request\n");
595 return MFX_ERR_MEMORY_ALLOC;
598 return MFX_ERR_UNSUPPORTED;
604 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
606 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
607 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual + 1]);
608 av_freep(&resp->mids);
612 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
614 QSVMid *qsv_mid = mid;
615 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)qsv_mid->hw_frames_ref->data;
616 AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
619 if (qsv_mid->locked_frame)
620 return MFX_ERR_UNDEFINED_BEHAVIOR;
622 /* Allocate a system memory frame that will hold the mapped data. */
623 qsv_mid->locked_frame = av_frame_alloc();
624 if (!qsv_mid->locked_frame)
625 return MFX_ERR_MEMORY_ALLOC;
626 qsv_mid->locked_frame->format = hw_frames_ctx->sw_format;
628 /* wrap the provided handle in a hwaccel AVFrame */
629 qsv_mid->hw_frame = av_frame_alloc();
630 if (!qsv_mid->hw_frame)
633 qsv_mid->hw_frame->data[3] = (uint8_t*)&qsv_mid->surf;
634 qsv_mid->hw_frame->format = AV_PIX_FMT_QSV;
636 // doesn't really matter what buffer is used here
637 qsv_mid->hw_frame->buf[0] = av_buffer_alloc(1);
638 if (!qsv_mid->hw_frame->buf[0])
641 qsv_mid->hw_frame->width = hw_frames_ctx->width;
642 qsv_mid->hw_frame->height = hw_frames_ctx->height;
644 qsv_mid->hw_frame->hw_frames_ctx = av_buffer_ref(qsv_mid->hw_frames_ref);
645 if (!qsv_mid->hw_frame->hw_frames_ctx)
648 qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
649 qsv_mid->surf.Data.MemId = qsv_mid->handle;
651 /* map the data to the system memory */
652 ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
653 AV_HWFRAME_MAP_DIRECT);
657 ptr->Pitch = qsv_mid->locked_frame->linesize[0];
658 ptr->Y = qsv_mid->locked_frame->data[0];
659 ptr->U = qsv_mid->locked_frame->data[1];
660 ptr->V = qsv_mid->locked_frame->data[1] + 1;
664 av_frame_free(&qsv_mid->hw_frame);
665 av_frame_free(&qsv_mid->locked_frame);
666 return MFX_ERR_MEMORY_ALLOC;
669 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
671 QSVMid *qsv_mid = mid;
673 av_frame_free(&qsv_mid->locked_frame);
674 av_frame_free(&qsv_mid->hw_frame);
679 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
681 QSVMid *qsv_mid = (QSVMid*)mid;
682 *hdl = qsv_mid->handle;
686 int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
687 AVBufferRef *device_ref, const char *load_plugins,
690 static const mfxHandleType handle_types[] = {
691 MFX_HANDLE_VA_DISPLAY,
692 MFX_HANDLE_D3D9_DEVICE_MANAGER,
693 MFX_HANDLE_D3D11_DEVICE,
695 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref->data;
696 AVQSVDeviceContext *device_hwctx = device_ctx->hwctx;
697 mfxSession parent_session = device_hwctx->session;
698 mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
699 mfxHDL handle = NULL;
704 mfxHandleType handle_type;
709 err = MFXQueryIMPL(parent_session, &impl);
710 if (err == MFX_ERR_NONE)
711 err = MFXQueryVersion(parent_session, &ver);
712 if (err != MFX_ERR_NONE)
713 return ff_qsv_print_error(avctx, err,
714 "Error querying the session attributes");
716 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
717 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
718 if (err == MFX_ERR_NONE) {
719 handle_type = handle_types[i];
725 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
726 "from the session\n");
729 #if QSV_VERSION_ATLEAST(1, 16)
730 init_par.GPUCopy = gpu_copy;
732 init_par.Implementation = impl;
733 init_par.Version = ver;
734 err = MFXInitEx(init_par, &session);
735 if (err != MFX_ERR_NONE)
736 return ff_qsv_print_error(avctx, err,
737 "Error initializing a child MFX session");
740 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
741 if (err != MFX_ERR_NONE)
742 return ff_qsv_print_error(avctx, err,
743 "Error setting a HW handle");
746 if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
747 err = MFXJoinSession(parent_session, session);
748 if (err != MFX_ERR_NONE)
749 return ff_qsv_print_error(avctx, err,
750 "Error joining session");
753 ret = qsv_load_plugins(session, load_plugins, avctx);
755 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
763 int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
764 QSVFramesContext *qsv_frames_ctx,
765 const char *load_plugins, int opaque, int gpu_copy)
767 mfxFrameAllocator frame_allocator = {
768 .pthis = qsv_frames_ctx,
769 .Alloc = qsv_frame_alloc,
770 .Lock = qsv_frame_lock,
771 .Unlock = qsv_frame_unlock,
772 .GetHDL = qsv_frame_get_hdl,
773 .Free = qsv_frame_free,
776 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
777 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
784 ret = ff_qsv_init_session_device(avctx, &session,
785 frames_ctx->device_ref, load_plugins, gpu_copy);
790 qsv_frames_ctx->logctx = avctx;
792 /* allocate the memory ids for the external frames */
793 av_buffer_unref(&qsv_frames_ctx->mids_buf);
794 qsv_frames_ctx->mids_buf = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
795 if (!qsv_frames_ctx->mids_buf)
796 return AVERROR(ENOMEM);
797 qsv_frames_ctx->mids = (QSVMid*)qsv_frames_ctx->mids_buf->data;
798 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
800 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
801 if (err != MFX_ERR_NONE)
802 return ff_qsv_print_error(avctx, err,
803 "Error setting a frame allocator");
810 int ff_qsv_close_internal_session(QSVSession *qs)
813 MFXClose(qs->session);
816 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
817 av_buffer_unref(&qs->va_device_ref);