compare.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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) { // 4th 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 4th 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. #ifdef _OPENMP
  147. #pragma omp parallel for reduction(+ : diff)
  148. #endif
  149. for (i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) {
  150. diff += HammingDistance(src_a + i, src_b + i, kBlockSize);
  151. }
  152. src_a += count & ~(kBlockSize - 1);
  153. src_b += count & ~(kBlockSize - 1);
  154. if (remainder) {
  155. diff += HammingDistance(src_a, src_b, remainder);
  156. src_a += remainder;
  157. src_b += remainder;
  158. }
  159. remainder = count & (kSimdSize - 1);
  160. if (remainder) {
  161. diff += HammingDistance_C(src_a, src_b, remainder);
  162. }
  163. return diff;
  164. }
  165. // TODO(fbarchard): Refactor into row function.
  166. LIBYUV_API
  167. uint64_t ComputeSumSquareError(const uint8_t* src_a,
  168. const uint8_t* src_b,
  169. int count) {
  170. // SumSquareError returns values 0 to 65535 for each squared difference.
  171. // Up to 65536 of those can be summed and remain within a uint32_t.
  172. // After each block of 65536 pixels, accumulate into a uint64_t.
  173. const int kBlockSize = 65536;
  174. int remainder = count & (kBlockSize - 1) & ~31;
  175. uint64_t sse = 0;
  176. int i;
  177. uint32_t (*SumSquareError)(const uint8_t* src_a, const uint8_t* src_b,
  178. int count) = SumSquareError_C;
  179. #if defined(HAS_SUMSQUAREERROR_NEON)
  180. if (TestCpuFlag(kCpuHasNEON)) {
  181. SumSquareError = SumSquareError_NEON;
  182. }
  183. #endif
  184. #if defined(HAS_SUMSQUAREERROR_SSE2)
  185. if (TestCpuFlag(kCpuHasSSE2)) {
  186. // Note only used for multiples of 16 so count is not checked.
  187. SumSquareError = SumSquareError_SSE2;
  188. }
  189. #endif
  190. #if defined(HAS_SUMSQUAREERROR_AVX2)
  191. if (TestCpuFlag(kCpuHasAVX2)) {
  192. // Note only used for multiples of 32 so count is not checked.
  193. SumSquareError = SumSquareError_AVX2;
  194. }
  195. #endif
  196. #if defined(HAS_SUMSQUAREERROR_MSA)
  197. if (TestCpuFlag(kCpuHasMSA)) {
  198. SumSquareError = SumSquareError_MSA;
  199. }
  200. #endif
  201. #ifdef _OPENMP
  202. #pragma omp parallel for reduction(+ : sse)
  203. #endif
  204. for (i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) {
  205. sse += SumSquareError(src_a + i, src_b + i, kBlockSize);
  206. }
  207. src_a += count & ~(kBlockSize - 1);
  208. src_b += count & ~(kBlockSize - 1);
  209. if (remainder) {
  210. sse += SumSquareError(src_a, src_b, remainder);
  211. src_a += remainder;
  212. src_b += remainder;
  213. }
  214. remainder = count & 31;
  215. if (remainder) {
  216. sse += SumSquareError_C(src_a, src_b, remainder);
  217. }
  218. return sse;
  219. }
  220. LIBYUV_API
  221. uint64_t ComputeSumSquareErrorPlane(const uint8_t* src_a,
  222. int stride_a,
  223. const uint8_t* src_b,
  224. int stride_b,
  225. int width,
  226. int height) {
  227. uint64_t sse = 0;
  228. int h;
  229. // Coalesce rows.
  230. if (stride_a == width && stride_b == width) {
  231. width *= height;
  232. height = 1;
  233. stride_a = stride_b = 0;
  234. }
  235. for (h = 0; h < height; ++h) {
  236. sse += ComputeSumSquareError(src_a, src_b, width);
  237. src_a += stride_a;
  238. src_b += stride_b;
  239. }
  240. return sse;
  241. }
  242. LIBYUV_API
  243. double SumSquareErrorToPsnr(uint64_t sse, uint64_t count) {
  244. double psnr;
  245. if (sse > 0) {
  246. double mse = (double)count / (double)sse;
  247. psnr = 10.0 * log10(255.0 * 255.0 * mse);
  248. } else {
  249. psnr = kMaxPsnr; // Limit to prevent divide by 0
  250. }
  251. if (psnr > kMaxPsnr) {
  252. psnr = kMaxPsnr;
  253. }
  254. return psnr;
  255. }
  256. LIBYUV_API
  257. double CalcFramePsnr(const uint8_t* src_a,
  258. int stride_a,
  259. const uint8_t* src_b,
  260. int stride_b,
  261. int width,
  262. int height) {
  263. const uint64_t samples = (uint64_t)width * (uint64_t)height;
  264. const uint64_t sse = ComputeSumSquareErrorPlane(src_a, stride_a, src_b,
  265. stride_b, width, height);
  266. return SumSquareErrorToPsnr(sse, samples);
  267. }
  268. LIBYUV_API
  269. double I420Psnr(const uint8_t* src_y_a,
  270. int stride_y_a,
  271. const uint8_t* src_u_a,
  272. int stride_u_a,
  273. const uint8_t* src_v_a,
  274. int stride_v_a,
  275. const uint8_t* src_y_b,
  276. int stride_y_b,
  277. const uint8_t* src_u_b,
  278. int stride_u_b,
  279. const uint8_t* src_v_b,
  280. int stride_v_b,
  281. int width,
  282. int height) {
  283. const uint64_t sse_y = ComputeSumSquareErrorPlane(
  284. src_y_a, stride_y_a, src_y_b, stride_y_b, width, height);
  285. const int width_uv = (width + 1) >> 1;
  286. const int height_uv = (height + 1) >> 1;
  287. const uint64_t sse_u = ComputeSumSquareErrorPlane(
  288. src_u_a, stride_u_a, src_u_b, stride_u_b, width_uv, height_uv);
  289. const uint64_t sse_v = ComputeSumSquareErrorPlane(
  290. src_v_a, stride_v_a, src_v_b, stride_v_b, width_uv, height_uv);
  291. const uint64_t samples = (uint64_t)width * (uint64_t)height +
  292. 2 * ((uint64_t)width_uv * (uint64_t)height_uv);
  293. const uint64_t sse = sse_y + sse_u + sse_v;
  294. return SumSquareErrorToPsnr(sse, samples);
  295. }
  296. static const int64_t cc1 = 26634; // (64^2*(.01*255)^2
  297. static const int64_t cc2 = 239708; // (64^2*(.03*255)^2
  298. static double Ssim8x8_C(const uint8_t* src_a,
  299. int stride_a,
  300. const uint8_t* src_b,
  301. int stride_b) {
  302. int64_t sum_a = 0;
  303. int64_t sum_b = 0;
  304. int64_t sum_sq_a = 0;
  305. int64_t sum_sq_b = 0;
  306. int64_t sum_axb = 0;
  307. int i;
  308. for (i = 0; i < 8; ++i) {
  309. int j;
  310. for (j = 0; j < 8; ++j) {
  311. sum_a += src_a[j];
  312. sum_b += src_b[j];
  313. sum_sq_a += src_a[j] * src_a[j];
  314. sum_sq_b += src_b[j] * src_b[j];
  315. sum_axb += src_a[j] * src_b[j];
  316. }
  317. src_a += stride_a;
  318. src_b += stride_b;
  319. }
  320. {
  321. const int64_t count = 64;
  322. // scale the constants by number of pixels
  323. const int64_t c1 = (cc1 * count * count) >> 12;
  324. const int64_t c2 = (cc2 * count * count) >> 12;
  325. const int64_t sum_a_x_sum_b = sum_a * sum_b;
  326. const int64_t ssim_n = (2 * sum_a_x_sum_b + c1) *
  327. (2 * count * sum_axb - 2 * sum_a_x_sum_b + c2);
  328. const int64_t sum_a_sq = sum_a * sum_a;
  329. const int64_t sum_b_sq = sum_b * sum_b;
  330. const int64_t ssim_d =
  331. (sum_a_sq + sum_b_sq + c1) *
  332. (count * sum_sq_a - sum_a_sq + count * sum_sq_b - sum_b_sq + c2);
  333. if (ssim_d == 0.0) {
  334. return DBL_MAX;
  335. }
  336. return ssim_n * 1.0 / ssim_d;
  337. }
  338. }
  339. // We are using a 8x8 moving window with starting location of each 8x8 window
  340. // on the 4x4 pixel grid. Such arrangement allows the windows to overlap
  341. // block boundaries to penalize blocking artifacts.
  342. LIBYUV_API
  343. double CalcFrameSsim(const uint8_t* src_a,
  344. int stride_a,
  345. const uint8_t* src_b,
  346. int stride_b,
  347. int width,
  348. int height) {
  349. int samples = 0;
  350. double ssim_total = 0;
  351. double (*Ssim8x8)(const uint8_t* src_a, int stride_a, const uint8_t* src_b,
  352. int stride_b) = Ssim8x8_C;
  353. // sample point start with each 4x4 location
  354. int i;
  355. for (i = 0; i < height - 8; i += 4) {
  356. int j;
  357. for (j = 0; j < width - 8; j += 4) {
  358. ssim_total += Ssim8x8(src_a + j, stride_a, src_b + j, stride_b);
  359. samples++;
  360. }
  361. src_a += stride_a * 4;
  362. src_b += stride_b * 4;
  363. }
  364. ssim_total /= samples;
  365. return ssim_total;
  366. }
  367. LIBYUV_API
  368. double I420Ssim(const uint8_t* src_y_a,
  369. int stride_y_a,
  370. const uint8_t* src_u_a,
  371. int stride_u_a,
  372. const uint8_t* src_v_a,
  373. int stride_v_a,
  374. const uint8_t* src_y_b,
  375. int stride_y_b,
  376. const uint8_t* src_u_b,
  377. int stride_u_b,
  378. const uint8_t* src_v_b,
  379. int stride_v_b,
  380. int width,
  381. int height) {
  382. const double ssim_y =
  383. CalcFrameSsim(src_y_a, stride_y_a, src_y_b, stride_y_b, width, height);
  384. const int width_uv = (width + 1) >> 1;
  385. const int height_uv = (height + 1) >> 1;
  386. const double ssim_u = CalcFrameSsim(src_u_a, stride_u_a, src_u_b, stride_u_b,
  387. width_uv, height_uv);
  388. const double ssim_v = CalcFrameSsim(src_v_a, stride_v_a, src_v_b, stride_v_b,
  389. width_uv, height_uv);
  390. return ssim_y * 0.8 + 0.1 * (ssim_u + ssim_v);
  391. }
  392. #ifdef __cplusplus
  393. } // extern "C"
  394. } // namespace libyuv
  395. #endif