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