mirror of
https://github.com/ryujinx-mirror/ryujinx.git
synced 2024-12-23 05:25:45 +00:00
Implement HwOpus multistream functions (#3275)
* Implement HwOpus multistream functions * Avoid one copy
This commit is contained in:
parent
610fc84f3e
commit
9444b4a647
10 changed files with 380 additions and 212 deletions
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,244 +1,112 @@
|
||||||
using Concentus;
|
|
||||||
using Concentus.Enums;
|
|
||||||
using Concentus.Structs;
|
|
||||||
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
|
namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
|
||||||
{
|
{
|
||||||
class IHardwareOpusDecoder : IpcService
|
class IHardwareOpusDecoder : IpcService
|
||||||
{
|
{
|
||||||
private int _sampleRate;
|
private readonly IDecoder _decoder;
|
||||||
private int _channelsCount;
|
private readonly OpusDecoderFlags _flags;
|
||||||
private bool _reset;
|
|
||||||
|
|
||||||
private OpusDecoder _decoder;
|
public IHardwareOpusDecoder(int sampleRate, int channelsCount, OpusDecoderFlags flags)
|
||||||
|
|
||||||
public IHardwareOpusDecoder(int sampleRate, int channelsCount)
|
|
||||||
{
|
{
|
||||||
_sampleRate = sampleRate;
|
_decoder = new Decoder(sampleRate, channelsCount);
|
||||||
_channelsCount = channelsCount;
|
_flags = flags;
|
||||||
_reset = false;
|
|
||||||
|
|
||||||
_decoder = new OpusDecoder(sampleRate, channelsCount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
_decoder = new MultiSampleDecoder(sampleRate, channelsCount, streams, coupledStreams, mapping);
|
||||||
|
_flags = flags;
|
||||||
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)]
|
[CommandHipc(0)]
|
||||||
// DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
|
// DecodeInterleavedOld(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
|
||||||
public ResultCode DecodeInterleavedOriginal(ServiceCtx context)
|
public ResultCode DecodeInterleavedOld(ServiceCtx context)
|
||||||
{
|
{
|
||||||
ResultCode result;
|
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: false);
|
||||||
|
}
|
||||||
|
|
||||||
ulong inPosition = context.Request.SendBuff[0].Position;
|
[CommandHipc(2)]
|
||||||
ulong inSize = context.Request.SendBuff[0].Size;
|
// DecodeInterleavedForMultiStreamOld(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
|
||||||
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
public ResultCode DecodeInterleavedForMultiStreamOld(ServiceCtx context)
|
||||||
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
{
|
||||||
|
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: false);
|
||||||
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+
|
[CommandHipc(4)] // 6.0.0+
|
||||||
// DecodeInterleavedWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
// DecodeInterleavedWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||||
public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context)
|
public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context)
|
||||||
{
|
{
|
||||||
ResultCode result;
|
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: true);
|
||||||
|
}
|
||||||
|
|
||||||
ulong inPosition = context.Request.SendBuff[0].Position;
|
[CommandHipc(5)] // 6.0.0+
|
||||||
ulong inSize = context.Request.SendBuff[0].Size;
|
// DecodeInterleavedForMultiStreamWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||||
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
public ResultCode DecodeInterleavedForMultiStreamWithPerfOld(ServiceCtx context)
|
||||||
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
{
|
||||||
|
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: true);
|
||||||
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+
|
[CommandHipc(6)] // 6.0.0+
|
||||||
// DecodeInterleavedOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
// DecodeInterleavedWithPerfAndResetOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||||
public ResultCode DecodeInterleavedOld(ServiceCtx context)
|
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;
|
[CommandHipc(7)] // 6.0.0+
|
||||||
ulong inSize = context.Request.SendBuff[0].Size;
|
// DecodeInterleavedForMultiStreamWithPerfAndResetOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||||
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
public ResultCode DecodeInterleavedForMultiStreamWithPerfAndResetOld(ServiceCtx context)
|
||||||
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
{
|
||||||
|
bool reset = context.RequestData.ReadBoolean();
|
||||||
|
|
||||||
byte[] buffer = new byte[inSize];
|
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset, withPerf: true);
|
||||||
|
|
||||||
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+
|
[CommandHipc(8)] // 7.0.0+
|
||||||
// DecodeInterleaved(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
// DecodeInterleaved(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||||
public ResultCode DecodeInterleaved(ServiceCtx context)
|
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<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 inPosition = context.Request.SendBuff[0].Position;
|
||||||
ulong inSize = context.Request.SendBuff[0].Size;
|
ulong inSize = context.Request.SendBuff[0].Size;
|
||||||
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
||||||
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
||||||
|
|
||||||
byte[] buffer = new byte[inSize];
|
ReadOnlySpan<byte> 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<short, byte>(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.
|
// This is the time the DSP took to process the request, TODO: fill this.
|
||||||
context.ResponseData.Write(0);
|
context.ResponseData.Write(0UL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
using Ryujinx.Common;
|
using Ryujinx.Common;
|
||||||
using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager;
|
using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager;
|
||||||
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Ryujinx.HLE.HOS.Services.Audio
|
namespace Ryujinx.HLE.HOS.Services.Audio
|
||||||
{
|
{
|
||||||
|
@ -16,7 +17,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio
|
||||||
int sampleRate = context.RequestData.ReadInt32();
|
int sampleRate = context.RequestData.ReadInt32();
|
||||||
int channelsCount = 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.
|
// Close transfer memory immediately as we don't use it.
|
||||||
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
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
|
// GetWorkBufferSize(bytes<8, 4>) -> u32
|
||||||
public ResultCode GetWorkBufferSize(ServiceCtx context)
|
public ResultCode GetWorkBufferSize(ServiceCtx context)
|
||||||
{
|
{
|
||||||
// NOTE: The sample rate is ignored because it is fixed to 48KHz.
|
|
||||||
int sampleRate = context.RequestData.ReadInt32();
|
int sampleRate = context.RequestData.ReadInt32();
|
||||||
int channelsCount = 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<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);
|
||||||
|
|
||||||
return ResultCode.Success;
|
return ResultCode.Success;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +84,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio
|
||||||
OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
|
OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
|
||||||
|
|
||||||
// UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result.
|
// 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.
|
// Close transfer memory immediately as we don't use it.
|
||||||
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
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<OpusParametersEx>();
|
OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
|
||||||
|
|
||||||
// NOTE: The sample rate is ignored because it is fixed to 48KHz.
|
int opusDecoderSize = GetOpusDecoderSize(parameters.ChannelsCount);
|
||||||
context.ResponseData.Write(GetOpusDecoderSize(parameters.ChannelCount));
|
|
||||||
|
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;
|
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);
|
||||||
|
|
||||||
|
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)
|
private static int GetOpusDecoderSize(int channelsCount)
|
||||||
{
|
{
|
||||||
const int silkDecoderSize = 0x2198;
|
const int SilkDecoderSize = 0x2160;
|
||||||
|
|
||||||
if (channelsCount < 1 || channelsCount > 2)
|
if (channelsCount < 1 || channelsCount > 2)
|
||||||
{
|
{
|
||||||
|
@ -74,24 +183,23 @@ namespace Ryujinx.HLE.HOS.Services.Audio
|
||||||
}
|
}
|
||||||
|
|
||||||
int celtDecoderSize = GetCeltDecoderSize(channelsCount);
|
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)
|
private static int GetCeltDecoderSize(int channelsCount)
|
||||||
{
|
{
|
||||||
const int decodeBufferSize = 0x2030;
|
const int DecodeBufferSize = 0x2030;
|
||||||
const int celtDecoderSize = 0x58;
|
const int Overlap = 120;
|
||||||
const int celtSigSize = 0x4;
|
const int EBandsCount = 21;
|
||||||
const int overlap = 120;
|
|
||||||
const int eBandsCount = 21;
|
|
||||||
|
|
||||||
return (decodeBufferSize + overlap * 4) * channelsCount +
|
return (DecodeBufferSize + Overlap * 4) * channelsCount + EBandsCount * 16 + 0x50;
|
||||||
eBandsCount * 16 +
|
|
||||||
celtDecoderSize +
|
|
||||||
celtSigSize;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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<uint> ChannelMappings;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<byte> Padding1;
|
||||||
|
|
||||||
|
public Array64<uint> ChannelMappings;
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,9 +12,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.Types
|
||||||
public uint length;
|
public uint length;
|
||||||
public uint finalRange;
|
public uint finalRange;
|
||||||
|
|
||||||
public static OpusPacketHeader FromStream(BinaryReader reader)
|
public static OpusPacketHeader FromSpan(ReadOnlySpan<byte> data)
|
||||||
{
|
{
|
||||||
OpusPacketHeader header = reader.ReadStruct<OpusPacketHeader>();
|
OpusPacketHeader header = MemoryMarshal.Cast<byte, OpusPacketHeader>(data)[0];
|
||||||
|
|
||||||
header.length = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(header.length) : header.length;
|
header.length = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(header.length) : header.length;
|
||||||
header.finalRange = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(header.finalRange) : header.finalRange;
|
header.finalRange = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(header.finalRange) : header.finalRange;
|
||||||
|
|
|
@ -7,8 +7,8 @@ namespace Ryujinx.HLE.HOS.Services.Audio.Types
|
||||||
struct OpusParametersEx
|
struct OpusParametersEx
|
||||||
{
|
{
|
||||||
public int SampleRate;
|
public int SampleRate;
|
||||||
public int ChannelCount;
|
public int ChannelsCount;
|
||||||
public OpusDecoderFlags UseLargeFrameSize;
|
public OpusDecoderFlags Flags;
|
||||||
|
|
||||||
Array4<byte> Padding1;
|
Array4<byte> Padding1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue