lavfi/atempo: remove dead increment.
[ffmpeg.git] / libavfilter / fifo.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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_bufferp(&buf->buf);
69         av_free(buf);
70     }
71
72     avfilter_unref_bufferp(&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             if (!buf_out)
173                 return AVERROR(ENOMEM);
174
175             buf_out->audio->nb_samples = link->request_samples;
176             buffer_offset(link, head, link->request_samples);
177         }
178     } else {
179         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
180
181         if (!s->buf_out) {
182             s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
183                                              link->request_samples);
184             if (!s->buf_out)
185                 return AVERROR(ENOMEM);
186
187             s->buf_out->audio->nb_samples = 0;
188             s->buf_out->pts               = head->pts;
189             s->allocated_samples          = link->request_samples;
190         } else if (link->request_samples != s->allocated_samples) {
191             av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
192                    "buffer was returned.\n");
193             return AVERROR(EINVAL);
194         }
195
196         while (s->buf_out->audio->nb_samples < s->allocated_samples) {
197             int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
198                             head->audio->nb_samples);
199
200             av_samples_copy(s->buf_out->extended_data, head->extended_data,
201                             s->buf_out->audio->nb_samples, 0, len, nb_channels,
202                             link->format);
203             s->buf_out->audio->nb_samples += len;
204
205             if (len == head->audio->nb_samples) {
206                 avfilter_unref_buffer(head);
207                 queue_pop(s);
208
209                 if (!s->root.next &&
210                     (ret = ff_request_frame(ctx->inputs[0])) < 0) {
211                     if (ret == AVERROR_EOF) {
212                         av_samples_set_silence(s->buf_out->extended_data,
213                                                s->buf_out->audio->nb_samples,
214                                                s->allocated_samples -
215                                                s->buf_out->audio->nb_samples,
216                                                nb_channels, link->format);
217                         s->buf_out->audio->nb_samples = s->allocated_samples;
218                         break;
219                     }
220                     return ret;
221                 }
222                 head = s->root.next->buf;
223             } else {
224                 buffer_offset(link, head, len);
225             }
226         }
227         buf_out = s->buf_out;
228         s->buf_out = NULL;
229     }
230     return ff_filter_samples(link, buf_out);
231 }
232
233 static int request_frame(AVFilterLink *outlink)
234 {
235     FifoContext *fifo = outlink->src->priv;
236     int ret = 0;
237
238     if (!fifo->root.next) {
239         if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
240             return ret;
241     }
242
243     /* by doing this, we give ownership of the reference to the next filter,
244      * so we don't have to worry about dereferencing it ourselves. */
245     switch (outlink->type) {
246     case AVMEDIA_TYPE_VIDEO:
247         if ((ret = ff_start_frame(outlink, fifo->root.next->buf)) < 0 ||
248             (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
249             (ret = ff_end_frame(outlink)) < 0)
250             return ret;
251
252         queue_pop(fifo);
253         break;
254     case AVMEDIA_TYPE_AUDIO:
255         if (outlink->request_samples) {
256             return return_audio_frame(outlink->src);
257         } else {
258             ret = ff_filter_samples(outlink, fifo->root.next->buf);
259             queue_pop(fifo);
260         }
261         break;
262     default:
263         return AVERROR(EINVAL);
264     }
265
266     return ret;
267 }
268
269 AVFilter avfilter_vf_fifo = {
270     .name      = "fifo",
271     .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
272
273     .init      = init,
274     .uninit    = uninit,
275
276     .priv_size = sizeof(FifoContext),
277
278     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
279                                           .type            = AVMEDIA_TYPE_VIDEO,
280                                           .get_video_buffer= ff_null_get_video_buffer,
281                                           .start_frame     = add_to_queue,
282                                           .draw_slice      = draw_slice,
283                                           .end_frame       = end_frame,
284                                           .rej_perms       = AV_PERM_REUSE2, },
285                                         { .name = NULL}},
286     .outputs   = (const AVFilterPad[]) {{ .name            = "default",
287                                           .type            = AVMEDIA_TYPE_VIDEO,
288                                           .request_frame   = request_frame, },
289                                         { .name = NULL}},
290 };
291
292 AVFilter avfilter_af_afifo = {
293     .name        = "afifo",
294     .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
295
296     .init      = init,
297     .uninit    = uninit,
298
299     .priv_size = sizeof(FifoContext),
300
301     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
302                                           .type             = AVMEDIA_TYPE_AUDIO,
303                                           .get_audio_buffer = ff_null_get_audio_buffer,
304                                           .filter_samples   = add_to_queue,
305                                           .rej_perms        = AV_PERM_REUSE2, },
306                                         { .name = NULL}},
307     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
308                                           .type             = AVMEDIA_TYPE_AUDIO,
309                                           .request_frame    = request_frame, },
310                                         { .name = NULL}},
311 };