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