ReadBit() public method

Reads the next bit from the packet and advances the position counter.
public ReadBit ( ) : bool
return bool
Beispiel #1
0
            protected override void Init(DataPacket packet)
            {
                var submapCount = 1;
                if (packet.ReadBit()) submapCount += (int)packet.ReadBits(4);

                // square polar mapping
                var couplingSteps = 0;
                if (packet.ReadBit())
                {
                    couplingSteps = (int)packet.ReadBits(8) + 1;
                }

                var couplingBits = Utils.ilog(_vorbis._channels - 1);
                CouplingSteps = new CouplingStep[couplingSteps];
                for (int j = 0; j < couplingSteps; j++)
                {
                    var magnitude = (int)packet.ReadBits(couplingBits);
                    var angle = (int)packet.ReadBits(couplingBits);
                    if (magnitude == angle || magnitude > _vorbis._channels - 1 || angle > _vorbis._channels - 1)
                        throw new Exception();
                    CouplingSteps[j] = new CouplingStep { Angle = angle, Magnitude = magnitude };
                }

                // reserved bits
                if (packet.ReadBits(2) != 0UL) throw new Exception();

                // channel multiplex
                var mux = new int[_vorbis._channels];
                if (submapCount > 1)
                {
                    for (int c = 0; c < ChannelSubmap.Length; c++)
                    {
                        mux[c] = (int)packet.ReadBits(4);
                        if (mux[c] >= submapCount) throw new Exception();
                    }
                }

                // submaps
                Submaps = new Submap[submapCount];
                for (int j = 0; j < submapCount; j++)
                {
                    packet.ReadBits(8); // unused placeholder
                    var floorNum = (int)packet.ReadBits(8);
                    if (floorNum >= _vorbis.Floors.Length) throw new Exception();
                    var residueNum = (int)packet.ReadBits(8);
                    if (residueNum >= _vorbis.Residues.Length) throw new Exception();

                    Submaps[j] = new Submap
                    {
                        Floor = _vorbis.Floors[floorNum],
                        Residue = _vorbis.Residues[floorNum]
                    };
                }

                ChannelSubmap = new Submap[_vorbis._channels];
                for (int c = 0; c < ChannelSubmap.Length; c++)
                {
                    ChannelSubmap[c] = Submaps[mux[c]];
                }
            }
Beispiel #2
0
 protected override void Init(DataPacket packet)
 {
   int length1 = 1;
   if (packet.ReadBit())
     length1 += (int) packet.ReadBits(4);
   int length2 = 0;
   if (packet.ReadBit())
     length2 = (int) packet.ReadBits(8) + 1;
   int count = Utils.ilog(this._vorbis._channels - 1);
   this.CouplingSteps = new VorbisMapping.CouplingStep[length2];
   for (int index = 0; index < length2; ++index)
   {
     int num1 = (int) packet.ReadBits(count);
     int num2 = (int) packet.ReadBits(count);
     if (num1 == num2 || num1 > this._vorbis._channels - 1 || num2 > this._vorbis._channels - 1)
       throw new InvalidDataException();
     this.CouplingSteps[index] = new VorbisMapping.CouplingStep()
     {
       Angle = num2,
       Magnitude = num1
     };
   }
   if ((long) packet.ReadBits(2) != 0L)
     throw new InvalidDataException();
   int[] numArray = new int[this._vorbis._channels];
   if (length1 > 1)
   {
     for (int index = 0; index < this.ChannelSubmap.Length; ++index)
     {
       numArray[index] = (int) packet.ReadBits(4);
       if (numArray[index] >= length1)
         throw new InvalidDataException();
     }
   }
   this.Submaps = new VorbisMapping.Submap[length1];
   for (int index1 = 0; index1 < length1; ++index1)
   {
     long num = (long) packet.ReadBits(8);
     int index2 = (int) packet.ReadBits(8);
     if (index2 >= this._vorbis.Floors.Length)
       throw new InvalidDataException();
     if ((int) packet.ReadBits(8) >= this._vorbis.Residues.Length)
       throw new InvalidDataException();
     this.Submaps[index1] = new VorbisMapping.Submap()
     {
       Floor = this._vorbis.Floors[index2],
       Residue = this._vorbis.Residues[index2]
     };
   }
   this.ChannelSubmap = new VorbisMapping.Submap[this._vorbis._channels];
   for (int index = 0; index < this.ChannelSubmap.Length; ++index)
     this.ChannelSubmap[index] = this.Submaps[numArray[index]];
 }
Beispiel #3
0
        internal static VorbisMode Init(VorbisStreamDecoder vorbis, DataPacket packet)
        {
            var mode = new VorbisMode(vorbis);
            mode.BlockFlag = packet.ReadBit();
            mode.WindowType = (int)packet.ReadBits(16);
            mode.TransformType = (int)packet.ReadBits(16);
            var mapping = (int)packet.ReadBits(8);

            if (mode.WindowType != 0 || mode.TransformType != 0 || mapping >= vorbis.Maps.Length) throw new InvalidDataException();

            mode.Mapping = vorbis.Maps[mapping];
            mode.BlockSize = mode.BlockFlag ? vorbis.Block1Size : vorbis.Block0Size;

            // now pre-calc the window(s)...
            if (mode.BlockFlag)
            {
                // long block
                mode._windows = new float[4][];
                mode._windows[0] = new float[vorbis.Block1Size];
                mode._windows[1] = new float[vorbis.Block1Size];
                mode._windows[2] = new float[vorbis.Block1Size];
                mode._windows[3] = new float[vorbis.Block1Size];
            }
            else
            {
                // short block
                mode._windows = new float[1][];
                mode._windows[0] = new float[vorbis.Block0Size];
            }
            mode.CalcWindows();

            return mode;
        }
Beispiel #4
0
        internal static VorbisMode Init(VorbisStreamDecoder vorbis, DataPacket packet)
        {
            VorbisMode vorbisMode = new VorbisMode(vorbis);

            vorbisMode.BlockFlag     = packet.ReadBit();
            vorbisMode.WindowType    = (int)packet.ReadBits(16);
            vorbisMode.TransformType = (int)packet.ReadBits(16);
            int num = (int)packet.ReadBits(8);

            if (vorbisMode.WindowType != 0 || vorbisMode.TransformType != 0 || num >= vorbis.Maps.Length)
            {
                throw new InvalidDataException();
            }
            vorbisMode.Mapping   = vorbis.Maps[num];
            vorbisMode.BlockSize = (vorbisMode.BlockFlag ? vorbis.Block1Size : vorbis.Block0Size);
            if (vorbisMode.BlockFlag)
            {
                vorbisMode._windows    = new float[4][];
                vorbisMode._windows[0] = new float[vorbis.Block1Size];
                vorbisMode._windows[1] = new float[vorbis.Block1Size];
                vorbisMode._windows[2] = new float[vorbis.Block1Size];
                vorbisMode._windows[3] = new float[vorbis.Block1Size];
            }
            else
            {
                vorbisMode._windows    = new float[1][];
                vorbisMode._windows[0] = new float[vorbis.Block0Size];
            }
            vorbisMode.CalcWindows();
            return(vorbisMode);
        }
        internal int GetPacketLength(DataPacket curPacket, DataPacket lastPacket)
        {
            if (lastPacket == null || curPacket.IsResync)
            {
                return(0);
            }
            if (curPacket.ReadBit())
            {
                return(0);
            }
            if (lastPacket.ReadBit())
            {
                return(0);
            }
            int num = (int)curPacket.ReadBits(_modeFieldBits);

            if (num < 0 || num >= Modes.Length)
            {
                return(0);
            }
            VorbisMode vorbisMode = Modes[num];

            num = (int)lastPacket.ReadBits(_modeFieldBits);
            if (num < 0 || num >= Modes.Length)
            {
                return(0);
            }
            VorbisMode vorbisMode2 = Modes[num];

            return(vorbisMode.BlockSize / 4 + vorbisMode2.BlockSize / 4);
        }
Beispiel #6
0
 internal static VorbisMode Init(VorbisStreamDecoder vorbis, DataPacket packet)
 {
   VorbisMode vorbisMode = new VorbisMode(vorbis);
   vorbisMode.BlockFlag = packet.ReadBit();
   vorbisMode.WindowType = (int) packet.ReadBits(16);
   vorbisMode.TransformType = (int) packet.ReadBits(16);
   int index = (int) packet.ReadBits(8);
   if (vorbisMode.WindowType != 0 || vorbisMode.TransformType != 0 || index >= vorbis.Maps.Length)
     throw new InvalidDataException();
   vorbisMode.Mapping = vorbis.Maps[index];
   vorbisMode.BlockSize = vorbisMode.BlockFlag ? vorbis.Block1Size : vorbis.Block0Size;
   if (vorbisMode.BlockFlag)
   {
     vorbisMode._windows = new float[4][];
     vorbisMode._windows[0] = new float[vorbis.Block1Size];
     vorbisMode._windows[1] = new float[vorbis.Block1Size];
     vorbisMode._windows[2] = new float[vorbis.Block1Size];
     vorbisMode._windows[3] = new float[vorbis.Block1Size];
   }
   else
   {
     vorbisMode._windows = new float[1][];
     vorbisMode._windows[0] = new float[vorbis.Block0Size];
   }
   vorbisMode.CalcWindows();
   return vorbisMode;
 }
Beispiel #7
0
            internal override PacketData UnpackPacket(DataPacket packet, int blockSize, int channel)
            {
                var data = _reusablePacketData[channel];

                data.BlockSize     = blockSize;
                data.ForceEnergy   = false;
                data.ForceNoEnergy = false;
                data.PostCount     = 0;
                Array.Clear(data.Posts, 0, 64);

                // hoist ReadPosts to here since that's all we're doing...
                if (packet.ReadBit())
                {
                    var postCount = 2;
                    data.Posts[0] = (int)packet.ReadBits(_yBits);
                    data.Posts[1] = (int)packet.ReadBits(_yBits);

                    for (int i = 0; i < _partitionClass.Length; i++)
                    {
                        var clsNum = _partitionClass[i];
                        var cdim   = _classDimensions[clsNum];
                        var cbits  = _classSubclasses[clsNum];
                        var csub   = (1 << cbits) - 1;
                        var cval   = 0U;
                        if (cbits > 0)
                        {
                            if ((cval = (uint)_classMasterbooks[clsNum].DecodeScalar(packet)) == uint.MaxValue)
                            {
                                // we read a bad value...  bail
                                postCount = 0;
                                break;
                            }
                        }
                        for (int j = 0; j < cdim; j++)
                        {
                            var book = _subclassBooks[clsNum][cval & csub];
                            cval >>= cbits;
                            if (book != null)
                            {
                                if ((data.Posts[postCount] = book.DecodeScalar(packet)) == -1)
                                {
                                    // we read a bad value... bail
                                    postCount = 0;
                                    i         = _partitionClass.Length;
                                    break;
                                }
                            }
                            ++postCount;
                        }
                    }

                    data.PostCount = postCount;
                }

                return(data);
            }
