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