2 * Intel MediaSDK QSV encoder/decoder shared code
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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>
27 #include "libavutil/avstring.h"
28 #include "libavutil/common.h"
29 #include "libavutil/error.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_qsv.h"
34 #include "qsv_internal.h"
36 int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
39 case AV_CODEC_ID_H264:
41 #if QSV_VERSION_ATLEAST(1, 8)
42 case AV_CODEC_ID_HEVC:
43 return MFX_CODEC_HEVC;
45 case AV_CODEC_ID_MPEG1VIDEO:
46 case AV_CODEC_ID_MPEG2VIDEO:
47 return MFX_CODEC_MPEG2;
54 return AVERROR(ENOSYS);
62 { MFX_ERR_NONE, 0, "success" },
63 { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
64 { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
65 { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
66 { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
67 { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
68 { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
69 { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
70 { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
71 { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
72 { MFX_ERR_MORE_DATA, AVERROR(EAGAIN), "expect more data at input" },
73 { MFX_ERR_MORE_SURFACE, AVERROR(EAGAIN), "expect more surface at output" },
74 { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
75 { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
76 { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
77 { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
78 { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
79 { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
80 { MFX_ERR_MORE_BITSTREAM, AVERROR(EAGAIN), "expect more bitstream at output" },
81 { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
82 { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
84 { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
85 { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
86 { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
87 { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
88 { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
89 { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
90 { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
91 { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
92 { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
95 int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
98 for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
99 if (qsv_errors[i].mfxerr == mfx_err) {
101 *desc = qsv_errors[i].desc;
102 return qsv_errors[i].averr;
106 *desc = "unknown error";
107 return AVERROR_UNKNOWN;
110 int ff_qsv_print_error(void *log_ctx, mfxStatus err,
111 const char *error_string)
115 ret = ff_qsv_map_error(err, &desc);
116 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
120 int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
121 const char *warning_string)
125 ret = ff_qsv_map_error(err, &desc);
126 av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
130 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
133 case AV_PIX_FMT_YUV420P:
134 case AV_PIX_FMT_YUVJ420P:
135 case AV_PIX_FMT_NV12:
136 *fourcc = MFX_FOURCC_NV12;
137 return AV_PIX_FMT_NV12;
138 case AV_PIX_FMT_YUV420P10:
139 case AV_PIX_FMT_P010:
140 *fourcc = MFX_FOURCC_P010;
141 return AV_PIX_FMT_P010;
143 return AVERROR(ENOSYS);
147 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
150 if (!load_plugins || !*load_plugins)
153 while (*load_plugins) {
158 char *plugin = av_get_token(&load_plugins, ":");
160 return AVERROR(ENOMEM);
161 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
162 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
163 err = AVERROR(EINVAL);
164 goto load_plugin_fail;
167 for (i = 0; i < sizeof(uid.Data); i++) {
168 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
170 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
171 err = AVERROR(EINVAL);
172 goto load_plugin_fail;
177 ret = MFXVideoUSER_Load(session, &uid, 1);
180 snprintf(errorbuf, sizeof(errorbuf),
181 "Could not load the requested plugin '%s'", plugin);
182 err = ff_qsv_print_error(logctx, ret, errorbuf);
183 goto load_plugin_fail;
198 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
199 const char *load_plugins)
201 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
202 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
207 ret = MFXInit(impl, &ver, session);
209 return ff_qsv_print_error(avctx, ret,
210 "Error initializing an internal MFX session");
212 ret = qsv_load_plugins(*session, load_plugins, avctx);
214 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
218 MFXQueryIMPL(*session, &impl);
220 switch (MFX_IMPL_BASETYPE(impl)) {
221 case MFX_IMPL_SOFTWARE:
224 case MFX_IMPL_HARDWARE:
225 case MFX_IMPL_HARDWARE2:
226 case MFX_IMPL_HARDWARE3:
227 case MFX_IMPL_HARDWARE4:
228 desc = "hardware accelerated";
234 av_log(avctx, AV_LOG_VERBOSE,
235 "Initialized an internal MFX session using %s implementation\n",
241 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
242 mfxFrameAllocResponse *resp)
244 QSVFramesContext *ctx = pthis;
245 mfxFrameInfo *i = &req->Info;
246 mfxFrameInfo *i1 = &ctx->info;
248 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
249 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
250 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
251 return MFX_ERR_UNSUPPORTED;
252 if (i->Width != i1->Width || i->Height != i1->Height ||
253 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
254 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
255 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
256 i->Width, i->Height, i->FourCC, i->ChromaFormat,
257 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
258 return MFX_ERR_UNSUPPORTED;
261 resp->mids = ctx->mids;
262 resp->NumFrameActual = ctx->nb_mids;
267 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
272 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
274 return MFX_ERR_UNSUPPORTED;
277 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
279 return MFX_ERR_UNSUPPORTED;
282 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
288 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
289 QSVFramesContext *qsv_frames_ctx,
290 const char *load_plugins, int opaque)
292 static const mfxHandleType handle_types[] = {
293 MFX_HANDLE_VA_DISPLAY,
294 MFX_HANDLE_D3D9_DEVICE_MANAGER,
295 MFX_HANDLE_D3D11_DEVICE,
297 mfxFrameAllocator frame_allocator = {
298 .pthis = qsv_frames_ctx,
299 .Alloc = qsv_frame_alloc,
300 .Lock = qsv_frame_lock,
301 .Unlock = qsv_frame_unlock,
302 .GetHDL = qsv_frame_get_hdl,
303 .Free = qsv_frame_free,
306 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
307 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
308 AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
309 mfxSession parent_session = device_hwctx->session;
314 mfxHDL handle = NULL;
315 mfxHandleType handle_type;
320 err = MFXQueryIMPL(parent_session, &impl);
321 if (err == MFX_ERR_NONE)
322 err = MFXQueryVersion(parent_session, &ver);
323 if (err != MFX_ERR_NONE)
324 return ff_qsv_print_error(avctx, err,
325 "Error querying the session attributes");
327 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
328 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
329 if (err == MFX_ERR_NONE) {
330 handle_type = handle_types[i];
336 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
337 "from the session\n");
340 err = MFXInit(impl, &ver, &session);
341 if (err != MFX_ERR_NONE)
342 return ff_qsv_print_error(avctx, err,
343 "Error initializing a child MFX session");
346 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
347 if (err != MFX_ERR_NONE)
348 return ff_qsv_print_error(avctx, err,
349 "Error setting a HW handle");
352 ret = qsv_load_plugins(session, load_plugins, avctx);
354 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
359 av_freep(&qsv_frames_ctx->mids);
360 qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
361 sizeof(*qsv_frames_ctx->mids));
362 if (!qsv_frames_ctx->mids)
363 return AVERROR(ENOMEM);
365 qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
366 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
367 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
368 qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
370 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
371 if (err != MFX_ERR_NONE)
372 return ff_qsv_print_error(avctx, err,
373 "Error setting a frame allocator");