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 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);
94 inlink->cur_buf = NULL;
97 static void queue_pop(FifoContext *s)
99 Buf *tmp = s->root.next->next;
100 if (s->last == s->root.next)
102 av_freep(&s->root.next);
106 static void end_frame(AVFilterLink *inlink) { }
108 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
111 * Move data pointers and pts offset samples forward.
113 static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
116 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
117 int planar = av_sample_fmt_is_planar(link->format);
118 int planes = planar ? nb_channels : 1;
119 int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
122 av_assert0(buf->audio->nb_samples > offset);
124 for (i = 0; i < planes; i++)
125 buf->extended_data[i] += block_align*offset;
126 if (buf->data != buf->extended_data)
127 memcpy(buf->data, buf->extended_data,
128 FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
129 buf->linesize[0] -= block_align*offset;
130 buf->audio->nb_samples -= offset;
132 if (buf->pts != AV_NOPTS_VALUE) {
133 buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
138 static int calc_ptr_alignment(AVFilterBufferRef *buf)
140 int planes = av_sample_fmt_is_planar(buf->format) ?
141 av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
145 for (p = 0; p < planes; p++) {
147 while ((intptr_t)buf->extended_data[p] % cur_align)
149 if (cur_align < min_align)
150 min_align = cur_align;
155 static int return_audio_frame(AVFilterContext *ctx)
157 AVFilterLink *link = ctx->outputs[0];
158 FifoContext *s = ctx->priv;
159 AVFilterBufferRef *head = s->root.next->buf;
160 AVFilterBufferRef *buf_out;
164 head->audio->nb_samples >= link->request_samples &&
165 calc_ptr_alignment(head) >= 32) {
166 if (head->audio->nb_samples == link->request_samples) {
170 buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
171 buf_out->audio->nb_samples = link->request_samples;
172 buffer_offset(link, head, link->request_samples);
175 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
178 s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
179 link->request_samples);
181 return AVERROR(ENOMEM);
183 s->buf_out->audio->nb_samples = 0;
184 s->buf_out->pts = head->pts;
185 s->allocated_samples = link->request_samples;
186 } else if (link->request_samples != s->allocated_samples) {
187 av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
188 "buffer was returned.\n");
189 return AVERROR(EINVAL);
192 while (s->buf_out->audio->nb_samples < s->allocated_samples) {
193 int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
194 head->audio->nb_samples);
196 av_samples_copy(s->buf_out->extended_data, head->extended_data,
197 s->buf_out->audio->nb_samples, 0, len, nb_channels,
199 s->buf_out->audio->nb_samples += len;
201 if (len == head->audio->nb_samples) {
202 avfilter_unref_buffer(head);
206 (ret = ff_request_frame(ctx->inputs[0])) < 0) {
207 if (ret == AVERROR_EOF) {
208 av_samples_set_silence(s->buf_out->extended_data,
209 s->buf_out->audio->nb_samples,
210 s->allocated_samples -
211 s->buf_out->audio->nb_samples,
212 nb_channels, link->format);
213 s->buf_out->audio->nb_samples = s->allocated_samples;
218 head = s->root.next->buf;
220 buffer_offset(link, head, len);
223 buf_out = s->buf_out;
226 return ff_filter_samples(link, buf_out);
229 static int request_frame(AVFilterLink *outlink)
231 FifoContext *fifo = outlink->src->priv;
234 if (!fifo->root.next) {
235 if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
239 /* by doing this, we give ownership of the reference to the next filter,
240 * so we don't have to worry about dereferencing it ourselves. */
241 switch (outlink->type) {
242 case AVMEDIA_TYPE_VIDEO:
243 ff_start_frame(outlink, fifo->root.next->buf);
244 ff_draw_slice (outlink, 0, outlink->h, 1);
245 ff_end_frame (outlink);
248 case AVMEDIA_TYPE_AUDIO:
249 if (outlink->request_samples) {
250 return return_audio_frame(outlink->src);
252 ret = ff_filter_samples(outlink, fifo->root.next->buf);
257 return AVERROR(EINVAL);
263 AVFilter avfilter_vf_fifo = {
265 .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
270 .priv_size = sizeof(FifoContext),
272 .inputs = (const AVFilterPad[]) {{ .name = "default",
273 .type = AVMEDIA_TYPE_VIDEO,
274 .get_video_buffer= ff_null_get_video_buffer,
275 .start_frame = start_frame,
276 .draw_slice = draw_slice,
277 .end_frame = end_frame,
278 .rej_perms = AV_PERM_REUSE2, },
280 .outputs = (const AVFilterPad[]) {{ .name = "default",
281 .type = AVMEDIA_TYPE_VIDEO,
282 .request_frame = request_frame, },
286 AVFilter avfilter_af_afifo = {
288 .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
293 .priv_size = sizeof(FifoContext),
295 .inputs = (AVFilterPad[]) {{ .name = "default",
296 .type = AVMEDIA_TYPE_AUDIO,
297 .get_audio_buffer = ff_null_get_audio_buffer,
298 .filter_samples = add_to_queue,
299 .rej_perms = AV_PERM_REUSE2, },
301 .outputs = (AVFilterPad[]) {{ .name = "default",
302 .type = AVMEDIA_TYPE_AUDIO,
303 .request_frame = request_frame, },