-
Notifications
You must be signed in to change notification settings - Fork 17
/
abstracttrack.cpp
265 lines (244 loc) · 8.28 KB
/
abstracttrack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "./abstracttrack.h"
#include "./exceptions.h"
#include "./mediaformat.h"
#include "./mp4/mp4ids.h"
#include "mpegaudio/mpegaudioframe.h"
using namespace std;
using namespace CppUtilities;
namespace TagParser {
/// \brief The AbstractTrackPrivate struct contains private fields of the AbstractTrack class.
struct AbstractTrackPrivate {};
/*!
* \class TagParser::AbstractTrack
* \brief The AbstractTrack class parses and stores technical information about
* video, audio and other kinds of media tracks.
*
* The tag class only provides the interface and common functionality. It is meant to be subclassed.
*/
/*!
* \brief Constructs a new track.
* \param inputStream Specifies the stream the track will read from to perform particular operations such
* as reading header information.
* \param outputStream Specifies the stream the track will write to to perform particular operations such
* as updating or making header information.
* \param startOffset The start offset of the track in the specified \a stream.
*/
AbstractTrack::AbstractTrack(istream &inputStream, ostream &outputStream, std::uint64_t startOffset)
: m_istream(&inputStream)
, m_ostream(&outputStream)
, m_reader(BinaryReader(&inputStream))
, m_writer(BinaryWriter(&outputStream))
, m_startOffset(startOffset)
, m_flags(TrackFlags::Enabled | TrackFlags::UsedInPresentation | TrackFlags::UsedWhenPreviewing)
, m_format()
, m_mediaType(MediaType::Unknown)
, m_version(0.0)
, m_size(0)
, m_trackNumber(0)
, m_id(0)
, m_bitrate(0.0)
, m_maxBitrate(0.0)
, m_samplingFrequency(0)
, m_extensionSamplingFrequency(0)
, m_bitsPerSample(0)
, m_bytesPerSecond(0)
, m_channelCount(0)
, m_channelConfig(0)
, m_extensionChannelConfig(0)
, m_sampleCount(0)
, m_quality(0)
, m_depth(0)
, m_fps(0)
, m_timeScale(0)
, m_colorSpace(0)
, m_fieldOrder(FieldOrder::Undetermined)
, m_stereoMode(StereoMode::Unknown)
, m_alphaMode(AlphaMode::Unknown)
, m_displayUnit(DisplayUnit::Unknown)
, m_aspectRatioType(AspectRatioType::Unknown)
{
}
/*!
* \brief Constructs a new track.
* \param stream Specifies the stream the track will read from or write
* to to perform particular operations such as reading header
* information.
* \param startOffset The start offset of the track in the specified \a stream.
*/
AbstractTrack::AbstractTrack(std::iostream &stream, std::uint64_t startOffset)
: AbstractTrack(stream, stream, startOffset)
{
}
/*!
* \brief Destroys the track.
*/
AbstractTrack::~AbstractTrack()
{
}
/*!
* \brief Returns a string with the channel configuration if available; otherwise returns nullptr.
*/
std::string_view AbstractTrack::channelConfigString() const
{
switch (m_format.general) {
case GeneralMediaFormat::Aac:
return m_channelConfig ? Mpeg4ChannelConfigs::channelConfigString(m_channelConfig) : std::string_view();
case GeneralMediaFormat::Mpeg1Audio:
case GeneralMediaFormat::Mpeg2Audio:
return mpegChannelModeString(static_cast<MpegChannelMode>(m_channelConfig));
default:
return std::string_view();
}
}
/*!
* \brief Returns the extension channel configuration if available; otherwise returns nullptr.
*/
std::uint8_t AbstractTrack::extensionChannelConfig() const
{
return m_extensionChannelConfig;
}
/*!
* \brief Returns a string with the extension channel configuration if available; otherwise returns nullptr.
*/
std::string_view AbstractTrack::extensionChannelConfigString() const
{
switch (m_format.general) {
case GeneralMediaFormat::Aac:
return m_extensionChannelConfig ? Mpeg4ChannelConfigs::channelConfigString(m_extensionChannelConfig) : std::string_view();
default:
return std::string_view();
}
}
/*!
* \brief Returns a label for the track.
*
* The label contains the ID, type, name and language of the track. It is intended to be used in a menu for
* selecting a track from the file.
*/
string AbstractTrack::label() const
{
stringstream ss;
ss << "ID: " << id();
ss << ", type: " << mediaTypeName();
if (!name().empty()) {
ss << ", name: \"" << name() << "\"";
}
if (const auto &language = locale().fullOrSomeAbbreviatedName(); !language.empty()) {
ss << ", language: " << language << "";
}
return ss.str();
}
/// \cond
string AbstractTrack::makeDescription(bool verbose) const
{
// use abbreviated format
const auto format = MediaFormat(m_format.general, verbose ? m_format.sub : 0, verbose ? m_format.extension : 0);
auto formatName = format.shortAbbreviation();
if (formatName.empty()) {
// fall back to media type name if no abbreviation available
formatName = mediaTypeName();
}
// find additional info and level
auto additionalInfoRef = std::string_view();
auto level = std::string();
switch (m_mediaType) {
case MediaType::Video:
if (!pixelSize().isNull()) {
additionalInfoRef = pixelSize().abbreviation();
} else if (!displaySize().isNull()) {
additionalInfoRef = displaySize().abbreviation();
}
if (verbose) {
switch (format.general) {
case GeneralMediaFormat::Mpeg4Video:
case GeneralMediaFormat::Avc:
case GeneralMediaFormat::Hevc:
if (version() != 0.0) {
level = "@L" + numberToString(version());
}
break;
default:;
}
}
break;
case MediaType::Audio:
case MediaType::Text:
if (channelCount()) {
if (const auto &localeName = locale().someAbbreviatedName(); !localeName.empty()) {
return argsToString(formatName, '-', channelCount(), "ch-", localeName);
} else {
return argsToString(formatName, '-', channelCount(), 'c', 'h');
}
} else if (const auto &localeName = locale().someAbbreviatedName(); !localeName.empty()) {
additionalInfoRef = localeName;
}
break;
default:;
}
if (!additionalInfoRef.empty()) {
return argsToString(formatName, level, '-', additionalInfoRef);
}
return argsToString(formatName, level);
}
/// \endcond
/*!
* \brief Returns a description about the track.
*
* The description contains the abbreviated format and further information depending on the media
* type (eg. display size in case of video, language in case of audio/text). It is intended to be joined
* with descriptions of other tracks to get a short technical description about the file.
*
* Examples (exact format might change in the future!):
* - HE-AAC-6ch-eng
*/
string AbstractTrack::description() const
{
return makeDescription(true);
}
/*!
* \brief Returns a short description about the track.
*
* See description() for details.
*
* Examples (exact format might change in the future!):
* - H.264-720p
* - HE-AAC-6ch-eng
*/
string AbstractTrack::shortDescription() const
{
return makeDescription(false);
}
/*!
* \brief Parses technical information about the track from the header.
*
* The information will be read from the associated stream. The stream and the
* start offset of the track have been specified when constructing the Track.
*
* The parsed information can be accessed using the corresponding methods.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws TagParser::Failure or a derived exception when a parsing
* error occurs.
*/
void AbstractTrack::parseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)
{
m_flags -= TrackFlags::HeaderValid;
m_istream->seekg(static_cast<streamoff>(m_startOffset), ios_base::beg);
try {
internalParseHeader(diag, progress);
m_flags += TrackFlags::HeaderValid;
} catch (const Failure &) {
throw;
}
}
/*!
* \fn AbstractTrack::internalParseHeader()
* \brief This method is internally called to parse header information.
* It needs to be implemented when subclassing this class.
*
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws TagParser::Failure or a derived exception when a parsing
* error occurs.
*/
} // namespace TagParser