Fix the wavedecoder a little.

Handle RIFX and corrupted files better.  Update the autotest so that it
is run properly (and copies files properly in shadow build).  Fix the
gendata script to properly create testdata.

Change-Id: I47b705507bebaef54df2835ec767c6b220c64678
Reviewed-on: http://codereview.qt-project.org/6380
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: derick hawcroft <derick.hawcroft@nokia.com>
This commit is contained in:
Michael Goddard
2011-10-11 13:50:50 +10:00
committed by Qt by Nokia
parent e0f306cff9
commit 8943111428
18 changed files with 131 additions and 56 deletions

View File

@@ -50,9 +50,10 @@ QWaveDecoder::QWaveDecoder(QIODevice *s, QObject *parent):
QIODevice(parent),
haveFormat(false),
dataSize(0),
remaining(0),
source(s),
state(QWaveDecoder::InitialState)
state(QWaveDecoder::InitialState),
junkToSkip(0),
bigEndian(false)
{
open(QIODevice::ReadOnly | QIODevice::Unbuffered);
@@ -106,7 +107,15 @@ qint64 QWaveDecoder::writeData(const char *data, qint64 len)
void QWaveDecoder::handleData()
{
bool valid = true;
// As a special "state", if we have junk to skip, we do
if (junkToSkip > 0) {
discardBytes(junkToSkip); // this also updates junkToSkip
// If we couldn't skip all the junk, return
if (junkToSkip > 0)
return;
}
if (state == QWaveDecoder::InitialState) {
if (source->bytesAvailable() < qint64(sizeof(RIFFHeader)))
return;
@@ -114,7 +123,8 @@ void QWaveDecoder::handleData()
RIFFHeader riff;
source->read(reinterpret_cast<char *>(&riff), sizeof(RIFFHeader));
if (qstrncmp(riff.descriptor.id, "RIFF", 4) != 0 ||
// RIFF = little endian RIFF, RIFX = big endian RIFF
if (((qstrncmp(riff.descriptor.id, "RIFF", 4) != 0) && (qstrncmp(riff.descriptor.id, "RIFX", 4) != 0)) ||
qstrncmp(riff.type, "WAVE", 4) != 0) {
source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
emit invalidFormat();
@@ -122,13 +132,17 @@ void QWaveDecoder::handleData()
return;
} else {
state = QWaveDecoder::WaitingForFormatState;
if (qstrncmp(riff.descriptor.id, "RIFX", 4) == 0)
bigEndian = true;
else
bigEndian = false;
}
}
if (state == QWaveDecoder::WaitingForFormatState) {
if (valid = findChunk("fmt ")) {
if (findChunk("fmt ")) {
chunk descriptor;
source->peek(reinterpret_cast<char *>(&descriptor), sizeof(chunk));
peekChunk(&descriptor);
if (source->bytesAvailable() < qint64(descriptor.size + sizeof(chunk)))
return;
@@ -138,20 +152,38 @@ void QWaveDecoder::handleData()
if (descriptor.size > sizeof(WAVEHeader))
discardBytes(descriptor.size - sizeof(WAVEHeader));
// Swizzle this
if (bigEndian) {
wave.audioFormat = qFromBigEndian<quint16>(wave.audioFormat);
}
if (wave.audioFormat != 0 && wave.audioFormat != 1) {
// 32bit wave files have format == 0xFFFE (WAVE_FORMAT_EXTENSIBLE).
// but don't support them at the moment.
source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
emit invalidFormat();
return;
} else {
int bps = qFromLittleEndian<quint16>(wave.bitsPerSample);
format.setCodec(QLatin1String("audio/pcm"));
format.setSampleType(bps == 8 ? QAudioFormat::UnSignedInt : QAudioFormat::SignedInt);
format.setByteOrder(QAudioFormat::LittleEndian);
format.setFrequency(qFromLittleEndian<quint32>(wave.sampleRate));
format.setSampleSize(bps);
format.setChannels(qFromLittleEndian<quint16>(wave.numChannels));
if (bigEndian) {
int bps = qFromBigEndian<quint16>(wave.bitsPerSample);
format.setSampleType(bps == 8 ? QAudioFormat::UnSignedInt : QAudioFormat::SignedInt);
format.setByteOrder(QAudioFormat::BigEndian);
format.setFrequency(qFromBigEndian<quint32>(wave.sampleRate));
format.setSampleSize(bps);
format.setChannels(qFromBigEndian<quint16>(wave.numChannels));
} else {
int bps = qFromLittleEndian<quint16>(wave.bitsPerSample);
format.setSampleType(bps == 8 ? QAudioFormat::UnSignedInt : QAudioFormat::SignedInt);
format.setByteOrder(QAudioFormat::LittleEndian);
format.setFrequency(qFromLittleEndian<quint32>(wave.sampleRate));
format.setSampleSize(bps);
format.setChannels(qFromLittleEndian<quint16>(wave.numChannels));
}
state = QWaveDecoder::WaitingForDataState;
}
@@ -159,11 +191,14 @@ void QWaveDecoder::handleData()
}
if (state == QWaveDecoder::WaitingForDataState) {
if (valid = findChunk("data")) {
if (findChunk("data")) {
source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
chunk descriptor;
source->read(reinterpret_cast<char *>(&descriptor), sizeof(chunk));
if (bigEndian)
descriptor.size = qFromBigEndian<quint32>(descriptor.size);
dataSize = descriptor.size;
haveFormat = true;
@@ -174,22 +209,24 @@ void QWaveDecoder::handleData()
}
}
if (source->atEnd() || !valid) {
if (source->atEnd()) {
source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
emit invalidFormat();
return;
}
}
bool QWaveDecoder::enoughDataAvailable()
{
if (source->bytesAvailable() < qint64(sizeof(chunk)))
chunk descriptor;
if (!peekChunk(&descriptor))
return false;
chunk descriptor;
source->peek(reinterpret_cast<char *>(&descriptor), sizeof(chunk));
// This is only called for the RIFF/RIFX header, before bigEndian is set,
// so we have to manually swizzle
if (qstrncmp(descriptor.id, "RIFX", 4) == 0)
descriptor.size = qFromBigEndian<quint32>(descriptor.size);
if (source->bytesAvailable() < qint64(sizeof(chunk) + descriptor.size))
return false;
@@ -199,19 +236,28 @@ bool QWaveDecoder::enoughDataAvailable()
bool QWaveDecoder::findChunk(const char *chunkId)
{
if (source->bytesAvailable() < qint64(sizeof(chunk)))
return false;
chunk descriptor;
source->peek(reinterpret_cast<char *>(&descriptor), sizeof(chunk));
if (!peekChunk(&descriptor))
return false;
if (qstrncmp(descriptor.id, chunkId, 4) == 0)
return true;
while (source->bytesAvailable() >= qint64(sizeof(chunk) + descriptor.size)) {
discardBytes(sizeof(chunk) + descriptor.size);
// It's possible that bytes->available() is less than the chunk size
// if it's corrupt.
junkToSkip = qint64(sizeof(chunk) + descriptor.size);
while (source->bytesAvailable() > 0) {
// Skip the current amount
if (junkToSkip > 0)
discardBytes(junkToSkip);
source->peek(reinterpret_cast<char *>(&descriptor), sizeof(chunk));
// If we still have stuff left, just exit and try again later
// since we can't call peekChunk
if (junkToSkip > 0)
return false;
if (!peekChunk(&descriptor))
return false;
if (qstrncmp(descriptor.id, chunkId, 4) == 0)
return true;
@@ -220,12 +266,35 @@ bool QWaveDecoder::findChunk(const char *chunkId)
return false;
}
// Handles endianness
bool QWaveDecoder::peekChunk(chunk *pChunk)
{
if (source->bytesAvailable() < qint64(sizeof(chunk)))
return false;
source->peek(reinterpret_cast<char *>(pChunk), sizeof(chunk));
if (bigEndian)
pChunk->size = qFromBigEndian<quint32>(pChunk->size);
return true;
}
void QWaveDecoder::discardBytes(qint64 numBytes)
{
if (source->isSequential())
source->read(numBytes);
else
// Discards a number of bytes
// If the iodevice doesn't have this many bytes in it,
// remember how much more junk we have to skip.
if (source->isSequential()) {
QByteArray r = source->read(numBytes); // uggh, wasted memory
if (r.size() < numBytes)
junkToSkip = numBytes - r.size();
else
junkToSkip = 0;
} else {
quint64 origPos = source->pos();
source->seek(source->pos() + numBytes);
junkToSkip = origPos + numBytes - source->pos();
}
}
QT_END_NAMESPACE

View File

@@ -106,6 +106,8 @@ private:
char id[4];
quint32 size;
};
bool peekChunk(chunk* pChunk);
struct RIFFHeader
{
chunk descriptor;
@@ -124,10 +126,11 @@ private:
bool haveFormat;
qint64 dataSize;
qint64 remaining;
QAudioFormat format;
QIODevice *source;
State state;
quint32 junkToSkip;
bool bigEndian;
};
QT_END_NAMESPACE

View File

@@ -27,6 +27,7 @@ SUBDIRS += \
qmediaobject \
qcamera \
qcamerabackend \
qwavedecoder
# These is disabled until intent is clearer
# qvideodevicecontrol \

View File

@@ -58,9 +58,9 @@ for channel in 1 2; do
for samplebits in 8 16 32; do
for samplerate in 44100 8000; do
if [ $samplebits -ne 8 ]; then
sox --endian "${endian}" -c ${channel} -b ${samplebits} -r ${samplerate} -n isawav_${channel}_${samplebits}_${samplerate}_${endian_extn}.wav synth 0.25 sine 300-3300
sox -n --endian "${endian}" -c ${channel} -b ${samplebits} -r ${samplerate} isawav_${channel}_${samplebits}_${samplerate}_${endian_extn}.wav synth 0.25 sine 300-3300
else
sox -c ${channel} -b ${samplebits} -r ${samplerate} -n isawav_${channel}_${samplebits}_${samplerate}.wav synth 0.25 sine 300-3300
sox -n -c ${channel} -b ${samplebits} -r ${samplerate} isawav_${channel}_${samplebits}_${samplerate}.wav synth 0.25 sine 300-3300
fi
done
done

View File

@@ -6,8 +6,8 @@ SOURCES += tst_qwavedecoder.cpp \
QT += multimedia-private testlib
CONFIG += no_private_qt_headers_warning testcase
data.path = .
data.sources = data
data.files = data
data.path = $${OUT_PWD}
INSTALLS += data

View File

@@ -44,18 +44,18 @@
#include <QtTest/QtTest>
#include <private/qwavedecoder_p.h>
#ifndef QTRY_VERIFY
#define QTRY_VERIFY(__expr) \
#ifndef QTRY_COMPARE
#define QTRY_COMPARE(__expr, __expected) \
do { \
const int __step = 50; \
const int __timeout = 10000; \
if (!(__expr)) { \
QTest::qWait(0); \
} \
for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \
for (int __i = 0; __i < __timeout && !((__expr) == (__expected)); __i+=__step) { \
QTest::qWait(__step); \
} \
QVERIFY(__expr); \
QCOMPARE(__expr, __expected); \
} while (0)
#endif
@@ -131,10 +131,11 @@ void tst_QWaveDecoder::integrity_data()
QTest::newRow("File isawav_2_16_8000_be.wav") << QString("isawav_2_16_8000_be.wav") << tst_QWaveDecoder::None << 2 << 16 << 8000 << QAudioFormat::BigEndian;
QTest::newRow("File isawav_2_16_44100_be.wav") << QString("isawav_2_16_44100_be.wav") << tst_QWaveDecoder::None << 2 << 16 << 44100 << QAudioFormat::BigEndian;
QTest::newRow("File isawav_1_32_8000_le.wav") << QString("isawav_1_32_8000_le.wav") << tst_QWaveDecoder::None << 1 << 32 << 8000 << QAudioFormat::LittleEndian;
QTest::newRow("File isawav_1_32_44100_le.wav") << QString("isawav_1_32_44100_le.wav") << tst_QWaveDecoder::None << 1 << 32 << 44100 << QAudioFormat::LittleEndian;
QTest::newRow("File isawav_2_32_8000_be.wav") << QString("isawav_2_32_8000_be.wav") << tst_QWaveDecoder::None << 2 << 32 << 8000 << QAudioFormat::BigEndian;
QTest::newRow("File isawav_2_32_44100_be.wav") << QString("isawav_2_32_44100_be.wav") << tst_QWaveDecoder::None << 2 << 32 << 44100 << QAudioFormat::BigEndian;
// 32 bit waves are not supported
QTest::newRow("File isawav_1_32_8000_le.wav") << QString("isawav_1_32_8000_le.wav") << tst_QWaveDecoder::FormatDescriptor << 1 << 32 << 8000 << QAudioFormat::LittleEndian;
QTest::newRow("File isawav_1_32_44100_le.wav") << QString("isawav_1_32_44100_le.wav") << tst_QWaveDecoder::FormatDescriptor << 1 << 32 << 44100 << QAudioFormat::LittleEndian;
QTest::newRow("File isawav_2_32_8000_be.wav") << QString("isawav_2_32_8000_be.wav") << tst_QWaveDecoder::FormatDescriptor << 2 << 32 << 8000 << QAudioFormat::BigEndian;
QTest::newRow("File isawav_2_32_44100_be.wav") << QString("isawav_2_32_44100_be.wav") << tst_QWaveDecoder::FormatDescriptor << 2 << 32 << 44100 << QAudioFormat::BigEndian;
}
void tst_QWaveDecoder::integrity()
@@ -146,6 +147,7 @@ void tst_QWaveDecoder::integrity()
QFETCH(int, samplerate);
QFETCH(QAudioFormat::Endian, byteorder);
QFile stream;
stream.setFileName(QString("data/") + file);
stream.open(QIODevice::ReadOnly);
@@ -157,28 +159,28 @@ void tst_QWaveDecoder::integrity()
QSignalSpy invalidFormatSpy(&waveDecoder, SIGNAL(invalidFormat()));
if (corruption == NotAWav) {
QTRY_VERIFY(validFormatSpy.count() == 0);
QTRY_VERIFY(invalidFormatSpy.count() == 0);
QTRY_COMPARE(validFormatSpy.count(), 0);
QTRY_COMPARE(invalidFormatSpy.count(), 0);
} else if (corruption == NoSampleData) {
QTRY_VERIFY(validFormatSpy.count() == 1);
QTRY_VERIFY(invalidFormatSpy.count() == 0);
QTRY_COMPARE(validFormatSpy.count(), 1);
QTRY_COMPARE(invalidFormatSpy.count(), 0);
QVERIFY(waveDecoder.audioFormat().isValid());
QVERIFY(waveDecoder.size() == 0);
QVERIFY(waveDecoder.duration() == 0);
} else if (corruption == FormatDescriptor) {
QTRY_VERIFY(invalidFormatSpy.count() == 1);
QTRY_VERIFY(validFormatSpy.count() == 0);
QTRY_COMPARE(invalidFormatSpy.count(), 1);
QTRY_COMPARE(validFormatSpy.count(), 0);
} else if (corruption == FormatString) {
QTRY_VERIFY(invalidFormatSpy.count() == 1);
QTRY_VERIFY(validFormatSpy.count() == 0);
QTRY_COMPARE(invalidFormatSpy.count(), 1);
QTRY_COMPARE(validFormatSpy.count(), 0);
QVERIFY(!waveDecoder.audioFormat().isValid());
} else if (corruption == DataDescriptor) {
QTRY_VERIFY(validFormatSpy.count() == 1);
QTRY_VERIFY(invalidFormatSpy.count() == 0);
QTRY_COMPARE(validFormatSpy.count(), 0);
QTRY_COMPARE(invalidFormatSpy.count(), 1);
QVERIFY(waveDecoder.size() == 0);
} else if (corruption == None) {
QTRY_VERIFY(validFormatSpy.count() == 1);
QTRY_VERIFY(invalidFormatSpy.count() == 0);
QTRY_COMPARE(validFormatSpy.count(), 1);
QTRY_COMPARE(invalidFormatSpy.count(), 0);
QVERIFY(waveDecoder.audioFormat().isValid());
QVERIFY(waveDecoder.size() > 0);
QVERIFY(waveDecoder.duration() == 250);
@@ -206,7 +208,7 @@ void tst_QWaveDecoder::readAllAtOnce()
QWaveDecoder waveDecoder(&stream);
QSignalSpy validFormatSpy(&waveDecoder, SIGNAL(formatKnown()));
QTRY_VERIFY(validFormatSpy.count() == 1);
QTRY_COMPARE(validFormatSpy.count(), 1);
QVERIFY(waveDecoder.size() > 0);
QByteArray buffer;
@@ -232,7 +234,7 @@ void tst_QWaveDecoder::readPerByte()
QWaveDecoder waveDecoder(&stream);
QSignalSpy validFormatSpy(&waveDecoder, SIGNAL(formatKnown()));
QTRY_VERIFY(validFormatSpy.count() == 1);
QTRY_COMPARE(validFormatSpy.count(), 1);
QVERIFY(waveDecoder.size() > 0);
qint64 readSize = 0;