Tag Parser  7.0.1
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
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 
9 using namespace std;
10 using namespace ConversionUtilities;
11 
12 namespace TagParser {
13 
23 Mpeg4Descriptor::Mpeg4Descriptor(ContainerType &container, uint64 startOffset, uint64 maxSize)
24  : GenericFileElement<Mpeg4Descriptor>(container, startOffset, maxSize)
25 {
26 }
27 
32  : GenericFileElement<Mpeg4Descriptor>(parent, startOffset)
33 {
34 }
35 
39 string Mpeg4Descriptor::parsingContext() const
40 {
41  return "parsing " % idToString() % " descriptor at " + startOffset();
42 }
43 
47 std::string Mpeg4Descriptor::idToString() const
48 {
49  return "0x" + ConversionUtilities::numberToString(id(), 16);
50 }
51 
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(startOffset());
66  // read ID
68  m_id = reader().readByte();
69  // read data size
70  byte 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 = maxTotalSize(); // using max size instead
80  }
81  m_firstChild.reset();
82  Mpeg4Descriptor *sibling = nullptr;
83  if (totalSize() < maxTotalSize()) {
84  if (parent()) {
85  sibling = new Mpeg4Descriptor(*(parent()), startOffset() + totalSize());
86  } else {
88  }
89  }
90  m_nextSibling.reset(sibling);
91 }
92 
93 } // namespace TagParser
std::string idToString() const
Converts the specified atom ID to a printable string.
FileElementTraits< ImplementationType >::ContainerType ContainerType
Specifies the type of the corresponding container.
std::unique_ptr< ImplementationType > m_firstChild
std::unique_ptr< ImplementationType > m_nextSibling
STL namespace.
uint64 startOffset() const
Returns the start offset in the related stream.
uint64 totalSize() const
Returns the total size of the element.
static constexpr byte minimumElementSize()
Returns the mimimum element size.
IoUtilities::BinaryReader & reader()
Returns the related BinaryReader.
void internalParse(Diagnostics &diag)
Parses the MPEG-4 descriptor.
ContainerType & container()
Returns the related container.
uint64 maxTotalSize() const
Returns maximum total size.
Mpeg4Descriptor(ContainerType &container, uint64 startOffset, uint64 maxSize)
Constructs a new top level descriptor with the specified container at the specified startOffset and w...
std::iostream & stream()
Returns the related stream.
ImplementationType * parent()
Returns the parent of the element.