From 06887c80a5e430d63f217a5cdd129f5e97034a36 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Jan 2019 15:31:28 -0500 Subject: [PATCH 1/5] hwopus: Fill in the rest of the unknown service function names Filled in via information provided by SwitchBrew. --- src/core/hle/service/audio/hwopus.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp index a850cadc87..abe21b992c 100644 --- a/src/core/hle/service/audio/hwopus.cpp +++ b/src/core/hle/service/audio/hwopus.cpp @@ -30,22 +30,24 @@ public: u32 channel_count) : ServiceFramework("IHardwareOpusDecoderManager"), decoder(std::move(decoder)), sample_rate(sample_rate), channel_count(channel_count) { + // clang-format off static const FunctionInfo functions[] = { - {0, &IHardwareOpusDecoderManager::DecodeInterleaved, "DecodeInterleaved"}, + {0, &IHardwareOpusDecoderManager::DecodeInterleavedOld, "DecodeInterleavedOld"}, {1, nullptr, "SetContext"}, - {2, nullptr, "DecodeInterleavedForMultiStream"}, + {2, nullptr, "DecodeInterleavedForMultiStreamOld"}, {3, nullptr, "SetContextForMultiStream"}, - {4, &IHardwareOpusDecoderManager::DecodeInterleavedWithPerformance, - "DecodeInterleavedWithPerformance"}, - {5, nullptr, "Unknown5"}, - {6, nullptr, "Unknown6"}, - {7, nullptr, "Unknown7"}, + {4, &IHardwareOpusDecoderManager::DecodeInterleavedWithPerfOld, "DecodeInterleavedWithPerfOld"}, + {5, nullptr, "DecodeInterleavedForMultiStreamWithPerfOld"}, + {6, nullptr, "DecodeInterleaved"}, + {7, nullptr, "DecodeInterleavedForMultiStream"}, }; + // clang-format on + RegisterHandlers(functions); } private: - void DecodeInterleaved(Kernel::HLERequestContext& ctx) { + void DecodeInterleavedOld(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Audio, "called"); u32 consumed = 0; @@ -65,7 +67,7 @@ private: ctx.WriteBuffer(samples.data(), samples.size() * sizeof(s16)); } - void DecodeInterleavedWithPerformance(Kernel::HLERequestContext& ctx) { + void DecodeInterleavedWithPerfOld(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Audio, "called"); u32 consumed = 0; From eb1a3c1f4a33eac87ac0fd3aa75f4f4e28a799ca Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Jan 2019 17:33:25 -0500 Subject: [PATCH 2/5] 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)) { From 44f39bfb68ad727cc13ca9bef6b01748c43e68c6 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Jan 2019 17:40:27 -0500 Subject: [PATCH 3/5] hwopus: Replace std::optional> with u64* This doesn't really offer anything over the use of a direct pointer, so we can just use that instead. --- src/core/hle/service/audio/hwopus.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp index 67aa9f6ad4..b70c831d2c 100644 --- a/src/core/hle/service/audio/hwopus.cpp +++ b/src/core/hle/service/audio/hwopus.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -53,7 +52,7 @@ private: u32 consumed = 0; u32 sample_count = 0; std::vector samples(ctx.GetWriteBufferSize() / sizeof(opus_int16)); - if (!Decoder_DecodeInterleaved(consumed, sample_count, ctx.ReadBuffer(), samples)) { + if (!Decoder_DecodeInterleaved(consumed, sample_count, ctx.ReadBuffer(), samples, nullptr)) { LOG_ERROR(Audio, "Failed to decode opus data"); IPC::ResponseBuilder rb{ctx, 2}; // TODO(ogniK): Use correct error code @@ -75,7 +74,7 @@ private: u64 performance = 0; std::vector samples(ctx.GetWriteBufferSize() / sizeof(opus_int16)); if (!Decoder_DecodeInterleaved(consumed, sample_count, ctx.ReadBuffer(), samples, - performance)) { + &performance)) { LOG_ERROR(Audio, "Failed to decode opus data"); IPC::ResponseBuilder rb{ctx, 2}; // TODO(ogniK): Use correct error code @@ -90,10 +89,8 @@ private: ctx.WriteBuffer(samples.data(), samples.size() * sizeof(s16)); } - bool Decoder_DecodeInterleaved( - u32& consumed, u32& sample_count, const std::vector& input, - std::vector& output, - std::optional> performance_time = std::nullopt) { + bool Decoder_DecodeInterleaved(u32& consumed, u32& sample_count, const std::vector& input, + std::vector& output, u64* out_performance_time) { const auto start_time = std::chrono::high_resolution_clock::now(); const std::size_t raw_output_sz = output.size() * sizeof(opus_int16); if (sizeof(OpusHeader) > input.size()) { @@ -136,8 +133,8 @@ private: const auto end_time = std::chrono::high_resolution_clock::now() - start_time; sample_count = out_sample_count; consumed = static_cast(sizeof(OpusHeader) + hdr.sz); - if (performance_time.has_value()) { - performance_time->get() = + if (out_performance_time != nullptr) { + *out_performance_time = std::chrono::duration_cast(end_time).count(); } From 07b86dc28cbddacedbb6f5985f9c522f88e68b1a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Jan 2019 18:15:29 -0500 Subject: [PATCH 4/5] hwopus: Deduplicate the decoding code within DecodeInterleavedOld and DecodeInterleavedWithPerfOld Keeps the logic in one spot for use by both functions. --- src/core/hle/service/audio/hwopus.cpp | 33 ++++++++++++--------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp index b70c831d2c..dffb890d51 100644 --- a/src/core/hle/service/audio/hwopus.cpp +++ b/src/core/hle/service/audio/hwopus.cpp @@ -49,43 +49,38 @@ private: void DecodeInterleavedOld(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Audio, "called"); - u32 consumed = 0; - u32 sample_count = 0; - std::vector samples(ctx.GetWriteBufferSize() / sizeof(opus_int16)); - if (!Decoder_DecodeInterleaved(consumed, sample_count, ctx.ReadBuffer(), samples, nullptr)) { - LOG_ERROR(Audio, "Failed to decode opus data"); - IPC::ResponseBuilder rb{ctx, 2}; - // TODO(ogniK): Use correct error code - rb.Push(ResultCode(-1)); - return; - } - IPC::ResponseBuilder rb{ctx, 4}; - rb.Push(RESULT_SUCCESS); - rb.Push(consumed); - rb.Push(sample_count); - ctx.WriteBuffer(samples.data(), samples.size() * sizeof(s16)); + DecodeInterleavedHelper(ctx, nullptr); } void DecodeInterleavedWithPerfOld(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Audio, "called"); + u64 performance = 0; + DecodeInterleavedHelper(ctx, &performance); + } + + void DecodeInterleavedHelper(Kernel::HLERequestContext& ctx, u64* performance) { u32 consumed = 0; u32 sample_count = 0; - u64 performance = 0; std::vector samples(ctx.GetWriteBufferSize() / sizeof(opus_int16)); + if (!Decoder_DecodeInterleaved(consumed, sample_count, ctx.ReadBuffer(), samples, - &performance)) { + performance)) { LOG_ERROR(Audio, "Failed to decode opus data"); IPC::ResponseBuilder rb{ctx, 2}; // TODO(ogniK): Use correct error code rb.Push(ResultCode(-1)); return; } - IPC::ResponseBuilder rb{ctx, 6}; + + const u32 param_size = performance != nullptr ? 6 : 4; + IPC::ResponseBuilder rb{ctx, param_size}; rb.Push(RESULT_SUCCESS); rb.Push(consumed); rb.Push(sample_count); - rb.Push(performance); + if (performance) { + rb.Push(*performance); + } ctx.WriteBuffer(samples.data(), samples.size() * sizeof(s16)); } From a897feb21e603a7844c44a8ef5e00ce42ab96b43 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 29 Jan 2019 18:21:17 -0500 Subject: [PATCH 5/5] hwopus: Implement DecodeInterleaved This functions almost identically to DecodeInterleavedWithPerfOld, however this function also has the ability to reset the decoder context. This is documented as a potentially desirable thing in the libopus manual in some circumstances as it says for the OPUS_RESET_STATE ctl: "This should be called when switching streams in order to prevent the back to back decoding from giving different result from one at a time decoding." --- src/core/hle/service/audio/hwopus.cpp | 39 ++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp index dffb890d51..11eba4a129 100644 --- a/src/core/hle/service/audio/hwopus.cpp +++ b/src/core/hle/service/audio/hwopus.cpp @@ -37,7 +37,7 @@ public: {3, nullptr, "SetContextForMultiStream"}, {4, &IHardwareOpusDecoderManager::DecodeInterleavedWithPerfOld, "DecodeInterleavedWithPerfOld"}, {5, nullptr, "DecodeInterleavedForMultiStreamWithPerfOld"}, - {6, nullptr, "DecodeInterleaved"}, + {6, &IHardwareOpusDecoderManager::DecodeInterleaved, "DecodeInterleaved"}, {7, nullptr, "DecodeInterleavedForMultiStream"}, }; // clang-format on @@ -46,24 +46,49 @@ public: } private: + /// Describes extra behavior that may be asked of the decoding context. + enum class ExtraBehavior { + /// No extra behavior. + None, + + /// Resets the decoder context back to a freshly initialized state. + ResetContext, + }; + void DecodeInterleavedOld(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Audio, "called"); - DecodeInterleavedHelper(ctx, nullptr); + DecodeInterleavedHelper(ctx, nullptr, ExtraBehavior::None); } void DecodeInterleavedWithPerfOld(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Audio, "called"); u64 performance = 0; - DecodeInterleavedHelper(ctx, &performance); + DecodeInterleavedHelper(ctx, &performance, ExtraBehavior::None); } - void DecodeInterleavedHelper(Kernel::HLERequestContext& ctx, u64* performance) { + void DecodeInterleaved(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Audio, "called"); + + IPC::RequestParser rp{ctx}; + const auto extra_behavior = + rp.Pop() ? ExtraBehavior::ResetContext : ExtraBehavior::None; + + u64 performance = 0; + DecodeInterleavedHelper(ctx, &performance, extra_behavior); + } + + void DecodeInterleavedHelper(Kernel::HLERequestContext& ctx, u64* performance, + ExtraBehavior extra_behavior) { u32 consumed = 0; u32 sample_count = 0; std::vector samples(ctx.GetWriteBufferSize() / sizeof(opus_int16)); + if (extra_behavior == ExtraBehavior::ResetContext) { + ResetDecoderContext(); + } + if (!Decoder_DecodeInterleaved(consumed, sample_count, ctx.ReadBuffer(), samples, performance)) { LOG_ERROR(Audio, "Failed to decode opus data"); @@ -136,6 +161,12 @@ private: return true; } + void ResetDecoderContext() { + ASSERT(decoder != nullptr); + + opus_decoder_ctl(decoder.get(), OPUS_RESET_STATE); + } + struct OpusHeader { u32_be sz; // Needs to be BE for some odd reason INSERT_PADDING_WORDS(1);