2015-09-06 19:57:33 +02:00
|
|
|
#include "./mp4tagfield.h"
|
|
|
|
#include "./mp4atom.h"
|
2018-03-07 01:17:50 +01:00
|
|
|
#include "./mp4container.h"
|
2015-09-06 19:57:33 +02:00
|
|
|
#include "./mp4ids.h"
|
2015-09-06 15:42:18 +02:00
|
|
|
|
2015-09-06 19:57:33 +02:00
|
|
|
#include "../exceptions.h"
|
2015-04-22 19:22:01 +02:00
|
|
|
|
2017-01-27 18:59:22 +01:00
|
|
|
#include <c++utilities/conversion/stringbuilder.h>
|
2015-04-22 19:22:01 +02:00
|
|
|
#include <c++utilities/io/binaryreader.h>
|
|
|
|
#include <c++utilities/io/binarywriter.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
2018-03-07 01:17:50 +01:00
|
|
|
#include <memory>
|
2015-04-22 19:22:01 +02:00
|
|
|
|
|
|
|
using namespace std;
|
2019-06-10 22:49:11 +02:00
|
|
|
using namespace CppUtilities;
|
2015-04-22 19:22:01 +02:00
|
|
|
|
2018-03-06 23:09:15 +01:00
|
|
|
namespace TagParser {
|
2015-04-22 19:22:01 +02:00
|
|
|
|
|
|
|
/*!
|
2018-06-03 20:38:32 +02:00
|
|
|
* \class TagParser::Mp4TagField
|
2015-04-22 19:22:01 +02:00
|
|
|
* \brief The Mp4TagField class is used by Mp4Tag to store the fields.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a new Mp4TagField.
|
|
|
|
*/
|
2018-03-07 01:17:50 +01:00
|
|
|
Mp4TagField::Mp4TagField()
|
|
|
|
: m_parsedRawDataType(RawDataType::Reserved)
|
|
|
|
, m_countryIndicator(0)
|
|
|
|
, m_langIndicator(0)
|
|
|
|
{
|
|
|
|
}
|
2015-04-22 19:22:01 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a new Mp4TagField with the specified \a id and \a value.
|
|
|
|
*/
|
2018-03-07 01:17:50 +01:00
|
|
|
Mp4TagField::Mp4TagField(IdentifierType id, const TagValue &value)
|
|
|
|
: TagField<Mp4TagField>(id, value)
|
|
|
|
, m_parsedRawDataType(RawDataType::Reserved)
|
|
|
|
, m_countryIndicator(0)
|
|
|
|
, m_langIndicator(0)
|
|
|
|
{
|
|
|
|
}
|
2015-04-22 19:22:01 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a new Mp4TagField with the specified \a mean, \a name and \a value.
|
|
|
|
*
|
|
|
|
* The ID will be set to Mp4TagAtomIds::Extended indicating an tag field using the
|
|
|
|
* reverse DNS style.
|
|
|
|
*
|
|
|
|
* \sa The last paragraph of <a href="http://atomicparsley.sourceforge.net/mpeg-4files.html">Known iTunes Metadata Atoms</a>
|
|
|
|
* gives additional information about this form of MP4 tag fields.
|
|
|
|
*/
|
2021-01-30 21:53:06 +01:00
|
|
|
Mp4TagField::Mp4TagField(std::string_view mean, std::string_view name, const TagValue &value)
|
2018-03-07 01:17:50 +01:00
|
|
|
: Mp4TagField(Mp4TagAtomIds::Extended, value)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
m_mean = mean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Parses field information from the specified Mp4Atom.
|
|
|
|
*
|
|
|
|
* The specified atom should be a child atom of the "ilst" atom.
|
|
|
|
* Each child of the "ilst" atom holds one field of the Mp4Tag.
|
|
|
|
*
|
|
|
|
* \throws Throws std::ios_base::failure when an IO error occurs.
|
2018-06-03 20:38:32 +02:00
|
|
|
* \throws Throws TagParser::Failure or a derived exception when a parsing
|
2015-04-22 19:22:01 +02:00
|
|
|
* error occurs.
|
|
|
|
*/
|
2018-03-05 17:49:29 +01:00
|
|
|
void Mp4TagField::reparse(Mp4Atom &ilstChild, Diagnostics &diag)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
// prepare reparsing
|
|
|
|
using namespace Mp4AtomIds;
|
|
|
|
using namespace Mp4TagAtomIds;
|
|
|
|
string context("parsing MP4 tag field");
|
2018-03-05 17:49:29 +01:00
|
|
|
ilstChild.parse(diag); // ensure child has been parsed
|
2015-04-22 19:22:01 +02:00
|
|
|
setId(ilstChild.id());
|
|
|
|
context = "parsing MP4 tag field " + ilstChild.idToString();
|
|
|
|
iostream &stream = ilstChild.stream();
|
|
|
|
BinaryReader &reader = ilstChild.container().reader();
|
2015-06-12 02:35:50 +02:00
|
|
|
int dataAtomFound = 0, meanAtomFound = 0, nameAtomFound = 0;
|
2018-03-07 01:17:50 +01:00
|
|
|
for (Mp4Atom *dataAtom = ilstChild.firstChild(); dataAtom; dataAtom = dataAtom->nextSibling()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
try {
|
2018-03-05 17:49:29 +01:00
|
|
|
dataAtom->parse(diag);
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->id() == Mp4AtomIds::Data) {
|
|
|
|
if (dataAtom->dataSize() < 8) {
|
2021-01-30 15:13:42 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
|
|
|
"Truncated child atom \"data\" in tag atom (ilst child) found. It will be ignored and discarded when applying changes.",
|
|
|
|
context);
|
2015-06-12 02:35:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
auto *val = &value();
|
|
|
|
auto *rawDataType = &m_parsedRawDataType;
|
|
|
|
auto *countryIndicator = &m_countryIndicator;
|
|
|
|
auto *languageIndicator = &m_langIndicator;
|
2018-03-07 01:17:50 +01:00
|
|
|
if (++dataAtomFound > 1) {
|
|
|
|
if (dataAtomFound == 2) {
|
2021-01-30 15:13:42 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
|
|
|
"Multiple \"data\" child atom in tag atom (ilst child) found. It will be ignored but preserved when applying changes.",
|
|
|
|
context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
auto &additionalData = m_additionalData.emplace_back();
|
|
|
|
val = &additionalData.value;
|
|
|
|
rawDataType = &additionalData.rawDataType;
|
|
|
|
countryIndicator = &additionalData.countryIndicator;
|
|
|
|
languageIndicator = &additionalData.languageIndicator;
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-06-02 22:56:08 +02:00
|
|
|
stream.seekg(static_cast<streamoff>(dataAtom->dataOffset()));
|
2018-03-07 01:17:50 +01:00
|
|
|
if (reader.readByte() != 0) {
|
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
|
|
|
"The version indicator byte is not zero, the tag atom might be unsupported and hence not be parsed correctly.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
setTypeInfo(*rawDataType = reader.readUInt24BE());
|
2015-04-22 19:22:01 +02:00
|
|
|
try { // try to show warning if parsed raw data type differs from expected raw data type for this atom id
|
2019-03-13 19:06:42 +01:00
|
|
|
const vector<std::uint32_t> expectedRawDataTypes = this->expectedRawDataTypes();
|
2018-03-07 01:17:50 +01:00
|
|
|
if (find(expectedRawDataTypes.cbegin(), expectedRawDataTypes.cend(), m_parsedRawDataType) == expectedRawDataTypes.cend()) {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "Unexpected data type indicator found.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
} catch (const Failure &) {
|
2015-04-22 19:22:01 +02:00
|
|
|
// tag id is unknown, it is not possible to validate parsed data type
|
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
*countryIndicator = reader.readUInt16BE(); // FIXME: use locale within the tag value
|
|
|
|
*languageIndicator = reader.readUInt16BE(); // FIXME: use locale within the tag value
|
|
|
|
switch (*rawDataType) {
|
2018-03-07 01:17:50 +01:00
|
|
|
case RawDataType::Utf8:
|
|
|
|
case RawDataType::Utf16:
|
2018-06-02 22:56:08 +02:00
|
|
|
stream.seekg(static_cast<streamoff>(dataAtom->dataOffset() + 8));
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignText(reader.readString(dataAtom->dataSize() - 8),
|
2018-03-07 01:17:50 +01:00
|
|
|
(m_parsedRawDataType == RawDataType::Utf16) ? TagTextEncoding::Utf16BigEndian : TagTextEncoding::Utf8);
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
case RawDataType::Gif:
|
|
|
|
case RawDataType::Jpeg:
|
|
|
|
case RawDataType::Png:
|
|
|
|
case RawDataType::Bmp: {
|
|
|
|
switch (m_parsedRawDataType) {
|
2015-04-22 19:22:01 +02:00
|
|
|
case RawDataType::Gif:
|
2021-01-29 23:21:38 +01:00
|
|
|
val->setMimeType("image/gif");
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
|
|
|
case RawDataType::Jpeg:
|
2021-01-29 23:21:38 +01:00
|
|
|
val->setMimeType("image/jpeg");
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
|
|
|
case RawDataType::Png:
|
2021-01-29 23:21:38 +01:00
|
|
|
val->setMimeType("image/png");
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
|
|
|
case RawDataType::Bmp:
|
2021-01-29 23:21:38 +01:00
|
|
|
val->setMimeType("image/bmp");
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
default:;
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-06-02 22:56:08 +02:00
|
|
|
const auto coverSize = static_cast<streamoff>(dataAtom->dataSize() - 8);
|
|
|
|
auto coverData = make_unique<char[]>(static_cast<size_t>(coverSize));
|
2015-04-22 19:22:01 +02:00
|
|
|
stream.read(coverData.get(), coverSize);
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignData(move(coverData), static_cast<size_t>(coverSize), TagDataType::Picture);
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
}
|
|
|
|
case RawDataType::BeSignedInt: {
|
2015-04-22 19:22:01 +02:00
|
|
|
int number = 0;
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->dataSize() > (8 + 4)) {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "Data atom stores integer of invalid size. Trying to read data anyways.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->dataSize() >= (8 + 4)) {
|
2015-04-22 19:22:01 +02:00
|
|
|
number = reader.readInt32BE();
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (dataAtom->dataSize() == (8 + 2)) {
|
2015-04-22 19:22:01 +02:00
|
|
|
number = reader.readInt16BE();
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (dataAtom->dataSize() == (8 + 1)) {
|
2015-04-22 19:22:01 +02:00
|
|
|
number = reader.readChar();
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (ilstChild.id()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
case PreDefinedGenre: // consider number as standard genre index
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignStandardGenreIndex(number);
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignInteger(number);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
}
|
|
|
|
case RawDataType::BeUnsignedInt: {
|
2015-04-22 19:22:01 +02:00
|
|
|
int number = 0;
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->dataSize() > (8 + 4)) {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "Data atom stores integer of invalid size. Trying to read data anyways.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->dataSize() >= (8 + 4)) {
|
2015-04-22 19:22:01 +02:00
|
|
|
number = static_cast<int>(reader.readUInt32BE());
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (dataAtom->dataSize() == (8 + 2)) {
|
2015-04-22 19:22:01 +02:00
|
|
|
number = static_cast<int>(reader.readUInt16BE());
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (dataAtom->dataSize() == (8 + 1)) {
|
2015-04-22 19:22:01 +02:00
|
|
|
number = static_cast<int>(reader.readByte());
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (ilstChild.id()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
case PreDefinedGenre: // consider number as standard genre index
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignStandardGenreIndex(number - 1);
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignInteger(number);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
switch (ilstChild.id()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
// track number, disk number and genre have no specific data type id
|
|
|
|
case TrackPosition:
|
|
|
|
case DiskPosition: {
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->dataSize() < (8 + 6)) {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "Track/disk position is truncated. Trying to read data anyways.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2019-03-13 19:06:42 +01:00
|
|
|
std::uint16_t pos = 0, total = 0;
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->dataSize() >= (8 + 4)) {
|
2015-04-22 19:22:01 +02:00
|
|
|
stream.seekg(2, ios_base::cur);
|
|
|
|
pos = reader.readUInt16BE();
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->dataSize() >= (8 + 6)) {
|
2015-04-22 19:22:01 +02:00
|
|
|
total = reader.readUInt16BE();
|
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignPosition(PositionInSet(pos, total));
|
2015-04-22 19:22:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PreDefinedGenre:
|
2018-03-07 01:17:50 +01:00
|
|
|
if (dataAtom->dataSize() < (8 + 2)) {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "Genre index is truncated.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
} else {
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignStandardGenreIndex(reader.readUInt16BE() - 1);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // no supported data type, read raw data
|
2018-06-02 22:56:08 +02:00
|
|
|
const auto dataSize = static_cast<streamsize>(dataAtom->dataSize() - 8);
|
|
|
|
auto data = make_unique<char[]>(static_cast<size_t>(dataSize));
|
2015-04-22 19:22:01 +02:00
|
|
|
stream.read(data.get(), dataSize);
|
2018-03-07 01:17:50 +01:00
|
|
|
if (ilstChild.id() == Mp4TagAtomIds::Cover) {
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignData(move(data), static_cast<size_t>(dataSize), TagDataType::Picture);
|
2015-04-22 19:22:01 +02:00
|
|
|
} else {
|
2021-01-29 23:21:38 +01:00
|
|
|
val->assignData(move(data), static_cast<size_t>(dataSize), TagDataType::Undefined);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (dataAtom->id() == Mp4AtomIds::Mean) {
|
|
|
|
if (dataAtom->dataSize() < 8) {
|
2021-01-30 15:13:42 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
|
|
|
"Truncated child atom \"mean\" in tag atom (ilst child) found. It will be ignored and discarded when applying changes.",
|
|
|
|
context);
|
2015-06-12 02:35:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (++meanAtomFound > 1) {
|
|
|
|
if (meanAtomFound == 2) {
|
2021-01-30 15:13:42 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
|
|
|
"Tag atom contains more than one mean atom. The additional mean atoms will be ignored and discarded when applying "
|
|
|
|
"changes.",
|
|
|
|
context);
|
2015-06-12 02:35:50 +02:00
|
|
|
}
|
2015-04-22 19:22:01 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-06-02 22:56:08 +02:00
|
|
|
stream.seekg(static_cast<streamoff>(dataAtom->dataOffset() + 4));
|
2015-06-12 02:35:50 +02:00
|
|
|
m_mean = reader.readString(dataAtom->dataSize() - 4);
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (dataAtom->id() == Mp4AtomIds::Name) {
|
|
|
|
if (dataAtom->dataSize() < 4) {
|
2021-01-30 15:13:42 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
|
|
|
"Truncated child atom \"name\" in tag atom (ilst child) found. It will be ignored and discarded when applying changes.",
|
|
|
|
context);
|
2015-06-12 02:35:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (++nameAtomFound > 1) {
|
|
|
|
if (nameAtomFound == 2) {
|
2021-01-30 15:13:42 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
|
|
|
"Tag atom contains more than one name atom. The addiational name atoms will be ignored and discarded when applying "
|
|
|
|
"changes.",
|
|
|
|
context);
|
2015-06-12 02:35:50 +02:00
|
|
|
}
|
2015-04-22 19:22:01 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-06-02 22:56:08 +02:00
|
|
|
stream.seekg(static_cast<streamoff>(dataAtom->dataOffset() + 4));
|
2015-06-12 02:35:50 +02:00
|
|
|
m_name = reader.readString(dataAtom->dataSize() - 4);
|
2015-04-22 19:22:01 +02:00
|
|
|
} else {
|
2018-03-07 01:17:50 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
2021-01-30 15:13:42 +01:00
|
|
|
"Unkown child atom \"" % dataAtom->idToString()
|
|
|
|
+ "\" in tag atom (ilst child) found. It will be ignored and discarded when applying changes.",
|
|
|
|
context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
} catch (const Failure &) {
|
2021-01-30 15:13:42 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning,
|
|
|
|
"Unable to parse all children atom in tag atom (ilst child) found. Invalid children will be ignored and discarded when applying "
|
|
|
|
"changes.",
|
|
|
|
context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (value().isEmpty()) {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "The field value is empty.", context);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 13:50:46 +01:00
|
|
|
/*!
|
|
|
|
* \brief Prepares making.
|
|
|
|
* \returns Returns a Mp4TagFieldMaker object which can be used to actually make the field.
|
|
|
|
* \remarks The field must NOT be mutated after making is prepared when it is intended to actually
|
|
|
|
* make the field using the make method of the returned object.
|
2018-06-03 20:38:32 +02:00
|
|
|
* \throws Throws TagParser::Failure or a derived exception when a making
|
2015-12-10 13:50:46 +01:00
|
|
|
* error occurs.
|
|
|
|
*
|
|
|
|
* This method might be useful when it is necessary to know the size of the field before making it.
|
|
|
|
*/
|
2018-03-05 17:49:29 +01:00
|
|
|
Mp4TagFieldMaker Mp4TagField::prepareMaking(Diagnostics &diag)
|
2015-12-10 13:50:46 +01:00
|
|
|
{
|
2018-03-05 17:49:29 +01:00
|
|
|
return Mp4TagFieldMaker(*this, diag);
|
2015-12-10 13:50:46 +01:00
|
|
|
}
|
|
|
|
|
2015-04-22 19:22:01 +02:00
|
|
|
/*!
|
|
|
|
* \brief Saves the field to the specified \a stream.
|
|
|
|
*
|
|
|
|
* \throws Throws std::ios_base::failure when an IO error occurs.
|
2018-06-03 20:38:32 +02:00
|
|
|
* \throws Throws TagParser::Failure or a derived exception when a making
|
2015-04-22 19:22:01 +02:00
|
|
|
* error occurs.
|
|
|
|
*/
|
2018-03-05 17:49:29 +01:00
|
|
|
void Mp4TagField::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 Returns the expected raw data types for the ID of the field.
|
|
|
|
*/
|
2019-03-13 19:06:42 +01:00
|
|
|
std::vector<std::uint32_t> Mp4TagField::expectedRawDataTypes() const
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
using namespace Mp4TagAtomIds;
|
2019-03-13 19:06:42 +01:00
|
|
|
std::vector<std::uint32_t> res;
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (id()) {
|
|
|
|
case Album:
|
|
|
|
case Artist:
|
|
|
|
case Comment:
|
|
|
|
case Year:
|
|
|
|
case Title:
|
|
|
|
case Genre:
|
|
|
|
case Composer:
|
|
|
|
case Encoder:
|
|
|
|
case Grouping:
|
|
|
|
case Description:
|
|
|
|
case Lyrics:
|
|
|
|
case RecordLabel:
|
|
|
|
case Performers:
|
|
|
|
case Lyricist:
|
2015-04-22 19:22:01 +02:00
|
|
|
res.push_back(RawDataType::Utf8);
|
|
|
|
res.push_back(RawDataType::Utf16);
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
case PreDefinedGenre:
|
|
|
|
case TrackPosition:
|
|
|
|
case DiskPosition:
|
2015-04-22 19:22:01 +02:00
|
|
|
res.push_back(RawDataType::Reserved);
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
case Bpm:
|
|
|
|
case Rating:
|
2015-04-22 19:22:01 +02:00
|
|
|
res.push_back(RawDataType::BeSignedInt);
|
|
|
|
res.push_back(RawDataType::BeUnsignedInt);
|
|
|
|
break;
|
|
|
|
case Cover:
|
|
|
|
res.push_back(RawDataType::Gif);
|
|
|
|
res.push_back(RawDataType::Jpeg);
|
|
|
|
res.push_back(RawDataType::Png);
|
|
|
|
res.push_back(RawDataType::Bmp);
|
|
|
|
break;
|
|
|
|
case Extended:
|
2018-03-07 01:17:50 +01:00
|
|
|
if (mean() != Mp4TagExtendedMeanIds::iTunes) {
|
2016-11-14 22:59:19 +01:00
|
|
|
throw Failure();
|
|
|
|
}
|
2017-09-14 19:33:49 +02:00
|
|
|
// assumption that extended "iTunes" tags always use Unicode correct?
|
|
|
|
res.push_back(RawDataType::Utf8);
|
|
|
|
res.push_back(RawDataType::Utf16);
|
|
|
|
break;
|
2015-04-22 19:22:01 +02:00
|
|
|
default:
|
|
|
|
throw Failure();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns an appropriate raw data type.
|
2021-01-29 23:21:38 +01:00
|
|
|
* \return
|
|
|
|
* Returns the type info if assigned; otherwise returns a raw data type considered as appropriate for the
|
|
|
|
* ID of the field and its value.
|
|
|
|
* \sa See Mp4TagField::appropriateRawDataTypeForValue() for the behavior if no type info is assigned.
|
2015-04-22 19:22:01 +02:00
|
|
|
*/
|
2019-03-13 19:06:42 +01:00
|
|
|
std::uint32_t Mp4TagField::appropriateRawDataType() const
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
2018-03-07 01:17:50 +01:00
|
|
|
if (isTypeInfoAssigned()) {
|
2015-04-22 19:22:01 +02:00
|
|
|
// obtain raw data type from tag field if present
|
|
|
|
return typeInfo();
|
2017-09-14 19:33:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 18:03:34 +02:00
|
|
|
// there is no raw data type assigned (tag field was not present in original file and
|
|
|
|
// has been inserted by the library's user without type)
|
|
|
|
// -> try to derive appropriate raw data type from atom ID
|
2021-01-29 23:21:38 +01:00
|
|
|
return appropriateRawDataTypeForValue(value());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns an appropriate raw data type.
|
|
|
|
* \returns
|
|
|
|
* Returns a raw data type considered as appropriate for the ID of the field and the specified \a value.
|
|
|
|
* \throws
|
|
|
|
* Throws TagParser::Failure if an appropriate raw data type can not be determined. It is possible to determine
|
|
|
|
* the raw data type for all supported tag field IDs (those where a conversion to KnownField via Mp4Tag exists).
|
|
|
|
*/
|
|
|
|
std::uint32_t Mp4TagField::appropriateRawDataTypeForValue(const TagValue &value) const
|
|
|
|
{
|
|
|
|
using namespace Mp4TagAtomIds;
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (id()) {
|
|
|
|
case Album:
|
|
|
|
case Artist:
|
|
|
|
case Comment:
|
|
|
|
case Year:
|
|
|
|
case Title:
|
|
|
|
case Genre:
|
|
|
|
case Composer:
|
|
|
|
case Encoder:
|
|
|
|
case Grouping:
|
|
|
|
case Description:
|
|
|
|
case Lyrics:
|
|
|
|
case RecordLabel:
|
|
|
|
case Performers:
|
|
|
|
case Lyricist:
|
2019-10-09 18:03:34 +02:00
|
|
|
case AlbumArtist:
|
2021-01-29 23:21:38 +01:00
|
|
|
switch (value.dataEncoding()) {
|
2018-03-07 01:17:50 +01:00
|
|
|
case TagTextEncoding::Utf8:
|
|
|
|
return RawDataType::Utf8;
|
|
|
|
case TagTextEncoding::Utf16BigEndian:
|
|
|
|
return RawDataType::Utf16;
|
|
|
|
default:;
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2017-09-14 19:33:49 +02:00
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
case TrackPosition:
|
|
|
|
case DiskPosition:
|
2017-09-14 19:33:49 +02:00
|
|
|
return RawDataType::Reserved;
|
2018-03-07 01:17:50 +01:00
|
|
|
case PreDefinedGenre:
|
|
|
|
case Bpm:
|
|
|
|
case Rating:
|
2017-09-14 19:33:49 +02:00
|
|
|
return RawDataType::BeSignedInt;
|
|
|
|
case Cover: {
|
2021-01-29 23:21:38 +01:00
|
|
|
const string &mimeType = value.mimeType();
|
2018-03-07 01:17:50 +01:00
|
|
|
if (mimeType == "image/jpg" || mimeType == "image/jpeg") { // "well-known" type
|
2017-09-14 19:33:49 +02:00
|
|
|
return RawDataType::Jpeg;
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (mimeType == "image/png") {
|
2017-09-14 19:33:49 +02:00
|
|
|
return RawDataType::Png;
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (mimeType == "image/bmp") {
|
2017-09-14 19:33:49 +02:00
|
|
|
return RawDataType::Bmp;
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
} break;
|
2017-09-14 19:33:49 +02:00
|
|
|
case Extended:
|
2018-03-07 01:17:50 +01:00
|
|
|
if (mean() != Mp4TagExtendedMeanIds::iTunes) {
|
2015-04-22 19:22:01 +02:00
|
|
|
throw Failure();
|
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
switch (value.dataEncoding()) {
|
2018-03-07 01:17:50 +01:00
|
|
|
case TagTextEncoding::Utf8:
|
|
|
|
return RawDataType::Utf8;
|
|
|
|
case TagTextEncoding::Utf16BigEndian:
|
|
|
|
return RawDataType::Utf16;
|
|
|
|
default:;
|
2017-09-14 19:33:49 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
default:;
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2019-10-09 18:03:34 +02:00
|
|
|
|
|
|
|
// do not forget to extend Mp4Tag::internallyGetFieldId() and Mp4Tag::internallyGetKnownField() as well
|
|
|
|
|
2017-09-14 19:33:49 +02:00
|
|
|
throw Failure();
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2021-01-30 18:25:46 +01:00
|
|
|
* \brief Clears MP4-specific values. Called via clear() and clearValue().
|
2015-04-22 19:22:01 +02:00
|
|
|
*/
|
2021-01-30 18:25:46 +01:00
|
|
|
void Mp4TagField::internallyClearValue()
|
|
|
|
{
|
|
|
|
value().clearDataAndMetadata();
|
|
|
|
m_additionalData.clear();
|
|
|
|
m_countryIndicator = 0;
|
|
|
|
m_langIndicator = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Clears MP4-specific values. Called via clear() and clearValue().
|
|
|
|
*/
|
|
|
|
void Mp4TagField::internallyClearFurtherData()
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
|
|
|
m_name.clear();
|
|
|
|
m_mean.clear();
|
|
|
|
m_parsedRawDataType = RawDataType::Reserved;
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:21:38 +01:00
|
|
|
/// \cond
|
|
|
|
Mp4TagFieldMaker::Data::Data()
|
|
|
|
: convertedData(stringstream::in | stringstream::out | stringstream::binary)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/// \endcond
|
|
|
|
|
2015-12-10 13:50:46 +01:00
|
|
|
/*!
|
2018-06-03 20:38:32 +02:00
|
|
|
* \class TagParser::Mp4TagFieldMaker
|
2015-12-10 13:50:46 +01:00
|
|
|
* \brief The Mp4TagFieldMaker class helps making tag fields.
|
|
|
|
* It allows to calculate the required size.
|
|
|
|
* \sa See Mp4TagFieldMaker::prepareMaking() for more information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Prepares making the specified \a field.
|
2015-12-22 23:54:35 +01:00
|
|
|
* \sa See Mp4TagField::prepareMaking() for more information.
|
2015-12-10 13:50:46 +01:00
|
|
|
*/
|
2018-03-07 01:17:50 +01:00
|
|
|
Mp4TagFieldMaker::Mp4TagFieldMaker(Mp4TagField &field, Diagnostics &diag)
|
|
|
|
: m_field(field)
|
2021-01-29 23:21:38 +01:00
|
|
|
, m_writer(nullptr)
|
|
|
|
, m_totalSize(0)
|
2015-12-10 13:50:46 +01:00
|
|
|
{
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!m_field.id()) {
|
2021-01-29 23:21:38 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "Invalid tag atom ID.", "making MP4 tag field");
|
2015-12-10 13:50:46 +01:00
|
|
|
throw InvalidDataException();
|
|
|
|
}
|
2017-01-23 00:25:53 +01:00
|
|
|
const string context("making MP4 tag field " + Mp4TagField::fieldIdToString(m_field.id()));
|
2018-03-07 01:17:50 +01:00
|
|
|
if (m_field.value().isEmpty() && (!m_field.mean().empty() || !m_field.name().empty())) {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Critical, "No tag value assigned.", context);
|
2015-12-10 13:50:46 +01:00
|
|
|
throw InvalidDataException();
|
|
|
|
}
|
2015-12-22 23:54:35 +01:00
|
|
|
|
2021-01-29 23:21:38 +01:00
|
|
|
// calculate size for name and mean
|
|
|
|
m_totalSize = 8 + (m_field.name().empty() ? 0 : (12 + m_field.name().size())) + (m_field.mean().empty() ? 0 : (12 + m_field.mean().size()));
|
|
|
|
|
|
|
|
// prepare making data atom and calculate the expected size
|
|
|
|
m_totalSize += prepareDataAtom(field.value(), field.countryIndicator(), field.languageIndicator(), context, diag);
|
|
|
|
for (const auto &additionalData : m_field.additionalData()) {
|
|
|
|
m_totalSize += prepareDataAtom(additionalData.value, additionalData.countryIndicator, additionalData.languageIndicator, context, diag);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_totalSize > numeric_limits<std::uint32_t>::max()) {
|
|
|
|
diag.emplace_back(DiagLevel::Critical, "Making a such big MP4 tag field is not possible.", context);
|
|
|
|
throw NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Prepares making a data atom for the specified \a value.
|
|
|
|
*/
|
|
|
|
std::uint64_t Mp4TagFieldMaker::prepareDataAtom(
|
|
|
|
const TagValue &value, std::uint16_t countryIndicator, std::uint16_t languageIndicator, const std::string &context, Diagnostics &diag)
|
|
|
|
{
|
|
|
|
// add new data entry
|
|
|
|
auto &data = m_data.emplace_back();
|
|
|
|
m_writer.setStream(&data.convertedData);
|
|
|
|
|
|
|
|
// assign local info
|
|
|
|
// FIXME: use locale within the tag value instead of just passing through current values
|
|
|
|
data.countryIndicator = countryIndicator;
|
|
|
|
data.languageIndicator = languageIndicator;
|
|
|
|
|
2015-12-10 13:50:46 +01:00
|
|
|
try {
|
|
|
|
// try to use appropriate raw data type
|
2021-01-29 23:21:38 +01:00
|
|
|
data.rawType = m_field.isTypeInfoAssigned() ? m_field.typeInfo() : m_field.appropriateRawDataTypeForValue(value);
|
2018-03-07 01:17:50 +01:00
|
|
|
} catch (const Failure &) {
|
2015-12-10 13:50:46 +01:00
|
|
|
// unable to obtain appropriate raw data type
|
2018-03-07 01:17:50 +01:00
|
|
|
if (m_field.id() == Mp4TagAtomIds::Cover) {
|
2016-05-14 00:24:01 +02:00
|
|
|
// assume JPEG image
|
2021-01-29 23:21:38 +01:00
|
|
|
data.rawType = RawDataType::Jpeg;
|
2018-03-07 01:17:50 +01:00
|
|
|
diag.emplace_back(
|
|
|
|
DiagLevel::Warning, "It was not possible to find an appropriate raw data type id. JPEG image will be assumed.", context);
|
2016-05-14 00:24:01 +02:00
|
|
|
} else {
|
|
|
|
// assume UTF-8 text
|
2021-01-29 23:21:38 +01:00
|
|
|
data.rawType = RawDataType::Utf8;
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Warning, "It was not possible to find an appropriate raw data type id. UTF-8 will be assumed.", context);
|
2016-05-14 00:24:01 +02:00
|
|
|
}
|
2015-12-10 13:50:46 +01:00
|
|
|
}
|
2015-12-22 23:54:35 +01:00
|
|
|
|
2015-12-10 13:50:46 +01:00
|
|
|
try {
|
2021-01-29 23:21:38 +01:00
|
|
|
if (!value.isEmpty()) { // there might be only mean and name info, but no data
|
|
|
|
data.convertedData.exceptions(std::stringstream::failbit | std::stringstream::badbit);
|
|
|
|
switch (data.rawType) {
|
2015-12-10 13:50:46 +01:00
|
|
|
case RawDataType::Utf8:
|
2021-01-30 15:00:10 +01:00
|
|
|
if (value.type() != TagDataType::Text || value.dataEncoding() != TagTextEncoding::Utf8) {
|
|
|
|
m_writer.writeString(value.toString(TagTextEncoding::Utf8));
|
|
|
|
}
|
|
|
|
break;
|
2015-12-10 13:50:46 +01:00
|
|
|
case RawDataType::Utf16:
|
2021-01-30 15:00:10 +01:00
|
|
|
if (value.type() != TagDataType::Text || value.dataEncoding() != TagTextEncoding::Utf16LittleEndian) {
|
|
|
|
m_writer.writeString(value.toString(TagTextEncoding::Utf16LittleEndian));
|
|
|
|
}
|
2015-12-10 13:50:46 +01:00
|
|
|
break;
|
|
|
|
case RawDataType::BeSignedInt: {
|
2021-01-29 23:21:38 +01:00
|
|
|
int number = value.toInteger();
|
2019-03-13 19:06:42 +01:00
|
|
|
if (number <= numeric_limits<std::int16_t>::max() && number >= numeric_limits<std::int16_t>::min()) {
|
|
|
|
m_writer.writeInt16BE(static_cast<std::int16_t>(number));
|
2015-12-10 13:50:46 +01:00
|
|
|
} else {
|
|
|
|
m_writer.writeInt32BE(number);
|
|
|
|
}
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
}
|
|
|
|
case RawDataType::BeUnsignedInt: {
|
2021-01-29 23:21:38 +01:00
|
|
|
int number = value.toInteger();
|
2019-03-13 19:06:42 +01:00
|
|
|
if (number <= numeric_limits<std::uint16_t>::max() && number >= numeric_limits<std::uint16_t>::min()) {
|
|
|
|
m_writer.writeUInt16BE(static_cast<std::uint16_t>(number));
|
2018-03-07 01:17:50 +01:00
|
|
|
} else if (number > 0) {
|
2019-03-13 19:06:42 +01:00
|
|
|
m_writer.writeUInt32BE(static_cast<std::uint32_t>(number));
|
2015-12-10 13:50:46 +01:00
|
|
|
} else {
|
2018-03-07 01:17:50 +01:00
|
|
|
throw ConversionException(
|
2019-03-13 19:06:42 +01:00
|
|
|
"Negative integer can not be assigned to the field with the ID \"" % interpretIntegerAsString<std::uint32_t>(m_field.id())
|
|
|
|
+ "\".");
|
2015-12-10 13:50:46 +01:00
|
|
|
}
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
}
|
|
|
|
case RawDataType::Bmp:
|
|
|
|
case RawDataType::Jpeg:
|
|
|
|
case RawDataType::Png:
|
2015-12-10 13:50:46 +01:00
|
|
|
break; // leave converted data empty to write original data later
|
|
|
|
default:
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (m_field.id()) {
|
2015-12-10 13:50:46 +01:00
|
|
|
// track number and disk number are exceptions
|
|
|
|
// raw data type 0 is used, information is stored as pair of unsigned integers
|
2018-03-07 01:17:50 +01:00
|
|
|
case Mp4TagAtomIds::TrackPosition:
|
|
|
|
case Mp4TagAtomIds::DiskPosition: {
|
2021-01-29 23:21:38 +01:00
|
|
|
PositionInSet pos = value.toPositionInSet();
|
2015-12-10 13:50:46 +01:00
|
|
|
m_writer.writeInt32BE(pos.position());
|
2019-03-13 19:06:42 +01:00
|
|
|
if (pos.total() <= numeric_limits<std::int16_t>::max()) {
|
|
|
|
m_writer.writeInt16BE(static_cast<std::int16_t>(pos.total()));
|
2015-12-10 13:50:46 +01:00
|
|
|
} else {
|
2018-03-07 01:17:50 +01:00
|
|
|
throw ConversionException(
|
2019-03-13 19:06:42 +01:00
|
|
|
"Integer can not be assigned to the field with the id \"" % interpretIntegerAsString<std::uint32_t>(m_field.id())
|
2018-03-07 01:17:50 +01:00
|
|
|
+ "\" because it is to big.");
|
2015-12-10 13:50:46 +01:00
|
|
|
}
|
|
|
|
m_writer.writeUInt16BE(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Mp4TagAtomIds::PreDefinedGenre:
|
2021-01-29 23:21:38 +01:00
|
|
|
m_writer.writeUInt16BE(static_cast<std::uint16_t>(value.toStandardGenreIndex()));
|
2015-12-10 13:50:46 +01:00
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
default:; // leave converted data empty to write original data later
|
2015-12-10 13:50:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
} catch (const ConversionException &e) {
|
2015-12-10 13:50:46 +01:00
|
|
|
// it was not possible to perform required conversions
|
2021-01-29 23:21:38 +01:00
|
|
|
if (char_traits<char>::length(e.what())) {
|
|
|
|
diag.emplace_back(DiagLevel::Critical, e.what(), context);
|
2015-12-10 13:50:46 +01:00
|
|
|
} else {
|
2018-03-05 17:49:29 +01:00
|
|
|
diag.emplace_back(DiagLevel::Critical, "The assigned tag value can not be converted to be written appropriately.", context);
|
2015-12-10 13:50:46 +01:00
|
|
|
}
|
|
|
|
throw InvalidDataException();
|
|
|
|
}
|
2015-12-22 23:54:35 +01:00
|
|
|
|
2021-01-29 23:21:38 +01:00
|
|
|
// calculate data size; assign raw data
|
|
|
|
if (value.isEmpty()) {
|
|
|
|
return data.size = 0;
|
|
|
|
} else if (data.convertedData.tellp()) {
|
|
|
|
data.size = static_cast<std::size_t>(data.convertedData.tellp());
|
|
|
|
} else {
|
|
|
|
data.rawData = std::string_view(value.dataPointer(), data.size = value.dataSize());
|
2018-06-02 22:56:08 +02:00
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
return data.size += 16;
|
2015-12-10 13:50:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Saves the field (specified when constructing the object) to the
|
2015-12-22 23:54:35 +01:00
|
|
|
* specified \a stream.
|
2015-12-10 13:50:46 +01:00
|
|
|
* \throws Throws std::ios_base::failure when an IO error occurs.
|
|
|
|
* \throws Throws Assumes the data is already validated and thus does NOT
|
2018-06-03 20:38:32 +02:00
|
|
|
* throw TagParser::Failure or a derived exception.
|
2015-12-10 13:50:46 +01:00
|
|
|
*/
|
|
|
|
void Mp4TagFieldMaker::make(ostream &stream)
|
|
|
|
{
|
|
|
|
m_writer.setStream(&stream);
|
|
|
|
// size of entire tag atom
|
2019-03-13 19:06:42 +01:00
|
|
|
m_writer.writeUInt32BE(static_cast<std::uint32_t>(m_totalSize));
|
2015-12-10 13:50:46 +01:00
|
|
|
// id of tag atom
|
|
|
|
m_writer.writeUInt32BE(m_field.id());
|
2021-01-29 23:21:38 +01:00
|
|
|
// write "mean" atom
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!m_field.mean().empty()) {
|
2019-03-13 19:06:42 +01:00
|
|
|
m_writer.writeUInt32BE(static_cast<std::uint32_t>(12 + m_field.mean().size()));
|
2015-12-10 13:50:46 +01:00
|
|
|
m_writer.writeUInt32BE(Mp4AtomIds::Mean);
|
|
|
|
m_writer.writeUInt32BE(0);
|
|
|
|
m_writer.writeString(m_field.mean());
|
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
// write "name" atom
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!m_field.name().empty()) {
|
2019-03-13 19:06:42 +01:00
|
|
|
m_writer.writeUInt32BE(static_cast<std::uint32_t>(12 + m_field.name().length()));
|
2015-12-10 13:50:46 +01:00
|
|
|
m_writer.writeUInt32BE(Mp4AtomIds::Name);
|
|
|
|
m_writer.writeUInt32BE(0);
|
|
|
|
m_writer.writeString(m_field.name());
|
|
|
|
}
|
2021-01-29 23:21:38 +01:00
|
|
|
// write "data" atoms
|
|
|
|
for (auto &data : m_data) {
|
|
|
|
if (!data.size) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
m_writer.writeUInt32BE(static_cast<std::uint32_t>(data.size)); // size of data atom
|
2015-12-10 13:50:46 +01:00
|
|
|
m_writer.writeUInt32BE(Mp4AtomIds::Data); // id of data atom
|
|
|
|
m_writer.writeByte(0); // version
|
2021-01-29 23:21:38 +01:00
|
|
|
m_writer.writeUInt24BE(data.rawType);
|
|
|
|
m_writer.writeUInt16BE(data.countryIndicator);
|
|
|
|
m_writer.writeUInt16BE(data.languageIndicator);
|
|
|
|
if (data.convertedData.tellp()) {
|
2015-12-10 13:50:46 +01:00
|
|
|
// write converted data
|
2021-01-29 23:21:38 +01:00
|
|
|
stream << data.convertedData.rdbuf();
|
2015-12-10 13:50:46 +01:00
|
|
|
} else {
|
|
|
|
// no conversion was needed, write data directly from tag value
|
2021-01-29 23:21:38 +01:00
|
|
|
stream.write(data.rawData.data(), static_cast<std::streamoff>(data.rawData.size()));
|
2015-12-10 13:50:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:17:50 +01:00
|
|
|
} // namespace TagParser
|