Skip to content

Commit

Permalink
fix: bugfix for adding/removing models.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiğithan Yiğit authored and Yiğithan Yiğit committed Feb 2, 2025
1 parent 6280b59 commit ad2435a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 33 deletions.
41 changes: 24 additions & 17 deletions libvmaf/src/feature/feature_collector.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,17 @@ int vmaf_feature_collector_mount_model(VmafFeatureCollector *feature_collector,

VmafPredictModel *m = malloc(sizeof(VmafPredictModel));
if (!m) return -ENOMEM;

m->model = model;
m->next = NULL;

VmafPredictModel **head = &feature_collector->models;
while (*head && (*head)->next != NULL)
*head = (*head)->next;

if (!(*head))
*head = m;
else
(*head)->next = m;
VmafPredictModel *head = feature_collector->models;
if (!head) {
feature_collector->models = m;
} else {
while (head->next) head = head->next;
head->next = m;
}

return 0;
}
Expand All @@ -263,17 +263,24 @@ int vmaf_feature_collector_unmount_model(VmafFeatureCollector *feature_collector
if (!feature_collector) return -EINVAL;
if (!model) return -EINVAL;

VmafPredictModel **head = &feature_collector->models;
while (*head && (*head)->model != model)
head = &(*head)->next;
VmafPredictModel *head = feature_collector->models;
VmafPredictModel *prev = NULL;

if (!(*head)) return -EINVAL;

VmafPredictModel *m = *head;
*head = m->next;
free(m);
while (head) {
if (head->model == model) {
if (prev) {
prev->next = head->next;
} else {
feature_collector->models = head->next;
}
free(head);
return 0;
}
prev = head;
head = head->next;
}

return 0;
return -ENOENT;
}

int vmaf_feature_collector_register_metadata(VmafFeatureCollector *feature_collector,
Expand Down
109 changes: 93 additions & 16 deletions libvmaf/test/test_feature_collector.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,58 @@ static char* test_model_mount()
err = vmaf_feature_collector_init(&feature_collector);
mu_assert("problem during vmaf_feature_collector_init", !err);

VmafModelConfig model_cfg = { 0 };
VmafModel *model;
vmaf_model_load(&model, &model_cfg, "vmaf_v0.6.1");
mu_assert("problem during vmaf_model_load", model);
char *model_names[] = {"vmaf_0", "vmaf_1", "vmaf_2"};

VmafModelConfig model_cfg = {
.name = model_names[0],
.flags = VMAF_MODEL_FLAGS_DEFAULT,
};

VmafModelConfig model_cfg1 = {
.name = model_names[1],
.flags = VMAF_MODEL_FLAGS_DEFAULT,
};

err = vmaf_feature_collector_mount_model(feature_collector, model);
VmafModelConfig model_cfg2 = {
.name = model_names[2],
.flags = VMAF_MODEL_FLAGS_DEFAULT,
};

VmafModel *model0, *model1, *model2;
vmaf_model_load(&model0, &model_cfg, "vmaf_v0.6.1");
mu_assert("problem during vmaf_model_load", model0);

vmaf_model_load(&model1, &model_cfg1, "vmaf_v0.6.1");
mu_assert("problem during vmaf_model_load", model0);

vmaf_model_load(&model2, &model_cfg2, "vmaf_v0.6.1");
mu_assert("problem during vmaf_model_load", model0);

err = vmaf_feature_collector_mount_model(feature_collector, model0);
mu_assert("problem during vmaf_model_mount",
feature_collector->models);

err = vmaf_feature_collector_mount_model(feature_collector, model);
err = vmaf_feature_collector_mount_model(feature_collector, model1);
mu_assert("problem during vmaf_model_mount",
feature_collector->models->next);

vmaf_model_destroy(model);
err = vmaf_feature_collector_mount_model(feature_collector, model2);
mu_assert("problem during vmaf_model_mount",
feature_collector->models->next);

VmafPredictModel *model_iter = feature_collector->models;
unsigned i = 0;

while (model_iter) {
mu_assert("model name does not match", !strcmp(model_iter->model->name,
model_names[i]));
model_iter = model_iter->next;
i++;
}

vmaf_model_destroy(model0);
vmaf_model_destroy(model1);
vmaf_model_destroy(model2);
vmaf_feature_collector_destroy(feature_collector);

return NULL;
Expand All @@ -85,20 +122,60 @@ static char* test_model_unmount()
err = vmaf_feature_collector_init(&feature_collector);
mu_assert("problem during vmaf_feature_collector_init", !err);

VmafModelConfig model_cfg = { 0 };
VmafModel *model;
vmaf_model_load(&model, &model_cfg, "vmaf_v0.6.1");
mu_assert("problem during vmaf_model_load", model);
char *model_names[] = {"vmaf_0", "vmaf_1", "vmaf_2"};

VmafModelConfig model_cfg = {
.name = model_names[0],
.flags = VMAF_MODEL_FLAGS_DEFAULT,
};

VmafModelConfig model_cfg1 = {
.name = model_names[1],
.flags = VMAF_MODEL_FLAGS_DEFAULT,
};

VmafModelConfig model_cfg2 = {
.name = model_names[2],
.flags = VMAF_MODEL_FLAGS_DEFAULT,
};

VmafModel *model0, *model1, *model2;
vmaf_model_load(&model0, &model_cfg, "vmaf_v0.6.1");
mu_assert("problem during vmaf_model_load", model0);

err = vmaf_feature_collector_mount_model(feature_collector, model);
vmaf_model_load(&model1, &model_cfg1, "vmaf_v0.6.1");
mu_assert("problem during vmaf_model_load", model0);

vmaf_model_load(&model2, &model_cfg2, "vmaf_v0.6.1");
mu_assert("problem during vmaf_model_load", model0);

err = vmaf_feature_collector_mount_model(feature_collector, model0);
mu_assert("problem during vmaf_model_mount",
feature_collector->models);

err = vmaf_feature_collector_unmount_model(feature_collector, model);
mu_assert("problem during vmaf_model_unmount",
!feature_collector->models);
err = vmaf_feature_collector_mount_model(feature_collector, model1);
mu_assert("problem during vmaf_model_mount",
feature_collector->models->next);

vmaf_model_destroy(model);
err = vmaf_feature_collector_mount_model(feature_collector, model2);
mu_assert("problem during vmaf_model_mount",
feature_collector->models->next);

err = vmaf_feature_collector_unmount_model(feature_collector, model0);
mu_assert("problem during vmaf_model_unmount", !err);

err = vmaf_feature_collector_unmount_model(feature_collector, model1);
mu_assert("problem during vmaf_model_unmount", !err);

err = vmaf_feature_collector_unmount_model(feature_collector, model2);
mu_assert("problem during vmaf_model_unmount", !err);

mu_assert("feature_collector->models should be NULL",
!feature_collector->models);

vmaf_model_destroy(model0);
vmaf_model_destroy(model1);
vmaf_model_destroy(model2);
vmaf_feature_collector_destroy(feature_collector);

return NULL;
Expand Down

0 comments on commit ad2435a

Please sign in to comment.