summaryrefslogtreecommitdiff
path: root/songdbj/javazoom/jl/decoder/BitReserve.java
blob: a5d3056d6190e9fd0b3af6a9570ad92dd27cc91f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * 11/19/04			1.0 moved to LGPL.
 * 
 * 12/12/99 0.0.7	Implementation stores single bits 
 *					as ints for better performance. mdm@techie.com.
 *
 * 02/28/99 0.0     Java Conversion by E.B, javalayer@javazoom.net
 *
 *                  Adapted from the public c code by Jeff Tsay.
 *
 *-----------------------------------------------------------------------
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as published
 *   by the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

package javazoom.jl.decoder;
	
/**
 * Implementation of Bit Reservoir for Layer III.
 * <p>
 * The implementation stores single bits as a word in the buffer. If
 * a bit is set, the corresponding word in the buffer will be non-zero.
 * If a bit is clear, the corresponding word is zero. Although this
 * may seem waseful, this can be a factor of two quicker than 
 * packing 8 bits to a byte and extracting. 
 * <p> 
 */

// REVIEW: there is no range checking, so buffer underflow or overflow
// can silently occur.
final class BitReserve
{
   /**
    * Size of the internal buffer to store the reserved bits.
    * Must be a power of 2. And x8, as each bit is stored as a single
    * entry.
    */
	private static final int		BUFSIZE = 4096*8;
	
	/**
	 * Mask that can be used to quickly implement the
	 * modulus operation on BUFSIZE.
	 */
	private static final int		BUFSIZE_MASK = BUFSIZE-1;
	
	private int 					offset, totbit, buf_byte_idx;
	private final int[] 			buf = new int[BUFSIZE];
	private int 					buf_bit_idx;
	
   BitReserve()
   {
	  
	  offset = 0;
      totbit = 0;
      buf_byte_idx = 0;	  
   }
      
   
   /**
    * Return totbit Field.
	*/
   public int hsstell() 
   { 
	   return(totbit); 
   }

   /**
    * Read a number bits from the bit stream.
    * @param N the number of
	*/
   public int hgetbits(int N)
   {
   	 totbit += N;
	 
	 int val = 0;
	 
	 int pos = buf_byte_idx;
	 if (pos+N < BUFSIZE)
	 {
		while (N-- > 0)
		{
			val <<= 1;
			val |= ((buf[pos++]!=0) ? 1 : 0);		 
		}
	}
	 else
	 {	 
		 while (N-- > 0)
		{
			 val <<= 1;			 
			 val |= ((buf[pos]!=0) ? 1 : 0);
			 pos = (pos+1) & BUFSIZE_MASK;
		}		 
	 }	
	 buf_byte_idx = pos;
	 return val;
   }
   
	 
   
   /**
    * Read 1 bit from the bit stream.
	*/
/*
   public int hget1bit_old()
   {
   	  int val;
	  totbit++;
	  if (buf_bit_idx == 0)
	  {
         buf_bit_idx = 8;
	     buf_byte_idx++;		 
	  }
      // BUFSIZE = 4096 = 2^12, so
      // buf_byte_idx%BUFSIZE == buf_byte_idx & 0xfff
      val = buf[buf_byte_idx & BUFSIZE_MASK] & putmask[buf_bit_idx];
      buf_bit_idx--;
	  val = val >>> buf_bit_idx;
      return val;   
   }
 */
   /**
    * Returns next bit from reserve.
    * @returns 0 if next bit is reset, or 1 if next bit is set.
    */
   public int hget1bit()
   {   	  
	  totbit++;	  
	  int val = buf[buf_byte_idx];
	  buf_byte_idx = (buf_byte_idx+1) & BUFSIZE_MASK;
      return val;
   }
   
   /**
    * Retrieves bits from the reserve.     
    */
/*   
   public int readBits(int[] out, int len)
   {
		if (buf_bit_idx == 0)
		{
		   buf_bit_idx = 8;
		   buf_byte_idx++;
		   current = buf[buf_byte_idx & BUFSIZE_MASK];
		}      
		
		
		
		// save total number of bits returned
		len = buf_bit_idx;
		buf_bit_idx = 0;
		  
		int b = current;
		int count = len-1;
		  
		while (count >= 0)
		{
		    out[count--] = (b & 0x1);
		    b >>>= 1;
		}
	  
		totbit += len;
		return len;
   }
  */
   
   /**
    * Write 8 bits into the bit stream.
	*/
   public void hputbuf(int val)
   {   	  
	   int ofs = offset;
	   buf[ofs++] = val & 0x80;
	   buf[ofs++] = val & 0x40;
	   buf[ofs++] = val & 0x20;
	   buf[ofs++] = val & 0x10;
	   buf[ofs++] = val & 0x08;
	   buf[ofs++] = val & 0x04;
	   buf[ofs++] = val & 0x02;
	   buf[ofs++] = val & 0x01;
	   
	   if (ofs==BUFSIZE)
			offset = 0;
	   else
			offset = ofs;
	   
   }
 
   /**
    * Rewind N bits in Stream.
	*/
   public void rewindNbits(int N)
   {
 	  totbit -= N;	  	  
	  buf_byte_idx -= N;
	  if (buf_byte_idx<0)
		  buf_byte_idx += BUFSIZE;
   }
	
   /**
    * Rewind N bytes in Stream.
	*/
   public void rewindNbytes(int N)
   {
      int bits = (N << 3);
	  totbit -= bits;
	  buf_byte_idx -= bits;	  
	  if (buf_byte_idx<0)
		  buf_byte_idx += BUFSIZE;
   }
}