Commit 20396d5c3de2e76dc064aa90584bbace579f5503

Authored by Hu Chunming
1 parent eac85cd5

添加车头车尾算法

.vscode/settings.json
... ... @@ -63,6 +63,60 @@
63 63 "istream": "cpp",
64 64 "functional": "cpp",
65 65 "tuple": "cpp",
66   - "utility": "cpp"
  66 + "utility": "cpp",
  67 + "atomic": "cpp",
  68 + "bit": "cpp",
  69 + "bitset": "cpp",
  70 + "cctype": "cpp",
  71 + "chrono": "cpp",
  72 + "clocale": "cpp",
  73 + "cmath": "cpp",
  74 + "complex": "cpp",
  75 + "condition_variable": "cpp",
  76 + "cstdarg": "cpp",
  77 + "cstddef": "cpp",
  78 + "cstdint": "cpp",
  79 + "cstdio": "cpp",
  80 + "cstdlib": "cpp",
  81 + "cstring": "cpp",
  82 + "ctime": "cpp",
  83 + "cwchar": "cpp",
  84 + "cwctype": "cpp",
  85 + "deque": "cpp",
  86 + "list": "cpp",
  87 + "map": "cpp",
  88 + "set": "cpp",
  89 + "unordered_map": "cpp",
  90 + "vector": "cpp",
  91 + "exception": "cpp",
  92 + "algorithm": "cpp",
  93 + "iterator": "cpp",
  94 + "memory_resource": "cpp",
  95 + "numeric": "cpp",
  96 + "optional": "cpp",
  97 + "random": "cpp",
  98 + "ratio": "cpp",
  99 + "regex": "cpp",
  100 + "string": "cpp",
  101 + "string_view": "cpp",
  102 + "system_error": "cpp",
  103 + "type_traits": "cpp",
  104 + "fstream": "cpp",
  105 + "future": "cpp",
  106 + "initializer_list": "cpp",
  107 + "iomanip": "cpp",
  108 + "iosfwd": "cpp",
  109 + "iostream": "cpp",
  110 + "limits": "cpp",
  111 + "mutex": "cpp",
  112 + "new": "cpp",
  113 + "ostream": "cpp",
  114 + "shared_mutex": "cpp",
  115 + "sstream": "cpp",
  116 + "stdexcept": "cpp",
  117 + "streambuf": "cpp",
  118 + "thread": "cpp",
  119 + "typeinfo": "cpp",
  120 + "variant": "cpp"
67 121 }
68 122 }
69 123 \ No newline at end of file
... ...
bin/libvehicle_analysis.so
No preview for this file type
bin/models/car_head_tail/head_tail_256_241220_310P.om 0 → 100644
No preview for this file type
build/src/Makefile
... ... @@ -18,6 +18,9 @@ ALGORITHM_PATH = $(PROJ_ALL_PATH)/algorithm
18 18  
19 19 INCLUDES = -I$(PROJ_ALL_PATH)/src/common \
20 20 -I$(PROJ_ALL_PATH)/src/common/dvpp \
  21 + -I$(PROJ_ALL_PATH)/src/common/cnn_cls \
  22 + -I$(PROJ_ALL_PATH)/src/common/dvppx \
  23 + -I$(PROJ_ALL_PATH)/src/common/model_process \
21 24 -I$(CUR_PROJ_PATH)/../va \
22 25 -I$(OPENCV_PATH)/include \
23 26 -I$(OPENCV_PATH)/include/opencv2 \
... ... @@ -52,6 +55,9 @@ SRCS := $(wildcard $(CUR_PROJ_PATH)/*.cpp) \
52 55 $(wildcard $(CUR_PROJ_PATH)/ai_engine_module/*.cpp) \
53 56 $(wildcard $(CUR_PROJ_PATH)/common/*.cpp) \
54 57 $(wildcard $(CUR_PROJ_PATH)/common/dvpp/*.cpp) \
  58 + $(wildcard $(CUR_PROJ_PATH)/common/cnn_cls/*.cpp) \
  59 + $(wildcard $(CUR_PROJ_PATH)/common/dvppx/*.cpp) \
  60 + $(wildcard $(CUR_PROJ_PATH)/common/model_process/*.cpp) \
55 61  
56 62 DIRS := $(notdir $(SRCS))
57 63 OBJS := $(patsubst %cpp, %o, $(DIRS))
... ... @@ -73,6 +79,18 @@ $(TARGET):$(OBJS)
73 79 %.o:$(CUR_PROJ_PATH)/common/dvpp/%.cpp
74 80 $(XX) $(CXXFLAGS) -c $<
75 81  
  82 +%.o:$(CUR_PROJ_PATH)/common/cnn_cls/%.cpp
  83 + $(XX) $(CXXFLAGS) -c $<
  84 +
  85 +%.o:$(CUR_PROJ_PATH)/common/dvppx/%.cpp
  86 + $(XX) $(CXXFLAGS) -c $<
  87 +
  88 +%.o:$(CUR_PROJ_PATH)/common/model_process/%.cpp
  89 + $(XX) $(CXXFLAGS) -c $<
  90 +
76 91 clean:
77 92 @rm -f $(TARGET)
78 93 @rm -f $(OBJS)
  94 +
  95 +cleano:
  96 + @rm -f $(OBJS)
79 97 \ No newline at end of file
... ...
src/PicAnalysis.cpp 0 → 100644
  1 +#include "PicAnalysis.h"
  2 +#include "./utils/logger.hpp"
  3 +
  4 +
  5 +PicAnalysis::PicAnalysis(/* args */)
  6 +{
  7 + aclInit(nullptr);
  8 +}
  9 +
  10 +PicAnalysis::~PicAnalysis()
  11 +{
  12 + aclFinalize();
  13 +}
  14 +
  15 +int PicAnalysis::init(int dev_id) {
  16 +
  17 + int ret = m_vehicle_analysis.init(dev_id, 16);
  18 + if(0 != ret){
  19 + return -1;
  20 + }
  21 +
  22 + head_tail_param ht_param;
  23 + ht_param.devId = dev_id;
  24 + ht_param.max_batch = 16;
  25 + ret = m_head_tail_algorithm.init(ht_param);
  26 + if(0 != ret){
  27 + return -1;
  28 + }
  29 +
  30 + ACL_CALL(aclrtCreateContext(&m_ctx, 0), ACL_ERROR_NONE, SY_FAILED);
  31 + ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_ERROR_NONE, SY_FAILED);
  32 +
  33 + ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
  34 + m_dvpp = new DvppProcess();
  35 + m_dvpp->InitResource(stream);
  36 +
  37 + return 0;
  38 +}
  39 +
  40 +int PicAnalysis::analysis_sync(vector<string> vec_file_path){
  41 +
  42 + const int batch_size = vec_file_path.size();
  43 +
  44 + vector<sy_img> vec_img;
  45 +
  46 + int ret = SY_FAILED;
  47 +
  48 + // ImageData 内部是智能指针,分析未处理完成前不得释放
  49 + ImageData dvpp_data[batch_size];
  50 + for (size_t i = 0; i < vec_file_path.size(); i++)
  51 + {
  52 + string file_path = vec_file_path[i];
  53 + ImageData src;
  54 + ret = Utils::ReadImageFile(src, file_path); //将二进制图像读入内存,并读取宽高信息
  55 + if(ret != SY_SUCCESS){
  56 + LOG_ERROR("ReadImageFile failed!");
  57 + return -1;
  58 + }
  59 +
  60 + ret = m_dvpp->CvtJpegToYuv420sp(dvpp_data[i], src); //解码
  61 + if(ret != SY_SUCCESS){
  62 + LOG_ERROR("CvtJpegToYuv420sp failed!");
  63 + return -1;
  64 + }
  65 +
  66 + sy_img img;
  67 + img.w_ = dvpp_data[i].width;
  68 + img.h_ = dvpp_data[i].height;
  69 + img.data_ = dvpp_data[i].data.get();
  70 +
  71 + vec_img.push_back(img);
  72 + }
  73 +
  74 + // m_vehicle_analysis.detect(vec_img);
  75 +
  76 + vector<HeadTailResult> head_tail_result;
  77 + ret = m_head_tail_algorithm.detect(vec_img, head_tail_result);
  78 + if (0 != ret) {
  79 + LOG_ERROR("m_head_tail_algorithm failed!");
  80 + head_tail_result.clear();
  81 + }
  82 +
  83 +
  84 + return 0;
  85 +}
  86 +
  87 +int PicAnalysis::release() {
  88 + ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_ERROR_NONE, SY_FAILED);
  89 +
  90 + delete m_dvpp;
  91 + m_dvpp = nullptr;
  92 +
  93 + if (stream != nullptr) {
  94 + int ret = aclrtDestroyStream(stream);
  95 + if (ret != ACL_SUCCESS) {
  96 + LOG_ERROR("destroy stream failed");
  97 + }
  98 + stream = nullptr;
  99 + }
  100 +
  101 + aclrtDestroyContext(m_ctx);
  102 +
  103 + return 0;
  104 +}
0 105 \ No newline at end of file
... ...
src/PicAnalysis.h 0 → 100644
  1 +#include "./ai_engine_module/include.h"
  2 +#include "./ai_engine_module/VehicleAnalysis.h"
  3 +#include "./ai_engine_module/VehicleHeadTail.h"
  4 +
  5 +using namespace std;
  6 +
  7 +
  8 +class PicAnalysis
  9 +{
  10 +public:
  11 + PicAnalysis(/* args */);
  12 + ~PicAnalysis();
  13 +
  14 + int init(int dev_id);
  15 +
  16 + // todo 接口定义是二进制流的,这个地方有待修改
  17 + int analysis_sync(vector<string> file_path);
  18 +
  19 + int release();
  20 +
  21 +private:
  22 + aclrtContext m_ctx{nullptr};
  23 + aclrtStream stream{nullptr};
  24 + DvppProcess* m_dvpp{nullptr};
  25 +
  26 + VehicleAnalysis m_vehicle_analysis;
  27 + VehicleHeadTail m_head_tail_algorithm;
  28 +};
  29 +
  30 +
