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