Beispiel #8
0
 protected override void Init(DataPacket packet)
 {
   this._begin = (int) packet.ReadBits(24);
   this._end = (int) packet.ReadBits(24);
   this._partitionSize = (int) packet.ReadBits(24) + 1;
   this._classifications = (int) packet.ReadBits(6) + 1;
   this._classBookNum = (int) packet.ReadBits(8);
   this._classBook = this._vorbis.Books[this._classBookNum];
   this._cascade = new int[this._classifications];
   int length = 0;
   int val2 = 0;
   for (int index = 0; index < this._classifications; ++index)
   {
     int num1 = 0;
     int num2 = (int) packet.ReadBits(3);
     if (packet.ReadBit())
       num1 = (int) packet.ReadBits(5);
     this._cascade[index] = num1 << 3 | num2;
     length += VorbisResidue.Residue0.icount(this._cascade[index]);
     val2 = Math.Max(Utils.ilog(this._cascade[index]), val2);
   }
   this._maxPasses = val2;
   int[] numArray = new int[length];
   for (int index = 0; index < length; ++index)
     numArray[index] = (int) packet.ReadBits(8);
   int num3 = 0;
   this._books = new VorbisCodebook[this._classifications][];
   this._bookNums = new int[this._classifications][];
   for (int index1 = 0; index1 < this._classifications; ++index1)
   {
     this._books[index1] = new VorbisCodebook[8];
     this._bookNums[index1] = new int[8];
     int num1 = 1;
     int index2 = 0;
     while (num1 < 256)
     {
       if ((this._cascade[index1] & num1) == num1)
       {
         int index3 = numArray[num3++];
         this._books[index1][index2] = this._vorbis.Books[index3];
         this._bookNums[index1][index2] = index3;
         if (this._books[index1][index2].MapType == 0)
           throw new InvalidDataException();
       }
       num1 <<= 1;
       ++index2;
     }
   }
   this._classWordsPerCodeWord = this._classBook.Dimensions;
   this._nToRead = this._end - this._begin;
   this._partsToRead = this._nToRead / this._partitionSize;
   this._partWords = (this._partsToRead + this._classWordsPerCodeWord - 1) / this._classWordsPerCodeWord;
 }
Beispiel #9
0
 internal override VorbisFloor.PacketData UnpackPacket(DataPacket packet, int blockSize)
 {
     VorbisFloor.Floor1.PacketData1 packetData1_1 = new VorbisFloor.Floor1.PacketData1();
     packetData1_1.BlockSize = blockSize;
     VorbisFloor.Floor1.PacketData1 packetData1_2 = packetData1_1;
     if (packet.ReadBit())
     {
         try
         {
             int   index1   = 2;
             int[] numArray = ACache.Get <int>(64);
             numArray[0] = (int)packet.ReadBits(this._yBits);
             numArray[1] = (int)packet.ReadBits(this._yBits);
             for (int index2 = 0; index2 < this._partitionClass.Length; ++index2)
             {
                 int  index3 = this._partitionClass[index2];
                 int  num1   = this._classDimensions[index3];
                 int  num2   = this._classSubclasses[index3];
                 int  num3   = (1 << num2) - 1;
                 uint num4   = 0U;
                 if (num2 > 0)
                 {
                     num4 = (uint)this._classMasterbooks[index3].DecodeScalar(packet);
                 }
                 for (int index4 = 0; index4 < num1; ++index4)
                 {
                     VorbisCodebook vorbisCodebook = this._subclassBooks[index3][(long)num4 & (long)num3];
                     num4 >>= num2;
                     if (vorbisCodebook != null)
                     {
                         numArray[index1] = vorbisCodebook.DecodeScalar(packet);
                     }
                     ++index1;
                 }
             }
             packetData1_2.Posts     = numArray;
             packetData1_2.PostCount = index1;
         }
         catch (EndOfStreamException ex)
         {
         }
     }
     return((VorbisFloor.PacketData)packetData1_2);
 }
Beispiel #10
0
            internal override PacketData UnpackPacket(DataPacket packet, int blockSize, int channel)
            {
                PacketData1 packetData = _reusablePacketData[channel];

                packetData.BlockSize     = blockSize;
                packetData.ForceEnergy   = false;
                packetData.ForceNoEnergy = false;
                packetData.PostCount     = 0;
                Array.Clear(packetData.Posts, 0, 64);
                if (packet.ReadBit())
                {
                    int num = 2;
                    packetData.Posts[0] = (int)packet.ReadBits(_yBits);
                    packetData.Posts[1] = (int)packet.ReadBits(_yBits);
                    for (int i = 0; i < _partitionClass.Length; i++)
                    {
                        int  num2 = _partitionClass[i];
                        int  num3 = _classDimensions[num2];
                        int  num4 = _classSubclasses[num2];
                        int  num5 = (1 << num4) - 1;
                        uint num6 = 0u;
                        if (num4 > 0 && (num6 = (uint)_classMasterbooks[num2].DecodeScalar(packet)) == uint.MaxValue)
                        {
                            num = 0;
                            break;
                        }
                        for (int j = 0; j < num3; j++)
                        {
                            VorbisCodebook vorbisCodebook = _subclassBooks[num2][num6 & num5];
                            num6 >>= num4;
                            if (vorbisCodebook != null && (packetData.Posts[num] = vorbisCodebook.DecodeScalar(packet)) == -1)
                            {
                                num = 0;
                                i   = _partitionClass.Length;
                                break;
                            }
                            num++;
                        }
                    }
                    packetData.PostCount = num;
                }
                return(packetData);
            }
        internal int GetPacketLength(DataPacket curPacket, DataPacket lastPacket)
        {
            // if we don't have a previous packet, or we're re-syncing, this packet has no audio data to return
            if (lastPacket == null || curPacket.IsResync)
            {
                return(0);
            }

            // make sure they are audio packets
            if (curPacket.ReadBit())
            {
                return(0);
            }
            if (lastPacket.ReadBit())
            {
                return(0);
            }

            // get the current packet's information
            int modeIndex = (int)curPacket.ReadUBits(_modeFieldBits);

            if (modeIndex < 0 || modeIndex >= Modes.Length)
            {
                return(0);
            }
            var mode = Modes[modeIndex];

            // get the last packet's information
            modeIndex = (int)lastPacket.ReadUBits(_modeFieldBits);
            if (modeIndex < 0 || modeIndex >= Modes.Length)
            {
                return(0);
            }

            // now calculate the totals...
            var prevMode = Modes[modeIndex];

            return(mode.BlockSize / 4 + prevMode.BlockSize / 4);
        }
Beispiel #12
0
        internal int GetPacketLength(DataPacket curPacket, DataPacket lastPacket)
        {
            if (lastPacket == null || curPacket.IsResync || (curPacket.ReadBit() || lastPacket.ReadBit()))
            {
                return(0);
            }
            int index1 = (int)curPacket.ReadBits(this._modeFieldBits);

            if (index1 < 0 || index1 >= this.Modes.Length)
            {
                return(0);
            }
            VorbisMode vorbisMode1 = this.Modes[index1];
            int        index2      = (int)lastPacket.ReadBits(this._modeFieldBits);

            if (index2 < 0 || index2 >= this.Modes.Length)
            {
                return(0);
            }
            VorbisMode vorbisMode2 = this.Modes[index2];

            return(vorbisMode1.BlockSize / 4 + vorbisMode2.BlockSize / 4);
        }
Beispiel #13
0
        internal static VorbisMode Init(VorbisStreamDecoder vorbis, DataPacket packet)
        {
            var mode = new VorbisMode(vorbis);

            mode.BlockFlag     = packet.ReadBit();
            mode.WindowType    = (int)packet.ReadBits(16);
            mode.TransformType = (int)packet.ReadBits(16);
            var mapping = (int)packet.ReadBits(8);

            if (mode.WindowType != 0 || mode.TransformType != 0 || mapping >= vorbis.Maps.Length)
            {
                throw new Exception();
            }

            mode.Mapping   = vorbis.Maps[mapping];
            mode.BlockSize = mode.BlockFlag ? vorbis.Block1Size : vorbis.Block0Size;

            // now pre-calc the window(s)...
            if (mode.BlockFlag)
            {
                // long block
                mode._windows    = new float[4][];
                mode._windows[0] = new float[vorbis.Block1Size];
                mode._windows[1] = new float[vorbis.Block1Size];
                mode._windows[2] = new float[vorbis.Block1Size];
                mode._windows[3] = new float[vorbis.Block1Size];
            }
            else
            {
                // short block
                mode._windows    = new float[1][];
                mode._windows[0] = new float[vorbis.Block0Size];
            }
            mode.CalcWindows();

            return(mode);
        }
Beispiel #14
0
            protected override void Init(DataPacket packet)
            {
                // this is pretty well stolen directly from libvorbis...  BSD license
                _begin           = (int)packet.ReadBits(24);
                _end             = (int)packet.ReadBits(24);
                _partitionSize   = (int)packet.ReadBits(24) + 1;
                _classifications = (int)packet.ReadBits(6) + 1;
                _classBook       = _vorbis.Books[(int)packet.ReadBits(8)];

                _cascade = new int[_classifications];
                var acc = 0;

                for (int i = 0; i < _classifications; i++)
                {
                    var low_bits = (int)packet.ReadBits(3);
                    if (packet.ReadBit())
                    {
                        _cascade[i] = (int)packet.ReadBits(5) << 3 | low_bits;
                    }
                    else
                    {
                        _cascade[i] = low_bits;
                    }
                    acc += icount(_cascade[i]);
                }

                var bookNums = new int[acc];

                for (var i = 0; i < acc; i++)
                {
                    bookNums[i] = (int)packet.ReadBits(8);
                    if (_vorbis.Books[bookNums[i]].MapType == 0)
                    {
                        throw new InvalidDataException();
                    }
                }

                var entries  = _classBook.Entries;
                var dim      = _classBook.Dimensions;
                var partvals = 1;

                while (dim > 0)
                {
                    partvals *= _classifications;
                    if (partvals > entries)
                    {
                        throw new InvalidDataException();
                    }
                    --dim;
                }

                // now the lookups
                dim = _classBook.Dimensions;

                _books = new VorbisCodebook[_classifications][];

                acc = 0;
                var maxstage = 0;
                int stages;

                for (int j = 0; j < _classifications; j++)
                {
                    stages    = Utils.ilog(_cascade[j]);
                    _books[j] = new VorbisCodebook[stages];
                    if (stages > 0)
                    {
                        maxstage = Math.Max(maxstage, stages);
                        for (int k = 0; k < stages; k++)
                        {
                            if ((_cascade[j] & (1 << k)) > 0)
                            {
                                _books[j][k] = _vorbis.Books[bookNums[acc++]];
                            }
                        }
                    }
                }
                _maxStages = maxstage;

                _decodeMap = new int[partvals][];
                for (int j = 0; j < partvals; j++)
                {
                    var val  = j;
                    var mult = partvals / _classifications;
                    _decodeMap[j] = new int[_classBook.Dimensions];
                    for (int k = 0; k < _classBook.Dimensions; k++)
                    {
                        var deco = val / mult;
                        val             -= deco * mult;
                        mult            /= _classifications;
                        _decodeMap[j][k] = deco;
                    }
                }

                _entryCache = new int[_partitionSize];

                _partWordCache = new int[_vorbis._channels][][];
                var maxPartWords = ((_end - _begin) / _partitionSize + _classBook.Dimensions - 1) / _classBook.Dimensions;

                for (int ch = 0; ch < _vorbis._channels; ch++)
                {
                    _partWordCache[ch] = new int[maxPartWords][];
                }
            }
