michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include "nsError.h" michael@0: #include "AbstractMediaDecoder.h" michael@0: #include "MediaResource.h" michael@0: #include "WaveReader.h" michael@0: #include "mozilla/dom/TimeRanges.h" michael@0: #include "MediaDecoderStateMachine.h" michael@0: #include "VideoUtils.h" michael@0: #include "nsISeekableStream.h" michael@0: michael@0: #include michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/CheckedInt.h" michael@0: #include "mozilla/Endian.h" michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: // Un-comment to enable logging of seek bisections. michael@0: //#define SEEK_LOGGING michael@0: michael@0: #ifdef PR_LOGGING michael@0: extern PRLogModuleInfo* gMediaDecoderLog; michael@0: #define LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg) michael@0: #ifdef SEEK_LOGGING michael@0: #define SEEK_LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg) michael@0: #else michael@0: #define SEEK_LOG(type, msg) michael@0: #endif michael@0: #else michael@0: #define LOG(type, msg) michael@0: #define SEEK_LOG(type, msg) michael@0: #endif michael@0: michael@0: struct waveIdToName { michael@0: uint32_t id; michael@0: nsCString name; michael@0: }; michael@0: michael@0: michael@0: // Magic values that identify RIFF chunks we're interested in. michael@0: static const uint32_t RIFF_CHUNK_MAGIC = 0x52494646; michael@0: static const uint32_t WAVE_CHUNK_MAGIC = 0x57415645; michael@0: static const uint32_t FRMT_CHUNK_MAGIC = 0x666d7420; michael@0: static const uint32_t DATA_CHUNK_MAGIC = 0x64617461; michael@0: static const uint32_t LIST_CHUNK_MAGIC = 0x4c495354; michael@0: michael@0: // Size of chunk header. 4 byte chunk header type and 4 byte size field. michael@0: static const uint16_t CHUNK_HEADER_SIZE = 8; michael@0: michael@0: // Size of RIFF header. RIFF chunk and 4 byte RIFF type. michael@0: static const uint16_t RIFF_INITIAL_SIZE = CHUNK_HEADER_SIZE + 4; michael@0: michael@0: // Size of required part of format chunk. Actual format chunks may be michael@0: // extended (for non-PCM encodings), but we skip any extended data. michael@0: static const uint16_t WAVE_FORMAT_CHUNK_SIZE = 16; michael@0: michael@0: // PCM encoding type from format chunk. Linear PCM is the only encoding michael@0: // supported by AudioStream. michael@0: static const uint16_t WAVE_FORMAT_ENCODING_PCM = 1; michael@0: michael@0: // We reject files with more than this number of channels if we're decoding for michael@0: // playback. michael@0: static const uint8_t MAX_CHANNELS = 2; michael@0: michael@0: namespace { michael@0: uint32_t michael@0: ReadUint32BE(const char** aBuffer) michael@0: { michael@0: uint32_t result = BigEndian::readUint32(*aBuffer); michael@0: *aBuffer += sizeof(uint32_t); michael@0: return result; michael@0: } michael@0: michael@0: uint32_t michael@0: ReadUint32LE(const char** aBuffer) michael@0: { michael@0: uint32_t result = LittleEndian::readUint32(*aBuffer); michael@0: *aBuffer += sizeof(uint32_t); michael@0: return result; michael@0: } michael@0: michael@0: uint16_t michael@0: ReadUint16LE(const char** aBuffer) michael@0: { michael@0: uint16_t result = LittleEndian::readUint16(*aBuffer); michael@0: *aBuffer += sizeof(uint16_t); michael@0: return result; michael@0: } michael@0: michael@0: int16_t michael@0: ReadInt16LE(const char** aBuffer) michael@0: { michael@0: uint16_t result = LittleEndian::readInt16(*aBuffer); michael@0: *aBuffer += sizeof(int16_t); michael@0: return result; michael@0: } michael@0: michael@0: uint8_t michael@0: ReadUint8(const char** aBuffer) michael@0: { michael@0: uint8_t result = uint8_t((*aBuffer)[0]); michael@0: *aBuffer += sizeof(uint8_t); michael@0: return result; michael@0: } michael@0: } michael@0: michael@0: WaveReader::WaveReader(AbstractMediaDecoder* aDecoder) michael@0: : MediaDecoderReader(aDecoder) michael@0: { michael@0: MOZ_COUNT_CTOR(WaveReader); michael@0: } michael@0: michael@0: WaveReader::~WaveReader() michael@0: { michael@0: MOZ_COUNT_DTOR(WaveReader); michael@0: } michael@0: michael@0: nsresult WaveReader::Init(MediaDecoderReader* aCloneDonor) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult WaveReader::ReadMetadata(MediaInfo* aInfo, michael@0: MetadataTags** aTags) michael@0: { michael@0: NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread."); michael@0: michael@0: bool loaded = LoadRIFFChunk(); michael@0: if (!loaded) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsAutoPtr tags; michael@0: michael@0: bool loadAllChunks = LoadAllChunks(tags); michael@0: if (!loadAllChunks) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: mInfo.mAudio.mHasAudio = true; michael@0: mInfo.mAudio.mRate = mSampleRate; michael@0: mInfo.mAudio.mChannels = mChannels; michael@0: michael@0: *aInfo = mInfo; michael@0: michael@0: *aTags = tags.forget(); michael@0: michael@0: ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor()); michael@0: michael@0: mDecoder->SetMediaDuration( michael@0: static_cast(BytesToTime(GetDataLength()) * USECS_PER_S)); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: template T UnsignedByteToAudioSample(uint8_t aValue); michael@0: template T SignedShortToAudioSample(int16_t aValue); michael@0: michael@0: template <> inline float michael@0: UnsignedByteToAudioSample(uint8_t aValue) michael@0: { michael@0: return aValue * (2.0f / UINT8_MAX) - 1.0f; michael@0: } michael@0: template <> inline int16_t michael@0: UnsignedByteToAudioSample(uint8_t aValue) michael@0: { michael@0: return int16_t(aValue * UINT16_MAX / UINT8_MAX + INT16_MIN); michael@0: } michael@0: michael@0: template <> inline float michael@0: SignedShortToAudioSample(int16_t aValue) michael@0: { michael@0: return AudioSampleToFloat(aValue); michael@0: } michael@0: template <> inline int16_t michael@0: SignedShortToAudioSample(int16_t aValue) michael@0: { michael@0: return aValue; michael@0: } michael@0: michael@0: bool WaveReader::DecodeAudioData() michael@0: { michael@0: NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread."); michael@0: michael@0: int64_t pos = GetPosition() - mWavePCMOffset; michael@0: int64_t len = GetDataLength(); michael@0: int64_t remaining = len - pos; michael@0: NS_ASSERTION(remaining >= 0, "Current wave position is greater than wave file length"); michael@0: michael@0: static const int64_t BLOCK_SIZE = 4096; michael@0: int64_t readSize = std::min(BLOCK_SIZE, remaining); michael@0: int64_t frames = readSize / mFrameSize; michael@0: michael@0: static_assert(uint64_t(BLOCK_SIZE) < UINT_MAX / michael@0: sizeof(AudioDataValue) / MAX_CHANNELS, michael@0: "bufferSize calculation could overflow."); michael@0: const size_t bufferSize = static_cast(frames * mChannels); michael@0: nsAutoArrayPtr sampleBuffer(new AudioDataValue[bufferSize]); michael@0: michael@0: static_assert(uint64_t(BLOCK_SIZE) < UINT_MAX / sizeof(char), michael@0: "BLOCK_SIZE too large for enumerator."); michael@0: nsAutoArrayPtr dataBuffer(new char[static_cast(readSize)]); michael@0: michael@0: if (!ReadAll(dataBuffer, readSize)) { michael@0: return false; michael@0: } michael@0: michael@0: // convert data to samples michael@0: const char* d = dataBuffer.get(); michael@0: AudioDataValue* s = sampleBuffer.get(); michael@0: for (int i = 0; i < frames; ++i) { michael@0: for (unsigned int j = 0; j < mChannels; ++j) { michael@0: if (mSampleFormat == FORMAT_U8) { michael@0: uint8_t v = ReadUint8(&d); michael@0: *s++ = UnsignedByteToAudioSample(v); michael@0: } else if (mSampleFormat == FORMAT_S16) { michael@0: int16_t v = ReadInt16LE(&d); michael@0: *s++ = SignedShortToAudioSample(v); michael@0: } michael@0: } michael@0: } michael@0: michael@0: double posTime = BytesToTime(pos); michael@0: double readSizeTime = BytesToTime(readSize); michael@0: NS_ASSERTION(posTime <= INT64_MAX / USECS_PER_S, "posTime overflow"); michael@0: NS_ASSERTION(readSizeTime <= INT64_MAX / USECS_PER_S, "readSizeTime overflow"); michael@0: NS_ASSERTION(frames < INT32_MAX, "frames overflow"); michael@0: michael@0: mAudioQueue.Push(new AudioData(pos, michael@0: static_cast(posTime * USECS_PER_S), michael@0: static_cast(readSizeTime * USECS_PER_S), michael@0: static_cast(frames), michael@0: sampleBuffer.forget(), michael@0: mChannels)); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool WaveReader::DecodeVideoFrame(bool &aKeyframeSkip, michael@0: int64_t aTimeThreshold) michael@0: { michael@0: NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread."); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: nsresult WaveReader::Seek(int64_t aTarget, int64_t aStartTime, int64_t aEndTime, int64_t aCurrentTime) michael@0: { michael@0: NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread."); michael@0: LOG(PR_LOG_DEBUG, ("%p About to seek to %lld", mDecoder, aTarget)); michael@0: if (NS_FAILED(ResetDecode())) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: double d = BytesToTime(GetDataLength()); michael@0: NS_ASSERTION(d < INT64_MAX / USECS_PER_S, "Duration overflow"); michael@0: int64_t duration = static_cast(d * USECS_PER_S); michael@0: double seekTime = std::min(aTarget, duration) / static_cast(USECS_PER_S); michael@0: int64_t position = RoundDownToFrame(static_cast(TimeToBytes(seekTime))); michael@0: NS_ASSERTION(INT64_MAX - mWavePCMOffset > position, "Integer overflow during wave seek"); michael@0: position += mWavePCMOffset; michael@0: return mDecoder->GetResource()->Seek(nsISeekableStream::NS_SEEK_SET, position); michael@0: } michael@0: michael@0: static double RoundToUsecs(double aSeconds) { michael@0: return floor(aSeconds * USECS_PER_S) / USECS_PER_S; michael@0: } michael@0: michael@0: nsresult WaveReader::GetBuffered(dom::TimeRanges* aBuffered, int64_t aStartTime) michael@0: { michael@0: if (!mInfo.HasAudio()) { michael@0: return NS_OK; michael@0: } michael@0: int64_t startOffset = mDecoder->GetResource()->GetNextCachedData(mWavePCMOffset); michael@0: while (startOffset >= 0) { michael@0: int64_t endOffset = mDecoder->GetResource()->GetCachedDataEnd(startOffset); michael@0: // Bytes [startOffset..endOffset] are cached. michael@0: NS_ASSERTION(startOffset >= mWavePCMOffset, "Integer underflow in GetBuffered"); michael@0: NS_ASSERTION(endOffset >= mWavePCMOffset, "Integer underflow in GetBuffered"); michael@0: michael@0: // We need to round the buffered ranges' times to microseconds so that they michael@0: // have the same precision as the currentTime and duration attribute on michael@0: // the media element. michael@0: aBuffered->Add(RoundToUsecs(BytesToTime(startOffset - mWavePCMOffset)), michael@0: RoundToUsecs(BytesToTime(endOffset - mWavePCMOffset))); michael@0: startOffset = mDecoder->GetResource()->GetNextCachedData(endOffset); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: WaveReader::ReadAll(char* aBuf, int64_t aSize, int64_t* aBytesRead) michael@0: { michael@0: uint32_t got = 0; michael@0: if (aBytesRead) { michael@0: *aBytesRead = 0; michael@0: } michael@0: do { michael@0: uint32_t read = 0; michael@0: if (NS_FAILED(mDecoder->GetResource()->Read(aBuf + got, uint32_t(aSize - got), &read))) { michael@0: NS_WARNING("Resource read failed"); michael@0: return false; michael@0: } michael@0: if (read == 0) { michael@0: return false; michael@0: } michael@0: got += read; michael@0: if (aBytesRead) { michael@0: *aBytesRead = got; michael@0: } michael@0: } while (got != aSize); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: WaveReader::LoadRIFFChunk() michael@0: { michael@0: char riffHeader[RIFF_INITIAL_SIZE]; michael@0: const char* p = riffHeader; michael@0: michael@0: NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() == 0, michael@0: "LoadRIFFChunk called when resource in invalid state"); michael@0: michael@0: if (!ReadAll(riffHeader, sizeof(riffHeader))) { michael@0: return false; michael@0: } michael@0: michael@0: static_assert(sizeof(uint32_t) * 3 <= RIFF_INITIAL_SIZE, michael@0: "Reads would overflow riffHeader buffer."); michael@0: if (ReadUint32BE(&p) != RIFF_CHUNK_MAGIC) { michael@0: NS_WARNING("resource data not in RIFF format"); michael@0: return false; michael@0: } michael@0: michael@0: // Skip over RIFF size field. michael@0: p += sizeof(uint32_t); michael@0: michael@0: if (ReadUint32BE(&p) != WAVE_CHUNK_MAGIC) { michael@0: NS_WARNING("Expected WAVE chunk"); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: WaveReader::LoadFormatChunk(uint32_t aChunkSize) michael@0: { michael@0: uint32_t rate, channels, frameSize, sampleFormat; michael@0: char waveFormat[WAVE_FORMAT_CHUNK_SIZE]; michael@0: const char* p = waveFormat; michael@0: michael@0: // RIFF chunks are always word (two byte) aligned. michael@0: NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0, michael@0: "LoadFormatChunk called with unaligned resource"); michael@0: michael@0: if (!ReadAll(waveFormat, sizeof(waveFormat))) { michael@0: return false; michael@0: } michael@0: michael@0: static_assert(sizeof(uint16_t) + michael@0: sizeof(uint16_t) + michael@0: sizeof(uint32_t) + michael@0: 4 + michael@0: sizeof(uint16_t) + michael@0: sizeof(uint16_t) <= sizeof(waveFormat), michael@0: "Reads would overflow waveFormat buffer."); michael@0: if (ReadUint16LE(&p) != WAVE_FORMAT_ENCODING_PCM) { michael@0: NS_WARNING("WAVE is not uncompressed PCM, compressed encodings are not supported"); michael@0: return false; michael@0: } michael@0: michael@0: channels = ReadUint16LE(&p); michael@0: rate = ReadUint32LE(&p); michael@0: michael@0: // Skip over average bytes per second field. michael@0: p += 4; michael@0: michael@0: frameSize = ReadUint16LE(&p); michael@0: michael@0: sampleFormat = ReadUint16LE(&p); michael@0: michael@0: // PCM encoded WAVEs are not expected to have an extended "format" chunk, michael@0: // but I have found WAVEs that have a extended "format" chunk with an michael@0: // extension size of 0 bytes. Be polite and handle this rather than michael@0: // considering the file invalid. This code skips any extension of the michael@0: // "format" chunk. michael@0: if (aChunkSize > WAVE_FORMAT_CHUNK_SIZE) { michael@0: char extLength[2]; michael@0: const char* p = extLength; michael@0: michael@0: if (!ReadAll(extLength, sizeof(extLength))) { michael@0: return false; michael@0: } michael@0: michael@0: static_assert(sizeof(uint16_t) <= sizeof(extLength), michael@0: "Reads would overflow extLength buffer."); michael@0: uint16_t extra = ReadUint16LE(&p); michael@0: if (aChunkSize - (WAVE_FORMAT_CHUNK_SIZE + 2) != extra) { michael@0: NS_WARNING("Invalid extended format chunk size"); michael@0: return false; michael@0: } michael@0: extra += extra % 2; michael@0: michael@0: if (extra > 0) { michael@0: static_assert(UINT16_MAX + (UINT16_MAX % 2) < UINT_MAX / sizeof(char), michael@0: "chunkExtension array too large for iterator."); michael@0: nsAutoArrayPtr chunkExtension(new char[extra]); michael@0: if (!ReadAll(chunkExtension.get(), extra)) { michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // RIFF chunks are always word (two byte) aligned. michael@0: NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0, michael@0: "LoadFormatChunk left resource unaligned"); michael@0: michael@0: // Make sure metadata is fairly sane. The rate check is fairly arbitrary, michael@0: // but the channels check is intentionally limited to mono or stereo michael@0: // when the media is intended for direct playback because that's what the michael@0: // audio backend currently supports. michael@0: unsigned int actualFrameSize = (sampleFormat == 8 ? 1 : 2) * channels; michael@0: if (rate < 100 || rate > 96000 || michael@0: (((channels < 1 || channels > MAX_CHANNELS) || michael@0: (frameSize != 1 && frameSize != 2 && frameSize != 4)) && michael@0: !mIgnoreAudioOutputFormat) || michael@0: (sampleFormat != 8 && sampleFormat != 16) || michael@0: frameSize != actualFrameSize) { michael@0: NS_WARNING("Invalid WAVE metadata"); michael@0: return false; michael@0: } michael@0: michael@0: ReentrantMonitorAutoEnter monitor(mDecoder->GetReentrantMonitor()); michael@0: mSampleRate = rate; michael@0: mChannels = channels; michael@0: mFrameSize = frameSize; michael@0: if (sampleFormat == 8) { michael@0: mSampleFormat = FORMAT_U8; michael@0: } else { michael@0: mSampleFormat = FORMAT_S16; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: WaveReader::FindDataOffset(uint32_t aChunkSize) michael@0: { michael@0: // RIFF chunks are always word (two byte) aligned. michael@0: NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0, michael@0: "FindDataOffset called with unaligned resource"); michael@0: michael@0: int64_t offset = mDecoder->GetResource()->Tell(); michael@0: if (offset <= 0 || offset > UINT32_MAX) { michael@0: NS_WARNING("PCM data offset out of range"); michael@0: return false; michael@0: } michael@0: michael@0: ReentrantMonitorAutoEnter monitor(mDecoder->GetReentrantMonitor()); michael@0: mWaveLength = aChunkSize; michael@0: mWavePCMOffset = uint32_t(offset); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: WaveReader::BytesToTime(int64_t aBytes) const michael@0: { michael@0: NS_ABORT_IF_FALSE(aBytes >= 0, "Must be >= 0"); michael@0: return float(aBytes) / mSampleRate / mFrameSize; michael@0: } michael@0: michael@0: int64_t michael@0: WaveReader::TimeToBytes(double aTime) const michael@0: { michael@0: NS_ABORT_IF_FALSE(aTime >= 0.0f, "Must be >= 0"); michael@0: return RoundDownToFrame(int64_t(aTime * mSampleRate * mFrameSize)); michael@0: } michael@0: michael@0: int64_t michael@0: WaveReader::RoundDownToFrame(int64_t aBytes) const michael@0: { michael@0: NS_ABORT_IF_FALSE(aBytes >= 0, "Must be >= 0"); michael@0: return aBytes - (aBytes % mFrameSize); michael@0: } michael@0: michael@0: int64_t michael@0: WaveReader::GetDataLength() michael@0: { michael@0: int64_t length = mWaveLength; michael@0: // If the decoder has a valid content length, and it's shorter than the michael@0: // expected length of the PCM data, calculate the playback duration from michael@0: // the content length rather than the expected PCM data length. michael@0: int64_t streamLength = mDecoder->GetResource()->GetLength(); michael@0: if (streamLength >= 0) { michael@0: int64_t dataLength = std::max(0, streamLength - mWavePCMOffset); michael@0: length = std::min(dataLength, length); michael@0: } michael@0: return length; michael@0: } michael@0: michael@0: int64_t michael@0: WaveReader::GetPosition() michael@0: { michael@0: return mDecoder->GetResource()->Tell(); michael@0: } michael@0: michael@0: bool michael@0: WaveReader::GetNextChunk(uint32_t* aChunk, uint32_t* aChunkSize) michael@0: { michael@0: NS_ABORT_IF_FALSE(aChunk, "Must have aChunk"); michael@0: NS_ABORT_IF_FALSE(aChunkSize, "Must have aChunkSize"); michael@0: NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0, michael@0: "GetNextChunk called with unaligned resource"); michael@0: michael@0: char chunkHeader[CHUNK_HEADER_SIZE]; michael@0: const char* p = chunkHeader; michael@0: michael@0: if (!ReadAll(chunkHeader, sizeof(chunkHeader))) { michael@0: return false; michael@0: } michael@0: michael@0: static_assert(sizeof(uint32_t) * 2 <= CHUNK_HEADER_SIZE, michael@0: "Reads would overflow chunkHeader buffer."); michael@0: *aChunk = ReadUint32BE(&p); michael@0: *aChunkSize = ReadUint32LE(&p); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: WaveReader::LoadListChunk(uint32_t aChunkSize, michael@0: nsAutoPtr &aTags) michael@0: { michael@0: // List chunks are always word (two byte) aligned. michael@0: NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0, michael@0: "LoadListChunk called with unaligned resource"); michael@0: michael@0: static const unsigned int MAX_CHUNK_SIZE = 1 << 16; michael@0: static_assert(uint64_t(MAX_CHUNK_SIZE) < UINT_MAX / sizeof(char), michael@0: "MAX_CHUNK_SIZE too large for enumerator."); michael@0: michael@0: if (aChunkSize > MAX_CHUNK_SIZE) { michael@0: return false; michael@0: } michael@0: michael@0: nsAutoArrayPtr chunk(new char[aChunkSize]); michael@0: if (!ReadAll(chunk.get(), aChunkSize)) { michael@0: return false; michael@0: } michael@0: michael@0: static const uint32_t INFO_LIST_MAGIC = 0x494e464f; michael@0: const char *p = chunk.get(); michael@0: if (ReadUint32BE(&p) != INFO_LIST_MAGIC) { michael@0: return false; michael@0: } michael@0: michael@0: const waveIdToName ID_TO_NAME[] = { michael@0: { 0x49415254, NS_LITERAL_CSTRING("artist") }, // IART michael@0: { 0x49434d54, NS_LITERAL_CSTRING("comments") }, // ICMT michael@0: { 0x49474e52, NS_LITERAL_CSTRING("genre") }, // IGNR michael@0: { 0x494e414d, NS_LITERAL_CSTRING("name") }, // INAM michael@0: }; michael@0: michael@0: const char* const end = chunk.get() + aChunkSize; michael@0: michael@0: aTags = new dom::HTMLMediaElement::MetadataTags; michael@0: michael@0: while (p + 8 < end) { michael@0: uint32_t id = ReadUint32BE(&p); michael@0: // Uppercase tag id, inspired by GStreamer's Wave parser. michael@0: id &= 0xDFDFDFDF; michael@0: michael@0: uint32_t length = ReadUint32LE(&p); michael@0: michael@0: // Subchunk shall not exceed parent chunk. michael@0: if (p + length > end) { michael@0: break; michael@0: } michael@0: michael@0: // Wrap the string, adjusting length to account for optional michael@0: // null termination in the chunk. michael@0: nsCString val(p, length); michael@0: if (length > 0 && val[length - 1] == '\0') { michael@0: val.SetLength(length - 1); michael@0: } michael@0: michael@0: // Chunks in List::INFO are always word (two byte) aligned. So round up if michael@0: // necessary. michael@0: length += length % 2; michael@0: p += length; michael@0: michael@0: if (!IsUTF8(val)) { michael@0: continue; michael@0: } michael@0: michael@0: for (size_t i = 0; i < mozilla::ArrayLength(ID_TO_NAME); ++i) { michael@0: if (id == ID_TO_NAME[i].id) { michael@0: aTags->Put(ID_TO_NAME[i].name, val); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: WaveReader::LoadAllChunks(nsAutoPtr &aTags) michael@0: { michael@0: // Chunks are always word (two byte) aligned. michael@0: NS_ABORT_IF_FALSE(mDecoder->GetResource()->Tell() % 2 == 0, michael@0: "LoadAllChunks called with unaligned resource"); michael@0: michael@0: bool loadFormatChunk = false; michael@0: bool findDataOffset = false; michael@0: michael@0: for (;;) { michael@0: static const unsigned int CHUNK_HEADER_SIZE = 8; michael@0: char chunkHeader[CHUNK_HEADER_SIZE]; michael@0: const char* p = chunkHeader; michael@0: michael@0: if (!ReadAll(chunkHeader, sizeof(chunkHeader))) { michael@0: return false; michael@0: } michael@0: michael@0: static_assert(sizeof(uint32_t) * 2 <= CHUNK_HEADER_SIZE, michael@0: "Reads would overflow chunkHeader buffer."); michael@0: michael@0: uint32_t magic = ReadUint32BE(&p); michael@0: uint32_t chunkSize = ReadUint32LE(&p); michael@0: int64_t chunkStart = GetPosition(); michael@0: michael@0: switch (magic) { michael@0: case FRMT_CHUNK_MAGIC: michael@0: loadFormatChunk = LoadFormatChunk(chunkSize); michael@0: if (!loadFormatChunk) { michael@0: return false; michael@0: } michael@0: break; michael@0: michael@0: case LIST_CHUNK_MAGIC: michael@0: if (!aTags) { michael@0: LoadListChunk(chunkSize, aTags); michael@0: } michael@0: break; michael@0: michael@0: case DATA_CHUNK_MAGIC: michael@0: findDataOffset = FindDataOffset(chunkSize); michael@0: return loadFormatChunk && findDataOffset; michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: // RIFF chunks are two-byte aligned, so round up if necessary. michael@0: chunkSize += chunkSize % 2; michael@0: michael@0: // Move forward to next chunk michael@0: CheckedInt64 forward = CheckedInt64(chunkStart) + chunkSize - GetPosition(); michael@0: michael@0: if (!forward.isValid() || forward.value() < 0) { michael@0: return false; michael@0: } michael@0: michael@0: static const int64_t MAX_CHUNK_SIZE = 1 << 16; michael@0: static_assert(uint64_t(MAX_CHUNK_SIZE) < UINT_MAX / sizeof(char), michael@0: "MAX_CHUNK_SIZE too large for enumerator."); michael@0: nsAutoArrayPtr chunk(new char[MAX_CHUNK_SIZE]); michael@0: while (forward.value() > 0) { michael@0: int64_t size = std::min(forward.value(), MAX_CHUNK_SIZE); michael@0: if (!ReadAll(chunk.get(), size)) { michael@0: return false; michael@0: } michael@0: forward -= size; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: } // namespace mozilla