Exemplo n.º 1
0
    public static int GetQuantVals(this IStaticCodeBook book)
    {
        var vals = (int)Math.Floor(Math.Pow(book.LengthList.Length, 1f / book.Dimensions));

        // the above *should* be reliable, but we'll not assume that FP is
        // ever reliable when bitstream sync is at stake; verify via integer
        // means that vals really is the greatest value of dim for which
        // vals^b->bim <= b->entries
        // treat the above as an initial guess
        while (true)
        {
            var acc  = 1;
            var acc1 = 1;

            for (var i = 0; i < book.Dimensions; i++)
            {
                acc  *= vals;
                acc1 *= vals + 1;
            }

            if ((acc <= book.LengthList.Length) && (acc1 > book.LengthList.Length))
            {
                return(vals);
            }

            if (acc > book.LengthList.Length)
            {
                vals--;
            }
            else
            {
                vals++;
            }
        }
    }
 public CodeBook(
     int dimensions,
     int entries,
     int usedEntries,
     IStaticCodeBook staticBook,
     float[] valueList,
     uint[] codeList,
     int[] decIndex,
     byte[] decCodeLengths,
     uint[] decFirstTable,
     int decFirstTableN,
     int decMaxLength,
     int quantValues,
     int minVal,
     int delta)
 {
     Dimensions     = dimensions;
     Entries        = entries;
     UsedEntries    = usedEntries;
     StaticBook     = staticBook;
     ValueList      = valueList;
     CodeList       = codeList;
     DecIndex       = decIndex;
     DecCodeLengths = decCodeLengths;
     DecFirstTable  = decFirstTable;
     DecFirstTableN = decFirstTableN;
     DecMaxLength   = decMaxLength;
     QuantValues    = quantValues;
     MinVal         = minVal;
     Delta          = delta;
 }
Exemplo n.º 3
0
    private static int GetOrAddBook(CodecSetup codecSetup, IStaticCodeBook codeBook)
    {
        int i;

        for (i = 0; i < codecSetup.BookParams.Count; i++)
        {
            if (codecSetup.BookParams[i] == codeBook)
            {
                return(i);
            }
        }

        codecSetup.BookParams.Add(codeBook);
        return(codecSetup.BookParams.Count - 1);
    }
 public static CodeBook InitEncode(IStaticCodeBook source)
 {
     return(new CodeBook(
                source.Dimensions,
                source.LengthList.Length,
                source.LengthList.Length,
                source,
                null,
                Encoding.MakeWords(source.LengthList, 0),
                null,
                null,
                null,
                0,
                0,
                source.GetQuantVals(),
                (int)Math.Round(Encoding.UnpackFloat(source.QuantMin)),
                (int)Math.Round(Encoding.UnpackFloat(source.QuantDelta))
                ));
 }
Exemplo n.º 5
0
 public ResidueTemplate(
     ResidueType residueType,
     ResidueLimitType limitType,
     int grouping,
     ResidueEntry residue,
     IStaticCodeBook bookAux,
     IStaticCodeBook booxAuxManaged,
     IStaticBookBlock booksBase,
     IStaticBookBlock booksBaseManaged)
 {
     ResidueType      = residueType;
     LimitType        = limitType;
     Residue          = residue;
     BookAux          = bookAux;
     BookAuxManaged   = booxAuxManaged;
     BooksBase        = booksBase;
     BooksBaseManaged = booksBaseManaged;
     Grouping         = grouping;
 }
Exemplo n.º 6
0
    private static void FillBooks(
        CodecSetup codecSetup,
        ResidueEntry r,
        IStaticCodeBook bookAux,
        IStaticBookBlock bookBlock)
    {
        for (var i = 0; i < r.Partitions; i++)
        {
            for (var k = 0; k < 4; k++)
            {
                if ((i < bookBlock.Books.Length) &&
                    (k < bookBlock.Books[i].Length) &&
                    (bookBlock.Books[i][k] != null))
                {
                    r.SecondStages[i] |= 1 << k;
                }
            }
        }

        r.GroupBook = GetOrAddBook(codecSetup, bookAux);

        var booklist = 0;

        for (var i = 0; i < r.Partitions; i++)
        {
            for (var k = 0; k < 4; k++)
            {
                if ((i < bookBlock.Books.Length) &&
                    (k < bookBlock.Books[i].Length))
                {
                    var sourceBook = bookBlock.Books[i][k];
                    if (sourceBook != null)
                    {
                        var bookid = GetOrAddBook(codecSetup, sourceBook);
                        r.BookList[booklist++] = bookid;
                    }
                }
            }
        }
    }
