Example #1
0
 public abstract Object unpack(Info info, csBuffer buffer);
Example #2
0
        public override Object unpack(Info vi, csBuffer opb)
        {
            int        count    = 0;
            int        maxclass = -1;
            InfoFloor1 info     = new InfoFloor1();

            /* read partitions */
            info.partitions = opb.read(5);                        // only 0 to 31 legal
            for (int j = 0; j < info.partitions; j++)
            {
                info.partitionclass[j] = opb.read(4);                 // only 0 to 15 legal
                if (maxclass < info.partitionclass[j])
                {
                    maxclass = info.partitionclass[j];
                }
            }

            /* read partition classes */
            for (int j = 0; j < maxclass + 1; j++)
            {
                info.class_dim[j]  = opb.read(3) + 1;                // 1 to 8
                info.class_subs[j] = opb.read(2);                    // 0,1,2,3 bits

                if (info.class_subs[j] < 0)
                {
                    info.free();
                    return(null);
                }

                if (info.class_subs[j] != 0)
                {
                    info.class_book[j] = opb.read(8);
                }

                if (info.class_book[j] < 0 || info.class_book[j] >= vi.books)
                {
                    info.free();
                    return(null);
                }

                for (int k = 0; k < (1 << info.class_subs[j]); k++)
                {
                    info.class_subbook[j][k] = opb.read(8) - 1;
                    if (info.class_subbook[j][k] < -1 || info.class_subbook[j][k] >= vi.books)
                    {
                        info.free();
                        return(null);
                    }
                }
            }

            /* read the post list */
            info.mult = opb.read(2) + 1;             // only 1,2,3,4 legal now
            int rangebits = opb.read(4);

            if (rangebits < 0)
            {
                info.free();
                return(null);
            }

            for (int j = 0, k = 0; j < info.partitions; j++)
            {
                count += info.class_dim[info.partitionclass[j]];
                if (count > VIF_POSIT)
                {
                    info.free();
                    return(null);
                }

                for (; k < count; k++)
                {
                    int t = info.postlist[k + 2] = opb.read(rangebits);
                    if (t < 0 || t >= (1 << rangebits))
                    {
                        info.free();
                        return(null);
                    }
                }
            }
            info.postlist[0] = 0;
            info.postlist[1] = 1 << rangebits;

            return(info);
        }
Example #3
0
        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);
        }
Example #4
0
 public abstract void pack(Info info, Object imap, csBuffer buffer);
Example #5
0
        // res0 (multistage, interleave, lattice)
        // returns the number of bits and *modifies a* to the remainder value
        internal int encodevs(float[] a, csBuffer b, int step, int addmul)
        {
            int best = besterror(a, step, addmul);

            return(encode(best, b));
        }
