Example #1
0
        void UnpackInfo(OggBitStream input)
        {
            Version = input.ReadInt32();
            if (Version != 0)
            {
                throw new InvalidDataException("Invalid Vorbis encoder version.");
            }
            Channels = input.ReadUInt8();
            Rate     = input.ReadInt32();

            BitrateUpper   = input.ReadInt32();
            BitrateNominal = input.ReadInt32();
            BitrateLower   = input.ReadInt32();

            CodecSetup.BlockSizes[0] = 1 << input.ReadBits(4);
            CodecSetup.BlockSizes[1] = 1 << input.ReadBits(4);

            if (input.ReadBits(1) != 1)
            {
                throw InvalidHeader();
            }
        }
Example #2
0
        internal void UnpackComment(OggBitStream input)
        {
            int vendor_len = input.ReadInt32();

            if (vendor_len < 0)
            {
                throw VorbisInfo.InvalidHeader();
            }
            var vendor = input.ReadBytes(vendor_len);

            int count = input.ReadInt32();

            if (count < 0)
            {
                throw VorbisInfo.InvalidHeader();
            }

            var comments = new List <byte[]> (count);

            for (int i = 0; i < count; ++i)
            {
                int len = input.ReadInt32();
                if (len < 0)
                {
                    throw VorbisInfo.InvalidHeader();
                }
                var bytes = input.ReadBytes(len);
                comments.Add(bytes);
            }
            if (input.ReadBits(1) != 1)
            {
                throw VorbisInfo.InvalidHeader();
            }

            this.Vendor   = vendor;
            this.Comments = comments;
        }
Example #3
0
        StaticCodebook StaticBookUnpack(OggBitStream input)
        {
            // make sure alignment is correct
            if (input.ReadBits(24) != 0x564342)
            {
                return(null);
            }

            var s = new StaticCodebook();

            // first the basic parameters
            s.dim     = input.ReadBits(16);
            s.entries = input.ReadBits(24);
            if (-1 == s.entries)
            {
                return(null);
            }

            if (iLog((uint)s.dim) + iLog((uint)s.entries) > 24)
            {
                return(null);
            }

            // codeword ordering.... length ordered or unordered?
            switch (input.ReadBits(1))
            {
            case 0:
                // allocated but unused entries?
                int unused = input.ReadBits(1);
                // unordered
                s.lengthlist = new byte[s.entries];

                // allocated but unused entries?
                if (unused > 0)
                {
                    // yes, unused entries
                    for (int i = 0; i < s.entries; ++i)
                    {
                        if (input.ReadBits(1) > 0)
                        {
                            int num = input.ReadBits(5);
                            if (-1 == num)
                            {
                                return(null);
                            }
                            s.lengthlist[i] = (byte)(num + 1);
                        }
                        else
                        {
                            s.lengthlist[i] = 0;
                        }
                    }
                }
                else
                {
                    // all entries used; no tagging
                    for (int i = 0; i < s.entries; ++i)
                    {
                        int num = input.ReadBits(5);
                        if (-1 == num)
                        {
                            return(null);
                        }
                        s.lengthlist[i] = (byte)(num + 1);
                    }
                }
                break;

            case 1: // ordered
                int length = input.ReadBits(5) + 1;
                if (0 == length)
                {
                    return(null);
                }
                s.lengthlist = new byte[s.entries];

                for (int i = 0; i < s.entries;)
                {
                    int num = input.ReadBits(iLog((uint)(s.entries - i)));
                    if (-1 == num || length > 32 || num > s.entries - i ||
                        (num > 0 && ((num - 1) >> (length - 1)) > 1))
                    {
                        return(null);
                    }
                    for (int j = 0; j < num; ++j, ++i)
                    {
                        s.lengthlist[i] = (byte)length;
                    }
                    length++;
                }
                break;

            default:
                return(null);
            }

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

            case 1:
            case 2:
                // implicitly populated value mapping
                // explicitly populated value mapping

                s.q_min       = input.ReadInt32();
                s.q_delta     = input.ReadInt32();
                s.q_quant     = input.ReadBits(4) + 1;
                s.q_sequencep = input.ReadBits(1);
                if (-1 == s.q_sequencep)
                {
                    return(null);
                }
                int quantvals = 0;
                switch (s.maptype)
                {
                case 1:
                    quantvals = s.dim == 0 ? 0 : s.Maptype1Quantvals();
                    break;

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

                // quantized values
                s.quantlist = new int[quantvals];
                for (int i = 0; i < quantvals; ++i)
                {
                    s.quantlist[i] = input.ReadBits(s.q_quant);
                }

                if (quantvals > 0 && s.quantlist[quantvals - 1] == -1)
                {
                    return(null);
                }
                break;

            default: // EOF
                return(null);
            }

            // all set
            return(s);
        }