vp9_blockiness.c 4.3 KB

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