Commit e109b00191e1296245a93343245d320454d2eb00

Authored by Hu Chunming
1 parent 4613ed54

修正多显卡问题

src/ai_platform/MultiSourceProcess.cpp
@@ -9,7 +9,6 @@ @@ -9,7 +9,6 @@
9 9
10 #include "../decoder/interface/DecoderManager.h" 10 #include "../decoder/interface/DecoderManager.h"
11 #include "../decoder/interface/utiltools.hpp" 11 #include "../decoder/interface/utiltools.hpp"
12 -#include "../util/vpc_util.h"  
13 #include "../util/crop_process.h" 12 #include "../util/crop_process.h"
14 #include "../helpers/time_helper.hpp" 13 #include "../helpers/time_helper.hpp"
15 #include "../helpers/os_helper.hpp" 14 #include "../helpers/os_helper.hpp"
@@ -17,6 +16,7 @@ @@ -17,6 +16,7 @@
17 #include "../reprocessing_module/save_snapshot_reprocessing.h" 16 #include "../reprocessing_module/save_snapshot_reprocessing.h"
18 17
19 #include "macro_definition.h" 18 #include "macro_definition.h"
  19 +#include "SourceSingleton.hpp"
20 20
21 #define VEHICLE_MULTI_BOXES 21 #define VEHICLE_MULTI_BOXES
22 22
@@ -59,12 +59,10 @@ void decode_finished_cbk(const void * userPtr){ @@ -59,12 +59,10 @@ void decode_finished_cbk(const void * userPtr){
59 } 59 }
60 60
61 CMultiSourceProcess::CMultiSourceProcess(){ 61 CMultiSourceProcess::CMultiSourceProcess(){
62 - aclInit(nullptr);  
63 } 62 }
64 63
65 CMultiSourceProcess::~CMultiSourceProcess(){ 64 CMultiSourceProcess::~CMultiSourceProcess(){
66 - dvpp_crop_release();  
67 - aclFinalize(); 65 + vpc_util.vpc_crop_release();
68 } 66 }
69 67
70 int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){ 68 int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){
@@ -72,6 +70,8 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){ @@ -72,6 +70,8 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){
72 set_default_logger(LogLevel(vptParam.log_level), "multi_source_process", vptParam.log_path, vptParam.log_mem, vptParam.log_mem); 70 set_default_logger(LogLevel(vptParam.log_level), "multi_source_process", vptParam.log_path, vptParam.log_mem, vptParam.log_mem);
73 LOG_INFO("编译时间:{} {}", __DATE__, __TIME__); 71 LOG_INFO("编译时间:{} {}", __DATE__, __TIME__);
74 72
  73 + SourceSingleton::getInstance();
  74 +
75 skip_frame_ = 5; 75 skip_frame_ = 5;
76 m_batch_size = 16; 76 m_batch_size = 16;
77 77
@@ -93,7 +93,7 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){ @@ -93,7 +93,7 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){
93 m_snapshot_reprocessing = snapshot_reprocessing::getInstance(); 93 m_snapshot_reprocessing = snapshot_reprocessing::getInstance();
94 m_save_snapshot_reprocessing = new save_snapshot_reprocessing(m_devId); 94 m_save_snapshot_reprocessing = new save_snapshot_reprocessing(m_devId);
95 95
96 - dvpp_crop_init(m_devId); 96 + vpc_util.vpc_crop_init(m_devId);
97 97
98 m_pAlgorthimThread = new thread([](void* arg) { 98 m_pAlgorthimThread = new thread([](void* arg) {
99 CMultiSourceProcess* process = (CMultiSourceProcess*)arg ; 99 CMultiSourceProcess* process = (CMultiSourceProcess*)arg ;
@@ -483,13 +483,13 @@ int CMultiSourceProcess::algorithm_vehicle_relult(vector<DeviceMemory*> vec_devM @@ -483,13 +483,13 @@ int CMultiSourceProcess::algorithm_vehicle_relult(vector<DeviceMemory*> vec_devM
483 483
484 ImgSaveInfo saveInfo; 484 ImgSaveInfo saveInfo;
485 saveInfo.file_path = fpath_origin; 485 saveInfo.file_path = fpath_origin;
486 - saveInfo.img_info = dvpp_devMem2vpcImg(result.memPtr); 486 + saveInfo.img_info = vpc_util.vpc_devMem2vpcImg(result.memPtr);
487 m_save_snapshot_reprocessing->reprocessing_process_wo_locus_async(saveInfo); 487 m_save_snapshot_reprocessing->reprocessing_process_wo_locus_async(saveInfo);
488 488
489 - vector<vpc_img_info> vec_obj_info_list = dvpp_crop_batch(result.memPtr, result.objs); 489 + vector<vpc_img_info> vec_obj_info_list = vpc_util.vpc_crop_batch(result.memPtr, result.objs);
490 if(vec_obj_info_list.size() != result.objs.size()){ 490 if(vec_obj_info_list.size() != result.objs.size()){
491 LOG_ERROR("vpc_crop size error !"); 491 LOG_ERROR("vpc_crop size error !");
492 - dvpp_imgList_release(vec_obj_info_list); 492 + vpc_util.vpc_imgList_release(vec_obj_info_list);
493 continue; 493 continue;
494 } 494 }
495 495
src/ai_platform/MultiSourceProcess.h
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 #include "../ai_engine_module/VPTProcess.h" 5 #include "../ai_engine_module/VPTProcess.h"
6 #include "../reprocessing_module/snapshot_reprocessing.h" 6 #include "../reprocessing_module/snapshot_reprocessing.h"
7 #include "../reprocessing_module/save_snapshot_reprocessing.h" 7 #include "../reprocessing_module/save_snapshot_reprocessing.h"
  8 +#include "../util/vpc_util.h"
8 9
9 #include <map> 10 #include <map>
10 #include <mutex> 11 #include <mutex>
@@ -78,6 +79,8 @@ private: @@ -78,6 +79,8 @@ private:
78 set<OBJ_KEY> m_total_snapshot_info_multi_object; 79 set<OBJ_KEY> m_total_snapshot_info_multi_object;
79 mutex m_total_mutex; 80 mutex m_total_mutex;
80 81
  82 + VPCUtil vpc_util;
  83 +
81 #ifdef POST_USE_RABBITMQ 84 #ifdef POST_USE_RABBITMQ
82 mq::Manager *mq_manager_{nullptr}; 85 mq::Manager *mq_manager_{nullptr};
83 #endif 86 #endif
src/ai_platform/SourceSingleton.hpp 0 → 100644
  1 +#include "acl/acl.h"
  2 +#include "acl/ops/acl_dvpp.h"
  3 +
  4 +class SourceSingleton {
  5 +public:
  6 + /**************************************************
  7 + * 接口:getInstance
  8 + * 功能:获取解码器管理者实例
  9 + * 参数:无
  10 + * 返回:成功返回 解码器管理者实例, 失败返回 nullptr
  11 + * 备注:调用其他接口前,需要先调用该接口获取管理者实例
  12 + **************************************************/
  13 + static SourceSingleton* getInstance(){
  14 + static SourceSingleton* singleton = nullptr;
  15 + if (singleton == nullptr){
  16 + singleton = new SourceSingleton();
  17 + }
  18 + return singleton;
  19 + }
  20 +
  21 + SourceSingleton(){
  22 + aclInit(nullptr);
  23 + }
  24 +
  25 + ~SourceSingleton(){
  26 + aclFinalize();
  27 + }
  28 +};
0 \ No newline at end of file 29 \ No newline at end of file
src/demo/demo.cpp
@@ -499,20 +499,7 @@ string createTask(void *handle, std::vector&lt;algorithm_type_t&gt; algor_vec, int gi) @@ -499,20 +499,7 @@ string createTask(void *handle, std::vector&lt;algorithm_type_t&gt; algor_vec, int gi)
499 return task_id_str; 499 return task_id_str;
500 } 500 }
501 501
502 -int main(int argc, char *argv[]) {  
503 - printf("new test\n");  
504 -  
505 - if (argc < 4) {  
506 - fprintf(stderr, "./xxx 0 2 10 1 ## [start_ai_id, end_ai_id) repeat_num gpu_id\n");  
507 - return -1;  
508 - }  
509 -  
510 - //! load params.  
511 - int start_id = atoi(argv[1]);  
512 - int end_id = atoi(argv[2]);  
513 - int repeat_num = atoi(argv[3]);  
514 - int gpuID = atoi(argv[4]);  
515 - 502 +void test_gpu(int gpuID){
516 tsl_aiplatform_param vptParam; 503 tsl_aiplatform_param vptParam;
517 vptParam.gpuid = gpuID; 504 vptParam.gpuid = gpuID;
518 vptParam.trt_serialize_file = ""; 505 vptParam.trt_serialize_file = "";
@@ -529,8 +516,7 @@ int main(int argc, char *argv[]) { @@ -529,8 +516,7 @@ int main(int argc, char *argv[]) {
529 int flag = tsl_aiplatform_init(&handle, vptParam); 516 int flag = tsl_aiplatform_init(&handle, vptParam);
530 if (0 != flag) { 517 if (0 != flag) {
531 printf("Init Failed! Error Code: %d\n", flag); 518 printf("Init Failed! Error Code: %d\n", flag);
532 - system("pause");  
533 - return 0; 519 + return;
534 } else { 520 } else {
535 printf("Init Success\n"); 521 printf("Init Success\n");
536 } 522 }
@@ -538,14 +524,36 @@ int main(int argc, char *argv[]) { @@ -538,14 +524,36 @@ int main(int argc, char *argv[]) {
538 std::vector<algorithm_type_t> algor_vec = {algorithm_type_t::FACE_SNAPSHOT, algorithm_type_t::HUMAN_SNAPSHOT,algorithm_type_t::ROAD_WORK_DET, 524 std::vector<algorithm_type_t> algor_vec = {algorithm_type_t::FACE_SNAPSHOT, algorithm_type_t::HUMAN_SNAPSHOT,algorithm_type_t::ROAD_WORK_DET,
539 algorithm_type_t::VEHICLE_SNAPSHOT, algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT, algorithm_type_t::VIDEO_TIMING_SNAPSHOT}; 525 algorithm_type_t::VEHICLE_SNAPSHOT, algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT, algorithm_type_t::VIDEO_TIMING_SNAPSHOT};
540 526
541 - string task_id = createTask(handle, algor_vec, 0); 527 + string task_id = createTask(handle, algor_vec, 0 + gpuID * 10);
  528 + string task_id1 = createTask(handle, algor_vec, 1 + gpuID * 10);
  529 + string task_id2 = createTask(handle, algor_vec, 2 + gpuID * 10);
542 530
543 - while (getchar() != 'q');  
544 531
545 - finish_task(handle, (char*)task_id.data(), 0); 532 + // finish_task(handle, (char*)task_id.data(), 0);
  533 +
  534 + // tsl_aiplatform_release(&handle);
  535 +}
  536 +
  537 +int main(int argc, char *argv[]) {
  538 + printf("new test\n");
  539 +
  540 + if (argc < 4) {
  541 + fprintf(stderr, "./xxx 0 2 10 1 ## [start_ai_id, end_ai_id) repeat_num gpu_id\n");
  542 + return -1;
  543 + }
  544 +
  545 + //! load params.
  546 + int start_id = atoi(argv[1]);
  547 + int end_id = atoi(argv[2]);
  548 + int repeat_num = atoi(argv[3]);
  549 + int gpuID = atoi(argv[4]);
  550 +
  551 + test_gpu(0);
  552 + test_gpu(1);
  553 +
  554 + while (getchar() != 'q');
546 555
547 - tsl_aiplatform_release(&handle);  
548 - printf("Done.\n"); 556 + printf("Done.\n");
549 557
550 return 0; 558 return 0;
551 } 559 }
552 \ No newline at end of file 560 \ No newline at end of file
src/reprocessing_module/save_snapshot_reprocessing.cpp
@@ -5,8 +5,6 @@ @@ -5,8 +5,6 @@
5 #include <opencv2/imgproc/types_c.h> 5 #include <opencv2/imgproc/types_c.h>
6 #include <algorithm> 6 #include <algorithm>
7 #include "../common/logger.hpp" 7 #include "../common/logger.hpp"
8 -#include "../util/JpegUtil.h"  
9 -#include "../util/vpc_util.h"  
10 8
11 const bool DRAW_ON_IMG = false; 9 const bool DRAW_ON_IMG = false;
12 10
@@ -28,14 +26,14 @@ save_snapshot_reprocessing::save_snapshot_reprocessing(int devId) { @@ -28,14 +26,14 @@ save_snapshot_reprocessing::save_snapshot_reprocessing(int devId) {
28 m_save_img_thread = std::thread(save_img_thread_process, this); 26 m_save_img_thread = std::thread(save_img_thread_process, this);
29 27
30 m_devId = devId; 28 m_devId = devId;
31 - DVPP_UTIL::dvpp_jpeg_init(m_devId); 29 + jpegUtil.jpeg_init(m_devId);
32 } 30 }
33 31
34 save_snapshot_reprocessing::~save_snapshot_reprocessing(){ 32 save_snapshot_reprocessing::~save_snapshot_reprocessing(){
35 // 结束线程 33 // 结束线程
36 bFinish = true; 34 bFinish = true;
37 m_save_img_thread.join(); 35 m_save_img_thread.join();
38 - DVPP_UTIL::dvpp_jpeg_release(); 36 + jpegUtil.jpeg_release();
39 } 37 }
40 38
41 // 释放资源 39 // 释放资源
@@ -48,7 +46,7 @@ void save_snapshot_reprocessing::save_snapshot_reprocessing_release() { @@ -48,7 +46,7 @@ void save_snapshot_reprocessing::save_snapshot_reprocessing_release() {
48 waitforsave_img_queue.pop(); 46 waitforsave_img_queue.pop();
49 47
50 if(!cur_image.file_path.empty()){ 48 if(!cur_image.file_path.empty()){
51 - dvpp_img_release(cur_image.img_info); 49 + vpc_util.vpc_img_release(cur_image.img_info);
52 } 50 }
53 } 51 }
54 52
@@ -96,9 +94,9 @@ void save_snapshot_reprocessing::save_img_process() { @@ -96,9 +94,9 @@ void save_snapshot_reprocessing::save_img_process() {
96 l.unlock(); 94 l.unlock();
97 95
98 if(!cur_image.file_path.empty()){ 96 if(!cur_image.file_path.empty()){
99 - DVPP_UTIL::dvpp_jpeg_encode(cur_image.img_info.pic_desc, cur_image.file_path); 97 + jpegUtil.jpeg_encode(cur_image.img_info.pic_desc, cur_image.file_path);
100 } 98 }
101 - dvpp_img_release(cur_image.img_info); 99 + vpc_util.vpc_img_release(cur_image.img_info);
102 100
103 #ifdef POST_USE_RABBITMQ 101 #ifdef POST_USE_RABBITMQ
104 if (callback_ != nullptr && cur_image.json_str.length() > 0) { 102 if (callback_ != nullptr && cur_image.json_str.length() > 0) {
src/reprocessing_module/save_snapshot_reprocessing.h
@@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
16 16
17 #include "../ai_platform/det_obj_header.h" 17 #include "../ai_platform/det_obj_header.h"
18 #include "../util/vpc_util.h" 18 #include "../util/vpc_util.h"
  19 +#include "../util/JpegUtil.h"
19 20
20 #ifdef POST_USE_RABBITMQ 21 #ifdef POST_USE_RABBITMQ
21 #include "post_reprocessing.hpp" 22 #include "post_reprocessing.hpp"
@@ -57,6 +58,9 @@ private: @@ -57,6 +58,9 @@ private:
57 bool bFinish = false; 58 bool bFinish = false;
58 int m_devId; 59 int m_devId;
59 60
  61 + JpegUtil jpegUtil;
  62 + VPCUtil vpc_util;
  63 +
60 #ifdef POST_USE_RABBITMQ 64 #ifdef POST_USE_RABBITMQ
61 callback_t callback_; 65 callback_t callback_;
62 std::shared_ptr<mq::post_rabbitmq_reprocessing> rbmq_handler_; 66 std::shared_ptr<mq::post_rabbitmq_reprocessing> rbmq_handler_;
src/reprocessing_module/snapshot_reprocessing.cpp
@@ -150,7 +150,7 @@ void snapshot_reprocessing::filter_vehicle(vector&lt;DeviceMemory*&gt; vec_devMem, vec @@ -150,7 +150,7 @@ void snapshot_reprocessing::filter_vehicle(vector&lt;DeviceMemory*&gt; vec_devMem, vec
150 int task_idx = 0; 150 int task_idx = 0;
151 151
152 for (auto memPtr : vec_devMem){ 152 for (auto memPtr : vec_devMem){
153 - string taskid = memPtr ->getId(); 153 + string taskid = memPtr->getId();
154 154
155 int effective_count = 0; 155 int effective_count = 0;
156 int effective_idx = 0; 156 int effective_idx = 0;
src/util/JpegUtil.cpp
@@ -5,110 +5,102 @@ @@ -5,110 +5,102 @@
5 5
6 using namespace std; 6 using namespace std;
7 7
8 -namespace DVPP_UTIL {  
9 8
10 - int32_t deviceId_;  
11 - aclrtContext context_;  
12 - aclrtStream stream_;  
13 - acldvppChannelDesc *dvppChannelDesc_; 9 +int JpegUtil::jpeg_init(int32_t devId){
  10 + deviceId_ = devId;
14 11
15 - int dvpp_jpeg_init(int32_t devId){  
16 - deviceId_ = devId; 12 + aclError ret;
  13 + /* 2.Run the management resource application, including Device, Context, Stream */
  14 + aclrtSetDevice(deviceId_);
  15 + aclrtCreateContext(&context_, deviceId_);
  16 + aclrtCreateStream(&stream_);
17 17
18 - aclError ret;  
19 - /* 2.Run the management resource application, including Device, Context, Stream */  
20 - aclrtSetDevice(deviceId_);  
21 - aclrtCreateContext(&context_, deviceId_);  
22 - aclrtCreateStream(&stream_); 18 + // channel 准备
  19 + dvppChannelDesc_ = acldvppCreateChannelDesc();
  20 + ret = acldvppCreateChannel(dvppChannelDesc_);
  21 +}
23 22
24 - // channel 准备  
25 - dvppChannelDesc_ = acldvppCreateChannelDesc();  
26 - ret = acldvppCreateChannel(dvppChannelDesc_);  
27 - } 23 +void JpegUtil::jpeg_release(){
  24 + aclError ret;
  25 + ret = aclrtSetDevice(deviceId_);
  26 + aclrtSetCurrentContext(context_);
28 27
29 - void dvpp_jpeg_release(){  
30 - aclError ret;  
31 - ret = aclrtSetDevice(deviceId_);  
32 - aclrtSetCurrentContext(context_);  
33 -  
34 - ret = acldvppDestroyChannel(dvppChannelDesc_);  
35 - ret = acldvppDestroyChannelDesc(dvppChannelDesc_);  
36 - dvppChannelDesc_ = nullptr;  
37 -  
38 - if (stream_ != nullptr) {  
39 - ret = aclrtDestroyStream(stream_);  
40 - if (ret != ACL_SUCCESS) {  
41 - printf("destroy stream failed");  
42 - }  
43 - stream_ = nullptr;  
44 - }  
45 - printf("end to destroy stream");  
46 -  
47 - if (context_ != nullptr) {  
48 - ret = aclrtDestroyContext(context_);  
49 - if (ret != ACL_SUCCESS) {  
50 - printf("destroy context failed");  
51 - }  
52 - context_ = nullptr;  
53 - }  
54 - printf("end to destroy context"); 28 + ret = acldvppDestroyChannel(dvppChannelDesc_);
  29 + ret = acldvppDestroyChannelDesc(dvppChannelDesc_);
  30 + dvppChannelDesc_ = nullptr;
55 31
56 - ret = aclrtResetDevice(deviceId_); 32 + if (stream_ != nullptr) {
  33 + ret = aclrtDestroyStream(stream_);
57 if (ret != ACL_SUCCESS) { 34 if (ret != ACL_SUCCESS) {
58 - printf("reset device failed"); 35 + printf("destroy stream failed");
59 } 36 }
60 - printf("end to reset device is %d", deviceId_); 37 + stream_ = nullptr;
61 } 38 }
  39 + printf("end to destroy stream");
62 40
63 - int32_t dvpp_jpege_save(char* pcData , uint32_t dataLen, string out_file_name)  
64 - {  
65 - FILE* fd = nullptr;  
66 - fd = fopen(out_file_name.c_str(), "wb");  
67 - if (fd == nullptr) {  
68 - printf("open output file err\n");  
69 - return 1; 41 + if (context_ != nullptr) {
  42 + ret = aclrtDestroyContext(context_);
  43 + if (ret != ACL_SUCCESS) {
  44 + printf("destroy context failed");
70 } 45 }
71 -  
72 - fwrite(pcData, dataLen, 1, fd);  
73 - fflush(fd);  
74 -  
75 - fclose(fd);  
76 - return 0; 46 + context_ = nullptr;
77 } 47 }
  48 + printf("end to destroy context");
78 49
79 - void dvpp_jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name){  
80 -  
81 - aclError aclRet ;  
82 - aclRet = aclrtSetDevice(deviceId_);  
83 - aclrtSetCurrentContext(context_);  
84 -  
85 - // 7. 创建图片编码配置数据,设置编码质量  
86 - // 编码质量范围[0, 100],其中level 0编码质量与level 100差不多,而在[1, 100]内数值越小输出图片质量越差。  
87 - acldvppJpegeConfig *jpegeConfig_ = acldvppCreateJpegeConfig();  
88 - acldvppSetJpegeConfigLevel(jpegeConfig_, 100);  
89 -  
90 - // 8. 申请输出内存,申请Device内存encodeOutBufferDev_,存放编码后的输出数据  
91 - uint32_t outBufferSize= 0;  
92 - int ret = acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize);  
93 - void *encodeOutBufferDev_ = nullptr;  
94 - ret = acldvppMalloc(&encodeOutBufferDev_, outBufferSize);  
95 -  
96 - // 9. 执行异步编码,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成  
97 - aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_);  
98 - aclRet = aclrtSynchronizeStream(stream_);  
99 -  
100 - // 该模式下,由于处理结果在Device侧,因此需要调用内存复制接口传输结果数据后,再释放Device侧内存  
101 - // 申请Host内存outputHostBuffer  
102 - void* outputHostBuffer = malloc(outBufferSize);  
103 - // 通过aclrtMemcpy接口将Device的处理结果数据传输到Host  
104 - aclRet = aclrtMemcpy(outputHostBuffer, outBufferSize, encodeOutBufferDev_, outBufferSize, ACL_MEMCPY_DEVICE_TO_HOST);  
105 - // 释放掉输入输出的device内存  
106 - (void)acldvppFree(encodeOutBufferDev_);  
107 - encodeOutBufferDev_ = nullptr;  
108 - // 数据使用完成后,释放内存  
109 - dvpp_jpege_save((char*)outputHostBuffer, outBufferSize, out_file_name);  
110 - free(outputHostBuffer);  
111 - outputHostBuffer = nullptr; 50 + ret = aclrtResetDevice(deviceId_);
  51 + if (ret != ACL_SUCCESS) {
  52 + printf("reset device failed");
  53 + }
  54 + printf("end to reset device is %d", deviceId_);
  55 +}
  56 +
  57 +int32_t JpegUtil::jpege_save(char* pcData , uint32_t dataLen, string out_file_name)
  58 +{
  59 + FILE* fd = nullptr;
  60 + fd = fopen(out_file_name.c_str(), "wb");
  61 + if (fd == nullptr) {
  62 + printf("open output file err\n");
  63 + return 1;
112 } 64 }
113 65
  66 + fwrite(pcData, dataLen, 1, fd);
  67 + fflush(fd);
  68 +
  69 + fclose(fd);
  70 + return 0;
  71 +}
  72 +
  73 +void JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name){
  74 +
  75 + aclError aclRet ;
  76 + aclRet = aclrtSetDevice(deviceId_);
  77 + aclrtSetCurrentContext(context_);
  78 +
  79 + // 7. 创建图片编码配置数据,设置编码质量
  80 + // 编码质量范围[0, 100],其中level 0编码质量与level 100差不多,而在[1, 100]内数值越小输出图片质量越差。
  81 + acldvppJpegeConfig *jpegeConfig_ = acldvppCreateJpegeConfig();
  82 + acldvppSetJpegeConfigLevel(jpegeConfig_, 100);
  83 +
  84 + // 8. 申请输出内存,申请Device内存encodeOutBufferDev_,存放编码后的输出数据
  85 + uint32_t outBufferSize= 0;
  86 + int ret = acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize);
  87 + void *encodeOutBufferDev_ = nullptr;
  88 + ret = acldvppMalloc(&encodeOutBufferDev_, outBufferSize);
  89 +
  90 + // 9. 执行异步编码,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
  91 + aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_);
  92 + aclRet = aclrtSynchronizeStream(stream_);
  93 +
  94 + // 该模式下,由于处理结果在Device侧,因此需要调用内存复制接口传输结果数据后,再释放Device侧内存
  95 + // 申请Host内存outputHostBuffer
  96 + void* outputHostBuffer = malloc(outBufferSize);
  97 + // 通过aclrtMemcpy接口将Device的处理结果数据传输到Host
  98 + aclRet = aclrtMemcpy(outputHostBuffer, outBufferSize, encodeOutBufferDev_, outBufferSize, ACL_MEMCPY_DEVICE_TO_HOST);
  99 + // 释放掉输入输出的device内存
  100 + (void)acldvppFree(encodeOutBufferDev_);
  101 + encodeOutBufferDev_ = nullptr;
  102 + // 数据使用完成后,释放内存
  103 + jpege_save((char*)outputHostBuffer, outBufferSize, out_file_name);
  104 + free(outputHostBuffer);
  105 + outputHostBuffer = nullptr;
114 } 106 }
115 \ No newline at end of file 107 \ No newline at end of file
src/util/JpegUtil.h
@@ -9,13 +9,22 @@ @@ -9,13 +9,22 @@
9 9
10 using namespace std; 10 using namespace std;
11 11
12 -namespace DVPP_UTIL { 12 +class JpegUtil {
  13 +public:
  14 + int jpeg_init(int32_t deviceId_);
13 15
14 - int dvpp_jpeg_init(int32_t deviceId_); 16 + void jpeg_release();
15 17
16 - void dvpp_jpeg_release(); 18 + void jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name);
17 19
18 - void dvpp_jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name);  
19 -} 20 +private:
  21 + int32_t jpege_save(char* pcData , uint32_t dataLen, string out_file_name);
  22 +
  23 +private:
  24 + int32_t deviceId_;
  25 + aclrtContext context_;
  26 + aclrtStream stream_;
  27 + acldvppChannelDesc *dvppChannelDesc_;
  28 +};
20 29
21 #endif // __JPEG_UTIL_H__ 30 #endif // __JPEG_UTIL_H__
22 \ No newline at end of file 31 \ No newline at end of file
src/util/vpc_util.cpp
@@ -21,54 +21,9 @@ @@ -21,54 +21,9 @@
21 #define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) 21 #define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args)
22 #define ERROR_LOG(fmt, args...) fprintf(stderr, "[ERROR] " fmt "\n", ##args) 22 #define ERROR_LOG(fmt, args...) fprintf(stderr, "[ERROR] " fmt "\n", ##args)
23 23
24 -typedef enum Result {  
25 - SUCCESS = 0,  
26 - FAILED = 1  
27 -} Result;  
28 -  
29 -typedef struct PicDesc {  
30 - std::string picName;  
31 - int width;  
32 - int height;  
33 -}PicDesc;  
34 -  
35 -typedef struct CropPicDesc {  
36 - std::string picName;  
37 - int left;  
38 - int top;  
39 - int width;  
40 - int height;  
41 -}CropPicDesc;  
42 -  
43 -int32_t deviceId_;  
44 -aclrtContext context_;  
45 -aclrtStream stream_;  
46 -  
47 -aclvdecChannelDesc *vdecChannelDesc_;  
48 -acldvppStreamDesc *streamInputDesc_;  
49 -acldvppPicDesc *picOutputDesc_;  
50 -acldvppChannelDesc *dvppChannelDesc_;  
51 -uint32_t inBufferSize;  
52 -uint32_t inputWidth;  
53 -uint32_t inputHeight;  
54 -aclrtRunMode runMode;  
55 -  
56 -PicDesc inPicDesc;  
57 -CropPicDesc outPicDesc;  
58 -  
59 -  
60 -  
61 -uint32_t AlignmentHelper(uint32_t origSize, uint32_t alignment)  
62 -{  
63 - if (alignment == 0) {  
64 - return 0;  
65 - }  
66 - uint32_t alignmentH = alignment - 1;  
67 - return (origSize + alignmentH) / alignment * alignment;  
68 -}  
69 24
70 25
71 -void dvpp_crop_release() 26 +void VPCUtil::vpc_crop_release()
72 { 27 {
73 aclError ret; 28 aclError ret;
74 // ret = aclrtSetDevice(deviceId_); 29 // ret = aclrtSetDevice(deviceId_);
@@ -103,33 +58,12 @@ void dvpp_crop_release() @@ -103,33 +58,12 @@ void dvpp_crop_release()
103 } 58 }
104 59
105 60
106 -int dvpp_crop(acldvppPicDesc *input_pic_desc) 61 +int VPCUtil::vpc_crop(acldvppPicDesc *input_pic_desc, video_object_info obj)
107 { 62 {
108 - /* 1.ACL initialization */  
109 - // aclInit(nullptr);  
110 -  
111 - /* 2.Run the management resource application, including Device, Context, Stream */  
112 - aclrtSetDevice(deviceId_);  
113 - aclrtCreateContext(&context_, deviceId_);  
114 - aclrtCreateStream(&stream_);  
115 - aclrtGetRunMode(&runMode);  
116 -  
117 - /* 3.Initialization parameters: width and height of the original image, crop width and height.  
118 - * Initialize folder: Output folder */  
119 -  
120 - outPicDesc={"output.jpg", 100, 100, 300, 300};  
121 -  
122 - const int orimodelInputWidth = outPicDesc.width; // cur model shape is 224 * 224  
123 - const int orimodelInputHeight = outPicDesc.height;  
124 - const int modelInputLeft = outPicDesc.left; // cur model shape is 224 * 224  
125 - const int modelInputTop = outPicDesc.top;  
126 -  
127 - /* 4. Channel description information when creating image data processing channels,  
128 - * dvppChannelDesc_ is acldvppChannelDesc type */  
129 - dvppChannelDesc_ = acldvppCreateChannelDesc();  
130 -  
131 - /* 5. Create the image data processing channel. */  
132 - acldvppCreateChannel(dvppChannelDesc_); 63 + const int orimodelInputWidth = obj.right - obj.left; // cur model shape is 224 * 224
  64 + const int orimodelInputHeight = obj.bottom - obj.top;
  65 + const int modelInputLeft = obj.left; // cur model shape is 224 * 224
  66 + const int modelInputTop = obj.top;
133 67
134 // GetPicDevBuffer4JpegD 68 // GetPicDevBuffer4JpegD
135 int modelInputWidth = (orimodelInputWidth + 15) / 16 * 16; 69 int modelInputWidth = (orimodelInputWidth + 15) / 16 * 16;
@@ -161,8 +95,6 @@ int dvpp_crop(acldvppPicDesc *input_pic_desc) @@ -161,8 +95,6 @@ int dvpp_crop(acldvppPicDesc *input_pic_desc)
161 95
162 ret = aclrtSynchronizeStream(stream_); 96 ret = aclrtSynchronizeStream(stream_);
163 97
164 - DVPP_UTIL::dvpp_jpeg_encode(vpcOutputDesc_, "output.jpg");  
165 -  
166 /* DestroycropResource */ 98 /* DestroycropResource */
167 (void)acldvppDestroyRoiConfig(cropArea_); 99 (void)acldvppDestroyRoiConfig(cropArea_);
168 cropArea_ = nullptr; 100 cropArea_ = nullptr;
@@ -174,10 +106,10 @@ int dvpp_crop(acldvppPicDesc *input_pic_desc) @@ -174,10 +106,10 @@ int dvpp_crop(acldvppPicDesc *input_pic_desc)
174 vpcOutBufferDev_ = nullptr; 106 vpcOutBufferDev_ = nullptr;
175 } 107 }
176 108
177 - return SUCCESS; 109 + return 0;
178 } 110 }
179 111
180 -int dvpp_crop_init(int32_t devId){ 112 +int VPCUtil::vpc_crop_init(int32_t devId){
181 deviceId_ = devId; 113 deviceId_ = devId;
182 114
183 aclError ret; 115 aclError ret;
@@ -185,7 +117,6 @@ int dvpp_crop_init(int32_t devId){ @@ -185,7 +117,6 @@ int dvpp_crop_init(int32_t devId){
185 aclrtSetDevice(deviceId_); 117 aclrtSetDevice(deviceId_);
186 aclrtCreateContext(&context_, deviceId_); 118 aclrtCreateContext(&context_, deviceId_);
187 aclrtCreateStream(&stream_); 119 aclrtCreateStream(&stream_);
188 - aclrtGetRunMode(&runMode);  
189 120
190 // channel 准备 121 // channel 准备
191 dvppChannelDesc_ = acldvppCreateChannelDesc(); 122 dvppChannelDesc_ = acldvppCreateChannelDesc();
@@ -193,7 +124,7 @@ int dvpp_crop_init(int32_t devId){ @@ -193,7 +124,7 @@ int dvpp_crop_init(int32_t devId){
193 124
194 } 125 }
195 126
196 -void check_coordinate(uint32_t& cropLeftOffset, uint32_t& cropRightOffset, uint32_t& cropTopOffset, uint32_t& cropBottomOffset, uint32_t width, uint32_t height){ 127 +static void check_coordinate(uint32_t& cropLeftOffset, uint32_t& cropRightOffset, uint32_t& cropTopOffset, uint32_t& cropBottomOffset, uint32_t width, uint32_t height){
197 if (cropLeftOffset < 0){ 128 if (cropLeftOffset < 0){
198 cropLeftOffset = 0; 129 cropLeftOffset = 0;
199 } 130 }
@@ -209,7 +140,7 @@ void check_coordinate(uint32_t&amp; cropLeftOffset, uint32_t&amp; cropRightOffset, uint3 @@ -209,7 +140,7 @@ void check_coordinate(uint32_t&amp; cropLeftOffset, uint32_t&amp; cropRightOffset, uint3
209 140
210 } 141 }
211 142
212 -vector<vpc_img_info> dvpp_crop_batch(DeviceMemory *devMem, vector<video_object_info> objs){ 143 +vector<vpc_img_info> VPCUtil::vpc_crop_batch(DeviceMemory *devMem, vector<video_object_info> objs){
213 144
214 vector<vpc_img_info> vec_img_info; 145 vector<vpc_img_info> vec_img_info;
215 146
@@ -324,7 +255,7 @@ vector&lt;vpc_img_info&gt; dvpp_crop_batch(DeviceMemory *devMem, vector&lt;video_object_i @@ -324,7 +255,7 @@ vector&lt;vpc_img_info&gt; dvpp_crop_batch(DeviceMemory *devMem, vector&lt;video_object_i
324 // vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i); 255 // vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
325 // string file_name = "output"; 256 // string file_name = "output";
326 // file_name = file_name + to_string(i) + ".jpg"; 257 // file_name = file_name + to_string(i) + ".jpg";
327 - // dvpp_jpeg_encode(vpcOutputDesc, file_name); 258 + // vpc_jpeg_encode(vpcOutputDesc, file_name);
328 } 259 }
329 260
330 aclrtSetCurrentContext(context_); 261 aclrtSetCurrentContext(context_);
@@ -365,7 +296,7 @@ vector&lt;vpc_img_info&gt; dvpp_crop_batch(DeviceMemory *devMem, vector&lt;video_object_i @@ -365,7 +296,7 @@ vector&lt;vpc_img_info&gt; dvpp_crop_batch(DeviceMemory *devMem, vector&lt;video_object_i
365 return vec_img_info; 296 return vec_img_info;
366 } 297 }
367 298
368 -vpc_img_info dvpp_devMem2vpcImg(DeviceMemory *devMem){ 299 +vpc_img_info VPCUtil::vpc_devMem2vpcImg(DeviceMemory *devMem){
369 vpc_img_info img_info ; 300 vpc_img_info img_info ;
370 301
371 int nBufferSize = devMem->getSize(); 302 int nBufferSize = devMem->getSize();
@@ -398,7 +329,7 @@ vpc_img_info dvpp_devMem2vpcImg(DeviceMemory *devMem){ @@ -398,7 +329,7 @@ vpc_img_info dvpp_devMem2vpcImg(DeviceMemory *devMem){
398 return img_info; 329 return img_info;
399 } 330 }
400 331
401 -void dvpp_img_release(vpc_img_info img_info){ 332 +void VPCUtil::vpc_img_release(vpc_img_info img_info){
402 if(img_info.pic_desc != nullptr){ 333 if(img_info.pic_desc != nullptr){
403 void *outputDataDev = acldvppGetPicDescData(img_info.pic_desc); 334 void *outputDataDev = acldvppGetPicDescData(img_info.pic_desc);
404 acldvppFree(outputDataDev); 335 acldvppFree(outputDataDev);
@@ -406,9 +337,9 @@ void dvpp_img_release(vpc_img_info img_info){ @@ -406,9 +337,9 @@ void dvpp_img_release(vpc_img_info img_info){
406 } 337 }
407 } 338 }
408 339
409 -void dvpp_imgList_release(vector<vpc_img_info>& imgList){ 340 +void VPCUtil::vpc_imgList_release(vector<vpc_img_info>& imgList){
410 for(int i=0; i < imgList.size(); i++){ 341 for(int i=0; i < imgList.size(); i++){
411 - dvpp_img_release(imgList[i]); 342 + vpc_img_release(imgList[i]);
412 } 343 }
413 imgList.clear(); 344 imgList.clear();
414 } 345 }
415 \ No newline at end of file 346 \ No newline at end of file
src/util/vpc_util.h
@@ -22,17 +22,31 @@ struct vpc_img_info{ @@ -22,17 +22,31 @@ struct vpc_img_info{
22 22
23 class DeviceMemory; 23 class DeviceMemory;
24 24
25 -int dvpp_crop(acldvppPicDesc *input_pic_desc); 25 +class VPCUtil {
26 26
27 -int dvpp_crop_init(int32_t devId); 27 +public:
  28 + int vpc_crop(acldvppPicDesc *input_pic_desc, video_object_info obj);
28 29
29 -vector<vpc_img_info> dvpp_crop_batch(DeviceMemory *devMem, vector<video_object_info> objs); 30 + int vpc_crop_init(int32_t devId);
30 31
31 -void dvpp_img_release(vpc_img_info );  
32 -void dvpp_imgList_release(vector<vpc_img_info>& ); 32 + vector<vpc_img_info> vpc_crop_batch(DeviceMemory *devMem, vector<video_object_info> objs);
  33 +
  34 + void vpc_img_release(vpc_img_info );
  35 +
  36 + void vpc_imgList_release(vector<vpc_img_info>& );
  37 +
  38 + void vpc_crop_release();
  39 +
  40 + vpc_img_info vpc_devMem2vpcImg(DeviceMemory *devMem);
  41 +
  42 +private:
  43 + int32_t deviceId_;
  44 + aclrtContext context_;
  45 + aclrtStream stream_;
  46 +
  47 + acldvppChannelDesc *dvppChannelDesc_;
  48 +};
33 49
34 -void dvpp_crop_release();  
35 50
36 -vpc_img_info dvpp_devMem2vpcImg(DeviceMemory *devMem);  
37 51
38 #endif //___VPC_UTIL_H__ 52 #endif //___VPC_UTIL_H__
39 \ No newline at end of file 53 \ No newline at end of file