Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H.264 parser and new overloaded makeFrame #380

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/include/H264Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class H264Utils {
static H264_NAL_TYPE getNALUType(const char *buffer);
static H264_NAL_TYPE getNALUType(Frame *frm);
static bool getNALUnit(const char *buffer, size_t length, size_t &offset);
static bool getNALUnitOffsetAndSizeBasedOnGivenType(char* buffer, size_t length, size_t& offset, int& naluSeparatorSize, H264_NAL_TYPE naluType , bool checkByType = true);
static std::tuple<short, const_buffer, const_buffer> parseNalu(const const_buffer input);
static H264_NAL_TYPE getNalTypeAfterSpsPps(void* frameData, size_t frameSize);
static bool extractSpsAndPpsFromExtradata(char* buffer, size_t length, char*& sps, int& spsSize, char*& pps, int& ppsSize);
};
1 change: 1 addition & 0 deletions base/include/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class Module {
frame_sp makeFrame(size_t size); // use only if 1 output pin is there
frame_sp makeFrame();
frame_sp makeFrame(frame_sp& bigFrame, size_t& newSize, string& pinId);
frame_sp makeFrame(frame_sp& frame, size_t& incrementSize);
frame_sp getEOSFrame();
frame_sp getEmptyFrame();

Expand Down
114 changes: 112 additions & 2 deletions base/src/H264Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,125 @@ H264Utils::H264_NAL_TYPE H264Utils::getNALUType(const char* buffer)
bool H264Utils::getNALUnit(const char* buffer, size_t length, size_t& offset)
{
if (length < 3) return false;
size_t cnt = 3;
size_t cnt = 4;

while (cnt < length)
{
if (buffer[cnt - 1] == 0x1 && buffer[cnt - 2] == 0x0 && buffer[cnt - 3] == 0x0)
if (buffer[cnt - 1] == 0x1 && buffer[cnt - 2] == 0x0 && buffer[cnt - 3] == 0x0 && buffer[cnt - 4] == 0x0)
{
offset = cnt;
return true;
}
else if (buffer[cnt - 1] == 0x1 && buffer[cnt - 2] == 0x0 && buffer[cnt - 3] == 0x0)
{
offset = cnt - 1;
return true;
}
cnt++;
}

return false;
}

bool H264Utils::getNALUnitOffsetAndSizeBasedOnGivenType(char* buffer, size_t length, size_t& offset, int& naluSeparatorSize, H264Utils::H264_NAL_TYPE naluType, bool checkByType)
{
if (length < 3) return false;
size_t cnt = 5;

while (cnt < length)
{
if (buffer[cnt - 3] == 0x1 && buffer[cnt - 4] == 0x0 && buffer[cnt - 5] == 0x0)
{
char type = (buffer[cnt - 2] & 0x1F);
if (type == naluType || !checkByType)
{
naluSeparatorSize = 3;
offset = cnt - 2;
return true;
}
}
else if (buffer[cnt - 2] == 0x1 && buffer[cnt - 3] == 0x0 && buffer[cnt - 4] == 0x0 && buffer[cnt - 5] == 0x0)
{
char type = (buffer[cnt - 1] & 0x1F);
if (type == naluType || !checkByType)
{
naluSeparatorSize = 4;
offset = cnt - 1;
return true;
}
}
cnt++;
}

return false;
}

bool H264Utils::extractSpsAndPpsFromExtradata(char* buffer, size_t length, char*& sps, int& spsSize, char*& pps, int& ppsSize)
{
if (length < 3) return false;
size_t cnt = 5;
spsSize = 0;
ppsSize = 0;
while ((cnt < length) && (!spsSize || !ppsSize))
{
if (buffer[cnt - 3] == 0x1 && buffer[cnt - 4] == 0x0 && buffer[cnt - 5] == 0x0)
{
char type = (buffer[cnt - 2] & 0x1F);
if (type == H264Utils::H264_NAL_TYPE_SEQ_PARAM)
{
sps = buffer + (cnt - 5);
size_t offset = 0;
int naluSeparatorSize = 0;
getNALUnitOffsetAndSizeBasedOnGivenType(buffer + (cnt - 2), length - (cnt - 2), offset, naluSeparatorSize, H264Utils::H264_NAL_TYPE::H264_NAL_TYPE_NON_IDR_SLICE, false);
spsSize = offset + 3 - naluSeparatorSize;
cnt += offset - naluSeparatorSize;
}
if (type == H264Utils::H264_NAL_TYPE_PIC_PARAM)
{
pps = buffer + (cnt - 5);
size_t offset = 0;
int naluSeparatorSize = 0;
getNALUnitOffsetAndSizeBasedOnGivenType(buffer + (cnt - 2), length - (cnt - 2), offset, naluSeparatorSize, H264Utils::H264_NAL_TYPE::H264_NAL_TYPE_NON_IDR_SLICE, false);
if (!offset && !naluSeparatorSize)
{
ppsSize = length - (cnt - 5);
}
else
{
ppsSize = offset + 3 - naluSeparatorSize;
}
cnt += offset - naluSeparatorSize;
}
}
else if (buffer[cnt - 2] == 0x1 && buffer[cnt - 3] == 0x0 && buffer[cnt - 4] == 0x0 && buffer[cnt - 5] == 0x0)
{
char type = (buffer[cnt - 1] & 0x1F);
if (type == H264Utils::H264_NAL_TYPE_SEQ_PARAM)
{
sps = buffer + (cnt - 5);
size_t offset = 0;
int naluSeparatorSize = 0;
getNALUnitOffsetAndSizeBasedOnGivenType(buffer + (cnt - 1), length - (cnt - 1), offset, naluSeparatorSize, H264Utils::H264_NAL_TYPE::H264_NAL_TYPE_NON_IDR_SLICE, false);
spsSize = offset + 4 - naluSeparatorSize;
cnt += offset - naluSeparatorSize;
}
if (type == H264Utils::H264_NAL_TYPE_PIC_PARAM)
{
pps = buffer + (cnt - 5);
size_t offset = 0;
int naluSeparatorSize = 0;
getNALUnitOffsetAndSizeBasedOnGivenType(buffer + (cnt - 1), length - (cnt - 1), offset, naluSeparatorSize, H264Utils::H264_NAL_TYPE::H264_NAL_TYPE_NON_IDR_SLICE, false);
if (!offset && !naluSeparatorSize)
{
ppsSize = length - (cnt - 5);
}
else
{
ppsSize = offset + 4 - naluSeparatorSize;
}
cnt += offset - naluSeparatorSize;
}
}
cnt++;
}

Expand Down
6 changes: 6 additions & 0 deletions base/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,12 @@ frame_sp Module::makeFrame(frame_sp &bigFrame, size_t &size, string &pinId)
return mOutputPinIdFrameFactoryMap[pinId]->create(bigFrame, size, mOutputPinIdFrameFactoryMap[pinId]);
}

frame_sp Module::makeFrame(frame_sp& frame, size_t& incrementSize)
{
*frame += incrementSize;
return frame;
}

void Module::setMetadata(std::string& pinId, framemetadata_sp& metadata){
mOutputPinIdFrameFactoryMap[pinId]->setMetadata(metadata);
return;
Expand Down
Loading
Loading