tagparser/avc/avcconfiguration.cpp

90 lines
2.6 KiB
C++
Raw Normal View History

2015-09-06 19:57:33 +02:00
#include "./avcconfiguration.h"
2015-04-22 19:22:01 +02:00
#include "../diagnostics.h"
2016-02-17 20:19:05 +01:00
#include "../exceptions.h"
#include "../mediaformat.h"
#include <c++utilities/io/binaryreader.h>
#include <limits>
2016-02-17 20:19:05 +01:00
using namespace std;
2019-06-10 22:49:11 +02:00
using namespace CppUtilities;
2016-02-17 20:19:05 +01:00
namespace TagParser {
2015-04-22 19:22:01 +02:00
2016-08-04 00:16:19 +02:00
/*!
* \class AvcConfiguration
* \brief The AvcConfiguration struct provides a parser for AVC configuration.
*/
/*!
* \brief Parses the AVC configuration using the specified \a reader.
* \throws Throws TruncatedDataException() when the config size exceeds the specified \a maxSize.
* \todo Implement logging/reporting parsing errors.
2016-08-04 00:16:19 +02:00
*/
2019-03-13 19:06:42 +01:00
void AvcConfiguration::parse(BinaryReader &reader, std::uint64_t maxSize, Diagnostics &diag)
2016-02-17 20:19:05 +01:00
{
VAR_UNUSED(diag)
2018-03-07 01:17:50 +01:00
if (maxSize < 7) {
2016-02-17 20:19:05 +01:00
throw TruncatedDataException();
}
maxSize -= 7;
reader.stream()->seekg(1, ios_base::cur); // always 1
profileIndication = reader.readByte();
profileCompat = reader.readByte();
levelIndication = reader.readByte();
naluSizeLength = (reader.readByte() & 0x03) + 1;
// read SPS info entries
2019-03-13 19:06:42 +01:00
std::uint8_t entryCount = reader.readByte() & 0x0f;
2016-02-17 20:19:05 +01:00
spsInfos.reserve(entryCount);
2018-03-07 01:17:50 +01:00
for (; entryCount; --entryCount) {
if (maxSize < 2) {
2016-02-17 20:19:05 +01:00
throw TruncatedDataException();
}
spsInfos.emplace_back();
try {
2019-03-13 19:06:42 +01:00
spsInfos.back().parse(
reader, maxSize > numeric_limits<std::uint32_t>::max() ? numeric_limits<std::uint32_t>::max() : static_cast<std::uint32_t>(maxSize));
2018-03-07 01:17:50 +01:00
} catch (const TruncatedDataException &) {
if (spsInfos.back().size > maxSize - 2) {
2016-02-17 20:19:05 +01:00
throw;
}
spsInfos.pop_back();
2018-03-07 01:17:50 +01:00
} catch (const Failure &) {
2016-02-17 20:19:05 +01:00
spsInfos.pop_back();
// TODO: log parsing error
}
maxSize -= spsInfos.back().size;
}
// read PPS info entries
entryCount = reader.readByte();
ppsInfos.reserve(entryCount);
2018-03-07 01:17:50 +01:00
for (; entryCount; --entryCount) {
if (maxSize < 2) {
2016-02-17 20:19:05 +01:00
throw TruncatedDataException();
}
ppsInfos.emplace_back();
try {
2019-03-13 19:06:42 +01:00
ppsInfos.back().parse(
reader, maxSize > numeric_limits<std::uint32_t>::max() ? numeric_limits<std::uint32_t>::max() : static_cast<std::uint32_t>(maxSize));
2018-03-07 01:17:50 +01:00
} catch (const TruncatedDataException &) {
if (ppsInfos.back().size > maxSize - 2) {
2016-02-17 20:19:05 +01:00
throw;
}
ppsInfos.pop_back();
2018-03-07 01:17:50 +01:00
} catch (const Failure &) {
2016-02-17 20:19:05 +01:00
ppsInfos.pop_back();
// TODO: log parsing error
}
maxSize -= ppsInfos.back().size;
}
// ignore remaining data
}
2018-03-07 01:17:50 +01:00
} // namespace TagParser