... ...
src/ai_engine_module/VehicleAnalysis.cpp
... ... @@ -2,7 +2,6 @@
2 2  
3 3  
4 4 VehicleAnalysis::VehicleAnalysis() {
5   - va_acl_init();
6 5 cout << va_get_version() << endl;
7 6 }
8 7  
... ... @@ -69,22 +68,9 @@ int VehicleAnalysis::init(int devId, int max_batch_size) {
69 68 return 0;
70 69 }
71 70  
72   -int VehicleAnalysis::detect(vector<ImageData> vec_img) {
73   -
74   - vector<sy_img> imgs;
75   -
76   - for (size_t i = 0; i < vec_img.size(); i++)
77   - {
78   - ImageData dvpp_data = vec_img[i];
79   - sy_img img;
80   - img.w_ = dvpp_data.width;
81   - img.h_ = dvpp_data.height;
82   - img.data_ = dvpp_data.data.get();
83   -
84   - imgs.push_back(img);
85   - }
  71 +int VehicleAnalysis::detect(vector<sy_img> vec_img) {
86 72  
87   - int batch_size = imgs.size();
  73 + int batch_size = vec_img.size();
88 74  
89 75 va_result *result=new va_result[batch_size];
90 76 for(int b=0;b<batch_size;b++)
... ... @@ -98,7 +84,7 @@ int VehicleAnalysis::detect(vector&lt;ImageData&gt; vec_img) {
98 84 }
99 85  
100 86 ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, SY_FAILED);
101   - int ret = va_batch(m_handle, imgs.data(), batch_size, result);
  87 + int ret = va_batch(m_handle, vec_img.data(), batch_size, result);
102 88  
103 89 for (int b = 0; b < batch_size; b++)
104 90 {
... ... @@ -109,16 +95,6 @@ int VehicleAnalysis::detect(vector&lt;ImageData&gt; vec_img) {
109 95 std::cout <<"car_count:" <<result[b].count<< std::endl;
110 96 for(int c=0;c<result[b].count;c++)
111 97 {
112   - //temp,中间结果,输出车牌被置黑的车小图
113   - //if(0)
114   - //{
115   - // cv::Mat tmp(result[b].info[c].car_img_data_cpu[0].h_, result[b].info[c].car_img_data_cpu[0].w_, CV_8UC3, result[b].info[c].car_img_data_cpu[0].data_);
116   - // char str_i[100];
117   - // sprintf(str_i, "%d_", c);
118   - // cv::imwrite((save_folder_roi+str_i+i).c_str(),tmp);
119   - //}
120   -
121   -
122 98 std::string str_vehicle_type;
123 99 int vehicle_type=result[b].info[c].type;
124 100 //std::cout << "vehicle_type="<<vehicle_type<< std::endl;
... ... @@ -640,6 +616,7 @@ int VehicleAnalysis::detect(vector&lt;ImageData&gt; vec_img) {
640 616  
641 617 void VehicleAnalysis::release(){
642 618 if(ctx){
  619 + aclrtSetCurrentContext(ctx);
643 620 aclrtDestroyContext(ctx);
644 621 }
645 622  
... ...
src/ai_engine_module/VehicleAnalysis.h
... ... @@ -16,7 +16,7 @@ public:
16 16  
17 17 int init(int devId, int max_batch_size);
18 18  
19   - int detect(vector<ImageData> vec_img);
  19 + int detect(vector<sy_img> vec_img);
20 20  
21 21 private:
22 22 void release();
... ...
src/ai_engine_module/VehicleHeadTail.cpp 0 → 100644
  1 +#include "VehicleHeadTail.h"
  2 +#include "../utils/logger.hpp"
  3 +
  4 +VehicleHeadTail::VehicleHeadTail(/* args */)
  5 +{
  6 +}
  7 +
  8 +VehicleHeadTail::~VehicleHeadTail()
  9 +{
  10 + release();
  11 +}
  12 +
  13 +int VehicleHeadTail::init(head_tail_param param){
  14 +
  15 + ACL_CALL(aclrtCreateContext(&m_ctx, param.devId), ACL_SUCCESS, SY_FAILED);
  16 +
  17 +
  18 + LOG_INFO("head_tail version: head_tail_256.2024.12.18");
  19 +
  20 + m_cnn_cls = new CnnCls();
  21 + int ret = m_cnn_cls->Init("./models/car_head_tail/head_tail_256_241220_310P.om");
  22 + if (ret != SY_SUCCESS) {
  23 + delete m_cnn_cls;
  24 + m_cnn_cls = nullptr;
  25 + LOG_ERROR("sy_hcp model init failed!");
  26 + return SY_FAILED;
  27 + }
  28 +
  29 + max_batch = param.max_batch;
  30 +
  31 + ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
  32 + m_dvpp = new DvppProcessx();
  33 + ret = m_dvpp->InitResource(stream);
  34 + if (ret != SY_SUCCESS) {
  35 + delete m_dvpp;
  36 + m_dvpp = nullptr;
  37 + LOG_ERROR("dvpp init failed!");
  38 + return SY_FAILED;
  39 + }
  40 +
  41 + return SY_SUCCESS;
  42 +}
  43 +
  44 +int VehicleHeadTail::detect(vector<sy_img> img_data_array, vector<HeadTailResult>& result){
  45 +
  46 + if (m_cnn_cls == NULL) {
  47 + LOG_ERROR("HumanCarParse get null handle!");
  48 + return SY_FAILED;
  49 + }
  50 +
  51 + ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);
  52 +
  53 + int inputW = m_cnn_cls->GetInputWidth();
  54 + int inputH = m_cnn_cls->GetInputHeight();
  55 +
  56 + for (int i = 0; i < img_data_array.size(); i++) {
  57 + if (img_data_array[i].data_ == NULL || img_data_array[i].w_ == 0 || img_data_array[i].h_ == 0) {
  58 + LOG_ERROR("HumanCarParse Get null input ptr!");
  59 + return SY_FAILED;
  60 + }
  61 + ImageData resizeImg, src;
  62 + ACL_CALL(Utils::CopysyImageDataToDvppV2(src, img_data_array[i]), SY_SUCCESS, SY_FAILED);
  63 + ACL_CALL(m_dvpp->CropAndPaste(resizeImg, src, inputW, inputH), SY_SUCCESS, SY_FAILED);
  64 + int ret = m_cnn_cls->Inference(resizeImg);
  65 + if (ret != SY_SUCCESS) {
  66 + LOG_ERROR( "HumanCarParse process error!");
  67 + return SY_MODEL_FORWARD_ERROR;
  68 + }
  69 +
  70 + vector<float> hcpRes;
  71 + ret = m_cnn_cls->PostProcess(hcpRes);
  72 + if (ret != SY_SUCCESS) {
  73 + LOG_ERROR("HumanCarParse postprocess error!");
  74 + return SY_MODEL_GETRESULT_ERROR;
  75 + }
  76 + if (hcpRes.size() != 2) {
  77 + LOG_ERROR("HumanCarParse postprocess error!");
  78 + return SY_MODEL_GETRESULT_ERROR;
  79 + }
  80 +
  81 + HeadTailResult one_result;
  82 + one_result.cls = hcpRes[0];
  83 + one_result.confidence = hcpRes[1];
  84 +
  85 + result.push_back(one_result);
  86 + }
  87 +
  88 + return SY_SUCCESS;
  89 +}
  90 +
  91 +int VehicleHeadTail::release() {
  92 +
  93 + ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_ERROR_NONE, SY_FAILED);
  94 +
  95 + if (m_cnn_cls) {
  96 + delete m_cnn_cls;
  97 + m_cnn_cls = nullptr;
  98 + }
  99 +
  100 + if (m_dvpp) {
  101 + delete m_dvpp;
  102 + m_dvpp = nullptr;
  103 + }
  104 +
  105 + if (stream != nullptr) {
  106 + int ret = aclrtDestroyStream(stream);
  107 + if (ret != ACL_SUCCESS) {
  108 + LOG_ERROR("destroy stream failed");
  109 + }
  110 + stream = nullptr;
  111 + }
  112 +
  113 + aclrtDestroyContext(m_ctx);
  114 +}
0 115 \ No newline at end of file
... ...
src/ai_engine_module/VehicleHeadTail.h 0 → 100644
  1 +#include "sy_errorinfo.h"
  2 +#include "cnn_cls.h"
  3 +#include "dvpp_processx.h"
  4 +#include <time.h>
  5 +#include <sys/time.h>
  6 +#include "stream_data.h"
  7 +#include <string.h>
  8 +#include <vector>
  9 +
  10 +using namespace atlas_utils;
  11 +using namespace std;
  12 +
  13 +typedef struct head_tail_param
  14 +{
  15 + int devId; //ָ指定显卡id
  16 + int max_batch;
  17 + head_tail_param() :devId(0), max_batch(8){};
  18 +} head_tail_param;
  19 +
  20 +struct HeadTailResult {
  21 + int cls;
  22 + float confidence;
  23 +};
  24 +
  25 +class VehicleHeadTail
  26 +{
  27 +
  28 +public:
  29 + VehicleHeadTail(/* args */);
  30 + ~VehicleHeadTail();
  31 +
  32 + int init(head_tail_param param);
  33 +
  34 + int detect(vector<sy_img>, vector<HeadTailResult>&);
  35 +
  36 +private:
  37 + int release();
  38 +
  39 +private:
  40 + aclrtContext m_ctx;
  41 + aclrtStream stream;
  42 + CnnCls* m_cnn_cls;
  43 + DvppProcessx* m_dvpp;
  44 + int max_batch;
  45 +};
  46 +
... ...
src/common/cnn_cls/cnn_cls.cpp 0 → 100755
  1 +#include "cnn_cls.h"
  2 +#include <iostream>
  3 +#include "acl/acl.h"
  4 +#include "model_process.h"
  5 +#include "sy_errorinfo.h"
  6 +#include <time.h>
  7 +#include <sys/time.h>
  8 +#include <algorithm>
  9 +
  10 +using namespace std;
  11 +
  12 +namespace atlas_utils {
  13 +
  14 +int CnnCls::Init(const char* modelPath) {
  15 + ACL_CALL(aclrtGetRunMode(&runMode_), SY_SUCCESS, SY_FAILED);//获取当前昇腾AI软件栈的运行模式,根据不同的运行模式,后续的接口调用方式不同
  16 + ACL_CALL(model_.LoadModelFromFileWithMem(modelPath), SY_SUCCESS, SY_FAILED);//从文件加载离线模型数据,由用户自行管理模型运行的内存
  17 + ACL_CALL(model_.CreateDesc(), SY_SUCCESS, SY_FAILED);//获取模型的描述信息
  18 + ACL_CALL(model_.CreateOutput(outDims_), SY_SUCCESS, SY_FAILED);
  19 + ACL_CALL(model_.GetInputDims(inDims_), SY_SUCCESS, SY_FAILED);
  20 + modelHeight_ = inDims_[0][1];
  21 + modelWidth_ = inDims_[0][2];
  22 +
  23 + return SY_SUCCESS;
  24 +}
  25 +
  26 +// double msecond1() {
  27 +// struct timeval tv;
  28 +// gettimeofday(&tv, 0);
  29 +// return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
  30 +// }
  31 +
  32 +int CnnCls::Inference(ImageData& input) {
  33 + model_.CreateInput(input.data.get(), input.size);
  34 + //double t1, t2;
  35 + //t1 = msecond1();
  36 + ACL_CALL(model_.Execute(), SY_SUCCESS, SY_FAILED);
  37 + //t2 = msecond1();
  38 + //printf("debug forward time: %.2f\n", t2 - t1);
  39 + model_.DestroyInput(); //需调用CreateInput的销毁类接口DestroyInput!!!
  40 + return SY_SUCCESS;
  41 +}
  42 +
  43 +int CnnCls::GetInputWidth() {
  44 + return modelWidth_;
  45 +}
  46 +
  47 +int CnnCls::GetInputHeight() {
  48 + return modelHeight_;
  49 +}
  50 +
  51 +int CnnCls::PostProcess(vector<float>& results) {
  52 + aclmdlDataset* modelOutput = model_.GetModelOutputData();
  53 + int outDatasetNum = aclmdlGetDatasetNumBuffers(modelOutput);
  54 + for (int i = 0; i < outDatasetNum; i++) {
  55 + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(modelOutput, i);
  56 + if (dataBuffer == nullptr) {
  57 + return SY_FAILED;
  58 + }
  59 + uint32_t dataBufferSize = aclGetDataBufferSize(dataBuffer);
  60 + void* data = aclGetDataBufferAddr(dataBuffer);
  61 + if (data == nullptr) {
  62 + return SY_FAILED;
  63 + }
  64 +
  65 + int length = dataBufferSize/sizeof(float);
  66 + float outInfo[length];
  67 +
  68 + if (runMode_ == ACL_HOST) {
  69 + ACL_CALL(aclrtMemcpy(outInfo, sizeof(outInfo), data, sizeof(outInfo), ACL_MEMCPY_DEVICE_TO_HOST),
  70 + ACL_SUCCESS, SY_FAILED);
  71 + } else {
  72 + ACL_CALL(aclrtMemcpy(outInfo, sizeof(outInfo), data, sizeof(outInfo), ACL_MEMCPY_DEVICE_TO_DEVICE),ACL_SUCCESS, SY_FAILED);
  73 + //return SY_FAILED;
  74 + }
  75 +
  76 + int argmax = std::distance(outInfo, std::max_element(outInfo, outInfo + length));
  77 + if(outInfo[argmax] < config.confThr){
  78 + // printf("outInfo[argmax]:%f\n",outInfo[argmax]);
  79 + // printf("config.confThr:%f\n",config.confThr);
  80 + results.emplace_back(-1);
  81 + results.emplace_back(0);
  82 + // INFO_LOG("vColor is low confidence!");
  83 + }
  84 + else{
  85 + results.emplace_back(argmax);
  86 + results.emplace_back(outInfo[argmax]);
  87 + }
  88 + /*
  89 + for(uint32_t b = 0; b < length; b++) {
  90 +
  91 + results.emplace_back(outInfo[b]);
  92 + }*/
  93 + }
  94 +
  95 +
  96 + return SY_SUCCESS;
  97 +}
  98 +
  99 +int CnnCls::PostProcess_batch(vector<vector<float>>& results) {
  100 + aclmdlDataset* modelOutput = model_.GetModelOutputData();
  101 + int outDatasetNum = aclmdlGetDatasetNumBuffers(modelOutput);
  102 + const int batchsize = outDims_[0][0];
  103 +
  104 + // 结果拷贝
  105 + vector<vector<float>> all_res;
  106 + for (int i = 0; i < outDatasetNum; i++) {
  107 + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(modelOutput, i);
  108 + if (dataBuffer == nullptr) {
  109 + return SY_FAILED;
  110 + }
  111 + uint32_t dataBufferSize = aclGetDataBufferSize(dataBuffer);
  112 + void* data = aclGetDataBufferAddr(dataBuffer);
  113 + if (data == nullptr) {
  114 + return SY_FAILED;
  115 + }
  116 +
  117 + int length = dataBufferSize/sizeof(float);
  118 + float outInfo[length];
  119 +
  120 + if (runMode_ == ACL_HOST) {
  121 + ACL_CALL(aclrtMemcpy(outInfo, sizeof(outInfo), data, sizeof(outInfo), ACL_MEMCPY_DEVICE_TO_HOST),ACL_SUCCESS, SY_FAILED);
  122 + } else {
  123 + ACL_CALL(aclrtMemcpy(outInfo, sizeof(outInfo), data, sizeof(outInfo), ACL_MEMCPY_DEVICE_TO_DEVICE),ACL_SUCCESS, SY_FAILED);
  124 + }
  125 +
  126 + vector<float> res;
  127 + res.assign(outInfo, outInfo + length);
  128 + all_res.emplace_back(res);
  129 + }
  130 +
  131 + // 按[b][cls]的形式返回
  132 + for (int b = 0; b < batchsize; b ++) {
  133 + vector<float> result;
  134 + for (const auto& outInfo : all_res) {
  135 + int single_length = outInfo.size() / batchsize;
  136 + // printf("batchsize:%d,single_length:%d\n",batchsize, single_length);
  137 +
  138 + int argmax = std::distance(outInfo.begin()+b*single_length, std::max_element(outInfo.begin()+b*single_length, outInfo.begin() + (b+1)*single_length));
  139 + float score = outInfo[b*single_length+argmax];
  140 + if(score < config.confThr) {
  141 + // printf("score:%f, confThr\n",score, config.confThr);
  142 + result.emplace_back(-1);
  143 + result.emplace_back(0);
  144 + }
  145 + else {
  146 + result.emplace_back(argmax);
  147 + result.emplace_back(score);
  148 + }
  149 + }
  150 + results.emplace_back(result);
  151 + }
  152 +
  153 + return SY_SUCCESS;
  154 +}
  155 +
  156 +void CnnCls::Release() {
  157 + model_.Unload();
  158 + model_.DestroyDesc();
  159 + model_.DestroyOutput();
  160 +}
  161 +
  162 +}
... ...
src/common/cnn_cls/cnn_cls.h 0 → 100755
  1 +#ifndef _CNNCls_H_
  2 +#define _CNNCls_H_
  3 +
  4 +#include <memory>
  5 +#include "utils.h"
  6 +#include "acl/acl.h"
  7 +#include "model_process.h"
  8 +
  9 +using namespace std;
  10 +
  11 +namespace atlas_utils {
  12 +
  13 +#ifndef BBOX_WIDTH
  14 +#define BBOX_WIDTH 8
  15 +#endif
  16 +
  17 +#ifndef DATA_TYPE_SIZE
  18 +#define DATA_TYPE_SIZE 4
  19 +#endif
  20 +
  21 +class CnnCls {
  22 +public:
  23 + struct ConfigParams {
  24 + float confThr = 0.0; //显示初始化,有的编译器会不执行默认初始化
  25 + };
  26 +
  27 + CnnCls() {}
  28 + ~CnnCls() {
  29 + Release();
  30 + }
  31 +
  32 + int Init(const char* modelPath);
  33 + int Inference(ImageData& input);
  34 + int PostProcess(vector<float>& result);
  35 + int PostProcess_batch(vector<vector<float>>& result);
  36 +
  37 + int GetInputWidth();
  38 + int GetInputHeight();
  39 +
  40 + ConfigParams config;
  41 +
  42 +private:
  43 + void Release();
  44 +
  45 + vector<vector<int>> outDims_;
  46 + vector<vector<int>> inDims_;
  47 + ModelProcess model_;
  48 + uint32_t modelWidth_;
  49 + uint32_t modelHeight_;
  50 + aclrtRunMode runMode_;
  51 +
  52 +};
  53 +
  54 +}
  55 +
  56 +#endif
... ...
src/common/dvppx/dvpp_cropandpastex.cpp 0 → 100755
  1 +/**
  2 +* Copyright 2020 Huawei Technologies Co., Ltd
  3 +*
  4 +* Licensed under the Apache License, Version 2.0 (the "License");
  5 +* you may not use this file except in compliance with the License.
  6 +* You may obtain a copy of the License at
  7 +
  8 +* http://www.apache.org/licenses/LICENSE-2.0
  9 +
  10 +* Unless required by applicable law or agreed to in writing, software
  11 +* distributed under the License is distributed on an "AS IS" BASIS,
  12 +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +* See the License for the specific language governing permissions and
  14 +* limitations under the License.
  15 +
  16 +* File dvpp_process.cpp
  17 +* Description: handle dvpp process
  18 +*/
  19 +
  20 +#include <iostream>
  21 +#include "acl/acl.h"
  22 +#include "utils.h"
  23 +#include "dvpp_cropandpastex.h"
  24 +#include "sy_errorinfo.h"
  25 +using namespace std;
  26 +
  27 +DvppCropAndPastex::DvppCropAndPastex(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc,
  28 + uint32_t width, uint32_t height)
  29 +: stream_(stream), dvppChannelDesc_(dvppChannelDesc),
  30 +vpcInputDesc_(nullptr), vpcOutputDesc_(nullptr),
  31 +vpcOutBufferDev_(nullptr),vpcOutBufferSize_(0){
  32 + size_.width = width;
  33 + size_.height = height;
  34 +}
  35 +
  36 +DvppCropAndPastex::~DvppCropAndPastex()
  37 +{
  38 + DestroyCropAndPasteResource();
  39 +}
  40 +
  41 +int DvppCropAndPastex::InitCropAndPasteInputDesc(ImageData& inputImage)
  42 +{
  43 + originalImageWidth_ = inputImage.width;
  44 + originalImageHeight_ = inputImage.height;
  45 + uint32_t alignWidth = inputImage.alignWidth;
  46 + uint32_t alignHeight = inputImage.alignHeight;
  47 + // printf("image w %d, h %d, align w%d, h%d",inputImage.width, inputImage.height, alignWidth, alignHeight);
  48 + if (alignWidth == 0 || alignHeight == 0) {
  49 + ERROR_LOG("InitResizeInputDesc AlignmentHelper failed. image w %d, h %d, align w%d, h%d",
  50 + inputImage.width, inputImage.height, alignWidth, alignHeight);
  51 + return SY_FAILED;
  52 + }
  53 + uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight);
  54 + vpcInputDesc_ = acldvppCreatePicDesc();
  55 + if (vpcInputDesc_ == nullptr) {
  56 + ERROR_LOG("acldvppCreatePicDesc vpcInputDesc_ failed");
  57 + return SY_FAILED;
  58 + }
  59 +
  60 + acldvppSetPicDescData(vpcInputDesc_, inputImage.data.get()); // JpegD . vpcResize
  61 + acldvppSetPicDescFormat(vpcInputDesc_, format_);
  62 + acldvppSetPicDescWidth(vpcInputDesc_, inputImage.width);
  63 + acldvppSetPicDescHeight(vpcInputDesc_, inputImage.height);
  64 + acldvppSetPicDescWidthStride(vpcInputDesc_, alignWidth);
  65 + acldvppSetPicDescHeightStride(vpcInputDesc_, alignHeight);
  66 + acldvppSetPicDescSize(vpcInputDesc_, inputBufferSize);
  67 + return SY_SUCCESS;
  68 +}
  69 +
  70 +int DvppCropAndPastex::InitCropAndPasteOutputDesc()
  71 +{
  72 + int resizeOutWidth = size_.width;
  73 + int resizeOutHeight = size_.height;
  74 + int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth);
  75 + int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight);
  76 +
  77 + if (resizeOutWidthStride == 0 || resizeOutHeightStride == 0) {
  78 + ERROR_LOG("InitResizeOutputDesc AlignmentHelper failed");
  79 + return SY_FAILED;
  80 + }
  81 +
  82 + vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride);
  83 + aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_);
  84 + //debug========================================================================
  85 + uint32_t size = ALIGN_UP(vpcOutBufferSize_,32) + 32;
  86 + aclRet = aclrtMemset(vpcOutBufferDev_,size, 128, size);
  87 + // aclRet = aclrtMemset(vpcOutBufferDev_,vpcOutBufferSize_, 0, vpcOutBufferSize_);
  88 + //debug end====================================================================
  89 + if (aclRet != ACL_SUCCESS) {
  90 + ERROR_LOG("acldvppMalloc vpcOutBufferDev_ failed, aclRet = %d", aclRet);
  91 + return SY_FAILED;
  92 + }
  93 +
  94 + vpcOutputDesc_ = acldvppCreatePicDesc();
  95 + if (vpcOutputDesc_ == nullptr) {
  96 + ERROR_LOG("acldvppCreatePicDesc vpcOutputDesc_ failed");
  97 + return SY_FAILED;
  98 + }
  99 + acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_);
  100 + acldvppSetPicDescFormat(vpcOutputDesc_, format_);
  101 + acldvppSetPicDescWidth(vpcOutputDesc_, resizeOutWidth);
  102 + acldvppSetPicDescHeight(vpcOutputDesc_, resizeOutHeight);
  103 + acldvppSetPicDescWidthStride(vpcOutputDesc_, resizeOutWidthStride);
  104 + acldvppSetPicDescHeightStride(vpcOutputDesc_, resizeOutHeightStride);
  105 + acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_);
  106 +
  107 + return SY_SUCCESS;
  108 +}
  109 +
  110 +int DvppCropAndPastex::InitRightCropAndPasteOutputDesc()
  111 +{
  112 + int resizeOutWidth = size_.width;
  113 + int resizeOutHeight = size_.height;
  114 + int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth);
  115 + int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight);
  116 + if (resizeOutWidthStride == 0 || resizeOutHeightStride == 0) {
  117 + ERROR_LOG("InitResizeOutputDesc AlignmentHelper failed");
  118 + return SY_FAILED;
  119 + }
  120 +
  121 + vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride);
  122 + //aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_);
  123 + // if (aclRet != ACL_SUCCESS) {
  124 + // ERROR_LOG("acldvppMalloc vpcOutBufferDev_ failed, aclRet = %d", aclRet);
  125 + // return SY_FAILED;
  126 + // }
  127 + vpcOutputDesc_ = acldvppCreatePicDesc();
  128 + if (vpcOutputDesc_ == nullptr) {
  129 + ERROR_LOG("acldvppCreatePicDesc vpcOutputDesc_ failed");
  130 + return SY_FAILED;
  131 + }
  132 + acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_ + vpcOutBufferSize_);
  133 + acldvppSetPicDescFormat(vpcOutputDesc_, format_);
  134 + acldvppSetPicDescWidth(vpcOutputDesc_, resizeOutWidth);
  135 + acldvppSetPicDescHeight(vpcOutputDesc_, resizeOutHeight);
  136 + acldvppSetPicDescWidthStride(vpcOutputDesc_, resizeOutWidthStride);
  137 + acldvppSetPicDescHeightStride(vpcOutputDesc_, resizeOutHeightStride);
  138 + acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_);
  139 +
  140 + return SY_SUCCESS;
  141 +}
  142 +
  143 +
  144 +// IN/OUT Desc
  145 +int DvppCropAndPastex::InitCropAndPasteResource(ImageData& inputImage) {
  146 + format_ = static_cast<acldvppPixelFormat>(PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  147 + if (SY_SUCCESS != InitCropAndPasteInputDesc(inputImage)) {
  148 + ERROR_LOG("InitCropAndPasteInputDesc failed");
  149 + return SY_FAILED;
  150 + }
  151 +
  152 + if (SY_SUCCESS != InitCropAndPasteOutputDesc()) {
  153 + ERROR_LOG("InitCropAndPasteOutputDesc failed");
  154 + return SY_FAILED;
  155 + }
  156 +
  157 + return SY_SUCCESS;
  158 +}
  159 +
  160 +int DvppCropAndPastex::InitRightCropAndPasteResource(ImageData& inputImage) {
  161 + format_ = static_cast<acldvppPixelFormat>(PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  162 + if (SY_SUCCESS != InitCropAndPasteInputDesc(inputImage)) {
  163 + ERROR_LOG("InitCropAndPasteInputDesc failed");
  164 + return SY_FAILED;
  165 + }
  166 +
  167 + if (SY_SUCCESS != InitRightCropAndPasteOutputDesc()) {
  168 + ERROR_LOG("InitCropAndPasteOutputDesc failed");
  169 + return SY_FAILED;
  170 + }
  171 +
  172 + return SY_SUCCESS;
  173 +}
  174 +
  175 +int DvppCropAndPastex::ResizeWithPadding(ImageData& resizedImage, ImageData& srcImage)
  176 +{
  177 + if (SY_SUCCESS != InitCropAndPasteResource(srcImage)) {
  178 + ERROR_LOG("Dvpp cropandpaste failed for init error");
  179 + return SY_FAILED;
  180 + }
  181 +
  182 + uint32_t cropLeftOffset = 0; // must even
  183 + uint32_t cropTopOffset = 0; // must even
  184 + uint32_t cropRightOffset = (((cropLeftOffset + originalImageWidth_) >> 1) << 1) -1; // must odd
  185 + uint32_t cropBottomOffset = (((cropTopOffset + originalImageHeight_) >> 1) << 1) -1; // must odd
  186 +
  187 + cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset,
  188 + cropTopOffset, cropBottomOffset);
  189 + if (cropArea_ == nullptr) {
  190 + ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
  191 + return SY_FAILED;
  192 + }
  193 + // cout << "====================================================" << endl;
  194 + // printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);
  195 +
  196 + bool widthRatioSmaller = true;
  197 + // The scaling ratio is based on the smaller ratio to ensure the smallest edge to fill the targe edge
  198 + float resizeRatio = static_cast<float>(size_.width) / srcImage.width;
  199 + if (resizeRatio > (static_cast<float>(size_.height) / srcImage.height)) {
  200 + resizeRatio = static_cast<float>(size_.height) / srcImage.height;
  201 + widthRatioSmaller = false;
  202 + }
  203 +
  204 + const int halfValue = 2;
  205 + uint32_t pasteLeftOffset = 0;
  206 + uint32_t pasteRightOffset = 0;
  207 + uint32_t pasteTopOffset = 0;
  208 + uint32_t pasteBottomOffset = 0;
  209 + // The left and up must be even, right and down must be odd which is required by acl
  210 + if (widthRatioSmaller) { //宽较长
  211 + pasteLeftOffset = 0; // must even
  212 + pasteRightOffset = (((pasteLeftOffset + size_.width) >> 1) << 1) -1; // must odd
  213 + // pasteTopOffset = ((static_cast<uint32_t>((size_.height - srcImage.height * resizeRatio) / halfValue) >> 1) << 1); // must even
  214 + // pasteBottomOffset = (((size_.height - pasteTopOffset) >> 1) << 1) -1; // must odd
  215 + //debug===============================================================================================
  216 + float pady = ((size_.height - srcImage.height * resizeRatio) / halfValue);
  217 + pasteTopOffset = ((static_cast<uint32_t>(pady) >> 1) << 1); // must even
  218 + pasteBottomOffset = ((static_cast<uint32_t>(size_.height - pady) >> 1) << 1) -1; // must odd
  219 + // if((int)(size_.height - srcImage.height * resizeRatio) % 2 ==0){
  220 + // pasteBottomOffset = ((static_cast<uint32_t>(size_.height - pady) >> 1) << 1) -1; // must odd
  221 + // }else{
  222 + // pasteBottomOffset = ((static_cast<uint32_t>(size_.height - pady + 1) >> 1) << 1) -1; // must odd
  223 + // }
  224 + //debug end============================================================================================
  225 + }else{ //高较长
  226 + uint32_t pad = (static_cast<uint32_t>((size_.width - srcImage.width * resizeRatio) / halfValue));
  227 + // printf("debug pad:%d\n",pad);
  228 + pasteLeftOffset = (pad + 8) / 16 * 16; // must even,作贴图区域时,需16对齐
  229 + // pasteLeftOffset = ALIGN_UP16(pad); // must even,作贴图区域时,需16对齐
  230 + pasteRightOffset = (((size_.width - pad) >> 1) << 1) -1; // must odd
  231 + pasteTopOffset = 0; // must even
  232 + pasteBottomOffset = (((pasteTopOffset + size_.height) >> 1) << 1) -1; // must odd
  233 +
  234 + }
  235 +
  236 + pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset,
  237 + pasteTopOffset, pasteBottomOffset);
  238 + if (pasteArea_ == nullptr) {
  239 + ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
  240 + return SY_FAILED;
  241 + }
  242 + // printf("debug paste area: %d %d %d %d\n", pasteLeftOffset, pasteTopOffset, pasteRightOffset, pasteBottomOffset);
  243 + // crop and patse pic
  244 + aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
  245 + vpcOutputDesc_, cropArea_, pasteArea_, stream_);
  246 + //printf("debug crop line:%d\n",__LINE__);
  247 + if (aclRet != ACL_SUCCESS) {
  248 + ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
  249 + return SY_FAILED;
  250 + }
  251 + aclRet = aclrtSynchronizeStream(stream_);
  252 + if (aclRet != ACL_SUCCESS) {
  253 + ERROR_LOG("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  254 + return SY_FAILED;
  255 + }
  256 +
  257 + resizedImage.width = size_.width;
  258 + resizedImage.height = size_.height;
  259 + resizedImage.alignWidth = ALIGN_UP16(size_.width);
  260 + resizedImage.alignHeight = ALIGN_UP2(size_.height);
  261 + resizedImage.size = vpcOutBufferSize_;
  262 + resizedImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);
  263 +
  264 + DestroyCropAndPasteResource();
  265 +
  266 + return SY_SUCCESS;
  267 +}
  268 +
  269 +
  270 +
  271 +int DvppCropAndPastex::PatchProcess(ImageData& resizedImage, ImageData& srcImage, uint32_t xmin, uint32_t ymin,
  272 + uint32_t xmax, uint32_t ymax)
  273 +{
  274 + if (SY_SUCCESS != InitCropAndPasteResource(srcImage)) {
  275 + ERROR_LOG("Dvpp cropandpaste failed for init error");
  276 + return SY_FAILED;
  277 + }
  278 +
  279 + uint32_t cropLeftOffset = ((xmin >> 1) << 1); // must even
  280 + uint32_t cropTopOffset = ((ymin >> 1) << 1); // must even
  281 + uint32_t cropRightOffset = ((xmax >> 1) << 1) -1; // must odd
  282 + uint32_t cropBottomOffset = ((ymax >> 1) << 1) -1; // must odd
  283 +
  284 + cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset,
  285 + cropTopOffset, cropBottomOffset);
  286 + if (cropArea_ == nullptr) {
  287 + ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
  288 + return SY_FAILED;
  289 + }
  290 + //printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);
  291 +
  292 + uint32_t pasteLeftOffset = 0; // must even
  293 + uint32_t pasteTopOffset = 0; // must even
  294 + uint32_t pasteRightOffset = (((pasteLeftOffset + size_.width) >> 1) << 1) -1; // must odd
  295 + uint32_t pasteBottomOffset = (((pasteTopOffset + size_.height) >> 1) << 1) -1; // must odd
  296 +
  297 + pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset,
  298 + pasteTopOffset, pasteBottomOffset);
  299 + if (pasteArea_ == nullptr) {
  300 + ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
  301 + return SY_FAILED;
  302 + }
  303 + //printf("debug paste area: %d %d %d %d\n", pasteLeftOffset, pasteTopOffset, pasteRightOffset, pasteBottomOffset);
  304 +
  305 + // crop and patse pic
  306 + aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
  307 + vpcOutputDesc_, cropArea_, pasteArea_, stream_);
  308 + //printf("debug crop line:%d\n",__LINE__);
  309 + if (aclRet != ACL_SUCCESS) {
  310 + ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
  311 + return SY_FAILED;
  312 + }
  313 +
  314 + aclRet = aclrtSynchronizeStream(stream_);
  315 + if (aclRet != ACL_SUCCESS) {
  316 + ERROR_LOG("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  317 + return SY_FAILED;
  318 + }
  319 +
  320 + resizedImage.width = size_.width;
  321 + resizedImage.height = size_.height;
  322 + resizedImage.alignWidth = ALIGN_UP16(size_.width);
  323 + resizedImage.alignHeight = ALIGN_UP2(size_.height);
  324 + resizedImage.size = vpcOutBufferSize_;
  325 + resizedImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);
  326 +
  327 + DestroyCropAndPasteResource();
  328 +
  329 + return SY_SUCCESS;
  330 +}
  331 +
  332 +int DvppCropAndPastex::Crop2Process(ImageData& resizedImage, ImageData& leftImage, ImageData& rightImage, ImageData& srcImage)
  333 +{
  334 + //left
  335 + if (SY_SUCCESS != InitCropAndPasteResource(srcImage)) {
  336 + ERROR_LOG("Dvpp cropandpaste failed for init error");
  337 + return SY_FAILED;
  338 + }
  339 + uint32_t lcropLeftOffset = 0; // must even
  340 + uint32_t lcropTopOffset = 0; // must even
  341 + uint32_t lcropRightOffset = ((srcImage.width/2 >> 1) << 1) -1; // must odd
  342 + uint32_t lcropBottomOffset = ((srcImage.height >> 1) << 1) -1; // must odd
  343 + cropArea_ = acldvppCreateRoiConfig(lcropLeftOffset, lcropRightOffset,lcropTopOffset, lcropBottomOffset);
  344 + if (cropArea_ == nullptr) {
  345 + ERROR_LOG("left acldvppCreateRoiConfig cropArea_ failed");
  346 + return SY_FAILED;
  347 + }
  348 + //printf("debug left crop area: %d %d %d %d\n", lcropLeftOffset, lcropTopOffset, lcropRightOffset, lcropBottomOffset);
  349 +
  350 + uint32_t lpasteLeftOffset = 0; // must even
  351 + uint32_t lpasteTopOffset = 0; // must even
  352 + uint32_t lpasteRightOffset = (((lpasteLeftOffset + size_.width) >> 1) << 1) -1; // must odd
  353 + uint32_t lpasteBottomOffset = (((lpasteTopOffset + size_.height) >> 1) << 1) -1; // must odd
  354 + pasteArea_ = acldvppCreateRoiConfig(lpasteLeftOffset, lpasteRightOffset,lpasteTopOffset, lpasteBottomOffset);
  355 + if (pasteArea_ == nullptr) {
  356 + ERROR_LOG("left acldvppCreateRoiConfig pasteArea_ failed");
  357 + return SY_FAILED;
  358 + }
  359 + //printf("debug left paste area: %d %d %d %d\n", lpasteLeftOffset, lpasteTopOffset, lpasteRightOffset, lpasteBottomOffset);
  360 +
  361 + // crop and patse pic
  362 + aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
  363 + vpcOutputDesc_, cropArea_, pasteArea_, stream_);
  364 + if (aclRet != ACL_SUCCESS) {
  365 + ERROR_LOG("left acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
  366 + return SY_FAILED;
  367 + }
  368 +
  369 + aclRet = aclrtSynchronizeStream(stream_);
  370 + if (aclRet != ACL_SUCCESS) {
  371 + ERROR_LOG("left crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  372 + return SY_FAILED;
  373 + }
  374 +
  375 + leftImage.width = size_.width;
  376 + leftImage.height = size_.height;
  377 + leftImage.alignWidth = ALIGN_UP16(size_.width);
  378 + leftImage.alignHeight = ALIGN_UP2(size_.height);
  379 + leftImage.size = vpcOutBufferSize_;
  380 + leftImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);
  381 +
  382 + DestroyCropAndPasteResource();
  383 +
  384 + //right
  385 + if (SY_SUCCESS != InitRightCropAndPasteResource(srcImage)) {
  386 + ERROR_LOG("Dvpp cropandpaste failed for init error");
  387 + return SY_FAILED;
  388 + }
  389 +
  390 + uint32_t rcropLeftOffset = ((srcImage.width/2 >> 1) << 1); // must even
  391 + uint32_t rcropTopOffset = 0; // must even
  392 + uint32_t rcropRightOffset = ((srcImage.width >> 1) << 1) -1; // must odd
  393 + uint32_t rcropBottomOffset = ((srcImage.height >> 1) << 1) -1; // must odd
  394 + cropArea_ = acldvppCreateRoiConfig(rcropLeftOffset, rcropRightOffset, rcropTopOffset, rcropBottomOffset);
  395 + if (cropArea_ == nullptr) {
  396 + ERROR_LOG("right acldvppCreateRoiConfig cropArea_ failed");
  397 + return SY_FAILED;
  398 + }
  399 + //printf("debug right crop area: %d %d %d %d\n", rcropLeftOffset, rcropTopOffset, rcropRightOffset, rcropBottomOffset);
  400 +
  401 + uint32_t rpasteLeftOffset = 0; // must even
  402 + uint32_t rpasteTopOffset = 0; // must even
  403 + uint32_t rpasteRightOffset = (((rpasteLeftOffset + size_.width) >> 1) << 1) -1; // must odd
  404 + uint32_t rpasteBottomOffset = (((rpasteTopOffset + size_.height) >> 1) << 1) -1; // must odd
  405 + pasteArea_ = acldvppCreateRoiConfig(rpasteLeftOffset, rpasteRightOffset, rpasteTopOffset, rpasteBottomOffset);
  406 + if (pasteArea_ == nullptr) {
  407 + ERROR_LOG("right acldvppCreateRoiConfig pasteArea_ failed");
  408 + return SY_FAILED;
  409 + }
  410 + //printf("debug right paste area: %d %d %d %d\n", rpasteLeftOffset, rpasteTopOffset, rpasteRightOffset, rpasteBottomOffset);
  411 +
  412 + // crop and patse pic
  413 + aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
  414 + vpcOutputDesc_, cropArea_, pasteArea_, stream_);
  415 + if (aclRet != ACL_SUCCESS) {
  416 + ERROR_LOG("right acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
  417 + return SY_FAILED;
  418 + }
  419 +
  420 + aclRet = aclrtSynchronizeStream(stream_);
  421 + if (aclRet != ACL_SUCCESS) {
  422 + ERROR_LOG("right crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  423 + return SY_FAILED;
  424 + }
  425 +
  426 + rightImage.width = size_.width;
  427 + rightImage.height = size_.height;
  428 + rightImage.alignWidth = ALIGN_UP16(size_.width);
  429 + rightImage.alignHeight = ALIGN_UP2(size_.height);
  430 + rightImage.size = vpcOutBufferSize_;
  431 + rightImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);
  432 + // rightImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_ + vpcOutBufferSize_);
  433 +
  434 + DestroyCropAndPasteResource();
  435 +
  436 + resizedImage.size = leftImage.size + rightImage.size;
  437 + resizedImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);
  438 +
  439 + return SY_SUCCESS;
  440 +}
  441 +
  442 +
  443 +int DvppCropAndPastex::Process(ImageData& resizedImage, ImageData& srcImage)
  444 +{
  445 + if (SY_SUCCESS != InitCropAndPasteResource(srcImage)) {
  446 + ERROR_LOG("Dvpp cropandpaste failed for init error");
  447 + return SY_FAILED;
  448 + }
  449 +
  450 + uint32_t cropLeftOffset = 0; // must even
  451 + uint32_t cropTopOffset = 0; // must even
  452 + uint32_t cropRightOffset = (((cropLeftOffset + originalImageWidth_) >> 1) << 1) -1; // must odd
  453 + uint32_t cropBottomOffset = (((cropTopOffset + originalImageHeight_) >> 1) << 1) -1; // must odd
  454 +
  455 + cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset,
  456 + cropTopOffset, cropBottomOffset);
  457 + if (cropArea_ == nullptr) {
  458 + ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
  459 + return SY_FAILED;
  460 + }
  461 + //printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);
  462 +
  463 + uint32_t pasteLeftOffset = 0; // must even
  464 + uint32_t pasteTopOffset = 0; // must even
  465 + uint32_t pasteRightOffset = (((pasteLeftOffset + size_.width) >> 1) << 1) -1; // must odd
  466 + uint32_t pasteBottomOffset = (((pasteTopOffset + size_.height) >> 1) << 1) -1; // must odd
  467 +
  468 + pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset,
  469 + pasteTopOffset, pasteBottomOffset);
  470 + if (pasteArea_ == nullptr) {
  471 + ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
  472 + return SY_FAILED;
  473 + }
  474 + //printf("debug paste area: %d %d %d %d\n", pasteLeftOffset, pasteTopOffset, pasteRightOffset, pasteBottomOffset);
  475 +
  476 + // crop and patse pic
  477 + aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
  478 + vpcOutputDesc_, cropArea_, pasteArea_, stream_);
  479 + //printf("debug crop line:%d\n",__LINE__);
  480 + if (aclRet != ACL_SUCCESS) {
  481 + ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
  482 + return SY_FAILED;
  483 + }
  484 +
  485 + aclRet = aclrtSynchronizeStream(stream_);
  486 + if (aclRet != ACL_SUCCESS) {
  487 + ERROR_LOG("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  488 + return SY_FAILED;
  489 + }
  490 +
  491 + resizedImage.width = size_.width;
  492 + resizedImage.height = size_.height;
  493 + resizedImage.alignWidth = ALIGN_UP16(size_.width);
  494 + resizedImage.alignHeight = ALIGN_UP2(size_.height);
  495 + resizedImage.size = vpcOutBufferSize_;
  496 + resizedImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);
  497 +
  498 + DestroyCropAndPasteResource();
  499 +
  500 + return SY_SUCCESS;
  501 +}
  502 +
  503 +
  504 +void DvppCropAndPastex::DestroyCropAndPasteResource()
  505 +{
  506 + if (cropArea_ != nullptr) {
  507 + (void)acldvppDestroyRoiConfig(cropArea_);
  508 + cropArea_ = nullptr;
  509 + }
  510 +
  511 + if (pasteArea_ != nullptr) {
  512 + (void)acldvppDestroyRoiConfig(pasteArea_);
  513 + pasteArea_ = nullptr;
  514 + }
  515 +
  516 + if (vpcInputDesc_ != nullptr) {
  517 + (void)acldvppDestroyPicDesc(vpcInputDesc_);
  518 + vpcInputDesc_ = nullptr;
  519 + }
  520 +
  521 + if (vpcOutputDesc_ != nullptr) {
  522 + (void)acldvppDestroyPicDesc(vpcOutputDesc_);
  523 + vpcOutputDesc_ = nullptr;
  524 + }
  525 +}
