1 /*
2 * The MIT License (MIT)
3 * This file is part of waifu2x-converter-cpp
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23 
24 #ifndef W2XCONV_H
25 #define W2XCONV_H
26 
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdbool.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #ifdef _WIN32
36 
37 #ifdef W2XCONV_IMPL
38 #define W2XCONV_EXPORT __declspec(dllexport)
39 #else
40 #define W2XCONV_EXPORT __declspec(dllimport)
41 #endif
42 
43 #else
44 
45 #ifdef W2XCONV_IMPL
46 #define W2XCONV_EXPORT __attribute__((visibility("default")))
47 #else
48 #define W2XCONV_EXPORT
49 #endif
50 
51 #endif
52 
53 #ifndef W2XCONV_TCHAR
54 #if defined(_WIN32) && defined(_UNICODE)
55 #define W2XCONV_TCHAR wchar_t
56 #else
57 #define W2XCONV_TCHAR char
58 #endif
59 #endif
60 
61 enum W2XConvGPUMode
62 {
63 	W2XCONV_GPU_DISABLE = 0,
64 	W2XCONV_GPU_AUTO = 1,
65 	W2XCONV_GPU_FORCE_OPENCL = 2
66 };
67 
68 enum W2XConvErrorCode
69 {
70 	W2XCONV_NOERROR,
71 	W2XCONV_ERROR_WIN32_ERROR,	/* errno_ = GetLastError() */
72 	W2XCONV_ERROR_WIN32_ERROR_PATH, /* u.win32_path */
73 	W2XCONV_ERROR_LIBC_ERROR,	/* errno_ */
74 	W2XCONV_ERROR_LIBC_ERROR_PATH,	/* libc_path */
75 
76 	W2XCONV_ERROR_MODEL_LOAD_FAILED, /* u.path */
77 
78 	W2XCONV_ERROR_IMREAD_FAILED,	/* u.path */
79 	W2XCONV_ERROR_IMWRITE_FAILED,	/* u.path */
80 
81 	W2XCONV_ERROR_RGB_MODEL_MISMATCH_TO_Y,
82 	W2XCONV_ERROR_Y_MODEL_MISMATCH_TO_RGB_F32,
83 
84 	W2XCONV_ERROR_OPENCL,	/* u.cl_error */
85 
86 	W2XCONV_ERROR_SCALE_LIMIT,
87 	W2XCONV_ERROR_SIZE_LIMIT,
88 	W2XCONV_ERROR_WEBP_SIZE_LIMIT,
89 	W2XCONV_ERROR_WEBP_LOSSY_SIZE_LIMIT,
90 };
91 
92 struct W2XConvError
93 {
94 	enum W2XConvErrorCode code;
95 
96 	union
97 	{
98 		char *path;
99 		unsigned int errno_;
100 
101 		struct
102 		{
103 			unsigned int errno_;
104 			char *path;
105 		} win32_path;
106 
107 		struct
108 		{
109 			int errno_;
110 			char *path;
111 		} libc_path;
112 
113 		struct
114 		{
115 			int error_code;
116 			int dev_id;
117 		} cl_error;
118 	} u;
119 };
120 
121 W2XCONV_EXPORT char *w2xconv_strerror(struct W2XConvError *e); /* should be free by w2xcvonv_free() */
122 W2XCONV_EXPORT void w2xconv_free(void *p);
123 
124 struct W2XConvFlopsCounter
125 {
126 	double flop;
127 	double filter_sec;
128 	double process_sec;
129 };
130 
131 enum W2XConvProcessorType
132 {
133 	W2XCONV_PROC_HOST,
134 	W2XCONV_PROC_CUDA,
135 	W2XCONV_PROC_OPENCL
136 };
137 
138 enum W2XConvFilterType
139 {
140 	W2XCONV_FILTER_DENOISE0,
141 	W2XCONV_FILTER_DENOISE1,
142 	W2XCONV_FILTER_DENOISE2,
143 	W2XCONV_FILTER_DENOISE3,
144 	W2XCONV_FILTER_SCALE2x
145 };
146 
147 /* W2XConvProcessor::sub_type */
148 #define W2XCONV_PROC_HOST_OPENCV 0x0000
149 #define W2XCONV_PROC_HOST_SSE3 0x0001
150 #define W2XCONV_PROC_HOST_AVX 0x0002
151 #define W2XCONV_PROC_HOST_FMA 0x0003
152 
153 #define W2XCONV_PROC_HOST_NEON 0x0104
154 
155 #define W2XCONV_PROC_HOST_ALTIVEC 0x0205
156 
157 #define W2XCONV_PROC_CUDA_NVIDIA 0
158 
159 #define W2XCONV_PROC_OPENCL_PLATFORM_MASK 0x00ff
160 #define W2XCONV_PROC_OPENCL_DEVICE_MASK   0xff00
161 
162 #define W2XCONV_PROC_OPENCL_PLATFORM_NVIDIA 0
163 #define W2XCONV_PROC_OPENCL_PLATFORM_AMD 1
164 #define W2XCONV_PROC_OPENCL_PLATFORM_INTEL 2
165 #define W2XCONV_PROC_OPENCL_PLATFORM_UNKNOWN 0xff
166 
167 #define W2XCONV_PROC_OPENCL_DEVICE_CPU (1<<8)
168 #define W2XCONV_PROC_OPENCL_DEVICE_GPU (2<<8)
169 #define W2XCONV_PROC_OPENCL_DEVICE_UNKNOWN (0xff<<8)
170 
171 #define W2XCONV_PROC_OPENCL_NVIDIA_GPU (W2XCONV_PROC_OPENCL_PLATFORM_NVIDIA | W2XCONV_PROC_OPENCL_DEVICE_GPU)
172 #define W2XCONV_PROC_OPENCL_AMD_GPU (W2XCONV_PROC_OPENCL_PLATFORM_AMD | W2XCONV_PROC_OPENCL_DEVICE_GPU)
173 #define W2XCONV_PROC_OPENCL_INTEL_GPU (W2XCONV_PROC_OPENCL_PLATFORM_INTEL | W2XCONV_PROC_OPENCL_DEVICE_GPU)
174 #define W2XCONV_PROC_OPENCL_UNKNOWN_GPU (W2XCONV_PROC_OPENCL_PLATFORM_UNKNOWN | W2XCONV_PROC_OPENCL_DEVICE_GPU)
175 
176 #define W2XCONV_PROC_OPENCL_AMD_CPU (W2XCONV_PROC_OPENCL_PLATFORM_AMD | W2XCONV_PROC_OPENCL_DEVICE_CPU)
177 #define W2XCONV_PROC_OPENCL_INTEL_CPU (W2XCONV_PROC_OPENCL_PLATFORM_INTEL | W2XCONV_PROC_OPENCL_DEVICE_CPU)
178 #define W2XCONV_PROC_OPENCL_UNKNOWN_CPU (W2XCONV_PROC_OPENCL_PLATFORM_UNKNOWN | W2XCONV_PROC_OPENCL_DEVICE_CPU)
179 
180 #define W2XCONV_PROC_OPENCL_UNKNOWN (W2XCONV_PROC_OPENCL_PLATFORM_UNKNOWN | W2XCONV_PROC_OPENCL_DEVICE_UNKNOWN)
181 
182 struct W2XConvProcessor
183 {
184 	enum W2XConvProcessorType type;
185 	int sub_type;
186 	int dev_id;
187 	int num_core;
188 	const char *dev_name;
189 };
190 
191 struct W2XConvThreadPool;
192 
193 struct W2XConv
194 {
195 	/* public */
196 	struct W2XConvError last_error;
197 	struct W2XConvFlopsCounter flops;
198 	const struct W2XConvProcessor *target_processor;
199 	int log_level;
200 	bool tta_mode;
201 
202 	/* internal */
203 	struct W2XConvImpl *impl;
204 };
205 
206 struct w2xconv_rgb_float3
207 {
208 	float r;
209 	float g;
210 	float b;
211 };
212 
213 W2XCONV_EXPORT	void get_png_background_colour(FILE *png_fp, bool *png_rgb, struct w2xconv_rgb_float3 *bkgd_colour);
214 
215 W2XCONV_EXPORT const struct W2XConvProcessor *w2xconv_get_processor_list(size_t *ret_num);
216 
217 W2XCONV_EXPORT struct W2XConv *w2xconv_init(enum W2XConvGPUMode gpu, int njob /* 0 = auto */, int log_level);
218 W2XCONV_EXPORT struct W2XConv *w2xconv_init_with_tta(enum W2XConvGPUMode gpu, int njob /* 0 = auto */, int log_level, bool tta_mode);
219 
220 W2XCONV_EXPORT struct W2XConv *w2xconv_init_with_processor(int processor_idx, int njob, int log_level);
221 W2XCONV_EXPORT struct W2XConv *w2xconv_init_with_processor_and_tta(int processor_idx, int njob, int log_level, bool tta_mode);
222 
223 /* return negative if failed */
224 W2XCONV_EXPORT int w2xconv_load_model(const int denoise_level, struct W2XConv *conv, const W2XCONV_TCHAR *model_dir);
225 W2XCONV_EXPORT int w2xconv_load_models(struct W2XConv *conv, const W2XCONV_TCHAR *model_dir);
226 
227 W2XCONV_EXPORT void w2xconv_set_model_3x3
228 (
229 	struct W2XConv *conv,
230 	enum W2XConvFilterType m,
231 	int layer_depth,
232 	int num_input_plane,
233 	const int *num_map, // num_map[layer_depth]
234 	const float *coef_list, // coef_list[layer_depth][num_map][3x3]
235 	const float *bias // bias[layer_depth][num_map]
236 );
237 
238 
239 W2XCONV_EXPORT void w2xconv_fini(struct W2XConv *conv);
240 
241 #ifdef HAVE_OPENCV
242 W2XCONV_EXPORT int w2xconv_convert_file
243 (
244 	struct W2XConv *conv,
245 	const W2XCONV_TCHAR *dst_path,
246 	const W2XCONV_TCHAR *src_path,
247 	int denoise_level, /* -1:none, 0:L0 denoise, 1:L1 denoise, 2:L2 denoise, 3:L3 denoise  */
248 	double scale,
249 	int block_size,
250 	int* imwrite_params
251 );
252 
253 W2XCONV_EXPORT int w2xconv_convert_rgb
254 (
255 	struct W2XConv *conv,
256 	unsigned char *dst, size_t dst_step_byte, /* rgb24 (src_w*ratio, src_h*ratio) */
257 	unsigned char *src, size_t src_step_byte, /* rgb24 (src_w, src_h) */
258 	int src_w, int src_h,
259 	int denoise_level, /* -1:none, 0:L0 denoise, 1:L1 denoise, 2:L2 denoise, 3:L3 denoise  */
260 	double scale,
261 	int block_size
262 );
263 
264 W2XCONV_EXPORT int w2xconv_convert_rgb_f32
265 (
266 	struct W2XConv *conv,
267 	unsigned char *dst, size_t dst_step_byte, /* rgb float32x3 normalized[0-1] (src_w*ratio, src_h*ratio) */
268 	unsigned char *src, size_t src_step_byte, /* rgb float32x3 normalized[0-1] (src_w, src_h) */
269 	int src_w, int src_h,
270 	int denoise_level, /* -1:none, 0:L0 denoise, 1:L1 denoise, 2:L2 denoise, 3:L3 denoise  */
271 	double scale,
272 	int block_size
273 );
274 
275 W2XCONV_EXPORT int w2xconv_convert_yuv
276 (
277 	struct W2XConv *conv,
278 	unsigned char *dst, size_t dst_step_byte, /* float32x3 normalized[0-1] (src_w*ratio, src_h*ratio) */
279 	unsigned char *src, size_t src_step_byte, /* float32x3 normalized[0-1] (src_w, src_h) */
280 	int denoise_level, /* -1:none, 0:L0 denoise, 1:L1 denoise, 2:L2 denoise, 3:L3 denoise  */
281 	double scale,
282 	int block_size
283 );
284 #endif
285 
286 W2XCONV_EXPORT int w2xconv_apply_filter_y
287 (
288 	struct W2XConv *conv,
289 	enum W2XConvFilterType type,
290 	unsigned char *dst, size_t dst_step_byte, /* float32x1 normalized[0-1] (src_w, src_h) */
291 	unsigned char *src, size_t src_step_byte, /* float32x1 normalized[0-1] (src_w, src_h) */
292 	int src_w, int src_h,
293 	int block_size
294 );
295 
296 W2XCONV_EXPORT int w2xconv_test(struct W2XConv *conv, int block_size);
297 
298 #ifdef __cplusplus
299 }
300 #endif
301 
302 #endif
303