diff --git a/src/util/JpegUtil.cpp b/src/util/JpegUtil.cpp index 396a24a..aaf8c6b 100755 --- a/src/util/JpegUtil.cpp +++ b/src/util/JpegUtil.cpp @@ -64,7 +64,7 @@ int32_t JpegUtil::jpege_save(char* pcData , uint32_t dataLen, string out_file_na FILE* fd = nullptr; fd = fopen(out_file_name.c_str(), "wb"); if (fd == nullptr) { - LOG_ERROR("open output file err"); + LOG_ERROR("open output file error"); return 1; } @@ -85,11 +85,13 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam uint32_t outBufferSize= 0; int ret = acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize); if (ret != ACL_SUCCESS) { + LOG_ERROR("acldvppJpegPredictEncSize failed!"); return false; } void *encodeOutBufferDev_ = nullptr; ret = acldvppMalloc(&encodeOutBufferDev_, outBufferSize); if (ret != ACL_SUCCESS) { + LOG_ERROR("acldvppMalloc failed!"); return false; } @@ -98,29 +100,37 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam // 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; } // 申请Host内存outputHostBuffer void* outputHostBuffer = malloc(outBufferSize); + if(outputHostBuffer == nullptr) { + LOG_ERROR("malloc failed!"); + break; + } // 通过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; } // 数据使用完成后,释放内存 ret = jpege_save((char*)outputHostBuffer, outBufferSize, out_file_name); + free(outputHostBuffer); + outputHostBuffer = nullptr; if(ret != 0) { - free(outputHostBuffer); - outputHostBuffer = nullptr; + LOG_ERROR("jpege_save failed!"); break; } diff --git a/src/util/vpc_util.cpp b/src/util/vpc_util.cpp index 06360eb..0a4adaf 100755 --- a/src/util/vpc_util.cpp +++ b/src/util/vpc_util.cpp @@ -18,6 +18,14 @@ #include "../decoder/interface/DeviceMemory.hpp" #include "../common/logger.hpp" +static uint32_t AlignSize(uint32_t origSize, uint32_t alignment){ + if (alignment == 0) { + return 0; + } + uint32_t alignmentH = alignment - 1; + return (origSize + alignmentH) / alignment * alignment; +} + void VPCUtil::release() { aclError ret; @@ -43,9 +51,39 @@ void VPCUtil::release() LOG_INFO("end to reset device is %d", deviceId_); } +static void adjustCoordinate(uint32_t& left, uint32_t& right, uint32_t& top, uint32_t& bottom, const uint32_t& width, const uint32_t& height) { + uint32_t cropWidth = right - left; + uint32_t cropHeight = bottom - top; + + if(cropWidth < 16) { + cropWidth += 16; //小于16的似乎抠不了图 + right += 16; + } + + if (left < 0) { + left = 0; + right = cropWidth; + } + + if (right >= width){ + right = width -1; + left = right - cropWidth; + } + + if (top < 0) { + top = 0; + bottom = cropHeight; + } + + if (bottom >= height){ + bottom = height -1; + top = bottom - cropHeight; + } +} + vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { - vpc_img_info img_info ; + vpc_img_info img_info; // uint32_t cropSizeWidth = (obj.right - obj.left) / 16 * 16; // uint32_t cropSizeHeight = (obj.bottom - obj.top) / 2 * 2; @@ -59,7 +97,21 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { uint32_t cropTopOffset = (obj.top + 1) / 2 * 2; // must even uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd + adjustCoordinate(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset, devMem->getWidth(), devMem->getHeight()); + cropSizeWidth = cropRightOffset - cropLeftOffset + oddNum; // 宽高要比实际的高才行,否则会抠图失败 + cropSizeHeight = cropBottomOffset - cropTopOffset + oddNum; + LOG_DEBUG("{} ,{} ,{} ,{} ", cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); + if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){ + LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset); + return img_info; + } + + aclError ret; + aclrtSetDevice(deviceId_); + ret = aclrtSetCurrentContext(context_); + if (ret != ACL_SUCCESS) { + LOG_ERROR("aclrtSetCurrentContext failed"); return img_info; } @@ -94,7 +146,7 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { bool bRet = false; do { - aclError ret = acldvppVpcCropAsync(dvppChannelDesc_, vpcInputDesc_, vpcOutputDesc_, cropArea_, stream_); + ret = acldvppVpcCropAsync(dvppChannelDesc_, vpcInputDesc_, vpcOutputDesc_, cropArea_, stream_); if (ret != ACL_SUCCESS) { LOG_ERROR("acldvppVpcCropAsync failed, task_id : {}", devMem->getId()); break; @@ -148,7 +200,6 @@ int VPCUtil::init(int32_t devId){ // channel 准备 dvppChannelDesc_ = acldvppCreateChannelDesc(); ret = acldvppCreateChannel(dvppChannelDesc_); - } vector VPCUtil::crop_batch(DeviceMemory *devMem, vector objs){ @@ -192,10 +243,10 @@ vector VPCUtil::crop_batch(DeviceMemory *devMem, vector VPCUtil::crop_batch(DeviceMemory *devMem, vectorgetWidth(), devMem->getHeight()); + cropSizeWidth = cropRightOffset - cropLeftOffset + oddNum; + cropSizeHeight = cropBottomOffset - cropTopOffset + oddNum; + LOG_DEBUG("{} ,{} ,{} ,{} ", cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); + if(cropRightOffset <= cropLeftOffset || cropBottomOffset <= cropTopOffset){ LOG_ERROR("{} <= {} || {} <= {} ", cropRightOffset, cropLeftOffset, cropBottomOffset, cropTopOffset); // 释放之前成功的部分 再退出