Example #1
0
        internal int DecodeScalar(DataPacket packet)
        {
            int index = (int)packet.TryPeekBits(PrefixBitLength, out int bitsRead);

            if (bitsRead == 0)
            {
                return(-1);
            }
            HuffmanListNode huffmanListNode = PrefixList[index];

            if (huffmanListNode != null)
            {
                packet.SkipBits(huffmanListNode.Length);
                return(huffmanListNode.Value);
            }
            index           = (int)packet.TryPeekBits(MaxBits, out bitsRead);
            huffmanListNode = PrefixOverflowTree;
            do
            {
                if (huffmanListNode.Bits == (index & huffmanListNode.Mask))
                {
                    packet.SkipBits(huffmanListNode.Length);
                    return(huffmanListNode.Value);
                }
            }while ((huffmanListNode = huffmanListNode.Next) != null);
            return(-1);
        }
Example #2
0
     internal static HuffmanListNode <T> BuildLinkedList <T>(T[] values, int[] lengthList, int[] codeList)
     {
         HuffmanListNode <T>[] array = new HuffmanListNode <T> [lengthList.Length];
         for (int index = 0; index < array.Length; ++index)
         {
             array[index] = new HuffmanListNode <T>()
             {
                 Value  = values[index],
                 Length = lengthList[index] <= 0 ? 99999 : lengthList[index],
                 Bits   = codeList[index],
                 Mask   = (1 << lengthList[index]) - 1
             }
         }
         ;
         Array.Sort <HuffmanListNode <T> >(array, (Comparison <HuffmanListNode <T> >)((i1, i2) =>
         {
             int local_0 = i1.Length - i2.Length;
             if (local_0 == 0)
             {
                 return(i1.Bits - i2.Bits);
             }
             else
             {
                 return(local_0);
             }
         }));
         for (int index = 1; index < array.Length && array[index].Length < 99999; ++index)
         {
             array[index - 1].Next = array[index];
         }
         return(array[0]);
     }
 }
Example #3
0
 static int SortCallback(HuffmanListNode i1, HuffmanListNode i2)
 {
     var len = i1.Length - i2.Length;
     if (len == 0)
     {
         return i1.Bits - i2.Bits;
     }
     return len;
 }
Example #4
0
        internal static List <HuffmanListNode> BuildPrefixedLinkedList(int[] values, int[] lengthList, int[] codeList, out int tableBits, out HuffmanListNode firstOverflowNode)
        {
            HuffmanListNode[] array = new HuffmanListNode[lengthList.Length];
            int num = 0;

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = new HuffmanListNode
                {
                    Value  = values[i],
                    Length = ((lengthList[i] <= 0) ? 99999 : lengthList[i]),
                    Bits   = codeList[i],
                    Mask   = (1 << lengthList[i]) - 1
                };
                if (lengthList[i] > 0 && num < lengthList[i])
                {
                    num = lengthList[i];
                }
            }
            Array.Sort(array, SortCallback);
            tableBits = ((num > 10) ? 10 : num);
            List <HuffmanListNode> list = new List <HuffmanListNode>(1 << tableBits);

            firstOverflowNode = null;
            for (int j = 0; j < array.Length && array[j].Length < 99999; j++)
            {
                if (firstOverflowNode == null)
                {
                    int length = array[j].Length;
                    if (length > tableBits)
                    {
                        firstOverflowNode = array[j];
                        continue;
                    }
                    int             num2            = 1 << tableBits - length;
                    HuffmanListNode huffmanListNode = array[j];
                    for (int k = 0; k < num2; k++)
                    {
                        int num3 = (k << length) | huffmanListNode.Bits;
                        while (list.Count <= num3)
                        {
                            list.Add(null);
                        }
                        list[num3] = huffmanListNode;
                    }
                }
                else
                {
                    array[j - 1].Next = array[j];
                }
            }
            while (list.Count < 1 << tableBits)
            {
                list.Add(null);
            }
            return(list);
        }
Example #5
0
        public static int SortCallback(HuffmanListNode i1, HuffmanListNode i2)
        {
            int num = i1.Length - i2.Length;

            if (num == 0)
            {
                return(i1.Bits - i2.Bits);
            }
            return(num);
        }
Example #6
0
        static int SortCallback(HuffmanListNode i1, HuffmanListNode i2)
        {
            var len = i1.Length - i2.Length;

            if (len == 0)
            {
                return(i1.Bits - i2.Bits);
            }
            return(len);
        }
Example #7
0
        internal int DecodeScalar(DataPacket packet)
        {
            int bitsRead;
            int num = (int)packet.TryPeekBits(this.MaxBits, out bitsRead);

            if (bitsRead == 0)
            {
                throw new InvalidDataException();
            }
            for (HuffmanListNode <int> huffmanListNode = this.LTree; huffmanListNode != null; huffmanListNode = huffmanListNode.Next)
            {
                if (huffmanListNode.Bits == (num & huffmanListNode.Mask))
                {
                    ++huffmanListNode.HitCount;
                    packet.SkipBits(huffmanListNode.Length);
                    return(huffmanListNode.Value);
                }
            }
            throw new InvalidDataException();
        }
