diff options
| author | 2023-11-04 18:25:40 +0000 | |
|---|---|---|
| committer | 2023-11-04 18:25:40 +0000 | |
| commit | 90aa937593e53a5d5e070fb623b228578b0b225f (patch) | |
| tree | 784daaa873597eb414d63ec680a8a1203b4b11b0 /src/audio_core/adsp | |
| parent | Merge pull request #11952 from liamwhite/opus_stereo_count (diff) | |
| download | yuzu-90aa937593e53a5d5e070fb623b228578b0b225f.tar.gz yuzu-90aa937593e53a5d5e070fb623b228578b0b225f.tar.xz yuzu-90aa937593e53a5d5e070fb623b228578b0b225f.zip | |
Convert files to LF eol
Diffstat (limited to 'src/audio_core/adsp')
| -rw-r--r-- | src/audio_core/adsp/apps/opus/opus_decode_object.cpp | 214 | ||||
| -rw-r--r-- | src/audio_core/adsp/apps/opus/opus_multistream_decode_object.cpp | 222 |
2 files changed, 218 insertions, 218 deletions
diff --git a/src/audio_core/adsp/apps/opus/opus_decode_object.cpp b/src/audio_core/adsp/apps/opus/opus_decode_object.cpp index 2c16d3769..e2b9eb566 100644 --- a/src/audio_core/adsp/apps/opus/opus_decode_object.cpp +++ b/src/audio_core/adsp/apps/opus/opus_decode_object.cpp | |||
| @@ -1,107 +1,107 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include "audio_core/adsp/apps/opus/opus_decode_object.h" | 4 | #include "audio_core/adsp/apps/opus/opus_decode_object.h" |
| 5 | #include "common/assert.h" | 5 | #include "common/assert.h" |
| 6 | 6 | ||
| 7 | namespace AudioCore::ADSP::OpusDecoder { | 7 | namespace AudioCore::ADSP::OpusDecoder { |
| 8 | namespace { | 8 | namespace { |
| 9 | bool IsValidChannelCount(u32 channel_count) { | 9 | bool IsValidChannelCount(u32 channel_count) { |
| 10 | return channel_count == 1 || channel_count == 2; | 10 | return channel_count == 1 || channel_count == 2; |
| 11 | } | 11 | } |
| 12 | } // namespace | 12 | } // namespace |
| 13 | 13 | ||
| 14 | u32 OpusDecodeObject::GetWorkBufferSize(u32 channel_count) { | 14 | u32 OpusDecodeObject::GetWorkBufferSize(u32 channel_count) { |
| 15 | if (!IsValidChannelCount(channel_count)) { | 15 | if (!IsValidChannelCount(channel_count)) { |
| 16 | return 0; | 16 | return 0; |
| 17 | } | 17 | } |
| 18 | return static_cast<u32>(sizeof(OpusDecodeObject)) + opus_decoder_get_size(channel_count); | 18 | return static_cast<u32>(sizeof(OpusDecodeObject)) + opus_decoder_get_size(channel_count); |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | OpusDecodeObject& OpusDecodeObject::Initialize(u64 buffer, u64 buffer2) { | 21 | OpusDecodeObject& OpusDecodeObject::Initialize(u64 buffer, u64 buffer2) { |
| 22 | auto* new_decoder = reinterpret_cast<OpusDecodeObject*>(buffer); | 22 | auto* new_decoder = reinterpret_cast<OpusDecodeObject*>(buffer); |
| 23 | auto* comparison = reinterpret_cast<OpusDecodeObject*>(buffer2); | 23 | auto* comparison = reinterpret_cast<OpusDecodeObject*>(buffer2); |
| 24 | 24 | ||
| 25 | if (new_decoder->magic == DecodeObjectMagic) { | 25 | if (new_decoder->magic == DecodeObjectMagic) { |
| 26 | if (!new_decoder->initialized || | 26 | if (!new_decoder->initialized || |
| 27 | (new_decoder->initialized && new_decoder->self == comparison)) { | 27 | (new_decoder->initialized && new_decoder->self == comparison)) { |
| 28 | new_decoder->state_valid = true; | 28 | new_decoder->state_valid = true; |
| 29 | } | 29 | } |
| 30 | } else { | 30 | } else { |
| 31 | new_decoder->initialized = false; | 31 | new_decoder->initialized = false; |
| 32 | new_decoder->state_valid = true; | 32 | new_decoder->state_valid = true; |
| 33 | } | 33 | } |
| 34 | return *new_decoder; | 34 | return *new_decoder; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | s32 OpusDecodeObject::InitializeDecoder(u32 sample_rate, u32 channel_count) { | 37 | s32 OpusDecodeObject::InitializeDecoder(u32 sample_rate, u32 channel_count) { |
| 38 | if (!state_valid) { | 38 | if (!state_valid) { |
| 39 | return OPUS_INVALID_STATE; | 39 | return OPUS_INVALID_STATE; |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | if (initialized) { | 42 | if (initialized) { |
| 43 | return OPUS_OK; | 43 | return OPUS_OK; |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | // Unfortunately libopus does not expose the OpusDecoder struct publicly, so we can't include | 46 | // Unfortunately libopus does not expose the OpusDecoder struct publicly, so we can't include |
| 47 | // it in this class. Nintendo does not allocate memory, which is why we have a workbuffer | 47 | // it in this class. Nintendo does not allocate memory, which is why we have a workbuffer |
| 48 | // provided. | 48 | // provided. |
| 49 | // We could use _create and have libopus allocate it for us, but then we have to separately | 49 | // We could use _create and have libopus allocate it for us, but then we have to separately |
| 50 | // track which decoder is being used between this and multistream in order to call the correct | 50 | // track which decoder is being used between this and multistream in order to call the correct |
| 51 | // destroy from the host side. | 51 | // destroy from the host side. |
| 52 | // This is a bit cringe, but is safe as these objects are only ever initialized inside the given | 52 | // This is a bit cringe, but is safe as these objects are only ever initialized inside the given |
| 53 | // workbuffer, and GetWorkBufferSize will guarantee there's enough space to follow. | 53 | // workbuffer, and GetWorkBufferSize will guarantee there's enough space to follow. |
| 54 | decoder = (LibOpusDecoder*)(this + 1); | 54 | decoder = (LibOpusDecoder*)(this + 1); |
| 55 | s32 ret = opus_decoder_init(decoder, sample_rate, channel_count); | 55 | s32 ret = opus_decoder_init(decoder, sample_rate, channel_count); |
| 56 | if (ret == OPUS_OK) { | 56 | if (ret == OPUS_OK) { |
| 57 | magic = DecodeObjectMagic; | 57 | magic = DecodeObjectMagic; |
| 58 | initialized = true; | 58 | initialized = true; |
| 59 | state_valid = true; | 59 | state_valid = true; |
| 60 | self = this; | 60 | self = this; |
| 61 | final_range = 0; | 61 | final_range = 0; |
| 62 | } | 62 | } |
| 63 | return ret; | 63 | return ret; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | s32 OpusDecodeObject::Shutdown() { | 66 | s32 OpusDecodeObject::Shutdown() { |
| 67 | if (!state_valid) { | 67 | if (!state_valid) { |
| 68 | return OPUS_INVALID_STATE; | 68 | return OPUS_INVALID_STATE; |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | if (initialized) { | 71 | if (initialized) { |
| 72 | magic = 0x0; | 72 | magic = 0x0; |
| 73 | initialized = false; | 73 | initialized = false; |
| 74 | state_valid = false; | 74 | state_valid = false; |
| 75 | self = nullptr; | 75 | self = nullptr; |
| 76 | final_range = 0; | 76 | final_range = 0; |
| 77 | decoder = nullptr; | 77 | decoder = nullptr; |
| 78 | } | 78 | } |
| 79 | return OPUS_OK; | 79 | return OPUS_OK; |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | s32 OpusDecodeObject::ResetDecoder() { | 82 | s32 OpusDecodeObject::ResetDecoder() { |
| 83 | return opus_decoder_ctl(decoder, OPUS_RESET_STATE); | 83 | return opus_decoder_ctl(decoder, OPUS_RESET_STATE); |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | s32 OpusDecodeObject::Decode(u32& out_sample_count, u64 output_data, u64 output_data_size, | 86 | s32 OpusDecodeObject::Decode(u32& out_sample_count, u64 output_data, u64 output_data_size, |
| 87 | u64 input_data, u64 input_data_size) { | 87 | u64 input_data, u64 input_data_size) { |
| 88 | ASSERT(initialized); | 88 | ASSERT(initialized); |
| 89 | out_sample_count = 0; | 89 | out_sample_count = 0; |
| 90 | 90 | ||
| 91 | if (!state_valid) { | 91 | if (!state_valid) { |
| 92 | return OPUS_INVALID_STATE; | 92 | return OPUS_INVALID_STATE; |
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | auto ret_code_or_samples = opus_decode( | 95 | auto ret_code_or_samples = opus_decode( |
| 96 | decoder, reinterpret_cast<const u8*>(input_data), static_cast<opus_int32>(input_data_size), | 96 | decoder, reinterpret_cast<const u8*>(input_data), static_cast<opus_int32>(input_data_size), |
| 97 | reinterpret_cast<opus_int16*>(output_data), static_cast<opus_int32>(output_data_size), 0); | 97 | reinterpret_cast<opus_int16*>(output_data), static_cast<opus_int32>(output_data_size), 0); |
| 98 | 98 | ||
| 99 | if (ret_code_or_samples < OPUS_OK) { | 99 | if (ret_code_or_samples < OPUS_OK) { |
| 100 | return ret_code_or_samples; | 100 | return ret_code_or_samples; |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | out_sample_count = ret_code_or_samples; | 103 | out_sample_count = ret_code_or_samples; |
| 104 | return opus_decoder_ctl(decoder, OPUS_GET_FINAL_RANGE_REQUEST, &final_range); | 104 | return opus_decoder_ctl(decoder, OPUS_GET_FINAL_RANGE_REQUEST, &final_range); |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | } // namespace AudioCore::ADSP::OpusDecoder | 107 | } // namespace AudioCore::ADSP::OpusDecoder |
diff --git a/src/audio_core/adsp/apps/opus/opus_multistream_decode_object.cpp b/src/audio_core/adsp/apps/opus/opus_multistream_decode_object.cpp index f6d362e68..7f1ed0450 100644 --- a/src/audio_core/adsp/apps/opus/opus_multistream_decode_object.cpp +++ b/src/audio_core/adsp/apps/opus/opus_multistream_decode_object.cpp | |||
| @@ -1,111 +1,111 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include "audio_core/adsp/apps/opus/opus_multistream_decode_object.h" | 4 | #include "audio_core/adsp/apps/opus/opus_multistream_decode_object.h" |
| 5 | #include "common/assert.h" | 5 | #include "common/assert.h" |
| 6 | 6 | ||
| 7 | namespace AudioCore::ADSP::OpusDecoder { | 7 | namespace AudioCore::ADSP::OpusDecoder { |
| 8 | 8 | ||
| 9 | namespace { | 9 | namespace { |
| 10 | bool IsValidChannelCount(u32 channel_count) { | 10 | bool IsValidChannelCount(u32 channel_count) { |
| 11 | return channel_count == 1 || channel_count == 2; | 11 | return channel_count == 1 || channel_count == 2; |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | bool IsValidStreamCounts(u32 total_stream_count, u32 stereo_stream_count) { | 14 | bool IsValidStreamCounts(u32 total_stream_count, u32 stereo_stream_count) { |
| 15 | return total_stream_count > 0 && stereo_stream_count > 0 && | 15 | return total_stream_count > 0 && stereo_stream_count > 0 && |
| 16 | stereo_stream_count <= total_stream_count && IsValidChannelCount(total_stream_count); | 16 | stereo_stream_count <= total_stream_count && IsValidChannelCount(total_stream_count); |
| 17 | } | 17 | } |
| 18 | } // namespace | 18 | } // namespace |
| 19 | 19 | ||
| 20 | u32 OpusMultiStreamDecodeObject::GetWorkBufferSize(u32 total_stream_count, | 20 | u32 OpusMultiStreamDecodeObject::GetWorkBufferSize(u32 total_stream_count, |
| 21 | u32 stereo_stream_count) { | 21 | u32 stereo_stream_count) { |
| 22 | if (IsValidStreamCounts(total_stream_count, stereo_stream_count)) { | 22 | if (IsValidStreamCounts(total_stream_count, stereo_stream_count)) { |
| 23 | return static_cast<u32>(sizeof(OpusMultiStreamDecodeObject)) + | 23 | return static_cast<u32>(sizeof(OpusMultiStreamDecodeObject)) + |
| 24 | opus_multistream_decoder_get_size(total_stream_count, stereo_stream_count); | 24 | opus_multistream_decoder_get_size(total_stream_count, stereo_stream_count); |
| 25 | } | 25 | } |
| 26 | return 0; | 26 | return 0; |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | OpusMultiStreamDecodeObject& OpusMultiStreamDecodeObject::Initialize(u64 buffer, u64 buffer2) { | 29 | OpusMultiStreamDecodeObject& OpusMultiStreamDecodeObject::Initialize(u64 buffer, u64 buffer2) { |
| 30 | auto* new_decoder = reinterpret_cast<OpusMultiStreamDecodeObject*>(buffer); | 30 | auto* new_decoder = reinterpret_cast<OpusMultiStreamDecodeObject*>(buffer); |
| 31 | auto* comparison = reinterpret_cast<OpusMultiStreamDecodeObject*>(buffer2); | 31 | auto* comparison = reinterpret_cast<OpusMultiStreamDecodeObject*>(buffer2); |
| 32 | 32 | ||
| 33 | if (new_decoder->magic == DecodeMultiStreamObjectMagic) { | 33 | if (new_decoder->magic == DecodeMultiStreamObjectMagic) { |
| 34 | if (!new_decoder->initialized || | 34 | if (!new_decoder->initialized || |
| 35 | (new_decoder->initialized && new_decoder->self == comparison)) { | 35 | (new_decoder->initialized && new_decoder->self == comparison)) { |
| 36 | new_decoder->state_valid = true; | 36 | new_decoder->state_valid = true; |
| 37 | } | 37 | } |
| 38 | } else { | 38 | } else { |
| 39 | new_decoder->initialized = false; | 39 | new_decoder->initialized = false; |
| 40 | new_decoder->state_valid = true; | 40 | new_decoder->state_valid = true; |
| 41 | } | 41 | } |
| 42 | return *new_decoder; | 42 | return *new_decoder; |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | s32 OpusMultiStreamDecodeObject::InitializeDecoder(u32 sample_rate, u32 total_stream_count, | 45 | s32 OpusMultiStreamDecodeObject::InitializeDecoder(u32 sample_rate, u32 total_stream_count, |
| 46 | u32 channel_count, u32 stereo_stream_count, | 46 | u32 channel_count, u32 stereo_stream_count, |
| 47 | u8* mappings) { | 47 | u8* mappings) { |
| 48 | if (!state_valid) { | 48 | if (!state_valid) { |
| 49 | return OPUS_INVALID_STATE; | 49 | return OPUS_INVALID_STATE; |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | if (initialized) { | 52 | if (initialized) { |
| 53 | return OPUS_OK; | 53 | return OPUS_OK; |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | // See OpusDecodeObject::InitializeDecoder for an explanation of this | 56 | // See OpusDecodeObject::InitializeDecoder for an explanation of this |
| 57 | decoder = (LibOpusMSDecoder*)(this + 1); | 57 | decoder = (LibOpusMSDecoder*)(this + 1); |
| 58 | s32 ret = opus_multistream_decoder_init(decoder, sample_rate, channel_count, total_stream_count, | 58 | s32 ret = opus_multistream_decoder_init(decoder, sample_rate, channel_count, total_stream_count, |
| 59 | stereo_stream_count, mappings); | 59 | stereo_stream_count, mappings); |
| 60 | if (ret == OPUS_OK) { | 60 | if (ret == OPUS_OK) { |
| 61 | magic = DecodeMultiStreamObjectMagic; | 61 | magic = DecodeMultiStreamObjectMagic; |
| 62 | initialized = true; | 62 | initialized = true; |
| 63 | state_valid = true; | 63 | state_valid = true; |
| 64 | self = this; | 64 | self = this; |
| 65 | final_range = 0; | 65 | final_range = 0; |
| 66 | } | 66 | } |
| 67 | return ret; | 67 | return ret; |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | s32 OpusMultiStreamDecodeObject::Shutdown() { | 70 | s32 OpusMultiStreamDecodeObject::Shutdown() { |
| 71 | if (!state_valid) { | 71 | if (!state_valid) { |
| 72 | return OPUS_INVALID_STATE; | 72 | return OPUS_INVALID_STATE; |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | if (initialized) { | 75 | if (initialized) { |
| 76 | magic = 0x0; | 76 | magic = 0x0; |
| 77 | initialized = false; | 77 | initialized = false; |
| 78 | state_valid = false; | 78 | state_valid = false; |
| 79 | self = nullptr; | 79 | self = nullptr; |
| 80 | final_range = 0; | 80 | final_range = 0; |
| 81 | decoder = nullptr; | 81 | decoder = nullptr; |
| 82 | } | 82 | } |
| 83 | return OPUS_OK; | 83 | return OPUS_OK; |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | s32 OpusMultiStreamDecodeObject::ResetDecoder() { | 86 | s32 OpusMultiStreamDecodeObject::ResetDecoder() { |
| 87 | return opus_multistream_decoder_ctl(decoder, OPUS_RESET_STATE); | 87 | return opus_multistream_decoder_ctl(decoder, OPUS_RESET_STATE); |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | s32 OpusMultiStreamDecodeObject::Decode(u32& out_sample_count, u64 output_data, | 90 | s32 OpusMultiStreamDecodeObject::Decode(u32& out_sample_count, u64 output_data, |
| 91 | u64 output_data_size, u64 input_data, u64 input_data_size) { | 91 | u64 output_data_size, u64 input_data, u64 input_data_size) { |
| 92 | ASSERT(initialized); | 92 | ASSERT(initialized); |
| 93 | out_sample_count = 0; | 93 | out_sample_count = 0; |
| 94 | 94 | ||
| 95 | if (!state_valid) { | 95 | if (!state_valid) { |
| 96 | return OPUS_INVALID_STATE; | 96 | return OPUS_INVALID_STATE; |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | auto ret_code_or_samples = opus_multistream_decode( | 99 | auto ret_code_or_samples = opus_multistream_decode( |
| 100 | decoder, reinterpret_cast<const u8*>(input_data), static_cast<opus_int32>(input_data_size), | 100 | decoder, reinterpret_cast<const u8*>(input_data), static_cast<opus_int32>(input_data_size), |
| 101 | reinterpret_cast<opus_int16*>(output_data), static_cast<opus_int32>(output_data_size), 0); | 101 | reinterpret_cast<opus_int16*>(output_data), static_cast<opus_int32>(output_data_size), 0); |
| 102 | 102 | ||
| 103 | if (ret_code_or_samples < OPUS_OK) { | 103 | if (ret_code_or_samples < OPUS_OK) { |
| 104 | return ret_code_or_samples; | 104 | return ret_code_or_samples; |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | out_sample_count = ret_code_or_samples; | 107 | out_sample_count = ret_code_or_samples; |
| 108 | return opus_multistream_decoder_ctl(decoder, OPUS_GET_FINAL_RANGE_REQUEST, &final_range); | 108 | return opus_multistream_decoder_ctl(decoder, OPUS_GET_FINAL_RANGE_REQUEST, &final_range); |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | } // namespace AudioCore::ADSP::OpusDecoder | 111 | } // namespace AudioCore::ADSP::OpusDecoder |