qsvenc: do not re-execute encoding on all positive status codes
[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
536bb17e
AK
120int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
121{
122 switch (format) {
123 case AV_PIX_FMT_YUV420P:
124 case AV_PIX_FMT_YUVJ420P:
21962261 125 case AV_PIX_FMT_NV12:
536bb17e
AK
126 *fourcc = MFX_FOURCC_NV12;
127 return AV_PIX_FMT_NV12;
92736c74 128 case AV_PIX_FMT_YUV420P10:
21962261 129 case AV_PIX_FMT_P010:
92736c74
AK
130 *fourcc = MFX_FOURCC_P010;
131 return AV_PIX_FMT_P010;
536bb17e
AK
132 default:
133 return AVERROR(ENOSYS);
134 }
135}
136
a0524d9b
AK
137static int qsv_load_plugins(mfxSession session, const char *load_plugins,
138 void *logctx)
139{
140 if (!load_plugins || !*load_plugins)
141 return 0;
142
143 while (*load_plugins) {
144 mfxPluginUID uid;
145 mfxStatus ret;
146 int i, err = 0;
147
148 char *plugin = av_get_token(&load_plugins, ":");
149 if (!plugin)
150 return AVERROR(ENOMEM);
151 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
152 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
153 err = AVERROR(EINVAL);
154 goto load_plugin_fail;
155 }
156
157 for (i = 0; i < sizeof(uid.Data); i++) {
158 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
159 if (err != 1) {
160 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
161 err = AVERROR(EINVAL);
162 goto load_plugin_fail;
163 }
164
165 }
166
167 ret = MFXVideoUSER_Load(session, &uid, 1);
168 if (ret < 0) {
95414eb2
AK
169 char errorbuf[128];
170 snprintf(errorbuf, sizeof(errorbuf),
171 "Could not load the requested plugin '%s'", plugin);
172 err = ff_qsv_print_error(logctx, ret, errorbuf);
a0524d9b
AK
173 goto load_plugin_fail;
174 }
175
176 if (*load_plugins)
177 load_plugins++;
178load_plugin_fail:
179 av_freep(&plugin);
180 if (err < 0)
181 return err;
182 }
183
184 return 0;
185
186}
187
66acb76b
AK
188int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
189 const char *load_plugins)
d0a63d8b
AK
190{
191 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
192 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
193
194 const char *desc;
195 int ret;
196
197 ret = MFXInit(impl, &ver, session);
95414eb2
AK
198 if (ret < 0)
199 return ff_qsv_print_error(avctx, ret,
200 "Error initializing an internal MFX session");
d0a63d8b 201
a0524d9b
AK
202 ret = qsv_load_plugins(*session, load_plugins, avctx);
203 if (ret < 0) {
204 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
205 return ret;
66acb76b
AK
206 }
207
ce9d7da7
LB
208 MFXQueryIMPL(*session, &impl);
209
210 switch (MFX_IMPL_BASETYPE(impl)) {
211 case MFX_IMPL_SOFTWARE:
212 desc = "software";
213 break;
214 case MFX_IMPL_HARDWARE:
215 case MFX_IMPL_HARDWARE2:
216 case MFX_IMPL_HARDWARE3:
217 case MFX_IMPL_HARDWARE4:
218 desc = "hardware accelerated";
219 break;
220 default:
221 desc = "unknown";
222 }
223
d0a63d8b
AK
224 av_log(avctx, AV_LOG_VERBOSE,
225 "Initialized an internal MFX session using %s implementation\n",
226 desc);
227
228 return 0;
229}
a0524d9b
AK
230
231static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
232 mfxFrameAllocResponse *resp)
233{
234 QSVFramesContext *ctx = pthis;
235 mfxFrameInfo *i = &req->Info;
236 mfxFrameInfo *i1 = &ctx->info;
237
238 if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) ||
ad9c9440 239 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)) ||
a0524d9b
AK
240 !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
241 return MFX_ERR_UNSUPPORTED;
242 if (i->Width != i1->Width || i->Height != i1->Height ||
243 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
244 av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
245 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
246 i->Width, i->Height, i->FourCC, i->ChromaFormat,
247 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
248 return MFX_ERR_UNSUPPORTED;
249 }
250
251 resp->mids = ctx->mids;
252 resp->NumFrameActual = ctx->nb_mids;
253
254 return MFX_ERR_NONE;
255}
256
257static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
258{
259 return MFX_ERR_NONE;
260}
261
262static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
263{
264 return MFX_ERR_UNSUPPORTED;
265}
266
267static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
268{
269 return MFX_ERR_UNSUPPORTED;
270}
271
272static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
273{
274 *hdl = mid;
275 return MFX_ERR_NONE;
276}
277
278int ff_qsv_init_session_hwcontext(AVCodecContext *avctx, mfxSession *psession,
279 QSVFramesContext *qsv_frames_ctx,
280 const char *load_plugins, int opaque)
281{
282 static const mfxHandleType handle_types[] = {
283 MFX_HANDLE_VA_DISPLAY,
284 MFX_HANDLE_D3D9_DEVICE_MANAGER,
285 MFX_HANDLE_D3D11_DEVICE,
286 };
287 mfxFrameAllocator frame_allocator = {
288 .pthis = qsv_frames_ctx,
289 .Alloc = qsv_frame_alloc,
290 .Lock = qsv_frame_lock,
291 .Unlock = qsv_frame_unlock,
292 .GetHDL = qsv_frame_get_hdl,
293 .Free = qsv_frame_free,
294 };
295
296 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
297 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
298 AVQSVDeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
299 mfxSession parent_session = device_hwctx->session;
300
301 mfxSession session;
302 mfxVersion ver;
303 mfxIMPL impl;
304 mfxHDL handle = NULL;
305 mfxHandleType handle_type;
306 mfxStatus err;
307
308 int i, ret;
309
310 err = MFXQueryIMPL(parent_session, &impl);
311 if (err == MFX_ERR_NONE)
312 err = MFXQueryVersion(parent_session, &ver);
95414eb2
AK
313 if (err != MFX_ERR_NONE)
314 return ff_qsv_print_error(avctx, err,
315 "Error querying the session attributes");
a0524d9b
AK
316
317 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
318 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
319 if (err == MFX_ERR_NONE) {
320 handle_type = handle_types[i];
321 break;
322 }
323 handle = NULL;
324 }
325 if (!handle) {
326 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
327 "from the session\n");
328 }
329
330 err = MFXInit(impl, &ver, &session);
95414eb2
AK
331 if (err != MFX_ERR_NONE)
332 return ff_qsv_print_error(avctx, err,
333 "Error initializing a child MFX session");
a0524d9b
AK
334
335 if (handle) {
336 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
95414eb2
AK
337 if (err != MFX_ERR_NONE)
338 return ff_qsv_print_error(avctx, err,
339 "Error setting a HW handle");
a0524d9b
AK
340 }
341
342 ret = qsv_load_plugins(session, load_plugins, avctx);
343 if (ret < 0) {
344 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
345 return ret;
346 }
347
348 if (!opaque) {
349 av_freep(&qsv_frames_ctx->mids);
350 qsv_frames_ctx->mids = av_mallocz_array(frames_hwctx->nb_surfaces,
351 sizeof(*qsv_frames_ctx->mids));
352 if (!qsv_frames_ctx->mids)
353 return AVERROR(ENOMEM);
354
355 qsv_frames_ctx->info = frames_hwctx->surfaces[0].Info;
356 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
357 for (i = 0; i < frames_hwctx->nb_surfaces; i++)
358 qsv_frames_ctx->mids[i] = frames_hwctx->surfaces[i].Data.MemId;
359
360 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
95414eb2
AK
361 if (err != MFX_ERR_NONE)
362 return ff_qsv_print_error(avctx, err,
363 "Error setting a frame allocator");
a0524d9b
AK
364 }
365
366 *psession = session;
367 return 0;
368}