... ...
src/common/dvppx/dvpp_cropandpastex.h 0 → 100755
  1 +/**
  2 +* Copyright 2020 Huawei Technologies Co., Ltd
  3 +*
  4 +* Licensed under the Apache License, Version 2.0 (the "License");
  5 +* you may not use this file except in compliance with the License.
  6 +* You may obtain a copy of the License at
  7 +
  8 +* http://www.apache.org/licenses/LICENSE-2.0
  9 +
  10 +* Unless required by applicable law or agreed to in writing, software
  11 +* distributed under the License is distributed on an "AS IS" BASIS,
  12 +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +* See the License for the specific language governing permissions and
  14 +* limitations under the License.
  15 +
  16 +* File dvpp_process.h
  17 +* Description: handle dvpp process
  18 +*/
  19 +#pragma once
  20 +#include <cstdint>
  21 +
  22 +#include "acl/acl.h"
  23 +#include "acl/ops/acl_dvpp.h"
  24 +#include "utils.h"
  25 +
  26 +class DvppCropAndPastex {
  27 +public:
  28 + /**
  29 + * @brief Constructor
  30 + * @param [in] stream: stream
  31 + */
  32 + DvppCropAndPastex(aclrtStream &stream, acldvppChannelDesc *dvppChannelDesc,
  33 + uint32_t width, uint32_t height);
  34 +
  35 + /**
  36 + * @brief Destructor
  37 + */
  38 + ~DvppCropAndPastex();
  39 +
  40 + /**
  41 + * @brief dvpp global init
  42 + * @return result
  43 + */
  44 + int InitResource();
  45 +
  46 + /**
  47 + * @brief init dvpp output para
  48 + * @param [in] modelInputWidth: model input width
  49 + * @param [in] modelInputHeight: model input height
  50 + * @return result
  51 + */
  52 + int InitOutputPara(int modelInputWidth, int modelInputHeight);
  53 +
  54 + /**
  55 + * @brief dvpp process
  56 + * @return result
  57 + */
  58 + int Process(ImageData& resizedImage, ImageData& srcImage);
  59 + int Crop2Process(ImageData& resizedImage, ImageData& leftImage, ImageData& rightImage, ImageData& srcImage);
  60 + int PatchProcess(ImageData& resizedImage, ImageData& srcImage, uint32_t xmin, uint32_t ymin,
  61 + uint32_t xmax, uint32_t ymax);
  62 +
  63 + int ResizeWithPadding(ImageData& resizedImage, ImageData& srcImage);
  64 +
  65 +private:
  66 + int InitCropAndPasteResource(ImageData& inputImage);
  67 + int InitCropAndPasteInputDesc(ImageData& inputImage);
  68 + int InitCropAndPasteOutputDesc();
  69 +
  70 + int InitRightCropAndPasteResource(ImageData& inputImage);
  71 + int InitRightCropAndPasteOutputDesc();
  72 +
  73 + void DestroyCropAndPasteResource();
  74 +
  75 + aclrtStream stream_;
  76 + acldvppChannelDesc *dvppChannelDesc_;
  77 +
  78 +
  79 + // IN/OUT Desc
  80 + acldvppPicDesc *vpcInputDesc_;
  81 + acldvppPicDesc *vpcOutputDesc_;
  82 +
  83 + uint32_t originalImageWidth_;
  84 + uint32_t originalImageHeight_;
  85 +
  86 + acldvppRoiConfig *cropArea_;
  87 + acldvppRoiConfig *pasteArea_;
  88 +
  89 + // output buffer
  90 + void *vpcOutBufferDev_;
  91 + uint32_t vpcOutBufferSize_;
  92 +
  93 + //model [W][H]
  94 + Resolution size_;
  95 + acldvppPixelFormat format_;
  96 +};
  97 +
