Skip to content

[what][bugfix][h264] 修复若干问题 #40

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

Merged
merged 2 commits into from
Jul 29, 2024
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
37 changes: 24 additions & 13 deletions H264Deserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ bool H264Deserialize::DeserializeSeiSyntax(H26xBinaryReader::ptr br, H264SeiSynt
break;
}
default:
br->Skip(sei->payloadSize * 8);
br->Skip((size_t)(sei->payloadSize * 8));
break;
}
return true;
Expand Down Expand Up @@ -723,6 +723,11 @@ bool H264Deserialize::DeserializeSpsSyntax(H26xBinaryReader::ptr br, H264SpsSynt
}
}
}
else
{
// Hint : When chroma_format_idc is not present, it shall be inferred to be equal to 1 (4:2:0 chroma format).
sps->chroma_format_idc = 1;
}
br->UE(sps->log2_max_frame_num_minus4);
{
// Hint : The value of log2_max_frame_num_minus4 shall be in the range of 0 to 12, inclusive.
Expand Down Expand Up @@ -779,7 +784,10 @@ bool H264Deserialize::DeserializeSpsSyntax(H26xBinaryReader::ptr br, H264SpsSynt
return false;
}
}
br->rbsp_trailing_bits();
if (!br->Eof())
{
br->rbsp_trailing_bits();
}
FillH264SpsContext(sps);
_contex->spsSet[sps->seq_parameter_set_id] = sps;
_contex->sps = sps;
Expand Down Expand Up @@ -1326,26 +1334,29 @@ bool H264Deserialize::DeserializePpsSyntax(H26xBinaryReader::ptr br, H264PpsSynt
}
}
}
br->SE(pps->second_chroma_qp_index_offset);
{
// Hint : second_chroma_qp_index_offset specifies the offset that shall be added to QPY and QSY for addressing the table of
// QPC values for the Cr chroma component. The value of second_chroma_qp_index_offset shall be in the range of −12 to
// +12, inclusive.
MPP_H26X_SYNTAXT_STRICT_CHECK(pps->second_chroma_qp_index_offset >= -12 && pps->second_chroma_qp_index_offset <= 12, "[sps] second_chroma_qp_index_offset out of range", return false);
}
}
else
br->SE(pps->second_chroma_qp_index_offset);
{
// Hint : When second_chroma_qp_index_offset is not present, it shall be inferred to be equal to chroma_qp_index_offset
pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
// Hint : second_chroma_qp_index_offset specifies the offset that shall be added to QPY and QSY for addressing the table of
// QPC values for the Cr chroma component. The value of second_chroma_qp_index_offset shall be in the range of −12 to
// +12, inclusive.
MPP_H26X_SYNTAXT_STRICT_CHECK(pps->second_chroma_qp_index_offset >= -12 && pps->second_chroma_qp_index_offset <= 12, "[sps] second_chroma_qp_index_offset out of range", return false);
}
}
else
{
// Hint : When second_chroma_qp_index_offset is not present, it shall be inferred to be equal to chroma_qp_index_offset
pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
}
if (!pps->pic_scaling_matrix_present_flag)
{
pps->ScalingList4x4 = sps->ScalingList4x4;
pps->ScalingList8x8 = sps->ScalingList8x8;
}
br->rbsp_trailing_bits();
if (!br->Eof())
{
br->rbsp_trailing_bits();
}
_contex->ppsSet[pps->pic_parameter_set_id] = pps;
_contex->pps = pps;
return true;
Expand Down
25 changes: 19 additions & 6 deletions H264SliceDecodingProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ namespace Codec
#endif /* ENABLE_MMP_SD_DEBUG */

#if ENABLE_MMP_SD_DEBUG

#if defined(_WIN32)
#define MPP_H264_SD_LOG(fmt, ...) do {\
char buf[512] = {0};\
sprintf_s(buf, fmt, ## __VA_ARGS__);\
H26x_LOG_INFO << buf << H26x_LOG_TERMINATOR;\
} while(0);

#else

#define MPP_H264_SD_LOG(fmt, ...) do {\
char buf[512] = {0};\
sprintf(buf, fmt, ## __VA_ARGS__);\
H26x_LOG_INFO << buf << H26x_LOG_TERMINATOR;\
} while(0);

#endif

