FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/get_bits.h
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 145 151 96.0%
Functions: 28 28 100.0%
Branches: 42 52 80.8%

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(name, gb) \
170 unsigned int name ## _index = (gb)->index; \
171 unsigned int av_unused name ## _cache
172
173 #if UNCHECKED_BITSTREAM_READER
174 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
175
176 #define BITS_AVAILABLE(name, gb) 1
177 #else
178 #define OPEN_READER(name, gb) \
179 OPEN_READER_NOSIZE(name, gb); \
180 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
181
182 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
183 #endif
184
185 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
186
187 #define UPDATE_CACHE_BE_EXT(name, gb, bits, dst_bits) name ## _cache = \
188 AV_RB ## bits((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) >> (bits - dst_bits)
189
190 #define UPDATE_CACHE_LE_EXT(name, gb, bits, dst_bits) name ## _cache = \
191 (uint ## dst_bits ## _t)(AV_RL ## bits((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7))
192
193 /* Using these two macros ensures that 32 bits are available. */
194 # define UPDATE_CACHE_LE_32(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 64, 32)
195 # define UPDATE_CACHE_BE_32(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 64, 32)
196
197 # define UPDATE_CACHE_LE(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 32, 32)
198 # define UPDATE_CACHE_BE(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 32, 32)
199
200 #ifdef BITSTREAM_READER_LE
201
202 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
203 # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_LE_32(name, (gb))
204
205 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
206
207 #else
208
209 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
210 # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_BE_32(name, (gb))
211
212 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
213
214 #endif
215
216 #if UNCHECKED_BITSTREAM_READER
217 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
218 #else
219 # define SKIP_COUNTER(name, gb, num) \
220 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
221 #endif
222
223 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
224
225 #define SKIP_BITS(name, gb, num) \
226 do { \
227 SKIP_CACHE(name, gb, num); \
228 SKIP_COUNTER(name, gb, num); \
229 } while (0)
230
231 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
232
233 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
234 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
235
236 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
237 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
238
239 #ifdef BITSTREAM_READER_LE
240 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
241 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
242 #else
243 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
244 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
245 #endif
246
247 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
248
249
250 320532409 static inline int get_bits_count(const GetBitContext *s)
251 {
252 320532409 return s->index;
253 }
254
255 /**
256 * Get the size of the GetBitContext's buffer in bytes.
257 *
258 * @param s the GetBitContext
259 * @param round_up If set, the number of bits will be rounded up to full bytes;
260 * this does not matter if the number of bits is known to be
261 * a multiple of eight, e.g. if the GetBitContext has been
262 * initialized with init_get_bits8.
263 */
264 24432 static inline int get_bits_bytesize(const GetBitContext *s, int round_up)
265 {
266
2/2
✓ Branch 0 taken 17754 times.
✓ Branch 1 taken 6678 times.
24432 return (s->size_in_bits + (round_up ? 7 : 0)) >> 3;
267 }
268
269 /**
270 * Skips the specified number of bits.
271 * @param n the number of bits to skip,
272 * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
273 * from the start to overflow int32_t. Staying within the bitstream + padding
274 * is sufficient, too.
275 */
276 4664827 static inline void skip_bits_long(GetBitContext *s, int n)
277 {
278 #if UNCHECKED_BITSTREAM_READER
279 2784663 s->index += n;
280 #else
281 1880164 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
282 #endif
283 4664827 }
284
285 /**
286 * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
287 * if MSB not set it is negative
288 * @param n length in bits
289 */
290 26952221 static inline int get_xbits(GetBitContext *s, int n)
291 {
292 register int sign;
293 register int32_t cache;
294 26952221 OPEN_READER(re, s);
295 av_assert2(n>0 && n<=25);
296 26952221 UPDATE_CACHE(re, s);
297 26952221 cache = GET_CACHE(re, s);
298 26952221 sign = ~cache >> 31;
299 26952221 LAST_SKIP_BITS(re, s, n);
300 26952221 CLOSE_READER(re, s);
301 26952221 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
302 }
303
304 1534760 static inline int get_xbits_le(GetBitContext *s, int n)
305 {
306 register int sign;
307 register int32_t cache;
308 1534760 OPEN_READER(re, s);
309 av_assert2(n>0 && n<=25);
310 1534760 UPDATE_CACHE_LE(re, s);
311 1534760 cache = GET_CACHE(re, s);
312 1534760 sign = sign_extend(~cache, n) >> 31;
313 1534760 LAST_SKIP_BITS(re, s, n);
314 1534760 CLOSE_READER(re, s);
315 1534760 return (zero_extend(sign ^ cache, n) ^ sign) - sign;
316 }
317
318 33305050 static inline int get_sbits(GetBitContext *s, int n)
319 {
320 register int tmp;
321 33305050 OPEN_READER(re, s);
322 av_assert2(n>0 && n<=25);
323 33305050 UPDATE_CACHE(re, s);
324 33305050 tmp = SHOW_SBITS(re, s, n);
325 33305050 LAST_SKIP_BITS(re, s, n);
326 33305050 CLOSE_READER(re, s);
327 33305050 return tmp;
328 }
329
330 /**
331 * Read 1-25 bits.
332 */
333 276589654 static inline unsigned int get_bits(GetBitContext *s, int n)
334 {
335 register unsigned int tmp;
336 276589654 OPEN_READER(re, s);
337 av_assert2(n>0 && n<=25);
338 276589654 UPDATE_CACHE(re, s);
339 276589654 tmp = SHOW_UBITS(re, s, n);
340 276589654 LAST_SKIP_BITS(re, s, n);
341 276589654 CLOSE_READER(re, s);
342 av_assert2(tmp < UINT64_C(1) << n);
343 276589654 return tmp;
344 }
345
346 /**
347 * Read 0-25 bits.
348 */
349 746532 static av_always_inline int get_bitsz(GetBitContext *s, int n)
350 {
351
2/2
✓ Branch 0 taken 668627 times.
✓ Branch 1 taken 77905 times.
746532 return n ? get_bits(s, n) : 0;
352 }
353
354 718813 static inline unsigned int get_bits_le(GetBitContext *s, int n)
355 {
356 register int tmp;
357 718813 OPEN_READER(re, s);
358 av_assert2(n>0 && n<=25);
359 718813 UPDATE_CACHE_LE(re, s);
360 718813 tmp = SHOW_UBITS_LE(re, s, n);
361 718813 LAST_SKIP_BITS(re, s, n);
362 718813 CLOSE_READER(re, s);
363 718813 return tmp;
364 }
365
366 /**
367 * Show 1-25 bits.
368 */
369 118265395 static inline unsigned int show_bits(GetBitContext *s, int n)
370 {
371 register unsigned int tmp;
372 118265395 OPEN_READER_NOSIZE(re, s);
373 av_assert2(n>0 && n<=25);
374 118265395 UPDATE_CACHE(re, s);
375 118265395 tmp = SHOW_UBITS(re, s, n);
376 118265395 return tmp;
377 }
378
379 115800244 static inline void skip_bits(GetBitContext *s, int n)
380 {
381 115800244 OPEN_READER(re, s);
382 115800244 LAST_SKIP_BITS(re, s, n);
383 115800244 CLOSE_READER(re, s);
384 115800244 }
385
386 473759956 static inline unsigned int get_bits1(GetBitContext *s)
387 {
388 473759956 unsigned int index = s->index;
389 473759956 uint8_t result = s->buffer[index >> 3];
390 #ifdef BITSTREAM_READER_LE
391 265293723 result >>= index & 7;
392 265293723 result &= 1;
393 #else
394 208466233 result <<= index & 7;
395 208466233 result >>= 8 - 1;
396 #endif
397 #if !UNCHECKED_BITSTREAM_READER
398
2/2
✓ Branch 0 taken 430004896 times.
✓ Branch 1 taken 32069 times.
430036965 if (s->index < s->size_in_bits_plus8)
399 #endif
400 473727887 index++;
401 473759956 s->index = index;
402
403 473759956 return result;
404 }
405
406 43299 static inline unsigned int show_bits1(GetBitContext *s)
407 {
408 43299 return show_bits(s, 1);
409 }
410
411 391516 static inline void skip_bits1(GetBitContext *s)
412 {
413 391516 skip_bits(s, 1);
414 391516 }
415
416 /**
417 * Read 0-32 bits.
418 */
419 57213187 static inline unsigned int get_bits_long(GetBitContext *s, int n)
420 {
421 av_assert2(n>=0 && n<=32);
422
2/2
✓ Branch 0 taken 3404960 times.
✓ Branch 1 taken 53808227 times.
57213187 if (!n) {
423 3404960 return 0;
424 } else if ((!HAVE_FAST_64BIT || av_builtin_constant_p(n <= MIN_CACHE_BITS))
425 && n <= MIN_CACHE_BITS) {
426 return get_bits(s, n);
427 } else {
428 #if HAVE_FAST_64BIT
429 unsigned tmp;
430 53808227 OPEN_READER(re, s);
431 53808227 UPDATE_CACHE_32(re, s);
432 53808227 tmp = SHOW_UBITS(re, s, n);
433 53808227 LAST_SKIP_BITS(re, s, n);
434 53808227 CLOSE_READER(re, s);
435 53808227 return tmp;
436 #else
437 #ifdef BITSTREAM_READER_LE
438 unsigned ret = get_bits(s, 16);
439 return ret | (get_bits(s, n - 16) << 16);
440 #else
441 unsigned ret = get_bits(s, 16) << (n - 16);
442 return ret | get_bits(s, n - 16);
443 #endif
444 #endif
445 }
446 }
447
448 /**
449 * Read 0-64 bits.
450 */
451 11443 static inline uint64_t get_bits64(GetBitContext *s, int n)
452 {
453
2/2
✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 5068 times.
11443 if (n <= 32) {
454 6375 return get_bits_long(s, n);
455 } else {
456 #ifdef BITSTREAM_READER_LE
457 1 uint64_t ret = get_bits_long(s, 32);
458 1 return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
459 #else
460 5067 uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
461 5067 return ret | get_bits_long(s, 32);
462 #endif
463 }
464 }
465
466 /**
467 * Read 0-32 bits as a signed integer.
468 */
469 4766805 static inline int get_sbits_long(GetBitContext *s, int n)
470 {
471 // sign_extend(x, 0) is undefined
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4766805 times.
4766805 if (!n)
473 return 0;
474
475 4766805 return sign_extend(get_bits_long(s, n), n);
476 }
477
478 /**
479 * Read 0-64 bits as a signed integer.
480 */
481 4624 static inline int64_t get_sbits64(GetBitContext *s, int n)
482 {
483 // sign_extend(x, 0) is undefined
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4624 times.
4624 if (!n)
485 return 0;
486
487 4624 return sign_extend64(get_bits64(s, n), n);
488 }
489
490 /**
491 * Show 0-32 bits.
492 */
493 3465890 static inline unsigned int show_bits_long(GetBitContext *s, int n)
494 {
495
2/2
✓ Branch 0 taken 18557 times.
✓ Branch 1 taken 3447333 times.
3465890 if (n <= MIN_CACHE_BITS) {
496 18557 return show_bits(s, n);
497 } else {
498 3447333 GetBitContext gb = *s;
499 3447333 return get_bits_long(&gb, n);
500 }
501 }
502
503
504 /**
505 * Initialize GetBitContext.
506 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
507 * larger than the actual read bits because some optimized bitstream
508 * readers read 32 or 64 bit at once and could read over the end
509 * @param bit_size the size of the buffer in bits
510 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
511 */
512 42528150 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
513 int bit_size)
514 {
515 42528150 int ret = 0;
516
517
4/6
✓ Branch 0 taken 42528150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42528150 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 42528142 times.
42528150 if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
518 8 bit_size = 0;
519 8 buffer = NULL;
520 8 ret = AVERROR_INVALIDDATA;
521 }
522
523 42528150 s->buffer = buffer;
524 42528150 s->size_in_bits = bit_size;
525 42528150 s->size_in_bits_plus8 = bit_size + 8;
526 42528150 s->index = 0;
527
528 42528150 return ret;
529 }
530
531 /**
532 * Initialize GetBitContext.
533 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
534 * larger than the actual read bits because some optimized bitstream
535 * readers read 32 or 64 bit at once and could read over the end
536 * @param byte_size the size of the buffer in bytes
537 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
538 */
539 13374986 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
540 int byte_size)
541 {
542
2/4
✓ Branch 0 taken 13374986 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13374986 times.
13374986 if (byte_size > INT_MAX / 8 || byte_size < 0)
543 byte_size = -1;
544 13374986 return init_get_bits(s, buffer, byte_size * 8);
545 }
546
547 200 static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer,
548 int byte_size)
549 {
550
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)
551 byte_size = -1;
552 200 return init_get_bits(s, buffer, byte_size * 8);
553 }
554
555 257032 static inline const uint8_t *align_get_bits(GetBitContext *s)
556 {
557 257032 int n = -get_bits_count(s) & 7;
558
2/2
✓ Branch 0 taken 72693 times.
✓ Branch 1 taken 184339 times.
257032 if (n)
559 72693 skip_bits(s, n);
560 257032 return s->buffer + (s->index >> 3);
561 }
562
563 /**
564 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
565 * If the vlc code is invalid and max_depth>1, then the number of bits removed
566 * is undefined.
567 */
568 #define GET_VLC(code, name, gb, table, bits, max_depth) \
569 do { \
570 int n, nb_bits; \
571 unsigned int index; \
572 \
573 index = SHOW_UBITS(name, gb, bits); \
574 code = table[index].sym; \
575 n = table[index].len; \
576 \
577 if (max_depth > 1 && n < 0) { \
578 LAST_SKIP_BITS(name, gb, bits); \
579 UPDATE_CACHE(name, gb); \
580 \
581 nb_bits = -n; \
582 \
583 index = SHOW_UBITS(name, gb, nb_bits) + code; \
584 code = table[index].sym; \
585 n = table[index].len; \
586 if (max_depth > 2 && n < 0) { \
587 LAST_SKIP_BITS(name, gb, nb_bits); \
588 UPDATE_CACHE(name, gb); \
589 \
590 nb_bits = -n; \
591 \
592 index = SHOW_UBITS(name, gb, nb_bits) + code; \
593 code = table[index].sym; \
594 n = table[index].len; \
595 } \
596 } \
597 SKIP_BITS(name, gb, n); \
598 } while (0)
599
600 #define GET_RL_VLC(level, run, name, gb, table, bits, \
601 max_depth, need_update) \
602 do { \
603 int n, nb_bits; \
604 unsigned int index; \
605 \
606 index = SHOW_UBITS(name, gb, bits); \
607 level = table[index].level; \
608 n = table[index].len8; \
609 \
610 if (max_depth > 1 && n < 0) { \
611 SKIP_BITS(name, gb, bits); \
612 if (need_update) { \
613 UPDATE_CACHE(name, gb); \
614 } \
615 \
616 nb_bits = -n; \
617 \
618 index = SHOW_UBITS(name, gb, nb_bits) + level; \
619 level = table[index].level; \
620 n = table[index].len8; \
621 if (max_depth > 2 && n < 0) { \
622 LAST_SKIP_BITS(name, gb, nb_bits); \
623 if (need_update) { \
624 UPDATE_CACHE(name, gb); \
625 } \
626 nb_bits = -n; \
627 \
628 index = SHOW_UBITS(name, gb, nb_bits) + level; \
629 level = table[index].level; \
630 n = table[index].len8; \
631 } \
632 } \
633 run = table[index].run; \
634 SKIP_BITS(name, gb, n); \
635 } while (0)
636
637 /**
638 * Parse a vlc code.
639 * @param bits is the number of bits which will be read at once, must be
640 * identical to nb_bits in vlc_init()
641 * @param max_depth is the number of times bits bits must be read to completely
642 * read the longest vlc code
643 * = (max_vlc_length + bits - 1) / bits
644 * @returns the code parsed or -1 if no vlc matches
645 */
646 286128042 static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table,
647 int bits, int max_depth)
648 {
649 int code;
650
651 286128042 OPEN_READER(re, s);
652 286128042 UPDATE_CACHE(re, s);
653
654
10/10
✓ Branch 0 taken 7704704 times.
✓ Branch 1 taken 175275886 times.
✓ Branch 2 taken 103501626 times.
✓ Branch 3 taken 32187212 times.
✓ Branch 4 taken 141317027 times.
✓ Branch 5 taken 102488 times.
✓ Branch 6 taken 22893807 times.
✓ Branch 7 taken 2194561 times.
✓ Branch 8 taken 630166 times.
✓ Branch 9 taken 22263069 times.
286128042 GET_VLC(code, re, s, table, bits, max_depth);
655
656 286128042 CLOSE_READER(re, s);
657
658 286128042 return code;
659 }
660
661 static inline int get_vlc_multi(GetBitContext *s, uint8_t *dst,
662 const VLC_MULTI_ELEM *const Jtable,
663 const VLCElem *const table,
664 const int bits, const int max_depth,
665 const int symbols_size)
666 {
667 dst[0] = get_vlc2(s, table, bits, max_depth);
668 return 1;
669 }
670
671 378883 static inline int decode012(GetBitContext *gb)
672 {
673 int n;
674 378883 n = get_bits1(gb);
675
2/2
✓ Branch 0 taken 263935 times.
✓ Branch 1 taken 114948 times.
378883 if (n == 0)
676 263935 return 0;
677 else
678 114948 return get_bits1(gb) + 1;
679 }
680
681 191971 static inline int decode210(GetBitContext *gb)
682 {
683
2/2
✓ Branch 1 taken 135836 times.
✓ Branch 2 taken 56135 times.
191971 if (get_bits1(gb))
684 135836 return 0;
685 else
686 56135 return 2 - get_bits1(gb);
687 }
688
689 271854480 static inline int get_bits_left(GetBitContext *gb)
690 {
691 271854480 return gb->size_in_bits - get_bits_count(gb);
692 }
693
694 135234 static inline int skip_1stop_8data_bits(GetBitContext *gb)
695 {
696
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 135234 times.
135234 if (get_bits_left(gb) <= 0)
697 return AVERROR_INVALIDDATA;
698
699
2/2
✓ Branch 1 taken 3114 times.
✓ Branch 2 taken 135234 times.
138348 while (get_bits1(gb)) {
700 3114 skip_bits(gb, 8);
701
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3114 times.
3114 if (get_bits_left(gb) <= 0)
702 return AVERROR_INVALIDDATA;
703 }
704
705 135234 return 0;
706 }
707
708 #endif // CACHED_BITSTREAM_READER
709
710 #endif /* AVCODEC_GET_BITS_H */
711