... ...
src/common/dvppx/dvpp_jpegdx.cpp 0 → 100755
  1 +/**
  2 +* Copyright 2020 Huawei Technologies Co., Ltd
  3 +*
  4 +* Licensed under the Apache License, Version 2.0 (the "License");
  5 +* you may not use this file except in compliance with the License.
  6 +* You may obtain a copy of the License at
  7 +
  8 +* http://www.apache.org/licenses/LICENSE-2.0
  9 +
  10 +* Unless required by applicable law or agreed to in writing, software
  11 +* distributed under the License is distributed on an "AS IS" BASIS,
  12 +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +* See the License for the specific language governing permissions and
  14 +* limitations under the License.
  15 +
  16 +* File dvpp_process.cpp
  17 +* Description: handle dvpp process
  18 +*/
  19 +
  20 +#include <iostream>
  21 +#include "acl/acl.h"
  22 +#include "dvpp_jpegdx.h"
  23 +#include "utils.h"
  24 +#include "sy_errorinfo.h"
  25 +using namespace std;
  26 +
  27 +DvppJpegDx::DvppJpegDx(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc)
  28 + : stream_(stream), dvppChannelDesc_(dvppChannelDesc),
  29 + decodeOutBufferDev_(nullptr), decodeOutputDesc_(nullptr),
  30 + decodeOutWidthStride_(0), decodeOutHeightStride_(0)
  31 +{
  32 +}
  33 +
  34 +DvppJpegDx::~DvppJpegDx()
  35 +{
  36 + DestroyDecodeResource();
  37 +}
  38 +
  39 +
  40 +int DvppJpegDx::InitDecodeOutputDesc(ImageData& inputImage)
  41 +{
  42 + //230316 ascend310p兼容=================================================
  43 + auto socVersion = aclrtGetSocName();
  44 + if (strncmp(socVersion, "Ascend310P3", sizeof("Ascend310P3") - 1) == 0) {
  45 + decodeOutWidth_ = ALIGN_UP2(inputImage.width);
  46 + decodeOutHeight_ = ALIGN_UP2(inputImage.height);
  47 + decodeOutWidthStride_ = ALIGN_UP64(inputImage.width); // 64-byte alignment
  48 + decodeOutHeightStride_ = ALIGN_UP16(inputImage.height); // 16-byte alignment
  49 + } else {
  50 + decodeOutWidth_ = inputImage.width;
  51 + decodeOutHeight_ = inputImage.height;
  52 + decodeOutWidthStride_ = ALIGN_UP128(inputImage.width); // 128-byte alignment
  53 + decodeOutHeightStride_ = ALIGN_UP16(inputImage.height); // 16-byte alignment
  54 + }
  55 + if (decodeOutWidthStride_ == 0 || decodeOutHeightStride_ == 0) {
  56 + ERROR_LOG("InitDecodeOutputDesc AlignmentHelper failed");
  57 + return SY_FAILED;
  58 + }
  59 + //=====================================================================
  60 +
  61 + acldvppJpegPredictDecSize(inputImage.data.get(), inputImage.size,
  62 + PIXEL_FORMAT_YUV_SEMIPLANAR_420, &decodeOutBufferSize_);
  63 +
  64 + aclError aclRet = acldvppMalloc(&decodeOutBufferDev_, decodeOutBufferSize_);
  65 + //debug========================================================================
  66 + uint32_t size = ALIGN_UP(decodeOutBufferSize_,32) + 32;
  67 + aclRet = aclrtMemset(decodeOutBufferDev_,size, 128, size);
  68 + // aclRet = aclrtMemset(decodeOutBufferDev_,decodeOutBufferSize_, 0, decodeOutBufferSize_);
  69 + //debug end====================================================================
  70 + if (aclRet != ACL_SUCCESS) {
  71 + ERROR_LOG("acldvppMalloc decodeOutBufferDev_ failed, aclRet = %d", aclRet);
  72 + return SY_FAILED;
  73 + }
  74 +
  75 + decodeOutputDesc_ = acldvppCreatePicDesc();
  76 + if (decodeOutputDesc_ == nullptr) {
  77 + ERROR_LOG("acldvppCreatePicDesc decodeOutputDesc_ failed");
  78 + return SY_FAILED;
  79 + }
  80 +
  81 + acldvppSetPicDescData(decodeOutputDesc_, decodeOutBufferDev_);
  82 + acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  83 + acldvppSetPicDescWidth(decodeOutputDesc_, decodeOutWidth_);
  84 + acldvppSetPicDescHeight(decodeOutputDesc_, decodeOutHeight_);
  85 + acldvppSetPicDescWidthStride(decodeOutputDesc_, decodeOutWidthStride_);
  86 + acldvppSetPicDescHeightStride(decodeOutputDesc_, decodeOutHeightStride_);
  87 + acldvppSetPicDescSize(decodeOutputDesc_, decodeOutBufferSize_);
  88 + return SY_SUCCESS;
  89 +}
  90 +
  91 +
  92 +int DvppJpegDx::Process(ImageData& dest, ImageData& src)
  93 +{
  94 + int ret = InitDecodeOutputDesc(src);
  95 + if (ret != SY_SUCCESS) {
  96 + ERROR_LOG("InitDecodeOutputDesc failed");
  97 + return SY_FAILED;
  98 + }
  99 +
  100 + ImageData imageDevice;
  101 + Utils::CopyImageDataToDvpp(imageDevice, src);
  102 +
  103 + //TODO:
  104 + aclError aclRet = acldvppJpegDecodeAsync(dvppChannelDesc_, reinterpret_cast<void *>(imageDevice.data.get()),
  105 + imageDevice.size, decodeOutputDesc_, stream_);
  106 + if (aclRet != ACL_SUCCESS) {
  107 + ERROR_LOG("acldvppJpegDecodeAsync failed, aclRet = %d", aclRet);
  108 + return SY_FAILED;
  109 + }
  110 +
  111 + aclRet = aclrtSynchronizeStream(stream_);
  112 + if (aclRet != ACL_SUCCESS) {
  113 + ERROR_LOG("decode aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  114 + return SY_FAILED;
  115 + }
  116 +
  117 + dest.width = decodeOutWidth_;
  118 + dest.height = decodeOutHeight_;
  119 + dest.alignWidth = decodeOutWidthStride_;
  120 + dest.alignHeight = decodeOutHeightStride_;
  121 + dest.size = YUV420SP_SIZE(dest.alignWidth, dest.alignHeight);
  122 + dest.data = SHARED_PRT_DVPP_BUF(decodeOutBufferDev_);
  123 + // cout << "dvpp w:" << dest.width << " h:" << dest.height << " alignw:" << dest.alignWidth <<" alignh:" <<dest.alignHeight << " size: " << dest.size << endl;
  124 + // INFO_LOG("convert image success");
  125 +
  126 + return SY_SUCCESS;
  127 +}
  128 +
  129 +void DvppJpegDx::DestroyDecodeResource()
  130 +{
  131 + if (decodeOutputDesc_ != nullptr) {
  132 + acldvppDestroyPicDesc(decodeOutputDesc_);
  133 + decodeOutputDesc_ = nullptr;
  134 + }
  135 +}
... ...
src/common/dvppx/dvpp_jpegdx.h 0 → 100755
  1 +/**
  2 +* Copyright 2020 Huawei Technologies Co., Ltd
  3 +*
  4 +* Licensed under the Apache License, Version 2.0 (the "License");
  5 +* you may not use this file except in compliance with the License.
  6 +* You may obtain a copy of the License at
  7 +
  8 +* http://www.apache.org/licenses/LICENSE-2.0
  9 +
  10 +* Unless required by applicable law or agreed to in writing, software
  11 +* distributed under the License is distributed on an "AS IS" BASIS,
  12 +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +* See the License for the specific language governing permissions and
  14 +* limitations under the License.
  15 +
  16 +* File dvpp_process.h
  17 +* Description: handle dvpp process
  18 +*/
  19 +#pragma once
  20 +#include <cstdint>
  21 +#include <string.h>
  22 +#include "acl/acl.h"
  23 +#include "acl/ops/acl_dvpp.h"
  24 +#include "utils.h"
  25 +
  26 +
  27 +/**
  28 + * DvppProcess
  29 + */
  30 +class DvppJpegDx {
  31 +public:
  32 + /**
  33 + * @brief Constructor
  34 + * @param [in] stream: stream
  35 + */
  36 + DvppJpegDx(aclrtStream &stream, acldvppChannelDesc *dvppChannelDesc);
  37 +
  38 + /**
  39 + * @brief Destructor
  40 + */
  41 + ~DvppJpegDx();
  42 +
  43 + /**
  44 + * @brief dvpp global init
  45 + * @return result
  46 + */
  47 + int InitResource();
  48 +
  49 + /**
  50 + * @brief init dvpp output para
  51 + * @param [in] modelInputWidth: model input width
  52 + * @param [in] modelInputHeight: model input height
  53 + * @return result
  54 + */
  55 + int InitOutputPara(int modelInputWidth, int modelInputHeight);
  56 +
  57 + /**
  58 + * @brief set jpegd input
  59 + * @param [in] inDevBuffer: device buffer of input pic
  60 + * @param [in] inDevBufferSize: device buffer size of input pic
  61 + * @param [in] inputWidth:width of pic
  62 + * @param [in] inputHeight:height of pic
  63 + */
  64 + void SetInput4JpegD(uint8_t* inDevBuffer, int inDevBufferSize, int inputWidth, int inputHeight);
  65 + int InitDecodeOutputDesc(ImageData& inputImage);
  66 + /**
  67 + * @brief gett dvpp output
  68 + * @param [in] outputBuffer: pointer which points to dvpp output buffer
  69 + * @param [out] outputSize: output size
  70 + */
  71 + void GetOutput(void **outputBuffer, int &outputSize);
  72 + int Process(ImageData& dest, ImageData& src);
  73 + /**
  74 + * @brief release encode resource
  75 + */
  76 + void DestroyEncodeResource();
  77 +
  78 +private:
  79 + void DestroyDecodeResource();
  80 + void DestroyResource();
  81 + void DestroyOutputPara();
  82 +
  83 + aclrtStream stream_;
  84 + acldvppChannelDesc *dvppChannelDesc_;
  85 +
  86 + void* decodeOutBufferDev_; // decode output buffer
  87 + acldvppPicDesc *decodeOutputDesc_; //decode output desc
  88 +
  89 + uint8_t *inDevBuffer_; // input pic dev buffer
  90 + uint32_t inDevBufferSizeD_; // input pic size for decode
  91 +
  92 + void *vpcOutBufferDev_; // vpc output buffer
  93 + uint32_t vpcOutBufferSize_; // vpc output size
  94 + //230316added
  95 + uint32_t decodeOutWidth_;
  96 + uint32_t decodeOutHeight_;
  97 + uint32_t decodeOutWidthStride_;
  98 + uint32_t decodeOutHeightStride_;
  99 + uint32_t decodeOutBufferSize_;
  100 +};
  101 +
... ...
src/common/dvppx/dvpp_processx.cpp 0 → 100755
  1 +/**
  2 +* Copyright 2020 Huawei Technologies Co., Ltd
  3 +*
  4 +* Licensed under the Apache License, Version 2.0 (the "License");
  5 +* you may not use this file except in compliance with the License.
  6 +* You may obtain a copy of the License at
  7 +
  8 +* http://www.apache.org/licenses/LICENSE-2.0
  9 +
  10 +* Unless required by applicable law or agreed to in writing, software
  11 +* distributed under the License is distributed on an "AS IS" BASIS,
  12 +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +* See the License for the specific language governing permissions and
  14 +* limitations under the License.
  15 +
  16 +* File dvpp_process.cpp
  17 +* Description: handle dvpp process
  18 +*/
  19 +
  20 +#include <iostream>
  21 +#include "acl/acl.h"
  22 +#include "dvpp_cropandpastex.h"
  23 +#include "dvpp_jpegdx.h"
  24 +#include "dvpp_processx.h"
  25 +#include "sy_errorinfo.h"
  26 +
  27 +using namespace std;
  28 +
  29 +DvppProcessx::DvppProcessx()
  30 + : isInitOk_(false), dvppChannelDesc_(nullptr) {
  31 + isGlobalContext_ = false;
  32 +}
  33 +
  34 +DvppProcessx::~DvppProcessx()
  35 +{
  36 + DestroyResource();
  37 +}
  38 +
  39 +void DvppProcessx::DestroyResource()
  40 +{
  41 + aclError aclRet;
  42 + if (dvppChannelDesc_ != nullptr) {
  43 + aclRet = acldvppDestroyChannel(dvppChannelDesc_);
  44 + if (aclRet != ACL_SUCCESS) {
  45 + ERROR_LOG("acldvppDestroyChannel failed, aclRet = %d", aclRet);
  46 + }
  47 +
  48 + (void)acldvppDestroyChannelDesc(dvppChannelDesc_);
  49 + dvppChannelDesc_ = nullptr;
  50 + }
  51 +
  52 + //INFO_LOG("end to destroy context");
  53 +}
  54 +
  55 +int DvppProcessx::InitResource(aclrtStream& stream)
  56 +{
  57 + aclError aclRet;
  58 + // create vpc channel description
  59 + dvppChannelDesc_ = acldvppCreateChannelDesc();
  60 + if (dvppChannelDesc_ == nullptr) {
  61 + ERROR_LOG("acldvppCreateChannelDesc failed");
  62 + return SY_FAILED;
  63 + }
  64 + // create vpc channel
  65 + aclRet = acldvppCreateChannel(dvppChannelDesc_);
  66 + if (aclRet != ACL_SUCCESS) {
  67 + ERROR_LOG("acldvppCreateChannel failed, aclRet = %d", aclRet);
  68 + return SY_FAILED;
  69 + }
  70 + stream_ = stream;
  71 + isInitOk_ = true;
  72 + //INFO_LOG("dvpp init resource ok");
  73 + return SY_SUCCESS;
  74 +}
  75 +
  76 +
  77 +int DvppProcessx::CropAndPaste(ImageData& dest, ImageData& src,
  78 + uint32_t width, uint32_t height) {
  79 + DvppCropAndPastex cropandpasteOp(stream_, dvppChannelDesc_, width, height);
  80 + return cropandpasteOp.Process(dest, src);
  81 +}
  82 +
  83 +int DvppProcessx::Crop2Paste(ImageData& dest, ImageData& leftImage, ImageData& rightImage,
  84 + ImageData& src,uint32_t width, uint32_t height) {
  85 + DvppCropAndPastex cro2pasteOp(stream_, dvppChannelDesc_, width, height);
  86 + return cro2pasteOp.Crop2Process(dest, leftImage, rightImage, src);
  87 +}
  88 +
  89 +
  90 +int DvppProcessx::CropAndPadding(ImageData& dest, ImageData& src,
  91 + uint32_t width, uint32_t height) {
  92 + DvppCropAndPastex cropandpaddingOp(stream_, dvppChannelDesc_, width, height);
  93 + return cropandpaddingOp.ResizeWithPadding(dest, src);
  94 +}
  95 +
  96 +
  97 +int DvppProcessx::PatchCropAndPaste(ImageData& dest, ImageData& src, uint32_t xmin, uint32_t ymin,
  98 + uint32_t xmax, uint32_t ymax, uint32_t width, uint32_t height) {
  99 + DvppCropAndPastex patchcropandpasteOp(stream_, dvppChannelDesc_, width, height);
  100 + return patchcropandpasteOp.PatchProcess(dest, src, xmin, ymin, xmax, ymax);
  101 +}
  102 +
  103 +int DvppProcessx::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) {
  104 + DvppJpegDx jpegD(stream_, dvppChannelDesc_);
  105 + return jpegD.Process(dest, src);
  106 +}
  107 +
  108 +// acldvppVpcConvertColorAsync 310/910暂不支持该接口
  109 +int DvppProcessx::ConvertColor(PicDesc& inPicDesc, PicDesc& outPicDesc)
  110 +{
  111 + // Input description
  112 + acldvppPicDesc* inputDesc = acldvppCreatePicDesc();
  113 + acldvppSetPicDescData(inputDesc, inPicDesc.dataBuffer);
  114 + acldvppSetPicDescFormat(inputDesc, inPicDesc.format);
  115 + acldvppSetPicDescWidth(inputDesc, inPicDesc.width);
  116 + acldvppSetPicDescHeight(inputDesc, inPicDesc.height);
  117 + acldvppSetPicDescWidthStride(inputDesc, inPicDesc.widthStride);
  118 + acldvppSetPicDescHeightStride(inputDesc, inPicDesc.heightStride);
  119 + acldvppSetPicDescSize(inputDesc, inPicDesc.dataSize);
  120 +
  121 + // Output description
  122 + int outWidthStride = ALIGN_UP16(inPicDesc.width) * 3; //Output RGB, so *3
  123 + int outHeightStride = ALIGN_UP2(inPicDesc.height);
  124 + outPicDesc.width = inPicDesc.width;
  125 + outPicDesc.height = inPicDesc.height;
  126 + outPicDesc.widthStride = outWidthStride;
  127 + outPicDesc.heightStride = outHeightStride;
  128 + outPicDesc.dataSize = outWidthStride * outHeightStride;
  129 + aclError ret = acldvppMalloc(&outPicDesc.dataBuffer, outPicDesc.dataSize);
  130 + if (ret != ACL_SUCCESS) {
  131 + ERROR_LOG("ConvertColor acldvppMalloc failed, errorCode = %d", static_cast<int32_t>(ret));
  132 + return SY_FAILED;
  133 + }
  134 +
  135 + acldvppPicDesc* outputDesc = acldvppCreatePicDesc();
  136 + acldvppSetPicDescData(outputDesc, outPicDesc.dataBuffer);
  137 + acldvppSetPicDescFormat(outputDesc, outPicDesc.format);
  138 + acldvppSetPicDescWidth(outputDesc, outPicDesc.width);
  139 + acldvppSetPicDescHeight(outputDesc, outPicDesc.height);
  140 + acldvppSetPicDescWidthStride(outputDesc, outPicDesc.widthStride);
  141 + acldvppSetPicDescHeightStride(outputDesc, outPicDesc.heightStride);
  142 + acldvppSetPicDescSize(outputDesc, outPicDesc.dataSize);
  143 +
  144 + // Execute ConvertColor
  145 + ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc, outputDesc, stream_);
  146 + if (ret != ACL_SUCCESS) {
  147 + ERROR_LOG("ConvertColor acldvppVpcConvertColorAsync failed, errorCode = %d", static_cast<int32_t>(ret));
  148 + return SY_FAILED;
  149 + }
  150 + ret = aclrtSynchronizeStream(stream_);
  151 + if (ret != ACL_SUCCESS) {
  152 + ERROR_LOG("ConvertColor aclrtSynchronizeStream failed, errorCode = %d", static_cast<int32_t>(ret));
  153 + return SY_FAILED;
  154 + }
  155 +
  156 + acldvppDestroyPicDesc(inputDesc);
  157 + acldvppDestroyPicDesc(outputDesc);
  158 +
  159 + return SY_SUCCESS;
  160 +}
  161 +
  162 +
  163 +
  164 +int DvppProcessx::CropAndPaddingBatch(ImageData* src, ImageData* dest, int batchsize, uint32_t output_width, uint32_t output_height) {
  165 +
  166 + const uint32_t inputBatchSize_ = batchsize;
  167 + const uint32_t outputBatchSize_ = batchsize;
  168 + if(inputBatchSize_ <= 0 || outputBatchSize_ <= 0) {
  169 + return SY_FAILED;
  170 + }
  171 +
  172 + aclError ret;
  173 +
  174 + // 输入
  175 + acldvppBatchPicDesc *vpcInputBatchDesc_ = acldvppCreateBatchPicDesc(inputBatchSize_);
  176 + if (vpcInputBatchDesc_ == nullptr) {
  177 + ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed");
  178 + return SY_FAILED;
  179 + }
  180 +
  181 + vector<void *> vecInPtr_;
  182 + for (uint32_t i = 0; i < inputBatchSize_; i++) {
  183 + void *inputBufferDev = src[i].data.get();
  184 + uint32_t inputBufferSize = src[i].size;
  185 +
  186 + vecInPtr_.push_back(inputBufferDev);
  187 + acldvppPicDesc *vpcInputDesc = acldvppGetPicDesc(vpcInputBatchDesc_, i);
  188 + (void)acldvppSetPicDescData(vpcInputDesc, inputBufferDev);
  189 + (void)acldvppSetPicDescFormat(vpcInputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  190 + (void)acldvppSetPicDescWidth(vpcInputDesc, src[i].width);
  191 + (void)acldvppSetPicDescHeight(vpcInputDesc, src[i].height);
  192 + (void)acldvppSetPicDescWidthStride(vpcInputDesc, src[i].alignWidth);
  193 + (void)acldvppSetPicDescHeightStride(vpcInputDesc, src[i].alignHeight);
  194 + (void)acldvppSetPicDescSize(vpcInputDesc, inputBufferSize);
  195 + }
  196 +
  197 + // 输出
  198 + acldvppBatchPicDesc *outputBatchPicDesc_ = acldvppCreateBatchPicDesc(outputBatchSize_);
  199 + if (outputBatchPicDesc_ == nullptr) {
  200 + ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed");
  201 + (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
  202 + return SY_FAILED;
  203 + }
  204 + vector<void *> vecOutPtr_;
  205 + acldvppPicDesc *vpcOutputDesc = nullptr;
  206 + acldvppRoiConfig *cropAreas[outputBatchSize_];
  207 + acldvppRoiConfig *pasteAreas[outputBatchSize_];
  208 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  209 +
  210 + uint32_t cropLeftOffset = 0; // must even
  211 + uint32_t cropTopOffset = 0; // must even
  212 + uint32_t cropRightOffset = (((cropLeftOffset + src[i].width) >> 1) << 1) -1; // must odd
  213 + uint32_t cropBottomOffset = (((cropTopOffset + src[i].height) >> 1) << 1) -1; // must odd
  214 +
  215 + cropAreas[i] = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  216 + if (cropAreas[i] == nullptr) {
  217 + ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
  218 + // 释放之前成功的部分 再退出
  219 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  220 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  221 + if (cropAreas[j] != nullptr) {
  222 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  223 + }
  224 + if (pasteAreas[j] != nullptr) {
  225 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  226 + }
  227 + }
  228 + return SY_FAILED;
  229 + }
  230 + // printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);
  231 +
  232 + bool widthRatioSmaller = true;
  233 + // The scaling ratio is based on the smaller ratio to ensure the smallest edge to fill the targe edge
  234 + float resizeRatio = static_cast<float>(output_width) / src[i].width;
  235 + if (resizeRatio > (static_cast<float>(output_height) / src[i].height)) {
  236 + resizeRatio = static_cast<float>(output_height) / src[i].height;
  237 + widthRatioSmaller = false;
  238 + }
  239 +
  240 + const int halfValue = 2;
  241 + uint32_t pasteLeftOffset = 0, pasteRightOffset = 0, pasteTopOffset = 0, pasteBottomOffset = 0;
  242 + // The left and up must be even, right and down must be odd which is required by acl
  243 + if (widthRatioSmaller) { //宽较长
  244 + pasteLeftOffset = 0; // must even
  245 + pasteRightOffset = (((pasteLeftOffset + output_width) >> 1) << 1) -1; // must odd
  246 + //debug===============================================================================================
  247 + float pady = ((output_height - src[i].height * resizeRatio) / halfValue);
  248 + pasteTopOffset = ((static_cast<uint32_t>(pady) >> 1) << 1); // must even
  249 + pasteBottomOffset = ((static_cast<uint32_t>(output_height - pady) >> 1) << 1) -1; // must odd
  250 + //debug end============================================================================================
  251 + }else { //高较长
  252 + uint32_t pad = (static_cast<uint32_t>((output_width - src[i].width * resizeRatio) / halfValue));
  253 + // printf("debug pad:%d\n",pad);
  254 + pasteLeftOffset = (pad + 8) / 16 * 16; // must even,作贴图区域时,需16对齐
  255 + pasteRightOffset = (((output_width - pad) >> 1) << 1) -1; // must odd
  256 + pasteTopOffset = 0; // must even
  257 + pasteBottomOffset = (((pasteTopOffset + output_height) >> 1) << 1) -1; // must odd
  258 + }
  259 +
  260 + pasteAreas[i] = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, pasteTopOffset, pasteBottomOffset);
  261 + if (pasteAreas[i] == nullptr) {
  262 + ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
  263 + // 释放之前成功的部分 再退出
  264 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  265 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  266 + if (cropAreas[j] != nullptr) {
  267 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  268 + }
  269 + if (pasteAreas[j] != nullptr) {
  270 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  271 + }
  272 + }
  273 + return SY_FAILED;
  274 + }
  275 +
  276 + int resizeOutWidth = output_width;
  277 + int resizeOutHeight = output_height;
  278 + int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth);
  279 + int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight);
  280 +
  281 + uint32_t vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride);
  282 + // uint32_t vpcOutBufferSize_ = ALIGN_UP(YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride),32) + 32;
  283 + void *vpcBatchOutputBufferDev = nullptr;
  284 + auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, vpcOutBufferSize_);
  285 + //debug========================================================================
  286 + ret = aclrtMemset(vpcBatchOutputBufferDev,vpcOutBufferSize_, 128, vpcOutBufferSize_);
  287 + //debug end====================================================================
  288 + if (ret != ACL_SUCCESS) {
  289 + ERROR_LOG("acldvppMalloc failed, size = %u, errorCode = %d.", vpcOutBufferSize_, static_cast<int32_t>(ret));
  290 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  291 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  292 + if (cropAreas[j] != nullptr) {
  293 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  294 + }
  295 + if (pasteAreas[j] != nullptr) {
  296 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  297 + }
  298 + }
  299 + return SY_FAILED;
  300 + }
  301 + vecOutPtr_.push_back(vpcBatchOutputBufferDev);
  302 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  303 + (void)acldvppSetPicDescData(vpcOutputDesc, vpcBatchOutputBufferDev);
  304 + (void)acldvppSetPicDescFormat(vpcOutputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  305 + (void)acldvppSetPicDescWidth(vpcOutputDesc, resizeOutWidth);
  306 + (void)acldvppSetPicDescHeight(vpcOutputDesc, resizeOutHeight);
  307 + (void)acldvppSetPicDescWidthStride(vpcOutputDesc, resizeOutWidthStride);
  308 + (void)acldvppSetPicDescHeightStride(vpcOutputDesc, resizeOutHeightStride);
  309 + (void)acldvppSetPicDescSize(vpcOutputDesc, vpcOutBufferSize_);
  310 + }
  311 +
  312 + bool bRet = false;
  313 + do {
  314 + // calculate total number of crop image
  315 + uint32_t totalNum = 0;
  316 + std::unique_ptr<uint32_t[]> roiNums(new (std::nothrow) uint32_t[inputBatchSize_]);
  317 + if (roiNums != nullptr){
  318 + for (int i = 0; i < inputBatchSize_; i++) {
  319 + // crop number of images from one source image is outputBatchSize_ / inputBatchSize_
  320 + roiNums[i] = outputBatchSize_ / inputBatchSize_;
  321 + totalNum += roiNums[i];
  322 + }
  323 + }
  324 + // crop number of images from last source image is:outputBatchSize_ / inputBatchSize_ + outputBatchSize_ % inputBatchSize_
  325 + if (outputBatchSize_ % inputBatchSize_ != 0) {
  326 + roiNums[inputBatchSize_ - 1] = (outputBatchSize_ - totalNum) + roiNums[inputBatchSize_ - 1];
  327 + }
  328 +
  329 + aclError aclRet = acldvppVpcBatchCropAndPasteAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums.get(), inputBatchSize_, outputBatchPicDesc_, cropAreas, pasteAreas, stream_); //
  330 + if (aclRet != ACL_SUCCESS) {
  331 + ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
  332 + break;
  333 + }
  334 +
  335 + aclRet = aclrtSynchronizeStream(stream_);
  336 + if (aclRet != ACL_SUCCESS) {
  337 + ERROR_LOG("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  338 + break;
  339 + }
  340 +
  341 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  342 +
  343 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  344 + void *outputDataDev = acldvppGetPicDescData(vpcOutputDesc);
  345 + uint32_t outputSize = acldvppGetPicDescSize(vpcOutputDesc);
  346 + uint32_t width = acldvppGetPicDescWidth(vpcOutputDesc);
  347 + uint32_t width_stride = acldvppGetPicDescWidthStride(vpcOutputDesc);
  348 + uint32_t height = acldvppGetPicDescHeight(vpcOutputDesc);
  349 + uint32_t height_stride = acldvppGetPicDescHeightStride(vpcOutputDesc);
  350 + acldvppPixelFormat fmt = acldvppGetPicDescFormat(vpcOutputDesc);
  351 +
  352 + dest[i].width = width;
  353 + dest[i].height = height;
  354 + dest[i].alignWidth = width_stride;
  355 + dest[i].alignHeight = height_stride;
  356 + dest[i].size = outputSize;
  357 + dest[i].data = SHARED_PRT_DVPP_BUF(outputDataDev);
  358 + }
  359 +
  360 + bRet = true;
  361 + } while (0);
  362 +
  363 + if (vpcInputBatchDesc_ != nullptr) {
  364 + (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
  365 + vpcInputBatchDesc_ = nullptr;
  366 + }
  367 +
  368 + if (!bRet){
  369 + for(int i = 0; i < vecOutPtr_.size(); i++){
  370 + if (vecOutPtr_[i] != nullptr){
  371 + acldvppFree(vecOutPtr_[i]);
  372 + }
  373 + }
  374 + }
  375 +
  376 + if (outputBatchPicDesc_ != nullptr) {
  377 + (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_);
  378 + outputBatchPicDesc_ = nullptr;
  379 + }
  380 +
  381 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  382 + if (cropAreas[i] != nullptr) {
  383 + (void)acldvppDestroyRoiConfig(cropAreas[i]);
  384 + cropAreas[i] = nullptr;
  385 + }
  386 + if (pasteAreas[i] != nullptr) {
  387 + (void)acldvppDestroyRoiConfig(pasteAreas[i]);
  388 + pasteAreas[i] = nullptr;
  389 + }
  390 + }
  391 +
  392 + return SY_SUCCESS;
  393 +}
  394 +
  395 +
  396 +int DvppProcessx::CropAndPasteBatch(ImageData* src, ImageData* dest, int batchsize, uint32_t output_width, uint32_t output_height) {
  397 +
  398 + const uint32_t inputBatchSize_ = batchsize;
  399 + const uint32_t outputBatchSize_ = batchsize;
  400 + if(inputBatchSize_ <= 0 || outputBatchSize_ <= 0) {
  401 + return SY_FAILED;
  402 + }
  403 +
  404 + aclError ret;
  405 +
  406 + // 输入
  407 + acldvppBatchPicDesc *vpcInputBatchDesc_ = acldvppCreateBatchPicDesc(inputBatchSize_);
  408 + if (vpcInputBatchDesc_ == nullptr) {
  409 + ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed");
  410 + return SY_FAILED;
  411 + }
  412 +
  413 + vector<void *> vecInPtr_;
  414 + for (uint32_t i = 0; i < inputBatchSize_; i++) {
  415 + void *inputBufferDev = src[i].data.get();
  416 + uint32_t inputBufferSize = src[i].size;
  417 +
  418 + vecInPtr_.push_back(inputBufferDev);
  419 + acldvppPicDesc *vpcInputDesc = acldvppGetPicDesc(vpcInputBatchDesc_, i);
  420 + (void)acldvppSetPicDescData(vpcInputDesc, inputBufferDev);
  421 + (void)acldvppSetPicDescFormat(vpcInputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  422 + (void)acldvppSetPicDescWidth(vpcInputDesc, src[i].width);
  423 + (void)acldvppSetPicDescHeight(vpcInputDesc, src[i].height);
  424 + (void)acldvppSetPicDescWidthStride(vpcInputDesc, src[i].alignWidth);
  425 + (void)acldvppSetPicDescHeightStride(vpcInputDesc, src[i].alignHeight);
  426 + (void)acldvppSetPicDescSize(vpcInputDesc, inputBufferSize);
  427 + }
  428 +
  429 + // 输出
  430 + acldvppBatchPicDesc *outputBatchPicDesc_ = acldvppCreateBatchPicDesc(outputBatchSize_);
  431 + if (outputBatchPicDesc_ == nullptr) {
  432 + ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed");
  433 + (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
  434 + return SY_FAILED;
  435 + }
  436 + vector<void *> vecOutPtr_;
  437 + acldvppPicDesc *vpcOutputDesc = nullptr;
  438 + acldvppRoiConfig *cropAreas[outputBatchSize_];
  439 + acldvppRoiConfig *pasteAreas[outputBatchSize_];
  440 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  441 +
  442 + uint32_t cropLeftOffset = 0; // must even
  443 + uint32_t cropTopOffset = 0; // must even
  444 + uint32_t cropRightOffset = (((cropLeftOffset + src[i].width) >> 1) << 1) -1; // must odd
  445 + uint32_t cropBottomOffset = (((cropTopOffset + src[i].height) >> 1) << 1) -1; // must odd
  446 +
  447 + cropAreas[i] = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  448 + if (cropAreas[i] == nullptr) {
  449 + ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
  450 + // 释放之前成功的部分 再退出
  451 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  452 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  453 + if (cropAreas[j] != nullptr) {
  454 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  455 + }
  456 + if (pasteAreas[j] != nullptr) {
  457 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  458 + }
  459 + }
  460 + return SY_FAILED;
  461 + }
  462 + // printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);
  463 +
  464 + uint32_t pasteLeftOffset = 0; // must even
  465 + uint32_t pasteTopOffset = 0; // must even
  466 + uint32_t pasteRightOffset = (((pasteLeftOffset + output_width) >> 1) << 1) -1; // must odd
  467 + uint32_t pasteBottomOffset = (((pasteTopOffset + output_height) >> 1) << 1) -1; // must odd
  468 +
  469 + pasteAreas[i] = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, pasteTopOffset, pasteBottomOffset);
  470 + if (pasteAreas[i] == nullptr) {
  471 + ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
  472 + // 释放之前成功的部分 再退出
  473 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  474 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  475 + if (cropAreas[j] != nullptr) {
  476 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  477 + }
  478 + if (pasteAreas[j] != nullptr) {
  479 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  480 + }
  481 + }
  482 + return SY_FAILED;
  483 + }
  484 +
  485 + int resizeOutWidth = output_width;
  486 + int resizeOutHeight = output_height;
  487 + int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth);
  488 + int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight);
  489 +
  490 + uint32_t vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride);
  491 + // uint32_t vpcOutBufferSize_ = ALIGN_UP(YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride),32) + 32;
  492 + void *vpcBatchOutputBufferDev = nullptr;
  493 + auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, vpcOutBufferSize_);
  494 + //debug========================================================================
  495 + ret = aclrtMemset(vpcBatchOutputBufferDev,vpcOutBufferSize_, 128, vpcOutBufferSize_);
  496 + //debug end====================================================================
  497 + if (ret != ACL_SUCCESS) {
  498 + ERROR_LOG("acldvppMalloc failed, size = %u, errorCode = %d.", vpcOutBufferSize_, static_cast<int32_t>(ret));
  499 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  500 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  501 + if (cropAreas[j] != nullptr) {
  502 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  503 + }
  504 + if (pasteAreas[j] != nullptr) {
  505 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  506 + }
  507 + }
  508 + return SY_FAILED;
  509 + }
  510 + vecOutPtr_.push_back(vpcBatchOutputBufferDev);
  511 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  512 + (void)acldvppSetPicDescData(vpcOutputDesc, vpcBatchOutputBufferDev);
  513 + (void)acldvppSetPicDescFormat(vpcOutputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  514 + (void)acldvppSetPicDescWidth(vpcOutputDesc, resizeOutWidth);
  515 + (void)acldvppSetPicDescHeight(vpcOutputDesc, resizeOutHeight);
  516 + (void)acldvppSetPicDescWidthStride(vpcOutputDesc, resizeOutWidthStride);
  517 + (void)acldvppSetPicDescHeightStride(vpcOutputDesc, resizeOutHeightStride);
  518 + (void)acldvppSetPicDescSize(vpcOutputDesc, vpcOutBufferSize_);
  519 + }
  520 +
  521 + bool bRet = false;
  522 + do {
  523 + // calculate total number of crop image
  524 + uint32_t totalNum = 0;
  525 + std::unique_ptr<uint32_t[]> roiNums(new (std::nothrow) uint32_t[inputBatchSize_]);
  526 + if (roiNums != nullptr){
  527 + for (int i = 0; i < inputBatchSize_; i++) {
  528 + // crop number of images from one source image is outputBatchSize_ / inputBatchSize_
  529 + roiNums[i] = outputBatchSize_ / inputBatchSize_;
  530 + totalNum += roiNums[i];
  531 + }
  532 + }
  533 + // crop number of images from last source image is:outputBatchSize_ / inputBatchSize_ + outputBatchSize_ % inputBatchSize_
  534 + if (outputBatchSize_ % inputBatchSize_ != 0) {
  535 + roiNums[inputBatchSize_ - 1] = (outputBatchSize_ - totalNum) + roiNums[inputBatchSize_ - 1];
  536 + }
  537 +
  538 + aclError aclRet = acldvppVpcBatchCropAndPasteAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums.get(), inputBatchSize_, outputBatchPicDesc_, cropAreas, pasteAreas, stream_); //
  539 + if (aclRet != ACL_SUCCESS) {
  540 + ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
  541 + break;
  542 + }
  543 +
  544 + aclRet = aclrtSynchronizeStream(stream_);
  545 + if (aclRet != ACL_SUCCESS) {
  546 + ERROR_LOG("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  547 + break;
  548 + }
  549 +
  550 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  551 +
  552 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  553 + void *outputDataDev = acldvppGetPicDescData(vpcOutputDesc);
  554 + uint32_t outputSize = acldvppGetPicDescSize(vpcOutputDesc);
  555 + uint32_t width = acldvppGetPicDescWidth(vpcOutputDesc);
  556 + uint32_t width_stride = acldvppGetPicDescWidthStride(vpcOutputDesc);
  557 + uint32_t height = acldvppGetPicDescHeight(vpcOutputDesc);
  558 + uint32_t height_stride = acldvppGetPicDescHeightStride(vpcOutputDesc);
  559 + acldvppPixelFormat fmt = acldvppGetPicDescFormat(vpcOutputDesc);
  560 +
  561 + /*
  562 + acldvppPicDesc *vpcInputDesc_= acldvppCreatePicDesc();
  563 + acldvppSetPicDescData(vpcInputDesc_, vecOutPtr_[i]);
  564 + acldvppSetPicDescFormat(vpcInputDesc_, fmt);
  565 + acldvppSetPicDescWidth(vpcInputDesc_, width);
  566 + acldvppSetPicDescHeight(vpcInputDesc_, height);
  567 + acldvppSetPicDescWidthStride(vpcInputDesc_, width_stride);
  568 + acldvppSetPicDescHeightStride(vpcInputDesc_, height_stride);
  569 + acldvppSetPicDescSize(vpcInputDesc_, outputSize);*/
  570 +
  571 + dest[i].width = width;
  572 + dest[i].height = height;
  573 + dest[i].alignWidth = ALIGN_UP16(width);
  574 + dest[i].alignHeight = ALIGN_UP2(height);
  575 + dest[i].size = outputSize;
  576 + dest[i].data = SHARED_PRT_DVPP_BUF(outputDataDev);
  577 +
  578 + }
  579 +
  580 + bRet = true;
  581 + } while (0);
  582 +
  583 +
  584 + if (vpcInputBatchDesc_ != nullptr) {
  585 + (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
  586 + vpcInputBatchDesc_ = nullptr;
  587 + }
  588 +
  589 + if (!bRet){
  590 + for(int i = 0; i < vecOutPtr_.size(); i++){
  591 + if (vecOutPtr_[i] != nullptr){
  592 + acldvppFree(vecOutPtr_[i]);
  593 + }
  594 + }
  595 + }
  596 +
  597 + if (outputBatchPicDesc_ != nullptr) {
  598 + (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_);
  599 + outputBatchPicDesc_ = nullptr;
  600 + }
  601 +
  602 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  603 + if (cropAreas[i] != nullptr) {
  604 + (void)acldvppDestroyRoiConfig(cropAreas[i]);
  605 + cropAreas[i] = nullptr;
  606 + }
  607 + if (pasteAreas[i] != nullptr) {
  608 + (void)acldvppDestroyRoiConfig(pasteAreas[i]);
  609 + pasteAreas[i] = nullptr;
  610 + }
  611 + }
  612 +
  613 + return SY_SUCCESS;
  614 +}
  615 +
  616 +
  617 +int DvppProcessx::CropAndPasteBatchV2(ImageData* src, ImageData* dest, PicRoi* PicRois, int batchsize, uint32_t output_width, uint32_t output_height) {
  618 +
  619 + const uint32_t inputBatchSize_ = batchsize;
  620 + const uint32_t outputBatchSize_ = batchsize;
  621 + if(inputBatchSize_ <= 0 || outputBatchSize_ <= 0) {
  622 + return SY_FAILED;
  623 + }
  624 +
  625 + aclError ret;
  626 +
  627 + // 输入
  628 + acldvppBatchPicDesc *vpcInputBatchDesc_ = acldvppCreateBatchPicDesc(inputBatchSize_);
  629 + if (vpcInputBatchDesc_ == nullptr) {
  630 + ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed");
  631 + return SY_FAILED;
  632 + }
  633 +
  634 + vector<void *> vecInPtr_;
  635 + for (uint32_t i = 0; i < inputBatchSize_; i++) {
  636 + void *inputBufferDev = src[i].data.get();
  637 + uint32_t inputBufferSize = src[i].size;
  638 +
  639 + vecInPtr_.push_back(inputBufferDev);
  640 + acldvppPicDesc *vpcInputDesc = acldvppGetPicDesc(vpcInputBatchDesc_, i);
  641 + (void)acldvppSetPicDescData(vpcInputDesc, inputBufferDev);
  642 + (void)acldvppSetPicDescFormat(vpcInputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  643 + (void)acldvppSetPicDescWidth(vpcInputDesc, src[i].width);
  644 + (void)acldvppSetPicDescHeight(vpcInputDesc, src[i].height);
  645 + (void)acldvppSetPicDescWidthStride(vpcInputDesc, src[i].alignWidth);
  646 + (void)acldvppSetPicDescHeightStride(vpcInputDesc, src[i].alignHeight);
  647 + (void)acldvppSetPicDescSize(vpcInputDesc, inputBufferSize);
  648 + }
  649 +
  650 + // 输出
  651 + acldvppBatchPicDesc *outputBatchPicDesc_ = acldvppCreateBatchPicDesc(outputBatchSize_);
  652 + if (outputBatchPicDesc_ == nullptr) {
  653 + ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed");
  654 + (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
  655 + return SY_FAILED;
  656 + }
  657 + vector<void *> vecOutPtr_;
  658 + acldvppPicDesc *vpcOutputDesc = nullptr;
  659 + acldvppRoiConfig *cropAreas[outputBatchSize_];
  660 + acldvppRoiConfig *pasteAreas[outputBatchSize_];
  661 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  662 +
  663 + uint32_t cropLeftOffset = ((PicRois[i].xmin >> 1) << 1); // must even
  664 + uint32_t cropTopOffset = ((PicRois[i].ymin >> 1) << 1); // must even
  665 + uint32_t cropRightOffset = ((PicRois[i].xmax >> 1) << 1) -1; // must odd
  666 + uint32_t cropBottomOffset = ((PicRois[i].ymax >> 1) << 1) -1; // must odd
  667 +
  668 + cropAreas[i] = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  669 + if (cropAreas[i] == nullptr) {
  670 + ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
  671 + // 释放之前成功的部分 再退出
  672 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  673 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  674 + if (cropAreas[j] != nullptr) {
  675 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  676 + }
  677 + if (pasteAreas[j] != nullptr) {
  678 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  679 + }
  680 + }
  681 + return SY_FAILED;
  682 + }
  683 + // printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);
  684 +
  685 + uint32_t pasteLeftOffset = 0; // must even
  686 + uint32_t pasteTopOffset = 0; // must even
  687 + uint32_t pasteRightOffset = (((pasteLeftOffset + output_width) >> 1) << 1) -1; // must odd
  688 + uint32_t pasteBottomOffset = (((pasteTopOffset + output_height) >> 1) << 1) -1; // must odd
  689 +
  690 + pasteAreas[i] = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, pasteTopOffset, pasteBottomOffset);
  691 + if (pasteAreas[i] == nullptr) {
  692 + ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
  693 + // 释放之前成功的部分 再退出
  694 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  695 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  696 + if (cropAreas[j] != nullptr) {
  697 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  698 + }
  699 + if (pasteAreas[j] != nullptr) {
  700 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  701 + }
  702 + }
  703 + return SY_FAILED;
  704 + }
  705 +
  706 + int resizeOutWidth = output_width;
  707 + int resizeOutHeight = output_height;
  708 + int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth);
  709 + int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight);
  710 +
  711 + uint32_t vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride);
  712 + // uint32_t vpcOutBufferSize_ = ALIGN_UP(YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride),32) + 32;
  713 + void *vpcBatchOutputBufferDev = nullptr;
  714 + auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, vpcOutBufferSize_);
  715 + //debug========================================================================
  716 + ret = aclrtMemset(vpcBatchOutputBufferDev,vpcOutBufferSize_, 128, vpcOutBufferSize_);
  717 + //debug end====================================================================
  718 + if (ret != ACL_SUCCESS) {
  719 + ERROR_LOG("acldvppMalloc failed, size = %u, errorCode = %d.", vpcOutBufferSize_, static_cast<int32_t>(ret));
  720 + for(int j = 0; j < vecOutPtr_.size(); j++) {
  721 + if (vecOutPtr_[j] != nullptr) { acldvppFree(vecOutPtr_[j]); }
  722 + if (cropAreas[j] != nullptr) {
  723 + (void)acldvppDestroyRoiConfig(cropAreas[j]); cropAreas[j] = nullptr;
  724 + }
  725 + if (pasteAreas[j] != nullptr) {
  726 + (void)acldvppDestroyRoiConfig(pasteAreas[j]); pasteAreas[j] = nullptr;
  727 + }
  728 + }
  729 + return SY_FAILED;
  730 + }
  731 + vecOutPtr_.push_back(vpcBatchOutputBufferDev);
  732 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  733 + (void)acldvppSetPicDescData(vpcOutputDesc, vpcBatchOutputBufferDev);
  734 + (void)acldvppSetPicDescFormat(vpcOutputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  735 + (void)acldvppSetPicDescWidth(vpcOutputDesc, resizeOutWidth);
  736 + (void)acldvppSetPicDescHeight(vpcOutputDesc, resizeOutHeight);
  737 + (void)acldvppSetPicDescWidthStride(vpcOutputDesc, resizeOutWidthStride);
  738 + (void)acldvppSetPicDescHeightStride(vpcOutputDesc, resizeOutHeightStride);
  739 + (void)acldvppSetPicDescSize(vpcOutputDesc, vpcOutBufferSize_);
  740 + }
  741 +
  742 + bool bRet = false;
  743 + do {
  744 + // calculate total number of crop image
  745 + uint32_t totalNum = 0;
  746 + std::unique_ptr<uint32_t[]> roiNums(new (std::nothrow) uint32_t[inputBatchSize_]);
  747 + if (roiNums != nullptr){
  748 + for (int i = 0; i < inputBatchSize_; i++) {
  749 + // crop number of images from one source image is outputBatchSize_ / inputBatchSize_
  750 + roiNums[i] = outputBatchSize_ / inputBatchSize_;
  751 + totalNum += roiNums[i];
  752 + }
  753 + }
  754 + // crop number of images from last source image is:outputBatchSize_ / inputBatchSize_ + outputBatchSize_ % inputBatchSize_
  755 + if (outputBatchSize_ % inputBatchSize_ != 0) {
  756 + roiNums[inputBatchSize_ - 1] = (outputBatchSize_ - totalNum) + roiNums[inputBatchSize_ - 1];
  757 + }
  758 +
  759 + aclError aclRet = acldvppVpcBatchCropAndPasteAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums.get(), inputBatchSize_, outputBatchPicDesc_, cropAreas, pasteAreas, stream_); //
  760 + if (aclRet != ACL_SUCCESS) {
  761 + ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
  762 + break;
  763 + }
  764 +
  765 + aclRet = aclrtSynchronizeStream(stream_);
  766 + if (aclRet != ACL_SUCCESS) {
  767 + ERROR_LOG("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
  768 + break;
  769 + }
  770 +
  771 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  772 +
  773 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  774 + void *outputDataDev = acldvppGetPicDescData(vpcOutputDesc);
  775 + uint32_t outputSize = acldvppGetPicDescSize(vpcOutputDesc);
  776 + uint32_t width = acldvppGetPicDescWidth(vpcOutputDesc);
  777 + uint32_t width_stride = acldvppGetPicDescWidthStride(vpcOutputDesc);
  778 + uint32_t height = acldvppGetPicDescHeight(vpcOutputDesc);
  779 + uint32_t height_stride = acldvppGetPicDescHeightStride(vpcOutputDesc);
  780 + acldvppPixelFormat fmt = acldvppGetPicDescFormat(vpcOutputDesc);
  781 +
  782 + /*
  783 + acldvppPicDesc *vpcInputDesc_= acldvppCreatePicDesc();
  784 + acldvppSetPicDescData(vpcInputDesc_, vecOutPtr_[i]);
  785 + acldvppSetPicDescFormat(vpcInputDesc_, fmt);
  786 + acldvppSetPicDescWidth(vpcInputDesc_, width);
  787 + acldvppSetPicDescHeight(vpcInputDesc_, height);
  788 + acldvppSetPicDescWidthStride(vpcInputDesc_, width_stride);
  789 + acldvppSetPicDescHeightStride(vpcInputDesc_, height_stride);
  790 + acldvppSetPicDescSize(vpcInputDesc_, outputSize);*/
  791 +
  792 + dest[i].width = width;
  793 + dest[i].height = height;
  794 + dest[i].alignWidth = ALIGN_UP16(width);
  795 + dest[i].alignHeight = ALIGN_UP2(height);
  796 + dest[i].size = outputSize;
  797 + dest[i].data = SHARED_PRT_DVPP_BUF(outputDataDev);
  798 + }
  799 +
  800 + bRet = true;
  801 + } while (0);
  802 +
  803 +
  804 + if (vpcInputBatchDesc_ != nullptr) {
  805 + (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
  806 + vpcInputBatchDesc_ = nullptr;
  807 + }
  808 +
  809 + if (!bRet){
  810 + for(int i = 0; i < vecOutPtr_.size(); i++){
  811 + if (vecOutPtr_[i] != nullptr){
  812 + acldvppFree(vecOutPtr_[i]);
  813 + }
  814 + }
  815 + }
  816 +
  817 + if (outputBatchPicDesc_ != nullptr) {
  818 + (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_);
  819 + outputBatchPicDesc_ = nullptr;
  820 + }
  821 +
  822 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  823 + if (cropAreas[i] != nullptr) {
  824 + (void)acldvppDestroyRoiConfig(cropAreas[i]);
  825 + cropAreas[i] = nullptr;
  826 + }
  827 + if (pasteAreas[i] != nullptr) {
  828 + (void)acldvppDestroyRoiConfig(pasteAreas[i]);
  829 + pasteAreas[i] = nullptr;
  830 + }
  831 + }
  832 +
  833 + return SY_SUCCESS;
  834 +}
  835 +
  836 +
  837 +
  838 +/*
  839 +vector<vpc_img_info> DvppProcessx::crop_batch(DeviceMemory *devMem, vector<video_object_info> objs){
  840 +
  841 + vector<vpc_img_info> vec_img_info;
  842 +
  843 + const uint32_t outputBatchSize_ = objs.size();
  844 + if(outputBatchSize_ <= 0){
  845 + return vec_img_info;
  846 + }
  847 +
  848 + aclError ret;
  849 + aclrtSetDevice(deviceId_);
  850 + ret = aclrtSetCurrentContext(context_);
  851 +
  852 + // 输入
  853 + acldvppBatchPicDesc *vpcInputBatchDesc_ = acldvppCreateBatchPicDesc(1);
  854 + if (vpcInputBatchDesc_ == nullptr) {
  855 + LOG_ERROR("acldvppCreatePicDesc outBatchPicDesc failed");
  856 + return vec_img_info;
  857 + }
  858 + acldvppPicDesc *vpcInputDesc_ = acldvppGetPicDesc(vpcInputBatchDesc_, 0);
  859 + acldvppSetPicDescData(vpcInputDesc_, devMem->getMem());
  860 + acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  861 + acldvppSetPicDescWidth(vpcInputDesc_, devMem->getWidth());
  862 + acldvppSetPicDescHeight(vpcInputDesc_, devMem->getHeight());
  863 + acldvppSetPicDescWidthStride(vpcInputDesc_, devMem->getWidthStride());
  864 + acldvppSetPicDescHeightStride(vpcInputDesc_, devMem->getHeightStride());
  865 + acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize());
  866 +
  867 + // 输出
  868 + acldvppBatchPicDesc *outputBatchPicDesc_ = acldvppCreateBatchPicDesc(outputBatchSize_);
  869 + if (outputBatchPicDesc_ == nullptr) {
  870 + LOG_ERROR("acldvppCreatePicDesc outBatchPicDesc failed");
  871 + (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
  872 + return vec_img_info;
  873 + }
  874 + vector<void *> vecOutPtr_;
  875 + acldvppPicDesc *vpcOutputDesc = nullptr;
  876 + acldvppRoiConfig *cropAreas[outputBatchSize_];
  877 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  878 + video_object_info obj = objs[i];
  879 +
  880 + // uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16;
  881 + // uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2;
  882 + uint32_t cropSizeWidth = (obj.right - obj.left + 15) / 16 * 16; //debug by zsh
  883 + uint32_t cropSizeHeight = (obj.bottom - obj.top + 1) / 2 * 2;
  884 +
  885 + uint32_t oddNum = 1;
  886 + uint32_t cropLeftOffset = (obj.left + 1) / 2 * 2; // must even
  887 + uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum; // must odd
  888 + uint32_t cropTopOffset = (obj.top + 1) / 2 * 2; // must even
  889 + uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd
  890 +
  891 + adjustCoordinate(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset, devMem->getWidth(), devMem->getHeight());
  892 + cropSizeWidth = cropRightOffset - cropLeftOffset + oddNum;
  893 + cropSizeHeight = cropBottomOffset - cropTopOffset + oddNum;
  894 + // LOG_DEBUG("{} ,{} ,{} ,{} ", cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  895 +
  896 + if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){
  897 + LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset);
  898 + // 释放之前成功的部分 再退出
  899 + for(int i = 0; i < vecOutPtr_.size(); i++){
  900 + if (vecOutPtr_[i] != nullptr){
  901 + acldvppFree(vecOutPtr_[i]);
  902 + }
  903 + if (cropAreas[i] != nullptr) {
  904 + (void)acldvppDestroyRoiConfig(cropAreas[i]);
  905 + cropAreas[i] = nullptr;
  906 + }
  907 + }
  908 + return vec_img_info;
  909 + }
  910 +
  911 + cropAreas[i] = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);
  912 +
  913 + uint32_t vpcOutBufferSize_ = cropSizeWidth * cropSizeHeight * 3 / 2;
  914 + void *vpcBatchOutputBufferDev = nullptr;
  915 + auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, vpcOutBufferSize_);
  916 + if (ret != ACL_SUCCESS) {
  917 + LOG_ERROR("acldvppMalloc failed, size = %u, errorCode = %d.", vpcOutBufferSize_, static_cast<int32_t>(ret));
  918 + // 释放之前成功的部分 再退出
  919 + for(int i = 0; i < vecOutPtr_.size(); i++){
  920 + if (vecOutPtr_[i] != nullptr){
  921 + acldvppFree(vecOutPtr_[i]);
  922 + }
  923 + if (cropAreas[i] != nullptr) {
  924 + (void)acldvppDestroyRoiConfig(cropAreas[i]);
  925 + cropAreas[i] = nullptr;
  926 + }
  927 + }
  928 + return vec_img_info;
  929 + }
  930 + vecOutPtr_.push_back(vpcBatchOutputBufferDev);
  931 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  932 + (void)acldvppSetPicDescData(vpcOutputDesc, vpcBatchOutputBufferDev);
  933 + (void)acldvppSetPicDescFormat(vpcOutputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
  934 + (void)acldvppSetPicDescWidth(vpcOutputDesc, cropSizeWidth);
  935 + (void)acldvppSetPicDescHeight(vpcOutputDesc, cropSizeHeight);
  936 + (void)acldvppSetPicDescWidthStride(vpcOutputDesc, cropSizeWidth);
  937 + (void)acldvppSetPicDescHeightStride(vpcOutputDesc, cropSizeHeight);
  938 + (void)acldvppSetPicDescSize(vpcOutputDesc, vpcOutBufferSize_);
  939 + }
  940 +
  941 + aclrtStream stream_;
  942 + aclrtCreateStream(&stream_);
  943 +
  944 + bool bRet = false;
  945 + do {
  946 + uint32_t roiNums[] = { outputBatchSize_ };
  947 + ret = acldvppVpcBatchCropAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums, 1, outputBatchPicDesc_, cropAreas, stream_);
  948 + if (ret != ACL_SUCCESS) {
  949 + LOG_ERROR("acldvppVpcBatchCropAsync failed, task_id : {}", devMem->getId());
  950 + break;
  951 + }
  952 + ret = aclrtSynchronizeStream(stream_);
  953 + if (ret != ACL_SUCCESS) {
  954 + LOG_ERROR("aclrtSynchronizeStream failed, task_id : {}", devMem->getId());
  955 + break;
  956 + }
  957 +
  958 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  959 + video_object_info obj = objs[i];
  960 +
  961 + vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  962 + void *outputDataDev = acldvppGetPicDescData(vpcOutputDesc);
  963 + uint32_t outputSize = acldvppGetPicDescSize(vpcOutputDesc);
  964 + uint32_t width = acldvppGetPicDescWidth(vpcOutputDesc);
  965 + uint32_t width_stride = acldvppGetPicDescWidthStride(vpcOutputDesc);
  966 + uint32_t height = acldvppGetPicDescHeight(vpcOutputDesc);
  967 + uint32_t height_stride = acldvppGetPicDescHeightStride(vpcOutputDesc);
  968 + acldvppPixelFormat fmt = acldvppGetPicDescFormat(vpcOutputDesc);
  969 +
  970 + acldvppPicDesc *vpcInputDesc_= acldvppCreatePicDesc();
  971 + acldvppSetPicDescData(vpcInputDesc_, vecOutPtr_[i]);
  972 + acldvppSetPicDescFormat(vpcInputDesc_, fmt);
  973 + acldvppSetPicDescWidth(vpcInputDesc_, width);
  974 + acldvppSetPicDescHeight(vpcInputDesc_, height);
  975 + acldvppSetPicDescWidthStride(vpcInputDesc_, width_stride);
  976 + acldvppSetPicDescHeightStride(vpcInputDesc_, height_stride);
  977 + acldvppSetPicDescSize(vpcInputDesc_, outputSize);
  978 +
  979 + vpc_img_info img_info ;
  980 + img_info.pic_desc = vpcInputDesc_;
  981 + img_info.object_id = obj.object_id;
  982 + img_info.task_id = obj.task_id; //该物体属于的任务ID号
  983 + img_info.task_frame_count = obj.task_frame_count; //该物体当前出现的帧号
  984 + img_info.index = obj.index; //该物体所属类别的编号
  985 + img_info.confidence = obj.confidence; //该物体的置信度
  986 + vec_img_info.push_back(img_info);
  987 + // vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
  988 + // string file_name = "output";
  989 + // file_name = file_name + to_string(i) + ".jpg";
  990 + // vpc_jpeg_encode(vpcOutputDesc, file_name);
  991 + }
  992 +
  993 + bRet = true;
  994 + } while (0);
  995 +
  996 + if (stream_ != nullptr) {
  997 + aclrtDestroyStream(stream_);
  998 + }
  999 +
  1000 + aclrtSetCurrentContext(context_);
  1001 +
  1002 + if (vpcInputBatchDesc_ != nullptr) {
  1003 + (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
  1004 + vpcInputBatchDesc_ = nullptr;
  1005 + }
  1006 +
  1007 + if (!bRet){
  1008 + for(int i = 0; i < vecOutPtr_.size(); i++){
  1009 + if (vecOutPtr_[i] != nullptr){
  1010 + acldvppFree(vecOutPtr_[i]);
  1011 + }
  1012 + }
  1013 + }
  1014 +
  1015 + if (outputBatchPicDesc_ != nullptr) {
  1016 + (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_);
  1017 + outputBatchPicDesc_ = nullptr;
  1018 + }
  1019 +
  1020 + // for (size_t i = 0; i < vec_img_info.size(); i++)
  1021 + // {
  1022 + // if(vec_img_info[i].pic_desc != nullptr){
  1023 + // void *outputDataDev = acldvppGetPicDescData(vec_img_info[i].pic_desc);
  1024 + // acldvppFree(outputDataDev);
  1025 + // acldvppDestroyPicDesc(vec_img_info[i].pic_desc);
  1026 + // }
  1027 + // }
  1028 +
  1029 + for (uint32_t i = 0; i < outputBatchSize_; i++) {
  1030 + if (cropAreas[i] != nullptr) {
  1031 + (void)acldvppDestroyRoiConfig(cropAreas[i]);
  1032 + cropAreas[i] = nullptr;
  1033 + }
  1034 + }
  1035 +
  1036 + return vec_img_info;
  1037 +}*/
  1038 +
