From cbf6857f742e469504f75a3bba5a3b1db0c647b1 Mon Sep 17 00:00:00 2001 From: Rajy Rawther Date: Fri, 17 Nov 2023 17:55:53 -0800 Subject: [PATCH] Rr/parser clean up (#75) * minor clean_up on parser struct allocation * minor clean_up * address review comments --- src/parser/hevc_parser.cpp | 108 ++++++++++--------------------------- src/parser/hevc_parser.h | 40 +++----------- 2 files changed, 36 insertions(+), 112 deletions(-) diff --git a/src/parser/hevc_parser.cpp b/src/parser/hevc_parser.cpp index a224368cda..c3506ea2f6 100644 --- a/src/parser/hevc_parser.cpp +++ b/src/parser/hevc_parser.cpp @@ -22,19 +22,33 @@ THE SOFTWARE. #include "hevc_parser.h" +template +inline T *AllocStruct(const int max_cnt) { + T *p = nullptr; + try { + p = (max_cnt == 1) ? new T : new T [max_cnt]; + } + catch(const std::exception& e) { + ERR(STR("Failed to alloc HEVC header struct Data, ") + STR(e.what())) + } + memset(p, 0, sizeof(T) * max_cnt); + return p; +} + HEVCVideoParser::HEVCVideoParser() { pic_count_ = 0; first_pic_after_eos_nal_unit_ = 0; - m_active_vps_id_ = -1; m_active_sps_id_ = -1; m_active_pps_id_ = -1; b_new_picture_ = false; - m_vps_ = NULL; - m_sps_ = NULL; - m_pps_ = NULL; - m_sh_ = NULL; - m_sh_copy_ = NULL; + // allocate all fixed size structors here + m_vps_ = AllocStruct(MAX_VPS_COUNT); + m_sps_ = AllocStruct(MAX_SPS_COUNT); + m_pps_ = AllocStruct(MAX_PPS_COUNT); + m_sh_ = AllocStruct(1); + m_sh_copy_ = AllocStruct(1); + m_sei_message_ = AllocStruct(1); memset(&curr_pic_info_, 0, sizeof(HevcPicInfo)); memset(&dpb_buffer_, 0, sizeof(DecodedPictureBuffer)); @@ -112,74 +126,8 @@ HEVCVideoParser::~HEVCVideoParser() { } } -HEVCVideoParser::VpsData* HEVCVideoParser::AllocVps() { - VpsData *p = nullptr; - try { - p = new VpsData [MAX_VPS_COUNT]; - } - catch(const std::exception& e) { - ERR(STR("Failed to alloc VPS Data, ") + STR(e.what())) - } - memset(p, 0, sizeof(VpsData) * MAX_VPS_COUNT); - return p; -} - -HEVCVideoParser::SpsData* HEVCVideoParser::AllocSps() { - SpsData *p = nullptr; - try { - p = new SpsData [MAX_SPS_COUNT]; - } - catch(const std::exception& e) { - ERR(STR("Failed to alloc SPS Data, ") + STR(e.what())) - } - memset(p, 0, sizeof(SpsData) * MAX_SPS_COUNT); - return p; -} - -HEVCVideoParser::PpsData* HEVCVideoParser::AllocPps() { - PpsData *p = nullptr; - try { - p = new PpsData [MAX_PPS_COUNT]; - } - catch(const std::exception& e) { - ERR(STR("Failed to alloc PPS Data, ") + STR(e.what())) - } - memset(p, 0, sizeof(PpsData) * MAX_PPS_COUNT); - return p; -} - -HEVCVideoParser::SliceHeaderData* HEVCVideoParser::AllocSliceHeader() { - SliceHeaderData *p = nullptr; - try { - p = new SliceHeaderData; - } - catch(const std::exception& e) { - ERR(STR("Failed to alloc Slice Header Data, ") + STR(e.what())) - } - memset(p, 0, sizeof(SliceHeaderData)); - return p; -} - -HEVCVideoParser::SeiMessageData* HEVCVideoParser::AllocSeiMessage() { - SeiMessageData *p = nullptr; - try { - p = new SeiMessageData; - } - catch(const std::exception& e) { - ERR(STR("Failed to alloc Sei Message Data, ") + STR(e.what())) - } - memset(p, 0, sizeof(SeiMessageData)); - return p; -} - ParserResult HEVCVideoParser::Init() { b_new_picture_ = false; - m_vps_ = AllocVps(); - m_sps_ = AllocSps(); - m_pps_ = AllocPps(); - m_sh_ = AllocSliceHeader(); - m_sh_copy_ = AllocSliceHeader(); - m_sei_message_ = AllocSeiMessage(); return PARSER_OK; } @@ -297,7 +245,7 @@ int HEVCVideoParser::SendPicForDecode() { dec_pic_params_.nBitstreamDataLen = pic_stream_data_size_; dec_pic_params_.pBitstreamData = pic_stream_data_ptr_; dec_pic_params_.nNumSlices = slice_num_; - dec_pic_params_.pSliceDataOffsets = NULL; // Todo: do we need this? Remove if not. + dec_pic_params_.pSliceDataOffsets = nullptr; // Todo: do we need this? Remove if not. dec_pic_params_.ref_pic_flag = 1; // HEVC decoded picture is always marked as short term at first. dec_pic_params_.intra_pic_flag = m_sh_->slice_type == HEVC_SLICE_TYPE_I ? 1 : 0; @@ -1295,7 +1243,7 @@ void HEVCVideoParser::ParseVps(uint8_t *nalu, size_t size) { } void HEVCVideoParser::ParseSps(uint8_t *nalu, size_t size) { - SpsData *sps_ptr = NULL; + SpsData *sps_ptr = nullptr; size_t offset = 0; uint32_t vps_id = Parser::ReadBits(nalu, offset, 4); @@ -1543,8 +1491,8 @@ void HEVCVideoParser::ParsePps(uint8_t *nalu, size_t size) { } bool HEVCVideoParser::ParseSliceHeader(uint8_t *nalu, size_t size) { - PpsData *pps_ptr = NULL; - SpsData *sps_ptr = NULL; + PpsData *pps_ptr = nullptr; + SpsData *sps_ptr = nullptr; size_t offset = 0; SliceHeaderData temp_sh; memset(m_sh_, 0, sizeof(SliceHeaderData)); @@ -1783,18 +1731,17 @@ bool HEVCVideoParser::ParseSliceHeader(uint8_t *nalu, size_t size) { m_sh_->slice_loop_filter_across_slices_enabled_flag = Parser::GetBit(nalu, offset); } - memcpy(m_sh_copy_, m_sh_, sizeof(*m_sh_)); + memcpy(m_sh_copy_, m_sh_, sizeof(SliceHeaderData)); } else { //dependant slice - memcpy(m_sh_, m_sh_copy_, sizeof(*m_sh_copy_)); + memcpy(m_sh_, m_sh_copy_, sizeof(SliceHeaderData)); m_sh_->first_slice_segment_in_pic_flag = temp_sh.first_slice_segment_in_pic_flag; m_sh_->no_output_of_prior_pics_flag = temp_sh.no_output_of_prior_pics_flag; m_sh_->slice_pic_parameter_set_id = temp_sh.slice_pic_parameter_set_id; m_sh_->dependent_slice_segment_flag = temp_sh.dependent_slice_segment_flag; m_sh_->slice_segment_address = temp_sh.slice_segment_address; } - if (pps_ptr->tiles_enabled_flag || pps_ptr->entropy_coding_sync_enabled_flag) { int max_num_entry_point_offsets; // 7.4.7.1 if (!pps_ptr->tiles_enabled_flag && pps_ptr->entropy_coding_sync_enabled_flag) { @@ -1861,6 +1808,9 @@ void HEVCVideoParser::ParseSeiMessage(uint8_t *nalu, size_t size) { sei_message_ptr->payload_size += temp_byte; // copy the payload to buffer + if (sei_message_ptr->payload_size > sizeof(m_sei_data_)) { + THROW("sei payload size is too big for sei buffer!"); + } memcpy(m_sei_data_, sei_message_ptr, sei_message_ptr->payload_size); } diff --git a/src/parser/hevc_parser.h b/src/parser/hevc_parser.h index 1d8ae227d9..6e39c70c85 100644 --- a/src/parser/hevc_parser.h +++ b/src/parser/hevc_parser.h @@ -69,7 +69,7 @@ public: * * @return rocDecStatus */ - virtual rocDecStatus UnInitialize(); // derived method + virtual rocDecStatus UnInitialize(); // derived method :: nothing to do for this /** * @brief HEVCParser object destructor @@ -672,15 +672,14 @@ protected: int32_t m_active_vps_id_; int32_t m_active_sps_id_; int32_t m_active_pps_id_; - VpsData* m_vps_; - SpsData* m_sps_; - PpsData* m_pps_; + VpsData* m_vps_ = nullptr; + SpsData* m_sps_ = nullptr; + PpsData* m_pps_ = nullptr; + SliceHeaderData* m_sh_ = nullptr; + SliceHeaderData* m_sh_copy_ = nullptr; + SeiMessageData* m_sei_message_ = nullptr; - SliceHeaderData* m_sh_; - SliceHeaderData* m_sh_copy_; NalUnitHeader slice_nal_unit_header_; - - SeiMessageData* m_sei_message_; uint8_t m_sei_data_[SEI_BUF_SIZE]; // to store SEI payload HevcPicInfo curr_pic_info_; bool b_new_picture_; @@ -896,31 +895,6 @@ private: */ ParserResult Init(); - /*! \brief Function to allocate memory for VPS Structure - * \return Returns pointer to the allocated memory for VpsData - */ - VpsData* AllocVps(); - - /*! \brief Function to allocate memory for SPS Structure - * \return Returns pointer to the allocated memory for SpsData - */ - SpsData* AllocSps(); - - /*! \brief Function to allocate memory for PPS Structure - * \return Returns pointer to the allocated memory for PpsData - */ - PpsData* AllocPps(); - - /*! \brief Function to allocate memory for Slice Header Structure - * \return Returns pointer to the allocated memory for SliceHeaderData - */ - SliceHeaderData* AllocSliceHeader(); - - /*! \brief Function to allocate memory for Sei Message Structure - * \return Returns pointer to the allocated memory for SeiMessageData - */ - SeiMessageData* AllocSeiMessage(); - // functions to fill structures for callback functions void FillSeqCallbackFn(SpsData* sps_data); void FillSeiMessageCallbackFn(SeiMessageData* sei_message_data);