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 const AVCodecHWConfigInternal *ff_qsv_hw_configs[] = {
45 &(const AVCodecHWConfigInternal) {
47 .pix_fmt = AV_PIX_FMT_QSV,
48 .methods = AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX |
49 AV_CODEC_HW_CONFIG_METHOD_AD_HOC,
50 .device_type = AV_HWDEVICE_TYPE_QSV,
57 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
58 AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
64 } else if (hw_frames_ref) {
65 if (q->internal_session) {
66 MFXClose(q->internal_session);
67 q->internal_session = NULL;
69 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
71 q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
72 if (!q->frames_ctx.hw_frames_ctx)
73 return AVERROR(ENOMEM);
75 ret = ff_qsv_init_session_frames(avctx, &q->internal_session,
76 &q->frames_ctx, q->load_plugins,
77 q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY);
79 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
83 q->session = q->internal_session;
84 } else if (hw_device_ref) {
85 if (q->internal_session) {
86 MFXClose(q->internal_session);
87 q->internal_session = NULL;
90 ret = ff_qsv_init_session_device(avctx, &q->internal_session,
91 hw_device_ref, q->load_plugins);
95 q->session = q->internal_session;
97 if (!q->internal_session) {
98 ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
104 q->session = q->internal_session;
107 /* make sure the decoder is uninitialized */
108 MFXVideoDECODE_Close(q->session);
113 static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
115 const AVPixFmtDescriptor *desc;
116 mfxSession session = NULL;
118 mfxVideoParam param = { 0 };
119 int frame_width = avctx->coded_width;
120 int frame_height = avctx->coded_height;
123 desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
127 if (!q->async_fifo) {
128 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
129 (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
131 return AVERROR(ENOMEM);
134 if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
135 AVQSVContext *user_ctx = avctx->hwaccel_context;
136 session = user_ctx->session;
137 iopattern = user_ctx->iopattern;
138 q->ext_buffers = user_ctx->ext_buffers;
139 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
142 if (avctx->hw_frames_ctx) {
143 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
144 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
147 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
148 iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
149 else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
150 iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
155 iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
156 q->iopattern = iopattern;
158 ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
160 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
164 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
168 param.mfx.CodecId = ret;
169 param.mfx.CodecProfile = ff_qsv_profile_to_mfx(avctx->codec_id, avctx->profile);
170 param.mfx.CodecLevel = avctx->level == FF_LEVEL_UNKNOWN ? MFX_LEVEL_UNKNOWN : avctx->level;
172 param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
173 param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
174 param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
175 param.mfx.FrameInfo.FourCC = q->fourcc;
176 param.mfx.FrameInfo.Width = frame_width;
177 param.mfx.FrameInfo.Height = frame_height;
178 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
180 switch (avctx->field_order) {
181 case AV_FIELD_PROGRESSIVE:
182 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
185 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
188 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_BFF;
191 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_UNKNOWN;
195 param.IOPattern = q->iopattern;
196 param.AsyncDepth = q->async_depth;
197 param.ExtParam = q->ext_buffers;
198 param.NumExtParam = q->nb_ext_buffers;
200 ret = MFXVideoDECODE_Init(q->session, ¶m);
202 return ff_qsv_print_error(avctx, ret,
203 "Error initializing the MFX video decoder");
205 q->frame_info = param.mfx.FrameInfo;
210 static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
214 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
218 if (frame->frame->format == AV_PIX_FMT_QSV) {
219 frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
221 frame->surface.Info = q->frame_info;
223 frame->surface.Data.PitchLow = frame->frame->linesize[0];
224 frame->surface.Data.Y = frame->frame->data[0];
225 frame->surface.Data.UV = frame->frame->data[1];
228 if (q->frames_ctx.mids) {
229 ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
233 frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
235 frame->surface.Data.ExtParam = &frame->ext_param;
236 frame->surface.Data.NumExtParam = 1;
237 frame->ext_param = (mfxExtBuffer*)&frame->dec_info;
238 frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO;
239 frame->dec_info.Header.BufferSz = sizeof(frame->dec_info);
246 static void qsv_clear_unused_frames(QSVContext *q)
248 QSVFrame *cur = q->work_frames;
250 if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
252 av_frame_unref(cur->frame);
258 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
260 QSVFrame *frame, **last;
263 qsv_clear_unused_frames(q);
265 frame = q->work_frames;
266 last = &q->work_frames;
269 ret = alloc_frame(avctx, q, frame);
272 *surf = &frame->surface;
280 frame = av_mallocz(sizeof(*frame));
282 return AVERROR(ENOMEM);
283 frame->frame = av_frame_alloc();
286 return AVERROR(ENOMEM);
290 ret = alloc_frame(avctx, q, frame);
294 *surf = &frame->surface;
299 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
301 QSVFrame *cur = q->work_frames;
303 if (surf == &cur->surface)
310 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
311 AVFrame *frame, int *got_frame,
315 mfxFrameSurface1 *insurf;
316 mfxFrameSurface1 *outsurf;
318 mfxBitstream bs = { { { 0 } } };
322 bs.Data = avpkt->data;
323 bs.DataLength = avpkt->size;
324 bs.MaxLength = bs.DataLength;
325 bs.TimeStamp = avpkt->pts;
326 if (avctx->field_order == AV_FIELD_PROGRESSIVE)
327 bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
330 sync = av_mallocz(sizeof(*sync));
333 return AVERROR(ENOMEM);
337 ret = get_surface(avctx, q, &insurf);
343 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
344 insurf, &outsurf, sync);
345 if (ret == MFX_WRN_DEVICE_BUSY)
348 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
350 if (ret != MFX_ERR_NONE &&
351 ret != MFX_ERR_MORE_DATA &&
352 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
353 ret != MFX_ERR_MORE_SURFACE) {
355 return ff_qsv_print_error(avctx, ret,
356 "Error during QSV decoding.");
359 /* make sure we do not enter an infinite loop if the SDK
360 * did not consume any data and did not return anything */
361 if (!*sync && !bs.DataOffset) {
362 bs.DataOffset = avpkt->size;
363 ++q->zero_consume_run;
364 if (q->zero_consume_run > 1)
365 ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
367 q->zero_consume_run = 0;
371 QSVFrame *out_frame = find_frame(q, outsurf);
374 av_log(avctx, AV_LOG_ERROR,
375 "The returned surface does not correspond to any frame\n");
380 out_frame->queued = 1;
381 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
382 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
387 if (!av_fifo_space(q->async_fifo) ||
388 (!avpkt->size && av_fifo_size(q->async_fifo))) {
391 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
392 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
393 out_frame->queued = 0;
395 if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
397 ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
398 } while (ret == MFX_WRN_IN_EXECUTION);
403 src_frame = out_frame->frame;
405 ret = av_frame_ref(frame, src_frame);
409 outsurf = &out_frame->surface;
412 FF_DISABLE_DEPRECATION_WARNINGS
413 frame->pkt_pts = outsurf->Data.TimeStamp;
414 FF_ENABLE_DEPRECATION_WARNINGS
416 frame->pts = outsurf->Data.TimeStamp;
419 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
420 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
421 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
422 frame->top_field_first =
423 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
424 frame->interlaced_frame =
425 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
426 frame->pict_type = ff_qsv_map_pictype(out_frame->dec_info.FrameType);
427 //Key frame is IDR frame is only suitable for H264. For HEVC, IRAPs are key frames.
428 if (avctx->codec_id == AV_CODEC_ID_H264)
429 frame->key_frame = !!(out_frame->dec_info.FrameType & MFX_FRAMETYPE_IDR);
431 /* update the surface properties */
432 if (avctx->pix_fmt == AV_PIX_FMT_QSV)
433 ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
438 return bs.DataOffset;
441 int ff_qsv_decode_close(QSVContext *q)
443 QSVFrame *cur = q->work_frames;
446 MFXVideoDECODE_Close(q->session);
448 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
452 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
453 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
459 q->work_frames = cur->next;
460 av_frame_free(&cur->frame);
462 cur = q->work_frames;
465 av_fifo_free(q->async_fifo);
466 q->async_fifo = NULL;
468 av_parser_close(q->parser);
469 avcodec_free_context(&q->avctx_internal);
471 if (q->internal_session)
472 MFXClose(q->internal_session);
474 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
475 av_buffer_unref(&q->frames_ctx.mids_buf);
480 int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
481 AVFrame *frame, int *got_frame, AVPacket *pkt)
486 const AVPixFmtDescriptor *desc;
488 if (!q->avctx_internal) {
489 q->avctx_internal = avcodec_alloc_context3(NULL);
490 if (!q->avctx_internal)
491 return AVERROR(ENOMEM);
493 q->parser = av_parser_init(avctx->codec_id);
495 return AVERROR(ENOMEM);
497 q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
498 q->orig_pix_fmt = AV_PIX_FMT_NONE;
502 return qsv_decode(avctx, q, frame, got_frame, pkt);
504 /* we assume the packets are already split properly and want
505 * just the codec parameters here */
506 av_parser_parse2(q->parser, q->avctx_internal,
507 &dummy_data, &dummy_size,
508 pkt->data, pkt->size, pkt->pts, pkt->dts,
511 avctx->field_order = q->parser->field_order;
512 /* TODO: flush delayed frames on reinit */
513 if (q->parser->format != q->orig_pix_fmt ||
514 FFALIGN(q->parser->coded_width, 16) != FFALIGN(avctx->coded_width, 16) ||
515 FFALIGN(q->parser->coded_height, 16) != FFALIGN(avctx->coded_height, 16)) {
516 enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
519 enum AVPixelFormat qsv_format;
521 qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
522 if (qsv_format < 0) {
523 av_log(avctx, AV_LOG_ERROR,
524 "Decoding pixel format '%s' is not supported\n",
525 av_get_pix_fmt_name(q->parser->format));
526 ret = AVERROR(ENOSYS);
530 q->orig_pix_fmt = q->parser->format;
531 avctx->pix_fmt = pix_fmts[1] = qsv_format;
532 avctx->width = q->parser->width;
533 avctx->height = q->parser->height;
534 avctx->coded_width = FFALIGN(q->parser->coded_width, 16);
535 avctx->coded_height = FFALIGN(q->parser->coded_height, 16);
536 avctx->level = q->avctx_internal->level;
537 avctx->profile = q->avctx_internal->profile;
539 ret = ff_get_format(avctx, pix_fmts);
543 avctx->pix_fmt = ret;
545 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
549 if (desc->comp[0].depth > 8) {
550 avctx->coded_width = FFALIGN(q->parser->coded_width, 32);
551 avctx->coded_height = FFALIGN(q->parser->coded_height, 32);
554 ret = qsv_decode_init(avctx, q);
559 return qsv_decode(avctx, q, frame, got_frame, pkt);
562 q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
566 void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
568 q->orig_pix_fmt = AV_PIX_FMT_NONE;