Blame view

src/decoder/dvpp/VpcUtils.cpp 22.9 KB
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include "VpcUtils.h"
  #include "depend_headers.h"
  
  #define ALIGN_UP(val, align) (((val) % (align) == 0) ? (val) : (((val) / (align) + 1) * (align)))
  
  #define CHECK_AND_RETURN(ret, message)    \
              if(ret != 0) {LOG_ERROR("{}", message); return ret;}
  #define CHECK_NOT_RETURN(ret, message)    \
              if(ret != 0) {LOG_ERROR("{}", message);}
  #define CHECK_AND_RETURN_NOVALUE(ret, message)    \
              if(ret != 0) {LOG_ERROR("{}", message); return;}
  #define CHECK_AND_BREAK(ret, message)    \
              if(ret != 0) {LOG_ERROR("{}", message); break;}
  
4061a3c3   Hu Chunming   更换decoder
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  
  static void adjustCoordinate(uint32_t& left, uint32_t& right, uint32_t& top, uint32_t& bottom, const uint32_t& width, const uint32_t& height) {
      uint32_t cropWidth = right - left;
      uint32_t cropHeight = bottom - top;
  
      if(cropWidth < 16) {
          cropWidth += 16; //小于16的似乎抠不了图
          right += 16;
      }
  
      if (left < 0) {
          left = 0;
          right = cropWidth;
      }
  
      if (right >= width){
          right = width -1;
          left = right - cropWidth;
      }
  
      if (top < 0) {
          top = 0;
          bottom = cropHeight;
      }
  
      if (bottom >= height){
          bottom = height -1;
          top = bottom - cropHeight;
      }
  }
  
  
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
47
48
49
50
51
  VpcUtils::VpcUtils(){
  
  }
  
  VpcUtils::~VpcUtils(){
4061a3c3   Hu Chunming   更换decoder
52
53
54
55
56
57
58
59
60
61
62
63
64
      if(nullptr != stream_){
          aclrtDestroyStream(stream_);
      }
  
      if(context_){
          aclrtDestroyContext(context_);
      }
  
      if (dvppChannelDesc_) {
          (void)acldvppDestroyChannel(dvppChannelDesc_);
          (void)acldvppDestroyChannelDesc(dvppChannelDesc_);
          dvppChannelDesc_ = nullptr;
      }
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
65
66
67
68
69
70
  }
  
  int VpcUtils::init(int devId){
  
      m_devId = devId;
  
4061a3c3   Hu Chunming   更换decoder
71
72
73
74
75
76
77
      aclrtSetDevice(m_devId);
      aclrtCreateContext(&context_, m_devId);
  
      CHECK_AND_RETURN(aclrtSetCurrentContext(context_), "aclrtSetCurrentContext failed");
      CHECK_AND_RETURN(aclrtCreateStream(&stream_), "aclrtCreateStream failed! ");
  
      dvppChannelDesc_ = acldvppCreateChannelDesc();
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
78
  
4061a3c3   Hu Chunming   更换decoder
79
      int ret = ACL_ERROR_NONE;
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
80
81
      do
      {
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
82
83
84
85
86
87
88
89
90
91
          ret = acldvppCreateChannel(dvppChannelDesc_);
          CHECK_AND_BREAK(ret, "acldvppCreateChannel failed !");
  
          ret = acldvppSetChannelDescMode(dvppChannelDesc_, DVPP_CHNMODE_VPC);
          CHECK_AND_BREAK(ret, "acldvppSetChannelDescMode failed !");
      } while (0);
      
      return ret;
  }
  
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
92
93
  DvppDataMemory* VpcUtils::convert2bgr(acldvppPicDesc *inputDesc_, int out_width, int out_height, bool key_frame){
  
4061a3c3   Hu Chunming   更换decoder
94
      aclrtSetDevice(m_devId);
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
      aclrtSetCurrentContext(context_);
  
      int out_buf_width = ALIGN_UP(out_width, 16) * 3;
      int out_buf_height = ALIGN_UP(out_height, 2);
      int out_buf_size = out_buf_width * out_buf_height;
  
      DvppDataMemory* rgbMem = new DvppDataMemory(3, out_buf_width, out_buf_width, out_buf_height, out_buf_height, out_buf_size, "", to_string(m_devId), key_frame, 0);
      void *outBufferDev_ = (void*)rgbMem->getMem();
  
      acldvppPicDesc *outputDesc_= acldvppCreatePicDesc();
      acldvppSetPicDescData(outputDesc_, outBufferDev_);
      acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888); 
      acldvppSetPicDescWidth(outputDesc_, out_width);
      acldvppSetPicDescHeight(outputDesc_, out_height);
      acldvppSetPicDescWidthStride(outputDesc_, out_buf_width);
      acldvppSetPicDescHeightStride(outputDesc_, out_buf_height);
      acldvppSetPicDescSize(outputDesc_, out_buf_size);
  
      aclError ret = ACL_ERROR_NONE;
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
114
115
116
117
118
119
120
121
122
123
124
125
126
127
      do{
          // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
          ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("acldvppVpcConvertColorAsync failed - out_width:{} out_height:{} out_buf_width:{}  out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width,  out_buf_height, out_buf_size);
              break;
          }
          ret = aclrtSynchronizeStream(stream_);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{}  out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width,  out_buf_height, out_buf_size);
              break;
          }
      }while(0);
  
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
128
129
130
131
132
133
134
135
136
137
138
139
      acldvppDestroyPicDesc(outputDesc_);
  
      if(ret != ACL_ERROR_NONE){
          delete rgbMem;
          rgbMem = nullptr;
      }
  
      return rgbMem;
  }
  
  DvppDataMemory* VpcUtils::convert2bgr(DvppDataMemory* inMem){
  
4061a3c3   Hu Chunming   更换decoder
140
      aclrtSetDevice(m_devId);
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
      aclrtSetCurrentContext(context_);
  
      int out_width = inMem->getWidth();
      int out_height = inMem->getHeight();
  
  
      acldvppPicDesc *inputDesc_= acldvppCreatePicDesc();
      acldvppSetPicDescData(inputDesc_, inMem->getMem());
      acldvppSetPicDescFormat(inputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); 
      acldvppSetPicDescWidth(inputDesc_, out_width);
      acldvppSetPicDescHeight(inputDesc_, out_height);
      acldvppSetPicDescWidthStride(inputDesc_, inMem->getWidthStride());
      acldvppSetPicDescHeightStride(inputDesc_, inMem->getHeightStride());
      acldvppSetPicDescSize(inputDesc_, inMem->getSize());
  
      int out_buf_width = ALIGN_UP(out_width, 16) * 3;
      int out_buf_height = ALIGN_UP(out_height, 2);
      int out_buf_size = out_buf_width * out_buf_height;
  
      DvppDataMemory* rgbMem = new DvppDataMemory(3, out_buf_width, out_buf_width, out_buf_height, out_buf_height, out_buf_size, inMem->getId(), inMem->getDeviceId(), false, inMem->getFrameNb());
      void *outBufferDev_ = (void*)rgbMem->getMem();
  
      acldvppPicDesc *outputDesc_= acldvppCreatePicDesc();
      acldvppSetPicDescData(outputDesc_, outBufferDev_);
      acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888); 
      acldvppSetPicDescWidth(outputDesc_, out_width);
      acldvppSetPicDescHeight(outputDesc_, out_height);
      acldvppSetPicDescWidthStride(outputDesc_, out_buf_width);
      acldvppSetPicDescHeightStride(outputDesc_, out_buf_height);
      acldvppSetPicDescSize(outputDesc_, out_buf_size);
  
      aclError ret = ACL_ERROR_NONE;
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
173
174
175
176
177
178
179
180
181
182
183
184
185
186
      do{
          // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
          ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("acldvppVpcConvertColorAsync failed - out_width:{} out_height:{} out_buf_width:{}  out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width,  out_buf_height, out_buf_size);
              break;
          }
          ret = aclrtSynchronizeStream(stream_);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{}  out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width,  out_buf_height, out_buf_size);
              break;
          }
      }while(0);
  
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
187
188
189
190
191
192
193
194
195
196
197
198
199
      acldvppDestroyPicDesc(outputDesc_);
      acldvppDestroyPicDesc(inputDesc_);
  
      if(ret != ACL_ERROR_NONE){
          delete rgbMem;
          rgbMem = nullptr;
      }
  
      return rgbMem;
  }
  
  DvppDataMemory* VpcUtils::resize(acldvppPicDesc *inputDesc_, int out_width, int out_height){
  
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
200
201
      int out_buf_width = ALIGN_UP(out_width, 16);
      int out_buf_height = ALIGN_UP(out_height, 2);
4061a3c3   Hu Chunming   更换decoder
202
203
204
205
      int out_buf_size = out_buf_width * out_buf_height * 3;
      
      aclrtSetDevice(m_devId);
      aclrtSetCurrentContext(context_);
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  
      DvppDataMemory* rgbMem = new DvppDataMemory(1, out_width, out_buf_width, out_height, out_buf_height, out_buf_size, "", "", false, 0);
      void *outBufferDev_ = (void*)rgbMem->getMem();
  
      acldvppPicDesc *outputDesc_= acldvppCreatePicDesc();
      acldvppSetPicDescData(outputDesc_, outBufferDev_);
      acldvppSetPicDescFormat(outputDesc_, acldvppGetPicDescFormat(inputDesc_)); 
      acldvppSetPicDescWidth(outputDesc_, out_width);
      acldvppSetPicDescHeight(outputDesc_, out_height);
      acldvppSetPicDescWidthStride(outputDesc_, out_buf_width);
      acldvppSetPicDescHeightStride(outputDesc_, out_buf_height);
      acldvppSetPicDescSize(outputDesc_, out_buf_size);
  
      acldvppResizeConfig *resizeConfig_ = acldvppCreateResizeConfig();
  
      aclError ret = ACL_ERROR_NONE;
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
222
223
224
225
226
227
228
229
230
231
232
233
234
235
      do{
          // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
          ret = acldvppVpcResizeAsync(dvppChannelDesc_, inputDesc_, outputDesc_, resizeConfig_, stream_);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("acldvppVpcResizeAsync failed - out_width:{} out_height:{} out_buf_width:{}  out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width,  out_buf_height, out_buf_size);
              break;
          }
          ret = aclrtSynchronizeStream(stream_);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{}  out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width,  out_buf_height, out_buf_size);
              break;
          }
      }while(0);
  
