summaryrefslogtreecommitdiffstats
path: root/src/audio_core/opus/decoder_manager.cpp
blob: 4a5382973a5863365a61222ccb9e80d2d1362f45 (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
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "audio_core/adsp/apps/opus/opus_decoder.h"
#include "audio_core/opus/decoder_manager.h"
#include "common/alignment.h"
#include "core/core.h"

namespace AudioCore::OpusDecoder {
using namespace Service::Audio;

namespace {
bool IsValidChannelCount(u32 channel_count) {
    return channel_count == 1 || channel_count == 2;
}

bool IsValidMultiStreamChannelCount(u32 channel_count) {
    return channel_count > 0 && channel_count <= OpusStreamCountMax;
}

bool IsValidSampleRate(u32 sample_rate) {
    return sample_rate == 8'000 || sample_rate == 12'000 || sample_rate == 16'000 ||
           sample_rate == 24'000 || sample_rate == 48'000;
}

bool IsValidStreamCount(u32 channel_count, u32 total_stream_count, u32 stereo_stream_count) {
    return total_stream_count > 0 && stereo_stream_count > 0 &&
           stereo_stream_count <= total_stream_count &&
           total_stream_count + stereo_stream_count <= channel_count;
}

} // namespace

OpusDecoderManager::OpusDecoderManager(Core::System& system_)
    : system{system_}, hardware_opus{system} {
    for (u32 i = 0; i < MaxChannels; i++) {
        required_workbuffer_sizes[i] = hardware_opus.GetWorkBufferSize(1 + i);
    }
}

Result OpusDecoderManager::GetWorkBufferSize(OpusParameters& params, u64& out_size) {
    OpusParametersEx ex{
        .sample_rate = params.sample_rate,
        .channel_count = params.channel_count,
        .use_large_frame_size = false,
    };
    R_RETURN(GetWorkBufferSizeExEx(ex, out_size));
}

Result OpusDecoderManager::GetWorkBufferSizeEx(OpusParametersEx& params, u64& out_size) {
    R_RETURN(GetWorkBufferSizeExEx(params, out_size));
}

Result OpusDecoderManager::GetWorkBufferSizeExEx(OpusParametersEx& params, u64& out_size) {
    R_UNLESS(IsValidChannelCount(params.channel_count), ResultInvalidOpusChannelCount);
    R_UNLESS(IsValidSampleRate(params.sample_rate), ResultInvalidOpusSampleRate);

    auto work_buffer_size{required_workbuffer_sizes[params.channel_count - 1]};
    auto frame_size{params.use_large_frame_size ? 5760 : 1920};
    work_buffer_size +=
        Common::AlignUp((frame_size * params.channel_count) / (48'000 / params.sample_rate), 64);
    out_size = work_buffer_size + 0x600;
    R_SUCCEED();
}

Result OpusDecoderManager::GetWorkBufferSizeForMultiStream(OpusMultiStreamParameters& params,
                                                           u64& out_size) {
    OpusMultiStreamParametersEx ex{
        .sample_rate = params.sample_rate,
        .channel_count = params.channel_count,
        .total_stream_count = params.total_stream_count,
        .stereo_stream_count = params.stereo_stream_count,
        .use_large_frame_size = false,
        .mappings = {},
    };
    R_RETURN(GetWorkBufferSizeForMultiStreamExEx(ex, out_size));
}

Result OpusDecoderManager::GetWorkBufferSizeForMultiStreamEx(OpusMultiStreamParametersEx& params,
                                                             u64& out_size) {
    R_RETURN(GetWorkBufferSizeForMultiStreamExEx(params, out_size));
}

Result OpusDecoderManager::GetWorkBufferSizeForMultiStreamExEx(OpusMultiStreamParametersEx& params,
                                                               u64& out_size) {
    R_UNLESS(IsValidMultiStreamChannelCount(params.channel_count), ResultInvalidOpusChannelCount);
    R_UNLESS(IsValidSampleRate(params.sample_rate), ResultInvalidOpusSampleRate);
    R_UNLESS(IsValidStreamCount(params.channel_count, params.total_stream_count,
                                params.stereo_stream_count),
             ResultInvalidOpusSampleRate);

    auto work_buffer_size{hardware_opus.GetWorkBufferSizeForMultiStream(
        params.total_stream_count, params.stereo_stream_count)};
    auto frame_size{params.use_large_frame_size ? 5760 : 1920};
    work_buffer_size += Common::AlignUp(1500 * params.total_stream_count, 64);
    work_buffer_size +=
        Common::AlignUp((frame_size * params.channel_count) / (48'000 / params.sample_rate), 64);
    out_size = work_buffer_size;
    R_SUCCEED();
}

} // namespace AudioCore::OpusDecoder