FFmpeg
fifo.c
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include "libavutil/common.h"
22 #include "libavutil/fifo.h"
23 #include "libavutil/lfg.h"
24 #include "libavutil/random_seed.h"
25 
26 typedef struct CBState {
27  unsigned int read_idx;
28  unsigned int write_idx;
29  unsigned int to_process;
30  unsigned int offset;
31 } CBState;
32 
33 static int read_cb(void *opaque, void *buf, size_t *nb_elems)
34 {
35  CBState *s = opaque;
36  unsigned *b = buf;
37 
38  *nb_elems = FFMIN(*nb_elems, s->to_process);
39 
40  for (unsigned i = 0; i < *nb_elems; i++)
41  if (b[i] != s->read_idx + s->offset + i) {
42  printf("Mismatch at idx %u offset %u i %u\n",
43  s->read_idx, s->offset, i);
44  return AVERROR_BUG;
45  }
46 
47  s->offset += *nb_elems;
48  s->to_process -= *nb_elems;
49 
50  return 0;
51 }
52 
53 static int write_cb(void *opaque, void *buf, size_t *nb_elems)
54 {
55  CBState *s = opaque;
56  unsigned *b = buf;
57 
58  *nb_elems = FFMIN(*nb_elems, s->to_process);
59 
60  for (unsigned i = 0; i < *nb_elems; i++)
61  b[i] = s->write_idx + i;
62 
63  s->write_idx += *nb_elems;
64  s->to_process -= *nb_elems;
65 
66  return 0;
67 }
68 
69 int main(void)
70 {
71  /* create a FIFO buffer */
72  AVFifo *fifo = av_fifo_alloc2(13, sizeof(int), 0);
73  int i, j, n, *p;
74 
75  /* fill data */
76  for (i = 0; av_fifo_can_write(fifo); i++)
77  av_fifo_write(fifo, &i, 1);
78 
79  /* peek_at at FIFO */
80  n = av_fifo_can_read(fifo);
81  for (i = 0; i < n; i++) {
82  av_fifo_peek(fifo, &j, 1, i);
83  printf("%d: %d\n", i, j);
84  }
85  printf("\n");
86 
87  /* generic peek at FIFO */
88 
89  n = av_fifo_can_read(fifo);
90  p = malloc(n * av_fifo_elem_size(fifo));
91  if (p == NULL) {
92  fprintf(stderr, "failed to allocate memory.\n");
93  exit(1);
94  }
95 
96  (void) av_fifo_peek(fifo, p, n, 0);
97 
98  /* read data at p */
99  for(i = 0; i < n; ++i)
100  printf("%d: %d\n", i, p[i]);
101 
102  putchar('\n');
103 
104  /* read data */
105  for (i = 0; av_fifo_can_read(fifo); i++) {
106  av_fifo_read(fifo, &j, 1);
107  printf("%d ", j);
108  }
109  printf("\n");
110 
111  /* fill data */
112  for (i = 0; av_fifo_can_write(fifo); i++)
113  av_fifo_write(fifo, &i, 1);
114 
115  /* peek_at at FIFO */
116  n = av_fifo_can_read(fifo);
117  for (i = 0; i < n; i++) {
118  av_fifo_peek(fifo, &j, 1, i);
119  printf("%d: %d\n", i, j);
120  }
121  putchar('\n');
122 
123  /* test fifo_grow */
124  (void) av_fifo_grow2(fifo, 15);
125 
126  /* fill data */
127  n = av_fifo_can_read(fifo);
128  for (i = n; av_fifo_can_write(fifo); ++i)
129  av_fifo_write(fifo, &i, 1);
130 
131  /* peek_at at FIFO */
132  n = av_fifo_can_read(fifo);
133  for (i = 0; i < n; i++) {
134  av_fifo_peek(fifo, &j, 1, i);
135  printf("%d: %d\n", i, j);
136  }
137 
138  av_fifo_freep2(&fifo);
139 
140  /* test randomly-sized write/read/peek with a callback */
141  {
142  CBState s = { 0 };
143  uint32_t seed = av_get_random_seed();
144 
145  AVLFG lfg;
146  int ret;
147 
148  av_lfg_init(&lfg, seed);
149 
150  fifo = av_fifo_alloc2(1, sizeof(unsigned), AV_FIFO_FLAG_AUTO_GROW);
151 
152  for (i = 0; i < 32; i++) {
153  size_t nb_elems = 16;
154  unsigned to_process = av_lfg_get(&lfg) % nb_elems;
155 
156  s.to_process = to_process;
157 
158  ret = av_fifo_write_from_cb(fifo, write_cb, &s, &nb_elems);
159  if (ret < 0 || s.to_process || nb_elems != to_process) {
160  printf("FIFO write fail; seed %"PRIu32"\n", seed);
161  return 1;
162  }
163 
164  nb_elems = av_fifo_can_read(fifo);
165  if (nb_elems > 1) {
166  s.offset = av_lfg_get(&lfg) % (nb_elems - 1);
167  nb_elems -= s.offset;
168 
169  s.to_process = av_lfg_get(&lfg) % nb_elems;
170  to_process = s.to_process;
171 
172  ret = av_fifo_peek_to_cb(fifo, read_cb, &s, &nb_elems, s.offset);
173  if (ret < 0 || s.to_process || nb_elems != to_process) {
174  printf("FIFO peek fail; seed %"PRIu32"\n", seed);
175  return 1;
176  }
177  }
178 
179  nb_elems = av_fifo_can_read(fifo);
180  to_process = nb_elems ? av_lfg_get(&lfg) % nb_elems : 0;
181  s.to_process = to_process;
182  s.offset = 0;
183 
184  ret = av_fifo_read_to_cb(fifo, read_cb, &s, &nb_elems);
185  if (ret < 0 || s.to_process || to_process != nb_elems) {
186  printf("FIFO read fail; seed %"PRIu32"\n", seed);
187  return 1;
188  }
189  s.read_idx += s.offset;
190  }
191  }
192 
193  av_fifo_freep2(&fifo);
194  free(p);
195 
196  return 0;
197 }
av_fifo_can_write
size_t av_fifo_can_write(const AVFifo *f)
Definition: fifo.c:94
av_fifo_peek_to_cb
int av_fifo_peek_to_cb(const AVFifo *f, AVFifoCB write_cb, void *opaque, size_t *nb_elems, size_t offset)
Feed data from a FIFO into a user-provided callback.
Definition: fifo.c:260
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
av_fifo_peek
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
Definition: fifo.c:255
b
#define b
Definition: input.c:41
CBState::to_process
unsigned int to_process
Definition: fifo.c:29
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:167
fifo.h
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
av_fifo_grow2
int av_fifo_grow2(AVFifo *f, size_t inc)
Enlarge an AVFifo.
Definition: fifo.c:99
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
CBState::read_idx
unsigned int read_idx
Definition: fifo.c:27
lfg.h
NULL
#define NULL
Definition: coverity.c:32
av_fifo_write_from_cb
int av_fifo_write_from_cb(AVFifo *f, AVFifoCB read_cb, void *opaque, size_t *nb_elems)
Write data from a user-provided callback into a FIFO.
Definition: fifo.c:193
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
seed
static unsigned int seed
Definition: videogen.c:78
av_fifo_elem_size
size_t av_fifo_elem_size(const AVFifo *f)
Definition: fifo.c:82
CBState::offset
unsigned int offset
Definition: fifo.c:30
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
av_fifo_read_to_cb
int av_fifo_read_to_cb(AVFifo *f, AVFifoCB write_cb, void *opaque, size_t *nb_elems)
Feed data from a FIFO into a user-provided callback.
Definition: fifo.c:247
AVFifo
Definition: fifo.c:35
read_cb
static int read_cb(void *opaque, void *buf, size_t *nb_elems)
Definition: fifo.c:33
printf
printf("static const uint8_t my_array[100] = {\n")
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
main
int main(void)
Definition: fifo.c:69
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ret
ret
Definition: filter_design.txt:187
av_fifo_alloc2
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition: fifo.c:47
random_seed.h
CBState::write_idx
unsigned int write_idx
Definition: fifo.c:28
write_cb
static int write_cb(void *opaque, void *buf, size_t *nb_elems)
Definition: fifo.c:53
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
CBState
Definition: fifo.c:26
av_fifo_freep2
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition: fifo.c:286
AV_FIFO_FLAG_AUTO_GROW
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Definition: fifo.h:67