2
0

psnr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2016 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 <math.h>
  11. #include <assert.h>
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx_dsp/psnr.h"
  14. #include "vpx_scale/yv12config.h"
  15. double vpx_sse_to_psnr(double samples, double peak, double sse) {
  16. if (sse > 0.0) {
  17. const double psnr = 10.0 * log10(samples * peak * peak / sse);
  18. return psnr > MAX_PSNR ? MAX_PSNR : psnr;
  19. } else {
  20. return MAX_PSNR;
  21. }
  22. }
  23. /* TODO(yaowu): The block_variance calls the unoptimized versions of variance()
  24. * and highbd_8_variance(). It should not.
  25. */
  26. static void encoder_variance(const uint8_t *a, int a_stride, const uint8_t *b,
  27. int b_stride, int w, int h, unsigned int *sse,
  28. int *sum) {
  29. int i, j;
  30. *sum = 0;
  31. *sse = 0;
  32. for (i = 0; i < h; i++) {
  33. for (j = 0; j < w; j++) {
  34. const int diff = a[j] - b[j];
  35. *sum += diff;
  36. *sse += diff * diff;
  37. }
  38. a += a_stride;
  39. b += b_stride;
  40. }
  41. }
  42. #if CONFIG_VP9_HIGHBITDEPTH
  43. static void encoder_highbd_variance64(const uint8_t *a8, int a_stride,
  44. const uint8_t *b8, int b_stride, int w,
  45. int h, uint64_t *sse, int64_t *sum) {
  46. int i, j;
  47. uint16_t *a = CONVERT_TO_SHORTPTR(a8);
  48. uint16_t *b = CONVERT_TO_SHORTPTR(b8);
  49. *sum = 0;
  50. *sse = 0;
  51. for (i = 0; i < h; i++) {
  52. for (j = 0; j < w; j++) {
  53. const int diff = a[j] - b[j];
  54. *sum += diff;
  55. *sse += diff * diff;
  56. }
  57. a += a_stride;
  58. b += b_stride;
  59. }
  60. }
  61. static void encoder_highbd_8_variance(const uint8_t *a8, int a_stride,
  62. const uint8_t *b8, int b_stride, int w,
  63. int h, unsigned int *sse, int *sum) {
  64. uint64_t sse_long = 0;
  65. int64_t sum_long = 0;
  66. encoder_highbd_variance64(a8, a_stride, b8, b_stride, w, h, &sse_long,
  67. &sum_long);
  68. *sse = (unsigned int)sse_long;
  69. *sum = (int)sum_long;
  70. }
  71. #endif // CONFIG_VP9_HIGHBITDEPTH
  72. static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
  73. int b_stride, int width, int height) {
  74. const int dw = width % 16;
  75. const int dh = height % 16;
  76. int64_t total_sse = 0;
  77. unsigned int sse = 0;
  78. int sum = 0;
  79. int x, y;
  80. if (dw > 0) {
  81. encoder_variance(&a[width - dw], a_stride, &b[width - dw], b_stride, dw,
  82. height, &sse, &sum);
  83. total_sse += sse;
  84. }
  85. if (dh > 0) {
  86. encoder_variance(&a[(height - dh) * a_stride], a_stride,
  87. &b[(height - dh) * b_stride], b_stride, width - dw, dh,
  88. &sse, &sum);
  89. total_sse += sse;
  90. }
  91. for (y = 0; y < height / 16; ++y) {
  92. const uint8_t *pa = a;
  93. const uint8_t *pb = b;
  94. for (x = 0; x < width / 16; ++x) {
  95. vpx_mse16x16(pa, a_stride, pb, b_stride, &sse);
  96. total_sse += sse;
  97. pa += 16;
  98. pb += 16;
  99. }
  100. a += 16 * a_stride;
  101. b += 16 * b_stride;
  102. }
  103. return total_sse;
  104. }
  105. #if CONFIG_VP9_HIGHBITDEPTH
  106. static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
  107. const uint8_t *b8, int b_stride, int width,
  108. int height, unsigned int input_shift) {
  109. const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
  110. const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
  111. int64_t total_sse = 0;
  112. int x, y;
  113. for (y = 0; y < height; ++y) {
  114. for (x = 0; x < width; ++x) {
  115. int64_t diff;
  116. diff = (a[x] >> input_shift) - (b[x] >> input_shift);
  117. total_sse += diff * diff;
  118. }
  119. a += a_stride;
  120. b += b_stride;
  121. }
  122. return total_sse;
  123. }
  124. static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
  125. int b_stride, int width, int height) {
  126. int64_t total_sse = 0;
  127. int x, y;
  128. const int dw = width % 16;
  129. const int dh = height % 16;
  130. unsigned int sse = 0;
  131. int sum = 0;
  132. if (dw > 0) {
  133. encoder_highbd_8_variance(&a[width - dw], a_stride, &b[width - dw],
  134. b_stride, dw, height, &sse, &sum);
  135. total_sse += sse;
  136. }
  137. if (dh > 0) {
  138. encoder_highbd_8_variance(&a[(height - dh) * a_stride], a_stride,
  139. &b[(height - dh) * b_stride], b_stride,
  140. width - dw, dh, &sse, &sum);
  141. total_sse += sse;
  142. }
  143. for (y = 0; y < height / 16; ++y) {
  144. const uint8_t *pa = a;
  145. const uint8_t *pb = b;
  146. for (x = 0; x < width / 16; ++x) {
  147. vpx_highbd_8_mse16x16(pa, a_stride, pb, b_stride, &sse);
  148. total_sse += sse;
  149. pa += 16;
  150. pb += 16;
  151. }
  152. a += 16 * a_stride;
  153. b += 16 * b_stride;
  154. }
  155. return total_sse;
  156. }
  157. #endif // CONFIG_VP9_HIGHBITDEPTH
  158. int64_t vpx_get_y_sse(const YV12_BUFFER_CONFIG *a,
  159. const YV12_BUFFER_CONFIG *b) {
  160. assert(a->y_crop_width == b->y_crop_width);
  161. assert(a->y_crop_height == b->y_crop_height);
  162. return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
  163. a->y_crop_width, a->y_crop_height);
  164. }
  165. #if CONFIG_VP9_HIGHBITDEPTH
  166. int64_t vpx_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
  167. const YV12_BUFFER_CONFIG *b) {
  168. assert(a->y_crop_width == b->y_crop_width);
  169. assert(a->y_crop_height == b->y_crop_height);
  170. assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
  171. assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
  172. return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
  173. a->y_crop_width, a->y_crop_height);
  174. }
  175. #endif // CONFIG_VP9_HIGHBITDEPTH
  176. #if CONFIG_VP9_HIGHBITDEPTH
  177. void vpx_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
  178. const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
  179. uint32_t bit_depth, uint32_t in_bit_depth) {
  180. const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
  181. const int heights[3] = { a->y_crop_height, a->uv_crop_height,
  182. a->uv_crop_height };
  183. const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
  184. const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
  185. const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
  186. const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
  187. int i;
  188. uint64_t total_sse = 0;
  189. uint32_t total_samples = 0;
  190. const double peak = (double)((1 << in_bit_depth) - 1);
  191. const unsigned int input_shift = bit_depth - in_bit_depth;
  192. for (i = 0; i < 3; ++i) {
  193. const int w = widths[i];
  194. const int h = heights[i];
  195. const uint32_t samples = w * h;
  196. uint64_t sse;
  197. if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
  198. if (input_shift) {
  199. sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i],
  200. b_strides[i], w, h, input_shift);
  201. } else {
  202. sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i],
  203. b_strides[i], w, h);
  204. }
  205. } else {
  206. sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
  207. }
  208. psnr->sse[1 + i] = sse;
  209. psnr->samples[1 + i] = samples;
  210. psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
  211. total_sse += sse;
  212. total_samples += samples;
  213. }
  214. psnr->sse[0] = total_sse;
  215. psnr->samples[0] = total_samples;
  216. psnr->psnr[0] =
  217. vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
  218. }
  219. #endif // !CONFIG_VP9_HIGHBITDEPTH
  220. void vpx_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
  221. PSNR_STATS *psnr) {
  222. static const double peak = 255.0;
  223. const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
  224. const int heights[3] = { a->y_crop_height, a->uv_crop_height,
  225. a->uv_crop_height };
  226. const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
  227. const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
  228. const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
  229. const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
  230. int i;
  231. uint64_t total_sse = 0;
  232. uint32_t total_samples = 0;
  233. for (i = 0; i < 3; ++i) {
  234. const int w = widths[i];
  235. const int h = heights[i];
  236. const uint32_t samples = w * h;
  237. const uint64_t sse =
  238. get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
  239. psnr->sse[1 + i] = sse;
  240. psnr->samples[1 + i] = samples;
  241. psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
  242. total_sse += sse;
  243. total_samples += samples;
  244. }
  245. psnr->sse[0] = total_sse;
  246. psnr->samples[0] = total_samples;
  247. psnr->psnr[0] =
  248. vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
  249. }