Deobfuscate LE SHOW_[SU]BITS; these are simple sign/zero-extend
[libav.git] / libavcodec / get_bits.h
CommitLineData
04d7f601
DB
1/*
2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
b78e7197
DB
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
04d7f601
DB
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
b78e7197 9 * version 2.1 of the License, or (at your option) any later version.
04d7f601 10 *
b78e7197 11 * FFmpeg is distributed in the hope that it will be useful,
04d7f601
DB
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
b78e7197 17 * License along with FFmpeg; if not, write to the Free Software
04d7f601
DB
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
caa336b4 21/**
9106a698
SS
22 * @file libavcodec/get_bits.h
23 * bitstream reader API header.
caa336b4
MN
24 */
25
9106a698
SS
26#ifndef AVCODEC_GET_BITS_H
27#define AVCODEC_GET_BITS_H
caa336b4 28
99545457
MR
29#include <stdint.h>
30#include <stdlib.h>
31#include <assert.h>
245976da
DB
32#include "libavutil/bswap.h"
33#include "libavutil/common.h"
34#include "libavutil/intreadwrite.h"
35#include "libavutil/log.h"
017c0811 36#include "mathops.h"
8fbc6aae 37
75c998a2 38#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
1ae7a851 39# define ALT_BITSTREAM_READER
75c998a2
AJ
40#endif
41
727c236a 42#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
1a91f1a0 43# if ARCH_ARM && !HAVE_FAST_UNALIGNED
a1b65018
AJ
44# define A32_BITSTREAM_READER
45# else
1ae7a851 46# define ALT_BITSTREAM_READER
caa336b4
MN
47//#define LIBMPEG2_BITSTREAM_READER
48//#define A32_BITSTREAM_READER
a1b65018 49# endif
727c236a 50#endif
115329f1 51
caa336b4
MN
52/* bit input */
53/* buffer, buffer_end and size_in_bits must be present and used by every reader */
54typedef struct GetBitContext {
55 const uint8_t *buffer, *buffer_end;
56#ifdef ALT_BITSTREAM_READER
57 int index;
58#elif defined LIBMPEG2_BITSTREAM_READER
59 uint8_t *buffer_ptr;
60 uint32_t cache;
61 int bit_count;
62#elif defined A32_BITSTREAM_READER
63 uint32_t *buffer_ptr;
64 uint32_t cache0;
65 uint32_t cache1;
66 int bit_count;
67#endif
68 int size_in_bits;
69} GetBitContext;
70
71#define VLC_TYPE int16_t
72
73typedef struct VLC {
74 int bits;
75 VLC_TYPE (*table)[2]; ///< code, bits
76 int table_size, table_allocated;
77} VLC;
78
79typedef struct RL_VLC_ELEM {
80 int16_t level;
81 int8_t len;
82 uint8_t run;
83} RL_VLC_ELEM;
84
caa336b4
MN
85/* Bitstream reader API docs:
86name
7a7c4a7b 87 arbitrary name which is used as prefix for the internal variables
caa336b4
MN
88
89gb
90 getbitcontext
91
92OPEN_READER(name, gb)
93 loads gb into local variables
94
95CLOSE_READER(name, gb)
96 stores local vars in gb
97
98UPDATE_CACHE(name, gb)
99 refills the internal cache from the bitstream
100 after this call at least MIN_CACHE_BITS will be available,
101
102GET_CACHE(name, gb)
103 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
104
105SHOW_UBITS(name, gb, num)
bb628dae 106 will return the next num bits
caa336b4
MN
107
108SHOW_SBITS(name, gb, num)
bb628dae 109 will return the next num bits and do sign extension
caa336b4
MN
110
111SKIP_BITS(name, gb, num)
112 will skip over the next num bits
bb628dae 113 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
caa336b4
MN
114
115SKIP_CACHE(name, gb, num)
116 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
117
118SKIP_COUNTER(name, gb, num)
119 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
120
121LAST_SKIP_CACHE(name, gb, num)
122 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
123
124LAST_SKIP_BITS(name, gb, num)
6249c33e 125 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
caa336b4
MN
126
127for examples see get_bits, show_bits, skip_bits, get_vlc
128*/
129
caa336b4
MN
130#ifdef ALT_BITSTREAM_READER
131# define MIN_CACHE_BITS 25
132
133# define OPEN_READER(name, gb)\
d9ef0d2e 134 unsigned int name##_index= (gb)->index;\
caa336b4
MN
135 int name##_cache= 0;\
136
137# define CLOSE_READER(name, gb)\
138 (gb)->index= name##_index;\
139
cea27ac7
MN
140# ifdef ALT_BITSTREAM_READER_LE
141# define UPDATE_CACHE(name, gb)\
67c1b32f 142 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
cea27ac7
MN
143
144# define SKIP_CACHE(name, gb, num)\
145 name##_cache >>= (num);
146# else
caa336b4 147# define UPDATE_CACHE(name, gb)\
67c1b32f 148 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
caa336b4
MN
149
150# define SKIP_CACHE(name, gb, num)\
cea27ac7
MN
151 name##_cache <<= (num);
152# endif
caa336b4
MN
153
154// FIXME name?
155# define SKIP_COUNTER(name, gb, num)\
156 name##_index += (num);\
157
158# define SKIP_BITS(name, gb, num)\
159 {\
160 SKIP_CACHE(name, gb, num)\
161 SKIP_COUNTER(name, gb, num)\
162 }\
163
164# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
165# define LAST_SKIP_CACHE(name, gb, num) ;
166
cea27ac7
MN
167# ifdef ALT_BITSTREAM_READER_LE
168# define SHOW_UBITS(name, gb, num)\
7cd7d19e 169 zero_extend(name##_cache, num)
7da4d4cd
GM
170
171# define SHOW_SBITS(name, gb, num)\
7cd7d19e 172 sign_extend(name##_cache, num)
cea27ac7 173# else
caa336b4
MN
174# define SHOW_UBITS(name, gb, num)\
175 NEG_USR32(name##_cache, num)
176
177# define SHOW_SBITS(name, gb, num)\
178 NEG_SSR32(name##_cache, num)
7da4d4cd 179# endif
caa336b4
MN
180
181# define GET_CACHE(name, gb)\
182 ((uint32_t)name##_cache)
183
8880c8bc 184static inline int get_bits_count(const GetBitContext *s){
caa336b4
MN
185 return s->index;
186}
5a7bd283
MN
187
188static inline void skip_bits_long(GetBitContext *s, int n){
6e4703ca 189 s->index += n;
5a7bd283
MN
190}
191
caa336b4
MN
192#elif defined LIBMPEG2_BITSTREAM_READER
193//libmpeg2 like reader
194
195# define MIN_CACHE_BITS 17
196
197# define OPEN_READER(name, gb)\
198 int name##_bit_count=(gb)->bit_count;\
199 int name##_cache= (gb)->cache;\
200 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
201
202# define CLOSE_READER(name, gb)\
203 (gb)->bit_count= name##_bit_count;\
204 (gb)->cache= name##_cache;\
205 (gb)->buffer_ptr= name##_buffer_ptr;\
206
caa336b4
MN
207# define UPDATE_CACHE(name, gb)\
208 if(name##_bit_count >= 0){\
71c465a1 209 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
caa336b4
MN
210 name##_buffer_ptr+=2;\
211 name##_bit_count-= 16;\
212 }\
213
caa336b4
MN
214# define SKIP_CACHE(name, gb, num)\
215 name##_cache <<= (num);\
216
217# define SKIP_COUNTER(name, gb, num)\
218 name##_bit_count += (num);\
219
220# define SKIP_BITS(name, gb, num)\
221 {\
222 SKIP_CACHE(name, gb, num)\
223 SKIP_COUNTER(name, gb, num)\
224 }\
225
226# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
227# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
228
229# define SHOW_UBITS(name, gb, num)\
230 NEG_USR32(name##_cache, num)
231
232# define SHOW_SBITS(name, gb, num)\
233 NEG_SSR32(name##_cache, num)
234
235# define GET_CACHE(name, gb)\
236 ((uint32_t)name##_cache)
237
8880c8bc 238static inline int get_bits_count(const GetBitContext *s){
caa336b4
MN
239 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
240}
241
6e4703ca
MN
242static inline void skip_bits_long(GetBitContext *s, int n){
243 OPEN_READER(re, s)
244 re_bit_count += n;
245 re_buffer_ptr += 2*(re_bit_count>>4);
246 re_bit_count &= 15;
247 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
248 UPDATE_CACHE(re, s)
249 CLOSE_READER(re, s)
250}
251
caa336b4
MN
252#elif defined A32_BITSTREAM_READER
253
254# define MIN_CACHE_BITS 32
255
256# define OPEN_READER(name, gb)\
257 int name##_bit_count=(gb)->bit_count;\
258 uint32_t name##_cache0= (gb)->cache0;\
259 uint32_t name##_cache1= (gb)->cache1;\
260 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
261
262# define CLOSE_READER(name, gb)\
263 (gb)->bit_count= name##_bit_count;\
264 (gb)->cache0= name##_cache0;\
265 (gb)->cache1= name##_cache1;\
266 (gb)->buffer_ptr= name##_buffer_ptr;\
267
268# define UPDATE_CACHE(name, gb)\
269 if(name##_bit_count > 0){\
270 const uint32_t next= be2me_32( *name##_buffer_ptr );\
271 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
272 name##_cache1 |= next<<name##_bit_count;\
273 name##_buffer_ptr++;\
274 name##_bit_count-= 32;\
275 }\
276
b250f9c6 277#if ARCH_X86
caa336b4 278# define SKIP_CACHE(name, gb, num)\
be449fca 279 __asm__(\
bb270c08
DB
280 "shldl %2, %1, %0 \n\t"\
281 "shll %2, %1 \n\t"\
caa336b4 282 : "+r" (name##_cache0), "+r" (name##_cache1)\
6ff3b2b8 283 : "Ic" ((uint8_t)(num))\
caa336b4
MN
284 );
285#else
286# define SKIP_CACHE(name, gb, num)\
287 name##_cache0 <<= (num);\
288 name##_cache0 |= NEG_USR32(name##_cache1,num);\
289 name##_cache1 <<= (num);
290#endif
291
292# define SKIP_COUNTER(name, gb, num)\
293 name##_bit_count += (num);\
294
295# define SKIP_BITS(name, gb, num)\
296 {\
297 SKIP_CACHE(name, gb, num)\
298 SKIP_COUNTER(name, gb, num)\
299 }\
300
301# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
302# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
303
304# define SHOW_UBITS(name, gb, num)\
305 NEG_USR32(name##_cache0, num)
306
307# define SHOW_SBITS(name, gb, num)\
308 NEG_SSR32(name##_cache0, num)
309
310# define GET_CACHE(name, gb)\
311 (name##_cache0)
312
8880c8bc 313static inline int get_bits_count(const GetBitContext *s){
caa336b4
MN
314 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
315}
316
5a7bd283
MN
317static inline void skip_bits_long(GetBitContext *s, int n){
318 OPEN_READER(re, s)
319 re_bit_count += n;
3dddf21f 320 re_buffer_ptr += re_bit_count>>5;
5a7bd283 321 re_bit_count &= 31;
1491e21c
MN
322 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
323 re_cache1 = 0;
5a7bd283 324 UPDATE_CACHE(re, s)
5a7bd283
MN
325 CLOSE_READER(re, s)
326}
327
caa336b4
MN
328#endif
329
330/**
331 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
115329f1 332 * if MSB not set it is negative
caa336b4 333 * @param n length in bits
115329f1 334 * @author BERO
caa336b4
MN
335 */
336static inline int get_xbits(GetBitContext *s, int n){
6b250e47 337 register int sign;
caa336b4
MN
338 register int32_t cache;
339 OPEN_READER(re, s)
340 UPDATE_CACHE(re, s)
341 cache = GET_CACHE(re,s);
6b250e47 342 sign=(~cache)>>31;
caa336b4
MN
343 LAST_SKIP_BITS(re, s, n)
344 CLOSE_READER(re, s)
6b250e47 345 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
caa336b4
MN
346}
347
348static inline int get_sbits(GetBitContext *s, int n){
349 register int tmp;
350 OPEN_READER(re, s)
351 UPDATE_CACHE(re, s)
352 tmp= SHOW_SBITS(re, s, n);
353 LAST_SKIP_BITS(re, s, n)
354 CLOSE_READER(re, s)
355 return tmp;
356}
357
358/**
0ac47d63 359 * reads 1-17 bits.
bb628dae 360 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
caa336b4
MN
361 */
362static inline unsigned int get_bits(GetBitContext *s, int n){
363 register int tmp;
364 OPEN_READER(re, s)
365 UPDATE_CACHE(re, s)
366 tmp= SHOW_UBITS(re, s, n);
367 LAST_SKIP_BITS(re, s, n)
368 CLOSE_READER(re, s)
369 return tmp;
370}
371
caa336b4 372/**
0ac47d63 373 * shows 1-17 bits.
bb628dae 374 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
caa336b4
MN
375 */
376static inline unsigned int show_bits(GetBitContext *s, int n){
377 register int tmp;
378 OPEN_READER(re, s)
379 UPDATE_CACHE(re, s)
380 tmp= SHOW_UBITS(re, s, n);
381// CLOSE_READER(re, s)
382 return tmp;
383}
384
caa336b4
MN
385static inline void skip_bits(GetBitContext *s, int n){
386 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
387 OPEN_READER(re, s)
388 UPDATE_CACHE(re, s)
389 LAST_SKIP_BITS(re, s, n)
390 CLOSE_READER(re, s)
391}
392
393static inline unsigned int get_bits1(GetBitContext *s){
394#ifdef ALT_BITSTREAM_READER
d9ef0d2e 395 unsigned int index= s->index;
caa336b4 396 uint8_t result= s->buffer[ index>>3 ];
cea27ac7
MN
397#ifdef ALT_BITSTREAM_READER_LE
398 result>>= (index&0x07);
399 result&= 1;
400#else
caa336b4
MN
401 result<<= (index&0x07);
402 result>>= 8 - 1;
cea27ac7 403#endif
caa336b4
MN
404 index++;
405 s->index= index;
406
407 return result;
408#else
409 return get_bits(s, 1);
410#endif
411}
412
413static inline unsigned int show_bits1(GetBitContext *s){
414 return show_bits(s, 1);
415}
416
417static inline void skip_bits1(GetBitContext *s){
418 skip_bits(s, 1);
419}
420
421/**
8fbc6aae
AJ
422 * reads 0-32 bits.
423 */
424static inline unsigned int get_bits_long(GetBitContext *s, int n){
425 if(n<=17) return get_bits(s, n);
426 else{
d1121caa
RD
427#ifdef ALT_BITSTREAM_READER_LE
428 int ret= get_bits(s, 16);
429 return ret | (get_bits(s, n-16) << 16);
430#else
8fbc6aae
AJ
431 int ret= get_bits(s, 16) << (n-16);
432 return ret | get_bits(s, n-16);
d1121caa 433#endif
8fbc6aae
AJ
434 }
435}
436
437/**
017c0811
JR
438 * reads 0-32 bits as a signed integer.
439 */
440static inline int get_sbits_long(GetBitContext *s, int n) {
441 return sign_extend(get_bits_long(s, n), n);
442}
443
444/**
8fbc6aae
AJ
445 * shows 0-32 bits.
446 */
447static inline unsigned int show_bits_long(GetBitContext *s, int n){
448 if(n<=17) return show_bits(s, n);
449 else{
450 GetBitContext gb= *s;
edd532db 451 return get_bits_long(&gb, n);
8fbc6aae
AJ
452 }
453}
454
455static inline int check_marker(GetBitContext *s, const char *msg)
456{
457 int bit= get_bits1(s);
458 if(!bit)
459 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
460
461 return bit;
462}
463
464/**
caa336b4
MN
465 * init GetBitContext.
466 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
467 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
468 * @param bit_size the size of the buffer in bits
17f0c3be
RD
469 *
470 * While GetBitContext stores the buffer size, for performance reasons you are
471 * responsible for checking for the buffer end yourself (take advantage of the padding)!
caa336b4
MN
472 */
473static inline void init_get_bits(GetBitContext *s,
474 const uint8_t *buffer, int bit_size)
475{
9061be9f 476 int buffer_size= (bit_size+7)>>3;
288f1e68
MB
477 if(buffer_size < 0 || bit_size < 0) {
478 buffer_size = bit_size = 0;
479 buffer = NULL;
480 }
caa336b4
MN
481
482 s->buffer= buffer;
483 s->size_in_bits= bit_size;
484 s->buffer_end= buffer + buffer_size;
485#ifdef ALT_BITSTREAM_READER
486 s->index=0;
487#elif defined LIBMPEG2_BITSTREAM_READER
6e4703ca
MN
488 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
489 s->bit_count = 16 + 8*((intptr_t)buffer&1);
490 skip_bits_long(s, 0);
a42ec9f4
MN
491#elif defined A32_BITSTREAM_READER
492 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
493 s->bit_count = 32 + 8*((intptr_t)buffer&3);
494 skip_bits_long(s, 0);
caa336b4
MN
495#endif
496}
497
ae5e7e7f 498static inline void align_get_bits(GetBitContext *s)
5a7bd283
MN
499{
500 int n= (-get_bits_count(s)) & 7;
501 if(n) skip_bits(s, n);
502}
503
b613bacc
LM
504#define init_vlc(vlc, nb_bits, nb_codes,\
505 bits, bits_wrap, bits_size,\
506 codes, codes_wrap, codes_size,\
507 flags)\
508 init_vlc_sparse(vlc, nb_bits, nb_codes,\
509 bits, bits_wrap, bits_size,\
510 codes, codes_wrap, codes_size,\
511 NULL, 0, 0, flags)
512
513int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
caa336b4
MN
514 const void *bits, int bits_wrap, int bits_size,
515 const void *codes, int codes_wrap, int codes_size,
b613bacc 516 const void *symbols, int symbols_wrap, int symbols_size,
cea27ac7 517 int flags);
cea27ac7 518#define INIT_VLC_LE 2
ccc54864 519#define INIT_VLC_USE_NEW_STATIC 4
caa336b4
MN
520void free_vlc(VLC *vlc);
521
d05b24ff
MN
522#define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
523{\
524 static VLC_TYPE table[static_size][2];\
525 (vlc)->table= table;\
526 (vlc)->table_allocated= static_size;\
527 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
528}
529
530
caa336b4
MN
531/**
532 *
fd10543e
AC
533 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
534 * If the vlc code is invalid and max_depth>1, then the number of bits removed
535 * is undefined.
caa336b4
MN
536 */
537#define GET_VLC(code, name, gb, table, bits, max_depth)\
538{\
d9ef0d2e
MN
539 int n, nb_bits;\
540 unsigned int index;\
caa336b4
MN
541\
542 index= SHOW_UBITS(name, gb, bits);\
543 code = table[index][0];\
544 n = table[index][1];\
545\
546 if(max_depth > 1 && n < 0){\
547 LAST_SKIP_BITS(name, gb, bits)\
548 UPDATE_CACHE(name, gb)\
549\
550 nb_bits = -n;\
551\
552 index= SHOW_UBITS(name, gb, nb_bits) + code;\
553 code = table[index][0];\
554 n = table[index][1];\
555 if(max_depth > 2 && n < 0){\
556 LAST_SKIP_BITS(name, gb, nb_bits)\
557 UPDATE_CACHE(name, gb)\
558\
559 nb_bits = -n;\
560\
561 index= SHOW_UBITS(name, gb, nb_bits) + code;\
562 code = table[index][0];\
563 n = table[index][1];\
564 }\
565 }\
566 SKIP_BITS(name, gb, n)\
567}
568
e91f4bf1 569#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
caa336b4 570{\
d9ef0d2e
MN
571 int n, nb_bits;\
572 unsigned int index;\
caa336b4
MN
573\
574 index= SHOW_UBITS(name, gb, bits);\
575 level = table[index].level;\
576 n = table[index].len;\
577\
578 if(max_depth > 1 && n < 0){\
e91f4bf1
MN
579 SKIP_BITS(name, gb, bits)\
580 if(need_update){\
581 UPDATE_CACHE(name, gb)\
582 }\
caa336b4
MN
583\
584 nb_bits = -n;\
585\
586 index= SHOW_UBITS(name, gb, nb_bits) + level;\
587 level = table[index].level;\
588 n = table[index].len;\
589 }\
590 run= table[index].run;\
591 SKIP_BITS(name, gb, n)\
592}
593
caa336b4
MN
594
595/**
596 * parses a vlc code, faster then get_vlc()
115329f1 597 * @param bits is the number of bits which will be read at once, must be
caa336b4 598 * identical to nb_bits in init_vlc()
e42dba48 599 * @param max_depth is the number of times bits bits must be read to completely
115329f1 600 * read the longest vlc code
caa336b4
MN
601 * = (max_vlc_length + bits - 1) / bits
602 */
849f1035 603static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
caa336b4
MN
604 int bits, int max_depth)
605{
606 int code;
115329f1 607
caa336b4
MN
608 OPEN_READER(re, s)
609 UPDATE_CACHE(re, s)
610
611 GET_VLC(code, re, s, table, bits, max_depth)
612
613 CLOSE_READER(re, s)
614 return code;
615}
616
617//#define TRACE
618
619#ifdef TRACE
caa336b4
MN
620static inline void print_bin(int bits, int n){
621 int i;
115329f1 622
caa336b4 623 for(i=n-1; i>=0; i--){
61f040dd 624 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
caa336b4
MN
625 }
626 for(i=n; i<24; i++)
61f040dd 627 av_log(NULL, AV_LOG_DEBUG, " ");
caa336b4
MN
628}
629
61f040dd 630static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
caa336b4 631 int r= get_bits(s, n);
115329f1 632
caa336b4 633 print_bin(r, n);
61f040dd 634 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
caa336b4
MN
635 return r;
636}
61f040dd 637static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
caa336b4
MN
638 int show= show_bits(s, 24);
639 int pos= get_bits_count(s);
640 int r= get_vlc2(s, table, bits, max_depth);
641 int len= get_bits_count(s) - pos;
642 int bits2= show>>(24-len);
115329f1 643
caa336b4 644 print_bin(bits2, len);
115329f1 645
61f040dd 646 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
caa336b4
MN
647 return r;
648}
61f040dd 649static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
caa336b4
MN
650 int show= show_bits(s, n);
651 int r= get_xbits(s, n);
115329f1 652
caa336b4 653 print_bin(show, n);
61f040dd 654 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
caa336b4
MN
655 return r;
656}
657
658#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
659#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
660#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
661#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
662#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
663
a9c9a240 664#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
caa336b4
MN
665
666#else //TRACE
a9c9a240 667#define tprintf(p, ...) {}
caa336b4
MN
668#endif
669
7d8b13b4 670static inline int decode012(GetBitContext *gb){
a64a006e
MN
671 int n;
672 n = get_bits1(gb);
673 if (n == 0)
674 return 0;
675 else
676 return get_bits1(gb) + 1;
677}
678
b44665c4
KS
679static inline int decode210(GetBitContext *gb){
680 if (get_bits1(gb))
681 return 0;
682 else
683 return 2 - get_bits1(gb);
684}
685
c47ca25e
RB
686static inline int get_bits_left(GetBitContext *gb)
687{
688 return gb->size_in_bits - get_bits_count(gb);
689}
690
9106a698 691#endif /* AVCODEC_GET_BITS_H */