blob: c5054b50bc112086e31451964e9aca552363bc5b [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 Xuf883b422016-08-30 14:01:10 -070023#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070024static INLINE int16_t signed_char_clamp_high(int t, int bd) {
25 switch (bd) {
26 case 10: return (int16_t)clamp(t, -128 * 4, 128 * 4 - 1);
27 case 12: return (int16_t)clamp(t, -128 * 16, 128 * 16 - 1);
28 case 8:
29 default: return (int16_t)clamp(t, -128, 128 - 1);
30 }
31}
32#endif
33
34// should we apply any filter at all: 11111111 yes, 00000000 no
35static INLINE int8_t filter_mask(uint8_t limit, uint8_t blimit, uint8_t p3,
36 uint8_t p2, uint8_t p1, uint8_t p0, uint8_t q0,
37 uint8_t q1, uint8_t q2, uint8_t q3) {
38 int8_t mask = 0;
39 mask |= (abs(p3 - p2) > limit) * -1;
40 mask |= (abs(p2 - p1) > limit) * -1;
41 mask |= (abs(p1 - p0) > limit) * -1;
42 mask |= (abs(q1 - q0) > limit) * -1;
43 mask |= (abs(q2 - q1) > limit) * -1;
44 mask |= (abs(q3 - q2) > limit) * -1;
45 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit) * -1;
46 return ~mask;
47}
48
49static INLINE int8_t flat_mask4(uint8_t thresh, uint8_t p3, uint8_t p2,
50 uint8_t p1, uint8_t p0, uint8_t q0, uint8_t q1,
51 uint8_t q2, uint8_t q3) {
52 int8_t mask = 0;
53 mask |= (abs(p1 - p0) > thresh) * -1;
54 mask |= (abs(q1 - q0) > thresh) * -1;
55 mask |= (abs(p2 - p0) > thresh) * -1;
56 mask |= (abs(q2 - q0) > thresh) * -1;
57 mask |= (abs(p3 - p0) > thresh) * -1;
58 mask |= (abs(q3 - q0) > thresh) * -1;
59 return ~mask;
60}
61
62static INLINE int8_t flat_mask5(uint8_t thresh, uint8_t p4, uint8_t p3,
63 uint8_t p2, uint8_t p1, uint8_t p0, uint8_t q0,
64 uint8_t q1, uint8_t q2, uint8_t q3,
65 uint8_t q4) {
66 int8_t mask = ~flat_mask4(thresh, p3, p2, p1, p0, q0, q1, q2, q3);
67 mask |= (abs(p4 - p0) > thresh) * -1;
68 mask |= (abs(q4 - q0) > thresh) * -1;
69 return ~mask;
70}
71
72// is there high edge variance internal edge: 11111111 yes, 00000000 no
73static INLINE int8_t hev_mask(uint8_t thresh, uint8_t p1, uint8_t p0,
74 uint8_t q0, uint8_t q1) {
75 int8_t hev = 0;
76 hev |= (abs(p1 - p0) > thresh) * -1;
77 hev |= (abs(q1 - q0) > thresh) * -1;
78 return hev;
79}
80
81static INLINE void filter4(int8_t mask, uint8_t thresh, uint8_t *op1,
82 uint8_t *op0, uint8_t *oq0, uint8_t *oq1) {
83 int8_t filter1, filter2;
84
85 const int8_t ps1 = (int8_t)*op1 ^ 0x80;
86 const int8_t ps0 = (int8_t)*op0 ^ 0x80;
87 const int8_t qs0 = (int8_t)*oq0 ^ 0x80;
88 const int8_t qs1 = (int8_t)*oq1 ^ 0x80;
89 const uint8_t hev = hev_mask(thresh, *op1, *op0, *oq0, *oq1);
90
91 // add outer taps if we have high edge variance
92 int8_t filter = signed_char_clamp(ps1 - qs1) & hev;
93
94 // inner taps
95 filter = signed_char_clamp(filter + 3 * (qs0 - ps0)) & mask;
96
97 // save bottom 3 bits so that we round one side +4 and the other +3
98 // if it equals 4 we'll set to adjust by -1 to account for the fact
99 // we'd round 3 the other way
100 filter1 = signed_char_clamp(filter + 4) >> 3;
101 filter2 = signed_char_clamp(filter + 3) >> 3;
102
103 *oq0 = signed_char_clamp(qs0 - filter1) ^ 0x80;
104 *op0 = signed_char_clamp(ps0 + filter2) ^ 0x80;
105
106 // outer tap adjustments
107 filter = ROUND_POWER_OF_TWO(filter1, 1) & ~hev;
108
109 *oq1 = signed_char_clamp(qs1 - filter) ^ 0x80;
110 *op1 = signed_char_clamp(ps1 + filter) ^ 0x80;
111}
112
Yaowu Xuf883b422016-08-30 14:01:10 -0700113void aom_lpf_horizontal_4_c(uint8_t *s, int p /* pitch */,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114 const uint8_t *blimit, const uint8_t *limit,
115 const uint8_t *thresh) {
116 int i;
117
118 // loop filter designed to work using chars so that we can make maximum use
119 // of 8 bit simd instructions.
120 for (i = 0; i < 8; ++i) {
121 const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
122 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
123 const int8_t mask =
124 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
125 filter4(mask, *thresh, s - 2 * p, s - 1 * p, s, s + 1 * p);
126 ++s;
127 }
128}
129
Yaowu Xuf883b422016-08-30 14:01:10 -0700130void aom_lpf_horizontal_4_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700131 const uint8_t *limit0, const uint8_t *thresh0,
132 const uint8_t *blimit1, const uint8_t *limit1,
133 const uint8_t *thresh1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700134 aom_lpf_horizontal_4_c(s, p, blimit0, limit0, thresh0);
135 aom_lpf_horizontal_4_c(s + 8, p, blimit1, limit1, thresh1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700136}
137
Yaowu Xuf883b422016-08-30 14:01:10 -0700138void aom_lpf_vertical_4_c(uint8_t *s, int pitch, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700139 const uint8_t *limit, const uint8_t *thresh) {
140 int i;
141
142 // loop filter designed to work using chars so that we can make maximum use
143 // of 8 bit simd instructions.
144 for (i = 0; i < 8; ++i) {
145 const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
146 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
147 const int8_t mask =
148 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
149 filter4(mask, *thresh, s - 2, s - 1, s, s + 1);
150 s += pitch;
151 }
152}
153
Yaowu Xuf883b422016-08-30 14:01:10 -0700154void aom_lpf_vertical_4_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155 const uint8_t *limit0, const uint8_t *thresh0,
156 const uint8_t *blimit1, const uint8_t *limit1,
157 const uint8_t *thresh1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700158 aom_lpf_vertical_4_c(s, pitch, blimit0, limit0, thresh0);
159 aom_lpf_vertical_4_c(s + 8 * pitch, pitch, blimit1, limit1, thresh1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160}
161
162static INLINE void filter8(int8_t mask, uint8_t thresh, uint8_t flat,
163 uint8_t *op3, uint8_t *op2, uint8_t *op1,
164 uint8_t *op0, uint8_t *oq0, uint8_t *oq1,
165 uint8_t *oq2, uint8_t *oq3) {
166 if (flat && mask) {
167 const uint8_t p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
168 const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3;
169
170 // 7-tap filter [1, 1, 1, 2, 1, 1, 1]
171 *op2 = ROUND_POWER_OF_TWO(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3);
172 *op1 = ROUND_POWER_OF_TWO(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3);
173 *op0 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3);
174 *oq0 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3);
175 *oq1 = ROUND_POWER_OF_TWO(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3);
176 *oq2 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3);
177 } else {
178 filter4(mask, thresh, op1, op0, oq0, oq1);
179 }
180}
181
Yaowu Xuf883b422016-08-30 14:01:10 -0700182void aom_lpf_horizontal_8_c(uint8_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183 const uint8_t *limit, const uint8_t *thresh) {
184 int i;
185
186 // loop filter designed to work using chars so that we can make maximum use
187 // of 8 bit simd instructions.
188 for (i = 0; i < 8; ++i) {
189 const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
190 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
191
192 const int8_t mask =
193 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
194 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
195 filter8(mask, *thresh, flat, s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p, s,
196 s + 1 * p, s + 2 * p, s + 3 * p);
197 ++s;
198 }
199}
200
Yaowu Xuf883b422016-08-30 14:01:10 -0700201void aom_lpf_horizontal_8_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202 const uint8_t *limit0, const uint8_t *thresh0,
203 const uint8_t *blimit1, const uint8_t *limit1,
204 const uint8_t *thresh1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700205 aom_lpf_horizontal_8_c(s, p, blimit0, limit0, thresh0);
206 aom_lpf_horizontal_8_c(s + 8, p, blimit1, limit1, thresh1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207}
208
Yaowu Xuf883b422016-08-30 14:01:10 -0700209void aom_lpf_vertical_8_c(uint8_t *s, int pitch, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210 const uint8_t *limit, const uint8_t *thresh) {
211 int i;
212
213 for (i = 0; i < 8; ++i) {
214 const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
215 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
216 const int8_t mask =
217 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
218 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
219 filter8(mask, *thresh, flat, s - 4, s - 3, s - 2, s - 1, s, s + 1, s + 2,
220 s + 3);
221 s += pitch;
222 }
223}
224
Yaowu Xuf883b422016-08-30 14:01:10 -0700225void aom_lpf_vertical_8_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 const uint8_t *limit0, const uint8_t *thresh0,
227 const uint8_t *blimit1, const uint8_t *limit1,
228 const uint8_t *thresh1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700229 aom_lpf_vertical_8_c(s, pitch, blimit0, limit0, thresh0);
230 aom_lpf_vertical_8_c(s + 8 * pitch, pitch, blimit1, limit1, thresh1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231}
232
233static INLINE void filter16(int8_t mask, uint8_t thresh, uint8_t flat,
234 uint8_t flat2, uint8_t *op7, uint8_t *op6,
235 uint8_t *op5, uint8_t *op4, uint8_t *op3,
236 uint8_t *op2, uint8_t *op1, uint8_t *op0,
237 uint8_t *oq0, uint8_t *oq1, uint8_t *oq2,
238 uint8_t *oq3, uint8_t *oq4, uint8_t *oq5,
239 uint8_t *oq6, uint8_t *oq7) {
240 if (flat2 && flat && mask) {
241 const uint8_t p7 = *op7, p6 = *op6, p5 = *op5, p4 = *op4, p3 = *op3,
242 p2 = *op2, p1 = *op1, p0 = *op0;
243
244 const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3, q4 = *oq4,
245 q5 = *oq5, q6 = *oq6, q7 = *oq7;
246
247 // 15-tap filter [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1]
248 *op6 = ROUND_POWER_OF_TWO(
249 p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 + q0, 4);
250 *op5 = ROUND_POWER_OF_TWO(
251 p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 + q0 + q1, 4);
252 *op4 = ROUND_POWER_OF_TWO(
253 p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 + q0 + q1 + q2, 4);
254 *op3 = ROUND_POWER_OF_TWO(
255 p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 + q0 + q1 + q2 + q3, 4);
256 *op2 = ROUND_POWER_OF_TWO(
257 p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 + q0 + q1 + q2 + q3 + q4,
258 4);
259 *op1 = ROUND_POWER_OF_TWO(p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 +
260 q0 + q1 + q2 + q3 + q4 + q5,
261 4);
262 *op0 = ROUND_POWER_OF_TWO(p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 +
263 q1 + q2 + q3 + q4 + q5 + q6,
264 4);
265 *oq0 = ROUND_POWER_OF_TWO(p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 +
266 q2 + q3 + q4 + q5 + q6 + q7,
267 4);
268 *oq1 = ROUND_POWER_OF_TWO(p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 +
269 q3 + q4 + q5 + q6 + q7 * 2,
270 4);
271 *oq2 = ROUND_POWER_OF_TWO(
272 p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3,
273 4);
274 *oq3 = ROUND_POWER_OF_TWO(
275 p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4, 4);
276 *oq4 = ROUND_POWER_OF_TWO(
277 p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5, 4);
278 *oq5 = ROUND_POWER_OF_TWO(
279 p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6, 4);
280 *oq6 = ROUND_POWER_OF_TWO(
281 p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7, 4);
282 } else {
283 filter8(mask, thresh, flat, op3, op2, op1, op0, oq0, oq1, oq2, oq3);
284 }
285}
286
287static void mb_lpf_horizontal_edge_w(uint8_t *s, int p, const uint8_t *blimit,
288 const uint8_t *limit,
289 const uint8_t *thresh, int count) {
290 int i;
291
292 // loop filter designed to work using chars so that we can make maximum use
293 // of 8 bit simd instructions.
294 for (i = 0; i < 8 * count; ++i) {
295 const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
296 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
297 const int8_t mask =
298 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
299 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
300 const int8_t flat2 =
301 flat_mask5(1, s[-8 * p], s[-7 * p], s[-6 * p], s[-5 * p], p0, q0,
302 s[4 * p], s[5 * p], s[6 * p], s[7 * p]);
303
304 filter16(mask, *thresh, flat, flat2, s - 8 * p, s - 7 * p, s - 6 * p,
305 s - 5 * p, s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p, s,
306 s + 1 * p, s + 2 * p, s + 3 * p, s + 4 * p, s + 5 * p, s + 6 * p,
307 s + 7 * p);
308 ++s;
309 }
310}
311
Yaowu Xuf883b422016-08-30 14:01:10 -0700312void aom_lpf_horizontal_edge_8_c(uint8_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700313 const uint8_t *limit, const uint8_t *thresh) {
314 mb_lpf_horizontal_edge_w(s, p, blimit, limit, thresh, 1);
315}
316
Yaowu Xuf883b422016-08-30 14:01:10 -0700317void aom_lpf_horizontal_edge_16_c(uint8_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 const uint8_t *limit, const uint8_t *thresh) {
319 mb_lpf_horizontal_edge_w(s, p, blimit, limit, thresh, 2);
320}
321
322static void mb_lpf_vertical_edge_w(uint8_t *s, int p, const uint8_t *blimit,
323 const uint8_t *limit, const uint8_t *thresh,
324 int count) {
325 int i;
326
327 for (i = 0; i < count; ++i) {
328 const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
329 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
330 const int8_t mask =
331 filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3);
332 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
333 const int8_t flat2 = flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0, q0, s[4],
334 s[5], s[6], s[7]);
335
336 filter16(mask, *thresh, flat, flat2, s - 8, s - 7, s - 6, s - 5, s - 4,
337 s - 3, s - 2, s - 1, s, s + 1, s + 2, s + 3, s + 4, s + 5, s + 6,
338 s + 7);
339 s += p;
340 }
341}
342
Yaowu Xuf883b422016-08-30 14:01:10 -0700343void aom_lpf_vertical_16_c(uint8_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344 const uint8_t *limit, const uint8_t *thresh) {
345 mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 8);
346}
347
Yaowu Xuf883b422016-08-30 14:01:10 -0700348void aom_lpf_vertical_16_dual_c(uint8_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349 const uint8_t *limit, const uint8_t *thresh) {
350 mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 16);
351}
352
Yaowu Xuf883b422016-08-30 14:01:10 -0700353#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700354// Should we apply any filter at all: 11111111 yes, 00000000 no ?
355static INLINE int8_t highbd_filter_mask(uint8_t limit, uint8_t blimit,
356 uint16_t p3, uint16_t p2, uint16_t p1,
357 uint16_t p0, uint16_t q0, uint16_t q1,
358 uint16_t q2, uint16_t q3, int bd) {
359 int8_t mask = 0;
360 int16_t limit16 = (uint16_t)limit << (bd - 8);
361 int16_t blimit16 = (uint16_t)blimit << (bd - 8);
362 mask |= (abs(p3 - p2) > limit16) * -1;
363 mask |= (abs(p2 - p1) > limit16) * -1;
364 mask |= (abs(p1 - p0) > limit16) * -1;
365 mask |= (abs(q1 - q0) > limit16) * -1;
366 mask |= (abs(q2 - q1) > limit16) * -1;
367 mask |= (abs(q3 - q2) > limit16) * -1;
368 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit16) * -1;
369 return ~mask;
370}
371
372static INLINE int8_t highbd_flat_mask4(uint8_t thresh, uint16_t p3, uint16_t p2,
373 uint16_t p1, uint16_t p0, uint16_t q0,
374 uint16_t q1, uint16_t q2, uint16_t q3,
375 int bd) {
376 int8_t mask = 0;
377 int16_t thresh16 = (uint16_t)thresh << (bd - 8);
378 mask |= (abs(p1 - p0) > thresh16) * -1;
379 mask |= (abs(q1 - q0) > thresh16) * -1;
380 mask |= (abs(p2 - p0) > thresh16) * -1;
381 mask |= (abs(q2 - q0) > thresh16) * -1;
382 mask |= (abs(p3 - p0) > thresh16) * -1;
383 mask |= (abs(q3 - q0) > thresh16) * -1;
384 return ~mask;
385}
386
387static INLINE int8_t highbd_flat_mask5(uint8_t thresh, uint16_t p4, uint16_t p3,
388 uint16_t p2, uint16_t p1, uint16_t p0,
389 uint16_t q0, uint16_t q1, uint16_t q2,
390 uint16_t q3, uint16_t q4, int bd) {
391 int8_t mask = ~highbd_flat_mask4(thresh, p3, p2, p1, p0, q0, q1, q2, q3, bd);
392 int16_t thresh16 = (uint16_t)thresh << (bd - 8);
393 mask |= (abs(p4 - p0) > thresh16) * -1;
394 mask |= (abs(q4 - q0) > thresh16) * -1;
395 return ~mask;
396}
397
398// Is there high edge variance internal edge:
399// 11111111_11111111 yes, 00000000_00000000 no ?
400static INLINE int16_t highbd_hev_mask(uint8_t thresh, uint16_t p1, uint16_t p0,
401 uint16_t q0, uint16_t q1, int bd) {
402 int16_t hev = 0;
403 int16_t thresh16 = (uint16_t)thresh << (bd - 8);
404 hev |= (abs(p1 - p0) > thresh16) * -1;
405 hev |= (abs(q1 - q0) > thresh16) * -1;
406 return hev;
407}
408
409static INLINE void highbd_filter4(int8_t mask, uint8_t thresh, uint16_t *op1,
410 uint16_t *op0, uint16_t *oq0, uint16_t *oq1,
411 int bd) {
412 int16_t filter1, filter2;
413 // ^0x80 equivalent to subtracting 0x80 from the values to turn them
414 // into -128 to +127 instead of 0 to 255.
415 int shift = bd - 8;
416 const int16_t ps1 = (int16_t)*op1 - (0x80 << shift);
417 const int16_t ps0 = (int16_t)*op0 - (0x80 << shift);
418 const int16_t qs0 = (int16_t)*oq0 - (0x80 << shift);
419 const int16_t qs1 = (int16_t)*oq1 - (0x80 << shift);
420 const uint16_t hev = highbd_hev_mask(thresh, *op1, *op0, *oq0, *oq1, bd);
421
422 // Add outer taps if we have high edge variance.
423 int16_t filter = signed_char_clamp_high(ps1 - qs1, bd) & hev;
424
425 // Inner taps.
426 filter = signed_char_clamp_high(filter + 3 * (qs0 - ps0), bd) & mask;
427
428 // Save bottom 3 bits so that we round one side +4 and the other +3
429 // if it equals 4 we'll set to adjust by -1 to account for the fact
430 // we'd round 3 the other way.
431 filter1 = signed_char_clamp_high(filter + 4, bd) >> 3;
432 filter2 = signed_char_clamp_high(filter + 3, bd) >> 3;
433
434 *oq0 = signed_char_clamp_high(qs0 - filter1, bd) + (0x80 << shift);
435 *op0 = signed_char_clamp_high(ps0 + filter2, bd) + (0x80 << shift);
436
437 // Outer tap adjustments.
438 filter = ROUND_POWER_OF_TWO(filter1, 1) & ~hev;
439
440 *oq1 = signed_char_clamp_high(qs1 - filter, bd) + (0x80 << shift);
441 *op1 = signed_char_clamp_high(ps1 + filter, bd) + (0x80 << shift);
442}
443
Yaowu Xuf883b422016-08-30 14:01:10 -0700444void aom_highbd_lpf_horizontal_4_c(uint16_t *s, int p /* pitch */,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445 const uint8_t *blimit, const uint8_t *limit,
446 const uint8_t *thresh, int bd) {
447 int i;
448
449 // loop filter designed to work using chars so that we can make maximum use
450 // of 8 bit simd instructions.
451 for (i = 0; i < 8; ++i) {
452 const uint16_t p3 = s[-4 * p];
453 const uint16_t p2 = s[-3 * p];
454 const uint16_t p1 = s[-2 * p];
455 const uint16_t p0 = s[-p];
456 const uint16_t q0 = s[0 * p];
457 const uint16_t q1 = s[1 * p];
458 const uint16_t q2 = s[2 * p];
459 const uint16_t q3 = s[3 * p];
460 const int8_t mask =
461 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
462 highbd_filter4(mask, *thresh, s - 2 * p, s - 1 * p, s, s + 1 * p, bd);
463 ++s;
464 }
465}
466
Yaowu Xuf883b422016-08-30 14:01:10 -0700467void aom_highbd_lpf_horizontal_4_dual_c(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700468 uint16_t *s, int p, const uint8_t *blimit0, const uint8_t *limit0,
469 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
470 const uint8_t *thresh1, int bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700471 aom_highbd_lpf_horizontal_4_c(s, p, blimit0, limit0, thresh0, bd);
472 aom_highbd_lpf_horizontal_4_c(s + 8, p, blimit1, limit1, thresh1, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473}
474
Yaowu Xuf883b422016-08-30 14:01:10 -0700475void aom_highbd_lpf_vertical_4_c(uint16_t *s, int pitch, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700476 const uint8_t *limit, const uint8_t *thresh,
477 int bd) {
478 int i;
479
480 // loop filter designed to work using chars so that we can make maximum use
481 // of 8 bit simd instructions.
482 for (i = 0; i < 8; ++i) {
483 const uint16_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
484 const uint16_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
485 const int8_t mask =
486 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
487 highbd_filter4(mask, *thresh, s - 2, s - 1, s, s + 1, bd);
488 s += pitch;
489 }
490}
491
Yaowu Xuf883b422016-08-30 14:01:10 -0700492void aom_highbd_lpf_vertical_4_dual_c(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700493 uint16_t *s, int pitch, const uint8_t *blimit0, const uint8_t *limit0,
494 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
495 const uint8_t *thresh1, int bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700496 aom_highbd_lpf_vertical_4_c(s, pitch, blimit0, limit0, thresh0, bd);
497 aom_highbd_lpf_vertical_4_c(s + 8 * pitch, pitch, blimit1, limit1, thresh1,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498 bd);
499}
500
501static INLINE void highbd_filter8(int8_t mask, uint8_t thresh, uint8_t flat,
502 uint16_t *op3, uint16_t *op2, uint16_t *op1,
503 uint16_t *op0, uint16_t *oq0, uint16_t *oq1,
504 uint16_t *oq2, uint16_t *oq3, int bd) {
505 if (flat && mask) {
506 const uint16_t p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
507 const uint16_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3;
508
509 // 7-tap filter [1, 1, 1, 2, 1, 1, 1]
510 *op2 = ROUND_POWER_OF_TWO(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3);
511 *op1 = ROUND_POWER_OF_TWO(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3);
512 *op0 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3);
513 *oq0 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3);
514 *oq1 = ROUND_POWER_OF_TWO(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3);
515 *oq2 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3);
516 } else {
517 highbd_filter4(mask, thresh, op1, op0, oq0, oq1, bd);
518 }
519}
520
Yaowu Xuf883b422016-08-30 14:01:10 -0700521void aom_highbd_lpf_horizontal_8_c(uint16_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522 const uint8_t *limit, const uint8_t *thresh,
523 int bd) {
524 int i;
525
526 // loop filter designed to work using chars so that we can make maximum use
527 // of 8 bit simd instructions.
528 for (i = 0; i < 8; ++i) {
529 const uint16_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
530 const uint16_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
531
532 const int8_t mask =
533 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
534 const int8_t flat =
535 highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3, bd);
536 highbd_filter8(mask, *thresh, flat, s - 4 * p, s - 3 * p, s - 2 * p,
537 s - 1 * p, s, s + 1 * p, s + 2 * p, s + 3 * p, bd);
538 ++s;
539 }
540}
541
Yaowu Xuf883b422016-08-30 14:01:10 -0700542void aom_highbd_lpf_horizontal_8_dual_c(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543 uint16_t *s, int p, const uint8_t *blimit0, const uint8_t *limit0,
544 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
545 const uint8_t *thresh1, int bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700546 aom_highbd_lpf_horizontal_8_c(s, p, blimit0, limit0, thresh0, bd);
547 aom_highbd_lpf_horizontal_8_c(s + 8, p, blimit1, limit1, thresh1, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700548}
549
Yaowu Xuf883b422016-08-30 14:01:10 -0700550void aom_highbd_lpf_vertical_8_c(uint16_t *s, int pitch, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 const uint8_t *limit, const uint8_t *thresh,
552 int bd) {
553 int i;
554
555 for (i = 0; i < 8; ++i) {
556 const uint16_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
557 const uint16_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
558 const int8_t mask =
559 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
560 const int8_t flat =
561 highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3, bd);
562 highbd_filter8(mask, *thresh, flat, s - 4, s - 3, s - 2, s - 1, s, s + 1,
563 s + 2, s + 3, bd);
564 s += pitch;
565 }
566}
567
Yaowu Xuf883b422016-08-30 14:01:10 -0700568void aom_highbd_lpf_vertical_8_dual_c(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700569 uint16_t *s, int pitch, const uint8_t *blimit0, const uint8_t *limit0,
570 const uint8_t *thresh0, const uint8_t *blimit1, const uint8_t *limit1,
571 const uint8_t *thresh1, int bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700572 aom_highbd_lpf_vertical_8_c(s, pitch, blimit0, limit0, thresh0, bd);
573 aom_highbd_lpf_vertical_8_c(s + 8 * pitch, pitch, blimit1, limit1, thresh1,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700574 bd);
575}
576
577static INLINE void highbd_filter16(int8_t mask, uint8_t thresh, uint8_t flat,
578 uint8_t flat2, uint16_t *op7, uint16_t *op6,
579 uint16_t *op5, uint16_t *op4, uint16_t *op3,
580 uint16_t *op2, uint16_t *op1, uint16_t *op0,
581 uint16_t *oq0, uint16_t *oq1, uint16_t *oq2,
582 uint16_t *oq3, uint16_t *oq4, uint16_t *oq5,
583 uint16_t *oq6, uint16_t *oq7, int bd) {
584 if (flat2 && flat && mask) {
585 const uint16_t p7 = *op7;
586 const uint16_t p6 = *op6;
587 const uint16_t p5 = *op5;
588 const uint16_t p4 = *op4;
589 const uint16_t p3 = *op3;
590 const uint16_t p2 = *op2;
591 const uint16_t p1 = *op1;
592 const uint16_t p0 = *op0;
593 const uint16_t q0 = *oq0;
594 const uint16_t q1 = *oq1;
595 const uint16_t q2 = *oq2;
596 const uint16_t q3 = *oq3;
597 const uint16_t q4 = *oq4;
598 const uint16_t q5 = *oq5;
599 const uint16_t q6 = *oq6;
600 const uint16_t q7 = *oq7;
601
602 // 15-tap filter [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1]
603 *op6 = ROUND_POWER_OF_TWO(
604 p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 + q0, 4);
605 *op5 = ROUND_POWER_OF_TWO(
606 p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 + q0 + q1, 4);
607 *op4 = ROUND_POWER_OF_TWO(
608 p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 + q0 + q1 + q2, 4);
609 *op3 = ROUND_POWER_OF_TWO(
610 p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 + q0 + q1 + q2 + q3, 4);
611 *op2 = ROUND_POWER_OF_TWO(
612 p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 + q0 + q1 + q2 + q3 + q4,
613 4);
614 *op1 = ROUND_POWER_OF_TWO(p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 +
615 q0 + q1 + q2 + q3 + q4 + q5,
616 4);
617 *op0 = ROUND_POWER_OF_TWO(p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 +
618 q1 + q2 + q3 + q4 + q5 + q6,
619 4);
620 *oq0 = ROUND_POWER_OF_TWO(p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 +
621 q2 + q3 + q4 + q5 + q6 + q7,
622 4);
623 *oq1 = ROUND_POWER_OF_TWO(p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 +
624 q3 + q4 + q5 + q6 + q7 * 2,
625 4);
626 *oq2 = ROUND_POWER_OF_TWO(
627 p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3,
628 4);
629 *oq3 = ROUND_POWER_OF_TWO(
630 p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4, 4);
631 *oq4 = ROUND_POWER_OF_TWO(
632 p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5, 4);
633 *oq5 = ROUND_POWER_OF_TWO(
634 p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6, 4);
635 *oq6 = ROUND_POWER_OF_TWO(
636 p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7, 4);
637 } else {
638 highbd_filter8(mask, thresh, flat, op3, op2, op1, op0, oq0, oq1, oq2, oq3,
639 bd);
640 }
641}
642
643static void highbd_mb_lpf_horizontal_edge_w(uint16_t *s, int p,
644 const uint8_t *blimit,
645 const uint8_t *limit,
646 const uint8_t *thresh, int count,
647 int bd) {
648 int i;
649
650 // loop filter designed to work using chars so that we can make maximum use
651 // of 8 bit simd instructions.
652 for (i = 0; i < 8 * count; ++i) {
653 const uint16_t p3 = s[-4 * p];
654 const uint16_t p2 = s[-3 * p];
655 const uint16_t p1 = s[-2 * p];
656 const uint16_t p0 = s[-p];
657 const uint16_t q0 = s[0 * p];
658 const uint16_t q1 = s[1 * p];
659 const uint16_t q2 = s[2 * p];
660 const uint16_t q3 = s[3 * p];
661 const int8_t mask =
662 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
663 const int8_t flat =
664 highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3, bd);
665 const int8_t flat2 =
666 highbd_flat_mask5(1, s[-8 * p], s[-7 * p], s[-6 * p], s[-5 * p], p0, q0,
667 s[4 * p], s[5 * p], s[6 * p], s[7 * p], bd);
668
669 highbd_filter16(mask, *thresh, flat, flat2, s - 8 * p, s - 7 * p, s - 6 * p,
670 s - 5 * p, s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p, s,
671 s + 1 * p, s + 2 * p, s + 3 * p, s + 4 * p, s + 5 * p,
672 s + 6 * p, s + 7 * p, bd);
673 ++s;
674 }
675}
676
Yaowu Xuf883b422016-08-30 14:01:10 -0700677void aom_highbd_lpf_horizontal_edge_8_c(uint16_t *s, int p,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700678 const uint8_t *blimit,
679 const uint8_t *limit,
680 const uint8_t *thresh, int bd) {
681 highbd_mb_lpf_horizontal_edge_w(s, p, blimit, limit, thresh, 1, bd);
682}
683
Yaowu Xuf883b422016-08-30 14:01:10 -0700684void aom_highbd_lpf_horizontal_edge_16_c(uint16_t *s, int p,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700685 const uint8_t *blimit,
686 const uint8_t *limit,
687 const uint8_t *thresh, int bd) {
688 highbd_mb_lpf_horizontal_edge_w(s, p, blimit, limit, thresh, 2, bd);
689}
690
691static void highbd_mb_lpf_vertical_edge_w(uint16_t *s, int p,
692 const uint8_t *blimit,
693 const uint8_t *limit,
694 const uint8_t *thresh, int count,
695 int bd) {
696 int i;
697
698 for (i = 0; i < count; ++i) {
699 const uint16_t p3 = s[-4];
700 const uint16_t p2 = s[-3];
701 const uint16_t p1 = s[-2];
702 const uint16_t p0 = s[-1];
703 const uint16_t q0 = s[0];
704 const uint16_t q1 = s[1];
705 const uint16_t q2 = s[2];
706 const uint16_t q3 = s[3];
707 const int8_t mask =
708 highbd_filter_mask(*limit, *blimit, p3, p2, p1, p0, q0, q1, q2, q3, bd);
709 const int8_t flat =
710 highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3, bd);
711 const int8_t flat2 = highbd_flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0,
712 q0, s[4], s[5], s[6], s[7], bd);
713
714 highbd_filter16(mask, *thresh, flat, flat2, s - 8, s - 7, s - 6, s - 5,
715 s - 4, s - 3, s - 2, s - 1, s, s + 1, s + 2, s + 3, s + 4,
716 s + 5, s + 6, s + 7, bd);
717 s += p;
718 }
719}
720
Yaowu Xuf883b422016-08-30 14:01:10 -0700721void aom_highbd_lpf_vertical_16_c(uint16_t *s, int p, const uint8_t *blimit,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722 const uint8_t *limit, const uint8_t *thresh,
723 int bd) {
724 highbd_mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 8, bd);
725}
726
Yaowu Xuf883b422016-08-30 14:01:10 -0700727void aom_highbd_lpf_vertical_16_dual_c(uint16_t *s, int p,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728 const uint8_t *blimit,
729 const uint8_t *limit,
730 const uint8_t *thresh, int bd) {
731 highbd_mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 16, bd);
732}
Yaowu Xuf883b422016-08-30 14:01:10 -0700733#endif // CONFIG_AOM_HIGHBITDEPTH