reformat benchmark source codes to meet the requirements of the annotation generation.
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3DecoderInfer / LayerIDecoder.java
1 /*
2  * 09/26/08     throw exception on subbband alloc error: Christopher G. Jennings (cjennings@acm.org)
3  * 
4  * 11/19/04             1.0 moved to LGPL.
5  * 
6  * 12/12/99             Initial version. Adapted from javalayer.java
7  *                              and Subband*.java. mdm@techie.com
8  *
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.
15  *
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.
20  *
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  *----------------------------------------------------------------------
25  */
26
27 /**
28  * Implements decoding of MPEG Audio Layer I frames.
29  */
30
31 class LayerIDecoder implements FrameDecoder {
32
33   protected Bitstream stream;
34
35   protected Header header;
36
37   protected SynthesisFilter filter1;
38
39   protected SynthesisFilter filter2;
40
41   protected Obuffer buffer;
42
43   protected int which_channels;
44
45   protected int mode;
46
47   protected int num_subbands;
48
49   protected Subband[] subbands;
50
51   protected Crc16 crc = null; // new Crc16[1] to enable CRC checking.
52
53   public LayerIDecoder() {
54     crc = new Crc16();
55   }
56
57   public void create(Bitstream stream0, Header header0, SynthesisFilter filtera, SynthesisFilter filterb, Obuffer buffer0, int which_ch0) {
58     stream = stream0;
59     header = header0;
60     filter1 = filtera;
61     filter2 = filterb;
62     buffer = buffer0;
63     which_channels = which_ch0;
64
65   }
66
67   public void decodeFrame() throws DecoderException {
68
69     num_subbands = header.number_of_subbands();
70     subbands = new Subband[32];
71     mode = header.mode();
72
73     createSubbands();
74
75     readAllocation();
76     readScaleFactorSelection();
77
78     if ((crc != null) || header.checksum_ok()) {
79       readScaleFactors();
80
81       readSampleData();
82     }
83
84   }
85
86   protected void createSubbands() {
87     int i;
88     if (mode == Header.SINGLE_CHANNEL) {
89       for (i = 0; i < num_subbands; ++i) {
90         subbands[i] = new SubbandLayer1(i);
91       }
92     } else if (mode == Header.JOINT_STEREO) {
93       for (i = 0; i < header.intensity_stereo_bound(); ++i) {
94         subbands[i] = new SubbandLayer1Stereo(i);
95       }
96       for (; i < num_subbands; ++i) {
97         subbands[i] = new SubbandLayer1IntensityStereo(i);
98       }
99     } else {
100       for (i = 0; i < num_subbands; ++i) {
101         subbands[i] = new SubbandLayer1Stereo(i);
102       }
103     }
104   }
105
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);
110
111   }
112
113   protected void readScaleFactorSelection() {
114     // scale factor selection not present for layer I.
115   }
116
117   protected void readScaleFactors() {
118     for (int i = 0; i < num_subbands; ++i)
119       subbands[i].read_scalefactor(stream, header);
120   }
121
122   protected void readSampleData() {
123
124     boolean read_ready = false;
125     boolean write_ready = false;
126
127     int mode = header.mode(); // header.mode() will return
128                               // DELTA(THIS)
129
130     int i;
131     do {
132
133       for (i = 0; i < num_subbands; ++i) {
134         read_ready = subbands[i].read_sampledata(stream); // DELTA[Loc[readSampleData.V],Loc[LayerIDecoder.L]]
135       }
136
137       do {
138         for (i = 0; i < num_subbands; ++i) {
139           write_ready = subbands[i].put_next_sample(which_channels, filter1, filter2);
140         }
141
142         filter1.calculate_pcm_samples(buffer);
143         if ((which_channels == OutputChannels.BOTH_CHANNELS) && (mode != Header.SINGLE_CHANNEL)) {
144           filter2.calculate_pcm_samples(buffer);
145         }
146
147       } while (!write_ready);
148
149     } while (!read_ready);
150
151   }
152
153   /**
154    * Class for layer I subbands in single channel mode. Used for single channel
155    * mode and in derived class for intensity stereo mode
156    */
157
158   static class SubbandLayer1 extends Subband {
159
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) };
162
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) };
164
165     protected int subbandnumber;
166
167     protected int samplenumber;
168
169     protected int allocation;
170
171     protected float scalefactor;
172
173     protected int samplelength;
174
175     protected float sample;
176
177     protected float factor;
178
179     protected float offset;
180
181     /**
182      * Construtor.
183      */
184     public SubbandLayer1(int subbandnumber) {
185       this.subbandnumber = subbandnumber;
186       samplenumber = 0;
187     }
188
189     /**
190            *
191            */
192     //
193
194     public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException {
195
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!
201       }
202
203       if (crc != null) {
204         crc.add_bits(allocation, 4); // allocation has [THIS,H]
205         // crc has [IN]
206       }
207       if (allocation != 0) {
208         samplelength = allocation + 1;
209         factor = table_factor[allocation];
210         offset = table_offset[allocation];
211       }
212     }
213
214     /**
215            *
216            */
217     public void read_scalefactor(Bitstream stream, Header header) {
218       if (allocation != 0)
219         scalefactor = scalefactors[stream.get_bits(6)];
220     }
221
222     // ssjava
223
224     public boolean read_sampledata(Bitstream stream) {
225       if (allocation != 0) {
226         sample = (float) (stream.get_bits(samplelength));
227       }
228       if (++samplenumber == 12) {
229         samplenumber = 0;
230         return true;
231       }
232       return false;
233     }
234
235     //
236
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);
241       }
242       return true;
243     }
244   };
245
246   /**
247    * Class for layer I subbands in joint stereo mode.
248    */
249
250   static class SubbandLayer1IntensityStereo extends SubbandLayer1 {
251
252     protected float channel2_scalefactor;
253
254     /**
255      * Constructor
256      */
257     public SubbandLayer1IntensityStereo(int subbandnumber) {
258       super(subbandnumber);
259     }
260
261     /**
262            *
263            */
264
265     public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException {
266       super.read_allocation(stream, header, crc);
267     }
268
269     /**
270            *
271            */
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)];
276       }
277     }
278
279     public boolean read_sampledata(Bitstream stream) {
280       return super.read_sampledata(stream);
281     }
282
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);
294         } else {
295           float sample2 = sample * channel2_scalefactor;
296           filter1.input_sample(sample2, subbandnumber);
297         }
298       }
299       return true;
300     }
301   };
302
303   /**
304    * Class for layer I subbands in stereo mode.
305    */
306
307   static class SubbandLayer1Stereo extends SubbandLayer1 {
308
309     protected int channel2_allocation;
310
311     protected float channel2_scalefactor;
312
313     protected int channel2_samplelength;
314
315     protected float channel2_sample;
316
317     protected float channel2_factor;
318
319     protected float channel2_offset;
320
321     /**
322      * Constructor
323      */
324     public SubbandLayer1Stereo(int subbandnumber) {
325       super(subbandnumber);
326     }
327
328     /**
329            *
330            */
331     // ssjava
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);
335       if (crc != null) {
336         crc.add_bits(allocation, 4);
337         crc.add_bits(channel2_allocation, 4);
338       }
339       if (allocation != 0) {
340         samplelength = allocation + 1;
341         factor = table_factor[allocation];
342         offset = table_offset[allocation];
343       }
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];
348       }
349     }
350
351     /**
352            *
353            */
354     public void read_scalefactor(Bitstream stream, Header header) {
355       if (allocation != 0)
356         scalefactor = scalefactors[stream.get_bits(6)];
357       if (channel2_allocation != 0)
358         channel2_scalefactor = scalefactors[stream.get_bits(6)];
359     }
360
361     /**
362            *
363            */
364
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));
369       }
370       return returnvalue;
371     }
372
373     /**
374            *
375            */
376
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);
383         else
384           filter1.input_sample(sample2, subbandnumber);
385       }
386       return true;
387     }
388
389   };
390
391 }