Commit d15d673668546da2c1920338100535152b00bec7

Authored by Hu Chunming
1 parent 40ac568f

优化代码;避免可能的泄漏

src/decoder/dvpp/DvppDataMemory.hpp
@@ -17,7 +17,7 @@ public: @@ -17,7 +17,7 @@ public:
17 } 17 }
18 18
19 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) 19 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)
20 - :DeviceMemory(3, _width, _width_stride, _height, _height_stride, _id, _dev_id, _key_frame, frame_nb, false){ 20 + :DeviceMemory(-1, _width, _width_stride, _height, _height_stride, _id, _dev_id, _key_frame, frame_nb, false){
21 data_size = _size; 21 data_size = _size;
22 data_type = 1; 22 data_type = 1;
23 pHwRgb = pHwData; 23 pHwRgb = pHwData;
src/decoder/dvpp/DvppDecoder.cpp
@@ -412,12 +412,13 @@ void DvppDecoder::read_thread() { @@ -412,12 +412,13 @@ void DvppDecoder::read_thread() {
412 data_pkt->frame_nb = frame_nb; 412 data_pkt->frame_nb = frame_nb;
413 m_pktQueue.push(data_pkt); 413 m_pktQueue.push(data_pkt);
414 m_pktQueue_mutex.unlock(); 414 m_pktQueue_mutex.unlock();
415 -  
416 bPushed = true; 415 bPushed = true;
  416 +
417 frame_count++; 417 frame_count++;
418 } 418 }
419 419
420 - if(!bPushed){ 420 + if(!bPushed)
  421 + {
421 av_packet_free(&pkt); 422 av_packet_free(&pkt);
422 pkt = nullptr; 423 pkt = nullptr;
423 } 424 }
@@ -661,6 +662,7 @@ void DvppDecoder::decode_thread(){ @@ -661,6 +662,7 @@ void DvppDecoder::decode_thread(){
661 bBreak = true; 662 bBreak = true;
662 break; 663 break;
663 }else if(ret == 1){ 664 }else if(ret == 1){
  665 + std::this_thread::sleep_for(std::chrono::milliseconds(3));
664 continue; 666 continue;
665 } 667 }
666 668
@@ -718,13 +720,6 @@ void DvppDecoder::decode_thread(){ @@ -718,13 +720,6 @@ void DvppDecoder::decode_thread(){
718 LOG_INFO("[{}]- decode thread exit.", m_dec_name); 720 LOG_INFO("[{}]- decode thread exit.", m_dec_name);
719 } 721 }
720 722
721 -// #include <fstream>  
722 -// #include <iostream>  
723 -// #include <cstring>  
724 -  
725 -// static int nRecoder = 0;  
726 -// std::ofstream outfile;  
727 -  
728 int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_count){ 723 int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_count){
729 724
730 // 此处需要判断 m_vdecQueue 队列长度,避免占用过多显存 725 // 此处需要判断 m_vdecQueue 队列长度,避免占用过多显存
@@ -746,7 +741,7 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c @@ -746,7 +741,7 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
746 data_pkt = m_pktQueue.front(); 741 data_pkt = m_pktQueue.front();
747 m_pktQueue.pop(); 742 m_pktQueue.pop();
748 m_pktQueue_mutex.unlock(); 743 m_pktQueue_mutex.unlock();
749 - 744 +
750 // 解码 745 // 解码
751 void *vdecInputbuf = nullptr; 746 void *vdecInputbuf = nullptr;
752 int ret = acldvppMalloc((void **)&vdecInputbuf, g_pkt_size); 747 int ret = acldvppMalloc((void **)&vdecInputbuf, g_pkt_size);
@@ -757,23 +752,6 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c @@ -757,23 +752,6 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
757 return 2; 752 return 2;
758 } 753 }
759 754
760 - // if(nRecoder == 0){  
761 - // outfile.open("pkt.bin", std::ios::binary | std::ios::app);  
762 - // if (!outfile) {  
763 - // std::cerr << "Failed to open file!" << std::endl;  
764 - // return 2;  
765 - // }  
766 - // }  
767 -  
768 - // outfile.write((const char*)pkt->data, pkt->size);  
769 -  
770 - // nRecoder ++ ;  
771 - // if(nRecoder >= 2000){  
772 - // outfile.close();  
773 - // return 2;  
774 - // }  
775 -  
776 -  
777 AVPacket* pkt = data_pkt->pkt; 755 AVPacket* pkt = data_pkt->pkt;
778 ret = aclrtMemcpy(vdecInputbuf, pkt->size, pkt->data, pkt->size, ACL_MEMCPY_HOST_TO_DEVICE); 756 ret = aclrtMemcpy(vdecInputbuf, pkt->size, pkt->data, pkt->size, ACL_MEMCPY_HOST_TO_DEVICE);
779 if(ACL_ERROR_NONE != ret){ 757 if(ACL_ERROR_NONE != ret){
@@ -792,6 +770,7 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c @@ -792,6 +770,7 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
792 return 2; 770 return 2;
793 } 771 }
794 772
  773 + bool bRet = false;
