compare.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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) =
  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)(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 ARGBDetectRow_C(const uint8* 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 ARGBDetect(const uint8* argb, int stride_argb, int width, int height) {
  90. uint32 fourcc = 0;
  91. int h;
  92. // Coalesce rows.
  93. if (stride_argb == width * 4) {
  94. width *= height;
  95. height = 1;
  96. stride_argb = 0;
  97. }
  98. for (h = 0; h < height && fourcc == 0; ++h) {
  99. fourcc = ARGBDetectRow_C(argb, width);
  100. argb += stride_argb;
  101. }
  102. return fourcc;
  103. }
  104. // TODO(fbarchard): Refactor into row function.
  105. LIBYUV_API
  106. uint64 ComputeSumSquareError(const uint8* src_a, const uint8* src_b,
  107. int count) {
  108. // SumSquareError returns values 0 to 65535 for each squared difference.
  109. // Up to 65536 of those can be summed and remain within a uint32.
  110. // After each block of 65536 pixels, accumulate into a uint64.
  111. const int kBlockSize = 65536;
  112. int remainder = count & (kBlockSize - 1) & ~31;
  113. uint64 sse = 0;
  114. int i;
  115. uint32 (*SumSquareError)(const uint8* src_a, const uint8* src_b, int count) =
  116. SumSquareError_C;
  117. #if defined(HAS_SUMSQUAREERROR_NEON)
  118. if (TestCpuFlag(kCpuHasNEON)) {
  119. SumSquareError = SumSquareError_NEON;
  120. }
  121. #endif
  122. #if defined(HAS_SUMSQUAREERROR_SSE2)
  123. if (TestCpuFlag(kCpuHasSSE2)) {
  124. // Note only used for multiples of 16 so count is not checked.
  125. SumSquareError = SumSquareError_SSE2;
  126. }
  127. #endif
  128. #if defined(HAS_SUMSQUAREERROR_AVX2)
  129. if (TestCpuFlag(kCpuHasAVX2)) {
  130. // Note only used for multiples of 32 so count is not checked.
  131. SumSquareError = SumSquareError_AVX2;
  132. }
  133. #endif
  134. #ifdef _OPENMP
  135. #pragma omp parallel for reduction(+: sse)
  136. #endif
  137. for (i = 0; i < (count - (kBlockSize - 1)); i += kBlockSize) {
  138. sse += SumSquareError(src_a + i, src_b + i, kBlockSize);
  139. }
  140. src_a += count & ~(kBlockSize - 1);
  141. src_b += count & ~(kBlockSize - 1);
  142. if (remainder) {
  143. sse += SumSquareError(src_a, src_b, remainder);
  144. src_a += remainder;
  145. src_b += remainder;
  146. }
  147. remainder = count & 31;
  148. if (remainder) {
  149. sse += SumSquareError_C(src_a, src_b, remainder);
  150. }
  151. return sse;
  152. }
  153. LIBYUV_API
  154. uint64 ComputeSumSquareErrorPlane(const uint8* src_a, int stride_a,
  155. const uint8* src_b, int stride_b,
  156. int width, int height) {
  157. uint64 sse = 0;
  158. int h;
  159. // Coalesce rows.
  160. if (stride_a == width &&
  161. stride_b == width) {
  162. width *= height;
  163. height = 1;
  164. stride_a = stride_b = 0;
  165. }
  166. for (h = 0; h < height; ++h) {
  167. sse += ComputeSumSquareError(src_a, src_b, width);
  168. src_a += stride_a;
  169. src_b += stride_b;
  170. }
  171. return sse;
  172. }
  173. LIBYUV_API
  174. double SumSquareErrorToPsnr(uint64 sse, uint64 count) {
  175. double psnr;
  176. if (sse > 0) {
  177. double mse = (double)(count) / (double)(sse);
  178. psnr = 10.0 * log10(255.0 * 255.0 * mse);
  179. } else {
  180. psnr = kMaxPsnr; // Limit to prevent divide by 0
  181. }
  182. if (psnr > kMaxPsnr)
  183. psnr = kMaxPsnr;
  184. return psnr;
  185. }
  186. LIBYUV_API
  187. double CalcFramePsnr(const uint8* src_a, int stride_a,
  188. const uint8* src_b, int stride_b,
  189. int width, int height) {
  190. const uint64 samples = width * height;
  191. const uint64 sse = ComputeSumSquareErrorPlane(src_a, stride_a,
  192. src_b, stride_b,
  193. width, height);
  194. return SumSquareErrorToPsnr(sse, samples);
  195. }
  196. LIBYUV_API
  197. double I420Psnr(const uint8* src_y_a, int stride_y_a,
  198. const uint8* src_u_a, int stride_u_a,
  199. const uint8* src_v_a, int stride_v_a,
  200. const uint8* src_y_b, int stride_y_b,
  201. const uint8* src_u_b, int stride_u_b,
  202. const uint8* src_v_b, int stride_v_b,
  203. int width, int height) {
  204. const uint64 sse_y = ComputeSumSquareErrorPlane(src_y_a, stride_y_a,
  205. src_y_b, stride_y_b,
  206. width, height);
  207. const int width_uv = (width + 1) >> 1;
  208. const int height_uv = (height + 1) >> 1;
  209. const uint64 sse_u = ComputeSumSquareErrorPlane(src_u_a, stride_u_a,
  210. src_u_b, stride_u_b,
  211. width_uv, height_uv);
  212. const uint64 sse_v = ComputeSumSquareErrorPlane(src_v_a, stride_v_a,
  213. src_v_b, stride_v_b,
  214. width_uv, height_uv);
  215. const uint64 samples = width * height + 2 * (width_uv * height_uv);
  216. const uint64 sse = sse_y + sse_u + sse_v;
  217. return SumSquareErrorToPsnr(sse, samples);
  218. }
  219. static const int64 cc1 = 26634; // (64^2*(.01*255)^2
  220. static const int64 cc2 = 239708; // (64^2*(.03*255)^2
  221. static double Ssim8x8_C(const uint8* src_a, int stride_a,
  222. const uint8* src_b, int stride_b) {
  223. int64 sum_a = 0;
  224. int64 sum_b = 0;
  225. int64 sum_sq_a = 0;
  226. int64 sum_sq_b = 0;
  227. int64 sum_axb = 0;
  228. int i;
  229. for (i = 0; i < 8; ++i) {
  230. int j;
  231. for (j = 0; j < 8; ++j) {
  232. sum_a += src_a[j];
  233. sum_b += src_b[j];
  234. sum_sq_a += src_a[j] * src_a[j];
  235. sum_sq_b += src_b[j] * src_b[j];
  236. sum_axb += src_a[j] * src_b[j];
  237. }
  238. src_a += stride_a;
  239. src_b += stride_b;
  240. }
  241. {
  242. const int64 count = 64;
  243. // scale the constants by number of pixels
  244. const int64 c1 = (cc1 * count * count) >> 12;
  245. const int64 c2 = (cc2 * count * count) >> 12;
  246. const int64 sum_a_x_sum_b = sum_a * sum_b;
  247. const int64 ssim_n = (2 * sum_a_x_sum_b + c1) *
  248. (2 * count * sum_axb - 2 * sum_a_x_sum_b + c2);
  249. const int64 sum_a_sq = sum_a*sum_a;
  250. const int64 sum_b_sq = sum_b*sum_b;
  251. const int64 ssim_d = (sum_a_sq + sum_b_sq + c1) *
  252. (count * sum_sq_a - sum_a_sq +
  253. count * sum_sq_b - sum_b_sq + c2);
  254. if (ssim_d == 0.0) {
  255. return DBL_MAX;
  256. }
  257. return ssim_n * 1.0 / ssim_d;
  258. }
  259. }
  260. // We are using a 8x8 moving window with starting location of each 8x8 window
  261. // on the 4x4 pixel grid. Such arrangement allows the windows to overlap
  262. // block boundaries to penalize blocking artifacts.
  263. LIBYUV_API
  264. double CalcFrameSsim(const uint8* src_a, int stride_a,
  265. const uint8* src_b, int stride_b,
  266. int width, int height) {
  267. int samples = 0;
  268. double ssim_total = 0;
  269. double (*Ssim8x8)(const uint8* src_a, int stride_a,
  270. const uint8* src_b, int stride_b) = Ssim8x8_C;
  271. // sample point start with each 4x4 location
  272. int i;
  273. for (i = 0; i < height - 8; i += 4) {
  274. int j;
  275. for (j = 0; j < width - 8; j += 4) {
  276. ssim_total += Ssim8x8(src_a + j, stride_a, src_b + j, stride_b);
  277. samples++;
  278. }
  279. src_a += stride_a * 4;
  280. src_b += stride_b * 4;
  281. }
  282. ssim_total /= samples;
  283. return ssim_total;
  284. }
  285. LIBYUV_API
  286. double I420Ssim(const uint8* src_y_a, int stride_y_a,
  287. const uint8* src_u_a, int stride_u_a,
  288. const uint8* src_v_a, int stride_v_a,
  289. const uint8* src_y_b, int stride_y_b,
  290. const uint8* src_u_b, int stride_u_b,
  291. const uint8* src_v_b, int stride_v_b,
  292. int width, int height) {
  293. const double ssim_y = CalcFrameSsim(src_y_a, stride_y_a,
  294. 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 double ssim_u = CalcFrameSsim(src_u_a, stride_u_a,
  298. src_u_b, stride_u_b,
  299. width_uv, height_uv);
  300. const double ssim_v = CalcFrameSsim(src_v_a, stride_v_a,
  301. src_v_b, stride_v_b,
  302. width_uv, height_uv);
  303. return ssim_y * 0.8 + 0.1 * (ssim_u + ssim_v);
  304. }
  305. #ifdef __cplusplus
  306. } // extern "C"
  307. } // namespace libyuv
  308. #endif