blob: 7387360b4b592630201b1047f78d9a92619080dd [file] [log] [blame]
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07001/*
Lester Lu6bc30d62021-12-16 19:13:21 +00002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07003 *
Lester Lu6bc30d62021-12-16 19:13:21 +00004 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * aomedia.org/license/patent-license/.
Yaowu Xu2ab7ff02016-09-02 12:04:54 -070011 */
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010012
13#include <math.h>
14#include <stdlib.h>
15
Tom Finegan44702c82018-05-22 13:00:39 -070016#include "config/aom_dsp_rtcd.h"
17#include "config/av1_rtcd.h"
18
Tom Finegandd3e2a52018-05-23 14:33:09 -070019#include "av1/common/cdef.h"
Yaowu Xu253c0012016-08-15 10:27:19 -070020
21/* Generated from gen_filter_tables.c. */
Yaowu Xuc4661822017-10-12 10:00:22 -070022DECLARE_ALIGNED(16, const int, cdef_directions[8][2]) = {
Steinar Midtskogen59782122017-07-20 08:49:43 +020023 { -1 * CDEF_BSTRIDE + 1, -2 * CDEF_BSTRIDE + 2 },
24 { 0 * CDEF_BSTRIDE + 1, -1 * CDEF_BSTRIDE + 2 },
25 { 0 * CDEF_BSTRIDE + 1, 0 * CDEF_BSTRIDE + 2 },
26 { 0 * CDEF_BSTRIDE + 1, 1 * CDEF_BSTRIDE + 2 },
27 { 1 * CDEF_BSTRIDE + 1, 2 * CDEF_BSTRIDE + 2 },
28 { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 1 },
29 { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 0 },
30 { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE - 1 }
31};
Yaowu Xu253c0012016-08-15 10:27:19 -070032
Yaowu Xu253c0012016-08-15 10:27:19 -070033/* Detect direction. 0 means 45-degree up-right, 2 is horizontal, and so on.
34 The search minimizes the weighted variance along all the lines in a
35 particular direction, i.e. the squared error between the input and a
36 "predicted" block where each pixel is replaced by the average along a line
37 in a particular direction. Since each direction have the same sum(x^2) term,
38 that term is never computed. See Section 2, step 2, of:
39 http://jmvalin.ca/notes/intra_paint.pdf */
Steinar Midtskogen94de0aa2017-08-02 10:30:12 +020040int cdef_find_dir_c(const uint16_t *img, int stride, int32_t *var,
41 int coeff_shift) {
Yaowu Xu253c0012016-08-15 10:27:19 -070042 int i;
clang-format21a0c2c2016-08-18 15:10:22 -070043 int32_t cost[8] = { 0 };
44 int partial[8][15] = { { 0 } };
Yaowu Xu253c0012016-08-15 10:27:19 -070045 int32_t best_cost = 0;
46 int best_dir = 0;
47 /* Instead of dividing by n between 2 and 8, we multiply by 3*5*7*8/n.
48 The output is then 840 times larger, but we don't care for finding
49 the max. */
clang-format21a0c2c2016-08-18 15:10:22 -070050 static const int div_table[] = { 0, 840, 420, 280, 210, 168, 140, 120, 105 };
Yaowu Xu253c0012016-08-15 10:27:19 -070051 for (i = 0; i < 8; i++) {
52 int j;
53 for (j = 0; j < 8; j++) {
54 int x;
55 /* We subtract 128 here to reduce the maximum range of the squared
56 partial sums. */
clang-format21a0c2c2016-08-18 15:10:22 -070057 x = (img[i * stride + j] >> coeff_shift) - 128;
Yaowu Xu253c0012016-08-15 10:27:19 -070058 partial[0][i + j] += x;
clang-format21a0c2c2016-08-18 15:10:22 -070059 partial[1][i + j / 2] += x;
Yaowu Xu253c0012016-08-15 10:27:19 -070060 partial[2][i] += x;
clang-format21a0c2c2016-08-18 15:10:22 -070061 partial[3][3 + i - j / 2] += x;
Yaowu Xu253c0012016-08-15 10:27:19 -070062 partial[4][7 + i - j] += x;
clang-format21a0c2c2016-08-18 15:10:22 -070063 partial[5][3 - i / 2 + j] += x;
Yaowu Xu253c0012016-08-15 10:27:19 -070064 partial[6][j] += x;
clang-format21a0c2c2016-08-18 15:10:22 -070065 partial[7][i / 2 + j] += x;
Yaowu Xu253c0012016-08-15 10:27:19 -070066 }
67 }
68 for (i = 0; i < 8; i++) {
clang-format21a0c2c2016-08-18 15:10:22 -070069 cost[2] += partial[2][i] * partial[2][i];
70 cost[6] += partial[6][i] * partial[6][i];
Yaowu Xu253c0012016-08-15 10:27:19 -070071 }
72 cost[2] *= div_table[8];
73 cost[6] *= div_table[8];
74 for (i = 0; i < 7; i++) {
clang-format21a0c2c2016-08-18 15:10:22 -070075 cost[0] += (partial[0][i] * partial[0][i] +
76 partial[0][14 - i] * partial[0][14 - i]) *
77 div_table[i + 1];
78 cost[4] += (partial[4][i] * partial[4][i] +
79 partial[4][14 - i] * partial[4][14 - i]) *
80 div_table[i + 1];
Yaowu Xu253c0012016-08-15 10:27:19 -070081 }
clang-format21a0c2c2016-08-18 15:10:22 -070082 cost[0] += partial[0][7] * partial[0][7] * div_table[8];
83 cost[4] += partial[4][7] * partial[4][7] * div_table[8];
Yaowu Xu253c0012016-08-15 10:27:19 -070084 for (i = 1; i < 8; i += 2) {
85 int j;
86 for (j = 0; j < 4 + 1; j++) {
clang-format21a0c2c2016-08-18 15:10:22 -070087 cost[i] += partial[i][3 + j] * partial[i][3 + j];
Yaowu Xu253c0012016-08-15 10:27:19 -070088 }
89 cost[i] *= div_table[8];
90 for (j = 0; j < 4 - 1; j++) {
clang-format21a0c2c2016-08-18 15:10:22 -070091 cost[i] += (partial[i][j] * partial[i][j] +
92 partial[i][10 - j] * partial[i][10 - j]) *
93 div_table[2 * j + 2];
Yaowu Xu253c0012016-08-15 10:27:19 -070094 }
95 }
96 for (i = 0; i < 8; i++) {
97 if (cost[i] > best_cost) {
98 best_cost = cost[i];
99 best_dir = i;
100 }
101 }
102 /* Difference between the optimal variance and the variance along the
103 orthogonal direction. Again, the sum(x^2) terms cancel out. */
104 *var = best_cost - cost[(best_dir + 4) & 7];
105 /* We'd normally divide by 840, but dividing by 1024 is close enough
106 for what we're going to do with this. */
107 *var >>= 10;
108 return best_dir;
109}
110
Steinar Midtskogen59782122017-07-20 08:49:43 +0200111const int cdef_pri_taps[2][2] = { { 4, 2 }, { 3, 3 } };
Johannecfb3152019-10-10 10:15:17 -0700112const int cdef_sec_taps[2] = { 2, 1 };
Steinar Midtskogen59782122017-07-20 08:49:43 +0200113
114/* Smooth in the direction detected. */
Steinar Midtskogen59782122017-07-20 08:49:43 +0200115void cdef_filter_block_c(uint8_t *dst8, uint16_t *dst16, int dstride,
116 const uint16_t *in, int pri_strength, int sec_strength,
117 int dir, int pri_damping, int sec_damping, int bsize,
Johann83607dc2018-11-02 11:50:31 -0700118 int coeff_shift) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200119 int i, j, k;
120 const int s = CDEF_BSTRIDE;
Steinar Midtskogenbc753052017-10-23 12:32:10 +0200121 const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
Johannecfb3152019-10-10 10:15:17 -0700122 const int *sec_taps = cdef_sec_taps;
Steinar Midtskogenab6c9c72017-11-30 13:38:26 +0100123 for (i = 0; i < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_4X8); i++) {
124 for (j = 0; j < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_8X4); j++) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200125 int16_t sum = 0;
126 int16_t y;
127 int16_t x = in[i * s + j];
Steinar Midtskogen59782122017-07-20 08:49:43 +0200128 int max = x;
129 int min = x;
Steinar Midtskogen612eb2e2018-01-02 15:50:50 +0100130 for (k = 0; k < 2; k++) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200131 int16_t p0 = in[i * s + j + cdef_directions[dir][k]];
132 int16_t p1 = in[i * s + j - cdef_directions[dir][k]];
133 sum += pri_taps[k] * constrain(p0 - x, pri_strength, pri_damping);
134 sum += pri_taps[k] * constrain(p1 - x, pri_strength, pri_damping);
Steinar Midtskogen59782122017-07-20 08:49:43 +0200135 if (p0 != CDEF_VERY_LARGE) max = AOMMAX(p0, max);
136 if (p1 != CDEF_VERY_LARGE) max = AOMMAX(p1, max);
137 min = AOMMIN(p0, min);
138 min = AOMMIN(p1, min);
Steinar Midtskogen59782122017-07-20 08:49:43 +0200139 int16_t s0 = in[i * s + j + cdef_directions[(dir + 2) & 7][k]];
140 int16_t s1 = in[i * s + j - cdef_directions[(dir + 2) & 7][k]];
141 int16_t s2 = in[i * s + j + cdef_directions[(dir + 6) & 7][k]];
142 int16_t s3 = in[i * s + j - cdef_directions[(dir + 6) & 7][k]];
Steinar Midtskogen59782122017-07-20 08:49:43 +0200143 if (s0 != CDEF_VERY_LARGE) max = AOMMAX(s0, max);
144 if (s1 != CDEF_VERY_LARGE) max = AOMMAX(s1, max);
145 if (s2 != CDEF_VERY_LARGE) max = AOMMAX(s2, max);
146 if (s3 != CDEF_VERY_LARGE) max = AOMMAX(s3, max);
147 min = AOMMIN(s0, min);
148 min = AOMMIN(s1, min);
149 min = AOMMIN(s2, min);
150 min = AOMMIN(s3, min);
Steinar Midtskogen59782122017-07-20 08:49:43 +0200151 sum += sec_taps[k] * constrain(s0 - x, sec_strength, sec_damping);
152 sum += sec_taps[k] * constrain(s1 - x, sec_strength, sec_damping);
153 sum += sec_taps[k] * constrain(s2 - x, sec_strength, sec_damping);
154 sum += sec_taps[k] * constrain(s3 - x, sec_strength, sec_damping);
155 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200156 y = clamp((int16_t)x + ((8 + sum - (sum < 0)) >> 4), min, max);
Steinar Midtskogen59782122017-07-20 08:49:43 +0200157 if (dst8)
158 dst8[i * dstride + j] = (uint8_t)y;
159 else
160 dst16[i * dstride + j] = (uint16_t)y;
161 }
162 }
163}
164
Steinar Midtskogen94de0aa2017-08-02 10:30:12 +0200165/* Compute the primary filter strength for an 8x8 block based on the
166 directional variance difference. A high variance difference means
167 that we have a highly directional pattern (e.g. a high contrast
168 edge), so we can apply more deringing. A low variance means that we
169 either have a low contrast edge, or a non-directional texture, so
170 we want to be careful not to blur. */
171static INLINE int adjust_strength(int strength, int32_t var) {
Steinar Midtskogenfade4632017-04-14 20:29:05 +0200172 const int i = var >> 6 ? AOMMIN(get_msb(var >> 6), 12) : 0;
Steinar Midtskogen94de0aa2017-08-02 10:30:12 +0200173 /* We use the variance of 8x8 blocks to adjust the strength. */
174 return var ? (strength * (4 + i) + 8) >> 4 : 0;
Yaowu Xu253c0012016-08-15 10:27:19 -0700175}
176
Hui Su584ba482019-06-19 11:48:05 -0700177void av1_cdef_filter_fb(uint8_t *dst8, uint16_t *dst16, int dstride,
178 uint16_t *in, int xdec, int ydec,
179 int dir[CDEF_NBLOCKS][CDEF_NBLOCKS], int *dirinit,
180 int var[CDEF_NBLOCKS][CDEF_NBLOCKS], int pli,
181 cdef_list *dlist, int cdef_count, int level,
182 int sec_strength, int damping, int coeff_shift) {
Jean-Marc Valin3e44bcc2016-10-11 16:53:59 -0400183 int bi;
Yaowu Xu253c0012016-08-15 10:27:19 -0700184 int bx;
185 int by;
Hui Su3af4d132019-06-19 16:03:54 -0700186 const int pri_strength = level << coeff_shift;
Steinar Midtskogen830d2ac2017-10-21 08:07:19 +0200187 sec_strength <<= coeff_shift;
Hui Su584ba482019-06-19 11:48:05 -0700188 damping += coeff_shift - (pli != AOM_PLANE_Y);
Hui Su3af4d132019-06-19 16:03:54 -0700189 const int bw_log2 = 3 - xdec;
190 const int bh_log2 = 3 - ydec;
Steinar Midtskogen8322ff02017-12-20 15:39:52 +0100191 if (dirinit && pri_strength == 0 && sec_strength == 0) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200192 // If we're here, both primary and secondary strengths are 0, and
193 // we still haven't written anything to y[] yet, so we just copy
194 // the input to y[]. This is necessary only for av1_cdef_search()
195 // and only av1_cdef_search() sets dirinit.
196 for (bi = 0; bi < cdef_count; bi++) {
197 by = dlist[bi].by;
198 bx = dlist[bi].bx;
Steinar Midtskogen95a2f862017-04-07 09:24:02 +0200199 // TODO(stemidts/jmvalin): SIMD optimisations
Hui Su3af4d132019-06-19 16:03:54 -0700200 for (int iy = 0; iy < 1 << bh_log2; iy++) {
201 memcpy(&dst16[(bi << (bw_log2 + bh_log2)) + (iy << bw_log2)],
202 &in[((by << bh_log2) + iy) * CDEF_BSTRIDE + (bx << bw_log2)],
Fyodor Kyslove08fbd22019-06-28 10:49:19 -0700203 ((size_t)1 << bw_log2) * sizeof(*dst16));
Hui Su3af4d132019-06-19 16:03:54 -0700204 }
Steinar Midtskogen95a2f862017-04-07 09:24:02 +0200205 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200206 return;
Steinar Midtskogena9d41e82017-03-17 12:48:15 +0100207 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200208
Steinar Midtskogen59782122017-07-20 08:49:43 +0200209 if (pli == 0) {
210 if (!dirinit || !*dirinit) {
211 for (bi = 0; bi < cdef_count; bi++) {
212 by = dlist[bi].by;
213 bx = dlist[bi].bx;
214 dir[by][bx] = cdef_find_dir(&in[8 * by * CDEF_BSTRIDE + 8 * bx],
215 CDEF_BSTRIDE, &var[by][bx], coeff_shift);
216 }
217 if (dirinit) *dirinit = 1;
218 }
219 }
Steinar Midtskogenab6c9c72017-11-30 13:38:26 +0100220 if (pli == 1 && xdec != ydec) {
221 for (bi = 0; bi < cdef_count; bi++) {
222 static const int conv422[8] = { 7, 0, 2, 4, 5, 6, 6, 6 };
223 static const int conv440[8] = { 1, 2, 2, 2, 3, 4, 6, 0 };
224 by = dlist[bi].by;
225 bx = dlist[bi].bx;
226 dir[by][bx] = (xdec ? conv422 : conv440)[dir[by][bx]];
227 }
228 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200229
Hui Su3af4d132019-06-19 16:03:54 -0700230 const int bsize =
231 ydec ? (xdec ? BLOCK_4X4 : BLOCK_8X4) : (xdec ? BLOCK_4X8 : BLOCK_8X8);
232 const int t = pri_strength;
233 const int s = sec_strength;
Steinar Midtskogen59782122017-07-20 08:49:43 +0200234 for (bi = 0; bi < cdef_count; bi++) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200235 by = dlist[bi].by;
236 bx = dlist[bi].bx;
Hui Su3af4d132019-06-19 16:03:54 -0700237 if (dst8) {
Johann83607dc2018-11-02 11:50:31 -0700238 cdef_filter_block(
Hui Su3af4d132019-06-19 16:03:54 -0700239 &dst8[(by << bh_log2) * dstride + (bx << bw_log2)], NULL, dstride,
240 &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)],
Johann83607dc2018-11-02 11:50:31 -0700241 (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
Hui Su584ba482019-06-19 11:48:05 -0700242 damping, damping, bsize, coeff_shift);
Hui Su3af4d132019-06-19 16:03:54 -0700243 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200244 cdef_filter_block(
clang-format4eafefe2017-09-04 12:51:20 -0700245 NULL,
Hui Su3af4d132019-06-19 16:03:54 -0700246 &dst16[dirinit ? bi << (bw_log2 + bh_log2)
247 : (by << bh_log2) * dstride + (bx << bw_log2)],
248 dirinit ? 1 << bw_log2 : dstride,
249 &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)],
Steinar Midtskogen59782122017-07-20 08:49:43 +0200250 (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
Hui Su584ba482019-06-19 11:48:05 -0700251 damping, damping, bsize, coeff_shift);
Hui Su3af4d132019-06-19 16:03:54 -0700252 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200253 }
Yaowu Xu253c0012016-08-15 10:27:19 -0700254}