fastssim.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. * This code was originally written by: Nathan E. Egge, at the Daala
  11. * project.
  12. */
  13. #include <assert.h>
  14. #include <math.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "./vpx_config.h"
  18. #include "./vpx_dsp_rtcd.h"
  19. #include "vpx_dsp/ssim.h"
  20. #include "vpx_ports/system_state.h"
  21. typedef struct fs_level fs_level;
  22. typedef struct fs_ctx fs_ctx;
  23. #define SSIM_C1 (255 * 255 * 0.01 * 0.01)
  24. #define SSIM_C2 (255 * 255 * 0.03 * 0.03)
  25. #if CONFIG_VP9_HIGHBITDEPTH
  26. #define SSIM_C1_10 (1023 * 1023 * 0.01 * 0.01)
  27. #define SSIM_C1_12 (4095 * 4095 * 0.01 * 0.01)
  28. #define SSIM_C2_10 (1023 * 1023 * 0.03 * 0.03)
  29. #define SSIM_C2_12 (4095 * 4095 * 0.03 * 0.03)
  30. #endif
  31. #define FS_MINI(_a, _b) ((_a) < (_b) ? (_a) : (_b))
  32. #define FS_MAXI(_a, _b) ((_a) > (_b) ? (_a) : (_b))
  33. struct fs_level {
  34. uint32_t *im1;
  35. uint32_t *im2;
  36. double *ssim;
  37. int w;
  38. int h;
  39. };
  40. struct fs_ctx {
  41. fs_level *level;
  42. int nlevels;
  43. unsigned *col_buf;
  44. };
  45. static void fs_ctx_init(fs_ctx *_ctx, int _w, int _h, int _nlevels) {
  46. unsigned char *data;
  47. size_t data_size;
  48. int lw;
  49. int lh;
  50. int l;
  51. lw = (_w + 1) >> 1;
  52. lh = (_h + 1) >> 1;
  53. data_size =
  54. _nlevels * sizeof(fs_level) + 2 * (lw + 8) * 8 * sizeof(*_ctx->col_buf);
  55. for (l = 0; l < _nlevels; l++) {
  56. size_t im_size;
  57. size_t level_size;
  58. im_size = lw * (size_t)lh;
  59. level_size = 2 * im_size * sizeof(*_ctx->level[l].im1);
  60. level_size += sizeof(*_ctx->level[l].ssim) - 1;
  61. level_size /= sizeof(*_ctx->level[l].ssim);
  62. level_size += im_size;
  63. level_size *= sizeof(*_ctx->level[l].ssim);
  64. data_size += level_size;
  65. lw = (lw + 1) >> 1;
  66. lh = (lh + 1) >> 1;
  67. }
  68. data = (unsigned char *)malloc(data_size);
  69. _ctx->level = (fs_level *)data;
  70. _ctx->nlevels = _nlevels;
  71. data += _nlevels * sizeof(*_ctx->level);
  72. lw = (_w + 1) >> 1;
  73. lh = (_h + 1) >> 1;
  74. for (l = 0; l < _nlevels; l++) {
  75. size_t im_size;
  76. size_t level_size;
  77. _ctx->level[l].w = lw;
  78. _ctx->level[l].h = lh;
  79. im_size = lw * (size_t)lh;
  80. level_size = 2 * im_size * sizeof(*_ctx->level[l].im1);
  81. level_size += sizeof(*_ctx->level[l].ssim) - 1;
  82. level_size /= sizeof(*_ctx->level[l].ssim);
  83. level_size *= sizeof(*_ctx->level[l].ssim);
  84. _ctx->level[l].im1 = (uint32_t *)data;
  85. _ctx->level[l].im2 = _ctx->level[l].im1 + im_size;
  86. data += level_size;
  87. _ctx->level[l].ssim = (double *)data;
  88. data += im_size * sizeof(*_ctx->level[l].ssim);
  89. lw = (lw + 1) >> 1;
  90. lh = (lh + 1) >> 1;
  91. }
  92. _ctx->col_buf = (unsigned *)data;
  93. }
  94. static void fs_ctx_clear(fs_ctx *_ctx) { free(_ctx->level); }
  95. static void fs_downsample_level(fs_ctx *_ctx, int _l) {
  96. const uint32_t *src1;
  97. const uint32_t *src2;
  98. uint32_t *dst1;
  99. uint32_t *dst2;
  100. int w2;
  101. int h2;
  102. int w;
  103. int h;
  104. int i;
  105. int j;
  106. w = _ctx->level[_l].w;
  107. h = _ctx->level[_l].h;
  108. dst1 = _ctx->level[_l].im1;
  109. dst2 = _ctx->level[_l].im2;
  110. w2 = _ctx->level[_l - 1].w;
  111. h2 = _ctx->level[_l - 1].h;
  112. src1 = _ctx->level[_l - 1].im1;
  113. src2 = _ctx->level[_l - 1].im2;
  114. for (j = 0; j < h; j++) {
  115. int j0offs;
  116. int j1offs;
  117. j0offs = 2 * j * w2;
  118. j1offs = FS_MINI(2 * j + 1, h2) * w2;
  119. for (i = 0; i < w; i++) {
  120. int i0;
  121. int i1;
  122. i0 = 2 * i;
  123. i1 = FS_MINI(i0 + 1, w2);
  124. dst1[j * w + i] = src1[j0offs + i0] + src1[j0offs + i1] +
  125. src1[j1offs + i0] + src1[j1offs + i1];
  126. dst2[j * w + i] = src2[j0offs + i0] + src2[j0offs + i1] +
  127. src2[j1offs + i0] + src2[j1offs + i1];
  128. }
  129. }
  130. }
  131. static void fs_downsample_level0(fs_ctx *_ctx, const uint8_t *_src1,
  132. int _s1ystride, const uint8_t *_src2,
  133. int _s2ystride, int _w, int _h, uint32_t bd,
  134. uint32_t shift) {
  135. uint32_t *dst1;
  136. uint32_t *dst2;
  137. int w;
  138. int h;
  139. int i;
  140. int j;
  141. w = _ctx->level[0].w;
  142. h = _ctx->level[0].h;
  143. dst1 = _ctx->level[0].im1;
  144. dst2 = _ctx->level[0].im2;
  145. for (j = 0; j < h; j++) {
  146. int j0;
  147. int j1;
  148. j0 = 2 * j;
  149. j1 = FS_MINI(j0 + 1, _h);
  150. for (i = 0; i < w; i++) {
  151. int i0;
  152. int i1;
  153. i0 = 2 * i;
  154. i1 = FS_MINI(i0 + 1, _w);
  155. if (bd == 8 && shift == 0) {
  156. dst1[j * w + i] =
  157. _src1[j0 * _s1ystride + i0] + _src1[j0 * _s1ystride + i1] +
  158. _src1[j1 * _s1ystride + i0] + _src1[j1 * _s1ystride + i1];
  159. dst2[j * w + i] =
  160. _src2[j0 * _s2ystride + i0] + _src2[j0 * _s2ystride + i1] +
  161. _src2[j1 * _s2ystride + i0] + _src2[j1 * _s2ystride + i1];
  162. } else {
  163. uint16_t *src1s = CONVERT_TO_SHORTPTR(_src1);
  164. uint16_t *src2s = CONVERT_TO_SHORTPTR(_src2);
  165. dst1[j * w + i] = (src1s[j0 * _s1ystride + i0] >> shift) +
  166. (src1s[j0 * _s1ystride + i1] >> shift) +
  167. (src1s[j1 * _s1ystride + i0] >> shift) +
  168. (src1s[j1 * _s1ystride + i1] >> shift);
  169. dst2[j * w + i] = (src2s[j0 * _s2ystride + i0] >> shift) +
  170. (src2s[j0 * _s2ystride + i1] >> shift) +
  171. (src2s[j1 * _s2ystride + i0] >> shift) +
  172. (src2s[j1 * _s2ystride + i1] >> shift);
  173. }
  174. }
  175. }
  176. }
  177. static void fs_apply_luminance(fs_ctx *_ctx, int _l, int bit_depth) {
  178. unsigned *col_sums_x;
  179. unsigned *col_sums_y;
  180. uint32_t *im1;
  181. uint32_t *im2;
  182. double *ssim;
  183. double c1;
  184. int w;
  185. int h;
  186. int j0offs;
  187. int j1offs;
  188. int i;
  189. int j;
  190. double ssim_c1 = SSIM_C1;
  191. #if CONFIG_VP9_HIGHBITDEPTH
  192. if (bit_depth == 10) ssim_c1 = SSIM_C1_10;
  193. if (bit_depth == 12) ssim_c1 = SSIM_C1_12;
  194. #else
  195. assert(bit_depth == 8);
  196. (void)bit_depth;
  197. #endif
  198. w = _ctx->level[_l].w;
  199. h = _ctx->level[_l].h;
  200. col_sums_x = _ctx->col_buf;
  201. col_sums_y = col_sums_x + w;
  202. im1 = _ctx->level[_l].im1;
  203. im2 = _ctx->level[_l].im2;
  204. for (i = 0; i < w; i++) col_sums_x[i] = 5 * im1[i];
  205. for (i = 0; i < w; i++) col_sums_y[i] = 5 * im2[i];
  206. for (j = 1; j < 4; j++) {
  207. j1offs = FS_MINI(j, h - 1) * w;
  208. for (i = 0; i < w; i++) col_sums_x[i] += im1[j1offs + i];
  209. for (i = 0; i < w; i++) col_sums_y[i] += im2[j1offs + i];
  210. }
  211. ssim = _ctx->level[_l].ssim;
  212. c1 = (double)(ssim_c1 * 4096 * (1 << 4 * _l));
  213. for (j = 0; j < h; j++) {
  214. unsigned mux;
  215. unsigned muy;
  216. int i0;
  217. int i1;
  218. mux = 5 * col_sums_x[0];
  219. muy = 5 * col_sums_y[0];
  220. for (i = 1; i < 4; i++) {
  221. i1 = FS_MINI(i, w - 1);
  222. mux += col_sums_x[i1];
  223. muy += col_sums_y[i1];
  224. }
  225. for (i = 0; i < w; i++) {
  226. ssim[j * w + i] *= (2 * mux * (double)muy + c1) /
  227. (mux * (double)mux + muy * (double)muy + c1);
  228. if (i + 1 < w) {
  229. i0 = FS_MAXI(0, i - 4);
  230. i1 = FS_MINI(i + 4, w - 1);
  231. mux += col_sums_x[i1] - col_sums_x[i0];
  232. muy += col_sums_x[i1] - col_sums_x[i0];
  233. }
  234. }
  235. if (j + 1 < h) {
  236. j0offs = FS_MAXI(0, j - 4) * w;
  237. for (i = 0; i < w; i++) col_sums_x[i] -= im1[j0offs + i];
  238. for (i = 0; i < w; i++) col_sums_y[i] -= im2[j0offs + i];
  239. j1offs = FS_MINI(j + 4, h - 1) * w;
  240. for (i = 0; i < w; i++) col_sums_x[i] += im1[j1offs + i];
  241. for (i = 0; i < w; i++) col_sums_y[i] += im2[j1offs + i];
  242. }
  243. }
  244. }
  245. #define FS_COL_SET(_col, _joffs, _ioffs) \
  246. do { \
  247. unsigned gx; \
  248. unsigned gy; \
  249. gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
  250. gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
  251. col_sums_gx2[(_col)] = gx * (double)gx; \
  252. col_sums_gy2[(_col)] = gy * (double)gy; \
  253. col_sums_gxgy[(_col)] = gx * (double)gy; \
  254. } while (0)
  255. #define FS_COL_ADD(_col, _joffs, _ioffs) \
  256. do { \
  257. unsigned gx; \
  258. unsigned gy; \
  259. gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
  260. gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
  261. col_sums_gx2[(_col)] += gx * (double)gx; \
  262. col_sums_gy2[(_col)] += gy * (double)gy; \
  263. col_sums_gxgy[(_col)] += gx * (double)gy; \
  264. } while (0)
  265. #define FS_COL_SUB(_col, _joffs, _ioffs) \
  266. do { \
  267. unsigned gx; \
  268. unsigned gy; \
  269. gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
  270. gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
  271. col_sums_gx2[(_col)] -= gx * (double)gx; \
  272. col_sums_gy2[(_col)] -= gy * (double)gy; \
  273. col_sums_gxgy[(_col)] -= gx * (double)gy; \
  274. } while (0)
  275. #define FS_COL_COPY(_col1, _col2) \
  276. do { \
  277. col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)]; \
  278. col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)]; \
  279. col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)]; \
  280. } while (0)
  281. #define FS_COL_HALVE(_col1, _col2) \
  282. do { \
  283. col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)] * 0.5; \
  284. col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)] * 0.5; \
  285. col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)] * 0.5; \
  286. } while (0)
  287. #define FS_COL_DOUBLE(_col1, _col2) \
  288. do { \
  289. col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)] * 2; \
  290. col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)] * 2; \
  291. col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)] * 2; \
  292. } while (0)
  293. static void fs_calc_structure(fs_ctx *_ctx, int _l, int bit_depth) {
  294. uint32_t *im1;
  295. uint32_t *im2;
  296. unsigned *gx_buf;
  297. unsigned *gy_buf;
  298. double *ssim;
  299. double col_sums_gx2[8];
  300. double col_sums_gy2[8];
  301. double col_sums_gxgy[8];
  302. double c2;
  303. int stride;
  304. int w;
  305. int h;
  306. int i;
  307. int j;
  308. double ssim_c2 = SSIM_C2;
  309. #if CONFIG_VP9_HIGHBITDEPTH
  310. if (bit_depth == 10) ssim_c2 = SSIM_C2_10;
  311. if (bit_depth == 12) ssim_c2 = SSIM_C2_12;
  312. #else
  313. assert(bit_depth == 8);
  314. (void)bit_depth;
  315. #endif
  316. w = _ctx->level[_l].w;
  317. h = _ctx->level[_l].h;
  318. im1 = _ctx->level[_l].im1;
  319. im2 = _ctx->level[_l].im2;
  320. ssim = _ctx->level[_l].ssim;
  321. gx_buf = _ctx->col_buf;
  322. stride = w + 8;
  323. gy_buf = gx_buf + 8 * stride;
  324. memset(gx_buf, 0, 2 * 8 * stride * sizeof(*gx_buf));
  325. c2 = ssim_c2 * (1 << 4 * _l) * 16 * 104;
  326. for (j = 0; j < h + 4; j++) {
  327. if (j < h - 1) {
  328. for (i = 0; i < w - 1; i++) {
  329. unsigned g1;
  330. unsigned g2;
  331. unsigned gx;
  332. unsigned gy;
  333. g1 = abs((int)im1[(j + 1) * w + i + 1] - (int)im1[j * w + i]);
  334. g2 = abs((int)im1[(j + 1) * w + i] - (int)im1[j * w + i + 1]);
  335. gx = 4 * FS_MAXI(g1, g2) + FS_MINI(g1, g2);
  336. g1 = abs((int)im2[(j + 1) * w + i + 1] - (int)im2[j * w + i]);
  337. g2 = abs((int)im2[(j + 1) * w + i] - (int)im2[j * w + i + 1]);
  338. gy = 4 * FS_MAXI(g1, g2) + FS_MINI(g1, g2);
  339. gx_buf[(j & 7) * stride + i + 4] = gx;
  340. gy_buf[(j & 7) * stride + i + 4] = gy;
  341. }
  342. } else {
  343. memset(gx_buf + (j & 7) * stride, 0, stride * sizeof(*gx_buf));
  344. memset(gy_buf + (j & 7) * stride, 0, stride * sizeof(*gy_buf));
  345. }
  346. if (j >= 4) {
  347. int k;
  348. col_sums_gx2[3] = col_sums_gx2[2] = col_sums_gx2[1] = col_sums_gx2[0] = 0;
  349. col_sums_gy2[3] = col_sums_gy2[2] = col_sums_gy2[1] = col_sums_gy2[0] = 0;
  350. col_sums_gxgy[3] = col_sums_gxgy[2] = col_sums_gxgy[1] =
  351. col_sums_gxgy[0] = 0;
  352. for (i = 4; i < 8; i++) {
  353. FS_COL_SET(i, -1, 0);
  354. FS_COL_ADD(i, 0, 0);
  355. for (k = 1; k < 8 - i; k++) {
  356. FS_COL_DOUBLE(i, i);
  357. FS_COL_ADD(i, -k - 1, 0);
  358. FS_COL_ADD(i, k, 0);
  359. }
  360. }
  361. for (i = 0; i < w; i++) {
  362. double mugx2;
  363. double mugy2;
  364. double mugxgy;
  365. mugx2 = col_sums_gx2[0];
  366. for (k = 1; k < 8; k++) mugx2 += col_sums_gx2[k];
  367. mugy2 = col_sums_gy2[0];
  368. for (k = 1; k < 8; k++) mugy2 += col_sums_gy2[k];
  369. mugxgy = col_sums_gxgy[0];
  370. for (k = 1; k < 8; k++) mugxgy += col_sums_gxgy[k];
  371. ssim[(j - 4) * w + i] = (2 * mugxgy + c2) / (mugx2 + mugy2 + c2);
  372. if (i + 1 < w) {
  373. FS_COL_SET(0, -1, 1);
  374. FS_COL_ADD(0, 0, 1);
  375. FS_COL_SUB(2, -3, 2);
  376. FS_COL_SUB(2, 2, 2);
  377. FS_COL_HALVE(1, 2);
  378. FS_COL_SUB(3, -4, 3);
  379. FS_COL_SUB(3, 3, 3);
  380. FS_COL_HALVE(2, 3);
  381. FS_COL_COPY(3, 4);
  382. FS_COL_DOUBLE(4, 5);
  383. FS_COL_ADD(4, -4, 5);
  384. FS_COL_ADD(4, 3, 5);
  385. FS_COL_DOUBLE(5, 6);
  386. FS_COL_ADD(5, -3, 6);
  387. FS_COL_ADD(5, 2, 6);
  388. FS_COL_DOUBLE(6, 7);
  389. FS_COL_ADD(6, -2, 7);
  390. FS_COL_ADD(6, 1, 7);
  391. FS_COL_SET(7, -1, 8);
  392. FS_COL_ADD(7, 0, 8);
  393. }
  394. }
  395. }
  396. }
  397. }
  398. #define FS_NLEVELS (4)
  399. /*These weights were derived from the default weights found in Wang's original
  400. Matlab implementation: {0.0448, 0.2856, 0.2363, 0.1333}.
  401. We drop the finest scale and renormalize the rest to sum to 1.*/
  402. static const double FS_WEIGHTS[FS_NLEVELS] = {
  403. 0.2989654541015625, 0.3141326904296875, 0.2473602294921875, 0.1395416259765625
  404. };
  405. static double fs_average(fs_ctx *_ctx, int _l) {
  406. double *ssim;
  407. double ret;
  408. int w;
  409. int h;
  410. int i;
  411. int j;
  412. w = _ctx->level[_l].w;
  413. h = _ctx->level[_l].h;
  414. ssim = _ctx->level[_l].ssim;
  415. ret = 0;
  416. for (j = 0; j < h; j++)
  417. for (i = 0; i < w; i++) ret += ssim[j * w + i];
  418. return pow(ret / (w * h), FS_WEIGHTS[_l]);
  419. }
  420. static double convert_ssim_db(double _ssim, double _weight) {
  421. assert(_weight >= _ssim);
  422. if ((_weight - _ssim) < 1e-10) return MAX_SSIM_DB;
  423. return 10 * (log10(_weight) - log10(_weight - _ssim));
  424. }
  425. static double calc_ssim(const uint8_t *_src, int _systride, const uint8_t *_dst,
  426. int _dystride, int _w, int _h, uint32_t _bd,
  427. uint32_t _shift) {
  428. fs_ctx ctx;
  429. double ret;
  430. int l;
  431. ret = 1;
  432. fs_ctx_init(&ctx, _w, _h, FS_NLEVELS);
  433. fs_downsample_level0(&ctx, _src, _systride, _dst, _dystride, _w, _h, _bd,
  434. _shift);
  435. for (l = 0; l < FS_NLEVELS - 1; l++) {
  436. fs_calc_structure(&ctx, l, _bd);
  437. ret *= fs_average(&ctx, l);
  438. fs_downsample_level(&ctx, l + 1);
  439. }
  440. fs_calc_structure(&ctx, l, _bd);
  441. fs_apply_luminance(&ctx, l, _bd);
  442. ret *= fs_average(&ctx, l);
  443. fs_ctx_clear(&ctx);
  444. return ret;
  445. }
  446. double vpx_calc_fastssim(const YV12_BUFFER_CONFIG *source,
  447. const YV12_BUFFER_CONFIG *dest, double *ssim_y,
  448. double *ssim_u, double *ssim_v, uint32_t bd,
  449. uint32_t in_bd) {
  450. double ssimv;
  451. uint32_t bd_shift = 0;
  452. vpx_clear_system_state();
  453. assert(bd >= in_bd);
  454. bd_shift = bd - in_bd;
  455. *ssim_y = calc_ssim(source->y_buffer, source->y_stride, dest->y_buffer,
  456. dest->y_stride, source->y_crop_width,
  457. source->y_crop_height, in_bd, bd_shift);
  458. *ssim_u = calc_ssim(source->u_buffer, source->uv_stride, dest->u_buffer,
  459. dest->uv_stride, source->uv_crop_width,
  460. source->uv_crop_height, in_bd, bd_shift);
  461. *ssim_v = calc_ssim(source->v_buffer, source->uv_stride, dest->v_buffer,
  462. dest->uv_stride, source->uv_crop_width,
  463. source->uv_crop_height, in_bd, bd_shift);
  464. ssimv = (*ssim_y) * .8 + .1 * ((*ssim_u) + (*ssim_v));
  465. return convert_ssim_db(ssimv, 1.0);
  466. }