Skip to content

Commit

Permalink
Add explicit frame parse reset method
Browse files Browse the repository at this point in the history
The frame size may be determined outside the parser (e.g. if the
remaining bytes are assumed to be part of the frame) and this method
allows the state to be reset.
  • Loading branch information
philipnbbc committed Sep 3, 2024
1 parent 6dfa618 commit dd24bd6
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 60 deletions.
1 change: 1 addition & 0 deletions apps/bmxparse/bmxparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,7 @@ int main(int argc, const char **argv)

frame_start = frame_size.GetSize();
frame_size.Reset();
parser->ResetParseFrameSize();
frame_count++;

if (end_of_data)
Expand Down
5 changes: 4 additions & 1 deletion include/bmx/essence_parser/AVCEssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class AVCCodedPictureEssenceParser : public EssenceParser
void SetPPS(const unsigned char *data, uint32_t size);

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);

virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);
Expand Down Expand Up @@ -339,7 +341,6 @@ class AVCCodedPictureEssenceParser : public EssenceParser
void SetSPSData(uint8_t id, const unsigned char *data, uint32_t size);
void SetPPSData(uint8_t id, const unsigned char *data, uint32_t size);

void ResetFrameSize();
void ResetFrameInfo();

private:
Expand Down Expand Up @@ -398,6 +399,8 @@ class AVCEssenceParser : public EssenceParser
void SetPPS(const unsigned char *data, uint32_t size);

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual ParsedFrameSize ParseFrameSize2(const unsigned char *data, uint32_t data_size);

virtual ParsedFrameSize ParseFrameInfo2(const unsigned char *data, ParsedFrameSize frame_size);
Expand Down
2 changes: 2 additions & 0 deletions include/bmx/essence_parser/DVEssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class DVEssenceParser : public EssenceParser
virtual ~DVEssenceParser();

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);

virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);
Expand Down
1 change: 1 addition & 0 deletions include/bmx/essence_parser/EssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class EssenceParser

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size) = 0;

virtual void ResetParseFrameSize() = 0;
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);
virtual ParsedFrameSize ParseFrameSize2(const unsigned char *data, uint32_t data_size);

Expand Down
2 changes: 2 additions & 0 deletions include/bmx/essence_parser/J2CEssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class J2CEssenceParser : public EssenceParser
virtual ~J2CEssenceParser();

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);

virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);
Expand Down
1 change: 1 addition & 0 deletions include/bmx/essence_parser/JXSEssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ namespace bmx
virtual ~JXSEssenceParser() {}

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);
virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);
virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);

Expand Down
5 changes: 2 additions & 3 deletions include/bmx/essence_parser/MJPEGEssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ class MJPEGEssenceParser : public EssenceParser
virtual ~MJPEGEssenceParser();

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);

virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);

private:
void Reset();

private:
bool mSingleField;

Expand Down
3 changes: 2 additions & 1 deletion include/bmx/essence_parser/MPEG2EssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class MPEG2EssenceParser : public EssenceParser
virtual ~MPEG2EssenceParser();

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);

virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);
Expand Down Expand Up @@ -96,7 +98,6 @@ class MPEG2EssenceParser : public EssenceParser
bool IsTFF() const { return mIsTFF; }

private:
void ResetFrameSize();
void ResetFrameInfo();

private:
Expand Down
2 changes: 2 additions & 0 deletions include/bmx/essence_parser/RDD36EssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class RDD36EssenceParser : public EssenceParser
virtual ~RDD36EssenceParser();

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);

virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);
Expand Down
3 changes: 2 additions & 1 deletion include/bmx/essence_parser/VC2EssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class VC2EssenceParser : public EssenceParser
virtual ~VC2EssenceParser();

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);

virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);
Expand Down Expand Up @@ -176,7 +178,6 @@ class VC2EssenceParser : public EssenceParser
bool ParseSourceParameters(VC2GetBitBuffer *buffer, SequenceHeader *sequence_header);
void SetCodingParameters(SequenceHeader *sequence_header);

void ResetFrameSize();
void ResetFrameInfo();

private:
Expand Down
2 changes: 2 additions & 0 deletions include/bmx/essence_parser/VC3EssenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class VC3EssenceParser : public EssenceParser
virtual ~VC3EssenceParser();

virtual uint32_t ParseFrameStart(const unsigned char *data, uint32_t data_size);

virtual void ResetParseFrameSize();
virtual uint32_t ParseFrameSize(const unsigned char *data, uint32_t data_size);

virtual void ParseFrameInfo(const unsigned char *data, uint32_t data_size);
Expand Down
31 changes: 17 additions & 14 deletions src/essence_parser/AVCEssenceParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ AVCCodedPictureEssenceParser::POCInfo::~POCInfo()

