1*b2055c35SXin Li // Copyright 2014 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // WebPPicture tools for measuring distortion
11*b2055c35SXin Li //
12*b2055c35SXin Li // Author: Skal ([email protected])
13*b2055c35SXin Li
14*b2055c35SXin Li #include "src/webp/encode.h"
15*b2055c35SXin Li
16*b2055c35SXin Li #if !(defined(WEBP_DISABLE_STATS) || defined(WEBP_REDUCE_SIZE))
17*b2055c35SXin Li
18*b2055c35SXin Li #include <math.h>
19*b2055c35SXin Li #include <stdlib.h>
20*b2055c35SXin Li
21*b2055c35SXin Li #include "src/dsp/dsp.h"
22*b2055c35SXin Li #include "src/enc/vp8i_enc.h"
23*b2055c35SXin Li #include "src/utils/utils.h"
24*b2055c35SXin Li
25*b2055c35SXin Li typedef double (*AccumulateFunc)(const uint8_t* src, int src_stride,
26*b2055c35SXin Li const uint8_t* ref, int ref_stride,
27*b2055c35SXin Li int w, int h);
28*b2055c35SXin Li
29*b2055c35SXin Li //------------------------------------------------------------------------------
30*b2055c35SXin Li // local-min distortion
31*b2055c35SXin Li //
32*b2055c35SXin Li // For every pixel in the *reference* picture, we search for the local best
33*b2055c35SXin Li // match in the compressed image. This is not a symmetrical measure.
34*b2055c35SXin Li
35*b2055c35SXin Li #define RADIUS 2 // search radius. Shouldn't be too large.
36*b2055c35SXin Li
AccumulateLSIM(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int w,int h)37*b2055c35SXin Li static double AccumulateLSIM(const uint8_t* src, int src_stride,
38*b2055c35SXin Li const uint8_t* ref, int ref_stride,
39*b2055c35SXin Li int w, int h) {
40*b2055c35SXin Li int x, y;
41*b2055c35SXin Li double total_sse = 0.;
42*b2055c35SXin Li for (y = 0; y < h; ++y) {
43*b2055c35SXin Li const int y_0 = (y - RADIUS < 0) ? 0 : y - RADIUS;
44*b2055c35SXin Li const int y_1 = (y + RADIUS + 1 >= h) ? h : y + RADIUS + 1;
45*b2055c35SXin Li for (x = 0; x < w; ++x) {
46*b2055c35SXin Li const int x_0 = (x - RADIUS < 0) ? 0 : x - RADIUS;
47*b2055c35SXin Li const int x_1 = (x + RADIUS + 1 >= w) ? w : x + RADIUS + 1;
48*b2055c35SXin Li double best_sse = 255. * 255.;
49*b2055c35SXin Li const double value = (double)ref[y * ref_stride + x];
50*b2055c35SXin Li int i, j;
51*b2055c35SXin Li for (j = y_0; j < y_1; ++j) {
52*b2055c35SXin Li const uint8_t* const s = src + j * src_stride;
53*b2055c35SXin Li for (i = x_0; i < x_1; ++i) {
54*b2055c35SXin Li const double diff = s[i] - value;
55*b2055c35SXin Li const double sse = diff * diff;
56*b2055c35SXin Li if (sse < best_sse) best_sse = sse;
57*b2055c35SXin Li }
58*b2055c35SXin Li }
59*b2055c35SXin Li total_sse += best_sse;
60*b2055c35SXin Li }
61*b2055c35SXin Li }
62*b2055c35SXin Li return total_sse;
63*b2055c35SXin Li }
64*b2055c35SXin Li #undef RADIUS
65*b2055c35SXin Li
AccumulateSSE(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int w,int h)66*b2055c35SXin Li static double AccumulateSSE(const uint8_t* src, int src_stride,
67*b2055c35SXin Li const uint8_t* ref, int ref_stride,
68*b2055c35SXin Li int w, int h) {
69*b2055c35SXin Li int y;
70*b2055c35SXin Li double total_sse = 0.;
71*b2055c35SXin Li for (y = 0; y < h; ++y) {
72*b2055c35SXin Li total_sse += VP8AccumulateSSE(src, ref, w);
73*b2055c35SXin Li src += src_stride;
74*b2055c35SXin Li ref += ref_stride;
75*b2055c35SXin Li }
76*b2055c35SXin Li return total_sse;
77*b2055c35SXin Li }
78*b2055c35SXin Li
79*b2055c35SXin Li //------------------------------------------------------------------------------
80*b2055c35SXin Li
AccumulateSSIM(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int w,int h)81*b2055c35SXin Li static double AccumulateSSIM(const uint8_t* src, int src_stride,
82*b2055c35SXin Li const uint8_t* ref, int ref_stride,
83*b2055c35SXin Li int w, int h) {
84*b2055c35SXin Li const int w0 = (w < VP8_SSIM_KERNEL) ? w : VP8_SSIM_KERNEL;
85*b2055c35SXin Li const int w1 = w - VP8_SSIM_KERNEL - 1;
86*b2055c35SXin Li const int h0 = (h < VP8_SSIM_KERNEL) ? h : VP8_SSIM_KERNEL;
87*b2055c35SXin Li const int h1 = h - VP8_SSIM_KERNEL - 1;
88*b2055c35SXin Li int x, y;
89*b2055c35SXin Li double sum = 0.;
90*b2055c35SXin Li for (y = 0; y < h0; ++y) {
91*b2055c35SXin Li for (x = 0; x < w; ++x) {
92*b2055c35SXin Li sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
93*b2055c35SXin Li }
94*b2055c35SXin Li }
95*b2055c35SXin Li for (; y < h1; ++y) {
96*b2055c35SXin Li for (x = 0; x < w0; ++x) {
97*b2055c35SXin Li sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
98*b2055c35SXin Li }
99*b2055c35SXin Li for (; x < w1; ++x) {
100*b2055c35SXin Li const int off1 = x - VP8_SSIM_KERNEL + (y - VP8_SSIM_KERNEL) * src_stride;
101*b2055c35SXin Li const int off2 = x - VP8_SSIM_KERNEL + (y - VP8_SSIM_KERNEL) * ref_stride;
102*b2055c35SXin Li sum += VP8SSIMGet(src + off1, src_stride, ref + off2, ref_stride);
103*b2055c35SXin Li }
104*b2055c35SXin Li for (; x < w; ++x) {
105*b2055c35SXin Li sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
106*b2055c35SXin Li }
107*b2055c35SXin Li }
108*b2055c35SXin Li for (; y < h; ++y) {
109*b2055c35SXin Li for (x = 0; x < w; ++x) {
110*b2055c35SXin Li sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
111*b2055c35SXin Li }
112*b2055c35SXin Li }
113*b2055c35SXin Li return sum;
114*b2055c35SXin Li }
115*b2055c35SXin Li
116*b2055c35SXin Li //------------------------------------------------------------------------------
117*b2055c35SXin Li // Distortion
118*b2055c35SXin Li
119*b2055c35SXin Li // Max value returned in case of exact similarity.
120*b2055c35SXin Li static const double kMinDistortion_dB = 99.;
121*b2055c35SXin Li
GetPSNR(double v,double size)122*b2055c35SXin Li static double GetPSNR(double v, double size) {
123*b2055c35SXin Li return (v > 0. && size > 0.) ? -4.3429448 * log(v / (size * 255 * 255.))
124*b2055c35SXin Li : kMinDistortion_dB;
125*b2055c35SXin Li }
126*b2055c35SXin Li
GetLogSSIM(double v,double size)127*b2055c35SXin Li static double GetLogSSIM(double v, double size) {
128*b2055c35SXin Li v = (size > 0.) ? v / size : 1.;
129*b2055c35SXin Li return (v < 1.) ? -10.0 * log10(1. - v) : kMinDistortion_dB;
130*b2055c35SXin Li }
131*b2055c35SXin Li
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,float * distortion,float * result)132*b2055c35SXin Li int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,
133*b2055c35SXin Li const uint8_t* ref, size_t ref_stride,
134*b2055c35SXin Li int width, int height, size_t x_step,
135*b2055c35SXin Li int type, float* distortion, float* result) {
136*b2055c35SXin Li uint8_t* allocated = NULL;
137*b2055c35SXin Li const AccumulateFunc metric = (type == 0) ? AccumulateSSE :
138*b2055c35SXin Li (type == 1) ? AccumulateSSIM :
139*b2055c35SXin Li AccumulateLSIM;
140*b2055c35SXin Li if (src == NULL || ref == NULL ||
141*b2055c35SXin Li src_stride < x_step * width || ref_stride < x_step * width ||
142*b2055c35SXin Li result == NULL || distortion == NULL) {
143*b2055c35SXin Li return 0;
144*b2055c35SXin Li }
145*b2055c35SXin Li
146*b2055c35SXin Li VP8SSIMDspInit();
147*b2055c35SXin Li if (x_step != 1) { // extract a packed plane if needed
148*b2055c35SXin Li int x, y;
149*b2055c35SXin Li uint8_t* tmp1;
150*b2055c35SXin Li uint8_t* tmp2;
151*b2055c35SXin Li allocated =
152*b2055c35SXin Li (uint8_t*)WebPSafeMalloc(2ULL * width * height, sizeof(*allocated));
153*b2055c35SXin Li if (allocated == NULL) return 0;
154*b2055c35SXin Li tmp1 = allocated;
155*b2055c35SXin Li tmp2 = tmp1 + (size_t)width * height;
156*b2055c35SXin Li for (y = 0; y < height; ++y) {
157*b2055c35SXin Li for (x = 0; x < width; ++x) {
158*b2055c35SXin Li tmp1[x + y * width] = src[x * x_step + y * src_stride];
159*b2055c35SXin Li tmp2[x + y * width] = ref[x * x_step + y * ref_stride];
160*b2055c35SXin Li }
161*b2055c35SXin Li }
162*b2055c35SXin Li src = tmp1;
163*b2055c35SXin Li ref = tmp2;
164*b2055c35SXin Li }
165*b2055c35SXin Li *distortion = (float)metric(src, width, ref, width, width, height);
166*b2055c35SXin Li WebPSafeFree(allocated);
167*b2055c35SXin Li
168*b2055c35SXin Li *result = (type == 1) ? (float)GetLogSSIM(*distortion, (double)width * height)
169*b2055c35SXin Li : (float)GetPSNR(*distortion, (double)width * height);
170*b2055c35SXin Li return 1;
171*b2055c35SXin Li }
172*b2055c35SXin Li
173*b2055c35SXin Li #ifdef WORDS_BIGENDIAN
174*b2055c35SXin Li #define BLUE_OFFSET 3 // uint32_t 0x000000ff is 0x00,00,00,ff in memory
175*b2055c35SXin Li #else
176*b2055c35SXin Li #define BLUE_OFFSET 0 // uint32_t 0x000000ff is 0xff,00,00,00 in memory
177*b2055c35SXin Li #endif
178*b2055c35SXin Li
WebPPictureDistortion(const WebPPicture * src,const WebPPicture * ref,int type,float results[5])179*b2055c35SXin Li int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
180*b2055c35SXin Li int type, float results[5]) {
181*b2055c35SXin Li int w, h, c;
182*b2055c35SXin Li int ok = 0;
183*b2055c35SXin Li WebPPicture p0, p1;
184*b2055c35SXin Li double total_size = 0., total_distortion = 0.;
185*b2055c35SXin Li if (src == NULL || ref == NULL ||
186*b2055c35SXin Li src->width != ref->width || src->height != ref->height ||
187*b2055c35SXin Li results == NULL) {
188*b2055c35SXin Li return 0;
189*b2055c35SXin Li }
190*b2055c35SXin Li
191*b2055c35SXin Li VP8SSIMDspInit();
192*b2055c35SXin Li if (!WebPPictureInit(&p0) || !WebPPictureInit(&p1)) return 0;
193*b2055c35SXin Li w = src->width;
194*b2055c35SXin Li h = src->height;
195*b2055c35SXin Li if (!WebPPictureView(src, 0, 0, w, h, &p0)) goto Error;
196*b2055c35SXin Li if (!WebPPictureView(ref, 0, 0, w, h, &p1)) goto Error;
197*b2055c35SXin Li
198*b2055c35SXin Li // We always measure distortion in ARGB space.
199*b2055c35SXin Li if (p0.use_argb == 0 && !WebPPictureYUVAToARGB(&p0)) goto Error;
200*b2055c35SXin Li if (p1.use_argb == 0 && !WebPPictureYUVAToARGB(&p1)) goto Error;
201*b2055c35SXin Li for (c = 0; c < 4; ++c) {
202*b2055c35SXin Li float distortion;
203*b2055c35SXin Li const size_t stride0 = 4 * (size_t)p0.argb_stride;
204*b2055c35SXin Li const size_t stride1 = 4 * (size_t)p1.argb_stride;
205*b2055c35SXin Li // results are reported as BGRA
206*b2055c35SXin Li const int offset = c ^ BLUE_OFFSET;
207*b2055c35SXin Li if (!WebPPlaneDistortion((const uint8_t*)p0.argb + offset, stride0,
208*b2055c35SXin Li (const uint8_t*)p1.argb + offset, stride1,
209*b2055c35SXin Li w, h, 4, type, &distortion, results + c)) {
210*b2055c35SXin Li goto Error;
211*b2055c35SXin Li }
212*b2055c35SXin Li total_distortion += distortion;
213*b2055c35SXin Li total_size += w * h;
214*b2055c35SXin Li }
215*b2055c35SXin Li
216*b2055c35SXin Li results[4] = (type == 1) ? (float)GetLogSSIM(total_distortion, total_size)
217*b2055c35SXin Li : (float)GetPSNR(total_distortion, total_size);
218*b2055c35SXin Li ok = 1;
219*b2055c35SXin Li
220*b2055c35SXin Li Error:
221*b2055c35SXin Li WebPPictureFree(&p0);
222*b2055c35SXin Li WebPPictureFree(&p1);
223*b2055c35SXin Li return ok;
224*b2055c35SXin Li }
225*b2055c35SXin Li
226*b2055c35SXin Li #undef BLUE_OFFSET
227*b2055c35SXin Li
228*b2055c35SXin Li #else // defined(WEBP_DISABLE_STATS)
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,float * distortion,float * result)229*b2055c35SXin Li int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,
230*b2055c35SXin Li const uint8_t* ref, size_t ref_stride,
231*b2055c35SXin Li int width, int height, size_t x_step,
232*b2055c35SXin Li int type, float* distortion, float* result) {
233*b2055c35SXin Li (void)src;
234*b2055c35SXin Li (void)src_stride;
235*b2055c35SXin Li (void)ref;
236*b2055c35SXin Li (void)ref_stride;
237*b2055c35SXin Li (void)width;
238*b2055c35SXin Li (void)height;
239*b2055c35SXin Li (void)x_step;
240*b2055c35SXin Li (void)type;
241*b2055c35SXin Li if (distortion == NULL || result == NULL) return 0;
242*b2055c35SXin Li *distortion = 0.f;
243*b2055c35SXin Li *result = 0.f;
244*b2055c35SXin Li return 1;
245*b2055c35SXin Li }
246*b2055c35SXin Li
WebPPictureDistortion(const WebPPicture * src,const WebPPicture * ref,int type,float results[5])247*b2055c35SXin Li int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
248*b2055c35SXin Li int type, float results[5]) {
249*b2055c35SXin Li int i;
250*b2055c35SXin Li (void)src;
251*b2055c35SXin Li (void)ref;
252*b2055c35SXin Li (void)type;
253*b2055c35SXin Li if (results == NULL) return 0;
254*b2055c35SXin Li for (i = 0; i < 5; ++i) results[i] = 0.f;
255*b2055c35SXin Li return 1;
256*b2055c35SXin Li }
257*b2055c35SXin Li
258*b2055c35SXin Li #endif // !defined(WEBP_DISABLE_STATS)
259