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 a bit is
\r
32 * set, the corresponding word in the buffer will be non-zero. If a bit is
\r
33 * clear, the corresponding word is zero. Although this may seem waseful, this
\r
34 * can be a factor of two quicker than packing 8 bits to a byte and extracting.
\r
38 // REVIEW: there is no range checking, so buffer underflow or overflow
\r
39 // can silently occur.
\r
40 @LATTICE("BUF<OFF,BUF<BIT,BIT*,OFF*")
\r
41 @METHODDEFAULT("OUT<THIS,THIS<IN,IN*,THISLOC=THIS,RETURNLOC=OUT")
\r
42 final class BitReserve {
\r
44 * Size of the internal buffer to store the reserved bits. Must be a power of
\r
45 * 2. And x8, as each bit is stored as a single entry.
\r
47 private static final int BUFSIZE = 4096 * 8;
\r
50 * Mask that can be used to quickly implement the modulus operation on
\r
53 private static final int BUFSIZE_MASK = BUFSIZE - 1;
\r
62 private int buf_byte_idx;
\r
65 private final int[] buf;
\r
71 buf = new int[BUFSIZE];
\r
75 * Return totbit Field.
\r
77 @RETURNLOC("THIS,BitReserve.BIT")
\r
78 public int hsstell() {
\r
83 * Read a number bits from the bit stream.
\r
88 public int hgetbits(@LOC("THIS,BitReserve.BIT") int N) {
\r
92 @LOC("OUT") int val = 0;
\r
94 @LOC("THIS,BitReserve.BIT") int pos = buf_byte_idx;
\r
95 if (pos + N < BUFSIZE) {
\r
98 val |= ((buf[pos++] != 0) ? 1 : 0);
\r
103 val |= ((buf[pos] != 0) ? 1 : 0);
\r
104 pos = (pos + 1) & BUFSIZE_MASK;
\r
108 buf_byte_idx = pos;
\r
115 * Returns next bit from reserve.
\r
117 * @returns 0 if next bit is reset, or 1 if next bit is set.
\r
119 @RETURNLOC("THIS,BitReserve.BIT")
\r
120 public int hget1bit() {
\r
122 @LOC("THIS,BitReserve.BIT") int val = buf[buf_byte_idx];
\r
123 buf_byte_idx = (buf_byte_idx + 1) & BUFSIZE_MASK;
\r
128 * Write 8 bits into the bit stream.
\r
130 @LATTICE("OUT<THIS,THIS<IN,THISLOC=THIS,GLOBALLOC=IN")
\r
131 public void hputbuf(@LOC("IN") int val) {
\r
132 @LOC("THIS,BitReserve.OFF") int ofs = offset;
\r
133 buf[ofs++] = val & 0x80;
\r
134 buf[ofs++] = val & 0x40;
\r
135 buf[ofs++] = val & 0x20;
\r
136 buf[ofs++] = val & 0x10;
\r
137 buf[ofs++] = val & 0x08;
\r
138 buf[ofs++] = val & 0x04;
\r
139 buf[ofs++] = val & 0x02;
\r
140 buf[ofs++] = val & 0x01;
\r
142 if (ofs == BUFSIZE)
\r
150 * Rewind N bits in Stream.
\r
152 public void rewindNbits(@LOC("THIS,BitReserve.BIT") int N) {
\r
155 if (buf_byte_idx < 0)
\r
156 buf_byte_idx += BUFSIZE;
\r
160 * Rewind N bytes in Stream.
\r
162 @LATTICE("THIS<BIT,BIT<N,THISLOC=THIS,GLOBALLOC=N")
\r
163 public void rewindNbytes(@LOC("N") int N) {
\r
164 @LOC("BIT") int bits = (N << 3);
\r
166 buf_byte_idx -= bits;
\r
167 if (buf_byte_idx < 0)
\r
168 buf_byte_idx += BUFSIZE;
\r