2 * Copyright (c) 2007 Bobby Bingham
4 * This file is part of FFmpeg.
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.
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.
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
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_bufferp(&buf->buf);
72 avfilter_unref_bufferp(&fifo->buf_out);
75 static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
77 FifoContext *fifo = inlink->dst->priv;
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);
86 fifo->last = fifo->last->next;
87 fifo->last->buf = buf;
92 static void queue_pop(FifoContext *s)
94 Buf *tmp = s->root.next->next;
95 if (s->last == s->root.next)
97 av_freep(&s->root.next);
101 static int end_frame(AVFilterLink *inlink)
106 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
112 * Move data pointers and pts offset samples forward.
114 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
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);
123 av_assert0(buf->audio->nb_samples > offset);
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;
133 if (buf->pts != AV_NOPTS_VALUE) {
134 buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
139 static int calc_ptr_alignment(AVFilterBufferRef *buf)
141 int planes = av_sample_fmt_is_planar(buf->format) ?
142 av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
146 for (p = 0; p < planes; p++) {
148 while ((intptr_t)buf->extended_data[p] % cur_align)
150 if (cur_align < min_align)
151 min_align = cur_align;
156 static int return_audio_frame(AVFilterContext *ctx)
158 AVFilterLink *link = ctx->outputs[0];
159 FifoContext *s = ctx->priv;
160 AVFilterBufferRef *head = s->root.next->buf;
161 AVFilterBufferRef *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) {
171 buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
173 return AVERROR(ENOMEM);
175 buf_out->audio->nb_samples = link->request_samples;
176 buffer_offset(link, head, link->request_samples);
179 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
182 s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
183 link->request_samples);
185 return AVERROR(ENOMEM);
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);
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);
200 av_samples_copy(s->buf_out->extended_data, head->extended_data,
201 s->buf_out->audio->nb_samples, 0, len, nb_channels,
203 s->buf_out->audio->nb_samples += len;
205 if (len == head->audio->nb_samples) {
206 avfilter_unref_buffer(head);
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;
222 head = s->root.next->buf;
224 buffer_offset(link, head, len);
227 buf_out = s->buf_out;
230 return ff_filter_samples(link, buf_out);
233 static int request_frame(AVFilterLink *outlink)
235 FifoContext *fifo = outlink->src->priv;
238 if (!fifo->root.next) {
239 if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
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)
254 case AVMEDIA_TYPE_AUDIO:
255 if (outlink->request_samples) {
256 return return_audio_frame(outlink->src);
258 ret = ff_filter_samples(outlink, fifo->root.next->buf);
263 return AVERROR(EINVAL);
269 AVFilter avfilter_vf_fifo = {
271 .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
276 .priv_size = sizeof(FifoContext),
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, },
286 .outputs = (const AVFilterPad[]) {{ .name = "default",
287 .type = AVMEDIA_TYPE_VIDEO,
288 .request_frame = request_frame, },
292 AVFilter avfilter_af_afifo = {
294 .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
299 .priv_size = sizeof(FifoContext),
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, },
307 .outputs = (const AVFilterPad[]) {{ .name = "default",
308 .type = AVMEDIA_TYPE_AUDIO,
309 .request_frame = request_frame, },