Beispiel #15
0
            public override void Init(DataPacket packet)
            {
                int num = 1;

                if (packet.ReadBit())
                {
                    num += (int)packet.ReadBits(4);
                }
                int num2 = 0;

                if (packet.ReadBit())
                {
                    num2 = (int)packet.ReadBits(8) + 1;
                }
                int count = Utils.ilog(_vorbis._channels - 1);

                CouplingSteps = new CouplingStep[num2];
                for (int i = 0; i < num2; i++)
                {
                    int num3 = (int)packet.ReadBits(count);
                    int num4 = (int)packet.ReadBits(count);
                    if (num3 == num4 || num3 > _vorbis._channels - 1 || num4 > _vorbis._channels - 1)
                    {
                        throw new InvalidDataException();
                    }
                    CouplingSteps[i] = new CouplingStep
                    {
                        Angle     = num4,
                        Magnitude = num3
                    };
                }
                if (packet.ReadBits(2) != 0L)
                {
                    throw new InvalidDataException();
                }
                int[] array = new int[_vorbis._channels];
                if (num > 1)
                {
                    for (int j = 0; j < ChannelSubmap.Length; j++)
                    {
                        array[j] = (int)packet.ReadBits(4);
                        if (array[j] >= num)
                        {
                            throw new InvalidDataException();
                        }
                    }
                }
                Submaps = new Submap[num];
                for (int k = 0; k < num; k++)
                {
                    packet.ReadBits(8);
                    int num5 = (int)packet.ReadBits(8);
                    if (num5 >= _vorbis.Floors.Length)
                    {
                        throw new InvalidDataException();
                    }
                    if ((int)packet.ReadBits(8) >= _vorbis.Residues.Length)
                    {
                        throw new InvalidDataException();
                    }
                    Submaps[k] = new Submap
                    {
                        Floor   = _vorbis.Floors[num5],
                        Residue = _vorbis.Residues[num5]
                    };
                }
                ChannelSubmap = new Submap[_vorbis._channels];
                for (int l = 0; l < ChannelSubmap.Length; l++)
                {
                    ChannelSubmap[l] = Submaps[array[l]];
                }
            }
Beispiel #16
0
            internal override PacketData UnpackPacket(DataPacket packet, int blockSize)
            {
                var data = new PacketData1 { BlockSize = blockSize };

                // hoist ReadPosts to here since that's all we're doing...
                if (packet.ReadBit())
                {
                    try
                    {
                        var postCount = 2;
                        var posts = ACache.Get<int>(64);
                        posts[0] = (int)packet.ReadBits(_yBits);
                        posts[1] = (int)packet.ReadBits(_yBits);

                        for (int i = 0; i < _partitionClass.Length; i++)
                        {
                            var clsNum = _partitionClass[i];
                            var cdim = _classDimensions[clsNum];
                            var cbits = _classSubclasses[clsNum];
                            var csub = (1 << cbits) - 1;
                            var cval = 0U;
                            if (cbits > 0)
                            {
                                cval = (uint)_classMasterbooks[clsNum].DecodeScalar(packet);
                            }
                            for (int j = 0; j < cdim; j++)
                            {
                                var book = _subclassBooks[clsNum][cval & csub];
                                cval >>= cbits;
                                if (book != null)
                                {
                                    posts[postCount] = (int)book.DecodeScalar(packet);
                                }
                                ++postCount;
                            }
                        }

                        data.Posts = posts;
                        data.PostCount = postCount;
                    }
                    catch (EndOfStreamException)
                    {
                    }
                }

                return data;
            }
        void InitTree(DataPacket packet)
        {
            bool sparse;
            int  total = 0;

            if (packet.ReadBit())
            {
                // ordered
                var len = (int)packet.ReadUBits(5) + 1;
                for (var i = 0; i < Entries;)
                {
                    int cnt = (int)packet.ReadUBits(Utils.ILog(Entries - i));

                    while (--cnt >= 0)
                    {
                        Lengths[i++] = len;
                    }

                    len++;
                }
                total  = 0;
                sparse = false;
            }
            else
            {
                // unordered
                sparse = packet.ReadBit();
                for (var i = 0; i < Entries; i++)
                {
                    if (!sparse || packet.ReadBit())
                    {
                        Lengths[i] = (int)packet.ReadUBits(5) + 1;
                        total++;
                    }
                    else
                    {
                        // mark the entry as unused
                        Lengths[i] = -1;
                    }
                }
            }

            // figure out the maximum bit size; if all are unused, don't do anything else
            if ((MaxBits = Lengths.Max()) > -1)
            {
                Span <int> codewordLengths = stackalloc int[0];
                if (sparse && total >= Entries >> 2)
                {
                    codewordLengths = stackalloc int[Entries];
                    for (int i = 0; i < Entries; i++)
                    {
                        codewordLengths[i] = Lengths[i];
                    }

                    sparse = false;
                }

                // compute size of sorted tables
                int sortedEntries = sparse ? total : 0;

                Span <int> values    = stackalloc int[0];
                Span <int> codewords = stackalloc int[0];
                if (!sparse)
                {
                    codewords = stackalloc int[Entries];
                }
                else if (sortedEntries != 0)
                {
                    codewordLengths = stackalloc int[sortedEntries];
                    codewords       = stackalloc int[sortedEntries];
                    values          = stackalloc int[sortedEntries];
                }

                if (!ComputeCodewords(sparse, sortedEntries, codewords, codewordLengths, Lengths, Entries, values))
                {
                    throw new InvalidDataException();
                }

                Span <int> valueList = stackalloc int[codewords.Length];
                if (values.Length != 0)
                {
                    valueList = values;
                }
                else
                {
                    for (int i = 0; i < codewords.Length; i++)
                    {
                        valueList[i] = i;
                    }
                }

                Span <int> lengthList = codewordLengths.Length > 0 ? codewordLengths : Lengths.AsSpan();
                PrefixList = Huffman.BuildPrefixedLinkedList(
                    valueList, lengthList, codewords, out PrefixBitLength, out PrefixOverflowTree);
            }
        }
Beispiel #18
0
 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;
 }
Beispiel #19
0
        private void InitTree(DataPacket packet)
        {
            int  num1 = 0;
            bool flag;

            if (packet.ReadBit())
            {
                int num2 = (int)packet.ReadBits(5) + 1;
                int num3 = 0;
                while (num3 < this.Entries)
                {
                    int num4 = (int)packet.ReadBits(Utils.ilog(this.Entries - num3));
                    while (--num4 >= 0)
                    {
                        this.Lengths[num3++] = num2;
                    }
                    ++num2;
                }
                num1 = 0;
                flag = false;
            }
            else
            {
                flag = packet.ReadBit();
                for (int index = 0; index < this.Entries; ++index)
                {
                    if (!flag || packet.ReadBit())
                    {
                        this.Lengths[index] = (int)packet.ReadBits(5) + 1;
                        ++num1;
                    }
                    else
                    {
                        this.Lengths[index] = -1;
                    }
                }
            }
            this.MaxBits = Enumerable.Max((IEnumerable <int>) this.Lengths);
            int[] numArray1 = (int[])null;
            if (flag && num1 >= this.Entries >> 2)
            {
                numArray1 = new int[this.Entries];
                Array.Copy((Array)this.Lengths, (Array)numArray1, this.Entries);
                flag = false;
            }
            int length = !flag ? 0 : num1;

            int[] numArray2 = (int[])null;
            int[] codeList  = (int[])null;
            if (!flag)
            {
                codeList = new int[this.Entries];
            }
            else if (length != 0)
            {
                numArray1 = new int[length];
                codeList  = new int[length];
                numArray2 = new int[length];
            }
            VorbisCodebook vorbisCodebook = this;

            int[] numArray3 = this.Lengths;
            int   num5      = this.Entries;

            int[] numArray4     = numArray2;
            int   num6          = flag ? 1 : 0;
            int   sortedEntries = length;

            int[] codewords       = codeList;
            int[] codewordLengths = numArray1;
            int[] len             = numArray3;
            int   n = num5;

            int[] values = numArray4;
            if (!vorbisCodebook.ComputeCodewords(num6 != 0, sortedEntries, codewords, codewordLengths, len, n, values))
            {
                throw new InvalidDataException();
            }
            this.LTree = Huffman.BuildLinkedList <int>(numArray2 ?? Enumerable.ToArray <int>(Enumerable.Range(0, codeList.Length)), numArray1 ?? this.Lengths, codeList);
        }
Beispiel #20
0
        void InitTree(DataPacket packet)
        {
            bool sparse;
            int total = 0;

            if (packet.ReadBit())
            {
                // ordered
                var len = (int)packet.ReadBits(5) + 1;
                for (var i = 0; i < Entries; )
                {
                    var cnt = (int)packet.ReadBits(Utils.ilog(Entries - i));

                    while (--cnt >= 0)
                    {
                        Lengths[i++] = len;
                    }

                    ++len;
                }
                total = 0;
                sparse = false;
            }
            else
            {
                // unordered
                sparse = packet.ReadBit();
                for (var i = 0; i < Entries; i++)
                {
                    if (!sparse || packet.ReadBit())
                    {
                        Lengths[i] = (int)packet.ReadBits(5) + 1;
                        ++total;
                    }
                    else
                    {
                        Lengths[i] = -1;
                    }
                }
            }
            MaxBits = Lengths.Max();

            int sortedCount = 0;
            int[] codewordLengths = null;
            if (sparse && total >= Entries >> 2)
            {
                codewordLengths = new int[Entries];
                Array.Copy(Lengths, codewordLengths, Entries);

                sparse = false;
            }

            // compute size of sorted tables
            if (sparse)
            {
                sortedCount = total;
            }
            else
            {
                sortedCount = 0;
            }

            int sortedEntries = sortedCount;

            int[] values = null;
            int[] codewords = null;
            if (!sparse)
            {
                codewords = new int[Entries];
            }
            else if (sortedEntries != 0)
            {
                codewordLengths = new int[sortedEntries];
                codewords = new int[sortedEntries];
                values = new int[sortedEntries];
            }

            if (!ComputeCodewords(sparse, sortedEntries, codewords, codewordLengths, len: Lengths, n: Entries, values: values)) throw new InvalidDataException();

            LTree = Huffman.BuildLinkedList<int>(values ?? Enumerable.Range(0, codewords.Length).ToArray(), codewordLengths ?? Lengths, codewords);
        }
