2
* 11/19/04 1.0 moved to LGPL.
4
* 12/12/99 0.0.7 Implementation stores single bits
5
* as ints for better performance. mdm@techie.com.
7
* 02/28/99 0.0 Java Conversion by E.B, javalayer@javazoom.net
9
* Adapted from the public c code by Jeff Tsay.
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.
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.
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
*----------------------------------------------------------------------
28
package javazoom.jl.decoder;
31
* Implementation of Bit Reservoir for Layer III.
33
* The implementation stores single bits as a word in the buffer. If a bit is
34
* set, the corresponding word in the buffer will be non-zero. If a bit is
35
* clear, the corresponding word is zero. Although this may seem waseful, this
36
* can be a factor of two quicker than packing 8 bits to a byte and extracting.
40
// REVIEW: there is no range checking, so buffer underflow or overflow
41
// can silently occur.
42
final class BitReserve {
44
* Size of the internal buffer to store the reserved bits. Must be a power
45
* of 2. And x8, as each bit is stored as a single entry.
47
private static final int BUFSIZE = 4096 * 8;
50
* Mask that can be used to quickly implement the modulus operation on
53
private static final int BUFSIZE_MASK = BUFSIZE - 1;
55
private int offset, totbit, buf_byte_idx;
57
private final int[] buf = new int[BUFSIZE];
59
private int buf_bit_idx;
69
* Return totbit Field.
71
public int hsstell() {
76
* Read a number bits from the bit stream.
81
public int hgetbits(int N) {
86
int pos = buf_byte_idx;
87
if (pos + N < BUFSIZE) {
90
val |= ((buf[pos++] != 0) ? 1 : 0);
95
val |= ((buf[pos] != 0) ? 1 : 0);
96
pos = (pos + 1) & BUFSIZE_MASK;
104
* Read 1 bit from the bit stream.
107
* public int hget1bit_old() { int val; totbit++; if (buf_bit_idx == 0) {
108
* buf_bit_idx = 8; buf_byte_idx++; } // BUFSIZE = 4096 = 2^12, so //
109
* buf_byte_idx%BUFSIZE == buf_byte_idx & 0xfff val = buf[buf_byte_idx &
110
* BUFSIZE_MASK] & putmask[buf_bit_idx]; buf_bit_idx--; val = val >>>
111
* buf_bit_idx; return val; }
114
* Returns next bit from reserve.
116
* @returns 0 if next bit is reset, or 1 if next bit is set.
118
public int hget1bit() {
120
int val = buf[buf_byte_idx];
121
buf_byte_idx = (buf_byte_idx + 1) & BUFSIZE_MASK;
126
* Retrieves bits from the reserve.
129
* public int readBits(int[] out, int len) { if (buf_bit_idx == 0) {
130
* buf_bit_idx = 8; buf_byte_idx++; current = buf[buf_byte_idx &
134
* // save total number of bits returned len = buf_bit_idx; buf_bit_idx =
137
* int b = current; int count = len-1;
139
* while (count >= 0) { out[count--] = (b & 0x1); b >>>= 1; }
141
* totbit += len; return len; }
145
* Write 8 bits into the bit stream.
147
public void hputbuf(int val) {
149
buf[ofs++] = val & 0x80;
150
buf[ofs++] = val & 0x40;
151
buf[ofs++] = val & 0x20;
152
buf[ofs++] = val & 0x10;
153
buf[ofs++] = val & 0x08;
154
buf[ofs++] = val & 0x04;
155
buf[ofs++] = val & 0x02;
156
buf[ofs++] = val & 0x01;
166
* Rewind N bits in Stream.
168
public void rewindNbits(int N) {
171
if (buf_byte_idx < 0)
172
buf_byte_idx += BUFSIZE;
176
* Rewind N bytes in Stream.
178
public void rewindNbytes(int N) {
181
buf_byte_idx -= bits;
182
if (buf_byte_idx < 0)
183
buf_byte_idx += BUFSIZE;