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/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/samplefmt.h"
38 AVFilterBufferRef *buf;
44 Buf *last; ///< last buffered frame
47 * When a specific number of output samples is requested, the partial
48 * buffer is stored here
50 AVFilterBufferRef *buf_out;
51 int allocated_samples; ///< number of samples buf_out was allocated for
54 static av_cold int init(AVFilterContext *ctx, const char *args)
56 FifoContext *fifo = ctx->priv;
57 fifo->last = &fifo->root;
62 static av_cold void uninit(AVFilterContext *ctx)
64 FifoContext *fifo = ctx->priv;
67 for (buf = fifo->root.next; buf; buf = tmp) {
69 avfilter_unref_bufferp(&buf->buf);
73 avfilter_unref_bufferp(&fifo->buf_out);
76 static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
78 FifoContext *fifo = inlink->dst->priv;
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);
102 * Move data pointers and pts offset samples forward.
104 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
107 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
108 int planar = av_sample_fmt_is_planar(link->format);
109 int planes = planar ? nb_channels : 1;
110 int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
113 av_assert0(buf->audio->nb_samples > offset);
115 for (i = 0; i < planes; i++)
116 buf->extended_data[i] += block_align*offset;
117 if (buf->data != buf->extended_data)
118 memcpy(buf->data, buf->extended_data,
119 FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
120 buf->linesize[0] -= block_align*offset;
121 buf->audio->nb_samples -= offset;
123 if (buf->pts != AV_NOPTS_VALUE) {
124 buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
129 static int calc_ptr_alignment(AVFilterBufferRef *buf)
131 int planes = av_sample_fmt_is_planar(buf->format) ?
132 av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
136 for (p = 0; p < planes; p++) {
138 while ((intptr_t)buf->extended_data[p] % cur_align)
140 if (cur_align < min_align)
141 min_align = cur_align;
146 static int return_audio_frame(AVFilterContext *ctx)
148 AVFilterLink *link = ctx->outputs[0];
149 FifoContext *s = ctx->priv;
150 AVFilterBufferRef *head = s->root.next->buf;
151 AVFilterBufferRef *buf_out;
155 head->audio->nb_samples >= link->request_samples &&
156 calc_ptr_alignment(head) >= 32) {
157 if (head->audio->nb_samples == link->request_samples) {
161 buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
163 return AVERROR(ENOMEM);
165 buf_out->audio->nb_samples = link->request_samples;
166 buffer_offset(link, head, link->request_samples);
169 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
172 s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
173 link->request_samples);
175 return AVERROR(ENOMEM);
177 s->buf_out->audio->nb_samples = 0;
178 s->buf_out->pts = head->pts;
179 s->allocated_samples = link->request_samples;
180 } else if (link->request_samples != s->allocated_samples) {
181 av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
182 "buffer was returned.\n");
183 return AVERROR(EINVAL);
186 while (s->buf_out->audio->nb_samples < s->allocated_samples) {
187 int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
188 head->audio->nb_samples);
190 av_samples_copy(s->buf_out->extended_data, head->extended_data,
191 s->buf_out->audio->nb_samples, 0, len, nb_channels,
193 s->buf_out->audio->nb_samples += len;
195 if (len == head->audio->nb_samples) {
196 avfilter_unref_buffer(head);
200 (ret = ff_request_frame(ctx->inputs[0])) < 0) {
201 if (ret == AVERROR_EOF) {
202 av_samples_set_silence(s->buf_out->extended_data,
203 s->buf_out->audio->nb_samples,
204 s->allocated_samples -
205 s->buf_out->audio->nb_samples,
206 nb_channels, link->format);
207 s->buf_out->audio->nb_samples = s->allocated_samples;
212 head = s->root.next->buf;
214 buffer_offset(link, head, len);
217 buf_out = s->buf_out;
220 return ff_filter_frame(link, buf_out);
223 static int request_frame(AVFilterLink *outlink)
225 FifoContext *fifo = outlink->src->priv;
228 if (!fifo->root.next) {
229 if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
231 av_assert0(fifo->root.next);
234 if (outlink->request_samples) {
235 return return_audio_frame(outlink->src);
237 ret = ff_filter_frame(outlink, fifo->root.next->buf);
244 static const AVFilterPad avfilter_vf_fifo_inputs[] = {
247 .type = AVMEDIA_TYPE_VIDEO,
248 .get_video_buffer = ff_null_get_video_buffer,
249 .filter_frame = add_to_queue,
250 .min_perms = AV_PERM_PRESERVE,
255 static const AVFilterPad avfilter_vf_fifo_outputs[] = {
258 .type = AVMEDIA_TYPE_VIDEO,
259 .request_frame = request_frame,
264 AVFilter avfilter_vf_fifo = {
266 .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
271 .priv_size = sizeof(FifoContext),
273 .inputs = avfilter_vf_fifo_inputs,
274 .outputs = avfilter_vf_fifo_outputs,
277 static const AVFilterPad avfilter_af_afifo_inputs[] = {
280 .type = AVMEDIA_TYPE_AUDIO,
281 .get_audio_buffer = ff_null_get_audio_buffer,
282 .filter_frame = add_to_queue,
283 .min_perms = AV_PERM_PRESERVE,
288 static const AVFilterPad avfilter_af_afifo_outputs[] = {
291 .type = AVMEDIA_TYPE_AUDIO,
292 .request_frame = request_frame,
297 AVFilter avfilter_af_afifo = {
299 .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
304 .priv_size = sizeof(FifoContext),
306 .inputs = avfilter_af_afifo_inputs,
307 .outputs = avfilter_af_afifo_outputs,