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