Tag Parser  10.0.1
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
oggpage.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_OGGPAGE_H
2 #define TAG_PARSER_OGGPAGE_H
3 
4 #include "../global.h"
5 
6 #include <cstdint>
7 #include <iosfwd>
8 #include <numeric>
9 #include <vector>
10 
11 namespace TagParser {
12 
14 public:
15  OggPage();
16  OggPage(std::istream &stream, std::uint64_t startOffset, std::int32_t maxSize);
17 
18  void parseHeader(std::istream &stream, std::uint64_t startOffset, std::int32_t maxSize);
19  static std::uint32_t computeChecksum(std::istream &stream, std::uint64_t startOffset);
20  static void updateChecksum(std::iostream &stream, std::uint64_t startOffset);
21 
22  std::uint64_t startOffset() const;
23  std::uint8_t streamStructureVersion() const;
24  std::uint8_t headerTypeFlag() const;
25  bool isContinued() const;
26  bool isFirstpage() const;
27  bool isLastPage() const;
28  std::uint64_t absoluteGranulePosition() const;
29  std::uint32_t streamSerialNumber() const;
30  bool matchesStreamSerialNumber(std::uint32_t streamSerialNumber) const;
31  std::uint32_t sequenceNumber() const;
32  std::uint32_t checksum() const;
33  std::uint8_t segmentTableSize() const;
34  const std::vector<std::uint32_t> &segmentSizes() const;
35  std::uint32_t headerSize() const;
36  std::uint32_t dataSize() const;
37  std::uint32_t totalSize() const;
38  std::uint64_t dataOffset(std::vector<std::uint32_t>::size_type segmentIndex = 0) const;
39  static std::uint32_t makeSegmentSizeDenotation(std::ostream &stream, std::uint32_t size);
40 
41 private:
42  std::uint64_t m_startOffset;
43  std::uint8_t m_streamStructureVersion;
44  std::uint8_t m_headerTypeFlag;
45  std::uint64_t m_absoluteGranulePosition;
46  std::uint32_t m_streamSerialNumber;
47  std::uint32_t m_sequenceNumber;
48  std::uint32_t m_checksum;
49  std::uint8_t m_segmentCount;
50  std::vector<std::uint32_t> m_segmentSizes;
51 };
52 
57  : m_startOffset(0)
58  , m_streamStructureVersion(0)
59  , m_headerTypeFlag(0)
60  , m_absoluteGranulePosition(0)
61  , m_streamSerialNumber(0)
62  , m_sequenceNumber(0)
63  , m_checksum(0)
64  , m_segmentCount(0)
65 {
66 }
67 
72 inline OggPage::OggPage(std::istream &stream, std::uint64_t startOffset, std::int32_t maxSize)
73  : OggPage()
74 {
75  parseHeader(stream, startOffset, maxSize);
76 }
77 
83 inline std::uint64_t OggPage::startOffset() const
84 {
85  return m_startOffset;
86 }
87 
91 inline std::uint8_t OggPage::streamStructureVersion() const
92 {
93  return m_streamStructureVersion;
94 }
95 
102 inline std::uint8_t OggPage::headerTypeFlag() const
103 {
104  return m_headerTypeFlag;
105 }
106 
110 inline bool OggPage::isContinued() const
111 {
112  return m_headerTypeFlag & 0x01;
113 }
114 
118 inline bool OggPage::isFirstpage() const
119 {
120  return m_headerTypeFlag & 0x02;
121 }
122 
126 inline bool OggPage::isLastPage() const
127 {
128  return m_headerTypeFlag & 0x04;
129 }
130 
142 inline std::uint64_t OggPage::absoluteGranulePosition() const
143 {
144  return m_absoluteGranulePosition;
145 }
146 
155 inline std::uint32_t OggPage::streamSerialNumber() const
156 {
157  return m_streamSerialNumber;
158 }
159 
164 inline bool OggPage::matchesStreamSerialNumber(std::uint32_t streamSerialNumber) const
165 {
166  return m_streamSerialNumber == streamSerialNumber;
167 }
168 
174 inline std::uint32_t OggPage::sequenceNumber() const
175 {
176  return m_sequenceNumber;
177 }
178 
189 inline std::uint32_t OggPage::checksum() const
190 {
191  return m_checksum;
192 }
193 
199 inline std::uint8_t OggPage::segmentTableSize() const
200 {
201  return m_segmentCount;
202 }
203 
209 inline const std::vector<std::uint32_t> &OggPage::segmentSizes() const
210 {
211  return m_segmentSizes;
212 }
213 
219 inline std::uint32_t OggPage::headerSize() const
220 {
221  return 27 + m_segmentCount;
222 }
223 
227 inline std::uint32_t OggPage::dataSize() const
228 {
229  return std::accumulate(m_segmentSizes.cbegin(), m_segmentSizes.cend(), 0u);
230 }
231 
235 inline std::uint32_t OggPage::totalSize() const
236 {
237  return headerSize() + dataSize();
238 }
239 
248 inline std::uint64_t OggPage::dataOffset(std::vector<std::uint32_t>::size_type segmentIndex) const
249 {
250  return startOffset() + headerSize()
251  + std::accumulate<decltype(m_segmentSizes)::const_iterator, std::uint64_t>(
252  m_segmentSizes.cbegin(), m_segmentSizes.cbegin() + static_cast<decltype(m_segmentSizes)::difference_type>(segmentIndex), 0u);
253 }
254 
255 } // namespace TagParser
256 
257 #endif // TAG_PARSER_OGGPAGE_H
The OggPage class is used to parse OGG pages.
Definition: oggpage.h:13
std::uint32_t sequenceNumber() const
Returns the page sequence number.
Definition: oggpage.h:174
std::uint32_t dataSize() const
Returns the data size in byte.
Definition: oggpage.h:227
void parseHeader(std::istream &stream, std::uint64_t startOffset, std::int32_t maxSize)
Parses the header read from the specified stream at the specified startOffset.
Definition: oggpage.cpp:24
bool isFirstpage() const
Returns whether this page is the first page of the logical bitstream.
Definition: oggpage.h:118
std::uint32_t totalSize() const
Returns the total size of the page in byte.
Definition: oggpage.h:235
std::uint64_t dataOffset(std::vector< std::uint32_t >::size_type segmentIndex=0) const
Returns the data offset of the segment with the specified segmentIndex.
Definition: oggpage.h:248
std::uint32_t streamSerialNumber() const
Returns the stream serial number.
Definition: oggpage.h:155
const std::vector< std::uint32_t > & segmentSizes() const
Returns the sizes of the segments of the page in byte.
Definition: oggpage.h:209
std::uint8_t headerTypeFlag() const
Returns the header type flag.
Definition: oggpage.h:102
OggPage()
Constructs a new OGG page.
Definition: oggpage.h:56
std::uint64_t startOffset() const
Returns the start offset of the page.
Definition: oggpage.h:83
bool matchesStreamSerialNumber(std::uint32_t streamSerialNumber) const
Returns whether the stream serial number of the current instance matches the specified one.
Definition: oggpage.h:164
std::uint32_t checksum() const
Returns the page checksum.
Definition: oggpage.h:189
std::uint8_t streamStructureVersion() const
Returns the stream structure version.
Definition: oggpage.h:91
bool isLastPage() const
Returns whether this page is the last page of the logical bitstream.
Definition: oggpage.h:126
bool isContinued() const
Returns whether this page is a continued packed (true) or a fresh packed (false).
Definition: oggpage.h:110
std::uint8_t segmentTableSize() const
Returns the size of the segment table.
Definition: oggpage.h:199
std::uint64_t absoluteGranulePosition() const
Returns the absolute granule position.
Definition: oggpage.h:142
std::uint32_t headerSize() const
Returns the header size in byte.
Definition: oggpage.h:219
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10