commit from lz
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +7 -0
- duix-android/dh_aigc_android/.gitignore +25 -0
- duix-android/dh_aigc_android/README.md +268 -0
- duix-android/dh_aigc_android/android_glide_lint.xml +7 -0
- duix-android/dh_aigc_android/build.gradle +39 -0
- duix-android/dh_aigc_android/demo.jks +0 -0
- duix-android/dh_aigc_android/duix-sdk/.gitignore +1 -0
- duix-android/dh_aigc_android/duix-sdk/build.gradle +68 -0
- duix-android/dh_aigc_android/duix-sdk/consumer-rules.pro +0 -0
- duix-android/dh_aigc_android/duix-sdk/proguard-rules.pro +104 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/AndroidManifest.xml +4 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/Android.mk64 +86 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/CMakeLists.txt +169 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes.h +41 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes_cbc.c +23 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes_core.c +1127 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes_ecb.c +24 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes_locl.h +42 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aesmain.c +111 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aesmain.h +16 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/base64.c +164 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/base64.h +29 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/cbc128.c +161 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gaes_stream.cc +213 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gaes_stream.h +22 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gaesmain +0 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gj_aes.c +69 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gj_aes.h +22 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gj_dll.h +21 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/makefile +3 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/modes.h +22 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/aicommon.h +24 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/aimodel.cpp +438 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/aimodel.h +136 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/blendgram.cpp +437 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/blendgram.h +287 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/bt +19 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/face_utils.cpp +114 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/face_utils.h +20 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/jlog.h +31 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/jmat.cpp +411 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/jmat.h +92 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/malpha.cpp +229 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/malpha.h +59 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/mfcc/AudioFFT.hpp +1264 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/mfcc/iir_filter.hpp +375 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/mfcc/mfcc.hpp +441 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/mfcc/sas_util.h +123 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/munet.cpp +332 -0
- duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/munet.h +28 -0
.gitattributes
CHANGED
@@ -33,3 +33,10 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
38 |
+
*.so filter=lfs diff=lfs merge=lfs -text
|
39 |
+
*.so.* filter=lfs diff=lfs merge=lfs -text
|
40 |
+
*.a filter=lfs diff=lfs merge=lfs -text
|
41 |
+
*.a.* filter=lfs diff=lfs merge=lfs -text
|
42 |
+
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/linux/gjgpgmain filter=lfs diff=lfs merge=lfs -text
|
duix-android/dh_aigc_android/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.iml
|
2 |
+
.gradle
|
3 |
+
/local.properties
|
4 |
+
/.idea/
|
5 |
+
.DS_Store
|
6 |
+
/build/
|
7 |
+
*/build/
|
8 |
+
*/*/build/
|
9 |
+
*/debug/
|
10 |
+
*/release/
|
11 |
+
/captures
|
12 |
+
.externalNativeBuild
|
13 |
+
.cxx
|
14 |
+
*.bat
|
15 |
+
*.apk
|
16 |
+
output-metadata.json
|
17 |
+
# app用到zip的请忽略
|
18 |
+
# 自定义了local.properties的请删除这条
|
19 |
+
local.properties
|
20 |
+
~$*
|
21 |
+
gradlew
|
22 |
+
.idea
|
23 |
+
# 自定义了根目录gradle/gradle-wrapper.properties的请删除这条(不推荐自定义)
|
24 |
+
#gradle
|
25 |
+
build
|
duix-android/dh_aigc_android/README.md
ADDED
@@ -0,0 +1,268 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# DUIX-SDK
|
2 |
+
2D虚拟数字人SDK,可以通过语音完成对虚拟人实时驱动。
|
3 |
+
|
4 |
+
## 一、产品介绍
|
5 |
+
|
6 |
+
2D 数字人虚拟人SDK ,可以通过语音完成对虚拟人实时驱动。
|
7 |
+
|
8 |
+
### 1. 适用场景
|
9 |
+
|
10 |
+
部署成本低: 无需客户提供技术团队进行配合,支持低成本快速部署在多种终端及大屏;
|
11 |
+
网络依赖小:可落地在地铁、银行、政务等多种场景的虚拟助理自助服务上;
|
12 |
+
功能多样化:可根据客户需求满足视频、媒体、客服、金融、广电等多个行业的多样化需求
|
13 |
+
|
14 |
+
### 2. 核心功能
|
15 |
+
|
16 |
+
提供定制形象的 AI 主播,智能客服等多场景形象租赁,支持客户快速部署和低成本运营;
|
17 |
+
专属形象定制:支持定制专属的虚拟助理形象,可选低成本或深度形象生成;
|
18 |
+
播报内容定制:支持定制专属的播报内容,应用在培训、播报等多种场景上;
|
19 |
+
实时互动问答:支持实时对话,也可定制专属问答库,可满足咨询查询、语音闲聊、虚拟陪伴、垂类场景的客服问答等需求。
|
20 |
+
|
21 |
+
## 二、SDK集成
|
22 |
+
|
23 |
+
### 1. 支持的系统和硬件版本
|
24 |
+
|
25 |
+
| 项目 | 描述 |
|
26 |
+
|--------|------------------------------------------------------------------|
|
27 |
+
| 系统 | 支持 Android 7.0+ ( API Level 24 )到 Android 13 ( API Level 33 )系统。 |
|
28 |
+
| CPU架构 | armeabi-v7a, arm64-v8a |
|
29 |
+
| 硬件要求 | 要求设备 CPU4 核极以上,内存 4G 及以上。可用存储空间 500MB 及以上。 |
|
30 |
+
| 网络 | 支持 WIF 及移动网络。如果使用云端问答库,设备带宽(用于数字人的实际带宽)期望 10mbps 及以上。 |
|
31 |
+
| 开发 IDE | Android Studio Giraffe \mid 2022.3.1 Patch 2 |
|
32 |
+
| 内存要求 | 可用于数字人的内存 >= 400MB |
|
33 |
+
|
34 |
+
##两个人物模型(切换男女形象,修改./duix-android/dh_aigc_android/test/src/main/java/ai/guiji/duix/test/ui/activity/MainActivity.kt的modelUrl变量即可)
|
35 |
+
```
|
36 |
+
女:https://cdn.guiji.ai/duix/digital/model/1712034391673/bendi1_0329.zip
|
37 |
+
男:https://digital-public.obs.cn-east-3.myhuaweicloud.com/duix/digital/model/1706009711636/liangwei_540s.zip
|
38 |
+
```
|
39 |
+
|
40 |
+
### 2. SDK集成
|
41 |
+
|
42 |
+
引入 sdk aar 包: duix_client_sdk_release_${version}.aar
|
43 |
+
app 目录新建 libs 目录,放入 aar 包,在 build.gradle 中增加配置如下
|
44 |
+
|
45 |
+
```gradle
|
46 |
+
dependencies {
|
47 |
+
// 使用阿里ASR需要将nuisdk-release.aar放到libs目录下(必选)
|
48 |
+
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
|
49 |
+
// sdk 中使用到 exoplayer 处理音频(必选)
|
50 |
+
implementation 'com.google.android.exoplayer:exoplayer:2.14.2'
|
51 |
+
|
52 |
+
// 云端问答接口使用的SSE组件(非必选)
|
53 |
+
implementation 'com.squareup.okhttp3:okhttp-sse:4.10.0'
|
54 |
+
|
55 |
+
...
|
56 |
+
}
|
57 |
+
```
|
58 |
+
|
59 |
+
权限要求, AndroidManifest.xml中,增加如下配置
|
60 |
+
|
61 |
+
```xml
|
62 |
+
|
63 |
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
64 |
+
<uses-permission android:name="android.permission.INTERNET" />
|
65 |
+
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
|
66 |
+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
67 |
+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
68 |
+
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
|
69 |
+
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
70 |
+
|
71 |
+
</manifest>
|
72 |
+
```
|
73 |
+
|
74 |
+
## 三、SDK调用及API说明
|
75 |
+
|
76 |
+
### 1. 初始化SDK
|
77 |
+
|
78 |
+
在渲染页onCreate()阶段构建DUIX对象并添加回调事件
|
79 |
+
|
80 |
+
```kotlin
|
81 |
+
duix = DUIX(mContext, baseDir, modelDir, mDUIXRender) { event, msg, info ->
|
82 |
+
when (event) {
|
83 |
+
ai.guiji.duix.sdk.client.Constant.CALLBACK_EVENT_INIT_READY -> {
|
84 |
+
initOK()
|
85 |
+
}
|
86 |
+
|
87 |
+
ai.guiji.duix.sdk.client.Constant.CALLBACK_EVENT_INIT_ERROR -> {
|
88 |
+
|
89 |
+
}
|
90 |
+
// ...
|
91 |
+
|
92 |
+
}
|
93 |
+
}
|
94 |
+
// 异步回调结果
|
95 |
+
duix?.init()
|
96 |
+
```
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
DUIX对象构建说明:
|
101 |
+
|
102 |
+
| 参数 | 类型 | 描述 |
|
103 |
+
| -------- | ---------- | ------------------------------------------------------------ |
|
104 |
+
| context | Context | 系统上下文 |
|
105 |
+
| baseDir | String | 存放模型驱动的配置文件,需要自行管理. 可将压缩文件解压到外部存储并提供文件夹路径 |
|
106 |
+
| modelDir | String | 存放模型文件的文件夹,需要自行管理. 可将压缩文件解压到外部存储并提供文件夹路径 |
|
107 |
+
| render | RenderSink | 渲染数据接口,sdk提供了默认的渲染组件继承自该接口,也可以自己实现 |
|
108 |
+
| callback | Callback | SDK处理的各种回调事件 |
|
109 |
+
|
110 |
+
参考demo LiveActivity示例
|
111 |
+
|
112 |
+
### 2. 获取SDK模型初始化状态
|
113 |
+
|
114 |
+
```kotlin
|
115 |
+
object : Callback {
|
116 |
+
fun onEvent(event: String, msg: String, info: Object) {
|
117 |
+
when (event) {
|
118 |
+
"init.ready" -> {
|
119 |
+
// SDK模型初始化成功
|
120 |
+
}
|
121 |
+
|
122 |
+
"init.error" -> {
|
123 |
+
//初始化失败
|
124 |
+
Log.e(TAG, "init error: $msg")
|
125 |
+
}
|
126 |
+
// ...
|
127 |
+
|
128 |
+
}
|
129 |
+
}
|
130 |
+
}
|
131 |
+
```
|
132 |
+
|
133 |
+
### 3. 数字人形象展示
|
134 |
+
|
135 |
+
使用RenderSink接口接受渲染帧数据,SDK中提供了该接口实现DUIXRenderer.java。也可以自己实现该接口自定义渲染。
|
136 |
+
其中RenderSink的定义如下:
|
137 |
+
|
138 |
+
```java
|
139 |
+
/**
|
140 |
+
* 渲染管道,通过该接口返回渲染数据
|
141 |
+
*/
|
142 |
+
public interface RenderSink {
|
143 |
+
|
144 |
+
// frame中的buffer数据以bgr顺序排列
|
145 |
+
void onVideoFrame(ImageFrame imageFrame);
|
146 |
+
|
147 |
+
}
|
148 |
+
```
|
149 |
+
|
150 |
+
使用DUIXRenderer及DUIXTextureView控件简单实现渲染展示,该控件支持透明通道可以自由设置背景及前景:
|
151 |
+
|
152 |
+
```kotlin
|
153 |
+
override fun onCreate(savedInstanceState: Bundle?) {
|
154 |
+
super.onCreate(savedInstanceState)
|
155 |
+
// ...
|
156 |
+
mDUIXRender =
|
157 |
+
DUIXRenderer(
|
158 |
+
mContext,
|
159 |
+
binding.glTextureView
|
160 |
+
)
|
161 |
+
|
162 |
+
binding.glTextureView.setEGLContextClientVersion(GL_CONTEXT_VERSION)
|
163 |
+
binding.glTextureView.setEGLConfigChooser(8, 8, 8, 8, 16, 0) // 透明
|
164 |
+
binding.glTextureView.isOpaque = false // 透明
|
165 |
+
binding.glTextureView.setRenderer(mDUIXRender)
|
166 |
+
binding.glTextureView.renderMode =
|
167 |
+
GLSurfaceView.RENDERMODE_WHEN_DIRTY // 一定要在设置完Render之后再调用
|
168 |
+
|
169 |
+
duix = DUIX(mContext, duixOptions, mDUIXRender) { event, msg, _ ->
|
170 |
+
}
|
171 |
+
// ...
|
172 |
+
}
|
173 |
+
```
|
174 |
+
|
175 |
+
### 4. 启动数字人播报
|
176 |
+
|
177 |
+
在初始化成功后,可以播放音频以驱动形象
|
178 |
+
|
179 |
+
```kotlin
|
180 |
+
duix?.playAudio(wavPath)
|
181 |
+
```
|
182 |
+
|
183 |
+
参数说明:
|
184 |
+
|
185 |
+
| 参数 | 类型 | 描述 |
|
186 |
+
|---------|--------|-----------------------------|
|
187 |
+
| wavPath | String | 16k采样率单通道的wav文件地址或https网络地址 |
|
188 |
+
|
189 |
+
音频播放状态及进度回调:
|
190 |
+
|
191 |
+
```kotlin
|
192 |
+
object : Callback {
|
193 |
+
fun onEvent(event: String, msg: String, info: Object) {
|
194 |
+
when (event) {
|
195 |
+
// ...
|
196 |
+
|
197 |
+
"play.start" -> {
|
198 |
+
// 开始播放音频
|
199 |
+
}
|
200 |
+
|
201 |
+
"play.end" -> {
|
202 |
+
// 完成播放音频
|
203 |
+
}
|
204 |
+
"play.error" -> {
|
205 |
+
// 音频播放异常
|
206 |
+
}
|
207 |
+
"play.progress" -> {
|
208 |
+
// 音频播放进度
|
209 |
+
}
|
210 |
+
|
211 |
+
}
|
212 |
+
}
|
213 |
+
}
|
214 |
+
|
215 |
+
```
|
216 |
+
|
217 |
+
### 5. 终止当前播报
|
218 |
+
|
219 |
+
当数字人正在播报时调用该接口终止播报。
|
220 |
+
|
221 |
+
函数定义:
|
222 |
+
|
223 |
+
```
|
224 |
+
boolean stopAudio();
|
225 |
+
```
|
226 |
+
|
227 |
+
调用示例如下:
|
228 |
+
|
229 |
+
```kotlin
|
230 |
+
duix?.stopAudio()
|
231 |
+
```
|
232 |
+
|
233 |
+
### 6. 播放动作区间
|
234 |
+
|
235 |
+
当模型中支持播放动作区间时可使用该函数播放多做区间,多个时随机播放。
|
236 |
+
|
237 |
+
函数定义:
|
238 |
+
|
239 |
+
```
|
240 |
+
void motion();
|
241 |
+
```
|
242 |
+
|
243 |
+
调用示例如下:
|
244 |
+
|
245 |
+
```kotlin
|
246 |
+
duix?.motion()
|
247 |
+
```
|
248 |
+
|
249 |
+
###
|
250 |
+
|
251 |
+
### 四. Proguard配置
|
252 |
+
|
253 |
+
如果代码使用了混淆,请在proguard-rules.pro中配置:
|
254 |
+
|
255 |
+
```
|
256 |
+
-keep class com.btows.ncnntest.**{*; }
|
257 |
+
-dontwarn com.squareup.okhttp3.**
|
258 |
+
-keep class com.squareup.okhttp3.** { *;}
|
259 |
+
```
|
260 |
+
|
261 |
+
## 五、注意事项
|
262 |
+
|
263 |
+
1. 驱动渲染必须正确的配置基础配置文件夹和对应模型文件夹的存储路径。
|
264 |
+
2. 播放的音频文件不宜过大,过大的音频导入会导致大量消耗CPU,从而造成绘制卡顿。
|
265 |
+
|
266 |
+
|
267 |
+
|
268 |
+
## 六、版本记录
|
duix-android/dh_aigc_android/android_glide_lint.xml
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="utf-8" ?>
|
2 |
+
<!-- https://github.com/bumptech/glide/issues/4940 -->
|
3 |
+
<lint>
|
4 |
+
<issue id="NotificationPermission">
|
5 |
+
<ignore regexp="com.bumptech.glide.request.target.NotificationTarget" />
|
6 |
+
</issue>
|
7 |
+
</lint>
|
duix-android/dh_aigc_android/build.gradle
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
2 |
+
buildscript {
|
3 |
+
repositories {
|
4 |
+
|
5 |
+
maven { url 'https://maven.aliyun.com/repository/public/' }
|
6 |
+
maven { url 'https://maven.aliyun.com/repository/central' }
|
7 |
+
maven { url 'https://maven.aliyun.com/repository/google' }
|
8 |
+
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
|
9 |
+
maven { url 'https://jitpack.io' }
|
10 |
+
maven { url 'https://repo1.maven.org/maven2/' }
|
11 |
+
google()
|
12 |
+
}
|
13 |
+
dependencies {
|
14 |
+
classpath 'com.android.tools.build:gradle:8.1.2'
|
15 |
+
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.10'
|
16 |
+
}
|
17 |
+
}
|
18 |
+
|
19 |
+
allprojects {
|
20 |
+
repositories {
|
21 |
+
|
22 |
+
maven { url 'https://maven.aliyun.com/repository/public/' }
|
23 |
+
maven { url 'https://maven.aliyun.com/repository/central' }
|
24 |
+
maven { url 'https://maven.aliyun.com/repository/google' }
|
25 |
+
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
|
26 |
+
maven { url 'https://jitpack.io' }
|
27 |
+
maven { url 'https://repo1.maven.org/maven2/' }
|
28 |
+
google()
|
29 |
+
}
|
30 |
+
}
|
31 |
+
|
32 |
+
ext {
|
33 |
+
compileSdkVersion = 33
|
34 |
+
buildToolsVersion = '30.0.2'
|
35 |
+
minSdkVersion = 24
|
36 |
+
targetSdkVersion = 33
|
37 |
+
versionCode = 2
|
38 |
+
versionName = "0.0.2"
|
39 |
+
}
|
duix-android/dh_aigc_android/demo.jks
ADDED
Binary file (2.68 kB). View file
|
|
duix-android/dh_aigc_android/duix-sdk/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
/build
|
duix-android/dh_aigc_android/duix-sdk/build.gradle
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
plugins {
|
2 |
+
id 'com.android.library'
|
3 |
+
}
|
4 |
+
|
5 |
+
android {
|
6 |
+
namespace 'ai.guiji.duix.sdk.client'
|
7 |
+
compileSdk 33
|
8 |
+
|
9 |
+
defaultConfig {
|
10 |
+
minSdk 24
|
11 |
+
versionCode 4
|
12 |
+
versionName '3.0.3'
|
13 |
+
|
14 |
+
externalNativeBuild {
|
15 |
+
cmake {
|
16 |
+
abiFilters 'arm64-v8a', "armeabi-v7a"
|
17 |
+
cppFlags "-std=c++17", "-fexceptions"
|
18 |
+
//arguments "-DANDROID_STL=c++_shared","-DANDROID_TOOLCHAIN=clang"
|
19 |
+
}
|
20 |
+
}
|
21 |
+
}
|
22 |
+
|
23 |
+
buildTypes {
|
24 |
+
debug {
|
25 |
+
minifyEnabled false
|
26 |
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
27 |
+
|
28 |
+
buildConfigField("String", "VERSION_NAME", "\"${defaultConfig.versionName}\"")
|
29 |
+
buildConfigField('int', 'VERSION_CODE', "${defaultConfig.versionCode}")
|
30 |
+
}
|
31 |
+
|
32 |
+
release {
|
33 |
+
minifyEnabled false
|
34 |
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
35 |
+
|
36 |
+
buildConfigField("String", "VERSION_NAME", "\"${defaultConfig.versionName}\"")
|
37 |
+
buildConfigField('int', 'VERSION_CODE', "${defaultConfig.versionCode}")
|
38 |
+
|
39 |
+
android.libraryVariants.all { variant ->
|
40 |
+
variant.outputs.all {
|
41 |
+
outputFileName = "duix_client_sdk_${buildType.name}_${defaultConfig.versionName}.aar"
|
42 |
+
}
|
43 |
+
}
|
44 |
+
}
|
45 |
+
}
|
46 |
+
|
47 |
+
externalNativeBuild {
|
48 |
+
cmake {
|
49 |
+
path "src/main/cpp/CMakeLists.txt"
|
50 |
+
version "3.10.2"
|
51 |
+
}
|
52 |
+
}
|
53 |
+
|
54 |
+
compileOptions {
|
55 |
+
sourceCompatibility JavaVersion.VERSION_1_8
|
56 |
+
targetCompatibility JavaVersion.VERSION_1_8
|
57 |
+
}
|
58 |
+
// kotlinOptions {
|
59 |
+
// jvmTarget = '1.8'
|
60 |
+
// }
|
61 |
+
}
|
62 |
+
|
63 |
+
dependencies {
|
64 |
+
|
65 |
+
implementation 'com.google.android.exoplayer:exoplayer:2.14.2'
|
66 |
+
implementation "org.java-websocket:Java-WebSocket:1.5.1"
|
67 |
+
implementation 'com.squareup.okhttp3:okhttp-sse:4.10.0'
|
68 |
+
}
|
duix-android/dh_aigc_android/duix-sdk/consumer-rules.pro
ADDED
File without changes
|
duix-android/dh_aigc_android/duix-sdk/proguard-rules.pro
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Add project specific ProGuard rules here.
|
2 |
+
# You can control the set of applied configuration files using the
|
3 |
+
# proguardFiles setting in build.gradle.
|
4 |
+
#
|
5 |
+
# For more details, see
|
6 |
+
# http://developer.android.com/guide/developing/tools/proguard.html
|
7 |
+
|
8 |
+
# If your project uses WebView with JS, uncomment the following
|
9 |
+
# and specify the fully qualified class name to the JavaScript interface
|
10 |
+
# class:
|
11 |
+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
12 |
+
# public *;
|
13 |
+
#}
|
14 |
+
|
15 |
+
# Uncomment this to preserve the line number information for
|
16 |
+
# debugging stack traces.
|
17 |
+
#-keepattributes SourceFile,LineNumberTable
|
18 |
+
|
19 |
+
# If you keep the line number information, uncomment this to
|
20 |
+
# hide the original source file name.
|
21 |
+
#-renamesourcefileattribute SourceFile
|
22 |
+
|
23 |
+
-optimizationpasses 5 #指定代码的压缩级别 0 - 7,一般都是5,无需改变
|
24 |
+
-dontusemixedcaseclassnames #不使用大小写混合
|
25 |
+
#告诉Proguard 不要跳过对非公开类的处理,默认是跳过
|
26 |
+
-dontskipnonpubliclibraryclasses #如果应用程序引入的有jar包,并且混淆jar包里面的class
|
27 |
+
-verbose #混淆时记录日志(混淆后生产映射文件 map 类名 -> 转化后类名的映射
|
28 |
+
#指定混淆时的算法,后面的参数是一个过滤器
|
29 |
+
#这个过滤器是谷歌推荐的算法,一般也不会改变
|
30 |
+
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
|
31 |
+
#类型转换错误 添加如下代码以便过滤泛型(不写可能会出现类型转换错误,一般情况把这个加上就是了),即避免泛型被混淆
|
32 |
+
-keepattributes Signature
|
33 |
+
#假如项目中有用到注解,应加入这行配置,对JSON实体映射也很重要,eg:fastjson
|
34 |
+
-keepattributes *Annotation*
|
35 |
+
#抛出异常时保留代码行数
|
36 |
+
-keepattributes SourceFile,LineNumberTable
|
37 |
+
#保持 native 的方法不去混淆
|
38 |
+
-keepclasseswithmembernames class * {
|
39 |
+
native <methods>;
|
40 |
+
}
|
41 |
+
|
42 |
+
#保持指定规则的方法不被混淆(Android layout 布局文件中为控件配置的onClick方法不能混淆)
|
43 |
+
-keepclassmembers class * extends android.app.Activity {
|
44 |
+
public void *(android.view.View);
|
45 |
+
}
|
46 |
+
#保持自定义控件指定规则的方法不被混淆
|
47 |
+
-keep public class * extends android.view.View {
|
48 |
+
public <init>(android.content.Context);
|
49 |
+
public <init>(android.content.Context, android.util.AttributeSet);
|
50 |
+
public <init>(android.content.Context, android.util.AttributeSet, int);
|
51 |
+
public void set*(...);
|
52 |
+
}
|
53 |
+
#保持枚举 enum 不被混淆
|
54 |
+
-keepclassmembers enum * {
|
55 |
+
public static **[] values();
|
56 |
+
public static ** valueOf(java.lang.String);
|
57 |
+
}
|
58 |
+
#保持 Parcelable 不被混淆(aidl文件不能去混淆)
|
59 |
+
-keep class * implements android.os.Parcelable {
|
60 |
+
public static final android.os.Parcelable$Creator *;
|
61 |
+
}
|
62 |
+
#需要序列化和反序列化的类不能被混淆(注:Java反射用到的类也不能被混淆)
|
63 |
+
-keepnames class * implements java.io.Serializable
|
64 |
+
#保护实现接口Serializable的类中,指定规则的类成员不被混淆
|
65 |
+
-keepclassmembers class * implements java.io.Serializable {
|
66 |
+
static final long serialVersionUID;
|
67 |
+
private static final java.io.ObjectStreamField[] serialPersistentFields;
|
68 |
+
!static !transient <fields>;
|
69 |
+
private void writeObject(java.io.ObjectOutputStream);
|
70 |
+
private void readObject(java.io.ObjectInputStream);
|
71 |
+
java.lang.Object writeReplace();
|
72 |
+
java.lang.Object readResolve();
|
73 |
+
}
|
74 |
+
#保持R文件不被混淆,否则,你的反射是获取不到资源id的
|
75 |
+
-keep class **.R$* { *; }
|
76 |
+
|
77 |
+
-keepclassmembers class * {
|
78 |
+
public <init> (org.json.JSONObject);
|
79 |
+
}
|
80 |
+
|
81 |
+
-keepclassmembers enum * {
|
82 |
+
public static **[] values();
|
83 |
+
public static ** valueOf(java.lang.String);
|
84 |
+
}
|
85 |
+
|
86 |
+
|
87 |
+
#以下针对App本身设置
|
88 |
+
|
89 |
+
|
90 |
+
-keep class com.btows.ncnntest.**{*; }
|
91 |
+
|
92 |
+
-keep class ai.guiji.duix.sdk.client.render.** {*;}
|
93 |
+
-keep class ai.guiji.duix.sdk.client.render.**$* {*;}
|
94 |
+
-keep class ai.guiji.duix.sdk.client.bean.** {*;}
|
95 |
+
-keep class ai.guiji.duix.sdk.client.DUIX{*; }
|
96 |
+
-keep class ai.guiji.duix.sdk.client.DUIX$* {*;}
|
97 |
+
-keep class ai.guiji.duix.sdk.client.Constant{*; }
|
98 |
+
-keep class ai.guiji.duix.sdk.client.Constant* {*;}
|
99 |
+
-keep class ai.guiji.duix.sdk.client.DUIXOptions{*; }
|
100 |
+
-keep class ai.guiji.duix.sdk.client.DUIXOptions* {*;}
|
101 |
+
-keep class ai.guiji.duix.sdk.client.Callback{*; }
|
102 |
+
-keep class ai.guiji.duix.sdk.client.Callback* {*;}
|
103 |
+
-keep class ai.guiji.duix.sdk.client.render.DUIXTextureView{*; }
|
104 |
+
-keep class ai.guiji.duix.sdk.client.render.DUIXTextureView$* {*;}
|
duix-android/dh_aigc_android/duix-sdk/src/main/AndroidManifest.xml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="utf-8"?>
|
2 |
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
3 |
+
<uses-permission android:name="android.permission.INTERNET" />
|
4 |
+
</manifest>
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/Android.mk64
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#/****************************************************************************
|
2 |
+
#* Cartoonifier, for Android.
|
3 |
+
#*****************************************************************************
|
4 |
+
#* by Shervin Emami, 5th Dec 2012 ([email protected])
|
5 |
+
#* http://www.shervinemami.info/
|
6 |
+
#*****************************************************************************
|
7 |
+
#* Ch1 of the book "Mastering OpenCV with Practical Computer Vision Projects"
|
8 |
+
#* Copyright Packt Publishing 2012.
|
9 |
+
#* http://www.packtpub.com/cool-projects-with-opencv/book
|
10 |
+
#****************************************************************************/
|
11 |
+
|
12 |
+
|
13 |
+
LOCAL_PATH := $(call my-dir)
|
14 |
+
|
15 |
+
|
16 |
+
include $(CLEAR_VARS)
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
LOCAL_SRC_FILES += \
|
21 |
+
base/coffeecatch.c \
|
22 |
+
android/DigitJni.cpp \
|
23 |
+
android/MsgcbJni.cpp \
|
24 |
+
android/JniHelper.cpp \
|
25 |
+
aisdk/jmat.cpp \
|
26 |
+
android/kmatarm.cpp \
|
27 |
+
aisdk/wavreader.cpp \
|
28 |
+
aisdk/wenet.cpp \
|
29 |
+
aisdk/aimodel.cpp \
|
30 |
+
aisdk/scrfd.cpp \
|
31 |
+
aisdk/pfpld.cpp \
|
32 |
+
aisdk/munet.cpp \
|
33 |
+
aisdk/blendgram.cpp \
|
34 |
+
aisdk/face_utils.cpp \
|
35 |
+
digit/netwav.cpp \
|
36 |
+
digit/looper.cpp \
|
37 |
+
digit/netcurl.cpp \
|
38 |
+
digit/GRender.cpp \
|
39 |
+
digit/GDigit.cpp \
|
40 |
+
digit/dispatchqueue.cpp \
|
41 |
+
render/EglRenderer.cpp \
|
42 |
+
render/RgbVideoRenderer.cpp \
|
43 |
+
render/SurfaceVideoRenderer.cpp \
|
44 |
+
render/RenderHelper.cpp \
|
45 |
+
render/AudioTrack.cpp \
|
46 |
+
render/AudioRenderer.cpp \
|
47 |
+
render/GlesProgram.cpp \
|
48 |
+
base/Log.cpp \
|
49 |
+
base/FrameSource.cpp \
|
50 |
+
base/MediaData.cpp \
|
51 |
+
base/MessageSource.cpp \
|
52 |
+
base/MessageHelper.cpp \
|
53 |
+
base/LoopThread.cpp \
|
54 |
+
base/XThread.cpp \
|
55 |
+
base/XTick.c \
|
56 |
+
base/cJSON.c \
|
57 |
+
base/dh_mem.c \
|
58 |
+
digit/grtcfg.c \
|
59 |
+
base/LoopThreadHelper.cpp \
|
60 |
+
)
|
61 |
+
|
62 |
+
LOCAL_ARM_NEON := true
|
63 |
+
LOCAL_MODULE := facedetect
|
64 |
+
LOCAL_LDLIBS += -llog -ldl -lm -lmediandk
|
65 |
+
LOCAL_LDLIBS += -lEGL -lGLESv2 -landroid
|
66 |
+
LOCAL_LDLIBS += -ljnigraphics -fopenmp
|
67 |
+
|
68 |
+
LOCAL_CFLAGS += -fpermissive
|
69 |
+
LOCAL_CPPFLAGS += -fpermissive
|
70 |
+
#LOCAL_CFLAGS += -ftree-vectorizer-verbose=2
|
71 |
+
LOCAL_CPPFLAGS += -std=c++17
|
72 |
+
LOCAL_LDLIBS += -lstdc++
|
73 |
+
|
74 |
+
LOCAL_C_INCLUDES += $(LOCAL_PATH)
|
75 |
+
LOCAL_C_INCLUDES += include
|
76 |
+
LOCAL_C_INCLUDES += base
|
77 |
+
LOCAL_C_INCLUDES += aisdk
|
78 |
+
LOCAL_C_INCLUDES += digit
|
79 |
+
LOCAL_C_INCLUDES += render
|
80 |
+
LOCAL_C_INCLUDES += android
|
81 |
+
LOCAL_C_INCLUDES += third/arm/include
|
82 |
+
LOCAL_C_INCLUDES += third/arm/include/ncnn
|
83 |
+
LOCAL_C_INCLUDES += third/opencv-mobile-4.6.0-android/sdk/native/jni/include/
|
84 |
+
LOCAL_C_INCLUDES += third/ncnn-20221128-android-vulkan-shared/arm64-v8a/include/ncnn
|
85 |
+
|
86 |
+
include $(BUILD_SHARED_LIBRARY)
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/CMakeLists.txt
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
project(scrfdncnn)
|
2 |
+
|
3 |
+
cmake_minimum_required(VERSION 3.10.2)
|
4 |
+
|
5 |
+
set(CMAKE_CXX_STANDARD 17)
|
6 |
+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -funwind-tables")
|
7 |
+
|
8 |
+
set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/third/opencv-mobile-4.6.0-android/sdk/native/jni)
|
9 |
+
find_package(OpenCV REQUIRED core imgproc highgui)
|
10 |
+
|
11 |
+
#set(ncnn_DIR ${CMAKE_SOURCE_DIR}/third/ncnn-20221128-android-vulkan-shared/${ANDROID_ABI}/lib/cmake/ncnn)
|
12 |
+
set(ncnn_DIR ${CMAKE_SOURCE_DIR}/third/ncnn-20231027-android-shared/${ANDROID_ABI}/lib/cmake/ncnn)
|
13 |
+
find_package(ncnn REQUIRED)
|
14 |
+
|
15 |
+
|
16 |
+
option(USE_OPENCV "shared library support" TRUE)
|
17 |
+
option(USE_NCNN "shared library support" TRUE)
|
18 |
+
|
19 |
+
include_directories(
|
20 |
+
include
|
21 |
+
base
|
22 |
+
render
|
23 |
+
aisdk
|
24 |
+
aes
|
25 |
+
digit
|
26 |
+
android
|
27 |
+
third/arm/include
|
28 |
+
third/arm/include/turbojpeg
|
29 |
+
)
|
30 |
+
|
31 |
+
add_library(scrfdncnn SHARED
|
32 |
+
android/DigitJni.cpp
|
33 |
+
android/MsgcbJni.cpp
|
34 |
+
android/JniHelper.cpp
|
35 |
+
aisdk/jmat.cpp
|
36 |
+
aisdk/wavreader.cpp
|
37 |
+
aisdk/wenet.cpp
|
38 |
+
aisdk/aimodel.cpp
|
39 |
+
aisdk/scrfd.cpp
|
40 |
+
aisdk/pfpld.cpp
|
41 |
+
aisdk/munet.cpp
|
42 |
+
aisdk/malpha.cpp
|
43 |
+
aisdk/wavcache.cpp
|
44 |
+
aisdk/blendgram.cpp
|
45 |
+
aisdk/face_utils.cpp
|
46 |
+
aisdk/netwav.cpp
|
47 |
+
digit/looper.cpp
|
48 |
+
digit/netcurl.cpp
|
49 |
+
digit/GRender.cpp
|
50 |
+
digit/GDigit.cpp
|
51 |
+
digit/dispatchqueue.cpp
|
52 |
+
render/EglRenderer.cpp
|
53 |
+
render/RgbVideoRenderer.cpp
|
54 |
+
render/SurfaceVideoRenderer.cpp
|
55 |
+
render/RenderHelper.cpp
|
56 |
+
base/BaseRenderHelper.cpp
|
57 |
+
base/AudioTrack.cpp
|
58 |
+
render/AudioRenderer.cpp
|
59 |
+
render/GlesProgram.cpp
|
60 |
+
base/Log.cpp
|
61 |
+
base/FrameSource.cpp
|
62 |
+
base/MediaData.cpp
|
63 |
+
base/MessageSource.cpp
|
64 |
+
base/MessageHelper.cpp
|
65 |
+
base/LoopThread.cpp
|
66 |
+
base/XThread.cpp
|
67 |
+
base/XTick.c
|
68 |
+
base/cJSON.c
|
69 |
+
base/dh_mem.c
|
70 |
+
digit/grtcfg.c
|
71 |
+
base/LoopThreadHelper.cpp
|
72 |
+
base/coffeecatch.c
|
73 |
+
aes/aes_cbc.c aes/aes_core.c aes/aes_ecb.c aes/base64.c aes/cbc128.c aes/gj_aes.c
|
74 |
+
aes/aesmain.c
|
75 |
+
)
|
76 |
+
|
77 |
+
|
78 |
+
add_library(turbojpeg STATIC IMPORTED)
|
79 |
+
set_target_properties(turbojpeg
|
80 |
+
PROPERTIES IMPORTED_LOCATION
|
81 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/libturbojpeg.a)
|
82 |
+
|
83 |
+
add_library(libjpeg STATIC IMPORTED)
|
84 |
+
set_target_properties(libjpeg
|
85 |
+
PROPERTIES IMPORTED_LOCATION
|
86 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/libjpeg.a)
|
87 |
+
|
88 |
+
#add_library(pplcommon STATIC IMPORTED)
|
89 |
+
#set_target_properties(pplcommon
|
90 |
+
# PROPERTIES IMPORTED_LOCATION
|
91 |
+
# ${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/libpplcommon_static.a)
|
92 |
+
|
93 |
+
#add_library(pplcv STATIC IMPORTED)
|
94 |
+
#set_target_properties(pplcv
|
95 |
+
# PROPERTIES IMPORTED_LOCATION
|
96 |
+
# ${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/libpplcv_static.a)
|
97 |
+
|
98 |
+
add_library(curl STATIC IMPORTED)
|
99 |
+
set_target_properties(curl
|
100 |
+
PROPERTIES IMPORTED_LOCATION
|
101 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/libcurl.a)
|
102 |
+
|
103 |
+
add_library(ssl STATIC IMPORTED)
|
104 |
+
set_target_properties(ssl
|
105 |
+
PROPERTIES IMPORTED_LOCATION
|
106 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/libssl.a)
|
107 |
+
|
108 |
+
add_library(crypto STATIC IMPORTED)
|
109 |
+
set_target_properties(crypto
|
110 |
+
PROPERTIES IMPORTED_LOCATION
|
111 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/libcrypto.a)
|
112 |
+
|
113 |
+
add_library(avcodec STATIC IMPORTED)
|
114 |
+
set_target_properties(avcodec
|
115 |
+
PROPERTIES IMPORTED_LOCATION
|
116 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/ffmpeg-lite/libavcodec.a)
|
117 |
+
|
118 |
+
add_library(avformat STATIC IMPORTED)
|
119 |
+
set_target_properties(avformat
|
120 |
+
PROPERTIES IMPORTED_LOCATION
|
121 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/ffmpeg-lite/libavformat.a)
|
122 |
+
|
123 |
+
add_library(avutil STATIC IMPORTED)
|
124 |
+
set_target_properties(avutil
|
125 |
+
PROPERTIES IMPORTED_LOCATION
|
126 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/ffmpeg-lite/libavutil.a)
|
127 |
+
|
128 |
+
add_library(swresample STATIC IMPORTED)
|
129 |
+
set_target_properties(swresample
|
130 |
+
PROPERTIES IMPORTED_LOCATION
|
131 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/ffmpeg-lite/libswresample.a)
|
132 |
+
|
133 |
+
add_library(swscale STATIC IMPORTED)
|
134 |
+
set_target_properties(swscale
|
135 |
+
PROPERTIES IMPORTED_LOCATION
|
136 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/ffmpeg-lite/libswscale.a)
|
137 |
+
|
138 |
+
find_library(log-lib log)
|
139 |
+
|
140 |
+
add_library(onnx-lib SHARED IMPORTED)
|
141 |
+
set_target_properties(
|
142 |
+
onnx-lib
|
143 |
+
PROPERTIES IMPORTED_LOCATION
|
144 |
+
${CMAKE_SOURCE_DIR}/third/arm/${ANDROID_ABI}/libonnxruntime.so)
|
145 |
+
|
146 |
+
target_link_libraries(scrfdncnn
|
147 |
+
ncnn
|
148 |
+
${OpenCV_LIBS}
|
149 |
+
${log-lib}
|
150 |
+
onnx-lib
|
151 |
+
camera2ndk
|
152 |
+
mediandk
|
153 |
+
libjpeg
|
154 |
+
turbojpeg
|
155 |
+
avformat
|
156 |
+
avcodec
|
157 |
+
avutil
|
158 |
+
swresample
|
159 |
+
swscale
|
160 |
+
curl
|
161 |
+
ssl
|
162 |
+
crypto
|
163 |
+
-landroid
|
164 |
+
-lmediandk
|
165 |
+
-lEGL
|
166 |
+
-lGLESv2
|
167 |
+
-lm -lz
|
168 |
+
)
|
169 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes.h
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
#ifndef HEADER_AES_H
|
4 |
+
# define HEADER_AES_H
|
5 |
+
|
6 |
+
# include <stddef.h>
|
7 |
+
|
8 |
+
# define AES_ENCRYPT 1
|
9 |
+
# define AES_DECRYPT 0
|
10 |
+
|
11 |
+
# define AES_MAXNR 14
|
12 |
+
# define AES_BLOCK_SIZE 16
|
13 |
+
|
14 |
+
struct aes_key_st {
|
15 |
+
# ifdef AES_LONG
|
16 |
+
unsigned long rd_key[4 * (AES_MAXNR + 1)];
|
17 |
+
# else
|
18 |
+
unsigned int rd_key[4 * (AES_MAXNR + 1)];
|
19 |
+
# endif
|
20 |
+
int rounds;
|
21 |
+
};
|
22 |
+
|
23 |
+
typedef struct aes_key_st AES_KEY;
|
24 |
+
|
25 |
+
|
26 |
+
int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key);
|
27 |
+
int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key);
|
28 |
+
|
29 |
+
void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key);
|
30 |
+
void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key);
|
31 |
+
|
32 |
+
void AES_ecb_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key,
|
33 |
+
const int enc);
|
34 |
+
|
35 |
+
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
36 |
+
size_t length, const AES_KEY *key,
|
37 |
+
unsigned char *ivec, const int enc);
|
38 |
+
|
39 |
+
#endif
|
40 |
+
|
41 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes_cbc.c
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
|
3 |
+
*
|
4 |
+
* Licensed under the OpenSSL license (the "License"). You may not use
|
5 |
+
* this file except in compliance with the License. You can obtain a copy
|
6 |
+
* in the file LICENSE in the source distribution or at
|
7 |
+
* https://www.openssl.org/source/license.html
|
8 |
+
*/
|
9 |
+
|
10 |
+
#include "aes.h"
|
11 |
+
#include "modes.h"
|
12 |
+
|
13 |
+
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
14 |
+
size_t len, const AES_KEY *key,
|
15 |
+
unsigned char *ivec, const int enc)
|
16 |
+
{
|
17 |
+
|
18 |
+
if (enc)
|
19 |
+
CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
|
20 |
+
(block128_f) AES_encrypt);
|
21 |
+
else
|
22 |
+
CRYPTO_cbc128_decrypt(in, out, len, key, ivec, (block128_f) AES_decrypt);
|
23 |
+
}
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes_core.c
ADDED
@@ -0,0 +1,1127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <assert.h>
|
2 |
+
|
3 |
+
#include "aes.h"
|
4 |
+
#include "aes_locl.h"
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
/*-
|
9 |
+
Te0[x] = S [x].[02, 01, 01, 03];
|
10 |
+
Te1[x] = S [x].[03, 02, 01, 01];
|
11 |
+
Te2[x] = S [x].[01, 03, 02, 01];
|
12 |
+
Te3[x] = S [x].[01, 01, 03, 02];
|
13 |
+
|
14 |
+
Td0[x] = Si[x].[0e, 09, 0d, 0b];
|
15 |
+
Td1[x] = Si[x].[0b, 0e, 09, 0d];
|
16 |
+
Td2[x] = Si[x].[0d, 0b, 0e, 09];
|
17 |
+
Td3[x] = Si[x].[09, 0d, 0b, 0e];
|
18 |
+
Td4[x] = Si[x].[01];
|
19 |
+
*/
|
20 |
+
|
21 |
+
static const u32 Te0[256] = {
|
22 |
+
0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
|
23 |
+
0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
|
24 |
+
0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
|
25 |
+
0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
|
26 |
+
0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
|
27 |
+
0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
|
28 |
+
0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
|
29 |
+
0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
|
30 |
+
0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
|
31 |
+
0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
|
32 |
+
0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
|
33 |
+
0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
|
34 |
+
0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
|
35 |
+
0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
|
36 |
+
0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
|
37 |
+
0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
|
38 |
+
0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
|
39 |
+
0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
|
40 |
+
0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
|
41 |
+
0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
|
42 |
+
0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
|
43 |
+
0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
|
44 |
+
0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
|
45 |
+
0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
|
46 |
+
0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
|
47 |
+
0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
|
48 |
+
0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
|
49 |
+
0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
|
50 |
+
0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
|
51 |
+
0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
|
52 |
+
0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
|
53 |
+
0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
|
54 |
+
0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
|
55 |
+
0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
|
56 |
+
0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
|
57 |
+
0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
|
58 |
+
0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
|
59 |
+
0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
|
60 |
+
0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
|
61 |
+
0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
|
62 |
+
0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
|
63 |
+
0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
|
64 |
+
0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
|
65 |
+
0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
|
66 |
+
0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
|
67 |
+
0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
|
68 |
+
0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
|
69 |
+
0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
|
70 |
+
0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
|
71 |
+
0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
|
72 |
+
0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
|
73 |
+
0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
|
74 |
+
0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
|
75 |
+
0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
|
76 |
+
0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
|
77 |
+
0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
|
78 |
+
0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
|
79 |
+
0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
|
80 |
+
0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
|
81 |
+
0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
|
82 |
+
0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
|
83 |
+
0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
|
84 |
+
0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
|
85 |
+
0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
|
86 |
+
};
|
87 |
+
static const u32 Te1[256] = {
|
88 |
+
0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU,
|
89 |
+
0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U,
|
90 |
+
0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU,
|
91 |
+
0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U,
|
92 |
+
0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU,
|
93 |
+
0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U,
|
94 |
+
0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU,
|
95 |
+
0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U,
|
96 |
+
0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U,
|
97 |
+
0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU,
|
98 |
+
0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U,
|
99 |
+
0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U,
|
100 |
+
0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U,
|
101 |
+
0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU,
|
102 |
+
0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U,
|
103 |
+
0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U,
|
104 |
+
0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU,
|
105 |
+
0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U,
|
106 |
+
0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U,
|
107 |
+
0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U,
|
108 |
+
0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU,
|
109 |
+
0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU,
|
110 |
+
0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U,
|
111 |
+
0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU,
|
112 |
+
0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU,
|
113 |
+
0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U,
|
114 |
+
0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU,
|
115 |
+
0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U,
|
116 |
+
0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU,
|
117 |
+
0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U,
|
118 |
+
0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U,
|
119 |
+
0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U,
|
120 |
+
0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU,
|
121 |
+
0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U,
|
122 |
+
0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU,
|
123 |
+
0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U,
|
124 |
+
0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU,
|
125 |
+
0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U,
|
126 |
+
0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U,
|
127 |
+
0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU,
|
128 |
+
0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU,
|
129 |
+
0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU,
|
130 |
+
0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U,
|
131 |
+
0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U,
|
132 |
+
0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU,
|
133 |
+
0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U,
|
134 |
+
0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU,
|
135 |
+
0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U,
|
136 |
+
0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU,
|
137 |
+
0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U,
|
138 |
+
0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU,
|
139 |
+
0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU,
|
140 |
+
0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U,
|
141 |
+
0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU,
|
142 |
+
0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U,
|
143 |
+
0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU,
|
144 |
+
0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U,
|
145 |
+
0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U,
|
146 |
+
0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U,
|
147 |
+
0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU,
|
148 |
+
0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU,
|
149 |
+
0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U,
|
150 |
+
0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU,
|
151 |
+
0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U,
|
152 |
+
};
|
153 |
+
static const u32 Te2[256] = {
|
154 |
+
0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU,
|
155 |
+
0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U,
|
156 |
+
0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU,
|
157 |
+
0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U,
|
158 |
+
0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU,
|
159 |
+
0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U,
|
160 |
+
0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU,
|
161 |
+
0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U,
|
162 |
+
0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U,
|
163 |
+
0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU,
|
164 |
+
0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U,
|
165 |
+
0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U,
|
166 |
+
0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U,
|
167 |
+
0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU,
|
168 |
+
0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U,
|
169 |
+
0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U,
|
170 |
+
0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU,
|
171 |
+
0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U,
|
172 |
+
0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U,
|
173 |
+
0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U,
|
174 |
+
0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU,
|
175 |
+
0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU,
|
176 |
+
0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U,
|
177 |
+
0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU,
|
178 |
+
0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU,
|
179 |
+
0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U,
|
180 |
+
0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU,
|
181 |
+
0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U,
|
182 |
+
0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU,
|
183 |
+
0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U,
|
184 |
+
0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U,
|
185 |
+
0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U,
|
186 |
+
0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU,
|
187 |
+
0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U,
|
188 |
+
0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU,
|
189 |
+
0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U,
|
190 |
+
0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU,
|
191 |
+
0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U,
|
192 |
+
0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U,
|
193 |
+
0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU,
|
194 |
+
0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU,
|
195 |
+
0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU,
|
196 |
+
0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U,
|
197 |
+
0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U,
|
198 |
+
0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU,
|
199 |
+
0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U,
|
200 |
+
0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU,
|
201 |
+
0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U,
|
202 |
+
0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU,
|
203 |
+
0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U,
|
204 |
+
0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU,
|
205 |
+
0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU,
|
206 |
+
0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U,
|
207 |
+
0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU,
|
208 |
+
0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U,
|
209 |
+
0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU,
|
210 |
+
0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U,
|
211 |
+
0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U,
|
212 |
+
0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U,
|
213 |
+
0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU,
|
214 |
+
0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU,
|
215 |
+
0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U,
|
216 |
+
0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU,
|
217 |
+
0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
|
218 |
+
};
|
219 |
+
static const u32 Te3[256] = {
|
220 |
+
0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
|
221 |
+
0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
|
222 |
+
0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
|
223 |
+
0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU,
|
224 |
+
0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU,
|
225 |
+
0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU,
|
226 |
+
0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U,
|
227 |
+
0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU,
|
228 |
+
0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU,
|
229 |
+
0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U,
|
230 |
+
0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U,
|
231 |
+
0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU,
|
232 |
+
0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU,
|
233 |
+
0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU,
|
234 |
+
0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU,
|
235 |
+
0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU,
|
236 |
+
0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U,
|
237 |
+
0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU,
|
238 |
+
0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU,
|
239 |
+
0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U,
|
240 |
+
0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U,
|
241 |
+
0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U,
|
242 |
+
0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U,
|
243 |
+
0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U,
|
244 |
+
0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU,
|
245 |
+
0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U,
|
246 |
+
0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU,
|
247 |
+
0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU,
|
248 |
+
0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U,
|
249 |
+
0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U,
|
250 |
+
0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U,
|
251 |
+
0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU,
|
252 |
+
0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U,
|
253 |
+
0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU,
|
254 |
+
0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU,
|
255 |
+
0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U,
|
256 |
+
0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U,
|
257 |
+
0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU,
|
258 |
+
0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U,
|
259 |
+
0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU,
|
260 |
+
0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U,
|
261 |
+
0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U,
|
262 |
+
0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U,
|
263 |
+
0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U,
|
264 |
+
0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU,
|
265 |
+
0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U,
|
266 |
+
0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU,
|
267 |
+
0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U,
|
268 |
+
0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU,
|
269 |
+
0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U,
|
270 |
+
0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU,
|
271 |
+
0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU,
|
272 |
+
0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU,
|
273 |
+
0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU,
|
274 |
+
0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U,
|
275 |
+
0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U,
|
276 |
+
0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U,
|
277 |
+
0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U,
|
278 |
+
0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U,
|
279 |
+
0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U,
|
280 |
+
0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU,
|
281 |
+
0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U,
|
282 |
+
0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU,
|
283 |
+
0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU,
|
284 |
+
};
|
285 |
+
|
286 |
+
static const u32 Td0[256] = {
|
287 |
+
0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
|
288 |
+
0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
|
289 |
+
0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
|
290 |
+
0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU,
|
291 |
+
0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U,
|
292 |
+
0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U,
|
293 |
+
0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU,
|
294 |
+
0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U,
|
295 |
+
0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU,
|
296 |
+
0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U,
|
297 |
+
0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U,
|
298 |
+
0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U,
|
299 |
+
0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U,
|
300 |
+
0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU,
|
301 |
+
0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U,
|
302 |
+
0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU,
|
303 |
+
0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U,
|
304 |
+
0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU,
|
305 |
+
0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U,
|
306 |
+
0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U,
|
307 |
+
0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U,
|
308 |
+
0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU,
|
309 |
+
0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U,
|
310 |
+
0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU,
|
311 |
+
0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U,
|
312 |
+
0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU,
|
313 |
+
0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U,
|
314 |
+
0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU,
|
315 |
+
0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU,
|
316 |
+
0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U,
|
317 |
+
0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU,
|
318 |
+
0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U,
|
319 |
+
0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU,
|
320 |
+
0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U,
|
321 |
+
0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U,
|
322 |
+
0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U,
|
323 |
+
0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU,
|
324 |
+
0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U,
|
325 |
+
0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U,
|
326 |
+
0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU,
|
327 |
+
0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U,
|
328 |
+
0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U,
|
329 |
+
0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U,
|
330 |
+
0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U,
|
331 |
+
0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U,
|
332 |
+
0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU,
|
333 |
+
0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U,
|
334 |
+
0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U,
|
335 |
+
0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U,
|
336 |
+
0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U,
|
337 |
+
0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U,
|
338 |
+
0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU,
|
339 |
+
0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU,
|
340 |
+
0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU,
|
341 |
+
0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU,
|
342 |
+
0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U,
|
343 |
+
0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U,
|
344 |
+
0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU,
|
345 |
+
0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU,
|
346 |
+
0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U,
|
347 |
+
0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU,
|
348 |
+
0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U,
|
349 |
+
0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
|
350 |
+
0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
|
351 |
+
};
|
352 |
+
static const u32 Td1[256] = {
|
353 |
+
0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU,
|
354 |
+
0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U,
|
355 |
+
0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU,
|
356 |
+
0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U,
|
357 |
+
0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U,
|
358 |
+
0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U,
|
359 |
+
0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U,
|
360 |
+
0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U,
|
361 |
+
0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U,
|
362 |
+
0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU,
|
363 |
+
0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU,
|
364 |
+
0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU,
|
365 |
+
0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U,
|
366 |
+
0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU,
|
367 |
+
0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U,
|
368 |
+
0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U,
|
369 |
+
0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U,
|
370 |
+
0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU,
|
371 |
+
0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU,
|
372 |
+
0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U,
|
373 |
+
0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU,
|
374 |
+
0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U,
|
375 |
+
0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU,
|
376 |
+
0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU,
|
377 |
+
0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U,
|
378 |
+
0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U,
|
379 |
+
0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U,
|
380 |
+
0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU,
|
381 |
+
0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U,
|
382 |
+
0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU,
|
383 |
+
0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U,
|
384 |
+
0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U,
|
385 |
+
0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U,
|
386 |
+
0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU,
|
387 |
+
0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U,
|
388 |
+
0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U,
|
389 |
+
0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U,
|
390 |
+
0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U,
|
391 |
+
0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U,
|
392 |
+
0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U,
|
393 |
+
0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU,
|
394 |
+
0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU,
|
395 |
+
0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U,
|
396 |
+
0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU,
|
397 |
+
0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U,
|
398 |
+
0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU,
|
399 |
+
0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU,
|
400 |
+
0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U,
|
401 |
+
0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU,
|
402 |
+
0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U,
|
403 |
+
0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U,
|
404 |
+
0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U,
|
405 |
+
0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U,
|
406 |
+
0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U,
|
407 |
+
0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U,
|
408 |
+
0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U,
|
409 |
+
0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU,
|
410 |
+
0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U,
|
411 |
+
0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U,
|
412 |
+
0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU,
|
413 |
+
0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U,
|
414 |
+
0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U,
|
415 |
+
0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U,
|
416 |
+
0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U,
|
417 |
+
};
|
418 |
+
static const u32 Td2[256] = {
|
419 |
+
0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U,
|
420 |
+
0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U,
|
421 |
+
0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U,
|
422 |
+
0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U,
|
423 |
+
0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU,
|
424 |
+
0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U,
|
425 |
+
0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U,
|
426 |
+
0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U,
|
427 |
+
0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U,
|
428 |
+
0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU,
|
429 |
+
0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U,
|
430 |
+
0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U,
|
431 |
+
0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU,
|
432 |
+
0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U,
|
433 |
+
0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U,
|
434 |
+
0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U,
|
435 |
+
0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U,
|
436 |
+
0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U,
|
437 |
+
0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U,
|
438 |
+
0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU,
|
439 |
+
0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U,
|
440 |
+
0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U,
|
441 |
+
0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U,
|
442 |
+
0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U,
|
443 |
+
0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U,
|
444 |
+
0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU,
|
445 |
+
0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU,
|
446 |
+
0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U,
|
447 |
+
0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU,
|
448 |
+
0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U,
|
449 |
+
0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU,
|
450 |
+
0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU,
|
451 |
+
0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU,
|
452 |
+
0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU,
|
453 |
+
0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U,
|
454 |
+
0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U,
|
455 |
+
0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U,
|
456 |
+
0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U,
|
457 |
+
0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U,
|
458 |
+
0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U,
|
459 |
+
0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U,
|
460 |
+
0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU,
|
461 |
+
0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU,
|
462 |
+
0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U,
|
463 |
+
0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U,
|
464 |
+
0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU,
|
465 |
+
0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU,
|
466 |
+
0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U,
|
467 |
+
0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U,
|
468 |
+
0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U,
|
469 |
+
0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U,
|
470 |
+
0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U,
|
471 |
+
0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U,
|
472 |
+
0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U,
|
473 |
+
0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU,
|
474 |
+
0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U,
|
475 |
+
0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U,
|
476 |
+
0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U,
|
477 |
+
0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U,
|
478 |
+
0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U,
|
479 |
+
0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U,
|
480 |
+
0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU,
|
481 |
+
0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U,
|
482 |
+
0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U,
|
483 |
+
};
|
484 |
+
static const u32 Td3[256] = {
|
485 |
+
0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU,
|
486 |
+
0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU,
|
487 |
+
0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U,
|
488 |
+
0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U,
|
489 |
+
0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU,
|
490 |
+
0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU,
|
491 |
+
0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U,
|
492 |
+
0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU,
|
493 |
+
0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U,
|
494 |
+
0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU,
|
495 |
+
0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U,
|
496 |
+
0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U,
|
497 |
+
0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U,
|
498 |
+
0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U,
|
499 |
+
0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U,
|
500 |
+
0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU,
|
501 |
+
0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU,
|
502 |
+
0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U,
|
503 |
+
0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U,
|
504 |
+
0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU,
|
505 |
+
0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU,
|
506 |
+
0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U,
|
507 |
+
0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U,
|
508 |
+
0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U,
|
509 |
+
0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U,
|
510 |
+
0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU,
|
511 |
+
0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U,
|
512 |
+
0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U,
|
513 |
+
0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU,
|
514 |
+
0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU,
|
515 |
+
0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U,
|
516 |
+
0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U,
|
517 |
+
0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U,
|
518 |
+
0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU,
|
519 |
+
0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U,
|
520 |
+
0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U,
|
521 |
+
0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U,
|
522 |
+
0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U,
|
523 |
+
0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U,
|
524 |
+
0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U,
|
525 |
+
0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U,
|
526 |
+
0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU,
|
527 |
+
0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U,
|
528 |
+
0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U,
|
529 |
+
0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU,
|
530 |
+
0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU,
|
531 |
+
0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U,
|
532 |
+
0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU,
|
533 |
+
0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U,
|
534 |
+
0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U,
|
535 |
+
0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U,
|
536 |
+
0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U,
|
537 |
+
0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U,
|
538 |
+
0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U,
|
539 |
+
0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU,
|
540 |
+
0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU,
|
541 |
+
0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU,
|
542 |
+
0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU,
|
543 |
+
0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U,
|
544 |
+
0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U,
|
545 |
+
0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U,
|
546 |
+
0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU,
|
547 |
+
0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U,
|
548 |
+
0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U,
|
549 |
+
};
|
550 |
+
static const u8 Td4[256] = {
|
551 |
+
0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U,
|
552 |
+
0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU,
|
553 |
+
0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U,
|
554 |
+
0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU,
|
555 |
+
0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU,
|
556 |
+
0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU,
|
557 |
+
0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U,
|
558 |
+
0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U,
|
559 |
+
0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U,
|
560 |
+
0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U,
|
561 |
+
0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU,
|
562 |
+
0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U,
|
563 |
+
0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU,
|
564 |
+
0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U,
|
565 |
+
0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U,
|
566 |
+
0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU,
|
567 |
+
0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU,
|
568 |
+
0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U,
|
569 |
+
0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U,
|
570 |
+
0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU,
|
571 |
+
0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U,
|
572 |
+
0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU,
|
573 |
+
0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U,
|
574 |
+
0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U,
|
575 |
+
0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U,
|
576 |
+
0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU,
|
577 |
+
0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU,
|
578 |
+
0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU,
|
579 |
+
0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U,
|
580 |
+
0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U,
|
581 |
+
0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U,
|
582 |
+
0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU,
|
583 |
+
};
|
584 |
+
static const u32 rcon[] = {
|
585 |
+
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
586 |
+
0x10000000, 0x20000000, 0x40000000, 0x80000000,
|
587 |
+
0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
|
588 |
+
};
|
589 |
+
|
590 |
+
/**
|
591 |
+
* Expand the cipher key into the encryption key schedule.
|
592 |
+
*/
|
593 |
+
int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
594 |
+
AES_KEY *key)
|
595 |
+
{
|
596 |
+
|
597 |
+
u32 *rk;
|
598 |
+
int i = 0;
|
599 |
+
u32 temp;
|
600 |
+
|
601 |
+
if (!userKey || !key)
|
602 |
+
return -1;
|
603 |
+
if (bits != 128 && bits != 192 && bits != 256)
|
604 |
+
return -2;
|
605 |
+
|
606 |
+
rk = key->rd_key;
|
607 |
+
|
608 |
+
if (bits == 128)
|
609 |
+
key->rounds = 10;
|
610 |
+
else if (bits == 192)
|
611 |
+
key->rounds = 12;
|
612 |
+
else
|
613 |
+
key->rounds = 14;
|
614 |
+
|
615 |
+
rk[0] = GETU32(userKey );
|
616 |
+
rk[1] = GETU32(userKey + 4);
|
617 |
+
rk[2] = GETU32(userKey + 8);
|
618 |
+
rk[3] = GETU32(userKey + 12);
|
619 |
+
if (bits == 128) {
|
620 |
+
while (1) {
|
621 |
+
temp = rk[3];
|
622 |
+
rk[4] = rk[0] ^
|
623 |
+
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
624 |
+
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
625 |
+
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
626 |
+
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
627 |
+
rcon[i];
|
628 |
+
rk[5] = rk[1] ^ rk[4];
|
629 |
+
rk[6] = rk[2] ^ rk[5];
|
630 |
+
rk[7] = rk[3] ^ rk[6];
|
631 |
+
if (++i == 10) {
|
632 |
+
return 0;
|
633 |
+
}
|
634 |
+
rk += 4;
|
635 |
+
}
|
636 |
+
}
|
637 |
+
rk[4] = GETU32(userKey + 16);
|
638 |
+
rk[5] = GETU32(userKey + 20);
|
639 |
+
if (bits == 192) {
|
640 |
+
while (1) {
|
641 |
+
temp = rk[ 5];
|
642 |
+
rk[ 6] = rk[ 0] ^
|
643 |
+
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
644 |
+
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
645 |
+
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
646 |
+
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
647 |
+
rcon[i];
|
648 |
+
rk[ 7] = rk[ 1] ^ rk[ 6];
|
649 |
+
rk[ 8] = rk[ 2] ^ rk[ 7];
|
650 |
+
rk[ 9] = rk[ 3] ^ rk[ 8];
|
651 |
+
if (++i == 8) {
|
652 |
+
return 0;
|
653 |
+
}
|
654 |
+
rk[10] = rk[ 4] ^ rk[ 9];
|
655 |
+
rk[11] = rk[ 5] ^ rk[10];
|
656 |
+
rk += 6;
|
657 |
+
}
|
658 |
+
}
|
659 |
+
rk[6] = GETU32(userKey + 24);
|
660 |
+
rk[7] = GETU32(userKey + 28);
|
661 |
+
if (bits == 256) {
|
662 |
+
while (1) {
|
663 |
+
temp = rk[ 7];
|
664 |
+
rk[ 8] = rk[ 0] ^
|
665 |
+
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
666 |
+
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
667 |
+
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
668 |
+
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
669 |
+
rcon[i];
|
670 |
+
rk[ 9] = rk[ 1] ^ rk[ 8];
|
671 |
+
rk[10] = rk[ 2] ^ rk[ 9];
|
672 |
+
rk[11] = rk[ 3] ^ rk[10];
|
673 |
+
if (++i == 7) {
|
674 |
+
return 0;
|
675 |
+
}
|
676 |
+
temp = rk[11];
|
677 |
+
rk[12] = rk[ 4] ^
|
678 |
+
(Te2[(temp >> 24) ] & 0xff000000) ^
|
679 |
+
(Te3[(temp >> 16) & 0xff] & 0x00ff0000) ^
|
680 |
+
(Te0[(temp >> 8) & 0xff] & 0x0000ff00) ^
|
681 |
+
(Te1[(temp ) & 0xff] & 0x000000ff);
|
682 |
+
rk[13] = rk[ 5] ^ rk[12];
|
683 |
+
rk[14] = rk[ 6] ^ rk[13];
|
684 |
+
rk[15] = rk[ 7] ^ rk[14];
|
685 |
+
|
686 |
+
rk += 8;
|
687 |
+
}
|
688 |
+
}
|
689 |
+
return 0;
|
690 |
+
}
|
691 |
+
|
692 |
+
/**
|
693 |
+
* Expand the cipher key into the decryption key schedule.
|
694 |
+
*/
|
695 |
+
int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
696 |
+
AES_KEY *key)
|
697 |
+
{
|
698 |
+
|
699 |
+
u32 *rk;
|
700 |
+
int i, j, status;
|
701 |
+
u32 temp;
|
702 |
+
|
703 |
+
/* first, start with an encryption schedule */
|
704 |
+
status = AES_set_encrypt_key(userKey, bits, key);
|
705 |
+
if (status < 0)
|
706 |
+
return status;
|
707 |
+
|
708 |
+
rk = key->rd_key;
|
709 |
+
|
710 |
+
/* invert the order of the round keys: */
|
711 |
+
for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) {
|
712 |
+
temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
|
713 |
+
temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
|
714 |
+
temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
|
715 |
+
temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
|
716 |
+
}
|
717 |
+
/* apply the inverse MixColumn transform to all round keys but the first and the last: */
|
718 |
+
for (i = 1; i < (key->rounds); i++) {
|
719 |
+
rk += 4;
|
720 |
+
rk[0] =
|
721 |
+
Td0[Te1[(rk[0] >> 24) ] & 0xff] ^
|
722 |
+
Td1[Te1[(rk[0] >> 16) & 0xff] & 0xff] ^
|
723 |
+
Td2[Te1[(rk[0] >> 8) & 0xff] & 0xff] ^
|
724 |
+
Td3[Te1[(rk[0] ) & 0xff] & 0xff];
|
725 |
+
rk[1] =
|
726 |
+
Td0[Te1[(rk[1] >> 24) ] & 0xff] ^
|
727 |
+
Td1[Te1[(rk[1] >> 16) & 0xff] & 0xff] ^
|
728 |
+
Td2[Te1[(rk[1] >> 8) & 0xff] & 0xff] ^
|
729 |
+
Td3[Te1[(rk[1] ) & 0xff] & 0xff];
|
730 |
+
rk[2] =
|
731 |
+
Td0[Te1[(rk[2] >> 24) ] & 0xff] ^
|
732 |
+
Td1[Te1[(rk[2] >> 16) & 0xff] & 0xff] ^
|
733 |
+
Td2[Te1[(rk[2] >> 8) & 0xff] & 0xff] ^
|
734 |
+
Td3[Te1[(rk[2] ) & 0xff] & 0xff];
|
735 |
+
rk[3] =
|
736 |
+
Td0[Te1[(rk[3] >> 24) ] & 0xff] ^
|
737 |
+
Td1[Te1[(rk[3] >> 16) & 0xff] & 0xff] ^
|
738 |
+
Td2[Te1[(rk[3] >> 8) & 0xff] & 0xff] ^
|
739 |
+
Td3[Te1[(rk[3] ) & 0xff] & 0xff];
|
740 |
+
}
|
741 |
+
return 0;
|
742 |
+
}
|
743 |
+
|
744 |
+
/*
|
745 |
+
* Encrypt a single block
|
746 |
+
* in and out can overlap
|
747 |
+
*/
|
748 |
+
void AES_encrypt(const unsigned char *in, unsigned char *out,
|
749 |
+
const AES_KEY *key) {
|
750 |
+
|
751 |
+
const u32 *rk;
|
752 |
+
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
753 |
+
#ifndef FULL_UNROLL
|
754 |
+
int r;
|
755 |
+
#endif /* ?FULL_UNROLL */
|
756 |
+
|
757 |
+
assert(in && out && key);
|
758 |
+
rk = key->rd_key;
|
759 |
+
|
760 |
+
/*
|
761 |
+
* map byte array block to cipher state
|
762 |
+
* and add initial round key:
|
763 |
+
*/
|
764 |
+
s0 = GETU32(in ) ^ rk[0];
|
765 |
+
s1 = GETU32(in + 4) ^ rk[1];
|
766 |
+
s2 = GETU32(in + 8) ^ rk[2];
|
767 |
+
s3 = GETU32(in + 12) ^ rk[3];
|
768 |
+
#ifdef FULL_UNROLL
|
769 |
+
/* round 1: */
|
770 |
+
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4];
|
771 |
+
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[ 5];
|
772 |
+
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[ 6];
|
773 |
+
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[ 7];
|
774 |
+
/* round 2: */
|
775 |
+
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[ 8];
|
776 |
+
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[ 9];
|
777 |
+
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[10];
|
778 |
+
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[11];
|
779 |
+
/* round 3: */
|
780 |
+
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[12];
|
781 |
+
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[13];
|
782 |
+
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[14];
|
783 |
+
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[15];
|
784 |
+
/* round 4: */
|
785 |
+
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[16];
|
786 |
+
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[17];
|
787 |
+
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[18];
|
788 |
+
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[19];
|
789 |
+
/* round 5: */
|
790 |
+
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[20];
|
791 |
+
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[21];
|
792 |
+
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[22];
|
793 |
+
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[23];
|
794 |
+
/* round 6: */
|
795 |
+
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[24];
|
796 |
+
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[25];
|
797 |
+
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[26];
|
798 |
+
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[27];
|
799 |
+
/* round 7: */
|
800 |
+
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[28];
|
801 |
+
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[29];
|
802 |
+
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[30];
|
803 |
+
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[31];
|
804 |
+
/* round 8: */
|
805 |
+
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[32];
|
806 |
+
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[33];
|
807 |
+
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[34];
|
808 |
+
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[35];
|
809 |
+
/* round 9: */
|
810 |
+
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[36];
|
811 |
+
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[37];
|
812 |
+
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[38];
|
813 |
+
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[39];
|
814 |
+
if (key->rounds > 10) {
|
815 |
+
/* round 10: */
|
816 |
+
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40];
|
817 |
+
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[41];
|
818 |
+
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[42];
|
819 |
+
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[43];
|
820 |
+
/* round 11: */
|
821 |
+
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[44];
|
822 |
+
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[45];
|
823 |
+
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[46];
|
824 |
+
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[47];
|
825 |
+
if (key->rounds > 12) {
|
826 |
+
/* round 12: */
|
827 |
+
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[48];
|
828 |
+
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[49];
|
829 |
+
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[50];
|
830 |
+
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[51];
|
831 |
+
/* round 13: */
|
832 |
+
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[52];
|
833 |
+
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[53];
|
834 |
+
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[54];
|
835 |
+
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[55];
|
836 |
+
}
|
837 |
+
}
|
838 |
+
rk += key->rounds << 2;
|
839 |
+
#else /* !FULL_UNROLL */
|
840 |
+
/*
|
841 |
+
* Nr - 1 full rounds:
|
842 |
+
*/
|
843 |
+
r = key->rounds >> 1;
|
844 |
+
for (;;) {
|
845 |
+
t0 =
|
846 |
+
Te0[(s0 >> 24) ] ^
|
847 |
+
Te1[(s1 >> 16) & 0xff] ^
|
848 |
+
Te2[(s2 >> 8) & 0xff] ^
|
849 |
+
Te3[(s3 ) & 0xff] ^
|
850 |
+
rk[4];
|
851 |
+
t1 =
|
852 |
+
Te0[(s1 >> 24) ] ^
|
853 |
+
Te1[(s2 >> 16) & 0xff] ^
|
854 |
+
Te2[(s3 >> 8) & 0xff] ^
|
855 |
+
Te3[(s0 ) & 0xff] ^
|
856 |
+
rk[5];
|
857 |
+
t2 =
|
858 |
+
Te0[(s2 >> 24) ] ^
|
859 |
+
Te1[(s3 >> 16) & 0xff] ^
|
860 |
+
Te2[(s0 >> 8) & 0xff] ^
|
861 |
+
Te3[(s1 ) & 0xff] ^
|
862 |
+
rk[6];
|
863 |
+
t3 =
|
864 |
+
Te0[(s3 >> 24) ] ^
|
865 |
+
Te1[(s0 >> 16) & 0xff] ^
|
866 |
+
Te2[(s1 >> 8) & 0xff] ^
|
867 |
+
Te3[(s2 ) & 0xff] ^
|
868 |
+
rk[7];
|
869 |
+
|
870 |
+
rk += 8;
|
871 |
+
if (--r == 0) {
|
872 |
+
break;
|
873 |
+
}
|
874 |
+
|
875 |
+
s0 =
|
876 |
+
Te0[(t0 >> 24) ] ^
|
877 |
+
Te1[(t1 >> 16) & 0xff] ^
|
878 |
+
Te2[(t2 >> 8) & 0xff] ^
|
879 |
+
Te3[(t3 ) & 0xff] ^
|
880 |
+
rk[0];
|
881 |
+
s1 =
|
882 |
+
Te0[(t1 >> 24) ] ^
|
883 |
+
Te1[(t2 >> 16) & 0xff] ^
|
884 |
+
Te2[(t3 >> 8) & 0xff] ^
|
885 |
+
Te3[(t0 ) & 0xff] ^
|
886 |
+
rk[1];
|
887 |
+
s2 =
|
888 |
+
Te0[(t2 >> 24) ] ^
|
889 |
+
Te1[(t3 >> 16) & 0xff] ^
|
890 |
+
Te2[(t0 >> 8) & 0xff] ^
|
891 |
+
Te3[(t1 ) & 0xff] ^
|
892 |
+
rk[2];
|
893 |
+
s3 =
|
894 |
+
Te0[(t3 >> 24) ] ^
|
895 |
+
Te1[(t0 >> 16) & 0xff] ^
|
896 |
+
Te2[(t1 >> 8) & 0xff] ^
|
897 |
+
Te3[(t2 ) & 0xff] ^
|
898 |
+
rk[3];
|
899 |
+
}
|
900 |
+
#endif /* ?FULL_UNROLL */
|
901 |
+
/*
|
902 |
+
* apply last round and
|
903 |
+
* map cipher state to byte array block:
|
904 |
+
*/
|
905 |
+
s0 =
|
906 |
+
(Te2[(t0 >> 24) ] & 0xff000000) ^
|
907 |
+
(Te3[(t1 >> 16) & 0xff] & 0x00ff0000) ^
|
908 |
+
(Te0[(t2 >> 8) & 0xff] & 0x0000ff00) ^
|
909 |
+
(Te1[(t3 ) & 0xff] & 0x000000ff) ^
|
910 |
+
rk[0];
|
911 |
+
PUTU32(out , s0);
|
912 |
+
s1 =
|
913 |
+
(Te2[(t1 >> 24) ] & 0xff000000) ^
|
914 |
+
(Te3[(t2 >> 16) & 0xff] & 0x00ff0000) ^
|
915 |
+
(Te0[(t3 >> 8) & 0xff] & 0x0000ff00) ^
|
916 |
+
(Te1[(t0 ) & 0xff] & 0x000000ff) ^
|
917 |
+
rk[1];
|
918 |
+
PUTU32(out + 4, s1);
|
919 |
+
s2 =
|
920 |
+
(Te2[(t2 >> 24) ] & 0xff000000) ^
|
921 |
+
(Te3[(t3 >> 16) & 0xff] & 0x00ff0000) ^
|
922 |
+
(Te0[(t0 >> 8) & 0xff] & 0x0000ff00) ^
|
923 |
+
(Te1[(t1 ) & 0xff] & 0x000000ff) ^
|
924 |
+
rk[2];
|
925 |
+
PUTU32(out + 8, s2);
|
926 |
+
s3 =
|
927 |
+
(Te2[(t3 >> 24) ] & 0xff000000) ^
|
928 |
+
(Te3[(t0 >> 16) & 0xff] & 0x00ff0000) ^
|
929 |
+
(Te0[(t1 >> 8) & 0xff] & 0x0000ff00) ^
|
930 |
+
(Te1[(t2 ) & 0xff] & 0x000000ff) ^
|
931 |
+
rk[3];
|
932 |
+
PUTU32(out + 12, s3);
|
933 |
+
}
|
934 |
+
|
935 |
+
/*
|
936 |
+
* Decrypt a single block
|
937 |
+
* in and out can overlap
|
938 |
+
*/
|
939 |
+
void AES_decrypt(const unsigned char *in, unsigned char *out,
|
940 |
+
const AES_KEY *key)
|
941 |
+
{
|
942 |
+
|
943 |
+
const u32 *rk;
|
944 |
+
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
945 |
+
#ifndef FULL_UNROLL
|
946 |
+
int r;
|
947 |
+
#endif /* ?FULL_UNROLL */
|
948 |
+
|
949 |
+
assert(in && out && key);
|
950 |
+
rk = key->rd_key;
|
951 |
+
|
952 |
+
/*
|
953 |
+
* map byte array block to cipher state
|
954 |
+
* and add initial round key:
|
955 |
+
*/
|
956 |
+
s0 = GETU32(in ) ^ rk[0];
|
957 |
+
s1 = GETU32(in + 4) ^ rk[1];
|
958 |
+
s2 = GETU32(in + 8) ^ rk[2];
|
959 |
+
s3 = GETU32(in + 12) ^ rk[3];
|
960 |
+
#ifdef FULL_UNROLL
|
961 |
+
/* round 1: */
|
962 |
+
t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[ 4];
|
963 |
+
t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[ 5];
|
964 |
+
t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[ 6];
|
965 |
+
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[ 7];
|
966 |
+
/* round 2: */
|
967 |
+
s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[ 8];
|
968 |
+
s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[ 9];
|
969 |
+
s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[10];
|
970 |
+
s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[11];
|
971 |
+
/* round 3: */
|
972 |
+
t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[12];
|
973 |
+
t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[13];
|
974 |
+
t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[14];
|
975 |
+
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[15];
|
976 |
+
/* round 4: */
|
977 |
+
s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[16];
|
978 |
+
s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[17];
|
979 |
+
s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[18];
|
980 |
+
s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[19];
|
981 |
+
/* round 5: */
|
982 |
+
t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[20];
|
983 |
+
t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[21];
|
984 |
+
t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[22];
|
985 |
+
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[23];
|
986 |
+
/* round 6: */
|
987 |
+
s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[24];
|
988 |
+
s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[25];
|
989 |
+
s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[26];
|
990 |
+
s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[27];
|
991 |
+
/* round 7: */
|
992 |
+
t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[28];
|
993 |
+
t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[29];
|
994 |
+
t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[30];
|
995 |
+
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[31];
|
996 |
+
/* round 8: */
|
997 |
+
s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[32];
|
998 |
+
s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[33];
|
999 |
+
s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[34];
|
1000 |
+
s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[35];
|
1001 |
+
/* round 9: */
|
1002 |
+
t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[36];
|
1003 |
+
t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[37];
|
1004 |
+
t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[38];
|
1005 |
+
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[39];
|
1006 |
+
if (key->rounds > 10) {
|
1007 |
+
/* round 10: */
|
1008 |
+
s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[40];
|
1009 |
+
s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[41];
|
1010 |
+
s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[42];
|
1011 |
+
s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[43];
|
1012 |
+
/* round 11: */
|
1013 |
+
t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[44];
|
1014 |
+
t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[45];
|
1015 |
+
t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[46];
|
1016 |
+
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[47];
|
1017 |
+
if (key->rounds > 12) {
|
1018 |
+
/* round 12: */
|
1019 |
+
s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[48];
|
1020 |
+
s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[49];
|
1021 |
+
s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[50];
|
1022 |
+
s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[51];
|
1023 |
+
/* round 13: */
|
1024 |
+
t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[52];
|
1025 |
+
t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[53];
|
1026 |
+
t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[54];
|
1027 |
+
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[55];
|
1028 |
+
}
|
1029 |
+
}
|
1030 |
+
rk += key->rounds << 2;
|
1031 |
+
#else /* !FULL_UNROLL */
|
1032 |
+
/*
|
1033 |
+
* Nr - 1 full rounds:
|
1034 |
+
*/
|
1035 |
+
r = key->rounds >> 1;
|
1036 |
+
for (;;) {
|
1037 |
+
t0 =
|
1038 |
+
Td0[(s0 >> 24) ] ^
|
1039 |
+
Td1[(s3 >> 16) & 0xff] ^
|
1040 |
+
Td2[(s2 >> 8) & 0xff] ^
|
1041 |
+
Td3[(s1 ) & 0xff] ^
|
1042 |
+
rk[4];
|
1043 |
+
t1 =
|
1044 |
+
Td0[(s1 >> 24) ] ^
|
1045 |
+
Td1[(s0 >> 16) & 0xff] ^
|
1046 |
+
Td2[(s3 >> 8) & 0xff] ^
|
1047 |
+
Td3[(s2 ) & 0xff] ^
|
1048 |
+
rk[5];
|
1049 |
+
t2 =
|
1050 |
+
Td0[(s2 >> 24) ] ^
|
1051 |
+
Td1[(s1 >> 16) & 0xff] ^
|
1052 |
+
Td2[(s0 >> 8) & 0xff] ^
|
1053 |
+
Td3[(s3 ) & 0xff] ^
|
1054 |
+
rk[6];
|
1055 |
+
t3 =
|
1056 |
+
Td0[(s3 >> 24) ] ^
|
1057 |
+
Td1[(s2 >> 16) & 0xff] ^
|
1058 |
+
Td2[(s1 >> 8) & 0xff] ^
|
1059 |
+
Td3[(s0 ) & 0xff] ^
|
1060 |
+
rk[7];
|
1061 |
+
|
1062 |
+
rk += 8;
|
1063 |
+
if (--r == 0) {
|
1064 |
+
break;
|
1065 |
+
}
|
1066 |
+
|
1067 |
+
s0 =
|
1068 |
+
Td0[(t0 >> 24) ] ^
|
1069 |
+
Td1[(t3 >> 16) & 0xff] ^
|
1070 |
+
Td2[(t2 >> 8) & 0xff] ^
|
1071 |
+
Td3[(t1 ) & 0xff] ^
|
1072 |
+
rk[0];
|
1073 |
+
s1 =
|
1074 |
+
Td0[(t1 >> 24) ] ^
|
1075 |
+
Td1[(t0 >> 16) & 0xff] ^
|
1076 |
+
Td2[(t3 >> 8) & 0xff] ^
|
1077 |
+
Td3[(t2 ) & 0xff] ^
|
1078 |
+
rk[1];
|
1079 |
+
s2 =
|
1080 |
+
Td0[(t2 >> 24) ] ^
|
1081 |
+
Td1[(t1 >> 16) & 0xff] ^
|
1082 |
+
Td2[(t0 >> 8) & 0xff] ^
|
1083 |
+
Td3[(t3 ) & 0xff] ^
|
1084 |
+
rk[2];
|
1085 |
+
s3 =
|
1086 |
+
Td0[(t3 >> 24) ] ^
|
1087 |
+
Td1[(t2 >> 16) & 0xff] ^
|
1088 |
+
Td2[(t1 >> 8) & 0xff] ^
|
1089 |
+
Td3[(t0 ) & 0xff] ^
|
1090 |
+
rk[3];
|
1091 |
+
}
|
1092 |
+
#endif /* ?FULL_UNROLL */
|
1093 |
+
/*
|
1094 |
+
* apply last round and
|
1095 |
+
* map cipher state to byte array block:
|
1096 |
+
*/
|
1097 |
+
s0 =
|
1098 |
+
((u32)Td4[(t0 >> 24) ] << 24) ^
|
1099 |
+
((u32)Td4[(t3 >> 16) & 0xff] << 16) ^
|
1100 |
+
((u32)Td4[(t2 >> 8) & 0xff] << 8) ^
|
1101 |
+
((u32)Td4[(t1 ) & 0xff]) ^
|
1102 |
+
rk[0];
|
1103 |
+
PUTU32(out , s0);
|
1104 |
+
s1 =
|
1105 |
+
((u32)Td4[(t1 >> 24) ] << 24) ^
|
1106 |
+
((u32)Td4[(t0 >> 16) & 0xff] << 16) ^
|
1107 |
+
((u32)Td4[(t3 >> 8) & 0xff] << 8) ^
|
1108 |
+
((u32)Td4[(t2 ) & 0xff]) ^
|
1109 |
+
rk[1];
|
1110 |
+
PUTU32(out + 4, s1);
|
1111 |
+
s2 =
|
1112 |
+
((u32)Td4[(t2 >> 24) ] << 24) ^
|
1113 |
+
((u32)Td4[(t1 >> 16) & 0xff] << 16) ^
|
1114 |
+
((u32)Td4[(t0 >> 8) & 0xff] << 8) ^
|
1115 |
+
((u32)Td4[(t3 ) & 0xff]) ^
|
1116 |
+
rk[2];
|
1117 |
+
PUTU32(out + 8, s2);
|
1118 |
+
s3 =
|
1119 |
+
((u32)Td4[(t3 >> 24) ] << 24) ^
|
1120 |
+
((u32)Td4[(t2 >> 16) & 0xff] << 16) ^
|
1121 |
+
((u32)Td4[(t1 >> 8) & 0xff] << 8) ^
|
1122 |
+
((u32)Td4[(t0 ) & 0xff]) ^
|
1123 |
+
rk[3];
|
1124 |
+
PUTU32(out + 12, s3);
|
1125 |
+
}
|
1126 |
+
|
1127 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes_ecb.c
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
|
3 |
+
*
|
4 |
+
* Licensed under the OpenSSL license (the "License"). You may not use
|
5 |
+
* this file except in compliance with the License. You can obtain a copy
|
6 |
+
* in the file LICENSE in the source distribution or at
|
7 |
+
* https://www.openssl.org/source/license.html
|
8 |
+
*/
|
9 |
+
|
10 |
+
#include <assert.h>
|
11 |
+
|
12 |
+
#include "aes.h"
|
13 |
+
#include "aes_locl.h"
|
14 |
+
|
15 |
+
void AES_ecb_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key, const int enc)
|
16 |
+
{
|
17 |
+
assert(in && out && key);
|
18 |
+
assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
|
19 |
+
|
20 |
+
if (AES_ENCRYPT == enc)
|
21 |
+
AES_encrypt(in, out, key);
|
22 |
+
else
|
23 |
+
AES_decrypt(in, out, key);
|
24 |
+
}
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aes_locl.h
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
|
3 |
+
*
|
4 |
+
* Licensed under the OpenSSL license (the "License"). You may not use
|
5 |
+
* this file except in compliance with the License. You can obtain a copy
|
6 |
+
* in the file LICENSE in the source distribution or at
|
7 |
+
* https://www.openssl.org/source/license.html
|
8 |
+
*/
|
9 |
+
|
10 |
+
#ifndef HEADER_AES_LOCL_H
|
11 |
+
# define HEADER_AES_LOCL_H
|
12 |
+
|
13 |
+
//# include <e_os2.h>
|
14 |
+
# include <stdio.h>
|
15 |
+
# include <stdlib.h>
|
16 |
+
# include <string.h>
|
17 |
+
|
18 |
+
# if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
|
19 |
+
# define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
|
20 |
+
# define GETU32(p) SWAP(*((u32 *)(p)))
|
21 |
+
# define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
|
22 |
+
# else
|
23 |
+
# define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]))
|
24 |
+
# define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); }
|
25 |
+
# endif
|
26 |
+
|
27 |
+
# ifdef AES_LONG
|
28 |
+
typedef unsigned long u32;
|
29 |
+
# else
|
30 |
+
typedef unsigned int u32;
|
31 |
+
# endif
|
32 |
+
typedef unsigned short u16;
|
33 |
+
typedef unsigned char u8;
|
34 |
+
|
35 |
+
# define MAXKC (256/32)
|
36 |
+
# define MAXKB (256/8)
|
37 |
+
# define MAXNR 14
|
38 |
+
|
39 |
+
/* This controls loop-unrolling in aes_core.c */
|
40 |
+
# undef FULL_UNROLL
|
41 |
+
|
42 |
+
#endif /* !HEADER_AES_LOCL_H */
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aesmain.c
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <stdio.h>
|
2 |
+
#include <stdlib.h>
|
3 |
+
#include <stdint.h>
|
4 |
+
#include <string.h>
|
5 |
+
#include "gj_aes.h"
|
6 |
+
#include "aesmain.h"
|
7 |
+
|
8 |
+
int mainenc(int enc,char* infn,char* outfn){
|
9 |
+
char result[255] ;
|
10 |
+
memset(result,0,255);
|
11 |
+
char* key = "yymrjzbwyrbjszrk";
|
12 |
+
char* aiv = "yymrjzbwyrbjszrk";
|
13 |
+
int base64 = 1;
|
14 |
+
int outlen = 0;
|
15 |
+
int encrst = 0;
|
16 |
+
char* fn1 = infn;
|
17 |
+
char* fn2 = outfn;
|
18 |
+
FILE* fr = fopen(fn1,"rb");
|
19 |
+
FILE* fw = fopen(fn2,"wb");
|
20 |
+
while(1){
|
21 |
+
if(!fr){
|
22 |
+
encrst = -1001;
|
23 |
+
break;
|
24 |
+
}
|
25 |
+
if(!fw){
|
26 |
+
encrst = -1002;
|
27 |
+
break;
|
28 |
+
}
|
29 |
+
gj_aesc_t* aesc = NULL;
|
30 |
+
init_aesc(key,aiv,enc,&aesc);
|
31 |
+
uint64_t size = 0;
|
32 |
+
uint64_t realsize = 0;
|
33 |
+
if(enc){
|
34 |
+
fwrite("gjdigits",1,8,fw);
|
35 |
+
fwrite(&size,1,8,fw);
|
36 |
+
fwrite(&size,1,8,fw);
|
37 |
+
fwrite(&size,1,8,fw);
|
38 |
+
|
39 |
+
while(!feof(fr)){
|
40 |
+
char data[16];
|
41 |
+
memset(data,0,16);
|
42 |
+
uint64_t rst = fread(data,1,16,fr);
|
43 |
+
if(rst){
|
44 |
+
size +=rst;
|
45 |
+
do_aesc(aesc,data,16,result,&outlen);
|
46 |
+
fwrite(result,1,outlen,fw);
|
47 |
+
}
|
48 |
+
}
|
49 |
+
fseek(fw,8,0);
|
50 |
+
fwrite(&size,1,8,fw);
|
51 |
+
|
52 |
+
}else{
|
53 |
+
uint64_t rst = fread(result,1,32,fr);
|
54 |
+
if(!rst){
|
55 |
+
encrst = -1003;
|
56 |
+
break;
|
57 |
+
}
|
58 |
+
if((result[0]!='g')||(result[1]!='j')){
|
59 |
+
encrst = -1004;
|
60 |
+
break;
|
61 |
+
}
|
62 |
+
uint64_t *psize = (uint64_t*)(result+8);
|
63 |
+
realsize = *psize;
|
64 |
+
if(realsize>1034*1024*1024){
|
65 |
+
encrst = -1005;
|
66 |
+
break;
|
67 |
+
}
|
68 |
+
while(!feof(fr)){
|
69 |
+
char data[16];
|
70 |
+
memset(data,0,16);
|
71 |
+
uint64_t rst = fread(data,1,16,fr);
|
72 |
+
if(rst){
|
73 |
+
size +=rst;
|
74 |
+
do_aesc(aesc,data,16,result,&outlen);
|
75 |
+
if(size>realsize){
|
76 |
+
outlen -= (size-realsize);
|
77 |
+
//printf("===%lu > %lu rst %lu %d outlen \n",size,realsize,rst,outlen);
|
78 |
+
}
|
79 |
+
fwrite(result,1,outlen,fw);
|
80 |
+
}
|
81 |
+
}
|
82 |
+
}
|
83 |
+
break;
|
84 |
+
}
|
85 |
+
if(fr) fclose(fr);
|
86 |
+
if(fw) fclose(fw);
|
87 |
+
return encrst;
|
88 |
+
}
|
89 |
+
|
90 |
+
|
91 |
+
#ifdef TEST
|
92 |
+
int main(int argc,char** argv){
|
93 |
+
if(argc<4){
|
94 |
+
printf("gaes enc|dec filein fileout\n");
|
95 |
+
return 0;
|
96 |
+
}
|
97 |
+
char k = argv[1][0];
|
98 |
+
if(k=='e'){
|
99 |
+
int rst = mainenc(1,argv[2],argv[3]);
|
100 |
+
printf("====enc %s to %s rst %d\n",argv[2],argv[3],rst);
|
101 |
+
return rst;
|
102 |
+
}else if(k=='d'){
|
103 |
+
int rst = mainenc(0,argv[2],argv[3]);
|
104 |
+
printf("====dec %s to %s rst %d\n",argv[2],argv[3],rst);
|
105 |
+
return rst;
|
106 |
+
}else{
|
107 |
+
printf("gaes enc|dec filein fileout\n");
|
108 |
+
return 0;
|
109 |
+
}
|
110 |
+
}
|
111 |
+
#endif
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/aesmain.h
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
#ifndef __AESMAIN_H
|
3 |
+
#define __AESMAIN_H
|
4 |
+
|
5 |
+
#include "gj_dll.h"
|
6 |
+
#ifdef __cplusplus
|
7 |
+
extern "C" {
|
8 |
+
#endif
|
9 |
+
|
10 |
+
int mainenc(int enc,char* infn,char* outfn);
|
11 |
+
|
12 |
+
#ifdef __cplusplus
|
13 |
+
}
|
14 |
+
#endif
|
15 |
+
#endif
|
16 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/base64.c
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This is a public domain base64 implementation written by WEI Zhicheng. */
|
2 |
+
|
3 |
+
#include "base64.h"
|
4 |
+
|
5 |
+
#define BASE64_PAD '='
|
6 |
+
#define BASE64DE_FIRST '+'
|
7 |
+
#define BASE64DE_LAST 'z'
|
8 |
+
|
9 |
+
/* BASE 64 encode table */
|
10 |
+
static const char base64en[] = {
|
11 |
+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
12 |
+
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
13 |
+
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
14 |
+
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
15 |
+
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
16 |
+
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
17 |
+
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
18 |
+
'4', '5', '6', '7', '8', '9', '+', '/',
|
19 |
+
};
|
20 |
+
|
21 |
+
/* ASCII order for BASE 64 decode, 255 in unused character */
|
22 |
+
static const unsigned char base64de[] = {
|
23 |
+
/* nul, soh, stx, etx, eot, enq, ack, bel, */
|
24 |
+
255, 255, 255, 255, 255, 255, 255, 255,
|
25 |
+
|
26 |
+
/* bs, ht, nl, vt, np, cr, so, si, */
|
27 |
+
255, 255, 255, 255, 255, 255, 255, 255,
|
28 |
+
|
29 |
+
/* dle, dc1, dc2, dc3, dc4, nak, syn, etb, */
|
30 |
+
255, 255, 255, 255, 255, 255, 255, 255,
|
31 |
+
|
32 |
+
/* can, em, sub, esc, fs, gs, rs, us, */
|
33 |
+
255, 255, 255, 255, 255, 255, 255, 255,
|
34 |
+
|
35 |
+
/* sp, '!', '"', '#', '$', '%', '&', ''', */
|
36 |
+
255, 255, 255, 255, 255, 255, 255, 255,
|
37 |
+
|
38 |
+
/* '(', ')', '*', '+', ',', '-', '.', '/', */
|
39 |
+
255, 255, 255, 62, 255, 255, 255, 63,
|
40 |
+
|
41 |
+
/* '0', '1', '2', '3', '4', '5', '6', '7', */
|
42 |
+
52, 53, 54, 55, 56, 57, 58, 59,
|
43 |
+
|
44 |
+
/* '8', '9', ':', ';', '<', '=', '>', '?', */
|
45 |
+
60, 61, 255, 255, 255, 255, 255, 255,
|
46 |
+
|
47 |
+
/* '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', */
|
48 |
+
255, 0, 1, 2, 3, 4, 5, 6,
|
49 |
+
|
50 |
+
/* 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', */
|
51 |
+
7, 8, 9, 10, 11, 12, 13, 14,
|
52 |
+
|
53 |
+
/* 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', */
|
54 |
+
15, 16, 17, 18, 19, 20, 21, 22,
|
55 |
+
|
56 |
+
/* 'X', 'Y', 'Z', '[', '\', ']', '^', '_', */
|
57 |
+
23, 24, 25, 255, 255, 255, 255, 255,
|
58 |
+
|
59 |
+
/* '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', */
|
60 |
+
255, 26, 27, 28, 29, 30, 31, 32,
|
61 |
+
|
62 |
+
/* 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', */
|
63 |
+
33, 34, 35, 36, 37, 38, 39, 40,
|
64 |
+
|
65 |
+
/* 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', */
|
66 |
+
41, 42, 43, 44, 45, 46, 47, 48,
|
67 |
+
|
68 |
+
/* 'x', 'y', 'z', '{', '|', '}', '~', del, */
|
69 |
+
49, 50, 51, 255, 255, 255, 255, 255
|
70 |
+
};
|
71 |
+
|
72 |
+
unsigned int
|
73 |
+
gjbase64_encode(const unsigned char *in, unsigned int inlen, char *out)
|
74 |
+
{
|
75 |
+
int s;
|
76 |
+
unsigned int i;
|
77 |
+
unsigned int j;
|
78 |
+
unsigned char c;
|
79 |
+
unsigned char l;
|
80 |
+
|
81 |
+
s = 0;
|
82 |
+
l = 0;
|
83 |
+
for (i = j = 0; i < inlen; i++) {
|
84 |
+
c = in[i];
|
85 |
+
|
86 |
+
switch (s) {
|
87 |
+
case 0:
|
88 |
+
s = 1;
|
89 |
+
out[j++] = base64en[(c >> 2) & 0x3F];
|
90 |
+
break;
|
91 |
+
case 1:
|
92 |
+
s = 2;
|
93 |
+
out[j++] = base64en[((l & 0x3) << 4) | ((c >> 4) & 0xF)];
|
94 |
+
break;
|
95 |
+
case 2:
|
96 |
+
s = 0;
|
97 |
+
out[j++] = base64en[((l & 0xF) << 2) | ((c >> 6) & 0x3)];
|
98 |
+
out[j++] = base64en[c & 0x3F];
|
99 |
+
break;
|
100 |
+
}
|
101 |
+
l = c;
|
102 |
+
}
|
103 |
+
|
104 |
+
switch (s) {
|
105 |
+
case 1:
|
106 |
+
out[j++] = base64en[(l & 0x3) << 4];
|
107 |
+
out[j++] = BASE64_PAD;
|
108 |
+
out[j++] = BASE64_PAD;
|
109 |
+
break;
|
110 |
+
case 2:
|
111 |
+
out[j++] = base64en[(l & 0xF) << 2];
|
112 |
+
out[j++] = BASE64_PAD;
|
113 |
+
break;
|
114 |
+
}
|
115 |
+
|
116 |
+
out[j] = 0;
|
117 |
+
|
118 |
+
return j;
|
119 |
+
}
|
120 |
+
|
121 |
+
unsigned int
|
122 |
+
gjbase64_decode(const char *in, unsigned int inlen, unsigned char *out)
|
123 |
+
{
|
124 |
+
unsigned int i;
|
125 |
+
unsigned int j;
|
126 |
+
unsigned char c;
|
127 |
+
|
128 |
+
if (inlen & 0x3) {
|
129 |
+
return 0;
|
130 |
+
}
|
131 |
+
|
132 |
+
for (i = j = 0; i < inlen; i++) {
|
133 |
+
if (in[i] == BASE64_PAD) {
|
134 |
+
break;
|
135 |
+
}
|
136 |
+
if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST) {
|
137 |
+
return 0;
|
138 |
+
}
|
139 |
+
|
140 |
+
c = base64de[(unsigned char)in[i]];
|
141 |
+
if (c == 255) {
|
142 |
+
return 0;
|
143 |
+
}
|
144 |
+
|
145 |
+
switch (i & 0x3) {
|
146 |
+
case 0:
|
147 |
+
out[j] = (c << 2) & 0xFF;
|
148 |
+
break;
|
149 |
+
case 1:
|
150 |
+
out[j++] |= (c >> 4) & 0x3;
|
151 |
+
out[j] = (c & 0xF) << 4;
|
152 |
+
break;
|
153 |
+
case 2:
|
154 |
+
out[j++] |= (c >> 2) & 0xF;
|
155 |
+
out[j] = (c & 0x3) << 6;
|
156 |
+
break;
|
157 |
+
case 3:
|
158 |
+
out[j++] |= c;
|
159 |
+
break;
|
160 |
+
}
|
161 |
+
}
|
162 |
+
|
163 |
+
return j;
|
164 |
+
}
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/base64.h
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef BASE64_H
|
2 |
+
#define BASE64_H
|
3 |
+
|
4 |
+
#define BASE64_ENCODE_OUT_SIZE(s) ((unsigned int)((((s) + 2) / 3) * 4 + 1))
|
5 |
+
#define BASE64_DECODE_OUT_SIZE(s) ((unsigned int)(((s) / 4) * 3))
|
6 |
+
|
7 |
+
#ifdef __cplusplus
|
8 |
+
extern "C"{
|
9 |
+
#endif
|
10 |
+
|
11 |
+
/*
|
12 |
+
* out is null-terminated encode string.
|
13 |
+
* return values is out length, exclusive terminating `\0'
|
14 |
+
*/
|
15 |
+
unsigned int
|
16 |
+
gjbase64_encode(const unsigned char *in, unsigned int inlen, char *out);
|
17 |
+
|
18 |
+
/*
|
19 |
+
* return values is out length
|
20 |
+
*/
|
21 |
+
unsigned int
|
22 |
+
gjbase64_decode(const char *in, unsigned int inlen, unsigned char *out);
|
23 |
+
|
24 |
+
#ifdef __cplusplus
|
25 |
+
}
|
26 |
+
#endif
|
27 |
+
|
28 |
+
|
29 |
+
#endif /* BASE64_H */
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/cbc128.c
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
|
3 |
+
*
|
4 |
+
* Licensed under the OpenSSL license (the "License"). You may not use
|
5 |
+
* this file except in compliance with the License. You can obtain a copy
|
6 |
+
* in the file LICENSE in the source distribution or at
|
7 |
+
* https://www.openssl.org/source/license.html
|
8 |
+
*/
|
9 |
+
|
10 |
+
// #include <openssl/crypto.h>
|
11 |
+
#include "modes.h"
|
12 |
+
#include <string.h>
|
13 |
+
|
14 |
+
#if !defined(STRICT_ALIGNMENT) && !defined(PEDANTIC)
|
15 |
+
# define STRICT_ALIGNMENT 0
|
16 |
+
#endif
|
17 |
+
|
18 |
+
void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
|
19 |
+
size_t len, const void *key,
|
20 |
+
unsigned char ivec[16], block128_f block)
|
21 |
+
{
|
22 |
+
size_t n;
|
23 |
+
const unsigned char *iv = ivec;
|
24 |
+
|
25 |
+
if (len == 0)
|
26 |
+
return;
|
27 |
+
|
28 |
+
#if !defined(OPENSSL_SMALL_FOOTPRINT)
|
29 |
+
if (STRICT_ALIGNMENT &&
|
30 |
+
((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
|
31 |
+
while (len >= 16) {
|
32 |
+
for (n = 0; n < 16; ++n)
|
33 |
+
out[n] = in[n] ^ iv[n];
|
34 |
+
(*block) (out, out, key);
|
35 |
+
iv = out;
|
36 |
+
len -= 16;
|
37 |
+
in += 16;
|
38 |
+
out += 16;
|
39 |
+
}
|
40 |
+
} else {
|
41 |
+
while (len >= 16) {
|
42 |
+
for (n = 0; n < 16; n += sizeof(size_t))
|
43 |
+
*(size_t *)(out + n) =
|
44 |
+
*(size_t *)(in + n) ^ *(size_t *)(iv + n);
|
45 |
+
(*block) (out, out, key);
|
46 |
+
iv = out;
|
47 |
+
len -= 16;
|
48 |
+
in += 16;
|
49 |
+
out += 16;
|
50 |
+
}
|
51 |
+
}
|
52 |
+
#endif
|
53 |
+
while (len) {
|
54 |
+
for (n = 0; n < 16 && n < len; ++n)
|
55 |
+
out[n] = in[n] ^ iv[n];
|
56 |
+
for (; n < 16; ++n)
|
57 |
+
out[n] = iv[n];
|
58 |
+
(*block) (out, out, key);
|
59 |
+
iv = out;
|
60 |
+
if (len <= 16)
|
61 |
+
break;
|
62 |
+
len -= 16;
|
63 |
+
in += 16;
|
64 |
+
out += 16;
|
65 |
+
}
|
66 |
+
memcpy(ivec, iv, 16);
|
67 |
+
}
|
68 |
+
|
69 |
+
void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
|
70 |
+
size_t len, const void *key,
|
71 |
+
unsigned char ivec[16], block128_f block)
|
72 |
+
{
|
73 |
+
size_t n;
|
74 |
+
union {
|
75 |
+
size_t t[16 / sizeof(size_t)];
|
76 |
+
unsigned char c[16];
|
77 |
+
} tmp;
|
78 |
+
|
79 |
+
if (len == 0)
|
80 |
+
return;
|
81 |
+
|
82 |
+
#if !defined(OPENSSL_SMALL_FOOTPRINT)
|
83 |
+
if (in != out) {
|
84 |
+
const unsigned char *iv = ivec;
|
85 |
+
|
86 |
+
if (STRICT_ALIGNMENT &&
|
87 |
+
((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
|
88 |
+
while (len >= 16) {
|
89 |
+
(*block) (in, out, key);
|
90 |
+
for (n = 0; n < 16; ++n)
|
91 |
+
out[n] ^= iv[n];
|
92 |
+
iv = in;
|
93 |
+
len -= 16;
|
94 |
+
in += 16;
|
95 |
+
out += 16;
|
96 |
+
}
|
97 |
+
} else if (16 % sizeof(size_t) == 0) { /* always true */
|
98 |
+
while (len >= 16) {
|
99 |
+
size_t *out_t = (size_t *)out, *iv_t = (size_t *)iv;
|
100 |
+
|
101 |
+
(*block) (in, out, key);
|
102 |
+
for (n = 0; n < 16 / sizeof(size_t); n++)
|
103 |
+
out_t[n] ^= iv_t[n];
|
104 |
+
iv = in;
|
105 |
+
len -= 16;
|
106 |
+
in += 16;
|
107 |
+
out += 16;
|
108 |
+
}
|
109 |
+
}
|
110 |
+
memcpy(ivec, iv, 16);
|
111 |
+
} else {
|
112 |
+
if (STRICT_ALIGNMENT &&
|
113 |
+
((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
|
114 |
+
unsigned char c;
|
115 |
+
while (len >= 16) {
|
116 |
+
(*block) (in, tmp.c, key);
|
117 |
+
for (n = 0; n < 16; ++n) {
|
118 |
+
c = in[n];
|
119 |
+
out[n] = tmp.c[n] ^ ivec[n];
|
120 |
+
ivec[n] = c;
|
121 |
+
}
|
122 |
+
len -= 16;
|
123 |
+
in += 16;
|
124 |
+
out += 16;
|
125 |
+
}
|
126 |
+
} else if (16 % sizeof(size_t) == 0) { /* always true */
|
127 |
+
while (len >= 16) {
|
128 |
+
size_t c, *out_t = (size_t *)out, *ivec_t = (size_t *)ivec;
|
129 |
+
const size_t *in_t = (const size_t *)in;
|
130 |
+
|
131 |
+
(*block) (in, tmp.c, key);
|
132 |
+
for (n = 0; n < 16 / sizeof(size_t); n++) {
|
133 |
+
c = in_t[n];
|
134 |
+
out_t[n] = tmp.t[n] ^ ivec_t[n];
|
135 |
+
ivec_t[n] = c;
|
136 |
+
}
|
137 |
+
len -= 16;
|
138 |
+
in += 16;
|
139 |
+
out += 16;
|
140 |
+
}
|
141 |
+
}
|
142 |
+
}
|
143 |
+
#endif
|
144 |
+
while (len) {
|
145 |
+
unsigned char c;
|
146 |
+
(*block) (in, tmp.c, key);
|
147 |
+
for (n = 0; n < 16 && n < len; ++n) {
|
148 |
+
c = in[n];
|
149 |
+
out[n] = tmp.c[n] ^ ivec[n];
|
150 |
+
ivec[n] = c;
|
151 |
+
}
|
152 |
+
if (len <= 16) {
|
153 |
+
for (; n < 16; ++n)
|
154 |
+
ivec[n] = in[n];
|
155 |
+
break;
|
156 |
+
}
|
157 |
+
len -= 16;
|
158 |
+
in += 16;
|
159 |
+
out += 16;
|
160 |
+
}
|
161 |
+
}
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gaes_stream.cc
ADDED
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
#include "gaes_stream.h"
|
3 |
+
|
4 |
+
#include <cstring>
|
5 |
+
#include <iostream>
|
6 |
+
#include <fstream>
|
7 |
+
#include <vector>
|
8 |
+
#include <cstdio>
|
9 |
+
#include <cstdlib>
|
10 |
+
#include "gj_aes.h"
|
11 |
+
|
12 |
+
|
13 |
+
class GaesIStreamBuf final: public std::streambuf
|
14 |
+
{
|
15 |
+
private:
|
16 |
+
char *m_inbuf;
|
17 |
+
size_t m_inbufsize;
|
18 |
+
bool m_owns_inbuf;
|
19 |
+
char *m_leftbuf;
|
20 |
+
|
21 |
+
FILE *file;
|
22 |
+
uint64_t cur_size;
|
23 |
+
uint64_t file_size;
|
24 |
+
gj_aesc_t* aesc ;
|
25 |
+
protected:
|
26 |
+
virtual std::streambuf* setbuf(char *s, std::streamsize n){
|
27 |
+
setg(0, 0, 0);
|
28 |
+
if (m_owns_inbuf) {
|
29 |
+
delete [] m_inbuf;
|
30 |
+
}
|
31 |
+
m_inbufsize = n;
|
32 |
+
if (s) {
|
33 |
+
m_inbuf = s;
|
34 |
+
m_owns_inbuf = false;
|
35 |
+
} else {
|
36 |
+
m_inbuf = new char[m_inbufsize];
|
37 |
+
m_leftbuf = new char[m_inbufsize];
|
38 |
+
m_owns_inbuf = true;
|
39 |
+
}
|
40 |
+
return this;
|
41 |
+
}
|
42 |
+
|
43 |
+
virtual int sync(){
|
44 |
+
int result = 0;
|
45 |
+
return result;
|
46 |
+
}
|
47 |
+
|
48 |
+
virtual int underflow() override{
|
49 |
+
int __c = traits_type::eof();
|
50 |
+
if (!file) return __c;
|
51 |
+
if(cur_size>=file_size){
|
52 |
+
printf("===eof %ld ===%ld\n",cur_size,file_size);
|
53 |
+
return __c;
|
54 |
+
}
|
55 |
+
bool initial = false;
|
56 |
+
if (eback() == 0) {
|
57 |
+
setg(m_inbuf, m_inbuf + m_inbufsize, m_inbuf + m_inbufsize);
|
58 |
+
initial = true;
|
59 |
+
}
|
60 |
+
const size_t unget_sz = initial ? 0 : std::min<size_t>((egptr() - eback()) / 2, 4);
|
61 |
+
if (gptr() == egptr()) {
|
62 |
+
memmove(eback(), egptr() - unget_sz, unget_sz);
|
63 |
+
size_t nmemb = static_cast<size_t>(egptr() - eback() - unget_sz);
|
64 |
+
char* pdst = eback() + unget_sz;
|
65 |
+
int modb = nmemb % 16;
|
66 |
+
size_t leftb = nmemb - modb;
|
67 |
+
char* pbuf = m_leftbuf;
|
68 |
+
size_t leftf = file_size - cur_size;
|
69 |
+
if(leftb>leftf)leftb=leftf;
|
70 |
+
memset(pbuf,0,m_inbufsize);
|
71 |
+
size_t rd = fread(pbuf, 1, leftb, file);
|
72 |
+
//printf("%d-%ld-%ld----------------%ld--%ld#\n",cur_size,file_size,modb,nmemb,rd);
|
73 |
+
//ssize_t readed = read(m_fd, eback() + unget_sz, nmemb);
|
74 |
+
if(rd>0){
|
75 |
+
cur_size += rd;
|
76 |
+
int cnt = leftb /16;
|
77 |
+
int k;
|
78 |
+
for(k=0;k<cnt;k++){
|
79 |
+
int outlen = 0;
|
80 |
+
do_aesc(aesc,pbuf,16,pdst,&outlen);
|
81 |
+
pbuf += 16;
|
82 |
+
pdst += 16;
|
83 |
+
}
|
84 |
+
setg(eback(), eback() + unget_sz, eback() + unget_sz + rd);
|
85 |
+
__c = traits_type::to_int_type(*gptr());
|
86 |
+
}
|
87 |
+
} else {
|
88 |
+
__c = traits_type::to_int_type(*gptr());
|
89 |
+
}
|
90 |
+
return __c;
|
91 |
+
}
|
92 |
+
public:
|
93 |
+
GaesIStreamBuf(std::string& filename) :m_inbuf(0), m_inbufsize(0), m_owns_inbuf(false){
|
94 |
+
setbuf(0, 1024);
|
95 |
+
cur_size = 0;
|
96 |
+
file = fopen(filename.c_str(), "rb");
|
97 |
+
fseek(file, 0, SEEK_END);
|
98 |
+
file_size = ftell(file); //获取音频文件大小
|
99 |
+
fseek(file, 0, SEEK_SET);
|
100 |
+
char* key = "yymrjzbwyrbjszrk";
|
101 |
+
char* aiv = "yymrjzbwyrbjszrk";
|
102 |
+
init_aesc(key,aiv,0,&this->aesc);
|
103 |
+
char head[50];
|
104 |
+
memset(head,0,50);
|
105 |
+
uint64_t rst = fread(head,1,8,file);
|
106 |
+
rst = fread(&cur_size,1,8,file);
|
107 |
+
printf("===head %s size %ld\n",head,cur_size);
|
108 |
+
rst = fread(head,1,16,file);
|
109 |
+
cur_size = 32;
|
110 |
+
}
|
111 |
+
|
112 |
+
~GaesIStreamBuf(){
|
113 |
+
close();
|
114 |
+
if (m_owns_inbuf) {
|
115 |
+
delete[] m_inbuf;
|
116 |
+
}
|
117 |
+
}
|
118 |
+
|
119 |
+
void close(){
|
120 |
+
if(aesc){
|
121 |
+
free_aesc(&this->aesc);
|
122 |
+
}
|
123 |
+
if (file){
|
124 |
+
fclose(file);
|
125 |
+
file = NULL;
|
126 |
+
}
|
127 |
+
}
|
128 |
+
};
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
GaesIStream::GaesIStream(std::string filename):
|
133 |
+
std::istream(new GaesIStreamBuf(filename)){
|
134 |
+
}
|
135 |
+
|
136 |
+
GaesIStream::~GaesIStream()
|
137 |
+
{
|
138 |
+
delete rdbuf();
|
139 |
+
}
|
140 |
+
|
141 |
+
#ifdef TEST
|
142 |
+
int maindec(int argc,char** argv){
|
143 |
+
std::string filename(argv[1]);// = "test.enc";
|
144 |
+
//std::string filename = "final.mdlenc";
|
145 |
+
GaesIStream fin(filename);
|
146 |
+
//std::string fn2 = "final.mdldec";
|
147 |
+
std::string fn2(argv[2]);// = "test.dec";
|
148 |
+
std::ofstream fout(fn2,std::ios::binary);
|
149 |
+
|
150 |
+
char buf[1024];
|
151 |
+
int rd = 0;
|
152 |
+
while(!fin.eof()){
|
153 |
+
//while((rd = fin.read(buf,16))>0){
|
154 |
+
//printf("===rd %ld\n",rd);
|
155 |
+
fin.read(buf,16);
|
156 |
+
fout.write(buf,16);
|
157 |
+
|
158 |
+
}
|
159 |
+
//char ch;
|
160 |
+
//while (fin.get(ch)) {
|
161 |
+
//printf("+");
|
162 |
+
//fout << ch;
|
163 |
+
//}
|
164 |
+
return 0;
|
165 |
+
}
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
int mainenc(int argc,char** argv){
|
170 |
+
char result[255] ;
|
171 |
+
memset(result,0,255);
|
172 |
+
char* key = "yymrjzbwyrbjszrk";
|
173 |
+
char* aiv = "yymrjzbwyrbjszrk";
|
174 |
+
int base64 = 1;
|
175 |
+
int outlen = 0;
|
176 |
+
gj_aesc_t* aesc = NULL;
|
177 |
+
init_aesc(key,aiv,1,&aesc);
|
178 |
+
char* fn1 = argv[1];
|
179 |
+
char* fn2 = argv[2];
|
180 |
+
FILE* fr = fopen(fn1,"rb");
|
181 |
+
FILE* fw = fopen(fn2,"wb");
|
182 |
+
fwrite("abcdefgh",1,8,fw);
|
183 |
+
uint64_t size = 0;
|
184 |
+
fwrite(&size,1,8,fw);
|
185 |
+
fwrite(&size,1,8,fw);
|
186 |
+
fwrite(&size,1,8,fw);
|
187 |
+
while(!feof(fr)){
|
188 |
+
char data[16];
|
189 |
+
memset(data,0,16);
|
190 |
+
uint64_t rst = fread(data,1,16,fr);
|
191 |
+
printf("===rst %d\n",rst);
|
192 |
+
if(rst){
|
193 |
+
size +=rst;
|
194 |
+
do_aesc(aesc,data,16,result,&outlen);
|
195 |
+
printf("===out %d\n",outlen);
|
196 |
+
fwrite(result,1,16,fw);
|
197 |
+
}
|
198 |
+
}
|
199 |
+
fseek(fw,8,0);
|
200 |
+
fwrite(&size,1,8,fw);
|
201 |
+
fclose(fr);
|
202 |
+
fclose(fw);
|
203 |
+
return 0;
|
204 |
+
}
|
205 |
+
|
206 |
+
int main(int argc,char** argv){
|
207 |
+
if(argc<4){
|
208 |
+
return mainenc(argc,argv);
|
209 |
+
}else{
|
210 |
+
return maindec(argc,argv);
|
211 |
+
}
|
212 |
+
}
|
213 |
+
#endif
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gaes_stream.h
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef COMPRESSED_STREAMS_ZSTD_STREAM_H
|
2 |
+
#define COMPRESSED_STREAMS_ZSTD_STREAM_H
|
3 |
+
|
4 |
+
#include <iostream>
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
class GaesIStream: public std::istream
|
11 |
+
{
|
12 |
+
public:
|
13 |
+
GaesIStream(std::string filename);
|
14 |
+
|
15 |
+
virtual ~GaesIStream();
|
16 |
+
};
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
#endif // COMPRESSED_STREAMS_ZSTD_STREAM_H
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gaesmain
ADDED
Binary file (64.3 kB). View file
|
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gj_aes.c
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <stdlib.h>
|
2 |
+
#include <string.h>
|
3 |
+
#include "gj_aes.h"
|
4 |
+
#include "base64.h"
|
5 |
+
|
6 |
+
#include "aes.h"
|
7 |
+
|
8 |
+
|
9 |
+
struct gj_aesc_s{
|
10 |
+
char key[16];
|
11 |
+
char iv[16];
|
12 |
+
int enc;
|
13 |
+
AES_KEY *aeskey;
|
14 |
+
};
|
15 |
+
|
16 |
+
int free_aesc(gj_aesc_t** paesc){
|
17 |
+
if(!paesc||!*paesc)return -1;
|
18 |
+
if((*paesc)->aeskey)free((*paesc)->aeskey);
|
19 |
+
free(*paesc);
|
20 |
+
*paesc = NULL;
|
21 |
+
return 0;
|
22 |
+
}
|
23 |
+
|
24 |
+
|
25 |
+
int init_aesc(char* key,char* iv,int enc,gj_aesc_t** paesc){
|
26 |
+
if(strlen(key)!=16) return -1;
|
27 |
+
if(strlen(iv)!=16) return -2;
|
28 |
+
gj_aesc_t* aesc = (gj_aesc_t*)malloc(sizeof(gj_aesc_t));
|
29 |
+
int k;
|
30 |
+
for(k=0;k<16;k++){
|
31 |
+
aesc->key[k]=key[k];
|
32 |
+
aesc->iv[k]=iv[k];
|
33 |
+
}
|
34 |
+
aesc->aeskey = (AES_KEY*)malloc(sizeof(AES_KEY));
|
35 |
+
aesc->enc = enc;
|
36 |
+
if(enc){
|
37 |
+
AES_set_encrypt_key((const unsigned char*)aesc->key, 128, aesc->aeskey);
|
38 |
+
}else{
|
39 |
+
AES_set_decrypt_key((const unsigned char*)aesc->key, 128, aesc->aeskey);
|
40 |
+
}
|
41 |
+
*paesc = aesc;
|
42 |
+
return 0;
|
43 |
+
}
|
44 |
+
|
45 |
+
int do_aesc(gj_aesc_t* aesc,char* in,int inlen,char* out,int* outlen){
|
46 |
+
char* psrc = in;
|
47 |
+
char* pdest = out;
|
48 |
+
int cnt = 0;
|
49 |
+
int left=inlen;
|
50 |
+
while(left>0){
|
51 |
+
AES_cbc_encrypt((const unsigned char*)psrc,(unsigned char*)pdest,16,aesc->aeskey,(unsigned char*)aesc->iv,aesc->enc);
|
52 |
+
psrc += 16;
|
53 |
+
pdest += 16;
|
54 |
+
left -= 16;
|
55 |
+
cnt += 16;
|
56 |
+
}
|
57 |
+
*outlen = cnt;
|
58 |
+
return 0;
|
59 |
+
}
|
60 |
+
|
61 |
+
int do_base64(int enc,char* in,int inlen,char* out,int* outlen){
|
62 |
+
if(enc){
|
63 |
+
gjbase64_encode((unsigned char*)in,inlen,out);
|
64 |
+
*outlen = strlen(out);
|
65 |
+
}else{
|
66 |
+
*outlen = gjbase64_decode(in,inlen,(unsigned char*)out);
|
67 |
+
}
|
68 |
+
return 0;
|
69 |
+
}
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gj_aes.h
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef __GJ_AES_H__
|
2 |
+
#define __GJ_AES_H__
|
3 |
+
|
4 |
+
#include "gj_dll.h"
|
5 |
+
#ifdef __cplusplus
|
6 |
+
extern "C" {
|
7 |
+
#endif
|
8 |
+
|
9 |
+
typedef struct gj_aesc_s gj_aesc_t;
|
10 |
+
|
11 |
+
GJLIBAPI int free_aesc(gj_aesc_t** paesc);
|
12 |
+
GJLIBAPI int init_aesc(char* key,char* iv,int enc,gj_aesc_t** paesc);
|
13 |
+
|
14 |
+
GJLIBAPI int do_aesc(gj_aesc_t* aesc,char* in,int inlen,char* out,int* outlen);
|
15 |
+
|
16 |
+
GJLIBAPI int do_base64(int enc,char* in,int inlen,char* out,int* outlen);
|
17 |
+
|
18 |
+
#ifdef __cplusplus
|
19 |
+
}
|
20 |
+
#endif
|
21 |
+
|
22 |
+
#endif
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/gj_dll.h
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef __GJ_DLL_H__
|
2 |
+
#define __GJ_DLL_H__
|
3 |
+
|
4 |
+
#ifdef __cplusplus
|
5 |
+
extern "C" {
|
6 |
+
#endif
|
7 |
+
#define GJLIB_EXPORT 1
|
8 |
+
#if defined(GJLIB_EXPORT)
|
9 |
+
#if defined _WIN32 || defined __CYGWIN__
|
10 |
+
#define GJLIBAPI __declspec(dllexport)
|
11 |
+
#else
|
12 |
+
#define GJLIBAPI __attribute__((visibility("default")))
|
13 |
+
#endif
|
14 |
+
#else
|
15 |
+
#define GJLIBAPI
|
16 |
+
#endif
|
17 |
+
|
18 |
+
#ifdef __cplusplus
|
19 |
+
}
|
20 |
+
#endif
|
21 |
+
#endif
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/makefile
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
all:
|
2 |
+
g++ -fPIC -o gjaesmain -g aesmain.c \
|
3 |
+
aes_cbc.c aes_core.c aes_ecb.c cbc128.c base64.c gj_aes.c -lm --std=c++11 -I. -DTEST
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aes/modes.h
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef HEADER_MODES_H
|
2 |
+
# define HEADER_MODES_H
|
3 |
+
|
4 |
+
# include <stddef.h>
|
5 |
+
|
6 |
+
typedef void (*block128_f) (const unsigned char in[16],
|
7 |
+
unsigned char out[16], const void *key);
|
8 |
+
|
9 |
+
typedef void (*cbc128_f) (const unsigned char *in, unsigned char *out,
|
10 |
+
size_t len, const void *key,
|
11 |
+
unsigned char ivec[16], int enc);
|
12 |
+
|
13 |
+
void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
|
14 |
+
size_t len, const void *key,
|
15 |
+
unsigned char ivec[16], block128_f block);
|
16 |
+
void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
|
17 |
+
size_t len, const void *key,
|
18 |
+
unsigned char ivec[16], block128_f block);
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
#endif
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/aicommon.h
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
//#define MFCC_OFFSET 6436
|
4 |
+
#define MFCC_OFFSET 6400
|
5 |
+
//##define MFCC_OFFSET 0
|
6 |
+
#define MFCC_DEFRMS 0.1f
|
7 |
+
#define MFCC_FPS 25
|
8 |
+
#define MFCC_RATE 16000
|
9 |
+
//#define MFCC_WAVCHUNK 960000
|
10 |
+
#define MFCC_WAVCHUNK 560000
|
11 |
+
//#define MFCC_WAVCHUNK 512
|
12 |
+
|
13 |
+
//#define MFCC_MELBASE 6001
|
14 |
+
#define MFCC_MELBASE 3501
|
15 |
+
#define MFCC_MELCHUNK 80
|
16 |
+
//#define MFCC_MELCHUNK 20
|
17 |
+
|
18 |
+
//#define MFCC_BNFBASE 1499
|
19 |
+
#define MFCC_BNFBASE 874
|
20 |
+
#define MFCC_BNFCHUNK 256
|
21 |
+
//input==== NodeArg(name='speech', type='tensor(float)', shape=['B', 'T', 80])
|
22 |
+
//input==== NodeArg(name='speech_lengths', type='tensor(int32)', shape=['B'])
|
23 |
+
//output==== NodeArg(name='encoder_out', type='tensor(float)', shape=['B', 'T_OUT', 'Addencoder_out_dim_2'])
|
24 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/aimodel.cpp
ADDED
@@ -0,0 +1,438 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include "aimodel.h"
|
2 |
+
#include <stdlib.h>
|
3 |
+
#include <string.h>
|
4 |
+
|
5 |
+
void AiCfg::dump(){
|
6 |
+
int incnt = size_inputs.size();
|
7 |
+
int outcnt = size_outputs.size();
|
8 |
+
std::cout<<"======in onnx:"<<incnt<<std::endl;
|
9 |
+
//std::cout<<"======in onnx:"<<name_inputs.size()<<std::endl;
|
10 |
+
for(int k=0;k<incnt;k++){
|
11 |
+
//std::string sname(name_inputs[k]);
|
12 |
+
//std::cout<<"in name:"<<sname<<std::endl;
|
13 |
+
std::cout<<"size input:"<<size_inputs[k]<<std::endl;
|
14 |
+
for(int m=0;m<shape_inputs[k].size();m++)
|
15 |
+
std::cout<<"shape :"<<shape_inputs[k][m]<<std::endl;
|
16 |
+
std::cout<<"kind input:"<<kind_inputs[k]<<std::endl;
|
17 |
+
}
|
18 |
+
std::cout<<"=========out onnx:"<<outcnt<<std::endl;
|
19 |
+
//std::cout<<"======in onnx:"<<name_outputs.size()<<std::endl;
|
20 |
+
for(int k=0;k<outcnt;k++){
|
21 |
+
//std::string sname(name_outputs[k]);
|
22 |
+
//std::cout<<"out name:"<<sname<<std::endl;
|
23 |
+
std::cout<<"size output:"<<size_outputs[k]<<std::endl;
|
24 |
+
for(int m=0;m<shape_outputs[k].size();m++)
|
25 |
+
std::cout<<"shape :"<<shape_outputs[k][m]<<std::endl;
|
26 |
+
std::cout<<"kind outintpu:"<<kind_outputs[k]<<std::endl;
|
27 |
+
}
|
28 |
+
}
|
29 |
+
|
30 |
+
int AiCfg::inShape(int inx,int dim,int val){
|
31 |
+
if(inx>=shape_inputs.size())return -1;
|
32 |
+
if(dim>=shape_inputs[inx].size())return -2;
|
33 |
+
shape_inputs[inx][dim] = val;
|
34 |
+
int size = 1;
|
35 |
+
for(int k=0;k<shape_inputs[inx].size();k++){
|
36 |
+
size *= shape_inputs[inx][dim];
|
37 |
+
}
|
38 |
+
if(size>0){
|
39 |
+
size_inputs[inx] = size;
|
40 |
+
}
|
41 |
+
return 0;
|
42 |
+
}
|
43 |
+
|
44 |
+
int AiCfg::outShape(int inx,int dim,int val){
|
45 |
+
if(inx>=shape_outputs.size())return -1;
|
46 |
+
if(dim>=shape_outputs[inx].size())return -2;
|
47 |
+
shape_outputs[inx][dim] = val;
|
48 |
+
int size = 1;
|
49 |
+
for(int k=0;k<shape_outputs[inx].size();k++){
|
50 |
+
size *= shape_outputs[inx][dim];
|
51 |
+
}
|
52 |
+
if(size>0){
|
53 |
+
size_outputs[inx] = size;
|
54 |
+
}
|
55 |
+
return 0;
|
56 |
+
}
|
57 |
+
|
58 |
+
AiCfg AiCfg::clone(){
|
59 |
+
AiCfg onecfg;
|
60 |
+
AiCfg* cfg = &onecfg;//new AiCfg();
|
61 |
+
//
|
62 |
+
cfg->names.assign(names.begin(),names.end());
|
63 |
+
cfg->kind_inputs.assign(kind_inputs.begin(),kind_inputs.end());
|
64 |
+
cfg->name_inputs.assign(name_inputs.begin(),name_inputs.end());
|
65 |
+
cfg->size_inputs.assign(size_inputs.begin(),size_inputs.end());
|
66 |
+
//cfg->m_sizeinput = m_sizeinput;
|
67 |
+
for(int k=0;k<shape_inputs.size();k++){
|
68 |
+
std::vector<int64_t> shape(shape_inputs[k]);
|
69 |
+
cfg->shape_inputs.push_back(shape);
|
70 |
+
}
|
71 |
+
|
72 |
+
cfg->kind_outputs.assign(kind_outputs.begin(),kind_outputs.end());
|
73 |
+
cfg->name_outputs.assign(name_outputs.begin(),name_outputs.end());
|
74 |
+
cfg->size_outputs.assign(size_outputs.begin(),size_outputs.end());
|
75 |
+
//cfg->m_sizeoutput = m_sizeoutput;
|
76 |
+
for(int k=0;k<shape_outputs.size();k++){
|
77 |
+
std::vector<int64_t> shape(shape_outputs[k]);
|
78 |
+
cfg->shape_outputs.push_back(shape);
|
79 |
+
}
|
80 |
+
return onecfg;//cfg;
|
81 |
+
}
|
82 |
+
|
83 |
+
AiModel::AiModel(){
|
84 |
+
m_cfg = new AiCfg();
|
85 |
+
}
|
86 |
+
|
87 |
+
AiModel::~AiModel(){
|
88 |
+
delete m_cfg;
|
89 |
+
}
|
90 |
+
|
91 |
+
int AiModel::doInitModel(){
|
92 |
+
return -1;
|
93 |
+
}
|
94 |
+
|
95 |
+
AiCfg AiModel::config(){
|
96 |
+
return m_cfg->clone();
|
97 |
+
}
|
98 |
+
|
99 |
+
void AiModel::dump(){
|
100 |
+
m_cfg->dump();
|
101 |
+
}
|
102 |
+
|
103 |
+
int AiModel::pushName(const char* name,int input){
|
104 |
+
std::string sname(name);
|
105 |
+
m_cfg->names.push_back(sname);
|
106 |
+
if(input){
|
107 |
+
m_cfg->name_inputs.push_back(m_cfg->names[m_cfg->names.size()-1].c_str());
|
108 |
+
}else{
|
109 |
+
m_cfg->name_outputs.push_back(m_cfg->names[m_cfg->names.size()-1].c_str());
|
110 |
+
}
|
111 |
+
return 0;
|
112 |
+
}
|
113 |
+
|
114 |
+
int AiModel::initModel(std::string& modelpath){
|
115 |
+
m_modelPath = modelpath;
|
116 |
+
m_inited = doInitModel();
|
117 |
+
printf("===init %d\n",m_inited);
|
118 |
+
|
119 |
+
return m_inited;
|
120 |
+
}
|
121 |
+
|
122 |
+
int AiModel::initModel(std::string& binfn,std::string& paramfn){
|
123 |
+
m_modelbin = binfn;
|
124 |
+
m_modelparam = paramfn;
|
125 |
+
m_inited = doInitModel();
|
126 |
+
//printf("===init %d\n",m_inited);
|
127 |
+
|
128 |
+
return m_inited;
|
129 |
+
}
|
130 |
+
|
131 |
+
int AiModel::doRunModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg){
|
132 |
+
return 0;
|
133 |
+
}
|
134 |
+
|
135 |
+
|
136 |
+
int AiModel::runModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg){
|
137 |
+
if(m_inited){
|
138 |
+
//std::cout<<"model not inited:"<<m_inited<<std::endl;
|
139 |
+
return -999;
|
140 |
+
}
|
141 |
+
return doRunModel(arrin,arrout,stream,pcfg);
|
142 |
+
}
|
143 |
+
|
144 |
+
OnnxModel::OnnxModel(){
|
145 |
+
}
|
146 |
+
|
147 |
+
OnnxModel::OnnxModel(int b,int w,int h):AiModel(){
|
148 |
+
m_batch = b;
|
149 |
+
m_width = w;
|
150 |
+
m_height = h;
|
151 |
+
}
|
152 |
+
|
153 |
+
OnnxModel::~OnnxModel(){
|
154 |
+
}
|
155 |
+
|
156 |
+
int OnnxModel::doInitModel(){
|
157 |
+
env = Ort::Env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING, "ONNX");
|
158 |
+
sessionOptions = Ort::SessionOptions();
|
159 |
+
//sessionOptions.AddConfigEntry("session.load_model_format","ORT");
|
160 |
+
//std::vector<std::string> availableProviders = Ort::GetAvailableProviders();
|
161 |
+
//auto cudaAvailable = std::find(availableProviders.begin(), availableProviders.end(), "CUDAExecutionProvider");
|
162 |
+
//OrtCUDAProviderOptions cudaOption;
|
163 |
+
|
164 |
+
//if(cudaAvailable != availableProviders.end()){
|
165 |
+
//std::cout << "Inference device: GPU" << std::endl;
|
166 |
+
//sessionOptions.AppendExecutionProvider_CUDA(cudaOption);
|
167 |
+
//}else{
|
168 |
+
//std::cout << "Inference device: CPU" << std::endl;
|
169 |
+
//}
|
170 |
+
|
171 |
+
session = Ort::Session(env, m_modelPath.c_str(), sessionOptions);
|
172 |
+
//Ort::AllocatorWithDefaultOptions allocator;
|
173 |
+
size_t numInputNodes = session.GetInputCount();
|
174 |
+
size_t numOutputNodes = session.GetOutputCount();
|
175 |
+
//std::cout << "input NUM: " << numInputNodes << std::endl;
|
176 |
+
//std::cout << "Output NUM: " << numOutputNodes << std::endl;
|
177 |
+
for(int k=0;k<numInputNodes;k++){
|
178 |
+
//m_cfg->name_inputs.push_back(session.GetInputName(k));//, allocator));
|
179 |
+
Ort::TypeInfo inputTypeInfo = session.GetInputTypeInfo(k);
|
180 |
+
auto tensorInfo = inputTypeInfo.GetTensorTypeAndShapeInfo();
|
181 |
+
auto elemType = tensorInfo.GetElementType();
|
182 |
+
m_cfg->kind_inputs.push_back((int)elemType);
|
183 |
+
std::vector<int64_t> inputTensorShape = tensorInfo.GetShape();
|
184 |
+
|
185 |
+
if(m_batch&&inputTensorShape.size()){
|
186 |
+
if(inputTensorShape[0]==-1)inputTensorShape[0]=m_batch;
|
187 |
+
}
|
188 |
+
if(m_batch&&(inputTensorShape.size()==4)){
|
189 |
+
if(inputTensorShape[0]==-1)inputTensorShape[0]=m_batch;
|
190 |
+
if(inputTensorShape[2]==-1)inputTensorShape[2]=m_height;
|
191 |
+
if(inputTensorShape[3]==-1)inputTensorShape[3]=m_width;
|
192 |
+
}
|
193 |
+
int size = 1;
|
194 |
+
for (auto shape : inputTensorShape){
|
195 |
+
size *= shape;
|
196 |
+
}
|
197 |
+
m_cfg->shape_inputs.push_back(inputTensorShape);
|
198 |
+
m_cfg->size_inputs.push_back(size);
|
199 |
+
}
|
200 |
+
|
201 |
+
for(int k=0;k<numOutputNodes;k++){
|
202 |
+
//m_cfg->name_outputs.push_back(session.GetOutputName(k));//, allocator));
|
203 |
+
Ort::TypeInfo outputTypeInfo = session.GetOutputTypeInfo(k);
|
204 |
+
auto tensorInfo = outputTypeInfo.GetTensorTypeAndShapeInfo();
|
205 |
+
auto elemType = tensorInfo.GetElementType();
|
206 |
+
m_cfg->kind_outputs.push_back((int)elemType);
|
207 |
+
std::vector<int64_t> outputTensorShape = tensorInfo.GetShape();
|
208 |
+
if(m_batch&&outputTensorShape.size()){
|
209 |
+
if(outputTensorShape[0]==-1)outputTensorShape[0]=m_batch;
|
210 |
+
}
|
211 |
+
if(m_batch&&(outputTensorShape.size()==4)){
|
212 |
+
if(outputTensorShape[0]==-1)outputTensorShape[0]=m_batch;
|
213 |
+
if(outputTensorShape[2]==-1)outputTensorShape[2]=m_height;
|
214 |
+
if(outputTensorShape[3]==-1)outputTensorShape[3]=m_width;
|
215 |
+
}
|
216 |
+
int size = 1;
|
217 |
+
for (auto shape : outputTensorShape){
|
218 |
+
size *= shape;
|
219 |
+
}
|
220 |
+
m_cfg->shape_outputs.push_back(outputTensorShape);
|
221 |
+
m_cfg->size_outputs.push_back(size);
|
222 |
+
}
|
223 |
+
|
224 |
+
return 0;
|
225 |
+
}
|
226 |
+
|
227 |
+
|
228 |
+
int OnnxModel::doRunModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg){
|
229 |
+
AiCfg* cfg = pcfg==nullptr?m_cfg:pcfg;
|
230 |
+
int incnt = cfg->size_inputs.size();
|
231 |
+
int outcnt = cfg->size_outputs.size();
|
232 |
+
std::cout<<"run onnx:"<<outcnt<<std::endl;
|
233 |
+
if(!arrin || !arrout)return -1;
|
234 |
+
std::vector<Ort::Value> inputTensors;
|
235 |
+
Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu( OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);
|
236 |
+
for(int k=0;k<incnt;k++){
|
237 |
+
inputTensors.push_back(Ort::Value::CreateTensor( memoryInfo, arrin[k] ,cfg->size_inputs[k]*4 , cfg->shape_inputs[k].data(), cfg->shape_inputs[k].size(), (ONNXTensorElementDataType)cfg->kind_inputs[k] ));
|
238 |
+
}
|
239 |
+
std::vector<Ort::Value> outputTensors;
|
240 |
+
for(int k=0;k<outcnt;k++){
|
241 |
+
outputTensors.push_back(Ort::Value::CreateTensor( memoryInfo, arrout[k] ,cfg->size_outputs[k]*4 , cfg->shape_outputs[k].data(), cfg->shape_outputs[k].size(),(ONNXTensorElementDataType)cfg->kind_outputs[k] ));
|
242 |
+
}
|
243 |
+
this->session.Run(Ort::RunOptions{nullptr}, cfg->names_in, inputTensors.data(), incnt, cfg->names_out, outputTensors.data(),outcnt);
|
244 |
+
if(1)return 0;
|
245 |
+
/*
|
246 |
+
//for(int k=0;k<9;k++)dumpfloat((float*)arrout[k],10);//size_outputs[0]);
|
247 |
+
std::vector<Ort::Value> aoutputTensors = this->session.Run(Ort::RunOptions{nullptr}, name_inputs.data(), inputTensors.data(), 1, name_outputs.data(), 9);
|
248 |
+
//bool* pmsk = (bool*)aoutputTensors[1].GetTensorData<bool>();
|
249 |
+
for(int k=0;k<9;k++){
|
250 |
+
float* pbnf = (float*)aoutputTensors[0].GetTensorData<float>();
|
251 |
+
memcpy(arrout[k],pbnf,size_outputs[k]*4);
|
252 |
+
}
|
253 |
+
*/
|
254 |
+
return 0;
|
255 |
+
}
|
256 |
+
|
257 |
+
NcnnModel::NcnnModel():AiModel(){
|
258 |
+
}
|
259 |
+
|
260 |
+
NcnnModel::NcnnModel(int w,int h):AiModel(){
|
261 |
+
m_width = w;
|
262 |
+
m_height = h;
|
263 |
+
}
|
264 |
+
|
265 |
+
NcnnModel::~NcnnModel(){
|
266 |
+
net.clear();
|
267 |
+
}
|
268 |
+
|
269 |
+
int NcnnModel::doInitModel(){
|
270 |
+
net.clear();
|
271 |
+
net.load_param(m_modelparam.c_str());
|
272 |
+
net.load_model(m_modelbin.c_str());
|
273 |
+
return 0; //
|
274 |
+
}
|
275 |
+
|
276 |
+
int NcnnModel::doRunModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg){
|
277 |
+
ncnn::Extractor ex = net.create_extractor();
|
278 |
+
AiCfg* cfg = pcfg==nullptr?m_cfg:pcfg;
|
279 |
+
int incnt = cfg->size_inputs.size();
|
280 |
+
int outcnt = cfg->size_outputs.size();
|
281 |
+
ncnn::Mat inmat[incnt];
|
282 |
+
for(int k=0;k<incnt;k++){
|
283 |
+
std::string name = cfg->name_inputs[k];
|
284 |
+
JMat* mat = (JMat*)arrin[k];
|
285 |
+
ncnn::Mat in_pack =mat->packingmat();
|
286 |
+
ex.input(name.c_str(), in_pack);
|
287 |
+
}
|
288 |
+
for(int k=0;k<outcnt;k++){
|
289 |
+
std::string name = cfg->name_outputs[k];
|
290 |
+
JMat* mat = (JMat*)arrout[k];
|
291 |
+
ncnn::Mat output;
|
292 |
+
ex.extract(name.c_str(), output);
|
293 |
+
ncnn::Mat in_park;
|
294 |
+
ncnn::convert_packing(output,in_park,3);
|
295 |
+
int size = mat->width()*mat->height()*3*sizeof(float);
|
296 |
+
memcpy((uint8_t*)mat->data(),in_park,size);
|
297 |
+
}
|
298 |
+
return 0;
|
299 |
+
}
|
300 |
+
|
301 |
+
#ifdef _TENSORRT_
|
302 |
+
#include "NvInferPlugin.h"
|
303 |
+
#include "cuda_runtime_api.h"
|
304 |
+
#include "cuda.h"
|
305 |
+
#define FPW 4
|
306 |
+
using namespace nvinfer1;
|
307 |
+
|
308 |
+
int TrtModel::doInitModel(){
|
309 |
+
std::cout<<"deserialize eng !"<<m_modelPath<<std::endl;
|
310 |
+
bool didInitPlugins = initLibNvInferPlugins(nullptr, "");
|
311 |
+
std::ifstream cache(m_modelPath,std::ios::binary);
|
312 |
+
cache.seekg(0,std::ios::end);
|
313 |
+
int engSize = cache.tellg();
|
314 |
+
if(!engSize)return -1;
|
315 |
+
cache.seekg(0,std::ios::beg);
|
316 |
+
void *modelMem = malloc(engSize);
|
317 |
+
if(!modelMem)return -2;
|
318 |
+
cache.read((char*)modelMem,engSize);
|
319 |
+
cache.close();
|
320 |
+
std::cout<<"deserialize size!"<<engSize<<std::endl;
|
321 |
+
|
322 |
+
IRuntime *runtime = nvinfer1::createInferRuntime(m_logger);
|
323 |
+
m_engine = runtime->deserializeCudaEngine(modelMem,engSize);
|
324 |
+
runtime->destroy();
|
325 |
+
free(modelMem);
|
326 |
+
if(! m_engine){
|
327 |
+
std::cout<<"deserialize eng error!"<<std::endl;
|
328 |
+
return -10;
|
329 |
+
}
|
330 |
+
|
331 |
+
std::cout<<"aaa"<<std::endl;
|
332 |
+
m_context = m_engine->createExecutionContext();
|
333 |
+
int bindings = m_engine->getNbBindings();
|
334 |
+
std::cout<<"bindings!"<<bindings<<std::endl;
|
335 |
+
for(int k=0;k<bindings;k++){
|
336 |
+
const char* name = m_engine->getBindingName(k);
|
337 |
+
std::string sname(name );
|
338 |
+
bool input = m_engine->bindingIsInput(k);
|
339 |
+
std::cout<<"name!"<<sname<<"===input"<<input<<std::endl;
|
340 |
+
Dims dims = m_engine->getBindingDimensions(k);
|
341 |
+
auto dt = m_engine->getBindingDataType(k);
|
342 |
+
int tdt = dt==DataType::kFLOAT?ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32;
|
343 |
+
std::vector<int64_t> tensorShape;
|
344 |
+
int dsize = 1;
|
345 |
+
for(int i = 0 ;i < dims.nbDims; i++){
|
346 |
+
tensorShape.push_back(dims.d[i]);
|
347 |
+
dsize *= dims.d[i];
|
348 |
+
//cout<<"==dsize!"<<dims.d[i]<<endl;
|
349 |
+
}
|
350 |
+
//cout<<"==========dsize!"<<dsize<<endl;
|
351 |
+
|
352 |
+
int size = dsize*FPW;//size_list[k];
|
353 |
+
if(input){
|
354 |
+
m_cfg->kind_inputs.push_back(tdt);
|
355 |
+
m_cfg->shape_inputs.push_back(tensorShape);
|
356 |
+
m_cfg->size_inputs.push_back(size);
|
357 |
+
m_cfg->name_inputs.push_back(name);
|
358 |
+
}else{
|
359 |
+
m_cfg->kind_outputs.push_back(tdt);
|
360 |
+
m_cfg->size_outputs.push_back(size);
|
361 |
+
m_cfg->name_outputs.push_back(name);
|
362 |
+
m_cfg->shape_outputs.push_back(tensorShape);
|
363 |
+
}
|
364 |
+
}
|
365 |
+
//getchar();
|
366 |
+
/*
|
367 |
+
m_sizeinput = 0;
|
368 |
+
for(int k=0;k<m_cfg->size_inputs.size();k++){
|
369 |
+
int size = m_cfg->size_inputs[k];
|
370 |
+
void* input ;
|
371 |
+
//int flag = cudaMalloc(&input,size);
|
372 |
+
//m_inputs.push_back(input);
|
373 |
+
//m_bindings[k]=input;
|
374 |
+
m_sizeinput += size;
|
375 |
+
//printf("===input %d output %d \n",m_sizeinput,m_sizeoutput);
|
376 |
+
}
|
377 |
+
std::cout<<"bbb"<<std::endl;
|
378 |
+
m_sizeoutput = 0;
|
379 |
+
int offset = m_cfg->size_inputs.size();
|
380 |
+
for(int k=0;k<m_cfg->size_outputs.size();k++){
|
381 |
+
int size = m_cfg->size_outputs[k];
|
382 |
+
void* output ;
|
383 |
+
//int flag = cudaMalloc(&output,size);
|
384 |
+
//m_outputs.push_back(output);
|
385 |
+
//m_bindings[offset+k]=output;
|
386 |
+
m_sizeoutput += size;
|
387 |
+
//printf("===input %d output %d \n",m_sizeinput,m_sizeoutput);
|
388 |
+
}
|
389 |
+
std::cout<<"ccc"<<std::endl;
|
390 |
+
*/
|
391 |
+
return 0;
|
392 |
+
}
|
393 |
+
|
394 |
+
int TrtModel::doRunModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg){
|
395 |
+
AiCfg* cfg = pcfg==nullptr?m_cfg:pcfg;
|
396 |
+
int incnt = cfg->size_inputs.size();
|
397 |
+
int outcnt = cfg->size_outputs.size();
|
398 |
+
std::cout<<"incnt:"<<incnt<<"==outcnt:"<<outcnt<<std::endl;
|
399 |
+
std::vector<void*> m_bindings(incnt+outcnt,NULL);
|
400 |
+
for(int k=0;k<incnt;k++){
|
401 |
+
std::cout<<"in:"<<cfg->size_inputs[k]<<std::endl;
|
402 |
+
m_bindings[k] = arrin[k];
|
403 |
+
}
|
404 |
+
for(int k=0;k<outcnt;k++){
|
405 |
+
std::cout<<"out:"<<cfg->size_outputs[k]<<std::endl;
|
406 |
+
m_bindings[incnt+k] = arrout[k];
|
407 |
+
}
|
408 |
+
bool status = m_context->enqueue(1,m_bindings.data(),(cudaStream_t)stream,nullptr);
|
409 |
+
return status?0:-1;
|
410 |
+
}
|
411 |
+
|
412 |
+
TrtModel::TrtModel(int b,int w,int h):AiModel(){
|
413 |
+
}
|
414 |
+
|
415 |
+
TrtModel::TrtModel():AiModel(){
|
416 |
+
}
|
417 |
+
|
418 |
+
TrtModel::~TrtModel(){
|
419 |
+
if(m_context){
|
420 |
+
m_context->destroy();
|
421 |
+
m_context = nullptr;
|
422 |
+
}
|
423 |
+
if(m_engine){
|
424 |
+
m_engine->destroy();
|
425 |
+
m_engine = nullptr;
|
426 |
+
}
|
427 |
+
}
|
428 |
+
|
429 |
+
#endif
|
430 |
+
/*
|
431 |
+
int main(int argc,char** argv){
|
432 |
+
OnnxModel *model=new OnnxModel();
|
433 |
+
std::string fn = "onnx/mfcc.onnx";
|
434 |
+
model->initModel(fn);
|
435 |
+
delete model;
|
436 |
+
return 0;
|
437 |
+
}
|
438 |
+
*/
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/aimodel.h
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
#include <vector>
|
4 |
+
#include <string>
|
5 |
+
#include <iostream>
|
6 |
+
#include <fstream>
|
7 |
+
#include <algorithm>
|
8 |
+
#include "jmat.h"
|
9 |
+
|
10 |
+
struct AiCfg{
|
11 |
+
std::vector<int64_t> kind_inputs;
|
12 |
+
std::vector<int64_t> size_inputs;
|
13 |
+
std::vector<std::string> names;
|
14 |
+
std::vector<const char*> name_inputs;
|
15 |
+
//int m_sizeinput;
|
16 |
+
std::vector<std::vector<int64_t>> shape_inputs;
|
17 |
+
|
18 |
+
std::vector<int64_t> kind_outputs;
|
19 |
+
std::vector<int64_t> size_outputs;
|
20 |
+
std::vector<const char*> name_outputs;
|
21 |
+
//int m_sizeoutput;
|
22 |
+
std::vector<std::vector<int64_t>> shape_outputs;
|
23 |
+
|
24 |
+
const char** names_in;
|
25 |
+
const char** names_out;
|
26 |
+
void dump();
|
27 |
+
int inShape(int inx,int dim,int val);
|
28 |
+
int outShape(int inx,int dim,int val);
|
29 |
+
AiCfg clone();
|
30 |
+
};
|
31 |
+
|
32 |
+
class AiModel{
|
33 |
+
protected:
|
34 |
+
int m_inited;
|
35 |
+
std::string m_modelPath;
|
36 |
+
std::string m_modelbin;
|
37 |
+
std::string m_modelparam;
|
38 |
+
|
39 |
+
AiCfg *m_cfg;
|
40 |
+
|
41 |
+
virtual int doInitModel();
|
42 |
+
virtual int doRunModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg=nullptr);
|
43 |
+
public:
|
44 |
+
void dump();
|
45 |
+
AiCfg config();
|
46 |
+
int pushName(const char* name,int input);
|
47 |
+
int initModel(std::string& modelpath);
|
48 |
+
int initModel(std::string& binfn,std::string& paramfn);
|
49 |
+
int runModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg = nullptr);
|
50 |
+
AiModel();
|
51 |
+
virtual ~AiModel();
|
52 |
+
};
|
53 |
+
|
54 |
+
#define _ONNX_
|
55 |
+
#ifdef _ONNX_
|
56 |
+
#include "onnx/onnxruntime_cxx_api.h"
|
57 |
+
class OnnxModel:public AiModel{
|
58 |
+
protected:
|
59 |
+
int m_batch = 0;
|
60 |
+
int m_width = 640;
|
61 |
+
int m_height = 960;
|
62 |
+
Ort::Env env{nullptr};
|
63 |
+
Ort::SessionOptions sessionOptions{nullptr};
|
64 |
+
Ort::Session session{nullptr};
|
65 |
+
int doInitModel()override;
|
66 |
+
int doRunModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg=nullptr)override;
|
67 |
+
public:
|
68 |
+
OnnxModel(int b,int w,int h);
|
69 |
+
OnnxModel();
|
70 |
+
virtual ~OnnxModel();
|
71 |
+
};
|
72 |
+
#endif
|
73 |
+
|
74 |
+
#define _NCNN_
|
75 |
+
#ifdef _NCNN_
|
76 |
+
#include "net.h"
|
77 |
+
class NcnnModel:public AiModel{
|
78 |
+
protected:
|
79 |
+
int m_width = 160;
|
80 |
+
int m_height = 160;
|
81 |
+
ncnn::Net net;
|
82 |
+
int doInitModel()override;
|
83 |
+
int doRunModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg=nullptr)override;
|
84 |
+
public:
|
85 |
+
NcnnModel(int w,int h);
|
86 |
+
NcnnModel();
|
87 |
+
virtual ~NcnnModel();
|
88 |
+
};
|
89 |
+
|
90 |
+
#endif
|
91 |
+
|
92 |
+
#ifdef _TENSORRT_
|
93 |
+
#include "NvInfer.h"
|
94 |
+
#include "NvInferLegacyDims.h"
|
95 |
+
#include "NvInferRuntime.h"
|
96 |
+
class Logger:public nvinfer1::ILogger {
|
97 |
+
public:
|
98 |
+
void log(nvinfer1::ILogger::Severity severity, const char *msg) noexcept override {
|
99 |
+
// suppress info-level messages
|
100 |
+
if (severity == Severity::kINFO)
|
101 |
+
return;
|
102 |
+
switch (severity) {
|
103 |
+
case Severity::kINTERNAL_ERROR:
|
104 |
+
std::cerr << "INTERNAL_ERROR: ";
|
105 |
+
break;
|
106 |
+
case Severity::kERROR:
|
107 |
+
std::cerr << "ERROR: ";
|
108 |
+
break;
|
109 |
+
case Severity::kWARNING:
|
110 |
+
std::cerr << "WARNING: ";
|
111 |
+
break;
|
112 |
+
case Severity::kINFO:
|
113 |
+
std::cerr << "INFO: ";
|
114 |
+
break;
|
115 |
+
default:
|
116 |
+
std::cerr << "UNKNOWN: ";
|
117 |
+
break;
|
118 |
+
}
|
119 |
+
std::cerr << msg << std::endl;
|
120 |
+
}
|
121 |
+
};
|
122 |
+
|
123 |
+
class TrtModel:public AiModel{
|
124 |
+
protected:
|
125 |
+
Logger m_logger;
|
126 |
+
nvinfer1::IExecutionContext *m_context;
|
127 |
+
nvinfer1::ICudaEngine *m_engine;
|
128 |
+
|
129 |
+
int doInitModel()override;
|
130 |
+
int doRunModel(void** arrin,void** arrout,void* stream,AiCfg* pcfg=nullptr)override;
|
131 |
+
public:
|
132 |
+
TrtModel(int b,int w,int h);
|
133 |
+
TrtModel();
|
134 |
+
virtual ~TrtModel();
|
135 |
+
};
|
136 |
+
#endif
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/blendgram.cpp
ADDED
@@ -0,0 +1,437 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <stdio.h>
|
2 |
+
#include <math.h>
|
3 |
+
#include <stdlib.h>
|
4 |
+
|
5 |
+
#include "blendgram.h"
|
6 |
+
|
7 |
+
|
8 |
+
void exColorBlend_Normal(uint8* T,uint8* A,uint8* B){ ColorBlend_Buffer(T,A,B,Normal); }
|
9 |
+
void exColorBlend_Lighten(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Lighten);}
|
10 |
+
void exColorBlend_Darken(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Darken);}
|
11 |
+
void exColorBlend_Multiply(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Multiply);}
|
12 |
+
void exColorBlend_Average(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Average);}
|
13 |
+
void exColorBlend_Add(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Add);}
|
14 |
+
|
15 |
+
void exColorBlend_Subtract(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Subtract);}
|
16 |
+
void exColorBlend_Difference(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Difference);}
|
17 |
+
void exColorBlend_Negation(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Negation);}
|
18 |
+
void exColorBlend_Screen(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Screen);}
|
19 |
+
void exColorBlend_Exclusion(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Exclusion);}
|
20 |
+
|
21 |
+
void exColorBlend_Overlay(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Overlay);}
|
22 |
+
void exColorBlend_SoftLight(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,SoftLight);}
|
23 |
+
void exColorBlend_HardLight(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,HardLight);}
|
24 |
+
void exColorBlend_ColorDodge(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,ColorDodge);}
|
25 |
+
void exColorBlend_ColorBurn(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,ColorBurn);}
|
26 |
+
|
27 |
+
void exColorBlend_LinearDodge(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,LinearDodge);}
|
28 |
+
void exColorBlend_LinearBurn(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,LinearBurn);}
|
29 |
+
void exColorBlend_LinearLight(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,LinearLight);}
|
30 |
+
void exColorBlend_VividLight(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,VividLight);}
|
31 |
+
void exColorBlend_PinLight(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,PinLight);}
|
32 |
+
|
33 |
+
void exColorBlend_HardMix(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,HardMix);}
|
34 |
+
void exColorBlend_Reflect(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Reflect);}
|
35 |
+
void exColorBlend_Glow(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Glow);}
|
36 |
+
void exColorBlend_Phoenix(uint8* T,uint8* A,uint8* B) { ColorBlend_Buffer(T,A,B,Phoenix);}
|
37 |
+
|
38 |
+
typedef void (*BlendFunc) (uint8* T,uint8* A,uint8* B);
|
39 |
+
static int MAX_FUNC = 25;
|
40 |
+
static BlendFunc blendfuncs[25]={
|
41 |
+
&exColorBlend_Normal,
|
42 |
+
&exColorBlend_Lighten,
|
43 |
+
&exColorBlend_Darken,
|
44 |
+
&exColorBlend_Multiply,
|
45 |
+
&exColorBlend_Average,
|
46 |
+
&exColorBlend_Add,
|
47 |
+
|
48 |
+
&exColorBlend_Subtract,
|
49 |
+
&exColorBlend_Difference,
|
50 |
+
&exColorBlend_Negation,
|
51 |
+
&exColorBlend_Screen,
|
52 |
+
&exColorBlend_Exclusion,
|
53 |
+
|
54 |
+
&exColorBlend_Overlay,
|
55 |
+
&exColorBlend_SoftLight,
|
56 |
+
&exColorBlend_HardLight,
|
57 |
+
&exColorBlend_ColorDodge,
|
58 |
+
&exColorBlend_ColorBurn,
|
59 |
+
|
60 |
+
&exColorBlend_LinearDodge,
|
61 |
+
&exColorBlend_LinearBurn,
|
62 |
+
&exColorBlend_LinearLight,
|
63 |
+
&exColorBlend_VividLight,
|
64 |
+
&exColorBlend_PinLight,
|
65 |
+
|
66 |
+
&exColorBlend_HardMix,
|
67 |
+
&exColorBlend_Reflect,
|
68 |
+
&exColorBlend_Glow,
|
69 |
+
&exColorBlend_Phoenix
|
70 |
+
};
|
71 |
+
|
72 |
+
void BlendGramSimp(unsigned char *Src,unsigned char* Mask, unsigned char *Dest, int Width, int Height, int Mode)
|
73 |
+
{
|
74 |
+
if(Mode<1)return;
|
75 |
+
if(Mode>=MAX_FUNC)return;
|
76 |
+
BlendFunc func=blendfuncs[Mode];
|
77 |
+
unsigned char *LinePS, *LinePD,*LinePM;
|
78 |
+
for (int Y = 0; Y < Height; Y += 1)
|
79 |
+
{
|
80 |
+
LinePS = Src + Y * Width * 4;
|
81 |
+
LinePM = Mask + Y * Width * 4;
|
82 |
+
LinePD = Dest + Y * Width * 4;
|
83 |
+
for (int X = 0; X < Width; X += 1)
|
84 |
+
{
|
85 |
+
func(LinePD,LinePS,LinePM);
|
86 |
+
LinePS += 4;
|
87 |
+
LinePM += 4;
|
88 |
+
LinePD += 4;
|
89 |
+
}
|
90 |
+
}
|
91 |
+
}
|
92 |
+
|
93 |
+
void BlendGramAlpha3(unsigned char *Src,unsigned char* Mask, unsigned char *Dest, int Width, int Height)
|
94 |
+
{
|
95 |
+
printf("w %d h %d\n",Width,Height);
|
96 |
+
unsigned char *LinePS, *LinePD,*LinePM;
|
97 |
+
for (int Y = 0; Y < Height; Y += 1)
|
98 |
+
{
|
99 |
+
LinePS = Src + Y * Width * 3;
|
100 |
+
LinePM = Mask + Y * Width * 3;
|
101 |
+
LinePD = Dest + Y * Width * 3;
|
102 |
+
for (int X = 0; X < Width; X += 1)
|
103 |
+
{
|
104 |
+
//func(LinePD,LinePS,LinePM);
|
105 |
+
//ColorBlend_Alpha(LinePD,LinePD,LinePS,*LinePM);
|
106 |
+
float alpha = *LinePM/255.0f;
|
107 |
+
float beta = 1.0f-alpha;
|
108 |
+
//if(beta<0.5f) printf("==alpha %f beta %f\n",alpha,beta);
|
109 |
+
//if(beta<0.5f) printf("od %u ps %u\n",LinePD[0],LinePS[0]);
|
110 |
+
LinePD[0] = CLAMPCOLOR( LinePD[0]*alpha+LinePS[0]*beta);
|
111 |
+
//if(beta<0.5f) printf("new %u ps%u \n",LinePD[0],LinePS[0]);
|
112 |
+
//if(beta<0.5f) getchar();
|
113 |
+
LinePD[1] = CLAMPCOLOR(LinePD[1]*alpha+LinePS[1]*beta);
|
114 |
+
LinePD[2] = CLAMPCOLOR( LinePD[2]*alpha+LinePS[2]*beta);
|
115 |
+
LinePS += 3;
|
116 |
+
LinePM += 3;
|
117 |
+
LinePD += 3;
|
118 |
+
}
|
119 |
+
}
|
120 |
+
}
|
121 |
+
|
122 |
+
void BlendGramAlpha(unsigned char *Src,unsigned char* Mask, unsigned char *Dest, int Width, int Height)
|
123 |
+
{
|
124 |
+
unsigned char *LinePS, *LinePD,*LinePM;
|
125 |
+
for (int Y = 0; Y < Height; Y += 1)
|
126 |
+
{
|
127 |
+
LinePS = Src + Y * Width * 3;
|
128 |
+
LinePM = Mask + Y * Width * 1;
|
129 |
+
LinePD = Dest + Y * Width * 3;
|
130 |
+
for (int X = 0; X < Width; X += 1)
|
131 |
+
{
|
132 |
+
//func(LinePD,LinePS,LinePM);
|
133 |
+
ColorBlend_Alpha(LinePD,LinePD,LinePS,*LinePM);
|
134 |
+
/*
|
135 |
+
float alpha = *LinePM/255.0f;
|
136 |
+
float beta = 1.0f-alpha;
|
137 |
+
//printf("==alpha %f beta %f\n",alpha,beta);
|
138 |
+
LinePD[0] = LinePD[0]*alpha+LinePS[0]*beta;
|
139 |
+
LinePD[1] = LinePD[1]*alpha+LinePS[1]*beta;
|
140 |
+
LinePD[2] = LinePD[2]*alpha+LinePS[2]*beta;
|
141 |
+
*/
|
142 |
+
LinePS += 3;
|
143 |
+
LinePM += 1;
|
144 |
+
LinePD += 3;
|
145 |
+
}
|
146 |
+
}
|
147 |
+
}
|
148 |
+
|
149 |
+
void BlendGramAlphaRev(unsigned char *Src,unsigned char* Mask, unsigned char *Dest, int Width, int Height)
|
150 |
+
{
|
151 |
+
unsigned char *LinePS, *LinePD,*LinePM;
|
152 |
+
for (int Y = 0; Y < Height; Y += 1)
|
153 |
+
{
|
154 |
+
LinePS = Src + Y * Width * 3;
|
155 |
+
LinePM = Mask + Y * Width * 1;
|
156 |
+
LinePD = Dest + Y * Width * 3;
|
157 |
+
for (int X = 0; X < Width; X += 1)
|
158 |
+
{
|
159 |
+
//func(LinePD,LinePS,LinePM);
|
160 |
+
ColorBlend_Alpha(LinePD,LinePS,LinePD,*LinePM);
|
161 |
+
LinePS += 3;
|
162 |
+
LinePM += 1;
|
163 |
+
LinePD += 3;
|
164 |
+
}
|
165 |
+
}
|
166 |
+
}
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
/*
|
172 |
+
void BlendGram(CBitmap* image,CBitmap* mask,int mode)
|
173 |
+
{
|
174 |
+
if(mode<1)return;
|
175 |
+
if(mode>=MAX_FUNC)return;
|
176 |
+
BlendFunc func=blendfuncs[mode];
|
177 |
+
int Stride=image->width*4;
|
178 |
+
unsigned char *LinePS, *LinePD,*LinePM;
|
179 |
+
for (int Y = 0; Y < image->height; Y += 1)
|
180 |
+
{
|
181 |
+
LinePS = (unsigned char*)image->pixels +image->stride*Y;
|
182 |
+
LinePM = (unsigned char*)mask->pixels + mask->stride*Y;
|
183 |
+
LinePD = (unsigned char*)image->pixels +image->stride*Y;
|
184 |
+
for (int X = 0; X < image->width; X += 1)
|
185 |
+
{
|
186 |
+
func(LinePD,LinePS,LinePM);
|
187 |
+
LinePS += 4;
|
188 |
+
LinePM += 4;
|
189 |
+
LinePD += 4;
|
190 |
+
}
|
191 |
+
}
|
192 |
+
}
|
193 |
+
|
194 |
+
void BlendImageAdjustWithMask(CBitmap* bmp,CBitmap* adj,CBitmap* dst ,CBitmap* msk,int mode)
|
195 |
+
{
|
196 |
+
unsigned char* bmppixels=(unsigned char*)bmp->pixels;
|
197 |
+
unsigned char* mskpixels=(unsigned char*)msk->pixels;
|
198 |
+
unsigned char* dstpixels=(unsigned char*)dst->pixels;
|
199 |
+
unsigned char* adjpixels=(unsigned char*)adj->pixels;
|
200 |
+
int stride=bmp->stride;
|
201 |
+
int width=bmp->width;
|
202 |
+
int height=bmp->height;
|
203 |
+
int X,Y;
|
204 |
+
unsigned char* LinePS , * LinePM , * LinePD , * LinePA ;
|
205 |
+
#pragma omp parallel for private(LinePS,LinePM,LinePD,LinePA,X,Y)
|
206 |
+
for (Y = 0; Y < height; Y ++)
|
207 |
+
{
|
208 |
+
int offset=stride*Y;
|
209 |
+
LinePS = bmppixels +offset;
|
210 |
+
LinePM = mskpixels +offset;
|
211 |
+
LinePD = dstpixels +offset;
|
212 |
+
LinePA = adjpixels +offset;
|
213 |
+
for (X = 0; X < width; X ++)
|
214 |
+
{
|
215 |
+
unsigned char M=*LinePM;
|
216 |
+
if(M==0xFF){
|
217 |
+
LinePD[0]=LinePS[0];
|
218 |
+
LinePD[1]=LinePS[1];
|
219 |
+
LinePD[2]=LinePS[2];
|
220 |
+
}else if(M==0x00){
|
221 |
+
LinePD[0]=LinePA[0];
|
222 |
+
LinePD[1]=LinePA[1];
|
223 |
+
LinePD[2]=LinePA[2];
|
224 |
+
}else{
|
225 |
+
ColorBlend_Alpha(LinePD,LinePS,LinePA,M);
|
226 |
+
}
|
227 |
+
LinePD[3]=LinePS[3];
|
228 |
+
LinePS += 4; LinePM += 4; LinePD += 4; LinePA += 4;
|
229 |
+
}
|
230 |
+
}
|
231 |
+
}
|
232 |
+
|
233 |
+
|
234 |
+
void BlendImageAdjustWithMaskEx(CBitmap* bmp,CBitmap* adj,CBitmap* dst ,CBitmap* msk,int mode)
|
235 |
+
{
|
236 |
+
unsigned char* bmppixels=(unsigned char*)bmp->pixels;
|
237 |
+
unsigned char* mskpixels=(unsigned char*)msk->pixels;
|
238 |
+
unsigned char* dstpixels=(unsigned char*)dst->pixels;
|
239 |
+
unsigned char* adjpixels=(unsigned char*)adj->pixels;
|
240 |
+
int stride=bmp->stride;
|
241 |
+
int width=bmp->width;
|
242 |
+
int height=bmp->height;
|
243 |
+
int X,Y;
|
244 |
+
unsigned char* LinePS , * LinePM , * LinePD , * LinePA ;
|
245 |
+
#pragma omp parallel for private(LinePS,LinePM,LinePD,LinePA,X,Y)
|
246 |
+
for (Y = 0; Y < height; Y ++)
|
247 |
+
{
|
248 |
+
int offset=stride*Y;
|
249 |
+
LinePS = bmppixels +offset;
|
250 |
+
LinePM = mskpixels +offset;
|
251 |
+
LinePD = dstpixels +offset;
|
252 |
+
LinePA = adjpixels +offset;
|
253 |
+
for (X = 0; X < width; X ++)
|
254 |
+
{
|
255 |
+
unsigned char M=*LinePM;
|
256 |
+
if(M==0xFF){
|
257 |
+
LinePD[0]=LinePS[0];
|
258 |
+
LinePD[1]=LinePS[1];
|
259 |
+
LinePD[2]=LinePS[2];
|
260 |
+
}else if(M==0x00){
|
261 |
+
LinePD[0]=LinePA[0];
|
262 |
+
LinePD[1]=LinePA[1];
|
263 |
+
LinePD[2]=LinePA[2];
|
264 |
+
}else{
|
265 |
+
//ColorBlend_Alpha(LinePD,LinePS,LinePA,M);
|
266 |
+
LinePD[0]=LinePS[0]*M>>8;
|
267 |
+
LinePD[1]=LinePS[1]*M>>8;
|
268 |
+
LinePD[2]=LinePS[2]*M>>8;
|
269 |
+
}
|
270 |
+
LinePD[3]=M;
|
271 |
+
LinePS += 4; LinePM += 4; LinePD += 4; LinePA += 4;
|
272 |
+
}
|
273 |
+
}
|
274 |
+
}
|
275 |
+
|
276 |
+
|
277 |
+
|
278 |
+
|
279 |
+
void BlendImageAdjustWithAlpha(CBitmap* bmp,CBitmap* adj,CBitmap* dst ,int alpha,int mode){
|
280 |
+
unsigned char* bmppixels=(unsigned char*)bmp->pixels;
|
281 |
+
unsigned char* dstpixels=(unsigned char*)dst->pixels;
|
282 |
+
unsigned char* adjpixels=(unsigned char*)adj->pixels;
|
283 |
+
int stride=bmp->stride;
|
284 |
+
int width=bmp->width;
|
285 |
+
int height=bmp->height;
|
286 |
+
int X,Y;
|
287 |
+
unsigned char M=CLAMPCOLOR(alpha);
|
288 |
+
unsigned char *LinePS , *LinePD , *LinePA ;
|
289 |
+
#pragma omp parallel for private(LinePS,LinePD,LinePA,X,Y)
|
290 |
+
for (Y = 0; Y < height; Y ++)
|
291 |
+
{
|
292 |
+
int offset=stride*Y;
|
293 |
+
LinePS = bmppixels +offset;
|
294 |
+
LinePD = dstpixels +offset;
|
295 |
+
LinePA = adjpixels +offset;
|
296 |
+
for (X = 0; X < width; X ++)
|
297 |
+
{
|
298 |
+
if(M==0xFF){
|
299 |
+
LinePD[0]=LinePS[0];
|
300 |
+
LinePD[1]=LinePS[1];
|
301 |
+
LinePD[2]=LinePS[2];
|
302 |
+
}else if(M==0x00){
|
303 |
+
LinePD[0]=LinePA[0];
|
304 |
+
LinePD[1]=LinePA[1];
|
305 |
+
LinePD[2]=LinePA[2];
|
306 |
+
}else{
|
307 |
+
ColorBlend_Alpha(LinePD,LinePS,LinePA,M);
|
308 |
+
}
|
309 |
+
LinePD[3]=LinePS[3];
|
310 |
+
LinePS += 4; LinePD += 4; LinePA += 4;
|
311 |
+
}
|
312 |
+
}
|
313 |
+
}
|
314 |
+
|
315 |
+
void BlendImageAdjustWithAlphaMask(CBitmap* bmp,CBitmap* adj,CBitmap* dst ,CBitmap* msk,int alpha,int mode){
|
316 |
+
unsigned char* bmppixels=(unsigned char*)bmp->pixels;
|
317 |
+
unsigned char* mskpixels=(unsigned char*)msk->pixels;
|
318 |
+
unsigned char* dstpixels=(unsigned char*)dst->pixels;
|
319 |
+
unsigned char* adjpixels=(unsigned char*)adj->pixels;
|
320 |
+
int stride=bmp->stride;
|
321 |
+
int width=bmp->width;
|
322 |
+
int height=bmp->height;
|
323 |
+
int X,Y;
|
324 |
+
unsigned char NM=CLAMPCOLOR(alpha);
|
325 |
+
unsigned char *LinePS , *LinePM , *LinePD , *LinePA ;
|
326 |
+
#pragma omp parallel for private(LinePS,LinePM,LinePD,LinePA,X,Y)
|
327 |
+
for (Y = 0; Y < height; Y ++)
|
328 |
+
{
|
329 |
+
int offset=stride*Y;
|
330 |
+
LinePS = bmppixels +offset;
|
331 |
+
LinePM = mskpixels +offset;
|
332 |
+
LinePD = dstpixels +offset;
|
333 |
+
LinePA = adjpixels +offset;
|
334 |
+
for (X = 0; X < width; X ++)
|
335 |
+
{
|
336 |
+
unsigned char M=*LinePM;
|
337 |
+
if(M==0xFF){
|
338 |
+
LinePD[0]=LinePS[0];
|
339 |
+
LinePD[1]=LinePS[1];
|
340 |
+
LinePD[2]=LinePS[2];
|
341 |
+
}else if(M==0x00){
|
342 |
+
if(NM==0xFF){
|
343 |
+
LinePD[0]=LinePS[0];
|
344 |
+
LinePD[1]=LinePS[1];
|
345 |
+
LinePD[2]=LinePS[2];
|
346 |
+
}else {
|
347 |
+
if(NM==0x00){
|
348 |
+
//none
|
349 |
+
LinePD[0]=LinePA[0];
|
350 |
+
LinePD[1]=LinePA[1];
|
351 |
+
LinePD[2]=LinePA[2];
|
352 |
+
}else{
|
353 |
+
ColorBlend_Alpha(LinePD,LinePS,LinePA,NM);
|
354 |
+
}
|
355 |
+
}
|
356 |
+
}else{
|
357 |
+
//
|
358 |
+
if(NM==0xFF){
|
359 |
+
LinePD[0]=LinePS[0];
|
360 |
+
LinePD[1]=LinePS[1];
|
361 |
+
LinePD[2]=LinePS[2];
|
362 |
+
}else{
|
363 |
+
if(NM==0x00){
|
364 |
+
ColorBlend_Alpha(LinePD,LinePS,LinePA,M);
|
365 |
+
}else{
|
366 |
+
ColorBlend_Alpha(LinePA,LinePS,LinePA,NM);
|
367 |
+
ColorBlend_Alpha(LinePD,LinePS,LinePA,M);
|
368 |
+
}
|
369 |
+
}
|
370 |
+
}
|
371 |
+
LinePD[3]=LinePS[3];
|
372 |
+
LinePS += 4; LinePM += 4; LinePD += 4; LinePA += 4;
|
373 |
+
}
|
374 |
+
}
|
375 |
+
}
|
376 |
+
|
377 |
+
void ReadAlphaBySrc(CBitmap* src,CBitmap* alpha){
|
378 |
+
memcpy(alpha,src,sizeof(CBitmap));
|
379 |
+
alpha->stride=src->width;
|
380 |
+
alpha->channel=1;
|
381 |
+
alpha->pixels=(CPixel*)malloc(alpha->width*alpha->height*sizeof(unsigned char));
|
382 |
+
unsigned char* bmppixels=(unsigned char*)src->pixels;
|
383 |
+
unsigned char* alapixels=(unsigned char*)alpha->pixels;
|
384 |
+
int stride=src->stride;
|
385 |
+
int width=src->width;
|
386 |
+
int height=src->height;
|
387 |
+
int X,Y;
|
388 |
+
unsigned char *LinePS , *LinePA;
|
389 |
+
//#pragma omp parallel for private(LinePS,LinePA)
|
390 |
+
for (Y = 0; Y < height; Y ++)
|
391 |
+
{
|
392 |
+
LinePS = bmppixels +stride*Y;
|
393 |
+
LinePA = alapixels +width*Y;
|
394 |
+
for (X = 0; X < width; X ++)
|
395 |
+
{
|
396 |
+
LinePA[0]=LinePS[3];
|
397 |
+
LinePS += 4; LinePA ++;
|
398 |
+
}
|
399 |
+
}
|
400 |
+
}
|
401 |
+
|
402 |
+
|
403 |
+
void CheckAlpha(CBitmap* bmp,CBitmap* alpha)
|
404 |
+
{
|
405 |
+
unsigned char* bmppixels=(unsigned char*)bmp->pixels;
|
406 |
+
unsigned char* alapixels=(unsigned char*)alpha->pixels;
|
407 |
+
int stride=bmp->stride;
|
408 |
+
int width=bmp->width;
|
409 |
+
int height=bmp->height;
|
410 |
+
int X,Y;
|
411 |
+
unsigned char *LinePS , *LinePA;
|
412 |
+
//#pragma omp parallel for private(LinePS,LinePA)
|
413 |
+
for (Y = 0; Y < height; Y ++)
|
414 |
+
{
|
415 |
+
LinePS = bmppixels +stride*Y;
|
416 |
+
LinePA = alapixels +width*Y;
|
417 |
+
for (X = 0; X < width; X ++)
|
418 |
+
{
|
419 |
+
//unsigned char M=LinePA[0];
|
420 |
+
if(*LinePA==0x00){
|
421 |
+
LinePS[0]=0;
|
422 |
+
LinePS[1]=0;
|
423 |
+
LinePS[2]=0;
|
424 |
+
LinePS[3]=0;
|
425 |
+
//}else if(M<0xff){
|
426 |
+
//if(LinePD[0]>M)LinePD[0]=M;
|
427 |
+
//if(LinePD[1]>M)LinePD[1]=M;
|
428 |
+
//if(LinePD[2]>M)LinePD[2]=M;
|
429 |
+
//LinePD[3]=M;
|
430 |
+
}else{
|
431 |
+
}
|
432 |
+
LinePS += 4; LinePA++;
|
433 |
+
}
|
434 |
+
}
|
435 |
+
}
|
436 |
+
*/
|
437 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/blendgram.h
ADDED
@@ -0,0 +1,287 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef __BLENDGRAM_H__
|
2 |
+
#define __BLENDGRAM_H__
|
3 |
+
|
4 |
+
#include <stdlib.h>
|
5 |
+
#include <stdio.h>
|
6 |
+
#include <math.h>
|
7 |
+
|
8 |
+
typedef unsigned char uchar;
|
9 |
+
#define CLAMPCOLOR(x) (uchar)((x)<(0)?(0):((x)>(255)?(255):(x)))
|
10 |
+
|
11 |
+
#define MMAX(A,B) ((A)>(B)?(A):(B))
|
12 |
+
#define MMIN(A,B) ((A)<(B)?(A):(B))
|
13 |
+
|
14 |
+
static int ConstBlend_Buffer = 0;
|
15 |
+
static int ConstBlend_Normal=ConstBlend_Buffer+1;
|
16 |
+
static int ConstBlend_Lighten=ConstBlend_Buffer+2;
|
17 |
+
static int ConstBlend_Darken=ConstBlend_Buffer+3;
|
18 |
+
static int ConstBlend_Multiply=ConstBlend_Buffer+4;
|
19 |
+
static int ConstBlend_Average=ConstBlend_Buffer+5;
|
20 |
+
|
21 |
+
static int ConstBlend_Add=ConstBlend_Buffer+6;
|
22 |
+
static int ConstBlend_Subtract=ConstBlend_Buffer+7;
|
23 |
+
static int ConstBlend_Difference=ConstBlend_Buffer+8;
|
24 |
+
static int ConstBlend_Negation=ConstBlend_Buffer+9;
|
25 |
+
static int ConstBlend_Screen=ConstBlend_Buffer+10;
|
26 |
+
static int ConstBlend_Exclusion=ConstBlend_Buffer+11;
|
27 |
+
static int ConstBlend_Overlay=ConstBlend_Buffer+12;
|
28 |
+
static int ConstBlend_SoftLight=ConstBlend_Buffer+13;
|
29 |
+
static int ConstBlend_HardLight=ConstBlend_Buffer+14;
|
30 |
+
static int ConstBlend_ColorDodge=ConstBlend_Buffer+15;
|
31 |
+
static int ConstBlend_ColorBurn=ConstBlend_Buffer+16;
|
32 |
+
static int ConstBlend_LinearDodge=ConstBlend_Buffer+17;
|
33 |
+
static int ConstBlend_LinearBurn=ConstBlend_Buffer+18;
|
34 |
+
static int ConstBlend_LinearLight=ConstBlend_Buffer+19;
|
35 |
+
static int ConstBlend_VividLight=ConstBlend_Buffer+20;
|
36 |
+
static int ConstBlend_PinLight=ConstBlend_Buffer+21;
|
37 |
+
static int ConstBlend_HardMix=ConstBlend_Buffer+22;
|
38 |
+
static int ConstBlend_Reflect=ConstBlend_Buffer+23;
|
39 |
+
static int ConstBlend_Glow=ConstBlend_Buffer+24;
|
40 |
+
static int ConstBlend_Phoenix=ConstBlend_Buffer+25;
|
41 |
+
|
42 |
+
//void BlendGram(CBitmap* immage,CBitmap* mask,int mode);
|
43 |
+
|
44 |
+
//#typedef unsigned char uint8
|
45 |
+
#define uint8 unsigned char
|
46 |
+
#define float64 double
|
47 |
+
#define TRUE 1
|
48 |
+
#define FALSE 0
|
49 |
+
|
50 |
+
inline uint8 mmin(uint8 A,uint8 B){
|
51 |
+
return A<B?A:B;
|
52 |
+
}
|
53 |
+
inline uint8 mmax(uint8 A,uint8 B){
|
54 |
+
return A>B?A:B;
|
55 |
+
}
|
56 |
+
|
57 |
+
#define ChannelBlend_Normal(A,B) ((uint8)(A))
|
58 |
+
#define ChannelBlend_Lighten(A,B) ((uint8)((B > A) ? B:A))
|
59 |
+
#define ChannelBlend_Darken(A,B) ((uint8)((B > A) ? A:B))
|
60 |
+
#define ChannelBlend_Multiply(A,B) ((uint8)((A * B) / 255))
|
61 |
+
#define ChannelBlend_Average(A,B) ((uint8)((A + B) / 2))
|
62 |
+
#define ChannelBlend_Add(A,B) ((uint8)(mmin(255, (A + B))))
|
63 |
+
#define ChannelBlend_Subtract(A,B) ((uint8)((A + B < 255) ? 0:(A + B - 255)))
|
64 |
+
#define ChannelBlend_Difference(A,B) ((uint8)(abs(A - B)))
|
65 |
+
#define ChannelBlend_Negation(A,B) ((uint8)(255 - abs(255 - A - B)))
|
66 |
+
#define ChannelBlend_Screen(A,B) ((uint8)(255 - (((255 - A) * (255 - B)) >> 8)))
|
67 |
+
#define ChannelBlend_Exclusion(A,B) ((uint8)(A + B - 2 * A * B / 255))
|
68 |
+
#define ChannelBlend_Overlay(A,B) ((uint8)((B < 128) ? (2 * A * B / 255):(255 - 2 * (255 - A) * (255 - B) / 255)))
|
69 |
+
#define ChannelBlend_SoftLight(A,B) ((uint8)((B < 128)?(2*((A>>1)+64))*((float)B/255):(255-(2*(255-((A>>1)+64))*(float)(255-B)/255))))
|
70 |
+
#define ChannelBlend_HardLight(A,B) (ChannelBlend_Overlay(B,A))
|
71 |
+
#define ChannelBlend_ColorDodge(A,B) ((uint8)((B == 255) ? B:mmin(255, ((A << 8 ) / (255 - B)))))
|
72 |
+
#define ChannelBlend_ColorBurn(A,B) ((uint8)((B == 0) ? B:mmax(0, (255 - ((255 - A) << 8 ) / B))))
|
73 |
+
#define ChannelBlend_LinearDodge(A,B)(ChannelBlend_Add(A,B))
|
74 |
+
#define ChannelBlend_LinearBurn(A,B) (ChannelBlend_Subtract(A,B))
|
75 |
+
#define ChannelBlend_LinearLight(A,B)((uint8)(B < 128)?ChannelBlend_LinearBurn(A,(2 * B)):ChannelBlend_LinearDodge(A,(2 * (B - 128))))
|
76 |
+
#define ChannelBlend_VividLight(A,B) ((uint8)(B < 128)?ChannelBlend_ColorBurn(A,(2 * B)):ChannelBlend_ColorDodge(A,(2 * (B - 128))))
|
77 |
+
#define ChannelBlend_PinLight(A,B) ((uint8)(B < 128)?ChannelBlend_Darken(A,(2 * B)):ChannelBlend_Lighten(A,(2 * (B - 128))))
|
78 |
+
#define ChannelBlend_HardMix(A,B) ((uint8)((ChannelBlend_VividLight(A,B) < 128) ? 0:255))
|
79 |
+
#define ChannelBlend_Reflect(A,B) ((uint8)((B == 255) ? B:mmin(255, (A * A / (255 - B)))))
|
80 |
+
#define ChannelBlend_Glow(A,B) (ChannelBlend_Reflect(B,A))
|
81 |
+
#define ChannelBlend_Phoenix(A,B) ((uint8)(mmin(A,B) - mmax(A,B) + 255))
|
82 |
+
#define ChannelBlend_SoftEx(A,B) (A*B/255+A*(255-((255-A)*(255-B)/255)-A*B/255)/255)
|
83 |
+
|
84 |
+
#define ChannelBlend_Alpha(A,B,O) ((uint8)(O * A + (1 - O) * B))
|
85 |
+
#define ChannelBlend_AlphaEx(A,B,O) ((uint8)((O * A + (255 - O) * B)/255))
|
86 |
+
#define ChannelBlend_AlphaF(A,B,F,O) (ChannelBlend_AlphaEx(F(A,B),A,O))
|
87 |
+
|
88 |
+
#define ColorBlend_Alpha(T,A,B,O) (T)[0] = ChannelBlend_AlphaEx((A)[0], (B)[0],O), (T)[1] = ChannelBlend_AlphaEx((A)[1], (B)[1],O), (T)[2] = ChannelBlend_AlphaEx((A)[2], (B)[2],O)
|
89 |
+
//, (T)[3] = ChannelBlend_AlphaEx((A)[3], (B)[3],O)
|
90 |
+
#define ColorBlend_AlphaF(T,A,B,F,O) (T)[0] = ChannelBlend_AlphaF((A)[0], (B)[0],F,O), (T)[1] = ChannelBlend_AlphaF((A)[1], (B)[1],F,O), (T)[2] = ChannelBlend_AlphaF((A)[2], (B )[2],F,O) , (T)[3] = ChannelBlend_AlphaEx((A)[3], (B)[3],O)
|
91 |
+
|
92 |
+
|
93 |
+
#define ColorBlend_Buffer(T,A,B,M) (T)[0] = ChannelBlend_##M((A)[0], (B)[0]), (T)[1] = ChannelBlend_##M((A)[1], (B)[1]), (T)[2] = ChannelBlend_##M((A)[2], (B)[2])
|
94 |
+
|
95 |
+
#define ColorBlend_Normal(T,A,B) (ColorBlend_Buffer(T,A,B,Normal))
|
96 |
+
#define ColorBlend_Lighten(T,A,B) (ColorBlend_Buffer(T,A,B,Lighten))
|
97 |
+
#define ColorBlend_Darken(T,A,B) (ColorBlend_Buffer(T,A,B,Darken))
|
98 |
+
#define ColorBlend_Multiply(T,A,B) (ColorBlend_Buffer(T,A,B,Multiply))
|
99 |
+
#define ColorBlend_Average(T,A,B) (ColorBlend_Buffer(T,A,B,Average))
|
100 |
+
#define ColorBlend_Add(T,A,B) (ColorBlend_Buffer(T,A,B,Add))
|
101 |
+
#define ColorBlend_Subtract(T,A,B) (ColorBlend_Buffer(T,A,B,Subtract))
|
102 |
+
#define ColorBlend_Difference(T,A,B) (ColorBlend_Buffer(T,A,B,Difference))
|
103 |
+
#define ColorBlend_Negation(T,A,B) (ColorBlend_Buffer(T,A,B,Negation))
|
104 |
+
#define ColorBlend_Screen(T,A,B) (ColorBlend_Buffer(T,A,B,Screen))
|
105 |
+
#define ColorBlend_Exclusion(T,A,B) (ColorBlend_Buffer(T,A,B,Exclusion))
|
106 |
+
#define ColorBlend_Overlay(T,A,B) (ColorBlend_Buffer(T,A,B,Overlay))
|
107 |
+
#define ColorBlend_SoftLight(T,A,B) (ColorBlend_Buffer(T,A,B,SoftLight))
|
108 |
+
#define ColorBlend_HardLight(T,A,B) (ColorBlend_Buffer(T,A,B,HardLight))
|
109 |
+
#define ColorBlend_ColorDodge(T,A,B) (ColorBlend_Buffer(T,A,B,ColorDodge))
|
110 |
+
#define ColorBlend_ColorBurn(T,A,B) (ColorBlend_Buffer(T,A,B,ColorBurn))
|
111 |
+
#define ColorBlend_LinearDodge(T,A,B) (ColorBlend_Buffer(T,A,B,LinearDodge))
|
112 |
+
#define ColorBlend_LinearBurn(T,A,B) (ColorBlend_Buffer(T,A,B,LinearBurn))
|
113 |
+
#define ColorBlend_LinearLight(T,A,B) (ColorBlend_Buffer(T,A,B,LinearLight))
|
114 |
+
#define ColorBlend_VividLight(T,A,B) (ColorBlend_Buffer(T,A,B,VividLight))
|
115 |
+
#define ColorBlend_PinLight(T,A,B) (ColorBlend_Buffer(T,A,B,PinLight))
|
116 |
+
#define ColorBlend_HardMix(T,A,B) (ColorBlend_Buffer(T,A,B,HardMix))
|
117 |
+
#define ColorBlend_Reflect(T,A,B) (ColorBlend_Buffer(T,A,B,Reflect))
|
118 |
+
#define ColorBlend_Glow(T,A,B) (ColorBlend_Buffer(T,A,B,Glow))
|
119 |
+
#define ColorBlend_Phoenix(T,A,B) (ColorBlend_Buffer(T,A,B,Phoenix))
|
120 |
+
|
121 |
+
|
122 |
+
#define ColorBlend_Hue(T,B,L) ColorBlend_Hls(T,B,L,HueL,LuminationB,SaturationB)
|
123 |
+
#define ColorBlend_Saturation(T,B,L) ColorBlend_Hls(T,B,L,HueB,LuminationB,SaturationL)
|
124 |
+
#define ColorBlend_Color(T,B,L) ColorBlend_Hls(T,B,L,HueL,LuminationB,SaturationL)
|
125 |
+
#define ColorBlend_Luminosity(T,B,L) ColorBlend_Hls(T,B,L,HueB,LuminationL,SaturationB)
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
+
#define ColorBlend_Hls(T,B,L,O1,O2,O3) { \
|
130 |
+
float64 HueB, LuminationB, SaturationB; \
|
131 |
+
float64 HueL, LuminationL, SaturationL; \
|
132 |
+
Color_RgbToHls((B)[2],(B)[1],(B)[0], &HueB, &LuminationB, &SaturationB); \
|
133 |
+
Color_RgbToHls((L)[2],(L)[1],(L)[0], &HueL, &LuminationL, &SaturationL); \
|
134 |
+
Color_HlsToRgb(O1,O2,O3,&(T)[2],&(T)[1],&(T)[0]); \
|
135 |
+
}
|
136 |
+
|
137 |
+
|
138 |
+
/*********************************************************************/
|
139 |
+
|
140 |
+
#define COLOR_OPAQUE (0)
|
141 |
+
#define COLOR_TRANSPARENT (127)
|
142 |
+
|
143 |
+
#define RGB_SIZE (3)
|
144 |
+
#define RGB_BPP (24)
|
145 |
+
#define RGB_MAXRED (255)
|
146 |
+
#define RGB_MAXGREEN (255)
|
147 |
+
#define RGB_MAXBLUE (255)
|
148 |
+
|
149 |
+
#define ARGB_SIZE (4)
|
150 |
+
#define ARGB_BPP (32)
|
151 |
+
#define ARGB_MAXALPHA (127)
|
152 |
+
#define ARGB_MAXRED (RGB_MAXRED)
|
153 |
+
#define ARGB_MAXGREEN (RGB_MAXGREEN)
|
154 |
+
#define ARGB_MAXBLUE (RGB_MAXBLUE)
|
155 |
+
|
156 |
+
/*********************************************************************/
|
157 |
+
|
158 |
+
#define Color_GetChannel(c,shift) ((uint8)((c) >> (shift)))
|
159 |
+
#define Color_Reverse(c,bpp) ((((uint8)(c) << 24) | ((uint8)((c) >> 8 ) << 16) | ((uint8)((c) >> 16) << 8 ) | \ ((uint8)((c) >> 24))) >> (32 - (bpp)))
|
160 |
+
|
161 |
+
#define Rgb_ByteWidth(width) ((width) * RGB_SIZE)
|
162 |
+
#define Rgb_PixelWidth(width) ((width) / RGB_SIZE)
|
163 |
+
|
164 |
+
#define Rgb_GetRed(rgb) (Color_GetChannel(rgb, 0))
|
165 |
+
#define Rgb_GetGreen(rgb) (Color_GetChannel(rgb, 8))
|
166 |
+
#define Rgb_GetBlue(rgb) (Color_GetChannel(rgb, 16))
|
167 |
+
|
168 |
+
#define Rgba_GetRed(rgba) (Color_GetChannel(rgba, 24))
|
169 |
+
#define Rgba_GetGreen(rgba) (Color_GetChannel(rgba, 16))
|
170 |
+
#define Rgba_GetBlue(rgba) (Color_GetChannel(rgba, 8))
|
171 |
+
#define Rgba_GetAlpha(rgba) (Color_GetChannel(rgba, 0))
|
172 |
+
|
173 |
+
#define Argb_GetAlpha(argb) (Color_GetChannel(argb, 24))
|
174 |
+
#define Argb_GetRed(argb) (Color_GetChannel(argb, 16))
|
175 |
+
#define Argb_GetGreen(argb) (Color_GetChannel(argb, 8))
|
176 |
+
#define Argb_GetBlue(argb) (Color_GetChannel(argb, 0))
|
177 |
+
|
178 |
+
#define MakeRgb(r,g,b) (((uint32)(uint8)(b) << 16) | ((uint16)(uint8)(g) << 8 ) | (uint8)(r))
|
179 |
+
#define MakeRgba(r,g,b,a) (((uint32)(uint8)(r) << 24) | ((uint16)(uint8)(g) << 16) | ((uint16)(uint8)(b) << 8 ) | (uint8)(a))
|
180 |
+
#define MakeArgb(a,r,g,b) (((uint32)(uint8)(a) << 24) | ((uint32)(uint8)(r) << 16) | ((uint16)(uint8)(g) << 8 ) | (uint8)(b))
|
181 |
+
#define HexToRgb(hex) (MakeRgb(((hex & 0xFF0000) >> 16), ((hex & 0x00FF00) >> 8 ), (hex & 0xFF)))
|
182 |
+
|
183 |
+
inline int Color_HueToRgb(float64 M1, float64 M2, float64 Hue, float64 *Channel)
|
184 |
+
{
|
185 |
+
if (Hue < 0.0)
|
186 |
+
Hue += 1.0;
|
187 |
+
else if (Hue > 1.0)
|
188 |
+
Hue -= 1.0;
|
189 |
+
|
190 |
+
if ((6.0 * Hue) < 1.0)
|
191 |
+
*Channel = (M1 + (M2 - M1) * Hue * 6.0);
|
192 |
+
else if ((2.0 * Hue) < 1.0)
|
193 |
+
*Channel = (M2);
|
194 |
+
else if ((3.0 * Hue) < 2.0)
|
195 |
+
*Channel = (M1 + (M2 - M1) * ((2.0F / 3.0F) - Hue) * 6.0);
|
196 |
+
else
|
197 |
+
*Channel = (M1);
|
198 |
+
|
199 |
+
return TRUE;
|
200 |
+
}
|
201 |
+
|
202 |
+
inline void Color_RgbToHls(uint8 Red, uint8 Green, uint8 Blue, float64 *Hue, float64 *Lumination, float64 *Saturation)
|
203 |
+
{
|
204 |
+
float64 Delta;
|
205 |
+
float64 Max, Min;
|
206 |
+
float64 Redf, Greenf, Bluef;
|
207 |
+
|
208 |
+
Redf = (float64)Red / 255.0;
|
209 |
+
Greenf = (float64)Green / 255.0;
|
210 |
+
Bluef = (float64)Blue / 255.0;
|
211 |
+
|
212 |
+
//Max = fmax(fmax(Redf, Greenf), Bluef);
|
213 |
+
//Min = fmin(fmin(Redf, Greenf), Bluef);
|
214 |
+
Max = MMAX(MMAX(Red, Green), Blue)/255.0;
|
215 |
+
Min = MMIN(MMIN(Red, Green), Blue)/255.0;
|
216 |
+
|
217 |
+
*Hue = 0;
|
218 |
+
*Lumination = (Max + Min) / 2.0F;
|
219 |
+
*Saturation = 0;
|
220 |
+
|
221 |
+
if (Max == Min)
|
222 |
+
return ;
|
223 |
+
|
224 |
+
Delta = (Max - Min);
|
225 |
+
|
226 |
+
if (*Lumination < 0.5)
|
227 |
+
*Saturation = Delta / (Max + Min);
|
228 |
+
else
|
229 |
+
*Saturation = Delta / (2.0 - Max - Min);
|
230 |
+
|
231 |
+
if (Redf == Max)
|
232 |
+
*Hue = (Greenf - Bluef) / Delta;
|
233 |
+
else if (Greenf == Max)
|
234 |
+
*Hue = 2.0 + (Bluef - Redf) / Delta;
|
235 |
+
else
|
236 |
+
*Hue = 4.0 + (Redf - Greenf) / Delta;
|
237 |
+
|
238 |
+
*Hue /= 6.0;
|
239 |
+
|
240 |
+
if (*Hue < 0.0)
|
241 |
+
*Hue += 1.0;
|
242 |
+
|
243 |
+
}
|
244 |
+
|
245 |
+
inline void Color_HlsToRgb(float64 Hue, float64 Lumination, float64 Saturation, uint8 *Red, uint8 *Green, uint8 *Blue)
|
246 |
+
{
|
247 |
+
float64 M1, M2;
|
248 |
+
float64 Redf, Greenf, Bluef;
|
249 |
+
|
250 |
+
if (Saturation == 0) {
|
251 |
+
Redf = Lumination;
|
252 |
+
Greenf = Lumination;
|
253 |
+
Bluef = Lumination;
|
254 |
+
} else {
|
255 |
+
if (Lumination <= 0.5)
|
256 |
+
M2 = Lumination * (1.0 + Saturation);
|
257 |
+
else
|
258 |
+
M2 = Lumination + Saturation - Lumination * Saturation;
|
259 |
+
|
260 |
+
M1 = (2.0 * Lumination - M2);
|
261 |
+
|
262 |
+
Color_HueToRgb(M1, M2, Hue + (1.0F / 3.0F), &Redf);
|
263 |
+
Color_HueToRgb(M1, M2, Hue, &Greenf);
|
264 |
+
Color_HueToRgb(M1, M2, Hue - (1.0F / 3.0F), &Bluef);
|
265 |
+
}
|
266 |
+
|
267 |
+
*Red = (uint8)(Redf * 255);
|
268 |
+
*Blue = (uint8)(Bluef * 255);
|
269 |
+
*Green = (uint8)(Greenf * 255);
|
270 |
+
|
271 |
+
}
|
272 |
+
|
273 |
+
void BlendGramSimp(unsigned char *Src,unsigned char* Mask, unsigned char *Dest, int Width, int Height, int Mode);
|
274 |
+
void BlendGramAlpha(unsigned char *Src,unsigned char* Mask, unsigned char *Dest, int Width, int Height);
|
275 |
+
void BlendGramAlpha3(unsigned char *Src,unsigned char* Mask, unsigned char *Dest, int Width, int Height);
|
276 |
+
void BlendGramAlphaRev(unsigned char *Src,unsigned char* Mask, unsigned char *Dest, int Width, int Height);
|
277 |
+
/*
|
278 |
+
void BlendImageAdjustWithMask(CBitmap* bmp,CBitmap* adj,CBitmap* dst ,CBitmap* msk,int mode);
|
279 |
+
void BlendImageAdjustWithMaskEx(CBitmap* bmp,CBitmap* adj,CBitmap* dst ,CBitmap* msk,int mode);
|
280 |
+
void BlendImageAdjustWithAlpha(CBitmap* bmp,CBitmap* adj,CBitmap* dst ,int alpha,int mode);
|
281 |
+
void BlendImageAdjustWithAlphaMask(CBitmap* bmp,CBitmap* adj,CBitmap* dst ,CBitmap* msk,int alpha,int mode);
|
282 |
+
|
283 |
+
void CheckAlpha(CBitmap* bmp,CBitmap* alpha);
|
284 |
+
void ReadAlphaBySrc(CBitmap* src,CBitmap* alpha);
|
285 |
+
*/
|
286 |
+
|
287 |
+
#endif
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/bt
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
g++ -g -I. \
|
2 |
+
-I/usr/include/opencv4 \
|
3 |
+
-I../third/x86/include \
|
4 |
+
-I../third/x86/include/ncnn/ \
|
5 |
+
-I../third/x86/include/onnx/ \
|
6 |
+
-I../third/x86/include/turbojpeg/ \
|
7 |
+
wavcache.cpp \
|
8 |
+
jmat.cpp \
|
9 |
+
wavreader.cpp \
|
10 |
+
wenet.cpp \
|
11 |
+
aimodel.cpp \
|
12 |
+
scrfd.cpp \
|
13 |
+
pfpld.cpp \
|
14 |
+
munet.cpp \
|
15 |
+
blendgram.cpp \
|
16 |
+
face_utils.cpp \
|
17 |
+
-L../third/x86/lib \
|
18 |
+
-lopencv_core -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_videoio \
|
19 |
+
-lonnxruntime -lncnn \
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/face_utils.cpp
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include "face_utils.h"
|
2 |
+
//#include <sys/timeb.h>
|
3 |
+
|
4 |
+
|
5 |
+
cv::Mat resize_image(cv::Mat srcimg, int height, int width, int* top, int* left){
|
6 |
+
cv::Mat dstimg;
|
7 |
+
int srch = srcimg.rows, srcw = srcimg.cols;
|
8 |
+
int neww = width;
|
9 |
+
int newh = height;
|
10 |
+
if (srch != srcw) {
|
11 |
+
float hw_scale = (float)srch / srcw;
|
12 |
+
if (hw_scale > 1) {
|
13 |
+
newh = height;
|
14 |
+
neww = int(width / hw_scale);
|
15 |
+
cv::resize(srcimg, dstimg, cv::Size(neww, newh), cv::INTER_AREA);
|
16 |
+
*left = int((width - neww) * 0.5);
|
17 |
+
cv::copyMakeBorder(dstimg, dstimg, 0, 0, *left, width - neww - *left, cv::BORDER_CONSTANT, 0);
|
18 |
+
}
|
19 |
+
else
|
20 |
+
{
|
21 |
+
newh = (int)height * hw_scale;
|
22 |
+
neww = width;
|
23 |
+
cv::resize(srcimg, dstimg,cv::Size(neww, newh), cv::INTER_AREA);
|
24 |
+
*top = (int)(height - newh) * 0.5;
|
25 |
+
cv::copyMakeBorder(dstimg, dstimg, *top, height - newh - *top, 0, 0, cv::BORDER_CONSTANT, 0);
|
26 |
+
|
27 |
+
}
|
28 |
+
} else {
|
29 |
+
cv::resize(srcimg, dstimg, cv::Size(neww, newh), cv::INTER_AREA);
|
30 |
+
}
|
31 |
+
return dstimg;
|
32 |
+
}
|
33 |
+
|
34 |
+
|
35 |
+
int dumpfile(char* file,char** pbuf){
|
36 |
+
std::string fname(file);
|
37 |
+
std::ifstream cache(fname,std::ios::binary);
|
38 |
+
cache.seekg(0,std::ios::end);
|
39 |
+
const int engSize = cache.tellg();
|
40 |
+
cache.seekg(0,std::ios::beg);
|
41 |
+
char *modelMem = (char*)malloc(engSize+8000);
|
42 |
+
cache.read(modelMem,engSize);
|
43 |
+
cache.close();
|
44 |
+
*pbuf = modelMem;
|
45 |
+
return engSize;
|
46 |
+
}
|
47 |
+
|
48 |
+
void dumpchar(char* abuf,int len){
|
49 |
+
uint8_t* buf = (uint8_t*)abuf;
|
50 |
+
printf("\n----------------------chardump------------------------\n");
|
51 |
+
int i;
|
52 |
+
for(i = 0; i < len; i++) {
|
53 |
+
printf("=%u=", buf[i]);
|
54 |
+
if( (i+1) % 16 == 0) {
|
55 |
+
printf("\n");
|
56 |
+
}
|
57 |
+
}
|
58 |
+
if(i%16 != 0) {
|
59 |
+
printf("\n");
|
60 |
+
}
|
61 |
+
printf("\n----------------------chardump------------------------\n");
|
62 |
+
}
|
63 |
+
|
64 |
+
|
65 |
+
void dumpfloat(float* abuf,int len){
|
66 |
+
printf("\n----------------------floatdump------------------------\n");
|
67 |
+
int i;
|
68 |
+
for(i = 0; i < len; i++) {
|
69 |
+
printf("=%f=", abuf[i]);
|
70 |
+
if( (i+1) % 16 == 0) {
|
71 |
+
printf("\n");
|
72 |
+
}
|
73 |
+
}
|
74 |
+
if(i%16 != 0) {
|
75 |
+
printf("\n");
|
76 |
+
}
|
77 |
+
printf("\n----------------------floatdump------------------------\n");
|
78 |
+
}
|
79 |
+
|
80 |
+
void dumphex(char* abuf,int len){
|
81 |
+
unsigned char* buf = (unsigned char*)abuf;
|
82 |
+
int i = 0;
|
83 |
+
printf("\n----------------------hexdump------------------------\n");
|
84 |
+
for(i = 0; i < len; i++) {
|
85 |
+
printf("=%02x=", buf[i]);
|
86 |
+
if( (i+1) % 16 == 0) {
|
87 |
+
printf("\n");
|
88 |
+
}
|
89 |
+
}
|
90 |
+
if(i%16 != 0) {
|
91 |
+
printf("\n");
|
92 |
+
}
|
93 |
+
printf("---------------------hexdump-------------------------\n\n");
|
94 |
+
}
|
95 |
+
|
96 |
+
int diffbuf(char* abuf,char* bbuf,int size){
|
97 |
+
char* pa = abuf;
|
98 |
+
char* pb = bbuf;
|
99 |
+
int diff = 0;
|
100 |
+
for(int k= 0;k<size;k++){
|
101 |
+
if(*pa++==*pb++){
|
102 |
+
}else{
|
103 |
+
diff++;
|
104 |
+
}
|
105 |
+
}
|
106 |
+
return diff;
|
107 |
+
}
|
108 |
+
|
109 |
+
uint64_t timer_msstamp() {
|
110 |
+
struct timespec ts;
|
111 |
+
clock_gettime(CLOCK_MONOTONIC, &ts);
|
112 |
+
return (ts.tv_sec*1000l) + (ts.tv_nsec/CLOCKS_PER_SEC);
|
113 |
+
}
|
114 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/face_utils.h
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
#include <fstream>
|
3 |
+
#include <sstream>
|
4 |
+
#include <iostream>
|
5 |
+
#include <vector>
|
6 |
+
//#include <opencv2/dnn.hpp>
|
7 |
+
#include <opencv2/imgproc.hpp>
|
8 |
+
#include <opencv2/highgui.hpp>
|
9 |
+
|
10 |
+
cv::Mat resize_image(cv::Mat srcimg, int height, int width, int* top, int* left);
|
11 |
+
|
12 |
+
void dumpchar(char* abuf,int len);
|
13 |
+
void dumphex(char* abuf,int len);
|
14 |
+
void dumpfloat(float* abuf,int len);
|
15 |
+
void dumpdouble(double* abuf,int len);
|
16 |
+
int dumpfile(char* file,char** pbuf);
|
17 |
+
int diffbuf(char* abuf,char* bbuf,int size);
|
18 |
+
|
19 |
+
uint64_t timer_msstamp();
|
20 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/jlog.h
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
#include <stdio.h>
|
3 |
+
|
4 |
+
|
5 |
+
#ifdef __cplusplus
|
6 |
+
extern "C" {
|
7 |
+
#endif
|
8 |
+
|
9 |
+
|
10 |
+
#ifdef LIB_JNI
|
11 |
+
#include <jni.h>
|
12 |
+
#include <android/log.h>
|
13 |
+
|
14 |
+
#define LOG_TAG "tooken"
|
15 |
+
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
|
16 |
+
#define LOGE(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
17 |
+
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
18 |
+
|
19 |
+
|
20 |
+
#else
|
21 |
+
|
22 |
+
#define LOGE(...) printf(__VA_ARGS__)
|
23 |
+
#define LOGI(...) printf(__VA_ARGS__)
|
24 |
+
#define LOGD(...) printf(__VA_ARGS__)
|
25 |
+
|
26 |
+
#endif
|
27 |
+
|
28 |
+
#ifdef __cplusplus
|
29 |
+
}
|
30 |
+
#endif
|
31 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/jmat.cpp
ADDED
@@ -0,0 +1,411 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include "jmat.h"
|
2 |
+
|
3 |
+
extern "C"{
|
4 |
+
#pragma pack(push)
|
5 |
+
#pragma pack(4)
|
6 |
+
|
7 |
+
typedef struct _gpg_hdr {
|
8 |
+
char head[4];
|
9 |
+
int box[4];
|
10 |
+
int size[4];
|
11 |
+
int width[4];
|
12 |
+
int height[4];
|
13 |
+
uint8_t channel[4];
|
14 |
+
uint8_t bit[4];
|
15 |
+
}gpg_hdr;
|
16 |
+
#pragma pack(pop)
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
int JBuf::zeros(){
|
21 |
+
memset(m_buf,0,m_size);
|
22 |
+
return m_size;
|
23 |
+
}
|
24 |
+
|
25 |
+
int JBuf::forceref(int bref){
|
26 |
+
if(m_ref!=bref){
|
27 |
+
m_ref = bref;
|
28 |
+
}
|
29 |
+
return 0;
|
30 |
+
}
|
31 |
+
|
32 |
+
JBuf::JBuf(uint32_t size,void* buf ){
|
33 |
+
if(buf){
|
34 |
+
m_ref = true;
|
35 |
+
m_buf = buf;
|
36 |
+
m_size = size;
|
37 |
+
}else{
|
38 |
+
m_ref = false;
|
39 |
+
m_size = size;
|
40 |
+
m_buf = malloc(size+1024);
|
41 |
+
}
|
42 |
+
}
|
43 |
+
|
44 |
+
JBuf::~JBuf(){
|
45 |
+
if(!m_ref){
|
46 |
+
free(m_buf);
|
47 |
+
m_buf = nullptr;
|
48 |
+
}
|
49 |
+
}
|
50 |
+
|
51 |
+
JBuf::JBuf(){
|
52 |
+
m_size = 0;
|
53 |
+
m_buf = nullptr;
|
54 |
+
}
|
55 |
+
|
56 |
+
JMat::JMat(){
|
57 |
+
init_tagarr();
|
58 |
+
}
|
59 |
+
|
60 |
+
void JMat::init_tagarr(){
|
61 |
+
memset(m_tagarr,0,512*sizeof(int));
|
62 |
+
}
|
63 |
+
|
64 |
+
int* JMat::tagarr(){
|
65 |
+
return m_tagarr;
|
66 |
+
}
|
67 |
+
|
68 |
+
int JMat::savegpg(std::string gpgfile){
|
69 |
+
gpg_hdr ghead;
|
70 |
+
memset(&ghead,0,sizeof(gpg_hdr));
|
71 |
+
ghead.head[0]='g';
|
72 |
+
ghead.head[1]='p';
|
73 |
+
ghead.head[2]='g';
|
74 |
+
ghead.head[3]='1';
|
75 |
+
ghead.size[0]=m_size;
|
76 |
+
ghead.width[0]=m_width;
|
77 |
+
ghead.height[0]=m_height;
|
78 |
+
ghead.channel[0]=m_channel;
|
79 |
+
ghead.bit[0]=m_bit;
|
80 |
+
|
81 |
+
FILE *gpgFile = NULL;
|
82 |
+
const char* fn = gpgfile.c_str();
|
83 |
+
if ((gpgFile = fopen(fn, "wb")) == NULL)return -1;
|
84 |
+
fwrite(&ghead,sizeof(gpg_hdr),1,gpgFile);
|
85 |
+
fwrite(m_buf, m_size, 1, gpgFile);
|
86 |
+
fclose(gpgFile);
|
87 |
+
return 0;
|
88 |
+
}
|
89 |
+
|
90 |
+
int JMat::load(std::string picfile){
|
91 |
+
const char* fn = picfile.c_str();
|
92 |
+
int len = strlen(fn);
|
93 |
+
if(len<4)return -1;
|
94 |
+
fn+= len-3;
|
95 |
+
int gpg = (fn[0]=='g')&&(fn[1]=='p')&&(fn[2]=='g');
|
96 |
+
if(gpg){
|
97 |
+
return loadgpg(picfile);
|
98 |
+
}else{
|
99 |
+
return loadjpg(picfile);
|
100 |
+
}
|
101 |
+
|
102 |
+
}
|
103 |
+
|
104 |
+
int JMat::loadgpg(std::string gpgfile){
|
105 |
+
FILE *gpgFile = NULL;
|
106 |
+
const char* fn = gpgfile.c_str();
|
107 |
+
if ((gpgFile = fopen(fn, "rb")) == NULL)return -1;
|
108 |
+
int rst = 0;
|
109 |
+
while(1){
|
110 |
+
gpg_hdr ghead;
|
111 |
+
memset(&ghead,0,sizeof(gpg_hdr));
|
112 |
+
fread(&ghead,sizeof(gpg_hdr),1,gpgFile);
|
113 |
+
char* arr=ghead.head;
|
114 |
+
if((arr[0]=='g')&&
|
115 |
+
(arr[1]=='p')&&
|
116 |
+
(arr[2]=='g')){
|
117 |
+
|
118 |
+
int imgSize = ghead.size[0];
|
119 |
+
if(m_size<imgSize){
|
120 |
+
if((!m_ref)&&m_buf)free(m_buf);
|
121 |
+
m_buf = malloc(imgSize);
|
122 |
+
}
|
123 |
+
m_size = imgSize;
|
124 |
+
m_width = ghead.width[0];
|
125 |
+
m_height = ghead.height[0];
|
126 |
+
m_channel = ghead.channel[0];
|
127 |
+
m_bit = ghead.bit[0];
|
128 |
+
fread(m_buf, m_size, 1, gpgFile);
|
129 |
+
}else{
|
130 |
+
rst = -11;
|
131 |
+
}
|
132 |
+
break;
|
133 |
+
}
|
134 |
+
fclose(gpgFile);
|
135 |
+
return rst;
|
136 |
+
}
|
137 |
+
|
138 |
+
#ifdef USE_TURBOJPG
|
139 |
+
#include "turbojpeg.h"
|
140 |
+
int JMat::loadjpg(std::string picfile,int flag){
|
141 |
+
tjhandle tjInstance = NULL;
|
142 |
+
int rst = 0;
|
143 |
+
size_t jpegSize = 0;
|
144 |
+
size_t imgSize = 0;
|
145 |
+
int newbuf = 0;
|
146 |
+
unsigned char *jpegBuf = NULL;
|
147 |
+
if(1){
|
148 |
+
long size;
|
149 |
+
FILE *jpegFile = NULL;
|
150 |
+
const char* fn = picfile.c_str();
|
151 |
+
if ((jpegFile = fopen(fn, "rb")) == NULL)return -1;
|
152 |
+
if (fseek(jpegFile, 0, SEEK_END) < 0 || ((size = ftell(jpegFile)) < 0) || (fseek(jpegFile, 0, SEEK_SET) < 0)){
|
153 |
+
fclose(jpegFile);
|
154 |
+
return -2;
|
155 |
+
}
|
156 |
+
if (size == 0){
|
157 |
+
fclose(jpegFile);
|
158 |
+
return -3;
|
159 |
+
}
|
160 |
+
jpegSize = size;
|
161 |
+
jpegBuf = (unsigned char*)tj3Alloc(jpegSize);
|
162 |
+
fread(jpegBuf, jpegSize, 1, jpegFile);
|
163 |
+
fclose(jpegFile);
|
164 |
+
}
|
165 |
+
if ((tjInstance = tj3Init(TJINIT_DECOMPRESS)) == NULL)return -11;
|
166 |
+
while(1){
|
167 |
+
unsigned char *imgBuf = NULL;
|
168 |
+
int w, h;
|
169 |
+
int inSubsamp, inColorspace;
|
170 |
+
int pixelFormat = TJPF_BGR;
|
171 |
+
rst = tj3DecompressHeader(tjInstance, jpegBuf, jpegSize);
|
172 |
+
if(rst<0){
|
173 |
+
rst = -12;
|
174 |
+
break;
|
175 |
+
}
|
176 |
+
w = tj3Get(tjInstance, TJPARAM_JPEGWIDTH);
|
177 |
+
h = tj3Get(tjInstance, TJPARAM_JPEGHEIGHT);
|
178 |
+
inSubsamp = tj3Get(tjInstance, TJPARAM_SUBSAMP);
|
179 |
+
inColorspace = tj3Get(tjInstance, TJPARAM_COLORSPACE);
|
180 |
+
imgSize = w * h * tjPixelSize[pixelFormat];
|
181 |
+
if(imgSize <0){
|
182 |
+
rst = -13;
|
183 |
+
break;
|
184 |
+
}
|
185 |
+
//printf("===imgSize %d m_size %d\n",imgSize,m_size);
|
186 |
+
if(m_size<imgSize){
|
187 |
+
if((!m_ref)&&m_buf)free(m_buf);
|
188 |
+
m_buf = malloc(imgSize);
|
189 |
+
m_ref = 0;
|
190 |
+
}
|
191 |
+
m_size = imgSize;
|
192 |
+
imgBuf = (unsigned char *)m_buf;
|
193 |
+
if(tj3Decompress8(tjInstance, jpegBuf, jpegSize, imgBuf, 0, pixelFormat) < 0){
|
194 |
+
rst = -15;
|
195 |
+
break;
|
196 |
+
}
|
197 |
+
//m_ref = 0;
|
198 |
+
m_bit = 1;
|
199 |
+
m_channel = 3;
|
200 |
+
m_stride = w*3;
|
201 |
+
m_width = w;
|
202 |
+
m_height = h;
|
203 |
+
break;
|
204 |
+
}
|
205 |
+
if(jpegBuf)tj3Free(jpegBuf);
|
206 |
+
jpegBuf = NULL;
|
207 |
+
tj3Destroy(tjInstance);
|
208 |
+
tjInstance = NULL;
|
209 |
+
return rst;
|
210 |
+
}
|
211 |
+
|
212 |
+
#else
|
213 |
+
int JMat::loadjpg(std::string picfile,int flag){
|
214 |
+
return -1;
|
215 |
+
}
|
216 |
+
#endif
|
217 |
+
|
218 |
+
JMat::JMat(int w,int h,float *buf ,int c ,int d ):JBuf(){
|
219 |
+
m_bit = sizeof(float);
|
220 |
+
m_width = w;
|
221 |
+
m_height = h;
|
222 |
+
m_channel = c;
|
223 |
+
m_stride = d?d:w*c;
|
224 |
+
m_size = m_bit*m_stride*m_height;
|
225 |
+
m_buf = buf;
|
226 |
+
m_ref = 1;
|
227 |
+
init_tagarr();
|
228 |
+
}
|
229 |
+
|
230 |
+
JMat::JMat(int w,int h,uint8_t *buf ,int c ,int d ):JBuf(){
|
231 |
+
m_bit = 1;
|
232 |
+
m_width = w;
|
233 |
+
m_height = h;
|
234 |
+
m_channel = c;
|
235 |
+
m_stride = d?d:w*c;
|
236 |
+
m_size = m_bit*m_stride*m_height;
|
237 |
+
m_buf = buf;
|
238 |
+
m_ref = 1;
|
239 |
+
init_tagarr();
|
240 |
+
}
|
241 |
+
|
242 |
+
JMat::JMat(int w,int h,int c ,int d ,int b):JBuf(){
|
243 |
+
m_bit = b==0?sizeof(float):b;
|
244 |
+
m_width = w;
|
245 |
+
m_height = h;
|
246 |
+
m_channel = c;
|
247 |
+
m_stride = d?d:w*c;
|
248 |
+
m_size = m_bit*m_stride*m_height;
|
249 |
+
//printf("===mat %d size %d\n",m_bit,m_size);
|
250 |
+
m_buf = malloc(m_size+m_bit*m_stride);
|
251 |
+
memset(m_buf,0,m_size+m_bit*m_stride);
|
252 |
+
m_ref = 0;
|
253 |
+
init_tagarr();
|
254 |
+
}
|
255 |
+
|
256 |
+
#ifdef USE_OPENCV
|
257 |
+
|
258 |
+
cv::Mat JMat::cvmat(){
|
259 |
+
if(m_channel == 3){
|
260 |
+
cv::Mat rrr(m_height,m_width,m_bit==1?CV_8UC3:CV_32FC3,m_buf);
|
261 |
+
return rrr;
|
262 |
+
}else if(m_channel == 1){
|
263 |
+
cv::Mat rrr(m_height,m_width,m_bit==1?CV_8UC1:CV_32FC1,m_buf);
|
264 |
+
return rrr;
|
265 |
+
}else{
|
266 |
+
cv::Mat rrr(m_height,m_width*m_channel,m_bit==1?CV_8UC1:CV_32FC1,m_buf);
|
267 |
+
return rrr;
|
268 |
+
}
|
269 |
+
}
|
270 |
+
|
271 |
+
int JMat::show(const char* title){
|
272 |
+
std::string name(title);
|
273 |
+
cv::Mat mat(m_height,m_width,m_channel==3?CV_8UC3:CV_8UC1,m_buf);
|
274 |
+
cv::imshow(name,mat);
|
275 |
+
return 0;
|
276 |
+
}
|
277 |
+
int JMat::tojpg(const char* fn){
|
278 |
+
cv::Mat mat(m_height,m_width,CV_8UC3,m_buf);
|
279 |
+
std::string name(fn);
|
280 |
+
return cv::imwrite(name,mat);
|
281 |
+
}
|
282 |
+
#else
|
283 |
+
int JMat::show(const char* title){
|
284 |
+
return 0;
|
285 |
+
}
|
286 |
+
int JMat::tojpg(const char* fn){
|
287 |
+
return 0;
|
288 |
+
}
|
289 |
+
#endif
|
290 |
+
|
291 |
+
|
292 |
+
int JMat::tobin(const char* fn){
|
293 |
+
FILE* file = fopen(fn, "w");
|
294 |
+
if(!file)return 0;
|
295 |
+
fwrite(m_buf, m_size, 1, file);
|
296 |
+
fclose(file);
|
297 |
+
return 1;
|
298 |
+
}
|
299 |
+
|
300 |
+
JMat* JMat::refclone(int ref){
|
301 |
+
if(ref){
|
302 |
+
if(m_bit==1){
|
303 |
+
return new JMat(m_width,m_height,(uint8_t*)m_buf,m_channel,m_stride);
|
304 |
+
}else{
|
305 |
+
return new JMat(m_width,m_height,(float*)m_buf,m_channel,m_stride);
|
306 |
+
}
|
307 |
+
}else{
|
308 |
+
JMat* cm = new JMat(m_width,m_height,m_channel,m_stride,m_bit);
|
309 |
+
memcpy(cm->m_buf,m_buf,m_size);
|
310 |
+
memcpy(cm->m_tagarr,m_tagarr,512*sizeof(int));
|
311 |
+
return cm;
|
312 |
+
}
|
313 |
+
}
|
314 |
+
|
315 |
+
JMat JMat::clone(){
|
316 |
+
JMat cm(m_width,m_height,m_channel,m_stride,m_bit);
|
317 |
+
//printf("==clone %d\n",m_size);
|
318 |
+
memcpy(cm.m_buf,m_buf,m_size);
|
319 |
+
memcpy(cm.m_tagarr,m_tagarr,512*sizeof(int));
|
320 |
+
return cm;
|
321 |
+
}
|
322 |
+
|
323 |
+
#ifdef USE_OPENCV
|
324 |
+
JMat::JMat(std::string picfile,int flag):JBuf(){
|
325 |
+
cv::Mat image = cv::imread(picfile);
|
326 |
+
m_bit = flag?1:sizeof(float);
|
327 |
+
m_width = image.cols;
|
328 |
+
m_height = image.rows;
|
329 |
+
m_channel = 3;//image.channels();
|
330 |
+
//printf("===channels %d\n",m_channel);
|
331 |
+
m_stride = m_width*m_channel;
|
332 |
+
m_size = m_bit*m_stride*m_height;
|
333 |
+
m_buf = malloc(m_size+m_bit*m_stride);
|
334 |
+
m_ref = 0;
|
335 |
+
if(flag){
|
336 |
+
memcpy(m_buf,image.data,m_size);
|
337 |
+
//printf("===w %d h %d\n",image.cols,image.rows);
|
338 |
+
//cv::imshow("aaa",image);
|
339 |
+
//cv::waitKey(0);
|
340 |
+
//cv::Mat fmat(m_height,m_width,CV_8UC3,m_buf);
|
341 |
+
//float scale = 1.0f/255.0f;
|
342 |
+
//image.convertTo(fmat,CV_32F,scale);
|
343 |
+
}else{
|
344 |
+
cv::Mat fmat(m_height,m_width,CV_32FC3,m_buf);
|
345 |
+
float scale = 1.0f/255.0f;
|
346 |
+
image.convertTo(fmat,CV_32F,scale);
|
347 |
+
}
|
348 |
+
image.release();
|
349 |
+
init_tagarr();
|
350 |
+
}
|
351 |
+
#else
|
352 |
+
JMat::JMat(std::string picfile,int flag):JBuf(){
|
353 |
+
|
354 |
+
}
|
355 |
+
#endif
|
356 |
+
|
357 |
+
JMat::~JMat(){
|
358 |
+
}
|
359 |
+
|
360 |
+
float* JMat::fdata(){
|
361 |
+
return (float*)m_buf;
|
362 |
+
}
|
363 |
+
|
364 |
+
float* JMat::frow(int row){
|
365 |
+
return ((float*)m_buf)+ row*m_stride;
|
366 |
+
}
|
367 |
+
|
368 |
+
float* JMat::fitem(int row,int col){
|
369 |
+
return ((float*)m_buf)+ row*m_stride + col;
|
370 |
+
|
371 |
+
}
|
372 |
+
|
373 |
+
|
374 |
+
uint8_t* JMat::udata(){
|
375 |
+
return (uint8_t*)m_buf;
|
376 |
+
}
|
377 |
+
/*
|
378 |
+
nc::NdArray<float> JMat::ncarray(){
|
379 |
+
bool own = false;
|
380 |
+
nc::NdArray<float> arr = nc::NdArray<float>((float*)m_buf, m_height, m_width, own);
|
381 |
+
return arr;
|
382 |
+
}
|
383 |
+
*/
|
384 |
+
|
385 |
+
|
386 |
+
#ifdef USE_NCNN
|
387 |
+
ncnn::Mat JMat::packingmat(){
|
388 |
+
ncnn::Mat in_pack(m_width,m_height,1,(void*)m_buf,(size_t)4u*3,3);
|
389 |
+
ncnn::Mat in ;
|
390 |
+
ncnn::convert_packing(in_pack,in,1);
|
391 |
+
return in;
|
392 |
+
}
|
393 |
+
|
394 |
+
ncnn::Mat JMat::ncnnmat(){
|
395 |
+
unsigned char* data = (unsigned char*)m_buf;
|
396 |
+
if(m_channel == 3){
|
397 |
+
ncnn::Mat mat = ncnn::Mat::from_pixels(data, ncnn::Mat::PIXEL_BGR, m_width, m_height);
|
398 |
+
return mat;
|
399 |
+
}else if(m_channel == 4){
|
400 |
+
ncnn::Mat mat = ncnn::Mat::from_pixels(data, ncnn::Mat::PIXEL_BGRA, m_width, m_height);
|
401 |
+
return mat;
|
402 |
+
}else if(m_channel == 1){
|
403 |
+
ncnn::Mat mat = ncnn::Mat::from_pixels(data, ncnn::Mat::PIXEL_GRAY, m_width, m_height);
|
404 |
+
return mat;
|
405 |
+
}else {
|
406 |
+
ncnn::Mat mat = ncnn::Mat::from_pixels(data, ncnn::Mat::PIXEL_GRAY, m_width*m_channel, m_height);
|
407 |
+
return mat;
|
408 |
+
}
|
409 |
+
}
|
410 |
+
#endif
|
411 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/jmat.h
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
#include <stdio.h>
|
3 |
+
#include <stdlib.h>
|
4 |
+
#include <stdio.h>
|
5 |
+
#include <memory>
|
6 |
+
#include <string>
|
7 |
+
#include <vector>
|
8 |
+
#include <string.h>
|
9 |
+
|
10 |
+
//#include "NumCpp.hpp"
|
11 |
+
#define USE_OPENCV
|
12 |
+
#define USE_NCNN
|
13 |
+
#define USE_TURBOJPG
|
14 |
+
|
15 |
+
#ifdef USE_OPENCV
|
16 |
+
#include "opencv2/core.hpp"
|
17 |
+
#include "opencv2/imgproc.hpp"
|
18 |
+
#include "opencv2/highgui.hpp"
|
19 |
+
#endif
|
20 |
+
|
21 |
+
#ifdef USE_NCNN
|
22 |
+
#include "mat.h"
|
23 |
+
#endif
|
24 |
+
|
25 |
+
#ifdef USE_EIGEN
|
26 |
+
#include "eigen3/Eigen/Core"
|
27 |
+
typedef Eigen::Matrix<float, 1, Eigen::Dynamic, Eigen::RowMajor> Vectorf;
|
28 |
+
typedef Eigen::Matrix<std::complex<float>, 1, Eigen::Dynamic, Eigen::RowMajor> Vectorcf;
|
29 |
+
typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> Matrixf;
|
30 |
+
typedef Eigen::Matrix<std::complex<float>, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> Matrixcf;
|
31 |
+
#endif
|
32 |
+
|
33 |
+
class JBuf{
|
34 |
+
protected:
|
35 |
+
bool m_ref = 0;
|
36 |
+
uint32_t m_size = 0;
|
37 |
+
void* m_buf = NULL;
|
38 |
+
public:
|
39 |
+
uint32_t size(){return m_size;} ;
|
40 |
+
void* data(){return m_buf;};
|
41 |
+
bool ref(){return m_ref;};
|
42 |
+
int zeros();
|
43 |
+
int forceref(int bref);
|
44 |
+
JBuf();
|
45 |
+
JBuf(uint32_t size,void* buf = nullptr);
|
46 |
+
virtual ~JBuf();
|
47 |
+
};
|
48 |
+
|
49 |
+
class JMat:public JBuf{
|
50 |
+
protected:
|
51 |
+
int m_bit = 0;
|
52 |
+
int m_width = 0;
|
53 |
+
int m_height = 0;
|
54 |
+
int m_channel = 0;
|
55 |
+
int m_stride = 0;
|
56 |
+
int m_tagarr[512];
|
57 |
+
void init_tagarr();
|
58 |
+
public:
|
59 |
+
int height(){return m_height;}
|
60 |
+
int width(){return m_width;}
|
61 |
+
int stride(){return m_stride;}
|
62 |
+
int channel(){return m_channel;}
|
63 |
+
JMat(int w,int h,float *buf ,int c = 3 ,int d = 0);
|
64 |
+
JMat(int w,int h,uint8_t *buf ,int c = 3 ,int d = 0);
|
65 |
+
JMat(int w,int h,int c = 3,int d = 0,int b=0);
|
66 |
+
JMat(std::string picfile,int flag=0);
|
67 |
+
JMat();
|
68 |
+
int load(std::string picfile);
|
69 |
+
int loadjpg(std::string picfile,int flag=0);
|
70 |
+
int savegpg(std::string gpgfile);
|
71 |
+
int loadgpg(std::string gpgfile);
|
72 |
+
float* fdata();
|
73 |
+
float* frow(int row);
|
74 |
+
float* fitem(int row,int col);
|
75 |
+
int tojpg(const char* fn);
|
76 |
+
int tobin(const char* fn);
|
77 |
+
int show(const char* title);
|
78 |
+
JMat clone();
|
79 |
+
JMat* refclone(int ref=1);
|
80 |
+
uint8_t* udata();
|
81 |
+
virtual ~JMat();
|
82 |
+
int* tagarr();
|
83 |
+
//nc::NdArray<float> ncarray();
|
84 |
+
#ifdef USE_OPENCV
|
85 |
+
cv::Mat cvmat();
|
86 |
+
#endif
|
87 |
+
#ifdef USE_NCNN
|
88 |
+
ncnn::Mat ncnnmat();
|
89 |
+
ncnn::Mat packingmat();
|
90 |
+
#endif
|
91 |
+
//Matrixf tomatrix();
|
92 |
+
};
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/malpha.cpp
ADDED
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include "malpha.h"
|
2 |
+
#include "blendgram.h"
|
3 |
+
#include "face_utils.h"
|
4 |
+
|
5 |
+
MWorkMat::MWorkMat(JMat* pic,JMat* msk,const int* boxs){
|
6 |
+
m_boxx = boxs[0];
|
7 |
+
m_boxy=boxs[1];
|
8 |
+
m_boxwidth=boxs[2]-m_boxx;
|
9 |
+
m_boxheight=boxs[3]-m_boxy;
|
10 |
+
//printf("x %d y %d w %d h %d \n",m_boxx,m_boxy,m_boxwidth,m_boxheight);
|
11 |
+
m_pic = pic;
|
12 |
+
m_msk = msk;
|
13 |
+
|
14 |
+
pic_real160 = new JMat(160,160,3,0,1);
|
15 |
+
pic_mask160 = new JMat(160,160,3,0,1);
|
16 |
+
//pic_crop160 = new JMat(160,160,3,0,1);
|
17 |
+
|
18 |
+
msk_real160 = new JMat(160,160,1,0,1);
|
19 |
+
|
20 |
+
//msk_mask160 = new JMat(160,160,3,0,1);
|
21 |
+
|
22 |
+
}
|
23 |
+
|
24 |
+
MWorkMat::~MWorkMat(){
|
25 |
+
matpic_org168.release();
|
26 |
+
matpic_roirst.release();
|
27 |
+
delete pic_real160;
|
28 |
+
delete pic_mask160;
|
29 |
+
delete msk_real160;
|
30 |
+
if(pic_clone160) delete pic_clone160;
|
31 |
+
}
|
32 |
+
|
33 |
+
int MWorkMat::munet(JMat** ppic,JMat** pmsk){
|
34 |
+
*ppic = pic_real160;
|
35 |
+
*pmsk = pic_mask160;
|
36 |
+
return 0;
|
37 |
+
}
|
38 |
+
|
39 |
+
int MWorkMat::premunet(){
|
40 |
+
matpic_roisrc = cv::Mat(m_pic->cvmat(),cv::Rect(m_boxx,m_boxy,m_boxwidth,m_boxheight));
|
41 |
+
cv::resize(matpic_roisrc , matpic_org168, cv::Size(168, 168), cv::INTER_AREA);
|
42 |
+
//vtacc
|
43 |
+
matpic_roi160 = cv::Mat(matpic_org168,cv::Rect(4,4,160,160));
|
44 |
+
cv::Mat cvmask = pic_mask160->cvmat();
|
45 |
+
cv::Mat cvreal = pic_real160->cvmat();
|
46 |
+
matpic_roi160.copyTo(cvmask);
|
47 |
+
matpic_roi160.copyTo(cvreal);
|
48 |
+
//cv::rectangle(cvmask,cv::Rect(5,5,150,150),cv::Scalar(0,0,0),-1);//,cv::LineTypes::FILLED);
|
49 |
+
cv::rectangle(cvmask,cv::Rect(5,5,150,145),cv::Scalar(0,0,0),-1);//,cv::LineTypes::FILLED);
|
50 |
+
//cv::rectangle(cvmask,cv::Rect(4,4,152,152),cv::Scalar(0,0,0),-1);//,cv::LineTypes::FILLED);
|
51 |
+
//cv::imwrite("cvmask.bmp",cvmask);
|
52 |
+
//cv::waitKey(0);
|
53 |
+
pic_clone160 = pic_real160->refclone(0);
|
54 |
+
return 0;
|
55 |
+
}
|
56 |
+
|
57 |
+
int MWorkMat::finmunet(JMat* fgpic){
|
58 |
+
cv::Mat cvreal = pic_real160->cvmat();
|
59 |
+
cvreal.copyTo(matpic_roi160);
|
60 |
+
//cv::imwrite("accpre.bmp",matpic_org168);
|
61 |
+
if(m_msk) vtacc((uint8_t*)matpic_org168.data,168*168);
|
62 |
+
//cv::imwrite("accend.bmp",matpic_org168);
|
63 |
+
cv::resize(matpic_org168, matpic_roirst, cv::Size(m_boxwidth, m_boxheight), cv::INTER_AREA);
|
64 |
+
if(fgpic){
|
65 |
+
matpic_roisrc = cv::Mat(fgpic->cvmat(),cv::Rect(m_boxx,m_boxy,m_boxwidth,m_boxheight));
|
66 |
+
matpic_roirst.copyTo(matpic_roisrc);
|
67 |
+
}else{
|
68 |
+
matpic_roirst.copyTo(matpic_roisrc);
|
69 |
+
}
|
70 |
+
return 0;
|
71 |
+
}
|
72 |
+
|
73 |
+
int MWorkMat::alpha(JMat** preal,JMat** pimg,JMat** pmsk){
|
74 |
+
*preal = pic_clone160;
|
75 |
+
*pimg = pic_real160;
|
76 |
+
*pmsk = msk_real160;
|
77 |
+
return 0;
|
78 |
+
}
|
79 |
+
|
80 |
+
int MWorkMat::prealpha(){
|
81 |
+
printf("x %d y %d w %d h %d \n",m_boxx,m_boxy,m_boxwidth,m_boxheight);
|
82 |
+
//m_msk->show("cba");
|
83 |
+
//cv::waitKey(0);
|
84 |
+
matmsk_roisrc = cv::Mat(m_msk->cvmat(),cv::Rect(m_boxx,m_boxy,m_boxwidth,m_boxheight));
|
85 |
+
cv::resize(matmsk_roisrc , matmsk_org168, cv::Size(168, 168), cv::INTER_AREA);
|
86 |
+
|
87 |
+
matmsk_roi160 = cv::Mat(matmsk_org168,cv::Rect(4,4,160,160));
|
88 |
+
cv::Mat cvmask = msk_real160->cvmat();
|
89 |
+
cv::cvtColor(matmsk_roi160,cvmask,cv::COLOR_RGB2GRAY);
|
90 |
+
|
91 |
+
//BlendGramAlphaRev(pic_clone160->udata(),msk_real160->udata(),pic_crop160->udata(),160,160);
|
92 |
+
//pic_crop160->show("aaa");
|
93 |
+
//cv::waitKey(0);
|
94 |
+
//pic_crop160
|
95 |
+
//
|
96 |
+
return 0;
|
97 |
+
}
|
98 |
+
|
99 |
+
int MWorkMat::finalpha(){
|
100 |
+
cv::Mat cvmask = msk_real160->cvmat();
|
101 |
+
cv::cvtColor(cvmask,matmsk_roi160,cv::COLOR_GRAY2RGB);
|
102 |
+
//
|
103 |
+
cv::resize(matmsk_org168, matmsk_roirst, cv::Size(m_boxwidth, m_boxheight), cv::INTER_AREA);
|
104 |
+
matmsk_roirst.copyTo(matmsk_roisrc);
|
105 |
+
return 0;
|
106 |
+
}
|
107 |
+
|
108 |
+
int MWorkMat::vtacc(uint8_t* buf,int count){
|
109 |
+
/*
|
110 |
+
int avgr = 0;
|
111 |
+
int avgb = 0;
|
112 |
+
int avgg = 0;
|
113 |
+
if(1){
|
114 |
+
uint8_t* pb = m_pic->udata();
|
115 |
+
for(int k=0;k<10;k++){
|
116 |
+
avgr += *pb++;
|
117 |
+
avgg += *pb++;
|
118 |
+
avgb += *pb++;
|
119 |
+
}
|
120 |
+
avgr =avgr/10 +10;
|
121 |
+
avgg =avgg/10 -20;
|
122 |
+
if(avgg<0)avgg=0;
|
123 |
+
avgb =avgb/10 + 10;
|
124 |
+
}
|
125 |
+
*/
|
126 |
+
uint8_t* pb = buf;
|
127 |
+
for(int k=0;k<count;k++){
|
128 |
+
int sum = (pb[0]+ pb[2])/2.0f;
|
129 |
+
if(pb[1]>=sum){
|
130 |
+
pb[1]=sum;
|
131 |
+
//pb[0]=0;
|
132 |
+
//pb[2]=0;
|
133 |
+
// }else if((pb[0]<avgr)&&(pb[1]>avgg)&&(pb[2]<avgb)){
|
134 |
+
//pb[1]=0;
|
135 |
+
//pb[0]=0;
|
136 |
+
//pb[2]=0;
|
137 |
+
}
|
138 |
+
pb+=3;
|
139 |
+
}
|
140 |
+
/*
|
141 |
+
long sum = 0l;
|
142 |
+
float mean = sum*0.5f/count;
|
143 |
+
uint8_t maxg = (mean>255.f)?255:mean;
|
144 |
+
//printf("sum %ld mean %f maxg %d\n",sum,mean,maxg);
|
145 |
+
//getchar();
|
146 |
+
pb = buf +1;
|
147 |
+
for(int k=0;k<count;k++){
|
148 |
+
if(*pb>maxg){
|
149 |
+
*pb = maxg;
|
150 |
+
}
|
151 |
+
pb+=3;
|
152 |
+
}
|
153 |
+
*/
|
154 |
+
return 0;
|
155 |
+
}
|
156 |
+
|
157 |
+
int MAlpha::doModel(JMat* real,JMat* img,JMat* pha){
|
158 |
+
if(1)return 0;
|
159 |
+
/*
|
160 |
+
if(1)return 0;
|
161 |
+
JMat picimg(160,160,3,0,1);
|
162 |
+
JMat picreal(160,160,3,0,1);
|
163 |
+
cv::cvtColor(real->cvmat(),picreal.cvmat(),cv::COLOR_RGB2BGR);
|
164 |
+
cv::cvtColor(img->cvmat(),picimg.cvmat(),cv::COLOR_RGB2BGR);
|
165 |
+
*/
|
166 |
+
float mean_vals[3] = {127.5f, 127.5f, 127.5f};
|
167 |
+
float norm_vals[3] = {1 / 127.5f, 1 / 127.5f, 1 / 127.5f};
|
168 |
+
ncnn::Mat inimg = ncnn::Mat::from_pixels(img->udata(), ncnn::Mat::PIXEL_BGR2RGB, 160, 160);
|
169 |
+
inimg.substract_mean_normalize(mean_vals, norm_vals);
|
170 |
+
ncnn::Mat inreal = ncnn::Mat::from_pixels(real->udata(), ncnn::Mat::PIXEL_BGR2RGB, 160, 160);
|
171 |
+
inreal.substract_mean_normalize(mean_vals, norm_vals);
|
172 |
+
ncnn::Mat inpha = ncnn::Mat::from_pixels(pha->udata(), ncnn::Mat::PIXEL_GRAY, 160, 160);
|
173 |
+
float gmean_vals[3] = {0.0f, 0.0f, 0.0f};
|
174 |
+
float gnorm_vals[3] = {1 / 255.0f, 1 / 255.0f, 1 / 255.0f};
|
175 |
+
inpha.substract_mean_normalize(gmean_vals, gnorm_vals);
|
176 |
+
|
177 |
+
ncnn::Mat inpic(160,160,7);
|
178 |
+
//printf("===in %d %d all %d %d pha %d %d\n",inreal.cstep,inreal.elempack,inpic.cstep,inpic.elempack,inpha.cstep,inpha.elempack);
|
179 |
+
//JMat picin(160,160,7);
|
180 |
+
float* buf = (float*)inpic.data;
|
181 |
+
memcpy(buf,inreal.data,inreal.cstep*inreal.c*sizeof(float));
|
182 |
+
buf += inpic.cstep*inreal.c;
|
183 |
+
memcpy(buf,inimg.data,inimg.cstep*inimg.c*sizeof(float));
|
184 |
+
buf += inimg.cstep*inimg.c;
|
185 |
+
memcpy(buf, inpha.data,inpha.cstep*sizeof(float));
|
186 |
+
//ncnn::Mat inpic(160,160,7,pd,4);
|
187 |
+
//ncnn::Mat inpack(160,160,1,pd,(size_t)4u*7,7);
|
188 |
+
//ncnn::Mat inpic;
|
189 |
+
//ncnn::convert_packing(inpack,inpic,1);
|
190 |
+
ncnn::Mat outpic;
|
191 |
+
ncnn::Extractor ex = net.create_extractor();
|
192 |
+
int rst = ex.input("input", inpic);
|
193 |
+
//printf("input %d\n",rst);
|
194 |
+
rst = ex.extract("output", outpic);
|
195 |
+
//printf("output %d\n",rst);
|
196 |
+
float outmean_vals[3] = {0.0f, 0.0f, 0.0f};
|
197 |
+
float outnorm_vals[3] = { 255.0f, 255.0f, 255.0f};
|
198 |
+
outpic.substract_mean_normalize(outmean_vals, outnorm_vals);
|
199 |
+
outpic.to_pixels(pha->udata(),ncnn::Mat::PIXEL_GRAY);
|
200 |
+
//pha->show("mmm");
|
201 |
+
//cv::waitKey(0);
|
202 |
+
//dumpfloat((float*)outpic.data+160*159,10);
|
203 |
+
//ncnn::Mat pakpic;
|
204 |
+
//ncnn::convert_packing(outpic,pakpic,1);
|
205 |
+
//dumpfloat((float*)pakpic.data,160*160*1);
|
206 |
+
//getchar();
|
207 |
+
/*
|
208 |
+
cv::Mat cvadj(160,160,CV_32FC1,outpic.data);
|
209 |
+
cv::Mat cvout;//(160,160,CV_8UC1);
|
210 |
+
float scale = 255.0f;//255.0f;
|
211 |
+
cvadj.convertTo(cvout,CV_8UC1,scale);
|
212 |
+
cvout.copyTo(pha->cvmat());
|
213 |
+
*/
|
214 |
+
//cv::imshow("pha",cvout);
|
215 |
+
//pha->show("pha");
|
216 |
+
//cv::waitKey(0);
|
217 |
+
return 0;
|
218 |
+
}
|
219 |
+
|
220 |
+
MAlpha::MAlpha(const char* fnbin,const char* fnparam):NcnnModel(160,160){
|
221 |
+
std::string fb(fnbin);
|
222 |
+
std::string fp(fnparam);
|
223 |
+
initModel(fb,fp);
|
224 |
+
}
|
225 |
+
|
226 |
+
MAlpha::~MAlpha(){
|
227 |
+
|
228 |
+
}
|
229 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/malpha.h
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
#include "jmat.h"
|
3 |
+
#include "net.h"
|
4 |
+
#include <opencv2/core/core.hpp>
|
5 |
+
#include <opencv2/highgui/highgui.hpp>
|
6 |
+
#include <opencv2/imgproc/imgproc.hpp>
|
7 |
+
#include <stdio.h>
|
8 |
+
#include <vector>
|
9 |
+
#include "aimodel.h"
|
10 |
+
|
11 |
+
class MWorkMat{
|
12 |
+
private:
|
13 |
+
int m_boxx;
|
14 |
+
int m_boxy;
|
15 |
+
int m_boxwidth;
|
16 |
+
int m_boxheight;
|
17 |
+
JMat* m_pic;
|
18 |
+
JMat* m_msk;
|
19 |
+
|
20 |
+
JMat* pic_real160;//blendimg
|
21 |
+
JMat* pic_mask160;
|
22 |
+
|
23 |
+
cv::Mat matpic_roisrc;//box area
|
24 |
+
cv::Mat matpic_org168;
|
25 |
+
cv::Mat matpic_roi160;
|
26 |
+
JMat* pic_clone160;//blendimg
|
27 |
+
cv::Mat matpic_roirst;
|
28 |
+
|
29 |
+
//JMat* pic_crop160;
|
30 |
+
//
|
31 |
+
JMat* msk_real160;
|
32 |
+
//JMat* msk_mask160;
|
33 |
+
|
34 |
+
cv::Mat matmsk_roisrc;//box area
|
35 |
+
cv::Mat matmsk_org168;
|
36 |
+
cv::Mat matmsk_roi160;
|
37 |
+
|
38 |
+
cv::Mat matmsk_roirst;
|
39 |
+
|
40 |
+
int vtacc(uint8_t* buf,int count);
|
41 |
+
public:
|
42 |
+
MWorkMat(JMat* pic,JMat* msk,const int* boxs);
|
43 |
+
int premunet();
|
44 |
+
int munet(JMat** ppic,JMat** pmsk);
|
45 |
+
int finmunet(JMat* fgpic=NULL);
|
46 |
+
int prealpha();
|
47 |
+
int alpha(JMat** preal,JMat** pimg,JMat** pmsk);
|
48 |
+
int finalpha();
|
49 |
+
|
50 |
+
virtual ~MWorkMat();
|
51 |
+
};
|
52 |
+
|
53 |
+
class MAlpha:public NcnnModel{
|
54 |
+
private:
|
55 |
+
public:
|
56 |
+
int doModel(JMat* real,JMat* img,JMat* pha);
|
57 |
+
MAlpha(const char* fnbin,const char* fnparam);
|
58 |
+
virtual ~MAlpha();
|
59 |
+
};
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/mfcc/AudioFFT.hpp
ADDED
@@ -0,0 +1,1264 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
#ifndef _AUDIOFFT_H
|
4 |
+
#define _AUDIOFFT_H
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
#include <cstddef>
|
9 |
+
#include <memory>
|
10 |
+
#include <cassert>
|
11 |
+
#include <cmath>
|
12 |
+
#include <cstring>
|
13 |
+
|
14 |
+
//#define AUDIOFFT_APPLE_ACCELERATE //AUDIOFFT_INTEL_IPP//AUDIOFFT_FFTW3//AUDIOFFT_APPLE_ACCELERATE
|
15 |
+
|
16 |
+
#if defined(AUDIOFFT_INTEL_IPP)
|
17 |
+
#define AUDIOFFT_INTEL_IPP_USED
|
18 |
+
#include <ipp.h>
|
19 |
+
#elif defined(AUDIOFFT_APPLE_ACCELERATE)
|
20 |
+
#define AUDIOFFT_APPLE_ACCELERATE_USED
|
21 |
+
#include <Accelerate/Accelerate.h>
|
22 |
+
#include <vector>
|
23 |
+
#elif defined (AUDIOFFT_FFTW3)
|
24 |
+
#define AUDIOFFT_FFTW3_USED
|
25 |
+
#include <fftw3.h>
|
26 |
+
#else
|
27 |
+
#if !defined(AUDIOFFT_OOURA)
|
28 |
+
#define AUDIOFFT_OOURA
|
29 |
+
#endif
|
30 |
+
#define AUDIOFFT_OOURA_USED
|
31 |
+
#include <vector>
|
32 |
+
#endif
|
33 |
+
|
34 |
+
namespace audiofft
|
35 |
+
{
|
36 |
+
|
37 |
+
namespace detail
|
38 |
+
{
|
39 |
+
class AudioFFTImpl;
|
40 |
+
}
|
41 |
+
|
42 |
+
|
43 |
+
// =============================================================
|
44 |
+
|
45 |
+
|
46 |
+
/**
|
47 |
+
* @class AudioFFT
|
48 |
+
* @brief Performs 1D FFTs
|
49 |
+
*/
|
50 |
+
class AudioFFT
|
51 |
+
{
|
52 |
+
public:
|
53 |
+
/**
|
54 |
+
* @brief Constructor
|
55 |
+
*/
|
56 |
+
AudioFFT();
|
57 |
+
|
58 |
+
AudioFFT(const AudioFFT&) = delete;
|
59 |
+
AudioFFT& operator=(const AudioFFT&) = delete;
|
60 |
+
|
61 |
+
/**
|
62 |
+
* @brief Destructor
|
63 |
+
*/
|
64 |
+
~AudioFFT();
|
65 |
+
|
66 |
+
/**
|
67 |
+
* @brief Initializes the FFT object
|
68 |
+
* @param size Size of the real input (must be power 2)
|
69 |
+
*/
|
70 |
+
void init(size_t size);
|
71 |
+
|
72 |
+
/**
|
73 |
+
* @brief Performs the forward FFT
|
74 |
+
* @param data The real input data (has to be of the length as specified in init())
|
75 |
+
* @param re The real part of the complex output (has to be of length as returned by ComplexSize())
|
76 |
+
* @param im The imaginary part of the complex output (has to be of length as returned by ComplexSize())
|
77 |
+
*/
|
78 |
+
void fft(const float* data, float* re, float* im);
|
79 |
+
|
80 |
+
/**
|
81 |
+
* @brief Performs the inverse FFT
|
82 |
+
* @param data The real output data (has to be of the length as specified in init())
|
83 |
+
* @param re The real part of the complex input (has to be of length as returned by ComplexSize())
|
84 |
+
* @param im The imaginary part of the complex input (has to be of length as returned by ComplexSize())
|
85 |
+
*/
|
86 |
+
void ifft(float* data, const float* re, const float* im);
|
87 |
+
|
88 |
+
/**
|
89 |
+
* @brief Calculates the necessary size of the real/imaginary complex arrays
|
90 |
+
* @param size The size of the real data
|
91 |
+
* @return The size of the real/imaginary complex arrays
|
92 |
+
*/
|
93 |
+
static size_t ComplexSize(size_t size);
|
94 |
+
|
95 |
+
private:
|
96 |
+
std::unique_ptr<detail::AudioFFTImpl> _impl;
|
97 |
+
};
|
98 |
+
|
99 |
+
|
100 |
+
/**
|
101 |
+
* @deprecated
|
102 |
+
* @brief Let's keep an AudioFFTBase type around for now because it has been here already in the 1st version in order to avoid breaking existing code.
|
103 |
+
*/
|
104 |
+
typedef AudioFFT AudioFFTBase;
|
105 |
+
|
106 |
+
} // End of namespace
|
107 |
+
|
108 |
+
|
109 |
+
namespace audiofft
|
110 |
+
{
|
111 |
+
|
112 |
+
namespace detail
|
113 |
+
{
|
114 |
+
|
115 |
+
class AudioFFTImpl
|
116 |
+
{
|
117 |
+
public:
|
118 |
+
AudioFFTImpl() = default;
|
119 |
+
AudioFFTImpl(const AudioFFTImpl&) = delete;
|
120 |
+
AudioFFTImpl& operator=(const AudioFFTImpl&) = delete;
|
121 |
+
virtual ~AudioFFTImpl() = default;
|
122 |
+
virtual void init(size_t size) = 0;
|
123 |
+
virtual void fft(const float* data, float* re, float* im) = 0;
|
124 |
+
virtual void ifft(float* data, const float* re, const float* im) = 0;
|
125 |
+
};
|
126 |
+
|
127 |
+
|
128 |
+
constexpr bool IsPowerOf2(size_t val)
|
129 |
+
{
|
130 |
+
return (val == 1 || (val & (val-1)) == 0);
|
131 |
+
}
|
132 |
+
|
133 |
+
|
134 |
+
template<typename TypeDest, typename TypeSrc>
|
135 |
+
void ConvertBuffer(TypeDest* dest, const TypeSrc* src, size_t len)
|
136 |
+
{
|
137 |
+
for (size_t i=0; i<len; ++i)
|
138 |
+
{
|
139 |
+
dest[i] = static_cast<TypeDest>(src[i]);
|
140 |
+
}
|
141 |
+
}
|
142 |
+
|
143 |
+
|
144 |
+
template<typename TypeDest, typename TypeSrc, typename TypeFactor>
|
145 |
+
void ScaleBuffer(TypeDest* dest, const TypeSrc* src, const TypeFactor factor, size_t len)
|
146 |
+
{
|
147 |
+
for (size_t i=0; i<len; ++i)
|
148 |
+
{
|
149 |
+
dest[i] = static_cast<TypeDest>(static_cast<TypeFactor>(src[i]) * factor);
|
150 |
+
}
|
151 |
+
}
|
152 |
+
|
153 |
+
} // End of namespace detail
|
154 |
+
|
155 |
+
|
156 |
+
// ================================================================
|
157 |
+
|
158 |
+
|
159 |
+
#ifdef AUDIOFFT_OOURA_USED
|
160 |
+
|
161 |
+
/**
|
162 |
+
* @internal
|
163 |
+
* @class OouraFFT
|
164 |
+
* @brief FFT implementation based on the great radix-4 routines by Takuya Ooura
|
165 |
+
*/
|
166 |
+
class OouraFFT : public detail::AudioFFTImpl
|
167 |
+
{
|
168 |
+
public:
|
169 |
+
OouraFFT() :
|
170 |
+
detail::AudioFFTImpl(),
|
171 |
+
_size(0),
|
172 |
+
_ip(),
|
173 |
+
_w(),
|
174 |
+
_buffer()
|
175 |
+
{
|
176 |
+
}
|
177 |
+
|
178 |
+
OouraFFT(const OouraFFT&) = delete;
|
179 |
+
OouraFFT& operator=(const OouraFFT&) = delete;
|
180 |
+
|
181 |
+
virtual void init(size_t size) override
|
182 |
+
{
|
183 |
+
if (_size != size)
|
184 |
+
{
|
185 |
+
_ip.resize(2 + static_cast<int>(std::sqrt(static_cast<double>(size))));
|
186 |
+
_w.resize(size / 2);
|
187 |
+
_buffer.resize(size);
|
188 |
+
_size = size;
|
189 |
+
|
190 |
+
const int size4 = static_cast<int>(_size) / 4;
|
191 |
+
makewt(size4, _ip.data(), _w.data());
|
192 |
+
makect(size4, _ip.data(), _w.data() + size4);
|
193 |
+
}
|
194 |
+
}
|
195 |
+
|
196 |
+
virtual void fft(const float* data, float* re, float* im) override
|
197 |
+
{
|
198 |
+
// Convert into the format as required by the Ooura FFT
|
199 |
+
detail::ConvertBuffer(_buffer.data(), data, _size);
|
200 |
+
|
201 |
+
rdft(static_cast<int>(_size), +1, _buffer.data(), _ip.data(), _w.data());
|
202 |
+
|
203 |
+
// Convert back to split-complex
|
204 |
+
{
|
205 |
+
double* b = _buffer.data();
|
206 |
+
double* bEnd = b + _size;
|
207 |
+
float *r = re;
|
208 |
+
float *i = im;
|
209 |
+
while (b != bEnd)
|
210 |
+
{
|
211 |
+
*(r++) = static_cast<float>(*(b++));
|
212 |
+
*(i++) = static_cast<float>(-(*(b++)));
|
213 |
+
}
|
214 |
+
}
|
215 |
+
const size_t size2 = _size / 2;
|
216 |
+
re[size2] = -im[0];
|
217 |
+
im[0] = 0.0;
|
218 |
+
im[size2] = 0.0;
|
219 |
+
}
|
220 |
+
|
221 |
+
virtual void ifft(float* data, const float* re, const float* im) override
|
222 |
+
{
|
223 |
+
// Convert into the format as required by the Ooura FFT
|
224 |
+
{
|
225 |
+
double* b = _buffer.data();
|
226 |
+
double* bEnd = b + _size;
|
227 |
+
const float *r = re;
|
228 |
+
const float *i = im;
|
229 |
+
while (b != bEnd)
|
230 |
+
{
|
231 |
+
*(b++) = static_cast<double>(*(r++));
|
232 |
+
*(b++) = -static_cast<double>(*(i++));
|
233 |
+
}
|
234 |
+
_buffer[1] = re[_size / 2];
|
235 |
+
}
|
236 |
+
|
237 |
+
rdft(static_cast<int>(_size), -1, _buffer.data(), _ip.data(), _w.data());
|
238 |
+
|
239 |
+
// Convert back to split-complex
|
240 |
+
detail::ScaleBuffer(data, _buffer.data(), 2.0 / static_cast<double>(_size), _size);
|
241 |
+
}
|
242 |
+
|
243 |
+
private:
|
244 |
+
size_t _size;
|
245 |
+
std::vector<int> _ip;
|
246 |
+
std::vector<double> _w;
|
247 |
+
std::vector<double> _buffer;
|
248 |
+
|
249 |
+
void rdft(int n, int isgn, double *a, int *ip, double *w)
|
250 |
+
{
|
251 |
+
int nw = ip[0];
|
252 |
+
int nc = ip[1];
|
253 |
+
|
254 |
+
if (isgn >= 0)
|
255 |
+
{
|
256 |
+
if (n > 4)
|
257 |
+
{
|
258 |
+
bitrv2(n, ip + 2, a);
|
259 |
+
cftfsub(n, a, w);
|
260 |
+
rftfsub(n, a, nc, w + nw);
|
261 |
+
}
|
262 |
+
else if (n == 4)
|
263 |
+
{
|
264 |
+
cftfsub(n, a, w);
|
265 |
+
}
|
266 |
+
double xi = a[0] - a[1];
|
267 |
+
a[0] += a[1];
|
268 |
+
a[1] = xi;
|
269 |
+
}
|
270 |
+
else
|
271 |
+
{
|
272 |
+
a[1] = 0.5 * (a[0] - a[1]);
|
273 |
+
a[0] -= a[1];
|
274 |
+
if (n > 4)
|
275 |
+
{
|
276 |
+
rftbsub(n, a, nc, w + nw);
|
277 |
+
bitrv2(n, ip + 2, a);
|
278 |
+
cftbsub(n, a, w);
|
279 |
+
}
|
280 |
+
else if (n == 4)
|
281 |
+
{
|
282 |
+
cftfsub(n, a, w);
|
283 |
+
}
|
284 |
+
}
|
285 |
+
}
|
286 |
+
|
287 |
+
|
288 |
+
/* -------- initializing routines -------- */
|
289 |
+
|
290 |
+
void makewt(int nw, int *ip, double *w)
|
291 |
+
{
|
292 |
+
int j, nwh;
|
293 |
+
double delta, x, y;
|
294 |
+
|
295 |
+
ip[0] = nw;
|
296 |
+
ip[1] = 1;
|
297 |
+
if (nw > 2) {
|
298 |
+
nwh = nw >> 1;
|
299 |
+
delta = atan(1.0) / nwh;
|
300 |
+
w[0] = 1;
|
301 |
+
w[1] = 0;
|
302 |
+
w[nwh] = cos(delta * nwh);
|
303 |
+
w[nwh + 1] = w[nwh];
|
304 |
+
if (nwh > 2) {
|
305 |
+
for (j = 2; j < nwh; j += 2) {
|
306 |
+
x = cos(delta * j);
|
307 |
+
y = sin(delta * j);
|
308 |
+
w[j] = x;
|
309 |
+
w[j + 1] = y;
|
310 |
+
w[nw - j] = y;
|
311 |
+
w[nw - j + 1] = x;
|
312 |
+
}
|
313 |
+
bitrv2(nw, ip + 2, w);
|
314 |
+
}
|
315 |
+
}
|
316 |
+
}
|
317 |
+
|
318 |
+
|
319 |
+
void makect(int nc, int *ip, double *c)
|
320 |
+
{
|
321 |
+
int j, nch;
|
322 |
+
double delta;
|
323 |
+
|
324 |
+
ip[1] = nc;
|
325 |
+
if (nc > 1) {
|
326 |
+
nch = nc >> 1;
|
327 |
+
delta = atan(1.0) / nch;
|
328 |
+
c[0] = cos(delta * nch);
|
329 |
+
c[nch] = 0.5 * c[0];
|
330 |
+
for (j = 1; j < nch; j++) {
|
331 |
+
c[j] = 0.5 * cos(delta * j);
|
332 |
+
c[nc - j] = 0.5 * sin(delta * j);
|
333 |
+
}
|
334 |
+
}
|
335 |
+
}
|
336 |
+
|
337 |
+
|
338 |
+
/* -------- child routines -------- */
|
339 |
+
|
340 |
+
|
341 |
+
void bitrv2(int n, int *ip, double *a)
|
342 |
+
{
|
343 |
+
int j, j1, k, k1, l, m, m2;
|
344 |
+
double xr, xi, yr, yi;
|
345 |
+
|
346 |
+
ip[0] = 0;
|
347 |
+
l = n;
|
348 |
+
m = 1;
|
349 |
+
while ((m << 3) < l) {
|
350 |
+
l >>= 1;
|
351 |
+
for (j = 0; j < m; j++) {
|
352 |
+
ip[m + j] = ip[j] + l;
|
353 |
+
}
|
354 |
+
m <<= 1;
|
355 |
+
}
|
356 |
+
m2 = 2 * m;
|
357 |
+
if ((m << 3) == l) {
|
358 |
+
for (k = 0; k < m; k++) {
|
359 |
+
for (j = 0; j < k; j++) {
|
360 |
+
j1 = 2 * j + ip[k];
|
361 |
+
k1 = 2 * k + ip[j];
|
362 |
+
xr = a[j1];
|
363 |
+
xi = a[j1 + 1];
|
364 |
+
yr = a[k1];
|
365 |
+
yi = a[k1 + 1];
|
366 |
+
a[j1] = yr;
|
367 |
+
a[j1 + 1] = yi;
|
368 |
+
a[k1] = xr;
|
369 |
+
a[k1 + 1] = xi;
|
370 |
+
j1 += m2;
|
371 |
+
k1 += 2 * m2;
|
372 |
+
xr = a[j1];
|
373 |
+
xi = a[j1 + 1];
|
374 |
+
yr = a[k1];
|
375 |
+
yi = a[k1 + 1];
|
376 |
+
a[j1] = yr;
|
377 |
+
a[j1 + 1] = yi;
|
378 |
+
a[k1] = xr;
|
379 |
+
a[k1 + 1] = xi;
|
380 |
+
j1 += m2;
|
381 |
+
k1 -= m2;
|
382 |
+
xr = a[j1];
|
383 |
+
xi = a[j1 + 1];
|
384 |
+
yr = a[k1];
|
385 |
+
yi = a[k1 + 1];
|
386 |
+
a[j1] = yr;
|
387 |
+
a[j1 + 1] = yi;
|
388 |
+
a[k1] = xr;
|
389 |
+
a[k1 + 1] = xi;
|
390 |
+
j1 += m2;
|
391 |
+
k1 += 2 * m2;
|
392 |
+
xr = a[j1];
|
393 |
+
xi = a[j1 + 1];
|
394 |
+
yr = a[k1];
|
395 |
+
yi = a[k1 + 1];
|
396 |
+
a[j1] = yr;
|
397 |
+
a[j1 + 1] = yi;
|
398 |
+
a[k1] = xr;
|
399 |
+
a[k1 + 1] = xi;
|
400 |
+
}
|
401 |
+
j1 = 2 * k + m2 + ip[k];
|
402 |
+
k1 = j1 + m2;
|
403 |
+
xr = a[j1];
|
404 |
+
xi = a[j1 + 1];
|
405 |
+
yr = a[k1];
|
406 |
+
yi = a[k1 + 1];
|
407 |
+
a[j1] = yr;
|
408 |
+
a[j1 + 1] = yi;
|
409 |
+
a[k1] = xr;
|
410 |
+
a[k1 + 1] = xi;
|
411 |
+
}
|
412 |
+
} else {
|
413 |
+
for (k = 1; k < m; k++) {
|
414 |
+
for (j = 0; j < k; j++) {
|
415 |
+
j1 = 2 * j + ip[k];
|
416 |
+
k1 = 2 * k + ip[j];
|
417 |
+
xr = a[j1];
|
418 |
+
xi = a[j1 + 1];
|
419 |
+
yr = a[k1];
|
420 |
+
yi = a[k1 + 1];
|
421 |
+
a[j1] = yr;
|
422 |
+
a[j1 + 1] = yi;
|
423 |
+
a[k1] = xr;
|
424 |
+
a[k1 + 1] = xi;
|
425 |
+
j1 += m2;
|
426 |
+
k1 += m2;
|
427 |
+
xr = a[j1];
|
428 |
+
xi = a[j1 + 1];
|
429 |
+
yr = a[k1];
|
430 |
+
yi = a[k1 + 1];
|
431 |
+
a[j1] = yr;
|
432 |
+
a[j1 + 1] = yi;
|
433 |
+
a[k1] = xr;
|
434 |
+
a[k1 + 1] = xi;
|
435 |
+
}
|
436 |
+
}
|
437 |
+
}
|
438 |
+
}
|
439 |
+
|
440 |
+
|
441 |
+
void cftfsub(int n, double *a, double *w)
|
442 |
+
{
|
443 |
+
int j, j1, j2, j3, l;
|
444 |
+
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
|
445 |
+
|
446 |
+
l = 2;
|
447 |
+
if (n > 8) {
|
448 |
+
cft1st(n, a, w);
|
449 |
+
l = 8;
|
450 |
+
while ((l << 2) < n) {
|
451 |
+
cftmdl(n, l, a, w);
|
452 |
+
l <<= 2;
|
453 |
+
}
|
454 |
+
}
|
455 |
+
if ((l << 2) == n) {
|
456 |
+
for (j = 0; j < l; j += 2) {
|
457 |
+
j1 = j + l;
|
458 |
+
j2 = j1 + l;
|
459 |
+
j3 = j2 + l;
|
460 |
+
x0r = a[j] + a[j1];
|
461 |
+
x0i = a[j + 1] + a[j1 + 1];
|
462 |
+
x1r = a[j] - a[j1];
|
463 |
+
x1i = a[j + 1] - a[j1 + 1];
|
464 |
+
x2r = a[j2] + a[j3];
|
465 |
+
x2i = a[j2 + 1] + a[j3 + 1];
|
466 |
+
x3r = a[j2] - a[j3];
|
467 |
+
x3i = a[j2 + 1] - a[j3 + 1];
|
468 |
+
a[j] = x0r + x2r;
|
469 |
+
a[j + 1] = x0i + x2i;
|
470 |
+
a[j2] = x0r - x2r;
|
471 |
+
a[j2 + 1] = x0i - x2i;
|
472 |
+
a[j1] = x1r - x3i;
|
473 |
+
a[j1 + 1] = x1i + x3r;
|
474 |
+
a[j3] = x1r + x3i;
|
475 |
+
a[j3 + 1] = x1i - x3r;
|
476 |
+
}
|
477 |
+
} else {
|
478 |
+
for (j = 0; j < l; j += 2) {
|
479 |
+
j1 = j + l;
|
480 |
+
x0r = a[j] - a[j1];
|
481 |
+
x0i = a[j + 1] - a[j1 + 1];
|
482 |
+
a[j] += a[j1];
|
483 |
+
a[j + 1] += a[j1 + 1];
|
484 |
+
a[j1] = x0r;
|
485 |
+
a[j1 + 1] = x0i;
|
486 |
+
}
|
487 |
+
}
|
488 |
+
}
|
489 |
+
|
490 |
+
|
491 |
+
void cftbsub(int n, double *a, double *w)
|
492 |
+
{
|
493 |
+
int j, j1, j2, j3, l;
|
494 |
+
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
|
495 |
+
|
496 |
+
l = 2;
|
497 |
+
if (n > 8) {
|
498 |
+
cft1st(n, a, w);
|
499 |
+
l = 8;
|
500 |
+
while ((l << 2) < n) {
|
501 |
+
cftmdl(n, l, a, w);
|
502 |
+
l <<= 2;
|
503 |
+
}
|
504 |
+
}
|
505 |
+
if ((l << 2) == n) {
|
506 |
+
for (j = 0; j < l; j += 2) {
|
507 |
+
j1 = j + l;
|
508 |
+
j2 = j1 + l;
|
509 |
+
j3 = j2 + l;
|
510 |
+
x0r = a[j] + a[j1];
|
511 |
+
x0i = -a[j + 1] - a[j1 + 1];
|
512 |
+
x1r = a[j] - a[j1];
|
513 |
+
x1i = -a[j + 1] + a[j1 + 1];
|
514 |
+
x2r = a[j2] + a[j3];
|
515 |
+
x2i = a[j2 + 1] + a[j3 + 1];
|
516 |
+
x3r = a[j2] - a[j3];
|
517 |
+
x3i = a[j2 + 1] - a[j3 + 1];
|
518 |
+
a[j] = x0r + x2r;
|
519 |
+
a[j + 1] = x0i - x2i;
|
520 |
+
a[j2] = x0r - x2r;
|
521 |
+
a[j2 + 1] = x0i + x2i;
|
522 |
+
a[j1] = x1r - x3i;
|
523 |
+
a[j1 + 1] = x1i - x3r;
|
524 |
+
a[j3] = x1r + x3i;
|
525 |
+
a[j3 + 1] = x1i + x3r;
|
526 |
+
}
|
527 |
+
} else {
|
528 |
+
for (j = 0; j < l; j += 2) {
|
529 |
+
j1 = j + l;
|
530 |
+
x0r = a[j] - a[j1];
|
531 |
+
x0i = -a[j + 1] + a[j1 + 1];
|
532 |
+
a[j] += a[j1];
|
533 |
+
a[j + 1] = -a[j + 1] - a[j1 + 1];
|
534 |
+
a[j1] = x0r;
|
535 |
+
a[j1 + 1] = x0i;
|
536 |
+
}
|
537 |
+
}
|
538 |
+
}
|
539 |
+
|
540 |
+
|
541 |
+
void cft1st(int n, double *a, double *w)
|
542 |
+
{
|
543 |
+
int j, k1, k2;
|
544 |
+
double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
|
545 |
+
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
|
546 |
+
|
547 |
+
x0r = a[0] + a[2];
|
548 |
+
x0i = a[1] + a[3];
|
549 |
+
x1r = a[0] - a[2];
|
550 |
+
x1i = a[1] - a[3];
|
551 |
+
x2r = a[4] + a[6];
|
552 |
+
x2i = a[5] + a[7];
|
553 |
+
x3r = a[4] - a[6];
|
554 |
+
x3i = a[5] - a[7];
|
555 |
+
a[0] = x0r + x2r;
|
556 |
+
a[1] = x0i + x2i;
|
557 |
+
a[4] = x0r - x2r;
|
558 |
+
a[5] = x0i - x2i;
|
559 |
+
a[2] = x1r - x3i;
|
560 |
+
a[3] = x1i + x3r;
|
561 |
+
a[6] = x1r + x3i;
|
562 |
+
a[7] = x1i - x3r;
|
563 |
+
wk1r = w[2];
|
564 |
+
x0r = a[8] + a[10];
|
565 |
+
x0i = a[9] + a[11];
|
566 |
+
x1r = a[8] - a[10];
|
567 |
+
x1i = a[9] - a[11];
|
568 |
+
x2r = a[12] + a[14];
|
569 |
+
x2i = a[13] + a[15];
|
570 |
+
x3r = a[12] - a[14];
|
571 |
+
x3i = a[13] - a[15];
|
572 |
+
a[8] = x0r + x2r;
|
573 |
+
a[9] = x0i + x2i;
|
574 |
+
a[12] = x2i - x0i;
|
575 |
+
a[13] = x0r - x2r;
|
576 |
+
x0r = x1r - x3i;
|
577 |
+
x0i = x1i + x3r;
|
578 |
+
a[10] = wk1r * (x0r - x0i);
|
579 |
+
a[11] = wk1r * (x0r + x0i);
|
580 |
+
x0r = x3i + x1r;
|
581 |
+
x0i = x3r - x1i;
|
582 |
+
a[14] = wk1r * (x0i - x0r);
|
583 |
+
a[15] = wk1r * (x0i + x0r);
|
584 |
+
k1 = 0;
|
585 |
+
for (j = 16; j < n; j += 16) {
|
586 |
+
k1 += 2;
|
587 |
+
k2 = 2 * k1;
|
588 |
+
wk2r = w[k1];
|
589 |
+
wk2i = w[k1 + 1];
|
590 |
+
wk1r = w[k2];
|
591 |
+
wk1i = w[k2 + 1];
|
592 |
+
wk3r = wk1r - 2 * wk2i * wk1i;
|
593 |
+
wk3i = 2 * wk2i * wk1r - wk1i;
|
594 |
+
x0r = a[j] + a[j + 2];
|
595 |
+
x0i = a[j + 1] + a[j + 3];
|
596 |
+
x1r = a[j] - a[j + 2];
|
597 |
+
x1i = a[j + 1] - a[j + 3];
|
598 |
+
x2r = a[j + 4] + a[j + 6];
|
599 |
+
x2i = a[j + 5] + a[j + 7];
|
600 |
+
x3r = a[j + 4] - a[j + 6];
|
601 |
+
x3i = a[j + 5] - a[j + 7];
|
602 |
+
a[j] = x0r + x2r;
|
603 |
+
a[j + 1] = x0i + x2i;
|
604 |
+
x0r -= x2r;
|
605 |
+
x0i -= x2i;
|
606 |
+
a[j + 4] = wk2r * x0r - wk2i * x0i;
|
607 |
+
a[j + 5] = wk2r * x0i + wk2i * x0r;
|
608 |
+
x0r = x1r - x3i;
|
609 |
+
x0i = x1i + x3r;
|
610 |
+
a[j + 2] = wk1r * x0r - wk1i * x0i;
|
611 |
+
a[j + 3] = wk1r * x0i + wk1i * x0r;
|
612 |
+
x0r = x1r + x3i;
|
613 |
+
x0i = x1i - x3r;
|
614 |
+
a[j + 6] = wk3r * x0r - wk3i * x0i;
|
615 |
+
a[j + 7] = wk3r * x0i + wk3i * x0r;
|
616 |
+
wk1r = w[k2 + 2];
|
617 |
+
wk1i = w[k2 + 3];
|
618 |
+
wk3r = wk1r - 2 * wk2r * wk1i;
|
619 |
+
wk3i = 2 * wk2r * wk1r - wk1i;
|
620 |
+
x0r = a[j + 8] + a[j + 10];
|
621 |
+
x0i = a[j + 9] + a[j + 11];
|
622 |
+
x1r = a[j + 8] - a[j + 10];
|
623 |
+
x1i = a[j + 9] - a[j + 11];
|
624 |
+
x2r = a[j + 12] + a[j + 14];
|
625 |
+
x2i = a[j + 13] + a[j + 15];
|
626 |
+
x3r = a[j + 12] - a[j + 14];
|
627 |
+
x3i = a[j + 13] - a[j + 15];
|
628 |
+
a[j + 8] = x0r + x2r;
|
629 |
+
a[j + 9] = x0i + x2i;
|
630 |
+
x0r -= x2r;
|
631 |
+
x0i -= x2i;
|
632 |
+
a[j + 12] = -wk2i * x0r - wk2r * x0i;
|
633 |
+
a[j + 13] = -wk2i * x0i + wk2r * x0r;
|
634 |
+
x0r = x1r - x3i;
|
635 |
+
x0i = x1i + x3r;
|
636 |
+
a[j + 10] = wk1r * x0r - wk1i * x0i;
|
637 |
+
a[j + 11] = wk1r * x0i + wk1i * x0r;
|
638 |
+
x0r = x1r + x3i;
|
639 |
+
x0i = x1i - x3r;
|
640 |
+
a[j + 14] = wk3r * x0r - wk3i * x0i;
|
641 |
+
a[j + 15] = wk3r * x0i + wk3i * x0r;
|
642 |
+
}
|
643 |
+
}
|
644 |
+
|
645 |
+
|
646 |
+
void cftmdl(int n, int l, double *a, double *w)
|
647 |
+
{
|
648 |
+
int j, j1, j2, j3, k, k1, k2, m, m2;
|
649 |
+
double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
|
650 |
+
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
|
651 |
+
|
652 |
+
m = l << 2;
|
653 |
+
for (j = 0; j < l; j += 2) {
|
654 |
+
j1 = j + l;
|
655 |
+
j2 = j1 + l;
|
656 |
+
j3 = j2 + l;
|
657 |
+
x0r = a[j] + a[j1];
|
658 |
+
x0i = a[j + 1] + a[j1 + 1];
|
659 |
+
x1r = a[j] - a[j1];
|
660 |
+
x1i = a[j + 1] - a[j1 + 1];
|
661 |
+
x2r = a[j2] + a[j3];
|
662 |
+
x2i = a[j2 + 1] + a[j3 + 1];
|
663 |
+
x3r = a[j2] - a[j3];
|
664 |
+
x3i = a[j2 + 1] - a[j3 + 1];
|
665 |
+
a[j] = x0r + x2r;
|
666 |
+
a[j + 1] = x0i + x2i;
|
667 |
+
a[j2] = x0r - x2r;
|
668 |
+
a[j2 + 1] = x0i - x2i;
|
669 |
+
a[j1] = x1r - x3i;
|
670 |
+
a[j1 + 1] = x1i + x3r;
|
671 |
+
a[j3] = x1r + x3i;
|
672 |
+
a[j3 + 1] = x1i - x3r;
|
673 |
+
}
|
674 |
+
wk1r = w[2];
|
675 |
+
for (j = m; j < l + m; j += 2) {
|
676 |
+
j1 = j + l;
|
677 |
+
j2 = j1 + l;
|
678 |
+
j3 = j2 + l;
|
679 |
+
x0r = a[j] + a[j1];
|
680 |
+
x0i = a[j + 1] + a[j1 + 1];
|
681 |
+
x1r = a[j] - a[j1];
|
682 |
+
x1i = a[j + 1] - a[j1 + 1];
|
683 |
+
x2r = a[j2] + a[j3];
|
684 |
+
x2i = a[j2 + 1] + a[j3 + 1];
|
685 |
+
x3r = a[j2] - a[j3];
|
686 |
+
x3i = a[j2 + 1] - a[j3 + 1];
|
687 |
+
a[j] = x0r + x2r;
|
688 |
+
a[j + 1] = x0i + x2i;
|
689 |
+
a[j2] = x2i - x0i;
|
690 |
+
a[j2 + 1] = x0r - x2r;
|
691 |
+
x0r = x1r - x3i;
|
692 |
+
x0i = x1i + x3r;
|
693 |
+
a[j1] = wk1r * (x0r - x0i);
|
694 |
+
a[j1 + 1] = wk1r * (x0r + x0i);
|
695 |
+
x0r = x3i + x1r;
|
696 |
+
x0i = x3r - x1i;
|
697 |
+
a[j3] = wk1r * (x0i - x0r);
|
698 |
+
a[j3 + 1] = wk1r * (x0i + x0r);
|
699 |
+
}
|
700 |
+
k1 = 0;
|
701 |
+
m2 = 2 * m;
|
702 |
+
for (k = m2; k < n; k += m2) {
|
703 |
+
k1 += 2;
|
704 |
+
k2 = 2 * k1;
|
705 |
+
wk2r = w[k1];
|
706 |
+
wk2i = w[k1 + 1];
|
707 |
+
wk1r = w[k2];
|
708 |
+
wk1i = w[k2 + 1];
|
709 |
+
wk3r = wk1r - 2 * wk2i * wk1i;
|
710 |
+
wk3i = 2 * wk2i * wk1r - wk1i;
|
711 |
+
for (j = k; j < l + k; j += 2) {
|
712 |
+
j1 = j + l;
|
713 |
+
j2 = j1 + l;
|
714 |
+
j3 = j2 + l;
|
715 |
+
x0r = a[j] + a[j1];
|
716 |
+
x0i = a[j + 1] + a[j1 + 1];
|
717 |
+
x1r = a[j] - a[j1];
|
718 |
+
x1i = a[j + 1] - a[j1 + 1];
|
719 |
+
x2r = a[j2] + a[j3];
|
720 |
+
x2i = a[j2 + 1] + a[j3 + 1];
|
721 |
+
x3r = a[j2] - a[j3];
|
722 |
+
x3i = a[j2 + 1] - a[j3 + 1];
|
723 |
+
a[j] = x0r + x2r;
|
724 |
+
a[j + 1] = x0i + x2i;
|
725 |
+
x0r -= x2r;
|
726 |
+
x0i -= x2i;
|
727 |
+
a[j2] = wk2r * x0r - wk2i * x0i;
|
728 |
+
a[j2 + 1] = wk2r * x0i + wk2i * x0r;
|
729 |
+
x0r = x1r - x3i;
|
730 |
+
x0i = x1i + x3r;
|
731 |
+
a[j1] = wk1r * x0r - wk1i * x0i;
|
732 |
+
a[j1 + 1] = wk1r * x0i + wk1i * x0r;
|
733 |
+
x0r = x1r + x3i;
|
734 |
+
x0i = x1i - x3r;
|
735 |
+
a[j3] = wk3r * x0r - wk3i * x0i;
|
736 |
+
a[j3 + 1] = wk3r * x0i + wk3i * x0r;
|
737 |
+
}
|
738 |
+
wk1r = w[k2 + 2];
|
739 |
+
wk1i = w[k2 + 3];
|
740 |
+
wk3r = wk1r - 2 * wk2r * wk1i;
|
741 |
+
wk3i = 2 * wk2r * wk1r - wk1i;
|
742 |
+
for (j = k + m; j < l + (k + m); j += 2) {
|
743 |
+
j1 = j + l;
|
744 |
+
j2 = j1 + l;
|
745 |
+
j3 = j2 + l;
|
746 |
+
x0r = a[j] + a[j1];
|
747 |
+
x0i = a[j + 1] + a[j1 + 1];
|
748 |
+
x1r = a[j] - a[j1];
|
749 |
+
x1i = a[j + 1] - a[j1 + 1];
|
750 |
+
x2r = a[j2] + a[j3];
|
751 |
+
x2i = a[j2 + 1] + a[j3 + 1];
|
752 |
+
x3r = a[j2] - a[j3];
|
753 |
+
x3i = a[j2 + 1] - a[j3 + 1];
|
754 |
+
a[j] = x0r + x2r;
|
755 |
+
a[j + 1] = x0i + x2i;
|
756 |
+
x0r -= x2r;
|
757 |
+
x0i -= x2i;
|
758 |
+
a[j2] = -wk2i * x0r - wk2r * x0i;
|
759 |
+
a[j2 + 1] = -wk2i * x0i + wk2r * x0r;
|
760 |
+
x0r = x1r - x3i;
|
761 |
+
x0i = x1i + x3r;
|
762 |
+
a[j1] = wk1r * x0r - wk1i * x0i;
|
763 |
+
a[j1 + 1] = wk1r * x0i + wk1i * x0r;
|
764 |
+
x0r = x1r + x3i;
|
765 |
+
x0i = x1i - x3r;
|
766 |
+
a[j3] = wk3r * x0r - wk3i * x0i;
|
767 |
+
a[j3 + 1] = wk3r * x0i + wk3i * x0r;
|
768 |
+
}
|
769 |
+
}
|
770 |
+
}
|
771 |
+
|
772 |
+
|
773 |
+
void rftfsub(int n, double *a, int nc, double *c)
|
774 |
+
{
|
775 |
+
int j, k, kk, ks, m;
|
776 |
+
double wkr, wki, xr, xi, yr, yi;
|
777 |
+
|
778 |
+
m = n >> 1;
|
779 |
+
ks = 2 * nc / m;
|
780 |
+
kk = 0;
|
781 |
+
for (j = 2; j < m; j += 2) {
|
782 |
+
k = n - j;
|
783 |
+
kk += ks;
|
784 |
+
wkr = 0.5 - c[nc - kk];
|
785 |
+
wki = c[kk];
|
786 |
+
xr = a[j] - a[k];
|
787 |
+
xi = a[j + 1] + a[k + 1];
|
788 |
+
yr = wkr * xr - wki * xi;
|
789 |
+
yi = wkr * xi + wki * xr;
|
790 |
+
a[j] -= yr;
|
791 |
+
a[j + 1] -= yi;
|
792 |
+
a[k] += yr;
|
793 |
+
a[k + 1] -= yi;
|
794 |
+
}
|
795 |
+
}
|
796 |
+
|
797 |
+
|
798 |
+
void rftbsub(int n, double *a, int nc, double *c)
|
799 |
+
{
|
800 |
+
int j, k, kk, ks, m;
|
801 |
+
double wkr, wki, xr, xi, yr, yi;
|
802 |
+
|
803 |
+
a[1] = -a[1];
|
804 |
+
m = n >> 1;
|
805 |
+
ks = 2 * nc / m;
|
806 |
+
kk = 0;
|
807 |
+
for (j = 2; j < m; j += 2) {
|
808 |
+
k = n - j;
|
809 |
+
kk += ks;
|
810 |
+
wkr = 0.5 - c[nc - kk];
|
811 |
+
wki = c[kk];
|
812 |
+
xr = a[j] - a[k];
|
813 |
+
xi = a[j + 1] + a[k + 1];
|
814 |
+
yr = wkr * xr + wki * xi;
|
815 |
+
yi = wkr * xi - wki * xr;
|
816 |
+
a[j] -= yr;
|
817 |
+
a[j + 1] = yi - a[j + 1];
|
818 |
+
a[k] += yr;
|
819 |
+
a[k + 1] = yi - a[k + 1];
|
820 |
+
}
|
821 |
+
a[m + 1] = -a[m + 1];
|
822 |
+
}
|
823 |
+
};
|
824 |
+
|
825 |
+
|
826 |
+
/**
|
827 |
+
* @internal
|
828 |
+
* @brief Concrete FFT implementation
|
829 |
+
*/
|
830 |
+
typedef OouraFFT AudioFFTImplementation;
|
831 |
+
|
832 |
+
|
833 |
+
#endif // AUDIOFFT_OOURA_USED
|
834 |
+
|
835 |
+
|
836 |
+
// ================================================================
|
837 |
+
|
838 |
+
|
839 |
+
#ifdef AUDIOFFT_INTEL_IPP_USED
|
840 |
+
|
841 |
+
|
842 |
+
/**
|
843 |
+
* @internal
|
844 |
+
* @class IntelIppFFT
|
845 |
+
* @brief FFT implementation using the Intel Integrated Performance Primitives
|
846 |
+
*/
|
847 |
+
class IntelIppFFT : public detail::AudioFFTImpl
|
848 |
+
{
|
849 |
+
public:
|
850 |
+
IntelIppFFT() :
|
851 |
+
detail::AudioFFTImpl(),
|
852 |
+
_size(0),
|
853 |
+
_operationalBufferSize(0),
|
854 |
+
_powerOf2(0),
|
855 |
+
_fftSpec(nullptr),
|
856 |
+
_fftSpecBuf(0),
|
857 |
+
_fftWorkBuf(0),
|
858 |
+
_operationalBuffer(nullptr)
|
859 |
+
{
|
860 |
+
ippInit();
|
861 |
+
}
|
862 |
+
|
863 |
+
IntelIppFFT(const IntelIppFFT&) = delete;
|
864 |
+
IntelIppFFT& operator=(const IntelIppFFT&) = delete;
|
865 |
+
|
866 |
+
virtual ~IntelIppFFT()
|
867 |
+
{
|
868 |
+
init(0);
|
869 |
+
}
|
870 |
+
|
871 |
+
virtual void init(size_t size) override
|
872 |
+
{
|
873 |
+
if (_fftSpec)
|
874 |
+
{
|
875 |
+
if (_fftWorkBuf) ippFree(_fftWorkBuf);
|
876 |
+
if (_fftSpecBuf) ippFree(_fftSpecBuf);
|
877 |
+
ippFree(_operationalBuffer);
|
878 |
+
|
879 |
+
_size = 0;
|
880 |
+
_operationalBufferSize = 0;
|
881 |
+
_powerOf2 = 0;
|
882 |
+
_fftSpec = 0;
|
883 |
+
}
|
884 |
+
|
885 |
+
if (size > 0)
|
886 |
+
{
|
887 |
+
_size = size;
|
888 |
+
_operationalBufferSize = _size + 2;
|
889 |
+
_powerOf2 = (int)(log((double)_size)/log(2.0));
|
890 |
+
|
891 |
+
// Query to get buffer sizes
|
892 |
+
int sizeFFTSpec,
|
893 |
+
sizeFFTInitBuf,
|
894 |
+
sizeFFTWorkBuf;
|
895 |
+
ippsFFTGetSize_R_32f(
|
896 |
+
_powerOf2,
|
897 |
+
IPP_FFT_NODIV_BY_ANY,
|
898 |
+
ippAlgHintAccurate,
|
899 |
+
&sizeFFTSpec,
|
900 |
+
&sizeFFTInitBuf,
|
901 |
+
&sizeFFTWorkBuf
|
902 |
+
);
|
903 |
+
|
904 |
+
Ipp8u* fftInitBuf;
|
905 |
+
|
906 |
+
// init buffers
|
907 |
+
_fftSpecBuf = ippsMalloc_8u(sizeFFTSpec);
|
908 |
+
_fftWorkBuf = ippsMalloc_8u(sizeFFTWorkBuf);
|
909 |
+
fftInitBuf = ippsMalloc_8u(sizeFFTInitBuf);
|
910 |
+
|
911 |
+
// Initialize FFT
|
912 |
+
ippsFFTInit_R_32f(
|
913 |
+
&_fftSpec,
|
914 |
+
_powerOf2,
|
915 |
+
IPP_FFT_NODIV_BY_ANY,
|
916 |
+
ippAlgHintAccurate,
|
917 |
+
_fftSpecBuf,
|
918 |
+
fftInitBuf
|
919 |
+
);
|
920 |
+
if (fftInitBuf) ippFree(fftInitBuf);
|
921 |
+
|
922 |
+
// init operational buffer
|
923 |
+
_operationalBuffer = ippsMalloc_32f(
|
924 |
+
_operationalBufferSize
|
925 |
+
);
|
926 |
+
}
|
927 |
+
}
|
928 |
+
|
929 |
+
virtual void fft(const float* data, float* re, float* im) override
|
930 |
+
{
|
931 |
+
size_t complexNumbersCount = _operationalBufferSize / 2;
|
932 |
+
ippsFFTFwd_RToCCS_32f(
|
933 |
+
data,
|
934 |
+
_operationalBuffer,
|
935 |
+
_fftSpec,
|
936 |
+
_fftWorkBuf
|
937 |
+
);
|
938 |
+
|
939 |
+
// no need to scale
|
940 |
+
|
941 |
+
size_t complexCounter = 0;
|
942 |
+
for (int i = 0; i < complexNumbersCount; ++i)
|
943 |
+
{
|
944 |
+
re[i] = _operationalBuffer[complexCounter++];
|
945 |
+
im[i] = _operationalBuffer[complexCounter++];
|
946 |
+
}
|
947 |
+
}
|
948 |
+
|
949 |
+
virtual void ifft(float* data, const float* re, const float* im) override
|
950 |
+
{
|
951 |
+
size_t complexNumbersCount = _operationalBufferSize / 2;
|
952 |
+
|
953 |
+
size_t complexCounter = 0;
|
954 |
+
for (int i = 0; i < complexNumbersCount; ++i)
|
955 |
+
{
|
956 |
+
_operationalBuffer[complexCounter++] = re[i];
|
957 |
+
_operationalBuffer[complexCounter++] = im[i];
|
958 |
+
}
|
959 |
+
|
960 |
+
ippsFFTInv_CCSToR_32f(
|
961 |
+
_operationalBuffer,
|
962 |
+
data,
|
963 |
+
_fftSpec,
|
964 |
+
_fftWorkBuf
|
965 |
+
);
|
966 |
+
|
967 |
+
// scaling
|
968 |
+
const float factor = 1.0f / static_cast<float>(_size);
|
969 |
+
ippsMulC_32f_I(factor, data, _size);
|
970 |
+
}
|
971 |
+
|
972 |
+
private:
|
973 |
+
size_t _size;
|
974 |
+
size_t _operationalBufferSize;
|
975 |
+
size_t _powerOf2;
|
976 |
+
IppsFFTSpec_R_32f* _fftSpec;
|
977 |
+
Ipp8u* _fftSpecBuf;
|
978 |
+
Ipp8u* _fftWorkBuf;
|
979 |
+
Ipp32f* _operationalBuffer;
|
980 |
+
};
|
981 |
+
|
982 |
+
|
983 |
+
/**
|
984 |
+
* @internal
|
985 |
+
* @brief Concrete FFT implementation
|
986 |
+
*/
|
987 |
+
typedef IntelIppFFT AudioFFTImplementation;
|
988 |
+
|
989 |
+
|
990 |
+
#endif // AUDIOFFT_INTEL_IPP_USED
|
991 |
+
|
992 |
+
|
993 |
+
// ================================================================
|
994 |
+
|
995 |
+
|
996 |
+
#ifdef AUDIOFFT_APPLE_ACCELERATE_USED
|
997 |
+
|
998 |
+
|
999 |
+
/**
|
1000 |
+
* @internal
|
1001 |
+
* @class AppleAccelerateFFT
|
1002 |
+
* @brief FFT implementation using the Apple Accelerate framework internally
|
1003 |
+
*/
|
1004 |
+
class AppleAccelerateFFT : public detail::AudioFFTImpl
|
1005 |
+
{
|
1006 |
+
public:
|
1007 |
+
AppleAccelerateFFT() :
|
1008 |
+
detail::AudioFFTImpl(),
|
1009 |
+
_size(0),
|
1010 |
+
_powerOf2(0),
|
1011 |
+
_fftSetup(0),
|
1012 |
+
_re(),
|
1013 |
+
_im()
|
1014 |
+
{
|
1015 |
+
}
|
1016 |
+
|
1017 |
+
AppleAccelerateFFT(const AppleAccelerateFFT&) = delete;
|
1018 |
+
AppleAccelerateFFT& operator=(const AppleAccelerateFFT&) = delete;
|
1019 |
+
|
1020 |
+
virtual ~AppleAccelerateFFT()
|
1021 |
+
{
|
1022 |
+
init(0);
|
1023 |
+
}
|
1024 |
+
|
1025 |
+
virtual void init(size_t size) override
|
1026 |
+
{
|
1027 |
+
if (_fftSetup)
|
1028 |
+
{
|
1029 |
+
vDSP_destroy_fftsetup(_fftSetup);
|
1030 |
+
_size = 0;
|
1031 |
+
_powerOf2 = 0;
|
1032 |
+
_fftSetup = 0;
|
1033 |
+
_re.clear();
|
1034 |
+
_im.clear();
|
1035 |
+
}
|
1036 |
+
|
1037 |
+
if (size > 0)
|
1038 |
+
{
|
1039 |
+
_size = size;
|
1040 |
+
_powerOf2 = 0;
|
1041 |
+
while ((1 << _powerOf2) < _size)
|
1042 |
+
{
|
1043 |
+
++_powerOf2;
|
1044 |
+
}
|
1045 |
+
_fftSetup = vDSP_create_fftsetup(_powerOf2, FFT_RADIX2);
|
1046 |
+
_re.resize(_size / 2);
|
1047 |
+
_im.resize(_size / 2);
|
1048 |
+
}
|
1049 |
+
}
|
1050 |
+
|
1051 |
+
virtual void fft(const float* data, float* re, float* im) override
|
1052 |
+
{
|
1053 |
+
const size_t size2 = _size / 2;
|
1054 |
+
DSPSplitComplex splitComplex;
|
1055 |
+
splitComplex.realp = re;
|
1056 |
+
splitComplex.imagp = im;
|
1057 |
+
vDSP_ctoz(reinterpret_cast<const COMPLEX*>(data), 2, &splitComplex, 1, size2);
|
1058 |
+
vDSP_fft_zrip(_fftSetup, &splitComplex, 1, _powerOf2, FFT_FORWARD);
|
1059 |
+
const float factor = 0.5f;
|
1060 |
+
vDSP_vsmul(re, 1, &factor, re, 1, size2);
|
1061 |
+
vDSP_vsmul(im, 1, &factor, im, 1, size2);
|
1062 |
+
re[size2] = im[0];
|
1063 |
+
im[0] = 0.0f;
|
1064 |
+
im[size2] = 0.0f;
|
1065 |
+
}
|
1066 |
+
|
1067 |
+
virtual void ifft(float* data, const float* re, const float* im) override
|
1068 |
+
{
|
1069 |
+
const size_t size2 = _size / 2;
|
1070 |
+
::memcpy(_re.data(), re, size2 * sizeof(float));
|
1071 |
+
::memcpy(_im.data(), im, size2 * sizeof(float));
|
1072 |
+
_im[0] = re[size2];
|
1073 |
+
DSPSplitComplex splitComplex;
|
1074 |
+
splitComplex.realp = _re.data();
|
1075 |
+
splitComplex.imagp = _im.data();
|
1076 |
+
vDSP_fft_zrip(_fftSetup, &splitComplex, 1, _powerOf2, FFT_INVERSE);
|
1077 |
+
vDSP_ztoc(&splitComplex, 1, reinterpret_cast<COMPLEX*>(data), 2, size2);
|
1078 |
+
const float factor = 1.0f / static_cast<float>(_size);
|
1079 |
+
vDSP_vsmul(data, 1, &factor, data, 1, _size);
|
1080 |
+
}
|
1081 |
+
|
1082 |
+
private:
|
1083 |
+
size_t _size;
|
1084 |
+
size_t _powerOf2;
|
1085 |
+
FFTSetup _fftSetup;
|
1086 |
+
std::vector<float> _re;
|
1087 |
+
std::vector<float> _im;
|
1088 |
+
};
|
1089 |
+
|
1090 |
+
|
1091 |
+
/**
|
1092 |
+
* @internal
|
1093 |
+
* @brief Concrete FFT implementation
|
1094 |
+
*/
|
1095 |
+
typedef AppleAccelerateFFT AudioFFTImplementation;
|
1096 |
+
|
1097 |
+
|
1098 |
+
#endif // AUDIOFFT_APPLE_ACCELERATE_USED
|
1099 |
+
|
1100 |
+
|
1101 |
+
// ================================================================
|
1102 |
+
|
1103 |
+
|
1104 |
+
#ifdef AUDIOFFT_FFTW3_USED
|
1105 |
+
|
1106 |
+
|
1107 |
+
/**
|
1108 |
+
* @internal
|
1109 |
+
* @class FFTW3FFT
|
1110 |
+
* @brief FFT implementation using FFTW3 internally (see fftw.org)
|
1111 |
+
*/
|
1112 |
+
class FFTW3FFT : public detail::AudioFFTImpl
|
1113 |
+
{
|
1114 |
+
public:
|
1115 |
+
FFTW3FFT() :
|
1116 |
+
detail::AudioFFTImpl(),
|
1117 |
+
_size(0),
|
1118 |
+
_complexSize(0),
|
1119 |
+
_planForward(0),
|
1120 |
+
_planBackward(0),
|
1121 |
+
_data(0),
|
1122 |
+
_re(0),
|
1123 |
+
_im(0)
|
1124 |
+
{
|
1125 |
+
}
|
1126 |
+
|
1127 |
+
FFTW3FFT(const FFTW3FFT&) = delete;
|
1128 |
+
FFTW3FFT& operator=(const FFTW3FFT&) = delete;
|
1129 |
+
|
1130 |
+
virtual ~FFTW3FFT()
|
1131 |
+
{
|
1132 |
+
init(0);
|
1133 |
+
}
|
1134 |
+
|
1135 |
+
virtual void init(size_t size) override
|
1136 |
+
{
|
1137 |
+
if (_size != size)
|
1138 |
+
{
|
1139 |
+
if (_size > 0)
|
1140 |
+
{
|
1141 |
+
fftwf_destroy_plan(_planForward);
|
1142 |
+
fftwf_destroy_plan(_planBackward);
|
1143 |
+
_planForward = 0;
|
1144 |
+
_planBackward = 0;
|
1145 |
+
_size = 0;
|
1146 |
+
_complexSize = 0;
|
1147 |
+
|
1148 |
+
if (_data)
|
1149 |
+
{
|
1150 |
+
fftwf_free(_data);
|
1151 |
+
_data = 0;
|
1152 |
+
}
|
1153 |
+
|
1154 |
+
if (_re)
|
1155 |
+
{
|
1156 |
+
fftwf_free(_re);
|
1157 |
+
_re = 0;
|
1158 |
+
}
|
1159 |
+
|
1160 |
+
if (_im)
|
1161 |
+
{
|
1162 |
+
fftwf_free(_im);
|
1163 |
+
_im = 0;
|
1164 |
+
}
|
1165 |
+
}
|
1166 |
+
|
1167 |
+
if (size > 0)
|
1168 |
+
{
|
1169 |
+
_size = size;
|
1170 |
+
_complexSize = AudioFFT::ComplexSize(_size);
|
1171 |
+
const size_t complexSize = AudioFFT::ComplexSize(_size);
|
1172 |
+
_data = reinterpret_cast<float*>(fftwf_malloc(_size * sizeof(float)));
|
1173 |
+
_re = reinterpret_cast<float*>(fftwf_malloc(complexSize * sizeof(float)));
|
1174 |
+
_im = reinterpret_cast<float*>(fftwf_malloc(complexSize * sizeof(float)));
|
1175 |
+
|
1176 |
+
fftw_iodim dim;
|
1177 |
+
dim.n = static_cast<int>(size);
|
1178 |
+
dim.is = 1;
|
1179 |
+
dim.os = 1;
|
1180 |
+
_planForward = fftwf_plan_guru_split_dft_r2c(1, &dim, 0, 0, _data, _re, _im, FFTW_MEASURE);
|
1181 |
+
_planBackward = fftwf_plan_guru_split_dft_c2r(1, &dim, 0, 0, _re, _im, _data, FFTW_MEASURE);
|
1182 |
+
}
|
1183 |
+
}
|
1184 |
+
}
|
1185 |
+
|
1186 |
+
virtual void fft(const float* data, float* re, float* im) override
|
1187 |
+
{
|
1188 |
+
::memcpy(_data, data, _size * sizeof(float));
|
1189 |
+
fftwf_execute_split_dft_r2c(_planForward, _data, _re, _im);
|
1190 |
+
::memcpy(re, _re, _complexSize * sizeof(float));
|
1191 |
+
::memcpy(im, _im, _complexSize * sizeof(float));
|
1192 |
+
}
|
1193 |
+
|
1194 |
+
virtual void ifft(float* data, const float* re, const float* im) override
|
1195 |
+
{
|
1196 |
+
::memcpy(_re, re, _complexSize * sizeof(float));
|
1197 |
+
::memcpy(_im, im, _complexSize * sizeof(float));
|
1198 |
+
fftwf_execute_split_dft_c2r(_planBackward, _re, _im, _data);
|
1199 |
+
detail::ScaleBuffer(data, _data, 1.0f / static_cast<float>(_size), _size);
|
1200 |
+
}
|
1201 |
+
|
1202 |
+
private:
|
1203 |
+
size_t _size;
|
1204 |
+
size_t _complexSize;
|
1205 |
+
fftwf_plan _planForward;
|
1206 |
+
fftwf_plan _planBackward;
|
1207 |
+
float* _data;
|
1208 |
+
float* _re;
|
1209 |
+
float* _im;
|
1210 |
+
};
|
1211 |
+
|
1212 |
+
|
1213 |
+
/**
|
1214 |
+
* @internal
|
1215 |
+
* @brief Concrete FFT implementation
|
1216 |
+
*/
|
1217 |
+
typedef FFTW3FFT AudioFFTImplementation;
|
1218 |
+
|
1219 |
+
|
1220 |
+
#endif // AUDIOFFT_FFTW3_USED
|
1221 |
+
|
1222 |
+
|
1223 |
+
// =============================================================
|
1224 |
+
|
1225 |
+
|
1226 |
+
AudioFFT::AudioFFT() :
|
1227 |
+
_impl(new AudioFFTImplementation())
|
1228 |
+
{
|
1229 |
+
}
|
1230 |
+
|
1231 |
+
|
1232 |
+
AudioFFT::~AudioFFT()
|
1233 |
+
{
|
1234 |
+
}
|
1235 |
+
|
1236 |
+
|
1237 |
+
void AudioFFT::init(size_t size)
|
1238 |
+
{
|
1239 |
+
assert(detail::IsPowerOf2(size));
|
1240 |
+
_impl->init(size);
|
1241 |
+
}
|
1242 |
+
|
1243 |
+
|
1244 |
+
void AudioFFT::fft(const float* data, float* re, float* im)
|
1245 |
+
{
|
1246 |
+
_impl->fft(data, re, im);
|
1247 |
+
}
|
1248 |
+
|
1249 |
+
|
1250 |
+
void AudioFFT::ifft(float* data, const float* re, const float* im)
|
1251 |
+
{
|
1252 |
+
_impl->ifft(data, re, im);
|
1253 |
+
}
|
1254 |
+
|
1255 |
+
|
1256 |
+
size_t AudioFFT::ComplexSize(size_t size)
|
1257 |
+
{
|
1258 |
+
return (size / 2) + 1;
|
1259 |
+
}
|
1260 |
+
|
1261 |
+
} // End of namespace
|
1262 |
+
|
1263 |
+
|
1264 |
+
#endif // Header guard
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/mfcc/iir_filter.hpp
ADDED
@@ -0,0 +1,375 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
#ifndef SERVICESUPERVISOR_IIR_FILTER_H
|
4 |
+
#define SERVICESUPERVISOR_IIR_FILTER_H
|
5 |
+
|
6 |
+
//E(t,f) is computed using a first-order in-finite impulse response (IIR) filter
|
7 |
+
#define UES_IIR_I
|
8 |
+
//#define UES_IIR_II
|
9 |
+
|
10 |
+
#ifdef UES_IIR_I
|
11 |
+
|
12 |
+
class IIR_I
|
13 |
+
{
|
14 |
+
private:
|
15 |
+
double *m_pNum;
|
16 |
+
double *m_pDen;
|
17 |
+
double *m_px;
|
18 |
+
double *m_py;
|
19 |
+
int m_num_order;
|
20 |
+
int m_den_order;
|
21 |
+
public:
|
22 |
+
IIR_I();
|
23 |
+
~IIR_I();
|
24 |
+
void reset();
|
25 |
+
void setPara(double num[], int num_order, double den[], int den_order);
|
26 |
+
void resp(double data_in[], int m, double data_out[], int n);
|
27 |
+
void filter(double data_in[], double data_out[], int len);
|
28 |
+
};
|
29 |
+
/** \brief 将滤波器的内部状态清零,滤波器的系数保留
|
30 |
+
* \return
|
31 |
+
*/
|
32 |
+
void IIR_I::reset()
|
33 |
+
{
|
34 |
+
for(int i = 0; i <= m_num_order; i++)
|
35 |
+
{
|
36 |
+
m_pNum[i] = 0.0;
|
37 |
+
}
|
38 |
+
for(int i = 0; i <= m_den_order; i++)
|
39 |
+
{
|
40 |
+
m_pDen[i] = 0.0;
|
41 |
+
}
|
42 |
+
}
|
43 |
+
IIR_I::IIR_I()
|
44 |
+
{
|
45 |
+
m_pNum = NULL;
|
46 |
+
m_pDen = NULL;
|
47 |
+
m_px = NULL;
|
48 |
+
m_py = NULL;
|
49 |
+
m_num_order = -1;
|
50 |
+
m_den_order = -1;
|
51 |
+
};
|
52 |
+
IIR_I::~IIR_I()
|
53 |
+
{
|
54 |
+
delete[] m_pNum;
|
55 |
+
delete[] m_pDen;
|
56 |
+
delete[] m_px;
|
57 |
+
delete[] m_py;
|
58 |
+
m_pNum = NULL;
|
59 |
+
m_pDen = NULL;
|
60 |
+
m_px = NULL;
|
61 |
+
m_py = NULL;
|
62 |
+
};
|
63 |
+
|
64 |
+
/** \brief
|
65 |
+
*
|
66 |
+
* \param num 分子多项式的系数,升序排列,num[0] 为常数项
|
67 |
+
* \param m 分子多项式的阶数
|
68 |
+
* \param den 分母多项式的系数,升序排列,den[0] 为常数项
|
69 |
+
* \param m 分母多项式的阶数
|
70 |
+
* \return
|
71 |
+
*/
|
72 |
+
void IIR_I::setPara(double num[], int num_order, double den[], int den_order)
|
73 |
+
{
|
74 |
+
delete[] m_pNum;
|
75 |
+
delete[] m_pDen;
|
76 |
+
delete[] m_px;
|
77 |
+
delete[] m_py;
|
78 |
+
m_pNum = new double[num_order + 1];
|
79 |
+
m_pDen = new double[den_order + 1];
|
80 |
+
m_num_order = num_order;
|
81 |
+
m_den_order = den_order;
|
82 |
+
m_px = new double[num_order + 1];
|
83 |
+
m_py = new double[den_order + 1];
|
84 |
+
for(int i = 0; i < m_num_order; i++)
|
85 |
+
{
|
86 |
+
m_pNum[i] = num[i];
|
87 |
+
m_px[i] = 0.0;
|
88 |
+
}
|
89 |
+
m_pNum[m_num_order] = 0.0;
|
90 |
+
m_px[m_num_order] = 0.0;
|
91 |
+
for(int i = 0; i < m_den_order; i++)
|
92 |
+
{
|
93 |
+
m_pDen[i] = den[i];
|
94 |
+
m_py[i] = 0.0;
|
95 |
+
}
|
96 |
+
m_pDen[m_den_order] = 0.0;
|
97 |
+
m_py[m_den_order] = 0.0;
|
98 |
+
}
|
99 |
+
|
100 |
+
/** \brief 计算 IIR 滤波器的时域响应,不影响滤波器的内部状态
|
101 |
+
* \param data_in 为滤波器的输入,0 时刻之前的输入默认为 0,data_in[M] 及之后的输入默认为data_in[M-1]
|
102 |
+
* \param data_out 滤波器的输出
|
103 |
+
* \param M 输入数据的长度
|
104 |
+
* \param N 输出数据的长度
|
105 |
+
* \return
|
106 |
+
*/
|
107 |
+
void IIR_I::resp(double data_in[], int M, double data_out[], int N)
|
108 |
+
{
|
109 |
+
int i, k, il;
|
110 |
+
for(k = 0; k < N; k++)
|
111 |
+
{
|
112 |
+
data_out[k] = 0.0;
|
113 |
+
for(i = 0; i <= m_num_order; i++)
|
114 |
+
{
|
115 |
+
if( k - i >= 0)
|
116 |
+
{
|
117 |
+
il = ((k - i) < M) ? (k - i) : (M - 1);
|
118 |
+
data_out[k] = data_out[k] + m_pNum[i] * data_in[il];
|
119 |
+
}
|
120 |
+
}
|
121 |
+
for(i = 1; i <= m_den_order; i++)
|
122 |
+
{
|
123 |
+
if( k - i >= 0)
|
124 |
+
{
|
125 |
+
data_out[k] = data_out[k] - m_pDen[i] * data_out[k - i];
|
126 |
+
}
|
127 |
+
}
|
128 |
+
}
|
129 |
+
}
|
130 |
+
|
131 |
+
/** \brief 滤波函数,采用直接I型结构
|
132 |
+
* 注:该函数内部修改过,移植librosa.pcen时参照scipy.signal.lfilter所做的设计。
|
133 |
+
*
|
134 |
+
* \param data_in[] 输入数据
|
135 |
+
* \param data_out[] 保存滤波后的数据
|
136 |
+
* \param len 数组的长度
|
137 |
+
* \return
|
138 |
+
*/
|
139 |
+
void IIR_I::filter(double data_in[], double data_out[], int len)
|
140 |
+
{
|
141 |
+
int i, k;
|
142 |
+
m_py[1] = 1; //修改的地方,因为公式中y[n-k],当为第一个元素时会出现y[-1],pcen中y[-1]会被认为为1。
|
143 |
+
for(k = 0; k < len; k++)
|
144 |
+
{
|
145 |
+
m_px[0] = data_in[k];
|
146 |
+
m_py[0] = 0.0;
|
147 |
+
for(i = 0; i <= m_num_order; i++)
|
148 |
+
{
|
149 |
+
m_py[0] = m_py[0] + m_pNum[i] * m_px[i];
|
150 |
+
}
|
151 |
+
for(i = 1; i <= m_den_order; i++)
|
152 |
+
{
|
153 |
+
m_py[0] = m_py[0] - m_pDen[i] * m_py[i];
|
154 |
+
}
|
155 |
+
for(i = m_num_order; i >= 1; i--)
|
156 |
+
{
|
157 |
+
m_px[i] = m_px[i-1];
|
158 |
+
}
|
159 |
+
for(i = m_den_order; i >= 1; i--)
|
160 |
+
{
|
161 |
+
m_py[i] = m_py[i-1];
|
162 |
+
}
|
163 |
+
data_out[k] = m_py[0];
|
164 |
+
}
|
165 |
+
}
|
166 |
+
|
167 |
+
#endif
|
168 |
+
|
169 |
+
#ifdef UES_IIR_II
|
170 |
+
|
171 |
+
/**< IIR 滤波器直接II型实现 */
|
172 |
+
class IIR_II
|
173 |
+
{
|
174 |
+
public:
|
175 |
+
IIR_II();
|
176 |
+
void reset();
|
177 |
+
void setPara(double num[], int num_order, double den[], int den_order);
|
178 |
+
void resp(double data_in[], int m, double data_out[], int n);
|
179 |
+
double filter(double data);
|
180 |
+
void filter(double data[], int len);
|
181 |
+
void filter(double data_in[], double data_out[], int len);
|
182 |
+
protected:
|
183 |
+
private:
|
184 |
+
double *m_pNum;
|
185 |
+
double *m_pDen;
|
186 |
+
double *m_pW;
|
187 |
+
int m_num_order;
|
188 |
+
int m_den_order;
|
189 |
+
int m_N;
|
190 |
+
};
|
191 |
+
|
192 |
+
class IIR_BODE
|
193 |
+
{
|
194 |
+
private:
|
195 |
+
double *m_pNum;
|
196 |
+
double *m_pDen;
|
197 |
+
int m_num_order;
|
198 |
+
int m_den_order;
|
199 |
+
std::complex<double> poly_val(double p[], int order, double omega);
|
200 |
+
public:
|
201 |
+
IIR_BODE();
|
202 |
+
void setPara(double num[], int num_order, double den[], int den_order);
|
203 |
+
std::complex<double> bode(double omega);
|
204 |
+
void bode(double omega[], int n, std::complex<double> resp[]);
|
205 |
+
};
|
206 |
+
|
207 |
+
IIR_II::IIR_II()
|
208 |
+
{
|
209 |
+
//ctor
|
210 |
+
m_pNum = NULL;
|
211 |
+
m_pDen = NULL;
|
212 |
+
m_pW = NULL;
|
213 |
+
m_num_order = -1;
|
214 |
+
m_den_order = -1;
|
215 |
+
m_N = 0;
|
216 |
+
};
|
217 |
+
|
218 |
+
/** \brief 将滤波器的内部状态清零,滤波器的系数保留
|
219 |
+
* \return
|
220 |
+
*/
|
221 |
+
void IIR_II::reset()
|
222 |
+
{
|
223 |
+
for(int i = 0; i < m_N; i++)
|
224 |
+
{
|
225 |
+
m_pW[i] = 0.0;
|
226 |
+
}
|
227 |
+
}
|
228 |
+
/** \brief
|
229 |
+
*
|
230 |
+
* \param num 分子多项式的系数,升序排列,num[0] 为常数项
|
231 |
+
* \param m 分子多项式的阶数
|
232 |
+
* \param den 分母多项式的系数,升序排列,den[0] 为常数项
|
233 |
+
* \param m 分母多项式的阶数
|
234 |
+
* \return
|
235 |
+
*/
|
236 |
+
void IIR_II::setPara(double num[], int num_order, double den[], int den_order)
|
237 |
+
{
|
238 |
+
delete[] m_pNum;
|
239 |
+
delete[] m_pDen;
|
240 |
+
delete[] m_pW;
|
241 |
+
m_num_order = num_order;
|
242 |
+
m_den_order = den_order;
|
243 |
+
m_N = fmax(num_order, den_order) + 1;
|
244 |
+
m_pNum = new double[m_N];
|
245 |
+
m_pDen = new double[m_N];
|
246 |
+
m_pW = new double[m_N];
|
247 |
+
for(int i = 0; i < m_N; i++)
|
248 |
+
{
|
249 |
+
m_pNum[i] = 0.0;
|
250 |
+
m_pDen[i] = 0.0;
|
251 |
+
m_pW[i] = 0.0;
|
252 |
+
}
|
253 |
+
for(int i = 0; i <= num_order; i++)
|
254 |
+
{
|
255 |
+
m_pNum[i] = num[i];
|
256 |
+
}
|
257 |
+
for(int i = 0; i <= den_order; i++)
|
258 |
+
{
|
259 |
+
m_pDen[i] = den[i];
|
260 |
+
}
|
261 |
+
}
|
262 |
+
/** \brief 计算 IIR 滤波器的时域响应,不影响滤波器的内部状态
|
263 |
+
* \param data_in 为滤波器的输入,0 时刻之前的输入默认为 0,data_in[M] 及之后的输入默认为data_in[M-1]
|
264 |
+
* \param data_out 滤波器的输出
|
265 |
+
* \param M 输入数据的长度
|
266 |
+
* \param N 输出数据的长度
|
267 |
+
* \return
|
268 |
+
*/
|
269 |
+
void IIR_II::resp(double data_in[], int M, double data_out[], int N)
|
270 |
+
{
|
271 |
+
int i, k, il;
|
272 |
+
for(k = 0; k < N; k++)
|
273 |
+
{
|
274 |
+
data_out[k] = 0.0;
|
275 |
+
for(i = 0; i <= m_num_order; i++)
|
276 |
+
{
|
277 |
+
if( k - i >= 0)
|
278 |
+
{
|
279 |
+
il = ((k - i) < M) ? (k - i) : (M - 1);
|
280 |
+
data_out[k] = data_out[k] + m_pNum[i] * data_in[il];
|
281 |
+
}
|
282 |
+
}
|
283 |
+
for(i = 1; i <= m_den_order; i++)
|
284 |
+
{
|
285 |
+
if( k - i >= 0)
|
286 |
+
{
|
287 |
+
data_out[k] = data_out[k] - m_pDen[i] * data_out[k - i];
|
288 |
+
}
|
289 |
+
}
|
290 |
+
}
|
291 |
+
}
|
292 |
+
/** \brief 滤波函数,采用直接II型结构
|
293 |
+
*
|
294 |
+
* \param data 输入数据
|
295 |
+
* \return 滤波后的结果
|
296 |
+
*/
|
297 |
+
double IIR_II::filter(double data)
|
298 |
+
{
|
299 |
+
m_pW[0] = data;
|
300 |
+
for(int i = 1; i <= m_den_order; i++) // 先更新 w[n] 的状态
|
301 |
+
{
|
302 |
+
m_pW[0] = m_pW[0] - m_pDen[i] * m_pW[i];
|
303 |
+
}
|
304 |
+
data = 0.0;
|
305 |
+
for(int i = 0; i <= m_num_order; i++)
|
306 |
+
{
|
307 |
+
data = data + m_pNum[i] * m_pW[i];
|
308 |
+
}
|
309 |
+
for(int i = m_N - 1; i >= 1; i--)
|
310 |
+
{
|
311 |
+
m_pW[i] = m_pW[i-1];
|
312 |
+
}
|
313 |
+
return data;
|
314 |
+
}
|
315 |
+
/** \brief 滤波函数,采用直接II型结构
|
316 |
+
*
|
317 |
+
* \param data[] 传入输入数据,返回时给出滤波后的结果
|
318 |
+
* \param len data[] 数组的长度
|
319 |
+
* \return
|
320 |
+
*/
|
321 |
+
void IIR_II::filter(double data[], int len)
|
322 |
+
{
|
323 |
+
int i, k;
|
324 |
+
for(k = 0; k < len; k++)
|
325 |
+
{
|
326 |
+
m_pW[0] = data[k];
|
327 |
+
for(i = 1; i <= m_den_order; i++) // 先更新 w[n] 的状态
|
328 |
+
{
|
329 |
+
m_pW[0] = m_pW[0] - m_pDen[i] * m_pW[i];
|
330 |
+
}
|
331 |
+
data[k] = 0.0;
|
332 |
+
for(i = 0; i <= m_num_order; i++)
|
333 |
+
{
|
334 |
+
data[k] = data[k] + m_pNum[i] * m_pW[i];
|
335 |
+
}
|
336 |
+
|
337 |
+
for(i = m_N - 1; i >= 1; i--)
|
338 |
+
{
|
339 |
+
m_pW[i] = m_pW[i-1];
|
340 |
+
}
|
341 |
+
}
|
342 |
+
}
|
343 |
+
/** \brief 滤波函数,采用直接II型结构
|
344 |
+
*
|
345 |
+
* \param data_in[] 输入数据
|
346 |
+
* \param data_out[] 保存滤波后的数据
|
347 |
+
* \param len 数组的长度
|
348 |
+
* \return
|
349 |
+
*/
|
350 |
+
void IIR_II::filter(double data_in[], double data_out[], int len)
|
351 |
+
{
|
352 |
+
int i, k;
|
353 |
+
for(k = 0; k < len; k++)
|
354 |
+
{
|
355 |
+
m_pW[0] = data_in[k];
|
356 |
+
for(i = 1; i <= m_den_order; i++) // 先更新 w[n] 的状态
|
357 |
+
{
|
358 |
+
m_pW[0] = m_pW[0] - m_pDen[i] * m_pW[i];
|
359 |
+
}
|
360 |
+
data_out[k] = 0.0;
|
361 |
+
for(i = 0; i <= m_num_order; i++)
|
362 |
+
{
|
363 |
+
data_out[k] = data_out[k] + m_pNum[i] * m_pW[i];
|
364 |
+
}
|
365 |
+
|
366 |
+
for(i = m_N - 1; i >= 1; i--)
|
367 |
+
{
|
368 |
+
m_pW[i] = m_pW[i-1];
|
369 |
+
}
|
370 |
+
}
|
371 |
+
}
|
372 |
+
|
373 |
+
#endif
|
374 |
+
|
375 |
+
#endif //SERVICESUPERVISOR_IIR_FILTER_H
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/mfcc/mfcc.hpp
ADDED
@@ -0,0 +1,441 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
#include "AudioFFT.hpp"
|
4 |
+
//#include"../third/numcpp/NumCpp.hpp"
|
5 |
+
#include "opencv2/opencv.hpp"
|
6 |
+
#include "iir_filter.hpp"
|
7 |
+
#include "sas_util.h"
|
8 |
+
|
9 |
+
int nSamplesPerSec = 16000; //采样率(每秒样本数) //Sample rate.(keda, thchs30, aishell)
|
10 |
+
int length_DFT = 1024;//2048; //傅里叶点数 //fft points (samples)
|
11 |
+
int hop_length = 160;//int(0.05 * nSamplesPerSec); //步长 //下一帧取数据相对于这一帧的右偏移量
|
12 |
+
int win_length = 800;// int(0.1 * nSamplesPerSec); //帧长 //假设16000采样率,则取取0.1s时间的数据
|
13 |
+
int number_filterbanks = 80; //过滤器数量 //Number of Mel banks to generate
|
14 |
+
float preemphasis = 0.97; //预加重(高通滤波器比例值)
|
15 |
+
int max_db = 100;
|
16 |
+
int ref_db = 20;
|
17 |
+
int r = 1; //librosa里的r=1,暂未深入分析其作用
|
18 |
+
double pi = 3.14159265358979323846;
|
19 |
+
|
20 |
+
cv::Mat_<double> mel_basis;
|
21 |
+
cv::Mat_<float> hannWindow;
|
22 |
+
|
23 |
+
std::shared_ptr<IIR_I> filter;
|
24 |
+
|
25 |
+
//"""Convert Hz to Mels"""
|
26 |
+
double hz_to_mel(double frequencies, bool htk = false) {
|
27 |
+
if (htk) {
|
28 |
+
return 2595.0 * log10(1.0 + frequencies / 700.0);
|
29 |
+
}
|
30 |
+
// Fill in the linear part
|
31 |
+
double f_min = 0.0;
|
32 |
+
double f_sp = 200.0 / 3;
|
33 |
+
double mels = (frequencies - f_min) / f_sp;
|
34 |
+
// Fill in the log-scale part
|
35 |
+
double min_log_hz = 1000.0; // beginning of log region (Hz)
|
36 |
+
double min_log_mel = (min_log_hz - f_min) / f_sp; // same (Mels)
|
37 |
+
double logstep = log(6.4) / 27.0; // step size for log region
|
38 |
+
|
39 |
+
// 对照Python平台的librosa库,移植
|
40 |
+
//如果是多维数列
|
41 |
+
// if (frequencies.ndim) {
|
42 |
+
// // If we have array data, vectorize
|
43 |
+
// log_t = (frequencies >= min_log_hz)
|
44 |
+
// mels[log_t] = min_log_mel + np.log(frequencies[log_t] / min_log_hz) / logstep
|
45 |
+
// } else
|
46 |
+
if (frequencies >= min_log_hz) {
|
47 |
+
// If we have scalar data, heck directly
|
48 |
+
mels = min_log_mel + log(frequencies / min_log_hz) / logstep;
|
49 |
+
}
|
50 |
+
return mels;
|
51 |
+
}
|
52 |
+
|
53 |
+
//"""Convert mel bin numbers to frequencies"""
|
54 |
+
cv::Mat_<double> mel_to_hz(cv::Mat_<double> mels, bool htk = false) {
|
55 |
+
// if (htk) {
|
56 |
+
// return //python://700.0 * (10.0**(mels / 2595.0) - 1.0);
|
57 |
+
// }
|
58 |
+
// Fill in the linear scale
|
59 |
+
double f_min = 0.0;
|
60 |
+
double f_sp = 200.0 / 3;
|
61 |
+
cv::Mat_<double> freqs = mels * f_sp + f_min;
|
62 |
+
// And now the nonlinear scale
|
63 |
+
double min_log_hz = 1000.0; // beginning of log region (Hz)
|
64 |
+
double min_log_mel = (min_log_hz - f_min) / f_sp; // same (Mels)
|
65 |
+
double logstep = log(6.4) / 27.0; // step size for log region
|
66 |
+
// 对照Python平台的librosa库,移植
|
67 |
+
//if (mels.ndim) {
|
68 |
+
// If we have vector data, vectorize
|
69 |
+
cv::Mat_<bool> log_t = (mels >= min_log_mel);
|
70 |
+
for (int i = 0; i < log_t.cols; i++) {
|
71 |
+
if (log_t(0, i)) {
|
72 |
+
freqs(0, i) = cv::exp((mels(0, i) - min_log_mel) * logstep) * min_log_hz;
|
73 |
+
}
|
74 |
+
}
|
75 |
+
//}
|
76 |
+
return freqs;
|
77 |
+
}
|
78 |
+
|
79 |
+
// 生成等差数列,类似np.linspace
|
80 |
+
cv::Mat_<double> cvlinspace(double min_, double max_, int length) {
|
81 |
+
auto cvmat = cv::Mat_<double>(1, length);
|
82 |
+
for (int i = 0; i < length; i++) {
|
83 |
+
cvmat(0, i) = ((max_ - min_) / (length - 1) * i) + min_;
|
84 |
+
}
|
85 |
+
return cvmat;
|
86 |
+
}
|
87 |
+
|
88 |
+
//"""Create a Filterbank matrix to combine FFT bins into Mel-frequency bins"""
|
89 |
+
cv::Mat_<double> mel_spectrogram_create(int nps, int n_fft, int n_mels) {
|
90 |
+
double f_max = nps / 2.0;
|
91 |
+
double f_min = 0;
|
92 |
+
int n_fft_2 = 1 + n_fft / 2;
|
93 |
+
// Initialize the weights
|
94 |
+
//auto weights = nc::zeros<double>(nc::uint32(n_mels), nc::uint32(n_fft_2));
|
95 |
+
auto weights = cv::Mat_<double>(n_mels, n_fft_2, 0.0);
|
96 |
+
// Center freqs of each FFT bin
|
97 |
+
//auto fftfreqs_ = nc::linspace<double>(f_min, f_max, nc::uint32(n_fft_2), true);
|
98 |
+
auto fftfreqs = cvlinspace(f_min, f_max, n_fft_2);
|
99 |
+
|
100 |
+
// 'Center freqs' of mel bands - uniformly spaced between limits
|
101 |
+
double min_mel = hz_to_mel(f_min, false);
|
102 |
+
double max_mel = hz_to_mel(f_max, false);
|
103 |
+
//auto mels_ = nc::linspace(min_mel, max_mel, nc::uint32(n_mels + 2));
|
104 |
+
auto mels = cvlinspace(min_mel, max_mel, n_mels + 2);
|
105 |
+
auto mel_f = mel_to_hz(mels, false);
|
106 |
+
|
107 |
+
//auto fdiff_ = nc::diff(mel_f_); //沿着指定轴计算第N维的离散差值(后一个元素减去前一个元素)
|
108 |
+
cv::Mat_<double> d1(1, mel_f.cols * mel_f.rows - 1, (double *) (mel_f.data) + 1);
|
109 |
+
cv::Mat_<double> d2(1, mel_f.cols * mel_f.rows - 1, (double *) (mel_f.data));
|
110 |
+
cv::Mat_<double> fdiff = d1 - d2;
|
111 |
+
|
112 |
+
//auto ramps = nc::subtract.outer(mel_f, fftfreqs); //nc没有subtract.outer
|
113 |
+
//nc::NdArray<double> ramps = nc::zeros<double>(mel_f.cols, fftfreqs.cols);
|
114 |
+
auto ramps = cv::Mat_<double>(mel_f.cols, fftfreqs.cols);
|
115 |
+
for (int i = 0; i < mel_f.cols; i++) {
|
116 |
+
for (int j = 0; j < fftfreqs.cols; j++) {
|
117 |
+
ramps(i, j) = mel_f(0, i) - fftfreqs(0, j);
|
118 |
+
}
|
119 |
+
}
|
120 |
+
|
121 |
+
for (int i = 0; i < n_mels; i++) {
|
122 |
+
// lower and upper slopes for all bins
|
123 |
+
//auto ramps_1 = nc::NdArray<double>(1, ramps.cols);
|
124 |
+
auto ramps_1 = cv::Mat_<double>(1, ramps.cols);
|
125 |
+
for (int j = 0; j < ramps.cols; j++) {
|
126 |
+
ramps_1(0, j) = ramps(i, j);
|
127 |
+
}
|
128 |
+
//auto ramps_2 = nc::NdArray<double>(1, ramps.cols);
|
129 |
+
auto ramps_2 = cv::Mat_<double>(1, ramps.cols);
|
130 |
+
for (int j = 0; j < ramps.cols; j++) {
|
131 |
+
ramps_2(0, j) = ramps(i + 2, j);
|
132 |
+
}
|
133 |
+
cv::Mat_<double> lower = ramps_1 * -1 / fdiff(0, i);
|
134 |
+
cv::Mat_<double> upper = ramps_2 / fdiff(0, i + 1);
|
135 |
+
// .. then intersect them with each other and zero
|
136 |
+
//auto weights_1 = nc::maximum(nc::zeros<double>(1, ramps.cols), nc::minimum(lower, upper));
|
137 |
+
cv::Mat c1 = lower;//(cv::Mat_<double>(1,5) << 1,2,-3,4,-5);
|
138 |
+
cv::Mat c2 = upper;
|
139 |
+
cv::Mat weights_1 = cv::Mat_<double>(1, lower.cols);
|
140 |
+
cv::min(c1, c2, weights_1);
|
141 |
+
cv::max(weights_1, 0, weights_1);
|
142 |
+
for (int j = 0; j < n_fft_2; j++) {
|
143 |
+
weights(i, j) = weights_1.at<double_t>(0, j);
|
144 |
+
}
|
145 |
+
}
|
146 |
+
|
147 |
+
// Slaney-style mel is scaled to be approx constant energy per channel
|
148 |
+
auto enorm = cv::Mat_<double>(1, n_mels);
|
149 |
+
for (int j = 0; j < n_mels; j++) {
|
150 |
+
enorm(0, j) = 2.0 / (mel_f(0, j + 2) - mel_f(0, j));
|
151 |
+
}
|
152 |
+
for (int j = 0; j < n_mels; j++) {
|
153 |
+
for (int k = 0; k < n_fft_2; k++) {
|
154 |
+
weights(j, k) *= enorm(0, j);
|
155 |
+
}
|
156 |
+
}
|
157 |
+
return weights;
|
158 |
+
}
|
159 |
+
|
160 |
+
//"""Short-time Fourier transform (STFT)""": 默认center=True, window='hann', pad_mode='reflect'
|
161 |
+
cv::Mat_<double> MagnitudeSpectrogram(const cv::Mat_<float> *emphasis_data, int n_fft = 2048, int hop_length = 0,
|
162 |
+
int win_length = 0) {
|
163 |
+
if (win_length == 0) {
|
164 |
+
win_length = n_fft;
|
165 |
+
}
|
166 |
+
if (hop_length == 0) {
|
167 |
+
hop_length = win_length / 4;
|
168 |
+
}
|
169 |
+
|
170 |
+
// reflect对称填充
|
171 |
+
int pad_lenght = n_fft / 2;
|
172 |
+
// 使用opencv里的copyMakeBorder来完成reflect填充
|
173 |
+
cv::Mat_<float> cv_padbuffer;
|
174 |
+
cv::copyMakeBorder(*emphasis_data, cv_padbuffer, 0, 0, pad_lenght, pad_lenght, cv::BORDER_REFLECT_101);
|
175 |
+
|
176 |
+
// windowing加窗:将每一帧乘以汉宁窗,以增加帧左端和右端的连续性。
|
177 |
+
// 生成一个1600长度的hannWindow,并居中到2048长度的
|
178 |
+
if (hannWindow.empty()) {
|
179 |
+
hannWindow = cv::Mat_<float>(1, n_fft, 0.0f);
|
180 |
+
int insert_cnt = 0;
|
181 |
+
if (n_fft > win_length) {
|
182 |
+
insert_cnt = (n_fft - win_length) / 2;
|
183 |
+
} else {
|
184 |
+
//std::cout << "\tn_fft:" << n_fft << " > win_length:" << n_fft << std::endl;
|
185 |
+
return cv::Mat_<double>(0, 0);
|
186 |
+
}
|
187 |
+
for (int k = 1; k <= win_length; k++) {
|
188 |
+
hannWindow(0, k - 1 + insert_cnt) = float(0.5 * (1 - cos(2 * pi * k / (win_length + 1))));
|
189 |
+
}
|
190 |
+
}
|
191 |
+
// opencv虽然有Hann窗生成函数,但是必须要求width > 1,height > 1
|
192 |
+
//cv::Mat_<double> cv_hannWindow;
|
193 |
+
//cv::createHanningWindow(cv_hannWindow, cv::Size(1, win_length), CV_64FC1);
|
194 |
+
|
195 |
+
int size = cv_padbuffer.rows * cv_padbuffer.cols;//padbuffer.size()
|
196 |
+
int number_feature_vectors = (size - n_fft) / hop_length + 1;
|
197 |
+
int number_coefficients = n_fft / 2 + 1;
|
198 |
+
cv::Mat_<float> feature_vector(number_feature_vectors, number_coefficients, 0.0f);
|
199 |
+
|
200 |
+
audiofft::AudioFFT fft; //将FFT初始化放在循环外,可达到最优速度
|
201 |
+
fft.init(size_t(n_fft));
|
202 |
+
for (int i = 0; i <= size - n_fft; i += hop_length) {
|
203 |
+
// 每次取一段数据
|
204 |
+
cv::Mat_<float> framef = cv::Mat_<float>(1, n_fft, (float *) (cv_padbuffer.data) + i).clone();
|
205 |
+
// 加hann窗
|
206 |
+
framef = framef.mul(hannWindow);
|
207 |
+
|
208 |
+
// 复数:Xrf实数,Xif虚数。
|
209 |
+
cv::Mat_<float> Xrf(1, number_coefficients);
|
210 |
+
cv::Mat_<float> Xif(1, number_coefficients);
|
211 |
+
fft.fft((float *) (framef.data), (float *) (Xrf.data), (float *) (Xif.data));
|
212 |
+
|
213 |
+
// 求模
|
214 |
+
cv::pow(Xrf, 2, Xrf);
|
215 |
+
cv::pow(Xif, 2, Xif);
|
216 |
+
cv::Mat_<float> cv_feature(1, number_coefficients, &(feature_vector[i / hop_length][0]));
|
217 |
+
cv::sqrt(Xrf + Xif, cv_feature);
|
218 |
+
}
|
219 |
+
cv::Mat_<float> cv_mag;
|
220 |
+
cv::transpose(feature_vector, cv_mag);
|
221 |
+
cv::Mat_<double> mag;
|
222 |
+
cv_mag.convertTo(mag, CV_64FC1);
|
223 |
+
|
224 |
+
return mag;
|
225 |
+
}
|
226 |
+
|
227 |
+
/*********************************************
|
228 |
+
* 名称:log_mel
|
229 |
+
* 功能:传入音频数据,输出log-mel方式提取的特征数据。
|
230 |
+
* 参数:@ifile_data 传入的音频数据
|
231 |
+
* @nSamples_per_sec 音频采样率
|
232 |
+
* 返回:cv::Mat_<double> 特征数据
|
233 |
+
*********************************************/
|
234 |
+
//cv::Mat_<double> log_mel(std::vector<uint8_t> &ifile_data, int nSamples_per_sec) {
|
235 |
+
int log_mel(float* ifile_data, int ifile_length,int nSamples_per_sec,float* ofile_data) {
|
236 |
+
if (nSamples_per_sec != nSamplesPerSec) {
|
237 |
+
//std::cout << R"(the "nSamples_per_sec" is not 16000.)" << std::endl;
|
238 |
+
return -1;//cv::Mat_<double>(0, 0);
|
239 |
+
}
|
240 |
+
//int ifile_length = int(ifile_data.size() / 4);
|
241 |
+
|
242 |
+
// pre-emphasis 预加重 //高通滤波
|
243 |
+
//cv::Mat_<float> d1(1, ifile_length - 1, (float *) (ifile_data.data()) + 1);
|
244 |
+
//cv::Mat_<float> d2(1, ifile_length-1 , (float *) (ifile_data.data()));
|
245 |
+
cv::Mat_<float> d1(1, ifile_length - 1, (float *) (ifile_data) + 1);
|
246 |
+
cv::Mat_<float> d2(1, ifile_length-1 , (float *) (ifile_data));
|
247 |
+
|
248 |
+
//std::cout<<ifile_length<<"====="<<d1[0][960000-1]<<std::endl;
|
249 |
+
cv::Mat_<float> cv_emphasis_data;
|
250 |
+
|
251 |
+
cv::hconcat(cv::Mat_<float>::zeros(1, 1), d1 - d2 * preemphasis, cv_emphasis_data);
|
252 |
+
//cv::print(cv_emphasis_data);
|
253 |
+
//std::cout<<ifile_length<<"====="<< cv_emphasis_data[0][960000-1]<<std::endl;
|
254 |
+
|
255 |
+
// magnitude spectrogram 幅度谱图
|
256 |
+
auto mag = MagnitudeSpectrogram(&cv_emphasis_data, length_DFT, hop_length, win_length);
|
257 |
+
auto magb = cv::abs(mag);
|
258 |
+
cv::pow(magb,2,mag);
|
259 |
+
//tooken
|
260 |
+
// 生成梅尔谱图 mel spectrogram //3ms
|
261 |
+
if (mel_basis.empty()) {
|
262 |
+
mel_basis = mel_spectrogram_create(nSamplesPerSec, length_DFT, number_filterbanks);
|
263 |
+
}
|
264 |
+
|
265 |
+
//cv::print(mel_basis);
|
266 |
+
//std::cout<<mel_basis.cols<<"=====cv_mel"<<mel_basis.rows<<std::endl;
|
267 |
+
// doc
|
268 |
+
cv::Mat cv_mel = mel_basis * mag;
|
269 |
+
//cv::Mat cv_mel = mel_basis.dot( mag);
|
270 |
+
|
271 |
+
// to decibel
|
272 |
+
//mel = 20 * np.log10(np.maximum(1e-5, mel))
|
273 |
+
//mag = 20 * np.log10(np.maximum(1e-5, mag))
|
274 |
+
//由于后续没用用到mag了,所以不再对mag做运算。
|
275 |
+
// 使用opencv来实现
|
276 |
+
//cv::log(cv::max(cv_mel, 1e-5), cv_mel);
|
277 |
+
|
278 |
+
//cv::log(cv::max(cv_mel, 1e-5), cv_mel);
|
279 |
+
cv::log(cv_mel+ 1e-5, cv_mel);
|
280 |
+
// opencv没有log10(),所以使用log(x)/log(10)来运算。
|
281 |
+
cv_mel = cv_mel / 2.3025850929940459 * 10; // 2.3025850929940459=log(10)
|
282 |
+
|
283 |
+
// normalize
|
284 |
+
//mel = np.clip((mel - hp.ref_db + hp.max_db) / hp.max_db, 1e-8, 1)
|
285 |
+
//mag = np.clip((mag - hp.ref_db + hp.max_db) / hp.max_db, 1e-8, 1)
|
286 |
+
//cv::normalize(cv_mel, cv_mel, 1e-8, 1.0, cv::NORM_MINMAX); // cv::normalize无法实现
|
287 |
+
//cv_mel = (cv_mel - ref_db + max_db) / max_db;
|
288 |
+
//cv_mel = cv::max(cv::min(cv_mel, 1.0), 1e-8);
|
289 |
+
cv_mel = cv_mel - ref_db;
|
290 |
+
//cv::print(cv_mel);
|
291 |
+
//std::cout<<cv_mel.cols<<"=====cv_mel"<<cv_mel.rows<<std::endl;
|
292 |
+
//std::cout<<"=====cv_mel"<<std::endl;
|
293 |
+
//getchar();
|
294 |
+
|
295 |
+
// Transpose
|
296 |
+
//mel = mel.T.astype(np.float32)
|
297 |
+
//mag = mag.T.astype(np.float32)
|
298 |
+
// 使用opencv的transpose
|
299 |
+
cv::Mat cv_mel_r;//(cv_mel.cols,cv_mel.rows,CV_64FC1,ofile_data);
|
300 |
+
cv::transpose(cv_mel, cv_mel_r);
|
301 |
+
//cv::Mat rcv(cv_mel_r.cols,cv_mel_r.rows, CV_32FC1,ofile_data);
|
302 |
+
cv::Mat rrr(cv_mel.cols,cv_mel.rows,CV_32FC1,ofile_data);
|
303 |
+
cv_mel_r.convertTo(rrr, CV_32FC1);
|
304 |
+
|
305 |
+
if (r == 1) {
|
306 |
+
// 原计算公式是:
|
307 |
+
// mel = mel[:len(mel) // hp.r * hp.r].reshape([len(mel) // hp.r, hp.r * hp.n_mels])
|
308 |
+
// 当r=1的时候公式运算无任何数值改变。
|
309 |
+
} else {
|
310 |
+
//std::cout << R"(the "r" is not 1.)" << std::endl;
|
311 |
+
}
|
312 |
+
// 返回mel特征向量
|
313 |
+
return 0;
|
314 |
+
}
|
315 |
+
|
316 |
+
/**--------------------------------- 以下是pcen运算方法 ---------------------------------**/
|
317 |
+
|
318 |
+
// scipy.signal.lfilter_zi()
|
319 |
+
cv::Mat_<double> cvlfilter_zi(cv::Mat_<double> b, cv::Mat_<double> a) {
|
320 |
+
if ((b.rows != 1) || (a.rows != 1)) {
|
321 |
+
//std::cout << "Numerator b and Denominator a must be 1-D." << std::endl;
|
322 |
+
}
|
323 |
+
if (a(0, 0) != 1) {
|
324 |
+
// Normalize the coefficients so a[0] == 1.
|
325 |
+
b = b / a(0, 0);
|
326 |
+
a = a / a(0, 0);
|
327 |
+
}
|
328 |
+
int len_a = a.cols * a.rows;
|
329 |
+
int len_b = b.cols * b.rows;
|
330 |
+
int n = len_a > len_b ? len_a : len_b;
|
331 |
+
if (len_a < n) {
|
332 |
+
cv::hconcat(a, cv::Mat_<float>::zeros(1, n - len_a), a);
|
333 |
+
} else if (len_b < n) {
|
334 |
+
cv::hconcat(b, cv::Mat_<float>::zeros(1, n - len_b), b);
|
335 |
+
}
|
336 |
+
return cv::Mat_<double>(0, 0);
|
337 |
+
}
|
338 |
+
/*
|
339 |
+
// scipy.signal.lfilter()
|
340 |
+
// Filter data along one-dimension with an IIR or FIR filter.
|
341 |
+
cv::Mat_<double> cvlfilter(cv::Mat_<double> &b, cv::Mat_<double> &a, cv::Mat_<double> &x,
|
342 |
+
cv::Mat_<double> &zi, int axis = -1) {
|
343 |
+
if (a.rows * a.cols == 1) {
|
344 |
+
// This path only supports types fdgFDGO to mirror _linear_filter below.
|
345 |
+
// Any of b, a, x, or zi can set the dtype, but there is no default
|
346 |
+
// casting of other types; instead a NotImplementedError is raised.
|
347 |
+
// 后续如果需要,则进行补充
|
348 |
+
} else {
|
349 |
+
// return sigtools._linear_filter(b, a, x, axis, zi)
|
350 |
+
// sigtools._linear_filter()
|
351 |
+
// (y,Vf) = _linear_filter(b,a,X,Dim=-1,Vi=None) implemented using Direct Form II transposed flow diagram.
|
352 |
+
// If Vi is not given, Vf is not returned.
|
353 |
+
;
|
354 |
+
}
|
355 |
+
}
|
356 |
+
*/
|
357 |
+
/*********************************************
|
358 |
+
* 名称:pcen
|
359 |
+
* 功能:传入音频数据,输出pcen方式提取的特征数据。
|
360 |
+
* 参数:@ifile_data 传入的音频数据
|
361 |
+
* @nSamples_per_sec 音频采样率
|
362 |
+
* 返回:cv::Mat_<double> 特征数据
|
363 |
+
*********************************************/
|
364 |
+
cv::Mat_<double> pcen(std::vector<uint8_t> &ifile_data, int nSamples_per_sec) {
|
365 |
+
//if (!(&ifile_data) || ifile_data.empty()) {
|
366 |
+
if (ifile_data.empty()) {
|
367 |
+
//std::cout << "error: invalid paramter: ifile_data" << std::endl;
|
368 |
+
return cv::Mat_<double>(0, 0);
|
369 |
+
}
|
370 |
+
if (nSamples_per_sec != nSamplesPerSec) {
|
371 |
+
// std::cout << R"(error: the "nSamples_per_sec" is not 16000.)" << std::endl;
|
372 |
+
return cv::Mat_<double>(0, 0);
|
373 |
+
}
|
374 |
+
int ifile_length = int(ifile_data.size() / 4);
|
375 |
+
cv::Mat_<float> cv_emphasis_data(1, ifile_length, (float *) (ifile_data.data()));
|
376 |
+
// std::cout<<ifile_length<<"====="<<cv_emphasis_data[0][960000-1]<<std::endl;
|
377 |
+
//getchar();
|
378 |
+
|
379 |
+
// magnitude spectrogram 幅度谱图
|
380 |
+
auto mag = MagnitudeSpectrogram(&cv_emphasis_data, length_DFT, hop_length, win_length);
|
381 |
+
mag = cv::abs(mag) * std::pow(2, 31);
|
382 |
+
|
383 |
+
// 生成梅尔谱图 mel spectrogram //3ms
|
384 |
+
if (mel_basis.empty()) {
|
385 |
+
mel_basis = mel_spectrogram_create(nSamplesPerSec, length_DFT, number_filterbanks);
|
386 |
+
}
|
387 |
+
|
388 |
+
// doc
|
389 |
+
cv::Mat_<double> mel = mel_basis * mag;
|
390 |
+
|
391 |
+
// 计算pcen特征
|
392 |
+
// double time_constant = 0.400;
|
393 |
+
// int sr = 22050;
|
394 |
+
// int hop_length = 512;
|
395 |
+
// double t_frames = time_constant * sr / double(hop_length);
|
396 |
+
// double b = (sqrt(1 + 4 * t_frames * t_frames) - 1) / (2 * t_frames * t_frames);
|
397 |
+
// cv::Mat_<double> zi = (cv::Mat_<double>(1, 1) << 0.94361056);
|
398 |
+
//
|
399 |
+
// cv::Mat_<double> in_b = (cv::Mat_<double>(1, 1) << b);
|
400 |
+
// cv::Mat_<double> in_a = (cv::Mat_<double>(1, 2) << 1, b - 1);
|
401 |
+
// cv::Mat_<double> zi = cvlfilter_zi(in_b, in_a);
|
402 |
+
// 第二个公式计算
|
403 |
+
// cv::Mat_<double> S_smooth = cvlfilter(in_b, in_a, mel, zi);
|
404 |
+
|
405 |
+
#if 1 // IIR滤波器
|
406 |
+
if (!filter) {
|
407 |
+
filter = std::make_shared<IIR_I>();
|
408 |
+
double iir_b[1] = {0.05638943879134889};
|
409 |
+
double iir_a[2] = {1.0, -0.9436105612086512};
|
410 |
+
//filter.reset();
|
411 |
+
filter->setPara(iir_b, 1, iir_a, 2);
|
412 |
+
}
|
413 |
+
cv::Mat_<double> S_smooth = cv::Mat_<double>(mel.rows, mel.cols);
|
414 |
+
for (int i = 0; i < mel.rows; i++) {
|
415 |
+
filter->filter(mel[i], S_smooth[i], mel.cols);
|
416 |
+
}
|
417 |
+
|
418 |
+
#endif
|
419 |
+
|
420 |
+
// 第一个公式计算
|
421 |
+
double gain = 0.98;
|
422 |
+
double bias = 2.0;
|
423 |
+
double power = 0.5;
|
424 |
+
double eps = 1e-6;
|
425 |
+
//python: smooth = np.exp(-gain * (np.log(eps) + np.log1p(S_smooth / eps)))
|
426 |
+
cv::Mat_<double> S_smooth_log1p;
|
427 |
+
cv::log(S_smooth / eps + 1, S_smooth_log1p);
|
428 |
+
cv::Mat_<double> smooth;
|
429 |
+
cv::exp((S_smooth_log1p + cv::log(eps)) * (-gain), smooth);
|
430 |
+
//python: S_out = (bias ** power) * np.expm1(power * np.log1p(ref * smooth / bias))
|
431 |
+
cv::Mat_<double> smooth_log1p;
|
432 |
+
cv::Mat_<double> smooth_log1p_exp;
|
433 |
+
cv::log(mel.mul(smooth) / bias + 1, smooth_log1p);
|
434 |
+
cv::exp(power * smooth_log1p, smooth_log1p_exp);
|
435 |
+
cv::Mat_<double> S_out = (smooth_log1p_exp - 1) * pow(bias, power);
|
436 |
+
// transpose
|
437 |
+
cv::Mat_<double> pcen;
|
438 |
+
cv::transpose(S_out, pcen);
|
439 |
+
|
440 |
+
return pcen;
|
441 |
+
}
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/mfcc/sas_util.h
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
#include <string>
|
4 |
+
#include <chrono>
|
5 |
+
#include <vector>
|
6 |
+
#include <assert.h>
|
7 |
+
#include <memory>
|
8 |
+
#include <fstream>
|
9 |
+
#include "opencv2/opencv.hpp"
|
10 |
+
|
11 |
+
using namespace std;
|
12 |
+
using namespace std::chrono;
|
13 |
+
|
14 |
+
class parambase
|
15 |
+
{
|
16 |
+
public:
|
17 |
+
string name;
|
18 |
+
string help;
|
19 |
+
string strval;
|
20 |
+
parambase(){}
|
21 |
+
virtual ~parambase(){}
|
22 |
+
virtual bool set(const char* value) {return true;};
|
23 |
+
};
|
24 |
+
|
25 |
+
/**
|
26 |
+
* �������
|
27 |
+
*/
|
28 |
+
class EnginePar
|
29 |
+
{
|
30 |
+
public:
|
31 |
+
static int cs_timeout; //�кŷ�����ɵij�ʱʱ��(Ĭ���������һ�νкŴ�����һ�����,Ĭ��ֵ5����)
|
32 |
+
static int cs_detecthandsup_time; //�кź���������ֵ�ʱ��(Ĭ��10s)
|
33 |
+
static int cs_detecthandsup_interval ; //�кź���������ֵ�ʱ����(Ĭ��1��1��)
|
34 |
+
static int cs_detectsmile_interval; //�кź�Ц����ʱ����(Ĭ��1��1��)
|
35 |
+
static int cs_detectspeech_interval;//�кź���������ʱ����(Ĭ��20��)
|
36 |
+
static int cs_detectpose_interval; //�кź���̬����ʱ����(Ĭ��5��1��)
|
37 |
+
static int detectpose_interval; //�ǽк��ڼ���̬����ʱ����(Ĭ��5��1��)
|
38 |
+
static int detectsmile_interval; //�ǽк��ڼ�Ц����ʱ����(Ĭ��1��1��)
|
39 |
+
static int detectappearance_interval; //��װ�����
|
40 |
+
static float action_turnpen_thrd; //ת����ֵ
|
41 |
+
static float action_turnchair_thrd; //ת����ֵ
|
42 |
+
static float action_record_time; //����¼��ʱ��
|
43 |
+
static float sit_supporthead_thrd; //��ͷ��ֵ
|
44 |
+
static float sit_layondesk_thrd; //ſ����ֵ
|
45 |
+
static float sit_relyingonchair_thrd;//������ֵ
|
46 |
+
static string log_path;
|
47 |
+
static string log_level;
|
48 |
+
static string temp_path;
|
49 |
+
static bool set(const char* key, const char* val);
|
50 |
+
static bool haskey(const char* key);
|
51 |
+
static const char* getvalue(const char* key);
|
52 |
+
};
|
53 |
+
/**
|
54 |
+
* ����ͷ��������
|
55 |
+
*/
|
56 |
+
enum VideoScene
|
57 |
+
{
|
58 |
+
SCENE_counter, // ��̨
|
59 |
+
SCENE_financial, // ����
|
60 |
+
SCENE_lobby, // ����
|
61 |
+
SCENE_hall // ����
|
62 |
+
};
|
63 |
+
/**
|
64 |
+
* ����ͷ��������
|
65 |
+
*/
|
66 |
+
class VideoPar
|
67 |
+
{
|
68 |
+
private:
|
69 |
+
vector<shared_ptr<parambase>> params;
|
70 |
+
public:
|
71 |
+
VideoScene scene; //����: 1��̨, 2����, 3����, 4����(��װ���)
|
72 |
+
bool audio_enable ; //��Ƶ���� 1��,0��
|
73 |
+
int audio_channels ; //��Ƶͨ���� 0,1,2,4,6
|
74 |
+
int audio_sample_rate ; // ������ 44100, 48000, 96000, 192000
|
75 |
+
bool video_enable ; // ��Ƶ���� 1��,0��
|
76 |
+
//int video_analyse_rate ; //��Ƶ��������: ����>0,ÿ�����֡��
|
77 |
+
bool video_sample_keyframe; //ֻ����ؼ�֡
|
78 |
+
bool video_record; //����¼����Ƶ 1��,0��
|
79 |
+
int video_record_duration; //��Ƶ¼��ʱ��,Ĭ��10s
|
80 |
+
int video_record_reviewtime; //��Ƶ¼�ƻ���ʱ��,Ĭ��5s
|
81 |
+
int face_minsize; //��С������С
|
82 |
+
VideoPar();
|
83 |
+
//~VideoPar();
|
84 |
+
bool set(const char* key, const char* val);
|
85 |
+
static bool haskey(const char* key);
|
86 |
+
};
|
87 |
+
|
88 |
+
template<class T>
|
89 |
+
inline int64_t NowTime()
|
90 |
+
{
|
91 |
+
return std::chrono::time_point_cast<T>(std::chrono::system_clock::now()).time_since_epoch().count();
|
92 |
+
}
|
93 |
+
|
94 |
+
/**--------------------------------- ������models����ģ�����õ��ķ��� ---------------------------------**/
|
95 |
+
|
96 |
+
inline bool detectFileExist(char *file_path) {
|
97 |
+
std::ifstream _ifstream;
|
98 |
+
_ifstream.open(file_path, std::ios::in);
|
99 |
+
if (!_ifstream) {
|
100 |
+
return false;
|
101 |
+
}
|
102 |
+
_ifstream.close();
|
103 |
+
return true;
|
104 |
+
}
|
105 |
+
|
106 |
+
// ����任��������xy������ת
|
107 |
+
inline cv::Mat_<double> rotate_point(cv::Mat_<double> xy, double angle) {
|
108 |
+
cv::Mat rotate_matrix = (cv::Mat_<double>(2, 2) << cos(angle), -sin(angle), sin(angle), cos(angle));
|
109 |
+
cv::transpose(rotate_matrix, rotate_matrix);
|
110 |
+
auto rotate_xy = xy * rotate_matrix;
|
111 |
+
return rotate_xy;
|
112 |
+
}
|
113 |
+
|
114 |
+
// �����Ƿ��ڿ���
|
115 |
+
inline bool check_point_in_rect(cv::Point point, cv::Rect rect) {
|
116 |
+
if ((rect.x < point.x && point.x < rect.x + rect.width) &&
|
117 |
+
(rect.y < point.y && point.y < rect.y + rect.height)) {
|
118 |
+
return true;//��rect�ڲ�
|
119 |
+
} else {
|
120 |
+
return false;//��rect���ϻ��ⲿ
|
121 |
+
}
|
122 |
+
}
|
123 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/munet.cpp
ADDED
@@ -0,0 +1,332 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include "munet.h"
|
2 |
+
#include "cpu.h"
|
3 |
+
#include "face_utils.h"
|
4 |
+
#include "blendgram.h"
|
5 |
+
|
6 |
+
Mobunet::Mobunet(const char* fnbin,const char* fnparam,const char* fnmsk){
|
7 |
+
initModel(fnbin,fnparam,fnmsk);
|
8 |
+
}
|
9 |
+
|
10 |
+
Mobunet::Mobunet(const char* modeldir,const char* modelid){
|
11 |
+
char fnbin[1024];
|
12 |
+
char fnparam[1024];
|
13 |
+
char fnmsk[1024];
|
14 |
+
sprintf(fnbin,"%s/%s.bin",modeldir,modelid);
|
15 |
+
sprintf(fnparam,"%s/%s.param",modeldir,modelid);
|
16 |
+
sprintf(fnmsk,"%s/weight_168u.bin",modeldir);
|
17 |
+
initModel(fnbin,fnparam,fnmsk);
|
18 |
+
}
|
19 |
+
|
20 |
+
int Mobunet::initModel(const char* binfn,const char* paramfn,const char* mskfn){
|
21 |
+
unet.clear();
|
22 |
+
//ncnn::set_cpu_powersave(2);
|
23 |
+
//ncnn::set_omp_num_threads(ncnn::get_big_cpu_count());
|
24 |
+
//unet.opt = ncnn::Option();
|
25 |
+
//unet.opt.use_vulkan_compute = true;
|
26 |
+
unet.opt.num_threads = ncnn::get_big_cpu_count();
|
27 |
+
//unet.load_param("model/mobileunet_v5_wenet_sim.param");
|
28 |
+
//unet.load_model("model/mobileunet_v5_wenet_sim.bin");
|
29 |
+
unet.load_param(paramfn);
|
30 |
+
unet.load_model(binfn);
|
31 |
+
char* wbuf = NULL;
|
32 |
+
dumpfile((char*)mskfn,&wbuf);
|
33 |
+
mat_weights = new JMat(160,160,(uint8_t*)wbuf,1);
|
34 |
+
mat_weights->forceref(0);
|
35 |
+
//mat_weights->show("weight");
|
36 |
+
//cv::waitKey(0);
|
37 |
+
return 0;
|
38 |
+
}
|
39 |
+
|
40 |
+
Mobunet::~Mobunet(){
|
41 |
+
unet.clear();
|
42 |
+
if(mat_weights){
|
43 |
+
delete mat_weights;
|
44 |
+
mat_weights = nullptr;
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
+
int Mobunet::domodelold(JMat* pic,JMat* msk,JMat* feat){
|
49 |
+
JMat picall(160*160,2,3,0,1);
|
50 |
+
uint8_t* buf = picall.udata();
|
51 |
+
int width = pic->width();
|
52 |
+
int height = pic->height();
|
53 |
+
cv::Mat c1(height,width,CV_8UC3,buf);
|
54 |
+
cv::Mat c2(height,width,CV_8UC3,buf+width*height*3);
|
55 |
+
cv::cvtColor(pic->cvmat(),c1,cv::COLOR_RGB2BGR);
|
56 |
+
cv::cvtColor(msk->cvmat(),c2,cv::COLOR_RGB2BGR);
|
57 |
+
ncnn::Mat inall = ncnn::Mat::from_pixels(buf, ncnn::Mat::PIXEL_BGR, 160*160, 2);
|
58 |
+
inall.substract_mean_normalize(mean_vals, norm_vals);
|
59 |
+
//inall.reshape(160,160,6);
|
60 |
+
ncnn::Mat inwenet(256,20,1,feat->data());
|
61 |
+
ncnn::Mat outpic;
|
62 |
+
ncnn::Extractor ex = unet.create_extractor();
|
63 |
+
ex.input("face", inall);
|
64 |
+
ex.input("audio", inwenet);
|
65 |
+
ex.extract("output", outpic);
|
66 |
+
float outmean_vals[3] = {-1.0f, -1.0f, -1.0f};
|
67 |
+
float outnorm_vals[3] = { 0.5f, 0.5f, 0.5f};
|
68 |
+
outpic.substract_mean_normalize(outmean_vals, outnorm_vals);
|
69 |
+
ncnn::Mat pakpic;
|
70 |
+
ncnn::convert_packing(outpic,pakpic,3);
|
71 |
+
cv::Mat cvadj(160,160,CV_32FC3,pakpic.data);
|
72 |
+
//dumpfloat((float*)cvadj.data,160*160*3);
|
73 |
+
cv::Mat cvreal;
|
74 |
+
float scale = 255.0f;
|
75 |
+
cvadj.convertTo(cvreal,CV_8UC3,scale);
|
76 |
+
cv::Mat cvmask;
|
77 |
+
cv::cvtColor(cvreal,cvmask,cv::COLOR_RGB2BGR);
|
78 |
+
cv::imshow("cvreal",cvreal);
|
79 |
+
cv::imshow("cvmask",cvmask);
|
80 |
+
//cv::waitKey(0);
|
81 |
+
//getchar();
|
82 |
+
BlendGramAlpha((uchar*)cvmask.data,(uchar*)mat_weights->data(),(uchar*)pic->data(),160,160);
|
83 |
+
return 0;
|
84 |
+
}
|
85 |
+
|
86 |
+
int Mobunet::domodel(JMat* pic,JMat* msk,JMat* feat){
|
87 |
+
//convert to bgr
|
88 |
+
//pic->tojpg("eee.bmp");
|
89 |
+
|
90 |
+
//JMat picmask(160,160,3,0,1);
|
91 |
+
//JMat picreal(160,160,3,0,1);
|
92 |
+
//cv::cvtColor(pic->cvmat(),picreal.cvmat(),cv::COLOR_RGB2BGR);
|
93 |
+
//cv::cvtColor(msk->cvmat(),picmask.cvmat(),cv::COLOR_RGB2BGR);
|
94 |
+
ncnn::Mat inmask = ncnn::Mat::from_pixels(msk->udata(), ncnn::Mat::PIXEL_BGR2RGB, 160, 160);
|
95 |
+
inmask.substract_mean_normalize(mean_vals, norm_vals);
|
96 |
+
ncnn::Mat inreal = ncnn::Mat::from_pixels(pic->udata(), ncnn::Mat::PIXEL_BGR2RGB, 160, 160);
|
97 |
+
inreal.substract_mean_normalize(mean_vals, norm_vals);
|
98 |
+
//
|
99 |
+
//JMat picin(160*160,6);
|
100 |
+
//char* pd = (char*)picin.data();
|
101 |
+
//memcpy(pd,inreal.data,160*160*3*4);
|
102 |
+
//memcpy(pd+ 160*160*3*4,inmask.data,160*160*3*4);
|
103 |
+
//
|
104 |
+
//ncnn::Mat inpack(160,160,1,pd,(size_t)4u*6,6);
|
105 |
+
//ncnn::Mat inpic;
|
106 |
+
//ncnn::convert_packing(inpack,inpic,1);
|
107 |
+
ncnn::Mat inpic(160,160,6);
|
108 |
+
//printf("===in %d %d all %d %d\n",inreal.cstep,inreal.elempack,inpic.cstep,inpic.elempack);
|
109 |
+
float* buf = (float*)inpic.data;
|
110 |
+
float* pr = (float*)inreal.data;
|
111 |
+
//printf("==%d==%f\n",pic->udata()[2],*pr);
|
112 |
+
memcpy(buf,pr,inreal.cstep*sizeof(float)*inreal.c);
|
113 |
+
/*
|
114 |
+
for(int k=0;k<3;k++){
|
115 |
+
memcpy(buf,pr,inreal.cstep*sizeof(float));
|
116 |
+
buf += inpic.cstep;
|
117 |
+
pr += inreal.cstep;
|
118 |
+
}
|
119 |
+
*/
|
120 |
+
buf+= inpic.cstep*inreal.c;
|
121 |
+
float* pm = (float*)inmask.data;
|
122 |
+
//printf("=%d===%f\n",msk->udata()[2],*pm);
|
123 |
+
memcpy(buf,pm,inmask.cstep*sizeof(float)*inmask.c);
|
124 |
+
/*
|
125 |
+
for(int k=0;k<3;k++){
|
126 |
+
memcpy(buf,pm,inreal.cstep*sizeof(float));
|
127 |
+
buf += inpic.cstep;
|
128 |
+
pm += inmask.cstep;
|
129 |
+
}
|
130 |
+
*/
|
131 |
+
|
132 |
+
ncnn::Mat inwenet(256,20,1,feat->data());
|
133 |
+
//ncnn::Mat inwenet(20,256,1,feat->data());
|
134 |
+
ncnn::Mat outpic;
|
135 |
+
ncnn::Extractor ex = unet.create_extractor();
|
136 |
+
ex.input("face", inpic);
|
137 |
+
ex.input("audio", inwenet);
|
138 |
+
ex.extract("output", outpic);
|
139 |
+
float outmean_vals[3] = {-1.0f, -1.0f, -1.0f};
|
140 |
+
float outnorm_vals[3] = { 127.5f, 127.5f, 127.5f};
|
141 |
+
outpic.substract_mean_normalize(outmean_vals, outnorm_vals);
|
142 |
+
cv::Mat cvout(160,160,CV_8UC3);
|
143 |
+
outpic.to_pixels(cvout.data,ncnn::Mat::PIXEL_RGB2BGR);
|
144 |
+
BlendGramAlpha((uchar*)cvout.data,(uchar*)mat_weights->data(),(uchar*)pic->data(),160,160);
|
145 |
+
//pic->tojpg("fff.bmp");
|
146 |
+
//getchar();
|
147 |
+
/*
|
148 |
+
ncnn::Mat pakpic;
|
149 |
+
ncnn::convert_packing(outpic,pakpic,3);
|
150 |
+
cv::Mat cvadj(160,160,CV_32FC3,pakpic.data);
|
151 |
+
//dumpfloat((float*)cvadj.data,160*160*3);
|
152 |
+
float scale = 255.0f;
|
153 |
+
cvadj.convertTo(picreal.cvmat(),CV_8UC3,scale);
|
154 |
+
cv::cvtColor(picreal.cvmat(),picmask.cvmat(),cv::COLOR_RGB2BGR);
|
155 |
+
*/
|
156 |
+
//getchar();
|
157 |
+
//getchar();
|
158 |
+
//cv::Mat cout;
|
159 |
+
//cv::cvtColor(picreal.cvmat(),cout,cv::COLOR_RGB2BGR);
|
160 |
+
//BlendGramAlpha((uchar*)cout.data,(uchar*)mat_weights->data(),(uchar*)pic->data(),160,160);
|
161 |
+
|
162 |
+
/*
|
163 |
+
float outmean_vals[3] = {-1.0f, -1.0f, -1.0f};
|
164 |
+
// float outnorm_vals[3] = { 2.0f, 2.0f, 2.0f};
|
165 |
+
float outnorm_vals[3] = { 127.5f, 127.5f, 127.5f};
|
166 |
+
outpic.substract_mean_normalize(outmean_vals, outnorm_vals);
|
167 |
+
|
168 |
+
ncnn::Mat pakpic;
|
169 |
+
ncnn::convert_packing(outpic,pakpic,3);
|
170 |
+
//
|
171 |
+
cv::Mat cvadj(160,160,CV_32FC3,pakpic.data);
|
172 |
+
//
|
173 |
+
float scale = 1.0f;
|
174 |
+
cvadj.convertTo(picreal.cvmat(),CV_8UC3,scale);
|
175 |
+
cv::Mat cout;
|
176 |
+
cv::cvtColor(picreal.cvmat(),cout,cv::COLOR_RGB2BGR);
|
177 |
+
BlendGramAlpha((uchar*)cout.data,(uchar*)mat_weights->data(),(uchar*)pic->data(),160,160);
|
178 |
+
//BlendGramAlpha((uchar*)picreal.udata(),(uchar*)mat_weights->data(),(uchar*)pic->data(),160,160);
|
179 |
+
//cv::cvtColor(picreal.cvmat(),pic->cvmat(),cv::COLOR_RGB2BGR);
|
180 |
+
*/
|
181 |
+
return 0;
|
182 |
+
}
|
183 |
+
|
184 |
+
|
185 |
+
int Mobunet::preprocess(JMat* pic,JMat* feat){
|
186 |
+
//pic 168
|
187 |
+
cv::Mat roipic(pic->cvmat(),cv::Rect(4,4,160,160));
|
188 |
+
JMat picmask(160,160,3,0,1);
|
189 |
+
JMat picreal(160,160,3,0,1);
|
190 |
+
cv::Mat cvmask = picmask.cvmat();
|
191 |
+
cv::Mat cvreal = picreal.cvmat();
|
192 |
+
roipic.copyTo(cvmask);
|
193 |
+
roipic.copyTo(cvreal);
|
194 |
+
cv::rectangle(cvmask,cv::Rect(5,5,150,145),cv::Scalar(0,0,0),-1);//,cv::LineTypes::FILLED);
|
195 |
+
domodel(&picreal,&picmask,feat);
|
196 |
+
cvreal.copyTo(roipic);
|
197 |
+
return 0;
|
198 |
+
}
|
199 |
+
|
200 |
+
int Mobunet::fgprocess(JMat* pic,const int* boxs,JMat* feat,JMat* fg){
|
201 |
+
int boxx, boxy ,boxwidth, boxheight ;
|
202 |
+
boxx = boxs[0];boxy=boxs[1];boxwidth=boxs[2]-boxx;boxheight=boxs[3]-boxy;
|
203 |
+
int stride = pic->stride();
|
204 |
+
cv::Mat roisrc(pic->cvmat(),cv::Rect(boxx,boxy,boxwidth,boxheight));
|
205 |
+
cv::Mat cvorig;
|
206 |
+
cv::resize(roisrc , cvorig, cv::Size(168, 168), cv::INTER_AREA);
|
207 |
+
JMat pic168(168,168,(uint8_t*)cvorig.data);
|
208 |
+
preprocess(&pic168,feat);
|
209 |
+
cv::Mat cvrst;;
|
210 |
+
cv::resize(cvorig , cvrst, cv::Size(boxwidth, boxheight), cv::INTER_AREA);
|
211 |
+
cv::Mat roidst(fg->cvmat(),cv::Rect(boxx,boxy,boxwidth,boxheight));
|
212 |
+
cvrst.copyTo(roidst);
|
213 |
+
return 0;
|
214 |
+
}
|
215 |
+
|
216 |
+
int Mobunet::process(JMat* pic,const int* boxs,JMat* feat){
|
217 |
+
int boxx, boxy ,boxwidth, boxheight ;
|
218 |
+
boxx = boxs[0];boxy=boxs[1];boxwidth=boxs[2]-boxx;boxheight=boxs[3]-boxy;
|
219 |
+
int stride = pic->stride();
|
220 |
+
cv::Mat roisrc(pic->cvmat(),cv::Rect(boxx,boxy,boxwidth,boxheight));
|
221 |
+
cv::Mat cvorig;
|
222 |
+
cv::resize(roisrc , cvorig, cv::Size(168, 168), cv::INTER_AREA);
|
223 |
+
JMat pic168(168,168,(uint8_t*)cvorig.data);
|
224 |
+
preprocess(&pic168,feat);
|
225 |
+
cv::Mat cvrst;;
|
226 |
+
cv::resize(cvorig , cvrst, cv::Size(boxwidth, boxheight), cv::INTER_AREA);
|
227 |
+
cvrst.copyTo(roisrc);
|
228 |
+
return 0;
|
229 |
+
}
|
230 |
+
|
231 |
+
int Mobunet::process2(JMat* pic,const int* boxs,JMat* feat){
|
232 |
+
int boxx, boxy ,boxwidth, boxheight ;
|
233 |
+
boxx = boxs[0];boxy=boxs[1];boxwidth=boxs[2]-boxx;boxheight=boxs[3]-boxy;
|
234 |
+
int stride = pic->stride();
|
235 |
+
|
236 |
+
cv::Mat cvsrc = pic->cvmat();
|
237 |
+
printf("cvsrc %d %d \n",cvsrc.cols,cvsrc.rows);
|
238 |
+
cv::Mat roisrc(cvsrc,cv::Rect(boxx,boxy,boxwidth,boxheight));
|
239 |
+
cv::Mat cvorig;
|
240 |
+
cv::resize(roisrc , cvorig, cv::Size(168, 168), cv::INTER_AREA);
|
241 |
+
/*
|
242 |
+
uint8_t* data =(uint8_t*)pic->data() + boxy*stride + boxx*pic->channel();
|
243 |
+
int scale_w = 168;
|
244 |
+
int scale_h = 168;
|
245 |
+
ncnn::Mat prepic = ncnn::Mat::from_pixels_resize(data, ncnn::Mat::PIXEL_BGR, boxwidth, boxheight, stride,scale_w, scale_h);
|
246 |
+
//pic 168
|
247 |
+
cv::Mat cvorig(168,168,CV_8UC3,prepic.data);
|
248 |
+
*/
|
249 |
+
|
250 |
+
cv::Mat roimask(cvorig,cv::Rect(4,4,160,160));
|
251 |
+
JMat picmask(160,160,3,0,1);
|
252 |
+
JMat picreal(160,160,3,0,1);
|
253 |
+
cv::Mat cvmask = picmask.cvmat();
|
254 |
+
cv::Mat cvreal = picreal.cvmat();
|
255 |
+
roimask.copyTo(cvmask);
|
256 |
+
roimask.copyTo(cvreal);
|
257 |
+
|
258 |
+
cv::rectangle(cvmask,cv::Rect(5,5,150,150),cv::Scalar(0,0,0),-1);//,cv::LineTypes::FILLED);
|
259 |
+
// cv::imshow("000",cvorig);
|
260 |
+
// cv::imshow("aaa",cvmask);
|
261 |
+
// cv::imshow("bbb",cvreal);
|
262 |
+
// cv::waitKey(0);
|
263 |
+
|
264 |
+
ncnn::Mat inmask = ncnn::Mat::from_pixels(picmask.udata(), ncnn::Mat::PIXEL_BGR2RGB, 160, 160);
|
265 |
+
inmask.substract_mean_normalize(mean_vals, norm_vals);
|
266 |
+
ncnn::Mat inreal = ncnn::Mat::from_pixels(picreal.udata(), ncnn::Mat::PIXEL_BGR2RGB, 160, 160);
|
267 |
+
inreal.substract_mean_normalize(mean_vals, norm_vals);
|
268 |
+
|
269 |
+
JMat picin(160*160,2,3);
|
270 |
+
char* pd = (char*)picin.data();
|
271 |
+
memcpy(pd,inreal.data,160*160*3*4);
|
272 |
+
memcpy(pd+ 160*160*3*4,inmask.data,160*160*3*4);
|
273 |
+
|
274 |
+
// char* pinpic = NULL;
|
275 |
+
// dumpfile("pic.bin",&pinpic);
|
276 |
+
// dumpfloat((float*)pd,10);
|
277 |
+
// dumpfloat((float*)pinpic,10);
|
278 |
+
//ncnn::Mat inpic(160,160,6,pd,4);
|
279 |
+
ncnn::Mat inpack(160,160,1,pd,(size_t)4u*6,6);
|
280 |
+
ncnn::Mat inpic;
|
281 |
+
ncnn::convert_packing(inpack,inpic,1);
|
282 |
+
|
283 |
+
// char* pwenet = NULL;
|
284 |
+
// dumpfile("wenet.bin",&pwenet);
|
285 |
+
ncnn::Mat inwenet(256,20,1,feat->data(),4);
|
286 |
+
ncnn::Mat outpic;
|
287 |
+
ncnn::Extractor ex = unet.create_extractor();
|
288 |
+
ex.input("face", inpic);
|
289 |
+
ex.input("audio", inwenet);
|
290 |
+
ex.extract("output", outpic);
|
291 |
+
|
292 |
+
float outmean_vals[3] = {-1.0f, -1.0f, -1.0f};
|
293 |
+
// float outnorm_vals[3] = { 2.0f, 2.0f, 2.0f};
|
294 |
+
float outnorm_vals[3] = { 127.5f, 127.5f, 127.5f};
|
295 |
+
outpic.substract_mean_normalize(outmean_vals, outnorm_vals);
|
296 |
+
|
297 |
+
ncnn::Mat pakpic;
|
298 |
+
ncnn::convert_packing(outpic,pakpic,3);
|
299 |
+
|
300 |
+
cv::Mat cvadj(160,160,CV_32FC3,pakpic.data);
|
301 |
+
cv::Mat cvout(160,160,CV_8UC3);
|
302 |
+
float scale = 1.0f;
|
303 |
+
cvadj.convertTo(cvout,CV_8UC3,scale);
|
304 |
+
//cv::imwrite("cvout.jpg",cvout);
|
305 |
+
cv::cvtColor(cvout,roimask,cv::COLOR_RGB2BGR);
|
306 |
+
// cvout.copyTo(roimask);
|
307 |
+
|
308 |
+
//cv::imwrite("roimask.jpg",roimask);
|
309 |
+
//cv::imwrite("cvorig.jpg",cvorig);
|
310 |
+
//cv::waitKey(0);
|
311 |
+
cv::resize(cvorig , roisrc, cv::Size(boxwidth, boxheight), cv::INTER_AREA);
|
312 |
+
//cv::imwrite("roisrc.jpg",roisrc);
|
313 |
+
//cv::imshow("cvsrc",cvsrc);
|
314 |
+
// cv::imshow("roisrc",roisrc);
|
315 |
+
// cv::imshow("cvorig",cvorig);
|
316 |
+
// cv::waitKey(20);
|
317 |
+
/*
|
318 |
+
{
|
319 |
+
uint8_t *pr = (uint8_t *) cvoutc.data;
|
320 |
+
printf("==%u %u %u\n", pr[0], pr[1], pr[2]);
|
321 |
+
}
|
322 |
+
//
|
323 |
+
float* p = (float*)cvadj.data;
|
324 |
+
printf("==%f %f %f\n",p[0],p[1],p[2]);
|
325 |
+
p+=160*160;
|
326 |
+
printf("==%f %f %f\n",p[0],p[1],p[2]);
|
327 |
+
p+=160*160;
|
328 |
+
printf("==%f %f %f\n",p[0],p[1],p[2]);
|
329 |
+
*/
|
330 |
+
return 0;
|
331 |
+
}
|
332 |
+
|
duix-android/dh_aigc_android/duix-sdk/src/main/cpp/aisdk/munet.h
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
#include "jmat.h"
|
3 |
+
#include "net.h"
|
4 |
+
#include <opencv2/core/core.hpp>
|
5 |
+
#include <opencv2/highgui/highgui.hpp>
|
6 |
+
#include <opencv2/imgproc/imgproc.hpp>
|
7 |
+
#include <stdio.h>
|
8 |
+
#include <vector>
|
9 |
+
|
10 |
+
|
11 |
+
class Mobunet{
|
12 |
+
private:
|
13 |
+
ncnn::Net unet;
|
14 |
+
float mean_vals[3] = {127.5f, 127.5f, 127.5f};
|
15 |
+
float norm_vals[3] = {1 / 127.5f, 1 / 127.5f, 1 / 127.5f};
|
16 |
+
JMat* mat_weights = nullptr;
|
17 |
+
int initModel(const char* binfn,const char* paramfn,const char* mskfn);
|
18 |
+
public:
|
19 |
+
int domodel(JMat* pic,JMat* msk,JMat* feat);
|
20 |
+
int domodelold(JMat* pic,JMat* msk,JMat* feat);
|
21 |
+
int preprocess(JMat* pic,JMat* feat);
|
22 |
+
int process(JMat* pic,const int* boxs,JMat* feat);
|
23 |
+
int fgprocess(JMat* pic,const int* boxs,JMat* feat,JMat* fg);
|
24 |
+
int process2(JMat* pic,const int* boxs,JMat* feat);
|
25 |
+
Mobunet(const char* modeldir,const char* modelid);
|
26 |
+
Mobunet(const char* fnbin,const char* fnparam,const char* fnmsk);
|
27 |
+
~Mobunet();
|
28 |
+
};
|