From 463c89b757ed42857a29f1d26e9848ae713f77d1 Mon Sep 17 00:00:00 2001 From: maohongchao <1506472071@qq.com> Date: Tue, 19 Jul 2022 17:59:50 +0800 Subject: [PATCH] cleancode --- .../0_data_process/jpegd/src/main.cpp | 183 ++++++++++-------- .../0_data_process/jpegd/src/main.h | 8 +- 2 files changed, 106 insertions(+), 85 deletions(-) diff --git a/cplusplus/level2_simple_inference/0_data_process/jpegd/src/main.cpp b/cplusplus/level2_simple_inference/0_data_process/jpegd/src/main.cpp index 5a285cf39..524a01fd5 100644 --- a/cplusplus/level2_simple_inference/0_data_process/jpegd/src/main.cpp +++ b/cplusplus/level2_simple_inference/0_data_process/jpegd/src/main.cpp @@ -1,5 +1,5 @@ /** -* Copyright 2020 Huawei Technologies Co., Ltd +* Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,16 +15,41 @@ */ #include -#include "main.h" -#include "acl/acl.h" #include -#include #include #include +#include +#include "acl/acl.h" #include "acl/ops/acl_dvpp.h" +#include "main.h" + using namespace std; +constexpr int ARGC_NUM = 2; + +void* MallocInputBuffer(const uint32_t &inputBuffSize, char* inputBuff) +{ + void* inBufferDev = nullptr; + aclError ret = acldvppMalloc(&inBufferDev, inputBuffSize); + if (ret != ACL_SUCCESS) { + ERROR_LOG("malloc device data buffer failed, aclRet is %d", ret); + return nullptr; + } + + if (runMode == ACL_HOST) { + ret = aclrtMemcpy(inBufferDev, inputBuffSize, inputBuff, inputBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); + } else { + ret = aclrtMemcpy(inBufferDev, inputBuffSize, inputBuff, inputBuffSize, ACL_MEMCPY_DEVICE_TO_DEVICE); + } + if (ret != ACL_SUCCESS) { + ERROR_LOG("memcpy failed. Input host buffer size is %u", inputBuffSize); + acldvppFree(inBufferDev); + return nullptr; + } + return inBufferDev; +} + void* GetDeviceBufferOfPicture(PicDesc &picDesc, uint32_t &devPicBufferSize) { if (picDesc.picName.empty()) { @@ -48,62 +73,43 @@ void* GetDeviceBufferOfPicture(PicDesc &picDesc, uint32_t &devPicBufferSize) size_t readSize = fread(inputBuff, sizeof(char), inputBuffSize, fp); if (readSize < inputBuffSize) { ERROR_LOG("need read file %s %u bytes, but only %zu readed", - picDesc.picName.c_str(), inputBuffSize, readSize); + picDesc.picName.c_str(), inputBuffSize, readSize); delete[] inputBuff; - fclose(fp); + fclose(fp); return nullptr; } + fclose(fp); acldvppJpegFormat format; - aclError aclRet = acldvppJpegGetImageInfoV2(inputBuff, inputBuffSize, &picDesc.width, &picDesc.height, nullptr, &format); if (aclRet != ACL_SUCCESS) { ERROR_LOG("get jpeg image info failed, errorCode is %d", static_cast(aclRet)); delete[] inputBuff; - fclose(fp); return nullptr; } - INFO_LOG("get jpeg image info successed, width=%d, height=%d, format=%d, jpegDecodeSize=%d", picDesc.width, picDesc.height, format, picDesc.jpegDecodeSize); + INFO_LOG("get jpeg image info successed, width=%d, height=%d, format=%d, jpegDecodeSize=%d", + picDesc.width, picDesc.height, format, picDesc.jpegDecodeSize); - // when you run, from the output, we can see that the original format is ACL_JPEG_CSS_420, + // when you run, from the output, we can see that the original format is ACL_JPEG_CSS_420, // so it can be decoded as PIXEL_FORMAT_YUV_SEMIPLANAR_420 or PIXEL_FORMAT_YVU_SEMIPLANAR_420 - aclRet = acldvppJpegPredictDecSize(inputBuff, inputBuffSize, PIXEL_FORMAT_YUV_SEMIPLANAR_420, &picDesc.jpegDecodeSize); + aclRet = acldvppJpegPredictDecSize(inputBuff, inputBuffSize, PIXEL_FORMAT_YUV_SEMIPLANAR_420, + &picDesc.jpegDecodeSize); if (aclRet != ACL_SUCCESS) { ERROR_LOG("get jpeg decode size failed, errorCode is %d", static_cast(aclRet)); delete[] inputBuff; - fclose(fp); - return nullptr; - } - - void *inBufferDev = nullptr; - aclError ret = acldvppMalloc(&inBufferDev, inputBuffSize); - if (ret != ACL_SUCCESS) { - delete[] inputBuff; - ERROR_LOG("malloc device data buffer failed, aclRet is %d", ret); - fclose(fp); return nullptr; } - if (runMode == ACL_HOST) { - ret = aclrtMemcpy(inBufferDev, inputBuffSize, inputBuff, inputBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - } - else { - ret = aclrtMemcpy(inBufferDev, inputBuffSize, inputBuff, inputBuffSize, ACL_MEMCPY_DEVICE_TO_DEVICE); - } - if (ret != ACL_SUCCESS) { - ERROR_LOG("memcpy failed. Input host buffer size is %u", - inputBuffSize); - acldvppFree(inBufferDev); + void *inBufferDev = MallocInputBuffer(inputBuffSize, inputBuff); + if (inBufferDev == nullptr) { delete[] inputBuff; - fclose(fp); return nullptr; } delete[] inputBuff; devPicBufferSize = inputBuffSize; - fclose(fp); return inBufferDev; } @@ -127,7 +133,7 @@ Result SaveDvppOutputData(const char *fileName, const void *devPtr, uint32_t dat aclError aclRet = aclrtMallocHost(&hostPtr, dataSize); if (aclRet != ACL_SUCCESS) { ERROR_LOG("malloc host data buffer failed, aclRet is %d", aclRet); - fclose(outFileFp); + fclose(outFileFp); return FAILED; } @@ -135,25 +141,24 @@ Result SaveDvppOutputData(const char *fileName, const void *devPtr, uint32_t dat if (aclRet != ACL_SUCCESS) { ERROR_LOG("dvpp output memcpy to host failed, aclRet is %d", aclRet); (void)aclrtFreeHost(hostPtr); - fclose(outFileFp); + fclose(outFileFp); return FAILED; } size_t writeSize = fwrite(hostPtr, sizeof(char), dataSize, outFileFp); if (writeSize != dataSize) { ERROR_LOG("need write %u bytes to %s, but only write %zu bytes.", - dataSize, fileName, writeSize); + dataSize, fileName, writeSize); (void)aclrtFreeHost(hostPtr); - fclose(outFileFp); + fclose(outFileFp); return FAILED; } (void)aclrtFreeHost(hostPtr); - } - else { + } else { size_t writeSize = fwrite(devPtr, sizeof(char), dataSize, outFileFp); if (writeSize != dataSize) { ERROR_LOG("need write %u bytes to %s, but only write %zu bytes.", - dataSize, fileName, writeSize); - fclose(outFileFp); + dataSize, fileName, writeSize); + fclose(outFileFp); return FAILED; } } @@ -166,7 +171,7 @@ void DestroyResource() { aclError ret; inDevBuffer_ = nullptr; - if (decodeOutputDesc_ != nullptr) { + if (decodeOutputDesc_ != nullptr) { acldvppDestroyPicDesc(decodeOutputDesc_); decodeOutputDesc_ = nullptr; } @@ -202,20 +207,13 @@ void DestroyResource() INFO_LOG("end to finalize acl"); } -int main(int argc, char *argv[]) { - - if((argc < 2) || (argv[1] == nullptr)){ - ERROR_LOG("Please input: ./main "); - return FAILED; - } - string image_path = string(argv[1]); - - - //1.acl init +void InitResource() +{ + // 1.acl init const char *aclConfigPath = "../src/acl.json"; aclInit(aclConfigPath); INFO_LOG("acl init success"); - //2.create Device,Context,Stream + // 2.create Device,Context,Stream aclrtSetDevice(deviceId_); INFO_LOG("open device %d success", deviceId_); // create context (set current) @@ -224,36 +222,15 @@ int main(int argc, char *argv[]) { aclrtCreateStream(&stream_); aclrtGetRunMode(&runMode); INFO_LOG("create stream success"); - - uint32_t image_width = 0; - uint32_t image_height = 0; - PicDesc testPic = {image_path, image_width, image_height}; - - INFO_LOG("start to process picture:%s", testPic.picName.c_str()); - // dvpp process - /*3.Read the picture into memory. InDevBuffer_ indicates the memory for storing the input picture, - inDevBufferSize indicates the memory size, please apply for the input memory in advance*/ - uint32_t devPicBufferSize; - void *picDevBuffer = GetDeviceBufferOfPicture(testPic, devPicBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed,index is 0"); - return FAILED; - } - //4.Create image data processing channel - dvppChannelDesc_ = acldvppCreateChannelDesc(); - acldvppCreateChannel(dvppChannelDesc_); - INFO_LOG("dvpp init resource success"); - - - SetInput(picDevBuffer, devPicBufferSize, testPic.width, testPic.height); +} +Result InitDecodeOutputDesc(const PicDesc &testPic) +{ // InitDecodeOutputDesc uint32_t decodeOutWidthStride = (inputWidth_ + 127) / 128 * 128; // 128-byte alignment uint32_t decodeOutHeightStride = (inputHeight_ + 15) / 16 * 16; // 16-byte alignment // use acldvppJpegPredictDecSize to get output size. - // uint32_t decodeOutBufferSize = decodeOutWidthStride * decodeOutHeightStride * 3 / 2; // yuv format size - // uint32_t decodeOutBufferSize = testPic.jpegDecodeSize; aclError ret = acldvppMalloc(&decodeOutDevBuffer_, testPic.jpegDecodeSize); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc jpegOutBufferDev failed, ret = %d", ret); @@ -268,7 +245,7 @@ int main(int argc, char *argv[]) { acldvppSetPicDescData(decodeOutputDesc_, decodeOutDevBuffer_); // here the format shoud be same with the value you set when you get decodeOutBufferSize from - acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); acldvppSetPicDescWidth(decodeOutputDesc_, inputWidth_); acldvppSetPicDescHeight(decodeOutputDesc_, inputHeight_); acldvppSetPicDescWidthStride(decodeOutputDesc_, decodeOutWidthStride); @@ -289,17 +266,57 @@ int main(int argc, char *argv[]) { } decodeDataSize_ = acldvppGetPicDescSize(decodeOutputDesc_); + return SUCCESS; +} + +int main(int argc, char *argv[]) +{ + if ((argc < ARGC_NUM) || (argv[1] == nullptr)) { + ERROR_LOG("Please input: ./main "); + return FAILED; + } + InitResource(); + + string image_path = string(argv[1]); + uint32_t image_width = 0; + uint32_t image_height = 0; + PicDesc testPic = {image_path, image_width, image_height}; + + INFO_LOG("start to process picture:%s", testPic.picName.c_str()); + + // dvpp process + // 3.Read the picture into memory. InDevBuffer_ indicates the memory for storing the input picture, + // inDevBufferSize indicates the memory size, please apply for the input memory in advance + uint32_t devPicBufferSize; + void *picDevBuffer = GetDeviceBufferOfPicture(testPic, devPicBufferSize); + if (picDevBuffer == nullptr) { + ERROR_LOG("get pic device buffer failed,index is 0"); + return FAILED; + } + // 4.Create image data processing channel + dvppChannelDesc_ = acldvppCreateChannelDesc(); + acldvppCreateChannel(dvppChannelDesc_); + INFO_LOG("dvpp init resource success"); + + SetInput(picDevBuffer, devPicBufferSize, testPic.width, testPic.height); + + aclError ret = InitDecodeOutputDesc(testPic); + if (ret != SUCCESS) { + ERROR_LOG("save dvpp output data failed"); + return FAILED; + } (void)acldvppFree(picDevBuffer); picDevBuffer = nullptr; - int dir_tail_index = image_path.find("/data"); - std::string outfile_dir = image_path.substr(0, dir_tail_index) + "/" + "out/output/"; - std::string outfile_path = outfile_dir + image_path.substr(dir_tail_index+5+1, image_path.rfind(".jpg")-dir_tail_index-5-1) - + "_jpegd_" + std::to_string(testPic.width) + "_" + std::to_string(testPic.height) + ".yuv"; + int dirTailIndex = image_path.find("/data"); + std::string outfile_dir = image_path.substr(0, dirTailIndex) + "/" + "out/output/"; + std::string outfile_path = outfile_dir + + image_path.substr(dirTailIndex+5+1, image_path.rfind(".jpg")-dirTailIndex-5-1) + + "_jpegd_" + std::to_string(testPic.width) + "_" + + std::to_string(testPic.height) + ".yuv"; INFO_LOG("outfile_path=%s", outfile_path.c_str()); - ret = SaveDvppOutputData(outfile_path.c_str(), decodeOutDevBuffer_, decodeDataSize_); if (ret != SUCCESS) { ERROR_LOG("save dvpp output data failed"); diff --git a/cplusplus/level2_simple_inference/0_data_process/jpegd/src/main.h b/cplusplus/level2_simple_inference/0_data_process/jpegd/src/main.h index 7ed232f87..85605b45c 100644 --- a/cplusplus/level2_simple_inference/0_data_process/jpegd/src/main.h +++ b/cplusplus/level2_simple_inference/0_data_process/jpegd/src/main.h @@ -1,5 +1,5 @@ /** -* Copyright 2020 Huawei Technologies Co., Ltd +* Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef VOS_INCLUDE_TIMER_H +#define VOS_INCLUDE_TIMER_H #pragma once -#include "acl/acl.h" #include #include +#include "acl/acl.h" #include "acl/ops/acl_dvpp.h" #define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) @@ -59,3 +61,5 @@ uint32_t inDevBufferSize_; // dvpp input buffer size uint32_t inputWidth_; // input pic width uint32_t inputHeight_; // input pic height aclrtRunMode runMode; + +#endif -- Gitee