blob: 5b98f69994fcd500ba88dc54f47b59a9e489a5be [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <stdlib.h>
13
Yaowu Xuf883b422016-08-30 14:01:10 -070014#include "./aom_config.h"
15#include "./aom_dsp_rtcd.h"
16#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017#include "aom_ports/mem.h"
18
19static INLINE int8_t signed_char_clamp(int t) {
20 return (int8_t)clamp(t, -128, 127);
21}
22
Yaowu Xuc27fc142016-08-22 16:08:15 -070023static INLINE int16_t signed_char_clamp_high(int t, int bd) {
24 switch (bd) {
25 case 10: return (int16_t)clamp(t, -128 * 4, 128 * 4 - 1);
26 case 12: return (int16_t)clamp(t, -128 * 16, 128 * 16 - 1);
27 case 8:
28 default: return (int16_t)clamp(t, -128, 128 - 1);
29 }
30}
Yaowu Xu6d0ed3e2018-02-12 10:38:16 -080031
Yaowu Xuc27fc142016-08-22 16:08:15 -070032// should we apply any filter at all: 11111111 yes, 00000000 no
Ryan Lei392d0ff2017-02-09 13:05:42 -080033static INLINE int8_t filter_mask2(uint8_t limit, uint8_t blimit, uint8_t p1,
34 uint8_t p0, uint8_t q0, uint8_t q1) {
35 int8_t mask = 0;
36 mask |= (abs(p1 - p0) > limit) * -1;
37 mask |= (abs(q1 - q0) > limit) * -1;
38 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit) * -1;
39 return ~mask;
40}
Yaowu Xu6d0ed3e2018-02-12 10:38:16 -080041
Yaowu Xuc27fc142016-08-22 16:08:15 -070042static INLINE int8_t filter_mask(uint8_t limit, uint8_t blimit, uint8_t p3,
43 uint8_t p2, uint8_t p1, uint8_t p0, uint8_t q0,
44 uint8_t q1, uint8_t q2, uint8_t q3) {
45 int8_t mask = 0;
46 mask |= (abs(p3 - p2) > limit) * -1;
47 mask |= (abs(p2 - p1) > limit) * -1;
48 mask |= (abs(p1 - p0) > limit) * -1;
49 mask |= (abs(q1 - q0) > limit) * -1;
50 mask |= (abs(q2 - q1) > limit) * -1;
51 mask |= (abs(q3 - q2) > limit) * -1;
52 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit) * -1;
53 return ~mask;
54}
55
Ryan0c90a5b2018-01-02 10:10:45 -080056static INLINE int8_t filter_mask3_chroma(uint8_t limit, uint8_t blimit,
57 uint8_t p2, uint8_t p1, uint8_t p0,
58 uint8_t q0, uint8_t q1, uint8_t q2) {
59 int8_t mask = 0;
60 mask |= (abs(p2 - p1) > limit) * -1;
61 mask |= (abs(p1 - p0) > limit) * -1;
62 mask |= (abs(q1 - q0) > limit) * -1;
63 mask |= (abs(q2 - q1) > limit) * -1;
64 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit) * -1;
65 return ~mask;
66}
67
Ola Hugosson4ce85212017-09-12 14:53:13 +020068static INLINE int8_t flat_mask3_chroma(uint8_t thresh, uint8_t p2, uint8_t p1,
69 uint8_t p0, uint8_t q0, uint8_t q1,
70 uint8_t q2) {
71 int8_t mask = 0;
72 mask |= (abs(p1 - p0) > thresh) * -1;
73 mask |= (abs(q1 - q0) > thresh) * -1;
74 mask |= (abs(p2 - p0) > thresh) * -1;
75 mask |= (abs(q2 - q0) > thresh) * -1;
76 return ~mask;
77}
Ola Hugosson4ce85212017-09-12 14:53:13 +020078
Yaowu Xuc27fc142016-08-22 16:08:15 -070079static INLINE int8_t flat_mask4(uint8_t thresh, uint8_t p3, uint8_t p2,
80 uint8_t p1, uint8_t p0, uint8_t q0, uint8_t q1,
81 uint8_t q2, uint8_t q3) {
82 int8_t mask = 0;
83 mask |= (abs(p1 - p0) > thresh) * -1;
84 mask |= (abs(q1 - q0) > thresh) * -1;
85 mask |= (abs(p2 - p0) > thresh) * -1;
86 mask |= (abs(q2 - q0) > thresh) * -1;
87 mask |= (abs(p3 - p0) > thresh) * -1;
88 mask |= (abs(q3 - q0) > thresh) * -1;
89 return ~mask;
90}
91
Yaowu Xuc27fc142016-08-22 16:08:15 -070092// is there high edge variance internal edge: 11111111 yes, 00000000 no
93static INLINE int8_t hev_mask(uint8_t thresh, uint8_t p1, uint8_t p0,
94 uint8_t q0, uint8_t q1) {
95 int8_t hev = 0;
96 hev |= (abs(p1 - p0) > thresh) * -1;
97 hev |= (abs(q1 - q0) > thresh) * -1;
98 return hev;
99}
100
101static INLINE void filter4(int8_t mask, uint8_t thresh, uint8_t *op1,
102 uint8_t *op0, uint8_t *oq0, uint8_t *oq1) {
103 int8_t filter1, filter2;
104
105 const int8_t ps1 = (int8_t)*op1 ^ 0x80;
106 const int8_t ps0 = (int8_t)*op0 ^ 0x80;
107 const int8_t qs0 = (int8_t)*oq0 ^ 0x80;
108 const int8_t qs1 = (int8_t)*oq1 ^ 0x80;
109 const uint8_t hev = hev_mask(thresh, *op1, *op0, *oq0, *oq1);
110
111 // add outer taps if we have high edge variance
112 int8_t filter = signed_char_clamp(ps1 - qs1) & hev;
113
114 // inner taps
115 filter = signed_char_clamp(filter + 3 * (qs0 - ps0)) & mask;
116
117 // save bottom 3 bits so that we round one side +4 and the other +3
118 // if it equals 4 we'll set to adjust by -1 to account for the fact
119 // we'd round 3 the other way
120 filter1 = signed_char_clamp(filter + 4) >> 3;
121 filter2 = signed_char_clamp(filter + 3) >> 3;
122
123 *oq0 = signed_char_clamp(qs0 - filter1) ^ 0x80;
124 *op0 = signed_char_clamp(ps0 + filter2) ^ 0x80;
125
126 // outer tap adjustments
127 filter = ROUND_POWER_OF_TWO(filter1, 1) & ~hev;
128
129 *oq1 = signed_char_clamp(qs1 - filter) ^ 0x80;
130 *op1 = signed_char_clamp(ps1 + filter) ^ 0x80;
131}
132
Yaowu Xuf883b422016-08-30 14:01:10 -0700133void aom_lpf_horizontal_4_c(uint8_t *s, int p /* pitch */,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134 const uint8_t *blimit, const uint8_t *limit,
135 const uint8_t *thresh) {
136 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700137 int count = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138
139 // loop filter designed to work using chars so that we can make maximum use
140 // of 8 bit simd instructions.
Ryan Lei17905ed2017-05-23 18:28:51 -0700141 for (i = 0; i < count; ++i) {
Ryan Lei392d0ff2017-02-09 13:05:42 -0800142 const uint8_t p1 = s[-2 * p], p0 = s[-p];
143 const uint8_t q0 = s[0 * p], q1 = s[1 * p];
144 const int8_t mask = filter_mask2(*limit, *blimit, p1, p0, q0, q1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145 filter4(mask, *thresh, s - 2 * p, s - 1 * p, s, s + 1 * p);
146 ++s;
147 }
148}
149
Yaowu Xuf883b422016-08-30 14:01:10 -0700150void aom_lpf_horizontal_4_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 const uint8_t *limit0, const uint8_t *thresh0,
152 const uint8_t *blimit1, const uint8_t *limit1,
153 const uint8_t *thresh1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700154 aom_lpf_horizontal_4_c(s, p, blimit0, limit0, thresh0);
Yi Luo771a80a2017-11-27 15:31:31 -0800155 aom_lpf_horizontal_4_c(s + 4, p, blimit1, limit1, thresh1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700156}
157
Yaowu Xuf883b422016-08-30 14:01:10 -0700158void aom_lpf_vertical_4_c(uint8_t *s, int pitch, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159 const uint8_t *limit, const uint8_t *thresh) {
160 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700161 int count = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162
163 // loop filter designed to work using chars so that we can make maximum use
164 // of 8 bit simd instructions.
Ryan Lei17905ed2017-05-23 18:28:51 -0700165 for (i = 0; i < count; ++i) {
Ryan Lei392d0ff2017-02-09 13:05:42 -0800166 const uint8_t p1 = s[-2], p0 = s[-1];
167 const uint8_t q0 = s[0], q1 = s[1];
168 const int8_t mask = filter_mask2(*limit, *blimit, p1, p0, q0, q1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169 filter4(mask, *thresh, s - 2, s - 1, s, s + 1);
170 s += pitch;
171 }
172}
173
Yaowu Xuf883b422016-08-30 14:01:10 -0700174void aom_lpf_vertical_4_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175 const uint8_t *limit0, const uint8_t *thresh0,
176 const uint8_t *blimit1, const uint8_t *limit1,
177 const uint8_t *thresh1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700178 aom_lpf_vertical_4_c(s, pitch, blimit0, limit0, thresh0);
Yi Luo771a80a2017-11-27 15:31:31 -0800179 aom_lpf_vertical_4_c(s + 4 * pitch, pitch, blimit1, limit1, thresh1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700180}
181
Ola Hugosson4ce85212017-09-12 14:53:13 +0200182static INLINE void filter6(int8_t mask, uint8_t thresh, int8_t flat,
183 uint8_t *op2, uint8_t *op1, uint8_t *op0,
184 uint8_t *oq0, uint8_t *oq1, uint8_t *oq2) {
185 if (flat && mask) {
186 const uint8_t p2 = *op2, p1 = *op1, p0 = *op0;
187 const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2;
188
189 // 5-tap filter [1, 2, 2, 2, 1]
190 *op1 = ROUND_POWER_OF_TWO(p2 * 3 + p1 * 2 + p0 * 2 + q0, 3);
191 *op0 = ROUND_POWER_OF_TWO(p2 + p1 * 2 + p0 * 2 + q0 * 2 + q1, 3);
192 *oq0 = ROUND_POWER_OF_TWO(p1 + p0 * 2 + q0 * 2 + q1 * 2 + q2, 3);
193 *oq1 = ROUND_POWER_OF_TWO(p0 + q0 * 2 + q1 * 2 + q2 * 3, 3);
194 } else {
195 filter4(mask, thresh, op1, op0, oq0, oq1);
196 }
197}
Ola Hugosson4ce85212017-09-12 14:53:13 +0200198
Cheng Chen60f59612017-05-25 15:08:41 -0700199static INLINE void filter8(int8_t mask, uint8_t thresh, int8_t flat,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700200 uint8_t *op3, uint8_t *op2, uint8_t *op1,
201 uint8_t *op0, uint8_t *oq0, uint8_t *oq1,
202 uint8_t *oq2, uint8_t *oq3) {
203 if (flat && mask) {
204 const uint8_t p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
205 const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3;
206
207 // 7-tap filter [1, 1, 1, 2, 1, 1, 1]
208 *op2 = ROUND_POWER_OF_TWO(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3);
209 *op1 = ROUND_POWER_OF_TWO(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3);
210 *op0 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3);
211 *oq0 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3);
212 *oq1 = ROUND_POWER_OF_TWO(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3);
213 *oq2 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3);
214 } else {
215 filter4(mask, thresh, op1, op0, oq0, oq1);
216 }
217}
218
Ola Hugosson4ce85212017-09-12 14:53:13 +0200219void aom_lpf_horizontal_6_c(uint8_t *s, int p, const uint8_t *blimit,
220 const uint8_t *limit, const uint8_t *thresh) {
221 int i;
Ola Hugosson4ce85212017-09-12 14:53:13 +0200222 int count = 4;
Ola Hugosson4ce85212017-09-12 14:53:13 +0200223
224 // loop filter designed to work using chars so that we can make maximum use
225 // of 8 bit simd instructions.
226 for (i = 0; i < count; ++i) {
Ryan0c90a5b2018-01-02 10:10:45 -0800227 const uint8_t p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
228 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p];
Ola Hugosson4ce85212017-09-12 14:53:13 +0200229
230 const int8_t mask =
Ryan0c90a5b2018-01-02 10:10:45 -0800231 filter_mask3_chroma(*limit, *blimit, p2, p1, p0, q0, q1, q2);
Ola Hugosson4ce85212017-09-12 14:53:13 +0200232 const int8_t flat = flat_mask3_chroma(1, p2, p1, p0, q0, q1, q2);
233 filter6(mask, *thresh, flat, s - 3 * p, s - 2 * p, s - 1 * p, s, s + 1 * p,
234 s + 2 * p);
235 ++s;
236 }
237}
Ola Hugosson4ce85212017-09-12 14:53:13 +0200238
Yaowu Xuf883b422016-08-30 14:01:10 -0700239void aom_lpf_horizontal_8_c(uint8_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240 const uint8_t *limit, const uint8_t *thresh) {
241 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700242 int count = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243
244 // loop filter designed to work using chars so that we can make maximum use
245 // of 8 bit simd instructions.
Ryan Lei17905ed2017-05-23 18:28:51 -0700246 for (i = 0; i < count; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700247 const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
248 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
249
250 const int8_t mask =
251 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
252 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
253 filter8(mask, *thresh, flat, s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p, s,
254 s + 1 * p, s + 2 * p, s + 3 * p);
255 ++s;
256 }
257}
258
Yaowu Xuf883b422016-08-30 14:01:10 -0700259void aom_lpf_horizontal_8_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700260 const uint8_t *limit0, const uint8_t *thresh0,
261 const uint8_t *blimit1, const uint8_t *limit1,
262 const uint8_t *thresh1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700263 aom_lpf_horizontal_8_c(s, p, blimit0, limit0, thresh0);
Yi Luo771a80a2017-11-27 15:31:31 -0800264 aom_lpf_horizontal_8_c(s + 4, p, blimit1, limit1, thresh1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265}
266
Ola Hugosson4ce85212017-09-12 14:53:13 +0200267void aom_lpf_vertical_6_c(uint8_t *s, int pitch, const uint8_t *blimit,
268 const uint8_t *limit, const uint8_t *thresh) {
269 int i;
Ola Hugosson4ce85212017-09-12 14:53:13 +0200270 int count = 4;
Ola Hugosson4ce85212017-09-12 14:53:13 +0200271
272 for (i = 0; i < count; ++i) {
Ryan0c90a5b2018-01-02 10:10:45 -0800273 const uint8_t p2 = s[-3], p1 = s[-2], p0 = s[-1];
274 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2];
Ola Hugosson4ce85212017-09-12 14:53:13 +0200275 const int8_t mask =
Ryan0c90a5b2018-01-02 10:10:45 -0800276 filter_mask3_chroma(*limit, *blimit, p2, p1, p0, q0, q1, q2);
Ola Hugosson4ce85212017-09-12 14:53:13 +0200277 const int8_t flat = flat_mask3_chroma(1, p2, p1, p0, q0, q1, q2);
278 filter6(mask, *thresh, flat, s - 3, s - 2, s - 1, s, s + 1, s + 2);
279 s += pitch;
280 }
281}
Ola Hugosson4ce85212017-09-12 14:53:13 +0200282
Yaowu Xuf883b422016-08-30 14:01:10 -0700283void aom_lpf_vertical_8_c(uint8_t *s, int pitch, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284 const uint8_t *limit, const uint8_t *thresh) {
285 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700286 int count = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287
Ryan Lei17905ed2017-05-23 18:28:51 -0700288 for (i = 0; i < count; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
290 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
291 const int8_t mask =
292 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
293 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
294 filter8(mask, *thresh, flat, s - 4, s - 3, s - 2, s - 1, s, s + 1, s + 2,
295 s + 3);
296 s += pitch;
297 }
298}
299
Yaowu Xuf883b422016-08-30 14:01:10 -0700300void aom_lpf_vertical_8_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700301 const uint8_t *limit0, const uint8_t *thresh0,
302 const uint8_t *blimit1, const uint8_t *limit1,
303 const uint8_t *thresh1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700304 aom_lpf_vertical_8_c(s, pitch, blimit0, limit0, thresh0);
Yi Luo771a80a2017-11-27 15:31:31 -0800305 aom_lpf_vertical_8_c(s + 4 * pitch, pitch, blimit1, limit1, thresh1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306}
307
Ola Hugosson4ce85212017-09-12 14:53:13 +0200308static INLINE void filter14(int8_t mask, uint8_t thresh, int8_t flat,
309 int8_t flat2, uint8_t *op6, uint8_t *op5,
310 uint8_t *op4, uint8_t *op3, uint8_t *op2,
311 uint8_t *op1, uint8_t *op0, uint8_t *oq0,
312 uint8_t *oq1, uint8_t *oq2, uint8_t *oq3,
313 uint8_t *oq4, uint8_t *oq5, uint8_t *oq6) {
314 if (flat2 && flat && mask) {
315 const uint8_t p6 = *op6, p5 = *op5, p4 = *op4, p3 = *op3, p2 = *op2,
316 p1 = *op1, p0 = *op0;
317 const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3, q4 = *oq4,
318 q5 = *oq5, q6 = *oq6;
319
320 // 13-tap filter [1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1]
321 *op5 = ROUND_POWER_OF_TWO(p6 * 7 + p5 * 2 + p4 * 2 + p3 + p2 + p1 + p0 + q0,
322 4);
323 *op4 = ROUND_POWER_OF_TWO(
324 p6 * 5 + p5 * 2 + p4 * 2 + p3 * 2 + p2 + p1 + p0 + q0 + q1, 4);
325 *op3 = ROUND_POWER_OF_TWO(
326 p6 * 4 + p5 + p4 * 2 + p3 * 2 + p2 * 2 + p1 + p0 + q0 + q1 + q2, 4);
327 *op2 = ROUND_POWER_OF_TWO(
328 p6 * 3 + p5 + p4 + p3 * 2 + p2 * 2 + p1 * 2 + p0 + q0 + q1 + q2 + q3,
329 4);
330 *op1 = ROUND_POWER_OF_TWO(p6 * 2 + p5 + p4 + p3 + p2 * 2 + p1 * 2 + p0 * 2 +
331 q0 + q1 + q2 + q3 + q4,
332 4);
333 *op0 = ROUND_POWER_OF_TWO(p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 * 2 +
334 q0 * 2 + q1 + q2 + q3 + q4 + q5,
335 4);
336 *oq0 = ROUND_POWER_OF_TWO(p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 * 2 +
337 q1 * 2 + q2 + q3 + q4 + q5 + q6,
338 4);
339 *oq1 = ROUND_POWER_OF_TWO(p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 * 2 +
340 q2 * 2 + q3 + q4 + q5 + q6 * 2,
341 4);
342 *oq2 = ROUND_POWER_OF_TWO(
343 p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 * 2 + q3 * 2 + q4 + q5 + q6 * 3,
344 4);
345 *oq3 = ROUND_POWER_OF_TWO(
346 p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 * 2 + q4 * 2 + q5 + q6 * 4, 4);
347 *oq4 = ROUND_POWER_OF_TWO(
348 p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 * 2 + q5 * 2 + q6 * 5, 4);
349 *oq5 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 * 2 + q6 * 7,
350 4);
351 } else {
352 filter8(mask, thresh, flat, op3, op2, op1, op0, oq0, oq1, oq2, oq3);
353 }
354}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700355
356static void mb_lpf_horizontal_edge_w(uint8_t *s, int p, const uint8_t *blimit,
357 const uint8_t *limit,
358 const uint8_t *thresh, int count) {
359 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700360 int step = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700361
362 // loop filter designed to work using chars so that we can make maximum use
363 // of 8 bit simd instructions.
Ryan Lei17905ed2017-05-23 18:28:51 -0700364 for (i = 0; i < step * count; ++i) {
Yaowu Xucbfffa82018-02-12 15:04:57 -0800365 const uint8_t p6 = s[-7 * p], p5 = s[-6 * p], p4 = s[-5 * p],
366 p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
Ryan Leidd6fa062017-04-13 12:05:33 -0700367 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p],
Yaowu Xucbfffa82018-02-12 15:04:57 -0800368 q4 = s[4 * p], q5 = s[5 * p], q6 = s[6 * p];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700369 const int8_t mask =
370 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
371 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
Ola Hugosson4ce85212017-09-12 14:53:13 +0200372 const int8_t flat2 = flat_mask4(1, p6, p5, p4, p0, q0, q4, q5, q6);
373
374 filter14(mask, *thresh, flat, flat2, s - 7 * p, s - 6 * p, s - 5 * p,
375 s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p, s, s + 1 * p,
376 s + 2 * p, s + 3 * p, s + 4 * p, s + 5 * p, s + 6 * p);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377 ++s;
378 }
379}
380
Ryan26802752018-02-20 10:29:05 -0800381void aom_lpf_horizontal_14_c(uint8_t *s, int p, const uint8_t *blimit,
James Zern1dbe80b2017-11-22 20:59:44 -0800382 const uint8_t *limit, const uint8_t *thresh) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383 mb_lpf_horizontal_edge_w(s, p, blimit, limit, thresh, 1);
384}
385
Cheng Chen85fea902018-05-17 18:58:20 -0700386void aom_lpf_horizontal_14_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
387 const uint8_t *limit0, const uint8_t *thresh0,
388 const uint8_t *blimit1, const uint8_t *limit1,
389 const uint8_t *thresh1) {
390 mb_lpf_horizontal_edge_w(s, p, blimit0, limit0, thresh0, 1);
391 mb_lpf_horizontal_edge_w(s + 4, p, blimit1, limit1, thresh1, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392}
393
394static void mb_lpf_vertical_edge_w(uint8_t *s, int p, const uint8_t *blimit,
395 const uint8_t *limit, const uint8_t *thresh,
396 int count) {
397 int i;
398
399 for (i = 0; i < count; ++i) {
Yaowu Xucbfffa82018-02-12 15:04:57 -0800400 const uint8_t p6 = s[-7], p5 = s[-6], p4 = s[-5], p3 = s[-4], p2 = s[-3],
401 p1 = s[-2], p0 = s[-1];
Ryan Leidd6fa062017-04-13 12:05:33 -0700402 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3], q4 = s[4],
Yaowu Xucbfffa82018-02-12 15:04:57 -0800403 q5 = s[5], q6 = s[6];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404 const int8_t mask =
405 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
406 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
Ola Hugosson4ce85212017-09-12 14:53:13 +0200407 const int8_t flat2 = flat_mask4(1, p6, p5, p4, p0, q0, q4, q5, q6);
408
409 filter14(mask, *thresh, flat, flat2, s - 7, s - 6, s - 5, s - 4, s - 3,
410 s - 2, s - 1, s, s + 1, s + 2, s + 3, s + 4, s + 5, s + 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411 s += p;
412 }
413}
414
Ryan26802752018-02-20 10:29:05 -0800415void aom_lpf_vertical_14_c(uint8_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700416 const uint8_t *limit, const uint8_t *thresh) {
Ryan Lei17905ed2017-05-23 18:28:51 -0700417 mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700418}
419
Cheng Chen85fea902018-05-17 18:58:20 -0700420void aom_lpf_vertical_14_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
421 const uint8_t *limit0, const uint8_t *thresh0,
422 const uint8_t *blimit1, const uint8_t *limit1,
423 const uint8_t *thresh1) {
424 mb_lpf_vertical_edge_w(s, pitch, blimit0, limit0, thresh0, 4);
425 mb_lpf_vertical_edge_w(s + 4 * pitch, pitch, blimit1, limit1, thresh1, 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700426}
427
Ryan Lei392d0ff2017-02-09 13:05:42 -0800428// Should we apply any filter at all: 11111111 yes, 00000000 no ?
429static INLINE int8_t highbd_filter_mask2(uint8_t limit, uint8_t blimit,
430 uint16_t p1, uint16_t p0, uint16_t q0,
431 uint16_t q1, int bd) {
432 int8_t mask = 0;
433 int16_t limit16 = (uint16_t)limit << (bd - 8);
434 int16_t blimit16 = (uint16_t)blimit << (bd - 8);
435 mask |= (abs(p1 - p0) > limit16) * -1;
436 mask |= (abs(q1 - q0) > limit16) * -1;
437 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit16) * -1;
438 return ~mask;
439}
Ryan Lei392d0ff2017-02-09 13:05:42 -0800440
Yaowu Xuc27fc142016-08-22 16:08:15 -0700441// Should we apply any filter at all: 11111111 yes, 00000000 no ?
442static INLINE int8_t highbd_filter_mask(uint8_t limit, uint8_t blimit,
443 uint16_t p3, uint16_t p2, uint16_t p1,
444 uint16_t p0, uint16_t q0, uint16_t q1,
445 uint16_t q2, uint16_t q3, int bd) {
446 int8_t mask = 0;
447 int16_t limit16 = (uint16_t)limit << (bd - 8);
448 int16_t blimit16 = (uint16_t)blimit << (bd - 8);
449 mask |= (abs(p3 - p2) > limit16) * -1;
450 mask |= (abs(p2 - p1) > limit16) * -1;
451 mask |= (abs(p1 - p0) > limit16) * -1;
452 mask |= (abs(q1 - q0) > limit16) * -1;
453 mask |= (abs(q2 - q1) > limit16) * -1;
454 mask |= (abs(q3 - q2) > limit16) * -1;
455 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit16) * -1;
456 return ~mask;
457}
458
Ryan0c90a5b2018-01-02 10:10:45 -0800459static INLINE int8_t highbd_filter_mask3_chroma(uint8_t limit, uint8_t blimit,
460 uint16_t p2, uint16_t p1,
461 uint16_t p0, uint16_t q0,
462 uint16_t q1, uint16_t q2,
463 int bd) {
464 int8_t mask = 0;
465 int16_t limit16 = (uint16_t)limit << (bd - 8);
466 int16_t blimit16 = (uint16_t)blimit << (bd - 8);
467 mask |= (abs(p2 - p1) > limit16) * -1;
468 mask |= (abs(p1 - p0) > limit16) * -1;
469 mask |= (abs(q1 - q0) > limit16) * -1;
470 mask |= (abs(q2 - q1) > limit16) * -1;
471 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit16) * -1;
472 return ~mask;
473}
474
Ola Hugosson4ce85212017-09-12 14:53:13 +0200475static INLINE int8_t highbd_flat_mask3_chroma(uint8_t thresh, uint16_t p2,
476 uint16_t p1, uint16_t p0,
477 uint16_t q0, uint16_t q1,
478 uint16_t q2, int bd) {
479 int8_t mask = 0;
480 int16_t thresh16 = (uint16_t)thresh << (bd - 8);
481 mask |= (abs(p1 - p0) > thresh16) * -1;
482 mask |= (abs(q1 - q0) > thresh16) * -1;
483 mask |= (abs(p2 - p0) > thresh16) * -1;
484 mask |= (abs(q2 - q0) > thresh16) * -1;
485 return ~mask;
486}
Ola Hugosson4ce85212017-09-12 14:53:13 +0200487
Yaowu Xuc27fc142016-08-22 16:08:15 -0700488static INLINE int8_t highbd_flat_mask4(uint8_t thresh, uint16_t p3, uint16_t p2,
489 uint16_t p1, uint16_t p0, uint16_t q0,
490 uint16_t q1, uint16_t q2, uint16_t q3,
491 int bd) {
492 int8_t mask = 0;
493 int16_t thresh16 = (uint16_t)thresh << (bd - 8);
494 mask |= (abs(p1 - p0) > thresh16) * -1;
495 mask |= (abs(q1 - q0) > thresh16) * -1;
496 mask |= (abs(p2 - p0) > thresh16) * -1;
497 mask |= (abs(q2 - q0) > thresh16) * -1;
498 mask |= (abs(p3 - p0) > thresh16) * -1;
499 mask |= (abs(q3 - q0) > thresh16) * -1;
500 return ~mask;
501}
502
Yaowu Xuc27fc142016-08-22 16:08:15 -0700503// Is there high edge variance internal edge:
504// 11111111_11111111 yes, 00000000_00000000 no ?
505static INLINE int16_t highbd_hev_mask(uint8_t thresh, uint16_t p1, uint16_t p0,
506 uint16_t q0, uint16_t q1, int bd) {
507 int16_t hev = 0;
508 int16_t thresh16 = (uint16_t)thresh << (bd - 8);
509 hev |= (abs(p1 - p0) > thresh16) * -1;
510 hev |= (abs(q1 - q0) > thresh16) * -1;
511 return hev;
512}
513
514static INLINE void highbd_filter4(int8_t mask, uint8_t thresh, uint16_t *op1,
515 uint16_t *op0, uint16_t *oq0, uint16_t *oq1,
516 int bd) {
517 int16_t filter1, filter2;
518 // ^0x80 equivalent to subtracting 0x80 from the values to turn them
519 // into -128 to +127 instead of 0 to 255.
520 int shift = bd - 8;
521 const int16_t ps1 = (int16_t)*op1 - (0x80 << shift);
522 const int16_t ps0 = (int16_t)*op0 - (0x80 << shift);
523 const int16_t qs0 = (int16_t)*oq0 - (0x80 << shift);
524 const int16_t qs1 = (int16_t)*oq1 - (0x80 << shift);
525 const uint16_t hev = highbd_hev_mask(thresh, *op1, *op0, *oq0, *oq1, bd);
526
527 // Add outer taps if we have high edge variance.
528 int16_t filter = signed_char_clamp_high(ps1 - qs1, bd) & hev;
529
530 // Inner taps.
531 filter = signed_char_clamp_high(filter + 3 * (qs0 - ps0), bd) & mask;
532
533 // Save bottom 3 bits so that we round one side +4 and the other +3
534 // if it equals 4 we'll set to adjust by -1 to account for the fact
535 // we'd round 3 the other way.
536 filter1 = signed_char_clamp_high(filter + 4, bd) >> 3;
537 filter2 = signed_char_clamp_high(filter + 3, bd) >> 3;
538
539 *oq0 = signed_char_clamp_high(qs0 - filter1, bd) + (0x80 << shift);
540 *op0 = signed_char_clamp_high(ps0 + filter2, bd) + (0x80 << shift);
541
542 // Outer tap adjustments.
543 filter = ROUND_POWER_OF_TWO(filter1, 1) & ~hev;
544
545 *oq1 = signed_char_clamp_high(qs1 - filter, bd) + (0x80 << shift);
546 *op1 = signed_char_clamp_high(ps1 + filter, bd) + (0x80 << shift);
547}
548
Yaowu Xuf883b422016-08-30 14:01:10 -0700549void aom_highbd_lpf_horizontal_4_c(uint16_t *s, int p /* pitch */,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700550 const uint8_t *blimit, const uint8_t *limit,
551 const uint8_t *thresh, int bd) {
552 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700553 int count = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554
555 // loop filter designed to work using chars so that we can make maximum use
556 // of 8 bit simd instructions.
Ryan Lei17905ed2017-05-23 18:28:51 -0700557 for (i = 0; i < count; ++i) {
Ryan Lei392d0ff2017-02-09 13:05:42 -0800558 const uint16_t p1 = s[-2 * p];
559 const uint16_t p0 = s[-p];
560 const uint16_t q0 = s[0 * p];
561 const uint16_t q1 = s[1 * p];
562 const int8_t mask =
563 highbd_filter_mask2(*limit, *blimit, p1, p0, q0, q1, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564 highbd_filter4(mask, *thresh, s - 2 * p, s - 1 * p, s, s + 1 * p, bd);
565 ++s;
566 }
567}
568
Yaowu Xuf883b422016-08-30 14:01:10 -0700569void aom_highbd_lpf_horizontal_4_dual_c(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700570 uint16_t *s, int p, const uint8_t *blimit0, const uint8_t *limit0,
571 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
572 const uint8_t *thresh1, int bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700573 aom_highbd_lpf_horizontal_4_c(s, p, blimit0, limit0, thresh0, bd);
Yi Luo771a80a2017-11-27 15:31:31 -0800574 aom_highbd_lpf_horizontal_4_c(s + 4, p, blimit1, limit1, thresh1, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700575}
576
Yaowu Xuf883b422016-08-30 14:01:10 -0700577void aom_highbd_lpf_vertical_4_c(uint16_t *s, int pitch, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700578 const uint8_t *limit, const uint8_t *thresh,
579 int bd) {
580 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700581 int count = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582
583 // loop filter designed to work using chars so that we can make maximum use
584 // of 8 bit simd instructions.
Ryan Lei17905ed2017-05-23 18:28:51 -0700585 for (i = 0; i < count; ++i) {
Ryan Lei392d0ff2017-02-09 13:05:42 -0800586 const uint16_t p1 = s[-2], p0 = s[-1];
587 const uint16_t q0 = s[0], q1 = s[1];
588 const int8_t mask =
589 highbd_filter_mask2(*limit, *blimit, p1, p0, q0, q1, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590 highbd_filter4(mask, *thresh, s - 2, s - 1, s, s + 1, bd);
591 s += pitch;
592 }
593}
594
Yaowu Xuf883b422016-08-30 14:01:10 -0700595void aom_highbd_lpf_vertical_4_dual_c(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 uint16_t *s, int pitch, const uint8_t *blimit0, const uint8_t *limit0,
597 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
598 const uint8_t *thresh1, int bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700599 aom_highbd_lpf_vertical_4_c(s, pitch, blimit0, limit0, thresh0, bd);
Yi Luo771a80a2017-11-27 15:31:31 -0800600 aom_highbd_lpf_vertical_4_c(s + 4 * pitch, pitch, blimit1, limit1, thresh1,
601 bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602}
603
Ola Hugosson4ce85212017-09-12 14:53:13 +0200604static INLINE void highbd_filter6(int8_t mask, uint8_t thresh, int8_t flat,
605 uint16_t *op2, uint16_t *op1, uint16_t *op0,
606 uint16_t *oq0, uint16_t *oq1, uint16_t *oq2,
607 int bd) {
608 if (flat && mask) {
609 const uint16_t p2 = *op2, p1 = *op1, p0 = *op0;
610 const uint16_t q0 = *oq0, q1 = *oq1, q2 = *oq2;
611
612 // 5-tap filter [1, 2, 2, 2, 1]
613 *op1 = ROUND_POWER_OF_TWO(p2 * 3 + p1 * 2 + p0 * 2 + q0, 3);
614 *op0 = ROUND_POWER_OF_TWO(p2 + p1 * 2 + p0 * 2 + q0 * 2 + q1, 3);
615 *oq0 = ROUND_POWER_OF_TWO(p1 + p0 * 2 + q0 * 2 + q1 * 2 + q2, 3);
616 *oq1 = ROUND_POWER_OF_TWO(p0 + q0 * 2 + q1 * 2 + q2 * 3, 3);
617 } else {
618 highbd_filter4(mask, thresh, op1, op0, oq0, oq1, bd);
619 }
620}
Ola Hugosson4ce85212017-09-12 14:53:13 +0200621
Cheng Chen60f59612017-05-25 15:08:41 -0700622static INLINE void highbd_filter8(int8_t mask, uint8_t thresh, int8_t flat,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623 uint16_t *op3, uint16_t *op2, uint16_t *op1,
624 uint16_t *op0, uint16_t *oq0, uint16_t *oq1,
625 uint16_t *oq2, uint16_t *oq3, int bd) {
626 if (flat && mask) {
627 const uint16_t p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
628 const uint16_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3;
629
630 // 7-tap filter [1, 1, 1, 2, 1, 1, 1]
631 *op2 = ROUND_POWER_OF_TWO(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3);
632 *op1 = ROUND_POWER_OF_TWO(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3);
633 *op0 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3);
634 *oq0 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3);
635 *oq1 = ROUND_POWER_OF_TWO(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3);
636 *oq2 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3);
637 } else {
638 highbd_filter4(mask, thresh, op1, op0, oq0, oq1, bd);
639 }
640}
641
Yaowu Xuf883b422016-08-30 14:01:10 -0700642void aom_highbd_lpf_horizontal_8_c(uint16_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700643 const uint8_t *limit, const uint8_t *thresh,
644 int bd) {
645 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700646 int count = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647
648 // loop filter designed to work using chars so that we can make maximum use
649 // of 8 bit simd instructions.
Ryan Lei17905ed2017-05-23 18:28:51 -0700650 for (i = 0; i < count; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 const uint16_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
652 const uint16_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
653
654 const int8_t mask =
655 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
656 const int8_t flat =
657 highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3, bd);
658 highbd_filter8(mask, *thresh, flat, s - 4 * p, s - 3 * p, s - 2 * p,
659 s - 1 * p, s, s + 1 * p, s + 2 * p, s + 3 * p, bd);
660 ++s;
661 }
662}
663
Ola Hugosson4ce85212017-09-12 14:53:13 +0200664void aom_highbd_lpf_horizontal_6_c(uint16_t *s, int p, const uint8_t *blimit,
665 const uint8_t *limit, const uint8_t *thresh,
666 int bd) {
667 int i;
Ola Hugosson4ce85212017-09-12 14:53:13 +0200668 int count = 4;
Ola Hugosson4ce85212017-09-12 14:53:13 +0200669
670 // loop filter designed to work using chars so that we can make maximum use
671 // of 8 bit simd instructions.
672 for (i = 0; i < count; ++i) {
Ryan0c90a5b2018-01-02 10:10:45 -0800673 const uint16_t p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
674 const uint16_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p];
Ola Hugosson4ce85212017-09-12 14:53:13 +0200675
676 const int8_t mask =
Ryan0c90a5b2018-01-02 10:10:45 -0800677 highbd_filter_mask3_chroma(*limit, *blimit, p2, p1, p0, q0, q1, q2, bd);
Ola Hugosson4ce85212017-09-12 14:53:13 +0200678 const int8_t flat = highbd_flat_mask3_chroma(1, p2, p1, p0, q0, q1, q2, bd);
679 highbd_filter6(mask, *thresh, flat, s - 3 * p, s - 2 * p, s - 1 * p, s,
680 s + 1 * p, s + 2 * p, bd);
681 ++s;
682 }
683}
Ola Hugosson4ce85212017-09-12 14:53:13 +0200684
Yaowu Xuf883b422016-08-30 14:01:10 -0700685void aom_highbd_lpf_horizontal_8_dual_c(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686 uint16_t *s, int p, const uint8_t *blimit0, const uint8_t *limit0,
687 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
688 const uint8_t *thresh1, int bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700689 aom_highbd_lpf_horizontal_8_c(s, p, blimit0, limit0, thresh0, bd);
Yi Luo771a80a2017-11-27 15:31:31 -0800690 aom_highbd_lpf_horizontal_8_c(s + 4, p, blimit1, limit1, thresh1, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700691}
692
Ola Hugosson4ce85212017-09-12 14:53:13 +0200693void aom_highbd_lpf_vertical_6_c(uint16_t *s, int pitch, const uint8_t *blimit,
694 const uint8_t *limit, const uint8_t *thresh,
695 int bd) {
696 int i;
Ola Hugosson4ce85212017-09-12 14:53:13 +0200697 int count = 4;
Ola Hugosson4ce85212017-09-12 14:53:13 +0200698
699 for (i = 0; i < count; ++i) {
Ryan0c90a5b2018-01-02 10:10:45 -0800700 const uint16_t p2 = s[-3], p1 = s[-2], p0 = s[-1];
701 const uint16_t q0 = s[0], q1 = s[1], q2 = s[2];
Ola Hugosson4ce85212017-09-12 14:53:13 +0200702 const int8_t mask =
Ryan0c90a5b2018-01-02 10:10:45 -0800703 highbd_filter_mask3_chroma(*limit, *blimit, p2, p1, p0, q0, q1, q2, bd);
Ola Hugosson4ce85212017-09-12 14:53:13 +0200704 const int8_t flat = highbd_flat_mask3_chroma(1, p2, p1, p0, q0, q1, q2, bd);
705 highbd_filter6(mask, *thresh, flat, s - 3, s - 2, s - 1, s, s + 1, s + 2,
706 bd);
707 s += pitch;
708 }
709}
Ola Hugosson4ce85212017-09-12 14:53:13 +0200710
Yaowu Xuf883b422016-08-30 14:01:10 -0700711void aom_highbd_lpf_vertical_8_c(uint16_t *s, int pitch, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700712 const uint8_t *limit, const uint8_t *thresh,
713 int bd) {
714 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700715 int count = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716
Ryan Lei17905ed2017-05-23 18:28:51 -0700717 for (i = 0; i < count; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718 const uint16_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
719 const uint16_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
720 const int8_t mask =
721 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
722 const int8_t flat =
723 highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3, bd);
724 highbd_filter8(mask, *thresh, flat, s - 4, s - 3, s - 2, s - 1, s, s + 1,
725 s + 2, s + 3, bd);
726 s += pitch;
727 }
728}
729
Yaowu Xuf883b422016-08-30 14:01:10 -0700730void aom_highbd_lpf_vertical_8_dual_c(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700731 uint16_t *s, int pitch, const uint8_t *blimit0, const uint8_t *limit0,
732 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
733 const uint8_t *thresh1, int bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 aom_highbd_lpf_vertical_8_c(s, pitch, blimit0, limit0, thresh0, bd);
Yi Luo771a80a2017-11-27 15:31:31 -0800735 aom_highbd_lpf_vertical_8_c(s + 4 * pitch, pitch, blimit1, limit1, thresh1,
736 bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737}
738
Ola Hugosson4ce85212017-09-12 14:53:13 +0200739static INLINE void highbd_filter14(int8_t mask, uint8_t thresh, int8_t flat,
740 int8_t flat2, uint16_t *op6, uint16_t *op5,
741 uint16_t *op4, uint16_t *op3, uint16_t *op2,
742 uint16_t *op1, uint16_t *op0, uint16_t *oq0,
743 uint16_t *oq1, uint16_t *oq2, uint16_t *oq3,
744 uint16_t *oq4, uint16_t *oq5, uint16_t *oq6,
745 int bd) {
746 if (flat2 && flat && mask) {
747 const uint16_t p6 = *op6;
748 const uint16_t p5 = *op5;
749 const uint16_t p4 = *op4;
750 const uint16_t p3 = *op3;
751 const uint16_t p2 = *op2;
752 const uint16_t p1 = *op1;
753 const uint16_t p0 = *op0;
754 const uint16_t q0 = *oq0;
755 const uint16_t q1 = *oq1;
756 const uint16_t q2 = *oq2;
757 const uint16_t q3 = *oq3;
758 const uint16_t q4 = *oq4;
759 const uint16_t q5 = *oq5;
760 const uint16_t q6 = *oq6;
761
762 // 13-tap filter [1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1]
763 *op5 = ROUND_POWER_OF_TWO(p6 * 7 + p5 * 2 + p4 * 2 + p3 + p2 + p1 + p0 + q0,
764 4);
765 *op4 = ROUND_POWER_OF_TWO(
766 p6 * 5 + p5 * 2 + p4 * 2 + p3 * 2 + p2 + p1 + p0 + q0 + q1, 4);
767 *op3 = ROUND_POWER_OF_TWO(
768 p6 * 4 + p5 + p4 * 2 + p3 * 2 + p2 * 2 + p1 + p0 + q0 + q1 + q2, 4);
769 *op2 = ROUND_POWER_OF_TWO(
770 p6 * 3 + p5 + p4 + p3 * 2 + p2 * 2 + p1 * 2 + p0 + q0 + q1 + q2 + q3,
771 4);
772 *op1 = ROUND_POWER_OF_TWO(p6 * 2 + p5 + p4 + p3 + p2 * 2 + p1 * 2 + p0 * 2 +
773 q0 + q1 + q2 + q3 + q4,
774 4);
775 *op0 = ROUND_POWER_OF_TWO(p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 * 2 +
776 q0 * 2 + q1 + q2 + q3 + q4 + q5,
777 4);
778 *oq0 = ROUND_POWER_OF_TWO(p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 * 2 +
779 q1 * 2 + q2 + q3 + q4 + q5 + q6,
780 4);
781 *oq1 = ROUND_POWER_OF_TWO(p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 * 2 +
782 q2 * 2 + q3 + q4 + q5 + q6 * 2,
783 4);
784 *oq2 = ROUND_POWER_OF_TWO(
785 p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 * 2 + q3 * 2 + q4 + q5 + q6 * 3,
786 4);
787 *oq3 = ROUND_POWER_OF_TWO(
788 p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 * 2 + q4 * 2 + q5 + q6 * 4, 4);
789 *oq4 = ROUND_POWER_OF_TWO(
790 p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 * 2 + q5 * 2 + q6 * 5, 4);
791 *oq5 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 * 2 + q6 * 7,
792 4);
793 } else {
794 highbd_filter8(mask, thresh, flat, op3, op2, op1, op0, oq0, oq1, oq2, oq3,
795 bd);
796 }
797}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798
799static void highbd_mb_lpf_horizontal_edge_w(uint16_t *s, int p,
800 const uint8_t *blimit,
801 const uint8_t *limit,
802 const uint8_t *thresh, int count,
803 int bd) {
804 int i;
Ryan Lei17905ed2017-05-23 18:28:51 -0700805 int step = 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806
807 // loop filter designed to work using chars so that we can make maximum use
808 // of 8 bit simd instructions.
Ryan Lei17905ed2017-05-23 18:28:51 -0700809 for (i = 0; i < step * count; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810 const uint16_t p3 = s[-4 * p];
811 const uint16_t p2 = s[-3 * p];
812 const uint16_t p1 = s[-2 * p];
813 const uint16_t p0 = s[-p];
814 const uint16_t q0 = s[0 * p];
815 const uint16_t q1 = s[1 * p];
816 const uint16_t q2 = s[2 * p];
817 const uint16_t q3 = s[3 * p];
818 const int8_t mask =
819 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
820 const int8_t flat =
821 highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3, bd);
Ola Hugosson4ce85212017-09-12 14:53:13 +0200822
Ola Hugosson4ce85212017-09-12 14:53:13 +0200823 const int8_t flat2 =
824 highbd_flat_mask4(1, s[-7 * p], s[-6 * p], s[-5 * p], p0, q0, s[4 * p],
825 s[5 * p], s[6 * p], bd);
826
827 highbd_filter14(mask, *thresh, flat, flat2, s - 7 * p, s - 6 * p, s - 5 * p,
828 s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p, s, s + 1 * p,
829 s + 2 * p, s + 3 * p, s + 4 * p, s + 5 * p, s + 6 * p, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700830 ++s;
831 }
832}
833
Ryan26802752018-02-20 10:29:05 -0800834void aom_highbd_lpf_horizontal_14_c(uint16_t *s, int p, const uint8_t *blimit,
James Zern684b7bd2017-11-28 12:55:51 -0800835 const uint8_t *limit, const uint8_t *thresh,
836 int bd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837 highbd_mb_lpf_horizontal_edge_w(s, p, blimit, limit, thresh, 1, bd);
838}
839
Cheng Chen85fea902018-05-17 18:58:20 -0700840void aom_highbd_lpf_horizontal_14_dual_c(
841 uint16_t *s, int p, const uint8_t *blimit0, const uint8_t *limit0,
842 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
843 const uint8_t *thresh1, int bd) {
844 highbd_mb_lpf_horizontal_edge_w(s, p, blimit0, limit0, thresh0, 1, bd);
845 highbd_mb_lpf_horizontal_edge_w(s + 4, p, blimit1, limit1, thresh1, 1, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846}
847
848static void highbd_mb_lpf_vertical_edge_w(uint16_t *s, int p,
849 const uint8_t *blimit,
850 const uint8_t *limit,
851 const uint8_t *thresh, int count,
852 int bd) {
853 int i;
854
855 for (i = 0; i < count; ++i) {
856 const uint16_t p3 = s[-4];
857 const uint16_t p2 = s[-3];
858 const uint16_t p1 = s[-2];
859 const uint16_t p0 = s[-1];
860 const uint16_t q0 = s[0];
861 const uint16_t q1 = s[1];
862 const uint16_t q2 = s[2];
863 const uint16_t q3 = s[3];
864 const int8_t mask =
865 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
866 const int8_t flat =
867 highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3, bd);
Ola Hugosson4ce85212017-09-12 14:53:13 +0200868 const int8_t flat2 =
869 highbd_flat_mask4(1, s[-7], s[-6], s[-5], p0, q0, s[4], s[5], s[6], bd);
870
871 highbd_filter14(mask, *thresh, flat, flat2, s - 7, s - 6, s - 5, s - 4,
872 s - 3, s - 2, s - 1, s, s + 1, s + 2, s + 3, s + 4, s + 5,
873 s + 6, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 s += p;
875 }
876}
877
Ryan26802752018-02-20 10:29:05 -0800878void aom_highbd_lpf_vertical_14_c(uint16_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700879 const uint8_t *limit, const uint8_t *thresh,
880 int bd) {
Ryan Lei17905ed2017-05-23 18:28:51 -0700881 highbd_mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 4, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882}
883
Cheng Chen85fea902018-05-17 18:58:20 -0700884void aom_highbd_lpf_vertical_14_dual_c(
885 uint16_t *s, int pitch, const uint8_t *blimit0, const uint8_t *limit0,
886 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
887 const uint8_t *thresh1, int bd) {
888 highbd_mb_lpf_vertical_edge_w(s, pitch, blimit0, limit0, thresh0, 4, bd);
889 highbd_mb_lpf_vertical_edge_w(s + 4 * pitch, pitch, blimit1, limit1, thresh1,
890 4, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891}