📄 mpegframeheader.java
字号:
/**
* @author : Paul Taylor
* <p/>
* Version @version:$Id: MPEGFrameHeader.java,v 1.13 2007/11/29 19:32:57 paultaylor Exp $
* Date :${DATE}
* <p/>
* Jaikoz Copyright Copyright (C) 2003 -2005 JThink Ltd
*/
package com.hadeslee.audiotag.audio.mp3;
import com.hadeslee.audiotag.FileConstants;
import com.hadeslee.audiotag.audio.exceptions.InvalidAudioFrameException;
import com.hadeslee.audiotag.logging.AbstractTagDisplayFormatter;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
/**
* Represents a MPEGFrameHeader, an MP3 is made up of a number of frames each frame starts with a four
* byte frame header.
*/
public class MPEGFrameHeader
{
/**
* Constants for MP3 Frame header, each frame has a basic header of
* 4 bytes
*/
private static final int BYTE_1 = 0;
private static final int BYTE_2 = 1;
private static final int BYTE_3 = 2;
private static final int BYTE_4 = 3;
public static final int HEADER_SIZE = 4;
/**
* Sync Value to identify the start of an MPEGFrame
*/
public static final int SYNC_SIZE = 2;
public static final int SYNC_BYTE1 = 0xFF;
public static final int SYNC_BYTE2 = 0xE0;
private static int position = 0;
private static final byte[] header = new byte[HEADER_SIZE];
/**
* Constants for MPEG Version
*/
public static final Map mpegVersionMap = new HashMap();
public final static int VERSION_2_5 = 0;
public final static int VERSION_2 = 2;
public final static int VERSION_1 = 3;
static
{
mpegVersionMap.put(VERSION_2_5, "MPEG-2.5");
mpegVersionMap.put(VERSION_2 , "MPEG-2");
mpegVersionMap.put(VERSION_1 , "MPEG-1");
}
/**
* Constants for MPEG Layer
*/
public static final Map mpegLayerMap = new HashMap();
public final static int LAYER_I = 3;
public final static int LAYER_II = 2;
public final static int LAYER_III = 1;
static
{
mpegLayerMap.put(LAYER_I, "Layer 1");
mpegLayerMap.put(LAYER_II, "Layer 2");
mpegLayerMap.put(LAYER_III, "Layer 3");
}
/**
* Slot Size is dependent on Layer
*/
public final static int LAYER_I_SLOT_SIZE = 4;
public final static int LAYER_II_SLOT_SIZE = 1;
public final static int LAYER_III_SLOT_SIZE = 1;
/**
* Bit Rates, the setBitrate varies for different Version and Layer
*/
private static final Map bitrateMap = new HashMap();
static
{
// MPEG-1, Layer I (E)
bitrateMap.put(0x1E, 32);
bitrateMap.put(0x2E, 64);
bitrateMap.put(0x3E, 96);
bitrateMap.put(0x4E, 128);
bitrateMap.put(0x5E, 160);
bitrateMap.put(0x6E, 192);
bitrateMap.put(0x7E, 224);
bitrateMap.put(0x8E, 256);
bitrateMap.put(0x9E, 288);
bitrateMap.put(0xAE, 320);
bitrateMap.put(0xBE, 352);
bitrateMap.put(0xCE, 384);
bitrateMap.put(0xDE, 416);
bitrateMap.put(0xEE, 448);
// MPEG-1, Layer II (C)
bitrateMap.put(0x1C, 32);
bitrateMap.put(0x2C, 48);
bitrateMap.put(0x3C, 56);
bitrateMap.put(0x4C, 64);
bitrateMap.put(0x5C, 80);
bitrateMap.put(0x6C, 96);
bitrateMap.put(0x7C, 112);
bitrateMap.put(0x8C, 128);
bitrateMap.put(0x9C, 160);
bitrateMap.put(0xAC, 192);
bitrateMap.put(0xBC, 224);
bitrateMap.put(0xCC, 256);
bitrateMap.put(0xDC, 320);
bitrateMap.put(0xEC, 384);
// MPEG-1, Layer III (A)
bitrateMap.put(0x1A, 32);
bitrateMap.put(0x2A, 40);
bitrateMap.put(0x3A, 48);
bitrateMap.put(0x4A, 56);
bitrateMap.put(0x5A, 64);
bitrateMap.put(0x6A, 80);
bitrateMap.put(0x7A, 96);
bitrateMap.put(0x8A, 112);
bitrateMap.put(0x9A, 128);
bitrateMap.put(0xAA, 160);
bitrateMap.put(0xBA, 192);
bitrateMap.put(0xCA, 224);
bitrateMap.put(0xDA, 256);
bitrateMap.put(0xEA, 320);
// MPEG-2, Layer I (6)
bitrateMap.put(0x16, 32);
bitrateMap.put(0x26, 48);
bitrateMap.put(0x36, 56);
bitrateMap.put(0x46, 64);
bitrateMap.put(0x56, 80);
bitrateMap.put(0x66, 96);
bitrateMap.put(0x76, 112);
bitrateMap.put(0x86, 128);
bitrateMap.put(0x96, 144);
bitrateMap.put(0xA6, 160);
bitrateMap.put(0xB6, 176);
bitrateMap.put(0xC6, 192);
bitrateMap.put(0xD6, 224);
bitrateMap.put(0xE6, 256);
// MPEG-2, Layer II (4)
bitrateMap.put(0x14, 8);
bitrateMap.put(0x24, 16);
bitrateMap.put(0x34, 24);
bitrateMap.put(0x44, 32);
bitrateMap.put(0x54, 40);
bitrateMap.put(0x64, 48);
bitrateMap.put(0x74, 56);
bitrateMap.put(0x84, 64);
bitrateMap.put(0x94, 80);
bitrateMap.put(0xA4, 96);
bitrateMap.put(0xB4, 112);
bitrateMap.put(0xC4, 128);
bitrateMap.put(0xD4, 144);
bitrateMap.put(0xE4, 160);
// MPEG-2, Layer III (2)
bitrateMap.put(0x12, 8);
bitrateMap.put(0x22, 16);
bitrateMap.put(0x32, 24);
bitrateMap.put(0x42, 32);
bitrateMap.put(0x52, 40);
bitrateMap.put(0x62, 48);
bitrateMap.put(0x72, 56);
bitrateMap.put(0x82, 64);
bitrateMap.put(0x92, 80);
bitrateMap.put(0xA2, 96);
bitrateMap.put(0xB2, 112);
bitrateMap.put(0xC2, 128);
bitrateMap.put(0xD2, 144);
bitrateMap.put(0xE2, 160);
}
/**
* Constants for Channel mode
*/
protected static final Map modeMap = new HashMap();
public final static int MODE_STEREO = 0;
public final static int MODE_JOINT_STEREO = 1;
public final static int MODE_DUAL_CHANNEL = 2;
public final static int MODE_MONO = 3;
static
{
modeMap.put(MODE_STEREO, "Stereo");
modeMap.put(MODE_JOINT_STEREO, "Joint Stereo");
modeMap.put(MODE_DUAL_CHANNEL, "Dual");
modeMap.put(MODE_MONO, "Mono");
}
/**
* Constants for Emphasis
*/
private static final Map emphasisMap = new HashMap();
public final static int EMPHASIS_NONE = 0;
public final static int EMPHASIS_5015MS = 1;
public final static int EMPHASIS_RESERVED = 2;
public final static int EMPHASIS_CCITT = 3;
static
{
emphasisMap.put(EMPHASIS_NONE, "None");
emphasisMap.put(EMPHASIS_5015MS, "5015MS");
emphasisMap.put(EMPHASIS_RESERVED, "Reserved");
emphasisMap.put(EMPHASIS_CCITT, "CCITT");
}
private static final Map modeExtensionMap = new HashMap();
private final static int MODE_EXTENSION_NONE = 0;
private final static int MODE_EXTENSION_ONE = 1;
private final static int MODE_EXTENSION_TWO = 2;
private final static int MODE_EXTENSION_THREE = 3;
private static final Map modeExtensionLayerIIIMap = new HashMap();
private final static int MODE_EXTENSION_OFF_OFF = 0;
private final static int MODE_EXTENSION_ON_OFF = 1;
private final static int MODE_EXTENSION_OFF_ON = 2;
private final static int MODE_EXTENSION_ON_ON = 3;
static
{
modeExtensionMap.put(MODE_EXTENSION_NONE, "4-31");
modeExtensionMap.put(MODE_EXTENSION_ONE, "8-31");
modeExtensionMap.put(MODE_EXTENSION_TWO, "12-31");
modeExtensionMap.put(MODE_EXTENSION_THREE, "16-31");
modeExtensionLayerIIIMap.put(MODE_EXTENSION_OFF_OFF, "off-off");
modeExtensionLayerIIIMap.put(MODE_EXTENSION_ON_OFF, "on-off");
modeExtensionLayerIIIMap.put(MODE_EXTENSION_OFF_ON, "off-on");
modeExtensionLayerIIIMap.put(MODE_EXTENSION_ON_ON, "on-on");
}
/**
* Sampling Rate in Hz
*/
private static final Map samplingRateMap = new HashMap();
private static final Map samplingV1Map = new HashMap();
private static final Map samplingV2Map = new HashMap();
private static final Map samplingV25Map = new HashMap();
static
{
samplingV1Map.put(0, 44100);
samplingV1Map.put(1, 48000);
samplingV1Map.put(2, 32000);
samplingV2Map.put(0, 22050);
samplingV2Map.put(1, 24000);
samplingV2Map.put(2, 16000);
samplingV25Map.put(0, 11025);
samplingV25Map.put(1, 12000);
samplingV25Map.put(2, 8000);
samplingRateMap.put(VERSION_1, samplingV1Map);
samplingRateMap.put(VERSION_2, samplingV2Map);
samplingRateMap.put(VERSION_2_5, samplingV25Map);
}
/* Samples Per Frame */
private static final Map samplesPerFrameMap = new HashMap();
private static final Map samplesPerFrameV1Map = new HashMap();
private static final Map samplesPerFrameV2Map = new HashMap();
private static final Map samplesPerFrameV25Map = new HashMap();
static
{
samplesPerFrameV1Map.put(LAYER_I , 384);
samplesPerFrameV1Map.put(LAYER_II , 1152);
samplesPerFrameV1Map.put(LAYER_III , 1152);
samplesPerFrameV2Map.put(LAYER_I , 384);
samplesPerFrameV2Map.put(LAYER_II , 1152);
samplesPerFrameV2Map.put(LAYER_III , 1152);
samplesPerFrameV25Map.put(LAYER_I , 384);
samplesPerFrameV25Map.put(LAYER_II , 1152);
samplesPerFrameV25Map.put(LAYER_III, 1152);
samplesPerFrameMap.put(VERSION_1 , samplesPerFrameV1Map);
samplesPerFrameMap.put(VERSION_2 , samplesPerFrameV2Map);
samplesPerFrameMap.put(VERSION_2_5, samplesPerFrameV25Map);
}
private static final int SCALE_BY_THOUSAND = 1000;
private static final int LAYER_I_FRAME_SIZE_COEFFICIENT = 12;
private static final int LAYER_II_FRAME_SIZE_COEFFICIENT = 144;
private static final int LAYER_III_FRAME_SIZE_COEFFICIENT = 144;
/**
* MP3 Frame Header bit mask
*/
private static final int MASK_MP3_ID = FileConstants.BIT3;
/**
* MP3 version, confusingly for MP3s the version is 1.
*/
private static final int MASK_MP3_VERSION = FileConstants.BIT4 | FileConstants.BIT3;
/**
* MP3 Layer, for MP3s the Layer is 3
*/
private static final int MASK_MP3_LAYER = FileConstants.BIT2 | FileConstants.BIT1;
/**
* Does it include a CRC Checksum at end of header, this can be used to check the header.
*/
private static final int MASK_MP3_PROTECTION = FileConstants.BIT0;
/**
* The setBitrate of this MP3
*/
private static final int MASK_MP3_BITRATE = FileConstants.BIT7 | FileConstants.BIT6 | FileConstants.BIT5 |
FileConstants.BIT4;
/**
* The sampling/frequency rate
*/
private static final int MASK_MP3_FREQUENCY = FileConstants.BIT3 + FileConstants.BIT2;
/**
* An extra padding bit is sometimes used to make sure frames are exactly the right length
*/
private static final int MASK_MP3_PADDING = FileConstants.BIT1;
/**
* Private bit set, for application specific
*/
private static final int MASK_MP3_PRIVACY = FileConstants.BIT0;
/**
* Channel Mode, Stero/Mono/Dual Channel
*/
private static final int MASK_MP3_MODE = FileConstants.BIT7 | FileConstants.BIT6;
/**
* MP3 Frame Header bit mask
*/
private static final int MASK_MP3_MODE_EXTENSION = FileConstants.BIT5 | FileConstants.BIT4;
/**
* MP3 Frame Header bit mask
*/
private static final int MASK_MP3_COPY = FileConstants.BIT3;
/**
* MP3 Frame Header bit mask
*/
private static final int MASK_MP3_HOME = FileConstants.BIT2;
/**
* MP3 Frame Header bit mask
*/
private static final int MASK_MP3_EMPHASIS = FileConstants.BIT1 | FileConstants.BIT0;
private byte[] mpegBytes;
/**
* The version of this MPEG frame (see the constants)
*/
private int version;
private String versionAsString;
/**
* Contains the mpeg layer of this frame (see constants)
*/
private int layer;
private String layerAsString;
/**
* Bitrate of this frame
*/
private Integer bitRate;
/**
* Channel Mode of this Frame (see constants)
*/
private int channelMode;
/**
* Channel Mode of this Frame As English String
*/
private String channelModeAsString;
/**
* Emphasis of this frame
*/
private int emphasis;
/**
*
* Emphasis mode string
*/
private String emphasisAsString;
/**
* Mode Extension
*/
private String modeExtension;
/**
* Flag indicating if this frame has padding byte
*/
private boolean isPadding;
/**
* Flag indicating if this frame contains copyrighted material
*/
private boolean isCopyrighted;
/**
* Flag indicating if this frame contains original material
*/
private boolean isOriginal;
/**
* Flag indicating if this frame is protected
*/
private boolean isProtected;
/**
* Flag indicating if this frame is private
*/
private boolean isPrivate;
private Integer samplingRate;
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -