1 /* 2 * Copyright (c) 2020-2021.Huawei Technologies Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DVPP_COMMON_H 17 #define DVPP_COMMON_H 18 19 #include "CommonDataType.h" 20 #include "ErrorCode.h" 21 #include "acl/ops/acl_dvpp.h" 22 23 const int MODULUS_NUM_2 = 2; 24 const uint32_t ODD_NUM_1 = 1; 25 26 struct Rect { 27 /* left location of the rectangle */ 28 uint32_t x; 29 /* top location of the rectangle */ 30 uint32_t y; 31 /* with of the rectangle */ 32 uint32_t width; 33 /* height of the rectangle */ 34 uint32_t height; 35 }; 36 37 struct DvppBaseData { 38 uint32_t dataSize; // Size of data in byte 39 uint8_t *data; 40 }; 41 42 struct VdecConfig { 43 int inputWidth = 0; 44 int inputHeight = 0; 45 acldvppStreamFormat inFormat = H264_MAIN_LEVEL; // stream format renference acldvppStreamFormat 46 acldvppPixelFormat outFormat = PIXEL_FORMAT_YUV_SEMIPLANAR_420; // output format renference acldvppPixelFormat 47 uint32_t channelId = 0; // user define channelId: 0-15 48 uint32_t deviceId = 0; 49 pthread_t threadId = 0; // thread for callback 50 aclvdecCallback callback = {0}; // user define how to process vdec out data 51 bool runflag = true; 52 }; 53 54 struct DeviceStreamData { 55 std::vector<ObjectDetectInfo> detectResult; 56 uint32_t framId; 57 uint32_t channelId; 58 }; 59 60 const uint32_t JPEGD_STRIDE_WIDTH = 128; // Jpegd module output width need to align up to 128 61 const uint32_t JPEGD_STRIDE_HEIGHT = 16; // Jpegd module output height need to align up to 16 62 const uint32_t PNGD_STRIDE_WIDTH = 128; // Pngd module output width need to align up to 128 63 const uint32_t PNGD_STRIDE_HEIGHT = 16; // Pngd module output height need to align up to 16 64 const uint32_t JPEGE_STRIDE_WIDTH = 16; // Jpege module input width need to align up to 16 65 const uint32_t JPEGE_STRIDE_HEIGHT = 1; // Jpege module input height remains unchanged 66 const uint32_t VPC_STRIDE_WIDTH = 16; // Vpc module output width need to align up to 16 67 const uint32_t VPC_STRIDE_HEIGHT = 2; // Vpc module output height need to align up to 2 68 const uint32_t VDEC_STRIDE_WIDTH = 16; // Vdec module output width need to align up to 16 69 const uint32_t VDEC_STRIDE_HEIGHT = 2; // Vdec module output width need to align up to 2 70 const uint32_t YUV_BYTES_NU = 3; // Numerator of yuv image, H x W x 3 / 2 71 const uint32_t YUV_BYTES_DE = 2; // Denominator of yuv image, H x W x 3 / 2 72 const uint32_t YUV422_WIDTH_NU = 2; // Width of YUV422, WidthStride = Width * 2 73 const uint32_t YUV444_RGB_WIDTH_NU = 3; // Width of YUV444 and RGB888, WidthStride = Width * 3 74 const uint32_t XRGB_WIDTH_NU = 4; // Width of XRGB8888, WidthStride = Width * 4 75 const uint32_t JPEG_OFFSET = 8; // Offset of input file for jpegd module 76 const uint32_t MAX_JPEGD_WIDTH = 8192; // Max width of jpegd module 77 const uint32_t MAX_JPEGD_HEIGHT = 8192; // Max height of jpegd module 78 const uint32_t MIN_JPEGD_WIDTH = 32; // Min width of jpegd module 79 const uint32_t MIN_JPEGD_HEIGHT = 32; // Min height of jpegd module 80 const uint32_t MAX_PNGD_WIDTH = 4096; // Max width of pngd module 81 const uint32_t MAX_PNGD_HEIGHT = 4096; // Max height of pngd module 82 const uint32_t MIN_PNGD_WIDTH = 32; // Min width of pngd module 83 const uint32_t MIN_PNGD_HEIGHT = 32; // Min height of pngd module 84 const uint32_t MAX_JPEGE_WIDTH = 8192; // Max width of jpege module 85 const uint32_t MAX_JPEGE_HEIGHT = 8192; // Max height of jpege module 86 const uint32_t MIN_JPEGE_WIDTH = 32; // Min width of jpege module 87 const uint32_t MIN_JPEGE_HEIGHT = 32; // Min height of jpege module 88 const uint32_t MAX_RESIZE_WIDTH = 4096; // Max width stride of resize module 89 const uint32_t MAX_RESIZE_HEIGHT = 4096; // Max height stride of resize module 90 const uint32_t MIN_RESIZE_WIDTH = 32; // Min width stride of resize module 91 const uint32_t MIN_RESIZE_HEIGHT = 6; // Min height stride of resize module 92 const float MIN_RESIZE_SCALE = 0.03125; // Min resize scale of resize module 93 const float MAX_RESIZE_SCALE = 16.0; // Min resize scale of resize module 94 const uint32_t MAX_VPC_WIDTH = 4096; // Max width of picture to VPC(resize/crop) 95 const uint32_t MAX_VPC_HEIGHT = 4096; // Max height of picture to VPC(resize/crop) 96 const uint32_t MIN_VPC_WIDTH = 32; // Min width of picture to VPC(resize/crop) 97 const uint32_t MIN_VPC_HEIGHT = 6; // Min height of picture to VPC(resize/crop) 98 const uint32_t MIN_CROP_WIDTH = 10; // Min width of crop area 99 const uint32_t MIN_CROP_HEIGHT = 6; // Min height of crop area 100 const uint8_t YUV_GREYER_VALUE = 128; // Filling value of the resized YUV image 101 102 #define CONVERT_TO_ODD(NUM) (((NUM) % MODULUS_NUM_2 != 0) ? (NUM) : ((NUM)-1)) // Convert the input to odd num 103 #define CONVERT_TO_EVEN(NUM) (((NUM) % MODULUS_NUM_2 == 0) ? (NUM) : ((NUM)-1)) // Convert the input to even num 104 #define CHECK_ODD(num) ((num) % MODULUS_NUM_2 != 0) 105 #define CHECK_EVEN(num) ((num) % MODULUS_NUM_2 == 0) 106 #define RELEASE_DVPP_DATA(dvppDataPtr) \ 107 do { \ 108 APP_ERROR retMacro; \ 109 if (dvppDataPtr != nullptr) { \ 110 retMacro = acldvppFree(dvppDataPtr); \ 111 if (retMacro != APP_ERR_OK) { \ 112 MS_LOG(ERROR) << "Failed to free memory on dvpp, ret = " << retMacro << "."; \ 113 } \ 114 dvppDataPtr = nullptr; \ 115 } \ 116 } while (0); 117 118 class DvppCommon { 119 public: 120 explicit DvppCommon(aclrtContext dvppContext, aclrtStream dvppStream); 121 explicit DvppCommon(const VdecConfig &vdecConfig); // Need by vdec 122 ~DvppCommon(); 123 APP_ERROR Init(void); 124 APP_ERROR InitVdec(); // Needed by vdec 125 APP_ERROR DeInit(void); 126 127 static APP_ERROR GetVpcDataSize(uint32_t widthVpc, uint32_t heightVpc, acldvppPixelFormat format, uint32_t &vpcSize); 128 static APP_ERROR GetVpcInputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format, 129 uint32_t &widthStride, uint32_t &heightStride); 130 static APP_ERROR GetVpcOutputStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format, 131 uint32_t &widthStride, uint32_t &heightStride); 132 static void GetJpegDecodeStrideSize(uint32_t width, uint32_t height, uint32_t &widthStride, uint32_t &heightStride); 133 static void GetPngDecodeStrideSize(uint32_t width, uint32_t height, uint32_t &widthStride, uint32_t &heightStride, 134 acldvppPixelFormat format); 135 static APP_ERROR GetJpegImageInfo(const void *data, uint32_t dataSize, uint32_t &width, uint32_t &height, 136 int32_t &components); 137 static APP_ERROR GetPngImageInfo(const void *data, uint32_t dataSize, uint32_t &width, uint32_t &height, 138 int32_t &components); 139 static APP_ERROR GetJpegDecodeDataSize(const void *data, uint32_t dataSize, acldvppPixelFormat format, 140 uint32_t &decSize); 141 static APP_ERROR GetPngDecodeDataSize(const void *data, uint32_t dataSize, acldvppPixelFormat format, 142 uint32_t &decSize); 143 static APP_ERROR GetJpegEncodeStrideSize(std::shared_ptr<DvppDataInfo> &input); 144 static APP_ERROR SetEncodeLevel(uint32_t level, acldvppJpegeConfig &jpegeConfig); 145 static APP_ERROR GetVideoDecodeStrideSize(uint32_t width, uint32_t height, acldvppPixelFormat format, 146 uint32_t &widthStride, uint32_t &heightStride); 147 static APP_ERROR GetVideoDecodeDataSize(uint32_t width, uint32_t height, acldvppPixelFormat format, 148 uint32_t &vdecSize); 149 150 // The following interfaces can be called only when the DvppCommon object is initialized with Init 151 APP_ERROR VpcResize(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize, 152 VpcProcessType processType = VPC_PT_DEFAULT); 153 APP_ERROR VpcCrop(const DvppCropInputInfo &input, const DvppDataInfo &output, bool withSynchronize); 154 APP_ERROR JpegDecode(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize); 155 156 APP_ERROR PngDecode(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize); 157 158 APP_ERROR JpegEncode(DvppDataInfo &input, DvppDataInfo &output, acldvppJpegeConfig *jpegeConfig, 159 bool withSynchronize); 160 161 APP_ERROR GetJpegEncodeDataSize(DvppDataInfo &input, acldvppJpegeConfig *jpegeConfig, uint32_t &encSize); 162 163 // These functions started with "Combine" encapsulate the DVPP process together, malloc DVPP memory, 164 // transfer pictures from host to device, and then execute the DVPP operation. 165 // The caller needs to pay attention to the release of the memory alloced in these functions. 166 // You can call the ReleaseDvppBuffer function to release memory after use completely. 167 APP_ERROR CombineResizeProcess(DvppDataInfo &input, DvppDataInfo &output, bool withSynchronize, 168 VpcProcessType processType = VPC_PT_DEFAULT); 169 APP_ERROR CombineCropProcess(DvppCropInputInfo &input, DvppDataInfo &output, bool withSynchronize); 170 APP_ERROR CombineJpegdProcess(const RawData &imageInfo, acldvppPixelFormat format, bool withSynchronize); 171 172 APP_ERROR SinkCombineJpegdProcess(std::shared_ptr<DvppDataInfo> &input, std::shared_ptr<DvppDataInfo> &output, 173 bool withSynchronize); 174 APP_ERROR SinkCombinePngdProcess(std::shared_ptr<DvppDataInfo> &input, std::shared_ptr<DvppDataInfo> &output, 175 bool withSynchronize); 176 177 APP_ERROR CombineJpegeProcess(const RawData &imageInfo, uint32_t width, uint32_t height, acldvppPixelFormat format, 178 bool withSynchronize); 179 // New feature for PNG format image decode 180 APP_ERROR CombinePngdProcess(const RawData &imageInfo, acldvppPixelFormat format, bool withSynchronize); 181 182 // The following interface can be called only when the DvppCommon object is initialized with InitVdec 183 APP_ERROR CombineVdecProcess(std::shared_ptr<DvppDataInfo> data, void *userData); 184 185 // Get the private member variables which are assigned in the interfaces which are started with "Combine" 186 std::shared_ptr<DvppDataInfo> GetInputImage(); 187 std::shared_ptr<DvppDataInfo> GetDecodedImage(); 188 std::shared_ptr<DvppDataInfo> GetResizedImage(); 189 std::shared_ptr<DvppDataInfo> GetEncodedImage(); 190 std::shared_ptr<DvppDataInfo> GetCropedImage(); 191 192 // Transfer DvppDataInfo from host to device, aim at resize and crop 193 APP_ERROR TransferYuvDataH2D(const DvppDataInfo &imageinfo); 194 // Transfer RawData(image) from host to device, aim at decode 195 APP_ERROR TransferImageH2D(const RawData &imageInfo, const std::shared_ptr<DvppDataInfo> &jpegInput); 196 // Transfer RawData(image on host) to inputImage_(data on device), this is for data sink mode 197 APP_ERROR SinkImageH2D(const RawData &imageInfo, acldvppPixelFormat format); 198 // This overload function is for PNG image sink, hence we ignore the pixel format 199 APP_ERROR SinkImageH2D(const RawData &imageInfo); 200 201 // Release the memory that is allocated in the interfaces which are started with "Combine" 202 void ReleaseDvppBuffer(); 203 APP_ERROR VdecSendEosFrame() const; 204 205 private: 206 APP_ERROR SetDvppPicDescData(const DvppDataInfo &dataInfo, acldvppPicDesc &picDesc); 207 APP_ERROR ResizeProcess(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, bool withSynchronize); 208 APP_ERROR ResizeWithPadding(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, CropRoiConfig &cropRoi, 209 CropRoiConfig &pasteRoi, bool withSynchronize); 210 void GetCropRoi(const DvppDataInfo &input, const DvppDataInfo &output, VpcProcessType processType, 211 CropRoiConfig &cropRoi); 212 void GetPasteRoi(const DvppDataInfo &input, const DvppDataInfo &output, VpcProcessType processType, 213 CropRoiConfig &pasteRoi); 214 APP_ERROR CropProcess(acldvppPicDesc &inputDesc, acldvppPicDesc &outputDesc, const CropRoiConfig &cropArea, 215 bool withSynchronize); 216 APP_ERROR CheckResizeParams(const DvppDataInfo &input, const DvppDataInfo &output); 217 APP_ERROR CheckCropParams(const DvppCropInputInfo &input); 218 APP_ERROR CreateStreamDesc(std::shared_ptr<DvppDataInfo> data); 219 APP_ERROR DestroyResource(); 220 221 std::shared_ptr<acldvppRoiConfig> cropAreaConfig_ = nullptr; 222 std::shared_ptr<acldvppRoiConfig> pasteAreaConfig_ = nullptr; 223 224 std::shared_ptr<acldvppPicDesc> cropInputDesc_ = nullptr; 225 std::shared_ptr<acldvppPicDesc> cropOutputDesc_ = nullptr; 226 std::shared_ptr<acldvppRoiConfig> cropRoiConfig_ = nullptr; 227 228 std::shared_ptr<acldvppPicDesc> encodeInputDesc_ = nullptr; 229 std::shared_ptr<acldvppJpegeConfig> jpegeConfig_ = nullptr; 230 231 std::shared_ptr<acldvppPicDesc> resizeInputDesc_ = nullptr; 232 std::shared_ptr<acldvppPicDesc> resizeOutputDesc_ = nullptr; 233 std::shared_ptr<acldvppResizeConfig> resizeConfig_ = nullptr; 234 235 std::shared_ptr<acldvppPicDesc> decodeOutputDesc_ = nullptr; 236 237 acldvppChannelDesc *dvppChannelDesc_ = nullptr; 238 239 // Ascend resource core: (context, stream) is a pair so must bind with each other to make all function perform well 240 aclrtContext dvppContext_ = nullptr; 241 aclrtStream dvppStream_ = nullptr; 242 243 std::shared_ptr<DvppDataInfo> inputImage_ = nullptr; 244 std::shared_ptr<DvppDataInfo> decodedImage_ = nullptr; 245 std::shared_ptr<DvppDataInfo> encodedImage_ = nullptr; 246 std::shared_ptr<DvppDataInfo> resizedImage_ = nullptr; 247 std::shared_ptr<DvppDataInfo> cropImage_ = nullptr; 248 bool isVdec_ = false; 249 aclvdecChannelDesc *vdecChannelDesc_ = nullptr; 250 acldvppStreamDesc *streamInputDesc_ = nullptr; 251 acldvppPicDesc *picOutputDesc_ = nullptr; 252 VdecConfig vdecConfig_; 253 }; 254 #endif 255