IHardwareOpusDecoder.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Concentus.Structs;
  2. using static Ryujinx.HLE.HOS.ErrorCode;
  3. namespace Ryujinx.HLE.HOS.Services.Aud
  4. {
  5. class IHardwareOpusDecoder : IpcService
  6. {
  7. private const int FixedSampleRate = 48000;
  8. private int _sampleRate;
  9. private int _channelsCount;
  10. private OpusDecoder _decoder;
  11. public IHardwareOpusDecoder(int sampleRate, int channelsCount)
  12. {
  13. _sampleRate = sampleRate;
  14. _channelsCount = channelsCount;
  15. _decoder = new OpusDecoder(FixedSampleRate, channelsCount);
  16. }
  17. [Command(0)]
  18. // DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
  19. public long DecodeInterleaved(ServiceCtx context)
  20. {
  21. long inPosition = context.Request.SendBuff[0].Position;
  22. long inSize = context.Request.SendBuff[0].Size;
  23. if (inSize < 8)
  24. {
  25. return MakeError(ErrorModule.Audio, AudErr.OpusInvalidInput);
  26. }
  27. long outPosition = context.Request.ReceiveBuff[0].Position;
  28. long outSize = context.Request.ReceiveBuff[0].Size;
  29. byte[] opusData = context.Memory.ReadBytes(inPosition, inSize);
  30. int processed = ((opusData[0] << 24) |
  31. (opusData[1] << 16) |
  32. (opusData[2] << 8) |
  33. (opusData[3] << 0)) + 8;
  34. if ((uint)processed > (ulong)inSize)
  35. {
  36. return MakeError(ErrorModule.Audio, AudErr.OpusInvalidInput);
  37. }
  38. short[] pcm = new short[outSize / 2];
  39. int frameSize = pcm.Length / (_channelsCount * 2);
  40. int samples = _decoder.Decode(opusData, 0, opusData.Length, pcm, 0, frameSize);
  41. foreach (short sample in pcm)
  42. {
  43. context.Memory.WriteInt16(outPosition, sample);
  44. outPosition += 2;
  45. }
  46. context.ResponseData.Write(processed);
  47. context.ResponseData.Write(samples);
  48. return 0;
  49. }
  50. [Command(4)]
  51. // DecodeInterleavedWithPerf(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
  52. public long DecodeInterleavedWithPerf(ServiceCtx context)
  53. {
  54. long result = DecodeInterleaved(context);
  55. // TODO: Figure out what this value is.
  56. // According to switchbrew, it is now used.
  57. context.ResponseData.Write(0L);
  58. return result;
  59. }
  60. }
  61. }