private ResultCode DecodeInterleavedInternal(BinaryReader input, out short[] outPcmData, long outputSize, out uint outConsumed, out int outSamples) { outPcmData = null; outConsumed = 0; outSamples = 0; long streamSize = input.BaseStream.Length; if (streamSize < Marshal.SizeOf <OpusPacketHeader>()) { return(ResultCode.OpusInvalidInput); } OpusPacketHeader header = OpusPacketHeader.FromStream(input); uint totalSize = header.length + (uint)Marshal.SizeOf <OpusPacketHeader>(); if (totalSize > streamSize) { return(ResultCode.OpusInvalidInput); } byte[] opusData = input.ReadBytes((int)header.length); ResultCode result = GetPacketNumSamples(out int numSamples, opusData); if (result == ResultCode.Success) { if ((uint)numSamples * (uint)_channelsCount * sizeof(short) > outputSize) { return(ResultCode.OpusInvalidInput); } outPcmData = new short[numSamples * _channelsCount]; if (_reset) { _reset = false; _decoder.ResetState(); } try { outSamples = _decoder.Decode(opusData, 0, opusData.Length, outPcmData, 0, outPcmData.Length / _channelsCount); outConsumed = totalSize; } catch (OpusException) { // TODO: as OpusException doesn't provide us the exact error code, this is kind of inaccurate in some cases... return(ResultCode.OpusInvalidInput); } } return(ResultCode.Success); }
public static ResultCode DecodeInterleaved( this IDecoder decoder, bool reset, ReadOnlySpan <byte> input, out short[] outPcmData, ulong outputSize, out uint outConsumed, out int outSamples) { outPcmData = null; outConsumed = 0; outSamples = 0; int streamSize = input.Length; if (streamSize < Unsafe.SizeOf <OpusPacketHeader>()) { return(ResultCode.OpusInvalidInput); } OpusPacketHeader header = OpusPacketHeader.FromSpan(input); int headerSize = Unsafe.SizeOf <OpusPacketHeader>(); uint totalSize = header.length + (uint)headerSize; if (totalSize > streamSize) { return(ResultCode.OpusInvalidInput); } byte[] opusData = input.Slice(headerSize, (int)header.length).ToArray(); ResultCode result = decoder.GetPacketNumSamples(out int numSamples, opusData); if (result == ResultCode.Success) { if ((uint)numSamples * (uint)decoder.ChannelsCount * sizeof(short) > outputSize) { return(ResultCode.OpusInvalidInput); } outPcmData = new short[numSamples * decoder.ChannelsCount]; if (reset) { decoder.ResetState(); } try { outSamples = decoder.Decode(opusData, 0, opusData.Length, outPcmData, 0, outPcmData.Length / decoder.ChannelsCount); outConsumed = totalSize; } catch (OpusException) { // TODO: as OpusException doesn't provide us the exact error code, this is kind of inaccurate in some cases... return(ResultCode.OpusInvalidInput); } } return(ResultCode.Success); }