3394
|
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>
|
|
5 *
|
|
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
|
|
7 * See http://liba52.sourceforge.net/ for updates.
|
|
8 *
|
|
9 * a52dec is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * a52dec is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 */
|
|
23
|
3570
|
24 // alternative (faster) bitstram reader (reades upto 3 bytes over the end of the input)
|
|
25 #define ALT_BITSTREAM_READER
|
|
26
|
3394
|
27 /* (stolen from the kernel) */
|
|
28 #ifdef WORDS_BIGENDIAN
|
|
29
|
|
30 # define swab32(x) (x)
|
|
31
|
|
32 #else
|
|
33
|
|
34 # if defined (__i386__)
|
|
35
|
|
36 # define swab32(x) __i386_swab32(x)
|
|
37 static inline const uint32_t __i386_swab32(uint32_t x)
|
|
38 {
|
|
39 __asm__("bswap %0" : "=r" (x) : "0" (x));
|
|
40 return x;
|
|
41 }
|
|
42
|
|
43 # else
|
|
44
|
|
45 # define swab32(x)\
|
|
46 ((((uint8_t*)&x)[0] << 24) | (((uint8_t*)&x)[1] << 16) | \
|
|
47 (((uint8_t*)&x)[2] << 8) | (((uint8_t*)&x)[3]))
|
|
48
|
|
49 # endif
|
|
50 #endif
|
|
51
|
3570
|
52 #ifdef ALT_BITSTREAM_READER
|
|
53 extern uint32_t *buffer_start;
|
|
54 extern int indx;
|
|
55 #else
|
3394
|
56 extern uint32_t bits_left;
|
|
57 extern uint32_t current_word;
|
3570
|
58 #endif
|
3394
|
59
|
|
60 void bitstream_set_ptr (uint8_t * buf);
|
|
61 uint32_t bitstream_get_bh(uint32_t num_bits);
|
|
62 int32_t bitstream_get_bh_2(uint32_t num_bits);
|
|
63
|
3570
|
64
|
3394
|
65 static inline uint32_t
|
3570
|
66 bitstream_get(uint32_t num_bits) // note num_bits is practically a constant due to inlineing
|
3394
|
67 {
|
3570
|
68 #ifdef ALT_BITSTREAM_READER
|
|
69 uint32_t result= swab32( *(uint32_t *)(((uint8_t *)buffer_start)+(indx>>3)) );
|
|
70
|
|
71 result<<= (indx&0x07);
|
|
72 result>>= 32 - num_bits;
|
|
73 indx+= num_bits;
|
|
74
|
|
75 return result;
|
|
76 #else
|
3394
|
77 uint32_t result;
|
3570
|
78
|
3394
|
79 if(num_bits < bits_left) {
|
|
80 result = (current_word << (32 - bits_left)) >> (32 - num_bits);
|
|
81 bits_left -= num_bits;
|
|
82 return result;
|
|
83 }
|
|
84
|
|
85 return bitstream_get_bh(num_bits);
|
3570
|
86 #endif
|
3394
|
87 }
|
|
88
|
|
89 static inline int32_t
|
|
90 bitstream_get_2(uint32_t num_bits)
|
|
91 {
|
3570
|
92 #ifdef ALT_BITSTREAM_READER
|
|
93 int32_t result= swab32( *(uint32_t *)(((uint8_t *)buffer_start)+(indx>>3)) );
|
|
94
|
|
95 result<<= (indx&0x07);
|
|
96 result>>= 32 - num_bits;
|
|
97 indx+= num_bits;
|
|
98
|
|
99 return result;
|
|
100 #else
|
3394
|
101 int32_t result;
|
|
102
|
|
103 if(num_bits < bits_left) {
|
|
104 result = (((int32_t)current_word) << (32 - bits_left)) >> (32 - num_bits);
|
|
105 bits_left -= num_bits;
|
|
106 return result;
|
|
107 }
|
|
108
|
|
109 return bitstream_get_bh_2(num_bits);
|
3570
|
110 #endif
|
3394
|
111 }
|