vp9_blockiness.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2014 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 "./vpx_config.h"
  11. #include "./vp9_rtcd.h"
  12. #include "vp9/common/vp9_common.h"
  13. #include "vp9/common/vp9_convolve.h"
  14. #include "vp9/common/vp9_filter.h"
  15. #include "vpx/vpx_integer.h"
  16. #include "vpx_ports/mem.h"
  17. static int horizontal_filter(const uint8_t *s) {
  18. return (s[1] - s[-2]) * 2 + (s[-1] - s[0]) * 6;
  19. }
  20. static int vertical_filter(const uint8_t *s, int p) {
  21. return (s[p] - s[-2 * p]) * 2 + (s[-p] - s[0]) * 6;
  22. }
  23. static int variance(int sum, int sum_squared, int size) {
  24. return sum_squared / size - (sum / size) * (sum / size);
  25. }
  26. // Calculate a blockiness level for a vertical block edge.
  27. // This function returns a new blockiness metric that's defined as
  28. // p0 p1 p2 p3
  29. // q0 q1 q2 q3
  30. // block edge ->
  31. // r0 r1 r2 r3
  32. // s0 s1 s2 s3
  33. // blockiness = p0*-2+q0*6+r0*-6+s0*2 +
  34. // p1*-2+q1*6+r1*-6+s1*2 +
  35. // p2*-2+q2*6+r2*-6+s2*2 +
  36. // p3*-2+q3*6+r3*-6+s3*2 ;
  37. // reconstructed_blockiness = abs(blockiness from reconstructed buffer -
  38. // blockiness from source buffer,0)
  39. //
  40. // I make the assumption that flat blocks are much more visible than high
  41. // contrast blocks. As such, I scale the result of the blockiness calc
  42. // by dividing the blockiness by the variance of the pixels on either side
  43. // of the edge as follows:
  44. // var_0 = (q0^2+q1^2+q2^2+q3^2) - ((q0 + q1 + q2 + q3) / 4 )^2
  45. // var_1 = (r0^2+r1^2+r2^2+r3^2) - ((r0 + r1 + r2 + r3) / 4 )^2
  46. // The returned blockiness is the scaled value
  47. // Reconstructed blockiness / ( 1 + var_0 + var_1 ) ;
  48. int blockiness_vertical(const uint8_t *s, int sp, const uint8_t *r, int rp,
  49. int size) {
  50. int s_blockiness = 0;
  51. int r_blockiness = 0;
  52. int sum_0 = 0;
  53. int sum_sq_0 = 0;
  54. int sum_1 = 0;
  55. int sum_sq_1 = 0;
  56. int i;
  57. int var_0;
  58. int var_1;
  59. for (i = 0; i < size; ++i, s += sp, r += rp) {
  60. s_blockiness += horizontal_filter(s);
  61. r_blockiness += horizontal_filter(r);
  62. sum_0 += s[0];
  63. sum_sq_0 += s[0]*s[0];
  64. sum_1 += s[-1];
  65. sum_sq_1 += s[-1]*s[-1];
  66. }
  67. var_0 = variance(sum_0, sum_sq_0, size);
  68. var_1 = variance(sum_1, sum_sq_1, size);
  69. r_blockiness = abs(r_blockiness);
  70. s_blockiness = abs(s_blockiness);
  71. if (r_blockiness > s_blockiness)
  72. return (r_blockiness - s_blockiness) / (1 + var_0 + var_1);
  73. else
  74. return 0;
  75. }
  76. // Calculate a blockiness level for a horizontal block edge
  77. // same as above.
  78. int blockiness_horizontal(const uint8_t *s, int sp, const uint8_t *r, int rp,
  79. int size) {
  80. int s_blockiness = 0;
  81. int r_blockiness = 0;
  82. int sum_0 = 0;
  83. int sum_sq_0 = 0;
  84. int sum_1 = 0;
  85. int sum_sq_1 = 0;
  86. int i;
  87. int var_0;
  88. int var_1;
  89. for (i = 0; i < size; ++i, ++s, ++r) {
  90. s_blockiness += vertical_filter(s, sp);
  91. r_blockiness += vertical_filter(r, rp);
  92. sum_0 += s[0];
  93. sum_sq_0 += s[0] * s[0];
  94. sum_1 += s[-sp];
  95. sum_sq_1 += s[-sp] * s[-sp];
  96. }
  97. var_0 = variance(sum_0, sum_sq_0, size);
  98. var_1 = variance(sum_1, sum_sq_1, size);
  99. r_blockiness = abs(r_blockiness);
  100. s_blockiness = abs(s_blockiness);
  101. if (r_blockiness > s_blockiness)
  102. return (r_blockiness - s_blockiness) / (1 + var_0 + var_1);
  103. else
  104. return 0;
  105. }
  106. // This function returns the blockiness for the entire frame currently by
  107. // looking at all borders in steps of 4.
  108. double vp9_get_blockiness(const unsigned char *img1, int img1_pitch,
  109. const unsigned char *img2, int img2_pitch,
  110. int width, int height ) {
  111. double blockiness = 0;
  112. int i, j;
  113. vp9_clear_system_state();
  114. for (i = 0; i < height; i += 4, img1 += img1_pitch * 4,
  115. img2 += img2_pitch * 4) {
  116. for (j = 0; j < width; j += 4) {
  117. if (i > 0 && i < height && j > 0 && j < width) {
  118. blockiness += blockiness_vertical(img1 + j, img1_pitch,
  119. img2 + j, img2_pitch, 4);
  120. blockiness += blockiness_horizontal(img1 + j, img1_pitch,
  121. img2 + j, img2_pitch, 4);
  122. }
  123. }
  124. }
  125. blockiness /= width * height / 16;
  126. return blockiness;
  127. }