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