Commit 6eb42f5599598f69d1e8c8402370aec11c6b46cb

Authored by Hu Chunming
1 parent 193bf0d8

优化jpeg日志;修复抠图坐标越界问题

src/util/JpegUtil.cpp
... ... @@ -64,7 +64,7 @@ int32_t JpegUtil::jpege_save(char* pcData , uint32_t dataLen, string out_file_na
64 64 FILE* fd = nullptr;
65 65 fd = fopen(out_file_name.c_str(), "wb");
66 66 if (fd == nullptr) {
67   - LOG_ERROR("open output file err");
  67 + LOG_ERROR("open output file error");
68 68 return 1;
69 69 }
70 70  
... ... @@ -85,11 +85,13 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam
85 85 uint32_t outBufferSize= 0;
86 86 int ret = acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize);
87 87 if (ret != ACL_SUCCESS) {
  88 + LOG_ERROR("acldvppJpegPredictEncSize failed!");
88 89 return false;
89 90 }
90 91 void *encodeOutBufferDev_ = nullptr;
91 92 ret = acldvppMalloc(&encodeOutBufferDev_, outBufferSize);
92 93 if (ret != ACL_SUCCESS) {
  94 + LOG_ERROR("acldvppMalloc failed!");
93 95 return false;
94 96 }
95 97  
... ... @@ -98,29 +100,37 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam
98 100 // 9. 执行异步编码,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
99 101 aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_);
100 102 if (ret != ACL_SUCCESS) {
  103 + LOG_ERROR("acldvppJpegEncodeAsync failed!");
101 104 break;
102 105 }
103 106 aclRet = aclrtSynchronizeStream(stream_);
104 107 if (ret != ACL_SUCCESS) {
  108 + LOG_ERROR("aclrtSynchronizeStream failed!");
105 109 break;
106 110 }
107 111  
108 112 // 申请Host内存outputHostBuffer
109 113 void* outputHostBuffer = malloc(outBufferSize);
  114 + if(outputHostBuffer == nullptr) {
  115 + LOG_ERROR("malloc failed!");
  116 + break;
  117 + }
110 118  
111 119 // 通过aclrtMemcpy接口将Device的处理结果数据传输到Host
112 120 aclRet = aclrtMemcpy(outputHostBuffer, outBufferSize, encodeOutBufferDev_, outBufferSize, ACL_MEMCPY_DEVICE_TO_HOST);
113 121 if (ret != ACL_SUCCESS) {
114 122 free(outputHostBuffer);
115 123 outputHostBuffer = nullptr;
  124 + LOG_ERROR("aclrtMemcpy failed!");
116 125 break;
117 126 }
118 127  
119 128 // 数据使用完成后,释放内存
120 129 ret = jpege_save((char*)outputHostBuffer, outBufferSize, out_file_name);
  130 + free(outputHostBuffer);
  131 + outputHostBuffer = nullptr;
121 132 if(ret != 0) {
122   - free(outputHostBuffer);
123   - outputHostBuffer = nullptr;
  133 + LOG_ERROR("jpege_save failed!");
124 134 break;
125 135 }
126 136  
... ...
src/util/vpc_util.cpp
... ... @@ -18,6 +18,14 @@
18 18 #include "../decoder/interface/DeviceMemory.hpp"
19 19 #include "../common/logger.hpp"
20 20  
  21 +static uint32_t AlignSize(uint32_t origSize, uint32_t alignment){
  22 + if (alignment == 0) {
  23 + return 0;
  24 + }
  25 + uint32_t alignmentH = alignment - 1;
  26 + return (origSize + alignmentH) / alignment * alignment;
  27 +}
  28 +
21 29 void VPCUtil::release()
22 30 {
23 31 aclError ret;
... ... @@ -43,9 +51,39 @@ void VPCUtil::release()
43 51 LOG_INFO("end to reset device is %d", deviceId_);
44 52 }
45 53  
  54 +static void adjustCoordinate(uint32_t& left, uint32_t& right, uint32_t& top, uint32_t& bottom, const uint32_t& width, const uint32_t& height) {
  55 + uint32_t cropWidth = right - left;
  56 + uint32_t cropHeight = bottom - top;
  57 +
  58 + if(cropWidth < 16) {
  59 + cropWidth += 16; //小于16的似乎抠不了图
  60 + right += 16;
  61 + }
  62 +
  63 + if (left < 0) {
  64 + left = 0;
  65 + right = cropWidth;
  66 + }
  67 +
  68 + if (right >= width){
  69 + right = width -1;
  70 + left = right - cropWidth;
  71 + }
  72 +
  73 + if (top < 0) {
  74 + top = 0;
  75 + bottom = cropHeight;
  76 + }
  77 +
  78 + if (bottom >= height){
  79 + bottom = height -1;
  80 + top = bottom - cropHeight;
  81 + }
  82 +}
  83 +
