2021-07-06 19:49:51 +02:00
|
|
|
using Ryujinx.Common;
|
2019-09-19 02:45:11 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager;
|
2021-07-06 19:49:51 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
2022-04-15 23:16:28 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2019-09-19 02:45:11 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Audio
|
2018-11-17 04:36:49 +01:00
|
|
|
{
|
2019-07-10 17:59:54 +02:00
|
|
|
[Service("hwopus")]
|
2018-11-17 04:36:49 +01:00
|
|
|
class IHardwareOpusDecoderManager : IpcService
|
|
|
|
{
|
2019-07-12 03:13:43 +02:00
|
|
|
public IHardwareOpusDecoderManager(ServiceCtx context) { }
|
2018-11-17 04:36:49 +01:00
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(0)]
|
2019-07-12 03:13:43 +02:00
|
|
|
// Initialize(bytes<8, 4>, u32, handle<copy>) -> object<nn::codec::detail::IHardwareOpusDecoder>
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode Initialize(ServiceCtx context)
|
2018-11-17 04:36:49 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int sampleRate = context.RequestData.ReadInt32();
|
|
|
|
int channelsCount = context.RequestData.ReadInt32();
|
2018-11-17 04:36:49 +01:00
|
|
|
|
2022-04-15 23:16:28 +02:00
|
|
|
MakeObject(context, new IHardwareOpusDecoder(sampleRate, channelsCount, OpusDecoderFlags.None));
|
2018-11-17 04:36:49 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
// Close transfer memory immediately as we don't use it.
|
|
|
|
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-11-17 04:36:49 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(1)]
|
2019-07-12 03:13:43 +02:00
|
|
|
// GetWorkBufferSize(bytes<8, 4>) -> u32
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetWorkBufferSize(ServiceCtx context)
|
2018-11-17 04:36:49 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int sampleRate = context.RequestData.ReadInt32();
|
|
|
|
int channelsCount = context.RequestData.ReadInt32();
|
2018-11-17 04:36:49 +01:00
|
|
|
|
2022-04-15 23:16:28 +02:00
|
|
|
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<copy>, buffer<unknown<0x110>, 0x19>) -> object<nn::codec::detail::IHardwareOpusDecoder>
|
|
|
|
public ResultCode InitializeForMultiStream(ServiceCtx context)
|
|
|
|
{
|
|
|
|
ulong parametersAddress = context.Request.PtrBuff[0].Position;
|
|
|
|
|
|
|
|
OpusMultiStreamParameters parameters = context.Memory.Read<OpusMultiStreamParameters>(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<unknown<0x110>, 0x19>) -> u32
|
|
|
|
public ResultCode GetWorkBufferSizeForMultiStream(ServiceCtx context)
|
|
|
|
{
|
|
|
|
ulong parametersAddress = context.Request.PtrBuff[0].Position;
|
|
|
|
|
|
|
|
OpusMultiStreamParameters parameters = context.Memory.Read<OpusMultiStreamParameters>(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);
|
2018-11-17 04:36:49 +01:00
|
|
|
|
2021-07-06 19:49:51 +02:00
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
[CommandHipc(4)] // 12.0.0+
|
|
|
|
// InitializeEx(OpusParametersEx, u32, handle<copy>) -> object<nn::codec::detail::IHardwareOpusDecoder>
|
|
|
|
public ResultCode InitializeEx(ServiceCtx context)
|
|
|
|
{
|
|
|
|
OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
|
|
|
|
|
|
|
|
// UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result.
|
2022-04-15 23:16:28 +02:00
|
|
|
MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelsCount, parameters.Flags));
|
2021-07-06 19:49:51 +02:00
|
|
|
|
|
|
|
// 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(5)] // 12.0.0+
|
|
|
|
// GetWorkBufferSizeEx(OpusParametersEx) -> u32
|
|
|
|
public ResultCode GetWorkBufferSizeEx(ServiceCtx context)
|
|
|
|
{
|
|
|
|
OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
|
|
|
|
|
2022-04-15 23:16:28 +02:00
|
|
|
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<copy>, buffer<unknown<0x118>, 0x19>) -> object<nn::codec::detail::IHardwareOpusDecoder>
|
|
|
|
public ResultCode InitializeForMultiStreamEx(ServiceCtx context)
|
|
|
|
{
|
|
|
|
ulong parametersAddress = context.Request.PtrBuff[0].Position;
|
|
|
|
|
|
|
|
OpusMultiStreamParametersEx parameters = context.Memory.Read<OpusMultiStreamParametersEx>(parametersAddress);
|
|
|
|
|
|
|
|
byte[] mappings = MemoryMarshal.Cast<uint, byte>(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<unknown<0x118>, 0x19>) -> u32
|
|
|
|
public ResultCode GetWorkBufferSizeForMultiStreamEx(ServiceCtx context)
|
|
|
|
{
|
|
|
|
ulong parametersAddress = context.Request.PtrBuff[0].Position;
|
|
|
|
|
|
|
|
OpusMultiStreamParametersEx parameters = context.Memory.Read<OpusMultiStreamParametersEx>(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);
|
2021-07-06 19:49:51 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-11-17 04:36:49 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 23:16:28 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static int GetOpusDecoderSize(int channelsCount)
|
2018-11-17 04:36:49 +01:00
|
|
|
{
|
2022-04-15 23:16:28 +02:00
|
|
|
const int SilkDecoderSize = 0x2160;
|
2018-11-17 04:36:49 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (channelsCount < 1 || channelsCount > 2)
|
2018-11-17 04:36:49 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int celtDecoderSize = GetCeltDecoderSize(channelsCount);
|
2022-04-15 23:16:28 +02:00
|
|
|
int opusDecoderSize = GetOpusDecoderAllocSize(channelsCount) | 0x4c;
|
2018-11-17 04:36:49 +01:00
|
|
|
|
2022-04-15 23:16:28 +02:00
|
|
|
return opusDecoderSize + SilkDecoderSize + celtDecoderSize;
|
|
|
|
}
|
2018-11-17 04:36:49 +01:00
|
|
|
|
2022-04-15 23:16:28 +02:00
|
|
|
private static int GetOpusDecoderAllocSize(int channelsCount)
|
|
|
|
{
|
|
|
|
return (channelsCount * 0x800 + 0x4803) & -0x800;
|
2018-11-17 04:36:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static int GetCeltDecoderSize(int channelsCount)
|
2018-11-17 04:36:49 +01:00
|
|
|
{
|
2022-04-15 23:16:28 +02:00
|
|
|
const int DecodeBufferSize = 0x2030;
|
|
|
|
const int Overlap = 120;
|
|
|
|
const int EBandsCount = 21;
|
|
|
|
|
|
|
|
return (DecodeBufferSize + Overlap * 4) * channelsCount + EBandsCount * 16 + 0x50;
|
2018-11-17 04:36:49 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-12 03:13:43 +02:00
|
|
|
}
|