FFmpeg
fifo.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * a very simple circular buffer FIFO implementation
22  */
23 
24 #ifndef AVUTIL_FIFO_H
25 #define AVUTIL_FIFO_H
26 
27 #include <stdint.h>
28 #include "avutil.h"
29 #include "attributes.h"
30 
31 typedef struct AVFifoBuffer {
32  uint8_t *buffer;
33  uint8_t *rptr, *wptr, *end;
34  uint32_t rndx, wndx;
35 } AVFifoBuffer;
36 
37 /**
38  * Initialize an AVFifoBuffer.
39  * @param size of FIFO
40  * @return AVFifoBuffer or NULL in case of memory allocation failure
41  */
42 AVFifoBuffer *av_fifo_alloc(unsigned int size);
43 
44 /**
45  * Initialize an AVFifoBuffer.
46  * @param nmemb number of elements
47  * @param size size of the single element
48  * @return AVFifoBuffer or NULL in case of memory allocation failure
49  */
50 AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
51 
52 /**
53  * Free an AVFifoBuffer.
54  * @param f AVFifoBuffer to free
55  */
57 
58 /**
59  * Free an AVFifoBuffer and reset pointer to NULL.
60  * @param f AVFifoBuffer to free
61  */
63 
64 /**
65  * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
66  * @param f AVFifoBuffer to reset
67  */
69 
70 /**
71  * Return the amount of data in bytes in the AVFifoBuffer, that is the
72  * amount of data you can read from it.
73  * @param f AVFifoBuffer to read from
74  * @return size
75  */
76 int av_fifo_size(const AVFifoBuffer *f);
77 
78 /**
79  * Return the amount of space in bytes in the AVFifoBuffer, that is the
80  * amount of data you can write into it.
81  * @param f AVFifoBuffer to write into
82  * @return size
83  */
84 int av_fifo_space(const AVFifoBuffer *f);
85 
86 /**
87  * Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
88  * Similar as av_fifo_gereric_read but without discarding data.
89  * @param f AVFifoBuffer to read from
90  * @param offset offset from current read position
91  * @param buf_size number of bytes to read
92  * @param func generic read function
93  * @param dest data destination
94  *
95  * @return a non-negative number on success, a negative error code on failure
96  */
97 int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void (*func)(void*, void*, int));
98 
99 /**
100  * Feed data from an AVFifoBuffer to a user-supplied callback.
101  * Similar as av_fifo_gereric_read but without discarding data.
102  * @param f AVFifoBuffer to read from
103  * @param buf_size number of bytes to read
104  * @param func generic read function
105  * @param dest data destination
106  *
107  * @return a non-negative number on success, a negative error code on failure
108  */
109 int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int));
110 
111 /**
112  * Feed data from an AVFifoBuffer to a user-supplied callback.
113  * @param f AVFifoBuffer to read from
114  * @param buf_size number of bytes to read
115  * @param func generic read function
116  * @param dest data destination
117  *
118  * @return a non-negative number on success, a negative error code on failure
119  */
120 int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int));
121 
122 /**
123  * Feed data from a user-supplied callback to an AVFifoBuffer.
124  * @param f AVFifoBuffer to write to
125  * @param src data source; non-const since it may be used as a
126  * modifiable context by the function defined in func
127  * @param size number of bytes to write
128  * @param func generic write function; the first parameter is src,
129  * the second is dest_buf, the third is dest_buf_size.
130  * func must return the number of bytes written to dest_buf, or <= 0 to
131  * indicate no more data available to write.
132  * If func is NULL, src is interpreted as a simple byte array for source data.
133  * @return the number of bytes written to the FIFO or a negative error code on failure
134  */
135 int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
136 
137 /**
138  * Resize an AVFifoBuffer.
139  * In case of reallocation failure, the old FIFO is kept unchanged.
140  *
141  * @param f AVFifoBuffer to resize
142  * @param size new AVFifoBuffer size in bytes
143  * @return <0 for failure, >=0 otherwise
144  */
145 int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
146 
147 /**
148  * Enlarge an AVFifoBuffer.
149  * In case of reallocation failure, the old FIFO is kept unchanged.
150  * The new fifo size may be larger than the requested size.
151  *
152  * @param f AVFifoBuffer to resize
153  * @param additional_space the amount of space in bytes to allocate in addition to av_fifo_size()
154  * @return <0 for failure, >=0 otherwise
155  */
156 int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space);
157 
158 /**
159  * Read and discard the specified amount of data from an AVFifoBuffer.
160  * @param f AVFifoBuffer to read from
161  * @param size amount of data to read in bytes
162  */
163 void av_fifo_drain(AVFifoBuffer *f, int size);
164 
165 #if FF_API_FIFO_PEEK2
166 /**
167  * Return a pointer to the data stored in a FIFO buffer at a certain offset.
168  * The FIFO buffer is not modified.
169  *
170  * @param f AVFifoBuffer to peek at, f must be non-NULL
171  * @param offs an offset in bytes, its absolute value must be less
172  * than the used buffer size or the returned pointer will
173  * point outside to the buffer data.
174  * The used buffer size can be checked with av_fifo_size().
175  * @deprecated use av_fifo_generic_peek_at()
176  */
178 static inline uint8_t *av_fifo_peek2(const AVFifoBuffer *f, int offs)
179 {
180  uint8_t *ptr = f->rptr + offs;
181  if (ptr >= f->end)
182  ptr = f->buffer + (ptr - f->end);
183  else if (ptr < f->buffer)
184  ptr = f->end - (f->buffer - ptr);
185  return ptr;
186 }
187 #endif
188 
189 #endif /* AVUTIL_FIFO_H */
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:68
av_fifo_generic_peek_at
int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void(*func)(void *, void *, int))
Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:161
av_fifo_realloc2
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size)
Resize an AVFifoBuffer.
Definition: fifo.c:81
AVFifoBuffer::wptr
uint8_t * wptr
Definition: fifo.h:33
av_fifo_generic_read
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:219
AVFifoBuffer
Definition: fifo.h:31
av_fifo_reset
void av_fifo_reset(AVFifoBuffer *f)
Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
Definition: fifo.c:65
av_fifo_size
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:71
av_fifo_generic_peek
int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:194
av_fifo_drain
void av_fifo_drain(AVFifoBuffer *f, int size)
Read and discard the specified amount of data from an AVFifoBuffer.
Definition: fifo.c:240
av_fifo_grow
int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space)
Enlarge an AVFifoBuffer.
Definition: fifo.c:115
f
#define f(width, name)
Definition: cbs_vp9.c:255
AVFifoBuffer::end
uint8_t * end
Definition: fifo.h:33
av_fifo_alloc_array
AVFifoBuffer * av_fifo_alloc_array(size_t nmemb, size_t size)
Initialize an AVFifoBuffer.
Definition: fifo.c:27
size
int size
Definition: twinvq_data.h:10344
attribute_deprecated
#define attribute_deprecated
Definition: attributes.h:104
av_fifo_generic_write
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:130
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
av_fifo_peek2
static attribute_deprecated uint8_t * av_fifo_peek2(const AVFifoBuffer *f, int offs)
Return a pointer to the data stored in a FIFO buffer at a certain offset.
Definition: fifo.h:178
AVFifoBuffer::buffer
uint8_t * buffer
Definition: fifo.h:32
AVFifoBuffer::wndx
uint32_t wndx
Definition: fifo.h:34
av_fifo_freep
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
Definition: fifo.c:57
av_fifo_free
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:49
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
avutil.h
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVFifoBuffer::rptr
uint8_t * rptr
Definition: fifo.h:33
av_fifo_alloc
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:44
AVFifoBuffer::rndx
uint32_t rndx
Definition: fifo.h:34
av_fifo_space
int av_fifo_space(const AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
Definition: fifo.c:76