mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
93f846cd1f
- Bug 1275016 - Rename Endian.h to EndianUtils.h to avoid #include confusion with Android's endian.h stdlib header. r=froydnj (b54a25f572) - add crashreporter stuff (aa7ef15337) - Bug 1261168 - Add AlignedAutoTArray type in Web Audio; r=padenot (285d2cb88b) - Bug 1273390. Part 1 - move some functions to private. r=jya. (07a3037e59) - Bug 1273390. Part 2 - add assertions. r=jya. (2cae7c596a) - Bug 1273390. Part 3 - rename some functions to be consistent with other sub-classes of MediaDataDecoder. r=jya. (c48c7060ce) - Bug 1273390. Part 4 - remove use of FlushableTaskQueue::Flush(). r=jya. (00565a65f4) - Bug 1273390. Part 5 - remove use of FlushableTaskQueue. r=jya. (30600b204e) - Bug 1273774. Part 1 - remove unused members and thread assertions. r=jya (f5177ed641) - Bug 1273774. Part 2 - do decoding jobs synchronously without dispatching. r=jya. (62d840d27c) - Bug 1273774. Part 3 - remove members no longer used. r=jya. (e957ca512a) - Bug 1244410: [ffmpeg] Ensure the last drained frame has the proper duration set. r=gerald (d5521bfdd4) - Bug 1271508. Part 1 - refactor FFmpegAudioDecoder code to be similar to FFmpegVideoDecoder::Input() so it would be easier to extract common code to the parent class. r=jya. (613e6c624c) - Bug 1271508. Part 2 - rename functions so they are the same as those of FFmpegAudioDecoder so it would be easier to extract common code to the parent class. r=jya. (cb281cba26) - Bug 1270350 - per comment 0, use SyncRunnable to repalce the boilerplate code. r=jya. (b99460e571) - Bug 1271508. Part 3 - extract code to the parent class and remove use of mTaskQueue from sub-classes. r=jya. (2a7ff4dd1e) - Bug 1274216 - remove use of FlushableTaskQueue from PlatformDecoderModule. r=jya. (eb160c5fa2) - Bug 1271517. Part 1 - remove use of FlushableTaskQueue::Flush() from FFmpegDataDecoder::Flush(). r=jya. (fdf10da4ab) - Bug 1271517. Part 2 - remove use of FlushableTaskQueue. r=jya. (a7016d8506) - Bug 1273397. Part 1 - rename some functions to be consistent with other MediaDataDecoder sub-classes. r=jya. (7eecb164be) - Bug 1273397. Part 2 - constify some members. r=jya. (e4482f9a23) - Bug 1273397. Part 3 - remove use of FlushableTaskQueue::Flush(). r=jya. (0b7ee073fe) - Bug 1273397. Part 4 - remove use of FlushableTaskQueue. r=jya. (6a612161d5) - Bug 1273397. Part 5 - add assertions. r=jya. (ff3a62a6fb) - Bug 1274199 - remove use of FlushableTaskQueue. r=cpearce. (adc4c84ede) - Bug 1273405. Part 1 - rename some functions to be consistent with other MediaDataDecoder sub-classes. r=jya. (af123d6c21) - Bug 1273405. Part 2 - remove use of FlushableTaskQueue::Flush(). r=jya. (2d144bfbcd) - Bug 1273405. Part 3 - remove use of FlushableTaskQueue. r=jya. (1e9ea3c2c7) - Bug 1273405. Part 4 - add assertions. r=jya. (b400647323) - Bug 1271491: [WMF] P1. Don't use main thread only preferences methods. r=cpearce (7177454dfb) - Bug 1262427. Don't try D3D11 harder. r=dvander (404147d6fa) - Use gfxConfig for D3D9 preferences. (bug 1270650, r=jrmuizel) (40d89c154c) - Bug 1271491: P2. Allow initialization of WMFPlatformDecoderModule from any threads. r=mattwoodrow (c8fe0bf009) - Bug 1271491: P3. Remove refcounting the number of time apple's linkers are called. r=cpearce (0324ffe876) - Bug 1271491: [ffmpeg] P4. Remove requirements to call Init on the main thread. r=cpearce (b511d7dfd5) - Bug 1271491: [GMP] P5. Allow GMPDecoderModule::Init() to be called off the main thread. r=cpearce (2131eb0b2e) - Bug 1266102 - Don't run VP9 benchmark on Android r=jya (57d7b573fe) - Bug 1271491: P6. Remove the need to call PDMFactory::Init(). r=cpearce (5726cfe49c) - Bug 1271491: P7. Remove unused members. r=alfredo (0f8a9dde73) - Bug 1268905 - Disable D3D11 with some Toshiba DLLs - r=cpearce (b5bf77442e) - Bug 1269204 - Disable D3D11 with idg10umd32 9.17.10.2857 - r=cpearce (7eb6a3d96b) - Bug 1273406 - Disable D3D11 with some iSonyVideoProcessor DLLs - r=cpearce (d9b6f0cefe) - Bug 1273406 - Ugly macros transform into beautiful constexpr goodness - r=cpearce (0671483695) - Bug 1273691 - Implement 'media.wmf.disable-d3d11-for-dlls' pref - r=cpearce (193ae53070) - Bug 1272225. Part 1 - add assertions to make thread constraints clear. r=jya. (83c620470e) - Bug 1272225. Part 2 - remove use of FlushableTaskQueue::Flush(). r=jya. (9473e092d1) - Bug 1272553. Part 1 - move code around to be able to extract common code in P2. r=jya. (d727f97ee8) - Bug 1272553. Part 2 - extract common code to the parent class. r=jya. (2fb3cd4bd9) - Bug 1272553. Part 3 - make mTaskQueue private. r=jya. (93fea98cb6) - Bug 1272232. Part 1 - move code around so we can extract common code in P2. r=jya. (8cdaab9078) - Bug 1272232. Part 2 - extract common code to the parent class. r=jya. (27156668b3) - Bug 1272232. Part 3 - constify some members and make them private when possible. r=jya. (550b963d97) - Bug 1272232. Part 4 - remove use of FlushableTaskQueue::Flush(). r=jya. (bdbfdeb6bc) - Bug 1272232. Part 5 - remove use of FlushableTaskQueue. r=jya. (640f889a9d) - Bug 1274913 - Move PDM log definition to header. r=njn (823b07f42b) - Bug 1275538: P1. Abort early if a skip request is in progress. r=gerald,kamidphish (d67b8a2236) - Bug 1272422 - Part 1: Expose control of suspending background video. r=cpearce (ec7193773f) - Bug 1272422 - Part 2: Vidoe -> Video. r=cpearce (97390aee69) - Bug 1272422 - Part 3: Don't reset audio queue. r=jya (e183db1062) - Bug 1272964: P1. Only activate skip to next keyframe logic when next keyframe time is known. r=gerald (1be74df027) - Bug 1272964: P2. Don't activate skip to next keyframe until we passed the internal seek target. r=gerald (c55b6ae003) - Bug 1258922: [MSE] P1. Initialise variable. r=gerald (56a5acb345) - Bug 1258922: [MSE] P2. Do not go over gap when attempting to find the next key frame. r=gerald (db1319f080) - Bug 1258922: [MSE] P3. Check that the data we are attempting to skip to is buffered. r=gerald (621d71d5d6) - Bug 1258922: [MSE] P4. Set draining flag to true when skip to next keyframe failed. r=gerald (6c75613faf) - Bug 1272916: [MSE] P1. Don't rely only on dts gap to establish if we have a gap in our source buffer. r=gerald (8770113b83) - Bug 1272964: [MSE] P3. Do not skip over gaps when searching for the next keyframe. r=gerald (76916c5ac6) - Bug 1272964: P4. Only flush decoder if skip to next keyframe actually succeeds. r=cpearce (5394708eef) - Bug 1270323: P1. Don't reset flag indicating that new data was received. r=cpearce (d32f06ef34) - Bug 1270323: P2. Don't process new incoming data while a skip to next keyframe is pending. r=cpearce (bca7909de9) - Bug 1270323: [ffmpeg] P3. Use the dts of the last sample input, not the dts of the last decoded sample (0d768c33ef) - Bug 1270323: P4. Don't drain decoder if we're already waiting for new data. r=cpearce (679302cb6e) - Bug 1270323: P5. Prevent potential null deref. r=cpearce (cc63270e06) - Bug 1275538: P2. Drop decoded frames that we know are already too late. r=kamidphish (4e7af9398c) - Bug 1273018: P1. Rename some members. r=gerald (3a92fbd994) - Bug 1273018: P2. Don't reject audio waiting promise when performing a video only seek. r=gerald (34e4988db1) - Bug 1273018: P3. Adjust range of audio assertions. r=gerald (feb2afd0ae) - Bug 1249706 - Backout a085ea2d24bb for blowing telemetry server's mind. r=backout (d61fb51f52) - Bug 1249706 - Fix 8fe22dd4fc8a (backout of a085ea2d24bb). r=bustage (ba65251db7) - Bug 1272964: [MSE] P5. Default to skipping to the next keyframe if no keyframe was found past currentTime. (29086fcf56) - Bug 1272964: P6. Exclude frames dropped due to internal seeking from calculations. r=cpearce (bf6faa7612) - Bug 1068151 - keep decoding a corrupted video. r=jya (3b5462e5b6) - Bug 1273947 - Update ResetDecode() to ResetDecode(TargetQueue) r=jya (6c28d46974) - Bug 1277508: P1. Don't attempt to demux new samples while we're currently draining. r=kamidphish (64f200b921) - Bug 1274933: Reject data promise when EOS is encountered following waiting for data. r=gerald (5bba4a7853) - Bug 1277508: P2. Add HasPendingDrain convenience method. r=kamidphish (3d89a90a97)
690 lines
19 KiB
C++
690 lines
19 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
#include "nsError.h"
|
|
#include "AbstractMediaDecoder.h"
|
|
#include "WaveReader.h"
|
|
#include "MediaDecoderStateMachine.h"
|
|
#include "VideoUtils.h"
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include <stdint.h>
|
|
#include "mozilla/ArrayUtils.h"
|
|
#include "mozilla/CheckedInt.h"
|
|
#include "mozilla/EndianUtils.h"
|
|
#include "mozilla/UniquePtr.h"
|
|
#include <algorithm>
|
|
|
|
using namespace mozilla::media;
|
|
|
|
namespace mozilla {
|
|
|
|
// Un-comment to enable logging of seek bisections.
|
|
//#define SEEK_LOGGING
|
|
|
|
extern LazyLogModule gMediaDecoderLog;
|
|
#define LOG(type, msg) MOZ_LOG(gMediaDecoderLog, type, msg)
|
|
#ifdef SEEK_LOGGING
|
|
#define SEEK_LOG(type, msg) MOZ_LOG(gMediaDecoderLog, type, msg)
|
|
#else
|
|
#define SEEK_LOG(type, msg)
|
|
#endif
|
|
|
|
struct waveIdToName {
|
|
uint32_t id;
|
|
nsCString name;
|
|
};
|
|
|
|
|
|
// Magic values that identify RIFF chunks we're interested in.
|
|
static const uint32_t RIFF_CHUNK_MAGIC = 0x52494646;
|
|
static const uint32_t WAVE_CHUNK_MAGIC = 0x57415645;
|
|
static const uint32_t FRMT_CHUNK_MAGIC = 0x666d7420;
|
|
static const uint32_t DATA_CHUNK_MAGIC = 0x64617461;
|
|
static const uint32_t LIST_CHUNK_MAGIC = 0x4c495354;
|
|
|
|
// Size of chunk header. 4 byte chunk header type and 4 byte size field.
|
|
static const uint16_t CHUNK_HEADER_SIZE = 8;
|
|
|
|
// Size of RIFF header. RIFF chunk and 4 byte RIFF type.
|
|
static const uint16_t RIFF_INITIAL_SIZE = CHUNK_HEADER_SIZE + 4;
|
|
|
|
// Size of required part of format chunk. Actual format chunks may be
|
|
// extended (for non-PCM encodings), but we skip any extended data.
|
|
static const uint16_t WAVE_FORMAT_CHUNK_SIZE = 16;
|
|
|
|
// PCM encoding type from format chunk. Linear PCM is the only encoding
|
|
// supported by AudioStream.
|
|
static const uint16_t WAVE_FORMAT_ENCODING_PCM = 1;
|
|
|
|
// We reject files with more than this number of channels if we're decoding for
|
|
// playback.
|
|
static const uint8_t MAX_CHANNELS = 2;
|
|
|
|
namespace {
|
|
uint32_t
|
|
ReadUint32BE(const char** aBuffer)
|
|
{
|
|
uint32_t result = BigEndian::readUint32(*aBuffer);
|
|
*aBuffer += sizeof(uint32_t);
|
|
return result;
|
|
}
|
|
|
|
uint32_t
|
|
ReadUint32LE(const char** aBuffer)
|
|
{
|
|
uint32_t result = LittleEndian::readUint32(*aBuffer);
|
|
*aBuffer += sizeof(uint32_t);
|
|
return result;
|
|
}
|
|
|
|
int32_t
|
|
ReadInt24LE(const char** aBuffer)
|
|
{
|
|
int32_t result = int32_t((uint8_t((*aBuffer)[2]) << 16) |
|
|
(uint8_t((*aBuffer)[1]) << 8 ) |
|
|
(uint8_t((*aBuffer)[0])));
|
|
if (((*aBuffer)[2] & 0x80) == 0x80) {
|
|
result = (result | 0xff000000);
|
|
}
|
|
|
|
*aBuffer += 3 * sizeof(char);
|
|
return result;
|
|
}
|
|
|
|
uint16_t
|
|
ReadUint16LE(const char** aBuffer)
|
|
{
|
|
uint16_t result = LittleEndian::readUint16(*aBuffer);
|
|
*aBuffer += sizeof(uint16_t);
|
|
return result;
|
|
}
|
|
|
|
int16_t
|
|
ReadInt16LE(const char** aBuffer)
|
|
{
|
|
uint16_t result = LittleEndian::readInt16(*aBuffer);
|
|
*aBuffer += sizeof(int16_t);
|
|
return result;
|
|
}
|
|
|
|
uint8_t
|
|
ReadUint8(const char** aBuffer)
|
|
{
|
|
uint8_t result = uint8_t((*aBuffer)[0]);
|
|
*aBuffer += sizeof(uint8_t);
|
|
return result;
|
|
}
|
|
} // namespace
|
|
|
|
WaveReader::WaveReader(AbstractMediaDecoder* aDecoder)
|
|
: MediaDecoderReader(aDecoder)
|
|
, mResource(aDecoder->GetResource())
|
|
{
|
|
MOZ_COUNT_CTOR(WaveReader);
|
|
}
|
|
|
|
WaveReader::~WaveReader()
|
|
{
|
|
MOZ_COUNT_DTOR(WaveReader);
|
|
}
|
|
|
|
nsresult WaveReader::ReadMetadata(MediaInfo* aInfo,
|
|
MetadataTags** aTags)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
bool loaded = LoadRIFFChunk();
|
|
if (!loaded) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
nsAutoPtr<dom::HTMLMediaElement::MetadataTags> tags;
|
|
|
|
bool loadAllChunks = LoadAllChunks(tags);
|
|
if (!loadAllChunks) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
mInfo.mAudio.mRate = mSampleRate;
|
|
mInfo.mAudio.mChannels = mChannels;
|
|
mInfo.mMetadataDuration.emplace(TimeUnit::FromSeconds(BytesToTime(GetDataLength())));
|
|
|
|
*aInfo = mInfo;
|
|
|
|
*aTags = tags.forget();
|
|
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
template <typename T> T UnsignedByteToAudioSample(uint8_t aValue);
|
|
template <typename T> T SignedShortToAudioSample(int16_t aValue);
|
|
template <typename T> T Signed24bIntToAudioSample(int32_t aValue);
|
|
|
|
template <> inline float
|
|
UnsignedByteToAudioSample<float>(uint8_t aValue)
|
|
{
|
|
return aValue * (2.0f / UINT8_MAX) - 1.0f;
|
|
}
|
|
template <> inline int16_t
|
|
UnsignedByteToAudioSample<int16_t>(uint8_t aValue)
|
|
{
|
|
return int16_t(aValue * UINT16_MAX / UINT8_MAX + INT16_MIN);
|
|
}
|
|
|
|
template <> inline float
|
|
SignedShortToAudioSample<float>(int16_t aValue)
|
|
{
|
|
return AudioSampleToFloat(aValue);
|
|
}
|
|
template <> inline int16_t
|
|
SignedShortToAudioSample<int16_t>(int16_t aValue)
|
|
{
|
|
return aValue;
|
|
}
|
|
|
|
template <> inline float
|
|
Signed24bIntToAudioSample<float>(int32_t aValue)
|
|
{
|
|
return aValue / 8388608.0f;
|
|
}
|
|
|
|
template <> inline int16_t
|
|
Signed24bIntToAudioSample<int16_t>(int32_t aValue)
|
|
{
|
|
return aValue / 256;
|
|
}
|
|
|
|
bool WaveReader::DecodeAudioData()
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
int64_t pos = GetPosition() - mWavePCMOffset;
|
|
int64_t len = GetDataLength();
|
|
int64_t remaining = len - pos;
|
|
NS_ASSERTION(remaining >= 0, "Current wave position is greater than wave file length");
|
|
|
|
static const int64_t BLOCK_SIZE = 6144;
|
|
int64_t readSize = std::min(BLOCK_SIZE, remaining);
|
|
int64_t frames = readSize / mFrameSize;
|
|
|
|
MOZ_ASSERT(BLOCK_SIZE % 3 == 0);
|
|
MOZ_ASSERT(BLOCK_SIZE % 2 == 0);
|
|
|
|
static_assert(uint64_t(BLOCK_SIZE) < UINT_MAX /
|
|
sizeof(AudioDataValue) / MAX_CHANNELS,
|
|
"bufferSize calculation could overflow.");
|
|
const size_t bufferSize = static_cast<size_t>(frames * mChannels);
|
|
AlignedAudioBuffer sampleBuffer(bufferSize);
|
|
if (!sampleBuffer) {
|
|
return false;
|
|
}
|
|
|
|
static_assert(uint64_t(BLOCK_SIZE) < UINT_MAX / sizeof(char),
|
|
"BLOCK_SIZE too large for enumerator.");
|
|
auto dataBuffer = MakeUnique<char[]>(static_cast<size_t>(readSize));
|
|
|
|
if (!ReadAll(dataBuffer.get(), readSize)) {
|
|
return false;
|
|
}
|
|
|
|
// convert data to samples
|
|
const char* d = dataBuffer.get();
|
|
AudioDataValue* s = sampleBuffer.get();
|
|
for (int i = 0; i < frames; ++i) {
|
|
for (unsigned int j = 0; j < mChannels; ++j) {
|
|
if (mSampleFormat == FORMAT_U8) {
|
|
uint8_t v = ReadUint8(&d);
|
|
*s++ = UnsignedByteToAudioSample<AudioDataValue>(v);
|
|
} else if (mSampleFormat == FORMAT_S16) {
|
|
int16_t v = ReadInt16LE(&d);
|
|
*s++ = SignedShortToAudioSample<AudioDataValue>(v);
|
|
} else if (mSampleFormat == FORMAT_S24) {
|
|
int32_t v = ReadInt24LE(&d);
|
|
*s++ = Signed24bIntToAudioSample<AudioDataValue>(v);
|
|
}
|
|
}
|
|
}
|
|
|
|
double posTime = BytesToTime(pos);
|
|
double readSizeTime = BytesToTime(readSize);
|
|
NS_ASSERTION(posTime <= INT64_MAX / USECS_PER_S, "posTime overflow");
|
|
NS_ASSERTION(readSizeTime <= INT64_MAX / USECS_PER_S, "readSizeTime overflow");
|
|
NS_ASSERTION(frames < INT32_MAX, "frames overflow");
|
|
|
|
mAudioQueue.Push(new AudioData(pos,
|
|
static_cast<int64_t>(posTime * USECS_PER_S),
|
|
static_cast<int64_t>(readSizeTime * USECS_PER_S),
|
|
static_cast<int32_t>(frames),
|
|
Move(sampleBuffer),
|
|
mChannels,
|
|
mSampleRate));
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WaveReader::DecodeVideoFrame(bool &aKeyframeSkip,
|
|
int64_t aTimeThreshold)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
return false;
|
|
}
|
|
|
|
RefPtr<MediaDecoderReader::SeekPromise>
|
|
WaveReader::Seek(SeekTarget aTarget, int64_t aEndTime)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
LOG(LogLevel::Debug, ("%p About to seek to %lld", mDecoder, aTarget.GetTime().ToMicroseconds()));
|
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
return SeekPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
|
|
}
|
|
double d = BytesToTime(GetDataLength());
|
|
NS_ASSERTION(d < INT64_MAX / USECS_PER_S, "Duration overflow");
|
|
media::TimeUnit duration = media::TimeUnit::FromSeconds(d);
|
|
double seekTime = std::min(aTarget.GetTime(), duration).ToSeconds();
|
|
int64_t position = RoundDownToFrame(static_cast<int64_t>(TimeToBytes(seekTime)));
|
|
NS_ASSERTION(INT64_MAX - mWavePCMOffset > position, "Integer overflow during wave seek");
|
|
position += mWavePCMOffset;
|
|
nsresult res = mResource.Seek(nsISeekableStream::NS_SEEK_SET, position);
|
|
if (NS_FAILED(res)) {
|
|
return SeekPromise::CreateAndReject(res, __func__);
|
|
} else {
|
|
return SeekPromise::CreateAndResolve(aTarget.GetTime(), __func__);
|
|
}
|
|
}
|
|
|
|
media::TimeIntervals WaveReader::GetBuffered()
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
if (!mInfo.HasAudio()) {
|
|
return media::TimeIntervals();
|
|
}
|
|
media::TimeIntervals buffered;
|
|
AutoPinned<MediaResource> resource(mDecoder->GetResource());
|
|
int64_t startOffset = resource->GetNextCachedData(mWavePCMOffset);
|
|
while (startOffset >= 0) {
|
|
int64_t endOffset = resource->GetCachedDataEnd(startOffset);
|
|
// Bytes [startOffset..endOffset] are cached.
|
|
NS_ASSERTION(startOffset >= mWavePCMOffset, "Integer underflow in GetBuffered");
|
|
NS_ASSERTION(endOffset >= mWavePCMOffset, "Integer underflow in GetBuffered");
|
|
|
|
// We need to round the buffered ranges' times to microseconds so that they
|
|
// have the same precision as the currentTime and duration attribute on
|
|
// the media element.
|
|
buffered += media::TimeInterval(
|
|
media::TimeUnit::FromSeconds(BytesToTime(startOffset - mWavePCMOffset)),
|
|
media::TimeUnit::FromSeconds(BytesToTime(endOffset - mWavePCMOffset)));
|
|
startOffset = resource->GetNextCachedData(endOffset);
|
|
}
|
|
return buffered;
|
|
}
|
|
|
|
bool
|
|
WaveReader::ReadAll(char* aBuf, int64_t aSize, int64_t* aBytesRead)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
if (aBytesRead) {
|
|
*aBytesRead = 0;
|
|
}
|
|
uint32_t read = 0;
|
|
if (NS_FAILED(mResource.Read(aBuf, uint32_t(aSize), &read))) {
|
|
NS_WARNING("Resource read failed");
|
|
return false;
|
|
}
|
|
if (!read) {
|
|
return false;
|
|
}
|
|
if (aBytesRead) {
|
|
*aBytesRead = read;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
WaveReader::LoadRIFFChunk()
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
char riffHeader[RIFF_INITIAL_SIZE];
|
|
const char* p = riffHeader;
|
|
|
|
MOZ_ASSERT(mResource.Tell() == 0,
|
|
"LoadRIFFChunk called when resource in invalid state");
|
|
|
|
if (!ReadAll(riffHeader, sizeof(riffHeader))) {
|
|
return false;
|
|
}
|
|
|
|
static_assert(sizeof(uint32_t) * 3 <= RIFF_INITIAL_SIZE,
|
|
"Reads would overflow riffHeader buffer.");
|
|
if (ReadUint32BE(&p) != RIFF_CHUNK_MAGIC) {
|
|
NS_WARNING("resource data not in RIFF format");
|
|
return false;
|
|
}
|
|
|
|
// Skip over RIFF size field.
|
|
p += sizeof(uint32_t);
|
|
|
|
if (ReadUint32BE(&p) != WAVE_CHUNK_MAGIC) {
|
|
NS_WARNING("Expected WAVE chunk");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
WaveReader::LoadFormatChunk(uint32_t aChunkSize)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
uint32_t rate, channels, frameSize, sampleFormat;
|
|
char waveFormat[WAVE_FORMAT_CHUNK_SIZE];
|
|
const char* p = waveFormat;
|
|
|
|
// RIFF chunks are always word (two byte) aligned.
|
|
MOZ_ASSERT(mResource.Tell() % 2 == 0,
|
|
"LoadFormatChunk called with unaligned resource");
|
|
|
|
if (!ReadAll(waveFormat, sizeof(waveFormat))) {
|
|
return false;
|
|
}
|
|
|
|
static_assert(sizeof(uint16_t) +
|
|
sizeof(uint16_t) +
|
|
sizeof(uint32_t) +
|
|
4 +
|
|
sizeof(uint16_t) +
|
|
sizeof(uint16_t) <= sizeof(waveFormat),
|
|
"Reads would overflow waveFormat buffer.");
|
|
if (ReadUint16LE(&p) != WAVE_FORMAT_ENCODING_PCM) {
|
|
NS_WARNING("WAVE is not uncompressed PCM, compressed encodings are not supported");
|
|
return false;
|
|
}
|
|
|
|
channels = ReadUint16LE(&p);
|
|
rate = ReadUint32LE(&p);
|
|
|
|
// Skip over average bytes per second field.
|
|
p += 4;
|
|
|
|
frameSize = ReadUint16LE(&p);
|
|
|
|
sampleFormat = ReadUint16LE(&p);
|
|
|
|
// PCM encoded WAVEs are not expected to have an extended "format" chunk,
|
|
// but I have found WAVEs that have a extended "format" chunk with an
|
|
// extension size of 0 bytes. Be polite and handle this rather than
|
|
// considering the file invalid. This code skips any extension of the
|
|
// "format" chunk.
|
|
if (aChunkSize > WAVE_FORMAT_CHUNK_SIZE) {
|
|
uint16_t extra = aChunkSize - WAVE_FORMAT_CHUNK_SIZE;
|
|
extra += extra % 2;
|
|
if (NS_FAILED(mResource.Seek(nsISeekableStream::NS_SEEK_CUR, extra))) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// RIFF chunks are always word (two byte) aligned.
|
|
MOZ_ASSERT(mResource.Tell() % 2 == 0,
|
|
"LoadFormatChunk left resource unaligned");
|
|
|
|
// Make sure metadata is fairly sane. The rate check is fairly arbitrary,
|
|
// but the channels check is intentionally limited to mono or stereo
|
|
// when the media is intended for direct playback because that's what the
|
|
// audio backend currently supports.
|
|
unsigned int actualFrameSize = sampleFormat * channels / 8;
|
|
if (rate < 100 || rate > 96000 ||
|
|
(((channels < 1 || channels > MAX_CHANNELS) ||
|
|
(frameSize != 1 && frameSize != 2 && frameSize != 3 &&
|
|
frameSize != 4 && frameSize != 6)) &&
|
|
!mIgnoreAudioOutputFormat) ||
|
|
(sampleFormat != 8 && sampleFormat != 16 && sampleFormat != 24) ||
|
|
frameSize != actualFrameSize) {
|
|
NS_WARNING("Invalid WAVE metadata");
|
|
return false;
|
|
}
|
|
|
|
mSampleRate = rate;
|
|
mChannels = channels;
|
|
mFrameSize = frameSize;
|
|
if (sampleFormat == 8) {
|
|
mSampleFormat = FORMAT_U8;
|
|
} else if (sampleFormat == 24) {
|
|
mSampleFormat = FORMAT_S24;
|
|
} else {
|
|
mSampleFormat = FORMAT_S16;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
WaveReader::FindDataOffset(uint32_t aChunkSize)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
// RIFF chunks are always word (two byte) aligned.
|
|
MOZ_ASSERT(mResource.Tell() % 2 == 0,
|
|
"FindDataOffset called with unaligned resource");
|
|
|
|
int64_t offset = mResource.Tell();
|
|
if (offset <= 0 || offset > UINT32_MAX) {
|
|
NS_WARNING("PCM data offset out of range");
|
|
return false;
|
|
}
|
|
|
|
mWaveLength = aChunkSize;
|
|
mWavePCMOffset = uint32_t(offset);
|
|
return true;
|
|
}
|
|
|
|
double
|
|
WaveReader::BytesToTime(int64_t aBytes) const
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
MOZ_ASSERT(aBytes >= 0, "Must be >= 0");
|
|
return float(aBytes) / mSampleRate / mFrameSize;
|
|
}
|
|
|
|
int64_t
|
|
WaveReader::TimeToBytes(double aTime) const
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
MOZ_ASSERT(aTime >= 0.0f, "Must be >= 0");
|
|
return RoundDownToFrame(int64_t(aTime * mSampleRate * mFrameSize));
|
|
}
|
|
|
|
int64_t
|
|
WaveReader::RoundDownToFrame(int64_t aBytes) const
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
MOZ_ASSERT(aBytes >= 0, "Must be >= 0");
|
|
return aBytes - (aBytes % mFrameSize);
|
|
}
|
|
|
|
int64_t
|
|
WaveReader::GetDataLength()
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
int64_t length = mWaveLength;
|
|
// If the decoder has a valid content length, and it's shorter than the
|
|
// expected length of the PCM data, calculate the playback duration from
|
|
// the content length rather than the expected PCM data length.
|
|
int64_t streamLength = mDecoder->GetResource()->GetLength();
|
|
if (streamLength >= 0) {
|
|
int64_t dataLength = std::max<int64_t>(0, streamLength - mWavePCMOffset);
|
|
length = std::min(dataLength, length);
|
|
}
|
|
return length;
|
|
}
|
|
|
|
int64_t
|
|
WaveReader::GetPosition()
|
|
{
|
|
return mResource.Tell();
|
|
}
|
|
|
|
bool
|
|
WaveReader::LoadListChunk(uint32_t aChunkSize,
|
|
nsAutoPtr<dom::HTMLMediaElement::MetadataTags> &aTags)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
// List chunks are always word (two byte) aligned.
|
|
MOZ_ASSERT(mResource.Tell() % 2 == 0,
|
|
"LoadListChunk called with unaligned resource");
|
|
|
|
static const unsigned int MAX_CHUNK_SIZE = 1 << 16;
|
|
static_assert(uint64_t(MAX_CHUNK_SIZE) < UINT_MAX / sizeof(char),
|
|
"MAX_CHUNK_SIZE too large for enumerator.");
|
|
|
|
if (aChunkSize > MAX_CHUNK_SIZE || aChunkSize < 4) {
|
|
return false;
|
|
}
|
|
|
|
auto chunk = MakeUnique<char[]>(aChunkSize);
|
|
if (!ReadAll(chunk.get(), aChunkSize)) {
|
|
return false;
|
|
}
|
|
|
|
static const uint32_t INFO_LIST_MAGIC = 0x494e464f;
|
|
const char* p = chunk.get();
|
|
if (ReadUint32BE(&p) != INFO_LIST_MAGIC) {
|
|
return false;
|
|
}
|
|
|
|
const waveIdToName ID_TO_NAME[] = {
|
|
{ 0x49415254, NS_LITERAL_CSTRING("artist") }, // IART
|
|
{ 0x49434d54, NS_LITERAL_CSTRING("comments") }, // ICMT
|
|
{ 0x49474e52, NS_LITERAL_CSTRING("genre") }, // IGNR
|
|
{ 0x494e414d, NS_LITERAL_CSTRING("name") }, // INAM
|
|
};
|
|
|
|
const char* const end = chunk.get() + aChunkSize;
|
|
|
|
aTags = new dom::HTMLMediaElement::MetadataTags;
|
|
|
|
while (p + 8 < end) {
|
|
uint32_t id = ReadUint32BE(&p);
|
|
// Uppercase tag id, inspired by GStreamer's Wave parser.
|
|
id &= 0xDFDFDFDF;
|
|
|
|
uint32_t length = ReadUint32LE(&p);
|
|
|
|
// Subchunk shall not exceed parent chunk.
|
|
if (uint32_t(end - p) < length) {
|
|
break;
|
|
}
|
|
|
|
// Wrap the string, adjusting length to account for optional
|
|
// null termination in the chunk.
|
|
nsCString val(p, length);
|
|
if (length > 0 && val[length - 1] == '\0') {
|
|
val.SetLength(length - 1);
|
|
}
|
|
|
|
// Chunks in List::INFO are always word (two byte) aligned. So round up if
|
|
// necessary.
|
|
length += length % 2;
|
|
p += length;
|
|
|
|
if (!IsUTF8(val)) {
|
|
continue;
|
|
}
|
|
|
|
for (size_t i = 0; i < mozilla::ArrayLength(ID_TO_NAME); ++i) {
|
|
if (id == ID_TO_NAME[i].id) {
|
|
aTags->Put(ID_TO_NAME[i].name, val);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
WaveReader::LoadAllChunks(nsAutoPtr<dom::HTMLMediaElement::MetadataTags> &aTags)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
// Chunks are always word (two byte) aligned.
|
|
MOZ_ASSERT(mResource.Tell() % 2 == 0,
|
|
"LoadAllChunks called with unaligned resource");
|
|
|
|
bool loadFormatChunk = false;
|
|
bool findDataOffset = false;
|
|
|
|
for (;;) {
|
|
static const unsigned int CHUNK_HEADER_SIZE = 8;
|
|
char chunkHeader[CHUNK_HEADER_SIZE];
|
|
const char* p = chunkHeader;
|
|
|
|
if (!ReadAll(chunkHeader, sizeof(chunkHeader))) {
|
|
return false;
|
|
}
|
|
|
|
static_assert(sizeof(uint32_t) * 2 <= CHUNK_HEADER_SIZE,
|
|
"Reads would overflow chunkHeader buffer.");
|
|
|
|
uint32_t magic = ReadUint32BE(&p);
|
|
uint32_t chunkSize = ReadUint32LE(&p);
|
|
int64_t chunkStart = GetPosition();
|
|
|
|
switch (magic) {
|
|
case FRMT_CHUNK_MAGIC:
|
|
loadFormatChunk = LoadFormatChunk(chunkSize);
|
|
if (!loadFormatChunk) {
|
|
return false;
|
|
}
|
|
break;
|
|
|
|
case LIST_CHUNK_MAGIC:
|
|
if (!aTags) {
|
|
LoadListChunk(chunkSize, aTags);
|
|
}
|
|
break;
|
|
|
|
case DATA_CHUNK_MAGIC:
|
|
findDataOffset = FindDataOffset(chunkSize);
|
|
return loadFormatChunk && findDataOffset;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
// RIFF chunks are two-byte aligned, so round up if necessary.
|
|
chunkSize += chunkSize % 2;
|
|
|
|
// Move forward to next chunk
|
|
CheckedInt64 forward = CheckedInt64(chunkStart) + chunkSize - GetPosition();
|
|
|
|
if (!forward.isValid() || forward.value() < 0) {
|
|
return false;
|
|
}
|
|
|
|
static const int64_t MAX_CHUNK_SIZE = 1 << 16;
|
|
static_assert(uint64_t(MAX_CHUNK_SIZE) < UINT_MAX / sizeof(char),
|
|
"MAX_CHUNK_SIZE too large for enumerator.");
|
|
auto chunk = MakeUnique<char[]>(MAX_CHUNK_SIZE);
|
|
while (forward.value() > 0) {
|
|
int64_t size = std::min(forward.value(), MAX_CHUNK_SIZE);
|
|
if (!ReadAll(chunk.get(), size)) {
|
|
return false;
|
|
}
|
|
forward -= size;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
} // namespace mozilla
|