c_api.cpp 41.7 KB
Newer Older
N
nihui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Tencent is pleased to support the open source community by making ncnn available.
 *
 * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

N
nihui 已提交
16 17 18 19
#include "platform.h"

#if NCNN_C_API

N
nihui 已提交
20 21
#include "c_api.h"

22 23
#include <stdlib.h>

N
nihui 已提交
24
#include "allocator.h"
N
nihuini 已提交
25 26 27
#include "blob.h"
#include "datareader.h"
#include "layer.h"
N
nihui 已提交
28
#include "mat.h"
N
nihuini 已提交
29
#include "modelbin.h"
N
nihui 已提交
30
#include "net.h"
N
nihuini 已提交
31 32
#include "option.h"
#include "paramdict.h"
N
nihui 已提交
33

N
nihui 已提交
34
using ncnn::Allocator;
N
nihui 已提交
35
using ncnn::Blob;
N
nihuini 已提交
36
using ncnn::DataReader;
N
nihui 已提交
37 38 39
using ncnn::Extractor;
using ncnn::Layer;
using ncnn::Mat;
N
nihuini 已提交
40
using ncnn::ModelBin;
N
nihui 已提交
41 42
using ncnn::Net;
using ncnn::Option;
N
nihuini 已提交
43
using ncnn::ParamDict;
N
nihui 已提交
44 45 46 47 48

