Commit 607d4be179a096e7c105bb1f52c913c2166a1856

Authored by Hu Chunming
1 parent d2511188

截图代码优化,避免失败情形显存泄漏

Showing 1 changed file with 90 additions and 58 deletions
src/util/vpc_util.cpp
@@ -88,11 +88,33 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { @@ -88,11 +88,33 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) {
88 acldvppSetPicDescWidthStride(vpcOutputDesc_, cropSizeWidth); 88 acldvppSetPicDescWidthStride(vpcOutputDesc_, cropSizeWidth);
89 acldvppSetPicDescHeightStride(vpcOutputDesc_, cropSizeHeight); 89 acldvppSetPicDescHeightStride(vpcOutputDesc_, cropSizeHeight);
90 acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); 90 acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_);
  91 +
91 aclrtStream stream_; 92 aclrtStream stream_;
92 aclrtCreateStream(&stream_); 93 aclrtCreateStream(&stream_);
93 - int ret = acldvppVpcCropAsync(dvppChannelDesc_, vpcInputDesc_, vpcOutputDesc_, cropArea_, stream_);  
94 - ret = aclrtSynchronizeStream(stream_); 94 + bool bRet = false;
  95 + do
  96 + {
  97 + aclError ret = acldvppVpcCropAsync(dvppChannelDesc_, vpcInputDesc_, vpcOutputDesc_, cropArea_, stream_);
  98 + if (ret != ACL_SUCCESS) {
  99 + LOG_ERROR("acldvppVpcCropAsync failed, task_id : {}", devMem->getId());
  100 + break;
  101 + }
  102 + ret = aclrtSynchronizeStream(stream_);
  103 + if (ret != ACL_SUCCESS) {
  104 + LOG_ERROR("aclrtSynchronizeStream failed, task_id : {}", devMem->getId());
  105 + break;
  106 + }
  107 +
  108 + img_info.pic_desc = vpcOutputDesc_;
  109 + img_info.object_id = obj.object_id;
  110 + img_info.task_id = obj.task_id; //该物体属于的任务ID号
  111 + img_info.task_frame_count = obj.task_frame_count; //该物体当前出现的帧号
  112 + img_info.index = obj.index; //该物体所属类别的编号
  113 + img_info.confidence = obj.confidence; //该物体的置信度
95 114
  115 + bRet = true;
  116 + } while (0);
  117 +
96 if (stream_ != nullptr) { 118 if (stream_ != nullptr) {
97 aclrtDestroyStream(stream_); 119 aclrtDestroyStream(stream_);
98 } 120 }
@@ -103,20 +125,15 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { @@ -103,20 +125,15 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) {
103 (void)acldvppDestroyRoiConfig(cropArea_); 125 (void)acldvppDestroyRoiConfig(cropArea_);
104 cropArea_ = nullptr; 126 cropArea_ = nullptr;
105 127
106 - img_info.pic_desc = vpcOutputDesc_;  
107 - img_info.object_id = obj.object_id;  
108 - img_info.task_id = obj.task_id; //该物体属于的任务ID号  
109 - img_info.task_frame_count = obj.task_frame_count; //该物体当前出现的帧号  
110 - img_info.index = obj.index; //该物体所属类别的编号  
111 - img_info.confidence = obj.confidence; //该物体的置信度  
112 -  
113 - // (void)acldvppDestroyPicDesc(vpcOutputDesc_);  
114 - // vpcOutputDesc_ = nullptr; 128 + if (!bRet) {
  129 + (void)acldvppDestroyPicDesc(vpcOutputDesc_);
  130 + vpcOutputDesc_ = nullptr;
115 131
116 - // if (vpcOutBufferDev_ != nullptr) {  
117 - // (void)acldvppFree(vpcOutBufferDev_);  
118 - // vpcOutBufferDev_ = nullptr;  
119 - // } 132 + if (vpcOutBufferDev_ != nullptr) {
  133 + (void)acldvppFree(vpcOutBufferDev_);
  134 + vpcOutBufferDev_ = nullptr;
  135 + }
  136 + }