4061a3c3   Hu Chunming   更换decoder
236
237
238
239
240
241
      acldvppDestroyResizeConfig(resizeConfig_);
      acldvppDestroyPicDesc(outputDesc_);
  
      if(ret != ACL_ERROR_NONE){
          delete rgbMem;
          rgbMem = nullptr;
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
242
243
      }
  
4061a3c3   Hu Chunming   更换decoder
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
      return rgbMem;
  }
  
  DvppDataMemory* VpcUtils::resize(DvppDataMemory* inMem, int out_width, int out_height) {
      
      int out_buf_width = ALIGN_UP(out_width, 16) * 3;
      int out_buf_height = ALIGN_UP(out_height, 2);
      int out_buf_size = out_buf_width * out_buf_height;
  
      aclrtSetDevice(m_devId);
      aclrtSetCurrentContext(context_);
  
      acldvppPicDesc *inputDesc_= acldvppCreatePicDesc();
      acldvppSetPicDescData(inputDesc_, inMem->getMem());
      acldvppSetPicDescFormat(inputDesc_, PIXEL_FORMAT_BGR_888); 
      acldvppSetPicDescWidth(inputDesc_, inMem->getWidth());
      acldvppSetPicDescHeight(inputDesc_, inMem->getHeight());
      acldvppSetPicDescWidthStride(inputDesc_, inMem->getWidthStride());
      acldvppSetPicDescHeightStride(inputDesc_, inMem->getHeightStride());
      acldvppSetPicDescSize(inputDesc_, inMem->getSize());
  
  
      DvppDataMemory* rgbMem = new DvppDataMemory(3, out_width, out_buf_width, out_height, out_buf_height, out_buf_size, inMem->getId(), inMem->getDeviceId(), false, inMem->getFrameNb());
      void *outBufferDev_ = (void*)rgbMem->getMem();
  
      acldvppPicDesc *outputDesc_= acldvppCreatePicDesc();
      acldvppSetPicDescData(outputDesc_, outBufferDev_);
      acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888); 
      acldvppSetPicDescWidth(outputDesc_, out_width);
      acldvppSetPicDescHeight(outputDesc_, out_height);
      acldvppSetPicDescWidthStride(outputDesc_, out_buf_width);
      acldvppSetPicDescHeightStride(outputDesc_, out_buf_height);
      acldvppSetPicDescSize(outputDesc_, out_buf_size);
  
      aclError ret = ACL_ERROR_NONE;
      acldvppResizeConfig *resizeConfig_ = acldvppCreateResizeConfig();
      do{
          // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
          ret = acldvppVpcResizeAsync(dvppChannelDesc_, inputDesc_, outputDesc_, resizeConfig_, stream_);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("acldvppVpcResizeAsync failed - out_width:{} out_height:{} out_buf_width:{}  out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width,  out_buf_height, out_buf_size);
              break;
          }
          ret = aclrtSynchronizeStream(stream_);
          if(ret != ACL_ERROR_NONE){
              LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{}  out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width,  out_buf_height, out_buf_size);
              break;
          }
      }while(0);
  
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
294
295
      acldvppDestroyResizeConfig(resizeConfig_);
      acldvppDestroyPicDesc(outputDesc_);
