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