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

Selectively allow more leading zeros when outputting feature values #1359

Merged
Merged
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
105 changes: 88 additions & 17 deletions libvmaf/src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ static const char *pool_method_name[] = {
[VMAF_POOL_METHOD_HARMONIC_MEAN] = "harmonic_mean",
};

static int count_leading_zeros_d(double x)
{
if(x < 0)
x = fabs(x);

int int_part = (int)x;
double fractional_part = x - int_part;

// Count leading zeroes in the fractional part
int leading_zeros_count = 0;

while (fractional_part < 1.0 && fractional_part != 0)
{
fractional_part *= 10; // Shift decimal point to the right
leading_zeros_count++;
}

return leading_zeros_count;
}

int vmaf_write_output_xml(VmafContext *vmaf, VmafFeatureCollector *fc,
FILE *outfile, unsigned subsample, unsigned width,
unsigned height, double fps, unsigned pic_cnt)
Expand All @@ -58,6 +78,7 @@ int vmaf_write_output_xml(VmafContext *vmaf, VmafFeatureCollector *fc,
fprintf(outfile, " <fyi fps=\"%.2f\" />\n", fps);

unsigned n_frames = 0;
int leading_zeros_count;
fprintf(outfile, " <frames>\n");
for (unsigned i = 0 ; i < max_capacity(fc); i++) {
if ((subsample > 1) && (i % subsample))
Expand All @@ -78,10 +99,15 @@ int vmaf_write_output_xml(VmafContext *vmaf, VmafFeatureCollector *fc,
continue;
if (!fc->feature_vector[j]->score[i].written)
continue;
fprintf(outfile, "%s=\"%.6f\" ",
vmaf_feature_name_alias(fc->feature_vector[j]->name),
fc->feature_vector[j]->score[i].value
);
leading_zeros_count = count_leading_zeros_d(fc->feature_vector[j]->score[i].value);
if (leading_zeros_count <= 6)
fprintf(outfile, "%s=\"%.6f\" ",
vmaf_feature_name_alias(fc->feature_vector[j]->name),
fc->feature_vector[j]->score[i].value);
else
fprintf(outfile, "%s=\"%.16f\" ",
vmaf_feature_name_alias(fc->feature_vector[j]->name),
fc->feature_vector[j]->score[i].value);
}
n_frames++;
fprintf(outfile, "/>\n");
Expand All @@ -99,7 +125,13 @@ int vmaf_write_output_xml(VmafContext *vmaf, VmafFeatureCollector *fc,
int err = vmaf_feature_score_pooled(vmaf, feature_name, j, &score,
0, pic_cnt - 1);
if (!err)
fprintf(outfile, "%s=\"%.6f\" ", pool_method_name[j], score);
{
leading_zeros_count = count_leading_zeros_d(score);
if (leading_zeros_count <= 6)
fprintf(outfile, "%s=\"%.6f\" ", pool_method_name[j], score);
else
fprintf(outfile, "%s=\"%.16f\" ", pool_method_name[j], score);
}
}
fprintf(outfile, "/>\n");
}
Expand All @@ -108,7 +140,13 @@ int vmaf_write_output_xml(VmafContext *vmaf, VmafFeatureCollector *fc,

fprintf(outfile, " <aggregate_metrics ");
for (unsigned i = 0; i < fc->aggregate_vector.cnt; i++) {
fprintf(outfile, "%s=\"%.6f\" ",
leading_zeros_count = count_leading_zeros_d(fc->aggregate_vector.metric[i].value);
if (leading_zeros_count <= 6)
fprintf(outfile, "%s=\"%.6f\" ",
fc->aggregate_vector.metric[i].name,
fc->aggregate_vector.metric[i].value);
else
fprintf(outfile, "%s=\"%.16f\" ",
fc->aggregate_vector.metric[i].name,
fc->aggregate_vector.metric[i].value);
}
Expand All @@ -123,6 +161,7 @@ int vmaf_write_output_json(VmafContext *vmaf, VmafFeatureCollector *fc,
FILE *outfile, unsigned subsample, double fps,
unsigned pic_cnt)
{
int leading_zeros_count;
fprintf(outfile, "{\n");
fprintf(outfile, " \"version\": \"%s\",\n", vmaf_version());
switch(fpclassify(fps)) {
Expand Down Expand Up @@ -167,11 +206,18 @@ int vmaf_write_output_json(VmafContext *vmaf, VmafFeatureCollector *fc,
case FP_NORMAL:
case FP_ZERO:
case FP_SUBNORMAL:
fprintf(outfile, " \"%s\": %.6f%s\n",
vmaf_feature_name_alias(fc->feature_vector[j]->name),
fc->feature_vector[j]->score[i].value,
cnt2 < cnt ? "," : ""
);
leading_zeros_count = count_leading_zeros_d(fc->feature_vector[j]->score[i].value);
if (leading_zeros_count <= 6)
fprintf(outfile, " \"%s\": %.6f%s\n",
vmaf_feature_name_alias(fc->feature_vector[j]->name),
fc->feature_vector[j]->score[i].value,
cnt2 < cnt ? "," : "");
else
fprintf(outfile, " \"%s\": %.16f%s\n",
vmaf_feature_name_alias(fc->feature_vector[j]->name),
fc->feature_vector[j]->score[i].value,
cnt2 < cnt ? "," : "");

break;
case FP_INFINITE:
case FP_NAN:
Expand Down Expand Up @@ -203,7 +249,12 @@ int vmaf_write_output_json(VmafContext *vmaf, VmafFeatureCollector *fc,
case FP_NORMAL:
case FP_ZERO:
case FP_SUBNORMAL:
fprintf(outfile, " \"%s\": %.6f",
leading_zeros_count = count_leading_zeros_d((double)score);
if (leading_zeros_count <= 6)
fprintf(outfile, " \"%s\": %.6f",
pool_method_name[j], score);
else
fprintf(outfile, " \"%s\": %.16f",
pool_method_name[j], score);
break;
case FP_INFINITE:
Expand All @@ -225,9 +276,17 @@ int vmaf_write_output_json(VmafContext *vmaf, VmafFeatureCollector *fc,
case FP_NORMAL:
case FP_ZERO:
case FP_SUBNORMAL:
fprintf(outfile, "\n \"%s\": %.6f",
leading_zeros_count = count_leading_zeros_d(fc->aggregate_vector.metric[i].value);
if (leading_zeros_count <= 6)
fprintf(outfile, "\n \"%s\": %.6f",
fc->aggregate_vector.metric[i].name,
fc->aggregate_vector.metric[i].value);
else
fprintf(outfile, "\n \"%s\": %.16f",
fc->aggregate_vector.metric[i].name,
fc->aggregate_vector.metric[i].value);


break;
case FP_INFINITE:
case FP_NAN:
Expand All @@ -239,14 +298,14 @@ int vmaf_write_output_json(VmafContext *vmaf, VmafFeatureCollector *fc,
}
fprintf(outfile, "\n }\n");
fprintf(outfile, "}\n");

return 0;
}

int vmaf_write_output_csv(VmafFeatureCollector *fc, FILE *outfile,
unsigned subsample)
{

int leading_zeros_count;
fprintf(outfile, "Frame,");
for (unsigned i = 0; i < fc->cnt; i++) {
fprintf(outfile, "%s,",
Expand All @@ -273,7 +332,12 @@ int vmaf_write_output_csv(VmafFeatureCollector *fc, FILE *outfile,
continue;
if (!fc->feature_vector[j]->score[i].written)
continue;
fprintf(outfile, "%.6f,", fc->feature_vector[j]->score[i].value);

leading_zeros_count = count_leading_zeros_d(fc->feature_vector[j]->score[i].value);
if (leading_zeros_count <= 6)
fprintf(outfile, "%.6f,", fc->feature_vector[j]->score[i].value);
else
fprintf(outfile, "%.16f,", fc->feature_vector[j]->score[i].value);
}
fprintf(outfile, "\n");
}
Expand All @@ -284,6 +348,7 @@ int vmaf_write_output_csv(VmafFeatureCollector *fc, FILE *outfile,
int vmaf_write_output_sub(VmafFeatureCollector *fc, FILE *outfile,
unsigned subsample)
{
int leading_zeros_count;
for (unsigned i = 0 ; i < max_capacity(fc); i++) {
if ((subsample > 1) && (i % subsample))
continue;
Expand All @@ -303,7 +368,13 @@ int vmaf_write_output_sub(VmafFeatureCollector *fc, FILE *outfile,
continue;
if (!fc->feature_vector[j]->score[i].written)
continue;
fprintf(outfile, "%s: %.6f|",
leading_zeros_count = count_leading_zeros_d(fc->feature_vector[j]->score[i].value);
if (leading_zeros_count <= 6)
fprintf(outfile, "%s: %.6f|",
vmaf_feature_name_alias(fc->feature_vector[j]->name),
fc->feature_vector[j]->score[i].value);
else
fprintf(outfile, "%s: %.16f|",
vmaf_feature_name_alias(fc->feature_vector[j]->name),
fc->feature_vector[j]->score[i].value);
}
Expand Down
Loading