2 * Copyright (c) 2007 Bobby Bingham
4 * This file is part of Libav.
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.
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.
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
23 * FIFO buffering filter
26 #include "libavutil/avassert.h"
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/samplefmt.h"
37 AVFilterBufferRef *buf;
43 Buf *last; ///< last buffered frame
46 * When a specific number of output samples is requested, the partial
47 * buffer is stored here
49 AVFilterBufferRef *buf_out;
50 int allocated_samples; ///< number of samples buf_out was allocated for
53 static av_cold int init(AVFilterContext *ctx, const char *args)
55 FifoContext *fifo = ctx->priv;
56 fifo->last = &fifo->root;
61 static av_cold void uninit(AVFilterContext *ctx)
63 FifoContext *fifo = ctx->priv;
66 for (buf = fifo->root.next; buf; buf = tmp) {
68 avfilter_unref_buffer(buf->buf);
72 avfilter_unref_buffer(fifo->buf_out);
75 static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
77 FifoContext *fifo = inlink->dst->priv;
79 fifo->last->next = av_mallocz(sizeof(Buf));
80 if (!fifo->last->next) {
81 avfilter_unref_buffer(buf);
82 return AVERROR(ENOMEM);
85 fifo->last = fifo->last->next;
86 fifo->last->buf = buf;
91 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
93 add_to_queue(inlink, buf);
96 static void queue_pop(FifoContext *s)
98 Buf *tmp = s->root.next->next;
99 if (s->last == s->root.next)
101 av_freep(&s->root.next);
105 static void end_frame(AVFilterLink *inlink) { }
107 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
110 * Move data pointers and pts offset samples forward.
112 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
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);
121 av_assert0(buf->audio->nb_samples > offset);
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;
131 if (buf->pts != AV_NOPTS_VALUE) {
132 buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
137 static int calc_ptr_alignment(AVFilterBufferRef *buf)
139 int planes = av_sample_fmt_is_planar(buf->format) ?
140 av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
144 for (p = 0; p < planes; p++) {
146 while ((intptr_t)buf->extended_data[p] % cur_align)
148 if (cur_align < min_align)
149 min_align = cur_align;
154 static int return_audio_frame(AVFilterContext *ctx)
156 AVFilterLink *link = ctx->outputs[0];
157 FifoContext *s = ctx->priv;
158 AVFilterBufferRef *head = s->root.next->buf;
159 AVFilterBufferRef *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) {
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);
174 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
177 s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
178 link->request_samples);
180 return AVERROR(ENOMEM);
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);
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);
195 av_samples_copy(s->buf_out->extended_data, head->extended_data,
196 s->buf_out->audio->nb_samples, 0, len, nb_channels,
198 s->buf_out->audio->nb_samples += len;
200 if (len == head->audio->nb_samples) {
201 avfilter_unref_buffer(head);
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;
217 head = s->root.next->buf;
219 buffer_offset(link, head, len);
222 buf_out = s->buf_out;
225 return ff_filter_samples(link, buf_out);
228 static int request_frame(AVFilterLink *outlink)
230 FifoContext *fifo = outlink->src->priv;
233 if (!fifo->root.next) {
234 if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
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);
247 case AVMEDIA_TYPE_AUDIO:
248 if (outlink->request_samples) {
249 return return_audio_frame(outlink->src);
251 ret = ff_filter_samples(outlink, fifo->root.next->buf);
256 return AVERROR(EINVAL);
262 AVFilter avfilter_vf_fifo = {
264 .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
269 .priv_size = sizeof(FifoContext),
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, },
279 .outputs = (AVFilterPad[]) {{ .name = "default",
280 .type = AVMEDIA_TYPE_VIDEO,
281 .request_frame = request_frame, },
285 AVFilter avfilter_af_afifo = {
287 .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
292 .priv_size = sizeof(FifoContext),
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, },
300 .outputs = (AVFilterPad[]) {{ .name = "default",
301 .type = AVMEDIA_TYPE_AUDIO,
302 .request_frame = request_frame, },