public int BlockSize(Packet Packet) { //codec_setup_info var Buffer = new NVorbis.Ogg.BBuffer(); int mode; Buffer.ReadInit(Packet.packet_base, Packet.packet, Packet.bytes); /* Check the packet type */ if (Buffer.Read(1) != 0) { /* Oops. This is not an audio data packet */ return(OV_ENOTAUDIO); } { int modebits = 0; uint v = (uint)Modes; while (v > 1) { modebits++; v >>= 1; } /* read our mode and pre/post windowsize */ mode = Buffer.Read(modebits); } if (mode == -1) { return(OV_EBADPACKET); } return(blocksizes[ModeParam[mode].blockflag]); }
// 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 SynthesisHeaderIn(Comment Comment, Packet Packet) { var Buffer = new NVorbis.Ogg.BBuffer(); if (Packet != null) { Buffer.ReadInit(Packet.packet_base, Packet.packet, Packet.bytes); // Which of the three types of header is this? // Also verify header-ness, vorbis { byte[] buffer = new byte[6]; int packtype = Buffer.Read(8); Buffer.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 (Packet.b_o_s == 0) { // Not the initial packet return(-1); } if (Rate != 0) { // previously initialized info header return(-1); } return(UnpackInfo(Buffer)); case 0x03: // least significant *bit* is read first if (Rate == 0) { // um... we didn't get the initial header return(-1); } return(Comment.unpack(Buffer)); case 0x05: // least significant *bit* is read first if (Rate == 0 || Comment.vendor == null) { // um... we didn;t get the initial header or comments yet return(-1); } return(UnpackBooks(Buffer)); default: // Not a valid vorbis header type //return(-1); break; } } } return(-1); }
int PackBooks(NVorbis.Ogg.BBuffer Buffer) { Buffer.Write(0x05, 8); Buffer.Write(_vorbis); // books Buffer.Write(Books - 1, 8); for (int i = 0; i < Books; i++) { if (BookParam[i].pack(Buffer) != 0) { //goto err_out; return(-1); } } // times Buffer.Write(Times - 1, 6); for (int i = 0; i < Times; i++) { Buffer.Write(TimeType[i], 16); FuncTime.time_P[TimeType[i]].pack(this.TimeParam[i], Buffer); } // floors Buffer.Write(floors - 1, 6); for (int i = 0; i < floors; i++) { Buffer.Write(FloorType[i], 16); FuncFloor.floor_P[FloorType[i]].pack(FloorParam[i], Buffer); } // residues Buffer.Write(residues - 1, 6); for (int i = 0; i < residues; i++) { Buffer.Write(residue_type[i], 16); FuncResidue.residue_P[residue_type[i]].pack(ResidueParam[i], Buffer); } // maps Buffer.Write(maps - 1, 6); for (int i = 0; i < maps; i++) { Buffer.Write(map_type[i], 16); FuncMapping.mapping_P[map_type[i]].pack(this, MapParam[i], Buffer); } // modes Buffer.Write(Modes - 1, 6); for (int i = 0; i < Modes; i++) { Buffer.Write(ModeParam[i].blockflag, 1); Buffer.Write(ModeParam[i].windowtype, 16); Buffer.Write(ModeParam[i].transformtype, 16); Buffer.Write(ModeParam[i].mapping, 8); } Buffer.Write(1, 1); return(0); }
/// <summary> /// Header packing/unpacking /// </summary> /// <param name="Buffer"></param> /// <returns></returns> int UnpackInfo(NVorbis.Ogg.BBuffer Buffer) { version = Buffer.Read(32); if (version != 0) { return(-1); } Channels = Buffer.Read(8); Rate = Buffer.Read(32); bitrate_upper = Buffer.Read(32); bitrate_nominal = Buffer.Read(32); bitrate_lower = Buffer.Read(32); blocksizes[0] = 1 << Buffer.Read(4); blocksizes[1] = 1 << Buffer.Read(4); if ((Rate < 1) || (Channels < 1) || (blocksizes[0] < 8) || (blocksizes[1] < blocksizes[0]) || (Buffer.Read(1) != 1)) { Clear(); return(-1); } return(0); }
// pack side int PackInfo(NVorbis.Ogg.BBuffer Buffer) { // preamble Buffer.Write(0x01, 8); Buffer.Write(_vorbis); // basic information about the stream Buffer.Write(0x00, 32); Buffer.Write(Channels, 8); Buffer.Write(Rate, 32); Buffer.Write(bitrate_upper, 32); Buffer.Write(bitrate_nominal, 32); Buffer.Write(bitrate_lower, 32); Buffer.Write(Util.ilog2(blocksizes[0]), 4); Buffer.Write(Util.ilog2(blocksizes[1]), 4); Buffer.Write(1, 1); return(0); }
/// <summary> /// All of the real encoding details are here. The modes, books, everything. /// </summary> /// <param name="Buffer"></param> /// <returns></returns> int UnpackBooks(NVorbis.Ogg.BBuffer Buffer) { Books = Buffer.Read(8) + 1; if (BookParam == null || BookParam.Length != Books) { BookParam = new StaticCodeBook[Books]; } for (int i = 0; i < Books; i++) { BookParam[i] = new StaticCodeBook(); if (BookParam[i].unpack(Buffer) != 0) { Clear(); return(-1); } } // time backend settings Times = Buffer.Read(6) + 1; if (TimeType == null || TimeType.Length != Times) { TimeType = new int[Times]; } if (TimeParam == null || TimeParam.Length != Times) { TimeParam = new Object[Times]; } for (int i = 0; i < Times; i++) { TimeType[i] = Buffer.Read(16); if (TimeType[i] < 0 || TimeType[i] >= VI_TIMEB) { Clear(); return(-1); } TimeParam[i] = FuncTime.time_P[TimeType[i]].unpack(this, Buffer); if (TimeParam[i] == null) { Clear(); return(-1); } } // floor backend settings floors = Buffer.Read(6) + 1; if (FloorType == null || FloorType.Length != floors) { FloorType = new int[floors]; } if (FloorParam == null || FloorParam.Length != floors) { FloorParam = new Object[floors]; } for (int i = 0; i < floors; i++) { FloorType[i] = Buffer.Read(16); if (FloorType[i] < 0 || FloorType[i] >= VI_FLOORB) { Clear(); return(-1); } FloorParam[i] = FuncFloor.floor_P[FloorType[i]].unpack(this, Buffer); if (FloorParam[i] == null) { Clear(); return(-1); } } // residue backend settings residues = Buffer.Read(6) + 1; if (residue_type == null || residue_type.Length != residues) { residue_type = new int[residues]; } if (ResidueParam == null || ResidueParam.Length != residues) { ResidueParam = new Object[residues]; } for (int i = 0; i < residues; i++) { residue_type[i] = Buffer.Read(16); if (residue_type[i] < 0 || residue_type[i] >= VI_RESB) { Clear(); return(-1); } ResidueParam[i] = FuncResidue.residue_P[residue_type[i]].unpack(this, Buffer); if (ResidueParam[i] == null) { Clear(); return(-1); } } // map backend settings maps = Buffer.Read(6) + 1; if (map_type == null || map_type.Length != maps) { map_type = new int[maps]; } if (MapParam == null || MapParam.Length != maps) { MapParam = new Object[maps]; } for (int i = 0; i < maps; i++) { map_type[i] = Buffer.Read(16); if (map_type[i] < 0 || map_type[i] >= VI_MAPB) { Clear(); return(-1); } MapParam[i] = FuncMapping.mapping_P[map_type[i]].unpack(this, Buffer); if (MapParam[i] == null) { Clear(); return(-1); } } // mode settings Modes = Buffer.Read(6) + 1; if (ModeParam == null || ModeParam.Length != Modes) { ModeParam = new InfoMode[Modes]; } for (int i = 0; i < Modes; i++) { ModeParam[i] = new InfoMode(); ModeParam[i].blockflag = Buffer.Read(1); ModeParam[i].windowtype = Buffer.Read(16); ModeParam[i].transformtype = Buffer.Read(16); ModeParam[i].mapping = Buffer.Read(8); if ((ModeParam[i].windowtype >= VI_WINDOWB) || (ModeParam[i].transformtype >= VI_WINDOWB) || (ModeParam[i].mapping >= maps)) { Clear(); return(-1); } } if (Buffer.Read(1) != 1) { Clear(); return(-1); } return(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 SynthesisHeaderIn(Comment Comment, Packet Packet) { var Buffer = new NVorbis.Ogg.BBuffer(); if (Packet != null) { Buffer.ReadInit(Packet.packet_base, Packet.packet, Packet.bytes); // Which of the three types of header is this? // Also verify header-ness, vorbis { byte[] buffer = new byte[6]; int packtype = Buffer.Read(8); Buffer.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 (Packet.b_o_s == 0) { // Not the initial packet return (-1); } if (Rate != 0) { // previously initialized info header return (-1); } return (UnpackInfo(Buffer)); case 0x03: // least significant *bit* is read first if (Rate == 0) { // um... we didn't get the initial header return (-1); } return (Comment.unpack(Buffer)); case 0x05: // least significant *bit* is read first if (Rate == 0 || Comment.vendor == null) { // um... we didn;t get the initial header or comments yet return (-1); } return (UnpackBooks(Buffer)); default: // Not a valid vorbis header type //return(-1); break; } } } return (-1); }
public int BlockSize(Packet Packet) { //codec_setup_info var Buffer = new NVorbis.Ogg.BBuffer(); int mode; Buffer.ReadInit(Packet.packet_base, Packet.packet, Packet.bytes); /* Check the packet type */ if (Buffer.Read(1) != 0) { /* Oops. This is not an audio data packet */ return (OV_ENOTAUDIO); } { int modebits = 0; uint v = (uint)Modes; while (v > 1) { modebits++; v >>= 1; } /* read our mode and pre/post windowsize */ mode = Buffer.Read(modebits); } if (mode == -1) return (OV_EBADPACKET); return (blocksizes[ModeParam[mode].blockflag]); }