2015-09-06 19:57:33 +02:00
|
|
|
#include "./matroskatag.h"
|
|
|
|
#include "./ebmlelement.h"
|
2015-04-22 19:22:01 +02:00
|
|
|
|
2018-03-05 17:49:29 +01:00
|
|
|
#include "../diagnostics.h"
|
|
|
|
|
2015-04-22 19:22:01 +02:00
|
|
|
#include <initializer_list>
|
2018-03-07 01:17:50 +01:00
|
|
|
#include <map>
|
2015-04-22 19:22:01 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ConversionUtilities;
|
|
|
|
|
2018-03-06 23:09:15 +01:00
|
|
|
namespace TagParser {
|
2015-04-22 19:22:01 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \class Media::MatroskaTag
|
|
|
|
* \brief Implementation of Media::Tag for the Matroska container.
|
|
|
|
*/
|
|
|
|
|
2017-03-07 17:16:17 +01:00
|
|
|
MatroskaTag::IdentifierType MatroskaTag::internallyGetFieldId(KnownField field) const
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
using namespace MatroskaTagIds;
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (field) {
|
|
|
|
case KnownField::Artist:
|
|
|
|
return artist();
|
|
|
|
case KnownField::Album:
|
|
|
|
return album();
|
|
|
|
case KnownField::Comment:
|
|
|
|
return comment();
|
|
|
|
case KnownField::RecordDate:
|
|
|
|
return dateRecorded();
|
|
|
|
case KnownField::Year:
|
|
|
|
return dateRelease();
|
|
|
|
case KnownField::Title:
|
|
|
|
return title();
|
|
|
|
case KnownField::Genre:
|
|
|
|
return genre();
|
|
|
|
case KnownField::PartNumber:
|
|
|
|
return partNumber();
|
|
|
|
case KnownField::TotalParts:
|
|
|
|
return totalParts();
|
|
|
|
case KnownField::Encoder:
|
|
|
|
return encoder();
|
|
|
|
case KnownField::EncoderSettings:
|
|
|
|
return encoderSettings();
|
|
|
|
case KnownField::Bpm:
|
|
|
|
return bpm();
|
|
|
|
case KnownField::Bps:
|
|
|
|
return bps();
|
|
|
|
case KnownField::Rating:
|
|
|
|
return rating();
|
|
|
|
case KnownField::Description:
|
|
|
|
return description();
|
|
|
|
case KnownField::Lyrics:
|
|
|
|
return lyrics();
|
|
|
|
case KnownField::RecordLabel:
|
|
|
|
return label();
|
|
|
|
case KnownField::Performers:
|
|
|
|
return actor();
|
|
|
|
case KnownField::Lyricist:
|
|
|
|
return lyricist();
|
|
|
|
case KnownField::Composer:
|
|
|
|
return composer();
|
|
|
|
case KnownField::Length:
|
|
|
|
return duration();
|
|
|
|
case KnownField::Language:
|
|
|
|
return language();
|
|
|
|
default:
|
|
|
|
return string();
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 17:16:17 +01:00
|
|
|
KnownField MatroskaTag::internallyGetKnownField(const IdentifierType &id) const
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
using namespace MatroskaTagIds;
|
2018-05-13 20:40:30 +02:00
|
|
|
static const map<string, KnownField> fieldMap({
|
2018-03-07 01:17:50 +01:00
|
|
|
{ artist(), KnownField::Artist },
|
|
|
|
{ album(), KnownField::Album },
|
|
|
|
{ comment(), KnownField::Comment },
|
|
|
|
{ dateRecorded(), KnownField::RecordDate },
|
|
|
|
{ dateRelease(), KnownField::Year },
|
|
|
|
{ title(), KnownField::Title },
|
|
|
|
{ partNumber(), KnownField::PartNumber },
|
|
|
|
{ totalParts(), KnownField::TotalParts },
|
|
|
|
{ encoder(), KnownField::Encoder },
|
|
|
|
{ encoderSettings(), KnownField::EncoderSettings },
|
|
|
|
{ bpm(), KnownField::Bpm },
|
|
|
|
{ bps(), KnownField::Bps },
|
|
|
|
{ rating(), KnownField::Rating },
|
|
|
|
{ description(), KnownField::Description },
|
|
|
|
{ lyrics(), KnownField::Lyrics },
|
|
|
|
{ label(), KnownField::RecordLabel },
|
|
|
|
{ actor(), KnownField::Performers },
|
|
|
|
{ lyricist(), KnownField::Lyricist },
|
|
|
|
{ composer(), KnownField::Composer },
|
|
|
|
{ duration(), KnownField::Length },
|
|
|
|
{ language(), KnownField::Language },
|
2015-04-22 19:22:01 +02:00
|
|
|
});
|
2018-05-13 20:40:30 +02:00
|
|
|
const auto knownField(fieldMap.find(id));
|
|
|
|
return knownField != fieldMap.cend() ? knownField->second : KnownField::Invalid;
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Parses tag information from the specified \a tagElement.
|
|
|
|
*
|
|
|
|
* \throws Throws std::ios_base::failure when an IO error occurs.
|
|
|
|
* \throws Throws Media::Failure or a derived exception when a parsing
|
|
|
|
* error occurs.
|
|
|
|
*/
|
2018-03-05 17:49:29 +01:00
|
|
|
void MatroskaTag::parse(EbmlElement &tagElement, Diagnostics &diag)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
static const string context("parsing Matroska tag");
|
2018-03-05 17:49:29 +01:00
|
|
|
tagElement.parse(diag);
|
2018-06-02 22:56:08 +02:00
|
|
|
if (tagElement.totalSize() > numeric_limits<uint32>::max()) {
|
|
|
|
// FIXME: Support this? Likely not very useful in practise.
|
|
|
|
diag.emplace_back(DiagLevel::Critical, "Matroska tag is too big.", context);
|
|
|
|
throw NotImplementedException();
|
|
|
|
}
|
|
|
|
m_size = static_cast<uint32>(tagElement.totalSize());
|
2018-03-07 01:17:50 +01:00
|
|
|
for (EbmlElement *child = tagElement.firstChild(); child; child = child->nextSibling()) {
|
2018-03-05 17:49:29 +01:00
|
|
|
child->parse(diag);
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (child->id()) {
|
2018-03-11 22:27:12 +01:00
|
|
|
case MatroskaIds::SimpleTag:
|
2015-04-22 19:22:01 +02:00
|
|
|
try {
|
2018-03-11 22:27:12 +01:00
|
|
|
MatroskaTagField field;
|
2018-03-05 17:49:29 +01:00
|
|
|
field.reparse(*child, diag, true);
|
2018-03-11 22:27:12 +01:00
|
|
|
fields().emplace(field.id(), move(field));
|
2018-03-07 01:17:50 +01:00
|
|
|
} catch (const Failure &) {
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
case MatroskaIds::Targets:
|
2018-03-05 17:49:29 +01:00
|
|
|
parseTargets(*child, diag);
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Prepares making.
|
|
|
|
* \returns Returns a MatroskaTagMaker object which can be used to actually make the tag.
|
|
|
|
* \remarks The tag must NOT be mutated after making is prepared when it is intended to actually
|
2017-06-11 01:21:56 +02:00
|
|
|
* make the tag using the make() method of the returned object.
|
2015-04-22 19:22:01 +02:00
|
|
|
* \throws Throws Media::Failure or a derived exception when a making error occurs.
|
|
|
|
*
|
|
|
|
* This method might be useful when it is necessary to know the size of the tag before making it.
|
|
|
|
* \sa make()
|
2017-06-11 01:21:56 +02:00
|
|
|
* \todo Make inline in next major release.
|
2015-04-22 19:22:01 +02:00
|
|
|
*/
|
2018-03-05 17:49:29 +01:00
|
|
|
MatroskaTagMaker MatroskaTag::prepareMaking(Diagnostics &diag)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
2018-03-05 17:49:29 +01:00
|
|
|
return MatroskaTagMaker(*this, diag);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes tag information to the specified \a stream (makes a "Tag"-element).
|
|
|
|
* \throws Throws std::ios_base::failure when an IO error occurs.
|
|
|
|
* \throws Throws Media::Failure or a derived exception when a making
|
|
|
|
* error occurs.
|
|
|
|
* \sa prepareMaking()
|
2017-06-11 01:21:56 +02:00
|
|
|
* \todo Make inline in next major release.
|
2015-04-22 19:22:01 +02:00
|
|
|
*/
|
2018-03-05 17:49:29 +01:00
|
|
|
void MatroskaTag::make(ostream &stream, Diagnostics &diag)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
2018-03-05 17:49:29 +01:00
|
|
|
prepareMaking(diag).make(stream);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Parses the specified \a targetsElement.
|
|
|
|
*
|
|
|
|
* \throws Throws std::ios_base::failure when an IO error occurs.
|
|
|
|
* \throws Throws Media::Failure or a derived exception when a parsing
|
|
|
|
* error occurs.
|
|
|
|
*/
|
2018-03-05 17:49:29 +01:00
|
|
|
void MatroskaTag::parseTargets(EbmlElement &targetsElement, Diagnostics &diag)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
static const string context("parsing targets of Matroska tag");
|
|
|
|
m_target.clear();
|
|
|
|
bool targetTypeValueFound = false;
|
|
|
|
bool targetTypeFound = false;
|
2018-03-05 17:49:29 +01:00
|
|
|
targetsElement.parse(diag);
|
2018-03-07 01:17:50 +01:00
|
|
|
for (EbmlElement *child = targetsElement.firstChild(); child; child = child->nextSibling()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
try {
|
2018-03-05 17:49:29 +01:00
|
|
|
child->parse(diag);
|
2018-03-07 01:17:50 +01:00
|
|
|
} catch (const Failure &) {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Critical, "Unable to parse childs of Targets element.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (child->id()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
case MatroskaIds::TargetTypeValue:
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!targetTypeValueFound) {
|
2015-04-22 19:22:01 +02:00
|
|
|
m_target.setLevel(child->readUInteger());
|
|
|
|
targetTypeValueFound = true;
|
|
|
|
} else {
|
2018-03-07 01:17:50 +01:00
|
|
|
diag.emplace_back(
|
|
|
|
DiagLevel::Warning, "Targets element contains multiple TargetTypeValue elements. Surplus elements will be ignored.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MatroskaIds::TargetType:
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!targetTypeFound) {
|
2015-04-22 19:22:01 +02:00
|
|
|
m_target.setLevelName(child->readString());
|
|
|
|
targetTypeFound = true;
|
|
|
|
} else {
|
2018-03-07 01:17:50 +01:00
|
|
|
diag.emplace_back(
|
|
|
|
DiagLevel::Warning, "Targets element contains multiple TargetType elements. Surplus elements will be ignored.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-08-17 23:20:38 +02:00
|
|
|
case MatroskaIds::TagTrackUID:
|
2015-04-22 19:22:01 +02:00
|
|
|
m_target.tracks().emplace_back(child->readUInteger());
|
|
|
|
break;
|
2017-08-17 23:20:38 +02:00
|
|
|
case MatroskaIds::TagEditionUID:
|
2015-04-22 19:22:01 +02:00
|
|
|
m_target.editions().emplace_back(child->readUInteger());
|
|
|
|
break;
|
2017-08-17 23:20:38 +02:00
|
|
|
case MatroskaIds::TagChapterUID:
|
2015-04-22 19:22:01 +02:00
|
|
|
m_target.chapters().emplace_back(child->readUInteger());
|
|
|
|
break;
|
2017-08-17 23:20:38 +02:00
|
|
|
case MatroskaIds::TagAttachmentUID:
|
2015-04-22 19:22:01 +02:00
|
|
|
m_target.attachments().emplace_back(child->readUInteger());
|
|
|
|
break;
|
|
|
|
default:
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "Targets element contains unknown element. It will be ignored.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!m_target.level()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
m_target.setLevel(50); // default level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \class Media::MatroskaTagMaker
|
|
|
|
* \brief The MatroskaTagMaker class helps writing Matroska "Tag"-elements storing tag information.
|
|
|
|
*
|
|
|
|
* An instance can be obtained using the MatroskaTag::prepareMaking() method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Prepares making the specified \a tag.
|
|
|
|
* \sa See MatroskaTag::prepareMaking() for more information.
|
|
|
|
*/
|
2018-03-07 01:17:50 +01:00
|
|
|
MatroskaTagMaker::MatroskaTagMaker(MatroskaTag &tag, Diagnostics &diag)
|
|
|
|
: m_tag(tag)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
// calculate size of "Targets" element
|
|
|
|
m_targetsSize = 0; // NOT including ID and size
|
2018-03-07 01:17:50 +01:00
|
|
|
if (m_tag.target().level() != 50) {
|
2015-10-13 23:32:00 +02:00
|
|
|
// size of "TargetTypeValue"
|
|
|
|
m_targetsSize += 2 + 1 + EbmlElement::calculateUIntegerLength(m_tag.target().level());
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!m_tag.target().levelName().empty()) {
|
2015-10-13 23:32:00 +02:00
|
|
|
// size of "TargetType"
|
|
|
|
m_targetsSize += 2 + EbmlElement::calculateSizeDenotationLength(m_tag.target().levelName().size()) + m_tag.target().levelName().size();
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
for (const auto &v : initializer_list<vector<uint64>>{
|
|
|
|
m_tag.target().tracks(), m_tag.target().editions(), m_tag.target().chapters(), m_tag.target().attachments() }) {
|
|
|
|
for (auto uid : v) {
|
2015-10-13 23:32:00 +02:00
|
|
|
// size of UID denotation
|
|
|
|
m_targetsSize += 2 + 1 + EbmlElement::calculateUIntegerLength(uid);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_tagSize = 2 + EbmlElement::calculateSizeDenotationLength(m_targetsSize) + m_targetsSize;
|
|
|
|
// calculate size of "SimpleTag" elements
|
2015-12-22 23:54:35 +01:00
|
|
|
m_maker.reserve(m_tag.fields().size());
|
2015-04-22 19:22:01 +02:00
|
|
|
m_simpleTagsSize = 0; // including ID and size
|
2018-03-07 01:17:50 +01:00
|
|
|
for (auto &pair : m_tag.fields()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
try {
|
2018-03-05 17:49:29 +01:00
|
|
|
m_maker.emplace_back(pair.second.prepareMaking(diag));
|
2015-12-22 23:54:35 +01:00
|
|
|
m_simpleTagsSize += m_maker.back().requiredSize();
|
2018-03-07 01:17:50 +01:00
|
|
|
} catch (const Failure &) {
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_tagSize += m_simpleTagsSize;
|
|
|
|
m_totalSize = 2 + EbmlElement::calculateSizeDenotationLength(m_tagSize) + m_tagSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Saves the tag (specified when constructing the object) to the
|
|
|
|
* specified \a stream (makes a "Tag"-element).
|
|
|
|
* \throws Throws std::ios_base::failure when an IO error occurs.
|
|
|
|
* \throws Throws Assumes the data is already validated and thus does NOT
|
|
|
|
* throw Media::Failure or a derived exception.
|
|
|
|
*/
|
|
|
|
void MatroskaTagMaker::make(ostream &stream) const
|
|
|
|
{
|
|
|
|
// write header
|
|
|
|
char buff[11];
|
|
|
|
BE::getBytes(static_cast<uint16>(MatroskaIds::Tag), buff);
|
|
|
|
stream.write(buff, 2); // ID
|
|
|
|
byte len = EbmlElement::makeSizeDenotation(m_tagSize, buff);
|
|
|
|
stream.write(buff, len); // size
|
|
|
|
// write "Targets" element
|
|
|
|
BE::getBytes(static_cast<uint16>(MatroskaIds::Targets), buff);
|
|
|
|
stream.write(buff, 2);
|
|
|
|
len = EbmlElement::makeSizeDenotation(m_targetsSize, buff);
|
|
|
|
stream.write(buff, len);
|
|
|
|
const TagTarget &t = m_tag.target();
|
2018-03-07 01:17:50 +01:00
|
|
|
if (t.level() != 50) {
|
2015-04-22 19:22:01 +02:00
|
|
|
// write "TargetTypeValue"
|
|
|
|
BE::getBytes(static_cast<uint16>(MatroskaIds::TargetTypeValue), buff);
|
|
|
|
stream.write(buff, 2);
|
|
|
|
len = EbmlElement::makeUInteger(t.level(), buff);
|
2018-06-02 22:56:08 +02:00
|
|
|
stream.put(static_cast<char>(0x80 | len));
|
2015-04-22 19:22:01 +02:00
|
|
|
stream.write(buff, len);
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!t.levelName().empty()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
// write "TargetType"
|
|
|
|
BE::getBytes(static_cast<uint16>(MatroskaIds::TargetType), buff);
|
|
|
|
stream.write(buff, 2);
|
|
|
|
len = EbmlElement::makeSizeDenotation(t.levelName().size(), buff);
|
|
|
|
stream.write(buff, len);
|
|
|
|
stream.write(t.levelName().c_str(), t.levelName().size());
|
|
|
|
}
|
|
|
|
// write UIDs
|
2018-03-07 01:17:50 +01:00
|
|
|
typedef pair<uint16, vector<uint64>> p;
|
|
|
|
for (const auto &pair : initializer_list<p>{ p(MatroskaIds::TagTrackUID, t.tracks()), p(MatroskaIds::TagEditionUID, t.editions()),
|
|
|
|
p(MatroskaIds::TagChapterUID, t.chapters()), p(MatroskaIds::TagAttachmentUID, t.attachments()) }) {
|
|
|
|
if (!pair.second.empty()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
BE::getBytes(pair.first, buff);
|
2018-03-07 01:17:50 +01:00
|
|
|
for (auto uid : pair.second) {
|
2015-04-22 19:22:01 +02:00
|
|
|
len = EbmlElement::makeUInteger(uid, buff + 3);
|
2018-06-02 22:56:08 +02:00
|
|
|
*(buff + 2) = static_cast<char>(0x80 | len);
|
2015-04-22 19:22:01 +02:00
|
|
|
stream.write(buff, 3 + len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// write "SimpleTag" elements using maker objects prepared previously
|
2018-03-07 01:17:50 +01:00
|
|
|
for (const auto &maker : m_maker) {
|
2015-04-22 19:22:01 +02:00
|
|
|
maker.make(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:17:50 +01:00
|
|
|
} // namespace TagParser
|