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 *----------------------------------------------------------------------
28 * Implements decoding of MPEG Audio Layer I frames.
31 class LayerIDecoder implements FrameDecoder {
33 protected Bitstream stream;
35 protected Header header;
37 protected SynthesisFilter filter1;
39 protected SynthesisFilter filter2;
41 protected Obuffer buffer;
43 protected int which_channels;
47 protected int num_subbands;
49 protected Subband[] subbands;
51 protected Crc16 crc = null; // new Crc16[1] to enable CRC checking.
53 public LayerIDecoder() {
57 public void create(Bitstream stream0, Header header0, SynthesisFilter filtera, SynthesisFilter filterb, Obuffer buffer0, int which_ch0) {
63 which_channels = which_ch0;
67 public void decodeFrame() throws DecoderException {
69 num_subbands = header.number_of_subbands();
70 subbands = new Subband[32];
76 readScaleFactorSelection();
78 if ((crc != null) || header.checksum_ok()) {
86 protected void createSubbands() {
88 if (mode == Header.SINGLE_CHANNEL) {
89 for (i = 0; i < num_subbands; ++i) {
90 subbands[i] = new SubbandLayer1(i);
92 } else if (mode == Header.JOINT_STEREO) {
93 for (i = 0; i < header.intensity_stereo_bound(); ++i) {
94 subbands[i] = new SubbandLayer1Stereo(i);
96 for (; i < num_subbands; ++i) {
97 subbands[i] = new SubbandLayer1IntensityStereo(i);
100 for (i = 0; i < num_subbands; ++i) {
101 subbands[i] = new SubbandLayer1Stereo(i);
106 protected void readAllocation() throws DecoderException {
107 // start to read audio data:
108 for (int i = 0; i < num_subbands; ++i)
109 subbands[i].read_allocation(stream, header, crc);
113 protected void readScaleFactorSelection() {
114 // scale factor selection not present for layer I.
117 protected void readScaleFactors() {
118 for (int i = 0; i < num_subbands; ++i)
119 subbands[i].read_scalefactor(stream, header);
122 protected void readSampleData() {
124 boolean read_ready = false;
125 boolean write_ready = false;
127 int mode = header.mode(); // header.mode() will return
133 for (i = 0; i < num_subbands; ++i) {
134 read_ready = subbands[i].read_sampledata(stream); // DELTA[Loc[readSampleData.V],Loc[LayerIDecoder.L]]
138 for (i = 0; i < num_subbands; ++i) {
139 write_ready = subbands[i].put_next_sample(which_channels, filter1, filter2);
142 filter1.calculate_pcm_samples(buffer);
143 if ((which_channels == OutputChannels.BOTH_CHANNELS) && (mode != Header.SINGLE_CHANNEL)) {
144 filter2.calculate_pcm_samples(buffer);
147 } while (!write_ready);
149 } while (!read_ready);
154 * Class for layer I subbands in single channel mode. Used for single channel
155 * mode and in derived class for intensity stereo mode
158 static class SubbandLayer1 extends Subband {
160 // Factors and offsets for sample requantization
161 public static final float table_factor[] = { 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), (1.0f / 16.0f) * (32.0f / 31.0f), (1.0f / 32.0f) * (64.0f / 63.0f), (1.0f / 64.0f) * (128.0f / 127.0f), (1.0f / 128.0f) * (256.0f / 255.0f), (1.0f / 256.0f) * (512.0f / 511.0f), (1.0f / 512.0f) * (1024.0f / 1023.0f), (1.0f / 1024.0f) * (2048.0f / 2047.0f), (1.0f / 2048.0f) * (4096.0f / 4095.0f), (1.0f / 4096.0f) * (8192.0f / 8191.0f), (1.0f / 8192.0f) * (16384.0f / 16383.0f), (1.0f / 16384.0f) * (32768.0f / 32767.0f) };
163 public static final float table_offset[] = { 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), ((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), ((1.0f / 128.0f) - 1.0f) * (256.0f / 255.0f), ((1.0f / 256.0f) - 1.0f) * (512.0f / 511.0f), ((1.0f / 512.0f) - 1.0f) * (1024.0f / 1023.0f), ((1.0f / 1024.0f) - 1.0f) * (2048.0f / 2047.0f), ((1.0f / 2048.0f) - 1.0f) * (4096.0f / 4095.0f), ((1.0f / 4096.0f) - 1.0f) * (8192.0f / 8191.0f), ((1.0f / 8192.0f) - 1.0f) * (16384.0f / 16383.0f), ((1.0f / 16384.0f) - 1.0f) * (32768.0f / 32767.0f) };
165 protected int subbandnumber;
167 protected int samplenumber;
169 protected int allocation;
171 protected float scalefactor;
173 protected int samplelength;
175 protected float sample;
177 protected float factor;
179 protected float offset;
184 public SubbandLayer1(int subbandnumber) {
185 this.subbandnumber = subbandnumber;
194 public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException {
196 if ((allocation = stream.get_bits(4)) == 15) {
197 // CGJ: catch this condition and throw appropriate exception
198 throw new DecoderException(DecoderErrors.ILLEGAL_SUBBAND_ALLOCATION, null);
199 // cerr << "WARNING: stream contains an illegal allocation!\n";
200 // MPEG-stream is corrupted!
204 crc.add_bits(allocation, 4); // allocation has [THIS,H]
207 if (allocation != 0) {
208 samplelength = allocation + 1;
209 factor = table_factor[allocation];
210 offset = table_offset[allocation];
217 public void read_scalefactor(Bitstream stream, Header header) {
219 scalefactor = scalefactors[stream.get_bits(6)];
224 public boolean read_sampledata(Bitstream stream) {
225 if (allocation != 0) {
226 sample = (float) (stream.get_bits(samplelength));
228 if (++samplenumber == 12) {
237 public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2) {
238 if ((allocation != 0) && (channels != OutputChannels.RIGHT_CHANNEL)) {
239 float scaled_sample = (sample * factor + offset) * scalefactor;
240 filter1.input_sample(scaled_sample, subbandnumber);
247 * Class for layer I subbands in joint stereo mode.
250 static class SubbandLayer1IntensityStereo extends SubbandLayer1 {
252 protected float channel2_scalefactor;
257 public SubbandLayer1IntensityStereo(int subbandnumber) {
258 super(subbandnumber);
265 public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException {
266 super.read_allocation(stream, header, crc);
272 public void read_scalefactor(Bitstream stream, Header header) {
273 if (allocation != 0) {
274 scalefactor = scalefactors[stream.get_bits(6)];
275 channel2_scalefactor = scalefactors[stream.get_bits(6)];
279 public boolean read_sampledata(Bitstream stream) {
280 return super.read_sampledata(stream);
283 public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2) {
284 if (allocation != 0) {
285 sample = sample * factor + offset; // requantization
286 if (channels == OutputChannels.BOTH_CHANNELS) {
287 float sample1 = sample * scalefactor;
288 float sample2 = sample * channel2_scalefactor;
289 filter1.input_sample(sample1, subbandnumber);
290 filter2.input_sample(sample2, subbandnumber);
291 } else if (channels == OutputChannels.LEFT_CHANNEL) {
292 float sample1 = sample * scalefactor;
293 filter1.input_sample(sample1, subbandnumber);
295 float sample2 = sample * channel2_scalefactor;
296 filter1.input_sample(sample2, subbandnumber);
304 * Class for layer I subbands in stereo mode.
307 static class SubbandLayer1Stereo extends SubbandLayer1 {
309 protected int channel2_allocation;
311 protected float channel2_scalefactor;
313 protected int channel2_samplelength;
315 protected float channel2_sample;
317 protected float channel2_factor;
319 protected float channel2_offset;
324 public SubbandLayer1Stereo(int subbandnumber) {
325 super(subbandnumber);
332 public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException {
333 allocation = stream.get_bits(4);
334 channel2_allocation = stream.get_bits(4);
336 crc.add_bits(allocation, 4);
337 crc.add_bits(channel2_allocation, 4);
339 if (allocation != 0) {
340 samplelength = allocation + 1;
341 factor = table_factor[allocation];
342 offset = table_offset[allocation];
344 if (channel2_allocation != 0) {
345 channel2_samplelength = channel2_allocation + 1;
346 channel2_factor = table_factor[channel2_allocation];
347 channel2_offset = table_offset[channel2_allocation];
354 public void read_scalefactor(Bitstream stream, Header header) {
356 scalefactor = scalefactors[stream.get_bits(6)];
357 if (channel2_allocation != 0)
358 channel2_scalefactor = scalefactors[stream.get_bits(6)];
365 public boolean read_sampledata(Bitstream stream) {
366 boolean returnvalue = super.read_sampledata(stream);
367 if (channel2_allocation != 0) {
368 channel2_sample = (float) (stream.get_bits(channel2_samplelength));
377 public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2) {
378 super.put_next_sample(channels, filter1, filter2);
379 if ((channel2_allocation != 0) && (channels != OutputChannels.LEFT_CHANNEL)) {
380 float sample2 = (channel2_sample * channel2_factor + channel2_offset) * channel2_scalefactor;
381 if (channels == OutputChannels.BOTH_CHANNELS)
382 filter2.input_sample(sample2, subbandnumber);
384 filter1.input_sample(sample2, subbandnumber);