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/pixfmt.h"
35 #include "libavutil/time.h"
40 #include "qsv_internal.h"
43 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
46 case AV_PIX_FMT_YUV420P:
47 case AV_PIX_FMT_YUVJ420P:
48 return AV_PIX_FMT_NV12;
50 return AVERROR(ENOSYS);
54 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
55 AVBufferRef *hw_frames_ref)
61 } else if (hw_frames_ref) {
62 if (q->internal_session) {
63 MFXClose(q->internal_session);
64 q->internal_session = NULL;
66 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
68 q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
69 if (!q->frames_ctx.hw_frames_ctx)
70 return AVERROR(ENOMEM);
72 ret = ff_qsv_init_session_hwcontext(avctx, &q->internal_session,
73 &q->frames_ctx, q->load_plugins,
74 q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY);
76 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
80 q->session = q->internal_session;
82 if (!q->internal_session) {
83 ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
89 q->session = q->internal_session;
92 /* make sure the decoder is uninitialized */
93 MFXVideoDECODE_Close(q->session);
98 static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
100 mfxSession session = NULL;
102 mfxVideoParam param = { { 0 } };
105 if (!q->async_fifo) {
106 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
107 (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
109 return AVERROR(ENOMEM);
112 if (avctx->hwaccel_context) {
113 AVQSVContext *user_ctx = avctx->hwaccel_context;
114 session = user_ctx->session;
115 iopattern = user_ctx->iopattern;
116 q->ext_buffers = user_ctx->ext_buffers;
117 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
120 if (avctx->hw_frames_ctx) {
121 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
122 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
125 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
126 iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
127 else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
128 iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
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 = 8;
151 param.mfx.FrameInfo.BitDepthChroma = 8;
152 param.mfx.FrameInfo.Shift = 0;
153 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
154 param.mfx.FrameInfo.Width = avctx->coded_width;
155 param.mfx.FrameInfo.Height = avctx->coded_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 if (MFX_ERR_INVALID_VIDEO_PARAM==ret) {
181 av_log(avctx, AV_LOG_ERROR,
182 "Error initializing the MFX video decoder, unsupported video\n");
184 av_log(avctx, AV_LOG_ERROR,
185 "Error initializing the MFX video decoder %d\n", ret);
187 return ff_qsv_error(ret);
193 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
197 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
201 if (frame->frame->format == AV_PIX_FMT_QSV) {
202 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
204 frame->surface_internal.Info.BitDepthLuma = 8;
205 frame->surface_internal.Info.BitDepthChroma = 8;
206 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
207 frame->surface_internal.Info.Width = avctx->coded_width;
208 frame->surface_internal.Info.Height = avctx->coded_height;
209 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
211 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
212 frame->surface_internal.Data.Y = frame->frame->data[0];
213 frame->surface_internal.Data.UV = frame->frame->data[1];
215 frame->surface = &frame->surface_internal;
221 static void qsv_clear_unused_frames(QSVContext *q)
223 QSVFrame *cur = q->work_frames;
225 if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
227 av_frame_unref(cur->frame);
233 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
235 QSVFrame *frame, **last;
238 qsv_clear_unused_frames(q);
240 frame = q->work_frames;
241 last = &q->work_frames;
243 if (!frame->surface) {
244 ret = alloc_frame(avctx, frame);
247 *surf = frame->surface;
255 frame = av_mallocz(sizeof(*frame));
257 return AVERROR(ENOMEM);
258 frame->frame = av_frame_alloc();
261 return AVERROR(ENOMEM);
265 ret = alloc_frame(avctx, frame);
269 *surf = frame->surface;
274 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
276 QSVFrame *cur = q->work_frames;
278 if (surf == cur->surface)
285 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
286 AVFrame *frame, int *got_frame,
290 mfxFrameSurface1 *insurf;
291 mfxFrameSurface1 *outsurf;
293 mfxBitstream bs = { { { 0 } } };
297 bs.Data = avpkt->data;
298 bs.DataLength = avpkt->size;
299 bs.MaxLength = bs.DataLength;
300 bs.TimeStamp = avpkt->pts;
303 sync = av_mallocz(sizeof(*sync));
306 return AVERROR(ENOMEM);
310 ret = get_surface(avctx, q, &insurf);
314 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
315 insurf, &outsurf, sync);
316 if (ret == MFX_WRN_DEVICE_BUSY)
319 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
321 if (ret != MFX_ERR_NONE &&
322 ret != MFX_ERR_MORE_DATA &&
323 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
324 ret != MFX_ERR_MORE_SURFACE) {
325 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
327 return ff_qsv_error(ret);
330 /* make sure we do not enter an infinite loop if the SDK
331 * did not consume any data and did not return anything */
332 if (!*sync && !bs.DataOffset) {
333 av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n");
334 bs.DataOffset = avpkt->size;
338 QSVFrame *out_frame = find_frame(q, outsurf);
341 av_log(avctx, AV_LOG_ERROR,
342 "The returned surface does not correspond to any frame\n");
347 out_frame->queued = 1;
348 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
349 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
354 if (!av_fifo_space(q->async_fifo) ||
355 (!avpkt->size && av_fifo_size(q->async_fifo))) {
358 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
359 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
360 out_frame->queued = 0;
363 ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
364 } while (ret == MFX_WRN_IN_EXECUTION);
368 src_frame = out_frame->frame;
370 ret = av_frame_ref(frame, src_frame);
374 outsurf = out_frame->surface;
377 FF_DISABLE_DEPRECATION_WARNINGS
378 frame->pkt_pts = outsurf->Data.TimeStamp;
379 FF_ENABLE_DEPRECATION_WARNINGS
381 frame->pts = outsurf->Data.TimeStamp;
384 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
385 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
386 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
387 frame->top_field_first =
388 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
389 frame->interlaced_frame =
390 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
395 return bs.DataOffset;
398 int ff_qsv_decode_close(QSVContext *q)
400 QSVFrame *cur = q->work_frames;
403 MFXVideoDECODE_Close(q->session);
405 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
409 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
410 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
416 q->work_frames = cur->next;
417 av_frame_free(&cur->frame);
419 cur = q->work_frames;
422 av_fifo_free(q->async_fifo);
423 q->async_fifo = NULL;
425 av_parser_close(q->parser);
426 avcodec_free_context(&q->avctx_internal);
428 if (q->internal_session)
429 MFXClose(q->internal_session);
431 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
432 av_freep(&q->frames_ctx.mids);
433 q->frames_ctx.nb_mids = 0;
438 int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
439 AVFrame *frame, int *got_frame, AVPacket *pkt)
445 if (!q->avctx_internal) {
446 q->avctx_internal = avcodec_alloc_context3(NULL);
447 if (!q->avctx_internal)
448 return AVERROR(ENOMEM);
450 q->parser = av_parser_init(avctx->codec_id);
452 return AVERROR(ENOMEM);
454 q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
455 q->orig_pix_fmt = AV_PIX_FMT_NONE;
459 return qsv_decode(avctx, q, frame, got_frame, pkt);
461 /* we assume the packets are already split properly and want
462 * just the codec parameters here */
463 av_parser_parse2(q->parser, q->avctx_internal,
464 &dummy_data, &dummy_size,
465 pkt->data, pkt->size, pkt->pts, pkt->dts,
468 /* TODO: flush delayed frames on reinit */
469 if (q->parser->format != q->orig_pix_fmt ||
470 q->parser->coded_width != avctx->coded_width ||
471 q->parser->coded_height != avctx->coded_height) {
472 enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
475 enum AVPixelFormat qsv_format;
477 qsv_format = ff_qsv_map_pixfmt(q->parser->format);
478 if (qsv_format < 0) {
479 av_log(avctx, AV_LOG_ERROR,
480 "Only 8-bit YUV420 streams are supported.\n");
481 ret = AVERROR(ENOSYS);
485 q->orig_pix_fmt = q->parser->format;
486 avctx->pix_fmt = pix_fmts[1] = qsv_format;
487 avctx->width = q->parser->width;
488 avctx->height = q->parser->height;
489 avctx->coded_width = q->parser->coded_width;
490 avctx->coded_height = q->parser->coded_height;
491 avctx->field_order = q->parser->field_order;
492 avctx->level = q->avctx_internal->level;
493 avctx->profile = q->avctx_internal->profile;
495 ret = ff_get_format(avctx, pix_fmts);
499 avctx->pix_fmt = ret;
501 ret = qsv_decode_init(avctx, q);
506 return qsv_decode(avctx, q, frame, got_frame, pkt);
509 q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
513 void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
515 q->orig_pix_fmt = AV_PIX_FMT_NONE;