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

Modify metadata_register API for support individual metric propagation. #1387

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
41 changes: 40 additions & 1 deletion libvmaf/include/libvmaf/libvmaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ int vmaf_feature_score_at_index(VmafContext *vmaf, const char *feature_name,
* Metadata structure.
*
* @param feature_name Name of the feature to fetch.
* vmaf going to take ownership of this string.
*
* @param picture_index Picture index.
*
Expand Down Expand Up @@ -285,6 +286,31 @@ typedef struct VmafMetadataConfiguration {
void *data;
} VmafMetadataConfiguration;


/**
* Metadata flags.
*
* Features can provide additional metrics about the score. To propagate those,
* the `VmafMetadataFlags` enum should be used for specifying which information should be
* propagated to the metadata.
*
* @param VMAF_METADATA_FLAG_NONE No flags.
*
* @param VMAF_METADATA_FLAG_FEATURE Include all indivual metrics.
*
* @param VMAF_METADATA_FLAG_MODEL Propagate only model score.
*
* @param VMAF_METADATA_FLAG_FEATURE_NAME Propagate only given feature name..
*
*/

enum VmafMetadataFlags {
VMAF_METADATA_FLAG_NONE = 0,
VMAF_METADATA_FLAG_FEATURE = 1 << 0,
VMAF_METADATA_FLAG_MODEL = 1 << 1,
VMAF_METADATA_FLAG_FEATURE_NAME = 1 << 2,
};

/**
* Register a callback to receive VMAF metadata.
*
Expand All @@ -296,7 +322,20 @@ typedef struct VmafMetadataConfiguration {
* @return 0 on success, or < 0 (a negative errno code) on error.
*/

int vmaf_register_metadata_handler(VmafContext *vmaf, VmafMetadataConfiguration cfg);
int vmaf_register_metadata_handler(VmafContext *vmaf, VmafMetadataConfiguration cfg, uint64_t flags);

/**
* Get the number of registered metadata handlers.
*
* @param vmaf The VMAF context allocated with `vmaf_init()`.
*
* @param count Number of registered metadata handlers.
*
*
* @return 0 on success, or < 0 (a negative errno code) on error.
*/

int vmaf_get_metadata_handler_count(VmafContext *vmaf, unsigned *count);

/**
* Pooled VMAF score for a specific interval.
Expand Down
20 changes: 20 additions & 0 deletions libvmaf/src/feature/feature_collector.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "metadata_handler.h"
#include "feature_collector.h"
#include "feature_name.h"
#include "feature_extractor.h"
#include "libvmaf/libvmaf.h"
#include "log.h"
#include "predict.h"
Expand Down Expand Up @@ -290,6 +291,25 @@ int vmaf_feature_collector_register_metadata(VmafFeatureCollector *feature_colle
return 0;
}

int vmaf_feature_collector_get_metadata_count(VmafFeatureCollector *feature_collector,
unsigned *count)
{
if (!feature_collector) return -EINVAL;
if (!count) return -EINVAL;

VmafCallbackList *metadata = feature_collector->metadata;
unsigned cnt = 0;
VmafCallbackItem *iter = metadata ? metadata->head : NULL;
while (iter) {
cnt++;
iter = iter->next;
}

*count = cnt;

return 0;
}

static FeatureVector *find_feature_vector(VmafFeatureCollector *fc,
const char *feature_name)
{
Expand Down
3 changes: 3 additions & 0 deletions libvmaf/src/feature/feature_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ int vmaf_feature_collector_append(VmafFeatureCollector *feature_collector,
int vmaf_feature_collector_register_metadata(VmafFeatureCollector *feature_collector,
VmafMetadataConfiguration metadata_cfg);

int vmaf_feature_collector_get_metadata_count(VmafFeatureCollector *feature_collector,
unsigned *count);

int vmaf_feature_collector_append_with_dict(VmafFeatureCollector *fc,
VmafDictionary *dict, const char *feature_name, double score,
unsigned index);
Expand Down
25 changes: 24 additions & 1 deletion libvmaf/src/libvmaf.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,13 +744,36 @@ int vmaf_read_pictures(VmafContext *vmaf, VmafPicture *ref, VmafPicture *dist,
return err;
}

int vmaf_register_metadata_handler(VmafContext *vmaf, VmafMetadataConfiguration cfg)
int vmaf_register_metadata_handler(VmafContext *vmaf, VmafMetadataConfiguration cfg, uint64_t flags)
{
if (!vmaf) return -EINVAL;

if (flags & VMAF_METADATA_FLAG_FEATURE) {
VmafFeatureExtractor *fex = vmaf_get_feature_extractor_by_name(cfg.feature_name);
if (!fex) return -EINVAL;
int err = 0;
for (unsigned i = 0; fex->provided_features[i] != NULL; i++) {
VmafMetadataConfiguration new_cfg = { 0 };
new_cfg.data = cfg.data;
new_cfg.callback = cfg.callback;
new_cfg.feature_name = strdup(fex->provided_features[i]);
err = vmaf_feature_collector_register_metadata(vmaf->feature_collector, new_cfg);
if (err) return err;
}
return 0;
}

return vmaf_feature_collector_register_metadata(vmaf->feature_collector, cfg);
}

int vmaf_get_metadata_handler_count(VmafContext *vmaf, unsigned *count)
{
if (!vmaf) return -EINVAL;
if (!count) return -EINVAL;

return vmaf_feature_collector_get_metadata_count(vmaf->feature_collector, count);
}

int vmaf_feature_score_at_index(VmafContext *vmaf, const char *feature_name,
double *score, unsigned index)
{
Expand Down
2 changes: 2 additions & 0 deletions libvmaf/src/metadata_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ int vmaf_metadata_destroy(VmafCallbackList *metadata)
VmafCallbackItem *iter = metadata->head;
while (iter) {
VmafCallbackItem *next = iter->next;
if (iter->metadata_cfg.feature_name)
free(iter->metadata_cfg.feature_name);
free(iter);
iter = next;
}
Expand Down
4 changes: 3 additions & 1 deletion libvmaf/test/test_predict.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static char* test_propagate_metadata()
};

VmafMetadataConfiguration m = {
.feature_name = "vmaf",
.feature_name = strdup("vmaf"),
.callback = set_meta,
.data = &meta_data,
};
Expand Down Expand Up @@ -127,6 +127,7 @@ static char* test_propagate_metadata()
vmaf_feature_collector_destroy(feature_collector);

m.data = NULL;
m.feature_name = strdup("vmaf");
err = vmaf_feature_collector_init(&feature_collector);
mu_assert("problem during vmaf_feature_collector_init", !err);

Expand All @@ -142,6 +143,7 @@ static char* test_propagate_metadata()
vmaf_feature_collector_destroy(feature_collector);

m.callback = NULL;
m.feature_name = strdup("vmaf");
err = vmaf_feature_collector_init(&feature_collector);
mu_assert("problem during vmaf_feature_collector_init", !err);

Expand Down
2 changes: 1 addition & 1 deletion libvmaf/test/test_propagate_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static char *test_propagate_metadata_append()
int err = vmaf_metadata_init(&propagate_metadata);
mu_assert("problem during vmaf_propagate_metadata_init", !err);

VmafMetadataConfiguration metadata_config;
VmafMetadataConfiguration metadata_config = {0};
metadata_config.callback = set_meta;
metadata_config.data = NULL;

Expand Down
Loading