-
Notifications
You must be signed in to change notification settings - Fork 818
Expand file tree
/
Copy pathmodel.c
More file actions
338 lines (300 loc) · 10.2 KB
/
model.c
File metadata and controls
338 lines (300 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <libvmaf/model.h>
#include "config.h"
#include "feature/feature_extractor.h"
#include "log.h"
#include "model.h"
#include "read_json_model.h"
#include "svm.h"
typedef struct VmafBuiltInModel {
const char *version;
const char *data;
const int *data_len;
} VmafBuiltInModel;
#if VMAF_BUILT_IN_MODELS
#if VMAF_FLOAT_FEATURES
extern const char src_vmaf_float_v0_6_1neg_json[];
extern const int src_vmaf_float_v0_6_1neg_json_len;
extern const char src_vmaf_float_v0_6_1_json[];
extern const int src_vmaf_float_v0_6_1_json_len;
extern const char src_vmaf_float_b_v0_6_3_json[];
extern const int src_vmaf_float_b_v0_6_3_json_len;
extern const char src_vmaf_float_4k_v0_6_1_json[];
extern const int src_vmaf_float_4k_v0_6_1_json_len;
#endif
extern const char src_vmaf_v0_6_1_json[];
extern const int src_vmaf_v0_6_1_json_len;
extern const char src_vmaf_b_v0_6_3_json[];
extern const int src_vmaf_b_v0_6_3_json_len;
extern const char src_vmaf_v0_6_1neg_json[];
extern const int src_vmaf_v0_6_1neg_json_len;
extern const char src_vmaf_4k_v0_6_1_json[];
extern const int src_vmaf_4k_v0_6_1_json_len;
extern const char src_vmaf_4k_v0_6_1neg_json[];
extern const int src_vmaf_4k_v0_6_1neg_json_len;
#endif
static const VmafBuiltInModel built_in_models[] = {
#if VMAF_BUILT_IN_MODELS
#if VMAF_FLOAT_FEATURES
{
.version = "vmaf_float_v0.6.1",
.data = src_vmaf_float_v0_6_1_json,
.data_len = &src_vmaf_float_v0_6_1_json_len,
},
{
.version = "vmaf_float_b_v0.6.3",
.data = src_vmaf_float_b_v0_6_3_json,
.data_len = &src_vmaf_float_b_v0_6_3_json_len,
},
{
.version = "vmaf_float_v0.6.1neg",
.data = src_vmaf_float_v0_6_1neg_json,
.data_len = &src_vmaf_float_v0_6_1neg_json_len,
},
{
.version = "vmaf_float_4k_v0.6.1",
.data = src_vmaf_float_4k_v0_6_1_json,
.data_len = &src_vmaf_float_4k_v0_6_1_json_len,
},
#endif
{
.version = "vmaf_v0.6.1",
.data = src_vmaf_v0_6_1_json,
.data_len = &src_vmaf_v0_6_1_json_len,
},
{
.version = "vmaf_b_v0.6.3",
.data = src_vmaf_b_v0_6_3_json,
.data_len = &src_vmaf_b_v0_6_3_json_len,
},
{
.version = "vmaf_v0.6.1neg",
.data = src_vmaf_v0_6_1neg_json,
.data_len = &src_vmaf_v0_6_1neg_json_len,
},
{
.version = "vmaf_4k_v0.6.1",
.data = src_vmaf_4k_v0_6_1_json,
.data_len = &src_vmaf_4k_v0_6_1_json_len,
},
{
.version = "vmaf_4k_v0.6.1neg",
.data = src_vmaf_4k_v0_6_1neg_json,
.data_len = &src_vmaf_4k_v0_6_1neg_json_len,
},
#endif
{ 0 }
};
#define BUILT_IN_MODEL_CNT \
((sizeof(built_in_models)) / (sizeof(built_in_models[0]))) - 1
int vmaf_model_load(VmafModel **model, VmafModelConfig *cfg,
const char *version)
{
const VmafBuiltInModel *built_in_model = NULL;
for (unsigned i = 0; i < BUILT_IN_MODEL_CNT; i++) {
if (!strcmp(version, built_in_models[i].version)) {
built_in_model = &built_in_models[i];
break;
}
}
if (!built_in_model) {
vmaf_log(VMAF_LOG_LEVEL_WARNING,
"no such built-in model: \"%s\"\n", version);
return -EINVAL;
}
return vmaf_read_json_model_from_buffer(model, cfg, built_in_model->data,
*built_in_model->data_len);
}
char *vmaf_model_generate_name(VmafModelConfig *cfg)
{
const char *default_name = "vmaf";
const size_t name_sz =
cfg->name ? strlen(cfg->name) + 1 : strlen(default_name) + 1;
char *name = malloc(name_sz);
if (!name) return NULL;
memset(name, 0, name_sz);
if (!cfg->name)
strcpy(name, default_name);
else
strcpy(name, cfg->name);
return name;
}
int vmaf_model_load_from_path(VmafModel **model, VmafModelConfig *cfg,
const char *path)
{
int err = vmaf_read_json_model_from_path(model, cfg, path);
if (err) {
vmaf_log(VMAF_LOG_LEVEL_ERROR,
"could not read model from path: \"%s\"\n", path);
char *ext = strrchr(path, '.');
if (ext && !strcmp(ext, ".pkl")) {
vmaf_log(
VMAF_LOG_LEVEL_ERROR,
"support for pkl model files has been removed, use json\n");
}
}
return err;
}
int vmaf_model_feature_overload(VmafModel *model, const char *feature_name,
VmafFeatureDictionary *opts_dict)
{
if (!model) return -EINVAL;
if (!feature_name) return -EINVAL;
if (!opts_dict) return -EINVAL;
int err = 0;
for (unsigned i = 0; i < model->n_features; i++) {
VmafFeatureExtractor *fex =
vmaf_get_feature_extractor_by_feature_name(model->feature[i].name, 0);
if (!fex) continue;
if (strcmp(feature_name, fex->name)) continue;
VmafDictionary *d =
vmaf_dictionary_merge((VmafDictionary**)&model->feature[i].opts_dict,
(VmafDictionary**)&opts_dict, 0);
if (!d) return -ENOMEM;
err = vmaf_dictionary_free(&model->feature[i].opts_dict);
if (err) goto exit;
model->feature[i].opts_dict = d;
}
exit:
err |= vmaf_dictionary_free((VmafDictionary**)&opts_dict);
return err;
}
void vmaf_model_destroy(VmafModel *model)
{
if (!model) return;
free(model->path);
free(model->name);
svm_free_and_destroy_model(&(model->svm));
for (unsigned i = 0; i < model->n_features; i++) {
free(model->feature[i].name);
vmaf_dictionary_free(&model->feature[i].opts_dict);
}
free(model->feature);
free(model->score_transform.knots.list);
free(model);
}
int vmaf_model_collection_append(VmafModelCollection **model_collection,
VmafModel *model)
{
if (!model_collection) return -EINVAL;
if (!model) return -EINVAL;
VmafModelCollection *mc = *model_collection;
if (!mc) {
mc = *model_collection = malloc(sizeof(*mc));
if (!mc) goto fail;
memset(mc, 0, sizeof(*mc));
const size_t initial_sz = 8 * sizeof(*mc->model);
mc->model = malloc(initial_sz);
if (!mc->model) goto fail_mc;
memset(mc->model, 0, initial_sz);
mc->size = 8;
mc->type = model->type;
const size_t name_sz = strlen(model->name) - 5 + 1;
mc->name = malloc(name_sz);
if (!mc->name) goto fail_model;
memset((char*)mc->name, 0, name_sz);
strncpy((char*)mc->name, model->name, name_sz - 1);
}
if (mc->type != model->type) return -EINVAL;
if (mc->cnt == mc->size) {
const size_t sz = mc->size * sizeof(*mc->model) * 2;
VmafModel **m = realloc(mc->model, sz);
if (!m) goto fail;
mc->model = m;
mc->size *= 2;
}
mc->model[mc->cnt++] = model;
return 0;
fail_model:
free(mc->model);
fail_mc:
free(mc);
fail:
*model_collection = NULL;
return -ENOMEM;
}
void vmaf_model_collection_destroy(VmafModelCollection *model_collection)
{
if (!model_collection) return;
for (unsigned i = 0; i < model_collection->cnt; i++)
vmaf_model_destroy(model_collection->model[i]);
free(model_collection->model);
free((char*)model_collection->name);
free(model_collection);
}
int vmaf_model_collection_load(VmafModel **model,
VmafModelCollection **model_collection,
VmafModelConfig *cfg,
const char *version)
{
const VmafBuiltInModel *built_in_model = NULL;
for (unsigned i = 0; i < BUILT_IN_MODEL_CNT; i++) {
if (!strcmp(version, built_in_models[i].version)) {
built_in_model = &built_in_models[i];
break;
}
}
if (!built_in_model) {
vmaf_log(VMAF_LOG_LEVEL_WARNING,
"no such built-in model collection: \"%s\"\n", version);
return -EINVAL;
}
return vmaf_read_json_model_collection_from_buffer(model, model_collection,
cfg, built_in_model->data,
*built_in_model->data_len);
}
int vmaf_model_collection_load_from_path(VmafModel **model,
VmafModelCollection **model_collection,
VmafModelConfig *cfg,
const char *path)
{
int err =
vmaf_read_json_model_collection_from_path(model, model_collection,
cfg, path);
if (err) {
vmaf_log(VMAF_LOG_LEVEL_ERROR,
"could not read model collection from path: \"%s\"\n", path);
char *ext = strrchr(path, '.');
if (ext && !strcmp(ext, ".pkl")) {
vmaf_log(
VMAF_LOG_LEVEL_ERROR,
"support for pkl model files has been removed, use json\n");
}
}
return err;
}
int vmaf_model_collection_feature_overload(VmafModel *model,
VmafModelCollection **model_collection,
const char *feature_name,
VmafFeatureDictionary *opts_dict)
{
if (!model_collection) return -EINVAL;
VmafModelCollection *mc = *model_collection;
int err = 0;
for (unsigned i = 0; i < mc->cnt; i++) {
VmafFeatureDictionary *d = NULL;
if (vmaf_dictionary_copy((VmafDictionary**)&opts_dict, (VmafDictionary**)&d)) goto exit;
err |= vmaf_model_feature_overload(mc->model[i], feature_name, d);
}
exit:
err |= vmaf_model_feature_overload(model, feature_name, opts_dict);
return err;
}
const void* vmaf_model_version_next(const void* prev, const char** version){
VmafBuiltInModel* prev_model = prev;
VmafBuiltInModel* out_model = NULL;
if (!prev_model){
out_model = &built_in_models[0];
}
if(prev_model - built_in_models < BUILT_IN_MODEL_CNT){
out_model = prev_model + 1;
}
if (version && out_model){
*version = out_model->version;
}
return out_model;
}