2015-09-06 19:57:33 +02:00
|
|
|
#include "./waveaudiostream.h"
|
2015-04-22 19:22:01 +02:00
|
|
|
|
2015-09-22 01:37:23 +02:00
|
|
|
#include "../mpegaudio/mpegaudioframestream.h"
|
|
|
|
|
2015-09-06 19:57:33 +02:00
|
|
|
#include "../exceptions.h"
|
|
|
|
#include "../mediaformat.h"
|
2015-04-22 19:22:01 +02:00
|
|
|
|
2015-09-23 00:03:05 +02:00
|
|
|
#include <c++utilities/conversion/stringconversion.h>
|
2018-03-07 01:17:50 +01:00
|
|
|
#include <c++utilities/io/binaryreader.h>
|
2015-06-12 02:35:50 +02:00
|
|
|
|
2015-04-22 19:22:01 +02:00
|
|
|
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
|
|
|
|
2015-06-12 02:35:50 +02:00
|
|
|
/*!
|
2018-06-03 20:38:32 +02:00
|
|
|
* \class TagParser::WaveFormatHeader
|
2015-06-12 02:35:50 +02:00
|
|
|
* \brief The WaveFormatHeader class parses the WAVEFORMATEX structure defined by MS.
|
|
|
|
*/
|
|
|
|
|
2019-01-01 23:39:40 +01:00
|
|
|
/*!
|
|
|
|
* \brief Parses the WAVE "fmt " header segment using the specified \a reader.
|
|
|
|
* \returns Returns the detected media format and the number of bytes read.
|
|
|
|
*/
|
2019-06-16 18:06:31 +02:00
|
|
|
std::uint64_t WaveFormatHeader::parse(CppUtilities::BinaryReader &reader, std::uint64_t maxSize, Diagnostics &diag)
|
2019-01-01 23:39:40 +01:00
|
|
|
{
|
2019-06-16 18:06:31 +02:00
|
|
|
uint64_t bytesRead = 0;
|
2019-01-01 23:39:40 +01:00
|
|
|
if (maxSize < 16) {
|
|
|
|
diag.emplace_back(DiagLevel::Warning, "\"fmt \" segment is truncated.", "parsing WAVE format header");
|
2019-06-16 18:06:31 +02:00
|
|
|
return bytesRead;
|
2019-01-01 23:39:40 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 02:35:50 +02:00
|
|
|
formatTag = reader.readUInt16LE();
|
|
|
|
channelCount = reader.readUInt16LE();
|
|
|
|
sampleRate = reader.readUInt32LE();
|
|
|
|
bytesPerSecond = reader.readUInt32LE();
|
|
|
|
chunkSize = reader.readUInt16LE();
|
|
|
|
bitsPerSample = reader.readUInt16LE();
|
2019-06-16 18:06:31 +02:00
|
|
|
bytesRead = 16;
|
2019-01-01 23:39:40 +01:00
|
|
|
|
|
|
|
// read extended header unless format is PCM
|
2019-06-16 18:06:31 +02:00
|
|
|
if (formatTag == 0x0001u || formatTag == 0x0003u) {
|
|
|
|
return bytesRead;
|
2019-01-01 23:39:40 +01:00
|
|
|
}
|
|
|
|
if ((maxSize -= 16) < 2) {
|
|
|
|
diag.emplace_back(DiagLevel::Warning, "\"fmt \" segment is truncated (extended header missing).", "parsing WAVE format header");
|
2019-06-16 18:06:31 +02:00
|
|
|
return bytesRead;
|
2019-01-01 23:39:40 +01:00
|
|
|
}
|
|
|
|
const auto extensionSize = reader.readUInt16LE();
|
2019-06-16 18:06:31 +02:00
|
|
|
bytesRead += 2;
|
2019-01-01 23:39:40 +01:00
|
|
|
if ((maxSize -= 2) < 2) {
|
|
|
|
diag.emplace_back(DiagLevel::Warning, "\"fmt \" segment is truncated (extended header truncated).", "parsing WAVE format header");
|
2019-06-16 18:06:31 +02:00
|
|
|
return bytesRead;
|
2019-01-01 23:39:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip extended header unless format is "WAVE_FORMAT_EXTENSIBLE"
|
|
|
|
if (formatTag != 65534) {
|
|
|
|
reader.stream()->seekg(extensionSize, ios_base::cur);
|
2019-06-16 18:06:31 +02:00
|
|
|
bytesRead += extensionSize;
|
|
|
|
return bytesRead;
|
2019-01-01 23:39:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// read extended header for "WAVE_FORMAT_EXTENSIBLE"
|
|
|
|
if (extensionSize != 22) {
|
|
|
|
diag.emplace_back(DiagLevel::Warning, "\"fmt \" extended header has unexptected size.", "parsing WAVE format header");
|
2019-06-16 18:06:31 +02:00
|
|
|
return bytesRead;
|
2019-01-01 23:39:40 +01:00
|
|
|
}
|
|
|
|
bitsPerSample = reader.readUInt16LE();
|
2019-06-16 18:06:31 +02:00
|
|
|
channelMask = reader.readUInt32LE();
|
|
|
|
guid1 = reader.readUInt64BE();
|
|
|
|
guid2 = reader.readUInt64BE();
|
|
|
|
return bytesRead += 22;
|
2015-06-12 02:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the media format denoted by the format tag.
|
|
|
|
*/
|
|
|
|
MediaFormat WaveFormatHeader::format() const
|
|
|
|
{
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (formatTag) {
|
|
|
|
case 0x0001u:
|
2019-01-01 23:39:40 +01:00
|
|
|
return MediaFormat(GeneralMediaFormat::Pcm, SubFormats::PcmIntLe);
|
|
|
|
case 0x0003u:
|
|
|
|
return MediaFormat(GeneralMediaFormat::Pcm, SubFormats::PcmFloatIeee);
|
2018-03-07 01:17:50 +01:00
|
|
|
case 0x0050u:
|
|
|
|
return MediaFormat(GeneralMediaFormat::Mpeg1Audio, SubFormats::Mpeg1Layer2);
|
|
|
|
case 0x0055u:
|
|
|
|
return MediaFormat(GeneralMediaFormat::Mpeg1Audio, SubFormats::Mpeg1Layer3);
|
|
|
|
default:
|
2019-06-16 18:06:31 +02:00
|
|
|
switch (guid2) {
|
|
|
|
case 0x000800000aa00389b71:
|
|
|
|
switch (guid1) {
|
|
|
|
case 0x0100000000001000ul:
|
|
|
|
return MediaFormat(GeneralMediaFormat::Pcm, SubFormats::PcmIntLe);
|
|
|
|
case 0x0300000000001000ul:
|
|
|
|
return MediaFormat(GeneralMediaFormat::Pcm, SubFormats::PcmFloatIeee);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
return GeneralMediaFormat::Unknown;
|
2015-06-12 02:35:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:22:01 +02:00
|
|
|
/*!
|
2018-06-03 20:38:32 +02:00
|
|
|
* \class TagParser::WaveAudioStream
|
|
|
|
* \brief Implementation of TagParser::AbstractTrack for the
|
2015-04-22 19:22:01 +02:00
|
|
|
* RIFF WAVE container format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a new track for the \a stream at the specified \a startOffset.
|
|
|
|
*/
|
2019-03-13 19:06:42 +01:00
|
|
|
WaveAudioStream::WaveAudioStream(iostream &stream, std::uint64_t startOffset)
|
2018-03-07 01:17:50 +01:00
|
|
|
: AbstractTrack(stream, startOffset)
|
|
|
|
, m_dataOffset(0)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
2015-06-07 00:18:28 +02:00
|
|
|
m_mediaType = MediaType::Audio;
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Destroys the track.
|
|
|
|
*/
|
|
|
|
WaveAudioStream::~WaveAudioStream()
|
2018-03-07 01:17:50 +01:00
|
|
|
{
|
|
|
|
}
|
2015-04-22 19:22:01 +02:00
|
|
|
|
|
|
|
TrackType WaveAudioStream::type() const
|
|
|
|
{
|
|
|
|
return TrackType::WaveAudioStream;
|
|
|
|
}
|
|
|
|
|
2015-06-12 02:35:50 +02:00
|
|
|
/*!
|
|
|
|
* \brief Adds the information from the specified \a waveHeader to the specified \a track.
|
|
|
|
*/
|
|
|
|
void WaveAudioStream::addInfo(const WaveFormatHeader &waveHeader, AbstractTrack &track)
|
|
|
|
{
|
2019-06-16 18:06:31 +02:00
|
|
|
track.m_format += waveHeader.format();
|
2019-06-10 22:49:11 +02:00
|
|
|
track.m_formatId = numberToString(waveHeader.formatTag);
|
2015-06-12 02:35:50 +02:00
|
|
|
track.m_channelCount = waveHeader.channelCount;
|
2015-08-13 03:23:28 +02:00
|
|
|
track.m_samplingFrequency = waveHeader.sampleRate;
|
2015-06-12 02:35:50 +02:00
|
|
|
track.m_bytesPerSecond = waveHeader.bytesPerSecond;
|
|
|
|
track.m_chunkSize = waveHeader.chunkSize;
|
|
|
|
track.m_bitsPerSample = waveHeader.bitsPerSample;
|
|
|
|
track.m_bitrate = waveHeader.bitrate();
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:49:29 +01:00
|
|
|
void WaveAudioStream::internalParseHeader(Diagnostics &diag)
|
2015-04-22 19:22:01 +02:00
|
|
|
{
|
2015-09-22 01:37:23 +02:00
|
|
|
const string context("parsing RIFF/WAVE header");
|
2018-03-07 01:17:50 +01:00
|
|
|
if (!m_istream) {
|
2015-04-22 19:22:01 +02:00
|
|
|
throw NoDataFoundException();
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (m_reader.readUInt32BE() != 0x52494646u) {
|
2018-03-07 01:11:42 +01:00
|
|
|
throw NoDataFoundException();
|
|
|
|
}
|
2018-06-02 22:56:08 +02:00
|
|
|
m_istream->seekg(static_cast<streamoff>(m_startOffset + 8));
|
2018-03-07 01:17:50 +01:00
|
|
|
if (m_reader.readUInt32BE() != 0x57415645u) {
|
2018-03-07 01:11:42 +01:00
|
|
|
throw NoDataFoundException();
|
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
while (!m_dataOffset) {
|
2019-01-01 23:39:40 +01:00
|
|
|
const auto segmentId = m_reader.readUInt32BE();
|
|
|
|
auto restHeaderLen = m_reader.readUInt32LE();
|
2018-03-07 01:17:50 +01:00
|
|
|
switch (segmentId) {
|
2019-01-01 23:39:40 +01:00
|
|
|
case 0x666D7420u: { // format segment
|
|
|
|
WaveFormatHeader waveHeader;
|
2019-06-16 18:06:31 +02:00
|
|
|
const auto bytesRead = waveHeader.parse(m_reader, restHeaderLen, diag);
|
2019-01-01 23:39:40 +01:00
|
|
|
addInfo(waveHeader, *this);
|
|
|
|
restHeaderLen -= bytesRead;
|
|
|
|
} break;
|
|
|
|
case 0x64617461u: // data segment
|
2019-03-13 19:06:42 +01:00
|
|
|
m_dataOffset = static_cast<std::uint64_t>(m_istream->tellg());
|
2018-03-07 01:11:42 +01:00
|
|
|
m_size = restHeaderLen;
|
|
|
|
m_sampleCount = m_size / m_chunkSize;
|
|
|
|
m_duration = TimeSpan::fromSeconds(static_cast<double>(m_sampleCount) / static_cast<double>(m_samplingFrequency));
|
|
|
|
break;
|
2018-03-07 01:17:50 +01:00
|
|
|
default:;
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-03-07 01:11:42 +01:00
|
|
|
m_istream->seekg(restHeaderLen, ios_base::cur);
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
2018-03-07 01:17:50 +01:00
|
|
|
if (m_format.general != GeneralMediaFormat::Mpeg1Audio || !m_dataOffset) {
|
2018-03-07 01:11:42 +01:00
|
|
|
return;
|
2015-09-22 01:37:23 +02:00
|
|
|
}
|
2018-06-02 22:56:08 +02:00
|
|
|
m_istream->seekg(static_cast<streamoff>(m_dataOffset));
|
2018-03-07 01:11:42 +01:00
|
|
|
MpegAudioFrame frame;
|
2018-07-28 14:56:00 +02:00
|
|
|
frame.parseHeader(m_reader, diag);
|
2018-03-07 01:11:42 +01:00
|
|
|
MpegAudioFrameStream::addInfo(frame, *this);
|
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_bytesPerSecond = static_cast<std::uint32_t>(m_bitrate * 125);
|
2018-03-07 01:11:42 +01:00
|
|
|
m_duration = TimeSpan::fromSeconds(static_cast<double>(m_size) / (m_bitrate * 128.0));
|
2015-04-22 19:22:01 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 01:17:50 +01:00
|
|
|
} // namespace TagParser
|