2 * 11/19/04 : 1.0 moved to LGPL.
3 * VBRI header support added, E.B javalayer@javazoom.net
5 * 12/04/03 : VBR (XING) header support added, E.B javalayer@javazoom.net
7 * 02/13/99 : Java Conversion by JavaZOOM , E.B javalayer@javazoom.net
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
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.
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.
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 *----------------------------------------------------------------------
34 * Class for extracting information from a frame header.
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 {
40 public static final int[][] frequencies = { { 22050, 24000, 16000, 1 },
41 { 44100, 48000, 32000, 1 }, { 11025, 12000, 8000, 1 } }; // SZD: MPEG25
44 * Constant for MPEG-2 LSF version
46 public static final int MPEG2_LSF = 0;
47 public static final int MPEG25_LSF = 2; // SZD
50 * Constant for MPEG-1 version
52 public static final int MPEG1 = 1;
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;
65 private int h_protection_bit;
67 private int h_bitrate_index;
69 private int h_padding_bit;
71 private int h_mode_extension;
73 private int h_version;
77 private int h_sample_frequency;
79 private int h_number_of_subbands;
81 private int h_intensity_stereo_bound;
83 private boolean h_copyright;
85 private boolean h_original;
86 // VBR support added by E.B
88 private double[] h_vbr_time_per_frame = { -1.0, 384.0, 1152.0, 1152.0 };
90 private boolean h_vbr;
92 private int h_vbr_frames;
94 private int h_vbr_scale;
96 private int h_vbr_bytes;
98 private byte[] h_vbr_toc;
101 private byte syncmode = Bitstream.INITIAL_SYNC;
106 public short checksum;
108 public int framesize;
113 private int _headerstring = -1; // E.B
115 private SideInfoBuffer sib;
116 private BitReserve br;
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());
128 buffer.append(version_string());
130 buffer.append(" no");
131 buffer.append(" checksums");
133 buffer.append(sample_frequency_string());
136 buffer.append(bitrate_string());
138 String s = buffer.toString();
143 * Read a 32-bit header from the bitstream.
145 int read_header(Bitstream stream, Crc16[] crcp) throws BitstreamException {
148 boolean sync = false;
150 headerstring = stream.syncHeader(syncmode);
151 if(headerstring==-1){
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;
161 throw stream.newBitstreamException(Bitstream.UNKNOWN_ERROR);
162 if ((h_sample_frequency = ((headerstring >>> 10) & 3)) == 3) {
163 throw stream.newBitstreamException(Bitstream.UNKNOWN_ERROR);
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;
175 h_intensity_stereo_bound = 0; // should never be used
176 if (((headerstring >>> 3) & 1) == 1)
178 if (((headerstring >>> 2) & 1) == 1)
180 // calculate number of subbands:
182 h_number_of_subbands = 32;
184 channel_bitrate = h_bitrate_index;
185 // calculate bitrate per channel:
186 if (h_mode != SINGLE_CHANNEL)
187 if (channel_bitrate == 4)
190 channel_bitrate -= 4;
191 if ((channel_bitrate == 1) || (channel_bitrate == 2))
192 if (h_sample_frequency == THIRTYTWO)
193 h_number_of_subbands = 12;
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;
200 h_number_of_subbands = 30;
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();
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);
213 if (stream.isSyncCurrentPosition(syncmode)) {
214 if (syncmode == Bitstream.INITIAL_SYNC) {
215 syncmode = Bitstream.STRICT_SYNC;
216 stream.set_syncword(headerstring & 0xFFF80CC0);
220 stream.unreadFrame();
223 stream.parse_frame();
224 if (h_protection_bit == 0) {
225 // frame contains a crc checksum
226 checksum = (short) stream.get_bits(16);
229 crc.add_bits(headerstring, 16);
233 if (h_sample_frequency == FOURTYFOUR_POINT_ONE) {
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; }
246 * Parse frame to extract optionnal VBR frame.
249 * @author E.B (javalayer@javazoom.net)
251 void parseVBR(byte[] firstframe) throws BitstreamException {
252 // Trying Xing header.
253 String xing = "Xing";
254 byte tmp[] = new byte[4];
256 // Compute "Xing" offset depending on MPEG version and channels.
257 if (h_version == MPEG1) {
258 if (h_mode == SINGLE_CHANNEL)
263 if (h_mode == SINGLE_CHANNEL)
269 System.arraycopy(firstframe, offset, tmp, 0, 4);
271 if (xing.equals(new String(tmp))) {
277 h_vbr_toc = new byte[100];
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);
288 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
289 & 0x0000FF00 | tmp[3] & 0x000000FF;
292 // Read size (if available).
293 if ((flags[3] & (byte) (1 << 1)) != 0) {
294 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
296 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
297 & 0x0000FF00 | tmp[3] & 0x000000FF;
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;
305 // Read scale (if available).
306 if ((flags[3] & (byte) (1 << 3)) != 0) {
307 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
309 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
310 & 0x0000FF00 | tmp[3] & 0x000000FF;
313 // System.out.println("VBR:"+xing+" Frames:"+ h_vbr_frames
314 // +" Size:"+h_vbr_bytes);
316 } catch (ArrayIndexOutOfBoundsException e) {
317 throw new BitstreamException("XingVBRHeader Corrupted", e);
320 // Trying VBRI header.
321 String vbri = "VBRI";
324 System.arraycopy(firstframe, offset, tmp, 0, 4);
326 if (vbri.equals(new String(tmp))) {
332 h_vbr_toc = new byte[100];
335 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
337 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8) & 0x0000FF00
338 | tmp[3] & 0x000000FF;
341 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
343 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8) & 0x0000FF00
344 | tmp[3] & 0x000000FF;
346 // System.out.println("VBR:"+vbri+" Frames:"+ h_vbr_frames
347 // +" Size:"+h_vbr_bytes);
351 } catch (ArrayIndexOutOfBoundsException e) {
352 throw new BitstreamException("VBRIVBRHeader Corrupted", e);
356 // Functions to query header contents:
361 public int version() {
374 * Returns bitrate index.
377 public int bitrate_index() {
378 return h_bitrate_index;
382 * Returns Sample Frequency.
384 public int sample_frequency() {
385 return h_sample_frequency;
391 public int frequency() {
392 return frequencies[h_version][h_sample_frequency];
404 * Returns Protection bit.
406 public boolean checksums() {
407 if (h_protection_bit == 0)
416 public boolean copyright() {
423 public boolean original() {
430 * @return true if VBR header is found
432 public boolean vbr() {
439 * @return scale of -1 if not available
441 public int vbr_scale() {
448 * @return vbr toc ot null if not available
450 public byte[] vbr_toc() {
455 * Returns Checksum flag. Compares computed checksum with stream checksum.
458 public boolean checksum_ok() {
459 return (checksum == crc.checksum());
462 // Seeking and layer III stuff
464 * Returns Layer III Padding bit.
466 public boolean padding() {
467 if (h_padding_bit == 0)
482 * Returns Mode Extension.
485 public int mode_extension() {
486 return h_mode_extension;
489 // E.B -> private to public
491 public static final int bitrates[][][] = {
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 } },
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 } },
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 } },
518 // E.B -> private to public
520 * Calculate Frame size. Calculates framesize in bytes excluding header size.
522 public int calculate_framesize() {
526 (12 * bitrates[h_version][0][h_bitrate_index])
527 / frequencies[h_version][h_sample_frequency];
528 if (h_padding_bit != 0)
530 framesize <<= 2; // one slot is 4 bytes long
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)
542 if (h_version == MPEG1) {
543 nSlots = framesize - ((h_mode == SINGLE_CHANNEL) ? 17 : 32) // side
546 - ((h_protection_bit != 0) ? 0 : 2) // CRC size
548 } else { // MPEG-2 LSF, SZD: MPEG-2.5 LSF
549 nSlots = framesize - ((h_mode == SINGLE_CHANNEL) ? 9 : 17) // side
552 - ((h_protection_bit != 0) ? 0 : 2) // CRC size
559 framesize -= 4; // subtract header size
564 * Returns the maximum number of frames in the stream.
567 * @return number of frames
569 public int max_number_of_frames(int streamsize) // E.B
574 if ((framesize + 4 - h_padding_bit) == 0)
577 return (streamsize / (framesize + 4 - h_padding_bit));
582 * Returns the maximum number of frames in the stream.
585 * @return number of frames
587 public int min_number_of_frames(int streamsize) // E.B
592 if ((framesize + 5 - h_padding_bit) == 0)
595 return (streamsize / (framesize + 5 - h_padding_bit));
602 * @return milliseconds per frame
604 public float ms_per_frame() // E.B
607 double tpf = h_vbr_time_per_frame[layer()] / frequency();
608 if ((h_version == MPEG2_LSF) || (h_version == MPEG25_LSF))
610 return ((float) (tpf * 1000));
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]);
622 * @return total milliseconds
624 public float total_ms(int streamsize) // E.B
626 return (max_number_of_frames(streamsize) * ms_per_frame());
630 * Returns synchronized header.
632 public int getSyncHeader() // E.B
634 return _headerstring;
637 // functions which return header informations as strings:
639 * Return Layer version.
641 public String layer_string() {
653 // E.B -> private to public
655 public static final String bitrate_str[][][] = {
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" } },
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" } },
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" } }, };
692 * @return bitrate in bps
694 public String bitrate_string() {
696 return Integer.toString(bitrate() / 1000) + " kb/s";
698 return bitrate_str[h_version][h_layer - 1][h_bitrate_index];
704 * @return bitrate in bps and average bitrate for VBR header
706 public int bitrate() {
708 return ((int) ((h_vbr_bytes * 8) / (ms_per_frame() * h_vbr_frames))) * 1000;
710 return bitrates[h_version][h_layer - 1][h_bitrate_index];
714 * Return Instant Bitrate. Bitrate for VBR is not constant.
716 * @return bitrate in bps
718 public int bitrate_instant() {
719 return bitrates[h_version][h_layer - 1][h_bitrate_index];
725 * @return frequency string in kHz
727 public String sample_frequency_string() {
728 switch (h_sample_frequency) {
730 if (h_version == MPEG1)
732 else if (h_version == MPEG2_LSF)
737 case FOURTYFOUR_POINT_ONE:
738 if (h_version == MPEG1)
740 else if (h_version == MPEG2_LSF)
746 if (h_version == MPEG1)
748 else if (h_version == MPEG2_LSF)
760 public String mode_string() {
765 return "Joint stereo";
767 return "Dual channel";
769 return "Single channel";
777 * @return MPEG-1 or MPEG-2 LSF or MPEG-2.5 LSF
779 public String version_string() {
785 case MPEG25_LSF: // SZD
786 return "MPEG-2.5 LSF";
792 * Returns the number of subbands in the current frame.
794 * @return number of subbands
797 public int number_of_subbands() {
798 return h_number_of_subbands;
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.
809 public int intensity_stereo_bound() {
810 return h_intensity_stereo_bound;
813 public void setSideInfoBuf(SideInfoBuffer sib) {
817 public void setBitReserve(BitReserve br) {
821 public SideInfoBuffer getSideInfoBuffer() {
825 public BitReserve getBitReserve() {