Example #6
0
        /*
         */

        // unpacks a codebook from the packet buffer into the codebook struct,
        // readies the codebook auxiliary structures for decode
        internal int unpack(csBuffer opb)
        {
            int i;

            //memset(s,0,sizeof(static_codebook));

            // make sure alignment is correct
            if (opb.read(24) != 0x564342)
            {
                //    goto _eofout;
                clear();
                return(-1);
            }

            // first the basic parameters
            dim     = opb.read(16);
            entries = opb.read(24);
            if (entries == -1)
            {
                //    goto _eofout;
                clear();
                return(-1);
            }

            // codeword ordering.... length ordered or unordered?
            switch (opb.read(1))
            {
            case 0:
                // unordered
                lengthlist = new int[entries];

                // allocated but unused entries?
                if (opb.read(1) != 0)
                {
                    // yes, unused entries

                    for (i = 0; i < entries; i++)
                    {
                        if (opb.read(1) != 0)
                        {
                            int num = opb.read(5);
                            if (num == -1)
                            {
                                //            goto _eofout;
                                clear();
                                return(-1);
                            }
                            lengthlist[i] = num + 1;
                        }
                        else
                        {
                            lengthlist[i] = 0;
                        }
                    }
                }
                else
                {
                    // all entries used; no tagging
                    for (i = 0; i < entries; i++)
                    {
                        int num = opb.read(5);
                        if (num == -1)
                        {
                            //          goto _eofout;
                            clear();
                            return(-1);
                        }
                        lengthlist[i] = num + 1;
                    }
                }
                break;

            case 1:
                // ordered
            {
                int length = opb.read(5) + 1;
                lengthlist = new int[entries];

                for (i = 0; i < entries;)
                {
                    int num = opb.read(ilog(entries - i));
                    if (num == -1)
                    {
                        //          goto _eofout;
                        clear();
                        return(-1);
                    }
                    for (int j = 0; j < num; j++, i++)
                    {
                        lengthlist[i] = length;
                    }
                    length++;
                }
            }
            break;

            default:
                // EOF
                return(-1);
            }

            // Do we have a mapping to unpack?
            switch ((maptype = opb.read(4)))
            {
            case 0:
                // no mapping
                break;

            case 1:
            case 2:
                // implicitly populated value mapping
                // explicitly populated value mapping
                q_min       = opb.read(32);
                q_delta     = opb.read(32);
                q_quant     = opb.read(4) + 1;
                q_sequencep = opb.read(1);

                {
                    int quantvals = 0;
                    switch (maptype)
                    {
                    case 1:
                        quantvals = maptype1_quantvals();
                        break;

                    case 2:
                        quantvals = entries * dim;
                        break;
                    }

                    // quantized values
                    quantlist = new int[quantvals];
                    for (i = 0; i < quantvals; i++)
                    {
                        quantlist[i] = opb.read(q_quant);
                    }
                    if (quantlist[quantvals - 1] == -1)
                    {
                        //        goto _eofout;
                        clear();
                        return(-1);
                    }
                }
                break;

            default:
                //    goto _eofout;
                clear();
                return(-1);
            }
            // all set
            return(0);
            //    _errout:
            //    _eofout:
            //    vorbis_staticbook_clear(s);
            //    return(-1);
        }
Example #7
0
        int pack_books(csBuffer opb)
        {
            Encoding AE = Encoding.UTF8;

            byte[] _vorbis_byt = AE.GetBytes(_vorbis);

            opb.write(0x05, 8);
            opb.write(_vorbis_byt);

            // books
            opb.write(books - 1, 8);
            for (int i = 0; i < books; i++)
            {
                if (book_param[i].pack(opb) != 0)
                {
                    //goto err_out;
                    return(-1);
                }
            }

            // times
            opb.write(times - 1, 6);
            for (int i = 0; i < times; i++)
            {
                opb.write(time_type[i], 16);
                FuncTime.time_P[time_type[i]].pack(this.time_param[i], opb);
            }

            // floors
            opb.write(floors - 1, 6);
            for (int i = 0; i < floors; i++)
            {
                opb.write(floor_type[i], 16);
                FuncFloor.floor_P[floor_type[i]].pack(floor_param[i], opb);
            }

            // residues
            opb.write(residues - 1, 6);
            for (int i = 0; i < residues; i++)
            {
                opb.write(residue_type[i], 16);
                FuncResidue.residue_P[residue_type[i]].pack(residue_param[i], opb);
            }

            // maps
            opb.write(maps - 1, 6);
            for (int i = 0; i < maps; i++)
            {
                opb.write(map_type[i], 16);
                FuncMapping.mapping_P[map_type[i]].pack(this, map_param[i], opb);
            }

            // modes
            opb.write(modes - 1, 6);
            for (int i = 0; i < modes; i++)
            {
                opb.write(mode_param[i].blockflag, 1);
                opb.write(mode_param[i].windowtype, 16);
                opb.write(mode_param[i].transformtype, 16);
                opb.write(mode_param[i].mapping, 8);
            }
            opb.write(1, 1);
            return(0);
            //err_out:
            //return(-1);
        }
