psnr.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2013 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. #include "./psnr.h" // NOLINT
  11. #ifdef _OPENMP
  12. #include <omp.h>
  13. #endif
  14. #ifdef _MSC_VER
  15. #include <intrin.h> // For __cpuid()
  16. #endif
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. typedef unsigned int uint32; // NOLINT
  21. #ifdef _MSC_VER
  22. typedef unsigned __int64 uint64;
  23. #else // COMPILER_MSVC
  24. #if defined(__LP64__) && !defined(__OpenBSD__) && !defined(__APPLE__)
  25. typedef unsigned long uint64; // NOLINT
  26. #else // defined(__LP64__) && !defined(__OpenBSD__) && !defined(__APPLE__)
  27. typedef unsigned long long uint64; // NOLINT
  28. #endif // __LP64__
  29. #endif // _MSC_VER
  30. // libyuv provides this function when linking library for jpeg support.
  31. #if !defined(HAVE_JPEG)
  32. #if !defined(LIBYUV_DISABLE_NEON) && defined(__ARM_NEON__) && \
  33. !defined(__aarch64__)
  34. #define HAS_SUMSQUAREERROR_NEON
  35. static uint32 SumSquareError_NEON(const uint8* src_a,
  36. const uint8* src_b, int count) {
  37. volatile uint32 sse;
  38. asm volatile (
  39. "vmov.u8 q7, #0 \n"
  40. "vmov.u8 q9, #0 \n"
  41. "vmov.u8 q8, #0 \n"
  42. "vmov.u8 q10, #0 \n"
  43. "1: \n"
  44. "vld1.u8 {q0}, [%0]! \n"
  45. "vld1.u8 {q1}, [%1]! \n"
  46. "vsubl.u8 q2, d0, d2 \n"
  47. "vsubl.u8 q3, d1, d3 \n"
  48. "vmlal.s16 q7, d4, d4 \n"
  49. "vmlal.s16 q8, d6, d6 \n"
  50. "vmlal.s16 q8, d5, d5 \n"
  51. "vmlal.s16 q10, d7, d7 \n"
  52. "subs %2, %2, #16 \n"
  53. "bhi 1b \n"
  54. "vadd.u32 q7, q7, q8 \n"
  55. "vadd.u32 q9, q9, q10 \n"
  56. "vadd.u32 q10, q7, q9 \n"
  57. "vpaddl.u32 q1, q10 \n"
  58. "vadd.u64 d0, d2, d3 \n"
  59. "vmov.32 %3, d0[0] \n"
  60. : "+r"(src_a),
  61. "+r"(src_b),
  62. "+r"(count),
  63. "=r"(sse)
  64. :
  65. : "memory", "cc", "q0", "q1", "q2", "q3", "q7", "q8", "q9", "q10");
  66. return sse;
  67. }
  68. #elif !defined(LIBYUV_DISABLE_NEON) && defined(__aarch64__)
  69. #define HAS_SUMSQUAREERROR_NEON
  70. static uint32 SumSquareError_NEON(const uint8* src_a,
  71. const uint8* src_b, int count) {
  72. volatile uint32 sse;
  73. asm volatile (
  74. "eor v16.16b, v16.16b, v16.16b \n"
  75. "eor v18.16b, v18.16b, v18.16b \n"
  76. "eor v17.16b, v17.16b, v17.16b \n"
  77. "eor v19.16b, v19.16b, v19.16b \n"
  78. "1: \n"
  79. "ld1 {v0.16b}, [%0], #16 \n"
  80. "ld1 {v1.16b}, [%1], #16 \n"
  81. "subs %w2, %w2, #16 \n"
  82. "usubl v2.8h, v0.8b, v1.8b \n"
  83. "usubl2 v3.8h, v0.16b, v1.16b \n"
  84. "smlal v16.4s, v2.4h, v2.4h \n"
  85. "smlal v17.4s, v3.4h, v3.4h \n"
  86. "smlal2 v18.4s, v2.8h, v2.8h \n"
  87. "smlal2 v19.4s, v3.8h, v3.8h \n"
  88. "b.gt 1b \n"
  89. "add v16.4s, v16.4s, v17.4s \n"
  90. "add v18.4s, v18.4s, v19.4s \n"
  91. "add v19.4s, v16.4s, v18.4s \n"
  92. "addv s0, v19.4s \n"
  93. "fmov %w3, s0 \n"
  94. : "+r"(src_a),
  95. "+r"(src_b),
  96. "+r"(count),
  97. "=r"(sse)
  98. :
  99. : "cc", "v0", "v1", "v2", "v3", "v16", "v17", "v18", "v19");
  100. return sse;
  101. }
  102. #elif !defined(LIBYUV_DISABLE_X86) && defined(_M_IX86) && defined(_MSC_VER)
  103. #define HAS_SUMSQUAREERROR_SSE2
  104. __declspec(naked)
  105. static uint32 SumSquareError_SSE2(const uint8* /*src_a*/,
  106. const uint8* /*src_b*/, int /*count*/) {
  107. __asm {
  108. mov eax, [esp + 4] // src_a
  109. mov edx, [esp + 8] // src_b
  110. mov ecx, [esp + 12] // count
  111. pxor xmm0, xmm0
  112. pxor xmm5, xmm5
  113. sub edx, eax
  114. wloop:
  115. movdqu xmm1, [eax]
  116. movdqu xmm2, [eax + edx]
  117. lea eax, [eax + 16]
  118. movdqu xmm3, xmm1
  119. psubusb xmm1, xmm2
  120. psubusb xmm2, xmm3
  121. por xmm1, xmm2
  122. movdqu xmm2, xmm1
  123. punpcklbw xmm1, xmm5
  124. punpckhbw xmm2, xmm5
  125. pmaddwd xmm1, xmm1
  126. pmaddwd xmm2, xmm2
  127. paddd xmm0, xmm1
  128. paddd xmm0, xmm2
  129. sub ecx, 16
  130. ja wloop
  131. pshufd xmm1, xmm0, 0EEh
  132. paddd xmm0, xmm1
  133. pshufd xmm1, xmm0, 01h
  134. paddd xmm0, xmm1
  135. movd eax, xmm0
  136. ret
  137. }
  138. }
  139. #elif !defined(LIBYUV_DISABLE_X86) && (defined(__x86_64__) || defined(__i386__))
  140. #define HAS_SUMSQUAREERROR_SSE2
  141. static uint32 SumSquareError_SSE2(const uint8* src_a,
  142. const uint8* src_b, int count) {
  143. uint32 sse;
  144. asm volatile ( // NOLINT
  145. "pxor %%xmm0,%%xmm0 \n"
  146. "pxor %%xmm5,%%xmm5 \n"
  147. "sub %0,%1 \n"
  148. "1: \n"
  149. "movdqu (%0),%%xmm1 \n"
  150. "movdqu (%0,%1,1),%%xmm2 \n"
  151. "lea 0x10(%0),%0 \n"
  152. "movdqu %%xmm1,%%xmm3 \n"
  153. "psubusb %%xmm2,%%xmm1 \n"
  154. "psubusb %%xmm3,%%xmm2 \n"
  155. "por %%xmm2,%%xmm1 \n"
  156. "movdqu %%xmm1,%%xmm2 \n"
  157. "punpcklbw %%xmm5,%%xmm1 \n"
  158. "punpckhbw %%xmm5,%%xmm2 \n"
  159. "pmaddwd %%xmm1,%%xmm1 \n"
  160. "pmaddwd %%xmm2,%%xmm2 \n"
  161. "paddd %%xmm1,%%xmm0 \n"
  162. "paddd %%xmm2,%%xmm0 \n"
  163. "sub $0x10,%2 \n"
  164. "ja 1b \n"
  165. "pshufd $0xee,%%xmm0,%%xmm1 \n"
  166. "paddd %%xmm1,%%xmm0 \n"
  167. "pshufd $0x1,%%xmm0,%%xmm1 \n"
  168. "paddd %%xmm1,%%xmm0 \n"
  169. "movd %%xmm0,%3 \n"
  170. : "+r"(src_a), // %0
  171. "+r"(src_b), // %1
  172. "+r"(count), // %2
  173. "=g"(sse) // %3
  174. :
  175. : "memory", "cc"
  176. #if defined(__SSE2__)
  177. , "xmm0", "xmm1", "xmm2", "xmm3", "xmm5"
  178. #endif
  179. ); // NOLINT
  180. return sse;
  181. }
  182. #endif // LIBYUV_DISABLE_X86 etc
  183. #if defined(HAS_SUMSQUAREERROR_SSE2)
  184. #if (defined(__pic__) || defined(__APPLE__)) && defined(__i386__)
  185. static __inline void __cpuid(int cpu_info[4], int info_type) {
  186. asm volatile ( // NOLINT
  187. "mov %%ebx, %%edi \n"
  188. "cpuid \n"
  189. "xchg %%edi, %%ebx \n"
  190. : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
  191. : "a"(info_type));
  192. }
  193. // For gcc/clang but not clangcl.
  194. #elif (defined(__i386__) || defined(__x86_64__)) && !defined(_MSC_VER)
  195. static __inline void __cpuid(int cpu_info[4], int info_type) {
  196. asm volatile ( // NOLINT
  197. "cpuid \n"
  198. : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
  199. : "a"(info_type));
  200. }
  201. #endif
  202. static int CpuHasSSE2() {
  203. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86)
  204. int cpu_info[4];
  205. __cpuid(cpu_info, 1);
  206. if (cpu_info[3] & 0x04000000) {
  207. return 1;
  208. }
  209. #endif
  210. return 0;
  211. }
  212. #endif // HAS_SUMSQUAREERROR_SSE2
  213. static uint32 SumSquareError_C(const uint8* src_a,
  214. const uint8* src_b, int count) {
  215. uint32 sse = 0u;
  216. for (int x = 0; x < count; ++x) {
  217. int diff = src_a[x] - src_b[x];
  218. sse += static_cast<uint32>(diff * diff);
  219. }
  220. return sse;
  221. }
  222. double ComputeSumSquareError(const uint8* src_a,
  223. const uint8* src_b, int count) {
  224. uint32 (*SumSquareError)(const uint8* src_a,
  225. const uint8* src_b, int count) = SumSquareError_C;
  226. #if defined(HAS_SUMSQUAREERROR_NEON)
  227. SumSquareError = SumSquareError_NEON;
  228. #endif
  229. #if defined(HAS_SUMSQUAREERROR_SSE2)
  230. if (CpuHasSSE2()) {
  231. SumSquareError = SumSquareError_SSE2;
  232. }
  233. #endif
  234. const int kBlockSize = 1 << 15;
  235. uint64 sse = 0;
  236. #ifdef _OPENMP
  237. #pragma omp parallel for reduction(+: sse)
  238. #endif
  239. for (int i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) {
  240. sse += SumSquareError(src_a + i, src_b + i, kBlockSize);
  241. }
  242. src_a += count & ~(kBlockSize - 1);
  243. src_b += count & ~(kBlockSize - 1);
  244. int remainder = count & (kBlockSize - 1) & ~15;
  245. if (remainder) {
  246. sse += SumSquareError(src_a, src_b, remainder);
  247. src_a += remainder;
  248. src_b += remainder;
  249. }
  250. remainder = count & 15;
  251. if (remainder) {
  252. sse += SumSquareError_C(src_a, src_b, remainder);
  253. }
  254. return static_cast<double>(sse);
  255. }
  256. #endif
  257. // PSNR formula: psnr = 10 * log10 (Peak Signal^2 * size / sse)
  258. // Returns 128.0 (kMaxPSNR) if sse is 0 (perfect match).
  259. double ComputePSNR(double sse, double size) {
  260. const double kMINSSE = 255.0 * 255.0 * size / pow(10.0, kMaxPSNR / 10.0);
  261. if (sse <= kMINSSE)
  262. sse = kMINSSE; // Produces max PSNR of 128
  263. return 10.0 * log10(255.0 * 255.0 * size / sse);
  264. }
  265. #ifdef __cplusplus
  266. } // extern "C"
  267. #endif