Exemplo n.º 1
0
        // https://xiph.org/vorbis/doc/libvorbis/vorbis_synthesis_headerin.html
        public void SynthesisHeaderin(VorbisComment vc, OggPacket op)
        {
            using (var input = new OggBitStream(op))
            {
                int packtype = input.ReadUInt8();
                var buf      = input.ReadBytes(6);
                if (!buf.AsciiEqual("vorbis"))
                {
                    throw new InvalidDataException("Not an Ogg/Vorbis stream.");
                }
                switch (packtype)
                {
                case 1:
                    if (!op.BoS)
                    {
                        throw InvalidHeader();
                    }
                    if (Rate != 0)
                    {
                        throw InvalidHeader();
                    }
                    UnpackInfo(input);
                    break;

                case 3:
                    if (0 == Rate)
                    {
                        throw InvalidHeader();
                    }
                    vc.UnpackComment(input);
                    break;

                case 5:
                    if (0 == Rate || null == vc.Vendor)
                    {
                        throw InvalidHeader();
                    }
                    UnpackBooks(input);
                    break;

                default:
                    throw InvalidHeader();
                }
            }
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
0
        void UnpackBooks(OggBitStream input)
        {
            // codebooks
            CodecSetup.Books = input.ReadUInt8() + 1;
            if (CodecSetup.Books <= 0)
            {
                throw InvalidHeader();
            }

            for (int i = 0; i < CodecSetup.Books; ++i)
            {
                var param = StaticBookUnpack(input);
                if (null == param)
                {
                    throw InvalidHeader();
                }
                CodecSetup.BookParam[i] = param;
            }

            // time backend settings; hooks are unused
            int times = input.ReadBits(6) + 1;

            if (times <= 0)
            {
                throw InvalidHeader();
            }
            for (int i = 0; i < times; ++i)
            {
                int test = input.ReadBits(16);
                if (test < 0 || test >= TimeB)
                {
                    throw InvalidHeader();
                }
            }

            // floor backend settings
            CodecSetup.Floors = input.ReadBits(6) + 1;
            if (CodecSetup.Floors <= 0)
            {
                throw InvalidHeader();
            }
            for (int i = 0; i < CodecSetup.Floors; i++)
            {
                int floor_type = input.ReadBits(16);
                if (floor_type < 0 || floor_type >= FloorB)
                {
                    throw InvalidHeader();
                }
                CodecSetup.FloorType[i] = floor_type;
                var param = FloorMethods[floor_type] (input);
                if (null == param)
                {
                    throw InvalidHeader();
                }
                CodecSetup.FloorParam[i] = param;
            }

            // residue backend settings
            CodecSetup.Residues = input.ReadBits(6) + 1;
            if (CodecSetup.Residues <= 0)
            {
                throw InvalidHeader();
            }
            for (int i = 0; i < CodecSetup.Residues; ++i)
            {
                int residue_type = input.ReadBits(16);
                if (residue_type < 0 || residue_type >= ResB)
                {
                    throw InvalidHeader();
                }
                CodecSetup.ResidueType[i] = residue_type;
                var param = UnpackResidue(input);
                if (null == param)
                {
                    throw InvalidHeader();
                }
                CodecSetup.ResidueParam[i] = param;
            }

            // map backend settings
            CodecSetup.Maps = input.ReadBits(6) + 1;
            if (CodecSetup.Maps <= 0)
            {
                throw InvalidHeader();
            }
            for (int i = 0; i < CodecSetup.Maps; ++i)
            {
                int map_type = input.ReadBits(16);
                if (map_type < 0 || map_type >= MapB)
                {
                    throw InvalidHeader();
                }
                CodecSetup.MapType[i] = map_type;
                var param = UnpackMapping(input);
                if (null == param)
                {
                    throw InvalidHeader();
                }
                CodecSetup.MapParam[i] = param;
            }

            // mode settings
            CodecSetup.Modes = input.ReadBits(6) + 1;
            if (CodecSetup.Modes <= 0)
            {
                throw InvalidHeader();
            }
            for (int i = 0; i < CodecSetup.Modes; ++i)
            {
                CodecSetup.ModeParam[i].BlockFlag     = input.ReadBits(1);
                CodecSetup.ModeParam[i].WindowType    = input.ReadBits(16);
                CodecSetup.ModeParam[i].TransformType = input.ReadBits(16);
                CodecSetup.ModeParam[i].Mapping       = input.ReadBits(8);

                if (CodecSetup.ModeParam[i].WindowType >= WindowB ||
                    CodecSetup.ModeParam[i].TransformType >= WindowB ||
                    CodecSetup.ModeParam[i].Mapping >= CodecSetup.Maps ||
                    CodecSetup.ModeParam[i].Mapping < 0)
                {
                    throw InvalidHeader();
                }
            }
            if (input.ReadBits(1) != 1)
            {
                throw InvalidHeader();
            }
        }