Beispiel #21
0
            protected override void Init(DataPacket packet)
            {
                // this is pretty well stolen directly from libvorbis...  BSD license
                _begin = (int)packet.ReadBits(24);
                _end = (int)packet.ReadBits(24);
                _partitionSize = (int)packet.ReadBits(24) + 1;
                _classifications = (int)packet.ReadBits(6) + 1;
                _classBook = _vorbis.Books[(int)packet.ReadBits(8)];

                _cascade = new int[_classifications];
                var acc = 0;
                for (int i = 0; i < _classifications; i++)
                {
                    var low_bits = (int)packet.ReadBits(3);
                    if (packet.ReadBit())
                    {
                        _cascade[i] = (int)packet.ReadBits(5) << 3 | low_bits;
                    }
                    else
                    {
                        _cascade[i] = low_bits;
                    }
                    acc += icount(_cascade[i]);
                }

                var bookNums = new int[acc];
                for (var i = 0; i < acc; i++)
                {
                    bookNums[i] = (int)packet.ReadBits(8);
                    if (_vorbis.Books[bookNums[i]].MapType == 0) throw new Exception();
                }

                var entries = _classBook.Entries;
                var dim = _classBook.Dimensions;
                var partvals = 1;
                while (dim > 0)
                {
                    partvals *= _classifications;
                    if (partvals > entries) throw new Exception();
                    --dim;
                }

                // now the lookups
                dim = _classBook.Dimensions;

                _books = new VorbisCodebook[_classifications][];

                acc = 0;
                var maxstage = 0;
                int stages;
                for (int j = 0; j < _classifications; j++)
                {
                    stages = Utils.ilog(_cascade[j]);
                    _books[j] = new VorbisCodebook[stages];
                    if (stages > 0)
                    {
                        maxstage = Math.Max(maxstage, stages);
                        for (int k = 0; k < stages; k++)
                        {
                            if ((_cascade[j] & (1 << k)) > 0)
                            {
                                _books[j][k] = _vorbis.Books[bookNums[acc++]];
                            }
                        }
                    }
                }
                _maxStages = maxstage;

                _decodeMap = new int[partvals][];
                for (int j = 0; j < partvals; j++)
                {
                    var val = j;
                    var mult = partvals / _classifications;
                    _decodeMap[j] = new int[_classBook.Dimensions];
                    for (int k = 0; k < _classBook.Dimensions; k++)
                    {
                        var deco = val / mult;
                        val -= deco * mult;
                        mult /= _classifications;
                        _decodeMap[j][k] = deco;
                    }
                }

                _entryCache = new int[_partitionSize];

                _partWordCache = new int[_vorbis._channels][][];
                var maxPartWords = ((_end - _begin) / _partitionSize + _classBook.Dimensions - 1) / _classBook.Dimensions;
                for (int ch = 0; ch < _vorbis._channels; ch++)
                {
                    _partWordCache[ch] = new int[maxPartWords][];
                }
            }
        public void InitLookupTable(DataPacket packet)
        {
            MapType = (int)packet.ReadBits(4);
            if (MapType == 0)
            {
                return;
            }
            float num   = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            float num2  = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            int   count = (int)packet.ReadBits(4) + 1;
            bool  flag  = packet.ReadBit();
            int   num3  = Entries * Dimensions;

            float[] array = new float[num3];
            if (MapType == 1)
            {
                num3 = lookup1_values();
            }
            uint[] array2 = new uint[num3];
            for (int i = 0; i < num3; i++)
            {
                array2[i] = (uint)packet.ReadBits(count);
            }
            if (MapType == 1)
            {
                for (int j = 0; j < Entries; j++)
                {
                    double num4 = 0.0;
                    int    num5 = 1;
                    for (int k = 0; k < Dimensions; k++)
                    {
                        int    num6 = j / num5 % num3;
                        double num7 = (double)((float)(double)array2[num6] * num2 + num) + num4;
                        array[j * Dimensions + k] = (float)num7;
                        if (flag)
                        {
                            num4 = num7;
                        }
                        num5 *= num3;
                    }
                }
            }
            else
            {
                for (int l = 0; l < Entries; l++)
                {
                    double num8 = 0.0;
                    int    num9 = l * Dimensions;
                    for (int m = 0; m < Dimensions; m++)
                    {
                        double num10 = (double)((float)(double)array2[num9] * num2 + num) + num8;
                        array[l * Dimensions + m] = (float)num10;
                        if (flag)
                        {
                            num8 = num10;
                        }
                        num9++;
                    }
                }
            }
            LookupTable = array;
        }
Beispiel #23
0
            public override void Init(DataPacket packet)
            {
                _begin           = (int)packet.ReadBits(24);
                _end             = (int)packet.ReadBits(24);
                _partitionSize   = (int)packet.ReadBits(24) + 1;
                _classifications = (int)packet.ReadBits(6) + 1;
                _classBook       = _vorbis.Books[(uint)packet.ReadBits(8)];
                _cascade         = new int[_classifications];
                int num = 0;

                for (int i = 0; i < _classifications; i++)
                {
                    int num2 = (int)packet.ReadBits(3);
                    if (packet.ReadBit())
                    {
                        _cascade[i] = ((int)((uint)packet.ReadBits(5) << 3) | num2);
                    }
                    else
                    {
                        _cascade[i] = num2;
                    }
                    num += icount(_cascade[i]);
                }
                int[] array = new int[num];
                for (int j = 0; j < num; j++)
                {
                    array[j] = (int)packet.ReadBits(8);
                    if (_vorbis.Books[array[j]].MapType == 0)
                    {
                        throw new InvalidDataException();
                    }
                }
                int entries = _classBook.Entries;
                int num3    = _classBook.Dimensions;
                int num4    = 1;

                while (num3 > 0)
                {
                    num4 *= _classifications;
                    if (num4 > entries)
                    {
                        throw new InvalidDataException();
                    }
                    num3--;
                }
                num3   = _classBook.Dimensions;
                _books = new VorbisCodebook[_classifications][];
                num    = 0;
                int num5 = 0;

                for (int k = 0; k < _classifications; k++)
                {
                    int num6 = Utils.ilog(_cascade[k]);
                    _books[k] = new VorbisCodebook[num6];
                    if (num6 <= 0)
                    {
                        continue;
                    }
                    num5 = Math.Max(num5, num6);
                    for (int l = 0; l < num6; l++)
                    {
                        if ((_cascade[k] & (1 << l)) > 0)
                        {
                            _books[k][l] = _vorbis.Books[array[num++]];
                        }
                    }
                }
                _maxStages = num5;
                _decodeMap = new int[num4][];
                for (int m = 0; m < num4; m++)
                {
                    int num7 = m;
                    int num8 = num4 / _classifications;
                    _decodeMap[m] = new int[_classBook.Dimensions];
                    for (int n = 0; n < _classBook.Dimensions; n++)
                    {
                        int num9 = num7 / num8;
                        num7            -= num9 * num8;
                        num8            /= _classifications;
                        _decodeMap[m][n] = num9;
                    }
                }
                _entryCache    = new int[_partitionSize];
                _partWordCache = new int[_vorbis._channels][][];
                int num10 = ((_end - _begin) / _partitionSize + _classBook.Dimensions - 1) / _classBook.Dimensions;

                for (int num11 = 0; num11 < _vorbis._channels; num11++)
                {
                    _partWordCache[num11] = new int[num10][];
                }
            }
Beispiel #24
0
            protected override void Init(DataPacket packet)
            {
                this._begin           = (int)packet.ReadBits(24);
                this._end             = (int)packet.ReadBits(24);
                this._partitionSize   = (int)packet.ReadBits(24) + 1;
                this._classifications = (int)packet.ReadBits(6) + 1;
                this._classBookNum    = (int)packet.ReadBits(8);
                this._classBook       = this._vorbis.Books[this._classBookNum];
                this._cascade         = new int[this._classifications];
                int length = 0;
                int val2   = 0;

                for (int index = 0; index < this._classifications; ++index)
                {
                    int num1 = 0;
                    int num2 = (int)packet.ReadBits(3);
                    if (packet.ReadBit())
                    {
                        num1 = (int)packet.ReadBits(5);
                    }
                    this._cascade[index] = num1 << 3 | num2;
                    length += VorbisResidue.Residue0.icount(this._cascade[index]);
                    val2    = Math.Max(Utils.ilog(this._cascade[index]), val2);
                }
                this._maxPasses = val2;
                int[] numArray = new int[length];
                for (int index = 0; index < length; ++index)
                {
                    numArray[index] = (int)packet.ReadBits(8);
                }
                int num3 = 0;

                this._books    = new VorbisCodebook[this._classifications][];
                this._bookNums = new int[this._classifications][];
                for (int index1 = 0; index1 < this._classifications; ++index1)
                {
                    this._books[index1]    = new VorbisCodebook[8];
                    this._bookNums[index1] = new int[8];
                    int num1   = 1;
                    int index2 = 0;
                    while (num1 < 256)
                    {
                        if ((this._cascade[index1] & num1) == num1)
                        {
                            int index3 = numArray[num3++];
                            this._books[index1][index2]    = this._vorbis.Books[index3];
                            this._bookNums[index1][index2] = index3;
                            if (this._books[index1][index2].MapType == 0)
                            {
                                throw new InvalidDataException();
                            }
                        }
                        num1 <<= 1;
                        ++index2;
                    }
                }
                this._classWordsPerCodeWord = this._classBook.Dimensions;
                this._nToRead     = this._end - this._begin;
                this._partsToRead = this._nToRead / this._partitionSize;
                this._partWords   = (this._partsToRead + this._classWordsPerCodeWord - 1) / this._classWordsPerCodeWord;
            }
Beispiel #25
0
        void InitTree(DataPacket packet)
        {
            bool sparse;
            int  total = 0;

            if (packet.ReadBit())
            {
                // ordered
                var len = (int)packet.ReadBits(5) + 1;
                for (var i = 0; i < Entries;)
                {
                    var cnt = (int)packet.ReadBits(Utils.ilog(Entries - i));

                    while (--cnt >= 0)
                    {
                        Lengths[i++] = len;
                    }

                    ++len;
                }
                total  = 0;
                sparse = false;
            }
            else
            {
                // unordered
                sparse = packet.ReadBit();
                for (var i = 0; i < Entries; i++)
                {
                    if (!sparse || packet.ReadBit())
                    {
                        Lengths[i] = (int)packet.ReadBits(5) + 1;
                        ++total;
                    }
                    else
                    {
                        Lengths[i] = -1;
                    }
                }
            }

            int max = ((Lengths.Length > 0) ? Lengths[0] : 0);

            foreach (int n in Lengths)
            {
                if (n > max)
                {
                    max = n;
                }
            }

            //MaxBits = Lengths.Max();
            MaxBits = max;

            int sortedCount = 0;

            int[] codewordLengths = null;
            if (sparse && total >= Entries >> 2)
            {
                codewordLengths = new int[Entries];
                Array.Copy(Lengths, codewordLengths, Entries);

                sparse = false;
            }

            // compute size of sorted tables
            if (sparse)
            {
                sortedCount = total;
            }
            else
            {
                sortedCount = 0;
            }

            int sortedEntries = sortedCount;

            int[] values    = null;
            int[] codewords = null;
            if (!sparse)
            {
                codewords = new int[Entries];
            }
            else if (sortedEntries != 0)
            {
                codewordLengths = new int[sortedEntries];
                codewords       = new int[sortedEntries];
                values          = new int[sortedEntries];
            }


            if (!ComputeCodewords(sparse, sortedEntries, codewords, codewordLengths, len: Lengths, n: Entries, values: values))
            {
                throw new InvalidDataException();
            }
            {
                List <int> ranges = new List <int>();
                for (int i = 0; i < codewords.Length; i++)
                {
                    ranges.Add(i);
                }
                PrefixList = Huffman.BuildPrefixedLinkedList(values ?? ranges.ToArray(), codewordLengths ?? Lengths, codewords, out PrefixBitLength, out PrefixOverflowTree);
            }
        }
Beispiel #26
0
        PacketDecodeInfo UnpackPacket(DataPacket packet)
        {
            // make sure we're on an audio packet
            if (packet.ReadBit())
            {
                // we really can't do anything... count the bits as waste
                return null;
            }

            var pdi = new PacketDecodeInfo();

            // get mode and prev/next flags
            var modeBits = _modeFieldBits;
            try
            {
                pdi.Mode = Modes[(int)packet.ReadBits(_modeFieldBits)];
                if (pdi.Mode.BlockFlag)
                {
                    pdi.PrevFlag = packet.ReadBit();
                    pdi.NextFlag = packet.ReadBit();
                    modeBits += 2;
                }
            }
            catch (EndOfStreamException)
            {
                return null;
            }

            try
            {
                var startBits = packet.BitsRead;

                // read the noise floor data (but don't decode yet)
                pdi.FloorData = ACache.Get<VorbisFloor.PacketData>(_channels);
                var noExecuteChannel = ACache.Get<bool>(_channels);
                for (int i = 0; i < _channels; i++)
                {
                    pdi.FloorData[i] = pdi.Mode.Mapping.ChannelSubmap[i].Floor.UnpackPacket(packet, pdi.Mode.BlockSize);
                    noExecuteChannel[i] = !pdi.FloorData[i].ExecuteChannel;
                }

                // make sure we handle no-energy channels correctly given the couplings...
                foreach (var step in pdi.Mode.Mapping.CouplingSteps)
                {
                    if (pdi.FloorData[step.Angle].ExecuteChannel || pdi.FloorData[step.Magnitude].ExecuteChannel)
                    {
                        pdi.FloorData[step.Angle].ForceEnergy = true;
                        pdi.FloorData[step.Magnitude].ForceEnergy = true;
                    }
                }

                var floorBits = packet.BitsRead - startBits;
                startBits = packet.BitsRead;

                pdi.Residue = ACache.Get<float>(_channels, pdi.Mode.BlockSize);
                foreach (var subMap in pdi.Mode.Mapping.Submaps)
                {
                    for (int j = 0; j < _channels; j++)
                    {
                        if (pdi.Mode.Mapping.ChannelSubmap[j] != subMap)
                        {
                            pdi.FloorData[j].ForceNoEnergy = true;
                        }
                    }

                    var rTemp = subMap.Residue.Decode(packet, noExecuteChannel, _channels, pdi.Mode.BlockSize);
                    for (int c = 0; c < _channels; c++)
                    {
                        var r = pdi.Residue[c];
                        var rt = rTemp[c];
                        for (int i = 0; i < pdi.Mode.BlockSize; i++)
                        {
                            r[i] += rt[i];
                        }
                    }
                    ACache.Return(ref rTemp);
                }
                ACache.Return(ref noExecuteChannel);

                _glueBits += 1;
                _modeBits += modeBits;
                _floorBits += floorBits;
                _resBits += packet.BitsRead - startBits;
                _wasteBits += 8 * packet.Length - packet.BitsRead;

                _packetCount += 1;
            }
            catch (EndOfStreamException)
            {
                ResetDecoder();
                pdi = null;
            }
            catch (InvalidDataException)
            {
                pdi = null;
            }

            return pdi;
        }
Beispiel #27
0
        void InitTree(DataPacket packet)
        {
            bool sparse;
            int total = 0;

            if (packet.ReadBit())
            {
                // ordered
                var len = (int)packet.ReadBits(5) + 1;
                for (var i = 0; i < Entries; )
                {
                    var cnt = (int)packet.ReadBits(Utils.ilog(Entries - i));

                    while (--cnt >= 0)
                    {
                        Lengths[i++] = len;
                    }

                    ++len;
                }
                total = 0;
                sparse = false;
            }
            else
            {
                // unordered
                sparse = packet.ReadBit();
                for (var i = 0; i < Entries; i++)
                {
                    if (!sparse || packet.ReadBit())
                    {
                        Lengths[i] = (int)packet.ReadBits(5) + 1;
                        ++total;
                    }
                    else
                    {
                        // mark the entry as unused
                        Lengths[i] = -1;
                    }
                }
            }
            // figure out the maximum bit size; if all are unused, don't do anything else
            int maxLength = Lengths[0];
            for (int i = 0; i < Lengths.Length; i++)
            {
                if (Lengths[i] > maxLength)
                {
                    maxLength = Lengths[i];
                }
            }

            if ((MaxBits = maxLength) > -1)
            {
                int sortedCount = 0;
                int[] codewordLengths = null;
                if (sparse && total >= Entries >> 2)
                {
                    codewordLengths = new int[Entries];
                    Array.Copy(Lengths, codewordLengths, Entries);

                    sparse = false;
                }

                // compute size of sorted tables
                if (sparse)
                {
                    sortedCount = total;
                }
                else
                {
                    sortedCount = 0;
                }

                int sortedEntries = sortedCount;

                int[] values = null;
                int[] codewords = null;
                if (!sparse)
                {
                    codewords = new int[Entries];
                }
                else if (sortedEntries != 0)
                {
                    codewordLengths = new int[sortedEntries];
                    codewords = new int[sortedEntries];
                    values = new int[sortedEntries];
                }

                if (!ComputeCodewords(sparse, sortedEntries, codewords, codewordLengths, len: Lengths, n: Entries, values: values))
                    throw new InvalidDataException();

                var list = new List<int>();
                for (int i = 0; i < codewords.Length; i++)
                {
                    list.Add(i);
                }

                PrefixList = Huffman.BuildPrefixedLinkedList(values ?? list.ToArray(), codewordLengths ?? Lengths, codewords, out PrefixBitLength, out PrefixOverflowTree);
            }
        }
Beispiel #28
0
 private void InitLookupTable(DataPacket packet)
 {
   this.MapType = (int) packet.ReadBits(4);
   if (this.MapType == 0)
     return;
   float num1 = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
   float num2 = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
   int count = (int) packet.ReadBits(4) + 1;
   bool flag = packet.ReadBit();
   int length = this.Entries * this.Dimensions;
   float[] numArray1 = new float[length];
   if (this.MapType == 1)
     length = this.lookup1_values();
   uint[] numArray2 = new uint[length];
   for (int index = 0; index < length; ++index)
     numArray2[index] = (uint) packet.ReadBits(count);
   if (this.MapType == 1)
   {
     for (int index1 = 0; index1 < this.Entries; ++index1)
     {
       double num3 = 0.0;
       int num4 = 1;
       for (int index2 = 0; index2 < this.Dimensions; ++index2)
       {
         int index3 = index1 / num4 % length;
         double num5 = (double) numArray2[index3] * (double) num2 + (double) num1 + num3;
         numArray1[index1 * this.Dimensions + index2] = (float) num5;
         if (flag)
           num3 = num5;
         num4 *= length;
       }
     }
   }
   else
   {
     for (int index1 = 0; index1 < this.Entries; ++index1)
     {
       double num3 = 0.0;
       int index2 = index1 * this.Dimensions;
       for (int index3 = 0; index3 < this.Dimensions; ++index3)
       {
         double num4 = (double) numArray2[index2] * (double) num2 + (double) num1 + num3;
         numArray1[index1 * this.Dimensions + index3] = (float) num4;
         if (flag)
           num3 = num4;
         ++index2;
       }
     }
   }
   this.LookupTable = numArray1;
 }
Beispiel #29
0
            internal override PacketData UnpackPacket(DataPacket packet, int blockSize, int channel)
            {
                var data = _reusablePacketData[channel];
                data.BlockSize = blockSize;
                data.ForceEnergy = false;
                data.ForceNoEnergy = false;
                data.PostCount = 0;
                Array.Clear(data.Posts, 0, 64);

                // hoist ReadPosts to here since that's all we're doing...
                if (packet.ReadBit())
                {
                    var postCount = 2;
                    data.Posts[0] = (int)packet.ReadBits(_yBits);
                    data.Posts[1] = (int)packet.ReadBits(_yBits);

                    for (int i = 0; i < _partitionClass.Length; i++)
                    {
                        var clsNum = _partitionClass[i];
                        var cdim = _classDimensions[clsNum];
                        var cbits = _classSubclasses[clsNum];
                        var csub = (1 << cbits) - 1;
                        var cval = 0U;
                        if (cbits > 0)
                        {
                            if ((cval = (uint)_classMasterbooks[clsNum].DecodeScalar(packet)) == uint.MaxValue)
                            {
                                // we read a bad value...  bail
                                postCount = 0;
                                break;
                            }
                        }
                        for (int j = 0; j < cdim; j++)
                        {
                            var book = _subclassBooks[clsNum][cval & csub];
                            cval >>= cbits;
                            if (book != null)
                            {
                                if ((data.Posts[postCount] = book.DecodeScalar(packet)) == -1)
                                {
                                    // we read a bad value... bail
                                    postCount = 0;
                                    i = _partitionClass.Length;
                                    break;
                                }
                            }
                            ++postCount;
                        }
                    }

                    data.PostCount = postCount;
                }

                return data;
            }
Beispiel #30
0
        private void InitLookupTable(DataPacket packet)
        {
            this.MapType = (int)packet.ReadBits(4);
            if (this.MapType == 0)
            {
                return;
            }
            float num1   = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            float num2   = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            int   count  = (int)packet.ReadBits(4) + 1;
            bool  flag   = packet.ReadBit();
            int   length = this.Entries * this.Dimensions;

            float[] numArray1 = new float[length];
            if (this.MapType == 1)
            {
                length = this.lookup1_values();
            }
            uint[] numArray2 = new uint[length];
            for (int index = 0; index < length; ++index)
            {
                numArray2[index] = (uint)packet.ReadBits(count);
            }
            if (this.MapType == 1)
            {
                for (int index1 = 0; index1 < this.Entries; ++index1)
                {
                    double num3 = 0.0;
                    int    num4 = 1;
                    for (int index2 = 0; index2 < this.Dimensions; ++index2)
                    {
                        int    index3 = index1 / num4 % length;
                        double num5   = (double)numArray2[index3] * (double)num2 + (double)num1 + num3;
                        numArray1[index1 * this.Dimensions + index2] = (float)num5;
                        if (flag)
                        {
                            num3 = num5;
                        }
                        num4 *= length;
                    }
                }
            }
            else
            {
                for (int index1 = 0; index1 < this.Entries; ++index1)
                {
                    double num3   = 0.0;
                    int    index2 = index1 * this.Dimensions;
                    for (int index3 = 0; index3 < this.Dimensions; ++index3)
                    {
                        double num4 = (double)numArray2[index2] * (double)num2 + (double)num1 + num3;
                        numArray1[index1 * this.Dimensions + index3] = (float)num4;
                        if (flag)
                        {
                            num3 = num4;
                        }
                        ++index2;
                    }
                }
            }
            this.LookupTable = numArray1;
        }
Beispiel #31
0
        void LoadBooks(DataPacket packet)
        {
            packet.SkipBits(8);
            if ( !packet.ReadBytes ( 6 ).SequenceEqual ( new byte [] { 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 } ) ) throw new ArgumentException ( "Corrupted book header!" );

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Init(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Init(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if ( !packet.ReadBit () ) throw new ArgumentException ();

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ilog(Modes.Length - 1);
        }
Beispiel #32
0
        void InitLookupTable(DataPacket packet)
        {
            MapType = (int)packet.ReadBits(4);
            if (MapType == 0) return;

            var minValue = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            var deltaValue = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            var valueBits = (int)packet.ReadBits(4) + 1;
            var sequence_p = packet.ReadBit();

            var lookupValueCount = Entries * Dimensions;
            var lookupTable = new float[lookupValueCount];
            if (MapType == 1)
            {
                lookupValueCount = lookup1_values();
            }

            var multiplicands = new uint[lookupValueCount];
            for (var i = 0; i < lookupValueCount; i++)
            {
                multiplicands[i] = (uint)packet.ReadBits(valueBits);
            }

            // now that we have the initial data read in, calculate the entry tree
            if (MapType == 1)
            {
                for (var idx = 0; idx < Entries; idx++)
                {
                    var last = 0.0;
                    var idxDiv = 1;
                    for (var i = 0; i < Dimensions; i++)
                    {
                        var moff = (idx / idxDiv) % lookupValueCount;
                        var value = (float)multiplicands[moff] * deltaValue + minValue + last;
                        lookupTable[idx * Dimensions + i] = (float)value;

                        if (sequence_p) last = value;

                        idxDiv *= lookupValueCount;
                    }
                }
            }
            else
            {
                for (var idx = 0; idx < Entries; idx++)
                {
                    var last = 0.0;
                    var moff = idx * Dimensions;
                    for (var i = 0; i < Dimensions; i++)
                    {
                        var value = multiplicands[moff] * deltaValue + minValue + last;
                        lookupTable[idx * Dimensions + i] = (float)value;

                        if (sequence_p) last = value;

                        ++moff;
                    }
                }
            }

            LookupTable = lookupTable;
        }
Beispiel #33
0
        private void LoadBooks(DataPacket packet)
        {
            packet.SkipBits(8);
            if (!Enumerable.SequenceEqual <byte>((IEnumerable <byte>)packet.ReadBytes(6), (IEnumerable <byte>) new byte[6]
            {
                (byte)118,
                (byte)111,
                (byte)114,
                (byte)98,
                (byte)105,
                (byte)115
            }))
            {
                throw new InvalidDataException("Corrupted book header!");
            }
            long bitsRead1 = packet.BitsRead;

            this._glueBits += packet.BitsRead;
            this.Books      = new VorbisCodebook[(int)packet.ReadByte() + 1];
            for (int number = 0; number < this.Books.Length; ++number)
            {
                this.Books[number] = VorbisCodebook.Init(this, packet, number);
            }
            this._bookBits += packet.BitsRead - bitsRead1;
            long bitsRead2 = packet.BitsRead;

            this.Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Times.Length; ++index)
            {
                this.Times[index] = VorbisTime.Init(this, packet);
            }
            this._timeHdrBits += packet.BitsRead - bitsRead2;
            long bitsRead3 = packet.BitsRead;

            this.Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Floors.Length; ++index)
            {
                this.Floors[index] = VorbisFloor.Init(this, packet);
            }
            this._floorHdrBits += packet.BitsRead - bitsRead3;
            long bitsRead4 = packet.BitsRead;

            this.Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Residues.Length; ++index)
            {
                this.Residues[index] = VorbisResidue.Init(this, packet);
            }
            this._resHdrBits += packet.BitsRead - bitsRead4;
            long bitsRead5 = packet.BitsRead;

            this.Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Maps.Length; ++index)
            {
                this.Maps[index] = VorbisMapping.Init(this, packet);
            }
            this._mapHdrBits += packet.BitsRead - bitsRead5;
            long bitsRead6 = packet.BitsRead;

            this.Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Modes.Length; ++index)
            {
                this.Modes[index] = VorbisMode.Init(this, packet);
            }
            this._modeHdrBits += packet.BitsRead - bitsRead6;
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }
            ++this._glueBits;
            this._wasteHdrBits += (long)(8 * packet.Length) - packet.BitsRead;
            this._modeFieldBits = Utils.ilog(this.Modes.Length - 1);
        }
        public void InitTree(DataPacket packet)
        {
            int  num = 0;
            bool flag;

            if (packet.ReadBit())
            {
                int num2 = (int)packet.ReadBits(5) + 1;
                int num3 = 0;
                while (num3 < Entries)
                {
                    int num4 = (int)packet.ReadBits(Utils.ilog(Entries - num3));
                    while (--num4 >= 0)
                    {
                        Lengths[num3++] = num2;
                    }
                    num2++;
                }
                num  = 0;
                flag = false;
            }
            else
            {
                flag = packet.ReadBit();
                for (int i = 0; i < Entries; i++)
                {
                    if (!flag || packet.ReadBit())
                    {
                        Lengths[i] = (int)packet.ReadBits(5) + 1;
                        num++;
                    }
                    else
                    {
                        Lengths[i] = -1;
                    }
                }
            }
            MaxBits = Lengths.Max();
            int num5 = 0;

            int[] array = null;
            if (flag && num >= Entries >> 2)
            {
                array = new int[Entries];
                Array.Copy(Lengths, array, Entries);
                flag = false;
            }
            num5 = (flag ? num : 0);
            int num6 = num5;

            int[] array2 = null;
            int[] array3 = null;
            if (!flag)
            {
                array3 = new int[Entries];
            }
            else if (num6 != 0)
            {
                array  = new int[num6];
                array3 = new int[num6];
                array2 = new int[num6];
            }
            if (!ComputeCodewords(flag, num6, array3, array, Lengths, Entries, array2))
            {
                throw new InvalidDataException();
            }
            PrefixList = Huffman.BuildPrefixedLinkedList(array2 ?? Enumerable.Range(0, array3.Length).ToArray(), array ?? Lengths, array3, out PrefixBitLength, out PrefixOverflowTree);
        }
        void InitLookupTable(DataPacket packet)
        {
            MapType = (int)packet.ReadUBits(4);
            if (MapType == 0)
            {
                return;
            }

            float minValue   = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            float deltaValue = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            int   valueBits  = (int)packet.ReadUBits(4) + 1;
            bool  sequence_p = packet.ReadBit();

            int lookupValueCount = Entries * Dimensions;

            LookupTable = new float[lookupValueCount];
            if (MapType == 1)
            {
                lookupValueCount = Lookup1_values();
            }

            Span <uint> multiplicands = stackalloc uint[lookupValueCount];

            for (var i = 0; i < lookupValueCount; i++)
            {
                multiplicands[i] = (uint)packet.ReadUBits(valueBits);
            }

            // now that we have the initial data read in, calculate the entry tree
            if (MapType == 1)
            {
                for (int idx = 0; idx < Entries; idx++)
                {
                    double last   = 0.0;
                    int    idxDiv = 1;
                    for (var i = 0; i < Dimensions; i++)
                    {
                        int    moff  = idx / idxDiv % lookupValueCount;
                        double value = multiplicands[moff] * deltaValue + minValue + last;
                        LookupTable[idx * Dimensions + i] = (float)value;

                        if (sequence_p)
                        {
                            last = value;
                        }

                        idxDiv *= lookupValueCount;
                    }
                }
            }
            else
            {
                for (int idx = 0; idx < Entries; idx++)
                {
                    double last = 0.0;
                    int    moff = idx * Dimensions;
                    for (var i = 0; i < Dimensions; i++)
                    {
                        double value = multiplicands[moff] * deltaValue + minValue + last;
                        LookupTable[idx * Dimensions + i] = (float)value;

                        if (sequence_p)
                        {
                            last = value;
                        }

                        moff++;
                    }
                }
            }
        }
Beispiel #36
0
        internal int GetPacketLength(DataPacket curPacket, DataPacket lastPacket)
        {
            // if we don't have a previous packet, or we're re-syncing, this packet has no audio data to return
            if (lastPacket == null || curPacket.IsResync) return 0;

            // make sure they are audio packets
            if (curPacket.ReadBit()) return 0;
            if (lastPacket.ReadBit()) return 0;

            // get the current packet's information
            var modeIdx = (int)curPacket.ReadBits(_modeFieldBits);
            if (modeIdx < 0 || modeIdx >= Modes.Length) return 0;
            var mode = Modes[modeIdx];

            // get the last packet's information
            modeIdx = (int)lastPacket.ReadBits(_modeFieldBits);
            if (modeIdx < 0 || modeIdx >= Modes.Length) return 0;
            var prevMode = Modes[modeIdx];

            // now calculate the totals...
            return mode.BlockSize / 4 + prevMode.BlockSize / 4;
        }
Beispiel #37
0
        void InitLookupTable(DataPacket packet)
        {
            MapType = (int)packet.ReadBits(4);
            if (MapType == 0)
            {
                return;
            }

            var minValue   = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            var deltaValue = Utils.ConvertFromVorbisFloat32(packet.ReadUInt32());
            var valueBits  = (int)packet.ReadBits(4) + 1;
            var sequence_p = packet.ReadBit();

            var lookupValueCount = Entries * Dimensions;
            var lookupTable      = new float[lookupValueCount];

            if (MapType == 1)
            {
                lookupValueCount = lookup1_values();
            }

            var multiplicands = new uint[lookupValueCount];

            for (var i = 0; i < lookupValueCount; i++)
            {
                multiplicands[i] = (uint)packet.ReadBits(valueBits);
            }

            // now that we have the initial data read in, calculate the entry tree
            if (MapType == 1)
            {
                for (var idx = 0; idx < Entries; idx++)
                {
                    var last   = 0.0;
                    var idxDiv = 1;
                    for (var i = 0; i < Dimensions; i++)
                    {
                        var moff  = (idx / idxDiv) % lookupValueCount;
                        var value = (float)multiplicands[moff] * deltaValue + minValue + last;
                        lookupTable[idx * Dimensions + i] = (float)value;

                        if (sequence_p)
                        {
                            last = value;
                        }

                        idxDiv *= lookupValueCount;
                    }
                }
            }
            else
            {
                for (var idx = 0; idx < Entries; idx++)
                {
                    var last = 0.0;
                    var moff = idx * Dimensions;
                    for (var i = 0; i < Dimensions; i++)
                    {
                        var value = multiplicands[moff] * deltaValue + minValue + last;
                        lookupTable[idx * Dimensions + i] = (float)value;

                        if (sequence_p)
                        {
                            last = value;
                        }

                        ++moff;
                    }
                }
            }

            LookupTable = lookupTable;
        }
Beispiel #38
0
        bool UnpackPacket(DataPacket packet)
        {
            // make sure we're on an audio packet
            if (packet.ReadBit())
            {
                // we really can't do anything... count the bits as waste
                return false;
            }

            // get mode and prev/next flags
            var modeBits = _modeFieldBits;
            _mode = Modes[(int)packet.ReadBits(_modeFieldBits)];
            if (_mode.BlockFlag)
            {
                _prevFlag = packet.ReadBit();
                _nextFlag = packet.ReadBit();
                modeBits += 2;
            }
            else
            {
                _prevFlag = _nextFlag = false;
            }

            if (packet.IsShort) return false;

            var startBits = packet.BitsRead;

            var halfBlockSize = _mode.BlockSize / 2;

            // read the noise floor data (but don't decode yet)
            for (int i = 0; i < _channels; i++)
            {
                _floorData[i] = _mode.Mapping.ChannelSubmap[i].Floor.UnpackPacket(packet, _mode.BlockSize, i);
                _noExecuteChannel[i] = !_floorData[i].ExecuteChannel;

                // go ahead and clear the residue buffers
                Array.Clear(_residue[i], 0, halfBlockSize);
            }

            // make sure we handle no-energy channels correctly given the couplings...
            foreach (var step in _mode.Mapping.CouplingSteps)
            {
                if (_floorData[step.Angle].ExecuteChannel || _floorData[step.Magnitude].ExecuteChannel)
                {
                    _floorData[step.Angle].ForceEnergy = true;
                    _floorData[step.Magnitude].ForceEnergy = true;
                }
            }

            var floorBits = packet.BitsRead - startBits;
            startBits = packet.BitsRead;

            foreach (var subMap in _mode.Mapping.Submaps)
            {
                for (int j = 0; j < _channels; j++)
                {
                    if (_mode.Mapping.ChannelSubmap[j] != subMap)
                    {
                        _floorData[j].ForceNoEnergy = true;
                    }
                }

                var rTemp = subMap.Residue.Decode(packet, _noExecuteChannel, _channels, _mode.BlockSize);
                for (int c = 0; c < _channels; c++)
                {
                    var r = _residue[c];
                    var rt = rTemp[c];
                    for (int i = 0; i < halfBlockSize; i++)
                    {
                        r[i] += rt[i];
                    }
                }
            }

            _glueBits += 1;
            _modeBits += modeBits;
            _floorBits += floorBits;
            _resBits += packet.BitsRead - startBits;
            _wasteBits += 8 * packet.Length - packet.BitsRead;

            _packetCount += 1;

            return true;
        }
