You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

c_api.cpp 39 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /* Tencent is pleased to support the open source community by making ncnn available.
  2. *
  3. * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. *
  5. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. * in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * https://opensource.org/licenses/BSD-3-Clause
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. */
  15. #include "c_api.h"
  16. #include <stdlib.h>
  17. #include "allocator.h"
  18. #include "blob.h"
  19. #include "datareader.h"
  20. #include "layer.h"
  21. #include "mat.h"
  22. #include "modelbin.h"
  23. #include "net.h"
  24. #include "option.h"
  25. #include "paramdict.h"
  26. using ncnn::Allocator;
  27. using ncnn::Blob;
  28. using ncnn::DataReader;
  29. using ncnn::Extractor;
  30. using ncnn::Layer;
  31. using ncnn::Mat;
  32. using ncnn::ModelBin;
  33. using ncnn::Net;
  34. using ncnn::Option;
  35. using ncnn::ParamDict;
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. const char* ncnn_version()
  40. {
  41. return NCNN_VERSION_STRING;
  42. }
  43. /* allocator api */
  44. class PoolAllocator_c_api : public ncnn::PoolAllocator
  45. {
  46. public:
  47. PoolAllocator_c_api(ncnn_allocator_t _allocator)
  48. : ncnn::PoolAllocator()
  49. {
  50. allocator = _allocator;
  51. }
  52. virtual void* fastMalloc(size_t size)
  53. {
  54. return allocator->fast_malloc(allocator, size);
  55. }
  56. virtual void fastFree(void* ptr)
  57. {
  58. return allocator->fast_free(allocator, ptr);
  59. }
  60. public:
  61. ncnn_allocator_t allocator;
  62. };
  63. static void* __ncnn_PoolAllocator_fast_malloc(ncnn_allocator_t allocator, size_t size)
  64. {
  65. return ((ncnn::PoolAllocator*)allocator->pthis)->ncnn::PoolAllocator::fastMalloc(size);
  66. }
  67. static void __ncnn_PoolAllocator_fast_free(ncnn_allocator_t allocator, void* ptr)
  68. {
  69. ((ncnn::PoolAllocator*)allocator->pthis)->ncnn::PoolAllocator::fastFree(ptr);
  70. }
  71. class UnlockedPoolAllocator_c_api : public ncnn::UnlockedPoolAllocator
  72. {
  73. public:
  74. UnlockedPoolAllocator_c_api(ncnn_allocator_t _allocator)
  75. : ncnn::UnlockedPoolAllocator()
  76. {
  77. allocator = _allocator;
  78. }
  79. virtual void* fastMalloc(size_t size)
  80. {
  81. return allocator->fast_malloc(allocator, size);
  82. }
  83. virtual void fastFree(void* ptr)
  84. {
  85. return allocator->fast_free(allocator, ptr);
  86. }
  87. public:
  88. ncnn_allocator_t allocator;
  89. };
  90. static void* __ncnn_UnlockedPoolAllocator_fast_malloc(ncnn_allocator_t allocator, size_t size)
  91. {
  92. return ((ncnn::UnlockedPoolAllocator*)allocator->pthis)->ncnn::UnlockedPoolAllocator::fastMalloc(size);
  93. }
  94. static void __ncnn_UnlockedPoolAllocator_fast_free(ncnn_allocator_t allocator, void* ptr)
  95. {
  96. ((ncnn::UnlockedPoolAllocator*)allocator->pthis)->ncnn::UnlockedPoolAllocator::fastFree(ptr);
  97. }
  98. ncnn_allocator_t ncnn_allocator_create_pool_allocator()
  99. {
  100. ncnn_allocator_t allocator = (ncnn_allocator_t)malloc(sizeof(struct __ncnn_allocator_t));
  101. allocator->pthis = (void*)(new PoolAllocator_c_api(allocator));
  102. allocator->fast_malloc = __ncnn_PoolAllocator_fast_malloc;
  103. allocator->fast_free = __ncnn_PoolAllocator_fast_free;
  104. return allocator;
  105. }
  106. ncnn_allocator_t ncnn_allocator_create_unlocked_pool_allocator()
  107. {
  108. ncnn_allocator_t allocator = (ncnn_allocator_t)malloc(sizeof(struct __ncnn_allocator_t));
  109. allocator->pthis = (void*)(new UnlockedPoolAllocator_c_api(allocator));
  110. allocator->fast_malloc = __ncnn_UnlockedPoolAllocator_fast_malloc;
  111. allocator->fast_free = __ncnn_UnlockedPoolAllocator_fast_free;
  112. return allocator;
  113. }
  114. void ncnn_allocator_destroy(ncnn_allocator_t allocator)
  115. {
  116. delete (Allocator*)allocator->pthis;
  117. free(allocator);
  118. }
  119. /* option api */
  120. ncnn_option_t ncnn_option_create()
  121. {
  122. return (ncnn_option_t)(new Option());
  123. }
  124. void ncnn_option_destroy(ncnn_option_t opt)
  125. {
  126. delete (Option*)opt;
  127. }
  128. int ncnn_option_get_num_threads(const ncnn_option_t opt)
  129. {
  130. return ((const Option*)opt)->num_threads;
  131. }
  132. void ncnn_option_set_num_threads(ncnn_option_t opt, int num_threads)
  133. {
  134. ((Option*)opt)->num_threads = num_threads;
  135. }
  136. int ncnn_option_get_use_vulkan_compute(const ncnn_option_t opt)
  137. {
  138. #if NCNN_VULKAN
  139. return ((const Option*)opt)->use_vulkan_compute;
  140. #else
  141. (void)opt;
  142. return 0;
  143. #endif
  144. }
  145. void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_compute)
  146. {
  147. #if NCNN_VULKAN
  148. ((Option*)opt)->use_vulkan_compute = use_vulkan_compute;
  149. #else
  150. (void)opt;
  151. (void)use_vulkan_compute;
  152. #endif
  153. }
  154. /* mat api */
  155. ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator)
  156. {
  157. return (ncnn_mat_t)(new Mat(w, 4u, (Allocator*)allocator));
  158. }
  159. ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator)
  160. {
  161. return (ncnn_mat_t)(new Mat(w, h, 4u, (Allocator*)allocator));
  162. }
  163. ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator)
  164. {
  165. return (ncnn_mat_t)(new Mat(w, h, c, 4u, (Allocator*)allocator));
  166. }
  167. ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator)
  168. {
  169. return (ncnn_mat_t)(new Mat(w, data, 4u, (Allocator*)allocator));
  170. }
  171. ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data, ncnn_allocator_t allocator)
  172. {
  173. return (ncnn_mat_t)(new Mat(w, h, data, 4u, (Allocator*)allocator));
  174. }
  175. ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data, ncnn_allocator_t allocator)
  176. {
  177. return (ncnn_mat_t)(new Mat(w, h, c, data, 4u, (Allocator*)allocator));
  178. }
  179. ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack, ncnn_allocator_t allocator)
  180. {
  181. return (ncnn_mat_t)(new Mat(w, elemsize, elempack, (Allocator*)allocator));
  182. }
  183. ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack, ncnn_allocator_t allocator)
  184. {
  185. return (ncnn_mat_t)(new Mat(w, h, elemsize, elempack, (Allocator*)allocator));
  186. }
  187. ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator)
  188. {
  189. return (ncnn_mat_t)(new Mat(w, h, c, elemsize, elempack, (Allocator*)allocator));
  190. }
  191. ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
  192. {
  193. return (ncnn_mat_t)(new Mat(w, data, elemsize, elempack, (Allocator*)allocator));
  194. }
  195. ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
  196. {
  197. return (ncnn_mat_t)(new Mat(w, h, data, elemsize, elempack, (Allocator*)allocator));
  198. }
  199. ncnn_mat_t ncnn_mat_create_external_3d_elem(int w, int h, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
  200. {
  201. return (ncnn_mat_t)(new Mat(w, h, c, data, elemsize, elempack, (Allocator*)allocator));
  202. }
  203. void ncnn_mat_destroy(ncnn_mat_t mat)
  204. {
  205. delete (Mat*)mat;
  206. }
  207. void ncnn_mat_fill_float(ncnn_mat_t mat, float v)
  208. {
  209. ((Mat*)mat)->fill(v);
  210. }
  211. ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat, ncnn_allocator_t allocator)
  212. {
  213. return (ncnn_mat_t)(new Mat(((const Mat*)mat)->clone((Allocator*)allocator)));
  214. }
  215. ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w, ncnn_allocator_t allocator)
  216. {
  217. return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, (Allocator*)allocator)));
  218. }
  219. ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h, ncnn_allocator_t allocator)
  220. {
  221. return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, (Allocator*)allocator)));
  222. }
  223. ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c, ncnn_allocator_t allocator)
  224. {
  225. return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, c, (Allocator*)allocator)));
  226. }
  227. int ncnn_mat_get_dims(const ncnn_mat_t mat)
  228. {
  229. return ((const Mat*)mat)->dims;
  230. }
  231. int ncnn_mat_get_w(const ncnn_mat_t mat)
  232. {
  233. return ((const Mat*)mat)->w;
  234. }
  235. int ncnn_mat_get_h(const ncnn_mat_t mat)
  236. {
  237. return ((const Mat*)mat)->h;
  238. }
  239. int ncnn_mat_get_c(const ncnn_mat_t mat)
  240. {
  241. return ((const Mat*)mat)->c;
  242. }
  243. size_t ncnn_mat_get_elemsize(const ncnn_mat_t mat)
  244. {
  245. return ((const Mat*)mat)->elemsize;
  246. }
  247. int ncnn_mat_get_elempack(const ncnn_mat_t mat)
  248. {
  249. return ((const Mat*)mat)->elempack;
  250. }
  251. size_t ncnn_mat_get_cstep(const ncnn_mat_t mat)
  252. {
  253. return ((const Mat*)mat)->cstep;
  254. }
  255. void* ncnn_mat_get_data(const ncnn_mat_t mat)
  256. {
  257. return ((const Mat*)mat)->data;
  258. }
  259. void* ncnn_mat_get_channel_data(const ncnn_mat_t mat, int c)
  260. {
  261. return ((const Mat*)mat)->channel(c).data;
  262. }
  263. #if NCNN_PIXEL
  264. /* mat pixel api */
  265. ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, ncnn_allocator_t allocator)
  266. {
  267. return (ncnn_mat_t)(new Mat(Mat::from_pixels(pixels, type, w, h, stride, (Allocator*)allocator)));
  268. }
  269. ncnn_mat_t ncnn_mat_from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int stride, int target_width, int target_height, ncnn_allocator_t allocator)
  270. {
  271. return (ncnn_mat_t)(new Mat(Mat::from_pixels_resize(pixels, type, w, h, stride, target_width, target_height, (Allocator*)allocator)));
  272. }
  273. ncnn_mat_t ncnn_mat_from_pixels_roi(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, ncnn_allocator_t allocator)
  274. {
  275. return (ncnn_mat_t)(new Mat(Mat::from_pixels_roi(pixels, type, w, h, stride, roix, roiy, roiw, roih, (Allocator*)allocator)));
  276. }
  277. ncnn_mat_t ncnn_mat_from_pixels_roi_resize(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, int target_width, int target_height, ncnn_allocator_t allocator)
  278. {
  279. return (ncnn_mat_t)(new Mat(Mat::from_pixels_roi_resize(pixels, type, w, h, stride, roix, roiy, roiw, roih, target_width, target_height, (Allocator*)allocator)));
  280. }
  281. void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride)
  282. {
  283. ((const Mat*)mat)->to_pixels(pixels, type, stride);
  284. }
  285. void ncnn_mat_to_pixels_resize(const ncnn_mat_t mat, unsigned char* pixels, int type, int target_width, int target_height, int target_stride)
  286. {
  287. ((const Mat*)mat)->to_pixels_resize(pixels, type, target_width, target_height, target_stride);
  288. }
  289. #endif /* NCNN_PIXEL */
  290. void ncnn_mat_substract_mean_normalize(ncnn_mat_t mat, const float* mean_vals, const float* norm_vals)
  291. {
  292. ((Mat*)mat)->substract_mean_normalize(mean_vals, norm_vals);
  293. }
  294. void ncnn_convert_packing(const ncnn_mat_t src, ncnn_mat_t* dst, int elempack, const ncnn_option_t opt)
  295. {
  296. Mat _dst;
  297. ncnn::convert_packing(*(const Mat*)src, _dst, elempack, *(Option*)opt);
  298. *dst = (ncnn_mat_t)(new Mat(_dst));
  299. }
  300. void ncnn_flatten(const ncnn_mat_t src, ncnn_mat_t* dst, const ncnn_option_t opt)
  301. {
  302. Mat _dst;
  303. ncnn::flatten(*(const Mat*)src, _dst, *(Option*)opt);
  304. *dst = (ncnn_mat_t)(new Mat(_dst));
  305. }
  306. /* blob api */
  307. #if NCNN_STRING
  308. const char* ncnn_blob_get_name(const ncnn_blob_t blob)
  309. {
  310. return ((const Blob*)blob)->name.c_str();
  311. }
  312. #endif /* NCNN_STRING */
  313. int ncnn_blob_get_producer(const ncnn_blob_t blob)
  314. {
  315. return ((const Blob*)blob)->producer;
  316. }
  317. int ncnn_blob_get_consumer(const ncnn_blob_t blob)
  318. {
  319. return ((const Blob*)blob)->consumer;
  320. }
  321. void ncnn_blob_get_shape(const ncnn_blob_t blob, int* dims, int* w, int* h, int* c)
  322. {
  323. const Mat& shape = ((const Blob*)blob)->shape;
  324. *dims = shape.dims;
  325. *w = shape.w;
  326. *h = shape.h;
  327. *c = shape.c;
  328. }
  329. /* paramdict api */
  330. ncnn_paramdict_t ncnn_paramdict_create()
  331. {
  332. return (ncnn_paramdict_t)(new ParamDict());
  333. }
  334. void ncnn_paramdict_destroy(ncnn_paramdict_t pd)
  335. {
  336. delete (ParamDict*)pd;
  337. }
  338. int ncnn_paramdict_get_type(const ncnn_paramdict_t pd, int id)
  339. {
  340. return ((const ParamDict*)pd)->type(id);
  341. }
  342. int ncnn_paramdict_get_int(const ncnn_paramdict_t pd, int id, int def)
  343. {
  344. return ((const ParamDict*)pd)->get(id, def);
  345. }
  346. float ncnn_paramdict_get_float(const ncnn_paramdict_t pd, int id, float def)
  347. {
  348. return ((const ParamDict*)pd)->get(id, def);
  349. }
  350. ncnn_mat_t ncnn_paramdict_get_array(ncnn_paramdict_t pd, int id, const ncnn_mat_t def)
  351. {
  352. return (ncnn_mat_t)(new Mat(((const ParamDict*)pd)->get(id, *(const Mat*)def)));
  353. }
  354. void ncnn_paramdict_set_int(ncnn_paramdict_t pd, int id, int i)
  355. {
  356. return ((ParamDict*)pd)->set(id, i);
  357. }
  358. void ncnn_paramdict_set_float(ncnn_paramdict_t pd, int id, float f)
  359. {
  360. return ((ParamDict*)pd)->set(id, f);
  361. }
  362. void ncnn_paramdict_set_array(ncnn_paramdict_t pd, int id, ncnn_mat_t v)
  363. {
  364. return ((ParamDict*)pd)->set(id, *(const Mat*)v);
  365. }
  366. /* datareader api */
  367. class DataReader_c_api : public ncnn::DataReader
  368. {
  369. public:
  370. DataReader_c_api(ncnn_datareader_t _dr)
  371. : ncnn::DataReader()
  372. {
  373. dr = _dr;
  374. }
  375. #if NCNN_STRING
  376. virtual int scan(const char* format, void* p) const
  377. {
  378. return dr->scan(dr, format, p);
  379. }
  380. #endif /* NCNN_STRING */
  381. virtual size_t read(void* buf, size_t size) const
  382. {
  383. return dr->read(dr, buf, size);
  384. }
  385. public:
  386. ncnn_datareader_t dr;
  387. };
  388. #if NCNN_STRING
  389. static int __ncnn_DataReader_scan(ncnn_datareader_t dr, const char* format, void* p)
  390. {
  391. return ((ncnn::DataReader*)dr->pthis)->ncnn::DataReader::scan(format, p);
  392. }
  393. #endif /* NCNN_STRING */
  394. static size_t __ncnn_DataReader_read(ncnn_datareader_t dr, void* buf, size_t size)
  395. {
  396. return ((ncnn::DataReader*)dr->pthis)->ncnn::DataReader::read(buf, size);
  397. }
  398. #if NCNN_STDIO
  399. class DataReaderFromStdio_c_api : public ncnn::DataReaderFromStdio
  400. {
  401. public:
  402. DataReaderFromStdio_c_api(FILE* fp, ncnn_datareader_t _dr)
  403. : ncnn::DataReaderFromStdio(fp)
  404. {
  405. dr = _dr;
  406. }
  407. #if NCNN_STRING
  408. virtual int scan(const char* format, void* p) const
  409. {
  410. return dr->scan(dr, format, p);
  411. }
  412. #endif /* NCNN_STRING */
  413. virtual size_t read(void* buf, size_t size) const
  414. {
  415. return dr->read(dr, buf, size);
  416. }
  417. public:
  418. ncnn_datareader_t dr;
  419. };
  420. #if NCNN_STRING
  421. static int __ncnn_DataReaderFromStdio_scan(ncnn_datareader_t dr, const char* format, void* p)
  422. {
  423. return ((ncnn::DataReaderFromStdio*)dr->pthis)->ncnn::DataReaderFromStdio::scan(format, p);
  424. }
  425. #endif /* NCNN_STRING */
  426. static size_t __ncnn_DataReaderFromStdio_read(ncnn_datareader_t dr, void* buf, size_t size)
  427. {
  428. return ((ncnn::DataReaderFromStdio*)dr->pthis)->ncnn::DataReaderFromStdio::read(buf, size);
  429. }
  430. #endif /* NCNN_STDIO */
  431. class DataReaderFromMemory_c_api : public ncnn::DataReaderFromMemory
  432. {
  433. public:
  434. DataReaderFromMemory_c_api(const unsigned char*& mem, ncnn_datareader_t _dr)
  435. : ncnn::DataReaderFromMemory(mem)
  436. {
  437. dr = _dr;
  438. }
  439. #if NCNN_STRING
  440. virtual int scan(const char* format, void* p) const
  441. {
  442. return dr->scan(dr, format, p);
  443. }
  444. #endif /* NCNN_STRING */
  445. virtual size_t read(void* buf, size_t size) const
  446. {
  447. return dr->read(dr, buf, size);
  448. }
  449. public:
  450. ncnn_datareader_t dr;
  451. };
  452. #if NCNN_STRING
  453. static int __ncnn_DataReaderFromMemory_scan(ncnn_datareader_t dr, const char* format, void* p)
  454. {
  455. return ((ncnn::DataReaderFromMemory*)dr->pthis)->ncnn::DataReaderFromMemory::scan(format, p);
  456. }
  457. #endif /* NCNN_STRING */
  458. static size_t __ncnn_DataReaderFromMemory_read(ncnn_datareader_t dr, void* buf, size_t size)
  459. {
  460. return ((ncnn::DataReaderFromMemory*)dr->pthis)->ncnn::DataReaderFromMemory::read(buf, size);
  461. }
  462. ncnn_datareader_t ncnn_datareader_create()
  463. {
  464. ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
  465. dr->pthis = (void*)(new DataReader_c_api(dr));
  466. #if NCNN_STRING
  467. dr->scan = __ncnn_DataReader_scan;
  468. #endif /* NCNN_STRING */
  469. dr->read = __ncnn_DataReader_read;
  470. return dr;
  471. }
  472. #if NCNN_STDIO
  473. ncnn_datareader_t ncnn_datareader_create_from_stdio(FILE* fp)
  474. {
  475. ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
  476. dr->pthis = (void*)(new DataReaderFromStdio_c_api(fp, dr));
  477. #if NCNN_STRING
  478. dr->scan = __ncnn_DataReaderFromStdio_scan;
  479. #endif /* NCNN_STRING */
  480. dr->read = __ncnn_DataReaderFromStdio_read;
  481. return dr;
  482. }
  483. #endif /* NCNN_STDIO */
  484. ncnn_datareader_t ncnn_datareader_create_from_memory(const unsigned char** mem)
  485. {
  486. ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
  487. dr->pthis = (void*)(new DataReaderFromMemory_c_api(*mem, dr));
  488. #if NCNN_STRING
  489. dr->scan = __ncnn_DataReaderFromMemory_scan;
  490. #endif /* NCNN_STRING */
  491. dr->read = __ncnn_DataReaderFromMemory_read;
  492. return dr;
  493. }
  494. void ncnn_datareader_destroy(ncnn_datareader_t dr)
  495. {
  496. delete (DataReader*)dr->pthis;
  497. free(dr);
  498. }
  499. /* modelbin api */
  500. class ModelBinFromDataReader_c_api : public ncnn::ModelBinFromDataReader
  501. {
  502. public:
  503. ModelBinFromDataReader_c_api(ncnn_modelbin_t _mb, const DataReader& dr)
  504. : ncnn::ModelBinFromDataReader(dr)
  505. {
  506. mb = _mb;
  507. }
  508. virtual Mat load(int w, int type) const
  509. {
  510. ncnn_mat_t m = mb->load_1d(mb, w, type);
  511. Mat m2 = *(Mat*)m;
  512. ncnn_mat_destroy(m);
  513. return m2;
  514. }
  515. virtual Mat load(int w, int h, int type) const
  516. {
  517. ncnn_mat_t m = mb->load_2d(mb, w, h, type);
  518. Mat m2 = *(Mat*)m;
  519. ncnn_mat_destroy(m);
  520. return m2;
  521. }
  522. virtual Mat load(int w, int h, int c, int type) const
  523. {
  524. ncnn_mat_t m = mb->load_3d(mb, w, h, c, type);
  525. Mat m2 = *(Mat*)m;
  526. ncnn_mat_destroy(m);
  527. return m2;
  528. }
  529. public:
  530. ncnn_modelbin_t mb;
  531. };
  532. static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_1d(const ncnn_modelbin_t mb, int w, int type)
  533. {
  534. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBinFromDataReader::load(w, type)));
  535. }
  536. static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
  537. {
  538. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBin::load(w, h, type)));
  539. }
  540. static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
  541. {
  542. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBin::load(w, h, c, type)));
  543. }
  544. class ModelBinFromMatArray_c_api : public ncnn::ModelBinFromMatArray
  545. {
  546. public:
  547. ModelBinFromMatArray_c_api(ncnn_modelbin_t _mb, const Mat* weights)
  548. : ncnn::ModelBinFromMatArray(weights)
  549. {
  550. mb = _mb;
  551. }
  552. virtual Mat load(int w, int type) const
  553. {
  554. ncnn_mat_t m = mb->load_1d(mb, w, type);
  555. Mat m2 = *(Mat*)m;
  556. ncnn_mat_destroy(m);
  557. return m2;
  558. }
  559. virtual Mat load(int w, int h, int type) const
  560. {
  561. ncnn_mat_t m = mb->load_2d(mb, w, h, type);
  562. Mat m2 = *(Mat*)m;
  563. ncnn_mat_destroy(m);
  564. return m2;
  565. }
  566. virtual Mat load(int w, int h, int c, int type) const
  567. {
  568. ncnn_mat_t m = mb->load_3d(mb, w, h, c, type);
  569. Mat m2 = *(Mat*)m;
  570. ncnn_mat_destroy(m);
  571. return m2;
  572. }
  573. public:
  574. ncnn_modelbin_t mb;
  575. };
  576. static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_1d(const ncnn_modelbin_t mb, int w, int type)
  577. {
  578. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBinFromMatArray::load(w, type)));
  579. }
  580. static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
  581. {
  582. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBin::load(w, h, type)));
  583. }
  584. static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
  585. {
  586. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBin::load(w, h, c, type)));
  587. }
  588. ncnn_modelbin_t ncnn_modelbin_create_from_datareader(const ncnn_datareader_t dr)
  589. {
  590. ncnn_modelbin_t mb = (ncnn_modelbin_t)malloc(sizeof(struct __ncnn_modelbin_t));
  591. mb->pthis = (void*)(new ModelBinFromDataReader_c_api(mb, *(const DataReader*)dr->pthis));
  592. mb->load_1d = __ncnn_ModelBinFromDataReader_load_1d;
  593. mb->load_2d = __ncnn_ModelBinFromDataReader_load_2d;
  594. mb->load_3d = __ncnn_ModelBinFromDataReader_load_3d;
  595. return mb;
  596. }
  597. ncnn_modelbin_t ncnn_modelbin_create_from_mat_array(const ncnn_mat_t* weights, int n)
  598. {
  599. std::vector<Mat> matarray(n);
  600. for (int i = 0; i < n; i++)
  601. {
  602. matarray[i] = *(const Mat*)weights[i];
  603. }
  604. ncnn_modelbin_t mb = (ncnn_modelbin_t)malloc(sizeof(struct __ncnn_modelbin_t));
  605. mb->pthis = (void*)(new ModelBinFromMatArray_c_api(mb, &matarray[0]));
  606. mb->load_1d = __ncnn_ModelBinFromMatArray_load_1d;
  607. mb->load_2d = __ncnn_ModelBinFromMatArray_load_2d;
  608. mb->load_3d = __ncnn_ModelBinFromMatArray_load_3d;
  609. return mb;
  610. }
  611. void ncnn_modelbin_destroy(ncnn_modelbin_t mb)
  612. {
  613. delete (ModelBin*)mb->pthis;
  614. free(mb);
  615. }
  616. static ncnn_mat_t __ncnn_modelbin_load_1d(const ncnn_modelbin_t mb, int w, int type)
  617. {
  618. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, type)));
  619. }
  620. static ncnn_mat_t __ncnn_modelbin_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
  621. {
  622. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, h, type)));
  623. }
  624. static ncnn_mat_t __ncnn_modelbin_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
  625. {
  626. return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, h, c, type)));
  627. }
  628. /* layer api */
  629. class Layer_c_api : public Layer
  630. {
  631. public:
  632. Layer_c_api(ncnn_layer_t _layer)
  633. : Layer()
  634. {
  635. layer = _layer;
  636. }
  637. virtual int load_param(const ParamDict& pd)
  638. {
  639. return layer->load_param(layer, (ncnn_paramdict_t)&pd);
  640. }
  641. virtual int load_model(const ModelBin& mb)
  642. {
  643. struct __ncnn_modelbin_t mb0;
  644. mb0.pthis = (void*)&mb;
  645. mb0.load_1d = __ncnn_modelbin_load_1d;
  646. mb0.load_2d = __ncnn_modelbin_load_2d;
  647. mb0.load_3d = __ncnn_modelbin_load_3d;
  648. return layer->load_model(layer, &mb0);
  649. }
  650. virtual int create_pipeline(const Option& opt)
  651. {
  652. return layer->create_pipeline(layer, (ncnn_option_t)&opt);
  653. }
  654. virtual int destroy_pipeline(const Option& opt)
  655. {
  656. return layer->destroy_pipeline(layer, (ncnn_option_t)&opt);
  657. }
  658. virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  659. {
  660. const int n = bottom_blobs.size();
  661. const int n2 = top_blobs.size();
  662. std::vector<ncnn_mat_t> bottom_blobs0(n);
  663. for (int i = 0; i < n; i++)
  664. {
  665. bottom_blobs0[i] = (ncnn_mat_t)&bottom_blobs[i];
  666. }
  667. std::vector<ncnn_mat_t*> top_blobs0(n2, (ncnn_mat_t*)0);
  668. int ret = layer->forward_n(layer, bottom_blobs0.data(), n, top_blobs0.data(), n2, (ncnn_option_t)&opt);
  669. for (int i = 0; i < n2; i++)
  670. {
  671. top_blobs[i] = *(Mat*)*top_blobs0[i];
  672. ncnn_mat_destroy(*top_blobs0[i]);
  673. }
  674. return ret;
  675. }
  676. virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  677. {
  678. ncnn_mat_t top_blob0 = 0;
  679. int ret = layer->forward_1(layer, (ncnn_mat_t)&bottom_blob, &top_blob0, (ncnn_option_t)&opt);
  680. top_blob = *(Mat*)top_blob0;
  681. ncnn_mat_destroy(top_blob0);
  682. return ret;
  683. }
  684. virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const
  685. {
  686. const int n = bottom_top_blobs.size();
  687. std::vector<ncnn_mat_t> bottom_top_blobs0(n);
  688. for (int i = 0; i < n; i++)
  689. {
  690. bottom_top_blobs0[i] = (ncnn_mat_t)&bottom_top_blobs[i];
  691. }
  692. return layer->forward_inplace_n(layer, bottom_top_blobs0.data(), n, (ncnn_option_t)&opt);
  693. }
  694. virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const
  695. {
  696. return layer->forward_inplace_1(layer, (ncnn_mat_t)&bottom_top_blob, (ncnn_option_t)&opt);
  697. }
  698. public:
  699. ncnn_layer_t layer;
  700. };
  701. static int __ncnn_Layer_load_param(ncnn_layer_t layer, const ncnn_paramdict_t pd)
  702. {
  703. return ((Layer*)layer->pthis)->Layer::load_param(*(const ParamDict*)pd);
  704. }
  705. static int __ncnn_Layer_load_model(ncnn_layer_t layer, const ncnn_modelbin_t mb)
  706. {
  707. return ((Layer*)layer->pthis)->Layer::load_model(*(const ModelBin*)mb);
  708. }
  709. static int __ncnn_Layer_create_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
  710. {
  711. return ((Layer*)layer->pthis)->Layer::create_pipeline(*(const Option*)opt);
  712. }
  713. static int __ncnn_Layer_destroy_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
  714. {
  715. return ((Layer*)layer->pthis)->Layer::destroy_pipeline(*(const Option*)opt);
  716. }
  717. static int __ncnn_Layer_forward_1(const ncnn_layer_t layer, const ncnn_mat_t bottom_blob, ncnn_mat_t* top_blob, const ncnn_option_t opt)
  718. {
  719. Mat _top_blob;
  720. int ret = ((const Layer*)layer->pthis)->Layer::forward(*(const Mat*)bottom_blob, _top_blob, *(const Option*)opt);
  721. *top_blob = (ncnn_mat_t)(new Mat(_top_blob));
  722. return ret;
  723. }
  724. static int __ncnn_Layer_forward_n(const ncnn_layer_t layer, const ncnn_mat_t* bottom_blobs, int n, ncnn_mat_t** top_blobs, int n2, const ncnn_option_t opt)
  725. {
  726. std::vector<Mat> _bottom_blobs(n);
  727. std::vector<Mat> _top_blobs(n2);
  728. for (int i = 0; i < n; i++)
  729. {
  730. _bottom_blobs[i] = *(Mat*)bottom_blobs[i];
  731. }
  732. int ret = ((const Layer*)layer->pthis)->Layer::forward(_bottom_blobs, _top_blobs, *(const Option*)opt);
  733. for (int i = 0; i < n2; i++)
  734. {
  735. *top_blobs[i] = (ncnn_mat_t)(new Mat(_top_blobs[i]));
  736. }
  737. return ret;
  738. }
  739. static int __ncnn_Layer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
  740. {
  741. return ((const Layer*)layer->pthis)->Layer::forward_inplace(*(Mat*)bottom_top_blob, *(const Option*)opt);
  742. }
  743. static int __ncnn_Layer_forward_inplace_n(const ncnn_layer_t layer, ncnn_mat_t* bottom_top_blobs, int n, const ncnn_option_t opt)
  744. {
  745. std::vector<Mat> _bottom_top_blobs(n);
  746. for (int i = 0; i < n; i++)
  747. {
  748. _bottom_top_blobs[i] = *(Mat*)bottom_top_blobs[i];
  749. }
  750. return ((const Layer*)layer->pthis)->Layer::forward_inplace(_bottom_top_blobs, *(const Option*)opt);
  751. }
  752. static int __ncnn_layer_load_param(ncnn_layer_t layer, const ncnn_paramdict_t pd)
  753. {
  754. return ((Layer*)layer->pthis)->load_param(*(const ParamDict*)pd);
  755. }
  756. static int __ncnn_layer_load_model(ncnn_layer_t layer, const ncnn_modelbin_t mb)
  757. {
  758. return ((Layer*)layer->pthis)->load_model(*(const ModelBin*)mb);
  759. }
  760. static int __ncnn_layer_create_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
  761. {
  762. return ((Layer*)layer->pthis)->create_pipeline(*(const Option*)opt);
  763. }
  764. static int __ncnn_layer_destroy_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
  765. {
  766. return ((Layer*)layer->pthis)->destroy_pipeline(*(const Option*)opt);
  767. }
  768. static int __ncnn_layer_forward_1(const ncnn_layer_t layer, const ncnn_mat_t bottom_blob, ncnn_mat_t* top_blob, const ncnn_option_t opt)
  769. {
  770. Mat _top_blob;
  771. int ret = ((const Layer*)layer->pthis)->forward(*(const Mat*)bottom_blob, _top_blob, *(const Option*)opt);
  772. *top_blob = (ncnn_mat_t)(new Mat(_top_blob));
  773. return ret;
  774. }
  775. static int __ncnn_layer_forward_n(const ncnn_layer_t layer, const ncnn_mat_t* bottom_blobs, int n, ncnn_mat_t** top_blobs, int n2, const ncnn_option_t opt)
  776. {
  777. std::vector<Mat> _bottom_blobs(n);
  778. std::vector<Mat> _top_blobs(n2);
  779. for (int i = 0; i < n; i++)
  780. {
  781. _bottom_blobs[i] = *(Mat*)bottom_blobs[i];
  782. }
  783. int ret = ((const Layer*)layer->pthis)->forward(_bottom_blobs, _top_blobs, *(const Option*)opt);
  784. for (int i = 0; i < n2; i++)
  785. {
  786. *top_blobs[i] = (ncnn_mat_t)(new Mat(_top_blobs[i]));
  787. }
  788. return ret;
  789. }
  790. static int __ncnn_layer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
  791. {
  792. return ((const Layer*)layer->pthis)->forward_inplace(*(Mat*)bottom_top_blob, *(const Option*)opt);
  793. }
  794. static int __ncnn_layer_forward_inplace_n(const ncnn_layer_t layer, ncnn_mat_t* bottom_top_blobs, int n, const ncnn_option_t opt)
  795. {
  796. std::vector<Mat> _bottom_top_blobs(n);
  797. for (int i = 0; i < n; i++)
  798. {
  799. _bottom_top_blobs[i] = *(Mat*)bottom_top_blobs[i];
  800. }
  801. return ((const Layer*)layer->pthis)->forward_inplace(_bottom_top_blobs, *(const Option*)opt);
  802. }
  803. ncnn_layer_t ncnn_layer_create()
  804. {
  805. ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
  806. layer->pthis = (void*)(new Layer_c_api(layer));
  807. layer->load_param = __ncnn_Layer_load_param;
  808. layer->load_model = __ncnn_Layer_load_model;
  809. layer->create_pipeline = __ncnn_Layer_create_pipeline;
  810. layer->destroy_pipeline = __ncnn_Layer_destroy_pipeline;
  811. layer->forward_1 = __ncnn_Layer_forward_1;
  812. layer->forward_n = __ncnn_Layer_forward_n;
  813. layer->forward_inplace_1 = __ncnn_Layer_forward_inplace_1;
  814. layer->forward_inplace_n = __ncnn_Layer_forward_inplace_n;
  815. return layer;
  816. }
  817. ncnn_layer_t ncnn_layer_create_by_typeindex(int typeindex)
  818. {
  819. ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
  820. layer->pthis = (void*)(ncnn::create_layer(typeindex));
  821. layer->load_param = __ncnn_layer_load_param;
  822. layer->load_model = __ncnn_layer_load_model;
  823. layer->create_pipeline = __ncnn_layer_create_pipeline;
  824. layer->destroy_pipeline = __ncnn_layer_destroy_pipeline;
  825. layer->forward_1 = __ncnn_layer_forward_1;
  826. layer->forward_n = __ncnn_layer_forward_n;
  827. layer->forward_inplace_1 = __ncnn_layer_forward_inplace_1;
  828. layer->forward_inplace_n = __ncnn_layer_forward_inplace_n;
  829. return layer;
  830. }
  831. #if NCNN_STRING
  832. ncnn_layer_t ncnn_layer_create_by_type(const char* type)
  833. {
  834. ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
  835. layer->pthis = (void*)(ncnn::create_layer(type));
  836. layer->load_param = __ncnn_layer_load_param;
  837. layer->load_model = __ncnn_layer_load_model;
  838. layer->create_pipeline = __ncnn_layer_create_pipeline;
  839. layer->destroy_pipeline = __ncnn_layer_destroy_pipeline;
  840. layer->forward_1 = __ncnn_layer_forward_1;
  841. layer->forward_n = __ncnn_layer_forward_n;
  842. layer->forward_inplace_1 = __ncnn_layer_forward_inplace_1;
  843. layer->forward_inplace_n = __ncnn_layer_forward_inplace_n;
  844. return layer;
  845. }
  846. #endif /* NCNN_STRING */
  847. void ncnn_layer_destroy(ncnn_layer_t layer)
  848. {
  849. delete (Layer*)layer->pthis;
  850. free(layer);
  851. }
  852. #if NCNN_STRING
  853. const char* ncnn_layer_get_name(const ncnn_layer_t layer)
  854. {
  855. return ((const Layer*)layer->pthis)->name.c_str();
  856. }
  857. #endif /* NCNN_STRING */
  858. int ncnn_layer_get_typeindex(const ncnn_layer_t layer)
  859. {
  860. return ((const Layer*)layer->pthis)->typeindex;
  861. }
  862. #if NCNN_STRING
  863. const char* ncnn_layer_get_type(const ncnn_layer_t layer)
  864. {
  865. return ((const Layer*)layer->pthis)->type.c_str();
  866. }
  867. #endif /* NCNN_STRING */
  868. int ncnn_layer_get_one_blob_only(const ncnn_layer_t layer)
  869. {
  870. return ((const Layer*)layer->pthis)->one_blob_only;
  871. }
  872. int ncnn_layer_get_support_inplace(const ncnn_layer_t layer)
  873. {
  874. return ((const Layer*)layer->pthis)->support_inplace;
  875. }
  876. int ncnn_layer_get_support_vulkan(const ncnn_layer_t layer)
  877. {
  878. return ((const Layer*)layer->pthis)->support_vulkan;
  879. }
  880. int ncnn_layer_get_support_packing(const ncnn_layer_t layer)
  881. {
  882. return ((const Layer*)layer->pthis)->support_packing;
  883. }
  884. int ncnn_layer_get_support_bf16_storage(const ncnn_layer_t layer)
  885. {
  886. return ((const Layer*)layer->pthis)->support_bf16_storage;
  887. }
  888. int ncnn_layer_get_support_fp16_storage(const ncnn_layer_t layer)
  889. {
  890. return ((const Layer*)layer->pthis)->support_fp16_storage;
  891. }
  892. int ncnn_layer_get_support_image_storage(const ncnn_layer_t layer)
  893. {
  894. return ((const Layer*)layer->pthis)->support_image_storage;
  895. }
  896. void ncnn_layer_set_one_blob_only(ncnn_layer_t layer, int enable)
  897. {
  898. ((Layer*)layer->pthis)->one_blob_only = enable;
  899. }
  900. void ncnn_layer_set_support_inplace(ncnn_layer_t layer, int enable)
  901. {
  902. ((Layer*)layer->pthis)->support_inplace = enable;
  903. }
  904. void ncnn_layer_set_support_vulkan(ncnn_layer_t layer, int enable)
  905. {
  906. ((Layer*)layer->pthis)->support_vulkan = enable;
  907. }
  908. void ncnn_layer_set_support_packing(ncnn_layer_t layer, int enable)
  909. {
  910. ((Layer*)layer->pthis)->support_packing = enable;
  911. }
  912. void ncnn_layer_set_support_bf16_storage(ncnn_layer_t layer, int enable)
  913. {
  914. ((Layer*)layer->pthis)->support_bf16_storage = enable;
  915. }
  916. void ncnn_layer_set_support_fp16_storage(ncnn_layer_t layer, int enable)
  917. {
  918. ((Layer*)layer->pthis)->support_fp16_storage = enable;
  919. }
  920. void ncnn_layer_set_support_image_storage(ncnn_layer_t layer, int enable)
  921. {
  922. ((Layer*)layer->pthis)->support_image_storage = enable;
  923. }
  924. int ncnn_layer_get_bottom_count(const ncnn_layer_t layer)
  925. {
  926. return (int)((const Layer*)layer->pthis)->bottoms.size();
  927. }
  928. int ncnn_layer_get_bottom(const ncnn_layer_t layer, int i)
  929. {
  930. return ((const Layer*)layer->pthis)->bottoms[i];
  931. }
  932. int ncnn_layer_get_top_count(const ncnn_layer_t layer)
  933. {
  934. return (int)((const Layer*)layer->pthis)->tops.size();
  935. }
  936. int ncnn_layer_get_top(const ncnn_layer_t layer, int i)
  937. {
  938. return ((const Layer*)layer->pthis)->tops[i];
  939. }
  940. void ncnn_blob_get_bottom_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c)
  941. {
  942. const Mat& shape = ((const Layer*)layer->pthis)->bottom_shapes[i];
  943. *dims = shape.dims;
  944. *w = shape.w;
  945. *h = shape.h;
  946. *c = shape.c;
  947. }
  948. void ncnn_blob_get_top_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c)
  949. {
  950. const Mat& shape = ((const Layer*)layer->pthis)->top_shapes[i];
  951. *dims = shape.dims;
  952. *w = shape.w;
  953. *h = shape.h;
  954. *c = shape.c;
  955. }
  956. /* net api */
  957. ncnn_net_t ncnn_net_create()
  958. {
  959. ncnn_net_t net = (ncnn_net_t)malloc(sizeof(struct __ncnn_net_t));
  960. net->pthis = (void*)(new Net());
  961. net->custom_layer_factory = 0;
  962. return net;
  963. }
  964. void ncnn_net_destroy(ncnn_net_t net)
  965. {
  966. delete (Net*)net->pthis;
  967. ncnn_net_custom_layer_factory_t ud = net->custom_layer_factory;
  968. while (ud)
  969. {
  970. ncnn_net_custom_layer_factory_t ud_next = ud->next;
  971. free(ud);
  972. ud = ud_next;
  973. }
  974. free(net);
  975. }
  976. void ncnn_net_set_option(ncnn_net_t net, ncnn_option_t opt)
  977. {
  978. ((Net*)net->pthis)->opt = *((Option*)opt);
  979. }
  980. static ::ncnn::Layer* __Layer_c_api_layer_creator(void* userdata)
  981. {
  982. ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)userdata;
  983. ncnn_layer_t layer0 = ud->creator(ud->userdata);
  984. ::ncnn::Layer* layer = (::ncnn::Layer*)layer0->pthis;
  985. layer->userdata = (void*)layer0;
  986. layer->one_blob_only = ncnn_layer_get_one_blob_only(layer0);
  987. layer->support_inplace = ncnn_layer_get_support_inplace(layer0);
  988. layer->support_vulkan = ncnn_layer_get_support_vulkan(layer0);
  989. layer->support_packing = ncnn_layer_get_support_packing(layer0);
  990. layer->support_bf16_storage = ncnn_layer_get_support_bf16_storage(layer0);
  991. layer->support_fp16_storage = ncnn_layer_get_support_fp16_storage(layer0);
  992. layer->support_image_storage = ncnn_layer_get_support_image_storage(layer0);
  993. return layer;
  994. }
  995. static void __Layer_c_api_layer_destroyer(::ncnn::Layer* layer, void* userdata)
  996. {
  997. ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)userdata;
  998. ncnn_layer_t layer0 = (ncnn_layer_t)layer->userdata;
  999. ud->destroyer(layer0, ud->userdata);
  1000. }
  1001. #if NCNN_STRING
  1002. void ncnn_net_register_custom_layer_by_type(ncnn_net_t net, const char* type, ncnn_layer_creator_t creator, ncnn_layer_destroyer_t destroyer, void* userdata)
  1003. {
  1004. ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)malloc(sizeof(struct __ncnn_net_custom_layer_factory_t));
  1005. ud->creator = creator;
  1006. ud->destroyer = destroyer;
  1007. ud->userdata = userdata;
  1008. ud->next = net->custom_layer_factory;
  1009. net->custom_layer_factory = ud;
  1010. ((Net*)net->pthis)->register_custom_layer(type, __Layer_c_api_layer_creator, __Layer_c_api_layer_destroyer, (void*)ud);
  1011. }
  1012. #endif /* NCNN_STRING */
  1013. void ncnn_net_register_custom_layer_by_typeindex(ncnn_net_t net, int typeindex, ncnn_layer_creator_t creator, ncnn_layer_destroyer_t destroyer, void* userdata)
  1014. {
  1015. ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)malloc(sizeof(struct __ncnn_net_custom_layer_factory_t));
  1016. ud->creator = creator;
  1017. ud->destroyer = destroyer;
  1018. ud->userdata = userdata;
  1019. ud->next = net->custom_layer_factory;
  1020. net->custom_layer_factory = ud;
  1021. ((Net*)net->pthis)->register_custom_layer(typeindex, __Layer_c_api_layer_creator, __Layer_c_api_layer_destroyer, (void*)ud);
  1022. }
  1023. #if NCNN_STDIO
  1024. #if NCNN_STRING
  1025. int ncnn_net_load_param(ncnn_net_t net, const char* path)
  1026. {
  1027. return ((Net*)net->pthis)->load_param(path);
  1028. }
  1029. #endif /* NCNN_STRING */
  1030. int ncnn_net_load_param_bin(ncnn_net_t net, const char* path)
  1031. {
  1032. return ((Net*)net->pthis)->load_param_bin(path);
  1033. }
  1034. int ncnn_net_load_model(ncnn_net_t net, const char* path)
  1035. {
  1036. return ((Net*)net->pthis)->load_model(path);
  1037. }
  1038. #endif /* NCNN_STDIO */
  1039. #if NCNN_STRING
  1040. int ncnn_net_load_param_memory(ncnn_net_t net, const char* mem)
  1041. {
  1042. return ((Net*)net->pthis)->load_param_mem(mem);
  1043. }
  1044. #endif /* NCNN_STRING */
  1045. int ncnn_net_load_param_bin_memory(ncnn_net_t net, const unsigned char* mem)
  1046. {
  1047. return ((Net*)net->pthis)->load_param(mem);
  1048. }
  1049. int ncnn_net_load_model_memory(ncnn_net_t net, const unsigned char* mem)
  1050. {
  1051. return ((Net*)net->pthis)->load_model(mem);
  1052. }
  1053. #if NCNN_STRING
  1054. int ncnn_net_load_param_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
  1055. {
  1056. return ((Net*)net->pthis)->load_param(*(const DataReader*)dr->pthis);
  1057. }
  1058. #endif /* NCNN_STRING */
  1059. int ncnn_net_load_param_bin_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
  1060. {
  1061. return ((Net*)net->pthis)->load_param_bin(*(const DataReader*)dr->pthis);
  1062. }
  1063. int ncnn_net_load_model_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
  1064. {
  1065. return ((Net*)net->pthis)->load_model(*(const DataReader*)dr->pthis);
  1066. }
  1067. void ncnn_net_clear(ncnn_net_t net)
  1068. {
  1069. return ((Net*)net->pthis)->clear();
  1070. }
  1071. /* extractor api */
  1072. ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net)
  1073. {
  1074. return (ncnn_extractor_t)(new Extractor(((Net*)net->pthis)->create_extractor()));
  1075. }
  1076. void ncnn_extractor_destroy(ncnn_extractor_t ex)
  1077. {
  1078. delete (Extractor*)ex;
  1079. }
  1080. void ncnn_extractor_set_option(ncnn_extractor_t ex, const ncnn_option_t opt)
  1081. {
  1082. ((Extractor*)ex)->set_num_threads(((const Option*)opt)->num_threads);
  1083. #if NCNN_VULKAN
  1084. ((Extractor*)ex)->set_vulkan_compute(((const Option*)opt)->use_vulkan_compute);
  1085. #endif
  1086. }
  1087. #if NCNN_STRING
  1088. int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat)
  1089. {
  1090. return ((Extractor*)ex)->input(name, *((const Mat*)mat));
  1091. }
  1092. int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat)
  1093. {
  1094. Mat mat0;
  1095. int ret = ((Extractor*)ex)->extract(name, mat0);
  1096. *mat = (ncnn_mat_t)(new Mat(mat0));
  1097. return ret;
  1098. }
  1099. #endif /* NCNN_STRING */
  1100. int ncnn_extractor_input_index(ncnn_extractor_t ex, int index, const ncnn_mat_t mat)
  1101. {
  1102. return ((Extractor*)ex)->input(index, *((const Mat*)mat));
  1103. }
  1104. int ncnn_extractor_extract_index(ncnn_extractor_t ex, int index, ncnn_mat_t* mat)
  1105. {
  1106. Mat mat0;
  1107. int ret = ((Extractor*)ex)->extract(index, mat0);
  1108. *mat = (ncnn_mat_t)(new Mat(mat0));
  1109. return ret;
  1110. }
  1111. #ifdef __cplusplus
  1112. } /* extern "C" */
  1113. #endif