summaryrefslogtreecommitdiff
path: root/apps/codecs/lib/ffmpeg_get_bits.h
diff options
context:
space:
mode:
authorNils Wallménius <nils@rockbox.org>2010-07-15 16:19:17 +0000
committerNils Wallménius <nils@rockbox.org>2010-07-15 16:19:17 +0000
commita87c61854ef614b258ca7d4d0b40db017884e63e (patch)
tree4f0129350a8a2d25ee5e5d218aa787ae2dbbeca3 /apps/codecs/lib/ffmpeg_get_bits.h
parent328f2f9c285dd9ccec4ddabe4d64a508b0e498fa (diff)
downloadrockbox-a87c61854ef614b258ca7d4d0b40db017884e63e.tar.gz
rockbox-a87c61854ef614b258ca7d4d0b40db017884e63e.zip
Sync codeclib bitstream code with upstream ffmpeg code. Build ffmpeg_bitstream.c as a part of the codec lib. Use this codeclib implementation in libffmpegFLAC. Implement adapted version of the unaligned longword reading optimization for coldfire from the libwma version of this code. Speeds up cook decoding by 2-3% on h300 and flac by 25% on h300, also speeds up flac decoding by 2% on c200 (decoding speed of cook on c200 is unchanged).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27430 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/lib/ffmpeg_get_bits.h')
-rw-r--r--apps/codecs/lib/ffmpeg_get_bits.h750
1 files changed, 750 insertions, 0 deletions
diff --git a/apps/codecs/lib/ffmpeg_get_bits.h b/apps/codecs/lib/ffmpeg_get_bits.h
new file mode 100644
index 0000000000..1a461e9e25
--- /dev/null
+++ b/apps/codecs/lib/ffmpeg_get_bits.h
@@ -0,0 +1,750 @@
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#include <stdlib.h>
31//#include <assert.h>
32//#include "libavutil/bswap.h"
33//#include "libavutil/common.h"
34//#include "libavutil/intreadwrite.h"
35//#include "libavutil/log.h"
36//#include "mathops.h"
37
38#include "codecs.h"
39
40/* rockbox' optimised inline functions */
41#define bswap_16(x) swap16(x)
42#define bswap_32(x) swap32(x)
43
44#ifdef ROCKBOX_BIG_ENDIAN
45#define be2me_16(x) (x)
46#define be2me_32(x) (x)
47#define le2me_16(x) bswap_16(x)
48#define le2me_32(x) bswap_32(x)
49#else
50#define be2me_16(x) bswap_16(x)
51#define be2me_32(x) bswap_32(x)
52#define le2me_16(x) (x)
53#define le2me_32(x) (x)
54#endif
55
56#define av_const __attribute__((const))
57#define av_always_inline inline __attribute__((always_inline))
58
59/* Coldfire cpu's support unaligned long reads */
60#ifdef CPU_COLDFIRE
61#define AV_RB32(x) (*(const uint32_t*)(x))
62#else
63/* The following define is taken from libavutil/intreadwrite.h */
64#define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
65 (((const uint8_t*)(x))[1] << 16) | \
66 (((const uint8_t*)(x))[2] << 8) | \
67 ((const uint8_t*)(x))[3])
68#endif
69/* The following is taken from mathops.h */
70
71#ifndef sign_extend
72static inline av_const int sign_extend(int val, unsigned bits)
73{
74 return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
75}
76#endif
77
78#ifndef NEG_SSR32
79# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
80#endif
81
82#ifndef NEG_USR32
83# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
84#endif
85
86/* these 2 are from libavutil/common.h */
87
88#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
89#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
90
91#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
92# define ALT_BITSTREAM_READER
93#endif
94
95/*
96#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
97# if ARCH_ARM && !HAVE_FAST_UNALIGNED
98# define A32_BITSTREAM_READER
99# else
100*/
101# define ALT_BITSTREAM_READER
102/*
103//#define LIBMPEG2_BITSTREAM_READER
104//#define A32_BITSTREAM_READER
105# endif
106#endif
107*/
108
109/* bit input */
110/* buffer, buffer_end and size_in_bits must be present and used by every reader */
111typedef struct GetBitContext {
112 const uint8_t *buffer, *buffer_end;
113#ifdef ALT_BITSTREAM_READER
114 int index;
115#elif defined LIBMPEG2_BITSTREAM_READER
116 uint8_t *buffer_ptr;
117 uint32_t cache;
118 int bit_count;
119#elif defined A32_BITSTREAM_READER
120 uint32_t *buffer_ptr;
121 uint32_t cache0;
122 uint32_t cache1;
123 int bit_count;
124#endif
125 int size_in_bits;
126} GetBitContext;
127
128#define VLC_TYPE int16_t
129
130typedef struct VLC {
131 int bits;
132 VLC_TYPE (*table)[2]; ///< code, bits
133 int table_size, table_allocated;
134} VLC;
135
136typedef struct RL_VLC_ELEM {
137 int16_t level;
138 int8_t len;
139 uint8_t run;
140} RL_VLC_ELEM;
141
142/* Bitstream reader API docs:
143name
144 arbitrary name which is used as prefix for the internal variables
145
146gb
147 getbitcontext
148
149OPEN_READER(name, gb)
150 loads gb into local variables
151
152CLOSE_READER(name, gb)
153 stores local vars in gb
154
155UPDATE_CACHE(name, gb)
156 refills the internal cache from the bitstream
157 after this call at least MIN_CACHE_BITS will be available,
158
159GET_CACHE(name, gb)
160 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
161
162SHOW_UBITS(name, gb, num)
163 will return the next num bits
164
165SHOW_SBITS(name, gb, num)
166 will return the next num bits and do sign extension
167
168SKIP_BITS(name, gb, num)
169 will skip over the next num bits
170 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
171
172SKIP_CACHE(name, gb, num)
173 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
174
175SKIP_COUNTER(name, gb, num)
176 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
177
178LAST_SKIP_CACHE(name, gb, num)
179 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
180
181LAST_SKIP_BITS(name, gb, num)
182 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
183
184for examples see get_bits, show_bits, skip_bits, get_vlc
185*/
186
187#ifdef ALT_BITSTREAM_READER
188# define MIN_CACHE_BITS 25
189
190# define OPEN_READER(name, gb)\
191 unsigned int name##_index= (gb)->index;\
192 int name##_cache= 0;\
193
194# define CLOSE_READER(name, gb)\
195 (gb)->index= name##_index;\
196
197# ifdef ALT_BITSTREAM_READER_LE
198# define UPDATE_CACHE(name, gb)\
199 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
200
201# define SKIP_CACHE(name, gb, num)\
202 name##_cache >>= (num);
203# else
204# define UPDATE_CACHE(name, gb)\
205 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
206
207# define SKIP_CACHE(name, gb, num)\
208 name##_cache <<= (num);
209# endif
210
211// FIXME name?
212# define SKIP_COUNTER(name, gb, num)\
213 name##_index += (num);\
214
215# define SKIP_BITS(name, gb, num)\
216 {\
217 SKIP_CACHE(name, gb, num)\
218 SKIP_COUNTER(name, gb, num)\
219 }\
220
221# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
222# define LAST_SKIP_CACHE(name, gb, num) ;
223
224# ifdef ALT_BITSTREAM_READER_LE
225# define SHOW_UBITS(name, gb, num)\
226 zero_extend(name##_cache, num)
227
228# define SHOW_SBITS(name, gb, num)\
229 sign_extend(name##_cache, num)
230# else
231# define SHOW_UBITS(name, gb, num)\
232 NEG_USR32(name##_cache, num)
233
234# define SHOW_SBITS(name, gb, num)\
235 NEG_SSR32(name##_cache, num)
236# endif
237
238# define GET_CACHE(name, gb)\
239 ((uint32_t)name##_cache)
240
241static inline int get_bits_count(const GetBitContext *s){
242 return s->index;
243}
244
245static inline void skip_bits_long(GetBitContext *s, int n){
246 s->index += n;
247}
248
249#elif defined LIBMPEG2_BITSTREAM_READER
250//libmpeg2 like reader
251
252# define MIN_CACHE_BITS 17
253
254# define OPEN_READER(name, gb)\
255 int name##_bit_count=(gb)->bit_count;\
256 int name##_cache= (gb)->cache;\
257 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
258
259# define CLOSE_READER(name, gb)\
260 (gb)->bit_count= name##_bit_count;\
261 (gb)->cache= name##_cache;\
262 (gb)->buffer_ptr= name##_buffer_ptr;\
263
264# define UPDATE_CACHE(name, gb)\
265 if(name##_bit_count >= 0){\
266 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
267 name##_buffer_ptr+=2;\
268 name##_bit_count-= 16;\
269 }\
270
271# define SKIP_CACHE(name, gb, num)\
272 name##_cache <<= (num);\
273
274# define SKIP_COUNTER(name, gb, num)\
275 name##_bit_count += (num);\
276
277# define SKIP_BITS(name, gb, num)\
278 {\
279 SKIP_CACHE(name, gb, num)\
280 SKIP_COUNTER(name, gb, num)\
281 }\
282
283# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
284# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
285
286# define SHOW_UBITS(name, gb, num)\
287 NEG_USR32(name##_cache, num)
288
289# define SHOW_SBITS(name, gb, num)\
290 NEG_SSR32(name##_cache, num)
291
292# define GET_CACHE(name, gb)\
293 ((uint32_t)name##_cache)
294
295static inline int get_bits_count(const GetBitContext *s){
296 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
297}
298
299static inline void skip_bits_long(GetBitContext *s, int n){
300 OPEN_READER(re, s)
301 re_bit_count += n;
302 re_buffer_ptr += 2*(re_bit_count>>4);
303 re_bit_count &= 15;
304 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
305 UPDATE_CACHE(re, s)
306 CLOSE_READER(re, s)
307}
308
309#elif defined A32_BITSTREAM_READER
310
311# define MIN_CACHE_BITS 32
312
313# define OPEN_READER(name, gb)\
314 int name##_bit_count=(gb)->bit_count;\
315 uint32_t name##_cache0= (gb)->cache0;\
316 uint32_t name##_cache1= (gb)->cache1;\
317 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
318
319# define CLOSE_READER(name, gb)\
320 (gb)->bit_count= name##_bit_count;\
321 (gb)->cache0= name##_cache0;\
322 (gb)->cache1= name##_cache1;\
323 (gb)->buffer_ptr= name##_buffer_ptr;\
324
325# define UPDATE_CACHE(name, gb)\
326 if(name##_bit_count > 0){\
327 const uint32_t next= av_be2ne32( *name##_buffer_ptr );\
328 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
329 name##_cache1 |= next<<name##_bit_count;\
330 name##_buffer_ptr++;\
331 name##_bit_count-= 32;\
332 }\
333
334#if ARCH_X86
335# define SKIP_CACHE(name, gb, num)\
336 __asm__(\
337 "shldl %2, %1, %0 \n\t"\
338 "shll %2, %1 \n\t"\
339 : "+r" (name##_cache0), "+r" (name##_cache1)\
340 : "Ic" ((uint8_t)(num))\
341 );
342#else
343# define SKIP_CACHE(name, gb, num)\
344 name##_cache0 <<= (num);\
345 name##_cache0 |= NEG_USR32(name##_cache1,num);\
346 name##_cache1 <<= (num);
347#endif
348
349# define SKIP_COUNTER(name, gb, num)\
350 name##_bit_count += (num);\
351
352# define SKIP_BITS(name, gb, num)\
353 {\
354 SKIP_CACHE(name, gb, num)\
355 SKIP_COUNTER(name, gb, num)\
356 }\
357
358# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
359# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
360
361# define SHOW_UBITS(name, gb, num)\
362 NEG_USR32(name##_cache0, num)
363
364# define SHOW_SBITS(name, gb, num)\
365 NEG_SSR32(name##_cache0, num)
366
367# define GET_CACHE(name, gb)\
368 (name##_cache0)
369
370static inline int get_bits_count(const GetBitContext *s){
371 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
372}
373
374static inline void skip_bits_long(GetBitContext *s, int n){
375 OPEN_READER(re, s)
376 re_bit_count += n;
377 re_buffer_ptr += re_bit_count>>5;
378 re_bit_count &= 31;
379 re_cache0 = av_be2ne32( re_buffer_ptr[-1] ) << re_bit_count;
380 re_cache1 = 0;
381 UPDATE_CACHE(re, s)
382 CLOSE_READER(re, s)
383}
384
385#endif
386
387/**
388 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
389 * if MSB not set it is negative
390 * @param n length in bits
391 * @author BERO
392 */
393static inline int get_xbits(GetBitContext *s, int n){
394 register int sign;
395 register int32_t cache;
396 OPEN_READER(re, s)
397 UPDATE_CACHE(re, s)
398 cache = GET_CACHE(re,s);
399 sign=(~cache)>>31;
400 LAST_SKIP_BITS(re, s, n)
401 CLOSE_READER(re, s)
402 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
403}
404
405static inline int get_sbits(GetBitContext *s, int n){
406 register int tmp;
407 OPEN_READER(re, s)
408 UPDATE_CACHE(re, s)
409 tmp= SHOW_SBITS(re, s, n);
410 LAST_SKIP_BITS(re, s, n)
411 CLOSE_READER(re, s)
412 return tmp;
413}
414
415/**
416 * reads 1-17 bits.
417 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
418 */
419static inline unsigned int get_bits(GetBitContext *s, int n){
420 register int tmp;
421 OPEN_READER(re, s)
422 UPDATE_CACHE(re, s)
423 tmp= SHOW_UBITS(re, s, n);
424 LAST_SKIP_BITS(re, s, n)
425 CLOSE_READER(re, s)
426 return tmp;
427}
428
429/**
430 * shows 1-17 bits.
431 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
432 */
433static inline unsigned int show_bits(GetBitContext *s, int n){
434 register int tmp;
435 OPEN_READER(re, s)
436 UPDATE_CACHE(re, s)
437 tmp= SHOW_UBITS(re, s, n);
438// CLOSE_READER(re, s)
439 return tmp;
440}
441
442static inline void skip_bits(GetBitContext *s, int n){
443 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
444 OPEN_READER(re, s)
445 UPDATE_CACHE(re, s)
446 LAST_SKIP_BITS(re, s, n)
447 CLOSE_READER(re, s)
448}
449
450static inline unsigned int get_bits1(GetBitContext *s){
451#ifdef ALT_BITSTREAM_READER
452 unsigned int index= s->index;
453 uint8_t result= s->buffer[ index>>3 ];
454#ifdef ALT_BITSTREAM_READER_LE
455 result>>= (index&0x07);
456 result&= 1;
457#else
458 result<<= (index&0x07);
459 result>>= 8 - 1;
460#endif
461 index++;
462 s->index= index;
463
464 return result;
465#else
466 return get_bits(s, 1);
467#endif
468}
469
470static inline unsigned int show_bits1(GetBitContext *s){
471 return show_bits(s, 1);
472}
473
474static inline void skip_bits1(GetBitContext *s){
475 skip_bits(s, 1);
476}
477
478/**
479 * reads 0-32 bits.
480 */
481static inline unsigned int get_bits_long(GetBitContext *s, int n){
482 if(n<=MIN_CACHE_BITS) return get_bits(s, n);
483 else{
484#ifdef ALT_BITSTREAM_READER_LE
485 int ret= get_bits(s, 16);
486 return ret | (get_bits(s, n-16) << 16);
487#else
488 int ret= get_bits(s, 16) << (n-16);
489 return ret | get_bits(s, n-16);
490#endif
491 }
492}
493
494/**
495 * reads 0-32 bits as a signed integer.
496 */
497static inline int get_sbits_long(GetBitContext *s, int n) {
498 return sign_extend(get_bits_long(s, n), n);
499}
500
501/**
502 * shows 0-32 bits.
503 */
504static inline unsigned int show_bits_long(GetBitContext *s, int n){
505 if(n<=MIN_CACHE_BITS) return show_bits(s, n);
506 else{
507 GetBitContext gb= *s;
508 return get_bits_long(&gb, n);
509 }
510}
511
512/* not used
513static inline int check_marker(GetBitContext *s, const char *msg)
514{
515 int bit= get_bits1(s);
516 if(!bit)
517 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
518
519 return bit;
520}
521*/
522
523/**
524 * init GetBitContext.
525 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
526 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
527 * @param bit_size the size of the buffer in bits
528 *
529 * While GetBitContext stores the buffer size, for performance reasons you are
530 * responsible for checking for the buffer end yourself (take advantage of the padding)!
531 */
532static inline void init_get_bits(GetBitContext *s,
533 const uint8_t *buffer, int bit_size)
534{
535 int buffer_size= (bit_size+7)>>3;
536 if(buffer_size < 0 || bit_size < 0) {
537 buffer_size = bit_size = 0;
538 buffer = NULL;
539 }
540
541 s->buffer= buffer;
542 s->size_in_bits= bit_size;
543 s->buffer_end= buffer + buffer_size;
544#ifdef ALT_BITSTREAM_READER
545 s->index=0;
546#elif defined LIBMPEG2_BITSTREAM_READER
547 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
548 s->bit_count = 16 + 8*((intptr_t)buffer&1);
549 skip_bits_long(s, 0);
550#elif defined A32_BITSTREAM_READER
551 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
552 s->bit_count = 32 + 8*((intptr_t)buffer&3);
553 skip_bits_long(s, 0);
554#endif
555}
556
557static inline void align_get_bits(GetBitContext *s)
558{
559 int n= (-get_bits_count(s)) & 7;
560 if(n) skip_bits(s, n);
561}
562
563#define init_vlc(vlc, nb_bits, nb_codes,\
564 bits, bits_wrap, bits_size,\
565 codes, codes_wrap, codes_size,\
566 flags)\
567 init_vlc_sparse(vlc, nb_bits, nb_codes,\
568 bits, bits_wrap, bits_size,\
569 codes, codes_wrap, codes_size,\
570 NULL, 0, 0, flags)
571
572int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
573 const void *bits, int bits_wrap, int bits_size,
574 const void *codes, int codes_wrap, int codes_size,
575 const void *symbols, int symbols_wrap, int symbols_size,
576 int flags);
577#define INIT_VLC_LE 2
578#define INIT_VLC_USE_NEW_STATIC 4
579void free_vlc(VLC *vlc);
580
581#define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
582{\
583 static VLC_TYPE table[static_size][2];\
584 (vlc)->table= table;\
585 (vlc)->table_allocated= static_size;\
586 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
587}
588
589
590/**
591 *
592 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
593 * If the vlc code is invalid and max_depth>1, then the number of bits removed
594 * is undefined.
595 */
596#define GET_VLC(code, name, gb, table, bits, max_depth)\
597{\
598 int n, nb_bits;\
599 unsigned int index;\
600\
601 index= SHOW_UBITS(name, gb, bits);\
602 code = table[index][0];\
603 n = table[index][1];\
604\
605 if(max_depth > 1 && n < 0){\
606 LAST_SKIP_BITS(name, gb, bits)\
607 UPDATE_CACHE(name, gb)\
608\
609 nb_bits = -n;\
610\
611 index= SHOW_UBITS(name, gb, nb_bits) + code;\
612 code = table[index][0];\
613 n = table[index][1];\
614 if(max_depth > 2 && n < 0){\
615 LAST_SKIP_BITS(name, gb, nb_bits)\
616 UPDATE_CACHE(name, gb)\
617\
618 nb_bits = -n;\
619\
620 index= SHOW_UBITS(name, gb, nb_bits) + code;\
621 code = table[index][0];\
622 n = table[index][1];\
623 }\
624 }\
625 SKIP_BITS(name, gb, n)\
626}
627
628#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
629{\
630 int n, nb_bits;\
631 unsigned int index;\
632\
633 index= SHOW_UBITS(name, gb, bits);\
634 level = table[index].level;\
635 n = table[index].len;\
636\
637 if(max_depth > 1 && n < 0){\
638 SKIP_BITS(name, gb, bits)\
639 if(need_update){\
640 UPDATE_CACHE(name, gb)\
641 }\
642\
643 nb_bits = -n;\
644\
645 index= SHOW_UBITS(name, gb, nb_bits) + level;\
646 level = table[index].level;\
647 n = table[index].len;\
648 }\
649 run= table[index].run;\
650 SKIP_BITS(name, gb, n)\
651}
652
653
654/**
655 * parses a vlc code, faster then get_vlc()
656 * @param bits is the number of bits which will be read at once, must be
657 * identical to nb_bits in init_vlc()
658 * @param max_depth is the number of times bits bits must be read to completely
659 * read the longest vlc code
660 * = (max_vlc_length + bits - 1) / bits
661 */
662static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
663 int bits, int max_depth)
664{
665 int code;
666
667 OPEN_READER(re, s)
668 UPDATE_CACHE(re, s)
669
670 GET_VLC(code, re, s, table, bits, max_depth)
671
672 CLOSE_READER(re, s)
673 return code;
674}
675
676//#define TRACE
677
678#ifdef TRACE
679static inline void print_bin(int bits, int n){
680 int i;
681
682 for(i=n-1; i>=0; i--){
683 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
684 }
685 for(i=n; i<24; i++)
686 av_log(NULL, AV_LOG_DEBUG, " ");
687}
688
689static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
690 int r= get_bits(s, n);
691
692 print_bin(r, n);
693 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);
694 return r;
695}
696static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
697 int show= show_bits(s, 24);
698 int pos= get_bits_count(s);
699 int r= get_vlc2(s, table, bits, max_depth);
700 int len= get_bits_count(s) - pos;
701 int bits2= show>>(24-len);
702
703 print_bin(bits2, len);
704
705 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
706 return r;
707}
708static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
709 int show= show_bits(s, n);
710 int r= get_xbits(s, n);
711
712 print_bin(show, n);
713 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);
714 return r;
715}
716
717#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
718#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
719#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
720#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
721#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
722
723#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
724
725#else //TRACE
726#define tprintf(p, ...) {}
727#endif
728
729static inline int decode012(GetBitContext *gb){
730 int n;
731 n = get_bits1(gb);
732 if (n == 0)
733 return 0;
734 else
735 return get_bits1(gb) + 1;
736}
737
738static inline int decode210(GetBitContext *gb){
739 if (get_bits1(gb))
740 return 0;
741 else
742 return 2 - get_bits1(gb);
743}
744
745static inline int get_bits_left(GetBitContext *gb)
746{
747 return gb->size_in_bits - get_bits_count(gb);
748}
749
750#endif /* AVCODEC_GET_BITS_H */