#ifdef __cplusplus
extern "C" {
#endif

N
nihui 已提交
49 50 51 52 53
const char* ncnn_version()
{
    return NCNN_VERSION_STRING;
}

N
nihui 已提交
54
/* allocator api */
55 56 57
class PoolAllocator_c_api : public ncnn::PoolAllocator
{
public:
N
nihuini 已提交
58 59
    PoolAllocator_c_api(ncnn_allocator_t _allocator)
        : ncnn::PoolAllocator()
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    {
        allocator = _allocator;
    }

    virtual void* fastMalloc(size_t size)
    {
        return allocator->fast_malloc(allocator, size);
    }

    virtual void fastFree(void* ptr)
    {
        return allocator->fast_free(allocator, ptr);
    }

public:
    ncnn_allocator_t allocator;
};

static void* __ncnn_PoolAllocator_fast_malloc(ncnn_allocator_t allocator, size_t size)
N
nihui 已提交
79
{
80
    return ((ncnn::PoolAllocator*)allocator->pthis)->ncnn::PoolAllocator::fastMalloc(size);
N
nihui 已提交
81 82
}

83
static void __ncnn_PoolAllocator_fast_free(ncnn_allocator_t allocator, void* ptr)
N
nihui 已提交
84
{
85
    ((ncnn::PoolAllocator*)allocator->pthis)->ncnn::PoolAllocator::fastFree(ptr);
N
nihui 已提交
86 87
}

88 89 90
class UnlockedPoolAllocator_c_api : public ncnn::UnlockedPoolAllocator
{
public:
N
nihuini 已提交
91 92
    UnlockedPoolAllocator_c_api(ncnn_allocator_t _allocator)
        : ncnn::UnlockedPoolAllocator()
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    {
        allocator = _allocator;
    }

    virtual void* fastMalloc(size_t size)
    {
        return allocator->fast_malloc(allocator, size);
    }

    virtual void fastFree(void* ptr)
    {
        return allocator->fast_free(allocator, ptr);
    }

public:
    ncnn_allocator_t allocator;
};

static void* __ncnn_UnlockedPoolAllocator_fast_malloc(ncnn_allocator_t allocator, size_t size)
{
    return ((ncnn::UnlockedPoolAllocator*)allocator->pthis)->ncnn::UnlockedPoolAllocator::fastMalloc(size);
}

static void __ncnn_UnlockedPoolAllocator_fast_free(ncnn_allocator_t allocator, void* ptr)
{
    ((ncnn::UnlockedPoolAllocator*)allocator->pthis)->ncnn::UnlockedPoolAllocator::fastFree(ptr);
}

ncnn_allocator_t ncnn_allocator_create_pool_allocator()
N
nihui 已提交
122
{
123 124 125 126 127
    ncnn_allocator_t allocator = (ncnn_allocator_t)malloc(sizeof(struct __ncnn_allocator_t));
    allocator->pthis = (void*)(new PoolAllocator_c_api(allocator));
    allocator->fast_malloc = __ncnn_PoolAllocator_fast_malloc;
    allocator->fast_free = __ncnn_PoolAllocator_fast_free;
    return allocator;
N
nihui 已提交
128 129
}

130
ncnn_allocator_t ncnn_allocator_create_unlocked_pool_allocator()
N
nihui 已提交
131
{
132 133 134 135 136
    ncnn_allocator_t allocator = (ncnn_allocator_t)malloc(sizeof(struct __ncnn_allocator_t));
    allocator->pthis = (void*)(new UnlockedPoolAllocator_c_api(allocator));
    allocator->fast_malloc = __ncnn_UnlockedPoolAllocator_fast_malloc;
    allocator->fast_free = __ncnn_UnlockedPoolAllocator_fast_free;
    return allocator;
N
nihui 已提交
137 138
}

139
void ncnn_allocator_destroy(ncnn_allocator_t allocator)
N
nihui 已提交
140
{
N
nihui 已提交
141 142 143 144 145
    if (allocator)
    {
        delete (Allocator*)allocator->pthis;
        free(allocator);
    }
N
nihui 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

/* option api */
ncnn_option_t ncnn_option_create()
{
    return (ncnn_option_t)(new Option());
}

void ncnn_option_destroy(ncnn_option_t opt)
{
    delete (Option*)opt;
}

int ncnn_option_get_num_threads(const ncnn_option_t opt)
{
    return ((const Option*)opt)->num_threads;
}

void ncnn_option_set_num_threads(ncnn_option_t opt, int num_threads)
{
    ((Option*)opt)->num_threads = num_threads;
}

N
nihui 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
int ncnn_option_get_use_local_pool_allocator(const ncnn_option_t opt)
{
    return ((Option*)opt)->use_local_pool_allocator;
}

void ncnn_option_set_use_local_pool_allocator(ncnn_option_t opt, int use_local_pool_allocator)
{
    ((Option*)opt)->use_local_pool_allocator = use_local_pool_allocator;
}

void ncnn_option_set_blob_allocator(ncnn_option_t opt, ncnn_allocator_t allocator)
{
    ((Option*)opt)->blob_allocator = allocator ? (Allocator*)allocator->pthis : NULL;
}

void ncnn_option_set_workspace_allocator(ncnn_option_t opt, ncnn_allocator_t allocator)
{
    ((Option*)opt)->workspace_allocator = allocator ? (Allocator*)allocator->pthis : NULL;
}

N
nihui 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
int ncnn_option_get_use_vulkan_compute(const ncnn_option_t opt)
{
#if NCNN_VULKAN
    return ((const Option*)opt)->use_vulkan_compute;
#else
    (void)opt;
    return 0;
#endif
}

void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_compute)
{
#if NCNN_VULKAN
    ((Option*)opt)->use_vulkan_compute = use_vulkan_compute;
#else
    (void)opt;
    (void)use_vulkan_compute;
#endif
}

N
nihui 已提交
209
/* mat api */
210 211 212 213 214
ncnn_mat_t ncnn_mat_create()
{
    return (ncnn_mat_t)(new Mat());
}

N
nihui 已提交
215
ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator)
N
nihui 已提交
216
{
N
nihui 已提交
217
    return (ncnn_mat_t)(new Mat(w, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
218 219
}

N
nihui 已提交
220
ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator)
N
nihui 已提交
221
{
N
nihui 已提交
222
    return (ncnn_mat_t)(new Mat(w, h, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
223 224
}

N
nihui 已提交
225
ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator)
N
nihui 已提交
226
{
N
nihui 已提交
227
    return (ncnn_mat_t)(new Mat(w, h, c, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
228 229 230 231
}

ncnn_mat_t ncnn_mat_create_4d(int w, int h, int d, int c, ncnn_allocator_t allocator)
{
N
nihui 已提交
232
    return (ncnn_mat_t)(new Mat(w, h, d, c, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
233 234
}

N
nihui 已提交
235
ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator)
N
nihui 已提交
236
{
N
nihui 已提交
237
    return (ncnn_mat_t)(new Mat(w, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
238 239
}

N
nihui 已提交
240
ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data, ncnn_allocator_t allocator)
N
nihui 已提交
241
{
N
nihui 已提交
242
    return (ncnn_mat_t)(new Mat(w, h, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
243 244
}

N
nihui 已提交
245
ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data, ncnn_allocator_t allocator)
N
nihui 已提交
246
{
N
nihui 已提交
247
    return (ncnn_mat_t)(new Mat(w, h, c, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
248 249 250 251
}

ncnn_mat_t ncnn_mat_create_external_4d(int w, int h, int d, int c, void* data, ncnn_allocator_t allocator)
{
N
nihui 已提交
252
    return (ncnn_mat_t)(new Mat(w, h, d, c, data, (size_t)4u, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
253 254
}

N
nihui 已提交
255
ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack, ncnn_allocator_t allocator)
N
nihuini 已提交
256
{
N
nihui 已提交
257
    return (ncnn_mat_t)(new Mat(w, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihuini 已提交
258 259
}

N
nihui 已提交
260
ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack, ncnn_allocator_t allocator)
N
nihuini 已提交
261
{
N
nihui 已提交
262
    return (ncnn_mat_t)(new Mat(w, h, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihuini 已提交
263 264
}

N
nihui 已提交
265
ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator)
N
nihuini 已提交
266
{
N
nihui 已提交
267
    return (ncnn_mat_t)(new Mat(w, h, c, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihuini 已提交
268 269
}

N
nihui 已提交
270 271
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)
{
N
nihui 已提交
272
    return (ncnn_mat_t)(new Mat(w, h, d, c, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
273 274
}

N
nihui 已提交
275
ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
N
nihuini 已提交
276
{
N
nihui 已提交
277
    return (ncnn_mat_t)(new Mat(w, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihuini 已提交
278 279
}

N
nihui 已提交
280
ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator)
N
nihuini 已提交
281
{
N
nihui 已提交
282
    return (ncnn_mat_t)(new Mat(w, h, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihuini 已提交
283 284
}

N
nihui 已提交
285
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)
N
nihuini 已提交
286
{
N
nihui 已提交
287
    return (ncnn_mat_t)(new Mat(w, h, c, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihuini 已提交
288 289
}

N
nihui 已提交
290 291
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)
{
N
nihui 已提交
292
    return (ncnn_mat_t)(new Mat(w, h, d, c, data, elemsize, elempack, allocator ? (Allocator*)allocator->pthis : NULL));
N
nihui 已提交
293 294
}

N
nihui 已提交
295 296 297 298 299
void ncnn_mat_destroy(ncnn_mat_t mat)
{
    delete (Mat*)mat;
}

N
nihuini 已提交
300 301 302 303 304
void ncnn_mat_fill_float(ncnn_mat_t mat, float v)
{
    ((Mat*)mat)->fill(v);
}

N
nihui 已提交
305
ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat, ncnn_allocator_t allocator)
N
nihuini 已提交
306
{
N
nihui 已提交
307
    return (ncnn_mat_t)(new Mat(((const Mat*)mat)->clone(allocator ? (Allocator*)allocator->pthis : NULL)));
N
nihuini 已提交
308 309
}

N
nihui 已提交
310
ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w, ncnn_allocator_t allocator)
N
nihuini 已提交
311
{
N
nihui 已提交
312
    return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, allocator ? (Allocator*)allocator->pthis : NULL)));
N
nihuini 已提交
313 314
}

N
nihui 已提交
315
ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h, ncnn_allocator_t allocator)
N
nihuini 已提交
316
{
N
nihui 已提交
317
    return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, allocator ? (Allocator*)allocator->pthis : NULL)));
N
nihuini 已提交
318 319
}

N
nihui 已提交
320
ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c, ncnn_allocator_t allocator)
N
nihuini 已提交
321
{
N
nihui 已提交
322
    return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, c, allocator ? (Allocator*)allocator->pthis : NULL)));
N
nihuini 已提交
323 324
}

N
nihui 已提交
325 326
ncnn_mat_t ncnn_mat_reshape_4d(const ncnn_mat_t mat, int w, int h, int d, int c, ncnn_allocator_t allocator)
{
N
nihui 已提交
327
    return (ncnn_mat_t)(new Mat(((const Mat*)mat)->reshape(w, h, d, c, allocator ? (Allocator*)allocator->pthis : NULL)));
N
nihui 已提交
328 329
}

N
nihuini 已提交
330
int ncnn_mat_get_dims(const ncnn_mat_t mat)
N
nihui 已提交
331
{
N
nihuini 已提交
332
    return ((const Mat*)mat)->dims;
N
nihui 已提交
333 334
}

N
nihuini 已提交
335
int ncnn_mat_get_w(const ncnn_mat_t mat)
N
nihui 已提交
336
{
N
nihuini 已提交
337
    return ((const Mat*)mat)->w;
N
nihui 已提交
338 339
}

N
nihuini 已提交
340
int ncnn_mat_get_h(const ncnn_mat_t mat)
N
nihui 已提交
341
{
N
nihuini 已提交
342
    return ((const Mat*)mat)->h;
N
nihui 已提交
343 344
}

N
nihui 已提交
345 346 347 348 349
int ncnn_mat_get_d(const ncnn_mat_t mat)
{
    return ((const Mat*)mat)->d;
}

N
nihuini 已提交
350
int ncnn_mat_get_c(const ncnn_mat_t mat)
N
nihui 已提交
351
{
N
nihuini 已提交
352
    return ((const Mat*)mat)->c;
N
nihui 已提交
353 354
}

N
nihuini 已提交
355
size_t ncnn_mat_get_elemsize(const ncnn_mat_t mat)
N
nihui 已提交
356
{
N
nihuini 已提交
357
    return ((const Mat*)mat)->elemsize;
N
nihui 已提交
358 359
}

N
nihuini 已提交
360
int ncnn_mat_get_elempack(const ncnn_mat_t mat)
N
nihui 已提交
361
{
N
nihuini 已提交
362
    return ((const Mat*)mat)->elempack;
N
nihui 已提交
363 364
}

N
nihuini 已提交
365
size_t ncnn_mat_get_cstep(const ncnn_mat_t mat)
N
nihui 已提交
366
{
N
nihuini 已提交
367
    return ((const Mat*)mat)->cstep;
N
nihui 已提交
368 369
}

N
nihuini 已提交
370
void* ncnn_mat_get_data(const ncnn_mat_t mat)
N
nihui 已提交
371
{
N
nihuini 已提交
372
    return ((const Mat*)mat)->data;
N
nihui 已提交
373 374
}

375 376 377 378 379
void* ncnn_mat_get_channel_data(const ncnn_mat_t mat, int c)
{
    return ((const Mat*)mat)->channel(c).data;
}

380 381
#if NCNN_PIXEL

N
nihui 已提交
382
/* mat pixel api */
N
nihui 已提交
383
ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, ncnn_allocator_t allocator)
N
nihui 已提交
384
{
N
nihui 已提交
385
    return (ncnn_mat_t)(new Mat(Mat::from_pixels(pixels, type, w, h, stride, allocator ? (Allocator*)allocator->pthis : NULL)));
N
nihui 已提交
386 387
}

N
nihui 已提交
388
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)
N
nihui 已提交
389
{
N
nihui 已提交
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)));
N
nihui 已提交
391 392
}

