-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathabstracttrack.h
783 lines (698 loc) · 20.6 KB
/
abstracttrack.h
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#ifndef TAG_PARSER_ABSTRACTTRACK_H
#define TAG_PARSER_ABSTRACTTRACK_H
#include "./aspectratio.h"
#include "./diagnostics.h"
#include "./localehelper.h"
#include "./margin.h"
#include "./mediaformat.h"
#include "./size.h"
#include <c++utilities/chrono/datetime.h>
#include <c++utilities/chrono/timespan.h>
#include <c++utilities/io/binaryreader.h>
#include <c++utilities/io/binarywriter.h>
#include <c++utilities/misc/flagenumclass.h>
#include <iosfwd>
#include <memory>
#include <string>
#include <string_view>
namespace TagParser {
class AbortableProgressFeedback;
class MpegAudioFrameStream;
class WaveAudioStream;
class Mp4Track;
/*!
* \brief The TrackType enum specifies the underlying file type of a track and the concrete class of the track object.
*/
enum class TrackType {
Unspecified, /**< The track type is not specified. */
MatroskaTrack, /**< The track is a TagParser::MatroskaTrack. */
MpegAudioFrameStream, /**< The track is a TagParser::MpegAudioFrameStream. */
Mp4Track, /**< The track is a TagParser::Mp4Track. */
WaveAudioStream, /**< The track is a TagParser::WaveAudioStream. */
OggStream, /**< The track is a TagParser::OggStream. */
AdtsStream, /**< The track is a TagParser::AdtsStream. */
FlacStream, /**< The track is a TagParser::FlacStream. */
IvfStream, /**< The track is a TagParser::IvfStream. */
};
/*!
* \brief The TrackFlags enum specifies miscellaneous boolean properties of a track.
*/
enum class TrackFlags : std::uint64_t {
None = 0, /**< No flags present */
HeaderValid = (1 << 0), /**< The track header is valid. Set by AbstractTrack::parseHeader() on success. */
Enabled = (1 << 2), /**< The track is marked as enabled. */
Default = (1 << 3), /**< The track is marked as default. */
Forced = (1 << 4), /**< The track is marked as forced. */
Lacing = (1 << 5), /**< The track has lacing. */
Encrypted = (1 << 6), /**< The track is encrypted. */
UsedInPresentation = (1 << 7), /**< The track is supposed to be used in presentation. */
UsedWhenPreviewing = (1 << 8), /**< The track is supposed to be used when previewing. */
Interlaced = (1 << 9), /**< The video is interlaced. */
};
/*!
* \brief The FieldOrder enum declares the field ordering of the video.
*/
enum class FieldOrder : std::uint64_t {
Progressive = 0,
Tff = 1,
Undetermined = 2,
Bff = 6,
BffSwapped = 9,
TffSwapped = 14,
};
/*!
* \brief The StereoMode enum specifies the Stereo-3D video mode.
* \sa https://matroska.org/technical/notes.html#multi-planar-and-3d-videos
*/
enum class StereoMode : std::uint64_t {
Unknown = std::numeric_limits<std::uint64_t>::max(),
};
/*!
* \brief The AlphaMode enum specifies the alpha video mode.
*/
enum class AlphaMode : std::uint64_t {
Unknown = std::numeric_limits<std::uint64_t>::max(),
};
/*!
* \brief The DisplayUnit enum specifies how display width and heigh are interpreted.
*/
enum class DisplayUnit : std::uint64_t { Pixels, Centimeters, Inches, DisplayAspectRatio, Unknown };
/*!
* \brief The AspectRatioType enum specifies the possible modifications to the aspect ratio.
*/
enum class AspectRatioType : std::uint64_t {
FreeResizing,
KeepAspectRatio,
Fixed,
Unknown = std::numeric_limits<std::uint64_t>::max(),
};
} // namespace TagParser
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(TagParser, TagParser::TrackFlags)
namespace TagParser {
struct AbstractTrackPrivate;
class TAG_PARSER_EXPORT AbstractTrack {
friend class MpegAudioFrameStream;
friend class WaveAudioStream;
friend class Mp4Track;
public:
virtual ~AbstractTrack();
virtual TrackType type() const;
std::istream &inputStream();
void setInputStream(std::istream &stream);
std::ostream &outputStream();
void setOutputStream(std::ostream &stream);
CppUtilities::BinaryReader &reader();
CppUtilities::BinaryWriter &writer();
std::uint64_t startOffset() const;
TrackFlags flags() const;
MediaFormat format() const;
double version() const;
std::string_view formatName() const;
std::string_view formatAbbreviation() const;
const std::string &formatId() const;
MediaType mediaType() const;
std::string_view mediaTypeName() const;
std::uint64_t size() const;
void setSize(std::uint64_t size);
std::uint32_t trackNumber() const;
void setTrackNumber(std::uint32_t trackNumber);
std::uint64_t id() const;
void setId(std::uint64_t id);
const std::string name() const;
void setName(std::string_view name);
const CppUtilities::TimeSpan &duration() const;
double bitrate() const;
double maxBitrate() const;
const CppUtilities::DateTime &creationTime() const;
const CppUtilities::DateTime &modificationTime() const;
const Locale &locale() const;
void setLocale(const Locale &locale);
std::uint32_t samplingFrequency() const;
std::uint32_t extensionSamplingFrequency() const;
std::uint16_t bitsPerSample() const;
std::uint16_t channelCount() const;
std::uint8_t channelConfig() const;
std::string_view channelConfigString() const;
std::uint8_t extensionChannelConfig() const;
std::string_view extensionChannelConfigString() const;
std::uint64_t sampleCount() const;
int quality() const;
const Size &pixelSize() const;
const Size &displaySize() const;
const Size &resolution() const;
const std::string &compressorName() const;
void setCompressorName(std::string_view compressorName);
std::uint16_t depth() const;
std::uint32_t fps() const;
std::string_view chromaFormat() const;
const AspectRatio &pixelAspectRatio() const;
bool isInterlaced() const;
std::uint32_t timeScale() const;
bool isEnabled() const;
void setEnabled(bool enabled);
bool isDefault() const;
void setDefault(bool isDefault);
bool isForced() const;
void setForced(bool forced);
bool hasLacing() const;
bool isEncrypted() const;
std::uint32_t colorSpace() const;
const Margin &cropping() const;
std::string label() const;
std::string description() const;
std::string shortDescription() const;
void parseHeader(Diagnostics &diag, AbortableProgressFeedback &progress);
bool isHeaderValid() const;
protected:
AbstractTrack(std::istream &inputStream, std::ostream &outputStream, std::uint64_t startOffset);
AbstractTrack(std::iostream &stream, std::uint64_t startOffset);
virtual void internalParseHeader(Diagnostics &diag, AbortableProgressFeedback &progress) = 0;
std::istream *m_istream;
std::ostream *m_ostream;
CppUtilities::BinaryReader m_reader;
CppUtilities::BinaryWriter m_writer;
std::uint64_t m_startOffset;
TrackFlags m_flags;
MediaFormat m_format;
std::string m_formatId;
std::string m_formatName;
MediaType m_mediaType;
double m_version;
std::uint64_t m_size;
std::uint32_t m_trackNumber;
std::uint64_t m_id;
std::string m_name;
CppUtilities::TimeSpan m_duration;
double m_bitrate;
double m_maxBitrate;
CppUtilities::DateTime m_creationTime;
CppUtilities::DateTime m_modificationTime;
Locale m_locale;
std::uint32_t m_samplingFrequency;
std::uint32_t m_extensionSamplingFrequency;
std::uint16_t m_bitsPerSample;
std::uint32_t m_bytesPerSecond;
std::uint16_t m_channelCount;
std::uint8_t m_channelConfig;
std::uint8_t m_extensionChannelConfig;
std::uint16_t m_chunkSize;
std::uint64_t m_sampleCount;
int m_quality;
Size m_pixelSize;
Size m_displaySize;
Size m_resolution;
std::string m_compressorName;
std::uint16_t m_depth;
std::uint32_t m_fps;
std::string_view m_chromaFormat;
AspectRatio m_pixelAspectRatio;
std::uint32_t m_timeScale;
std::uint32_t m_colorSpace;
Margin m_cropping;
FieldOrder m_fieldOrder;
StereoMode m_stereoMode;
AlphaMode m_alphaMode;
DisplayUnit m_displayUnit;
AspectRatioType m_aspectRatioType;
std::unique_ptr<AbstractTrackPrivate> m_p;
private:
std::string makeDescription(bool verbose) const;
};
/*!
* \brief Returns the associated input stream.
*/
inline std::istream &AbstractTrack::inputStream()
{
return *m_istream;
}
/*!
* \brief Assigns another input stream.
*
* The track will read from the \a stream to perform
* particular operations such as reading header information.
*/
inline void AbstractTrack::setInputStream(std::istream &stream)
{
m_reader.setStream(m_istream = &stream);
}
/*!
* \brief Returns the associated output stream.
*/
inline std::ostream &AbstractTrack::outputStream()
{
return *m_ostream;
}
/*!
* \brief Assigns another output stream.
*
* The track will write to the \a stream to perform
* particular operations such as updating or making header information.
*/
inline void AbstractTrack::setOutputStream(std::ostream &stream)
{
m_writer.setStream(m_ostream = &stream);
}
/*!
* \brief Returns a binary reader for the associated stream.
*
* \remarks The returned reader is internally used when reading binary data
* from the associated stream. Do not change its byte order.
*/
inline CppUtilities::BinaryReader &AbstractTrack::reader()
{
return m_reader;
}
/*!
* \brief Returns a binary writer for the associated stream.
*
* \remarks The returned writer is internally used when writing binary data
* to the associated stream. Do not change its byte order.
*/
inline CppUtilities::BinaryWriter &AbstractTrack::writer()
{
return m_writer;
}
/*!
* \brief Returns the type of the track if known; otherwise returns TrackType::Unspecified.
*/
inline TrackType AbstractTrack::type() const
{
return TrackType::Unspecified;
}
/*!
* \brief Returns the start offset of the track in the associated stream.
*/
inline std::uint64_t AbstractTrack::startOffset() const
{
return m_startOffset;
}
/*!
* \brief Returns flags (various boolean properties) of this track.
* \sa TrackFlags
*/
inline TrackFlags AbstractTrack::flags() const
{
return m_flags;
}
/*!
* \brief Returns the format of the track if known; otherwise returns MediaFormat::Unknown.
*/
inline MediaFormat AbstractTrack::format() const
{
return m_format;
}
/*!
* \brief Returns the version/level of the track if known; otherwise returns 0.
*/
inline double AbstractTrack::version() const
{
return m_version;
}
/*!
* \brief Returns the format of the track as C-style string if known; otherwise
* returns the format abbreviation or an empty string.
* \remarks
* - The string might get invalidated when the track is (re)parsed.
*/
inline std::string_view AbstractTrack::formatName() const
{
return m_format || m_formatName.empty() ? m_format.name() : m_formatName;
}
/*!
* \brief Returns the a more or less common abbreviation for the format of the track
* if known; otherwise returns an empty string.
*/
inline std::string_view AbstractTrack::formatAbbreviation() const
{
const auto abbr = m_format.abbreviation();
return !abbr.empty() || m_formatId.empty() ? abbr : m_formatId;
}
/*!
* \brief Returns the format/codec ID. This is usually the raw format identifier
* extracted from the container) if known; otherwise returns an empty string.
*/
inline const std::string &AbstractTrack::formatId() const
{
return m_formatId;
}
/*!
* \brief Returns the media type if known; otherwise returns MediaType::Other.
*/
inline MediaType AbstractTrack::mediaType() const
{
return m_mediaType;
}
/*!
* \brief Returns the string representation of the media type of the track.
*/
inline std::string_view AbstractTrack::mediaTypeName() const
{
return ::TagParser::mediaTypeName(m_mediaType);
}
/*!
* \brief Returns the size in bytes if known; otherwise returns 0.
*/
inline std::uint64_t AbstractTrack::size() const
{
return m_size;
}
/*!
* \brief Sets the size in bytes.
* \remarks
* This is used by MediaFileInfo to set the track size for certain types of tracks before invoking the parsing.
* If you use this a class derived from AbstractTrack directly you may want to do the same if not the entire
* input stream is supposed to be considered part of the track and the parser would otherwise assume that (like
* the parser of MpegAudioFrameStream might do).
*/
inline void AbstractTrack::setSize(std::uint64_t size)
{
m_size = size;
}
/*!
* \brief Returns the track number if known; otherwise returns 0.
*/
inline std::uint32_t AbstractTrack::trackNumber() const
{
return m_trackNumber;
}
/*!
* \brief Sets the track number.
* \remarks Whether the new value is applied when saving changes depends on the implementation.
*/
inline void AbstractTrack::setTrackNumber(std::uint32_t trackNumber)
{
m_trackNumber = trackNumber;
}
/*!
* \brief Returns the track ID if known; otherwise returns 0.
*/
inline std::uint64_t AbstractTrack::id() const
{
return m_id;
}
/*!
* \brief Sets the track ID.
* \remarks Whether the new value is applied when saving changes depends on the implementation.
*/
inline void AbstractTrack::setId(std::uint64_t id)
{
m_id = id;
}
/*!
* \brief Returns the track name if known; otherwise returns an empty string.
*/
inline const std::string AbstractTrack::name() const
{
return m_name;
}
/*!
* \brief Sets the name.
* \remarks Whether the new value is applied when saving changes depends on the implementation.
*/
inline void AbstractTrack::setName(std::string_view name)
{
m_name = name;
}
/*!
* \brief Returns the duration if known; otherwise returns a TimeSpan of zero ticks.
*/
inline const CppUtilities::TimeSpan &AbstractTrack::duration() const
{
return m_duration;
}
/*!
* \brief Returns the average bitrate in kbit/s if known; otherwise returns zero.
*/
inline double AbstractTrack::bitrate() const
{
return m_bitrate;
}
/*!
* \brief Returns the maximum bitrate in kbit/s if known; otherwise returns zero.
*/
inline double AbstractTrack::maxBitrate() const
{
return m_maxBitrate;
}
/*!
* \brief Returns the creation time if known; otherwise returns a DateTime of zero ticks.
*/
inline const CppUtilities::DateTime &AbstractTrack::creationTime() const
{
return m_creationTime;
}
/*!
* \brief Returns the time of the last modification if known; otherwise returns a DateTime of zero ticks.
*/
inline const CppUtilities::DateTime &AbstractTrack::modificationTime() const
{
return m_modificationTime;
}
/*!
* \brief Returns the locale of the track if known; otherwise returns an empty locale.
*
* The format of the locale depends on the particular format/implementation.
*/
inline const Locale &AbstractTrack::locale() const
{
return m_locale;
}
/*!
* \brief Sets the locale of the track.
* \remarks Whether the new value is applied when saving changes depends on the format/implementation.
*/
inline void AbstractTrack::setLocale(const Locale &locale)
{
m_locale = locale;
}
/*!
* \brief Returns the number of samples per second if known; otherwise returns 0.
*/
inline std::uint32_t AbstractTrack::samplingFrequency() const
{
return m_samplingFrequency;
}
/*!
* \brief Returns the number of samples per second if known; otherwise returns 0.
* \remarks This sample rate value takes extensions like SBR into account.
*/
inline std::uint32_t AbstractTrack::extensionSamplingFrequency() const
{
return m_extensionSamplingFrequency;
}
/*!
* \brief Returns the number of bits per sample; otherwise returns 0.
*/
inline std::uint16_t AbstractTrack::bitsPerSample() const
{
return m_bitsPerSample;
}
/*!
* \brief Returns the number of channels if known; otherwise returns 0.
*
* This value only makes sense for audio tracks.
*/
inline std::uint16_t AbstractTrack::channelCount() const
{
return m_channelCount;
}
/*!
* \brief Returns the channel configuration.
*
* This is the MPEG-4 channel config for MPEG-4 audio.
* \sa Mpeg4ChannelConfigs::channelConfigString()
*/
inline std::uint8_t AbstractTrack::channelConfig() const
{
return m_channelConfig;
}
/*!
* \brief Returns the number of samples/frames if known; otherwise returns 0.
*/
inline std::uint64_t AbstractTrack::sampleCount() const
{
return m_sampleCount;
}
/*!
* \brief Returns the quality if known; otherwise returns 0.
*
* The scale depends on the format.
*/
inline int AbstractTrack::quality() const
{
return m_quality;
}
/*!
* \brief Returns the size of the encoded video frames if known; otherwise returns a zero size.
*
* This value only makes sense for video tracks.
*/
inline const Size &AbstractTrack::pixelSize() const
{
return m_pixelSize;
}
/*!
* \brief Returns the size of the video frames to display if known; otherwise returns a zero size.
*
* This value only makes sense for video tracks.
*/
inline const Size &AbstractTrack::displaySize() const
{
return m_displaySize;
}
/*!
* \brief Returns the resolution if known; otherwise returns a zero size.
*
* This value only makes sense for video tracks.
*/
inline const Size &AbstractTrack::resolution() const
{
return m_resolution;
}
/*!
* \brief Returns the compressor name if known; otherwise returns an empty string.
*/
inline const std::string &AbstractTrack::compressorName() const
{
return m_compressorName;
}
/*!
* \brief Returns the compressor name if known; otherwise returns an empty string.
* \remarks Whether the new value is applied when saving changes depends on the implementation.
*/
inline void AbstractTrack::setCompressorName(std::string_view compressorName)
{
m_compressorName = compressorName;
}
/*!
* \brief Returns the bit depth if known; otherwise returns 0.
*/
inline std::uint16_t AbstractTrack::depth() const
{
return m_depth;
}
/*!
* \brief Returns the number of frames per second if known; otherwise returns 0.
*
* This value only makes sense for video tracks.
*/
inline std::uint32_t AbstractTrack::fps() const
{
return m_fps;
}
/*!
* \brief Returns the chroma subsampling format if known; otherwise returns nullptr.
*
* This value only makes sense for video tracks.
*/
inline std::string_view AbstractTrack::chromaFormat() const
{
return m_chromaFormat;
}
/*!
* \brief Returns the pixel aspect ratio (PAR).
*/
inline const AspectRatio &AbstractTrack::pixelAspectRatio() const
{
return m_pixelAspectRatio;
}
/*!
* \brief Returns true if the video is interlaced; otherwise returns false.
*
* This value only makes sense for video tracks.
*/
inline bool AbstractTrack::isInterlaced() const
{
return m_flags & TrackFlags::Interlaced;
}
/*!
* \brief Returns the time scale if known; otherwise returns 0.
*
* The time scale depends on the format.
*/
inline std::uint32_t AbstractTrack::timeScale() const
{
return m_timeScale;
}
/*!
* \brief Returns true if the track is marked as enabled; otherwise returns false.
*/
inline bool AbstractTrack::isEnabled() const
{
return m_flags & TrackFlags::Enabled;
}
/*!
* \brief Sets whether the track is enabled.
* \remarks Whether the new value is applied when saving changes depends on the implementation.
*/
inline void AbstractTrack::setEnabled(bool enabled)
{
CppUtilities::modFlagEnum(m_flags, TrackFlags::Enabled, enabled);
}
/*!
* \brief Returns true if the track is marked as default; otherwise returns false.
*/
inline bool AbstractTrack::isDefault() const
{
return m_flags & TrackFlags::Default;
}
/*!
* \brief Sets whether the track is a default track.
* \remarks Whether the new value is applied when saving changes depends on the implementation.
*/
inline void AbstractTrack::setDefault(bool isDefault)
{
CppUtilities::modFlagEnum(m_flags, TrackFlags::Default, isDefault);
}
/*!
* \brief Returns true if the track is marked as forced; otherwise returns false.
*/
inline bool AbstractTrack::isForced() const
{
return m_flags & TrackFlags::Forced;
}
/*!
* \brief Sets whether the track is forced.
* \remarks Whether the new value is applied when saving changes depends on the implementation.
*/
inline void AbstractTrack::setForced(bool forced)
{
CppUtilities::modFlagEnum(m_flags, TrackFlags::Forced, forced);
}
/*!
* \brief Returns true if the track has lacing; otherwise returns false.
*/
inline bool AbstractTrack::hasLacing() const
{
return m_flags & TrackFlags::Lacing;
}
/*!
* \brief Returns true if the track is marked as encrypted; otherwise returns false.
*/
inline bool AbstractTrack::isEncrypted() const
{
return m_flags & TrackFlags::Encrypted;
}
/*!
* \brief Returns the color space if known; otherwise returns 0.
*/
inline std::uint32_t AbstractTrack::colorSpace() const
{
return m_colorSpace;
}
/*!
* \brief Returns the cropping if known; otherwise returns zero margins.
*/
inline const Margin &AbstractTrack::cropping() const
{
return m_cropping;
}
/*!
* \brief Returns an indication whether the track header is valid.
*/
inline bool AbstractTrack::isHeaderValid() const
{
return m_flags & TrackFlags::HeaderValid;
}
} // namespace TagParser
#endif // TAG_PARSER_ABSTRACTTRACK_H