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"
38 #include "qsv_internal.h"
41 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
44 case AV_PIX_FMT_YUV420P:
45 case AV_PIX_FMT_YUVJ420P:
46 return AV_PIX_FMT_NV12;
48 return AVERROR(ENOSYS);
52 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
55 if (!q->internal_session) {
56 int ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
62 q->session = q->internal_session;
67 /* make sure the decoder is uninitialized */
68 MFXVideoDECODE_Close(q->session);
73 int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
75 mfxVideoParam param = { { 0 } };
79 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
80 (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
82 return AVERROR(ENOMEM);
85 ret = qsv_init_session(avctx, q, session);
87 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
92 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
96 param.mfx.CodecId = ret;
97 param.mfx.CodecProfile = avctx->profile;
98 param.mfx.CodecLevel = avctx->level;
100 param.mfx.FrameInfo.BitDepthLuma = 8;
101 param.mfx.FrameInfo.BitDepthChroma = 8;
102 param.mfx.FrameInfo.Shift = 0;
103 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
104 param.mfx.FrameInfo.Width = avctx->coded_width;
105 param.mfx.FrameInfo.Height = avctx->coded_height;
106 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
108 param.IOPattern = q->iopattern;
109 param.AsyncDepth = q->async_depth;
110 param.ExtParam = q->ext_buffers;
111 param.NumExtParam = q->nb_ext_buffers;
113 ret = MFXVideoDECODE_Init(q->session, ¶m);
115 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
116 return ff_qsv_error(ret);
122 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
126 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
130 if (frame->frame->format == AV_PIX_FMT_QSV) {
131 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
133 frame->surface_internal.Info.BitDepthLuma = 8;
134 frame->surface_internal.Info.BitDepthChroma = 8;
135 frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
136 frame->surface_internal.Info.Width = avctx->coded_width;
137 frame->surface_internal.Info.Height = avctx->coded_height;
138 frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
140 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
141 frame->surface_internal.Data.Y = frame->frame->data[0];
142 frame->surface_internal.Data.UV = frame->frame->data[1];
144 frame->surface = &frame->surface_internal;
150 static void qsv_clear_unused_frames(QSVContext *q)
152 QSVFrame *cur = q->work_frames;
154 if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
156 av_frame_unref(cur->frame);
162 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
164 QSVFrame *frame, **last;
167 qsv_clear_unused_frames(q);
169 frame = q->work_frames;
170 last = &q->work_frames;
172 if (!frame->surface) {
173 ret = alloc_frame(avctx, frame);
176 *surf = frame->surface;
184 frame = av_mallocz(sizeof(*frame));
186 return AVERROR(ENOMEM);
187 frame->frame = av_frame_alloc();
190 return AVERROR(ENOMEM);
194 ret = alloc_frame(avctx, frame);
198 *surf = frame->surface;
203 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
205 QSVFrame *cur = q->work_frames;
207 if (surf == cur->surface)
214 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
215 AVFrame *frame, int *got_frame,
219 mfxFrameSurface1 *insurf;
220 mfxFrameSurface1 *outsurf;
222 mfxBitstream bs = { { { 0 } } };
226 bs.Data = avpkt->data;
227 bs.DataLength = avpkt->size;
228 bs.MaxLength = bs.DataLength;
229 bs.TimeStamp = avpkt->pts;
233 ret = get_surface(avctx, q, &insurf);
237 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
238 insurf, &outsurf, &sync);
239 if (ret == MFX_WRN_DEVICE_BUSY)
242 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
244 if (ret != MFX_ERR_NONE &&
245 ret != MFX_ERR_MORE_DATA &&
246 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
247 ret != MFX_ERR_MORE_SURFACE) {
248 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
249 return ff_qsv_error(ret);
252 /* make sure we do not enter an infinite loop if the SDK
253 * did not consume any data and did not return anything */
254 if (!sync && !bs.DataOffset) {
255 av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n");
256 bs.DataOffset = avpkt->size;
260 QSVFrame *out_frame = find_frame(q, outsurf);
263 av_log(avctx, AV_LOG_ERROR,
264 "The returned surface does not correspond to any frame\n");
268 out_frame->queued = 1;
269 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
270 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
273 if (!av_fifo_space(q->async_fifo) ||
274 (!avpkt->size && av_fifo_size(q->async_fifo))) {
277 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
278 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
279 out_frame->queued = 0;
281 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
283 src_frame = out_frame->frame;
285 ret = av_frame_ref(frame, src_frame);
289 outsurf = out_frame->surface;
291 frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
294 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
295 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
296 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
297 frame->top_field_first =
298 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
299 frame->interlaced_frame =
300 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
305 return bs.DataOffset;
308 int ff_qsv_decode_close(QSVContext *q)
310 QSVFrame *cur = q->work_frames;
313 q->work_frames = cur->next;
314 av_frame_free(&cur->frame);
316 cur = q->work_frames;
319 av_fifo_free(q->async_fifo);
320 q->async_fifo = NULL;
322 av_parser_close(q->parser);
323 avcodec_free_context(&q->avctx_internal);
325 if (q->internal_session)
326 MFXClose(q->internal_session);
331 int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
332 AVFrame *frame, int *got_frame, AVPacket *pkt)
338 if (!q->avctx_internal) {
339 q->avctx_internal = avcodec_alloc_context3(NULL);
340 if (!q->avctx_internal)
341 return AVERROR(ENOMEM);
343 if (avctx->extradata) {
344 q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
345 if (!q->avctx_internal->extradata)
346 return AVERROR(ENOMEM);
348 memcpy(q->avctx_internal->extradata, avctx->extradata,
349 avctx->extradata_size);
350 q->avctx_internal->extradata_size = avctx->extradata_size;
353 q->parser = av_parser_init(avctx->codec_id);
355 return AVERROR(ENOMEM);
357 q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
358 q->orig_pix_fmt = AV_PIX_FMT_NONE;
362 return qsv_decode(avctx, q, frame, got_frame, pkt);
364 /* we assume the packets are already split properly and want
365 * just the codec parameters here */
366 av_parser_parse2(q->parser, q->avctx_internal,
367 &dummy_data, &dummy_size,
368 pkt->data, pkt->size, pkt->pts, pkt->dts,
371 /* TODO: flush delayed frames on reinit */
372 if (q->parser->format != q->orig_pix_fmt ||
373 q->parser->coded_width != avctx->coded_width ||
374 q->parser->coded_height != avctx->coded_height) {
375 mfxSession session = NULL;
377 enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
380 enum AVPixelFormat qsv_format;
382 qsv_format = ff_qsv_map_pixfmt(q->parser->format);
383 if (qsv_format < 0) {
384 av_log(avctx, AV_LOG_ERROR,
385 "Only 8-bit YUV420 streams are supported.\n");
386 ret = AVERROR(ENOSYS);
390 q->orig_pix_fmt = q->parser->format;
391 avctx->pix_fmt = pix_fmts[1] = qsv_format;
392 avctx->width = q->parser->width;
393 avctx->height = q->parser->height;
394 avctx->coded_width = q->parser->coded_width;
395 avctx->coded_height = q->parser->coded_height;
396 avctx->level = q->avctx_internal->level;
397 avctx->profile = q->avctx_internal->profile;
399 ret = ff_get_format(avctx, pix_fmts);
403 avctx->pix_fmt = ret;
405 if (avctx->hwaccel_context) {
406 AVQSVContext *user_ctx = avctx->hwaccel_context;
407 session = user_ctx->session;
408 q->iopattern = user_ctx->iopattern;
409 q->ext_buffers = user_ctx->ext_buffers;
410 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
413 ret = ff_qsv_decode_init(avctx, q, session);
418 return qsv_decode(avctx, q, frame, got_frame, pkt);
421 q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
425 void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
427 q->orig_pix_fmt = AV_PIX_FMT_NONE;