From 56a2e9f5e71ac2d302816296309975e50c990594 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 11 Aug 2015 17:28:29 -0700 Subject: [PATCH] WebPPictureDistortion: support ARGB format for 'pic' when computing distortion. using a *tmp_plane buffer to split a/r/g/b planes up appeared to be the easiest route, compared to copy-pasting the whole code and making it x_stride aware... Change-Id: I0898ef1df62bd3e1713b77187b31b5eeef3832fe --- examples/cwebp.c | 34 +++++---- src/enc/picture_psnr.c | 157 ++++++++++++++++++++++++----------------- src/webp/encode.h | 6 +- 3 files changed, 115 insertions(+), 82 deletions(-) diff --git a/examples/cwebp.c b/examples/cwebp.c index eb50540f..e8cbee3e 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -1110,16 +1110,19 @@ int main(int argc, const char *argv[]) { if (print_distortion >= 0) { // print distortion static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" }; float values[5]; - // Comparison is performed in YUVA colorspace. - if (original_picture.use_argb && - !WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A)) { - fprintf(stderr, "Error while converting original picture to YUVA.\n"); - goto Error; - } - if (picture.use_argb && - !WebPPictureARGBToYUVA(&picture, WEBP_YUV420A)) { - fprintf(stderr, "Error while converting compressed picture to YUVA.\n"); - goto Error; + 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, print_distortion, values)) { @@ -1127,9 +1130,14 @@ int main(int argc, const char *argv[]) { goto Error; } if (!short_output) { - fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n", - distortion_names[print_distortion], - values[0], values[1], values[2], values[3], values[4]); + fprintf(stderr, "%s: ", distortion_names[print_distortion]); + if (picture.use_argb) { + fprintf(stderr, "B:%.2f G:%.2f R:%.2f A:%.2f Total:%.2f\n", + 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 { fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]); } diff --git a/src/enc/picture_psnr.c b/src/enc/picture_psnr.c index 2254b7e5..40214efc 100644 --- a/src/enc/picture_psnr.c +++ b/src/enc/picture_psnr.c @@ -12,8 +12,10 @@ // Author: Skal (pascal.massimino@gmail.com) #include +#include #include "./vp8enci.h" +#include "../utils/utils.h" //------------------------------------------------------------------------------ // local-min distortion @@ -23,9 +25,9 @@ #define RADIUS 2 // search radius. Shouldn't be too large. -static float AccumulateLSIM(const uint8_t* src, int src_stride, - const uint8_t* ref, int ref_stride, - int w, int h) { +static void AccumulateLSIM(const uint8_t* src, int src_stride, + const uint8_t* ref, int ref_stride, + int w, int h, DistoStats* stats) { int x, y; double total_sse = 0.; for (y = 0; y < h; ++y) { @@ -38,16 +40,22 @@ static float AccumulateLSIM(const uint8_t* src, int src_stride, const double value = (double)ref[y * ref_stride + x]; int i, j; for (j = y_0; j < y_1; ++j) { - const uint8_t* s = src + j * src_stride; + const uint8_t* const s = src + j * src_stride; for (i = x_0; i < x_1; ++i) { - const double sse = (double)(s[i] - value) * (s[i] - value); + const double diff = s[i] - value; + const double sse = diff * diff; if (sse < best_sse) best_sse = sse; } } total_sse += best_sse; } } - return (float)total_sse; + stats->w = w * h; + stats->xm = 0; + stats->ym = 0; + stats->xxm = total_sse; + stats->yym = 0; + stats->xxm = 0; } #undef RADIUS @@ -64,73 +72,90 @@ static float GetPSNR(const double v) { int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref, int type, float result[5]) { DistoStats stats[5]; - int has_alpha; - int uv_w, uv_h; - - if (src == NULL || ref == NULL || - src->width != ref->width || src->height != ref->height || - src->y == NULL || ref->y == NULL || - src->u == NULL || ref->u == NULL || - src->v == NULL || ref->v == NULL || - result == NULL) { - return 0; - } - // TODO(skal): provide distortion for ARGB too. - if (src->use_argb == 1 || src->use_argb != ref->use_argb) { - 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; - } + int w, h; memset(stats, 0, sizeof(stats)); - uv_w = (src->width + 1) >> 1; - uv_h = (src->height + 1) >> 1; - if (type >= 2) { - float sse[4]; - sse[0] = AccumulateLSIM(src->y, src->y_stride, - ref->y, ref->y_stride, src->width, src->height); - sse[1] = AccumulateLSIM(src->u, src->uv_stride, - ref->u, ref->uv_stride, uv_w, uv_h); - sse[2] = AccumulateLSIM(src->v, src->uv_stride, - ref->v, ref->uv_stride, uv_w, uv_h); - sse[3] = has_alpha ? AccumulateLSIM(src->a, src->a_stride, - ref->a, ref->a_stride, - src->width, src->height) - : 0.f; - result[0] = GetPSNR(sse[0] / (src->width * src->height)); - result[1] = GetPSNR(sse[1] / (uv_w * uv_h)); - result[2] = GetPSNR(sse[2] / (uv_w * uv_h)); - result[3] = GetPSNR(sse[3] / (src->width * src->height)); - { - double total_sse = sse[0] + sse[1] + sse[2]; - int total_pixels = src->width * src->height + 2 * uv_w * uv_h; - if (has_alpha) { - total_pixels += src->width * src->height; - total_sse += sse[3]; + if (src == NULL || ref == NULL || + src->width != ref->width || src->height != ref->height || + src->use_argb != ref->use_argb || result == NULL) { + return 0; + } + w = src->width; + h = src->height; + + if (src->use_argb == 1) { + if (src->argb == NULL || ref->argb == NULL) { + return 0; + } else { + int i, j, c; + uint8_t* tmp1, *tmp2; + uint8_t* const tmp_plane = + (uint8_t*)WebPSafeMalloc(2ULL * w * h, sizeof(*tmp_plane)); + if (tmp_plane == NULL) return 0; + tmp1 = tmp_plane; + tmp2 = tmp_plane + w * h; + for (c = 0; c < 4; ++c) { + for (j = 0; j < h; ++j) { + for (i = 0; i < w; ++i) { + 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); + } + } + if (type >= 2) { + AccumulateLSIM(tmp1, w, tmp2, w, w, h, &stats[c]); + } else { + VP8SSIMAccumulatePlane(tmp1, w, tmp2, w, w, h, &stats[c]); + } } - result[4] = GetPSNR(total_sse / total_pixels); + free(tmp_plane); } } else { - int c; - VP8SSIMAccumulatePlane(src->y, src->y_stride, - ref->y, ref->y_stride, - src->width, src->height, &stats[0]); - VP8SSIMAccumulatePlane(src->u, src->uv_stride, - ref->u, ref->uv_stride, - uv_w, uv_h, &stats[1]); - VP8SSIMAccumulatePlane(src->v, src->uv_stride, - ref->v, ref->uv_stride, - uv_w, uv_h, &stats[2]); - if (has_alpha) { - VP8SSIMAccumulatePlane(src->a, src->a_stride, - ref->a, ref->a_stride, - src->width, src->height, &stats[3]); + 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; + if (type >= 2) { + AccumulateLSIM(src->y, src->y_stride, ref->y, ref->y_stride, + w, h, &stats[0]); + AccumulateLSIM(src->u, src->uv_stride, ref->u, ref->uv_stride, + uv_w, uv_h, &stats[1]); + AccumulateLSIM(src->v, src->uv_stride, ref->v, ref->uv_stride, + uv_w, uv_h, &stats[2]); + if (has_alpha) { + AccumulateLSIM(src->a, src->a_stride, ref->a, ref->a_stride, + w, h, &stats[3]); + } + } else { + VP8SSIMAccumulatePlane(src->y, src->y_stride, + ref->y, ref->y_stride, + w, h, &stats[0]); + VP8SSIMAccumulatePlane(src->u, src->uv_stride, + ref->u, ref->uv_stride, + uv_w, uv_h, &stats[1]); + VP8SSIMAccumulatePlane(src->v, src->uv_stride, + ref->v, ref->uv_stride, + uv_w, uv_h, &stats[2]); + if (has_alpha) { + VP8SSIMAccumulatePlane(src->a, src->a_stride, + ref->a, ref->a_stride, + w, h, &stats[3]); + } + } + } + // Final stat calculations. + { + int c; for (c = 0; c <= 4; ++c) { if (type == 1) { const double v = VP8SSIMGet(&stats[c]); diff --git a/src/webp/encode.h b/src/webp/encode.h index 3195e417..f61a275a 100644 --- a/src/webp/encode.h +++ b/src/webp/encode.h @@ -20,7 +20,7 @@ extern "C" { #endif -#define WEBP_ENCODER_ABI_VERSION 0x0207 // MAJOR(8b) + MINOR(8b) +#define WEBP_ENCODER_ABI_VERSION 0x0208 // MAJOR(8b) + MINOR(8b) // Note: forward declaring enumerations is not allowed in (strict) C and C++, // the types are left here for reference. @@ -379,8 +379,8 @@ WEBP_EXTERN(void) WebPPictureFree(WebPPicture* picture); // Returns false in case of memory allocation error. WEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst); -// Compute PSNR, SSIM or LSIM distortion metric between two pictures. -// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order. +// 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. // Returns false in case of error (src and ref don't have same dimension, ...) // Warning: this function is rather CPU-intensive. WEBP_EXTERN(int) WebPPictureDistortion(