├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── build-linux_RK3588.sh ├── include ├── 3rdparty │ └── rga │ │ └── RK3588 │ │ ├── include │ │ ├── GrallocOps.h │ │ ├── RgaApi.h │ │ ├── RgaMutex.h │ │ ├── RgaSingleton.h │ │ ├── RgaUtils.h │ │ ├── RockchipRga.h │ │ ├── drmrga.h │ │ ├── im2d.h │ │ ├── im2d.hpp │ │ ├── im2d_buffer.h │ │ ├── im2d_common.h │ │ ├── im2d_expand.h │ │ ├── im2d_mpi.h │ │ ├── im2d_single.h │ │ ├── im2d_task.h │ │ ├── im2d_type.h │ │ ├── im2d_version.h │ │ └── rga.h │ │ └── lib │ │ └── Linux │ │ └── aarch64 │ │ └── librga.so ├── ThreadPool.hpp ├── coreNum.hpp ├── drm_func.h ├── librknn_api.so ├── librknnrt.so ├── postprocess.h ├── preprocess.h ├── rga_func.h ├── rkYolov5s.hpp ├── rknnPool.hpp ├── rknn_api.h └── rknn_matmul_api.h ├── model ├── RK3588 │ └── yolov5s-640-640.rknn └── coco_80_labels_list.txt ├── performance.sh └── src ├── main.cc ├── postprocess.cc ├── preprocess.cc └── rkYolov5s.cc /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.dylib 16 | *.dll 17 | 18 | # Fortran module files 19 | *.mod 20 | *.smod 21 | 22 | # Compiled Static libraries 23 | *.lai 24 | *.la 25 | *.a 26 | *.lib 27 | 28 | # Executables 29 | *.exe 30 | *.out 31 | *.app 32 | 33 | *.mp4 34 | /build 35 | /install 36 | /.vscode 37 | 38 | *.png 39 | *.jpg 40 | *.mp4 -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4.1) 2 | 3 | project(rknn_yolov5_demo) 4 | 5 | set(CMAKE_CXX_STANDARD 14) 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_FLAGS "-pthread") 8 | 9 | # skip 3rd-party lib dependencies 10 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-shlib-undefined") 11 | 12 | # install target and libraries 13 | set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install/rknn_yolov5_demo_${CMAKE_SYSTEM_NAME}) 14 | 15 | set(CMAKE_SKIP_INSTALL_RPATH FALSE) 16 | set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) 17 | set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") 18 | 19 | # rknn api 20 | set(RKNN_API_PATH ${CMAKE_SOURCE_DIR}/../../runtime/RK3588/${CMAKE_SYSTEM_NAME}/librknn_api) 21 | set(LIB_ARCH aarch64) 22 | set(RKNN_RT_LIB ${CMAKE_SOURCE_DIR}/include/librknnrt.so) 23 | 24 | #rga 25 | set(RGA_PATH ${CMAKE_SOURCE_DIR}/include/3rdparty/rga/RK3588) 26 | set(RGA_LIB ${RGA_PATH}/lib/Linux//${LIB_ARCH}/librga.so) 27 | 28 | 29 | include_directories(${RKNN_API_PATH}/include) 30 | include_directories(${CMAKE_SOURCE_DIR}/include/3rdparty) 31 | include_directories(${RGA_PATH}/include) 32 | 33 | # opencv 34 | find_package(OpenCV REQUIRED) 35 | 36 | set(CMAKE_INSTALL_RPATH "lib") 37 | 38 | 39 | # rknn_yolov5_demo 40 | include_directories( ${CMAKE_SOURCE_DIR}/include) 41 | 42 | add_executable(rknn_yolov5_demo 43 | src/main.cc 44 | src/postprocess.cc 45 | src/preprocess.cc 46 | src/rkYolov5s.cc 47 | ) 48 | 49 | target_link_libraries(rknn_yolov5_demo 50 | ${RKNN_RT_LIB} 51 | ${OpenCV_LIBS} 52 | ${RGA_LIB} 53 | ) 54 | 55 | 56 | # install target and libraries 57 | set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install/rknn_yolov5_demo_${CMAKE_SYSTEM_NAME}) 58 | install(TARGETS rknn_yolov5_demo DESTINATION ./) 59 | install(PROGRAMS ${RKNN_RT_LIB} DESTINATION lib) 60 | install(PROGRAMS ${RGA_LIB} DESTINATION lib) 61 | install(DIRECTORY model DESTINATION ./) 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 简介 2 | * 此仓库为c++实现, 大体改自[rknpu2](https://github.com/rockchip-linux/rknpu2), python快速部署见于[rknn-multi-threaded](https://github.com/leafqycc/rknn-multi-threaded) 3 | * 使用[线程池](https://github.com/senlinzhan/dpool)异步操作rknn模型, 提高rk3588/rk3588s的NPU使用率, 进而提高推理帧数 4 | * [yolov5s](https://github.com/rockchip-linux/rknpu2/tree/master/examples/rknn_yolov5_demo/model/RK3588)使用relu激活函数进行优化,提高推理帧率 5 | 6 | # 更新说明 7 | * 修复了cmake找不到pthread的问题 8 | * 新增nosigmoid分支,使用[rknn_model_zoo](https://github.com/airockchip/rknn_model_zoo/tree/main/models)下的模型以达到极限性能提升 9 | * 将RK3588 NPU SDK 更新至官方主线1.5.0, [yolov5s-silu](https://github.com/rockchip-linux/rknn-toolkit2/tree/v1.4.0/examples/onnx/yolov5)将沿用1.4.0的旧版本模型, [yolov5s-relu](https://github.com/rockchip-linux/rknpu2/tree/master/examples/rknn_yolov5_demo/model/RK3588)更新至1.5.0版本, 弃用nosigmoid分支。 10 | * 新增v1.5.0分支(向下兼容1.4.0), main分支更新至v1.5.2, 修改了项目结构, 将rknn模型线程池封装成类(include/rknnPool.hpp) 11 | 12 | # 使用说明 13 | ### 演示 14 | * 系统需安装有**OpenCV** 15 | * 下载Releases中的测试视频于项目根目录,运行build-linux_RK3588.sh 16 | * 可切换至root用户运行performance.sh定频提高性能和稳定性 17 | * 编译完成后进入install运行命令./rknn_yolov5_demo **模型所在路径** **视频所在路径/摄像头序号** 18 | 19 | ### 部署应用 20 | * 参考include/rkYolov5s.hpp中的rkYolov5s类构建rknn模型类 21 | 22 | # 多线程模型帧率测试 23 | * 使用performance.sh进行CPU/NPU定频尽量减少误差 24 | * 测试模型来源: 25 | * [yolov5s-relu](https://github.com/rockchip-linux/rknpu2/tree/master/examples/rknn_yolov5_demo/model/RK3588) 26 | * 测试视频可见于 [bilibili](https://www.bilibili.com/video/BV1zo4y1x7aE/?spm_id_from=333.999.0.0) 27 | 28 | | 模型\线程数 | 1 | 2 | 3 | 4 | 5 | 6 | 9 | 12 | 29 | | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | 30 | | Yolov5s - relu | 41.6044 | 71.6037 | 98.6057 | 98.0068 | 104.6001 | 114.7454 | 129.5693 | 140.8788 | 31 | 32 | # 补充 33 | * 异常处理尚未完善, 目前仅支持rk3588/rk3588s下的运行 34 | 35 | # Acknowledgements 36 | * https://github.com/rockchip-linux/rknpu2 37 | * https://github.com/senlinzhan/dpool 38 | * https://github.com/ultralytics/yolov5 39 | * https://github.com/airockchip/rknn_model_zoo -------------------------------------------------------------------------------- /build-linux_RK3588.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | 3 | # TARGET_SOC="rk3588" 4 | GCC_COMPILER=aarch64-linux-gnu 5 | 6 | export LD_LIBRARY_PATH=${TOOL_CHAIN}/lib64:$LD_LIBRARY_PATH 7 | export CC=${GCC_COMPILER}-gcc 8 | export CXX=${GCC_COMPILER}-g++ 9 | 10 | ROOT_PWD=$( cd "$( dirname $0 )" && cd -P "$( dirname "$SOURCE" )" && pwd ) 11 | 12 | # build 13 | BUILD_DIR=${ROOT_PWD}/build/build_linux_aarch64 14 | 15 | if [ ! -d "${BUILD_DIR}" ]; then 16 | mkdir -p ${BUILD_DIR} 17 | fi 18 | 19 | cd ${BUILD_DIR} 20 | cmake ../.. -DCMAKE_SYSTEM_NAME=Linux 21 | make -j8 22 | make install 23 | cd - 24 | 25 | # relu版本 26 | cd install/rknn_yolov5_demo_Linux/ && ./rknn_yolov5_demo ./model/RK3588/yolov5s-640-640.rknn ../../720p60hz.mp4 27 | # 使用摄像头 28 | # cd install/rknn_yolov5_demo_Linux/ && ./rknn_yolov5_demo ./model/RK3588/yolov5s-640-640.rknn 0 29 | 30 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/GrallocOps.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Zhiqin Wei 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _rk_graphic_buffer_h_ 20 | #define _rk_graphic_buffer_h_ 21 | 22 | #ifdef ANDROID 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | #include 40 | #include 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | 48 | #include "drmrga.h" 49 | #include "rga.h" 50 | 51 | // ------------------------------------------------------------------------------- 52 | int RkRgaGetHandleFd(buffer_handle_t handle, int *fd); 53 | int RkRgaGetHandleAttributes(buffer_handle_t handle, 54 | std::vector *attrs); 55 | int RkRgaGetHandleMapAddress(buffer_handle_t handle, 56 | void **buf); 57 | #endif //Android 58 | 59 | #endif //_rk_graphic_buffer_h_ 60 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/RgaApi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Zhiqin Wei 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #ifndef _rockchip_rga_c_h_ 19 | #define _rockchip_rga_c_h_ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | #include "drmrga.h" 34 | #include "rga.h" 35 | 36 | #ifdef __cplusplus 37 | extern "C"{ 38 | #endif 39 | 40 | /* 41 | * Compatible with the old version of C interface.The new 42 | * version of the C interface no longer requires users to 43 | * initialize rga, so RgaInit and RgaDeInit are just for 44 | * compatibility with the old C interface, so please do 45 | * not use ctx, because it is usually a NULL. 46 | */ 47 | #define RgaInit(ctx) ({ \ 48 | int ret = 0; \ 49 | ret = c_RkRgaInit(); \ 50 | c_RkRgaGetContext(ctx); \ 51 | ret;\ 52 | }) 53 | #define RgaDeInit(ctx) { \ 54 | (void)ctx; /* unused */ \ 55 | c_RkRgaDeInit(); \ 56 | } 57 | #define RgaBlit(...) c_RkRgaBlit(__VA_ARGS__) 58 | #define RgaCollorFill(...) c_RkRgaColorFill(__VA_ARGS__) 59 | #define RgaFlush() c_RkRgaFlush() 60 | 61 | int c_RkRgaInit(); 62 | void c_RkRgaDeInit(); 63 | void c_RkRgaGetContext(void **ctx); 64 | int c_RkRgaBlit(rga_info_t *src, rga_info_t *dst, rga_info_t *src1); 65 | int c_RkRgaColorFill(rga_info_t *dst); 66 | int c_RkRgaFlush(); 67 | 68 | #ifndef ANDROID /* linux */ 69 | int c_RkRgaGetAllocBuffer(bo_t *bo_info, int width, int height, int bpp); 70 | int c_RkRgaGetAllocBufferCache(bo_t *bo_info, int width, int height, int bpp); 71 | int c_RkRgaGetMmap(bo_t *bo_info); 72 | int c_RkRgaUnmap(bo_t *bo_info); 73 | int c_RkRgaFree(bo_t *bo_info); 74 | int c_RkRgaGetBufferFd(bo_t *bo_info, int *fd); 75 | #endif /* #ifndef ANDROID */ 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | #endif /* #ifndef _rockchip_rga_c_h_ */ 82 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/RgaMutex.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * PutinLee 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | 20 | #ifndef _LIBS_RGA_MUTEX_H 21 | #define _LIBS_RGA_MUTEX_H 22 | 23 | #ifndef ANDROID 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | 31 | // Enable thread safety attributes only with clang. 32 | // The attributes can be safely erased when compiling with other compilers. 33 | #if defined(__clang__) && (!defined(SWIG)) 34 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) 35 | #else 36 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op 37 | #endif 38 | 39 | #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) 40 | 41 | #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) 42 | 43 | #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) 44 | 45 | #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) 46 | 47 | #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) 48 | 49 | #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) 50 | 51 | #define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) 52 | 53 | #define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) 54 | 55 | #define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) 56 | 57 | #define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) 58 | 59 | #define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) 60 | 61 | #define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) 62 | 63 | #define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) 64 | 65 | #define TRY_ACQUIRE_SHARED(...) \ 66 | THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) 67 | 68 | #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) 69 | 70 | #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) 71 | 72 | #define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) 73 | 74 | #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) 75 | 76 | #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) 77 | 78 | class Condition; 79 | 80 | /* 81 | * NOTE: This class is for code that builds on Win32. Its usage is 82 | * deprecated for code which doesn't build for Win32. New code which 83 | * doesn't build for Win32 should use std::mutex and std::lock_guard instead. 84 | * 85 | * Simple mutex class. The implementation is system-dependent. 86 | * 87 | * The mutex must be unlocked by the thread that locked it. They are not 88 | * recursive, i.e. the same thread can't lock it multiple times. 89 | */ 90 | class CAPABILITY("mutex") Mutex { 91 | public: 92 | enum { 93 | PRIVATE = 0, 94 | SHARED = 1 95 | }; 96 | 97 | Mutex(); 98 | explicit Mutex(const char* name); 99 | explicit Mutex(int type, const char* name = nullptr); 100 | ~Mutex(); 101 | 102 | // lock or unlock the mutex 103 | int32_t lock() ACQUIRE(); 104 | void unlock() RELEASE(); 105 | 106 | // lock if possible; returns 0 on success, error otherwise 107 | int32_t tryLock() TRY_ACQUIRE(0); 108 | 109 | int32_t timedLock(int64_t timeoutNs) TRY_ACQUIRE(0); 110 | 111 | // Manages the mutex automatically. It'll be locked when Autolock is 112 | // constructed and released when Autolock goes out of scope. 113 | class SCOPED_CAPABILITY Autolock { 114 | public: 115 | inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) { 116 | mLock.lock(); 117 | } 118 | inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) { 119 | mLock.lock(); 120 | } 121 | inline ~Autolock() RELEASE() { 122 | mLock.unlock(); 123 | } 124 | 125 | private: 126 | Mutex& mLock; 127 | // Cannot be copied or moved - declarations only 128 | Autolock(const Autolock&); 129 | Autolock& operator=(const Autolock&); 130 | }; 131 | 132 | private: 133 | friend class Condition; 134 | 135 | // A mutex cannot be copied 136 | Mutex(const Mutex&); 137 | Mutex& operator=(const Mutex&); 138 | 139 | pthread_mutex_t mMutex; 140 | }; 141 | 142 | // --------------------------------------------------------------------------- 143 | inline Mutex::Mutex() { 144 | pthread_mutex_init(&mMutex, nullptr); 145 | } 146 | inline Mutex::Mutex(__attribute__((unused)) const char* name) { 147 | pthread_mutex_init(&mMutex, nullptr); 148 | } 149 | inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) { 150 | if (type == SHARED) { 151 | pthread_mutexattr_t attr; 152 | pthread_mutexattr_init(&attr); 153 | pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); 154 | pthread_mutex_init(&mMutex, &attr); 155 | pthread_mutexattr_destroy(&attr); 156 | } else { 157 | pthread_mutex_init(&mMutex, nullptr); 158 | } 159 | } 160 | inline Mutex::~Mutex() { 161 | pthread_mutex_destroy(&mMutex); 162 | } 163 | inline int32_t Mutex::lock() { 164 | return -pthread_mutex_lock(&mMutex); 165 | } 166 | inline void Mutex::unlock() { 167 | pthread_mutex_unlock(&mMutex); 168 | } 169 | inline int32_t Mutex::tryLock() { 170 | return -pthread_mutex_trylock(&mMutex); 171 | } 172 | inline int32_t Mutex::timedLock(int64_t timeoutNs) { 173 | timespec now; 174 | clock_gettime(CLOCK_REALTIME, &now); 175 | timeoutNs += now.tv_sec*1000000000 + now.tv_nsec; 176 | const struct timespec ts = { 177 | /* .tv_sec = */ static_cast(timeoutNs / 1000000000), 178 | /* .tv_nsec = */ static_cast(timeoutNs % 1000000000), 179 | }; 180 | return -pthread_mutex_timedlock(&mMutex, &ts); 181 | } 182 | 183 | // --------------------------------------------------------------------------- 184 | 185 | /* 186 | * Automatic mutex. Declare one of these at the top of a function. 187 | * When the function returns, it will go out of scope, and release the 188 | * mutex. 189 | */ 190 | 191 | typedef Mutex::Autolock AutoMutex; 192 | #endif // __ANDROID_VNDK__ 193 | #endif // _LIBS_RGA_MUTEX_H 194 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/RgaSingleton.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Zhiqin Wei 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _LIBS_RGA_SINGLETON_H 20 | #define _LIBS_RGA_SINGLETON_H 21 | 22 | #ifndef ANDROID 23 | #include "RgaMutex.h" 24 | 25 | #if defined(__clang__) 26 | #pragma clang diagnostic push 27 | #pragma clang diagnostic ignored "-Wundefined-var-template" 28 | #endif 29 | 30 | template 31 | class Singleton { 32 | public: 33 | static TYPE& getInstance() { 34 | Mutex::Autolock _l(sLock); 35 | TYPE* instance = sInstance; 36 | if (instance == nullptr) { 37 | instance = new TYPE(); 38 | sInstance = instance; 39 | } 40 | return *instance; 41 | } 42 | 43 | static bool hasInstance() { 44 | Mutex::Autolock _l(sLock); 45 | return sInstance != nullptr; 46 | } 47 | 48 | protected: 49 | ~Singleton() { } 50 | Singleton() { } 51 | 52 | private: 53 | Singleton(const Singleton&); 54 | Singleton& operator = (const Singleton&); 55 | static Mutex sLock; 56 | static TYPE* sInstance; 57 | }; 58 | 59 | #if defined(__clang__) 60 | #pragma clang diagnostic pop 61 | #endif 62 | 63 | #define RGA_SINGLETON_STATIC_INSTANCE(TYPE) \ 64 | template<> ::Mutex \ 65 | (::Singleton< TYPE >::sLock)(::Mutex::PRIVATE); \ 66 | template<> TYPE* ::Singleton< TYPE >::sInstance(nullptr); /* NOLINT */ \ 67 | template class ::Singleton< TYPE >; 68 | 69 | #endif //ANDROID 70 | #endif //_LIBS_RGA_SINGLETON_H 71 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/RgaUtils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Zhiqin Wei 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _rga_utils_h_ 20 | #define _rga_utils_h_ 21 | 22 | // ------------------------------------------------------------------------------- 23 | float get_bpp_from_format(int format); 24 | int get_perPixel_stride_from_format(int format); 25 | int get_buf_from_file(void *buf, int f, int sw, int sh, int index); 26 | int output_buf_data_to_file(void *buf, int f, int sw, int sh, int index); 27 | const char *translate_format_str(int format); 28 | int get_buf_from_file_FBC(void *buf, int f, int sw, int sh, int index); 29 | int output_buf_data_to_file_FBC(void *buf, int f, int sw, int sh, int index); 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/RockchipRga.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Zhiqin Wei 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _rockchip_rga_h_ 20 | #define _rockchip_rga_h_ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include "drmrga.h" 34 | #include "GrallocOps.h" 35 | #include "RgaUtils.h" 36 | #include "rga.h" 37 | 38 | ////////////////////////////////////////////////////////////////////////////////// 39 | #ifndef ANDROID 40 | #include "RgaSingleton.h" 41 | #endif 42 | 43 | #ifdef ANDROID 44 | #include 45 | #include 46 | #include 47 | 48 | namespace android { 49 | #endif 50 | 51 | class RockchipRga :public Singleton { 52 | public: 53 | 54 | static inline RockchipRga& get() { 55 | return getInstance(); 56 | } 57 | 58 | int RkRgaInit(); 59 | void RkRgaDeInit(); 60 | void RkRgaGetContext(void **ctx); 61 | #ifndef ANDROID /* LINUX */ 62 | int RkRgaAllocBuffer(int drm_fd /* input */, bo_t *bo_info, 63 | int width, int height, int bpp, int flags); 64 | int RkRgaFreeBuffer(int drm_fd /* input */, bo_t *bo_info); 65 | int RkRgaGetAllocBuffer(bo_t *bo_info, int width, int height, int bpp); 66 | int RkRgaGetAllocBufferExt(bo_t *bo_info, int width, int height, int bpp, int flags); 67 | int RkRgaGetAllocBufferCache(bo_t *bo_info, int width, int height, int bpp); 68 | int RkRgaGetMmap(bo_t *bo_info); 69 | int RkRgaUnmap(bo_t *bo_info); 70 | int RkRgaFree(bo_t *bo_info); 71 | int RkRgaGetBufferFd(bo_t *bo_info, int *fd); 72 | #else 73 | int RkRgaGetBufferFd(buffer_handle_t handle, int *fd); 74 | int RkRgaGetHandleMapCpuAddress(buffer_handle_t handle, void **buf); 75 | #endif 76 | int RkRgaBlit(rga_info *src, rga_info *dst, rga_info *src1); 77 | int RkRgaCollorFill(rga_info *dst); 78 | int RkRgaCollorPalette(rga_info *src, rga_info *dst, rga_info *lut); 79 | int RkRgaFlush(); 80 | 81 | 82 | void RkRgaSetLogOnceFlag(int log) { 83 | mLogOnce = log; 84 | } 85 | void RkRgaSetAlwaysLogFlag(bool log) { 86 | mLogAlways = log; 87 | } 88 | void RkRgaLogOutRgaReq(struct rga_req rgaReg); 89 | int RkRgaLogOutUserPara(rga_info *rgaInfo); 90 | inline bool RkRgaIsReady() { 91 | return mSupportRga; 92 | } 93 | 94 | RockchipRga(); 95 | ~RockchipRga(); 96 | private: 97 | bool mSupportRga; 98 | int mLogOnce; 99 | int mLogAlways; 100 | void * mContext; 101 | 102 | friend class Singleton; 103 | }; 104 | 105 | #ifdef ANDROID 106 | }; // namespace android 107 | #endif 108 | 109 | #endif 110 | 111 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/drmrga.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Zhiqin Wei 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _rk_drm_rga_ 20 | #define _rk_drm_rga_ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "rga.h" 27 | 28 | #ifdef ANDROID 29 | #define DRMRGA_HARDWARE_MODULE_ID "librga" 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #ifdef ANDROID_12 37 | #include 38 | #endif 39 | 40 | #endif 41 | 42 | #define RGA_BLIT_SYNC 0x5017 43 | #define RGA_BLIT_ASYNC 0x5018 44 | 45 | #ifndef ANDROID /* LINUX */ 46 | /* flip source image horizontally (around the vertical axis) */ 47 | #define HAL_TRANSFORM_FLIP_H 0x01 48 | /* flip source image vertically (around the horizontal axis)*/ 49 | #define HAL_TRANSFORM_FLIP_V 0x02 50 | /* rotate source image 90 degrees clockwise */ 51 | #define HAL_TRANSFORM_ROT_90 0x04 52 | /* rotate source image 180 degrees */ 53 | #define HAL_TRANSFORM_ROT_180 0x03 54 | /* rotate source image 270 degrees clockwise */ 55 | #define HAL_TRANSFORM_ROT_270 0x07 56 | #endif 57 | 58 | #define HAL_TRANSFORM_FLIP_H_V 0x08 59 | 60 | /*****************************************************************************/ 61 | 62 | /* for compatibility */ 63 | #define DRM_RGA_MODULE_API_VERSION HWC_MODULE_API_VERSION_0_1 64 | #define DRM_RGA_DEVICE_API_VERSION HWC_DEVICE_API_VERSION_0_1 65 | #define DRM_RGA_API_VERSION HWC_DEVICE_API_VERSION 66 | 67 | #define DRM_RGA_TRANSFORM_ROT_MASK 0x0000000F 68 | #define DRM_RGA_TRANSFORM_ROT_0 0x00000000 69 | #define DRM_RGA_TRANSFORM_ROT_90 HAL_TRANSFORM_ROT_90 70 | #define DRM_RGA_TRANSFORM_ROT_180 HAL_TRANSFORM_ROT_180 71 | #define DRM_RGA_TRANSFORM_ROT_270 HAL_TRANSFORM_ROT_270 72 | 73 | #define DRM_RGA_TRANSFORM_FLIP_MASK 0x00000003 74 | #define DRM_RGA_TRANSFORM_FLIP_H HAL_TRANSFORM_FLIP_H 75 | #define DRM_RGA_TRANSFORM_FLIP_V HAL_TRANSFORM_FLIP_V 76 | 77 | enum { 78 | AWIDTH = 0, 79 | AHEIGHT, 80 | ASTRIDE, 81 | AFORMAT, 82 | ASIZE, 83 | ATYPE, 84 | }; 85 | /*****************************************************************************/ 86 | 87 | #ifndef ANDROID /* LINUX */ 88 | /* memory type definitions. */ 89 | enum drm_rockchip_gem_mem_type { 90 | /* Physically Continuous memory and used as default. */ 91 | ROCKCHIP_BO_CONTIG = 1 << 0, 92 | /* cachable mapping. */ 93 | ROCKCHIP_BO_CACHABLE = 1 << 1, 94 | /* write-combine mapping. */ 95 | ROCKCHIP_BO_WC = 1 << 2, 96 | ROCKCHIP_BO_SECURE = 1 << 3, 97 | ROCKCHIP_BO_MASK = ROCKCHIP_BO_CONTIG | ROCKCHIP_BO_CACHABLE | 98 | ROCKCHIP_BO_WC | ROCKCHIP_BO_SECURE 99 | }; 100 | 101 | typedef struct bo { 102 | int fd; 103 | void *ptr; 104 | size_t size; 105 | size_t offset; 106 | size_t pitch; 107 | unsigned handle; 108 | } bo_t; 109 | #endif 110 | 111 | /* 112 | @value size: user not need care about.For avoid read/write out of memory 113 | */ 114 | typedef struct rga_rect { 115 | int xoffset; 116 | int yoffset; 117 | int width; 118 | int height; 119 | int wstride; 120 | int hstride; 121 | int format; 122 | int size; 123 | } rga_rect_t; 124 | 125 | typedef struct rga_nn { 126 | int nn_flag; 127 | int scale_r; 128 | int scale_g; 129 | int scale_b; 130 | int offset_r; 131 | int offset_g; 132 | int offset_b; 133 | } rga_nn_t; 134 | 135 | typedef struct rga_dither { 136 | int enable; 137 | int mode; 138 | int lut0_l; 139 | int lut0_h; 140 | int lut1_l; 141 | int lut1_h; 142 | } rga_dither_t; 143 | 144 | struct rga_mosaic_info { 145 | uint8_t enable; 146 | uint8_t mode; 147 | }; 148 | 149 | struct rga_pre_intr_info { 150 | uint8_t enable; 151 | 152 | uint8_t read_intr_en; 153 | uint8_t write_intr_en; 154 | uint8_t read_hold_en; 155 | uint32_t read_threshold; 156 | uint32_t write_start; 157 | uint32_t write_step; 158 | }; 159 | 160 | /* MAX(min, (max - channel_value)) */ 161 | struct rga_osd_invert_factor { 162 | uint8_t alpha_max; 163 | uint8_t alpha_min; 164 | uint8_t yg_max; 165 | uint8_t yg_min; 166 | uint8_t crb_max; 167 | uint8_t crb_min; 168 | }; 169 | 170 | struct rga_color { 171 | union { 172 | struct { 173 | uint8_t red; 174 | uint8_t green; 175 | uint8_t blue; 176 | uint8_t alpha; 177 | }; 178 | uint32_t value; 179 | }; 180 | }; 181 | 182 | struct rga_osd_bpp2 { 183 | uint8_t ac_swap; // ac swap flag 184 | // 0: CA 185 | // 1: AC 186 | uint8_t endian_swap; // rgba2bpp endian swap 187 | // 0: Big endian 188 | // 1: Little endian 189 | struct rga_color color0; 190 | struct rga_color color1; 191 | }; 192 | 193 | struct rga_osd_mode_ctrl { 194 | uint8_t mode; // OSD cal mode: 195 | // 0b'1: statistics mode 196 | // 1b'1: auto inversion overlap mode 197 | uint8_t direction_mode; // horizontal or vertical 198 | // 0: horizontal 199 | // 1: vertical 200 | uint8_t width_mode; // using @fix_width or LUT width 201 | // 0: fix width 202 | // 1: LUT width 203 | uint16_t block_fix_width; // OSD block fixed width 204 | // real width = (fix_width + 1) * 2 205 | uint8_t block_num; // OSD block num 206 | uint16_t flags_index; // auto invert flags index 207 | 208 | /* invertion config */ 209 | uint8_t color_mode; // selete color 210 | // 0: src1 color 211 | // 1: config data color 212 | uint8_t invert_flags_mode; // invert flag selete 213 | // 0: use RAM flag 214 | // 1: usr last result 215 | uint8_t default_color_sel; // default color mode 216 | // 0: default is bright 217 | // 1: default is dark 218 | uint8_t invert_enable; // invert channel enable 219 | // 1 << 0: aplha enable 220 | // 1 << 1: Y/G disable 221 | // 1 << 2: C/RB disable 222 | uint8_t invert_mode; // invert cal mode 223 | // 0: normal(max-data) 224 | // 1: swap 225 | uint8_t invert_thresh; // if luma > thresh, osd_flag to be 1 226 | uint8_t unfix_index; // OSD width config index 227 | }; 228 | 229 | struct rga_osd_info { 230 | uint8_t enable; 231 | 232 | struct rga_osd_mode_ctrl mode_ctrl; 233 | struct rga_osd_invert_factor cal_factor; 234 | struct rga_osd_bpp2 bpp2_info; 235 | 236 | union { 237 | struct { 238 | uint32_t last_flags1; 239 | uint32_t last_flags0; 240 | }; 241 | uint64_t last_flags; 242 | }; 243 | 244 | union { 245 | struct { 246 | uint32_t cur_flags1; 247 | uint32_t cur_flags0; 248 | }; 249 | uint64_t cur_flags; 250 | }; 251 | }; 252 | 253 | /* 254 | @value fd: use fd to share memory, it can be ion shard fd,and dma fd. 255 | @value virAddr:userspace address 256 | @value phyAddr:use phy address 257 | @value hnd: use buffer_handle_t 258 | */ 259 | typedef struct rga_info { 260 | int fd; 261 | void *virAddr; 262 | void *phyAddr; 263 | #ifndef ANDROID /* LINUX */ 264 | unsigned hnd; 265 | #else /* Android */ 266 | buffer_handle_t hnd; 267 | #endif 268 | int format; 269 | rga_rect_t rect; 270 | unsigned int blend; 271 | int bufferSize; 272 | int rotation; 273 | int color; 274 | int testLog; 275 | int mmuFlag; 276 | int colorkey_en; 277 | int colorkey_mode; 278 | int colorkey_max; 279 | int colorkey_min; 280 | int scale_mode; 281 | int color_space_mode; 282 | int sync_mode; 283 | rga_nn_t nn; 284 | rga_dither_t dither; 285 | int rop_code; 286 | int rd_mode; 287 | unsigned short is_10b_compact; 288 | unsigned short is_10b_endian; 289 | 290 | int in_fence_fd; 291 | int out_fence_fd; 292 | 293 | int core; 294 | int priority; 295 | 296 | unsigned short enable; 297 | 298 | int handle; 299 | 300 | struct rga_mosaic_info mosaic_info; 301 | 302 | struct rga_osd_info osd_info; 303 | 304 | struct rga_pre_intr_info pre_intr; 305 | 306 | int mpi_mode; 307 | 308 | union { 309 | int ctx_id; 310 | int job_handle; 311 | }; 312 | 313 | char reserve[402]; 314 | } rga_info_t; 315 | 316 | 317 | typedef struct drm_rga { 318 | rga_rect_t src; 319 | rga_rect_t dst; 320 | } drm_rga_t; 321 | 322 | /* 323 | @fun rga_set_rect:For use to set the rects esayly 324 | 325 | @param rect:The rect user want to set,like setting the src rect: 326 | drm_rga_t rects; 327 | rga_set_rect(rects.src,0,0,1920,1080,1920,NV12); 328 | mean to set the src rect to the value. 329 | */ 330 | static inline int rga_set_rect(rga_rect_t *rect, 331 | int x, int y, int w, int h, int sw, int sh, int f) { 332 | if (!rect) 333 | return -EINVAL; 334 | 335 | rect->xoffset = x; 336 | rect->yoffset = y; 337 | rect->width = w; 338 | rect->height = h; 339 | rect->wstride = sw; 340 | rect->hstride = sh; 341 | rect->format = f; 342 | 343 | return 0; 344 | } 345 | 346 | #ifndef ANDROID /* LINUX */ 347 | static inline void rga_set_rotation(rga_info_t *info, int angle) { 348 | if (angle == 90) 349 | info->rotation = HAL_TRANSFORM_ROT_90; 350 | else if (angle == 180) 351 | info->rotation = HAL_TRANSFORM_ROT_180; 352 | else if (angle == 270) 353 | info->rotation = HAL_TRANSFORM_ROT_270; 354 | } 355 | #endif 356 | /*****************************************************************************/ 357 | 358 | #endif 359 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * PutinLee 5 | * Cerf Yu 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | #ifndef _im2d_h_ 21 | #define _im2d_h_ 22 | 23 | #include "im2d_version.h" 24 | #include "im2d_type.h" 25 | 26 | #include "im2d_common.h" 27 | #include "im2d_buffer.h" 28 | #include "im2d_single.h" 29 | #include "im2d_task.h" 30 | #include "im2d_mpi.h" 31 | 32 | #endif /* #ifndef _im2d_h_ */ 33 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * PutinLee 5 | * Cerf Yu 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | #ifndef _im2d_hpp_ 20 | #define _im2d_hpp_ 21 | 22 | #include "im2d.h" 23 | #include "im2d_expand.h" 24 | 25 | #endif /* #ifndef _im2d_hpp_ */ 26 | 27 | 28 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d_buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Cerf Yu 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #ifndef _im2d_buffer_h_ 19 | #define _im2d_buffer_h_ 20 | 21 | #include "im2d_type.h" 22 | 23 | /** 24 | * Import external buffers into RGA driver. 25 | * 26 | * @param fd/va/pa 27 | * Select dma_fd/virtual_address/physical_address by buffer type 28 | * @param size 29 | * Describes the size of the image buffer 30 | * 31 | * @return rga_buffer_handle_t 32 | */ 33 | #ifdef __cplusplus 34 | IM_API rga_buffer_handle_t importbuffer_fd(int fd, int size); 35 | IM_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, int size); 36 | IM_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, int size); 37 | #endif 38 | 39 | /** 40 | * Import external buffers into RGA driver. 41 | * 42 | * @param fd/va/pa 43 | * Select dma_fd/virtual_address/physical_address by buffer type 44 | * @param width 45 | * Describes the pixel width stride of the image buffer 46 | * @param height 47 | * Describes the pixel height stride of the image buffer 48 | * @param format 49 | * Describes the pixel format of the image buffer 50 | * 51 | * @return rga_buffer_handle_t 52 | */ 53 | #ifdef __cplusplus 54 | IM_API rga_buffer_handle_t importbuffer_fd(int fd, int width, int height, int format); 55 | IM_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, int width, int height, int format); 56 | IM_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, int width, int height, int format); 57 | #endif 58 | 59 | /** 60 | * Import external buffers into RGA driver. 61 | * 62 | * @param fd/va/pa 63 | * Select dma_fd/virtual_address/physical_address by buffer type 64 | * @param param 65 | * Configure buffer parameters 66 | * 67 | * @return rga_buffer_handle_t 68 | */ 69 | IM_EXPORT_API rga_buffer_handle_t importbuffer_fd(int fd, im_handle_param_t *param); 70 | IM_EXPORT_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, im_handle_param_t *param); 71 | IM_EXPORT_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, im_handle_param_t *param); 72 | 73 | /** 74 | * Import external buffers into RGA driver. 75 | * 76 | * @param handle 77 | * rga buffer handle 78 | * 79 | * @return success or else negative error code. 80 | */ 81 | IM_EXPORT_API IM_STATUS releasebuffer_handle(rga_buffer_handle_t handle); 82 | 83 | /** 84 | * Wrap image Parameters. 85 | * 86 | * @param handle/virtualaddr/physicaladdr/fd 87 | * RGA buffer handle/virtualaddr/physicaladdr/fd. 88 | * @param width 89 | * Width of image manipulation area. 90 | * @param height 91 | * Height of image manipulation area. 92 | * @param wstride 93 | * Width pixel stride, default (width = wstride). 94 | * @param hstride 95 | * Height pixel stride, default (height = hstride). 96 | * @param format 97 | * Image format. 98 | * 99 | * @return rga_buffer_t 100 | */ 101 | #define wrapbuffer_handle(handle, width, height, format, ...) \ 102 | ({ \ 103 | rga_buffer_t im2d_api_buffer; \ 104 | int __args[] = {__VA_ARGS__}; \ 105 | int __argc = sizeof(__args)/sizeof(int); \ 106 | if (__argc == 0) { \ 107 | im2d_api_buffer = wrapbuffer_handle_t(handle, width, height, width, height, format); \ 108 | } else if (__argc == 2){ \ 109 | im2d_api_buffer = wrapbuffer_handle_t(handle, width, height, __args[0], __args[1], format); \ 110 | } else { \ 111 | memset(&im2d_api_buffer, 0x0, sizeof(im2d_api_buffer)); \ 112 | printf("invalid parameter\n"); \ 113 | } \ 114 | im2d_api_buffer; \ 115 | }) 116 | 117 | #define wrapbuffer_virtualaddr(vir_addr, width, height, format, ...) \ 118 | ({ \ 119 | rga_buffer_t im2d_api_buffer; \ 120 | int __args[] = {__VA_ARGS__}; \ 121 | int __argc = sizeof(__args)/sizeof(int); \ 122 | if (__argc == 0) { \ 123 | im2d_api_buffer = wrapbuffer_virtualaddr_t(vir_addr, width, height, width, height, format); \ 124 | } else if (__argc == 2){ \ 125 | im2d_api_buffer = wrapbuffer_virtualaddr_t(vir_addr, width, height, __args[0], __args[1], format); \ 126 | } else { \ 127 | memset(&im2d_api_buffer, 0x0, sizeof(im2d_api_buffer)); \ 128 | printf("invalid parameter\n"); \ 129 | } \ 130 | im2d_api_buffer; \ 131 | }) 132 | 133 | #define wrapbuffer_physicaladdr(phy_addr, width, height, format, ...) \ 134 | ({ \ 135 | rga_buffer_t im2d_api_buffer; \ 136 | int __args[] = {__VA_ARGS__}; \ 137 | int __argc = sizeof(__args)/sizeof(int); \ 138 | if (__argc == 0) { \ 139 | im2d_api_buffer = wrapbuffer_physicaladdr_t(phy_addr, width, height, width, height, format); \ 140 | } else if (__argc == 2){ \ 141 | im2d_api_buffer = wrapbuffer_physicaladdr_t(phy_addr, width, height, __args[0], __args[1], format); \ 142 | } else { \ 143 | memset(&im2d_api_buffer, 0x0, sizeof(im2d_api_buffer)); \ 144 | printf("invalid parameter\n"); \ 145 | } \ 146 | im2d_api_buffer; \ 147 | }) 148 | 149 | #define wrapbuffer_fd(fd, width, height, format, ...) \ 150 | ({ \ 151 | rga_buffer_t im2d_api_buffer; \ 152 | int __args[] = {__VA_ARGS__}; \ 153 | int __argc = sizeof(__args)/sizeof(int); \ 154 | if (__argc == 0) { \ 155 | im2d_api_buffer = wrapbuffer_fd_t(fd, width, height, width, height, format); \ 156 | } else if (__argc == 2){ \ 157 | im2d_api_buffer = wrapbuffer_fd_t(fd, width, height, __args[0], __args[1], format); \ 158 | } else { \ 159 | memset(&im2d_api_buffer, 0x0, sizeof(im2d_api_buffer)); \ 160 | printf("invalid parameter\n"); \ 161 | } \ 162 | im2d_api_buffer; \ 163 | }) 164 | /* Symbols for define *_t functions */ 165 | IM_C_API rga_buffer_t wrapbuffer_handle_t(rga_buffer_handle_t handle, int width, int height, int wstride, int hstride, int format); 166 | IM_C_API rga_buffer_t wrapbuffer_virtualaddr_t(void* vir_addr, int width, int height, int wstride, int hstride, int format); 167 | IM_C_API rga_buffer_t wrapbuffer_physicaladdr_t(void* phy_addr, int width, int height, int wstride, int hstride, int format); 168 | IM_C_API rga_buffer_t wrapbuffer_fd_t(int fd, int width, int height, int wstride, int hstride, int format); 169 | 170 | #ifdef __cplusplus 171 | #undef wrapbuffer_handle 172 | IM_API rga_buffer_t wrapbuffer_handle(rga_buffer_handle_t handle, 173 | int width, int height, int format); 174 | IM_API rga_buffer_t wrapbuffer_handle(rga_buffer_handle_t handle, 175 | int width, int height, int format, 176 | int wstride, int hstride); 177 | #endif 178 | 179 | #endif /* #ifndef _im2d_buffer_h_ */ 180 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Cerf Yu 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #ifndef _im2d_common_h_ 19 | #define _im2d_common_h_ 20 | 21 | #include "im2d_type.h" 22 | 23 | /** 24 | * Query RGA basic information, supported resolution, supported format, etc. 25 | * 26 | * @param name 27 | * RGA_VENDOR 28 | * RGA_VERSION 29 | * RGA_MAX_INPUT 30 | * RGA_MAX_OUTPUT 31 | * RGA_INPUT_FORMAT 32 | * RGA_OUTPUT_FORMAT 33 | * RGA_EXPECTED 34 | * RGA_ALL 35 | * 36 | * @returns a string describing properties of RGA. 37 | */ 38 | IM_EXPORT_API const char* querystring(int name); 39 | 40 | /** 41 | * String to output the error message 42 | * 43 | * @param status 44 | * process result value. 45 | * 46 | * @returns error message. 47 | */ 48 | #define imStrError(...) \ 49 | ({ \ 50 | const char* im2d_api_err; \ 51 | int __args[] = {__VA_ARGS__}; \ 52 | int __argc = sizeof(__args)/sizeof(int); \ 53 | if (__argc == 0) { \ 54 | im2d_api_err = imStrError_t(IM_STATUS_INVALID_PARAM); \ 55 | } else if (__argc == 1){ \ 56 | im2d_api_err = imStrError_t((IM_STATUS)__args[0]); \ 57 | } else { \ 58 | im2d_api_err = ("Fatal error, imStrError() too many parameters\n"); \ 59 | printf("Fatal error, imStrError() too many parameters\n"); \ 60 | } \ 61 | im2d_api_err; \ 62 | }) 63 | IM_C_API const char* imStrError_t(IM_STATUS status); 64 | 65 | /** 66 | * check im2d api header file 67 | * 68 | * @param header_version 69 | * Default is RGA_CURRENT_API_HEADER_VERSION, no need to change if there are no special cases. 70 | * 71 | * @returns no error or else negative error code. 72 | */ 73 | #ifdef __cplusplus 74 | IM_API IM_STATUS imcheckHeader(im_api_version_t header_version = RGA_CURRENT_API_HEADER_VERSION); 75 | #endif 76 | 77 | /** 78 | * check RGA basic information, supported resolution, supported format, etc. 79 | * 80 | * @param src 81 | * @param dst 82 | * @param pat 83 | * @param src_rect 84 | * @param dst_rect 85 | * @param pat_rect 86 | * @param mode_usage 87 | * 88 | * @returns no error or else negative error code. 89 | */ 90 | #define imcheck(src, dst, src_rect, dst_rect, ...) \ 91 | ({ \ 92 | IM_STATUS __ret = IM_STATUS_NOERROR; \ 93 | rga_buffer_t __pat; \ 94 | im_rect __pat_rect; \ 95 | memset(&__pat, 0, sizeof(rga_buffer_t)); \ 96 | memset(&__pat_rect, 0, sizeof(im_rect)); \ 97 | int __args[] = {__VA_ARGS__}; \ 98 | int __argc = sizeof(__args)/sizeof(int); \ 99 | if (__argc == 0) { \ 100 | __ret = imcheck_t(src, dst, __pat, src_rect, dst_rect, __pat_rect, 0); \ 101 | } else if (__argc == 1){ \ 102 | __ret = imcheck_t(src, dst, __pat, src_rect, dst_rect, __pat_rect, __args[0]); \ 103 | } else { \ 104 | __ret = IM_STATUS_FAILED; \ 105 | printf("check failed\n"); \ 106 | } \ 107 | __ret; \ 108 | }) 109 | #define imcheck_composite(src, dst, pat, src_rect, dst_rect, pat_rect, ...) \ 110 | ({ \ 111 | IM_STATUS __ret = IM_STATUS_NOERROR; \ 112 | int __args[] = {__VA_ARGS__}; \ 113 | int __argc = sizeof(__args)/sizeof(int); \ 114 | if (__argc == 0) { \ 115 | __ret = imcheck_t(src, dst, pat, src_rect, dst_rect, pat_rect, 0); \ 116 | } else if (__argc == 1){ \ 117 | __ret = imcheck_t(src, dst, pat, src_rect, dst_rect, pat_rect, __args[0]); \ 118 | } else { \ 119 | __ret = IM_STATUS_FAILED; \ 120 | printf("check failed\n"); \ 121 | } \ 122 | __ret; \ 123 | }) 124 | IM_C_API IM_STATUS imcheck_t(const rga_buffer_t src, const rga_buffer_t dst, const rga_buffer_t pat, 125 | const im_rect src_rect, const im_rect dst_rect, const im_rect pat_rect, const int mode_usage); 126 | /* Compatible with the legacy symbol */ 127 | IM_C_API void rga_check_perpare(rga_buffer_t *src, rga_buffer_t *dst, rga_buffer_t *pat, 128 | im_rect *src_rect, im_rect *dst_rect, im_rect *pat_rect, int mode_usage); 129 | 130 | /** 131 | * block until all execution is complete 132 | * 133 | * @param release_fence_fd 134 | * RGA job release fence fd 135 | * 136 | * @returns success or else negative error code. 137 | */ 138 | IM_EXPORT_API IM_STATUS imsync(int release_fence_fd); 139 | 140 | /** 141 | * config 142 | * 143 | * @param name 144 | * enum IM_CONFIG_NAME 145 | * @param value 146 | * 147 | * @returns success or else negative error code. 148 | */ 149 | IM_EXPORT_API IM_STATUS imconfig(IM_CONFIG_NAME name, uint64_t value); 150 | 151 | #endif /* #ifndef _im2d_common_h_ */ 152 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d_expand.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Cerf Yu 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #ifndef _im2d_expand_h_ 19 | #define _im2d_expand_h_ 20 | 21 | #ifdef __cplusplus 22 | 23 | #include "im2d_type.h" 24 | 25 | // #if ANDROID 26 | 27 | // #include 28 | 29 | // using namespace android; 30 | 31 | // IM_API rga_buffer_handle_t importbuffer_GraphicBuffer_handle(buffer_handle_t hnd); 32 | // IM_API rga_buffer_handle_t importbuffer_GraphicBuffer(sp buf); 33 | 34 | // IM_API rga_buffer_t wrapbuffer_handle(buffer_handle_t hnd); 35 | // IM_API rga_buffer_t wrapbuffer_GraphicBuffer(sp buf); 36 | 37 | // #if USE_AHARDWAREBUFFER 38 | // #include 39 | // IM_API rga_buffer_handle_t importbuffer_AHardwareBuffer(AHardwareBuffer *buf); 40 | // IM_API rga_buffer_t wrapbuffer_AHardwareBuffer(AHardwareBuffer *buf); 41 | 42 | // #endif /* #if USE_AHARDWAREBUFFER */ 43 | // #endif /* #if ANDROID */ 44 | 45 | #endif /* #ifdef __cplusplus */ 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d_mpi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Cerf Yu 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #ifndef _im2d_mpi_hpp_ 19 | #define _im2d_mpi_hpp_ 20 | 21 | #include "im2d_type.h" 22 | 23 | /** 24 | * Create and config an rga ctx for rockit-ko 25 | * 26 | * @param flags 27 | * Some configuration flags for this job 28 | * 29 | * @returns job id. 30 | */ 31 | IM_EXPORT_API im_ctx_id_t imbegin(uint32_t flags); 32 | 33 | /** 34 | * Cancel and delete an rga ctx for rockit-ko 35 | * 36 | * @param flags 37 | * Some configuration flags for this job 38 | * 39 | * @returns success or else negative error code. 40 | */ 41 | IM_EXPORT_API IM_STATUS imcancel(im_ctx_id_t id); 42 | 43 | /** 44 | * process for rockit-ko 45 | * 46 | * @param src 47 | * The input source image and is also the foreground image in blend. 48 | * @param dst 49 | * The output destination image and is also the foreground image in blend. 50 | * @param pat 51 | * The foreground image, or a LUT table. 52 | * @param srect 53 | * The rectangle on the src channel image that needs to be processed. 54 | * @param drect 55 | * The rectangle on the dst channel image that needs to be processed. 56 | * @param prect 57 | * The rectangle on the pat channel image that needs to be processed. 58 | * @param acquire_fence_fd 59 | * @param release_fence_fd 60 | * @param opt 61 | * The image processing options configuration. 62 | * @param usage 63 | * The image processing usage. 64 | * @param ctx_id 65 | * ctx id 66 | * 67 | * @returns success or else negative error code. 68 | */ 69 | #ifdef __cplusplus 70 | IM_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat, 71 | im_rect srect, im_rect drect, im_rect prect, 72 | int acquire_fence_fd, int *release_fence_fd, 73 | im_opt_t *opt, int usage, im_ctx_id_t ctx_id); 74 | #endif 75 | IM_EXPORT_API IM_STATUS improcess_ctx(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat, 76 | im_rect srect, im_rect drect, im_rect prect, 77 | int acquire_fence_fd, int *release_fence_fd, 78 | im_opt_t *opt, int usage, im_ctx_id_t ctx_id); 79 | 80 | #endif /* #ifndef _im2d_mpi_hpp_ */ -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d_single.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Cerf Yu 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #ifndef _im2d_single_h_ 19 | #define _im2d_single_h_ 20 | 21 | #include "im2d_type.h" 22 | 23 | #ifdef __cplusplus 24 | 25 | /** 26 | * copy 27 | * 28 | * @param src 29 | * The input source image. 30 | * @param dst 31 | * The output destination image. 32 | * @param sync 33 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 34 | * @param release_fence_fd 35 | * When 'sync == 0', the fence_fd used to identify the current job state 36 | * 37 | * @returns success or else negative error code. 38 | */ 39 | IM_API IM_STATUS imcopy(const rga_buffer_t src, rga_buffer_t dst, int sync = 1, int *release_fence_fd = NULL); 40 | 41 | /** 42 | * Resize 43 | * 44 | * @param src 45 | * The input source image. 46 | * @param dst 47 | * The output destination image. 48 | * @param fx 49 | * X-direction resize factor. 50 | * @param fy 51 | * X-direction resize factor. 52 | * @param interpolation 53 | * Interpolation formula(Only RGA1 support). 54 | * @param sync 55 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 56 | * @param release_fence_fd 57 | * When 'sync == 0', the fence_fd used to identify the current job state 58 | * 59 | * @returns success or else negative error code. 60 | */ 61 | IM_API IM_STATUS imresize(const rga_buffer_t src, rga_buffer_t dst, double fx = 0, double fy = 0, int interpolation = 0, int sync = 1, int *release_fence_fd = NULL); 62 | 63 | /** 64 | * Crop 65 | * 66 | * @param src 67 | * The input source image. 68 | * @param dst 69 | * The output destination image. 70 | * @param rect 71 | * The rectangle on the source image that needs to be cropped. 72 | * @param sync 73 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 74 | * @param release_fence_fd 75 | * When 'sync == 0', the fence_fd used to identify the current job state 76 | * 77 | * @returns success or else negative error code. 78 | */ 79 | IM_API IM_STATUS imcrop(const rga_buffer_t src, rga_buffer_t dst, im_rect rect, int sync = 1, int *release_fence_fd = NULL); 80 | 81 | /** 82 | * translate 83 | * 84 | * @param src 85 | * The input source image. 86 | * @param dst 87 | * The output destination image. 88 | * @param x 89 | * Output the coordinates of the starting point in the X-direction of the destination image. 90 | * @param y 91 | * Output the coordinates of the starting point in the Y-direction of the destination image. 92 | * @param sync 93 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 94 | * @param release_fence_fd 95 | * When 'sync == 0', the fence_fd used to identify the current job state 96 | * 97 | * @returns success or else negative error code. 98 | */ 99 | IM_API IM_STATUS imtranslate(const rga_buffer_t src, rga_buffer_t dst, int x, int y, int sync = 1, int *release_fence_fd = NULL); 100 | 101 | /** 102 | * format convert 103 | * 104 | * @param src 105 | * The input source image. 106 | * @param dst 107 | * The output destination image. 108 | * @param sfmt 109 | * The source image format. 110 | * @param dfmt 111 | * The destination image format. 112 | * @param mode 113 | * color space mode: 114 | * IM_YUV_TO_RGB_BT601_LIMIT 115 | * IM_YUV_TO_RGB_BT601_FULL 116 | * IM_YUV_TO_RGB_BT709_LIMIT 117 | * IM_RGB_TO_YUV_BT601_FULL 118 | * IM_RGB_TO_YUV_BT601_LIMIT 119 | * IM_RGB_TO_YUV_BT709_LIMIT 120 | * @param sync 121 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 122 | * @param release_fence_fd 123 | * When 'sync == 0', the fence_fd used to identify the current job state 124 | * 125 | * @returns success or else negative error code. 126 | */ 127 | IM_API IM_STATUS imcvtcolor(rga_buffer_t src, rga_buffer_t dst, int sfmt, int dfmt, int mode = IM_COLOR_SPACE_DEFAULT, int sync = 1, int *release_fence_fd = NULL); 128 | 129 | /** 130 | * rotation 131 | * 132 | * @param src 133 | * The input source image. 134 | * @param dst 135 | * The output destination image. 136 | * @param rotation 137 | * IM_HAL_TRANSFORM_ROT_90 138 | * IM_HAL_TRANSFORM_ROT_180 139 | * IM_HAL_TRANSFORM_ROT_270 140 | * @param sync 141 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 142 | * @param release_fence_fd 143 | * When 'sync == 0', the fence_fd used to identify the current job state 144 | * 145 | * @returns success or else negative error code. 146 | */ 147 | IM_API IM_STATUS imrotate(const rga_buffer_t src, rga_buffer_t dst, int rotation, int sync = 1, int *release_fence_fd = NULL); 148 | 149 | /** 150 | * flip 151 | * 152 | * @param src 153 | * The input source image. 154 | * @param dst 155 | * The output destination image. 156 | * @param mode 157 | * IM_HAL_TRANSFORM_FLIP_H 158 | * IM_HAL_TRANSFORM_FLIP_V 159 | * @param sync 160 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 161 | * @param release_fence_fd 162 | * When 'sync == 0', the fence_fd used to identify the current job state 163 | * 164 | * @returns success or else negative error code. 165 | */ 166 | IM_API IM_STATUS imflip(const rga_buffer_t src, rga_buffer_t dst, int mode, int sync = 1, int *release_fence_fd = NULL); 167 | 168 | /** 169 | * 2-channel blend (SRC + DST -> DST or SRCA + SRCB -> DST) 170 | * 171 | * @param fg_image 172 | * The foreground image. 173 | * @param bg_image 174 | * The background image, which is also the output destination image. 175 | * @param mode 176 | * Port-Duff mode: 177 | * IM_ALPHA_BLEND_SRC 178 | * IM_ALPHA_BLEND_DST 179 | * IM_ALPHA_BLEND_SRC_OVER 180 | * IM_ALPHA_BLEND_DST_OVER 181 | * @param sync 182 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 183 | * @param release_fence_fd 184 | * When 'sync == 0', the fence_fd used to identify the current job state 185 | * 186 | * @returns success or else negative error code. 187 | */ 188 | IM_API IM_STATUS imblend(const rga_buffer_t fd_image, rga_buffer_t bg_image, int mode = IM_ALPHA_BLEND_SRC_OVER, int sync = 1, int *release_fence_fd = NULL); 189 | 190 | /** 191 | * 3-channel blend (SRC + DST -> DST or SRCA + SRCB -> DST) 192 | * 193 | * @param fg_image 194 | * The foreground image. 195 | * @param bg_image 196 | * The background image. 197 | * @param output_image 198 | * The output destination image. 199 | * @param mode 200 | * Port-Duff mode: 201 | * IM_ALPHA_BLEND_SRC 202 | * IM_ALPHA_BLEND_DST 203 | * IM_ALPHA_BLEND_SRC_OVER 204 | * IM_ALPHA_BLEND_DST_OVER 205 | * @param sync 206 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 207 | * @param release_fence_fd 208 | * When 'sync == 0', the fence_fd used to identify the current job state 209 | * 210 | * @returns success or else negative error code. 211 | */ 212 | IM_API IM_STATUS imcomposite(const rga_buffer_t srcA, const rga_buffer_t srcB, rga_buffer_t dst, int mode = IM_ALPHA_BLEND_SRC_OVER, int sync = 1, int *release_fence_fd = NULL); 213 | 214 | /** 215 | * color key 216 | * 217 | * @param fg_image 218 | * The foreground image. 219 | * @param bg_image 220 | * The background image, which is also the output destination image. 221 | * @param colorkey_range 222 | * The range of color key. 223 | * @param sync 224 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 225 | * 226 | * @returns success or else negative error code. 227 | */ 228 | IM_API IM_STATUS imcolorkey(const rga_buffer_t src, rga_buffer_t dst, im_colorkey_range range, int mode = IM_ALPHA_COLORKEY_NORMAL, int sync = 1, int *release_fence_fd = NULL); 229 | 230 | /** 231 | * OSD 232 | * 233 | * @param osd 234 | * The osd text block. 235 | * @param dst 236 | * The background image. 237 | * @param osd_rect 238 | * The rectangle on the source image that needs to be OSD. 239 | * @param osd_config 240 | * osd mode configuration. 241 | * @param sync 242 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 243 | * 244 | * @returns success or else negative error code. 245 | */ 246 | IM_API IM_STATUS imosd(const rga_buffer_t osd,const rga_buffer_t dst, 247 | const im_rect osd_rect, im_osd_t *osd_config, 248 | int sync = 1, int *release_fence_fd = NULL); 249 | 250 | /** 251 | * nn quantize 252 | * 253 | * @param src 254 | * The input source image. 255 | * @param dst 256 | * The output destination image. 257 | * @param nninfo 258 | * nn configuration 259 | * @param sync 260 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 261 | * 262 | * @returns success or else negative error code. 263 | */ 264 | IM_API IM_STATUS imquantize(const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info, int sync = 1, int *release_fence_fd = NULL); 265 | 266 | /** 267 | * ROP 268 | * 269 | * @param src 270 | * The input source image. 271 | * @param dst 272 | * The output destination image. 273 | * @param rop_code 274 | * The ROP opcode. 275 | * @param sync 276 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 277 | * 278 | * @returns success or else negative error code. 279 | */ 280 | IM_API IM_STATUS imrop(const rga_buffer_t src, rga_buffer_t dst, int rop_code, int sync = 1, int *release_fence_fd = NULL); 281 | 282 | /** 283 | * fill/reset/draw 284 | * 285 | * @param dst 286 | * The output destination image. 287 | * @param rect 288 | * The rectangle on the source image that needs to be filled with color. 289 | * @param color 290 | * The fill color value. 291 | * @param sync 292 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 293 | * 294 | * @returns success or else negative error code. 295 | */ 296 | IM_API IM_STATUS imfill(rga_buffer_t dst, im_rect rect, int color, int sync = 1, int *release_fence_fd = NULL); 297 | 298 | /** 299 | * fill array 300 | * 301 | * @param dst 302 | * The output destination image. 303 | * @param rect_array 304 | * The rectangle arrays on the source image that needs to be filled with color. 305 | * @param array_size 306 | * The size of rectangular area arrays. 307 | * @param color 308 | * The fill color value. 309 | * @param sync 310 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 311 | * 312 | * @returns success or else negative error code. 313 | */ 314 | IM_API IM_STATUS imfillArray(rga_buffer_t dst, im_rect *rect_array, int array_size, uint32_t color, int sync = 1, int *release_fence_fd = NULL); 315 | 316 | /** 317 | * fill rectangle 318 | * 319 | * @param dst 320 | * The output destination image. 321 | * @param rect 322 | * The rectangle on the source image that needs to be filled with color. 323 | * @param color 324 | * The fill color value. 325 | * @param thickness 326 | * Thickness of lines that make up the rectangle. Negative values, like -1, 327 | * mean that the function has to draw a filled rectangle. 328 | * @param sync 329 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 330 | * 331 | * @returns success or else negative error code. 332 | */ 333 | IM_API IM_STATUS imrectangle(rga_buffer_t dst, im_rect rect, 334 | uint32_t color, int thickness, 335 | int sync = 1, int *release_fence_fd = NULL); 336 | 337 | /** 338 | * fill rectangle array 339 | * 340 | * @param dst 341 | * The output destination image. 342 | * @param rect_array 343 | * The rectangle arrays on the source image that needs to be filled with color. 344 | * @param array_size 345 | * The size of rectangular area arrays. 346 | * @param color 347 | * The fill color value. 348 | * @param thickness 349 | * Thickness of lines that make up the rectangle. Negative values, like -1, 350 | * mean that the function has to draw a filled rectangle. 351 | * @param sync 352 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 353 | * 354 | * @returns success or else negative error code. 355 | */ 356 | IM_API IM_STATUS imrectangleArray(rga_buffer_t dst, im_rect *rect_array, int array_size, 357 | uint32_t color, int thickness, 358 | int sync = 1, int *release_fence_fd = NULL); 359 | 360 | /** 361 | * MOSAIC 362 | * 363 | * @param image 364 | * The output destination image. 365 | * @param rect 366 | * The rectangle on the source image that needs to be mosaicked. 367 | * @param mosaic_mode 368 | * mosaic block width configuration: 369 | * IM_MOSAIC_8 370 | * IM_MOSAIC_16 371 | * IM_MOSAIC_32 372 | * IM_MOSAIC_64 373 | * IM_MOSAIC_128 374 | * @param sync 375 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 376 | * 377 | * @returns success or else negative error code. 378 | */ 379 | IM_API IM_STATUS immosaic(const rga_buffer_t image, im_rect rect, int mosaic_mode, int sync = 1, int *release_fence_fd = NULL); 380 | 381 | /** 382 | * MOSAIC array 383 | * 384 | * @param image 385 | * The output destination image. 386 | * @param rect_array 387 | * The rectangle arrays on the source image that needs to be filled with color. 388 | * @param array_size 389 | * The size of rectangular area arrays. 390 | * @param mosaic_mode 391 | * mosaic block width configuration: 392 | * IM_MOSAIC_8 393 | * IM_MOSAIC_16 394 | * IM_MOSAIC_32 395 | * IM_MOSAIC_64 396 | * IM_MOSAIC_128 397 | * @param sync 398 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 399 | * 400 | * @returns success or else negative error code. 401 | */ 402 | IM_API IM_STATUS immosaicArray(const rga_buffer_t image, im_rect *rect_array, int array_size, int mosaic_mode, int sync = 1, int *release_fence_fd = NULL); 403 | 404 | /** 405 | * palette 406 | * 407 | * @param src 408 | * The input source image. 409 | * @param dst 410 | * The output destination image. 411 | * @param lut 412 | * The LUT table. 413 | * @param sync 414 | * When 'sync == 1', wait for the operation to complete and return, otherwise return directly. 415 | * 416 | * @returns success or else negative error code. 417 | */ 418 | IM_API IM_STATUS impalette(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut, int sync = 1, int *release_fence_fd = NULL); 419 | 420 | /** 421 | * process for single task mode 422 | * 423 | * @param src 424 | * The input source image and is also the foreground image in blend. 425 | * @param dst 426 | * The output destination image and is also the foreground image in blend. 427 | * @param pat 428 | * The foreground image, or a LUT table. 429 | * @param srect 430 | * The rectangle on the src channel image that needs to be processed. 431 | * @param drect 432 | * The rectangle on the dst channel image that needs to be processed. 433 | * @param prect 434 | * The rectangle on the pat channel image that needs to be processed. 435 | * @param opt 436 | * The image processing options configuration. 437 | * @param usage 438 | * The image processing usage. 439 | * 440 | * @returns success or else negative error code. 441 | */ 442 | IM_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat, 443 | im_rect srect, im_rect drect, im_rect prect, 444 | int acquire_fence_fd, int *release_fence_fd, 445 | im_opt_t *opt_ptr, int usage); 446 | 447 | /** 448 | * make border 449 | * 450 | * @param src 451 | * The input source image. 452 | * @param dst 453 | * The output destination image. 454 | * @param top 455 | * the top pixels 456 | * @param bottom 457 | * the bottom pixels 458 | * @param left 459 | * the left pixels 460 | * @param right 461 | * the right pixels 462 | * @param border_type 463 | * Border type. 464 | * @param value 465 | * The pixel value at which the border is filled. 466 | * 467 | * @returns success or else negative error code. 468 | */ 469 | IM_API IM_STATUS immakeBorder(rga_buffer_t src, rga_buffer_t dst, 470 | int top, int bottom, int left, int right, 471 | int border_type, int value = 0, 472 | int sync = 1, int acquir_fence_fd = -1, int *release_fence_fd = NULL); 473 | 474 | #endif /* #ifdef __cplusplus */ 475 | 476 | IM_C_API IM_STATUS immosaic(const rga_buffer_t image, im_rect rect, int mosaic_mode, int sync); 477 | IM_C_API IM_STATUS imosd(const rga_buffer_t osd,const rga_buffer_t dst, 478 | const im_rect osd_rect, im_osd_t *osd_config, int sync); 479 | IM_C_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat, 480 | im_rect srect, im_rect drect, im_rect prect, int usage); 481 | 482 | /* Start: Symbols reserved for compatibility with macro functions */ 483 | IM_C_API IM_STATUS imcopy_t(const rga_buffer_t src, rga_buffer_t dst, int sync); 484 | IM_C_API IM_STATUS imresize_t(const rga_buffer_t src, rga_buffer_t dst, double fx, double fy, int interpolation, int sync); 485 | IM_C_API IM_STATUS imcrop_t(const rga_buffer_t src, rga_buffer_t dst, im_rect rect, int sync); 486 | IM_C_API IM_STATUS imtranslate_t(const rga_buffer_t src, rga_buffer_t dst, int x, int y, int sync); 487 | IM_C_API IM_STATUS imcvtcolor_t(rga_buffer_t src, rga_buffer_t dst, int sfmt, int dfmt, int mode, int sync); 488 | IM_C_API IM_STATUS imrotate_t(const rga_buffer_t src, rga_buffer_t dst, int rotation, int sync); 489 | IM_C_API IM_STATUS imflip_t (const rga_buffer_t src, rga_buffer_t dst, int mode, int sync); 490 | IM_C_API IM_STATUS imblend_t(const rga_buffer_t srcA, const rga_buffer_t srcB, rga_buffer_t dst, int mode, int sync); 491 | IM_C_API IM_STATUS imcolorkey_t(const rga_buffer_t src, rga_buffer_t dst, im_colorkey_range range, int mode, int sync); 492 | IM_C_API IM_STATUS imquantize_t(const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info, int sync); 493 | IM_C_API IM_STATUS imrop_t(const rga_buffer_t src, rga_buffer_t dst, int rop_code, int sync); 494 | IM_C_API IM_STATUS imfill_t(rga_buffer_t dst, im_rect rect, int color, int sync); 495 | IM_C_API IM_STATUS impalette_t(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut, int sync); 496 | /* End: Symbols reserved for compatibility with macro functions */ 497 | 498 | #ifndef __cplusplus 499 | 500 | #define RGA_GET_MIN(n1, n2) ((n1) < (n2) ? (n1) : (n2)) 501 | 502 | /** 503 | * copy 504 | * 505 | * @param src 506 | * @param dst 507 | * @param sync 508 | * wait until operation complete 509 | * 510 | * @returns success or else negative error code. 511 | */ 512 | #define imcopy(src, dst, ...) \ 513 | ({ \ 514 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 515 | int __args[] = {__VA_ARGS__}; \ 516 | int __argc = sizeof(__args)/sizeof(int); \ 517 | if (__argc == 0) { \ 518 | __ret = imcopy_t(src, dst, 1); \ 519 | } else if (__argc == 1){ \ 520 | __ret = imcopy_t(src, dst, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 521 | } else { \ 522 | __ret = IM_STATUS_INVALID_PARAM; \ 523 | printf("invalid parameter\n"); \ 524 | } \ 525 | __ret; \ 526 | }) 527 | 528 | /** 529 | * Resize 530 | * 531 | * @param src 532 | * @param dst 533 | * @param fx 534 | * @param fy 535 | * @param interpolation 536 | * @param sync 537 | * wait until operation complete 538 | * 539 | * @returns success or else negative error code. 540 | */ 541 | #define imresize(src, dst, ...) \ 542 | ({ \ 543 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 544 | double __args[] = {__VA_ARGS__}; \ 545 | int __argc = sizeof(__args)/sizeof(double); \ 546 | if (__argc == 0) { \ 547 | __ret = imresize_t(src, dst, 0, 0, INTER_LINEAR, 1); \ 548 | } else if (__argc == 2){ \ 549 | __ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], INTER_LINEAR, 1); \ 550 | } else if (__argc == 3){ \ 551 | __ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], (int)__args[RGA_GET_MIN(__argc, 2)], 1); \ 552 | } else if (__argc == 4){ \ 553 | __ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], (int)__args[RGA_GET_MIN(__argc, 2)], (int)__args[RGA_GET_MIN(__argc, 3)]); \ 554 | } else { \ 555 | __ret = IM_STATUS_INVALID_PARAM; \ 556 | printf("invalid parameter\n"); \ 557 | } \ 558 | __ret; \ 559 | }) 560 | 561 | #define impyramid(src, dst, direction) \ 562 | imresize_t(src, \ 563 | dst, \ 564 | direction == IM_UP_SCALE ? 0.5 : 2, \ 565 | direction == IM_UP_SCALE ? 0.5 : 2, \ 566 | INTER_LINEAR, 1) 567 | 568 | /** 569 | * format convert 570 | * 571 | * @param src 572 | * @param dst 573 | * @param sfmt 574 | * @param dfmt 575 | * @param mode 576 | * color space mode: IM_COLOR_SPACE_MODE 577 | * @param sync 578 | * wait until operation complete 579 | * 580 | * @returns success or else negative error code. 581 | */ 582 | #define imcvtcolor(src, dst, sfmt, dfmt, ...) \ 583 | ({ \ 584 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 585 | int __args[] = {__VA_ARGS__}; \ 586 | int __argc = sizeof(__args)/sizeof(int); \ 587 | if (__argc == 0) { \ 588 | __ret = imcvtcolor_t(src, dst, sfmt, dfmt, IM_COLOR_SPACE_DEFAULT, 1); \ 589 | } else if (__argc == 1){ \ 590 | __ret = imcvtcolor_t(src, dst, sfmt, dfmt, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \ 591 | } else if (__argc == 2){ \ 592 | __ret = imcvtcolor_t(src, dst, sfmt, dfmt, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \ 593 | } else { \ 594 | __ret = IM_STATUS_INVALID_PARAM; \ 595 | printf("invalid parameter\n"); \ 596 | } \ 597 | __ret; \ 598 | }) 599 | 600 | /** 601 | * Crop 602 | * 603 | * @param src 604 | * @param dst 605 | * @param rect 606 | * @param sync 607 | * wait until operation complete 608 | * 609 | * @returns success or else negative error code. 610 | */ 611 | #define imcrop(src, dst, rect, ...) \ 612 | ({ \ 613 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 614 | int __args[] = {__VA_ARGS__}; \ 615 | int __argc = sizeof(__args)/sizeof(int); \ 616 | if (__argc == 0) { \ 617 | __ret = imcrop_t(src, dst, rect, 1); \ 618 | } else if (__argc == 1){ \ 619 | __ret = imcrop_t(src, dst, rect, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 620 | } else { \ 621 | __ret = IM_STATUS_INVALID_PARAM; \ 622 | printf("invalid parameter\n"); \ 623 | } \ 624 | __ret; \ 625 | }) 626 | 627 | /** 628 | * translate 629 | * 630 | * @param src 631 | * @param dst 632 | * @param x 633 | * @param y 634 | * @param sync 635 | * wait until operation complete 636 | * 637 | * @returns success or else negative error code. 638 | */ 639 | #define imtranslate(src, dst, x, y, ...) \ 640 | ({ \ 641 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 642 | int __args[] = {__VA_ARGS__}; \ 643 | int __argc = sizeof(__args)/sizeof(int); \ 644 | if (__argc == 0) { \ 645 | __ret = imtranslate_t(src, dst, x, y, 1); \ 646 | } else if (__argc == 1){ \ 647 | __ret = imtranslate_t(src, dst, x, y, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 648 | } else { \ 649 | __ret = IM_STATUS_INVALID_PARAM; \ 650 | printf("invalid parameter\n"); \ 651 | } \ 652 | __ret; \ 653 | }) 654 | 655 | /** 656 | * rotation 657 | * 658 | * @param src 659 | * @param dst 660 | * @param rotation 661 | * IM_HAL_TRANSFORM_ROT_90 662 | * IM_HAL_TRANSFORM_ROT_180 663 | * IM_HAL_TRANSFORM_ROT_270 664 | * @param sync 665 | * wait until operation complete 666 | * 667 | * @returns success or else negative error code. 668 | */ 669 | #define imrotate(src, dst, rotation, ...) \ 670 | ({ \ 671 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 672 | int __args[] = {__VA_ARGS__}; \ 673 | int __argc = sizeof(__args)/sizeof(int); \ 674 | if (__argc == 0) { \ 675 | __ret = imrotate_t(src, dst, rotation, 1); \ 676 | } else if (__argc == 1){ \ 677 | __ret = imrotate_t(src, dst, rotation, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 678 | } else { \ 679 | __ret = IM_STATUS_INVALID_PARAM; \ 680 | printf("invalid parameter\n"); \ 681 | } \ 682 | __ret; \ 683 | }) 684 | 685 | 686 | /** 687 | * flip 688 | * 689 | * @param src 690 | * @param dst 691 | * @param mode 692 | * IM_HAL_TRANSFORM_FLIP_H 693 | * IM_HAL_TRANSFORM_FLIP_V 694 | * @param sync 695 | * wait until operation complete 696 | * 697 | * @returns success or else negative error code. 698 | */ 699 | #define imflip(src, dst, mode, ...) \ 700 | ({ \ 701 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 702 | int __args[] = {__VA_ARGS__}; \ 703 | int __argc = sizeof(__args)/sizeof(int); \ 704 | if (__argc == 0) { \ 705 | __ret = imflip_t(src, dst, mode, 1); \ 706 | } else if (__argc == 1){ \ 707 | __ret = imflip_t(src, dst, mode, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 708 | } else { \ 709 | __ret = IM_STATUS_INVALID_PARAM; \ 710 | printf("invalid parameter\n"); \ 711 | } \ 712 | __ret; \ 713 | }) 714 | 715 | /** 716 | * blend (SRC + DST -> DST or SRCA + SRCB -> DST) 717 | * 718 | * @param srcA 719 | * @param srcB can be NULL. 720 | * @param dst 721 | * @param mode 722 | * IM_ALPHA_BLEND_MODE 723 | * @param sync 724 | * wait until operation complete 725 | * 726 | * @returns success or else negative error code. 727 | */ 728 | #define imblend(srcA, dst, ...) \ 729 | ({ \ 730 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 731 | rga_buffer_t srcB; \ 732 | memset(&srcB, 0x00, sizeof(rga_buffer_t)); \ 733 | int __args[] = {__VA_ARGS__}; \ 734 | int __argc = sizeof(__args)/sizeof(int); \ 735 | if (__argc == 0) { \ 736 | __ret = imblend_t(srcA, srcB, dst, IM_ALPHA_BLEND_SRC_OVER, 1); \ 737 | } else if (__argc == 1){ \ 738 | __ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \ 739 | } else if (__argc == 2){ \ 740 | __ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \ 741 | } else { \ 742 | __ret = IM_STATUS_INVALID_PARAM; \ 743 | printf("invalid parameter\n"); \ 744 | } \ 745 | __ret; \ 746 | }) 747 | #define imcomposite(srcA, srcB, dst, ...) \ 748 | ({ \ 749 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 750 | int __args[] = {__VA_ARGS__}; \ 751 | int __argc = sizeof(__args)/sizeof(int); \ 752 | if (__argc == 0) { \ 753 | __ret = imblend_t(srcA, srcB, dst, IM_ALPHA_BLEND_SRC_OVER, 1); \ 754 | } else if (__argc == 1){ \ 755 | __ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \ 756 | } else if (__argc == 2){ \ 757 | __ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \ 758 | } else { \ 759 | __ret = IM_STATUS_INVALID_PARAM; \ 760 | printf("invalid parameter\n"); \ 761 | } \ 762 | __ret; \ 763 | }) 764 | 765 | /** 766 | * color key 767 | * 768 | * @param src 769 | * @param dst 770 | * @param colorkey_range 771 | * max color 772 | * min color 773 | * @param sync 774 | * wait until operation complete 775 | * 776 | * @returns success or else negative error code. 777 | */ 778 | #define imcolorkey(src, dst, range, ...) \ 779 | ({ \ 780 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 781 | int __args[] = {__VA_ARGS__}; \ 782 | int __argc = sizeof(__args)/sizeof(int); \ 783 | if (__argc == 0) { \ 784 | __ret = imcolorkey_t(src, dst, range, IM_ALPHA_COLORKEY_NORMAL, 1); \ 785 | } else if (__argc == 1){ \ 786 | __ret = imcolorkey_t(src, dst, range, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \ 787 | } else if (__argc == 2){ \ 788 | __ret = imcolorkey_t(src, dst, range, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \ 789 | } else { \ 790 | __ret = IM_STATUS_INVALID_PARAM; \ 791 | printf("invalid parameter\n"); \ 792 | } \ 793 | __ret; \ 794 | }) 795 | 796 | /** 797 | * nn quantize 798 | * 799 | * @param src 800 | * @param dst 801 | * @param nninfo 802 | * @param sync 803 | * wait until operation complete 804 | * 805 | * @returns success or else negative error code. 806 | */ 807 | #define imquantize(src, dst, nn_info, ...) \ 808 | ({ \ 809 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 810 | int __args[] = {__VA_ARGS__}; \ 811 | int __argc = sizeof(__args)/sizeof(int); \ 812 | if (__argc == 0) { \ 813 | __ret = imquantize_t(src, dst, nn_info, 1); \ 814 | } else if (__argc == 1){ \ 815 | __ret = imquantize_t(src, dst, nn_info, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 816 | } else { \ 817 | __ret = IM_STATUS_INVALID_PARAM; \ 818 | printf("invalid parameter\n"); \ 819 | } \ 820 | __ret; \ 821 | }) 822 | 823 | 824 | /** 825 | * ROP 826 | * 827 | * @param src 828 | * @param dst 829 | * @param rop_code 830 | * @param sync 831 | * wait until operation complete 832 | * 833 | * @returns success or else negative error code. 834 | */ 835 | #define imrop(src, dst, rop_code, ...) \ 836 | ({ \ 837 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 838 | int __args[] = {__VA_ARGS__}; \ 839 | int __argc = sizeof(__args)/sizeof(int); \ 840 | if (__argc == 0) { \ 841 | __ret = imrop_t(src, dst, rop_code, 1); \ 842 | } else if (__argc == 1){ \ 843 | __ret = imrop_t(src, dst, rop_code, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 844 | } else { \ 845 | __ret = IM_STATUS_INVALID_PARAM; \ 846 | printf("invalid parameter\n"); \ 847 | } \ 848 | __ret; \ 849 | }) 850 | 851 | /** 852 | * fill/reset/draw 853 | * 854 | * @param src 855 | * @param dst 856 | * @param rect 857 | * @param color 858 | * @param sync 859 | * wait until operation complete 860 | * 861 | * @returns success or else negative error code. 862 | */ 863 | #define imfill(buf, rect, color, ...) \ 864 | ({ \ 865 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 866 | int __args[] = {__VA_ARGS__}; \ 867 | int __argc = sizeof(__args)/sizeof(int); \ 868 | if (__argc == 0) { \ 869 | __ret = imfill_t(buf, rect, color, 1); \ 870 | } else if (__argc == 1){ \ 871 | __ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 872 | } else { \ 873 | __ret = IM_STATUS_INVALID_PARAM; \ 874 | printf("invalid parameter\n"); \ 875 | } \ 876 | __ret; \ 877 | }) 878 | 879 | #define imreset(buf, rect, color, ...) \ 880 | ({ \ 881 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 882 | int __args[] = {__VA_ARGS__}; \ 883 | int __argc = sizeof(__args)/sizeof(int); \ 884 | if (__argc == 0) { \ 885 | __ret = imfill_t(buf, rect, color, 1); \ 886 | } else if (__argc == 1){ \ 887 | __ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 888 | } else { \ 889 | __ret = IM_STATUS_INVALID_PARAM; \ 890 | printf("invalid parameter\n"); \ 891 | } \ 892 | __ret; \ 893 | }) 894 | 895 | #define imdraw(buf, rect, color, ...) \ 896 | ({ \ 897 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 898 | int __args[] = {__VA_ARGS__}; \ 899 | int __argc = sizeof(__args)/sizeof(int); \ 900 | if (__argc == 0) { \ 901 | __ret = imfill_t(buf, rect, color, 1); \ 902 | } else if (__argc == 1){ \ 903 | __ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 904 | } else { \ 905 | __ret = IM_STATUS_INVALID_PARAM; \ 906 | printf("invalid parameter\n"); \ 907 | } \ 908 | __ret; \ 909 | }) 910 | 911 | /** 912 | * palette 913 | * 914 | * @param src 915 | * @param dst 916 | * @param lut 917 | * @param sync 918 | * wait until operation complete 919 | * 920 | * @returns success or else negative error code. 921 | */ 922 | #define impalette(src, dst, lut, ...) \ 923 | ({ \ 924 | IM_STATUS __ret = IM_STATUS_SUCCESS; \ 925 | int __args[] = {__VA_ARGS__}; \ 926 | int __argc = sizeof(__args)/sizeof(int); \ 927 | if (__argc == 0) { \ 928 | __ret = impalette_t(src, dst, lut, 1); \ 929 | } else if (__argc == 1){ \ 930 | __ret = impalette_t(src, dst, lut, (int)__args[RGA_GET_MIN(__argc, 0)]); \ 931 | } else { \ 932 | __ret = IM_STATUS_INVALID_PARAM; \ 933 | printf("invalid parameter\n"); \ 934 | } \ 935 | __ret; \ 936 | }) 937 | /* End define IM2D macro API */ 938 | #endif 939 | 940 | #endif /* #ifndef _im2d_single_h_ */ -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d_task.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Cerf Yu 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | #ifndef _im2d_task_h_ 19 | #define _im2d_task_h_ 20 | 21 | #include "im2d_type.h" 22 | 23 | #ifdef __cplusplus 24 | 25 | /** 26 | * Create an rga job 27 | * 28 | * @param flags 29 | * Some configuration flags for this job 30 | * 31 | * @returns job handle. 32 | */ 33 | IM_API im_job_handle_t imbeginJob(uint64_t flags = 0); 34 | 35 | /** 36 | * Submit and run an rga job 37 | * 38 | * @param job_handle 39 | * This is the job handle that will be submitted. 40 | * @param sync_mode 41 | * run mode: 42 | * IM_SYNC 43 | * IM_ASYNC 44 | * @param acquire_fence_fd 45 | * @param release_fence_fd 46 | * 47 | * @returns success or else negative error code. 48 | */ 49 | IM_API IM_STATUS imendJob(im_job_handle_t job_handle, 50 | int sync_mode = IM_SYNC, 51 | int acquire_fence_fd = 0, int *release_fence_fd = NULL); 52 | 53 | /** 54 | * Cancel and delete an rga job 55 | * 56 | * @param job_handle 57 | * This is the job handle that will be cancelled. 58 | * 59 | * @returns success or else negative error code. 60 | */ 61 | IM_API IM_STATUS imcancelJob(im_job_handle_t job_handle); 62 | 63 | /** 64 | * Add copy task 65 | * 66 | * @param job_handle 67 | * Insert the task into the job handle. 68 | * @param src 69 | * The input source image. 70 | * @param dst 71 | * The output destination image. 72 | * 73 | * @returns success or else negative error code. 74 | */ 75 | IM_API IM_STATUS imcopyTask(im_job_handle_t job_handle, const rga_buffer_t src, rga_buffer_t dst); 76 | 77 | /** 78 | * Add resize task 79 | * 80 | * @param job_handle 81 | * Insert the task into the job handle. 82 | * @param src 83 | * The input source image. 84 | * @param dst 85 | * The output destination image. 86 | * @param fx 87 | * X-direction resize factor. 88 | * @param fy 89 | * X-direction resize factor. 90 | * @param interpolation 91 | * Interpolation formula(Only RGA1 support). 92 | * 93 | * @returns success or else negative error code. 94 | */ 95 | IM_API IM_STATUS imresizeTask(im_job_handle_t job_handle, 96 | const rga_buffer_t src, rga_buffer_t dst, 97 | double fx = 0, double fy = 0, 98 | int interpolation = 0); 99 | 100 | /** 101 | * Add crop task 102 | * 103 | * @param job_handle 104 | * Insert the task into the job handle. 105 | * @param src 106 | * The input source image. 107 | * @param dst 108 | * The output destination image. 109 | * @param rect 110 | * The rectangle on the source image that needs to be cropped. 111 | * 112 | * @returns success or else negative error code. 113 | */ 114 | IM_API IM_STATUS imcropTask(im_job_handle_t job_handle, 115 | const rga_buffer_t src, rga_buffer_t dst, im_rect rect); 116 | 117 | /** 118 | * Add translate task 119 | * 120 | * @param job_handle 121 | * Insert the task into the job handle. 122 | * @param src 123 | * The input source image. 124 | * @param dst 125 | * The output destination image. 126 | * @param x 127 | * Output the coordinates of the starting point in the X-direction of the destination image. 128 | * @param y 129 | * Output the coordinates of the starting point in the Y-direction of the destination image. 130 | * 131 | * @returns success or else negative error code. 132 | */ 133 | IM_API IM_STATUS imtranslateTask(im_job_handle_t job_handle, 134 | const rga_buffer_t src, rga_buffer_t dst, int x, int y); 135 | 136 | /** 137 | * Add format convert task 138 | * 139 | * @param job_handle 140 | * Insert the task into the job handle. 141 | * @param src 142 | * The input source image. 143 | * @param dst 144 | * The output destination image. 145 | * @param sfmt 146 | * The source image format. 147 | * @param dfmt 148 | * The destination image format. 149 | * @param mode 150 | * color space mode: 151 | * IM_YUV_TO_RGB_BT601_LIMIT 152 | * IM_YUV_TO_RGB_BT601_FULL 153 | * IM_YUV_TO_RGB_BT709_LIMIT 154 | * IM_RGB_TO_YUV_BT601_FULL 155 | * IM_RGB_TO_YUV_BT601_LIMIT 156 | * IM_RGB_TO_YUV_BT709_LIMIT 157 | * 158 | * @returns success or else negative error code. 159 | */ 160 | IM_API IM_STATUS imcvtcolorTask(im_job_handle_t job_handle, 161 | rga_buffer_t src, rga_buffer_t dst, 162 | int sfmt, int dfmt, int mode = IM_COLOR_SPACE_DEFAULT); 163 | 164 | /** 165 | * Add rotation task 166 | * 167 | * @param job_handle 168 | * Insert the task into the job handle. 169 | * @param src 170 | * The input source image. 171 | * @param dst 172 | * The output destination image. 173 | * @param rotation 174 | * IM_HAL_TRANSFORM_ROT_90 175 | * IM_HAL_TRANSFORM_ROT_180 176 | * IM_HAL_TRANSFORM_ROT_270 177 | * 178 | * @returns success or else negative error code. 179 | */ 180 | IM_API IM_STATUS imrotateTask(im_job_handle_t job_handle, 181 | const rga_buffer_t src, rga_buffer_t dst, int rotation); 182 | 183 | /** 184 | * Add flip task 185 | * 186 | * @param job_handle 187 | * Insert the task into the job handle. 188 | * @param src 189 | * The input source image. 190 | * @param dst 191 | * The output destination image. 192 | * @param mode 193 | * IM_HAL_TRANSFORM_FLIP_H 194 | * IM_HAL_TRANSFORM_FLIP_V 195 | * 196 | * @returns success or else negative error code. 197 | */ 198 | IM_API IM_STATUS imflipTask(im_job_handle_t job_handle, 199 | const rga_buffer_t src, rga_buffer_t dst, int mode); 200 | 201 | /** 202 | * Add blend(SRC + DST -> DST) task 203 | * 204 | * @param job_handle 205 | * Insert the task into the job handle. 206 | * @param fg_image 207 | * The foreground image. 208 | * @param bg_image 209 | * The background image, which is also the output destination image. 210 | * @param mode 211 | * Port-Duff mode: 212 | * IM_ALPHA_BLEND_SRC 213 | * IM_ALPHA_BLEND_DST 214 | * IM_ALPHA_BLEND_SRC_OVER 215 | * IM_ALPHA_BLEND_DST_OVER 216 | * 217 | * @returns success or else negative error code. 218 | */ 219 | IM_API IM_STATUS imblendTask(im_job_handle_t job_handle, 220 | const rga_buffer_t fg_image, rga_buffer_t bg_image, 221 | int mode = IM_ALPHA_BLEND_SRC_OVER); 222 | 223 | /** 224 | * Add composite(SRCA + SRCB -> DST) task 225 | * 226 | * @param job_handle 227 | * Insert the task into the job handle. 228 | * @param fg_image 229 | * The foreground image. 230 | * @param bg_image 231 | * The background image. 232 | * @param output_image 233 | * The output destination image. 234 | * @param mode 235 | * Port-Duff mode: 236 | * IM_ALPHA_BLEND_SRC 237 | * IM_ALPHA_BLEND_DST 238 | * IM_ALPHA_BLEND_SRC_OVER 239 | * IM_ALPHA_BLEND_DST_OVER 240 | * 241 | * @returns success or else negative error code. 242 | */ 243 | IM_API IM_STATUS imcompositeTask(im_job_handle_t job_handle, 244 | const rga_buffer_t fg_image, const rga_buffer_t bg_image, 245 | rga_buffer_t output_image, 246 | int mode = IM_ALPHA_BLEND_SRC_OVER); 247 | 248 | /** 249 | * Add color key task 250 | * 251 | * @param job_handle 252 | * Insert the task into the job handle. 253 | * @param fg_image 254 | * The foreground image. 255 | * @param bg_image 256 | * The background image, which is also the output destination image. 257 | * @param colorkey_range 258 | * The range of color key. 259 | * 260 | * @returns success or else negative error code. 261 | */ 262 | IM_API IM_STATUS imcolorkeyTask(im_job_handle_t job_handle, 263 | const rga_buffer_t fg_image, rga_buffer_t bg_image, 264 | im_colorkey_range range, int mode = IM_ALPHA_COLORKEY_NORMAL); 265 | 266 | /** 267 | * Add OSD task 268 | * 269 | * @param job_handle 270 | * Insert the task into the job handle. 271 | * @param osd 272 | * The osd text block. 273 | * @param dst 274 | * The background image. 275 | * @param osd_rect 276 | * The rectangle on the source image that needs to be OSD. 277 | * @param osd_config 278 | * osd mode configuration. 279 | * 280 | * @returns success or else negative error code. 281 | */ 282 | IM_API IM_STATUS imosdTask(im_job_handle_t job_handle, 283 | const rga_buffer_t osd,const rga_buffer_t bg_image, 284 | const im_rect osd_rect, im_osd_t *osd_config); 285 | 286 | /** 287 | * Add nn quantize task 288 | * 289 | * @param job_handle 290 | * Insert the task into the job handle. 291 | * @param src 292 | * The input source image. 293 | * @param dst 294 | * The output destination image. 295 | * @param nninfo 296 | * nn configuration 297 | * 298 | * @returns success or else negative error code. 299 | */ 300 | IM_API IM_STATUS imquantizeTask(im_job_handle_t job_handle, 301 | const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info); 302 | 303 | /** 304 | * Add ROP task 305 | * 306 | * @param job_handle 307 | * Insert the task into the job handle. 308 | * @param src 309 | * The input source image. 310 | * @param dst 311 | * The output destination image. 312 | * @param rop_code 313 | * The ROP opcode. 314 | * 315 | * @returns success or else negative error code. 316 | */ 317 | IM_API IM_STATUS imropTask(im_job_handle_t job_handle, 318 | const rga_buffer_t src, rga_buffer_t dst, int rop_code); 319 | 320 | /** 321 | * Add color fill task 322 | * 323 | * @param job_handle 324 | * Insert the task into the job handle. 325 | * @param dst 326 | * The output destination image. 327 | * @param rect 328 | * The rectangle on the source image that needs to be filled with color. 329 | * @param color 330 | * The fill color value. 331 | * 332 | * @returns success or else negative error code. 333 | */ 334 | IM_API IM_STATUS imfillTask(im_job_handle_t job_handle, rga_buffer_t dst, im_rect rect, uint32_t color); 335 | 336 | /** 337 | * Add color fill task array 338 | * 339 | * @param job_handle 340 | * Insert the task into the job handle. 341 | * @param dst 342 | * The output destination image. 343 | * @param rect_array 344 | * The rectangle arrays on the source image that needs to be filled with color. 345 | * @param array_size 346 | * The size of rectangular area arrays. 347 | * @param color 348 | * The fill color value. 349 | * 350 | * @returns success or else negative error code. 351 | */ 352 | IM_API IM_STATUS imfillTaskArray(im_job_handle_t job_handle, 353 | rga_buffer_t dst, 354 | im_rect *rect_array, int array_size, uint32_t color); 355 | 356 | /** 357 | * Add fill rectangle task 358 | * 359 | * @param job_handle 360 | * Insert the task into the job handle. 361 | * @param dst 362 | * The output destination image. 363 | * @param rect 364 | * The rectangle on the source image that needs to be filled with color. 365 | * @param color 366 | * The fill color value. 367 | * @param thickness 368 | * Thickness of lines that make up the rectangle. Negative values, like -1, 369 | * mean that the function has to draw a filled rectangle. 370 | * 371 | * @returns success or else negative error code. 372 | */ 373 | IM_API IM_STATUS imrectangleTask(im_job_handle_t job_handle, 374 | rga_buffer_t dst, 375 | im_rect rect, 376 | uint32_t color, int thickness); 377 | 378 | /** 379 | * Add fill rectangle task array 380 | * 381 | * @param job_handle 382 | * Insert the task into the job handle. 383 | * @param dst 384 | * The output destination image. 385 | * @param rect_array 386 | * The rectangle arrays on the source image that needs to be filled with color. 387 | * @param array_size 388 | * The size of rectangular area arrays. 389 | * @param color 390 | * The fill color value. 391 | * @param thickness 392 | * Thickness of lines that make up the rectangle. Negative values, like -1, 393 | * mean that the function has to draw a filled rectangle. 394 | * 395 | * @returns success or else negative error code. 396 | */ 397 | IM_API IM_STATUS imrectangleTaskArray(im_job_handle_t job_handle, 398 | rga_buffer_t dst, 399 | im_rect *rect_array, int array_size, 400 | uint32_t color, int thickness); 401 | 402 | /** 403 | * Add mosaic task 404 | * 405 | * @param job_handle 406 | * Insert the task into the job handle. 407 | * @param image 408 | * The output destination image. 409 | * @param rect 410 | * The rectangle on the source image that needs to be mosaicked. 411 | * @param mosaic_mode 412 | * mosaic block width configuration: 413 | * IM_MOSAIC_8 414 | * IM_MOSAIC_16 415 | * IM_MOSAIC_32 416 | * IM_MOSAIC_64 417 | * IM_MOSAIC_128 418 | * 419 | * @returns success or else negative error code. 420 | */ 421 | IM_API IM_STATUS immosaicTask(im_job_handle_t job_handle, 422 | const rga_buffer_t image, im_rect rect, int mosaic_mode); 423 | 424 | /** 425 | * Add mosaic task 426 | * 427 | * @param job_handle 428 | * Insert the task into the job handle. 429 | * @param image 430 | * The output destination image. 431 | * @param rect_array 432 | * The rectangle arrays on the source image that needs to be filled with color. 433 | * @param array_size 434 | * The size of rectangular area arrays. 435 | * @param mosaic_mode 436 | * mosaic block width configuration: 437 | * IM_MOSAIC_8 438 | * IM_MOSAIC_16 439 | * IM_MOSAIC_32 440 | * IM_MOSAIC_64 441 | * IM_MOSAIC_128 442 | * 443 | * @returns success or else negative error code. 444 | */ 445 | IM_API IM_STATUS immosaicTaskArray(im_job_handle_t job_handle, 446 | const rga_buffer_t image, 447 | im_rect *rect_array, int array_size, int mosaic_mode); 448 | 449 | /** 450 | * Add palette task 451 | * 452 | * @param job_handle 453 | * Insert the task into the job handle. 454 | * @param src 455 | * The input source image. 456 | * @param dst 457 | * The output destination image. 458 | * @param lut 459 | * The LUT table. 460 | * 461 | * @returns success or else negative error code. 462 | */ 463 | IM_API IM_STATUS impaletteTask(im_job_handle_t job_handle, 464 | rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut); 465 | 466 | /** 467 | * Add process task 468 | * 469 | * @param job_handle 470 | * Insert the task into the job handle. 471 | * @param src 472 | * The input source image and is also the foreground image in blend. 473 | * @param dst 474 | * The output destination image and is also the foreground image in blend. 475 | * @param pat 476 | * The foreground image, or a LUT table. 477 | * @param srect 478 | * The rectangle on the src channel image that needs to be processed. 479 | * @param drect 480 | * The rectangle on the dst channel image that needs to be processed. 481 | * @param prect 482 | * The rectangle on the pat channel image that needs to be processed. 483 | * @param opt 484 | * The image processing options configuration. 485 | * @param usage 486 | * The image processing usage. 487 | * 488 | * @returns success or else negative error code. 489 | */ 490 | IM_API IM_STATUS improcessTask(im_job_handle_t job_handle, 491 | rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat, 492 | im_rect srect, im_rect drect, im_rect prect, 493 | im_opt_t *opt_ptr, int usage); 494 | 495 | #endif /* #ifdef __cplusplus */ 496 | 497 | #endif /* #ifndef _im2d_task_h_ */ 498 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Cerf Yu 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _RGA_IM2D_TYPE_H_ 20 | #define _RGA_IM2D_TYPE_H_ 21 | 22 | #include 23 | 24 | #include "rga.h" 25 | 26 | #define IM_API /* define API export as needed */ 27 | 28 | #ifdef __cplusplus 29 | #define IM_C_API extern "C" 30 | #define IM_EXPORT_API extern "C" 31 | #else 32 | #define IM_C_API 33 | #define IM_EXPORT_API 34 | #endif 35 | 36 | #ifdef __cplusplus 37 | #define DEFAULT_INITIALIZER(x) = x 38 | #else 39 | #define DEFAULT_INITIALIZER(x) 40 | #endif 41 | 42 | typedef uint32_t im_api_version_t; 43 | typedef uint32_t im_job_handle_t; 44 | typedef uint32_t im_ctx_id_t; 45 | typedef uint32_t rga_buffer_handle_t; 46 | 47 | typedef enum { 48 | /* Rotation */ 49 | IM_HAL_TRANSFORM_ROT_90 = 1 << 0, 50 | IM_HAL_TRANSFORM_ROT_180 = 1 << 1, 51 | IM_HAL_TRANSFORM_ROT_270 = 1 << 2, 52 | IM_HAL_TRANSFORM_FLIP_H = 1 << 3, 53 | IM_HAL_TRANSFORM_FLIP_V = 1 << 4, 54 | IM_HAL_TRANSFORM_FLIP_H_V = 1 << 5, 55 | IM_HAL_TRANSFORM_MASK = 0x3f, 56 | 57 | /* 58 | * Blend 59 | * Additional blend usage, can be used with both source and target configs. 60 | * If none of the below is set, the default "SRC over DST" is applied. 61 | */ 62 | IM_ALPHA_BLEND_SRC_OVER = 1 << 6, /* Default, Porter-Duff "SRC over DST" */ 63 | IM_ALPHA_BLEND_SRC = 1 << 7, /* Porter-Duff "SRC" */ 64 | IM_ALPHA_BLEND_DST = 1 << 8, /* Porter-Duff "DST" */ 65 | IM_ALPHA_BLEND_SRC_IN = 1 << 9, /* Porter-Duff "SRC in DST" */ 66 | IM_ALPHA_BLEND_DST_IN = 1 << 10, /* Porter-Duff "DST in SRC" */ 67 | IM_ALPHA_BLEND_SRC_OUT = 1 << 11, /* Porter-Duff "SRC out DST" */ 68 | IM_ALPHA_BLEND_DST_OUT = 1 << 12, /* Porter-Duff "DST out SRC" */ 69 | IM_ALPHA_BLEND_DST_OVER = 1 << 13, /* Porter-Duff "DST over SRC" */ 70 | IM_ALPHA_BLEND_SRC_ATOP = 1 << 14, /* Porter-Duff "SRC ATOP" */ 71 | IM_ALPHA_BLEND_DST_ATOP = 1 << 15, /* Porter-Duff "DST ATOP" */ 72 | IM_ALPHA_BLEND_XOR = 1 << 16, /* Xor */ 73 | IM_ALPHA_BLEND_MASK = 0x1ffc0, 74 | 75 | IM_ALPHA_COLORKEY_NORMAL = 1 << 17, 76 | IM_ALPHA_COLORKEY_INVERTED = 1 << 18, 77 | IM_ALPHA_COLORKEY_MASK = 0x60000, 78 | 79 | IM_SYNC = 1 << 19, 80 | IM_CROP = 1 << 20, /* Unused */ 81 | IM_COLOR_FILL = 1 << 21, 82 | IM_COLOR_PALETTE = 1 << 22, 83 | IM_NN_QUANTIZE = 1 << 23, 84 | IM_ROP = 1 << 24, 85 | IM_ALPHA_BLEND_PRE_MUL = 1 << 25, 86 | IM_ASYNC = 1 << 26, 87 | IM_MOSAIC = 1 << 27, 88 | IM_OSD = 1 << 28, 89 | IM_PRE_INTR = 1 << 29, 90 | } IM_USAGE; 91 | 92 | typedef enum { 93 | IM_RASTER_MODE = 1 << 0, 94 | IM_FBC_MODE = 1 << 1, 95 | IM_TILE_MODE = 1 << 2, 96 | } IM_RD_MODE; 97 | 98 | typedef enum { 99 | IM_SCHEDULER_RGA3_CORE0 = 1 << 0, 100 | IM_SCHEDULER_RGA3_CORE1 = 1 << 1, 101 | IM_SCHEDULER_RGA2_CORE0 = 1 << 2, 102 | IM_SCHEDULER_RGA3_DEFAULT = IM_SCHEDULER_RGA3_CORE0, 103 | IM_SCHEDULER_RGA2_DEFAULT = IM_SCHEDULER_RGA2_CORE0, 104 | IM_SCHEDULER_MASK = 0x7, 105 | IM_SCHEDULER_DEFAULT = 0, 106 | } IM_SCHEDULER_CORE; 107 | 108 | typedef enum { 109 | IM_ROP_AND = 0x88, 110 | IM_ROP_OR = 0xee, 111 | IM_ROP_NOT_DST = 0x55, 112 | IM_ROP_NOT_SRC = 0x33, 113 | IM_ROP_XOR = 0xf6, 114 | IM_ROP_NOT_XOR = 0xf9, 115 | } IM_ROP_CODE; 116 | 117 | typedef enum { 118 | IM_MOSAIC_8 = 0x0, 119 | IM_MOSAIC_16 = 0x1, 120 | IM_MOSAIC_32 = 0x2, 121 | IM_MOSAIC_64 = 0x3, 122 | IM_MOSAIC_128 = 0x4, 123 | } IM_MOSAIC_MODE; 124 | 125 | typedef enum { 126 | IM_BORDER_CONSTANT = 0, /* iiiiii|abcdefgh|iiiiiii with some specified value 'i' */ 127 | IM_BORDER_REFLECT = 2, /* fedcba|abcdefgh|hgfedcb */ 128 | IM_BORDER_WRAP = 3, /* cdefgh|abcdefgh|abcdefg */ 129 | } IM_BORDER_TYPE; 130 | 131 | /* Status codes, returned by any blit function */ 132 | typedef enum { 133 | IM_YUV_TO_RGB_BT601_LIMIT = 1 << 0, 134 | IM_YUV_TO_RGB_BT601_FULL = 2 << 0, 135 | IM_YUV_TO_RGB_BT709_LIMIT = 3 << 0, 136 | IM_YUV_TO_RGB_MASK = 3 << 0, 137 | IM_RGB_TO_YUV_BT601_FULL = 1 << 2, 138 | IM_RGB_TO_YUV_BT601_LIMIT = 2 << 2, 139 | IM_RGB_TO_YUV_BT709_LIMIT = 3 << 2, 140 | IM_RGB_TO_YUV_MASK = 3 << 2, 141 | IM_RGB_TO_Y4 = 1 << 4, 142 | IM_RGB_TO_Y4_DITHER = 2 << 4, 143 | IM_RGB_TO_Y1_DITHER = 3 << 4, 144 | IM_Y4_MASK = 3 << 4, 145 | IM_RGB_FULL = 1 << 8, 146 | IM_RGB_CLIP = 2 << 8, 147 | IM_YUV_BT601_LIMIT_RANGE = 3 << 8, 148 | IM_YUV_BT601_FULL_RANGE = 4 << 8, 149 | IM_YUV_BT709_LIMIT_RANGE = 5 << 8, 150 | IM_YUV_BT709_FULL_RANGE = 6 << 8, 151 | IM_FULL_CSC_MASK = 0xf << 8, 152 | IM_COLOR_SPACE_DEFAULT = 0, 153 | } IM_COLOR_SPACE_MODE; 154 | 155 | typedef enum { 156 | IM_UP_SCALE, 157 | IM_DOWN_SCALE, 158 | } IM_SCALE; 159 | 160 | typedef enum { 161 | INTER_NEAREST, 162 | INTER_LINEAR, 163 | INTER_CUBIC, 164 | } IM_SCALE_MODE; 165 | 166 | typedef enum { 167 | IM_CONFIG_SCHEDULER_CORE, 168 | IM_CONFIG_PRIORITY, 169 | IM_CONFIG_CHECK, 170 | } IM_CONFIG_NAME; 171 | 172 | typedef enum { 173 | IM_OSD_MODE_STATISTICS = 0x1 << 0, 174 | IM_OSD_MODE_AUTO_INVERT = 0x1 << 1, 175 | } IM_OSD_MODE; 176 | 177 | typedef enum { 178 | IM_OSD_INVERT_CHANNEL_NONE = 0x0, 179 | IM_OSD_INVERT_CHANNEL_Y_G = 0x1 << 0, 180 | IM_OSD_INVERT_CHANNEL_C_RB = 0x1 << 1, 181 | IM_OSD_INVERT_CHANNEL_ALPHA = 0x1 << 2, 182 | IM_OSD_INVERT_CHANNEL_COLOR = IM_OSD_INVERT_CHANNEL_Y_G | 183 | IM_OSD_INVERT_CHANNEL_C_RB, 184 | IM_OSD_INVERT_CHANNEL_BOTH = IM_OSD_INVERT_CHANNEL_COLOR | 185 | IM_OSD_INVERT_CHANNEL_ALPHA, 186 | } IM_OSD_INVERT_CHANNEL; 187 | 188 | typedef enum { 189 | IM_OSD_FLAGS_INTERNAL = 0, 190 | IM_OSD_FLAGS_EXTERNAL, 191 | } IM_OSD_FLAGS_MODE; 192 | 193 | typedef enum { 194 | IM_OSD_INVERT_USE_FACTOR, 195 | IM_OSD_INVERT_USE_SWAP, 196 | } IM_OSD_INVERT_MODE; 197 | 198 | typedef enum { 199 | IM_OSD_BACKGROUND_DEFAULT_BRIGHT = 0, 200 | IM_OSD_BACKGROUND_DEFAULT_DARK, 201 | } IM_OSD_BACKGROUND_DEFAULT; 202 | 203 | typedef enum { 204 | IM_OSD_BLOCK_MODE_NORMAL = 0, 205 | IM_OSD_BLOCK_MODE_DIFFERENT, 206 | } IM_OSD_BLOCK_WIDTH_MODE; 207 | 208 | typedef enum { 209 | IM_OSD_MODE_HORIZONTAL, 210 | IM_OSD_MODE_VERTICAL, 211 | } IM_OSD_DIRECTION; 212 | 213 | typedef enum { 214 | IM_OSD_COLOR_PIXEL, 215 | IM_OSD_COLOR_EXTERNAL, 216 | } IM_OSD_COLOR_MODE; 217 | 218 | typedef enum { 219 | IM_INTR_READ_INTR = 1 << 0, 220 | IM_INTR_READ_HOLD = 1 << 1, 221 | IM_INTR_WRITE_INTR = 1 << 2, 222 | } IM_PRE_INTR_FLAGS; 223 | 224 | typedef enum { 225 | IM_CONTEXT_NONE = 0x0, 226 | IM_CONTEXT_SRC_FIX_ENABLE = 0x1 << 0, // Enable kernel to modify the image parameters of the channel. 227 | IM_CONTEXT_SRC_CACHE_INFO = 0x1 << 1, // It will replace the parameters in ctx with the modified parameters. 228 | IM_CONTEXT_SRC1_FIX_ENABLE = 0x1 << 2, 229 | IM_CONTEXT_SRC1_CACHE_INFO = 0x1 << 3, 230 | IM_CONTEXT_DST_FIX_ENABLE = 0x1 << 4, 231 | IM_CONTEXT_DST_CACHE_INFO = 0x1 << 5, 232 | } IM_CONTEXT_FLAGS; 233 | 234 | /* Get RGA basic information index */ 235 | typedef enum { 236 | RGA_VENDOR = 0, 237 | RGA_VERSION, 238 | RGA_MAX_INPUT, 239 | RGA_MAX_OUTPUT, 240 | RGA_BYTE_STRIDE, 241 | RGA_SCALE_LIMIT, 242 | RGA_INPUT_FORMAT, 243 | RGA_OUTPUT_FORMAT, 244 | RGA_FEATURE, 245 | RGA_EXPECTED, 246 | RGA_ALL, 247 | } IM_INFORMATION; 248 | 249 | /* Status codes, returned by any blit function */ 250 | typedef enum { 251 | IM_STATUS_NOERROR = 2, 252 | IM_STATUS_SUCCESS = 1, 253 | IM_STATUS_NOT_SUPPORTED = -1, 254 | IM_STATUS_OUT_OF_MEMORY = -2, 255 | IM_STATUS_INVALID_PARAM = -3, 256 | IM_STATUS_ILLEGAL_PARAM = -4, 257 | IM_STATUS_ERROR_VERSION = -5, 258 | IM_STATUS_FAILED = 0, 259 | } IM_STATUS; 260 | 261 | /* Rectangle definition */ 262 | typedef struct { 263 | int x; /* upper-left x */ 264 | int y; /* upper-left y */ 265 | int width; /* width */ 266 | int height; /* height */ 267 | } im_rect; 268 | 269 | typedef struct { 270 | int max; /* The Maximum value of the color key */ 271 | int min; /* The minimum value of the color key */ 272 | } im_colorkey_range; 273 | 274 | 275 | typedef struct im_nn { 276 | int scale_r; /* scaling factor on R channal */ 277 | int scale_g; /* scaling factor on G channal */ 278 | int scale_b; /* scaling factor on B channal */ 279 | int offset_r; /* offset on R channal */ 280 | int offset_g; /* offset on G channal */ 281 | int offset_b; /* offset on B channal */ 282 | } im_nn_t; 283 | 284 | /* im_info definition */ 285 | typedef struct { 286 | void* vir_addr; /* virtual address */ 287 | void* phy_addr; /* physical address */ 288 | int fd; /* shared fd */ 289 | 290 | int width; /* width */ 291 | int height; /* height */ 292 | int wstride; /* wstride */ 293 | int hstride; /* hstride */ 294 | int format; /* format */ 295 | 296 | int color_space_mode; /* color_space_mode */ 297 | int global_alpha; /* global_alpha */ 298 | int rd_mode; 299 | 300 | /* legarcy */ 301 | int color; /* color, used by color fill */ 302 | im_colorkey_range colorkey_range; /* range value of color key */ 303 | im_nn_t nn; 304 | int rop_code; 305 | 306 | rga_buffer_handle_t handle; /* buffer handle */ 307 | } rga_buffer_t; 308 | 309 | typedef struct im_color { 310 | union { 311 | struct { 312 | uint8_t red; 313 | uint8_t green; 314 | uint8_t blue; 315 | uint8_t alpha; 316 | }; 317 | uint32_t value; 318 | }; 319 | } im_color_t; 320 | 321 | typedef struct im_osd_invert_factor { 322 | uint8_t alpha_max; 323 | uint8_t alpha_min; 324 | uint8_t yg_max; 325 | uint8_t yg_min; 326 | uint8_t crb_max; 327 | uint8_t crb_min; 328 | } im_osd_invert_factor_t; 329 | 330 | typedef struct im_osd_bpp2 { 331 | uint8_t ac_swap; // ac swap flag 332 | // 0: CA 333 | // 1: AC 334 | uint8_t endian_swap; // rgba2bpp endian swap 335 | // 0: Big endian 336 | // 1: Little endian 337 | im_color_t color0; 338 | im_color_t color1; 339 | } im_osd_bpp2_t; 340 | 341 | typedef struct im_osd_block { 342 | int width_mode; // normal or different 343 | // IM_OSD_BLOCK_MODE_NORMAL 344 | // IM_OSD_BLOCK_MODE_DIFFERENT 345 | union { 346 | int width; // normal_mode block width 347 | int width_index; // different_mode block width index in RAM 348 | }; 349 | 350 | int block_count; // block count 351 | 352 | int background_config; // background config is bright or dark 353 | // IM_OSD_BACKGROUND_DEFAULT_BRIGHT 354 | // IM_OSD_BACKGROUND_DEFAULT_DARK 355 | 356 | int direction; // osd block direction 357 | // IM_OSD_MODE_HORIZONTAL 358 | // IM_OSD_MODE_VERTICAL 359 | 360 | int color_mode; // using src1 color or config color 361 | // IM_OSD_COLOR_PIXEL 362 | // IM_OSD_COLOR_EXTERNAL 363 | im_color_t normal_color; // config color: normal 364 | im_color_t invert_color; // config color: invert 365 | } im_osd_block_t; 366 | 367 | typedef struct im_osd_invert { 368 | int invert_channel; // invert channel config: 369 | // IM_OSD_INVERT_CHANNEL_NONE 370 | // IM_OSD_INVERT_CHANNEL_Y_G 371 | // IM_OSD_INVERT_CHANNEL_C_RB 372 | // IM_OSD_INVERT_CHANNEL_ALPHA 373 | // IM_OSD_INVERT_CHANNEL_COLOR 374 | // IM_OSD_INVERT_CHANNEL_BOTH 375 | int flags_mode; // use external or inertnal RAM invert flags 376 | // IM_OSD_FLAGS_EXTERNAL 377 | // IM_OSD_FLAGS_INTERNAL 378 | int flags_index; // flags index when using internal RAM invert flags 379 | 380 | uint64_t invert_flags; // external invert flags 381 | uint64_t current_flags; // current flags 382 | 383 | int invert_mode; // invert use swap or factor 384 | // IM_OSD_INVERT_USE_FACTOR 385 | // IM_OSD_INVERT_USE_SWAP 386 | im_osd_invert_factor_t factor; 387 | 388 | int threash; 389 | } im_osd_invert_t; 390 | 391 | typedef struct im_osd { 392 | int osd_mode; // osd mode: statistics or auto_invert 393 | // IM_OSD_MODE_STATISTICS 394 | // IM_OSD_MODE_AUTO_INVERT 395 | im_osd_block_t block_parm; // osd block info config 396 | 397 | im_osd_invert_t invert_config; 398 | 399 | im_osd_bpp2_t bpp2_info; 400 | } im_osd_t; 401 | 402 | typedef struct im_intr_config { 403 | uint32_t flags; 404 | 405 | int read_threshold; 406 | int write_start; 407 | int write_step; 408 | } im_intr_config_t; 409 | 410 | typedef struct im_opt { 411 | im_api_version_t version DEFAULT_INITIALIZER(RGA_CURRENT_API_HEADER_VERSION); 412 | 413 | int color; /* color, used by color fill */ 414 | im_colorkey_range colorkey_range; /* range value of color key */ 415 | im_nn_t nn; 416 | int rop_code; 417 | 418 | int priority; 419 | int core; 420 | 421 | int mosaic_mode; 422 | 423 | im_osd_t osd_config; 424 | 425 | im_intr_config_t intr_config; 426 | 427 | char reserve[128]; 428 | } im_opt_t; 429 | 430 | typedef struct im_handle_param { 431 | uint32_t width; 432 | uint32_t height; 433 | uint32_t format; 434 | } im_handle_param_t; 435 | 436 | #endif /* _RGA_IM2D_TYPE_H_ */ 437 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/im2d_version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Cerf Yu 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _RGA_IM2D_VERSION_H_ 20 | #define _RGA_IM2D_VERSION_H_ 21 | 22 | #define RGA_VERSION_STR_HELPER(x) #x 23 | #define RGA_VERSION_STR(x) RGA_VERSION_STR_HELPER(x) 24 | 25 | /* RGA im2d api verison */ 26 | #define RGA_API_MAJOR_VERSION 1 27 | #define RGA_API_MINOR_VERSION 9 28 | #define RGA_API_REVISION_VERSION 1 29 | #define RGA_API_BUILD_VERSION 4 30 | 31 | #define RGA_API_VERSION \ 32 | RGA_VERSION_STR(RGA_API_MAJOR_VERSION) "." \ 33 | RGA_VERSION_STR(RGA_API_MINOR_VERSION) "." \ 34 | RGA_VERSION_STR(RGA_API_REVISION_VERSION) "_[" \ 35 | RGA_VERSION_STR(RGA_API_BUILD_VERSION) "]" 36 | #define RGA_API_FULL_VERSION "rga_api version " RGA_API_VERSION 37 | 38 | /* For header file version verification */ 39 | #define RGA_CURRENT_API_VERSION (\ 40 | (RGA_API_MAJOR_VERSION & 0xff) << 24 | \ 41 | (RGA_API_MINOR_VERSION & 0xff) << 16 | \ 42 | (RGA_API_REVISION_VERSION & 0xff) << 8 | \ 43 | (RGA_API_BUILD_VERSION & 0xff)\ 44 | ) 45 | #define RGA_CURRENT_API_HEADER_VERSION RGA_CURRENT_API_VERSION 46 | 47 | #endif /* _RGA_IM2D_VERSION_H_ */ 48 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/include/rga.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Rockchip Electronics Co., Ltd. 3 | * Authors: 4 | * Zhiqin Wei 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _RGA_DRIVER_H_ 20 | #define _RGA_DRIVER_H_ 21 | 22 | 23 | #ifndef ENABLE 24 | #define ENABLE 1 25 | #endif 26 | 27 | #ifndef DISABLE 28 | #define DISABLE 0 29 | #endif 30 | 31 | #ifdef __cplusplus 32 | extern "C" 33 | { 34 | #endif 35 | 36 | /* In order to be compatible with RK_FORMAT_XX and HAL_PIXEL_FORMAT_XX, 37 | * RK_FORMAT_XX is shifted to the left by 8 bits to distinguish. */ 38 | typedef enum _Rga_SURF_FORMAT { 39 | RK_FORMAT_RGBA_8888 = 0x0 << 8, 40 | RK_FORMAT_RGBX_8888 = 0x1 << 8, 41 | RK_FORMAT_RGB_888 = 0x2 << 8, 42 | RK_FORMAT_BGRA_8888 = 0x3 << 8, 43 | RK_FORMAT_RGB_565 = 0x4 << 8, 44 | RK_FORMAT_RGBA_5551 = 0x5 << 8, 45 | RK_FORMAT_RGBA_4444 = 0x6 << 8, 46 | RK_FORMAT_BGR_888 = 0x7 << 8, 47 | 48 | RK_FORMAT_YCbCr_422_SP = 0x8 << 8, 49 | RK_FORMAT_YCbCr_422_P = 0x9 << 8, 50 | RK_FORMAT_YCbCr_420_SP = 0xa << 8, 51 | RK_FORMAT_YCbCr_420_P = 0xb << 8, 52 | 53 | RK_FORMAT_YCrCb_422_SP = 0xc << 8, 54 | RK_FORMAT_YCrCb_422_P = 0xd << 8, 55 | RK_FORMAT_YCrCb_420_SP = 0xe << 8, 56 | RK_FORMAT_YCrCb_420_P = 0xf << 8, 57 | 58 | RK_FORMAT_BPP1 = 0x10 << 8, 59 | RK_FORMAT_BPP2 = 0x11 << 8, 60 | RK_FORMAT_BPP4 = 0x12 << 8, 61 | RK_FORMAT_BPP8 = 0x13 << 8, 62 | 63 | RK_FORMAT_Y4 = 0x14 << 8, 64 | RK_FORMAT_YCbCr_400 = 0x15 << 8, 65 | 66 | RK_FORMAT_BGRX_8888 = 0x16 << 8, 67 | 68 | RK_FORMAT_YVYU_422 = 0x18 << 8, 69 | RK_FORMAT_YVYU_420 = 0x19 << 8, 70 | RK_FORMAT_VYUY_422 = 0x1a << 8, 71 | RK_FORMAT_VYUY_420 = 0x1b << 8, 72 | RK_FORMAT_YUYV_422 = 0x1c << 8, 73 | RK_FORMAT_YUYV_420 = 0x1d << 8, 74 | RK_FORMAT_UYVY_422 = 0x1e << 8, 75 | RK_FORMAT_UYVY_420 = 0x1f << 8, 76 | 77 | RK_FORMAT_YCbCr_420_SP_10B = 0x20 << 8, 78 | RK_FORMAT_YCrCb_420_SP_10B = 0x21 << 8, 79 | RK_FORMAT_YCbCr_422_SP_10B = 0x22 << 8, 80 | RK_FORMAT_YCrCb_422_SP_10B = 0x23 << 8, 81 | /* For compatibility with misspellings */ 82 | RK_FORMAT_YCbCr_422_10b_SP = RK_FORMAT_YCbCr_422_SP_10B, 83 | RK_FORMAT_YCrCb_422_10b_SP = RK_FORMAT_YCrCb_422_SP_10B, 84 | 85 | RK_FORMAT_BGR_565 = 0x24 << 8, 86 | RK_FORMAT_BGRA_5551 = 0x25 << 8, 87 | RK_FORMAT_BGRA_4444 = 0x26 << 8, 88 | 89 | RK_FORMAT_ARGB_8888 = 0x28 << 8, 90 | RK_FORMAT_XRGB_8888 = 0x29 << 8, 91 | RK_FORMAT_ARGB_5551 = 0x2a << 8, 92 | RK_FORMAT_ARGB_4444 = 0x2b << 8, 93 | RK_FORMAT_ABGR_8888 = 0x2c << 8, 94 | RK_FORMAT_XBGR_8888 = 0x2d << 8, 95 | RK_FORMAT_ABGR_5551 = 0x2e << 8, 96 | RK_FORMAT_ABGR_4444 = 0x2f << 8, 97 | 98 | RK_FORMAT_RGBA2BPP = 0x30 << 8, 99 | 100 | RK_FORMAT_UNKNOWN = 0x100 << 8, 101 | } RgaSURF_FORMAT; 102 | 103 | enum { 104 | yuv2rgb_mode0 = 0x0, /* BT.601 MPEG */ 105 | yuv2rgb_mode1 = 0x1, /* BT.601 JPEG */ 106 | yuv2rgb_mode2 = 0x2, /* BT.709 */ 107 | 108 | rgb2yuv_601_full = 0x1 << 8, 109 | rgb2yuv_709_full = 0x2 << 8, 110 | yuv2yuv_601_limit_2_709_limit = 0x3 << 8, 111 | yuv2yuv_601_limit_2_709_full = 0x4 << 8, 112 | yuv2yuv_709_limit_2_601_limit = 0x5 << 8, 113 | yuv2yuv_709_limit_2_601_full = 0x6 << 8, //not support 114 | yuv2yuv_601_full_2_709_limit = 0x7 << 8, 115 | yuv2yuv_601_full_2_709_full = 0x8 << 8, //not support 116 | yuv2yuv_709_full_2_601_limit = 0x9 << 8, //not support 117 | yuv2yuv_709_full_2_601_full = 0xa << 8, //not support 118 | full_csc_mask = 0xf00, 119 | }; 120 | 121 | enum { 122 | RGA3_SCHEDULER_CORE0 = 1 << 0, 123 | RGA3_SCHEDULER_CORE1 = 1 << 1, 124 | RGA2_SCHEDULER_CORE0 = 1 << 2, 125 | }; 126 | 127 | /* RGA3 rd_mode */ 128 | enum 129 | { 130 | raster_mode = 0x1 << 0, 131 | fbc_mode = 0x1 << 1, 132 | tile_mode = 0x1 << 2, 133 | }; 134 | 135 | #ifdef __cplusplus 136 | } 137 | #endif 138 | 139 | #endif /*_RK29_IPP_DRIVER_H_*/ 140 | -------------------------------------------------------------------------------- /include/3rdparty/rga/RK3588/lib/Linux/aarch64/librga.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leafqycc/rknn-cpp-Multithreading/d86b5a99b8cdbd6b77c25e7aa3ec77fd1b7d5f6e/include/3rdparty/rga/RK3588/lib/Linux/aarch64/librga.so -------------------------------------------------------------------------------- /include/ThreadPool.hpp: -------------------------------------------------------------------------------- 1 | #ifndef THREADPOOL_H 2 | #define THREADPOOL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace dpool 15 | { 16 | 17 | class ThreadPool 18 | { 19 | public: 20 | using MutexGuard = std::lock_guard; 21 | using UniqueLock = std::unique_lock; 22 | using Thread = std::thread; 23 | using ThreadID = std::thread::id; 24 | using Task = std::function; 25 | 26 | ThreadPool() 27 | : ThreadPool(Thread::hardware_concurrency()) 28 | { 29 | } 30 | 31 | explicit ThreadPool(size_t maxThreads) 32 | : quit_(false), 33 | currentThreads_(0), 34 | idleThreads_(0), 35 | maxThreads_(maxThreads) 36 | { 37 | } 38 | 39 | // disable the copy operations 40 | ThreadPool(const ThreadPool &) = delete; 41 | ThreadPool &operator=(const ThreadPool &) = delete; 42 | 43 | ~ThreadPool() 44 | { 45 | { 46 | MutexGuard guard(mutex_); 47 | quit_ = true; 48 | } 49 | cv_.notify_all(); 50 | 51 | for (auto &elem : threads_) 52 | { 53 | assert(elem.second.joinable()); 54 | elem.second.join(); 55 | } 56 | } 57 | 58 | template 59 | auto submit(Func &&func, Ts &&...params) 60 | -> std::future::type> 61 | { 62 | auto execute = std::bind(std::forward(func), std::forward(params)...); 63 | 64 | using ReturnType = typename std::result_of::type; 65 | using PackagedTask = std::packaged_task; 66 | 67 | auto task = std::make_shared(std::move(execute)); 68 | auto result = task->get_future(); 69 | 70 | MutexGuard guard(mutex_); 71 | assert(!quit_); 72 | 73 | tasks_.emplace([task]() 74 | { (*task)(); }); 75 | if (idleThreads_ > 0) 76 | { 77 | cv_.notify_one(); 78 | } 79 | else if (currentThreads_ < maxThreads_) 80 | { 81 | Thread t(&ThreadPool::worker, this); 82 | assert(threads_.find(t.get_id()) == threads_.end()); 83 | threads_[t.get_id()] = std::move(t); 84 | ++currentThreads_; 85 | } 86 | 87 | return result; 88 | } 89 | 90 | size_t threadsNum() const 91 | { 92 | MutexGuard guard(mutex_); 93 | return currentThreads_; 94 | } 95 | 96 | private: 97 | void worker() 98 | { 99 | while (true) 100 | { 101 | Task task; 102 | { 103 | UniqueLock uniqueLock(mutex_); 104 | ++idleThreads_; 105 | auto hasTimedout = !cv_.wait_for(uniqueLock, 106 | std::chrono::seconds(WAIT_SECONDS), 107 | [this]() 108 | { 109 | return quit_ || !tasks_.empty(); 110 | }); 111 | --idleThreads_; 112 | if (tasks_.empty()) 113 | { 114 | if (quit_) 115 | { 116 | --currentThreads_; 117 | return; 118 | } 119 | if (hasTimedout) 120 | { 121 | --currentThreads_; 122 | joinFinishedThreads(); 123 | finishedThreadIDs_.emplace(std::this_thread::get_id()); 124 | return; 125 | } 126 | } 127 | task = std::move(tasks_.front()); 128 | tasks_.pop(); 129 | } 130 | task(); 131 | } 132 | } 133 | 134 | void joinFinishedThreads() 135 | { 136 | while (!finishedThreadIDs_.empty()) 137 | { 138 | auto id = std::move(finishedThreadIDs_.front()); 139 | finishedThreadIDs_.pop(); 140 | auto iter = threads_.find(id); 141 | 142 | assert(iter != threads_.end()); 143 | assert(iter->second.joinable()); 144 | 145 | iter->second.join(); 146 | threads_.erase(iter); 147 | } 148 | } 149 | 150 | static constexpr size_t WAIT_SECONDS = 2; 151 | 152 | bool quit_; 153 | size_t currentThreads_; 154 | size_t idleThreads_; 155 | size_t maxThreads_; 156 | 157 | mutable std::mutex mutex_; 158 | std::condition_variable cv_; 159 | std::queue tasks_; 160 | std::queue finishedThreadIDs_; 161 | std::unordered_map threads_; 162 | }; 163 | 164 | constexpr size_t ThreadPool::WAIT_SECONDS; 165 | 166 | } // namespace dpool 167 | 168 | #endif /* THREADPOOL_H */ -------------------------------------------------------------------------------- /include/coreNum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CORENUM_H 2 | #define CORENUM_H 3 | 4 | #include 5 | 6 | #include "rknn_api.h" 7 | 8 | const int RK3588 = 3; 9 | 10 | // 设置模型需要绑定的核心 11 | // Set the core of the model that needs to be bound 12 | int get_core_num() 13 | { 14 | static int core_num = 0; 15 | static std::mutex mtx; 16 | 17 | std::lock_guard lock(mtx); 18 | 19 | int temp = core_num % RK3588; 20 | core_num++; 21 | return temp; 22 | } 23 | #endif -------------------------------------------------------------------------------- /include/drm_func.h: -------------------------------------------------------------------------------- 1 | #ifndef __DRM_FUNC_H__ 2 | #define __DRM_FUNC_H__ 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include // open function 8 | #include // close function 9 | #include 10 | #include 11 | 12 | 13 | #include 14 | #include "libdrm/drm_fourcc.h" 15 | #include "xf86drm.h" 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | typedef int (* FUNC_DRM_IOCTL)(int fd, unsigned long request, void *arg); 22 | 23 | typedef struct _drm_context{ 24 | void *drm_handle; 25 | FUNC_DRM_IOCTL io_func; 26 | } drm_context; 27 | 28 | /* memory type definitions. */ 29 | enum drm_rockchip_gem_mem_type 30 | { 31 | /* Physically Continuous memory and used as default. */ 32 | ROCKCHIP_BO_CONTIG = 1 << 0, 33 | /* cachable mapping. */ 34 | ROCKCHIP_BO_CACHABLE = 1 << 1, 35 | /* write-combine mapping. */ 36 | ROCKCHIP_BO_WC = 1 << 2, 37 | ROCKCHIP_BO_SECURE = 1 << 3, 38 | ROCKCHIP_BO_MASK = ROCKCHIP_BO_CONTIG | ROCKCHIP_BO_CACHABLE | 39 | ROCKCHIP_BO_WC | ROCKCHIP_BO_SECURE 40 | }; 41 | 42 | int drm_init(drm_context *drm_ctx); 43 | 44 | void* drm_buf_alloc(drm_context *drm_ctx,int drm_fd, int TexWidth, int TexHeight,int bpp,int *fd,unsigned int *handle,size_t *actual_size); 45 | 46 | int drm_buf_destroy(drm_context *drm_ctx,int drm_fd,int buf_fd, int handle,void *drm_buf,size_t size); 47 | 48 | void drm_deinit(drm_context *drm_ctx, int drm_fd); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | #endif /*__DRM_FUNC_H__*/ -------------------------------------------------------------------------------- /include/librknn_api.so: -------------------------------------------------------------------------------- 1 | librknnrt.so -------------------------------------------------------------------------------- /include/librknnrt.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leafqycc/rknn-cpp-Multithreading/d86b5a99b8cdbd6b77c25e7aa3ec77fd1b7d5f6e/include/librknnrt.so -------------------------------------------------------------------------------- /include/postprocess.h: -------------------------------------------------------------------------------- 1 | #ifndef _RKNN_YOLOV5_DEMO_POSTPROCESS_H_ 2 | #define _RKNN_YOLOV5_DEMO_POSTPROCESS_H_ 3 | 4 | #include 5 | #include 6 | 7 | #define OBJ_NAME_MAX_SIZE 16 8 | #define OBJ_NUMB_MAX_SIZE 64 9 | #define OBJ_CLASS_NUM 80 10 | #define NMS_THRESH 0.45 11 | #define BOX_THRESH 0.25 12 | #define PROP_BOX_SIZE (5 + OBJ_CLASS_NUM) 13 | 14 | typedef struct _BOX_RECT 15 | { 16 | int left; 17 | int right; 18 | int top; 19 | int bottom; 20 | } BOX_RECT; 21 | 22 | typedef struct __detect_result_t 23 | { 24 | char name[OBJ_NAME_MAX_SIZE]; 25 | BOX_RECT box; 26 | float prop; 27 | } detect_result_t; 28 | 29 | typedef struct _detect_result_group_t 30 | { 31 | int id; 32 | int count; 33 | detect_result_t results[OBJ_NUMB_MAX_SIZE]; 34 | } detect_result_group_t; 35 | 36 | int post_process(int8_t *input0, int8_t *input1, int8_t *input2, int model_in_h, int model_in_w, 37 | float conf_threshold, float nms_threshold, BOX_RECT pads, float scale_w, float scale_h, 38 | std::vector &qnt_zps, std::vector &qnt_scales, 39 | detect_result_group_t *group); 40 | 41 | void deinitPostProcess(); 42 | #endif //_RKNN_YOLOV5_DEMO_POSTPROCESS_H_ 43 | -------------------------------------------------------------------------------- /include/preprocess.h: -------------------------------------------------------------------------------- 1 | #ifndef _RKNN_YOLOV5_DEMO_PREPROCESS_H_ 2 | #define _RKNN_YOLOV5_DEMO_PREPROCESS_H_ 3 | 4 | #include 5 | #include "im2d.h" 6 | #include "rga.h" 7 | #include "opencv2/core/core.hpp" 8 | #include "opencv2/imgcodecs.hpp" 9 | #include "opencv2/imgproc.hpp" 10 | #include "postprocess.h" 11 | 12 | void letterbox(const cv::Mat &image, cv::Mat &padded_image, BOX_RECT &pads, const float scale, const cv::Size &target_size, const cv::Scalar &pad_color = cv::Scalar(128, 128, 128)); 13 | 14 | int resize_rga(rga_buffer_t &src, rga_buffer_t &dst, const cv::Mat &image, cv::Mat &resized_image, const cv::Size &target_size); 15 | 16 | #endif //_RKNN_YOLOV5_DEMO_PREPROCESS_H_ 17 | -------------------------------------------------------------------------------- /include/rga_func.h: -------------------------------------------------------------------------------- 1 | #ifndef __RGA_FUNC_H__ 2 | #define __RGA_FUNC_H__ 3 | 4 | #include 5 | #include "RgaApi.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | typedef int(* FUNC_RGA_INIT)(); 12 | typedef void(* FUNC_RGA_DEINIT)(); 13 | typedef int(* FUNC_RGA_BLIT)(rga_info_t *, rga_info_t *, rga_info_t *); 14 | 15 | typedef struct _rga_context{ 16 | void *rga_handle; 17 | FUNC_RGA_INIT init_func; 18 | FUNC_RGA_DEINIT deinit_func; 19 | FUNC_RGA_BLIT blit_func; 20 | } rga_context; 21 | 22 | int RGA_init(rga_context* rga_ctx); 23 | 24 | void img_resize_fast(rga_context *rga_ctx, int src_fd, int src_w, int src_h, uint64_t dst_phys, int dst_w, int dst_h); 25 | 26 | void img_resize_slow(rga_context *rga_ctx, void *src_virt, int src_w, int src_h, void *dst_virt, int dst_w, int dst_h); 27 | 28 | int RGA_deinit(rga_context* rga_ctx); 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | #endif/*__RGA_FUNC_H__*/ 34 | -------------------------------------------------------------------------------- /include/rkYolov5s.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RKYOLOV5S_H 2 | #define RKYOLOV5S_H 3 | 4 | #include "rknn_api.h" 5 | 6 | #include "opencv2/core/core.hpp" 7 | 8 | static void dump_tensor_attr(rknn_tensor_attr *attr); 9 | static unsigned char *load_data(FILE *fp, size_t ofst, size_t sz); 10 | static unsigned char *load_model(const char *filename, int *model_size); 11 | static int saveFloat(const char *file_name, float *output, int element_size); 12 | 13 | class rkYolov5s 14 | { 15 | private: 16 | int ret; 17 | std::mutex mtx; 18 | std::string model_path; 19 | unsigned char *model_data; 20 | 21 | rknn_context ctx; 22 | rknn_input_output_num io_num; 23 | rknn_tensor_attr *input_attrs; 24 | rknn_tensor_attr *output_attrs; 25 | rknn_input inputs[1]; 26 | 27 | int channel, width, height; 28 | int img_width, img_height; 29 | 30 | float nms_threshold, box_conf_threshold; 31 | 32 | public: 33 | rkYolov5s(const std::string &model_path); 34 | int init(rknn_context *ctx_in, bool isChild); 35 | rknn_context *get_pctx(); 36 | cv::Mat infer(cv::Mat &ori_img); 37 | ~rkYolov5s(); 38 | }; 39 | 40 | #endif -------------------------------------------------------------------------------- /include/rknnPool.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RKNNPOOL_H 2 | #define RKNNPOOL_H 3 | 4 | #include "ThreadPool.hpp" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // rknnModel模型类, inputType模型输入类型, outputType模型输出类型 12 | template 13 | class rknnPool 14 | { 15 | private: 16 | int threadNum; 17 | std::string modelPath; 18 | 19 | long long id; 20 | std::mutex idMtx, queueMtx; 21 | std::unique_ptr pool; 22 | std::queue> futs; 23 | std::vector> models; 24 | 25 | protected: 26 | int getModelId(); 27 | 28 | public: 29 | rknnPool(const std::string modelPath, int threadNum); 30 | int init(); 31 | // 模型推理/Model inference 32 | int put(inputType inputData); 33 | // 获取推理结果/Get the results of your inference 34 | int get(outputType &outputData); 35 | ~rknnPool(); 36 | }; 37 | 38 | template 39 | rknnPool::rknnPool(const std::string modelPath, int threadNum) 40 | { 41 | this->modelPath = modelPath; 42 | this->threadNum = threadNum; 43 | this->id = 0; 44 | } 45 | 46 | template 47 | int rknnPool::init() 48 | { 49 | try 50 | { 51 | this->pool = std::make_unique(this->threadNum); 52 | for (int i = 0; i < this->threadNum; i++) 53 | models.push_back(std::make_shared(this->modelPath.c_str())); 54 | } 55 | catch (const std::bad_alloc &e) 56 | { 57 | std::cout << "Out of memory: " << e.what() << std::endl; 58 | return -1; 59 | } 60 | // 初始化模型/Initialize the model 61 | for (int i = 0, ret = 0; i < threadNum; i++) 62 | { 63 | ret = models[i]->init(models[0]->get_pctx(), i != 0); 64 | if (ret != 0) 65 | return ret; 66 | } 67 | 68 | return 0; 69 | } 70 | 71 | template 72 | int rknnPool::getModelId() 73 | { 74 | std::lock_guard lock(idMtx); 75 | int modelId = id % threadNum; 76 | id++; 77 | return modelId; 78 | } 79 | 80 | template 81 | int rknnPool::put(inputType inputData) 82 | { 83 | std::lock_guard lock(queueMtx); 84 | futs.push(pool->submit(&rknnModel::infer, models[this->getModelId()], inputData)); 85 | return 0; 86 | } 87 | 88 | template 89 | int rknnPool::get(outputType &outputData) 90 | { 91 | std::lock_guard lock(queueMtx); 92 | if(futs.empty() == true) 93 | return 1; 94 | outputData = futs.front().get(); 95 | futs.pop(); 96 | return 0; 97 | } 98 | 99 | template 100 | rknnPool::~rknnPool() 101 | { 102 | while (!futs.empty()) 103 | { 104 | outputType temp = futs.front().get(); 105 | futs.pop(); 106 | } 107 | } 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /include/rknn_api.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * Copyright (c) 2017 - 2022 by Rockchip Corp. All rights reserved. 4 | * 5 | * The material in this file is confidential and contains trade secrets 6 | * of Rockchip Corporation. This is proprietary information owned by 7 | * Rockchip Corporation. No part of this work may be disclosed, 8 | * reproduced, copied, transmitted, or used in any way for any purpose, 9 | * without the express written permission of Rockchip Corporation. 10 | * 11 | *****************************************************************************/ 12 | 13 | 14 | #ifndef _RKNN_API_H 15 | #define _RKNN_API_H 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #include 22 | 23 | /* 24 | Definition of extended flag for rknn_init. 25 | */ 26 | /* set high priority context. */ 27 | #define RKNN_FLAG_PRIOR_HIGH 0x00000000 28 | 29 | /* set medium priority context */ 30 | #define RKNN_FLAG_PRIOR_MEDIUM 0x00000001 31 | 32 | /* set low priority context. */ 33 | #define RKNN_FLAG_PRIOR_LOW 0x00000002 34 | 35 | /* asynchronous mode. 36 | when enable, rknn_outputs_get will not block for too long because it directly retrieves the result of 37 | the previous frame which can increase the frame rate on single-threaded mode, but at the cost of 38 | rknn_outputs_get not retrieves the result of the current frame. 39 | in multi-threaded mode you do not need to turn this mode on. */ 40 | #define RKNN_FLAG_ASYNC_MASK 0x00000004 41 | 42 | /* collect performance mode. 43 | when enable, you can get detailed performance reports via rknn_query(ctx, RKNN_QUERY_PERF_DETAIL, ...), 44 | but it will reduce the frame rate. */ 45 | #define RKNN_FLAG_COLLECT_PERF_MASK 0x00000008 46 | 47 | /* allocate all memory in outside, includes weight/internal/inputs/outputs */ 48 | #define RKNN_FLAG_MEM_ALLOC_OUTSIDE 0x00000010 49 | 50 | /* weight sharing with the same network structure */ 51 | #define RKNN_FLAG_SHARE_WEIGHT_MEM 0x00000020 52 | 53 | /* send fence fd from outside */ 54 | #define RKNN_FLAG_FENCE_IN_OUTSIDE 0x00000040 55 | 56 | /* get fence fd from inside */ 57 | #define RKNN_FLAG_FENCE_OUT_OUTSIDE 0x00000080 58 | 59 | /* dummy init flag: could only get total_weight_size and total_internal_size by rknn_query*/ 60 | #define RKNN_FLAG_COLLECT_MODEL_INFO_ONLY 0x00000100 61 | 62 | /* set GPU as the preferred execution backend When the operator is not supported by the NPU */ 63 | #define RKNN_FLAG_EXECUTE_FALLBACK_PRIOR_DEVICE_GPU 0x00000400 64 | 65 | /* allocate internal memory in outside */ 66 | #define RKNN_FLAG_INTERNAL_ALLOC_OUTSIDE 0x00000200 67 | 68 | /* 69 | Error code returned by the RKNN API. 70 | */ 71 | #define RKNN_SUCC 0 /* execute succeed. */ 72 | #define RKNN_ERR_FAIL -1 /* execute failed. */ 73 | #define RKNN_ERR_TIMEOUT -2 /* execute timeout. */ 74 | #define RKNN_ERR_DEVICE_UNAVAILABLE -3 /* device is unavailable. */ 75 | #define RKNN_ERR_MALLOC_FAIL -4 /* memory malloc fail. */ 76 | #define RKNN_ERR_PARAM_INVALID -5 /* parameter is invalid. */ 77 | #define RKNN_ERR_MODEL_INVALID -6 /* model is invalid. */ 78 | #define RKNN_ERR_CTX_INVALID -7 /* context is invalid. */ 79 | #define RKNN_ERR_INPUT_INVALID -8 /* input is invalid. */ 80 | #define RKNN_ERR_OUTPUT_INVALID -9 /* output is invalid. */ 81 | #define RKNN_ERR_DEVICE_UNMATCH -10 /* the device is unmatch, please update rknn sdk 82 | and npu driver/firmware. */ 83 | #define RKNN_ERR_INCOMPATILE_PRE_COMPILE_MODEL -11 /* This RKNN model use pre_compile mode, but not compatible with current driver. */ 84 | #define RKNN_ERR_INCOMPATILE_OPTIMIZATION_LEVEL_VERSION -12 /* This RKNN model set optimization level, but not compatible with current driver. */ 85 | #define RKNN_ERR_TARGET_PLATFORM_UNMATCH -13 /* This RKNN model set target platform, but not compatible with current platform. */ 86 | 87 | /* 88 | Definition for tensor 89 | */ 90 | #define RKNN_MAX_DIMS 16 /* maximum dimension of tensor. */ 91 | #define RKNN_MAX_NUM_CHANNEL 15 /* maximum channel number of input tensor. */ 92 | #define RKNN_MAX_NAME_LEN 256 /* maximum name lenth of tensor. */ 93 | #define RKNN_MAX_DYNAMIC_SHAPE_NUM 512 /* maximum number of dynamic shape for each input. */ 94 | 95 | #ifdef __arm__ 96 | typedef uint32_t rknn_context; 97 | #else 98 | typedef uint64_t rknn_context; 99 | #endif 100 | 101 | 102 | /* 103 | The query command for rknn_query 104 | */ 105 | typedef enum _rknn_query_cmd { 106 | RKNN_QUERY_IN_OUT_NUM = 0, /* query the number of input & output tensor. */ 107 | RKNN_QUERY_INPUT_ATTR = 1, /* query the attribute of input tensor. */ 108 | RKNN_QUERY_OUTPUT_ATTR = 2, /* query the attribute of output tensor. */ 109 | RKNN_QUERY_PERF_DETAIL = 3, /* query the detail performance, need set 110 | RKNN_FLAG_COLLECT_PERF_MASK when call rknn_init, 111 | this query needs to be valid after rknn_outputs_get. */ 112 | RKNN_QUERY_PERF_RUN = 4, /* query the time of run, 113 | this query needs to be valid after rknn_outputs_get. */ 114 | RKNN_QUERY_SDK_VERSION = 5, /* query the sdk & driver version */ 115 | 116 | RKNN_QUERY_MEM_SIZE = 6, /* query the weight & internal memory size */ 117 | RKNN_QUERY_CUSTOM_STRING = 7, /* query the custom string */ 118 | 119 | RKNN_QUERY_NATIVE_INPUT_ATTR = 8, /* query the attribute of native input tensor. */ 120 | RKNN_QUERY_NATIVE_OUTPUT_ATTR = 9, /* query the attribute of native output tensor. */ 121 | 122 | RKNN_QUERY_NATIVE_NC1HWC2_INPUT_ATTR = 8, /* query the attribute of native input tensor. */ 123 | RKNN_QUERY_NATIVE_NC1HWC2_OUTPUT_ATTR = 9, /* query the attribute of native output tensor. */ 124 | 125 | RKNN_QUERY_NATIVE_NHWC_INPUT_ATTR = 10, /* query the attribute of native input tensor. */ 126 | RKNN_QUERY_NATIVE_NHWC_OUTPUT_ATTR = 11, /* query the attribute of native output tensor. */ 127 | 128 | RKNN_QUERY_DEVICE_MEM_INFO = 12, /* query the attribute of rknn memory information. */ 129 | 130 | RKNN_QUERY_INPUT_DYNAMIC_RANGE = 13, /* query the dynamic shape range of rknn input tensor. */ 131 | RKNN_QUERY_CURRENT_INPUT_ATTR = 14, /* query the current shape of rknn input tensor, only valid for dynamic rknn model*/ 132 | RKNN_QUERY_CURRENT_OUTPUT_ATTR = 15, /* query the current shape of rknn output tensor, only valid for dynamic rknn model*/ 133 | 134 | RKNN_QUERY_CURRENT_NATIVE_INPUT_ATTR = 16, /* query the current native shape of rknn input tensor, only valid for dynamic rknn model*/ 135 | RKNN_QUERY_CURRENT_NATIVE_OUTPUT_ATTR = 17, /* query the current native shape of rknn output tensor, only valid for dynamic rknn model*/ 136 | 137 | 138 | RKNN_QUERY_CMD_MAX 139 | } rknn_query_cmd; 140 | 141 | /* 142 | the tensor data type. 143 | */ 144 | typedef enum _rknn_tensor_type { 145 | RKNN_TENSOR_FLOAT32 = 0, /* data type is float32. */ 146 | RKNN_TENSOR_FLOAT16, /* data type is float16. */ 147 | RKNN_TENSOR_INT8, /* data type is int8. */ 148 | RKNN_TENSOR_UINT8, /* data type is uint8. */ 149 | RKNN_TENSOR_INT16, /* data type is int16. */ 150 | RKNN_TENSOR_UINT16, /* data type is uint16. */ 151 | RKNN_TENSOR_INT32, /* data type is int32. */ 152 | RKNN_TENSOR_UINT32, /* data type is uint32. */ 153 | RKNN_TENSOR_INT64, /* data type is int64. */ 154 | RKNN_TENSOR_BOOL, 155 | 156 | RKNN_TENSOR_TYPE_MAX 157 | } rknn_tensor_type; 158 | 159 | inline static const char* get_type_string(rknn_tensor_type type) 160 | { 161 | switch(type) { 162 | case RKNN_TENSOR_FLOAT32: return "FP32"; 163 | case RKNN_TENSOR_FLOAT16: return "FP16"; 164 | case RKNN_TENSOR_INT8: return "INT8"; 165 | case RKNN_TENSOR_UINT8: return "UINT8"; 166 | case RKNN_TENSOR_INT16: return "INT16"; 167 | case RKNN_TENSOR_UINT16: return "UINT16"; 168 | case RKNN_TENSOR_INT32: return "INT32"; 169 | case RKNN_TENSOR_UINT32: return "UINT32"; 170 | case RKNN_TENSOR_INT64: return "INT64"; 171 | case RKNN_TENSOR_BOOL: return "BOOL"; 172 | default: return "UNKNOW"; 173 | } 174 | } 175 | 176 | /* 177 | the quantitative type. 178 | */ 179 | typedef enum _rknn_tensor_qnt_type { 180 | RKNN_TENSOR_QNT_NONE = 0, /* none. */ 181 | RKNN_TENSOR_QNT_DFP, /* dynamic fixed point. */ 182 | RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC, /* asymmetric affine. */ 183 | 184 | RKNN_TENSOR_QNT_MAX 185 | } rknn_tensor_qnt_type; 186 | 187 | inline static const char* get_qnt_type_string(rknn_tensor_qnt_type type) 188 | { 189 | switch(type) { 190 | case RKNN_TENSOR_QNT_NONE: return "NONE"; 191 | case RKNN_TENSOR_QNT_DFP: return "DFP"; 192 | case RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC: return "AFFINE"; 193 | default: return "UNKNOW"; 194 | } 195 | } 196 | 197 | /* 198 | the tensor data format. 199 | */ 200 | typedef enum _rknn_tensor_format { 201 | RKNN_TENSOR_NCHW = 0, /* data format is NCHW. */ 202 | RKNN_TENSOR_NHWC, /* data format is NHWC. */ 203 | RKNN_TENSOR_NC1HWC2, /* data format is NC1HWC2. */ 204 | RKNN_TENSOR_UNDEFINED, 205 | 206 | RKNN_TENSOR_FORMAT_MAX 207 | } rknn_tensor_format; 208 | 209 | /* 210 | the mode of running on target NPU core. 211 | */ 212 | typedef enum _rknn_core_mask { 213 | RKNN_NPU_CORE_AUTO = 0, /* default, run on NPU core randomly. */ 214 | RKNN_NPU_CORE_0 = 1, /* run on NPU core 0. */ 215 | RKNN_NPU_CORE_1 = 2, /* run on NPU core 1. */ 216 | RKNN_NPU_CORE_2 = 4, /* run on NPU core 2. */ 217 | RKNN_NPU_CORE_0_1 = RKNN_NPU_CORE_0 | RKNN_NPU_CORE_1, /* run on NPU core 0 and core 1. */ 218 | RKNN_NPU_CORE_0_1_2 = RKNN_NPU_CORE_0_1 | RKNN_NPU_CORE_2, /* run on NPU core 0 and core 1 and core 2. */ 219 | 220 | RKNN_NPU_CORE_UNDEFINED, 221 | } rknn_core_mask; 222 | 223 | inline static const char* get_format_string(rknn_tensor_format fmt) 224 | { 225 | switch(fmt) { 226 | case RKNN_TENSOR_NCHW: return "NCHW"; 227 | case RKNN_TENSOR_NHWC: return "NHWC"; 228 | case RKNN_TENSOR_NC1HWC2: return "NC1HWC2"; 229 | case RKNN_TENSOR_UNDEFINED: return "UNDEFINED"; 230 | default: return "UNKNOW"; 231 | } 232 | } 233 | 234 | /* 235 | the information for RKNN_QUERY_IN_OUT_NUM. 236 | */ 237 | typedef struct _rknn_input_output_num { 238 | uint32_t n_input; /* the number of input. */ 239 | uint32_t n_output; /* the number of output. */ 240 | } rknn_input_output_num; 241 | 242 | /* 243 | the information for RKNN_QUERY_INPUT_ATTR / RKNN_QUERY_OUTPUT_ATTR. 244 | */ 245 | typedef struct _rknn_tensor_attr { 246 | uint32_t index; /* input parameter, the index of input/output tensor, 247 | need set before call rknn_query. */ 248 | 249 | uint32_t n_dims; /* the number of dimensions. */ 250 | uint32_t dims[RKNN_MAX_DIMS]; /* the dimensions array. */ 251 | char name[RKNN_MAX_NAME_LEN]; /* the name of tensor. */ 252 | 253 | uint32_t n_elems; /* the number of elements. */ 254 | uint32_t size; /* the bytes size of tensor. */ 255 | 256 | rknn_tensor_format fmt; /* the data format of tensor. */ 257 | rknn_tensor_type type; /* the data type of tensor. */ 258 | rknn_tensor_qnt_type qnt_type; /* the quantitative type of tensor. */ 259 | int8_t fl; /* fractional length for RKNN_TENSOR_QNT_DFP. */ 260 | int32_t zp; /* zero point for RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC. */ 261 | float scale; /* scale for RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC. */ 262 | 263 | uint32_t w_stride; /* the stride of tensor along the width dimention of input, 264 | Note: it is read-only, 0 means equal to width. */ 265 | uint32_t size_with_stride; /* the bytes size of tensor with stride. */ 266 | 267 | uint8_t pass_through; /* pass through mode, for rknn_set_io_mem interface. 268 | if TRUE, the buf data is passed directly to the input node of the rknn model 269 | without any conversion. the following variables do not need to be set. 270 | if FALSE, the buf data is converted into an input consistent with the model 271 | according to the following type and fmt. so the following variables 272 | need to be set.*/ 273 | uint32_t h_stride; /* the stride along the height dimention of input, 274 | Note: it is write-only, if it was set to 0, h_stride = height. */ 275 | } rknn_tensor_attr; 276 | 277 | typedef struct _rknn_input_range { 278 | uint32_t index; /* input parameter, the index of input/output tensor, 279 | need set before call rknn_query. */ 280 | uint32_t shape_number; /* the number of shape. */ 281 | rknn_tensor_format fmt; /* the data format of tensor. */ 282 | char name[RKNN_MAX_NAME_LEN]; /* the name of tensor. */ 283 | uint32_t dyn_range[RKNN_MAX_DYNAMIC_SHAPE_NUM][RKNN_MAX_DIMS]; /* the dynamic input dimensions range. */ 284 | uint32_t n_dims; /* the number of dimensions. */ 285 | 286 | } rknn_input_range; 287 | 288 | /* 289 | the information for RKNN_QUERY_PERF_DETAIL. 290 | */ 291 | typedef struct _rknn_perf_detail { 292 | char* perf_data; /* the string pointer of perf detail. don't need free it by user. */ 293 | uint64_t data_len; /* the string length. */ 294 | } rknn_perf_detail; 295 | 296 | /* 297 | the information for RKNN_QUERY_PERF_RUN. 298 | */ 299 | typedef struct _rknn_perf_run { 300 | int64_t run_duration; /* real inference time (us) */ 301 | } rknn_perf_run; 302 | 303 | /* 304 | the information for RKNN_QUERY_SDK_VERSION. 305 | */ 306 | typedef struct _rknn_sdk_version { 307 | char api_version[256]; /* the version of rknn api. */ 308 | char drv_version[256]; /* the version of rknn driver. */ 309 | } rknn_sdk_version; 310 | 311 | /* 312 | the information for RKNN_QUERY_MEM_SIZE. 313 | */ 314 | typedef struct _rknn_mem_size { 315 | uint32_t total_weight_size; /* the weight memory size */ 316 | uint32_t total_internal_size; /* the internal memory size, exclude inputs/outputs */ 317 | uint64_t total_dma_allocated_size; /* total dma memory allocated size */ 318 | uint32_t total_sram_size; /* total system sram size reserved for rknn */ 319 | uint32_t free_sram_size; /* free system sram size reserved for rknn */ 320 | uint32_t reserved[10]; /* reserved */ 321 | } rknn_mem_size; 322 | 323 | /* 324 | the information for RKNN_QUERY_CUSTOM_STRING. 325 | */ 326 | typedef struct _rknn_custom_string { 327 | char string[1024]; /* the string of custom, lengths max to 1024 bytes */ 328 | } rknn_custom_string; 329 | 330 | /* 331 | The flags of rknn_tensor_mem. 332 | */ 333 | typedef enum _rknn_tensor_mem_flags { 334 | RKNN_TENSOR_MEMORY_FLAGS_ALLOC_INSIDE = 1, /*Used to mark in rknn_destroy_mem() whether it is necessary to release the "mem" pointer itself. 335 | If the flag RKNN_TENSOR_MEMORY_FLAGS_ALLOC_INSIDE is set, rknn_destroy_mem() will call free(mem).*/ 336 | RKNN_TENSOR_MEMORY_FLAGS_FROM_FD = 2, /*Used to mark in rknn_create_mem_from_fd() whether it is necessary to release the "mem" pointer itself. 337 | If the flag RKNN_TENSOR_MEMORY_FLAGS_FROM_FD is set, rknn_destroy_mem() will call free(mem).*/ 338 | RKNN_TENSOR_MEMORY_FLAGS_FROM_PHYS = 3, /*Used to mark in rknn_create_mem_from_phys() whether it is necessary to release the "mem" pointer itself. 339 | If the flag RKNN_TENSOR_MEMORY_FLAGS_FROM_PHYS is set, rknn_destroy_mem() will call free(mem).*/ 340 | RKNN_TENSOR_MEMORY_FLAGS_UNKNOWN 341 | } rknn_tensor_mem_flags; 342 | 343 | /* 344 | the memory information of tensor. 345 | */ 346 | typedef struct _rknn_tensor_memory { 347 | void* virt_addr; /* the virtual address of tensor buffer. */ 348 | uint64_t phys_addr; /* the physical address of tensor buffer. */ 349 | int32_t fd; /* the fd of tensor buffer. */ 350 | int32_t offset; /* indicates the offset of the memory. */ 351 | uint32_t size; /* the size of tensor buffer. */ 352 | uint32_t flags; /* the flags of tensor buffer, reserved */ 353 | void * priv_data; /* the private data of tensor buffer. */ 354 | } rknn_tensor_mem; 355 | 356 | /* 357 | the input information for rknn_input_set. 358 | */ 359 | typedef struct _rknn_input { 360 | uint32_t index; /* the input index. */ 361 | void* buf; /* the input buf for index. */ 362 | uint32_t size; /* the size of input buf. */ 363 | uint8_t pass_through; /* pass through mode. 364 | if TRUE, the buf data is passed directly to the input node of the rknn model 365 | without any conversion. the following variables do not need to be set. 366 | if FALSE, the buf data is converted into an input consistent with the model 367 | according to the following type and fmt. so the following variables 368 | need to be set.*/ 369 | rknn_tensor_type type; /* the data type of input buf. */ 370 | rknn_tensor_format fmt; /* the data format of input buf. 371 | currently the internal input format of NPU is NCHW by default. 372 | so entering NCHW data can avoid the format conversion in the driver. */ 373 | } rknn_input; 374 | 375 | /* 376 | the output information for rknn_outputs_get. 377 | */ 378 | typedef struct _rknn_output { 379 | uint8_t want_float; /* want transfer output data to float */ 380 | uint8_t is_prealloc; /* whether buf is pre-allocated. 381 | if TRUE, the following variables need to be set. 382 | if FALSE, the following variables do not need to be set. */ 383 | uint32_t index; /* the output index. */ 384 | void* buf; /* the output buf for index. 385 | when is_prealloc = FALSE and rknn_outputs_release called, 386 | this buf pointer will be free and don't use it anymore. */ 387 | uint32_t size; /* the size of output buf. */ 388 | } rknn_output; 389 | 390 | /* 391 | the extend information for rknn_init. 392 | */ 393 | typedef struct _rknn_init_extend { 394 | rknn_context ctx; /* rknn context */ 395 | int32_t real_model_offset; /* real rknn model file offset, only valid when init context with rknn file path */ 396 | uint32_t real_model_size; /* real rknn model file size, only valid when init context with rknn file path */ 397 | uint8_t reserved[120]; /* reserved */ 398 | } rknn_init_extend; 399 | 400 | /* 401 | the extend information for rknn_run. 402 | */ 403 | typedef struct _rknn_run_extend { 404 | uint64_t frame_id; /* output parameter, indicate current frame id of run. */ 405 | int32_t non_block; /* block flag of run, 0 is block else 1 is non block */ 406 | int32_t timeout_ms; /* timeout for block mode, in milliseconds */ 407 | int32_t fence_fd; /* fence fd from other unit */ 408 | } rknn_run_extend; 409 | 410 | /* 411 | the extend information for rknn_outputs_get. 412 | */ 413 | typedef struct _rknn_output_extend { 414 | uint64_t frame_id; /* output parameter, indicate the frame id of outputs, corresponds to 415 | struct rknn_run_extend.frame_id.*/ 416 | } rknn_output_extend; 417 | 418 | 419 | /* rknn_init 420 | 421 | initial the context and load the rknn model. 422 | 423 | input: 424 | rknn_context* context the pointer of context handle. 425 | void* model if size > 0, pointer to the rknn model, if size = 0, filepath to the rknn model. 426 | uint32_t size the size of rknn model. 427 | uint32_t flag extend flag, see the define of RKNN_FLAG_XXX_XXX. 428 | rknn_init_extend* extend the extend information of init. 429 | return: 430 | int error code. 431 | */ 432 | int rknn_init(rknn_context* context, void* model, uint32_t size, uint32_t flag, rknn_init_extend* extend); 433 | 434 | /* rknn_dup_context 435 | 436 | initial the context and load the rknn model. 437 | 438 | input: 439 | rknn_context* context_in the pointer of context in handle. 440 | rknn_context* context_out the pointer of context out handle. 441 | return: 442 | int error code. 443 | */ 444 | int rknn_dup_context(rknn_context* context_in, rknn_context* context_out); 445 | 446 | /* rknn_destroy 447 | 448 | unload the rknn model and destroy the context. 449 | 450 | input: 451 | rknn_context context the handle of context. 452 | return: 453 | int error code. 454 | */ 455 | int rknn_destroy(rknn_context context); 456 | 457 | 458 | /* rknn_query 459 | 460 | query the information about model or others. see rknn_query_cmd. 461 | 462 | input: 463 | rknn_context context the handle of context. 464 | rknn_query_cmd cmd the command of query. 465 | void* info the buffer point of information. 466 | uint32_t size the size of information. 467 | return: 468 | int error code. 469 | */ 470 | int rknn_query(rknn_context context, rknn_query_cmd cmd, void* info, uint32_t size); 471 | 472 | 473 | /* rknn_inputs_set 474 | 475 | set inputs information by input index of rknn model. 476 | inputs information see rknn_input. 477 | 478 | input: 479 | rknn_context context the handle of context. 480 | uint32_t n_inputs the number of inputs. 481 | rknn_input inputs[] the arrays of inputs information, see rknn_input. 482 | return: 483 | int error code 484 | */ 485 | int rknn_inputs_set(rknn_context context, uint32_t n_inputs, rknn_input inputs[]); 486 | 487 | /* 488 | rknn_set_batch_core_num 489 | 490 | set rknn batch core_num. 491 | 492 | input: 493 | rknn_context context the handle of context. 494 | int core_num the core number. 495 | return: 496 | int error code. 497 | 498 | */ 499 | int rknn_set_batch_core_num(rknn_context context, int core_num); 500 | 501 | /* rknn_set_core_mask 502 | 503 | set rknn core mask.(only supported on RK3588 now) 504 | 505 | RKNN_NPU_CORE_AUTO: auto mode, default value 506 | RKNN_NPU_CORE_0: core 0 mode 507 | RKNN_NPU_CORE_1: core 1 mode 508 | RKNN_NPU_CORE_2: core 2 mode 509 | RKNN_NPU_CORE_0_1: combine core 0/1 mode 510 | RKNN_NPU_CORE_0_1_2: combine core 0/1/2 mode 511 | 512 | input: 513 | rknn_context context the handle of context. 514 | rknn_core_mask core_mask the core mask. 515 | return: 516 | int error code. 517 | */ 518 | int rknn_set_core_mask(rknn_context context, rknn_core_mask core_mask); 519 | 520 | /* rknn_run 521 | 522 | run the model to execute inference. 523 | 524 | input: 525 | rknn_context context the handle of context. 526 | rknn_run_extend* extend the extend information of run. 527 | return: 528 | int error code. 529 | */ 530 | int rknn_run(rknn_context context, rknn_run_extend* extend); 531 | 532 | 533 | /* rknn_wait 534 | 535 | wait the model after execute inference. 536 | 537 | input: 538 | rknn_context context the handle of context. 539 | rknn_run_extend* extend the extend information of run. 540 | return: 541 | int error code. 542 | */ 543 | int rknn_wait(rknn_context context, rknn_run_extend* extend); 544 | 545 | 546 | /* rknn_outputs_get 547 | 548 | wait the inference to finish and get the outputs. 549 | this function will block until inference finish. 550 | the results will set to outputs[]. 551 | 552 | input: 553 | rknn_context context the handle of context. 554 | uint32_t n_outputs the number of outputs. 555 | rknn_output outputs[] the arrays of output, see rknn_output. 556 | rknn_output_extend* the extend information of output. 557 | return: 558 | int error code. 559 | */ 560 | int rknn_outputs_get(rknn_context context, uint32_t n_outputs, rknn_output outputs[], rknn_output_extend* extend); 561 | 562 | 563 | /* rknn_outputs_release 564 | 565 | release the outputs that get by rknn_outputs_get. 566 | after called, the rknn_output[x].buf get from rknn_outputs_get will 567 | also be free when rknn_output[x].is_prealloc = FALSE. 568 | 569 | input: 570 | rknn_context context the handle of context. 571 | uint32_t n_ouputs the number of outputs. 572 | rknn_output outputs[] the arrays of output. 573 | return: 574 | int error code 575 | */ 576 | int rknn_outputs_release(rknn_context context, uint32_t n_ouputs, rknn_output outputs[]); 577 | 578 | 579 | /* new api for zero copy */ 580 | 581 | /* rknn_create_mem_from_phys (memory allocated outside) 582 | 583 | initialize tensor memory from physical address. 584 | 585 | input: 586 | rknn_context ctx the handle of context. 587 | uint64_t phys_addr physical address. 588 | void *virt_addr virtual address. 589 | uint32_t size the size of tensor buffer. 590 | return: 591 | rknn_tensor_mem the pointer of tensor memory information. 592 | */ 593 | rknn_tensor_mem* rknn_create_mem_from_phys(rknn_context ctx, uint64_t phys_addr, void *virt_addr, uint32_t size); 594 | 595 | 596 | /* rknn_create_mem_from_fd (memory allocated outside) 597 | 598 | initialize tensor memory from file description. 599 | 600 | input: 601 | rknn_context ctx the handle of context. 602 | int32_t fd file description. 603 | void *virt_addr virtual address. 604 | uint32_t size the size of tensor buffer. 605 | int32_t offset indicates the offset of the memory (virt_addr without offset). 606 | return: 607 | rknn_tensor_mem the pointer of tensor memory information. 608 | */ 609 | rknn_tensor_mem* rknn_create_mem_from_fd(rknn_context ctx, int32_t fd, void *virt_addr, uint32_t size, int32_t offset); 610 | 611 | 612 | /* rknn_create_mem_from_mb_blk (memory allocated outside) 613 | 614 | create tensor memory from mb_blk. 615 | 616 | input: 617 | rknn_context ctx the handle of context. 618 | void *mb_blk mb_blk allocate from system api. 619 | int32_t offset indicates the offset of the memory. 620 | return: 621 | rknn_tensor_mem the pointer of tensor memory information. 622 | */ 623 | rknn_tensor_mem* rknn_create_mem_from_mb_blk(rknn_context ctx, void *mb_blk, int32_t offset); 624 | 625 | 626 | /* rknn_create_mem (memory allocated inside) 627 | 628 | create tensor memory. 629 | 630 | input: 631 | rknn_context ctx the handle of context. 632 | uint32_t size the size of tensor buffer. 633 | return: 634 | rknn_tensor_mem the pointer of tensor memory information. 635 | */ 636 | rknn_tensor_mem* rknn_create_mem(rknn_context ctx, uint32_t size); 637 | 638 | 639 | /* rknn_destroy_mem (support allocate inside and outside) 640 | 641 | destroy tensor memory. 642 | 643 | input: 644 | rknn_context ctx the handle of context. 645 | rknn_tensor_mem *mem the pointer of tensor memory information. 646 | return: 647 | int error code 648 | */ 649 | int rknn_destroy_mem(rknn_context ctx, rknn_tensor_mem *mem); 650 | 651 | 652 | /* rknn_set_weight_mem 653 | 654 | set the weight memory. 655 | 656 | input: 657 | rknn_context ctx the handle of context. 658 | rknn_tensor_mem *mem the array of tensor memory information 659 | return: 660 | int error code. 661 | */ 662 | int rknn_set_weight_mem(rknn_context ctx, rknn_tensor_mem *mem); 663 | 664 | 665 | /* rknn_set_internal_mem 666 | 667 | set the internal memory. 668 | 669 | input: 670 | rknn_context ctx the handle of context. 671 | rknn_tensor_mem *mem the array of tensor memory information 672 | return: 673 | int error code. 674 | */ 675 | int rknn_set_internal_mem(rknn_context ctx, rknn_tensor_mem *mem); 676 | 677 | 678 | /* rknn_set_io_mem 679 | 680 | set the input and output tensors buffer. 681 | 682 | input: 683 | rknn_context ctx the handle of context. 684 | rknn_tensor_mem *mem the array of tensor memory information. 685 | rknn_tensor_attr *attr the attribute of input or output tensor buffer. 686 | return: 687 | int error code. 688 | */ 689 | int rknn_set_io_mem(rknn_context ctx, rknn_tensor_mem *mem, rknn_tensor_attr *attr); 690 | 691 | /* rknn_set_input_shape(deprecated) 692 | 693 | set the input tensor shape (only valid for dynamic shape rknn model). 694 | 695 | input: 696 | rknn_context ctx the handle of context. 697 | rknn_tensor_attr *attr the attribute of input or output tensor buffer. 698 | return: 699 | int error code. 700 | */ 701 | int rknn_set_input_shape(rknn_context ctx, rknn_tensor_attr* attr); 702 | 703 | /* rknn_set_input_shapes 704 | 705 | set all the input tensor shapes. graph will run under current set of input shapes after rknn_set_input_shapes.(only valid for dynamic shape rknn model). 706 | 707 | input: 708 | rknn_context ctx the handle of context. 709 | uint32_t n_inputs the number of inputs. 710 | rknn_tensor_attr attr[] the attribute array of all input tensors. 711 | return: 712 | int error code. 713 | */ 714 | int rknn_set_input_shapes(rknn_context ctx, uint32_t n_inputs, rknn_tensor_attr attr[]); 715 | 716 | #ifdef __cplusplus 717 | } //extern "C" 718 | #endif 719 | 720 | #endif //_RKNN_API_H 721 | -------------------------------------------------------------------------------- /include/rknn_matmul_api.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * Copyright (c) 2017 - 2018 by Rockchip Corp. All rights reserved. 4 | * 5 | * The material in this file is confidential and contains trade secrets 6 | * of Rockchip Corporation. This is proprietary information owned by 7 | * Rockchip Corporation. No part of this work may be disclosed, 8 | * reproduced, copied, transmitted, or used in any way for any purpose, 9 | * without the express written permission of Rockchip Corporation. 10 | * 11 | *****************************************************************************/ 12 | 13 | #ifndef _RKNN_MATMUL_API_H 14 | #define _RKNN_MATMUL_API_H 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | #include "rknn_api.h" 21 | 22 | typedef rknn_context rknn_matmul_ctx; 23 | 24 | typedef struct _rknn_matmul_tensor_attr 25 | { 26 | char name[RKNN_MAX_NAME_LEN]; 27 | 28 | // indicate A(M, K) or B(K, N) or C(M, N) 29 | uint32_t n_dims; 30 | uint32_t dims[RKNN_MAX_DIMS]; 31 | 32 | // matmul tensor size 33 | uint32_t size; 34 | 35 | // matmul tensor data type 36 | // int8 : A, B 37 | // int32: C 38 | rknn_tensor_type type; 39 | } rknn_matmul_tensor_attr; 40 | 41 | typedef struct _rknn_matmul_io_attr 42 | { 43 | // indicate A(M, K) or B(K, N) or C(M, N) 44 | rknn_matmul_tensor_attr A; 45 | rknn_matmul_tensor_attr B; 46 | rknn_matmul_tensor_attr C; 47 | } rknn_matmul_io_attr; 48 | 49 | /* 50 | matmul information struct 51 | */ 52 | typedef struct rknn_matmul_info_t 53 | { 54 | int32_t M; 55 | int32_t K; // limit: rk356x: int8 type must be aligned with 32byte, float16 type must be aligned with 16byte; 56 | // rk3588: int8 type must be aligned with 32byte, float16 type must be aligned with 32byte; 57 | int32_t N; // limit: rk356x: int8 type must be aligned with 16byte, float16 type must be aligned with 8byte; 58 | // rk3588: int8 type must be aligned with 32byte, float16 type must be aligned with 16byte; 59 | 60 | // matmul data type 61 | // int8: int8(A) x int8(B) -> int32(C) 62 | // float16: float16(A) x float16(B) -> float32(C) 63 | rknn_tensor_type type; 64 | 65 | // matmul native layout for B 66 | // 0: normal layout 67 | // 1: native layout 68 | int32_t native_layout; 69 | 70 | // matmul perf layout for A and C 71 | // 0: normal layout 72 | // 1: perf layout 73 | int32_t perf_layout; 74 | } rknn_matmul_info; 75 | 76 | /* rknn_matmul_create 77 | 78 | params: 79 | rknn_matmul_ctx *ctx the handle of context. 80 | rknn_matmul_info *info the matmal information. 81 | rknn_matmul_io_attr *io_attr inputs/output attribute 82 | return: 83 | int error code 84 | */ 85 | int rknn_matmul_create(rknn_matmul_ctx* ctx, rknn_matmul_info* info, rknn_matmul_io_attr* io_attr); 86 | 87 | /* rknn_matmul_set_io_mem 88 | 89 | params: 90 | rknn_matmul_ctx ctx the handle of context. 91 | rknn_tensor_mem *mem the pointer of tensor memory information. 92 | rknn_matmul_tensor_attr *attr the attribute of input or output tensor buffer. 93 | return: 94 | int error code. 95 | 96 | formula: 97 | C = A * B, 98 | 99 | limit: 100 | K <= 4096 101 | K limit: rk356x: int8 type must be aligned with 32byte, float16 type must be aligned with 16byte; 102 | rk3588: int8 type must be aligned with 32byte, float16 type must be aligned with 32byte; 103 | N limit: rk356x: int8 type must be aligned with 16byte, float16 type must be aligned with 8byte; 104 | rk3588: int8 type must be aligned with 32byte, float16 type must be aligned with 16byte; 105 | 106 | A shape: M x K 107 | normal layout: (M, K) 108 | [M1K1, M1K2, ..., M1Kk, 109 | M2K1, M2K2, ..., M2Kk, 110 | ... 111 | MmK1, MmK2, ..., MmKk] 112 | for rk356x: 113 | int8: 114 | perf layout: (K / 8, M, 8) 115 | [K1M1, K2M1, ..., K8M1, 116 | K9M2, K10M2, ..., K16M2, 117 | ... 118 | K(k-7)Mm, K(k-6)Mm, ..., KkMm] 119 | float16: 120 | perf layout: (K / 4, M, 4) 121 | [K1M1, K2M1, ..., K4M1, 122 | K9M2, K10M2, ..., K8M2, 123 | ... 124 | K(k-3)Mm, K(k-2)Mm, ..., KkMm] 125 | for rk3588: 126 | int8: 127 | perf layout: (K / 16, M, 16) 128 | [K1M1, K2M1, ..., K16M1, 129 | K9M2, K10M2, ..., K32M2, 130 | ... 131 | K(k-15)Mm, K(k-14)Mm, ..., KkMm] 132 | float16: 133 | perf layout: (K / 8, M, 8) 134 | [K1M1, K2M1, ..., K8M1, 135 | K9M2, K10M2, ..., K16M2, 136 | ... 137 | K(k-7)Mm, K(k-6)Mm, ..., KkMm] 138 | B shape: K x N 139 | normal layout: (K, N) 140 | [K1N1, K1N2, ..., K1Nn, 141 | K2N1, K2N2, ..., K2Nn, 142 | ... 143 | KkN1, KkN2, ..., KkNn] 144 | for rk356x: 145 | int8: 146 | native layout: (N / 16, K / 32, 16, 32) 147 | [K1N1, K2N1, ..., K32N1, 148 | K1N2, K2N2, ..., K32N2, 149 | ... 150 | K1N16, K2N16, ..., K32N16, 151 | K33N1, K34N1, ..., K64N1, 152 | K33N2, K34N2, ..., K64N2, 153 | ... 154 | K(k-31)N16, K(k-30)N16, ..., KkN16, 155 | K1N17, K2N17, ..., K32N17, 156 | K1N18, K2N18, ..., K32N18, 157 | ... 158 | K(k-31)Nn, K(k-30)Nn, ..., KkNn] 159 | float16: 160 | native layout: (N / 8, K / 16, 8, 16) 161 | [K1N1, K2N1, ..., K16N1, 162 | K1N2, K2N2, ..., K16N2, 163 | ... 164 | K1N8, K2N8, ..., K16N8, 165 | K17N1, K18N1, ..., K32N1, 166 | K17N2, K18N2, ..., K32N2, 167 | ... 168 | K(k-15)N8, K(k-30)N8, ..., KkN8, 169 | K1N9, K2N9, ..., K16N9, 170 | K1N10, K2N10, ..., K16N10, 171 | ... 172 | K(k-15)Nn, K(k-14)Nn, ..., KkNn] 173 | for rk3588: 174 | int8: 175 | native layout: (N / 32, K / 32, 32, 32) 176 | [K1N1, K2N1, ..., K32N1, 177 | K1N2, K2N2, ..., K32N2, 178 | ... 179 | K1N32, K2N32, ..., K32N32, 180 | K33N1, K34N1, ..., K64N1, 181 | K33N2, K34N2, ..., K64N2, 182 | ... 183 | K(k-31)N32, K(k-30)N32, ..., KkN32, 184 | K1N33, K2N33, ..., K32N33, 185 | K1N34, K2N34, ..., K32N34, 186 | ... 187 | K(k-31)Nn, K(k-30)Nn, ..., KkNn] 188 | float16: 189 | native layout: (N / 16, K / 32, 16, 32) 190 | [K1N1, K2N1, ..., K32N1, 191 | K1N2, K2N2, ..., K32N2, 192 | ... 193 | K1N16, K2N16, ..., K32N16, 194 | K33N1, K34N1, ..., K64N1, 195 | K33N2, K34N2, ..., K64N2, 196 | ... 197 | K(k-31)N16, K(k-30)N16, ..., KkN16, 198 | K1N17, K2N17, ..., K32N17, 199 | K1N18, K2N18, ..., K32N18, 200 | ... 201 | K(k-31)Nn, K(k-30)Nn, ..., KkNn] 202 | C shape: M x N 203 | normal layout: (M, N) 204 | [M1N1, M1N2, ..., M1Nn, 205 | M2N1, M2N2, ..., M2Nn, 206 | ... 207 | MmN1, MmN2, ..., MmNn] 208 | perf layout: (N / 4, M, 4) 209 | [N1M1, N2M1, ..., N4M1, 210 | N5M2, N6M2, ..., N8M2, 211 | ... 212 | N(n-3)Mm, N(n-2)Mm, ..., NnMm] 213 | */ 214 | int rknn_matmul_set_io_mem(rknn_matmul_ctx ctx, rknn_tensor_mem* mem, rknn_matmul_tensor_attr* attr); 215 | 216 | /* rknn_matmul_set_core_mask 217 | 218 | set rknn core mask.(only support rk3588 in current) 219 | 220 | RKNN_NPU_CORE_AUTO: auto mode, default value 221 | RKNN_NPU_CORE_0: core 0 mode 222 | RKNN_NPU_CORE_1: core 1 mode 223 | RKNN_NPU_CORE_2: core 2 mode 224 | RKNN_NPU_CORE_0_1: combine core 0/1 mode 225 | RKNN_NPU_CORE_0_1_2: combine core 0/1/2 mode 226 | 227 | input: 228 | rknn_matmul_ctx context the handle of context. 229 | rknn_core_mask core_mask the core mask. 230 | return: 231 | int error code. 232 | */ 233 | int rknn_matmul_set_core_mask(rknn_matmul_ctx context, rknn_core_mask core_mask); 234 | 235 | /* rknn_matmul_run 236 | 237 | run the matmul in blocking mode 238 | 239 | params: 240 | rknn_matmul_ctx ctx the handle of context. 241 | return: 242 | int error code. 243 | */ 244 | int rknn_matmul_run(rknn_matmul_ctx ctx); 245 | 246 | /* rknn_matmul_destroy 247 | 248 | destroy the matmul context 249 | 250 | params: 251 | rknn_matmul_ctx ctx the handle of context. 252 | return: 253 | int error code. 254 | */ 255 | int rknn_matmul_destroy(rknn_matmul_ctx ctx); 256 | 257 | #ifdef __cplusplus 258 | } // extern "C" 259 | #endif 260 | 261 | #endif // _RKNN_MATMUL_API_H -------------------------------------------------------------------------------- /model/RK3588/yolov5s-640-640.rknn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leafqycc/rknn-cpp-Multithreading/d86b5a99b8cdbd6b77c25e7aa3ec77fd1b7d5f6e/model/RK3588/yolov5s-640-640.rknn -------------------------------------------------------------------------------- /model/coco_80_labels_list.txt: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorcycle 5 | airplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | couch 59 | potted plant 60 | bed 61 | dining table 62 | toilet 63 | tv 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush 81 | -------------------------------------------------------------------------------- /performance.sh: -------------------------------------------------------------------------------- 1 | # 请切换到root用户 2 | 3 | # CPU定频 4 | echo "CPU0-3可用频率/CPU6-7 available frequency:" 5 | sudo cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies 6 | sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor 7 | sudo echo 1800000 > /sys/devices/system/cpu/cpufreq/policy0/scaling_setspeed 8 | echo "CPU0-3当前频率/CPU0-3 current frequency:" 9 | sudo cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq 10 | 11 | echo "CPU4-5可用频率/CPU6-7 available frequency:" 12 | sudo cat /sys/devices/system/cpu/cpufreq/policy4/scaling_available_frequencies 13 | sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy4/scaling_governor 14 | sudo echo 2400000 > /sys/devices/system/cpu/cpufreq/policy4/scaling_setspeed 15 | echo "CPU4-5 当前频率/CPU4-5 current frequency:" 16 | sudo cat /sys/devices/system/cpu/cpufreq/policy4/cpuinfo_cur_freq 17 | 18 | echo "CPU6-7可用频率:/CPU6-7 available frequency" 19 | sudo cat /sys/devices/system/cpu/cpufreq/policy6/scaling_available_frequencies 20 | sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy6/scaling_governor 21 | sudo echo 2400000 > /sys/devices/system/cpu/cpufreq/policy6/scaling_setspeed 22 | echo "CPU6-7 当前频率/CPU6-7 current frequency:" 23 | sudo cat /sys/devices/system/cpu/cpufreq/policy6/cpuinfo_cur_freq 24 | 25 | # NPU定频 26 | echo "NPU可用频率/NPU available frequency:" 27 | sudo cat /sys/class/devfreq/fdab0000.npu/available_frequencies 28 | sudo echo userspace > /sys/class/devfreq/fdab0000.npu/governor 29 | sudo echo 1000000000 > /sys/class/devfreq/fdab0000.npu/userspace/set_freq 30 | echo "NPU当前频率/NPU current frequency:" 31 | sudo cat /sys/class/devfreq/fdab0000.npu/cur_freq 32 | -------------------------------------------------------------------------------- /src/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "opencv2/core/core.hpp" 6 | #include "opencv2/highgui/highgui.hpp" 7 | #include "opencv2/imgproc/imgproc.hpp" 8 | #include "rkYolov5s.hpp" 9 | #include "rknnPool.hpp" 10 | int main(int argc, char **argv) 11 | { 12 | char *model_name = NULL; 13 | if (argc != 3) 14 | { 15 | printf("Usage: %s \n", argv[0]); 16 | return -1; 17 | } 18 | // 参数二,模型所在路径/The path where the model is located 19 | model_name = (char *)argv[1]; 20 | // 参数三, 视频/摄像头 21 | char *vedio_name = argv[2]; 22 | 23 | // 初始化rknn线程池/Initialize the rknn thread pool 24 | int threadNum = 3; 25 | rknnPool testPool(model_name, threadNum); 26 | if (testPool.init() != 0) 27 | { 28 | printf("rknnPool init fail!\n"); 29 | return -1; 30 | } 31 | 32 | cv::namedWindow("Camera FPS"); 33 | cv::VideoCapture capture; 34 | if (strlen(vedio_name) == 1) 35 | capture.open((int)(vedio_name[0] - '0')); 36 | else 37 | capture.open(vedio_name); 38 | 39 | struct timeval time; 40 | gettimeofday(&time, nullptr); 41 | auto startTime = time.tv_sec * 1000 + time.tv_usec / 1000; 42 | 43 | int frames = 0; 44 | auto beforeTime = startTime; 45 | while (capture.isOpened()) 46 | { 47 | cv::Mat img; 48 | if (capture.read(img) == false) 49 | break; 50 | if (testPool.put(img) != 0) 51 | break; 52 | 53 | if (frames >= threadNum && testPool.get(img) != 0) 54 | break; 55 | cv::imshow("Camera FPS", img); 56 | if (cv::waitKey(1) == 'q') // 延时1毫秒,按q键退出/Press q to exit 57 | break; 58 | frames++; 59 | 60 | if (frames % 120 == 0) 61 | { 62 | gettimeofday(&time, nullptr); 63 | auto currentTime = time.tv_sec * 1000 + time.tv_usec / 1000; 64 | printf("120帧内平均帧率:\t %f fps/s\n", 120.0 / float(currentTime - beforeTime) * 1000.0); 65 | beforeTime = currentTime; 66 | } 67 | } 68 | 69 | // 清空rknn线程池/Clear the thread pool 70 | while (true) 71 | { 72 | cv::Mat img; 73 | if (testPool.get(img) != 0) 74 | break; 75 | cv::imshow("Camera FPS", img); 76 | if (cv::waitKey(1) == 'q') // 延时1毫秒,按q键退出/Press q to exit 77 | break; 78 | frames++; 79 | } 80 | 81 | gettimeofday(&time, nullptr); 82 | auto endTime = time.tv_sec * 1000 + time.tv_usec / 1000; 83 | 84 | printf("Average:\t %f fps/s\n", float(frames) / float(endTime - startTime) * 1000.0); 85 | 86 | return 0; 87 | } -------------------------------------------------------------------------------- /src/postprocess.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 by Rockchip Electronics Co., Ltd. All Rights Reserved. 2 | // 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 | #include "postprocess.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #define LABEL_NALE_TXT_PATH "./model/coco_80_labels_list.txt" 27 | 28 | static char *labels[OBJ_CLASS_NUM]; 29 | 30 | const int anchor0[6] = {10, 13, 16, 30, 33, 23}; 31 | const int anchor1[6] = {30, 61, 62, 45, 59, 119}; 32 | const int anchor2[6] = {116, 90, 156, 198, 373, 326}; 33 | 34 | inline static int clamp(float val, int min, int max) { return val > min ? (val < max ? val : max) : min; } 35 | 36 | char *readLine(FILE *fp, char *buffer, int *len) 37 | { 38 | int ch; 39 | int i = 0; 40 | size_t buff_len = 0; 41 | 42 | buffer = (char *)malloc(buff_len + 1); 43 | if (!buffer) 44 | return NULL; // Out of memory 45 | 46 | while ((ch = fgetc(fp)) != '\n' && ch != EOF) 47 | { 48 | buff_len++; 49 | void *tmp = realloc(buffer, buff_len + 1); 50 | if (tmp == NULL) 51 | { 52 | free(buffer); 53 | return NULL; // Out of memory 54 | } 55 | buffer = (char *)tmp; 56 | 57 | buffer[i] = (char)ch; 58 | i++; 59 | } 60 | buffer[i] = '\0'; 61 | 62 | *len = buff_len; 63 | 64 | // Detect end 65 | if (ch == EOF && (i == 0 || ferror(fp))) 66 | { 67 | free(buffer); 68 | return NULL; 69 | } 70 | return buffer; 71 | } 72 | 73 | int readLines(const char *fileName, char *lines[], int max_line) 74 | { 75 | FILE *file = fopen(fileName, "r"); 76 | char *s; 77 | int i = 0; 78 | int n = 0; 79 | 80 | if (file == NULL) 81 | { 82 | printf("Open %s fail!\n", fileName); 83 | return -1; 84 | } 85 | 86 | while ((s = readLine(file, s, &n)) != NULL) 87 | { 88 | lines[i++] = s; 89 | if (i >= max_line) 90 | break; 91 | } 92 | fclose(file); 93 | return i; 94 | } 95 | 96 | int loadLabelName(const char *locationFilename, char *label[]) 97 | { 98 | printf("loadLabelName %s\n", locationFilename); 99 | readLines(locationFilename, label, OBJ_CLASS_NUM); 100 | return 0; 101 | } 102 | 103 | static float CalculateOverlap(float xmin0, float ymin0, float xmax0, float ymax0, float xmin1, float ymin1, float xmax1, 104 | float ymax1) 105 | { 106 | float w = fmax(0.f, fmin(xmax0, xmax1) - fmax(xmin0, xmin1) + 1.0); 107 | float h = fmax(0.f, fmin(ymax0, ymax1) - fmax(ymin0, ymin1) + 1.0); 108 | float i = w * h; 109 | float u = (xmax0 - xmin0 + 1.0) * (ymax0 - ymin0 + 1.0) + (xmax1 - xmin1 + 1.0) * (ymax1 - ymin1 + 1.0) - i; 110 | return u <= 0.f ? 0.f : (i / u); 111 | } 112 | 113 | static int nms(int validCount, std::vector &outputLocations, std::vector classIds, std::vector &order, 114 | int filterId, float threshold) 115 | { 116 | for (int i = 0; i < validCount; ++i) 117 | { 118 | if (order[i] == -1 || classIds[i] != filterId) 119 | { 120 | continue; 121 | } 122 | int n = order[i]; 123 | for (int j = i + 1; j < validCount; ++j) 124 | { 125 | int m = order[j]; 126 | if (m == -1 || classIds[i] != filterId) 127 | { 128 | continue; 129 | } 130 | float xmin0 = outputLocations[n * 4 + 0]; 131 | float ymin0 = outputLocations[n * 4 + 1]; 132 | float xmax0 = outputLocations[n * 4 + 0] + outputLocations[n * 4 + 2]; 133 | float ymax0 = outputLocations[n * 4 + 1] + outputLocations[n * 4 + 3]; 134 | 135 | float xmin1 = outputLocations[m * 4 + 0]; 136 | float ymin1 = outputLocations[m * 4 + 1]; 137 | float xmax1 = outputLocations[m * 4 + 0] + outputLocations[m * 4 + 2]; 138 | float ymax1 = outputLocations[m * 4 + 1] + outputLocations[m * 4 + 3]; 139 | 140 | float iou = CalculateOverlap(xmin0, ymin0, xmax0, ymax0, xmin1, ymin1, xmax1, ymax1); 141 | 142 | if (iou > threshold) 143 | { 144 | order[j] = -1; 145 | } 146 | } 147 | } 148 | return 0; 149 | } 150 | 151 | static int quick_sort_indice_inverse(std::vector &input, int left, int right, std::vector &indices) 152 | { 153 | float key; 154 | int key_index; 155 | int low = left; 156 | int high = right; 157 | if (left < right) 158 | { 159 | key_index = indices[left]; 160 | key = input[left]; 161 | while (low < high) 162 | { 163 | while (low < high && input[high] <= key) 164 | { 165 | high--; 166 | } 167 | input[low] = input[high]; 168 | indices[low] = indices[high]; 169 | while (low < high && input[low] >= key) 170 | { 171 | low++; 172 | } 173 | input[high] = input[low]; 174 | indices[high] = indices[low]; 175 | } 176 | input[low] = key; 177 | indices[low] = key_index; 178 | quick_sort_indice_inverse(input, left, low - 1, indices); 179 | quick_sort_indice_inverse(input, low + 1, right, indices); 180 | } 181 | return low; 182 | } 183 | 184 | static float sigmoid(float x) { return 1.0 / (1.0 + expf(-x)); } 185 | 186 | static float unsigmoid(float y) { return -1.0 * logf((1.0 / y) - 1.0); } 187 | 188 | inline static int32_t __clip(float val, float min, float max) 189 | { 190 | float f = val <= min ? min : (val >= max ? max : val); 191 | return f; 192 | } 193 | 194 | static int8_t qnt_f32_to_affine(float f32, int32_t zp, float scale) 195 | { 196 | float dst_val = (f32 / scale) + zp; 197 | int8_t res = (int8_t)__clip(dst_val, -128, 127); 198 | return res; 199 | } 200 | 201 | static float deqnt_affine_to_f32(int8_t qnt, int32_t zp, float scale) { return ((float)qnt - (float)zp) * scale; } 202 | 203 | static int process(int8_t *input, int *anchor, int grid_h, int grid_w, int height, int width, int stride, 204 | std::vector &boxes, std::vector &objProbs, std::vector &classId, float threshold, 205 | int32_t zp, float scale) 206 | { 207 | int validCount = 0; 208 | int grid_len = grid_h * grid_w; 209 | int8_t thres_i8 = qnt_f32_to_affine(threshold, zp, scale); 210 | for (int a = 0; a < 3; a++) 211 | { 212 | for (int i = 0; i < grid_h; i++) 213 | { 214 | for (int j = 0; j < grid_w; j++) 215 | { 216 | int8_t box_confidence = input[(PROP_BOX_SIZE * a + 4) * grid_len + i * grid_w + j]; 217 | if (box_confidence >= thres_i8) 218 | { 219 | int offset = (PROP_BOX_SIZE * a) * grid_len + i * grid_w + j; 220 | int8_t *in_ptr = input + offset; 221 | float box_x = (deqnt_affine_to_f32(*in_ptr, zp, scale)) * 2.0 - 0.5; 222 | float box_y = (deqnt_affine_to_f32(in_ptr[grid_len], zp, scale)) * 2.0 - 0.5; 223 | float box_w = (deqnt_affine_to_f32(in_ptr[2 * grid_len], zp, scale)) * 2.0; 224 | float box_h = (deqnt_affine_to_f32(in_ptr[3 * grid_len], zp, scale)) * 2.0; 225 | box_x = (box_x + j) * (float)stride; 226 | box_y = (box_y + i) * (float)stride; 227 | box_w = box_w * box_w * (float)anchor[a * 2]; 228 | box_h = box_h * box_h * (float)anchor[a * 2 + 1]; 229 | box_x -= (box_w / 2.0); 230 | box_y -= (box_h / 2.0); 231 | 232 | int8_t maxClassProbs = in_ptr[5 * grid_len]; 233 | int maxClassId = 0; 234 | for (int k = 1; k < OBJ_CLASS_NUM; ++k) 235 | { 236 | int8_t prob = in_ptr[(5 + k) * grid_len]; 237 | if (prob > maxClassProbs) 238 | { 239 | maxClassId = k; 240 | maxClassProbs = prob; 241 | } 242 | } 243 | if (maxClassProbs > thres_i8) 244 | { 245 | objProbs.push_back((deqnt_affine_to_f32(maxClassProbs, zp, scale)) * (deqnt_affine_to_f32(box_confidence, zp, scale))); 246 | classId.push_back(maxClassId); 247 | validCount++; 248 | boxes.push_back(box_x); 249 | boxes.push_back(box_y); 250 | boxes.push_back(box_w); 251 | boxes.push_back(box_h); 252 | } 253 | } 254 | } 255 | } 256 | } 257 | return validCount; 258 | } 259 | 260 | int post_process(int8_t *input0, int8_t *input1, int8_t *input2, int model_in_h, int model_in_w, float conf_threshold, 261 | float nms_threshold, BOX_RECT pads, float scale_w, float scale_h, std::vector &qnt_zps, 262 | std::vector &qnt_scales, detect_result_group_t *group) 263 | { 264 | static int init = -1; 265 | if (init == -1) 266 | { 267 | int ret = 0; 268 | ret = loadLabelName(LABEL_NALE_TXT_PATH, labels); 269 | if (ret < 0) 270 | { 271 | return -1; 272 | } 273 | 274 | init = 0; 275 | } 276 | memset(group, 0, sizeof(detect_result_group_t)); 277 | 278 | std::vector filterBoxes; 279 | std::vector objProbs; 280 | std::vector classId; 281 | 282 | // stride 8 283 | int stride0 = 8; 284 | int grid_h0 = model_in_h / stride0; 285 | int grid_w0 = model_in_w / stride0; 286 | int validCount0 = 0; 287 | validCount0 = process(input0, (int *)anchor0, grid_h0, grid_w0, model_in_h, model_in_w, stride0, filterBoxes, objProbs, 288 | classId, conf_threshold, qnt_zps[0], qnt_scales[0]); 289 | 290 | // stride 16 291 | int stride1 = 16; 292 | int grid_h1 = model_in_h / stride1; 293 | int grid_w1 = model_in_w / stride1; 294 | int validCount1 = 0; 295 | validCount1 = process(input1, (int *)anchor1, grid_h1, grid_w1, model_in_h, model_in_w, stride1, filterBoxes, objProbs, 296 | classId, conf_threshold, qnt_zps[1], qnt_scales[1]); 297 | 298 | // stride 32 299 | int stride2 = 32; 300 | int grid_h2 = model_in_h / stride2; 301 | int grid_w2 = model_in_w / stride2; 302 | int validCount2 = 0; 303 | validCount2 = process(input2, (int *)anchor2, grid_h2, grid_w2, model_in_h, model_in_w, stride2, filterBoxes, objProbs, 304 | classId, conf_threshold, qnt_zps[2], qnt_scales[2]); 305 | 306 | int validCount = validCount0 + validCount1 + validCount2; 307 | // no object detect 308 | if (validCount <= 0) 309 | { 310 | return 0; 311 | } 312 | 313 | std::vector indexArray; 314 | for (int i = 0; i < validCount; ++i) 315 | { 316 | indexArray.push_back(i); 317 | } 318 | 319 | quick_sort_indice_inverse(objProbs, 0, validCount - 1, indexArray); 320 | 321 | std::set class_set(std::begin(classId), std::end(classId)); 322 | 323 | for (auto c : class_set) 324 | { 325 | nms(validCount, filterBoxes, classId, indexArray, c, nms_threshold); 326 | } 327 | 328 | int last_count = 0; 329 | group->count = 0; 330 | /* box valid detect target */ 331 | for (int i = 0; i < validCount; ++i) 332 | { 333 | if (indexArray[i] == -1 || last_count >= OBJ_NUMB_MAX_SIZE) 334 | { 335 | continue; 336 | } 337 | int n = indexArray[i]; 338 | 339 | float x1 = filterBoxes[n * 4 + 0] - pads.left; 340 | float y1 = filterBoxes[n * 4 + 1] - pads.top; 341 | float x2 = x1 + filterBoxes[n * 4 + 2]; 342 | float y2 = y1 + filterBoxes[n * 4 + 3]; 343 | int id = classId[n]; 344 | float obj_conf = objProbs[i]; 345 | 346 | group->results[last_count].box.left = (int)(clamp(x1, 0, model_in_w) / scale_w); 347 | group->results[last_count].box.top = (int)(clamp(y1, 0, model_in_h) / scale_h); 348 | group->results[last_count].box.right = (int)(clamp(x2, 0, model_in_w) / scale_w); 349 | group->results[last_count].box.bottom = (int)(clamp(y2, 0, model_in_h) / scale_h); 350 | group->results[last_count].prop = obj_conf; 351 | char *label = labels[id]; 352 | strncpy(group->results[last_count].name, label, OBJ_NAME_MAX_SIZE); 353 | 354 | // printf("result %2d: (%4d, %4d, %4d, %4d), %s\n", i, group->results[last_count].box.left, 355 | // group->results[last_count].box.top, 356 | // group->results[last_count].box.right, group->results[last_count].box.bottom, label); 357 | last_count++; 358 | } 359 | group->count = last_count; 360 | 361 | return 0; 362 | } 363 | 364 | void deinitPostProcess() 365 | { 366 | for (int i = 0; i < OBJ_CLASS_NUM; i++) 367 | { 368 | if (labels[i] != nullptr) 369 | { 370 | free(labels[i]); 371 | labels[i] = nullptr; 372 | } 373 | } 374 | } 375 | -------------------------------------------------------------------------------- /src/preprocess.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2023 by Rockchip Electronics Co., Ltd. All Rights Reserved. 2 | // 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 | #include 16 | #include "im2d.h" 17 | #include "rga.h" 18 | #include "opencv2/core/core.hpp" 19 | #include "opencv2/imgcodecs.hpp" 20 | #include "opencv2/imgproc.hpp" 21 | #include "postprocess.h" 22 | 23 | void letterbox(const cv::Mat &image, cv::Mat &padded_image, BOX_RECT &pads, const float scale, const cv::Size &target_size, const cv::Scalar &pad_color) 24 | { 25 | // 调整图像大小 26 | cv::Mat resized_image; 27 | cv::resize(image, resized_image, cv::Size(), scale, scale); 28 | 29 | // 计算填充大小 30 | int pad_width = target_size.width - resized_image.cols; 31 | int pad_height = target_size.height - resized_image.rows; 32 | 33 | pads.left = pad_width / 2; 34 | pads.right = pad_width - pads.left; 35 | pads.top = pad_height / 2; 36 | pads.bottom = pad_height - pads.top; 37 | 38 | // 在图像周围添加填充 39 | cv::copyMakeBorder(resized_image, padded_image, pads.top, pads.bottom, pads.left, pads.right, cv::BORDER_CONSTANT, pad_color); 40 | } 41 | 42 | int resize_rga(rga_buffer_t &src, rga_buffer_t &dst, const cv::Mat &image, cv::Mat &resized_image, const cv::Size &target_size) 43 | { 44 | im_rect src_rect; 45 | im_rect dst_rect; 46 | memset(&src_rect, 0, sizeof(src_rect)); 47 | memset(&dst_rect, 0, sizeof(dst_rect)); 48 | size_t img_width = image.cols; 49 | size_t img_height = image.rows; 50 | if (image.type() != CV_8UC3) 51 | { 52 | printf("source image type is %d!\n", image.type()); 53 | return -1; 54 | } 55 | size_t target_width = target_size.width; 56 | size_t target_height = target_size.height; 57 | src = wrapbuffer_virtualaddr((void *)image.data, img_width, img_height, RK_FORMAT_RGB_888); 58 | dst = wrapbuffer_virtualaddr((void *)resized_image.data, target_width, target_height, RK_FORMAT_RGB_888); 59 | int ret = imcheck(src, dst, src_rect, dst_rect); 60 | if (IM_STATUS_NOERROR != ret) 61 | { 62 | fprintf(stderr, "rga check error! %s", imStrError((IM_STATUS)ret)); 63 | return -1; 64 | } 65 | IM_STATUS STATUS = imresize(src, dst); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /src/rkYolov5s.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "rknn_api.h" 4 | 5 | #include "postprocess.h" 6 | #include "preprocess.h" 7 | 8 | #include "opencv2/core/core.hpp" 9 | #include "opencv2/highgui/highgui.hpp" 10 | #include "opencv2/imgproc/imgproc.hpp" 11 | 12 | #include "coreNum.hpp" 13 | #include "rkYolov5s.hpp" 14 | 15 | static void dump_tensor_attr(rknn_tensor_attr *attr) 16 | { 17 | std::string shape_str = attr->n_dims < 1 ? "" : std::to_string(attr->dims[0]); 18 | for (int i = 1; i < attr->n_dims; ++i) 19 | { 20 | shape_str += ", " + std::to_string(attr->dims[i]); 21 | } 22 | 23 | // printf(" index=%d, name=%s, n_dims=%d, dims=[%s], n_elems=%d, size=%d, w_stride = %d, size_with_stride=%d, fmt=%s, " 24 | // "type=%s, qnt_type=%s, " 25 | // "zp=%d, scale=%f\n", 26 | // attr->index, attr->name, attr->n_dims, shape_str.c_str(), attr->n_elems, attr->size, attr->w_stride, 27 | // attr->size_with_stride, get_format_string(attr->fmt), get_type_string(attr->type), 28 | // get_qnt_type_string(attr->qnt_type), attr->zp, attr->scale); 29 | } 30 | 31 | static unsigned char *load_data(FILE *fp, size_t ofst, size_t sz) 32 | { 33 | unsigned char *data; 34 | int ret; 35 | 36 | data = NULL; 37 | 38 | if (NULL == fp) 39 | { 40 | return NULL; 41 | } 42 | 43 | ret = fseek(fp, ofst, SEEK_SET); 44 | if (ret != 0) 45 | { 46 | printf("blob seek failure.\n"); 47 | return NULL; 48 | } 49 | 50 | data = (unsigned char *)malloc(sz); 51 | if (data == NULL) 52 | { 53 | printf("buffer malloc failure.\n"); 54 | return NULL; 55 | } 56 | ret = fread(data, 1, sz, fp); 57 | return data; 58 | } 59 | 60 | static unsigned char *load_model(const char *filename, int *model_size) 61 | { 62 | FILE *fp; 63 | unsigned char *data; 64 | 65 | fp = fopen(filename, "rb"); 66 | if (NULL == fp) 67 | { 68 | printf("Open file %s failed.\n", filename); 69 | return NULL; 70 | } 71 | 72 | fseek(fp, 0, SEEK_END); 73 | int size = ftell(fp); 74 | 75 | data = load_data(fp, 0, size); 76 | 77 | fclose(fp); 78 | 79 | *model_size = size; 80 | return data; 81 | } 82 | 83 | static int saveFloat(const char *file_name, float *output, int element_size) 84 | { 85 | FILE *fp; 86 | fp = fopen(file_name, "w"); 87 | for (int i = 0; i < element_size; i++) 88 | { 89 | fprintf(fp, "%.6f\n", output[i]); 90 | } 91 | fclose(fp); 92 | return 0; 93 | } 94 | 95 | rkYolov5s::rkYolov5s(const std::string &model_path) 96 | { 97 | this->model_path = model_path; 98 | nms_threshold = NMS_THRESH; // 默认的NMS阈值 99 | box_conf_threshold = BOX_THRESH; // 默认的置信度阈值 100 | } 101 | 102 | int rkYolov5s::init(rknn_context *ctx_in, bool share_weight) 103 | { 104 | printf("Loading model...\n"); 105 | int model_data_size = 0; 106 | model_data = load_model(model_path.c_str(), &model_data_size); 107 | // 模型参数复用/Model parameter reuse 108 | if (share_weight == true) 109 | ret = rknn_dup_context(ctx_in, &ctx); 110 | else 111 | ret = rknn_init(&ctx, model_data, model_data_size, 0, NULL); 112 | if (ret < 0) 113 | { 114 | printf("rknn_init error ret=%d\n", ret); 115 | return -1; 116 | } 117 | 118 | // 设置模型绑定的核心/Set the core of the model that needs to be bound 119 | rknn_core_mask core_mask; 120 | switch (get_core_num()) 121 | { 122 | case 0: 123 | core_mask = RKNN_NPU_CORE_0; 124 | break; 125 | case 1: 126 | core_mask = RKNN_NPU_CORE_1; 127 | break; 128 | case 2: 129 | core_mask = RKNN_NPU_CORE_2; 130 | break; 131 | } 132 | ret = rknn_set_core_mask(ctx, core_mask); 133 | if (ret < 0) 134 | { 135 | printf("rknn_init core error ret=%d\n", ret); 136 | return -1; 137 | } 138 | 139 | rknn_sdk_version version; 140 | ret = rknn_query(ctx, RKNN_QUERY_SDK_VERSION, &version, sizeof(rknn_sdk_version)); 141 | if (ret < 0) 142 | { 143 | printf("rknn_init error ret=%d\n", ret); 144 | return -1; 145 | } 146 | printf("sdk version: %s driver version: %s\n", version.api_version, version.drv_version); 147 | 148 | // 获取模型输入输出参数/Obtain the input and output parameters of the model 149 | ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num)); 150 | if (ret < 0) 151 | { 152 | printf("rknn_init error ret=%d\n", ret); 153 | return -1; 154 | } 155 | printf("model input num: %d, output num: %d\n", io_num.n_input, io_num.n_output); 156 | 157 | // 设置输入参数/Set the input parameters 158 | input_attrs = (rknn_tensor_attr *)calloc(io_num.n_input, sizeof(rknn_tensor_attr)); 159 | for (int i = 0; i < io_num.n_input; i++) 160 | { 161 | input_attrs[i].index = i; 162 | ret = rknn_query(ctx, RKNN_QUERY_INPUT_ATTR, &(input_attrs[i]), sizeof(rknn_tensor_attr)); 163 | if (ret < 0) 164 | { 165 | printf("rknn_init error ret=%d\n", ret); 166 | return -1; 167 | } 168 | dump_tensor_attr(&(input_attrs[i])); 169 | } 170 | 171 | // 设置输出参数/Set the output parameters 172 | output_attrs = (rknn_tensor_attr *)calloc(io_num.n_output, sizeof(rknn_tensor_attr)); 173 | for (int i = 0; i < io_num.n_output; i++) 174 | { 175 | output_attrs[i].index = i; 176 | ret = rknn_query(ctx, RKNN_QUERY_OUTPUT_ATTR, &(output_attrs[i]), sizeof(rknn_tensor_attr)); 177 | dump_tensor_attr(&(output_attrs[i])); 178 | } 179 | 180 | if (input_attrs[0].fmt == RKNN_TENSOR_NCHW) 181 | { 182 | printf("model is NCHW input fmt\n"); 183 | channel = input_attrs[0].dims[1]; 184 | height = input_attrs[0].dims[2]; 185 | width = input_attrs[0].dims[3]; 186 | } 187 | else 188 | { 189 | printf("model is NHWC input fmt\n"); 190 | height = input_attrs[0].dims[1]; 191 | width = input_attrs[0].dims[2]; 192 | channel = input_attrs[0].dims[3]; 193 | } 194 | printf("model input height=%d, width=%d, channel=%d\n", height, width, channel); 195 | 196 | memset(inputs, 0, sizeof(inputs)); 197 | inputs[0].index = 0; 198 | inputs[0].type = RKNN_TENSOR_UINT8; 199 | inputs[0].size = width * height * channel; 200 | inputs[0].fmt = RKNN_TENSOR_NHWC; 201 | inputs[0].pass_through = 0; 202 | 203 | return 0; 204 | } 205 | 206 | rknn_context *rkYolov5s::get_pctx() 207 | { 208 | return &ctx; 209 | } 210 | 211 | cv::Mat rkYolov5s::infer(cv::Mat &orig_img) 212 | { 213 | std::lock_guard lock(mtx); 214 | cv::Mat img; 215 | cv::cvtColor(orig_img, img, cv::COLOR_BGR2RGB); 216 | img_width = img.cols; 217 | img_height = img.rows; 218 | 219 | BOX_RECT pads; 220 | memset(&pads, 0, sizeof(BOX_RECT)); 221 | cv::Size target_size(width, height); 222 | cv::Mat resized_img(target_size.height, target_size.width, CV_8UC3); 223 | // 计算缩放比例/Calculate the scaling ratio 224 | float scale_w = (float)target_size.width / img.cols; 225 | float scale_h = (float)target_size.height / img.rows; 226 | 227 | // 图像缩放/Image scaling 228 | if (img_width != width || img_height != height) 229 | { 230 | // rga 231 | rga_buffer_t src; 232 | rga_buffer_t dst; 233 | memset(&src, 0, sizeof(src)); 234 | memset(&dst, 0, sizeof(dst)); 235 | ret = resize_rga(src, dst, img, resized_img, target_size); 236 | if (ret != 0) 237 | { 238 | fprintf(stderr, "resize with rga error\n"); 239 | } 240 | /********* 241 | // opencv 242 | float min_scale = std::min(scale_w, scale_h); 243 | scale_w = min_scale; 244 | scale_h = min_scale; 245 | letterbox(img, resized_img, pads, min_scale, target_size); 246 | *********/ 247 | inputs[0].buf = resized_img.data; 248 | } 249 | else 250 | { 251 | inputs[0].buf = img.data; 252 | } 253 | 254 | rknn_inputs_set(ctx, io_num.n_input, inputs); 255 | 256 | rknn_output outputs[io_num.n_output]; 257 | memset(outputs, 0, sizeof(outputs)); 258 | for (int i = 0; i < io_num.n_output; i++) 259 | { 260 | outputs[i].want_float = 0; 261 | } 262 | 263 | // 模型推理/Model inference 264 | ret = rknn_run(ctx, NULL); 265 | ret = rknn_outputs_get(ctx, io_num.n_output, outputs, NULL); 266 | 267 | // 后处理/Post-processing 268 | detect_result_group_t detect_result_group; 269 | std::vector out_scales; 270 | std::vector out_zps; 271 | for (int i = 0; i < io_num.n_output; ++i) 272 | { 273 | out_scales.push_back(output_attrs[i].scale); 274 | out_zps.push_back(output_attrs[i].zp); 275 | } 276 | post_process((int8_t *)outputs[0].buf, (int8_t *)outputs[1].buf, (int8_t *)outputs[2].buf, height, width, 277 | box_conf_threshold, nms_threshold, pads, scale_w, scale_h, out_zps, out_scales, &detect_result_group); 278 | 279 | // 绘制框体/Draw the box 280 | char text[256]; 281 | for (int i = 0; i < detect_result_group.count; i++) 282 | { 283 | detect_result_t *det_result = &(detect_result_group.results[i]); 284 | sprintf(text, "%s %.1f%%", det_result->name, det_result->prop * 100); 285 | // 打印预测物体的信息/Prints information about the predicted object 286 | // printf("%s @ (%d %d %d %d) %f\n", det_result->name, det_result->box.left, det_result->box.top, 287 | // det_result->box.right, det_result->box.bottom, det_result->prop); 288 | int x1 = det_result->box.left; 289 | int y1 = det_result->box.top; 290 | int x2 = det_result->box.right; 291 | int y2 = det_result->box.bottom; 292 | rectangle(orig_img, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(256, 0, 0, 256), 3); 293 | putText(orig_img, text, cv::Point(x1, y1 + 12), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255)); 294 | } 295 | 296 | ret = rknn_outputs_release(ctx, io_num.n_output, outputs); 297 | 298 | return orig_img; 299 | } 300 | 301 | rkYolov5s::~rkYolov5s() 302 | { 303 | deinitPostProcess(); 304 | 305 | ret = rknn_destroy(ctx); 306 | 307 | if (model_data) 308 | free(model_data); 309 | 310 | if (input_attrs) 311 | free(input_attrs); 312 | if (output_attrs) 313 | free(output_attrs); 314 | } 315 | --------------------------------------------------------------------------------