| 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 | /** |
| 23 | * @file |
| 24 | * bitstream reader API header. |
| 25 | */ |
| 26 |
| 27 | #ifndef AVCODEC_GET_BITS_H |
| 28 | #define AVCODEC_GET_BITS_H |
| 29 |
| 30 | #include <stdint.h> |
| 31 |
| 32 | #ifndef NEG_USR32 |
| 33 | # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) |
| 34 | #endif |
| 35 |
| 36 | /* |
| 37 | * Safe bitstream reading: |
| 38 | * optionally, the get_bits API can check to ensure that we |
| 39 | * don't read past input buffer boundaries. This is protected |
| 40 | * with CONFIG_SAFE_BITSTREAM_READER at the global level, and |
| 41 | * then below that with UNCHECKED_BITSTREAM_READER at the per- |
| 42 | * decoder level. This means that decoders that check internally |
| 43 | * can "#define UNCHECKED_BITSTREAM_READER 1" to disable |
| 44 | * overread checks. |
| 45 | * Boundary checking causes a minor performance penalty so for |
| 46 | * applications that won't want/need this, it can be disabled |
| 47 | * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0". |
| 48 | */ |
| 49 | #ifndef UNCHECKED_BITSTREAM_READER |
| 50 | #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER |
| 51 | #endif |
| 52 |
| 53 | #ifndef CACHED_BITSTREAM_READER |
| 54 | #define CACHED_BITSTREAM_READER 0 |
| 55 | #endif |
| 56 |
| 57 | typedef struct GetBitContext { |
| 58 | const uint8_t *buffer, *buffer_end; |
| 59 | #if CACHED_BITSTREAM_READER |
| 60 | uint64_t cache; |
| 61 | unsigned bits_left; |
| 62 | #endif |
| 63 | int index; |
| 64 | int size_in_bits; |
| 65 | int size_in_bits_plus8; |
| 66 | } GetBitContext; |
| 67 |
| 68 | static inline unsigned int get_bits(GetBitContext *s, int n); |
| 69 | static inline void skip_bits(GetBitContext *s, int n); |
| 70 | static inline unsigned int show_bits(GetBitContext *s, int n); |
| 71 |
| 72 | /* Bitstream reader API docs: |
| 73 | * name |
| 74 | * arbitrary name which is used as prefix for the internal variables |
| 75 | * |
| 76 | * gb |
| 77 | * getbitcontext |
| 78 | * |
| 79 | * OPEN_READER(name, gb) |
| 80 | * load gb into local variables |
| 81 | * |
| 82 | * CLOSE_READER(name, gb) |
| 83 | * store local vars in gb |
| 84 | * |
| 85 | * UPDATE_CACHE(name, gb) |
| 86 | * Refill the internal cache from the bitstream. |
| 87 | * After this call at least MIN_CACHE_BITS will be available. |
| 88 | * |
| 89 | * GET_CACHE(name, gb) |
| 90 | * Will output the contents of the internal cache, |
| 91 | * next bit is MSB of 32 or 64 bits (FIXME 64 bits). |
| 92 | * |
| 93 | * SHOW_UBITS(name, gb, num) |
| 94 | * Will return the next num bits. |
| 95 | * |
| 96 | * SHOW_SBITS(name, gb, num) |
| 97 | * Will return the next num bits and do sign extension. |
| 98 | * |
| 99 | * SKIP_BITS(name, gb, num) |
| 100 | * Will skip over the next num bits. |
| 101 | * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER. |
| 102 | * |
| 103 | * SKIP_CACHE(name, gb, num) |
| 104 | * Will remove the next num bits from the cache (note SKIP_COUNTER |
| 105 | * MUST be called before UPDATE_CACHE / CLOSE_READER). |
| 106 | * |
| 107 | * SKIP_COUNTER(name, gb, num) |
| 108 | * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS). |
| 109 | * |
| 110 | * LAST_SKIP_BITS(name, gb, num) |
| 111 | * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER. |
| 112 | * |
| 113 | * BITS_LEFT(name, gb) |
| 114 | * Return the number of bits left |
| 115 | * |
| 116 | * For examples see get_bits, show_bits, skip_bits, get_vlc. |
| 117 | */ |
| 118 |
| 119 | #if CACHED_BITSTREAM_READER |
| 120 | # define MIN_CACHE_BITS 64 |
| 121 | #elif defined LONG_BITSTREAM_READER |
| 122 | # define MIN_CACHE_BITS 32 |
| 123 | #else |
| 124 | # define MIN_CACHE_BITS 25 |
| 125 | #endif |
| 126 |
| 127 | #if !CACHED_BITSTREAM_READER |
| 128 |
| 129 | #define OPEN_READER_NOSIZE(name, gb) \ |
| 130 | unsigned int name ## _index = (gb)->index; \ |
| 131 | unsigned int av_unused name ## _cache |
| 132 |
| 133 | #if UNCHECKED_BITSTREAM_READER |
| 134 | #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb) |
| 135 |
| 136 | #define BITS_AVAILABLE(name, gb) 1 |
| 137 | #else |
| 138 | #define OPEN_READER(name, gb) \ |
| 139 | OPEN_READER_NOSIZE(name, gb); \ |
| 140 | unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8 |
| 141 |
| 142 | #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8 |
| 143 | #endif |
| 144 |
| 145 | #define CLOSE_READER(name, gb) (gb)->index = name ## _index |
| 146 |
| 147 | # ifdef LONG_BITSTREAM_READER |
| 148 |
| 149 | # define UPDATE_CACHE_LE(name, gb) name ## _cache = \ |
| 150 | AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7) |
| 151 |
| 152 | # define UPDATE_CACHE_BE(name, gb) name ## _cache = \ |
| 153 | AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7)) |
| 154 |
| 155 | #else |
| 156 |
| 157 | # define UPDATE_CACHE_LE(name, gb) name ## _cache = \ |
| 158 | AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7) |
| 159 |
| 160 | # define UPDATE_CACHE_BE(name, gb) name ## _cache = \ |
| 161 | AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) |
| 162 |
| 163 | #endif |
| 164 |
| 165 |
| 166 | #ifdef BITSTREAM_READER_LE |
| 167 |
| 168 | # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb) |
| 169 |
| 170 | # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num) |
| 171 |
| 172 | #else |
| 173 |
| 174 | # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb) |
| 175 |
| 176 | # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num) |
| 177 |
| 178 | #endif |
| 179 |
| 180 | #if UNCHECKED_BITSTREAM_READER |
| 181 | # define SKIP_COUNTER(name, gb, num) name ## _index += (num) |
| 182 | #else |
| 183 | # define SKIP_COUNTER(name, gb, num) \ |
| 184 | name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num)) |
| 185 | #endif |
| 186 |
| 187 | #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index)) |
| 188 |
| 189 | #define SKIP_BITS(name, gb, num) \ |
| 190 | do { \ |
| 191 | SKIP_CACHE(name, gb, num); \ |
| 192 | SKIP_COUNTER(name, gb, num); \ |
| 193 | } while (0) |
| 194 |
| 195 | #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) |
| 196 |
| 197 | #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num) |
| 198 | #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num) |
| 199 |
| 200 | #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num) |
| 201 | #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num) |
| 202 |
| 203 | #ifdef BITSTREAM_READER_LE |
| 204 | # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num) |
| 205 | # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num) |
| 206 | #else |
| 207 | # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num) |
| 208 | # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num) |
| 209 | #endif |
| 210 |
| 211 | #define GET_CACHE(name, gb) ((uint32_t) name ## _cache) |
| 212 |
| 213 | #endif |
| 214 |
| 215 | static inline int get_bits_count(const GetBitContext *s) |
| 216 | { |
| 217 | #if CACHED_BITSTREAM_READER |
| 218 | return s->index - s->bits_left; |
| 219 | #else |
| 220 | return s->index; |
| 221 | #endif |
| 222 | } |
| 223 |
| 224 | #if CACHED_BITSTREAM_READER |
| 225 | static inline void refill_32(GetBitContext *s, int is_le) |
| 226 | { |
| 227 | #if !UNCHECKED_BITSTREAM_READER |
| 228 | if (s->index >> 3 >= s->buffer_end - s->buffer) |
| 229 | return; |
| 230 | #endif |
| 231 |
| 232 | if (is_le) |
| 233 | s->cache = (uint64_t)AV_RL32(s->buffer + (s->index >> 3)) << s->bits_left | s->cache; |
| 234 | else |
| 235 | s->cache = s->cache | (uint64_t)AV_RB32(s->buffer + (s->index >> 3)) << (32 - s->bits_left); |
| 236 | s->index += 32; |
| 237 | s->bits_left += 32; |
| 238 | } |
| 239 |
| 240 | static inline void refill_64(GetBitContext *s, int is_le) |
| 241 | { |
| 242 | #if !UNCHECKED_BITSTREAM_READER |
| 243 | if (s->index >> 3 >= s->buffer_end - s->buffer) |
| 244 | return; |
| 245 | #endif |
| 246 |
| 247 | if (is_le) |
| 248 | s->cache = AV_RL64(s->buffer + (s->index >> 3)); |
| 249 | else |
| 250 | s->cache = AV_RB64(s->buffer + (s->index >> 3)); |
| 251 | s->index += 64; |
| 252 | s->bits_left = 64; |
| 253 | } |
| 254 |
| 255 | static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le) |
| 256 | { |
| 257 | uint64_t ret; |
| 258 | av_assert2(n>0 && n<=63); |
| 259 | if (is_le) { |
| 260 | ret = s->cache & ((UINT64_C(1) << n) - 1); |
| 261 | s->cache >>= n; |
| 262 | } else { |
| 263 | ret = s->cache >> (64 - n); |
| 264 | s->cache <<= n; |
| 265 | } |
| 266 | s->bits_left -= n; |
| 267 | return ret; |
| 268 | } |
| 269 |
| 270 | static inline unsigned show_val(const GetBitContext *s, unsigned n) |
| 271 | { |
| 272 | #ifdef BITSTREAM_READER_LE |
| 273 | return s->cache & ((UINT64_C(1) << n) - 1); |
| 274 | #else |
| 275 | return s->cache >> (64 - n); |
| 276 | #endif |
| 277 | } |
| 278 | #endif |
| 279 |
| 280 | /** |
| 281 | * Skips the specified number of bits. |
| 282 | * @param n the number of bits to skip, |
| 283 | * For the UNCHECKED_BITSTREAM_READER this must not cause the distance |
| 284 | * from the start to overflow int32_t. Staying within the bitstream + padding |
| 285 | * is sufficient, too. |
| 286 | */ |
| 287 | static inline void skip_bits_long(GetBitContext *s, int n) |
| 288 | { |
| 289 | #if CACHED_BITSTREAM_READER |
| 290 | skip_bits(s, n); |
| 291 | #else |
| 292 | #if UNCHECKED_BITSTREAM_READER |
| 293 | s->index += n; |
| 294 | #else |
| 295 | s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index); |
| 296 | #endif |
| 297 | #endif |
| 298 | } |
| 299 |
| 300 | #if CACHED_BITSTREAM_READER |
| 301 | static inline void skip_remaining(GetBitContext *s, unsigned n) |
| 302 | { |
| 303 | #ifdef BITSTREAM_READER_LE |
| 304 | s->cache >>= n; |
| 305 | #else |
| 306 | s->cache <<= n; |
| 307 | #endif |
| 308 | s->bits_left -= n; |
| 309 | } |
| 310 | #endif |
| 311 |
| 312 | /** |
| 313 | * Read 1-25 bits. |
| 314 | */ |
| 315 | static inline unsigned int get_bits(GetBitContext *s, int n) |
| 316 | { |
| 317 | unsigned int tmp; |
| 318 | #if CACHED_BITSTREAM_READER |
| 319 |
| 320 | av_assert2(n>0 && n<=32); |
| 321 | if (n > s->bits_left) { |
| 322 | #ifdef BITSTREAM_READER_LE |
| 323 | refill_32(s, 1); |
| 324 | #else |
| 325 | refill_32(s, 0); |
| 326 | #endif |
| 327 | if (s->bits_left < 32) |
| 328 | s->bits_left = n; |
| 329 | } |
| 330 |
| 331 | #ifdef BITSTREAM_READER_LE |
| 332 | tmp = get_val(s, n, 1); |
| 333 | #else |
| 334 | tmp = get_val(s, n, 0); |
| 335 | #endif |
| 336 | #else |
| 337 | OPEN_READER(re, s); |
| 338 | av_assert2(n>0 && n<=25); |
| 339 | UPDATE_CACHE(re, s); |
| 340 | tmp = SHOW_UBITS(re, s, n); |
| 341 | LAST_SKIP_BITS(re, s, n); |
| 342 | CLOSE_READER(re, s); |
| 343 | #endif |
| 344 | av_assert2(tmp < UINT64_C(1) << n); |
| 345 | return tmp; |
| 346 | } |
| 347 |
| 348 | static inline void skip_bits(GetBitContext *s, int n) |
| 349 | { |
| 350 | #if CACHED_BITSTREAM_READER |
| 351 | if (n < s->bits_left) |
| 352 | skip_remaining(s, n); |
| 353 | else { |
| 354 | n -= s->bits_left; |
| 355 | s->cache = 0; |
| 356 | s->bits_left = 0; |
| 357 |
| 358 | if (n >= 64) { |
| 359 | unsigned skip = (n / 8) * 8; |
| 360 |
| 361 | n -= skip; |
| 362 | s->index += skip; |
| 363 | } |
| 364 | #ifdef BITSTREAM_READER_LE |
| 365 | refill_64(s, 1); |
| 366 | #else |
| 367 | refill_64(s, 0); |
| 368 | #endif |
| 369 | if (n) |
| 370 | skip_remaining(s, n); |
| 371 | } |
| 372 | #else |
| 373 | OPEN_READER(re, s); |
| 374 | LAST_SKIP_BITS(re, s, n); |
| 375 | CLOSE_READER(re, s); |
| 376 | #endif |
| 377 | } |
| 378 |
| 379 | static inline unsigned int get_bits1(GetBitContext *s) |
| 380 | { |
| 381 | #if CACHED_BITSTREAM_READER |
| 382 | if (!s->bits_left) |
| 383 | #ifdef BITSTREAM_READER_LE |
| 384 | refill_64(s, 1); |
| 385 | #else |
| 386 | refill_64(s, 0); |
| 387 | #endif |
| 388 |
| 389 | #ifdef BITSTREAM_READER_LE |
| 390 | return get_val(s, 1, 1); |
| 391 | #else |
| 392 | return get_val(s, 1, 0); |
| 393 | #endif |
| 394 | #else |
| 395 | unsigned int index = s->index; |
| 396 | uint8_t result = s->buffer[index >> 3]; |
| 397 | #ifdef BITSTREAM_READER_LE |
| 398 | result >>= index & 7; |
| 399 | result &= 1; |
| 400 | #else |
| 401 | result <<= index & 7; |
| 402 | result >>= 8 - 1; |
| 403 | #endif |
| 404 | #if !UNCHECKED_BITSTREAM_READER |
| 405 | if (s->index < s->size_in_bits_plus8) |
| 406 | #endif |
| 407 | index++; |
| 408 | s->index = index; |
| 409 |
| 410 | return result; |
| 411 | #endif |
| 412 | } |
| 413 |
| 414 | static inline void skip_bits1(GetBitContext *s) |
| 415 | { |
| 416 | skip_bits(s, 1); |
| 417 | } |
| 418 |
| 419 | /** |
| 420 | * Read 0-32 bits. |
| 421 | */ |
| 422 | static inline unsigned int get_bits_long(GetBitContext *s, int n) |
| 423 | { |
| 424 | av_assert2(n>=0 && n<=32); |
| 425 | if (!n) { |
| 426 | return 0; |
| 427 | #if CACHED_BITSTREAM_READER |
| 428 | } |
| 429 | return get_bits(s, n); |
| 430 | #else |
| 431 | } else if (n <= MIN_CACHE_BITS) { |
| 432 | return get_bits(s, n); |
| 433 | } else { |
| 434 | #ifdef BITSTREAM_READER_LE |
| 435 | unsigned ret = get_bits(s, 16); |
| 436 | return ret | (get_bits(s, n - 16) << 16); |
| 437 | #else |
| 438 | unsigned ret = get_bits(s, 16) << (n - 16); |
| 439 | return ret | get_bits(s, n - 16); |
| 440 | #endif |
| 441 | } |
| 442 | #endif |
| 443 | } |
| 444 |
| 445 | /** |
| 446 | * Read 0-64 bits. |
| 447 | */ |
| 448 | static inline uint64_t get_bits64(GetBitContext *s, int n) |
| 449 | { |
| 450 | if (n <= 32) { |
| 451 | return get_bits_long(s, n); |
| 452 | } else { |
| 453 | #ifdef BITSTREAM_READER_LE |
| 454 | uint64_t ret = get_bits_long(s, 32); |
| 455 | return ret | (uint64_t) get_bits_long(s, n - 32) << 32; |
| 456 | #else |
| 457 | uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32; |
| 458 | return ret | get_bits_long(s, 32); |
| 459 | #endif |
| 460 | } |
| 461 | } |
| 462 |
| 463 | static inline int check_marker(void *logctx, GetBitContext *s, const char *msg) |
| 464 | { |
| 465 | int bit = get_bits1(s); |
| 466 | if (!bit) |
| 467 | av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n", |
| 468 | get_bits_count(s) - 1, s->size_in_bits, msg); |
| 469 |
| 470 | return bit; |
| 471 | } |
| 472 |
| 473 | static inline int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer, |
| 474 | int bit_size, int is_le) |
| 475 | { |
| 476 | int buffer_size; |
| 477 | int ret = 0; |
| 478 |
| 479 | if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) { |
| 480 | bit_size = 0; |
| 481 | buffer = NULL; |
| 482 | ret = AVERROR_INVALIDDATA; |
| 483 | } |
| 484 |
| 485 | buffer_size = (bit_size + 7) >> 3; |
| 486 |
| 487 | s->buffer = buffer; |
| 488 | s->size_in_bits = bit_size; |
| 489 | s->size_in_bits_plus8 = bit_size + 8; |
| 490 | s->buffer_end = buffer + buffer_size; |
| 491 | s->index = 0; |
| 492 |
| 493 | #if CACHED_BITSTREAM_READER |
| 494 | s->cache = 0; |
| 495 | s->bits_left = 0; |
| 496 | refill_64(s, is_le); |
| 497 | #endif |
| 498 |
| 499 | return ret; |
| 500 | } |
| 501 |
| 502 | /** |
| 503 | * Initialize GetBitContext. |
| 504 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes |
| 505 | * larger than the actual read bits because some optimized bitstream |
| 506 | * readers read 32 or 64 bit at once and could read over the end |
| 507 | * @param bit_size the size of the buffer in bits |
| 508 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. |
| 509 | */ |
| 510 | static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer, |
| 511 | int bit_size) |
| 512 | { |
| 513 | #ifdef BITSTREAM_READER_LE |
| 514 | return init_get_bits_xe(s, buffer, bit_size, 1); |
| 515 | #else |
| 516 | return init_get_bits_xe(s, buffer, bit_size, 0); |
| 517 | #endif |
| 518 | } |
| 519 |
| 520 | /** |
| 521 | * Initialize GetBitContext. |
| 522 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes |
| 523 | * larger than the actual read bits because some optimized bitstream |
| 524 | * readers read 32 or 64 bit at once and could read over the end |
| 525 | * @param byte_size the size of the buffer in bytes |
| 526 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. |
| 527 | */ |
| 528 | static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer, |
| 529 | int byte_size) |
| 530 | { |
| 531 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
| 532 | byte_size = -1; |
| 533 | return init_get_bits(s, buffer, byte_size * 8); |
| 534 | } |
| 535 |
| 536 | static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer, |
| 537 | int byte_size) |
| 538 | { |
| 539 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
| 540 | byte_size = -1; |
| 541 | return init_get_bits_xe(s, buffer, byte_size * 8, 1); |
| 542 | } |
| 543 |
| 544 | static inline const uint8_t *align_get_bits(GetBitContext *s) |
| 545 | { |
| 546 | int n = -get_bits_count(s) & 7; |
| 547 | if (n) |
| 548 | skip_bits(s, n); |
| 549 | return s->buffer + (s->index >> 3); |
| 550 | } |
| 551 |
| 552 | /** |
| 553 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. |
| 554 | * If the vlc code is invalid and max_depth>1, then the number of bits removed |
| 555 | * is undefined. |
| 556 | */ |
| 557 | #define GET_VLC(code, name, gb, table, bits, max_depth) \ |
| 558 | do { \ |
| 559 | int n, nb_bits; \ |
| 560 | unsigned int index; \ |
| 561 | \ |
| 562 | index = SHOW_UBITS(name, gb, bits); \ |
| 563 | code = table[index][0]; \ |
| 564 | n = table[index][1]; \ |
| 565 | \ |
| 566 | if (max_depth > 1 && n < 0) { \ |
| 567 | LAST_SKIP_BITS(name, gb, bits); \ |
| 568 | UPDATE_CACHE(name, gb); \ |
| 569 | \ |
| 570 | nb_bits = -n; \ |
| 571 | \ |
| 572 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ |
| 573 | code = table[index][0]; \ |
| 574 | n = table[index][1]; \ |
| 575 | if (max_depth > 2 && n < 0) { \ |
| 576 | LAST_SKIP_BITS(name, gb, nb_bits); \ |
| 577 | UPDATE_CACHE(name, gb); \ |
| 578 | \ |
| 579 | nb_bits = -n; \ |
| 580 | \ |
| 581 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ |
| 582 | code = table[index][0]; \ |
| 583 | n = table[index][1]; \ |
| 584 | } \ |
| 585 | } \ |
| 586 | SKIP_BITS(name, gb, n); \ |
| 587 | } while (0) |
| 588 |
| 589 | #define GET_RL_VLC(level, run, name, gb, table, bits, \ |
| 590 | max_depth, need_update) \ |
| 591 | do { \ |
| 592 | int n, nb_bits; \ |
| 593 | unsigned int index; \ |
| 594 | \ |
| 595 | index = SHOW_UBITS(name, gb, bits); \ |
| 596 | level = table[index].level; \ |
| 597 | n = table[index].len; \ |
| 598 | \ |
| 599 | if (max_depth > 1 && n < 0) { \ |
| 600 | SKIP_BITS(name, gb, bits); \ |
| 601 | if (need_update) { \ |
| 602 | UPDATE_CACHE(name, gb); \ |
| 603 | } \ |
| 604 | \ |
| 605 | nb_bits = -n; \ |
| 606 | \ |
| 607 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ |
| 608 | level = table[index].level; \ |
| 609 | n = table[index].len; \ |
| 610 | if (max_depth > 2 && n < 0) { \ |
| 611 | LAST_SKIP_BITS(name, gb, nb_bits); \ |
| 612 | if (need_update) { \ |
| 613 | UPDATE_CACHE(name, gb); \ |
| 614 | } \ |
| 615 | nb_bits = -n; \ |
| 616 | \ |
| 617 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ |
| 618 | level = table[index].level; \ |
| 619 | n = table[index].len; \ |
| 620 | } \ |
| 621 | } \ |
| 622 | run = table[index].run; \ |
| 623 | SKIP_BITS(name, gb, n); \ |
| 624 | } while (0) |
| 625 |
| 626 | static inline int decode012(GetBitContext *gb) |
| 627 | { |
| 628 | int n; |
| 629 | n = get_bits1(gb); |
| 630 | if (n == 0) |
| 631 | return 0; |
| 632 | else |
| 633 | return get_bits1(gb) + 1; |
| 634 | } |
| 635 |
| 636 | static inline int decode210(GetBitContext *gb) |
| 637 | { |
| 638 | if (get_bits1(gb)) |
| 639 | return 0; |
| 640 | else |
| 641 | return 2 - get_bits1(gb); |
| 642 | } |
| 643 |
| 644 | static inline int get_bits_left(GetBitContext *gb) |
| 645 | { |
| 646 | return gb->size_in_bits - get_bits_count(gb); |
| 647 | } |
| 648 |
| 649 | static inline int skip_1stop_8data_bits(GetBitContext *gb) |
| 650 | { |
| 651 | if (get_bits_left(gb) <= 0) |
| 652 | return AVERROR_INVALIDDATA; |
| 653 |
| 654 | while (get_bits1(gb)) { |
| 655 | skip_bits(gb, 8); |
| 656 | if (get_bits_left(gb) <= 0) |
| 657 | return AVERROR_INVALIDDATA; |
| 658 | } |
| 659 |
| 660 | return 0; |
| 661 | } |
| 662 |
| 663 | static inline unsigned int show_bits_long(GetBitContext *s, int n) |
| 664 | { |
| 665 | if (n <= MIN_CACHE_BITS) { |
| 666 | return show_bits(s, n); |
| 667 | } else { |
| 668 | GetBitContext gb = *s; |
| 669 | return get_bits_long(&gb, n); |
| 670 | } |
| 671 | } |
| 672 |
| 673 | #endif /* AVCODEC_GET_BITS_H */ |
| 674 |