N
nihui 已提交
393
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)
N
nihui 已提交
394
{
N
nihui 已提交
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)));
N
nihui 已提交
396 397
}

N
nihui 已提交
398
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)
N
nihui 已提交
399
{
N
nihui 已提交
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)));
N
nihui 已提交
401 402
}

N
nihuini 已提交
403
void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride)
N
nihui 已提交
404
{
N
nihuini 已提交
405
    ((const Mat*)mat)->to_pixels(pixels, type, stride);
N
nihui 已提交
406 407
}

N
nihuini 已提交
408
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)
N
nihui 已提交
409
{
N
nihuini 已提交
410
    ((const Mat*)mat)->to_pixels_resize(pixels, type, target_width, target_height, target_stride);
N
nihui 已提交
411 412
}

N
nihui 已提交
413
#endif /* NCNN_PIXEL */
414

N
nihui 已提交
415 416 417 418 419
void ncnn_mat_substract_mean_normalize(ncnn_mat_t mat, const float* mean_vals, const float* norm_vals)
{
    ((Mat*)mat)->substract_mean_normalize(mean_vals, norm_vals);
}

N
nihui 已提交
420
void ncnn_convert_packing(const ncnn_mat_t src, ncnn_mat_t* dst, int elempack, const ncnn_option_t opt)
N
nihuini 已提交
421
{
N
nihuini 已提交
422
    Mat _dst;
N
nihui 已提交
423
    ncnn::convert_packing(*(const Mat*)src, _dst, elempack, *(Option*)opt);
N
nihuini 已提交
424 425 426
    *dst = (ncnn_mat_t)(new Mat(_dst));
}

N
nihui 已提交
427
void ncnn_flatten(const ncnn_mat_t src, ncnn_mat_t* dst, const ncnn_option_t opt)
N
nihuini 已提交
428 429
{
    Mat _dst;
N
nihui 已提交
430
    ncnn::flatten(*(const Mat*)src, _dst, *(Option*)opt);
N
nihuini 已提交
431
    *dst = (ncnn_mat_t)(new Mat(_dst));
N
nihuini 已提交
432 433
}

N
nihui 已提交
434
/* blob api */
N
nihui 已提交
435
#if NCNN_STRING
N
nihuini 已提交
436
const char* ncnn_blob_get_name(const ncnn_blob_t blob)
N
nihui 已提交
437
{
N
nihuini 已提交
438
    return ((const Blob*)blob)->name.c_str();
N
nihui 已提交
439
}
N
nihui 已提交
440
#endif /* NCNN_STRING */
N
nihui 已提交
441

N
nihuini 已提交
442
int ncnn_blob_get_producer(const ncnn_blob_t blob)
N
nihui 已提交
443
{
N
nihuini 已提交
444
    return ((const Blob*)blob)->producer;
N
nihui 已提交
445 446
}

N
nihuini 已提交
447
int ncnn_blob_get_consumer(const ncnn_blob_t blob)
N
nihui 已提交
448
{
N
nihuini 已提交
449
    return ((const Blob*)blob)->consumer;
N
nihui 已提交
450 451
}

N
nihuini 已提交
452
void ncnn_blob_get_shape(const ncnn_blob_t blob, int* dims, int* w, int* h, int* c)
N
nihui 已提交
453
{
N
nihuini 已提交
454
    const Mat& shape = ((const Blob*)blob)->shape;
N
nihui 已提交
455 456 457 458 459 460
    *dims = shape.dims;
    *w = shape.w;
    *h = shape.h;
    *c = shape.c;
}

N
nihuini 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
/* paramdict api */
ncnn_paramdict_t ncnn_paramdict_create()
{
    return (ncnn_paramdict_t)(new ParamDict());
}

void ncnn_paramdict_destroy(ncnn_paramdict_t pd)
{
    delete (ParamDict*)pd;
}

int ncnn_paramdict_get_type(const ncnn_paramdict_t pd, int id)
{
    return ((const ParamDict*)pd)->type(id);
}

int ncnn_paramdict_get_int(const ncnn_paramdict_t pd, int id, int def)
{
    return ((const ParamDict*)pd)->get(id, def);
}

N
nihui 已提交
482
float ncnn_paramdict_get_float(const ncnn_paramdict_t pd, int id, float def)
N
nihuini 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
{
    return ((const ParamDict*)pd)->get(id, def);
}

ncnn_mat_t ncnn_paramdict_get_array(ncnn_paramdict_t pd, int id, const ncnn_mat_t def)
{
    return (ncnn_mat_t)(new Mat(((const ParamDict*)pd)->get(id, *(const Mat*)def)));
}

void ncnn_paramdict_set_int(ncnn_paramdict_t pd, int id, int i)
{
    return ((ParamDict*)pd)->set(id, i);
}

void ncnn_paramdict_set_float(ncnn_paramdict_t pd, int id, float f)
{
    return ((ParamDict*)pd)->set(id, f);
}

void ncnn_paramdict_set_array(ncnn_paramdict_t pd, int id, ncnn_mat_t v)
{
    return ((ParamDict*)pd)->set(id, *(const Mat*)v);
}

/* datareader api */
508 509 510
class DataReader_c_api : public ncnn::DataReader
{
public:
N
nihuini 已提交
511 512
    DataReader_c_api(ncnn_datareader_t _dr)
        : ncnn::DataReader()
513 514 515 516 517 518 519 520 521
    {
        dr = _dr;
    }

#if NCNN_STRING
    virtual int scan(const char* format, void* p) const
    {
        return dr->scan(dr, format, p);
    }
N
nihui 已提交
522
#endif /* NCNN_STRING */
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

    virtual size_t read(void* buf, size_t size) const
    {
        return dr->read(dr, buf, size);
    }

public:
    ncnn_datareader_t dr;
};

#if NCNN_STRING
static int __ncnn_DataReader_scan(ncnn_datareader_t dr, const char* format, void* p)
{
    return ((ncnn::DataReader*)dr->pthis)->ncnn::DataReader::scan(format, p);
}
N
nihui 已提交
538
#endif /* NCNN_STRING */
539 540 541 542 543 544

