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,H<T,HV<T,SYNC*,HV*,FS*,HI*")
37 @METHODDEFAULT("THIS<IN,THISLOC=THIS,GLOBALLOC=THIS,RETURNLOC=THIS")
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
116 private SideInfoBuffer sib;
118 private BitReserve br;
123 @LATTICE("OUT<BUF,BUF<THIS,THISLOC=THIS,RETURNLOC=OUT")
124 public String toString() {
125 @LOC("BUF") StringBuffer buffer = new StringBuffer(200);
126 buffer.append("Layer ");
127 buffer.append(layer_string());
128 buffer.append(" frame ");
129 buffer.append(mode_string());
131 buffer.append(version_string());
133 buffer.append(" no");
134 buffer.append(" checksums");
136 buffer.append(sample_frequency_string());
139 buffer.append(bitrate_string());
141 @LOC("OUT") String s = buffer.toString();
146 * Read a 32-bit header from the bitstream.
148 int read_header(Bitstream stream, Crc16[] crcp) throws BitstreamException {
151 boolean sync = false;
153 headerstring = stream.syncHeader(syncmode);
154 if (headerstring == -1) {
157 _headerstring = headerstring; // E.B
158 if (syncmode == Bitstream.INITIAL_SYNC) {
159 h_version = ((headerstring >>> 19) & 1);
160 if (((headerstring >>> 20) & 1) == 0) // SZD: MPEG2.5 detection
161 if (h_version == MPEG2_LSF)
162 h_version = MPEG25_LSF;
164 throw stream.newBitstreamException(Bitstream.UNKNOWN_ERROR);
165 if ((h_sample_frequency = ((headerstring >>> 10) & 3)) == 3) {
166 throw stream.newBitstreamException(Bitstream.UNKNOWN_ERROR);
169 h_layer = 4 - (headerstring >>> 17) & 3;
170 h_protection_bit = (headerstring >>> 16) & 1;
171 h_bitrate_index = (headerstring >>> 12) & 0xF;
172 h_padding_bit = (headerstring >>> 9) & 1;
173 h_mode = ((headerstring >>> 6) & 3);
174 h_mode_extension = (headerstring >>> 4) & 3;
175 if (h_mode == JOINT_STEREO)
176 h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
178 h_intensity_stereo_bound = 0; // should never be used
179 if (((headerstring >>> 3) & 1) == 1)
181 if (((headerstring >>> 2) & 1) == 1)
183 // calculate number of subbands:
185 h_number_of_subbands = 32;
187 channel_bitrate = h_bitrate_index;
188 // calculate bitrate per channel:
189 if (h_mode != SINGLE_CHANNEL)
190 if (channel_bitrate == 4)
193 channel_bitrate -= 4;
194 if ((channel_bitrate == 1) || (channel_bitrate == 2))
195 if (h_sample_frequency == THIRTYTWO)
196 h_number_of_subbands = 12;
198 h_number_of_subbands = 8;
199 else if ((h_sample_frequency == FOURTYEIGHT)
200 || ((channel_bitrate >= 3) && (channel_bitrate <= 5)))
201 h_number_of_subbands = 27;
203 h_number_of_subbands = 30;
205 if (h_intensity_stereo_bound > h_number_of_subbands)
206 h_intensity_stereo_bound = h_number_of_subbands;
207 // calculate framesize and nSlots
208 calculate_framesize();
210 int framesizeloaded = stream.read_frame_data(framesize);
211 if ((framesize >= 0) && (framesizeloaded != framesize)) {
212 // Data loaded does not match to expected framesize,
213 // it might be an ID3v1 TAG. (Fix 11/17/04).
214 throw stream.newBitstreamException(Bitstream.INVALIDFRAME);
216 if (stream.isSyncCurrentPosition(syncmode)) {
217 if (syncmode == Bitstream.INITIAL_SYNC) {
218 syncmode = Bitstream.STRICT_SYNC;
219 stream.set_syncword(headerstring & 0xFFF80CC0);
223 stream.unreadFrame();
226 stream.parse_frame();
227 if (h_protection_bit == 0) {
228 // frame contains a crc checksum
229 checksum = (short) stream.get_bits(16);
232 crc.add_bits(headerstring, 16);
236 if (h_sample_frequency == FOURTYFOUR_POINT_ONE) {
238 * if (offset == null) { int max = max_number_of_frames(stream); offset =
239 * new int[max]; for(int i=0; i<max; i++) offset[i] = 0; } // E.B :
240 * Investigate more int cf = stream.current_frame(); int lf =
241 * stream.last_frame(); if ((cf > 0) && (cf == lf)) { offset[cf] =
242 * offset[cf-1] + h_padding_bit; } else { offset[0] = h_padding_bit; }
249 * Parse frame to extract optionnal VBR frame.
252 * @author E.B (javalayer@javazoom.net)
254 void parseVBR(byte[] firstframe) throws BitstreamException {
255 // Trying Xing header.
256 String xing = "Xing";
257 byte tmp[] = new byte[4];
259 // Compute "Xing" offset depending on MPEG version and channels.
260 if (h_version == MPEG1) {
261 if (h_mode == SINGLE_CHANNEL)
266 if (h_mode == SINGLE_CHANNEL)
272 System.arraycopy(firstframe, offset, tmp, 0, 4);
274 if (xing.equals(new String(tmp))) {
280 h_vbr_toc = new byte[100];
284 byte flags[] = new byte[4];
285 System.arraycopy(firstframe, offset + length, flags, 0, flags.length);
286 length += flags.length;
287 // Read number of frames (if available).
288 if ((flags[3] & (byte) (1 << 0)) != 0) {
289 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
291 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
292 & 0x0000FF00 | tmp[3] & 0x000000FF;
295 // Read size (if available).
296 if ((flags[3] & (byte) (1 << 1)) != 0) {
297 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
299 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
300 & 0x0000FF00 | tmp[3] & 0x000000FF;
303 // Read TOC (if available).
304 if ((flags[3] & (byte) (1 << 2)) != 0) {
305 System.arraycopy(firstframe, offset + length, h_vbr_toc, 0, h_vbr_toc.length);
306 length += h_vbr_toc.length;
308 // Read scale (if available).
309 if ((flags[3] & (byte) (1 << 3)) != 0) {
310 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
312 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8)
313 & 0x0000FF00 | tmp[3] & 0x000000FF;
316 // System.out.println("VBR:"+xing+" Frames:"+ h_vbr_frames
317 // +" Size:"+h_vbr_bytes);
319 } catch (ArrayIndexOutOfBoundsException e) {
320 throw new BitstreamException("XingVBRHeader Corrupted", e);
323 // Trying VBRI header.
324 String vbri = "VBRI";
327 System.arraycopy(firstframe, offset, tmp, 0, 4);
329 if (vbri.equals(new String(tmp))) {
335 h_vbr_toc = new byte[100];
338 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
340 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8) & 0x0000FF00
341 | tmp[3] & 0x000000FF;
344 System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
346 (tmp[0] << 24) & 0xFF000000 | (tmp[1] << 16) & 0x00FF0000 | (tmp[2] << 8) & 0x0000FF00
347 | tmp[3] & 0x000000FF;
349 // System.out.println("VBR:"+vbri+" Frames:"+ h_vbr_frames
350 // +" Size:"+h_vbr_bytes);
354 } catch (ArrayIndexOutOfBoundsException e) {
355 throw new BitstreamException("VBRIVBRHeader Corrupted", e);
359 // Functions to query header contents:
363 @RETURNLOC("THIS,Header.HV")
364 public int version() {
371 @RETURNLOC("THIS,Header.H")
377 * Returns bitrate index.
379 public int bitrate_index() {
380 return h_bitrate_index;
384 * Returns Sample Frequency.
386 @RETURNLOC("THIS,Header.H")
387 public int sample_frequency() {
388 return h_sample_frequency;
394 @RETURNLOC("THIS,Header.FS")
395 public int frequency() {
396 return frequencies[h_version][h_sample_frequency];
402 @RETURNLOC("THIS,Header.H")
408 * Returns Protection bit.
410 @RETURNLOC("THIS,Header.H")
411 public boolean checksums() {
412 if (h_protection_bit == 0)
421 public boolean copyright() {
428 public boolean original() {
435 * @return true if VBR header is found
437 public boolean vbr() {
444 * @return scale of -1 if not available
446 public int vbr_scale() {
453 * @return vbr toc ot null if not available
455 public byte[] vbr_toc() {
460 * Returns Checksum flag. Compares computed checksum with stream checksum.
463 public boolean checksum_ok() {
464 return (checksum == crc.checksum());
467 // Seeking and layer III stuff
469 * Returns Layer III Padding bit.
471 public boolean padding() {
472 if (h_padding_bit == 0)
481 @RETURNLOC("THIS,Header.NS")
487 * Returns Mode Extension.
489 @RETURNLOC("THIS,Header.H")
490 public int mode_extension() {
491 return h_mode_extension;
494 // E.B -> private to public
495 public static final int bitrates[][][] = {
497 { 0 /* free format */, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000,
498 160000, 176000, 192000, 224000, 256000, 0 },
499 { 0 /* free format */, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000,
500 96000, 112000, 128000, 144000, 160000, 0 },
501 { 0 /* free format */, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000,
502 96000, 112000, 128000, 144000, 160000, 0 } },
505 { 0 /* free format */, 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000,
506 288000, 320000, 352000, 384000, 416000, 448000, 0 },
507 { 0 /* free format */, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000,
508 192000, 224000, 256000, 320000, 384000, 0 },
509 { 0 /* free format */, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000,
510 160000, 192000, 224000, 256000, 320000, 0 } },
513 { 0 /* free format */, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000,
514 160000, 176000, 192000, 224000, 256000, 0 },
515 { 0 /* free format */, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000,
516 96000, 112000, 128000, 144000, 160000, 0 },
517 { 0 /* free format */, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000,
518 96000, 112000, 128000, 144000, 160000, 0 } },
522 // E.B -> private to public
524 * Calculate Frame size. Calculates framesize in bytes excluding header size.
526 public int calculate_framesize() {
530 (12 * bitrates[h_version][0][h_bitrate_index])
531 / frequencies[h_version][h_sample_frequency];
532 if (h_padding_bit != 0)
534 framesize <<= 2; // one slot is 4 bytes long
538 (144 * bitrates[h_version][h_layer - 1][h_bitrate_index])
539 / frequencies[h_version][h_sample_frequency];
540 if (h_version == MPEG2_LSF || h_version == MPEG25_LSF)
541 framesize >>= 1; // SZD
542 if (h_padding_bit != 0)
546 if (h_version == MPEG1) {
547 nSlots = framesize - ((h_mode == SINGLE_CHANNEL) ? 17 : 32) // side
550 - ((h_protection_bit != 0) ? 0 : 2) // CRC size
552 } else { // MPEG-2 LSF, SZD: MPEG-2.5 LSF
553 nSlots = framesize - ((h_mode == SINGLE_CHANNEL) ? 9 : 17) // side
556 - ((h_protection_bit != 0) ? 0 : 2) // CRC size
563 framesize -= 4; // subtract header size
568 * Returns the maximum number of frames in the stream.
571 * @return number of frames
573 public int max_number_of_frames(int streamsize) // E.B
578 if ((framesize + 4 - h_padding_bit) == 0)
581 return (streamsize / (framesize + 4 - h_padding_bit));
586 * Returns the maximum number of frames in the stream.
589 * @return number of frames
591 public int min_number_of_frames(int streamsize) // E.B
596 if ((framesize + 5 - h_padding_bit) == 0)
599 return (streamsize / (framesize + 5 - h_padding_bit));
606 * @return milliseconds per frame
608 @LATTICE("OUT<THIS,THISLOC=THIS,RETURNLOC=OUT")
609 public float ms_per_frame() // E.B
612 @LOC("OUT") double tpf = h_vbr_time_per_frame[layer()] / frequency();
613 if ((h_version == MPEG2_LSF) || (h_version == MPEG25_LSF))
615 return ((float) (tpf * 1000));
617 @LOC("OUT") float ms_per_frame_array[][] =
618 { { 8.707483f, 8.0f, 12.0f }, { 26.12245f, 24.0f, 36.0f }, { 26.12245f, 24.0f, 36.0f } };
619 return (ms_per_frame_array[h_layer - 1][h_sample_frequency]);
627 * @return total milliseconds
629 public float total_ms(int streamsize) // E.B
631 return (max_number_of_frames(streamsize) * ms_per_frame());
635 * Returns synchronized header.
637 public int getSyncHeader() // E.B
639 return _headerstring;
642 // functions which return header informations as strings:
644 * Return Layer version.
646 @RETURNLOC("THIS,Header.H")
647 public String layer_string() {
659 // E.B -> private to public
660 public static final String bitrate_str[][][] = {
662 { "free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
663 "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s", "160 kbit/s", "176 kbit/s",
664 "192 kbit/s", "224 kbit/s", "256 kbit/s", "forbidden" },
665 { "free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s", "40 kbit/s",
666 "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", "96 kbit/s", "112 kbit/s",
667 "128 kbit/s", "144 kbit/s", "160 kbit/s", "forbidden" },
668 { "free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s", "40 kbit/s",
669 "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", "96 kbit/s", "112 kbit/s",
670 "128 kbit/s", "144 kbit/s", "160 kbit/s", "forbidden" } },
673 { "free format", "32 kbit/s", "64 kbit/s", "96 kbit/s", "128 kbit/s", "160 kbit/s",
674 "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s", "320 kbit/s", "352 kbit/s",
675 "384 kbit/s", "416 kbit/s", "448 kbit/s", "forbidden" },
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", "160 kbit/s", "192 kbit/s", "224 kbit/s",
678 "256 kbit/s", "320 kbit/s", "384 kbit/s", "forbidden" },
679 { "free format", "32 kbit/s", "40 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s",
680 "80 kbit/s", "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s",
681 "224 kbit/s", "256 kbit/s", "320 kbit/s", "forbidden" } },
684 { "free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
685 "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s", "160 kbit/s", "176 kbit/s",
686 "192 kbit/s", "224 kbit/s", "256 kbit/s", "forbidden" },
687 { "free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s", "40 kbit/s",
688 "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", "96 kbit/s", "112 kbit/s",
689 "128 kbit/s", "144 kbit/s", "160 kbit/s", "forbidden" },
690 { "free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s", "40 kbit/s",
691 "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", "96 kbit/s", "112 kbit/s",
692 "128 kbit/s", "144 kbit/s", "160 kbit/s", "forbidden" } }, };
697 * @return bitrate in bps
699 @RETURNLOC("THIS,Header.FS")
700 public String bitrate_string() {
701 @LOC("THIS,Header.T") String kbs = " kb/s";
703 return Integer.toString(bitrate() / 1000) + kbs;
705 return bitrate_str[h_version][h_layer - 1][h_bitrate_index];
712 * @return bitrate in bps and average bitrate for VBR header
714 @RETURNLOC("THIS,Header.FS")
715 public int bitrate() {
717 return ((int) ((h_vbr_bytes * 8) / (ms_per_frame() * h_vbr_frames))) * 1000;
719 return bitrates[h_version][h_layer - 1][h_bitrate_index];
724 * Return Instant Bitrate. Bitrate for VBR is not constant.
726 * @return bitrate in bps
728 public int bitrate_instant() {
729 return bitrates[h_version][h_layer - 1][h_bitrate_index];
735 * @return frequency string in kHz
737 @RETURNLOC("THIS,Header.FS")
738 public String sample_frequency_string() {
739 switch (h_sample_frequency) {
741 if (h_version == MPEG1)
743 else if (h_version == MPEG2_LSF)
748 case FOURTYFOUR_POINT_ONE:
749 if (h_version == MPEG1)
751 else if (h_version == MPEG2_LSF)
757 if (h_version == MPEG1)
759 else if (h_version == MPEG2_LSF)
771 @RETURNLOC("THIS,Header.H")
772 public String mode_string() {
777 return "Joint stereo";
779 return "Dual channel";
781 return "Single channel";
789 * @return MPEG-1 or MPEG-2 LSF or MPEG-2.5 LSF
791 @RETURNLOC("THIS,Header.HV")
792 public String version_string() {
798 case MPEG25_LSF: // SZD
799 return "MPEG-2.5 LSF";
805 * Returns the number of subbands in the current frame.
807 * @return number of subbands
809 public int number_of_subbands() {
810 return h_number_of_subbands;
814 * Returns Intensity Stereo. (Layer II joint stereo only). Returns the number
815 * of subbands which are in stereo mode, subbands above that limit are in
816 * intensity stereo mode.
820 public int intensity_stereo_bound() {
821 return h_intensity_stereo_bound;
824 public void setSideInfoBuf(SideInfoBuffer sib) {
828 public void setBitReserve(BitReserve br) {
832 @RETURNLOC("THIS,Header.T")
833 public SideInfoBuffer getSideInfoBuffer() {
837 @RETURNLOC("THIS,Header.T")
838 public BitReserve getBitReserve() {