• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * FIFO buffering filter
24  */
25 
26 #include "libavutil/common.h"
27 #include "libavutil/mathematics.h"
28 
29 #include "audio.h"
30 #include "avfilter.h"
31 #include "internal.h"
32 
33 typedef struct Buf {
34     AVFrame *frame;
35     struct Buf *next;
36 } Buf;
37 
38 typedef struct FifoContext {
39     Buf  root;
40     Buf *last;   ///< last buffered frame
41 
42     /**
43      * When a specific number of output samples is requested, the partial
44      * buffer is stored here
45      */
46     AVFrame *out;
47     int allocated_samples;      ///< number of samples out was allocated for
48 } FifoContext;
49 
init(AVFilterContext * ctx)50 static av_cold int init(AVFilterContext *ctx)
51 {
52     FifoContext *s = ctx->priv;
53     s->last = &s->root;
54 
55     return 0;
56 }
57 
uninit(AVFilterContext * ctx)58 static av_cold void uninit(AVFilterContext *ctx)
59 {
60     FifoContext *s = ctx->priv;
61     Buf *buf, *tmp;
62 
63     for (buf = s->root.next; buf; buf = tmp) {
64         tmp = buf->next;
65         av_frame_free(&buf->frame);
66         av_free(buf);
67     }
68 
69     av_frame_free(&s->out);
70 }
71 
add_to_queue(AVFilterLink * inlink,AVFrame * frame)72 static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
73 {
74     FifoContext *s = inlink->dst->priv;
75 
76     s->last->next = av_mallocz(sizeof(Buf));
77     if (!s->last->next) {
78         av_frame_free(&frame);
79         return AVERROR(ENOMEM);
80     }
81 
82     s->last = s->last->next;
83     s->last->frame = frame;
84 
85     return 0;
86 }
87 
queue_pop(FifoContext * s)88 static void queue_pop(FifoContext *s)
89 {
90     Buf *tmp = s->root.next->next;
91     if (s->last == s->root.next)
92         s->last = &s->root;
93     av_freep(&s->root.next);
94     s->root.next = tmp;
95 }
96 
request_frame(AVFilterLink * outlink)97 static int request_frame(AVFilterLink *outlink)
98 {
99     FifoContext *s = outlink->src->priv;
100     int ret = 0;
101 
102     if (!s->root.next) {
103         if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
104             return ret;
105         if (!s->root.next)
106             return 0;
107     }
108     ret = ff_filter_frame(outlink, s->root.next->frame);
109     queue_pop(s);
110     return ret;
111 }
112 
113 static const AVFilterPad avfilter_vf_fifo_inputs[] = {
114     {
115         .name         = "default",
116         .type         = AVMEDIA_TYPE_VIDEO,
117         .filter_frame = add_to_queue,
118     },
119 };
120 
121 static const AVFilterPad avfilter_vf_fifo_outputs[] = {
122     {
123         .name          = "default",
124         .type          = AVMEDIA_TYPE_VIDEO,
125         .request_frame = request_frame,
126     },
127 };
128 
129 const AVFilter ff_vf_fifo = {
130     .name        = "fifo",
131     .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
132     .init        = init,
133     .uninit      = uninit,
134     .priv_size   = sizeof(FifoContext),
135     .flags       = AVFILTER_FLAG_METADATA_ONLY,
136     FILTER_INPUTS(avfilter_vf_fifo_inputs),
137     FILTER_OUTPUTS(avfilter_vf_fifo_outputs),
138 };
139 
140 static const AVFilterPad avfilter_af_afifo_inputs[] = {
141     {
142         .name         = "default",
143         .type         = AVMEDIA_TYPE_AUDIO,
144         .filter_frame = add_to_queue,
145     },
146 };
147 
148 static const AVFilterPad avfilter_af_afifo_outputs[] = {
149     {
150         .name          = "default",
151         .type          = AVMEDIA_TYPE_AUDIO,
152         .request_frame = request_frame,
153     },
154 };
155 
156 const AVFilter ff_af_afifo = {
157     .name        = "afifo",
158     .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
159     .init        = init,
160     .uninit      = uninit,
161     .priv_size   = sizeof(FifoContext),
162     .flags       = AVFILTER_FLAG_METADATA_ONLY,
163     FILTER_INPUTS(avfilter_af_afifo_inputs),
164     FILTER_OUTPUTS(avfilter_af_afifo_outputs),
165 };
166