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/mem.h"
31 #include "libavutil/log.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/time.h"
38 #include "qsv_internal.h"
41 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
44 case AV_PIX_FMT_YUV420P:
45 case AV_PIX_FMT_YUVJ420P:
46 return AV_PIX_FMT_NV12;
48 return AVERROR(ENOSYS);
52 int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, AVPacket *avpkt)
54 mfxVideoParam param = { { 0 } };
55 mfxBitstream bs = { { { 0 } } };
58 q->iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
60 if (avctx->hwaccel_context) {
61 AVQSVContext *qsv = avctx->hwaccel_context;
63 q->session = qsv->session;
64 q->iopattern = qsv->iopattern;
65 q->ext_buffers = qsv->ext_buffers;
66 q->nb_ext_buffers = qsv->nb_ext_buffers;
69 ret = ff_qsv_init_internal_session(avctx, &q->internal_qs, NULL);
73 q->session = q->internal_qs.session;
78 bs.Data = avpkt->data;
79 bs.DataLength = avpkt->size;
80 bs.MaxLength = bs.DataLength;
81 bs.TimeStamp = avpkt->pts;
83 return AVERROR_INVALIDDATA;
85 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
87 av_log(avctx, AV_LOG_ERROR, "Unsupported codec_id %08x\n", avctx->codec_id);
91 param.mfx.CodecId = ret;
93 ret = MFXVideoDECODE_DecodeHeader(q->session, &bs, ¶m);
94 if (MFX_ERR_MORE_DATA==ret) {
95 return AVERROR(EAGAIN);
97 av_log(avctx, AV_LOG_ERROR, "Decode header error %d\n", ret);
98 return ff_qsv_error(ret);
100 param.IOPattern = q->iopattern;
101 param.AsyncDepth = q->async_depth;
102 param.ExtParam = q->ext_buffers;
103 param.NumExtParam = q->nb_ext_buffers;
104 param.mfx.FrameInfo.BitDepthLuma = 8;
105 param.mfx.FrameInfo.BitDepthChroma = 8;
107 ret = MFXVideoDECODE_Init(q->session, ¶m);
109 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
110 return ff_qsv_error(ret);
113 avctx->pix_fmt = AV_PIX_FMT_NV12;
114 avctx->profile = param.mfx.CodecProfile;
115 avctx->level = param.mfx.CodecLevel;
116 avctx->coded_width = param.mfx.FrameInfo.Width;
117 avctx->coded_height = param.mfx.FrameInfo.Height;
118 avctx->width = param.mfx.FrameInfo.CropW - param.mfx.FrameInfo.CropX;
119 avctx->height = param.mfx.FrameInfo.CropH - param.mfx.FrameInfo.CropY;
121 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
122 (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
124 return AVERROR(ENOMEM);
130 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
134 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
138 if (frame->frame->format == AV_PIX_FMT_QSV) {
139 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
141 frame->surface_internal.Info.BitDepthLuma = 8;
142 frame->surface_internal.Info.BitDepthChroma = 8;
143 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
144 frame->surface_internal.Info.Width = avctx->coded_width;
145 frame->surface_internal.Info.Height = avctx->coded_height;
146 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
148 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
149 frame->surface_internal.Data.Y = frame->frame->data[0];
150 frame->surface_internal.Data.UV = frame->frame->data[1];
152 frame->surface = &frame->surface_internal;
158 static void qsv_clear_unused_frames(QSVContext *q)
160 QSVFrame *cur = q->work_frames;
162 if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
164 av_frame_unref(cur->frame);
170 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
172 QSVFrame *frame, **last;
175 qsv_clear_unused_frames(q);
177 frame = q->work_frames;
178 last = &q->work_frames;
180 if (!frame->surface) {
181 ret = alloc_frame(avctx, frame);
184 *surf = frame->surface;
192 frame = av_mallocz(sizeof(*frame));
194 return AVERROR(ENOMEM);
195 frame->frame = av_frame_alloc();
198 return AVERROR(ENOMEM);
202 ret = alloc_frame(avctx, frame);
206 *surf = frame->surface;
211 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
213 QSVFrame *cur = q->work_frames;
215 if (surf == cur->surface)
222 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
223 AVFrame *frame, int *got_frame,
227 mfxFrameSurface1 *insurf;
228 mfxFrameSurface1 *outsurf;
230 mfxBitstream bs = { { { 0 } } };
234 bs.Data = avpkt->data;
235 bs.DataLength = avpkt->size;
236 bs.MaxLength = bs.DataLength;
237 bs.TimeStamp = avpkt->pts;
241 ret = get_surface(avctx, q, &insurf);
245 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
246 insurf, &outsurf, &sync);
247 if (ret == MFX_WRN_DEVICE_BUSY)
250 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
252 if (ret != MFX_ERR_NONE &&
253 ret != MFX_ERR_MORE_DATA &&
254 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
255 ret != MFX_ERR_MORE_SURFACE) {
256 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
257 return ff_qsv_error(ret);
261 QSVFrame *out_frame = find_frame(q, outsurf);
264 av_log(avctx, AV_LOG_ERROR,
265 "The returned surface does not correspond to any frame\n");
269 out_frame->queued = 1;
270 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
271 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
274 if (!av_fifo_space(q->async_fifo) ||
275 (!avpkt->size && av_fifo_size(q->async_fifo))) {
278 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
279 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
280 out_frame->queued = 0;
282 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
284 src_frame = out_frame->frame;
286 ret = av_frame_ref(frame, src_frame);
290 outsurf = out_frame->surface;
292 frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
295 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
296 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
297 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
298 frame->top_field_first =
299 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
300 frame->interlaced_frame =
301 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
306 return bs.DataOffset;
309 int ff_qsv_decode_close(QSVContext *q)
311 QSVFrame *cur = q->work_frames;
314 q->work_frames = cur->next;
315 av_frame_free(&cur->frame);
317 cur = q->work_frames;
320 av_fifo_free(q->async_fifo);
321 q->async_fifo = NULL;
323 MFXVideoDECODE_Close(q->session);
326 ff_qsv_close_internal_session(&q->internal_qs);