diff options
Diffstat (limited to 'src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager')
5 files changed, 274 insertions, 0 deletions
diff --git a/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/Decoder.cs b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/Decoder.cs new file mode 100644 index 00000000..b77fc4b0 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/Decoder.cs @@ -0,0 +1,27 @@ +using Concentus.Structs; + +namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager +{ + class Decoder : IDecoder + { + private readonly OpusDecoder _decoder; + + public int SampleRate => _decoder.SampleRate; + public int ChannelsCount => _decoder.NumChannels; + + public Decoder(int sampleRate, int channelsCount) + { + _decoder = new OpusDecoder(sampleRate, channelsCount); + } + + public int Decode(byte[] inData, int inDataOffset, int len, short[] outPcm, int outPcmOffset, int frameSize) + { + return _decoder.Decode(inData, inDataOffset, len, outPcm, outPcmOffset, frameSize); + } + + public void ResetState() + { + _decoder.ResetState(); + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/DecoderCommon.cs b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/DecoderCommon.cs new file mode 100644 index 00000000..944541cc --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/DecoderCommon.cs @@ -0,0 +1,92 @@ +using Concentus; +using Concentus.Enums; +using Concentus.Structs; +using Ryujinx.HLE.HOS.Services.Audio.Types; +using System; +using System.Runtime.CompilerServices; + +namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager +{ + static class DecoderCommon + { + private static ResultCode GetPacketNumSamples(this IDecoder decoder, out int numSamples, byte[] packet) + { + int result = OpusPacketInfo.GetNumSamples(packet, 0, packet.Length, decoder.SampleRate); + + numSamples = result; + + if (result == OpusError.OPUS_INVALID_PACKET) + { + return ResultCode.OpusInvalidInput; + } + else if (result == OpusError.OPUS_BAD_ARG) + { + 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; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IDecoder.cs b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IDecoder.cs new file mode 100644 index 00000000..9047c266 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IDecoder.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager +{ + interface IDecoder + { + int SampleRate { get; } + int ChannelsCount { get; } + + int Decode(byte[] inData, int inDataOffset, int len, short[] outPcm, int outPcmOffset, int frameSize); + void ResetState(); + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs new file mode 100644 index 00000000..e94b31ca --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs @@ -0,0 +1,116 @@ +using Ryujinx.HLE.HOS.Services.Audio.Types; +using System; +using System.Runtime.InteropServices; + +namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager +{ + class IHardwareOpusDecoder : IpcService + { + private readonly IDecoder _decoder; + private readonly OpusDecoderFlags _flags; + + public IHardwareOpusDecoder(int sampleRate, int channelsCount, OpusDecoderFlags flags) + { + _decoder = new Decoder(sampleRate, channelsCount); + _flags = flags; + } + + public IHardwareOpusDecoder(int sampleRate, int channelsCount, int streams, int coupledStreams, OpusDecoderFlags flags, byte[] mapping) + { + _decoder = new MultiSampleDecoder(sampleRate, channelsCount, streams, coupledStreams, mapping); + _flags = flags; + } + + [CommandCmif(0)] + // DecodeInterleavedOld(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>) + public ResultCode DecodeInterleavedOld(ServiceCtx context) + { + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: false); + } + + [CommandCmif(2)] + // DecodeInterleavedForMultiStreamOld(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>) + public ResultCode DecodeInterleavedForMultiStreamOld(ServiceCtx context) + { + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: false); + } + + [CommandCmif(4)] // 6.0.0+ + // DecodeInterleavedWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>) + public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context) + { + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: true); + } + + [CommandCmif(5)] // 6.0.0+ + // DecodeInterleavedForMultiStreamWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>) + public ResultCode DecodeInterleavedForMultiStreamWithPerfOld(ServiceCtx context) + { + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: true); + } + + [CommandCmif(6)] // 6.0.0+ + // DecodeInterleavedWithPerfAndResetOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>) + public ResultCode DecodeInterleavedWithPerfAndResetOld(ServiceCtx context) + { + bool reset = context.RequestData.ReadBoolean(); + + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset, withPerf: true); + } + + [CommandCmif(7)] // 6.0.0+ + // DecodeInterleavedForMultiStreamWithPerfAndResetOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>) + public ResultCode DecodeInterleavedForMultiStreamWithPerfAndResetOld(ServiceCtx context) + { + bool reset = context.RequestData.ReadBoolean(); + + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset, withPerf: true); + } + + [CommandCmif(8)] // 7.0.0+ + // DecodeInterleaved(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>) + public ResultCode DecodeInterleaved(ServiceCtx context) + { + bool reset = context.RequestData.ReadBoolean(); + + return DecodeInterleavedInternal(context, _flags, reset, withPerf: true); + } + + [CommandCmif(9)] // 7.0.0+ + // DecodeInterleavedForMultiStream(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>) + public ResultCode DecodeInterleavedForMultiStream(ServiceCtx context) + { + bool reset = context.RequestData.ReadBoolean(); + + return DecodeInterleavedInternal(context, _flags, reset, withPerf: true); + } + + private ResultCode DecodeInterleavedInternal(ServiceCtx context, OpusDecoderFlags flags, bool reset, bool withPerf) + { + 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; + + ReadOnlySpan<byte> input = context.Memory.GetSpan(inPosition, (int)inSize); + + ResultCode result = _decoder.DecodeInterleaved(reset, input, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); + + if (result == ResultCode.Success) + { + context.Memory.Write(outputPosition, MemoryMarshal.Cast<short, byte>(outPcmData.AsSpan())); + + context.ResponseData.Write(outConsumed); + context.ResponseData.Write(outSamples); + + if (withPerf) + { + // This is the time the DSP took to process the request, TODO: fill this. + context.ResponseData.Write(0UL); + } + } + + return result; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/MultiSampleDecoder.cs b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/MultiSampleDecoder.cs new file mode 100644 index 00000000..23721d3b --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/MultiSampleDecoder.cs @@ -0,0 +1,28 @@ +using Concentus.Structs; + +namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager +{ + class MultiSampleDecoder : IDecoder + { + private readonly OpusMSDecoder _decoder; + + public int SampleRate => _decoder.SampleRate; + public int ChannelsCount { get; } + + public MultiSampleDecoder(int sampleRate, int channelsCount, int streams, int coupledStreams, byte[] mapping) + { + ChannelsCount = channelsCount; + _decoder = new OpusMSDecoder(sampleRate, channelsCount, streams, coupledStreams, mapping); + } + + public int Decode(byte[] inData, int inDataOffset, int len, short[] outPcm, int outPcmOffset, int frameSize) + { + return _decoder.DecodeMultistream(inData, inDataOffset, len, outPcm, outPcmOffset, frameSize, 0); + } + + public void ResetState() + { + _decoder.ResetState(); + } + } +}
\ No newline at end of file |
