-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext_template.c
More file actions
241 lines (222 loc) · 11.8 KB
/
ext_template.c
File metadata and controls
241 lines (222 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// #include <required_standard_headers.h>
#include <stdint.h>
#include <stddef.h>
// #include <required_project_headers.h>
// #include <required_custom_headers.h>
// SHARED: 表示这个函数是导出函数
// SHARED: This function is exported
#if defined(_WIN32) || defined(_WIN64)
#define SHARED __declspec(dllexport)
#define SUPPORT_DLLENTRY
#else
#define SHARED __attribute__((visibility("default")))
#endif
#ifdef __GNUC__
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
// 签名: 验证扩展是否加载正确
// Sign: Verify that the extension is loaded correctly
SHARED const char img2arr_ext_sign[] = "img2arr.<stage>.<type>.<name>";
// 扩展属性enum。用于为管线进行特化提示,以进行优化。仅预处理阶段使用。
// Extension attribute enum. Used to specialize the pipeline for optimization. Only used in the preprocessing stage.
enum ExtAttr{
/*
没有任何额外属性,这会确保函数接收到的输入缓冲区和输出缓冲区始终不同。
No additional attributes, this ensures that the input and output buffers received by the function are always different.
*/
ATTR_NONE = 0,
/*
可以重用输入缓冲区,函数接收到的输入缓冲区和输出缓冲区有可能相同。
The input buffer can be reused, and the input and output buffers received by the function may be the same.
*/
ATTR_REUSE = 1,
/*
只读扩展。保证函数的输出缓冲区是NULL。
Read-only extension. Ensure that the output buffer of the function is NULL.
*/
ATTR_READONLY = 2
};
/**
* @brief 初始化函数。当扩展被加载时,会被调用一次,在重新加载前不会再次调用。
* This function is called once when the extension is loaded, and it will not be called again before reloading.
* @return 错误码,0表示成功,非0表示失败。若函数无返回,则可能返回随机值
* Error code, 0 means success, non-0 means failure. If the function has no return, it may return a random value.
* @note 没有此函数并不会导致扩展加载失败,只是无法自定义初始化。
* @note No such function will not cause the extension to fail to load, but it cannot be customized to initialize.
*/
SHARED int init(void){
// Implement here.
return 0;
}
// ext.py传入的参数解析结构体。可以作为处理结果输出。
// The parameter parsing structure passed in by ext.py. It can be used as the output of the processing result.
typedef struct {
// 这里填写参数列表
// Fill in the output parameter list here
}__attribute__((packed)) args_t;
/**
* @brief 获取输出数据信息。在调用`f0`或`f1`之前会被调用以确认输出缓冲区大小及其属性.
* Get output data information. It will be called before calling `f0` or `f1` to confirm the size and attributes of the output buffer.
* @param args[in/out] 参数解析结构体。
* Parameter parsing structure.
* @param in_shape[in] 输入缓冲区形状。关于具体内容,参考下面的注释说明。
* Input buffer shape. For specific content, refer to the comment description below.
* @param out_shape[out] 输出缓冲区形状。关于具体内容,参考下面的注释说明。
* Output buffer shape. For specific content, refer to the comment description below.
* @param attr[out] 扩展属性,应是`ExtAttr`中的某个值。当扩展是预处理扩展是时,它用于为管线进行特化提示,以进行优化,其余类型则无效。若不赋值,则默认为`ATTR_NONE`。
* Extension attribute, should be a value in `ExtAttr`. When the extension is a preprocessing extension, it is used to specialize the pipeline for optimization, otherwise it is invalid. If not assigned, the default is `ATTR_NONE`.
* @return 错误码,0表示成功,非0表示失败。若函数无返回,则返回随机值,容易导致错误。
* Error code, 0 means success, non-0 means failure. If the function has no return, it returns a random value, which is easy to cause errors.
*/
SHARED int io_GetOutInfo(args_t* args, size_t in_shape[ ], size_t out_shape[ ], int* attr){
// 指定输出的尺寸及其属性
// Specify the size and attributes of the output
// const size_t height = in_shape[0];
// const size_t width = in_shape[1];
// out_shape[0] = height;
// out_shape[1] = width;
// *attr = ATTR_NONE;
// Other Implement here.
return 0;
}
/**
* @brief 主函数:多线程实现。
* Multi-threaded implementation.
* @param threads[in] 任务数。
* Number of tasks.
* @param idx[in] 任务索引。
* Task index.
* @param args[in/out] 参数解析结构体。
* Parameter parsing structure.
* @param in_buf[in] 输入缓冲区,格式为`[*in_shape, 4]`。
* Input buffer, format is `[*in_shape, 4]`.
* @param out_buf[out] 输出缓冲区。大小由`io_GetOutInfo`指定。
* Output buffer. The size is specified by `io_GetOutInfo`.
* @param in_shape[in] 输入缓冲区形状。关于具体内容,参考下面的注释说明。
* Input buffer shape. For specific content, refer to the comment description below.
* @return 错误码,0表示成功,非0表示失败。若函数无返回,则可能返回随机值
* Error code, 0 means success, non-0 means failure. If the function has no return, it may return a random value.
*/
SHARED int f1(size_t threads, size_t idx, args_t* args, uint8_t* in_buf, uint8_t* out_buf, size_t in_shape[ ]){
// 计算该线程处理的像素点范围。如果需要特殊需求,请自行修改。
// Calculate the pixel range processed by this thread. If you need special requirements, please modify it yourself.
const size_t size = in_shape[0] * in_shape[1];
const size_t start = (size * idx / threads);
const size_t end = (size * (idx + 1) / threads);
// Implement here. If no return
return 0;
}
/**
* @brief 主函数:单线程实现。
* Single-threaded implementation.
* @param args[in/out] 参数解析结构体。
* Parameter parsing structure.
* @param in_buf[in] 输入缓冲区,格式为`[*in_shape, 4]`。
* Input buffer, format is `[*in_shape, 4]`.
* @param out_buf[out] 输出缓冲区。大小由`io_GetOutInfo`指定。
* Output buffer. The size is specified by `io_GetOutInfo`.
* @param in_shape[in] 输入缓冲区形状。关于具体内容,参考下面的注释说明。
* Input buffer shape. For specific content, refer to the comment description below.
* @return 错误码,0表示成功,非0表示失败。若函数无返回,则可能返回随机值
* Error code, 0 means success, non-0 means failure. If the function has no return, it may return a random value.
*/
SHARED int f0(args_t* args, uint8_t* in_buf, uint8_t* out_buf, size_t in_shape[ ]){
// Implement here.
// f1(1, 0, args, in_buf, out_buf, in_shape);
return 0;
}
/**
* @brief 获取预览图像输出信息。在调用`f0p`或`f1p`之前会被调用以确认输出缓冲区大小及其属性。仅在编码阶段扩展中有效。
* Get output data information. It will be called before calling `f0` or `f1` to confirm the size and attributes of the output buffer. Only valid in the encoding stage extension.
* @param args[in/out] 参数解析结构体。
* Parameter parsing structure.
* @param in_shape[in] 输入缓冲区形状。关于具体内容,参考下面的注释说明。
* Input buffer shape. For specific content, refer to the comment description below.
* @param out_shape[out] 输出缓冲区形状。关于具体内容,参考下面的注释说明。
* Output buffer shape. For specific content, refer to the comment description below.
* @return 错误码,0表示成功,非0表示失败。若函数无返回,则返回随机值
* Error code, 0 means success, non-0 means failure. If the function has no return, it returns a random value
*/
SHARED int io_GetViewOutInfo(args_t* args, size_t in_shape[ ], size_t out_shape[ ]){
// 指定输出的尺寸及其属性
// Specify the size and attributes of the output
// const size_t height = in_shape[0];
// const size_t width = in_shape[1];
// out_shape[0] = height;
// out_shape[1] = width;
// *attr = ATTR_NONE;
// Other Implement here.
return 0;
}
/**
* @brief 预览图像函数:多线程实现。仅在编码阶段扩展中有效。
* Multi-threaded implementation.
* @param threads[in] 任务数。
* Number of tasks.
* @param idx[in] 任务索引。
* Task index.
* @param args[in/out] 参数解析结构体。
* Parameter parsing structure.
* @param in_buf[in] 输入缓冲区,格式为`[*in_shape, 4]`。
* Input buffer, format is `[*in_shape, 4]`.
* @param out_buf[out] 输出缓冲区。大小由`io_GetViewOutInfo`指定。
* Output buffer. The size is specified by `io_GetViewOutInfo`.
* @param in_shape[in] 输入缓冲区形状。关于具体内容,参考下面的注释说明。
* Input buffer shape. For specific content, refer to the comment description below.
* @return 错误码,0表示成功,非0表示失败。若函数无返回,则可能返回随机值
* Error code, 0 means success, non-0 means failure. If the function has no return, it may return a random value.
*/
SHARED int f1p(size_t threads, size_t idx, args_t* args, uint8_t* in_buf, uint8_t* out_buf, size_t in_shape[ ]){
// 计算该线程处理的像素点范围。如果需要特殊需求,请自行修改。
// Calculate the pixel range processed by this thread. If you need special requirements, please modify it yourself.
const size_t size = in_shape[0] * in_shape[1];
const size_t start = (size * idx / threads);
const size_t end = (size * (idx + 1) / threads);
// Implement here.
return 0;
}
/**
* @brief 预览图像函数:单线程实现。仅在编码阶段扩展中有效。
* Single-threaded implementation.
* @param args[in/out] 参数解析结构体。
* Parameter parsing structure.
* @param in_buf[in] 输入缓冲区,格式为`[*in_shape, 4]`。
* Input buffer, format is `[*in_shape, 4]`.
* @param out_buf[out] 输出缓冲区。大小由`io_GetViewOutInfo`指定。
* Output buffer. The size is specified by `io_GetViewOutInfo`.
* @param in_shape[in] 输入缓冲区形状。关于具体内容,参考下面的注释说明。
* Input buffer shape. For specific content, refer to the comment description below.
* @return 错误码,0表示成功,非0表示失败。若函数无返回,则可能返回随机值
* Error code, 0 means success, non-0 means failure. If the function has no return, it may return a random value.
*/
SHARED int f0p(args_t* args, uint8_t* in_buf, uint8_t* out_buf, size_t in_shape[ ]){
// Implement here.
// f1p(1, 0, args, in_buf, out_buf, in_shape);
return 0;
}
/*
缓冲区形状说明:
缓冲区分为图像缓冲区、数据缓冲区,未来可能会进一步增加。
对于图像缓冲区,其shape为`[height, width]`,一个像素是RGBA8888,
此时:`shape[0]`表示图像的高度,`shape[1]`表示图像的宽度。
对于数据缓冲区,其shape为`[length]`,一个元素是uint8_t,
此时:`shape[0]`表示数据的长度。
预处理扩展中:
io_GetOutInfo: in_shape[height, width] -> out_shape[height, width]
f0: in_buffer[height, width, 4] -> out_buffer[out_shape[0], out_shape[1], 4]
f1同理。
编码扩展中:
io_GetOutInfo: in_shape[height, width] -> out_shape[length]
io_GetViewOutInfo: in_shape[height, width] -> out_shape_v[height, width]
f0: in_buffer[height, width, 4] -> out_buffer[out_shape[0]]
f0p: in_buffer[height, width, 4] -> out_buffer[out_shape_v[0], out_shape_v[1], 4]
f1同理。
输出扩展中:
io_GetOutInfo: in_shape[length] -> out_shape[length]
f0: in_buffer[length] -> out_buffer[out_shape[0]]
f1同理。
*/