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 switch (MFX_IMPL_BASETYPE(impl)) {
121 case MFX_IMPL_SOFTWARE:
124 case MFX_IMPL_HARDWARE:
125 case MFX_IMPL_HARDWARE2:
126 case MFX_IMPL_HARDWARE3:
127 case MFX_IMPL_HARDWARE4:
128 desc = "hardware accelerated";
134 av_log(avctx, AV_LOG_VERBOSE,
135 "Initialized an internal MFX session using %s implementation\n",
139 q->session = q->internal_session;
141 q->session = session;
144 /* make sure the decoder is uninitialized */
145 MFXVideoDECODE_Close(q->session);
150 int ff_qsv_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
152 mfxVideoParam param = { { 0 } };
155 ret = qsv_init_session(avctx, q, session);
157 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
162 ret = codec_id_to_mfx(avctx->codec_id);
166 param.mfx.CodecId = ret;
167 param.mfx.CodecProfile = avctx->profile;
168 param.mfx.CodecLevel = avctx->level;
170 param.mfx.FrameInfo.BitDepthLuma = 8;
171 param.mfx.FrameInfo.BitDepthChroma = 8;
172 param.mfx.FrameInfo.Shift = 0;
173 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
174 param.mfx.FrameInfo.Width = avctx->coded_width;
175 param.mfx.FrameInfo.Height = avctx->coded_height;
176 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
178 param.IOPattern = q->iopattern;
179 param.AsyncDepth = q->async_depth;
180 param.ExtParam = q->ext_buffers;
181 param.NumExtParam = q->nb_ext_buffers;
183 ret = MFXVideoDECODE_Init(q->session, ¶m);
185 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
186 return ff_qsv_error(ret);
192 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
196 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
200 if (frame->frame->format == AV_PIX_FMT_QSV) {
201 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
203 frame->surface_internal.Info.BitDepthLuma = 8;
204 frame->surface_internal.Info.BitDepthChroma = 8;
205 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
206 frame->surface_internal.Info.Width = avctx->coded_width;
207 frame->surface_internal.Info.Height = avctx->coded_height;
208 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
210 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
211 frame->surface_internal.Data.Y = frame->frame->data[0];
212 frame->surface_internal.Data.UV = frame->frame->data[1];
214 frame->surface = &frame->surface_internal;
220 static void qsv_clear_unused_frames(QSVContext *q)
222 QSVFrame *cur = q->work_frames;
224 if (cur->surface && !cur->surface->Data.Locked) {
226 av_frame_unref(cur->frame);
232 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
234 QSVFrame *frame, **last;
237 qsv_clear_unused_frames(q);
239 frame = q->work_frames;
240 last = &q->work_frames;
242 if (!frame->surface) {
243 ret = alloc_frame(avctx, frame);
246 *surf = frame->surface;
254 frame = av_mallocz(sizeof(*frame));
256 return AVERROR(ENOMEM);
257 frame->frame = av_frame_alloc();
260 return AVERROR(ENOMEM);
264 ret = alloc_frame(avctx, frame);
268 *surf = frame->surface;
273 static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
275 QSVFrame *cur = q->work_frames;
277 if (surf == cur->surface)
284 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
285 AVFrame *frame, int *got_frame,
288 mfxFrameSurface1 *insurf;
289 mfxFrameSurface1 *outsurf;
291 mfxBitstream bs = { { { 0 } } };
295 bs.Data = avpkt->data;
296 bs.DataLength = avpkt->size;
297 bs.MaxLength = bs.DataLength;
298 bs.TimeStamp = avpkt->pts;
302 ret = get_surface(avctx, q, &insurf);
306 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
307 insurf, &outsurf, &sync);
308 if (ret == MFX_WRN_DEVICE_BUSY)
311 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
313 if (ret != MFX_ERR_NONE &&
314 ret != MFX_ERR_MORE_DATA &&
315 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
316 ret != MFX_ERR_MORE_SURFACE) {
317 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
318 return ff_qsv_error(ret);
324 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
326 src_frame = find_frame(q, outsurf);
328 av_log(avctx, AV_LOG_ERROR,
329 "The returned surface does not correspond to any frame\n");
333 ret = av_frame_ref(frame, src_frame);
337 frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
340 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
341 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
342 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
343 frame->top_field_first =
344 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
345 frame->interlaced_frame =
346 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
351 return bs.DataOffset;
354 int ff_qsv_close(QSVContext *q)
356 QSVFrame *cur = q->work_frames;
359 q->work_frames = cur->next;
360 av_frame_free(&cur->frame);
362 cur = q->work_frames;
365 if (q->internal_session)
366 MFXClose(q->internal_session);