From 9444b4a647d86fbae3fb06993244613a7b15064e Mon Sep 17 00:00:00 2001 From: gdkchan Date: Fri, 15 Apr 2022 18:16:28 -0300 Subject: [PATCH] Implement HwOpus multistream functions (#3275) * Implement HwOpus multistream functions * Avoid one copy --- .../HardwareOpusDecoderManager/Decoder.cs | 27 ++ .../DecoderCommon.cs | 92 +++++++ .../HardwareOpusDecoderManager/IDecoder.cs | 11 + .../IHardwareOpusDecoder.cs | 248 ++++-------------- .../MultiSampleDecoder.cs | 28 ++ .../Audio/IHardwareOpusDecoderManager.cs | 144 ++++++++-- .../Audio/Types/OpusMultiStreamParameters.cs | 15 ++ .../Types/OpusMultiStreamParametersEx.cs | 19 ++ .../Services/Audio/Types/OpusPacketHeader.cs | 4 +- .../Services/Audio/Types/OpusParametersEx.cs | 4 +- 10 files changed, 380 insertions(+), 212 deletions(-) create mode 100644 Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/Decoder.cs create mode 100644 Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/DecoderCommon.cs create mode 100644 Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IDecoder.cs create mode 100644 Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/MultiSampleDecoder.cs create mode 100644 Ryujinx.HLE/HOS/Services/Audio/Types/OpusMultiStreamParameters.cs create mode 100644 Ryujinx.HLE/HOS/Services/Audio/Types/OpusMultiStreamParametersEx.cs diff --git a/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/Decoder.cs b/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/Decoder.cs new file mode 100644 index 0000000000..b77fc4b053 --- /dev/null +++ b/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/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/DecoderCommon.cs b/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/DecoderCommon.cs new file mode 100644 index 0000000000..944541cce5 --- /dev/null +++ b/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 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()) + { + return ResultCode.OpusInvalidInput; + } + + OpusPacketHeader header = OpusPacketHeader.FromSpan(input); + int headerSize = Unsafe.SizeOf(); + 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/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IDecoder.cs b/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IDecoder.cs new file mode 100644 index 0000000000..9047c26685 --- /dev/null +++ b/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/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs b/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs index 44eeb32de0..84c55d5ec1 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs @@ -1,244 +1,112 @@ -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 readonly IDecoder _decoder; + private readonly OpusDecoderFlags _flags; - private OpusDecoder _decoder; - - public IHardwareOpusDecoder(int sampleRate, int channelsCount) + public IHardwareOpusDecoder(int sampleRate, int channelsCount, OpusDecoderFlags flags) { - _sampleRate = sampleRate; - _channelsCount = channelsCount; - _reset = false; - - _decoder = new OpusDecoder(sampleRate, channelsCount); + _decoder = new Decoder(sampleRate, channelsCount); + _flags = flags; } - private ResultCode GetPacketNumSamples(out int numSamples, byte[] packet) + public IHardwareOpusDecoder(int sampleRate, int channelsCount, int streams, int coupledStreams, OpusDecoderFlags flags, byte[] mapping) { - 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()) - { - return ResultCode.OpusInvalidInput; - } - - OpusPacketHeader header = OpusPacketHeader.FromStream(input); - - uint totalSize = header.length + (uint)Marshal.SizeOf(); - - 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; + _decoder = new MultiSampleDecoder(sampleRate, channelsCount, streams, coupledStreams, mapping); + _flags = flags; } [CommandHipc(0)] - // DecodeInterleaved(buffer) -> (u32, u32, buffer) - public ResultCode DecodeInterleavedOriginal(ServiceCtx context) + // DecodeInterleavedOld(buffer) -> (u32, u32, buffer) + public ResultCode DecodeInterleavedOld(ServiceCtx context) { - ResultCode result; + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: false); + } - 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(2)] + // DecodeInterleavedForMultiStreamOld(buffer) -> (u32, u32, buffer) + public ResultCode DecodeInterleavedForMultiStreamOld(ServiceCtx context) + { + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: false); } [CommandHipc(4)] // 6.0.0+ // DecodeInterleavedWithPerfOld(buffer) -> (u32, u32, u64, buffer) public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context) { - ResultCode result; + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: true); + } - 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(5)] // 6.0.0+ + // DecodeInterleavedForMultiStreamWithPerfOld(buffer) -> (u32, u32, u64, buffer) + public ResultCode DecodeInterleavedForMultiStreamWithPerfOld(ServiceCtx context) + { + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: true); } [CommandHipc(6)] // 6.0.0+ - // DecodeInterleavedOld(bool reset, buffer) -> (u32, u32, u64, buffer) - public ResultCode DecodeInterleavedOld(ServiceCtx context) + // DecodeInterleavedWithPerfAndResetOld(bool reset, buffer) -> (u32, u32, u64, buffer) + public ResultCode DecodeInterleavedWithPerfAndResetOld(ServiceCtx context) { - ResultCode result; + bool reset = context.RequestData.ReadBoolean(); - _reset = context.RequestData.ReadBoolean(); + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset, withPerf: true); + } - 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; + [CommandHipc(7)] // 6.0.0+ + // DecodeInterleavedForMultiStreamWithPerfAndResetOld(bool reset, buffer) -> (u32, u32, u64, buffer) + public ResultCode DecodeInterleavedForMultiStreamWithPerfAndResetOld(ServiceCtx context) + { + bool reset = context.RequestData.ReadBoolean(); - 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; + return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset, withPerf: true); } [CommandHipc(8)] // 7.0.0+ // DecodeInterleaved(bool reset, buffer) -> (u32, u32, u64, buffer) public ResultCode DecodeInterleaved(ServiceCtx context) { - ResultCode result; + bool reset = context.RequestData.ReadBoolean(); - _reset = context.RequestData.ReadBoolean(); + return DecodeInterleavedInternal(context, _flags, reset, withPerf: true); + } + [CommandHipc(9)] // 7.0.0+ + // DecodeInterleavedForMultiStream(bool reset, buffer) -> (u32, u32, u64, buffer) + 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; - byte[] buffer = new byte[inSize]; + ReadOnlySpan input = context.Memory.GetSpan(inPosition, (int)inSize); - context.Memory.Read(inPosition, buffer); + ResultCode result = _decoder.DecodeInterleaved(reset, input, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); - using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer))) + if (result == ResultCode.Success) { - result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples); + context.Memory.Write(outputPosition, MemoryMarshal.Cast(outPcmData.AsSpan())); - if (result == ResultCode.Success) + context.ResponseData.Write(outConsumed); + context.ResponseData.Write(outSamples); + + if (withPerf) { - 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); + context.ResponseData.Write(0UL); } } diff --git a/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/MultiSampleDecoder.cs b/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/MultiSampleDecoder.cs new file mode 100644 index 0000000000..23721d3bf2 --- /dev/null +++ b/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 diff --git a/Ryujinx.HLE/HOS/Services/Audio/IHardwareOpusDecoderManager.cs b/Ryujinx.HLE/HOS/Services/Audio/IHardwareOpusDecoderManager.cs index d8e4f75d09..81ea952b50 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/IHardwareOpusDecoderManager.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/IHardwareOpusDecoderManager.cs @@ -1,6 +1,7 @@ using Ryujinx.Common; using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager; using Ryujinx.HLE.HOS.Services.Audio.Types; +using System.Runtime.InteropServices; namespace Ryujinx.HLE.HOS.Services.Audio { @@ -16,7 +17,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio int sampleRate = context.RequestData.ReadInt32(); int channelsCount = context.RequestData.ReadInt32(); - MakeObject(context, new IHardwareOpusDecoder(sampleRate, channelsCount)); + MakeObject(context, new IHardwareOpusDecoder(sampleRate, channelsCount, OpusDecoderFlags.None)); // Close transfer memory immediately as we don't use it. context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]); @@ -28,11 +29,50 @@ namespace Ryujinx.HLE.HOS.Services.Audio // GetWorkBufferSize(bytes<8, 4>) -> u32 public ResultCode GetWorkBufferSize(ServiceCtx context) { - // NOTE: The sample rate is ignored because it is fixed to 48KHz. int sampleRate = context.RequestData.ReadInt32(); int channelsCount = context.RequestData.ReadInt32(); - context.ResponseData.Write(GetOpusDecoderSize(channelsCount)); + int opusDecoderSize = GetOpusDecoderSize(channelsCount); + + int frameSize = BitUtils.AlignUp(channelsCount * 1920 / (48000 / sampleRate), 64); + int totalSize = opusDecoderSize + 1536 + frameSize; + + context.ResponseData.Write(totalSize); + + return ResultCode.Success; + } + + [CommandHipc(2)] // 3.0.0+ + // InitializeForMultiStream(u32, handle, buffer, 0x19>) -> object + public ResultCode InitializeForMultiStream(ServiceCtx context) + { + ulong parametersAddress = context.Request.PtrBuff[0].Position; + + OpusMultiStreamParameters parameters = context.Memory.Read(parametersAddress); + + MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelsCount, OpusDecoderFlags.None)); + + // Close transfer memory immediately as we don't use it. + context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]); + + return ResultCode.Success; + } + + [CommandHipc(3)] // 3.0.0+ + // GetWorkBufferSizeForMultiStream(buffer, 0x19>) -> u32 + public ResultCode GetWorkBufferSizeForMultiStream(ServiceCtx context) + { + ulong parametersAddress = context.Request.PtrBuff[0].Position; + + OpusMultiStreamParameters parameters = context.Memory.Read(parametersAddress); + + int opusDecoderSize = GetOpusMultistreamDecoderSize(parameters.NumberOfStreams, parameters.NumberOfStereoStreams); + + int streamSize = BitUtils.AlignUp(parameters.NumberOfStreams * 1500, 64); + int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * 1920 / (48000 / parameters.SampleRate), 64); + int totalSize = opusDecoderSize + streamSize + frameSize; + + context.ResponseData.Write(totalSize); return ResultCode.Success; } @@ -44,7 +84,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio OpusParametersEx parameters = context.RequestData.ReadStruct(); // UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result. - MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelCount)); + MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelsCount, parameters.Flags)); // Close transfer memory immediately as we don't use it. context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]); @@ -58,15 +98,84 @@ namespace Ryujinx.HLE.HOS.Services.Audio { OpusParametersEx parameters = context.RequestData.ReadStruct(); - // NOTE: The sample rate is ignored because it is fixed to 48KHz. - context.ResponseData.Write(GetOpusDecoderSize(parameters.ChannelCount)); + int opusDecoderSize = GetOpusDecoderSize(parameters.ChannelsCount); + + int frameSizeMono48KHz = parameters.Flags.HasFlag(OpusDecoderFlags.LargeFrameSize) ? 5760 : 1920; + int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * frameSizeMono48KHz / (48000 / parameters.SampleRate), 64); + int totalSize = opusDecoderSize + 1536 + frameSize; + + context.ResponseData.Write(totalSize); return ResultCode.Success; } + [CommandHipc(6)] // 12.0.0+ + // InitializeForMultiStreamEx(u32, handle, buffer, 0x19>) -> object + public ResultCode InitializeForMultiStreamEx(ServiceCtx context) + { + ulong parametersAddress = context.Request.PtrBuff[0].Position; + + OpusMultiStreamParametersEx parameters = context.Memory.Read(parametersAddress); + + byte[] mappings = MemoryMarshal.Cast(parameters.ChannelMappings.ToSpan()).ToArray(); + + // UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result. + MakeObject(context, new IHardwareOpusDecoder( + parameters.SampleRate, + parameters.ChannelsCount, + parameters.NumberOfStreams, + parameters.NumberOfStereoStreams, + parameters.Flags, + mappings)); + + // Close transfer memory immediately as we don't use it. + context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]); + + return ResultCode.Success; + } + + [CommandHipc(7)] // 12.0.0+ + // GetWorkBufferSizeForMultiStreamEx(buffer, 0x19>) -> u32 + public ResultCode GetWorkBufferSizeForMultiStreamEx(ServiceCtx context) + { + ulong parametersAddress = context.Request.PtrBuff[0].Position; + + OpusMultiStreamParametersEx parameters = context.Memory.Read(parametersAddress); + + int opusDecoderSize = GetOpusMultistreamDecoderSize(parameters.NumberOfStreams, parameters.NumberOfStereoStreams); + + int frameSizeMono48KHz = parameters.Flags.HasFlag(OpusDecoderFlags.LargeFrameSize) ? 5760 : 1920; + int streamSize = BitUtils.AlignUp(parameters.NumberOfStreams * 1500, 64); + int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * frameSizeMono48KHz / (48000 / parameters.SampleRate), 64); + int totalSize = opusDecoderSize + streamSize + frameSize; + + context.ResponseData.Write(totalSize); + + return ResultCode.Success; + } + + private static int GetOpusMultistreamDecoderSize(int streams, int coupledStreams) + { + if (streams < 1 || coupledStreams > streams || coupledStreams < 0) + { + return 0; + } + + int coupledSize = GetOpusDecoderSize(2); + int monoSize = GetOpusDecoderSize(1); + + return Align4(monoSize - GetOpusDecoderAllocSize(1)) * (streams - coupledStreams) + + Align4(coupledSize - GetOpusDecoderAllocSize(2)) * coupledStreams + 0xb90c; + } + + private static int Align4(int value) + { + return BitUtils.AlignUp(value, 4); + } + private static int GetOpusDecoderSize(int channelsCount) { - const int silkDecoderSize = 0x2198; + const int SilkDecoderSize = 0x2160; if (channelsCount < 1 || channelsCount > 2) { @@ -74,24 +183,23 @@ namespace Ryujinx.HLE.HOS.Services.Audio } int celtDecoderSize = GetCeltDecoderSize(channelsCount); + int opusDecoderSize = GetOpusDecoderAllocSize(channelsCount) | 0x4c; - int opusDecoderSize = (channelsCount * 0x800 + 0x4807) & -0x800 | 0x50; + return opusDecoderSize + SilkDecoderSize + celtDecoderSize; + } - return opusDecoderSize + silkDecoderSize + celtDecoderSize; + private static int GetOpusDecoderAllocSize(int channelsCount) + { + return (channelsCount * 0x800 + 0x4803) & -0x800; } private static int GetCeltDecoderSize(int channelsCount) { - const int decodeBufferSize = 0x2030; - const int celtDecoderSize = 0x58; - const int celtSigSize = 0x4; - const int overlap = 120; - const int eBandsCount = 21; + const int DecodeBufferSize = 0x2030; + const int Overlap = 120; + const int EBandsCount = 21; - return (decodeBufferSize + overlap * 4) * channelsCount + - eBandsCount * 16 + - celtDecoderSize + - celtSigSize; + return (DecodeBufferSize + Overlap * 4) * channelsCount + EBandsCount * 16 + 0x50; } } } \ No newline at end of file diff --git a/Ryujinx.HLE/HOS/Services/Audio/Types/OpusMultiStreamParameters.cs b/Ryujinx.HLE/HOS/Services/Audio/Types/OpusMultiStreamParameters.cs new file mode 100644 index 0000000000..fd63a4f791 --- /dev/null +++ b/Ryujinx.HLE/HOS/Services/Audio/Types/OpusMultiStreamParameters.cs @@ -0,0 +1,15 @@ +using Ryujinx.Common.Memory; +using System.Runtime.InteropServices; + +namespace Ryujinx.HLE.HOS.Services.Audio.Types +{ + [StructLayout(LayoutKind.Sequential, Size = 0x110)] + struct OpusMultiStreamParameters + { + public int SampleRate; + public int ChannelsCount; + public int NumberOfStreams; + public int NumberOfStereoStreams; + public Array64 ChannelMappings; + } +} diff --git a/Ryujinx.HLE/HOS/Services/Audio/Types/OpusMultiStreamParametersEx.cs b/Ryujinx.HLE/HOS/Services/Audio/Types/OpusMultiStreamParametersEx.cs new file mode 100644 index 0000000000..1315c734e7 --- /dev/null +++ b/Ryujinx.HLE/HOS/Services/Audio/Types/OpusMultiStreamParametersEx.cs @@ -0,0 +1,19 @@ +using Ryujinx.Common.Memory; +using System.Runtime.InteropServices; + +namespace Ryujinx.HLE.HOS.Services.Audio.Types +{ + [StructLayout(LayoutKind.Sequential, Size = 0x118)] + struct OpusMultiStreamParametersEx + { + public int SampleRate; + public int ChannelsCount; + public int NumberOfStreams; + public int NumberOfStereoStreams; + public OpusDecoderFlags Flags; + + Array4 Padding1; + + public Array64 ChannelMappings; + } +} diff --git a/Ryujinx.HLE/HOS/Services/Audio/Types/OpusPacketHeader.cs b/Ryujinx.HLE/HOS/Services/Audio/Types/OpusPacketHeader.cs index 0a50a5c885..3d63029484 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/Types/OpusPacketHeader.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/Types/OpusPacketHeader.cs @@ -12,9 +12,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.Types public uint length; public uint finalRange; - public static OpusPacketHeader FromStream(BinaryReader reader) + public static OpusPacketHeader FromSpan(ReadOnlySpan data) { - OpusPacketHeader header = reader.ReadStruct(); + OpusPacketHeader header = MemoryMarshal.Cast(data)[0]; header.length = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(header.length) : header.length; header.finalRange = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(header.finalRange) : header.finalRange; diff --git a/Ryujinx.HLE/HOS/Services/Audio/Types/OpusParametersEx.cs b/Ryujinx.HLE/HOS/Services/Audio/Types/OpusParametersEx.cs index f00df2f8c4..f088ed012d 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/Types/OpusParametersEx.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/Types/OpusParametersEx.cs @@ -7,8 +7,8 @@ namespace Ryujinx.HLE.HOS.Services.Audio.Types struct OpusParametersEx { public int SampleRate; - public int ChannelCount; - public OpusDecoderFlags UseLargeFrameSize; + public int ChannelsCount; + public OpusDecoderFlags Flags; Array4 Padding1; }