qsv{dec,enc}: use a struct as a memory id with internal memory allocator
[libav.git] / libavcodec / qsv.c
CommitLineData
d0a63d8b
AK
1/*
2 * Intel MediaSDK QSV encoder/decoder shared code
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <mfx/mfxvideo.h>
66acb76b 22#include <mfx/mfxplugin.h>
d0a63d8b 23
66acb76b
AK
24#include <stdio.h>
25#include <string.h>
26
27#include "libavutil/avstring.h"
a0524d9b 28#include "libavutil/common.h"
d0a63d8b 29#include "libavutil/error.h"
a0524d9b
AK
30#include "libavutil/hwcontext.h"
31#include "libavutil/hwcontext_qsv.h"
d0a63d8b
AK
32
33#include "avcodec.h"
34#include "qsv_internal.h"
35
36int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
37{
38 switch (codec_id) {
39 case AV_CODEC_ID_H264:
40 return MFX_CODEC_AVC;
66acb76b
AK
41#if QSV_VERSION_ATLEAST(1, 8)
42 case AV_CODEC_ID_HEVC:
43 return MFX_CODEC_HEVC;
44#endif
d0a63d8b
AK
45 case AV_CODEC_ID_MPEG1VIDEO:
46 case AV_CODEC_ID_MPEG2VIDEO:
47 return MFX_CODEC_MPEG2;
48 case AV_CODEC_ID_VC1:
49 return MFX_CODEC_VC1;
50 default:
51 break;
52 }
53
54 return AVERROR(ENOSYS);
55}
56
95414eb2
AK
57static const struct {
58 mfxStatus mfxerr;
59 int averr;
60 const char *desc;
61} qsv_errors[] = {
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" },
83
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" },
93};
94
95int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
d0a63d8b 96{
95414eb2
AK
97 int i;
98 for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
99 if (qsv_errors[i].mfxerr == mfx_err) {
100 if (desc)
101 *desc = qsv_errors[i].desc;
102 return qsv_errors[i].averr;
103 }
d0a63d8b 104 }
95414eb2
AK
105 if (desc)
106 *desc = "unknown error";
107 return AVERROR_UNKNOWN;
108}
109
110int ff_qsv_print_error(void *log_ctx, mfxStatus err,
111 const char *error_string)
112{
113 const char *desc;
114 int ret;
115 ret = ff_qsv_map_error(err, &desc);
116 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
117 return ret;
d0a63d8b
AK
118}
119
8e07c22e
AK
120int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
121 const char *warning_string)
122{
123 const char *desc;
124 int ret;
125 ret = ff_qsv_map_error(err, &desc);
126 av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
127 return ret;
128}
129
536bb17e
AK
130int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
131{
132 switch (format) {
133 case AV_PIX_FMT_YUV420P:
134 case AV_PIX_FMT_YUVJ420P:
21962261 135 case AV_PIX_FMT_NV12:
536bb17e
AK
136 *fourcc = MFX_FOURCC_NV12;
137 return AV_PIX_FMT_NV12;
92736c74 138 case AV_PIX_FMT_YUV420P10:
21962261 139 case AV_PIX_FMT_P010:
92736c74
AK
140 *fourcc = MFX_FOURCC_P010;
141 return AV_PIX_FMT_P010;
536bb17e
AK
142 default:
143 return AVERROR(ENOSYS);
144 }
145}
146
00aeedd8
AK
147int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
148{
149 int i;
150 for (i = 0; i < ctx->nb_mids; i++) {
151 QSVMid *mid = &ctx->mids[i];
152 if (mid->handle == frame->surface.Data.MemId)
153 return i;
154 }
155 return AVERROR_BUG;
156}
157
a0524d9b
AK
158static int qsv_load_plugins(mfxSession session, const char *load_plugins,
159 void *logctx)
160{
161 if (!load_plugins || !*load_plugins)
162 return 0;
163
164 while (*load_plugins) {
165 mfxPluginUID uid;
166 mfxStatus ret;
167 int i, err = 0;
168
169 char *plugin = av_get_token(&load_plugins, ":");
170 if (!plugin)
171 return AVERROR(ENOMEM);
172 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
173 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
174 err = AVERROR(EINVAL);
175 goto load_plugin_fail;
176 }
177
178 for (i = 0; i < sizeof(uid.Data); i++) {
179 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
180 if (err != 1) {
181 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
182 err = AVERROR(EINVAL);
183 goto load_plugin_fail;
184 }
185
186 }
187
188 ret = MFXVideoUSER_Load(session, &uid, 1);
189 if (ret < 0) {
95414eb2
AK
190 char errorbuf[128];
191 snprintf(errorbuf, sizeof(errorbuf),
192 "Could not load the requested plugin '%s'", plugin);
193 err = ff_qsv_print_error(logctx, ret, errorbuf);
a0524d9b
AK
194 goto load_plugin_fail;
195 }
196
197 if (*load_plugins)
198 load_plugins++;
199load_plugin_fail:
200 av_freep(&plugin);
201 if (err < 0)
202 return err;
203 }
204
205 return 0;
206
207}
208
66acb76b
AK
209int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
210 const char *load_plugins)
d0a63d8b
AK
211{
212 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
213 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
214
215 const char *desc;
216 int ret;
217
218 ret = MFXInit(impl, &ver, session);
95414eb2
AK
219 if (ret < 0)
220 return ff_qsv_print_error(avctx, ret,
221 "Error initializing an internal MFX session");
d0a63d8b 222
a0524d9b
AK
223 ret = qsv_load_plugins(*session, load_plugins, avctx);
224 if (ret < 0) {
225 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
226 return ret;
66acb76b
AK
227 }
228
ce9d7da7
LB
229 MFXQueryIMPL(*session, &impl);
230
231 switch (MFX_IMPL_BASETYPE(impl)) {
232 case MFX_IMPL_SOFTWARE:
233 desc = "software";
234 break;
235 case MFX_IMPL_HARDWARE:
236 case MFX_IMPL_HARDWARE2:
237 case MFX_IMPL_HARDWARE3:
238 case MFX_IMPL_HARDWARE4:
239 desc = "hardware accelerated";
240 break;
241 default:
242 desc = "unknown";
243 }
244
d0a63d8b
AK
245 av_log(avctx, AV_LOG_VERBOSE,
246 "Initialized an internal MFX session using %s implementation\n",
247 desc);
248
249 return 0;
250}
a0524d9b
AK
251
252static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
253 mfxFrameAllocResponse *resp)
254{
255 QSVFramesContext *ctx = pthis;
256 mfxFrameInfo *i = &req->Info;
257 mfxFrameInfo *i1 = &ctx->info;
00aeedd8 258 int j;
a0524d9b
AK
259
260 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
ad9c9440 261 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
a0524d9b
AK
262 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
263 return MFX_ERR_UNSUPPORTED;
264 if (i->Width != i1->Width || i->Height != i1->Height ||
265 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
266 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
267 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
268 i->Width, i->Height, i->FourCC, i->ChromaFormat,
269 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
270 return MFX_ERR_UNSUPPORTED;
271 }
272
00aeedd8
AK
273 resp->mids = av_mallocz_array(ctx->nb_mids, sizeof(*resp->mids));
274 if (!resp->mids)
275 return MFX_ERR_MEMORY_ALLOC;
276
277 for (j = 0; j < ctx->nb_mids; j++)
278 resp->mids[j] = &ctx->mids[j];
279
a0524d9b
AK
280 resp->NumFrameActual = ctx->nb_mids;
281
282 return MFX_ERR_NONE;
283}
284
285static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
286{
00aeedd8 287 av_freep(&resp->mids);
a0524d9b
AK
288 return MFX_ERR_NONE;
289}
290
291static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
292{
293 return MFX_ERR_UNSUPPORTED;
294}
295
296static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
297{
298 return MFX_ERR_UNSUPPORTED;
299}
300
301static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
302{
00aeedd8
AK
303 QSVMid *qsv_mid = (QSVMid*)mid;
304 *hdl = qsv_mid->handle;
a0524d9b
AK
305 return MFX_ERR_NONE;
306}
307
308int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
309 QSVFramesContext *qsv_frames_ctx,
310 const char *load_plugins, int opaque)
311{
312 static const mfxHandleType handle_types[] = {
313 MFX_HANDLE_VA_DISPLAY,
314 MFX_HANDLE_D3D9_DEVICE_MANAGER,
315 MFX_HANDLE_D3D11_DEVICE,
316 };
317 mfxFrameAllocator frame_allocator = {
318 .pthis = qsv_frames_ctx,
319 .Alloc = qsv_frame_alloc,
320 .Lock = qsv_frame_lock,
321 .Unlock = qsv_frame_unlock,
322 .GetHDL = qsv_frame_get_hdl,
323 .Free = qsv_frame_free,
324 };
325
326 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
327 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
328 AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
329 mfxSession parent_session = device_hwctx->session;
330
331 mfxSession session;
332 mfxVersion ver;
333 mfxIMPL impl;
334 mfxHDL handle = NULL;
335 mfxHandleType handle_type;
336 mfxStatus err;
337
338 int i, ret;
339
340 err = MFXQueryIMPL(parent_session, &impl);
341 if (err == MFX_ERR_NONE)
342 err = MFXQueryVersion(parent_session, &ver);
95414eb2
AK
343 if (err != MFX_ERR_NONE)
344 return ff_qsv_print_error(avctx, err,
345 "Error querying the session attributes");
a0524d9b
AK
346
347 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
348 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
349 if (err == MFX_ERR_NONE) {
350 handle_type = handle_types[i];
351 break;
352 }
353 handle = NULL;
354 }
355 if (!handle) {
356 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
357 "from the session\n");
358 }
359
360 err = MFXInit(impl, &ver, &session);
95414eb2
AK
361 if (err != MFX_ERR_NONE)
362 return ff_qsv_print_error(avctx, err,
363 "Error initializing a child MFX session");
a0524d9b
AK
364
365 if (handle) {
366 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
95414eb2
AK
367 if (err != MFX_ERR_NONE)
368 return ff_qsv_print_error(avctx, err,
369 "Error setting a HW handle");
a0524d9b
AK
370 }
371
372 ret = qsv_load_plugins(session, load_plugins, avctx);
373 if (ret < 0) {
374 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
375 return ret;
376 }
377
378 if (!opaque) {
379 av_freep(&qsv_frames_ctx->mids);
380 qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
381 sizeof(*qsv_frames_ctx->mids));
382 if (!qsv_frames_ctx->mids)
383 return AVERROR(ENOMEM);
384
385 qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
386 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
387 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
00aeedd8 388 qsv_frames_ctx->mids[i].handle = frames_hwctx->surfaces[i].Data.MemId;
a0524d9b
AK
389
390 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
95414eb2
AK
391 if (err != MFX_ERR_NONE)
392 return ff_qsv_print_error(avctx, err,
393 "Error setting a frame allocator");
a0524d9b
AK
394 }
395
396 *psession = session;
397 return 0;
398}