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 *----------------------------------------------------------------------
29 * Implementation of Bit Reservoir for Layer III.
31 * The implementation stores single bits as a word in the buffer. If a bit is
32 * set, the corresponding word in the buffer will be non-zero. If a bit is
33 * clear, the corresponding word is zero. Although this may seem waseful, this
34 * can be a factor of two quicker than packing 8 bits to a byte and extracting.
38 // REVIEW: there is no range checking, so buffer underflow or overflow
39 // can silently occur.
40 @LATTICE("BUF<OFF,BUF<BIT,BIT*,OFF*")
41 @METHODDEFAULT("OUT<THIS,THIS<IN,IN*,OUT*,THISLOC=THIS,RETURNLOC=OUT")
42 final class BitReserve {
44 * Size of the internal buffer to store the reserved bits. Must be a power of
45 * 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;
62 public int buf_byte_idx;
65 private final int[] buf;
71 buf = new int[BUFSIZE];
75 * Return totbit Field.
77 @RETURNLOC("THIS,BitReserve.BIT")
78 public int hsstell() {
83 * Read a number bits from the bit stream.
88 @PCLOC("THIS,BitReserve.BIT")
89 public int hgetbits(@LOC("THIS,BitReserve.BIT") int N) {
93 @LOC("OUT") int val = 0;
95 @LOC("THIS,BitReserve.BIT") int pos = buf_byte_idx;
96 if (pos + N < BUFSIZE) {
97 TERMINATE: while (N-- > 0) {
99 val |= ((buf[pos++] != 0) ? 1 : 0);
102 TERMINATE: while (N-- > 0) {
104 val |= ((buf[pos] != 0) ? 1 : 0);
105 pos = (pos + 1) & BUFSIZE_MASK;
116 * Returns next bit from reserve.
118 * @returns 0 if next bit is reset, or 1 if next bit is set.
120 @RETURNLOC("THIS,BitReserve.BIT")
121 public int hget1bit() {
123 @LOC("THIS,BitReserve.BIT") int val = buf[buf_byte_idx];
124 buf_byte_idx = (buf_byte_idx + 1) & BUFSIZE_MASK;
129 * Write 8 bits into the bit stream.
131 @LATTICE("OUT<THIS,THIS<IN,THISLOC=THIS,GLOBALLOC=IN")
132 public void hputbuf(@LOC("IN") int val) {
133 @LOC("THIS,BitReserve.OFF") int ofs = offset;
134 buf[ofs++] = val & 0x80;
135 buf[ofs++] = val & 0x40;
136 buf[ofs++] = val & 0x20;
137 buf[ofs++] = val & 0x10;
138 buf[ofs++] = val & 0x08;
139 buf[ofs++] = val & 0x04;
140 buf[ofs++] = val & 0x02;
141 buf[ofs++] = val & 0x01;
151 * Rewind N bits in Stream.
153 public void rewindNbits(@LOC("THIS,BitReserve.BIT") int N) {
156 if (buf_byte_idx < 0)
157 buf_byte_idx += BUFSIZE;
161 * Rewind N bytes in Stream.
163 @LATTICE("THIS<BIT,BIT<N,THISLOC=THIS,GLOBALLOC=N")
164 public void rewindNbytes(@LOC("N") int N) {
165 @LOC("BIT") int bits = (N << 3);
167 buf_byte_idx -= bits;
168 if (buf_byte_idx < 0)
169 buf_byte_idx += BUFSIZE;