static size_t __ncnn_DataReader_read(ncnn_datareader_t dr, void* buf, size_t size)
{
    return ((ncnn::DataReader*)dr->pthis)->ncnn::DataReader::read(buf, size);
}

N
nihui 已提交
545
#if NCNN_STDIO
546 547 548
class DataReaderFromStdio_c_api : public ncnn::DataReaderFromStdio
{
public:
N
nihuini 已提交
549 550
    DataReaderFromStdio_c_api(FILE* fp, ncnn_datareader_t _dr)
        : ncnn::DataReaderFromStdio(fp)
551 552 553 554 555 556 557 558 559
    {
        dr = _dr;
    }

#if NCNN_STRING
    virtual int scan(const char* format, void* p) const
    {
        return dr->scan(dr, format, p);
    }
N
nihui 已提交
560
#endif /* NCNN_STRING */
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

    virtual size_t read(void* buf, size_t size) const
    {
        return dr->read(dr, buf, size);
    }

public:
    ncnn_datareader_t dr;
};

#if NCNN_STRING
static int __ncnn_DataReaderFromStdio_scan(ncnn_datareader_t dr, const char* format, void* p)
{
    return ((ncnn::DataReaderFromStdio*)dr->pthis)->ncnn::DataReaderFromStdio::scan(format, p);
}
N
nihui 已提交
576
#endif /* NCNN_STRING */
577 578 579 580 581

static size_t __ncnn_DataReaderFromStdio_read(ncnn_datareader_t dr, void* buf, size_t size)
{
    return ((ncnn::DataReaderFromStdio*)dr->pthis)->ncnn::DataReaderFromStdio::read(buf, size);
}
N
nihui 已提交
582
#endif /* NCNN_STDIO */
583 584 585 586

class DataReaderFromMemory_c_api : public ncnn::DataReaderFromMemory
{
public:
N
nihuini 已提交
587 588
    DataReaderFromMemory_c_api(const unsigned char*& mem, ncnn_datareader_t _dr)
        : ncnn::DataReaderFromMemory(mem)
589 590 591 592 593 594 595 596 597
    {
        dr = _dr;
    }

#if NCNN_STRING
    virtual int scan(const char* format, void* p) const
    {
        return dr->scan(dr, format, p);
    }
N
nihui 已提交
598
#endif /* NCNN_STRING */
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

    virtual size_t read(void* buf, size_t size) const
    {
        return dr->read(dr, buf, size);
    }

public:
    ncnn_datareader_t dr;
};

#if NCNN_STRING
static int __ncnn_DataReaderFromMemory_scan(ncnn_datareader_t dr, const char* format, void* p)
{
    return ((ncnn::DataReaderFromMemory*)dr->pthis)->ncnn::DataReaderFromMemory::scan(format, p);
}
N
nihui 已提交
614
#endif /* NCNN_STRING */
615 616 617 618 619 620 621 622 623 624

static size_t __ncnn_DataReaderFromMemory_read(ncnn_datareader_t dr, void* buf, size_t size)
{
    return ((ncnn::DataReaderFromMemory*)dr->pthis)->ncnn::DataReaderFromMemory::read(buf, size);
}

ncnn_datareader_t ncnn_datareader_create()
{
    ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
    dr->pthis = (void*)(new DataReader_c_api(dr));
N
nihui 已提交
625
#if NCNN_STRING
626
    dr->scan = __ncnn_DataReader_scan;
N
nihui 已提交
627
#endif /* NCNN_STRING */
628 629 630 631
    dr->read = __ncnn_DataReader_read;
    return dr;
}

N
nihuini 已提交
632
#if NCNN_STDIO
N
nihui 已提交
633
ncnn_datareader_t ncnn_datareader_create_from_stdio(FILE* fp)
N
nihuini 已提交
634
{
635 636 637 638
    ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
    dr->pthis = (void*)(new DataReaderFromStdio_c_api(fp, dr));
#if NCNN_STRING
    dr->scan = __ncnn_DataReaderFromStdio_scan;
N
nihui 已提交
639
#endif /* NCNN_STRING */
640 641
    dr->read = __ncnn_DataReaderFromStdio_read;
    return dr;
N
nihuini 已提交
642
}
N
nihui 已提交
643
#endif /* NCNN_STDIO */
N
nihuini 已提交
644

N
nihui 已提交
645
ncnn_datareader_t ncnn_datareader_create_from_memory(const unsigned char** mem)
N
nihuini 已提交
646
{
647 648 649 650
    ncnn_datareader_t dr = (ncnn_datareader_t)malloc(sizeof(struct __ncnn_datareader_t));
    dr->pthis = (void*)(new DataReaderFromMemory_c_api(*mem, dr));
#if NCNN_STRING
    dr->scan = __ncnn_DataReaderFromMemory_scan;
N
nihui 已提交
651
#endif /* NCNN_STRING */
652 653
    dr->read = __ncnn_DataReaderFromMemory_read;
    return dr;
N
nihuini 已提交
654 655 656 657
}

void ncnn_datareader_destroy(ncnn_datareader_t dr)
{
658 659
    delete (DataReader*)dr->pthis;
    free(dr);
N
nihuini 已提交
660 661 662
}

/* modelbin api */
663 664 665
class ModelBinFromDataReader_c_api : public ncnn::ModelBinFromDataReader
{
public:
N
nihuini 已提交
666 667
    ModelBinFromDataReader_c_api(ncnn_modelbin_t _mb, const DataReader& dr)
        : ncnn::ModelBinFromDataReader(dr)
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    {
        mb = _mb;
    }

    virtual Mat load(int w, int type) const
    {
        ncnn_mat_t m = mb->load_1d(mb, w, type);
        Mat m2 = *(Mat*)m;
        ncnn_mat_destroy(m);
        return m2;
    }

    virtual Mat load(int w, int h, int type) const
    {
        ncnn_mat_t m = mb->load_2d(mb, w, h, type);
        Mat m2 = *(Mat*)m;
        ncnn_mat_destroy(m);
        return m2;
    }

    virtual Mat load(int w, int h, int c, int type) const
    {
        ncnn_mat_t m = mb->load_3d(mb, w, h, c, type);
        Mat m2 = *(Mat*)m;
        ncnn_mat_destroy(m);
        return m2;
    }

public:
    ncnn_modelbin_t mb;
};

static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_1d(const ncnn_modelbin_t mb, int w, int type)
{
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBinFromDataReader::load(w, type)));
}

static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
{
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBin::load(w, h, type)));
}

static ncnn_mat_t __ncnn_ModelBinFromDataReader_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
{
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromDataReader*)mb->pthis)->ncnn::ModelBin::load(w, h, c, type)));
}

class ModelBinFromMatArray_c_api : public ncnn::ModelBinFromMatArray
{
public:
N
nihuini 已提交
718 719
    ModelBinFromMatArray_c_api(ncnn_modelbin_t _mb, const Mat* weights)
        : ncnn::ModelBinFromMatArray(weights)
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
    {
        mb = _mb;
    }

    virtual Mat load(int w, int type) const
    {
        ncnn_mat_t m = mb->load_1d(mb, w, type);
        Mat m2 = *(Mat*)m;
        ncnn_mat_destroy(m);
        return m2;
    }

