]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsvdec.c
qsvdec: fix a memleak of async_fifo
[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     if (!q->async_fifo) {
77         q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
78                                       (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
79         if (!q->async_fifo)
80             return AVERROR(ENOMEM);
81     }
82
83     ret = qsv_init_session(avctx, q, session);
84     if (ret < 0) {
85         av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
86         return ret;
87     }
88
89
90     ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
91     if (ret < 0)
92         return ret;
93
94     param.mfx.CodecId      = ret;
95     param.mfx.CodecProfile = avctx->profile;
96     param.mfx.CodecLevel   = avctx->level;
97
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;
105
106     param.IOPattern   = q->iopattern;
107     param.AsyncDepth  = q->async_depth;
108     param.ExtParam    = q->ext_buffers;
109     param.NumExtParam = q->nb_ext_buffers;
110
111     ret = MFXVideoDECODE_Init(q->session, &param);
112     if (ret < 0) {
113         av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
114         return ff_qsv_error(ret);
115     }
116
117     return 0;
118 }
119
120 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
121 {
122     int ret;
123
124     ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
125     if (ret < 0)
126         return ret;
127
128     if (frame->frame->format == AV_PIX_FMT_QSV) {
129         frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
130     } else {
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;
137
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];
141
142         frame->surface = &frame->surface_internal;
143     }
144
145     return 0;
146 }
147
148 static void qsv_clear_unused_frames(QSVContext *q)
149 {
150     QSVFrame *cur = q->work_frames;
151     while (cur) {
152         if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
153             cur->surface = NULL;
154             av_frame_unref(cur->frame);
155         }
156         cur = cur->next;
157     }
158 }
159
160 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
161 {
162     QSVFrame *frame, **last;
163     int ret;
164
165     qsv_clear_unused_frames(q);
166
167     frame = q->work_frames;
168     last  = &q->work_frames;
169     while (frame) {
170         if (!frame->surface) {
171             ret = alloc_frame(avctx, frame);
172             if (ret < 0)
173                 return ret;
174             *surf = frame->surface;
175             return 0;
176         }
177
178         last  = &frame->next;
179         frame = frame->next;
180     }
181
182     frame = av_mallocz(sizeof(*frame));
183     if (!frame)
184         return AVERROR(ENOMEM);
185     frame->frame = av_frame_alloc();
186     if (!frame->frame) {
187         av_freep(&frame);
188         return AVERROR(ENOMEM);
189     }
190     *last = frame;
191
192     ret = alloc_frame(avctx, frame);
193     if (ret < 0)
194         return ret;
195
196     *surf = frame->surface;
197
198     return 0;
199 }
200
201 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
202 {
203     QSVFrame *cur = q->work_frames;
204     while (cur) {
205         if (surf == cur->surface)
206             return cur;
207         cur = cur->next;
208     }
209     return NULL;
210 }
211
212 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
213                   AVFrame *frame, int *got_frame,
214                   AVPacket *avpkt)
215 {
216     QSVFrame *out_frame;
217     mfxFrameSurface1 *insurf;
218     mfxFrameSurface1 *outsurf;
219     mfxSyncPoint sync;
220     mfxBitstream bs = { { { 0 } } };
221     int ret;
222
223     if (avpkt->size) {
224         bs.Data       = avpkt->data;
225         bs.DataLength = avpkt->size;
226         bs.MaxLength  = bs.DataLength;
227         bs.TimeStamp  = avpkt->pts;
228     }
229
230     do {
231         ret = get_surface(avctx, q, &insurf);
232         if (ret < 0)
233             return ret;
234
235         ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
236                                               insurf, &outsurf, &sync);
237         if (ret == MFX_WRN_DEVICE_BUSY)
238             av_usleep(1);
239
240     } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
241
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);
248     }
249
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;
255     }
256
257     if (sync) {
258         QSVFrame *out_frame = find_frame(q, outsurf);
259
260         if (!out_frame) {
261             av_log(avctx, AV_LOG_ERROR,
262                    "The returned surface does not correspond to any frame\n");
263             return AVERROR_BUG;
264         }
265
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);
269     }
270
271     if (!av_fifo_space(q->async_fifo) ||
272         (!avpkt->size && av_fifo_size(q->async_fifo))) {
273         AVFrame *src_frame;
274
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;
278
279         MFXVideoCORE_SyncOperation(q->session, sync, 60000);
280
281         src_frame = out_frame->frame;
282
283         ret = av_frame_ref(frame, src_frame);
284         if (ret < 0)
285             return ret;
286
287         outsurf = out_frame->surface;
288
289         frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
290
291         frame->repeat_pict =
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);
299
300         *got_frame = 1;
301     }
302
303     return bs.DataOffset;
304 }
305
306 int ff_qsv_decode_close(QSVContext *q)
307 {
308     QSVFrame *cur = q->work_frames;
309
310     while (cur) {
311         q->work_frames = cur->next;
312         av_frame_free(&cur->frame);
313         av_freep(&cur);
314         cur = q->work_frames;
315     }
316
317     av_fifo_free(q->async_fifo);
318     q->async_fifo = NULL;
319
320     if (q->internal_session)
321         MFXClose(q->internal_session);
322
323     return 0;
324 }