#else
#define MPP_H264_SD_LOG(fmt, ...)
#endif /* ENABLE_MMP_SD_DEBUG */
Expand Down Expand Up @@ -172,7 +185,7 @@ static void UnMarkUsedForShortTermReference(H264PictureContext::cache pictures,
// Hint : not support for now
assert(false);
}
MPP_H264_SD_LOG("[RF] UnMarkUsedForShortTermReference, PicNum(%ld) FrameNum(%d)", _picture->PicNum, _picture->FrameNum);
MPP_H264_SD_LOG("[RF] UnMarkUsedForShortTermReference, PicNum(%lld) FrameNum(%d)", _picture->PicNum, _picture->FrameNum);
// Hint : 参考 FFmpeg 6.x 以及 openh264 , 此处应当只 umark 一个 short term picture, 按照 DPB 的顺序
// 同时 ISO 中也存在 `a short-term reference picture` 而非 `all short-term refernce pictures`
break;
Expand Down Expand Up @@ -481,7 +494,7 @@ void H264SliceDecodingProcess::DecodeH264PictureOrderCountType1(H264PictureConte
expectedPicOrderCnt = picOrderCntCycleCnt * ExpectedDeltaPerPicOrderCntCycle;
for (int64_t i=0; i<=frameNumInPicOrderCntCycle; i++)
{
expectedPicOrderCnt = expectedPicOrderCnt + sps->offset_for_ref_frame[i];
expectedPicOrderCnt = expectedPicOrderCnt + sps->offset_for_ref_frame[(size_t)i];
}
}
else
Expand Down Expand Up @@ -1061,7 +1074,7 @@ void H264SliceDecodingProcess::ModificationProcessForReferencePictureLists(H264S
{
RefPicListX[cIdx] = RefPicListX[cIdx-1];
}
MPP_H264_SD_LOG("[MRPL] refIdxLX(%d) abs_diff_pic_num_minus1(%d) picNumLX(%ld)", refIdxLX, abs_diff_pic_num_minus1, picNumLX);
MPP_H264_SD_LOG("[MRPL] refIdxLX(%d) abs_diff_pic_num_minus1(%d) picNumLX(%lld)", refIdxLX, abs_diff_pic_num_minus1, picNumLX);
RefPicListX[refIdxLX++] = FindPictureByPicNum(pictures, picNumLX); // short-term reference picture with PicNum equal to picNumLX
uint32_t nIdx = refIdxLX;
for (uint32_t cIdx = refIdxLX; cIdx <= num_ref_idx_lX_active_minus1+1; cIdx++)
Expand Down Expand Up @@ -1316,7 +1329,7 @@ void H264SliceDecodingProcess::SlidingWindowDecodedReferencePictureMarkingProces
__picture = _picture;
}
}
MPP_H264_SD_LOG("[DRPM] Mark short term picture to unsued FrameNum(%d) FrameNumWrap(%ld)", __picture->FrameNum, __picture->FrameNumWrap);
MPP_H264_SD_LOG("[DRPM] Mark short term picture to unsued FrameNum(%d) FrameNumWrap(%lld)", __picture->FrameNum, __picture->FrameNumWrap);
__picture->referenceFlag = H264PictureContext::unused_for_reference;
if (__picture->field_pic_flag == 1)
{
Expand Down Expand Up @@ -1419,7 +1432,7 @@ void H264SliceDecodingProcess::AdaptiveMemoryControlDecodedReferencePicutreMarki
{
uint32_t max_long_term_frame_idx_plus1 = slice->drpm->memory_management_control_operations_datas[index++].max_long_term_frame_idx_plus1;
int64_t MaxLongTermFrameIdx = max_long_term_frame_idx_plus1 == 0 ? no_long_term_frame_indices : max_long_term_frame_idx_plus1 - 1;
MPP_H264_SD_LOG("[MM] mmco(%d) max_long_term_frame_idx_plus1(%d) MaxLongTermFrameIdx(%ld)", memory_management_control_operation, max_long_term_frame_idx_plus1, MaxLongTermFrameIdx);
MPP_H264_SD_LOG("[MM] mmco(%d) max_long_term_frame_idx_plus1(%d) MaxLongTermFrameIdx(%lld)", memory_management_control_operation, max_long_term_frame_idx_plus1, MaxLongTermFrameIdx);
for (auto _picture : pictures)
{
if (_picture->referenceFlag & H264PictureContext::used_for_long_term_reference && _picture->LongTermFrameIdx > max_long_term_frame_idx_plus1 - 1)
Expand Down Expand Up @@ -1546,7 +1559,7 @@ void H264SliceDecodingProcess::SliceDecodingProcess(H264NalSyntax::ptr nal)
#ifdef ENABLE_MMP_SD_DEBUG
static uint64_t count = 0;
#endif /* ENABLE_MMP_SD_DEBUG */
MPP_H264_SD_LOG("[DP] %ld nal_unit_type(%s-%d) slice_type(%s-%d) frame_num(%ld) nal_ref_idc(%d)",
MPP_H264_SD_LOG("[DP] %lld nal_unit_type(%s-%d) slice_type(%s-%d) frame_num(%lld) nal_ref_idc(%d)",
count++,
H264NaluTypeToStr(nal->nal_unit_type).c_str(),
nal->nal_unit_type,
Expand Down
20 changes: 16 additions & 4 deletions H26xBinaryReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void H26xBinaryReader::MoveNextByte()

bool H26xBinaryReader::Eof()
{
return _reader->Eof();
return _reader->Eof() && _curBitPos == 8;
}

void H26xBinaryReader::BeginNalUnit()
Expand Down Expand Up @@ -289,7 +289,19 @@ bool H26xBinaryReader::more_rbsp_data()
//
if (_rbspEndByte > _reader->Tell()) // cache hint
{
return true;
// * 可能最后一个字节完全是 rbsp_trailing_bits (1 rbsp_stop_one_bit + 7 rbsp_alignment_zero_bit)
if (_curBitPos == 8 && (_rbspEndByte == _reader->Tell() + 1))
{
ReadOneByteAuto();
if (_curValue == 0x80)
{
return false;
}
}
else
{
return true;
}
}
else if (_reader->Tell() == _rbspEndByte) // reach end of rbsp
{
Expand Down Expand Up @@ -372,7 +384,7 @@ void H26xBinaryReader::rbsp_trailing_bits()
uint8_t rbsp_alignment_zero_bit;
U(1, rbsp_stop_one_bit);
assert(rbsp_stop_one_bit == 1);
while (!(_curBitPos == 0 || _curBitPos == 8))
while (!(_curBitPos == 0 || _curBitPos == 8) && more_data_in_byte_stream())
{
U(1, rbsp_alignment_zero_bit);
// assert(rbsp_alignment_zero_bit == 0);
Expand All @@ -386,7 +398,7 @@ bool H26xBinaryReader::more_data_in_byte_stream()
// specified as follows:
// - If more data follow in the byte stream, the return value of more_data_in_byte_stream( ) is equal to TRUE.
// - Otherwise, the return value of more_data_in_byte_stream( ) is equal to FALSE.
return !_reader->Eof();
return !(_reader->Eof() && _curBitPos == 8);
}

void H26xBinaryReader::byte_alignment()
Expand Down