    virtual Mat load(int w, int h, int type) const
    {
        ncnn_mat_t m = mb->load_2d(mb, w, h, type);
        Mat m2 = *(Mat*)m;
        ncnn_mat_destroy(m);
        return m2;
    }

    virtual Mat load(int w, int h, int c, int type) const
    {
        ncnn_mat_t m = mb->load_3d(mb, w, h, c, type);
        Mat m2 = *(Mat*)m;
        ncnn_mat_destroy(m);
        return m2;
    }

public:
    ncnn_modelbin_t mb;
};

static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_1d(const ncnn_modelbin_t mb, int w, int type)
{
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBinFromMatArray::load(w, type)));
}

static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
{
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBin::load(w, h, type)));
}

static ncnn_mat_t __ncnn_ModelBinFromMatArray_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
{
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBinFromMatArray*)mb->pthis)->ncnn::ModelBin::load(w, h, c, type)));
}

N
nihui 已提交
767
ncnn_modelbin_t ncnn_modelbin_create_from_datareader(const ncnn_datareader_t dr)
N
nihuini 已提交
768
{
769 770 771 772 773 774
    ncnn_modelbin_t mb = (ncnn_modelbin_t)malloc(sizeof(struct __ncnn_modelbin_t));
    mb->pthis = (void*)(new ModelBinFromDataReader_c_api(mb, *(const DataReader*)dr->pthis));
    mb->load_1d = __ncnn_ModelBinFromDataReader_load_1d;
    mb->load_2d = __ncnn_ModelBinFromDataReader_load_2d;
    mb->load_3d = __ncnn_ModelBinFromDataReader_load_3d;
    return mb;
N
nihuini 已提交
775 776
}

N
nihui 已提交
777
ncnn_modelbin_t ncnn_modelbin_create_from_mat_array(const ncnn_mat_t* weights, int n)
N
nihuini 已提交
778 779 780 781 782 783
{
    std::vector<Mat> matarray(n);
    for (int i = 0; i < n; i++)
    {
        matarray[i] = *(const Mat*)weights[i];
    }
784 785 786 787 788 789
    ncnn_modelbin_t mb = (ncnn_modelbin_t)malloc(sizeof(struct __ncnn_modelbin_t));
    mb->pthis = (void*)(new ModelBinFromMatArray_c_api(mb, &matarray[0]));
    mb->load_1d = __ncnn_ModelBinFromMatArray_load_1d;
    mb->load_2d = __ncnn_ModelBinFromMatArray_load_2d;
    mb->load_3d = __ncnn_ModelBinFromMatArray_load_3d;
    return mb;
N
nihuini 已提交
790 791 792 793
}

void ncnn_modelbin_destroy(ncnn_modelbin_t mb)
{
794 795
    delete (ModelBin*)mb->pthis;
    free(mb);
N
nihuini 已提交
796 797
}

798
static ncnn_mat_t __ncnn_modelbin_load_1d(const ncnn_modelbin_t mb, int w, int type)
N
nihuini 已提交
799
{
800
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, type)));
N
nihuini 已提交
801 802
}

803
static ncnn_mat_t __ncnn_modelbin_load_2d(const ncnn_modelbin_t mb, int w, int h, int type)
N
nihuini 已提交
804
{
805
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, h, type)));
N
nihuini 已提交
806 807
}

808
static ncnn_mat_t __ncnn_modelbin_load_3d(const ncnn_modelbin_t mb, int w, int h, int c, int type)
N
nihuini 已提交
809
{
810
    return (ncnn_mat_t)(new Mat(((const ncnn::ModelBin*)mb->pthis)->load(w, h, c, type)));
N
nihuini 已提交
811 812
}

N
nihui 已提交
813
/* layer api */
814 815 816
class Layer_c_api : public Layer
{
public:
N
nihuini 已提交
817 818
    Layer_c_api(ncnn_layer_t _layer)
        : Layer()
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
    {
        layer = _layer;
    }

    virtual int load_param(const ParamDict& pd)
    {
        return layer->load_param(layer, (ncnn_paramdict_t)&pd);
    }

    virtual int load_model(const ModelBin& mb)
    {
        struct __ncnn_modelbin_t mb0;
        mb0.pthis = (void*)&mb;
        mb0.load_1d = __ncnn_modelbin_load_1d;
        mb0.load_2d = __ncnn_modelbin_load_2d;
        mb0.load_3d = __ncnn_modelbin_load_3d;
        return layer->load_model(layer, &mb0);
    }

    virtual int create_pipeline(const Option& opt)
    {
        return layer->create_pipeline(layer, (ncnn_option_t)&opt);
    }

    virtual int destroy_pipeline(const Option& opt)
    {
        return layer->destroy_pipeline(layer, (ncnn_option_t)&opt);
    }

    virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
    {
        const int n = bottom_blobs.size();
        const int n2 = top_blobs.size();
        std::vector<ncnn_mat_t> bottom_blobs0(n);
        for (int i = 0; i < n; i++)
        {
            bottom_blobs0[i] = (ncnn_mat_t)&bottom_blobs[i];
        }
857
        std::vector<ncnn_mat_t> top_blobs0(n2, (ncnn_mat_t)0);
N
nihui 已提交
858
        int ret = layer->forward_n(layer, &bottom_blobs0[0], n, &top_blobs0[0], n2, (ncnn_option_t)&opt);
859 860
        for (int i = 0; i < n2; i++)
        {
861 862
            top_blobs[i] = *(Mat*)top_blobs0[i];
            ncnn_mat_destroy(top_blobs0[i]);
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
        }
        return ret;
    }

    virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
    {
        ncnn_mat_t top_blob0 = 0;
        int ret = layer->forward_1(layer, (ncnn_mat_t)&bottom_blob, &top_blob0, (ncnn_option_t)&opt);
        top_blob = *(Mat*)top_blob0;
        ncnn_mat_destroy(top_blob0);
        return ret;
    }

    virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const
    {
        const int n = bottom_top_blobs.size();
        std::vector<ncnn_mat_t> bottom_top_blobs0(n);
        for (int i = 0; i < n; i++)
        {
            bottom_top_blobs0[i] = (ncnn_mat_t)&bottom_top_blobs[i];
        }
N
nihui 已提交
884
        return layer->forward_inplace_n(layer, &bottom_top_blobs0[0], n, (ncnn_option_t)&opt);
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
    }

    virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const
    {
        return layer->forward_inplace_1(layer, (ncnn_mat_t)&bottom_top_blob, (ncnn_option_t)&opt);
    }

public:
    ncnn_layer_t layer;
};

static int __ncnn_Layer_load_param(ncnn_layer_t layer, const ncnn_paramdict_t pd)
{
    return ((Layer*)layer->pthis)->Layer::load_param(*(const ParamDict*)pd);
}

static int __ncnn_Layer_load_model(ncnn_layer_t layer, const ncnn_modelbin_t mb)
{
    return ((Layer*)layer->pthis)->Layer::load_model(*(const ModelBin*)mb);
}

static int __ncnn_Layer_create_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
{
    return ((Layer*)layer->pthis)->Layer::create_pipeline(*(const Option*)opt);
}

