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"
37 #include "qsv_internal.h"
40 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
43 case AV_PIX_FMT_YUV420P:
44 case AV_PIX_FMT_YUVJ420P:
45 return AV_PIX_FMT_NV12;
47 return AVERROR(ENOSYS);
51 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
54 if (!q->internal_session) {
55 int ret = ff_qsv_init_internal_session(avctx, &q->internal_session, NULL);
60 q->session = q->internal_session;
65 /* make sure the decoder is uninitialized */
66 MFXVideoDECODE_Close(q->session);
71 int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
73 mfxVideoParam param = { { 0 } };
76 ret = qsv_init_session(avctx, q, session);
78 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
83 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
87 param.mfx.CodecId = ret;
88 param.mfx.CodecProfile = avctx->profile;
89 param.mfx.CodecLevel = avctx->level;
91 param.mfx.FrameInfo.BitDepthLuma = 8;
92 param.mfx.FrameInfo.BitDepthChroma = 8;
93 param.mfx.FrameInfo.Shift = 0;
94 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
95 param.mfx.FrameInfo.Width = avctx->coded_width;
96 param.mfx.FrameInfo.Height = avctx->coded_height;
97 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
99 param.IOPattern = q->iopattern;
100 param.AsyncDepth = q->async_depth;
101 param.ExtParam = q->ext_buffers;
102 param.NumExtParam = q->nb_ext_buffers;
104 ret = MFXVideoDECODE_Init(q->session, ¶m);
106 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
107 return ff_qsv_error(ret);
113 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
117 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
121 if (frame->frame->format == AV_PIX_FMT_QSV) {
122 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
124 frame->surface_internal.Info.BitDepthLuma = 8;
125 frame->surface_internal.Info.BitDepthChroma = 8;
126 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
127 frame->surface_internal.Info.Width = avctx->coded_width;
128 frame->surface_internal.Info.Height = avctx->coded_height;
129 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
131 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
132 frame->surface_internal.Data.Y = frame->frame->data[0];
133 frame->surface_internal.Data.UV = frame->frame->data[1];
135 frame->surface = &frame->surface_internal;
141 static void qsv_clear_unused_frames(QSVContext *q)
143 QSVFrame *cur = q->work_frames;
145 if (cur->surface && !cur->surface->Data.Locked) {
147 av_frame_unref(cur->frame);
153 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
155 QSVFrame *frame, **last;
158 qsv_clear_unused_frames(q);
160 frame = q->work_frames;
161 last = &q->work_frames;
163 if (!frame->surface) {
164 ret = alloc_frame(avctx, frame);
167 *surf = frame->surface;
175 frame = av_mallocz(sizeof(*frame));
177 return AVERROR(ENOMEM);
178 frame->frame = av_frame_alloc();
181 return AVERROR(ENOMEM);
185 ret = alloc_frame(avctx, frame);
189 *surf = frame->surface;
194 static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
196 QSVFrame *cur = q->work_frames;
198 if (surf == cur->surface)
205 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
206 AVFrame *frame, int *got_frame,
209 mfxFrameSurface1 *insurf;
210 mfxFrameSurface1 *outsurf;
212 mfxBitstream bs = { { { 0 } } };
216 bs.Data = avpkt->data;
217 bs.DataLength = avpkt->size;
218 bs.MaxLength = bs.DataLength;
219 bs.TimeStamp = avpkt->pts;
223 ret = get_surface(avctx, q, &insurf);
227 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
228 insurf, &outsurf, &sync);
229 if (ret == MFX_WRN_DEVICE_BUSY)
232 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
234 if (ret != MFX_ERR_NONE &&
235 ret != MFX_ERR_MORE_DATA &&
236 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
237 ret != MFX_ERR_MORE_SURFACE) {
238 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
239 return ff_qsv_error(ret);
245 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
247 src_frame = find_frame(q, outsurf);
249 av_log(avctx, AV_LOG_ERROR,
250 "The returned surface does not correspond to any frame\n");
254 ret = av_frame_ref(frame, src_frame);
258 frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
261 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
262 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
263 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
264 frame->top_field_first =
265 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
266 frame->interlaced_frame =
267 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
272 return bs.DataOffset;
275 int ff_qsv_decode_close(QSVContext *q)
277 QSVFrame *cur = q->work_frames;
280 q->work_frames = cur->next;
281 av_frame_free(&cur->frame);
283 cur = q->work_frames;
286 if (q->internal_session)
287 MFXClose(q->internal_session);