2
0

ssim.c 17 KB

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