120 137
121 return img_info; 138 return img_info;
122 } 139 }
@@ -234,49 +251,62 @@ vector<vpc_img_info> VPCUtil::crop_batch(DeviceMemory *devMem, vector<video_obje @@ -234,49 +251,62 @@ vector<vpc_img_info> VPCUtil::crop_batch(DeviceMemory *devMem, vector<video_obje
234 aclrtStream stream_; 251 aclrtStream stream_;
235 aclrtCreateStream(&stream_); 252 aclrtCreateStream(&stream_);
236 253
237 - uint32_t roiNums[] = { outputBatchSize_ };  
238 - ret = acldvppVpcBatchCropAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums, 1, outputBatchPicDesc_, cropAreas, stream_);  
239 - ret = aclrtSynchronizeStream(stream_); 254 + bool bRet = false;
  255 + do {
  256 + uint32_t roiNums[] = { outputBatchSize_ };
  257 + ret = acldvppVpcBatchCropAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums, 1, outputBatchPicDesc_, cropAreas, stream_);
  258 + if (ret != ACL_SUCCESS) {
  259 + LOG_ERROR("acldvppVpcBatchCropAsync failed, task_id : {}", devMem->getId());
  260 + break;
  261 + }
  262 + ret = aclrtSynchronizeStream(stream_);
  263 + if (ret != ACL_SUCCESS) {
  264 + LOG_ERROR("aclrtSynchronizeStream failed, task_id : {}", devMem->getId());
  265 + break;
  266 + }
240 267
  268 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  269 + video_object_info obj = objs[i];
  270 +
  271 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  272 + void *outputDataDev = acldvppGetPicDescData(vpcOutputDesc);
  273 + uint32_t outputSize = acldvppGetPicDescSize(vpcOutputDesc);
  274 + uint32_t width = acldvppGetPicDescWidth(vpcOutputDesc);
  275 + uint32_t width_stride = acldvppGetPicDescWidthStride(vpcOutputDesc);
  276 + uint32_t height = acldvppGetPicDescHeight(vpcOutputDesc);
  277 + uint32_t height_stride = acldvppGetPicDescHeightStride(vpcOutputDesc);
  278 + acldvppPixelFormat fmt = acldvppGetPicDescFormat(vpcOutputDesc);
  279 +
  280 + acldvppPicDesc *vpcInputDesc_= acldvppCreatePicDesc();
  281 + acldvppSetPicDescData(vpcInputDesc_, vecOutPtr_[i]);
  282 + acldvppSetPicDescFormat(vpcInputDesc_, fmt);
  283 + acldvppSetPicDescWidth(vpcInputDesc_, width);
  284 + acldvppSetPicDescHeight(vpcInputDesc_, height);
  285 + acldvppSetPicDescWidthStride(vpcInputDesc_, width_stride);
  286 + acldvppSetPicDescHeightStride(vpcInputDesc_, height_stride);
  287 + acldvppSetPicDescSize(vpcInputDesc_, outputSize);
  288 +
  289 + vpc_img_info img_info ;
  290 + img_info.pic_desc = vpcInputDesc_;
  291 + img_info.object_id = obj.object_id;
  292 + img_info.task_id = obj.task_id; //该物体属于的任务ID号
  293 + img_info.task_frame_count = obj.task_frame_count; //该物体当前出现的帧号
  294 + img_info.index = obj.index; //该物体所属类别的编号
  295 + img_info.confidence = obj.confidence; //该物体的置信度
  296 + vec_img_info.push_back(img_info);
  297 + // vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  298 + // string file_name = "output";
  299 + // file_name = file_name + to_string(i) + ".jpg";
  300 + // vpc_jpeg_encode(vpcOutputDesc, file_name);
  301 + }
  302 +
  303 + bRet = true;
  304 + } while (0);
  305 +
