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/common.h"
27 #include "libavutil/mathematics.h"
38 typedef struct FifoContext {
40 Buf *last; ///< last buffered frame
43 * When a specific number of output samples is requested, the partial
44 * buffer is stored here
47 int allocated_samples; ///< number of samples out was allocated for
50 static av_cold int init(AVFilterContext *ctx)
52 FifoContext *s = ctx->priv;
58 static av_cold void uninit(AVFilterContext *ctx)
60 FifoContext *s = ctx->priv;
63 for (buf = s->root.next; buf; buf = tmp) {
65 av_frame_free(&buf->frame);
69 av_frame_free(&s->out);
72 static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
74 FifoContext *s = inlink->dst->priv;
76 s->last->next = av_mallocz(sizeof(Buf));
78 av_frame_free(&frame);
79 return AVERROR(ENOMEM);
82 s->last = s->last->next;
83 s->last->frame = frame;
88 static void queue_pop(FifoContext *s)
90 Buf *tmp = s->root.next->next;
91 if (s->last == s->root.next)
93 av_freep(&s->root.next);
97 static int request_frame(AVFilterLink *outlink)
99 FifoContext *s = outlink->src->priv;
103 if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
108 ret = ff_filter_frame(outlink, s->root.next->frame);
113 static const AVFilterPad avfilter_vf_fifo_inputs[] = {
116 .type = AVMEDIA_TYPE_VIDEO,
117 .filter_frame = add_to_queue,
122 static const AVFilterPad avfilter_vf_fifo_outputs[] = {
125 .type = AVMEDIA_TYPE_VIDEO,
126 .request_frame = request_frame,
131 const AVFilter ff_vf_fifo = {
133 .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
136 .priv_size = sizeof(FifoContext),
137 .inputs = avfilter_vf_fifo_inputs,
138 .outputs = avfilter_vf_fifo_outputs,
141 static const AVFilterPad avfilter_af_afifo_inputs[] = {
144 .type = AVMEDIA_TYPE_AUDIO,
145 .filter_frame = add_to_queue,
150 static const AVFilterPad avfilter_af_afifo_outputs[] = {
153 .type = AVMEDIA_TYPE_AUDIO,
154 .request_frame = request_frame,
159 const AVFilter ff_af_afifo = {
161 .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
164 .priv_size = sizeof(FifoContext),
165 .inputs = avfilter_af_afifo_inputs,
166 .outputs = avfilter_af_afifo_outputs,