Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libyuv / source / compare.cc
1 /*
2  *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS. All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "libyuv/compare.h"
12
13 #include <float.h>
14 #include <math.h>
15 #ifdef _OPENMP
16 #include <omp.h>
17 #endif
18
19 #include "libyuv/basic_types.h"
20 #include "libyuv/cpu_id.h"
21 #include "libyuv/row.h"
22
23 #ifdef __cplusplus
24 namespace libyuv {
25 extern "C" {
26 #endif
27
28 // hash seed of 5381 recommended.
29 // Internal C version of HashDjb2 with int sized count for efficiency.
30 uint32 HashDjb2_C(const uint8* src, int count, uint32 seed);
31
32 // This module is for Visual C x86
33 #if !defined(LIBYUV_DISABLE_X86) && \
34     (defined(_M_IX86) || \
35     (defined(__x86_64__) || (defined(__i386__) && !defined(__pic__))))
36 #define HAS_HASHDJB2_SSE41
37 uint32 HashDjb2_SSE41(const uint8* src, int count, uint32 seed);
38
39 #if _MSC_VER >= 1700
40 #define HAS_HASHDJB2_AVX2
41 uint32 HashDjb2_AVX2(const uint8* src, int count, uint32 seed);
42 #endif
43
44 #endif  // HAS_HASHDJB2_SSE41
45
46 // hash seed of 5381 recommended.
47 LIBYUV_API
48 uint32 HashDjb2(const uint8* src, uint64 count, uint32 seed) {
49   const int kBlockSize = 1 << 15;  // 32768;
50   int remainder;
51   uint32 (*HashDjb2_SSE)(const uint8* src, int count, uint32 seed) = HashDjb2_C;
52 #if defined(HAS_HASHDJB2_SSE41)
53   if (TestCpuFlag(kCpuHasSSE41)) {
54     HashDjb2_SSE = HashDjb2_SSE41;
55   }
56 #endif
57 #if defined(HAS_HASHDJB2_AVX2)
58   if (TestCpuFlag(kCpuHasAVX2)) {
59     HashDjb2_SSE = HashDjb2_AVX2;
60   }
61 #endif
62
63   while (count >= (uint64)(kBlockSize)) {
64     seed = HashDjb2_SSE(src, kBlockSize, seed);
65     src += kBlockSize;
66     count -= kBlockSize;
67   }
68   remainder = (int)(count) & ~15;
69   if (remainder) {
70     seed = HashDjb2_SSE(src, remainder, seed);
71     src += remainder;
72     count -= remainder;
73   }
74   remainder = (int)(count) & 15;
75   if (remainder) {
76     seed = HashDjb2_C(src, remainder, seed);
77   }
78   return seed;
79 }
80
81 uint32 SumSquareError_C(const uint8* src_a, const uint8* src_b, int count);
82 #if !defined(LIBYUV_DISABLE_NEON) && \
83     (defined(__ARM_NEON__) || defined(LIBYUV_NEON) || defined(__aarch64__))
84 #define HAS_SUMSQUAREERROR_NEON
85 uint32 SumSquareError_NEON(const uint8* src_a, const uint8* src_b, int count);
86 #endif
87 #if !defined(LIBYUV_DISABLE_X86) && \
88     (defined(_M_IX86) || defined(__x86_64__) || defined(__i386__))
89 #define HAS_SUMSQUAREERROR_SSE2
90 uint32 SumSquareError_SSE2(const uint8* src_a, const uint8* src_b, int count);
91 #endif
92 // Visual C 2012 required for AVX2.
93 #if !defined(LIBYUV_DISABLE_X86) && defined(_M_IX86) && _MSC_VER >= 1700
94 #define HAS_SUMSQUAREERROR_AVX2
95 uint32 SumSquareError_AVX2(const uint8* src_a, const uint8* src_b, int count);
96 #endif
97
98 // TODO(fbarchard): Refactor into row function.
99 LIBYUV_API
100 uint64 ComputeSumSquareError(const uint8* src_a, const uint8* src_b,
101                              int count) {
102   // SumSquareError returns values 0 to 65535 for each squared difference.
103   // Up to 65536 of those can be summed and remain within a uint32.
104   // After each block of 65536 pixels, accumulate into a uint64.
105   const int kBlockSize = 65536;
106   int remainder = count & (kBlockSize - 1) & ~31;
107   uint64 sse = 0;
108   int i;
109   uint32 (*SumSquareError)(const uint8* src_a, const uint8* src_b, int count) =
110       SumSquareError_C;
111 #if defined(HAS_SUMSQUAREERROR_NEON)
112   if (TestCpuFlag(kCpuHasNEON)) {
113     SumSquareError = SumSquareError_NEON;
114   }
115 #endif
116 #if defined(HAS_SUMSQUAREERROR_SSE2)
117   if (TestCpuFlag(kCpuHasSSE2)) {
118     // Note only used for multiples of 16 so count is not checked.
119     SumSquareError = SumSquareError_SSE2;
120   }
121 #endif
122 #if defined(HAS_SUMSQUAREERROR_AVX2)
123   if (TestCpuFlag(kCpuHasAVX2)) {
124     // Note only used for multiples of 32 so count is not checked.
125     SumSquareError = SumSquareError_AVX2;
126   }
127 #endif
128 #ifdef _OPENMP
129 #pragma omp parallel for reduction(+: sse)
130 #endif
131   for (i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) {
132     sse += SumSquareError(src_a + i, src_b + i, kBlockSize);
133   }
134   src_a += count & ~(kBlockSize - 1);
135   src_b += count & ~(kBlockSize - 1);
136   if (remainder) {
137     sse += SumSquareError(src_a, src_b, remainder);
138     src_a += remainder;
139     src_b += remainder;
140   }
141   remainder = count & 31;
142   if (remainder) {
143     sse += SumSquareError_C(src_a, src_b, remainder);
144   }
145   return sse;
146 }
147
148 LIBYUV_API
149 uint64 ComputeSumSquareErrorPlane(const uint8* src_a, int stride_a,
150                                   const uint8* src_b, int stride_b,
151                                   int width, int height) {
152   uint64 sse = 0;
153   int h;
154   // Coalesce rows.
155   if (stride_a == width &&
156       stride_b == width) {
157     width *= height;
158     height = 1;
159     stride_a = stride_b = 0;
160   }
161   for (h = 0; h < height; ++h) {
162     sse += ComputeSumSquareError(src_a, src_b, width);
163     src_a += stride_a;
164     src_b += stride_b;
165   }
166   return sse;
167 }
168
169 LIBYUV_API
170 double SumSquareErrorToPsnr(uint64 sse, uint64 count) {
171   double psnr;
172   if (sse > 0) {
173     double mse = (double)(count) / (double)(sse);
174     psnr = 10.0 * log10(255.0 * 255.0 * mse);
175   } else {
176     psnr = kMaxPsnr;      // Limit to prevent divide by 0
177   }
178
179   if (psnr > kMaxPsnr)
180     psnr = kMaxPsnr;
181
182   return psnr;
183 }
184
185 LIBYUV_API
186 double CalcFramePsnr(const uint8* src_a, int stride_a,
187                      const uint8* src_b, int stride_b,
188                      int width, int height) {
189   const uint64 samples = width * height;
190   const uint64 sse = ComputeSumSquareErrorPlane(src_a, stride_a,
191                                                 src_b, stride_b,
192                                                 width, height);
193   return SumSquareErrorToPsnr(sse, samples);
194 }
195
196 LIBYUV_API
197 double I420Psnr(const uint8* src_y_a, int stride_y_a,
198                 const uint8* src_u_a, int stride_u_a,
199                 const uint8* src_v_a, int stride_v_a,
200                 const uint8* src_y_b, int stride_y_b,
201                 const uint8* src_u_b, int stride_u_b,
202                 const uint8* src_v_b, int stride_v_b,
203                 int width, int height) {
204   const uint64 sse_y = ComputeSumSquareErrorPlane(src_y_a, stride_y_a,
205                                                   src_y_b, stride_y_b,
206                                                   width, height);
207   const int width_uv = (width + 1) >> 1;
208   const int height_uv = (height + 1) >> 1;
209   const uint64 sse_u = ComputeSumSquareErrorPlane(src_u_a, stride_u_a,
210                                                   src_u_b, stride_u_b,
211                                                   width_uv, height_uv);
212   const uint64 sse_v = ComputeSumSquareErrorPlane(src_v_a, stride_v_a,
213                                                   src_v_b, stride_v_b,
214                                                   width_uv, height_uv);
215   const uint64 samples = width * height + 2 * (width_uv * height_uv);
216   const uint64 sse = sse_y + sse_u + sse_v;
217   return SumSquareErrorToPsnr(sse, samples);
218 }
219
220 static const int64 cc1 =  26634;  // (64^2*(.01*255)^2
221 static const int64 cc2 = 239708;  // (64^2*(.03*255)^2
222
223 static double Ssim8x8_C(const uint8* src_a, int stride_a,
224                         const uint8* src_b, int stride_b) {
225   int64 sum_a = 0;
226   int64 sum_b = 0;
227   int64 sum_sq_a = 0;
228   int64 sum_sq_b = 0;
229   int64 sum_axb = 0;
230
231   int i;
232   for (i = 0; i < 8; ++i) {
233     int j;
234     for (j = 0; j < 8; ++j) {
235       sum_a += src_a[j];
236       sum_b += src_b[j];
237       sum_sq_a += src_a[j] * src_a[j];
238       sum_sq_b += src_b[j] * src_b[j];
239       sum_axb += src_a[j] * src_b[j];
240     }
241
242     src_a += stride_a;
243     src_b += stride_b;
244   }
245
246   {
247     const int64 count = 64;
248     // scale the constants by number of pixels
249     const int64 c1 = (cc1 * count * count) >> 12;
250     const int64 c2 = (cc2 * count * count) >> 12;
251
252     const int64 sum_a_x_sum_b = sum_a * sum_b;
253
254     const int64 ssim_n = (2 * sum_a_x_sum_b + c1) *
255                          (2 * count * sum_axb - 2 * sum_a_x_sum_b + c2);
256
257     const int64 sum_a_sq = sum_a*sum_a;
258     const int64 sum_b_sq = sum_b*sum_b;
259
260     const int64 ssim_d = (sum_a_sq + sum_b_sq + c1) *
261                          (count * sum_sq_a - sum_a_sq +
262                           count * sum_sq_b - sum_b_sq + c2);
263
264     if (ssim_d == 0.0) {
265       return DBL_MAX;
266     }
267     return ssim_n * 1.0 / ssim_d;
268   }
269 }
270
271 // We are using a 8x8 moving window with starting location of each 8x8 window
272 // on the 4x4 pixel grid. Such arrangement allows the windows to overlap
273 // block boundaries to penalize blocking artifacts.
274 LIBYUV_API
275 double CalcFrameSsim(const uint8* src_a, int stride_a,
276                      const uint8* src_b, int stride_b,
277                      int width, int height) {
278   int samples = 0;
279   double ssim_total = 0;
280   double (*Ssim8x8)(const uint8* src_a, int stride_a,
281                     const uint8* src_b, int stride_b) = Ssim8x8_C;
282
283   // sample point start with each 4x4 location
284   int i;
285   for (i = 0; i < height - 8; i += 4) {
286     int j;
287     for (j = 0; j < width - 8; j += 4) {
288       ssim_total += Ssim8x8(src_a + j, stride_a, src_b + j, stride_b);
289       samples++;
290     }
291
292     src_a += stride_a * 4;
293     src_b += stride_b * 4;
294   }
295
296   ssim_total /= samples;
297   return ssim_total;
298 }
299
300 LIBYUV_API
301 double I420Ssim(const uint8* src_y_a, int stride_y_a,
302                 const uint8* src_u_a, int stride_u_a,
303                 const uint8* src_v_a, int stride_v_a,
304                 const uint8* src_y_b, int stride_y_b,
305                 const uint8* src_u_b, int stride_u_b,
306                 const uint8* src_v_b, int stride_v_b,
307                 int width, int height) {
308   const double ssim_y = CalcFrameSsim(src_y_a, stride_y_a,
309                                       src_y_b, stride_y_b, width, height);
310   const int width_uv = (width + 1) >> 1;
311   const int height_uv = (height + 1) >> 1;
312   const double ssim_u = CalcFrameSsim(src_u_a, stride_u_a,
313                                       src_u_b, stride_u_b,
314                                       width_uv, height_uv);
315   const double ssim_v = CalcFrameSsim(src_v_a, stride_v_a,
316                                       src_v_b, stride_v_b,
317                                       width_uv, height_uv);
318   return ssim_y * 0.8 + 0.1 * (ssim_u + ssim_v);
319 }
320
321 #ifdef __cplusplus
322 }  // extern "C"
323 }  // namespace libyuv
324 #endif