2020-02-23 11:01:21 +01:00
|
|
|
// Copyright 2019 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "audio_core/hle/fdk_decoder.h"
|
2023-06-17 01:06:18 +02:00
|
|
|
#include "common/dynamic_library/fdk-aac.h"
|
|
|
|
|
|
|
|
using namespace DynamicLibrary;
|
2020-02-23 11:01:21 +01:00
|
|
|
|
|
|
|
namespace AudioCore::HLE {
|
|
|
|
|
|
|
|
class FDKDecoder::Impl {
|
|
|
|
public:
|
|
|
|
explicit Impl(Memory::MemorySystem& memory);
|
|
|
|
~Impl();
|
2023-05-14 18:55:10 +02:00
|
|
|
std::optional<BinaryMessage> ProcessRequest(const BinaryMessage& request);
|
2020-02-23 11:01:21 +01:00
|
|
|
bool IsValid() const {
|
|
|
|
return decoder != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-05-14 18:55:10 +02:00
|
|
|
std::optional<BinaryMessage> Initalize(const BinaryMessage& request);
|
2020-02-23 11:01:21 +01:00
|
|
|
|
2023-05-14 18:55:10 +02:00
|
|
|
std::optional<BinaryMessage> Decode(const BinaryMessage& request);
|
2020-02-23 11:01:21 +01:00
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
Memory::MemorySystem& memory;
|
|
|
|
|
|
|
|
HANDLE_AACDECODER decoder = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
FDKDecoder::Impl::Impl(Memory::MemorySystem& memory) : memory(memory) {
|
2023-06-17 01:06:18 +02:00
|
|
|
if (!FdkAac::LoadFdkAac()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:01:21 +01:00
|
|
|
// allocate an array of LIB_INFO structures
|
|
|
|
// if we don't pre-fill the whole segment with zeros, when we call `aacDecoder_GetLibInfo`
|
|
|
|
// it will segfault, upon investigation, there is some code in fdk_aac depends on your initial
|
|
|
|
// values in this array
|
|
|
|
LIB_INFO decoder_info[FDK_MODULE_LAST] = {};
|
|
|
|
// get library information and fill the struct
|
2023-06-17 01:06:18 +02:00
|
|
|
if (FdkAac::aacDecoder_GetLibInfo(decoder_info) != 0) {
|
2020-02-23 11:01:21 +01:00
|
|
|
LOG_ERROR(Audio_DSP, "Failed to retrieve fdk_aac library information!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_INFO(Audio_DSP, "Using fdk_aac version {} (build date: {})", decoder_info[0].versionStr,
|
|
|
|
decoder_info[0].build_date);
|
|
|
|
|
|
|
|
// choose the input format when initializing: 1 layer of ADTS
|
2023-06-17 01:06:18 +02:00
|
|
|
decoder = FdkAac::aacDecoder_Open(TRANSPORT_TYPE::TT_MP4_ADTS, 1);
|
2020-02-23 11:01:21 +01:00
|
|
|
// set maximum output channel to two (stereo)
|
|
|
|
// if the input samples have more channels, fdk_aac will perform a downmix
|
2023-06-17 01:06:18 +02:00
|
|
|
AAC_DECODER_ERROR ret = FdkAac::aacDecoder_SetParam(decoder, AAC_PCM_MAX_OUTPUT_CHANNELS, 2);
|
2020-02-23 11:01:21 +01:00
|
|
|
if (ret != AAC_DEC_OK) {
|
|
|
|
// unable to set this parameter reflects the decoder implementation might be broken
|
|
|
|
// we'd better shuts down everything
|
2023-06-17 01:06:18 +02:00
|
|
|
FdkAac::aacDecoder_Close(decoder);
|
2020-02-23 11:01:21 +01:00
|
|
|
decoder = nullptr;
|
|
|
|
LOG_ERROR(Audio_DSP, "Unable to set downmix parameter: {}", ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 18:55:10 +02:00
|
|
|
std::optional<BinaryMessage> FDKDecoder::Impl::Initalize(const BinaryMessage& request) {
|
|
|
|
BinaryMessage response = request;
|
|
|
|
response.header.result = ResultStatus::Success;
|
2020-02-23 11:01:21 +01:00
|
|
|
|
|
|
|
if (decoder) {
|
|
|
|
LOG_INFO(Audio_DSP, "FDK Decoder initialized");
|
|
|
|
Clear();
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Audio_DSP, "Decoder not initialized");
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
FDKDecoder::Impl::~Impl() {
|
2023-06-17 01:06:18 +02:00
|
|
|
if (decoder) {
|
|
|
|
FdkAac::aacDecoder_Close(decoder);
|
|
|
|
}
|
2020-02-23 11:01:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FDKDecoder::Impl::Clear() {
|
|
|
|
s16 decoder_output[8192];
|
|
|
|
// flush and re-sync the decoder, discarding the internal buffer
|
|
|
|
// we actually don't care if this succeeds or not
|
|
|
|
// FLUSH - flush internal buffer
|
|
|
|
// INTR - treat the current internal buffer as discontinuous
|
|
|
|
// CONCEAL - try to interpolate and smooth out the samples
|
2023-06-17 01:06:18 +02:00
|
|
|
if (decoder) {
|
|
|
|
FdkAac::aacDecoder_DecodeFrame(decoder, decoder_output, 8192,
|
|
|
|
AACDEC_FLUSH & AACDEC_INTR & AACDEC_CONCEAL);
|
|
|
|
}
|
2020-02-23 11:01:21 +01:00
|
|
|
}
|
|
|
|
|
2023-05-14 18:55:10 +02:00
|
|
|
std::optional<BinaryMessage> FDKDecoder::Impl::ProcessRequest(const BinaryMessage& request) {
|
|
|
|
if (request.header.codec != DecoderCodec::DecodeAAC) {
|
2020-02-23 11:01:21 +01:00
|
|
|
LOG_ERROR(Audio_DSP, "FDK AAC Decoder cannot handle such codec: {}",
|
2023-05-14 18:55:10 +02:00
|
|
|
static_cast<u16>(request.header.codec));
|
2020-02-23 11:01:21 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-05-14 18:55:10 +02:00
|
|
|
switch (request.header.cmd) {
|
2020-02-23 11:01:21 +01:00
|
|
|
case DecoderCommand::Init: {
|
|
|
|
return Initalize(request);
|
|
|
|
}
|
2023-05-14 18:55:10 +02:00
|
|
|
case DecoderCommand::EncodeDecode: {
|
2020-02-23 11:01:21 +01:00
|
|
|
return Decode(request);
|
|
|
|
}
|
2023-07-17 02:54:40 +02:00
|
|
|
case DecoderCommand::Shutdown:
|
|
|
|
case DecoderCommand::SaveState:
|
|
|
|
case DecoderCommand::LoadState: {
|
|
|
|
LOG_WARNING(Audio_DSP, "Got unimplemented binary request: {}",
|
|
|
|
static_cast<u16>(request.header.cmd));
|
2023-05-14 18:55:10 +02:00
|
|
|
BinaryMessage response = request;
|
2023-06-03 04:00:09 +02:00
|
|
|
response.header.result = ResultStatus::Success;
|
2020-02-23 11:01:21 +01:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
default:
|
2023-05-14 18:55:10 +02:00
|
|
|
LOG_ERROR(Audio_DSP, "Got unknown binary request: {}",
|
|
|
|
static_cast<u16>(request.header.cmd));
|
2020-02-23 11:01:21 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 18:55:10 +02:00
|
|
|
std::optional<BinaryMessage> FDKDecoder::Impl::Decode(const BinaryMessage& request) {
|
2023-06-03 04:00:09 +02:00
|
|
|
BinaryMessage response{};
|
2023-05-14 18:55:10 +02:00
|
|
|
response.header.codec = request.header.codec;
|
|
|
|
response.header.cmd = request.header.cmd;
|
|
|
|
response.decode_aac_response.size = request.decode_aac_request.size;
|
2020-02-23 11:01:21 +01:00
|
|
|
|
|
|
|
if (!decoder) {
|
|
|
|
LOG_DEBUG(Audio_DSP, "Decoder not initalized");
|
|
|
|
// This is a hack to continue games that are not compiled with the aac codec
|
2023-05-14 18:55:10 +02:00
|
|
|
response.decode_aac_response.num_channels = 2;
|
|
|
|
response.decode_aac_response.num_samples = 1024;
|
2020-02-23 11:01:21 +01:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2023-05-14 18:55:10 +02:00
|
|
|
if (request.decode_aac_request.src_addr < Memory::FCRAM_PADDR ||
|
2023-05-26 03:58:17 +02:00
|
|
|
request.decode_aac_request.src_addr + request.decode_aac_request.size >
|
2023-05-14 18:55:10 +02:00
|
|
|
Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) {
|
|
|
|
LOG_ERROR(Audio_DSP, "Got out of bounds src_addr {:08x}",
|
|
|
|
request.decode_aac_request.src_addr);
|
2020-02-23 11:01:21 +01:00
|
|
|
return {};
|
|
|
|
}
|
2023-05-26 03:58:17 +02:00
|
|
|
u8* data = memory.GetFCRAMPointer(request.decode_aac_request.src_addr - Memory::FCRAM_PADDR);
|
2020-02-23 11:01:21 +01:00
|
|
|
|
|
|
|
std::array<std::vector<s16>, 2> out_streams;
|
|
|
|
|
2023-06-17 01:06:18 +02:00
|
|
|
u32 data_size = request.decode_aac_request.size;
|
2020-02-23 11:01:21 +01:00
|
|
|
|
|
|
|
// decoding loops
|
|
|
|
AAC_DECODER_ERROR result = AAC_DEC_OK;
|
2023-05-15 04:50:07 +02:00
|
|
|
// Up to 2048 samples, up to 2 channels each
|
|
|
|
s16 decoder_output[4096];
|
2020-02-23 11:01:21 +01:00
|
|
|
// note that we don't free this pointer as it is automatically freed by fdk_aac
|
|
|
|
CStreamInfo* stream_info;
|
|
|
|
// how many bytes to be queued into the decoder, decrementing from the buffer size
|
|
|
|
u32 buffer_remaining = data_size;
|
|
|
|
// alias the data_size as an u32
|
|
|
|
u32 input_size = data_size;
|
|
|
|
|
|
|
|
while (buffer_remaining) {
|
|
|
|
// queue the input buffer, fdk_aac will automatically slice out the buffer it needs
|
|
|
|
// from the input buffer
|
2023-06-17 01:06:18 +02:00
|
|
|
result = FdkAac::aacDecoder_Fill(decoder, &data, &input_size, &buffer_remaining);
|
2020-02-23 11:01:21 +01:00
|
|
|
if (result != AAC_DEC_OK) {
|
|
|
|
// there are some issues when queuing the input buffer
|
|
|
|
LOG_ERROR(Audio_DSP, "Failed to enqueue the input samples");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
// get output from decoder
|
2023-06-17 01:06:18 +02:00
|
|
|
result = FdkAac::aacDecoder_DecodeFrame(decoder, decoder_output,
|
|
|
|
sizeof(decoder_output) / sizeof(s16), 0);
|
2020-02-23 11:01:21 +01:00
|
|
|
if (result == AAC_DEC_OK) {
|
|
|
|
// get the stream information
|
2023-06-17 01:06:18 +02:00
|
|
|
stream_info = FdkAac::aacDecoder_GetStreamInfo(decoder);
|
2020-02-23 11:01:21 +01:00
|
|
|
// fill the stream information for binary response
|
2023-05-14 18:55:10 +02:00
|
|
|
response.decode_aac_response.sample_rate = GetSampleRateEnum(stream_info->sampleRate);
|
|
|
|
response.decode_aac_response.num_channels = stream_info->numChannels;
|
|
|
|
response.decode_aac_response.num_samples = stream_info->frameSize;
|
2020-02-23 11:01:21 +01:00
|
|
|
// fill the output
|
|
|
|
// the sample size = frame_size * channel_counts
|
2023-05-15 04:50:07 +02:00
|
|
|
for (int sample = 0; sample < stream_info->frameSize; sample++) {
|
|
|
|
for (int ch = 0; ch < stream_info->numChannels; ch++) {
|
|
|
|
out_streams[ch].push_back(
|
|
|
|
decoder_output[(sample * stream_info->numChannels) + ch]);
|
2020-02-23 11:01:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (result == AAC_DEC_TRANSPORT_SYNC_ERROR) {
|
|
|
|
// decoder has some synchronization problems, try again with new samples,
|
|
|
|
// using old samples might trigger this error again
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Audio_DSP, "Error decoding the sample: {}", result);
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 04:50:07 +02:00
|
|
|
|
2020-02-23 11:01:21 +01:00
|
|
|
// transfer the decoded buffer from vector to the FCRAM
|
2023-05-15 04:50:07 +02:00
|
|
|
for (std::size_t ch = 0; ch < out_streams.size(); ch++) {
|
|
|
|
if (!out_streams[ch].empty()) {
|
|
|
|
auto byte_size = out_streams[ch].size() * sizeof(s16);
|
2023-05-14 18:55:10 +02:00
|
|
|
auto dst = ch == 0 ? request.decode_aac_request.dst_addr_ch0
|
|
|
|
: request.decode_aac_request.dst_addr_ch1;
|
2023-05-15 04:50:07 +02:00
|
|
|
if (dst < Memory::FCRAM_PADDR ||
|
|
|
|
dst + byte_size > Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) {
|
|
|
|
LOG_ERROR(Audio_DSP, "Got out of bounds dst_addr_ch{} {:08x}", ch, dst);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::memcpy(memory.GetFCRAMPointer(dst - Memory::FCRAM_PADDR), out_streams[ch].data(),
|
|
|
|
byte_size);
|
2020-02-23 11:01:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
FDKDecoder::FDKDecoder(Memory::MemorySystem& memory) : impl(std::make_unique<Impl>(memory)) {}
|
|
|
|
|
|
|
|
FDKDecoder::~FDKDecoder() = default;
|
|
|
|
|
2023-05-14 18:55:10 +02:00
|
|
|
std::optional<BinaryMessage> FDKDecoder::ProcessRequest(const BinaryMessage& request) {
|
2020-02-23 11:01:21 +01:00
|
|
|
return impl->ProcessRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FDKDecoder::IsValid() const {
|
|
|
|
return impl->IsValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore::HLE
|