46 84 vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) {
47 85  
48   - vpc_img_info img_info ;
  86 + vpc_img_info img_info;
49 87  
50 88 // uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16;
51 89 // uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2;
... ... @@ -59,7 +97,21 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) {
59 97 uint32_t cropTopOffset = (obj.top + 1) / 2 * 2; // must even
60 98 uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd
61 99  
  100 + adjustCoordinate(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset, devMem->getWidth(), devMem->getHeight());
  101 + cropSizeWidth = cropRightOffset - cropLeftOffset + oddNum; // 宽高要比实际的高才行,否则会抠图失败
  102 + cropSizeHeight = cropBottomOffset - cropTopOffset + oddNum;
  103 + LOG_DEBUG("{} ,{} ,{} ,{} ", cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  104 +
62 105 if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){
  106 + LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset);
  107 + return img_info;
  108 + }
  109 +
  110 + aclError ret;
  111 + aclrtSetDevice(deviceId_);
  112 + ret = aclrtSetCurrentContext(context_);
  113 + if (ret != ACL_SUCCESS) {
  114 + LOG_ERROR("aclrtSetCurrentContext failed");
63 115 return img_info;
64 116 }
65 117  
... ... @@ -94,7 +146,7 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) {
94 146 bool bRet = false;
95 147 do
96 148 {
97   - aclError ret = acldvppVpcCropAsync(dvppChannelDesc_, vpcInputDesc_, vpcOutputDesc_, cropArea_, stream_);
  149 + ret = acldvppVpcCropAsync(dvppChannelDesc_, vpcInputDesc_, vpcOutputDesc_, cropArea_, stream_);
98 150 if (ret != ACL_SUCCESS) {
99 151 LOG_ERROR("acldvppVpcCropAsync failed, task_id : {}", devMem->getId());
100 152 break;
... ... @@ -148,7 +200,6 @@ int VPCUtil::init(int32_t devId){
148 200 // channel 准备
149 201 dvppChannelDesc_ = acldvppCreateChannelDesc();
150 202 ret = acldvppCreateChannel(dvppChannelDesc_);
151   -
152 203 }
153 204  
154 205 vector<vpc_img_info> VPCUtil::crop_batch(DeviceMemory *devMem, vector<video_object_info> objs){
... ... @@ -192,10 +243,10 @@ vector&lt;vpc_img_info&gt; VPCUtil::crop_batch(DeviceMemory *devMem, vector&lt;video_obje
192 243 for (uint32_t i = 0; i < outputBatchSize_; i++) {
193 244 video_object_info obj = objs[i];
194 245  
195   - uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16;
196   - uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2;
197   - // uint32_t cropSizeWidth = (obj.right - obj.left + 15) / 16 * 16; //debug by zsh
198   - // uint32_t cropSizeHeight = (obj.bottom - obj.top + 1) / 2 * 2;
  246 + // uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16;
  247 + // uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2;
  248 + uint32_t cropSizeWidth = (obj.right - obj.left + 15) / 16 * 16; //debug by zsh
  249 + uint32_t cropSizeHeight = (obj.bottom - obj.top + 1) / 2 * 2;
199 250  
200 251 uint32_t oddNum = 1;
201 252 uint32_t cropLeftOffset = (obj.left + 1) / 2 * 2; // must even
... ... @@ -203,6 +254,11 @@ vector&lt;vpc_img_info&gt; VPCUtil::crop_batch(DeviceMemory *devMem, vector&lt;video_obje
203 254 uint32_t cropTopOffset = (obj.top + 1) / 2 * 2; // must even
204 255 uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd
205 256  
  257 + adjustCoordinate(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset, devMem->getWidth(), devMem->getHeight());
  258 + cropSizeWidth = cropRightOffset - cropLeftOffset + oddNum;
  259 + cropSizeHeight = cropBottomOffset - cropTopOffset + oddNum;
  260 + LOG_DEBUG("{} ,{} ,{} ,{} ", cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  261 +
206 262 if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){
207 263 LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset);
208 264 // 释放之前成功的部分 再退出
... ...