... ...
src/common/dvppx/dvpp_processx.h 0 → 100755
  1 +/**
  2 +* Copyright 2020 Huawei Technologies Co., Ltd
  3 +*
  4 +* Licensed under the Apache License, Version 2.0 (the "License");
  5 +* you may not use this file except in compliance with the License.
  6 +* You may obtain a copy of the License at
  7 +
  8 +* http://www.apache.org/licenses/LICENSE-2.0
  9 +
  10 +* Unless required by applicable law or agreed to in writing, software
  11 +* distributed under the License is distributed on an "AS IS" BASIS,
  12 +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 +* See the License for the specific language governing permissions and
  14 +* limitations under the License.
  15 +
  16 +* File dvpp_process.h
  17 +* Description: handle dvpp process
  18 +*/
  19 +#pragma once
  20 +#include <cstdint>
  21 +
  22 +#include "acl/acl.h"
  23 +#include "acl/ops/acl_dvpp.h"
  24 +#include "utils.h"
  25 +
  26 +
  27 +typedef struct PicDesc {
  28 + std::string picName;
  29 +
  30 + uint32_t width;
  31 + uint32_t height;
  32 + uint32_t widthStride;
  33 + uint32_t heightStride;
  34 +
  35 + acldvppPixelFormat format;
  36 +
  37 + uint32_t dataSize;
  38 + void* dataBuffer;
  39 +
  40 + uint32_t predictedJpegDecodeSize;
  41 +} PicDesc;
  42 +
  43 +
  44 +typedef struct PicRoi {
  45 + uint32_t xmin;
  46 + uint32_t ymin;
  47 + uint32_t xmax;
  48 + uint32_t ymax;
  49 +} PicRoi;
  50 +
  51 +
  52 +/**
  53 + * DvppProcessx
  54 + */
  55 +class DvppProcessx {
  56 +public:
  57 + DvppProcessx();
  58 +
  59 + ~DvppProcessx();
  60 +
  61 + int CropAndPadding(ImageData& src, ImageData& dest,
  62 + uint32_t width, uint32_t height);
  63 + int CropAndPaste(ImageData& src, ImageData& dest,
  64 + uint32_t width, uint32_t height);
  65 + int PatchCropAndPaste(ImageData& dest, ImageData& src, uint32_t xmin, uint32_t ymin,
  66 + uint32_t xmax, uint32_t ymax, uint32_t width, uint32_t height);
  67 + int Crop2Paste(ImageData& dest, ImageData& leftImage, ImageData& rightImage,
  68 + ImageData& src,uint32_t width, uint32_t height);
  69 + int CvtJpegToYuv420sp(ImageData& src, ImageData& dest);
  70 + int ConvertColor(PicDesc& inPicDesc, PicDesc& outPicDesc);
  71 + int CropAndPaddingBatch(ImageData* src, ImageData* dest, int batchsize, uint32_t width, uint32_t height);
  72 + int CropAndPasteBatch(ImageData* src, ImageData* dest, int batchsize, uint32_t width, uint32_t height);
  73 + int CropAndPasteBatchV2(ImageData* src, ImageData* dest, PicRoi* PicRois, int batchsize, uint32_t width, uint32_t height);
  74 + // vector<vpc_img_info> crop_batch(DeviceMemory *devMem, vector<video_object_info> objs);
  75 + int InitResource(aclrtStream& stream);
  76 + void DestroyResource();
  77 +
  78 +protected:
  79 + int isInitOk_;
  80 + aclrtStream stream_;
  81 + acldvppChannelDesc *dvppChannelDesc_;
  82 + bool isGlobalContext_;
  83 +
  84 +};
  85 +
