protected override bool WriteVectors(VorbisCodebook codebook, DataPacket packet, float[][] residue, int channel, int offset, int partitionSize) { var chPtr = 0; offset /= _channels; for (var c = 0; c < partitionSize;) { var entry = codebook.DecodeScalar(packet); if (entry == -1) { return(true); } for (var d = 0; d < codebook.Dimensions; d++, c++) { residue[chPtr][offset] += codebook[entry, d]; if (++chPtr == _channels) { chPtr = 0; offset++; } } } return(false); }
internal static VorbisCodebook Init(VorbisStreamDecoder vorbis, DataPacket packet, int number) { var temp = new VorbisCodebook(); temp.BookNum = number; temp.Init(packet); return(temp); }
protected override bool WriteVectors(VorbisCodebook codebook, DataPacket packet, float[][] residue, int channel, int offset, int partitionSize) { var res = residue[channel]; for (var i = 0; i < partitionSize;) { var entry = codebook.DecodeScalar(packet); if (entry == -1) { return(true); } for (var j = 0; j < codebook.Dimensions; i++, j++) { res[offset + i] += codebook[entry, j]; } } return(false); }
protected virtual bool WriteVectors(VorbisCodebook codebook, DataPacket packet, float[][] residue, int channel, int offset, int partitionSize) { var res = residue[channel]; var step = partitionSize / codebook.Dimensions; for (var i = 0; i < step; i++) { if ((_entryCache[i] = codebook.DecodeScalar(packet)) == -1) { return(true); } } for (var i = 0; i < codebook.Dimensions; i++) { for (var j = 0; j < step; j++, offset++) { res[offset] += codebook[_entryCache[j], i]; } } return(false); }
protected override void Init(DataPacket packet) { _partitionClass = new int[(int)packet.ReadBits(5)]; for (var i = 0; i < _partitionClass.Length; i++) { _partitionClass[i] = (int)packet.ReadBits(4); } var maximum_class = _partitionClass.Max(); _classDimensions = new int[maximum_class + 1]; _classSubclasses = new int[maximum_class + 1]; _classMasterbooks = new VorbisCodebook[maximum_class + 1]; _classMasterBookIndex = new int[maximum_class + 1]; _subclassBooks = new VorbisCodebook[maximum_class + 1][]; _subclassBookIndex = new int[maximum_class + 1][]; for (var i = 0; i <= maximum_class; i++) { _classDimensions[i] = (int)packet.ReadBits(3) + 1; _classSubclasses[i] = (int)packet.ReadBits(2); if (_classSubclasses[i] > 0) { _classMasterBookIndex[i] = (int)packet.ReadBits(8); _classMasterbooks[i] = _vorbis.Books[_classMasterBookIndex[i]]; } _subclassBooks[i] = new VorbisCodebook[1 << _classSubclasses[i]]; _subclassBookIndex[i] = new int[_subclassBooks[i].Length]; for (var j = 0; j < _subclassBooks[i].Length; j++) { var bookNum = (int)packet.ReadBits(8) - 1; if (bookNum >= 0) { _subclassBooks[i][j] = _vorbis.Books[bookNum]; } _subclassBookIndex[i][j] = bookNum; } } _multiplier = (int)packet.ReadBits(2); _range = _rangeLookup[_multiplier]; _yBits = _yBitsLookup[_multiplier]; ++_multiplier; var rangeBits = (int)packet.ReadBits(4); var xList = new List <int>(); xList.Add(0); xList.Add(1 << rangeBits); for (var i = 0; i < _partitionClass.Length; i++) { var classNum = _partitionClass[i]; for (var j = 0; j < _classDimensions[classNum]; j++) { xList.Add((int)packet.ReadBits(rangeBits)); } } _xList = xList.ToArray(); // precalc the low and high neighbors (and init the sort table) _lNeigh = new int[xList.Count]; _hNeigh = new int[xList.Count]; _sortIdx = new int[xList.Count]; _sortIdx[0] = 0; _sortIdx[1] = 1; for (var i = 2; i < _lNeigh.Length; i++) { _lNeigh[i] = 0; _hNeigh[i] = 1; _sortIdx[i] = i; for (var j = 2; j < i; j++) { var temp = _xList[j]; if (temp < _xList[i]) { if (temp > _xList[_lNeigh[i]]) { _lNeigh[i] = j; } } else { if (temp < _xList[_hNeigh[i]]) { _hNeigh[i] = j; } } } } // precalc the sort table for (var i = 0; i < _sortIdx.Length - 1; i++) { for (var j = i + 1; j < _sortIdx.Length; j++) { if (_xList[i] == _xList[j]) { throw new InvalidDataException(); } if (_xList[_sortIdx[i]] > _xList[_sortIdx[j]]) { // swap the sort indexes var temp = _sortIdx[i]; _sortIdx[i] = _sortIdx[j]; _sortIdx[j] = temp; } } } // pre-create our packet data instances _reusablePacketData = new PacketData1[_vorbis._channels]; for (var i = 0; i < _reusablePacketData.Length; i++) { _reusablePacketData[i] = new PacketData1(); } }
private bool LoadBooks(DataPacket packet) { if (!packet.ReadBytes(7).SequenceEqual(new byte[] { 0x05, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 })) { return(false); } if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber))) { _pagesSeen.Add(_lastPageSeen); } var bits = packet.BitsRead; _glueBits += packet.BitsRead; // get books Books = new VorbisCodebook[packet.ReadByte() + 1]; for (var i = 0; i < Books.Length; i++) { Books[i] = VorbisCodebook.Init(this, packet, i); } _bookBits += packet.BitsRead - bits; bits = packet.BitsRead; // get times Times = new VorbisTime[(int)packet.ReadBits(6) + 1]; for (var i = 0; i < Times.Length; i++) { Times[i] = VorbisTime.Init(this, packet); } _timeHdrBits += packet.BitsRead - bits; bits = packet.BitsRead; // get floor Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1]; for (var i = 0; i < Floors.Length; i++) { Floors[i] = VorbisFloor.Init(this, packet); } _floorHdrBits += packet.BitsRead - bits; bits = packet.BitsRead; // get residue Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1]; for (var i = 0; i < Residues.Length; i++) { Residues[i] = VorbisResidue.Init(this, packet); } _resHdrBits += packet.BitsRead - bits; bits = packet.BitsRead; // get map Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1]; for (var i = 0; i < Maps.Length; i++) { Maps[i] = VorbisMapping.Init(this, packet); } _mapHdrBits += packet.BitsRead - bits; bits = packet.BitsRead; // get mode settings Modes = new VorbisMode[(int)packet.ReadBits(6) + 1]; for (var i = 0; i < Modes.Length; i++) { Modes[i] = VorbisMode.Init(this, packet); } _modeHdrBits += packet.BitsRead - bits; // check the framing bit if (!packet.ReadBit()) { throw new InvalidDataException(); } ++_glueBits; _wasteHdrBits += 8 * packet.Length - packet.BitsRead; _modeFieldBits = Utils.ilog(Modes.Length - 1); return(true); }
protected override void Init(DataPacket packet) { // this is pretty well stolen directly from libvorbis... BSD license _begin = (int)packet.ReadBits(24); _end = (int)packet.ReadBits(24); _partitionSize = (int)packet.ReadBits(24) + 1; _classifications = (int)packet.ReadBits(6) + 1; _classBook = _vorbis.Books[(int)packet.ReadBits(8)]; _cascade = new int[_classifications]; var acc = 0; for (var i = 0; i < _classifications; i++) { var low_bits = (int)packet.ReadBits(3); if (packet.ReadBit()) { _cascade[i] = (int)packet.ReadBits(5) << 3 | low_bits; } else { _cascade[i] = low_bits; } acc += icount(_cascade[i]); } var bookNums = new int[acc]; for (var i = 0; i < acc; i++) { bookNums[i] = (int)packet.ReadBits(8); if (_vorbis.Books[bookNums[i]].MapType == 0) { throw new InvalidDataException(); } } var entries = _classBook.Entries; var dim = _classBook.Dimensions; var partvals = 1; while (dim > 0) { partvals *= _classifications; if (partvals > entries) { throw new InvalidDataException(); } --dim; } // now the lookups dim = _classBook.Dimensions; _books = new VorbisCodebook[_classifications][]; acc = 0; var maxstage = 0; int stages; for (var j = 0; j < _classifications; j++) { stages = Utils.ilog(_cascade[j]); _books[j] = new VorbisCodebook[stages]; if (stages > 0) { maxstage = Math.Max(maxstage, stages); for (var k = 0; k < stages; k++) { if ((_cascade[j] & (1 << k)) > 0) { _books[j][k] = _vorbis.Books[bookNums[acc++]]; } } } } _maxStages = maxstage; _decodeMap = new int[partvals][]; for (var j = 0; j < partvals; j++) { var val = j; var mult = partvals / _classifications; _decodeMap[j] = new int[_classBook.Dimensions]; for (var k = 0; k < _classBook.Dimensions; k++) { var deco = val / mult; val -= deco * mult; mult /= _classifications; _decodeMap[j][k] = deco; } } _entryCache = new int[_partitionSize]; _partWordCache = new int[_vorbis._channels][][]; var maxPartWords = ((_end - _begin) / _partitionSize + _classBook.Dimensions - 1) / _classBook.Dimensions; for (var ch = 0; ch < _vorbis._channels; ch++) { _partWordCache[ch] = new int[maxPartWords][]; } }