Fix the wavedecoder a little.
authorMichael Goddard <michael.goddard@nokia.com>
Tue, 11 Oct 2011 03:50:50 +0000 (13:50 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 11 Oct 2011 06:59:48 +0000 (08:59 +0200)
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:
src/multimedia/effects/qwavedecoder_p.cpp
src/multimedia/effects/qwavedecoder_p.h
tests/auto/multimedia.pro
tests/auto/qwavedecoder/data/gendata.sh
tests/auto/qwavedecoder/data/isawav_1_16_44100_le.wav
tests/auto/qwavedecoder/data/isawav_1_16_8000_le.wav
tests/auto/qwavedecoder/data/isawav_1_32_44100_le.wav
tests/auto/qwavedecoder/data/isawav_1_32_8000_le.wav
tests/auto/qwavedecoder/data/isawav_1_8_44100.wav
tests/auto/qwavedecoder/data/isawav_1_8_8000.wav
tests/auto/qwavedecoder/data/isawav_2_16_44100_be.wav
tests/auto/qwavedecoder/data/isawav_2_16_8000_be.wav
tests/auto/qwavedecoder/data/isawav_2_32_44100_be.wav
tests/auto/qwavedecoder/data/isawav_2_32_8000_be.wav
tests/auto/qwavedecoder/data/isawav_2_8_44100.wav
tests/auto/qwavedecoder/data/isawav_2_8_8000.wav
tests/auto/qwavedecoder/qwavedecoder.pro
tests/auto/qwavedecoder/tst_qwavedecoder.cpp

index e173aed..ad4ecf2 100644 (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
index 6c4c00c..0d26fec 100644 (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
index 6a6d08b..307a252 100644 (file)
@@ -27,6 +27,7 @@ SUBDIRS += \
     qmediaobject \
     qcamera \
     qcamerabackend \
+    qwavedecoder
 
 # These is disabled until intent is clearer
 #    qvideodevicecontrol \
index 9954ed4..0dd82ce 100755 (executable)
@@ -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
index d8373cd..88b1a83 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_1_16_44100_le.wav and b/tests/auto/qwavedecoder/data/isawav_1_16_44100_le.wav differ
index 6467b9a..83a4059 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_1_16_8000_le.wav and b/tests/auto/qwavedecoder/data/isawav_1_16_8000_le.wav differ
index 6d4efde..9c437b1 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_1_32_44100_le.wav and b/tests/auto/qwavedecoder/data/isawav_1_32_44100_le.wav differ
index 9963f63..f90a8bc 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_1_32_8000_le.wav and b/tests/auto/qwavedecoder/data/isawav_1_32_8000_le.wav differ
index 6386969..7d10829 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_1_8_44100.wav and b/tests/auto/qwavedecoder/data/isawav_1_8_44100.wav differ
index ac7b40f..76c08e8 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_1_8_8000.wav and b/tests/auto/qwavedecoder/data/isawav_1_8_8000.wav differ
index d3ffb04..ca0cd42 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_2_16_44100_be.wav and b/tests/auto/qwavedecoder/data/isawav_2_16_44100_be.wav differ
index 8638f19..3a68459 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_2_16_8000_be.wav and b/tests/auto/qwavedecoder/data/isawav_2_16_8000_be.wav differ
index edf6555..f1aaf29 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_2_32_44100_be.wav and b/tests/auto/qwavedecoder/data/isawav_2_32_44100_be.wav differ
index 642f10c..c10c208 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_2_32_8000_be.wav and b/tests/auto/qwavedecoder/data/isawav_2_32_8000_be.wav differ
index dcb0017..befd02b 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_2_8_44100.wav and b/tests/auto/qwavedecoder/data/isawav_2_8_44100.wav differ
index 901ee35..ce8b0d0 100644 (file)
Binary files a/tests/auto/qwavedecoder/data/isawav_2_8_8000.wav and b/tests/auto/qwavedecoder/data/isawav_2_8_8000.wav differ
index 435aae9..4f0339a 100644 (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
 
index aeed403..6bf529f 100644 (file)
 #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;