1 /** 2 * Copyright 2022-2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 8 * http://www.apache.org/licenses/LICENSE-2.0 9 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_UTILS_ACL_LITE_UTILS_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_UTILS_ACL_LITE_UTILS_H_ 18 19 #include <unistd.h> 20 21 #include <chrono> 22 #include <iostream> 23 #include <map> 24 #include <memory> 25 #include <mutex> 26 #include <string> 27 #include <vector> 28 29 #include "acl/ops/acl_dvpp.h" 30 31 #include "minddata/dataset/kernels/image/dvpp/utils/AclLiteError.h" 32 #include "minddata/dataset/kernels/image/dvpp/utils/AclLiteType.h" 33 #include "transform/symbol/acl_rt_symbol.h" 34 #include "transform/symbol/symbol_utils.h" 35 36 /** 37 * @brief calculate RGB 24bits image size 38 * @param [in]: width: image width 39 * @param [in]: height: image height 40 * @return bytes size of image 41 */ 42 #define RGBU8_IMAGE_SIZE(width, height) ((width) * (height)*3) 43 44 /** 45 * @brief calculate RGB C3F32 image size 46 * @param [in]: width: image width 47 * @param [in]: height: image height 48 * @return bytes size of image 49 */ 50 #define RGBF32_IMAGE_SIZE(width, height) ((width) * (height)*3 * sizeof(float)) 51 52 /** 53 * @brief calculate YUVSP420 image size 54 * @param [in]: width: image width 55 * @param [in]: height: image height 56 * @return bytes size of image 57 */ 58 #define YUV420SP_SIZE(width, height) ((width) * (height)*3 / 2) 59 60 /** 61 * @brief calculate YUVSP420 nv12 load to opencv mat height paramter 62 * @param [in]: height: yuv image height 63 * @return bytes size of image 64 */ 65 #define YUV420SP_CV_MAT_HEIGHT(height) ((height)*3 / 2) 66 67 /** 68 * @brief generate shared pointer of dvpp memory 69 * @param [in]: buf: memory pointer, malloc by acldvppMalloc 70 * @return shared pointer of input buffer 71 */ 72 #define SHARED_PTR_DVPP_BUF(buf) \ 73 (std::shared_ptr<uint8_t>(reinterpret_cast<uint8_t *>(buf), [](uint8_t *p) { acldvppFree(p); })) 74 75 /** 76 * @brief generate shared pointer of device memory 77 * @param [in]: buf: memory pointer, malloc by acldvppMalloc 78 * @return shared pointer of input buffer 79 */ 80 #define SHARED_PTR_DEV_BUF(buf) \ 81 (std::shared_ptr<uint8_t>(reinterpret_cast<uint8_t *>(buf), [](uint8_t *p) { CALL_ASCEND_API(aclrtFree, p); })) 82 83 /** 84 * @brief generate shared pointer of memory 85 * @param [in]: buf memory pointer, malloc by new 86 * @return shared pointer of input buffer 87 */ 88 #define SHARED_PTR_U8_BUF(buf) \ 89 (std::shared_ptr<uint8_t>(reinterpret_cast<uint8_t *>(buf), [](uint8_t *p) { delete[](p); })) 90 91 /** 92 * @brief calculate aligned number 93 * @param [in]: num: the original number that to aligned 94 * @param [in]: align: the align factor 95 * @return the number after aligned 96 */ 97 #define ALIGN_UP(num, align) (((num) + (align)-1) & ~((align)-1)) 98 99 /** 100 * @brief calculate number align with 2 101 * @param [in]: num: the original number that to aligned 102 * @return the number after aligned 103 */ 104 #define ALIGN_UP2(num) ALIGN_UP(num, 2) 105 106 /** 107 * @brief calculate number align with 16 108 * @param [in]: num: the original number that to aligned 109 * @return the number after aligned 110 */ 111 #define ALIGN_UP16(num) ALIGN_UP(num, 16) 112 113 /** 114 * @brief calculate number align with 128 115 * @param [in]: num: the original number that to aligned 116 * @return the number after aligned 117 */ 118 #define ALIGN_UP128(num) ALIGN_UP(num, 128) 119 120 /** 121 * @brief calculate elements num of array 122 * @param [in]: array: the array variable 123 * @return elements num of array 124 */ 125 #define SIZEOF_ARRAY(array) (sizeof(array) / sizeof(array[0])) 126 127 /** 128 * @brief Write acl error level log to host log 129 * @param [in]: fmt: the input format string 130 * @return none 131 */ 132 #define ACLLITE_LOG_ERROR(fmt, ...) \ 133 do { \ 134 aclAppLog(ACL_ERROR, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ 135 fprintf(stdout, "[ERROR] " fmt "\n", ##__VA_ARGS__); \ 136 } while (0) 137 138 /** 139 * @brief Write acl info level log to host log 140 * @param [in]: fmt: the input format string 141 * @return none 142 */ 143 #define ACLLITE_LOG_INFO(fmt, ...) \ 144 do { \ 145 aclAppLog(ACL_INFO, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ 146 fprintf(stdout, "[INFO] " fmt "\n", ##__VA_ARGS__); \ 147 } while (0) 148 149 /** 150 * @brief Write acl warining level log to host log 151 * @param [in]: fmt: the input format string 152 * @return none 153 */ 154 #define ACLLITE_LOG_WARNING(fmt, ...) \ 155 do { \ 156 aclAppLog(ACL_WARNING, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ 157 fprintf(stdout, "[WARNING] " fmt "\n", ##__VA_ARGS__); \ 158 } while (0) 159 160 /** 161 * @brief Write acl debug level log to host log 162 * @param [in]: fmt: the input format string 163 * @return none 164 */ 165 #define ACLLITE_LOG_DEBUG(fmt, ...) \ 166 do { \ 167 aclAppLog(ACL_DEBUG, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ 168 fprintf(stdout, "[INFO] " fmt "\n", ##__VA_ARGS__); \ 169 } while (0) 170 171 /** 172 * @brief define variable record time && 173 set start time 174 * @param [X]: function name 175 * @return X_START X_END 176 */ 177 #define TIME_START(X) auto X##_START = std::chrono::steady_clock::now(), X##_END = X##_START 178 179 /** 180 * @brief set end time 181 * @param [X]: function name 182 * @return none 183 */ 184 #define TIME_END(X) X##_END = std::chrono::steady_clock::now() 185 186 /** 187 * @brief calculate time by nanosecond 188 * @param [X]: function name 189 * @return none 190 */ 191 #define TIME_NSEC(X) std::chrono::duration_cast<std::chrono::nanoseconds>(X##_END - X##_START).count() 192 193 /** 194 * @brief show time by nanosecond 195 * @param [X]: function name 196 * @return none 197 */ 198 #define TIME_NSEC_SHOW(X) cout << "Func " << #X << " cost : " << TIME_NSEC(X) << " ns " << endl 199 200 /** 201 * @brief calculate time and show by microsecond 202 * @param [X]: variable name 203 * @return none 204 */ 205 #define TIME_USEC(X) std::chrono::duration_cast<std::chrono::microseconds>(X##_END - X##_START).count() 206 207 /** 208 * @brief show time by microsecond 209 * @param [X]: function name 210 * @return none 211 */ 212 #define TIME_USEC_SHOW(X) cout << "Func " << #X << " cost : " << TIME_USEC(X) << " us " << endl 213 214 /** 215 * @brief calculate time and show by millisecond 216 * @param [X]: variable name 217 * @return none 218 */ 219 #define TIME_MSEC(X) std::chrono::duration_cast<std::chrono::milliseconds>(X##_END - X##_START).count() 220 221 /** 222 * @brief show time by millisecond 223 * @param [X]: function name 224 * @return none 225 */ 226 #define TIME_MSEC_SHOW(X) cout << "Func " << #X << " cost : " << TIME_MSEC(X) << " ms " << endl 227 228 /** 229 * @brief calculate time and show by second 230 * @param [X]: variable name 231 * @return none 232 */ 233 #define TIME_SEC(X) std::chrono::duration_cast<std::chrono::seconds>(X##_END - X##_START).count() 234 235 /** 236 * @brief show time by second 237 * @param [X]: function name 238 * @return none 239 */ 240 #define TIME_SEC_SHOW(X) cout << "Func " << #X << " cost : " << TIME_SEC(X) << " s " << endl 241 242 /** 243 * @brief calculate time and show by minute 244 * @param [X]: variable name 245 * @return none 246 */ 247 #define TIME_MINUTE(X) std::chrono::duration_cast<std::chrono::minutes>(X##_END - X##_START).count() 248 249 /** 250 * @brief show time by minute 251 * @param [X]: function name 252 * @return none 253 */ 254 #define TIME_MINUTE_SHOW(X) cout << "Func " << #X << " cost : " << TIME_MINUTE(X) << " min " << endl 255 256 /** 257 * @brief calculate time and show by hour 258 * @param [X]: variable name 259 * @return none 260 */ 261 #define TIME_HOUR(X) std::chrono::duration_cast<std::chrono::hours>(X##_END - X##_START).count() 262 263 /** 264 * @brief show time by hour 265 * @param [X]: function name 266 * @return none 267 */ 268 #define TIME_HOUR_SHOW(X) cout << "Func " << #X << " cost : " << TIME_HOUR(X) << " h " << endl 269 270 /** 271 * @brief Recognize the string is a accessable directory or not 272 * @param [in]: path: the input string 273 * @return bool true: is directory; false: not directory 274 */ 275 bool IsDirectory(const std::string &path); 276 277 /** 278 * @brief Copy data to device 279 * @param [in]: data: The data to copy 280 * @param [in]: size: The data bytes size 281 * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, 282 * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST 283 * @param [in]: memType: The dest memory type:MEMORY_NORMAL(in Atlas200DK), 284 * MEMORY_DEVICE, MEMORY_DVPP 285 * @return void* The dest memory pointer 286 */ 287 void *CopyDataToDevice(const void *data, uint32_t size, aclrtRunMode curRunMode, MemoryType memType); 288 289 /** 290 * @brief Copy data to device buffer 291 * @param [in]: dest: The device buffer 292 * @param [in]: destSize: The device buffer size 293 * @param [in]: src: The data to copy 294 * @param [in]: srcSize: The data bytes size 295 * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, 296 * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST 297 * @return AclLiteError ACLLITE_OK: copy success 298 * others: copy failed 299 */ 300 AclLiteError CopyDataToDeviceEx(void *dest, uint32_t destSize, const void *src, uint32_t srcSize, aclrtRunMode runMode); 301 302 /** 303 * @brief Copy data to host 304 * @param [in]: data: The data to be copy 305 * @param [in]: size: The data bytes size 306 * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, 307 * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST 308 * @param [in]: memType: The dest memory type:MEMORY_NORMAL, MEMORY_HOST 309 * @return void* The dest memory pointer 310 */ 311 void *CopyDataToHost(const void *data, uint32_t size, aclrtRunMode curRunMode, MemoryType memType); 312 313 /** 314 * @brief Copy data to host buffer 315 * @param [in]: dest: The host buffer 316 * @param [in]: destSize: The host buffer size 317 * @param [in]: src: The data to copy 318 * @param [in]: srcSize: The data bytes size 319 * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, 320 * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST 321 * @return AclLiteError ACLLITE_OK: copy success 322 * others: copy failed 323 */ 324 AclLiteError CopyDataToHostEx(void *dest, uint32_t destSize, const void *src, uint32_t srcSize, aclrtRunMode runMode); 325 326 /** 327 * @brief Copy data to memory 328 * @param [in]: data: The data to be copy 329 * @param [in]: size: The data bytes size 330 * @param [in]: policy: the kind of sync, 331 * typedef enum aclrtMemcpyKind { 332 * ACL_MEMCPY_HOST_TO_HOST, // Memory copy from Host to Host 333 * ACL_MEMCPY_HOST_TO_DEVICE, // Memory copy from Host to Device 334 * ACL_MEMCPY_DEVICE_TO_HOST, // Memory copy from Device to Host 335 * ACL_MEMCPY_DEVICE_TO_DEVICE, // Memory copy from Device to Device 336 * } aclrtMemcpyKind; 337 * @param [in]: memType: The dest memory type 338 * @return void* The dest memory pointer 339 */ 340 void *CopyData(const void *data, uint32_t size, aclrtMemcpyKind policy, MemoryType memType); 341 342 /** 343 * @brief Read jpeg image file. Only support baseline, not support progressive 344 * @param [out]: image: image data read from file. 345 * @param [in]: fileName: The data bytes size 346 * @return AclLiteError ACLLITE_OK: read success 347 * others: read failed 348 */ 349 AclLiteError ReadJpeg(ImageData &image, const std::string &fileName); 350 351 /** 352 * @brief Get all files from file list string 353 * @param [in]: pathList: files list string, seperate by ',', 354 * the element could be file path or directory 355 * @param [in]: fileVec: The data bytes size 356 * @return AclLiteError ACLLITE_OK: read success 357 * others: read failed 358 */ 359 void GetAllFiles(const std::string &pathList, std::vector<std::string> &fileVec); 360 361 /** 362 * @brief Save data to binary file 363 * @param [in]: filename: binary file name with path 364 * @param [in]: data: binary data 365 * @param [in]: size: bytes size of data 366 * @return AclLiteError ACLLITE_OK: read success 367 * others: read failed 368 */ 369 void SaveBinFile(const std::string &filename, const void *data, uint32_t size); 370 371 /** 372 * @brief Read binary file to buffer 373 * @param [in]: fileName: binary file name with path 374 * @param [in]: data: buffer 375 * @param [in]: size: buffer size 376 * @return AclLiteError ACLLITE_OK: read success 377 * others: read failed 378 */ 379 AclLiteError ReadBinFile(const std::string &fileName, void *&data, uint32_t &size); 380 381 /** 382 * @brief Copy image to memory that malloc by new 383 * @param [out]: destImage: The image after copy 384 * @param [in]: srcImage: The image to copy 385 * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, 386 * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST 387 * @return AclLiteError ACLLITE_OK: read success 388 * others: read failed 389 */ 390 AclLiteError CopyImageToLocal(ImageData &destImage, ImageData &srcImage, aclrtRunMode curRunMode); 391 392 /** 393 * @brief Copy image to acl device 394 * @param [out]: destImage: The image after copy 395 * @param [in]: srcImage: The image to copy 396 * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, 397 * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST 398 * @param [in]: memType: memory type, dvpp is MEMORY_DVPP, 399 * device is MEMPRY_DEVICE 400 * @return AclLiteError ACLLITE_OK: read success 401 * others: read failed 402 */ 403 AclLiteError CopyImageToDevice(ImageData &destImage, ImageData &srcImage, aclrtRunMode curRunMode, MemoryType memType); 404 405 /** 406 * @brief Match ip address string as <1-255>.<0-255>.<0-255>.<0-255>:<port> 407 * @param [in]: addrStr: Ip address string 408 * @return bool true: The input string match success 409 * false: is not match 410 */ 411 bool IsIpAddrWithPort(const std::string &addrStr); 412 413 /** 414 * @brief Split ip address string <1-255>.<0-255>.<0-255>.<0-255>:<port> to 415 * ip and port 416 * @param [out]: ip: Ip address <1-255>.<0-255>.<0-255>.<0-255> 417 * @param [out]: port: port string 418 * @param [in]: addr: Ip address string 419 * @return None 420 */ 421 void ParseIpAddr(std::string &ip, std::string &port, const std::string &addr); 422 423 /** 424 * @brief Judge input string is mp4 file path 425 * @param [in]: path: file path 426 * @return bool true: input string is mp4 file path 427 * false: is not mp4 file path 428 */ 429 bool IsVideoFile(const std::string &path); 430 431 /** 432 * @brief Judge input string is rtsp addr link rtsp:// 433 * @param [in]: str: input string 434 * @return bool true: input string is rtsp address 435 * false: is not rtsp address 436 */ 437 bool IsRtspAddr(const std::string &str); 438 439 /** 440 * @brief Judge input string is digit string 441 * @param [in]: str: input string 442 * @return bool true: input string is digit string 443 * false: is not rtsp address 444 */ 445 bool IsDigitStr(const std::string &str); 446 447 /** 448 * @brief Test file path is exist or not 449 * @param [in]: path: file path 450 * @return bool true: file path is exist 451 * false: is not exist 452 */ 453 bool IsPathExist(const std::string &path); 454 455 /** 456 * @brief read file and save information to config 457 * @param [out]: config: map, save option information 458 * @param [in]: configFile: string, file 459 * @return bool true: read config success 460 * false: read config fail 461 */ 462 bool ReadConfig(std::map<std::string, std::string> &config, const char *configFile); 463 464 /** 465 * @brief print option information 466 * @param [in]: m: map, save option information 467 * @return None 468 */ 469 void PrintConfig(const std::map<std::string, std::string> &m); 470 471 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_UTILS_ACL_LITE_UTILS_H_ 472