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 all 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 @@ -42,6 +36,17 @@ extern const char src_vmaf_4k_v0_6_1neg_json;
extern const int src_vmaf_4k_v0_6_1neg_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 @@ -322,3 +327,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
24 changes: 24 additions & 0 deletions libvmaf/test/test_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,29 @@ static char *test_model_set_flags()
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 *next = NULL;
VmafModelConfig config = {.name = NULL, .flags = VMAF_MODEL_FLAGS_DEFAULT};
while (next = vmaf_model_descriptor_next(next))
{
VmafBuiltInModel *builtin = next;
mu_assert("Version field should match on both builtin and next", next->version == builtin->version);
VmafModel *model;
printf(next->version);
printf("\n");
int err = vmaf_model_load(&model, &config, next->version);

mu_assert("Model load should not return an error",err==0);
vmaf_model_destroy(model);
}
return NULL;
}

char *run_tests()
{
mu_run_test(test_json_model);
Expand All @@ -382,5 +405,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;
}