2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
22 * @file libavcodec/get_bits.h
23 * bitstream reader API header.
26 #ifndef AVCODEC_GET_BITS_H
27 #define AVCODEC_GET_BITS_H
32 #include "libavutil/bswap.h"
33 #include "libavutil/common.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/log.h"
38 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
39 # define ALT_BITSTREAM_READER
42 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
44 # define A32_BITSTREAM_READER
46 # define ALT_BITSTREAM_READER
47 //#define LIBMPEG2_BITSTREAM_READER
48 //#define A32_BITSTREAM_READER
53 // avoid +32 for shift optimization (gcc should do that ...)
54 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
55 __asm__ ("sarl %1, %0\n\t"
57 : "ic" ((uint8_t)(-s
))
61 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
62 __asm__ ("shrl %1, %0\n\t"
64 : "ic" ((uint8_t)(-s
))
69 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
70 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
74 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
75 typedef struct GetBitContext
{
76 const uint8_t *buffer
, *buffer_end
;
77 #ifdef ALT_BITSTREAM_READER
79 #elif defined LIBMPEG2_BITSTREAM_READER
83 #elif defined A32_BITSTREAM_READER
92 #define VLC_TYPE int16_t
96 VLC_TYPE (*table
)[2]; ///< code, bits
97 int table_size
, table_allocated
;
100 typedef struct RL_VLC_ELEM
{
106 /* Bitstream reader API docs:
108 arbitrary name which is used as prefix for the internal variables
113 OPEN_READER(name, gb)
114 loads gb into local variables
116 CLOSE_READER(name, gb)
117 stores local vars in gb
119 UPDATE_CACHE(name, gb)
120 refills the internal cache from the bitstream
121 after this call at least MIN_CACHE_BITS will be available,
124 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
126 SHOW_UBITS(name, gb, num)
127 will return the next num bits
129 SHOW_SBITS(name, gb, num)
130 will return the next num bits and do sign extension
132 SKIP_BITS(name, gb, num)
133 will skip over the next num bits
134 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
136 SKIP_CACHE(name, gb, num)
137 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
139 SKIP_COUNTER(name, gb, num)
140 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
142 LAST_SKIP_CACHE(name, gb, num)
143 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
145 LAST_SKIP_BITS(name, gb, num)
146 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
148 for examples see get_bits, show_bits, skip_bits, get_vlc
151 #ifdef ALT_BITSTREAM_READER
152 # define MIN_CACHE_BITS 25
154 # define OPEN_READER(name, gb)\
155 int name##_index= (gb)->index;\
156 int name##_cache= 0;\
158 # define CLOSE_READER(name, gb)\
159 (gb)->index= name##_index;\
161 # ifdef ALT_BITSTREAM_READER_LE
162 # define UPDATE_CACHE(name, gb)\
163 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
165 # define SKIP_CACHE(name, gb, num)\
166 name##_cache >>= (num);
168 # define UPDATE_CACHE(name, gb)\
169 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
171 # define SKIP_CACHE(name, gb, num)\
172 name##_cache <<= (num);
176 # define SKIP_COUNTER(name, gb, num)\
177 name##_index += (num);\
179 # define SKIP_BITS(name, gb, num)\
181 SKIP_CACHE(name, gb, num)\
182 SKIP_COUNTER(name, gb, num)\
185 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
186 # define LAST_SKIP_CACHE(name, gb, num) ;
188 # ifdef ALT_BITSTREAM_READER_LE
189 # define SHOW_UBITS(name, gb, num)\
190 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
192 # define SHOW_SBITS(name, gb, num)\
193 NEG_SSR32((name##_cache)<<(32-(num)), num)
195 # define SHOW_UBITS(name, gb, num)\
196 NEG_USR32(name##_cache, num)
198 # define SHOW_SBITS(name, gb, num)\
199 NEG_SSR32(name##_cache, num)
202 # define GET_CACHE(name, gb)\
203 ((uint32_t)name##_cache)
205 static inline int get_bits_count(GetBitContext
*s
){
209 static inline void skip_bits_long(GetBitContext
*s
, int n
){
213 #elif defined LIBMPEG2_BITSTREAM_READER
214 //libmpeg2 like reader
216 # define MIN_CACHE_BITS 17
218 # define OPEN_READER(name, gb)\
219 int name##_bit_count=(gb)->bit_count;\
220 int name##_cache= (gb)->cache;\
221 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
223 # define CLOSE_READER(name, gb)\
224 (gb)->bit_count= name##_bit_count;\
225 (gb)->cache= name##_cache;\
226 (gb)->buffer_ptr= name##_buffer_ptr;\
228 # define UPDATE_CACHE(name, gb)\
229 if(name##_bit_count >= 0){\
230 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
231 name##_buffer_ptr+=2;\
232 name##_bit_count-= 16;\
235 # define SKIP_CACHE(name, gb, num)\
236 name##_cache <<= (num);\
238 # define SKIP_COUNTER(name, gb, num)\
239 name##_bit_count += (num);\
241 # define SKIP_BITS(name, gb, num)\
243 SKIP_CACHE(name, gb, num)\
244 SKIP_COUNTER(name, gb, num)\
247 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
248 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
250 # define SHOW_UBITS(name, gb, num)\
251 NEG_USR32(name##_cache, num)
253 # define SHOW_SBITS(name, gb, num)\
254 NEG_SSR32(name##_cache, num)
256 # define GET_CACHE(name, gb)\
257 ((uint32_t)name##_cache)
259 static inline int get_bits_count(GetBitContext
*s
){
260 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
263 static inline void skip_bits_long(GetBitContext
*s
, int n
){
266 re_buffer_ptr
+= 2*(re_bit_count
>>4);
268 re_cache
= ((re_buffer_ptr
[-2]<<8) + re_buffer_ptr
[-1]) << (16+re_bit_count
);
273 #elif defined A32_BITSTREAM_READER
275 # define MIN_CACHE_BITS 32
277 # define OPEN_READER(name, gb)\
278 int name##_bit_count=(gb)->bit_count;\
279 uint32_t name##_cache0= (gb)->cache0;\
280 uint32_t name##_cache1= (gb)->cache1;\
281 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
283 # define CLOSE_READER(name, gb)\
284 (gb)->bit_count= name##_bit_count;\
285 (gb)->cache0= name##_cache0;\
286 (gb)->cache1= name##_cache1;\
287 (gb)->buffer_ptr= name##_buffer_ptr;\
289 # define UPDATE_CACHE(name, gb)\
290 if(name##_bit_count > 0){\
291 const uint32_t next= be2me_32( *name##_buffer_ptr );\
292 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
293 name##_cache1 |= next<<name##_bit_count;\
294 name##_buffer_ptr++;\
295 name##_bit_count-= 32;\
299 # define SKIP_CACHE(name, gb, num)\
301 "shldl %2, %1, %0 \n\t"\
303 : "+r" (name##_cache0), "+r" (name##_cache1)\
304 : "Ic" ((uint8_t)(num))\
307 # define SKIP_CACHE(name, gb, num)\
308 name##_cache0 <<= (num);\
309 name##_cache0 |= NEG_USR32(name##_cache1,num);\
310 name##_cache1 <<= (num);
313 # define SKIP_COUNTER(name, gb, num)\
314 name##_bit_count += (num);\
316 # define SKIP_BITS(name, gb, num)\
318 SKIP_CACHE(name, gb, num)\
319 SKIP_COUNTER(name, gb, num)\
322 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
323 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
325 # define SHOW_UBITS(name, gb, num)\
326 NEG_USR32(name##_cache0, num)
328 # define SHOW_SBITS(name, gb, num)\
329 NEG_SSR32(name##_cache0, num)
331 # define GET_CACHE(name, gb)\
334 static inline int get_bits_count(GetBitContext
*s
){
335 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
338 static inline void skip_bits_long(GetBitContext
*s
, int n
){
341 re_buffer_ptr
+= re_bit_count
>>5;
343 re_cache0
= be2me_32( re_buffer_ptr
[-1] ) << re_bit_count
;
352 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
353 * if MSB not set it is negative
354 * @param n length in bits
357 static inline int get_xbits(GetBitContext
*s
, int n
){
359 register int32_t cache
;
362 cache
= GET_CACHE(re
,s
);
364 LAST_SKIP_BITS(re
, s
, n
)
366 return (NEG_USR32(sign
^ cache
, n
) ^ sign
) - sign
;
369 static inline int get_sbits(GetBitContext
*s
, int n
){
373 tmp
= SHOW_SBITS(re
, s
, n
);
374 LAST_SKIP_BITS(re
, s
, n
)
381 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
383 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
387 tmp
= SHOW_UBITS(re
, s
, n
);
388 LAST_SKIP_BITS(re
, s
, n
)
395 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
397 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
401 tmp
= SHOW_UBITS(re
, s
, n
);
402 // CLOSE_READER(re, s)
406 static inline void skip_bits(GetBitContext
*s
, int n
){
407 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
410 LAST_SKIP_BITS(re
, s
, n
)
414 static inline unsigned int get_bits1(GetBitContext
*s
){
415 #ifdef ALT_BITSTREAM_READER
417 uint8_t result
= s
->buffer
[ index
>>3 ];
418 #ifdef ALT_BITSTREAM_READER_LE
419 result
>>= (index
&0x07);
422 result
<<= (index
&0x07);
430 return get_bits(s
, 1);
434 static inline unsigned int show_bits1(GetBitContext
*s
){
435 return show_bits(s
, 1);
438 static inline void skip_bits1(GetBitContext
*s
){
445 static inline unsigned int get_bits_long(GetBitContext
*s
, int n
){
446 if(n
<=17) return get_bits(s
, n
);
448 #ifdef ALT_BITSTREAM_READER_LE
449 int ret
= get_bits(s
, 16);
450 return ret
| (get_bits(s
, n
-16) << 16);
452 int ret
= get_bits(s
, 16) << (n
-16);
453 return ret
| get_bits(s
, n
-16);
459 * reads 0-32 bits as a signed integer.
461 static inline int get_sbits_long(GetBitContext
*s
, int n
) {
462 return sign_extend(get_bits_long(s
, n
), n
);
468 static inline unsigned int show_bits_long(GetBitContext
*s
, int n
){
469 if(n
<=17) return show_bits(s
, n
);
471 GetBitContext gb
= *s
;
472 return get_bits_long(&gb
, n
);
476 static inline int check_marker(GetBitContext
*s
, const char *msg
)
478 int bit
= get_bits1(s
);
480 av_log(NULL
, AV_LOG_INFO
, "Marker bit missing %s\n", msg
);
486 * init GetBitContext.
487 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
488 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
489 * @param bit_size the size of the buffer in bits
491 * While GetBitContext stores the buffer size, for performance reasons you are
492 * responsible for checking for the buffer end yourself (take advantage of the padding)!
494 static inline void init_get_bits(GetBitContext
*s
,
495 const uint8_t *buffer
, int bit_size
)
497 int buffer_size
= (bit_size
+7)>>3;
498 if(buffer_size
< 0 || bit_size
< 0) {
499 buffer_size
= bit_size
= 0;
504 s
->size_in_bits
= bit_size
;
505 s
->buffer_end
= buffer
+ buffer_size
;
506 #ifdef ALT_BITSTREAM_READER
508 #elif defined LIBMPEG2_BITSTREAM_READER
509 s
->buffer_ptr
= (uint8_t*)((intptr_t)buffer
&(~1));
510 s
->bit_count
= 16 + 8*((intptr_t)buffer
&1);
511 skip_bits_long(s
, 0);
512 #elif defined A32_BITSTREAM_READER
513 s
->buffer_ptr
= (uint32_t*)((intptr_t)buffer
&(~3));
514 s
->bit_count
= 32 + 8*((intptr_t)buffer
&3);
515 skip_bits_long(s
, 0);
519 static inline void align_get_bits(GetBitContext
*s
)
521 int n
= (-get_bits_count(s
)) & 7;
522 if(n
) skip_bits(s
, n
);
525 #define init_vlc(vlc, nb_bits, nb_codes,\
526 bits, bits_wrap, bits_size,\
527 codes, codes_wrap, codes_size,\
529 init_vlc_sparse(vlc, nb_bits, nb_codes,\
530 bits, bits_wrap, bits_size,\
531 codes, codes_wrap, codes_size,\
534 int init_vlc_sparse(VLC
*vlc
, int nb_bits
, int nb_codes
,
535 const void *bits
, int bits_wrap
, int bits_size
,
536 const void *codes
, int codes_wrap
, int codes_size
,
537 const void *symbols
, int symbols_wrap
, int symbols_size
,
539 #define INIT_VLC_LE 2
540 #define INIT_VLC_USE_NEW_STATIC 4
541 void free_vlc(VLC
*vlc
);
543 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
545 static VLC_TYPE table[static_size][2];\
546 (vlc)->table= table;\
547 (vlc)->table_allocated= static_size;\
548 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
554 * if the vlc code is invalid and max_depth=1 than no bits will be removed
555 * if the vlc code is invalid and max_depth>1 than the number of bits removed
558 #define GET_VLC(code, name, gb, table, bits, max_depth)\
560 int n, index, nb_bits;\
562 index= SHOW_UBITS(name, gb, bits);\
563 code = table[index][0];\
564 n = table[index][1];\
566 if(max_depth > 1 && n < 0){\
567 LAST_SKIP_BITS(name, gb, bits)\
568 UPDATE_CACHE(name, gb)\
572 index= SHOW_UBITS(name, gb, nb_bits) + code;\
573 code = table[index][0];\
574 n = table[index][1];\
575 if(max_depth > 2 && n < 0){\
576 LAST_SKIP_BITS(name, gb, nb_bits)\
577 UPDATE_CACHE(name, gb)\
581 index= SHOW_UBITS(name, gb, nb_bits) + code;\
582 code = table[index][0];\
583 n = table[index][1];\
586 SKIP_BITS(name, gb, n)\
589 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
591 int n, index, nb_bits;\
593 index= SHOW_UBITS(name, gb, bits);\
594 level = table[index].level;\
595 n = table[index].len;\
597 if(max_depth > 1 && n < 0){\
598 SKIP_BITS(name, gb, bits)\
600 UPDATE_CACHE(name, gb)\
605 index= SHOW_UBITS(name, gb, nb_bits) + level;\
606 level = table[index].level;\
607 n = table[index].len;\
609 run= table[index].run;\
610 SKIP_BITS(name, gb, n)\
615 * parses a vlc code, faster then get_vlc()
616 * @param bits is the number of bits which will be read at once, must be
617 * identical to nb_bits in init_vlc()
618 * @param max_depth is the number of times bits bits must be read to completely
619 * read the longest vlc code
620 * = (max_vlc_length + bits - 1) / bits
622 static av_always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
623 int bits
, int max_depth
)
630 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
639 static inline void print_bin(int bits
, int n
){
642 for(i
=n
-1; i
>=0; i
--){
643 av_log(NULL
, AV_LOG_DEBUG
, "%d", (bits
>>i
)&1);
646 av_log(NULL
, AV_LOG_DEBUG
, " ");
649 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
650 int r
= get_bits(s
, n
);
653 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
);
656 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, const char *func
, int line
){
657 int show
= show_bits(s
, 24);
658 int pos
= get_bits_count(s
);
659 int r
= get_vlc2(s
, table
, bits
, max_depth
);
660 int len
= get_bits_count(s
) - pos
;
661 int bits2
= show
>>(24-len
);
663 print_bin(bits2
, len
);
665 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
668 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
669 int show
= show_bits(s
, n
);
670 int r
= get_xbits(s
, n
);
673 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
);
677 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
678 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
679 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
680 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
681 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
683 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
686 #define tprintf(p, ...) {}
689 static inline int decode012(GetBitContext
*gb
){
695 return get_bits1(gb
) + 1;
698 static inline int decode210(GetBitContext
*gb
){
702 return 2 - get_bits1(gb
);
705 static inline int get_bits_left(GetBitContext
*gb
)
707 return gb
->size_in_bits
- get_bits_count(gb
);
710 #endif /* AVCODEC_GET_BITS_H */