diff options
author | thexai <58434170+thexai@users.noreply.github.com> | 2024-06-28 15:32:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-28 15:32:07 +0200 |
commit | 4c16f448fc06979bfd2b46693774f6a53b08e256 (patch) | |
tree | be1c20e9ee8a697651c33cec57d1085dec034ed9 | |
parent | c23991299e4e7e5ad32967646d402b30c74d0c6e (diff) | |
parent | 3420eccbe1a5ca36024f0609f1c93d88b5889e7e (diff) |
Merge pull request #25367 from thexai/mat-simplify
[Audio] PackerMAT: simplify code - remove non-essential code
-rw-r--r-- | xbmc/cores/AudioEngine/Utils/PackerMAT.cpp | 122 | ||||
-rw-r--r-- | xbmc/cores/AudioEngine/Utils/PackerMAT.h | 52 |
2 files changed, 8 insertions, 166 deletions
diff --git a/xbmc/cores/AudioEngine/Utils/PackerMAT.cpp b/xbmc/cores/AudioEngine/Utils/PackerMAT.cpp index 22811b80dc..e6a5d621b3 100644 --- a/xbmc/cores/AudioEngine/Utils/PackerMAT.cpp +++ b/xbmc/cores/AudioEngine/Utils/PackerMAT.cpp @@ -57,42 +57,24 @@ CPackerMAT::CPackerMAT() // high-bitrate streams can overshoot this size and therefor require proper handling of dynamic padding. bool CPackerMAT::PackTrueHD(const uint8_t* data, int size) { - TrueHDMajorSyncInfo info; + // discard too small packets (cannot be valid) + if (size < 10) + return false; - // get the ratebits and output timing from the sync frame + // get the ratebits from the major sync frame if (AV_RB32(data + 4) == FORMAT_MAJOR_SYNC) { - info = ParseTrueHDMajorSyncHeaders(data, size); - - if (!info.valid) - return false; - - m_state.ratebits = info.ratebits; + // read audio_sampling_frequency (high nibble after format major sync) + m_state.ratebits = data[8] >> 4; } - else if (m_state.prevFrametimeValid == false) + else if (!m_state.prevFrametimeValid) { // only start streaming on a major sync frame - m_state.numberOfSamplesOffset = 0; return false; } const uint16_t frameTime = AV_RB16(data + 2); uint32_t spaceSize = 0; - const uint16_t frameSamples = 40 << (m_state.ratebits & 7); - m_state.outputTiming += frameSamples; - - if (info.outputTimingPresent) - { - if (m_state.outputTimingValid && (info.outputTiming != m_state.outputTiming)) - { - CLog::Log(LOGWARNING, - "CPackerMAT::PackTrueHD: detected a stream discontinuity -> output timing " - "expected: {}, found: {}", - m_state.outputTiming, info.outputTiming); - } - m_state.outputTiming = info.outputTiming; - m_state.outputTimingValid = true; - } // compute final padded size for the previous frame, if any if (m_state.prevFrametimeValid) @@ -153,9 +135,6 @@ bool CPackerMAT::PackTrueHD(const uint8_t* data, int size) } } - // count the number of samples in this frame - m_state.samples += frameSamples; - // write actual audio data to the buffer int remaining = FillDataBuffer(data, size, Type::DATA); @@ -334,96 +313,9 @@ void CPackerMAT::FlushPacket() assert(GetCount() == MAT_BUFFER_SIZE); - // normal number of samples per frame - const uint16_t frameSamples = 40 << (m_state.ratebits & 7); - const uint32_t MATSamples = (frameSamples * 24); - // push MAT packet to output queue m_outputQueue.emplace_back(std::move(m_buffer)); - // we expect 24 frames per MAT frame, so calculate an offset from that - // this is done after delivery, because it modifies the duration of the frame, - // eg. the start of the next frame - if (MATSamples != m_state.samples) - m_state.numberOfSamplesOffset += m_state.samples - MATSamples; - - m_state.samples = 0; - m_buffer.clear(); m_bufferCount = 0; } - -TrueHDMajorSyncInfo CPackerMAT::ParseTrueHDMajorSyncHeaders(const uint8_t* p, int buffsize) const -{ - TrueHDMajorSyncInfo info; - - if (buffsize < 32) - return {}; - - // parse major sync and look for a restart header - int majorSyncSize = 28; - if (p[29] & 1) // restart header exists - { - int extensionSize = p[30] >> 4; // calculate headers size - majorSyncSize += 2 + extensionSize * 2; - } - - CBitStream bs(p + 4, buffsize - 4); - - bs.SkipBits(32); // format_sync - - info.ratebits = bs.ReadBits(4); // ratebits - info.valid = true; - - // (1) 6ch_multichannel_type - // (1) 8ch_multichannel_type - // (2) reserved - // (2) 2ch_presentation_channel_modifier - // (2) 6ch_presentation_channel_modifier - // (5) 6ch_presentation_channel_assignment - // (2) 8ch_presentation_channel_modifier - // (13) 8ch_presentation_channel_assignment - // (16) signature - // (16) flags - // (16) reserved - // (1) variable_rate - // (15) peak_data_rate - bs.SkipBits(1 + 1 + 2 + 2 + 2 + 5 + 2 + 13 + 16 + 16 + 16 + 1 + 15); - - const int numSubstreams = bs.ReadBits(4); - - bs.SkipBits(4 + (majorSyncSize - 17) * 8); - - // substream directory - for (int i = 0; i < numSubstreams; i++) - { - int extraSubstreamWord = bs.ReadBits(1); - // (1) restart_nonexistent - // (1) crc_present - // (1) reserved - // (12) substream_end_ptr - bs.SkipBits(15); - if (extraSubstreamWord) - bs.SkipBits(16); // drc_gain_update, drc_time_update, reserved - } - - // substream segments - for (int i = 0; i < numSubstreams; i++) - { - if (bs.ReadBits(1)) - { // block_header_exists - if (bs.ReadBits(1)) - { // restart_header_exists - bs.SkipBits(14); // restart_sync_word - info.outputTiming = bs.ReadBits(16); - info.outputTimingPresent = true; - // XXX: restart header - } - // XXX: Block header - } - // XXX: All blocks, all substreams? - break; - } - - return info; -} diff --git a/xbmc/cores/AudioEngine/Utils/PackerMAT.h b/xbmc/cores/AudioEngine/Utils/PackerMAT.h index 27cf1fcb64..476f049487 100644 --- a/xbmc/cores/AudioEngine/Utils/PackerMAT.h +++ b/xbmc/cores/AudioEngine/Utils/PackerMAT.h @@ -12,14 +12,6 @@ #include <stdint.h> #include <vector> -struct TrueHDMajorSyncInfo -{ - int ratebits{0}; - uint16_t outputTiming{0}; - bool outputTimingPresent{false}; - bool valid{false}; -}; - enum class Type { PADDING, @@ -49,13 +41,7 @@ private: // 10 -> 176.4 kHz int ratebits; - // Output timing obtained parsing TrueHD major sync headers (when available) or - // inferred increasing a counter the rest of the time. - uint16_t outputTiming; - bool outputTimingValid; - - // Input timing of audio unit (obtained of each audio unit) and used to calculate padding - // bytes. On the contrary of outputTiming, frametime is present in all audio units. + // input timing of previous audio unit used to calculate padding bytes uint16_t prevFrametime; bool prevFrametimeValid; @@ -63,8 +49,6 @@ private: uint32_t prevMatFramesize; // size in bytes of previous MAT frame uint32_t padding; // padding bytes pending to write - uint32_t samples; // number of samples accumulated in current MAT frame - int numberOfSamplesOffset; // offset respect number of samples in a standard MAT frame (40 * 24) }; void WriteHeader(); @@ -73,7 +57,6 @@ private: uint32_t GetCount() const { return m_bufferCount; } int FillDataBuffer(const uint8_t* data, int size, Type type); void FlushPacket(); - TrueHDMajorSyncInfo ParseTrueHDMajorSyncHeaders(const uint8_t* p, int buffsize) const; MATState m_state{}; @@ -81,36 +64,3 @@ private: std::vector<uint8_t> m_buffer; std::deque<std::vector<uint8_t>> m_outputQueue; }; - -class CBitStream -{ -public: - // opens an existing byte array as bitstream - CBitStream(const uint8_t* bytes, int _size) - { - data = bytes; - size = _size; - } - - // reads bits from bitstream - int ReadBits(int bits) - { - int dat = 0; - for (int i = index; i < index + bits; i++) - { - dat = dat * 2 + getbit(data[i / 8], i % 8); - } - index += bits; - return dat; - } - - // skip bits from bitstream - void SkipBits(int bits) { index += bits; } - -private: - uint8_t getbit(uint8_t x, int y) { return (x >> (7 - y)) & 1; } - - const uint8_t* data{nullptr}; - int size{0}; - int index{0}; -}; |