Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1298710 - Remove ByteReader::DiscardRemaining and AutoByteReader …
Browse files Browse the repository at this point in the history
…- r=jya

DiscardRemaning was needed to prevent debug-time assertion that the buffer was
read completely or explicitly discarded.

However this required extra work in cases where buffer didn't need to be read
to the end.
And also it could cause crashes (in debug versions) if a buffer was not fully
read, be it because the parser was incorrect or because the media file itself
was wrong (though possibly still readable despite that).
Finding parser issues is still possible by manually instrumenting ByteReader
during development.
And reading media file with small recoverable errors is a bonus.

MozReview-Commit-ID: 2RUYzaYAeRW
  • Loading branch information
squelart committed Sep 4, 2016
1 parent b065628 commit 6e79122
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 41 deletions.
2 changes: 0 additions & 2 deletions dom/media/MP3Demuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ MP3TrackDemuxer::FindNextFrame() {
// If we've found neither an MPEG frame header nor an ID3v2 tag,
// the reader shouldn't have any bytes remaining.
MOZ_ASSERT(foundFrame || bytesToSkip || !reader.Remaining());
reader.DiscardRemaining();

if (foundFrame && mParser.FirstFrame().Length() &&
!VerifyFrameConsistency(mParser.FirstFrame(), mParser.CurrentFrame())) {
Expand Down Expand Up @@ -584,7 +583,6 @@ MP3TrackDemuxer::GetNextFrame(const MediaByteRange& aRange) {
// First frame parsed, let's read VBR info if available.
ByteReader reader(frame->Data(), frame->Size());
mParser.ParseVBRHeader(&reader);
reader.DiscardRemaining();
mFirstFrameOffset = frame->mOffset;
}

Expand Down
8 changes: 4 additions & 4 deletions dom/media/flac/FlacFrameParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "OggCodecState.h"
#include "VideoUtils.h"

using mp4_demuxer::AutoByteReader;
using mp4_demuxer::ByteReader;

namespace mozilla
{
Expand Down Expand Up @@ -62,7 +62,7 @@ FlacFrameParser::DecodeHeaderBlock(const uint8_t* aPacket, size_t aLength)
// Not a header block.
return false;
}
AutoByteReader br(aPacket, aLength);
ByteReader br(aPacket, aLength);

mPacketCount++;

Expand Down Expand Up @@ -210,11 +210,11 @@ FlacFrameParser::IsHeaderBlock(const uint8_t* aPacket, size_t aLength) const
}
if (aPacket[0] == 0x7f) {
// Ogg packet
AutoByteReader br(aPacket + 1, aLength - 1);
ByteReader br(aPacket + 1, aLength - 1);
const uint8_t* signature = br.Read(4);
return signature && !memcmp(signature, "FLAC", 4);
}
AutoByteReader br(aPacket, aLength - 1);
ByteReader br(aPacket, aLength - 1);
const uint8_t* signature = br.Read(4);
if (signature && !memcmp(signature, "fLaC", 4)) {
// Flac start header, must have STREAMINFO as first metadata block;
Expand Down
1 change: 0 additions & 1 deletion dom/media/mediasource/ContainerParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ class MP4ContainerParser : public ContainerParser {
}
reader.Read(size - 8);
}
reader.DiscardRemaining();
}

bool StartWithInitSegment()
Expand Down
2 changes: 0 additions & 2 deletions dom/media/platforms/agnostic/WAVDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ WaveDataDecoder::DoDecode(MediaRawData* aSample)
}
}

aReader.DiscardRemaining();

int64_t duration = frames / mInfo.mRate;

mCallback->Output(new AudioData(aOffset,
Expand Down
2 changes: 0 additions & 2 deletions media/libstagefright/binding/AnnexB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ AnnexB::ConvertExtraDataToAnnexB(const mozilla::MediaByteBuffer* aExtraData)

// MP4Box adds extra bytes that we ignore. I don't know what they do.
}
reader.DiscardRemaining();

return annexB.forget();
}
Expand Down Expand Up @@ -330,7 +329,6 @@ AnnexB::HasSPS(const mozilla::MediaByteBuffer* aExtraData)
return false;
}
uint8_t numSps = reader.ReadU8() & 0x1f;
reader.DiscardRemaining();

return numSps > 0;
}
Expand Down
4 changes: 0 additions & 4 deletions media/libstagefright/binding/H264.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,12 @@ H264::DecodeSPSFromExtraData(const mozilla::MediaByteBuffer* aExtraData, SPSData

if (!(reader.ReadU8() & 0x1f)) {
// No SPS.
reader.DiscardRemaining();
return false;
}
uint16_t length = reader.ReadU16();

if ((reader.PeekU8() & 0x1f) != 7) {
// Not a SPS NAL type.
reader.DiscardRemaining();
return false;
}

Expand All @@ -438,8 +436,6 @@ H264::DecodeSPSFromExtraData(const mozilla::MediaByteBuffer* aExtraData, SPSData
return false;
}

reader.DiscardRemaining();

RefPtr<mozilla::MediaByteBuffer> rawNAL = new mozilla::MediaByteBuffer;
rawNAL->AppendElements(ptr, length);

Expand Down
4 changes: 2 additions & 2 deletions media/libstagefright/binding/include/mp4_demuxer/Box.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ class BoxReader
, mReader(mBuffer.Elements(), mBuffer.Length())
{
}
AutoByteReader* operator->() { return &mReader; }
ByteReader* operator->() { return &mReader; }

private:
nsTArray<uint8_t> mBuffer;
AutoByteReader mReader;
ByteReader mReader;
};
}

Expand Down
24 changes: 0 additions & 24 deletions media/libstagefright/binding/include/mp4_demuxer/ByteReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,13 @@ class MOZ_RAII ByteReader

~ByteReader()
{
NS_ASSERTION(!mRemaining, "Not all bytes have been processed");
}

size_t Offset()
{
return mLength - mRemaining;
}

// Make it explicit if we're not using the extra bytes.
void DiscardRemaining()
{
mRemaining = 0;
}

size_t Remaining() const { return mRemaining; }

bool CanRead8() { return mRemaining >= 1; }
Expand Down Expand Up @@ -351,23 +344,6 @@ class MOZ_RAII ByteReader
size_t mLength;
};

// A ByteReader that automatically discards remaining data before destruction.
class MOZ_RAII AutoByteReader : public ByteReader
{
public:
AutoByteReader(const uint8_t* aData, size_t aSize)
: ByteReader(aData, aSize)
{
}
~AutoByteReader()
{
ByteReader::DiscardRemaining();
}

// Prevent unneeded DiscardRemaining() calls.
void DiscardRemaining() = delete;
};

} // namespace mp4_demuxer

#endif

0 comments on commit 6e79122

Please sign in to comment.