Exemplo n.º 7
0
        private void PackStaticBook(EncodeBuffer buffer, IStaticCodeBook book)
        {
            var ordered = false;

            // first the basic parameters
            buffer.Write(0x564342, 24);
            buffer.Write((uint)book.Dimensions, 16);
            buffer.Write((uint)book.LengthList.Length, 24);

            // pack the codewords.  There are two packing types; length ordered and length random.
            int i;

            for (i = 1; i < book.LengthList.Length; i++)
            {
                if ((book.LengthList[i - 1] == 0) || (book.LengthList[i] < book.LengthList[i - 1]))
                {
                    break;
                }
            }

            if (i == book.LengthList.Length)
            {
                ordered = true;
            }

            if (ordered)
            {
                // length ordered.  We only need to say how many codewords of each length.  The actual codewords are generated deterministically
                buffer.Write(1, 1);

                buffer.Write((uint)(book.LengthList[0] - 1), 5);  // 1 to 32

                var count = 0;
                for (i = 1; i < book.LengthList.Length; i++)
                {
                    var current  = book.LengthList[i];
                    var previous = book.LengthList[i - 1];

                    if (current <= previous)
                    {
                        continue;
                    }

                    for (var j = previous; j < current; j++)
                    {
                        buffer.Write((uint)(i - count), Encoding.Log(book.LengthList.Length - count));
                        count = i;
                    }
                }

                buffer.Write((uint)(i - count), Encoding.Log(book.LengthList.Length - count));
            }
            else
            {
                // length unordered. Again, we don't code the codeword itself, just the length. This time, though, we have to encode each length
                buffer.Write(0, 1);

                /* algorithmic mapping has use for 'unused entries', which we tag
                 * here.  The algorithmic mapping happens as usual, but the unused
                 * entry has no codeword. */
                for (i = 0; i < book.LengthList.Length; i++)
                {
                    if (book.LengthList[i] == 0)
                    {
                        break;
                    }
                }

                if (i == book.LengthList.Length)
                {
                    buffer.Write(0, 1); // no unused entries
                    for (i = 0; i < book.LengthList.Length; i++)
                    {
                        buffer.Write((uint)(book.LengthList[i] - 1), 5);
                    }
                }
                else
                {
                    buffer.Write(1, 1); // we have unused entries; thus we tag
                    for (i = 0; i < book.LengthList.Length; i++)
                    {
                        if (book.LengthList[i] == 0)
                        {
                            buffer.Write(0, 1);
                        }
                        else
                        {
                            buffer.Write(1, 1);
                            buffer.Write((uint)(book.LengthList[i] - 1), 5);
                        }
                    }
                }
            }

            buffer.Write((uint)book.MapType, 4);
            if (book.MapType == CodeBookMapType.None)
            {
                return;
            }

            // is the entry number the desired return value, or do we have a mapping? If we have a mapping, what type?
            if ((book.MapType != CodeBookMapType.Implicit) && (book.MapType != CodeBookMapType.Listed))
            {
                throw new InvalidOperationException("Unknown CodeBookMapType: {book.MapType}");
            }

            if (book.QuantList == null)
            {
                throw new InvalidOperationException("QuantList cannot be null");
            }

            // values that define the dequantization
            buffer.Write((uint)book.QuantMin, 32);
            buffer.Write((uint)book.QuantDelta, 32);
            buffer.Write((uint)(book.Quant - 1), 4);
            buffer.Write((uint)book.QuantSequenceP, 1);

            var quantVals = 0;

            switch (book.MapType)
            {
            case CodeBookMapType.Implicit:
                // a single column of (c.entries/c.dim) quantized values for building a full value list algorithmically (square lattice)
                quantVals = book.GetQuantVals();
                break;

            case CodeBookMapType.Listed:
                // every value (c.entries*c.dim total) specified explicitly
                quantVals = book.LengthList.Length * book.Dimensions;
                break;
            }

            // quantized values
            for (i = 0; i < quantVals; i++)
            {
                buffer.Write((uint)Math.Abs(book.QuantList[i]), book.Quant);
            }
        }