Some people confuse vidix with kernel drivers, so let's add a note about it
[mplayer/glamo.git] / liba52 / bitstream.h
blob7e4ff676c86c26d6e6734f59ba92b2bb08661683
1 /*
2 * bitstream.h
3 * Copyright (C) 2000-2001 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates.
9 * Modified for use with MPlayer, changes contained in liba52_changes.diff.
10 * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
11 * $Id$
13 * a52dec is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * a52dec is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /* code from ffmpeg/libavcodec */
29 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC_ == 3 && __GNUC_MINOR__ > 0)
30 # define always_inline __attribute__((always_inline)) inline
31 #else
32 # define always_inline inline
33 #endif
35 #if defined(__sparc__) || defined(hpux)
37 * the alt bitstream reader performs unaligned memory accesses; that doesn't work
38 * on sparc/hpux. For now, disable ALT_BITSTREAM_READER.
40 #undef ALT_BITSTREAM_READER
41 #else
42 // alternative (faster) bitstram reader (reades upto 3 bytes over the end of the input)
43 #define ALT_BITSTREAM_READER
45 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
46 #if defined (ARCH_X86) || defined(ARCH_ARMV4L)
47 # define unaligned32(a) (*(uint32_t*)(a))
48 #else
49 # ifdef __GNUC__
50 static always_inline uint32_t unaligned32(const void *v) {
51 struct Unaligned {
52 uint32_t i;
53 } __attribute__((packed));
55 return ((const struct Unaligned *) v)->i;
57 # elif defined(__DECC)
58 static inline uint32_t unaligned32(const void *v) {
59 return *(const __unaligned uint32_t *) v;
61 # else
62 static inline uint32_t unaligned32(const void *v) {
63 return *(const uint32_t *) v;
65 # endif
66 #endif //!ARCH_X86
68 #endif
70 /* (stolen from the kernel) */
71 #ifdef WORDS_BIGENDIAN
73 # define swab32(x) (x)
75 #else
77 # if defined (__i386__)
79 # define swab32(x) __i386_swab32(x)
80 static always_inline const uint32_t __i386_swab32(uint32_t x)
82 __asm__("bswap %0" : "=r" (x) : "0" (x));
83 return x;
86 # else
88 # define swab32(x) __generic_swab32(x)
89 static always_inline const uint32_t __generic_swab32(uint32_t x)
91 return ((((uint8_t*)&x)[0] << 24) | (((uint8_t*)&x)[1] << 16) |
92 (((uint8_t*)&x)[2] << 8) | (((uint8_t*)&x)[3]));
94 # endif
95 #endif
97 #ifdef ALT_BITSTREAM_READER
98 extern uint32_t *buffer_start;
99 extern int indx;
100 #else
101 extern uint32_t bits_left;
102 extern uint32_t current_word;
103 #endif
105 void bitstream_set_ptr (uint8_t * buf);
106 uint32_t bitstream_get_bh(uint32_t num_bits);
107 int32_t bitstream_get_bh_2(uint32_t num_bits);
110 static inline uint32_t
111 bitstream_get(uint32_t num_bits) // note num_bits is practically a constant due to inlineing
113 #ifdef ALT_BITSTREAM_READER
114 uint32_t result= swab32( unaligned32(((uint8_t *)buffer_start)+(indx>>3)) );
116 result<<= (indx&0x07);
117 result>>= 32 - num_bits;
118 indx+= num_bits;
120 return result;
121 #else
122 uint32_t result;
124 if(num_bits < bits_left) {
125 result = (current_word << (32 - bits_left)) >> (32 - num_bits);
126 bits_left -= num_bits;
127 return result;
130 return bitstream_get_bh(num_bits);
131 #endif
134 static inline void bitstream_skip(int num_bits)
136 #ifdef ALT_BITSTREAM_READER
137 indx+= num_bits;
138 #else
139 bitstream_get(num_bits);
140 #endif
143 static inline int32_t
144 bitstream_get_2(uint32_t num_bits)
146 #ifdef ALT_BITSTREAM_READER
147 int32_t result= swab32( unaligned32(((uint8_t *)buffer_start)+(indx>>3)) );
149 result<<= (indx&0x07);
150 result>>= 32 - num_bits;
151 indx+= num_bits;
153 return result;
154 #else
155 int32_t result;
157 if(num_bits < bits_left) {
158 result = (((int32_t)current_word) << (32 - bits_left)) >> (32 - num_bits);
159 bits_left -= num_bits;
160 return result;
163 return bitstream_get_bh_2(num_bits);
164 #endif