ADPCM Codecs
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 
26 #pragma once
27 #include <stdint.h>
28 #include "config-adpcm.h"
29 #include "intreadwrite.h"
30 #include "adpcm.h"
31 
32 namespace adpcm_ffmpeg {
33 
34 /*
35  * Safe bitstream reading:
36  * optionally, the get_bits API can check to ensure that we
37  * don't read past input buffer boundaries. This is protected
38  * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
39  * then below that with UNCHECKED_BITSTREAM_READER at the per-
40  * decoder level. This means that decoders that check internally
41  * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
42  * overread checks.
43  * Boundary checking causes a minor performance penalty so for
44  * applications that won't want/need this, it can be disabled
45  * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
46  */
47 #ifndef UNCHECKED_BITSTREAM_READER
48 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
49 #endif
50 
51 #ifndef CACHED_BITSTREAM_READER
52 #define CACHED_BITSTREAM_READER 0
53 #endif
54 
55 #if CACHED_BITSTREAM_READER
56 
57 // we always want the LE implementation, to provide get_bits_le()
58 #define BITSTREAM_LE
59 
60 #ifndef BITSTREAM_READER_LE
61 # define BITSTREAM_BE
62 # define BITSTREAM_DEFAULT_BE
63 #endif
64 
65 #include "bitstream.h"
66 
67 #undef BITSTREAM_LE
68 #undef BITSTREAM_BE
69 #undef BITSTREAM_DEFAULT_BE
70 
71 #define get_bits_count bits_tell
72 #define get_bits_left bits_left
73 #define skip_bits_long bits_skip
74 #define skip_bits bits_skip
75 #define get_bits bits_read_nz
76 #define get_bitsz bits_read
77 #define get_bits_long bits_read
78 #define get_bits1 bits_read_bit
79 #define get_bits64 bits_read_64
80 #define get_xbits bits_read_xbits
81 #define get_sbits bits_read_signed_nz
82 #define get_sbits_long bits_read_signed
83 #define show_bits bits_peek
84 #define show_bits_long bits_peek
85 #define init_get_bits bits_init
86 #define init_get_bits8 bits_init8
87 #define align_get_bits bits_align
88 #define get_vlc2 bits_read_vlc
89 
90 #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size)
91 #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n)
92 
93 #define show_bits1(s) bits_peek(s, 1)
94 #define skip_bits1(s) bits_skip(s, 1)
95 
96 #define skip_1stop_8data_bits bits_skip_1stop_8data
97 
98 #else // CACHED_BITSTREAM_READER
99 
101  const uint8_t *buffer, *buffer_end;
102  int index;
103  int size_in_bits;
104  int size_in_bits_plus8;
105 };
106 
107 
108 /* Bitstream reader API docs:
109  * name
110  * arbitrary name which is used as prefix for the internal variables
111  *
112  * gb
113  * getbitcontext
114  *
115  * OPEN_READER(name, gb)
116  * load gb into local variables
117  *
118  * CLOSE_READER(name, gb)
119  * store local vars in gb
120  *
121  * UPDATE_CACHE(name, gb)
122  * Refill the internal cache from the bitstream.
123  * After this call at least MIN_CACHE_BITS will be available.
124  *
125  * GET_CACHE(name, gb)
126  * Will output the contents of the internal cache,
127  * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
128  *
129  * SHOW_UBITS(name, gb, num)
130  * Will return the next num bits.
131  *
132  * SHOW_SBITS(name, gb, num)
133  * Will return the next num bits and do sign extension.
134  *
135  * SKIP_BITS(name, gb, num)
136  * Will skip over the next num bits.
137  * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
138  *
139  * SKIP_CACHE(name, gb, num)
140  * Will remove the next num bits from the cache (note SKIP_COUNTER
141  * MUST be called before UPDATE_CACHE / CLOSE_READER).
142  *
143  * SKIP_COUNTER(name, gb, num)
144  * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
145  *
146  * LAST_SKIP_BITS(name, gb, num)
147  * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
148  *
149  * BITS_LEFT(name, gb)
150  * Return the number of bits left
151  *
152  * For examples see get_bits, show_bits, skip_bits, get_vlc.
153  */
154 
155 #if defined LONG_BITSTREAM_READER
156 # define MIN_CACHE_BITS 32
157 #else
158 # define MIN_CACHE_BITS 25
159 #endif
160 
161 #define OPEN_READER_NOSIZE(name, gb) \
162  unsigned int name ## _index = (gb)->index; \
163  unsigned int av_unused name ## _cache
164 
165 #if UNCHECKED_BITSTREAM_READER
166 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
167 
168 #define BITS_AVAILABLE(name, gb) 1
169 #else
170 #define OPEN_READER(name, gb) \
171  OPEN_READER_NOSIZE(name, gb); \
172  unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
173 
174 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
175 #endif
176 
177 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
178 
179 # ifdef LONG_BITSTREAM_READER
180 
181 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
182  AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
183 
184 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
185  AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
186 
187 #else
188 
189 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
190  AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
191 
192 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
193  AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
194 
195 #endif
196 
197 
198 #ifdef BITSTREAM_READER_LE
199 
200 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
201 
202 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
203 
204 #else
205 
206 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
207 
208 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
209 
210 #endif
211 
212 #if UNCHECKED_BITSTREAM_READER
213 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
214 #else
215 # define SKIP_COUNTER(name, gb, num) \
216  name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
217 #endif
218 
219 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
220 
221 #define SKIP_BITS(name, gb, num) \
222  do { \
223  SKIP_CACHE(name, gb, num); \
224  SKIP_COUNTER(name, gb, num); \
225  } while (0)
226 
227 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
228 
229 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
230 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
231 
232 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
233 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
234 
235 #ifdef BITSTREAM_READER_LE
236 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
237 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
238 #else
239 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
240 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
241 #endif
242 
243 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
244 
245 
246 static inline int get_bits_count(const GetBitContext *s)
247 {
248  return s->index;
249 }
250 
258 static inline void skip_bits_long(GetBitContext *s, int n)
259 {
260 #if UNCHECKED_BITSTREAM_READER
261  s->index += n;
262 #else
263  s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
264 #endif
265 }
266 
272 static inline int get_xbits(GetBitContext *s, int n)
273 {
274  int sign;
275  int32_t cache;
276  OPEN_READER(re, s);
277  av_assert(n>0 && n<=25);
278  UPDATE_CACHE(re, s);
279  cache = GET_CACHE(re, s);
280  sign = ~cache >> 31;
281  LAST_SKIP_BITS(re, s, n);
282  CLOSE_READER(re, s);
283  return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
284 }
285 
286 static inline int get_xbits_le(GetBitContext *s, int n)
287 {
288  int sign;
289  int32_t cache;
290  OPEN_READER(re, s);
291  av_assert(n>0 && n<=25);
292  UPDATE_CACHE_LE(re, s);
293  cache = GET_CACHE(re, s);
294  sign = sign_extend(~cache, n) >> 31;
295  LAST_SKIP_BITS(re, s, n);
296  CLOSE_READER(re, s);
297  return (zero_extend(sign ^ cache, n) ^ sign) - sign;
298 }
299 
300 static inline int get_sbits(GetBitContext *s, int n)
301 {
302  int tmp;
303  OPEN_READER(re, s);
304  av_assert(n>0 && n<=25);
305  UPDATE_CACHE(re, s);
306  tmp = SHOW_SBITS(re, s, n);
307  LAST_SKIP_BITS(re, s, n);
308  CLOSE_READER(re, s);
309  return tmp;
310 }
311 
315 static unsigned int get_bits(GetBitContext *s, int n)
316 {
317  unsigned int tmp;
318  OPEN_READER(re, s);
319  av_assert(n>0 && n<=25);
320  UPDATE_CACHE(re, s);
321  tmp = SHOW_UBITS(re, s, n);
322  LAST_SKIP_BITS(re, s, n);
323  CLOSE_READER(re, s);
324  av_assert(tmp < UINT64_C(1) << n);
325  return tmp;
326 }
327 
331 static av_always_inline int get_bitsz(GetBitContext *s, int n)
332 {
333  return n ? get_bits(s, n) : 0;
334 }
335 
336 static inline unsigned int get_bits_le(GetBitContext *s, int n)
337 {
338  int tmp;
339  OPEN_READER(re, s);
340  av_assert(n>0 && n<=25);
341  UPDATE_CACHE_LE(re, s);
342  tmp = SHOW_UBITS_LE(re, s, n);
343  LAST_SKIP_BITS(re, s, n);
344  CLOSE_READER(re, s);
345  return tmp;
346 }
347 
351 static inline unsigned int show_bits(GetBitContext *s, int n)
352 {
353  unsigned int tmp;
354  OPEN_READER_NOSIZE(re, s);
355  av_assert(n>0 && n<=25);
356  UPDATE_CACHE(re, s);
357  tmp = SHOW_UBITS(re, s, n);
358  return tmp;
359 }
360 
361 static inline void skip_bits(GetBitContext *s, int n)
362 {
363  OPEN_READER(re, s);
364  LAST_SKIP_BITS(re, s, n);
365  CLOSE_READER(re, s);
366 }
367 
368 static inline unsigned int get_bits1(GetBitContext *s)
369 {
370  unsigned int index = s->index;
371  uint8_t result = s->buffer[index >> 3];
372 #ifdef BITSTREAM_READER_LE
373  result >>= index & 7;
374  result &= 1;
375 #else
376  result <<= index & 7;
377  result >>= 8 - 1;
378 #endif
379 #if !UNCHECKED_BITSTREAM_READER
380  if (s->index < s->size_in_bits_plus8)
381 #endif
382  index++;
383  s->index = index;
384 
385  return result;
386 }
387 
388 static inline unsigned int show_bits1(GetBitContext *s)
389 {
390  return show_bits(s, 1);
391 }
392 
393 static inline void skip_bits1(GetBitContext *s)
394 {
395  skip_bits(s, 1);
396 }
397 
401 static inline unsigned int get_bits_long(GetBitContext *s, int n)
402 {
403  av_assert(n>=0 && n<=32);
404  if (!n) {
405  return 0;
406  } else if (n <= MIN_CACHE_BITS) {
407  return get_bits(s, n);
408  } else {
409 #ifdef BITSTREAM_READER_LE
410  unsigned ret = get_bits(s, 16);
411  return ret | (get_bits(s, n - 16) << 16);
412 #else
413  unsigned ret = get_bits(s, 16) << (n - 16);
414  return ret | get_bits(s, n - 16);
415 #endif
416  }
417 }
418 
422 static inline uint64_t get_bits64(GetBitContext *s, int n)
423 {
424  if (n <= 32) {
425  return get_bits_long(s, n);
426  } else {
427 #ifdef BITSTREAM_READER_LE
428  uint64_t ret = get_bits_long(s, 32);
429  return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
430 #else
431  uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
432  return ret | get_bits_long(s, 32);
433 #endif
434  }
435 }
436 
440 static inline int get_sbits_long(GetBitContext *s, int n)
441 {
442  // sign_extend(x, 0) is undefined
443  if (!n)
444  return 0;
445 
446  return sign_extend(get_bits_long(s, n), n);
447 }
448 
452 static inline int64_t get_sbits64(GetBitContext *s, int n)
453 {
454  // sign_extend(x, 0) is undefined
455  if (!n)
456  return 0;
457 
458  return sign_extend64(get_bits64(s, n), n);
459 }
460 
464 static inline unsigned int show_bits_long(GetBitContext *s, int n)
465 {
466  if (n <= MIN_CACHE_BITS) {
467  return show_bits(s, n);
468  } else {
469  GetBitContext gb = *s;
470  return get_bits_long(&gb, n);
471  }
472 }
473 
474 
483 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
484  int bit_size)
485 {
486  int buffer_size;
487  int ret = 0;
488 
489  if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
490  bit_size = 0;
491  buffer = NULL;
492  ret = AVERROR_INVALIDDATA;
493  }
494 
495  buffer_size = (bit_size + 7) >> 3;
496 
497  s->buffer = buffer;
498  s->size_in_bits = bit_size;
499  s->size_in_bits_plus8 = bit_size + 8;
500  s->buffer_end = buffer + buffer_size;
501  s->index = 0;
502 
503  return ret;
504 }
505 
514 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
515  int byte_size)
516 {
517  if (byte_size > INT_MAX / 8 || byte_size < 0)
518  byte_size = -1;
519  return init_get_bits(s, buffer, byte_size * 8);
520 }
521 
522 static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer,
523  int byte_size)
524 {
525  if (byte_size > INT_MAX / 8 || byte_size < 0)
526  byte_size = -1;
527  return init_get_bits(s, buffer, byte_size * 8);
528 }
529 
530 static inline const uint8_t *align_get_bits(GetBitContext *s)
531 {
532  int n = -get_bits_count(s) & 7;
533  if (n)
534  skip_bits(s, n);
535  return s->buffer + (s->index >> 3);
536 }
537 
543 #define GET_VLC(code, name, gb, table, bits, max_depth) \
544  do { \
545  int n, nb_bits; \
546  unsigned int index; \
547  \
548  index = SHOW_UBITS(name, gb, bits); \
549  code = table[index].sym; \
550  n = table[index].len; \
551  \
552  if (max_depth > 1 && n < 0) { \
553  LAST_SKIP_BITS(name, gb, bits); \
554  UPDATE_CACHE(name, gb); \
555  \
556  nb_bits = -n; \
557  \
558  index = SHOW_UBITS(name, gb, nb_bits) + code; \
559  code = table[index].sym; \
560  n = table[index].len; \
561  if (max_depth > 2 && n < 0) { \
562  LAST_SKIP_BITS(name, gb, nb_bits); \
563  UPDATE_CACHE(name, gb); \
564  \
565  nb_bits = -n; \
566  \
567  index = SHOW_UBITS(name, gb, nb_bits) + code; \
568  code = table[index].sym; \
569  n = table[index].len; \
570  } \
571  } \
572  SKIP_BITS(name, gb, n); \
573  } while (0)
574 
575 #define GET_RL_VLC(level, run, name, gb, table, bits, \
576  max_depth, need_update) \
577  do { \
578  int n, nb_bits; \
579  unsigned int index; \
580  \
581  index = SHOW_UBITS(name, gb, bits); \
582  level = table[index].level; \
583  n = table[index].len; \
584  \
585  if (max_depth > 1 && n < 0) { \
586  SKIP_BITS(name, gb, bits); \
587  if (need_update) { \
588  UPDATE_CACHE(name, gb); \
589  } \
590  \
591  nb_bits = -n; \
592  \
593  index = SHOW_UBITS(name, gb, nb_bits) + level; \
594  level = table[index].level; \
595  n = table[index].len; \
596  if (max_depth > 2 && n < 0) { \
597  LAST_SKIP_BITS(name, gb, nb_bits); \
598  if (need_update) { \
599  UPDATE_CACHE(name, gb); \
600  } \
601  nb_bits = -n; \
602  \
603  index = SHOW_UBITS(name, gb, nb_bits) + level; \
604  level = table[index].level; \
605  n = table[index].len; \
606  } \
607  } \
608  run = table[index].run; \
609  SKIP_BITS(name, gb, n); \
610  } while (0)
611 
621 // static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table,
622 // int bits, int max_depth)
623 // {
624 // int code;
625 
626 // OPEN_READER(re, s);
627 // UPDATE_CACHE(re, s);
628 
629 // GET_VLC(code, re, s, table, bits, max_depth);
630 
631 // CLOSE_READER(re, s);
632 
633 // return code;
634 // }
635 
636 static inline int decode012(GetBitContext *gb)
637 {
638  int n;
639  n = get_bits1(gb);
640  if (n == 0)
641  return 0;
642  else
643  return get_bits1(gb) + 1;
644 }
645 
646 static inline int decode210(GetBitContext *gb)
647 {
648  if (get_bits1(gb))
649  return 0;
650  else
651  return 2 - get_bits1(gb);
652 }
653 
654 static inline int get_bits_left(GetBitContext *gb)
655 {
656  return gb->size_in_bits - get_bits_count(gb);
657 }
658 
659 static inline int skip_1stop_8data_bits(GetBitContext *gb)
660 {
661  if (get_bits_left(gb) <= 0)
662  return AVERROR_INVALIDDATA;
663 
664  while (get_bits1(gb)) {
665  skip_bits(gb, 8);
666  if (get_bits_left(gb) <= 0)
667  return AVERROR_INVALIDDATA;
668  }
669 
670  return 0;
671 }
672 
673 #endif // CACHED_BITSTREAM_READER
674 
675 }
676 
Definition: get_bits.h:100