Пример #1
0
        private int[] HuffmanDecoding(List <int> list, Dictionary <int, int> counts)
        {
            Huffman <int> huffman = new Huffman <int>(counts);

            return(huffman.Decode(list).ToArray());
        }
Пример #2
0
        private void MultiHuffmanEncoding(int i, int[] yRunLenEncoded, int[] cBRunLenEncoded, int[] cRRunLenEncoded, bool isLastFrame, int[][] YHuffmanValues, int[][] CbHuffmanValues, int[][] CrHuffmanValues)
        {
            YHuffmanValues[i % keyFrameEvery]  = yRunLenEncoded;
            CbHuffmanValues[i % keyFrameEvery] = cBRunLenEncoded;
            CrHuffmanValues[i % keyFrameEvery] = cRRunLenEncoded;

            if (isLastFrame)
            {
                int[] yTemp  = new int[0];
                int[] cBTemp = new int[0];
                int[] cRTemp = new int[0];

                int frameAfterLastKeyFrame = i - (i % keyFrameEvery);
                int max = (i % keyFrameEvery) + 1;

                for (int j = 0; j < max; j++)
                {
                    int yLength  = yTemp.Length;
                    int cBLength = cBTemp.Length;
                    int cRLength = cRTemp.Length;
                    Array.Resize(ref yTemp, yLength + YHuffmanValues[j].Length);
                    Array.Resize(ref cBTemp, cBLength + CbHuffmanValues[j].Length);
                    Array.Resize(ref cRTemp, cRLength + CrHuffmanValues[j].Length);
                    Array.Copy(YHuffmanValues[j], 0, yTemp, yLength, YHuffmanValues[j].Length);
                    Array.Copy(CbHuffmanValues[j], 0, cBTemp, cBLength, CbHuffmanValues[j].Length);
                    Array.Copy(CrHuffmanValues[j], 0, cRTemp, cRLength, CrHuffmanValues[j].Length);
                }

                Huffman <int> YHuffman  = new Huffman <int>(yTemp);
                Huffman <int> CbHuffman = new Huffman <int>(cBTemp);
                Huffman <int> CrHuffman = new Huffman <int>(cRTemp);

                for (int j = 0; j < max; j++)
                {
                    int k = frameAfterLastKeyFrame + j;
                    YBitArray[k]  = HuffmanEncoding(YHuffman, YHuffmanValues[j]);
                    CbBitArray[k] = HuffmanEncoding(CbHuffman, CbHuffmanValues[j]);
                    CrBitArray[k] = HuffmanEncoding(CrHuffman, CrHuffmanValues[j]);
                }

                YHuffmanCounts[i / keyFrameEvery]  = YHuffman.GetCounts();
                CbHuffmanCounts[i / keyFrameEvery] = CbHuffman.GetCounts();
                CrHuffmanCounts[i / keyFrameEvery] = CrHuffman.GetCounts();
            }
            else if (i % keyFrameEvery == keyFrameEvery - 1)
            {
                int[] yTemp  = new int[0];
                int[] cBTemp = new int[0];
                int[] cRTemp = new int[0];

                for (int j = 0; j < keyFrameEvery; j++)
                {
                    int yLength  = yTemp.Length;
                    int cBLength = cBTemp.Length;
                    int cRLength = cRTemp.Length;
                    Array.Resize(ref yTemp, yLength + YHuffmanValues[j].Length);
                    Array.Resize(ref cBTemp, cBLength + CbHuffmanValues[j].Length);
                    Array.Resize(ref cRTemp, cRLength + CrHuffmanValues[j].Length);
                    Array.Copy(YHuffmanValues[j], 0, yTemp, yLength, YHuffmanValues[j].Length);
                    Array.Copy(CbHuffmanValues[j], 0, cBTemp, cBLength, CbHuffmanValues[j].Length);
                    Array.Copy(CrHuffmanValues[j], 0, cRTemp, cRLength, CrHuffmanValues[j].Length);
                }

                Huffman <int> YHuffman  = new Huffman <int>(yTemp);
                Huffman <int> CbHuffman = new Huffman <int>(cBTemp);
                Huffman <int> CrHuffman = new Huffman <int>(cRTemp);

                for (int j = 0; j < keyFrameEvery; j++)
                {
                    int k = i - keyFrameEvery + 1 + j;
                    YBitArray[k]  = HuffmanEncoding(YHuffman, YHuffmanValues[j]);
                    CbBitArray[k] = HuffmanEncoding(CbHuffman, CbHuffmanValues[j]);
                    CrBitArray[k] = HuffmanEncoding(CrHuffman, CrHuffmanValues[j]);
                }

                YHuffmanCounts[i / keyFrameEvery]  = YHuffman.GetCounts();
                CbHuffmanCounts[i / keyFrameEvery] = CbHuffman.GetCounts();
                CrHuffmanCounts[i / keyFrameEvery] = CrHuffman.GetCounts();
            }
        }
Пример #3
0
 private List <int> HuffmanEncoding(Huffman <int> huffman, int[] array)
 {
     return(huffman.Encode(array));
 }