1 /*
2  *  Copyright (c) 2010 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/common/vp9_common.h"
13 #include "vp9/common/vp9_loopfilter.h"
14 #include "vp9/common/vp9_onyxc_int.h"
15 
signed_char_clamp(int t)16 static INLINE int8_t signed_char_clamp(int t) {
17   return (int8_t)clamp(t, -128, 127);
18 }
19 
20 // should we apply any filter at all: 11111111 yes, 00000000 no
filter_mask(uint8_t limit,uint8_t blimit,uint8_t p3,uint8_t p2,uint8_t p1,uint8_t p0,uint8_t q0,uint8_t q1,uint8_t q2,uint8_t q3)21 static INLINE int8_t filter_mask(uint8_t limit, uint8_t blimit,
22                                  uint8_t p3, uint8_t p2,
23                                  uint8_t p1, uint8_t p0,
24                                  uint8_t q0, uint8_t q1,
25                                  uint8_t q2, uint8_t q3) {
26   int8_t mask = 0;
27   mask |= (abs(p3 - p2) > limit) * -1;
28   mask |= (abs(p2 - p1) > limit) * -1;
29   mask |= (abs(p1 - p0) > limit) * -1;
30   mask |= (abs(q1 - q0) > limit) * -1;
31   mask |= (abs(q2 - q1) > limit) * -1;
32   mask |= (abs(q3 - q2) > limit) * -1;
33   mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2  > blimit) * -1;
34   return ~mask;
35 }
36 
flat_mask4(uint8_t thresh,uint8_t p3,uint8_t p2,uint8_t p1,uint8_t p0,uint8_t q0,uint8_t q1,uint8_t q2,uint8_t q3)37 static INLINE int8_t flat_mask4(uint8_t thresh,
38                                 uint8_t p3, uint8_t p2,
39                                 uint8_t p1, uint8_t p0,
40                                 uint8_t q0, uint8_t q1,
41                                 uint8_t q2, uint8_t q3) {
42   int8_t mask = 0;
43   mask |= (abs(p1 - p0) > thresh) * -1;
44   mask |= (abs(q1 - q0) > thresh) * -1;
45   mask |= (abs(p2 - p0) > thresh) * -1;
46   mask |= (abs(q2 - q0) > thresh) * -1;
47   mask |= (abs(p3 - p0) > thresh) * -1;
48   mask |= (abs(q3 - q0) > thresh) * -1;
49   return ~mask;
50 }
51 
flat_mask5(uint8_t thresh,uint8_t p4,uint8_t p3,uint8_t p2,uint8_t p1,uint8_t p0,uint8_t q0,uint8_t q1,uint8_t q2,uint8_t q3,uint8_t q4)52 static INLINE int8_t flat_mask5(uint8_t thresh,
53                                 uint8_t p4, uint8_t p3,
54                                 uint8_t p2, uint8_t p1,
55                                 uint8_t p0, uint8_t q0,
56                                 uint8_t q1, uint8_t q2,
57                                 uint8_t q3, uint8_t q4) {
58   int8_t mask = ~flat_mask4(thresh, p3, p2, p1, p0, q0, q1, q2, q3);
59   mask |= (abs(p4 - p0) > thresh) * -1;
60   mask |= (abs(q4 - q0) > thresh) * -1;
61   return ~mask;
62 }
63 
64 // is there high edge variance internal edge: 11111111 yes, 00000000 no
hev_mask(uint8_t thresh,uint8_t p1,uint8_t p0,uint8_t q0,uint8_t q1)65 static INLINE int8_t hev_mask(uint8_t thresh, uint8_t p1, uint8_t p0,
66                               uint8_t q0, uint8_t q1) {
67   int8_t hev = 0;
68   hev  |= (abs(p1 - p0) > thresh) * -1;
69   hev  |= (abs(q1 - q0) > thresh) * -1;
70   return hev;
71 }
72 
filter4(int8_t mask,uint8_t thresh,uint8_t * op1,uint8_t * op0,uint8_t * oq0,uint8_t * oq1)73 static INLINE void filter4(int8_t mask, uint8_t thresh, uint8_t *op1,
74                            uint8_t *op0, uint8_t *oq0, uint8_t *oq1) {
75   int8_t filter1, filter2;
76 
77   const int8_t ps1 = (int8_t) *op1 ^ 0x80;
78   const int8_t ps0 = (int8_t) *op0 ^ 0x80;
79   const int8_t qs0 = (int8_t) *oq0 ^ 0x80;
80   const int8_t qs1 = (int8_t) *oq1 ^ 0x80;
81   const uint8_t hev = hev_mask(thresh, *op1, *op0, *oq0, *oq1);
82 
83   // add outer taps if we have high edge variance
84   int8_t filter = signed_char_clamp(ps1 - qs1) & hev;
85 
86   // inner taps
87   filter = signed_char_clamp(filter + 3 * (qs0 - ps0)) & mask;
88 
89   // save bottom 3 bits so that we round one side +4 and the other +3
90   // if it equals 4 we'll set to adjust by -1 to account for the fact
91   // we'd round 3 the other way
92   filter1 = signed_char_clamp(filter + 4) >> 3;
93   filter2 = signed_char_clamp(filter + 3) >> 3;
94 
95   *oq0 = signed_char_clamp(qs0 - filter1) ^ 0x80;
96   *op0 = signed_char_clamp(ps0 + filter2) ^ 0x80;
97 
98   // outer tap adjustments
99   filter = ROUND_POWER_OF_TWO(filter1, 1) & ~hev;
100 
101   *oq1 = signed_char_clamp(qs1 - filter) ^ 0x80;
102   *op1 = signed_char_clamp(ps1 + filter) ^ 0x80;
103 }
104 
vp9_lpf_horizontal_4_c(uint8_t * s,int p,const uint8_t * blimit,const uint8_t * limit,const uint8_t * thresh,int count)105 void vp9_lpf_horizontal_4_c(uint8_t *s, int p /* pitch */,
106                             const uint8_t *blimit, const uint8_t *limit,
107                             const uint8_t *thresh, int count) {
108   int i;
109 
110   // loop filter designed to work using chars so that we can make maximum use
111   // of 8 bit simd instructions.
112   for (i = 0; i < 8 * count; ++i) {
113     const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
114     const uint8_t q0 = s[0 * p],  q1 = s[1 * p],  q2 = s[2 * p],  q3 = s[3 * p];
115     const int8_t mask = filter_mask(*limit, *blimit,
116                                     p3, p2, p1, p0, q0, q1, q2, q3);
117     filter4(mask, *thresh, s - 2 * p, s - 1 * p, s, s + 1 * p);
118     ++s;
119   }
120 }
121 
vp9_lpf_horizontal_4_dual_c(uint8_t * s,int p,const uint8_t * blimit0,const uint8_t * limit0,const uint8_t * thresh0,const uint8_t * blimit1,const uint8_t * limit1,const uint8_t * thresh1)122 void vp9_lpf_horizontal_4_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
123                                  const uint8_t *limit0, const uint8_t *thresh0,
124                                  const uint8_t *blimit1, const uint8_t *limit1,
125                                  const uint8_t *thresh1) {
126   vp9_lpf_horizontal_4_c(s, p, blimit0, limit0, thresh0, 1);
127   vp9_lpf_horizontal_4_c(s + 8, p, blimit1, limit1, thresh1, 1);
128 }
129 
vp9_lpf_vertical_4_c(uint8_t * s,int pitch,const uint8_t * blimit,const uint8_t * limit,const uint8_t * thresh,int count)130 void vp9_lpf_vertical_4_c(uint8_t *s, int pitch, const uint8_t *blimit,
131                           const uint8_t *limit, const uint8_t *thresh,
132                           int count) {
133   int i;
134 
135   // loop filter designed to work using chars so that we can make maximum use
136   // of 8 bit simd instructions.
137   for (i = 0; i < 8 * count; ++i) {
138     const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
139     const uint8_t q0 = s[0],  q1 = s[1],  q2 = s[2],  q3 = s[3];
140     const int8_t mask = filter_mask(*limit, *blimit,
141                                     p3, p2, p1, p0, q0, q1, q2, q3);
142     filter4(mask, *thresh, s - 2, s - 1, s, s + 1);
143     s += pitch;
144   }
145 }
146 
vp9_lpf_vertical_4_dual_c(uint8_t * s,int pitch,const uint8_t * blimit0,const uint8_t * limit0,const uint8_t * thresh0,const uint8_t * blimit1,const uint8_t * limit1,const uint8_t * thresh1)147 void vp9_lpf_vertical_4_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
148                                const uint8_t *limit0, const uint8_t *thresh0,
149                                const uint8_t *blimit1, const uint8_t *limit1,
150                                const uint8_t *thresh1) {
151   vp9_lpf_vertical_4_c(s, pitch, blimit0, limit0, thresh0, 1);
152   vp9_lpf_vertical_4_c(s + 8 * pitch, pitch, blimit1, limit1,
153                                   thresh1, 1);
154 }
155 
filter8(int8_t mask,uint8_t thresh,uint8_t flat,uint8_t * op3,uint8_t * op2,uint8_t * op1,uint8_t * op0,uint8_t * oq0,uint8_t * oq1,uint8_t * oq2,uint8_t * oq3)156 static INLINE void filter8(int8_t mask, uint8_t thresh, uint8_t flat,
157                            uint8_t *op3, uint8_t *op2,
158                            uint8_t *op1, uint8_t *op0,
159                            uint8_t *oq0, uint8_t *oq1,
160                            uint8_t *oq2, uint8_t *oq3) {
161   if (flat && mask) {
162     const uint8_t p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
163     const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3;
164 
165     // 7-tap filter [1, 1, 1, 2, 1, 1, 1]
166     *op2 = ROUND_POWER_OF_TWO(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3);
167     *op1 = ROUND_POWER_OF_TWO(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3);
168     *op0 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3);
169     *oq0 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3);
170     *oq1 = ROUND_POWER_OF_TWO(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3);
171     *oq2 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3);
172   } else {
173     filter4(mask, thresh, op1,  op0, oq0, oq1);
174   }
175 }
176 
vp9_lpf_horizontal_8_c(uint8_t * s,int p,const uint8_t * blimit,const uint8_t * limit,const uint8_t * thresh,int count)177 void vp9_lpf_horizontal_8_c(uint8_t *s, int p, const uint8_t *blimit,
178                             const uint8_t *limit, const uint8_t *thresh,
179                             int count) {
180   int i;
181 
182   // loop filter designed to work using chars so that we can make maximum use
183   // of 8 bit simd instructions.
184   for (i = 0; i < 8 * count; ++i) {
185     const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
186     const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
187 
188     const int8_t mask = filter_mask(*limit, *blimit,
189                                     p3, p2, p1, p0, q0, q1, q2, q3);
190     const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
191     filter8(mask, *thresh, flat, s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p,
192                                  s,         s + 1 * p, s + 2 * p, s + 3 * p);
193     ++s;
194   }
195 }
196 
vp9_lpf_horizontal_8_dual_c(uint8_t * s,int p,const uint8_t * blimit0,const uint8_t * limit0,const uint8_t * thresh0,const uint8_t * blimit1,const uint8_t * limit1,const uint8_t * thresh1)197 void vp9_lpf_horizontal_8_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
198                                  const uint8_t *limit0, const uint8_t *thresh0,
199                                  const uint8_t *blimit1, const uint8_t *limit1,
200                                  const uint8_t *thresh1) {
201   vp9_lpf_horizontal_8_c(s, p, blimit0, limit0, thresh0, 1);
202   vp9_lpf_horizontal_8_c(s + 8, p, blimit1, limit1, thresh1, 1);
203 }
204 
vp9_lpf_vertical_8_c(uint8_t * s,int pitch,const uint8_t * blimit,const uint8_t * limit,const uint8_t * thresh,int count)205 void vp9_lpf_vertical_8_c(uint8_t *s, int pitch, const uint8_t *blimit,
206                           const uint8_t *limit, const uint8_t *thresh,
207                           int count) {
208   int i;
209 
210   for (i = 0; i < 8 * count; ++i) {
211     const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
212     const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
213     const int8_t mask = filter_mask(*limit, *blimit,
214                                     p3, p2, p1, p0, q0, q1, q2, q3);
215     const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
216     filter8(mask, *thresh, flat, s - 4, s - 3, s - 2, s - 1,
217                                  s,     s + 1, s + 2, s + 3);
218     s += pitch;
219   }
220 }
221 
vp9_lpf_vertical_8_dual_c(uint8_t * s,int pitch,const uint8_t * blimit0,const uint8_t * limit0,const uint8_t * thresh0,const uint8_t * blimit1,const uint8_t * limit1,const uint8_t * thresh1)222 void vp9_lpf_vertical_8_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
223                                const uint8_t *limit0, const uint8_t *thresh0,
224                                const uint8_t *blimit1, const uint8_t *limit1,
225                                const uint8_t *thresh1) {
226   vp9_lpf_vertical_8_c(s, pitch, blimit0, limit0, thresh0, 1);
227   vp9_lpf_vertical_8_c(s + 8 * pitch, pitch, blimit1, limit1,
228                                     thresh1, 1);
229 }
230 
filter16(int8_t mask,uint8_t thresh,uint8_t flat,uint8_t flat2,uint8_t * op7,uint8_t * op6,uint8_t * op5,uint8_t * op4,uint8_t * op3,uint8_t * op2,uint8_t * op1,uint8_t * op0,uint8_t * oq0,uint8_t * oq1,uint8_t * oq2,uint8_t * oq3,uint8_t * oq4,uint8_t * oq5,uint8_t * oq6,uint8_t * oq7)231 static INLINE void filter16(int8_t mask, uint8_t thresh,
232                             uint8_t flat, uint8_t flat2,
233                             uint8_t *op7, uint8_t *op6,
234                             uint8_t *op5, uint8_t *op4,
235                             uint8_t *op3, uint8_t *op2,
236                             uint8_t *op1, uint8_t *op0,
237                             uint8_t *oq0, uint8_t *oq1,
238                             uint8_t *oq2, uint8_t *oq3,
239                             uint8_t *oq4, uint8_t *oq5,
240                             uint8_t *oq6, uint8_t *oq7) {
241   if (flat2 && flat && mask) {
242     const uint8_t p7 = *op7, p6 = *op6, p5 = *op5, p4 = *op4,
243                   p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
244 
245     const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3,
246                   q4 = *oq4, q5 = *oq5, q6 = *oq6, q7 = *oq7;
247 
248     // 15-tap filter [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1]
249     *op6 = ROUND_POWER_OF_TWO(p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 +
250                               q0, 4);
251     *op5 = ROUND_POWER_OF_TWO(p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 +
252                               q0 + q1, 4);
253     *op4 = ROUND_POWER_OF_TWO(p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 +
254                               q0 + q1 + q2, 4);
255     *op3 = ROUND_POWER_OF_TWO(p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 +
256                               q0 + q1 + q2 + q3, 4);
257     *op2 = ROUND_POWER_OF_TWO(p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 +
258                               q0 + q1 + q2 + q3 + q4, 4);
259     *op1 = ROUND_POWER_OF_TWO(p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 +
260                               q0 + q1 + q2 + q3 + q4 + q5, 4);
261     *op0 = ROUND_POWER_OF_TWO(p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 +
262                               q0 + q1 + q2 + q3 + q4 + q5 + q6, 4);
263     *oq0 = ROUND_POWER_OF_TWO(p6 + p5 + p4 + p3 + p2 + p1 + p0 +
264                               q0 * 2 + q1 + q2 + q3 + q4 + q5 + q6 + q7, 4);
265     *oq1 = ROUND_POWER_OF_TWO(p5 + p4 + p3 + p2 + p1 + p0 +
266                               q0 + q1 * 2 + q2 + q3 + q4 + q5 + q6 + q7 * 2, 4);
267     *oq2 = ROUND_POWER_OF_TWO(p4 + p3 + p2 + p1 + p0 +
268                               q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3, 4);
269     *oq3 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + p0 +
270                               q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4, 4);
271     *oq4 = ROUND_POWER_OF_TWO(p2 + p1 + p0 +
272                               q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5, 4);
273     *oq5 = ROUND_POWER_OF_TWO(p1 + p0 +
274                               q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6, 4);
275     *oq6 = ROUND_POWER_OF_TWO(p0 +
276                               q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7, 4);
277   } else {
278     filter8(mask, thresh, flat, op3, op2, op1, op0, oq0, oq1, oq2, oq3);
279   }
280 }
281 
vp9_lpf_horizontal_16_c(uint8_t * s,int p,const uint8_t * blimit,const uint8_t * limit,const uint8_t * thresh,int count)282 void vp9_lpf_horizontal_16_c(uint8_t *s, int p, const uint8_t *blimit,
283                              const uint8_t *limit, const uint8_t *thresh,
284                              int count) {
285   int i;
286 
287   // loop filter designed to work using chars so that we can make maximum use
288   // of 8 bit simd instructions.
289   for (i = 0; i < 8 * count; ++i) {
290     const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
291     const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
292     const int8_t mask = filter_mask(*limit, *blimit,
293                                     p3, p2, p1, p0, q0, q1, q2, q3);
294     const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
295     const int8_t flat2 = flat_mask5(1,
296                              s[-8 * p], s[-7 * p], s[-6 * p], s[-5 * p], p0,
297                              q0, s[4 * p], s[5 * p], s[6 * p], s[7 * p]);
298 
299     filter16(mask, *thresh, flat, flat2,
300              s - 8 * p, s - 7 * p, s - 6 * p, s - 5 * p,
301              s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p,
302              s,         s + 1 * p, s + 2 * p, s + 3 * p,
303              s + 4 * p, s + 5 * p, s + 6 * p, s + 7 * p);
304     ++s;
305   }
306 }
307 
mb_lpf_vertical_edge_w(uint8_t * s,int p,const uint8_t * blimit,const uint8_t * limit,const uint8_t * thresh,int count)308 static void mb_lpf_vertical_edge_w(uint8_t *s, int p,
309                                    const uint8_t *blimit,
310                                    const uint8_t *limit,
311                                    const uint8_t *thresh,
312                                    int count) {
313   int i;
314 
315   for (i = 0; i < count; ++i) {
316     const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
317     const uint8_t q0 = s[0], q1 = s[1],  q2 = s[2], q3 = s[3];
318     const int8_t mask = filter_mask(*limit, *blimit,
319                                     p3, p2, p1, p0, q0, q1, q2, q3);
320     const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
321     const int8_t flat2 = flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0,
322                                     q0, s[4], s[5], s[6], s[7]);
323 
324     filter16(mask, *thresh, flat, flat2,
325              s - 8, s - 7, s - 6, s - 5, s - 4, s - 3, s - 2, s - 1,
326              s,     s + 1, s + 2, s + 3, s + 4, s + 5, s + 6, s + 7);
327     s += p;
328   }
329 }
330 
vp9_lpf_vertical_16_c(uint8_t * s,int p,const uint8_t * blimit,const uint8_t * limit,const uint8_t * thresh)331 void vp9_lpf_vertical_16_c(uint8_t *s, int p, const uint8_t *blimit,
332                            const uint8_t *limit, const uint8_t *thresh) {
333   mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 8);
334 }
335 
vp9_lpf_vertical_16_dual_c(uint8_t * s,int p,const uint8_t * blimit,const uint8_t * limit,const uint8_t * thresh)336 void vp9_lpf_vertical_16_dual_c(uint8_t *s, int p, const uint8_t *blimit,
337                                 const uint8_t *limit, const uint8_t *thresh) {
338   mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 16);
339 }
340