Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose builtin models #1129

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions libvmaf/include/libvmaf/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ typedef struct VmafModelCollectionScore {
} bootstrap;
} VmafModelCollectionScore;

typedef struct VmafModelDescriptor{
const char *version;
} VmafModelDescriptor;

/**
* Iterate through all built in vmaf models
*
* @param prev previous model. NULL to get the first model
*
* @return next model or NULL after the last model
*
*/
const VmafModelDescriptor *vmaf_model_descriptor_next(const VmafModelDescriptor *prev);

int vmaf_model_collection_load(VmafModel **model,
VmafModelCollection **model_collection,
VmafModelConfig *cfg,
Expand Down
29 changes: 23 additions & 6 deletions libvmaf/src/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
#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;
Expand All @@ -40,6 +34,17 @@ extern const char src_vmaf_4k_v0_6_1_json;
extern const int src_vmaf_4k_v0_6_1_json_len;
#endif

/**
* For the purposes of working with vmaf_model_descriptor_next,
* the version property of VmafBuiltInModel must be the first property
* declared on this struct
*/
typedef struct VmafBuiltInModel {
const char *version;
const char *data;
const int *data_len;
} VmafBuiltInModel;

static const VmafBuiltInModel built_in_models[] = {
#if VMAF_BUILT_IN_MODELS
#if VMAF_FLOAT_FEATURES
Expand Down Expand Up @@ -316,3 +321,15 @@ int vmaf_model_collection_feature_overload(VmafModel *model,
return err;
}

const VmafModelDescriptor *vmaf_model_descriptor_next(const VmafModelDescriptor *prev){
// Cast *VmafModelDescriptor into *VmafBuiltInModel
VmafBuiltInModel *builtin = (VmafBuiltInModel*)prev;

if(!builtin)
return (VmafModelDescriptor*)&built_in_models[0];

if(builtin - built_in_models < BUILT_IN_MODEL_CNT)
return (VmafModelDescriptor*)(builtin + 1);

return NULL;
}
10 changes: 10 additions & 0 deletions libvmaf/src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ typedef struct VmafModelCollection {
const char *name;
} VmafModelCollection;

/**
* Iterate through all built in vmaf models
*
* @param prev previous model. NULL to get the first model
*
* @return next model or NULL after the last model
*
*/
const VmafModelDescriptor *vmaf_model_descriptor_next(const VmafModelDescriptor *prev);

char *vmaf_model_generate_name(VmafModelConfig *cfg);

int vmaf_model_collection_append(VmafModelCollection **model_collection,
Expand Down
120 changes: 77 additions & 43 deletions libvmaf/test/test_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ static int model_compare(VmafModel *model_a, VmafModel *model_b)
{
int err = 0;

//err += model_a->type != model_b->type;
// err += model_a->type != model_b->type;

err += model_a->slope != model_b->slope;
err += model_a->intercept != model_b->intercept;

err += model_a->n_features != model_b->n_features;
for (unsigned i = 0; i < model_a->n_features; i++) {
//err += strcmp(model_a->feature[i].name, model_b->feature[i].name) != 0;
err += model_a->feature[i].slope != model_b->feature[i].slope;
err += model_a->feature[i].intercept != model_b->feature[i].intercept;
err += !model_a->feature[i].opts_dict != !model_b->feature[i].opts_dict;
for (unsigned i = 0; i < model_a->n_features; i++)
{
// err += strcmp(model_a->feature[i].name, model_b->feature[i].name) != 0;
err += model_a->feature[i].slope != model_b->feature[i].slope;
err += model_a->feature[i].intercept != model_b->feature[i].intercept;
err += !model_a->feature[i].opts_dict != !model_b->feature[i].opts_dict;
}

err += model_a->score_clip.enabled != model_b->score_clip.enabled;
Expand All @@ -54,7 +55,8 @@ static int model_compare(VmafModel *model_a, VmafModel *model_b)
err += model_a->score_transform.p2.enabled != model_b->score_transform.p2.enabled;
err += model_a->score_transform.p2.value != model_b->score_transform.p2.value;
err += model_a->score_transform.knots.enabled != model_b->score_transform.knots.enabled;
for (unsigned i = 0; i < model_a->score_transform.knots.n_knots; i++) {
for (unsigned i = 0; i < model_a->score_transform.knots.n_knots; i++)
{
err += model_a->score_transform.knots.list[i].x != model_b->score_transform.knots.list[i].x;
err += model_a->score_transform.knots.list[i].y != model_b->score_transform.knots.list[i].y;
}
Expand All @@ -69,14 +71,14 @@ static char *test_json_model()
int err = 0;

VmafModel *model_json;
VmafModelConfig cfg_json = { 0 };
const char *path_json = JSON_MODEL_PATH"vmaf_v0.6.1neg.json";
VmafModelConfig cfg_json = {0};
const char *path_json = JSON_MODEL_PATH "vmaf_v0.6.1neg.json";

err = vmaf_read_json_model_from_path(&model_json, &cfg_json, path_json);
mu_assert("problem during vmaf_read_json_model", !err);

VmafModel *model;
VmafModelConfig cfg = { 0 };
VmafModelConfig cfg = {0};
const char *version = "vmaf_v0.6.1neg";

err = vmaf_model_load(&model, &cfg, version);
Expand All @@ -96,14 +98,14 @@ static char *test_built_in_model()
int err = 0;

VmafModel *model;
VmafModelConfig cfg = { 0 };
VmafModelConfig cfg = {0};
const char *version = "vmaf_v0.6.1neg";
err = vmaf_model_load(&model, &cfg, version);
mu_assert("problem during vmaf_model_load", !err);

VmafModel *model_file;
VmafModelConfig cfg_file = { 0 };
const char *path = JSON_MODEL_PATH"vmaf_v0.6.1neg.json";
VmafModelConfig cfg_file = {0};
const char *path = JSON_MODEL_PATH "vmaf_v0.6.1neg.json";
err = vmaf_model_load_from_path(&model_file, &cfg_file, path);
mu_assert("problem during vmaf_model_load_from_path", !err);

Expand All @@ -121,8 +123,8 @@ static char *test_model_load_and_destroy()
int err;

VmafModel *model;
VmafModelConfig cfg = { 0 };
const char *path = JSON_MODEL_PATH"vmaf_float_v0.6.1.json";
VmafModelConfig cfg = {0};
const char *path = JSON_MODEL_PATH "vmaf_float_v0.6.1.json";
err = vmaf_model_load_from_path(&model, &cfg, path);
mu_assert("problem during vmaf_model_load_from_path", !err);

Expand All @@ -145,7 +147,7 @@ static char *test_model_feature()
int err;

VmafModel *model;
VmafModelConfig cfg = { 0 };
VmafModelConfig cfg = {0};
const char *version = "vmaf_v0.6.1";
err = vmaf_model_load(&model, &cfg, version);
mu_assert("problem during vmaf_model_load", !err);
Expand Down Expand Up @@ -173,13 +175,13 @@ static char *test_model_feature()

const VmafDictionaryEntry *e =
vmaf_dictionary_get(&model->feature[0].opts_dict,
"adm_enhancement_gain_limit", 0);
"adm_enhancement_gain_limit", 0);
mu_assert("dict should have a new key/val pair",
!strcmp(e->key, "adm_enhancement_gain_limit") &&
!strcmp(e->val, "1.1"));
!strcmp(e->val, "1.1"));

VmafModel *model_neg;
VmafModelConfig cfg_neg = { 0 };
VmafModelConfig cfg_neg = {0};
const char *version_neg = "vmaf_v0.6.1neg";
err = vmaf_model_load(&model_neg, &cfg_neg, version_neg);
mu_assert("problem during vmaf_model_load", !err);
Expand All @@ -200,10 +202,10 @@ static char *test_model_feature()
model->feature[0].opts_dict);
const VmafDictionaryEntry *e2 =
vmaf_dictionary_get(&model->feature[0].opts_dict,
"adm_enhancement_gain_limit", 0);
"adm_enhancement_gain_limit", 0);
mu_assert("dict should have an existing key/val pair",
!strcmp(e2->key, "adm_enhancement_gain_limit") &&
!strcmp(e2->val, "1.1"));
!strcmp(e2->val, "1.1"));

err = vmaf_model_feature_overload(model, "adm", dict_neg);

Expand All @@ -215,10 +217,10 @@ static char *test_model_feature()
model->feature[0].opts_dict);
const VmafDictionaryEntry *e3 =
vmaf_dictionary_get(&model->feature[0].opts_dict,
"adm_enhancement_gain_limit", 0);
"adm_enhancement_gain_limit", 0);
mu_assert("dict should have an updated key/val pair",
!strcmp(e3->key, "adm_enhancement_gain_limit") &&
!strcmp(e3->val, "1.2"));
!strcmp(e3->val, "1.2"));

