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