IHardwareOpusDecoder.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using Concentus;
  2. using Concentus.Enums;
  3. using Concentus.Structs;
  4. using Ryujinx.HLE.HOS.Services.Audio.Types;
  5. using System;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
  9. {
  10. class IHardwareOpusDecoder : IpcService
  11. {
  12. private int _sampleRate;
  13. private int _channelsCount;
  14. private bool _reset;
  15. private OpusDecoder _decoder;
  16. public IHardwareOpusDecoder(int sampleRate, int channelsCount)
  17. {
  18. _sampleRate = sampleRate;
  19. _channelsCount = channelsCount;
  20. _reset = false;
  21. _decoder = new OpusDecoder(sampleRate, channelsCount);
  22. }
  23. private ResultCode GetPacketNumSamples(out int numSamples, byte[] packet)
  24. {
  25. int result = OpusPacketInfo.GetNumSamples(_decoder, packet, 0, packet.Length);
  26. numSamples = result;
  27. if (result == OpusError.OPUS_INVALID_PACKET)
  28. {
  29. return ResultCode.OpusInvalidInput;
  30. }
  31. else if (result == OpusError.OPUS_BAD_ARG)
  32. {
  33. return ResultCode.OpusInvalidInput;
  34. }
  35. return ResultCode.Success;
  36. }
  37. private ResultCode DecodeInterleavedInternal(BinaryReader input, out short[] outPcmData, long outputSize, out uint outConsumed, out int outSamples)
  38. {
  39. outPcmData = null;
  40. outConsumed = 0;
  41. outSamples = 0;
  42. long streamSize = input.BaseStream.Length;
  43. if (streamSize < Marshal.SizeOf<OpusPacketHeader>())
  44. {
  45. return ResultCode.OpusInvalidInput;
  46. }
  47. OpusPacketHeader header = OpusPacketHeader.FromStream(input);
  48. uint totalSize = header.length + (uint)Marshal.SizeOf<OpusPacketHeader>();
  49. if (totalSize > streamSize)
  50. {
  51. return ResultCode.OpusInvalidInput;
  52. }
  53. byte[] opusData = input.ReadBytes((int)header.length);
  54. ResultCode result = GetPacketNumSamples(out int numSamples, opusData);
  55. if (result == ResultCode.Success)
  56. {
  57. if ((uint)numSamples * (uint)_channelsCount * sizeof(short) > outputSize)
  58. {
  59. return ResultCode.OpusInvalidInput;
  60. }
  61. outPcmData = new short[numSamples * _channelsCount];
  62. if (_reset)
  63. {
  64. _reset = false;
  65. _decoder.ResetState();
  66. }
  67. try
  68. {
  69. outSamples = _decoder.Decode(opusData, 0, opusData.Length, outPcmData, 0, outPcmData.Length / _channelsCount);
  70. outConsumed = totalSize;
  71. }
  72. catch (OpusException)
  73. {
  74. // TODO: as OpusException doesn't provide us the exact error code, this is kind of inaccurate in some cases...
  75. return ResultCode.OpusInvalidInput;
  76. }
  77. }
  78. return ResultCode.Success;
  79. }
  80. [Command(0)]
  81. // DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
  82. public ResultCode DecodeInterleavedOriginal(ServiceCtx context)
  83. {
  84. ResultCode result;
  85. long inPosition = context.Request.SendBuff[0].Position;
  86. long inSize = context.Request.SendBuff[0].Size;
  87. long outputPosition = context.Request.ReceiveBuff[0].Position;
  88. long outputSize = context.Request.ReceiveBuff[0].Size;
  89. using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize))))
  90. {
  91. result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
  92. if (result == ResultCode.Success)
  93. {
  94. byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
  95. Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
  96. context.Memory.WriteBytes(outputPosition, pcmDataBytes);
  97. context.ResponseData.Write(outConsumed);
  98. context.ResponseData.Write(outSamples);
  99. }
  100. }
  101. return result;
  102. }
  103. [Command(4)] // 6.0.0+
  104. // DecodeInterleavedWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
  105. public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context)
  106. {
  107. ResultCode result;
  108. long inPosition = context.Request.SendBuff[0].Position;
  109. long inSize = context.Request.SendBuff[0].Size;
  110. long outputPosition = context.Request.ReceiveBuff[0].Position;
  111. long outputSize = context.Request.ReceiveBuff[0].Size;
  112. using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize))))
  113. {
  114. result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
  115. if (result == ResultCode.Success)
  116. {
  117. byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
  118. Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
  119. context.Memory.WriteBytes(outputPosition, pcmDataBytes);
  120. context.ResponseData.Write(outConsumed);
  121. context.ResponseData.Write(outSamples);
  122. // This is the time the DSP took to process the request, TODO: fill this.
  123. context.ResponseData.Write(0);
  124. }
  125. }
  126. return result;
  127. }
  128. [Command(6)] // 6.0.0+
  129. // DecodeInterleavedOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
  130. public ResultCode DecodeInterleavedOld(ServiceCtx context)
  131. {
  132. ResultCode result;
  133. _reset = context.RequestData.ReadBoolean();
  134. long inPosition = context.Request.SendBuff[0].Position;
  135. long inSize = context.Request.SendBuff[0].Size;
  136. long outputPosition = context.Request.ReceiveBuff[0].Position;
  137. long outputSize = context.Request.ReceiveBuff[0].Size;
  138. using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize))))
  139. {
  140. result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
  141. if (result == ResultCode.Success)
  142. {
  143. byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
  144. Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
  145. context.Memory.WriteBytes(outputPosition, pcmDataBytes);
  146. context.ResponseData.Write(outConsumed);
  147. context.ResponseData.Write(outSamples);
  148. // This is the time the DSP took to process the request, TODO: fill this.
  149. context.ResponseData.Write(0);
  150. }
  151. }
  152. return result;
  153. }
  154. [Command(8)] // 7.0.0+
  155. // DecodeInterleaved(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>)
  156. public ResultCode DecodeInterleaved(ServiceCtx context)
  157. {
  158. ResultCode result;
  159. _reset = context.RequestData.ReadBoolean();
  160. long inPosition = context.Request.SendBuff[0].Position;
  161. long inSize = context.Request.SendBuff[0].Size;
  162. long outputPosition = context.Request.ReceiveBuff[0].Position;
  163. long outputSize = context.Request.ReceiveBuff[0].Size;
  164. using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize))))
  165. {
  166. result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
  167. if (result == ResultCode.Success)
  168. {
  169. byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
  170. Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
  171. context.Memory.WriteBytes(outputPosition, pcmDataBytes);
  172. context.ResponseData.Write(outConsumed);
  173. context.ResponseData.Write(outSamples);
  174. // This is the time the DSP took to process the request, TODO: fill this.
  175. context.ResponseData.Write(0);
  176. }
  177. }
  178. return result;
  179. }
  180. }
  181. }