react-native-vision-camera-spoof-detector 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -24
- package/VisionCameraSpoofDetector.podspec +12 -2
- package/ios/HybridSpoofDetectorOutput.swift +72 -157
- package/ios/NcnnLivenessDetector.h +24 -0
- package/ios/NcnnLivenessDetector.mm +248 -0
- package/ios/ncnn.xcframework/Info.plist +27 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/ncnn +0 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/ncnn +0 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/ncnn +0 -0
- package/ios/openmp.xcframework/Info.plist +27 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/openmp +0 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/openmp +0 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/openmp +0 -0
- package/package.json +1 -1
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
#import "NcnnLivenessDetector.h"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
#if HAS_NCNN
|
|
10
|
+
#include <ncnn/net.h>
|
|
11
|
+
#endif
|
|
12
|
+
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
struct ModelConfig {
|
|
16
|
+
float scale;
|
|
17
|
+
float shiftX;
|
|
18
|
+
float shiftY;
|
|
19
|
+
int height;
|
|
20
|
+
int width;
|
|
21
|
+
std::string name;
|
|
22
|
+
bool originalResize;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
NSError *DetectorError(NSInteger code, NSString *message) {
|
|
26
|
+
return [NSError errorWithDomain:@"NcnnLivenessDetector"
|
|
27
|
+
code:code
|
|
28
|
+
userInfo:@{NSLocalizedDescriptionKey : message}];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
bool ReadModelConfigs(NSString *configPath, std::vector<ModelConfig> &configs, NSError **error) {
|
|
32
|
+
NSData *data = [NSData dataWithContentsOfFile:configPath];
|
|
33
|
+
if (data == nil) {
|
|
34
|
+
if (error != nullptr) {
|
|
35
|
+
*error = DetectorError(1, [NSString stringWithFormat:@"Could not read model config at %@", configPath]);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
NSError *jsonError = nil;
|
|
41
|
+
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
|
|
42
|
+
if (![json isKindOfClass:[NSArray class]] || [(NSArray *)json count] == 0) {
|
|
43
|
+
if (error != nullptr) {
|
|
44
|
+
*error = jsonError ?: DetectorError(2, @"Model config must contain a non-empty JSON array");
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (id item in (NSArray *)json) {
|
|
50
|
+
if (![item isKindOfClass:[NSDictionary class]]) {
|
|
51
|
+
if (error != nullptr) {
|
|
52
|
+
*error = DetectorError(3, @"Each model config entry must be a JSON object");
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
NSDictionary *entry = (NSDictionary *)item;
|
|
58
|
+
NSString *name = entry[@"name"];
|
|
59
|
+
NSNumber *width = entry[@"width"];
|
|
60
|
+
NSNumber *height = entry[@"height"];
|
|
61
|
+
NSNumber *scale = entry[@"scale"];
|
|
62
|
+
if (![name isKindOfClass:[NSString class]] || name.length == 0 ||
|
|
63
|
+
![width isKindOfClass:[NSNumber class]] || width.intValue <= 0 ||
|
|
64
|
+
![height isKindOfClass:[NSNumber class]] || height.intValue <= 0 ||
|
|
65
|
+
![scale isKindOfClass:[NSNumber class]] || scale.floatValue <= 0) {
|
|
66
|
+
if (error != nullptr) {
|
|
67
|
+
*error = DetectorError(4, @"Model config entries require a name, positive dimensions, and positive scale");
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
configs.push_back({
|
|
73
|
+
scale.floatValue,
|
|
74
|
+
[entry[@"shift_x"] respondsToSelector:@selector(floatValue)] ? [entry[@"shift_x"] floatValue] : 0.0f,
|
|
75
|
+
[entry[@"shift_y"] respondsToSelector:@selector(floatValue)] ? [entry[@"shift_y"] floatValue] : 0.0f,
|
|
76
|
+
height.intValue,
|
|
77
|
+
width.intValue,
|
|
78
|
+
name.UTF8String ?: "",
|
|
79
|
+
[entry[@"org_resize"] respondsToSelector:@selector(boolValue)] ? [entry[@"org_resize"] boolValue] : false,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
} // namespace
|
|
87
|
+
|
|
88
|
+
@interface NcnnLivenessDetector () {
|
|
89
|
+
void *_implementation;
|
|
90
|
+
}
|
|
91
|
+
@property(nonatomic, copy, readwrite) NSString *lastError;
|
|
92
|
+
@end
|
|
93
|
+
|
|
94
|
+
class NcnnLivenessDetectorImpl {
|
|
95
|
+
public:
|
|
96
|
+
#if HAS_NCNN
|
|
97
|
+
std::vector<std::unique_ptr<ncnn::Net>> networks;
|
|
98
|
+
#endif
|
|
99
|
+
std::vector<ModelConfig> configs;
|
|
100
|
+
bool loaded = false;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
@implementation NcnnLivenessDetector
|
|
104
|
+
|
|
105
|
+
- (instancetype)init {
|
|
106
|
+
self = [super init];
|
|
107
|
+
if (self) {
|
|
108
|
+
_implementation = new NcnnLivenessDetectorImpl();
|
|
109
|
+
_lastError = @"";
|
|
110
|
+
}
|
|
111
|
+
return self;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
- (BOOL)loadModelsAtPath:(NSString *)modelPath {
|
|
115
|
+
auto *implementation = static_cast<NcnnLivenessDetectorImpl *>(_implementation);
|
|
116
|
+
if (implementation->loaded) {
|
|
117
|
+
return YES;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#if HAS_NCNN
|
|
121
|
+
NSError *error = nil;
|
|
122
|
+
NSString *configPath = [modelPath stringByAppendingPathComponent:@"config.json"];
|
|
123
|
+
if (!ReadModelConfigs(configPath, implementation->configs, &error)) {
|
|
124
|
+
self.lastError = error.localizedDescription;
|
|
125
|
+
return NO;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const ModelConfig &config : implementation->configs) {
|
|
129
|
+
auto network = std::make_unique<ncnn::Net>();
|
|
130
|
+
network->opt.use_vulkan_compute = false;
|
|
131
|
+
network->opt.use_fp16_storage = true;
|
|
132
|
+
|
|
133
|
+
NSString *parameterPath = [modelPath stringByAppendingPathComponent:
|
|
134
|
+
[NSString stringWithUTF8String:(config.name + ".param").c_str()]];
|
|
135
|
+
NSString *weightsPath = [modelPath stringByAppendingPathComponent:
|
|
136
|
+
[NSString stringWithUTF8String:(config.name + ".bin").c_str()]];
|
|
137
|
+
if (network->load_param(parameterPath.UTF8String) != 0 ||
|
|
138
|
+
network->load_model(weightsPath.UTF8String) != 0) {
|
|
139
|
+
self.lastError = [NSString stringWithFormat:@"Failed to load NCNN model %@", [NSString stringWithUTF8String:config.name.c_str()]];
|
|
140
|
+
implementation->networks.clear();
|
|
141
|
+
implementation->configs.clear();
|
|
142
|
+
return NO;
|
|
143
|
+
}
|
|
144
|
+
implementation->networks.push_back(std::move(network));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
implementation->loaded = true;
|
|
148
|
+
self.lastError = @"";
|
|
149
|
+
return YES;
|
|
150
|
+
#else
|
|
151
|
+
self.lastError = @"The bundled NCNN runtime is available only for physical arm64 iOS devices";
|
|
152
|
+
return NO;
|
|
153
|
+
#endif
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
- (float)scoreForBGRABytes:(const void *)bytes
|
|
157
|
+
byteCount:(NSUInteger)byteCount
|
|
158
|
+
width:(NSInteger)width
|
|
159
|
+
height:(NSInteger)height
|
|
160
|
+
bytesPerRow:(NSInteger)bytesPerRow
|
|
161
|
+
left:(NSInteger)left
|
|
162
|
+
top:(NSInteger)top
|
|
163
|
+
right:(NSInteger)right
|
|
164
|
+
bottom:(NSInteger)bottom {
|
|
165
|
+
auto *implementation = static_cast<NcnnLivenessDetectorImpl *>(_implementation);
|
|
166
|
+
if (!implementation->loaded) {
|
|
167
|
+
self.lastError = @"NCNN models are not loaded";
|
|
168
|
+
return NAN;
|
|
169
|
+
}
|
|
170
|
+
if (width <= 0 || height <= 0 || bytesPerRow < width * 4 ||
|
|
171
|
+
bytes == nullptr || byteCount < (NSUInteger)(bytesPerRow * height) ||
|
|
172
|
+
left < 0 || top < 0 || right <= left || bottom <= top ||
|
|
173
|
+
right > width || bottom > height) {
|
|
174
|
+
self.lastError = @"Invalid BGRA frame dimensions, buffer size, or face bounds";
|
|
175
|
+
return NAN;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
#if HAS_NCNN
|
|
179
|
+
ncnn::Mat image = ncnn::Mat::from_pixels(
|
|
180
|
+
static_cast<const unsigned char *>(bytes),
|
|
181
|
+
ncnn::Mat::PIXEL_BGRA2RGB,
|
|
182
|
+
static_cast<int>(width),
|
|
183
|
+
static_cast<int>(height),
|
|
184
|
+
static_cast<int>(bytesPerRow));
|
|
185
|
+
if (image.empty()) {
|
|
186
|
+
self.lastError = @"Could not convert the BGRA camera frame to NCNN input";
|
|
187
|
+
return NAN;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const int boxWidth = static_cast<int>(right - left);
|
|
191
|
+
const int boxHeight = static_cast<int>(bottom - top);
|
|
192
|
+
float totalScore = 0.0f;
|
|
193
|
+
|
|
194
|
+
for (size_t index = 0; index < implementation->networks.size(); index++) {
|
|
195
|
+
const ModelConfig &config = implementation->configs[index];
|
|
196
|
+
ncnn::Net *network = implementation->networks[index].get();
|
|
197
|
+
const float scale = std::min(
|
|
198
|
+
static_cast<float>(height) / boxHeight,
|
|
199
|
+
std::min(static_cast<float>(width) / boxWidth, config.scale));
|
|
200
|
+
const int cropWidth = static_cast<int>(boxWidth * scale);
|
|
201
|
+
const int cropHeight = static_cast<int>(boxHeight * scale);
|
|
202
|
+
const int cropLeft = std::max(0, static_cast<int>(left - (cropWidth - boxWidth) / 2.0f));
|
|
203
|
+
const int cropTop = std::max(0, static_cast<int>(top - (cropHeight - boxHeight) / 2.0f));
|
|
204
|
+
const int boundedWidth = std::min(static_cast<int>(width) - cropLeft, cropWidth);
|
|
205
|
+
const int boundedHeight = std::min(static_cast<int>(height) - cropTop, cropHeight);
|
|
206
|
+
if (boundedWidth <= 0 || boundedHeight <= 0) {
|
|
207
|
+
self.lastError = @"The expanded face crop is empty";
|
|
208
|
+
return NAN;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
ncnn::Mat face;
|
|
212
|
+
ncnn::copy_cut_border(
|
|
213
|
+
image,
|
|
214
|
+
face,
|
|
215
|
+
cropTop,
|
|
216
|
+
static_cast<int>(height) - cropTop - boundedHeight,
|
|
217
|
+
cropLeft,
|
|
218
|
+
static_cast<int>(width) - cropLeft - boundedWidth);
|
|
219
|
+
ncnn::Mat input;
|
|
220
|
+
ncnn::resize_bilinear(face, input, config.width, config.height);
|
|
221
|
+
|
|
222
|
+
ncnn::Extractor extractor = network->create_extractor();
|
|
223
|
+
ncnn::Mat output;
|
|
224
|
+
if (input.empty() || extractor.input("data", input) != 0 ||
|
|
225
|
+
extractor.extract("softmax", output) != 0 || output.empty()) {
|
|
226
|
+
self.lastError = [NSString stringWithFormat:@"NCNN inference failed for model %@", [NSString stringWithUTF8String:config.name.c_str()]];
|
|
227
|
+
return NAN;
|
|
228
|
+
}
|
|
229
|
+
totalScore += output.w >= 2 ? output[1] : output[0];
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (implementation->networks.empty()) {
|
|
233
|
+
self.lastError = @"No NCNN models are loaded";
|
|
234
|
+
return NAN;
|
|
235
|
+
}
|
|
236
|
+
self.lastError = @"";
|
|
237
|
+
return totalScore / static_cast<float>(implementation->networks.size());
|
|
238
|
+
#else
|
|
239
|
+
self.lastError = @"NCNN inference is unavailable for this build target";
|
|
240
|
+
return NAN;
|
|
241
|
+
#endif
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
- (void)dealloc {
|
|
245
|
+
delete static_cast<NcnnLivenessDetectorImpl *>(_implementation);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
@end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>AvailableLibraries</key>
|
|
6
|
+
<array>
|
|
7
|
+
<dict>
|
|
8
|
+
<key>BinaryPath</key>
|
|
9
|
+
<string>ncnn.framework/Versions/A/ncnn</string>
|
|
10
|
+
<key>LibraryIdentifier</key>
|
|
11
|
+
<string>ios-arm64</string>
|
|
12
|
+
<key>LibraryPath</key>
|
|
13
|
+
<string>ncnn.framework</string>
|
|
14
|
+
<key>SupportedArchitectures</key>
|
|
15
|
+
<array>
|
|
16
|
+
<string>arm64</string>
|
|
17
|
+
</array>
|
|
18
|
+
<key>SupportedPlatform</key>
|
|
19
|
+
<string>ios</string>
|
|
20
|
+
</dict>
|
|
21
|
+
</array>
|
|
22
|
+
<key>CFBundlePackageType</key>
|
|
23
|
+
<string>XFWK</string>
|
|
24
|
+
<key>XCFrameworkFormatVersion</key>
|
|
25
|
+
<string>1.0</string>
|
|
26
|
+
</dict>
|
|
27
|
+
</plist>
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
// Copyright 2018 Tencent
|
|
2
|
+
// SPDX-License-Identifier: BSD-3-Clause
|
|
3
|
+
|
|
4
|
+
#ifndef NCNN_ALLOCATOR_H
|
|
5
|
+
#define NCNN_ALLOCATOR_H
|
|
6
|
+
|
|
7
|
+
#ifdef _WIN32
|
|
8
|
+
#define WIN32_LEAN_AND_MEAN
|
|
9
|
+
#include <windows.h>
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
#include "platform.h"
|
|
13
|
+
|
|
14
|
+
#include <stdlib.h>
|
|
15
|
+
|
|
16
|
+
#if NCNN_PLATFORM_API
|
|
17
|
+
#if __ANDROID_API__ >= 26
|
|
18
|
+
#include <android/hardware_buffer.h>
|
|
19
|
+
#endif // __ANDROID_API__ >= 26
|
|
20
|
+
#endif // NCNN_PLATFORM_API
|
|
21
|
+
|
|
22
|
+
namespace ncnn {
|
|
23
|
+
|
|
24
|
+
// the alignment of all the allocated buffers
|
|
25
|
+
#if NCNN_AVX512
|
|
26
|
+
#define NCNN_MALLOC_ALIGN 64
|
|
27
|
+
#elif NCNN_AVX
|
|
28
|
+
#define NCNN_MALLOC_ALIGN 32
|
|
29
|
+
#else
|
|
30
|
+
#define NCNN_MALLOC_ALIGN 16
|
|
31
|
+
#endif
|
|
32
|
+
|
|
33
|
+
// we have some optimized kernels that may overread buffer a bit in loop
|
|
34
|
+
// it is common to interleave next-loop data load with arithmetic instructions
|
|
35
|
+
// allocating more bytes keeps us safe from SEGV_ACCERR failure
|
|
36
|
+
#define NCNN_MALLOC_OVERREAD 64
|
|
37
|
+
|
|
38
|
+
// Aligns a pointer to the specified number of bytes
|
|
39
|
+
// ptr Aligned pointer
|
|
40
|
+
// n Alignment size that must be a power of two
|
|
41
|
+
template<typename _Tp>
|
|
42
|
+
static NCNN_FORCEINLINE _Tp* alignPtr(_Tp* ptr, int n = (int)sizeof(_Tp))
|
|
43
|
+
{
|
|
44
|
+
return (_Tp*)(((size_t)ptr + n - 1) & -n);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Aligns a buffer size to the specified number of bytes
|
|
48
|
+
// The function returns the minimum number that is greater or equal to sz and is divisible by n
|
|
49
|
+
// sz Buffer size to align
|
|
50
|
+
// n Alignment size that must be a power of two
|
|
51
|
+
static NCNN_FORCEINLINE size_t alignSize(size_t sz, int n)
|
|
52
|
+
{
|
|
53
|
+
return (sz + n - 1) & -n;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static NCNN_FORCEINLINE void* fastMalloc(size_t size)
|
|
57
|
+
{
|
|
58
|
+
#if _MSC_VER
|
|
59
|
+
return _aligned_malloc(size, NCNN_MALLOC_ALIGN);
|
|
60
|
+
#elif (defined(__unix__) || defined(__APPLE__)) && _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17)
|
|
61
|
+
void* ptr = 0;
|
|
62
|
+
if (posix_memalign(&ptr, NCNN_MALLOC_ALIGN, size + NCNN_MALLOC_OVERREAD))
|
|
63
|
+
ptr = 0;
|
|
64
|
+
return ptr;
|
|
65
|
+
#elif __ANDROID__ && __ANDROID_API__ < 17
|
|
66
|
+
return memalign(NCNN_MALLOC_ALIGN, size + NCNN_MALLOC_OVERREAD);
|
|
67
|
+
#else
|
|
68
|
+
unsigned char* udata = (unsigned char*)malloc(size + sizeof(void*) + NCNN_MALLOC_ALIGN + NCNN_MALLOC_OVERREAD);
|
|
69
|
+
if (!udata)
|
|
70
|
+
return 0;
|
|
71
|
+
unsigned char** adata = alignPtr((unsigned char**)udata + 1, NCNN_MALLOC_ALIGN);
|
|
72
|
+
adata[-1] = udata;
|
|
73
|
+
return adata;
|
|
74
|
+
#endif
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static NCNN_FORCEINLINE void fastFree(void* ptr)
|
|
78
|
+
{
|
|
79
|
+
if (ptr)
|
|
80
|
+
{
|
|
81
|
+
#if _MSC_VER
|
|
82
|
+
_aligned_free(ptr);
|
|
83
|
+
#elif (defined(__unix__) || defined(__APPLE__)) && _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17)
|
|
84
|
+
free(ptr);
|
|
85
|
+
#elif __ANDROID__ && __ANDROID_API__ < 17
|
|
86
|
+
free(ptr);
|
|
87
|
+
#else
|
|
88
|
+
unsigned char* udata = ((unsigned char**)ptr)[-1];
|
|
89
|
+
free(udata);
|
|
90
|
+
#endif
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#if NCNN_THREADS
|
|
95
|
+
// exchange-add operation for atomic operations on reference counters
|
|
96
|
+
#if defined __riscv && !defined __riscv_atomic
|
|
97
|
+
// riscv target without A extension
|
|
98
|
+
static NCNN_FORCEINLINE int NCNN_XADD(int* addr, int delta)
|
|
99
|
+
{
|
|
100
|
+
int tmp = *addr;
|
|
101
|
+
*addr += delta;
|
|
102
|
+
return tmp;
|
|
103
|
+
}
|
|
104
|
+
#elif defined __INTEL_COMPILER && !(defined WIN32 || defined _WIN32)
|
|
105
|
+
// atomic increment on the linux version of the Intel(tm) compiler
|
|
106
|
+
#define NCNN_XADD(addr, delta) (int)_InterlockedExchangeAdd(const_cast<void*>(reinterpret_cast<volatile void*>(addr)), delta)
|
|
107
|
+
#elif defined __GNUC__
|
|
108
|
+
#if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__)
|
|
109
|
+
#ifdef __ATOMIC_ACQ_REL
|
|
110
|
+
#define NCNN_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
|
|
111
|
+
#else
|
|
112
|
+
#define NCNN_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
|
|
113
|
+
#endif
|
|
114
|
+
#else
|
|
115
|
+
#if defined __ATOMIC_ACQ_REL && !defined __clang__
|
|
116
|
+
// version for gcc >= 4.7
|
|
117
|
+
#define NCNN_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
|
|
118
|
+
#else
|
|
119
|
+
#define NCNN_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
|
|
120
|
+
#endif
|
|
121
|
+
#endif
|
|
122
|
+
#elif defined _MSC_VER && !defined RC_INVOKED
|
|
123
|
+
#define NCNN_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
|
|
124
|
+
#else
|
|
125
|
+
// thread-unsafe branch
|
|
126
|
+
static NCNN_FORCEINLINE int NCNN_XADD(int* addr, int delta)
|
|
127
|
+
{
|
|
128
|
+
int tmp = *addr;
|
|
129
|
+
*addr += delta;
|
|
130
|
+
return tmp;
|
|
131
|
+
}
|
|
132
|
+
#endif
|
|
133
|
+
#else // NCNN_THREADS
|
|
134
|
+
static NCNN_FORCEINLINE int NCNN_XADD(int* addr, int delta)
|
|
135
|
+
{
|
|
136
|
+
int tmp = *addr;
|
|
137
|
+
*addr += delta;
|
|
138
|
+
return tmp;
|
|
139
|
+
}
|
|
140
|
+
#endif // NCNN_THREADS
|
|
141
|
+
|
|
142
|
+
class NCNN_EXPORT Allocator
|
|
143
|
+
{
|
|
144
|
+
public:
|
|
145
|
+
virtual ~Allocator();
|
|
146
|
+
virtual void* fastMalloc(size_t size) = 0;
|
|
147
|
+
virtual void fastFree(void* ptr) = 0;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
class PoolAllocatorPrivate;
|
|
151
|
+
class NCNN_EXPORT PoolAllocator : public Allocator
|
|
152
|
+
{
|
|
153
|
+
public:
|
|
154
|
+
PoolAllocator();
|
|
155
|
+
~PoolAllocator();
|
|
156
|
+
|
|
157
|
+
// ratio range 0 ~ 1
|
|
158
|
+
// default cr = 0
|
|
159
|
+
void set_size_compare_ratio(float scr);
|
|
160
|
+
|
|
161
|
+
// budget drop threshold
|
|
162
|
+
// default threshold = 10
|
|
163
|
+
void set_size_drop_threshold(size_t);
|
|
164
|
+
|
|
165
|
+
// release all budgets immediately
|
|
166
|
+
void clear();
|
|
167
|
+
|
|
168
|
+
virtual void* fastMalloc(size_t size);
|
|
169
|
+
virtual void fastFree(void* ptr);
|
|
170
|
+
|
|
171
|
+
private:
|
|
172
|
+
PoolAllocator(const PoolAllocator&);
|
|
173
|
+
PoolAllocator& operator=(const PoolAllocator&);
|
|
174
|
+
|
|
175
|
+
private:
|
|
176
|
+
PoolAllocatorPrivate* const d;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
class UnlockedPoolAllocatorPrivate;
|
|
180
|
+
class NCNN_EXPORT UnlockedPoolAllocator : public Allocator
|
|
181
|
+
{
|
|
182
|
+
public:
|
|
183
|
+
UnlockedPoolAllocator();
|
|
184
|
+
~UnlockedPoolAllocator();
|
|
185
|
+
|
|
186
|
+
// ratio range 0 ~ 1
|
|
187
|
+
// default cr = 0
|
|
188
|
+
void set_size_compare_ratio(float scr);
|
|
189
|
+
|
|
190
|
+
// budget drop threshold
|
|
191
|
+
// default threshold = 10
|
|
192
|
+
void set_size_drop_threshold(size_t);
|
|
193
|
+
|
|
194
|
+
// release all budgets immediately
|
|
195
|
+
void clear();
|
|
196
|
+
|
|
197
|
+
virtual void* fastMalloc(size_t size);
|
|
198
|
+
virtual void fastFree(void* ptr);
|
|
199
|
+
|
|
200
|
+
private:
|
|
201
|
+
UnlockedPoolAllocator(const UnlockedPoolAllocator&);
|
|
202
|
+
UnlockedPoolAllocator& operator=(const UnlockedPoolAllocator&);
|
|
203
|
+
|
|
204
|
+
private:
|
|
205
|
+
UnlockedPoolAllocatorPrivate* const d;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
#if NCNN_VULKAN
|
|
209
|
+
|
|
210
|
+
class VulkanDevice;
|
|
211
|
+
|
|
212
|
+
class NCNN_EXPORT VkBufferMemory
|
|
213
|
+
{
|
|
214
|
+
public:
|
|
215
|
+
VkBuffer buffer;
|
|
216
|
+
|
|
217
|
+
// the base offset assigned by allocator
|
|
218
|
+
size_t offset;
|
|
219
|
+
size_t capacity;
|
|
220
|
+
|
|
221
|
+
VkDeviceMemory memory;
|
|
222
|
+
void* mapped_ptr;
|
|
223
|
+
|
|
224
|
+
// buffer state, modified by command functions internally
|
|
225
|
+
mutable VkAccessFlags access_flags;
|
|
226
|
+
mutable VkPipelineStageFlags stage_flags;
|
|
227
|
+
|
|
228
|
+
// initialize and modified by mat
|
|
229
|
+
int refcount;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
class NCNN_EXPORT VkImageMemory
|
|
233
|
+
{
|
|
234
|
+
public:
|
|
235
|
+
VkImage image;
|
|
236
|
+
VkImageView imageview;
|
|
237
|
+
|
|
238
|
+
// underlying info assigned by allocator
|
|
239
|
+
int width;
|
|
240
|
+
int height;
|
|
241
|
+
int depth;
|
|
242
|
+
VkFormat format;
|
|
243
|
+
|
|
244
|
+
VkDeviceMemory memory;
|
|
245
|
+
void* mapped_ptr;
|
|
246
|
+
|
|
247
|
+
// the base offset assigned by allocator
|
|
248
|
+
size_t bind_offset;
|
|
249
|
+
size_t bind_capacity;
|
|
250
|
+
|
|
251
|
+
// image state, modified by command functions internally
|
|
252
|
+
mutable VkAccessFlags access_flags;
|
|
253
|
+
mutable VkImageLayout image_layout;
|
|
254
|
+
mutable VkPipelineStageFlags stage_flags;
|
|
255
|
+
|
|
256
|
+
// in-execution state, modified by command functions internally
|
|
257
|
+
mutable int command_refcount;
|
|
258
|
+
|
|
259
|
+
// initialize and modified by mat
|
|
260
|
+
int refcount;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
class NCNN_EXPORT VkAllocator
|
|
264
|
+
{
|
|
265
|
+
public:
|
|
266
|
+
explicit VkAllocator(const VulkanDevice* _vkdev);
|
|
267
|
+
virtual ~VkAllocator();
|
|
268
|
+
|
|
269
|
+
virtual void clear();
|
|
270
|
+
|
|
271
|
+
virtual VkBufferMemory* fastMalloc(size_t size) = 0;
|
|
272
|
+
virtual void fastFree(VkBufferMemory* ptr) = 0;
|
|
273
|
+
virtual int flush(VkBufferMemory* ptr);
|
|
274
|
+
virtual int invalidate(VkBufferMemory* ptr);
|
|
275
|
+
|
|
276
|
+
virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack) = 0;
|
|
277
|
+
virtual void fastFree(VkImageMemory* ptr) = 0;
|
|
278
|
+
|
|
279
|
+
public:
|
|
280
|
+
const VulkanDevice* vkdev;
|
|
281
|
+
uint32_t buffer_memory_type_index;
|
|
282
|
+
uint32_t image_memory_type_index;
|
|
283
|
+
uint32_t reserved_type_index;
|
|
284
|
+
bool mappable;
|
|
285
|
+
bool coherent;
|
|
286
|
+
|
|
287
|
+
protected:
|
|
288
|
+
VkBuffer create_buffer(size_t size, VkBufferUsageFlags usage);
|
|
289
|
+
VkDeviceMemory allocate_memory(size_t size, uint32_t memory_type_index);
|
|
290
|
+
VkDeviceMemory allocate_dedicated_memory(size_t size, uint32_t memory_type_index, VkImage image, VkBuffer buffer);
|
|
291
|
+
|
|
292
|
+
VkImage create_image(int width, int height, int depth, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage);
|
|
293
|
+
VkImageView create_imageview(VkImage image, VkFormat format);
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
class VkBlobAllocatorPrivate;
|
|
297
|
+
class NCNN_EXPORT VkBlobAllocator : public VkAllocator
|
|
298
|
+
{
|
|
299
|
+
public:
|
|
300
|
+
explicit VkBlobAllocator(const VulkanDevice* vkdev, size_t preferred_block_size = 16 * 1024 * 1024); // 16M
|
|
301
|
+
virtual ~VkBlobAllocator();
|
|
302
|
+
|
|
303
|
+
public:
|
|
304
|
+
// release all budgets immediately
|
|
305
|
+
virtual void clear();
|
|
306
|
+
|
|
307
|
+
virtual VkBufferMemory* fastMalloc(size_t size);
|
|
308
|
+
virtual void fastFree(VkBufferMemory* ptr);
|
|
309
|
+
virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack);
|
|
310
|
+
virtual void fastFree(VkImageMemory* ptr);
|
|
311
|
+
|
|
312
|
+
private:
|
|
313
|
+
VkBlobAllocator(const VkBlobAllocator&);
|
|
314
|
+
VkBlobAllocator& operator=(const VkBlobAllocator&);
|
|
315
|
+
|
|
316
|
+
private:
|
|
317
|
+
VkBlobAllocatorPrivate* const d;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
class VkWeightAllocatorPrivate;
|
|
321
|
+
class NCNN_EXPORT VkWeightAllocator : public VkAllocator
|
|
322
|
+
{
|
|
323
|
+
public:
|
|
324
|
+
explicit VkWeightAllocator(const VulkanDevice* vkdev, size_t preferred_block_size = 8 * 1024 * 1024); // 8M
|
|
325
|
+
virtual ~VkWeightAllocator();
|
|
326
|
+
|
|
327
|
+
public:
|
|
328
|
+
// release all blocks immediately
|
|
329
|
+
virtual void clear();
|
|
330
|
+
|
|
331
|
+
public:
|
|
332
|
+
virtual VkBufferMemory* fastMalloc(size_t size);
|
|
333
|
+
virtual void fastFree(VkBufferMemory* ptr);
|
|
334
|
+
virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack);
|
|
335
|
+
virtual void fastFree(VkImageMemory* ptr);
|
|
336
|
+
|
|
337
|
+
private:
|
|
338
|
+
VkWeightAllocator(const VkWeightAllocator&);
|
|
339
|
+
VkWeightAllocator& operator=(const VkWeightAllocator&);
|
|
340
|
+
|
|
341
|
+
private:
|
|
342
|
+
VkWeightAllocatorPrivate* const d;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
class VkStagingAllocatorPrivate;
|
|
346
|
+
class NCNN_EXPORT VkStagingAllocator : public VkAllocator
|
|
347
|
+
{
|
|
348
|
+
public:
|
|
349
|
+
explicit VkStagingAllocator(const VulkanDevice* vkdev);
|
|
350
|
+
virtual ~VkStagingAllocator();
|
|
351
|
+
|
|
352
|
+
public:
|
|
353
|
+
// ratio range 0 ~ 1
|
|
354
|
+
// default cr = 0.75
|
|
355
|
+
void set_size_compare_ratio(float scr);
|
|
356
|
+
|
|
357
|
+
// release all budgets immediately
|
|
358
|
+
virtual void clear();
|
|
359
|
+
|
|
360
|
+
virtual VkBufferMemory* fastMalloc(size_t size);
|
|
361
|
+
virtual void fastFree(VkBufferMemory* ptr);
|
|
362
|
+
virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack);
|
|
363
|
+
virtual void fastFree(VkImageMemory* ptr);
|
|
364
|
+
|
|
365
|
+
private:
|
|
366
|
+
VkStagingAllocator(const VkStagingAllocator&);
|
|
367
|
+
VkStagingAllocator& operator=(const VkStagingAllocator&);
|
|
368
|
+
|
|
369
|
+
private:
|
|
370
|
+
VkStagingAllocatorPrivate* const d;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
class VkWeightStagingAllocatorPrivate;
|
|
374
|
+
class NCNN_EXPORT VkWeightStagingAllocator : public VkAllocator
|
|
375
|
+
{
|
|
376
|
+
public:
|
|
377
|
+
explicit VkWeightStagingAllocator(const VulkanDevice* vkdev);
|
|
378
|
+
virtual ~VkWeightStagingAllocator();
|
|
379
|
+
|
|
380
|
+
public:
|
|
381
|
+
virtual VkBufferMemory* fastMalloc(size_t size);
|
|
382
|
+
virtual void fastFree(VkBufferMemory* ptr);
|
|
383
|
+
virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack);
|
|
384
|
+
virtual void fastFree(VkImageMemory* ptr);
|
|
385
|
+
|
|
386
|
+
private:
|
|
387
|
+
VkWeightStagingAllocator(const VkWeightStagingAllocator&);
|
|
388
|
+
VkWeightStagingAllocator& operator=(const VkWeightStagingAllocator&);
|
|
389
|
+
|
|
390
|
+
private:
|
|
391
|
+
VkWeightStagingAllocatorPrivate* const d;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
#if NCNN_PLATFORM_API
|
|
395
|
+
#if __ANDROID_API__ >= 26
|
|
396
|
+
class NCNN_EXPORT VkAndroidHardwareBufferImageAllocator : public VkAllocator
|
|
397
|
+
{
|
|
398
|
+
public:
|
|
399
|
+
VkAndroidHardwareBufferImageAllocator(const VulkanDevice* _vkdev, AHardwareBuffer* _hb);
|
|
400
|
+
virtual ~VkAndroidHardwareBufferImageAllocator();
|
|
401
|
+
|
|
402
|
+
public:
|
|
403
|
+
virtual VkBufferMemory* fastMalloc(size_t size);
|
|
404
|
+
virtual void fastFree(VkBufferMemory* ptr);
|
|
405
|
+
virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack);
|
|
406
|
+
virtual void fastFree(VkImageMemory* ptr);
|
|
407
|
+
|
|
408
|
+
private:
|
|
409
|
+
VkAndroidHardwareBufferImageAllocator(const VkAndroidHardwareBufferImageAllocator&);
|
|
410
|
+
VkAndroidHardwareBufferImageAllocator& operator=(const VkAndroidHardwareBufferImageAllocator&);
|
|
411
|
+
|
|
412
|
+
public:
|
|
413
|
+
int init();
|
|
414
|
+
|
|
415
|
+
int width() const;
|
|
416
|
+
int height() const;
|
|
417
|
+
uint64_t external_format() const;
|
|
418
|
+
|
|
419
|
+
public:
|
|
420
|
+
AHardwareBuffer* hb;
|
|
421
|
+
AHardwareBuffer_Desc bufferDesc;
|
|
422
|
+
VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties;
|
|
423
|
+
VkAndroidHardwareBufferPropertiesANDROID bufferProperties;
|
|
424
|
+
VkSamplerYcbcrConversionKHR samplerYcbcrConversion;
|
|
425
|
+
};
|
|
426
|
+
#endif // __ANDROID_API__ >= 26
|
|
427
|
+
#endif // NCNN_PLATFORM_API
|
|
428
|
+
|
|
429
|
+
#endif // NCNN_VULKAN
|
|
430
|
+
|
|
431
|
+
} // namespace ncnn
|
|
432
|
+
|
|
433
|
+
#endif // NCNN_ALLOCATOR_H
|