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)
54 mfxVideoParam param = { { 0 } };
57 q->iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
59 if (avctx->hwaccel_context) {
60 AVQSVContext *qsv = avctx->hwaccel_context;
62 q->session = qsv->session;
63 q->iopattern = qsv->iopattern;
64 q->ext_buffers = qsv->ext_buffers;
65 q->nb_ext_buffers = qsv->nb_ext_buffers;
68 ret = ff_qsv_init_internal_session(avctx, &q->internal_qs, NULL);
72 q->session = q->internal_qs.session;
75 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
79 param.mfx.CodecId = ret;
80 param.mfx.CodecProfile = avctx->profile;
81 param.mfx.CodecLevel = avctx->level;
83 param.mfx.FrameInfo.BitDepthLuma = 8;
84 param.mfx.FrameInfo.BitDepthChroma = 8;
85 param.mfx.FrameInfo.Shift = 0;
86 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
87 param.mfx.FrameInfo.Width = avctx->coded_width;
88 param.mfx.FrameInfo.Height = avctx->coded_height;
89 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
91 param.IOPattern = q->iopattern;
92 param.AsyncDepth = q->async_depth;
93 param.ExtParam = q->ext_buffers;
94 param.NumExtParam = q->nb_ext_buffers;
96 ret = MFXVideoDECODE_Init(q->session, ¶m);
98 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
99 return ff_qsv_error(ret);
105 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
109 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
113 if (frame->frame->format == AV_PIX_FMT_QSV) {
114 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
116 frame->surface_internal.Info.BitDepthLuma = 8;
117 frame->surface_internal.Info.BitDepthChroma = 8;
118 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
119 frame->surface_internal.Info.Width = avctx->coded_width;
120 frame->surface_internal.Info.Height = avctx->coded_height;
121 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
123 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
124 frame->surface_internal.Data.Y = frame->frame->data[0];
125 frame->surface_internal.Data.UV = frame->frame->data[1];
127 frame->surface = &frame->surface_internal;
133 static void qsv_clear_unused_frames(QSVContext *q)
135 QSVFrame *cur = q->work_frames;
137 if (cur->surface && !cur->surface->Data.Locked) {
139 av_frame_unref(cur->frame);
145 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
147 QSVFrame *frame, **last;
150 qsv_clear_unused_frames(q);
152 frame = q->work_frames;
153 last = &q->work_frames;
155 if (!frame->surface) {
156 ret = alloc_frame(avctx, frame);
159 *surf = frame->surface;
167 frame = av_mallocz(sizeof(*frame));
169 return AVERROR(ENOMEM);
170 frame->frame = av_frame_alloc();
173 return AVERROR(ENOMEM);
177 ret = alloc_frame(avctx, frame);
181 *surf = frame->surface;
186 static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
188 QSVFrame *cur = q->work_frames;
190 if (surf == cur->surface)
197 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
198 AVFrame *frame, int *got_frame,
201 mfxFrameSurface1 *insurf;
202 mfxFrameSurface1 *outsurf;
204 mfxBitstream bs = { { { 0 } } };
208 bs.Data = avpkt->data;
209 bs.DataLength = avpkt->size;
210 bs.MaxLength = bs.DataLength;
211 bs.TimeStamp = avpkt->pts;
215 ret = get_surface(avctx, q, &insurf);
219 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
220 insurf, &outsurf, &sync);
221 if (ret == MFX_WRN_DEVICE_BUSY)
224 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
226 if (ret != MFX_ERR_NONE &&
227 ret != MFX_ERR_MORE_DATA &&
228 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
229 ret != MFX_ERR_MORE_SURFACE) {
230 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
231 return ff_qsv_error(ret);
237 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
239 src_frame = find_frame(q, outsurf);
241 av_log(avctx, AV_LOG_ERROR,
242 "The returned surface does not correspond to any frame\n");
246 ret = av_frame_ref(frame, src_frame);
250 frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
253 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
254 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
255 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
256 frame->top_field_first =
257 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
258 frame->interlaced_frame =
259 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
264 return bs.DataOffset;
267 int ff_qsv_decode_close(QSVContext *q)
269 QSVFrame *cur = q->work_frames;
272 q->work_frames = cur->next;
273 av_frame_free(&cur->frame);
275 cur = q->work_frames;
277 ff_qsv_close_internal_session(&q->internal_qs);