1 /*
2 * a very simple circular buffer FIFO implementation
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2006 Roman Shaposhnik
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
26
28 {
30 if (!buffer)
33 if (!f) {
36 }
40 return f;
41 }
42
44 {
47 }
48
50 {
53 }
54
56 {
57 if (f) {
60 }
61 }
62
64 {
65 if (f) {
68 }
69 }
70
72 {
75 }
76
78 {
79 return (uint32_t)(f->
wndx - f->
rndx);
80 }
81
83 {
85 }
86
88 {
89 unsigned int old_size = f->
end - f->
buffer;
90
91 if (old_size < new_size) {
94
95 if (!f2)
101 *f = *f2;
103 }
104 return 0;
105 }
106
108 {
109 unsigned int old_size = f->
end - f->
buffer;
112
114
115 if (old_size < size)
117 return 0;
118 }
119
120 /* src must NOT be const as it can be a context for func that may need
121 * updating (like a pointer or byte counter) */
123 int (*
func)(
void *,
void *,
int))
124 {
126 uint32_t wndx= f->
wndx;
128
129 do {
132 len =
func(src, wptr, len);
133 if (len <= 0)
134 break;
135 } else {
136 memcpy(wptr, src, len);
138 }
139 // Write memory barrier needed for SMP here in theory
145 } while (size > 0);
149 }
150
152 {
154
156
157 /*
158 * *ndx are indexes modulo 2^32, they are intended to overflow,
159 * to handle *ndx greater than 4gb.
160 */
162
163 if (offset >= f->
end - rptr)
165 else
167
168 while (buf_size > 0) {
170
173
174 len =
FFMIN(f->
end - rptr, buf_size);
176 func(dest, rptr, len);
177 else {
178 memcpy(dest, rptr, len);
180 }
181
184 }
185
186 return 0;
187 }
188
190 void (*
func)(
void *,
void *,
int))
191 {
192 // Read memory barrier needed for SMP here in theory
194
195 do {
198 func(dest, rptr, len);
199 else {
200 memcpy(dest, rptr, len);
202 }
203 // memory barrier needed for SMP here in theory
208 } while (buf_size > 0);
209
210 return 0;
211 }
212
214 void (*
func)(
void *,
void *,
int))
215 {
216 // Read memory barrier needed for SMP here in theory
217 do {
221 else {
222 memcpy(dest, f->
rptr, len);
224 }
225 // memory barrier needed for SMP here in theory
228 } while (buf_size > 0);
229 return 0;
230 }
231
232 /** Discard data from the FIFO. */
234 {
240 }
241
242 #ifdef TEST
243
245 {
246 /* create a FIFO buffer */
249
250 /* fill data */
253
254 /* peek at FIFO */
256 for (i = -n + 1; i <
n; i++) {
258 printf("%d: %d\n", i, *v);
259 }
260 printf("\n");
261
262 /* peek_at at FIFO */
264 for (i = 0; i <
n; i++) {
266 printf("%d: %d\n", i, j);
267 }
268 printf("\n");
269
270 /* read data */
273 printf("%d ", j);
274 }
275 printf("\n");
276
277 /* test *ndx overflow */
279 fifo->
rndx = fifo->
wndx = ~(uint32_t)0 - 5;
280
281 /* fill data */
284
285 /* peek_at at FIFO */
287 for (i = 0; i <
n; i++) {
289 printf("%d: %d\n", i, j);
290 }
291
293
294 return 0;
295 }
296
297 #endif
int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
Enlarge an AVFifoBuffer.
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.
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.
static AVFifoBuffer * fifo_alloc_common(void *buffer, size_t size)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
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...
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
static 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.
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.
simple assert() macros that are a bit more flexible than ISO C assert().
static const uint8_t offset[127][2]
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 ...
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.
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
a very simple circular buffer FIFO implementation
int(* func)(AVBPrint *dst, const char *in, const char *arg)
AVFifoBuffer * av_fifo_alloc_array(size_t nmemb, size_t size)
Initialize an AVFifoBuffer.
common internal and external API header
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
#define av_malloc_array(a, b)
int main(int argc, char **argv)
void av_fifo_reset(AVFifoBuffer *f)
Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied...
void av_fifo_drain(AVFifoBuffer *f, int size)
Discard data from the FIFO.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...