qsvdec: Allow use of hw_device_ctx to make the internal session
[libav.git] / libavcodec / qsvdec.c
CommitLineData
4e08c821
AK
1/*
2 * Intel MediaSDK QSV codec-independent code
3 *
4 * copyright (c) 2013 Luca Barbato
5 * copyright (c) 2015 Anton Khirnov <anton@khirnov.net>
6 *
7 * This file is part of Libav.
8 *
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <string.h>
25#include <sys/types.h>
26
27#include <mfx/mfxvideo.h>
28
29#include "libavutil/common.h"
a0524d9b
AK
30#include "libavutil/hwcontext.h"
31#include "libavutil/hwcontext_qsv.h"
4e08c821
AK
32#include "libavutil/mem.h"
33#include "libavutil/log.h"
92736c74 34#include "libavutil/pixdesc.h"
4e08c821
AK
35#include "libavutil/pixfmt.h"
36#include "libavutil/time.h"
37
38#include "avcodec.h"
39#include "internal.h"
96dca089 40#include "qsv.h"
d0a63d8b 41#include "qsv_internal.h"
b04d009b 42#include "qsvdec.h"
4e08c821 43
a0524d9b 44static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
8848ba0b 45 AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
4e08c821 46{
a0524d9b
AK
47 int ret;
48
49 if (session) {
50 q->session = session;
51 } else if (hw_frames_ref) {
52 if (q->internal_session) {
53 MFXClose(q->internal_session);
54 q->internal_session = NULL;
55 }
56 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
57
58 q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
59 if (!q->frames_ctx.hw_frames_ctx)
60 return AVERROR(ENOMEM);
61
4936a48b
MT
62 ret = ff_qsv_init_session_frames(avctx, &q->internal_session,
63 &q->frames_ctx, q->load_plugins,
64 q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY);
a0524d9b
AK
65 if (ret < 0) {
66 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
67 return ret;
68 }
69
70 q->session = q->internal_session;
8848ba0b
MT
71 } else if (hw_device_ref) {
72 if (q->internal_session) {
73 MFXClose(q->internal_session);
74 q->internal_session = NULL;
75 }
76
77 ret = ff_qsv_init_session_device(avctx, &q->internal_session,
78 hw_device_ref, q->load_plugins);
79 if (ret < 0)
80 return ret;
81
82 q->session = q->internal_session;
a0524d9b 83 } else {
4e08c821 84 if (!q->internal_session) {
a0524d9b
AK
85 ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
86 q->load_plugins);
d0a63d8b
AK
87 if (ret < 0)
88 return ret;
4e08c821
AK
89 }
90
91 q->session = q->internal_session;
4e08c821
AK
92 }
93
94 /* make sure the decoder is uninitialized */
95 MFXVideoDECODE_Close(q->session);
96
97 return 0;
98}
99
6f19bbcf 100static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
4e08c821 101{
92736c74 102 const AVPixFmtDescriptor *desc;
6f19bbcf 103 mfxSession session = NULL;
a0524d9b 104 int iopattern = 0;
76167140 105 mfxVideoParam param = { 0 };
924e2ecd
AK
106 int frame_width = avctx->coded_width;
107 int frame_height = avctx->coded_height;
4e08c821
AK
108 int ret;
109
92736c74
AK
110 desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
111 if (!desc)
112 return AVERROR_BUG;
113
22522d9c
AK
114 if (!q->async_fifo) {
115 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
3c53627a 116 (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
22522d9c
AK
117 if (!q->async_fifo)
118 return AVERROR(ENOMEM);
119 }
f5c4d38c 120
e328178d 121 if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
6f19bbcf
AK
122 AVQSVContext *user_ctx = avctx->hwaccel_context;
123 session = user_ctx->session;
a0524d9b 124 iopattern = user_ctx->iopattern;
6f19bbcf
AK
125 q->ext_buffers = user_ctx->ext_buffers;
126 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
127 }
128
a0524d9b
AK
129 if (avctx->hw_frames_ctx) {
130 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
131 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
132
133 if (!iopattern) {
134 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
135 iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
136 else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
137 iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
138 }
924e2ecd
AK
139
140 frame_width = frames_hwctx->surfaces[0].Info.Width;
141 frame_height = frames_hwctx->surfaces[0].Info.Height;
a0524d9b
AK
142 }
143
144 if (!iopattern)
145 iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
146 q->iopattern = iopattern;
147
8848ba0b 148 ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
4e08c821
AK
149 if (ret < 0) {
150 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
151 return ret;
152 }
153
d0a63d8b 154 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
4e08c821
AK
155 if (ret < 0)
156 return ret;
157
158 param.mfx.CodecId = ret;
cd1047f3
MT
159 param.mfx.CodecProfile = ff_qsv_profile_to_mfx(avctx->codec_id, avctx->profile);
160 param.mfx.CodecLevel = avctx->level == FF_LEVEL_UNKNOWN ? MFX_LEVEL_UNKNOWN : avctx->level;
4e08c821 161
92736c74
AK
162 param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
163 param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
164 param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
536bb17e 165 param.mfx.FrameInfo.FourCC = q->fourcc;
924e2ecd
AK
166 param.mfx.FrameInfo.Width = frame_width;
167 param.mfx.FrameInfo.Height = frame_height;
4e08c821
AK
168 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
169
030d84fa
MT
170 switch (avctx->field_order) {
171 case AV_FIELD_PROGRESSIVE:
172 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
173 break;
174 case AV_FIELD_TT:
175 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
176 break;
177 case AV_FIELD_BB:
178 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_BFF;
179 break;
180 default:
181 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_UNKNOWN;
182 break;
183 }
184
4e08c821
AK
185 param.IOPattern = q->iopattern;
186 param.AsyncDepth = q->async_depth;
187 param.ExtParam = q->ext_buffers;
188 param.NumExtParam = q->nb_ext_buffers;
189
190 ret = MFXVideoDECODE_Init(q->session, &param);
95414eb2
AK
191 if (ret < 0)
192 return ff_qsv_print_error(avctx, ret,
193 "Error initializing the MFX video decoder");
4e08c821 194
ce320cf1
AK
195 q->frame_info = param.mfx.FrameInfo;
196
4e08c821
AK
197 return 0;
198}
199
ce320cf1 200static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
4e08c821
AK
201{
202 int ret;
203
204 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
205 if (ret < 0)
206 return ret;
207
208 if (frame->frame->format == AV_PIX_FMT_QSV) {
404e5147 209 frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
4e08c821 210 } else {
404e5147 211 frame->surface.Info = q->frame_info;
4e08c821 212
404e5147
AK
213 frame->surface.Data.PitchLow = frame->frame->linesize[0];
214 frame->surface.Data.Y = frame->frame->data[0];
215 frame->surface.Data.UV = frame->frame->data[1];
4e08c821
AK
216 }
217
00aeedd8
AK
218 if (q->frames_ctx.mids) {
219 ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
220 if (ret < 0)
221 return ret;
222
223 frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
224 }
225
404e5147
AK
226 frame->used = 1;
227
4e08c821
AK
228 return 0;
229}
230
231static void qsv_clear_unused_frames(QSVContext *q)
232{
233 QSVFrame *cur = q->work_frames;
234 while (cur) {
404e5147
AK
235 if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
236 cur->used = 0;
4e08c821
AK
237 av_frame_unref(cur->frame);
238 }
239 cur = cur->next;
240 }
241}
242
243static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
244{
245 QSVFrame *frame, **last;
246 int ret;
247
248 qsv_clear_unused_frames(q);
249
250 frame = q->work_frames;
251 last = &q->work_frames;
252 while (frame) {
404e5147 253 if (!frame->used) {
ce320cf1 254 ret = alloc_frame(avctx, q, frame);
4e08c821
AK
255 if (ret < 0)
256 return ret;
404e5147 257 *surf = &frame->surface;
4e08c821
AK
258 return 0;
259 }
260
261 last = &frame->next;
262 frame = frame->next;
263 }
264
265 frame = av_mallocz(sizeof(*frame));
266 if (!frame)
267 return AVERROR(ENOMEM);
268 frame->frame = av_frame_alloc();
269 if (!frame->frame) {
270 av_freep(&frame);
271 return AVERROR(ENOMEM);
272 }
273 *last = frame;
274
ce320cf1 275 ret = alloc_frame(avctx, q, frame);
4e08c821
AK
276 if (ret < 0)
277 return ret;
278
404e5147 279 *surf = &frame->surface;
4e08c821
AK
280
281 return 0;
282}
283
f5c4d38c 284static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
4e08c821
AK
285{
286 QSVFrame *cur = q->work_frames;
287 while (cur) {
404e5147 288 if (surf == &cur->surface)
f5c4d38c 289 return cur;
4e08c821
AK
290 cur = cur->next;
291 }
292 return NULL;
293}
294
96dca089
AK
295static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
296 AVFrame *frame, int *got_frame,
297 AVPacket *avpkt)
4e08c821 298{
f5c4d38c 299 QSVFrame *out_frame;
4e08c821
AK
300 mfxFrameSurface1 *insurf;
301 mfxFrameSurface1 *outsurf;
3c53627a 302 mfxSyncPoint *sync;
4e08c821
AK
303 mfxBitstream bs = { { { 0 } } };
304 int ret;
305
306 if (avpkt->size) {
307 bs.Data = avpkt->data;
308 bs.DataLength = avpkt->size;
309 bs.MaxLength = bs.DataLength;
310 bs.TimeStamp = avpkt->pts;
311 }
312
3c53627a
AK
313 sync = av_mallocz(sizeof(*sync));
314 if (!sync) {
315 av_freep(&sync);
316 return AVERROR(ENOMEM);
317 }
318
4e08c821
AK
319 do {
320 ret = get_surface(avctx, q, &insurf);
d32bdadd
TG
321 if (ret < 0) {
322 av_freep(&sync);
4e08c821 323 return ret;
d32bdadd 324 }
4e08c821
AK
325
326 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
3c53627a 327 insurf, &outsurf, sync);
4e08c821
AK
328 if (ret == MFX_WRN_DEVICE_BUSY)
329 av_usleep(1);
330
331 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
332
333 if (ret != MFX_ERR_NONE &&
334 ret != MFX_ERR_MORE_DATA &&
335 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
336 ret != MFX_ERR_MORE_SURFACE) {
3c53627a 337 av_freep(&sync);
95414eb2
AK
338 return ff_qsv_print_error(avctx, ret,
339 "Error during QSV decoding.");
4e08c821
AK
340 }
341
aa9d15d8
AK
342 /* make sure we do not enter an infinite loop if the SDK
343 * did not consume any data and did not return anything */
3c53627a 344 if (!*sync && !bs.DataOffset) {
aa9d15d8 345 bs.DataOffset = avpkt->size;
0940b748
MT
346 ++q->zero_consume_run;
347 if (q->zero_consume_run > 1)
348 ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
349 } else {
350 q->zero_consume_run = 0;
aa9d15d8
AK
351 }
352
3c53627a 353 if (*sync) {
f5c4d38c 354 QSVFrame *out_frame = find_frame(q, outsurf);
4e08c821 355
f5c4d38c 356 if (!out_frame) {
4e08c821
AK
357 av_log(avctx, AV_LOG_ERROR,
358 "The returned surface does not correspond to any frame\n");
3c53627a 359 av_freep(&sync);
4e08c821
AK
360 return AVERROR_BUG;
361 }
362
f5c4d38c
AK
363 out_frame->queued = 1;
364 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
365 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
3c53627a
AK
366 } else {
367 av_freep(&sync);
f5c4d38c
AK
368 }
369
370 if (!av_fifo_space(q->async_fifo) ||
371 (!avpkt->size && av_fifo_size(q->async_fifo))) {
372 AVFrame *src_frame;
373
374 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
375 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
376 out_frame->queued = 0;
377
b68e3531
AK
378 if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
379 do {
380 ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
381 } while (ret == MFX_WRN_IN_EXECUTION);
382 }
f5c4d38c 383
3c53627a
AK
384 av_freep(&sync);
385
f5c4d38c
AK
386 src_frame = out_frame->frame;
387
4e08c821
AK
388 ret = av_frame_ref(frame, src_frame);
389 if (ret < 0)
390 return ret;
391
404e5147 392 outsurf = &out_frame->surface;
f5c4d38c 393
32c83590
AK
394#if FF_API_PKT_PTS
395FF_DISABLE_DEPRECATION_WARNINGS
396 frame->pkt_pts = outsurf->Data.TimeStamp;
397FF_ENABLE_DEPRECATION_WARNINGS
398#endif
399 frame->pts = outsurf->Data.TimeStamp;
4e08c821
AK
400
401 frame->repeat_pict =
402 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
403 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
404 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
405 frame->top_field_first =
406 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
407 frame->interlaced_frame =
408 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
409
404e5147
AK
410 /* update the surface properties */
411 if (avctx->pix_fmt == AV_PIX_FMT_QSV)
412 ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
413
4e08c821
AK
414 *got_frame = 1;
415 }
416
417 return bs.DataOffset;
418}
419
9ba27c23 420int ff_qsv_decode_close(QSVContext *q)
4e08c821
AK
421{
422 QSVFrame *cur = q->work_frames;
423
2c32eace
AK
424 if (q->session)
425 MFXVideoDECODE_Close(q->session);
426
3c53627a
AK
427 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
428 QSVFrame *out_frame;
429 mfxSyncPoint *sync;
430
431 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
432 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
433
434 av_freep(&sync);
435 }
436
4e08c821
AK
437 while (cur) {
438 q->work_frames = cur->next;
439 av_frame_free(&cur->frame);
440 av_freep(&cur);
441 cur = q->work_frames;
442 }
443
f5c4d38c
AK
444 av_fifo_free(q->async_fifo);
445 q->async_fifo = NULL;
446
96dca089
AK
447 av_parser_close(q->parser);
448 avcodec_free_context(&q->avctx_internal);
449
4e08c821
AK
450 if (q->internal_session)
451 MFXClose(q->internal_session);
452
a0524d9b 453 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
4ab61cd9 454 av_buffer_unref(&q->frames_ctx.mids_buf);
a0524d9b 455
4e08c821
AK
456 return 0;
457}
96dca089
AK
458
459int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
460 AVFrame *frame, int *got_frame, AVPacket *pkt)
461{
462 uint8_t *dummy_data;
463 int dummy_size;
464 int ret;
465
466 if (!q->avctx_internal) {
467 q->avctx_internal = avcodec_alloc_context3(NULL);
468 if (!q->avctx_internal)
469 return AVERROR(ENOMEM);
470
471 if (avctx->extradata) {
059a9348 472 q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
96dca089
AK
473 if (!q->avctx_internal->extradata)
474 return AVERROR(ENOMEM);
475
476 memcpy(q->avctx_internal->extradata, avctx->extradata,
477 avctx->extradata_size);
478 q->avctx_internal->extradata_size = avctx->extradata_size;
479 }
480
481 q->parser = av_parser_init(avctx->codec_id);
482 if (!q->parser)
483 return AVERROR(ENOMEM);
484
485 q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
486 q->orig_pix_fmt = AV_PIX_FMT_NONE;
487 }
488
489 if (!pkt->size)
490 return qsv_decode(avctx, q, frame, got_frame, pkt);
491
492 /* we assume the packets are already split properly and want
493 * just the codec parameters here */
494 av_parser_parse2(q->parser, q->avctx_internal,
495 &dummy_data, &dummy_size,
496 pkt->data, pkt->size, pkt->pts, pkt->dts,
497 pkt->pos);
498
499 /* TODO: flush delayed frames on reinit */
500 if (q->parser->format != q->orig_pix_fmt ||
501 q->parser->coded_width != avctx->coded_width ||
502 q->parser->coded_height != avctx->coded_height) {
96dca089
AK
503 enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
504 AV_PIX_FMT_NONE,
505 AV_PIX_FMT_NONE };
506 enum AVPixelFormat qsv_format;
507
536bb17e 508 qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
96dca089
AK
509 if (qsv_format < 0) {
510 av_log(avctx, AV_LOG_ERROR,
92736c74
AK
511 "Decoding pixel format '%s' is not supported\n",
512 av_get_pix_fmt_name(q->parser->format));
96dca089
AK
513 ret = AVERROR(ENOSYS);
514 goto reinit_fail;
515 }
516
517 q->orig_pix_fmt = q->parser->format;
518 avctx->pix_fmt = pix_fmts[1] = qsv_format;
519 avctx->width = q->parser->width;
520 avctx->height = q->parser->height;
521 avctx->coded_width = q->parser->coded_width;
522 avctx->coded_height = q->parser->coded_height;
030d84fa 523 avctx->field_order = q->parser->field_order;
96dca089
AK
524 avctx->level = q->avctx_internal->level;
525 avctx->profile = q->avctx_internal->profile;
526
527 ret = ff_get_format(avctx, pix_fmts);
528 if (ret < 0)
529 goto reinit_fail;
530
531 avctx->pix_fmt = ret;
532
6f19bbcf 533 ret = qsv_decode_init(avctx, q);
96dca089
AK
534 if (ret < 0)
535 goto reinit_fail;
536 }
537
538 return qsv_decode(avctx, q, frame, got_frame, pkt);
539
540reinit_fail:
541 q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
542 return ret;
543}
544
545void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
546{
547 q->orig_pix_fmt = AV_PIX_FMT_NONE;
548}