lavfi: check all ff_start_frame/draw_slice/end_frame calls for errors
[ffmpeg.git] / libavfilter / fifo.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * FIFO buffering filter
24  */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/samplefmt.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct Buf {
37     AVFilterBufferRef *buf;
38     struct Buf        *next;
39 } Buf;
40
41 typedef struct {
42     Buf  root;
43     Buf *last;   ///< last buffered frame
44
45     /**
46      * When a specific number of output samples is requested, the partial
47      * buffer is stored here
48      */
49     AVFilterBufferRef *buf_out;
50     int allocated_samples;      ///< number of samples buf_out was allocated for
51 } FifoContext;
52
53 static av_cold int init(AVFilterContext *ctx, const char *args)
54 {
55     FifoContext *fifo = ctx->priv;
56     fifo->last = &fifo->root;
57
58     return 0;
59 }
60
61 static av_cold void uninit(AVFilterContext *ctx)
62 {
63     FifoContext *fifo = ctx->priv;
64     Buf *buf, *tmp;
65
66     for (buf = fifo->root.next; buf; buf = tmp) {
67         tmp = buf->next;
68         avfilter_unref_buffer(buf->buf);
69         av_free(buf);
70     }
71
72     avfilter_unref_buffer(fifo->buf_out);
73 }
74
75 static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
76 {
77     FifoContext *fifo = inlink->dst->priv;
78
79     inlink->cur_buf = NULL;
80     fifo->last->next = av_mallocz(sizeof(Buf));
81     if (!fifo->last->next) {
82         avfilter_unref_buffer(buf);
83         return AVERROR(ENOMEM);
84     }
85
86     fifo->last = fifo->last->next;
87     fifo->last->buf = buf;
88
89     return 0;
90 }
91
92 static void queue_pop(FifoContext *s)
93 {
94     Buf *tmp = s->root.next->next;
95     if (s->last == s->root.next)
96         s->last = &s->root;
97     av_freep(&s->root.next);
98     s->root.next = tmp;
99 }
100
101 static int end_frame(AVFilterLink *inlink)
102 {
103     return 0;
104 }
105
106 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
107 {
108     return 0;
109 }
110
111 /**
112  * Move data pointers and pts offset samples forward.
113  */
114 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
115                           int offset)
116 {
117     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
118     int planar = av_sample_fmt_is_planar(link->format);
119     int planes = planar ? nb_channels : 1;
120     int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
121     int i;
122
123     av_assert0(buf->audio->nb_samples > offset);
124
125     for (i = 0; i < planes; i++)
126         buf->extended_data[i] += block_align*offset;
127     if (buf->data != buf->extended_data)
128         memcpy(buf->data, buf->extended_data,
129                FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
130     buf->linesize[0] -= block_align*offset;
131     buf->audio->nb_samples -= offset;
132
133     if (buf->pts != AV_NOPTS_VALUE) {
134         buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
135                                  link->time_base);
136     }
137 }
138
139 static int calc_ptr_alignment(AVFilterBufferRef *buf)
140 {
141     int planes = av_sample_fmt_is_planar(buf->format) ?
142                  av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
143     int min_align = 128;
144     int p;
145
146     for (p = 0; p < planes; p++) {
147         int cur_align = 128;
148         while ((intptr_t)buf->extended_data[p] % cur_align)
149             cur_align >>= 1;
150         if (cur_align < min_align)
151             min_align = cur_align;
152     }
153     return min_align;
154 }
155
156 static int return_audio_frame(AVFilterContext *ctx)
157 {
158     AVFilterLink *link = ctx->outputs[0];
159     FifoContext *s = ctx->priv;
160     AVFilterBufferRef *head = s->root.next->buf;
161     AVFilterBufferRef *buf_out;
162     int ret;
163
164     if (!s->buf_out &&
165         head->audio->nb_samples >= link->request_samples &&
166         calc_ptr_alignment(head) >= 32) {
167         if (head->audio->nb_samples == link->request_samples) {
168             buf_out = head;
169             queue_pop(s);
170         } else {
171             buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
172             buf_out->audio->nb_samples = link->request_samples;
173             buffer_offset(link, head, link->request_samples);
174         }
175     } else {
176         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
177
178         if (!s->buf_out) {
179             s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
180                                              link->request_samples);
181             if (!s->buf_out)
182                 return AVERROR(ENOMEM);
183
184             s->buf_out->audio->nb_samples = 0;
185             s->buf_out->pts               = head->pts;
186             s->allocated_samples          = link->request_samples;
187         } else if (link->request_samples != s->allocated_samples) {
188             av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
189                    "buffer was returned.\n");
190             return AVERROR(EINVAL);
191         }
192
193         while (s->buf_out->audio->nb_samples < s->allocated_samples) {
194             int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
195                             head->audio->nb_samples);
196
197             av_samples_copy(s->buf_out->extended_data, head->extended_data,
198                             s->buf_out->audio->nb_samples, 0, len, nb_channels,
199                             link->format);
200             s->buf_out->audio->nb_samples += len;
201
202             if (len == head->audio->nb_samples) {
203                 avfilter_unref_buffer(head);
204                 queue_pop(s);
205
206                 if (!s->root.next &&
207                     (ret = ff_request_frame(ctx->inputs[0])) < 0) {
208                     if (ret == AVERROR_EOF) {
209                         av_samples_set_silence(s->buf_out->extended_data,
210                                                s->buf_out->audio->nb_samples,
211                                                s->allocated_samples -
212                                                s->buf_out->audio->nb_samples,
213                                                nb_channels, link->format);
214                         s->buf_out->audio->nb_samples = s->allocated_samples;
215                         break;
216                     }
217                     return ret;
218                 }
219                 head = s->root.next->buf;
220             } else {
221                 buffer_offset(link, head, len);
222             }
223         }
224         buf_out = s->buf_out;
225         s->buf_out = NULL;
226     }
227     return ff_filter_samples(link, buf_out);
228 }
229
230 static int request_frame(AVFilterLink *outlink)
231 {
232     FifoContext *fifo = outlink->src->priv;
233     int ret = 0;
234
235     if (!fifo->root.next) {
236         if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
237             return ret;
238     }
239
240     /* by doing this, we give ownership of the reference to the next filter,
241      * so we don't have to worry about dereferencing it ourselves. */
242     switch (outlink->type) {
243     case AVMEDIA_TYPE_VIDEO:
244         if ((ret = ff_start_frame(outlink, fifo->root.next->buf)) < 0 ||
245             (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
246             (ret = ff_end_frame(outlink)) < 0)
247             return ret;
248
249         queue_pop(fifo);
250         break;
251     case AVMEDIA_TYPE_AUDIO:
252         if (outlink->request_samples) {
253             return return_audio_frame(outlink->src);
254         } else {
255             ret = ff_filter_samples(outlink, fifo->root.next->buf);
256             queue_pop(fifo);
257         }
258         break;
259     default:
260         return AVERROR(EINVAL);
261     }
262
263     return ret;
264 }
265
266 AVFilter avfilter_vf_fifo = {
267     .name      = "fifo",
268     .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
269
270     .init      = init,
271     .uninit    = uninit,
272
273     .priv_size = sizeof(FifoContext),
274
275     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
276                                           .type            = AVMEDIA_TYPE_VIDEO,
277                                           .get_video_buffer= ff_null_get_video_buffer,
278                                           .start_frame     = add_to_queue,
279                                           .draw_slice      = draw_slice,
280                                           .end_frame       = end_frame,
281                                           .rej_perms       = AV_PERM_REUSE2, },
282                                         { .name = NULL}},
283     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
284                                           .type            = AVMEDIA_TYPE_VIDEO,
285                                           .request_frame   = request_frame, },
286                                         { .name = NULL}},
287 };
288
289 AVFilter avfilter_af_afifo = {
290     .name        = "afifo",
291     .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
292
293     .init      = init,
294     .uninit    = uninit,
295
296     .priv_size = sizeof(FifoContext),
297
298     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
299                                           .type             = AVMEDIA_TYPE_AUDIO,
300                                           .get_audio_buffer = ff_null_get_audio_buffer,
301                                           .filter_samples   = add_to_queue,
302                                           .rej_perms        = AV_PERM_REUSE2, },
303                                         { .name = NULL}},
304     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
305                                           .type             = AVMEDIA_TYPE_AUDIO,
306                                           .request_frame    = request_frame, },
307                                         { .name = NULL}},
308 };