Commit d15d673668546da2c1920338100535152b00bec7

Authored by Hu Chunming
1 parent 40ac568f

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

src/decoder/dvpp/DvppDataMemory.hpp
... ... @@ -17,7 +17,7 @@ public:
17 17 }
18 18  
19 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 21 data_size = _size;
22 22 data_type = 1;
23 23 pHwRgb = pHwData;
... ...
src/decoder/dvpp/DvppDecoder.cpp
... ... @@ -412,12 +412,13 @@ void DvppDecoder::read_thread() {
412 412 data_pkt->frame_nb = frame_nb;
413 413 m_pktQueue.push(data_pkt);
414 414 m_pktQueue_mutex.unlock();
415   -
416 415 bPushed = true;
  416 +
417 417 frame_count++;
418 418 }
419 419  
420   - if(!bPushed){
  420 + if(!bPushed)
  421 + {
421 422 av_packet_free(&pkt);
422 423 pkt = nullptr;
423 424 }
... ... @@ -661,6 +662,7 @@ void DvppDecoder::decode_thread(){
661 662 bBreak = true;
662 663 break;
663 664 }else if(ret == 1){
  665 + std::this_thread::sleep_for(std::chrono::milliseconds(3));
664 666 continue;
665 667 }
666 668  
... ... @@ -718,13 +720,6 @@ void DvppDecoder::decode_thread(){
718 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 723 int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_count){
729 724  
730 725 // 此处需要判断 m_vdecQueue 队列长度,避免占用过多显存
... ... @@ -746,7 +741,7 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
746 741 data_pkt = m_pktQueue.front();
747 742 m_pktQueue.pop();
748 743 m_pktQueue_mutex.unlock();
749   -
  744 +
750 745 // 解码
751 746 void *vdecInputbuf = nullptr;
752 747 int ret = acldvppMalloc((void **)&vdecInputbuf, g_pkt_size);
... ... @@ -757,23 +752,6 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
757 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 755 AVPacket* pkt = data_pkt->pkt;
778 756 ret = aclrtMemcpy(vdecInputbuf, pkt->size, pkt->data, pkt->size, ACL_MEMCPY_HOST_TO_DEVICE);
779 757 if(ACL_ERROR_NONE != ret){
... ... @@ -792,6 +770,7 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
792 770 return 2;
793 771 }
794 772  
  773 + bool bRet = false;
795 774 acldvppStreamDesc *input_stream_desc = nullptr;
796 775 acldvppPicDesc *output_pic_desc = nullptr;
797 776 do{
... ... @@ -817,20 +796,24 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
817 796 // user_data->startTime = startTime;
818 797 user_data->sendTime = UtilTools::get_cur_time_ms();
819 798 user_data->self = this;
  799 +
  800 + m_vdecQueue_mutex.lock();
820 801 ret = aclvdecSendFrame(vdecChannelDesc, input_stream_desc, output_pic_desc, nullptr, reinterpret_cast<void *>(user_data));
821 802 delete data_pkt;
822 803 data_pkt = nullptr;
823 804 if(ret != ACL_ERROR_NONE){
  805 + LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name);
  806 + m_vdecQueue_mutex.unlock();
824 807 delete user_data;
825 808 user_data = nullptr;
826   - LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name);
827 809 break;
828 810 }
829   -
830   - m_vdecQueue_mutex.lock();
  811 +
831 812 m_vdecQueue.push(vdecInputbuf);
832 813 m_vdecQueue_mutex.unlock();
833 814  
  815 + bRet = true;
  816 +
834 817 return 0;
835 818 }while (0);
836 819  
... ... @@ -839,19 +822,25 @@ int DvppDecoder::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_c
839 822 data_pkt = nullptr;
840 823 }
841 824  
  825 + if (!bRet){
  826 + acldvppFree(vdecInputbuf);
  827 + vdecInputbuf = nullptr;
  828 + }
  829 +
842 830 // 报错情形
843 831 if(input_stream_desc){
844 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 835 if (vdecOutputBuf){
851 836 acldvppFree(vdecOutputBuf);
852 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 844 return 1;
856 845 }
857 846  
... ... @@ -894,7 +883,7 @@ void DvppDecoder::release_dvpp(){
894 883 }
895 884  
896 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 889 void DvppDecoder::set_mq_callback(mq_callback_t cb) {
... ...