Tag Parser  6.4.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/stringconversion.h>
6 #include <c++utilities/conversion/stringbuilder.h>
7 #include <c++utilities/io/binaryreader.h>
8 
9 using namespace std;
10 using namespace ConversionUtilities;
11 
12 namespace Media {
13 
23 Mpeg4Descriptor::Mpeg4Descriptor(containerType &container, uint64 startOffset, uint64 maxSize) :
24  GenericFileElement<Mpeg4Descriptor>(container, startOffset, maxSize)
25 {}
26 
31  GenericFileElement<Mpeg4Descriptor>(parent, startOffset)
32 {}
33 
37 string Mpeg4Descriptor::parsingContext() const
38 {
39  return "parsing " % idToString() % " descriptor at " + startOffset();
40 }
41 
45 std::string Mpeg4Descriptor::idToString() const
46 {
47  return "0x" + ConversionUtilities::numberToString(id(), 16);
48 }
49 
55 {
58  addNotification(NotificationType::Critical, "Descriptor is smaller than 2 byte and hence invalid. The maximum size within the encloding element is " % numberToString(maxTotalSize()) + '.', "parsing MPEG-4 descriptor");
59  throw TruncatedDataException();
60  }
61  stream().seekg(startOffset());
62  // read ID
64  m_id = reader().readByte();
65  // read data size
66  byte tmp = reader().readByte();
67  m_dataSize = tmp & 0x7F;
68  while(tmp & 0x80) {
69  m_dataSize = (m_dataSize << 7) | ((tmp = reader().readByte()) & 0x7F);
70  ++m_sizeLength;
71  }
72  // check whether the denoted data size exceeds the available data size
73  if(maxTotalSize() < totalSize()) {
74  addNotification(NotificationType::Warning, "The descriptor seems to be truncated; unable to parse siblings of that ", parsingContext());
75  m_dataSize = maxTotalSize(); // using max size instead
76  }
77  m_firstChild.reset();
78  implementationType *sibling = nullptr;
79  if(totalSize() < maxTotalSize()) {
80  if(parent()) {
81  sibling = new implementationType(*(parent()), startOffset() + totalSize());
82  } else {
84  }
85  }
86  m_nextSibling.reset(sibling);
87 }
88 
89 }
uint64 startOffset() const
Returns the start offset in the related stream.
Mpeg4Descriptor(containerType &container, uint64 startOffset, uint64 maxSize)
Constructs a new top level descriptor with the specified container at the specified startOffset and w...
void invalidateStatus()
Invalidates the current status.
std::iostream & stream()
Returns the related stream.
IoUtilities::BinaryReader & reader()
Returns the related BinaryReader.
static constexpr byte minimumElementSize()
Returns the mimimum element size.
std::unique_ptr< implementationType > m_firstChild
FileElementTraits< Mpeg4Descriptor >::implementationType implementationType
Specifies the type of the actual implementation.
uint64 totalSize() const
Returns the total size of the element.
STL namespace.
void addNotification(const Notification &notification)
This method is meant to be called by the derived class to add a notification.
The Mpeg4Descriptor class helps to parse MPEG-4 descriptors.
std::unique_ptr< implementationType > m_nextSibling
The GenericFileElement class helps to parse binary files which consist of an arboreal element strucut...
uint64 maxTotalSize() const
Returns maximum total size.
The exception that is thrown when the data to be parsed is truncated and therefore can not be parsed ...
Definition: exceptions.h:35
implementationType * parent()
Returns the parent of the element.
std::string idToString() const
Converts the specified atom ID to a printable string.
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
containerType & container()
Returns the related container.
void internalParse()
Parses the MPEG-4 descriptor.