Index: third_party/libwebp/enc/picture.c |
diff --git a/third_party/libwebp/enc/picture.c b/third_party/libwebp/enc/picture.c |
index 44eed060836f762ffe8f4f06beeac8fac4d30cfd..739a7aa26aea8d0cb3f1f142b5661c0e3334f9d2 100644 |
--- a/third_party/libwebp/enc/picture.c |
+++ b/third_party/libwebp/enc/picture.c |
@@ -290,8 +290,11 @@ int WebPPictureView(const WebPPicture* src, |
dst->y = src->y + top * src->y_stride + left; |
dst->u = src->u + (top >> 1) * src->uv_stride + (left >> 1); |
dst->v = src->v + (top >> 1) * src->uv_stride + (left >> 1); |
+ dst->y_stride = src->y_stride; |
+ dst->uv_stride = src->uv_stride; |
if (src->a != NULL) { |
dst->a = src->a + top * src->a_stride + left; |
+ dst->a_stride = src->a_stride; |
} |
#ifdef WEBP_EXPERIMENTAL_FEATURES |
if (src->u0 != NULL) { |
@@ -299,10 +302,12 @@ int WebPPictureView(const WebPPicture* src, |
IS_YUV_CSP(dst->colorspace, WEBP_YUV422) ? (left >> 1) : left; |
dst->u0 = src->u0 + top * src->uv0_stride + left_pos; |
dst->v0 = src->v0 + top * src->uv0_stride + left_pos; |
+ dst->uv0_stride = src->uv0_stride; |
} |
#endif |
} else { |
dst->argb = src->argb + top * src->argb_stride + left; |
+ dst->argb_stride = src->argb_stride; |
} |
return 1; |
} |
@@ -801,11 +806,11 @@ int WebPPictureYUVAToARGB(WebPPicture* picture) { |
// Insert alpha values if needed, in replacement for the default 0xff ones. |
if (picture->colorspace & WEBP_CSP_ALPHA_BIT) { |
for (y = 0; y < height; ++y) { |
- uint32_t* const dst = picture->argb + y * picture->argb_stride; |
+ uint32_t* const argb_dst = picture->argb + y * picture->argb_stride; |
const uint8_t* const src = picture->a + y * picture->a_stride; |
int x; |
for (x = 0; x < width; ++x) { |
- dst[x] = (dst[x] & 0x00ffffffu) | (src[x] << 24); |
+ argb_dst[x] = (argb_dst[x] & 0x00ffffffu) | (src[x] << 24); |
} |
} |
} |
@@ -906,67 +911,135 @@ void WebPCleanupTransparentArea(WebPPicture* pic) { |
#undef SIZE |
#undef SIZE2 |
+//------------------------------------------------------------------------------ |
+// local-min distortion |
+// |
+// For every pixel in the *reference* picture, we search for the local best |
+// match in the compressed image. This is not a symmetrical measure. |
+ |
+// search radius. Shouldn't be too large. |
+#define RADIUS 2 |
+ |
+static float AccumulateLSIM(const uint8_t* src, int src_stride, |
+ const uint8_t* ref, int ref_stride, |
+ int w, int h) { |
+ int x, y; |
+ double total_sse = 0.; |
+ for (y = 0; y < h; ++y) { |
+ const int y_0 = (y - RADIUS < 0) ? 0 : y - RADIUS; |
+ const int y_1 = (y + RADIUS + 1 >= h) ? h : y + RADIUS + 1; |
+ for (x = 0; x < w; ++x) { |
+ const int x_0 = (x - RADIUS < 0) ? 0 : x - RADIUS; |
+ const int x_1 = (x + RADIUS + 1 >= w) ? w : x + RADIUS + 1; |
+ double best_sse = 255. * 255.; |
+ 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; |
+ for (i = x_0; i < x_1; ++i) { |
+ const double sse = (double)(s[i] - value) * (s[i] - value); |
+ if (sse < best_sse) best_sse = sse; |
+ } |
+ } |
+ total_sse += best_sse; |
+ } |
+ } |
+ return (float)total_sse; |
+} |
+#undef RADIUS |
//------------------------------------------------------------------------------ |
// Distortion |
// Max value returned in case of exact similarity. |
static const double kMinDistortion_dB = 99.; |
+static float GetPSNR(const double v) { |
+ return (float)((v > 0.) ? -4.3429448 * log(v / (255 * 255.)) |
+ : kMinDistortion_dB); |
+} |
-int WebPPictureDistortion(const WebPPicture* pic1, const WebPPicture* pic2, |
+int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref, |
int type, float result[5]) { |
- int c; |
DistoStats stats[5]; |
int has_alpha; |
+ int uv_w, uv_h; |
- if (pic1 == NULL || pic2 == NULL || |
- pic1->width != pic2->width || pic1->height != pic2->height || |
- pic1->y == NULL || pic2->y == NULL || |
- pic1->u == NULL || pic2->u == NULL || |
- pic1->v == NULL || pic2->v == NULL || |
+ 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 (pic1->use_argb == 1 || pic1->use_argb != pic2->use_argb) { |
+ if (src->use_argb == 1 || src->use_argb != ref->use_argb) { |
return 0; |
} |
- has_alpha = !!(pic1->colorspace & WEBP_CSP_ALPHA_BIT); |
- if (has_alpha != !!(pic2->colorspace & WEBP_CSP_ALPHA_BIT) || |
- (has_alpha && (pic1->a == NULL || pic2->a == NULL))) { |
+ 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; |
} |
memset(stats, 0, sizeof(stats)); |
- VP8SSIMAccumulatePlane(pic1->y, pic1->y_stride, |
- pic2->y, pic2->y_stride, |
- pic1->width, pic1->height, &stats[0]); |
- VP8SSIMAccumulatePlane(pic1->u, pic1->uv_stride, |
- pic2->u, pic2->uv_stride, |
- (pic1->width + 1) >> 1, (pic1->height + 1) >> 1, |
- &stats[1]); |
- VP8SSIMAccumulatePlane(pic1->v, pic1->uv_stride, |
- pic2->v, pic2->uv_stride, |
- (pic1->width + 1) >> 1, (pic1->height + 1) >> 1, |
- &stats[2]); |
- if (has_alpha) { |
- VP8SSIMAccumulatePlane(pic1->a, pic1->a_stride, |
- pic2->a, pic2->a_stride, |
- pic1->width, pic1->height, &stats[3]); |
- } |
- for (c = 0; c <= 4; ++c) { |
- if (type == 1) { |
- const double v = VP8SSIMGet(&stats[c]); |
- result[c] = (float)((v < 1.) ? -10.0 * log10(1. - v) |
- : kMinDistortion_dB); |
- } else { |
- const double v = VP8SSIMGetSquaredError(&stats[c]); |
- result[c] = (float)((v > 0.) ? -4.3429448 * log(v / (255 * 255.)) |
- : kMinDistortion_dB); |
+ |
+ uv_w = HALVE(src->width); |
+ uv_h = HALVE(src->height); |
+ 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]; |
+ } |
+ result[4] = GetPSNR(total_sse / total_pixels); |
+ } |
+ } 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]); |
+ } |
+ for (c = 0; c <= 4; ++c) { |
+ if (type == 1) { |
+ const double v = VP8SSIMGet(&stats[c]); |
+ result[c] = (float)((v < 1.) ? -10.0 * log10(1. - v) |
+ : kMinDistortion_dB); |
+ } else { |
+ const double v = VP8SSIMGetSquaredError(&stats[c]); |
+ result[c] = GetPSNR(v); |
+ } |
+ // Accumulate forward |
+ if (c < 4) VP8SSIMAddStats(&stats[c], &stats[4]); |
} |
- // Accumulate forward |
- if (c < 4) VP8SSIMAddStats(&stats[c], &stats[4]); |
} |
return 1; |
} |