... ...
src/common/model_process/model_process.cpp 0 → 100755
  1 +#include "model_process.h"
  2 +#include <iostream>
  3 +#include "utils.h"
  4 +#include "sy_errorinfo.h"
  5 +#include <cstring>
  6 +
  7 +using namespace std;
  8 +
  9 +ModelProcess::ModelProcess():loadFlag_(false), modelId_(0), modelMemPtr_(nullptr), modelMemSize_(0),
  10 +modelWeightPtr_(nullptr),modelWeightSize_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr) {
  11 +}
  12 +
  13 +ModelProcess::~ModelProcess() {
  14 + Unload();
  15 + DestroyDesc();
  16 + DestroyInput();
  17 + DestroyOutput();
  18 +}
  19 +
  20 +int ModelProcess::LoadModelFromFileWithMem(const char *modelPath) {
  21 + if (loadFlag_) {
  22 + return SY_FAILED;
  23 + }
  24 +
  25 + ACL_CALL(aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_),
  26 + ACL_SUCCESS, SY_FAILED);
  27 +
  28 + ACL_CALL(aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST),
  29 + ACL_SUCCESS, SY_FAILED);
  30 +
  31 + ACL_CALL(aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST),
  32 + ACL_SUCCESS, SY_FAILED);
  33 +
  34 + ACL_CALL(aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_,
  35 + modelMemSize_, modelWeightPtr_, modelWeightSize_), ACL_SUCCESS, SY_FAILED);
  36 +
  37 + loadFlag_ = true;
  38 +
  39 + return SY_SUCCESS;
  40 +}
  41 +
  42 +int ModelProcess::CreateDesc() {
  43 + modelDesc_ = aclmdlCreateDesc();
  44 + if (modelDesc_ == nullptr) {
  45 + return SY_FAILED;
  46 + }
  47 +
  48 + ACL_CALL(aclmdlGetDesc(modelDesc_, modelId_), ACL_SUCCESS, SY_FAILED);
  49 +
  50 + return SY_SUCCESS;
  51 +}
  52 +
  53 +void ModelProcess::DestroyDesc() {
  54 + if (modelDesc_ != nullptr) {
  55 + (void)aclmdlDestroyDesc(modelDesc_);
  56 + modelDesc_ = nullptr;
  57 + }
  58 +}
  59 +
  60 +int ModelProcess::CreateOutput(vector<vector<int>>& dims) {
  61 + if (modelDesc_ == nullptr) {
  62 + return SY_FAILED;
  63 + }
  64 +
  65 + output_ = aclmdlCreateDataset();
  66 + if (output_ == nullptr) {
  67 + return SY_FAILED;
  68 + }
  69 +
  70 + aclError ret;
  71 + size_t outputSize = aclmdlGetNumOutputs(modelDesc_);
  72 + for (size_t i = 0; i < outputSize; ++i) {
  73 + size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i);
  74 +
  75 + void *outputBuffer = nullptr;
  76 + ACL_CALL(aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY),
  77 + ACL_SUCCESS, SY_FAILED);
  78 +
  79 + aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size);
  80 + if (outputData == nullptr) {
  81 + aclrtFree(outputBuffer);
  82 + return SY_FAILED;
  83 + }
  84 +
  85 + ret = aclmdlAddDatasetBuffer(output_, outputData);
  86 + if (ret != ACL_SUCCESS) {
  87 + aclrtFree(outputBuffer);
  88 + aclDestroyDataBuffer(outputData);
  89 + return SY_FAILED;
  90 + }
  91 +
  92 + // get output dims
  93 + aclmdlIODims outDims;
  94 + vector<int> dims_;
  95 + ACL_CALL(aclmdlGetOutputDims(modelDesc_, i, &outDims), ACL_SUCCESS, SY_FAILED);
  96 + for (int j = 0; j < outDims.dimCount; j++) {
  97 + dims_.emplace_back(outDims.dims[j]);
  98 + }
  99 + dims.emplace_back(dims_);
  100 +
  101 + }
  102 +
  103 + return SY_SUCCESS;
  104 +}
  105 +
  106 +void ModelProcess::DestroyOutput() {
  107 + if (output_ == nullptr) {
  108 + return;
  109 + }
  110 +
  111 + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) {
  112 + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i);
  113 + void* data = aclGetDataBufferAddr(dataBuffer);
  114 + (void)aclrtFree(data);
  115 + (void)aclDestroyDataBuffer(dataBuffer);
  116 + dataBuffer = nullptr;
  117 + }
  118 +
  119 + (void)aclmdlDestroyDataset(output_);
  120 + output_ = nullptr;
  121 +}
  122 +
  123 +// get input dims
  124 +int ModelProcess::GetInputDims(vector<vector<int>>& dims) {
  125 + if (modelDesc_ == nullptr) {
  126 + return SY_FAILED;
  127 + }
  128 +
  129 + aclError ret;
  130 + size_t inputSize = aclmdlGetNumInputs(modelDesc_);
  131 + //debug===================================
  132 + // uint32_t modelInputSize = aclmdlGetInputSizeByIndex(modelDesc_, 0);
  133 + // cout << "modelInputSize:" << modelInputSize << endl;
  134 + //debug end===============================
  135 + for (size_t i = 0; i < inputSize; ++i) {
  136 + // get output dims
  137 + aclmdlIODims inDims;
  138 + vector<int> dims_;
  139 + ACL_CALL(aclmdlGetInputDims(modelDesc_, i, &inDims), ACL_SUCCESS, SY_FAILED);
  140 + for (int j = 0; j < inDims.dimCount; j++) {
  141 + dims_.emplace_back(inDims.dims[j]);
  142 + }
  143 + dims.emplace_back(dims_);
  144 + }
  145 +
  146 + return SY_SUCCESS;
  147 +}
  148 +
  149 +int ModelProcess::CreateInput(void *input, size_t inputsize) {
  150 + input_ = aclmdlCreateDataset();
  151 + if (input_ == nullptr) {
  152 + return SY_FAILED;
  153 + }
  154 +
  155 + aclDataBuffer* inputData = aclCreateDataBuffer(input, inputsize);
  156 + if (inputData == nullptr) {
  157 + return SY_FAILED;
  158 + }
  159 +
  160 + aclmdlAddDatasetBuffer(input_, inputData);
  161 + if (inputData == nullptr) {
  162 + aclDestroyDataBuffer(inputData);
  163 + inputData = nullptr;
  164 + return SY_FAILED;
  165 + }
  166 +
  167 + return SY_SUCCESS;
  168 +}
  169 +
  170 +int ModelProcess::CreateInputV2(void *input, size_t inputsize) {
  171 + input_ = aclmdlCreateDataset();
  172 + if (input_ == nullptr) {
  173 + return SY_FAILED;
  174 + }
  175 + aclError ret;
  176 + for(size_t index = 0; index < aclmdlGetNumInputs(modelDesc_); ++index)
  177 + {
  178 + const char* name = aclmdlGetInputNameByIndex(modelDesc_, index);
  179 + size_t inputLen = aclmdlGetInputSizeByIndex(modelDesc_, index);
  180 + if(strcmp(name, ACL_DYNAMIC_TENSOR_NAME) == 0)
  181 + {
  182 + void *data = nullptr;
  183 + ret = aclrtMalloc(&data, inputLen, ACL_MEM_MALLOC_HUGE_FIRST);
  184 + aclDataBuffer* batchBuffer = aclCreateDataBuffer(data, inputLen);
  185 + if (batchBuffer == nullptr) {
  186 + (void)aclrtFree(data);
  187 + data = nullptr;
  188 + return SY_FAILED;
  189 + }
  190 + ret = aclmdlAddDatasetBuffer(input_, batchBuffer);
  191 + if (ret != ACL_SUCCESS) {
  192 + ERROR_LOG("add input dataset buffer failed, errorCode = %d.", static_cast<int32_t>(ret));
  193 + aclDestroyDataBuffer(batchBuffer);
  194 + batchBuffer = nullptr;
  195 + (void)aclrtFree(data);
  196 + data = nullptr;
  197 + return SY_FAILED;
  198 + }
  199 + }
  200 + else
  201 + {
  202 + aclDataBuffer* inputData = aclCreateDataBuffer(input, inputsize);
  203 + if (inputData == nullptr) {
  204 + return SY_FAILED;
  205 + }
  206 + ret = aclmdlAddDatasetBuffer(input_, inputData);
  207 + if (inputData == nullptr) {
  208 + aclDestroyDataBuffer(inputData);
  209 + inputData = nullptr;
  210 + return SY_FAILED;
  211 + }
  212 + }
  213 + }
  214 +
  215 + return SY_SUCCESS;
  216 +}
  217 +
  218 +int ModelProcess::AddInputBuff(void *input, size_t inputsize) {
  219 + aclDataBuffer* inputData = aclCreateDataBuffer(input, inputsize);
  220 + if (inputData == nullptr) {
  221 + return SY_FAILED;
  222 + }
  223 +
  224 + aclmdlAddDatasetBuffer(input_, inputData);
  225 + if (inputData == nullptr) {
  226 + aclDestroyDataBuffer(inputData);
  227 + inputData = nullptr;
  228 + return SY_FAILED;
  229 + }
  230 +
  231 + return SY_SUCCESS;
  232 +}
  233 +
  234 +
  235 +void ModelProcess::DestroyInput() {
  236 + if (input_ == nullptr) {
  237 + return;
  238 + }
  239 +
  240 + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) {
  241 + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i);
  242 + aclDestroyDataBuffer(dataBuffer);
  243 + dataBuffer = nullptr;
  244 + }
  245 + aclmdlDestroyDataset(input_);
  246 + input_ = nullptr;
  247 +}
  248 +
  249 +void ModelProcess::DestroyInputV2() {
  250 + if (input_ == nullptr) {
  251 + return;
  252 + }
  253 +
  254 + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) {
  255 + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i);
  256 + if (dataBuffer == nullptr) {
  257 + continue;
  258 + }
  259 + void *data = aclGetDataBufferAddr(dataBuffer);
  260 + if (data == nullptr){
  261 + (void)aclDestroyDataBuffer(dataBuffer);
  262 + dataBuffer = nullptr;
  263 + continue;
  264 + }
  265 + const char* name = aclmdlGetInputNameByIndex(modelDesc_, i);
  266 + if(strcmp(name, ACL_DYNAMIC_TENSOR_NAME) == 0) {
  267 + (void)aclrtFree(data);
  268 + data = nullptr;
  269 + }
  270 +
  271 + (void)aclDestroyDataBuffer(dataBuffer);
  272 + dataBuffer = nullptr;
  273 + }
  274 + (void)aclmdlDestroyDataset(input_);
  275 + input_ = nullptr;
  276 +}
  277 +
  278 +int ModelProcess::Execute() {
  279 + aclError ret = aclmdlExecute(modelId_, input_, output_);
  280 + if (ret != ACL_SUCCESS) {
  281 + ERROR_LOG("execute model failed, modelId is %u %d", modelId_, ret);
  282 + return SY_FAILED;
  283 + }
  284 + // ACL_CALL(aclmdlExecute(modelId_, input_, output_), ACL_SUCCESS, SY_FAILED);
  285 +
  286 + return SY_SUCCESS;
  287 +}
  288 +
  289 +int ModelProcess::Execute(int batchsize) {
  290 + size_t index;
  291 + aclError ret = aclmdlGetInputIndexByName(modelDesc_, ACL_DYNAMIC_TENSOR_NAME, &index);
  292 + ret = aclmdlSetDynamicBatchSize(modelId_, input_, index, batchsize);
  293 + if (ret != ACL_SUCCESS) {
  294 + ERROR_LOG("aclmdlSetDynamicBatchSize failed, modelId is %u %d", modelId_, ret);
  295 + }
  296 + ret = aclmdlExecute(modelId_, input_, output_);
  297 + if (ret != ACL_SUCCESS) {
  298 + ERROR_LOG("execute model failed, modelId is %u %d", modelId_, ret);
  299 + return SY_FAILED;
  300 + }
  301 + return SY_SUCCESS;
  302 +}
  303 +
  304 +void ModelProcess::Unload() {
  305 + if (!loadFlag_) {
  306 + return;
  307 + }
  308 +
  309 + aclError ret = aclmdlUnload(modelId_);
  310 + if (ret != ACL_SUCCESS) {
  311 + return;
  312 + }
  313 +
  314 + if (modelDesc_ != nullptr) {
  315 + (void)aclmdlDestroyDesc(modelDesc_);
  316 + modelDesc_ = nullptr;
  317 + }
  318 +
  319 + if (modelMemPtr_ != nullptr) {
  320 + aclrtFree(modelMemPtr_);
  321 + modelMemPtr_ = nullptr;
  322 + modelMemSize_ = 0;
  323 + }
  324 +
  325 + if (modelWeightPtr_ != nullptr) {
  326 + aclrtFree(modelWeightPtr_);
  327 + modelWeightPtr_ = nullptr;
  328 + modelWeightSize_ = 0;
  329 + }
  330 +
  331 + loadFlag_ = false;
  332 +}
  333 +
  334 +aclmdlDataset *ModelProcess::GetModelOutputData() {
  335 + return output_;
  336 +}
  337 +
  338 +