static int __ncnn_Layer_destroy_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
{
    return ((Layer*)layer->pthis)->Layer::destroy_pipeline(*(const Option*)opt);
}

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)
{
    Mat _top_blob;
    int ret = ((const Layer*)layer->pthis)->Layer::forward(*(const Mat*)bottom_blob, _top_blob, *(const Option*)opt);
    *top_blob = (ncnn_mat_t)(new Mat(_top_blob));
    return ret;
}

924
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)
925 926 927 928 929 930 931 932 933 934
{
    std::vector<Mat> _bottom_blobs(n);
    std::vector<Mat> _top_blobs(n2);
    for (int i = 0; i < n; i++)
    {
        _bottom_blobs[i] = *(Mat*)bottom_blobs[i];
    }
    int ret = ((const Layer*)layer->pthis)->Layer::forward(_bottom_blobs, _top_blobs, *(const Option*)opt);
    for (int i = 0; i < n2; i++)
    {
935
        top_blobs[i] = (ncnn_mat_t)(new Mat(_top_blobs[i]));
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
    }
    return ret;
}

static int __ncnn_Layer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
{
    return ((const Layer*)layer->pthis)->Layer::forward_inplace(*(Mat*)bottom_top_blob, *(const Option*)opt);
}

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)
{
    std::vector<Mat> _bottom_top_blobs(n);
    for (int i = 0; i < n; i++)
    {
        _bottom_top_blobs[i] = *(Mat*)bottom_top_blobs[i];
    }
    return ((const Layer*)layer->pthis)->Layer::forward_inplace(_bottom_top_blobs, *(const Option*)opt);
}

static int __ncnn_layer_load_param(ncnn_layer_t layer, const ncnn_paramdict_t pd)
{
    return ((Layer*)layer->pthis)->load_param(*(const ParamDict*)pd);
}

static int __ncnn_layer_load_model(ncnn_layer_t layer, const ncnn_modelbin_t mb)
{
    return ((Layer*)layer->pthis)->load_model(*(const ModelBin*)mb);
}

static int __ncnn_layer_create_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
{
    return ((Layer*)layer->pthis)->create_pipeline(*(const Option*)opt);
}

static int __ncnn_layer_destroy_pipeline(ncnn_layer_t layer, const ncnn_option_t opt)
{
    return ((Layer*)layer->pthis)->destroy_pipeline(*(const Option*)opt);
}

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)
{
    Mat _top_blob;
    int ret = ((const Layer*)layer->pthis)->forward(*(const Mat*)bottom_blob, _top_blob, *(const Option*)opt);
    *top_blob = (ncnn_mat_t)(new Mat(_top_blob));
    return ret;
}

983
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)
984 985 986 987 988 989 990 991 992 993
{
    std::vector<Mat> _bottom_blobs(n);
    std::vector<Mat> _top_blobs(n2);
    for (int i = 0; i < n; i++)
    {
        _bottom_blobs[i] = *(Mat*)bottom_blobs[i];
    }
    int ret = ((const Layer*)layer->pthis)->forward(_bottom_blobs, _top_blobs, *(const Option*)opt);
    for (int i = 0; i < n2; i++)
    {
994
        top_blobs[i] = (ncnn_mat_t)(new Mat(_top_blobs[i]));
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
    }
    return ret;
}

static int __ncnn_layer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
{
    return ((const Layer*)layer->pthis)->forward_inplace(*(Mat*)bottom_top_blob, *(const Option*)opt);
}

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)
{
    std::vector<Mat> _bottom_top_blobs(n);
    for (int i = 0; i < n; i++)
    {
        _bottom_top_blobs[i] = *(Mat*)bottom_top_blobs[i];
    }
    return ((const Layer*)layer->pthis)->forward_inplace(_bottom_top_blobs, *(const Option*)opt);
}

ncnn_layer_t ncnn_layer_create()
{
    ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
    layer->pthis = (void*)(new Layer_c_api(layer));
    layer->load_param = __ncnn_Layer_load_param;
    layer->load_model = __ncnn_Layer_load_model;
    layer->create_pipeline = __ncnn_Layer_create_pipeline;
    layer->destroy_pipeline = __ncnn_Layer_destroy_pipeline;
    layer->forward_1 = __ncnn_Layer_forward_1;
    layer->forward_n = __ncnn_Layer_forward_n;
    layer->forward_inplace_1 = __ncnn_Layer_forward_inplace_1;
    layer->forward_inplace_n = __ncnn_Layer_forward_inplace_n;
    return layer;
}

N
nihuini 已提交
1029 1030
ncnn_layer_t ncnn_layer_create_by_typeindex(int typeindex)
{
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
    layer->pthis = (void*)(ncnn::create_layer(typeindex));
    layer->load_param = __ncnn_layer_load_param;
    layer->load_model = __ncnn_layer_load_model;
    layer->create_pipeline = __ncnn_layer_create_pipeline;
    layer->destroy_pipeline = __ncnn_layer_destroy_pipeline;
    layer->forward_1 = __ncnn_layer_forward_1;
    layer->forward_n = __ncnn_layer_forward_n;
    layer->forward_inplace_1 = __ncnn_layer_forward_inplace_1;
    layer->forward_inplace_n = __ncnn_layer_forward_inplace_n;
    return layer;
N
nihuini 已提交
1042 1043
}

N
nihui 已提交
1044
#if NCNN_STRING
N
nihuini 已提交
1045 1046
ncnn_layer_t ncnn_layer_create_by_type(const char* type)
{
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    ncnn_layer_t layer = (ncnn_layer_t)malloc(sizeof(__ncnn_layer_t));
    layer->pthis = (void*)(ncnn::create_layer(type));
    layer->load_param = __ncnn_layer_load_param;
    layer->load_model = __ncnn_layer_load_model;
    layer->create_pipeline = __ncnn_layer_create_pipeline;
    layer->destroy_pipeline = __ncnn_layer_destroy_pipeline;
    layer->forward_1 = __ncnn_layer_forward_1;
    layer->forward_n = __ncnn_layer_forward_n;
    layer->forward_inplace_1 = __ncnn_layer_forward_inplace_1;
    layer->forward_inplace_n = __ncnn_layer_forward_inplace_n;
    return layer;
N
nihuini 已提交
1058
}
N
nihui 已提交
1059
#endif /* NCNN_STRING */
N
nihuini 已提交
1060 1061 1062

void ncnn_layer_destroy(ncnn_layer_t layer)
{
1063 1064
    delete (Layer*)layer->pthis;
    free(layer);
N
nihuini 已提交
1065 1066
}

N
nihui 已提交
1067
#if NCNN_STRING
N
nihuini 已提交
1068
const char* ncnn_layer_get_name(const ncnn_layer_t layer)
N
nihui 已提交
1069
{
1070
    return ((const Layer*)layer->pthis)->name.c_str();
N
nihui 已提交
1071
}
N
nihui 已提交
1072
#endif /* NCNN_STRING */
N
nihui 已提交
1073

N
nihuini 已提交
1074
int ncnn_layer_get_typeindex(const ncnn_layer_t layer)
N
nihui 已提交
1075
{
1076
    return ((const Layer*)layer->pthis)->typeindex;
N
nihui 已提交
1077 1078
}

