vpc_util.cpp 14.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <dirent.h>
#include <fstream>
#include <cstring>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <map>
#include <cstdint>
#include "acl/acl.h"
#include "acl/ops/acl_dvpp.h"

#include "vpc_util.h"
#include "JpegUtil.h"
#include "../decoder/interface/DeviceMemory.hpp"

#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO]  " fmt "\n", ##args)
#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN]  " fmt "\n", ##args)
#define ERROR_LOG(fmt, args...) fprintf(stderr, "[ERROR]  " fmt "\n", ##args)

typedef enum Result {
    SUCCESS = 0,
    FAILED = 1
} Result;

typedef struct PicDesc {
    std::string picName;
    int width;
    int height;
}PicDesc;

typedef struct CropPicDesc {
    std::string picName;
    int left;
    int top;
    int width;
    int height;
}CropPicDesc;

int32_t deviceId_;
aclrtContext context_;
aclrtStream stream_;

aclvdecChannelDesc *vdecChannelDesc_;
acldvppStreamDesc *streamInputDesc_;
acldvppPicDesc *picOutputDesc_;
acldvppChannelDesc *dvppChannelDesc_;
uint32_t inBufferSize;
uint32_t inputWidth;
uint32_t inputHeight;
aclrtRunMode runMode;

PicDesc inPicDesc;
CropPicDesc outPicDesc;



uint32_t AlignmentHelper(uint32_t origSize, uint32_t alignment)
{
    if (alignment == 0) {
        return 0;
    }
    uint32_t alignmentH = alignment - 1;
    return (origSize + alignmentH) / alignment * alignment;
}


void dvpp_crop_release()
{
    aclError ret;
    // ret = aclrtSetDevice(deviceId_);
    // aclrtSetCurrentContext(context_);

    ret = acldvppDestroyChannel(dvppChannelDesc_);
    ret = acldvppDestroyChannelDesc(dvppChannelDesc_);

    if (stream_ != nullptr) {
        ret = aclrtDestroyStream(stream_);
        if (ret != ACL_SUCCESS) {
            ERROR_LOG("destroy stream failed");
        }
        stream_ = nullptr;
    }
    INFO_LOG("end to destroy stream");

    if (context_ != nullptr) {
        ret = aclrtDestroyContext(context_);
        if (ret != ACL_SUCCESS) {
            ERROR_LOG("destroy context failed");
        }
        context_ = nullptr;
    }
    INFO_LOG("end to destroy context");

    ret = aclrtResetDevice(deviceId_);
    if (ret != ACL_SUCCESS) {
        ERROR_LOG("reset device failed");
    }
    INFO_LOG("end to reset device is %d", deviceId_);
}


