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