private uint ReadUInt32() { byte[] x = new byte[4]; _fs.Read(x, 0, 4); _fileOffset += 4; return(BitConverterBE.ToUInt32(x, 0)); }
private void ParseMP3Frames(byte[] buff) { int[] MPEG1BitRate = new int[] { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 }; int[] MPEG2XBitRate = new int[] { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 }; int[] MPEG1SampleRate = new int[] { 44100, 48000, 32000, 0 }; int[] MPEG20SampleRate = new int[] { 22050, 24000, 16000, 0 }; int[] MPEG25SampleRate = new int[] { 11025, 12000, 8000, 0 }; int offset = 0; int length = buff.Length; while (length >= 4) { ulong header; int mpegVersion, layer, bitRate, sampleRate, padding, channelMode; int frameLen; header = (ulong)BitConverterBE.ToUInt32(buff, offset) << 32; if (BitHelper.Read(ref header, 11) != 0x7FF) { break; } mpegVersion = BitHelper.Read(ref header, 2); layer = BitHelper.Read(ref header, 2); BitHelper.Read(ref header, 1); bitRate = BitHelper.Read(ref header, 4); sampleRate = BitHelper.Read(ref header, 2); padding = BitHelper.Read(ref header, 1); BitHelper.Read(ref header, 1); channelMode = BitHelper.Read(ref header, 2); if ((mpegVersion == 1) || (layer != 1) || (bitRate == 0) || (bitRate == 15) || (sampleRate == 3)) { break; } bitRate = ((mpegVersion == 3) ? MPEG1BitRate[bitRate] : MPEG2XBitRate[bitRate]) * 1000; if (mpegVersion == 3) { sampleRate = MPEG1SampleRate[sampleRate]; } else if (mpegVersion == 2) { sampleRate = MPEG20SampleRate[sampleRate]; } else { sampleRate = MPEG25SampleRate[sampleRate]; } frameLen = GetFrameLength(mpegVersion, bitRate, sampleRate, padding); if (frameLen > length) { break; } bool isVBRHeaderFrame = false; if (_frameOffsets.Count == 0) { // Check for an existing VBR header just to be safe (I haven't seen any in FLVs) int o = offset + GetFrameDataOffset(mpegVersion, channelMode); if (BitConverterBE.ToUInt32(buff, o) == 0x58696E67) { // "Xing" isVBRHeaderFrame = true; _delayWrite = false; _hasVBRHeader = true; } } if (isVBRHeaderFrame) { } else if (_firstBitRate == 0) { _firstBitRate = bitRate; _mpegVersion = mpegVersion; _sampleRate = sampleRate; _channelMode = channelMode; _firstFrameHeader = BitConverterBE.ToUInt32(buff, offset); } else if (!_isVBR && (bitRate != _firstBitRate)) { _isVBR = true; if (_hasVBRHeader) { } else if (_delayWrite) { WriteVBRHeader(true); _writeVBRHeader = true; _delayWrite = false; } else { _warnings.Add("Detected VBR too late, cannot add VBR header."); } } _frameOffsets.Add(_totalFrameLength + (uint)offset); offset += frameLen; length -= frameLen; } _totalFrameLength += (uint)buff.Length; }