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
23 * bitstream reader API header.
26 #ifndef AVCODEC_GET_BITS_H
27 #define AVCODEC_GET_BITS_H
31 #include "ffmpeg_intreadwrite.h"
33 //#include "libavutil/bswap.h"
34 //#include "libavutil/common.h"
35 //#include "libavutil/intreadwrite.h"
36 //#include "libavutil/log.h"
37 //#include "mathops.h"
41 /* rockbox' optimised inline functions */
42 #define bswap_16(x) swap16(x)
43 #define bswap_32(x) swap32(x)
45 #ifdef ROCKBOX_BIG_ENDIAN
46 #define be2me_16(x) (x)
47 #define be2me_32(x) (x)
48 #define le2me_16(x) bswap_16(x)
49 #define le2me_32(x) bswap_32(x)
51 #define be2me_16(x) bswap_16(x)
52 #define be2me_32(x) bswap_32(x)
53 #define le2me_16(x) (x)
54 #define le2me_32(x) (x)
57 #define av_const __attribute__((const))
58 #define av_always_inline inline __attribute__((always_inline))
60 /* The following is taken from mathops.h */
63 static inline av_const
int sign_extend(int val
, unsigned bits
)
65 return (val
<< ((8 * sizeof(int)) - bits
)) >> ((8 * sizeof(int)) - bits
);
70 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
74 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
77 /* these 2 are from libavutil/common.h */
79 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
80 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
82 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
83 # define ALT_BITSTREAM_READER
87 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
88 # if ARCH_ARM && !HAVE_FAST_UNALIGNED
89 # define A32_BITSTREAM_READER
92 # define ALT_BITSTREAM_READER
94 //#define LIBMPEG2_BITSTREAM_READER
95 //#define A32_BITSTREAM_READER
101 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
102 typedef struct GetBitContext
{
103 const uint8_t *buffer
, *buffer_end
;
104 #ifdef ALT_BITSTREAM_READER
106 #elif defined LIBMPEG2_BITSTREAM_READER
110 #elif defined A32_BITSTREAM_READER
111 uint32_t *buffer_ptr
;
119 #define VLC_TYPE int16_t
123 VLC_TYPE (*table
)[2]; ///< code, bits
124 int table_size
, table_allocated
;
127 typedef struct RL_VLC_ELEM
{
133 /* Bitstream reader API docs:
135 arbitrary name which is used as prefix for the internal variables
140 OPEN_READER(name, gb)
141 loads gb into local variables
143 CLOSE_READER(name, gb)
144 stores local vars in gb
146 UPDATE_CACHE(name, gb)
147 refills the internal cache from the bitstream
148 after this call at least MIN_CACHE_BITS will be available,
151 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
153 SHOW_UBITS(name, gb, num)
154 will return the next num bits
156 SHOW_SBITS(name, gb, num)
157 will return the next num bits and do sign extension
159 SKIP_BITS(name, gb, num)
160 will skip over the next num bits
161 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
163 SKIP_CACHE(name, gb, num)
164 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
166 SKIP_COUNTER(name, gb, num)
167 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
169 LAST_SKIP_CACHE(name, gb, num)
170 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
172 LAST_SKIP_BITS(name, gb, num)
173 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
175 for examples see get_bits, show_bits, skip_bits, get_vlc
178 #ifdef ALT_BITSTREAM_READER
179 # define MIN_CACHE_BITS 25
182 /* ROCKBOX: work around "set but not used" warning */
183 # define OPEN_READER(name, gb)\
184 unsigned int name##_index= (gb)->index;\
185 int name##_cache __attribute__((unused)) = 0;\
187 # define CLOSE_READER(name, gb)\
188 (gb)->index= name##_index;\
190 # ifdef ALT_BITSTREAM_READER_LE
191 # define UPDATE_CACHE(name, gb)\
192 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
194 # define SKIP_CACHE(name, gb, num)\
195 name##_cache >>= (num);
197 # define UPDATE_CACHE(name, gb)\
198 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
200 # define SKIP_CACHE(name, gb, num)\
201 name##_cache <<= (num);
205 # define SKIP_COUNTER(name, gb, num)\
206 name##_index += (num);\
208 # define SKIP_BITS(name, gb, num)\
210 SKIP_CACHE(name, gb, num)\
211 SKIP_COUNTER(name, gb, num)\
214 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
215 # define LAST_SKIP_CACHE(name, gb, num) ;
217 # ifdef ALT_BITSTREAM_READER_LE
218 # define SHOW_UBITS(name, gb, num)\
219 zero_extend(name##_cache, num)
221 # define SHOW_SBITS(name, gb, num)\
222 sign_extend(name##_cache, num)
224 # define SHOW_UBITS(name, gb, num)\
225 NEG_USR32(name##_cache, num)
227 # define SHOW_SBITS(name, gb, num)\
228 NEG_SSR32(name##_cache, num)
231 # define GET_CACHE(name, gb)\
232 ((uint32_t)name##_cache)
234 static inline int get_bits_count(const GetBitContext
*s
){
238 static inline void skip_bits_long(GetBitContext
*s
, int n
){
242 #elif defined LIBMPEG2_BITSTREAM_READER
243 //libmpeg2 like reader
245 # define MIN_CACHE_BITS 17
247 # define OPEN_READER(name, gb)\
248 int name##_bit_count=(gb)->bit_count;\
249 int name##_cache= (gb)->cache;\
250 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
252 # define CLOSE_READER(name, gb)\
253 (gb)->bit_count= name##_bit_count;\
254 (gb)->cache= name##_cache;\
255 (gb)->buffer_ptr= name##_buffer_ptr;\
257 # define UPDATE_CACHE(name, gb)\
258 if(name##_bit_count >= 0){\
259 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
260 name##_buffer_ptr+=2;\
261 name##_bit_count-= 16;\
264 # define SKIP_CACHE(name, gb, num)\
265 name##_cache <<= (num);\
267 # define SKIP_COUNTER(name, gb, num)\
268 name##_bit_count += (num);\
270 # define SKIP_BITS(name, gb, num)\
272 SKIP_CACHE(name, gb, num)\
273 SKIP_COUNTER(name, gb, num)\
276 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
277 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
279 # define SHOW_UBITS(name, gb, num)\
280 NEG_USR32(name##_cache, num)
282 # define SHOW_SBITS(name, gb, num)\
283 NEG_SSR32(name##_cache, num)
285 # define GET_CACHE(name, gb)\
286 ((uint32_t)name##_cache)
288 static inline int get_bits_count(const GetBitContext
*s
){
289 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
292 static inline void skip_bits_long(GetBitContext
*s
, int n
){
295 re_buffer_ptr
+= 2*(re_bit_count
>>4);
297 re_cache
= ((re_buffer_ptr
[-2]<<8) + re_buffer_ptr
[-1]) << (16+re_bit_count
);
302 #elif defined A32_BITSTREAM_READER
304 # define MIN_CACHE_BITS 32
306 # define OPEN_READER(name, gb)\
307 int name##_bit_count=(gb)->bit_count;\
308 uint32_t name##_cache0= (gb)->cache0;\
309 uint32_t name##_cache1= (gb)->cache1;\
310 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
312 # define CLOSE_READER(name, gb)\
313 (gb)->bit_count= name##_bit_count;\
314 (gb)->cache0= name##_cache0;\
315 (gb)->cache1= name##_cache1;\
316 (gb)->buffer_ptr= name##_buffer_ptr;\
318 # define UPDATE_CACHE(name, gb)\
319 if(name##_bit_count > 0){\
320 const uint32_t next= av_be2ne32( *name##_buffer_ptr );\
321 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
322 name##_cache1 |= next<<name##_bit_count;\
323 name##_buffer_ptr++;\
324 name##_bit_count-= 32;\
328 # define SKIP_CACHE(name, gb, num)\
330 "shldl %2, %1, %0 \n\t"\
332 : "+r" (name##_cache0), "+r" (name##_cache1)\
333 : "Ic" ((uint8_t)(num))\
336 # define SKIP_CACHE(name, gb, num)\
337 name##_cache0 <<= (num);\
338 name##_cache0 |= NEG_USR32(name##_cache1,num);\
339 name##_cache1 <<= (num);
342 # define SKIP_COUNTER(name, gb, num)\
343 name##_bit_count += (num);\
345 # define SKIP_BITS(name, gb, num)\
347 SKIP_CACHE(name, gb, num)\
348 SKIP_COUNTER(name, gb, num)\
351 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
352 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
354 # define SHOW_UBITS(name, gb, num)\
355 NEG_USR32(name##_cache0, num)
357 # define SHOW_SBITS(name, gb, num)\
358 NEG_SSR32(name##_cache0, num)
360 # define GET_CACHE(name, gb)\
363 static inline int get_bits_count(const GetBitContext
*s
){
364 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
367 static inline void skip_bits_long(GetBitContext
*s
, int n
){
370 re_buffer_ptr
+= re_bit_count
>>5;
372 re_cache0
= av_be2ne32( re_buffer_ptr
[-1] ) << re_bit_count
;
381 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
382 * if MSB not set it is negative
383 * @param n length in bits
386 static inline int get_xbits(GetBitContext
*s
, int n
){
388 register int32_t cache
;
391 cache
= GET_CACHE(re
,s
);
393 LAST_SKIP_BITS(re
, s
, n
)
395 return (NEG_USR32(sign
^ cache
, n
) ^ sign
) - sign
;
398 static inline int get_sbits(GetBitContext
*s
, int n
){
402 tmp
= SHOW_SBITS(re
, s
, n
);
403 LAST_SKIP_BITS(re
, s
, n
)
410 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
412 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
416 tmp
= SHOW_UBITS(re
, s
, n
);
417 LAST_SKIP_BITS(re
, s
, n
)
424 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
426 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
430 tmp
= SHOW_UBITS(re
, s
, n
);
431 // CLOSE_READER(re, s)
435 static inline void skip_bits(GetBitContext
*s
, int n
){
436 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
439 LAST_SKIP_BITS(re
, s
, n
)
443 static inline unsigned int get_bits1(GetBitContext
*s
){
444 #ifdef ALT_BITSTREAM_READER
445 unsigned int index
= s
->index
;
446 uint8_t result
= s
->buffer
[ index
>>3 ];
447 #ifdef ALT_BITSTREAM_READER_LE
448 result
>>= (index
&0x07);
451 result
<<= (index
&0x07);
459 return get_bits(s
, 1);
463 static inline unsigned int show_bits1(GetBitContext
*s
){
464 return show_bits(s
, 1);
467 static inline void skip_bits1(GetBitContext
*s
){
474 static inline unsigned int get_bits_long(GetBitContext
*s
, int n
){
475 if(n
<=MIN_CACHE_BITS
) return get_bits(s
, n
);
477 #ifdef ALT_BITSTREAM_READER_LE
478 int ret
= get_bits(s
, 16);
479 return ret
| (get_bits(s
, n
-16) << 16);
481 int ret
= get_bits(s
, 16) << (n
-16);
482 return ret
| get_bits(s
, n
-16);
488 * reads 0-32 bits as a signed integer.
490 static inline int get_sbits_long(GetBitContext
*s
, int n
) {
491 return sign_extend(get_bits_long(s
, n
), n
);
497 static inline unsigned int show_bits_long(GetBitContext
*s
, int n
){
498 if(n
<=MIN_CACHE_BITS
) return show_bits(s
, n
);
500 GetBitContext gb
= *s
;
501 return get_bits_long(&gb
, n
);
506 static inline int check_marker(GetBitContext *s, const char *msg)
508 int bit= get_bits1(s);
510 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
517 * init GetBitContext.
518 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
519 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
520 * @param bit_size the size of the buffer in bits
522 * While GetBitContext stores the buffer size, for performance reasons you are
523 * responsible for checking for the buffer end yourself (take advantage of the padding)!
525 static inline void init_get_bits(GetBitContext
*s
,
526 const uint8_t *buffer
, int bit_size
)
528 int buffer_size
= (bit_size
+7)>>3;
529 if(buffer_size
< 0 || bit_size
< 0) {
530 buffer_size
= bit_size
= 0;
535 s
->size_in_bits
= bit_size
;
536 s
->buffer_end
= buffer
+ buffer_size
;
537 #ifdef ALT_BITSTREAM_READER
539 #elif defined LIBMPEG2_BITSTREAM_READER
540 s
->buffer_ptr
= (uint8_t*)((intptr_t)buffer
&(~1));
541 s
->bit_count
= 16 + 8*((intptr_t)buffer
&1);
542 skip_bits_long(s
, 0);
543 #elif defined A32_BITSTREAM_READER
544 s
->buffer_ptr
= (uint32_t*)((intptr_t)buffer
&(~3));
545 s
->bit_count
= 32 + 8*((intptr_t)buffer
&3);
546 skip_bits_long(s
, 0);
550 static inline void align_get_bits(GetBitContext
*s
)
552 int n
= (-get_bits_count(s
)) & 7;
553 if(n
) skip_bits(s
, n
);
556 #define init_vlc(vlc, nb_bits, nb_codes,\
557 bits, bits_wrap, bits_size,\
558 codes, codes_wrap, codes_size,\
560 init_vlc_sparse(vlc, nb_bits, nb_codes,\
561 bits, bits_wrap, bits_size,\
562 codes, codes_wrap, codes_size,\
565 int init_vlc_sparse(VLC
*vlc
, int nb_bits
, int nb_codes
,
566 const void *bits
, int bits_wrap
, int bits_size
,
567 const void *codes
, int codes_wrap
, int codes_size
,
568 const void *symbols
, int symbols_wrap
, int symbols_size
,
570 #define INIT_VLC_LE 2
571 #define INIT_VLC_USE_NEW_STATIC 4
572 void free_vlc(VLC
*vlc
);
574 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
576 static VLC_TYPE table[static_size][2];\
577 (vlc)->table= table;\
578 (vlc)->table_allocated= static_size;\
579 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
585 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
586 * If the vlc code is invalid and max_depth>1, then the number of bits removed
589 #define GET_VLC(code, name, gb, table, bits, max_depth)\
594 index= SHOW_UBITS(name, gb, bits);\
595 code = table[index][0];\
596 n = table[index][1];\
598 if(max_depth > 1 && n < 0){\
599 LAST_SKIP_BITS(name, gb, bits)\
600 UPDATE_CACHE(name, gb)\
604 index= SHOW_UBITS(name, gb, nb_bits) + code;\
605 code = table[index][0];\
606 n = table[index][1];\
607 if(max_depth > 2 && n < 0){\
608 LAST_SKIP_BITS(name, gb, nb_bits)\
609 UPDATE_CACHE(name, gb)\
613 index= SHOW_UBITS(name, gb, nb_bits) + code;\
614 code = table[index][0];\
615 n = table[index][1];\
618 SKIP_BITS(name, gb, n)\
621 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
626 index= SHOW_UBITS(name, gb, bits);\
627 level = table[index].level;\
628 n = table[index].len;\
630 if(max_depth > 1 && n < 0){\
631 SKIP_BITS(name, gb, bits)\
633 UPDATE_CACHE(name, gb)\
638 index= SHOW_UBITS(name, gb, nb_bits) + level;\
639 level = table[index].level;\
640 n = table[index].len;\
642 run= table[index].run;\
643 SKIP_BITS(name, gb, n)\
648 * parses a vlc code, faster then get_vlc()
649 * @param bits is the number of bits which will be read at once, must be
650 * identical to nb_bits in init_vlc()
651 * @param max_depth is the number of times bits bits must be read to completely
652 * read the longest vlc code
653 * = (max_vlc_length + bits - 1) / bits
655 static av_always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
656 int bits
, int max_depth
)
663 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
672 static inline void print_bin(int bits
, int n
){
675 for(i
=n
-1; i
>=0; i
--){
676 av_log(NULL
, AV_LOG_DEBUG
, "%d", (bits
>>i
)&1);
679 av_log(NULL
, AV_LOG_DEBUG
, " ");
682 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
683 int r
= get_bits(s
, n
);
686 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
);
689 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, const char *func
, int line
){
690 int show
= show_bits(s
, 24);
691 int pos
= get_bits_count(s
);
692 int r
= get_vlc2(s
, table
, bits
, max_depth
);
693 int len
= get_bits_count(s
) - pos
;
694 int bits2
= show
>>(24-len
);
696 print_bin(bits2
, len
);
698 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
701 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
702 int show
= show_bits(s
, n
);
703 int r
= get_xbits(s
, n
);
706 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
);
710 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
711 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
712 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
713 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
714 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
716 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
719 #define tprintf(p, ...) {}
722 static inline int decode012(GetBitContext
*gb
){
728 return get_bits1(gb
) + 1;
731 static inline int decode210(GetBitContext
*gb
){
735 return 2 - get_bits1(gb
);
738 static inline int get_bits_left(GetBitContext
*gb
)
740 return gb
->size_in_bits
- get_bits_count(gb
);
743 #endif /* AVCODEC_GET_BITS_H */