hwopus.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include <cstring>
  6. #include <memory>
  7. #include <optional>
  8. #include <vector>
  9. #include <opus.h>
  10. #include "common/common_funcs.h"
  11. #include "common/logging/log.h"
  12. #include "core/hle/ipc_helpers.h"
  13. #include "core/hle/kernel/hle_ipc.h"
  14. #include "core/hle/service/audio/hwopus.h"
  15. namespace Service::Audio {
  16. struct OpusDeleter {
  17. void operator()(void* ptr) const {
  18. operator delete(ptr);
  19. }
  20. };
  21. class IHardwareOpusDecoderManager final : public ServiceFramework<IHardwareOpusDecoderManager> {
  22. public:
  23. IHardwareOpusDecoderManager(std::unique_ptr<OpusDecoder, OpusDeleter> decoder, u32 sample_rate,
  24. u32 channel_count)
  25. : ServiceFramework("IHardwareOpusDecoderManager"), decoder(std::move(decoder)),
  26. sample_rate(sample_rate), channel_count(channel_count) {
  27. static const FunctionInfo functions[] = {
  28. {0, &IHardwareOpusDecoderManager::DecodeInterleaved, "DecodeInterleaved"},
  29. {1, nullptr, "SetContext"},
  30. {2, nullptr, "DecodeInterleavedForMultiStream"},
  31. {3, nullptr, "SetContextForMultiStream"},
  32. {4, &IHardwareOpusDecoderManager::DecodeInterleavedWithPerformance,
  33. "DecodeInterleavedWithPerformance"},
  34. {5, nullptr, "Unknown5"},
  35. {6, nullptr, "Unknown6"},
  36. {7, nullptr, "Unknown7"},
  37. };
  38. RegisterHandlers(functions);
  39. }
  40. private:
  41. void DecodeInterleaved(Kernel::HLERequestContext& ctx) {
  42. u32 consumed = 0;
  43. u32 sample_count = 0;
  44. std::vector<opus_int16> samples(ctx.GetWriteBufferSize() / sizeof(opus_int16));
  45. if (!Decoder_DecodeInterleaved(consumed, sample_count, ctx.ReadBuffer(), samples)) {
  46. IPC::ResponseBuilder rb{ctx, 2};
  47. // TODO(ogniK): Use correct error code
  48. rb.Push(ResultCode(-1));
  49. return;
  50. }
  51. IPC::ResponseBuilder rb{ctx, 4};
  52. rb.Push(RESULT_SUCCESS);
  53. rb.Push<u32>(consumed);
  54. rb.Push<u32>(sample_count);
  55. ctx.WriteBuffer(samples.data(), samples.size() * sizeof(s16));
  56. }
  57. void DecodeInterleavedWithPerformance(Kernel::HLERequestContext& ctx) {
  58. u32 consumed = 0;
  59. u32 sample_count = 0;
  60. u64 performance = 0;
  61. std::vector<opus_int16> samples(ctx.GetWriteBufferSize() / sizeof(opus_int16));
  62. if (!Decoder_DecodeInterleaved(consumed, sample_count, ctx.ReadBuffer(), samples,
  63. performance)) {
  64. IPC::ResponseBuilder rb{ctx, 2};
  65. // TODO(ogniK): Use correct error code
  66. rb.Push(ResultCode(-1));
  67. return;
  68. }
  69. IPC::ResponseBuilder rb{ctx, 6};
  70. rb.Push(RESULT_SUCCESS);
  71. rb.Push<u32>(consumed);
  72. rb.Push<u64>(performance);
  73. rb.Push<u32>(sample_count);
  74. ctx.WriteBuffer(samples.data(), samples.size() * sizeof(s16));
  75. }
  76. bool Decoder_DecodeInterleaved(
  77. u32& consumed, u32& sample_count, const std::vector<u8>& input,
  78. std::vector<opus_int16>& output,
  79. std::optional<std::reference_wrapper<u64>> performance_time = std::nullopt) {
  80. const auto start_time = std::chrono::high_resolution_clock::now();
  81. std::size_t raw_output_sz = output.size() * sizeof(opus_int16);
  82. if (sizeof(OpusHeader) > input.size())
  83. return false;
  84. OpusHeader hdr{};
  85. std::memcpy(&hdr, input.data(), sizeof(OpusHeader));
  86. if (sizeof(OpusHeader) + static_cast<u32>(hdr.sz) > input.size()) {
  87. return false;
  88. }
  89. auto frame = input.data() + sizeof(OpusHeader);
  90. auto decoded_sample_count = opus_packet_get_nb_samples(
  91. frame, static_cast<opus_int32>(input.size() - sizeof(OpusHeader)),
  92. static_cast<opus_int32>(sample_rate));
  93. if (decoded_sample_count * channel_count * sizeof(u16) > raw_output_sz)
  94. return false;
  95. auto out_sample_count =
  96. opus_decode(decoder.get(), frame, hdr.sz, output.data(),
  97. (static_cast<int>(raw_output_sz / sizeof(s16) / channel_count)), 0);
  98. if (out_sample_count < 0)
  99. return false;
  100. const auto end_time = std::chrono::high_resolution_clock::now() - start_time;
  101. sample_count = out_sample_count;
  102. consumed = static_cast<u32>(sizeof(OpusHeader) + hdr.sz);
  103. if (performance_time.has_value()) {
  104. performance_time->get() =
  105. std::chrono::duration_cast<std::chrono::milliseconds>(end_time).count();
  106. }
  107. return true;
  108. }
  109. struct OpusHeader {
  110. u32_be sz; // Needs to be BE for some odd reason
  111. INSERT_PADDING_WORDS(1);
  112. };
  113. static_assert(sizeof(OpusHeader) == 0x8, "OpusHeader is an invalid size");
  114. std::unique_ptr<OpusDecoder, OpusDeleter> decoder;
  115. u32 sample_rate;
  116. u32 channel_count;
  117. };
  118. static std::size_t WorkerBufferSize(u32 channel_count) {
  119. ASSERT_MSG(channel_count == 1 || channel_count == 2, "Invalid channel count");
  120. return opus_decoder_get_size(static_cast<int>(channel_count));
  121. }
  122. void HwOpus::GetWorkBufferSize(Kernel::HLERequestContext& ctx) {
  123. IPC::RequestParser rp{ctx};
  124. auto sample_rate = rp.Pop<u32>();
  125. auto channel_count = rp.Pop<u32>();
  126. ASSERT_MSG(sample_rate == 48000 || sample_rate == 24000 || sample_rate == 16000 ||
  127. sample_rate == 12000 || sample_rate == 8000,
  128. "Invalid sample rate");
  129. ASSERT_MSG(channel_count == 1 || channel_count == 2, "Invalid channel count");
  130. u32 worker_buffer_sz = static_cast<u32>(WorkerBufferSize(channel_count));
  131. LOG_DEBUG(Audio, "called worker_buffer_sz={}", worker_buffer_sz);
  132. IPC::ResponseBuilder rb{ctx, 3};
  133. rb.Push(RESULT_SUCCESS);
  134. rb.Push<u32>(worker_buffer_sz);
  135. }
  136. void HwOpus::OpenOpusDecoder(Kernel::HLERequestContext& ctx) {
  137. IPC::RequestParser rp{ctx};
  138. auto sample_rate = rp.Pop<u32>();
  139. auto channel_count = rp.Pop<u32>();
  140. auto buffer_sz = rp.Pop<u32>();
  141. LOG_DEBUG(Audio, "called sample_rate={}, channel_count={}, buffer_size={}", sample_rate,
  142. channel_count, buffer_sz);
  143. ASSERT_MSG(sample_rate == 48000 || sample_rate == 24000 || sample_rate == 16000 ||
  144. sample_rate == 12000 || sample_rate == 8000,
  145. "Invalid sample rate");
  146. ASSERT_MSG(channel_count == 1 || channel_count == 2, "Invalid channel count");
  147. std::size_t worker_sz = WorkerBufferSize(channel_count);
  148. ASSERT_MSG(buffer_sz >= worker_sz, "Worker buffer too large");
  149. std::unique_ptr<OpusDecoder, OpusDeleter> decoder{
  150. static_cast<OpusDecoder*>(operator new(worker_sz))};
  151. if (opus_decoder_init(decoder.get(), sample_rate, channel_count)) {
  152. IPC::ResponseBuilder rb{ctx, 2};
  153. // TODO(ogniK): Use correct error code
  154. rb.Push(ResultCode(-1));
  155. return;
  156. }
  157. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  158. rb.Push(RESULT_SUCCESS);
  159. rb.PushIpcInterface<IHardwareOpusDecoderManager>(std::move(decoder), sample_rate,
  160. channel_count);
  161. }
  162. HwOpus::HwOpus() : ServiceFramework("hwopus") {
  163. static const FunctionInfo functions[] = {
  164. {0, &HwOpus::OpenOpusDecoder, "OpenOpusDecoder"},
  165. {1, &HwOpus::GetWorkBufferSize, "GetWorkBufferSize"},
  166. {2, nullptr, "OpenOpusDecoderForMultiStream"},
  167. {3, nullptr, "GetWorkBufferSizeForMultiStream"},
  168. };
  169. RegisterHandlers(functions);
  170. }
  171. HwOpus::~HwOpus() = default;
  172. } // namespace Service::Audio