Tag Parser  9.4.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
id3v2tag.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_ID3V2TAG_H
2 #define TAG_PARSER_ID3V2TAG_H
3 
4 #include "./id3v2frame.h"
5 
6 #include "../fieldbasedtag.h"
7 
8 #include <map>
9 
10 namespace TagParser {
11 
12 class Id3v2Tag;
13 
15  bool operator()(std::uint32_t lhs, std::uint32_t rhs) const;
16 };
17 
19  friend class Id3v2Tag;
20 
21 public:
22  void make(std::ostream &stream, std::uint32_t padding, Diagnostics &diag);
23  const Id3v2Tag &tag() const;
24  std::uint64_t requiredSize() const;
25 
26 private:
27  Id3v2TagMaker(Id3v2Tag &tag, Diagnostics &diag);
28 
29  Id3v2Tag &m_tag;
30  std::uint32_t m_framesSize;
31  std::uint32_t m_requiredSize;
32  std::vector<Id3v2FrameMaker> m_maker;
33 };
34 
38 inline const Id3v2Tag &Id3v2TagMaker::tag() const
39 {
40  return m_tag;
41 }
42 
47 inline std::uint64_t Id3v2TagMaker::requiredSize() const
48 {
49  return m_requiredSize;
50 }
51 
56 public:
59 };
60 
61 class TAG_PARSER_EXPORT Id3v2Tag : public FieldMapBasedTag<Id3v2Tag> {
62  friend class FieldMapBasedTag<Id3v2Tag>;
63  friend class Id3v2TagMaker;
64 
65 public:
66  Id3v2Tag();
67 
68  static constexpr TagType tagType = TagType::Id3v2Tag;
69  static constexpr const char *tagName = "ID3v2 tag";
70  static constexpr TagTextEncoding defaultTextEncoding = TagTextEncoding::Utf16LittleEndian;
71  TagTextEncoding proposedTextEncoding() const override;
72  bool canEncodingBeUsed(TagTextEncoding encoding) const override;
73  bool supportsDescription(KnownField field) const override;
74  bool supportsMimeType(KnownField field) const override;
75  bool supportsMultipleValues(KnownField field) const override;
76  void ensureTextValuesAreProperlyEncoded() override;
77 
78  void parse(std::istream &sourceStream, const std::uint64_t maximalSize, Diagnostics &diag);
79  Id3v2TagMaker prepareMaking(Diagnostics &diag);
80  void make(std::ostream &targetStream, std::uint32_t padding, Diagnostics &diag);
81 
82  std::uint8_t majorVersion() const;
83  std::uint8_t revisionVersion() const;
84  void setVersion(std::uint8_t majorVersion, std::uint8_t revisionVersion);
85  bool isVersionSupported() const;
86  std::uint8_t flags() const;
87  bool isUnsynchronisationUsed() const;
88  bool hasExtendedHeader() const;
89  bool isExperimental() const;
90  bool hasFooter() const;
91  std::uint32_t extendedHeaderSize() const;
92  std::uint32_t paddingSize() const;
93 
94 protected:
95  IdentifierType internallyGetFieldId(KnownField field) const;
96  KnownField internallyGetKnownField(const IdentifierType &id) const;
97  TagDataType internallyGetProposedDataType(const std::uint32_t &id) const;
98  std::vector<const TagValue *> internallyGetValues(const IdentifierType &id) const;
99  bool internallySetValues(const IdentifierType &id, const std::vector<TagValue> &values);
100 
101 private:
102  void convertOldRecordDateFields(const std::string &diagContext, Diagnostics &diag);
103  void removeOldRecordDateRelatedFields();
104  void prepareRecordDataForMaking(const std::string &diagContext, Diagnostics &diag);
105 
106 private:
107  std::uint8_t m_majorVersion;
108  std::uint8_t m_revisionVersion;
109  std::uint8_t m_flags;
110  std::uint32_t m_sizeExcludingHeader;
111  std::uint32_t m_extendedHeaderSize;
112  std::uint32_t m_paddingSize;
113 };
114 
119  : m_majorVersion(4)
120  , m_revisionVersion(0)
121  , m_flags(0)
122  , m_sizeExcludingHeader(0)
123  , m_extendedHeaderSize(0)
124  , m_paddingSize(0)
125 {
126 }
127 
129 {
130  return m_majorVersion > 3 ? TagTextEncoding::Utf8 : TagTextEncoding::Utf16LittleEndian;
131 }
132 
133 inline bool Id3v2Tag::canEncodingBeUsed(TagTextEncoding encoding) const
134 {
135  return encoding == TagTextEncoding::Latin1 || (encoding == TagTextEncoding::Utf8 && m_majorVersion > 3)
136  || encoding == TagTextEncoding::Utf16BigEndian || encoding == TagTextEncoding::Utf16LittleEndian;
137 }
138 
140 {
141  switch (field) {
142  case KnownField::Cover:
143  case KnownField::Lyrics:
144  case KnownField::SynchronizedLyrics:
145  return true;
146  default:
147  return false;
148  }
149 }
150 
151 inline bool Id3v2Tag::supportsMimeType(KnownField field) const
152 {
153  return field == KnownField::Cover;
154 }
155 
159 inline std::uint8_t Id3v2Tag::majorVersion() const
160 {
161  return m_majorVersion;
162 }
163 
167 inline std::uint8_t Id3v2Tag::revisionVersion() const
168 {
169  return m_revisionVersion;
170 }
171 
178 inline bool Id3v2Tag::isVersionSupported() const
179 {
180  return m_majorVersion == 2 || m_majorVersion == 3 || m_majorVersion == 4;
181 }
182 
186 inline std::uint8_t Id3v2Tag::flags() const
187 {
188  return m_flags;
189 }
190 
195 {
196  return m_flags & 0x80;
197 }
198 
202 inline bool Id3v2Tag::hasExtendedHeader() const
203 {
204  return (m_majorVersion >= 3) && (m_flags & 0x40);
205 }
206 
210 inline bool Id3v2Tag::isExperimental() const
211 {
212  return (m_majorVersion >= 3) && (m_flags & 0x20);
213 }
214 
218 inline bool Id3v2Tag::hasFooter() const
219 {
220  return (m_majorVersion >= 3) && (m_flags & 0x10);
221 }
222 
226 inline std::uint32_t Id3v2Tag::extendedHeaderSize() const
227 {
228  return m_extendedHeaderSize;
229 }
230 
235 inline std::uint32_t Id3v2Tag::paddingSize() const
236 {
237  return m_paddingSize;
238 }
239 
240 } // namespace TagParser
241 
242 #endif // TAG_PARSER_ID3V2TAG_H
TagParser::Id3v2Tag::isVersionSupported
bool isVersionSupported() const
Returns an indication whether the version is supported by the Id3v2Tag class.
Definition: id3v2tag.h:178
TagParser::Id3v2Tag::hasExtendedHeader
bool hasExtendedHeader() const
Returns an indication whether an extended header is used.
Definition: id3v2tag.h:202
TagParser::RawDataType::Utf8
@ Utf8
Definition: mp4tagfield.h:21
TagParser::Id3v2Frame
The Id3v2Frame class is used by Id3v2Tag to store the fields.
Definition: id3v2frame.h:86
TagParser::Mp4TagAtomIds::Lyrics
@ Lyrics
Definition: mp4ids.h:105
TagParser::Id3v2Tag::isExperimental
bool isExperimental() const
Returns an indication whether the tag is labeled as experimental.
Definition: id3v2tag.h:210
TagParser::TagTextEncoding
TagTextEncoding
Specifies the text encoding.
Definition: tagvalue.h:25
TagParser::Mp4TagAtomIds::Cover
@ Cover
Definition: mp4ids.h:94
TagParser::Id3v2Tag::flags
std::uint8_t flags() const
Returns the flags read from the ID3v2 header.
Definition: id3v2tag.h:186
TagParser::Id3v2Tag::hasFooter
bool hasFooter() const
Returns an indication whether a footer is present.
Definition: id3v2tag.h:218
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
TagParser::Id3v2Tag::supportsDescription
bool supportsDescription(KnownField field) const override
Returns an indications whether the specified field supports descriptions.
Definition: id3v2tag.h:139
TagParser
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
TagParser::TagDataType
TagDataType
Specifies the data type.
Definition: tagvalue.h:54
TagParser::Id3v2Tag::extendedHeaderSize
std::uint32_t extendedHeaderSize() const
Returns the size of the extended header if one is present; otherwise returns 0.
Definition: id3v2tag.h:226
TagParser::Id3v2Tag::isUnsynchronisationUsed
bool isUnsynchronisationUsed() const
Returns an indication whether unsynchronisation is used.
Definition: id3v2tag.h:194
TagParser::Id3v2Tag::majorVersion
std::uint8_t majorVersion() const
Returns the major version if known; otherwise returns 0.
Definition: id3v2tag.h:159
TagParser::Id3v2Tag::canEncodingBeUsed
bool canEncodingBeUsed(TagTextEncoding encoding) const override
Returns an indication whether the specified encoding can be used to provide string values for the tag...
Definition: id3v2tag.h:133
TagParser::Id3v2Tag::proposedTextEncoding
TagTextEncoding proposedTextEncoding() const override
Returns the proposed text encoding.
Definition: id3v2tag.h:128
TagParser::Id3v2Tag
Implementation of TagParser::Tag for ID3v2 tags.
Definition: id3v2tag.h:61
TagParser::KnownField
KnownField
Specifies the field.
Definition: tag.h:42
TagParser::Id3v2Tag::Id3v2Tag
Id3v2Tag()
Constructs a new tag.
Definition: id3v2tag.h:118
TagParser::FieldMapBasedTag
The FieldMapBasedTag provides a generic implementation of Tag which stores the tag fields using std::...
Definition: fieldbasedtag.h:31
TagParser::TagType::Id3v2Tag
@ Id3v2Tag
TAG_PARSER_EXPORT
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
TagParser::Id3v2Tag::revisionVersion
std::uint8_t revisionVersion() const
Returns the revision version if known; otherwise returns 0.
Definition: id3v2tag.h:167
TagParser::FrameComparer
Defines the order which is used to store ID3v2 frames.
Definition: id3v2tag.h:14
TagParser::Id3v2TagMaker::requiredSize
std::uint64_t requiredSize() const
Returns the number of bytes which will be written when making the tag.
Definition: id3v2tag.h:47
TagParser::FieldMapBasedTagTraits
Defines traits for the specified ImplementationType.
Definition: fieldbasedtag.h:17
id3v2frame.h
TagParser::Id3v2Tag::paddingSize
std::uint32_t paddingSize() const
Returns the size of the padding between the tag and the first MPEG frame if one is present; otherwise...
Definition: id3v2tag.h:235
TagParser::Id3v2Tag::supportsMimeType
bool supportsMimeType(KnownField field) const override
Returns an indications whether the specified field supports mime types.
Definition: id3v2tag.h:151
TagParser::Id3v2TagMaker
The Id3v2TagMaker class helps writing ID3v2 tags.
Definition: id3v2tag.h:18
TagParser::TagType
TagType
Specifies the tag type.
Definition: tag.h:20
TagParser::Id3v2TagMaker::tag
const Id3v2Tag & tag() const
Returns the associated tag.
Definition: id3v2tag.h:38