]> git.sesse.net Git - ffmpeg/blob - libavcodec/get_bits.h
Merge commit '5846b496f0a1dd5be4ef714622940674305ec00f'
[ffmpeg] / libavcodec / get_bits.h
1 /*
2  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (c) 2016 Alexandra Hájková
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 /**
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 #include "libavutil/common.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/log.h"
35 #include "libavutil/avassert.h"
36 #include "avcodec.h"
37 #include "mathops.h"
38 #include "vlc.h"
39
40 /*
41  * Safe bitstream reading:
42  * optionally, the get_bits API can check to ensure that we
43  * don't read past input buffer boundaries. This is protected
44  * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
45  * then below that with UNCHECKED_BITSTREAM_READER at the per-
46  * decoder level. This means that decoders that check internally
47  * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
48  * overread checks.
49  * Boundary checking causes a minor performance penalty so for
50  * applications that won't want/need this, it can be disabled
51  * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
52  */
53 #ifndef UNCHECKED_BITSTREAM_READER
54 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
55 #endif
56
57 #ifndef CACHED_BITSTREAM_READER
58 #define CACHED_BITSTREAM_READER 0
59 #endif
60
61 typedef struct GetBitContext {
62     const uint8_t *buffer, *buffer_end;
63 #if CACHED_BITSTREAM_READER
64     uint64_t cache;
65     unsigned bits_left;
66 #endif
67     int index;
68     int size_in_bits;
69     int size_in_bits_plus8;
70 } GetBitContext;
71
72 static inline unsigned int get_bits(GetBitContext *s, int n);
73 static inline void skip_bits(GetBitContext *s, int n);
74 static inline unsigned int show_bits(GetBitContext *s, int n);
75
76 /* Bitstream reader API docs:
77  * name
78  *   arbitrary name which is used as prefix for the internal variables
79  *
80  * gb
81  *   getbitcontext
82  *
83  * OPEN_READER(name, gb)
84  *   load gb into local variables
85  *
86  * CLOSE_READER(name, gb)
87  *   store local vars in gb
88  *
89  * UPDATE_CACHE(name, gb)
90  *   Refill the internal cache from the bitstream.
91  *   After this call at least MIN_CACHE_BITS will be available.
92  *
93  * GET_CACHE(name, gb)
94  *   Will output the contents of the internal cache,
95  *   next bit is MSB of 32 or 64 bits (FIXME 64 bits).
96  *
97  * SHOW_UBITS(name, gb, num)
98  *   Will return the next num bits.
99  *
100  * SHOW_SBITS(name, gb, num)
101  *   Will return the next num bits and do sign extension.
102  *
103  * SKIP_BITS(name, gb, num)
104  *   Will skip over the next num bits.
105  *   Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
106  *
107  * SKIP_CACHE(name, gb, num)
108  *   Will remove the next num bits from the cache (note SKIP_COUNTER
109  *   MUST be called before UPDATE_CACHE / CLOSE_READER).
110  *
111  * SKIP_COUNTER(name, gb, num)
112  *   Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
113  *
114  * LAST_SKIP_BITS(name, gb, num)
115  *   Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
116  *
117  * BITS_LEFT(name, gb)
118  *   Return the number of bits left
119  *
120  * For examples see get_bits, show_bits, skip_bits, get_vlc.
121  */
122
123 #if CACHED_BITSTREAM_READER
124 #   define MIN_CACHE_BITS 64
125 #elif defined LONG_BITSTREAM_READER
126 #   define MIN_CACHE_BITS 32
127 #else
128 #   define MIN_CACHE_BITS 25
129 #endif
130
131 #if !CACHED_BITSTREAM_READER
132
133 #define OPEN_READER_NOSIZE(name, gb)            \
134     unsigned int name ## _index = (gb)->index;  \
135     unsigned int av_unused name ## _cache
136
137 #if UNCHECKED_BITSTREAM_READER
138 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
139
140 #define BITS_AVAILABLE(name, gb) 1
141 #else
142 #define OPEN_READER(name, gb)                   \
143     OPEN_READER_NOSIZE(name, gb);               \
144     unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
145
146 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
147 #endif
148
149 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
150
151 # ifdef LONG_BITSTREAM_READER
152
153 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
154       AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
155
156 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
157       AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
158
159 #else
160
161 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
162       AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
163
164 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
165       AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
166
167 #endif
168
169
170 #ifdef BITSTREAM_READER_LE
171
172 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
173
174 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
175
176 #else
177
178 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
179
180 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
181
182 #endif
183
184 #if UNCHECKED_BITSTREAM_READER
185 #   define SKIP_COUNTER(name, gb, num) name ## _index += (num)
186 #else
187 #   define SKIP_COUNTER(name, gb, num) \
188     name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
189 #endif
190
191 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
192
193 #define SKIP_BITS(name, gb, num)                \
194     do {                                        \
195         SKIP_CACHE(name, gb, num);              \
196         SKIP_COUNTER(name, gb, num);            \
197     } while (0)
198
199 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
200
201 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
202 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
203
204 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
205 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
206
207 #ifdef BITSTREAM_READER_LE
208 #   define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
209 #   define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
210 #else
211 #   define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
212 #   define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
213 #endif
214
215 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
216
217 #endif
218
219 static inline int get_bits_count(const GetBitContext *s)
220 {
221 #if CACHED_BITSTREAM_READER
222     return s->index - s->bits_left;
223 #else
224     return s->index;
225 #endif
226 }
227
228 #if CACHED_BITSTREAM_READER
229 static inline void refill_32(GetBitContext *s)
230 {
231 #if !UNCHECKED_BITSTREAM_READER
232     if (s->index >> 3 >= s->buffer_end - s->buffer)
233         return;
234 #endif
235
236 #ifdef BITSTREAM_READER_LE
237     s->cache       = (uint64_t)AV_RL32(s->buffer + (s->index >> 3)) << s->bits_left | s->cache;
238 #else
239     s->cache       = s->cache | (uint64_t)AV_RB32(s->buffer + (s->index >> 3)) << (32 - s->bits_left);
240 #endif
241     s->index     += 32;
242     s->bits_left += 32;
243 }
244
245 static inline void refill_64(GetBitContext *s)
246 {
247 #if !UNCHECKED_BITSTREAM_READER
248     if (s->index >> 3 >= s->buffer_end - s->buffer)
249         return;
250 #endif
251
252 #ifdef BITSTREAM_READER_LE
253     s->cache = AV_RL64(s->buffer + (s->index >> 3));
254 #else
255     s->cache = AV_RB64(s->buffer + (s->index >> 3));
256 #endif
257     s->index += 64;
258     s->bits_left = 64;
259 }
260
261 static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le)
262 {
263     uint64_t ret;
264     av_assert2(n>0 && n<=63);
265     if (is_le) {
266         ret = s->cache & ((UINT64_C(1) << n) - 1);
267         s->cache >>= n;
268     } else {
269         ret = s->cache >> (64 - n);
270         s->cache <<= n;
271     }
272     s->bits_left -= n;
273     return ret;
274 }
275
276 static inline unsigned show_val(const GetBitContext *s, unsigned n)
277 {
278 #ifdef BITSTREAM_READER_LE
279     return s->cache & ((UINT64_C(1) << n) - 1);
280 #else
281     return s->cache >> (64 - n);
282 #endif
283 }
284 #endif
285
286 /**
287  * Skips the specified number of bits.
288  * @param n the number of bits to skip,
289  *          For the UNCHECKED_BITSTREAM_READER this must not cause the distance
290  *          from the start to overflow int32_t. Staying within the bitstream + padding
291  *          is sufficient, too.
292  */
293 static inline void skip_bits_long(GetBitContext *s, int n)
294 {
295 #if CACHED_BITSTREAM_READER
296     skip_bits(s, n);
297 #else
298 #if UNCHECKED_BITSTREAM_READER
299     s->index += n;
300 #else
301     s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
302 #endif
303 #endif
304 }
305
306 #if CACHED_BITSTREAM_READER
307 static inline void skip_remaining(GetBitContext *s, unsigned n)
308 {
309 #ifdef BITSTREAM_READER_LE
310     s->cache >>= n;
311 #else
312     s->cache <<= n;
313 #endif
314     s->bits_left -= n;
315 }
316 #endif
317
318 /**
319  * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
320  * if MSB not set it is negative
321  * @param n length in bits
322  */
323 static inline int get_xbits(GetBitContext *s, int n)
324 {
325 #if CACHED_BITSTREAM_READER
326     int32_t cache = show_bits(s, 32);
327     int sign = ~cache >> 31;
328     skip_remaining(s, n);
329
330     return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
331 #else
332     register int sign;
333     register int32_t cache;
334     OPEN_READER(re, s);
335     av_assert2(n>0 && n<=25);
336     UPDATE_CACHE(re, s);
337     cache = GET_CACHE(re, s);
338     sign  = ~cache >> 31;
339     LAST_SKIP_BITS(re, s, n);
340     CLOSE_READER(re, s);
341     return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
342 #endif
343 }
344
345 #if !CACHED_BITSTREAM_READER
346 static inline int get_xbits_le(GetBitContext *s, int n)
347 {
348     register int sign;
349     register int32_t cache;
350     OPEN_READER(re, s);
351     av_assert2(n>0 && n<=25);
352     UPDATE_CACHE_LE(re, s);
353     cache = GET_CACHE(re, s);
354     sign  = sign_extend(~cache, n) >> 31;
355     LAST_SKIP_BITS(re, s, n);
356     CLOSE_READER(re, s);
357     return (zero_extend(sign ^ cache, n) ^ sign) - sign;
358 }
359 #endif
360
361 static inline int get_sbits(GetBitContext *s, int n)
362 {
363     register int tmp;
364 #if CACHED_BITSTREAM_READER
365     av_assert2(n>0 && n<=25);
366     tmp = sign_extend(get_bits(s, n), n);
367 #else
368     OPEN_READER(re, s);
369     av_assert2(n>0 && n<=25);
370     UPDATE_CACHE(re, s);
371     tmp = SHOW_SBITS(re, s, n);
372     LAST_SKIP_BITS(re, s, n);
373     CLOSE_READER(re, s);
374 #endif
375     return tmp;
376 }
377
378 /**
379  * Read 1-25 bits.
380  */
381 static inline unsigned int get_bits(GetBitContext *s, int n)
382 {
383     register unsigned int tmp;
384 #if CACHED_BITSTREAM_READER
385
386     av_assert2(n>0 && n<=32);
387     if (n > s->bits_left) {
388         refill_32(s);
389         if (s->bits_left < 32)
390             s->bits_left = n;
391     }
392
393 #ifdef BITSTREAM_READER_LE
394     tmp = get_val(s, n, 1);
395 #else
396     tmp = get_val(s, n, 0);
397 #endif
398 #else
399     OPEN_READER(re, s);
400     av_assert2(n>0 && n<=25);
401     UPDATE_CACHE(re, s);
402     tmp = SHOW_UBITS(re, s, n);
403     LAST_SKIP_BITS(re, s, n);
404     CLOSE_READER(re, s);
405 #endif
406     av_assert2(tmp < UINT64_C(1) << n);
407     return tmp;
408 }
409
410 /**
411  * Read 0-25 bits.
412  */
413 static av_always_inline int get_bitsz(GetBitContext *s, int n)
414 {
415     return n ? get_bits(s, n) : 0;
416 }
417
418 static inline unsigned int get_bits_le(GetBitContext *s, int n)
419 {
420 #if CACHED_BITSTREAM_READER
421     av_assert2(n>0 && n<=32);
422     if (n > s->bits_left) {
423         refill_32(s);
424         if (s->bits_left < 32)
425             s->bits_left = n;
426     }
427
428     return get_val(s, n, 1);
429 #else
430     register int tmp;
431     OPEN_READER(re, s);
432     av_assert2(n>0 && n<=25);
433     UPDATE_CACHE_LE(re, s);
434     tmp = SHOW_UBITS_LE(re, s, n);
435     LAST_SKIP_BITS(re, s, n);
436     CLOSE_READER(re, s);
437     return tmp;
438 #endif
439 }
440
441 /**
442  * Show 1-25 bits.
443  */
444 static inline unsigned int show_bits(GetBitContext *s, int n)
445 {
446     register unsigned int tmp;
447 #if CACHED_BITSTREAM_READER
448     if (n > s->bits_left)
449         refill_32(s);
450
451     tmp = show_val(s, n);
452 #else
453     OPEN_READER_NOSIZE(re, s);
454     av_assert2(n>0 && n<=25);
455     UPDATE_CACHE(re, s);
456     tmp = SHOW_UBITS(re, s, n);
457 #endif
458     return tmp;
459 }
460
461 static inline void skip_bits(GetBitContext *s, int n)
462 {
463 #if CACHED_BITSTREAM_READER
464     if (n < s->bits_left)
465         skip_remaining(s, n);
466     else {
467         n -= s->bits_left;
468         s->cache = 0;
469         s->bits_left = 0;
470
471         if (n >= 64) {
472             unsigned skip = (n / 8) * 8;
473
474             n -= skip;
475             s->index += skip;
476         }
477         refill_64(s);
478         if (n)
479             skip_remaining(s, n);
480     }
481 #else
482     OPEN_READER(re, s);
483     LAST_SKIP_BITS(re, s, n);
484     CLOSE_READER(re, s);
485 #endif
486 }
487
488 static inline unsigned int get_bits1(GetBitContext *s)
489 {
490 #if CACHED_BITSTREAM_READER
491     if (!s->bits_left)
492         refill_64(s);
493
494 #ifdef BITSTREAM_READER_LE
495     return get_val(s, 1, 1);
496 #else
497     return get_val(s, 1, 0);
498 #endif
499 #else
500     unsigned int index = s->index;
501     uint8_t result     = s->buffer[index >> 3];
502 #ifdef BITSTREAM_READER_LE
503     result >>= index & 7;
504     result  &= 1;
505 #else
506     result <<= index & 7;
507     result >>= 8 - 1;
508 #endif
509 #if !UNCHECKED_BITSTREAM_READER
510     if (s->index < s->size_in_bits_plus8)
511 #endif
512         index++;
513     s->index = index;
514
515     return result;
516 #endif
517 }
518
519 static inline unsigned int show_bits1(GetBitContext *s)
520 {
521     return show_bits(s, 1);
522 }
523
524 static inline void skip_bits1(GetBitContext *s)
525 {
526     skip_bits(s, 1);
527 }
528
529 /**
530  * Read 0-32 bits.
531  */
532 static inline unsigned int get_bits_long(GetBitContext *s, int n)
533 {
534     av_assert2(n>=0 && n<=32);
535     if (!n) {
536         return 0;
537 #if CACHED_BITSTREAM_READER
538     }
539     return get_bits(s, n);
540 #else
541     } else if (n <= MIN_CACHE_BITS) {
542         return get_bits(s, n);
543     } else {
544 #ifdef BITSTREAM_READER_LE
545         unsigned ret = get_bits(s, 16);
546         return ret | (get_bits(s, n - 16) << 16);
547 #else
548         unsigned ret = get_bits(s, 16) << (n - 16);
549         return ret | get_bits(s, n - 16);
550 #endif
551     }
552 #endif
553 }
554
555 /**
556  * Read 0-64 bits.
557  */
558 static inline uint64_t get_bits64(GetBitContext *s, int n)
559 {
560     if (n <= 32) {
561         return get_bits_long(s, n);
562     } else {
563 #ifdef BITSTREAM_READER_LE
564         uint64_t ret = get_bits_long(s, 32);
565         return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
566 #else
567         uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
568         return ret | get_bits_long(s, 32);
569 #endif
570     }
571 }
572
573 /**
574  * Read 0-32 bits as a signed integer.
575  */
576 static inline int get_sbits_long(GetBitContext *s, int n)
577 {
578     // sign_extend(x, 0) is undefined
579     if (!n)
580         return 0;
581
582     return sign_extend(get_bits_long(s, n), n);
583 }
584
585 /**
586  * Show 0-32 bits.
587  */
588 static inline unsigned int show_bits_long(GetBitContext *s, int n)
589 {
590     if (n <= MIN_CACHE_BITS) {
591         return show_bits(s, n);
592     } else {
593         GetBitContext gb = *s;
594         return get_bits_long(&gb, n);
595     }
596 }
597
598 static inline int check_marker(void *logctx, GetBitContext *s, const char *msg)
599 {
600     int bit = get_bits1(s);
601     if (!bit)
602         av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n",
603                get_bits_count(s) - 1, s->size_in_bits, msg);
604
605     return bit;
606 }
607
608 /**
609  * Initialize GetBitContext.
610  * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
611  *        larger than the actual read bits because some optimized bitstream
612  *        readers read 32 or 64 bit at once and could read over the end
613  * @param bit_size the size of the buffer in bits
614  * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
615  */
616 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
617                                 int bit_size)
618 {
619     int buffer_size;
620     int ret = 0;
621
622     if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
623         bit_size    = 0;
624         buffer      = NULL;
625         ret         = AVERROR_INVALIDDATA;
626     }
627
628     buffer_size = (bit_size + 7) >> 3;
629
630     s->buffer             = buffer;
631     s->size_in_bits       = bit_size;
632     s->size_in_bits_plus8 = bit_size + 8;
633     s->buffer_end         = buffer + buffer_size;
634     s->index              = 0;
635
636 #if CACHED_BITSTREAM_READER
637     refill_64(s);
638 #endif
639
640     return ret;
641 }
642
643 /**
644  * Initialize GetBitContext.
645  * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
646  *        larger than the actual read bits because some optimized bitstream
647  *        readers read 32 or 64 bit at once and could read over the end
648  * @param byte_size the size of the buffer in bytes
649  * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
650  */
651 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
652                                  int byte_size)
653 {
654     if (byte_size > INT_MAX / 8 || byte_size < 0)
655         byte_size = -1;
656     return init_get_bits(s, buffer, byte_size * 8);
657 }
658
659 static inline const uint8_t *align_get_bits(GetBitContext *s)
660 {
661     int n = -get_bits_count(s) & 7;
662     if (n)
663         skip_bits(s, n);
664     return s->buffer + (s->index >> 3);
665 }
666
667 /**
668  * If the vlc code is invalid and max_depth=1, then no bits will be removed.
669  * If the vlc code is invalid and max_depth>1, then the number of bits removed
670  * is undefined.
671  */
672 #define GET_VLC(code, name, gb, table, bits, max_depth)         \
673     do {                                                        \
674         int n, nb_bits;                                         \
675         unsigned int index;                                     \
676                                                                 \
677         index = SHOW_UBITS(name, gb, bits);                     \
678         code  = table[index][0];                                \
679         n     = table[index][1];                                \
680                                                                 \
681         if (max_depth > 1 && n < 0) {                           \
682             LAST_SKIP_BITS(name, gb, bits);                     \
683             UPDATE_CACHE(name, gb);                             \
684                                                                 \
685             nb_bits = -n;                                       \
686                                                                 \
687             index = SHOW_UBITS(name, gb, nb_bits) + code;       \
688             code  = table[index][0];                            \
689             n     = table[index][1];                            \
690             if (max_depth > 2 && n < 0) {                       \
691                 LAST_SKIP_BITS(name, gb, nb_bits);              \
692                 UPDATE_CACHE(name, gb);                         \
693                                                                 \
694                 nb_bits = -n;                                   \
695                                                                 \
696                 index = SHOW_UBITS(name, gb, nb_bits) + code;   \
697                 code  = table[index][0];                        \
698                 n     = table[index][1];                        \
699             }                                                   \
700         }                                                       \
701         SKIP_BITS(name, gb, n);                                 \
702     } while (0)
703
704 #define GET_RL_VLC(level, run, name, gb, table, bits,  \
705                    max_depth, need_update)                      \
706     do {                                                        \
707         int n, nb_bits;                                         \
708         unsigned int index;                                     \
709                                                                 \
710         index = SHOW_UBITS(name, gb, bits);                     \
711         level = table[index].level;                             \
712         n     = table[index].len;                               \
713                                                                 \
714         if (max_depth > 1 && n < 0) {                           \
715             SKIP_BITS(name, gb, bits);                          \
716             if (need_update) {                                  \
717                 UPDATE_CACHE(name, gb);                         \
718             }                                                   \
719                                                                 \
720             nb_bits = -n;                                       \
721                                                                 \
722             index = SHOW_UBITS(name, gb, nb_bits) + level;      \
723             level = table[index].level;                         \
724             n     = table[index].len;                           \
725             if (max_depth > 2 && n < 0) {                       \
726                 LAST_SKIP_BITS(name, gb, nb_bits);              \
727                 if (need_update) {                              \
728                     UPDATE_CACHE(name, gb);                     \
729                 }                                               \
730                 nb_bits = -n;                                   \
731                                                                 \
732                 index = SHOW_UBITS(name, gb, nb_bits) + level;  \
733                 level = table[index].level;                     \
734                 n     = table[index].len;                       \
735             }                                                   \
736         }                                                       \
737         run = table[index].run;                                 \
738         SKIP_BITS(name, gb, n);                                 \
739     } while (0)
740
741 /* Return the LUT element for the given bitstream configuration. */
742 static inline int set_idx(GetBitContext *s, int code, int *n, int *nb_bits,
743                           VLC_TYPE (*table)[2])
744 {
745     unsigned idx;
746
747     *nb_bits = -*n;
748     idx = show_bits(s, *nb_bits) + code;
749     *n = table[idx][1];
750
751     return table[idx][0];
752 }
753
754 /**
755  * Parse a vlc code.
756  * @param bits is the number of bits which will be read at once, must be
757  *             identical to nb_bits in init_vlc()
758  * @param max_depth is the number of times bits bits must be read to completely
759  *                  read the longest vlc code
760  *                  = (max_vlc_length + bits - 1) / bits
761  * @returns the code parsed or -1 if no vlc matches
762  */
763 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
764                                      int bits, int max_depth)
765 {
766 #if CACHED_BITSTREAM_READER
767     int nb_bits;
768     unsigned idx = show_bits(s, bits);
769     int code = table[idx][0];
770     int n    = table[idx][1];
771
772     if (max_depth > 1 && n < 0) {
773         skip_remaining(s, bits);
774         code = set_idx(s, code, &n, &nb_bits, table);
775         if (max_depth > 2 && n < 0) {
776             skip_remaining(s, nb_bits);
777             code = set_idx(s, code, &n, &nb_bits, table);
778         }
779     }
780     skip_remaining(s, n);
781
782     return code;
783 #else
784     int code;
785
786     OPEN_READER(re, s);
787     UPDATE_CACHE(re, s);
788
789     GET_VLC(code, re, s, table, bits, max_depth);
790
791     CLOSE_READER(re, s);
792
793     return code;
794 #endif
795 }
796
797 static inline int decode012(GetBitContext *gb)
798 {
799     int n;
800     n = get_bits1(gb);
801     if (n == 0)
802         return 0;
803     else
804         return get_bits1(gb) + 1;
805 }
806
807 static inline int decode210(GetBitContext *gb)
808 {
809     if (get_bits1(gb))
810         return 0;
811     else
812         return 2 - get_bits1(gb);
813 }
814
815 static inline int get_bits_left(GetBitContext *gb)
816 {
817     return gb->size_in_bits - get_bits_count(gb);
818 }
819
820 static inline int skip_1stop_8data_bits(GetBitContext *gb)
821 {
822     if (get_bits_left(gb) <= 0)
823         return AVERROR_INVALIDDATA;
824
825     while (get_bits1(gb)) {
826         skip_bits(gb, 8);
827         if (get_bits_left(gb) <= 0)
828             return AVERROR_INVALIDDATA;
829     }
830
831     return 0;
832 }
833
834 #endif /* AVCODEC_GET_BITS_H */