Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
23 | #include <stdint.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | #include "avassert.h" | ||
27 | #include "error.h" | ||
28 | #include "fifo.h" | ||
29 | #include "macros.h" | ||
30 | #include "mem.h" | ||
31 | |||
32 | // by default the FIFO can be auto-grown to 1MB | ||
33 | #define AUTO_GROW_DEFAULT_BYTES (1024 * 1024) | ||
34 | |||
35 | struct AVFifo { | ||
36 | uint8_t *buffer; | ||
37 | |||
38 | size_t elem_size, nb_elems; | ||
39 | size_t offset_r, offset_w; | ||
40 | // distinguishes the ambiguous situation offset_r == offset_w | ||
41 | int is_empty; | ||
42 | |||
43 | unsigned int flags; | ||
44 | size_t auto_grow_limit; | ||
45 | }; | ||
46 | |||
47 | 86915 | AVFifo *av_fifo_alloc2(size_t nb_elems, size_t elem_size, | |
48 | unsigned int flags) | ||
49 | { | ||
50 | AVFifo *f; | ||
51 | 86915 | void *buffer = NULL; | |
52 | |||
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86915 times.
|
86915 | if (!elem_size) |
54 | ✗ | return NULL; | |
55 | |||
56 |
1/2✓ Branch 0 taken 86915 times.
✗ Branch 1 not taken.
|
86915 | if (nb_elems) { |
57 | 86915 | buffer = av_realloc_array(NULL, nb_elems, elem_size); | |
58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86915 times.
|
86915 | if (!buffer) |
59 | ✗ | return NULL; | |
60 | } | ||
61 | 86915 | f = av_mallocz(sizeof(*f)); | |
62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86915 times.
|
86915 | if (!f) { |
63 | ✗ | av_free(buffer); | |
64 | ✗ | return NULL; | |
65 | } | ||
66 | 86915 | f->buffer = buffer; | |
67 | 86915 | f->nb_elems = nb_elems; | |
68 | 86915 | f->elem_size = elem_size; | |
69 | 86915 | f->is_empty = 1; | |
70 | |||
71 | 86915 | f->flags = flags; | |
72 | 86915 | f->auto_grow_limit = FFMAX(AUTO_GROW_DEFAULT_BYTES / elem_size, 1); | |
73 | |||
74 | 86915 | return f; | |
75 | } | ||
76 | |||
77 | ✗ | void av_fifo_auto_grow_limit(AVFifo *f, size_t max_elems) | |
78 | { | ||
79 | ✗ | f->auto_grow_limit = max_elems; | |
80 | ✗ | } | |
81 | |||
82 | 1 | size_t av_fifo_elem_size(const AVFifo *f) | |
83 | { | ||
84 | 1 | return f->elem_size; | |
85 | } | ||
86 | |||
87 | 20069424 | size_t av_fifo_can_read(const AVFifo *f) | |
88 | { | ||
89 |
4/4✓ Branch 0 taken 13877641 times.
✓ Branch 1 taken 6191783 times.
✓ Branch 2 taken 7161808 times.
✓ Branch 3 taken 6715833 times.
|
20069424 | if (f->offset_w <= f->offset_r && !f->is_empty) |
90 | 7161808 | return f->nb_elems - f->offset_r + f->offset_w; | |
91 | 12907616 | return f->offset_w - f->offset_r; | |
92 | } | ||
93 | |||
94 | 5694771 | size_t av_fifo_can_write(const AVFifo *f) | |
95 | { | ||
96 | 5694771 | return f->nb_elems - av_fifo_can_read(f); | |
97 | } | ||
98 | |||
99 | 59312 | int av_fifo_grow2(AVFifo *f, size_t inc) | |
100 | { | ||
101 | uint8_t *tmp; | ||
102 | |||
103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59312 times.
|
59312 | if (inc > SIZE_MAX - f->nb_elems) |
104 | ✗ | return AVERROR(EINVAL); | |
105 | |||
106 | 59312 | tmp = av_realloc_array(f->buffer, f->nb_elems + inc, f->elem_size); | |
107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59312 times.
|
59312 | if (!tmp) |
108 | ✗ | return AVERROR(ENOMEM); | |
109 | 59312 | f->buffer = tmp; | |
110 | |||
111 | // move the data from the beginning of the ring buffer | ||
112 | // to the newly allocated space | ||
113 |
4/4✓ Branch 0 taken 59305 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 59287 times.
✓ Branch 3 taken 18 times.
|
59312 | if (f->offset_w <= f->offset_r && !f->is_empty) { |
114 | 59287 | const size_t copy = FFMIN(inc, f->offset_w); | |
115 | 59287 | memcpy(tmp + f->nb_elems * f->elem_size, tmp, copy * f->elem_size); | |
116 |
2/2✓ Branch 0 taken 640 times.
✓ Branch 1 taken 58647 times.
|
59287 | if (copy < f->offset_w) { |
117 | 640 | memmove(tmp, tmp + copy * f->elem_size, | |
118 | 640 | (f->offset_w - copy) * f->elem_size); | |
119 | 640 | f->offset_w -= copy; | |
120 | } else | ||
121 |
2/2✓ Branch 0 taken 57812 times.
✓ Branch 1 taken 835 times.
|
58647 | f->offset_w = copy == inc ? 0 : f->nb_elems + copy; |
122 | } | ||
123 | |||
124 | 59312 | f->nb_elems += inc; | |
125 | |||
126 | 59312 | return 0; | |
127 | } | ||
128 | |||
129 | 3533189 | static int fifo_check_space(AVFifo *f, size_t to_write) | |
130 | { | ||
131 | 3533189 | const size_t can_write = av_fifo_can_write(f); | |
132 |
2/2✓ Branch 0 taken 58967 times.
✓ Branch 1 taken 3474222 times.
|
3533189 | const size_t need_grow = to_write > can_write ? to_write - can_write : 0; |
133 | size_t can_grow; | ||
134 | |||
135 |
2/2✓ Branch 0 taken 3474222 times.
✓ Branch 1 taken 58967 times.
|
3533189 | if (!need_grow) |
136 | 3474222 | return 0; | |
137 | |||
138 | 117934 | can_grow = f->auto_grow_limit > f->nb_elems ? | |
139 |
1/2✓ Branch 0 taken 58967 times.
✗ Branch 1 not taken.
|
58967 | f->auto_grow_limit - f->nb_elems : 0; |
140 |
2/4✓ Branch 0 taken 58967 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 58967 times.
✗ Branch 3 not taken.
|
58967 | if ((f->flags & AV_FIFO_FLAG_AUTO_GROW) && need_grow <= can_grow) { |
141 | // allocate a bit more than necessary, if we can | ||
142 |
1/2✓ Branch 0 taken 58967 times.
✗ Branch 1 not taken.
|
58967 | const size_t inc = (need_grow < can_grow / 2 ) ? need_grow * 2 : can_grow; |
143 | 58967 | return av_fifo_grow2(f, inc); | |
144 | } | ||
145 | |||
146 | ✗ | return AVERROR(ENOSPC); | |
147 | } | ||
148 | |||
149 | 3533189 | static int fifo_write_common(AVFifo *f, const uint8_t *buf, size_t *nb_elems, | |
150 | AVFifoCB read_cb, void *opaque) | ||
151 | { | ||
152 | 3533189 | size_t to_write = *nb_elems; | |
153 | size_t offset_w; | ||
154 | 3533189 | int ret = 0; | |
155 | |||
156 | 3533189 | ret = fifo_check_space(f, to_write); | |
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3533189 times.
|
3533189 | if (ret < 0) |
158 | ✗ | return ret; | |
159 | |||
160 | 3533189 | offset_w = f->offset_w; | |
161 | |||
162 |
2/2✓ Branch 0 taken 3527951 times.
✓ Branch 1 taken 3533157 times.
|
7061108 | while (to_write > 0) { |
163 | 3527951 | size_t len = FFMIN(f->nb_elems - offset_w, to_write); | |
164 | 3527951 | uint8_t *wptr = f->buffer + offset_w * f->elem_size; | |
165 | |||
166 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 3527884 times.
|
3527951 | if (read_cb) { |
167 | 67 | ret = read_cb(opaque, wptr, &len); | |
168 |
3/4✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 32 times.
|
67 | if (ret < 0 || len == 0) |
169 | break; | ||
170 | } else { | ||
171 | 3527884 | memcpy(wptr, buf, len * f->elem_size); | |
172 | 3527884 | buf += len * f->elem_size; | |
173 | } | ||
174 | 3527919 | offset_w += len; | |
175 |
2/2✓ Branch 0 taken 707061 times.
✓ Branch 1 taken 2820858 times.
|
3527919 | if (offset_w >= f->nb_elems) |
176 | 707061 | offset_w = 0; | |
177 | 3527919 | to_write -= len; | |
178 | } | ||
179 | 3533189 | f->offset_w = offset_w; | |
180 | |||
181 |
2/2✓ Branch 0 taken 3523209 times.
✓ Branch 1 taken 9980 times.
|
3533189 | if (*nb_elems != to_write) |
182 | 3523209 | f->is_empty = 0; | |
183 | 3533189 | *nb_elems -= to_write; | |
184 | |||
185 | 3533189 | return ret; | |
186 | } | ||
187 | |||
188 | 3533157 | int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems) | |
189 | { | ||
190 | 3533157 | return fifo_write_common(f, buf, &nb_elems, NULL, NULL); | |
191 | } | ||
192 | |||
193 | 32 | int av_fifo_write_from_cb(AVFifo *f, AVFifoCB read_cb, | |
194 | void *opaque, size_t *nb_elems) | ||
195 | { | ||
196 | 32 | return fifo_write_common(f, NULL, nb_elems, read_cb, opaque); | |
197 | } | ||
198 | |||
199 | 4848644 | static int fifo_peek_common(const AVFifo *f, uint8_t *buf, size_t *nb_elems, | |
200 | size_t offset, AVFifoCB write_cb, void *opaque) | ||
201 | { | ||
202 | 4848644 | size_t to_read = *nb_elems; | |
203 | 4848644 | size_t offset_r = f->offset_r; | |
204 | 4848644 | size_t can_read = av_fifo_can_read(f); | |
205 | 4848644 | int ret = 0; | |
206 | |||
207 |
3/4✓ Branch 0 taken 4848644 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 900370 times.
✓ Branch 3 taken 3948274 times.
|
4848644 | if (offset > can_read || to_read > can_read - offset) { |
208 | 900370 | *nb_elems = 0; | |
209 | 900370 | return AVERROR(EINVAL); | |
210 | } | ||
211 | |||
212 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3948270 times.
|
3948274 | if (offset_r >= f->nb_elems - offset) |
213 | 4 | offset_r -= f->nb_elems - offset; | |
214 | else | ||
215 | 3948270 | offset_r += offset; | |
216 | |||
217 |
2/2✓ Branch 0 taken 3953070 times.
✓ Branch 1 taken 3948211 times.
|
7901281 | while (to_read > 0) { |
218 | 3953070 | size_t len = FFMIN(f->nb_elems - offset_r, to_read); | |
219 | 3953070 | uint8_t *rptr = f->buffer + offset_r * f->elem_size; | |
220 | |||
221 |
2/2✓ Branch 0 taken 13023 times.
✓ Branch 1 taken 3940047 times.
|
3953070 | if (write_cb) { |
222 | 13023 | ret = write_cb(opaque, rptr, &len); | |
223 |
3/4✓ Branch 0 taken 13023 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12960 times.
✓ Branch 3 taken 63 times.
|
13023 | if (ret < 0 || len == 0) |
224 | break; | ||
225 | } else { | ||
226 | 3940047 | memcpy(buf, rptr, len * f->elem_size); | |
227 | 3940047 | buf += len * f->elem_size; | |
228 | } | ||
229 | 3953007 | offset_r += len; | |
230 |
2/2✓ Branch 0 taken 765609 times.
✓ Branch 1 taken 3187398 times.
|
3953007 | if (offset_r >= f->nb_elems) |
231 | 765609 | offset_r = 0; | |
232 | 3953007 | to_read -= len; | |
233 | } | ||
234 | |||
235 | 3948274 | *nb_elems -= to_read; | |
236 | |||
237 | 3948274 | return ret; | |
238 | } | ||
239 | |||
240 | 4322660 | int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems) | |
241 | { | ||
242 | 4322660 | int ret = fifo_peek_common(f, buf, &nb_elems, 0, NULL, NULL); | |
243 | 4322660 | av_fifo_drain2(f, nb_elems); | |
244 | 4322660 | return ret; | |
245 | } | ||
246 | |||
247 | 8196 | int av_fifo_read_to_cb(AVFifo *f, AVFifoCB write_cb, | |
248 | void *opaque, size_t *nb_elems) | ||
249 | { | ||
250 | 8196 | int ret = fifo_peek_common(f, NULL, nb_elems, 0, write_cb, opaque); | |
251 | 8196 | av_fifo_drain2(f, *nb_elems); | |
252 | 8196 | return ret; | |
253 | } | ||
254 | |||
255 | 517757 | int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset) | |
256 | { | ||
257 | 517757 | return fifo_peek_common(f, buf, &nb_elems, offset, NULL, NULL); | |
258 | } | ||
259 | |||
260 | 31 | int av_fifo_peek_to_cb(const AVFifo *f, AVFifoCB write_cb, void *opaque, | |
261 | size_t *nb_elems, size_t offset) | ||
262 | { | ||
263 | 31 | return fifo_peek_common(f, NULL, nb_elems, offset, write_cb, opaque); | |
264 | } | ||
265 | |||
266 | 4330946 | void av_fifo_drain2(AVFifo *f, size_t size) | |
267 | { | ||
268 | 4330946 | const size_t cur_size = av_fifo_can_read(f); | |
269 | |||
270 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4330946 times.
|
4330946 | av_assert0(cur_size >= size); |
271 |
2/2✓ Branch 0 taken 2518752 times.
✓ Branch 1 taken 1812194 times.
|
4330946 | if (cur_size == size) |
272 | 2518752 | f->is_empty = 1; | |
273 | |||
274 |
2/2✓ Branch 0 taken 642478 times.
✓ Branch 1 taken 3688468 times.
|
4330946 | if (f->offset_r >= f->nb_elems - size) |
275 | 642478 | f->offset_r -= f->nb_elems - size; | |
276 | else | ||
277 | 3688468 | f->offset_r += size; | |
278 | 4330946 | } | |
279 | |||
280 | ✗ | void av_fifo_reset2(AVFifo *f) | |
281 | { | ||
282 | ✗ | f->offset_r = f->offset_w = 0; | |
283 | ✗ | f->is_empty = 1; | |
284 | ✗ | } | |
285 | |||
286 | 87193 | void av_fifo_freep2(AVFifo **f) | |
287 | { | ||
288 |
2/2✓ Branch 0 taken 86915 times.
✓ Branch 1 taken 278 times.
|
87193 | if (*f) { |
289 | 86915 | av_freep(&(*f)->buffer); | |
290 | 86915 | av_freep(f); | |
291 | } | ||
292 | 87193 | } | |
293 |