mp3decoder compiled by our research compiler produces the same output that I get...
[IRC.git] / Robust / src / Tests / ssJava / mp3decoder / Header.java
1 /*
2  * 11/19/04 : 1.0 moved to LGPL.
3  *            VBRI header support added, E.B javalayer@javazoom.net
4  * 
5  * 12/04/03 : VBR (XING) header support added, E.B javalayer@javazoom.net
6  *
7  * 02/13/99 : Java Conversion by JavaZOOM , E.B javalayer@javazoom.net
8  *
9  * Declarations for MPEG header class
10  * A few layer III, MPEG-2 LSF, and seeking modifications made by Jeff Tsay.
11  * Last modified : 04/19/97
12  *
13  *  @(#) header.h 1.7, last edit: 6/15/94 16:55:33
14  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
15  *  @(#) Berlin University of Technology
16  *-----------------------------------------------------------------------
17  *   This program is free software; you can redistribute it and/or modify
18  *   it under the terms of the GNU Library General Public License as published
19  *   by the Free Software Foundation; either version 2 of the License, or
20  *   (at your option) any later version.
21  *
22  *   This program is distributed in the hope that it will be useful,
23  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *   GNU Library General Public License for more details.
26  *
27  *   You should have received a copy of the GNU Library General Public
28  *   License along with this program; if not, write to the Free Software
29  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30  *----------------------------------------------------------------------
31  */
32
33 /**
34  * Class for extracting information from a frame header.
35  */
36 @LATTICE("HI<HNS,HNS<H,C<H,NS<FS,FS<H,FS<HV,H<SYNC,HV<SYNC,HV<T,SYNC*,HV*,FS*,HI*")
37 @METHODDEFAULT("OUT<V,V<THIS,THIS<SH,SH<IN,SH*,THISLOC=THIS,GLOBALLOC=IN")
38 public final class Header {
39
40   public static final int[][] frequencies = { { 22050, 24000, 16000, 1 },
41       { 44100, 48000, 32000, 1 }, { 11025, 12000, 8000, 1 } }; // SZD: MPEG25
42
43   /**
44    * Constant for MPEG-2 LSF version
45    */
46   public static final int MPEG2_LSF = 0;
47   public static final int MPEG25_LSF = 2; // SZD
48
49   /**
50    * Constant for MPEG-1 version
51    */
52   public static final int MPEG1 = 1;
53
54   public static final int STEREO = 0;
55   public static final int JOINT_STEREO = 1;
56   public static final int DUAL_CHANNEL = 2;
57   public static final int SINGLE_CHANNEL = 3;
58   public static final int FOURTYFOUR_POINT_ONE = 0;
59   public static final int FOURTYEIGHT = 1;
60   public static final int THIRTYTWO = 2;
61
62   @LOC("H")
63   private int h_layer;
64   @LOC("H")
65   private int h_protection_bit;
66   @LOC("H")
67   private int h_bitrate_index;
68   @LOC("H")
69   private int h_padding_bit;
70   @LOC("H")
71   private int h_mode_extension;
72   @LOC("HV")
73   private int h_version;
74   @LOC("H")
75   private int h_mode;
76   @LOC("H")
77   private int h_sample_frequency;
78   @LOC("HNS")
79   private int h_number_of_subbands;
80   @LOC("HI")
81   private int h_intensity_stereo_bound;
82   @LOC("H")
83   private boolean h_copyright;
84   @LOC("H")
85   private boolean h_original;
86   // VBR support added by E.B
87   @LOC("T")
88   private double[] h_vbr_time_per_frame = { -1.0, 384.0, 1152.0, 1152.0 };
89   @LOC("T")
90   private boolean h_vbr;
91   @LOC("T")
92   private int h_vbr_frames;
93   @LOC("T")
94   private int h_vbr_scale;
95   @LOC("T")
96   private int h_vbr_bytes;
97   @LOC("T")
98   private byte[] h_vbr_toc;
99
100   @LOC("SYNC")
101   private byte syncmode = Bitstream.INITIAL_SYNC;
102   @LOC("C")
103   private Crc16 crc;
104
105   @LOC("C")
106   public short checksum;
107   @LOC("FS")
108   public int framesize;
109   @LOC("NS")
110   public int nSlots;
111
112   @LOC("T")
113   private int _headerstring = -1; // E.B
114
115   private SideInfoBuffer sib;
116   private BitReserve br;
117
118   Header() {
119   }
120
121   public String toString() {
122     StringBuffer buffer = new StringBuffer(200);
123     buffer.append("Layer ");
124     buffer.append(layer_string());
125     buffer.append(" frame ");
126     buffer.append(mode_string());
127     buffer.append(' ');
128     buffer.append(version_string());
129     if (!checksums())
130       buffer.append(" no");
131     buffer.append(" checksums");
132     buffer.append(' ');
133     buffer.append(sample_frequency_string());
134     buffer.append(',');
135     buffer.append(' ');
136     buffer.append(bitrate_string());
137
138     String s = buffer.toString();
139     return s;
140   }
141
142   /**
143    * Read a 32-bit header from the bitstream.
144    */
145   int read_header(Bitstream stream, Crc16[] crcp) throws BitstreamException {
146     int headerstring;
147     int channel_bitrate;
148     boolean sync = false;
149     do {
150       headerstring = stream.syncHeader(syncmode);
151       if(headerstring==-1){
152         return -1;
153       }
154       _headerstring = headerstring; // E.B
155       if (syncmode == Bitstream.INITIAL_SYNC) {
156         h_version = ((headerstring >>> 19) & 1);
157         if (((headerstring >>> 20) & 1) == 0) // SZD: MPEG2.5 detection
158           if (h_version == MPEG2_LSF)
159             h_version = MPEG25_LSF;
160           else
161             throw stream.newBitstreamException(Bitstream.UNKNOWN_ERROR);
162         if ((h_sample_frequency = ((headerstring >>> 10) & 3)) == 3) {
163           throw stream.newBitstreamException(Bitstream.UNKNOWN_ERROR);
164         }
165       }
166       h_layer = 4 - (headerstring >>> 17) & 3;
167       h_protection_bit = (headerstring >>> 16) & 1;
168       h_bitrate_index = (headerstring >>> 12) & 0xF;
169       h_padding_bit = (headerstring >>> 9) & 1;
170       h_mode = ((headerstring >>> 6) & 3);
171       h_mode_extension = (headerstring >>> 4) & 3;
172       if (h_mode == JOINT_STEREO)
173         h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
174       else
175         h_intensity_stereo_bound = 0; // should never be used
176       if (((headerstring >>> 3) & 1) == 1)
177         h_copyright = true;
178       if (((headerstring >>> 2) & 1) == 1)
179         h_original = true;
180       // calculate number of subbands:
181       if (h_layer == 1)
182         h_number_of_subbands = 32;
183       else {
184         channel_bitrate = h_bitrate_index;
185         // calculate bitrate per channel:
186         if (h_mode != SINGLE_CHANNEL)
187           if (channel_bitrate == 4)
188             channel_bitrate = 1;
189           else
190             channel_bitrate -= 4;
191         if ((channel_bitrate == 1) || (channel_bitrate == 2))
192           if (h_sample_frequency == THIRTYTWO)
193             h_number_of_subbands = 12;
194           else
195             h_number_of_subbands = 8;
196         else if ((h_sample_frequency == FOURTYEIGHT)
197             || ((channel_bitrate >= 3) && (channel_bitrate <= 5)))
198           h_number_of_subbands = 27;
199         else
200           h_number_of_subbands = 30;
201       }
202       if (h_intensity_stereo_bound > h_number_of_subbands)
203         h_intensity_stereo_bound = h_number_of_subbands;
204       // calculate framesize and nSlots
205       calculate_framesize();
206       // read framedata:
207       int framesizeloaded = stream.read_frame_data(framesize);
208       if ((framesize >= 0) && (framesizeloaded != framesize)) {
209         // Data loaded does not match to expected framesize,
210         // it might be an ID3v1 TAG. (Fix 11/17/04).
211         throw stream.newBitstreamException(Bitstream.INVALIDFRAME);
212       }
213       if (stream.isSyncCurrentPosition(syncmode)) {
214         if (syncmode == Bitstream.INITIAL_SYNC) {
215           syncmode = Bitstream.STRICT_SYNC;
216           stream.set_syncword(headerstring & 0xFFF80CC0);
217         }
218         sync = true;
219       } else {
220         stream.unreadFrame();
221       }
222     } while (!sync);
223     stream.parse_frame();
224     if (h_protection_bit == 0) {
225       // frame contains a crc checksum
226       checksum = (short) stream.get_bits(16);
227       if (crc == null)
228         crc = new Crc16();
229       crc.add_bits(headerstring, 16);
230       crcp[0] = crc;
231     } else
232       crcp[0] = null;
233     if (h_sample_frequency == FOURTYFOUR_POINT_ONE) {
234       /*
235        * if (offset == null) { int max = max_number_of_frames(stream); offset =
236        * new int[max]; for(int i=0; i<max; i++) offset[i] = 0; } // E.B :
237        * Investigate more int cf = stream.current_frame(); int lf =
238        * stream.last_frame(); if ((cf > 0) && (cf == lf)) { offset[cf] =
239        * offset[cf-1] + h_padding_bit; } else { offset[0] = h_padding_bit; }
240        */
241     }
242     return 0;
243   }
244
245   /**
246    * Parse frame to extract optionnal VBR frame.
247    * 
248    * @param firstframe
249    * @author E.B (javalayer@javazoom.net)
250    */
251   void parseVBR(byte[] firstframe) throws BitstreamException {
252     // Trying Xing header.
253     String xing = "Xing";
254     byte tmp[] = new byte[4];
255     int offset = 0;
256     // Compute "Xing" offset depending on MPEG version and channels.
257     if (h_version == MPEG1) {
258       if (h_mode == SINGLE_CHANNEL)
259         offset = 21 - 4;
260       else
261         offset = 36 - 4;
262     } else {
263       if (h_mode == SINGLE_CHANNEL)
264         offset = 13 - 4;
265       else
266         offset = 21 - 4;
267     }
268     try {
269       System.arraycopy(firstframe, offset, tmp, 0, 4);
270       // Is "Xing" ?
271       if (xing.equals(new String(tmp))) {
272         // Yes.
273         h_vbr = true;
274         h_vbr_frames = -1;
275         h_vbr_bytes = -1;
276         h_vbr_scale = -1;
277         h_vbr_toc = new byte[100];
278
279         int length = 4;
280         // Read flags.
281         byte flags[] = new byte[4];
282         System.arraycopy(firstframe, offset + length, flags, 0, flags.length);
283         length += flags.length;
284         // Read number of frames (if available).
285         if ((flags[3] & (byte) (1 << 0)) != 0) {
286           System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
287           h_vbr_frames =
288               (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
289                   & 0x0000FF00 | tmp[3] & 0x000000FF;
290           length += 4;
291         }
292         // Read size (if available).
293         if ((flags[3] & (byte) (1 << 1)) != 0) {
294           System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
295           h_vbr_bytes =
296               (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
297                   & 0x0000FF00 | tmp[3] & 0x000000FF;
298           length += 4;
299         }
300         // Read TOC (if available).
301         if ((flags[3] & (byte) (1 << 2)) != 0) {
302           System.arraycopy(firstframe, offset + length, h_vbr_toc, 0, h_vbr_toc.length);
303           length += h_vbr_toc.length;
304         }
305         // Read scale (if available).
306         if ((flags[3] & (byte) (1 << 3)) != 0) {
307           System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
308           h_vbr_scale =
309               (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
310                   & 0x0000FF00 | tmp[3] & 0x000000FF;
311           length += 4;
312         }
313         // System.out.println("VBR:"+xing+" Frames:"+ h_vbr_frames
314         // +" Size:"+h_vbr_bytes);
315       }
316     } catch (ArrayIndexOutOfBoundsException e) {
317       throw new BitstreamException("XingVBRHeader Corrupted", e);
318     }
319
320     // Trying VBRI header.
321     String vbri = "VBRI";
322     offset = 36 - 4;
323     try {
324       System.arraycopy(firstframe, offset, tmp, 0, 4);
325       // Is "VBRI" ?
326       if (vbri.equals(new String(tmp))) {
327         // Yes.
328         h_vbr = true;
329         h_vbr_frames = -1;
330         h_vbr_bytes = -1;
331         h_vbr_scale = -1;
332         h_vbr_toc = new byte[100];
333         // Bytes.
334         int length = 4 + 6;
335         System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
336         h_vbr_bytes =
337             (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8) & 0x0000FF00
338                 | tmp[3] & 0x000000FF;
339         length += 4;
340         // Frames.
341         System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
342         h_vbr_frames =
343             (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8) & 0x0000FF00
344                 | tmp[3] & 0x000000FF;
345         length += 4;
346         // System.out.println("VBR:"+vbri+" Frames:"+ h_vbr_frames
347         // +" Size:"+h_vbr_bytes);
348         // TOC
349         // TODO
350       }
351     } catch (ArrayIndexOutOfBoundsException e) {
352       throw new BitstreamException("VBRIVBRHeader Corrupted", e);
353     }
354   }
355
356   // Functions to query header contents:
357   /**
358    * Returns version.
359    */
360   @RETURNLOC("OUT")
361   public int version() {
362     return h_version;
363   }
364
365   /**
366    * Returns Layer ID.
367    */
368   @RETURNLOC("OUT")
369   public int layer() {
370     return h_layer;
371   }
372
373   /**
374    * Returns bitrate index.
375    */
376   @RETURNLOC("OUT")
377   public int bitrate_index() {
378     return h_bitrate_index;
379   }
380
381   /**
382    * Returns Sample Frequency.
383    */
384   public int sample_frequency() {
385     return h_sample_frequency;
386   }
387
388   /**
389    * Returns Frequency.
390    */
391   public int frequency() {
392     return frequencies[h_version][h_sample_frequency];
393   }
394
395   /**
396    * Returns Mode.
397    */
398   @RETURNLOC("OUT")
399   public int mode() {
400     return h_mode;
401   }
402
403   /**
404    * Returns Protection bit.
405    */
406   public boolean checksums() {
407     if (h_protection_bit == 0)
408       return true;
409     else
410       return false;
411   }
412
413   /**
414    * Returns Copyright.
415    */
416   public boolean copyright() {
417     return h_copyright;
418   }
419
420   /**
421    * Returns Original.
422    */
423   public boolean original() {
424     return h_original;
425   }
426
427   /**
428    * Return VBR.
429    * 
430    * @return true if VBR header is found
431    */
432   public boolean vbr() {
433     return h_vbr;
434   }
435
436   /**
437    * Return VBR scale.
438    * 
439    * @return scale of -1 if not available
440    */
441   public int vbr_scale() {
442     return h_vbr_scale;
443   }
444
445   /**
446    * Return VBR TOC.
447    * 
448    * @return vbr toc ot null if not available
449    */
450   public byte[] vbr_toc() {
451     return h_vbr_toc;
452   }
453
454   /**
455    * Returns Checksum flag. Compares computed checksum with stream checksum.
456    */
457   @RETURNLOC("OUT")
458   public boolean checksum_ok() {
459     return (checksum == crc.checksum());
460   }
461
462   // Seeking and layer III stuff
463   /**
464    * Returns Layer III Padding bit.
465    */
466   public boolean padding() {
467     if (h_padding_bit == 0)
468       return false;
469     else
470       return true;
471   }
472
473   /**
474    * Returns Slots.
475    */
476   @RETURNLOC("OUT")
477   public int slots() {
478     return nSlots;
479   }
480
481   /**
482    * Returns Mode Extension.
483    */
484   @RETURNLOC("OUT")
485   public int mode_extension() {
486     return h_mode_extension;
487   }
488
489   // E.B -> private to public
490   @LOC("T")
491   public static final int bitrates[][][] = {
492       {
493           { 0 /* free format */, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000,
494               160000, 176000, 192000, 224000, 256000, 0 },
495           { 0 /* free format */, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000,
496               96000, 112000, 128000, 144000, 160000, 0 },
497           { 0 /* free format */, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000,
498               96000, 112000, 128000, 144000, 160000, 0 } },
499
500       {
501           { 0 /* free format */, 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000,
502               288000, 320000, 352000, 384000, 416000, 448000, 0 },
503           { 0 /* free format */, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000,
504               192000, 224000, 256000, 320000, 384000, 0 },
505           { 0 /* free format */, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000,
506               160000, 192000, 224000, 256000, 320000, 0 } },
507       // SZD: MPEG2.5
508       {
509           { 0 /* free format */, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000,
510               160000, 176000, 192000, 224000, 256000, 0 },
511           { 0 /* free format */, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000,
512               96000, 112000, 128000, 144000, 160000, 0 },
513           { 0 /* free format */, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000,
514               96000, 112000, 128000, 144000, 160000, 0 } },
515
516   };
517
518   // E.B -> private to public
519   /**
520    * Calculate Frame size. Calculates framesize in bytes excluding header size.
521    */
522   public int calculate_framesize() {
523
524     if (h_layer == 1) {
525       framesize =
526           (12 * bitrates[h_version][0][h_bitrate_index])
527               / frequencies[h_version][h_sample_frequency];
528       if (h_padding_bit != 0)
529         framesize++;
530       framesize <<= 2; // one slot is 4 bytes long
531       nSlots = 0;
532     } else {
533       framesize =
534           (144 * bitrates[h_version][h_layer - 1][h_bitrate_index])
535               / frequencies[h_version][h_sample_frequency];
536       if (h_version == MPEG2_LSF || h_version == MPEG25_LSF)
537         framesize >>= 1; // SZD
538       if (h_padding_bit != 0)
539         framesize++;
540       // Layer III slots
541       if (h_layer == 3) {
542         if (h_version == MPEG1) {
543           nSlots = framesize - ((h_mode == SINGLE_CHANNEL) ? 17 : 32) // side
544                                                                       // info
545                                                                       // size
546               - ((h_protection_bit != 0) ? 0 : 2) // CRC size
547               - 4; // header size
548         } else { // MPEG-2 LSF, SZD: MPEG-2.5 LSF
549           nSlots = framesize - ((h_mode == SINGLE_CHANNEL) ? 9 : 17) // side
550                                                                      // info
551                                                                      // size
552               - ((h_protection_bit != 0) ? 0 : 2) // CRC size
553               - 4; // header size
554         }
555       } else {
556         nSlots = 0;
557       }
558     }
559     framesize -= 4; // subtract header size
560     return framesize;
561   }
562
563   /**
564    * Returns the maximum number of frames in the stream.
565    * 
566    * @param streamsize
567    * @return number of frames
568    */
569   public int max_number_of_frames(int streamsize) // E.B
570   {
571     if (h_vbr == true)
572       return h_vbr_frames;
573     else {
574       if ((framesize + 4 - h_padding_bit) == 0)
575         return 0;
576       else
577         return (streamsize / (framesize + 4 - h_padding_bit));
578     }
579   }
580
581   /**
582    * Returns the maximum number of frames in the stream.
583    * 
584    * @param streamsize
585    * @return number of frames
586    */
587   public int min_number_of_frames(int streamsize) // E.B
588   {
589     if (h_vbr == true)
590       return h_vbr_frames;
591     else {
592       if ((framesize + 5 - h_padding_bit) == 0)
593         return 0;
594       else
595         return (streamsize / (framesize + 5 - h_padding_bit));
596     }
597   }
598
599   /**
600    * Returns ms/frame.
601    * 
602    * @return milliseconds per frame
603    */
604   public float ms_per_frame() // E.B
605   {
606     if (h_vbr == true) {
607       double tpf = h_vbr_time_per_frame[layer()] / frequency();
608       if ((h_version == MPEG2_LSF) || (h_version == MPEG25_LSF))
609         tpf /= 2;
610       return ((float) (tpf * 1000));
611     } else {
612       float ms_per_frame_array[][] =
613           { { 8.707483f, 8.0f, 12.0f }, { 26.12245f, 24.0f, 36.0f }, { 26.12245f, 24.0f, 36.0f } };
614       return (ms_per_frame_array[h_layer - 1][h_sample_frequency]);
615     }
616   }
617
618   /**
619    * Returns total ms.
620    * 
621    * @param streamsize
622    * @return total milliseconds
623    */
624   public float total_ms(int streamsize) // E.B
625   {
626     return (max_number_of_frames(streamsize) * ms_per_frame());
627   }
628
629   /**
630    * Returns synchronized header.
631    */
632   public int getSyncHeader() // E.B
633   {
634     return _headerstring;
635   }
636
637   // functions which return header informations as strings:
638   /**
639    * Return Layer version.
640    */
641   public String layer_string() {
642     switch (h_layer) {
643     case 1:
644       return "I";
645     case 2:
646       return "II";
647     case 3:
648       return "III";
649     }
650     return null;
651   }
652
653   // E.B -> private to public
654   @LOC("T")
655   public static final String bitrate_str[][][] = {
656       {
657           { "free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
658               "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s", "160 kbit/s", "176 kbit/s",
659               "192 kbit/s", "224 kbit/s", "256 kbit/s", "forbidden" },
660           { "free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s", "40 kbit/s",
661               "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", "96 kbit/s", "112 kbit/s",
662               "128 kbit/s", "144 kbit/s", "160 kbit/s", "forbidden" },
663           { "free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s", "40 kbit/s",
664               "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", "96 kbit/s", "112 kbit/s",
665               "128 kbit/s", "144 kbit/s", "160 kbit/s", "forbidden" } },
666
667       {
668           { "free format", "32 kbit/s", "64 kbit/s", "96 kbit/s", "128 kbit/s", "160 kbit/s",
669               "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s", "320 kbit/s", "352 kbit/s",
670               "384 kbit/s", "416 kbit/s", "448 kbit/s", "forbidden" },
671           { "free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
672               "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s", "224 kbit/s",
673               "256 kbit/s", "320 kbit/s", "384 kbit/s", "forbidden" },
674           { "free format", "32 kbit/s", "40 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s",
675               "80 kbit/s", "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s",
676               "224 kbit/s", "256 kbit/s", "320 kbit/s", "forbidden" } },
677       // SZD: MPEG2.5
678       {
679           { "free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
680               "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s", "160 kbit/s", "176 kbit/s",
681               "192 kbit/s", "224 kbit/s", "256 kbit/s", "forbidden" },
682           { "free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s", "40 kbit/s",
683               "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", "96 kbit/s", "112 kbit/s",
684               "128 kbit/s", "144 kbit/s", "160 kbit/s", "forbidden" },
685           { "free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s", "40 kbit/s",
686               "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", "96 kbit/s", "112 kbit/s",
687               "128 kbit/s", "144 kbit/s", "160 kbit/s", "forbidden" } }, };
688
689   /**
690    * Return Bitrate.
691    * 
692    * @return bitrate in bps
693    */
694   public String bitrate_string() {
695     if (h_vbr == true) {
696       return Integer.toString(bitrate() / 1000) + " kb/s";
697     } else
698       return bitrate_str[h_version][h_layer - 1][h_bitrate_index];
699   }
700
701   /**
702    * Return Bitrate.
703    * 
704    * @return bitrate in bps and average bitrate for VBR header
705    */
706   public int bitrate() {
707     if (h_vbr == true) {
708       return ((int) ((h_vbr_bytes * 8) / (ms_per_frame() * h_vbr_frames))) * 1000;
709     } else
710       return bitrates[h_version][h_layer - 1][h_bitrate_index];
711   }
712
713   /**
714    * Return Instant Bitrate. Bitrate for VBR is not constant.
715    * 
716    * @return bitrate in bps
717    */
718   public int bitrate_instant() {
719     return bitrates[h_version][h_layer - 1][h_bitrate_index];
720   }
721
722   /**
723    * Returns Frequency
724    * 
725    * @return frequency string in kHz
726    */
727   public String sample_frequency_string() {
728     switch (h_sample_frequency) {
729     case THIRTYTWO:
730       if (h_version == MPEG1)
731         return "32 kHz";
732       else if (h_version == MPEG2_LSF)
733         return "16 kHz";
734       else
735         // SZD
736         return "8 kHz";
737     case FOURTYFOUR_POINT_ONE:
738       if (h_version == MPEG1)
739         return "44.1 kHz";
740       else if (h_version == MPEG2_LSF)
741         return "22.05 kHz";
742       else
743         // SZD
744         return "11.025 kHz";
745     case FOURTYEIGHT:
746       if (h_version == MPEG1)
747         return "48 kHz";
748       else if (h_version == MPEG2_LSF)
749         return "24 kHz";
750       else
751         // SZD
752         return "12 kHz";
753     }
754     return (null);
755   }
756
757   /**
758    * Returns Mode.
759    */
760   public String mode_string() {
761     switch (h_mode) {
762     case STEREO:
763       return "Stereo";
764     case JOINT_STEREO:
765       return "Joint stereo";
766     case DUAL_CHANNEL:
767       return "Dual channel";
768     case SINGLE_CHANNEL:
769       return "Single channel";
770     }
771     return null;
772   }
773
774   /**
775    * Returns Version.
776    * 
777    * @return MPEG-1 or MPEG-2 LSF or MPEG-2.5 LSF
778    */
779   public String version_string() {
780     switch (h_version) {
781     case MPEG1:
782       return "MPEG-1";
783     case MPEG2_LSF:
784       return "MPEG-2 LSF";
785     case MPEG25_LSF: // SZD
786       return "MPEG-2.5 LSF";
787     }
788     return (null);
789   }
790
791   /**
792    * Returns the number of subbands in the current frame.
793    * 
794    * @return number of subbands
795    */
796   @RETURNLOC("OUT")
797   public int number_of_subbands() {
798     return h_number_of_subbands;
799   }
800
801   /**
802    * Returns Intensity Stereo. (Layer II joint stereo only). Returns the number
803    * of subbands which are in stereo mode, subbands above that limit are in
804    * intensity stereo mode.
805    * 
806    * @return intensity
807    */
808   @RETURNLOC("OUT")
809   public int intensity_stereo_bound() {
810     return h_intensity_stereo_bound;
811   }
812
813   public void setSideInfoBuf(SideInfoBuffer sib) {
814     this.sib = sib;
815   }
816
817   public void setBitReserve(BitReserve br) {
818     this.br = br;
819   }
820
821   public SideInfoBuffer getSideInfoBuffer() {
822     return sib;
823   }
824
825   public BitReserve getBitReserve() {
826     return br;
827   }
828
829 }