compare.cc 13 KB

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