Tag Parser  9.1.2
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 frame header
45  m_frames.emplace_back();
46  MpegAudioFrame &frame = m_frames.back();
47  frame.parseHeader(m_reader, diag);
48  addInfo(frame, *this);
49  if (frame.isXingBytesfieldPresent()) {
50  std::uint32_t xingSize = frame.xingBytesfield();
51  if (m_size && xingSize != m_size) {
52  diag.emplace_back(DiagLevel::Warning,
53  "Real length of MPEG audio frames is not equal with value provided by Xing header. The Xing header value will be used.", context);
54  m_size = xingSize;
55  }
56  }
57  m_bitrate = frame.isXingFramefieldPresent() ? ((static_cast<double>(m_size) * 8.0)
58  / (static_cast<double>(frame.xingFrameCount() * frame.sampleCount()) / static_cast<double>(frame.samplingFrequency())) / 1024.0)
59  : frame.bitrate();
60  m_duration = TimeSpan::fromSeconds(static_cast<double>(m_size) / (m_bytesPerSecond = static_cast<std::uint32_t>(m_bitrate * 125)));
61 }
62 
63 } // namespace TagParser
TagParser::AbstractTrack::m_samplingFrequency
std::uint32_t m_samplingFrequency
Definition: abstracttrack.h:139
TagParser::MpegAudioFrame::samplingFrequency
std::uint32_t samplingFrequency() const
Returns the sampeling frequency of the frame if known; otherwise returns 0.
Definition: mpegaudioframe.cpp:110
TagParser::MpegAudioFrame::parseHeader
void parseHeader(CppUtilities::BinaryReader &reader, Diagnostics &diag)
Parses the header read using the specified reader.
Definition: mpegaudioframe.cpp:47
TagParser::MpegAudioFrame::layer
int layer() const
Returns the MPEG layer if known (1, 2, or 3); otherwise returns 0.
Definition: mpegaudioframe.cpp:93
TagParser::AbstractTrack::m_version
double m_version
Definition: abstracttrack.h:128
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:465
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:173
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:143
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:152
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::AbstractTrack::m_format
MediaFormat m_format
Definition: abstracttrack.h:124
TagParser::AbstractTrack::m_channelConfig
std::uint8_t m_channelConfig
Definition: abstracttrack.h:144
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:76
TagParser::MediaFormat
The MediaFormat class specifies the format of media data.
Definition: mediaformat.h:245
mpegaudioframestream.h