Elffuss BraidHub

kikocis/ncnn public

master · 46.7 KiBRawPermalink
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
16#include "platform.h"
17
18#if NCNN_C_API
19
20#include "c_api.h"
21
22#include <stdlib.h>
23
24#include "allocator.h"
25#include "blob.h"
26#include "datareader.h"
27#include "layer.h"
28#include "mat.h"
29#include "modelbin.h"
30#include "net.h"
31#include "option.h"
32#include "paramdict.h"
33
34using ncnn::Allocator;
35using ncnn::Blob;
36using ncnn::DataReader;
37using ncnn::Extractor;
38using ncnn::Layer;
39using ncnn::Mat;
40using ncnn::ModelBin;
41using ncnn::Net;
42using ncnn::Option;
43using ncnn::ParamDict;
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49const char* ncnn_version()
50{
51 return NCNN_VERSION_STRING;
52}
53
54/* allocator api */
55class PoolAllocator_c_api : public ncnn::PoolAllocator
56{
57public:
58 PoolAllocator_c_api(ncnn_allocator_t _allocator)
59 : ncnn::PoolAllocator()
60 {
61 allocator = _allocator;
62 }
63
64 virtual void* fastMalloc(size_t size)
65 {
66 return allocator->fast_malloc(allocator, size);
67 }
68
69 virtual void fastFree(void* ptr)
70 {
71 return allocator->fast_free(allocator, ptr);
72 }
73
74public:
75 ncnn_allocator_t allocator;
76};
77
78static void* __ncnn_PoolAllocator_fast_malloc(ncnn_allocator_t allocator, size_t size)
79{
80 return ((ncnn::PoolAllocator*)allocator->pthis)->ncnn::PoolAllocator::fastMalloc(size);
81}
82
83static void __ncnn_PoolAllocator_fast_free(ncnn_allocator_t allocator, void* ptr)
84{
85 ((ncnn::PoolAllocator*)allocator->pthis)->ncnn::PoolAllocator::fastFree(ptr);
86}
87
88class UnlockedPoolAllocator_c_api : public ncnn::UnlockedPoolAllocator
89{
90public:
91 UnlockedPoolAllocator_c_api(ncnn_allocator_t _allocator)
92 : ncnn::UnlockedPoolAllocator()
93 {
94 allocator = _allocator;
95 }
96
97 virtual void* fastMalloc(size_t size)
98 {
99 return allocator->fast_malloc(allocator, size);
100 }
101
102 virtual void fastFree(void* ptr)
103 {
104 return allocator->fast_free(allocator, ptr);
105 }
106
107public:
108 ncnn_allocator_t allocator;
109};
110
111static void* __ncnn_UnlockedPoolAllocator_fast_malloc(ncnn_allocator_t allocator, size_t size)
112{
113 return ((ncnn::UnlockedPoolAllocator*)allocator->pthis)->ncnn::UnlockedPoolAllocator::fastMalloc(size);
114}
115
116static void __ncnn_UnlockedPoolAllocator_fast_free(ncnn_allocator_t allocator, void* ptr)
117{
118 ((ncnn::UnlockedPoolAllocator*)allocator->pthis)->ncnn::UnlockedPoolAllocator::fastFree(ptr);
119}
120
121ncnn_allocator_t ncnn_allocator_create_pool_allocator()
122{
123 ncnn_allocator_t allocator = (ncnn_allocator_t)malloc(sizeof(struct __ncnn_allocator_t));
124 allocator->pthis = (void*)(new PoolAllocator_c_api(allocator));
125 allocator->fast_malloc = __ncnn_PoolAllocator_fast_malloc;
126 allocator->fast_free = __ncnn_PoolAllocator_fast_free;
127 return allocator;
128}
129
130ncnn_allocator_t ncnn_allocator_create_unlocked_pool_allocator()
131{
132 ncnn_allocator_t allocator = (ncnn_allocator_t)malloc(sizeof(struct __ncnn_allocator_t));
133 allocator->pthis = (void*)(new UnlockedPoolAllocator_c_api(allocator));
134 allocator->fast_malloc = __ncnn_UnlockedPoolAllocator_fast_malloc;
135 allocator->fast_free = __ncnn_UnlockedPoolAllocator_fast_free;
136 return allocator;
137}
138
139void ncnn_allocator_destroy(ncnn_allocator_t allocator)
140{
141 if (allocator)
142 {
143 delete (Allocator*)allocator->pthis;
144 free(allocator);
145 }
146}
147
148/* option api */
149ncnn_option_t ncnn_option_create()
150{
151 return (ncnn_option_t)(new Option());
152}
153
154void ncnn_option_destroy(ncnn_option_t opt)
155{
156 delete (Option*)opt;
157}
158
159int ncnn_option_get_num_threads(const ncnn_option_t opt)
160{
161 return ((const Option*)opt)->num_threads;
162}
163
164void ncnn_option_set_num_threads(ncnn_option_t opt, int num_threads)
165{
166 ((Option*)opt)->num_threads = num_threads;
167}
168
169int ncnn_option_get_use_local_pool_allocator(const ncnn_option_t opt)
170{
171 return ((Option*)opt)->use_local_pool_allocator;
172}
173
174void ncnn_option_set_use_local_pool_allocator(ncnn_option_t opt, int use_local_pool_allocator)
175{
176 ((Option*)opt)->use_local_pool_allocator = use_local_pool_allocator;
177}
178
179void ncnn_option_set_blob_allocator(ncnn_option_t opt, ncnn_allocator_t allocator)
180{
181 ((Option*)opt)->blob_allocator = allocator ? (Allocator*)allocator->pthis : NULL;
182}
183
184void ncnn_option_set_workspace_allocator(ncnn_option_t opt, ncnn_allocator_t allocator)
185{
186 ((Option*)opt)->workspace_allocator = allocator ? (Allocator*)allocator->pthis : NULL;
187}
188
189int ncnn_option_get_use_vulkan_compute(const ncnn_option_t opt)
190{
191#if NCNN_VULKAN
192 return ((const Option*)opt)->use_vulkan_compute;
193#else
194 (void)opt;
195 return 0;
196#endif
197}
198
199void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_compute)
200{
201#if NCNN_VULKAN
202 ((Option*)opt)->use_vulkan_compute = use_vulkan_compute;
203#else
204 (void)opt;
205 (void)use_vulkan_compute;
206#endif
207}
208
209/* mat api */
210ncnn_mat_t ncnn_mat_create()
211{
212 return (ncnn_mat_t)(new Mat());
213}
214
215ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator)
216{
217 return (ncnn_mat_t)(new Mat(w, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
218}
219
220ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator)
221{
222 return (ncnn_mat_t)(new Mat(w, h, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
223}
224
225ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator)
226{
227 return (ncnn_mat_t)(new Mat(w, h, c, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
228}
229
230ncnn_mat_t ncnn_mat_create_4d(int w, int h, int d, int c, ncnn_allocator_t allocator)
231{
232 return (ncnn_mat_t)(new Mat(w, h, d, c, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
233}
234
235ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator)
236{
237 return (ncnn_mat_t)(new Mat(w, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
238}
239
240ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data, ncnn_allocator_t allocator)
241{
242 return (ncnn_mat_t)(new Mat(w, h, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
243}
244
245ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data, ncnn_allocator_t allocator)
246{
247 return (ncnn_mat_t)(new Mat(w, h, c, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
248}
249
250ncnn_mat_t ncnn_mat_create_external_4d(int w, int h, int d, int c, void* data, ncnn_allocator_t allocator)
251{
252 return (ncnn_mat_t)(new Mat(w, h, d, c, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
253}
254
255ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack, ncnn_allocator_t allocator)
256{
257 return (ncnn_mat_t)(new Mat(w, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
258}
259
260ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack, ncnn_allocator_t allocator)
261{
262 return (ncnn_mat_t)(new Mat(w, h, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
263}
264
265ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator)
266{
267 return (ncnn_mat_t)(new Mat(w, h, c, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
268}
269
270ncnn_mat_t ncnn_mat_create_4d_elem(int w, int h, int d, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator)
271{
272 return (ncnn_mat_t)(new Mat(w, h, d, c, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
273}
274
275ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
276{
277 return (ncnn_mat_t)(new Mat(w, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
278}
279
280ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
281{
282 return (ncnn_mat_t)(new Mat(w, h, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
283}
284
285ncnn_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)
286{
287 return (ncnn_mat_t)(new Mat(w, h, c, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
288}
289
290ncnn_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)
291{
292 return (ncnn_mat_t)(new Mat(w, h, d, c, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
293}
294
295void ncnn_mat_destroy(ncnn_mat_t mat)
296{
297 delete (Mat*)mat;
298}
299
300void ncnn_mat_fill_float(ncnn_mat_t mat, float v)
301{
302 ((Mat*)mat)->fill(v);
303}
304
305ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat, ncnn_allocator_t allocator)
306{
307 return (ncnn_mat_t)(new Mat(((const Mat*)mat)->clone(allocator ? (Allocator*)allocator->pthis : NULL)));
308}
309
310ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w, ncnn_allocator_t allocator)
311{
312 return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, allocator ? (Allocator*)allocator->pthis : NULL)));
313}
314
315ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h, ncnn_allocator_t allocator)
316{
317 return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, allocator ? (Allocator*)allocator->pthis : NULL)));
318}
319
320ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c, ncnn_allocator_t allocator)
321{
322 return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, c, allocator ? (Allocator*)allocator->pthis : NULL)));
323}
324
325ncnn_mat_t ncnn_mat_reshape_4d(const ncnn_mat_t mat, int w, int h, int d, int c, ncnn_allocator_t allocator)
326{
327 return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, d, c, allocator ? (Allocator*)allocator->pthis : NULL)));
328}
329
330int ncnn_mat_get_dims(const ncnn_mat_t mat)
331{
332 return ((const Mat*)mat)->dims;
333}
334
335int ncnn_mat_get_w(const ncnn_mat_t mat)
336{
337 return ((const Mat*)mat)->w;
338}
339
340int ncnn_mat_get_h(const ncnn_mat_t mat)
341{
342 return ((const Mat*)mat)->h;
343}
344
345int ncnn_mat_get_d(const ncnn_mat_t mat)
346{
347 return ((const Mat*)mat)->d;
348}
349
350int ncnn_mat_get_c(const ncnn_mat_t mat)
351{
352 return ((const Mat*)mat)->c;
353}
354
355size_t ncnn_mat_get_elemsize(const ncnn_mat_t mat)
356{
357 return ((const Mat*)mat)->elemsize;
358}
359
360int ncnn_mat_get_elempack(const ncnn_mat_t mat)
361{
362 return ((const Mat*)mat)->elempack;
363}
364
365size_t ncnn_mat_get_cstep(const ncnn_mat_t mat)
366{
367 return ((const Mat*)mat)->cstep;
368}
369
370void* ncnn_mat_get_data(const ncnn_mat_t mat)
371{
372 return ((const Mat*)mat)->data;
373}
374
375void* ncnn_mat_get_channel_data(const ncnn_mat_t mat, int c)
376{
377 return ((const Mat*)mat)->channel(c).data;
378}
379
380#if NCNN_PIXEL
381
382/* mat pixel api */
383ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, ncnn_allocator_t allocator)
384{
385 return (ncnn_mat_t)(new Mat(Mat::from_pixels(pixels, type, w, h, stride, allocator ? (Allocator*)allocator->pthis : NULL)));
386}
387
388ncnn_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)
389{
390 return (ncnn_mat_t)(new Mat(Mat::from_pixels_resize(pixels, type, w, h, stride, target_width, target_height, allocator ? (Allocator*)allocator->pthis : NULL)));
391}
392
393ncnn_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)
394{
395 return (ncnn_mat_t)(new Mat(Mat::from_pixels_roi(pixels, type, w, h, stride, roix, roiy, roiw, roih, allocator ? (Allocator*)allocator->pthis : NULL)));
396}
397
398ncnn_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)
399{
400 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)));
401}
402
403void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride)
404{
405 ((const Mat*)mat)->to_pixels(pixels, type, stride);
406}
407
408void ncnn_mat_to_pixels_resize(const ncnn_mat_t mat, unsigned char* pixels, int type, int target_width, int target_height, int target_stride)
409{
410 ((const Mat*)mat)->to_pixels_resize(pixels, type, target_width, target_height, target_stride);
411}
412
413#endif /* NCNN_PIXEL */
414
415void ncnn_mat_substract_mean_normalize(ncnn_mat_t mat, const float* mean_vals, const float* norm_vals)
416{
417 ((Mat*)mat)->substract_mean_normalize(mean_vals, norm_vals);
418}
419
420void ncnn_convert_packing(const ncnn_mat_t src, ncnn_mat_t* dst, int elempack, const ncnn_option_t opt)
421{
422 Mat _dst;
423 ncnn::convert_packing(*(const Mat*)src, _dst, elempack, *(Option*)opt);
424 *dst = (ncnn_mat_t)(new Mat(_dst));
425}
426
427void ncnn_flatten(const ncnn_mat_t src, ncnn_mat_t* dst, const ncnn_option_t opt)
428{
429 Mat _dst;
430 ncnn::flatten(*(const Mat*)src, _dst, *(Option*)opt);
431 *dst = (ncnn_mat_t)(new Mat(_dst));
432}
433
434/* blob api */
435#if NCNN_STRING
436const char* ncnn_blob_get_name(const ncnn_blob_t blob)
437{
438 return ((const Blob*)blob)->name.c_str();
439}
440#endif /* NCNN_STRING */
441
442int ncnn_blob_get_producer(const ncnn_blob_t blob)
443{
444 return ((const Blob*)blob)->producer;
445}
446
447int ncnn_blob_get_consumer(const ncnn_blob_t blob)
448{
449 return ((const Blob*)blob)->consumer;
450}
451
452void ncnn_blob_get_shape(const ncnn_blob_t blob, int* dims, int* w, int* h, int* c)
453{
454 const Mat& shape = ((const Blob*)blob)->shape;
455 *dims = shape.dims;
456 *w = shape.w;
457 *h = shape.h;
458 *c = shape.c;
459}
460
461/* paramdict api */
462ncnn_paramdict_t ncnn_paramdict_create()
463{
464 return (ncnn_paramdict_t)(new ParamDict());
465}
466
467void ncnn_paramdict_destroy(ncnn_paramdict_t pd)
468{
469 delete (ParamDict*)pd;
470}
471
472int ncnn_paramdict_get_type(const ncnn_paramdict_t pd, int id)
473{
474 return ((const ParamDict*)pd)->type(id);
475}
476
477int ncnn_paramdict_get_int(const ncnn_paramdict_t pd, int id, int def)
478{
479 return ((const ParamDict*)pd)->get(id, def);
480}
481
482float ncnn_paramdict_get_float(const ncnn_paramdict_t pd, int id, float def)
483{
484 return ((const ParamDict*)pd)->get(id, def);
485}
486
487ncnn_mat_t ncnn_paramdict_get_array(ncnn_paramdict_t pd, int id, const ncnn_mat_t def)
488{
489 return (ncnn_mat_t)(new Mat(((const ParamDict*)pd)->get(id, *(const Mat*)def)));
490}
491
492void ncnn_paramdict_set_int(ncnn_paramdict_t pd, int id, int i)
493{
494 return ((ParamDict*)pd)->set(id, i);
495}
496
497void ncnn_paramdict_set_float(ncnn_paramdict_t pd, int id, float f)
498{
499 return ((ParamDict*)pd)->set(id, f);
500}
501
502void ncnn_paramdict_set_array(ncnn_paramdict_t pd, int id, ncnn_mat_t v)
503{
504 return ((ParamDict*)pd)->set(id, *(const Mat*)v);
505}
506
507/* datareader api */
508class DataReader_c_api : public ncnn::DataReader
509{
510public:
511 DataReader_c_api(ncnn_datareader_t _dr)
512 : ncnn::DataReader()
513 {
514 dr = _dr;
515 }
516
517#if NCNN_STRING
518 virtual int scan(const char* format, void* p) const
519 {
520 return dr->scan(dr, format, p);
521 }
522#endif /* NCNN_STRING */
523
524 virtual size_t read(void* buf, size_t size) const
525 {
526 return dr->read(dr, buf, size);
527 }
528
529public:
530 ncnn_datareader_t dr;
531};
532
533#if NCNN_STRING
534static int __ncnn_DataReader_scan(ncnn_datareader_t dr, const char* format, void* p)
535{
536 return ((ncnn::DataReader*)dr->pthis)->ncnn::DataReader::scan(format, p);
537}
538#endif /* NCNN_STRING */
539
540static size_t __ncnn_DataReader_read(ncnn_datareader_t dr, void* buf, size_t size)
541{
542 return ((ncnn::DataReader*)dr->pthis)->ncnn::DataReader::read(buf, size);
543}
544
545#if NCNN_STDIO
546class DataReaderFromStdio_c_api : public ncnn::DataReaderFromStdio
547{
548public:
549 DataReaderFromStdio_c_api(FILE* fp, ncnn_datareader_t _dr)
550 : ncnn::DataReaderFromStdio(fp)
551 {
552 dr = _dr;
553 }
554
555#if NCNN_STRING
556 virtual int scan(const char* format, void* p) const
557 {
558 return dr->scan(dr, format, p);
559 }
560#endif /* NCNN_STRING */
561
562 virtual size_t read(void* buf, size_t size) const
563 {
564 return dr->read(dr, buf, size);
565 }
566
567public:
568 ncnn_datareader_t dr;
569};
570
571#if NCNN_STRING
572static int __ncnn_DataReaderFromStdio_scan(ncnn_datareader_t dr, const char* format, void* p)
573{
574 return ((ncnn::DataReaderFromStdio*)dr->pthis)->ncnn::DataReaderFromStdio::scan(format, p);
575}
576#endif /* NCNN_STRING */
577
578static size_t __ncnn_DataReaderFromStdio_read(ncnn_datareader_t dr, void* buf, size_t size)
579{
580 return ((ncnn::DataReaderFromStdio*)dr->pthis)->ncnn::DataReaderFromStdio::read(buf, size);
581}
582#endif /* NCNN_STDIO */
583
584class DataReaderFromMemory_c_api : public ncnn::DataReaderFromMemory
585{
586public:
587 DataReaderFromMemory_c_api(const unsigned char*& mem, ncnn_datareader_t _dr)
588 : ncnn::DataReaderFromMemory(mem)
589 {
590 dr = _dr;
591 }
592
593#if NCNN_STRING
594 virtual int scan(const char* format, void* p) const
595 {
596 return dr->scan(dr, format, p);
597 }
598#endif /* NCNN_STRING */
599
600 virtual size_t read(void* buf, size_t size) const
601 {
602 return dr->read(dr, buf, size);
603 }
604
605public:
606 ncnn_datareader_t dr;
607};
608
609#if NCNN_STRING
610static int __ncnn_DataReaderFromMemory_scan(ncnn_datareader_t dr, const char* format, void* p)
611{
612 return ((ncnn::DataReaderFromMemory*)dr->pthis)->ncnn::DataReaderFromMemory::scan(format, p);
613}
614#endif /* NCNN_STRING */
615
616static size_t __ncnn_DataReaderFromMemory_read(ncnn_datareader_t dr, void* buf, size_t size)
617{
618 return ((ncnn::DataReaderFromMemory*)dr->pthis)->ncnn::DataReaderFromMemory::read(buf, size);
619}
620
621ncnn_datareader_t ncnn_datareader_create()
622{
623 ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
624 dr->pthis = (void*)(new DataReader_c_api(dr));
625#if NCNN_STRING
626 dr->scan = __ncnn_DataReader_scan;
627#endif /* NCNN_STRING */
628 dr->read = __ncnn_DataReader_read;
629 return dr;
630}
631
632#if NCNN_STDIO
633ncnn_datareader_t ncnn_datareader_create_from_stdio(FILE* fp)
634{
635 ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
636 dr->pthis = (void*)(new DataReaderFromStdio_c_api(fp, dr));
637#if NCNN_STRING
638 dr->scan = __ncnn_DataReaderFromStdio_scan;
639#endif /* NCNN_STRING */
640 dr->read = __ncnn_DataReaderFromStdio_read;
641 return dr;
642}
643#endif /* NCNN_STDIO */
644
645ncnn_datareader_t ncnn_datareader_create_from_memory(const unsigned char** mem)
646{
647 ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
648 dr->pthis = (void*)(new DataReaderFromMemory_c_api(*mem, dr));
649#if NCNN_STRING
650 dr->scan = __ncnn_DataReaderFromMemory_scan;
651#endif /* NCNN_STRING */
652 dr->read = __ncnn_DataReaderFromMemory_read;
653 return dr;
654}
655
656void ncnn_datareader_destroy(ncnn_datareader_t dr)
657{
658 delete (DataReader*)dr->pthis;
659 free(dr);
660}
661
662/* modelbin api */
663class ModelBinFromDataReader_c_api : public ncnn::ModelBinFromDataReader
664{
665public:
666 ModelBinFromDataReader_c_api(ncnn_modelbin_t _mb, const DataReader& dr)
667 : ncnn::ModelBinFromDataReader(dr)
668 {
669 mb = _mb;
670 }
671
672 virtual Mat load(int w, int type) const
673 {
674 ncnn_mat_t m = mb->load_1d(mb, w, type);
675 Mat m2 = *(Mat*)m;
676 ncnn_mat_destroy(m);
677 return m2;
678 }
679
680 virtual Mat load(int w, int h, int type) const
681 {
682 ncnn_mat_t m = mb->load_2d(mb, w, h, type);
683 Mat m2 = *(Mat*)m;
684 ncnn_mat_destroy(m);
685 return m2;
686 }
687
688 virtual Mat load(int w, int h, int c, int type) const
689 {
690 ncnn_mat_t m = mb->load_3d(mb, w, h, c, type);
691 Mat m2 = *(Mat*)m;
692 ncnn_mat_destroy(m);
693 return m2;
694 }
695
696public:
697 ncnn_modelbin_t mb;
698};
699
700static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_1d(const ncnn_modelbin_t mb, int w, int type)
701{
702 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBinFromDataReader::load(w, type)));
703}
704
705static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
706{
707 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBin::load(w, h, type)));
708}
709
710static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
711{
712 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBin::load(w, h, c, type)));
713}
714
715class ModelBinFromMatArray_c_api : public ncnn::ModelBinFromMatArray
716{
717public:
718 ModelBinFromMatArray_c_api(ncnn_modelbin_t _mb, const Mat* weights)
719 : ncnn::ModelBinFromMatArray(weights)
720 {
721 mb = _mb;
722 }
723
724 virtual Mat load(int w, int type) const
725 {
726 ncnn_mat_t m = mb->load_1d(mb, w, type);
727 Mat m2 = *(Mat*)m;
728 ncnn_mat_destroy(m);
729 return m2;
730 }
731
732 virtual Mat load(int w, int h, int type) const
733 {
734 ncnn_mat_t m = mb->load_2d(mb, w, h, type);
735 Mat m2 = *(Mat*)m;
736 ncnn_mat_destroy(m);
737 return m2;
738 }
739
740 virtual Mat load(int w, int h, int c, int type) const
741 {
742 ncnn_mat_t m = mb->load_3d(mb, w, h, c, type);
743 Mat m2 = *(Mat*)m;
744 ncnn_mat_destroy(m);
745 return m2;
746 }
747
748public:
749 ncnn_modelbin_t mb;
750};
751
752static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_1d(const ncnn_modelbin_t mb, int w, int type)
753{
754 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBinFromMatArray::load(w, type)));
755}
756
757static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
758{
759 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBin::load(w, h, type)));
760}
761
762static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
763{
764 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBin::load(w, h, c, type)));
765}
766
767ncnn_modelbin_t ncnn_modelbin_create_from_datareader(const ncnn_datareader_t dr)
768{
769 ncnn_modelbin_t mb = (ncnn_modelbin_t)malloc(sizeof(struct __ncnn_modelbin_t));
770 mb->pthis = (void*)(new ModelBinFromDataReader_c_api(mb, *(const DataReader*)dr->pthis));
771 mb->load_1d = __ncnn_ModelBinFromDataReader_load_1d;
772 mb->load_2d = __ncnn_ModelBinFromDataReader_load_2d;
773 mb->load_3d = __ncnn_ModelBinFromDataReader_load_3d;
774 return mb;
775}
776
777ncnn_modelbin_t ncnn_modelbin_create_from_mat_array(const ncnn_mat_t* weights, int n)
778{
779 std::vector<Mat> matarray(n);
780 for (int i = 0; i < n; i++)
781 {
782 matarray[i] = *(const Mat*)weights[i];
783 }
784 ncnn_modelbin_t mb = (ncnn_modelbin_t)malloc(sizeof(struct __ncnn_modelbin_t));
785 mb->pthis = (void*)(new ModelBinFromMatArray_c_api(mb, &matarray[0]));
786 mb->load_1d = __ncnn_ModelBinFromMatArray_load_1d;
787 mb->load_2d = __ncnn_ModelBinFromMatArray_load_2d;
788 mb->load_3d = __ncnn_ModelBinFromMatArray_load_3d;
789 return mb;
790}
791
792void ncnn_modelbin_destroy(ncnn_modelbin_t mb)
793{
794 delete (ModelBin*)mb->pthis;
795 free(mb);
796}
797
798static ncnn_mat_t __ncnn_modelbin_load_1d(const ncnn_modelbin_t mb, int w, int type)
799{
800 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, type)));
801}
802
803static ncnn_mat_t __ncnn_modelbin_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
804{
805 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, h, type)));
806}
807
808static ncnn_mat_t __ncnn_modelbin_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
809{
810 return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, h, c, type)));
811}
812
813/* layer api */
814class Layer_c_api : public Layer
815{
816public:
817 Layer_c_api(ncnn_layer_t _layer)
818 : Layer()
819 {
820 layer = _layer;
821 }
822
823 virtual int load_param(const ParamDict& pd)
824 {
825 return layer->load_param(layer, (ncnn_paramdict_t)&pd);
826 }
827
828 virtual int load_model(const ModelBin& mb)
829 {
830 struct __ncnn_modelbin_t mb0;
831 mb0.pthis = (void*)&mb;
832 mb0.load_1d = __ncnn_modelbin_load_1d;
833 mb0.load_2d = __ncnn_modelbin_load_2d;
834 mb0.load_3d = __ncnn_modelbin_load_3d;
835 return layer->load_model(layer, &mb0);
836 }
837
838 virtual int create_pipeline(const Option& opt)
839 {
840 return layer->create_pipeline(layer, (ncnn_option_t)&opt);
841 }
842
843 virtual int destroy_pipeline(const Option& opt)
844 {
845 return layer->destroy_pipeline(layer, (ncnn_option_t)&opt);
846 }
847
848 virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
849 {
850 const int n = bottom_blobs.size();
851 const int n2 = top_blobs.size();
852 std::vector<ncnn_mat_t> bottom_blobs0(n);
853 for (int i = 0; i < n; i++)
854 {
855 bottom_blobs0[i] = (ncnn_mat_t)&bottom_blobs[i];
856 }
857 std::vector<ncnn_mat_t> top_blobs0(n2, (ncnn_mat_t)0);
858 int ret = layer->forward_n(layer, &bottom_blobs0[0], n, &top_blobs0[0], n2, (ncnn_option_t)&opt);
859 for (int i = 0; i < n2; i++)
860 {
861 top_blobs[i] = *(Mat*)top_blobs0[i];
862 ncnn_mat_destroy(top_blobs0[i]);
863 }
864 return ret;
865 }
866
867 virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
868 {
869 ncnn_mat_t top_blob0 = 0;
870 int ret = layer->forward_1(layer, (ncnn_mat_t)&bottom_blob, &top_blob0, (ncnn_option_t)&opt);
871 top_blob = *(Mat*)top_blob0;
872 ncnn_mat_destroy(top_blob0);
873 return ret;
874 }
875
876 virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const
877 {
878 const int n = bottom_top_blobs.size();
879 std::vector<ncnn_mat_t> bottom_top_blobs0(n);
880 for (int i = 0; i < n; i++)
881 {
882 bottom_top_blobs0[i] = (ncnn_mat_t)&bottom_top_blobs[i];
883 }
884 return layer->forward_inplace_n(layer, &bottom_top_blobs0[0], n, (ncnn_option_t)&opt);
885 }
886
887 virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const
888 {
889 return layer->forward_inplace_1(layer, (ncnn_mat_t)&bottom_top_blob, (ncnn_option_t)&opt);
890 }
891
892public:
893 ncnn_layer_t layer;
894};
895
896static int __ncnn_Layer_load_param(ncnn_layer_t layer, const ncnn_paramdict_t pd)
897{
898 return ((Layer*)layer->pthis)->Layer::load_param(*(const ParamDict*)pd);
899}
900
901static int __ncnn_Layer_load_model(ncnn_layer_t layer, const ncnn_modelbin_t mb)
902{
903 return ((Layer*)layer->pthis)->Layer::load_model(*(const ModelBin*)mb);
904}
905
906static int __ncnn_Layer_create_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
907{
908 return ((Layer*)layer->pthis)->Layer::create_pipeline(*(const Option*)opt);
909}
910
911static int __ncnn_Layer_destroy_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
912{
913 return ((Layer*)layer->pthis)->Layer::destroy_pipeline(*(const Option*)opt);
914}
915
916static 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)
917{
918 Mat _top_blob;
919 int ret = ((const Layer*)layer->pthis)->Layer::forward(*(const Mat*)bottom_blob, _top_blob, *(const Option*)opt);
920 *top_blob = (ncnn_mat_t)(new Mat(_top_blob));
921 return ret;
922}
923
924static 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)
925{
926 std::vector<Mat> _bottom_blobs(n);
927 std::vector<Mat> _top_blobs(n2);
928 for (int i = 0; i < n; i++)
929 {
930 _bottom_blobs[i] = *(Mat*)bottom_blobs[i];
931 }
932 int ret = ((const Layer*)layer->pthis)->Layer::forward(_bottom_blobs, _top_blobs, *(const Option*)opt);
933 for (int i = 0; i < n2; i++)
934 {
935 top_blobs[i] = (ncnn_mat_t)(new Mat(_top_blobs[i]));
936 }
937 return ret;
938}
939
940static int __ncnn_Layer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
941{
942 return ((const Layer*)layer->pthis)->Layer::forward_inplace(*(Mat*)bottom_top_blob, *(const Option*)opt);
943}
944
945static int __ncnn_Layer_forward_inplace_n(const ncnn_layer_t layer, ncnn_mat_t* bottom_top_blobs, int n, const ncnn_option_t opt)
946{
947 std::vector<Mat> _bottom_top_blobs(n);
948 for (int i = 0; i < n; i++)
949 {
950 _bottom_top_blobs[i] = *(Mat*)bottom_top_blobs[i];
951 }
952 return ((const Layer*)layer->pthis)->Layer::forward_inplace(_bottom_top_blobs, *(const Option*)opt);
953}
954
955static int __ncnn_layer_load_param(ncnn_layer_t layer, const ncnn_paramdict_t pd)
956{
957 return ((Layer*)layer->pthis)->load_param(*(const ParamDict*)pd);
958}
959
960static int __ncnn_layer_load_model(ncnn_layer_t layer, const ncnn_modelbin_t mb)
961{
962 return ((Layer*)layer->pthis)->load_model(*(const ModelBin*)mb);
963}
964
965static int __ncnn_layer_create_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
966{
967 return ((Layer*)layer->pthis)->create_pipeline(*(const Option*)opt);
968}
969
970static int __ncnn_layer_destroy_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
971{
972 return ((Layer*)layer->pthis)->destroy_pipeline(*(const Option*)opt);
973}
974
975static 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)
976{
977 Mat _top_blob;
978 int ret = ((const Layer*)layer->pthis)->forward(*(const Mat*)bottom_blob, _top_blob, *(const Option*)opt);
979 *top_blob = (ncnn_mat_t)(new Mat(_top_blob));
980 return ret;
981}
982
983static 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)
984{
985 std::vector<Mat> _bottom_blobs(n);
986 std::vector<Mat> _top_blobs(n2);
987 for (int i = 0; i < n; i++)
988 {
989 _bottom_blobs[i] = *(Mat*)bottom_blobs[i];
990 }
991 int ret = ((const Layer*)layer->pthis)->forward(_bottom_blobs, _top_blobs, *(const Option*)opt);
992 for (int i = 0; i < n2; i++)
993 {
994 top_blobs[i] = (ncnn_mat_t)(new Mat(_top_blobs[i]));
995 }
996 return ret;
997}
998
999static int __ncnn_layer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
1000{
1001 return ((const Layer*)layer->pthis)->forward_inplace(*(Mat*)bottom_top_blob, *(const Option*)opt);
1002}
1003
1004static int __ncnn_layer_forward_inplace_n(const ncnn_layer_t layer, ncnn_mat_t* bottom_top_blobs, int n, const ncnn_option_t opt)
1005{
1006 std::vector<Mat> _bottom_top_blobs(n);
1007 for (int i = 0; i < n; i++)
1008 {
1009 _bottom_top_blobs[i] = *(Mat*)bottom_top_blobs[i];
1010 }
1011 return ((const Layer*)layer->pthis)->forward_inplace(_bottom_top_blobs, *(const Option*)opt);
1012}
1013
1014ncnn_layer_t ncnn_layer_create()
1015{
1016 ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
1017 layer->pthis = (void*)(new Layer_c_api(layer));
1018 layer->load_param = __ncnn_Layer_load_param;
1019 layer->load_model = __ncnn_Layer_load_model;
1020 layer->create_pipeline = __ncnn_Layer_create_pipeline;
1021 layer->destroy_pipeline = __ncnn_Layer_destroy_pipeline;
1022 layer->forward_1 = __ncnn_Layer_forward_1;
1023 layer->forward_n = __ncnn_Layer_forward_n;
1024 layer->forward_inplace_1 = __ncnn_Layer_forward_inplace_1;
1025 layer->forward_inplace_n = __ncnn_Layer_forward_inplace_n;
1026 return layer;
1027}
1028
1029ncnn_layer_t ncnn_layer_create_by_typeindex(int typeindex)
1030{
1031 void* pthis = (void*)(ncnn::create_layer(typeindex));
1032 if (!pthis)
1033 {
1034 return 0;
1035 }
1036
1037 ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
1038 layer->pthis = pthis;
1039 layer->load_param = __ncnn_layer_load_param;
1040 layer->load_model = __ncnn_layer_load_model;
1041 layer->create_pipeline = __ncnn_layer_create_pipeline;
1042 layer->destroy_pipeline = __ncnn_layer_destroy_pipeline;
1043 layer->forward_1 = __ncnn_layer_forward_1;
1044 layer->forward_n = __ncnn_layer_forward_n;
1045 layer->forward_inplace_1 = __ncnn_layer_forward_inplace_1;
1046 layer->forward_inplace_n = __ncnn_layer_forward_inplace_n;
1047 return layer;
1048}
1049
1050#if NCNN_STRING
1051ncnn_layer_t ncnn_layer_create_by_type(const char* type)
1052{
1053 void* pthis = (void*)(ncnn::create_layer(type));
1054 if (!pthis)
1055 {
1056 return 0;
1057 }
1058
1059 ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
1060 layer->pthis = pthis;
1061 layer->load_param = __ncnn_layer_load_param;
1062 layer->load_model = __ncnn_layer_load_model;
1063 layer->create_pipeline = __ncnn_layer_create_pipeline;
1064 layer->destroy_pipeline = __ncnn_layer_destroy_pipeline;
1065 layer->forward_1 = __ncnn_layer_forward_1;
1066 layer->forward_n = __ncnn_layer_forward_n;
1067 layer->forward_inplace_1 = __ncnn_layer_forward_inplace_1;
1068 layer->forward_inplace_n = __ncnn_layer_forward_inplace_n;
1069 return layer;
1070}
1071
1072int ncnn_layer_type_to_index(const char* type)
1073{
1074 return ncnn::layer_to_index(type);
1075}
1076#endif /* NCNN_STRING */
1077
1078void ncnn_layer_destroy(ncnn_layer_t layer)
1079{
1080 delete (Layer*)layer->pthis;
1081 free(layer);
1082}
1083
1084#if NCNN_STRING
1085const char* ncnn_layer_get_name(const ncnn_layer_t layer)
1086{
1087 return ((const Layer*)layer->pthis)->name.c_str();
1088}
1089#endif /* NCNN_STRING */
1090
1091int ncnn_layer_get_typeindex(const ncnn_layer_t layer)
1092{
1093 return ((const Layer*)layer->pthis)->typeindex;
1094}
1095
1096#if NCNN_STRING
1097const char* ncnn_layer_get_type(const ncnn_layer_t layer)
1098{
1099 return ((const Layer*)layer->pthis)->type.c_str();
1100}
1101#endif /* NCNN_STRING */
1102
1103int ncnn_layer_get_one_blob_only(const ncnn_layer_t layer)
1104{
1105 return ((const Layer*)layer->pthis)->one_blob_only;
1106}
1107
1108int ncnn_layer_get_support_inplace(const ncnn_layer_t layer)
1109{
1110 return ((const Layer*)layer->pthis)->support_inplace;
1111}
1112
1113int ncnn_layer_get_support_vulkan(const ncnn_layer_t layer)
1114{
1115 return ((const Layer*)layer->pthis)->support_vulkan;
1116}
1117
1118int ncnn_layer_get_support_packing(const ncnn_layer_t layer)
1119{
1120 return ((const Layer*)layer->pthis)->support_packing;
1121}
1122
1123int ncnn_layer_get_support_bf16_storage(const ncnn_layer_t layer)
1124{
1125 return ((const Layer*)layer->pthis)->support_bf16_storage;
1126}
1127
1128int ncnn_layer_get_support_fp16_storage(const ncnn_layer_t layer)
1129{
1130 return ((const Layer*)layer->pthis)->support_fp16_storage;
1131}
1132
1133int ncnn_layer_get_support_image_storage(const ncnn_layer_t layer)
1134{
1135 return ((const Layer*)layer->pthis)->support_image_storage;
1136}
1137
1138void ncnn_layer_set_one_blob_only(ncnn_layer_t layer, int enable)
1139{
1140 ((Layer*)layer->pthis)->one_blob_only = enable;
1141}
1142
1143void ncnn_layer_set_support_inplace(ncnn_layer_t layer, int enable)
1144{
1145 ((Layer*)layer->pthis)->support_inplace = enable;
1146}
1147
1148void ncnn_layer_set_support_vulkan(ncnn_layer_t layer, int enable)
1149{
1150 ((Layer*)layer->pthis)->support_vulkan = enable;
1151}
1152
1153void ncnn_layer_set_support_packing(ncnn_layer_t layer, int enable)
1154{
1155 ((Layer*)layer->pthis)->support_packing = enable;
1156}
1157
1158void ncnn_layer_set_support_bf16_storage(ncnn_layer_t layer, int enable)
1159{
1160 ((Layer*)layer->pthis)->support_bf16_storage = enable;
1161}
1162
1163void ncnn_layer_set_support_fp16_storage(ncnn_layer_t layer, int enable)
1164{
1165 ((Layer*)layer->pthis)->support_fp16_storage = enable;
1166}
1167
1168void ncnn_layer_set_support_image_storage(ncnn_layer_t layer, int enable)
1169{
1170 ((Layer*)layer->pthis)->support_image_storage = enable;
1171}
1172
1173int ncnn_layer_get_bottom_count(const ncnn_layer_t layer)
1174{
1175 return (int)((const Layer*)layer->pthis)->bottoms.size();
1176}
1177
1178int ncnn_layer_get_bottom(const ncnn_layer_t layer, int i)
1179{
1180 return ((const Layer*)layer->pthis)->bottoms[i];
1181}
1182
1183int ncnn_layer_get_top_count(const ncnn_layer_t layer)
1184{
1185 return (int)((const Layer*)layer->pthis)->tops.size();
1186}
1187
1188int ncnn_layer_get_top(const ncnn_layer_t layer, int i)
1189{
1190 return ((const Layer*)layer->pthis)->tops[i];
1191}
1192
1193void ncnn_blob_get_bottom_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c)
1194{
1195 const Mat& shape = ((const Layer*)layer->pthis)->bottom_shapes[i];
1196 *dims = shape.dims;
1197 *w = shape.w;
1198 *h = shape.h;
1199 *c = shape.c;
1200}
1201
1202void ncnn_blob_get_top_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c)
1203{
1204 const Mat& shape = ((const Layer*)layer->pthis)->top_shapes[i];
1205 *dims = shape.dims;
1206 *w = shape.w;
1207 *h = shape.h;
1208 *c = shape.c;
1209}
1210
1211/* net api */
1212ncnn_net_t ncnn_net_create()
1213{
1214 ncnn_net_t net = (ncnn_net_t)malloc(sizeof(struct __ncnn_net_t));
1215 net->pthis = (void*)(new Net());
1216 net->custom_layer_factory = 0;
1217 return net;
1218}
1219
1220void ncnn_net_destroy(ncnn_net_t net)
1221{
1222 delete (Net*)net->pthis;
1223 ncnn_net_custom_layer_factory_t ud = net->custom_layer_factory;
1224 while (ud)
1225 {
1226 ncnn_net_custom_layer_factory_t ud_next = ud->next;
1227 free(ud);
1228 ud = ud_next;
1229 }
1230 free(net);
1231}
1232
1233ncnn_option_t ncnn_net_get_option(ncnn_net_t net)
1234{
1235 return (ncnn_option_t)(&((Net*)(net->pthis))->opt);
1236}
1237
1238void ncnn_net_set_option(ncnn_net_t net, ncnn_option_t opt)
1239{
1240 ((Net*)net->pthis)->opt = *((Option*)opt);
1241}
1242
1243#if NCNN_VULKAN
1244void ncnn_net_set_vulkan_device(ncnn_net_t net, int device_index)
1245{
1246 ((Net*)net->pthis)->set_vulkan_device(device_index);
1247}
1248#endif
1249
1250static ::ncnn::Layer* __Layer_c_api_layer_creator(void* userdata)
1251{
1252 ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)userdata;
1253
1254 ncnn_layer_t layer0 = ud->creator(ud->userdata);
1255
1256 ::ncnn::Layer* layer = (::ncnn::Layer*)layer0->pthis;
1257
1258 layer->userdata = (void*)layer0;
1259
1260 layer->one_blob_only = ncnn_layer_get_one_blob_only(layer0);
1261 layer->support_inplace = ncnn_layer_get_support_inplace(layer0);
1262 layer->support_vulkan = ncnn_layer_get_support_vulkan(layer0);
1263 layer->support_packing = ncnn_layer_get_support_packing(layer0);
1264
1265 layer->support_bf16_storage = ncnn_layer_get_support_bf16_storage(layer0);
1266 layer->support_fp16_storage = ncnn_layer_get_support_fp16_storage(layer0);
1267 layer->support_image_storage = ncnn_layer_get_support_image_storage(layer0);
1268
1269 return layer;
1270}
1271
1272static void __Layer_c_api_layer_destroyer(::ncnn::Layer* layer, void* userdata)
1273{
1274 ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)userdata;
1275
1276 ncnn_layer_t layer0 = (ncnn_layer_t)layer->userdata;
1277
1278 ud->destroyer(layer0, ud->userdata);
1279}
1280
1281#if NCNN_STRING
1282void 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)
1283{
1284 ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)malloc(sizeof(struct __ncnn_net_custom_layer_factory_t));
1285 ud->creator = creator;
1286 ud->destroyer = destroyer;
1287 ud->userdata = userdata;
1288 ud->next = net->custom_layer_factory;
1289 net->custom_layer_factory = ud;
1290 ((Net*)net->pthis)->register_custom_layer(type, __Layer_c_api_layer_creator, __Layer_c_api_layer_destroyer, (void*)ud);
1291}
1292#endif /* NCNN_STRING */
1293
1294void 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)
1295{
1296 ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)malloc(sizeof(struct __ncnn_net_custom_layer_factory_t));
1297 ud->creator = creator;
1298 ud->destroyer = destroyer;
1299 ud->userdata = userdata;
1300 ud->next = net->custom_layer_factory;
1301 net->custom_layer_factory = ud;
1302 ((Net*)net->pthis)->register_custom_layer(typeindex, __Layer_c_api_layer_creator, __Layer_c_api_layer_destroyer, (void*)ud);
1303}
1304
1305#if NCNN_STDIO
1306#if NCNN_STRING
1307int ncnn_net_load_param(ncnn_net_t net, const char* path)
1308{
1309 return ((Net*)net->pthis)->load_param(path);
1310}
1311#endif /* NCNN_STRING */
1312
1313int ncnn_net_load_param_bin(ncnn_net_t net, const char* path)
1314{
1315 return ((Net*)net->pthis)->load_param_bin(path);
1316}
1317
1318int ncnn_net_load_model(ncnn_net_t net, const char* path)
1319{
1320 return ((Net*)net->pthis)->load_model(path);
1321}
1322#endif /* NCNN_STDIO */
1323
1324#if NCNN_STDIO
1325#if NCNN_STRING
1326int ncnn_net_load_param_memory(ncnn_net_t net, const char* mem)
1327{
1328 return ((Net*)net->pthis)->load_param_mem(mem);
1329}
1330#endif /* NCNN_STRING */
1331#endif /* NCNN_STDIO */
1332
1333int ncnn_net_load_param_bin_memory(ncnn_net_t net, const unsigned char* mem)
1334{
1335 return ((Net*)net->pthis)->load_param(mem);
1336}
1337
1338int ncnn_net_load_model_memory(ncnn_net_t net, const unsigned char* mem)
1339{
1340 return ((Net*)net->pthis)->load_model(mem);
1341}
1342
1343#if NCNN_STRING
1344int ncnn_net_load_param_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
1345{
1346 return ((Net*)net->pthis)->load_param(*(const DataReader*)dr->pthis);
1347}
1348#endif /* NCNN_STRING */
1349
1350int ncnn_net_load_param_bin_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
1351{
1352 return ((Net*)net->pthis)->load_param_bin(*(const DataReader*)dr->pthis);
1353}
1354
1355int ncnn_net_load_model_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
1356{
1357 return ((Net*)net->pthis)->load_model(*(const DataReader*)dr->pthis);
1358}
1359
1360void ncnn_net_clear(ncnn_net_t net)
1361{
1362 return ((Net*)net->pthis)->clear();
1363}
1364
1365int ncnn_net_get_input_count(const ncnn_net_t net)
1366{
1367 return (int)((Net*)net->pthis)->input_indexes().size();
1368}
1369
1370int ncnn_net_get_output_count(const ncnn_net_t net)
1371{
1372 return (int)((Net*)net->pthis)->output_indexes().size();
1373}
1374
1375#if NCNN_STRING
1376const char* ncnn_net_get_input_name(const ncnn_net_t net, int i)
1377{
1378 return ((Net*)net->pthis)->input_names()[i];
1379}
1380
1381const char* ncnn_net_get_output_name(const ncnn_net_t net, int i)
1382{
1383 return ((Net*)net->pthis)->output_names()[i];
1384}
1385#endif /* NCNN_STRING */
1386
1387int ncnn_net_get_input_index(const ncnn_net_t net, int i)
1388{
1389 return ((Net*)net->pthis)->input_indexes()[i];
1390}
1391
1392int ncnn_net_get_output_index(const ncnn_net_t net, int i)
1393{
1394 return ((Net*)net->pthis)->output_indexes()[i];
1395}
1396
1397/* extractor api */
1398ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net)
1399{
1400 return (ncnn_extractor_t)(new Extractor(((Net*)net->pthis)->create_extractor()));
1401}
1402
1403void ncnn_extractor_destroy(ncnn_extractor_t ex)
1404{
1405 delete (Extractor*)ex;
1406}
1407
1408void ncnn_extractor_set_option(ncnn_extractor_t ex, const ncnn_option_t opt)
1409{
1410 ((Extractor*)ex)->set_num_threads(((const Option*)opt)->num_threads);
1411#if NCNN_VULKAN
1412 ((Extractor*)ex)->set_vulkan_compute(((const Option*)opt)->use_vulkan_compute);
1413#endif
1414}
1415
1416#if NCNN_STRING
1417int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat)
1418{
1419 return ((Extractor*)ex)->input(name, *((const Mat*)mat));
1420}
1421
1422int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat)
1423{
1424 Mat mat0;
1425 int ret = ((Extractor*)ex)->extract(name, mat0);
1426 *mat = (ncnn_mat_t)(new Mat(mat0));
1427 return ret;
1428}
1429#endif /* NCNN_STRING */
1430
1431int ncnn_extractor_input_index(ncnn_extractor_t ex, int index, const ncnn_mat_t mat)
1432{
1433 return ((Extractor*)ex)->input(index, *((const Mat*)mat));
1434}
1435
1436int ncnn_extractor_extract_index(ncnn_extractor_t ex, int index, ncnn_mat_t* mat)
1437{
1438 Mat mat0;
1439 int ret = ((Extractor*)ex)->extract(index, mat0);
1440 *mat = (ncnn_mat_t)(new Mat(mat0));
1441 return ret;
1442}
1443
1444void ncnn_copy_make_border(const ncnn_mat_t src, ncnn_mat_t dst, int top, int bottom, int left, int right, int type, float v, const ncnn_option_t opt)
1445{
1446 const Option _opt = opt ? *((const Option*)opt) : Option();
1447 copy_make_border(*(const Mat*)src, *(Mat*)dst, top, bottom, left, right, type, v, _opt);
1448}
1449
1450void ncnn_copy_make_border_3d(const ncnn_mat_t src, ncnn_mat_t dst, int top, int bottom, int left, int right, int front, int behind, int type, float v, const ncnn_option_t opt)
1451{
1452 const Option _opt = opt ? *((const Option*)opt) : Option();
1453 copy_make_border_3d(*(const Mat*)src, *(Mat*)dst, top, bottom, left, right, front, behind, type, v, _opt);
1454}
1455
1456void ncnn_copy_cut_border(const ncnn_mat_t src, ncnn_mat_t dst, int top, int bottom, int left, int right, const ncnn_option_t opt)
1457{
1458 const Option _opt = opt ? *((const Option*)opt) : Option();
1459 copy_cut_border(*(const Mat*)src, *(Mat*)dst, top, bottom, left, right, _opt);
1460}
1461
1462void ncnn_copy_cut_border_3d(const ncnn_mat_t src, ncnn_mat_t dst, int top, int bottom, int left, int right, int front, int behind, const ncnn_option_t opt)
1463{
1464 const Option _opt = opt ? *((const Option*)opt) : Option();
1465 copy_cut_border_3d(*(const Mat*)src, *(Mat*)dst, top, bottom, left, right, front, behind, _opt);
1466}
1467
1468#if NCNN_PIXEL_DRAWING
1469void ncnn_draw_rectangle_c1(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
1470{
1471 ncnn::draw_rectangle_c1(pixels, w, h, w, rx, ry, rw, rh, color, thickness);
1472}
1473
1474void ncnn_draw_rectangle_c2(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
1475{
1476 ncnn::draw_rectangle_c2(pixels, w, h, w * 2, rx, ry, rw, rh, color, thickness);
1477}
1478
1479void ncnn_draw_rectangle_c3(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
1480{
1481 ncnn::draw_rectangle_c3(pixels, w, h, w * 3, rx, ry, rw, rh, color, thickness);
1482}
1483
1484void ncnn_draw_rectangle_c4(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
1485{
1486 ncnn::draw_rectangle_c4(pixels, w, h, w * 4, rx, ry, rw, rh, color, thickness);
1487}
1488
1489void ncnn_draw_text_c1(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1490{
1491 ncnn::draw_text_c1(pixels, w, h, w, text, x, y, fontpixelsize, color);
1492}
1493
1494void ncnn_draw_text_c2(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1495{
1496 ncnn::draw_text_c2(pixels, w, h, w * 2, text, x, y, fontpixelsize, color);
1497}
1498
1499void ncnn_draw_text_c3(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1500{
1501 ncnn::draw_text_c3(pixels, w, h, w * 3, text, x, y, fontpixelsize, color);
1502}
1503
1504void ncnn_draw_text_c4(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1505{
1506 ncnn::draw_text_c4(pixels, w, h, w * 4, text, x, y, fontpixelsize, color);
1507}
1508
1509void ncnn_draw_circle_c1(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
1510{
1511 ncnn::draw_circle_c1(pixels, w, h, w, cx, cy, radius, color, thickness);
1512}
1513
1514void ncnn_draw_circle_c2(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
1515{
1516 ncnn::draw_circle_c2(pixels, w, h, w * 2, cx, cy, radius, color, thickness);
1517}
1518
1519void ncnn_draw_circle_c3(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
1520{
1521 ncnn::draw_circle_c3(pixels, w, h, w * 3, cx, cy, radius, color, thickness);
1522}
1523
1524void ncnn_draw_circle_c4(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
1525{
1526 ncnn::draw_circle_c4(pixels, w, h, w * 4, cx, cy, radius, color, thickness);
1527}
1528
1529void ncnn_draw_line_c1(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1530{
1531 ncnn::draw_line_c1(pixels, w, h, w, x0, y0, x1, y1, color, thickness);
1532}
1533
1534void ncnn_draw_line_c2(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1535{
1536 ncnn::draw_line_c2(pixels, w, h, w * 2, x0, y0, x1, y1, color, thickness);
1537}
1538
1539void ncnn_draw_line_c3(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1540{
1541 ncnn::draw_line_c3(pixels, w, h, w * 3, x0, y0, x1, y1, color, thickness);
1542}
1543
1544void ncnn_draw_line_c4(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1545{
1546 ncnn::draw_line_c4(pixels, w, h, w * 4, x0, y0, x1, y1, color, thickness);
1547}
1548#endif /* NCNN_PIXEL_DRAWING */
1549
1550#ifdef __cplusplus
1551} /* extern "C" */
1552#endif
1553
1554#endif /* NCNN_C_API */