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

Support for multiple SPS/PPS, then pick the first one. #2544

Merged
merged 1 commit into from
Aug 26, 2021
Merged
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
62 changes: 38 additions & 24 deletions trunk/src/kernel/srs_kernel_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,41 +806,55 @@ srs_error_t SrsFormat::avc_demux_sps_pps(SrsBuffer* stream)
}
int8_t numOfSequenceParameterSets = stream->read_1bytes();
numOfSequenceParameterSets &= 0x1f;
if (numOfSequenceParameterSets != 1) {
if (numOfSequenceParameterSets < 1) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS");
}
if (!stream->require(2)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS size");
}
uint16_t sequenceParameterSetLength = stream->read_2bytes();
if (!stream->require(sequenceParameterSetLength)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS data");
}
if (sequenceParameterSetLength > 0) {
vcodec->sequenceParameterSetNALUnit.resize(sequenceParameterSetLength);
stream->read_bytes(&vcodec->sequenceParameterSetNALUnit[0], sequenceParameterSetLength);
// Support for multiple SPS, then pick the first non-empty one.
for (int i = 0; i < numOfSequenceParameterSets; ++i) {
if (!stream->require(2)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS size");
}
uint16_t sequenceParameterSetLength = stream->read_2bytes();
if (!stream->require(sequenceParameterSetLength)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS data");
}
if (vcodec->sequenceParameterSetNALUnit.size() > 0) {
stream->skip(sequenceParameterSetLength);
continue;
}
if (sequenceParameterSetLength > 0) {
vcodec->sequenceParameterSetNALUnit.resize(sequenceParameterSetLength);
stream->read_bytes(&vcodec->sequenceParameterSetNALUnit[0], sequenceParameterSetLength);
}
}

// 1 pps
if (!stream->require(1)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS");
}
int8_t numOfPictureParameterSets = stream->read_1bytes();
numOfPictureParameterSets &= 0x1f;
if (numOfPictureParameterSets != 1) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS");
}
if (!stream->require(2)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS size");
}
uint16_t pictureParameterSetLength = stream->read_2bytes();
if (!stream->require(pictureParameterSetLength)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS data");
if (numOfPictureParameterSets < 1) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS");
}
if (pictureParameterSetLength > 0) {
vcodec->pictureParameterSetNALUnit.resize(pictureParameterSetLength);
stream->read_bytes(&vcodec->pictureParameterSetNALUnit[0], pictureParameterSetLength);
// Support for multiple PPS, then pick the first non-empty one.
for (int i = 0; i < numOfPictureParameterSets; ++i) {
if (!stream->require(2)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS size");
}
uint16_t pictureParameterSetLength = stream->read_2bytes();
if (!stream->require(pictureParameterSetLength)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS data");
}
if (vcodec->pictureParameterSetNALUnit.size() > 0) {
stream->skip(pictureParameterSetLength);
continue;
}
if (pictureParameterSetLength > 0) {
vcodec->pictureParameterSetNALUnit.resize(pictureParameterSetLength);
stream->read_bytes(&vcodec->pictureParameterSetNALUnit[0], pictureParameterSetLength);
}
}

return avc_demux_sps();
}

Expand Down
16 changes: 16 additions & 0 deletions trunk/src/utest/srs_utest_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3723,6 +3723,22 @@ VOID TEST(KernelCodecTest, VideoFormatSepcial)
};
HELPER_EXPECT_SUCCESS(f.on_video(0, (char*)buf, sizeof(buf)));
}

if (true) {
SrsFormat f;
HELPER_EXPECT_SUCCESS(f.initialize());
uint8_t buf[] = {
0x17, // 1, Keyframe; 7, AVC.
0x00, // 0, Sequence header.
0x00, 0x00, 0x00, // Timestamp.
// AVC extra data, SPS/PPS.
0x00, 0x00, 0x00, 0x00,
0x00, // lengthSizeMinusOne
0x02, 0x00, 0x00, 0x00, 0x00, // 2 SPS,
0x02, 0x00, 0x00, 0x00, 0x00 // 2 PPS,
};
HELPER_EXPECT_SUCCESS(f.on_video(0, (char*)buf, sizeof(buf)));
}
}

VOID TEST(KernelCodecTest, VideoFormat)
Expand Down