FFmpeg
fifo.c
Go to the documentation of this file.
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 {
35  struct Buf *next;
36 } Buf;
37 
38 typedef struct FifoContext {
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  */
47  int allocated_samples; ///< number of samples out was allocated for
48 } FifoContext;
49 
51 {
52  FifoContext *s = ctx->priv;
53  s->last = &s->root;
54 
55  return 0;
56 }
57 
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 
73 {
74  FifoContext *s = inlink->dst->priv;
75 
76  s->last->next = av_mallocz(sizeof(Buf));
77  if (!s->last->next) {
79  return AVERROR(ENOMEM);
80  }
81 
82  s->last = s->last->next;
83  s->last->frame = frame;
84 
85  return 0;
86 }
87 
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 
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 
114  {
115  .name = "default",
116  .type = AVMEDIA_TYPE_VIDEO,
117  .filter_frame = add_to_queue,
118  },
119  { NULL }
120 };
121 
123  {
124  .name = "default",
125  .type = AVMEDIA_TYPE_VIDEO,
126  .request_frame = request_frame,
127  },
128  { NULL }
129 };
130 
132  .name = "fifo",
133  .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
134  .init = init,
135  .uninit = uninit,
136  .priv_size = sizeof(FifoContext),
139 };
140 
142  {
143  .name = "default",
144  .type = AVMEDIA_TYPE_AUDIO,
145  .filter_frame = add_to_queue,
146  },
147  { NULL }
148 };
149 
151  {
152  .name = "default",
153  .type = AVMEDIA_TYPE_AUDIO,
154  .request_frame = request_frame,
155  },
156  { NULL }
157 };
158 
160  .name = "afifo",
161  .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
162  .init = init,
163  .uninit = uninit,
164  .priv_size = sizeof(FifoContext),
167 };
avfilter_af_afifo_outputs
static const AVFilterPad avfilter_af_afifo_outputs[]
Definition: fifo.c:150
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: fifo.c:58
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
FifoContext::root
Buf root
Definition: fifo.c:39
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
FifoContext
Definition: fifo.c:38
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
Buf::frame
AVFrame * frame
Definition: fifo.c:34
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:27
ff_request_frame
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:395
avfilter_vf_fifo_inputs
static const AVFilterPad avfilter_vf_fifo_inputs[]
Definition: fifo.c:113
mathematics.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
avfilter_vf_fifo_outputs
static const AVFilterPad avfilter_vf_fifo_outputs[]
Definition: fifo.c:122
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:348
queue_pop
static void queue_pop(FifoContext *s)
Definition: fifo.c:88
FifoContext::out
AVFrame * out
When a specific number of output samples is requested, the partial buffer is stored here.
Definition: fifo.c:46
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
FifoContext::allocated_samples
int allocated_samples
number of samples out was allocated for
Definition: fifo.c:47
av_cold
#define av_cold
Definition: attributes.h:90
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
Buf::next
struct Buf * next
Definition: fifo.c:35
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
NULL
#define NULL
Definition: coverity.c:32
avfilter_af_afifo_inputs
static const AVFilterPad avfilter_af_afifo_inputs[]
Definition: fifo.c:141
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:341
Buf
Definition: fifo.c:33
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: fifo.c:97
add_to_queue
static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
Definition: fifo.c:72
internal.h
common.h
ff_vf_fifo
const AVFilter ff_vf_fifo
Definition: fifo.c:131
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:243
ff_af_afifo
const AVFilter ff_af_afifo
Definition: fifo.c:159
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
FifoContext::last
Buf * last
last buffered frame
Definition: fifo.c:40
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
audio.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
init
static av_cold int init(AVFilterContext *ctx)
Definition: fifo.c:50