libaom: remove references to RGB pixfmts
[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>
2fd6e7d0 23#include <mfx/mfxjpeg.h>
d0a63d8b 24
66acb76b
AK
25#include <stdio.h>
26#include <string.h>
27
28#include "libavutil/avstring.h"
a0524d9b 29#include "libavutil/common.h"
d0a63d8b 30#include "libavutil/error.h"
a0524d9b
AK
31#include "libavutil/hwcontext.h"
32#include "libavutil/hwcontext_qsv.h"
4ab61cd9 33#include "libavutil/imgutils.h"
d0a63d8b
AK
34
35#include "avcodec.h"
36#include "qsv_internal.h"
37
e0b16457
MT
38#if QSV_VERSION_ATLEAST(1, 12)
39#include "mfx/mfxvp8.h"
40#endif
41
d0a63d8b
AK
42int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
43{
44 switch (codec_id) {
45 case AV_CODEC_ID_H264:
46 return MFX_CODEC_AVC;
66acb76b
AK
47#if QSV_VERSION_ATLEAST(1, 8)
48 case AV_CODEC_ID_HEVC:
49 return MFX_CODEC_HEVC;
50#endif
d0a63d8b
AK
51 case AV_CODEC_ID_MPEG1VIDEO:
52 case AV_CODEC_ID_MPEG2VIDEO:
53 return MFX_CODEC_MPEG2;
54 case AV_CODEC_ID_VC1:
55 return MFX_CODEC_VC1;
e0b16457
MT
56#if QSV_VERSION_ATLEAST(1, 12)
57 case AV_CODEC_ID_VP8:
58 return MFX_CODEC_VP8;
59#endif
2fd6e7d0
HZ
60 case AV_CODEC_ID_MJPEG:
61 return MFX_CODEC_JPEG;
d0a63d8b
AK
62 default:
63 break;
64 }
65
66 return AVERROR(ENOSYS);
67}
68
cd1047f3
MT
69int ff_qsv_profile_to_mfx(enum AVCodecID codec_id, int profile)
70{
71 if (profile == FF_PROFILE_UNKNOWN)
72 return MFX_PROFILE_UNKNOWN;
73 switch (codec_id) {
74 case AV_CODEC_ID_H264:
75 case AV_CODEC_ID_HEVC:
76 return profile;
77 case AV_CODEC_ID_VC1:
78 return 4 * profile + 1;
79 case AV_CODEC_ID_MPEG2VIDEO:
80 return 0x10 * profile;
81 }
82 return MFX_PROFILE_UNKNOWN;
83}
84
95414eb2
AK
85static const struct {
86 mfxStatus mfxerr;
87 int averr;
88 const char *desc;
89} qsv_errors[] = {
90 { MFX_ERR_NONE, 0, "success" },
91 { MFX_ERR_UNKNOWN, AVERROR_UNKNOWN, "unknown error" },
92 { MFX_ERR_NULL_PTR, AVERROR(EINVAL), "NULL pointer" },
93 { MFX_ERR_UNSUPPORTED, AVERROR(ENOSYS), "unsupported" },
94 { MFX_ERR_MEMORY_ALLOC, AVERROR(ENOMEM), "failed to allocate memory" },
95 { MFX_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOMEM), "insufficient input/output buffer" },
96 { MFX_ERR_INVALID_HANDLE, AVERROR(EINVAL), "invalid handle" },
97 { MFX_ERR_LOCK_MEMORY, AVERROR(EIO), "failed to lock the memory block" },
98 { MFX_ERR_NOT_INITIALIZED, AVERROR_BUG, "not initialized" },
99 { MFX_ERR_NOT_FOUND, AVERROR(ENOSYS), "specified object was not found" },
984736dd
AK
100 /* the following 3 errors should always be handled explicitly, so those "mappings"
101 * are for completeness only */
102 { MFX_ERR_MORE_DATA, AVERROR_UNKNOWN, "expect more data at input" },
103 { MFX_ERR_MORE_SURFACE, AVERROR_UNKNOWN, "expect more surface at output" },
104 { MFX_ERR_MORE_BITSTREAM, AVERROR_UNKNOWN, "expect more bitstream at output" },
95414eb2
AK
105 { MFX_ERR_ABORTED, AVERROR_UNKNOWN, "operation aborted" },
106 { MFX_ERR_DEVICE_LOST, AVERROR(EIO), "device lost" },
107 { MFX_ERR_INCOMPATIBLE_VIDEO_PARAM, AVERROR(EINVAL), "incompatible video parameters" },
108 { MFX_ERR_INVALID_VIDEO_PARAM, AVERROR(EINVAL), "invalid video parameters" },
109 { MFX_ERR_UNDEFINED_BEHAVIOR, AVERROR_BUG, "undefined behavior" },
110 { MFX_ERR_DEVICE_FAILED, AVERROR(EIO), "device failed" },
95414eb2
AK
111 { MFX_ERR_INCOMPATIBLE_AUDIO_PARAM, AVERROR(EINVAL), "incompatible audio parameters" },
112 { MFX_ERR_INVALID_AUDIO_PARAM, AVERROR(EINVAL), "invalid audio parameters" },
113
114 { MFX_WRN_IN_EXECUTION, 0, "operation in execution" },
115 { MFX_WRN_DEVICE_BUSY, 0, "device busy" },
116 { MFX_WRN_VIDEO_PARAM_CHANGED, 0, "video parameters changed" },
117 { MFX_WRN_PARTIAL_ACCELERATION, 0, "partial acceleration" },
118 { MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, 0, "incompatible video parameters" },
119 { MFX_WRN_VALUE_NOT_CHANGED, 0, "value is saturated" },
120 { MFX_WRN_OUT_OF_RANGE, 0, "value out of range" },
121 { MFX_WRN_FILTER_SKIPPED, 0, "filter skipped" },
122 { MFX_WRN_INCOMPATIBLE_AUDIO_PARAM, 0, "incompatible audio parameters" },
123};
124
125int ff_qsv_map_error(mfxStatus mfx_err, const char **desc)
d0a63d8b 126{
95414eb2
AK
127 int i;
128 for (i = 0; i < FF_ARRAY_ELEMS(qsv_errors); i++) {
129 if (qsv_errors[i].mfxerr == mfx_err) {
130 if (desc)
131 *desc = qsv_errors[i].desc;
132 return qsv_errors[i].averr;
133 }
d0a63d8b 134 }
95414eb2
AK
135 if (desc)
136 *desc = "unknown error";
137 return AVERROR_UNKNOWN;
138}
139
140int ff_qsv_print_error(void *log_ctx, mfxStatus err,
141 const char *error_string)
142{
143 const char *desc;
144 int ret;
145 ret = ff_qsv_map_error(err, &desc);
146 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
147 return ret;
d0a63d8b
AK
148}
149
8e07c22e
AK
150int ff_qsv_print_warning(void *log_ctx, mfxStatus err,
151 const char *warning_string)
152{
153 const char *desc;
154 int ret;
155 ret = ff_qsv_map_error(err, &desc);
156 av_log(log_ctx, AV_LOG_WARNING, "%s: %s (%d)\n", warning_string, desc, err);
157 return ret;
158}
159
4ab61cd9
AK
160static enum AVPixelFormat qsv_map_fourcc(uint32_t fourcc)
161{
162 switch (fourcc) {
163 case MFX_FOURCC_NV12: return AV_PIX_FMT_NV12;
164 case MFX_FOURCC_P010: return AV_PIX_FMT_P010;
165 case MFX_FOURCC_P8: return AV_PIX_FMT_PAL8;
166 }
167 return AV_PIX_FMT_NONE;
168}
169
536bb17e
AK
170int ff_qsv_map_pixfmt(enum AVPixelFormat format, uint32_t *fourcc)
171{
172 switch (format) {
173 case AV_PIX_FMT_YUV420P:
174 case AV_PIX_FMT_YUVJ420P:
21962261 175 case AV_PIX_FMT_NV12:
536bb17e
AK
176 *fourcc = MFX_FOURCC_NV12;
177 return AV_PIX_FMT_NV12;
92736c74 178 case AV_PIX_FMT_YUV420P10:
21962261 179 case AV_PIX_FMT_P010:
92736c74
AK
180 *fourcc = MFX_FOURCC_P010;
181 return AV_PIX_FMT_P010;
536bb17e
AK
182 default:
183 return AVERROR(ENOSYS);
184 }
185}
186
00aeedd8
AK
187int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
188{
189 int i;
190 for (i = 0; i < ctx->nb_mids; i++) {
191 QSVMid *mid = &ctx->mids[i];
192 if (mid->handle == frame->surface.Data.MemId)
193 return i;
194 }
195 return AVERROR_BUG;
196}
197
a0524d9b
AK
198static int qsv_load_plugins(mfxSession session, const char *load_plugins,
199 void *logctx)
200{
201 if (!load_plugins || !*load_plugins)
202 return 0;
203
204 while (*load_plugins) {
205 mfxPluginUID uid;
206 mfxStatus ret;
207 int i, err = 0;
208
209 char *plugin = av_get_token(&load_plugins, ":");
210 if (!plugin)
211 return AVERROR(ENOMEM);
212 if (strlen(plugin) != 2 * sizeof(uid.Data)) {
213 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID length\n");
214 err = AVERROR(EINVAL);
215 goto load_plugin_fail;
216 }
217
218 for (i = 0; i < sizeof(uid.Data); i++) {
219 err = sscanf(plugin + 2 * i, "%2hhx", uid.Data + i);
220 if (err != 1) {
221 av_log(logctx, AV_LOG_ERROR, "Invalid plugin UID\n");
222 err = AVERROR(EINVAL);
223 goto load_plugin_fail;
224 }
225
226 }
227
228 ret = MFXVideoUSER_Load(session, &uid, 1);
229 if (ret < 0) {
95414eb2
AK
230 char errorbuf[128];
231 snprintf(errorbuf, sizeof(errorbuf),
232 "Could not load the requested plugin '%s'", plugin);
233 err = ff_qsv_print_error(logctx, ret, errorbuf);
a0524d9b
AK
234 goto load_plugin_fail;
235 }
236
237 if (*load_plugins)
238 load_plugins++;
239load_plugin_fail:
240 av_freep(&plugin);
241 if (err < 0)
242 return err;
243 }
244
245 return 0;
246
247}
248
66acb76b
AK
249int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session,
250 const char *load_plugins)
d0a63d8b
AK
251{
252 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
253 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
254
255 const char *desc;
256 int ret;
257
258 ret = MFXInit(impl, &ver, session);
95414eb2
AK
259 if (ret < 0)
260 return ff_qsv_print_error(avctx, ret,
261 "Error initializing an internal MFX session");
d0a63d8b 262
a0524d9b
AK
263 ret = qsv_load_plugins(*session, load_plugins, avctx);
264 if (ret < 0) {
265 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
266 return ret;
66acb76b
AK
267 }
268
ce9d7da7
LB
269 MFXQueryIMPL(*session, &impl);
270
271 switch (MFX_IMPL_BASETYPE(impl)) {
272 case MFX_IMPL_SOFTWARE:
273 desc = "software";
274 break;
275 case MFX_IMPL_HARDWARE:
276 case MFX_IMPL_HARDWARE2:
277 case MFX_IMPL_HARDWARE3:
278 case MFX_IMPL_HARDWARE4:
279 desc = "hardware accelerated";
280 break;
281 default:
282 desc = "unknown";
283 }
284
d0a63d8b
AK
285 av_log(avctx, AV_LOG_VERBOSE,
286 "Initialized an internal MFX session using %s implementation\n",
287 desc);
288
289 return 0;
290}
a0524d9b 291
4ab61cd9
AK
292static void mids_buf_free(void *opaque, uint8_t *data)
293{
294 AVBufferRef *hw_frames_ref = opaque;
295 av_buffer_unref(&hw_frames_ref);
296 av_freep(&data);
297}
298
299static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
300{
301 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
302 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
303 int nb_surfaces = frames_hwctx->nb_surfaces;
304
305 AVBufferRef *mids_buf, *hw_frames_ref1;
306 QSVMid *mids;
307 int i;
308
309 hw_frames_ref1 = av_buffer_ref(hw_frames_ref);
310 if (!hw_frames_ref1)
311 return NULL;
312
313 mids = av_mallocz_array(nb_surfaces, sizeof(*mids));
314 if (!mids) {
315 av_buffer_unref(&hw_frames_ref1);
316 return NULL;
317 }
318
319 mids_buf = av_buffer_create((uint8_t*)mids, nb_surfaces * sizeof(*mids),
320 mids_buf_free, hw_frames_ref1, 0);
321 if (!mids_buf) {
322 av_buffer_unref(&hw_frames_ref1);
323 av_freep(&mids);
324 return NULL;
325 }
326
327 for (i = 0; i < nb_surfaces; i++) {
328 QSVMid *mid = &mids[i];
329 mid->handle = frames_hwctx->surfaces[i].Data.MemId;
330 mid->hw_frames_ref = hw_frames_ref1;
331 }
332
333 return mids_buf;
334}
335
336static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_ref,
337 AVBufferRef *mids_buf)
338{
339 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ref->data;
340 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
341 QSVMid *mids = (QSVMid*)mids_buf->data;
342 int nb_surfaces = frames_hwctx->nb_surfaces;
343 int i;
344
345 // the allocated size of the array is two larger than the number of
346 // surfaces, we store the references to the frames context and the
347 // QSVMid array there
348 resp->mids = av_mallocz_array(nb_surfaces + 2, sizeof(*resp->mids));
349 if (!resp->mids)
350 return AVERROR(ENOMEM);
351
352 for (i = 0; i < nb_surfaces; i++)
353 resp->mids[i] = &mids[i];
354 resp->NumFrameActual = nb_surfaces;
355
356 resp->mids[resp->NumFrameActual] = (mfxMemId)av_buffer_ref(hw_frames_ref);
357 if (!resp->mids[resp->NumFrameActual]) {
358 av_freep(&resp->mids);
359 return AVERROR(ENOMEM);
360 }
361
362 resp->mids[resp->NumFrameActual + 1] = av_buffer_ref(mids_buf);
363 if (!resp->mids[resp->NumFrameActual + 1]) {
364 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
365 av_freep(&resp->mids);
366 return AVERROR(ENOMEM);
367 }
368
369 return 0;
370}
371
a0524d9b
AK
372static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
373 mfxFrameAllocResponse *resp)
374{
375 QSVFramesContext *ctx = pthis;
4ab61cd9 376 int ret;
a0524d9b 377
4ab61cd9
AK
378 /* this should only be called from an encoder or decoder and
379 * only allocates video memory frames */
380 if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
381 MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) ||
382 !(req->Type & (MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_ENCODE)))
a0524d9b 383 return MFX_ERR_UNSUPPORTED;
a0524d9b 384
4ab61cd9
AK
385 if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
386 /* external frames -- fill from the caller-supplied frames context */
387 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
388 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
389 mfxFrameInfo *i = &req->Info;
390 mfxFrameInfo *i1 = &frames_hwctx->surfaces[0].Info;
391
6829a079 392 if (i->Width > i1->Width || i->Height > i1->Height ||
4ab61cd9
AK
393 i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
394 av_log(ctx->logctx, AV_LOG_ERROR, "Mismatching surface properties in an "
395 "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
396 i->Width, i->Height, i->FourCC, i->ChromaFormat,
397 i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
398 return MFX_ERR_UNSUPPORTED;
399 }
400
401 ret = qsv_setup_mids(resp, ctx->hw_frames_ctx, ctx->mids_buf);
402 if (ret < 0) {
403 av_log(ctx->logctx, AV_LOG_ERROR,
404 "Error filling an external frame allocation request\n");
405 return MFX_ERR_MEMORY_ALLOC;
406 }
407 } else if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME) {
408 /* internal frames -- allocate a new hw frames context */
409 AVHWFramesContext *ext_frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
410 mfxFrameInfo *i = &req->Info;
411
412 AVBufferRef *frames_ref, *mids_buf;
413 AVHWFramesContext *frames_ctx;
414 AVQSVFramesContext *frames_hwctx;
415
416 frames_ref = av_hwframe_ctx_alloc(ext_frames_ctx->device_ref);
417 if (!frames_ref)
418 return MFX_ERR_MEMORY_ALLOC;
419
420 frames_ctx = (AVHWFramesContext*)frames_ref->data;
421 frames_hwctx = frames_ctx->hwctx;
422
423 frames_ctx->format = AV_PIX_FMT_QSV;
424 frames_ctx->sw_format = qsv_map_fourcc(i->FourCC);
425 frames_ctx->width = i->Width;
426 frames_ctx->height = i->Height;
427 frames_ctx->initial_pool_size = req->NumFrameSuggested;
428
429 frames_hwctx->frame_type = req->Type;
430
431 ret = av_hwframe_ctx_init(frames_ref);
432 if (ret < 0) {
433 av_log(ctx->logctx, AV_LOG_ERROR,
434 "Error initializing a frames context for an internal frame "
435 "allocation request\n");
436 av_buffer_unref(&frames_ref);
437 return MFX_ERR_MEMORY_ALLOC;
438 }
00aeedd8 439
4ab61cd9
AK
440 mids_buf = qsv_create_mids(frames_ref);
441 if (!mids_buf) {
442 av_buffer_unref(&frames_ref);
443 return MFX_ERR_MEMORY_ALLOC;
444 }
00aeedd8 445
4ab61cd9
AK
446 ret = qsv_setup_mids(resp, frames_ref, mids_buf);
447 av_buffer_unref(&mids_buf);
448 av_buffer_unref(&frames_ref);
449 if (ret < 0) {
450 av_log(ctx->logctx, AV_LOG_ERROR,
451 "Error filling an internal frame allocation request\n");
452 return MFX_ERR_MEMORY_ALLOC;
453 }
454 } else {
455 return MFX_ERR_UNSUPPORTED;
456 }
a0524d9b
AK
457
458 return MFX_ERR_NONE;
459}
460
461static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
462{
4ab61cd9
AK
463 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
464 av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual + 1]);
00aeedd8 465 av_freep(&resp->mids);
a0524d9b
AK
466 return MFX_ERR_NONE;
467}
468
469static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
470{
4ab61cd9
AK
471 QSVMid *qsv_mid = mid;
472 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)qsv_mid->hw_frames_ref->data;
473 AVQSVFramesContext *hw_frames_hwctx = hw_frames_ctx->hwctx;
4ab61cd9 474 int ret;
4ab61cd9
AK
475
476 if (qsv_mid->locked_frame)
477 return MFX_ERR_UNDEFINED_BEHAVIOR;
478
479 /* Allocate a system memory frame that will hold the mapped data. */
480 qsv_mid->locked_frame = av_frame_alloc();
481 if (!qsv_mid->locked_frame)
482 return MFX_ERR_MEMORY_ALLOC;
483 qsv_mid->locked_frame->format = hw_frames_ctx->sw_format;
484
485 /* wrap the provided handle in a hwaccel AVFrame */
486 qsv_mid->hw_frame = av_frame_alloc();
487 if (!qsv_mid->hw_frame)
488 goto fail;
489
490 qsv_mid->hw_frame->data[3] = (uint8_t*)&qsv_mid->surf;
491 qsv_mid->hw_frame->format = AV_PIX_FMT_QSV;
492
493 // doesn't really matter what buffer is used here
494 qsv_mid->hw_frame->buf[0] = av_buffer_alloc(1);
495 if (!qsv_mid->hw_frame->buf[0])
496 goto fail;
497
498 qsv_mid->hw_frame->width = hw_frames_ctx->width;
499 qsv_mid->hw_frame->height = hw_frames_ctx->height;
500
501 qsv_mid->hw_frame->hw_frames_ctx = av_buffer_ref(qsv_mid->hw_frames_ref);
502 if (!qsv_mid->hw_frame->hw_frames_ctx)
503 goto fail;
504
505 qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
506 qsv_mid->surf.Data.MemId = qsv_mid->handle;
507
508 /* map the data to the system memory */
509 ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
510 AV_HWFRAME_MAP_DIRECT);
511 if (ret < 0)
512 goto fail;
513
514 ptr->Pitch = qsv_mid->locked_frame->linesize[0];
515 ptr->Y = qsv_mid->locked_frame->data[0];
516 ptr->U = qsv_mid->locked_frame->data[1];
517 ptr->V = qsv_mid->locked_frame->data[1] + 1;
518
519 return MFX_ERR_NONE;
520fail:
521 av_frame_free(&qsv_mid->hw_frame);
522 av_frame_free(&qsv_mid->locked_frame);
523 return MFX_ERR_MEMORY_ALLOC;
a0524d9b
AK
524}
525
526static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
527{
4ab61cd9 528 QSVMid *qsv_mid = mid;
4ab61cd9
AK
529
530 av_frame_free(&qsv_mid->locked_frame);
531 av_frame_free(&qsv_mid->hw_frame);
532
533 return MFX_ERR_NONE;
a0524d9b
AK
534}
535
536static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
537{
00aeedd8
AK
538 QSVMid *qsv_mid = (QSVMid*)mid;
539 *hdl = qsv_mid->handle;
a0524d9b
AK
540 return MFX_ERR_NONE;
541}
542
4936a48b
MT
543int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
544 AVBufferRef *device_ref, const char *load_plugins)
a0524d9b
AK
545{
546 static const mfxHandleType handle_types[] = {
547 MFX_HANDLE_VA_DISPLAY,
548 MFX_HANDLE_D3D9_DEVICE_MANAGER,
549 MFX_HANDLE_D3D11_DEVICE,
550 };
4936a48b
MT
551 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref->data;
552 AVQSVDeviceContext *device_hwctx = device_ctx->hwctx;
a0524d9b
AK
553 mfxSession parent_session = device_hwctx->session;
554
555 mfxSession session;
556 mfxVersion ver;
557 mfxIMPL impl;
558 mfxHDL handle = NULL;
559 mfxHandleType handle_type;
560 mfxStatus err;
561
562 int i, ret;
563
564 err = MFXQueryIMPL(parent_session, &impl);
565 if (err == MFX_ERR_NONE)
566 err = MFXQueryVersion(parent_session, &ver);
95414eb2
AK
567 if (err != MFX_ERR_NONE)
568 return ff_qsv_print_error(avctx, err,
569 "Error querying the session attributes");
a0524d9b
AK
570
571 for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
572 err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
573 if (err == MFX_ERR_NONE) {
574 handle_type = handle_types[i];
575 break;
576 }
577 handle = NULL;
578 }
579 if (!handle) {
580 av_log(avctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
581 "from the session\n");
582 }
583
584 err = MFXInit(impl, &ver, &session);
95414eb2
AK
585 if (err != MFX_ERR_NONE)
586 return ff_qsv_print_error(avctx, err,
587 "Error initializing a child MFX session");
a0524d9b
AK
588
589 if (handle) {
590 err = MFXVideoCORE_SetHandle(session, handle_type, handle);
95414eb2
AK
591 if (err != MFX_ERR_NONE)
592 return ff_qsv_print_error(avctx, err,
593 "Error setting a HW handle");
a0524d9b
AK
594 }
595
ea25ccd1
LB
596 err = MFXJoinSession(parent_session, session);
597 if (err != MFX_ERR_NONE)
598 return ff_qsv_print_error(avctx, err,
599 "Error joining session");
600
a0524d9b
AK
601 ret = qsv_load_plugins(session, load_plugins, avctx);
602 if (ret < 0) {
603 av_log(avctx, AV_LOG_ERROR, "Error loading plugins\n");
604 return ret;
605 }
606
4936a48b
MT
607 *psession = session;
608 return 0;
609}
610
611int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
612 QSVFramesContext *qsv_frames_ctx,
613 const char *load_plugins, int opaque)
614{
615 mfxFrameAllocator frame_allocator = {
616 .pthis = qsv_frames_ctx,
617 .Alloc = qsv_frame_alloc,
618 .Lock = qsv_frame_lock,
619 .Unlock = qsv_frame_unlock,
620 .GetHDL = qsv_frame_get_hdl,
621 .Free = qsv_frame_free,
622 };
623
624 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)qsv_frames_ctx->hw_frames_ctx->data;
625 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
626
627 mfxSession session;
628 mfxStatus err;
629
630 int ret;
631
632 ret = ff_qsv_init_session_device(avctx, &session,
633 frames_ctx->device_ref, load_plugins);
634 if (ret < 0)
635 return ret;
636
a0524d9b 637 if (!opaque) {
4ab61cd9 638 qsv_frames_ctx->logctx = avctx;
a0524d9b 639
4ab61cd9
AK
640 /* allocate the memory ids for the external frames */
641 av_buffer_unref(&qsv_frames_ctx->mids_buf);
642 qsv_frames_ctx->mids_buf = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
643 if (!qsv_frames_ctx->mids_buf)
644 return AVERROR(ENOMEM);
645 qsv_frames_ctx->mids = (QSVMid*)qsv_frames_ctx->mids_buf->data;
a0524d9b 646 qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
a0524d9b
AK
647
648 err = MFXVideoCORE_SetFrameAllocator(session, &frame_allocator);
95414eb2
AK
649 if (err != MFX_ERR_NONE)
650 return ff_qsv_print_error(avctx, err,
651 "Error setting a frame allocator");
a0524d9b
AK
652 }
653
654 *psession = session;
655 return 0;
656}