N
nihui 已提交
1079
#if NCNN_STRING
N
nihuini 已提交
1080
const char* ncnn_layer_get_type(const ncnn_layer_t layer)
N
nihui 已提交
1081
{
1082
    return ((const Layer*)layer->pthis)->type.c_str();
N
nihui 已提交
1083
}
N
nihui 已提交
1084
#endif /* NCNN_STRING */
N
nihui 已提交
1085

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
int ncnn_layer_get_one_blob_only(const ncnn_layer_t layer)
{
    return ((const Layer*)layer->pthis)->one_blob_only;
}

int ncnn_layer_get_support_inplace(const ncnn_layer_t layer)
{
    return ((const Layer*)layer->pthis)->support_inplace;
}

int ncnn_layer_get_support_vulkan(const ncnn_layer_t layer)
{
    return ((const Layer*)layer->pthis)->support_vulkan;
}

int ncnn_layer_get_support_packing(const ncnn_layer_t layer)
{
    return ((const Layer*)layer->pthis)->support_packing;
}

int ncnn_layer_get_support_bf16_storage(const ncnn_layer_t layer)
{
    return ((const Layer*)layer->pthis)->support_bf16_storage;
}

int ncnn_layer_get_support_fp16_storage(const ncnn_layer_t layer)
{
    return ((const Layer*)layer->pthis)->support_fp16_storage;
}

int ncnn_layer_get_support_image_storage(const ncnn_layer_t layer)
{
    return ((const Layer*)layer->pthis)->support_image_storage;
}

void ncnn_layer_set_one_blob_only(ncnn_layer_t layer, int enable)
{
    ((Layer*)layer->pthis)->one_blob_only = enable;
}

void ncnn_layer_set_support_inplace(ncnn_layer_t layer, int enable)
{
    ((Layer*)layer->pthis)->support_inplace = enable;
}

void ncnn_layer_set_support_vulkan(ncnn_layer_t layer, int enable)
{
    ((Layer*)layer->pthis)->support_vulkan = enable;
}

void ncnn_layer_set_support_packing(ncnn_layer_t layer, int enable)
{
    ((Layer*)layer->pthis)->support_packing = enable;
}

void ncnn_layer_set_support_bf16_storage(ncnn_layer_t layer, int enable)
{
    ((Layer*)layer->pthis)->support_bf16_storage = enable;
}

void ncnn_layer_set_support_fp16_storage(ncnn_layer_t layer, int enable)
{
    ((Layer*)layer->pthis)->support_fp16_storage = enable;
}

void ncnn_layer_set_support_image_storage(ncnn_layer_t layer, int enable)
{
    ((Layer*)layer->pthis)->support_image_storage = enable;
}

N
nihuini 已提交
1156
int ncnn_layer_get_bottom_count(const ncnn_layer_t layer)
N
nihui 已提交
1157
{
1158
    return (int)((const Layer*)layer->pthis)->bottoms.size();
N
nihui 已提交
1159 1160
}

N
nihuini 已提交
1161
int ncnn_layer_get_bottom(const ncnn_layer_t layer, int i)
N
nihui 已提交
1162
{
1163
    return ((const Layer*)layer->pthis)->bottoms[i];
N
nihui 已提交
1164 1165
}

N
nihuini 已提交
1166
int ncnn_layer_get_top_count(const ncnn_layer_t layer)
N
nihui 已提交
1167
{
1168
    return (int)((const Layer*)layer->pthis)->tops.size();
N
nihui 已提交
1169 1170
}

N
nihuini 已提交
1171
int ncnn_layer_get_top(const ncnn_layer_t layer, int i)
N
nihui 已提交
1172
{
1173
    return ((const Layer*)layer->pthis)->tops[i];
N
nihui 已提交
1174 1175
}

N
nihuini 已提交
1176
void ncnn_blob_get_bottom_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c)
N
nihui 已提交
1177
{
1178
    const Mat& shape = ((const Layer*)layer->pthis)->bottom_shapes[i];
N
nihui 已提交
1179 1180 1181 1182 1183 1184
    *dims = shape.dims;
    *w = shape.w;
    *h = shape.h;
    *c = shape.c;
}

N
nihuini 已提交
1185
void ncnn_blob_get_top_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c)
N
nihui 已提交
1186
{
1187
    const Mat& shape = ((const Layer*)layer->pthis)->top_shapes[i];
N
nihui 已提交
1188 1189 1190 1191 1192 1193
    *dims = shape.dims;
    *w = shape.w;
    *h = shape.h;
    *c = shape.c;
}

1194 1195
/* net api */
ncnn_net_t ncnn_net_create()
N
nihuini 已提交
1196
{
1197 1198 1199 1200
    ncnn_net_t net = (ncnn_net_t)malloc(sizeof(struct __ncnn_net_t));
    net->pthis = (void*)(new Net());
    net->custom_layer_factory = 0;
    return net;
N
nihuini 已提交
1201 1202
}

1203
void ncnn_net_destroy(ncnn_net_t net)
N
nihuini 已提交
1204
{
1205 1206 1207 1208 1209 1210 1211 1212 1213
    delete (Net*)net->pthis;
    ncnn_net_custom_layer_factory_t ud = net->custom_layer_factory;
    while (ud)
    {
        ncnn_net_custom_layer_factory_t ud_next = ud->next;
        free(ud);
        ud = ud_next;
    }
    free(net);
N
nihuini 已提交
1214 1215
}

N
nihui 已提交
1216 1217 1218 1219 1220
ncnn_option_t ncnn_net_get_option(ncnn_net_t net)
{
    return (ncnn_option_t)(&((Net*)(net->pthis))->opt);
}

1221
void ncnn_net_set_option(ncnn_net_t net, ncnn_option_t opt)
N
nihuini 已提交
1222
{
1223
    ((Net*)net->pthis)->opt = *((Option*)opt);
N
nihuini 已提交
1224 1225
}

1226
static ::ncnn::Layer* __Layer_c_api_layer_creator(void* userdata)
N
nihuini 已提交
1227
{
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)userdata;

    ncnn_layer_t layer0 = ud->creator(ud->userdata);

    ::ncnn::Layer* layer = (::ncnn::Layer*)layer0->pthis;

    layer->userdata = (void*)layer0;

    layer->one_blob_only = ncnn_layer_get_one_blob_only(layer0);
    layer->support_inplace = ncnn_layer_get_support_inplace(layer0);
    layer->support_vulkan = ncnn_layer_get_support_vulkan(layer0);
    layer->support_packing = ncnn_layer_get_support_packing(layer0);

    layer->support_bf16_storage = ncnn_layer_get_support_bf16_storage(layer0);
    layer->support_fp16_storage = ncnn_layer_get_support_fp16_storage(layer0);
    layer->support_image_storage = ncnn_layer_get_support_image_storage(layer0);

    return layer;
N
nihuini 已提交
1246 1247
}

1248
static void __Layer_c_api_layer_destroyer(::ncnn::Layer* layer, void* userdata)
N
nihuini 已提交
1249
{
1250 1251 1252 1253 1254
    ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)userdata;

    ncnn_layer_t layer0 = (ncnn_layer_t)layer->userdata;

    ud->destroyer(layer0, ud->userdata);
N
nihuini 已提交
1255 1256
}