vmaf_model_destroy(model);
vmaf_model_destroy(model_neg);
Expand All @@ -234,7 +236,7 @@ static char *test_model_check_default_behavior_unset_flags()
VmafModelConfig cfg = {
.name = "some_vmaf",
};
const char *path = JSON_MODEL_PATH"vmaf_float_v0.6.1.json";
const char *path = JSON_MODEL_PATH "vmaf_float_v0.6.1.json";
err = vmaf_model_load_from_path(&model, &cfg, path);
mu_assert("problem during vmaf_model_load_from_path", !err);
mu_assert("Model name is inconsistent.\n", !strcmp(model->name, "some_vmaf"));
Expand All @@ -258,7 +260,7 @@ static char *test_model_check_default_behavior_set_flags()
.name = "some_vmaf",
.flags = VMAF_MODEL_FLAGS_DEFAULT,
};
const char *path = JSON_MODEL_PATH"vmaf_float_v0.6.1.json";
const char *path = JSON_MODEL_PATH "vmaf_float_v0.6.1.json";
err = vmaf_model_load_from_path(&model, &cfg, path);
mu_assert("problem during vmaf_model_load_from_path", !err);
mu_assert("Model name is inconsistent.\n", !strcmp(model->name, "some_vmaf"));
Expand All @@ -281,7 +283,7 @@ static char *test_model_set_flags()
VmafModelConfig cfg1 = {
.flags = VMAF_MODEL_FLAG_ENABLE_TRANSFORM,
};
const char *path1 = JSON_MODEL_PATH"vmaf_float_v0.6.1.json";
const char *path1 = JSON_MODEL_PATH "vmaf_float_v0.6.1.json";
err = vmaf_model_load_from_path(&model1, &cfg1, path1);
mu_assert("problem during vmaf_model_load_from_path", !err);
mu_assert("Score transform must be enabled.\n",
Expand All @@ -294,7 +296,7 @@ static char *test_model_set_flags()
VmafModelConfig cfg2 = {
.flags = VMAF_MODEL_FLAG_DISABLE_CLIP,
};
const char *path2 = JSON_MODEL_PATH"vmaf_float_v0.6.1.json";
const char *path2 = JSON_MODEL_PATH "vmaf_float_v0.6.1.json";
err = vmaf_model_load_from_path(&model2, &cfg2, path2);
mu_assert("problem during vmaf_model_load_from_path", !err);
mu_assert("Score transform must be disabled.\n",
Expand All @@ -303,9 +305,9 @@ static char *test_model_set_flags()
!model2->score_clip.enabled);
vmaf_model_destroy(model2);

VmafModel *model3;
VmafModelConfig cfg3 = { 0 };
const char *path3 = JSON_MODEL_PATH"vmaf_float_v0.6.1.json";
VmafModel *model3;
VmafModelConfig cfg3 = {0};
const char *path3 = JSON_MODEL_PATH "vmaf_float_v0.6.1.json";
err = vmaf_model_load_from_path(&model3, &cfg3, path3);
mu_assert("problem during vmaf_model_load_from_path", !err);
mu_assert("feature[0].opts_dict must be NULL.\n",
Expand All @@ -322,9 +324,9 @@ static char *test_model_set_flags()
!model3->feature[5].opts_dict);
vmaf_model_destroy(model3);

VmafModel *model4;
VmafModelConfig cfg4 = { 0 };
const char *path4 = JSON_MODEL_PATH"vmaf_float_v0.6.1neg.json";
VmafModel *model4;
VmafModelConfig cfg4 = {0};
const char *path4 = JSON_MODEL_PATH "vmaf_float_v0.6.1neg.json";
err = vmaf_model_load_from_path(&model4, &cfg4, path4);
mu_assert("problem during vmaf_model_load_from_path", !err);
mu_assert("feature[0].opts_dict must not be NULL.\n",
Expand All @@ -343,34 +345,65 @@ static char *test_model_set_flags()
const VmafDictionaryEntry *entry = NULL;
entry = vmaf_dictionary_get(&model4->feature[0].opts_dict, "adm_enhn_gain_limit", 0);
mu_assert("feature[0].opts_dict must have key adm_enhn_gain_limit.\n",
strcmp(entry->key, "adm_enhn_gain_limit")==0);
strcmp(entry->key, "adm_enhn_gain_limit") == 0);
mu_assert("feature[0].opts_dict[\"adm_enhn_gain_limit\"] must have value 1.\n",
strcmp(entry->val, "1")==0);
strcmp(entry->val, "1") == 0);
entry = vmaf_dictionary_get(&model4->feature[2].opts_dict, "vif_enhn_gain_limit", 0);
mu_assert("feature[2].opts_dict must have key vif_enhn_gain_limit.\n",
strcmp(entry->key, "vif_enhn_gain_limit")==0);
strcmp(entry->key, "vif_enhn_gain_limit") == 0);
mu_assert("feature[2].opts_dict[\"vif_enhn_gain_limit\"] must have value 1.\n",
strcmp(entry->val, "1")==0);
strcmp(entry->val, "1") == 0);
entry = vmaf_dictionary_get(&model4->feature[3].opts_dict, "vif_enhn_gain_limit", 0);
mu_assert("feature[3].opts_dict must have key vif_enhn_gain_limit.\n",
strcmp(entry->key, "vif_enhn_gain_limit")==0);
strcmp(entry->key, "vif_enhn_gain_limit") == 0);
mu_assert("feature[3].opts_dict[\"vif_enhn_gain_limit\"] must have value 1.\n",
strcmp(entry->val, "1")==0);
strcmp(entry->val, "1") == 0);
entry = vmaf_dictionary_get(&model4->feature[4].opts_dict, "vif_enhn_gain_limit", 0);
mu_assert("feature[4].opts_dict must have key vif_enhn_gain_limit.\n",
strcmp(entry->key, "vif_enhn_gain_limit")==0);
strcmp(entry->key, "vif_enhn_gain_limit") == 0);
mu_assert("feature[4].opts_dict[\"vif_enhn_gain_limit\"] must have value 1.\n",
strcmp(entry->val, "1")==0);
strcmp(entry->val, "1") == 0);
entry = vmaf_dictionary_get(&model4->feature[5].opts_dict, "vif_enhn_gain_limit", 0);
mu_assert("feature[5].opts_dict must have key vif_enhn_gain_limit.\n",
strcmp(entry->key, "vif_enhn_gain_limit")==0);
strcmp(entry->key, "vif_enhn_gain_limit") == 0);
mu_assert("feature[5].opts_dict[\"vif_enhn_gain_limit\"] must have value 1.\n",
strcmp(entry->val, "1")==0);
strcmp(entry->val, "1") == 0);

