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 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
111 if (!load_plugins || !*load_plugins)
114 while (*load_plugins) {
119 char *plugin = av_get_token(&load_plugins, ":");
121 return AVERROR(ENOMEM);
122 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
123 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
124 err = AVERROR(EINVAL);
125 goto load_plugin_fail;
128 for (i = 0; i < sizeof(uid.Data); i++) {
129 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
131 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
132 err = AVERROR(EINVAL);
133 goto load_plugin_fail;
138 ret = MFXVideoUSER_Load(session, &uid, 1);
140 av_log(logctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
142 err = ff_qsv_error(ret);
143 goto load_plugin_fail;
158 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
159 const char *load_plugins)
161 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
162 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
167 ret = MFXInit(impl, &ver, session);
169 av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
170 return ff_qsv_error(ret);
173 ret = qsv_load_plugins(*session, load_plugins, avctx);
175 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
179 MFXQueryIMPL(*session, &impl);
181 switch (MFX_IMPL_BASETYPE(impl)) {
182 case MFX_IMPL_SOFTWARE:
185 case MFX_IMPL_HARDWARE:
186 case MFX_IMPL_HARDWARE2:
187 case MFX_IMPL_HARDWARE3:
188 case MFX_IMPL_HARDWARE4:
189 desc = "hardware accelerated";
195 av_log(avctx, AV_LOG_VERBOSE,
196 "Initialized an internal MFX session using %s implementation\n",
202 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
203 mfxFrameAllocResponse *resp)
205 QSVFramesContext *ctx = pthis;
206 mfxFrameInfo *i = &req->Info;
207 mfxFrameInfo *i1 = &ctx->info;
209 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
210 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
211 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
212 return MFX_ERR_UNSUPPORTED;
213 if (i->Width != i1->Width || i->Height != i1->Height ||
214 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
215 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
216 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
217 i->Width, i->Height, i->FourCC, i->ChromaFormat,
218 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
219 return MFX_ERR_UNSUPPORTED;
222 resp->mids = ctx->mids;
223 resp->NumFrameActual = ctx->nb_mids;
228 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
233 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
235 return MFX_ERR_UNSUPPORTED;
238 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
240 return MFX_ERR_UNSUPPORTED;
243 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
249 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
250 QSVFramesContext *qsv_frames_ctx,
251 const char *load_plugins, int opaque)
253 static const mfxHandleType handle_types[] = {
254 MFX_HANDLE_VA_DISPLAY,
255 MFX_HANDLE_D3D9_DEVICE_MANAGER,
256 MFX_HANDLE_D3D11_DEVICE,
258 mfxFrameAllocator frame_allocator = {
259 .pthis = qsv_frames_ctx,
260 .Alloc = qsv_frame_alloc,
261 .Lock = qsv_frame_lock,
262 .Unlock = qsv_frame_unlock,
263 .GetHDL = qsv_frame_get_hdl,
264 .Free = qsv_frame_free,
267 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
268 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
269 AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
270 mfxSession parent_session = device_hwctx->session;
275 mfxHDL handle = NULL;
276 mfxHandleType handle_type;
281 err = MFXQueryIMPL(parent_session, &impl);
282 if (err == MFX_ERR_NONE)
283 err = MFXQueryVersion(parent_session, &ver);
284 if (err != MFX_ERR_NONE) {
285 av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
286 return ff_qsv_error(err);
289 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
290 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
291 if (err == MFX_ERR_NONE) {
292 handle_type = handle_types[i];
298 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
299 "from the session\n");
302 err = MFXInit(impl, &ver, &session);
303 if (err != MFX_ERR_NONE) {
304 av_log(avctx, AV_LOG_ERROR,
305 "Error initializing a child MFX session: %d\n", err);
306 return ff_qsv_error(err);
310 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
311 if (err != MFX_ERR_NONE) {
312 av_log(avctx, AV_LOG_ERROR, "Error setting a HW handle: %d\n", err);
313 return ff_qsv_error(err);
317 ret = qsv_load_plugins(session, load_plugins, avctx);
319 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
324 av_freep(&qsv_frames_ctx->mids);
325 qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
326 sizeof(*qsv_frames_ctx->mids));
327 if (!qsv_frames_ctx->mids)
328 return AVERROR(ENOMEM);
330 qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
331 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
332 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
333 qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
335 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
336 if (err != MFX_ERR_NONE) {
337 av_log(avctx, AV_LOG_ERROR, "Error setting a frame allocator: %d\n", err);
338 return ff_qsv_error(err);