Fix #endif comments.
[libav.git] / libavcodec / bitstream.h
CommitLineData
04d7f601
DB
1/*
2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
b78e7197
DB
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
04d7f601
DB
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
b78e7197 9 * version 2.1 of the License, or (at your option) any later version.
04d7f601 10 *
b78e7197 11 * FFmpeg is distributed in the hope that it will be useful,
04d7f601
DB
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
b78e7197 17 * License along with FFmpeg; if not, write to the Free Software
04d7f601
DB
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
caa336b4
MN
21/**
22 * @file bitstream.h
23 * bitstream api header.
24 */
25
26#ifndef BITSTREAM_H
27#define BITSTREAM_H
28
8fbc6aae
AJ
29#include "log.h"
30
75c998a2
AJ
31#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
32#define ALT_BITSTREAM_READER
33#endif
34
caa336b4
MN
35//#define ALT_BITSTREAM_WRITER
36//#define ALIGNED_BITSTREAM_WRITER
727c236a 37#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
a1b65018
AJ
38# ifdef ARCH_ARMV4L
39# define A32_BITSTREAM_READER
40# else
caa336b4
MN
41#define ALT_BITSTREAM_READER
42//#define LIBMPEG2_BITSTREAM_READER
43//#define A32_BITSTREAM_READER
a1b65018 44# endif
727c236a 45#endif
caa336b4 46#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
115329f1 47
fcbc799c
MN
48extern const uint8_t ff_reverse[256];
49
caa336b4
MN
50#if defined(ARCH_X86) || defined(ARCH_X86_64)
51// avoid +32 for shift optimization (gcc should do that ...)
52static inline int32_t NEG_SSR32( int32_t a, int8_t s){
53 asm ("sarl %1, %0\n\t"
54 : "+r" (a)
55 : "ic" ((uint8_t)(-s))
56 );
57 return a;
58}
59static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
60 asm ("shrl %1, %0\n\t"
61 : "+r" (a)
62 : "ic" ((uint8_t)(-s))
63 );
64 return a;
65}
66#else
67# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
68# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
69#endif
70
71/* bit output */
72
73/* buf and buf_end must be present and used by every alternative writer. */
74typedef struct PutBitContext {
75#ifdef ALT_BITSTREAM_WRITER
76 uint8_t *buf, *buf_end;
77 int index;
78#else
79 uint32_t bit_buf;
80 int bit_left;
81 uint8_t *buf, *buf_ptr, *buf_end;
82#endif
83} PutBitContext;
84
85static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
86{
288f1e68
MB
87 if(buffer_size < 0) {
88 buffer_size = 0;
89 buffer = NULL;
90 }
9061be9f 91
caa336b4
MN
92 s->buf = buffer;
93 s->buf_end = s->buf + buffer_size;
94#ifdef ALT_BITSTREAM_WRITER
95 s->index=0;
96 ((uint32_t*)(s->buf))[0]=0;
97// memset(buffer, 0, buffer_size);
98#else
99 s->buf_ptr = s->buf;
100 s->bit_left=32;
101 s->bit_buf=0;
102#endif
103}
104
105/* return the number of bits output */
106static inline int put_bits_count(PutBitContext *s)
107{
108#ifdef ALT_BITSTREAM_WRITER
109 return s->index;
110#else
111 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
112#endif
113}
114
115/* pad the end of the output stream with zeros */
116static inline void flush_put_bits(PutBitContext *s)
117{
118#ifdef ALT_BITSTREAM_WRITER
119 align_put_bits(s);
120#else
121 s->bit_buf<<= s->bit_left;
122 while (s->bit_left < 32) {
123 /* XXX: should test end of buffer */
124 *s->buf_ptr++=s->bit_buf >> 24;
125 s->bit_buf<<=8;
126 s->bit_left+=8;
127 }
128 s->bit_left=32;
129 s->bit_buf=0;
130#endif
131}
132
133void align_put_bits(PutBitContext *s);
344b825c 134void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
caa336b4
MN
135
136/* bit input */
137/* buffer, buffer_end and size_in_bits must be present and used by every reader */
138typedef struct GetBitContext {
139 const uint8_t *buffer, *buffer_end;
140#ifdef ALT_BITSTREAM_READER
141 int index;
142#elif defined LIBMPEG2_BITSTREAM_READER
143 uint8_t *buffer_ptr;
144 uint32_t cache;
145 int bit_count;
146#elif defined A32_BITSTREAM_READER
147 uint32_t *buffer_ptr;
148 uint32_t cache0;
149 uint32_t cache1;
150 int bit_count;
151#endif
152 int size_in_bits;
153} GetBitContext;
154
155#define VLC_TYPE int16_t
156
157typedef struct VLC {
158 int bits;
159 VLC_TYPE (*table)[2]; ///< code, bits
160 int table_size, table_allocated;
161} VLC;
162
163typedef struct RL_VLC_ELEM {
164 int16_t level;
165 int8_t len;
166 uint8_t run;
167} RL_VLC_ELEM;
168
e6f0ad51 169#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS)
caa336b4
MN
170#define UNALIGNED_STORES_ARE_BAD
171#endif
172
173/* used to avoid missaligned exceptions on some archs (alpha, ...) */
174#if defined(ARCH_X86) || defined(ARCH_X86_64)
b8d10977 175# define unaligned16(a) (*(const uint16_t*)(a))
caa336b4 176# define unaligned32(a) (*(const uint32_t*)(a))
b8d10977 177# define unaligned64(a) (*(const uint64_t*)(a))
caa336b4
MN
178#else
179# ifdef __GNUC__
b8d10977
MR
180# define unaligned(x) \
181static inline uint##x##_t unaligned##x(const void *v) { \
182 struct Unaligned { \
183 uint##x##_t i; \
184 } __attribute__((packed)); \
185 \
186 return ((const struct Unaligned *) v)->i; \
caa336b4
MN
187}
188# elif defined(__DECC)
b8d10977
MR
189# define unaligned(x) \
190static inline uint##x##_t unaligned##x##(const void *v) { \
191 return *(const __unaligned uint##x##_t *) v; \
caa336b4
MN
192}
193# else
b8d10977
MR
194# define unaligned(x) \
195static inline uint##x##_t unaligned##x##(const void *v) { \
196 return *(const uint##x##_t *) v; \
caa336b4
MN
197}
198# endif
b8d10977
MR
199unaligned(16)
200unaligned(32)
201unaligned(64)
202#undef unaligned
c45b5de0 203#endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
caa336b4
MN
204
205#ifndef ALT_BITSTREAM_WRITER
206static inline void put_bits(PutBitContext *s, int n, unsigned int value)
207{
208 unsigned int bit_buf;
209 int bit_left;
210
caa336b4
MN
211 // printf("put_bits=%d %x\n", n, value);
212 assert(n == 32 || value < (1U << n));
115329f1 213
caa336b4
MN
214 bit_buf = s->bit_buf;
215 bit_left = s->bit_left;
216
217 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
218 /* XXX: optimize */
219 if (n < bit_left) {
220 bit_buf = (bit_buf<<n) | value;
221 bit_left-=n;
222 } else {
bb270c08 223 bit_buf<<=bit_left;
caa336b4
MN
224 bit_buf |= value >> (n - bit_left);
225#ifdef UNALIGNED_STORES_ARE_BAD
226 if (3 & (intptr_t) s->buf_ptr) {
227 s->buf_ptr[0] = bit_buf >> 24;
228 s->buf_ptr[1] = bit_buf >> 16;
229 s->buf_ptr[2] = bit_buf >> 8;
230 s->buf_ptr[3] = bit_buf ;
231 } else
232#endif
233 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
234 //printf("bitbuf = %08x\n", bit_buf);
235 s->buf_ptr+=4;
bb270c08 236 bit_left+=32 - n;
caa336b4
MN
237 bit_buf = value;
238 }
239
240 s->bit_buf = bit_buf;
241 s->bit_left = bit_left;
242}
243#endif
244
245
246#ifdef ALT_BITSTREAM_WRITER
247static inline void put_bits(PutBitContext *s, int n, unsigned int value)
248{
249# ifdef ALIGNED_BITSTREAM_WRITER
250# if defined(ARCH_X86) || defined(ARCH_X86_64)
251 asm volatile(
bb270c08
DB
252 "movl %0, %%ecx \n\t"
253 "xorl %%eax, %%eax \n\t"
254 "shrdl %%cl, %1, %%eax \n\t"
255 "shrl %%cl, %1 \n\t"
256 "movl %0, %%ecx \n\t"
257 "shrl $3, %%ecx \n\t"
258 "andl $0xFFFFFFFC, %%ecx \n\t"
259 "bswapl %1 \n\t"
260 "orl %1, (%2, %%ecx) \n\t"
261 "bswapl %%eax \n\t"
262 "addl %3, %0 \n\t"
263 "movl %%eax, 4(%2, %%ecx) \n\t"
264 : "=&r" (s->index), "=&r" (value)
265 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
266 : "%eax", "%ecx"
caa336b4
MN
267 );
268# else
269 int index= s->index;
270 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
115329f1
DB
271
272 value<<= 32-n;
273
caa336b4
MN
274 ptr[0] |= be2me_32(value>>(index&31));
275 ptr[1] = be2me_32(value<<(32-(index&31)));
276//if(n>24) printf("%d %d\n", n, value);
277 index+= n;
278 s->index= index;
279# endif
280# else //ALIGNED_BITSTREAM_WRITER
281# if defined(ARCH_X86) || defined(ARCH_X86_64)
282 asm volatile(
bb270c08
DB
283 "movl $7, %%ecx \n\t"
284 "andl %0, %%ecx \n\t"
285 "addl %3, %%ecx \n\t"
286 "negl %%ecx \n\t"
287 "shll %%cl, %1 \n\t"
288 "bswapl %1 \n\t"
289 "movl %0, %%ecx \n\t"
290 "shrl $3, %%ecx \n\t"
291 "orl %1, (%%ecx, %2) \n\t"
292 "addl %3, %0 \n\t"
293 "movl $0, 4(%%ecx, %2) \n\t"
294 : "=&r" (s->index), "=&r" (value)
295 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
296 : "%ecx"
caa336b4
MN
297 );
298# else
299 int index= s->index;
300 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
115329f1 301
caa336b4
MN
302 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
303 ptr[1] = 0;
304//if(n>24) printf("%d %d\n", n, value);
305 index+= n;
306 s->index= index;
307# endif
308# endif //!ALIGNED_BITSTREAM_WRITER
309}
310#endif
311
312
313static inline uint8_t* pbBufPtr(PutBitContext *s)
314{
315#ifdef ALT_BITSTREAM_WRITER
bb270c08 316 return s->buf + (s->index>>3);
caa336b4 317#else
bb270c08 318 return s->buf_ptr;
caa336b4
MN
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
bb270c08 330 s->index += n<<3;
caa336b4
MN
331#else
332 assert(s->bit_left==32);
bb270c08 333 s->buf_ptr += n;
115329f1 334#endif
caa336b4
MN
335}
336
337/**
1b781f82
MN
338 * skips the given number of bits.
339 * must only be used if the actual values in the bitstream dont 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;
115329f1 348#endif
1b781f82
MN
349}
350
351/**
caa336b4
MN
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 abritary 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)
bb628dae 379 will return the next num bits
caa336b4
MN
380
381SHOW_SBITS(name, gb, num)
bb628dae 382 will return the next num bits and do sign extension
caa336b4
MN
383
384SKIP_BITS(name, gb, num)
385 will skip over the next num bits
bb628dae 386 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
caa336b4
MN
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)
bb628dae 398 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
caa336b4
MN
399
400for examples see get_bits, show_bits, skip_bits, get_vlc
401*/
402
403static inline int unaligned32_be(const void *v)
404{
405#ifdef CONFIG_ALIGN
bb270c08
DB
406 const uint8_t *p=v;
407 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
caa336b4 408#else
bb270c08 409 return be2me_32( unaligned32(v)); //original
caa336b4
MN
410#endif
411}
412
cea27ac7
MN
413static inline int unaligned32_le(const void *v)
414{
415#ifdef CONFIG_ALIGN
416 const uint8_t *p=v;
417 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
418#else
419 return le2me_32( unaligned32(v)); //original
420#endif
421}
422
caa336b4
MN
423#ifdef ALT_BITSTREAM_READER
424# define MIN_CACHE_BITS 25
425
426# define OPEN_READER(name, gb)\
427 int name##_index= (gb)->index;\
428 int name##_cache= 0;\
429
430# define CLOSE_READER(name, gb)\
431 (gb)->index= name##_index;\
432
cea27ac7
MN
433# ifdef ALT_BITSTREAM_READER_LE
434# define UPDATE_CACHE(name, gb)\
435 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
436
437# define SKIP_CACHE(name, gb, num)\
438 name##_cache >>= (num);
439# else
caa336b4
MN
440# define UPDATE_CACHE(name, gb)\
441 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
442
443# define SKIP_CACHE(name, gb, num)\
cea27ac7
MN
444 name##_cache <<= (num);
445# endif
caa336b4
MN
446
447// FIXME name?
448# define SKIP_COUNTER(name, gb, num)\
449 name##_index += (num);\
450
451# define SKIP_BITS(name, gb, num)\
452 {\
453 SKIP_CACHE(name, gb, num)\
454 SKIP_COUNTER(name, gb, num)\
455 }\
456
457# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
458# define LAST_SKIP_CACHE(name, gb, num) ;
459
cea27ac7
MN
460# ifdef ALT_BITSTREAM_READER_LE
461# define SHOW_UBITS(name, gb, num)\
462 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
7da4d4cd
GM
463
464# define SHOW_SBITS(name, gb, num)\
465 NEG_SSR32((name##_cache)<<(32-(num)), num)
cea27ac7 466# else
caa336b4
MN
467# define SHOW_UBITS(name, gb, num)\
468 NEG_USR32(name##_cache, num)
469
470# define SHOW_SBITS(name, gb, num)\
471 NEG_SSR32(name##_cache, num)
7da4d4cd 472# endif
caa336b4
MN
473
474# define GET_CACHE(name, gb)\
475 ((uint32_t)name##_cache)
476
477static inline int get_bits_count(GetBitContext *s){
478 return s->index;
479}
5a7bd283
MN
480
481static inline void skip_bits_long(GetBitContext *s, int n){
6e4703ca 482 s->index += n;
5a7bd283
MN
483}
484
caa336b4
MN
485#elif defined LIBMPEG2_BITSTREAM_READER
486//libmpeg2 like reader
487
488# define MIN_CACHE_BITS 17
489
490# define OPEN_READER(name, gb)\
491 int name##_bit_count=(gb)->bit_count;\
492 int name##_cache= (gb)->cache;\
493 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
494
495# define CLOSE_READER(name, gb)\
496 (gb)->bit_count= name##_bit_count;\
497 (gb)->cache= name##_cache;\
498 (gb)->buffer_ptr= name##_buffer_ptr;\
499
500#ifdef LIBMPEG2_BITSTREAM_READER_HACK
501
502# define UPDATE_CACHE(name, gb)\
503 if(name##_bit_count >= 0){\
504 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
5bb8a0da 505 name##_buffer_ptr += 2;\
caa336b4
MN
506 name##_bit_count-= 16;\
507 }\
508
509#else
510
511# define UPDATE_CACHE(name, gb)\
512 if(name##_bit_count >= 0){\
513 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
514 name##_buffer_ptr+=2;\
515 name##_bit_count-= 16;\
516 }\
517
518#endif
519
520# define SKIP_CACHE(name, gb, num)\
521 name##_cache <<= (num);\
522
523# define SKIP_COUNTER(name, gb, num)\
524 name##_bit_count += (num);\
525
526# define SKIP_BITS(name, gb, num)\
527 {\
528 SKIP_CACHE(name, gb, num)\
529 SKIP_COUNTER(name, gb, num)\
530 }\
531
532# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
533# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
534
535# define SHOW_UBITS(name, gb, num)\
536 NEG_USR32(name##_cache, num)
537
538# define SHOW_SBITS(name, gb, num)\
539 NEG_SSR32(name##_cache, num)
540
541# define GET_CACHE(name, gb)\
542 ((uint32_t)name##_cache)
543
544static inline int get_bits_count(GetBitContext *s){
545 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
546}
547
6e4703ca
MN
548static inline void skip_bits_long(GetBitContext *s, int n){
549 OPEN_READER(re, s)
550 re_bit_count += n;
551 re_buffer_ptr += 2*(re_bit_count>>4);
552 re_bit_count &= 15;
553 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
554 UPDATE_CACHE(re, s)
555 CLOSE_READER(re, s)
556}
557
caa336b4
MN
558#elif defined A32_BITSTREAM_READER
559
560# define MIN_CACHE_BITS 32
561
562# define OPEN_READER(name, gb)\
563 int name##_bit_count=(gb)->bit_count;\
564 uint32_t name##_cache0= (gb)->cache0;\
565 uint32_t name##_cache1= (gb)->cache1;\
566 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
567
568# define CLOSE_READER(name, gb)\
569 (gb)->bit_count= name##_bit_count;\
570 (gb)->cache0= name##_cache0;\
571 (gb)->cache1= name##_cache1;\
572 (gb)->buffer_ptr= name##_buffer_ptr;\
573
574# define UPDATE_CACHE(name, gb)\
575 if(name##_bit_count > 0){\
576 const uint32_t next= be2me_32( *name##_buffer_ptr );\
577 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
578 name##_cache1 |= next<<name##_bit_count;\
579 name##_buffer_ptr++;\
580 name##_bit_count-= 32;\
581 }\
582
583#if defined(ARCH_X86) || defined(ARCH_X86_64)
584# define SKIP_CACHE(name, gb, num)\
585 asm(\
bb270c08
DB
586 "shldl %2, %1, %0 \n\t"\
587 "shll %2, %1 \n\t"\
caa336b4 588 : "+r" (name##_cache0), "+r" (name##_cache1)\
6ff3b2b8 589 : "Ic" ((uint8_t)(num))\
caa336b4
MN
590 );
591#else
592# define SKIP_CACHE(name, gb, num)\
593 name##_cache0 <<= (num);\
594 name##_cache0 |= NEG_USR32(name##_cache1,num);\
595 name##_cache1 <<= (num);
596#endif
597
598# define SKIP_COUNTER(name, gb, num)\
599 name##_bit_count += (num);\
600
601# define SKIP_BITS(name, gb, num)\
602 {\
603 SKIP_CACHE(name, gb, num)\
604 SKIP_COUNTER(name, gb, num)\
605 }\
606
607# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
608# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
609
610# define SHOW_UBITS(name, gb, num)\
611 NEG_USR32(name##_cache0, num)
612
613# define SHOW_SBITS(name, gb, num)\
614 NEG_SSR32(name##_cache0, num)
615
616# define GET_CACHE(name, gb)\
617 (name##_cache0)
618
619static inline int get_bits_count(GetBitContext *s){
620 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
621}
622
5a7bd283
MN
623static inline void skip_bits_long(GetBitContext *s, int n){
624 OPEN_READER(re, s)
625 re_bit_count += n;
3dddf21f 626 re_buffer_ptr += re_bit_count>>5;
5a7bd283 627 re_bit_count &= 31;
1491e21c
MN
628 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
629 re_cache1 = 0;
5a7bd283 630 UPDATE_CACHE(re, s)
5a7bd283
MN
631 CLOSE_READER(re, s)
632}
633
caa336b4
MN
634#endif
635
636/**
637 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
115329f1 638 * if MSB not set it is negative
caa336b4 639 * @param n length in bits
115329f1 640 * @author BERO
caa336b4
MN
641 */
642static inline int get_xbits(GetBitContext *s, int n){
6b250e47 643 register int sign;
caa336b4
MN
644 register int32_t cache;
645 OPEN_READER(re, s)
646 UPDATE_CACHE(re, s)
647 cache = GET_CACHE(re,s);
6b250e47 648 sign=(~cache)>>31;
caa336b4
MN
649 LAST_SKIP_BITS(re, s, n)
650 CLOSE_READER(re, s)
6b250e47 651 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
caa336b4
MN
652}
653
654static inline int get_sbits(GetBitContext *s, int n){
655 register int tmp;
656 OPEN_READER(re, s)
657 UPDATE_CACHE(re, s)
658 tmp= SHOW_SBITS(re, s, n);
659 LAST_SKIP_BITS(re, s, n)
660 CLOSE_READER(re, s)
661 return tmp;
662}
663
664/**
665 * reads 0-17 bits.
bb628dae 666 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
caa336b4
MN
667 */
668static inline unsigned int get_bits(GetBitContext *s, int n){
669 register int tmp;
670 OPEN_READER(re, s)
671 UPDATE_CACHE(re, s)
672 tmp= SHOW_UBITS(re, s, n);
673 LAST_SKIP_BITS(re, s, n)
674 CLOSE_READER(re, s)
675 return tmp;
676}
677
caa336b4
MN
678/**
679 * shows 0-17 bits.
bb628dae 680 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
caa336b4
MN
681 */
682static inline unsigned int show_bits(GetBitContext *s, int n){
683 register int tmp;
684 OPEN_READER(re, s)
685 UPDATE_CACHE(re, s)
686 tmp= SHOW_UBITS(re, s, n);
687// CLOSE_READER(re, s)
688 return tmp;
689}
690
caa336b4
MN
691static inline void skip_bits(GetBitContext *s, int n){
692 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
693 OPEN_READER(re, s)
694 UPDATE_CACHE(re, s)
695 LAST_SKIP_BITS(re, s, n)
696 CLOSE_READER(re, s)
697}
698
699static inline unsigned int get_bits1(GetBitContext *s){
700#ifdef ALT_BITSTREAM_READER
701 int index= s->index;
702 uint8_t result= s->buffer[ index>>3 ];
cea27ac7
MN
703#ifdef ALT_BITSTREAM_READER_LE
704 result>>= (index&0x07);
705 result&= 1;
706#else
caa336b4
MN
707 result<<= (index&0x07);
708 result>>= 8 - 1;
cea27ac7 709#endif
caa336b4
MN
710 index++;
711 s->index= index;
712
713 return result;
714#else
715 return get_bits(s, 1);
716#endif
717}
718
719static inline unsigned int show_bits1(GetBitContext *s){
720 return show_bits(s, 1);
721}
722
723static inline void skip_bits1(GetBitContext *s){
724 skip_bits(s, 1);
725}
726
727/**
8fbc6aae
AJ
728 * reads 0-32 bits.
729 */
730static inline unsigned int get_bits_long(GetBitContext *s, int n){
731 if(n<=17) return get_bits(s, n);
732 else{
d1121caa
RD
733#ifdef ALT_BITSTREAM_READER_LE
734 int ret= get_bits(s, 16);
735 return ret | (get_bits(s, n-16) << 16);
736#else
8fbc6aae
AJ
737 int ret= get_bits(s, 16) << (n-16);
738 return ret | get_bits(s, n-16);
d1121caa 739#endif
8fbc6aae
AJ
740 }
741}
742
743/**
744 * shows 0-32 bits.
745 */
746static inline unsigned int show_bits_long(GetBitContext *s, int n){
747 if(n<=17) return show_bits(s, n);
748 else{
749 GetBitContext gb= *s;
750 int ret= get_bits_long(s, n);
751 *s= gb;
752 return ret;
753 }
754}
755
756static inline int check_marker(GetBitContext *s, const char *msg)
757{
758 int bit= get_bits1(s);
759 if(!bit)
760 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
761
762 return bit;
763}
764
765/**
caa336b4
MN
766 * init GetBitContext.
767 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
768 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
769 * @param bit_size the size of the buffer in bits
770 */
771static inline void init_get_bits(GetBitContext *s,
772 const uint8_t *buffer, int bit_size)
773{
9061be9f 774 int buffer_size= (bit_size+7)>>3;
288f1e68
MB
775 if(buffer_size < 0 || bit_size < 0) {
776 buffer_size = bit_size = 0;
777 buffer = NULL;
778 }
caa336b4
MN
779
780 s->buffer= buffer;
781 s->size_in_bits= bit_size;
782 s->buffer_end= buffer + buffer_size;
783#ifdef ALT_BITSTREAM_READER
784 s->index=0;
785#elif defined LIBMPEG2_BITSTREAM_READER
6e4703ca
MN
786 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
787 s->bit_count = 16 + 8*((intptr_t)buffer&1);
788 skip_bits_long(s, 0);
a42ec9f4
MN
789#elif defined A32_BITSTREAM_READER
790 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
791 s->bit_count = 32 + 8*((intptr_t)buffer&3);
792 skip_bits_long(s, 0);
caa336b4
MN
793#endif
794}
795
ae5e7e7f 796static inline void align_get_bits(GetBitContext *s)
5a7bd283
MN
797{
798 int n= (-get_bits_count(s)) & 7;
799 if(n) skip_bits(s, n);
800}
801
caa336b4
MN
802int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
803 const void *bits, int bits_wrap, int bits_size,
804 const void *codes, int codes_wrap, int codes_size,
cea27ac7
MN
805 int flags);
806#define INIT_VLC_USE_STATIC 1
807#define INIT_VLC_LE 2
caa336b4
MN
808void free_vlc(VLC *vlc);
809
810/**
811 *
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
815 */
816#define GET_VLC(code, name, gb, table, bits, max_depth)\
817{\
818 int n, index, nb_bits;\
819\
820 index= SHOW_UBITS(name, gb, bits);\
821 code = table[index][0];\
822 n = table[index][1];\
823\
824 if(max_depth > 1 && n < 0){\
825 LAST_SKIP_BITS(name, gb, bits)\
826 UPDATE_CACHE(name, gb)\
827\
828 nb_bits = -n;\
829\
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)\
836\
837 nb_bits = -n;\
838\
839 index= SHOW_UBITS(name, gb, nb_bits) + code;\
840 code = table[index][0];\
841 n = table[index][1];\
842 }\
843 }\
844 SKIP_BITS(name, gb, n)\
845}
846
e91f4bf1 847#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
caa336b4
MN
848{\
849 int n, index, nb_bits;\
850\
851 index= SHOW_UBITS(name, gb, bits);\
852 level = table[index].level;\
853 n = table[index].len;\
854\
855 if(max_depth > 1 && n < 0){\
e91f4bf1
MN
856 SKIP_BITS(name, gb, bits)\
857 if(need_update){\
858 UPDATE_CACHE(name, gb)\
859 }\
caa336b4
MN
860\
861 nb_bits = -n;\
862\
863 index= SHOW_UBITS(name, gb, nb_bits) + level;\
864 level = table[index].level;\
865 n = table[index].len;\
866 }\
867 run= table[index].run;\
868 SKIP_BITS(name, gb, n)\
869}
870
caa336b4
MN
871
872/**
873 * parses a vlc code, faster then get_vlc()
115329f1 874 * @param bits is the number of bits which will be read at once, must be
caa336b4
MN
875 * identical to nb_bits in init_vlc()
876 * @param max_depth is the number of times bits bits must be readed to completly
115329f1 877 * read the longest vlc code
caa336b4
MN
878 * = (max_vlc_length + bits - 1) / bits
879 */
880static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
881 int bits, int max_depth)
882{
883 int code;
115329f1 884
caa336b4
MN
885 OPEN_READER(re, s)
886 UPDATE_CACHE(re, s)
887
888 GET_VLC(code, re, s, table, bits, max_depth)
889
890 CLOSE_READER(re, s)
891 return code;
892}
893
894//#define TRACE
895
896#ifdef TRACE
caa336b4
MN
897static inline void print_bin(int bits, int n){
898 int i;
115329f1 899
caa336b4 900 for(i=n-1; i>=0; i--){
61f040dd 901 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
caa336b4
MN
902 }
903 for(i=n; i<24; i++)
61f040dd 904 av_log(NULL, AV_LOG_DEBUG, " ");
caa336b4
MN
905}
906
61f040dd 907static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
caa336b4 908 int r= get_bits(s, n);
115329f1 909
caa336b4 910 print_bin(r, n);
61f040dd 911 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);
caa336b4
MN
912 return r;
913}
61f040dd 914static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
caa336b4
MN
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);
115329f1 920
caa336b4 921 print_bin(bits2, len);
115329f1 922
61f040dd 923 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
caa336b4
MN
924 return r;
925}
61f040dd 926static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
caa336b4
MN
927 int show= show_bits(s, n);
928 int r= get_xbits(s, n);
115329f1 929
caa336b4 930 print_bin(show, n);
61f040dd 931 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);
caa336b4
MN
932 return r;
933}
934
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__)
940
941#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
942
943#else //TRACE
944#define tprintf(...) {}
945#endif
946
7d8b13b4 947static inline int decode012(GetBitContext *gb){
a64a006e
MN
948 int n;
949 n = get_bits1(gb);
950 if (n == 0)
951 return 0;
952 else
953 return get_bits1(gb) + 1;
954}
955
caa336b4 956#endif /* BITSTREAM_H */