2019-10-11 15:22:24 +00:00
|
|
|
using Concentus;
|
|
|
|
using Concentus.Enums;
|
2018-11-17 03:36:49 +00:00
|
|
|
using Concentus.Structs;
|
2019-10-11 15:22:24 +00:00
|
|
|
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Runtime.InteropServices;
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-09-19 00:45:11 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
|
2018-11-17 03:36:49 +00:00
|
|
|
{
|
|
|
|
class IHardwareOpusDecoder : IpcService
|
|
|
|
{
|
2019-10-11 15:22:24 +00:00
|
|
|
private int _sampleRate;
|
|
|
|
private int _channelsCount;
|
|
|
|
private bool _reset;
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private OpusDecoder _decoder;
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public IHardwareOpusDecoder(int sampleRate, int channelsCount)
|
2018-11-17 03:36:49 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_sampleRate = sampleRate;
|
|
|
|
_channelsCount = channelsCount;
|
2019-10-11 15:22:24 +00:00
|
|
|
_reset = false;
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
_decoder = new OpusDecoder(sampleRate, channelsCount);
|
2018-11-17 03:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
private ResultCode GetPacketNumSamples(out int numSamples, byte[] packet)
|
2018-11-17 03:36:49 +00:00
|
|
|
{
|
2019-10-11 15:22:24 +00:00
|
|
|
int result = OpusPacketInfo.GetNumSamples(_decoder, packet, 0, packet.Length);
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
numSamples = result;
|
|
|
|
|
|
|
|
if (result == OpusError.OPUS_INVALID_PACKET)
|
|
|
|
{
|
|
|
|
return ResultCode.OpusInvalidInput;
|
|
|
|
}
|
|
|
|
else if (result == OpusError.OPUS_BAD_ARG)
|
2018-11-17 03:36:49 +00:00
|
|
|
{
|
2019-07-14 19:04:38 +00:00
|
|
|
return ResultCode.OpusInvalidInput;
|
2018-11-17 03:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
private ResultCode DecodeInterleavedInternal(BinaryReader input, out short[] outPcmData, long outputSize, out uint outConsumed, out int outSamples)
|
|
|
|
{
|
|
|
|
outPcmData = null;
|
|
|
|
outConsumed = 0;
|
|
|
|
outSamples = 0;
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
long streamSize = input.BaseStream.Length;
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
if (streamSize < Marshal.SizeOf<OpusPacketHeader>())
|
2018-11-17 03:36:49 +00:00
|
|
|
{
|
2019-07-14 19:04:38 +00:00
|
|
|
return ResultCode.OpusInvalidInput;
|
2018-11-17 03:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
OpusPacketHeader header = OpusPacketHeader.FromStream(input);
|
|
|
|
|
|
|
|
uint totalSize = header.length + (uint)Marshal.SizeOf<OpusPacketHeader>();
|
|
|
|
|
|
|
|
if (totalSize > streamSize)
|
|
|
|
{
|
|
|
|
return ResultCode.OpusInvalidInput;
|
|
|
|
}
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
byte[] opusData = input.ReadBytes((int)header.length);
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
ResultCode result = GetPacketNumSamples(out int numSamples, opusData);
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
if (result == ResultCode.Success)
|
2018-11-17 03:36:49 +00:00
|
|
|
{
|
2019-10-11 15:22:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(0)]
|
2019-10-11 15:22:24 +00:00
|
|
|
// DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
|
|
|
|
public ResultCode DecodeInterleavedOriginal(ServiceCtx context)
|
|
|
|
{
|
|
|
|
ResultCode result;
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
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;
|
2019-10-11 15:22:24 +00:00
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
byte[] buffer = new byte[inSize];
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Read(inPosition, buffer);
|
2020-05-03 22:54:50 +00:00
|
|
|
|
|
|
|
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
|
2019-10-11 15:22:24 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
|
2019-10-11 15:22:24 +00:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
|
|
|
|
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(outputPosition, pcmDataBytes);
|
2019-10-11 15:22:24 +00:00
|
|
|
|
|
|
|
context.ResponseData.Write(outConsumed);
|
|
|
|
context.ResponseData.Write(outSamples);
|
|
|
|
}
|
2018-11-17 03:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-11-17 03:36:49 +00:00
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(4)] // 6.0.0+
|
2019-10-11 15:22:24 +00:00
|
|
|
// DecodeInterleavedWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
|
|
|
public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context)
|
|
|
|
{
|
|
|
|
ResultCode result;
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
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;
|
2019-10-11 15:22:24 +00:00
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
byte[] buffer = new byte[inSize];
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Read(inPosition, buffer);
|
2020-05-03 22:54:50 +00:00
|
|
|
|
|
|
|
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
|
2019-10-11 15:22:24 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
|
2019-10-11 15:22:24 +00:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
|
|
|
|
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(outputPosition, pcmDataBytes);
|
2019-10-11 15:22:24 +00:00
|
|
|
|
|
|
|
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;
|
2018-11-17 03:36:49 +00:00
|
|
|
}
|
2019-07-12 01:13:43 +00:00
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(6)] // 6.0.0+
|
2019-10-11 15:22:24 +00:00
|
|
|
// DecodeInterleavedOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
|
|
|
public ResultCode DecodeInterleavedOld(ServiceCtx context)
|
2019-07-12 01:13:43 +00:00
|
|
|
{
|
2019-10-11 15:22:24 +00:00
|
|
|
ResultCode result;
|
|
|
|
|
|
|
|
_reset = context.RequestData.ReadBoolean();
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
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;
|
2019-10-11 15:22:24 +00:00
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
byte[] buffer = new byte[inSize];
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Read(inPosition, buffer);
|
2020-05-03 22:54:50 +00:00
|
|
|
|
|
|
|
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
|
2019-10-11 15:22:24 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
|
2019-10-11 15:22:24 +00:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
|
|
|
|
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(outputPosition, pcmDataBytes);
|
2019-10-11 15:22:24 +00:00
|
|
|
|
|
|
|
context.ResponseData.Write(outConsumed);
|
|
|
|
context.ResponseData.Write(outSamples);
|
2019-07-12 01:13:43 +00:00
|
|
|
|
2019-10-11 15:22:24 +00:00
|
|
|
// This is the time the DSP took to process the request, TODO: fill this.
|
|
|
|
context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-13 22:01:24 +00:00
|
|
|
[CommandHipc(8)] // 7.0.0+
|
2019-10-11 15:22:24 +00:00
|
|
|
// DecodeInterleaved(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
|
|
|
public ResultCode DecodeInterleaved(ServiceCtx context)
|
|
|
|
{
|
|
|
|
ResultCode result;
|
|
|
|
|
|
|
|
_reset = context.RequestData.ReadBoolean();
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
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;
|
2019-10-11 15:22:24 +00:00
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
byte[] buffer = new byte[inSize];
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Read(inPosition, buffer);
|
2020-05-03 22:54:50 +00:00
|
|
|
|
|
|
|
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
|
2019-10-11 15:22:24 +00:00
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
|
2019-10-11 15:22:24 +00:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
|
|
|
|
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(outputPosition, pcmDataBytes);
|
2019-10-11 15:22:24 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 01:13:43 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-11-17 03:36:49 +00:00
|
|
|
}
|
2019-07-12 01:13:43 +00:00
|
|
|
}
|