Tag Parser  8.0.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 IoUtilities;
10 using namespace ConversionUtilities;
11 using namespace ChronoUtilities;
12 
13 namespace TagParser {
14 
23 void MpegAudioFrameStream::addInfo(const MpegAudioFrame &frame, AbstractTrack &track)
24 {
25  track.m_version = frame.mpegVersion();
26  track.m_format = MediaFormat(GeneralMediaFormat::Mpeg1Audio, static_cast<unsigned char>(frame.layer()));
27  track.m_channelCount = frame.channelMode() == MpegChannelMode::SingleChannel ? 1 : 2;
28  track.m_channelConfig = static_cast<byte>(frame.channelMode());
29  track.m_samplingFrequency = frame.samplingFrequency();
30 }
31 
32 void MpegAudioFrameStream::internalParseHeader(Diagnostics &diag)
33 {
34  static const string context("parsing MPEG audio frame header");
35  if (!m_istream) {
36  throw NoDataFoundException();
37  }
38  // get size
39  m_istream->seekg(-128, ios_base::end);
40  if (m_reader.readUInt24BE() == 0x544147) {
41  m_size = static_cast<uint64>(m_istream->tellg()) - 3u - m_startOffset;
42  } else {
43  m_size = static_cast<uint64>(m_istream->tellg()) + 125u - m_startOffset;
44  }
45  m_istream->seekg(static_cast<streamoff>(m_startOffset), ios_base::beg);
46  // parse frame header
47  m_frames.emplace_back();
48  MpegAudioFrame &frame = m_frames.back();
49  frame.parseHeader(m_reader, diag);
50  addInfo(frame, *this);
51  if (frame.isXingBytesfieldPresent()) {
52  uint32 xingSize = frame.xingBytesfield();
53  if (m_size && xingSize != m_size) {
54  diag.emplace_back(DiagLevel::Warning,
55  "Real length of MPEG audio frames is not equal with value provided by Xing header. The Xing header value will be used.", context);
56  m_size = xingSize;
57  }
58  }
59  m_bitrate = frame.isXingFramefieldPresent()
60  ? ((static_cast<double>(m_size) * 8.0)
61  / (static_cast<double>(frame.xingFrameCount() * frame.sampleCount()) / static_cast<double>(frame.samplingFrequency())) / 1024.0)
62  : frame.bitrate();
63  m_duration = TimeSpan::fromSeconds(static_cast<double>(m_size) / (m_bytesPerSecond = static_cast<uint32>(m_bitrate * 125)));
64 }
65 
66 } // namespace TagParser
uint32 sampleCount() const
Returns the sample count if known; otherwise returns 0.
The MpegAudioFrame class is used to parse MPEG audio frames.
double mpegVersion() const
Returns the MPEG version if known (1.0, 2.0 or 2.5); otherwise returns 0.
The MediaFormat class specifies the format of media data.
Definition: mediaformat.h:244
STL namespace.
MpegChannelMode channelMode() const
Returns the channel mode if known; otherwise returns MpegChannelMode::Unspecifed. ...
constexpr bool isXingFramefieldPresent() const
Returns an indication whether the Xing frame field is present.
int layer() const
Returns the MPEG layer if known (1, 2, or 3); otherwise returns 0.
Contains utility classes helping to read and write streams.
constexpr bool isXingBytesfieldPresent() const
Returns an indication whether the Xing bytes field is present.
The exception that is thrown when the data to be parsed holds no parsable information.
Definition: exceptions.h:18
void parseHeader(IoUtilities::BinaryReader &reader, Diagnostics &diag)
Parses the header read using the specified reader.
uint16 bitrate() const
Returns the bitrate of the frame if known; otherwise returns 0.
The AbstractTrack class parses and stores technical information about video, audio and other kinds of...
Definition: abstracttrack.h:40
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
constexpr uint32 xingFrameCount() const
Returns an indication whether the Xing frame count is present.
constexpr uint32 xingBytesfield() const
Returns the Xing bytes field if known; otherwise returns 0.
uint32 samplingFrequency() const
Returns the sampeling frequency of the frame if known; otherwise returns 0.
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156