tagparser/mpegaudio/mpegaudioframestream.cpp

64 lines
2.4 KiB
C++
Raw Normal View History

2015-09-06 19:57:33 +02:00
#include "./mpegaudioframestream.h"
2015-04-22 19:22:01 +02:00
2015-09-06 19:57:33 +02:00
#include "../exceptions.h"
#include "../mediaformat.h"
2015-04-22 19:22:01 +02:00
#include <sstream>
using namespace std;
2019-06-10 22:49:11 +02:00
using namespace CppUtilities;
2015-04-22 19:22:01 +02:00
namespace TagParser {
2015-04-22 19:22:01 +02:00
/*!
* \class TagParser::MpegAudioFrameStream
* \brief Implementation of TagParser::AbstractTrack MPEG audio streams.
2015-04-22 19:22:01 +02:00
*/
/*!
* \brief Adds the information from the specified \a frame to the specified \a track.
*/
void MpegAudioFrameStream::addInfo(const MpegAudioFrame &frame, AbstractTrack &track)
{
track.m_version = frame.mpegVersion();
track.m_format = MediaFormat(GeneralMediaFormat::Mpeg1Audio, static_cast<unsigned char>(frame.layer()));
track.m_channelCount = frame.channelMode() == MpegChannelMode::SingleChannel ? 1 : 2;
2019-03-13 19:06:42 +01:00
track.m_channelConfig = static_cast<std::uint8_t>(frame.channelMode());
track.m_samplingFrequency = frame.samplingFrequency();
}
void MpegAudioFrameStream::internalParseHeader(Diagnostics &diag)
2015-04-22 19:22:01 +02:00
{
static const string context("parsing MPEG audio frame header");
2018-03-07 01:17:50 +01:00
if (!m_istream) {
2015-04-22 19:22:01 +02:00
throw NoDataFoundException();
}
// get size
m_istream->seekg(-128, ios_base::end);
2018-03-07 01:17:50 +01:00
if (m_reader.readUInt24BE() == 0x544147) {
2019-03-13 19:06:42 +01:00
m_size = static_cast<std::uint64_t>(m_istream->tellg()) - 3u - m_startOffset;
2015-04-22 19:22:01 +02:00
} else {
2019-03-13 19:06:42 +01:00
m_size = static_cast<std::uint64_t>(m_istream->tellg()) + 125u - m_startOffset;
2015-04-22 19:22:01 +02:00
}
m_istream->seekg(static_cast<streamoff>(m_startOffset), ios_base::beg);
2015-04-22 19:22:01 +02:00
// parse frame header
m_frames.emplace_back();
MpegAudioFrame &frame = m_frames.back();
2018-07-28 14:56:00 +02:00
frame.parseHeader(m_reader, diag);
addInfo(frame, *this);
2018-03-07 01:17:50 +01:00
if (frame.isXingBytesfieldPresent()) {
2019-03-13 19:06:42 +01:00
std::uint32_t xingSize = frame.xingBytesfield();
2018-03-07 01:17:50 +01:00
if (m_size && xingSize != m_size) {
diag.emplace_back(DiagLevel::Warning,
"Real length of MPEG audio frames is not equal with value provided by Xing header. The Xing header value will be used.", context);
2015-04-22 19:22:01 +02:00
m_size = xingSize;
}
}
2019-05-04 21:03:09 +02:00
m_bitrate = frame.isXingFramefieldPresent() ? ((static_cast<double>(m_size) * 8.0)
/ (static_cast<double>(frame.xingFrameCount() * frame.sampleCount()) / static_cast<double>(frame.samplingFrequency())) / 1024.0)
: frame.bitrate();
2019-03-13 19:06:42 +01:00
m_duration = TimeSpan::fromSeconds(static_cast<double>(m_size) / (m_bytesPerSecond = static_cast<std::uint32_t>(m_bitrate * 125)));
2015-04-22 19:22:01 +02:00
}
2018-03-07 01:17:50 +01:00
} // namespace TagParser