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);
77 enum AVCodecID codec_id;
80 } qsv_profile_map[] = {
81 #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, MFX_PROFILE_ ## v }
82 MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2_SIMPLE ),
83 MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2_MAIN ),
84 MAP(MPEG2VIDEO, MPEG2_HIGH, MPEG2_HIGH ),
86 MAP(H264, H264_BASELINE, AVC_BASELINE ),
87 MAP(H264, H264_CONSTRAINED_BASELINE, AVC_BASELINE),
88 #if QSV_VERSION_ATLEAST(1, 3)
89 MAP(H264, H264_EXTENDED, AVC_EXTENDED ),
91 MAP(H264, H264_MAIN, AVC_MAIN ),
92 MAP(H264, H264_HIGH, AVC_HIGH ),
93 MAP(H264, H264_HIGH_422, AVC_HIGH_422 ),
95 #if QSV_VERSION_ATLEAST(1, 8)
96 MAP(HEVC, HEVC_MAIN, HEVC_MAIN ),
97 MAP(HEVC, HEVC_MAIN_10, HEVC_MAIN10 ),
98 MAP(HEVC, HEVC_MAIN_STILL_PICTURE, HEVC_MAINSP ),
100 #if QSV_VERSION_ATLEAST(1, 16)
101 MAP(HEVC, HEVC_REXT, HEVC_REXT ),
104 MAP(VC1, VC1_SIMPLE, VC1_SIMPLE ),
105 MAP(VC1, VC1_MAIN, VC1_MAIN ),
106 MAP(VC1, VC1_COMPLEX, VC1_ADVANCED ),
107 MAP(VC1, VC1_ADVANCED, VC1_ADVANCED ),
111 int ff_qsv_profile_to_mfx(enum AVCodecID codec_id, int profile)
114 if (profile == FF_PROFILE_UNKNOWN)
115 return MFX_PROFILE_UNKNOWN;
117 for (i = 0; i < FF_ARRAY_ELEMS(qsv_profile_map); i++) {
118 if (qsv_profile_map[i].codec_id != codec_id)
120 if (qsv_profile_map[i].codec_profile == profile)
121 return qsv_profile_map[i].mfx_profile;
124 return MFX_PROFILE_UNKNOWN;
127 int ff_qsv_level_to_mfx(enum AVCodecID codec_id, int level)
129 if (level == FF_LEVEL_UNKNOWN)
130 return MFX_LEVEL_UNKNOWN;
133 case AV_CODEC_ID_HEVC:
140 static const struct {
143 } qsv_iopatterns[] = {
144 {MFX_IOPATTERN_IN_VIDEO_MEMORY, "input is video memory surface" },
145 {MFX_IOPATTERN_IN_SYSTEM_MEMORY, "input is system memory surface" },
146 {MFX_IOPATTERN_IN_OPAQUE_MEMORY, "input is opaque memory surface" },
147 {MFX_IOPATTERN_OUT_VIDEO_MEMORY, "output is video memory surface" },
148 {MFX_IOPATTERN_OUT_SYSTEM_MEMORY, "output is system memory surface" },
149 {MFX_IOPATTERN_OUT_OPAQUE_MEMORY, "output is opaque memory surface" },
152 int ff_qsv_print_iopattern(void *log_ctx, int mfx_iopattern,
153 const char *extra_string)
155 const char *desc = NULL;
157 for (int i = 0; i < FF_ARRAY_ELEMS(qsv_iopatterns); i++) {
158 if (qsv_iopatterns[i].mfx_iopattern == mfx_iopattern) {
159 desc = qsv_iopatterns[i].desc;
163 desc = "unknown iopattern";
165 av_log(log_ctx, AV_LOG_VERBOSE, "%s: %s\n", extra_string, desc);
169 static const struct {
174 { MFX_ERR_NONE, 0, "success" },
175 { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
176 { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
177 { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
178 { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
179 { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
180 { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
181 { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
182 { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
183 { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
184 /* the following 3 errors should always be handled explicitly, so those "mappings"
185 * are for completeness only */
186 { MFX_ERR_MORE_DATA, AVERROR_UNKNOWN, "expect more data at input" },
187 { MFX_ERR_MORE_SURFACE, AVERROR_UNKNOWN, "expect more surface at output" },
188 { MFX_ERR_MORE_BITSTREAM, AVERROR_UNKNOWN, "expect more bitstream at output" },
189 { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
190 { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
191 { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
192 { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
193 { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
194 { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
195 { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
196 { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
198 { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
199 { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
200 { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
201 { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
202 { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
203 { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
204 { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
205 { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
206 { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
209 int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
212 for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
213 if (qsv_errors[i].mfxerr == mfx_err) {
215 *desc = qsv_errors[i].desc;
216 return qsv_errors[i].averr;
220 *desc = "unknown error";
221 return AVERROR_UNKNOWN;
224 int ff_qsv_print_error(void *log_ctx, mfxStatus err,
225 const char *error_string)
229 ret = ff_qsv_map_error(err, &desc);
230 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
234 int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
235 const char *warning_string)
239 ret = ff_qsv_map_error(err, &desc);
240 av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
244 enum AVPixelFormat ff_qsv_map_fourcc(uint32_t fourcc)
247 case MFX_FOURCC_NV12: return AV_PIX_FMT_NV12;
248 case MFX_FOURCC_P010: return AV_PIX_FMT_P010;
249 case MFX_FOURCC_P8: return AV_PIX_FMT_PAL8;
251 return AV_PIX_FMT_NONE;
254 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
257 case AV_PIX_FMT_YUV420P:
258 case AV_PIX_FMT_YUVJ420P:
259 case AV_PIX_FMT_NV12:
260 *fourcc = MFX_FOURCC_NV12;
261 return AV_PIX_FMT_NV12;
262 case AV_PIX_FMT_YUV420P10:
263 case AV_PIX_FMT_P010:
264 *fourcc = MFX_FOURCC_P010;
265 return AV_PIX_FMT_P010;
267 return AVERROR(ENOSYS);
271 int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
274 for (i = 0; i < ctx->nb_mids; i++) {
275 QSVMid *mid = &ctx->mids[i];
276 if (mid->handle == frame->surface.Data.MemId)
282 enum AVFieldOrder ff_qsv_map_picstruct(int mfx_pic_struct)
284 enum AVFieldOrder field = AV_FIELD_UNKNOWN;
285 switch (mfx_pic_struct & 0xF) {
286 case MFX_PICSTRUCT_PROGRESSIVE:
287 field = AV_FIELD_PROGRESSIVE;
289 case MFX_PICSTRUCT_FIELD_TFF:
292 case MFX_PICSTRUCT_FIELD_BFF:
300 enum AVPictureType ff_qsv_map_pictype(int mfx_pic_type)
302 enum AVPictureType type;
303 switch (mfx_pic_type & 0x7) {
304 case MFX_FRAMETYPE_I:
305 if (mfx_pic_type & MFX_FRAMETYPE_S)
306 type = AV_PICTURE_TYPE_SI;
308 type = AV_PICTURE_TYPE_I;
310 case MFX_FRAMETYPE_B:
311 type = AV_PICTURE_TYPE_B;
313 case MFX_FRAMETYPE_P:
314 if (mfx_pic_type & MFX_FRAMETYPE_S)
315 type = AV_PICTURE_TYPE_SP;
317 type = AV_PICTURE_TYPE_P;
319 case MFX_FRAMETYPE_UNKNOWN:
320 type = AV_PICTURE_TYPE_NONE;
329 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
332 if (!load_plugins || !*load_plugins)
335 while (*load_plugins) {
340 char *plugin = av_get_token(&load_plugins, ":");
342 return AVERROR(ENOMEM);
343 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
344 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
345 err = AVERROR(EINVAL);
346 goto load_plugin_fail;
349 for (i = 0; i < sizeof(uid.Data); i++) {
350 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
352 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
353 err = AVERROR(EINVAL);
354 goto load_plugin_fail;
359 ret = MFXVideoUSER_Load(session, &uid, 1);
362 snprintf(errorbuf, sizeof(errorbuf),
363 "Could not load the requested plugin '%s'", plugin);
364 err = ff_qsv_print_error(logctx, ret, errorbuf);
365 goto load_plugin_fail;
380 //This code is only required for Linux since a display handle is required.
381 //For Windows the session is complete and ready to use.
383 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
384 static int ff_qsv_set_display_handle(AVCodecContext *avctx, QSVSession *qs)
386 AVDictionary *child_device_opts = NULL;
387 AVVAAPIDeviceContext *hwctx;
390 av_dict_set(&child_device_opts, "kernel_driver", "i915", 0);
391 av_dict_set(&child_device_opts, "driver", "iHD", 0);
393 ret = av_hwdevice_ctx_create(&qs->va_device_ref, AV_HWDEVICE_TYPE_VAAPI, NULL, child_device_opts, 0);
395 av_log(avctx, AV_LOG_ERROR, "Failed to create a VAAPI device.\n");
398 qs->va_device_ctx = (AVHWDeviceContext*)qs->va_device_ref->data;
399 hwctx = qs->va_device_ctx->hwctx;
401 ret = MFXVideoCORE_SetHandle(qs->session,
402 (mfxHandleType)MFX_HANDLE_VA_DISPLAY, (mfxHDL)hwctx->display);
404 return ff_qsv_print_error(avctx, ret, "Error during set display handle\n");
408 av_dict_free(&child_device_opts);
412 #endif //AVCODEC_QSV_LINUX_SESSION_HANDLE
414 int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs,
415 const char *load_plugins, int gpu_copy)
417 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
418 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
419 mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
424 #if QSV_VERSION_ATLEAST(1, 16)
425 init_par.GPUCopy = gpu_copy;
427 init_par.Implementation = impl;
428 init_par.Version = ver;
429 ret = MFXInitEx(init_par, &qs->session);
431 return ff_qsv_print_error(avctx, ret,
432 "Error initializing an internal MFX session");
434 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
435 ret = ff_qsv_set_display_handle(avctx, qs);
440 ret = qsv_load_plugins(qs->session, load_plugins, avctx);
442 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
446 MFXQueryIMPL(qs->session, &impl);
448 switch (MFX_IMPL_BASETYPE(impl)) {
449 case MFX_IMPL_SOFTWARE:
452 case MFX_IMPL_HARDWARE:
453 case MFX_IMPL_HARDWARE2:
454 case MFX_IMPL_HARDWARE3:
455 case MFX_IMPL_HARDWARE4:
456 desc = "hardware accelerated";
462 av_log(avctx, AV_LOG_VERBOSE,
463 "Initialized an internal MFX session using %s implementation\n",
469 static void mids_buf_free(void *opaque, uint8_t *data)
471 AVBufferRef *hw_frames_ref = opaque;
472 av_buffer_unref(&hw_frames_ref);
476 static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
478 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
479 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
480 int nb_surfaces = frames_hwctx->nb_surfaces;
482 AVBufferRef *mids_buf, *hw_frames_ref1;
486 hw_frames_ref1 = av_buffer_ref(hw_frames_ref);
490 mids = av_mallocz_array(nb_surfaces, sizeof(*mids));
492 av_buffer_unref(&hw_frames_ref1);
496 mids_buf = av_buffer_create((uint8_t*)mids, nb_surfaces * sizeof(*mids),
497 mids_buf_free, hw_frames_ref1, 0);
499 av_buffer_unref(&hw_frames_ref1);
504 for (i = 0; i < nb_surfaces; i++) {
505 QSVMid *mid = &mids[i];
506 mid->handle = frames_hwctx->surfaces[i].Data.MemId;
507 mid->hw_frames_ref = hw_frames_ref1;
513 static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_ref,
514 AVBufferRef *mids_buf)
516 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
517 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
518 QSVMid *mids = (QSVMid*)mids_buf->data;
519 int nb_surfaces = frames_hwctx->nb_surfaces;
522 // the allocated size of the array is two larger than the number of
523 // surfaces, we store the references to the frames context and the
524 // QSVMid array there
525 resp->mids = av_mallocz_array(nb_surfaces + 2, sizeof(*resp->mids));
527 return AVERROR(ENOMEM);
529 for (i = 0; i < nb_surfaces; i++)
530 resp->mids[i] = &mids[i];
531 resp->NumFrameActual = nb_surfaces;
533 resp->mids[resp->NumFrameActual] = (mfxMemId)av_buffer_ref(hw_frames_ref);
534 if (!resp->mids[resp->NumFrameActual]) {
535 av_freep(&resp->mids);
536 return AVERROR(ENOMEM);
539 resp->mids[resp->NumFrameActual + 1] = av_buffer_ref(mids_buf);
540 if (!resp->mids[resp->NumFrameActual + 1]) {
541 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
542 av_freep(&resp->mids);
543 return AVERROR(ENOMEM);
549 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
550 mfxFrameAllocResponse *resp)
552 QSVFramesContext *ctx = pthis;
555 /* this should only be called from an encoder or decoder and
556 * only allocates video memory frames */
557 if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
558 MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) ||
559 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)))
560 return MFX_ERR_UNSUPPORTED;
562 if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
563 /* external frames -- fill from the caller-supplied frames context */
564 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
565 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
566 mfxFrameInfo *i = &req->Info;
567 mfxFrameInfo *i1 = &frames_hwctx->surfaces[0].Info;
569 if (i->Width > i1->Width || i->Height > i1->Height ||
570 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
571 av_log(ctx->logctx, AV_LOG_ERROR, "Mismatching surface properties in an "
572 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
573 i->Width, i->Height, i->FourCC, i->ChromaFormat,
574 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
575 return MFX_ERR_UNSUPPORTED;
578 ret = qsv_setup_mids(resp, ctx->hw_frames_ctx, ctx->mids_buf);
580 av_log(ctx->logctx, AV_LOG_ERROR,
581 "Error filling an external frame allocation request\n");
582 return MFX_ERR_MEMORY_ALLOC;
584 } else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
585 /* internal frames -- allocate a new hw frames context */
586 AVHWFramesContext *ext_frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
587 mfxFrameInfo *i = &req->Info;
589 AVBufferRef *frames_ref, *mids_buf;
590 AVHWFramesContext *frames_ctx;
591 AVQSVFramesContext *frames_hwctx;
593 frames_ref = av_hwframe_ctx_alloc(ext_frames_ctx->device_ref);
595 return MFX_ERR_MEMORY_ALLOC;
597 frames_ctx = (AVHWFramesContext*)frames_ref->data;
598 frames_hwctx = frames_ctx->hwctx;
600 frames_ctx->format = AV_PIX_FMT_QSV;
601 frames_ctx->sw_format = ff_qsv_map_fourcc(i->FourCC);
602 frames_ctx->width = i->Width;
603 frames_ctx->height = i->Height;
604 frames_ctx->initial_pool_size = req->NumFrameSuggested;
606 frames_hwctx->frame_type = req->Type;
608 ret = av_hwframe_ctx_init(frames_ref);
610 av_log(ctx->logctx, AV_LOG_ERROR,
611 "Error initializing a frames context for an internal frame "
612 "allocation request\n");
613 av_buffer_unref(&frames_ref);
614 return MFX_ERR_MEMORY_ALLOC;
617 mids_buf = qsv_create_mids(frames_ref);
619 av_buffer_unref(&frames_ref);
620 return MFX_ERR_MEMORY_ALLOC;
623 ret = qsv_setup_mids(resp, frames_ref, mids_buf);
624 av_buffer_unref(&mids_buf);
625 av_buffer_unref(&frames_ref);
627 av_log(ctx->logctx, AV_LOG_ERROR,
628 "Error filling an internal frame allocation request\n");
629 return MFX_ERR_MEMORY_ALLOC;
632 return MFX_ERR_UNSUPPORTED;
638 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
640 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
641 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual + 1]);
642 av_freep(&resp->mids);
646 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
648 QSVMid *qsv_mid = mid;
649 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)qsv_mid->hw_frames_ref->data;
650 AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
653 if (qsv_mid->locked_frame)
654 return MFX_ERR_UNDEFINED_BEHAVIOR;
656 /* Allocate a system memory frame that will hold the mapped data. */
657 qsv_mid->locked_frame = av_frame_alloc();
658 if (!qsv_mid->locked_frame)
659 return MFX_ERR_MEMORY_ALLOC;
660 qsv_mid->locked_frame->format = hw_frames_ctx->sw_format;
662 /* wrap the provided handle in a hwaccel AVFrame */
663 qsv_mid->hw_frame = av_frame_alloc();
664 if (!qsv_mid->hw_frame)
667 qsv_mid->hw_frame->data[3] = (uint8_t*)&qsv_mid->surf;
668 qsv_mid->hw_frame->format = AV_PIX_FMT_QSV;
670 // doesn't really matter what buffer is used here
671 qsv_mid->hw_frame->buf[0] = av_buffer_alloc(1);
672 if (!qsv_mid->hw_frame->buf[0])
675 qsv_mid->hw_frame->width = hw_frames_ctx->width;
676 qsv_mid->hw_frame->height = hw_frames_ctx->height;
678 qsv_mid->hw_frame->hw_frames_ctx = av_buffer_ref(qsv_mid->hw_frames_ref);
679 if (!qsv_mid->hw_frame->hw_frames_ctx)
682 qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
683 qsv_mid->surf.Data.MemId = qsv_mid->handle;
685 /* map the data to the system memory */
686 ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
687 AV_HWFRAME_MAP_DIRECT);
691 ptr->Pitch = qsv_mid->locked_frame->linesize[0];
692 ptr->Y = qsv_mid->locked_frame->data[0];
693 ptr->U = qsv_mid->locked_frame->data[1];
694 ptr->V = qsv_mid->locked_frame->data[1] + 1;
698 av_frame_free(&qsv_mid->hw_frame);
699 av_frame_free(&qsv_mid->locked_frame);
700 return MFX_ERR_MEMORY_ALLOC;
703 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
705 QSVMid *qsv_mid = mid;
707 av_frame_free(&qsv_mid->locked_frame);
708 av_frame_free(&qsv_mid->hw_frame);
713 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
715 QSVMid *qsv_mid = (QSVMid*)mid;
716 *hdl = qsv_mid->handle;
720 int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
721 AVBufferRef *device_ref, const char *load_plugins,
724 static const mfxHandleType handle_types[] = {
725 MFX_HANDLE_VA_DISPLAY,
726 MFX_HANDLE_D3D9_DEVICE_MANAGER,
727 MFX_HANDLE_D3D11_DEVICE,
729 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref->data;
730 AVQSVDeviceContext *device_hwctx = device_ctx->hwctx;
731 mfxSession parent_session = device_hwctx->session;
732 mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
733 mfxHDL handle = NULL;
738 mfxHandleType handle_type;
743 err = MFXQueryIMPL(parent_session, &impl);
744 if (err == MFX_ERR_NONE)
745 err = MFXQueryVersion(parent_session, &ver);
746 if (err != MFX_ERR_NONE)
747 return ff_qsv_print_error(avctx, err,
748 "Error querying the session attributes");
750 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
751 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
752 if (err == MFX_ERR_NONE) {
753 handle_type = handle_types[i];
759 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
760 "from the session\n");
763 #if QSV_VERSION_ATLEAST(1, 16)
764 init_par.GPUCopy = gpu_copy;
766 init_par.Implementation = impl;
767 init_par.Version = ver;
768 err = MFXInitEx(init_par, &session);
769 if (err != MFX_ERR_NONE)
770 return ff_qsv_print_error(avctx, err,
771 "Error initializing a child MFX session");
774 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
775 if (err != MFX_ERR_NONE)
776 return ff_qsv_print_error(avctx, err,
777 "Error setting a HW handle");
780 if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
781 err = MFXJoinSession(parent_session, session);
782 if (err != MFX_ERR_NONE)
783 return ff_qsv_print_error(avctx, err,
784 "Error joining session");
787 ret = qsv_load_plugins(session, load_plugins, avctx);
789 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
797 int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
798 QSVFramesContext *qsv_frames_ctx,
799 const char *load_plugins, int opaque, int gpu_copy)
801 mfxFrameAllocator frame_allocator = {
802 .pthis = qsv_frames_ctx,
803 .Alloc = qsv_frame_alloc,
804 .Lock = qsv_frame_lock,
805 .Unlock = qsv_frame_unlock,
806 .GetHDL = qsv_frame_get_hdl,
807 .Free = qsv_frame_free,
810 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
811 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
818 ret = ff_qsv_init_session_device(avctx, &session,
819 frames_ctx->device_ref, load_plugins, gpu_copy);
824 qsv_frames_ctx->logctx = avctx;
826 /* allocate the memory ids for the external frames */
827 av_buffer_unref(&qsv_frames_ctx->mids_buf);
828 qsv_frames_ctx->mids_buf = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
829 if (!qsv_frames_ctx->mids_buf)
830 return AVERROR(ENOMEM);
831 qsv_frames_ctx->mids = (QSVMid*)qsv_frames_ctx->mids_buf->data;
832 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
834 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
835 if (err != MFX_ERR_NONE)
836 return ff_qsv_print_error(avctx, err,
837 "Error setting a frame allocator");
844 int ff_qsv_close_internal_session(QSVSession *qs)
847 MFXClose(qs->session);
850 #ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
851 av_buffer_unref(&qs->va_device_ref);