blob: 76d65ad2dcd5939d1145242888cd55bcc711b46b [file] [log] [blame]
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07001/*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010011
12#include <math.h>
13#include <stdlib.h>
14
Tom Finegan44702c82018-05-22 13:00:39 -070015#include "config/aom_dsp_rtcd.h"
16#include "config/av1_rtcd.h"
17
Tom Finegandd3e2a52018-05-23 14:33:09 -070018#include "av1/common/cdef.h"
Yaowu Xu253c0012016-08-15 10:27:19 -070019
20/* Generated from gen_filter_tables.c. */
Yaowu Xuc4661822017-10-12 10:00:22 -070021DECLARE_ALIGNED(16, const int, cdef_directions[8][2]) = {
Steinar Midtskogen59782122017-07-20 08:49:43 +020022 { -1 * CDEF_BSTRIDE + 1, -2 * CDEF_BSTRIDE + 2 },
23 { 0 * CDEF_BSTRIDE + 1, -1 * CDEF_BSTRIDE + 2 },
24 { 0 * CDEF_BSTRIDE + 1, 0 * CDEF_BSTRIDE + 2 },
25 { 0 * CDEF_BSTRIDE + 1, 1 * CDEF_BSTRIDE + 2 },
26 { 1 * CDEF_BSTRIDE + 1, 2 * CDEF_BSTRIDE + 2 },
27 { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 1 },
28 { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 0 },
29 { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE - 1 }
30};
Yaowu Xu253c0012016-08-15 10:27:19 -070031
Yaowu Xu253c0012016-08-15 10:27:19 -070032/* Detect direction. 0 means 45-degree up-right, 2 is horizontal, and so on.
33 The search minimizes the weighted variance along all the lines in a
34 particular direction, i.e. the squared error between the input and a
35 "predicted" block where each pixel is replaced by the average along a line
36 in a particular direction. Since each direction have the same sum(x^2) term,
37 that term is never computed. See Section 2, step 2, of:
38 http://jmvalin.ca/notes/intra_paint.pdf */
Steinar Midtskogen94de0aa2017-08-02 10:30:12 +020039int cdef_find_dir_c(const uint16_t *img, int stride, int32_t *var,
40 int coeff_shift) {
Yaowu Xu253c0012016-08-15 10:27:19 -070041 int i;
clang-format21a0c2c2016-08-18 15:10:22 -070042 int32_t cost[8] = { 0 };
43 int partial[8][15] = { { 0 } };
Yaowu Xu253c0012016-08-15 10:27:19 -070044 int32_t best_cost = 0;
45 int best_dir = 0;
46 /* Instead of dividing by n between 2 and 8, we multiply by 3*5*7*8/n.
47 The output is then 840 times larger, but we don't care for finding
48 the max. */
clang-format21a0c2c2016-08-18 15:10:22 -070049 static const int div_table[] = { 0, 840, 420, 280, 210, 168, 140, 120, 105 };
Yaowu Xu253c0012016-08-15 10:27:19 -070050 for (i = 0; i < 8; i++) {
51 int j;
52 for (j = 0; j < 8; j++) {
53 int x;
54 /* We subtract 128 here to reduce the maximum range of the squared
55 partial sums. */
clang-format21a0c2c2016-08-18 15:10:22 -070056 x = (img[i * stride + j] >> coeff_shift) - 128;
Yaowu Xu253c0012016-08-15 10:27:19 -070057 partial[0][i + j] += x;
clang-format21a0c2c2016-08-18 15:10:22 -070058 partial[1][i + j / 2] += x;
Yaowu Xu253c0012016-08-15 10:27:19 -070059 partial[2][i] += x;
clang-format21a0c2c2016-08-18 15:10:22 -070060 partial[3][3 + i - j / 2] += x;
Yaowu Xu253c0012016-08-15 10:27:19 -070061 partial[4][7 + i - j] += x;
clang-format21a0c2c2016-08-18 15:10:22 -070062 partial[5][3 - i / 2 + j] += x;
Yaowu Xu253c0012016-08-15 10:27:19 -070063 partial[6][j] += x;
clang-format21a0c2c2016-08-18 15:10:22 -070064 partial[7][i / 2 + j] += x;
Yaowu Xu253c0012016-08-15 10:27:19 -070065 }
66 }
67 for (i = 0; i < 8; i++) {
clang-format21a0c2c2016-08-18 15:10:22 -070068 cost[2] += partial[2][i] * partial[2][i];
69 cost[6] += partial[6][i] * partial[6][i];
Yaowu Xu253c0012016-08-15 10:27:19 -070070 }
71 cost[2] *= div_table[8];
72 cost[6] *= div_table[8];
73 for (i = 0; i < 7; i++) {
clang-format21a0c2c2016-08-18 15:10:22 -070074 cost[0] += (partial[0][i] * partial[0][i] +
75 partial[0][14 - i] * partial[0][14 - i]) *
76 div_table[i + 1];
77 cost[4] += (partial[4][i] * partial[4][i] +
78 partial[4][14 - i] * partial[4][14 - i]) *
79 div_table[i + 1];
Yaowu Xu253c0012016-08-15 10:27:19 -070080 }
clang-format21a0c2c2016-08-18 15:10:22 -070081 cost[0] += partial[0][7] * partial[0][7] * div_table[8];
82 cost[4] += partial[4][7] * partial[4][7] * div_table[8];
Yaowu Xu253c0012016-08-15 10:27:19 -070083 for (i = 1; i < 8; i += 2) {
84 int j;
85 for (j = 0; j < 4 + 1; j++) {
clang-format21a0c2c2016-08-18 15:10:22 -070086 cost[i] += partial[i][3 + j] * partial[i][3 + j];
Yaowu Xu253c0012016-08-15 10:27:19 -070087 }
88 cost[i] *= div_table[8];
89 for (j = 0; j < 4 - 1; j++) {
clang-format21a0c2c2016-08-18 15:10:22 -070090 cost[i] += (partial[i][j] * partial[i][j] +
91 partial[i][10 - j] * partial[i][10 - j]) *
92 div_table[2 * j + 2];
Yaowu Xu253c0012016-08-15 10:27:19 -070093 }
94 }
95 for (i = 0; i < 8; i++) {
96 if (cost[i] > best_cost) {
97 best_cost = cost[i];
98 best_dir = i;
99 }
100 }
101 /* Difference between the optimal variance and the variance along the
102 orthogonal direction. Again, the sum(x^2) terms cancel out. */
103 *var = best_cost - cost[(best_dir + 4) & 7];
104 /* We'd normally divide by 840, but dividing by 1024 is close enough
105 for what we're going to do with this. */
106 *var >>= 10;
107 return best_dir;
108}
109
Steinar Midtskogen59782122017-07-20 08:49:43 +0200110const int cdef_pri_taps[2][2] = { { 4, 2 }, { 3, 3 } };
111const int cdef_sec_taps[2][2] = { { 2, 1 }, { 2, 1 } };
Steinar Midtskogen59782122017-07-20 08:49:43 +0200112
113/* Smooth in the direction detected. */
Steinar Midtskogen59782122017-07-20 08:49:43 +0200114void cdef_filter_block_c(uint8_t *dst8, uint16_t *dst16, int dstride,
115 const uint16_t *in, int pri_strength, int sec_strength,
116 int dir, int pri_damping, int sec_damping, int bsize,
Johann83607dc2018-11-02 11:50:31 -0700117 int coeff_shift) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200118 int i, j, k;
119 const int s = CDEF_BSTRIDE;
Steinar Midtskogenbc753052017-10-23 12:32:10 +0200120 const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
121 const int *sec_taps = cdef_sec_taps[(pri_strength >> coeff_shift) & 1];
Steinar Midtskogenab6c9c72017-11-30 13:38:26 +0100122 for (i = 0; i < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_4X8); i++) {
123 for (j = 0; j < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_8X4); j++) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200124 int16_t sum = 0;
125 int16_t y;
126 int16_t x = in[i * s + j];
Steinar Midtskogen59782122017-07-20 08:49:43 +0200127 int max = x;
128 int min = x;
Steinar Midtskogen612eb2e2018-01-02 15:50:50 +0100129 for (k = 0; k < 2; k++) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200130 int16_t p0 = in[i * s + j + cdef_directions[dir][k]];
131 int16_t p1 = in[i * s + j - cdef_directions[dir][k]];
132 sum += pri_taps[k] * constrain(p0 - x, pri_strength, pri_damping);
133 sum += pri_taps[k] * constrain(p1 - x, pri_strength, pri_damping);
Steinar Midtskogen59782122017-07-20 08:49:43 +0200134 if (p0 != CDEF_VERY_LARGE) max = AOMMAX(p0, max);
135 if (p1 != CDEF_VERY_LARGE) max = AOMMAX(p1, max);
136 min = AOMMIN(p0, min);
137 min = AOMMIN(p1, min);
Steinar Midtskogen59782122017-07-20 08:49:43 +0200138 int16_t s0 = in[i * s + j + cdef_directions[(dir + 2) & 7][k]];
139 int16_t s1 = in[i * s + j - cdef_directions[(dir + 2) & 7][k]];
140 int16_t s2 = in[i * s + j + cdef_directions[(dir + 6) & 7][k]];
141 int16_t s3 = in[i * s + j - cdef_directions[(dir + 6) & 7][k]];
Steinar Midtskogen59782122017-07-20 08:49:43 +0200142 if (s0 != CDEF_VERY_LARGE) max = AOMMAX(s0, max);
143 if (s1 != CDEF_VERY_LARGE) max = AOMMAX(s1, max);
144 if (s2 != CDEF_VERY_LARGE) max = AOMMAX(s2, max);
145 if (s3 != CDEF_VERY_LARGE) max = AOMMAX(s3, max);
146 min = AOMMIN(s0, min);
147 min = AOMMIN(s1, min);
148 min = AOMMIN(s2, min);
149 min = AOMMIN(s3, min);
Steinar Midtskogen59782122017-07-20 08:49:43 +0200150 sum += sec_taps[k] * constrain(s0 - x, sec_strength, sec_damping);
151 sum += sec_taps[k] * constrain(s1 - x, sec_strength, sec_damping);
152 sum += sec_taps[k] * constrain(s2 - x, sec_strength, sec_damping);
153 sum += sec_taps[k] * constrain(s3 - x, sec_strength, sec_damping);
154 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200155 y = clamp((int16_t)x + ((8 + sum - (sum < 0)) >> 4), min, max);
Steinar Midtskogen59782122017-07-20 08:49:43 +0200156 if (dst8)
157 dst8[i * dstride + j] = (uint8_t)y;
158 else
159 dst16[i * dstride + j] = (uint16_t)y;
160 }
161 }
162}
163
Steinar Midtskogen94de0aa2017-08-02 10:30:12 +0200164/* Compute the primary filter strength for an 8x8 block based on the
165 directional variance difference. A high variance difference means
166 that we have a highly directional pattern (e.g. a high contrast
167 edge), so we can apply more deringing. A low variance means that we
168 either have a low contrast edge, or a non-directional texture, so
169 we want to be careful not to blur. */
170static INLINE int adjust_strength(int strength, int32_t var) {
Steinar Midtskogenfade4632017-04-14 20:29:05 +0200171 const int i = var >> 6 ? AOMMIN(get_msb(var >> 6), 12) : 0;
Steinar Midtskogen94de0aa2017-08-02 10:30:12 +0200172 /* We use the variance of 8x8 blocks to adjust the strength. */
173 return var ? (strength * (4 + i) + 8) >> 4 : 0;
Yaowu Xu253c0012016-08-15 10:27:19 -0700174}
175
Hui Su584ba482019-06-19 11:48:05 -0700176void av1_cdef_filter_fb(uint8_t *dst8, uint16_t *dst16, int dstride,
177 uint16_t *in, int xdec, int ydec,
178 int dir[CDEF_NBLOCKS][CDEF_NBLOCKS], int *dirinit,
179 int var[CDEF_NBLOCKS][CDEF_NBLOCKS], int pli,
180 cdef_list *dlist, int cdef_count, int level,
181 int sec_strength, int damping, int coeff_shift) {
Jean-Marc Valin3e44bcc2016-10-11 16:53:59 -0400182 int bi;
Yaowu Xu253c0012016-08-15 10:27:19 -0700183 int bx;
184 int by;
Hui Su3af4d132019-06-19 16:03:54 -0700185 const int pri_strength = level << coeff_shift;
Steinar Midtskogen830d2ac2017-10-21 08:07:19 +0200186 sec_strength <<= coeff_shift;
Hui Su584ba482019-06-19 11:48:05 -0700187 damping += coeff_shift - (pli != AOM_PLANE_Y);
Hui Su3af4d132019-06-19 16:03:54 -0700188 const int bw_log2 = 3 - xdec;
189 const int bh_log2 = 3 - ydec;
Steinar Midtskogen8322ff02017-12-20 15:39:52 +0100190 if (dirinit && pri_strength == 0 && sec_strength == 0) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200191 // If we're here, both primary and secondary strengths are 0, and
192 // we still haven't written anything to y[] yet, so we just copy
193 // the input to y[]. This is necessary only for av1_cdef_search()
194 // and only av1_cdef_search() sets dirinit.
195 for (bi = 0; bi < cdef_count; bi++) {
196 by = dlist[bi].by;
197 bx = dlist[bi].bx;
Steinar Midtskogen95a2f862017-04-07 09:24:02 +0200198 // TODO(stemidts/jmvalin): SIMD optimisations
Hui Su3af4d132019-06-19 16:03:54 -0700199 for (int iy = 0; iy < 1 << bh_log2; iy++) {
200 memcpy(&dst16[(bi << (bw_log2 + bh_log2)) + (iy << bw_log2)],
201 &in[((by << bh_log2) + iy) * CDEF_BSTRIDE + (bx << bw_log2)],
Fyodor Kyslove08fbd22019-06-28 10:49:19 -0700202 ((size_t)1 << bw_log2) * sizeof(*dst16));
Hui Su3af4d132019-06-19 16:03:54 -0700203 }
Steinar Midtskogen95a2f862017-04-07 09:24:02 +0200204 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200205 return;
Steinar Midtskogena9d41e82017-03-17 12:48:15 +0100206 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200207
Steinar Midtskogen59782122017-07-20 08:49:43 +0200208 if (pli == 0) {
209 if (!dirinit || !*dirinit) {
210 for (bi = 0; bi < cdef_count; bi++) {
211 by = dlist[bi].by;
212 bx = dlist[bi].bx;
213 dir[by][bx] = cdef_find_dir(&in[8 * by * CDEF_BSTRIDE + 8 * bx],
214 CDEF_BSTRIDE, &var[by][bx], coeff_shift);
215 }
216 if (dirinit) *dirinit = 1;
217 }
218 }
Steinar Midtskogenab6c9c72017-11-30 13:38:26 +0100219 if (pli == 1 && xdec != ydec) {
220 for (bi = 0; bi < cdef_count; bi++) {
221 static const int conv422[8] = { 7, 0, 2, 4, 5, 6, 6, 6 };
222 static const int conv440[8] = { 1, 2, 2, 2, 3, 4, 6, 0 };
223 by = dlist[bi].by;
224 bx = dlist[bi].bx;
225 dir[by][bx] = (xdec ? conv422 : conv440)[dir[by][bx]];
226 }
227 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200228
Hui Su3af4d132019-06-19 16:03:54 -0700229 const int bsize =
230 ydec ? (xdec ? BLOCK_4X4 : BLOCK_8X4) : (xdec ? BLOCK_4X8 : BLOCK_8X8);
231 const int t = pri_strength;
232 const int s = sec_strength;
Steinar Midtskogen59782122017-07-20 08:49:43 +0200233 for (bi = 0; bi < cdef_count; bi++) {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200234 by = dlist[bi].by;
235 bx = dlist[bi].bx;
Hui Su3af4d132019-06-19 16:03:54 -0700236 if (dst8) {
Johann83607dc2018-11-02 11:50:31 -0700237 cdef_filter_block(
Hui Su3af4d132019-06-19 16:03:54 -0700238 &dst8[(by << bh_log2) * dstride + (bx << bw_log2)], NULL, dstride,
239 &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)],
Johann83607dc2018-11-02 11:50:31 -0700240 (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
Hui Su584ba482019-06-19 11:48:05 -0700241 damping, damping, bsize, coeff_shift);
Hui Su3af4d132019-06-19 16:03:54 -0700242 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +0200243 cdef_filter_block(
clang-format4eafefe2017-09-04 12:51:20 -0700244 NULL,
Hui Su3af4d132019-06-19 16:03:54 -0700245 &dst16[dirinit ? bi << (bw_log2 + bh_log2)
246 : (by << bh_log2) * dstride + (bx << bw_log2)],
247 dirinit ? 1 << bw_log2 : dstride,
248 &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)],
Steinar Midtskogen59782122017-07-20 08:49:43 +0200249 (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
Hui Su584ba482019-06-19 11:48:05 -0700250 damping, damping, bsize, coeff_shift);
Hui Su3af4d132019-06-19 16:03:54 -0700251 }
Steinar Midtskogen59782122017-07-20 08:49:43 +0200252 }
Yaowu Xu253c0012016-08-15 10:27:19 -0700253}