... ...
src/common/model_process/model_process.h 0 → 100755
  1 +#ifndef _MODEL_PROCESS_H_
  2 +#define _MODEL_PROCESS_H_
  3 +
  4 +#include <iostream>
  5 +#include <vector>
  6 +#include "utils.h"
  7 +#include "acl/acl.h"
  8 +
  9 +class ModelProcess {
  10 +public:
  11 + ModelProcess();
  12 + ~ModelProcess();
  13 +
  14 + int LoadModelFromFileWithMem(const char *modelPath);
  15 + void Unload();
  16 +
  17 + int CreateDesc();
  18 + void DestroyDesc();
  19 + aclmdlDesc* GetmodelDesc() { return modelDesc_;};
  20 +
  21 + int CreateOutput(vector<vector<int>>& dims);
  22 + void DestroyOutput();
  23 +
  24 + int CreateInput(void *input, size_t inputsize);
  25 + int CreateInputV2(void *input, size_t inputsize);
  26 + int AddInputBuff(void *input, size_t inputsize);
  27 + int GetInputDims(vector<vector<int>>& dims);
  28 +
  29 + void DestroyInput();
  30 + void DestroyInputV2();
  31 +
  32 + int Execute();
  33 + int Execute(int batchsize);
  34 +
  35 + aclmdlDataset *GetModelOutputData();
  36 +
  37 +private:
  38 + bool loadFlag_; // model load flag
  39 + uint32_t modelId_;
  40 + void *modelMemPtr_;
  41 + size_t modelMemSize_;
  42 + void *modelWeightPtr_;
  43 + size_t modelWeightSize_;
  44 + aclmdlDesc *modelDesc_;
  45 + aclmdlDataset *output_;
  46 + aclmdlDataset *input_;
  47 +};
  48 +
  49 +#endif
