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_buffer(buf->buf);
72 avfilter_unref_buffer(fifo->buf_out);
75 static void add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
77 FifoContext *fifo = inlink->dst->priv;
79 fifo->last->next = av_mallocz(sizeof(Buf));
80 fifo->last = fifo->last->next;
81 fifo->last->buf = buf;
84 static void queue_pop(FifoContext *s)
86 Buf *tmp = s->root.next->next;
87 if (s->last == s->root.next)
89 av_freep(&s->root.next);
93 static void end_frame(AVFilterLink *inlink) { }
95 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
98 * Move data pointers and pts offset samples forward.
100 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
103 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
104 int planar = av_sample_fmt_is_planar(link->format);
105 int planes = planar ? nb_channels : 1;
106 int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
109 av_assert0(buf->audio->nb_samples > offset);
111 for (i = 0; i < planes; i++)
112 buf->extended_data[i] += block_align*offset;
113 if (buf->data != buf->extended_data)
114 memcpy(buf->data, buf->extended_data,
115 FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
116 buf->linesize[0] -= block_align*offset;
117 buf->audio->nb_samples -= offset;
119 if (buf->pts != AV_NOPTS_VALUE) {
120 buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
125 static int calc_ptr_alignment(AVFilterBufferRef *buf)
127 int planes = av_sample_fmt_is_planar(buf->format) ?
128 av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
132 for (p = 0; p < planes; p++) {
134 while ((intptr_t)buf->extended_data[p] % cur_align)
136 if (cur_align < min_align)
137 min_align = cur_align;
142 static int return_audio_frame(AVFilterContext *ctx)
144 AVFilterLink *link = ctx->outputs[0];
145 FifoContext *s = ctx->priv;
146 AVFilterBufferRef *head = s->root.next->buf;
147 AVFilterBufferRef *buf_out;
151 head->audio->nb_samples >= link->request_samples &&
152 calc_ptr_alignment(head) >= 32) {
153 if (head->audio->nb_samples == link->request_samples) {
157 buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
158 buf_out->audio->nb_samples = link->request_samples;
159 buffer_offset(link, head, link->request_samples);
162 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
165 s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
166 link->request_samples);
168 return AVERROR(ENOMEM);
170 s->buf_out->audio->nb_samples = 0;
171 s->buf_out->pts = head->pts;
172 s->allocated_samples = link->request_samples;
173 } else if (link->request_samples != s->allocated_samples) {
174 av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
175 "buffer was returned.\n");
176 return AVERROR(EINVAL);
179 while (s->buf_out->audio->nb_samples < s->allocated_samples) {
180 int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
181 head->audio->nb_samples);
183 av_samples_copy(s->buf_out->extended_data, head->extended_data,
184 s->buf_out->audio->nb_samples, 0, len, nb_channels,
186 s->buf_out->audio->nb_samples += len;
188 if (len == head->audio->nb_samples) {
189 avfilter_unref_buffer(head);
193 (ret = ff_request_frame(ctx->inputs[0])) < 0) {
194 if (ret == AVERROR_EOF) {
195 av_samples_set_silence(s->buf_out->extended_data,
196 s->buf_out->audio->nb_samples,
197 s->allocated_samples -
198 s->buf_out->audio->nb_samples,
199 nb_channels, link->format);
200 s->buf_out->audio->nb_samples = s->allocated_samples;
205 head = s->root.next->buf;
207 buffer_offset(link, head, len);
210 buf_out = s->buf_out;
213 ff_filter_samples(link, buf_out);
218 static int request_frame(AVFilterLink *outlink)
220 FifoContext *fifo = outlink->src->priv;
223 if (!fifo->root.next) {
224 if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
228 /* by doing this, we give ownership of the reference to the next filter,
229 * so we don't have to worry about dereferencing it ourselves. */
230 switch (outlink->type) {
231 case AVMEDIA_TYPE_VIDEO:
232 ff_start_frame(outlink, fifo->root.next->buf);
233 ff_draw_slice (outlink, 0, outlink->h, 1);
234 ff_end_frame (outlink);
237 case AVMEDIA_TYPE_AUDIO:
238 if (outlink->request_samples) {
239 return return_audio_frame(outlink->src);
241 ff_filter_samples(outlink, fifo->root.next->buf);
246 return AVERROR(EINVAL);
252 AVFilter avfilter_vf_fifo = {
254 .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
259 .priv_size = sizeof(FifoContext),
261 .inputs = (const AVFilterPad[]) {{ .name = "default",
262 .type = AVMEDIA_TYPE_VIDEO,
263 .get_video_buffer= ff_null_get_video_buffer,
264 .start_frame = add_to_queue,
265 .draw_slice = draw_slice,
266 .end_frame = end_frame,
267 .rej_perms = AV_PERM_REUSE2, },
269 .outputs = (const AVFilterPad[]) {{ .name = "default",
270 .type = AVMEDIA_TYPE_VIDEO,
271 .request_frame = request_frame, },
275 AVFilter avfilter_af_afifo = {
277 .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
282 .priv_size = sizeof(FifoContext),
284 .inputs = (AVFilterPad[]) {{ .name = "default",
285 .type = AVMEDIA_TYPE_AUDIO,
286 .get_audio_buffer = ff_null_get_audio_buffer,
287 .filter_samples = add_to_queue,
288 .rej_perms = AV_PERM_REUSE2, },
290 .outputs = (AVFilterPad[]) {{ .name = "default",
291 .type = AVMEDIA_TYPE_AUDIO,
292 .request_frame = request_frame, },