Tag Parser  7.0.0
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 <c++utilities/conversion/types.h>
7 
8 #include <iosfwd>
9 #include <numeric>
10 #include <vector>
11 
12 namespace TagParser {
13 
15 public:
16  OggPage();
17  OggPage(std::istream &stream, uint64 startOffset, int32 maxSize);
18 
19  void parseHeader(std::istream &stream, uint64 startOffset, int32 maxSize);
20  static uint32 computeChecksum(std::istream &stream, uint64 startOffset);
21  static void updateChecksum(std::iostream &stream, uint64 startOffset);
22 
23  uint64 startOffset() const;
24  byte streamStructureVersion() const;
25  byte headerTypeFlag() const;
26  bool isContinued() const;
27  bool isFirstpage() const;
28  bool isLastPage() const;
29  uint64 absoluteGranulePosition() const;
30  uint32 streamSerialNumber() const;
31  bool matchesStreamSerialNumber(uint32 streamSerialNumber) const;
32  uint32 sequenceNumber() const;
33  uint32 checksum() const;
34  byte segmentTableSize() const;
35  const std::vector<uint32> &segmentSizes() const;
36  uint32 headerSize() const;
37  uint32 dataSize() const;
38  uint32 totalSize() const;
39  uint64 dataOffset(byte segmentIndex = 0) const;
40  static uint32 makeSegmentSizeDenotation(std::ostream &stream, uint32 size);
41 
42 private:
43  uint64 m_startOffset;
44  byte m_streamStructureVersion;
45  byte m_headerTypeFlag;
46  uint64 m_absoluteGranulePosition;
47  uint32 m_streamSerialNumber;
48  uint32 m_sequenceNumber;
49  uint32 m_checksum;
50  byte m_segmentCount;
51  std::vector<uint32> m_segmentSizes;
52 };
53 
58  : m_startOffset(0)
59  , m_streamStructureVersion(0)
60  , m_headerTypeFlag(0)
61  , m_absoluteGranulePosition(0)
62  , m_streamSerialNumber(0)
63  , m_sequenceNumber(0)
64  , m_checksum(0)
65  , m_segmentCount(0)
66 {
67 }
68 
73 inline OggPage::OggPage(std::istream &stream, uint64 startOffset, int32 maxSize)
74  : OggPage()
75 {
76  parseHeader(stream, startOffset, maxSize);
77 }
78 
84 inline uint64 OggPage::startOffset() const
85 {
86  return m_startOffset;
87 }
88 
93 {
94  return m_streamStructureVersion;
95 }
96 
103 inline byte OggPage::headerTypeFlag() const
104 {
105  return m_headerTypeFlag;
106 }
107 
111 inline bool OggPage::isContinued() const
112 {
113  return m_headerTypeFlag & 0x01;
114 }
115 
119 inline bool OggPage::isFirstpage() const
120 {
121  return m_headerTypeFlag & 0x02;
122 }
123 
127 inline bool OggPage::isLastPage() const
128 {
129  return m_headerTypeFlag & 0x04;
130 }
131 
143 inline uint64 OggPage::absoluteGranulePosition() const
144 {
145  return m_absoluteGranulePosition;
146 }
147 
156 inline uint32 OggPage::streamSerialNumber() const
157 {
158  return m_streamSerialNumber;
159 }
160 
165 inline bool OggPage::matchesStreamSerialNumber(uint32 streamSerialNumber) const
166 {
167  return m_streamSerialNumber == streamSerialNumber;
168 }
169 
175 inline uint32 OggPage::sequenceNumber() const
176 {
177  return m_sequenceNumber;
178 }
179 
190 inline uint32 OggPage::checksum() const
191 {
192  return m_checksum;
193 }
194 
200 inline byte OggPage::segmentTableSize() const
201 {
202  return m_segmentCount;
203 }
204 
210 inline const std::vector<uint32> &OggPage::segmentSizes() const
211 {
212  return m_segmentSizes;
213 }
214 
220 inline uint32 OggPage::headerSize() const
221 {
222  return 27 + m_segmentCount;
223 }
224 
228 inline uint32 OggPage::dataSize() const
229 {
230  return std::accumulate(m_segmentSizes.cbegin(), m_segmentSizes.cend(), 0u);
231 }
232 
236 inline uint32 OggPage::totalSize() const
237 {
238  return headerSize() + dataSize();
239 }
240 
249 inline uint64 OggPage::dataOffset(byte segmentIndex) const
250 {
251  return startOffset() + headerSize() + std::accumulate(m_segmentSizes.cbegin(), m_segmentSizes.cbegin() + segmentIndex, 0);
252 }
253 
254 } // namespace TagParser
255 
256 #endif // TAG_PARSER_OGGPAGE_H
uint64 dataOffset(byte segmentIndex=0) const
Returns the data offset of the segment with the specified segmentIndex.
Definition: oggpage.h:249
bool isLastPage() const
Returns whether this page is the last page of the logical bitstream.
Definition: oggpage.h:127
byte headerTypeFlag() const
Returns the header type flag.
Definition: oggpage.h:103
uint32 dataSize() const
Returns the data size in byte.
Definition: oggpage.h:228
uint32 totalSize() const
Returns the total size of the page in byte.
Definition: oggpage.h:236
bool matchesStreamSerialNumber(uint32 streamSerialNumber) const
Returns whether the stream serial number of the current instance matches the specified one...
Definition: oggpage.h:165
const std::vector< uint32 > & segmentSizes() const
Returns the sizes of the segments of the page in byte.
Definition: oggpage.h:210
uint32 sequenceNumber() const
Returns the page sequence number.
Definition: oggpage.h:175
uint64 startOffset() const
Returns the start offset of the page.
Definition: oggpage.h:84
void parseHeader(std::istream &stream, uint64 startOffset, int32 maxSize)
Parses the header read from the specified stream at the specified startOffset.
Definition: oggpage.cpp:25
uint32 checksum() const
Returns the page checksum.
Definition: oggpage.h:190
uint32 streamSerialNumber() const
Returns the stream serial number.
Definition: oggpage.h:156
uint64 absoluteGranulePosition() const
Returns the absolute granule position.
Definition: oggpage.h:143
bool isFirstpage() const
Returns whether this page is the first page of the logical bitstream.
Definition: oggpage.h:119
uint32 headerSize() const
Returns the header size in byte.
Definition: oggpage.h:220
bool isContinued() const
Returns whether this page is a continued packed (true) or a fresh packed (false). ...
Definition: oggpage.h:111
OggPage()
Constructs a new OGG page.
Definition: oggpage.h:57
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
byte streamStructureVersion() const
Returns the stream structure version.
Definition: oggpage.h:92
byte segmentTableSize() const
Returns the size of the segment table.
Definition: oggpage.h:200