Tag Parser 11.4.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
mpegaudioframestream.cpp
Go to the documentation of this file.
2
3#include "../exceptions.h"
4#include "../mediaformat.h"
5
6#include <sstream>
7
8using namespace std;
9using namespace CppUtilities;
10
11namespace TagParser {
12
22{
23 track.m_version = frame.mpegVersion();
24 track.m_format = MediaFormat(GeneralMediaFormat::Mpeg1Audio, static_cast<unsigned char>(frame.layer()));
26 track.m_channelConfig = static_cast<std::uint8_t>(frame.channelMode());
28}
29
31{
32 CPP_UTILITIES_UNUSED(progress)
33
34 static const string context("parsing MPEG audio frame header");
35 if (!m_istream) {
37 }
38 // get size
39 m_istream->seekg(-128, ios_base::end);
40 if (m_reader.readUInt24BE() == 0x544147) {
41 m_size = static_cast<std::uint64_t>(m_istream->tellg()) - 3u - m_startOffset;
42 } else {
43 m_size = static_cast<std::uint64_t>(m_istream->tellg()) + 125u - m_startOffset;
44 }
45 m_istream->seekg(static_cast<streamoff>(m_startOffset), ios_base::beg);
46 // parse frames until the first valid, non-empty frame is reached
47 for (size_t invalidByteskipped = 0; m_frames.size() < 200 && invalidByteskipped <= 0x600u;) {
48 MpegAudioFrame &frame = invalidByteskipped > 0 ? m_frames.back() : m_frames.emplace_back();
49 try {
50 frame.parseHeader(m_reader, diag);
51 } catch (const InvalidDataException &e) {
52 if (++invalidByteskipped > 1) {
53 diag.pop_back();
54 }
55 m_istream->seekg(-3, ios_base::cur);
56 continue;
57 }
58 if (invalidByteskipped > 1) {
59 diag.emplace_back(DiagLevel::Critical, argsToString("The next ", invalidByteskipped, " bytes are junk as well."), context);
60 }
61 if (!frame.size()) {
62 continue; // likely just junk, check further frames
63 }
64 invalidByteskipped = 0;
65 if (frame.isProtectedByCrc()) {
66 m_istream->seekg(2, ios_base::cur);
67 }
68 break;
69 }
70 if (!m_frames.back().isValid()) {
71 return;
72 }
73 const MpegAudioFrame &frame = m_frames.back();
74 addInfo(frame, *this);
75 if (frame.isXingBytesfieldPresent()) {
76 std::uint32_t xingSize = frame.xingBytesfield();
77 if (m_size && xingSize != m_size) {
78 diag.emplace_back(DiagLevel::Warning,
79 "Real length of MPEG audio frames is not in accordance with value provided by Xing header. The Xing header value will be used.",
80 context);
81 m_size = xingSize;
82 }
83 }
84 m_bitrate = frame.isXingFramefieldPresent() ? ((static_cast<double>(m_size) * 8.0)
85 / (static_cast<double>(frame.xingFrameCount() * frame.sampleCount()) / static_cast<double>(frame.samplingFrequency())) / 1024.0)
86 : frame.bitrate();
87 m_duration = TimeSpan::fromSeconds(static_cast<double>(m_size) / (m_bytesPerSecond = static_cast<std::uint32_t>(m_bitrate * 125)));
88}
89
90} // namespace TagParser
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks.
The AbstractTrack class parses and stores technical information about video, audio and other kinds of...
std::uint32_t m_bytesPerSecond
std::uint16_t m_channelCount
std::uint8_t m_channelConfig
CppUtilities::TimeSpan m_duration
CppUtilities::BinaryReader m_reader
std::uint64_t m_startOffset
std::istream * m_istream
std::uint32_t m_samplingFrequency
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
The exception that is thrown when the data to be parsed or to be made seems invalid and therefore can...
Definition: exceptions.h:25
The MediaFormat class specifies the format of media data.
Definition: mediaformat.h:246
static void addInfo(const MpegAudioFrame &frame, AbstractTrack &track)
Adds the information from the specified frame to the specified track.
void internalParseHeader(Diagnostics &diag, AbortableProgressFeedback &progress) override
This method is internally called to parse header information.
The MpegAudioFrame class is used to parse MPEG audio frames.
std::uint32_t size() const
Returns the size if known; otherwise returns 0.
std::uint32_t samplingFrequency() const
Returns the sampeling frequency of the frame if known; otherwise returns 0.
MpegChannelMode channelMode() const
Returns the channel mode if known; otherwise returns MpegChannelMode::Unspecifed.
constexpr bool isProtectedByCrc() const
Returns an indication whether the frame is protected by CRC.
std::uint32_t sampleCount() const
Returns the sample count if known; otherwise returns 0.
int layer() const
Returns the MPEG layer if known (1, 2, or 3); otherwise returns 0.
std::uint16_t bitrate() const
Returns the bitrate of the frame if known; otherwise returns 0.
constexpr std::uint32_t xingFrameCount() const
Returns an indication whether the Xing frame count is present.
constexpr bool isXingBytesfieldPresent() const
Returns an indication whether the Xing bytes field is present.
void parseHeader(CppUtilities::BinaryReader &reader, Diagnostics &diag)
Parses the header read using the specified reader.
double mpegVersion() const
Returns the MPEG version if known (1.0, 2.0 or 2.5); otherwise returns 0.
constexpr bool isXingFramefieldPresent() const
Returns an indication whether the Xing frame field is present.
constexpr std::uint32_t xingBytesfield() const
Returns the Xing bytes field if known; otherwise returns 0.
The exception that is thrown when the data to be parsed holds no parsable information (e....
Definition: exceptions.h:18
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10