From d15d673668546da2c1920338100535152b00bec7 Mon Sep 17 00:00:00 2001 From: fiss <2657262686@qq.com> Date: Wed, 30 Aug 2023 15:20:09 +0800 Subject: [PATCH] 优化代码;避免可能的泄漏 --- src/decoder/dvpp/DvppDataMemory.hpp | 2 +- src/decoder/dvpp/DvppDecoder.cpp | 57 +++++++++++++++++++++++---------------------------------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/decoder/dvpp/DvppDataMemory.hpp b/src/decoder/dvpp/DvppDataMemory.hpp index dd73856..e8e32bc 100755 --- a/src/decoder/dvpp/DvppDataMemory.hpp +++ b/src/decoder/dvpp/DvppDataMemory.hpp @@ -17,7 +17,7 @@ public: } DvppDataMemory( int _width, int _width_stride, int _height, int _height_stride, int _size, string _id, string _dev_id, bool _key_frame, unsigned long long frame_nb, unsigned char * pHwData) - :DeviceMemory(3, _width, _width_stride, _height, _height_stride, _id, _dev_id, _key_frame, frame_nb, false){ + :DeviceMemory(-1, _width, _width_stride, _height, _height_stride, _id, _dev_id, _key_frame, frame_nb, false){ data_size = _size; data_type = 1; pHwRgb = pHwData; diff --git a/src/decoder/dvpp/DvppDecoder.cpp b/src/decoder/dvpp/DvppDecoder.cpp index 1522263..ae782bd 100755 --- a/src/decoder/dvpp/DvppDecoder.cpp +++ b/src/decoder/dvpp/DvppDecoder.cpp @@ -412,12 +412,13 @@ void DvppDecoder::read_thread() { data_pkt->frame_nb = frame_nb; m_pktQueue.push(data_pkt); m_pktQueue_mutex.unlock(); - bPushed = true; + frame_count++; } - if(!bPushed){ + if(!bPushed) + { av_packet_free(&pkt); pkt = nullptr; } @@ -661,6 +662,7 @@ void DvppDecoder::decode_thread(){ bBreak = true; break; }else if(ret == 1){ + std::this_thread::sleep_for(std::chrono::milliseconds(3)); continue; } @@ -718,13 +720,6 @@ void DvppDecoder::decode_thread(){ LOG_INFO("[{}]- decode thread exit.", m_dec_name); } -// #include -// #include -// #include - -// static int nRecoder = 0; -// std::ofstream outfile; - int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_count){ // 此处需要判断 m_vdecQueue 队列长度,避免占用过多显存 @@ -746,7 +741,7 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c data_pkt = m_pktQueue.front(); m_pktQueue.pop(); m_pktQueue_mutex.unlock(); - + // 解码 void *vdecInputbuf = nullptr; int ret = acldvppMalloc((void **)&vdecInputbuf, g_pkt_size); @@ -757,23 +752,6 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c return 2; } - // if(nRecoder == 0){ - // outfile.open("pkt.bin", std::ios::binary | std::ios::app); - // if (!outfile) { - // std::cerr << "Failed to open file!" << std::endl; - // return 2; - // } - // } - - // outfile.write((const char*)pkt->data, pkt->size); - - // nRecoder ++ ; - // if(nRecoder >= 2000){ - // outfile.close(); - // return 2; - // } - - AVPacket* pkt = data_pkt->pkt; ret = aclrtMemcpy(vdecInputbuf, pkt->size, pkt->data, pkt->size, ACL_MEMCPY_HOST_TO_DEVICE); if(ACL_ERROR_NONE != ret){ @@ -792,6 +770,7 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c return 2; } + bool bRet = false; acldvppStreamDesc *input_stream_desc = nullptr; acldvppPicDesc *output_pic_desc = nullptr; do{ @@ -817,20 +796,24 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c // user_data->startTime = startTime; user_data->sendTime = UtilTools::get_cur_time_ms(); user_data->self = this; + + m_vdecQueue_mutex.lock(); ret = aclvdecSendFrame(vdecChannelDesc, input_stream_desc, output_pic_desc, nullptr, reinterpret_cast(user_data)); delete data_pkt; data_pkt = nullptr; if(ret != ACL_ERROR_NONE){ + LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name); + m_vdecQueue_mutex.unlock(); delete user_data; user_data = nullptr; - LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name); break; } - - m_vdecQueue_mutex.lock(); + m_vdecQueue.push(vdecInputbuf); m_vdecQueue_mutex.unlock(); + bRet = true; + return 0; }while (0); @@ -839,19 +822,25 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c data_pkt = nullptr; } + if (!bRet){ + acldvppFree(vdecInputbuf); + vdecInputbuf = nullptr; + } + // 报错情形 if(input_stream_desc){ CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input_stream_desc), "acldvppDestroyStreamDesc failed"); } - if(output_pic_desc){ - CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed"); - } if (vdecOutputBuf){ acldvppFree(vdecOutputBuf); vdecOutputBuf = nullptr; } + if(output_pic_desc){ + CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed"); + } + return 1; } @@ -894,7 +883,7 @@ void DvppDecoder::release_dvpp(){ } void DvppDecoder::doRecode(RecoderInfo& recoderInfo) { - m_recoderManager.create_recode_task2(recoderInfo); + m_recoderManager.create_recode_task(recoderInfo); } void DvppDecoder::set_mq_callback(mq_callback_t cb) { -- libgit2 0.21.4