lavfi: add error handling to filter_samples().
[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     fifo->last->next = av_mallocz(sizeof(Buf));
80     if (!fifo->last->next) {
81         avfilter_unref_buffer(buf);
82         return AVERROR(ENOMEM);
83     }
84
85     fifo->last = fifo->last->next;
86     fifo->last->buf = buf;
87
88     return 0;
89 }
90
91 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
92 {
93     add_to_queue(inlink, buf);
94 }
95
96 static void queue_pop(FifoContext *s)
97 {
98     Buf *tmp = s->root.next->next;
99     if (s->last == s->root.next)
100         s->last = &s->root;
101     av_freep(&s->root.next);
102     s->root.next = tmp;
103 }
104
105 static void end_frame(AVFilterLink *inlink) { }
106
107 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
108
109 /**
110  * Move data pointers and pts offset samples forward.
111  */
112 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
113                           int offset)
114 {
115     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
116     int planar = av_sample_fmt_is_planar(link->format);
117     int planes = planar ? nb_channels : 1;
118     int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
119     int i;
120
121     av_assert0(buf->audio->nb_samples > offset);
122
123     for (i = 0; i < planes; i++)
124         buf->extended_data[i] += block_align*offset;
125     if (buf->data != buf->extended_data)
126         memcpy(buf->data, buf->extended_data,
127                FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
128     buf->linesize[0] -= block_align*offset;
129     buf->audio->nb_samples -= offset;
130
131     if (buf->pts != AV_NOPTS_VALUE) {
132         buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
133                                  link->time_base);
134     }
135 }
136
137 static int calc_ptr_alignment(AVFilterBufferRef *buf)
138 {
139     int planes = av_sample_fmt_is_planar(buf->format) ?
140                  av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
141     int min_align = 128;
142     int p;
143
144     for (p = 0; p < planes; p++) {
145         int cur_align = 128;
146         while ((intptr_t)buf->extended_data[p] % cur_align)
147             cur_align >>= 1;
148         if (cur_align < min_align)
149             min_align = cur_align;
150     }
151     return min_align;
152 }
153
154 static int return_audio_frame(AVFilterContext *ctx)
155 {
156     AVFilterLink *link = ctx->outputs[0];
157     FifoContext *s = ctx->priv;
158     AVFilterBufferRef *head = s->root.next->buf;
159     AVFilterBufferRef *buf_out;
160     int ret;
161
162     if (!s->buf_out &&
163         head->audio->nb_samples >= link->request_samples &&
164         calc_ptr_alignment(head) >= 32) {
165         if (head->audio->nb_samples == link->request_samples) {
166             buf_out = head;
167             queue_pop(s);
168         } else {
169             buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
170             buf_out->audio->nb_samples = link->request_samples;
171             buffer_offset(link, head, link->request_samples);
172         }
173     } else {
174         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
175
176         if (!s->buf_out) {
177             s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
178                                              link->request_samples);
179             if (!s->buf_out)
180                 return AVERROR(ENOMEM);
181
182             s->buf_out->audio->nb_samples = 0;
183             s->buf_out->pts               = head->pts;
184             s->allocated_samples          = link->request_samples;
185         } else if (link->request_samples != s->allocated_samples) {
186             av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
187                    "buffer was returned.\n");
188             return AVERROR(EINVAL);
189         }
190
191         while (s->buf_out->audio->nb_samples < s->allocated_samples) {
192             int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
193                             head->audio->nb_samples);
194
195             av_samples_copy(s->buf_out->extended_data, head->extended_data,
196                             s->buf_out->audio->nb_samples, 0, len, nb_channels,
197                             link->format);
198             s->buf_out->audio->nb_samples += len;
199
200             if (len == head->audio->nb_samples) {
201                 avfilter_unref_buffer(head);
202                 queue_pop(s);
203
204                 if (!s->root.next &&
205                     (ret = ff_request_frame(ctx->inputs[0])) < 0) {
206                     if (ret == AVERROR_EOF) {
207                         av_samples_set_silence(s->buf_out->extended_data,
208                                                s->buf_out->audio->nb_samples,
209                                                s->allocated_samples -
210                                                s->buf_out->audio->nb_samples,
211                                                nb_channels, link->format);
212                         s->buf_out->audio->nb_samples = s->allocated_samples;
213                         break;
214                     }
215                     return ret;
216                 }
217                 head = s->root.next->buf;
218             } else {
219                 buffer_offset(link, head, len);
220             }
221         }
222         buf_out = s->buf_out;
223         s->buf_out = NULL;
224     }
225     return ff_filter_samples(link, buf_out);
226 }
227
228 static int request_frame(AVFilterLink *outlink)
229 {
230     FifoContext *fifo = outlink->src->priv;
231     int ret = 0;
232
233     if (!fifo->root.next) {
234         if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
235             return ret;
236     }
237
238     /* by doing this, we give ownership of the reference to the next filter,
239      * so we don't have to worry about dereferencing it ourselves. */
240     switch (outlink->type) {
241     case AVMEDIA_TYPE_VIDEO:
242         ff_start_frame(outlink, fifo->root.next->buf);
243         ff_draw_slice (outlink, 0, outlink->h, 1);
244         ff_end_frame  (outlink);
245         queue_pop(fifo);
246         break;
247     case AVMEDIA_TYPE_AUDIO:
248         if (outlink->request_samples) {
249             return return_audio_frame(outlink->src);
250         } else {
251             ret = ff_filter_samples(outlink, fifo->root.next->buf);
252             queue_pop(fifo);
253         }
254         break;
255     default:
256         return AVERROR(EINVAL);
257     }
258
259     return ret;
260 }
261
262 AVFilter avfilter_vf_fifo = {
263     .name      = "fifo",
264     .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
265
266     .init      = init,
267     .uninit    = uninit,
268
269     .priv_size = sizeof(FifoContext),
270
271     .inputs    = (AVFilterPad[]) {{ .name            = "default",
272                                     .type            = AVMEDIA_TYPE_VIDEO,
273                                     .get_video_buffer= ff_null_get_video_buffer,
274                                     .start_frame     = start_frame,
275                                     .draw_slice      = draw_slice,
276                                     .end_frame       = end_frame,
277                                     .rej_perms       = AV_PERM_REUSE2, },
278                                   { .name = NULL}},
279     .outputs   = (AVFilterPad[]) {{ .name            = "default",
280                                     .type            = AVMEDIA_TYPE_VIDEO,
281                                     .request_frame   = request_frame, },
282                                   { .name = NULL}},
283 };
284
285 AVFilter avfilter_af_afifo = {
286     .name        = "afifo",
287     .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
288
289     .init      = init,
290     .uninit    = uninit,
291
292     .priv_size = sizeof(FifoContext),
293
294     .inputs    = (AVFilterPad[]) {{ .name             = "default",
295                                     .type             = AVMEDIA_TYPE_AUDIO,
296                                     .get_audio_buffer = ff_null_get_audio_buffer,
297                                     .filter_samples   = add_to_queue,
298                                     .rej_perms        = AV_PERM_REUSE2, },
299                                   { .name = NULL}},
300     .outputs   = (AVFilterPad[]) {{ .name             = "default",
301                                     .type             = AVMEDIA_TYPE_AUDIO,
302                                     .request_frame    = request_frame, },
303                                   { .name = NULL}},
304 };