Path: blob/main/src/lwjgl/java/javazoom/jl/decoder/BitReserve.java
8650 views
/*1* 11/19/04 1.0 moved to LGPL.2*3* 12/12/99 0.0.7 Implementation stores single bits4* as ints for better performance. [email protected].5*6* 02/28/99 0.0 Java Conversion by E.B, [email protected]7*8* Adapted from the public c code by Jeff Tsay.9*10*-----------------------------------------------------------------------11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU Library General Public License as published13* by the Free Software Foundation; either version 2 of the License, or14* (at your option) any later version.15*16* This program is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19* GNU Library General Public License for more details.20*21* You should have received a copy of the GNU Library General Public22* License along with this program; if not, write to the Free Software23* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.24*----------------------------------------------------------------------25*/2627package javazoom.jl.decoder;2829/**30* Implementation of Bit Reservoir for Layer III.31* <p>32* The implementation stores single bits as a word in the buffer. If33* a bit is set, the corresponding word in the buffer will be non-zero.34* If a bit is clear, the corresponding word is zero. Although this35* may seem wasteful, this can be a factor of two quicker than36* packing 8 bits to a byte and extracting.37* <p>38*/3940// REVIEW: there is no range checking, so buffer underflow or overflow41// can silently occur.42final class BitReserve43{44/**45* Size of the internal buffer to store the reserved bits.46* Must be a power of 2. And x8, as each bit is stored as a single47* entry.48*/49private static final int BUFSIZE = 4096*8;5051/**52* Mask that can be used to quickly implement the53* modulus operation on BUFSIZE.54*/55private static final int BUFSIZE_MASK = BUFSIZE-1;5657private int offset, totbit, buf_byte_idx;58private final int[] buf = new int[BUFSIZE];59private int buf_bit_idx;6061BitReserve()62{6364offset = 0;65totbit = 0;66buf_byte_idx = 0;67}686970/**71* Return totbit Field.72*/73public int hsstell()74{75return(totbit);76}7778/**79* Read a number bits from the bit stream.80* @param N the number of81*/82public int hgetbits(int N)83{84totbit += N;8586int val = 0;8788int pos = buf_byte_idx;89if (pos+N < BUFSIZE)90{91while (N-- > 0)92{93val <<= 1;94val |= ((buf[pos++]!=0) ? 1 : 0);95}96}97else98{99while (N-- > 0)100{101val <<= 1;102val |= ((buf[pos]!=0) ? 1 : 0);103pos = (pos+1) & BUFSIZE_MASK;104}105}106buf_byte_idx = pos;107return val;108}109110111112/**113* Read 1 bit from the bit stream.114*/115/*116public int hget1bit_old()117{118int val;119totbit++;120if (buf_bit_idx == 0)121{122buf_bit_idx = 8;123buf_byte_idx++;124}125// BUFSIZE = 4096 = 2^12, so126// buf_byte_idx%BUFSIZE == buf_byte_idx & 0xfff127val = buf[buf_byte_idx & BUFSIZE_MASK] & putmask[buf_bit_idx];128buf_bit_idx--;129val = val >>> buf_bit_idx;130return val;131}132*/133/**134* Returns next bit from reserve.135*136* @return 0 if next bit is reset, or 1 if next bit is set.137*/138public int hget1bit()139{140totbit++;141int val = buf[buf_byte_idx];142buf_byte_idx = (buf_byte_idx+1) & BUFSIZE_MASK;143return val;144}145146/**147* Retrieves bits from the reserve.148*/149/*150public int readBits(int[] out, int len)151{152if (buf_bit_idx == 0)153{154buf_bit_idx = 8;155buf_byte_idx++;156current = buf[buf_byte_idx & BUFSIZE_MASK];157}158159160161// save total number of bits returned162len = buf_bit_idx;163buf_bit_idx = 0;164165int b = current;166int count = len-1;167168while (count >= 0)169{170out[count--] = (b & 0x1);171b >>>= 1;172}173174totbit += len;175return len;176}177*/178179/**180* Write 8 bits into the bit stream.181*/182public void hputbuf(int val)183{184int ofs = offset;185buf[ofs++] = val & 0x80;186buf[ofs++] = val & 0x40;187buf[ofs++] = val & 0x20;188buf[ofs++] = val & 0x10;189buf[ofs++] = val & 0x08;190buf[ofs++] = val & 0x04;191buf[ofs++] = val & 0x02;192buf[ofs++] = val & 0x01;193194if (ofs==BUFSIZE)195offset = 0;196else197offset = ofs;198199}200201/**202* Rewind N bits in Stream.203*/204public void rewindNbits(int N)205{206totbit -= N;207buf_byte_idx -= N;208if (buf_byte_idx<0)209buf_byte_idx += BUFSIZE;210}211212/**213* Rewind N bytes in Stream.214*/215public void rewindNbytes(int N)216{217int bits = (N << 3);218totbit -= bits;219buf_byte_idx -= bits;220if (buf_byte_idx<0)221buf_byte_idx += BUFSIZE;222}223}224225226