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