IHardwareOpusDecoder.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. [CommandHipc(0)]
  81. // DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
  82. public ResultCode DecodeInterleavedOriginal(ServiceCtx context)
  83. {
  84. ResultCode result;
  85. ulong inPosition = context.Request.SendBuff[0].Position;
  86. ulong inSize = context.Request.SendBuff[0].Size;
  87. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  88. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  89. byte[] buffer = new byte[inSize];
  90. context.Memory.Read(inPosition, buffer);
  91. using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
  92. {
  93. result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
  94. if (result == ResultCode.Success)
  95. {
  96. byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
  97. Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
  98. context.Memory.Write(outputPosition, pcmDataBytes);
  99. context.ResponseData.Write(outConsumed);
  100. context.ResponseData.Write(outSamples);
  101. }
  102. }
  103. return result;
  104. }
  105. [CommandHipc(4)] // 6.0.0+
  106. // DecodeInterleavedWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
  107. public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context)
  108. {
  109. ResultCode result;
  110. ulong inPosition = context.Request.SendBuff[0].Position;
  111. ulong inSize = context.Request.SendBuff[0].Size;
  112. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  113. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  114. byte[] buffer = new byte[inSize];
  115. context.Memory.Read(inPosition, buffer);
  116. using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
  117. {
  118. result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
  119. if (result == ResultCode.Success)
  120. {
  121. byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
  122. Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
  123. context.Memory.Write(outputPosition, pcmDataBytes);
  124. context.ResponseData.Write(outConsumed);
  125. context.ResponseData.Write(outSamples);
  126. // This is the time the DSP took to process the request, TODO: fill this.
  127. context.ResponseData.Write(0);
  128. }
  129. }
  130. return result;
  131. }
  132. [CommandHipc(6)] // 6.0.0+
  133. // DecodeInterleavedOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
  134. public ResultCode DecodeInterleavedOld(ServiceCtx context)
  135. {
  136. ResultCode result;
  137. _reset = context.RequestData.ReadBoolean();
  138. ulong inPosition = context.Request.SendBuff[0].Position;
  139. ulong inSize = context.Request.SendBuff[0].Size;
  140. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  141. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  142. byte[] buffer = new byte[inSize];
  143. context.Memory.Read(inPosition, buffer);
  144. using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
  145. {
  146. result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
  147. if (result == ResultCode.Success)
  148. {
  149. byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
  150. Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
  151. context.Memory.Write(outputPosition, pcmDataBytes);
  152. context.ResponseData.Write(outConsumed);
  153. context.ResponseData.Write(outSamples);
  154. // This is the time the DSP took to process the request, TODO: fill this.
  155. context.ResponseData.Write(0);
  156. }
  157. }
  158. return result;
  159. }
  160. [CommandHipc(8)] // 7.0.0+
  161. // DecodeInterleaved(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>)
  162. public ResultCode DecodeInterleaved(ServiceCtx context)
  163. {
  164. ResultCode result;
  165. _reset = context.RequestData.ReadBoolean();
  166. ulong inPosition = context.Request.SendBuff[0].Position;
  167. ulong inSize = context.Request.SendBuff[0].Size;
  168. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  169. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  170. byte[] buffer = new byte[inSize];
  171. context.Memory.Read(inPosition, buffer);
  172. using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
  173. {
  174. result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
  175. if (result == ResultCode.Success)
  176. {
  177. byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
  178. Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
  179. context.Memory.Write(outputPosition, pcmDataBytes);
  180. context.ResponseData.Write(outConsumed);
  181. context.ResponseData.Write(outSamples);
  182. // This is the time the DSP took to process the request, TODO: fill this.
  183. context.ResponseData.Write(0);
  184. }
  185. }
  186. return result;
  187. }
  188. }
  189. }