]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsvdec.c
qsvdec: properly handle asynchronous decoding
[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_internal.h"
38 #include "qsvdec.h"
39
40 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
41 {
42     switch (format) {
43     case AV_PIX_FMT_YUV420P:
44     case AV_PIX_FMT_YUVJ420P:
45         return AV_PIX_FMT_NV12;
46     default:
47         return AVERROR(ENOSYS);
48     }
49 }
50
51 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
52 {
53     if (!session) {
54         if (!q->internal_session) {
55             int ret = ff_qsv_init_internal_session(avctx, &q->internal_session, NULL);
56             if (ret < 0)
57                 return ret;
58         }
59
60         q->session = q->internal_session;
61     } else {
62         q->session = session;
63     }
64
65     /* make sure the decoder is uninitialized */
66     MFXVideoDECODE_Close(q->session);
67
68     return 0;
69 }
70
71 int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
72 {
73     mfxVideoParam param = { { 0 } };
74     int ret;
75
76     q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
77                                   (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
78     if (!q->async_fifo)
79         return AVERROR(ENOMEM);
80
81     ret = qsv_init_session(avctx, q, session);
82     if (ret < 0) {
83         av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
84         return ret;
85     }
86
87
88     ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
89     if (ret < 0)
90         return ret;
91
92     param.mfx.CodecId      = ret;
93     param.mfx.CodecProfile = avctx->profile;
94     param.mfx.CodecLevel   = avctx->level;
95
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;
103
104     param.IOPattern   = q->iopattern;
105     param.AsyncDepth  = q->async_depth;
106     param.ExtParam    = q->ext_buffers;
107     param.NumExtParam = q->nb_ext_buffers;
108
109     ret = MFXVideoDECODE_Init(q->session, &param);
110     if (ret < 0) {
111         av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
112         return ff_qsv_error(ret);
113     }
114
115     return 0;
116 }
117
118 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
119 {
120     int ret;
121
122     ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
123     if (ret < 0)
124         return ret;
125
126     if (frame->frame->format == AV_PIX_FMT_QSV) {
127         frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
128     } else {
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;
135
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];
139
140         frame->surface = &frame->surface_internal;
141     }
142
143     return 0;
144 }
145
146 static void qsv_clear_unused_frames(QSVContext *q)
147 {
148     QSVFrame *cur = q->work_frames;
149     while (cur) {
150         if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
151             cur->surface = NULL;
152             av_frame_unref(cur->frame);
153         }
154         cur = cur->next;
155     }
156 }
157
158 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
159 {
160     QSVFrame *frame, **last;
161     int ret;
162
163     qsv_clear_unused_frames(q);
164
165     frame = q->work_frames;
166     last  = &q->work_frames;
167     while (frame) {
168         if (!frame->surface) {
169             ret = alloc_frame(avctx, frame);
170             if (ret < 0)
171                 return ret;
172             *surf = frame->surface;
173             return 0;
174         }
175
176         last  = &frame->next;
177         frame = frame->next;
178     }
179
180     frame = av_mallocz(sizeof(*frame));
181     if (!frame)
182         return AVERROR(ENOMEM);
183     frame->frame = av_frame_alloc();
184     if (!frame->frame) {
185         av_freep(&frame);
186         return AVERROR(ENOMEM);
187     }
188     *last = frame;
189
190     ret = alloc_frame(avctx, frame);
191     if (ret < 0)
192         return ret;
193
194     *surf = frame->surface;
195
196     return 0;
197 }
198
199 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
200 {
201     QSVFrame *cur = q->work_frames;
202     while (cur) {
203         if (surf == cur->surface)
204             return cur;
205         cur = cur->next;
206     }
207     return NULL;
208 }
209
210 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
211                   AVFrame *frame, int *got_frame,
212                   AVPacket *avpkt)
213 {
214     QSVFrame *out_frame;
215     mfxFrameSurface1 *insurf;
216     mfxFrameSurface1 *outsurf;
217     mfxSyncPoint sync;
218     mfxBitstream bs = { { { 0 } } };
219     int ret;
220
221     if (avpkt->size) {
222         bs.Data       = avpkt->data;
223         bs.DataLength = avpkt->size;
224         bs.MaxLength  = bs.DataLength;
225         bs.TimeStamp  = avpkt->pts;
226     }
227
228     do {
229         ret = get_surface(avctx, q, &insurf);
230         if (ret < 0)
231             return ret;
232
233         ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
234                                               insurf, &outsurf, &sync);
235         if (ret == MFX_WRN_DEVICE_BUSY)
236             av_usleep(1);
237
238     } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
239
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);
246     }
247
248     if (sync) {
249         QSVFrame *out_frame = find_frame(q, outsurf);
250
251         if (!out_frame) {
252             av_log(avctx, AV_LOG_ERROR,
253                    "The returned surface does not correspond to any frame\n");
254             return AVERROR_BUG;
255         }
256
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);
260     }
261
262     if (!av_fifo_space(q->async_fifo) ||
263         (!avpkt->size && av_fifo_size(q->async_fifo))) {
264         AVFrame *src_frame;
265
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;
269
270         MFXVideoCORE_SyncOperation(q->session, sync, 60000);
271
272         src_frame = out_frame->frame;
273
274         ret = av_frame_ref(frame, src_frame);
275         if (ret < 0)
276             return ret;
277
278         outsurf = out_frame->surface;
279
280         frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
281
282         frame->repeat_pict =
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);
290
291         *got_frame = 1;
292     }
293
294     return bs.DataOffset;
295 }
296
297 int ff_qsv_decode_close(QSVContext *q)
298 {
299     QSVFrame *cur = q->work_frames;
300
301     while (cur) {
302         q->work_frames = cur->next;
303         av_frame_free(&cur->frame);
304         av_freep(&cur);
305         cur = q->work_frames;
306     }
307
308     av_fifo_free(q->async_fifo);
309     q->async_fifo = NULL;
310
311     if (q->internal_session)
312         MFXClose(q->internal_session);
313
314     return 0;
315 }