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
|
|
|
|
2018-03-06 23:09:15 +01:00
|
|
|
namespace TagParser {
|
2015-04-22 19:22:01 +02:00
|
|
|
|
|
|
|
/*!
|
2018-06-03 20:38:32 +02:00
|
|
|
* \class TagParser::MpegAudioFrameStream
|
|
|
|
* \brief Implementation of TagParser::AbstractTrack MPEG audio streams.
|
2015-04-22 19:22:01 +02:00
|
|
|
*/
|
|
|
|
|
2015-06-10 01:28:22 +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();
|
2018-06-02 22:56:08 +02:00
|
|
|
track.m_format = MediaFormat(GeneralMediaFormat::Mpeg1Audio, static_cast<unsigned char>(frame.layer()));
|
2015-06-10 01:28:22 +02:00
|
|
|
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());
|
2015-08-13 03:23:28 +02:00
|
|
|
track.m_samplingFrequency = frame.samplingFrequency();
|
2015-06-10 01:28:22 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 17:49:29 +01:00
|
|
|
void MpegAudioFrameStream::internalParseHeader(Diagnostics &diag)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
2015-06-10 01:28:22 +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
|
|
|
}
|
2018-06-02 22:56:08 +02:00
|
|
|
m_istream->seekg(static_cast<streamoff>(m_startOffset), ios_base::beg);
|
2020-11-27 00:10:41 +01:00
|
|
|
// parse frames until the first valid, non-empty frame is reached
|
|
|
|
for (size_t invalidByteskipped = 0; m_frames.size() < 200 && invalidByteskipped <= 0x600u;) {
|
|
|
|
MpegAudioFrame &frame = invalidByteskipped > 0 ? m_frames.back() : m_frames.emplace_back();
|
|
|
|
try {
|
|
|
|
frame.parseHeader(m_reader, diag);
|
|
|
|
} catch (const InvalidDataException &e) {
|
|
|
|
if (++invalidByteskipped > 1) {
|
|
|
|
diag.pop_back();
|
|
|
|
}
|
|
|
|
m_istream->seekg(-3, ios_base::cur);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (invalidByteskipped > 1) {
|
|
|
|
diag.emplace_back(DiagLevel::Critical, argsToString("The next ", invalidByteskipped, " bytes are junk as well."), context);
|
|
|
|
}
|
|
|
|
if (!frame.size()) {
|
|
|
|
continue; // likely just junk, check further frames
|
|
|
|
}
|
|
|
|
invalidByteskipped = 0;
|
2020-11-14 22:58:40 +01:00
|
|
|
if (frame.isProtectedByCrc()) {
|
|
|
|
m_istream->seekg(2, ios_base::cur);
|
|
|
|
}
|
2020-11-27 00:10:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!m_frames.back().isValid()) {
|
|
|
|
return;
|
2020-11-14 22:58:40 +01:00
|
|
|
}
|
|
|
|
const MpegAudioFrame &frame = m_frames.back();
|
2015-06-10 01:28:22 +02:00
|
|
|
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,
|
2020-11-14 22:58:40 +01:00
|
|
|
"Real length of MPEG audio frames is not in accordance 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
|