• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/compare_row.h"
21 #include "libyuv/cpu_id.h"
22 #include "libyuv/row.h"
23 #include "libyuv/video_common.h"
24 
25 #ifdef __cplusplus
26 namespace libyuv {
27 extern "C" {
28 #endif
29 
30 // hash seed of 5381 recommended.
31 LIBYUV_API
HashDjb2(const uint8_t * src,uint64_t count,uint32_t seed)32 uint32_t HashDjb2(const uint8_t* src, uint64_t count, uint32_t seed) {
33   const int kBlockSize = 1 << 15;  // 32768;
34   int remainder;
35   uint32_t (*HashDjb2_SSE)(const uint8_t* src, int count, uint32_t seed) =
36       HashDjb2_C;
37 #if defined(HAS_HASHDJB2_SSE41)
38   if (TestCpuFlag(kCpuHasSSE41)) {
39     HashDjb2_SSE = HashDjb2_SSE41;
40   }
41 #endif
42 #if defined(HAS_HASHDJB2_AVX2)
43   if (TestCpuFlag(kCpuHasAVX2)) {
44     HashDjb2_SSE = HashDjb2_AVX2;
45   }
46 #endif
47 
48   while (count >= (uint64_t)(kBlockSize)) {
49     seed = HashDjb2_SSE(src, kBlockSize, seed);
50     src += kBlockSize;
51     count -= kBlockSize;
52   }
53   remainder = (int)count & ~15;
54   if (remainder) {
55     seed = HashDjb2_SSE(src, remainder, seed);
56     src += remainder;
57     count -= remainder;
58   }
59   remainder = (int)count & 15;
60   if (remainder) {
61     seed = HashDjb2_C(src, remainder, seed);
62   }
63   return seed;
64 }
65 
ARGBDetectRow_C(const uint8_t * argb,int width)66 static uint32_t ARGBDetectRow_C(const uint8_t* argb, int width) {
67   int x;
68   for (x = 0; x < width - 1; x += 2) {
69     if (argb[0] != 255) {  // First byte is not Alpha of 255, so not ARGB.
70       return FOURCC_BGRA;
71     }
72     if (argb[3] != 255) {  // Fourth byte is not Alpha of 255, so not BGRA.
73       return FOURCC_ARGB;
74     }
75     if (argb[4] != 255) {  // Second pixel first byte is not Alpha of 255.
76       return FOURCC_BGRA;
77     }
78     if (argb[7] != 255) {  // Second pixel fourth byte is not Alpha of 255.
79       return FOURCC_ARGB;
80     }
81     argb += 8;
82   }
83   if (width & 1) {
84     if (argb[0] != 255) {  // First byte is not Alpha of 255, so not ARGB.
85       return FOURCC_BGRA;
86     }
87     if (argb[3] != 255) {  // 4th byte is not Alpha of 255, so not BGRA.
88       return FOURCC_ARGB;
89     }
90   }
91   return 0;
92 }
93 
94 // Scan an opaque argb image and return fourcc based on alpha offset.
95 // Returns FOURCC_ARGB, FOURCC_BGRA, or 0 if unknown.
96 LIBYUV_API
ARGBDetect(const uint8_t * argb,int stride_argb,int width,int height)97 uint32_t ARGBDetect(const uint8_t* argb,
98                     int stride_argb,
99                     int width,
100                     int height) {
101   uint32_t fourcc = 0;
102   int h;
103 
104   // Coalesce rows.
105   if (stride_argb == width * 4) {
106     width *= height;
107     height = 1;
108     stride_argb = 0;
109   }
110   for (h = 0; h < height && fourcc == 0; ++h) {
111     fourcc = ARGBDetectRow_C(argb, width);
112     argb += stride_argb;
113   }
114   return fourcc;
115 }
116 
117 // NEON version accumulates in 16 bit shorts which overflow at 65536 bytes.
118 // So actual maximum is 1 less loop, which is 64436 - 32 bytes.
119 
120 LIBYUV_API
ComputeHammingDistance(const uint8_t * src_a,const uint8_t * src_b,int count)121 uint64_t ComputeHammingDistance(const uint8_t* src_a,
122                                 const uint8_t* src_b,
123                                 int count) {
124   const int kBlockSize = 1 << 15;  // 32768;
125   const int kSimdSize = 64;
126   // SIMD for multiple of 64, and C for remainder
127   int remainder = count & (kBlockSize - 1) & ~(kSimdSize - 1);
128   uint64_t diff = 0;
129   int i;
130   uint32_t (*HammingDistance)(const uint8_t* src_a, const uint8_t* src_b,
131                               int count) = HammingDistance_C;
132 #if defined(HAS_HAMMINGDISTANCE_NEON)
133   if (TestCpuFlag(kCpuHasNEON)) {
134     HammingDistance = HammingDistance_NEON;
135   }
136 #endif
137 #if defined(HAS_HAMMINGDISTANCE_SSSE3)
138   if (TestCpuFlag(kCpuHasSSSE3)) {
139     HammingDistance = HammingDistance_SSSE3;
140   }
141 #endif
142 #if defined(HAS_HAMMINGDISTANCE_SSE42)
143   if (TestCpuFlag(kCpuHasSSE42)) {
144     HammingDistance = HammingDistance_SSE42;
145   }
146 #endif
147 #if defined(HAS_HAMMINGDISTANCE_AVX2)
148   if (TestCpuFlag(kCpuHasAVX2)) {
149     HammingDistance = HammingDistance_AVX2;
150   }
151 #endif
152 #if defined(HAS_HAMMINGDISTANCE_MSA)
153   if (TestCpuFlag(kCpuHasMSA)) {
154     HammingDistance = HammingDistance_MSA;
155   }
156 #endif
157 
158 #ifdef _OPENMP
159 #pragma omp parallel for reduction(+ : diff)
160 #endif
161   for (i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) {
162     diff += HammingDistance(src_a + i, src_b + i, kBlockSize);
163   }
164   src_a += count & ~(kBlockSize - 1);
165   src_b += count & ~(kBlockSize - 1);
166   if (remainder) {
167     diff += HammingDistance(src_a, src_b, remainder);
168     src_a += remainder;
169     src_b += remainder;
170   }
171   remainder = count & (kSimdSize - 1);
172   if (remainder) {
173     diff += HammingDistance_C(src_a, src_b, remainder);
174   }
175   return diff;
176 }
177 
178 // TODO(fbarchard): Refactor into row function.
179 LIBYUV_API
ComputeSumSquareError(const uint8_t * src_a,const uint8_t * src_b,int count)180 uint64_t ComputeSumSquareError(const uint8_t* src_a,
181                                const uint8_t* src_b,
182                                int count) {
183   // SumSquareError returns values 0 to 65535 for each squared difference.
184   // Up to 65536 of those can be summed and remain within a uint32_t.
185   // After each block of 65536 pixels, accumulate into a uint64_t.
186   const int kBlockSize = 65536;
187   int remainder = count & (kBlockSize - 1) & ~31;
188   uint64_t sse = 0;
189   int i;
190   uint32_t (*SumSquareError)(const uint8_t* src_a, const uint8_t* src_b,
191                              int count) = SumSquareError_C;
192 #if defined(HAS_SUMSQUAREERROR_NEON)
193   if (TestCpuFlag(kCpuHasNEON)) {
194     SumSquareError = SumSquareError_NEON;
195   }
196 #endif
197 #if defined(HAS_SUMSQUAREERROR_SSE2)
198   if (TestCpuFlag(kCpuHasSSE2)) {
199     // Note only used for multiples of 16 so count is not checked.
200     SumSquareError = SumSquareError_SSE2;
201   }
202 #endif
203 #if defined(HAS_SUMSQUAREERROR_AVX2)
204   if (TestCpuFlag(kCpuHasAVX2)) {
205     // Note only used for multiples of 32 so count is not checked.
206     SumSquareError = SumSquareError_AVX2;
207   }
208 #endif
209 #if defined(HAS_SUMSQUAREERROR_MSA)
210   if (TestCpuFlag(kCpuHasMSA)) {
211     SumSquareError = SumSquareError_MSA;
212   }
213 #endif
214 #ifdef _OPENMP
215 #pragma omp parallel for reduction(+ : sse)
216 #endif
217   for (i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) {
218     sse += SumSquareError(src_a + i, src_b + i, kBlockSize);
219   }
220   src_a += count & ~(kBlockSize - 1);
221   src_b += count & ~(kBlockSize - 1);
222   if (remainder) {
223     sse += SumSquareError(src_a, src_b, remainder);
224     src_a += remainder;
225     src_b += remainder;
226   }
227   remainder = count & 31;
228   if (remainder) {
229     sse += SumSquareError_C(src_a, src_b, remainder);
230   }
231   return sse;
232 }
233 
234 LIBYUV_API
ComputeSumSquareErrorPlane(const uint8_t * src_a,int stride_a,const uint8_t * src_b,int stride_b,int width,int height)235 uint64_t ComputeSumSquareErrorPlane(const uint8_t* src_a,
236                                     int stride_a,
237                                     const uint8_t* src_b,
238                                     int stride_b,
239                                     int width,
240                                     int height) {
241   uint64_t sse = 0;
242   int h;
243   // Coalesce rows.
244   if (stride_a == width && stride_b == width) {
245     width *= height;
246     height = 1;
247     stride_a = stride_b = 0;
248   }
249   for (h = 0; h < height; ++h) {
250     sse += ComputeSumSquareError(src_a, src_b, width);
251     src_a += stride_a;
252     src_b += stride_b;
253   }
254   return sse;
255 }
256 
257 LIBYUV_API
SumSquareErrorToPsnr(uint64_t sse,uint64_t count)258 double SumSquareErrorToPsnr(uint64_t sse, uint64_t count) {
259   double psnr;
260   if (sse > 0) {
261     double mse = (double)count / (double)sse;
262     psnr = 10.0 * log10(255.0 * 255.0 * mse);
263   } else {
264     psnr = kMaxPsnr;  // Limit to prevent divide by 0
265   }
266 
267   if (psnr > kMaxPsnr) {
268     psnr = kMaxPsnr;
269   }
270 
271   return psnr;
272 }
273 
274 LIBYUV_API
CalcFramePsnr(const uint8_t * src_a,int stride_a,const uint8_t * src_b,int stride_b,int width,int height)275 double CalcFramePsnr(const uint8_t* src_a,
276                      int stride_a,
277                      const uint8_t* src_b,
278                      int stride_b,
279                      int width,
280                      int height) {
281   const uint64_t samples = (uint64_t)width * (uint64_t)height;
282   const uint64_t sse = ComputeSumSquareErrorPlane(src_a, stride_a, src_b,
283                                                   stride_b, width, height);
284   return SumSquareErrorToPsnr(sse, samples);
285 }
286 
287 LIBYUV_API
I420Psnr(const uint8_t * src_y_a,int stride_y_a,const uint8_t * src_u_a,int stride_u_a,const uint8_t * src_v_a,int stride_v_a,const uint8_t * src_y_b,int stride_y_b,const uint8_t * src_u_b,int stride_u_b,const uint8_t * src_v_b,int stride_v_b,int width,int height)288 double I420Psnr(const uint8_t* src_y_a,
289                 int stride_y_a,
290                 const uint8_t* src_u_a,
291                 int stride_u_a,
292                 const uint8_t* src_v_a,
293                 int stride_v_a,
294                 const uint8_t* src_y_b,
295                 int stride_y_b,
296                 const uint8_t* src_u_b,
297                 int stride_u_b,
298                 const uint8_t* src_v_b,
299                 int stride_v_b,
300                 int width,
301                 int height) {
302   const uint64_t sse_y = ComputeSumSquareErrorPlane(
303       src_y_a, stride_y_a, src_y_b, stride_y_b, width, height);
304   const int width_uv = (width + 1) >> 1;
305   const int height_uv = (height + 1) >> 1;
306   const uint64_t sse_u = ComputeSumSquareErrorPlane(
307       src_u_a, stride_u_a, src_u_b, stride_u_b, width_uv, height_uv);
308   const uint64_t sse_v = ComputeSumSquareErrorPlane(
309       src_v_a, stride_v_a, src_v_b, stride_v_b, width_uv, height_uv);
310   const uint64_t samples = (uint64_t)width * (uint64_t)height +
311                            2 * ((uint64_t)width_uv * (uint64_t)height_uv);
312   const uint64_t sse = sse_y + sse_u + sse_v;
313   return SumSquareErrorToPsnr(sse, samples);
314 }
315 
316 static const int64_t cc1 = 26634;   // (64^2*(.01*255)^2
317 static const int64_t cc2 = 239708;  // (64^2*(.03*255)^2
318 
Ssim8x8_C(const uint8_t * src_a,int stride_a,const uint8_t * src_b,int stride_b)319 static double Ssim8x8_C(const uint8_t* src_a,
320                         int stride_a,
321                         const uint8_t* src_b,
322                         int stride_b) {
323   int64_t sum_a = 0;
324   int64_t sum_b = 0;
325   int64_t sum_sq_a = 0;
326   int64_t sum_sq_b = 0;
327   int64_t sum_axb = 0;
328 
329   int i;
330   for (i = 0; i < 8; ++i) {
331     int j;
332     for (j = 0; j < 8; ++j) {
333       sum_a += src_a[j];
334       sum_b += src_b[j];
335       sum_sq_a += src_a[j] * src_a[j];
336       sum_sq_b += src_b[j] * src_b[j];
337       sum_axb += src_a[j] * src_b[j];
338     }
339 
340     src_a += stride_a;
341     src_b += stride_b;
342   }
343 
344   {
345     const int64_t count = 64;
346     // scale the constants by number of pixels
347     const int64_t c1 = (cc1 * count * count) >> 12;
348     const int64_t c2 = (cc2 * count * count) >> 12;
349 
350     const int64_t sum_a_x_sum_b = sum_a * sum_b;
351 
352     const int64_t ssim_n = (2 * sum_a_x_sum_b + c1) *
353                            (2 * count * sum_axb - 2 * sum_a_x_sum_b + c2);
354 
355     const int64_t sum_a_sq = sum_a * sum_a;
356     const int64_t sum_b_sq = sum_b * sum_b;
357 
358     const int64_t ssim_d =
359         (sum_a_sq + sum_b_sq + c1) *
360         (count * sum_sq_a - sum_a_sq + count * sum_sq_b - sum_b_sq + c2);
361 
362     if (ssim_d == 0.0) {
363       return DBL_MAX;
364     }
365     return ssim_n * 1.0 / ssim_d;
366   }
367 }
368 
369 // We are using a 8x8 moving window with starting location of each 8x8 window
370 // on the 4x4 pixel grid. Such arrangement allows the windows to overlap
371 // block boundaries to penalize blocking artifacts.
372 LIBYUV_API
CalcFrameSsim(const uint8_t * src_a,int stride_a,const uint8_t * src_b,int stride_b,int width,int height)373 double CalcFrameSsim(const uint8_t* src_a,
374                      int stride_a,
375                      const uint8_t* src_b,
376                      int stride_b,
377                      int width,
378                      int height) {
379   int samples = 0;
380   double ssim_total = 0;
381   double (*Ssim8x8)(const uint8_t* src_a, int stride_a, const uint8_t* src_b,
382                     int stride_b) = Ssim8x8_C;
383 
384   // sample point start with each 4x4 location
385   int i;
386   for (i = 0; i < height - 8; i += 4) {
387     int j;
388     for (j = 0; j < width - 8; j += 4) {
389       ssim_total += Ssim8x8(src_a + j, stride_a, src_b + j, stride_b);
390       samples++;
391     }
392 
393     src_a += stride_a * 4;
394     src_b += stride_b * 4;
395   }
396 
397   ssim_total /= samples;
398   return ssim_total;
399 }
400 
401 LIBYUV_API
I420Ssim(const uint8_t * src_y_a,int stride_y_a,const uint8_t * src_u_a,int stride_u_a,const uint8_t * src_v_a,int stride_v_a,const uint8_t * src_y_b,int stride_y_b,const uint8_t * src_u_b,int stride_u_b,const uint8_t * src_v_b,int stride_v_b,int width,int height)402 double I420Ssim(const uint8_t* src_y_a,
403                 int stride_y_a,
404                 const uint8_t* src_u_a,
405                 int stride_u_a,
406                 const uint8_t* src_v_a,
407                 int stride_v_a,
408                 const uint8_t* src_y_b,
409                 int stride_y_b,
410                 const uint8_t* src_u_b,
411                 int stride_u_b,
412                 const uint8_t* src_v_b,
413                 int stride_v_b,
414                 int width,
415                 int height) {
416   const double ssim_y =
417       CalcFrameSsim(src_y_a, stride_y_a, src_y_b, stride_y_b, width, height);
418   const int width_uv = (width + 1) >> 1;
419   const int height_uv = (height + 1) >> 1;
420   const double ssim_u = CalcFrameSsim(src_u_a, stride_u_a, src_u_b, stride_u_b,
421                                       width_uv, height_uv);
422   const double ssim_v = CalcFrameSsim(src_v_a, stride_v_a, src_v_b, stride_v_b,
423                                       width_uv, height_uv);
424   return ssim_y * 0.8 + 0.1 * (ssim_u + ssim_v);
425 }
426 
427 #ifdef __cplusplus
428 }  // extern "C"
429 }  // namespace libyuv
430 #endif
431