Example #8
0
        override public Object unpack(Info vi, csBuffer opb)
        {
            // also responsible for range checking
            InfoMapping0 info = new InfoMapping0();

            // !!!!
            if (opb.read(1) != 0)
            {
                info.submaps = opb.read(4) + 1;
            }
            else
            {
                info.submaps = 1;
            }

            if (opb.read(1) != 0)
            {
                info.coupling_steps = opb.read(8) + 1;

                for (int i = 0; i < info.coupling_steps; i++)
                {
                    int testM = info.coupling_mag[i] = opb.read(ilog2(vi.channels));
                    int testA = info.coupling_ang[i] = opb.read(ilog2(vi.channels));

                    if (testM < 0 ||
                        testA < 0 ||
                        testM == testA ||
                        testM >= vi.channels ||
                        testA >= vi.channels)
                    {
                        //goto err_out;
                        info.free();
                        return(null);
                    }
                }
            }

            if (opb.read(2) > 0)
            {             /* 2,3:reserved */
                //goto err_out;
                info.free();
                return(null);
            }

            if (info.submaps > 1)
            {
                for (int i = 0; i < vi.channels; i++)
                {
                    info.chmuxlist[i] = opb.read(4);
                    if (info.chmuxlist[i] >= info.submaps)
                    {
                        //goto err_out;
                        info.free();
                        return(null);
                    }
                }
            }

            for (int i = 0; i < info.submaps; i++)
            {
                info.timesubmap[i] = opb.read(8);
                if (info.timesubmap[i] >= vi.times)
                {
                    //goto err_out;
                    info.free();
                    return(null);
                }
                info.floorsubmap[i] = opb.read(8);
                if (info.floorsubmap[i] >= vi.floors)
                {
                    //goto err_out;
                    info.free();
                    return(null);
                }
                info.residuesubmap[i] = opb.read(8);
                if (info.residuesubmap[i] >= vi.residues)
                {
                    //goto err_out;
                    info.free();
                    return(null);
                }
            }
            return(info);
            //err_out:
            //free_info(info);
            //return(NULL);
        }
Example #9
0
        // all of the real encoding details are here.  The modes, books,
        // everything
        int unpack_books(csBuffer opb)
        {
            //d* codebooks
            books = opb.read(8) + 1;

            if (book_param == null || book_param.Length != books)
            {
                book_param = new StaticCodeBook[books];
            }
            for (int i = 0; i < books; i++)
            {
                book_param[i] = new StaticCodeBook();
                if (book_param[i].unpack(opb) != 0)
                {
                    //goto err_out;
                    clear();
                    return(-1);
                }
            }

            // time backend settings
            times = opb.read(6) + 1;
            if (time_type == null || time_type.Length != times)
            {
                time_type = new int[times];
            }
            if (time_param == null || time_param.Length != times)
            {
                time_param = new Object[times];
            }
            for (int i = 0; i < times; i++)
            {
                time_type[i] = opb.read(16);
                if (time_type[i] < 0 || time_type[i] >= VI_TIMEB)
                {
                    //goto err_out;
                    clear();
                    return(-1);
                }
                time_param[i] = FuncTime.time_P[time_type[i]].unpack(this, opb);
                if (time_param[i] == null)
                {
                    //goto err_out;
                    clear();
                    return(-1);
                }
            }

            // floor backend settings
            floors = opb.read(6) + 1;
            if (floor_type == null || floor_type.Length != floors)
            {
                floor_type = new int[floors];
            }
            if (floor_param == null || floor_param.Length != floors)
            {
                floor_param = new Object[floors];
            }

            for (int i = 0; i < floors; i++)
            {
                floor_type[i] = opb.read(16);
                if (floor_type[i] < 0 || floor_type[i] >= VI_FLOORB)
                {
                    //goto err_out;
                    clear();
                    return(-1);
                }

                floor_param[i] = FuncFloor.floor_P[floor_type[i]].unpack(this, opb);
                if (floor_param[i] == null)
                {
                    //goto err_out;
                    clear();
                    return(-1);
                }
            }

            // residue backend settings
            residues = opb.read(6) + 1;

            if (residue_type == null || residue_type.Length != residues)
            {
                residue_type = new int[residues];
            }

            if (residue_param == null || residue_param.Length != residues)
            {
                residue_param = new Object[residues];
            }

            for (int i = 0; i < residues; i++)
            {
                residue_type[i] = opb.read(16);
                if (residue_type[i] < 0 || residue_type[i] >= VI_RESB)
                {
                    //	goto err_out;
                    clear();
                    return(-1);
                }
                residue_param[i] = FuncResidue.residue_P[residue_type[i]].unpack(this, opb);
                if (residue_param[i] == null)
                {
                    //	goto err_out;
                    clear();
                    return(-1);
                }
            }

            // map backend settings
            maps = opb.read(6) + 1;
            if (map_type == null || map_type.Length != maps)
            {
                map_type = new int[maps];
            }
            if (map_param == null || map_param.Length != maps)
            {
                map_param = new Object[maps];
            }
            for (int i = 0; i < maps; i++)
            {
                map_type[i] = opb.read(16);
                if (map_type[i] < 0 || map_type[i] >= VI_MAPB)
                {
                    //	goto err_out;
                    clear();
                    return(-1);
                }
                map_param[i] = FuncMapping.mapping_P[map_type[i]].unpack(this, opb);
                if (map_param[i] == null)
                {
                    //    goto err_out;
                    clear();
                    return(-1);
                }
            }

            // mode settings
            modes = opb.read(6) + 1;
            if (mode_param == null || mode_param.Length != modes)
            {
                mode_param = new InfoMode[modes];
            }
            for (int i = 0; i < modes; i++)
            {
                mode_param[i]               = new InfoMode();
                mode_param[i].blockflag     = opb.read(1);
                mode_param[i].windowtype    = opb.read(16);
                mode_param[i].transformtype = opb.read(16);
                mode_param[i].mapping       = opb.read(8);

                if ((mode_param[i].windowtype >= VI_WINDOWB) ||
                    (mode_param[i].transformtype >= VI_WINDOWB) ||
                    (mode_param[i].mapping >= maps))
                {
                    //      goto err_out;
                    clear();
                    return(-1);
                }
            }

            if (opb.read(1) != 1)
            {
                //goto err_out; // top level EOP check
                clear();
                return(-1);
            }

            return(0);
            // err_out:
            //  vorbis_info_clear(vi);
            //  return(-1);
        }
