FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/get_bits.h
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 145 151 96.0%
Functions: 27 27 100.0%
Branches: 40 50 80.0%

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_left bits_left
80 #define skip_bits_long bits_skip
81 #define skip_bits bits_skip
82 #define get_bits bits_read_nz
83 #define get_bitsz bits_read
84 #define get_bits_long bits_read
85 #define get_bits1 bits_read_bit
86 #define get_bits64 bits_read_64
87 #define get_xbits bits_read_xbits
88 #define get_sbits bits_read_signed_nz
89 #define get_sbits_long bits_read_signed
90 #define show_bits bits_peek
91 #define show_bits_long bits_peek
92 #define init_get_bits bits_init
93 #define init_get_bits8 bits_init8
94 #define align_get_bits bits_align
95 #define get_vlc2 bits_read_vlc
96 #define get_vlc_multi bits_read_vlc_multi
97
98 #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size)
99 #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n)
100
101 #define show_bits1(s) bits_peek(s, 1)
102 #define skip_bits1(s) bits_skip(s, 1)
103
104 #define skip_1stop_8data_bits bits_skip_1stop_8data
105
106 #else // CACHED_BITSTREAM_READER
107
108 typedef struct GetBitContext {
109 const uint8_t *buffer, *buffer_end;
110 int index;
111 int size_in_bits;
112 int size_in_bits_plus8;
113 } GetBitContext;
114
115 static inline unsigned int get_bits(GetBitContext *s, int n);
116 static inline void skip_bits(GetBitContext *s, int n);
117 static inline unsigned int show_bits(GetBitContext *s, int n);
118
119 /* Bitstream reader API docs:
120 * name
121 * arbitrary name which is used as prefix for the internal variables
122 *
123 * gb
124 * getbitcontext
125 *
126 * OPEN_READER(name, gb)
127 * load gb into local variables
128 *
129 * CLOSE_READER(name, gb)
130 * store local vars in gb
131 *
132 * UPDATE_CACHE(name, gb)
133 * Refill the internal cache from the bitstream.
134 * After this call at least MIN_CACHE_BITS will be available.
135 *
136 * GET_CACHE(name, gb)
137 * Will output the contents of the internal cache,
138 * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
139 *
140 * SHOW_UBITS(name, gb, num)
141 * Will return the next num bits.
142 *
143 * SHOW_SBITS(name, gb, num)
144 * Will return the next num bits and do sign extension.
145 *
146 * SKIP_BITS(name, gb, num)
147 * Will skip over the next num bits.
148 * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
149 *
150 * SKIP_CACHE(name, gb, num)
151 * Will remove the next num bits from the cache (note SKIP_COUNTER
152 * MUST be called before UPDATE_CACHE / CLOSE_READER).
153 *
154 * SKIP_COUNTER(name, gb, num)
155 * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
156 *
157 * LAST_SKIP_BITS(name, gb, num)
158 * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
159 *
160 * BITS_LEFT(name, gb)
161 * Return the number of bits left
162 *
163 * For examples see get_bits, show_bits, skip_bits, get_vlc.
164 */
165
166 #if defined LONG_BITSTREAM_READER
167 # define MIN_CACHE_BITS 32
168 #else
169 # define MIN_CACHE_BITS 25
170 #endif
171
172 #define OPEN_READER_NOSIZE(name, gb) \
173 unsigned int name ## _index = (gb)->index; \
174 unsigned int av_unused name ## _cache
175
176 #if UNCHECKED_BITSTREAM_READER
177 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
178
179 #define BITS_AVAILABLE(name, gb) 1
180 #else
181 #define OPEN_READER(name, gb) \
182 OPEN_READER_NOSIZE(name, gb); \
183 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
184
185 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
186 #endif
187
188 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
189
190 #define UPDATE_CACHE_BE_EXT(name, gb, bits, dst_bits) name ## _cache = \
191 AV_RB ## bits((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) >> (bits - dst_bits)
192
193 #define UPDATE_CACHE_LE_EXT(name, gb, bits, dst_bits) name ## _cache = \
194 (uint ## dst_bits ## _t)(AV_RL ## bits((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7))
195
196 /* Using these two macros ensures that 32 bits are available. */
197 # define UPDATE_CACHE_LE_32(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 64, 32)
198
199 # define UPDATE_CACHE_BE_32(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 64, 32)
200
201 # ifdef LONG_BITSTREAM_READER
202
203 # define UPDATE_CACHE_LE(name, gb) UPDATE_CACHE_LE_32(name, (gb))
204
205 # define UPDATE_CACHE_BE(name, gb) UPDATE_CACHE_BE_32(name, (gb))
206
207 #else
208
209 # define UPDATE_CACHE_LE(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 32, 32)
210
211 # define UPDATE_CACHE_BE(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 32, 32)
212
213 #endif
214
215
216 #ifdef BITSTREAM_READER_LE
217
218 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
219 # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_LE_32(name, (gb))
220
221 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
222
223 #else
224
225 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
226 # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_BE_32(name, (gb))
227
228 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
229
230 #endif
231
232 #if UNCHECKED_BITSTREAM_READER
233 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
234 #else
235 # define SKIP_COUNTER(name, gb, num) \
236 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
237 #endif
238
239 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
240
241 #define SKIP_BITS(name, gb, num) \
242 do { \
243 SKIP_CACHE(name, gb, num); \
244 SKIP_COUNTER(name, gb, num); \
245 } while (0)
246
247 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
248
249 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
250 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
251
252 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
253 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
254
255 #ifdef BITSTREAM_READER_LE
256 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
257 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
258 #else
259 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
260 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
261 #endif
262
263 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
264
265
266 306560567 static inline int get_bits_count(const GetBitContext *s)
267 {
268 306560567 return s->index;
269 }
270
271 /**
272 * Skips the specified number of bits.
273 * @param n the number of bits to skip,
274 * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
275 * from the start to overflow int32_t. Staying within the bitstream + padding
276 * is sufficient, too.
277 */
278 4763771 static inline void skip_bits_long(GetBitContext *s, int n)
279 {
280 #if UNCHECKED_BITSTREAM_READER
281 2783178 s->index += n;
282 #else
283 1980593 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
284 #endif
285 4763771 }
286
287 /**
288 * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
289 * if MSB not set it is negative
290 * @param n length in bits
291 */
292 26889200 static inline int get_xbits(GetBitContext *s, int n)
293 {
294 register int sign;
295 register int32_t cache;
296 26889200 OPEN_READER(re, s);
297 av_assert2(n>0 && n<=25);
298 26889200 UPDATE_CACHE(re, s);
299 26889200 cache = GET_CACHE(re, s);
300 26889200 sign = ~cache >> 31;
301 26889200 LAST_SKIP_BITS(re, s, n);
302 26889200 CLOSE_READER(re, s);
303 26889200 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
304 }
305
306 1534760 static inline int get_xbits_le(GetBitContext *s, int n)
307 {
308 register int sign;
309 register int32_t cache;
310 1534760 OPEN_READER(re, s);
311 av_assert2(n>0 && n<=25);
312 1534760 UPDATE_CACHE_LE(re, s);
313 1534760 cache = GET_CACHE(re, s);
314 1534760 sign = sign_extend(~cache, n) >> 31;
315 1534760 LAST_SKIP_BITS(re, s, n);
316 1534760 CLOSE_READER(re, s);
317 1534760 return (zero_extend(sign ^ cache, n) ^ sign) - sign;
318 }
319
320 33300890 static inline int get_sbits(GetBitContext *s, int n)
321 {
322 register int tmp;
323 33300890 OPEN_READER(re, s);
324 av_assert2(n>0 && n<=25);
325 33300890 UPDATE_CACHE(re, s);
326 33300890 tmp = SHOW_SBITS(re, s, n);
327 33300890 LAST_SKIP_BITS(re, s, n);
328 33300890 CLOSE_READER(re, s);
329 33300890 return tmp;
330 }
331
332 /**
333 * Read 1-25 bits.
334 */
335 269786536 static inline unsigned int get_bits(GetBitContext *s, int n)
336 {
337 register unsigned int tmp;
338 269786536 OPEN_READER(re, s);
339 av_assert2(n>0 && n<=25);
340 269786536 UPDATE_CACHE(re, s);
341 269786536 tmp = SHOW_UBITS(re, s, n);
342 269786536 LAST_SKIP_BITS(re, s, n);
343 269786536 CLOSE_READER(re, s);
344 av_assert2(tmp < UINT64_C(1) << n);
345 269786536 return tmp;
346 }
347
348 /**
349 * Read 0-25 bits.
350 */
351 764563 static av_always_inline int get_bitsz(GetBitContext *s, int n)
352 {
353
2/2
✓ Branch 0 taken 686658 times.
✓ Branch 1 taken 77905 times.
764563 return n ? get_bits(s, n) : 0;
354 }
355
356 718813 static inline unsigned int get_bits_le(GetBitContext *s, int n)
357 {
358 register int tmp;
359 718813 OPEN_READER(re, s);
360 av_assert2(n>0 && n<=25);
361 718813 UPDATE_CACHE_LE(re, s);
362 718813 tmp = SHOW_UBITS_LE(re, s, n);
363 718813 LAST_SKIP_BITS(re, s, n);
364 718813 CLOSE_READER(re, s);
365 718813 return tmp;
366 }
367
368 /**
369 * Show 1-25 bits.
370 */
371 113611574 static inline unsigned int show_bits(GetBitContext *s, int n)
372 {
373 register unsigned int tmp;
374 113611574 OPEN_READER_NOSIZE(re, s);
375 av_assert2(n>0 && n<=25);
376 113611574 UPDATE_CACHE(re, s);
377 113611574 tmp = SHOW_UBITS(re, s, n);
378 113611574 return tmp;
379 }
380
381 110799175 static inline void skip_bits(GetBitContext *s, int n)
382 {
383 110799175 OPEN_READER(re, s);
384 110799175 LAST_SKIP_BITS(re, s, n);
385 110799175 CLOSE_READER(re, s);
386 110799175 }
387
388 454630873 static inline unsigned int get_bits1(GetBitContext *s)
389 {
390 454630873 unsigned int index = s->index;
391 454630873 uint8_t result = s->buffer[index >> 3];
392 #ifdef BITSTREAM_READER_LE
393 265269752 result >>= index & 7;
394 265269752 result &= 1;
395 #else
396 189361121 result <<= index & 7;
397 189361121 result >>= 8 - 1;
398 #endif
399 #if !UNCHECKED_BITSTREAM_READER
400
2/2
✓ Branch 0 taken 411016592 times.
✓ Branch 1 taken 15330 times.
411031922 if (s->index < s->size_in_bits_plus8)
401 #endif
402 454615543 index++;
403 454630873 s->index = index;
404
405 454630873 return result;
406 }
407
408 43276 static inline unsigned int show_bits1(GetBitContext *s)
409 {
410 43276 return show_bits(s, 1);
411 }
412
413 303794 static inline void skip_bits1(GetBitContext *s)
414 {
415 303794 skip_bits(s, 1);
416 303794 }
417
418 /**
419 * Read 0-32 bits.
420 */
421 57301353 static inline unsigned int get_bits_long(GetBitContext *s, int n)
422 {
423 av_assert2(n>=0 && n<=32);
424
2/2
✓ Branch 0 taken 3404960 times.
✓ Branch 1 taken 53896393 times.
57301353 if (!n) {
425 3404960 return 0;
426 } else if ((!HAVE_FAST_64BIT || av_builtin_constant_p(n <= MIN_CACHE_BITS))
427 && n <= MIN_CACHE_BITS) {
428 return get_bits(s, n);
429 } else {
430 #if HAVE_FAST_64BIT
431 unsigned tmp;
432 53896393 OPEN_READER(re, s);
433 53896393 UPDATE_CACHE_32(re, s);
434 53896393 tmp = SHOW_UBITS(re, s, n);
435 53896393 LAST_SKIP_BITS(re, s, n);
436 53896393 CLOSE_READER(re, s);
437 53896393 return tmp;
438 #else
439 #ifdef BITSTREAM_READER_LE
440 unsigned ret = get_bits(s, 16);
441 return ret | (get_bits(s, n - 16) << 16);
442 #else
443 unsigned ret = get_bits(s, 16) << (n - 16);
444 return ret | get_bits(s, n - 16);
445 #endif
446 #endif
447 }
448 }
449
450 /**
451 * Read 0-64 bits.
452 */
453 11318 static inline uint64_t get_bits64(GetBitContext *s, int n)
454 {
455
2/2
✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 4943 times.
11318 if (n <= 32) {
456 6375 return get_bits_long(s, n);
457 } else {
458 #ifdef BITSTREAM_READER_LE
459 1 uint64_t ret = get_bits_long(s, 32);
460 1 return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
461 #else
462 4942 uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
463 4942 return ret | get_bits_long(s, 32);
464 #endif
465 }
466 }
467
468 /**
469 * Read 0-32 bits as a signed integer.
470 */
471 4766618 static inline int get_sbits_long(GetBitContext *s, int n)
472 {
473 // sign_extend(x, 0) is undefined
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4766618 times.
4766618 if (!n)
475 return 0;
476
477 4766618 return sign_extend(get_bits_long(s, n), n);
478 }
479
480 /**
481 * Read 0-64 bits as a signed integer.
482 */
483 4624 static inline int64_t get_sbits64(GetBitContext *s, int n)
484 {
485 // sign_extend(x, 0) is undefined
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4624 times.
4624 if (!n)
487 return 0;
488
489 4624 return sign_extend64(get_bits64(s, n), n);
490 }
491
492 /**
493 * Show 0-32 bits.
494 */
495 3566768 static inline unsigned int show_bits_long(GetBitContext *s, int n)
496 {
497
2/2
✓ Branch 0 taken 17475 times.
✓ Branch 1 taken 3549293 times.
3566768 if (n <= MIN_CACHE_BITS) {
498 17475 return show_bits(s, n);
499 } else {
500 3549293 GetBitContext gb = *s;
501 3549293 return get_bits_long(&gb, n);
502 }
503 }
504
505
506 /**
507 * Initialize GetBitContext.
508 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
509 * larger than the actual read bits because some optimized bitstream
510 * readers read 32 or 64 bit at once and could read over the end
511 * @param bit_size the size of the buffer in bits
512 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
513 */
514 29838858 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
515 int bit_size)
516 {
517 int buffer_size;
518 29838858 int ret = 0;
519
520
4/6
✓ Branch 0 taken 29838858 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29838858 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 29838854 times.
29838858 if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
521 4 bit_size = 0;
522 4 buffer = NULL;
523 4 ret = AVERROR_INVALIDDATA;
524 }
525
526 29838858 buffer_size = (bit_size + 7) >> 3;
527
528 29838858 s->buffer = buffer;
529 29838858 s->size_in_bits = bit_size;
530 29838858 s->size_in_bits_plus8 = bit_size + 8;
531 29838858 s->buffer_end = buffer + buffer_size;
532 29838858 s->index = 0;
533
534 29838858 return ret;
535 }
536
537 /**
538 * Initialize GetBitContext.
539 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
540 * larger than the actual read bits because some optimized bitstream
541 * readers read 32 or 64 bit at once and could read over the end
542 * @param byte_size the size of the buffer in bytes
543 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
544 */
545 645163 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
546 int byte_size)
547 {
548
2/4
✓ Branch 0 taken 645163 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 645163 times.
645163 if (byte_size > INT_MAX / 8 || byte_size < 0)
549 byte_size = -1;
550 645163 return init_get_bits(s, buffer, byte_size * 8);
551 }
552
553 200 static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer,
554 int byte_size)
555 {
556
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)
557 byte_size = -1;
558 200 return init_get_bits(s, buffer, byte_size * 8);
559 }
560
561 281215 static inline const uint8_t *align_get_bits(GetBitContext *s)
562 {
563 281215 int n = -get_bits_count(s) & 7;
564
2/2
✓ Branch 0 taken 95413 times.
✓ Branch 1 taken 185802 times.
281215 if (n)
565 95413 skip_bits(s, n);
566 281215 return s->buffer + (s->index >> 3);
567 }
568
569 /**
570 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
571 * If the vlc code is invalid and max_depth>1, then the number of bits removed
572 * is undefined.
573 */
574 #define GET_VLC(code, name, gb, table, bits, max_depth) \
575 do { \
576 int n, nb_bits; \
577 unsigned int index; \
578 \
579 index = SHOW_UBITS(name, gb, bits); \
580 code = table[index].sym; \
581 n = table[index].len; \
582 \
583 if (max_depth > 1 && n < 0) { \
584 LAST_SKIP_BITS(name, gb, bits); \
585 UPDATE_CACHE(name, gb); \
586 \
587 nb_bits = -n; \
588 \
589 index = SHOW_UBITS(name, gb, nb_bits) + code; \
590 code = table[index].sym; \
591 n = table[index].len; \
592 if (max_depth > 2 && n < 0) { \
593 LAST_SKIP_BITS(name, gb, nb_bits); \
594 UPDATE_CACHE(name, gb); \
595 \
596 nb_bits = -n; \
597 \
598 index = SHOW_UBITS(name, gb, nb_bits) + code; \
599 code = table[index].sym; \
600 n = table[index].len; \
601 } \
602 } \
603 SKIP_BITS(name, gb, n); \
604 } while (0)
605
606 #define GET_RL_VLC(level, run, name, gb, table, bits, \
607 max_depth, need_update) \
608 do { \
609 int n, nb_bits; \
610 unsigned int index; \
611 \
612 index = SHOW_UBITS(name, gb, bits); \
613 level = table[index].level; \
614 n = table[index].len; \
615 \
616 if (max_depth > 1 && n < 0) { \
617 SKIP_BITS(name, gb, bits); \
618 if (need_update) { \
619 UPDATE_CACHE(name, gb); \
620 } \
621 \
622 nb_bits = -n; \
623 \
624 index = SHOW_UBITS(name, gb, nb_bits) + level; \
625 level = table[index].level; \
626 n = table[index].len; \
627 if (max_depth > 2 && n < 0) { \
628 LAST_SKIP_BITS(name, gb, nb_bits); \
629 if (need_update) { \
630 UPDATE_CACHE(name, gb); \
631 } \
632 nb_bits = -n; \
633 \
634 index = SHOW_UBITS(name, gb, nb_bits) + level; \
635 level = table[index].level; \
636 n = table[index].len; \
637 } \
638 } \
639 run = table[index].run; \
640 SKIP_BITS(name, gb, n); \
641 } while (0)
642
643 /**
644 * Parse a vlc code.
645 * @param bits is the number of bits which will be read at once, must be
646 * identical to nb_bits in vlc_init()
647 * @param max_depth is the number of times bits bits must be read to completely
648 * read the longest vlc code
649 * = (max_vlc_length + bits - 1) / bits
650 * @returns the code parsed or -1 if no vlc matches
651 */
652 291305025 static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table,
653 int bits, int max_depth)
654 {
655 int code;
656
657 291305025 OPEN_READER(re, s);
658 291305025 UPDATE_CACHE(re, s);
659
660
10/10
✓ Branch 0 taken 7684008 times.
✓ Branch 1 taken 180851032 times.
✓ Branch 2 taken 103124076 times.
✓ Branch 3 taken 32555603 times.
✓ Branch 4 taken 146482399 times.
✓ Branch 5 taken 103091 times.
✓ Branch 6 taken 22889243 times.
✓ Branch 7 taken 2587443 times.
✓ Branch 8 taken 630122 times.
✓ Branch 9 taken 22258551 times.
291305025 GET_VLC(code, re, s, table, bits, max_depth);
661
662 291305025 CLOSE_READER(re, s);
663
664 291305025 return code;
665 }
666
667 static inline int get_vlc_multi(GetBitContext *s, uint8_t *dst,
668 const VLC_MULTI_ELEM *const Jtable,
669 const VLCElem *const table,
670 const int bits, const int max_depth,
671 const int symbols_size)
672 {
673 dst[0] = get_vlc2(s, table, bits, max_depth);
674 return 1;
675 }
676
677 378878 static inline int decode012(GetBitContext *gb)
678 {
679 int n;
680 378878 n = get_bits1(gb);
681
2/2
✓ Branch 0 taken 263931 times.
✓ Branch 1 taken 114947 times.
378878 if (n == 0)
682 263931 return 0;
683 else
684 114947 return get_bits1(gb) + 1;
685 }
686
687 191971 static inline int decode210(GetBitContext *gb)
688 {
689
2/2
✓ Branch 1 taken 135836 times.
✓ Branch 2 taken 56135 times.
191971 if (get_bits1(gb))
690 135836 return 0;
691 else
692 56135 return 2 - get_bits1(gb);
693 }
694
695 270825714 static inline int get_bits_left(GetBitContext *gb)
696 {
697 270825714 return gb->size_in_bits - get_bits_count(gb);
698 }
699
700 134608 static inline int skip_1stop_8data_bits(GetBitContext *gb)
701 {
702
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 134608 times.
134608 if (get_bits_left(gb) <= 0)
703 return AVERROR_INVALIDDATA;
704
705
2/2
✓ Branch 1 taken 3114 times.
✓ Branch 2 taken 134608 times.
137722 while (get_bits1(gb)) {
706 3114 skip_bits(gb, 8);
707
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3114 times.
3114 if (get_bits_left(gb) <= 0)
708 return AVERROR_INVALIDDATA;
709 }
710
711 134608 return 0;
712 }
713
714 #endif // CACHED_BITSTREAM_READER
715
716 #endif /* AVCODEC_GET_BITS_H */
717