psnrhvs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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: Gregory Maxwell, at the Daala
  11. * project.
  12. */
  13. #include <assert.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <math.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. #include "vpx_dsp/psnr.h"
  22. #if !defined(M_PI)
  23. #define M_PI (3.141592653589793238462643)
  24. #endif
  25. #include <string.h>
  26. static void od_bin_fdct8x8(tran_low_t *y, int ystride, const int16_t *x,
  27. int xstride) {
  28. int i, j;
  29. (void)xstride;
  30. vpx_fdct8x8(x, y, ystride);
  31. for (i = 0; i < 8; i++)
  32. for (j = 0; j < 8; j++)
  33. *(y + ystride * i + j) = (*(y + ystride * i + j) + 4) >> 3;
  34. }
  35. #if CONFIG_VP9_HIGHBITDEPTH
  36. static void hbd_od_bin_fdct8x8(tran_low_t *y, int ystride, const int16_t *x,
  37. int xstride) {
  38. int i, j;
  39. (void)xstride;
  40. vpx_highbd_fdct8x8(x, y, ystride);
  41. for (i = 0; i < 8; i++)
  42. for (j = 0; j < 8; j++)
  43. *(y + ystride * i + j) = (*(y + ystride * i + j) + 4) >> 3;
  44. }
  45. #endif
  46. /* Normalized inverse quantization matrix for 8x8 DCT at the point of
  47. * transparency. This is not the JPEG based matrix from the paper,
  48. this one gives a slightly higher MOS agreement.*/
  49. static const double csf_y[8][8] = {
  50. { 1.6193873005, 2.2901594831, 2.08509755623, 1.48366094411, 1.00227514334,
  51. 0.678296995242, 0.466224900598, 0.3265091542 },
  52. { 2.2901594831, 1.94321815382, 2.04793073064, 1.68731108984, 1.2305666963,
  53. 0.868920337363, 0.61280991668, 0.436405793551 },
  54. { 2.08509755623, 2.04793073064, 1.34329019223, 1.09205635862, 0.875748795257,
  55. 0.670882927016, 0.501731932449, 0.372504254596 },
  56. { 1.48366094411, 1.68731108984, 1.09205635862, 0.772819797575, 0.605636379554,
  57. 0.48309405692, 0.380429446972, 0.295774038565 },
  58. { 1.00227514334, 1.2305666963, 0.875748795257, 0.605636379554, 0.448996256676,
  59. 0.352889268808, 0.283006984131, 0.226951348204 },
  60. { 0.678296995242, 0.868920337363, 0.670882927016, 0.48309405692,
  61. 0.352889268808, 0.27032073436, 0.215017739696, 0.17408067321 },
  62. { 0.466224900598, 0.61280991668, 0.501731932449, 0.380429446972,
  63. 0.283006984131, 0.215017739696, 0.168869545842, 0.136153931001 },
  64. { 0.3265091542, 0.436405793551, 0.372504254596, 0.295774038565,
  65. 0.226951348204, 0.17408067321, 0.136153931001, 0.109083846276 }
  66. };
  67. static const double csf_cb420[8][8] = {
  68. { 1.91113096927, 2.46074210438, 1.18284184739, 1.14982565193, 1.05017074788,
  69. 0.898018824055, 0.74725392039, 0.615105596242 },
  70. { 2.46074210438, 1.58529308355, 1.21363250036, 1.38190029285, 1.33100189972,
  71. 1.17428548929, 0.996404342439, 0.830890433625 },
  72. { 1.18284184739, 1.21363250036, 0.978712413627, 1.02624506078, 1.03145147362,
  73. 0.960060382087, 0.849823426169, 0.731221236837 },
  74. { 1.14982565193, 1.38190029285, 1.02624506078, 0.861317501629, 0.801821139099,
  75. 0.751437590932, 0.685398513368, 0.608694761374 },
  76. { 1.05017074788, 1.33100189972, 1.03145147362, 0.801821139099, 0.676555426187,
  77. 0.605503172737, 0.55002013668, 0.495804539034 },
  78. { 0.898018824055, 1.17428548929, 0.960060382087, 0.751437590932,
  79. 0.605503172737, 0.514674450957, 0.454353482512, 0.407050308965 },
  80. { 0.74725392039, 0.996404342439, 0.849823426169, 0.685398513368,
  81. 0.55002013668, 0.454353482512, 0.389234902883, 0.342353999733 },
  82. { 0.615105596242, 0.830890433625, 0.731221236837, 0.608694761374,
  83. 0.495804539034, 0.407050308965, 0.342353999733, 0.295530605237 }
  84. };
  85. static const double csf_cr420[8][8] = {
  86. { 2.03871978502, 2.62502345193, 1.26180942886, 1.11019789803, 1.01397751469,
  87. 0.867069376285, 0.721500455585, 0.593906509971 },
  88. { 2.62502345193, 1.69112867013, 1.17180569821, 1.3342742857, 1.28513006198,
  89. 1.13381474809, 0.962064122248, 0.802254508198 },
  90. { 1.26180942886, 1.17180569821, 0.944981930573, 0.990876405848,
  91. 0.995903384143, 0.926972725286, 0.820534991409, 0.706020324706 },
  92. { 1.11019789803, 1.3342742857, 0.990876405848, 0.831632933426, 0.77418706195,
  93. 0.725539939514, 0.661776842059, 0.587716619023 },
  94. { 1.01397751469, 1.28513006198, 0.995903384143, 0.77418706195, 0.653238524286,
  95. 0.584635025748, 0.531064164893, 0.478717061273 },
  96. { 0.867069376285, 1.13381474809, 0.926972725286, 0.725539939514,
  97. 0.584635025748, 0.496936637883, 0.438694579826, 0.393021669543 },
  98. { 0.721500455585, 0.962064122248, 0.820534991409, 0.661776842059,
  99. 0.531064164893, 0.438694579826, 0.375820256136, 0.330555063063 },
  100. { 0.593906509971, 0.802254508198, 0.706020324706, 0.587716619023,
  101. 0.478717061273, 0.393021669543, 0.330555063063, 0.285345396658 }
  102. };
  103. static double convert_score_db(double _score, double _weight, int bit_depth) {
  104. int16_t pix_max = 255;
  105. assert(_score * _weight >= 0.0);
  106. if (bit_depth == 10)
  107. pix_max = 1023;
  108. else if (bit_depth == 12)
  109. pix_max = 4095;
  110. if (_weight * _score < pix_max * pix_max * 1e-10) return MAX_PSNR;
  111. return 10 * (log10(pix_max * pix_max) - log10(_weight * _score));
  112. }
  113. static double calc_psnrhvs(const unsigned char *src, int _systride,
  114. const unsigned char *dst, int _dystride, double _par,
  115. int _w, int _h, int _step, const double _csf[8][8],
  116. uint32_t bit_depth, uint32_t _shift) {
  117. double ret;
  118. const uint8_t *_src8 = src;
  119. const uint8_t *_dst8 = dst;
  120. const uint16_t *_src16 = CONVERT_TO_SHORTPTR(src);
  121. const uint16_t *_dst16 = CONVERT_TO_SHORTPTR(dst);
  122. DECLARE_ALIGNED(16, int16_t, dct_s[8 * 8]);
  123. DECLARE_ALIGNED(16, int16_t, dct_d[8 * 8]);
  124. DECLARE_ALIGNED(16, tran_low_t, dct_s_coef[8 * 8]);
  125. DECLARE_ALIGNED(16, tran_low_t, dct_d_coef[8 * 8]);
  126. double mask[8][8];
  127. int pixels;
  128. int x;
  129. int y;
  130. (void)_par;
  131. ret = pixels = 0;
  132. /*In the PSNR-HVS-M paper[1] the authors describe the construction of
  133. their masking table as "we have used the quantization table for the
  134. color component Y of JPEG [6] that has been also obtained on the
  135. basis of CSF. Note that the values in quantization table JPEG have
  136. been normalized and then squared." Their CSF matrix (from PSNR-HVS)
  137. was also constructed from the JPEG matrices. I can not find any obvious
  138. scheme of normalizing to produce their table, but if I multiply their
  139. CSF by 0.3885746225901003 and square the result I get their masking table.
  140. I have no idea where this constant comes from, but deviating from it
  141. too greatly hurts MOS agreement.
  142. [1] Nikolay Ponomarenko, Flavia Silvestri, Karen Egiazarian, Marco Carli,
  143. Jaakko Astola, Vladimir Lukin, "On between-coefficient contrast masking
  144. of DCT basis functions", CD-ROM Proceedings of the Third
  145. International Workshop on Video Processing and Quality Metrics for Consumer
  146. Electronics VPQM-07, Scottsdale, Arizona, USA, 25-26 January, 2007, 4 p.
  147. Suggested in aomedia issue #2363:
  148. 0.3885746225901003 is a reciprocal of the maximum coefficient (2.573509)
  149. of the old JPEG based matrix from the paper. Since you are not using that,
  150. divide by actual maximum coefficient. */
  151. for (x = 0; x < 8; x++)
  152. for (y = 0; y < 8; y++)
  153. mask[x][y] = (_csf[x][y] / _csf[1][0]) * (_csf[x][y] / _csf[1][0]);
  154. for (y = 0; y < _h - 7; y += _step) {
  155. for (x = 0; x < _w - 7; x += _step) {
  156. int i;
  157. int j;
  158. double s_means[4];
  159. double d_means[4];
  160. double s_vars[4];
  161. double d_vars[4];
  162. double s_gmean = 0;
  163. double d_gmean = 0;
  164. double s_gvar = 0;
  165. double d_gvar = 0;
  166. double s_mask = 0;
  167. double d_mask = 0;
  168. for (i = 0; i < 4; i++)
  169. s_means[i] = d_means[i] = s_vars[i] = d_vars[i] = 0;
  170. for (i = 0; i < 8; i++) {
  171. for (j = 0; j < 8; j++) {
  172. int sub = ((i & 12) >> 2) + ((j & 12) >> 1);
  173. if (bit_depth == 8 && _shift == 0) {
  174. dct_s[i * 8 + j] = _src8[(y + i) * _systride + (j + x)];
  175. dct_d[i * 8 + j] = _dst8[(y + i) * _dystride + (j + x)];
  176. } else if (bit_depth == 10 || bit_depth == 12) {
  177. dct_s[i * 8 + j] = _src16[(y + i) * _systride + (j + x)] >> _shift;
  178. dct_d[i * 8 + j] = _dst16[(y + i) * _dystride + (j + x)] >> _shift;
  179. }
  180. s_gmean += dct_s[i * 8 + j];
  181. d_gmean += dct_d[i * 8 + j];
  182. s_means[sub] += dct_s[i * 8 + j];
  183. d_means[sub] += dct_d[i * 8 + j];
  184. }
  185. }
  186. s_gmean /= 64.f;
  187. d_gmean /= 64.f;
  188. for (i = 0; i < 4; i++) s_means[i] /= 16.f;
  189. for (i = 0; i < 4; i++) d_means[i] /= 16.f;
  190. for (i = 0; i < 8; i++) {
  191. for (j = 0; j < 8; j++) {
  192. int sub = ((i & 12) >> 2) + ((j & 12) >> 1);
  193. s_gvar += (dct_s[i * 8 + j] - s_gmean) * (dct_s[i * 8 + j] - s_gmean);
  194. d_gvar += (dct_d[i * 8 + j] - d_gmean) * (dct_d[i * 8 + j] - d_gmean);
  195. s_vars[sub] += (dct_s[i * 8 + j] - s_means[sub]) *
  196. (dct_s[i * 8 + j] - s_means[sub]);
  197. d_vars[sub] += (dct_d[i * 8 + j] - d_means[sub]) *
  198. (dct_d[i * 8 + j] - d_means[sub]);
  199. }
  200. }
  201. s_gvar *= 1 / 63.f * 64;
  202. d_gvar *= 1 / 63.f * 64;
  203. for (i = 0; i < 4; i++) s_vars[i] *= 1 / 15.f * 16;
  204. for (i = 0; i < 4; i++) d_vars[i] *= 1 / 15.f * 16;
  205. if (s_gvar > 0)
  206. s_gvar = (s_vars[0] + s_vars[1] + s_vars[2] + s_vars[3]) / s_gvar;
  207. if (d_gvar > 0)
  208. d_gvar = (d_vars[0] + d_vars[1] + d_vars[2] + d_vars[3]) / d_gvar;
  209. #if CONFIG_VP9_HIGHBITDEPTH
  210. if (bit_depth == 10 || bit_depth == 12) {
  211. hbd_od_bin_fdct8x8(dct_s_coef, 8, dct_s, 8);
  212. hbd_od_bin_fdct8x8(dct_d_coef, 8, dct_d, 8);
  213. }
  214. #endif
  215. if (bit_depth == 8) {
  216. od_bin_fdct8x8(dct_s_coef, 8, dct_s, 8);
  217. od_bin_fdct8x8(dct_d_coef, 8, dct_d, 8);
  218. }
  219. for (i = 0; i < 8; i++)
  220. for (j = (i == 0); j < 8; j++)
  221. s_mask += dct_s_coef[i * 8 + j] * dct_s_coef[i * 8 + j] * mask[i][j];
  222. for (i = 0; i < 8; i++)
  223. for (j = (i == 0); j < 8; j++)
  224. d_mask += dct_d_coef[i * 8 + j] * dct_d_coef[i * 8 + j] * mask[i][j];
  225. s_mask = sqrt(s_mask * s_gvar) / 32.f;
  226. d_mask = sqrt(d_mask * d_gvar) / 32.f;
  227. if (d_mask > s_mask) s_mask = d_mask;
  228. for (i = 0; i < 8; i++) {
  229. for (j = 0; j < 8; j++) {
  230. double err;
  231. err = fabs((double)(dct_s_coef[i * 8 + j] - dct_d_coef[i * 8 + j]));
  232. if (i != 0 || j != 0)
  233. err = err < s_mask / mask[i][j] ? 0 : err - s_mask / mask[i][j];
  234. ret += (err * _csf[i][j]) * (err * _csf[i][j]);
  235. pixels++;
  236. }
  237. }
  238. }
  239. }
  240. if (pixels <= 0) return 0;
  241. ret /= pixels;
  242. return ret;
  243. }
  244. double vpx_psnrhvs(const YV12_BUFFER_CONFIG *src,
  245. const YV12_BUFFER_CONFIG *dest, double *y_psnrhvs,
  246. double *u_psnrhvs, double *v_psnrhvs, uint32_t bd,
  247. uint32_t in_bd) {
  248. double psnrhvs;
  249. const double par = 1.0;
  250. const int step = 7;
  251. uint32_t bd_shift = 0;
  252. vpx_clear_system_state();
  253. assert(bd == 8 || bd == 10 || bd == 12);
  254. assert(bd >= in_bd);
  255. bd_shift = bd - in_bd;
  256. *y_psnrhvs = calc_psnrhvs(src->y_buffer, src->y_stride, dest->y_buffer,
  257. dest->y_stride, par, src->y_crop_width,
  258. src->y_crop_height, step, csf_y, bd, bd_shift);
  259. *u_psnrhvs = calc_psnrhvs(src->u_buffer, src->uv_stride, dest->u_buffer,
  260. dest->uv_stride, par, src->uv_crop_width,
  261. src->uv_crop_height, step, csf_cb420, bd, bd_shift);
  262. *v_psnrhvs = calc_psnrhvs(src->v_buffer, src->uv_stride, dest->v_buffer,
  263. dest->uv_stride, par, src->uv_crop_width,
  264. src->uv_crop_height, step, csf_cr420, bd, bd_shift);
  265. psnrhvs = (*y_psnrhvs) * .8 + .1 * ((*u_psnrhvs) + (*v_psnrhvs));
  266. return convert_score_db(psnrhvs, 1.0, in_bd);
  267. }