aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Audio/IHardwareOpusDecoderManager.cs
blob: d8e4f75d09eb86e1970873f46a5ed4e060ad945b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Ryujinx.Common;
using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager;
using Ryujinx.HLE.HOS.Services.Audio.Types;

namespace Ryujinx.HLE.HOS.Services.Audio
{
    [Service("hwopus")]
    class IHardwareOpusDecoderManager : IpcService
    {
        public IHardwareOpusDecoderManager(ServiceCtx context) { }

        [CommandHipc(0)]
        // Initialize(bytes<8, 4>, u32, handle<copy>) -> object<nn::codec::detail::IHardwareOpusDecoder>
        public ResultCode Initialize(ServiceCtx context)
        {
            int sampleRate    = context.RequestData.ReadInt32();
            int channelsCount = context.RequestData.ReadInt32();

            MakeObject(context, new IHardwareOpusDecoder(sampleRate, channelsCount));

            // Close transfer memory immediately as we don't use it.
            context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);

            return ResultCode.Success;
        }

        [CommandHipc(1)]
        // GetWorkBufferSize(bytes<8, 4>) -> u32
        public ResultCode GetWorkBufferSize(ServiceCtx context)
        {
            // NOTE: The sample rate is ignored because it is fixed to 48KHz.
            int sampleRate    = context.RequestData.ReadInt32();
            int channelsCount = context.RequestData.ReadInt32();

            context.ResponseData.Write(GetOpusDecoderSize(channelsCount));

            return ResultCode.Success;
        }

        [CommandHipc(4)] // 12.0.0+
        // InitializeEx(OpusParametersEx, u32, handle<copy>) -> object<nn::codec::detail::IHardwareOpusDecoder>
        public ResultCode InitializeEx(ServiceCtx context)
        {
            OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();

            // UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result.
            MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelCount));

            // Close transfer memory immediately as we don't use it.
            context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);

            return ResultCode.Success;
        }

        [CommandHipc(5)] // 12.0.0+
        // GetWorkBufferSizeEx(OpusParametersEx) -> u32
        public ResultCode GetWorkBufferSizeEx(ServiceCtx context)
        {
            OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();

            // NOTE: The sample rate is ignored because it is fixed to 48KHz.
            context.ResponseData.Write(GetOpusDecoderSize(parameters.ChannelCount));

            return ResultCode.Success;
        }

        private static int GetOpusDecoderSize(int channelsCount)
        {
            const int silkDecoderSize = 0x2198;

            if (channelsCount < 1 || channelsCount > 2)
            {
                return 0;
            }

            int celtDecoderSize = GetCeltDecoderSize(channelsCount);

            int opusDecoderSize = (channelsCount * 0x800 + 0x4807) & -0x800 | 0x50;

            return opusDecoderSize + silkDecoderSize + celtDecoderSize;
        }

        private static int GetCeltDecoderSize(int channelsCount)
        {
            const int decodeBufferSize = 0x2030;
            const int celtDecoderSize  = 0x58;
            const int celtSigSize      = 0x4;
            const int overlap          = 120;
            const int eBandsCount      = 21;

            return (decodeBufferSize + overlap * 4) * channelsCount +
                    eBandsCount * 16 +
                    celtDecoderSize +
                    celtSigSize;
        }
    }
}