sync with en/mplayer.1 r29731
[mplayer/glamo.git] / liba52 / bitstream.h
blob76f5556ea9960da94e08c6267512bafcac08c8af
1 /*
2 * bitstream.h
3 * Copyright (C) 2000-2002 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 changelog at http://svn.mplayerhq.hu/mplayer/trunk/
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(__sparc__) || defined(hpux)
31 * the alt bitstream reader performs unaligned memory accesses; that doesn't work
32 * on sparc/hpux. For now, disable ALT_BITSTREAM_READER.
34 #undef ALT_BITSTREAM_READER
35 #else
36 // alternative (faster) bitstram reader (reades upto 3 bytes over the end of the input)
37 #define ALT_BITSTREAM_READER
39 /* used to avoid misaligned exceptions on some archs (alpha, ...) */
40 #if ARCH_X86 || HAVE_ARMV6
41 # define unaligned32(a) (*(uint32_t*)(a))
42 #else
43 # ifdef __GNUC__
44 static inline uint32_t unaligned32(const void *v) {
45 struct Unaligned {
46 uint32_t i;
47 } __attribute__((packed));
49 return ((const struct Unaligned *) v)->i;
51 # elif defined(__DECC)
52 static inline uint32_t unaligned32(const void *v) {
53 return *(const __unaligned uint32_t *) v;
55 # else
56 static inline uint32_t unaligned32(const void *v) {
57 return *(const uint32_t *) v;
59 # endif
60 #endif //!ARCH_X86
62 #endif
64 /* (stolen from the kernel) */
65 #if HAVE_BIGENDIAN
67 # define swab32(x) (x)
69 #else
71 # if defined (__i386__)
73 # define swab32(x) __i386_swab32(x)
74 static inline const uint32_t __i386_swab32(uint32_t x)
76 __asm__("bswap %0" : "=r" (x) : "0" (x));
77 return x;
80 # else
82 # define swab32(x) __generic_swab32(x)
83 static inline const uint32_t __generic_swab32(uint32_t x)
85 return ((((uint8_t*)&x)[0] << 24) | (((uint8_t*)&x)[1] << 16) |
86 (((uint8_t*)&x)[2] << 8) | (((uint8_t*)&x)[3]));
88 # endif
89 #endif
91 #ifdef ALT_BITSTREAM_READER
92 extern int indx;
93 #endif
95 void a52_bitstream_set_ptr (a52_state_t * state, uint8_t * buf);
96 uint32_t a52_bitstream_get_bh (a52_state_t * state, uint32_t num_bits);
97 int32_t a52_bitstream_get_bh_2 (a52_state_t * state, uint32_t num_bits);
99 static inline uint32_t bitstream_get (a52_state_t * state, uint32_t num_bits)
101 #ifdef ALT_BITSTREAM_READER
102 uint32_t result= swab32( unaligned32(((uint8_t *)state->buffer_start)+(indx>>3)) );
104 result<<= (indx&0x07);
105 result>>= 32 - num_bits;
106 indx+= num_bits;
108 return result;
109 #else
110 uint32_t result;
112 if (num_bits < state->bits_left) {
113 result = (state->current_word << (32 - state->bits_left)) >> (32 - num_bits);
114 state->bits_left -= num_bits;
115 return result;
118 return a52_bitstream_get_bh (state, num_bits);
119 #endif
122 static inline void bitstream_skip(a52_state_t * state, int num_bits)
124 #ifdef ALT_BITSTREAM_READER
125 indx+= num_bits;
126 #else
127 bitstream_get(state, num_bits);
128 #endif
131 static inline int32_t bitstream_get_2 (a52_state_t * state, uint32_t num_bits)
133 #ifdef ALT_BITSTREAM_READER
134 int32_t result= swab32( unaligned32(((uint8_t *)state->buffer_start)+(indx>>3)) );
136 result<<= (indx&0x07);
137 result>>= 32 - num_bits;
138 indx+= num_bits;
140 return result;
141 #else
142 int32_t result;
144 if (num_bits < state->bits_left) {
145 result = (((int32_t)state->current_word) << (32 - state->bits_left)) >> (32 - num_bits);
146 state->bits_left -= num_bits;
147 return result;
150 return a52_bitstream_get_bh_2 (state, num_bits);
151 #endif