From eb1a3c1f4a33eac87ac0fd3aa75f4f4e28a799ca Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Jan 2019 17:33:25 -0500 Subject: [PATCH] hwopus: Mark local variables as const where applicable Makes non-mutable state more explicit. --- src/core/hle/service/audio/hwopus.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp index abe21b992c..67aa9f6ad4 100644 --- a/src/core/hle/service/audio/hwopus.cpp +++ b/src/core/hle/service/audio/hwopus.cpp @@ -95,12 +95,13 @@ private: std::vector& output, std::optional> performance_time = std::nullopt) { const auto start_time = std::chrono::high_resolution_clock::now(); - std::size_t raw_output_sz = output.size() * sizeof(opus_int16); + const std::size_t raw_output_sz = output.size() * sizeof(opus_int16); if (sizeof(OpusHeader) > input.size()) { LOG_ERROR(Audio, "Input is smaller than the header size, header_sz={}, input_sz={}", sizeof(OpusHeader), input.size()); return false; } + OpusHeader hdr{}; std::memcpy(&hdr, input.data(), sizeof(OpusHeader)); if (sizeof(OpusHeader) + static_cast(hdr.sz) > input.size()) { @@ -108,8 +109,9 @@ private: sizeof(OpusHeader) + static_cast(hdr.sz), input.size()); return false; } - auto frame = input.data() + sizeof(OpusHeader); - auto decoded_sample_count = opus_packet_get_nb_samples( + + const auto frame = input.data() + sizeof(OpusHeader); + const auto decoded_sample_count = opus_packet_get_nb_samples( frame, static_cast(input.size() - sizeof(OpusHeader)), static_cast(sample_rate)); if (decoded_sample_count * channel_count * sizeof(u16) > raw_output_sz) { @@ -119,8 +121,9 @@ private: decoded_sample_count * channel_count * sizeof(u16), raw_output_sz); return false; } + const int frame_size = (static_cast(raw_output_sz / sizeof(s16) / channel_count)); - auto out_sample_count = + const auto out_sample_count = opus_decode(decoder.get(), frame, hdr.sz, output.data(), frame_size, 0); if (out_sample_count < 0) { LOG_ERROR(Audio, @@ -129,6 +132,7 @@ private: out_sample_count, frame_size, static_cast(hdr.sz)); return false; } + const auto end_time = std::chrono::high_resolution_clock::now() - start_time; sample_count = out_sample_count; consumed = static_cast(sizeof(OpusHeader) + hdr.sz); @@ -136,6 +140,7 @@ private: performance_time->get() = std::chrono::duration_cast(end_time).count(); } + return true; } @@ -159,6 +164,7 @@ void HwOpus::GetWorkBufferSize(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; const auto sample_rate = rp.Pop(); const auto channel_count = rp.Pop(); + LOG_DEBUG(Audio, "called with sample_rate={}, channel_count={}", sample_rate, channel_count); ASSERT_MSG(sample_rate == 48000 || sample_rate == 24000 || sample_rate == 16000 || @@ -176,9 +182,10 @@ void HwOpus::GetWorkBufferSize(Kernel::HLERequestContext& ctx) { void HwOpus::OpenOpusDecoder(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - auto sample_rate = rp.Pop(); - auto channel_count = rp.Pop(); - auto buffer_sz = rp.Pop(); + const auto sample_rate = rp.Pop(); + const auto channel_count = rp.Pop(); + const auto buffer_sz = rp.Pop(); + LOG_DEBUG(Audio, "called sample_rate={}, channel_count={}, buffer_size={}", sample_rate, channel_count, buffer_sz); @@ -187,8 +194,9 @@ void HwOpus::OpenOpusDecoder(Kernel::HLERequestContext& ctx) { "Invalid sample rate"); ASSERT_MSG(channel_count == 1 || channel_count == 2, "Invalid channel count"); - std::size_t worker_sz = WorkerBufferSize(channel_count); + const std::size_t worker_sz = WorkerBufferSize(channel_count); ASSERT_MSG(buffer_sz >= worker_sz, "Worker buffer too large"); + std::unique_ptr decoder{ static_cast(operator new(worker_sz))}; if (const int err = opus_decoder_init(decoder.get(), sample_rate, channel_count)) {