Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 18 additions & 6 deletions libvmaf/src/read_json_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,9 @@ static int model_parse(json_stream *s, VmafModel *model,
return err;
}

static int vmaf_read_json_model(VmafModel **model, VmafModelConfig *cfg,
json_stream *s)
static int model_alloc_and_parse(VmafModel *m, VmafModelConfig *cfg,
json_stream *s)
{
VmafModel *const m = *model = malloc(sizeof(*m));
if (!m) return -ENOMEM;
memset(m, 0, sizeof(*m));

const size_t model_sz = sizeof(*m->feature) * MAX_FEATURE_COUNT;
m->feature = malloc(model_sz);
if (!m->feature) return -ENOMEM;
Expand All @@ -466,6 +462,22 @@ static int vmaf_read_json_model(VmafModel **model, VmafModelConfig *cfg,
return model_parse(s, m, cfg->flags);
}

static int vmaf_read_json_model(VmafModel **model, VmafModelConfig *cfg,
json_stream *s)
{
VmafModel *const m = *model = malloc(sizeof(*m));
if (!m) return -ENOMEM;
memset(m, 0, sizeof(*m));

const int err = model_alloc_and_parse(m, cfg, s);
if (err) {
// do not hand a partially built model back to the caller
vmaf_model_destroy(m);
*model = NULL;
}
return err;
}

int vmaf_read_json_model_from_buffer(VmafModel **model, VmafModelConfig *cfg,
const char *data, const int data_len)
{
Expand Down
15 changes: 15 additions & 0 deletions libvmaf/test/test_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ static char *test_json_model()
return NULL;
}

static char *test_json_model_failed_read_leaves_no_model()
{
VmafModel *model = NULL;
VmafModelConfig cfg = { 0 };
// a model collection is not a model: the vmaf tool tries this read first
const char *path = JSON_MODEL_PATH"vmaf_b_v0.6.3.json";

int err = vmaf_read_json_model_from_path(&model, &cfg, path);
mu_assert("a model collection was read as a single model", err);
mu_assert("a failed read handed back a partially built model", !model);

return NULL;
}

#if VMAF_BUILT_IN_MODELS
static char *test_built_in_model()
{
Expand Down Expand Up @@ -374,6 +388,7 @@ static char *test_model_set_flags()
char *run_tests()
{
mu_run_test(test_json_model);
mu_run_test(test_json_model_failed_read_leaves_no_model);
#if VMAF_BUILT_IN_MODELS
mu_run_test(test_built_in_model);
#endif
Expand Down