diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt index 2caed12337..3cb9b538d6 100644 --- a/src/audio_core/CMakeLists.txt +++ b/src/audio_core/CMakeLists.txt @@ -71,6 +71,14 @@ elseif(ENABLE_FDK) target_compile_definitions(audio_core PUBLIC HAVE_FDK) endif() +if(ANDROID) + target_sources(audio_core PRIVATE + hle/mediandk_decoder.cpp + hle/mediandk_decoder.h + ) + target_link_libraries(audio_core PRIVATE mediandk) +endif() + if(SDL2_FOUND) target_link_libraries(audio_core PRIVATE SDL2) target_compile_definitions(audio_core PRIVATE HAVE_SDL2) @@ -80,4 +88,3 @@ if(ENABLE_CUBEB) target_link_libraries(audio_core PRIVATE cubeb) target_compile_definitions(audio_core PUBLIC HAVE_CUBEB) endif() - diff --git a/src/audio_core/hle/hle.cpp b/src/audio_core/hle/hle.cpp index 052e507c50..513ca45f39 100644 --- a/src/audio_core/hle/hle.cpp +++ b/src/audio_core/hle/hle.cpp @@ -7,6 +7,8 @@ #include "audio_core/hle/wmf_decoder.h" #elif HAVE_FFMPEG #include "audio_core/hle/ffmpeg_decoder.h" +#elif ANDROID +#include "audio_core/hle/mediandk_decoder.h" #elif HAVE_FDK #include "audio_core/hle/fdk_decoder.h" #endif @@ -99,6 +101,8 @@ DspHle::Impl::Impl(DspHle& parent_, Memory::MemorySystem& memory) : parent(paren decoder = std::make_unique(memory); #elif defined(HAVE_FFMPEG) decoder = std::make_unique(memory); +#elif ANDROID + decoder = std::make_unique(memory); #elif defined(HAVE_FDK) decoder = std::make_unique(memory); #else diff --git a/src/audio_core/hle/mediandk_decoder.cpp b/src/audio_core/hle/mediandk_decoder.cpp new file mode 100644 index 0000000000..f731258b01 --- /dev/null +++ b/src/audio_core/hle/mediandk_decoder.cpp @@ -0,0 +1,241 @@ +// Copyright 2019 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include +#include + +#include +#include + +#include "audio_core/hle/adts.h" +#include "audio_core/hle/mediandk_decoder.h" + +namespace AudioCore::HLE { + +struct AMediaCodecRelease { + void operator()(AMediaCodec* codec) const { + AMediaCodec_stop(codec); + AMediaCodec_delete(codec); + }; +}; + +class MediaNDKDecoder::Impl { +public: + explicit Impl(Memory::MemorySystem& memory); + ~Impl(); + std::optional ProcessRequest(const BinaryRequest& request); + + bool SetMediaType(const ADTSData& adts_data); + +private: + std::optional Initalize(const BinaryRequest& request); + std::optional Decode(const BinaryRequest& request); + + Memory::MemorySystem& mMemory; + std::unique_ptr mDecoder; + // default: 2 channles, 48000 samplerate + ADTSData mADTSData{/* MPEG2 */ false, /*profile*/ 2, /*channels*/ 2, + /*channel_idx*/ 2, /*framecount*/ 0, /*samplerate_idx*/ 3, + /*length*/ 0, /*samplerate*/ 48000}; +}; + +MediaNDKDecoder::Impl::Impl(Memory::MemorySystem& memory) : mMemory(memory) { + SetMediaType(mADTSData); +} + +MediaNDKDecoder::Impl::~Impl() = default; + +std::optional MediaNDKDecoder::Impl::Initalize(const BinaryRequest& request) { + BinaryResponse response; + std::memcpy(&response, &request, sizeof(response)); + response.unknown1 = 0x0; + return response; +} + +bool MediaNDKDecoder::Impl::SetMediaType(const ADTSData& adts_data) { + const char* mime = "audio/mp4a-latm"; + if (mDecoder && mADTSData.profile == adts_data.profile && + mADTSData.channel_idx == adts_data.channel_idx && + mADTSData.samplerate_idx == adts_data.samplerate_idx) { + return true; + } + mDecoder.reset(AMediaCodec_createDecoderByType(mime)); + if (mDecoder == nullptr) { + return false; + } + + u8 csd_0[2]; + csd_0[0] = static_cast((adts_data.profile << 3) | (adts_data.samplerate_idx >> 1)); + csd_0[1] = + static_cast(((adts_data.samplerate_idx << 7) & 0x80) | (adts_data.channel_idx << 3)); + AMediaFormat* format = AMediaFormat_new(); + AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, mime); + AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, adts_data.samplerate); + AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, adts_data.channels); + AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_IS_ADTS, 1); + AMediaFormat_setBuffer(format, "csd-0", csd_0, sizeof(csd_0)); + + media_status_t status = AMediaCodec_configure(mDecoder.get(), format, NULL, NULL, 0); + if (status != AMEDIA_OK) { + AMediaFormat_delete(format); + mDecoder.reset(); + return false; + } + + status = AMediaCodec_start(mDecoder.get()); + if (status != AMEDIA_OK) { + AMediaFormat_delete(format); + mDecoder.reset(); + return false; + } + + AMediaFormat_delete(format); + mADTSData = adts_data; + return true; +} + +std::optional MediaNDKDecoder::Impl::ProcessRequest(const BinaryRequest& request) { + if (request.codec != DecoderCodec::AAC) { + LOG_ERROR(Audio_DSP, "AAC Decoder cannot handle such codec: {}", + static_cast(request.codec)); + return {}; + } + + switch (request.cmd) { + case DecoderCommand::Init: { + return Initalize(request); + } + case DecoderCommand::Decode: { + return Decode(request); + } + case DecoderCommand::Unknown: { + BinaryResponse response; + std::memcpy(&response, &request, sizeof(response)); + response.unknown1 = 0x0; + return response; + } + default: + LOG_ERROR(Audio_DSP, "Got unknown binary request: {}", static_cast(request.cmd)); + return {}; + } +} + +std::optional MediaNDKDecoder::Impl::Decode(const BinaryRequest& request) { + BinaryResponse response; + response.codec = request.codec; + response.cmd = request.cmd; + response.size = request.size; + response.num_samples = 1024; + + if (request.src_addr < Memory::FCRAM_PADDR || + request.src_addr + request.size > Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) { + LOG_ERROR(Audio_DSP, "Got out of bounds src_addr {:08x}", request.src_addr); + return response; + } + + u8* data = mMemory.GetFCRAMPointer(request.src_addr - Memory::FCRAM_PADDR); + ADTSData adts_data = ParseADTS(reinterpret_cast(data)); + SetMediaType(adts_data); + response.num_channels = adts_data.channels; + if (!mDecoder) { + LOG_ERROR(Audio_DSP, "Missing decoder for profile: {}, channels: {}, samplerate: {}", + adts_data.profile, adts_data.channels, adts_data.samplerate); + return {}; + } + + // input + constexpr int timeout = 160; + std::size_t buffer_size = 0; + u8* buffer = nullptr; + ssize_t buffer_index = AMediaCodec_dequeueInputBuffer(mDecoder.get(), timeout); + if (buffer_index < 0) { + LOG_ERROR(Audio_DSP, "Failed to enqueue the input samples: {}", buffer_index); + return response; + } + buffer = AMediaCodec_getInputBuffer(mDecoder.get(), buffer_index, &buffer_size); + if (buffer_size < request.size) { + return response; + } + std::memcpy(buffer, data, request.size); + media_status_t status = + AMediaCodec_queueInputBuffer(mDecoder.get(), buffer_index, 0, request.size, 0, 0); + if (status != AMEDIA_OK) { + LOG_WARNING(Audio_DSP, "Try queue input buffer again later!"); + return response; + } + + // output + AMediaCodecBufferInfo info; + std::array, 2> out_streams; + buffer_index = AMediaCodec_dequeueOutputBuffer(mDecoder.get(), &info, timeout); + switch (buffer_index) { + case AMEDIACODEC_INFO_TRY_AGAIN_LATER: + LOG_WARNING(Audio_DSP, "Failed to dequeue output buffer: timeout!"); + break; + case AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED: + LOG_WARNING(Audio_DSP, "Failed to dequeue output buffer: buffers changed!"); + break; + case AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED: { + AMediaFormat* format = AMediaCodec_getOutputFormat(mDecoder.get()); + LOG_WARNING(Audio_DSP, "output format: {}", AMediaFormat_toString(format)); + AMediaFormat_delete(format); + buffer_index = AMediaCodec_dequeueOutputBuffer(mDecoder.get(), &info, timeout); + } + default: { + int offset = info.offset; + buffer = AMediaCodec_getOutputBuffer(mDecoder.get(), buffer_index, &buffer_size); + while (offset < info.size) { + for (int channel = 0; channel < response.num_channels; channel++) { + u16 pcm_data; + std::memcpy(&pcm_data, buffer + offset, sizeof(pcm_data)); + out_streams[channel].push_back(pcm_data); + offset += sizeof(pcm_data); + } + } + AMediaCodec_releaseOutputBuffer(mDecoder.get(), buffer_index, info.size != 0); + } + } + + // transfer the decoded buffer from vector to the FCRAM + size_t stream0_size = out_streams[0].size() * sizeof(u16); + if (stream0_size != 0) { + if (request.dst_addr_ch0 < Memory::FCRAM_PADDR || + request.dst_addr_ch0 + stream0_size > + Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) { + LOG_ERROR(Audio_DSP, "Got out of bounds dst_addr_ch0 {:08x}", request.dst_addr_ch0); + return response; + } + std::memcpy(mMemory.GetFCRAMPointer(request.dst_addr_ch0 - Memory::FCRAM_PADDR), + out_streams[0].data(), stream0_size); + } + + size_t stream1_size = out_streams[1].size() * sizeof(u16); + if (stream1_size != 0) { + if (request.dst_addr_ch1 < Memory::FCRAM_PADDR || + request.dst_addr_ch1 + stream1_size > + Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) { + LOG_ERROR(Audio_DSP, "Got out of bounds dst_addr_ch1 {:08x}", request.dst_addr_ch1); + return response; + } + std::memcpy(mMemory.GetFCRAMPointer(request.dst_addr_ch1 - Memory::FCRAM_PADDR), + out_streams[1].data(), stream1_size); + } + return response; +} + +MediaNDKDecoder::MediaNDKDecoder(Memory::MemorySystem& memory) + : impl(std::make_unique(memory)) {} + +MediaNDKDecoder::~MediaNDKDecoder() = default; + +std::optional MediaNDKDecoder::ProcessRequest(const BinaryRequest& request) { + return impl->ProcessRequest(request); +} + +bool MediaNDKDecoder::IsValid() const { + return true; +} + +} // namespace AudioCore::HLE diff --git a/src/audio_core/hle/mediandk_decoder.h b/src/audio_core/hle/mediandk_decoder.h new file mode 100644 index 0000000000..1de1e15f74 --- /dev/null +++ b/src/audio_core/hle/mediandk_decoder.h @@ -0,0 +1,22 @@ +// Copyright 2019 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. +#pragma once + +#include "audio_core/hle/decoder.h" + +namespace AudioCore::HLE { + +class MediaNDKDecoder final : public DecoderBase { +public: + explicit MediaNDKDecoder(Memory::MemorySystem& memory); + ~MediaNDKDecoder() override; + std::optional ProcessRequest(const BinaryRequest& request) override; + bool IsValid() const override; + +private: + class Impl; + std::unique_ptr impl; +}; + +} // namespace AudioCore::HLE