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