public byte[] Decode(Stream stream) { using (var ms = new MemoryStream()) { var br = new BitReader(stream); int i = 0; int prevValue = -1; byte lastChar = 0; prevValue = br.ReadBits(lookup.CodeLen); lastChar = lookup.OutputText(lastChar, prevValue, ms); while (br.BitsRead < br.BitsLength) { if (lookup.Count >= 0xFFF) { if (br.BitsRead + lookup.CodeLen <= br.BitsLength) { lookup.Reset(); prevValue = br.ReadBits(lookup.CodeLen); lastChar = lookup.OutputText(lastChar, prevValue, ms); } else { break; } continue; } int value; if (br.BitsRead + lookup.CodeLen <= br.BitsLength) { lookup.EnsureCodeLen(); value = br.ReadBits(lookup.CodeLen); } else { break; } if (value != lookup.Count) { lastChar = lookup.OutputText(lastChar, value, ms); if (prevValue != -1) lookup.AppendChar(lastChar, prevValue); } else { if (prevValue != -1) lookup.AppendChar(lastChar, prevValue); lastChar = lookup.OutputText(lastChar, value, ms); } prevValue = value; } return ms.ToArray(); } }
public override void ReadSetupData( VorbisCodec codec, BitReader reader ) { blockFlag = reader.ReadBit(); int windowType = reader.ReadBits( 16 ); int transformType = reader.ReadBits( 16 ); modeMapping = reader.ReadBits( 8 ); if( windowType > 0 || transformType > 0 ) throw new InvalidDataException( "windowType and/or transformType not 0" ); }
public override void ReadSetupData( VorbisCodec codec, BitReader reader ) { submaps = reader.ReadBit() == 0 ? 1 : reader.ReadBits( 4 ) + 1; couplingSteps = reader.ReadBit() == 0 ? 0 : reader.ReadBits( 8 ) + 1; int channels = codec.channels; if( couplingSteps > 0 && channels > 1 ) { magnitude = new byte[couplingSteps]; angle = new byte[couplingSteps]; int bits = VorbisUtils.iLog( channels - 1 ); for( int i = 0; i < couplingSteps; i++ ) { magnitude[i] = (byte)reader.ReadBits( bits ); angle[i] = (byte)reader.ReadBits( bits ); } } if( reader.ReadBits( 2 ) != 0 ) throw new InvalidDataException( "Reserved field not 0!" ); if( submaps > 1 ) { mux = new byte[channels]; for( int i = 0; i < mux.Length; i++ ) mux[i] = (byte)reader.ReadBits( 4 ); } else { mux = new byte[channels]; } submapFloor = new byte[submaps]; submapResidue = new byte[submaps]; for( int i = 0; i < submaps; i++ ) { reader.ReadBits( 8 ); // unused time configuration submapFloor[i] = (byte)reader.ReadBits( 8 ); submapResidue[i] = (byte)reader.ReadBits( 8 ); } }
public void TestReadBits() { MemoryStream stream = new MemoryStream(new byte[] { 0xe4, 0x1b, 0x1b, 0xf1, 0xff, 0xbb}); // 11100100, 00011011, 00011011 BitReader reader = new BitReader(stream); Expect(reader.ReadBits(1), EqualTo(0x1)); Expect(reader.ReadBits(2), EqualTo(0x3)); Expect(reader.ReadBits(5), EqualTo(0x4)); ExpectPosition(1, stream); Expect(reader.ReadBits(7), EqualTo(0xD)); ExpectPosition(2, stream); Expect(reader.ReadBits(8), EqualTo(0x8D)); ExpectPosition(3, stream); Expect(reader.ReadBits(2), EqualTo(0x3)); ExpectPosition(4, stream); Expect(reader.ReadByte(), EqualTo(0xff)); ExpectPosition(5, stream); Expect(reader.ReadBits(4), EqualTo(0xb)); Expect(reader.ReadBits(4), EqualTo(0xb)); }
public IEnumerable<AudioChunk> StreamData( Stream source ) { info = new AudioChunk(); PrimitiveReader reader = new PrimitiveReader( source ); while( true ) { // Read frame header BitReader bitReader = new BitReader( reader ); bitReader.BigEndian = true; //Console.WriteLine( "start pos" + reader.Position ); // Skip any padding('00') bytes before the start of a frame byte data = 0; while( ( data = reader.ReadByte() ) == 0 ); // Make sure that the 'data' byte is the first 8 bits of the sync word. if( data != 0xFF ) { throw new InvalidDataException( "Invalid frame sync value." ); } int frameSync = bitReader.ReadBits( 3/*11*/ ); if( frameSync != 0x7/*FF*/ ) { throw new InvalidDataException( "Invalid frame sync value." ); } int versionId = bitReader.ReadBits( 2 ); int layerIndex = bitReader.ReadBits( 2 ); bool crcProtection = bitReader.ReadBit() == 0; int bitrateIndex = bitReader.ReadBits( 4 ); int samplingRateIndex = bitReader.ReadBits( 2 ); bool padded = bitReader.ReadBit() != 0; int privateBit = bitReader.ReadBit(); int channelMode = bitReader.ReadBits( 2 ); int modeExtension = bitReader.ReadBits( 2 ); int copyrightBit = bitReader.ReadBit(); int originalBit = bitReader.ReadBit(); int emphasis = bitReader.ReadBits( 2 ); int bitrate = GetBitRate( (MpegVersion)versionId, layerIndex, bitrateIndex ); info.Frequency = samplingRates[versionId][samplingRateIndex]; ushort crc = 0; if( crcProtection ) { crc = (ushort)bitReader.ReadBits( 16 ); } MpegFrame frame = new MpegFrame(); frame.Bitrate = bitrate; frame.ChannelMode = (ChannelMode)channelMode; frame.Channels = Common.GetNumberOfChannels( frame.ChannelMode ); frame.CrcProtected = crcProtection; frame.Padding = padded; frame.ModeExtension = modeExtension; frame.SampleRate = info.Frequency; frame.Emphasis = emphasis; frame.Version = (MpegVersion)versionId; LayerIndex index2 = (LayerIndex)layerIndex; info.Data = null; //Console.WriteLine( "padding: {0}, type: {1}, sr: {2}, br: {3}", //frame.Padding, index2, frame.SampleRate, frame.Bitrate ); if( layerIndex == (int)LayerIndex.Layer1 ) { info.Data = decoder1.Decode( frame, bitReader ); } else if( layerIndex == (int)LayerIndex.Layer2 ) { info.Data = decoder2.Decode( frame, bitReader ); } else if( layerIndex == (int)LayerIndex.Layer3 ) { throw new NotSupportedException( "Layer III not supported" ); } else { throw new InvalidDataException( "Invalid layer" ); } info.Channels = frame.Channels; info.BitsPerSample = 16; //if( bitReader.offset == 8 ) { //reader.ReadByte(); //} yield return info; } }
public override void ReadSetupData( VorbisCodec codec, BitReader reader ) { int syncPattern = reader.ReadBits( 24 ); if( syncPattern != 0x564342 ) { throw new InvalidDataException( "Invalid codebook sync pattern: " + syncPattern ); } int dimensions = reader.ReadBits( 16 ); int entries = reader.ReadBits( 24 ); int ordered = reader.ReadBit(); codewordLengths = new byte[entries]; if( ordered == 0 ) { int sparse = reader.ReadBit(); for( int i = 0; i < entries; i++ ) { if( sparse == 0 || ( reader.ReadBit() == 1 ) ) { codewordLengths[i] = (byte)( reader.ReadBits( 5 ) + 1 ); } } } else { int curEntry = 0; int curLength = reader.ReadBits( 5 ) + 1; while( curEntry < entries ) { int number = reader.ReadBits( VorbisUtils.iLog( entries - curEntry ) ); for( int i = curEntry; i < curEntry + number; i++ ) { codewordLengths[i] = (byte)curLength; } curEntry += number; curLength++; } } BuildHuffmanTree(); int lookupType = reader.ReadBits( 4 ); if( lookupType == 1 || lookupType == 2 ) { float minValue = VorbisUtils.Unpack( reader.ReadBitsU( 32 ) ); float deltaValue = VorbisUtils.Unpack( reader.ReadBitsU( 32 ) ); int valueBits = reader.ReadBits( 4 ) + 1; int sequenceP = reader.ReadBit(); int lookupValues = 0; if( lookupType == 1 ) { lookupValues = VorbisUtils.lookup1_values( entries, dimensions ); } else { lookupValues = entries * dimensions; } multiplicands = new ushort[lookupValues]; for( int i = 0; i < multiplicands.Length; i++ ) { multiplicands[i] = (ushort)reader.ReadBits( valueBits ); } VQ = new float[entries][]; for( int i = 0; i < entries; i++ ) { float[] vector = new float[dimensions]; if( lookupType == 1 ) { float last = 0; int indexDivisor = 1; for( int j = 0; j < dimensions; j++ ) { int multiplicandOffset = ( i / indexDivisor ) % lookupValues; vector[j] = multiplicands[multiplicandOffset] * deltaValue + minValue + last; if( sequenceP != 0 ) last = vector[j]; indexDivisor *= lookupValues; } } else { float last = 0; int mulitiplicandOffset = i * dimensions; for( int j = 0; j < dimensions; j++ ) { vector[j] = multiplicands[mulitiplicandOffset] * deltaValue + minValue + last; if( sequenceP != 0 ) last = vector[j]; mulitiplicandOffset++; } } VQ[i] = vector; } } }