Skip to content

Commit

Permalink
Refactored AAC parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiden committed Nov 13, 2024
1 parent 2ec8124 commit db953db
Show file tree
Hide file tree
Showing 22 changed files with 726 additions and 330 deletions.
10 changes: 5 additions & 5 deletions src/projects/base/info/decoder_configuration_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class DecoderConfigurationRecord
{
public:
std::shared_ptr<ov::Data> GetData()
std::shared_ptr<const ov::Data> GetData()
{
if (_updated)
{
Expand All @@ -16,18 +16,18 @@ class DecoderConfigurationRecord
return _data;
}

virtual bool Parse(const std::shared_ptr<ov::Data> &data) = 0;
virtual bool Parse(const std::shared_ptr<const ov::Data> &data) = 0;
virtual bool IsValid() const = 0;
virtual bool Equals(const std::shared_ptr<DecoderConfigurationRecord> &other) = 0;

// RFC 6381
virtual ov::String GetCodecsParameter() const = 0;

protected:
virtual std::shared_ptr<ov::Data> Serialize() = 0;
virtual std::shared_ptr<const ov::Data> Serialize() = 0;

// Set serialized data
void SetData(const std::shared_ptr<ov::Data> &data)
void SetData(const std::shared_ptr<const ov::Data> &data)
{
_data = data;
_updated = false;
Expand All @@ -39,6 +39,6 @@ class DecoderConfigurationRecord
}

private:
std::shared_ptr<ov::Data> _data = nullptr;
std::shared_ptr<const ov::Data> _data = nullptr;
bool _updated = true;
};
201 changes: 112 additions & 89 deletions src/projects/base/ovlibrary/bit_reader.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#pragma once

#include <utility>
#include <base/ovlibrary/ovlibrary.h>
#include <stdint.h>
#include <type_traits>

#include <algorithm>
#include <base/ovlibrary/ovlibrary.h>
#include <type_traits>
#include <utility>

#include "byte_io.h"

class BitReader
{

public:
BitReader(const uint8_t *buffer, size_t capacity) :
_buffer(buffer),
_position(buffer),
_capacity(capacity)
{
}

template<typename T>
BitReader(const uint8_t *buffer, size_t capacity)
: _buffer(buffer),
_position(buffer),
_capacity(capacity)
{
}

template <typename T>
T ReadBytes(bool big_endian = true)
{
T value;
bool result = ReadBytes(value, big_endian);
if(result == false)
if (result == false)
{
return 0;
}
Expand All @@ -39,7 +39,7 @@ class BitReader

bool SkipBytes(size_t length)
{
if (length > static_cast<size_t>(_capacity - (_position - _buffer)))
if (length > static_cast<size_t>(_capacity - (_position - _buffer)))
{
return false;
}
Expand All @@ -61,15 +61,15 @@ class BitReader
}

// Note: ReadBytes() API obtains the bits without considering _bit_offset
template<typename T>
bool ReadBytes(T& value, bool big_endian = true)
template <typename T>
bool ReadBytes(T &value, bool big_endian = true)
{
if (sizeof(value) > static_cast<size_t>(_capacity - (_position - _buffer)))
if (sizeof(value) > static_cast<size_t>(_capacity - (_position - _buffer)))
{
return false;
}

if(big_endian == true)
if (big_endian == true)
{
value = ByteReader<T>::ReadBigEndian(_position);
}
Expand All @@ -83,89 +83,72 @@ class BitReader
return true;
}

template<typename T>
T ReadBits(uint8_t bits)
template <typename T>
T ReadBits(uint8_t bits)
{
T value;
bool result = ReadBits(bits, value);
if(result == false)

if (ReadBits<T>(bits, value))
{
return 0;
return value;
}

return value;
return static_cast<T>(0);
}

template<typename T>
bool ReadBits(uint8_t bits, T& value)
{
if (bits > sizeof(value) * 8)
{
OV_ASSERT2(false);
return false;
}
value = 0;
if (bits == 0)
template <typename T>
bool ReadBits(uint8_t bits, T &value)
{
if constexpr (std::is_enum_v<T>)
{
return 0;
}
auto underlying_value = static_cast<std::underlying_type_t<T>>(value);
if (ReadBitsInternal(bits, underlying_value))
{
value = static_cast<T>(underlying_value);
return true;
}

if (static_cast<size_t>((bits + 7) / 8) > static_cast<size_t>(_capacity - (_position - _buffer)))
{
return false;
}

while (bits)
{
const uint8_t bits_from_this_byte = std::min(bits >= 8 ? 8 : bits % 8, 8 - _bit_offset);
const uint8_t mask_offset = 8 - bits_from_this_byte - _bit_offset;
const uint8_t mask = ((1 << bits_from_this_byte ) - 1) << mask_offset;
value <<= bits_from_this_byte;
value |= (*_position & mask) >> mask_offset;
bits -= bits_from_this_byte;
_bit_offset += bits_from_this_byte;
if (_bit_offset == 8)
{
NextPosition();
_bit_offset = 0;
}
}
return true;
}
else
{
return ReadBitsInternal(bits, value);
}
}

bool ReadBit(uint8_t &value)
{
return ReadBits<uint8_t>(1, value);
}
{
return ReadBits<uint8_t>(1, value);
}

bool ReadBoolBit()
bool ReadBoolBit()
{
bool value;
bool result = ReadBit(value);
if(result == false)
if (result == false)
{
return false;
}

return value;
}

bool ReadBit(bool &value)
{
uint8_t bit;
if (ReadBit(bit))
{
value = bit == 1 ? true : false;
return true;
}
return false;
}
bool ReadBit(bool &value)
{
uint8_t bit;
if (ReadBit(bit))
{
value = bit == 1 ? true : false;
return true;
}
return false;
}

uint8_t ReadBit()
{
uint8_t value;
bool result = ReadBit(value);
if(result == false)
if (result == false)
{
return 0;
}
Expand All @@ -180,7 +163,7 @@ class BitReader

size_t BytesSetionConsumed()
{
if(_lap_position == nullptr)
if (_lap_position == nullptr)
{
return 0;
}
Expand All @@ -189,7 +172,7 @@ class BitReader
return bytes;
}

const uint8_t* CurrentPosition()
const uint8_t *CurrentPosition()
{
return _position;
}
Expand All @@ -204,26 +187,66 @@ class BitReader
return (_capacity * 8) - BitsConsumed();
}

size_t BytesConsumed() const
{
return _position - _buffer;
}
size_t BytesConsumed() const
{
return _position - _buffer;
}

size_t BitsConsumed() const
{
return (BytesConsumed() * 8) + _bit_offset;
}
size_t BitsConsumed() const
{
return (BytesConsumed() * 8) + _bit_offset;
}

protected:
template <typename T>
bool ReadBitsInternal(uint8_t bits, T &value)
{
if (bits > sizeof(value) * 8)
{
OV_ASSERT2(false);
return false;
}

value = 0;

if (bits == 0)
{
return true;
}

if (static_cast<size_t>((bits + 7) / 8) > static_cast<size_t>(_capacity - (_position - _buffer)))
{
return false;
}

while (bits)
{
const uint8_t bits_from_this_byte = std::min(bits >= 8 ? 8 : bits % 8, 8 - _bit_offset);
const uint8_t mask_offset = 8 - bits_from_this_byte - _bit_offset;
const uint8_t mask = ((1 << bits_from_this_byte) - 1) << mask_offset;
value <<= bits_from_this_byte;
value |= (*_position & mask) >> mask_offset;
bits -= bits_from_this_byte;
_bit_offset += bits_from_this_byte;
if (_bit_offset == 8)
{
NextPosition();
_bit_offset = 0;
}
}

return true;
}

virtual void NextPosition()
{
_position ++;
_position++;
}

const uint8_t* _buffer;
const uint8_t* _position;
const uint8_t* _lap_position;
size_t _capacity;
int _bit_offset = 0;
uint8_t _mask = 0x80;
const uint8_t *_buffer;
const uint8_t *_position;
const uint8_t *_lap_position;
size_t _capacity;
int _bit_offset = 0;
uint8_t _mask = 0x80;
};
10 changes: 10 additions & 0 deletions src/projects/base/ovlibrary/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ namespace ov
return IsEqual(data->GetData(), data->GetLength());
}

bool Data::IsEqual(const std::shared_ptr<const Data> &data) const
{
return IsEqual(data->GetData(), data->GetLength());
}

bool Data::IsEqual(const std::shared_ptr<Data> &data) const
{
return IsEqual(data->GetData(), data->GetLength());
}

bool Data::IsEmpty() const
{
return (GetLength() == 0);
Expand Down
6 changes: 2 additions & 4 deletions src/projects/base/ovlibrary/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,8 @@ namespace ov
bool IsEqual(const void *data, size_t length) const;
bool IsEqual(const Data &data) const;
bool IsEqual(const Data *data) const;
bool IsEqual(const std::shared_ptr<Data> &data) const
{
return IsEqual(data->GetData(), data->GetLength());
}
bool IsEqual(const std::shared_ptr<const Data> &data) const;
bool IsEqual(const std::shared_ptr<Data> &data) const;

bool IsEmpty() const;

Expand Down
6 changes: 3 additions & 3 deletions src/projects/mediarouter/mediarouter_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ bool MediaRouteStream::ProcessAACRawStream(std::shared_ptr<MediaTrack> &media_tr
return false;
}

media_track->SetSampleRate(audio_config->SamplerateNum());
media_track->SetSampleRate(audio_config->Samplerate());
media_track->GetChannel().SetLayout(audio_config->Channel() == 1 ? AudioChannel::Layout::LayoutMono : AudioChannel::Layout::LayoutStereo);

media_track->SetDecoderConfigurationRecord(audio_config);
Expand Down Expand Up @@ -611,10 +611,10 @@ bool MediaRouteStream::ProcessAACAdtsStream(std::shared_ptr<MediaTrack> &media_t

auto audio_config = std::make_shared<AudioSpecificConfig>();
audio_config->SetObjectType(adts.ObjectType());
audio_config->SetSamplingFrequency(adts.Samplerate());
audio_config->SetSamplingFrequencyIndex(adts.SamplingFrequencyIndex());
audio_config->SetChannel(adts.ChannelConfiguration());

media_track->SetSampleRate(audio_config->SamplerateNum());
media_track->SetSampleRate(audio_config->Samplerate());
media_track->GetChannel().SetLayout(audio_config->Channel() == 1 ? AudioChannel::Layout::LayoutMono : AudioChannel::Layout::LayoutStereo);

media_track->SetDecoderConfigurationRecord(audio_config);
Expand Down
Loading

0 comments on commit db953db

Please sign in to comment.