public MPAInfo(MPAHeader hdr, UInt64 duration) { ai_ = new AudioInfo(); ai_.bitRate = MPAHeaderParser.bitrate(hdr); ai_.bitsPerSample = 16; ai_.channels = (UInt16)(hdr.channelMode < 3 ? 2 : 1); ai_.sampleRate = MPAHeaderParser.samplingRate(hdr); ai_.layer = (UInt16)(4 - hdr.layer); ai_.vbr = false; //todo duration_ = duration; switch (hdr.version) { case 3: { mt_ = MediaType.MPEG10; break; } case 2: { mt_ = MediaType.MPEG20; break; } case 0: { mt_ = MediaType.MPEG25; break; } } }
public bool isEqual(MPAHeader hdr) { return hdr.valid && valid && hdr.samplingRate == samplingRate && hdr.version == version && hdr.layer == layer; }
public static UInt16 samplingRate(MPAHeader hdr) { return MPASr[3 - hdr.version][hdr.samplingRate]; }
public static UInt16 samples(MPAHeader hdr) { return MPASpf[3 - hdr.version][3 - hdr.layer]; }
public static MPAHeader parse(Bitstream stream) { MPAHeader hdr = new MPAHeader(); hdr.valid = false; do { hdr.sync = (UInt16)(stream.read(11)); hdr.version = (UInt16)(stream.read(2)); hdr.layer = (UInt16)(stream.read(2)); hdr.prot = (UInt16)(1 - stream.read(1)); hdr.bitRate = (UInt16)(stream.read(4)); hdr.samplingRate = (UInt16)(stream.read(2)); hdr.padding = (UInt16)(stream.read(1)); hdr.priv = (UInt16)(stream.read(1)); hdr.channelMode = (UInt16)(stream.read(2)); hdr.modeExtension = (UInt16)(stream.read(2)); hdr.copyright = (UInt16)(stream.read(1)); hdr.original = (UInt16)(stream.read(1)); hdr.emphasis = (UInt16)(stream.read(2)); hdr.valid = (hdr.sync == 0x7FF) && (hdr.bitRate != 0) && (hdr.bitRate != 15) && (hdr.version != 0) && (hdr.layer != 1); } while (false); return hdr; }
public static MPAHeader parse(byte[] buff, int offset) { MPAHeader hdr = new MPAHeader(); hdr.valid = false; do { if (buff == null || buff.Length < 4 + offset) { break; } hdr.sync = (UInt16)((buff[offset + 0] << 3) + (buff[offset + 1] >> 5)); hdr.version = (UInt16)((buff[offset + 1] >> 3) & 0x03); hdr.layer = (UInt16)((buff[offset + 1] >> 1) & 0x03); hdr.prot = (UInt16)(1 - (buff[offset + 1] & 0x01)); hdr.bitRate = (UInt16)((buff[offset + 2] >> 4) & 0x0F); hdr.samplingRate = (UInt16)((buff[offset + 2] >> 2) & 0x03); hdr.padding = (UInt16)((buff[offset + 2] >> 1) & 0x01); hdr.priv = (UInt16)(buff[offset + 2] & 0x01); hdr.channelMode = (UInt16)((buff[offset + 3] >> 6) & 0x03); hdr.modeExtension = (UInt16)((buff[offset + 3] >> 4) & 0x03); hdr.copyright = (UInt16)((buff[offset + 3] >> 3) & 0x01); hdr.original = (UInt16)((buff[offset + 3] >> 2) & 0x01); hdr.emphasis = (UInt16)(buff[offset + 3] & 0x03); hdr.valid = (hdr.sync == 0x7FF) && (hdr.bitRate != 0) && (hdr.bitRate != 15) && (hdr.version != 0) && (hdr.layer != 1); } while (false); return hdr; }
public static UInt32 mpaPacketSize(MPAHeader hdr) { return (samples(hdr) * bitrate(hdr) / (8 * (UInt32)samplingRate(hdr))) + hdr.padding; }
public static UInt32 bitrate(MPAHeader hdr) { switch (hdr.version) { case 3: { return MPABr[hdr.bitRate][3 - hdr.layer]; } default: { return MPABr[hdr.bitRate][3 + (hdr.layer < 3 ? 1 : 0)]; } } }
public static int getIndex(MPAHeader hdr) { if (hdr.version == 2) //MPEG2 { return 4; } UInt32 bitrate = (UInt32)((MPAHeaderParser.bitrate(hdr) / 1000) / ((hdr.channelMode == 3) ? 1 : 2)); switch (hdr.samplingRate) { case 0: { switch (bitrate) { case 32: case 48: return 2; case 56: case 64: case 80: return 0; case 96: case 112: case 128: case 160: case 192: return 1; } Debug.Assert(false); } break; case 1: { switch (bitrate) { case 32: case 48: return 2; case 56: case 64: case 80: case 96: case 112: case 128: case 160: case 192: return 0; } Debug.Assert(false); } break; case 2: { switch (bitrate) { case 32: case 48: return 3; case 56: case 64: case 80: return 0; case 96: case 112: case 128: case 160: case 192: return 1; } Debug.Assert(false); } break; default: { Debug.Assert(false); } break; }; Debug.Assert(false); return 0; }
/////////////////////////////////////////// private void checkError(MPAHeader hdr) { if (hdr.prot > 0) { strm_.skip(16); } }
public ErrorCode nextSample(int streamId, out ISample sample) { sample = null; MPAHeader hdr = new MPAHeader(); hdr.valid = false; byte[] tmp = new byte[4]; while (!hdr.valid) { tmp[0] = tmp[1]; tmp[1] = tmp[2]; tmp[2] = tmp[3]; reader_.read1(tmp, 3); hdr = MPAHeaderParser.parse(tmp, 0); } byte[] buffer = new byte[MPAHeaderParser.mpaPacketSize(hdr)]; System.Buffer.BlockCopy(tmp, 0, buffer, 0, 4); int size = (int)MPAHeaderParser.mpaPacketSize(hdr) - 4; reader_.read(buffer, ref size, 4); if (size < (int)length_ - 4) { return ErrorCode.EndOfStream; } UInt64 start = 100000 * (UInt64)pos_ * samples_ / info_.getAudioInfo().sampleRate; UInt64 stop = 100000 * (UInt64)(pos_ + 1) * samples_ / info_.getAudioInfo().sampleRate; sample = new Sample(buffer, start, stop, seek_, discontinuous_); discontinuous_ = false; ++pos_; return ErrorCode.Success; }