]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsvdec.c
qsvdec: properly handle the warning from MFXVideoCORE_SyncOperation
[ffmpeg] / libavcodec / qsvdec.c
1 /*
2  * Intel MediaSDK QSV codec-independent code
3  *
4  * copyright (c) 2013 Luca Barbato
5  * copyright (c) 2015 Anton Khirnov <anton@khirnov.net>
6  *
7  * This file is part of Libav.
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <string.h>
25 #include <sys/types.h>
26
27 #include <mfx/mfxvideo.h>
28
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"
34
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "qsv.h"
38 #include "qsv_internal.h"
39 #include "qsvdec.h"
40
41 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
42 {
43     switch (format) {
44     case AV_PIX_FMT_YUV420P:
45     case AV_PIX_FMT_YUVJ420P:
46         return AV_PIX_FMT_NV12;
47     default:
48         return AVERROR(ENOSYS);
49     }
50 }
51
52 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
53 {
54     if (!session) {
55         if (!q->internal_session) {
56             int ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
57                                                    q->load_plugins);
58             if (ret < 0)
59                 return ret;
60         }
61
62         q->session = q->internal_session;
63     } else {
64         q->session = session;
65     }
66
67     /* make sure the decoder is uninitialized */
68     MFXVideoDECODE_Close(q->session);
69
70     return 0;
71 }
72
73 static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
74 {
75     mfxVideoParam param = { { 0 } };
76     int ret;
77
78     if (!q->async_fifo) {
79         q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
80                                       (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
81         if (!q->async_fifo)
82             return AVERROR(ENOMEM);
83     }
84
85     ret = qsv_init_session(avctx, q, session);
86     if (ret < 0) {
87         av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
88         return ret;
89     }
90
91
92     ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
93     if (ret < 0)
94         return ret;
95
96     param.mfx.CodecId      = ret;
97     param.mfx.CodecProfile = avctx->profile;
98     param.mfx.CodecLevel   = avctx->level;
99
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;
107
108     param.IOPattern   = q->iopattern;
109     param.AsyncDepth  = q->async_depth;
110     param.ExtParam    = q->ext_buffers;
111     param.NumExtParam = q->nb_ext_buffers;
112
113     ret = MFXVideoDECODE_Init(q->session, &param);
114     if (ret < 0) {
115         av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
116         return ff_qsv_error(ret);
117     }
118
119     return 0;
120 }
121
122 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
123 {
124     int ret;
125
126     ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
127     if (ret < 0)
128         return ret;
129
130     if (frame->frame->format == AV_PIX_FMT_QSV) {
131         frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
132     } else {
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;
139
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];
143
144         frame->surface = &frame->surface_internal;
145     }
146
147     return 0;
148 }
149
150 static void qsv_clear_unused_frames(QSVContext *q)
151 {
152     QSVFrame *cur = q->work_frames;
153     while (cur) {
154         if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
155             cur->surface = NULL;
156             av_frame_unref(cur->frame);
157         }
158         cur = cur->next;
159     }
160 }
161
162 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
163 {
164     QSVFrame *frame, **last;
165     int ret;
166
167     qsv_clear_unused_frames(q);
168
169     frame = q->work_frames;
170     last  = &q->work_frames;
171     while (frame) {
172         if (!frame->surface) {
173             ret = alloc_frame(avctx, frame);
174             if (ret < 0)
175                 return ret;
176             *surf = frame->surface;
177             return 0;
178         }
179
180         last  = &frame->next;
181         frame = frame->next;
182     }
183
184     frame = av_mallocz(sizeof(*frame));
185     if (!frame)
186         return AVERROR(ENOMEM);
187     frame->frame = av_frame_alloc();
188     if (!frame->frame) {
189         av_freep(&frame);
190         return AVERROR(ENOMEM);
191     }
192     *last = frame;
193
194     ret = alloc_frame(avctx, frame);
195     if (ret < 0)
196         return ret;
197
198     *surf = frame->surface;
199
200     return 0;
201 }
202
203 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
204 {
205     QSVFrame *cur = q->work_frames;
206     while (cur) {
207         if (surf == cur->surface)
208             return cur;
209         cur = cur->next;
210     }
211     return NULL;
212 }
213
214 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
215                       AVFrame *frame, int *got_frame,
216                       AVPacket *avpkt)
217 {
218     QSVFrame *out_frame;
219     mfxFrameSurface1 *insurf;
220     mfxFrameSurface1 *outsurf;
221     mfxSyncPoint sync;
222     mfxBitstream bs = { { { 0 } } };
223     int ret;
224
225     if (avpkt->size) {
226         bs.Data       = avpkt->data;
227         bs.DataLength = avpkt->size;
228         bs.MaxLength  = bs.DataLength;
229         bs.TimeStamp  = avpkt->pts;
230     }
231
232     do {
233         ret = get_surface(avctx, q, &insurf);
234         if (ret < 0)
235             return ret;
236
237         ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
238                                               insurf, &outsurf, &sync);
239         if (ret == MFX_WRN_DEVICE_BUSY)
240             av_usleep(1);
241
242     } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
243
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);
250     }
251
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;
257     }
258
259     if (sync) {
260         QSVFrame *out_frame = find_frame(q, outsurf);
261
262         if (!out_frame) {
263             av_log(avctx, AV_LOG_ERROR,
264                    "The returned surface does not correspond to any frame\n");
265             return AVERROR_BUG;
266         }
267
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);
271     }
272
273     if (!av_fifo_space(q->async_fifo) ||
274         (!avpkt->size && av_fifo_size(q->async_fifo))) {
275         AVFrame *src_frame;
276
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;
280
281         do {
282             ret = MFXVideoCORE_SyncOperation(q->session, sync, 1000);
283         } while (ret == MFX_WRN_IN_EXECUTION);
284
285         src_frame = out_frame->frame;
286
287         ret = av_frame_ref(frame, src_frame);
288         if (ret < 0)
289             return ret;
290
291         outsurf = out_frame->surface;
292
293         frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
294
295         frame->repeat_pict =
296             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
297             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
298             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
299         frame->top_field_first =
300             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
301         frame->interlaced_frame =
302             !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
303
304         *got_frame = 1;
305     }
306
307     return bs.DataOffset;
308 }
309
310 int ff_qsv_decode_close(QSVContext *q)
311 {
312     QSVFrame *cur = q->work_frames;
313
314     if (q->session)
315         MFXVideoDECODE_Close(q->session);
316
317     while (cur) {
318         q->work_frames = cur->next;
319         av_frame_free(&cur->frame);
320         av_freep(&cur);
321         cur = q->work_frames;
322     }
323
324     av_fifo_free(q->async_fifo);
325     q->async_fifo = NULL;
326
327     av_parser_close(q->parser);
328     avcodec_free_context(&q->avctx_internal);
329
330     if (q->internal_session)
331         MFXClose(q->internal_session);
332
333     return 0;
334 }
335
336 int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
337                         AVFrame *frame, int *got_frame, AVPacket *pkt)
338 {
339     uint8_t *dummy_data;
340     int dummy_size;
341     int ret;
342
343     if (!q->avctx_internal) {
344         q->avctx_internal = avcodec_alloc_context3(NULL);
345         if (!q->avctx_internal)
346             return AVERROR(ENOMEM);
347
348         if (avctx->extradata) {
349             q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
350             if (!q->avctx_internal->extradata)
351                 return AVERROR(ENOMEM);
352
353             memcpy(q->avctx_internal->extradata, avctx->extradata,
354                    avctx->extradata_size);
355             q->avctx_internal->extradata_size = avctx->extradata_size;
356         }
357
358         q->parser = av_parser_init(avctx->codec_id);
359         if (!q->parser)
360             return AVERROR(ENOMEM);
361
362         q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
363         q->orig_pix_fmt   = AV_PIX_FMT_NONE;
364     }
365
366     if (!pkt->size)
367         return qsv_decode(avctx, q, frame, got_frame, pkt);
368
369     /* we assume the packets are already split properly and want
370      * just the codec parameters here */
371     av_parser_parse2(q->parser, q->avctx_internal,
372                      &dummy_data, &dummy_size,
373                      pkt->data, pkt->size, pkt->pts, pkt->dts,
374                      pkt->pos);
375
376     /* TODO: flush delayed frames on reinit */
377     if (q->parser->format       != q->orig_pix_fmt    ||
378         q->parser->coded_width  != avctx->coded_width ||
379         q->parser->coded_height != avctx->coded_height) {
380         mfxSession session = NULL;
381
382         enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
383                                            AV_PIX_FMT_NONE,
384                                            AV_PIX_FMT_NONE };
385         enum AVPixelFormat qsv_format;
386
387         qsv_format = ff_qsv_map_pixfmt(q->parser->format);
388         if (qsv_format < 0) {
389             av_log(avctx, AV_LOG_ERROR,
390                    "Only 8-bit YUV420 streams are supported.\n");
391             ret = AVERROR(ENOSYS);
392             goto reinit_fail;
393         }
394
395         q->orig_pix_fmt     = q->parser->format;
396         avctx->pix_fmt      = pix_fmts[1] = qsv_format;
397         avctx->width        = q->parser->width;
398         avctx->height       = q->parser->height;
399         avctx->coded_width  = q->parser->coded_width;
400         avctx->coded_height = q->parser->coded_height;
401         avctx->level        = q->avctx_internal->level;
402         avctx->profile      = q->avctx_internal->profile;
403
404         ret = ff_get_format(avctx, pix_fmts);
405         if (ret < 0)
406             goto reinit_fail;
407
408         avctx->pix_fmt = ret;
409
410         if (avctx->hwaccel_context) {
411             AVQSVContext *user_ctx = avctx->hwaccel_context;
412             session           = user_ctx->session;
413             q->iopattern      = user_ctx->iopattern;
414             q->ext_buffers    = user_ctx->ext_buffers;
415             q->nb_ext_buffers = user_ctx->nb_ext_buffers;
416         }
417
418         ret = qsv_decode_init(avctx, q, session);
419         if (ret < 0)
420             goto reinit_fail;
421     }
422
423     return qsv_decode(avctx, q, frame, got_frame, pkt);
424
425 reinit_fail:
426     q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
427     return ret;
428 }
429
430 void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
431 {
432     q->orig_pix_fmt = AV_PIX_FMT_NONE;
433 }