| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | 124 | static int read_cb(void *opaque, void *buf, size_t *nb_elems) | |
| 34 | { | ||
| 35 | 124 | CBState *s = opaque; | |
| 36 | 124 | unsigned *b = buf; | |
| 37 | |||
| 38 | 124 | *nb_elems = FFMIN(*nb_elems, s->to_process); | |
| 39 | |||
| 40 |
2/2✓ Branch 0 taken 375 times.
✓ Branch 1 taken 124 times.
|
499 | for (unsigned i = 0; i < *nb_elems; i++) |
| 41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
|
375 | 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 | 124 | s->offset += *nb_elems; | |
| 48 | 124 | s->to_process -= *nb_elems; | |
| 49 | |||
| 50 | 124 | return 0; | |
| 51 | } | ||
| 52 | |||
| 53 | 68 | static int write_cb(void *opaque, void *buf, size_t *nb_elems) | |
| 54 | { | ||
| 55 | 68 | CBState *s = opaque; | |
| 56 | 68 | unsigned *b = buf; | |
| 57 | |||
| 58 | 68 | *nb_elems = FFMIN(*nb_elems, s->to_process); | |
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 235 times.
✓ Branch 1 taken 68 times.
|
303 | for (unsigned i = 0; i < *nb_elems; i++) |
| 61 | 235 | b[i] = s->write_idx + i; | |
| 62 | |||
| 63 | 68 | s->write_idx += *nb_elems; | |
| 64 | 68 | s->to_process -= *nb_elems; | |
| 65 | |||
| 66 | 68 | return 0; | |
| 67 | } | ||
| 68 | |||
| 69 | 1 | int main(void) | |
| 70 | { | ||
| 71 | /* create a FIFO buffer */ | ||
| 72 | 1 | AVFifo *fifo = av_fifo_alloc2(13, sizeof(int), 0); | |
| 73 | int i, j, n, *p; | ||
| 74 | |||
| 75 | /* fill data */ | ||
| 76 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1 times.
|
14 | for (i = 0; av_fifo_can_write(fifo); i++) |
| 77 | 13 | av_fifo_write(fifo, &i, 1); | |
| 78 | |||
| 79 | /* peek_at at FIFO */ | ||
| 80 | 1 | n = av_fifo_can_read(fifo); | |
| 81 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
|
14 | for (i = 0; i < n; i++) { |
| 82 | 13 | av_fifo_peek(fifo, &j, 1, i); | |
| 83 | 13 | printf("%d: %d\n", i, j); | |
| 84 | } | ||
| 85 | 1 | printf("\n"); | |
| 86 | |||
| 87 | /* generic peek at FIFO */ | ||
| 88 | |||
| 89 | 1 | n = av_fifo_can_read(fifo); | |
| 90 | 1 | p = malloc(n * av_fifo_elem_size(fifo)); | |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (p == NULL) { |
| 92 | ✗ | fprintf(stderr, "failed to allocate memory.\n"); | |
| 93 | ✗ | exit(1); | |
| 94 | } | ||
| 95 | |||
| 96 | 1 | (void) av_fifo_peek(fifo, p, n, 0); | |
| 97 | |||
| 98 | /* read data at p */ | ||
| 99 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
|
14 | for(i = 0; i < n; ++i) |
| 100 | 13 | printf("%d: %d\n", i, p[i]); | |
| 101 | |||
| 102 | 1 | putchar('\n'); | |
| 103 | |||
| 104 | /* read data */ | ||
| 105 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1 times.
|
14 | for (i = 0; av_fifo_can_read(fifo); i++) { |
| 106 | 13 | av_fifo_read(fifo, &j, 1); | |
| 107 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
|
13 | printf(i == 0 ? "%d" : " %d", j); |
| 108 | } | ||
| 109 | 1 | printf("\n"); | |
| 110 | |||
| 111 | /* fill data */ | ||
| 112 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1 times.
|
14 | for (i = 0; av_fifo_can_write(fifo); i++) |
| 113 | 13 | av_fifo_write(fifo, &i, 1); | |
| 114 | |||
| 115 | /* peek_at at FIFO */ | ||
| 116 | 1 | n = av_fifo_can_read(fifo); | |
| 117 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
|
14 | for (i = 0; i < n; i++) { |
| 118 | 13 | av_fifo_peek(fifo, &j, 1, i); | |
| 119 | 13 | printf("%d: %d\n", i, j); | |
| 120 | } | ||
| 121 | 1 | putchar('\n'); | |
| 122 | |||
| 123 | /* test fifo_grow */ | ||
| 124 | 1 | (void) av_fifo_grow2(fifo, 15); | |
| 125 | |||
| 126 | /* fill data */ | ||
| 127 | 1 | n = av_fifo_can_read(fifo); | |
| 128 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1 times.
|
16 | for (i = n; av_fifo_can_write(fifo); ++i) |
| 129 | 15 | av_fifo_write(fifo, &i, 1); | |
| 130 | |||
| 131 | /* peek_at at FIFO */ | ||
| 132 | 1 | n = av_fifo_can_read(fifo); | |
| 133 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1 times.
|
29 | for (i = 0; i < n; i++) { |
| 134 | 28 | av_fifo_peek(fifo, &j, 1, i); | |
| 135 | 28 | printf("%d: %d\n", i, j); | |
| 136 | } | ||
| 137 | |||
| 138 | 1 | av_fifo_freep2(&fifo); | |
| 139 | |||
| 140 | /* test randomly-sized write/read/peek with a callback */ | ||
| 141 | { | ||
| 142 | 1 | CBState s = { 0 }; | |
| 143 | 1 | uint32_t seed = av_get_random_seed(); | |
| 144 | |||
| 145 | AVLFG lfg; | ||
| 146 | int ret; | ||
| 147 | |||
| 148 | 1 | av_lfg_init(&lfg, seed); | |
| 149 | |||
| 150 | 1 | fifo = av_fifo_alloc2(1, sizeof(unsigned), AV_FIFO_FLAG_AUTO_GROW); | |
| 151 | |||
| 152 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (i = 0; i < 32; i++) { |
| 153 | 32 | size_t nb_elems = 16; | |
| 154 | 32 | unsigned to_process = av_lfg_get(&lfg) % nb_elems; | |
| 155 | |||
| 156 | 32 | s.to_process = to_process; | |
| 157 | |||
| 158 | 32 | ret = av_fifo_write_from_cb(fifo, write_cb, &s, &nb_elems); | |
| 159 |
3/6✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 32 times.
|
32 | 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 | 32 | nb_elems = av_fifo_can_read(fifo); | |
| 165 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
|
32 | if (nb_elems > 1) { |
| 166 | 31 | s.offset = av_lfg_get(&lfg) % (nb_elems - 1); | |
| 167 | 31 | nb_elems -= s.offset; | |
| 168 | |||
| 169 | 31 | s.to_process = av_lfg_get(&lfg) % nb_elems; | |
| 170 | 31 | to_process = s.to_process; | |
| 171 | |||
| 172 | 31 | ret = av_fifo_peek_to_cb(fifo, read_cb, &s, &nb_elems, s.offset); | |
| 173 |
3/6✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 31 times.
|
31 | 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 | 32 | nb_elems = av_fifo_can_read(fifo); | |
| 180 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | to_process = nb_elems ? av_lfg_get(&lfg) % nb_elems : 0; |
| 181 | 32 | s.to_process = to_process; | |
| 182 | 32 | s.offset = 0; | |
| 183 | |||
| 184 | 32 | ret = av_fifo_read_to_cb(fifo, read_cb, &s, &nb_elems); | |
| 185 |
3/6✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 32 times.
|
32 | if (ret < 0 || s.to_process || to_process != nb_elems) { |
| 186 | ✗ | printf("FIFO read fail; seed %"PRIu32"\n", seed); | |
| 187 | ✗ | return 1; | |
| 188 | } | ||
| 189 | 32 | s.read_idx += s.offset; | |
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | 1 | av_fifo_freep2(&fifo); | |
| 194 | 1 | free(p); | |
| 195 | |||
| 196 | 1 | return 0; | |
| 197 | } | ||
| 198 |