| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2016 Alexandra Hájková | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #ifdef BITSTREAM_TEMPLATE_LE | ||
| 22 | # define BS_SUFFIX_LOWER _le | ||
| 23 | # define BS_SUFFIX_UPPER LE | ||
| 24 | #else | ||
| 25 | # define BS_SUFFIX_LOWER _be | ||
| 26 | # define BS_SUFFIX_UPPER BE | ||
| 27 | #endif | ||
| 28 | |||
| 29 | #define BS_JOIN(x, y, z) x ## y ## z | ||
| 30 | #define BS_JOIN3(x, y, z) BS_JOIN(x, y, z) | ||
| 31 | #define BS_FUNC(x) BS_JOIN3(bits_, x, BS_SUFFIX_LOWER) | ||
| 32 | |||
| 33 | #define BSCTX BS_JOIN3(Bitstream, Context, BS_SUFFIX_UPPER) | ||
| 34 | |||
| 35 | typedef struct BSCTX { | ||
| 36 | uint64_t bits; // stores bits read from the buffer | ||
| 37 | const uint8_t *buffer, *buffer_end; | ||
| 38 | const uint8_t *ptr; // pointer to the position inside a buffer | ||
| 39 | unsigned bits_valid; // number of bits left in bits field | ||
| 40 | unsigned size_in_bits; | ||
| 41 | } BSCTX; | ||
| 42 | |||
| 43 | /** | ||
| 44 | * @return | ||
| 45 | * - 0 on successful refill | ||
| 46 | * - a negative number when bitstream end is hit | ||
| 47 | * | ||
| 48 | * Always succeeds when UNCHECKED_BITSTREAM_READER is enabled. | ||
| 49 | */ | ||
| 50 | 23563 | static inline int BS_FUNC(priv_refill_64)(BSCTX *bc) | |
| 51 | { | ||
| 52 | #if !UNCHECKED_BITSTREAM_READER | ||
| 53 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 21696 times.
|
21702 | if (bc->ptr >= bc->buffer_end) |
| 54 | 6 | return -1; | |
| 55 | #endif | ||
| 56 | |||
| 57 | #ifdef BITSTREAM_TEMPLATE_LE | ||
| 58 | 19776 | bc->bits = AV_RL64(bc->ptr); | |
| 59 | #else | ||
| 60 | 3781 | bc->bits = AV_RB64(bc->ptr); | |
| 61 | #endif | ||
| 62 | 23557 | bc->ptr += 8; | |
| 63 | 23557 | bc->bits_valid = 64; | |
| 64 | |||
| 65 | 23557 | return 0; | |
| 66 | } | ||
| 67 | |||
| 68 | /** | ||
| 69 | * @return | ||
| 70 | * - 0 on successful refill | ||
| 71 | * - a negative number when bitstream end is hit | ||
| 72 | * | ||
| 73 | * Always succeeds when UNCHECKED_BITSTREAM_READER is enabled. | ||
| 74 | */ | ||
| 75 | 9193224 | static inline int BS_FUNC(priv_refill_32)(BSCTX *bc) | |
| 76 | { | ||
| 77 | #if !UNCHECKED_BITSTREAM_READER | ||
| 78 |
2/2✓ Branch 0 taken 248 times.
✓ Branch 1 taken 3986886 times.
|
3987134 | if (bc->ptr >= bc->buffer_end) |
| 79 | 248 | return -1; | |
| 80 | #endif | ||
| 81 | |||
| 82 | #ifdef BITSTREAM_TEMPLATE_LE | ||
| 83 | 217957 | bc->bits |= (uint64_t)AV_RL32(bc->ptr) << bc->bits_valid; | |
| 84 | #else | ||
| 85 | 8975019 | bc->bits |= (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_valid); | |
| 86 | #endif | ||
| 87 | 9192976 | bc->ptr += 4; | |
| 88 | 9192976 | bc->bits_valid += 32; | |
| 89 | |||
| 90 | 9192976 | return 0; | |
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Initialize BitstreamContext. | ||
| 95 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
| 96 | * larger than the actual read bits because some optimized bitstream | ||
| 97 | * readers read 32 or 64 bits at once and could read over the end | ||
| 98 | * @param bit_size the size of the buffer in bits | ||
| 99 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||
| 100 | */ | ||
| 101 | 3897 | static inline int BS_FUNC(init)(BSCTX *bc, const uint8_t *buffer, | |
| 102 | unsigned int bit_size) | ||
| 103 | { | ||
| 104 | unsigned int buffer_size; | ||
| 105 | |||
| 106 |
2/4✓ Branch 0 taken 3175 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3175 times.
|
3897 | if (bit_size > INT_MAX - 7 || !buffer) { |
| 107 | ✗ | bc->buffer = NULL; | |
| 108 | ✗ | bc->ptr = NULL; | |
| 109 | ✗ | bc->bits_valid = 0; | |
| 110 | ✗ | return AVERROR_INVALIDDATA; | |
| 111 | } | ||
| 112 | |||
| 113 | 3897 | buffer_size = (bit_size + 7) >> 3; | |
| 114 | |||
| 115 | 3897 | bc->buffer = buffer; | |
| 116 | 3897 | bc->buffer_end = buffer + buffer_size; | |
| 117 | 3897 | bc->ptr = bc->buffer; | |
| 118 | 3897 | bc->size_in_bits = bit_size; | |
| 119 | 3897 | bc->bits_valid = 0; | |
| 120 | 3897 | bc->bits = 0; | |
| 121 | |||
| 122 | 3897 | BS_FUNC(priv_refill_64)(bc); | |
| 123 | |||
| 124 | 3897 | return 0; | |
| 125 | } | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Initialize BitstreamContext. | ||
| 129 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
| 130 | * larger than the actual read bits because some optimized bitstream | ||
| 131 | * readers read 32 or 64 bits at once and could read over the end | ||
| 132 | * @param byte_size the size of the buffer in bytes | ||
| 133 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow | ||
| 134 | */ | ||
| 135 | 2453 | static inline int BS_FUNC(init8)(BSCTX *bc, const uint8_t *buffer, | |
| 136 | unsigned int byte_size) | ||
| 137 | { | ||
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2453 times.
|
2453 | if (byte_size > INT_MAX / 8) |
| 139 | ✗ | return AVERROR_INVALIDDATA; | |
| 140 | 2453 | return BS_FUNC(init)(bc, buffer, byte_size * 8); | |
| 141 | } | ||
| 142 | |||
| 143 | /** | ||
| 144 | * Return number of bits already read. | ||
| 145 | */ | ||
| 146 | 451 | static inline int BS_FUNC(tell)(const BSCTX *bc) | |
| 147 | { | ||
| 148 | 451 | return (bc->ptr - bc->buffer) * 8 - bc->bits_valid; | |
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * Return buffer size in bits. | ||
| 153 | */ | ||
| 154 | static inline int BS_FUNC(size)(const BSCTX *bc) | ||
| 155 | { | ||
| 156 | return bc->size_in_bits; | ||
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Return buffer size in bytes. | ||
| 161 | */ | ||
| 162 | static inline int BS_FUNC(bytesize)(const BSCTX *bc, int round_up) | ||
| 163 | { | ||
| 164 | return (bc->size_in_bits + (round_up ? 7 : 0)) >> 3; | ||
| 165 | } | ||
| 166 | |||
| 167 | /** | ||
| 168 | * Return the number of the bits left in a buffer. | ||
| 169 | */ | ||
| 170 | 51459126 | static inline int BS_FUNC(left)(const BSCTX *bc) | |
| 171 | { | ||
| 172 | 51459126 | return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_valid; | |
| 173 | } | ||
| 174 | |||
| 175 | 61176481 | static inline uint64_t BS_FUNC(priv_val_show)(BSCTX *bc, unsigned int n) | |
| 176 | { | ||
| 177 |
2/4✓ Branch 0 taken 273 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 273 times.
|
273 | av_assert2(n > 0 && n <= 64); |
| 178 | |||
| 179 | #ifdef BITSTREAM_TEMPLATE_LE | ||
| 180 | 1561114 | return bc->bits & (UINT64_MAX >> (64 - n)); | |
| 181 | #else | ||
| 182 | 59615367 | return bc->bits >> (64 - n); | |
| 183 | #endif | ||
| 184 | } | ||
| 185 | |||
| 186 | 61176518 | static inline void BS_FUNC(priv_skip_remaining)(BSCTX *bc, unsigned int n) | |
| 187 | { | ||
| 188 | #ifdef BITSTREAM_TEMPLATE_LE | ||
| 189 | 1561199 | bc->bits >>= n; | |
| 190 | #else | ||
| 191 | 59615319 | bc->bits <<= n; | |
| 192 | #endif | ||
| 193 | 61176518 | bc->bits_valid -= n; | |
| 194 | 61176518 | } | |
| 195 | |||
| 196 | 1561158 | static inline uint64_t BS_FUNC(priv_val_get)(BSCTX *bc, unsigned int n) | |
| 197 | { | ||
| 198 | uint64_t ret; | ||
| 199 | |||
| 200 |
2/4✓ Branch 0 taken 170 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 170 times.
|
170 | av_assert2(n > 0 && n < 64); |
| 201 | |||
| 202 | 1561158 | ret = BS_FUNC(priv_val_show)(bc, n); | |
| 203 | 1561158 | BS_FUNC(priv_skip_remaining)(bc, n); | |
| 204 | |||
| 205 | 1561158 | return ret; | |
| 206 | } | ||
| 207 | |||
| 208 | /** | ||
| 209 | * Return one bit from the buffer. | ||
| 210 | */ | ||
| 211 | 686450 | static inline unsigned int BS_FUNC(read_bit)(BSCTX *bc) | |
| 212 | { | ||
| 213 |
3/4✓ Branch 0 taken 19462 times.
✓ Branch 1 taken 666988 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19462 times.
|
686450 | if (!bc->bits_valid && BS_FUNC(priv_refill_64)(bc) < 0) |
| 214 | ✗ | return 0; | |
| 215 | |||
| 216 | 686450 | return BS_FUNC(priv_val_get)(bc, 1); | |
| 217 | } | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Return n bits from the buffer, n has to be in the 1-32 range. | ||
| 221 | * May be faster than bits_read() when n is not a compile-time constant and is | ||
| 222 | * known to be non-zero; | ||
| 223 | */ | ||
| 224 | 874618 | static inline uint32_t BS_FUNC(read_nz)(BSCTX *bc, unsigned int n) | |
| 225 | { | ||
| 226 |
2/4✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
|
104 | av_assert2(n > 0 && n <= 32); |
| 227 | |||
| 228 |
2/2✓ Branch 0 taken 217941 times.
✓ Branch 1 taken 656677 times.
|
874618 | if (n > bc->bits_valid) { |
| 229 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 217941 times.
|
217941 | if (BS_FUNC(priv_refill_32)(bc) < 0) |
| 230 | ✗ | bc->bits_valid = n; | |
| 231 | } | ||
| 232 | |||
| 233 | 874618 | return BS_FUNC(priv_val_get)(bc, n); | |
| 234 | } | ||
| 235 | |||
| 236 | /** | ||
| 237 | * Return n bits from the buffer, n has to be in the 0-32 range. | ||
| 238 | */ | ||
| 239 | 860013 | static inline uint32_t BS_FUNC(read)(BSCTX *bc, unsigned int n) | |
| 240 | { | ||
| 241 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | av_assert2(n <= 32); |
| 242 | |||
| 243 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 860012 times.
|
860013 | if (!n) |
| 244 | 1 | return 0; | |
| 245 | |||
| 246 | 860012 | return BS_FUNC(read_nz)(bc, n); | |
| 247 | } | ||
| 248 | |||
| 249 | /** | ||
| 250 | * Return n bits from the buffer, n has to be in the 0-63 range. | ||
| 251 | */ | ||
| 252 | 56 | static inline uint64_t BS_FUNC(read_63)(BSCTX *bc, unsigned int n) | |
| 253 | { | ||
| 254 | 56 | uint64_t ret = 0; | |
| 255 | 56 | unsigned left = 0; | |
| 256 | |||
| 257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | av_assert2(n <= 63); |
| 258 | |||
| 259 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 54 times.
|
56 | if (!n) |
| 260 | 2 | return 0; | |
| 261 | |||
| 262 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 17 times.
|
54 | if (n > bc->bits_valid) { |
| 263 | 37 | left = bc->bits_valid; | |
| 264 | 37 | n -= left; | |
| 265 | |||
| 266 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 1 times.
|
37 | if (left) |
| 267 | 36 | ret = BS_FUNC(priv_val_get)(bc, left); | |
| 268 | |||
| 269 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
|
37 | if (BS_FUNC(priv_refill_64)(bc) < 0) |
| 270 | ✗ | bc->bits_valid = n; | |
| 271 | |||
| 272 | } | ||
| 273 | |||
| 274 | #ifdef BITSTREAM_TEMPLATE_LE | ||
| 275 | 39 | ret = BS_FUNC(priv_val_get)(bc, n) << left | ret; | |
| 276 | #else | ||
| 277 | 15 | ret = BS_FUNC(priv_val_get)(bc, n) | ret << n; | |
| 278 | #endif | ||
| 279 | |||
| 280 | 54 | return ret; | |
| 281 | } | ||
| 282 | |||
| 283 | /** | ||
| 284 | * Return n bits from the buffer, n has to be in the 0-64 range. | ||
| 285 | */ | ||
| 286 | 39 | static inline uint64_t BS_FUNC(read_64)(BSCTX *bc, unsigned int n) | |
| 287 | { | ||
| 288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | av_assert2(n <= 64); |
| 289 | |||
| 290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (n == 64) { |
| 291 | ✗ | uint64_t ret = BS_FUNC(read_63)(bc, 63); | |
| 292 | #ifdef BITSTREAM_TEMPLATE_LE | ||
| 293 | ✗ | return ret | ((uint64_t)BS_FUNC(read_bit)(bc) << 63); | |
| 294 | #else | ||
| 295 | ✗ | return (ret << 1) | (uint64_t)BS_FUNC(read_bit)(bc); | |
| 296 | #endif | ||
| 297 | } | ||
| 298 | 39 | return BS_FUNC(read_63)(bc, n); | |
| 299 | } | ||
| 300 | |||
| 301 | /** | ||
| 302 | * Return n bits from the buffer as a signed integer, n has to be in the 1-32 | ||
| 303 | * range. May be faster than bits_read_signed() when n is not a compile-time | ||
| 304 | * constant and is known to be non-zero; | ||
| 305 | */ | ||
| 306 | 10416 | static inline int32_t BS_FUNC(read_signed_nz)(BSCTX *bc, unsigned int n) | |
| 307 | { | ||
| 308 |
2/4✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
|
56 | av_assert2(n > 0 && n <= 32); |
| 309 | 10416 | return sign_extend(BS_FUNC(read_nz)(bc, n), n); | |
| 310 | } | ||
| 311 | |||
| 312 | /** | ||
| 313 | * Return n bits from the buffer as a signed integer. | ||
| 314 | * n has to be in the 0-32 range. | ||
| 315 | */ | ||
| 316 | 31 | static inline int32_t BS_FUNC(read_signed)(BSCTX *bc, unsigned int n) | |
| 317 | { | ||
| 318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | av_assert2(n <= 32); |
| 319 | |||
| 320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (!n) |
| 321 | ✗ | return 0; | |
| 322 | |||
| 323 | 31 | return BS_FUNC(read_signed_nz)(bc, n); | |
| 324 | } | ||
| 325 | |||
| 326 | /** | ||
| 327 | * Return n bits from the buffer but do not change the buffer state. | ||
| 328 | * n has to be in the 1-32 range. May | ||
| 329 | */ | ||
| 330 | 51742524 | static inline uint32_t BS_FUNC(peek_nz)(BSCTX *bc, unsigned int n) | |
| 331 | { | ||
| 332 |
2/4✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 103 times.
|
103 | av_assert2(n > 0 && n <= 32); |
| 333 | |||
| 334 |
2/2✓ Branch 0 taken 7593869 times.
✓ Branch 1 taken 44148655 times.
|
51742524 | if (n > bc->bits_valid) |
| 335 | 7593869 | BS_FUNC(priv_refill_32)(bc); | |
| 336 | |||
| 337 | 51742524 | return BS_FUNC(priv_val_show)(bc, n); | |
| 338 | } | ||
| 339 | |||
| 340 | /** | ||
| 341 | * Return n bits from the buffer but do not change the buffer state. | ||
| 342 | * n has to be in the 0-32 range. | ||
| 343 | */ | ||
| 344 | 51742469 | static inline uint32_t BS_FUNC(peek)(BSCTX *bc, unsigned int n) | |
| 345 | { | ||
| 346 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | av_assert2(n <= 32); |
| 347 | |||
| 348 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 51742464 times.
|
51742469 | if (!n) |
| 349 | 5 | return 0; | |
| 350 | |||
| 351 | 51742464 | return BS_FUNC(peek_nz)(bc, n); | |
| 352 | } | ||
| 353 | |||
| 354 | /** | ||
| 355 | * Return n bits from the buffer as a signed integer, do not change the buffer | ||
| 356 | * state. n has to be in the 1-32 range. May be faster than bits_peek_signed() | ||
| 357 | * when n is not a compile-time constant and is known to be non-zero; | ||
| 358 | */ | ||
| 359 | 43 | static inline int BS_FUNC(peek_signed_nz)(BSCTX *bc, unsigned int n) | |
| 360 | { | ||
| 361 |
2/4✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
|
43 | av_assert2(n > 0 && n <= 32); |
| 362 | 43 | return sign_extend(BS_FUNC(peek_nz)(bc, n), n); | |
| 363 | } | ||
| 364 | |||
| 365 | /** | ||
| 366 | * Return n bits from the buffer as a signed integer, | ||
| 367 | * do not change the buffer state. | ||
| 368 | * n has to be in the 0-32 range. | ||
| 369 | */ | ||
| 370 | 18 | static inline int BS_FUNC(peek_signed)(BSCTX *bc, unsigned int n) | |
| 371 | { | ||
| 372 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | av_assert2(n <= 32); |
| 373 | |||
| 374 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (!n) |
| 375 | ✗ | return 0; | |
| 376 | |||
| 377 | 18 | return BS_FUNC(peek_signed_nz)(bc, n); | |
| 378 | } | ||
| 379 | |||
| 380 | /** | ||
| 381 | * Skip n bits in the buffer. | ||
| 382 | */ | ||
| 383 | 266 | static inline void BS_FUNC(skip)(BSCTX *bc, unsigned int n) | |
| 384 | { | ||
| 385 |
2/2✓ Branch 0 taken 99 times.
✓ Branch 1 taken 167 times.
|
266 | if (n < bc->bits_valid) |
| 386 | 99 | BS_FUNC(priv_skip_remaining)(bc, n); | |
| 387 | else { | ||
| 388 | 167 | n -= bc->bits_valid; | |
| 389 | 167 | bc->bits = 0; | |
| 390 | 167 | bc->bits_valid = 0; | |
| 391 | |||
| 392 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 167 times.
|
167 | if (n >= 64) { |
| 393 | ✗ | unsigned int skip = n / 8; | |
| 394 | |||
| 395 | ✗ | n -= skip * 8; | |
| 396 | ✗ | bc->ptr += skip; | |
| 397 | } | ||
| 398 | 167 | BS_FUNC(priv_refill_64)(bc); | |
| 399 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 126 times.
|
167 | if (n) |
| 400 | 41 | BS_FUNC(priv_skip_remaining)(bc, n); | |
| 401 | } | ||
| 402 | 266 | } | |
| 403 | |||
| 404 | /** | ||
| 405 | * Seek to the given bit position. | ||
| 406 | */ | ||
| 407 | static inline void BS_FUNC(seek)(BSCTX *bc, unsigned pos) | ||
| 408 | { | ||
| 409 | bc->ptr = bc->buffer; | ||
| 410 | bc->bits = 0; | ||
| 411 | bc->bits_valid = 0; | ||
| 412 | |||
| 413 | BS_FUNC(skip)(bc, pos); | ||
| 414 | } | ||
| 415 | |||
| 416 | /** | ||
| 417 | * Skip bits to a byte boundary. | ||
| 418 | */ | ||
| 419 | 74 | static inline const uint8_t *BS_FUNC(align)(BSCTX *bc) | |
| 420 | { | ||
| 421 | 74 | unsigned int n = -BS_FUNC(tell)(bc) & 7; | |
| 422 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 8 times.
|
74 | if (n) |
| 423 | 66 | BS_FUNC(skip)(bc, n); | |
| 424 | 74 | return bc->buffer + (BS_FUNC(tell)(bc) >> 3); | |
| 425 | } | ||
| 426 | |||
| 427 | /** | ||
| 428 | * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). | ||
| 429 | * If MSB not set it is negative. | ||
| 430 | * @param n length in bits | ||
| 431 | */ | ||
| 432 | static inline int BS_FUNC(read_xbits)(BSCTX *bc, unsigned int n) | ||
| 433 | { | ||
| 434 | int32_t cache = BS_FUNC(peek)(bc, 32); | ||
| 435 | int sign = ~cache >> 31; | ||
| 436 | BS_FUNC(priv_skip_remaining)(bc, n); | ||
| 437 | |||
| 438 | return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign; | ||
| 439 | } | ||
| 440 | |||
| 441 | /** | ||
| 442 | * Return decoded truncated unary code for the values 0, 1, 2. | ||
| 443 | */ | ||
| 444 | static inline int BS_FUNC(decode012)(BSCTX *bc) | ||
| 445 | { | ||
| 446 | if (!BS_FUNC(read_bit)(bc)) | ||
| 447 | return 0; | ||
| 448 | else | ||
| 449 | return BS_FUNC(read_bit)(bc) + 1; | ||
| 450 | } | ||
| 451 | |||
| 452 | /** | ||
| 453 | * Return decoded truncated unary code for the values 2, 1, 0. | ||
| 454 | */ | ||
| 455 | static inline int BS_FUNC(decode210)(BSCTX *bc) | ||
| 456 | { | ||
| 457 | if (BS_FUNC(read_bit)(bc)) | ||
| 458 | return 0; | ||
| 459 | else | ||
| 460 | return 2 - BS_FUNC(read_bit)(bc); | ||
| 461 | } | ||
| 462 | |||
| 463 | /* Read sign bit and flip the sign of the provided value accordingly. */ | ||
| 464 | 13 | static inline int BS_FUNC(apply_sign)(BSCTX *bc, int val) | |
| 465 | { | ||
| 466 | 13 | int sign = BS_FUNC(read_signed)(bc, 1); | |
| 467 | 13 | return (val ^ sign) - sign; | |
| 468 | } | ||
| 469 | |||
| 470 | static inline int BS_FUNC(skip_1stop_8data)(BSCTX *s) | ||
| 471 | { | ||
| 472 | if (BS_FUNC(left)(s) <= 0) | ||
| 473 | return AVERROR_INVALIDDATA; | ||
| 474 | |||
| 475 | while (BS_FUNC(read_bit)(s)) { | ||
| 476 | BS_FUNC(skip)(s, 8); | ||
| 477 | if (BS_FUNC(left)(s) <= 0) | ||
| 478 | return AVERROR_INVALIDDATA; | ||
| 479 | } | ||
| 480 | |||
| 481 | return 0; | ||
| 482 | } | ||
| 483 | |||
| 484 | /** | ||
| 485 | * Return the LUT element for the given bitstream configuration. | ||
| 486 | */ | ||
| 487 | 284862 | static inline int BS_FUNC(priv_set_idx)(BSCTX *bc, int code, int *n, | |
| 488 | int *nb_bits, const VLCElem *table) | ||
| 489 | { | ||
| 490 | unsigned idx; | ||
| 491 | |||
| 492 | 284862 | *nb_bits = -*n; | |
| 493 | 284862 | idx = BS_FUNC(peek)(bc, *nb_bits) + code; | |
| 494 | 284862 | *n = table[idx].len; | |
| 495 | |||
| 496 | 284862 | return table[idx].sym; | |
| 497 | } | ||
| 498 | |||
| 499 | /** | ||
| 500 | * Parse a vlc code. | ||
| 501 | * @param bits is the number of bits which will be read at once, must be | ||
| 502 | * identical to nb_bits in vlc_init() | ||
| 503 | * @param max_depth is the number of times bits bits must be read to completely | ||
| 504 | * read the longest vlc code | ||
| 505 | * = (max_vlc_length + bits - 1) / bits | ||
| 506 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. | ||
| 507 | * If the vlc code is invalid and max_depth>1, then the number of bits removed | ||
| 508 | * is undefined. | ||
| 509 | */ | ||
| 510 | 30302187 | static inline int BS_FUNC(read_vlc)(BSCTX *bc, const VLCElem *table, | |
| 511 | int bits, int max_depth) | ||
| 512 | { | ||
| 513 | int nb_bits; | ||
| 514 | 30302187 | unsigned idx = BS_FUNC(peek)(bc, bits); | |
| 515 | 30302187 | int code = table[idx].sym; | |
| 516 | 30302187 | int n = table[idx].len; | |
| 517 | |||
| 518 |
3/4✓ Branch 0 taken 30302187 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 201847 times.
✓ Branch 3 taken 30100340 times.
|
30302187 | if (max_depth > 1 && n < 0) { |
| 519 | 201847 | BS_FUNC(priv_skip_remaining)(bc, bits); | |
| 520 | 201847 | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | |
| 521 |
3/4✓ Branch 0 taken 201847 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 201790 times.
|
201847 | if (max_depth > 2 && n < 0) { |
| 522 | 57 | BS_FUNC(priv_skip_remaining)(bc, nb_bits); | |
| 523 | 57 | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | |
| 524 | } | ||
| 525 | } | ||
| 526 | 30302187 | BS_FUNC(priv_skip_remaining)(bc, n); | |
| 527 | |||
| 528 | 30302187 | return code; | |
| 529 | } | ||
| 530 | |||
| 531 | /** | ||
| 532 | * Parse a vlc / vlc_multi code. | ||
| 533 | * @param bits is the number of bits which will be read at once, must be | ||
| 534 | * identical to nb_bits in vlc_init() | ||
| 535 | * @param max_depth is the number of times bits bits must be read to completely | ||
| 536 | * read the longest vlc code | ||
| 537 | * = (max_vlc_length + bits - 1) / bits | ||
| 538 | * @param dst the parsed symbol(s) will be stored here. Up to 8 bytes are written | ||
| 539 | * @returns number of symbols parsed | ||
| 540 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. | ||
| 541 | * If the vlc code is invalid and max_depth>1, then the number of bits removed | ||
| 542 | * is undefined. | ||
| 543 | */ | ||
| 544 | 21155372 | static inline int BS_FUNC(read_vlc_multi)(BSCTX *bc, uint8_t dst[8], | |
| 545 | const VLC_MULTI_ELEM *const Jtable, | ||
| 546 | const VLCElem *const table, | ||
| 547 | const int bits, const int max_depth, | ||
| 548 | const int symbols_size) | ||
| 549 | { | ||
| 550 | 21155372 | unsigned idx = BS_FUNC(peek)(bc, bits); | |
| 551 | 21155372 | int ret, nb_bits, code, n = Jtable[idx].len; | |
| 552 |
2/2✓ Branch 0 taken 21072414 times.
✓ Branch 1 taken 82958 times.
|
21155372 | if (Jtable[idx].num) { |
| 553 | 21072414 | AV_COPY64U(dst, Jtable[idx].val8); | |
| 554 | 21072414 | ret = Jtable[idx].num; | |
| 555 | } else { | ||
| 556 | 82958 | code = table[idx].sym; | |
| 557 | 82958 | n = table[idx].len; | |
| 558 |
2/4✓ Branch 0 taken 82958 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 82958 times.
✗ Branch 3 not taken.
|
82958 | if (max_depth > 1 && n < 0) { |
| 559 | 82958 | BS_FUNC(priv_skip_remaining)(bc, bits); | |
| 560 | 82958 | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | |
| 561 |
2/4✓ Branch 0 taken 82958 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 82958 times.
|
82958 | if (max_depth > 2 && n < 0) { |
| 562 | ✗ | BS_FUNC(priv_skip_remaining)(bc, nb_bits); | |
| 563 | ✗ | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | |
| 564 | } | ||
| 565 | } | ||
| 566 |
1/2✓ Branch 0 taken 82958 times.
✗ Branch 1 not taken.
|
82958 | if (symbols_size == 1) |
| 567 | 82958 | *dst = code; | |
| 568 | else | ||
| 569 | ✗ | AV_WN16(dst, code); | |
| 570 | 82958 | ret = n > 0; | |
| 571 | } | ||
| 572 | 21155372 | BS_FUNC(priv_skip_remaining)(bc, n); | |
| 573 | |||
| 574 | 21155372 | return ret; | |
| 575 | } | ||
| 576 | |||
| 577 | #undef BSCTX | ||
| 578 | #undef BS_FUNC | ||
| 579 | #undef BS_JOIN3 | ||
| 580 | #undef BS_JOIN | ||
| 581 | #undef BS_SUFFIX_UPPER | ||
| 582 | #undef BS_SUFFIX_LOWER | ||
| 583 |