Tag Parser: abstracttrack.cpp Source File

Tag Parser 12.5.2 C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags

Loading...
Searching...
No Matches
abstracttrack.cpp
Go to the documentation of this file.
1#include "./abstracttrack.h"
2#include "./exceptions.h"
3#include "./mediaformat.h"
4
5#include "./mp4/mp4ids.h"
6
7#include "mpegaudio/mpegaudioframe.h"
8
9using namespace std;
10using namespace CppUtilities;
11
12namespace TagParser {
13
15 struct AbstractTrackPrivate {};
16
24
33AbstractTrack::AbstractTrack(istream &inputStream, ostream &outputStream, std::uint64_t startOffset)
34 : m_istream(&inputStream)
35 , m_ostream(&outputStream)
36 , m_reader(BinaryReader(&inputStream))
37 , m_writer(BinaryWriter(&outputStream))
38 , m_startOffset(startOffset)
39 , m_flags(TrackFlags::Enabled | TrackFlags::UsedInPresentation | TrackFlags::UsedWhenPreviewing)
40 , m_format()
41 , m_mediaType(MediaType::Unknown)
42 , m_version(0.0)
43 , m_size(0)
44 , m_trackNumber(0)
45 , m_id(0)
46 , m_bitrate(0.0)
47 , m_maxBitrate(0.0)
48 , m_samplingFrequency(0)
49 , m_extensionSamplingFrequency(0)
50 , m_bitsPerSample(0)
51 , m_bytesPerSecond(0)
52 , m_channelCount(0)
53 , m_channelConfig(0)
54 , m_extensionChannelConfig(0)
55 , m_sampleCount(0)
56 , m_quality(0)
57 , m_depth(0)
58 , m_fps(0)
59 , m_timeScale(0)
60 , m_colorSpace(0)
61 , m_fieldOrder(FieldOrder::Undetermined)
62 , m_stereoMode(StereoMode::Unknown)
63 , m_alphaMode(AlphaMode::Unknown)
64 , m_displayUnit(DisplayUnit::Unknown)
65 , m_aspectRatioType(AspectRatioType::Unknown)
66{
67}
68
76 AbstractTrack::AbstractTrack(std::iostream &stream, std::uint64_t startOffset)
77 : AbstractTrack(stream, stream, startOffset)
78{
79}
80
84AbstractTrack::~AbstractTrack()
85{
86}
87
91std::string_view AbstractTrack::channelConfigString() const
92{
93 switch (m_format.general) {
94 case GeneralMediaFormat::Aac:
95 return m_channelConfig ? Mpeg4ChannelConfigs::channelConfigString(m_channelConfig) : std::string_view();
96 case GeneralMediaFormat::Mpeg1Audio:
97 case GeneralMediaFormat::Mpeg2Audio:
98 return mpegChannelModeString(static_cast<MpegChannelMode>(m_channelConfig));
99 default:
100 return std::string_view();
101 }
102}
103
107std::uint8_t AbstractTrack::extensionChannelConfig() const
108{
109 return m_extensionChannelConfig;
110}
111
115std::string_view AbstractTrack::extensionChannelConfigString() const
116{
117 switch (m_format.general) {
118 case GeneralMediaFormat::Aac:
119 return m_extensionChannelConfig ? Mpeg4ChannelConfigs::channelConfigString(m_extensionChannelConfig) : std::string_view();
120 default:
121 return std::string_view();
122 }
123}
124
131 string AbstractTrack::label() const
132{
133 stringstream ss;
134 ss << "ID: " << id();
135 ss << ", type: " << mediaTypeName();
136 if (!name().empty()) {
137 ss << ", name: \"" << name() << "\"";
138 }
139 if (const auto &language = locale().fullOrSomeAbbreviatedName(); !language.empty()) {
140 ss << ", language: " << language << "";
141 }
142 return ss.str();
143}
144
146string AbstractTrack::makeDescription(bool verbose) const
147{
148 // use abbreviated format
149 const auto format = MediaFormat(m_format.general, verbose ? m_format.sub : 0, verbose ? m_format.extension : 0);
150 auto formatName = format.shortAbbreviation();
151 if (formatName.empty()) {
152 // fall back to media type name if no abbreviation available
153 formatName = mediaTypeName();
154 }
155
156 // find additional info and level
157 auto additionalInfoRef = std::string_view();
158 auto level = std::string();
159 switch (m_mediaType) {
160 case MediaType::Video:
161 if (!pixelSize().isNull()) {
162 additionalInfoRef = pixelSize().abbreviation();
163 } else if (!displaySize().isNull()) {
164 additionalInfoRef = displaySize().abbreviation();
165 }
166 if (verbose) {
167 switch (format.general) {
168 case GeneralMediaFormat::Mpeg4Video:
169 case GeneralMediaFormat::Avc:
170 case GeneralMediaFormat::Hevc:
171 if (version() != 0.0) {
172 level = "@L" + numberToString(version());
173 }
174 break;
175 default:;
176 }
177 }
178 break;
179 case MediaType::Audio:
180 case MediaType::Text:
181 if (channelCount()) {
182 if (const auto &localeName = locale().someAbbreviatedName(); !localeName.empty()) {
183 return argsToString(formatName, '-', channelCount(), "ch-", localeName);
184 } else {
185 return argsToString(formatName, '-', channelCount(), 'c', 'h');
186 }
187 } else if (const auto &localeName = locale().someAbbreviatedName(); !localeName.empty()) {
188 additionalInfoRef = localeName;
189 }
190 break;
191 default:;
192 }
193
194 if (!additionalInfoRef.empty()) {
195 return argsToString(formatName, level, '-', additionalInfoRef);
196 }
197 return argsToString(formatName, level);
198}
200
212 string AbstractTrack::description() const
213{
214 return makeDescription(true);
215}
216
226 string AbstractTrack::shortDescription() const
227{
228 return makeDescription(false);
229}
230
243 void AbstractTrack::parseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)
244{
245 m_flags -= TrackFlags::HeaderValid;
246 m_istream->seekg(static_cast<streamoff>(m_startOffset), ios_base::beg);
247 try {
248 internalParseHeader(diag, progress);
249 m_flags += TrackFlags::HeaderValid;
250 } catch (const Failure &) {
251 throw;
252 }
253}
254
264
265} // namespace TagParser
abstracttrack.h
TagParser::AbortableProgressFeedback
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks.
Definition progressfeedback.h:186
TagParser::AbstractTrack::id
std::uint64_t id() const
Returns the track ID if known; otherwise returns 0.
Definition abstracttrack.h:433
TagParser::AbstractTrack::m_flags
TrackFlags m_flags
Definition abstracttrack.h:197
TagParser::AbstractTrack::m_ostream
std::ostream * m_ostream
Definition abstracttrack.h:193
TagParser::AbstractTrack::m_colorSpace
std::uint32_t m_colorSpace
Definition abstracttrack.h:232
TagParser::AbstractTrack::label
std::string label() const
Returns a label for the track.
Definition abstracttrack.cpp:131
TagParser::AbstractTrack::internalParseHeader
virtual void internalParseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)=0
This method is internally called to parse header information.
TagParser::AbstractTrack::m_extensionChannelConfig
std::uint8_t m_extensionChannelConfig
Definition abstracttrack.h:219
TagParser::AbstractTrack::m_sampleCount
std::uint64_t m_sampleCount
Definition abstracttrack.h:221
TagParser::AbstractTrack::startOffset
std::uint64_t startOffset() const
Returns the start offset of the track in the associated stream.
Definition abstracttrack.h:316
TagParser::AbstractTrack::locale
const Locale & locale() const
Returns the locale of the track if known; otherwise returns an empty locale.
Definition abstracttrack.h:509
TagParser::AbstractTrack::m_bytesPerSecond
std::uint32_t m_bytesPerSecond
Definition abstracttrack.h:216
TagParser::AbstractTrack::extensionChannelConfigString
std::string_view extensionChannelConfigString() const
Returns a string with the extension channel configuration if available; otherwise returns nullptr.
Definition abstracttrack.cpp:115
TagParser::AbstractTrack::m_version
double m_version
Definition abstracttrack.h:202
TagParser::AbstractTrack::m_quality
int m_quality
Definition abstracttrack.h:222
TagParser::AbstractTrack::m_aspectRatioType
AspectRatioType m_aspectRatioType
Definition abstracttrack.h:238
TagParser::AbstractTrack::formatName
std::string_view formatName() const
Returns the format of the track as C-style string if known; otherwise returns the format abbreviation...
Definition abstracttrack.h:352
TagParser::AbstractTrack::m_depth
std::uint16_t m_depth
Definition abstracttrack.h:227
TagParser::AbstractTrack::displaySize
const Size & displaySize() const
Returns the size of the video frames to display if known; otherwise returns a zero size.
Definition abstracttrack.h:602
TagParser::AbstractTrack::m_size
std::uint64_t m_size
Definition abstracttrack.h:203
TagParser::AbstractTrack::version
double version() const
Returns the version/level of the track if known; otherwise returns 0.
Definition abstracttrack.h:341
TagParser::AbstractTrack::pixelSize
const Size & pixelSize() const
Returns the size of the encoded video frames if known; otherwise returns a zero size.
Definition abstracttrack.h:592
TagParser::AbstractTrack::m_bitsPerSample
std::uint16_t m_bitsPerSample
Definition abstracttrack.h:215
TagParser::AbstractTrack::extensionChannelConfig
std::uint8_t extensionChannelConfig() const
Returns the extension channel configuration if available; otherwise returns nullptr.
Definition abstracttrack.cpp:107
TagParser::AbstractTrack::m_trackNumber
std::uint32_t m_trackNumber
Definition abstracttrack.h:204
TagParser::AbstractTrack::inputStream
std::istream & inputStream()
Returns the associated input stream.
Definition abstracttrack.h:248
TagParser::AbstractTrack::AbstractTrack
AbstractTrack(std::istream &inputStream, std::ostream &outputStream, std::uint64_t startOffset)
Constructs a new track.
Definition abstracttrack.cpp:33
TagParser::AbstractTrack::m_channelCount
std::uint16_t m_channelCount
Definition abstracttrack.h:217
TagParser::AbstractTrack::m_channelConfig
std::uint8_t m_channelConfig
Definition abstracttrack.h:218
TagParser::AbstractTrack::m_reader
CppUtilities::BinaryReader m_reader
Definition abstracttrack.h:194
TagParser::AbstractTrack::shortDescription
std::string shortDescription() const
Returns a short description about the track.
Definition abstracttrack.cpp:226
TagParser::AbstractTrack::mediaTypeName
std::string_view mediaTypeName() const
Returns the string representation of the media type of the track.
Definition abstracttrack.h:387
TagParser::AbstractTrack::m_startOffset
std::uint64_t m_startOffset
Definition abstracttrack.h:196
TagParser::AbstractTrack::m_maxBitrate
double m_maxBitrate
Definition abstracttrack.h:209
TagParser::AbstractTrack::parseHeader
void parseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)
Parses technical information about the track from the header.
Definition abstracttrack.cpp:243
TagParser::AbstractTrack::m_timeScale
std::uint32_t m_timeScale
Definition abstracttrack.h:231
TagParser::AbstractTrack::m_alphaMode
AlphaMode m_alphaMode
Definition abstracttrack.h:236
TagParser::AbstractTrack::m_writer
CppUtilities::BinaryWriter m_writer
Definition abstracttrack.h:195
TagParser::AbstractTrack::channelCount
std::uint16_t channelCount() const
Returns the number of channels if known; otherwise returns 0.
Definition abstracttrack.h:553
TagParser::AbstractTrack::outputStream
std::ostream & outputStream()
Returns the associated output stream.
Definition abstracttrack.h:267
TagParser::AbstractTrack::m_fps
std::uint32_t m_fps
Definition abstracttrack.h:228
TagParser::AbstractTrack::m_extensionSamplingFrequency
std::uint32_t m_extensionSamplingFrequency
Definition abstracttrack.h:214
TagParser::AbstractTrack::channelConfigString
std::string_view channelConfigString() const
Returns a string with the channel configuration if available; otherwise returns nullptr.
Definition abstracttrack.cpp:91
TagParser::AbstractTrack::name
const std::string name() const
Returns the track name if known; otherwise returns an empty string.
Definition abstracttrack.h:450
TagParser::AbstractTrack::m_fieldOrder
FieldOrder m_fieldOrder
Definition abstracttrack.h:234
TagParser::AbstractTrack::m_format
MediaFormat m_format
Definition abstracttrack.h:198
TagParser::AbstractTrack::m_bitrate
double m_bitrate
Definition abstracttrack.h:208
TagParser::AbstractTrack::m_istream
std::istream * m_istream
Definition abstracttrack.h:192
TagParser::AbstractTrack::m_samplingFrequency
std::uint32_t m_samplingFrequency
Definition abstracttrack.h:213
TagParser::AbstractTrack::description
std::string description() const
Returns a description about the track.
Definition abstracttrack.cpp:212
TagParser::AbstractTrack::m_stereoMode
StereoMode m_stereoMode
Definition abstracttrack.h:235
TagParser::AbstractTrack::m_displayUnit
DisplayUnit m_displayUnit
Definition abstracttrack.h:237
TagParser::AbstractTrack::m_id
std::uint64_t m_id
Definition abstracttrack.h:205
TagParser::AbstractTrack::m_mediaType
MediaType m_mediaType
Definition abstracttrack.h:201
TagParser::AbstractTrack::format
MediaFormat format() const
Returns the format of the track if known; otherwise returns MediaFormat::Unknown.
Definition abstracttrack.h:333
TagParser::AbstractTrack::~AbstractTrack
virtual ~AbstractTrack()
Destroys the track.
Definition abstracttrack.cpp:84
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition diagnostics.h:156
TagParser::Failure
The class inherits from std::exception and serves as base class for exceptions thrown by the elements...
Definition exceptions.h:11
TagParser::MediaFormat
The MediaFormat class specifies the format of media data.
Definition mediaformat.h:247
TagParser::MediaFormat::sub
unsigned char sub
Definition mediaformat.h:261
TagParser::MediaFormat::shortAbbreviation
std::string_view shortAbbreviation() const
Returns a short abbreviation of the media format as C-style string.
Definition mediaformat.cpp:765
TagParser::MediaFormat::extension
unsigned char extension
Definition mediaformat.h:262
TagParser::MediaFormat::general
GeneralMediaFormat general
Definition mediaformat.h:260
TagParser::Size::abbreviation
std::string_view abbreviation() const
Returns an abbreviation for the current instance, eg.
Definition size.cpp:17
exceptions.h
mediaformat.h
mp4ids.h
mpegaudioframe.h
CppUtilities
Definition abstractcontainer.h:16
TagParser::Mpeg4ChannelConfigs::channelConfigString
TAG_PARSER_EXPORT std::string_view channelConfigString(std::uint8_t config)
Returns the string representation for the specified MPEG-4 channel config.
Definition mp4ids.cpp:439
TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
TagParser::AspectRatioType
AspectRatioType
The AspectRatioType enum specifies the possible modifications to the aspect ratio.
Definition abstracttrack.h:95
TagParser::mpegChannelModeString
TAG_PARSER_EXPORT std::string_view mpegChannelModeString(MpegChannelMode channelMode)
Returns the string representation for the specified channelMode.
Definition mpegaudioframe.cpp:17
TagParser::StereoMode
StereoMode
The StereoMode enum specifies the Stereo-3D video mode.
Definition abstracttrack.h:76
TagParser::StereoMode::Unknown
@ Unknown
Definition abstracttrack.h:77
TagParser::DisplayUnit
DisplayUnit
The DisplayUnit enum specifies how display width and heigh are interpreted.
Definition abstracttrack.h:90
TagParser::AlphaMode
AlphaMode
The AlphaMode enum specifies the alpha video mode.
Definition abstracttrack.h:83
TagParser::MediaType
MediaType
The MediaType enum specifies the type of media data (audio, video, text, ...).
Definition mediaformat.h:14
TagParser::MediaType::Video
@ Video
Definition mediaformat.h:17
TagParser::MediaType::Text
@ Text
Definition mediaformat.h:18
TagParser::MediaType::Audio
@ Audio
Definition mediaformat.h:16
TagParser::GeneralMediaFormat::Hevc
@ Hevc
Definition mediaformat.h:55
TagParser::GeneralMediaFormat::Avc
@ Avc
Definition mediaformat.h:40
TagParser::GeneralMediaFormat::Aac
@ Aac
Definition mediaformat.h:32
TagParser::GeneralMediaFormat::Mpeg1Audio
@ Mpeg1Audio
Definition mediaformat.h:65
TagParser::GeneralMediaFormat::Mpeg4Video
@ Mpeg4Video
Definition mediaformat.h:70
TagParser::GeneralMediaFormat::Mpeg2Audio
@ Mpeg2Audio
Definition mediaformat.h:67
TagParser::FieldOrder
FieldOrder
The FieldOrder enum declares the field ordering of the video.
Definition abstracttrack.h:63
TagParser::FieldOrder::Undetermined
@ Undetermined
Definition abstracttrack.h:66
TagParser::MpegChannelMode
MpegChannelMode
Specifies the channel mode.
Definition mpegaudioframe.h:19
TagParser::TrackFlags
TrackFlags
The TrackFlags enum specifies miscellaneous boolean properties of a track.
Definition abstracttrack.h:47
TagParser::TrackFlags::Enabled
@ Enabled
Definition abstracttrack.h:50
TagParser::TrackFlags::HeaderValid
@ HeaderValid
Definition abstracttrack.h:49
TagParser::TrackFlags::UsedWhenPreviewing
@ UsedWhenPreviewing
Definition abstracttrack.h:56
TagParser::TrackFlags::UsedInPresentation
@ UsedInPresentation
Definition abstracttrack.h:55
TagParser::AbstractTrackPrivate
The AbstractTrackPrivate struct contains private fields of the AbstractTrack class.
Definition abstracttrack.cpp:15