int dvpp_crop(acldvppPicDesc *input_pic_desc)
{
    /* 1.ACL initialization  */
    // aclInit(nullptr);

    /* 2.Run the management resource application, including Device, Context, Stream */
    aclrtSetDevice(deviceId_);
    aclrtCreateContext(&context_, deviceId_);
    aclrtCreateStream(&stream_);
    aclrtGetRunMode(&runMode);

    /* 3.Initialization parameters: width and height of the original image, crop width and height. 
     * Initialize folder: Output folder */

    outPicDesc={"output.jpg", 100, 100, 300, 300};

    const int orimodelInputWidth = outPicDesc.width; // cur model shape is 224 * 224
    const int orimodelInputHeight = outPicDesc.height;
    const int modelInputLeft = outPicDesc.left; // cur model shape is 224 * 224
    const int modelInputTop = outPicDesc.top;

    /* 4. Channel description information when creating image data processing channels, 
     * dvppChannelDesc_ is acldvppChannelDesc type */
    dvppChannelDesc_ = acldvppCreateChannelDesc();

    /* 5. Create the image data processing channel. */
    acldvppCreateChannel(dvppChannelDesc_);

    // GetPicDevBuffer4JpegD
    int modelInputWidth = (orimodelInputWidth + 15) / 16 * 16;
    int modelInputHeight = (orimodelInputHeight + 1) / 2 * 2;
    
    uint32_t oddNum = 1;
    uint32_t cropSizeWidth = modelInputWidth;
    uint32_t cropSizeHeight = modelInputHeight;
    uint32_t cropLeftOffset = modelInputLeft;  // must even
    uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum;  // must odd
    uint32_t cropTopOffset = modelInputTop;  // must even
    uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum;  // must odd
    acldvppRoiConfig  *cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);

    /* processdecode */
    uint32_t vpcOutBufferSize_ = modelInputWidth * modelInputHeight * 3 / 2;
    void *vpcOutBufferDev_ = nullptr;
    acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_);
    acldvppPicDesc *vpcOutputDesc_ = acldvppCreatePicDesc();
    acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_);
    acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
    acldvppSetPicDescWidth(vpcOutputDesc_, modelInputWidth);
    acldvppSetPicDescHeight(vpcOutputDesc_, modelInputHeight);
    acldvppSetPicDescWidthStride(vpcOutputDesc_, modelInputWidth);
    acldvppSetPicDescHeightStride(vpcOutputDesc_, modelInputHeight);
    acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_);

    int ret = acldvppVpcCropAsync(dvppChannelDesc_, input_pic_desc, vpcOutputDesc_, cropArea_, stream_);

    ret = aclrtSynchronizeStream(stream_);

    DVPP_UTIL::dvpp_jpeg_encode(vpcOutputDesc_, "output.jpg");

    /* DestroycropResource */
    (void)acldvppDestroyRoiConfig(cropArea_);
    cropArea_ = nullptr;
    (void)acldvppDestroyPicDesc(vpcOutputDesc_);
    vpcOutputDesc_ = nullptr;

    if (vpcOutBufferDev_ != nullptr) {
        (void)acldvppFree(vpcOutBufferDev_);
	    vpcOutBufferDev_ = nullptr;
    }

    return SUCCESS;
}

int dvpp_crop_init(int32_t devId){
    deviceId_ = devId;

    aclError ret;
    /* 2.Run the management resource application, including Device, Context, Stream */
    aclrtSetDevice(deviceId_);
    aclrtCreateContext(&context_, deviceId_);
    aclrtCreateStream(&stream_);
    aclrtGetRunMode(&runMode);

    // channel  准备
    dvppChannelDesc_ = acldvppCreateChannelDesc();
    ret = acldvppCreateChannel(dvppChannelDesc_);

}

void check_coordinate(uint32_t& cropLeftOffset, uint32_t& cropRightOffset, uint32_t& cropTopOffset, uint32_t& cropBottomOffset, uint32_t width, uint32_t height){
    if (cropLeftOffset < 0){
        cropLeftOffset = 0;
    }
    if (cropTopOffset < 0){
        cropTopOffset = 0;
    }
    if(cropRightOffset > width){
        cropRightOffset = width;
    }
    if(cropBottomOffset > height){
        cropBottomOffset = height;
    }
    
}

