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