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