vmaf_model_destroy(model4);
return NULL;
}

/**
* This test may fail if VmafBuiltInModel's memory layout has changed
* (version MUST be the first defined property in the struct)
*/
char *test_model_descriptor_next()
{
VmafModelDescriptor *first = vmaf_model_descriptor_next(NULL);
VmafBuiltInModel *builtin = first;
mu_assert("When given null, return first item",first == &built_in_models[0]);
mu_assert("Version field matches on the first VmafBuiltInModel",first->version == builtin->version);

for(int i=1;i==BUILT_IN_MODEL_CNT;i++){
ThatNerdUKnow marked this conversation as resolved.
Show resolved Hide resolved
VmafBuiltInModel *builtin = &built_in_models[i];
VmafModelDescriptor *current = builtin;
VmafBuiltInModel *expected = &built_in_models[i+1];

VmafModelDescriptor *next = vmaf_model_descriptor_next(current);

mu_assert("Pointer returned from vmaf_model_descriptor_next is not null",next);
mu_assert("Given the address of a VmafBuiltInModel, returns the address of the next VmafBuiltInModel",next==expected);
mu_assert("Version field should match on both pointers",expected->version == next->version);
}

VmafModelDescriptor *last = &built_in_models[BUILT_IN_MODEL_CNT];

VmafModelDescriptor *shouldBeNull = vmaf_model_descriptor_next(last);

mu_assert("When given the last item in the array, returns null",!shouldBeNull);
return NULL;
}

char *run_tests()
{
mu_run_test(test_json_model);
Expand All @@ -382,5 +415,6 @@ char *run_tests()
mu_run_test(test_model_check_default_behavior_set_flags);
mu_run_test(test_model_set_flags);
mu_run_test(test_model_feature);
mu_run_test(test_model_descriptor_next);
return NULL;
}