IHardwareOpusDecoderManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager;
  3. using Ryujinx.HLE.HOS.Services.Audio.Types;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.HLE.HOS.Services.Audio
  6. {
  7. [Service("hwopus")]
  8. class IHardwareOpusDecoderManager : IpcService
  9. {
  10. public IHardwareOpusDecoderManager(ServiceCtx context) { }
  11. [CommandHipc(0)]
  12. // Initialize(bytes<8, 4>, u32, handle<copy>) -> object<nn::codec::detail::IHardwareOpusDecoder>
  13. public ResultCode Initialize(ServiceCtx context)
  14. {
  15. int sampleRate = context.RequestData.ReadInt32();
  16. int channelsCount = context.RequestData.ReadInt32();
  17. MakeObject(context, new IHardwareOpusDecoder(sampleRate, channelsCount, OpusDecoderFlags.None));
  18. // Close transfer memory immediately as we don't use it.
  19. context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
  20. return ResultCode.Success;
  21. }
  22. [CommandHipc(1)]
  23. // GetWorkBufferSize(bytes<8, 4>) -> u32
  24. public ResultCode GetWorkBufferSize(ServiceCtx context)
  25. {
  26. int sampleRate = context.RequestData.ReadInt32();
  27. int channelsCount = context.RequestData.ReadInt32();
  28. int opusDecoderSize = GetOpusDecoderSize(channelsCount);
  29. int frameSize = BitUtils.AlignUp(channelsCount * 1920 / (48000 / sampleRate), 64);
  30. int totalSize = opusDecoderSize + 1536 + frameSize;
  31. context.ResponseData.Write(totalSize);
  32. return ResultCode.Success;
  33. }
  34. [CommandHipc(2)] // 3.0.0+
  35. // InitializeForMultiStream(u32, handle<copy>, buffer<unknown<0x110>, 0x19>) -> object<nn::codec::detail::IHardwareOpusDecoder>
  36. public ResultCode InitializeForMultiStream(ServiceCtx context)
  37. {
  38. ulong parametersAddress = context.Request.PtrBuff[0].Position;
  39. OpusMultiStreamParameters parameters = context.Memory.Read<OpusMultiStreamParameters>(parametersAddress);
  40. MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelsCount, OpusDecoderFlags.None));
  41. // Close transfer memory immediately as we don't use it.
  42. context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
  43. return ResultCode.Success;
  44. }
  45. [CommandHipc(3)] // 3.0.0+
  46. // GetWorkBufferSizeForMultiStream(buffer<unknown<0x110>, 0x19>) -> u32
  47. public ResultCode GetWorkBufferSizeForMultiStream(ServiceCtx context)
  48. {
  49. ulong parametersAddress = context.Request.PtrBuff[0].Position;
  50. OpusMultiStreamParameters parameters = context.Memory.Read<OpusMultiStreamParameters>(parametersAddress);
  51. int opusDecoderSize = GetOpusMultistreamDecoderSize(parameters.NumberOfStreams, parameters.NumberOfStereoStreams);
  52. int streamSize = BitUtils.AlignUp(parameters.NumberOfStreams * 1500, 64);
  53. int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * 1920 / (48000 / parameters.SampleRate), 64);
  54. int totalSize = opusDecoderSize + streamSize + frameSize;
  55. context.ResponseData.Write(totalSize);
  56. return ResultCode.Success;
  57. }
  58. [CommandHipc(4)] // 12.0.0+
  59. // InitializeEx(OpusParametersEx, u32, handle<copy>) -> object<nn::codec::detail::IHardwareOpusDecoder>
  60. public ResultCode InitializeEx(ServiceCtx context)
  61. {
  62. OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
  63. // UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result.
  64. MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelsCount, parameters.Flags));
  65. // Close transfer memory immediately as we don't use it.
  66. context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
  67. return ResultCode.Success;
  68. }
  69. [CommandHipc(5)] // 12.0.0+
  70. // GetWorkBufferSizeEx(OpusParametersEx) -> u32
  71. public ResultCode GetWorkBufferSizeEx(ServiceCtx context)
  72. {
  73. OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
  74. int opusDecoderSize = GetOpusDecoderSize(parameters.ChannelsCount);
  75. int frameSizeMono48KHz = parameters.Flags.HasFlag(OpusDecoderFlags.LargeFrameSize) ? 5760 : 1920;
  76. int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * frameSizeMono48KHz / (48000 / parameters.SampleRate), 64);
  77. int totalSize = opusDecoderSize + 1536 + frameSize;
  78. context.ResponseData.Write(totalSize);
  79. return ResultCode.Success;
  80. }
  81. [CommandHipc(6)] // 12.0.0+
  82. // InitializeForMultiStreamEx(u32, handle<copy>, buffer<unknown<0x118>, 0x19>) -> object<nn::codec::detail::IHardwareOpusDecoder>
  83. public ResultCode InitializeForMultiStreamEx(ServiceCtx context)
  84. {
  85. ulong parametersAddress = context.Request.PtrBuff[0].Position;
  86. OpusMultiStreamParametersEx parameters = context.Memory.Read<OpusMultiStreamParametersEx>(parametersAddress);
  87. byte[] mappings = MemoryMarshal.Cast<uint, byte>(parameters.ChannelMappings.AsSpan()).ToArray();
  88. // UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result.
  89. MakeObject(context, new IHardwareOpusDecoder(
  90. parameters.SampleRate,
  91. parameters.ChannelsCount,
  92. parameters.NumberOfStreams,
  93. parameters.NumberOfStereoStreams,
  94. parameters.Flags,
  95. mappings));
  96. // Close transfer memory immediately as we don't use it.
  97. context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
  98. return ResultCode.Success;
  99. }
  100. [CommandHipc(7)] // 12.0.0+
  101. // GetWorkBufferSizeForMultiStreamEx(buffer<unknown<0x118>, 0x19>) -> u32
  102. public ResultCode GetWorkBufferSizeForMultiStreamEx(ServiceCtx context)
  103. {
  104. ulong parametersAddress = context.Request.PtrBuff[0].Position;
  105. OpusMultiStreamParametersEx parameters = context.Memory.Read<OpusMultiStreamParametersEx>(parametersAddress);
  106. int opusDecoderSize = GetOpusMultistreamDecoderSize(parameters.NumberOfStreams, parameters.NumberOfStereoStreams);
  107. int frameSizeMono48KHz = parameters.Flags.HasFlag(OpusDecoderFlags.LargeFrameSize) ? 5760 : 1920;
  108. int streamSize = BitUtils.AlignUp(parameters.NumberOfStreams * 1500, 64);
  109. int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * frameSizeMono48KHz / (48000 / parameters.SampleRate), 64);
  110. int totalSize = opusDecoderSize + streamSize + frameSize;
  111. context.ResponseData.Write(totalSize);
  112. return ResultCode.Success;
  113. }
  114. private static int GetOpusMultistreamDecoderSize(int streams, int coupledStreams)
  115. {
  116. if (streams < 1 || coupledStreams > streams || coupledStreams < 0)
  117. {
  118. return 0;
  119. }
  120. int coupledSize = GetOpusDecoderSize(2);
  121. int monoSize = GetOpusDecoderSize(1);
  122. return Align4(monoSize - GetOpusDecoderAllocSize(1)) * (streams - coupledStreams) +
  123. Align4(coupledSize - GetOpusDecoderAllocSize(2)) * coupledStreams + 0xb90c;
  124. }
  125. private static int Align4(int value)
  126. {
  127. return BitUtils.AlignUp(value, 4);
  128. }
  129. private static int GetOpusDecoderSize(int channelsCount)
  130. {
  131. const int SilkDecoderSize = 0x2160;
  132. if (channelsCount < 1 || channelsCount > 2)
  133. {
  134. return 0;
  135. }
  136. int celtDecoderSize = GetCeltDecoderSize(channelsCount);
  137. int opusDecoderSize = GetOpusDecoderAllocSize(channelsCount) | 0x4c;
  138. return opusDecoderSize + SilkDecoderSize + celtDecoderSize;
  139. }
  140. private static int GetOpusDecoderAllocSize(int channelsCount)
  141. {
  142. return (channelsCount * 0x800 + 0x4803) & -0x800;
  143. }
  144. private static int GetCeltDecoderSize(int channelsCount)
  145. {
  146. const int DecodeBufferSize = 0x2030;
  147. const int Overlap = 120;
  148. const int EBandsCount = 21;
  149. return (DecodeBufferSize + Overlap * 4) * channelsCount + EBandsCount * 16 + 0x50;
  150. }
  151. }
  152. }