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.
30 @LATTICE("SB1<SB,SB<F,F<H,H<SH,SH*,SB1*")
31 @METHODDEFAULT("MODE<THIS,THIS<C,C<IN,THISLOC=THIS,C*")
32 class LayerIDecoder implements FrameDecoder {
35 protected Bitstream stream;
37 protected Header header;
39 protected SynthesisFilter filter1;
41 protected SynthesisFilter filter2;
43 protected Obuffer buffer;
45 protected int which_channels;
50 protected int num_subbands;
52 protected Subband[] subbands;
55 protected Crc16 crc = null; // new Crc16[1] to enable CRC checking.
57 public LayerIDecoder() {
61 public void create(@LOC("IN") Bitstream stream0, @LOC("IN") Header header0,
62 @LOC("IN") SynthesisFilter filtera, @LOC("IN") SynthesisFilter filterb,
63 @LOC("IN") Obuffer buffer0, @LOC("IN") int which_ch0) {
69 which_channels = which_ch0;
73 public void decodeFrame() throws DecoderException {
75 num_subbands = header.number_of_subbands();
76 subbands = new Subband[32];
82 readScaleFactorSelection();
84 if ((crc != null) || header.checksum_ok()) {
92 protected void createSubbands() {
93 @LOC("THIS,LayerIDecoder.H") int i;
94 if (mode == Header.SINGLE_CHANNEL) {
95 for (i = 0; i < num_subbands; ++i) {
96 subbands[i] = new SubbandLayer1(i);
98 } else if (mode == Header.JOINT_STEREO) {
99 for (i = 0; i < header.intensity_stereo_bound(); ++i) {
100 subbands[i] = new SubbandLayer1Stereo(i);
102 for (; i < num_subbands; ++i) {
103 subbands[i] = new SubbandLayer1IntensityStereo(i);
106 for (i = 0; i < num_subbands; ++i) {
107 subbands[i] = new SubbandLayer1Stereo(i);
112 protected void readAllocation() throws DecoderException {
113 // start to read audio data:
114 for (@LOC("THIS,LayerIDecoder.SB") int i = 0; i < num_subbands; ++i)
115 subbands[i].read_allocation(stream, header, crc);
119 protected void readScaleFactorSelection() {
120 // scale factor selection not present for layer I.
123 protected void readScaleFactors() {
124 for (@LOC("THIS,LayerIDecoder.SB") int i = 0; i < num_subbands; ++i)
125 subbands[i].read_scalefactor(stream, header);
128 @LATTICE("MODE<THIS,THIS<C,THISLOC=THIS,C*")
129 protected void readSampleData() {
131 @LOC("THIS,LayerIDecoder.SB1") boolean read_ready = false;
132 @LOC("THIS,LayerIDecoder.SB1") boolean write_ready = false;
134 @LOC("MODE") int mode = header.mode(); // header.mode() will return
137 @LOC("THIS,LayerIDecoder.SB1") int i;
140 for (i = 0; i < num_subbands; ++i) {
141 read_ready = subbands[i].read_sampledata(stream); // DELTA[Loc[readSampleData.V],Loc[LayerIDecoder.L]]
145 for (i = 0; i < num_subbands; ++i) {
146 write_ready = subbands[i].put_next_sample(which_channels, filter1, filter2);
149 filter1.calculate_pcm_samples(buffer);
150 if ((which_channels == OutputChannels.BOTH_CHANNELS) && (mode != Header.SINGLE_CHANNEL)) {
151 filter2.calculate_pcm_samples(buffer);
154 } while (!write_ready);
156 } while (!read_ready);
161 * Class for layer I subbands in single channel mode. Used for single channel
162 * mode and in derived class for intensity stereo mode
164 @LATTICE("S<L,L<H,H<SH,SH<SH0,SH*,L*")
165 @METHODDEFAULT("OUT<V,V<THIS,THIS<C,C<IN,C*,THISLOC=THIS,RETURNLOC=OUT")
166 static class SubbandLayer1 extends Subband {
168 // Factors and offsets for sample requantization
169 public static final float table_factor[] = { 0.0f, (1.0f / 2.0f) * (4.0f / 3.0f),
170 (1.0f / 4.0f) * (8.0f / 7.0f), (1.0f / 8.0f) * (16.0f / 15.0f),
171 (1.0f / 16.0f) * (32.0f / 31.0f), (1.0f / 32.0f) * (64.0f / 63.0f),
172 (1.0f / 64.0f) * (128.0f / 127.0f), (1.0f / 128.0f) * (256.0f / 255.0f),
173 (1.0f / 256.0f) * (512.0f / 511.0f), (1.0f / 512.0f) * (1024.0f / 1023.0f),
174 (1.0f / 1024.0f) * (2048.0f / 2047.0f), (1.0f / 2048.0f) * (4096.0f / 4095.0f),
175 (1.0f / 4096.0f) * (8192.0f / 8191.0f), (1.0f / 8192.0f) * (16384.0f / 16383.0f),
176 (1.0f / 16384.0f) * (32768.0f / 32767.0f) };
178 public static final float table_offset[] = { 0.0f, ((1.0f / 2.0f) - 1.0f) * (4.0f / 3.0f),
179 ((1.0f / 4.0f) - 1.0f) * (8.0f / 7.0f), ((1.0f / 8.0f) - 1.0f) * (16.0f / 15.0f),
180 ((1.0f / 16.0f) - 1.0f) * (32.0f / 31.0f), ((1.0f / 32.0f) - 1.0f) * (64.0f / 63.0f),
181 ((1.0f / 64.0f) - 1.0f) * (128.0f / 127.0f), ((1.0f / 128.0f) - 1.0f) * (256.0f / 255.0f),
182 ((1.0f / 256.0f) - 1.0f) * (512.0f / 511.0f),
183 ((1.0f / 512.0f) - 1.0f) * (1024.0f / 1023.0f),
184 ((1.0f / 1024.0f) - 1.0f) * (2048.0f / 2047.0f),
185 ((1.0f / 2048.0f) - 1.0f) * (4096.0f / 4095.0f),
186 ((1.0f / 4096.0f) - 1.0f) * (8192.0f / 8191.0f),
187 ((1.0f / 8192.0f) - 1.0f) * (16384.0f / 16383.0f),
188 ((1.0f / 16384.0f) - 1.0f) * (32768.0f / 32767.0f) };
191 protected int subbandnumber;
193 protected int samplenumber;
195 protected int allocation;
197 protected float scalefactor;
199 protected int samplelength;
201 protected float sample;
203 protected float factor;
205 protected float offset;
210 public SubbandLayer1(@LOC("IN") int subbandnumber) {
211 this.subbandnumber = subbandnumber;
218 // @LATTICE("IN<THIS,THISLOC=THIS")
219 @LATTICE("THIS<IN,THISLOC=THIS")
220 public void read_allocation(@LOC("THIS,LayerIDecoder$SubbandLayer1.SH") Bitstream stream,
221 @LOC("IN") Header header, @LOC("THIS,LayerIDecoder$SubbandLayer1.L") Crc16 crc)
222 throws DecoderException {
224 if ((allocation = stream.get_bits(4)) == 15) {
225 // CGJ: catch this condition and throw appropriate exception
226 throw new DecoderException(DecoderErrors.ILLEGAL_SUBBAND_ALLOCATION, null);
227 // cerr << "WARNING: stream contains an illegal allocation!\n";
228 // MPEG-stream is corrupted!
232 crc.add_bits(allocation, 4); // allocation has [THIS,H]
235 if (allocation != 0) {
236 samplelength = allocation + 1;
237 factor = table_factor[allocation];
238 offset = table_offset[allocation];
245 public void read_scalefactor(@LOC("IN") Bitstream stream, @LOC("IN") Header header) {
247 scalefactor = scalefactors[stream.get_bits(6)];
251 @LATTICE("THIS<IN,THISLOC=THIS")
252 @RETURNLOC("THIS,LayerIDecoder$SubbandLayer1.S")
253 public boolean read_sampledata(@LOC("THIS,LayerIDecoder$SubbandLayer1.S") Bitstream stream) {
254 if (allocation != 0) {
255 sample = (float) (stream.get_bits(samplelength));
257 if (++samplenumber == 12) {
264 // @METHODDEFAULT("OUT<V,V<THIS,THIS<C,C<IN,C*,THISLOC=THIS,RETURNLOC=OUT")
265 @LATTICE("THIS<IN,OUT,THISLOC=THIS,RETURNLOC=OUT")
266 public boolean put_next_sample(@LOC("IN") int channels,
267 @LOC("THIS,LayerIDecoder$SubbandLayer1.S") SynthesisFilter filter1,
268 @LOC("THIS,LayerIDecoder$SubbandLayer1.S") SynthesisFilter filter2) {
269 if ((allocation != 0) && (channels != OutputChannels.RIGHT_CHANNEL)) {
270 @LOC("THIS,LayerIDecoder$SubbandLayer1.L") float scaled_sample =
271 (sample * factor + offset) * scalefactor;
272 filter1.input_sample(scaled_sample, subbandnumber);
279 * Class for layer I subbands in joint stereo mode.
281 @LATTICE("S<L,L<H,H<SH,SH<SH0,SH*")
282 @METHODDEFAULT("OUT<V,V<THIS,THIS<C,C<IN,C*,THISLOC=THIS,RETURNLOC=OUT")
283 static class SubbandLayer1IntensityStereo extends SubbandLayer1 {
285 protected float channel2_scalefactor;
290 public SubbandLayer1IntensityStereo(@LOC("IN") int subbandnumber) {
291 super(subbandnumber);
297 @LATTICE("THIS<IN2,IN2<IN1,IN1<IN0,THISLOC=THIS")
298 public void read_allocation(@LOC("IN1") Bitstream stream, @LOC("IN0") Header header,
299 @LOC("IN2") Crc16 crc) throws DecoderException {
300 super.read_allocation(stream, header, crc);
306 public void read_scalefactor(@LOC("IN") Bitstream stream, @LOC("IN") Header header) {
307 if (allocation != 0) {
308 scalefactor = scalefactors[stream.get_bits(6)];
309 channel2_scalefactor = scalefactors[stream.get_bits(6)];
313 public boolean read_sampledata(@LOC("IN") Bitstream stream) {
314 return super.read_sampledata(stream);
317 public boolean put_next_sample(@LOC("IN") int channels, @LOC("IN") SynthesisFilter filter1,
318 @LOC("IN") SynthesisFilter filter2) {
319 if (allocation != 0) {
320 sample = sample * factor + offset; // requantization
321 if (channels == OutputChannels.BOTH_CHANNELS) {
322 @LOC("OUT") float sample1 = sample * scalefactor;
323 @LOC("OUT") float sample2 = sample * channel2_scalefactor;
324 filter1.input_sample(sample1, subbandnumber);
325 filter2.input_sample(sample2, subbandnumber);
326 } else if (channels == OutputChannels.LEFT_CHANNEL) {
327 @LOC("OUT") float sample1 = sample * scalefactor;
328 filter1.input_sample(sample1, subbandnumber);
330 @LOC("OUT") float sample2 = sample * channel2_scalefactor;
331 filter1.input_sample(sample2, subbandnumber);
339 * Class for layer I subbands in stereo mode.
341 @LATTICE("S<L,L<H,H<SH,SH<SH0,SH*,S*")
342 @METHODDEFAULT("OUT<V,V<THIS,THIS<C,C<IN,C*,THISLOC=THIS,RETURNLOC=OUT")
343 static class SubbandLayer1Stereo extends SubbandLayer1 {
345 protected int channel2_allocation;
347 protected float channel2_scalefactor;
349 protected int channel2_samplelength;
351 protected float channel2_sample;
353 protected float channel2_factor;
355 protected float channel2_offset;
360 public SubbandLayer1Stereo(@LOC("IN") int subbandnumber) {
361 super(subbandnumber);
368 public void read_allocation(@LOC("THIS,LayerIDecoder$SubbandLayer1Stereo.SH") Bitstream stream,
369 @LOC("IN") Header header, @LOC("THIS,LayerIDecoder$SubbandLayer1Stereo.L") Crc16 crc)
370 throws DecoderException {
371 allocation = stream.get_bits(4);
372 channel2_allocation = stream.get_bits(4);
374 crc.add_bits(allocation, 4);
375 crc.add_bits(channel2_allocation, 4);
377 if (allocation != 0) {
378 samplelength = allocation + 1;
379 factor = table_factor[allocation];
380 offset = table_offset[allocation];
382 if (channel2_allocation != 0) {
383 channel2_samplelength = channel2_allocation + 1;
384 channel2_factor = table_factor[channel2_allocation];
385 channel2_offset = table_offset[channel2_allocation];
392 public void read_scalefactor(@LOC("IN") Bitstream stream, @LOC("IN") Header header) {
394 scalefactor = scalefactors[stream.get_bits(6)];
395 if (channel2_allocation != 0)
396 channel2_scalefactor = scalefactors[stream.get_bits(6)];
402 @RETURNLOC("THIS,LayerIDecoder$SubbandLayer1Stereo.S")
403 public boolean read_sampledata(@LOC("THIS,LayerIDecoder$SubbandLayer1Stereo.S") Bitstream stream) {
404 @LOC("THIS,LayerIDecoder$SubbandLayer1Stereo.S") boolean returnvalue =
405 super.read_sampledata(stream);
406 if (channel2_allocation != 0) {
407 channel2_sample = (float) (stream.get_bits(channel2_samplelength));
416 public boolean put_next_sample(@LOC("IN") int channels, @LOC("IN") SynthesisFilter filter1,
417 @LOC("IN") SynthesisFilter filter2) {
418 super.put_next_sample(channels, filter1, filter2);
419 if ((channel2_allocation != 0) && (channels != OutputChannels.LEFT_CHANNEL)) {
420 @LOC("OUT") float sample2 =
421 (channel2_sample * channel2_factor + channel2_offset) * channel2_scalefactor;
422 if (channels == OutputChannels.BOTH_CHANNELS)
423 filter2.input_sample(sample2, subbandnumber);
425 filter1.input_sample(sample2, subbandnumber);