| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | ||
| 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 | /** | ||
| 22 | * @file | ||
| 23 | * bitstream reader API header. | ||
| 24 | */ | ||
| 25 | |||
| 26 | #ifndef AVCODEC_GET_BITS_H | ||
| 27 | #define AVCODEC_GET_BITS_H | ||
| 28 | |||
| 29 | #include <stdint.h> | ||
| 30 | |||
| 31 | #include "libavutil/common.h" | ||
| 32 | #include "libavutil/intreadwrite.h" | ||
| 33 | #include "libavutil/avassert.h" | ||
| 34 | |||
| 35 | #include "defs.h" | ||
| 36 | #include "mathops.h" | ||
| 37 | #include "vlc.h" | ||
| 38 | |||
| 39 | /* | ||
| 40 | * Safe bitstream reading: | ||
| 41 | * optionally, the get_bits API can check to ensure that we | ||
| 42 | * don't read past input buffer boundaries. This is protected | ||
| 43 | * with CONFIG_SAFE_BITSTREAM_READER at the global level, and | ||
| 44 | * then below that with UNCHECKED_BITSTREAM_READER at the per- | ||
| 45 | * decoder level. This means that decoders that check internally | ||
| 46 | * can "#define UNCHECKED_BITSTREAM_READER 1" to disable | ||
| 47 | * overread checks. | ||
| 48 | * Boundary checking causes a minor performance penalty so for | ||
| 49 | * applications that won't want/need this, it can be disabled | ||
| 50 | * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0". | ||
| 51 | */ | ||
| 52 | #ifndef UNCHECKED_BITSTREAM_READER | ||
| 53 | #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER | ||
| 54 | #endif | ||
| 55 | |||
| 56 | #ifndef CACHED_BITSTREAM_READER | ||
| 57 | #define CACHED_BITSTREAM_READER 0 | ||
| 58 | #endif | ||
| 59 | |||
| 60 | #if CACHED_BITSTREAM_READER | ||
| 61 | |||
| 62 | // we always want the LE implementation, to provide get_bits_le() | ||
| 63 | #define BITSTREAM_LE | ||
| 64 | |||
| 65 | #ifndef BITSTREAM_READER_LE | ||
| 66 | # define BITSTREAM_BE | ||
| 67 | # define BITSTREAM_DEFAULT_BE | ||
| 68 | #endif | ||
| 69 | |||
| 70 | #include "bitstream.h" | ||
| 71 | |||
| 72 | #undef BITSTREAM_LE | ||
| 73 | #undef BITSTREAM_BE | ||
| 74 | #undef BITSTREAM_DEFAULT_BE | ||
| 75 | |||
| 76 | typedef BitstreamContext GetBitContext; | ||
| 77 | |||
| 78 | #define get_bits_count bits_tell | ||
| 79 | #define get_bits_bytesize bits_bytesize | ||
| 80 | #define get_bits_left bits_left | ||
| 81 | #define skip_bits_long bits_skip | ||
| 82 | #define skip_bits bits_skip | ||
| 83 | #define get_bits bits_read_nz | ||
| 84 | #define get_bitsz bits_read | ||
| 85 | #define get_bits_long bits_read | ||
| 86 | #define get_bits1 bits_read_bit | ||
| 87 | #define get_bits64 bits_read_64 | ||
| 88 | #define get_xbits bits_read_xbits | ||
| 89 | #define get_sbits bits_read_signed_nz | ||
| 90 | #define get_sbits_long bits_read_signed | ||
| 91 | #define show_bits bits_peek | ||
| 92 | #define show_bits_long bits_peek | ||
| 93 | #define init_get_bits bits_init | ||
| 94 | #define init_get_bits8 bits_init8 | ||
| 95 | #define align_get_bits bits_align | ||
| 96 | #define get_vlc2 bits_read_vlc | ||
| 97 | #define get_vlc_multi bits_read_vlc_multi | ||
| 98 | |||
| 99 | #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size) | ||
| 100 | #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n) | ||
| 101 | |||
| 102 | #define show_bits1(s) bits_peek(s, 1) | ||
| 103 | #define skip_bits1(s) bits_skip(s, 1) | ||
| 104 | |||
| 105 | #define skip_1stop_8data_bits bits_skip_1stop_8data | ||
| 106 | |||
| 107 | #else // CACHED_BITSTREAM_READER | ||
| 108 | |||
| 109 | typedef struct GetBitContext { | ||
| 110 | const uint8_t *buffer; | ||
| 111 | int index; | ||
| 112 | int size_in_bits; | ||
| 113 | int size_in_bits_plus8; | ||
| 114 | } GetBitContext; | ||
| 115 | |||
| 116 | static inline unsigned int get_bits(GetBitContext *s, int n); | ||
| 117 | static inline void skip_bits(GetBitContext *s, int n); | ||
| 118 | static inline unsigned int show_bits(GetBitContext *s, int n); | ||
| 119 | |||
| 120 | /* Bitstream reader API docs: | ||
| 121 | * name | ||
| 122 | * arbitrary name which is used as prefix for the internal variables | ||
| 123 | * | ||
| 124 | * gb | ||
| 125 | * getbitcontext | ||
| 126 | * | ||
| 127 | * OPEN_READER(name, gb) | ||
| 128 | * load gb into local variables | ||
| 129 | * | ||
| 130 | * CLOSE_READER(name, gb) | ||
| 131 | * store local vars in gb | ||
| 132 | * | ||
| 133 | * UPDATE_CACHE(name, gb) | ||
| 134 | * Refill the internal cache from the bitstream. | ||
| 135 | * After this call at least MIN_CACHE_BITS will be available. | ||
| 136 | * | ||
| 137 | * GET_CACHE(name, gb) | ||
| 138 | * Will output the contents of the internal cache, | ||
| 139 | * next bit is MSB of 32 or 64 bits (FIXME 64 bits). | ||
| 140 | * | ||
| 141 | * SHOW_UBITS(name, gb, num) | ||
| 142 | * Will return the next num bits. | ||
| 143 | * | ||
| 144 | * SHOW_SBITS(name, gb, num) | ||
| 145 | * Will return the next num bits and do sign extension. | ||
| 146 | * | ||
| 147 | * SKIP_BITS(name, gb, num) | ||
| 148 | * Will skip over the next num bits. | ||
| 149 | * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER. | ||
| 150 | * | ||
| 151 | * SKIP_CACHE(name, gb, num) | ||
| 152 | * Will remove the next num bits from the cache (note SKIP_COUNTER | ||
| 153 | * MUST be called before UPDATE_CACHE / CLOSE_READER). | ||
| 154 | * | ||
| 155 | * SKIP_COUNTER(name, gb, num) | ||
| 156 | * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS). | ||
| 157 | * | ||
| 158 | * LAST_SKIP_BITS(name, gb, num) | ||
| 159 | * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER. | ||
| 160 | * | ||
| 161 | * BITS_LEFT(name, gb) | ||
| 162 | * Return the number of bits left | ||
| 163 | * | ||
| 164 | * For examples see get_bits, show_bits, skip_bits, get_vlc. | ||
| 165 | */ | ||
| 166 | |||
| 167 | #define MIN_CACHE_BITS 25 | ||
| 168 | |||
| 169 | #define OPEN_READER_NOSIZE_NOCACHE(name, gb) \ | ||
| 170 | unsigned int name ## _index = (gb)->index | ||
| 171 | |||
| 172 | #define OPEN_READER_NOSIZE(name, gb) \ | ||
| 173 | OPEN_READER_NOSIZE_NOCACHE(name, gb); \ | ||
| 174 | unsigned int name ## _cache | ||
| 175 | |||
| 176 | #if UNCHECKED_BITSTREAM_READER | ||
| 177 | #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb) | ||
| 178 | #define OPEN_READER_SIZE(name, gb) ((void)0) | ||
| 179 | #define BITS_AVAILABLE(name, gb) 1 | ||
| 180 | #else | ||
| 181 | #define OPEN_READER_SIZE(name, gb) unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8 | ||
| 182 | #define OPEN_READER(name, gb) \ | ||
| 183 | OPEN_READER_NOSIZE(name, gb); \ | ||
| 184 | OPEN_READER_SIZE(name, gb) | ||
| 185 | |||
| 186 | #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8 | ||
| 187 | #endif | ||
| 188 | |||
| 189 | #define CLOSE_READER(name, gb) (gb)->index = name ## _index | ||
| 190 | |||
| 191 | #define UPDATE_CACHE_BE_EXT(name, gb, bits, dst_bits) name ## _cache = \ | ||
| 192 | AV_RB ## bits((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) >> (bits - dst_bits) | ||
| 193 | |||
| 194 | #define UPDATE_CACHE_LE_EXT(name, gb, bits, dst_bits) name ## _cache = \ | ||
| 195 | (uint ## dst_bits ## _t)(AV_RL ## bits((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)) | ||
| 196 | |||
| 197 | /* Using these two macros ensures that 32 bits are available. */ | ||
| 198 | # define UPDATE_CACHE_LE_32(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 64, 32) | ||
| 199 | # define UPDATE_CACHE_BE_32(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 64, 32) | ||
| 200 | |||
| 201 | # define UPDATE_CACHE_LE(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 32, 32) | ||
| 202 | # define UPDATE_CACHE_BE(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 32, 32) | ||
| 203 | |||
| 204 | #ifdef BITSTREAM_READER_LE | ||
| 205 | |||
| 206 | # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb) | ||
| 207 | # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_LE_32(name, (gb)) | ||
| 208 | |||
| 209 | # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num) | ||
| 210 | |||
| 211 | #else | ||
| 212 | |||
| 213 | # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb) | ||
| 214 | # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_BE_32(name, (gb)) | ||
| 215 | |||
| 216 | # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num) | ||
| 217 | |||
| 218 | #endif | ||
| 219 | |||
| 220 | #if UNCHECKED_BITSTREAM_READER | ||
| 221 | # define SKIP_COUNTER(name, gb, num) name ## _index += (num) | ||
| 222 | #else | ||
| 223 | # define SKIP_COUNTER(name, gb, num) \ | ||
| 224 | name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num)) | ||
| 225 | #endif | ||
| 226 | |||
| 227 | #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index)) | ||
| 228 | |||
| 229 | #define SKIP_BITS(name, gb, num) \ | ||
| 230 | do { \ | ||
| 231 | SKIP_CACHE(name, gb, num); \ | ||
| 232 | SKIP_COUNTER(name, gb, num); \ | ||
| 233 | } while (0) | ||
| 234 | |||
| 235 | #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) | ||
| 236 | |||
| 237 | #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num) | ||
| 238 | #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num) | ||
| 239 | |||
| 240 | #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num) | ||
| 241 | #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num) | ||
| 242 | |||
| 243 | #ifdef BITSTREAM_READER_LE | ||
| 244 | # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num) | ||
| 245 | # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num) | ||
| 246 | #else | ||
| 247 | # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num) | ||
| 248 | # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num) | ||
| 249 | #endif | ||
| 250 | |||
| 251 | #define GET_CACHE(name, gb) ((uint32_t) name ## _cache) | ||
| 252 | |||
| 253 | |||
| 254 | 320895867 | static inline int get_bits_count(const GetBitContext *s) | |
| 255 | { | ||
| 256 | 320895867 | return s->index; | |
| 257 | } | ||
| 258 | |||
| 259 | /** | ||
| 260 | * Get the size of the GetBitContext's buffer in bytes. | ||
| 261 | * | ||
| 262 | * @param s the GetBitContext | ||
| 263 | * @param round_up If set, the number of bits will be rounded up to full bytes; | ||
| 264 | * this does not matter if the number of bits is known to be | ||
| 265 | * a multiple of eight, e.g. if the GetBitContext has been | ||
| 266 | * initialized with init_get_bits8. | ||
| 267 | */ | ||
| 268 | 26316 | static inline int get_bits_bytesize(const GetBitContext *s, int round_up) | |
| 269 | { | ||
| 270 |
2/2✓ Branch 0 taken 17820 times.
✓ Branch 1 taken 8496 times.
|
26316 | return (s->size_in_bits + (round_up ? 7 : 0)) >> 3; |
| 271 | } | ||
| 272 | |||
| 273 | /** | ||
| 274 | * Skips the specified number of bits. | ||
| 275 | * @param n the number of bits to skip, | ||
| 276 | * For the UNCHECKED_BITSTREAM_READER this must not cause the distance | ||
| 277 | * from the start to overflow int32_t. Staying within the bitstream + padding | ||
| 278 | * is sufficient, too. | ||
| 279 | */ | ||
| 280 | 4732526 | static inline void skip_bits_long(GetBitContext *s, int n) | |
| 281 | { | ||
| 282 | #if UNCHECKED_BITSTREAM_READER | ||
| 283 | 2785959 | s->index += n; | |
| 284 | #else | ||
| 285 | 1946567 | s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index); | |
| 286 | #endif | ||
| 287 | 4732526 | } | |
| 288 | |||
| 289 | /** | ||
| 290 | * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). | ||
| 291 | * if MSB not set it is negative | ||
| 292 | * @param n length in bits | ||
| 293 | */ | ||
| 294 | 27217820 | static inline int get_xbits(GetBitContext *s, int n) | |
| 295 | { | ||
| 296 | register int sign; | ||
| 297 | register int32_t cache; | ||
| 298 | 27217820 | OPEN_READER(re, s); | |
| 299 | av_assert2(n>0 && n<=25); | ||
| 300 | 27217820 | UPDATE_CACHE(re, s); | |
| 301 | 27217820 | cache = GET_CACHE(re, s); | |
| 302 | 27217820 | sign = ~cache >> 31; | |
| 303 | 27217820 | LAST_SKIP_BITS(re, s, n); | |
| 304 | 27217820 | CLOSE_READER(re, s); | |
| 305 | 27217820 | return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; | |
| 306 | } | ||
| 307 | |||
| 308 | 1534760 | static inline int get_xbits_le(GetBitContext *s, int n) | |
| 309 | { | ||
| 310 | register int sign; | ||
| 311 | register int32_t cache; | ||
| 312 | 1534760 | OPEN_READER(re, s); | |
| 313 | av_assert2(n>0 && n<=25); | ||
| 314 | 1534760 | UPDATE_CACHE_LE(re, s); | |
| 315 | 1534760 | cache = GET_CACHE(re, s); | |
| 316 | 1534760 | sign = sign_extend(~cache, n) >> 31; | |
| 317 | 1534760 | LAST_SKIP_BITS(re, s, n); | |
| 318 | 1534760 | CLOSE_READER(re, s); | |
| 319 | 1534760 | return (zero_extend(sign ^ cache, n) ^ sign) - sign; | |
| 320 | } | ||
| 321 | |||
| 322 | 33296922 | static inline int get_sbits(GetBitContext *s, int n) | |
| 323 | { | ||
| 324 | register int tmp; | ||
| 325 | 33296922 | OPEN_READER(re, s); | |
| 326 | av_assert2(n>0 && n<=25); | ||
| 327 | 33296922 | UPDATE_CACHE(re, s); | |
| 328 | 33296922 | tmp = SHOW_SBITS(re, s, n); | |
| 329 | 33296922 | LAST_SKIP_BITS(re, s, n); | |
| 330 | 33296922 | CLOSE_READER(re, s); | |
| 331 | 33296922 | return tmp; | |
| 332 | } | ||
| 333 | |||
| 334 | /** | ||
| 335 | * Read 1-25 bits. | ||
| 336 | */ | ||
| 337 | 276822018 | static inline unsigned int get_bits(GetBitContext *s, int n) | |
| 338 | { | ||
| 339 | register unsigned int tmp; | ||
| 340 | 276822018 | OPEN_READER(re, s); | |
| 341 | av_assert2(n>0 && n<=25); | ||
| 342 | 276822018 | UPDATE_CACHE(re, s); | |
| 343 | 276822018 | tmp = SHOW_UBITS(re, s, n); | |
| 344 | 276822018 | LAST_SKIP_BITS(re, s, n); | |
| 345 | 276822018 | CLOSE_READER(re, s); | |
| 346 | av_assert2(tmp < UINT64_C(1) << n); | ||
| 347 | 276822018 | return tmp; | |
| 348 | } | ||
| 349 | |||
| 350 | /** | ||
| 351 | * Read 0-25 bits. | ||
| 352 | */ | ||
| 353 | 746532 | static av_always_inline int get_bitsz(GetBitContext *s, int n) | |
| 354 | { | ||
| 355 |
2/2✓ Branch 0 taken 668627 times.
✓ Branch 1 taken 77905 times.
|
746532 | return n ? get_bits(s, n) : 0; |
| 356 | } | ||
| 357 | |||
| 358 | 781265 | static inline unsigned int get_bits_le(GetBitContext *s, int n) | |
| 359 | { | ||
| 360 | register int tmp; | ||
| 361 | 781265 | OPEN_READER(re, s); | |
| 362 | av_assert2(n>0 && n<=25); | ||
| 363 | 781265 | UPDATE_CACHE_LE(re, s); | |
| 364 | 781265 | tmp = SHOW_UBITS_LE(re, s, n); | |
| 365 | 781265 | LAST_SKIP_BITS(re, s, n); | |
| 366 | 781265 | CLOSE_READER(re, s); | |
| 367 | 781265 | return tmp; | |
| 368 | } | ||
| 369 | |||
| 370 | /** | ||
| 371 | * Show 1-25 bits. | ||
| 372 | */ | ||
| 373 | 118127529 | static inline unsigned int show_bits(GetBitContext *s, int n) | |
| 374 | { | ||
| 375 | register unsigned int tmp; | ||
| 376 | 118127529 | OPEN_READER_NOSIZE(re, s); | |
| 377 | av_assert2(n>0 && n<=25); | ||
| 378 | 118127529 | UPDATE_CACHE(re, s); | |
| 379 | 118127529 | tmp = SHOW_UBITS(re, s, n); | |
| 380 | 118127529 | return tmp; | |
| 381 | } | ||
| 382 | |||
| 383 | 115660148 | static inline void skip_bits(GetBitContext *s, int n) | |
| 384 | { | ||
| 385 | 115660148 | OPEN_READER_NOSIZE_NOCACHE(re, s); | |
| 386 | 32631057 | OPEN_READER_SIZE(re, s); | |
| 387 | 115660148 | LAST_SKIP_BITS(re, s, n); | |
| 388 | 115660148 | CLOSE_READER(re, s); | |
| 389 | 115660148 | } | |
| 390 | |||
| 391 | 473696026 | static inline unsigned int get_bits1(GetBitContext *s) | |
| 392 | { | ||
| 393 | 473696026 | unsigned int index = s->index; | |
| 394 | 473696026 | uint8_t result = s->buffer[index >> 3]; | |
| 395 | #ifdef BITSTREAM_READER_LE | ||
| 396 | 265291985 | result >>= index & 7; | |
| 397 | 265291985 | result &= 1; | |
| 398 | #else | ||
| 399 | 208404041 | result <<= index & 7; | |
| 400 | 208404041 | result >>= 8 - 1; | |
| 401 | #endif | ||
| 402 | #if !UNCHECKED_BITSTREAM_READER | ||
| 403 |
2/2✓ Branch 0 taken 429957759 times.
✓ Branch 1 taken 26977 times.
|
429984736 | if (s->index < s->size_in_bits_plus8) |
| 404 | #endif | ||
| 405 | 473669049 | index++; | |
| 406 | 473696026 | s->index = index; | |
| 407 | |||
| 408 | 473696026 | return result; | |
| 409 | } | ||
| 410 | |||
| 411 | 43332 | static inline unsigned int show_bits1(GetBitContext *s) | |
| 412 | { | ||
| 413 | 43332 | return show_bits(s, 1); | |
| 414 | } | ||
| 415 | |||
| 416 | 392261 | static inline void skip_bits1(GetBitContext *s) | |
| 417 | { | ||
| 418 | 392261 | skip_bits(s, 1); | |
| 419 | 392261 | } | |
| 420 | |||
| 421 | /** | ||
| 422 | * Read 0-32 bits. | ||
| 423 | */ | ||
| 424 | 57476302 | static inline unsigned int get_bits_long(GetBitContext *s, int n) | |
| 425 | { | ||
| 426 | av_assert2(n>=0 && n<=32); | ||
| 427 |
2/2✓ Branch 0 taken 3404960 times.
✓ Branch 1 taken 54071342 times.
|
57476302 | if (!n) { |
| 428 | 3404960 | return 0; | |
| 429 | } else if ((!HAVE_FAST_64BIT || av_builtin_constant_p(n <= MIN_CACHE_BITS)) | ||
| 430 | && n <= MIN_CACHE_BITS) { | ||
| 431 | return get_bits(s, n); | ||
| 432 | } else { | ||
| 433 | #if HAVE_FAST_64BIT | ||
| 434 | unsigned tmp; | ||
| 435 | 54071342 | OPEN_READER(re, s); | |
| 436 | 54071342 | UPDATE_CACHE_32(re, s); | |
| 437 | 54071342 | tmp = SHOW_UBITS(re, s, n); | |
| 438 | 54071342 | LAST_SKIP_BITS(re, s, n); | |
| 439 | 54071342 | CLOSE_READER(re, s); | |
| 440 | 54071342 | return tmp; | |
| 441 | #else | ||
| 442 | #ifdef BITSTREAM_READER_LE | ||
| 443 | unsigned ret = get_bits(s, 16); | ||
| 444 | return ret | (get_bits(s, n - 16) << 16); | ||
| 445 | #else | ||
| 446 | unsigned ret = get_bits(s, 16) << (n - 16); | ||
| 447 | return ret | get_bits(s, n - 16); | ||
| 448 | #endif | ||
| 449 | #endif | ||
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | /** | ||
| 454 | * Read 0-64 bits. | ||
| 455 | */ | ||
| 456 | 11450 | static inline uint64_t get_bits64(GetBitContext *s, int n) | |
| 457 | { | ||
| 458 |
2/2✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 5075 times.
|
11450 | if (n <= 32) { |
| 459 | 6375 | return get_bits_long(s, n); | |
| 460 | } else { | ||
| 461 | #ifdef BITSTREAM_READER_LE | ||
| 462 | 1 | uint64_t ret = get_bits_long(s, 32); | |
| 463 | 1 | return ret | (uint64_t) get_bits_long(s, n - 32) << 32; | |
| 464 | #else | ||
| 465 | 5074 | uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32; | |
| 466 | 5074 | return ret | get_bits_long(s, 32); | |
| 467 | #endif | ||
| 468 | } | ||
| 469 | } | ||
| 470 | |||
| 471 | /** | ||
| 472 | * Read 0-32 bits as a signed integer. | ||
| 473 | */ | ||
| 474 | 4766805 | static inline int get_sbits_long(GetBitContext *s, int n) | |
| 475 | { | ||
| 476 | // sign_extend(x, 0) is undefined | ||
| 477 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4766805 times.
|
4766805 | if (!n) |
| 478 | ✗ | return 0; | |
| 479 | |||
| 480 | 4766805 | return sign_extend(get_bits_long(s, n), n); | |
| 481 | } | ||
| 482 | |||
| 483 | /** | ||
| 484 | * Read 0-64 bits as a signed integer. | ||
| 485 | */ | ||
| 486 | 4624 | static inline int64_t get_sbits64(GetBitContext *s, int n) | |
| 487 | { | ||
| 488 | // sign_extend(x, 0) is undefined | ||
| 489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4624 times.
|
4624 | if (!n) |
| 490 | ✗ | return 0; | |
| 491 | |||
| 492 | 4624 | return sign_extend64(get_bits64(s, n), n); | |
| 493 | } | ||
| 494 | |||
| 495 | /** | ||
| 496 | * Show 0-32 bits. | ||
| 497 | */ | ||
| 498 | 3532944 | static inline unsigned int show_bits_long(GetBitContext *s, int n) | |
| 499 | { | ||
| 500 |
2/2✓ Branch 0 taken 19098 times.
✓ Branch 1 taken 3513846 times.
|
3532944 | if (n <= MIN_CACHE_BITS) { |
| 501 | 19098 | return show_bits(s, n); | |
| 502 | } else { | ||
| 503 | 3513846 | GetBitContext gb = *s; | |
| 504 | 3513846 | return get_bits_long(&gb, n); | |
| 505 | } | ||
| 506 | } | ||
| 507 | |||
| 508 | |||
| 509 | /** | ||
| 510 | * Initialize GetBitContext. | ||
| 511 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
| 512 | * larger than the actual read bits because some optimized bitstream | ||
| 513 | * readers read 32 or 64 bit at once and could read over the end | ||
| 514 | * @param bit_size the size of the buffer in bits | ||
| 515 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||
| 516 | */ | ||
| 517 | 42520314 | static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer, | |
| 518 | int bit_size) | ||
| 519 | { | ||
| 520 | 42520314 | int ret = 0; | |
| 521 | |||
| 522 |
4/6✓ Branch 0 taken 42520314 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42520314 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 42520306 times.
|
42520314 | if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) { |
| 523 | 8 | bit_size = 0; | |
| 524 | 8 | buffer = NULL; | |
| 525 | 8 | ret = AVERROR_INVALIDDATA; | |
| 526 | } | ||
| 527 | |||
| 528 | 42520314 | s->buffer = buffer; | |
| 529 | 42520314 | s->size_in_bits = bit_size; | |
| 530 | 42520314 | s->size_in_bits_plus8 = bit_size + 8; | |
| 531 | 42520314 | s->index = 0; | |
| 532 | |||
| 533 | 42520314 | return ret; | |
| 534 | } | ||
| 535 | |||
| 536 | /** | ||
| 537 | * Initialize GetBitContext. | ||
| 538 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
| 539 | * larger than the actual read bits because some optimized bitstream | ||
| 540 | * readers read 32 or 64 bit at once and could read over the end | ||
| 541 | * @param byte_size the size of the buffer in bytes | ||
| 542 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||
| 543 | */ | ||
| 544 | 13401177 | static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer, | |
| 545 | int byte_size) | ||
| 546 | { | ||
| 547 |
2/4✓ Branch 0 taken 13401177 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13401177 times.
|
13401177 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
| 548 | ✗ | byte_size = -1; | |
| 549 | 13401177 | return init_get_bits(s, buffer, byte_size * 8); | |
| 550 | } | ||
| 551 | |||
| 552 | 200 | static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer, | |
| 553 | int byte_size) | ||
| 554 | { | ||
| 555 |
2/4✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 200 times.
|
200 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
| 556 | ✗ | byte_size = -1; | |
| 557 | 200 | return init_get_bits(s, buffer, byte_size * 8); | |
| 558 | } | ||
| 559 | |||
| 560 | 257609 | static inline const uint8_t *align_get_bits(GetBitContext *s) | |
| 561 | { | ||
| 562 | 257609 | int n = -get_bits_count(s) & 7; | |
| 563 |
2/2✓ Branch 0 taken 73147 times.
✓ Branch 1 taken 184462 times.
|
257609 | if (n) |
| 564 | 73147 | skip_bits(s, n); | |
| 565 | 257609 | return s->buffer + (s->index >> 3); | |
| 566 | } | ||
| 567 | |||
| 568 | /** | ||
| 569 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. | ||
| 570 | * If the vlc code is invalid and max_depth>1, then the number of bits removed | ||
| 571 | * is undefined. | ||
| 572 | */ | ||
| 573 | #define GET_VLC(code, name, gb, table, bits, max_depth) \ | ||
| 574 | do { \ | ||
| 575 | int n, nb_bits; \ | ||
| 576 | unsigned int index; \ | ||
| 577 | \ | ||
| 578 | index = SHOW_UBITS(name, gb, bits); \ | ||
| 579 | code = table[index].sym; \ | ||
| 580 | n = table[index].len; \ | ||
| 581 | \ | ||
| 582 | if (max_depth > 1 && n < 0) { \ | ||
| 583 | LAST_SKIP_BITS(name, gb, bits); \ | ||
| 584 | UPDATE_CACHE(name, gb); \ | ||
| 585 | \ | ||
| 586 | nb_bits = -n; \ | ||
| 587 | \ | ||
| 588 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ | ||
| 589 | code = table[index].sym; \ | ||
| 590 | n = table[index].len; \ | ||
| 591 | if (max_depth > 2 && n < 0) { \ | ||
| 592 | LAST_SKIP_BITS(name, gb, nb_bits); \ | ||
| 593 | UPDATE_CACHE(name, gb); \ | ||
| 594 | \ | ||
| 595 | nb_bits = -n; \ | ||
| 596 | \ | ||
| 597 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ | ||
| 598 | code = table[index].sym; \ | ||
| 599 | n = table[index].len; \ | ||
| 600 | } \ | ||
| 601 | } \ | ||
| 602 | SKIP_BITS(name, gb, n); \ | ||
| 603 | } while (0) | ||
| 604 | |||
| 605 | #define GET_RL_VLC(level, run, name, gb, table, bits, \ | ||
| 606 | max_depth, need_update) \ | ||
| 607 | do { \ | ||
| 608 | int n, nb_bits; \ | ||
| 609 | unsigned int index; \ | ||
| 610 | \ | ||
| 611 | index = SHOW_UBITS(name, gb, bits); \ | ||
| 612 | level = table[index].level; \ | ||
| 613 | n = table[index].len8; \ | ||
| 614 | \ | ||
| 615 | if (max_depth > 1 && n < 0) { \ | ||
| 616 | SKIP_BITS(name, gb, bits); \ | ||
| 617 | if (need_update) { \ | ||
| 618 | UPDATE_CACHE(name, gb); \ | ||
| 619 | } \ | ||
| 620 | \ | ||
| 621 | nb_bits = -n; \ | ||
| 622 | \ | ||
| 623 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ | ||
| 624 | level = table[index].level; \ | ||
| 625 | n = table[index].len8; \ | ||
| 626 | if (max_depth > 2 && n < 0) { \ | ||
| 627 | LAST_SKIP_BITS(name, gb, nb_bits); \ | ||
| 628 | if (need_update) { \ | ||
| 629 | UPDATE_CACHE(name, gb); \ | ||
| 630 | } \ | ||
| 631 | nb_bits = -n; \ | ||
| 632 | \ | ||
| 633 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ | ||
| 634 | level = table[index].level; \ | ||
| 635 | n = table[index].len8; \ | ||
| 636 | } \ | ||
| 637 | } \ | ||
| 638 | run = table[index].run; \ | ||
| 639 | SKIP_BITS(name, gb, n); \ | ||
| 640 | } while (0) | ||
| 641 | |||
| 642 | /** | ||
| 643 | * Parse a vlc code. | ||
| 644 | * @param bits is the number of bits which will be read at once, must be | ||
| 645 | * identical to nb_bits in vlc_init() | ||
| 646 | * @param max_depth is the number of times bits bits must be read to completely | ||
| 647 | * read the longest vlc code | ||
| 648 | * = (max_vlc_length + bits - 1) / bits | ||
| 649 | * @returns the code parsed or -1 if no vlc matches | ||
| 650 | */ | ||
| 651 | 287447589 | static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, | |
| 652 | int bits, int max_depth) | ||
| 653 | { | ||
| 654 | int code; | ||
| 655 | |||
| 656 | 287447589 | OPEN_READER(re, s); | |
| 657 | 287447589 | UPDATE_CACHE(re, s); | |
| 658 | |||
| 659 |
10/10✓ Branch 0 taken 7704704 times.
✓ Branch 1 taken 176264624 times.
✓ Branch 2 taken 103832435 times.
✓ Branch 3 taken 32192220 times.
✓ Branch 4 taken 142300757 times.
✓ Branch 5 taken 102488 times.
✓ Branch 6 taken 22893178 times.
✓ Branch 7 taken 2200198 times.
✓ Branch 8 taken 630161 times.
✓ Branch 9 taken 22262445 times.
|
287447589 | GET_VLC(code, re, s, table, bits, max_depth); |
| 660 | |||
| 661 | 287447589 | CLOSE_READER(re, s); | |
| 662 | |||
| 663 | 287447589 | return code; | |
| 664 | } | ||
| 665 | |||
| 666 | static inline int get_vlc_multi(GetBitContext *s, uint8_t *dst, | ||
| 667 | const VLC_MULTI_ELEM *const Jtable, | ||
| 668 | const VLCElem *const table, | ||
| 669 | const int bits, const int max_depth, | ||
| 670 | const int symbols_size) | ||
| 671 | { | ||
| 672 | dst[0] = get_vlc2(s, table, bits, max_depth); | ||
| 673 | return 1; | ||
| 674 | } | ||
| 675 | |||
| 676 | 378883 | static inline int decode012(GetBitContext *gb) | |
| 677 | { | ||
| 678 | int n; | ||
| 679 | 378883 | n = get_bits1(gb); | |
| 680 |
2/2✓ Branch 0 taken 263935 times.
✓ Branch 1 taken 114948 times.
|
378883 | if (n == 0) |
| 681 | 263935 | return 0; | |
| 682 | else | ||
| 683 | 114948 | return get_bits1(gb) + 1; | |
| 684 | } | ||
| 685 | |||
| 686 | 191971 | static inline int decode210(GetBitContext *gb) | |
| 687 | { | ||
| 688 |
2/2✓ Branch 1 taken 135836 times.
✓ Branch 2 taken 56135 times.
|
191971 | if (get_bits1(gb)) |
| 689 | 135836 | return 0; | |
| 690 | else | ||
| 691 | 56135 | return 2 - get_bits1(gb); | |
| 692 | } | ||
| 693 | |||
| 694 | 272152232 | static inline int get_bits_left(GetBitContext *gb) | |
| 695 | { | ||
| 696 | 272152232 | return gb->size_in_bits - get_bits_count(gb); | |
| 697 | } | ||
| 698 | |||
| 699 | 141858 | static inline int skip_1stop_8data_bits(GetBitContext *gb) | |
| 700 | { | ||
| 701 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 141858 times.
|
141858 | if (get_bits_left(gb) <= 0) |
| 702 | ✗ | return AVERROR_INVALIDDATA; | |
| 703 | |||
| 704 |
2/2✓ Branch 1 taken 3114 times.
✓ Branch 2 taken 141858 times.
|
144972 | while (get_bits1(gb)) { |
| 705 | 3114 | skip_bits(gb, 8); | |
| 706 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3114 times.
|
3114 | if (get_bits_left(gb) <= 0) |
| 707 | ✗ | return AVERROR_INVALIDDATA; | |
| 708 | } | ||
| 709 | |||
| 710 | 141858 | return 0; | |
| 711 | } | ||
| 712 | |||
| 713 | #endif // CACHED_BITSTREAM_READER | ||
| 714 | |||
| 715 | #endif /* AVCODEC_GET_BITS_H */ | ||
| 716 |