IHardwareOpusDecoder.cs 2.5 KB

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