2 * 09/26/08 throw exception on subbband alloc error: Christopher G. Jennings (cjennings@acm.org)
4 * 11/19/04 1.0 moved to LGPL.
6 * 12/12/99 Initial version. Adapted from javalayer.java
7 * and Subband*.java. mdm@techie.com
9 * 02/28/99 Initial version : javalayer.java by E.B
10 *-----------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Library General Public License as published
13 * by the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *----------------------------------------------------------------------
27 package javazoom.jl.decoder;
30 * Implements decoding of MPEG Audio Layer I frames.
32 class LayerIDecoder implements FrameDecoder
34 protected Bitstream stream;
35 protected Header header;
36 protected SynthesisFilter filter1, filter2;
37 protected Obuffer buffer;
38 protected int which_channels;
41 protected int num_subbands;
42 protected Subband[] subbands;
43 protected Crc16 crc = null; // new Crc16[1] to enable CRC checking.
45 public LayerIDecoder()
50 public void create(Bitstream stream0, Header header0,
51 SynthesisFilter filtera, SynthesisFilter filterb,
52 Obuffer buffer0, int which_ch0)
59 which_channels = which_ch0;
63 public void decodeFrame() throws DecoderException
66 num_subbands = header.number_of_subbands();
67 subbands = new Subband[32];
73 readScaleFactorSelection();
75 if ((crc != null) || header.checksum_ok())
84 protected void createSubbands()
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)
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);
99 for (i = 0; i < num_subbands; ++i)
100 subbands[i] = new SubbandLayer1Stereo(i);
104 protected void readAllocation() throws DecoderException
106 // start to read audio data:
107 for (int i = 0; i < num_subbands; ++i)
108 subbands[i].read_allocation(stream, header, crc);
112 protected void readScaleFactorSelection()
114 // scale factor selection not present for layer I.
117 protected void readScaleFactors()
119 for (int i = 0; i < num_subbands; ++i)
120 subbands[i].read_scalefactor(stream, header);
123 protected void readSampleData()
125 boolean read_ready = false;
126 boolean write_ready = false;
127 int mode = header.mode();
131 for (i = 0; i < num_subbands; ++i)
132 read_ready = subbands[i].read_sampledata(stream);
135 for (i = 0; i < num_subbands; ++i)
136 write_ready = subbands[i].put_next_sample(which_channels,filter1, filter2);
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);
147 * Abstract base class for subband classes of layer I and II
149 static abstract class Subband
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)
156 // Scalefactors for layer I and II, Annex 3-B.1 in ISO/IEC DIS 11172:
157 public static final float scalefactors[] =
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 */
177 public abstract void read_allocation (Bitstream stream, Header header, Crc16 crc) throws DecoderException;
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);
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
188 static class SubbandLayer1 extends Subband
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)
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)
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;
221 public SubbandLayer1(int subbandnumber)
223 this.subbandnumber = subbandnumber;
230 public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException
232 if ((allocation = stream.get_bits (4)) == 15)
234 // CGJ: catch this condition and throw appropriate exception
235 throw new DecoderException(DecoderErrors.ILLEGAL_SUBBAND_ALLOCATION, null);
236 // cerr << "WARNING: stream contains an illegal allocation!\n";
237 // MPEG-stream is corrupted!
240 if (crc != null) crc.add_bits (allocation, 4);
243 samplelength = allocation + 1;
244 factor = table_factor[allocation];
245 offset = table_offset[allocation];
252 public void read_scalefactor(Bitstream stream, Header header)
254 if (allocation != 0) scalefactor = scalefactors[stream.get_bits(6)];
260 public boolean read_sampledata(Bitstream stream)
264 sample = (float) (stream.get_bits(samplelength));
266 if (++samplenumber == 12)
277 public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2)
279 if ((allocation !=0) && (channels != OutputChannels.RIGHT_CHANNEL))
281 float scaled_sample = (sample * factor + offset) * scalefactor;
282 filter1.input_sample (scaled_sample, subbandnumber);
289 * Class for layer I subbands in joint stereo mode.
291 static class SubbandLayer1IntensityStereo extends SubbandLayer1
293 protected float channel2_scalefactor;
298 public SubbandLayer1IntensityStereo(int subbandnumber)
300 super(subbandnumber);
306 public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException
308 super.read_allocation (stream, header, crc);
314 public void read_scalefactor (Bitstream stream, Header header)
318 scalefactor = scalefactors[stream.get_bits(6)];
319 channel2_scalefactor = scalefactors[stream.get_bits(6)];
326 public boolean read_sampledata(Bitstream stream)
328 return super.read_sampledata (stream);
334 public boolean put_next_sample (int channels, SynthesisFilter filter1, SynthesisFilter filter2)
338 sample = sample * factor + offset; // requantization
339 if (channels == OutputChannels.BOTH_CHANNELS)
341 float sample1 = sample * scalefactor,
342 sample2 = sample * channel2_scalefactor;
343 filter1.input_sample(sample1, subbandnumber);
344 filter2.input_sample(sample2, subbandnumber);
346 else if (channels == OutputChannels.LEFT_CHANNEL)
348 float sample1 = sample * scalefactor;
349 filter1.input_sample(sample1, subbandnumber);
353 float sample2 = sample * channel2_scalefactor;
354 filter1.input_sample(sample2, subbandnumber);
362 * Class for layer I subbands in stereo mode.
364 static class SubbandLayer1Stereo extends SubbandLayer1
366 protected int channel2_allocation;
367 protected float channel2_scalefactor;
368 protected int channel2_samplelength;
369 protected float channel2_sample;
370 protected float channel2_factor, channel2_offset;
376 public SubbandLayer1Stereo(int subbandnumber)
378 super(subbandnumber);
384 public void read_allocation (Bitstream stream, Header header, Crc16 crc) throws DecoderException
386 allocation = stream.get_bits(4);
387 channel2_allocation = stream.get_bits(4);
390 crc.add_bits (allocation, 4);
391 crc.add_bits (channel2_allocation, 4);
395 samplelength = allocation + 1;
396 factor = table_factor[allocation];
397 offset = table_offset[allocation];
399 if (channel2_allocation != 0)
401 channel2_samplelength = channel2_allocation + 1;
402 channel2_factor = table_factor[channel2_allocation];
403 channel2_offset = table_offset[channel2_allocation];
410 public void read_scalefactor(Bitstream stream, Header header)
412 if (allocation != 0) scalefactor = scalefactors[stream.get_bits(6)];
413 if (channel2_allocation != 0) channel2_scalefactor = scalefactors[stream.get_bits(6)];
419 public boolean read_sampledata (Bitstream stream)
421 boolean returnvalue = super.read_sampledata(stream);
422 if (channel2_allocation != 0)
424 channel2_sample = (float) (stream.get_bits(channel2_samplelength));
432 public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2)
434 super.put_next_sample (channels, filter1, filter2);
435 if ((channel2_allocation != 0) && (channels != OutputChannels.LEFT_CHANNEL))
437 float sample2 = (channel2_sample * channel2_factor + channel2_offset) *
438 channel2_scalefactor;
439 if (channels == OutputChannels.BOTH_CHANNELS)
440 filter2.input_sample (sample2, subbandnumber);
442 filter1.input_sample (sample2, subbandnumber);