Tag Parser  10.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
mp4tagfield.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_MP4TAGATOM_H
2 #define TAG_PARSER_MP4TAGATOM_H
3 
4 #include "../generictagfield.h"
5 
6 #include <c++utilities/conversion/stringconversion.h>
7 #include <c++utilities/io/binarywriter.h>
8 
9 #include <cstdint>
10 #include <sstream>
11 #include <vector>
12 
13 namespace TagParser {
14 
18 namespace RawDataType {
19 enum KnownValue : std::uint32_t {
20  Reserved = 0,
21  Utf8 = 1,
22  Utf16 = 2,
23  Sjis = 3,
24  Utf8Sort = 4,
25  Utf16Sort = 5,
26  Html = 6,
27  Xml = 7,
28  Uuid = 8,
29  Isrc = 9,
30  Mi3p = 10,
31  Gif = 12,
32  Jpeg = 13,
33  Png = 14,
34  Url = 15,
35  Duration = 16,
36  DateTime = 17,
37  Genred = 18,
38  BeSignedInt = 21,
40  BeFloat32 = 23,
41  BeFloat64 = 24,
42  Upc = 25,
43  Bmp = 27,
45  Undefined = 255
46 };
47 }
48 
49 class Mp4TagField;
50 class Diagnostics;
51 
56 public:
57  using IdentifierType = std::uint32_t;
58  using TypeInfoType = std::uint32_t;
59 };
60 
61 class Mp4Atom;
62 
64  friend class Mp4TagField;
65 
66 public:
68  void make(std::ostream &stream);
69  const Mp4TagField &field() const;
70  std::uint64_t requiredSize() const;
71 
72 private:
74  struct Data {
75  Data();
76  Data(Data &&) = default;
77  std::string_view rawData;
78  std::stringstream convertedData;
79  std::uint64_t size = 0;
80  std::uint32_t rawType = 0;
81  std::uint16_t countryIndicator = 0;
82  std::uint16_t languageIndicator = 0;
83  };
85 
87  std::uint64_t prepareDataAtom(
88  const TagValue &value, std::uint16_t countryIndicator, std::uint16_t languageIndicator, const std::string &context, Diagnostics &diag);
89 
90  Mp4TagField &m_field;
91  CppUtilities::BinaryWriter m_writer;
92  std::vector<Data> m_data;
93  std::uint64_t m_totalSize;
94 };
95 
99 inline const Mp4TagField &Mp4TagFieldMaker::field() const
100 {
101  return m_field;
102 }
103 
107 inline std::uint64_t Mp4TagFieldMaker::requiredSize() const
108 {
109  return m_totalSize;
110 }
111 
112 class TAG_PARSER_EXPORT Mp4TagField : public TagField<Mp4TagField> {
113  friend class TagField<Mp4TagField>;
114 
115 public:
116  struct AdditionalData {
118  std::uint32_t rawDataType = 0;
119  std::uint16_t countryIndicator = 0;
120  std::uint16_t languageIndicator = 0;
121  };
122 
123  Mp4TagField();
124  Mp4TagField(IdentifierType id, const TagValue &value);
125  Mp4TagField(std::string_view mean, std::string_view name, const TagValue &value);
126 
127  void reparse(Mp4Atom &ilstChild, Diagnostics &diag);
128  Mp4TagFieldMaker prepareMaking(Diagnostics &diag);
129  void make(std::ostream &stream, Diagnostics &diag);
130 
131  const std::vector<AdditionalData> &additionalData() const;
132  std::vector<AdditionalData> &additionalData();
133  bool isAdditionalTypeInfoUsed() const;
134  const std::string &name() const;
135  void setName(const std::string &name);
136  const std::string &mean() const;
137  void setMean(const std::string &mean);
138  std::uint32_t parsedRawDataType() const;
139  std::uint16_t countryIndicator() const;
140  std::uint16_t languageIndicator() const;
141  bool supportsNestedFields() const;
142  std::vector<std::uint32_t> expectedRawDataTypes() const;
143  std::uint32_t appropriateRawDataType() const;
144  std::uint32_t appropriateRawDataTypeForValue(const TagValue &value) const;
145 
146  static IdentifierType fieldIdFromString(std::string_view idString);
147  static std::string fieldIdToString(IdentifierType id);
148 
149 private:
150  void internallyClearValue();
151  void internallyClearFurtherData();
152  std::string m_name;
153  std::string m_mean;
154  std::vector<AdditionalData> m_additionalData;
155  std::uint32_t m_parsedRawDataType;
156  std::uint16_t m_countryIndicator;
157  std::uint16_t m_langIndicator;
158 };
159 
164 inline const std::vector<Mp4TagField::AdditionalData> &Mp4TagField::additionalData() const
165 {
166  return m_additionalData;
167 }
168 
173 inline std::vector<Mp4TagField::AdditionalData> &Mp4TagField::additionalData()
174 {
175  return m_additionalData;
176 }
177 
182 {
183  return false;
184 }
185 
189 inline const std::string &Mp4TagField::name() const
190 {
191  return m_name;
192 }
193 
197 inline void Mp4TagField::setName(const std::string &name)
198 {
199  m_name = name;
200 }
201 
205 inline const std::string &Mp4TagField::mean() const
206 {
207  return m_mean;
208 }
209 
213 inline void Mp4TagField::setMean(const std::string &mean)
214 {
215  m_mean = mean;
216 }
217 
221 inline std::uint32_t Mp4TagField::parsedRawDataType() const
222 {
223  return m_parsedRawDataType;
224 }
225 
229 inline std::uint16_t Mp4TagField::countryIndicator() const
230 {
231  return m_countryIndicator;
232 }
233 
237 inline std::uint16_t Mp4TagField::languageIndicator() const
238 {
239  return m_langIndicator;
240 }
241 
246 {
247  return false;
248 }
249 
256 {
257  const auto latin1 = CppUtilities::convertUtf8ToLatin1(idString.data(), idString.size());
258  switch (latin1.second) {
259  case 4:
260  return CppUtilities::BE::toUInt32(latin1.first.get());
261  default:
262  throw CppUtilities::ConversionException("MP4 ID must be exactly 4 chars");
263  }
264 }
265 
272 {
273  const auto utf8 = CppUtilities::convertLatin1ToUtf8(CppUtilities::interpretIntegerAsString<std::uint32_t>(id).data(), 4);
274  return std::string(utf8.first.get(), utf8.second);
275 }
276 
277 } // namespace TagParser
278 
279 #endif // TAG_PARSER_MP4TAGATOM_H
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
The Mp4Atom class helps to parse MP4 files.
Definition: mp4atom.h:38
The Mp4TagFieldMaker class helps making tag fields.
Definition: mp4tagfield.h:63
Mp4TagFieldMaker(Mp4TagFieldMaker &&)=default
std::uint64_t requiredSize() const
Returns number of bytes which will be written when making the field.
Definition: mp4tagfield.h:107
const Mp4TagField & field() const
Returns the associated field.
Definition: mp4tagfield.h:99
The Mp4TagField class is used by Mp4Tag to store the fields.
Definition: mp4tagfield.h:112
bool supportsNestedFields() const
Returns whether nested fields are supported.
Definition: mp4tagfield.h:245
const std::vector< AdditionalData > & additionalData() const
Returns additional data (and the corresponding raw data type, country and language).
Definition: mp4tagfield.h:164
const std::string & name() const
Returns the "name" for "extended" fields.
Definition: mp4tagfield.h:189
void setName(const std::string &name)
Sets the "name" for the "extended" field.
Definition: mp4tagfield.h:197
bool isAdditionalTypeInfoUsed() const
Returns whether the additional type info is used.
Definition: mp4tagfield.h:181
const std::string & mean() const
Returns the "mean" for "extended" fields.
Definition: mp4tagfield.h:205
static IdentifierType fieldIdFromString(std::string_view idString)
Converts the specified ID string representation to an actual ID.
Definition: mp4tagfield.h:255
std::uint32_t parsedRawDataType() const
Returns the raw data type which has been determined when parsing.
Definition: mp4tagfield.h:221
void setMean(const std::string &mean)
Sets the "mean" for the "extended" field.
Definition: mp4tagfield.h:213
std::uint16_t languageIndicator() const
Returns the language indicator.
Definition: mp4tagfield.h:237
std::uint16_t countryIndicator() const
Returns the country indicator.
Definition: mp4tagfield.h:229
static std::string fieldIdToString(IdentifierType id)
Returns the string representation for the specified id.
Definition: mp4tagfield.h:271
Defines traits for the specified ImplementationType.
The TagField class is used by FieldMapBasedTag to store the fields.
typename TagFieldTraits< Mp4TagField >::IdentifierType IdentifierType
The TagValue class wraps values of different types.
Definition: tagvalue.h:95
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10