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