Bug 1487797 - P1. Move sample index from FrameHeader to Frame. r=jya

Differential Revision: https://phabricator.services.mozilla.com/D9043

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Chun-Min Chang 2018-10-22 22:51:21 +00:00
Родитель 9b692b9cbd
Коммит 4deffa6a58
1 изменённых файлов: 29 добавлений и 20 удалений

Просмотреть файл

@ -36,9 +36,6 @@ public:
bool IsValid() const { return mValid; } bool IsValid() const { return mValid; }
// Return the index (in samples) from the beginning of the track.
int64_t Index() const { return mIndex; }
// Parse the current packet and check that it made a valid flac frame header. // Parse the current packet and check that it made a valid flac frame header.
// From https://xiph.org/flac/format.html#frame_header // From https://xiph.org/flac/format.html#frame_header
// A valid header is one that can be decoded without error and that has a // A valid header is one that can be decoded without error and that has a
@ -87,11 +84,11 @@ public:
} }
// Sample or frame count. // Sample or frame count.
int64_t frame_or_sample_num = br.ReadUTF8(); int64_t frameOrSampleNum = br.ReadUTF8();
if (frame_or_sample_num < 0) { if (frameOrSampleNum < 0) {
// Sample/frame number invalid.
return false; return false;
} }
mFrameOrSampleNum = frameOrSampleNum;
// Blocksize // Blocksize
if (bs_code == 0) { if (bs_code == 0) {
@ -105,13 +102,6 @@ public:
mBlocksize = FlacBlocksizeTable[bs_code]; mBlocksize = FlacBlocksizeTable[bs_code];
} }
// The sample index is either:
// 1- coded sample number if blocksize is variable or
// 2- coded frame number if blocksize is known.
// A frame is made of Blocksize sample.
mIndex = mVariableBlockSize ? frame_or_sample_num
: frame_or_sample_num * mBlocksize;
// Sample rate. // Sample rate.
if (sr_code < 12) { if (sr_code < 12) {
mInfo.mRate = FlacSampleRateTable[sr_code]; mInfo.mRate = FlacSampleRateTable[sr_code];
@ -152,8 +142,11 @@ private:
FLAC_CHMODE_MID_SIDE, FLAC_CHMODE_MID_SIDE,
}; };
AudioInfo mInfo; AudioInfo mInfo;
// Index in samples from start; // mFrameOrSampleNum is either:
int64_t mIndex = 0; // 1- coded sample number if blocksize is variable or
// 2- coded frame number if blocksize is fixed.
// A frame is made of Blocksize sample.
uint64_t mFrameOrSampleNum = 0;
bool mVariableBlockSize = false; bool mVariableBlockSize = false;
uint32_t mBlocksize = 0;; uint32_t mBlocksize = 0;;
uint32_t mSize = 0; uint32_t mSize = 0;
@ -290,6 +283,7 @@ public:
if (foundOffset >= 0) { if (foundOffset >= 0) {
SetOffset(aResource, foundOffset + offset); SetOffset(aResource, foundOffset + offset);
SetIndex();
return true; return true;
} }
@ -318,10 +312,13 @@ public:
void SetEndOffset(int64_t aOffset) { mSize = aOffset - mOffset; } void SetEndOffset(int64_t aOffset) { mSize = aOffset - mOffset; }
void SetEndTime(int64_t aIndex) // Return the index (in samples) from the beginning of the track.
uint64_t Index() const { return mIndex; }
void SetEndTime(uint64_t aIndex)
{ {
if (aIndex > Header().mIndex) { if (aIndex > Index()) {
mDuration = aIndex - Header().mIndex; mDuration = aIndex - Index();
} }
} }
@ -333,7 +330,7 @@ public:
return TimeUnit::Invalid(); return TimeUnit::Invalid();
} }
MOZ_ASSERT(Header().Info().mRate, "Invalid Frame. Need Header"); MOZ_ASSERT(Header().Info().mRate, "Invalid Frame. Need Header");
return FramesToTimeUnit(Header().mIndex, Header().Info().mRate); return FramesToTimeUnit(Index(), Header().Info().mRate);
} }
TimeUnit Duration() const TimeUnit Duration() const
@ -368,6 +365,18 @@ private:
aResource.Seek(SEEK_SET, mOffset); aResource.Seek(SEEK_SET, mOffset);
} }
void SetIndex()
{
// Make sure the header has been parsed.
MOZ_ASSERT(Header().mBlocksize);
mIndex = Header().mVariableBlockSize
? Header().mFrameOrSampleNum
: Header().mFrameOrSampleNum * Header().mBlocksize;
}
// The index in samples from start.
uint64_t mIndex = 0;
// The offset to the start of the header. // The offset to the start of the header.
int64_t mOffset = 0; int64_t mOffset = 0;
uint32_t mSize = 0; uint32_t mSize = 0;
@ -413,7 +422,7 @@ public:
mFrame.SetEndOffset(aResource.Tell()); mFrame.SetEndOffset(aResource.Tell());
} else if (mNextFrame.IsValid()) { } else if (mNextFrame.IsValid()) {
mFrame.SetEndOffset(mNextFrame.Offset()); mFrame.SetEndOffset(mNextFrame.Offset());
mFrame.SetEndTime(mNextFrame.Header().Index()); mFrame.SetEndTime(mNextFrame.Index());
} }
} }