summaryrefslogtreecommitdiff
path: root/apps/codecs/libcook/bitstream.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libcook/bitstream.h')
-rw-r--r--apps/codecs/libcook/bitstream.h955
1 files changed, 955 insertions, 0 deletions
diff --git a/apps/codecs/libcook/bitstream.h b/apps/codecs/libcook/bitstream.h
new file mode 100644
index 0000000000..3670285904
--- /dev/null
+++ b/apps/codecs/libcook/bitstream.h
@@ -0,0 +1,955 @@
1/*
2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21/**
22 * @file libavcodec/bitstream.h
23 * bitstream api header.
24 */
25
26#ifndef AVCODEC_BITSTREAM_H
27#define AVCODEC_BITSTREAM_H
28
29#include <stdint.h>
30#include <stdlib.h>
31#include <assert.h>
32#include "libavutil/bswap.h"
33#include "libavutil/common.h"
34#include "libavutil/intreadwrite.h"
35#include "libavutil/log.h"
36#include "mathops.h"
37
38#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
39# define ALT_BITSTREAM_READER
40#endif
41
42//#define ALT_BITSTREAM_WRITER
43//#define ALIGNED_BITSTREAM_WRITER
44#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
45# if ARCH_ARM
46# define A32_BITSTREAM_READER
47# else
48# define ALT_BITSTREAM_READER
49//#define LIBMPEG2_BITSTREAM_READER
50//#define A32_BITSTREAM_READER
51# endif
52#endif
53
54extern const uint8_t ff_reverse[256];
55
56#if ARCH_X86
57// avoid +32 for shift optimization (gcc should do that ...)
58static inline int32_t NEG_SSR32( int32_t a, int8_t s){
59 __asm__ ("sarl %1, %0\n\t"
60 : "+r" (a)
61 : "ic" ((uint8_t)(-s))
62 );
63 return a;
64}
65static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
66 __asm__ ("shrl %1, %0\n\t"
67 : "+r" (a)
68 : "ic" ((uint8_t)(-s))
69 );
70 return a;
71}
72#else
73# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
74# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
75#endif
76
77/* bit output */
78
79/* buf and buf_end must be present and used by every alternative writer. */
80typedef struct PutBitContext {
81#ifdef ALT_BITSTREAM_WRITER
82 uint8_t *buf, *buf_end;
83 int index;
84#else
85 uint32_t bit_buf;
86 int bit_left;
87 uint8_t *buf, *buf_ptr, *buf_end;
88#endif
89 int size_in_bits;
90} PutBitContext;
91
92static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
93{
94 if(buffer_size < 0) {
95 buffer_size = 0;
96 buffer = NULL;
97 }
98
99 s->size_in_bits= 8*buffer_size;
100 s->buf = buffer;
101 s->buf_end = s->buf + buffer_size;
102#ifdef ALT_BITSTREAM_WRITER
103 s->index=0;
104 ((uint32_t*)(s->buf))[0]=0;
105// memset(buffer, 0, buffer_size);
106#else
107 s->buf_ptr = s->buf;
108 s->bit_left=32;
109 s->bit_buf=0;
110#endif
111}
112
113/* return the number of bits output */
114static inline int put_bits_count(PutBitContext *s)
115{
116#ifdef ALT_BITSTREAM_WRITER
117 return s->index;
118#else
119 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
120#endif
121}
122
123/* pad the end of the output stream with zeros */
124static inline void flush_put_bits(PutBitContext *s)
125{
126#ifdef ALT_BITSTREAM_WRITER
127 align_put_bits(s);
128#else
129#ifndef BITSTREAM_WRITER_LE
130 s->bit_buf<<= s->bit_left;
131#endif
132 while (s->bit_left < 32) {
133 /* XXX: should test end of buffer */
134#ifdef BITSTREAM_WRITER_LE
135 *s->buf_ptr++=s->bit_buf;
136 s->bit_buf>>=8;
137#else
138 *s->buf_ptr++=s->bit_buf >> 24;
139 s->bit_buf<<=8;
140#endif
141 s->bit_left+=8;
142 }
143 s->bit_left=32;
144 s->bit_buf=0;
145#endif
146}
147
148void align_put_bits(PutBitContext *s);
149void ff_put_string(PutBitContext * pbc, const char *s, int put_zero);
150void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
151
152/* bit input */
153/* buffer, buffer_end and size_in_bits must be present and used by every reader */
154typedef struct GetBitContext {
155 const uint8_t *buffer, *buffer_end;
156#ifdef ALT_BITSTREAM_READER
157 int index;
158#elif defined LIBMPEG2_BITSTREAM_READER
159 uint8_t *buffer_ptr;
160 uint32_t cache;
161 int bit_count;
162#elif defined A32_BITSTREAM_READER
163 uint32_t *buffer_ptr;
164 uint32_t cache0;
165 uint32_t cache1;
166 int bit_count;
167#endif
168 int size_in_bits;
169} GetBitContext;
170
171#define VLC_TYPE int16_t
172
173typedef struct VLC {
174 int bits;
175 VLC_TYPE (*table)[2]; ///< code, bits
176 int table_size, table_allocated;
177} VLC;
178
179typedef struct RL_VLC_ELEM {
180 int16_t level;
181 int8_t len;
182 uint8_t run;
183} RL_VLC_ELEM;
184
185#ifndef ALT_BITSTREAM_WRITER
186static inline void put_bits(PutBitContext *s, int n, unsigned int value)
187{
188 unsigned int bit_buf;
189 int bit_left;
190
191 // printf("put_bits=%d %x\n", n, value);
192 assert(n == 32 || value < (1U << n));
193
194 bit_buf = s->bit_buf;
195 bit_left = s->bit_left;
196
197 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
198 /* XXX: optimize */
199#ifdef BITSTREAM_WRITER_LE
200 bit_buf |= value << (32 - bit_left);
201 if (n >= bit_left) {
202#if !HAVE_FAST_UNALIGNED
203 if (3 & (intptr_t) s->buf_ptr) {
204 AV_WL32(s->buf_ptr, bit_buf);
205 } else
206#endif
207 *(uint32_t *)s->buf_ptr = le2me_32(bit_buf);
208 s->buf_ptr+=4;
209 bit_buf = (bit_left==32)?0:value >> bit_left;
210 bit_left+=32;
211 }
212 bit_left-=n;
213#else
214 if (n < bit_left) {
215 bit_buf = (bit_buf<<n) | value;
216 bit_left-=n;
217 } else {
218 bit_buf<<=bit_left;
219 bit_buf |= value >> (n - bit_left);
220#if !HAVE_FAST_UNALIGNED
221 if (3 & (intptr_t) s->buf_ptr) {
222 AV_WB32(s->buf_ptr, bit_buf);
223 } else
224#endif
225 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
226 //printf("bitbuf = %08x\n", bit_buf);
227 s->buf_ptr+=4;
228 bit_left+=32 - n;
229 bit_buf = value;
230 }
231#endif
232
233 s->bit_buf = bit_buf;
234 s->bit_left = bit_left;
235}
236#endif
237
238
239#ifdef ALT_BITSTREAM_WRITER
240static inline void put_bits(PutBitContext *s, int n, unsigned int value)
241{
242# ifdef ALIGNED_BITSTREAM_WRITER
243# if ARCH_X86
244 __asm__ volatile(
245 "movl %0, %%ecx \n\t"
246 "xorl %%eax, %%eax \n\t"
247 "shrdl %%cl, %1, %%eax \n\t"
248 "shrl %%cl, %1 \n\t"
249 "movl %0, %%ecx \n\t"
250 "shrl $3, %%ecx \n\t"
251 "andl $0xFFFFFFFC, %%ecx \n\t"
252 "bswapl %1 \n\t"
253 "orl %1, (%2, %%ecx) \n\t"
254 "bswapl %%eax \n\t"
255 "addl %3, %0 \n\t"
256 "movl %%eax, 4(%2, %%ecx) \n\t"
257 : "=&r" (s->index), "=&r" (value)
258 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
259 : "%eax", "%ecx"
260 );
261# else
262 int index= s->index;
263 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
264
265 value<<= 32-n;
266
267 ptr[0] |= be2me_32(value>>(index&31));
268 ptr[1] = be2me_32(value<<(32-(index&31)));
269//if(n>24) printf("%d %d\n", n, value);
270 index+= n;
271 s->index= index;
272# endif
273# else //ALIGNED_BITSTREAM_WRITER
274# if ARCH_X86
275 __asm__ volatile(
276 "movl $7, %%ecx \n\t"
277 "andl %0, %%ecx \n\t"
278 "addl %3, %%ecx \n\t"
279 "negl %%ecx \n\t"
280 "shll %%cl, %1 \n\t"
281 "bswapl %1 \n\t"
282 "movl %0, %%ecx \n\t"
283 "shrl $3, %%ecx \n\t"
284 "orl %1, (%%ecx, %2) \n\t"
285 "addl %3, %0 \n\t"
286 "movl $0, 4(%%ecx, %2) \n\t"
287 : "=&r" (s->index), "=&r" (value)
288 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
289 : "%ecx"
290 );
291# else
292 int index= s->index;
293 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
294
295 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
296 ptr[1] = 0;
297//if(n>24) printf("%d %d\n", n, value);
298 index+= n;
299 s->index= index;
300# endif
301# endif //!ALIGNED_BITSTREAM_WRITER
302}
303#endif
304
305static inline void put_sbits(PutBitContext *pb, int bits, int32_t val)
306{
307 assert(bits >= 0 && bits <= 31);
308
309 put_bits(pb, bits, val & ((1<<bits)-1));
310}
311
312
313static inline uint8_t* pbBufPtr(PutBitContext *s)
314{
315#ifdef ALT_BITSTREAM_WRITER
316 return s->buf + (s->index>>3);
317#else
318 return s->buf_ptr;
319#endif
320}
321
322/**
323 *
324 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
325 */
326static inline void skip_put_bytes(PutBitContext *s, int n){
327 assert((put_bits_count(s)&7)==0);
328#ifdef ALT_BITSTREAM_WRITER
329 FIXME may need some cleaning of the buffer
330 s->index += n<<3;
331#else
332 assert(s->bit_left==32);
333 s->buf_ptr += n;
334#endif
335}
336
337/**
338 * Skips the given number of bits.
339 * Must only be used if the actual values in the bitstream do not matter.
340 */
341static inline void skip_put_bits(PutBitContext *s, int n){
342#ifdef ALT_BITSTREAM_WRITER
343 s->index += n;
344#else
345 s->bit_left -= n;
346 s->buf_ptr-= s->bit_left>>5;
347 s->bit_left &= 31;
348#endif
349}
350
351/**
352 * Changes the end of the buffer.
353 */
354static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
355 s->buf_end= s->buf + size;
356}
357
358/* Bitstream reader API docs:
359name
360 arbitrary name which is used as prefix for the internal variables
361
362gb
363 getbitcontext
364
365OPEN_READER(name, gb)
366 loads gb into local variables
367
368CLOSE_READER(name, gb)
369 stores local vars in gb
370
371UPDATE_CACHE(name, gb)
372 refills the internal cache from the bitstream
373 after this call at least MIN_CACHE_BITS will be available,
374
375GET_CACHE(name, gb)
376 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
377
378SHOW_UBITS(name, gb, num)
379 will return the next num bits
380
381SHOW_SBITS(name, gb, num)
382 will return the next num bits and do sign extension
383
384SKIP_BITS(name, gb, num)
385 will skip over the next num bits
386 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
387
388SKIP_CACHE(name, gb, num)
389 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
390
391SKIP_COUNTER(name, gb, num)
392 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
393
394LAST_SKIP_CACHE(name, gb, num)
395 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
396
397LAST_SKIP_BITS(name, gb, num)
398 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
399
400for examples see get_bits, show_bits, skip_bits, get_vlc
401*/
402
403#ifdef ALT_BITSTREAM_READER
404# define MIN_CACHE_BITS 25
405
406# define OPEN_READER(name, gb)\
407 int name##_index= (gb)->index;\
408 int name##_cache= 0;\
409
410# define CLOSE_READER(name, gb)\
411 (gb)->index= name##_index;\
412
413# ifdef ALT_BITSTREAM_READER_LE
414# define UPDATE_CACHE(name, gb)\
415 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
416
417# define SKIP_CACHE(name, gb, num)\
418 name##_cache >>= (num);
419# else
420# define UPDATE_CACHE(name, gb)\
421 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
422
423# define SKIP_CACHE(name, gb, num)\
424 name##_cache <<= (num);
425# endif
426
427// FIXME name?
428# define SKIP_COUNTER(name, gb, num)\
429 name##_index += (num);\
430
431# define SKIP_BITS(name, gb, num)\
432 {\
433 SKIP_CACHE(name, gb, num)\
434 SKIP_COUNTER(name, gb, num)\
435 }\
436
437# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
438# define LAST_SKIP_CACHE(name, gb, num) ;
439
440# ifdef ALT_BITSTREAM_READER_LE
441# define SHOW_UBITS(name, gb, num)\
442 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
443
444# define SHOW_SBITS(name, gb, num)\
445 NEG_SSR32((name##_cache)<<(32-(num)), num)
446# else
447# define SHOW_UBITS(name, gb, num)\
448 NEG_USR32(name##_cache, num)
449
450# define SHOW_SBITS(name, gb, num)\
451 NEG_SSR32(name##_cache, num)
452# endif
453
454# define GET_CACHE(name, gb)\
455 ((uint32_t)name##_cache)
456
457static inline int get_bits_count(GetBitContext *s){
458 return s->index;
459}
460
461static inline void skip_bits_long(GetBitContext *s, int n){
462 s->index += n;
463}
464
465#elif defined LIBMPEG2_BITSTREAM_READER
466//libmpeg2 like reader
467
468# define MIN_CACHE_BITS 17
469
470# define OPEN_READER(name, gb)\
471 int name##_bit_count=(gb)->bit_count;\
472 int name##_cache= (gb)->cache;\
473 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
474
475# define CLOSE_READER(name, gb)\
476 (gb)->bit_count= name##_bit_count;\
477 (gb)->cache= name##_cache;\
478 (gb)->buffer_ptr= name##_buffer_ptr;\
479
480# define UPDATE_CACHE(name, gb)\
481 if(name##_bit_count >= 0){\
482 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
483 name##_buffer_ptr+=2;\
484 name##_bit_count-= 16;\
485 }\
486
487# define SKIP_CACHE(name, gb, num)\
488 name##_cache <<= (num);\
489
490# define SKIP_COUNTER(name, gb, num)\
491 name##_bit_count += (num);\
492
493# define SKIP_BITS(name, gb, num)\
494 {\
495 SKIP_CACHE(name, gb, num)\
496 SKIP_COUNTER(name, gb, num)\
497 }\
498
499# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
500# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
501
502# define SHOW_UBITS(name, gb, num)\
503 NEG_USR32(name##_cache, num)
504
505# define SHOW_SBITS(name, gb, num)\
506 NEG_SSR32(name##_cache, num)
507
508# define GET_CACHE(name, gb)\
509 ((uint32_t)name##_cache)
510
511static inline int get_bits_count(GetBitContext *s){
512 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
513}
514
515static inline void skip_bits_long(GetBitContext *s, int n){
516 OPEN_READER(re, s)
517 re_bit_count += n;
518 re_buffer_ptr += 2*(re_bit_count>>4);
519 re_bit_count &= 15;
520 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
521 UPDATE_CACHE(re, s)
522 CLOSE_READER(re, s)
523}
524
525#elif defined A32_BITSTREAM_READER
526
527# define MIN_CACHE_BITS 32
528
529# define OPEN_READER(name, gb)\
530 int name##_bit_count=(gb)->bit_count;\
531 uint32_t name##_cache0= (gb)->cache0;\
532 uint32_t name##_cache1= (gb)->cache1;\
533 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
534
535# define CLOSE_READER(name, gb)\
536 (gb)->bit_count= name##_bit_count;\
537 (gb)->cache0= name##_cache0;\
538 (gb)->cache1= name##_cache1;\
539 (gb)->buffer_ptr= name##_buffer_ptr;\
540
541# define UPDATE_CACHE(name, gb)\
542 if(name##_bit_count > 0){\
543 const uint32_t next= be2me_32( *name##_buffer_ptr );\
544 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
545 name##_cache1 |= next<<name##_bit_count;\
546 name##_buffer_ptr++;\
547 name##_bit_count-= 32;\
548 }\
549
550#if ARCH_X86
551# define SKIP_CACHE(name, gb, num)\
552 __asm__(\
553 "shldl %2, %1, %0 \n\t"\
554 "shll %2, %1 \n\t"\
555 : "+r" (name##_cache0), "+r" (name##_cache1)\
556 : "Ic" ((uint8_t)(num))\
557 );
558#else
559# define SKIP_CACHE(name, gb, num)\
560 name##_cache0 <<= (num);\
561 name##_cache0 |= NEG_USR32(name##_cache1,num);\
562 name##_cache1 <<= (num);
563#endif
564
565# define SKIP_COUNTER(name, gb, num)\
566 name##_bit_count += (num);\
567
568# define SKIP_BITS(name, gb, num)\
569 {\
570 SKIP_CACHE(name, gb, num)\
571 SKIP_COUNTER(name, gb, num)\
572 }\
573
574# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
575# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
576
577# define SHOW_UBITS(name, gb, num)\
578 NEG_USR32(name##_cache0, num)
579
580# define SHOW_SBITS(name, gb, num)\
581 NEG_SSR32(name##_cache0, num)
582
583# define GET_CACHE(name, gb)\
584 (name##_cache0)
585
586static inline int get_bits_count(GetBitContext *s){
587 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
588}
589
590static inline void skip_bits_long(GetBitContext *s, int n){
591 OPEN_READER(re, s)
592 re_bit_count += n;
593 re_buffer_ptr += re_bit_count>>5;
594 re_bit_count &= 31;
595 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
596 re_cache1 = 0;
597 UPDATE_CACHE(re, s)
598 CLOSE_READER(re, s)
599}
600
601#endif
602
603/**
604 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
605 * if MSB not set it is negative
606 * @param n length in bits
607 * @author BERO
608 */
609static inline int get_xbits(GetBitContext *s, int n){
610 register int sign;
611 register int32_t cache;
612 OPEN_READER(re, s)
613 UPDATE_CACHE(re, s)
614 cache = GET_CACHE(re,s);
615 sign=(~cache)>>31;
616 LAST_SKIP_BITS(re, s, n)
617 CLOSE_READER(re, s)
618 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
619}
620
621static inline int get_sbits(GetBitContext *s, int n){
622 register int tmp;
623 OPEN_READER(re, s)
624 UPDATE_CACHE(re, s)
625 tmp= SHOW_SBITS(re, s, n);
626 LAST_SKIP_BITS(re, s, n)
627 CLOSE_READER(re, s)
628 return tmp;
629}
630
631/**
632 * reads 1-17 bits.
633 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
634 */
635static inline unsigned int get_bits(GetBitContext *s, int n){
636 register int tmp;
637 OPEN_READER(re, s)
638 UPDATE_CACHE(re, s)
639 tmp= SHOW_UBITS(re, s, n);
640 LAST_SKIP_BITS(re, s, n)
641 CLOSE_READER(re, s)
642 return tmp;
643}
644
645/**
646 * shows 1-17 bits.
647 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
648 */
649static inline unsigned int show_bits(GetBitContext *s, int n){
650 register int tmp;
651 OPEN_READER(re, s)
652 UPDATE_CACHE(re, s)
653 tmp= SHOW_UBITS(re, s, n);
654// CLOSE_READER(re, s)
655 return tmp;
656}
657
658static inline void skip_bits(GetBitContext *s, int n){
659 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
660 OPEN_READER(re, s)
661 UPDATE_CACHE(re, s)
662 LAST_SKIP_BITS(re, s, n)
663 CLOSE_READER(re, s)
664}
665
666static inline unsigned int get_bits1(GetBitContext *s){
667#ifdef ALT_BITSTREAM_READER
668 int index= s->index;
669 uint8_t result= s->buffer[ index>>3 ];
670#ifdef ALT_BITSTREAM_READER_LE
671 result>>= (index&0x07);
672 result&= 1;
673#else
674 result<<= (index&0x07);
675 result>>= 8 - 1;
676#endif
677 index++;
678 s->index= index;
679
680 return result;
681#else
682 return get_bits(s, 1);
683#endif
684}
685
686static inline unsigned int show_bits1(GetBitContext *s){
687 return show_bits(s, 1);
688}
689
690static inline void skip_bits1(GetBitContext *s){
691 skip_bits(s, 1);
692}
693
694/**
695 * reads 0-32 bits.
696 */
697static inline unsigned int get_bits_long(GetBitContext *s, int n){
698 if(n<=17) return get_bits(s, n);
699 else{
700#ifdef ALT_BITSTREAM_READER_LE
701 int ret= get_bits(s, 16);
702 return ret | (get_bits(s, n-16) << 16);
703#else
704 int ret= get_bits(s, 16) << (n-16);
705 return ret | get_bits(s, n-16);
706#endif
707 }
708}
709
710/**
711 * reads 0-32 bits as a signed integer.
712 */
713static inline int get_sbits_long(GetBitContext *s, int n) {
714 return sign_extend(get_bits_long(s, n), n);
715}
716
717/**
718 * shows 0-32 bits.
719 */
720static inline unsigned int show_bits_long(GetBitContext *s, int n){
721 if(n<=17) return show_bits(s, n);
722 else{
723 GetBitContext gb= *s;
724 return get_bits_long(&gb, n);
725 }
726}
727
728static inline int check_marker(GetBitContext *s, const char *msg)
729{
730 int bit= get_bits1(s);
731 if(!bit)
732 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
733
734 return bit;
735}
736
737/**
738 * init GetBitContext.
739 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
740 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
741 * @param bit_size the size of the buffer in bits
742 */
743static inline void init_get_bits(GetBitContext *s,
744 const uint8_t *buffer, int bit_size)
745{
746 int buffer_size= (bit_size+7)>>3;
747 if(buffer_size < 0 || bit_size < 0) {
748 buffer_size = bit_size = 0;
749 buffer = NULL;
750 }
751
752 s->buffer= buffer;
753 s->size_in_bits= bit_size;
754 s->buffer_end= buffer + buffer_size;
755#ifdef ALT_BITSTREAM_READER
756 s->index=0;
757#elif defined LIBMPEG2_BITSTREAM_READER
758 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
759 s->bit_count = 16 + 8*((intptr_t)buffer&1);
760 skip_bits_long(s, 0);
761#elif defined A32_BITSTREAM_READER
762 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
763 s->bit_count = 32 + 8*((intptr_t)buffer&3);
764 skip_bits_long(s, 0);
765#endif
766}
767
768static inline void align_get_bits(GetBitContext *s)
769{
770 int n= (-get_bits_count(s)) & 7;
771 if(n) skip_bits(s, n);
772}
773
774#define init_vlc(vlc, nb_bits, nb_codes,\
775 bits, bits_wrap, bits_size,\
776 codes, codes_wrap, codes_size,\
777 flags)\
778 init_vlc_sparse(vlc, nb_bits, nb_codes,\
779 bits, bits_wrap, bits_size,\
780 codes, codes_wrap, codes_size,\
781 NULL, 0, 0, flags)
782
783int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
784 const void *bits, int bits_wrap, int bits_size,
785 const void *codes, int codes_wrap, int codes_size,
786 const void *symbols, int symbols_wrap, int symbols_size,
787 int flags);
788#define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
789#define INIT_VLC_LE 2
790#define INIT_VLC_USE_NEW_STATIC 4
791void free_vlc(VLC *vlc);
792
793#define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
794{\
795 static VLC_TYPE table[static_size][2];\
796 (vlc)->table= table;\
797 (vlc)->table_allocated= static_size;\
798 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
799}
800
801
802/**
803 *
804 * if the vlc code is invalid and max_depth=1 than no bits will be removed
805 * if the vlc code is invalid and max_depth>1 than the number of bits removed
806 * is undefined
807 */
808#define GET_VLC(code, name, gb, table, bits, max_depth)\
809{\
810 int n, index, nb_bits;\
811\
812 index= SHOW_UBITS(name, gb, bits);\
813 code = table[index][0];\
814 n = table[index][1];\
815\
816 if(max_depth > 1 && n < 0){\
817 LAST_SKIP_BITS(name, gb, bits)\
818 UPDATE_CACHE(name, gb)\
819\
820 nb_bits = -n;\
821\
822 index= SHOW_UBITS(name, gb, nb_bits) + code;\
823 code = table[index][0];\
824 n = table[index][1];\
825 if(max_depth > 2 && n < 0){\
826 LAST_SKIP_BITS(name, gb, nb_bits)\
827 UPDATE_CACHE(name, gb)\
828\
829 nb_bits = -n;\
830\
831 index= SHOW_UBITS(name, gb, nb_bits) + code;\
832 code = table[index][0];\
833 n = table[index][1];\
834 }\
835 }\
836 SKIP_BITS(name, gb, n)\
837}
838
839#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
840{\
841 int n, index, nb_bits;\
842\
843 index= SHOW_UBITS(name, gb, bits);\
844 level = table[index].level;\
845 n = table[index].len;\
846\
847 if(max_depth > 1 && n < 0){\
848 SKIP_BITS(name, gb, bits)\
849 if(need_update){\
850 UPDATE_CACHE(name, gb)\
851 }\
852\
853 nb_bits = -n;\
854\
855 index= SHOW_UBITS(name, gb, nb_bits) + level;\
856 level = table[index].level;\
857 n = table[index].len;\
858 }\
859 run= table[index].run;\
860 SKIP_BITS(name, gb, n)\
861}
862
863
864/**
865 * parses a vlc code, faster then get_vlc()
866 * @param bits is the number of bits which will be read at once, must be
867 * identical to nb_bits in init_vlc()
868 * @param max_depth is the number of times bits bits must be read to completely
869 * read the longest vlc code
870 * = (max_vlc_length + bits - 1) / bits
871 */
872static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
873 int bits, int max_depth)
874{
875 int code;
876
877 OPEN_READER(re, s)
878 UPDATE_CACHE(re, s)
879
880 GET_VLC(code, re, s, table, bits, max_depth)
881
882 CLOSE_READER(re, s)
883 return code;
884}
885
886//#define TRACE
887
888#ifdef TRACE
889static inline void print_bin(int bits, int n){
890 int i;
891
892 for(i=n-1; i>=0; i--){
893 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
894 }
895 for(i=n; i<24; i++)
896 av_log(NULL, AV_LOG_DEBUG, " ");
897}
898
899static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
900 int r= get_bits(s, n);
901
902 print_bin(r, n);
903 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);
904 return r;
905}
906static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
907 int show= show_bits(s, 24);
908 int pos= get_bits_count(s);
909 int r= get_vlc2(s, table, bits, max_depth);
910 int len= get_bits_count(s) - pos;
911 int bits2= show>>(24-len);
912
913 print_bin(bits2, len);
914
915 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
916 return r;
917}
918static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
919 int show= show_bits(s, n);
920 int r= get_xbits(s, n);
921
922 print_bin(show, n);
923 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);
924 return r;
925}
926
927#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
928#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
929#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
930#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
931#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
932
933#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
934
935#else //TRACE
936#define tprintf(p, ...) {}
937#endif
938
939static inline int decode012(GetBitContext *gb){
940 int n;
941 n = get_bits1(gb);
942 if (n == 0)
943 return 0;
944 else
945 return get_bits1(gb) + 1;
946}
947
948static inline int decode210(GetBitContext *gb){
949 if (get_bits1(gb))
950 return 0;
951 else
952 return 2 - get_bits1(gb);
953}
954
955#endif /* AVCODEC_BITSTREAM_H */