38a7463cfc0d617e676cdb44df5261b2b443a4ae
[libav.git] / libavcodec / get_bits.h
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
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 "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 /* bit input */
53 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
54 typedef struct GetBitContext {
55 const uint8_t *buffer, *buffer_end;
56 int index;
57 int size_in_bits;
58 #if !UNCHECKED_BITSTREAM_READER
59 int size_in_bits_plus8;
60 #endif
61 } GetBitContext;
62
63 #define VLC_TYPE int16_t
64
65 typedef struct VLC {
66 int bits;
67 VLC_TYPE (*table)[2]; ///< code, bits
68 int table_size, table_allocated;
69 } VLC;
70
71 typedef struct RL_VLC_ELEM {
72 int16_t level;
73 int8_t len;
74 uint8_t run;
75 } RL_VLC_ELEM;
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, next bit is MSB of 32 or 64 bit (FIXME 64bit)
96
97 SHOW_UBITS(name, gb, num)
98 will return the next num bits
99
100 SHOW_SBITS(name, gb, num)
101 will return the next num bits and do sign extension
102
103 SKIP_BITS(name, gb, num)
104 will skip over the next num bits
105 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
106
107 SKIP_CACHE(name, gb, num)
108 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
109
110 SKIP_COUNTER(name, gb, num)
111 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
112
113 LAST_SKIP_CACHE(name, gb, num)
114 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
115
116 LAST_SKIP_BITS(name, gb, num)
117 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
118
119 for examples see get_bits, show_bits, skip_bits, get_vlc
120 */
121
122 #ifdef LONG_BITSTREAM_READER
123 # define MIN_CACHE_BITS 32
124 #else
125 # define MIN_CACHE_BITS 25
126 #endif
127
128 # define OPEN_READER(name, gb) \
129 unsigned int name##_index = (gb)->index; \
130 unsigned int av_unused name##_cache = 0
131
132 # define CLOSE_READER(name, gb) (gb)->index = name##_index
133
134 # ifdef ALT_BITSTREAM_READER_LE
135 # ifdef LONG_BITSTREAM_READER
136 # define UPDATE_CACHE(name, gb) \
137 name##_cache = AV_RL64((gb)->buffer+(name##_index>>3)) >> (name##_index&0x07)
138 # else
139 # define UPDATE_CACHE(name, gb) \
140 name##_cache = AV_RL32((gb)->buffer+(name##_index>>3)) >> (name##_index&0x07)
141 # endif
142
143 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
144 # else
145 # ifdef LONG_BITSTREAM_READER
146 # define UPDATE_CACHE(name, gb) \
147 name##_cache = AV_RB64((gb)->buffer+(name##_index >> 3)) >> (32 - (name##_index & 0x07))
148 # else
149 # define UPDATE_CACHE(name, gb) \
150 name##_cache = AV_RB32((gb)->buffer+(name##_index>>3)) << (name##_index&0x07)
151 # endif
152
153 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
154 # endif
155
156 // FIXME name?
157 #if UNCHECKED_BITSTREAM_READER
158 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
159 #else
160 # define SKIP_COUNTER(name, gb, num) \
161 name##_index = FFMIN((gb)->size_in_bits_plus8, name##_index + (num))
162 #endif
163
164 # define SKIP_BITS(name, gb, num) do { \
165 SKIP_CACHE(name, gb, num); \
166 SKIP_COUNTER(name, gb, num); \
167 } while (0)
168
169 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
170 # define LAST_SKIP_CACHE(name, gb, num)
171
172 # ifdef ALT_BITSTREAM_READER_LE
173 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
174
175 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
176 # else
177 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
178
179 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
180 # endif
181
182 # define GET_CACHE(name, gb) ((uint32_t)name##_cache)
183
184 static inline int get_bits_count(const GetBitContext *s){
185 return s->index;
186 }
187
188 static inline void skip_bits_long(GetBitContext *s, int n){
189 #if UNCHECKED_BITSTREAM_READER
190 s->index += n;
191 #else
192 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
193 #endif
194 }
195
196 /**
197 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
198 * if MSB not set it is negative
199 * @param n length in bits
200 * @author BERO
201 */
202 static inline int get_xbits(GetBitContext *s, int n){
203 register int sign;
204 register int32_t cache;
205 OPEN_READER(re, s);
206 UPDATE_CACHE(re, s);
207 cache = GET_CACHE(re, s);
208 sign = ~cache >> 31;
209 LAST_SKIP_BITS(re, s, n);
210 CLOSE_READER(re, s);
211 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
212 }
213
214 static inline int get_sbits(GetBitContext *s, int n){
215 register int tmp;
216 OPEN_READER(re, s);
217 UPDATE_CACHE(re, s);
218 tmp = SHOW_SBITS(re, s, n);
219 LAST_SKIP_BITS(re, s, n);
220 CLOSE_READER(re, s);
221 return tmp;
222 }
223
224 /**
225 * Read 1-25 bits.
226 */
227 static inline unsigned int get_bits(GetBitContext *s, int n){
228 register int tmp;
229 OPEN_READER(re, s);
230 UPDATE_CACHE(re, s);
231 tmp = SHOW_UBITS(re, s, n);
232 LAST_SKIP_BITS(re, s, n);
233 CLOSE_READER(re, s);
234 return tmp;
235 }
236
237 /**
238 * Show 1-25 bits.
239 */
240 static inline unsigned int show_bits(GetBitContext *s, int n){
241 register int tmp;
242 OPEN_READER(re, s);
243 UPDATE_CACHE(re, s);
244 tmp = SHOW_UBITS(re, s, n);
245 return tmp;
246 }
247
248 static inline void skip_bits(GetBitContext *s, int n){
249 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
250 OPEN_READER(re, s);
251 UPDATE_CACHE(re, s);
252 LAST_SKIP_BITS(re, s, n);
253 CLOSE_READER(re, s);
254 }
255
256 static inline unsigned int get_bits1(GetBitContext *s){
257 unsigned int index = s->index;
258 uint8_t result = s->buffer[index>>3];
259 #ifdef ALT_BITSTREAM_READER_LE
260 result >>= index & 7;
261 result &= 1;
262 #else
263 result <<= index & 7;
264 result >>= 8 - 1;
265 #endif
266 #if !UNCHECKED_BITSTREAM_READER
267 if (s->index < s->size_in_bits_plus8)
268 #endif
269 index++;
270 s->index = index;
271
272 return result;
273 }
274
275 static inline unsigned int show_bits1(GetBitContext *s){
276 return show_bits(s, 1);
277 }
278
279 static inline void skip_bits1(GetBitContext *s){
280 skip_bits(s, 1);
281 }
282
283 /**
284 * Read 0-32 bits.
285 */
286 static inline unsigned int get_bits_long(GetBitContext *s, int n){
287 if (n <= MIN_CACHE_BITS) return get_bits(s, n);
288 else {
289 #ifdef ALT_BITSTREAM_READER_LE
290 int ret = get_bits(s, 16);
291 return ret | (get_bits(s, n-16) << 16);
292 #else
293 int ret = get_bits(s, 16) << (n-16);
294 return ret | get_bits(s, n-16);
295 #endif
296 }
297 }
298
299 /**
300 * Read 0-32 bits as a signed integer.
301 */
302 static inline int get_sbits_long(GetBitContext *s, int n) {
303 return sign_extend(get_bits_long(s, n), n);
304 }
305
306 /**
307 * Show 0-32 bits.
308 */
309 static inline unsigned int show_bits_long(GetBitContext *s, int n){
310 if (n <= MIN_CACHE_BITS) return show_bits(s, n);
311 else {
312 GetBitContext gb = *s;
313 return get_bits_long(&gb, n);
314 }
315 }
316
317 static inline int check_marker(GetBitContext *s, const char *msg)
318 {
319 int bit = get_bits1(s);
320 if (!bit)
321 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
322
323 return bit;
324 }
325
326 /**
327 * Inititalize GetBitContext.
328 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
329 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
330 * @param bit_size the size of the buffer in bits
331 *
332 * While GetBitContext stores the buffer size, for performance reasons you are
333 * responsible for checking for the buffer end yourself (take advantage of the padding)!
334 */
335 static inline void init_get_bits(GetBitContext *s,
336 const uint8_t *buffer, int bit_size)
337 {
338 int buffer_size = (bit_size+7)>>3;
339 if (buffer_size < 0 || bit_size < 0) {
340 buffer_size = bit_size = 0;
341 buffer = NULL;
342 }
343
344 s->buffer = buffer;
345 s->size_in_bits = bit_size;
346 #if !UNCHECKED_BITSTREAM_READER
347 s->size_in_bits_plus8 = bit_size + 8;
348 #endif
349 s->buffer_end = buffer + buffer_size;
350 s->index = 0;
351 }
352
353 static inline void align_get_bits(GetBitContext *s)
354 {
355 int n = -get_bits_count(s) & 7;
356 if (n) skip_bits(s, n);
357 }
358
359 #define init_vlc(vlc, nb_bits, nb_codes, \
360 bits, bits_wrap, bits_size, \
361 codes, codes_wrap, codes_size, \
362 flags) \
363 init_vlc_sparse(vlc, nb_bits, nb_codes, \
364 bits, bits_wrap, bits_size, \
365 codes, codes_wrap, codes_size, \
366 NULL, 0, 0, flags)
367
368 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
369 const void *bits, int bits_wrap, int bits_size,
370 const void *codes, int codes_wrap, int codes_size,
371 const void *symbols, int symbols_wrap, int symbols_size,
372 int flags);
373 #define INIT_VLC_LE 2
374 #define INIT_VLC_USE_NEW_STATIC 4
375 void free_vlc(VLC *vlc);
376
377 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
378 static VLC_TYPE table[static_size][2]; \
379 (vlc)->table = table; \
380 (vlc)->table_allocated = static_size; \
381 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
382 } while (0)
383
384
385 /**
386 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
387 * If the vlc code is invalid and max_depth>1, then the number of bits removed
388 * is undefined.
389 */
390 #define GET_VLC(code, name, gb, table, bits, max_depth) do { \
391 int n, nb_bits; \
392 unsigned int index; \
393 \
394 index = SHOW_UBITS(name, gb, bits); \
395 code = table[index][0]; \
396 n = table[index][1]; \
397 \
398 if (max_depth > 1 && n < 0) { \
399 LAST_SKIP_BITS(name, gb, bits); \
400 UPDATE_CACHE(name, gb); \
401 \
402 nb_bits = -n; \
403 \
404 index = SHOW_UBITS(name, gb, nb_bits) + code; \
405 code = table[index][0]; \
406 n = table[index][1]; \
407 if (max_depth > 2 && n < 0) { \
408 LAST_SKIP_BITS(name, gb, nb_bits); \
409 UPDATE_CACHE(name, gb); \
410 \
411 nb_bits = -n; \
412 \
413 index = SHOW_UBITS(name, gb, nb_bits) + code; \
414 code = table[index][0]; \
415 n = table[index][1]; \
416 } \
417 } \
418 SKIP_BITS(name, gb, n); \
419 } while (0)
420
421 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) do { \
422 int n, nb_bits; \
423 unsigned int index; \
424 \
425 index = SHOW_UBITS(name, gb, bits); \
426 level = table[index].level; \
427 n = table[index].len; \
428 \
429 if (max_depth > 1 && n < 0) { \
430 SKIP_BITS(name, gb, bits); \
431 if (need_update) { \
432 UPDATE_CACHE(name, gb); \
433 } \
434 \
435 nb_bits = -n; \
436 \
437 index = SHOW_UBITS(name, gb, nb_bits) + level; \
438 level = table[index].level; \
439 n = table[index].len; \
440 } \
441 run = table[index].run; \
442 SKIP_BITS(name, gb, n); \
443 } while (0)
444
445
446 /**
447 * Parse a vlc code, faster than get_vlc().
448 * @param bits is the number of bits which will be read at once, must be
449 * identical to nb_bits in init_vlc()
450 * @param max_depth is the number of times bits bits must be read to completely
451 * read the longest vlc code
452 * = (max_vlc_length + bits - 1) / bits
453 */
454 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
455 int bits, int max_depth)
456 {
457 int code;
458
459 OPEN_READER(re, s);
460 UPDATE_CACHE(re, s);
461
462 GET_VLC(code, re, s, table, bits, max_depth);
463
464 CLOSE_READER(re, s);
465 return code;
466 }
467
468 static inline int decode012(GetBitContext *gb){
469 int n;
470 n = get_bits1(gb);
471 if (n == 0)
472 return 0;
473 else
474 return get_bits1(gb) + 1;
475 }
476
477 static inline int decode210(GetBitContext *gb){
478 if (get_bits1(gb))
479 return 0;
480 else
481 return 2 - get_bits1(gb);
482 }
483
484 static inline int get_bits_left(GetBitContext *gb)
485 {
486 return gb->size_in_bits - get_bits_count(gb);
487 }
488
489 //#define TRACE
490
491 #ifdef TRACE
492 static inline void print_bin(int bits, int n){
493 int i;
494
495 for (i = n-1; i >= 0; i--) {
496 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
497 }
498 for (i = n; i < 24; i++)
499 av_log(NULL, AV_LOG_DEBUG, " ");
500 }
501
502 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
503 const char *func, int line){
504 int r = get_bits(s, n);
505
506 print_bin(r, n);
507 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
508 r, n, r, get_bits_count(s)-n, file, func, line);
509 return r;
510 }
511 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
512 int bits, int max_depth, char *file,
513 const char *func, int line){
514 int show = show_bits(s, 24);
515 int pos = get_bits_count(s);
516 int r = get_vlc2(s, table, bits, max_depth);
517 int len = get_bits_count(s) - pos;
518 int bits2 = show >> (24-len);
519
520 print_bin(bits2, len);
521
522 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
523 bits2, len, r, pos, file, func, line);
524 return r;
525 }
526 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
527 const char *func, int line){
528 int show = show_bits(s, n);
529 int r = get_xbits(s, n);
530
531 print_bin(show, n);
532 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
533 show, n, r, get_bits_count(s)-n, file, func, line);
534 return r;
535 }
536
537 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
538 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
539 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
540 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
541 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
542
543 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
544
545 #else //TRACE
546 #define tprintf(p, ...) {}
547 #endif
548
549 #endif /* AVCODEC_GET_BITS_H */