tagparser/adts/adtsframe.h

152 lines
3.2 KiB
C
Raw Normal View History

#ifndef TAG_PARSER_ADTSFRAME_H
#define TAG_PARSER_ADTSFRAME_H
2015-07-18 00:55:35 +02:00
2016-08-29 15:43:05 +02:00
#include "../global.h"
2015-07-18 00:55:35 +02:00
#include <c++utilities/conversion/types.h>
namespace IoUtilities {
class BinaryReader;
}
namespace TagParser {
2015-07-18 00:55:35 +02:00
2018-03-07 01:17:50 +01:00
class TAG_PARSER_EXPORT AdtsFrame {
2015-07-18 00:55:35 +02:00
public:
2018-07-10 14:11:11 +02:00
constexpr AdtsFrame();
2015-07-18 00:55:35 +02:00
void parseHeader(IoUtilities::BinaryReader &reader);
2018-07-10 14:11:11 +02:00
constexpr bool isValid() const;
constexpr bool isMpeg4() const;
constexpr bool hasCrc() const;
constexpr byte mpeg4AudioObjectId() const;
constexpr byte mpeg4SamplingFrequencyIndex() const;
constexpr byte mpeg4ChannelConfig() const;
constexpr uint16 totalSize() const;
constexpr byte headerSize() const;
constexpr uint16 dataSize() const;
constexpr uint16 bufferFullness() const;
constexpr byte frameCount() const;
constexpr uint16 crc() const;
2015-07-18 00:55:35 +02:00
private:
uint16 m_header1;
uint64 m_header2;
};
/*!
* \brief Constructs a new frame.
*/
2018-07-10 14:11:11 +02:00
constexpr AdtsFrame::AdtsFrame()
2018-03-07 01:17:50 +01:00
: m_header1(0)
2018-07-10 14:11:11 +02:00
, m_header2(0)
2018-03-07 01:17:50 +01:00
{
}
2015-07-18 00:55:35 +02:00
/*!
* \brief Returns an indication whether the frame is valid.
*/
2018-07-10 14:11:11 +02:00
constexpr bool AdtsFrame::isValid() const
2015-07-18 00:55:35 +02:00
{
return ((m_header1 & 0xFFF6u) == 0xFFF0u) && (totalSize() >= headerSize());
2015-07-18 00:55:35 +02:00
}
/*!
* \brief Returns whether the MPEG version is MPEG-4; otherwise the MPEG version is MPEG-2.
*/
2018-07-10 14:11:11 +02:00
constexpr bool AdtsFrame::isMpeg4() const
2015-07-18 00:55:35 +02:00
{
return m_header1 & 0x8u;
}
/*!
* \brief Returns whether a CRC-16 checksum is present ("protection absent" bit is NOT set).
2015-07-18 00:55:35 +02:00
*/
2018-07-10 14:11:11 +02:00
constexpr bool AdtsFrame::hasCrc() const
2015-07-18 00:55:35 +02:00
{
return (m_header1 & 0x1u) == 0;
2015-07-18 00:55:35 +02:00
}
/*!
* \brief Returns the MPEG-4 audio object type ID.
* \sa TagParser::Mpeg4AudioObjectIds
2015-07-18 00:55:35 +02:00
* \sa Mpeg4AudioObjectIds::idToMediaFormat()
*/
2018-07-10 14:11:11 +02:00
constexpr byte AdtsFrame::mpeg4AudioObjectId() const
2015-07-18 00:55:35 +02:00
{
return (m_header2 >> 0x36) + 0x1u;
}
/*!
* \brief Returns the MPEG-4 sample rate index.
* \sa TagParser::mpeg4SampleRateTable
2015-07-18 00:55:35 +02:00
*/
2018-07-10 14:11:11 +02:00
constexpr byte AdtsFrame::mpeg4SamplingFrequencyIndex() const
2015-07-18 00:55:35 +02:00
{
return (m_header2 >> 0x32) & 0xFu;
}
/*!
* \brief Returns the MPEG-4 channel configuration.
* \sa TagParser::Mpeg4ChannelConfigs
* \sa TagParser::mpeg4SampleRateTable::channelConfigString()
2015-07-18 00:55:35 +02:00
*/
2018-07-10 14:11:11 +02:00
constexpr byte AdtsFrame::mpeg4ChannelConfig() const
2015-07-18 00:55:35 +02:00
{
return (m_header2 >> 0x2E) & 0x7u;
}
/*!
* \brief Returns the size of the frame (including the header) in bytes.
2015-07-18 00:55:35 +02:00
*/
2018-07-10 14:11:11 +02:00
constexpr uint16 AdtsFrame::totalSize() const
2015-07-18 00:55:35 +02:00
{
return (m_header2 >> 0x1D) & 0x1FFFu;
}
/*!
* \brief Retruns the header size in bytes (9 if CRC is present; otherwise 7).
*/
2018-07-10 14:11:11 +02:00
constexpr byte AdtsFrame::headerSize() const
{
return hasCrc() ? 9 : 7;
}
/*!
* \brief Returns the data size (total size minus header size) in bytes.
*/
2018-07-10 14:11:11 +02:00
constexpr uint16 AdtsFrame::dataSize() const
{
return totalSize() - headerSize();
}
2015-07-18 00:55:35 +02:00
/*!
* \brief Returns the buffer fullness.
*/
2018-07-10 14:11:11 +02:00
constexpr uint16 AdtsFrame::bufferFullness() const
2015-07-18 00:55:35 +02:00
{
return (m_header2 >> 0x12) & 0x7FFu;
}
/*!
* \brief Returns the number of AAC frames (RDBs) in the ADTS frame.
*/
2018-07-10 14:11:11 +02:00
constexpr byte AdtsFrame::frameCount() const
2015-07-18 00:55:35 +02:00
{
return ((m_header2 >> 0x10) & 0x3u) + 0x1u;
}
/*!
* \brief Returns the CRC-16 checksum of the frame.
* \sa hasCrc()
*/
2018-07-10 14:11:11 +02:00
constexpr uint16 AdtsFrame::crc() const
2015-07-18 00:55:35 +02:00
{
return m_header2 & 0xFFFFu;
}
2018-03-07 01:17:50 +01:00
} // namespace TagParser
2015-07-18 00:55:35 +02:00
#endif // TAG_PARSER_ADTSFRAME_H