introduce WebPPlaneDistortion to compute plane distortion

Make WebPPictureDistortion() only compute distortion on A/R/G/B planes, not Y/U/V(A).
(not just for SSIM, but PSNR too).

This is to avoid problems with using SSIM on U/V channels.
If Y/U/V distortion is needed, one can always use WebPPlaneDistortion() individually.

Change-Id: If8bc9c3ac12a8d2220f03224694fc389b16b7da9
This commit is contained in:
Pascal Massimino 2016-10-19 09:12:13 +02:00
parent 0104d730bf
commit 2f51b614b0
3 changed files with 92 additions and 92 deletions

View File

@ -1093,20 +1093,6 @@ int main(int argc, const char *argv[]) {
if (print_distortion >= 0) { // print distortion if (print_distortion >= 0) { // print distortion
static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" }; static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
float values[5]; float values[5];
if (picture.use_argb != original_picture.use_argb) {
// Somehow, the WebPEncode() call converted the original picture.
// We need to make both match before calling WebPPictureDistortion().
int ok = 0;
if (picture.use_argb) {
ok = WebPPictureYUVAToARGB(&original_picture);
} else {
ok = WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A);
}
if (!ok) {
fprintf(stderr, "Error while converting original picture.\n");
goto Error;
}
}
if (!WebPPictureDistortion(&picture, &original_picture, if (!WebPPictureDistortion(&picture, &original_picture,
print_distortion, values)) { print_distortion, values)) {
fprintf(stderr, "Error while computing the distortion.\n"); fprintf(stderr, "Error while computing the distortion.\n");
@ -1114,13 +1100,8 @@ int main(int argc, const char *argv[]) {
} }
if (!short_output) { if (!short_output) {
fprintf(stderr, "%s: ", distortion_names[print_distortion]); fprintf(stderr, "%s: ", distortion_names[print_distortion]);
if (picture.use_argb) { fprintf(stderr, "B:%.2f G:%.2f R:%.2f A:%.2f Total:%.2f\n",
fprintf(stderr, "B:%.2f G:%.2f R:%.2f A:%.2f Total:%.2f\n", values[0], values[1], values[2], values[3], values[4]);
values[0], values[1], values[2], values[3], values[4]);
} else {
fprintf(stderr, "Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
values[0], values[1], values[2], values[3], values[4]);
}
} else { } else {
fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]); fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]);
} }

View File

