hwopus.cpp 8.5 KB

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