N
nihui 已提交
1257
#if NCNN_STRING
1258
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)
N
nihuini 已提交
1259
{
1260 1261 1262 1263 1264 1265 1266
    ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)malloc(sizeof(struct __ncnn_net_custom_layer_factory_t));
    ud->creator = creator;
    ud->destroyer = destroyer;
    ud->userdata = userdata;
    ud->next = net->custom_layer_factory;
    net->custom_layer_factory = ud;
    ((Net*)net->pthis)->register_custom_layer(type, __Layer_c_api_layer_creator, __Layer_c_api_layer_destroyer, (void*)ud);
N
nihuini 已提交
1267
}
N
nihui 已提交
1268
#endif /* NCNN_STRING */
N
nihuini 已提交
1269

1270
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)
N
nihuini 已提交
1271
{
1272 1273 1274 1275 1276 1277 1278
    ncnn_net_custom_layer_factory_t ud = (ncnn_net_custom_layer_factory_t)malloc(sizeof(struct __ncnn_net_custom_layer_factory_t));
    ud->creator = creator;
    ud->destroyer = destroyer;
    ud->userdata = userdata;
    ud->next = net->custom_layer_factory;
    net->custom_layer_factory = ud;
    ((Net*)net->pthis)->register_custom_layer(typeindex, __Layer_c_api_layer_creator, __Layer_c_api_layer_destroyer, (void*)ud);
N
nihuini 已提交
1279 1280
}

1281 1282 1283
#if NCNN_STDIO
#if NCNN_STRING
int ncnn_net_load_param(ncnn_net_t net, const char* path)
N
nihuini 已提交
1284
{
1285
    return ((Net*)net->pthis)->load_param(path);
N
nihuini 已提交
1286
}
1287
#endif /* NCNN_STRING */
N
nihuini 已提交
1288

1289
int ncnn_net_load_param_bin(ncnn_net_t net, const char* path)
N
nihui 已提交
1290
{
1291
    return ((Net*)net->pthis)->load_param_bin(path);
N
nihui 已提交
1292 1293
}

1294
int ncnn_net_load_model(ncnn_net_t net, const char* path)
N
nihui 已提交
1295
{
1296
    return ((Net*)net->pthis)->load_model(path);
N
nihui 已提交
1297
}
1298
#endif /* NCNN_STDIO */
N
nihui 已提交
1299

N
nihuini 已提交
1300
#if NCNN_STDIO
1301 1302
#if NCNN_STRING
int ncnn_net_load_param_memory(ncnn_net_t net, const char* mem)
N
nihui 已提交
1303
{
1304
    return ((Net*)net->pthis)->load_param_mem(mem);
N
nihui 已提交
1305
}
1306
#endif /* NCNN_STRING */
N
nihuini 已提交
1307
#endif /* NCNN_STDIO */
N
nihui 已提交
1308

1309
int ncnn_net_load_param_bin_memory(ncnn_net_t net, const unsigned char* mem)
N
nihui 已提交
1310
{
1311
    return ((Net*)net->pthis)->load_param(mem);
N
nihui 已提交
1312 1313
}

1314
int ncnn_net_load_model_memory(ncnn_net_t net, const unsigned char* mem)
N
nihui 已提交
1315
{
1316
    return ((Net*)net->pthis)->load_model(mem);
N
nihui 已提交
1317 1318
}

1319 1320
#if NCNN_STRING
int ncnn_net_load_param_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
N
nihui 已提交
1321
{
1322
    return ((Net*)net->pthis)->load_param(*(const DataReader*)dr->pthis);
N
nihui 已提交
1323
}
1324
#endif /* NCNN_STRING */
N
nihui 已提交
1325

1326
int ncnn_net_load_param_bin_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
N
nihui 已提交
1327
{
1328
    return ((Net*)net->pthis)->load_param_bin(*(const DataReader*)dr->pthis);
N
nihui 已提交
1329 1330
}

1331
int ncnn_net_load_model_datareader(ncnn_net_t net, const ncnn_datareader_t dr)
N
nihui 已提交
1332
{
1333
    return ((Net*)net->pthis)->load_model(*(const DataReader*)dr->pthis);
N
nihui 已提交
1334 1335
}

1336
void ncnn_net_clear(ncnn_net_t net)
N
nihui 已提交
1337
{
1338
    return ((Net*)net->pthis)->clear();
N
nihui 已提交
1339 1340
}

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
int ncnn_net_get_input_count(const ncnn_net_t net)
{
    return (int)((Net*)net->pthis)->input_indexes().size();
}

int ncnn_net_get_output_count(const ncnn_net_t net)
{
    return (int)((Net*)net->pthis)->output_indexes().size();
}

#if NCNN_STRING
const char* ncnn_net_get_input_name(const ncnn_net_t net, int i)
{
    return ((Net*)net->pthis)->input_names()[i];
}

const char* ncnn_net_get_output_name(const ncnn_net_t net, int i)
{
    return ((Net*)net->pthis)->output_names()[i];
}
#endif /* NCNN_STRING */

int ncnn_net_get_input_index(const ncnn_net_t net, int i)
{
    return ((Net*)net->pthis)->input_indexes()[i];
}

int ncnn_net_get_output_index(const ncnn_net_t net, int i)
{
    return ((Net*)net->pthis)->output_indexes()[i];
}

N
nihui 已提交
1373 1374 1375
/* extractor api */
ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net)
{
1376
    return (ncnn_extractor_t)(new Extractor(((Net*)net->pthis)->create_extractor()));
N
nihui 已提交
1377 1378 1379 1380 1381 1382 1383
}

void ncnn_extractor_destroy(ncnn_extractor_t ex)
{
    delete (Extractor*)ex;
}

N
nihuini 已提交
1384
void ncnn_extractor_set_option(ncnn_extractor_t ex, const ncnn_option_t opt)
N
nihui 已提交
1385
{
N
nihuini 已提交
1386
    ((Extractor*)ex)->set_num_threads(((const Option*)opt)->num_threads);
N
nihui 已提交
1387
#if NCNN_VULKAN
N
nihuini 已提交
1388
    ((Extractor*)ex)->set_vulkan_compute(((const Option*)opt)->use_vulkan_compute);
N
nihui 已提交
1389 1390 1391
#endif
}

1392
#if NCNN_STRING
N
nihuini 已提交
1393
int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat)
N
nihui 已提交
1394
{
N
nihuini 已提交
1395
    return ((Extractor*)ex)->input(name, *((const Mat*)mat));
N
nihui 已提交
1396 1397 1398 1399 1400 1401 1402 1403
}

int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat)
{
    Mat mat0;
    int ret = ((Extractor*)ex)->extract(name, mat0);
    *mat = (ncnn_mat_t)(new Mat(mat0));
    return ret;
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
}
#endif /* NCNN_STRING */

int ncnn_extractor_input_index(ncnn_extractor_t ex, int index, const ncnn_mat_t mat)
{
    return ((Extractor*)ex)->input(index, *((const Mat*)mat));
}

int ncnn_extractor_extract_index(ncnn_extractor_t ex, int index, ncnn_mat_t* mat)
{
    Mat mat0;
    int ret = ((Extractor*)ex)->extract(index, mat0);
    *mat = (ncnn_mat_t)(new Mat(mat0));
    return ret;
N
nihui 已提交
1418 1419 1420 1421 1422
}

#ifdef __cplusplus
} /* extern "C" */
#endif
N
nihui 已提交
1423 1424

#endif /* NCNN_C_API */