4061a3c3   Hu Chunming   更换decoder
296
      acldvppDestroyPicDesc(inputDesc_);
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
297
298
299
300
301
302
303
  
      if(ret != ACL_ERROR_NONE){
          delete rgbMem;
          rgbMem = nullptr;
      }
  
      return rgbMem;
4061a3c3   Hu Chunming   更换decoder
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
  }
  
  DvppDataMemory* VpcUtils::crop(DvppDataMemory* devMem, AreaInfo obj) {
  
      check_area(obj, devMem->getWidth(), devMem->getHeight());
  
      // uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16;
      // uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2;
      uint32_t cropSizeWidth = ALIGN_UP(obj.right - obj.left, 16); 
      uint32_t cropSizeHeight = ALIGN_UP(obj.bottom - obj.top, 2);
  
      
      uint32_t oddNum = 1;
      uint32_t cropLeftOffset = (obj.left + 1) / 2 * 2;  // must even
      uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum;  // must odd
      uint32_t cropTopOffset = (obj.top + 1) / 2 * 2;  // must even
      uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum;  // must odd
  
      adjustCoordinate(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset, devMem->getWidth(), devMem->getHeight());
      cropSizeWidth = cropRightOffset - cropLeftOffset + oddNum; // 宽高要比实际的高才行,否则会抠图失败
      cropSizeHeight = cropBottomOffset - cropTopOffset + oddNum;
      // LOG_DEBUG("{} ,{} ,{} ,{} ", cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  
      if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){
          LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset);
          return nullptr;
      }
  
      aclError ret;
      aclrtSetDevice(m_devId);
      ret = aclrtSetCurrentContext(context_);
      if (ret != ACL_SUCCESS) {
          LOG_ERROR("aclrtSetCurrentContext failed");
          return nullptr;
      }
  
      // LOG_INFO("crop src {} ({}, {}, {}, {})", obj.object_id, obj.left, obj.right, obj.top, obj.bottom);
      // LOG_INFO("crop {} ({}, {}, {}, {})", obj.object_id, cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
      acldvppRoiConfig *cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  
      acldvppPicDesc *vpcInputDesc_ = acldvppCreatePicDesc();
  	acldvppSetPicDescData(vpcInputDesc_, devMem->getMem()); 
  	acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_BGR_888);
  	acldvppSetPicDescWidth(vpcInputDesc_, devMem->getWidth());
  	acldvppSetPicDescHeight(vpcInputDesc_, devMem->getHeight());
  	acldvppSetPicDescWidthStride(vpcInputDesc_, devMem->getWidthStride());
  	acldvppSetPicDescHeightStride(vpcInputDesc_, devMem->getHeightStride());
  	acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize());
  
      /* processdecode */
      uint32_t vpcOutBufferSize_ = cropSizeWidth * cropSizeHeight * 3;
  
      DvppDataMemory* rgbMem = new DvppDataMemory(3, cropSizeWidth, cropSizeWidth, cropSizeHeight, cropSizeHeight, vpcOutBufferSize_, devMem->getId(), devMem->getDeviceId(), false, devMem->getFrameNb());
      void *outBufferDev_ = (void*)rgbMem->getMem();
  
      acldvppPicDesc *vpcOutputDesc_ = acldvppCreatePicDesc();
      acldvppSetPicDescData(vpcOutputDesc_, outBufferDev_);
      acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_BGR_888);
      acldvppSetPicDescWidth(vpcOutputDesc_, cropSizeWidth);
      acldvppSetPicDescHeight(vpcOutputDesc_, cropSizeHeight);
      acldvppSetPicDescWidthStride(vpcOutputDesc_, cropSizeWidth*3);
      acldvppSetPicDescHeightStride(vpcOutputDesc_, cropSizeHeight);
      acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_);
  
      bool bRet = false;
      do
      {
          ret = acldvppVpcCropAsync(dvppChannelDesc_, vpcInputDesc_, vpcOutputDesc_, cropArea_, stream_);
          if (ret != ACL_SUCCESS) {
              LOG_ERROR("acldvppVpcCropAsync failed, task_id : {}", devMem->getId());
              break;
          }
          ret = aclrtSynchronizeStream(stream_);
          if (ret != ACL_SUCCESS) {
              LOG_ERROR("aclrtSynchronizeStream failed, task_id : {}", devMem->getId());
              break;
          }
      } while (0);
  
      acldvppDestroyPicDesc(vpcInputDesc_);
      (void)acldvppDestroyPicDesc(vpcOutputDesc_);
      (void)acldvppDestroyRoiConfig(cropArea_);
  
      if(ret != ACL_ERROR_NONE){
          delete rgbMem;
          rgbMem = nullptr;
      }
  
      return rgbMem;
  }
  
  void VpcUtils::check_area(AreaInfo& area, int width, int height) {
      if (area.left < 0) {
          area.left = 0;
      }
      
      if (area.top < 0) {
          area.top = 0;
      }
  
      if (area.right > width) {
          area.right = width;
      }
  
      if (area.bottom > height) {
          area.bottom = height;
      }
  }
  
  vector<DvppDataMemory*> VpcUtils::crop_batch(DvppDataMemory* devMem, const vector<AreaInfo> objs) {
  
      vector<DvppDataMemory*> vec_img_info;
  
      const uint32_t outputBatchSize_ = objs.size();
      if(outputBatchSize_ <= 0){
          return vec_img_info;
      }
  
      aclError ret;
      aclrtSetDevice(m_devId);
      ret = aclrtSetCurrentContext(context_);
  
      // 输入
  	acldvppBatchPicDesc *vpcInputBatchDesc_ = acldvppCreateBatchPicDesc(1);
      if (vpcInputBatchDesc_ == nullptr) {
          LOG_ERROR("acldvppCreatePicDesc outBatchPicDesc failed");
          return vec_img_info;
      }
  	acldvppPicDesc *vpcInputDesc_ = acldvppGetPicDesc(vpcInputBatchDesc_, 0);
  	acldvppSetPicDescData(vpcInputDesc_, devMem->getMem()); 
  	acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_BGR_888);
  	acldvppSetPicDescWidth(vpcInputDesc_, devMem->getWidth());
  	acldvppSetPicDescHeight(vpcInputDesc_, devMem->getHeight());
  	acldvppSetPicDescWidthStride(vpcInputDesc_, devMem->getWidthStride());
  	acldvppSetPicDescHeightStride(vpcInputDesc_, devMem->getHeightStride());
  	acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize());
  
      // 输出
      acldvppBatchPicDesc *outputBatchPicDesc_ = acldvppCreateBatchPicDesc(outputBatchSize_);
      if (outputBatchPicDesc_ == nullptr) {
          LOG_ERROR("acldvppCreatePicDesc outBatchPicDesc failed");
          (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
          return vec_img_info;
      }
      vector<void *> vecOutPtr_;
      acldvppPicDesc *vpcOutputDesc = nullptr;
      acldvppRoiConfig *cropAreas[outputBatchSize_];
      for (uint32_t i = 0; i < outputBatchSize_; i++) {
          AreaInfo obj = objs[i];
  
          check_area(obj, devMem->getWidth(), devMem->getHeight());
  
          uint32_t cropSizeWidth = (obj.right - obj.left + 15) / 16 * 16; //debug by zsh
          uint32_t cropSizeHeight = (obj.bottom - obj.top + 1) / 2 * 2;
          
          uint32_t oddNum = 1;
          uint32_t cropLeftOffset = (obj.left + 1) / 2 * 2;  // must even
          uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum;  // must odd
          uint32_t cropTopOffset = (obj.top + 1) / 2 * 2;  // must even
          uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum;  // must odd
  
          adjustCoordinate(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset, devMem->getWidth(), devMem->getHeight());
          cropSizeWidth = cropRightOffset - cropLeftOffset + oddNum;
          cropSizeHeight = cropBottomOffset - cropTopOffset + oddNum;
          // LOG_DEBUG("{} ,{} ,{} ,{} ", cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  
          if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){
              LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset);
              // 释放之前成功的部分 再退出
              for(int j = 0; j < vecOutPtr_.size(); j++){
                  if (vecOutPtr_[j] != nullptr){
                      acldvppFree(vecOutPtr_[j]);
                  }
                  if (cropAreas[j] != nullptr) {
                      (void)acldvppDestroyRoiConfig(cropAreas[j]);
                      cropAreas[j] = nullptr;
                  }
              }
              (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
              return vec_img_info;
          }
          
          cropAreas[i] = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  
          uint32_t vpcOutBufferSize_ = cropSizeWidth * cropSizeHeight * 3;
          void *vpcBatchOutputBufferDev = nullptr;
          auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, vpcOutBufferSize_);
          if (ret != ACL_SUCCESS) {
              LOG_ERROR("acldvppMalloc failed, size = %u, errorCode = %d.", vpcOutBufferSize_, static_cast<int32_t>(ret));
              // 释放之前成功的部分 再退出
              for(int i = 0; i < vecOutPtr_.size(); i++){
                  if (vecOutPtr_[i] != nullptr){
                      acldvppFree(vecOutPtr_[i]);
                  }
                  if (cropAreas[i] != nullptr) {
                      (void)acldvppDestroyRoiConfig(cropAreas[i]);
                      cropAreas[i] = nullptr;
                  }
              }
              (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
              return vec_img_info;
          }
          vecOutPtr_.push_back(vpcBatchOutputBufferDev);
          vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
          (void)acldvppSetPicDescData(vpcOutputDesc, vpcBatchOutputBufferDev);
          (void)acldvppSetPicDescFormat(vpcOutputDesc, PIXEL_FORMAT_BGR_888);
          (void)acldvppSetPicDescWidth(vpcOutputDesc, cropSizeWidth);
          (void)acldvppSetPicDescHeight(vpcOutputDesc, cropSizeHeight);
          (void)acldvppSetPicDescWidthStride(vpcOutputDesc, cropSizeWidth*3);
          (void)acldvppSetPicDescHeightStride(vpcOutputDesc, cropSizeHeight);
          (void)acldvppSetPicDescSize(vpcOutputDesc, vpcOutBufferSize_);
      }
  
      bool bRet = false;
      do {
          uint32_t roiNums[] = { outputBatchSize_ };
          ret = acldvppVpcBatchCropAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums, 1, outputBatchPicDesc_, cropAreas, stream_);
          if (ret != ACL_SUCCESS) {
              LOG_ERROR("acldvppVpcBatchCropAsync failed, task_id : {}", devMem->getId());
              break;
          }
          ret = aclrtSynchronizeStream(stream_);
          if (ret != ACL_SUCCESS) {
              LOG_ERROR("aclrtSynchronizeStream failed, task_id : {}", devMem->getId());
              break;
          }
  
          for (uint32_t i = 0; i < outputBatchSize_; i++) {
  
              vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
              void *outputDataDev = acldvppGetPicDescData(vpcOutputDesc);
              uint32_t outputSize = acldvppGetPicDescSize(vpcOutputDesc);
              uint32_t width = acldvppGetPicDescWidth(vpcOutputDesc);
              uint32_t width_stride = acldvppGetPicDescWidthStride(vpcOutputDesc);
              uint32_t height = acldvppGetPicDescHeight(vpcOutputDesc);
              uint32_t height_stride = acldvppGetPicDescHeightStride(vpcOutputDesc);
              acldvppPixelFormat  fmt = acldvppGetPicDescFormat(vpcOutputDesc);
  
881ac79d   Hu Chunming   代码调通,结果正常输出
542
              DvppDataMemory* rgbMem = new DvppDataMemory(width, width_stride, height, height_stride, outputSize, devMem->getId(), devMem->getDeviceId(), false, devMem->getFrameNb(), (unsigned char *)outputDataDev);
4061a3c3   Hu Chunming   更换decoder
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  
              vec_img_info.push_back(rgbMem);
          }
  
          bRet = true;
      } while (0);
  
      if (vpcInputBatchDesc_ != nullptr) {
          (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
          vpcInputBatchDesc_ = nullptr;
      }
  
      if (!bRet){
          for(int i = 0; i < vecOutPtr_.size(); i++){
              if (vecOutPtr_[i] != nullptr){
                  acldvppFree(vecOutPtr_[i]);
              }
          }
      }
  
      if (outputBatchPicDesc_ != nullptr) {
          (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_);
          outputBatchPicDesc_ = nullptr;
      }
  
      for (uint32_t i = 0; i < outputBatchSize_; i++) {
          if (cropAreas[i] != nullptr) {
              (void)acldvppDestroyRoiConfig(cropAreas[i]);
              cropAreas[i] = nullptr;
          }
      }
  
      return vec_img_info;
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
576
  }