diff options
author | Michael Goddard <michael.goddard@nokia.com> | 2011-10-11 13:50:50 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-10-11 08:59:48 +0200 |
commit | 8943111428929771774be73430c8a6d553b1ba21 (patch) | |
tree | 9932a845f756420cd99e7ab1a2e427a4fe18d656 | |
parent | e0f306cff947dc9779d43e225d47491d1a7b61fa (diff) |
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>
18 files changed, 131 insertions, 56 deletions
diff --git a/src/multimedia/effects/qwavedecoder_p.cpp b/src/multimedia/effects/qwavedecoder_p.cpp index e173aed..ad4ecf2 100644 --- a/src/multimedia/effects/qwavedecoder_p.cpp +++ b/src/multimedia/effects/qwavedecoder_p.cpp @@ -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 diff --git a/src/multimedia/effects/qwavedecoder_p.h b/src/multimedia/effects/qwavedecoder_p.h index 6c4c00c..0d26fec 100644 --- a/src/multimedia/effects/qwavedecoder_p.h +++ b/src/multimedia/effects/qwavedecoder_p.h @@ -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 diff --git a/tests/auto/multimedia.pro b/tests/auto/multimedia.pro index 6a6d08b..307a252 100644 --- a/tests/auto/multimedia.pro +++ b/tests/auto/multimedia.pro @@ -27,6 +27,7 @@ SUBDIRS += \ qmediaobject \ qcamera \ qcamerabackend \ + qwavedecoder # These is disabled until intent is clearer # qvideodevicecontrol \ diff --git a/tests/auto/qwavedecoder/data/gendata.sh b/tests/auto/qwavedecoder/data/gendata.sh index 9954ed4..0dd82ce 100755 --- a/tests/auto/qwavedecoder/data/gendata.sh +++ b/tests/auto/qwavedecoder/data/gendata.sh @@ -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 diff --git a/tests/auto/qwavedecoder/data/isawav_1_16_44100_le.wav b/tests/auto/qwavedecoder/data/isawav_1_16_44100_le.wav Binary files differindex d8373cd..88b1a83 100644 --- a/tests/auto/qwavedecoder/data/isawav_1_16_44100_le.wav +++ b/tests/auto/qwavedecoder/data/isawav_1_16_44100_le.wav diff --git a/tests/auto/qwavedecoder/data/isawav_1_16_8000_le.wav b/tests/auto/qwavedecoder/data/isawav_1_16_8000_le.wav Binary files differindex 6467b9a..83a4059 100644 --- a/tests/auto/qwavedecoder/data/isawav_1_16_8000_le.wav +++ b/tests/auto/qwavedecoder/data/isawav_1_16_8000_le.wav diff --git a/tests/auto/qwavedecoder/data/isawav_1_32_44100_le.wav b/tests/auto/qwavedecoder/data/isawav_1_32_44100_le.wav Binary files differindex 6d4efde..9c437b1 100644 --- a/tests/auto/qwavedecoder/data/isawav_1_32_44100_le.wav +++ b/tests/auto/qwavedecoder/data/isawav_1_32_44100_le.wav diff --git a/tests/auto/qwavedecoder/data/isawav_1_32_8000_le.wav b/tests/auto/qwavedecoder/data/isawav_1_32_8000_le.wav Binary files differindex 9963f63..f90a8bc 100644 --- a/tests/auto/qwavedecoder/data/isawav_1_32_8000_le.wav +++ b/tests/auto/qwavedecoder/data/isawav_1_32_8000_le.wav diff --git a/tests/auto/qwavedecoder/data/isawav_1_8_44100.wav b/tests/auto/qwavedecoder/data/isawav_1_8_44100.wav Binary files differindex 6386969..7d10829 100644 --- a/tests/auto/qwavedecoder/data/isawav_1_8_44100.wav +++ b/tests/auto/qwavedecoder/data/isawav_1_8_44100.wav diff --git a/tests/auto/qwavedecoder/data/isawav_1_8_8000.wav b/tests/auto/qwavedecoder/data/isawav_1_8_8000.wav Binary files differindex ac7b40f..76c08e8 100644 --- a/tests/auto/qwavedecoder/data/isawav_1_8_8000.wav +++ b/tests/auto/qwavedecoder/data/isawav_1_8_8000.wav diff --git a/tests/auto/qwavedecoder/data/isawav_2_16_44100_be.wav b/tests/auto/qwavedecoder/data/isawav_2_16_44100_be.wav Binary files differindex d3ffb04..ca0cd42 100644 --- a/tests/auto/qwavedecoder/data/isawav_2_16_44100_be.wav +++ b/tests/auto/qwavedecoder/data/isawav_2_16_44100_be.wav diff --git a/tests/auto/qwavedecoder/data/isawav_2_16_8000_be.wav b/tests/auto/qwavedecoder/data/isawav_2_16_8000_be.wav Binary files differindex 8638f19..3a68459 100644 --- a/tests/auto/qwavedecoder/data/isawav_2_16_8000_be.wav +++ b/tests/auto/qwavedecoder/data/isawav_2_16_8000_be.wav diff --git a/tests/auto/qwavedecoder/data/isawav_2_32_44100_be.wav b/tests/auto/qwavedecoder/data/isawav_2_32_44100_be.wav Binary files differindex edf6555..f1aaf29 100644 --- a/tests/auto/qwavedecoder/data/isawav_2_32_44100_be.wav +++ b/tests/auto/qwavedecoder/data/isawav_2_32_44100_be.wav diff --git a/tests/auto/qwavedecoder/data/isawav_2_32_8000_be.wav b/tests/auto/qwavedecoder/data/isawav_2_32_8000_be.wav Binary files differindex 642f10c..c10c208 100644 --- a/tests/auto/qwavedecoder/data/isawav_2_32_8000_be.wav +++ b/tests/auto/qwavedecoder/data/isawav_2_32_8000_be.wav diff --git a/tests/auto/qwavedecoder/data/isawav_2_8_44100.wav b/tests/auto/qwavedecoder/data/isawav_2_8_44100.wav Binary files differindex dcb0017..befd02b 100644 --- a/tests/auto/qwavedecoder/data/isawav_2_8_44100.wav +++ b/tests/auto/qwavedecoder/data/isawav_2_8_44100.wav diff --git a/tests/auto/qwavedecoder/data/isawav_2_8_8000.wav b/tests/auto/qwavedecoder/data/isawav_2_8_8000.wav Binary files differindex 901ee35..ce8b0d0 100644 --- a/tests/auto/qwavedecoder/data/isawav_2_8_8000.wav +++ b/tests/auto/qwavedecoder/data/isawav_2_8_8000.wav diff --git a/tests/auto/qwavedecoder/qwavedecoder.pro b/tests/auto/qwavedecoder/qwavedecoder.pro index 435aae9..4f0339a 100644 --- a/tests/auto/qwavedecoder/qwavedecoder.pro +++ b/tests/auto/qwavedecoder/qwavedecoder.pro @@ -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 diff --git a/tests/auto/qwavedecoder/tst_qwavedecoder.cpp b/tests/auto/qwavedecoder/tst_qwavedecoder.cpp index aeed403..6bf529f 100644 --- a/tests/auto/qwavedecoder/tst_qwavedecoder.cpp +++ b/tests/auto/qwavedecoder/tst_qwavedecoder.cpp @@ -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; |