Commit b6c85a1ee67ed516db7df4041ddff3e14f9c04da
1 parent
d708ccb3
修复jni中解码失败导致的内存泄露问题
Showing
1 changed file
with
95 additions
and
99 deletions
jni/VehicleNativeInterface.cpp
... | ... | @@ -6,6 +6,7 @@ |
6 | 6 | #include <string> |
7 | 7 | #include <string.h> |
8 | 8 | #include <chrono> |
9 | +#include <mutex> | |
9 | 10 | |
10 | 11 | #include "../src/village_pic_interface.h" |
11 | 12 | |
... | ... | @@ -17,6 +18,7 @@ |
17 | 18 | //dvpp���� |
18 | 19 | #include "dvpp_process.h" |
19 | 20 | #include "utils.h" |
21 | +#include "logger.hpp" | |
20 | 22 | //#include "img_codec.h" |
21 | 23 | |
22 | 24 | double msecond() { |
... | ... | @@ -111,6 +113,11 @@ sy_command global_vehicle_manned_config; //�Ƿ������� |
111 | 113 | //sy_command global_vehicle_pose_config; //�Ƿ�����pose��� //20210618 atlas |
112 | 114 | |
113 | 115 | int gpu_id; |
116 | +DvppProcess* dvpp = nullptr; | |
117 | +aclrtContext ctx = nullptr; | |
118 | +aclrtStream stream = nullptr; | |
119 | + | |
120 | +std::mutex m_single_mtx; | |
114 | 121 | |
115 | 122 | /* |
116 | 123 | * Class: com_objecteye_nativeinterface_vehicle_VehicleNativeInterface |
... | ... | @@ -151,16 +158,20 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
151 | 158 | param.log_days = aiEngineParam_logDays; |
152 | 159 | param.log_mem = aiEngineParam_logMem; |
153 | 160 | |
154 | - printf("jni dev_id:%d \n", param.dev_id); | |
155 | - printf("jni sdk_path:%s_\n", param.sdk_path.c_str()); | |
156 | - printf("jni log_level:%d\n", param.log_level); | |
157 | - printf("jni log_path:%s\n", param.log_path.c_str()); | |
158 | - printf("jni log_days:%d\n", param.log_days); | |
159 | - printf("jni log_mem:%ld\n", param.log_mem); | |
161 | + | |
162 | + string log_path = param.log_path + "/jni.log"; | |
163 | + set_default_logger(LogLevel(param.log_level), "PicAnalysis", log_path.c_str(), param.log_mem, param.log_days); | |
164 | + | |
165 | + LOG_INFO("jni dev_id:{}", param.dev_id); | |
166 | + LOG_INFO("jni sdk_path:{}", param.sdk_path.c_str()); | |
167 | + LOG_INFO("jni log_level:{}", param.log_level); | |
168 | + LOG_INFO("jni log_path:{}", param.log_path.c_str()); | |
169 | + LOG_INFO("jni log_days:{}", param.log_days); | |
170 | + LOG_INFO("jni log_mem:{}", param.log_mem); | |
160 | 171 | |
161 | 172 | int ret = village_pic_init(&vaHandle, param); |
162 | 173 | if (ret != SUCCESS) { |
163 | - printf("jni info:va_init failed."); | |
174 | + LOG_ERROR("jni village_pic_init failed."); | |
164 | 175 | return ret; |
165 | 176 | } |
166 | 177 | |
... | ... | @@ -182,33 +193,21 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
182 | 193 | env->SetLongArrayRegion(handle, 0, 1, temp); |
183 | 194 | } |
184 | 195 | env->ReleaseStringUTFChars(str_sdk_path, sdk_path); |
185 | - return ret; | |
186 | -} | |
196 | + env->ReleaseStringUTFChars(str_aiEngineParam_logPath, aiEngineParam_logPath); | |
187 | 197 | |
198 | + ACL_CALL(aclrtCreateContext(&ctx, gpu_id), ACL_ERROR_NONE, FAILED); | |
199 | + ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, FAILED); | |
188 | 200 | |
201 | + ACL_CALL(aclrtCreateStream(&stream), ACL_ERROR_NONE, FAILED); | |
202 | + | |
203 | + dvpp = new DvppProcess(); | |
204 | + dvpp->InitResource(stream); | |
189 | 205 | |
190 | -void CvBGR2NV21(cv::Mat& bgr, unsigned char* yuv) { | |
191 | - int stride = (bgr.cols + 127) / 128 * 128; | |
192 | - int strideH = (bgr.rows + 15) / 16 * 16; | |
193 | - for (int i = 0; i < bgr.rows; i++) { | |
194 | - for (int j = 0; j < bgr.cols; j++) { | |
195 | - int B = bgr.at<cv::Vec3b>(i, j)[0]; | |
196 | - int G = bgr.at<cv::Vec3b>(i, j)[1]; | |
197 | - int R = bgr.at<cv::Vec3b>(i, j)[2]; | |
206 | + LOG_ERROR("jni init succeed."); | |
198 | 207 | |
199 | - int Y = (77 * R + 150 * G + 29 * B) >> 8; | |
200 | - yuv[i * stride + j] = (Y < 0) ? 0 : ((Y > 255) ? 255 : Y); | |
201 | - if (i % 2 == 0 && j % 2 == 0) { | |
202 | - int U = ((-44 * R - 87 * G + 131 * B) >> 8) + 128; | |
203 | - int V = ((131 * R - 110 * G - 21 * B) >> 8) + 128; | |
204 | - yuv[strideH * stride + i / 2 * stride + j] = (V < 0) ? 0 : ((V > 255) ? 255 : V); | |
205 | - yuv[strideH * stride + i / 2 * stride + j + 1] = (U < 0) ? 0 : ((U > 255) ? 255 : U); | |
206 | - } | |
207 | - } | |
208 | - } | |
208 | + return ret; | |
209 | 209 | } |
210 | 210 | |
211 | - | |
212 | 211 | /* |
213 | 212 | * Class: com_objecteye_nativeinterface_vehicle_VehicleNativeInterface |
214 | 213 | * Method: batch |
... | ... | @@ -218,35 +217,25 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
218 | 217 | { |
219 | 218 | int ret = FAILED; |
220 | 219 | void *vaHandle = (void *)handle; |
220 | + if (nullptr == vaHandle || nullptr == dvpp || nullptr == stream || nullptr == ctx) | |
221 | + { | |
222 | + return ret; | |
223 | + } | |
224 | + | |
225 | + std::lock_guard<std::mutex> l(m_single_mtx); | |
221 | 226 | |
222 | - // ��ȡͼƬ���� | |
223 | 227 | jclass cls_syImgParam = env->FindClass("com/objecteye/pojo/common/SyImgParam"); |
224 | 228 | jfieldID fid_image_str = env->GetFieldID(cls_syImgParam, "image_string", "Ljava/lang/String;");//base64�����ⲿͳһ��url��fileת��base64���� |
225 | 229 | |
226 | 230 | sy_img batch_img[batchSize]; |
227 | - cv::Mat car_images[batchSize]; | |
228 | 231 | ImageData dvpp_data[batchSize]; |
229 | 232 | |
230 | - aclrtContext ctx; | |
231 | - ACL_CALL(aclrtCreateContext(&ctx, gpu_id), ACL_ERROR_NONE, FAILED); | |
232 | 233 | ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, FAILED); |
233 | - //��ʼ��dvpp | |
234 | - aclrtStream stream = nullptr; | |
235 | - ACL_CALL(aclrtCreateStream(&stream), ACL_ERROR_NONE, FAILED); | |
236 | - | |
237 | - DvppProcess* dvpp = new DvppProcess(); | |
238 | - dvpp->InitResource(stream); | |
239 | - //Base64Utils decoder(); | |
240 | - | |
241 | - | |
242 | - //wh20210623 | |
243 | - std::shared_ptr<uint8_t> NV21data[batchSize]; | |
244 | - | |
245 | 234 | |
246 | - if (format != 0 && format != 1) | |
235 | + if (format != 1) | |
247 | 236 | { |
248 | - printf("ERROR FORMAT TYPE: %d\n", format); | |
249 | - return -1; | |
237 | + LOG_ERROR("ERROR FORMAT TYPE: {}", format); | |
238 | + return FAILED; | |
250 | 239 | } |
251 | 240 | |
252 | 241 | double t1,t2,t3; |
... | ... | @@ -258,48 +247,44 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
258 | 247 | jstring imageString1 = (jstring)env->GetObjectField(syImgParam, fid_image_str); |
259 | 248 | const char *image_str = env->GetStringUTFChars(imageString1, JNI_FALSE); |
260 | 249 | string imageString = image_str; |
261 | - | |
262 | - //printf("format = %d\n", format); | |
263 | - if (format == 0) //url | |
264 | - { | |
265 | - printf("url is not surposed \n"); | |
266 | - } | |
267 | - else | |
268 | - { | |
269 | - | |
270 | - string decode_str; | |
271 | - //decode_str = decoder->Decode(encode_str[b].data(), encode_str[b].size()); | |
272 | - decode_str = Decode(imageString.data(), imageString.size()); | |
273 | - | |
274 | - //debug======================================================== | |
275 | - ImageData src; | |
276 | - uint32_t binFileBufferLen = decode_str.size() + 8;//�ֽ��� | |
277 | - uint8_t* binFileBufferData = new(std::nothrow) uint8_t[binFileBufferLen]; | |
278 | - | |
279 | - if (binFileBufferData == nullptr) { | |
280 | - ERROR_LOG("malloc binFileBufferData failed"); | |
281 | - return FAILED; | |
282 | - } | |
283 | - | |
284 | - memcpy((char *)binFileBufferData, decode_str.data(), binFileBufferLen); | |
285 | - int32_t ch = 0; | |
286 | - ACL_CALL(acldvppJpegGetImageInfo(binFileBufferData, binFileBufferLen, | |
287 | - &(src.width), &(src.height), &ch), SUCCESS, FAILED); | |
288 | - src.data.reset(binFileBufferData, [](uint8_t* p) { delete[](p); }); | |
289 | - src.size = binFileBufferLen; | |
290 | - // printf("vpdr_api_batch:img.w=%d ,img.h=%d \n",src.width,src.height); | |
291 | 250 | |
292 | - | |
293 | - ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data[i], src), SUCCESS, FAILED); | |
294 | - | |
295 | - batch_img[i].w_ = dvpp_data[i].width;//dvpp_data[b].alignWidth; | |
296 | - batch_img[i].h_ = dvpp_data[i].height;//dvpp_data[b].alignHeight; | |
297 | - //batch_img[i].data_ = (uint8_t*)Utils::CopyDataDeviceToLocal(dvpp_data[i].data.get(), dvpp_data[i].size);//dvpp_data[b].data.get(); | |
298 | - batch_img[i].data_ = dvpp_data[i].data.get(); | |
299 | - | |
300 | - } | |
301 | - | |
302 | - env->ReleaseStringUTFChars(imageString1, image_str); | |
251 | + string decode_str; | |
252 | + //decode_str = decoder->Decode(encode_str[b].data(), encode_str[b].size()); | |
253 | + decode_str = Decode(imageString.data(), imageString.size()); | |
254 | + env->ReleaseStringUTFChars(imageString1, image_str); | |
255 | + | |
256 | + //debug======================================================== | |
257 | + ImageData src; | |
258 | + uint32_t binFileBufferLen = decode_str.size() + 8;//�ֽ��� | |
259 | + uint8_t* binFileBufferData = new(std::nothrow) uint8_t[binFileBufferLen]; | |
260 | + | |
261 | + if (binFileBufferData == nullptr) { | |
262 | + LOG_ERROR("malloc binFileBufferData failed"); | |
263 | + return FAILED; | |
264 | + } | |
265 | + | |
266 | + src.data.reset(binFileBufferData, [](uint8_t* p) { delete[](p); }); | |
267 | + | |
268 | + memcpy((char *)binFileBufferData, decode_str.data(), binFileBufferLen); | |
269 | + int32_t ch = 0; | |
270 | + ret = acldvppJpegGetImageInfo(binFileBufferData, binFileBufferLen, &(src.width), &(src.height), &ch); | |
271 | + if (SUCCESS != ret) { | |
272 | + LOG_ERROR("acldvppJpegGetImageInfo failed"); | |
273 | + return FAILED; | |
274 | + } | |
275 | + | |
276 | + src.size = binFileBufferLen; | |
277 | + | |
278 | + ret = dvpp->CvtJpegToYuv420sp(dvpp_data[i], src); | |
279 | + if (SUCCESS != ret) { | |
280 | + LOG_ERROR("CvtJpegToYuv420sp failed"); | |
281 | + return FAILED; | |
282 | + } | |
283 | + | |
284 | + batch_img[i].w_ = dvpp_data[i].width;//dvpp_data[b].alignWidth; | |
285 | + batch_img[i].h_ = dvpp_data[i].height;//dvpp_data[b].alignHeight; | |
286 | + //batch_img[i].data_ = (uint8_t*)Utils::CopyDataDeviceToLocal(dvpp_data[i].data.get(), dvpp_data[i].size);//dvpp_data[b].data.get(); | |
287 | + batch_img[i].data_ = dvpp_data[i].data.get(); | |
303 | 288 | } |
304 | 289 | t2 = msecond(); |
305 | 290 | //printf("va jni info:decode time: %.2f\n", (t2 - t1)); |
... | ... | @@ -309,8 +294,8 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
309 | 294 | std::vector<AnalysisResult> vec_result = village_pic_analysis(vaHandle, batch_img, batchSize); |
310 | 295 | if (vec_result.size() <= 0 || vec_result.size() != batchSize) |
311 | 296 | { |
312 | - printf("jni info:village_pic_analysis failed."); | |
313 | - return -1; | |
297 | + LOG_ERROR("village_pic_analysis failed."); | |
298 | + return FAILED; | |
314 | 299 | } |
315 | 300 | cout << "result size:" << vec_result.size() << endl; |
316 | 301 | |
... | ... | @@ -344,7 +329,7 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
344 | 329 | jclass cls_StainVplateResultParam = env->FindClass("com/objecteye/pojo/vehicle/StainVplateResultParam"); |
345 | 330 | if (nullptr == cls_StainVplateResultParam) |
346 | 331 | { |
347 | - cout << "cls_StainVplateResultParam find class error!" << endl; | |
332 | + LOG_ERROR("cls_StainVplateResultParam find class error!"); | |
348 | 333 | } |
349 | 334 | |
350 | 335 | jclass cls_MannedResultParam = env->FindClass("com/objecteye/pojo/vehicle/MannedResultParam"); |
... | ... | @@ -366,13 +351,13 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
366 | 351 | jmethodID mid_VplateResult = env->GetMethodID(cls_VplateResult, "<init>", "(Lcom/objecteye/pojo/common/SyRectParam;F[Lcom/objecteye/pojo/vehicle/VehiclePlateNumParam;FIILcom/objecteye/pojo/vehicle/StainVplateResultParam;)V"); |
367 | 352 | if (nullptr == mid_VplateResult) |
368 | 353 | { |
369 | - cout << "mid_VplateResult GetMethodID error!" << endl; | |
354 | + LOG_ERROR("mid_VplateResult GetMethodID error!"); | |
370 | 355 | } |
371 | 356 | jmethodID mid_VehiclePendantDetResult = env->GetMethodID(cls_VehiclePendantDetResult, "<init>", "([Lcom/objecteye/pojo/vehicle/VehiclePendantDetectInfoParam;I)V"); |
372 | 357 | jmethodID mid_VehicleIllegalDetResult = env->GetMethodID(cls_VehicleIllegalDetResult, "<init>", "(Lcom/objecteye/pojo/vehicle/VehicleIllegalDetectInfoParam;Lcom/objecteye/pojo/vehicle/VehicleIllegalDetectInfoParam;)V"); |
373 | 358 | if (nullptr == mid_VehicleIllegalDetResult) |
374 | 359 | { |
375 | - cout << "mid_VehicleIllegalDetResult GetMethodID error!" << endl; | |
360 | + LOG_ERROR("mid_VehicleIllegalDetResult GetMethodID error!"); | |
376 | 361 | } |
377 | 362 | jmethodID mid_VehicleFeaResult = env->GetMethodID(cls_VehicleFeaResult, "<init>", "([F)V"); |
378 | 363 | jmethodID mid_SyRect = env->GetMethodID(cls_SyRect, "<init>", "(IIII)V"); |
... | ... | @@ -618,12 +603,6 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
618 | 603 | env->SetObjectArrayElement(vehicleAnalysisResultParams, i, vehicleAnalysisResult); |
619 | 604 | } |
620 | 605 | |
621 | - ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, FAILED); | |
622 | - delete dvpp; | |
623 | - dvpp = nullptr; | |
624 | - (void) aclrtDestroyStream(stream); | |
625 | - aclrtDestroyContext(ctx); | |
626 | - | |
627 | 606 | return SUCCESS; |
628 | 607 | } |
629 | 608 | |
... | ... | @@ -639,6 +618,23 @@ JNIEXPORT void JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI |
639 | 618 | { |
640 | 619 | village_pic_release(&vaHandle); |
641 | 620 | } |
621 | + | |
622 | + if (ctx) { | |
623 | + aclrtSetCurrentContext(ctx); | |
624 | + } | |
625 | + | |
626 | + if (dvpp) { | |
627 | + delete dvpp; | |
628 | + dvpp = nullptr; | |
629 | + } | |
630 | + | |
631 | + if (stream) { | |
632 | + (void) aclrtDestroyStream(stream); | |
633 | + } | |
634 | + | |
635 | + if (ctx) { | |
636 | + aclrtDestroyContext(ctx); | |
637 | + } | |
642 | 638 | } |
643 | 639 | |
644 | 640 | /* | ... | ... |