annotate liba52/bitstream.h @ 8763:19e96e60a3d0

Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
author anders
date Sat, 04 Jan 2003 06:19:25 +0000
parents 34b5a0b12558
children f9eac474eb74
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 *
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
9 * a52dec is free software; you can redistribute it and/or modify
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
12 * (at your option) any later version.
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
13 *
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
14 * a52dec is distributed in the hope that it will be useful,
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
17 * GNU General Public License for more details.
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
18 *
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
20 * along with this program; if not, write to the Free Software
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
22 */
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
23
7255
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
24 #ifdef __sparc__
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
25 /*
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
26 * the alt bitstream reader performs unaligned memory accesses; that doesn't work
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
27 * on sparc. For now, disable ALT_BITSTREAM_READER.
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
28 */
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
29 #undef ALT_BITSTREAM_READER
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
30 #else
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
31 // 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
32 #define ALT_BITSTREAM_READER
7255
34b5a0b12558 The ALT_BITSTREAM_READER code does not work on SPARC, because the code accesses
jkeil
parents: 4053
diff changeset
33 #endif
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
34
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
35 /* (stolen from the kernel) */
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
36 #ifdef WORDS_BIGENDIAN
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
37
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
38 # define swab32(x) (x)
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
39
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
40 #else
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
41
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
42 # if defined (__i386__)
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
43
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
44 # define swab32(x) __i386_swab32(x)
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
45 static inline const uint32_t __i386_swab32(uint32_t x)
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
46 {
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
47 __asm__("bswap %0" : "=r" (x) : "0" (x));
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
48 return x;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
49 }
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
50
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
51 # else
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
52
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
53 # define swab32(x)\
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
54 ((((uint8_t*)&x)[0] << 24) | (((uint8_t*)&x)[1] << 16) | \
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
55 (((uint8_t*)&x)[2] << 8) | (((uint8_t*)&x)[3]))
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
56
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
57 # endif
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
58 #endif
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
59
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
60 #ifdef ALT_BITSTREAM_READER
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
61 extern uint32_t *buffer_start;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
62 extern int indx;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
63 #else
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
64 extern uint32_t bits_left;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
65 extern uint32_t current_word;
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
66 #endif
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
67
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
68 void bitstream_set_ptr (uint8_t * buf);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
69 uint32_t bitstream_get_bh(uint32_t num_bits);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
70 int32_t bitstream_get_bh_2(uint32_t num_bits);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
71
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
72
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
73 static inline uint32_t
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
74 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
75 {
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
76 #ifdef ALT_BITSTREAM_READER
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
77 uint32_t result= swab32( *(uint32_t *)(((uint8_t *)buffer_start)+(indx>>3)) );
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
78
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
79 result<<= (indx&0x07);
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
80 result>>= 32 - num_bits;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
81 indx+= num_bits;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
82
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
83 return result;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
84 #else
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
85 uint32_t result;
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
86
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
87 if(num_bits < bits_left) {
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
88 result = (current_word << (32 - bits_left)) >> (32 - num_bits);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
89 bits_left -= num_bits;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
90 return result;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
91 }
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
92
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
93 return bitstream_get_bh(num_bits);
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
94 #endif
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
95 }
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
96
4053
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
97 static inline void bitstream_skip(int num_bits)
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
98 {
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
99 #ifdef ALT_BITSTREAM_READER
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
100 indx+= num_bits;
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
101 #else
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
102 bitstream_get(num_bits);
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
103 #endif
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
104 }
75415651e3b9 bitstream_skip() instead of bitstream_get() if possible
michael
parents: 3570
diff changeset
105
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
106 static inline int32_t
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
107 bitstream_get_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 #ifdef ALT_BITSTREAM_READER
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
110 int32_t result= swab32( *(uint32_t *)(((uint8_t *)buffer_start)+(indx>>3)) );
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
111
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
112 result<<= (indx&0x07);
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
113 result>>= 32 - num_bits;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
114 indx+= num_bits;
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 return result;
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
117 #else
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
118 int32_t result;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
119
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
120 if(num_bits < bits_left) {
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
121 result = (((int32_t)current_word) << (32 - bits_left)) >> (32 - num_bits);
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
122 bits_left -= num_bits;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
123 return result;
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
124 }
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
125
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
126 return bitstream_get_bh_2(num_bits);
3570
c03e0bb35c16 faster (and simpler) bitstream reader (in C)
michael
parents: 3394
diff changeset
127 #endif
3394
35b18ed357c2 imported from liba52 CVS
arpi
parents:
diff changeset
128 }