| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using Concentus;
- using Concentus.Enums;
- using Concentus.Structs;
- using Ryujinx.HLE.HOS.Services.Audio.Types;
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
- {
- class IHardwareOpusDecoder : IpcService
- {
- private int _sampleRate;
- private int _channelsCount;
- private bool _reset;
- private OpusDecoder _decoder;
- public IHardwareOpusDecoder(int sampleRate, int channelsCount)
- {
- _sampleRate = sampleRate;
- _channelsCount = channelsCount;
- _reset = false;
- _decoder = new OpusDecoder(sampleRate, channelsCount);
- }
- private ResultCode GetPacketNumSamples(out int numSamples, byte[] packet)
- {
- int result = OpusPacketInfo.GetNumSamples(_decoder, packet, 0, packet.Length);
- numSamples = result;
- if (result == OpusError.OPUS_INVALID_PACKET)
- {
- return ResultCode.OpusInvalidInput;
- }
- else if (result == OpusError.OPUS_BAD_ARG)
- {
- return ResultCode.OpusInvalidInput;
- }
- return ResultCode.Success;
- }
- 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;
- }
- [CommandHipc(0)]
- // DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
- public ResultCode DecodeInterleavedOriginal(ServiceCtx context)
- {
- ResultCode result;
- ulong inPosition = context.Request.SendBuff[0].Position;
- ulong inSize = context.Request.SendBuff[0].Size;
- ulong outputPosition = context.Request.ReceiveBuff[0].Position;
- ulong outputSize = context.Request.ReceiveBuff[0].Size;
- byte[] buffer = new byte[inSize];
- context.Memory.Read(inPosition, buffer);
- using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
- {
- result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
- if (result == ResultCode.Success)
- {
- byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
- Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
- context.Memory.Write(outputPosition, pcmDataBytes);
- context.ResponseData.Write(outConsumed);
- context.ResponseData.Write(outSamples);
- }
- }
- return result;
- }
- [CommandHipc(4)] // 6.0.0+
- // DecodeInterleavedWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
- public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context)
- {
- ResultCode result;
- ulong inPosition = context.Request.SendBuff[0].Position;
- ulong inSize = context.Request.SendBuff[0].Size;
- ulong outputPosition = context.Request.ReceiveBuff[0].Position;
- ulong outputSize = context.Request.ReceiveBuff[0].Size;
- byte[] buffer = new byte[inSize];
- context.Memory.Read(inPosition, buffer);
- using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
- {
- result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
- if (result == ResultCode.Success)
- {
- byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
- Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
- context.Memory.Write(outputPosition, pcmDataBytes);
- context.ResponseData.Write(outConsumed);
- context.ResponseData.Write(outSamples);
- // This is the time the DSP took to process the request, TODO: fill this.
- context.ResponseData.Write(0);
- }
- }
- return result;
- }
- [CommandHipc(6)] // 6.0.0+
- // DecodeInterleavedOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
- public ResultCode DecodeInterleavedOld(ServiceCtx context)
- {
- ResultCode result;
- _reset = context.RequestData.ReadBoolean();
- ulong inPosition = context.Request.SendBuff[0].Position;
- ulong inSize = context.Request.SendBuff[0].Size;
- ulong outputPosition = context.Request.ReceiveBuff[0].Position;
- ulong outputSize = context.Request.ReceiveBuff[0].Size;
- byte[] buffer = new byte[inSize];
- context.Memory.Read(inPosition, buffer);
- using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
- {
- result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
- if (result == ResultCode.Success)
- {
- byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
- Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
- context.Memory.Write(outputPosition, pcmDataBytes);
- context.ResponseData.Write(outConsumed);
- context.ResponseData.Write(outSamples);
- // This is the time the DSP took to process the request, TODO: fill this.
- context.ResponseData.Write(0);
- }
- }
- return result;
- }
- [CommandHipc(8)] // 7.0.0+
- // DecodeInterleaved(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>)
- public ResultCode DecodeInterleaved(ServiceCtx context)
- {
- ResultCode result;
- _reset = context.RequestData.ReadBoolean();
- ulong inPosition = context.Request.SendBuff[0].Position;
- ulong inSize = context.Request.SendBuff[0].Size;
- ulong outputPosition = context.Request.ReceiveBuff[0].Position;
- ulong outputSize = context.Request.ReceiveBuff[0].Size;
- byte[] buffer = new byte[inSize];
- context.Memory.Read(inPosition, buffer);
- using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
- {
- result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
- if (result == ResultCode.Success)
- {
- byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
- Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
- context.Memory.Write(outputPosition, pcmDataBytes);
- context.ResponseData.Write(outConsumed);
- context.ResponseData.Write(outSamples);
- // This is the time the DSP took to process the request, TODO: fill this.
- context.ResponseData.Write(0);
- }
- }
- return result;
- }
- }
- }
|