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)
43 # if ARCH_ARM && !HAVE_FAST_UNALIGNED
44 # define A32_BITSTREAM_READER
46 # define ALT_BITSTREAM_READER
47 //#define LIBMPEG2_BITSTREAM_READER
48 //#define A32_BITSTREAM_READER
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 #ifdef ALT_BITSTREAM_READER
58 #elif defined LIBMPEG2_BITSTREAM_READER
62 #elif defined A32_BITSTREAM_READER
71 #define VLC_TYPE int16_t
75 VLC_TYPE (*table
)[2]; ///< code, bits
76 int table_size
, table_allocated
;
79 typedef struct RL_VLC_ELEM
{
85 /* Bitstream reader API docs:
87 arbitrary name which is used as prefix for the internal variables
93 loads gb into local variables
95 CLOSE_READER(name, gb)
96 stores local vars in gb
98 UPDATE_CACHE(name, gb)
99 refills the internal cache from the bitstream
100 after this call at least MIN_CACHE_BITS will be available,
103 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
105 SHOW_UBITS(name, gb, num)
106 will return the next num bits
108 SHOW_SBITS(name, gb, num)
109 will return the next num bits and do sign extension
111 SKIP_BITS(name, gb, num)
112 will skip over the next num bits
113 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
115 SKIP_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)
118 SKIP_COUNTER(name, gb, num)
119 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
121 LAST_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
124 LAST_SKIP_BITS(name, gb, num)
125 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
127 for examples see get_bits, show_bits, skip_bits, get_vlc
130 #ifdef ALT_BITSTREAM_READER
131 # define MIN_CACHE_BITS 25
133 # define OPEN_READER(name, gb)\
134 unsigned int name##_index= (gb)->index;\
135 int name##_cache= 0;\
137 # define CLOSE_READER(name, gb)\
138 (gb)->index= name##_index;\
140 # ifdef ALT_BITSTREAM_READER_LE
141 # define UPDATE_CACHE(name, gb)\
142 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
144 # define SKIP_CACHE(name, gb, num)\
145 name##_cache >>= (num);
147 # define UPDATE_CACHE(name, gb)\
148 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
150 # define SKIP_CACHE(name, gb, num)\
151 name##_cache <<= (num);
155 # define SKIP_COUNTER(name, gb, num)\
156 name##_index += (num);\
158 # define SKIP_BITS(name, gb, num)\
160 SKIP_CACHE(name, gb, num)\
161 SKIP_COUNTER(name, gb, num)\
164 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
165 # define LAST_SKIP_CACHE(name, gb, num) ;
167 # ifdef ALT_BITSTREAM_READER_LE
168 # define SHOW_UBITS(name, gb, num)\
169 zero_extend(name##_cache, num)
171 # define SHOW_SBITS(name, gb, num)\
172 sign_extend(name##_cache, num)
174 # define SHOW_UBITS(name, gb, num)\
175 NEG_USR32(name##_cache, num)
177 # define SHOW_SBITS(name, gb, num)\
178 NEG_SSR32(name##_cache, num)
181 # define GET_CACHE(name, gb)\
182 ((uint32_t)name##_cache)
184 static inline int get_bits_count(const GetBitContext
*s
){
188 static inline void skip_bits_long(GetBitContext
*s
, int n
){
192 #elif defined LIBMPEG2_BITSTREAM_READER
193 //libmpeg2 like reader
195 # define MIN_CACHE_BITS 17
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;\
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;\
207 # define UPDATE_CACHE(name, gb)\
208 if(name##_bit_count >= 0){\
209 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
210 name##_buffer_ptr+=2;\
211 name##_bit_count-= 16;\
214 # define SKIP_CACHE(name, gb, num)\
215 name##_cache <<= (num);\
217 # define SKIP_COUNTER(name, gb, num)\
218 name##_bit_count += (num);\
220 # define SKIP_BITS(name, gb, num)\
222 SKIP_CACHE(name, gb, num)\
223 SKIP_COUNTER(name, gb, num)\
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)
229 # define SHOW_UBITS(name, gb, num)\
230 NEG_USR32(name##_cache, num)
232 # define SHOW_SBITS(name, gb, num)\
233 NEG_SSR32(name##_cache, num)
235 # define GET_CACHE(name, gb)\
236 ((uint32_t)name##_cache)
238 static inline int get_bits_count(const GetBitContext
*s
){
239 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
242 static inline void skip_bits_long(GetBitContext
*s
, int n
){
245 re_buffer_ptr
+= 2*(re_bit_count
>>4);
247 re_cache
= ((re_buffer_ptr
[-2]<<8) + re_buffer_ptr
[-1]) << (16+re_bit_count
);
252 #elif defined A32_BITSTREAM_READER
254 # define MIN_CACHE_BITS 32
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;\
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;\
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;\
278 # define SKIP_CACHE(name, gb, num)\
280 "shldl %2, %1, %0 \n\t"\
282 : "+r" (name##_cache0), "+r" (name##_cache1)\
283 : "Ic" ((uint8_t)(num))\
286 # define SKIP_CACHE(name, gb, num)\
287 name##_cache0 <<= (num);\
288 name##_cache0 |= NEG_USR32(name##_cache1,num);\
289 name##_cache1 <<= (num);
292 # define SKIP_COUNTER(name, gb, num)\
293 name##_bit_count += (num);\
295 # define SKIP_BITS(name, gb, num)\
297 SKIP_CACHE(name, gb, num)\
298 SKIP_COUNTER(name, gb, num)\
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)
304 # define SHOW_UBITS(name, gb, num)\
305 NEG_USR32(name##_cache0, num)
307 # define SHOW_SBITS(name, gb, num)\
308 NEG_SSR32(name##_cache0, num)
310 # define GET_CACHE(name, gb)\
313 static inline int get_bits_count(const GetBitContext
*s
){
314 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
317 static inline void skip_bits_long(GetBitContext
*s
, int n
){
320 re_buffer_ptr
+= re_bit_count
>>5;
322 re_cache0
= be2me_32( re_buffer_ptr
[-1] ) << re_bit_count
;
331 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
332 * if MSB not set it is negative
333 * @param n length in bits
336 static inline int get_xbits(GetBitContext
*s
, int n
){
338 register int32_t cache
;
341 cache
= GET_CACHE(re
,s
);
343 LAST_SKIP_BITS(re
, s
, n
)
345 return (NEG_USR32(sign
^ cache
, n
) ^ sign
) - sign
;
348 static inline int get_sbits(GetBitContext
*s
, int n
){
352 tmp
= SHOW_SBITS(re
, s
, n
);
353 LAST_SKIP_BITS(re
, s
, n
)
360 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
362 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
366 tmp
= SHOW_UBITS(re
, s
, n
);
367 LAST_SKIP_BITS(re
, s
, n
)
374 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
376 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
380 tmp
= SHOW_UBITS(re
, s
, n
);
381 // CLOSE_READER(re, s)
385 static inline void skip_bits(GetBitContext
*s
, int n
){
386 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
389 LAST_SKIP_BITS(re
, s
, n
)
393 static inline unsigned int get_bits1(GetBitContext
*s
){
394 #ifdef ALT_BITSTREAM_READER
395 unsigned int index
= s
->index
;
396 uint8_t result
= s
->buffer
[ index
>>3 ];
397 #ifdef ALT_BITSTREAM_READER_LE
398 result
>>= (index
&0x07);
401 result
<<= (index
&0x07);
409 return get_bits(s
, 1);
413 static inline unsigned int show_bits1(GetBitContext
*s
){
414 return show_bits(s
, 1);
417 static inline void skip_bits1(GetBitContext
*s
){
424 static inline unsigned int get_bits_long(GetBitContext
*s
, int n
){
425 if(n
<=17) return get_bits(s
, n
);
427 #ifdef ALT_BITSTREAM_READER_LE
428 int ret
= get_bits(s
, 16);
429 return ret
| (get_bits(s
, n
-16) << 16);
431 int ret
= get_bits(s
, 16) << (n
-16);
432 return ret
| get_bits(s
, n
-16);
438 * reads 0-32 bits as a signed integer.
440 static inline int get_sbits_long(GetBitContext
*s
, int n
) {
441 return sign_extend(get_bits_long(s
, n
), n
);
447 static inline unsigned int show_bits_long(GetBitContext
*s
, int n
){
448 if(n
<=17) return show_bits(s
, n
);
450 GetBitContext gb
= *s
;
451 return get_bits_long(&gb
, n
);
455 static inline int check_marker(GetBitContext
*s
, const char *msg
)
457 int bit
= get_bits1(s
);
459 av_log(NULL
, AV_LOG_INFO
, "Marker bit missing %s\n", msg
);
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
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)!
473 static inline void init_get_bits(GetBitContext
*s
,
474 const uint8_t *buffer
, int bit_size
)
476 int buffer_size
= (bit_size
+7)>>3;
477 if(buffer_size
< 0 || bit_size
< 0) {
478 buffer_size
= bit_size
= 0;
483 s
->size_in_bits
= bit_size
;
484 s
->buffer_end
= buffer
+ buffer_size
;
485 #ifdef ALT_BITSTREAM_READER
487 #elif defined LIBMPEG2_BITSTREAM_READER
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);
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);
498 static inline void align_get_bits(GetBitContext
*s
)
500 int n
= (-get_bits_count(s
)) & 7;
501 if(n
) skip_bits(s
, n
);
504 #define init_vlc(vlc, nb_bits, nb_codes,\
505 bits, bits_wrap, bits_size,\
506 codes, codes_wrap, codes_size,\
508 init_vlc_sparse(vlc, nb_bits, nb_codes,\
509 bits, bits_wrap, bits_size,\
510 codes, codes_wrap, codes_size,\
513 int init_vlc_sparse(VLC
*vlc
, int nb_bits
, int nb_codes
,
514 const void *bits
, int bits_wrap
, int bits_size
,
515 const void *codes
, int codes_wrap
, int codes_size
,
516 const void *symbols
, int symbols_wrap
, int symbols_size
,
518 #define INIT_VLC_LE 2
519 #define INIT_VLC_USE_NEW_STATIC 4
520 void free_vlc(VLC
*vlc
);
522 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
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);\
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
537 #define GET_VLC(code, name, gb, table, bits, max_depth)\
542 index= SHOW_UBITS(name, gb, bits);\
543 code = table[index][0];\
544 n = table[index][1];\
546 if(max_depth > 1 && n < 0){\
547 LAST_SKIP_BITS(name, gb, bits)\
548 UPDATE_CACHE(name, gb)\
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)\
561 index= SHOW_UBITS(name, gb, nb_bits) + code;\
562 code = table[index][0];\
563 n = table[index][1];\
566 SKIP_BITS(name, gb, n)\
569 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
574 index= SHOW_UBITS(name, gb, bits);\
575 level = table[index].level;\
576 n = table[index].len;\
578 if(max_depth > 1 && n < 0){\
579 SKIP_BITS(name, gb, bits)\
581 UPDATE_CACHE(name, gb)\
586 index= SHOW_UBITS(name, gb, nb_bits) + level;\
587 level = table[index].level;\
588 n = table[index].len;\
590 run= table[index].run;\
591 SKIP_BITS(name, gb, n)\
596 * parses a vlc code, faster then get_vlc()
597 * @param bits is the number of bits which will be read at once, must be
598 * identical to nb_bits in init_vlc()
599 * @param max_depth is the number of times bits bits must be read to completely
600 * read the longest vlc code
601 * = (max_vlc_length + bits - 1) / bits
603 static av_always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
604 int bits
, int max_depth
)
611 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
620 static inline void print_bin(int bits
, int n
){
623 for(i
=n
-1; i
>=0; i
--){
624 av_log(NULL
, AV_LOG_DEBUG
, "%d", (bits
>>i
)&1);
627 av_log(NULL
, AV_LOG_DEBUG
, " ");
630 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
631 int r
= get_bits(s
, n
);
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
);
637 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, const char *func
, int line
){
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
);
644 print_bin(bits2
, len
);
646 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
649 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
650 int show
= show_bits(s
, n
);
651 int r
= get_xbits(s
, n
);
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
);
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__)
664 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
667 #define tprintf(p, ...) {}
670 static inline int decode012(GetBitContext
*gb
){
676 return get_bits1(gb
) + 1;
679 static inline int decode210(GetBitContext
*gb
){
683 return 2 - get_bits1(gb
);
686 static inline int get_bits_left(GetBitContext
*gb
)
688 return gb
->size_in_bits
- get_bits_count(gb
);
691 #endif /* AVCODEC_GET_BITS_H */