diff options
Diffstat (limited to 'songdbj/javazoom/jl/decoder/LayerIDecoder.java')
-rw-r--r-- | songdbj/javazoom/jl/decoder/LayerIDecoder.java | 444 |
1 files changed, 0 insertions, 444 deletions
diff --git a/songdbj/javazoom/jl/decoder/LayerIDecoder.java b/songdbj/javazoom/jl/decoder/LayerIDecoder.java deleted file mode 100644 index b633dd2403..0000000000 --- a/songdbj/javazoom/jl/decoder/LayerIDecoder.java +++ /dev/null | |||
@@ -1,444 +0,0 @@ | |||
1 | /* | ||
2 | * 11/19/04 1.0 moved to LGPL. | ||
3 | * | ||
4 | * 12/12/99 Initial version. Adapted from javalayer.java | ||
5 | * and Subband*.java. mdm@techie.com | ||
6 | * | ||
7 | * 02/28/99 Initial version : javalayer.java by E.B | ||
8 | *----------------------------------------------------------------------- | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU Library General Public License as published | ||
11 | * by the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU Library General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Library General Public | ||
20 | * License along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
22 | *---------------------------------------------------------------------- | ||
23 | */ | ||
24 | |||
25 | package javazoom.jl.decoder; | ||
26 | |||
27 | /** | ||
28 | * Implements decoding of MPEG Audio Layer I frames. | ||
29 | */ | ||
30 | class LayerIDecoder implements FrameDecoder | ||
31 | { | ||
32 | protected Bitstream stream; | ||
33 | protected Header header; | ||
34 | protected SynthesisFilter filter1, filter2; | ||
35 | protected Obuffer buffer; | ||
36 | protected int which_channels; | ||
37 | protected int mode; | ||
38 | |||
39 | protected int num_subbands; | ||
40 | protected Subband[] subbands; | ||
41 | protected Crc16 crc = null; // new Crc16[1] to enable CRC checking. | ||
42 | |||
43 | public LayerIDecoder() | ||
44 | { | ||
45 | crc = new Crc16(); | ||
46 | } | ||
47 | |||
48 | public void create(Bitstream stream0, Header header0, | ||
49 | SynthesisFilter filtera, SynthesisFilter filterb, | ||
50 | Obuffer buffer0, int which_ch0) | ||
51 | { | ||
52 | stream = stream0; | ||
53 | header = header0; | ||
54 | filter1 = filtera; | ||
55 | filter2 = filterb; | ||
56 | buffer = buffer0; | ||
57 | which_channels = which_ch0; | ||
58 | |||
59 | } | ||
60 | |||
61 | |||
62 | |||
63 | public void decodeFrame() | ||
64 | { | ||
65 | |||
66 | num_subbands = header.number_of_subbands(); | ||
67 | subbands = new Subband[32]; | ||
68 | mode = header.mode(); | ||
69 | |||
70 | createSubbands(); | ||
71 | |||
72 | readAllocation(); | ||
73 | readScaleFactorSelection(); | ||
74 | |||
75 | if ((crc != null) || header.checksum_ok()) | ||
76 | { | ||
77 | readScaleFactors(); | ||
78 | |||
79 | readSampleData(); | ||
80 | } | ||
81 | |||
82 | } | ||
83 | |||
84 | protected void createSubbands() | ||
85 | { | ||
86 | int i; | ||
87 | if (mode == Header.SINGLE_CHANNEL) | ||
88 | for (i = 0; i < num_subbands; ++i) | ||
89 | subbands[i] = new SubbandLayer1(i); | ||
90 | else if (mode == Header.JOINT_STEREO) | ||
91 | { | ||
92 | for (i = 0; i < header.intensity_stereo_bound(); ++i) | ||
93 | subbands[i] = new SubbandLayer1Stereo(i); | ||
94 | for (; i < num_subbands; ++i) | ||
95 | subbands[i] = new SubbandLayer1IntensityStereo(i); | ||
96 | } | ||
97 | else | ||
98 | { | ||
99 | for (i = 0; i < num_subbands; ++i) | ||
100 | subbands[i] = new SubbandLayer1Stereo(i); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | protected void readAllocation() | ||
105 | { | ||
106 | // start to read audio data: | ||
107 | for (int i = 0; i < num_subbands; ++i) | ||
108 | subbands[i].read_allocation(stream, header, crc); | ||
109 | |||
110 | } | ||
111 | |||
112 | protected void readScaleFactorSelection() | ||
113 | { | ||
114 | // scale factor selection not present for layer I. | ||
115 | } | ||
116 | |||
117 | protected void readScaleFactors() | ||
118 | { | ||
119 | for (int i = 0; i < num_subbands; ++i) | ||
120 | subbands[i].read_scalefactor(stream, header); | ||
121 | } | ||
122 | |||
123 | protected void readSampleData() | ||
124 | { | ||
125 | boolean read_ready = false; | ||
126 | boolean write_ready = false; | ||
127 | int mode = header.mode(); | ||
128 | int i; | ||
129 | do | ||
130 | { | ||
131 | for (i = 0; i < num_subbands; ++i) | ||
132 | read_ready = subbands[i].read_sampledata(stream); | ||
133 | do | ||
134 | { | ||
135 | for (i = 0; i < num_subbands; ++i) | ||
136 | write_ready = subbands[i].put_next_sample(which_channels,filter1, filter2); | ||
137 | |||
138 | filter1.calculate_pcm_samples(buffer); | ||
139 | if ((which_channels == OutputChannels.BOTH_CHANNELS) && (mode != Header.SINGLE_CHANNEL)) | ||
140 | filter2.calculate_pcm_samples(buffer); | ||
141 | } while (!write_ready); | ||
142 | } while (!read_ready); | ||
143 | |||
144 | } | ||
145 | |||
146 | /** | ||
147 | * Abstract base class for subband classes of layer I and II | ||
148 | */ | ||
149 | static abstract class Subband | ||
150 | { | ||
151 | /* | ||
152 | * Changes from version 1.1 to 1.2: | ||
153 | * - array size increased by one, although a scalefactor with index 63 | ||
154 | * is illegal (to prevent segmentation faults) | ||
155 | */ | ||
156 | // Scalefactors for layer I and II, Annex 3-B.1 in ISO/IEC DIS 11172: | ||
157 | public static final float scalefactors[] = | ||
158 | { | ||
159 | 2.00000000000000f, 1.58740105196820f, 1.25992104989487f, 1.00000000000000f, | ||
160 | 0.79370052598410f, 0.62996052494744f, 0.50000000000000f, 0.39685026299205f, | ||
161 | 0.31498026247372f, 0.25000000000000f, 0.19842513149602f, 0.15749013123686f, | ||
162 | 0.12500000000000f, 0.09921256574801f, 0.07874506561843f, 0.06250000000000f, | ||
163 | 0.04960628287401f, 0.03937253280921f, 0.03125000000000f, 0.02480314143700f, | ||
164 | 0.01968626640461f, 0.01562500000000f, 0.01240157071850f, 0.00984313320230f, | ||
165 | 0.00781250000000f, 0.00620078535925f, 0.00492156660115f, 0.00390625000000f, | ||
166 | 0.00310039267963f, 0.00246078330058f, 0.00195312500000f, 0.00155019633981f, | ||
167 | 0.00123039165029f, 0.00097656250000f, 0.00077509816991f, 0.00061519582514f, | ||
168 | 0.00048828125000f, 0.00038754908495f, 0.00030759791257f, 0.00024414062500f, | ||
169 | 0.00019377454248f, 0.00015379895629f, 0.00012207031250f, 0.00009688727124f, | ||
170 | 0.00007689947814f, 0.00006103515625f, 0.00004844363562f, 0.00003844973907f, | ||
171 | 0.00003051757813f, 0.00002422181781f, 0.00001922486954f, 0.00001525878906f, | ||
172 | 0.00001211090890f, 0.00000961243477f, 0.00000762939453f, 0.00000605545445f, | ||
173 | 0.00000480621738f, 0.00000381469727f, 0.00000302772723f, 0.00000240310869f, | ||
174 | 0.00000190734863f, 0.00000151386361f, 0.00000120155435f, 0.00000000000000f /* illegal scalefactor */ | ||
175 | }; | ||
176 | |||
177 | public abstract void read_allocation (Bitstream stream, Header header, Crc16 crc); | ||
178 | public abstract void read_scalefactor (Bitstream stream, Header header); | ||
179 | public abstract boolean read_sampledata (Bitstream stream); | ||
180 | public abstract boolean put_next_sample (int channels, SynthesisFilter filter1, SynthesisFilter filter2); | ||
181 | }; | ||
182 | |||
183 | /** | ||
184 | * Class for layer I subbands in single channel mode. | ||
185 | * Used for single channel mode | ||
186 | * and in derived class for intensity stereo mode | ||
187 | */ | ||
188 | static class SubbandLayer1 extends Subband | ||
189 | { | ||
190 | |||
191 | // Factors and offsets for sample requantization | ||
192 | public static final float table_factor[] = { | ||
193 | 0.0f, (1.0f/2.0f) * (4.0f/3.0f), (1.0f/4.0f) * (8.0f/7.0f), (1.0f/8.0f) * (16.0f/15.0f), | ||
194 | (1.0f/16.0f) * (32.0f/31.0f), (1.0f/32.0f) * (64.0f/63.0f), (1.0f/64.0f) * (128.0f/127.0f), | ||
195 | (1.0f/128.0f) * (256.0f/255.0f), (1.0f/256.0f) * (512.0f/511.0f), | ||
196 | (1.0f/512.0f) * (1024.0f/1023.0f), (1.0f/1024.0f) * (2048.0f/2047.0f), | ||
197 | (1.0f/2048.0f) * (4096.0f/4095.0f), (1.0f/4096.0f) * (8192.0f/8191.0f), | ||
198 | (1.0f/8192.0f) * (16384.0f/16383.0f), (1.0f/16384.0f) * (32768.0f/32767.0f) | ||
199 | }; | ||
200 | |||
201 | public static final float table_offset[] = { | ||
202 | 0.0f, ((1.0f/2.0f)-1.0f) * (4.0f/3.0f), ((1.0f/4.0f)-1.0f) * (8.0f/7.0f), ((1.0f/8.0f)-1.0f) * (16.0f/15.0f), | ||
203 | ((1.0f/16.0f)-1.0f) * (32.0f/31.0f), ((1.0f/32.0f)-1.0f) * (64.0f/63.0f), ((1.0f/64.0f)-1.0f) * (128.0f/127.0f), | ||
204 | ((1.0f/128.0f)-1.0f) * (256.0f/255.0f), ((1.0f/256.0f)-1.0f) * (512.0f/511.0f), | ||
205 | ((1.0f/512.0f)-1.0f) * (1024.0f/1023.0f), ((1.0f/1024.0f)-1.0f) * (2048.0f/2047.0f), | ||
206 | ((1.0f/2048.0f)-1.0f) * (4096.0f/4095.0f), ((1.0f/4096.0f)-1.0f) * (8192.0f/8191.0f), | ||
207 | ((1.0f/8192.0f)-1.0f) * (16384.0f/16383.0f), ((1.0f/16384.0f)-1.0f) * (32768.0f/32767.0f) | ||
208 | }; | ||
209 | |||
210 | protected int subbandnumber; | ||
211 | protected int samplenumber; | ||
212 | protected int allocation; | ||
213 | protected float scalefactor; | ||
214 | protected int samplelength; | ||
215 | protected float sample; | ||
216 | protected float factor, offset; | ||
217 | |||
218 | /** | ||
219 | * Construtor. | ||
220 | */ | ||
221 | public SubbandLayer1(int subbandnumber) | ||
222 | { | ||
223 | this.subbandnumber = subbandnumber; | ||
224 | samplenumber = 0; | ||
225 | } | ||
226 | |||
227 | /** | ||
228 | * | ||
229 | */ | ||
230 | public void read_allocation(Bitstream stream, Header header, Crc16 crc) | ||
231 | { | ||
232 | if ((allocation = stream.get_bits (4)) == 15) ; | ||
233 | // cerr << "WARNING: stream contains an illegal allocation!\n"; | ||
234 | // MPEG-stream is corrupted! | ||
235 | if (crc != null) | ||
236 | crc.add_bits (allocation, 4); | ||
237 | if (allocation != 0) | ||
238 | { | ||
239 | samplelength = allocation + 1; | ||
240 | factor = table_factor[allocation]; | ||
241 | offset = table_offset[allocation]; | ||
242 | } | ||
243 | } | ||
244 | |||
245 | /** | ||
246 | * | ||
247 | */ | ||
248 | public void read_scalefactor(Bitstream stream, Header header) | ||
249 | { | ||
250 | if (allocation != 0) scalefactor = scalefactors[stream.get_bits(6)]; | ||
251 | } | ||
252 | |||
253 | /** | ||
254 | * | ||
255 | */ | ||
256 | public boolean read_sampledata(Bitstream stream) | ||
257 | { | ||
258 | if (allocation != 0) | ||
259 | { | ||
260 | sample = (float) (stream.get_bits(samplelength)); | ||
261 | } | ||
262 | if (++samplenumber == 12) | ||
263 | { | ||
264 | samplenumber = 0; | ||
265 | return true; | ||
266 | } | ||
267 | return false; | ||
268 | } | ||
269 | |||
270 | /** | ||
271 | * | ||
272 | */ | ||
273 | public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2) | ||
274 | { | ||
275 | if ((allocation !=0) && (channels != OutputChannels.RIGHT_CHANNEL)) | ||
276 | { | ||
277 | float scaled_sample = (sample * factor + offset) * scalefactor; | ||
278 | filter1.input_sample (scaled_sample, subbandnumber); | ||
279 | } | ||
280 | return true; | ||
281 | } | ||
282 | }; | ||
283 | |||
284 | /** | ||
285 | * Class for layer I subbands in joint stereo mode. | ||
286 | */ | ||
287 | static class SubbandLayer1IntensityStereo extends SubbandLayer1 | ||
288 | { | ||
289 | protected float channel2_scalefactor; | ||
290 | |||
291 | /** | ||
292 | * Constructor | ||
293 | */ | ||
294 | public SubbandLayer1IntensityStereo(int subbandnumber) | ||
295 | { | ||
296 | super(subbandnumber); | ||
297 | } | ||
298 | |||
299 | /** | ||
300 | * | ||
301 | */ | ||
302 | public void read_allocation(Bitstream stream, Header header, Crc16 crc) | ||
303 | { | ||
304 | super.read_allocation (stream, header, crc); | ||
305 | } | ||
306 | |||
307 | /** | ||
308 | * | ||
309 | */ | ||
310 | public void read_scalefactor (Bitstream stream, Header header) | ||
311 | { | ||
312 | if (allocation != 0) | ||
313 | { | ||
314 | scalefactor = scalefactors[stream.get_bits(6)]; | ||
315 | channel2_scalefactor = scalefactors[stream.get_bits(6)]; | ||
316 | } | ||
317 | } | ||
318 | |||
319 | /** | ||
320 | * | ||
321 | */ | ||
322 | public boolean read_sampledata(Bitstream stream) | ||
323 | { | ||
324 | return super.read_sampledata (stream); | ||
325 | } | ||
326 | |||
327 | /** | ||
328 | * | ||
329 | */ | ||
330 | public boolean put_next_sample (int channels, SynthesisFilter filter1, SynthesisFilter filter2) | ||
331 | { | ||
332 | if (allocation !=0 ) | ||
333 | { | ||
334 | sample = sample * factor + offset; // requantization | ||
335 | if (channels == OutputChannels.BOTH_CHANNELS) | ||
336 | { | ||
337 | float sample1 = sample * scalefactor, | ||
338 | sample2 = sample * channel2_scalefactor; | ||
339 | filter1.input_sample(sample1, subbandnumber); | ||
340 | filter2.input_sample(sample2, subbandnumber); | ||
341 | } | ||
342 | else if (channels == OutputChannels.LEFT_CHANNEL) | ||
343 | { | ||
344 | float sample1 = sample * scalefactor; | ||
345 | filter1.input_sample(sample1, subbandnumber); | ||
346 | } | ||
347 | else | ||
348 | { | ||
349 | float sample2 = sample * channel2_scalefactor; | ||
350 | filter1.input_sample(sample2, subbandnumber); | ||
351 | } | ||
352 | } | ||
353 | return true; | ||
354 | } | ||
355 | }; | ||
356 | |||
357 | /** | ||
358 | * Class for layer I subbands in stereo mode. | ||
359 | */ | ||
360 | static class SubbandLayer1Stereo extends SubbandLayer1 | ||
361 | { | ||
362 | protected int channel2_allocation; | ||
363 | protected float channel2_scalefactor; | ||
364 | protected int channel2_samplelength; | ||
365 | protected float channel2_sample; | ||
366 | protected float channel2_factor, channel2_offset; | ||
367 | |||
368 | |||
369 | /** | ||
370 | * Constructor | ||
371 | */ | ||
372 | public SubbandLayer1Stereo(int subbandnumber) | ||
373 | { | ||
374 | super(subbandnumber); | ||
375 | } | ||
376 | |||
377 | /** | ||
378 | * | ||
379 | */ | ||
380 | public void read_allocation (Bitstream stream, Header header, Crc16 crc) | ||
381 | { | ||
382 | allocation = stream.get_bits(4); | ||
383 | channel2_allocation = stream.get_bits(4); | ||
384 | if (crc != null) | ||
385 | { | ||
386 | crc.add_bits (allocation, 4); | ||
387 | crc.add_bits (channel2_allocation, 4); | ||
388 | } | ||
389 | if (allocation != 0) | ||
390 | { | ||
391 | samplelength = allocation + 1; | ||
392 | factor = table_factor[allocation]; | ||
393 | offset = table_offset[allocation]; | ||
394 | } | ||
395 | if (channel2_allocation != 0) | ||
396 | { | ||
397 | channel2_samplelength = channel2_allocation + 1; | ||
398 | channel2_factor = table_factor[channel2_allocation]; | ||
399 | channel2_offset = table_offset[channel2_allocation]; | ||
400 | } | ||
401 | } | ||
402 | |||
403 | /** | ||
404 | * | ||
405 | */ | ||
406 | public void read_scalefactor(Bitstream stream, Header header) | ||
407 | { | ||
408 | if (allocation != 0) scalefactor = scalefactors[stream.get_bits(6)]; | ||
409 | if (channel2_allocation != 0) channel2_scalefactor = scalefactors[stream.get_bits(6)]; | ||
410 | } | ||
411 | |||
412 | /** | ||
413 | * | ||
414 | */ | ||
415 | public boolean read_sampledata (Bitstream stream) | ||
416 | { | ||
417 | boolean returnvalue = super.read_sampledata(stream); | ||
418 | if (channel2_allocation != 0) | ||
419 | { | ||
420 | channel2_sample = (float) (stream.get_bits(channel2_samplelength)); | ||
421 | } | ||
422 | return(returnvalue); | ||
423 | } | ||
424 | |||
425 | /** | ||
426 | * | ||
427 | */ | ||
428 | public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2) | ||
429 | { | ||
430 | super.put_next_sample (channels, filter1, filter2); | ||
431 | if ((channel2_allocation != 0) && (channels != OutputChannels.LEFT_CHANNEL)) | ||
432 | { | ||
433 | float sample2 = (channel2_sample * channel2_factor + channel2_offset) * | ||
434 | channel2_scalefactor; | ||
435 | if (channels == OutputChannels.BOTH_CHANNELS) | ||
436 | filter2.input_sample (sample2, subbandnumber); | ||
437 | else | ||
438 | filter1.input_sample (sample2, subbandnumber); | ||
439 | } | ||
440 | return true; | ||
441 | } | ||
442 | }; | ||
443 | |||
444 | } | ||