• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "config/av1_rtcd.h"
13 #include "config/aom_config.h"
14 #include "config/aom_dsp_rtcd.h"
15 
16 #include "av1/common/common.h"
17 #include "av1/common/filter.h"
18 #include "aom/aom_integer.h"
19 #include "aom_dsp/aom_filter.h"
20 #include "aom_ports/mem.h"
21 #include "aom_ports/system_state.h"
22 
horizontal_filter(const uint8_t * s)23 static int horizontal_filter(const uint8_t *s) {
24   return (s[1] - s[-2]) * 2 + (s[-1] - s[0]) * 6;
25 }
26 
vertical_filter(const uint8_t * s,int p)27 static int vertical_filter(const uint8_t *s, int p) {
28   return (s[p] - s[-2 * p]) * 2 + (s[-p] - s[0]) * 6;
29 }
30 
variance(int sum,int sum_squared,int size)31 static int variance(int sum, int sum_squared, int size) {
32   return sum_squared / size - (sum / size) * (sum / size);
33 }
34 // Calculate a blockiness level for a vertical block edge.
35 // This function returns a new blockiness metric that's defined as
36 
37 //              p0 p1 p2 p3
38 //              q0 q1 q2 q3
39 // block edge ->
40 //              r0 r1 r2 r3
41 //              s0 s1 s2 s3
42 
43 // blockiness =  p0*-2+q0*6+r0*-6+s0*2 +
44 //               p1*-2+q1*6+r1*-6+s1*2 +
45 //               p2*-2+q2*6+r2*-6+s2*2 +
46 //               p3*-2+q3*6+r3*-6+s3*2 ;
47 
48 // reconstructed_blockiness = abs(blockiness from reconstructed buffer -
49 //                                blockiness from source buffer,0)
50 //
51 // I make the assumption that flat blocks are much more visible than high
52 // contrast blocks. As such, I scale the result of the blockiness calc
53 // by dividing the blockiness by the variance of the pixels on either side
54 // of the edge as follows:
55 // var_0 = (q0^2+q1^2+q2^2+q3^2) - ((q0 + q1 + q2 + q3) / 4 )^2
56 // var_1 = (r0^2+r1^2+r2^2+r3^2) - ((r0 + r1 + r2 + r3) / 4 )^2
57 // The returned blockiness is the scaled value
58 // Reconstructed blockiness / ( 1 + var_0 + var_1 ) ;
blockiness_vertical(const uint8_t * s,int sp,const uint8_t * r,int rp,int size)59 static int blockiness_vertical(const uint8_t *s, int sp, const uint8_t *r,
60                                int rp, int size) {
61   int s_blockiness = 0;
62   int r_blockiness = 0;
63   int sum_0 = 0;
64   int sum_sq_0 = 0;
65   int sum_1 = 0;
66   int sum_sq_1 = 0;
67   int i;
68   int var_0;
69   int var_1;
70   for (i = 0; i < size; ++i, s += sp, r += rp) {
71     s_blockiness += horizontal_filter(s);
72     r_blockiness += horizontal_filter(r);
73     sum_0 += s[0];
74     sum_sq_0 += s[0] * s[0];
75     sum_1 += s[-1];
76     sum_sq_1 += s[-1] * s[-1];
77   }
78   var_0 = variance(sum_0, sum_sq_0, size);
79   var_1 = variance(sum_1, sum_sq_1, size);
80   r_blockiness = abs(r_blockiness);
81   s_blockiness = abs(s_blockiness);
82 
83   if (r_blockiness > s_blockiness)
84     return (r_blockiness - s_blockiness) / (1 + var_0 + var_1);
85   else
86     return 0;
87 }
88 
89 // Calculate a blockiness level for a horizontal block edge
90 // same as above.
blockiness_horizontal(const uint8_t * s,int sp,const uint8_t * r,int rp,int size)91 static int blockiness_horizontal(const uint8_t *s, int sp, const uint8_t *r,
92                                  int rp, int size) {
93   int s_blockiness = 0;
94   int r_blockiness = 0;
95   int sum_0 = 0;
96   int sum_sq_0 = 0;
97   int sum_1 = 0;
98   int sum_sq_1 = 0;
99   int i;
100   int var_0;
101   int var_1;
102   for (i = 0; i < size; ++i, ++s, ++r) {
103     s_blockiness += vertical_filter(s, sp);
104     r_blockiness += vertical_filter(r, rp);
105     sum_0 += s[0];
106     sum_sq_0 += s[0] * s[0];
107     sum_1 += s[-sp];
108     sum_sq_1 += s[-sp] * s[-sp];
109   }
110   var_0 = variance(sum_0, sum_sq_0, size);
111   var_1 = variance(sum_1, sum_sq_1, size);
112   r_blockiness = abs(r_blockiness);
113   s_blockiness = abs(s_blockiness);
114 
115   if (r_blockiness > s_blockiness)
116     return (r_blockiness - s_blockiness) / (1 + var_0 + var_1);
117   else
118     return 0;
119 }
120 
121 // This function returns the blockiness for the entire frame currently by
122 // looking at all borders in steps of 4.
av1_get_blockiness(const unsigned char * img1,int img1_pitch,const unsigned char * img2,int img2_pitch,int width,int height)123 double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
124                           const unsigned char *img2, int img2_pitch, int width,
125                           int height) {
126   double blockiness = 0;
127   int i, j;
128   aom_clear_system_state();
129   for (i = 0; i < height;
130        i += 4, img1 += img1_pitch * 4, img2 += img2_pitch * 4) {
131     for (j = 0; j < width; j += 4) {
132       if (i > 0 && i < height && j > 0 && j < width) {
133         blockiness +=
134             blockiness_vertical(img1 + j, img1_pitch, img2 + j, img2_pitch, 4);
135         blockiness += blockiness_horizontal(img1 + j, img1_pitch, img2 + j,
136                                             img2_pitch, 4);
137       }
138     }
139   }
140   blockiness /= width * height / 16;
141   return blockiness;
142 }
143