annotate liba52/bitstream.h @ 17700:a0c511c2d0bd

update config.mak to match ffmpeg's version (fix alpha compilation)
author aurel
date Mon, 27 Feb 2006 22:44:22 +0000
parents 07f1e7669772
children 4bad7f00556e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
1 /*
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
2 * bitstream.h
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
3 * Copyright (C) 2000-2001 Michel Lespinasse <walken@zoy.org>
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
5 *
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
7 * See http://liba52.sourceforge.net/ for updates.
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
8 *
14991
07f1e7669772 Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents: 10489
diff changeset
9 * Modified for use with MPlayer, changes contained in liba52_changes.diff.
07f1e7669772 Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents: 10489
diff changeset
10 * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
07f1e7669772 Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents: 10489
diff changeset
11 * $Id$
07f1e7669772 Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents: 10489
diff changeset
12 *
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
13 * a52dec is free software; you can redistribute it and/or modify
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
14 * it under the terms of the GNU General Public License as published by
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
15 * the Free Software Foundation; either version 2 of the License, or
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
16 * (at your option) any later version.
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
17 *
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
18 * a52dec is distributed in the hope that it will be useful,
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
21 * GNU General Public License for more details.
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
22 *
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
23 * You should have received a copy of the GNU General Public License
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
24 * along with this program; if not, write to the Free Software
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
26 */
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
27
10489
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
28 /* code from ffmpeg/libavcodec */
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
29 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC_ == 3 && __GNUC_MINOR__ > 0)
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
30 # define always_inline __attribute__((always_inline)) inline
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
31 #else
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
32 # define always_inline inline
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
33 #endif
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
34
10067
f9eac474eb74 this is a important patch for hpux 11.00, because it avoid the
diego
parents: 7255
diff changeset
35 #if defined(__sparc__) || defined(hpux)
7255
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
36 /*
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
37 * the alt bitstream reader performs unaligned memory accesses; that doesn't work
10067
f9eac474eb74 this is a important patch for hpux 11.00, because it avoid the
diego
parents: 7255
diff changeset
38 * on sparc/hpux. For now, disable ALT_BITSTREAM_READER.
7255
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
39 */
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
40 #undef ALT_BITSTREAM_READER
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
41 #else
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
42 // alternative (faster) bitstram reader (reades upto 3 bytes over the end of the input)
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
43 #define ALT_BITSTREAM_READER
10082
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
44
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
45 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
10313
b479b224d435 arm support by armin.gerritsen@philips.com (also seen a patch like this on openzaurus' mplayer port)
alex
parents: 10082
diff changeset
46 #if defined (ARCH_X86) || defined(ARCH_ARMV4L)
10082
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
47 # define unaligned32(a) (*(uint32_t*)(a))
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
48 #else
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
49 # ifdef __GNUC__
10489
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
50 static always_inline uint32_t unaligned32(const void *v) {
10082
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
51 struct Unaligned {
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
52 uint32_t i;
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
53 } __attribute__((packed));
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
54
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
55 return ((const struct Unaligned *) v)->i;
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
56 }
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
57 # elif defined(__DECC)
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
58 static inline uint32_t unaligned32(const void *v) {
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
59 return *(const __unaligned uint32_t *) v;
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
60 }
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
61 # else
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
62 static inline uint32_t unaligned32(const void *v) {
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
63 return *(const uint32_t *) v;
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
64 }
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
65 # endif
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
66 #endif //!ARCH_X86
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
67
7255
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
68 #endif
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
69
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
70 /* (stolen from the kernel) */
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
71 #ifdef WORDS_BIGENDIAN
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
72
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
73 # define swab32(x) (x)
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
74
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
75 #else
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
76
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
77 # if defined (__i386__)
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
78
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
79 # define swab32(x) __i386_swab32(x)
10489
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
80 static always_inline const uint32_t __i386_swab32(uint32_t x)
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
81 {
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
82 __asm__("bswap %0" : "=r" (x) : "0" (x));
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
83 return x;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
84 }
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
85
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
86 # else
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
87
10489
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
88 # define swab32(x) __generic_swab32(x)
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
89 static always_inline const uint32_t __generic_swab32(uint32_t x)
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
90 {
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
91 return ((((uint8_t*)&x)[0] << 24) | (((uint8_t*)&x)[1] << 16) |
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
92 (((uint8_t*)&x)[2] << 8) | (((uint8_t*)&x)[3]));
b7a6edb6100e Changed swab32 from macro to inline function, this fixes compilation on alpha (with gcc2.95). Based on patch by KotH
alex
parents: 10313
diff changeset
93 }
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
94 # endif
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
95 #endif
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
96
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
97 #ifdef ALT_BITSTREAM_READER
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
98 extern uint32_t *buffer_start;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
99 extern int indx;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
100 #else
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
101 extern uint32_t bits_left;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
102 extern uint32_t current_word;
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
103 #endif
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
104
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
105 void bitstream_set_ptr (uint8_t * buf);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
106 uint32_t bitstream_get_bh(uint32_t num_bits);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
107 int32_t bitstream_get_bh_2(uint32_t num_bits);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
108
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
109
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
110 static inline uint32_t
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
111 bitstream_get(uint32_t num_bits) // note num_bits is practically a constant due to inlineing
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
112 {
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
113 #ifdef ALT_BITSTREAM_READER
10082
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
114 uint32_t result= swab32( unaligned32(((uint8_t *)buffer_start)+(indx>>3)) );
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
115
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
116 result<<= (indx&0x07);
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
117 result>>= 32 - num_bits;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
118 indx+= num_bits;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
119
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
120 return result;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
121 #else
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
122 uint32_t result;
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
123
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
124 if(num_bits < bits_left) {
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
125 result = (current_word << (32 - bits_left)) >> (32 - num_bits);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
126 bits_left -= num_bits;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
127 return result;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
128 }
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
129
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
130 return bitstream_get_bh(num_bits);
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
131 #endif
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
132 }
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
133
4053
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
134 static inline void bitstream_skip(int num_bits)
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
135 {
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
136 #ifdef ALT_BITSTREAM_READER
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
137 indx+= num_bits;
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
138 #else
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
139 bitstream_get(num_bits);
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
140 #endif
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
141 }
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
142
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
143 static inline int32_t
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
144 bitstream_get_2(uint32_t num_bits)
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
145 {
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
146 #ifdef ALT_BITSTREAM_READER
10082
1c95e3d5888a fixed alignment bug
michael
parents: 10067
diff changeset
147 int32_t result= swab32( unaligned32(((uint8_t *)buffer_start)+(indx>>3)) );
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
148
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
149 result<<= (indx&0x07);
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
150 result>>= 32 - num_bits;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
151 indx+= num_bits;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
152
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
153 return result;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
154 #else
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
155 int32_t result;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
156
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
157 if(num_bits < bits_left) {
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
158 result = (((int32_t)current_word) << (32 - bits_left)) >> (32 - num_bits);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
159 bits_left -= num_bits;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
160 return result;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
161 }
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
162
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
163 return bitstream_get_bh_2(num_bits);
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
164 #endif
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
165 }