Beispiel #39
0
        void InitTree(DataPacket packet)
        {
            bool sparse;
            int  total = 0;

            if (packet.ReadBit())
            {
                // ordered
                var len = (int)packet.ReadBits(5) + 1;
                for (var i = 0; i < Entries;)
                {
                    var cnt = (int)packet.ReadBits(Utils.ilog(Entries - i));

                    while (--cnt >= 0)
                    {
                        Lengths[i++] = len;
                    }

                    ++len;
                }
                total  = 0;
                sparse = false;
            }
            else
            {
                // unordered
                sparse = packet.ReadBit();
                for (var i = 0; i < Entries; i++)
                {
                    if (!sparse || packet.ReadBit())
                    {
                        Lengths[i] = (int)packet.ReadBits(5) + 1;
                        ++total;
                    }
                    else
                    {
                        // mark the entry as unused
                        Lengths[i] = -1;
                    }
                }
            }
            // figure out the maximum bit size; if all are unused, don't do anything else
            if ((MaxBits = Lengths.Max()) > -1)
            {
                int   sortedCount     = 0;
                int[] codewordLengths = null;
                if (sparse && total >= Entries >> 2)
                {
                    codewordLengths = new int[Entries];
                    Array.Copy(Lengths, codewordLengths, Entries);

                    sparse = false;
                }

                // compute size of sorted tables
                if (sparse)
                {
                    sortedCount = total;
                }
                else
                {
                    sortedCount = 0;
                }

                int sortedEntries = sortedCount;

                int[] values    = null;
                int[] codewords = null;
                if (!sparse)
                {
                    codewords = new int[Entries];
                }
                else if (sortedEntries != 0)
                {
                    codewordLengths = new int[sortedEntries];
                    codewords       = new int[sortedEntries];
                    values          = new int[sortedEntries];
                }

                if (!ComputeCodewords(sparse, sortedEntries, codewords, codewordLengths, len: Lengths, n: Entries, values: values))
                {
                    throw new InvalidDataException();
                }

                PrefixList = Huffman.BuildPrefixedLinkedList(values ?? Enumerable.Range(0, codewords.Length).ToArray(), codewordLengths ?? Lengths, codewords, out PrefixBitLength, out PrefixOverflowTree);
            }
        }
Beispiel #40
0
 private void LoadBooks(DataPacket packet)
 {
   packet.SkipBits(8);
   if (!Enumerable.SequenceEqual<byte>((IEnumerable<byte>) packet.ReadBytes(6), (IEnumerable<byte>) new byte[6]
   {
     (byte) 118,
     (byte) 111,
     (byte) 114,
     (byte) 98,
     (byte) 105,
     (byte) 115
   }))
     throw new InvalidDataException("Corrupted book header!");
   long bitsRead1 = packet.BitsRead;
   this._glueBits += packet.BitsRead;
   this.Books = new VorbisCodebook[(int) packet.ReadByte() + 1];
   for (int number = 0; number < this.Books.Length; ++number)
     this.Books[number] = VorbisCodebook.Init(this, packet, number);
   this._bookBits += packet.BitsRead - bitsRead1;
   long bitsRead2 = packet.BitsRead;
   this.Times = new VorbisTime[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Times.Length; ++index)
     this.Times[index] = VorbisTime.Init(this, packet);
   this._timeHdrBits += packet.BitsRead - bitsRead2;
   long bitsRead3 = packet.BitsRead;
   this.Floors = new VorbisFloor[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Floors.Length; ++index)
     this.Floors[index] = VorbisFloor.Init(this, packet);
   this._floorHdrBits += packet.BitsRead - bitsRead3;
   long bitsRead4 = packet.BitsRead;
   this.Residues = new VorbisResidue[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Residues.Length; ++index)
     this.Residues[index] = VorbisResidue.Init(this, packet);
   this._resHdrBits += packet.BitsRead - bitsRead4;
   long bitsRead5 = packet.BitsRead;
   this.Maps = new VorbisMapping[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Maps.Length; ++index)
     this.Maps[index] = VorbisMapping.Init(this, packet);
   this._mapHdrBits += packet.BitsRead - bitsRead5;
   long bitsRead6 = packet.BitsRead;
   this.Modes = new VorbisMode[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Modes.Length; ++index)
     this.Modes[index] = VorbisMode.Init(this, packet);
   this._modeHdrBits += packet.BitsRead - bitsRead6;
   if (!packet.ReadBit())
     throw new InvalidDataException();
   ++this._glueBits;
   this._wasteHdrBits += (long) (8 * packet.Length) - packet.BitsRead;
   this._modeFieldBits = Utils.ilog(this.Modes.Length - 1);
 }
Beispiel #41
0
        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);
        }
Beispiel #42
0
 internal int GetPacketLength(DataPacket curPacket, DataPacket lastPacket)
 {
   if (lastPacket == null || curPacket.IsResync || (curPacket.ReadBit() || lastPacket.ReadBit()))
     return 0;
   int index1 = (int) curPacket.ReadBits(this._modeFieldBits);
   if (index1 < 0 || index1 >= this.Modes.Length)
     return 0;
   VorbisMode vorbisMode1 = this.Modes[index1];
   int index2 = (int) lastPacket.ReadBits(this._modeFieldBits);
   if (index2 < 0 || index2 >= this.Modes.Length)
     return 0;
   VorbisMode vorbisMode2 = this.Modes[index2];
   return vorbisMode1.BlockSize / 4 + vorbisMode2.BlockSize / 4;
 }
        public bool UnpackPacket(DataPacket packet)
        {
            if (packet.ReadBit())
            {
                return(false);
            }
            int num = _modeFieldBits;

            _mode = Modes[(uint)packet.ReadBits(_modeFieldBits)];
            if (_mode.BlockFlag)
            {
                _prevFlag = packet.ReadBit();
                _nextFlag = packet.ReadBit();
                num      += 2;
            }
            else
            {
                _prevFlag = (_nextFlag = false);
            }
            if (packet.IsShort)
            {
                return(false);
            }
            long bitsRead = packet.BitsRead;
            int  num2     = _mode.BlockSize / 2;

            for (int i = 0; i < _channels; i++)
            {
                _floorData[i]        = _mode.Mapping.ChannelSubmap[i].Floor.UnpackPacket(packet, _mode.BlockSize, i);
                _noExecuteChannel[i] = !_floorData[i].ExecuteChannel;
                Array.Clear(_residue[i], 0, num2);
            }
            VorbisMapping.CouplingStep[] couplingSteps = _mode.Mapping.CouplingSteps;
            foreach (VorbisMapping.CouplingStep couplingStep in couplingSteps)
            {
                if (_floorData[couplingStep.Angle].ExecuteChannel || _floorData[couplingStep.Magnitude].ExecuteChannel)
                {
                    _floorData[couplingStep.Angle].ForceEnergy     = true;
                    _floorData[couplingStep.Magnitude].ForceEnergy = true;
                }
            }
            long num3 = packet.BitsRead - bitsRead;

            bitsRead = packet.BitsRead;
            VorbisMapping.Submap[] submaps = _mode.Mapping.Submaps;
            foreach (VorbisMapping.Submap submap in submaps)
            {
                for (int k = 0; k < _channels; k++)
                {
                    if (_mode.Mapping.ChannelSubmap[k] != submap)
                    {
                        _floorData[k].ForceNoEnergy = true;
                    }
                }
                float[][] array = submap.Residue.Decode(packet, _noExecuteChannel, _channels, _mode.BlockSize);
                for (int l = 0; l < _channels; l++)
                {
                    float[] array2 = _residue[l];
                    float[] array3 = array[l];
                    for (int m = 0; m < num2; m++)
                    {
                        array2[m] += array3[m];
                    }
                }
            }
            _glueBits++;
            _modeBits  += num;
            _floorBits += num3;
            _resBits   += packet.BitsRead - bitsRead;
            _wasteBits += 8 * packet.Length - packet.BitsRead;
            _packetCount++;
            return(true);
        }
