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
34
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
71
72 #undef BITSTREAM_LE
73 #undef BITSTREAM_BE
74 #undef BITSTREAM_DEFAULT_BE
75
77
78 #define get_bits_count bits_tell
79 #define get_bits_left bits_left
80 #define skip_bits_long bits_skip
81 #define skip_bits bits_skip
82 #define get_bits bits_read_nz
83 #define get_bitsz bits_read
84 #define get_bits_long bits_read
85 #define get_bits1 bits_read_bit
86 #define get_bits64 bits_read_64
87 #define get_xbits bits_read_xbits
88 #define get_sbits bits_read_signed_nz
89 #define get_sbits_long bits_read_signed
90 #define show_bits bits_peek
91 #define show_bits_long bits_peek
92 #define init_get_bits bits_init
93 #define init_get_bits8 bits_init8
94 #define align_get_bits bits_align
95 #define get_vlc2 bits_read_vlc
96
97 #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size)
98 #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n)
99
100 #define show_bits1(s) bits_peek(s, 1)
101 #define skip_bits1(s) bits_skip(s, 1)
102
103 #define skip_1stop_8data_bits bits_skip_1stop_8data
104
105 #else // CACHED_BITSTREAM_READER
106
113
117
118 /* Bitstream reader API docs:
119 * name
120 * arbitrary name which is used as prefix for the internal variables
121 *
122 * gb
123 * getbitcontext
124 *
125 * OPEN_READER(name, gb)
126 * load gb into local variables
127 *
128 * CLOSE_READER(name, gb)
129 * store local vars in gb
130 *
131 * UPDATE_CACHE(name, gb)
132 * Refill the internal cache from the bitstream.
133 * After this call at least MIN_CACHE_BITS will be available.
134 *
135 * GET_CACHE(name, gb)
136 * Will output the contents of the internal cache,
137 * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
138 *
139 * SHOW_UBITS(name, gb, num)
140 * Will return the next num bits.
141 *
142 * SHOW_SBITS(name, gb, num)
143 * Will return the next num bits and do sign extension.
144 *
145 * SKIP_BITS(name, gb, num)
146 * Will skip over the next num bits.
147 * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
148 *
149 * SKIP_CACHE(name, gb, num)
150 * Will remove the next num bits from the cache (note SKIP_COUNTER
151 * MUST be called before UPDATE_CACHE / CLOSE_READER).
152 *
153 * SKIP_COUNTER(name, gb, num)
154 * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
155 *
156 * LAST_SKIP_BITS(name, gb, num)
157 * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
158 *
159 * BITS_LEFT(name, gb)
160 * Return the number of bits left
161 *
162 * For examples see get_bits, show_bits, skip_bits, get_vlc.
163 */
164
165 #if defined LONG_BITSTREAM_READER
166 # define MIN_CACHE_BITS 32
167 #else
168 # define MIN_CACHE_BITS 25
169 #endif
170
171 #define OPEN_READER_NOSIZE(name, gb) \
172 unsigned int name ## _index = (gb)->index; \
173 unsigned int av_unused name ## _cache
174
175 #if UNCHECKED_BITSTREAM_READER
176 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
177
178 #define BITS_AVAILABLE(name, gb) 1
179 #else
180 #define OPEN_READER(name, gb) \
181 OPEN_READER_NOSIZE(name, gb); \
182 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
183
184 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
185 #endif
186
187 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
188
189 # ifdef LONG_BITSTREAM_READER
190
191 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
192 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
193
194 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
195 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
196
197 #else
198
199 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
200 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
201
202 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
203 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
204
205 #endif
206
207
208 #ifdef BITSTREAM_READER_LE
209
210 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
211
212 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
213
214 #else
215
216 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
217
218 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
219
220 #endif
221
222 #if UNCHECKED_BITSTREAM_READER
223 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
224 #else
225 # define SKIP_COUNTER(name, gb, num) \
226 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
227 #endif
228
229 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
230
231 #define SKIP_BITS(name, gb, num) \
232 do { \
233 SKIP_CACHE(name, gb, num); \
234 SKIP_COUNTER(name, gb, num); \
235 } while (0)
236
237 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
238
239 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
240 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
241
242 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
243 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
244
245 #ifdef BITSTREAM_READER_LE
246 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
247 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
248 #else
249 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
250 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
251 #endif
252
253 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
254
255
257 {
259 }
260
261 /**
262 * Skips the specified number of bits.
263 * @param n the number of bits to skip,
264 * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
265 * from the start to overflow int32_t. Staying within the bitstream + padding
266 * is sufficient, too.
267 */
269 {
270 #if UNCHECKED_BITSTREAM_READER
272 #else
273 s->index +=
av_clip(n, -
s->index,
s->size_in_bits_plus8 -
s->index);
274 #endif
275 }
276
277 /**
278 * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
279 * if MSB not set it is negative
280 * @param n length in bits
281 */
283 {
284 register int sign;
290 sign = ~cache >> 31;
293 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
294 }
295
297 {
298 register int sign;
307 return (
zero_extend(sign ^ cache, n) ^ sign) - sign;
308 }
309
311 {
320 }
321
322 /**
323 * Read 1-25 bits.
324 */
326 {
327 register unsigned int tmp;
336 }
337
338 /**
339 * Read 0-25 bits.
340 */
342 {
344 }
345
347 {
356 }
357
358 /**
359 * Show 1-25 bits.
360 */
362 {
363 register unsigned int tmp;
369 }
370
372 {
376 }
377
379 {
380 unsigned int index =
s->index;
382 #ifdef BITSTREAM_READER_LE
385 #else
388 #endif
389 #if !UNCHECKED_BITSTREAM_READER
390 if (
s->index <
s->size_in_bits_plus8)
391 #endif
394
396 }
397
399 {
401 }
402
404 {
406 }
407
408 /**
409 * Read 0-32 bits.
410 */
412 {
414 if (!n) {
415 return 0;
418 } else {
419 #ifdef BITSTREAM_READER_LE
422 #else
425 #endif
426 }
427 }
428
429 /**
430 * Read 0-64 bits.
431 */
433 {
434 if (n <= 32) {
436 } else {
437 #ifdef BITSTREAM_READER_LE
440 #else
443 #endif
444 }
445 }
446
447 /**
448 * Read 0-32 bits as a signed integer.
449 */
451 {
452 // sign_extend(x, 0) is undefined
453 if (!n)
454 return 0;
455
457 }
458
459 /**
460 * Read 0-64 bits as a signed integer.
461 */
463 {
464 // sign_extend(x, 0) is undefined
465 if (!n)
466 return 0;
467
469 }
470
471 /**
472 * Show 0-32 bits.
473 */
475 {
478 } else {
481 }
482 }
483
484
485 /**
486 * Initialize GetBitContext.
487 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
488 * larger than the actual read bits because some optimized bitstream
489 * readers read 32 or 64 bit at once and could read over the end
490 * @param bit_size the size of the buffer in bits
491 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
492 */
494 int bit_size)
495 {
496 int buffer_size;
498
500 bit_size = 0;
503 }
504
505 buffer_size = (bit_size + 7) >> 3;
506
508 s->size_in_bits = bit_size;
509 s->size_in_bits_plus8 = bit_size + 8;
510 s->buffer_end =
buffer + buffer_size;
512
514 }
515
516 /**
517 * Initialize GetBitContext.
518 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
519 * larger than the actual read bits because some optimized bitstream
520 * readers read 32 or 64 bit at once and could read over the end
521 * @param byte_size the size of the buffer in bytes
522 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
523 */
525 int byte_size)
526 {
527 if (byte_size > INT_MAX / 8 || byte_size < 0)
528 byte_size = -1;
530 }
531
533 int byte_size)
534 {
535 if (byte_size > INT_MAX / 8 || byte_size < 0)
536 byte_size = -1;
538 }
539
541 {
543 if (n)
545 return s->buffer + (
s->index >> 3);
546 }
547
548 /**
549 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
550 * If the vlc code is invalid and max_depth>1, then the number of bits removed
551 * is undefined.
552 */
553 #define GET_VLC(code, name, gb, table, bits, max_depth) \
554 do { \
555 int n, nb_bits; \
556 unsigned int index; \
557 \
558 index = SHOW_UBITS(name, gb, bits); \
559 code = table[index].sym; \
560 n = table[index].len; \
561 \
562 if (max_depth > 1 && n < 0) { \
563 LAST_SKIP_BITS(name, gb, bits); \
564 UPDATE_CACHE(name, gb); \
565 \
566 nb_bits = -n; \
567 \
568 index = SHOW_UBITS(name, gb, nb_bits) + code; \
569 code = table[index].sym; \
570 n = table[index].len; \
571 if (max_depth > 2 && n < 0) { \
572 LAST_SKIP_BITS(name, gb, nb_bits); \
573 UPDATE_CACHE(name, gb); \
574 \
575 nb_bits = -n; \
576 \
577 index = SHOW_UBITS(name, gb, nb_bits) + code; \
578 code = table[index].sym; \
579 n = table[index].len; \
580 } \
581 } \
582 SKIP_BITS(name, gb, n); \
583 } while (0)
584
585 #define GET_RL_VLC(level, run, name, gb, table, bits, \
586 max_depth, need_update) \
587 do { \
588 int n, nb_bits; \
589 unsigned int index; \
590 \
591 index = SHOW_UBITS(name, gb, bits); \
592 level = table[index].level; \
593 n = table[index].len; \
594 \
595 if (max_depth > 1 && n < 0) { \
596 SKIP_BITS(name, gb, bits); \
597 if (need_update) { \
598 UPDATE_CACHE(name, gb); \
599 } \
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 if (max_depth > 2 && n < 0) { \
607 LAST_SKIP_BITS(name, gb, nb_bits); \
608 if (need_update) { \
609 UPDATE_CACHE(name, gb); \
610 } \
611 nb_bits = -n; \
612 \
613 index = SHOW_UBITS(name, gb, nb_bits) + level; \
614 level = table[index].level; \
615 n = table[index].len; \
616 } \
617 } \
618 run = table[index].run; \
619 SKIP_BITS(name, gb, n); \
620 } while (0)
621
622 /**
623 * Parse a vlc code.
624 * @param bits is the number of bits which will be read at once, must be
625 * identical to nb_bits in init_vlc()
626 * @param max_depth is the number of times bits bits must be read to completely
627 * read the longest vlc code
628 * = (max_vlc_length + bits - 1) / bits
629 * @returns the code parsed or -1 if no vlc matches
630 */
632 int bits,
int max_depth)
633 {
635
638
640
642
644 }
645
647 {
648 int n;
650 if (n == 0)
651 return 0;
652 else
654 }
655
657 {
659 return 0;
660 else
662 }
663
665 {
667 }
668
670 {
673
678 }
679
680 return 0;
681 }
682
683 #endif // CACHED_BITSTREAM_READER
684
685 #endif /* AVCODEC_GET_BITS_H */