lcd-m6sp.c: remove \r
[kugel-rb.git] / apps / codecs / lib / ffmpeg_bitstream.h
blobf0105a4afe98ee87eb0cf01e8f78b1e3f9b17486
1 /*
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
21 #ifndef BITSTREAM_H
22 #define BITSTREAM_H
24 #include <inttypes.h>
25 #include <stdlib.h>
26 //#include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include "ffmpeg_bswap.h"
31 /* The following 2 defines are taken from libavutil/intreadwrite.h */
32 #define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
33 (((const uint8_t*)(x))[1] << 16) | \
34 (((const uint8_t*)(x))[2] << 8) | \
35 ((const uint8_t*)(x))[3])
36 #define AV_WB32(p, d) do { \
37 ((uint8_t*)(p))[3] = (d); \
38 ((uint8_t*)(p))[2] = (d)>>8; \
39 ((uint8_t*)(p))[1] = (d)>>16; \
40 ((uint8_t*)(p))[0] = (d)>>24; } while(0)
42 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
43 # define ALT_BITSTREAM_READER
44 #endif
46 //#define ALT_BITSTREAM_WRITER
47 //#define ALIGNED_BITSTREAM_WRITER
48 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
49 # if defined(ARCH_ARM)
50 # define A32_BITSTREAM_READER
51 # else
52 # define ALT_BITSTREAM_READER
53 //#define LIBMPEG2_BITSTREAM_READER
54 //#define A32_BITSTREAM_READER
55 # endif
56 #endif
58 extern const uint8_t ff_reverse[256];
60 #if defined(ARCH_X86)
61 // avoid +32 for shift optimization (gcc should do that ...)
62 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
63 __asm__ ("sarl %1, %0\n\t"
64 : "+r" (a)
65 : "ic" ((uint8_t)(-s))
67 return a;
69 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
70 __asm__ ("shrl %1, %0\n\t"
71 : "+r" (a)
72 : "ic" ((uint8_t)(-s))
74 return a;
76 #else
77 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
78 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
79 #endif
81 /* bit output */
83 /* buf and buf_end must be present and used by every alternative writer. */
84 typedef struct PutBitContext {
85 #ifdef ALT_BITSTREAM_WRITER
86 uint8_t *buf, *buf_end;
87 int index;
88 #else
89 uint32_t bit_buf;
90 int bit_left;
91 uint8_t *buf, *buf_ptr, *buf_end;
92 #endif
93 int size_in_bits;
94 } PutBitContext;
96 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
98 if(buffer_size < 0) {
99 buffer_size = 0;
100 buffer = NULL;
103 s->size_in_bits= 8*buffer_size;
104 s->buf = buffer;
105 s->buf_end = s->buf + buffer_size;
106 #ifdef ALT_BITSTREAM_WRITER
107 s->index=0;
108 ((uint32_t*)(s->buf))[0]=0;
109 // memset(buffer, 0, buffer_size);
110 #else
111 s->buf_ptr = s->buf;
112 s->bit_left=32;
113 s->bit_buf=0;
114 #endif
117 /* return the number of bits output */
118 static inline int put_bits_count(PutBitContext *s)
120 #ifdef ALT_BITSTREAM_WRITER
121 return s->index;
122 #else
123 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
124 #endif
127 /* pad the end of the output stream with zeros */
128 static inline void flush_put_bits(PutBitContext *s)
130 #ifdef ALT_BITSTREAM_WRITER
131 align_put_bits(s);
132 #else
133 #ifndef BITSTREAM_WRITER_LE
134 s->bit_buf<<= s->bit_left;
135 #endif
136 while (s->bit_left < 32) {
137 /* XXX: should test end of buffer */
138 #ifdef BITSTREAM_WRITER_LE
139 *s->buf_ptr++=s->bit_buf;
140 s->bit_buf>>=8;
141 #else
142 *s->buf_ptr++=s->bit_buf >> 24;
143 s->bit_buf<<=8;
144 #endif
145 s->bit_left+=8;
147 s->bit_left=32;
148 s->bit_buf=0;
149 #endif
152 void align_put_bits(PutBitContext *s);
153 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero);
154 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
156 /* bit input */
157 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
158 typedef struct GetBitContext {
159 const uint8_t *buffer, *buffer_end;
160 #ifdef ALT_BITSTREAM_READER
161 int index;
162 #elif defined LIBMPEG2_BITSTREAM_READER
163 uint8_t *buffer_ptr;
164 uint32_t cache;
165 int bit_count;
166 #elif defined A32_BITSTREAM_READER
167 uint32_t *buffer_ptr;
168 uint32_t cache0;
169 uint32_t cache1;
170 int bit_count;
171 #endif
172 int size_in_bits;
173 } GetBitContext;
175 #define VLC_TYPE int16_t
177 typedef struct VLC {
178 int bits;
179 VLC_TYPE (*table)[2]; ///< code, bits
180 int table_size, table_allocated;
181 } VLC;
183 typedef struct RL_VLC_ELEM {
184 int16_t level;
185 int8_t len;
186 uint8_t run;
187 } RL_VLC_ELEM;
189 #ifndef ALT_BITSTREAM_WRITER
190 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
192 unsigned int bit_buf;
193 int bit_left;
195 // printf("put_bits=%d %x\n", n, value);
196 //assert(n == 32 || value < (1U << n));
198 bit_buf = s->bit_buf;
199 bit_left = s->bit_left;
201 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
202 /* XXX: optimize */
203 #ifdef BITSTREAM_WRITER_LE
204 bit_buf |= value << (32 - bit_left);
205 if (n >= bit_left) {
206 #if !HAVE_FAST_UNALIGNED
207 if (3 & (intptr_t) s->buf_ptr) {
208 AV_WL32(s->buf_ptr, bit_buf);
209 } else
210 #endif
211 *(uint32_t *)s->buf_ptr = le2me_32(bit_buf);
212 s->buf_ptr+=4;
213 bit_buf = (bit_left==32)?0:value >> bit_left;
214 bit_left+=32;
216 bit_left-=n;
217 #else
218 if (n < bit_left) {
219 bit_buf = (bit_buf<<n) | value;
220 bit_left-=n;
221 } else {
222 bit_buf<<=bit_left;
223 bit_buf |= value >> (n - bit_left);
224 #if !defined(HAVE_FAST_UNALIGNED)
225 if (3 & (intptr_t) s->buf_ptr) {
226 AV_WB32(s->buf_ptr, bit_buf);
227 } else
228 #endif
229 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
230 //printf("bitbuf = %08x\n", bit_buf);
231 s->buf_ptr+=4;
232 bit_left+=32 - n;
233 bit_buf = value;
235 #endif
237 s->bit_buf = bit_buf;
238 s->bit_left = bit_left;
240 #endif
243 #ifdef ALT_BITSTREAM_WRITER
244 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
246 # ifdef ALIGNED_BITSTREAM_WRITER
247 # if ARCH_X86
248 __asm__ volatile(
249 "movl %0, %%ecx \n\t"
250 "xorl %%eax, %%eax \n\t"
251 "shrdl %%cl, %1, %%eax \n\t"
252 "shrl %%cl, %1 \n\t"
253 "movl %0, %%ecx \n\t"
254 "shrl $3, %%ecx \n\t"
255 "andl $0xFFFFFFFC, %%ecx \n\t"
256 "bswapl %1 \n\t"
257 "orl %1, (%2, %%ecx) \n\t"
258 "bswapl %%eax \n\t"
259 "addl %3, %0 \n\t"
260 "movl %%eax, 4(%2, %%ecx) \n\t"
261 : "=&r" (s->index), "=&r" (value)
262 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
263 : "%eax", "%ecx"
265 # else
266 int index= s->index;
267 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
269 value<<= 32-n;
271 ptr[0] |= be2me_32(value>>(index&31));
272 ptr[1] = be2me_32(value<<(32-(index&31)));
273 //if(n>24) printf("%d %d\n", n, value);
274 index+= n;
275 s->index= index;
276 # endif
277 # else //ALIGNED_BITSTREAM_WRITER
278 # if ARCH_X86
279 __asm__ volatile(
280 "movl $7, %%ecx \n\t"
281 "andl %0, %%ecx \n\t"
282 "addl %3, %%ecx \n\t"
283 "negl %%ecx \n\t"
284 "shll %%cl, %1 \n\t"
285 "bswapl %1 \n\t"
286 "movl %0, %%ecx \n\t"
287 "shrl $3, %%ecx \n\t"
288 "orl %1, (%%ecx, %2) \n\t"
289 "addl %3, %0 \n\t"
290 "movl $0, 4(%%ecx, %2) \n\t"
291 : "=&r" (s->index), "=&r" (value)
292 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
293 : "%ecx"
295 # else
296 int index= s->index;
297 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
299 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
300 ptr[1] = 0;
301 //if(n>24) printf("%d %d\n", n, value);
302 index+= n;
303 s->index= index;
304 # endif
305 # endif //!ALIGNED_BITSTREAM_WRITER
307 #endif
309 static inline void put_sbits(PutBitContext *pb, int bits, int32_t val)
311 //assert(bits >= 0 && bits <= 31);
313 put_bits(pb, bits, val & ((1<<bits)-1));
317 static inline uint8_t* pbBufPtr(PutBitContext *s)
319 #ifdef ALT_BITSTREAM_WRITER
320 return s->buf + (s->index>>3);
321 #else
322 return s->buf_ptr;
323 #endif
328 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
330 static inline void skip_put_bytes(PutBitContext *s, int n){
331 //assert((put_bits_count(s)&7)==0);
332 #ifdef ALT_BITSTREAM_WRITER
333 FIXME may need some cleaning of the buffer
334 s->index += n<<3;
335 #else
336 //assert(s->bit_left==32);
337 s->buf_ptr += n;
338 #endif
342 * Skips the given number of bits.
343 * Must only be used if the actual values in the bitstream do not matter.
345 static inline void skip_put_bits(PutBitContext *s, int n){
346 #ifdef ALT_BITSTREAM_WRITER
347 s->index += n;
348 #else
349 s->bit_left -= n;
350 s->buf_ptr-= s->bit_left>>5;
351 s->bit_left &= 31;
352 #endif
356 * Changes the end of the buffer.
358 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
359 s->buf_end= s->buf + size;
362 /* Bitstream reader API docs:
363 name
364 arbitrary name which is used as prefix for the internal variables
367 getbitcontext
369 OPEN_READER(name, gb)
370 loads gb into local variables
372 CLOSE_READER(name, gb)
373 stores local vars in gb
375 UPDATE_CACHE(name, gb)
376 refills the internal cache from the bitstream
377 after this call at least MIN_CACHE_BITS will be available,
379 GET_CACHE(name, gb)
380 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
382 SHOW_UBITS(name, gb, num)
383 will return the next num bits
385 SHOW_SBITS(name, gb, num)
386 will return the next num bits and do sign extension
388 SKIP_BITS(name, gb, num)
389 will skip over the next num bits
390 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
392 SKIP_CACHE(name, gb, num)
393 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
395 SKIP_COUNTER(name, gb, num)
396 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
398 LAST_SKIP_CACHE(name, gb, num)
399 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
401 LAST_SKIP_BITS(name, gb, num)
402 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
404 for examples see get_bits, show_bits, skip_bits, get_vlc
407 #ifdef ALT_BITSTREAM_READER
408 # define MIN_CACHE_BITS 25
410 # define OPEN_READER(name, gb)\
411 int name##_index= (gb)->index;\
412 int name##_cache= 0;\
414 # define CLOSE_READER(name, gb)\
415 (gb)->index= name##_index;\
417 # ifdef ALT_BITSTREAM_READER_LE
418 # define UPDATE_CACHE(name, gb)\
419 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
421 # define SKIP_CACHE(name, gb, num)\
422 name##_cache >>= (num);
423 # else
424 # define UPDATE_CACHE(name, gb)\
425 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
427 # define SKIP_CACHE(name, gb, num)\
428 name##_cache <<= (num);
429 # endif
431 // FIXME name?
432 # define SKIP_COUNTER(name, gb, num)\
433 name##_index += (num);\
435 # define SKIP_BITS(name, gb, num)\
437 SKIP_CACHE(name, gb, num)\
438 SKIP_COUNTER(name, gb, num)\
441 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
442 # define LAST_SKIP_CACHE(name, gb, num) ;
444 # ifdef ALT_BITSTREAM_READER_LE
445 # define SHOW_UBITS(name, gb, num)\
446 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
448 # define SHOW_SBITS(name, gb, num)\
449 NEG_SSR32((name##_cache)<<(32-(num)), num)
450 # else
451 # define SHOW_UBITS(name, gb, num)\
452 NEG_USR32(name##_cache, num)
454 # define SHOW_SBITS(name, gb, num)\
455 NEG_SSR32(name##_cache, num)
456 # endif
458 # define GET_CACHE(name, gb)\
459 ((uint32_t)name##_cache)
461 static inline int get_bits_count(GetBitContext *s){
462 return s->index;
465 static inline void skip_bits_long(GetBitContext *s, int n){
466 s->index += n;
469 #elif defined LIBMPEG2_BITSTREAM_READER
470 //libmpeg2 like reader
472 # define MIN_CACHE_BITS 17
474 # define OPEN_READER(name, gb)\
475 int name##_bit_count=(gb)->bit_count;\
476 int name##_cache= (gb)->cache;\
477 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
479 # define CLOSE_READER(name, gb)\
480 (gb)->bit_count= name##_bit_count;\
481 (gb)->cache= name##_cache;\
482 (gb)->buffer_ptr= name##_buffer_ptr;\
484 # define UPDATE_CACHE(name, gb)\
485 if(name##_bit_count >= 0){\
486 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
487 name##_buffer_ptr+=2;\
488 name##_bit_count-= 16;\
491 # define SKIP_CACHE(name, gb, num)\
492 name##_cache <<= (num);\
494 # define SKIP_COUNTER(name, gb, num)\
495 name##_bit_count += (num);\
497 # define SKIP_BITS(name, gb, num)\
499 SKIP_CACHE(name, gb, num)\
500 SKIP_COUNTER(name, gb, num)\
503 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
504 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
506 # define SHOW_UBITS(name, gb, num)\
507 NEG_USR32(name##_cache, num)
509 # define SHOW_SBITS(name, gb, num)\
510 NEG_SSR32(name##_cache, num)
512 # define GET_CACHE(name, gb)\
513 ((uint32_t)name##_cache)
515 static inline int get_bits_count(GetBitContext *s){
516 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
519 static inline void skip_bits_long(GetBitContext *s, int n){
520 OPEN_READER(re, s)
521 re_bit_count += n;
522 re_buffer_ptr += 2*(re_bit_count>>4);
523 re_bit_count &= 15;
524 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
525 UPDATE_CACHE(re, s)
526 CLOSE_READER(re, s)
529 #elif defined A32_BITSTREAM_READER
531 # define MIN_CACHE_BITS 32
533 # define OPEN_READER(name, gb)\
534 int name##_bit_count=(gb)->bit_count;\
535 uint32_t name##_cache0= (gb)->cache0;\
536 uint32_t name##_cache1= (gb)->cache1;\
537 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
539 # define CLOSE_READER(name, gb)\
540 (gb)->bit_count= name##_bit_count;\
541 (gb)->cache0= name##_cache0;\
542 (gb)->cache1= name##_cache1;\
543 (gb)->buffer_ptr= name##_buffer_ptr;\
545 # define UPDATE_CACHE(name, gb)\
546 if(name##_bit_count > 0){\
547 const uint32_t next= be2me_32( *name##_buffer_ptr );\
548 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
549 name##_cache1 |= next<<name##_bit_count;\
550 name##_buffer_ptr++;\
551 name##_bit_count-= 32;\
554 #if ARCH_X86
555 # define SKIP_CACHE(name, gb, num)\
556 __asm__(\
557 "shldl %2, %1, %0 \n\t"\
558 "shll %2, %1 \n\t"\
559 : "+r" (name##_cache0), "+r" (name##_cache1)\
560 : "Ic" ((uint8_t)(num))\
562 #else
563 # define SKIP_CACHE(name, gb, num)\
564 name##_cache0 <<= (num);\
565 name##_cache0 |= NEG_USR32(name##_cache1,num);\
566 name##_cache1 <<= (num);
567 #endif
569 # define SKIP_COUNTER(name, gb, num)\
570 name##_bit_count += (num);\
572 # define SKIP_BITS(name, gb, num)\
574 SKIP_CACHE(name, gb, num)\
575 SKIP_COUNTER(name, gb, num)\
578 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
579 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
581 # define SHOW_UBITS(name, gb, num)\
582 NEG_USR32(name##_cache0, num)
584 # define SHOW_SBITS(name, gb, num)\
585 NEG_SSR32(name##_cache0, num)
587 # define GET_CACHE(name, gb)\
588 (name##_cache0)
590 static inline int get_bits_count(GetBitContext *s){
591 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
594 static inline void skip_bits_long(GetBitContext *s, int n){
595 OPEN_READER(re, s)
596 re_bit_count += n;
597 re_buffer_ptr += re_bit_count>>5;
598 re_bit_count &= 31;
599 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
600 re_cache1 = 0;
601 UPDATE_CACHE(re, s)
602 CLOSE_READER(re, s)
605 #endif
608 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
609 * if MSB not set it is negative
610 * @param n length in bits
611 * @author BERO
613 static inline int get_xbits(GetBitContext *s, int n){
614 register int sign;
615 register int32_t cache;
616 OPEN_READER(re, s)
617 UPDATE_CACHE(re, s)
618 cache = GET_CACHE(re,s);
619 sign=(~cache)>>31;
620 LAST_SKIP_BITS(re, s, n)
621 CLOSE_READER(re, s)
622 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
625 static inline int get_sbits(GetBitContext *s, int n){
626 register int tmp;
627 OPEN_READER(re, s)
628 UPDATE_CACHE(re, s)
629 tmp= SHOW_SBITS(re, s, n);
630 LAST_SKIP_BITS(re, s, n)
631 CLOSE_READER(re, s)
632 return tmp;
636 * reads 1-17 bits.
637 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
639 static inline unsigned int get_bits(GetBitContext *s, int n){
640 register int tmp;
641 OPEN_READER(re, s)
642 UPDATE_CACHE(re, s)
643 tmp= SHOW_UBITS(re, s, n);
644 LAST_SKIP_BITS(re, s, n)
645 CLOSE_READER(re, s)
646 return tmp;
650 * shows 1-17 bits.
651 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
653 static inline unsigned int show_bits(GetBitContext *s, int n){
654 register int tmp;
655 OPEN_READER(re, s)
656 UPDATE_CACHE(re, s)
657 tmp= SHOW_UBITS(re, s, n);
658 // CLOSE_READER(re, s)
659 return tmp;
662 static inline void skip_bits(GetBitContext *s, int n){
663 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
664 OPEN_READER(re, s)
665 UPDATE_CACHE(re, s)
666 LAST_SKIP_BITS(re, s, n)
667 CLOSE_READER(re, s)
670 static inline unsigned int get_bits1(GetBitContext *s){
671 #ifdef ALT_BITSTREAM_READER
672 int index= s->index;
673 uint8_t result= s->buffer[ index>>3 ];
674 #ifdef ALT_BITSTREAM_READER_LE
675 result>>= (index&0x07);
676 result&= 1;
677 #else
678 result<<= (index&0x07);
679 result>>= 8 - 1;
680 #endif
681 index++;
682 s->index= index;
684 return result;
685 #else
686 return get_bits(s, 1);
687 #endif
690 static inline unsigned int show_bits1(GetBitContext *s){
691 return show_bits(s, 1);
694 static inline void skip_bits1(GetBitContext *s){
695 skip_bits(s, 1);
699 * reads 0-32 bits.
701 static inline unsigned int get_bits_long(GetBitContext *s, int n){
702 if(n<=17) return get_bits(s, n);
703 else{
704 #ifdef ALT_BITSTREAM_READER_LE
705 int ret= get_bits(s, 16);
706 return ret | (get_bits(s, n-16) << 16);
707 #else
708 int ret= get_bits(s, 16) << (n-16);
709 return ret | get_bits(s, n-16);
710 #endif
714 #if 0
716 * reads 0-32 bits as a signed integer.
718 static inline int get_sbits_long(GetBitContext *s, int n) {
719 return sign_extend(get_bits_long(s, n), n);
721 #endif
724 * shows 0-32 bits.
726 static inline unsigned int show_bits_long(GetBitContext *s, int n){
727 if(n<=17) return show_bits(s, n);
728 else{
729 GetBitContext gb= *s;
730 return get_bits_long(&gb, n);
734 #if 0
735 static inline int check_marker(GetBitContext *s, const char *msg)
737 int bit= get_bits1(s);
738 if(!bit)
739 printf("Marker bit missing %s\n", msg);
741 return bit;
743 #endif
746 * init GetBitContext.
747 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
748 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
749 * @param bit_size the size of the buffer in bits
751 static inline void init_get_bits(GetBitContext *s,
752 const uint8_t *buffer, int bit_size)
754 int buffer_size= (bit_size+7)>>3;
755 if(buffer_size < 0 || bit_size < 0) {
756 buffer_size = bit_size = 0;
757 buffer = NULL;
760 s->buffer= buffer;
761 s->size_in_bits= bit_size;
762 s->buffer_end= buffer + buffer_size;
763 #ifdef ALT_BITSTREAM_READER
764 s->index=0;
765 #elif defined LIBMPEG2_BITSTREAM_READER
766 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
767 s->bit_count = 16 + 8*((intptr_t)buffer&1);
768 skip_bits_long(s, 0);
769 #elif defined A32_BITSTREAM_READER
770 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
771 s->bit_count = 32 + 8*((intptr_t)buffer&3);
772 skip_bits_long(s, 0);
773 #endif
776 static inline void align_get_bits(GetBitContext *s)
778 int n= (-get_bits_count(s)) & 7;
779 if(n) skip_bits(s, n);
782 #define init_vlc(vlc, nb_bits, nb_codes,\
783 bits, bits_wrap, bits_size,\
784 codes, codes_wrap, codes_size,\
785 flags)\
786 init_vlc_sparse(vlc, nb_bits, nb_codes,\
787 bits, bits_wrap, bits_size,\
788 codes, codes_wrap, codes_size,\
789 NULL, 0, 0, flags)
791 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
792 const void *bits, int bits_wrap, int bits_size,
793 const void *codes, int codes_wrap, int codes_size,
794 const void *symbols, int symbols_wrap, int symbols_size,
795 int flags);
796 #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
797 #define INIT_VLC_LE 2
798 #define INIT_VLC_USE_NEW_STATIC 4
799 void free_vlc(VLC *vlc);
801 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
803 static VLC_TYPE table[static_size][2];\
804 (vlc)->table= table;\
805 (vlc)->table_allocated= static_size;\
806 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
812 * if the vlc code is invalid and max_depth=1 than no bits will be removed
813 * if the vlc code is invalid and max_depth>1 than the number of bits removed
814 * is undefined
816 #define GET_VLC(code, name, gb, table, bits, max_depth)\
818 int n, index, nb_bits;\
820 index= SHOW_UBITS(name, gb, bits);\
821 code = table[index][0];\
822 n = table[index][1];\
824 if(max_depth > 1 && n < 0){\
825 LAST_SKIP_BITS(name, gb, bits)\
826 UPDATE_CACHE(name, gb)\
828 nb_bits = -n;\
830 index= SHOW_UBITS(name, gb, nb_bits) + code;\
831 code = table[index][0];\
832 n = table[index][1];\
833 if(max_depth > 2 && n < 0){\
834 LAST_SKIP_BITS(name, gb, nb_bits)\
835 UPDATE_CACHE(name, gb)\
837 nb_bits = -n;\
839 index= SHOW_UBITS(name, gb, nb_bits) + code;\
840 code = table[index][0];\
841 n = table[index][1];\
844 SKIP_BITS(name, gb, n)\
847 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
849 int n, index, nb_bits;\
851 index= SHOW_UBITS(name, gb, bits);\
852 level = table[index].level;\
853 n = table[index].len;\
855 if(max_depth > 1 && n < 0){\
856 SKIP_BITS(name, gb, bits)\
857 if(need_update){\
858 UPDATE_CACHE(name, gb)\
861 nb_bits = -n;\
863 index= SHOW_UBITS(name, gb, nb_bits) + level;\
864 level = table[index].level;\
865 n = table[index].len;\
867 run= table[index].run;\
868 SKIP_BITS(name, gb, n)\
873 * parses a vlc code, faster then get_vlc()
874 * @param bits is the number of bits which will be read at once, must be
875 * identical to nb_bits in init_vlc()
876 * @param max_depth is the number of times bits bits must be read to completely
877 * read the longest vlc code
878 * = (max_vlc_length + bits - 1) / bits
880 static inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
881 int bits, int max_depth)
883 int code;
885 OPEN_READER(re, s)
886 UPDATE_CACHE(re, s)
888 GET_VLC(code, re, s, table, bits, max_depth)
890 CLOSE_READER(re, s)
891 return code;
894 //#define TRACE
896 #ifdef TRACE
897 static inline void print_bin(int bits, int n){
898 int i;
900 for(i=n-1; i>=0; i--){
901 printf("%d", (bits>>i)&1);
903 for(i=n; i<24; i++)
904 printf(" ");
907 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
908 int r= get_bits(s, n);
910 print_bin(r, n);
911 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
912 return r;
914 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
915 int show= show_bits(s, 24);
916 int pos= get_bits_count(s);
917 int r= get_vlc2(s, table, bits, max_depth);
918 int len= get_bits_count(s) - pos;
919 int bits2= show>>(24-len);
921 print_bin(bits2, len);
923 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
924 return r;
926 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
927 int show= show_bits(s, n);
928 int r= get_xbits(s, n);
930 print_bin(show, n);
931 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
932 return r;
935 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
936 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
937 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
938 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
939 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
941 #define tprintf(p, ...) printf
943 #else //TRACE
944 #define tprintf(p, ...) {}
945 #endif
947 static inline int decode012(GetBitContext *gb){
948 int n;
949 n = get_bits1(gb);
950 if (n == 0)
951 return 0;
952 else
953 return get_bits1(gb) + 1;
956 static inline int decode210(GetBitContext *gb){
957 if (get_bits1(gb))
958 return 0;
959 else
960 return 2 - get_bits1(gb);
963 #endif /* BITSTREAM_H */