Tag Parser: mp4/mpeg4descriptor.cpp Source File

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

Loading...
Searching...
No Matches
mpeg4descriptor.cpp
Go to the documentation of this file.
1#include "./mpeg4descriptor.h"
2#include "./mp4container.h"
3#include "./mp4ids.h"
4
5#include <c++utilities/conversion/stringbuilder.h>
6#include <c++utilities/conversion/stringconversion.h>
7#include <c++utilities/io/binaryreader.h>
8
9using namespace std;
10using namespace CppUtilities;
11
12namespace TagParser {
13
18
23Mpeg4Descriptor::Mpeg4Descriptor(ContainerType &container, std::uint64_t startOffset, std::uint64_t maxSize)
24 : GenericFileElement<Mpeg4Descriptor>(container, startOffset, maxSize)
25{
26}
27
31 Mpeg4Descriptor::Mpeg4Descriptor(Mpeg4Descriptor &parent, std::uint64_t startOffset)
32 : GenericFileElement<Mpeg4Descriptor>(parent, startOffset)
33{
34}
35
39string Mpeg4Descriptor::parsingContext() const
40{
41 return "parsing " % idToString() % " descriptor at " + startOffset();
42}
43
47std::string Mpeg4Descriptor::idToString() const
48{
49 return "0x" + numberToString(id(), static_cast<std::uint8_t>(16));
50}
51
56 void Mpeg4Descriptor::internalParse(Diagnostics &diag)
57{
58 if (maxTotalSize() < minimumElementSize()) {
59 diag.emplace_back(DiagLevel::Critical,
60 "Descriptor is smaller than 2 byte and hence invalid. The maximum size within the encloding element is " % numberToString(maxTotalSize())
61 + '.',
62 "parsing MPEG-4 descriptor");
63 throw TruncatedDataException();
64 }
65 stream().seekg(static_cast<streamoff>(startOffset()));
66 // read ID
67 m_idLength = m_sizeLength = 1;
68 m_id = reader().readByte();
69 // read data size
70 std::uint8_t tmp = reader().readByte();
71 m_dataSize = tmp & 0x7F;
72 while (tmp & 0x80) {
73 m_dataSize = (m_dataSize << 7) | ((tmp = reader().readByte()) & 0x7F);
74 ++m_sizeLength;
75 }
76 // check whether the denoted data size exceeds the available data size
77 if (maxTotalSize() < totalSize()) {
78 diag.emplace_back(DiagLevel::Warning, "The descriptor seems to be truncated; unable to parse siblings of that ", parsingContext());
79 m_dataSize = static_cast<std::uint32_t>(maxTotalSize()); // using max size instead
80 }
81 m_firstChild.reset();
82
83 // check for siblings
84 if (totalSize() >= maxTotalSize()) {
85 m_nextSibling.reset();
86 return;
87 }
88 if (parent()) {
89 m_nextSibling.reset(new Mpeg4Descriptor(*(parent()), startOffset() + totalSize()));
90 } else {
91 m_nextSibling.reset(new Mpeg4Descriptor(container(), startOffset() + totalSize(), maxTotalSize() - totalSize()));
92 }
93}
94
95} // namespace TagParser
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition diagnostics.h:156
TagParser::GenericFileElement< Mpeg4Descriptor >::GenericFileElement
GenericFileElement(ContainerType &container, std::uint64_t startOffset)
TagParser::GenericFileElement< Mpeg4Descriptor >::m_dataSize
DataSizeType m_dataSize
Definition genericfileelement.h:138
TagParser::GenericFileElement< Mpeg4Descriptor >::startOffset
std::uint64_t startOffset() const
Definition genericfileelement.h:266
TagParser::GenericFileElement< Mpeg4Descriptor >::m_idLength
std::uint32_t m_idLength
Definition genericfileelement.h:139
TagParser::GenericFileElement< Mpeg4Descriptor >::ContainerType
typename FileElementTraits< Mpeg4Descriptor >::ContainerType ContainerType
Definition genericfileelement.h:51
TagParser::GenericFileElement< Mpeg4Descriptor >::stream
std::iostream & stream()
Definition genericfileelement.h:242
TagParser::GenericFileElement< Mpeg4Descriptor >::m_nextSibling
std::unique_ptr< Mpeg4Descriptor > m_nextSibling
Definition genericfileelement.h:142
TagParser::GenericFileElement< Mpeg4Descriptor >::parent
Mpeg4Descriptor * parent()
Definition genericfileelement.h:390
TagParser::GenericFileElement< Mpeg4Descriptor >::minimumElementSize
static constexpr std::uint8_t minimumElementSize()
Definition genericfileelement.h:996
TagParser::GenericFileElement< Mpeg4Descriptor >::m_firstChild
std::unique_ptr< Mpeg4Descriptor > m_firstChild
Definition genericfileelement.h:143
TagParser::GenericFileElement< Mpeg4Descriptor >::m_id
IdentifierType m_id
Definition genericfileelement.h:135
TagParser::GenericFileElement< Mpeg4Descriptor >::totalSize
std::uint64_t totalSize() const
Definition genericfileelement.h:348
TagParser::GenericFileElement< Mpeg4Descriptor >::container
ContainerType & container()
Definition genericfileelement.h:225
TagParser::GenericFileElement< Mpeg4Descriptor >::reader
CppUtilities::BinaryReader & reader()
Definition genericfileelement.h:250
TagParser::GenericFileElement< Mpeg4Descriptor >::maxTotalSize
std::uint64_t maxTotalSize() const
Definition genericfileelement.h:367
TagParser::GenericFileElement< Mpeg4Descriptor >::m_sizeLength
std::uint32_t m_sizeLength
Definition genericfileelement.h:140
TagParser::Mpeg4Descriptor::idToString
std::string idToString() const
Converts the specified atom ID to a printable string.
Definition mpeg4descriptor.cpp:47
TagParser::Mpeg4Descriptor::Mpeg4Descriptor
Mpeg4Descriptor(ContainerType &container, std::uint64_t startOffset, std::uint64_t maxSize)
Constructs a new top level descriptor with the specified container at the specified startOffset and w...
Definition mpeg4descriptor.cpp:23
TagParser::Mpeg4Descriptor::internalParse
void internalParse(Diagnostics &diag)
Parses the MPEG-4 descriptor.
Definition mpeg4descriptor.cpp:56
TagParser::TruncatedDataException
The exception that is thrown when the data to be parsed is truncated and therefore can not be parsed ...
Definition exceptions.h:39
mp4container.h
mp4ids.h
mpeg4descriptor.h
CppUtilities
Definition abstractcontainer.h:16
TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
TagParser::DiagLevel::Warning
@ Warning
Definition diagnostics.h:20
TagParser::DiagLevel::Critical
@ Critical
Definition diagnostics.h:21