compare.cc 12 KB

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