Tag Parser: mpegaudio/mpegaudioframe.h Source File

Tag Parser 12.5.3 C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags

Loading...
Searching...
No Matches
mpegaudioframe.h
Go to the documentation of this file.
1#ifndef TAG_PARSER_MP3FRAMEAUDIOSTREAM_H
2#define TAG_PARSER_MP3FRAMEAUDIOSTREAM_H
3
4#include "../diagnostics.h"
5
6#include <cstdint>
7#include <iostream>
8#include <string_view>
9
10namespace CppUtilities {
11class BinaryReader;
12}
13
14namespace TagParser {
15
19 enum class MpegChannelMode {
20 Stereo,
21 JointStereo,
22 DualChannel,
23 SingleChannel,
24 Unspecifed
25};
26
27 TAG_PARSER_EXPORT std::string_view mpegChannelModeString(MpegChannelMode channelMode);
28
29 enum class XingHeaderFlags {
30 None = 0x0u,
31 HasFramesField = 0x1u,
32 HasBytesField = 0x2u,
33 HasTocField = 0x4u,
34 HasQualityIndicator = 0x8u
35};
36
37 class TAG_PARSER_EXPORT MpegAudioFrame {
38public:
39 constexpr MpegAudioFrame();
40
41 void parseHeader(CppUtilities::BinaryReader &reader, Diagnostics &diag);
42
43 constexpr bool isValid() const;
44 double mpegVersion() const;
45 int layer() const;
46 constexpr bool isProtectedByCrc() const;
47 std::uint16_t bitrate() const;
48 std::uint32_t samplingFrequency() const;
49 constexpr std::uint32_t paddingSize() const;
50 MpegChannelMode channelMode() const;
51 constexpr bool hasCopyright() const;
52 constexpr bool isOriginal() const;
53 std::uint32_t sampleCount() const;
54 std::uint32_t size() const;
55 constexpr bool isXingHeaderAvailable() const;
56 constexpr XingHeaderFlags xingHeaderFlags() const;
57 constexpr bool isXingFramefieldPresent() const;
58 constexpr bool isXingBytesfieldPresent() const;
59 constexpr bool isXingTocFieldPresent() const;
60 constexpr bool isXingQualityIndicatorFieldPresent() const;
61 constexpr std::uint32_t xingFrameCount() const;
62 constexpr std::uint32_t xingBytesfield() const;
63 constexpr std::uint32_t xingQualityIndicator() const;
64
65private:
66 static constexpr std::uint64_t s_xingHeaderOffset = 0x24;
67 static constexpr std::uint32_t s_sync = 0xFFE00000u;
68 static const std::uint16_t s_bitrateTable[0x2][0x3][0xF];
69 std::uint32_t m_header;
70 std::uint64_t m_xingHeader;
71 XingHeaderFlags m_xingHeaderFlags;
72 std::uint32_t m_xingFramefield;
73 std::uint32_t m_xingBytesfield;
74 std::uint32_t m_xingQualityIndicator;
75};
76
80 constexpr MpegAudioFrame::MpegAudioFrame()
81 : m_header(0)
82 , m_xingHeader(0)
83 , m_xingHeaderFlags(XingHeaderFlags::None)
84 , m_xingFramefield(0)
85 , m_xingBytesfield(0)
86 , m_xingQualityIndicator(0)
87{
88}
89
93 constexpr bool MpegAudioFrame::isValid() const
94{
95 return (m_header & s_sync) == s_sync;
96}
97
101 constexpr bool MpegAudioFrame::isProtectedByCrc() const
102{
103 return (m_header & 0x10000u) != 0x10000u;
104}
105
109 inline std::uint16_t MpegAudioFrame::bitrate() const
110{
111 if (mpegVersion() > 0.0 && layer() > 0) {
112 return s_bitrateTable[mpegVersion() == 1.0 ? 0 : 1][layer() - 1][(m_header & 0xf000u) >> 12];
113 } else {
114 return 0;
115 }
116}
117
121 constexpr std::uint32_t MpegAudioFrame::paddingSize() const
122{
123 if (isValid() && (m_header & 0x200u)) {
124 return (m_header & 0x60000u) == 0x60000u ? 4u /* layer 1 */ : 1u /* layer 2 and 3 */;
125 } else {
126 return 0;
127 }
128}
129
133 constexpr bool MpegAudioFrame::hasCopyright() const
134{
135 return (m_header & 0x8u) == 0x8u;
136}
137
141 constexpr bool MpegAudioFrame::isOriginal() const
142{
143 return (m_header & 0x4u) == 0x4u;
144}
145
146 constexpr XingHeaderFlags operator|(XingHeaderFlags lhs, XingHeaderFlags rhs)
147{
148 return static_cast<XingHeaderFlags>(static_cast<int>(lhs) | static_cast<int>(rhs));
149}
150
151 constexpr XingHeaderFlags operator&(XingHeaderFlags lhs, XingHeaderFlags rhs)
152{
153 return static_cast<XingHeaderFlags>(static_cast<int>(lhs) & static_cast<int>(rhs));
154}
155
159 constexpr bool MpegAudioFrame::isXingHeaderAvailable() const
160{
161 return ((m_xingHeader & 0x58696e6700000000uL) == 0x58696e6700000000uL) || ((m_xingHeader & 0x496e666f00000000uL) == 0x496e666f00000000uL);
162}
163
167 constexpr XingHeaderFlags MpegAudioFrame::xingHeaderFlags() const
168{
169 return m_xingHeaderFlags;
170}
171
175 constexpr bool MpegAudioFrame::isXingFramefieldPresent() const
176{
177 return isXingHeaderAvailable() && ((m_xingHeaderFlags & XingHeaderFlags::HasFramesField) == XingHeaderFlags::HasFramesField);
178}
179
183 constexpr bool MpegAudioFrame::isXingBytesfieldPresent() const
184{
185 return isXingHeaderAvailable() && ((m_xingHeaderFlags & XingHeaderFlags::HasBytesField) == XingHeaderFlags::HasBytesField);
186}
187
191 constexpr bool MpegAudioFrame::isXingTocFieldPresent() const
192{
193 return isXingHeaderAvailable() && ((m_xingHeaderFlags & XingHeaderFlags::HasTocField) == XingHeaderFlags::HasTocField);
194}
195
199 constexpr bool MpegAudioFrame::isXingQualityIndicatorFieldPresent() const
200{
201 return isXingHeaderAvailable() && ((m_xingHeaderFlags & XingHeaderFlags::HasQualityIndicator) == XingHeaderFlags::HasQualityIndicator);
202}
203
207 constexpr std::uint32_t MpegAudioFrame::xingFrameCount() const
208{
209 return m_xingFramefield;
210}
211
215 constexpr std::uint32_t MpegAudioFrame::xingBytesfield() const
216{
217 return m_xingBytesfield;
218}
219
223 constexpr std::uint32_t MpegAudioFrame::xingQualityIndicator() const
224{
225 return m_xingQualityIndicator;
226}
227
228} // namespace TagParser
229
230#endif // TAG_PARSER_MP3FRAMEAUDIOSTREAM_H
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition diagnostics.h:156
TagParser::MpegAudioFrame::size
std::uint32_t size() const
Returns the size if known; otherwise returns 0.
Definition mpegaudioframe.cpp:208
TagParser::MpegAudioFrame::isXingQualityIndicatorFieldPresent
constexpr bool isXingQualityIndicatorFieldPresent() const
Returns an indication whether the Xing quality indicator field is present.
Definition mpegaudioframe.h:199
TagParser::MpegAudioFrame::samplingFrequency
std::uint32_t samplingFrequency() const
Returns the sampeling frequency of the frame if known; otherwise returns 0.
Definition mpegaudioframe.cpp:122
TagParser::MpegAudioFrame::channelMode
MpegChannelMode channelMode() const
Returns the channel mode if known; otherwise returns MpegChannelMode::Unspecifed.
Definition mpegaudioframe.cpp:164
TagParser::MpegAudioFrame::isProtectedByCrc
constexpr bool isProtectedByCrc() const
Returns an indication whether the frame is protected by CRC.
Definition mpegaudioframe.h:101
TagParser::MpegAudioFrame::paddingSize
constexpr std::uint32_t paddingSize() const
Returns the padding size if known; otherwise returns 0.
Definition mpegaudioframe.h:121
TagParser::MpegAudioFrame::hasCopyright
constexpr bool hasCopyright() const
Returns an indication whether the frame is copyrighted.
Definition mpegaudioframe.h:133
TagParser::MpegAudioFrame::sampleCount
std::uint32_t sampleCount() const
Returns the sample count if known; otherwise returns 0.
Definition mpegaudioframe.cpp:185
TagParser::MpegAudioFrame::isXingTocFieldPresent
constexpr bool isXingTocFieldPresent() const
Returns an indication whether the Xing TOC is present.
Definition mpegaudioframe.h:191
TagParser::MpegAudioFrame::isValid
constexpr bool isValid() const
Returns an indication whether the frame is valid.
Definition mpegaudioframe.h:93
TagParser::MpegAudioFrame::layer
int layer() const
Returns the MPEG layer if known (1, 2, or 3); otherwise returns 0.
Definition mpegaudioframe.cpp:105
TagParser::MpegAudioFrame::isOriginal
constexpr bool isOriginal() const
Returns an indication whether the frame labeled as original.
Definition mpegaudioframe.h:141
TagParser::MpegAudioFrame::bitrate
std::uint16_t bitrate() const
Returns the bitrate of the frame if known; otherwise returns 0.
Definition mpegaudioframe.h:109
TagParser::MpegAudioFrame::xingFrameCount
constexpr std::uint32_t xingFrameCount() const
Returns an indication whether the Xing frame count is present.
Definition mpegaudioframe.h:207
TagParser::MpegAudioFrame::xingQualityIndicator
constexpr std::uint32_t xingQualityIndicator() const
Returns the Xing quality indicator if known; otherwise returns 0.
Definition mpegaudioframe.h:223
TagParser::MpegAudioFrame::isXingBytesfieldPresent
constexpr bool isXingBytesfieldPresent() const
Returns an indication whether the Xing bytes field is present.
Definition mpegaudioframe.h:183
TagParser::MpegAudioFrame::parseHeader
void parseHeader(CppUtilities::BinaryReader &reader, Diagnostics &diag)
Parses the header read using the specified reader.
Definition mpegaudioframe.cpp:49
TagParser::MpegAudioFrame::isXingHeaderAvailable
constexpr bool isXingHeaderAvailable() const
Returns an indication whether a Xing header is present.
Definition mpegaudioframe.h:159
TagParser::MpegAudioFrame::xingHeaderFlags
constexpr XingHeaderFlags xingHeaderFlags() const
Returns the Xing header flags.
Definition mpegaudioframe.h:167
TagParser::MpegAudioFrame::mpegVersion
double mpegVersion() const
Returns the MPEG version if known (1.0, 2.0 or 2.5); otherwise returns 0.
Definition mpegaudioframe.cpp:88
TagParser::MpegAudioFrame::isXingFramefieldPresent
constexpr bool isXingFramefieldPresent() const
Returns an indication whether the Xing frame field is present.
Definition mpegaudioframe.h:175
TagParser::MpegAudioFrame::xingBytesfield
constexpr std::uint32_t xingBytesfield() const
Returns the Xing bytes field if known; otherwise returns 0.
Definition mpegaudioframe.h:215
TagParser::MpegAudioFrame::MpegAudioFrame
constexpr MpegAudioFrame()
Constructs a new frame.
Definition mpegaudioframe.h:80
diagnostics.h
TAG_PARSER_EXPORT
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
Definition global.h:14
CppUtilities
Definition abstractcontainer.h:16
TagParser::SubFormats::None
@ None
Definition mediaformat.h:112
TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
TagParser::mpegChannelModeString
TAG_PARSER_EXPORT std::string_view mpegChannelModeString(MpegChannelMode channelMode)
Returns the string representation for the specified channelMode.
Definition mpegaudioframe.cpp:17
TagParser::operator|
constexpr XingHeaderFlags operator|(XingHeaderFlags lhs, XingHeaderFlags rhs)
Definition mpegaudioframe.h:146
TagParser::XingHeaderFlags
XingHeaderFlags
Definition mpegaudioframe.h:29
TagParser::XingHeaderFlags::HasTocField
@ HasTocField
Definition mpegaudioframe.h:33
TagParser::XingHeaderFlags::HasFramesField
@ HasFramesField
Definition mpegaudioframe.h:31
TagParser::XingHeaderFlags::HasBytesField
@ HasBytesField
Definition mpegaudioframe.h:32
TagParser::XingHeaderFlags::HasQualityIndicator
@ HasQualityIndicator
Definition mpegaudioframe.h:34
TagParser::operator&
constexpr XingHeaderFlags operator&(XingHeaderFlags lhs, XingHeaderFlags rhs)
Definition mpegaudioframe.h:151
TagParser::MpegChannelMode
MpegChannelMode
Specifies the channel mode.
Definition mpegaudioframe.h:19
TagParser::MpegChannelMode::DualChannel
@ DualChannel
Definition mpegaudioframe.h:22
TagParser::MpegChannelMode::JointStereo
@ JointStereo
Definition mpegaudioframe.h:21
TagParser::MpegChannelMode::Unspecifed
@ Unspecifed
Definition mpegaudioframe.h:24
TagParser::MpegChannelMode::SingleChannel
@ SingleChannel
Definition mpegaudioframe.h:23
TagParser::MpegChannelMode::Stereo
@ Stereo
Definition mpegaudioframe.h:20