⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 header.java

📁 java编写的mp3播放器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 12/04/03 : VBR (XING) header support added, E.B javalayer@javazoom.net
 *
 * 02/13/99 : Java Conversion by JavaZOOM , E.B javalayer@javazoom.net
 *
 * Declarations for MPEG header class
 * A few layer III, MPEG-2 LSF, and seeking modifications made by Jeff Tsay.
 * Last modified : 04/19/97
 *
 *  @(#) header.h 1.7, last edit: 6/15/94 16:55:33
 *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
 *  @(#) Berlin University of Technology
 *---------------------------------------------------------------------------
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *--------------------------------------------------------------------------
 */
package javazoom.jl.decoder;

/**
 * Class for extracting information from a frame header.
 */
public final class Header
{
	public  static final int[][]	frequencies =
						{{22050, 24000, 16000, 1},
						{44100, 48000, 32000, 1},
						{11025, 12000, 8000, 1}};	// SZD: MPEG25

	/**
	 * Constant for MPEG-2 LSF version
	 */
	public static final int		MPEG2_LSF = 0;
	public static final int		MPEG25_LSF = 2;	// SZD

	/**
	 * Constant for MPEG-1 version
	 */
	public static final int		MPEG1 = 1;

	public static final int		STEREO = 0;
	public static final int		JOINT_STEREO = 1;
	public static final int		DUAL_CHANNEL = 2;
	public static final int		SINGLE_CHANNEL = 3;
	public static final int		FOURTYFOUR_POINT_ONE = 0;
	public static final int		FOURTYEIGHT=1;
	public static final int		THIRTYTWO=2;

	private int				h_layer, h_protection_bit, h_bitrate_index,
	  						h_padding_bit, h_mode_extension;
	private int				h_version;
	private int				h_mode;
	private int				h_sample_frequency;
	private int				h_number_of_subbands, h_intensity_stereo_bound;
	private boolean			h_copyright, h_original;
	// VBR support added by E.B
	private double[] 		h_vbr_time_per_frame = {-1, 384, 1152, 1152};
	private boolean			h_vbr;
	private int				h_vbr_frames;
	private int				h_vbr_scale;
	private int				h_vbr_bytes;
	private byte[]			h_vbr_toc;
	
	private byte			syncmode = Bitstream.INITIAL_SYNC;
	private Crc16			crc;

	public short			checksum;
	public int				framesize;
	public int				nSlots;

	private int				_headerstring = -1; // E.B

	Header()
	{
	}
	public String toString()
	{
		StringBuffer buffer = new StringBuffer(200);
		buffer.append("Layer ");
		buffer.append(layer_string());
		buffer.append(" frame ");
		buffer.append(mode_string());
		buffer.append(' ');
		buffer.append(version_string());
		if (!checksums())
			buffer.append(" no");
		buffer.append(" checksums");
		buffer.append(' ');
		buffer.append(sample_frequency_string());
		buffer.append(',');
		buffer.append(' ');
		buffer.append(bitrate_string());

		String s =  buffer.toString();
		return s;
	}

	/**
	 * Read a 32-bit header from the bitstream.
	 */
	void read_header(Bitstream stream, Crc16[] crcp) throws BitstreamException
	{
		int headerstring;
		int channel_bitrate;
		boolean sync = false;
		do
		{
			headerstring = stream.syncHeader(syncmode);
			_headerstring = headerstring; // E.B
			if (syncmode == Bitstream.INITIAL_SYNC)
			{
				h_version = ((headerstring >>> 19) & 1);
				if (((headerstring >>> 20) & 1) == 0) // SZD: MPEG2.5 detection
					if (h_version == MPEG2_LSF)
						h_version = MPEG25_LSF;
					else
						throw stream.newBitstreamException(Bitstream.UNKNOWN_ERROR);
				if ((h_sample_frequency = ((headerstring >>> 10) & 3)) == 3)
				{
					throw stream.newBitstreamException(Bitstream.UNKNOWN_ERROR);
				}
			}
			h_layer = 4 - (headerstring >>> 17) & 3;
			h_protection_bit = (headerstring >>> 16) & 1;
			h_bitrate_index = (headerstring >>> 12) & 0xF;
			h_padding_bit = (headerstring >>> 9) & 1;
			h_mode = ((headerstring >>> 6) & 3);
			h_mode_extension = (headerstring >>> 4) & 3;
			if (h_mode == JOINT_STEREO)
				h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
			else
				h_intensity_stereo_bound = 0; // should never be used
			if (((headerstring >>> 3) & 1) == 1)
				h_copyright = true;
			if (((headerstring >>> 2) & 1) == 1)
				h_original = true;
			// calculate number of subbands:
			if (h_layer == 1)
				h_number_of_subbands = 32;
			else
			{
				channel_bitrate = h_bitrate_index;
				// calculate bitrate per channel:
				if (h_mode != SINGLE_CHANNEL)
					if (channel_bitrate == 4)
						channel_bitrate = 1;
					else
						channel_bitrate -= 4;
				if ((channel_bitrate == 1) || (channel_bitrate == 2))
					if (h_sample_frequency == THIRTYTWO)
						h_number_of_subbands = 12;
					else
						h_number_of_subbands = 8;
				else if ((h_sample_frequency == FOURTYEIGHT) || ((channel_bitrate >= 3) && (channel_bitrate <= 5)))
					h_number_of_subbands = 27;
				else
					h_number_of_subbands = 30;
			}
			if (h_intensity_stereo_bound > h_number_of_subbands)
				h_intensity_stereo_bound = h_number_of_subbands;
			// calculate framesize and nSlots
			calculate_framesize();
			// read framedata:
			stream.read_frame_data(framesize);
			if (stream.isSyncCurrentPosition(syncmode))
			{
				if (syncmode == Bitstream.INITIAL_SYNC)
				{
					syncmode = Bitstream.STRICT_SYNC;
					stream.set_syncword(headerstring & 0xFFF80CC0);
				}
				sync = true;
			}
			else
			{
				stream.unreadFrame();
			}
		}
		while (!sync);
		stream.parse_frame();
		if (h_protection_bit == 0)
		{
			// frame contains a crc checksum
			checksum = (short) stream.get_bits(16);
			if (crc == null)
				crc = new Crc16();
			crc.add_bits(headerstring, 16);
			crcp[0] = crc;
		}
		else
			crcp[0] = null;
		if (h_sample_frequency == FOURTYFOUR_POINT_ONE)
		{
			/*
				if (offset == null)
			  {
				  int max = max_number_of_frames(stream);
				  offset = new int[max];
			     for(int i=0; i<max; i++) offset[i] = 0;
			  }
			  // E.B : Investigate more
			  int cf = stream.current_frame();
			  int lf = stream.last_frame();
			  if ((cf > 0) && (cf == lf))
			  {
				   offset[cf] = offset[cf-1] + h_padding_bit;
			  }
			  else
			  {
				       offset[0] = h_padding_bit;
			  }
			*/
		}
	}

	/**
	 * Parse frame to extract optionnal VBR frame.
	 * @param firstframe
	 * @author E.B (javalayer@javazoom.net)
	 */
	void parseVBR(byte[] firstframe) throws BitstreamException
	{
		String xing = "Xing";
		byte tmp[] = new byte[4];
		int offset = 0;
		// Computer "Xing" offset depending on MPEG version and channels.
		if (h_version == MPEG1) 
		{
		  if (h_mode == SINGLE_CHANNEL)  offset=21-4;
		  else offset=36-4;
		} 
		else 
		{
		  if (h_mode == SINGLE_CHANNEL) offset=23-4;
		  else offset = 21-4;		  
		}
		try
		{
			System.arraycopy(firstframe, offset, tmp, 0, 4);
			// Is "Xing" header ?
			if (xing.equals(new String(tmp)))
			{
				//Yes.
				h_vbr = true;
				h_vbr_frames = -1;
				h_vbr_bytes = -1;
				h_vbr_scale = -1;
				h_vbr_toc = new byte[100];
				
				int length = 4;
				// Read flags.
				byte flags[] = new byte[4];
				System.arraycopy(firstframe, offset + length, flags, 0, flags.length);
				length += flags.length;
				// Read number of frames (if available).
				if ((flags[3] & (byte) (1 << 0)) != 0)
				{
					System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
					h_vbr_frames = (tmp[0] << 24)&0xFF000000 | (tmp[1] << 16)&0x00FF0000 | (tmp[2] << 8)&0x0000FF00 | tmp[3]&0x000000FF;
					length += 4;	
				}
				// Read size (if available).
				if ((flags[3] & (byte) (1 << 1)) != 0)
				{
					System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
					h_vbr_bytes = (tmp[0] << 24)&0xFF000000 | (tmp[1] << 16)&0x00FF0000 | (tmp[2] << 8)&0x0000FF00 | tmp[3]&0x000000FF;
					length += 4;	
				}
				// Read TOC (if available).
				if ((flags[3] & (byte) (1 << 2)) != 0)
				{
					System.arraycopy(firstframe, offset + length, h_vbr_toc, 0, h_vbr_toc.length);
					length += h_vbr_toc.length;	
				}
				// Read scale (if available).
				if ((flags[3] & (byte) (1 << 3)) != 0)
				{
					System.arraycopy(firstframe, offset + length, tmp, 0, tmp.length);
					h_vbr_scale = (tmp[0] << 24)&0xFF000000 | (tmp[1] << 16)&0x00FF0000 | (tmp[2] << 8)&0x0000FF00 | tmp[3]&0x000000FF;
					length += 4;	
				}				
			}				
		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			throw new BitstreamException("XingVBRHeader Corrupted",e);
		}
	}
	
	// Functions to query header contents:
	/**
	 * Returns version.
	 */
	public int version() { return h_version; }

	/**
	 * Returns Layer ID.
	 */
	public int layer() { return h_layer; }

	/**
	 * Returns bitrate index.
	 */
	public int bitrate_index() { return h_bitrate_index; }

	/**
	 * Returns Sample Frequency.
	 */
	public int sample_frequency() { return h_sample_frequency; }

	/**
	 * Returns Frequency.
	 */
	public int frequency() {return frequencies[h_version][h_sample_frequency];}

	/**
	 * Returns Mode.
	 */
	public int mode() { return h_mode; }

	/**
	 * Returns Protection bit.
	 */
	public boolean checksums()
	{
		if (h_protection_bit == 0) return true;
	  else return false;
	}

	/**
	 * Returns Copyright.
	 */
	public boolean copyright() { return h_copyright; }

	/**
	 * Returns Original.
	 */
	public boolean original() { return h_original; }

	/**
	 * Return VBR.
	 * @return true if VBR header is found
	 */
	public boolean vbr() { return h_vbr; }

	/**
	 * Return VBR scale.
	 * @return scale of -1 if not available

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -