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"
39 int ff_qsv_error(int mfx_err)
44 case MFX_ERR_MEMORY_ALLOC:
45 case MFX_ERR_NOT_ENOUGH_BUFFER:
46 return AVERROR(ENOMEM);
47 case MFX_ERR_INVALID_HANDLE:
48 return AVERROR(EINVAL);
49 case MFX_ERR_DEVICE_FAILED:
50 case MFX_ERR_DEVICE_LOST:
51 case MFX_ERR_LOCK_MEMORY:
53 case MFX_ERR_NULL_PTR:
54 case MFX_ERR_UNDEFINED_BEHAVIOR:
55 case MFX_ERR_NOT_INITIALIZED:
57 case MFX_ERR_UNSUPPORTED:
58 case MFX_ERR_NOT_FOUND:
59 return AVERROR(ENOSYS);
60 case MFX_ERR_MORE_DATA:
61 case MFX_ERR_MORE_SURFACE:
62 case MFX_ERR_MORE_BITSTREAM:
63 return AVERROR(EAGAIN);
64 case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
65 case MFX_ERR_INVALID_VIDEO_PARAM:
66 return AVERROR(EINVAL);
70 return AVERROR_UNKNOWN;
74 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
77 case AV_PIX_FMT_YUV420P:
78 case AV_PIX_FMT_YUVJ420P:
79 return AV_PIX_FMT_NV12;
81 return AVERROR(ENOSYS);
85 static int codec_id_to_mfx(enum AVCodecID codec_id)
88 case AV_CODEC_ID_H264:
90 case AV_CODEC_ID_MPEG1VIDEO:
91 case AV_CODEC_ID_MPEG2VIDEO:
92 return MFX_CODEC_MPEG2;
99 return AVERROR(ENOSYS);
102 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
105 if (!q->internal_session) {
106 mfxIMPL impl = MFX_IMPL_AUTO_ANY;
107 mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
112 ret = MFXInit(impl, &ver, &q->internal_session);
114 av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
115 return ff_qsv_error(ret);
118 MFXQueryIMPL(q->internal_session, &impl);
120 if (impl & MFX_IMPL_SOFTWARE)
122 else if (impl & MFX_IMPL_HARDWARE)
123 desc = "hardware accelerated";
127 av_log(avctx, AV_LOG_VERBOSE,
128 "Initialized an internal MFX session using %s implementation\n",
132 q->session = q->internal_session;
134 q->session = session;
137 /* make sure the decoder is uninitialized */
138 MFXVideoDECODE_Close(q->session);
143 int ff_qsv_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
145 mfxVideoParam param = { { 0 } };
148 ret = qsv_init_session(avctx, q, session);
150 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
155 ret = codec_id_to_mfx(avctx->codec_id);
159 param.mfx.CodecId = ret;
160 param.mfx.CodecProfile = avctx->profile;
161 param.mfx.CodecLevel = avctx->level;
163 param.mfx.FrameInfo.BitDepthLuma = 8;
164 param.mfx.FrameInfo.BitDepthChroma = 8;
165 param.mfx.FrameInfo.Shift = 0;
166 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
167 param.mfx.FrameInfo.Width = avctx->coded_width;
168 param.mfx.FrameInfo.Height = avctx->coded_height;
169 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
171 param.IOPattern = q->iopattern;
172 param.AsyncDepth = q->async_depth;
173 param.ExtParam = q->ext_buffers;
174 param.NumExtParam = q->nb_ext_buffers;
176 ret = MFXVideoDECODE_Init(q->session, ¶m);
178 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
179 return ff_qsv_error(ret);
185 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
189 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
193 if (frame->frame->format == AV_PIX_FMT_QSV) {
194 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
196 frame->surface_internal.Info.BitDepthLuma = 8;
197 frame->surface_internal.Info.BitDepthChroma = 8;
198 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
199 frame->surface_internal.Info.Width = avctx->coded_width;
200 frame->surface_internal.Info.Height = avctx->coded_height;
201 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
203 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
204 frame->surface_internal.Data.Y = frame->frame->data[0];
205 frame->surface_internal.Data.UV = frame->frame->data[1];
207 frame->surface = &frame->surface_internal;
213 static void qsv_clear_unused_frames(QSVContext *q)
215 QSVFrame *cur = q->work_frames;
217 if (cur->surface && !cur->surface->Data.Locked) {
219 av_frame_unref(cur->frame);
225 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
227 QSVFrame *frame, **last;
230 qsv_clear_unused_frames(q);
232 frame = q->work_frames;
233 last = &q->work_frames;
235 if (!frame->surface) {
236 ret = alloc_frame(avctx, frame);
239 *surf = frame->surface;
247 frame = av_mallocz(sizeof(*frame));
249 return AVERROR(ENOMEM);
250 frame->frame = av_frame_alloc();
253 return AVERROR(ENOMEM);
257 ret = alloc_frame(avctx, frame);
261 *surf = frame->surface;
266 static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
268 QSVFrame *cur = q->work_frames;
270 if (surf == cur->surface)
277 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
278 AVFrame *frame, int *got_frame,
281 mfxFrameSurface1 *insurf;
282 mfxFrameSurface1 *outsurf;
284 mfxBitstream bs = { { { 0 } } };
288 bs.Data = avpkt->data;
289 bs.DataLength = avpkt->size;
290 bs.MaxLength = bs.DataLength;
291 bs.TimeStamp = avpkt->pts;
295 ret = get_surface(avctx, q, &insurf);
299 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
300 insurf, &outsurf, &sync);
301 if (ret == MFX_WRN_DEVICE_BUSY)
304 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
306 if (ret != MFX_ERR_NONE &&
307 ret != MFX_ERR_MORE_DATA &&
308 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
309 ret != MFX_ERR_MORE_SURFACE) {
310 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
311 return ff_qsv_error(ret);
317 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
319 src_frame = find_frame(q, outsurf);
321 av_log(avctx, AV_LOG_ERROR,
322 "The returned surface does not correspond to any frame\n");
326 ret = av_frame_ref(frame, src_frame);
330 frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
333 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
334 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
335 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
336 frame->top_field_first =
337 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
338 frame->interlaced_frame =
339 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
344 return bs.DataOffset;
347 int ff_qsv_close(QSVContext *q)
349 QSVFrame *cur = q->work_frames;
352 q->work_frames = cur->next;
353 av_frame_free(&cur->frame);
355 cur = q->work_frames;
358 if (q->internal_session)
359 MFXClose(q->internal_session);