@ -124,86 +124,90 @@ static double GetLogSSIM(double v, double size) {
return (v < 1.) ? -10.0 * log10(1. - v) : kMinDistortion_dB; return (v < 1.) ? -10.0 * log10(1. - v) : kMinDistortion_dB;
} }
int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref, int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,
int type, float results[5]) { const uint8_t* ref, size_t ref_stride,
int w, h, c; int width, int height, size_t x_step,
double disto[4] = { 0. }; int type, float* distortion, float* result) {
double sizes[4] = { 0. }; uint8_t* allocated = NULL;
double total_size = 0., total_disto = 0.;
const AccumulateFunc metric = (type == 0) ? AccumulateSSE : const AccumulateFunc metric = (type == 0) ? AccumulateSSE :
(type == 1) ? AccumulateSSIM : (type == 1) ? AccumulateSSIM :
AccumulateLSIM; AccumulateLSIM;
VP8SSIMDspInit();
if (src == NULL || ref == NULL || if (src == NULL || ref == NULL ||
src->width != ref->width || src->height != ref->height || src_stride < x_step * width || ref_stride < x_step * width ||
src->use_argb != ref->use_argb || results == NULL) { result == NULL || distortion == NULL) {
return 0; return 0;
} }
w = src->width;
h = src->height;
if (src->use_argb == 1) { VP8SSIMDspInit();
if (src->argb == NULL || ref->argb == NULL) { if (x_step != 1) { // extract a packed plane if needed
return 0; int x, y;
} else { uint8_t* tmp1;
int i, j; uint8_t* tmp2;
uint8_t* tmp1, *tmp2; allocated =
uint8_t* const tmp_plane = (uint8_t*)WebPSafeMalloc(2ULL * width * height, sizeof(*allocated));
(uint8_t*)WebPSafeMalloc(2ULL * w * h, sizeof(*tmp_plane)); if (allocated == NULL) return 0;
if (tmp_plane == NULL) return 0; tmp1 = allocated;
tmp1 = tmp_plane; tmp2 = tmp1 + (size_t)width * height;
tmp2 = tmp_plane + w * h; for (y = 0; y < height; ++y) {
for (c = 0; c < 4; ++c) { for (x = 0; x < width; ++x) {
for (j = 0; j < h; ++j) { tmp1[x + y * width] = src[x * x_step + y * src_stride];
for (i = 0; i < w; ++i) { tmp2[x + y * width] = ref[x * x_step + y * ref_stride];
tmp1[j * w + i] = src->argb[i + j * src->argb_stride] >> (c * 8);
tmp2[j * w + i] = ref->argb[i + j * ref->argb_stride] >> (c * 8);
}
}
sizes[c] = w * h;
disto[c] = metric(tmp1, w, tmp2, w, w, h);
} }
WebPSafeFree(tmp_plane);
}
} else {
int has_alpha, uv_w, uv_h;
if (src->y == NULL || ref->y == NULL ||
src->u == NULL || ref->u == NULL ||
src->v == NULL || ref->v == NULL) {
return 0;
}
has_alpha = !!(src->colorspace & WEBP_CSP_ALPHA_BIT);
if (has_alpha != !!(ref->colorspace & WEBP_CSP_ALPHA_BIT) ||
(has_alpha && (src->a == NULL || ref->a == NULL))) {
return 0;
}
uv_w = (src->width + 1) >> 1;
uv_h = (src->height + 1) >> 1;
sizes[0] = w * h;
sizes[1] = sizes[2] = uv_w * uv_h;
sizes[3] = has_alpha ? w * h : 0.;
disto[0] = metric(src->y, src->y_stride, ref->y, ref->y_stride, w, h);
disto[1] = metric(src->u, src->uv_stride, ref->u, ref->uv_stride,
uv_w, uv_h);
disto[2] = metric(src->v, src->uv_stride, ref->v, ref->uv_stride,
uv_w, uv_h);
if (has_alpha) {
disto[3] = metric(src->a, src->a_stride, ref->a, ref->a_stride, w, h);
} }
src = tmp1;
ref = tmp2;
} }
*distortion = (float)metric(src, width, ref, width, width, height);
WebPSafeFree(allocated);
for (c = 0; c < 4; ++c) { *result = (type == 1) ? (float)GetLogSSIM(*distortion, (double)width * height)
total_disto += disto[c]; : (float)GetPSNR(*distortion, (double)width * height);
total_size += sizes[c];
results[c] = (type == 1) ? (float)GetLogSSIM(disto[c], sizes[c])
: (float)GetPSNR(disto[c], sizes[c]);
}
results[4] = (type == 1) ? (float)GetLogSSIM(total_disto, total_size)
: (float)GetPSNR(total_disto, total_size);
return 1; return 1;
} }
int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
int type, float results[5]) {
int w, h, c;
int ok = 0;
WebPPicture p0, p1;
double total_size = 0., total_distortion = 0.;
if (src == NULL || ref == NULL ||
src->width != ref->width || src->height != ref->height ||
results == NULL) {
return 0;
}
VP8SSIMDspInit();
if (!WebPPictureInit(&p0) || !WebPPictureInit(&p1)) return 0;
w = src->width;
h = src->height;
if (!WebPPictureView(src, 0, 0, w, h, &p0)) goto Error;
if (!WebPPictureView(ref, 0, 0, w, h, &p1)) goto Error;
// We always measure distortion in ARGB space.
if (p0.use_argb == 0 && !WebPPictureYUVAToARGB(&p0)) goto Error;
if (p1.use_argb == 0 && !WebPPictureYUVAToARGB(&p1)) goto Error;
for (c = 0; c < 4; ++c) {
float distortion;
const size_t stride0 = 4 * (size_t)p0.argb_stride;
const size_t stride1 = 4 * (size_t)p1.argb_stride;
if (!WebPPlaneDistortion((const uint8_t*)p0.argb + c, stride0,
(const uint8_t*)p1.argb + c, stride1,
w, h, 4, type, &distortion, results + c)) {
goto Error;
}
total_distortion += distortion;
total_size += w * h;
}
results[4] = (type == 1) ? (float)GetLogSSIM(total_distortion, total_size)
: (float)GetPSNR(total_distortion, total_size);
ok = 1;
Error:
WebPPictureFree(&p0);
WebPPictureFree(&p1);
return ok;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -20,7 +20,7 @@
extern "C" { extern "C" {
#endif #endif
#define WEBP_ENCODER_ABI_VERSION 0x0209 // MAJOR(8b) + MINOR(8b) #define WEBP_ENCODER_ABI_VERSION 0x020b // MAJOR(8b) + MINOR(8b)
// Note: forward declaring enumerations is not allowed in (strict) C and C++, // Note: forward declaring enumerations is not allowed in (strict) C and C++,
// the types are left here for reference. // the types are left here for reference.
@ -388,9 +388,24 @@ WEBP_EXTERN(void) WebPPictureFree(WebPPicture* picture);
// Returns false in case of memory allocation error. // Returns false in case of memory allocation error.
WEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst); WEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst);
// Compute the single distortion for packed planes of samples.
// 'src' will be compared to 'ref', and the raw distortion stored into
// '*distortion'. The refined metric (log(MSE), log(1 - ssim),...' will be
// stored in '*result'.
// 'x_step' is the horizontal stride (in bytes) between samples.
// 'src/ref_stride' is the byte distance between rows.
// Returns false in case of error (bad parameter, memory allocation error, ...).
WEBP_EXTERN(int) WebPPlaneDistortion(const uint8_t* src, size_t src_stride,
const uint8_t* ref, size_t ref_stride,
int width, int height,
size_t x_step,
int type, // 0 = PSNR, 1 = SSIM, 2 = LSIM
float* distortion, float* result);
// Compute PSNR, SSIM or LSIM distortion metric between two pictures. Results // Compute PSNR, SSIM or LSIM distortion metric between two pictures. Results
// are in dB, stored in result[] in the Y/U/V/Alpha/All or B/G/R/A/All order. // are in dB, stored in result[] in the B/G/R/A/All order. The distortion is
// Returns false in case of error (src and ref don't have same dimension, ...) // always performed using ARGB samples. Hence if the input is YUV(A), the
// picture will be internally converted to ARGB (just for the measurement).
// Warning: this function is rather CPU-intensive. // Warning: this function is rather CPU-intensive.
WEBP_EXTERN(int) WebPPictureDistortion( WEBP_EXTERN(int) WebPPictureDistortion(
const WebPPicture* src, const WebPPicture* ref, const WebPPicture* src, const WebPPicture* ref,