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