2018-12-19 16:12:57 +00:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "audio_core/hle/wmf_decoder.h"
|
|
|
|
#include "audio_core/hle/wmf_decoder_utils.h"
|
|
|
|
|
|
|
|
namespace AudioCore::HLE {
|
|
|
|
|
|
|
|
class WMFDecoder::Impl {
|
|
|
|
public:
|
|
|
|
explicit Impl(Memory::MemorySystem& memory);
|
|
|
|
~Impl();
|
|
|
|
std::optional<BinaryResponse> ProcessRequest(const BinaryRequest& request);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::optional<BinaryResponse> Initalize(const BinaryRequest& request);
|
|
|
|
|
|
|
|
std::optional<BinaryResponse> Decode(const BinaryRequest& request);
|
|
|
|
|
2019-01-29 05:23:57 +00:00
|
|
|
MFOutputState DecodingLoop(ADTSData adts_header, std::array<std::vector<u8>, 2>& out_streams);
|
2018-12-19 16:12:57 +00:00
|
|
|
|
2019-02-13 20:49:41 +00:00
|
|
|
bool transform_initialized = false;
|
|
|
|
bool format_selected = false;
|
2018-12-19 16:12:57 +00:00
|
|
|
|
|
|
|
Memory::MemorySystem& memory;
|
|
|
|
|
2019-01-14 21:27:43 +00:00
|
|
|
unique_mfptr<IMFTransform> transform;
|
2018-12-19 16:12:57 +00:00
|
|
|
DWORD in_stream_id = 0;
|
|
|
|
DWORD out_stream_id = 0;
|
|
|
|
};
|
|
|
|
|
2019-02-13 20:49:41 +00:00
|
|
|
WMFDecoder::Impl::Impl(Memory::MemorySystem& memory) : memory(memory) {
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
hr = CoInitialize(NULL);
|
|
|
|
// S_FALSE will be returned when COM has already been initialized
|
|
|
|
if (hr != S_OK && hr != S_FALSE) {
|
|
|
|
ReportError("Failed to start COM components", hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lite startup is faster and all what we need is included
|
|
|
|
hr = MFStartup(MF_VERSION, MFSTARTUP_LITE);
|
|
|
|
if (hr != S_OK) {
|
|
|
|
// Do you know you can't initialize MF in test mode or safe mode?
|
|
|
|
ReportError("Failed to initialize Media Foundation", hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_INFO(Audio_DSP, "Media Foundation activated");
|
|
|
|
}
|
2018-12-19 16:12:57 +00:00
|
|
|
|
2019-02-09 18:59:00 +00:00
|
|
|
WMFDecoder::Impl::~Impl() {
|
2019-02-13 20:49:41 +00:00
|
|
|
if (transform_initialized) {
|
|
|
|
MFFlush(transform.get());
|
|
|
|
// delete the transform object before shutting down MF
|
|
|
|
// otherwise access violation will occur
|
|
|
|
transform.reset();
|
|
|
|
MFShutdown();
|
|
|
|
CoUninitialize();
|
|
|
|
}
|
|
|
|
transform_initialized = false;
|
|
|
|
format_selected = false;
|
2019-02-09 18:59:00 +00:00
|
|
|
}
|
2018-12-19 16:12:57 +00:00
|
|
|
|
|
|
|
std::optional<BinaryResponse> WMFDecoder::Impl::ProcessRequest(const BinaryRequest& request) {
|
|
|
|
if (request.codec != DecoderCodec::AAC) {
|
|
|
|
LOG_ERROR(Audio_DSP, "Got unknown codec {}", static_cast<u16>(request.codec));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (request.cmd) {
|
|
|
|
case DecoderCommand::Init: {
|
2019-01-06 04:53:24 +00:00
|
|
|
LOG_INFO(Audio_DSP, "WMFDecoder initializing");
|
2018-12-19 16:12:57 +00:00
|
|
|
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<u16>(request.cmd));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<BinaryResponse> WMFDecoder::Impl::Initalize(const BinaryRequest& request) {
|
|
|
|
BinaryResponse response;
|
|
|
|
std::memcpy(&response, &request, sizeof(response));
|
|
|
|
response.unknown1 = 0x0;
|
2019-02-09 04:42:50 +00:00
|
|
|
transform = MFDecoderInit();
|
2018-12-19 16:12:57 +00:00
|
|
|
|
2019-02-09 04:42:50 +00:00
|
|
|
if (transform == nullptr) {
|
2019-02-13 20:49:41 +00:00
|
|
|
LOG_CRITICAL(Audio_DSP, "Can't initialize decoder");
|
2018-12-19 16:12:57 +00:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr = transform->GetStreamIDs(1, &in_stream_id, 1, &out_stream_id);
|
|
|
|
if (hr == E_NOTIMPL) {
|
|
|
|
// if not implemented, it means this MFT does not assign stream ID for you
|
|
|
|
in_stream_id = 0;
|
|
|
|
out_stream_id = 0;
|
|
|
|
} else if (FAILED(hr)) {
|
|
|
|
ReportError("Decoder failed to initialize the stream ID", hr);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2019-02-13 20:49:41 +00:00
|
|
|
transform_initialized = true;
|
|
|
|
format_selected = false; // select format again if application request initialize the DSP
|
2018-12-19 16:12:57 +00:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2019-01-29 05:23:57 +00:00
|
|
|
MFOutputState WMFDecoder::Impl::DecodingLoop(ADTSData adts_header,
|
2019-01-29 05:53:55 +00:00
|
|
|
std::array<std::vector<u8>, 2>& out_streams) {
|
2019-02-04 00:29:59 +00:00
|
|
|
MFOutputState output_status = MFOutputState::OK;
|
2019-02-06 04:13:40 +00:00
|
|
|
std::optional<std::vector<f32>> output_buffer;
|
2019-01-14 21:27:43 +00:00
|
|
|
unique_mfptr<IMFSample> output;
|
2018-12-19 16:12:57 +00:00
|
|
|
|
|
|
|
while (true) {
|
2019-01-14 21:27:43 +00:00
|
|
|
auto [output_status, output] = ReceiveSample(transform.get(), out_stream_id);
|
2018-12-19 16:12:57 +00:00
|
|
|
|
|
|
|
// 0 -> okay; 3 -> okay but more data available (buffer too small)
|
2019-02-04 00:29:59 +00:00
|
|
|
if (output_status == MFOutputState::OK || output_status == MFOutputState::HaveMoreData) {
|
2019-02-06 04:13:40 +00:00
|
|
|
output_buffer = CopySampleToBuffer(output.get());
|
2018-12-19 16:12:57 +00:00
|
|
|
|
|
|
|
// the following was taken from ffmpeg version of the decoder
|
|
|
|
f32 val_f32;
|
2019-02-09 18:59:00 +00:00
|
|
|
for (std::size_t i = 0; i < output_buffer->size();) {
|
2018-12-19 16:12:57 +00:00
|
|
|
for (std::size_t channel = 0; channel < adts_header.channels; channel++) {
|
2019-02-06 04:13:40 +00:00
|
|
|
val_f32 = output_buffer->at(i);
|
2018-12-19 16:12:57 +00:00
|
|
|
s16 val = static_cast<s16>(0x7FFF * val_f32);
|
|
|
|
out_streams[channel].push_back(val & 0xFF);
|
|
|
|
out_streams[channel].push_back(val >> 8);
|
2019-02-09 04:22:14 +00:00
|
|
|
// i is incremented on per channel basis
|
|
|
|
i++;
|
2018-12-19 16:12:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case of "ok" only, just return quickly
|
2019-02-04 00:29:59 +00:00
|
|
|
if (output_status == MFOutputState::OK)
|
|
|
|
return MFOutputState::OK;
|
2018-12-19 16:12:57 +00:00
|
|
|
|
|
|
|
// for status = 2, reset MF
|
2019-02-04 00:29:59 +00:00
|
|
|
if (output_status == MFOutputState::NeedReconfig) {
|
2019-02-13 20:49:41 +00:00
|
|
|
format_selected = false;
|
2019-02-09 23:49:48 +00:00
|
|
|
return MFOutputState::NeedReconfig;
|
2018-12-19 16:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// for status = 3, try again with new buffer
|
2019-02-04 00:29:59 +00:00
|
|
|
if (output_status == MFOutputState::HaveMoreData)
|
2018-12-19 16:12:57 +00:00
|
|
|
continue;
|
|
|
|
|
2019-02-04 05:42:18 +00:00
|
|
|
// according to MS document, this is not an error (?!)
|
|
|
|
if (output_status == MFOutputState::NeedMoreInput)
|
2019-02-04 00:29:59 +00:00
|
|
|
return MFOutputState::NeedMoreInput;
|
2019-01-06 20:29:21 +00:00
|
|
|
|
2019-02-04 00:29:59 +00:00
|
|
|
return MFOutputState::FatalError; // return on other status
|
2018-12-19 16:12:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 00:29:59 +00:00
|
|
|
return MFOutputState::FatalError;
|
2018-12-19 16:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<BinaryResponse> WMFDecoder::Impl::Decode(const BinaryRequest& request) {
|
|
|
|
BinaryResponse response;
|
|
|
|
response.codec = request.codec;
|
|
|
|
response.cmd = request.cmd;
|
|
|
|
response.size = request.size;
|
|
|
|
response.num_channels = 2;
|
|
|
|
response.num_samples = 1024;
|
|
|
|
|
2019-02-13 20:49:41 +00:00
|
|
|
if (!transform_initialized) {
|
2019-02-09 18:59:00 +00:00
|
|
|
LOG_DEBUG(Audio_DSP, "Decoder not initialized");
|
2019-02-09 05:23:40 +00:00
|
|
|
// This is a hack to continue games when decoder failed to initialize
|
2018-12-19 16:12:57 +00:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
u8* data = memory.GetFCRAMPointer(request.src_addr - Memory::FCRAM_PADDR);
|
|
|
|
|
|
|
|
std::array<std::vector<u8>, 2> out_streams;
|
2019-01-14 21:27:43 +00:00
|
|
|
unique_mfptr<IMFSample> sample;
|
2019-02-04 00:29:59 +00:00
|
|
|
MFInputState input_status = MFInputState::OK;
|
2019-02-09 23:49:48 +00:00
|
|
|
MFOutputState output_status = MFOutputState::OK;
|
2019-02-09 05:23:40 +00:00
|
|
|
std::optional<ADTSMeta> adts_meta = DetectMediaType((char*)data, request.size);
|
2018-12-19 16:12:57 +00:00
|
|
|
|
2019-02-09 05:23:40 +00:00
|
|
|
if (!adts_meta) {
|
2018-12-19 16:12:57 +00:00
|
|
|
LOG_ERROR(Audio_DSP, "Unable to deduce decoding parameters from ADTS stream");
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2019-02-09 05:23:40 +00:00
|
|
|
response.num_channels = adts_meta->ADTSHeader.channels;
|
2019-01-06 04:05:12 +00:00
|
|
|
|
2019-02-13 20:49:41 +00:00
|
|
|
if (!format_selected) {
|
2018-12-19 16:12:57 +00:00
|
|
|
LOG_DEBUG(Audio_DSP, "New ADTS stream: channels = {}, sample rate = {}",
|
2019-02-09 05:23:40 +00:00
|
|
|
adts_meta->ADTSHeader.channels, adts_meta->ADTSHeader.samplerate);
|
|
|
|
SelectInputMediaType(transform.get(), in_stream_id, adts_meta->ADTSHeader,
|
|
|
|
adts_meta->AACTag, 14);
|
2019-01-07 23:57:20 +00:00
|
|
|
SelectOutputMediaType(transform.get(), out_stream_id);
|
|
|
|
SendSample(transform.get(), in_stream_id, nullptr);
|
2018-12-19 16:12:57 +00:00
|
|
|
// cache the result from detect_mediatype and call select_*_mediatype only once
|
|
|
|
// This could increase performance very slightly
|
|
|
|
transform->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
|
2019-02-13 20:49:41 +00:00
|
|
|
format_selected = true;
|
2018-12-19 16:12:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 05:23:57 +00:00
|
|
|
sample = CreateSample((void*)data, request.size, 1, 0);
|
2018-12-19 16:12:57 +00:00
|
|
|
sample->SetUINT32(MFSampleExtension_CleanPoint, 1);
|
|
|
|
|
|
|
|
while (true) {
|
2019-01-14 21:27:43 +00:00
|
|
|
input_status = SendSample(transform.get(), in_stream_id, sample.get());
|
2019-02-09 23:49:48 +00:00
|
|
|
output_status = DecodingLoop(adts_meta->ADTSHeader, out_streams);
|
2018-12-19 16:12:57 +00:00
|
|
|
|
2019-02-09 23:49:48 +00:00
|
|
|
if (output_status == MFOutputState::FatalError) {
|
2019-01-06 04:53:24 +00:00
|
|
|
// if the decode issues are caused by MFT not accepting new samples, try again
|
2018-12-19 16:12:57 +00:00
|
|
|
// NOTICE: you are required to check the output even if you already knew/guessed
|
|
|
|
// MFT didn't accept the input sample
|
2019-02-04 00:29:59 +00:00
|
|
|
if (input_status == MFInputState::NotAccepted) {
|
2018-12-19 16:12:57 +00:00
|
|
|
// try again
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:29:21 +00:00
|
|
|
LOG_ERROR(Audio_DSP, "Errors occurred when receiving output");
|
2018-12-19 16:12:57 +00:00
|
|
|
return response;
|
2019-02-09 23:49:48 +00:00
|
|
|
} else if (output_status == MFOutputState::NeedReconfig) {
|
2019-02-13 20:49:41 +00:00
|
|
|
// flush the transform
|
|
|
|
MFFlush(transform.get());
|
2019-02-09 23:49:48 +00:00
|
|
|
// decode again
|
|
|
|
return this->Decode(request);
|
2018-12-19 16:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break; // jump out of the loop if at least we don't have obvious issues
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_streams[0].size() != 0) {
|
|
|
|
if (request.dst_addr_ch0 < Memory::FCRAM_PADDR ||
|
|
|
|
request.dst_addr_ch0 + out_streams[0].size() >
|
|
|
|
Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) {
|
|
|
|
LOG_ERROR(Audio_DSP, "Got out of bounds dst_addr_ch0 {:08x}", request.dst_addr_ch0);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::memcpy(memory.GetFCRAMPointer(request.dst_addr_ch0 - Memory::FCRAM_PADDR),
|
|
|
|
out_streams[0].data(), out_streams[0].size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_streams[1].size() != 0) {
|
|
|
|
if (request.dst_addr_ch1 < Memory::FCRAM_PADDR ||
|
|
|
|
request.dst_addr_ch1 + out_streams[1].size() >
|
|
|
|
Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) {
|
|
|
|
LOG_ERROR(Audio_DSP, "Got out of bounds dst_addr_ch1 {:08x}", request.dst_addr_ch1);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::memcpy(memory.GetFCRAMPointer(request.dst_addr_ch1 - Memory::FCRAM_PADDR),
|
|
|
|
out_streams[1].data(), out_streams[1].size());
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
WMFDecoder::WMFDecoder(Memory::MemorySystem& memory) : impl(std::make_unique<Impl>(memory)) {}
|
|
|
|
|
|
|
|
WMFDecoder::~WMFDecoder() = default;
|
|
|
|
|
|
|
|
std::optional<BinaryResponse> WMFDecoder::ProcessRequest(const BinaryRequest& request) {
|
|
|
|
return impl->ProcessRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore::HLE
|