diff --git a/src/util/JpegUtil.cpp b/src/util/JpegUtil.cpp index 2f1e253..35e465c 100755 --- a/src/util/JpegUtil.cpp +++ b/src/util/JpegUtil.cpp @@ -6,39 +6,48 @@ using namespace std; +#define ACL_CHECK_AND_ABORT(ret, message) \ + if(ret != 0) {LOG_ERROR("{}", message); std::abort();} int JpegUtil::jpeg_init(int32_t devId){ deviceId_ = devId; - aclError ret; - /* 2.Run the management resource application, including Device, Context, Stream */ - aclrtCreateContext(&context_, deviceId_); + ACL_CHECK_AND_ABORT(aclrtCreateContext(&context_, deviceId_), "aclrtCreateContext failed!"); // channel 准备 dvppChannelDesc_ = acldvppCreateChannelDesc(); - ret = acldvppCreateChannel(dvppChannelDesc_); + if (dvppChannelDesc_ == nullptr) + { + LOG_ERROR("acldvppCreateChannelDesc failed!"); + std::abort(); + } + + ACL_CHECK_AND_ABORT(acldvppCreateChannel(dvppChannelDesc_), "acldvppCreateChannel failed!"); // 创建图片编码配置数据,设置编码质量 // 编码质量范围[0, 100],其中level 0编码质量与level 100差不多,而在[1, 100]内数值越小输出图片质量越差。 jpegeConfig_ = acldvppCreateJpegeConfig(); - acldvppSetJpegeConfigLevel(jpegeConfig_, 100); + if (jpegeConfig_ == nullptr) + { + LOG_ERROR("acldvppCreateJpegeConfig failed!"); + std::abort(); + } + ACL_CHECK_AND_ABORT(acldvppSetJpegeConfigLevel(jpegeConfig_, 100), "acldvppSetJpegeConfigLevel failed!"); } void JpegUtil::jpeg_release(){ - aclError ret; - aclrtSetCurrentContext(context_); - ret = acldvppDestroyChannel(dvppChannelDesc_); - ret = acldvppDestroyChannelDesc(dvppChannelDesc_); + ACL_CHECK_AND_ABORT(aclrtSetCurrentContext(context_), "aclrtSetCurrentContext failed!"); + + ACL_CHECK_AND_ABORT(acldvppDestroyChannel(dvppChannelDesc_), "acldvppDestroyChannel failed!"); + ACL_CHECK_AND_ABORT(acldvppDestroyChannelDesc(dvppChannelDesc_), "acldvppDestroyChannelDesc failed!"); + dvppChannelDesc_ = nullptr; acldvppDestroyJpegeConfig(jpegeConfig_); if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("destroy context failed"); - } + ACL_CHECK_AND_ABORT(aclrtDestroyContext(context_), "aclrtDestroyContext failed!"); context_ = nullptr; } } @@ -61,38 +70,23 @@ int32_t JpegUtil::jpege_save(char* pcData , uint32_t dataLen, string out_file_na bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name) { - aclError aclRet ; - aclrtSetCurrentContext(context_); + ACL_CHECK_AND_ABORT(aclrtSetCurrentContext(context_), "aclrtSetCurrentContext failed!"); // 8. 申请输出内存,申请Device内存encodeOutBufferDev_,存放编码后的输出数据 uint32_t outBufferSize= 0; - int ret = acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize); - if (ret != ACL_SUCCESS) { - LOG_ERROR("acldvppJpegPredictEncSize failed!"); - return false; - } + ACL_CHECK_AND_ABORT(acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize), "acldvppJpegPredictEncSize failed!"); + int ret ; + void *encodeOutBufferDev_ = nullptr; - ret = acldvppMalloc(&encodeOutBufferDev_, outBufferSize); - if (ret != ACL_SUCCESS) { - LOG_ERROR("acldvppMalloc failed!"); - return false; - } + ACL_CHECK_AND_ABORT(acldvppMalloc(&encodeOutBufferDev_, outBufferSize), "acldvppJpegPredictEncSize failed!"); bool bRet = false; aclrtStream stream_{nullptr}; aclrtCreateStream(&stream_); do { // 9. 执行异步编码,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 - aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("acldvppJpegEncodeAsync failed!"); - break; - } - aclRet = aclrtSynchronizeStream(stream_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("aclrtSynchronizeStream failed!"); - break; - } + ACL_CHECK_AND_ABORT(acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_), "acldvppJpegEncodeAsync failed!"); + ACL_CHECK_AND_ABORT(aclrtSynchronizeStream(stream_), "aclrtSynchronizeStream failed!"); // 申请Host内存outputHostBuffer void* outputHostBuffer = malloc(outBufferSize); @@ -102,13 +96,7 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam } // 通过aclrtMemcpy接口将Device的处理结果数据传输到Host - aclRet = aclrtMemcpy(outputHostBuffer, outBufferSize, encodeOutBufferDev_, outBufferSize, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_SUCCESS) { - free(outputHostBuffer); - outputHostBuffer = nullptr; - LOG_ERROR("aclrtMemcpy failed!"); - break; - } + ACL_CHECK_AND_ABORT(aclrtMemcpy(outputHostBuffer, outBufferSize, encodeOutBufferDev_, outBufferSize, ACL_MEMCPY_DEVICE_TO_HOST), "aclrtMemcpy failed!"); // 数据使用完成后,释放内存 ret = jpege_save((char*)outputHostBuffer, outBufferSize, out_file_name); @@ -123,10 +111,7 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam } while (0); if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("destroy stream failed"); - } + ACL_CHECK_AND_ABORT(aclrtDestroyStream(stream_), "aclrtDestroyStream failed!"); stream_ = nullptr; } diff --git a/src/util/vpc_util.cpp b/src/util/vpc_util.cpp index a67655a..1e7bcb0 100755 --- a/src/util/vpc_util.cpp +++ b/src/util/vpc_util.cpp @@ -18,6 +18,9 @@ #include "../decoder/interface/DeviceMemory.hpp" #include "../common/logger.hpp" +#define ACL_CHECK_AND_ABORT(ret, message) \ + if(ret != 0) {LOG_ERROR("{}", message); std::abort();} + static uint32_t AlignSize(uint32_t origSize, uint32_t alignment){ if (alignment == 0) { return 0; @@ -29,37 +32,39 @@ static uint32_t AlignSize(uint32_t origSize, uint32_t alignment){ void VPCUtil::release() { if (context_ != nullptr) { - aclrtSetCurrentContext(context_); + ACL_CHECK_AND_ABORT(aclrtSetCurrentContext(context_), "aclrtSetCurrentContext failed !"); - aclError ret = acldvppDestroyChannel(dvppChannelDesc_); - ret = acldvppDestroyChannelDesc(dvppChannelDesc_); + ACL_CHECK_AND_ABORT(acldvppDestroyChannel(dvppChannelDesc_), "acldvppDestroyChannel failed !"); + ACL_CHECK_AND_ABORT(acldvppDestroyChannelDesc(dvppChannelDesc_), "acldvppDestroyChannelDesc failed !"); if (stream_ != nullptr) { - aclrtDestroyStream(stream_); + ACL_CHECK_AND_ABORT(aclrtDestroyStream(stream_), "acldvppDestroyChannelDesc failed !"); stream_ = nullptr; } - ret = aclrtDestroyContext(context_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("destroy context failed"); - } + ACL_CHECK_AND_ABORT(aclrtDestroyContext(context_), "aclrtDestroyContext failed !"); + context_ = nullptr; } - LOG_INFO("end to reset device is %d", deviceId_); + LOG_INFO("end to reset device is {}", deviceId_); } int VPCUtil::init(int32_t devId){ deviceId_ = devId; - aclError ret; - aclrtCreateContext(&context_, deviceId_); - - aclrtCreateStream(&stream_); + ACL_CHECK_AND_ABORT(aclrtCreateContext(&context_, deviceId_), "aclrtCreateContext failed !"); + ACL_CHECK_AND_ABORT(aclrtCreateStream(&stream_), "aclrtCreateStream failed !"); // channel 准备 dvppChannelDesc_ = acldvppCreateChannelDesc(); - ret = acldvppCreateChannel(dvppChannelDesc_); + if (dvppChannelDesc_ == nullptr) + { + LOG_INFO("acldvppCreateChannelDesc failed!"); + std::abort(); + } + + ACL_CHECK_AND_ABORT(acldvppCreateChannel(dvppChannelDesc_), "acldvppCreateChannel failed !"); } static void adjustCoordinate(uint32_t& left, uint32_t& right, uint32_t& top, uint32_t& bottom, const uint32_t& width, const uint32_t& height) { @@ -119,11 +124,7 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { } aclError ret; - ret = aclrtSetCurrentContext(context_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("aclrtSetCurrentContext failed"); - return img_info; - } + ACL_CHECK_AND_ABORT(aclrtSetCurrentContext(context_), "aclrtSetCurrentContext failed !"); // LOG_INFO("crop src {} ({}, {}, {}, {})", obj.object_id, obj.left, obj.right, obj.top, obj.bottom); // LOG_INFO("crop {} ({}, {}, {}, {})", obj.object_id, cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); @@ -204,7 +205,7 @@ vector VPCUtil::crop_batch(DeviceMemory *devMem, vector