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

libvmaf: add validation for pixel values above expected max value #1352

Closed
wants to merge 1 commit into from
Closed
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
61 changes: 61 additions & 0 deletions libvmaf/src/libvmaf.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,60 @@ static int threaded_read_pictures(VmafContext *vmaf, VmafPicture *ref,
return vmaf_picture_unref(ref) | vmaf_picture_unref(dist);
}

static int validate_pixel_values_lbd(VmafPicture *pic) {
int bpc = pic->bpc;
uint8_t max_val = (1 << bpc) - 1;
int channel = 0;
uint8_t *data = (uint8_t *)pic->data[channel];
size_t stride = pic->stride[channel];
int middle_row = pic->h[channel] / 2;
for (unsigned j = 0; j < pic->w[channel]; j++) {
if (data[middle_row * stride + j] > max_val) {
vmaf_log(
VMAF_LOG_LEVEL_ERROR,
"Invalid input. The input contains values greater than %d, which exceeds the maximum value for a %d-bit depth format.",
max_val, bpc
);
return -EINVAL;
}
}
return 0;
}

static int validate_pixel_values_hbd(VmafPicture *pic) {
int bpc = pic->bpc;
uint16_t max_val = (1 << bpc) - 1;
int channel = 0;
uint16_t *data = (uint16_t *)pic->data[channel];
size_t stride = pic->stride[channel];
int middle_row = pic->h[channel] / 2;
for (unsigned j = 0; j < pic->w[channel]; j++) {
if (data[middle_row * stride + j] > max_val) {
vmaf_log(
VMAF_LOG_LEVEL_ERROR,
"Invalid input. The input contains values greater than %d, which exceeds the maximum value for a %d-bit depth format.",
max_val, bpc
);
return -EINVAL;
}
}
return 0;
}

/* This function validates that there are no pixel values above the maximum possible value for the given bitdepth.
* For example, if the given bitdepth is 10, no values should be above 1023. However, checking every pixel incurs
* significant overhead, so we only check the middle row as a heuristic.
*/
static int validate_pixel_values(VmafPicture *pic) {
int bpc = pic->bpc;
if (bpc <= 8) {
return validate_pixel_values_lbd(pic);
}
else {
return validate_pixel_values_hbd(pic);
}
}

static int validate_pic_params(VmafContext *vmaf, VmafPicture *ref,
VmafPicture *dist)
{
Expand Down Expand Up @@ -481,6 +535,13 @@ static int validate_pic_params(VmafContext *vmaf, VmafPicture *ref,
if (ref_priv->buf_type != dist_priv->buf_type)
return -EINVAL;

if (validate_pixel_values(ref)) {
return -EINVAL;
}
if (validate_pixel_values(dist)) {
return -EINVAL;
}

return 0;
}

Expand Down