aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Audio/IHardwareOpusDecoderManager.cs
blob: 8df8a38c9e155650f36ced96ac592a6045920cdd (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Ryujinx.Common;
using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager;
using Ryujinx.HLE.HOS.Services.Audio.Types;
using System.Runtime.InteropServices;

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

        [CommandCmif(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, OpusDecoderFlags.None));

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

            return ResultCode.Success;
        }

        [CommandCmif(1)]
        // GetWorkBufferSize(bytes<8, 4>) -> u32
        public ResultCode GetWorkBufferSize(ServiceCtx context)
        {
            int sampleRate    = context.RequestData.ReadInt32();
            int channelsCount = context.RequestData.ReadInt32();

            int opusDecoderSize = GetOpusDecoderSize(channelsCount);

            int frameSize = BitUtils.AlignUp(channelsCount * 1920 / (48000 / sampleRate), 64);
            int totalSize = opusDecoderSize + 1536 + frameSize;

            context.ResponseData.Write(totalSize);

            return ResultCode.Success;
        }

        [CommandCmif(2)] // 3.0.0+
        // InitializeForMultiStream(u32, handle<copy>, buffer<unknown<0x110>, 0x19>) -> object<nn::codec::detail::IHardwareOpusDecoder>
        public ResultCode InitializeForMultiStream(ServiceCtx context)
        {
            ulong parametersAddress = context.Request.PtrBuff[0].Position;

            OpusMultiStreamParameters parameters = context.Memory.Read<OpusMultiStreamParameters>(parametersAddress);

            MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelsCount, OpusDecoderFlags.None));

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

            return ResultCode.Success;
        }

        [CommandCmif(3)] // 3.0.0+
        // GetWorkBufferSizeForMultiStream(buffer<unknown<0x110>, 0x19>) -> u32
        public ResultCode GetWorkBufferSizeForMultiStream(ServiceCtx context)
        {
            ulong parametersAddress = context.Request.PtrBuff[0].Position;

            OpusMultiStreamParameters parameters = context.Memory.Read<OpusMultiStreamParameters>(parametersAddress);

            int opusDecoderSize = GetOpusMultistreamDecoderSize(parameters.NumberOfStreams, parameters.NumberOfStereoStreams);

            int streamSize = BitUtils.AlignUp(parameters.NumberOfStreams * 1500, 64);
            int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * 1920 / (48000 / parameters.SampleRate), 64);
            int totalSize = opusDecoderSize + streamSize + frameSize;

            context.ResponseData.Write(totalSize);

            return ResultCode.Success;
        }

        [CommandCmif(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.ChannelsCount, parameters.Flags));

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

            return ResultCode.Success;
        }

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

            int opusDecoderSize = GetOpusDecoderSize(parameters.ChannelsCount);

            int frameSizeMono48KHz = parameters.Flags.HasFlag(OpusDecoderFlags.LargeFrameSize) ? 5760 : 1920;
            int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * frameSizeMono48KHz / (48000 / parameters.SampleRate), 64);
            int totalSize = opusDecoderSize + 1536 + frameSize;

            context.ResponseData.Write(totalSize);

            return ResultCode.Success;
        }

        [CommandCmif(6)] // 12.0.0+
        // InitializeForMultiStreamEx(u32, handle<copy>, buffer<unknown<0x118>, 0x19>) -> object<nn::codec::detail::IHardwareOpusDecoder>
        public ResultCode InitializeForMultiStreamEx(ServiceCtx context)
        {
            ulong parametersAddress = context.Request.PtrBuff[0].Position;

            OpusMultiStreamParametersEx parameters = context.Memory.Read<OpusMultiStreamParametersEx>(parametersAddress);

            byte[] mappings = MemoryMarshal.Cast<uint, byte>(parameters.ChannelMappings.AsSpan()).ToArray();

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

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

            return ResultCode.Success;
        }

        [CommandCmif(7)] // 12.0.0+
        // GetWorkBufferSizeForMultiStreamEx(buffer<unknown<0x118>, 0x19>) -> u32
        public ResultCode GetWorkBufferSizeForMultiStreamEx(ServiceCtx context)
        {
            ulong parametersAddress = context.Request.PtrBuff[0].Position;

            OpusMultiStreamParametersEx parameters = context.Memory.Read<OpusMultiStreamParametersEx>(parametersAddress);

            int opusDecoderSize = GetOpusMultistreamDecoderSize(parameters.NumberOfStreams, parameters.NumberOfStereoStreams);

            int frameSizeMono48KHz = parameters.Flags.HasFlag(OpusDecoderFlags.LargeFrameSize) ? 5760 : 1920;
            int streamSize = BitUtils.AlignUp(parameters.NumberOfStreams * 1500, 64);
            int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * frameSizeMono48KHz / (48000 / parameters.SampleRate), 64);
            int totalSize = opusDecoderSize + streamSize + frameSize;

            context.ResponseData.Write(totalSize);

            return ResultCode.Success;
        }

        private static int GetOpusMultistreamDecoderSize(int streams, int coupledStreams)
        {
            if (streams < 1 || coupledStreams > streams || coupledStreams < 0)
            {
                return 0;
            }

            int coupledSize = GetOpusDecoderSize(2);
            int monoSize = GetOpusDecoderSize(1);

            return Align4(monoSize - GetOpusDecoderAllocSize(1)) * (streams - coupledStreams) +
                Align4(coupledSize - GetOpusDecoderAllocSize(2)) * coupledStreams + 0xb90c;
        }

        private static int Align4(int value)
        {
            return BitUtils.AlignUp(value, 4);
        }

        private static int GetOpusDecoderSize(int channelsCount)
        {
            const int SilkDecoderSize = 0x2160;

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

            int celtDecoderSize = GetCeltDecoderSize(channelsCount);
            int opusDecoderSize = GetOpusDecoderAllocSize(channelsCount) | 0x4c;

            return opusDecoderSize + SilkDecoderSize + celtDecoderSize;
        }

        private static int GetOpusDecoderAllocSize(int channelsCount)
        {
            return (channelsCount * 0x800 + 0x4803) & -0x800;
        }

        private static int GetCeltDecoderSize(int channelsCount)
        {
            const int DecodeBufferSize = 0x2030;
            const int Overlap          = 120;
            const int EBandsCount      = 21;

            return (DecodeBufferSize + Overlap * 4) * channelsCount + EBandsCount * 16 + 0x50;
        }
    }
}