2
0

psnr.cc 10.0 KB

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