void ProcessParameterChange(DataPacket packet) { _parameterChangePacket = null; // try to do a stream header... var wasPeek = false; var doFullReset = false; if (ProcessStreamHeader(packet)) { packet.Done(); wasPeek = true; doFullReset = true; packet = _packetProvider.PeekNextPacket(); if (packet == null) { throw new InvalidDataException("Couldn't get next packet!"); } } // try to do a comment header... if (LoadComments(packet)) { if (wasPeek) { _packetProvider.GetNextPacket().Done(); } else { packet.Done(); } wasPeek = true; packet = _packetProvider.PeekNextPacket(); if (packet == null) { throw new InvalidDataException("Couldn't get next packet!"); } } // try to do a book header... if (LoadBooks(packet)) { if (wasPeek) { _packetProvider.GetNextPacket().Done(); } else { packet.Done(); } } ResetDecoder(doFullReset); }
public void ProcessParameterChange(DataPacket packet) { _parameterChangePacket = null; bool flag = false; bool isFullReset = false; if (ProcessStreamHeader(packet)) { packet.Done(); flag = true; isFullReset = true; packet = _packetProvider.PeekNextPacket(); if (packet == null) { throw new InvalidDataException("Couldn't get next packet!"); } } if (LoadComments(packet)) { if (flag) { _packetProvider.GetNextPacket().Done(); } else { packet.Done(); } flag = true; packet = _packetProvider.PeekNextPacket(); if (packet == null) { throw new InvalidDataException("Couldn't get next packet!"); } } if (LoadBooks(packet)) { if (flag) { _packetProvider.GetNextPacket().Done(); } else { packet.Done(); } } ResetDecoder(isFullReset); }
internal bool TryInit() { var initialPacket = _packetProvider.GetNextPacket(); // make sure it's a vorbis stream... if (!initialPacket.ReadBytes(7).SequenceEqual(new byte[] { 0x01, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 })) { _glueBits += initialPacket.Length * 8; return(false); } _glueBits += 56; // now load the initial header ProcessStreamHeader(initialPacket); // finally, load the comment and book headers... DataPacket commentsPacket = null, booksPacket = null; while (commentsPacket == null || booksPacket == null) { var packet = _packetProvider.GetNextPacket(); if (packet.IsResync) { throw new InvalidDataException("Missing header packets!"); } if (!_pagesSeen.Contains(packet.PageSequenceNumber)) { _pagesSeen.Add(packet.PageSequenceNumber); } switch (packet.PeekByte()) { case 1: throw new InvalidDataException("Found second init header!"); case 3: LoadComments(packet); commentsPacket = packet; break; case 5: LoadBooks(packet); booksPacket = packet; break; } } // tell the packets that we're done with them initialPacket.Done(); commentsPacket.Done(); booksPacket.Done(); // get the decoding logic bootstrapped InitDecoder(); return(true); }
internal bool TryInit() { if (!ProcessStreamHeader(_packetProvider.PeekNextPacket())) { return(false); } _packetProvider.GetNextPacket().Done(); DataPacket nextPacket = _packetProvider.GetNextPacket(); if (!LoadComments(nextPacket)) { throw new InvalidDataException("Comment header was not readable!"); } nextPacket.Done(); nextPacket = _packetProvider.GetNextPacket(); if (!LoadBooks(nextPacket)) { throw new InvalidDataException("Book header was not readable!"); } nextPacket.Done(); InitDecoder(); return(true); }
void DecodeNextPacket() { _sw.Start(); DataPacket packet = null; try { // get the next packet var packetProvider = _packetProvider; if (packetProvider != null) { packet = packetProvider.GetNextPacket(); } // if the packet is null, we've hit the end or the packet reader has been disposed... if (packet == null) { _eosFound = true; return; } // keep our page count in sync if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber))) { _pagesSeen.Add(_lastPageSeen); } // check for resync if (packet.IsResync) { ResetDecoder(false); // if we're a resync, our current decoder state is invalid... } // check for parameter change if (packet == _parameterChangePacket) { _isParameterChange = true; ProcessParameterChange(packet); return; } if (!UnpackPacket(packet)) { packet.Done(); _wasteBits += 8 * packet.Length; return; } packet.Done(); // we can now safely decode all the data without having to worry about a corrupt or partial packet DecodePacket(); var samplesDecoded = OverlapSamples(); // we can do something cool here... mark down how many samples were decoded in this packet if (packet.GranuleCount.HasValue == false) { packet.GranuleCount = samplesDecoded; } // update our position UpdatePosition(samplesDecoded, packet); // a little statistical housekeeping... var sc = Utils.Sum(_sampleCountHistory) + samplesDecoded; _bitsPerPacketHistory.Enqueue((int)packet.BitsRead); _sampleCountHistory.Enqueue(samplesDecoded); while (sc > _sampleRate) { _bitsPerPacketHistory.Dequeue(); sc -= _sampleCountHistory.Dequeue(); } } catch { if (packet != null) { packet.Done(); } throw; } finally { _sw.Stop(); } }
private VorbisStreamDecoder.PacketDecodeInfo UnpackPacket(DataPacket packet) { if (packet.ReadBit()) return (VorbisStreamDecoder.PacketDecodeInfo) null; VorbisStreamDecoder.PacketDecodeInfo packetDecodeInfo = new VorbisStreamDecoder.PacketDecodeInfo(); int num1 = this._modeFieldBits; try { packetDecodeInfo.Mode = this.Modes[(int) packet.ReadBits(this._modeFieldBits)]; if (packetDecodeInfo.Mode.BlockFlag) { packetDecodeInfo.PrevFlag = packet.ReadBit(); packetDecodeInfo.NextFlag = packet.ReadBit(); num1 += 2; } } catch (EndOfStreamException ex) { return (VorbisStreamDecoder.PacketDecodeInfo) null; } try { long bitsRead1 = packet.BitsRead; packetDecodeInfo.FloorData = ACache.Get<VorbisFloor.PacketData>(this._channels); bool[] buffer1 = ACache.Get<bool>(this._channels); for (int index = 0; index < this._channels; ++index) { packetDecodeInfo.FloorData[index] = packetDecodeInfo.Mode.Mapping.ChannelSubmap[index].Floor.UnpackPacket(packet, packetDecodeInfo.Mode.BlockSize); buffer1[index] = !packetDecodeInfo.FloorData[index].ExecuteChannel; } foreach (VorbisMapping.CouplingStep couplingStep in packetDecodeInfo.Mode.Mapping.CouplingSteps) { if (packetDecodeInfo.FloorData[couplingStep.Angle].ExecuteChannel || packetDecodeInfo.FloorData[couplingStep.Magnitude].ExecuteChannel) { packetDecodeInfo.FloorData[couplingStep.Angle].ForceEnergy = true; packetDecodeInfo.FloorData[couplingStep.Magnitude].ForceEnergy = true; } } long num2 = packet.BitsRead - bitsRead1; long bitsRead2 = packet.BitsRead; packetDecodeInfo.Residue = ACache.Get<float>(this._channels, packetDecodeInfo.Mode.BlockSize); foreach (VorbisMapping.Submap submap in packetDecodeInfo.Mode.Mapping.Submaps) { for (int index = 0; index < this._channels; ++index) { if (packetDecodeInfo.Mode.Mapping.ChannelSubmap[index] != submap) packetDecodeInfo.FloorData[index].ForceNoEnergy = true; } float[][] buffer2 = submap.Residue.Decode(packet, buffer1, this._channels, packetDecodeInfo.Mode.BlockSize); for (int index1 = 0; index1 < this._channels; ++index1) { float[] numArray1 = packetDecodeInfo.Residue[index1]; float[] numArray2 = buffer2[index1]; for (int index2 = 0; index2 < packetDecodeInfo.Mode.BlockSize; ++index2) numArray1[index2] += numArray2[index2]; } ACache.Return<float>(ref buffer2); } ACache.Return<bool>(ref buffer1); ++this._glueBits; this._modeBits += (long) num1; this._floorBits += num2; this._resBits += packet.BitsRead - bitsRead2; this._wasteBits += (long) (8 * packet.Length) - packet.BitsRead; ++this._packetCount; } catch (EndOfStreamException ex) { this.ResetDecoder(); packetDecodeInfo = (VorbisStreamDecoder.PacketDecodeInfo) null; } catch (InvalidDataException ex) { packetDecodeInfo = (VorbisStreamDecoder.PacketDecodeInfo) null; } packet.Done(); return packetDecodeInfo; }
void ProcessParameterChange(DataPacket packet) { _parameterChangePacket = null; // try to do a stream header... var wasPeek = false; var doFullReset = false; if (ProcessStreamHeader(packet)) { packet.Done(); wasPeek = true; doFullReset = true; packet = _packetProvider.PeekNextPacket(); if (packet == null) throw new InvalidDataException("Couldn't get next packet!"); } // try to do a comment header... if (LoadComments(packet)) { if (wasPeek) { _packetProvider.GetNextPacket().Done(); } else { packet.Done(); } wasPeek = true; packet = _packetProvider.PeekNextPacket(); if (packet == null) throw new InvalidDataException("Couldn't get next packet!"); } // try to do a book header... if (LoadBooks(packet)) { if (wasPeek) { _packetProvider.GetNextPacket().Done(); } else { packet.Done(); } } ResetDecoder(doFullReset); }
private VorbisStreamDecoder.PacketDecodeInfo UnpackPacket(DataPacket packet) { if (packet.ReadBit()) { return((VorbisStreamDecoder.PacketDecodeInfo)null); } VorbisStreamDecoder.PacketDecodeInfo packetDecodeInfo = new VorbisStreamDecoder.PacketDecodeInfo(); int num1 = this._modeFieldBits; try { packetDecodeInfo.Mode = this.Modes[(int)packet.ReadBits(this._modeFieldBits)]; if (packetDecodeInfo.Mode.BlockFlag) { packetDecodeInfo.PrevFlag = packet.ReadBit(); packetDecodeInfo.NextFlag = packet.ReadBit(); num1 += 2; } } catch (EndOfStreamException ex) { return((VorbisStreamDecoder.PacketDecodeInfo)null); } try { long bitsRead1 = packet.BitsRead; packetDecodeInfo.FloorData = ACache.Get <VorbisFloor.PacketData>(this._channels); bool[] buffer1 = ACache.Get <bool>(this._channels); for (int index = 0; index < this._channels; ++index) { packetDecodeInfo.FloorData[index] = packetDecodeInfo.Mode.Mapping.ChannelSubmap[index].Floor.UnpackPacket(packet, packetDecodeInfo.Mode.BlockSize); buffer1[index] = !packetDecodeInfo.FloorData[index].ExecuteChannel; } foreach (VorbisMapping.CouplingStep couplingStep in packetDecodeInfo.Mode.Mapping.CouplingSteps) { if (packetDecodeInfo.FloorData[couplingStep.Angle].ExecuteChannel || packetDecodeInfo.FloorData[couplingStep.Magnitude].ExecuteChannel) { packetDecodeInfo.FloorData[couplingStep.Angle].ForceEnergy = true; packetDecodeInfo.FloorData[couplingStep.Magnitude].ForceEnergy = true; } } long num2 = packet.BitsRead - bitsRead1; long bitsRead2 = packet.BitsRead; packetDecodeInfo.Residue = ACache.Get <float>(this._channels, packetDecodeInfo.Mode.BlockSize); foreach (VorbisMapping.Submap submap in packetDecodeInfo.Mode.Mapping.Submaps) { for (int index = 0; index < this._channels; ++index) { if (packetDecodeInfo.Mode.Mapping.ChannelSubmap[index] != submap) { packetDecodeInfo.FloorData[index].ForceNoEnergy = true; } } float[][] buffer2 = submap.Residue.Decode(packet, buffer1, this._channels, packetDecodeInfo.Mode.BlockSize); for (int index1 = 0; index1 < this._channels; ++index1) { float[] numArray1 = packetDecodeInfo.Residue[index1]; float[] numArray2 = buffer2[index1]; for (int index2 = 0; index2 < packetDecodeInfo.Mode.BlockSize; ++index2) { numArray1[index2] += numArray2[index2]; } } ACache.Return <float>(ref buffer2); } ACache.Return <bool>(ref buffer1); ++this._glueBits; this._modeBits += (long)num1; this._floorBits += num2; this._resBits += packet.BitsRead - bitsRead2; this._wasteBits += (long)(8 * packet.Length) - packet.BitsRead; ++this._packetCount; } catch (EndOfStreamException ex) { this.ResetDecoder(); packetDecodeInfo = (VorbisStreamDecoder.PacketDecodeInfo)null; } catch (InvalidDataException ex) { packetDecodeInfo = (VorbisStreamDecoder.PacketDecodeInfo)null; } packet.Done(); return(packetDecodeInfo); }
public void DecodeNextPacket() { _sw.Start(); DataPacket dataPacket = null; try { IPacketProvider packetProvider = _packetProvider; if (packetProvider != null) { dataPacket = packetProvider.GetNextPacket(); } if (dataPacket == null) { _eosFound = true; } else { if (!_pagesSeen.Contains(_lastPageSeen = dataPacket.PageSequenceNumber)) { _pagesSeen.Add(_lastPageSeen); } if (dataPacket.IsResync) { ResetDecoder(isFullReset: false); } if (dataPacket == _parameterChangePacket) { _isParameterChange = true; ProcessParameterChange(dataPacket); } else if (!UnpackPacket(dataPacket)) { dataPacket.Done(); _wasteBits += 8 * dataPacket.Length; } else { dataPacket.Done(); DecodePacket(); int num = OverlapSamples(); if (!dataPacket.GranuleCount.HasValue) { dataPacket.GranuleCount = num; } UpdatePosition(num, dataPacket); int num2 = Utils.Sum(_sampleCountHistory) + num; _bitsPerPacketHistory.Enqueue((int)dataPacket.BitsRead); _sampleCountHistory.Enqueue(num); while (num2 > _sampleRate) { _bitsPerPacketHistory.Dequeue(); num2 -= _sampleCountHistory.Dequeue(); } } } } catch { dataPacket?.Done(); throw; } finally { _sw.Stop(); } }