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>
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);
57 int ff_qsv_profile_to_mfx(enum AVCodecID codec_id, int profile)
59 if (profile == FF_PROFILE_UNKNOWN)
60 return MFX_PROFILE_UNKNOWN;
62 case AV_CODEC_ID_H264:
63 case AV_CODEC_ID_HEVC:
66 return 4 * profile + 1;
67 case AV_CODEC_ID_MPEG2VIDEO:
68 return 0x10 * profile;
70 return MFX_PROFILE_UNKNOWN;
73 int ff_qsv_error(int mfx_err)
78 case MFX_ERR_MEMORY_ALLOC:
79 case MFX_ERR_NOT_ENOUGH_BUFFER:
80 return AVERROR(ENOMEM);
81 case MFX_ERR_INVALID_HANDLE:
82 return AVERROR(EINVAL);
83 case MFX_ERR_DEVICE_FAILED:
84 case MFX_ERR_DEVICE_LOST:
85 case MFX_ERR_LOCK_MEMORY:
87 case MFX_ERR_NULL_PTR:
88 case MFX_ERR_UNDEFINED_BEHAVIOR:
89 case MFX_ERR_NOT_INITIALIZED:
91 case MFX_ERR_UNSUPPORTED:
92 case MFX_ERR_NOT_FOUND:
93 return AVERROR(ENOSYS);
94 case MFX_ERR_MORE_DATA:
95 case MFX_ERR_MORE_SURFACE:
96 case MFX_ERR_MORE_BITSTREAM:
97 return AVERROR(EAGAIN);
98 case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
99 case MFX_ERR_INVALID_VIDEO_PARAM:
100 return AVERROR(EINVAL);
101 case MFX_ERR_ABORTED:
102 case MFX_ERR_UNKNOWN:
104 return AVERROR_UNKNOWN;
108 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
111 case AV_PIX_FMT_YUV420P:
112 case AV_PIX_FMT_YUVJ420P:
113 *fourcc = MFX_FOURCC_NV12;
114 return AV_PIX_FMT_NV12;
115 case AV_PIX_FMT_YUV420P10:
116 *fourcc = MFX_FOURCC_P010;
117 return AV_PIX_FMT_P010;
119 return AVERROR(ENOSYS);
123 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
126 if (!load_plugins || !*load_plugins)
129 while (*load_plugins) {
134 char *plugin = av_get_token(&load_plugins, ":");
136 return AVERROR(ENOMEM);
137 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
138 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
139 err = AVERROR(EINVAL);
140 goto load_plugin_fail;
143 for (i = 0; i < sizeof(uid.Data); i++) {
144 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
146 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
147 err = AVERROR(EINVAL);
148 goto load_plugin_fail;
153 ret = MFXVideoUSER_Load(session, &uid, 1);
155 av_log(logctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
157 err = ff_qsv_error(ret);
158 goto load_plugin_fail;
173 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
174 const char *load_plugins)
176 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
177 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
182 ret = MFXInit(impl, &ver, session);
184 av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
185 return ff_qsv_error(ret);
188 ret = qsv_load_plugins(*session, load_plugins, avctx);
190 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
194 MFXQueryIMPL(*session, &impl);
196 switch (MFX_IMPL_BASETYPE(impl)) {
197 case MFX_IMPL_SOFTWARE:
200 case MFX_IMPL_HARDWARE:
201 case MFX_IMPL_HARDWARE2:
202 case MFX_IMPL_HARDWARE3:
203 case MFX_IMPL_HARDWARE4:
204 desc = "hardware accelerated";
210 av_log(avctx, AV_LOG_VERBOSE,
211 "Initialized an internal MFX session using %s implementation\n",
217 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
218 mfxFrameAllocResponse *resp)
220 QSVFramesContext *ctx = pthis;
221 mfxFrameInfo *i = &req->Info;
222 mfxFrameInfo *i1 = &ctx->info;
224 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
225 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
226 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
227 return MFX_ERR_UNSUPPORTED;
228 if (i->Width != i1->Width || i->Height != i1->Height ||
229 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
230 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
231 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
232 i->Width, i->Height, i->FourCC, i->ChromaFormat,
233 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
234 return MFX_ERR_UNSUPPORTED;
237 resp->mids = ctx->mids;
238 resp->NumFrameActual = ctx->nb_mids;
243 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
248 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
250 return MFX_ERR_UNSUPPORTED;
253 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
255 return MFX_ERR_UNSUPPORTED;
258 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
264 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
265 QSVFramesContext *qsv_frames_ctx,
266 const char *load_plugins, int opaque)
268 static const mfxHandleType handle_types[] = {
269 MFX_HANDLE_VA_DISPLAY,
270 MFX_HANDLE_D3D9_DEVICE_MANAGER,
271 MFX_HANDLE_D3D11_DEVICE,
273 mfxFrameAllocator frame_allocator = {
274 .pthis = qsv_frames_ctx,
275 .Alloc = qsv_frame_alloc,
276 .Lock = qsv_frame_lock,
277 .Unlock = qsv_frame_unlock,
278 .GetHDL = qsv_frame_get_hdl,
279 .Free = qsv_frame_free,
282 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
283 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
284 AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
285 mfxSession parent_session = device_hwctx->session;
290 mfxHDL handle = NULL;
291 mfxHandleType handle_type;
296 err = MFXQueryIMPL(parent_session, &impl);
297 if (err == MFX_ERR_NONE)
298 err = MFXQueryVersion(parent_session, &ver);
299 if (err != MFX_ERR_NONE) {
300 av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
301 return ff_qsv_error(err);
304 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
305 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
306 if (err == MFX_ERR_NONE) {
307 handle_type = handle_types[i];
313 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
314 "from the session\n");
317 err = MFXInit(impl, &ver, &session);
318 if (err != MFX_ERR_NONE) {
319 av_log(avctx, AV_LOG_ERROR,
320 "Error initializing a child MFX session: %d\n", err);
321 return ff_qsv_error(err);
325 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
326 if (err != MFX_ERR_NONE) {
327 av_log(avctx, AV_LOG_ERROR, "Error setting a HW handle: %d\n", err);
328 return ff_qsv_error(err);
332 ret = qsv_load_plugins(session, load_plugins, avctx);
334 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
339 av_freep(&qsv_frames_ctx->mids);
340 qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
341 sizeof(*qsv_frames_ctx->mids));
342 if (!qsv_frames_ctx->mids)
343 return AVERROR(ENOMEM);
345 qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
346 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
347 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
348 qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
350 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
351 if (err != MFX_ERR_NONE) {
352 av_log(avctx, AV_LOG_ERROR, "Error setting a frame allocator: %d\n", err);
353 return ff_qsv_error(err);