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 int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
95 case AV_PIX_FMT_YUV420P:
96 case AV_PIX_FMT_YUVJ420P:
97 *fourcc = MFX_FOURCC_NV12;
98 return AV_PIX_FMT_NV12;
99 case AV_PIX_FMT_YUV420P10:
100 *fourcc = MFX_FOURCC_P010;
101 return AV_PIX_FMT_P010;
103 return AVERROR(ENOSYS);
107 static int qsv_load_plugins(mfxSession session, const char *load_plugins,
110 if (!load_plugins || !*load_plugins)
113 while (*load_plugins) {
118 char *plugin = av_get_token(&load_plugins, ":");
120 return AVERROR(ENOMEM);
121 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
122 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
123 err = AVERROR(EINVAL);
124 goto load_plugin_fail;
127 for (i = 0; i < sizeof(uid.Data); i++) {
128 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
130 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
131 err = AVERROR(EINVAL);
132 goto load_plugin_fail;
137 ret = MFXVideoUSER_Load(session, &uid, 1);
139 av_log(logctx, AV_LOG_ERROR, "Could not load the requested plugin: %s\n",
141 err = ff_qsv_error(ret);
142 goto load_plugin_fail;
157 int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
158 const char *load_plugins)
160 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
161 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
166 ret = MFXInit(impl, &ver, session);
168 av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
169 return ff_qsv_error(ret);
172 ret = qsv_load_plugins(*session, load_plugins, avctx);
174 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
178 MFXQueryIMPL(*session, &impl);
180 switch (MFX_IMPL_BASETYPE(impl)) {
181 case MFX_IMPL_SOFTWARE:
184 case MFX_IMPL_HARDWARE:
185 case MFX_IMPL_HARDWARE2:
186 case MFX_IMPL_HARDWARE3:
187 case MFX_IMPL_HARDWARE4:
188 desc = "hardware accelerated";
194 av_log(avctx, AV_LOG_VERBOSE,
195 "Initialized an internal MFX session using %s implementation\n",
201 static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
202 mfxFrameAllocResponse *resp)
204 QSVFramesContext *ctx = pthis;
205 mfxFrameInfo *i = &req->Info;
206 mfxFrameInfo *i1 = &ctx->info;
208 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
209 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
210 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
211 return MFX_ERR_UNSUPPORTED;
212 if (i->Width != i1->Width || i->Height != i1->Height ||
213 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
214 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
215 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
216 i->Width, i->Height, i->FourCC, i->ChromaFormat,
217 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
218 return MFX_ERR_UNSUPPORTED;
221 resp->mids = ctx->mids;
222 resp->NumFrameActual = ctx->nb_mids;
227 static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
232 static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
234 return MFX_ERR_UNSUPPORTED;
237 static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
239 return MFX_ERR_UNSUPPORTED;
242 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
248 int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
249 QSVFramesContext *qsv_frames_ctx,
250 const char *load_plugins, int opaque)
252 static const mfxHandleType handle_types[] = {
253 MFX_HANDLE_VA_DISPLAY,
254 MFX_HANDLE_D3D9_DEVICE_MANAGER,
255 MFX_HANDLE_D3D11_DEVICE,
257 mfxFrameAllocator frame_allocator = {
258 .pthis = qsv_frames_ctx,
259 .Alloc = qsv_frame_alloc,
260 .Lock = qsv_frame_lock,
261 .Unlock = qsv_frame_unlock,
262 .GetHDL = qsv_frame_get_hdl,
263 .Free = qsv_frame_free,
266 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
267 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
268 AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
269 mfxSession parent_session = device_hwctx->session;
274 mfxHDL handle = NULL;
275 mfxHandleType handle_type;
280 err = MFXQueryIMPL(parent_session, &impl);
281 if (err == MFX_ERR_NONE)
282 err = MFXQueryVersion(parent_session, &ver);
283 if (err != MFX_ERR_NONE) {
284 av_log(avctx, AV_LOG_ERROR, "Error querying the session attributes\n");
285 return ff_qsv_error(err);
288 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
289 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
290 if (err == MFX_ERR_NONE) {
291 handle_type = handle_types[i];
297 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
298 "from the session\n");
301 err = MFXInit(impl, &ver, &session);
302 if (err != MFX_ERR_NONE) {
303 av_log(avctx, AV_LOG_ERROR,
304 "Error initializing a child MFX session: %d\n", err);
305 return ff_qsv_error(err);
309 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
310 if (err != MFX_ERR_NONE) {
311 av_log(avctx, AV_LOG_ERROR, "Error setting a HW handle: %d\n", err);
312 return ff_qsv_error(err);
316 ret = qsv_load_plugins(session, load_plugins, avctx);
318 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
323 av_freep(&qsv_frames_ctx->mids);
324 qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
325 sizeof(*qsv_frames_ctx->mids));
326 if (!qsv_frames_ctx->mids)
327 return AVERROR(ENOMEM);
329 qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
330 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
331 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
332 qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
334 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
335 if (err != MFX_ERR_NONE) {
336 av_log(avctx, AV_LOG_ERROR, "Error setting a frame allocator: %d\n", err);
337 return ff_qsv_error(err);