From 6b6bcdb2a0d22f54ba05ab920c54d30758521f24 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 28 Jul 2008 14:46:09 +0000 Subject: [PATCH] --- juce_amalgamated.cpp | 203 ++++++++++-------- juce_amalgamated.h | 6 +- .../juce_OggVorbisAudioFormat.cpp | 93 ++++---- .../io/streams/juce_BufferedInputStream.cpp | 21 +- .../juce_GZIPDecompressorInputStream.cpp | 12 +- .../juce_GZIPDecompressorInputStream.h | 2 +- 6 files changed, 191 insertions(+), 146 deletions(-) diff --git a/juce_amalgamated.cpp b/juce_amalgamated.cpp index 12837d0981..d3a9eb7210 100644 --- a/juce_amalgamated.cpp +++ b/juce_amalgamated.cpp @@ -7151,7 +7151,7 @@ void BufferedInputStream::ensureBuffered() int BufferedInputStream::read (void* destBuffer, int maxBytesToRead) { if (position >= bufferStart - && position + maxBytesToRead < lastReadPos) + && position + maxBytesToRead <= lastReadPos) { memcpy (destBuffer, buffer + (position - bufferStart), maxBytesToRead); position += maxBytesToRead; @@ -7160,21 +7160,28 @@ int BufferedInputStream::read (void* destBuffer, int maxBytesToRead) } else { + if (position < bufferStart || position >= lastReadPos) + ensureBuffered(); + int bytesRead = 0; while (maxBytesToRead > 0) { + const int bytesAvailable = jmin (maxBytesToRead, (int) (lastReadPos - position)); + + if (bytesAvailable > 0) + { + memcpy (destBuffer, buffer + (position - bufferStart), bytesAvailable); + maxBytesToRead -= bytesAvailable; + bytesRead += bytesAvailable; + position += bytesAvailable; + destBuffer = (void*) (((char*) destBuffer) + bytesAvailable); + } + ensureBuffered(); if (isExhausted()) break; - - const int bytesAvailable = jmin (maxBytesToRead, (int) (lastReadPos - position)); - memcpy (destBuffer, buffer + (position - bufferStart), bytesAvailable); - maxBytesToRead -= bytesAvailable; - bytesRead += bytesAvailable; - position += bytesAvailable; - destBuffer = (void*) (((char*) destBuffer) + bytesAvailable); } return bytesRead; @@ -7919,52 +7926,56 @@ void ZipFile::init() { numEntries = 0; int pos = findEndOfZipEntryTable (in); - const int size = (int) (in->getTotalLength() - pos); - in->setPosition (pos); - MemoryBlock headerData; - - if (in->readIntoMemoryBlock (headerData, size) == size) + if (pos >= 0 && pos < in->getTotalLength()) { - pos = 0; + const int size = (int) (in->getTotalLength() - pos); + + in->setPosition (pos); + MemoryBlock headerData; - for (int i = 0; i < numEntries; ++i) + if (in->readIntoMemoryBlock (headerData, size) == size) { - if (pos + 46 > size) - break; + pos = 0; - const char* const buffer = ((const char*) headerData.getData()) + pos; + for (int i = 0; i < numEntries; ++i) + { + if (pos + 46 > size) + break; - const int fileNameLen = littleEndianShort (buffer + 28); + const char* const buffer = ((const char*) headerData.getData()) + pos; - if (pos + 46 + fileNameLen > size) - break; + const int fileNameLen = littleEndianShort (buffer + 28); - ZipEntryInfo* const zei = new ZipEntryInfo(); - zei->entry.filename = String (buffer + 46, fileNameLen); + if (pos + 46 + fileNameLen > size) + break; + + ZipEntryInfo* const zei = new ZipEntryInfo(); + zei->entry.filename = String (buffer + 46, fileNameLen); - const int time = littleEndianShort (buffer + 12); - const int date = littleEndianShort (buffer + 14); + const int time = littleEndianShort (buffer + 12); + const int date = littleEndianShort (buffer + 14); - const int year = 1980 + (date >> 9); - const int month = ((date >> 5) & 15) - 1; - const int day = date & 31; - const int hours = time >> 11; - const int minutes = (time >> 5) & 63; - const int seconds = (time & 31) << 1; + const int year = 1980 + (date >> 9); + const int month = ((date >> 5) & 15) - 1; + const int day = date & 31; + const int hours = time >> 11; + const int minutes = (time >> 5) & 63; + const int seconds = (time & 31) << 1; - zei->entry.fileTime = Time (year, month, day, hours, minutes, seconds); + zei->entry.fileTime = Time (year, month, day, hours, minutes, seconds); - zei->compressed = littleEndianShort (buffer + 10) != 0; - zei->compressedSize = littleEndianInt (buffer + 20); - zei->entry.uncompressedSize = littleEndianInt (buffer + 24); + zei->compressed = littleEndianShort (buffer + 10) != 0; + zei->compressedSize = littleEndianInt (buffer + 20); + zei->entry.uncompressedSize = littleEndianInt (buffer + 24); - zei->streamOffset = littleEndianInt (buffer + 42); - entries.add (zei); + zei->streamOffset = littleEndianInt (buffer + 42); + entries.add (zei); - pos += 46 + fileNameLen - + littleEndianShort (buffer + 30) - + littleEndianShort (buffer + 32); + pos += 46 + fileNameLen + + littleEndianShort (buffer + 30) + + littleEndianShort (buffer + 32); + } } } @@ -7979,11 +7990,12 @@ int ZipFile::findEndOfZipEntryTable (InputStream* input) in.setPosition (in.getTotalLength()); int64 pos = in.getPosition(); + const int64 lowestPos = jmax ((int64) 0, pos - 1024); char buffer [32]; zeromem (buffer, sizeof (buffer)); - while (pos > 0) + while (pos > lowestPos) { in.setPosition (pos - 22); pos = in.getPosition(); @@ -97991,7 +98003,6 @@ public: } bool needsInput() const throw() { return dataSize <= 0; } - int getTotalOut() const throw() { return (stream != 0) ? stream->total_out : 0; } void setInput (uint8* const data_, const int size) throw() { @@ -98050,7 +98061,8 @@ GZIPDecompressorInputStream::GZIPDecompressorInputStream (InputStream* const sou noWrap (noWrap_), isEof (false), activeBufferSize (0), - originalSourcePos (sourceStream_->getPosition()) + originalSourcePos (sourceStream_->getPosition()), + currentPos (0) { buffer = (uint8*) juce_malloc (gzipDecompBufferSize); helper = new GZIPDecompressHelper (noWrap_); @@ -98088,6 +98100,7 @@ int GZIPDecompressorInputStream::read (void* destBuffer, int howMany) while (! h->error) { const int n = h->doNextBlock (d, howMany); + currentPos += n; if (n == 0) { @@ -98137,15 +98150,11 @@ bool GZIPDecompressorInputStream::isExhausted() int64 GZIPDecompressorInputStream::getPosition() { - const GZIPDecompressHelper* const h = (GZIPDecompressHelper*) helper; - - return h->getTotalOut() + activeBufferSize; + return currentPos; } bool GZIPDecompressorInputStream::setPosition (int64 newPos) { - const int64 currentPos = getPosition(); - if (newPos != currentPos) { // reset the stream and start again.. @@ -98154,6 +98163,7 @@ bool GZIPDecompressorInputStream::setPosition (int64 newPos) isEof = false; activeBufferSize = 0; + currentPos = 0; helper = new GZIPDecompressHelper (noWrap); sourceStream->setPosition (originalSourcePos); @@ -179949,7 +179959,7 @@ public: OggReader (InputStream* const inp) : AudioFormatReader (inp, oggFormatName), - reservoir (2, 2048), + reservoir (2, 4096), reservoirStart (0), samplesInReservoir (0) { @@ -179985,58 +179995,73 @@ public: int64 startSampleInFile, int numSamples) { - if (startSampleInFile < reservoirStart - || startSampleInFile + numSamples > reservoirStart + samplesInReservoir) + int writeOffset = 0; + + while (numSamples > 0) { - // buffer miss, so refill the reservoir - int bitStream = 0; + const int numAvailable = reservoirStart + samplesInReservoir - startSampleInFile; - reservoirStart = (int) jmax ((int64) 0, startSampleInFile - 32); - samplesInReservoir = jmax (numSamples + 32, reservoir.getNumSamples()); - reservoir.setSize (numChannels, samplesInReservoir, false, false, true); + if (startSampleInFile >= reservoirStart && numAvailable > 0) + { + // got a few samples overlapping, so use them before seeking.. - if (reservoirStart != (int) ov_pcm_tell (&ovFile)) - ov_pcm_seek (&ovFile, reservoirStart); + for (unsigned int i = 0; i < numChannels; ++i) + { + if (destSamples[i] == 0) + break; - int offset = 0; - int numToRead = samplesInReservoir; + memcpy (destSamples[i] + writeOffset, + reservoir.getSampleData (jmin (i, reservoir.getNumChannels()), + (int) (startSampleInFile - reservoirStart)), + sizeof (float) * numSamples); + } - while (numToRead > 0) - { - float** dataIn = 0; + startSampleInFile += numAvailable; + numSamples -= numAvailable; + writeOffset += numAvailable; - const int samps = ov_read_float (&ovFile, &dataIn, numToRead, &bitStream); - if (samps == 0) + if (numSamples == 0) break; + } - jassert (samps <= numToRead); + if (startSampleInFile < reservoirStart + || startSampleInFile + numSamples > reservoirStart + samplesInReservoir) + { + // buffer miss, so refill the reservoir + int bitStream = 0; + + reservoirStart = jmax (0, (int) startSampleInFile); + samplesInReservoir = reservoir.getNumSamples(); + + if (reservoirStart != (int) ov_pcm_tell (&ovFile)) + ov_pcm_seek (&ovFile, reservoirStart); - for (int i = jmin (numChannels, reservoir.getNumChannels()); --i >= 0;) + int offset = 0; + int numToRead = samplesInReservoir; + + while (numToRead > 0) { - memcpy (reservoir.getSampleData (i, offset), - dataIn[i], - sizeof (float) * samps); - } + float** dataIn = 0; - numToRead -= samps; - offset += samps; - } + const int samps = ov_read_float (&ovFile, &dataIn, numToRead, &bitStream); + if (samps == 0) + break; - if (numToRead > 0) - reservoir.clear (offset, numToRead); - } + jassert (samps <= numToRead); - if (numSamples > 0) - { - for (unsigned int i = 0; i < numChannels; ++i) - { - if (destSamples[i] == 0) - break; + for (int i = jmin (numChannels, reservoir.getNumChannels()); --i >= 0;) + { + memcpy (reservoir.getSampleData (i, offset), + dataIn[i], + sizeof (float) * samps); + } + + numToRead -= samps; + offset += samps; + } - memcpy (destSamples[i], - reservoir.getSampleData (jmin (i, reservoir.getNumChannels()), - (int) (startSampleInFile - reservoirStart)), - sizeof (float) * numSamples); + if (numToRead > 0) + reservoir.clear (offset, numToRead); } } @@ -244888,7 +244913,7 @@ public: location = T("http://") + location; if (levelsOfRedirection++ < 3) - return open (location, headers, postData, isPost, callback, callbackContext); + return open (location, headers, postData, isPost, callback, callbackContext, timeOutMs); } else { @@ -253426,7 +253451,7 @@ public: location = T("http://") + location; if (levelsOfRedirection++ < 3) - return open (location, headers, postData, isPost, callback, callbackContext); + return open (location, headers, postData, isPost, callback, callbackContext, timeOutMs); } else { diff --git a/juce_amalgamated.h b/juce_amalgamated.h index 39df5e0b61..1a9798fa06 100644 --- a/juce_amalgamated.h +++ b/juce_amalgamated.h @@ -12614,7 +12614,7 @@ private: const bool deleteSourceWhenDestroyed, noWrap; bool isEof; int activeBufferSize; - int64 originalSourcePos; + int64 originalSourcePos, currentPos; uint8* buffer; void* helper; @@ -19548,7 +19548,7 @@ public: int sourceClipWidth, int sourceClipHeight, const AffineTransform& transform, - const bool fillAlphaChannelWithCurrentBrush) const throw(); + const bool fillAlphaChannelWithCurrentBrush = false) const throw(); /** Draws an image to fit within a designated rectangle. @@ -19577,7 +19577,7 @@ public: const int destWidth, const int destHeight, const RectanglePlacement& placementWithinTarget, - const bool fillAlphaChannelWithCurrentBrush) const throw(); + const bool fillAlphaChannelWithCurrentBrush = false) const throw(); /** Returns the position of the bounding box for the current clipping region. diff --git a/src/juce_appframework/audio/audio_file_formats/juce_OggVorbisAudioFormat.cpp b/src/juce_appframework/audio/audio_file_formats/juce_OggVorbisAudioFormat.cpp index 079dfd9439..81093228c7 100644 --- a/src/juce_appframework/audio/audio_file_formats/juce_OggVorbisAudioFormat.cpp +++ b/src/juce_appframework/audio/audio_file_formats/juce_OggVorbisAudioFormat.cpp @@ -98,7 +98,7 @@ public: //============================================================================== OggReader (InputStream* const inp) : AudioFormatReader (inp, oggFormatName), - reservoir (2, 2048), + reservoir (2, 4096), reservoirStart (0), samplesInReservoir (0) { @@ -135,58 +135,73 @@ public: int64 startSampleInFile, int numSamples) { - if (startSampleInFile < reservoirStart - || startSampleInFile + numSamples > reservoirStart + samplesInReservoir) + int writeOffset = 0; + + while (numSamples > 0) { - // buffer miss, so refill the reservoir - int bitStream = 0; + const int numAvailable = reservoirStart + samplesInReservoir - startSampleInFile; - reservoirStart = (int) jmax ((int64) 0, startSampleInFile - 32); - samplesInReservoir = jmax (numSamples + 32, reservoir.getNumSamples()); - reservoir.setSize (numChannels, samplesInReservoir, false, false, true); + if (startSampleInFile >= reservoirStart && numAvailable > 0) + { + // got a few samples overlapping, so use them before seeking.. - if (reservoirStart != (int) ov_pcm_tell (&ovFile)) - ov_pcm_seek (&ovFile, reservoirStart); + for (unsigned int i = 0; i < numChannels; ++i) + { + if (destSamples[i] == 0) + break; - int offset = 0; - int numToRead = samplesInReservoir; + memcpy (destSamples[i] + writeOffset, + reservoir.getSampleData (jmin (i, reservoir.getNumChannels()), + (int) (startSampleInFile - reservoirStart)), + sizeof (float) * numSamples); + } - while (numToRead > 0) - { - float** dataIn = 0; + startSampleInFile += numAvailable; + numSamples -= numAvailable; + writeOffset += numAvailable; - const int samps = ov_read_float (&ovFile, &dataIn, numToRead, &bitStream); - if (samps == 0) + if (numSamples == 0) break; + } + + if (startSampleInFile < reservoirStart + || startSampleInFile + numSamples > reservoirStart + samplesInReservoir) + { + // buffer miss, so refill the reservoir + int bitStream = 0; + + reservoirStart = jmax (0, (int) startSampleInFile); + samplesInReservoir = reservoir.getNumSamples(); + + if (reservoirStart != (int) ov_pcm_tell (&ovFile)) + ov_pcm_seek (&ovFile, reservoirStart); - jassert (samps <= numToRead); + int offset = 0; + int numToRead = samplesInReservoir; - for (int i = jmin (numChannels, reservoir.getNumChannels()); --i >= 0;) + while (numToRead > 0) { - memcpy (reservoir.getSampleData (i, offset), - dataIn[i], - sizeof (float) * samps); - } + float** dataIn = 0; - numToRead -= samps; - offset += samps; - } + const int samps = ov_read_float (&ovFile, &dataIn, numToRead, &bitStream); + if (samps == 0) + break; - if (numToRead > 0) - reservoir.clear (offset, numToRead); - } + jassert (samps <= numToRead); - if (numSamples > 0) - { - for (unsigned int i = 0; i < numChannels; ++i) - { - if (destSamples[i] == 0) - break; + for (int i = jmin (numChannels, reservoir.getNumChannels()); --i >= 0;) + { + memcpy (reservoir.getSampleData (i, offset), + dataIn[i], + sizeof (float) * samps); + } + + numToRead -= samps; + offset += samps; + } - memcpy (destSamples[i], - reservoir.getSampleData (jmin (i, reservoir.getNumChannels()), - (int) (startSampleInFile - reservoirStart)), - sizeof (float) * numSamples); + if (numToRead > 0) + reservoir.clear (offset, numToRead); } } diff --git a/src/juce_core/io/streams/juce_BufferedInputStream.cpp b/src/juce_core/io/streams/juce_BufferedInputStream.cpp index 418486c37e..77581076c4 100644 --- a/src/juce_core/io/streams/juce_BufferedInputStream.cpp +++ b/src/juce_core/io/streams/juce_BufferedInputStream.cpp @@ -135,21 +135,28 @@ int BufferedInputStream::read (void* destBuffer, int maxBytesToRead) } else { + if (position < bufferStart || position >= lastReadPos) + ensureBuffered(); + int bytesRead = 0; while (maxBytesToRead > 0) { + const int bytesAvailable = jmin (maxBytesToRead, (int) (lastReadPos - position)); + + if (bytesAvailable > 0) + { + memcpy (destBuffer, buffer + (position - bufferStart), bytesAvailable); + maxBytesToRead -= bytesAvailable; + bytesRead += bytesAvailable; + position += bytesAvailable; + destBuffer = (void*) (((char*) destBuffer) + bytesAvailable); + } + ensureBuffered(); if (isExhausted()) break; - - const int bytesAvailable = jmin (maxBytesToRead, (int) (lastReadPos - position)); - memcpy (destBuffer, buffer + (position - bufferStart), bytesAvailable); - maxBytesToRead -= bytesAvailable; - bytesRead += bytesAvailable; - position += bytesAvailable; - destBuffer = (void*) (((char*) destBuffer) + bytesAvailable); } return bytesRead; diff --git a/src/juce_core/io/streams/juce_GZIPDecompressorInputStream.cpp b/src/juce_core/io/streams/juce_GZIPDecompressorInputStream.cpp index 2ca3f59633..20353b6b3e 100644 --- a/src/juce_core/io/streams/juce_GZIPDecompressorInputStream.cpp +++ b/src/juce_core/io/streams/juce_GZIPDecompressorInputStream.cpp @@ -122,7 +122,6 @@ public: } bool needsInput() const throw() { return dataSize <= 0; } - int getTotalOut() const throw() { return (stream != 0) ? stream->total_out : 0; } void setInput (uint8* const data_, const int size) throw() { @@ -182,7 +181,8 @@ GZIPDecompressorInputStream::GZIPDecompressorInputStream (InputStream* const sou noWrap (noWrap_), isEof (false), activeBufferSize (0), - originalSourcePos (sourceStream_->getPosition()) + originalSourcePos (sourceStream_->getPosition()), + currentPos (0) { buffer = (uint8*) juce_malloc (gzipDecompBufferSize); helper = new GZIPDecompressHelper (noWrap_); @@ -220,6 +220,7 @@ int GZIPDecompressorInputStream::read (void* destBuffer, int howMany) while (! h->error) { const int n = h->doNextBlock (d, howMany); + currentPos += n; if (n == 0) { @@ -269,15 +270,11 @@ bool GZIPDecompressorInputStream::isExhausted() int64 GZIPDecompressorInputStream::getPosition() { - const GZIPDecompressHelper* const h = (GZIPDecompressHelper*) helper; - - return h->getTotalOut() + activeBufferSize; + return currentPos; } bool GZIPDecompressorInputStream::setPosition (int64 newPos) { - const int64 currentPos = getPosition(); - if (newPos != currentPos) { // reset the stream and start again.. @@ -286,6 +283,7 @@ bool GZIPDecompressorInputStream::setPosition (int64 newPos) isEof = false; activeBufferSize = 0; + currentPos = 0; helper = new GZIPDecompressHelper (noWrap); sourceStream->setPosition (originalSourcePos); diff --git a/src/juce_core/io/streams/juce_GZIPDecompressorInputStream.h b/src/juce_core/io/streams/juce_GZIPDecompressorInputStream.h index 540c071864..79afcdef93 100644 --- a/src/juce_core/io/streams/juce_GZIPDecompressorInputStream.h +++ b/src/juce_core/io/streams/juce_GZIPDecompressorInputStream.h @@ -85,7 +85,7 @@ private: const bool deleteSourceWhenDestroyed, noWrap; bool isEof; int activeBufferSize; - int64 originalSourcePos; + int64 originalSourcePos, currentPos; uint8* buffer; void* helper;