Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/javazoom/jl/decoder/BitReserve.java
8650 views
1
/*
2
* 11/19/04 1.0 moved to LGPL.
3
*
4
* 12/12/99 0.0.7 Implementation stores single bits
5
* as ints for better performance. [email protected].
6
*
7
* 02/28/99 0.0 Java Conversion by E.B, [email protected]
8
*
9
* Adapted from the public c code by Jeff Tsay.
10
*
11
*-----------------------------------------------------------------------
12
* This program is free software; you can redistribute it and/or modify
13
* it under the terms of the GNU Library General Public License as published
14
* by the Free Software Foundation; either version 2 of the License, or
15
* (at your option) any later version.
16
*
17
* This program is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
* GNU Library General Public License for more details.
21
*
22
* You should have received a copy of the GNU Library General Public
23
* License along with this program; if not, write to the Free Software
24
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
*----------------------------------------------------------------------
26
*/
27
28
package javazoom.jl.decoder;
29
30
/**
31
* Implementation of Bit Reservoir for Layer III.
32
* <p>
33
* The implementation stores single bits as a word in the buffer. If
34
* a bit is set, the corresponding word in the buffer will be non-zero.
35
* If a bit is clear, the corresponding word is zero. Although this
36
* may seem wasteful, this can be a factor of two quicker than
37
* packing 8 bits to a byte and extracting.
38
* <p>
39
*/
40
41
// REVIEW: there is no range checking, so buffer underflow or overflow
42
// can silently occur.
43
final class BitReserve
44
{
45
/**
46
* Size of the internal buffer to store the reserved bits.
47
* Must be a power of 2. And x8, as each bit is stored as a single
48
* entry.
49
*/
50
private static final int BUFSIZE = 4096*8;
51
52
/**
53
* Mask that can be used to quickly implement the
54
* modulus operation on BUFSIZE.
55
*/
56
private static final int BUFSIZE_MASK = BUFSIZE-1;
57
58
private int offset, totbit, buf_byte_idx;
59
private final int[] buf = new int[BUFSIZE];
60
private int buf_bit_idx;
61
62
BitReserve()
63
{
64
65
offset = 0;
66
totbit = 0;
67
buf_byte_idx = 0;
68
}
69
70
71
/**
72
* Return totbit Field.
73
*/
74
public int hsstell()
75
{
76
return(totbit);
77
}
78
79
/**
80
* Read a number bits from the bit stream.
81
* @param N the number of
82
*/
83
public int hgetbits(int N)
84
{
85
totbit += N;
86
87
int val = 0;
88
89
int pos = buf_byte_idx;
90
if (pos+N < BUFSIZE)
91
{
92
while (N-- > 0)
93
{
94
val <<= 1;
95
val |= ((buf[pos++]!=0) ? 1 : 0);
96
}
97
}
98
else
99
{
100
while (N-- > 0)
101
{
102
val <<= 1;
103
val |= ((buf[pos]!=0) ? 1 : 0);
104
pos = (pos+1) & BUFSIZE_MASK;
105
}
106
}
107
buf_byte_idx = pos;
108
return val;
109
}
110
111
112
113
/**
114
* Read 1 bit from the bit stream.
115
*/
116
/*
117
public int hget1bit_old()
118
{
119
int val;
120
totbit++;
121
if (buf_bit_idx == 0)
122
{
123
buf_bit_idx = 8;
124
buf_byte_idx++;
125
}
126
// BUFSIZE = 4096 = 2^12, so
127
// buf_byte_idx%BUFSIZE == buf_byte_idx & 0xfff
128
val = buf[buf_byte_idx & BUFSIZE_MASK] & putmask[buf_bit_idx];
129
buf_bit_idx--;
130
val = val >>> buf_bit_idx;
131
return val;
132
}
133
*/
134
/**
135
* Returns next bit from reserve.
136
*
137
* @return 0 if next bit is reset, or 1 if next bit is set.
138
*/
139
public int hget1bit()
140
{
141
totbit++;
142
int val = buf[buf_byte_idx];
143
buf_byte_idx = (buf_byte_idx+1) & BUFSIZE_MASK;
144
return val;
145
}
146
147
/**
148
* Retrieves bits from the reserve.
149
*/
150
/*
151
public int readBits(int[] out, int len)
152
{
153
if (buf_bit_idx == 0)
154
{
155
buf_bit_idx = 8;
156
buf_byte_idx++;
157
current = buf[buf_byte_idx & BUFSIZE_MASK];
158
}
159
160
161
162
// save total number of bits returned
163
len = buf_bit_idx;
164
buf_bit_idx = 0;
165
166
int b = current;
167
int count = len-1;
168
169
while (count >= 0)
170
{
171
out[count--] = (b & 0x1);
172
b >>>= 1;
173
}
174
175
totbit += len;
176
return len;
177
}
178
*/
179
180
/**
181
* Write 8 bits into the bit stream.
182
*/
183
public void hputbuf(int val)
184
{
185
int ofs = offset;
186
buf[ofs++] = val & 0x80;
187
buf[ofs++] = val & 0x40;
188
buf[ofs++] = val & 0x20;
189
buf[ofs++] = val & 0x10;
190
buf[ofs++] = val & 0x08;
191
buf[ofs++] = val & 0x04;
192
buf[ofs++] = val & 0x02;
193
buf[ofs++] = val & 0x01;
194
195
if (ofs==BUFSIZE)
196
offset = 0;
197
else
198
offset = ofs;
199
200
}
201
202
/**
203
* Rewind N bits in Stream.
204
*/
205
public void rewindNbits(int N)
206
{
207
totbit -= N;
208
buf_byte_idx -= N;
209
if (buf_byte_idx<0)
210
buf_byte_idx += BUFSIZE;
211
}
212
213
/**
214
* Rewind N bytes in Stream.
215
*/
216
public void rewindNbytes(int N)
217
{
218
int bits = (N << 3);
219
totbit -= bits;
220
buf_byte_idx -= bits;
221
if (buf_byte_idx<0)
222
buf_byte_idx += BUFSIZE;
223
}
224
}
225
226