IHardwareOpusDecoderManager.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager;
  2. namespace Ryujinx.HLE.HOS.Services.Audio
  3. {
  4. [Service("hwopus")]
  5. class IHardwareOpusDecoderManager : IpcService
  6. {
  7. public IHardwareOpusDecoderManager(ServiceCtx context) { }
  8. [CommandHipc(0)]
  9. // Initialize(bytes<8, 4>, u32, handle<copy>) -> object<nn::codec::detail::IHardwareOpusDecoder>
  10. public ResultCode Initialize(ServiceCtx context)
  11. {
  12. int sampleRate = context.RequestData.ReadInt32();
  13. int channelsCount = context.RequestData.ReadInt32();
  14. MakeObject(context, new IHardwareOpusDecoder(sampleRate, channelsCount));
  15. // Close transfer memory immediately as we don't use it.
  16. context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
  17. return ResultCode.Success;
  18. }
  19. [CommandHipc(1)]
  20. // GetWorkBufferSize(bytes<8, 4>) -> u32
  21. public ResultCode GetWorkBufferSize(ServiceCtx context)
  22. {
  23. // Note: The sample rate is ignored because it is fixed to 48KHz.
  24. int sampleRate = context.RequestData.ReadInt32();
  25. int channelsCount = context.RequestData.ReadInt32();
  26. context.ResponseData.Write(GetOpusDecoderSize(channelsCount));
  27. return ResultCode.Success;
  28. }
  29. private static int GetOpusDecoderSize(int channelsCount)
  30. {
  31. const int silkDecoderSize = 0x2198;
  32. if (channelsCount < 1 || channelsCount > 2)
  33. {
  34. return 0;
  35. }
  36. int celtDecoderSize = GetCeltDecoderSize(channelsCount);
  37. int opusDecoderSize = (channelsCount * 0x800 + 0x4807) & -0x800 | 0x50;
  38. return opusDecoderSize + silkDecoderSize + celtDecoderSize;
  39. }
  40. private static int GetCeltDecoderSize(int channelsCount)
  41. {
  42. const int decodeBufferSize = 0x2030;
  43. const int celtDecoderSize = 0x58;
  44. const int celtSigSize = 0x4;
  45. const int overlap = 120;
  46. const int eBandsCount = 21;
  47. return (decodeBufferSize + overlap * 4) * channelsCount +
  48. eBandsCount * 16 +
  49. celtDecoderSize +
  50. celtSigSize;
  51. }
  52. }
  53. }