Example #8
0
        internal static HuffmanListNode BuildLinkedList(int[] values, int[] lengthList, int[] codeList)
        {
            HuffmanListNode[] list = new HuffmanListNode[lengthList.Length];

            for (int i = 0; i < list.Length; i++)
            {
                list[i] = new HuffmanListNode
                {
                    Value = values[i],
                    Length = lengthList[i] <= 0 ? 99999 : lengthList[i],
                    Bits = codeList[i],
                    Mask = (1 << lengthList[i]) - 1,
                };
            }

            Array.Sort(list, SortCallback);

            for (int i = 1; i < list.Length && list[i].Length < 99999; i++)
            {
                list[i - 1].Next = list[i];
            }

            return list[0];
        }
Example #9
0
        static internal HuffmanListNode BuildLinkedList(int[] values, int[] lengthList, int[] codeList)
        {
            HuffmanListNode[] list = new HuffmanListNode[lengthList.Length];

            for (int i = 0; i < list.Length; i++)
            {
                list[i] = new HuffmanListNode
                {
                    Value  = values[i],
                    Length = lengthList[i] <= 0 ? 99999 : lengthList[i],
                    Bits   = codeList[i],
                    Mask   = (1 << lengthList[i]) - 1,
                };
            }

            Array.Sort(list, SortCallback);

            for (int i = 1; i < list.Length && list[i].Length < 99999; i++)
            {
                list[i - 1].Next = list[i];
            }

            return(list[0]);
        }
Example #10
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);
        }
Example #11
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);
        }
Example #12
0
        static internal System.Collections.Generic.List <HuffmanListNode> BuildPrefixedLinkedList(int[] values, int[] lengthList, int[] codeList, out int tableBits, out HuffmanListNode firstOverflowNode)
        {
            HuffmanListNode[] list = new HuffmanListNode[lengthList.Length];

            var maxLen = 0;

            for (int i = 0; i < list.Length; i++)
            {
                list[i] = new HuffmanListNode
                {
                    Value  = values[i],
                    Length = lengthList[i] <= 0 ? 99999 : lengthList[i],
                    Bits   = codeList[i],
                    Mask   = (1 << lengthList[i]) - 1,
                };
                if (lengthList[i] > 0 && maxLen < lengthList[i])
                {
                    maxLen = lengthList[i];
                }
            }

            Array.Sort(list, SortCallback);

            tableBits = maxLen > MAX_TABLE_BITS ? MAX_TABLE_BITS : maxLen;

            var prefixList = new System.Collections.Generic.List <HuffmanListNode>(1 << tableBits);

            firstOverflowNode = null;
            for (int i = 0; i < list.Length && list[i].Length < 99999; i++)
            {
                if (firstOverflowNode == null)
                {
                    var itemBits = list[i].Length;
                    if (itemBits > tableBits)
                    {
                        firstOverflowNode = list[i];
                    }
                    else
                    {
                        var maxVal = 1 << (tableBits - itemBits);
                        var item   = list[i];
                        for (int j = 0; j < maxVal; j++)
                        {
                            var idx = (j << itemBits) | item.Bits;
                            while (prefixList.Count <= idx)
                            {
                                prefixList.Add(null);
                            }
                            prefixList[idx] = item;
                        }
                    }
                }
                else
                {
                    list[i - 1].Next = list[i];
                }
            }

            while (prefixList.Count < 1 << tableBits)
            {
                prefixList.Add(null);
            }

            return(prefixList);
        }
Example #13
0
        static internal System.Collections.Generic.List<HuffmanListNode> BuildPrefixedLinkedList(int[] values, int[] lengthList, int[] codeList, out int tableBits, out HuffmanListNode firstOverflowNode)
        {
            HuffmanListNode[] list = new HuffmanListNode[lengthList.Length];

            var maxLen = 0;
            for (int i = 0; i < list.Length; i++)
            {
                list[i] = new HuffmanListNode
                {
                    Value = values[i],
                    Length = lengthList[i] <= 0 ? 99999 : lengthList[i],
                    Bits = codeList[i],
                    Mask = (1 << lengthList[i]) - 1,
                };
                if (lengthList[i] > 0 && maxLen < lengthList[i])
                {
                    maxLen = lengthList[i];
                }
            }

            Array.Sort(list, SortCallback);

            tableBits = maxLen > MAX_TABLE_BITS ? MAX_TABLE_BITS : maxLen;

            var prefixList = new System.Collections.Generic.List<HuffmanListNode>(1 << tableBits);
            firstOverflowNode = null;
            for (int i = 0; i < list.Length && list[i].Length < 99999; i++)
            {
                if (firstOverflowNode == null)
                {
                    var itemBits = list[i].Length;
                    if (itemBits > tableBits)
                    {
                        firstOverflowNode = list[i];
                    }
                    else
                    {
                        var maxVal = 1 << (tableBits - itemBits);
                        var item = list[i];
                        for (int j = 0; j < maxVal; j++)
                        {
                            var idx = (j << itemBits) | item.Bits;
                            while (prefixList.Count <= idx)
                            {
                                prefixList.Add(null);
                            }
                            prefixList[idx] = item;
                        }
                    }
                }
                else
                {
                    list[i - 1].Next = list[i];
                }
            }

            while (prefixList.Count < 1 << tableBits)
            {
                prefixList.Add(null);
            }

            return prefixList;
        }
Example #14
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);
 }