OLD | NEW |
1 // Copyright 2014 Google Inc. All Rights Reserved. | 1 // Copyright 2014 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
4 // that can be found in the COPYING file in the root of the source | 4 // that can be found in the COPYING file in the root of the source |
5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // WebPPicture tools for measuring distortion | 10 // WebPPicture tools for measuring distortion |
11 // | 11 // |
12 // Author: Skal (pascal.massimino@gmail.com) | 12 // Author: Skal (pascal.massimino@gmail.com) |
13 | 13 |
14 #include <math.h> | 14 #include <math.h> |
| 15 #include <stdlib.h> |
15 | 16 |
16 #include "./vp8enci.h" | 17 #include "./vp8enci.h" |
| 18 #include "../utils/utils.h" |
17 | 19 |
18 //------------------------------------------------------------------------------ | 20 //------------------------------------------------------------------------------ |
19 // local-min distortion | 21 // local-min distortion |
20 // | 22 // |
21 // For every pixel in the *reference* picture, we search for the local best | 23 // For every pixel in the *reference* picture, we search for the local best |
22 // match in the compressed image. This is not a symmetrical measure. | 24 // match in the compressed image. This is not a symmetrical measure. |
23 | 25 |
24 #define RADIUS 2 // search radius. Shouldn't be too large. | 26 #define RADIUS 2 // search radius. Shouldn't be too large. |
25 | 27 |
26 static float AccumulateLSIM(const uint8_t* src, int src_stride, | 28 static void AccumulateLSIM(const uint8_t* src, int src_stride, |
27 const uint8_t* ref, int ref_stride, | 29 const uint8_t* ref, int ref_stride, |
28 int w, int h) { | 30 int w, int h, DistoStats* stats) { |
29 int x, y; | 31 int x, y; |
30 double total_sse = 0.; | 32 double total_sse = 0.; |
31 for (y = 0; y < h; ++y) { | 33 for (y = 0; y < h; ++y) { |
32 const int y_0 = (y - RADIUS < 0) ? 0 : y - RADIUS; | 34 const int y_0 = (y - RADIUS < 0) ? 0 : y - RADIUS; |
33 const int y_1 = (y + RADIUS + 1 >= h) ? h : y + RADIUS + 1; | 35 const int y_1 = (y + RADIUS + 1 >= h) ? h : y + RADIUS + 1; |
34 for (x = 0; x < w; ++x) { | 36 for (x = 0; x < w; ++x) { |
35 const int x_0 = (x - RADIUS < 0) ? 0 : x - RADIUS; | 37 const int x_0 = (x - RADIUS < 0) ? 0 : x - RADIUS; |
36 const int x_1 = (x + RADIUS + 1 >= w) ? w : x + RADIUS + 1; | 38 const int x_1 = (x + RADIUS + 1 >= w) ? w : x + RADIUS + 1; |
37 double best_sse = 255. * 255.; | 39 double best_sse = 255. * 255.; |
38 const double value = (double)ref[y * ref_stride + x]; | 40 const double value = (double)ref[y * ref_stride + x]; |
39 int i, j; | 41 int i, j; |
40 for (j = y_0; j < y_1; ++j) { | 42 for (j = y_0; j < y_1; ++j) { |
41 const uint8_t* s = src + j * src_stride; | 43 const uint8_t* const s = src + j * src_stride; |
42 for (i = x_0; i < x_1; ++i) { | 44 for (i = x_0; i < x_1; ++i) { |
43 const double sse = (double)(s[i] - value) * (s[i] - value); | 45 const double diff = s[i] - value; |
| 46 const double sse = diff * diff; |
44 if (sse < best_sse) best_sse = sse; | 47 if (sse < best_sse) best_sse = sse; |
45 } | 48 } |
46 } | 49 } |
47 total_sse += best_sse; | 50 total_sse += best_sse; |
48 } | 51 } |
49 } | 52 } |
50 return (float)total_sse; | 53 stats->w = w * h; |
| 54 stats->xm = 0; |
| 55 stats->ym = 0; |
| 56 stats->xxm = total_sse; |
| 57 stats->yym = 0; |
| 58 stats->xxm = 0; |
51 } | 59 } |
52 #undef RADIUS | 60 #undef RADIUS |
53 | 61 |
54 //------------------------------------------------------------------------------ | 62 //------------------------------------------------------------------------------ |
55 // Distortion | 63 // Distortion |
56 | 64 |
57 // Max value returned in case of exact similarity. | 65 // Max value returned in case of exact similarity. |
58 static const double kMinDistortion_dB = 99.; | 66 static const double kMinDistortion_dB = 99.; |
59 static float GetPSNR(const double v) { | 67 static float GetPSNR(const double v) { |
60 return (float)((v > 0.) ? -4.3429448 * log(v / (255 * 255.)) | 68 return (float)((v > 0.) ? -4.3429448 * log(v / (255 * 255.)) |
61 : kMinDistortion_dB); | 69 : kMinDistortion_dB); |
62 } | 70 } |
63 | 71 |
64 int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref, | 72 int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref, |
65 int type, float result[5]) { | 73 int type, float result[5]) { |
66 DistoStats stats[5]; | 74 DistoStats stats[5]; |
67 int has_alpha; | 75 int w, h; |
68 int uv_w, uv_h; | 76 |
| 77 memset(stats, 0, sizeof(stats)); |
69 | 78 |
70 if (src == NULL || ref == NULL || | 79 if (src == NULL || ref == NULL || |
71 src->width != ref->width || src->height != ref->height || | 80 src->width != ref->width || src->height != ref->height || |
72 src->y == NULL || ref->y == NULL || | 81 src->use_argb != ref->use_argb || result == NULL) { |
73 src->u == NULL || ref->u == NULL || | |
74 src->v == NULL || ref->v == NULL || | |
75 result == NULL) { | |
76 return 0; | 82 return 0; |
77 } | 83 } |
78 // TODO(skal): provide distortion for ARGB too. | 84 w = src->width; |
79 if (src->use_argb == 1 || src->use_argb != ref->use_argb) { | 85 h = src->height; |
80 return 0; | |
81 } | |
82 | 86 |
83 has_alpha = !!(src->colorspace & WEBP_CSP_ALPHA_BIT); | 87 if (src->use_argb == 1) { |
84 if (has_alpha != !!(ref->colorspace & WEBP_CSP_ALPHA_BIT) || | 88 if (src->argb == NULL || ref->argb == NULL) { |
85 (has_alpha && (src->a == NULL || ref->a == NULL))) { | 89 return 0; |
86 return 0; | 90 } else { |
87 } | 91 int i, j, c; |
88 | 92 uint8_t* tmp1, *tmp2; |
89 memset(stats, 0, sizeof(stats)); | 93 uint8_t* const tmp_plane = |
90 | 94 (uint8_t*)WebPSafeMalloc(2ULL * w * h, sizeof(*tmp_plane)); |
91 uv_w = (src->width + 1) >> 1; | 95 if (tmp_plane == NULL) return 0; |
92 uv_h = (src->height + 1) >> 1; | 96 tmp1 = tmp_plane; |
93 if (type >= 2) { | 97 tmp2 = tmp_plane + w * h; |
94 float sse[4]; | 98 for (c = 0; c < 4; ++c) { |
95 sse[0] = AccumulateLSIM(src->y, src->y_stride, | 99 for (j = 0; j < h; ++j) { |
96 ref->y, ref->y_stride, src->width, src->height); | 100 for (i = 0; i < w; ++i) { |
97 sse[1] = AccumulateLSIM(src->u, src->uv_stride, | 101 tmp1[j * w + i] = src->argb[i + j * src->argb_stride] >> (c * 8); |
98 ref->u, ref->uv_stride, uv_w, uv_h); | 102 tmp2[j * w + i] = ref->argb[i + j * ref->argb_stride] >> (c * 8); |
99 sse[2] = AccumulateLSIM(src->v, src->uv_stride, | 103 } |
100 ref->v, ref->uv_stride, uv_w, uv_h); | 104 } |
101 sse[3] = has_alpha ? AccumulateLSIM(src->a, src->a_stride, | 105 if (type >= 2) { |
102 ref->a, ref->a_stride, | 106 AccumulateLSIM(tmp1, w, tmp2, w, w, h, &stats[c]); |
103 src->width, src->height) | 107 } else { |
104 : 0.f; | 108 VP8SSIMAccumulatePlane(tmp1, w, tmp2, w, w, h, &stats[c]); |
105 result[0] = GetPSNR(sse[0] / (src->width * src->height)); | 109 } |
106 result[1] = GetPSNR(sse[1] / (uv_w * uv_h)); | |
107 result[2] = GetPSNR(sse[2] / (uv_w * uv_h)); | |
108 result[3] = GetPSNR(sse[3] / (src->width * src->height)); | |
109 { | |
110 double total_sse = sse[0] + sse[1] + sse[2]; | |
111 int total_pixels = src->width * src->height + 2 * uv_w * uv_h; | |
112 if (has_alpha) { | |
113 total_pixels += src->width * src->height; | |
114 total_sse += sse[3]; | |
115 } | 110 } |
116 result[4] = GetPSNR(total_sse / total_pixels); | 111 free(tmp_plane); |
117 } | 112 } |
118 } else { | 113 } else { |
| 114 int has_alpha, uv_w, uv_h; |
| 115 if (src->y == NULL || ref->y == NULL || |
| 116 src->u == NULL || ref->u == NULL || |
| 117 src->v == NULL || ref->v == NULL) { |
| 118 return 0; |
| 119 } |
| 120 has_alpha = !!(src->colorspace & WEBP_CSP_ALPHA_BIT); |
| 121 if (has_alpha != !!(ref->colorspace & WEBP_CSP_ALPHA_BIT) || |
| 122 (has_alpha && (src->a == NULL || ref->a == NULL))) { |
| 123 return 0; |
| 124 } |
| 125 |
| 126 uv_w = (src->width + 1) >> 1; |
| 127 uv_h = (src->height + 1) >> 1; |
| 128 if (type >= 2) { |
| 129 AccumulateLSIM(src->y, src->y_stride, ref->y, ref->y_stride, |
| 130 w, h, &stats[0]); |
| 131 AccumulateLSIM(src->u, src->uv_stride, ref->u, ref->uv_stride, |
| 132 uv_w, uv_h, &stats[1]); |
| 133 AccumulateLSIM(src->v, src->uv_stride, ref->v, ref->uv_stride, |
| 134 uv_w, uv_h, &stats[2]); |
| 135 if (has_alpha) { |
| 136 AccumulateLSIM(src->a, src->a_stride, ref->a, ref->a_stride, |
| 137 w, h, &stats[3]); |
| 138 } |
| 139 } else { |
| 140 VP8SSIMAccumulatePlane(src->y, src->y_stride, |
| 141 ref->y, ref->y_stride, |
| 142 w, h, &stats[0]); |
| 143 VP8SSIMAccumulatePlane(src->u, src->uv_stride, |
| 144 ref->u, ref->uv_stride, |
| 145 uv_w, uv_h, &stats[1]); |
| 146 VP8SSIMAccumulatePlane(src->v, src->uv_stride, |
| 147 ref->v, ref->uv_stride, |
| 148 uv_w, uv_h, &stats[2]); |
| 149 if (has_alpha) { |
| 150 VP8SSIMAccumulatePlane(src->a, src->a_stride, |
| 151 ref->a, ref->a_stride, |
| 152 w, h, &stats[3]); |
| 153 } |
| 154 } |
| 155 } |
| 156 // Final stat calculations. |
| 157 { |
119 int c; | 158 int c; |
120 VP8SSIMAccumulatePlane(src->y, src->y_stride, | |
121 ref->y, ref->y_stride, | |
122 src->width, src->height, &stats[0]); | |
123 VP8SSIMAccumulatePlane(src->u, src->uv_stride, | |
124 ref->u, ref->uv_stride, | |
125 uv_w, uv_h, &stats[1]); | |
126 VP8SSIMAccumulatePlane(src->v, src->uv_stride, | |
127 ref->v, ref->uv_stride, | |
128 uv_w, uv_h, &stats[2]); | |
129 if (has_alpha) { | |
130 VP8SSIMAccumulatePlane(src->a, src->a_stride, | |
131 ref->a, ref->a_stride, | |
132 src->width, src->height, &stats[3]); | |
133 } | |
134 for (c = 0; c <= 4; ++c) { | 159 for (c = 0; c <= 4; ++c) { |
135 if (type == 1) { | 160 if (type == 1) { |
136 const double v = VP8SSIMGet(&stats[c]); | 161 const double v = VP8SSIMGet(&stats[c]); |
137 result[c] = (float)((v < 1.) ? -10.0 * log10(1. - v) | 162 result[c] = (float)((v < 1.) ? -10.0 * log10(1. - v) |
138 : kMinDistortion_dB); | 163 : kMinDistortion_dB); |
139 } else { | 164 } else { |
140 const double v = VP8SSIMGetSquaredError(&stats[c]); | 165 const double v = VP8SSIMGetSquaredError(&stats[c]); |
141 result[c] = GetPSNR(v); | 166 result[c] = GetPSNR(v); |
142 } | 167 } |
143 // Accumulate forward | 168 // Accumulate forward |
144 if (c < 4) VP8SSIMAddStats(&stats[c], &stats[4]); | 169 if (c < 4) VP8SSIMAddStats(&stats[c], &stats[4]); |
145 } | 170 } |
146 } | 171 } |
147 return 1; | 172 return 1; |
148 } | 173 } |
149 | 174 |
150 //------------------------------------------------------------------------------ | 175 //------------------------------------------------------------------------------ |
OLD | NEW |