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);
57 int ff_qsv_error(int mfx_err)
62 case MFX_ERR_MEMORY_ALLOC:
63 case MFX_ERR_NOT_ENOUGH_BUFFER:
64 return AVERROR(ENOMEM);
65 case MFX_ERR_INVALID_HANDLE:
66 return AVERROR(EINVAL);
67 case MFX_ERR_DEVICE_FAILED:
68 case MFX_ERR_DEVICE_LOST:
69 case MFX_ERR_LOCK_MEMORY:
71 case MFX_ERR_NULL_PTR:
72 case MFX_ERR_UNDEFINED_BEHAVIOR:
73 case MFX_ERR_NOT_INITIALIZED:
75 case MFX_ERR_UNSUPPORTED:
76 case MFX_ERR_NOT_FOUND:
77 return AVERROR(ENOSYS);
78 case MFX_ERR_MORE_DATA:
79 case MFX_ERR_MORE_SURFACE:
80 case MFX_ERR_MORE_BITSTREAM:
81 return AVERROR(EAGAIN);
82 case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
83 case MFX_ERR_INVALID_VIDEO_PARAM:
84 return AVERROR(EINVAL);
88 return AVERROR_UNKNOWN;
92 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
95 if (!load_plugins || !*load_plugins)
98 while (*load_plugins) {
103 char *plugin = av_get_token(&load_plugins, ":");
105 return AVERROR(ENOMEM);
106 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
107 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
108 err = AVERROR(EINVAL);
109 goto load_plugin_fail;
112 for (i = 0; i < sizeof(uid.Data); i++) {
113 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
115 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
116 err = AVERROR(EINVAL);
117 goto load_plugin_fail;
122 ret = MFXVideoUSER_Load(session, &uid, 1);
124 av_log(logctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
126 err = ff_qsv_error(ret);
127 goto load_plugin_fail;
142 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
143 const char *load_plugins)
145 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
146 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
151 ret = MFXInit(impl, &ver, session);
153 av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
154 return ff_qsv_error(ret);
157 ret = qsv_load_plugins(*session, load_plugins, avctx);
159 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
163 MFXQueryIMPL(*session, &impl);
165 switch (MFX_IMPL_BASETYPE(impl)) {
166 case MFX_IMPL_SOFTWARE:
169 case MFX_IMPL_HARDWARE:
170 case MFX_IMPL_HARDWARE2:
171 case MFX_IMPL_HARDWARE3:
172 case MFX_IMPL_HARDWARE4:
173 desc = "hardware accelerated";
179 av_log(avctx, AV_LOG_VERBOSE,
180 "Initialized an internal MFX session using %s implementation\n",
186 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
187 mfxFrameAllocResponse *resp)
189 QSVFramesContext *ctx = pthis;
190 mfxFrameInfo *i = &req->Info;
191 mfxFrameInfo *i1 = &ctx->info;
193 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
194 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
195 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
196 return MFX_ERR_UNSUPPORTED;
197 if (i->Width != i1->Width || i->Height != i1->Height ||
198 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
199 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
200 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
201 i->Width, i->Height, i->FourCC, i->ChromaFormat,
202 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
203 return MFX_ERR_UNSUPPORTED;
206 resp->mids = ctx->mids;
207 resp->NumFrameActual = ctx->nb_mids;
212 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
217 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
219 return MFX_ERR_UNSUPPORTED;
222 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
224 return MFX_ERR_UNSUPPORTED;
227 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
233 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
234 QSVFramesContext *qsv_frames_ctx,
235 const char *load_plugins, int opaque)
237 static const mfxHandleType handle_types[] = {
238 MFX_HANDLE_VA_DISPLAY,
239 MFX_HANDLE_D3D9_DEVICE_MANAGER,
240 MFX_HANDLE_D3D11_DEVICE,
242 mfxFrameAllocator frame_allocator = {
243 .pthis = qsv_frames_ctx,
244 .Alloc = qsv_frame_alloc,
245 .Lock = qsv_frame_lock,
246 .Unlock = qsv_frame_unlock,
247 .GetHDL = qsv_frame_get_hdl,
248 .Free = qsv_frame_free,
251 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
252 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
253 AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
254 mfxSession parent_session = device_hwctx->session;
259 mfxHDL handle = NULL;
260 mfxHandleType handle_type;
265 err = MFXQueryIMPL(parent_session, &impl);
266 if (err == MFX_ERR_NONE)
267 err = MFXQueryVersion(parent_session, &ver);
268 if (err != MFX_ERR_NONE) {
269 av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
270 return ff_qsv_error(err);
273 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
274 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
275 if (err == MFX_ERR_NONE) {
276 handle_type = handle_types[i];
282 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
283 "from the session\n");
286 err = MFXInit(impl, &ver, &session);
287 if (err != MFX_ERR_NONE) {
288 av_log(avctx, AV_LOG_ERROR,
289 "Error initializing a child MFX session: %d\n", err);
290 return ff_qsv_error(err);
294 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
295 if (err != MFX_ERR_NONE) {
296 av_log(avctx, AV_LOG_ERROR, "Error setting a HW handle: %d\n", err);
297 return ff_qsv_error(err);
301 ret = qsv_load_plugins(session, load_plugins, avctx);
303 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
308 av_freep(&qsv_frames_ctx->mids);
309 qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
310 sizeof(*qsv_frames_ctx->mids));
311 if (!qsv_frames_ctx->mids)
312 return AVERROR(ENOMEM);
314 qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
315 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
316 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
317 qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
319 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
320 if (err != MFX_ERR_NONE) {
321 av_log(avctx, AV_LOG_ERROR, "Error setting a frame allocator: %d\n", err);
322 return ff_qsv_error(err);