795 acldvppStreamDesc *input_stream_desc = nullptr; 774 acldvppStreamDesc *input_stream_desc = nullptr;
796 acldvppPicDesc *output_pic_desc = nullptr; 775 acldvppPicDesc *output_pic_desc = nullptr;
797 do{ 776 do{
@@ -817,20 +796,24 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c @@ -817,20 +796,24 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
817 // user_data->startTime = startTime; 796 // user_data->startTime = startTime;
818 user_data->sendTime = UtilTools::get_cur_time_ms(); 797 user_data->sendTime = UtilTools::get_cur_time_ms();
819 user_data->self = this; 798 user_data->self = this;
  799 +
  800 + m_vdecQueue_mutex.lock();
820 ret = aclvdecSendFrame(vdecChannelDesc, input_stream_desc, output_pic_desc, nullptr, reinterpret_cast<void *>(user_data)); 801 ret = aclvdecSendFrame(vdecChannelDesc, input_stream_desc, output_pic_desc, nullptr, reinterpret_cast<void *>(user_data));
821 delete data_pkt; 802 delete data_pkt;
822 data_pkt = nullptr; 803 data_pkt = nullptr;
823 if(ret != ACL_ERROR_NONE){ 804 if(ret != ACL_ERROR_NONE){
  805 + LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name);
  806 + m_vdecQueue_mutex.unlock();
824 delete user_data; 807 delete user_data;
825 user_data = nullptr; 808 user_data = nullptr;
826 - LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name);  
827 break; 809 break;
828 } 810 }
829 -  
830 - m_vdecQueue_mutex.lock(); 811 +
831 m_vdecQueue.push(vdecInputbuf); 812 m_vdecQueue.push(vdecInputbuf);
832 m_vdecQueue_mutex.unlock(); 813 m_vdecQueue_mutex.unlock();
833 814
  815 + bRet = true;
  816 +
834 return 0; 817 return 0;
835 }while (0); 818 }while (0);
836 819
@@ -839,19 +822,25 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c @@ -839,19 +822,25 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
839 data_pkt = nullptr; 822 data_pkt = nullptr;
840 } 823 }
841 824
  825 + if (!bRet){
  826 + acldvppFree(vdecInputbuf);
  827 + vdecInputbuf = nullptr;
  828 + }
  829 +
842 // 报错情形 830 // 报错情形
843 if(input_stream_desc){ 831 if(input_stream_desc){
844 CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input_stream_desc), "acldvppDestroyStreamDesc failed"); 832 CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input_stream_desc), "acldvppDestroyStreamDesc failed");
845 } 833 }
846 - if(output_pic_desc){  
847 - CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed");  
848 - }  
849 834
850 if (vdecOutputBuf){ 835 if (vdecOutputBuf){
851 acldvppFree(vdecOutputBuf); 836 acldvppFree(vdecOutputBuf);
852 vdecOutputBuf = nullptr; 837 vdecOutputBuf = nullptr;
853 } 838 }
854 839
  840 + if(output_pic_desc){
  841 + CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed");
  842 + }
  843 +
855 return 1; 844 return 1;
856 } 845 }
857 846
@@ -894,7 +883,7 @@ void DvppDecoder::release_dvpp(){ @@ -894,7 +883,7 @@ void DvppDecoder::release_dvpp(){
894 } 883 }
895 884
896 void DvppDecoder::doRecode(RecoderInfo& recoderInfo) { 885 void DvppDecoder::doRecode(RecoderInfo& recoderInfo) {
897 - m_recoderManager.create_recode_task2(recoderInfo); 886 + m_recoderManager.create_recode_task(recoderInfo);
898 } 887 }
899 888
900 void DvppDecoder::set_mq_callback(mq_callback_t cb) { 889 void DvppDecoder::set_mq_callback(mq_callback_t cb) {