AVCCodedPictureEssenceParser::AVCCodedPictureEssenceParser()
{
ResetFrameSize();
ResetParseFrameSize();
ResetFrameInfo();
}

Expand Down Expand Up @@ -469,6 +469,13 @@ uint32_t AVCCodedPictureEssenceParser::ParseFrameStart(const unsigned char *data
return offset - 1;
}

void AVCCodedPictureEssenceParser::ResetParseFrameSize()
{
mHavePrimPicSliceHeader = false;
memset(&mPrimPicSliceHeader, 0, sizeof(mPrimPicSliceHeader));
mOffset = 0;
}

uint32_t AVCCodedPictureEssenceParser::ParseFrameSize(const unsigned char *data, uint32_t data_size)
{
BMX_CHECK(data_size < ESSENCE_PARSER_NULL_OFFSET);
Expand All @@ -482,7 +489,7 @@ uint32_t AVCCodedPictureEssenceParser::ParseFrameSize(const unsigned char *data,
// access unit's NAL unit

if (mOffset == 0)
ResetFrameSize();
ResetParseFrameSize();

bool have_frame_end = false;
bool have_issue = false;
Expand Down Expand Up @@ -1867,13 +1874,6 @@ void AVCCodedPictureEssenceParser::SetPPSData(uint8_t id, const unsigned char *d
mPPSData[id] = new ParamSetData(data, size);
}

void AVCCodedPictureEssenceParser::ResetFrameSize()
{
mHavePrimPicSliceHeader = false;
memset(&mPrimPicSliceHeader, 0, sizeof(mPrimPicSliceHeader));
mOffset = 0;
}

void AVCCodedPictureEssenceParser::ResetFrameInfo()
{
mHavePrimPicSliceHeader = false;
Expand Down Expand Up @@ -1945,10 +1945,16 @@ uint32_t AVCEssenceParser::ParseFrameStart(const unsigned char *data, uint32_t d
return mCodedPictureParser.ParseFrameStart(data, data_size);
}

void AVCEssenceParser::ResetParseFrameSize()
{
mCodedPictureParser.ResetParseFrameSize();
mParsedFrameSize.Reset();
}

ParsedFrameSize AVCEssenceParser::ParseFrameSize2(const unsigned char *data, uint32_t data_size)
{
if (mParsedFrameSize.IsComplete())
mParsedFrameSize.Reset();
ResetParseFrameSize();

if (mParsedFrameSize.HaveFirstField()) {
// It is field coded and the first field has been parsed
Expand Down Expand Up @@ -2038,10 +2044,7 @@ ParsedFrameSize AVCEssenceParser::ParseFrameInfo2(const unsigned char *data, Par

// Parse the field sizes if the frame_size passed in doesn't have the second field offset
if (frame_size.IsFrame()) {
// The frame size might've been completed outside ParseFrameSize2 using remaining available
// data. That's why an explicit frame size parsing state reset is required here
mParsedFrameSize.Reset();

ResetParseFrameSize();
ParseFrameSize2(data, frame_size.GetFirstFieldOrFrameSize());

// The second field size won't be known from parsing because there is no next picture.
Expand Down
4 changes: 4 additions & 0 deletions src/essence_parser/DVEssenceParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ uint32_t DVEssenceParser::ParseFrameStart(const unsigned char *data, uint32_t da
return 0;
}

void DVEssenceParser::ResetParseFrameSize()
{
}

uint32_t DVEssenceParser::ParseFrameSize(const unsigned char *data, uint32_t data_size)
{
BMX_ASSERT(DV_PARSER_MIN_DATA_SIZE >= DV_DIF_SEQUENCE_SIZE);
Expand Down
4 changes: 4 additions & 0 deletions src/essence_parser/J2CEssenceParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ uint32_t J2CEssenceParser::ParseFrameStart(const unsigned char *data, uint32_t d
return 0;
}

void J2CEssenceParser::ResetParseFrameSize()
{
}

uint32_t J2CEssenceParser::ParseFrameSize(const unsigned char *data, uint32_t data_size)
{
BMX_CHECK(data_size != ESSENCE_PARSER_NULL_OFFSET);
Expand Down
4 changes: 4 additions & 0 deletions src/essence_parser/JXSEssenceParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ uint32_t JXSEssenceParser::ParseFrameStart(const unsigned char *data, uint32_t d
return 0;
}

void JXSEssenceParser::ResetParseFrameSize()
{
}

uint32_t JXSEssenceParser::ParseFrameSize(const unsigned char *data, uint32_t data_size)
{
BMX_CHECK(data_size != ESSENCE_PARSER_NULL_OFFSET);
Expand Down
27 changes: 13 additions & 14 deletions src/essence_parser/MJPEGEssenceParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using namespace bmx;
MJPEGEssenceParser::MJPEGEssenceParser(bool single_field)
{
mSingleField = single_field;
Reset();
ResetParseFrameSize();
}

MJPEGEssenceParser::~MJPEGEssenceParser()
Expand All @@ -60,6 +60,16 @@ uint32_t MJPEGEssenceParser::ParseFrameStart(const unsigned char *data, uint32_t
return 0;
}

void MJPEGEssenceParser::ResetParseFrameSize()
{
mOffset = 0;
mState = 0;
mHaveLenByte1 = false;
mHaveLenByte2 = false;
mSkipCount = 0;
mFieldCount = 0;
}

uint32_t MJPEGEssenceParser::ParseFrameSize(const unsigned char *data, uint32_t data_size)
{
// states
Expand All @@ -86,7 +96,7 @@ uint32_t MJPEGEssenceParser::ParseFrameSize(const unsigned char *data, uint32_t
mState = 1;
} else {
// invalid image start
Reset();
ResetParseFrameSize();
return ESSENCE_PARSER_NULL_FRAME_SIZE;
}
break;
Expand All @@ -105,7 +115,7 @@ uint32_t MJPEGEssenceParser::ParseFrameSize(const unsigned char *data, uint32_t
{
uint32_t image_size = mOffset + 1;
if (mSingleField || mFieldCount == 1) {
Reset();
ResetParseFrameSize();
return image_size;
}
mFieldCount++;
Expand Down Expand Up @@ -158,14 +168,3 @@ void MJPEGEssenceParser::ParseFrameInfo(const unsigned char *data, uint32_t data
(void)data;
(void)data_size;
}

void MJPEGEssenceParser::Reset()
{
mOffset = 0;
mState = 0;
mHaveLenByte1 = false;
mHaveLenByte2 = false;
mSkipCount = 0;
mFieldCount = 0;
}

24 changes: 12 additions & 12 deletions src/essence_parser/MPEG2EssenceParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static const FrameRateMap FRAME_RATE_MAP[] =

MPEG2EssenceParser::MPEG2EssenceParser()
{
ResetFrameSize();
ResetParseFrameSize();
ResetFrameInfo();

mHorizontalSize = 0;
Expand Down Expand Up @@ -137,6 +137,15 @@ uint32_t MPEG2EssenceParser::ParseFrameStart(const unsigned char *data, uint32_t
return ESSENCE_PARSER_NULL_OFFSET;
}

void MPEG2EssenceParser::ResetParseFrameSize()
{
mOffset = 0;
mState = 0xffffffff;
mSequenceHeader = false;
mGroupHeader = false;
mPictureStart = false;
}

uint32_t MPEG2EssenceParser::ParseFrameSize(const unsigned char *data, uint32_t data_size)
{
BMX_CHECK(data_size != ESSENCE_PARSER_NULL_OFFSET);
Expand All @@ -156,7 +165,7 @@ uint32_t MPEG2EssenceParser::ParseFrameSize(const unsigned char *data, uint32_t
(mState == PICTURE_START_CODE && mPictureStart))
{
uint32_t frame_size = mOffset - 3;
ResetFrameSize();
ResetParseFrameSize();
return frame_size;
}

Expand All @@ -167,7 +176,7 @@ uint32_t MPEG2EssenceParser::ParseFrameSize(const unsigned char *data, uint32_t
else if (mOffset == 3)
{
// not a valid frame start
ResetFrameSize();
ResetParseFrameSize();
return ESSENCE_PARSER_NULL_FRAME_SIZE;
}

Expand Down Expand Up @@ -402,15 +411,6 @@ void MPEG2EssenceParser::ParseFrameAllInfo(const unsigned char *data, uint32_t d
}
}

void MPEG2EssenceParser::ResetFrameSize()
{
mOffset = 0;
mState = 0xffffffff;
mSequenceHeader = false;
mGroupHeader = false;
mPictureStart = false;
}

void MPEG2EssenceParser::ResetFrameInfo()
{
mHaveSequenceHeader = false;
Expand Down
4 changes: 4 additions & 0 deletions src/essence_parser/RDD36EssenceParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ uint32_t RDD36EssenceParser::ParseFrameStart(const unsigned char *data, uint32_t
return 0;
}

void RDD36EssenceParser::ResetParseFrameSize()
{
}

uint32_t RDD36EssenceParser::ParseFrameSize(const unsigned char *data, uint32_t data_size)
{
BMX_CHECK(data_size != ESSENCE_PARSER_NULL_OFFSET);
Expand Down
2 changes: 2 additions & 0 deletions src/essence_parser/RawEssenceReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ bool RawEssenceReader::ReadAndParseSample()
sample_num_read += ReadBytes(mReadBlockSize);
}

mEssenceParser->ResetParseFrameSize();

ParsedFrameSize sample_size;
while (true) {
sample_size = mEssenceParser->ParseFrameSize2(mSampleBuffer.GetBytes() + sample_start_offset, sample_num_read);
Expand Down
Loading

0 comments on commit dd24bd6

Please sign in to comment.