vector<vpc_img_info> dvpp_crop_batch(DeviceMemory *devMem, vector<video_object_info> objs){

    vector<vpc_img_info> vec_img_info;

    /* 1.ACL initialization  */
    // aclInit(nullptr);
    aclError ret;
    aclrtSetDevice(deviceId_);
    ret = aclrtSetCurrentContext(context_);

    // 输入
	acldvppBatchPicDesc *vpcInputBatchDesc_ = acldvppCreateBatchPicDesc(1);
	acldvppPicDesc *vpcInputDesc_ = acldvppGetPicDesc(vpcInputBatchDesc_, 0);
	acldvppSetPicDescData(vpcInputDesc_, devMem->getMem()); 
	acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
	acldvppSetPicDescWidth(vpcInputDesc_, devMem->getWidth());
	acldvppSetPicDescHeight(vpcInputDesc_, devMem->getHeight());
	acldvppSetPicDescWidthStride(vpcInputDesc_, devMem->getWidthStride());
	acldvppSetPicDescHeightStride(vpcInputDesc_, devMem->getHeightStride());
	acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize());

    const uint32_t outputBatchSize_ = objs.size();
    // 输出
    acldvppBatchPicDesc *outputBatchPicDesc_ = acldvppCreateBatchPicDesc(outputBatchSize_);
    if (outputBatchPicDesc_ == nullptr) {
        ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed");
        return vec_img_info;
    }
    vector<void *> vecOutPtr_;
    acldvppPicDesc *vpcOutputDesc = nullptr;
    // acldvppRoiConfig cropAreas[outputBatchSize_];
    // std::unique_ptr<acldvppRoiConfig *[]>
    //     cropAreas(new(std::nothrow) acldvppRoiConfig *[outputBatchSize_ * sizeof(acldvppRoiConfig *)]);
    acldvppRoiConfig *cropAreas[outputBatchSize_];
    for (uint32_t i = 0; i < outputBatchSize_; i++) {
        video_object_info obj = objs[i];

        int orimodelInputWidth = obj.right - obj.left; 
        int orimodelInputHeight = obj.bottom - obj.top;
        // GetPicDevBuffer4JpegD
        int modelInputWidth = (orimodelInputWidth + 15) / 16 * 16;
        int modelInputHeight = (orimodelInputHeight + 1) / 2 * 2;

        uint32_t oddNum = 1;
        uint32_t cropSizeWidth = modelInputWidth;
        uint32_t cropSizeHeight = modelInputHeight;
        uint32_t cropLeftOffset = obj.left;  // must even
        uint32_t cropRightOffset = obj.right;//cropLeftOffset + cropSizeWidth - oddNum;  // must odd
        uint32_t cropTopOffset = obj.top;  // must even
        uint32_t cropBottomOffset = obj.bottom;//cropTopOffset + cropSizeHeight - oddNum;  // must odd

        check_coordinate(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset, devMem->getWidth(), devMem->getHeight());
        
        cropAreas[i] = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset);

        uint32_t vpcOutBufferSize_ = modelInputWidth * modelInputHeight * 3 / 2;
        void *vpcBatchOutputBufferDev = nullptr;
        auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, vpcOutBufferSize_);
        if (ret != ACL_SUCCESS) {
            ERROR_LOG("acldvppMalloc failed, size = %u, errorCode = %d.", vpcOutBufferSize_, static_cast<int32_t>(ret));
            // 释放之前成功的部分 再退出
            for(int i = 0; i < vecOutPtr_.size(); i++){
                if (vecOutPtr_[i] != nullptr){
                    acldvppFree(vecOutPtr_[i]);
                }
            }
            return vec_img_info;
        }
        vecOutPtr_.push_back(vpcBatchOutputBufferDev);
        vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
        (void)acldvppSetPicDescData(vpcOutputDesc, vpcBatchOutputBufferDev);
        (void)acldvppSetPicDescFormat(vpcOutputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
        (void)acldvppSetPicDescWidth(vpcOutputDesc, modelInputWidth);
        (void)acldvppSetPicDescHeight(vpcOutputDesc, modelInputHeight);
        (void)acldvppSetPicDescWidthStride(vpcOutputDesc, modelInputWidth);
        (void)acldvppSetPicDescHeightStride(vpcOutputDesc, modelInputHeight);
        (void)acldvppSetPicDescSize(vpcOutputDesc, vpcOutBufferSize_);
    }

    uint32_t roiNums[] = { outputBatchSize_ };
    ret = acldvppVpcBatchCropAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums, 1, outputBatchPicDesc_, cropAreas, stream_);
    ret = aclrtSynchronizeStream(stream_);

    for (uint32_t i = 0; i < outputBatchSize_; i++) {
        video_object_info obj = objs[i];

        vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
        void *outputDataDev = acldvppGetPicDescData(vpcOutputDesc);
        uint32_t outputSize = acldvppGetPicDescSize(vpcOutputDesc);
        uint32_t width = acldvppGetPicDescWidth(vpcOutputDesc);
        uint32_t width_stride = acldvppGetPicDescWidthStride(vpcOutputDesc);
        uint32_t height = acldvppGetPicDescHeight(vpcOutputDesc);
        uint32_t height_stride = acldvppGetPicDescHeightStride(vpcOutputDesc);
        acldvppPixelFormat  fmt = acldvppGetPicDescFormat(vpcOutputDesc);

        acldvppPicDesc *vpcInputDesc_= acldvppCreatePicDesc();
        acldvppSetPicDescData(vpcInputDesc_, vecOutPtr_[i]); 
        acldvppSetPicDescFormat(vpcInputDesc_, fmt);
        acldvppSetPicDescWidth(vpcInputDesc_, width);
        acldvppSetPicDescHeight(vpcInputDesc_, height);
        acldvppSetPicDescWidthStride(vpcInputDesc_, width_stride);
        acldvppSetPicDescHeightStride(vpcInputDesc_, height_stride);
        acldvppSetPicDescSize(vpcInputDesc_, outputSize);

        vpc_img_info img_info ;
        img_info.pic_desc = vpcInputDesc_;
        img_info.object_id = obj.object_id;
        img_info.task_id = obj.task_id;              //该物体属于的任务ID号
        img_info.task_frame_count = obj.task_frame_count;     //该物体当前出现的帧号
        img_info.index = obj.index;                //该物体所属类别的编号
        img_info.confidence = obj.confidence;      //该物体的置信度
        vec_img_info.push_back(img_info);
        // vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i);
        // string file_name = "output";
        // file_name = file_name + to_string(i) + ".jpg";
        // dvpp_jpeg_encode(vpcOutputDesc, file_name);
    }

    aclrtSetCurrentContext(context_);

    if (vpcInputBatchDesc_ != nullptr) {
        (void)acldvppDestroyBatchPicDesc(vpcInputBatchDesc_);
        vpcInputBatchDesc_ = nullptr;
    }

    // for(int i = 0; i < vecOutPtr_.size(); i++){
    //     if (vecOutPtr_[i] != nullptr){
    //         acldvppFree(vecOutPtr_[i]);
    //     }
    // }

    if (outputBatchPicDesc_ != nullptr) {
        (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_);
        outputBatchPicDesc_ = nullptr;
    }

    // for (size_t i = 0; i < vec_img_info.size(); i++)
    // {
    //     if(vec_img_info[i].pic_desc != nullptr){
    //         void *outputDataDev = acldvppGetPicDescData(vec_img_info[i].pic_desc);
    //         acldvppFree(outputDataDev);
    //         acldvppDestroyPicDesc(vec_img_info[i].pic_desc);
    //     }
    // }
    

    for (uint32_t i = 0; i < outputBatchSize_; i++) {
        if (cropAreas[i] != nullptr) {
            (void)acldvppDestroyRoiConfig(cropAreas[i]);
            cropAreas[i] = nullptr;
        }
    }

    return vec_img_info;
}