Example #10
0
        // The Vorbis header is in three packets; the initial small packet in
        // the first page that identifies basic parameters, a second packet
        // with bitstream comments and a third packet that holds the
        // codebook.

        public int synthesis_headerin(Comment vc, Packet op)
        {
            csBuffer opb = new csBuffer();

            if (op != null)
            {
                opb.readinit(op.packet_base, op.packet, op.bytes);

                // Which of the three types of header is this?
                // Also verify header-ness, vorbis
                {
                    byte[] buffer   = new byte[6];
                    int    packtype = opb.read(8);
                    //memset(buffer,0,6);
                    opb.read(buffer, 6);
                    if (buffer[0] != 'v' || buffer[1] != 'o' || buffer[2] != 'r' ||
                        buffer[3] != 'b' || buffer[4] != 'i' || buffer[5] != 's')
                    {
                        // not a vorbis header
                        return(-1);
                    }
                    switch (packtype)
                    {
                    case 0x01:                     // least significant *bit* is read first
                        if (op.b_o_s == 0)
                        {
                            // Not the initial packet
                            return(-1);
                        }
                        if (rate != 0)
                        {
                            // previously initialized info header
                            return(-1);
                        }
                        return(unpack_info(opb));

                    case 0x03:                     // least significant *bit* is read first
                        if (rate == 0)
                        {
                            // um... we didn't get the initial header
                            return(-1);
                        }
                        return(vc.unpack(opb));

                    case 0x05:                     // least significant *bit* is read first
                        if (rate == 0 || vc.vendor == null)
                        {
                            // um... we didn;t get the initial header or comments yet
                            return(-1);
                        }
                        return(unpack_books(opb));

                    default:
                        // Not a valid vorbis header type
                        //return(-1);
                        break;
                    }
                }
            }
            return(-1);
        }
Example #11
0
 override public Object unpack(Info vi, csBuffer opb)
 {
     return("");
 }
Example #12
0
 override public void pack(Object i, csBuffer opb)
 {
 }
