csvorbis.StaticCodeBook.pack C# (CSharp) Method

pack() private method

private pack ( csBuffer opb ) : int
opb csogg.csBuffer
return int
        internal int pack(csBuffer opb)
        {
            int i;
            bool ordered=false;

            opb.write(0x564342,24);
            opb.write(dim, 16);
            opb.write(entries, 24);

            // pack the codewords.  There are two packings; length ordered and
            // length random.  Decide between the two now.

            for(i=1;i<entries;i++)
            {
                if(lengthlist[i]<lengthlist[i-1])break;
            }
            if(i==entries)ordered=true;

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

                int count=0;
                opb.write(1,1);               // ordered
                opb.write(lengthlist[0]-1,5); // 1 to 32

                for(i=1;i<entries;i++)
                {
                    int _this=lengthlist[i];
                    int _last=lengthlist[i-1];
                    if(_this>_last)
                    {
                        for(int j=_last;j<_this;j++)
                        {
                            opb.write(i-count,ilog(entries-count));
                            count=i;
                        }
                    }
                }
                opb.write(i-count,ilog(entries-count));
            }
            else
            {
                // length random.  Again, we don't code the codeword itself, just
                // the length.  This time, though, we have to encode each length
                opb.write(0,1);   // unordered

                // algortihmic 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<entries;i++)
                {
                    if(lengthlist[i]==0)break;
                }

                if(i==entries)
                {
                    opb.write(0,1); // no unused entries
                    for(i=0;i<entries;i++)
                    {
                        opb.write(lengthlist[i]-1,5);
                    }
                }
                else
                {
                    opb.write(1,1); // we have unused entries; thus we tag
                    for(i=0;i<entries;i++)
                    {
                        if(lengthlist[i]==0)
                        {
                            opb.write(0,1);
                        }
                        else
                        {
                            opb.write(1,1);
                            opb.write(lengthlist[i]-1,5);
                        }
                    }
                }
            }

            // is the entry number the desired return value, or do we have a
            // mapping? If we have a mapping, what type?
            opb.write(maptype,4);
            switch(maptype)
            {
                case 0:
                    // no mapping
                    break;
                case 1:
                case 2:
                    // implicitly populated value mapping
                    // explicitly populated value mapping
                    if(quantlist==null)
                    {
                        // no quantlist?  error
                        return(-1);
                    }

                    // values that define the dequantization
                    opb.write(q_min,32);
                    opb.write(q_delta,32);
                    opb.write(q_quant-1,4);
                    opb.write(q_sequencep,1);

                {
                    int quantvals=0;
                    switch(maptype)
                    {
                        case 1:
                            // a single column of (c->entries/c->dim) quantized values for
                            // building a full value list algorithmically (square lattice)
                            quantvals=maptype1_quantvals();
                            break;
                        case 2:
                            // every value (c->entries*c->dim total) specified explicitly
                            quantvals=entries*dim;
                            break;
                    }

                    // quantized values
                    for(i=0;i<quantvals;i++)
                    {
                        opb.write(Math.Abs(quantlist[i]),q_quant);
                    }
                }
                    break;
                default:
                    // error case; we don't have any other map types now
                    return(-1);
            }
            return(0);
        }