From 6281660844255f9680a04bdaacd5b9753764bb06 Mon Sep 17 00:00:00 2001 From: liushuyu Date: Sat, 9 Feb 2019 11:59:00 -0700 Subject: [PATCH] audio_core: hle: mf: address yet another batch of reviews --- src/audio_core/hle/adts.h | 2 +- src/audio_core/hle/adts_reader.cpp | 2 +- src/audio_core/hle/wmf_decoder.cpp | 22 +++++++++-------- src/audio_core/hle/wmf_decoder_utils.cpp | 30 +++++++++++++----------- src/audio_core/hle/wmf_decoder_utils.h | 14 ++++++++--- 5 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/audio_core/hle/adts.h b/src/audio_core/hle/adts.h index 11c736986..9aba09fc3 100644 --- a/src/audio_core/hle/adts.h +++ b/src/audio_core/hle/adts.h @@ -20,4 +20,4 @@ ADTSData ParseADTS(const char* buffer); // last two bytes of MF AAC decoder user data // see https://docs.microsoft.com/en-us/windows/desktop/medfound/aac-decoder#example-media-types -u16 MFGetAACTag(const ADTSData input); +u16 MFGetAACTag(const ADTSData& input); diff --git a/src/audio_core/hle/adts_reader.cpp b/src/audio_core/hle/adts_reader.cpp index 93118f887..e70c32a3b 100644 --- a/src/audio_core/hle/adts_reader.cpp +++ b/src/audio_core/hle/adts_reader.cpp @@ -50,7 +50,7 @@ ADTSData ParseADTS(const char* buffer) { // Frame length flag (1 bit) // Depends on core coder (1 bit) // Extension flag (1 bit) -u16 MFGetAACTag(const ADTSData input) { +u16 MFGetAACTag(const ADTSData& input) { u16 tag = 0; tag |= input.profile << 11; diff --git a/src/audio_core/hle/wmf_decoder.cpp b/src/audio_core/hle/wmf_decoder.cpp index 3b4087bf7..44a250c33 100644 --- a/src/audio_core/hle/wmf_decoder.cpp +++ b/src/audio_core/hle/wmf_decoder.cpp @@ -22,7 +22,7 @@ private: MFOutputState DecodingLoop(ADTSData adts_header, std::array, 2>& out_streams); - bool initalized = false; + bool initialized = false; bool selected = false; Memory::MemorySystem& memory; @@ -36,7 +36,9 @@ WMFDecoder::Impl::Impl(Memory::MemorySystem& memory) : memory(memory) { MFCoInit(); } -WMFDecoder::Impl::~Impl() = default; +WMFDecoder::Impl::~Impl() { + Clear(); +} std::optional WMFDecoder::Impl::ProcessRequest(const BinaryRequest& request) { if (request.codec != DecoderCodec::AAC) { @@ -65,7 +67,7 @@ std::optional WMFDecoder::Impl::ProcessRequest(const BinaryReque } std::optional WMFDecoder::Impl::Initalize(const BinaryRequest& request) { - if (initalized) { + if (initialized) { Clear(); } @@ -89,16 +91,16 @@ std::optional WMFDecoder::Impl::Initalize(const BinaryRequest& r return response; } - initalized = true; + initialized = true; return response; } void WMFDecoder::Impl::Clear() { - if (initalized) { + if (initialized) { MFFlush(transform.get()); - MFDeInit(transform.get()); + MFDestroy(); } - initalized = false; + initialized = false; selected = false; } @@ -117,7 +119,7 @@ MFOutputState WMFDecoder::Impl::DecodingLoop(ADTSData adts_header, // the following was taken from ffmpeg version of the decoder f32 val_f32; - for (size_t i = 0; i < output_buffer->size();) { + for (std::size_t i = 0; i < output_buffer->size();) { for (std::size_t channel = 0; channel < adts_header.channels; channel++) { val_f32 = output_buffer->at(i); s16 val = static_cast(0x7FFF * val_f32); @@ -161,8 +163,8 @@ std::optional WMFDecoder::Impl::Decode(const BinaryRequest& requ response.num_channels = 2; response.num_samples = 1024; - if (!initalized) { - LOG_DEBUG(Audio_DSP, "Decoder not initalized"); + if (!initialized) { + LOG_DEBUG(Audio_DSP, "Decoder not initialized"); // This is a hack to continue games when decoder failed to initialize return response; } diff --git a/src/audio_core/hle/wmf_decoder_utils.cpp b/src/audio_core/hle/wmf_decoder_utils.cpp index c0c9c5744..7dec97abf 100644 --- a/src/audio_core/hle/wmf_decoder_utils.cpp +++ b/src/audio_core/hle/wmf_decoder_utils.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. #include "common/logging/log.h" +#include "common/string_util.h" #include "wmf_decoder_utils.h" // utility functions @@ -9,16 +10,17 @@ void ReportError(std::string msg, HRESULT hr) { if (SUCCEEDED(hr)) { return; } - LPSTR err; - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, hr, - // hardcode to use en_US because if any user had problems with this - // we can help them w/o translating anything - // default is to use the language currently active on the operating system - MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&err, 0, nullptr); + LPWSTR err; + FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, hr, + // hardcode to use en_US because if any user had problems with this + // we can help them w/o translating anything + // default is to use the language currently active on the operating system + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPWSTR)&err, 0, nullptr); if (err != nullptr) { - LOG_CRITICAL(Audio_DSP, "{}: {}", msg, err); + LOG_CRITICAL(Audio_DSP, "{}: {}", msg, Common::UTF16ToUTF8(err)); + LocalFree(err); } LOG_CRITICAL(Audio_DSP, "{}: {:08x}", msg, hr); } @@ -82,8 +84,8 @@ unique_mfptr MFDecoderInit(GUID audio_format) { return std::move(transform); } -void MFDeInit(IMFTransform* transform) { - MFShutdownObject(transform); +void MFDestroy() { + MFShutdown(); CoUninitialize(); } @@ -126,11 +128,11 @@ unique_mfptr CreateSample(void* data, DWORD len, DWORD alignment, LON ReportError("Unable to set sample duration, but continuing anyway", hr); } - return std::move(sample); + return sample; } bool SelectInputMediaType(IMFTransform* transform, int in_stream_id, const ADTSData& adts, - UINT8* user_data, UINT32 user_data_len, GUID audio_format) { + const UINT8* user_data, UINT32 user_data_len, GUID audio_format) { HRESULT hr = S_OK; unique_mfptr t; @@ -209,7 +211,7 @@ bool SelectOutputMediaType(IMFTransform* transform, int out_stream_id, GUID audi return false; } -std::optional DetectMediaType(char* buffer, size_t len) { +std::optional DetectMediaType(char* buffer, std::size_t len) { if (len < 7) { return std::nullopt; } diff --git a/src/audio_core/hle/wmf_decoder_utils.h b/src/audio_core/hle/wmf_decoder_utils.h index a5e5d0154..ac114edc9 100644 --- a/src/audio_core/hle/wmf_decoder_utils.h +++ b/src/audio_core/hle/wmf_decoder_utils.h @@ -29,6 +29,14 @@ struct MFRelease { }; }; +template <> +struct MFRelease { + void operator()(IMFTransform* pointer) const { + MFShutdownObject(pointer); + pointer->Release(); + }; +}; + // wrapper facilities for dealing with pointers template using unique_mfptr = std::unique_ptr>; @@ -67,13 +75,13 @@ struct ADTSMeta { // exported functions bool MFCoInit(); unique_mfptr MFDecoderInit(GUID audio_format = MFAudioFormat_AAC); -void MFDeInit(IMFTransform* transform); +void MFDestroy(); unique_mfptr CreateSample(void* data, DWORD len, DWORD alignment = 1, LONGLONG duration = 0); bool SelectInputMediaType(IMFTransform* transform, int in_stream_id, const ADTSData& adts, - UINT8* user_data, UINT32 user_data_len, + const UINT8* user_data, UINT32 user_data_len, GUID audio_format = MFAudioFormat_AAC); -std::optional DetectMediaType(char* buffer, size_t len); +std::optional DetectMediaType(char* buffer, std::size_t len); bool SelectOutputMediaType(IMFTransform* transform, int out_stream_id, GUID audio_format = MFAudioFormat_PCM); void MFFlush(IMFTransform* transform);