FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
get_bits.h
Go to the documentation of this file.
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 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/log.h"
33 #include "libavutil/avassert.h"
34 #include "mathops.h"
35 
36 /*
37  * Safe bitstream reading:
38  * optionally, the get_bits API can check to ensure that we
39  * don't read past input buffer boundaries. This is protected
40  * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
41  * then below that with UNCHECKED_BITSTREAM_READER at the per-
42  * decoder level. This means that decoders that check internally
43  * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
44  * overread checks.
45  * Boundary checking causes a minor performance penalty so for
46  * applications that won't want/need this, it can be disabled
47  * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
48  */
49 #ifndef UNCHECKED_BITSTREAM_READER
50 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
51 #endif
52 
53 typedef struct GetBitContext {
55  int index;
59 
60 #define VLC_TYPE int16_t
61 
62 typedef struct VLC {
63  int bits;
64  VLC_TYPE (*table)[2]; ///< code, bits
66 } VLC;
67 
68 typedef struct RL_VLC_ELEM {
69  int16_t level;
70  int8_t len;
72 } RL_VLC_ELEM;
73 
74 /* Bitstream reader API docs:
75 name
76  arbitrary name which is used as prefix for the internal variables
77 
78 gb
79  getbitcontext
80 
81 OPEN_READER(name, gb)
82  load gb into local variables
83 
84 CLOSE_READER(name, gb)
85  store local vars in gb
86 
87 UPDATE_CACHE(name, gb)
88  refill the internal cache from the bitstream
89  after this call at least MIN_CACHE_BITS will be available,
90 
91 GET_CACHE(name, gb)
92  will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
93 
94 SHOW_UBITS(name, gb, num)
95  will return the next num bits
96 
97 SHOW_SBITS(name, gb, num)
98  will return the next num bits and do sign extension
99 
100 SKIP_BITS(name, gb, num)
101  will skip over the next num bits
102  note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
103 
104 SKIP_CACHE(name, gb, num)
105  will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
106 
107 SKIP_COUNTER(name, gb, num)
108  will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
109 
110 LAST_SKIP_BITS(name, gb, num)
111  like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER
112 
113 for examples see get_bits, show_bits, skip_bits, get_vlc
114 */
115 
116 #ifdef LONG_BITSTREAM_READER
117 # define MIN_CACHE_BITS 32
118 #else
119 # define MIN_CACHE_BITS 25
120 #endif
121 
122 #if UNCHECKED_BITSTREAM_READER
123 #define OPEN_READER(name, gb) \
124  unsigned int name##_index = (gb)->index; \
125  av_unused unsigned int name##_cache
126 
127 #define HAVE_BITS_REMAINING(name, gb) 1
128 #else
129 #define OPEN_READER(name, gb) \
130  unsigned int name##_index = (gb)->index; \
131  unsigned int av_unused name##_cache = 0; \
132  unsigned int av_unused name##_size_plus8 = \
133  (gb)->size_in_bits_plus8
134 
135 #define HAVE_BITS_REMAINING(name, gb) \
136  name##_index < name##_size_plus8
137 #endif
138 
139 #define CLOSE_READER(name, gb) (gb)->index = name##_index
140 
141 #ifdef BITSTREAM_READER_LE
142 
143 # ifdef LONG_BITSTREAM_READER
144 # define UPDATE_CACHE(name, gb) name##_cache = \
145  AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
146 # else
147 # define UPDATE_CACHE(name, gb) name##_cache = \
148  AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
149 # endif
150 
151 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
152 
153 #else
154 
155 # ifdef LONG_BITSTREAM_READER
156 # define UPDATE_CACHE(name, gb) name##_cache = \
157  AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
158 # else
159 # define UPDATE_CACHE(name, gb) name##_cache = \
160  AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
161 # endif
162 
163 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
164 
165 #endif
166 
167 #if UNCHECKED_BITSTREAM_READER
168 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
169 #else
170 # define SKIP_COUNTER(name, gb, num) \
171  name##_index = FFMIN(name##_size_plus8, name##_index + (num))
172 #endif
173 
174 #define SKIP_BITS(name, gb, num) do { \
175  SKIP_CACHE(name, gb, num); \
176  SKIP_COUNTER(name, gb, num); \
177  } while (0)
178 
179 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
180 
181 #ifdef BITSTREAM_READER_LE
182 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
183 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
184 #else
185 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
186 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
187 #endif
188 
189 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
190 
191 static inline int get_bits_count(const GetBitContext *s)
192 {
193  return s->index;
194 }
195 
196 static inline void skip_bits_long(GetBitContext *s, int n){
197 #if UNCHECKED_BITSTREAM_READER
198  s->index += n;
199 #else
200  s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
201 #endif
202 }
203 
204 /**
205  * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
206  * if MSB not set it is negative
207  * @param n length in bits
208  */
209 static inline int get_xbits(GetBitContext *s, int n)
210 {
211  register int sign;
212  register int32_t cache;
213  OPEN_READER(re, s);
214  UPDATE_CACHE(re, s);
215  cache = GET_CACHE(re, s);
216  sign = ~cache >> 31;
217  LAST_SKIP_BITS(re, s, n);
218  CLOSE_READER(re, s);
219  return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
220 }
221 
222 static inline int get_sbits(GetBitContext *s, int n)
223 {
224  register int tmp;
225  OPEN_READER(re, s);
226  av_assert2(n>0 && n<=25);
227  UPDATE_CACHE(re, s);
228  tmp = SHOW_SBITS(re, s, n);
229  LAST_SKIP_BITS(re, s, n);
230  CLOSE_READER(re, s);
231  return tmp;
232 }
233 
234 /**
235  * Read 1-25 bits.
236  */
237 static inline unsigned int get_bits(GetBitContext *s, int n)
238 {
239  register int tmp;
240  OPEN_READER(re, s);
241  av_assert2(n>0 && n<=25);
242  UPDATE_CACHE(re, s);
243  tmp = SHOW_UBITS(re, s, n);
244  LAST_SKIP_BITS(re, s, n);
245  CLOSE_READER(re, s);
246  return tmp;
247 }
248 
249 /**
250  * Show 1-25 bits.
251  */
252 static inline unsigned int show_bits(GetBitContext *s, int n)
253 {
254  register int tmp;
255  OPEN_READER(re, s);
256  av_assert2(n>0 && n<=25);
257  UPDATE_CACHE(re, s);
258  tmp = SHOW_UBITS(re, s, n);
259  return tmp;
260 }
261 
262 static inline void skip_bits(GetBitContext *s, int n)
263 {
264  OPEN_READER(re, s);
265  UPDATE_CACHE(re, s);
266  LAST_SKIP_BITS(re, s, n);
267  CLOSE_READER(re, s);
268 }
269 
270 static inline unsigned int get_bits1(GetBitContext *s)
271 {
272  unsigned int index = s->index;
273  uint8_t result = s->buffer[index>>3];
274 #ifdef BITSTREAM_READER_LE
275  result >>= index & 7;
276  result &= 1;
277 #else
278  result <<= index & 7;
279  result >>= 8 - 1;
280 #endif
281 #if !UNCHECKED_BITSTREAM_READER
282  if (s->index < s->size_in_bits_plus8)
283 #endif
284  index++;
285  s->index = index;
286 
287  return result;
288 }
289 
290 static inline unsigned int show_bits1(GetBitContext *s)
291 {
292  return show_bits(s, 1);
293 }
294 
295 static inline void skip_bits1(GetBitContext *s)
296 {
297  skip_bits(s, 1);
298 }
299 
300 /**
301  * Read 0-32 bits.
302  */
303 static inline unsigned int get_bits_long(GetBitContext *s, int n)
304 {
305  if (!n) {
306  return 0;
307  } else if (n <= MIN_CACHE_BITS)
308  return get_bits(s, n);
309  else {
310 #ifdef BITSTREAM_READER_LE
311  unsigned ret = get_bits(s, 16);
312  return ret | (get_bits(s, n-16) << 16);
313 #else
314  unsigned ret = get_bits(s, 16) << (n-16);
315  return ret | get_bits(s, n-16);
316 #endif
317  }
318 }
319 
320 /**
321  * Read 0-64 bits.
322  */
323 static inline uint64_t get_bits64(GetBitContext *s, int n)
324 {
325  if (n <= 32) {
326  return get_bits_long(s, n);
327  } else {
328 #ifdef BITSTREAM_READER_LE
329  uint64_t ret = get_bits_long(s, 32);
330  return ret | (uint64_t)get_bits_long(s, n - 32) << 32;
331 #else
332  uint64_t ret = (uint64_t)get_bits_long(s, n - 32) << 32;
333  return ret | get_bits_long(s, 32);
334 #endif
335  }
336 }
337 
338 /**
339  * Read 0-32 bits as a signed integer.
340  */
341 static inline int get_sbits_long(GetBitContext *s, int n)
342 {
343  return sign_extend(get_bits_long(s, n), n);
344 }
345 
346 /**
347  * Show 0-32 bits.
348  */
349 static inline unsigned int show_bits_long(GetBitContext *s, int n)
350 {
351  if (n <= MIN_CACHE_BITS)
352  return show_bits(s, n);
353  else {
354  GetBitContext gb = *s;
355  return get_bits_long(&gb, n);
356  }
357 }
358 
359 static inline int check_marker(GetBitContext *s, const char *msg)
360 {
361  int bit = get_bits1(s);
362  if (!bit)
363  av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
364 
365  return bit;
366 }
367 
368 /**
369  * Initialize GetBitContext.
370  * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes
371  * larger than the actual read bits because some optimized bitstream
372  * readers read 32 or 64 bit at once and could read over the end
373  * @param bit_size the size of the buffer in bits
374  * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
375  */
376 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
377  int bit_size)
378 {
379  int buffer_size;
380  int ret = 0;
381 
382  if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
383  buffer_size = bit_size = 0;
384  buffer = NULL;
385  ret = AVERROR_INVALIDDATA;
386  }
387 
388  buffer_size = (bit_size + 7) >> 3;
389 
390  s->buffer = buffer;
391  s->size_in_bits = bit_size;
392  s->size_in_bits_plus8 = bit_size + 8;
393  s->buffer_end = buffer + buffer_size;
394  s->index = 0;
395  return ret;
396 }
397 
398 /**
399  * Initialize GetBitContext.
400  * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes
401  * larger than the actual read bits because some optimized bitstream
402  * readers read 32 or 64 bit at once and could read over the end
403  * @param byte_size the size of the buffer in bytes
404  * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
405  */
406 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
407  int byte_size)
408 {
409  if (byte_size > INT_MAX / 8 || byte_size < 0)
410  byte_size = -1;
411  return init_get_bits(s, buffer, byte_size * 8);
412 }
413 
414 static inline void align_get_bits(GetBitContext *s)
415 {
416  int n = -get_bits_count(s) & 7;
417  if (n) skip_bits(s, n);
418 }
419 
420 #define init_vlc(vlc, nb_bits, nb_codes, \
421  bits, bits_wrap, bits_size, \
422  codes, codes_wrap, codes_size, \
423  flags) \
424  ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
425  bits, bits_wrap, bits_size, \
426  codes, codes_wrap, codes_size, \
427  NULL, 0, 0, flags)
428 
429 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
430  const void *bits, int bits_wrap, int bits_size,
431  const void *codes, int codes_wrap, int codes_size,
432  const void *symbols, int symbols_wrap, int symbols_size,
433  int flags);
434 #define INIT_VLC_LE 2
435 #define INIT_VLC_USE_NEW_STATIC 4
436 void ff_free_vlc(VLC *vlc);
437 
438 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
439  static VLC_TYPE table[static_size][2]; \
440  (vlc)->table = table; \
441  (vlc)->table_allocated = static_size; \
442  init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
443  } while (0)
444 
445 
446 /**
447  * If the vlc code is invalid and max_depth=1, then no bits will be removed.
448  * If the vlc code is invalid and max_depth>1, then the number of bits removed
449  * is undefined.
450  */
451 #define GET_VLC(code, name, gb, table, bits, max_depth) \
452  do { \
453  int n, nb_bits; \
454  unsigned int index; \
455  \
456  index = SHOW_UBITS(name, gb, bits); \
457  code = table[index][0]; \
458  n = table[index][1]; \
459  \
460  if (max_depth > 1 && n < 0) { \
461  LAST_SKIP_BITS(name, gb, bits); \
462  UPDATE_CACHE(name, gb); \
463  \
464  nb_bits = -n; \
465  \
466  index = SHOW_UBITS(name, gb, nb_bits) + code; \
467  code = table[index][0]; \
468  n = table[index][1]; \
469  if (max_depth > 2 && n < 0) { \
470  LAST_SKIP_BITS(name, gb, nb_bits); \
471  UPDATE_CACHE(name, gb); \
472  \
473  nb_bits = -n; \
474  \
475  index = SHOW_UBITS(name, gb, nb_bits) + code; \
476  code = table[index][0]; \
477  n = table[index][1]; \
478  } \
479  } \
480  SKIP_BITS(name, gb, n); \
481  } while (0)
482 
483 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
484  do { \
485  int n, nb_bits; \
486  unsigned int index; \
487  \
488  index = SHOW_UBITS(name, gb, bits); \
489  level = table[index].level; \
490  n = table[index].len; \
491  \
492  if (max_depth > 1 && n < 0) { \
493  SKIP_BITS(name, gb, bits); \
494  if (need_update) { \
495  UPDATE_CACHE(name, gb); \
496  } \
497  \
498  nb_bits = -n; \
499  \
500  index = SHOW_UBITS(name, gb, nb_bits) + level; \
501  level = table[index].level; \
502  n = table[index].len; \
503  } \
504  run = table[index].run; \
505  SKIP_BITS(name, gb, n); \
506  } while (0)
507 
508 
509 /**
510  * Parse a vlc code.
511  * @param bits is the number of bits which will be read at once, must be
512  * identical to nb_bits in init_vlc()
513  * @param max_depth is the number of times bits bits must be read to completely
514  * read the longest vlc code
515  * = (max_vlc_length + bits - 1) / bits
516  */
518  int bits, int max_depth)
519 {
520  int code;
521 
522  OPEN_READER(re, s);
523  UPDATE_CACHE(re, s);
524 
525  GET_VLC(code, re, s, table, bits, max_depth);
526 
527  CLOSE_READER(re, s);
528  return code;
529 }
530 
531 static inline int decode012(GetBitContext *gb)
532 {
533  int n;
534  n = get_bits1(gb);
535  if (n == 0)
536  return 0;
537  else
538  return get_bits1(gb) + 1;
539 }
540 
541 static inline int decode210(GetBitContext *gb)
542 {
543  if (get_bits1(gb))
544  return 0;
545  else
546  return 2 - get_bits1(gb);
547 }
548 
549 static inline int get_bits_left(GetBitContext *gb)
550 {
551  return gb->size_in_bits - get_bits_count(gb);
552 }
553 
554 //#define TRACE
555 
556 #ifdef TRACE
557 static inline void print_bin(int bits, int n)
558 {
559  int i;
560 
561  for (i = n-1; i >= 0; i--) {
562  av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
563  }
564  for (i = n; i < 24; i++)
565  av_log(NULL, AV_LOG_DEBUG, " ");
566 }
567 
568 static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
569  const char *func, int line)
570 {
571  int r = get_bits(s, n);
572 
573  print_bin(r, n);
574  av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
575  r, n, r, get_bits_count(s)-n, file, func, line);
576  return r;
577 }
578 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
579  int bits, int max_depth, const char *file,
580  const char *func, int line)
581 {
582  int show = show_bits(s, 24);
583  int pos = get_bits_count(s);
584  int r = get_vlc2(s, table, bits, max_depth);
585  int len = get_bits_count(s) - pos;
586  int bits2 = show >> (24-len);
587 
588  print_bin(bits2, len);
589 
590  av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
591  bits2, len, r, pos, file, func, line);
592  return r;
593 }
594 static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
595  const char *func, int line)
596 {
597  int show = show_bits(s, n);
598  int r = get_xbits(s, n);
599 
600  print_bin(show, n);
601  av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
602  show, n, r, get_bits_count(s)-n, file, func, line);
603  return r;
604 }
605 
606 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
607 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
608 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
609 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
610 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
611 
612 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
613 
614 #else //TRACE
615 #define tprintf(p, ...) {}
616 #endif
617 
618 #endif /* AVCODEC_GET_BITS_H */