private static void WriteSpectra(BitWriter writer, CriHcaChannel channel, int subFrame) { for (int i = 0; i < channel.CodedScaleFactorCount; i++) { int resolution = channel.Resolution[i]; int quantizedSpectra = channel.QuantizedSpectra[subFrame][i]; if (resolution == 0) { continue; } if (resolution < 8) { int bits = CriHcaTables.QuantizeSpectrumBits[resolution][quantizedSpectra + 8]; writer.Write(CriHcaTables.QuantizeSpectrumValue[resolution][quantizedSpectra + 8], bits); } else if (resolution < 16) { int bits = CriHcaTables.QuantizedSpectrumMaxBits[resolution] - 1; writer.Write(Math.Abs(quantizedSpectra), bits); if (quantizedSpectra != 0) { writer.Write(quantizedSpectra > 0 ? 0 : 1, 1); } } } }
private static void CalculateGain(CriHcaChannel channel) { for (int i = 0; i < channel.CodedScaleFactorCount; i++) { channel.Gain[i] = DequantizerScalingTable[channel.ScaleFactors[i]] * QuantizerStepSize[channel.Resolution[i]]; } }
public CriHcaFrame(HcaInfo hca) { Hca = hca; ChannelType[] channelTypes = GetChannelTypes(hca); Channels = new CriHcaChannel[hca.ChannelCount]; for (int i = 0; i < Channels.Length; i++) { Channels[i] = new CriHcaChannel { Type = channelTypes[i], CodedScaleFactorCount = channelTypes[i] == StereoSecondary ? hca.BaseBandCount : hca.BaseBandCount + hca.StereoBandCount }; } AthCurve = hca.UseAthCurve ? ScaleAthCurve(hca.SampleRate) : new byte[SamplesPerSubFrame]; }
private static void CalculateOptimalDeltaLength(CriHcaChannel channel) { bool emptyChannel = true; for (int i = 0; i < channel.CodedScaleFactorCount; i++) { if (channel.ScaleFactors[i] != 0) { emptyChannel = false; break; } } if (emptyChannel) { channel.HeaderLengthBits = 3; channel.ScaleFactorDeltaBits = 0; return; } int minDeltaBits = 6; int minLength = 3 + 6 * channel.CodedScaleFactorCount; for (int deltaBits = 1; deltaBits < 6; deltaBits++) { int maxDelta = (1 << (deltaBits - 1)) - 1; int length = 3 + 6; for (int band = 1; band < channel.CodedScaleFactorCount; band++) { int delta = channel.ScaleFactors[band] - channel.ScaleFactors[band - 1]; length += Math.Abs(delta) > maxDelta ? deltaBits + 6 : deltaBits; } if (length < minLength) { minLength = length; minDeltaBits = deltaBits; } } channel.HeaderLengthBits = minLength; channel.ScaleFactorDeltaBits = minDeltaBits; }
private static bool ReadScaleFactors(CriHcaChannel channel, BitReader reader) { channel.ScaleFactorDeltaBits = reader.ReadInt(3); if (channel.ScaleFactorDeltaBits == 0) { Array.Clear(channel.ScaleFactors, 0, channel.ScaleFactors.Length); return(true); } if (channel.ScaleFactorDeltaBits >= 6) { for (int i = 0; i < channel.CodedScaleFactorCount; i++) { channel.ScaleFactors[i] = reader.ReadInt(6); } return(true); } return(DeltaDecode(reader, channel.ScaleFactorDeltaBits, 6, channel.CodedScaleFactorCount, channel.ScaleFactors)); }
private static void WriteScaleFactors(BitWriter writer, CriHcaChannel channel) { int deltaBits = channel.ScaleFactorDeltaBits; var scales = channel.ScaleFactors; writer.Write(deltaBits, 3); if (deltaBits == 0) { return; } if (deltaBits == 6) { for (int i = 0; i < channel.CodedScaleFactorCount; i++) { writer.Write(scales[i], 6); } return; } writer.Write(scales[0], 6); int maxDelta = (1 << (deltaBits - 1)) - 1; int escapeValue = (1 << deltaBits) - 1; for (int i = 1; i < channel.CodedScaleFactorCount; i++) { int delta = scales[i] - scales[i - 1]; if (Math.Abs(delta) > maxDelta) { writer.Write(escapeValue, deltaBits); writer.Write(scales[i], 6); } else { writer.Write(maxDelta + delta, deltaBits); } } }