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 } };
77 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
78 (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
80 return AVERROR(ENOMEM);
83 ret = qsv_init_session(avctx, q, session);
85 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
90 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
94 param.mfx.CodecId = ret;
95 param.mfx.CodecProfile = avctx->profile;
96 param.mfx.CodecLevel = avctx->level;
98 param.mfx.FrameInfo.BitDepthLuma = 8;
99 param.mfx.FrameInfo.BitDepthChroma = 8;
100 param.mfx.FrameInfo.Shift = 0;
101 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
102 param.mfx.FrameInfo.Width = avctx->coded_width;
103 param.mfx.FrameInfo.Height = avctx->coded_height;
104 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
106 param.IOPattern = q->iopattern;
107 param.AsyncDepth = q->async_depth;
108 param.ExtParam = q->ext_buffers;
109 param.NumExtParam = q->nb_ext_buffers;
111 ret = MFXVideoDECODE_Init(q->session, ¶m);
113 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
114 return ff_qsv_error(ret);
120 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
124 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
128 if (frame->frame->format == AV_PIX_FMT_QSV) {
129 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
131 frame->surface_internal.Info.BitDepthLuma = 8;
132 frame->surface_internal.Info.BitDepthChroma = 8;
133 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
134 frame->surface_internal.Info.Width = avctx->coded_width;
135 frame->surface_internal.Info.Height = avctx->coded_height;
136 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
138 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
139 frame->surface_internal.Data.Y = frame->frame->data[0];
140 frame->surface_internal.Data.UV = frame->frame->data[1];
142 frame->surface = &frame->surface_internal;
148 static void qsv_clear_unused_frames(QSVContext *q)
150 QSVFrame *cur = q->work_frames;
152 if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
154 av_frame_unref(cur->frame);
160 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
162 QSVFrame *frame, **last;
165 qsv_clear_unused_frames(q);
167 frame = q->work_frames;
168 last = &q->work_frames;
170 if (!frame->surface) {
171 ret = alloc_frame(avctx, frame);
174 *surf = frame->surface;
182 frame = av_mallocz(sizeof(*frame));
184 return AVERROR(ENOMEM);
185 frame->frame = av_frame_alloc();
188 return AVERROR(ENOMEM);
192 ret = alloc_frame(avctx, frame);
196 *surf = frame->surface;
201 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
203 QSVFrame *cur = q->work_frames;
205 if (surf == cur->surface)
212 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
213 AVFrame *frame, int *got_frame,
217 mfxFrameSurface1 *insurf;
218 mfxFrameSurface1 *outsurf;
220 mfxBitstream bs = { { { 0 } } };
224 bs.Data = avpkt->data;
225 bs.DataLength = avpkt->size;
226 bs.MaxLength = bs.DataLength;
227 bs.TimeStamp = avpkt->pts;
231 ret = get_surface(avctx, q, &insurf);
235 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
236 insurf, &outsurf, &sync);
237 if (ret == MFX_WRN_DEVICE_BUSY)
240 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
242 if (ret != MFX_ERR_NONE &&
243 ret != MFX_ERR_MORE_DATA &&
244 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
245 ret != MFX_ERR_MORE_SURFACE) {
246 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
247 return ff_qsv_error(ret);
250 /* make sure we do not enter an infinite loop if the SDK
251 * did not consume any data and did not return anything */
252 if (!sync && !bs.DataOffset) {
253 av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n");
254 bs.DataOffset = avpkt->size;
258 QSVFrame *out_frame = find_frame(q, outsurf);
261 av_log(avctx, AV_LOG_ERROR,
262 "The returned surface does not correspond to any frame\n");
266 out_frame->queued = 1;
267 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
268 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
271 if (!av_fifo_space(q->async_fifo) ||
272 (!avpkt->size && av_fifo_size(q->async_fifo))) {
275 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
276 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
277 out_frame->queued = 0;
279 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
281 src_frame = out_frame->frame;
283 ret = av_frame_ref(frame, src_frame);
287 outsurf = out_frame->surface;
289 frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
292 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
293 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
294 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
295 frame->top_field_first =
296 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
297 frame->interlaced_frame =
298 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
303 return bs.DataOffset;
306 int ff_qsv_decode_close(QSVContext *q)
308 QSVFrame *cur = q->work_frames;
311 q->work_frames = cur->next;
312 av_frame_free(&cur->frame);
314 cur = q->work_frames;
317 av_fifo_free(q->async_fifo);
318 q->async_fifo = NULL;
320 if (q->internal_session)
321 MFXClose(q->internal_session);