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