241 if (stream_ != nullptr) { 306 if (stream_ != nullptr) {
242 aclrtDestroyStream(stream_); 307 aclrtDestroyStream(stream_);
243 } 308 }
244 309
245 - for (uint32_t i = 0; i < outputBatchSize_; i++) {  
246 - video_object_info obj = objs[i];  
247 -  
248 - vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);  
249 - void *outputDataDev = acldvppGetPicDescData(vpcOutputDesc);  
250 - uint32_t outputSize = acldvppGetPicDescSize(vpcOutputDesc);  
251 - uint32_t width = acldvppGetPicDescWidth(vpcOutputDesc);  
252 - uint32_t width_stride = acldvppGetPicDescWidthStride(vpcOutputDesc);  
253 - uint32_t height = acldvppGetPicDescHeight(vpcOutputDesc);  
254 - uint32_t height_stride = acldvppGetPicDescHeightStride(vpcOutputDesc);  
255 - acldvppPixelFormat fmt = acldvppGetPicDescFormat(vpcOutputDesc);  
256 -  
257 - acldvppPicDesc *vpcInputDesc_= acldvppCreatePicDesc();  
258 - acldvppSetPicDescData(vpcInputDesc_, vecOutPtr_[i]);  
259 - acldvppSetPicDescFormat(vpcInputDesc_, fmt);  
260 - acldvppSetPicDescWidth(vpcInputDesc_, width);  
261 - acldvppSetPicDescHeight(vpcInputDesc_, height);  
262 - acldvppSetPicDescWidthStride(vpcInputDesc_, width_stride);  
263 - acldvppSetPicDescHeightStride(vpcInputDesc_, height_stride);  
264 - acldvppSetPicDescSize(vpcInputDesc_, outputSize);  
265 -  
266 - vpc_img_info img_info ;  
267 - img_info.pic_desc = vpcInputDesc_;  
268 - img_info.object_id = obj.object_id;  
269 - img_info.task_id = obj.task_id; //该物体属于的任务ID号  
270 - img_info.task_frame_count = obj.task_frame_count; //该物体当前出现的帧号  
271 - img_info.index = obj.index; //该物体所属类别的编号  
272 - img_info.confidence = obj.confidence; //该物体的置信度  
273 - vec_img_info.push_back(img_info);  
274 - // vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);  
275 - // string file_name = "output";  
276 - // file_name = file_name + to_string(i) + ".jpg";  
277 - // vpc_jpeg_encode(vpcOutputDesc, file_name);  
278 - }  
279 -  
280 aclrtSetCurrentContext(context_); 310 aclrtSetCurrentContext(context_);
281 311
282 if (vpcInputBatchDesc_ != nullptr) { 312 if (vpcInputBatchDesc_ != nullptr) {
@@ -284,11 +314,13 @@ vector&lt;vpc_img_info&gt; VPCUtil::crop_batch(DeviceMemory *devMem, vector&lt;video_obje @@ -284,11 +314,13 @@ vector&lt;vpc_img_info&gt; VPCUtil::crop_batch(DeviceMemory *devMem, vector&lt;video_obje
284 vpcInputBatchDesc_ = nullptr; 314 vpcInputBatchDesc_ = nullptr;
285 } 315 }
286 316
287 - // for(int i = 0; i < vecOutPtr_.size(); i++){  
288 - // if (vecOutPtr_[i] != nullptr){  
289 - // acldvppFree(vecOutPtr_[i]);  
290 - // }  
291 - // } 317 + if (!bRet){
  318 + for(int i = 0; i < vecOutPtr_.size(); i++){
  319 + if (vecOutPtr_[i] != nullptr){
  320 + acldvppFree(vecOutPtr_[i]);
  321 + }
  322 + }
  323 + }
292 324
293 if (outputBatchPicDesc_ != nullptr) { 325 if (outputBatchPicDesc_ != nullptr) {
294 (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_); 326 (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_);