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