Fix hwopus DecodeInterleaved implementation (#786)

* Fix hwopus DecodeInterleaved implementation

Also implement new variants of this api.

This should fix #763

* Sample rate shouldn't be hardcoded

This fix issues while opening Pokémon Let's Go pause menu.

* Apply Ac_K's suggestion about EndianSwap

* Address gdkchan's comment

* Address Ac_k's comment
This commit is contained in:
Thomas Guillemard 2019-10-11 17:22:24 +02:00 committed by Ac_K
parent 1aba033ba7
commit 9142aca48f
5 changed files with 240 additions and 50 deletions

View file

@ -1,6 +1,8 @@
namespace Ryujinx.HLE.Utilities using System;
namespace Ryujinx.Common
{ {
static class EndianSwap public static class EndianSwap
{ {
public static ushort Swap16(ushort value) => (ushort)(((value >> 8) & 0xff) | (value << 8)); public static ushort Swap16(ushort value) => (ushort)(((value >> 8) & 0xff) | (value << 8));
@ -13,5 +15,17 @@
((uintVal << 8) & 0x00ff0000) | ((uintVal << 8) & 0x00ff0000) |
((uintVal << 24) & 0xff000000)); ((uintVal << 24) & 0xff000000));
} }
public static uint FromBigEndianToPlatformEndian(uint value)
{
uint result = value;
if (BitConverter.IsLittleEndian)
{
result = (uint)EndianSwap.Swap32((int)result);
}
return result;
}
} }
} }

View file

@ -1,9 +1,9 @@
using LibHac.Fs; using LibHac.Fs;
using LibHac.Fs.NcaUtils; using LibHac.Fs.NcaUtils;
using Ryujinx.Common;
using Ryujinx.HLE.FileSystem; using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.FileSystem.Content; using Ryujinx.HLE.FileSystem.Content;
using Ryujinx.HLE.Resource; using Ryujinx.HLE.Resource;
using Ryujinx.HLE.Utilities;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using static Ryujinx.HLE.Utilities.FontUtils; using static Ryujinx.HLE.Utilities.FontUtils;

View file

@ -1,13 +1,18 @@
using Concentus;
using Concentus.Enums;
using Concentus.Structs; 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 namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
{ {
class IHardwareOpusDecoder : IpcService class IHardwareOpusDecoder : IpcService
{ {
private const int FixedSampleRate = 48000; private int _sampleRate;
private int _channelsCount;
private int _sampleRate; private bool _reset;
private int _channelsCount;
private OpusDecoder _decoder; private OpusDecoder _decoder;
@ -15,65 +20,211 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
{ {
_sampleRate = sampleRate; _sampleRate = sampleRate;
_channelsCount = channelsCount; _channelsCount = channelsCount;
_reset = false;
_decoder = new OpusDecoder(FixedSampleRate, channelsCount); _decoder = new OpusDecoder(sampleRate, channelsCount);
} }
[Command(0)] private ResultCode GetPacketNumSamples(out int numSamples, byte[] packet)
// DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
public ResultCode DecodeInterleaved(ServiceCtx context)
{ {
long inPosition = context.Request.SendBuff[0].Position; int result = OpusPacketInfo.GetNumSamples(_decoder, packet, 0, packet.Length);
long inSize = context.Request.SendBuff[0].Size;
if (inSize < 8) numSamples = result;
if (result == OpusError.OPUS_INVALID_PACKET)
{ {
return ResultCode.OpusInvalidInput; return ResultCode.OpusInvalidInput;
} }
else if (result == OpusError.OPUS_BAD_ARG)
long outPosition = context.Request.ReceiveBuff[0].Position;
long outSize = context.Request.ReceiveBuff[0].Size;
byte[] opusData = context.Memory.ReadBytes(inPosition, inSize);
int processed = ((opusData[0] << 24) |
(opusData[1] << 16) |
(opusData[2] << 8) |
(opusData[3] << 0)) + 8;
if ((uint)processed > (ulong)inSize)
{ {
return ResultCode.OpusInvalidInput; return ResultCode.OpusInvalidInput;
} }
short[] pcm = new short[outSize / 2];
int frameSize = pcm.Length / (_channelsCount * 2);
int samples = _decoder.Decode(opusData, 0, opusData.Length, pcm, 0, frameSize);
foreach (short sample in pcm)
{
context.Memory.WriteInt16(outPosition, sample);
outPosition += 2;
}
context.ResponseData.Write(processed);
context.ResponseData.Write(samples);
return ResultCode.Success; return ResultCode.Success;
} }
[Command(4)] private ResultCode DecodeInterleavedInternal(BinaryReader input, out short[] outPcmData, long outputSize, out uint outConsumed, out int outSamples)
// DecodeInterleavedWithPerf(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
public ResultCode DecodeInterleavedWithPerf(ServiceCtx context)
{ {
ResultCode result = DecodeInterleaved(context); outPcmData = null;
outConsumed = 0;
outSamples = 0;
// TODO: Figure out what this value is. long streamSize = input.BaseStream.Length;
// According to switchbrew, it is now used.
context.ResponseData.Write(0L); 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;
}
[Command(0)]
// DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
public ResultCode DecodeInterleavedOriginal(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;
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize))))
{
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);
context.Memory.WriteBytes(outputPosition, pcmDataBytes);
context.ResponseData.Write(outConsumed);
context.ResponseData.Write(outSamples);
}
}
return result;
}
[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;
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize))))
{
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);
context.Memory.WriteBytes(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;
}
[Command(6)] // 6.0.0+
// DecodeInterleavedOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
public ResultCode DecodeInterleavedOld(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;
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize))))
{
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);
context.Memory.WriteBytes(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;
}
[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;
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize))))
{
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);
context.Memory.WriteBytes(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 result;
} }

View file

@ -0,0 +1,24 @@
using Ryujinx.Common;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Audio.Types
{
[StructLayout(LayoutKind.Sequential)]
struct OpusPacketHeader
{
public uint length;
public uint finalRange;
public static OpusPacketHeader FromStream(BinaryReader reader)
{
OpusPacketHeader header = reader.ReadStruct<OpusPacketHeader>();
header.length = EndianSwap.FromBigEndianToPlatformEndian(header.length);
header.finalRange = EndianSwap.FromBigEndianToPlatformEndian(header.finalRange);
return header;
}
}
}

View file

@ -1,4 +1,5 @@
using Ryujinx.Common.Logging; using Ryujinx.Common;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.Utilities; using Ryujinx.HLE.Utilities;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;