ssim.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (c) 2016, Alliance for Open Media. All rights reserved
  3. *
  4. * This source code is subject to the terms of the BSD 2 Clause License and
  5. * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
  6. * was not distributed with this source code in the LICENSE file, you can
  7. * obtain it at www.aomedia.org/license/software. If the Alliance for Open
  8. * Media Patent License 1.0 was not distributed with this source code in the
  9. * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  10. */
  11. #include <assert.h>
  12. #include <math.h>
  13. #include "config/aom_dsp_rtcd.h"
  14. #include "aom_dsp/ssim.h"
  15. #include "aom_ports/mem.h"
  16. #if CONFIG_INTERNAL_STATS
  17. void aom_ssim_parms_16x16_c(const uint8_t *s, int sp, const uint8_t *r, int rp,
  18. uint32_t *sum_s, uint32_t *sum_r,
  19. uint32_t *sum_sq_s, uint32_t *sum_sq_r,
  20. uint32_t *sum_sxr) {
  21. int i, j;
  22. for (i = 0; i < 16; i++, s += sp, r += rp) {
  23. for (j = 0; j < 16; j++) {
  24. *sum_s += s[j];
  25. *sum_r += r[j];
  26. *sum_sq_s += s[j] * s[j];
  27. *sum_sq_r += r[j] * r[j];
  28. *sum_sxr += s[j] * r[j];
  29. }
  30. }
  31. }
  32. #endif // CONFIG_INTERNAL_STATS
  33. void aom_ssim_parms_8x8_c(const uint8_t *s, int sp, const uint8_t *r, int rp,
  34. uint32_t *sum_s, uint32_t *sum_r, uint32_t *sum_sq_s,
  35. uint32_t *sum_sq_r, uint32_t *sum_sxr) {
  36. int i, j;
  37. for (i = 0; i < 8; i++, s += sp, r += rp) {
  38. for (j = 0; j < 8; j++) {
  39. *sum_s += s[j];
  40. *sum_r += r[j];
  41. *sum_sq_s += s[j] * s[j];
  42. *sum_sq_r += r[j] * r[j];
  43. *sum_sxr += s[j] * r[j];
  44. }
  45. }
  46. }
  47. static const int64_t cc1 = 26634; // (64^2*(.01*255)^2
  48. static const int64_t cc2 = 239708; // (64^2*(.03*255)^2
  49. static const int64_t cc1_10 = 428658; // (64^2*(.01*1023)^2
  50. static const int64_t cc2_10 = 3857925; // (64^2*(.03*1023)^2
  51. static const int64_t cc1_12 = 6868593; // (64^2*(.01*4095)^2
  52. static const int64_t cc2_12 = 61817334; // (64^2*(.03*4095)^2
  53. static double similarity(uint32_t sum_s, uint32_t sum_r, uint32_t sum_sq_s,
  54. uint32_t sum_sq_r, uint32_t sum_sxr, int count,
  55. uint32_t bd) {
  56. double ssim_n, ssim_d;
  57. int64_t c1 = 0, c2 = 0;
  58. if (bd == 8) {
  59. // scale the constants by number of pixels
  60. c1 = (cc1 * count * count) >> 12;
  61. c2 = (cc2 * count * count) >> 12;
  62. } else if (bd == 10) {
  63. c1 = (cc1_10 * count * count) >> 12;
  64. c2 = (cc2_10 * count * count) >> 12;
  65. } else if (bd == 12) {
  66. c1 = (cc1_12 * count * count) >> 12;
  67. c2 = (cc2_12 * count * count) >> 12;
  68. } else {
  69. assert(0);
  70. // Return similarity as zero for unsupported bit-depth values.
  71. return 0;
  72. }
  73. ssim_n = (2.0 * sum_s * sum_r + c1) *
  74. (2.0 * count * sum_sxr - 2.0 * sum_s * sum_r + c2);
  75. ssim_d = ((double)sum_s * sum_s + (double)sum_r * sum_r + c1) *
  76. ((double)count * sum_sq_s - (double)sum_s * sum_s +
  77. (double)count * sum_sq_r - (double)sum_r * sum_r + c2);
  78. return ssim_n / ssim_d;
  79. }
  80. static double ssim_8x8(const uint8_t *s, int sp, const uint8_t *r, int rp) {
  81. uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0;
  82. aom_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r,
  83. &sum_sxr);
  84. return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64, 8);
  85. }
  86. // We are using a 8x8 moving window with starting location of each 8x8 window
  87. // on the 4x4 pixel grid. Such arrangement allows the windows to overlap
  88. // block boundaries to penalize blocking artifacts.
  89. double aom_ssim2(const uint8_t *img1, const uint8_t *img2, int stride_img1,
  90. int stride_img2, int width, int height) {
  91. int i, j;
  92. int samples = 0;
  93. double ssim_total = 0;
  94. // sample point start with each 4x4 location
  95. for (i = 0; i <= height - 8;
  96. i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) {
  97. for (j = 0; j <= width - 8; j += 4) {
  98. double v = ssim_8x8(img1 + j, stride_img1, img2 + j, stride_img2);
  99. ssim_total += v;
  100. samples++;
  101. }
  102. }
  103. ssim_total /= samples;
  104. return ssim_total;
  105. }
  106. #if CONFIG_INTERNAL_STATS
  107. void aom_lowbd_calc_ssim(const YV12_BUFFER_CONFIG *source,
  108. const YV12_BUFFER_CONFIG *dest, double *weight,
  109. double *fast_ssim) {
  110. double abc[3];
  111. for (int i = 0; i < 3; ++i) {
  112. const int is_uv = i > 0;
  113. abc[i] = aom_ssim2(source->buffers[i], dest->buffers[i],
  114. source->strides[is_uv], dest->strides[is_uv],
  115. source->crop_widths[is_uv], source->crop_heights[is_uv]);
  116. }
  117. *weight = 1;
  118. *fast_ssim = abc[0] * .8 + .1 * (abc[1] + abc[2]);
  119. }
  120. // traditional ssim as per: http://en.wikipedia.org/wiki/Structural_similarity
  121. //
  122. // Re working out the math ->
  123. //
  124. // ssim(x,y) = (2*mean(x)*mean(y) + c1)*(2*cov(x,y)+c2) /
  125. // ((mean(x)^2+mean(y)^2+c1)*(var(x)+var(y)+c2))
  126. //
  127. // mean(x) = sum(x) / n
  128. //
  129. // cov(x,y) = (n*sum(xi*yi)-sum(x)*sum(y))/(n*n)
  130. //
  131. // var(x) = (n*sum(xi*xi)-sum(xi)*sum(xi))/(n*n)
  132. //
  133. // ssim(x,y) =
  134. // (2*sum(x)*sum(y)/(n*n) + c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))/(n*n)+c2) /
  135. // (((sum(x)*sum(x)+sum(y)*sum(y))/(n*n) +c1) *
  136. // ((n*sum(xi*xi) - sum(xi)*sum(xi))/(n*n)+
  137. // (n*sum(yi*yi) - sum(yi)*sum(yi))/(n*n)+c2)))
  138. //
  139. // factoring out n*n
  140. //
  141. // ssim(x,y) =
  142. // (2*sum(x)*sum(y) + n*n*c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))+n*n*c2) /
  143. // (((sum(x)*sum(x)+sum(y)*sum(y)) + n*n*c1) *
  144. // (n*sum(xi*xi)-sum(xi)*sum(xi)+n*sum(yi*yi)-sum(yi)*sum(yi)+n*n*c2))
  145. //
  146. // Replace c1 with n*n * c1 for the final step that leads to this code:
  147. // The final step scales by 12 bits so we don't lose precision in the constants.
  148. static double ssimv_similarity(const Ssimv *sv, int64_t n) {
  149. // Scale the constants by number of pixels.
  150. const int64_t c1 = (cc1 * n * n) >> 12;
  151. const int64_t c2 = (cc2 * n * n) >> 12;
  152. const double l = 1.0 * (2 * sv->sum_s * sv->sum_r + c1) /
  153. (sv->sum_s * sv->sum_s + sv->sum_r * sv->sum_r + c1);
  154. // Since these variables are unsigned sums, convert to double so
  155. // math is done in double arithmetic.
  156. const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2) /
  157. (n * sv->sum_sq_s - sv->sum_s * sv->sum_s +
  158. n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2);
  159. return l * v;
  160. }
  161. // The first term of the ssim metric is a luminance factor.
  162. //
  163. // (2*mean(x)*mean(y) + c1)/ (mean(x)^2+mean(y)^2+c1)
  164. //
  165. // This luminance factor is super sensitive to the dark side of luminance
  166. // values and completely insensitive on the white side. check out 2 sets
  167. // (1,3) and (250,252) the term gives ( 2*1*3/(1+9) = .60
  168. // 2*250*252/ (250^2+252^2) => .99999997
  169. //
  170. // As a result in this tweaked version of the calculation in which the
  171. // luminance is taken as percentage off from peak possible.
  172. //
  173. // 255 * 255 - (sum_s - sum_r) / count * (sum_s - sum_r) / count
  174. //
  175. static double ssimv_similarity2(const Ssimv *sv, int64_t n) {
  176. // Scale the constants by number of pixels.
  177. const int64_t c1 = (cc1 * n * n) >> 12;
  178. const int64_t c2 = (cc2 * n * n) >> 12;
  179. const double mean_diff = (1.0 * sv->sum_s - sv->sum_r) / n;
  180. const double l = (255 * 255 - mean_diff * mean_diff + c1) / (255 * 255 + c1);
  181. // Since these variables are unsigned, sums convert to double so
  182. // math is done in double arithmetic.
  183. const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2) /
  184. (n * sv->sum_sq_s - sv->sum_s * sv->sum_s +
  185. n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2);
  186. return l * v;
  187. }
  188. static void ssimv_parms(uint8_t *img1, int img1_pitch, uint8_t *img2,
  189. int img2_pitch, Ssimv *sv) {
  190. aom_ssim_parms_8x8(img1, img1_pitch, img2, img2_pitch, &sv->sum_s, &sv->sum_r,
  191. &sv->sum_sq_s, &sv->sum_sq_r, &sv->sum_sxr);
  192. }
  193. double aom_get_ssim_metrics(uint8_t *img1, int img1_pitch, uint8_t *img2,
  194. int img2_pitch, int width, int height, Ssimv *sv2,
  195. Metrics *m, int do_inconsistency) {
  196. double dssim_total = 0;
  197. double ssim_total = 0;
  198. double ssim2_total = 0;
  199. double inconsistency_total = 0;
  200. int i, j;
  201. int c = 0;
  202. double norm;
  203. double old_ssim_total = 0;
  204. // We can sample points as frequently as we like start with 1 per 4x4.
  205. for (i = 0; i < height;
  206. i += 4, img1 += img1_pitch * 4, img2 += img2_pitch * 4) {
  207. for (j = 0; j < width; j += 4, ++c) {
  208. Ssimv sv = { 0, 0, 0, 0, 0, 0 };
  209. double ssim;
  210. double ssim2;
  211. double dssim;
  212. uint32_t var_new;
  213. uint32_t var_old;
  214. uint32_t mean_new;
  215. uint32_t mean_old;
  216. double ssim_new;
  217. double ssim_old;
  218. // Not sure there's a great way to handle the edge pixels
  219. // in ssim when using a window. Seems biased against edge pixels
  220. // however you handle this. This uses only samples that are
  221. // fully in the frame.
  222. if (j + 8 <= width && i + 8 <= height) {
  223. ssimv_parms(img1 + j, img1_pitch, img2 + j, img2_pitch, &sv);
  224. }
  225. ssim = ssimv_similarity(&sv, 64);
  226. ssim2 = ssimv_similarity2(&sv, 64);
  227. sv.ssim = ssim2;
  228. // dssim is calculated to use as an actual error metric and
  229. // is scaled up to the same range as sum square error.
  230. // Since we are subsampling every 16th point maybe this should be
  231. // *16 ?
  232. dssim = 255 * 255 * (1 - ssim2) / 2;
  233. // Here I introduce a new error metric: consistency-weighted
  234. // SSIM-inconsistency. This metric isolates frames where the
  235. // SSIM 'suddenly' changes, e.g. if one frame in every 8 is much
  236. // sharper or blurrier than the others. Higher values indicate a
  237. // temporally inconsistent SSIM. There are two ideas at work:
  238. //
  239. // 1) 'SSIM-inconsistency': the total inconsistency value
  240. // reflects how much SSIM values are changing between this
  241. // source / reference frame pair and the previous pair.
  242. //
  243. // 2) 'consistency-weighted': weights de-emphasize areas in the
  244. // frame where the scene content has changed. Changes in scene
  245. // content are detected via changes in local variance and local
  246. // mean.
  247. //
  248. // Thus the overall measure reflects how inconsistent the SSIM
  249. // values are, over consistent regions of the frame.
  250. //
  251. // The metric has three terms:
  252. //
  253. // term 1 -> uses change in scene Variance to weight error score
  254. // 2 * var(Fi)*var(Fi-1) / (var(Fi)^2+var(Fi-1)^2)
  255. // larger changes from one frame to the next mean we care
  256. // less about consistency.
  257. //
  258. // term 2 -> uses change in local scene luminance to weight error
  259. // 2 * avg(Fi)*avg(Fi-1) / (avg(Fi)^2+avg(Fi-1)^2)
  260. // larger changes from one frame to the next mean we care
  261. // less about consistency.
  262. //
  263. // term3 -> measures inconsistency in ssim scores between frames
  264. // 1 - ( 2 * ssim(Fi)*ssim(Fi-1)/(ssim(Fi)^2+sssim(Fi-1)^2).
  265. //
  266. // This term compares the ssim score for the same location in 2
  267. // subsequent frames.
  268. var_new = sv.sum_sq_s - sv.sum_s * sv.sum_s / 64;
  269. var_old = sv2[c].sum_sq_s - sv2[c].sum_s * sv2[c].sum_s / 64;
  270. mean_new = sv.sum_s;
  271. mean_old = sv2[c].sum_s;
  272. ssim_new = sv.ssim;
  273. ssim_old = sv2[c].ssim;
  274. if (do_inconsistency) {
  275. // We do the metric once for every 4x4 block in the image. Since
  276. // we are scaling the error to SSE for use in a psnr calculation
  277. // 1.0 = 4x4x255x255 the worst error we can possibly have.
  278. static const double kScaling = 4. * 4 * 255 * 255;
  279. // The constants have to be non 0 to avoid potential divide by 0
  280. // issues other than that they affect kind of a weighting between
  281. // the terms. No testing of what the right terms should be has been
  282. // done.
  283. static const double c1 = 1, c2 = 1, c3 = 1;
  284. // This measures how much consistent variance is in two consecutive
  285. // source frames. 1.0 means they have exactly the same variance.
  286. const double variance_term =
  287. (2.0 * var_old * var_new + c1) /
  288. (1.0 * var_old * var_old + 1.0 * var_new * var_new + c1);
  289. // This measures how consistent the local mean are between two
  290. // consecutive frames. 1.0 means they have exactly the same mean.
  291. const double mean_term =
  292. (2.0 * mean_old * mean_new + c2) /
  293. (1.0 * mean_old * mean_old + 1.0 * mean_new * mean_new + c2);
  294. // This measures how consistent the ssims of two
  295. // consecutive frames is. 1.0 means they are exactly the same.
  296. double ssim_term =
  297. pow((2.0 * ssim_old * ssim_new + c3) /
  298. (ssim_old * ssim_old + ssim_new * ssim_new + c3),
  299. 5);
  300. double this_inconsistency;
  301. // Floating point math sometimes makes this > 1 by a tiny bit.
  302. // We want the metric to scale between 0 and 1.0 so we can convert
  303. // it to an snr scaled value.
  304. if (ssim_term > 1) ssim_term = 1;
  305. // This converts the consistency metric to an inconsistency metric
  306. // ( so we can scale it like psnr to something like sum square error.
  307. // The reason for the variance and mean terms is the assumption that
  308. // if there are big changes in the source we shouldn't penalize
  309. // inconsistency in ssim scores a bit less as it will be less visible
  310. // to the user.
  311. this_inconsistency = (1 - ssim_term) * variance_term * mean_term;
  312. this_inconsistency *= kScaling;
  313. inconsistency_total += this_inconsistency;
  314. }
  315. sv2[c] = sv;
  316. ssim_total += ssim;
  317. ssim2_total += ssim2;
  318. dssim_total += dssim;
  319. old_ssim_total += ssim_old;
  320. }
  321. old_ssim_total += 0;
  322. }
  323. norm = 1. / (width / 4) / (height / 4);
  324. ssim_total *= norm;
  325. ssim2_total *= norm;
  326. m->ssim2 = ssim2_total;
  327. m->ssim = ssim_total;
  328. if (old_ssim_total == 0) inconsistency_total = 0;
  329. m->ssimc = inconsistency_total;
  330. m->dssim = dssim_total;
  331. return inconsistency_total;
  332. }
  333. #endif // CONFIG_INTERNAL_STATS
  334. #if CONFIG_AV1_HIGHBITDEPTH
  335. void aom_highbd_ssim_parms_8x8_c(const uint16_t *s, int sp, const uint16_t *r,
  336. int rp, uint32_t *sum_s, uint32_t *sum_r,
  337. uint32_t *sum_sq_s, uint32_t *sum_sq_r,
  338. uint32_t *sum_sxr) {
  339. int i, j;
  340. for (i = 0; i < 8; i++, s += sp, r += rp) {
  341. for (j = 0; j < 8; j++) {
  342. *sum_s += s[j];
  343. *sum_r += r[j];
  344. *sum_sq_s += s[j] * s[j];
  345. *sum_sq_r += r[j] * r[j];
  346. *sum_sxr += s[j] * r[j];
  347. }
  348. }
  349. }
  350. static double highbd_ssim_8x8(const uint16_t *s, int sp, const uint16_t *r,
  351. int rp, uint32_t bd, uint32_t shift) {
  352. uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0;
  353. aom_highbd_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r,
  354. &sum_sxr);
  355. return similarity(sum_s >> shift, sum_r >> shift, sum_sq_s >> (2 * shift),
  356. sum_sq_r >> (2 * shift), sum_sxr >> (2 * shift), 64, bd);
  357. }
  358. double aom_highbd_ssim2(const uint8_t *img1, const uint8_t *img2,
  359. int stride_img1, int stride_img2, int width, int height,
  360. uint32_t bd, uint32_t shift) {
  361. int i, j;
  362. int samples = 0;
  363. double ssim_total = 0;
  364. // sample point start with each 4x4 location
  365. for (i = 0; i <= height - 8;
  366. i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) {
  367. for (j = 0; j <= width - 8; j += 4) {
  368. double v = highbd_ssim_8x8(CONVERT_TO_SHORTPTR(img1 + j), stride_img1,
  369. CONVERT_TO_SHORTPTR(img2 + j), stride_img2, bd,
  370. shift);
  371. ssim_total += v;
  372. samples++;
  373. }
  374. }
  375. ssim_total /= samples;
  376. return ssim_total;
  377. }
  378. #if CONFIG_INTERNAL_STATS
  379. void aom_highbd_calc_ssim(const YV12_BUFFER_CONFIG *source,
  380. const YV12_BUFFER_CONFIG *dest, double *weight,
  381. uint32_t bd, uint32_t in_bd, double *fast_ssim) {
  382. assert(bd >= in_bd);
  383. uint32_t shift = bd - in_bd;
  384. double abc[3];
  385. for (int i = 0; i < 3; ++i) {
  386. const int is_uv = i > 0;
  387. abc[i] = aom_highbd_ssim2(source->buffers[i], dest->buffers[i],
  388. source->strides[is_uv], dest->strides[is_uv],
  389. source->crop_widths[is_uv],
  390. source->crop_heights[is_uv], in_bd, shift);
  391. }
  392. weight[0] = 1;
  393. fast_ssim[0] = abc[0] * .8 + .1 * (abc[1] + abc[2]);
  394. if (bd > in_bd) {
  395. // Compute SSIM based on stream bit depth
  396. shift = 0;
  397. for (int i = 0; i < 3; ++i) {
  398. const int is_uv = i > 0;
  399. abc[i] = aom_highbd_ssim2(source->buffers[i], dest->buffers[i],
  400. source->strides[is_uv], dest->strides[is_uv],
  401. source->crop_widths[is_uv],
  402. source->crop_heights[is_uv], bd, shift);
  403. }
  404. weight[1] = 1;
  405. fast_ssim[1] = abc[0] * .8 + .1 * (abc[1] + abc[2]);
  406. }
  407. }
  408. #endif // CONFIG_INTERNAL_STATS
  409. #endif // CONFIG_AV1_HIGHBITDEPTH
  410. #if CONFIG_INTERNAL_STATS
  411. void aom_calc_ssim(const YV12_BUFFER_CONFIG *orig,
  412. const YV12_BUFFER_CONFIG *recon, const uint32_t bit_depth,
  413. const uint32_t in_bit_depth, int is_hbd, double *weight,
  414. double *frame_ssim2) {
  415. #if CONFIG_AV1_HIGHBITDEPTH
  416. if (is_hbd) {
  417. aom_highbd_calc_ssim(orig, recon, weight, bit_depth, in_bit_depth,
  418. frame_ssim2);
  419. return;
  420. }
  421. #else
  422. (void)bit_depth;
  423. (void)in_bit_depth;
  424. (void)is_hbd;
  425. #endif // CONFIG_AV1_HIGHBITDEPTH
  426. aom_lowbd_calc_ssim(orig, recon, weight, frame_ssim2);
  427. }
  428. #endif // CONFIG_INTERNAL_STATS