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 Libav.
9 * Libav 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 * Libav 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 Libav; 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 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
77 (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
79 return AVERROR(ENOMEM);
81 ret = qsv_init_session(avctx, q, session);
83 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
88 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
92 param.mfx.CodecId = ret;
93 param.mfx.CodecProfile = avctx->profile;
94 param.mfx.CodecLevel = avctx->level;
96 param.mfx.FrameInfo.BitDepthLuma = 8;
97 param.mfx.FrameInfo.BitDepthChroma = 8;
98 param.mfx.FrameInfo.Shift = 0;
99 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
100 param.mfx.FrameInfo.Width = avctx->coded_width;
101 param.mfx.FrameInfo.Height = avctx->coded_height;
102 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
104 param.IOPattern = q->iopattern;
105 param.AsyncDepth = q->async_depth;
106 param.ExtParam = q->ext_buffers;
107 param.NumExtParam = q->nb_ext_buffers;
109 ret = MFXVideoDECODE_Init(q->session, ¶m);
111 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
112 return ff_qsv_error(ret);
118 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
122 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
126 if (frame->frame->format == AV_PIX_FMT_QSV) {
127 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
129 frame->surface_internal.Info.BitDepthLuma = 8;
130 frame->surface_internal.Info.BitDepthChroma = 8;
131 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
132 frame->surface_internal.Info.Width = avctx->coded_width;
133 frame->surface_internal.Info.Height = avctx->coded_height;
134 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
136 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
137 frame->surface_internal.Data.Y = frame->frame->data[0];
138 frame->surface_internal.Data.UV = frame->frame->data[1];
140 frame->surface = &frame->surface_internal;
146 static void qsv_clear_unused_frames(QSVContext *q)
148 QSVFrame *cur = q->work_frames;
150 if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
152 av_frame_unref(cur->frame);
158 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
160 QSVFrame *frame, **last;
163 qsv_clear_unused_frames(q);
165 frame = q->work_frames;
166 last = &q->work_frames;
168 if (!frame->surface) {
169 ret = alloc_frame(avctx, frame);
172 *surf = frame->surface;
180 frame = av_mallocz(sizeof(*frame));
182 return AVERROR(ENOMEM);
183 frame->frame = av_frame_alloc();
186 return AVERROR(ENOMEM);
190 ret = alloc_frame(avctx, frame);
194 *surf = frame->surface;
199 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
201 QSVFrame *cur = q->work_frames;
203 if (surf == cur->surface)
210 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
211 AVFrame *frame, int *got_frame,
215 mfxFrameSurface1 *insurf;
216 mfxFrameSurface1 *outsurf;
218 mfxBitstream bs = { { { 0 } } };
222 bs.Data = avpkt->data;
223 bs.DataLength = avpkt->size;
224 bs.MaxLength = bs.DataLength;
225 bs.TimeStamp = avpkt->pts;
229 ret = get_surface(avctx, q, &insurf);
233 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
234 insurf, &outsurf, &sync);
235 if (ret == MFX_WRN_DEVICE_BUSY)
238 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
240 if (ret != MFX_ERR_NONE &&
241 ret != MFX_ERR_MORE_DATA &&
242 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
243 ret != MFX_ERR_MORE_SURFACE) {
244 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
245 return ff_qsv_error(ret);
249 QSVFrame *out_frame = find_frame(q, outsurf);
252 av_log(avctx, AV_LOG_ERROR,
253 "The returned surface does not correspond to any frame\n");
257 out_frame->queued = 1;
258 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
259 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
262 if (!av_fifo_space(q->async_fifo) ||
263 (!avpkt->size && av_fifo_size(q->async_fifo))) {
266 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
267 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
268 out_frame->queued = 0;
270 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
272 src_frame = out_frame->frame;
274 ret = av_frame_ref(frame, src_frame);
278 outsurf = out_frame->surface;
280 frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
283 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
284 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
285 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
286 frame->top_field_first =
287 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
288 frame->interlaced_frame =
289 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
294 return bs.DataOffset;
297 int ff_qsv_decode_close(QSVContext *q)
299 QSVFrame *cur = q->work_frames;
302 q->work_frames = cur->next;
303 av_frame_free(&cur->frame);
305 cur = q->work_frames;
308 av_fifo_free(q->async_fifo);
309 q->async_fifo = NULL;
311 if (q->internal_session)
312 MFXClose(q->internal_session);