private static void ReadSpectra(BitReader reader, Channel channel) { int[] values = channel.SpectraValuesBuffer; Array.Clear(channel.QuantizedSpectra, 0, channel.QuantizedSpectra.Length); int maxHuffPrecision = Tables.MaxHuffPrecision(channel.Config.HighSampleRate); for (int i = 0; i < channel.CodedQuantUnits; i++) { int subbandCount = Tables.QuantUnitToCoeffCount[i]; int precision = channel.Precisions[i] + 1; if (precision <= maxHuffPrecision) { HuffmanCodebook huff = Tables.HuffmanSpectrum[channel.CodebookSet[i]][precision][Tables.QuantUnitToCodebookIndex[i]]; int groupCount = subbandCount >> huff.ValueCountPower; for (int j = 0; j < groupCount; j++) { values[j] = ReadHuffmanValue(huff, reader); } DecodeHuffmanValues(channel.QuantizedSpectra, Tables.QuantUnitToCoeffIndex[i], subbandCount, huff, values); } else { int subbandIndex = Tables.QuantUnitToCoeffIndex[i]; for (int j = subbandIndex; j < Tables.QuantUnitToCoeffIndex[i + 1]; j++) { channel.QuantizedSpectra[j] = reader.ReadSignedInt(precision); } } } }
private static void ReadVlcDeltaOffsetWithBaseline(BitReader reader, Channel channel, int[] baseline, int baselineLength) { int[] sf = channel.ScaleFactors; int baseValue = reader.ReadOffsetBinary(5, BitReader.OffsetBias.Negative); int bitLength = reader.ReadInt(2) + 1; HuffmanCodebook codebook = Tables.HuffmanScaleFactorsUnsigned[bitLength]; int unitCount = Math.Min(channel.Block.ExtensionUnit, baselineLength); sf[0] = reader.ReadInt(bitLength); for (int i = 1; i < unitCount; i++) { int delta = Unpack.ReadHuffmanValue(codebook, reader); sf[i] = (sf[i - 1] + delta) & (codebook.ValueMax - 1); } for (int i = 0; i < unitCount; i++) { sf[i] += baseValue + baseline[i]; } for (int i = unitCount; i < channel.Block.ExtensionUnit; i++) { sf[i] = reader.ReadInt(5); } }
public static int ReadHuffmanValue(HuffmanCodebook huff, BitReader reader, bool signed = false) { int code = reader.PeekInt(huff.MaxBitSize); byte value = huff.Lookup[code]; int bits = huff.Bits[value]; reader.Position += bits; return(signed ? Bit.SignExtend32(value, huff.ValueBits) : value); }
private static void ReadVlcDistanceToBaseline(BitReader reader, Channel channel, int[] baseline, int baselineLength) { int[] sf = channel.ScaleFactors; int bitLength = reader.ReadInt(2) + 2; HuffmanCodebook codebook = Tables.HuffmanScaleFactorsSigned[bitLength]; int unitCount = Math.Min(channel.Block.ExtensionUnit, baselineLength); for (int i = 0; i < unitCount; i++) { int distance = Unpack.ReadHuffmanValue(codebook, reader, true); sf[i] = (baseline[i] + distance) & 31; } for (int i = unitCount; i < channel.Block.ExtensionUnit; i++) { sf[i] = reader.ReadInt(5); } }
private static void ReadVlcDeltaOffset(BitReader reader, Channel channel) { int weightIndex = reader.ReadInt(3); byte[] weights = ScaleFactorWeights[weightIndex]; int[] sf = channel.ScaleFactors; int baseValue = reader.ReadInt(5); int bitLength = reader.ReadInt(2) + 3; HuffmanCodebook codebook = Tables.HuffmanScaleFactorsUnsigned[bitLength]; sf[0] = reader.ReadInt(bitLength); for (int i = 1; i < channel.Block.ExtensionUnit; i++) { int delta = Unpack.ReadHuffmanValue(codebook, reader); sf[i] = (sf[i - 1] + delta) & (codebook.ValueMax - 1); } for (int i = 0; i < channel.Block.ExtensionUnit; i++) { sf[i] += baseValue - weights[i]; } }
private static void DecodeHuffmanValues(int[] spectrum, int index, int bandCount, HuffmanCodebook huff, int[] values) { int valueCount = bandCount >> huff.ValueCountPower; int mask = (1 << huff.ValueBits) - 1; for (int i = 0; i < valueCount; i++) { int value = values[i]; for (int j = 0; j < huff.ValueCount; j++) { spectrum[index++] = Bit.SignExtend32(value & mask, huff.ValueBits); value >>= huff.ValueBits; } } }