コード例 #1
0
        private float[][] DecodeNextPacket(out int packetStartindex, out int packetValidLength, out int packetTotalLength, out bool isEndOfStream, out long?samplePosition, out int bitsRead, out int bitsRemaining, out int containerOverheadBits)
        {
            IPacket packet = null;

            try
            {
                if ((packet = _packetProvider.GetNextPacket()) == null)
                {
                    // no packet? we're at the end of the stream
                    isEndOfStream = true;
                }
                else
                {
                    // if the packet is flagged as the end of the stream, we can safely mark _eosFound
                    isEndOfStream = packet.IsEndOfStream;

                    // resync... that means we've probably lost some data; pick up a new position
                    if (packet.IsResync)
                    {
                        _hasPosition = false;
                    }

                    // grab the container overhead now, since the read won't affect it
                    containerOverheadBits = packet.ContainerOverheadBits;

                    // make sure the packet starts with a 0 bit as per the spec
                    if (packet.ReadBit())
                    {
                        bitsRemaining = packet.BitsRemaining + 1;
                    }
                    else
                    {
                        // if we get here, we should have a good packet; decode it and add it to the buffer
                        var mode = _modes[(int)packet.ReadBits(_modeFieldBits)];
                        if (_nextPacketBuf == null)
                        {
                            _nextPacketBuf = new float[_channels][];
                            for (var i = 0; i < _channels; i++)
                            {
                                _nextPacketBuf[i] = new float[_block1Size];
                            }
                        }
                        if (mode.Decode(packet, _nextPacketBuf, out packetStartindex, out packetValidLength, out packetTotalLength))
                        {
                            // per the spec, do not decode more samples than the last granulePosition
                            samplePosition = packet.GranulePosition;
                            bitsRead       = packet.BitsRead;
                            bitsRemaining  = packet.BitsRemaining;
                            return(_nextPacketBuf);
                        }
                        bitsRemaining = packet.BitsRead + packet.BitsRemaining;
                    }
                }
                packetStartindex      = 0;
                packetValidLength     = 0;
                packetTotalLength     = 0;
                samplePosition        = null;
                bitsRead              = 0;
                bitsRemaining         = 0;
                containerOverheadBits = 0;
                return(null);
            }
            finally
            {
                packet?.Done();
            }
        }