3
* bitstream api header.
9
//#define ALT_BITSTREAM_WRITER
10
//#define ALIGNED_BITSTREAM_WRITER
12
#define ALT_BITSTREAM_READER
13
//#define LIBMPEG2_BITSTREAM_READER
14
//#define A32_BITSTREAM_READER
15
#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
17
extern const uint8_t ff_reverse[256];
19
#if defined(ARCH_X86) || defined(ARCH_X86_64)
20
// avoid +32 for shift optimization (gcc should do that ...)
21
static inline int32_t NEG_SSR32( int32_t a, int8_t s){
22
asm ("sarl %1, %0\n\t"
24
: "ic" ((uint8_t)(-s))
28
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
29
asm ("shrl %1, %0\n\t"
31
: "ic" ((uint8_t)(-s))
36
# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
37
# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
42
/* buf and buf_end must be present and used by every alternative writer. */
43
typedef struct PutBitContext {
44
#ifdef ALT_BITSTREAM_WRITER
45
uint8_t *buf, *buf_end;
50
uint8_t *buf, *buf_ptr, *buf_end;
54
static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
62
s->buf_end = s->buf + buffer_size;
63
#ifdef ALT_BITSTREAM_WRITER
65
((uint32_t*)(s->buf))[0]=0;
66
// memset(buffer, 0, buffer_size);
74
/* return the number of bits output */
75
static inline int put_bits_count(PutBitContext *s)
77
#ifdef ALT_BITSTREAM_WRITER
80
return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
84
/* pad the end of the output stream with zeros */
85
static inline void flush_put_bits(PutBitContext *s)
87
#ifdef ALT_BITSTREAM_WRITER
90
s->bit_buf<<= s->bit_left;
91
while (s->bit_left < 32) {
92
/* XXX: should test end of buffer */
93
*s->buf_ptr++=s->bit_buf >> 24;
102
void align_put_bits(PutBitContext *s);
103
void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
106
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
107
typedef struct GetBitContext {
108
const uint8_t *buffer, *buffer_end;
109
#ifdef ALT_BITSTREAM_READER
111
#elif defined LIBMPEG2_BITSTREAM_READER
115
#elif defined A32_BITSTREAM_READER
116
uint32_t *buffer_ptr;
124
#define VLC_TYPE int16_t
128
VLC_TYPE (*table)[2]; ///< code, bits
129
int table_size, table_allocated;
132
typedef struct RL_VLC_ELEM {
138
#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS)
139
#define UNALIGNED_STORES_ARE_BAD
142
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
143
#if defined(ARCH_X86) || defined(ARCH_X86_64)
144
# define unaligned16(a) (*(const uint16_t*)(a))
145
# define unaligned32(a) (*(const uint32_t*)(a))
146
# define unaligned64(a) (*(const uint64_t*)(a))
149
# define unaligned(x) \
150
static inline uint##x##_t unaligned##x(const void *v) { \
153
} __attribute__((packed)); \
155
return ((const struct Unaligned *) v)->i; \
157
# elif defined(__DECC)
158
# define unaligned(x) \
159
static inline uint##x##_t unaligned##x##(const void *v) { \
160
return *(const __unaligned uint##x##_t *) v; \
163
# define unaligned(x) \
164
static inline uint##x##_t unaligned##x##(const void *v) { \
165
return *(const uint##x##_t *) v; \
174
#ifndef ALT_BITSTREAM_WRITER
175
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
177
unsigned int bit_buf;
180
// printf("put_bits=%d %x\n", n, value);
181
assert(n == 32 || value < (1U << n));
183
bit_buf = s->bit_buf;
184
bit_left = s->bit_left;
186
// printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
189
bit_buf = (bit_buf<<n) | value;
193
bit_buf |= value >> (n - bit_left);
194
#ifdef UNALIGNED_STORES_ARE_BAD
195
if (3 & (intptr_t) s->buf_ptr) {
196
s->buf_ptr[0] = bit_buf >> 24;
197
s->buf_ptr[1] = bit_buf >> 16;
198
s->buf_ptr[2] = bit_buf >> 8;
199
s->buf_ptr[3] = bit_buf ;
202
*(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
203
//printf("bitbuf = %08x\n", bit_buf);
209
s->bit_buf = bit_buf;
210
s->bit_left = bit_left;
215
#ifdef ALT_BITSTREAM_WRITER
216
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
218
# ifdef ALIGNED_BITSTREAM_WRITER
219
# if defined(ARCH_X86) || defined(ARCH_X86_64)
221
"movl %0, %%ecx \n\t"
222
"xorl %%eax, %%eax \n\t"
223
"shrdl %%cl, %1, %%eax \n\t"
225
"movl %0, %%ecx \n\t"
226
"shrl $3, %%ecx \n\t"
227
"andl $0xFFFFFFFC, %%ecx \n\t"
229
"orl %1, (%2, %%ecx) \n\t"
232
"movl %%eax, 4(%2, %%ecx) \n\t"
233
: "=&r" (s->index), "=&r" (value)
234
: "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
239
uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
243
ptr[0] |= be2me_32(value>>(index&31));
244
ptr[1] = be2me_32(value<<(32-(index&31)));
245
//if(n>24) printf("%d %d\n", n, value);
249
# else //ALIGNED_BITSTREAM_WRITER
250
# if defined(ARCH_X86) || defined(ARCH_X86_64)
252
"movl $7, %%ecx \n\t"
253
"andl %0, %%ecx \n\t"
254
"addl %3, %%ecx \n\t"
258
"movl %0, %%ecx \n\t"
259
"shrl $3, %%ecx \n\t"
260
"orl %1, (%%ecx, %2) \n\t"
262
"movl $0, 4(%%ecx, %2) \n\t"
263
: "=&r" (s->index), "=&r" (value)
264
: "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
269
uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
271
ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
273
//if(n>24) printf("%d %d\n", n, value);
277
# endif //!ALIGNED_BITSTREAM_WRITER
282
static inline uint8_t* pbBufPtr(PutBitContext *s)
284
#ifdef ALT_BITSTREAM_WRITER
285
return s->buf + (s->index>>3);
293
* PutBitContext must be flushed & aligned to a byte boundary before calling this.
295
static inline void skip_put_bytes(PutBitContext *s, int n){
296
assert((put_bits_count(s)&7)==0);
297
#ifdef ALT_BITSTREAM_WRITER
298
FIXME may need some cleaning of the buffer
301
assert(s->bit_left==32);
307
* skips the given number of bits.
308
* must only be used if the actual values in the bitstream dont matter
310
static inline void skip_put_bits(PutBitContext *s, int n){
311
#ifdef ALT_BITSTREAM_WRITER
315
s->buf_ptr-= s->bit_left>>5;
321
* Changes the end of the buffer.
323
static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
324
s->buf_end= s->buf + size;
327
/* Bitstream reader API docs:
329
abritary name which is used as prefix for the internal variables
334
OPEN_READER(name, gb)
335
loads gb into local variables
337
CLOSE_READER(name, gb)
338
stores local vars in gb
340
UPDATE_CACHE(name, gb)
341
refills the internal cache from the bitstream
342
after this call at least MIN_CACHE_BITS will be available,
345
will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
347
SHOW_UBITS(name, gb, num)
348
will return the next num bits
350
SHOW_SBITS(name, gb, num)
351
will return the next num bits and do sign extension
353
SKIP_BITS(name, gb, num)
354
will skip over the next num bits
355
note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
357
SKIP_CACHE(name, gb, num)
358
will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
360
SKIP_COUNTER(name, gb, num)
361
will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
363
LAST_SKIP_CACHE(name, gb, num)
364
will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
366
LAST_SKIP_BITS(name, gb, num)
367
is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
369
for examples see get_bits, show_bits, skip_bits, get_vlc
372
static inline int unaligned32_be(const void *v)
376
return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
378
return be2me_32( unaligned32(v)); //original
382
static inline int unaligned32_le(const void *v)
386
return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
388
return le2me_32( unaligned32(v)); //original
392
#ifdef ALT_BITSTREAM_READER
393
# define MIN_CACHE_BITS 25
395
# define OPEN_READER(name, gb)\
396
int name##_index= (gb)->index;\
397
int name##_cache= 0;\
399
# define CLOSE_READER(name, gb)\
400
(gb)->index= name##_index;\
402
# ifdef ALT_BITSTREAM_READER_LE
403
# define UPDATE_CACHE(name, gb)\
404
name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
406
# define SKIP_CACHE(name, gb, num)\
407
name##_cache >>= (num);
409
# define UPDATE_CACHE(name, gb)\
410
name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
412
# define SKIP_CACHE(name, gb, num)\
413
name##_cache <<= (num);
417
# define SKIP_COUNTER(name, gb, num)\
418
name##_index += (num);\
420
# define SKIP_BITS(name, gb, num)\
422
SKIP_CACHE(name, gb, num)\
423
SKIP_COUNTER(name, gb, num)\
426
# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
427
# define LAST_SKIP_CACHE(name, gb, num) ;
429
# ifdef ALT_BITSTREAM_READER_LE
430
# define SHOW_UBITS(name, gb, num)\
431
((name##_cache) & (NEG_USR32(0xffffffff,num)))
433
# define SHOW_UBITS(name, gb, num)\
434
NEG_USR32(name##_cache, num)
437
# define SHOW_SBITS(name, gb, num)\
438
NEG_SSR32(name##_cache, num)
440
# define GET_CACHE(name, gb)\
441
((uint32_t)name##_cache)
443
static inline int get_bits_count(GetBitContext *s){
446
#elif defined LIBMPEG2_BITSTREAM_READER
447
//libmpeg2 like reader
449
# define MIN_CACHE_BITS 17
451
# define OPEN_READER(name, gb)\
452
int name##_bit_count=(gb)->bit_count;\
453
int name##_cache= (gb)->cache;\
454
uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
456
# define CLOSE_READER(name, gb)\
457
(gb)->bit_count= name##_bit_count;\
458
(gb)->cache= name##_cache;\
459
(gb)->buffer_ptr= name##_buffer_ptr;\
461
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
463
# define UPDATE_CACHE(name, gb)\
464
if(name##_bit_count >= 0){\
465
name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
466
name##_buffer_ptr += 2;\
467
name##_bit_count-= 16;\
472
# define UPDATE_CACHE(name, gb)\
473
if(name##_bit_count >= 0){\
474
name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
475
name##_buffer_ptr+=2;\
476
name##_bit_count-= 16;\
481
# define SKIP_CACHE(name, gb, num)\
482
name##_cache <<= (num);\
484
# define SKIP_COUNTER(name, gb, num)\
485
name##_bit_count += (num);\
487
# define SKIP_BITS(name, gb, num)\
489
SKIP_CACHE(name, gb, num)\
490
SKIP_COUNTER(name, gb, num)\
493
# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
494
# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
496
# define SHOW_UBITS(name, gb, num)\
497
NEG_USR32(name##_cache, num)
499
# define SHOW_SBITS(name, gb, num)\
500
NEG_SSR32(name##_cache, num)
502
# define GET_CACHE(name, gb)\
503
((uint32_t)name##_cache)
505
static inline int get_bits_count(GetBitContext *s){
506
return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
509
#elif defined A32_BITSTREAM_READER
511
# define MIN_CACHE_BITS 32
513
# define OPEN_READER(name, gb)\
514
int name##_bit_count=(gb)->bit_count;\
515
uint32_t name##_cache0= (gb)->cache0;\
516
uint32_t name##_cache1= (gb)->cache1;\
517
uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
519
# define CLOSE_READER(name, gb)\
520
(gb)->bit_count= name##_bit_count;\
521
(gb)->cache0= name##_cache0;\
522
(gb)->cache1= name##_cache1;\
523
(gb)->buffer_ptr= name##_buffer_ptr;\
525
# define UPDATE_CACHE(name, gb)\
526
if(name##_bit_count > 0){\
527
const uint32_t next= be2me_32( *name##_buffer_ptr );\
528
name##_cache0 |= NEG_USR32(next,name##_bit_count);\
529
name##_cache1 |= next<<name##_bit_count;\
530
name##_buffer_ptr++;\
531
name##_bit_count-= 32;\
534
#if defined(ARCH_X86) || defined(ARCH_X86_64)
535
# define SKIP_CACHE(name, gb, num)\
537
"shldl %2, %1, %0 \n\t"\
539
: "+r" (name##_cache0), "+r" (name##_cache1)\
540
: "Ic" ((uint8_t)num)\
543
# define SKIP_CACHE(name, gb, num)\
544
name##_cache0 <<= (num);\
545
name##_cache0 |= NEG_USR32(name##_cache1,num);\
546
name##_cache1 <<= (num);
549
# define SKIP_COUNTER(name, gb, num)\
550
name##_bit_count += (num);\
552
# define SKIP_BITS(name, gb, num)\
554
SKIP_CACHE(name, gb, num)\
555
SKIP_COUNTER(name, gb, num)\
558
# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
559
# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
561
# define SHOW_UBITS(name, gb, num)\
562
NEG_USR32(name##_cache0, num)
564
# define SHOW_SBITS(name, gb, num)\
565
NEG_SSR32(name##_cache0, num)
567
# define GET_CACHE(name, gb)\
570
static inline int get_bits_count(GetBitContext *s){
571
return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
577
* read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
578
* if MSB not set it is negative
579
* @param n length in bits
582
static inline int get_xbits(GetBitContext *s, int n){
584
register int32_t cache;
587
cache = GET_CACHE(re,s);
589
LAST_SKIP_BITS(re, s, n)
591
return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
594
static inline int get_sbits(GetBitContext *s, int n){
598
tmp= SHOW_SBITS(re, s, n);
599
LAST_SKIP_BITS(re, s, n)
606
* Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
608
static inline unsigned int get_bits(GetBitContext *s, int n){
612
tmp= SHOW_UBITS(re, s, n);
613
LAST_SKIP_BITS(re, s, n)
618
unsigned int get_bits_long(GetBitContext *s, int n);
622
* Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
624
static inline unsigned int show_bits(GetBitContext *s, int n){
628
tmp= SHOW_UBITS(re, s, n);
629
// CLOSE_READER(re, s)
633
unsigned int show_bits_long(GetBitContext *s, int n);
635
static inline void skip_bits(GetBitContext *s, int n){
636
//Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
639
LAST_SKIP_BITS(re, s, n)
643
static inline unsigned int get_bits1(GetBitContext *s){
644
#ifdef ALT_BITSTREAM_READER
646
uint8_t result= s->buffer[ index>>3 ];
647
#ifdef ALT_BITSTREAM_READER_LE
648
result>>= (index&0x07);
651
result<<= (index&0x07);
659
return get_bits(s, 1);
663
static inline unsigned int show_bits1(GetBitContext *s){
664
return show_bits(s, 1);
667
static inline void skip_bits1(GetBitContext *s){
672
* init GetBitContext.
673
* @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
674
* because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
675
* @param bit_size the size of the buffer in bits
677
static inline void init_get_bits(GetBitContext *s,
678
const uint8_t *buffer, int bit_size)
680
int buffer_size= (bit_size+7)>>3;
681
if(buffer_size < 0 || bit_size < 0) {
682
buffer_size = bit_size = 0;
687
s->size_in_bits= bit_size;
688
s->buffer_end= buffer + buffer_size;
689
#ifdef ALT_BITSTREAM_READER
691
#elif defined LIBMPEG2_BITSTREAM_READER
692
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
695
s->cache = (*buffer++)<<24;
696
s->buffer_ptr = buffer;
701
s->buffer_ptr = buffer;
705
#elif defined A32_BITSTREAM_READER
706
s->buffer_ptr = (uint32_t*)buffer;
717
#ifdef A32_BITSTREAM_READER
722
int check_marker(GetBitContext *s, const char *msg);
723
void align_get_bits(GetBitContext *s);
724
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
725
const void *bits, int bits_wrap, int bits_size,
726
const void *codes, int codes_wrap, int codes_size,
728
#define INIT_VLC_USE_STATIC 1
729
#define INIT_VLC_LE 2
730
void free_vlc(VLC *vlc);
734
* if the vlc code is invalid and max_depth=1 than no bits will be removed
735
* if the vlc code is invalid and max_depth>1 than the number of bits removed
738
#define GET_VLC(code, name, gb, table, bits, max_depth)\
740
int n, index, nb_bits;\
742
index= SHOW_UBITS(name, gb, bits);\
743
code = table[index][0];\
744
n = table[index][1];\
746
if(max_depth > 1 && n < 0){\
747
LAST_SKIP_BITS(name, gb, bits)\
748
UPDATE_CACHE(name, gb)\
752
index= SHOW_UBITS(name, gb, nb_bits) + code;\
753
code = table[index][0];\
754
n = table[index][1];\
755
if(max_depth > 2 && n < 0){\
756
LAST_SKIP_BITS(name, gb, nb_bits)\
757
UPDATE_CACHE(name, gb)\
761
index= SHOW_UBITS(name, gb, nb_bits) + code;\
762
code = table[index][0];\
763
n = table[index][1];\
766
SKIP_BITS(name, gb, n)\
769
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
771
int n, index, nb_bits;\
773
index= SHOW_UBITS(name, gb, bits);\
774
level = table[index].level;\
775
n = table[index].len;\
777
if(max_depth > 1 && n < 0){\
778
SKIP_BITS(name, gb, bits)\
780
UPDATE_CACHE(name, gb)\
785
index= SHOW_UBITS(name, gb, nb_bits) + level;\
786
level = table[index].level;\
787
n = table[index].len;\
789
run= table[index].run;\
790
SKIP_BITS(name, gb, n)\
795
* parses a vlc code, faster then get_vlc()
796
* @param bits is the number of bits which will be read at once, must be
797
* identical to nb_bits in init_vlc()
798
* @param max_depth is the number of times bits bits must be readed to completly
799
* read the longest vlc code
800
* = (max_vlc_length + bits - 1) / bits
802
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
803
int bits, int max_depth)
810
GET_VLC(code, re, s, table, bits, max_depth)
820
static inline void print_bin(int bits, int n){
823
for(i=n-1; i>=0; i--){
824
av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
827
av_log(NULL, AV_LOG_DEBUG, " ");
830
static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
831
int r= get_bits(s, n);
834
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);
837
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
838
int show= show_bits(s, 24);
839
int pos= get_bits_count(s);
840
int r= get_vlc2(s, table, bits, max_depth);
841
int len= get_bits_count(s) - pos;
842
int bits2= show>>(24-len);
844
print_bin(bits2, len);
846
av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
849
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
850
int show= show_bits(s, n);
851
int r= get_xbits(s, n);
854
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);
858
#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
859
#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
860
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
861
#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
862
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
864
#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
867
#define tprintf(...) {}
870
static inline int decode012(GetBitContext *gb){
876
return get_bits1(gb) + 1;
879
#endif /* BITSTREAM_H */