fixed RIFF/WAVE parser

This commit is contained in:
Martchus 2015-09-22 01:37:23 +02:00
parent 8cc75e137a
commit ac396bb26f
1 changed files with 44 additions and 20 deletions

View File

@ -1,11 +1,14 @@
#include "./waveaudiostream.h"
#include "../mpegaudio/mpegaudioframestream.h"
#include "../exceptions.h"
#include "../mediaformat.h"
#include <c++utilities/io/binaryreader.h>
using namespace std;
using namespace ChronoUtilities;
namespace Media {
@ -27,7 +30,7 @@ WaveFormatHeader::WaveFormatHeader() :
{}
/*!
* \brief Parses the WAVE header using the specified \a reader.
* \brief Parses the WAVE "fmt " header segment using the specified \a reader.
* \remarks Reads 16 bytes from the associated stream.
*/
void WaveFormatHeader::parse(IoUtilities::BinaryReader &reader)
@ -63,7 +66,8 @@ MediaFormat WaveFormatHeader::format() const
* \brief Constructs a new track for the \a stream at the specified \a startOffset.
*/
WaveAudioStream::WaveAudioStream(iostream &stream, uint64 startOffset) :
AbstractTrack(stream, startOffset)
AbstractTrack(stream, startOffset),
m_dataOffset(0)
{
m_mediaType = MediaType::Audio;
}
@ -95,28 +99,37 @@ void WaveAudioStream::addInfo(const WaveFormatHeader &waveHeader, AbstractTrack
void WaveAudioStream::internalParseHeader()
{
const string context("parsing RIFF/WAVE header");
if(!m_istream) {
throw NoDataFoundException();
}
if(m_reader.readUInt32BE() == 0x52494646u) {
m_istream->seekg(4, ios_base::cur);
if(m_reader.readUInt32BE() == 0x57415645u && m_reader.readUInt32BE() == 0x666D7420u) {
uint32 restHeaderLen = m_reader.readUInt32LE();
m_dataOffset = static_cast<uint64>(m_istream->tellg()) + static_cast<uint64>(restHeaderLen);
if(restHeaderLen >= 16u) {
WaveFormatHeader waveHeader;
waveHeader.parse(m_reader);
addInfo(waveHeader, *this);
}
if(restHeaderLen > 16u) {
m_istream->seekg(m_dataOffset, ios_base::beg);
}
if(m_reader.readUInt32BE() == 0x64617461u) {
m_size = m_reader.readUInt32LE();
m_sampleCount = m_size / m_chunkSize;
m_duration = ChronoUtilities::TimeSpan::fromSeconds(static_cast<double>(m_sampleCount) / static_cast<double>(m_samplingFrequency));
} else {
throw NoDataFoundException();
m_istream->seekg(m_startOffset + 8);
if(m_reader.readUInt32BE() == 0x57415645u) {
while(!m_dataOffset) {
uint32 segmentId = m_reader.readUInt32BE();
uint32 restHeaderLen = m_reader.readUInt32LE();
switch(segmentId) {
case 0x666D7420u:
if(restHeaderLen >= 16u) {
WaveFormatHeader waveHeader;
waveHeader.parse(m_reader);
addInfo(waveHeader, *this);
restHeaderLen -= 16u;
} else {
addNotification(NotificationType::Warning, "\"fmt \" segment is truncated.", context);
}
break;
case 0x64617461u:
m_dataOffset = m_istream->tellg();
m_size = restHeaderLen;
m_sampleCount = m_size / m_chunkSize;
m_duration = TimeSpan::fromSeconds(static_cast<double>(m_sampleCount) / static_cast<double>(m_samplingFrequency));
break;
default:
;
}
m_istream->seekg(restHeaderLen, ios_base::cur);
}
} else {
throw NoDataFoundException();
@ -124,6 +137,17 @@ void WaveAudioStream::internalParseHeader()
} else {
throw NoDataFoundException();
}
if(m_format.general == GeneralMediaFormat::Mpeg1Audio && m_dataOffset) {
m_istream->seekg(m_dataOffset);
MpegAudioFrame frame;
frame.parseHeader(m_reader);
MpegAudioFrameStream::addInfo(frame, *this);
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();
m_bytesPerSecond = m_bitrate * 125;
m_duration = TimeSpan::fromSeconds(static_cast<double>(m_size) / (m_bitrate * 128.0));
}
}
}