| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * cached bitstream reader test | ||
| 3 | * copyright (c) 2022 Anton Khirnov <anton@khirnov.net> | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #define ASSERT_LEVEL 2 | ||
| 23 | |||
| 24 | #include "libavutil/avassert.h" | ||
| 25 | #include "libavutil/lfg.h" | ||
| 26 | #include "libavutil/random_seed.h" | ||
| 27 | |||
| 28 | #include "libavcodec/bitstream.h" | ||
| 29 | #include "libavcodec/defs.h" | ||
| 30 | |||
| 31 | #ifdef BITSTREAM_LE | ||
| 32 | #define BITSTREAM_WRITER_LE | ||
| 33 | #endif | ||
| 34 | #include "libavcodec/put_bits.h" | ||
| 35 | |||
| 36 | #define SIZE 157 | ||
| 37 | |||
| 38 | enum Op { | ||
| 39 | OP_READ, | ||
| 40 | OP_READ_NZ, | ||
| 41 | OP_READ_BIT, | ||
| 42 | OP_READ_63, | ||
| 43 | OP_READ_64, | ||
| 44 | OP_READ_SIGNED, | ||
| 45 | OP_READ_SIGNED_NZ, | ||
| 46 | OP_APPLY_SIGN, | ||
| 47 | OP_ALIGN, | ||
| 48 | OP_NB, | ||
| 49 | }; | ||
| 50 | |||
| 51 | 2 | int main(int argc, char **argv) | |
| 52 | { | ||
| 53 | BitstreamContext bc; | ||
| 54 | PutBitContext pb; | ||
| 55 | AVLFG lfg; | ||
| 56 | |||
| 57 | uint8_t buf[SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; | ||
| 58 | uint8_t dst[SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; | ||
| 59 | |||
| 60 | uint32_t random_seed; | ||
| 61 | uint64_t val, val1; | ||
| 62 | int32_t sval, sval1; | ||
| 63 | unsigned count; | ||
| 64 | int ret; | ||
| 65 | |||
| 66 | /* generate random input, using a given or random seed */ | ||
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (argc > 1) |
| 68 | ✗ | random_seed = strtoul(argv[1], NULL, 0); | |
| 69 | else | ||
| 70 | 2 | random_seed = av_get_random_seed(); | |
| 71 | |||
| 72 | 2 | fprintf(stderr, "Testing with LFG seed: %"PRIu32"\n", random_seed); | |
| 73 | 2 | av_lfg_init(&lfg, random_seed); | |
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 314 times.
✓ Branch 1 taken 2 times.
|
316 | for (unsigned i = 0; i < SIZE; i++) |
| 76 | 314 | buf[i] = av_lfg_get(&lfg); | |
| 77 | |||
| 78 | 2 | ret = bits_init8 (&bc, buf, SIZE); | |
| 79 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | av_assert0(ret >= 0); |
| 80 | 2 | init_put_bits(&pb, dst, SIZE); | |
| 81 | |||
| 82 | /* use a random sequence of bitreading operations to transfer data | ||
| 83 | * from BitstreamContext to PutBitContext */ | ||
| 84 |
2/2✓ Branch 1 taken 156 times.
✓ Branch 2 taken 2 times.
|
158 | while (bits_left(&bc) > 0) { |
| 85 | 156 | enum Op op = av_lfg_get(&lfg) % OP_NB; | |
| 86 | |||
| 87 |
9/10✓ Branch 0 taken 19 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 25 times.
✓ Branch 7 taken 14 times.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
|
156 | switch (op) { |
| 88 | 19 | case OP_READ: | |
| 89 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 18 times.
|
19 | count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1); |
| 90 | 19 | val1 = bits_peek(&bc, count); | |
| 91 | 19 | val = bits_read(&bc, count); | |
| 92 | |||
| 93 | 19 | fprintf(stderr, "%d read %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val); | |
| 94 | |||
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | av_assert0(val == val1); |
| 96 | |||
| 97 | 19 | put_bits64(&pb, count, val); | |
| 98 | 19 | break; | |
| 99 | 17 | case OP_READ_NZ: | |
| 100 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 16 times.
|
17 | count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1); |
| 101 | 17 | count = FFMAX(count, 1); | |
| 102 | 17 | val1 = bits_peek_nz(&bc, count); | |
| 103 | 17 | val = bits_read_nz(&bc, count); | |
| 104 | |||
| 105 | 17 | fprintf(stderr, "%d read_nz %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val); | |
| 106 | |||
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | av_assert0(val == val1); |
| 108 | |||
| 109 | 17 | put_bits64(&pb, count, val); | |
| 110 | 17 | break; | |
| 111 | 15 | case OP_READ_BIT: | |
| 112 | 15 | val = bits_read_bit(&bc); | |
| 113 | |||
| 114 | 15 | fprintf(stderr, "%d read_bit: %"PRIu64"\n", bits_tell(&bc) - 1, val); | |
| 115 | |||
| 116 | 15 | put_bits(&pb, 1, val); | |
| 117 | 15 | break; | |
| 118 | 17 | case OP_READ_63: | |
| 119 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 3 taken 15 times.
|
17 | count = av_lfg_get(&lfg) % FFMIN(64, bits_left(&bc) + 1); |
| 120 | 17 | val = bits_read_63(&bc, count); | |
| 121 | |||
| 122 | 17 | fprintf(stderr, "%d read_63 %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val); | |
| 123 | |||
| 124 | 17 | put_bits64(&pb, count, val); | |
| 125 | 17 | break; | |
| 126 | 16 | case OP_READ_64: | |
| 127 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
16 | count = av_lfg_get(&lfg) % FFMIN(65, bits_left(&bc) + 1); |
| 128 | 16 | val = bits_read_64(&bc, count); | |
| 129 | |||
| 130 | 16 | fprintf(stderr, "%d read_64 %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val); | |
| 131 | |||
| 132 | 16 | put_bits64(&pb, count, val); | |
| 133 | 16 | break; | |
| 134 | 18 | case OP_READ_SIGNED: | |
| 135 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 17 times.
|
18 | count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1); |
| 136 | 18 | sval1 = bits_peek_signed(&bc, count); | |
| 137 | 18 | sval = bits_read_signed(&bc, count); | |
| 138 | |||
| 139 | 18 | fprintf(stderr, "%d read_signed %u: %"PRId32"\n", bits_tell(&bc) - count, count, sval); | |
| 140 | |||
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | av_assert0(sval == sval1); |
| 142 | |||
| 143 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 | if (count == 32) put_bits32(&pb, sval); |
| 144 | 16 | else put_sbits(&pb, count, sval); | |
| 145 | 18 | break; | |
| 146 | 25 | case OP_READ_SIGNED_NZ: | |
| 147 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 3 taken 22 times.
|
25 | count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1); |
| 148 | 25 | count = FFMAX(count, 1); | |
| 149 | 25 | sval1 = bits_peek_signed_nz(&bc, count); | |
| 150 | 25 | sval = bits_read_signed_nz(&bc, count); | |
| 151 | |||
| 152 | 25 | fprintf(stderr, "%d read_signed_nz %u: %"PRId32"\n", bits_tell(&bc) - count, count, sval); | |
| 153 | |||
| 154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | av_assert0(sval == sval1); |
| 155 | |||
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (count == 32) put_bits32(&pb, sval); |
| 157 | 25 | else put_sbits(&pb, count, sval); | |
| 158 | 25 | break; | |
| 159 | 14 | case OP_ALIGN: | |
| 160 | 14 | count = (bits_tell(&bc) + 7) / 8 * 8 - bits_tell(&bc); | |
| 161 | |||
| 162 | 14 | fprintf(stderr, "%d align %u\n", bits_tell(&bc), count); | |
| 163 | |||
| 164 | 14 | put_bits(&pb, count, bits_peek(&bc, count)); | |
| 165 | 14 | bits_align(&bc); | |
| 166 | 14 | break; | |
| 167 | 15 | case OP_APPLY_SIGN: | |
| 168 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
|
15 | if (bits_left(&bc) < 2) |
| 169 | ✗ | continue; | |
| 170 | |||
| 171 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 14 times.
|
15 | count = av_lfg_get(&lfg) % FFMIN(32, bits_left(&bc)); |
| 172 | 15 | count = FFMAX(count, 1); | |
| 173 | |||
| 174 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 13 times.
|
15 | if (!bits_peek(&bc, count)) |
| 175 | 2 | continue; | |
| 176 | |||
| 177 | 13 | val = bits_read(&bc, count); | |
| 178 | 13 | sval = bits_apply_sign(&bc, val); | |
| 179 | |||
| 180 | 13 | fprintf(stderr, "%d apply_sign %u %"PRId32"\n", | |
| 181 | 13 | bits_tell(&bc) - count - 1, count, sval); | |
| 182 | |||
| 183 | 13 | put_bits64(&pb, count, FFABS(sval)); | |
| 184 | 13 | put_bits(&pb, 1, sval < 0); | |
| 185 | |||
| 186 | 13 | break; | |
| 187 | ✗ | default: | |
| 188 | ✗ | av_assert0(0); | |
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | 2 | flush_put_bits(&pb); | |
| 193 | |||
| 194 |
2/2✓ Branch 0 taken 314 times.
✓ Branch 1 taken 2 times.
|
316 | for (unsigned i = 0; i < SIZE; i++) |
| 195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 314 times.
|
314 | if (buf[i] != dst[i]) { |
| 196 | ✗ | fprintf(stderr, "Mismatch at byte %u: %hhu %hhu; seed %"PRIu32"\n", | |
| 197 | ✗ | i, buf[i], dst[i], random_seed); | |
| 198 | ✗ | return 1; | |
| 199 | } | ||
| 200 | |||
| 201 | 2 | return 0; | |
| 202 | } | ||
| 203 |