0 50 \ No newline at end of file
... ...
src/common/utils.cpp
... ... @@ -29,7 +29,8 @@ void* Utils::CopyDataHostToDvpp(void* data, int size) {
29 29 size, aclRet);
30 30 return nullptr;
31 31 }
32   - // INFO_LOG("malloc dvpp memory size %d ok", size);
  32 + // INFO_LOG("%d malloc dvpp memory size %d ok", __LINE__, size);
  33 + // cout << "data:" << data << endl;
33 34 // copy input to device memory
34 35 aclRet = aclrtMemcpy(buffer, size, data, size, ACL_MEMCPY_HOST_TO_DEVICE);
35 36 if (aclRet != ACL_SUCCESS) {
... ... @@ -37,7 +38,7 @@ void* Utils::CopyDataHostToDvpp(void* data, int size) {
37 38 acldvppFree(buffer);
38 39 return nullptr;
39 40 }
40   - //INFO_LOG("copy data to dvpp ok");
  41 + // INFO_LOG("copy data to dvpp ok");
41 42  
42 43 return buffer;
43 44 }
... ... @@ -53,7 +54,7 @@ void* Utils::CopyDataDeviceToDvpp(void* data, int size) {
53 54 size, aclRet);
54 55 return nullptr;
55 56 }
56   - // INFO_LOG("malloc dvpp memory size %d ok", size);
  57 + // INFO_LOG("%d malloc dvpp memory size %d ok", __LINE__, size);
57 58 // copy input to device memory
58 59 aclRet = aclrtMemcpy(buffer, size, data, size, ACL_MEMCPY_DEVICE_TO_DEVICE);
59 60 if (aclRet != ACL_SUCCESS) {
... ... @@ -61,7 +62,7 @@ void* Utils::CopyDataDeviceToDvpp(void* data, int size) {
61 62 acldvppFree(buffer);
62 63 return nullptr;
63 64 }
64   - //INFO_LOG("copy data to dvpp ok");
  65 + // INFO_LOG("copy data to dvpp ok");
65 66  
66 67 return buffer;
67 68 }
... ... @@ -119,6 +120,46 @@ int Utils::CopysyImageDataToDvpp(ImageData&amp; imageDevice, sy_img srcImage) {
119 120 return SY_SUCCESS;
120 121 }
121 122  
  123 +
  124 +// vdec
  125 +int Utils::CopysyImageDataToDvppV2(ImageData& imageDevice, sy_img srcImage) {
  126 + aclError ret = aclrtGetRunMode(&runMode_);
  127 + if (ret != ACL_SUCCESS) {
  128 + ERROR_LOG("acl get run mode failed");
  129 + return SY_FAILED;
  130 + }
  131 +
  132 + void* buffer = nullptr;
  133 + uint32_t alignWidth = ALIGN_UP16(srcImage.w_); // 16-byte alignment
  134 + uint32_t alignHeight = ALIGN_UP2(srcImage.h_); // 2-byte alignment
  135 + uint32_t size = YUV420SP_SIZE(alignWidth, alignHeight);
  136 + // cout << srcImage.w_ << " " << srcImage.h_ << " "<< alignWidth << " "<< alignHeight << " " << size << endl;
  137 + if (runMode_ == ACL_HOST) {
  138 + buffer = Utils::CopyDataHostToDvpp(srcImage.data_, size);
  139 + if (buffer == nullptr) {
  140 + ERROR_LOG("Copy image to device failed");
  141 + return SY_FAILED;
  142 + }
  143 + }
  144 + else {
  145 + buffer = Utils::CopyDataDeviceToDvpp(srcImage.data_, size);
  146 + if (buffer == nullptr) {
  147 + ERROR_LOG("Copy image to device failed");
  148 + return SY_FAILED;
  149 + }
  150 + }
  151 +
  152 + imageDevice.width = srcImage.w_;
  153 + imageDevice.height = srcImage.h_;
  154 + imageDevice.alignWidth = alignWidth;
  155 + imageDevice.alignHeight = alignHeight;
  156 + imageDevice.size = size;
  157 + imageDevice.data.reset((uint8_t*)buffer, [](uint8_t* p) { acldvppFree((void *)p); });
  158 +
  159 + return SY_SUCCESS;
  160 +}
  161 +
  162 +
122 163 int Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) {
123 164 aclError ret = aclrtGetRunMode(&runMode_);
124 165 if (ret != ACL_SUCCESS) {
... ... @@ -259,6 +300,52 @@ int Utils::ReadImageFile(ImageData&amp; image, std::string fileName) {
259 300 }
260 301  
261 302  
  303 +int Utils::ReadPngFile(ImageData& image, std::string fileName) {
  304 + struct stat sBuf;
  305 + int fileStatus = stat(fileName.data(), &sBuf);
  306 + if (fileStatus == -1) {
  307 + ERROR_LOG("failed to get file");
  308 + return SY_FAILED;
  309 + }
  310 + if (S_ISREG(sBuf.st_mode) == 0) {
  311 + ERROR_LOG("%s is not a file, please enter a file", fileName.c_str());
  312 + return SY_FAILED;
  313 + }
  314 + std::ifstream binFile(fileName, std::ifstream::binary);
  315 + if (binFile.is_open() == false) {
  316 + ERROR_LOG("open file %s failed", fileName.c_str());
  317 + return SY_FAILED;
  318 + }
  319 +
  320 + binFile.seekg(0, binFile.end);
  321 + uint32_t binFileBufferLen = binFile.tellg();
  322 + if (binFileBufferLen == 0) {
  323 + ERROR_LOG("binfile is empty, filename is %s", fileName.c_str());
  324 + binFile.close();
  325 + return SY_FAILED;
  326 + }
  327 +
  328 + binFile.seekg(0, binFile.beg);
  329 +
  330 + uint8_t* binFileBufferData = new(std::nothrow) uint8_t[binFileBufferLen];
  331 + if (binFileBufferData == nullptr) {
  332 + ERROR_LOG("malloc binFileBufferData failed");
  333 + binFile.close();
  334 + return SY_FAILED;
  335 + }
  336 + binFile.read((char *)binFileBufferData, binFileBufferLen);
  337 + binFile.close();
  338 +
  339 + int32_t ch = 0;
  340 + acldvppPngGetImageInfo(binFileBufferData, binFileBufferLen,
  341 + &(image.width), &(image.height), &ch);
  342 + image.data.reset(binFileBufferData, [](uint8_t* p) { delete[](p); });
  343 + image.size = binFileBufferLen;
  344 + return SY_SUCCESS;
  345 +}
  346 +
  347 +
  348 +
262 349 float Utils::sigmoid(float val){
263 350 return 1.0/(1.0+exp(-val));
264 351 }
... ...
src/common/utils.h
... ... @@ -72,9 +72,11 @@ public:
72 72 static void* CopyDataHostToDevice(void* deviceData, uint32_t dataSize);
73 73 static void* CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize);
74 74 static int ReadImageFile(ImageData& image, std::string fileName);
  75 + static int ReadPngFile(ImageData& image, std::string fileName);
75 76 static int CopyImageDataToDevice(ImageData& imageDevice, ImageData srcImage, aclrtRunMode mode);
76 77 static int CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage);
77 78 static int CopysyImageDataToDvpp(ImageData& imageDevice, sy_img srcImage);
  79 + static int CopysyImageDataToDvppV2(ImageData& imageDevice, sy_img srcImage);
78 80 static void* CopyDataHostToDvpp(void* data, int size);
79 81 static bool CreateFolder(std::string folderPath, mode_t mode = 0700);
80 82 static bool WriteImage(unsigned char* data, int32_t size, const char* filename);
... ...
src/main.cpp 0 → 100644
  1 +#include "PicAnalysis.h"
  2 +
  3 +int main() {
  4 +
  5 + PicAnalysis pic_analysis;
  6 +
  7 + pic_analysis.init(0);
  8 +
  9 + vector<string> vec_path;
  10 + for (size_t i = 0; i < 10; i++)
  11 + {
  12 + vec_path.push_back("./test_head_tail.jpg");
  13 + }
  14 +
  15 + pic_analysis.analysis_sync(vec_path);
  16 +
  17 + pic_analysis.release();
  18 +
  19 + return 0;
  20 +}
0 21 \ No newline at end of file
... ...
src/src.cpp deleted
1   -#include "./ai_engine_module/VehicleAnalysis.h"
2   -#include "./ai_engine_module/include.h"
3   -#include "./utils/logger.hpp"
4   -
5   -int main() {
6   -
7   - VehicleAnalysis va;
8   - va.init(0, 16);
9   -
10   - string filename = "./test.jpg";
11   -
12   - aclrtStream stream = nullptr;
13   - ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
14   - DvppProcess* dvpp = new DvppProcess();
15   - dvpp->InitResource(stream);
16   -
17   - const int batchsize = 1;
18   -
19   - vector<ImageData> vec_img;
20   - ImageData dvpp_data;
21   - for (int b = 0; b < batchsize; b++) {
22   - ImageData src;
23   - Utils::ReadImageFile(src, filename); //将二进制图像读入内存,并读取宽高信息
24   -
25   - ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data, src), SY_SUCCESS, SY_FAILED); //解码
26   - vec_img.push_back(dvpp_data);
27   - }
28   -
29   - printf("1 \n");
30   -
31   - va.detect(vec_img);
32   -
33   -
34   - delete dvpp;
35   - dvpp = nullptr;
36   -
37   - if (stream != nullptr) {
38   - int ret = aclrtDestroyStream(stream);
39   - if (ret != ACL_SUCCESS) {
40   - LOG_ERROR("destroy stream failed");
41   - }
42   - stream = nullptr;
43   - }
44   -
45   - return 0;
46   -}
47 0 \ No newline at end of file