vpc_img_info dvpp_devMem2vpcImg(DeviceMemory *devMem){
    vpc_img_info img_info ;

    int nBufferSize = devMem->getSize();

    void *devBuffer = nullptr;
    auto ret = acldvppMalloc(&devBuffer, nBufferSize);
    if (ret != ACL_SUCCESS) {
        ERROR_LOG("acldvppMalloc failed, size = %u, errorCode = %d.", nBufferSize, static_cast<int32_t>(ret));
        // 这里应释放之前成功的部分 再退出
        return img_info;
    }

    aclrtMemcpy(devBuffer, nBufferSize, devMem->getMem(), nBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE);

    acldvppPicDesc *vpcInputDesc_= acldvppCreatePicDesc();
	acldvppSetPicDescData(vpcInputDesc_, devBuffer); 
	acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
	acldvppSetPicDescWidth(vpcInputDesc_, devMem->getWidth());
	acldvppSetPicDescHeight(vpcInputDesc_, devMem->getHeight());
	acldvppSetPicDescWidthStride(vpcInputDesc_, devMem->getWidthStride());
	acldvppSetPicDescHeightStride(vpcInputDesc_, devMem->getHeightStride());
	acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize());

    img_info.pic_desc = vpcInputDesc_;
    img_info.object_id = -1;
    img_info.task_id = devMem->getId();              //该物体属于的任务ID号
    img_info.index = -1;                //该物体所属类别的编号
    img_info.confidence = 0.0;      //该物体的置信度

    return img_info;
}

void dvpp_img_release(vpc_img_info img_info){
    if(img_info.pic_desc != nullptr){
        void *outputDataDev = acldvppGetPicDescData(img_info.pic_desc);
        acldvppFree(outputDataDev);
        acldvppDestroyPicDesc(img_info.pic_desc);
    }
}

void dvpp_imgList_release(vector<vpc_img_info>& imgList){
    for(int i=0; i < imgList.size(); i++){
        dvpp_img_release(imgList[i]);
    }
    imgList.clear();
}