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