Example #13
0
        void unpack_books(csBuffer opb)
        {
            books      = opb.read(8) + 1;
            book_param = new StaticCodeBook[books];
            for (int i = 0; i < books; i++)
            {
                book_param[i] = new StaticCodeBook();
                book_param[i].unpack(opb);
            }

            times = opb.read(6) + 1;
            for (int i = 0; i < times; i++)
            {
                opb.read(16);
            }

            floors      = opb.read(6) + 1;
            floor_funcs = new Floor1[floors];
            floor_param = new object[floors];
            for (int i = 0; i < floors; i++)
            {
                int type = opb.read(16);
                if (type != 1)
                {
                    throw new csorbisException("floor type must be 1");
                }
                floor_funcs[i] = new Floor1();
                floor_param[i] = floor_funcs[i].unpack(this, opb);
            }

            residues      = opb.read(6) + 1;
            residue_funcs = new FuncResidue[residues];
            residue_param = new object[residues];
            for (int i = 0; i < residues; i++)
            {
                int type = opb.read(16);
                if (type > 2)
                {
                    throw new csorbisException("residue type must be <= 2");
                }
                residue_funcs[i] = FuncResidue.make(type);
                residue_param[i] = residue_funcs[i].unpack(this, opb);
            }

            maps      = opb.read(6) + 1;
            map_funcs = new FuncMapping[maps];
            map_param = new object[maps];
            for (int i = 0; i < maps; i++)
            {
                int type = opb.read(16);
                if (type != 0)
                {
                    throw new csorbisException("mapping type must be 0");
                }
                map_funcs[i] = new Mapping0();
                map_param[i] = map_funcs[i].unpack(this, opb);
            }

            modes      = opb.read(6) + 1;
            mode_param = new InfoMode[modes];
            for (int i = 0; i < modes; i++)
            {
                mode_param[i]               = new InfoMode();
                mode_param[i].blockflag     = opb.read(1);
                mode_param[i].windowtype    = opb.read(16);
                mode_param[i].transformtype = opb.read(16);
                mode_param[i].mapping       = opb.read(8);
            }
            opb.read(1);
        }
Example #14
0
 public abstract void pack(Object i, csBuffer opb);
Example #15
0
 // returns the number of bits
 internal int encode(int a, csBuffer b)
 {
     b.write(codelist[a], c.lengthlist[a]);
     return(c.lengthlist[a]);
 }
Example #16
0
 public abstract Object unpack(Info vi, csBuffer opb);
Example #17
0
        // Also responsible for range checking
        public override object unpack(Info vi, csBuffer opb)
        {
            InfoMapping0 info = new InfoMapping0();

            if (opb.read(1) != 0)
            {
                info.submaps = opb.read(4) + 1;
            }
            else
            {
                info.submaps = 1;
            }

            if (opb.read(1) != 0)
            {
                info.coupling_steps = opb.read(8) + 1;

                for (int i = 0; i < info.coupling_steps; i++)
                {
                    int testM = info.coupling_mag[i] = opb.read(Util.ilog2(vi.Channels));
                    int testA = info.coupling_ang[i] = opb.read(Util.ilog2(vi.Channels));

                    if (testM < 0 ||
                        testA < 0 ||
                        testM == testA ||
                        testM >= vi.Channels ||
                        testA >= vi.Channels)
                    {
                        info.free();
                        return(null);
                    }
                }
            }

            if (opb.read(2) > 0)
            {
                /* 2,3:reserved */
                info.free();
                return(null);
            }

            if (info.submaps > 1)
            {
                for (int i = 0; i < vi.Channels; i++)
                {
                    info.chmuxlist[i] = opb.read(4);
                    if (info.chmuxlist[i] >= info.submaps)
                    {
                        info.free();
                        return(null);
                    }
                }
            }

            for (int i = 0; i < info.submaps; i++)
            {
                info.timesubmap[i] = opb.read(8);
                if (info.timesubmap[i] >= vi.times)
                {
                    info.free();
                    return(null);
                }

                info.floorsubmap[i] = opb.read(8);
                if (info.floorsubmap[i] >= vi.floors)
                {
                    info.free();
                    return(null);
                }

                info.residuesubmap[i] = opb.read(8);
                if (info.residuesubmap[i] >= vi.residues)
                {
                    info.free();
                    return(null);
                }
            }

            return(info);
        }