2 * Intel MediaSDK QSV codec-independent code
4 * copyright (c) 2013 Luca Barbato
5 * copyright (c) 2015 Anton Khirnov <anton@khirnov.net>
7 * This file is part of FFmpeg.
9 * FFmpeg 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.
14 * FFmpeg 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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
27 #include <mfx/mfxvideo.h>
29 #include "libavutil/common.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_qsv.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/log.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/pixfmt.h"
36 #include "libavutil/time.h"
41 #include "qsv_internal.h"
44 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
45 AVBufferRef *hw_frames_ref)
51 } else if (hw_frames_ref) {
52 if (q->internal_session) {
53 MFXClose(q->internal_session);
54 q->internal_session = NULL;
56 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
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);
62 ret = ff_qsv_init_session_hwcontext(avctx, &q->internal_session,
63 &q->frames_ctx, q->load_plugins,
64 q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY);
66 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
70 q->session = q->internal_session;
72 if (!q->internal_session) {
73 ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
79 q->session = q->internal_session;
82 /* make sure the decoder is uninitialized */
83 MFXVideoDECODE_Close(q->session);
88 static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
90 const AVPixFmtDescriptor *desc;
91 mfxSession session = NULL;
93 mfxVideoParam param = { { 0 } };
94 int frame_width = avctx->coded_width;
95 int frame_height = avctx->coded_height;
98 desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
102 if (!q->async_fifo) {
103 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
104 (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
106 return AVERROR(ENOMEM);
109 if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
110 AVQSVContext *user_ctx = avctx->hwaccel_context;
111 session = user_ctx->session;
112 iopattern = user_ctx->iopattern;
113 q->ext_buffers = user_ctx->ext_buffers;
114 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
117 if (avctx->hw_frames_ctx) {
118 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
119 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
122 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
123 iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
124 else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
125 iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
128 frame_width = frames_hwctx->surfaces[0].Info.Width;
129 frame_height = frames_hwctx->surfaces[0].Info.Height;
133 iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
134 q->iopattern = iopattern;
136 ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx);
138 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
142 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
146 param.mfx.CodecId = ret;
147 param.mfx.CodecProfile = ff_qsv_profile_to_mfx(avctx->codec_id, avctx->profile);
148 param.mfx.CodecLevel = avctx->level == FF_LEVEL_UNKNOWN ? MFX_LEVEL_UNKNOWN : avctx->level;
150 param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
151 param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
152 param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
153 param.mfx.FrameInfo.FourCC = q->fourcc;
154 param.mfx.FrameInfo.Width = frame_width;
155 param.mfx.FrameInfo.Height = frame_height;
156 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
158 switch (avctx->field_order) {
159 case AV_FIELD_PROGRESSIVE:
160 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
163 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
166 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_BFF;
169 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_UNKNOWN;
173 param.IOPattern = q->iopattern;
174 param.AsyncDepth = q->async_depth;
175 param.ExtParam = q->ext_buffers;
176 param.NumExtParam = q->nb_ext_buffers;
178 ret = MFXVideoDECODE_Init(q->session, ¶m);
180 return ff_qsv_print_error(avctx, ret,
181 "Error initializing the MFX video decoder");
183 q->frame_info = param.mfx.FrameInfo;
188 static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
192 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
196 if (frame->frame->format == AV_PIX_FMT_QSV) {
197 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
199 frame->surface_internal.Info = q->frame_info;
201 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
202 frame->surface_internal.Data.Y = frame->frame->data[0];
203 frame->surface_internal.Data.UV = frame->frame->data[1];
205 frame->surface = &frame->surface_internal;
211 static void qsv_clear_unused_frames(QSVContext *q)
213 QSVFrame *cur = q->work_frames;
215 if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
217 av_frame_unref(cur->frame);
223 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
225 QSVFrame *frame, **last;
228 qsv_clear_unused_frames(q);
230 frame = q->work_frames;
231 last = &q->work_frames;
233 if (!frame->surface) {
234 ret = alloc_frame(avctx, q, frame);
237 *surf = frame->surface;
245 frame = av_mallocz(sizeof(*frame));
247 return AVERROR(ENOMEM);
248 frame->frame = av_frame_alloc();
251 return AVERROR(ENOMEM);
255 ret = alloc_frame(avctx, q, frame);
259 *surf = frame->surface;
264 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
266 QSVFrame *cur = q->work_frames;
268 if (surf == cur->surface)
275 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
276 AVFrame *frame, int *got_frame,
280 mfxFrameSurface1 *insurf;
281 mfxFrameSurface1 *outsurf;
283 mfxBitstream bs = { { { 0 } } };
287 bs.Data = avpkt->data;
288 bs.DataLength = avpkt->size;
289 bs.MaxLength = bs.DataLength;
290 bs.TimeStamp = avpkt->pts;
293 sync = av_mallocz(sizeof(*sync));
296 return AVERROR(ENOMEM);
300 ret = get_surface(avctx, q, &insurf);
306 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
307 insurf, &outsurf, sync);
308 if (ret == MFX_WRN_DEVICE_BUSY)
311 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
313 if (ret != MFX_ERR_NONE &&
314 ret != MFX_ERR_MORE_DATA &&
315 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
316 ret != MFX_ERR_MORE_SURFACE) {
318 return ff_qsv_print_error(avctx, ret,
319 "Error during QSV decoding.");
322 /* make sure we do not enter an infinite loop if the SDK
323 * did not consume any data and did not return anything */
324 if (!*sync && !bs.DataOffset) {
325 av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n");
326 bs.DataOffset = avpkt->size;
330 QSVFrame *out_frame = find_frame(q, outsurf);
333 av_log(avctx, AV_LOG_ERROR,
334 "The returned surface does not correspond to any frame\n");
339 out_frame->queued = 1;
340 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
341 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
346 if (!av_fifo_space(q->async_fifo) ||
347 (!avpkt->size && av_fifo_size(q->async_fifo))) {
350 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
351 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
352 out_frame->queued = 0;
355 ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
356 } while (ret == MFX_WRN_IN_EXECUTION);
360 src_frame = out_frame->frame;
362 ret = av_frame_ref(frame, src_frame);
366 outsurf = out_frame->surface;
369 FF_DISABLE_DEPRECATION_WARNINGS
370 frame->pkt_pts = outsurf->Data.TimeStamp;
371 FF_ENABLE_DEPRECATION_WARNINGS
373 frame->pts = outsurf->Data.TimeStamp;
376 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
377 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
378 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
379 frame->top_field_first =
380 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
381 frame->interlaced_frame =
382 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
387 return bs.DataOffset;
390 int ff_qsv_decode_close(QSVContext *q)
392 QSVFrame *cur = q->work_frames;
395 MFXVideoDECODE_Close(q->session);
397 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
401 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
402 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
408 q->work_frames = cur->next;
409 av_frame_free(&cur->frame);
411 cur = q->work_frames;
414 av_fifo_free(q->async_fifo);
415 q->async_fifo = NULL;
417 av_parser_close(q->parser);
418 avcodec_free_context(&q->avctx_internal);
420 if (q->internal_session)
421 MFXClose(q->internal_session);
423 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
424 av_freep(&q->frames_ctx.mids);
425 q->frames_ctx.nb_mids = 0;
430 int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
431 AVFrame *frame, int *got_frame, AVPacket *pkt)
437 if (!q->avctx_internal) {
438 q->avctx_internal = avcodec_alloc_context3(NULL);
439 if (!q->avctx_internal)
440 return AVERROR(ENOMEM);
442 q->parser = av_parser_init(avctx->codec_id);
444 return AVERROR(ENOMEM);
446 q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
447 q->orig_pix_fmt = AV_PIX_FMT_NONE;
451 return qsv_decode(avctx, q, frame, got_frame, pkt);
453 /* we assume the packets are already split properly and want
454 * just the codec parameters here */
455 av_parser_parse2(q->parser, q->avctx_internal,
456 &dummy_data, &dummy_size,
457 pkt->data, pkt->size, pkt->pts, pkt->dts,
460 /* TODO: flush delayed frames on reinit */
461 if (q->parser->format != q->orig_pix_fmt ||
462 q->parser->coded_width != avctx->coded_width ||
463 q->parser->coded_height != avctx->coded_height) {
464 enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
467 enum AVPixelFormat qsv_format;
469 qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
470 if (qsv_format < 0) {
471 av_log(avctx, AV_LOG_ERROR,
472 "Decoding pixel format '%s' is not supported\n",
473 av_get_pix_fmt_name(q->parser->format));
474 ret = AVERROR(ENOSYS);
478 q->orig_pix_fmt = q->parser->format;
479 avctx->pix_fmt = pix_fmts[1] = qsv_format;
480 avctx->width = q->parser->width;
481 avctx->height = q->parser->height;
482 avctx->coded_width = q->parser->coded_width;
483 avctx->coded_height = q->parser->coded_height;
484 avctx->field_order = q->parser->field_order;
485 avctx->level = q->avctx_internal->level;
486 avctx->profile = q->avctx_internal->profile;
488 ret = ff_get_format(avctx, pix_fmts);
492 avctx->pix_fmt = ret;
494 ret = qsv_decode_init(avctx, q);
499 return qsv_decode(avctx, q, frame, got_frame, pkt);
502 q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
506 void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
508 q->orig_pix_fmt = AV_PIX_FMT_NONE;