2 * 11/19/04 1.0 moved to LGPL.
\r
4 * 12/12/99 0.0.7 Implementation stores single bits
\r
5 * as ints for better performance. mdm@techie.com.
\r
7 * 02/28/99 0.0 Java Conversion by E.B, javalayer@javazoom.net
\r
9 * Adapted from the public c code by Jeff Tsay.
\r
11 *-----------------------------------------------------------------------
\r
12 * This program is free software; you can redistribute it and/or modify
\r
13 * it under the terms of the GNU Library General Public License as published
\r
14 * by the Free Software Foundation; either version 2 of the License, or
\r
15 * (at your option) any later version.
\r
17 * This program is distributed in the hope that it will be useful,
\r
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
20 * GNU Library General Public License for more details.
\r
22 * You should have received a copy of the GNU Library General Public
\r
23 * License along with this program; if not, write to the Free Software
\r
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\r
25 *----------------------------------------------------------------------
\r
29 * Implementation of Bit Reservoir for Layer III.
\r
31 * The implementation stores single bits as a word in the buffer. If
\r
32 * a bit is set, the corresponding word in the buffer will be non-zero.
\r
33 * If a bit is clear, the corresponding word is zero. Although this
\r
34 * may seem waseful, this can be a factor of two quicker than
\r
35 * packing 8 bits to a byte and extracting.
\r
39 // REVIEW: there is no range checking, so buffer underflow or overflow
\r
40 // can silently occur.
\r
41 final class BitReserve
\r
44 * Size of the internal buffer to store the reserved bits.
\r
45 * Must be a power of 2. And x8, as each bit is stored as a single
\r
48 private static final int BUFSIZE = 4096*8;
\r
51 * Mask that can be used to quickly implement the
\r
52 * modulus operation on BUFSIZE.
\r
54 private static final int BUFSIZE_MASK = BUFSIZE-1;
\r
56 private int offset, totbit, buf_byte_idx;
\r
57 private final int[] buf = new int[BUFSIZE];
\r
58 private int buf_bit_idx;
\r
70 * Return totbit Field.
\r
72 public int hsstell()
\r
78 * Read a number bits from the bit stream.
\r
79 * @param N the number of
\r
81 public int hgetbits(int N)
\r
87 int pos = buf_byte_idx;
\r
88 if (pos+N < BUFSIZE)
\r
93 val |= ((buf[pos++]!=0) ? 1 : 0);
\r
101 val |= ((buf[pos]!=0) ? 1 : 0);
\r
102 pos = (pos+1) & BUFSIZE_MASK;
\r
105 buf_byte_idx = pos;
\r
112 * Read 1 bit from the bit stream.
\r
115 public int hget1bit_old()
\r
119 if (buf_bit_idx == 0)
\r
124 // BUFSIZE = 4096 = 2^12, so
\r
125 // buf_byte_idx%BUFSIZE == buf_byte_idx & 0xfff
\r
126 val = buf[buf_byte_idx & BUFSIZE_MASK] & putmask[buf_bit_idx];
\r
128 val = val >>> buf_bit_idx;
\r
133 * Returns next bit from reserve.
\r
134 * @returns 0 if next bit is reset, or 1 if next bit is set.
\r
136 public int hget1bit()
\r
139 int val = buf[buf_byte_idx];
\r
140 buf_byte_idx = (buf_byte_idx+1) & BUFSIZE_MASK;
\r
145 * Retrieves bits from the reserve.
\r
148 public int readBits(int[] out, int len)
\r
150 if (buf_bit_idx == 0)
\r
154 current = buf[buf_byte_idx & BUFSIZE_MASK];
\r
159 // save total number of bits returned
\r
168 out[count--] = (b & 0x1);
\r
178 * Write 8 bits into the bit stream.
\r
180 public void hputbuf(int val)
\r
183 buf[ofs++] = val & 0x80;
\r
184 buf[ofs++] = val & 0x40;
\r
185 buf[ofs++] = val & 0x20;
\r
186 buf[ofs++] = val & 0x10;
\r
187 buf[ofs++] = val & 0x08;
\r
188 buf[ofs++] = val & 0x04;
\r
189 buf[ofs++] = val & 0x02;
\r
190 buf[ofs++] = val & 0x01;
\r
200 * Rewind N bits in Stream.
\r
202 public void rewindNbits(int N)
\r
206 if (buf_byte_idx<0)
\r
207 buf_byte_idx += BUFSIZE;
\r
211 * Rewind N bytes in Stream.
\r
213 public void rewindNbytes(int N)
\r
215 int bits = (N << 3);
\r
217 buf_byte_idx -= bits;
\r
218 if (buf_byte_idx<0)
\r
219 buf_byte_idx += BUFSIZE;
\r