picture_psnr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // WebPPicture tools for measuring distortion
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <math.h>
  14. #include "./vp8enci.h"
  15. //------------------------------------------------------------------------------
  16. // local-min distortion
  17. //
  18. // For every pixel in the *reference* picture, we search for the local best
  19. // match in the compressed image. This is not a symmetrical measure.
  20. #define RADIUS 2 // search radius. Shouldn't be too large.
  21. static float AccumulateLSIM(const uint8_t* src, int src_stride,
  22. const uint8_t* ref, int ref_stride,
  23. int w, int h) {
  24. int x, y;
  25. double total_sse = 0.;
  26. for (y = 0; y < h; ++y) {
  27. const int y_0 = (y - RADIUS < 0) ? 0 : y - RADIUS;
  28. const int y_1 = (y + RADIUS + 1 >= h) ? h : y + RADIUS + 1;
  29. for (x = 0; x < w; ++x) {
  30. const int x_0 = (x - RADIUS < 0) ? 0 : x - RADIUS;
  31. const int x_1 = (x + RADIUS + 1 >= w) ? w : x + RADIUS + 1;
  32. double best_sse = 255. * 255.;
  33. const double value = (double)ref[y * ref_stride + x];
  34. int i, j;
  35. for (j = y_0; j < y_1; ++j) {
  36. const uint8_t* s = src + j * src_stride;
  37. for (i = x_0; i < x_1; ++i) {
  38. const double sse = (double)(s[i] - value) * (s[i] - value);
  39. if (sse < best_sse) best_sse = sse;
  40. }
  41. }
  42. total_sse += best_sse;
  43. }
  44. }
  45. return (float)total_sse;
  46. }
  47. #undef RADIUS
  48. //------------------------------------------------------------------------------
  49. // Distortion
  50. // Max value returned in case of exact similarity.
  51. static const double kMinDistortion_dB = 99.;
  52. static float GetPSNR(const double v) {
  53. return (float)((v > 0.) ? -4.3429448 * log(v / (255 * 255.))
  54. : kMinDistortion_dB);
  55. }
  56. int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
  57. int type, float result[5]) {
  58. DistoStats stats[5];
  59. int has_alpha;
  60. int uv_w, uv_h;
  61. if (src == NULL || ref == NULL ||
  62. src->width != ref->width || src->height != ref->height ||
  63. src->y == NULL || ref->y == NULL ||
  64. src->u == NULL || ref->u == NULL ||
  65. src->v == NULL || ref->v == NULL ||
  66. result == NULL) {
  67. return 0;
  68. }
  69. // TODO(skal): provide distortion for ARGB too.
  70. if (src->use_argb == 1 || src->use_argb != ref->use_argb) {
  71. return 0;
  72. }
  73. has_alpha = !!(src->colorspace & WEBP_CSP_ALPHA_BIT);
  74. if (has_alpha != !!(ref->colorspace & WEBP_CSP_ALPHA_BIT) ||
  75. (has_alpha && (src->a == NULL || ref->a == NULL))) {
  76. return 0;
  77. }
  78. memset(stats, 0, sizeof(stats));
  79. uv_w = (src->width + 1) >> 1;
  80. uv_h = (src->height + 1) >> 1;
  81. if (type >= 2) {
  82. float sse[4];
  83. sse[0] = AccumulateLSIM(src->y, src->y_stride,
  84. ref->y, ref->y_stride, src->width, src->height);
  85. sse[1] = AccumulateLSIM(src->u, src->uv_stride,
  86. ref->u, ref->uv_stride, uv_w, uv_h);
  87. sse[2] = AccumulateLSIM(src->v, src->uv_stride,
  88. ref->v, ref->uv_stride, uv_w, uv_h);
  89. sse[3] = has_alpha ? AccumulateLSIM(src->a, src->a_stride,
  90. ref->a, ref->a_stride,
  91. src->width, src->height)
  92. : 0.f;
  93. result[0] = GetPSNR(sse[0] / (src->width * src->height));
  94. result[1] = GetPSNR(sse[1] / (uv_w * uv_h));
  95. result[2] = GetPSNR(sse[2] / (uv_w * uv_h));
  96. result[3] = GetPSNR(sse[3] / (src->width * src->height));
  97. {
  98. double total_sse = sse[0] + sse[1] + sse[2];
  99. int total_pixels = src->width * src->height + 2 * uv_w * uv_h;
  100. if (has_alpha) {
  101. total_pixels += src->width * src->height;
  102. total_sse += sse[3];
  103. }
  104. result[4] = GetPSNR(total_sse / total_pixels);
  105. }
  106. } else {
  107. int c;
  108. VP8SSIMAccumulatePlane(src->y, src->y_stride,
  109. ref->y, ref->y_stride,
  110. src->width, src->height, &stats[0]);
  111. VP8SSIMAccumulatePlane(src->u, src->uv_stride,
  112. ref->u, ref->uv_stride,
  113. uv_w, uv_h, &stats[1]);
  114. VP8SSIMAccumulatePlane(src->v, src->uv_stride,
  115. ref->v, ref->uv_stride,
  116. uv_w, uv_h, &stats[2]);
  117. if (has_alpha) {
  118. VP8SSIMAccumulatePlane(src->a, src->a_stride,
  119. ref->a, ref->a_stride,
  120. src->width, src->height, &stats[3]);
  121. }
  122. for (c = 0; c <= 4; ++c) {
  123. if (type == 1) {
  124. const double v = VP8SSIMGet(&stats[c]);
  125. result[c] = (float)((v < 1.) ? -10.0 * log10(1. - v)
  126. : kMinDistortion_dB);
  127. } else {
  128. const double v = VP8SSIMGetSquaredError(&stats[c]);
  129. result[c] = GetPSNR(v);
  130. }
  131. // Accumulate forward
  132. if (c < 4) VP8SSIMAddStats(&stats[c], &stats[4]);
  133. }
  134. }
  135. return 1;
  136. }
  137. //------------------------------------------------------------------------------