Beispiel #44
0
            protected override void Init(DataPacket packet)
            {
                var submapCount = 1;

                if (packet.ReadBit())
                {
                    submapCount += (int)packet.ReadBits(4);
                }

                // square polar mapping
                var couplingSteps = 0;

                if (packet.ReadBit())
                {
                    couplingSteps = (int)packet.ReadBits(8) + 1;
                }

                var couplingBits = Utils.ilog(_vorbis._channels - 1);

                CouplingSteps = new CouplingStep[couplingSteps];
                for (int j = 0; j < couplingSteps; j++)
                {
                    var magnitude = (int)packet.ReadBits(couplingBits);
                    var angle     = (int)packet.ReadBits(couplingBits);
                    if (magnitude == angle || magnitude > _vorbis._channels - 1 || angle > _vorbis._channels - 1)
                    {
                        throw new InvalidDataException();
                    }
                    CouplingSteps[j] = new CouplingStep {
                        Angle = angle, Magnitude = magnitude
                    };
                }

                // reserved bits
                if (packet.ReadBits(2) != 0UL)
                {
                    throw new InvalidDataException();
                }

                // channel multiplex
                var mux = new int[_vorbis._channels];

                if (submapCount > 1)
                {
                    for (int c = 0; c < ChannelSubmap.Length; c++)
                    {
                        mux[c] = (int)packet.ReadBits(4);
                        if (mux[c] >= submapCount)
                        {
                            throw new InvalidDataException();
                        }
                    }
                }

                // submaps
                Submaps = new Submap[submapCount];
                for (int j = 0; j < submapCount; j++)
                {
                    packet.ReadBits(8); // unused placeholder
                    var floorNum = (int)packet.ReadBits(8);
                    if (floorNum >= _vorbis.Floors.Length)
                    {
                        throw new InvalidDataException();
                    }
                    var residueNum = (int)packet.ReadBits(8);
                    if (residueNum >= _vorbis.Residues.Length)
                    {
                        throw new InvalidDataException();
                    }

                    Submaps[j] = new Submap
                    {
                        Floor   = _vorbis.Floors[floorNum],
                        Residue = _vorbis.Residues[floorNum]
                    };
                }

                ChannelSubmap = new Submap[_vorbis._channels];
                for (int c = 0; c < ChannelSubmap.Length; c++)
                {
                    ChannelSubmap[c] = Submaps[mux[c]];
                }
            }
        bool LoadBooks(DataPacket packet)
        {
            bool equal = true;
            byte[] sequence = new byte[] { 0x05, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 };
            byte[] packets  = packet.ReadBytes(7);

            for (int i = 0; i < 7; i++)
            {
                if (packets[i] != sequence[i])
                {
                    equal = false;
                    break;
                }
            }

            if (!equal)
            {
                return false;
            }

            if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber))) _pagesSeen.Add(_lastPageSeen);

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Init(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Init(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if (!packet.ReadBit()) throw new InvalidDataException();

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ilog(Modes.Length - 1);

            return true;
        }
Beispiel #46
0
        void LoadBooks(DataPacket packet)
        {
            packet.SkipBits(8);
            if (!packet.ReadBytes(6).SequenceEqual(new byte[] { 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 }))
            {
                throw new InvalidDataException("Corrupted book header!");
            }

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits       = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits          = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Init(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits           = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Init(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ilog(Modes.Length - 1);
        }
Beispiel #47
0
            protected override void Init(DataPacket packet)
            {
                _begin = (int)packet.ReadBits(24);
                _end = (int)packet.ReadBits(24);
                _partitionSize = (int)packet.ReadBits(24) + 1;
                _classifications = (int)packet.ReadBits(6) + 1;
                _classBookNum = (int)packet.ReadBits(8);

                _classBook = _vorbis.Books[_classBookNum];

                _cascade = new int[_classifications];
                var acc = 0;
                var maxBits = 0;
                for (int i = 0; i < _classifications; i++)
                {
                    var high_bits = 0;
                    var low_bits = (int)packet.ReadBits(3);
                    if (packet.ReadBit()) high_bits = (int)packet.ReadBits(5);
                    _cascade[i] = high_bits << 3 | low_bits;
                    acc += icount(_cascade[i]);
                    maxBits = Math.Max(Utils.ilog(_cascade[i]), maxBits);
                }
                _maxPasses = maxBits;

                var bookNums = new int[acc];
                for (var i = 0; i < acc; i++)
                {
                    bookNums[i] = (int)packet.ReadBits(8);
                }

                var bookIdx = 0;
                _books = new VorbisCodebook[_classifications][];
                _bookNums = new int[_classifications][];
                for (int i = 0; i < _classifications; i++)
                {
                    _books[i] = new VorbisCodebook[8];
                    _bookNums[i] = new int[8];
                    for (int j = 1, idx = 0; j < 256; j <<= 1, idx++)
                    {
                        if ((_cascade[i] & j) == j)
                        {
                            var bookNum = bookNums[bookIdx++];
                            _books[i][idx] = _vorbis.Books[bookNum];
                            _bookNums[i][idx] = bookNum;
                            if (_books[i][idx].MapType == 0) throw new InvalidDataException();
                        }
                    }
                }

                _classWordsPerCodeWord = _classBook.Dimensions;
                _nToRead = _end - _begin;
                _partsToRead = _nToRead / _partitionSize;
                _partWords = (_partsToRead + _classWordsPerCodeWord - 1) / _classWordsPerCodeWord;
            }
Beispiel #48
0
        bool LoadBooks(DataPacket packet)
        {
            bool equal = true;

            byte[] sequence = new byte[] { 0x05, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 };
            byte[] packets  = packet.ReadBytes(7);

            for (int i = 0; i < 7; i++)
            {
                if (packets[i] != sequence[i])
                {
                    equal = false;
                    break;
                }
            }

            if (!equal)
            {
                return(false);
            }

            if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber)))
            {
                _pagesSeen.Add(_lastPageSeen);
            }

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits       = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits          = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Init(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits           = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Init(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ilog(Modes.Length - 1);

            return(true);
        }
Beispiel #49
0
 private void InitTree(DataPacket packet)
 {
   int num1 = 0;
   bool flag;
   if (packet.ReadBit())
   {
     int num2 = (int) packet.ReadBits(5) + 1;
     int num3 = 0;
     while (num3 < this.Entries)
     {
       int num4 = (int) packet.ReadBits(Utils.ilog(this.Entries - num3));
       while (--num4 >= 0)
         this.Lengths[num3++] = num2;
       ++num2;
     }
     num1 = 0;
     flag = false;
   }
   else
   {
     flag = packet.ReadBit();
     for (int index = 0; index < this.Entries; ++index)
     {
       if (!flag || packet.ReadBit())
       {
         this.Lengths[index] = (int) packet.ReadBits(5) + 1;
         ++num1;
       }
       else
         this.Lengths[index] = -1;
     }
   }
   this.MaxBits = Enumerable.Max((IEnumerable<int>) this.Lengths);
   int[] numArray1 = (int[]) null;
   if (flag && num1 >= this.Entries >> 2)
   {
     numArray1 = new int[this.Entries];
     Array.Copy((Array) this.Lengths, (Array) numArray1, this.Entries);
     flag = false;
   }
   int length = !flag ? 0 : num1;
   int[] numArray2 = (int[]) null;
   int[] codeList = (int[]) null;
   if (!flag)
     codeList = new int[this.Entries];
   else if (length != 0)
   {
     numArray1 = new int[length];
     codeList = new int[length];
     numArray2 = new int[length];
   }
   VorbisCodebook vorbisCodebook = this;
   int[] numArray3 = this.Lengths;
   int num5 = this.Entries;
   int[] numArray4 = numArray2;
   int num6 = flag ? 1 : 0;
   int sortedEntries = length;
   int[] codewords = codeList;
   int[] codewordLengths = numArray1;
   int[] len = numArray3;
   int n = num5;
   int[] values = numArray4;
   if (!vorbisCodebook.ComputeCodewords(num6 != 0, sortedEntries, codewords, codewordLengths, len, n, values))
     throw new InvalidDataException();
   this.LTree = Huffman.BuildLinkedList<int>(numArray2 ?? Enumerable.ToArray<int>(Enumerable.Range(0, codeList.Length)), numArray1 ?? this.Lengths, codeList);
 }
Beispiel #50
0
 internal override VorbisFloor.PacketData UnpackPacket(DataPacket packet, int blockSize)
 {
   VorbisFloor.Floor1.PacketData1 packetData1_1 = new VorbisFloor.Floor1.PacketData1();
   packetData1_1.BlockSize = blockSize;
   VorbisFloor.Floor1.PacketData1 packetData1_2 = packetData1_1;
   if (packet.ReadBit())
   {
     try
     {
       int index1 = 2;
       int[] numArray = ACache.Get<int>(64);
       numArray[0] = (int) packet.ReadBits(this._yBits);
       numArray[1] = (int) packet.ReadBits(this._yBits);
       for (int index2 = 0; index2 < this._partitionClass.Length; ++index2)
       {
         int index3 = this._partitionClass[index2];
         int num1 = this._classDimensions[index3];
         int num2 = this._classSubclasses[index3];
         int num3 = (1 << num2) - 1;
         uint num4 = 0U;
         if (num2 > 0)
           num4 = (uint) this._classMasterbooks[index3].DecodeScalar(packet);
         for (int index4 = 0; index4 < num1; ++index4)
         {
           VorbisCodebook vorbisCodebook = this._subclassBooks[index3][(long) num4 & (long) num3];
           num4 >>= num2;
           if (vorbisCodebook != null)
             numArray[index1] = vorbisCodebook.DecodeScalar(packet);
           ++index1;
         }
       }
       packetData1_2.Posts = numArray;
       packetData1_2.PostCount = index1;
     }
     catch (EndOfStreamException ex)
     {
     }
   }
   return (VorbisFloor.PacketData) packetData1_2;
 }
        public bool LoadBooks(DataPacket packet)
        {
            if (!packet.ReadBytes(7).SequenceEqual(new byte[7]
            {
                5,
                118,
                111,
                114,
                98,
                105,
                115
            }))
            {
                return(false);
            }
            if (!_pagesSeen.Contains(_lastPageSeen = packet.PageSequenceNumber))
            {
                _pagesSeen.Add(_lastPageSeen);
            }
            long bitsRead = packet.BitsRead;

            _glueBits += packet.BitsRead;
            Books      = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }
            _bookBits += packet.BitsRead - bitsRead;
            bitsRead   = packet.BitsRead;
            Times      = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int j = 0; j < Times.Length; j++)
            {
                Times[j] = VorbisTime.Init(this, packet);
            }
            _timeHdrBits += packet.BitsRead - bitsRead;
            bitsRead      = packet.BitsRead;
            Floors        = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int k = 0; k < Floors.Length; k++)
            {
                Floors[k] = VorbisFloor.Init(this, packet);
            }
            _floorHdrBits += packet.BitsRead - bitsRead;
            bitsRead       = packet.BitsRead;
            Residues       = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int l = 0; l < Residues.Length; l++)
            {
                Residues[l] = VorbisResidue.Init(this, packet);
            }
            _resHdrBits += packet.BitsRead - bitsRead;
            bitsRead     = packet.BitsRead;
            Maps         = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int m = 0; m < Maps.Length; m++)
            {
                Maps[m] = VorbisMapping.Init(this, packet);
            }
            _mapHdrBits += packet.BitsRead - bitsRead;
            bitsRead     = packet.BitsRead;
            Modes        = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int n = 0; n < Modes.Length; n++)
            {
                Modes[n] = VorbisMode.Init(this, packet);
            }
            _modeHdrBits += packet.BitsRead - bitsRead;
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }
            _glueBits++;
            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;
            _modeFieldBits = Utils.ilog(Modes.Length - 1);
            return(true);
        }
Beispiel #52
0
        bool UnpackPacket(DataPacket packet)
        {
            // make sure we're on an audio packet
            if (packet.ReadBit())
            {
                // we really can't do anything... count the bits as waste
                return(false);
            }

            // get mode and prev/next flags
            var modeBits = _modeFieldBits;

            _mode = Modes[(int)packet.ReadBits(_modeFieldBits)];
            if (_mode.BlockFlag)
            {
                _prevFlag = packet.ReadBit();
                _nextFlag = packet.ReadBit();
                modeBits += 2;
            }
            else
            {
                _prevFlag = _nextFlag = false;
            }

            if (packet.IsShort)
            {
                return(false);
            }

            var startBits = packet.BitsRead;

            var halfBlockSize = _mode.BlockSize / 2;

            // read the noise floor data (but don't decode yet)
            for (int i = 0; i < _channels; i++)
            {
                _floorData[i]        = _mode.Mapping.ChannelSubmap[i].Floor.UnpackPacket(packet, _mode.BlockSize, i);
                _noExecuteChannel[i] = !_floorData[i].ExecuteChannel;

                // go ahead and clear the residue buffers
                Array.Clear(_residue[i], 0, halfBlockSize);
            }

            // make sure we handle no-energy channels correctly given the couplings...
            foreach (var step in _mode.Mapping.CouplingSteps)
            {
                if (_floorData[step.Angle].ExecuteChannel || _floorData[step.Magnitude].ExecuteChannel)
                {
                    _floorData[step.Angle].ForceEnergy     = true;
                    _floorData[step.Magnitude].ForceEnergy = true;
                }
            }

            var floorBits = packet.BitsRead - startBits;

            startBits = packet.BitsRead;

            foreach (var subMap in _mode.Mapping.Submaps)
            {
                for (int j = 0; j < _channels; j++)
                {
                    if (_mode.Mapping.ChannelSubmap[j] != subMap)
                    {
                        _floorData[j].ForceNoEnergy = true;
                    }
                }

                var rTemp = subMap.Residue.Decode(packet, _noExecuteChannel, _channels, _mode.BlockSize);
                for (int c = 0; c < _channels; c++)
                {
                    var r  = _residue[c];
                    var rt = rTemp[c];
                    for (int i = 0; i < halfBlockSize; i++)
                    {
                        r[i] += rt[i];
                    }
                }
            }

            _glueBits  += 1;
            _modeBits  += modeBits;
            _floorBits += floorBits;
            _resBits   += packet.BitsRead - startBits;
            _wasteBits += 8 * packet.Length - packet.BitsRead;

            _packetCount += 1;

            return(true);
        }
        bool LoadBooks(DataPacket packet)
        {
            if (!CheckForHeader(packet, bookHeader))
            {
                return(false);
            }

            if (!_pagesSeen.Contains(_lastPageSeen = packet.PageSequenceNumber))
            {
                _pagesSeen.Add(_lastPageSeen);
            }

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Create(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits       = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits          = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Create(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits           = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Create(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ILog(Modes.Length - 1);

            return(true);
        }