1 /*
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 */
11
12 #include <math.h>
13 #include <stdlib.h>
14
15 #include "config/aom_dsp_rtcd.h"
16 #include "config/av1_rtcd.h"
17
18 #include "av1/common/cdef.h"
19
20 /* Generated from gen_filter_tables.c. */
21 DECLARE_ALIGNED(16, const int, cdef_directions[8][2]) = {
22 { -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 };
31
32 /* 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 */
cdef_find_dir_c(const uint16_t * img,int stride,int32_t * var,int coeff_shift)39 int cdef_find_dir_c(const uint16_t *img, int stride, int32_t *var,
40 int coeff_shift) {
41 int i;
42 int32_t cost[8] = { 0 };
43 int partial[8][15] = { { 0 } };
44 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. */
49 static const int div_table[] = { 0, 840, 420, 280, 210, 168, 140, 120, 105 };
50 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. */
56 x = (img[i * stride + j] >> coeff_shift) - 128;
57 partial[0][i + j] += x;
58 partial[1][i + j / 2] += x;
59 partial[2][i] += x;
60 partial[3][3 + i - j / 2] += x;
61 partial[4][7 + i - j] += x;
62 partial[5][3 - i / 2 + j] += x;
63 partial[6][j] += x;
64 partial[7][i / 2 + j] += x;
65 }
66 }
67 for (i = 0; i < 8; i++) {
68 cost[2] += partial[2][i] * partial[2][i];
69 cost[6] += partial[6][i] * partial[6][i];
70 }
71 cost[2] *= div_table[8];
72 cost[6] *= div_table[8];
73 for (i = 0; i < 7; i++) {
74 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];
80 }
81 cost[0] += partial[0][7] * partial[0][7] * div_table[8];
82 cost[4] += partial[4][7] * partial[4][7] * div_table[8];
83 for (i = 1; i < 8; i += 2) {
84 int j;
85 for (j = 0; j < 4 + 1; j++) {
86 cost[i] += partial[i][3 + j] * partial[i][3 + j];
87 }
88 cost[i] *= div_table[8];
89 for (j = 0; j < 4 - 1; j++) {
90 cost[i] += (partial[i][j] * partial[i][j] +
91 partial[i][10 - j] * partial[i][10 - j]) *
92 div_table[2 * j + 2];
93 }
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
110 const int cdef_pri_taps[2][2] = { { 4, 2 }, { 3, 3 } };
111 const int cdef_sec_taps[2][2] = { { 2, 1 }, { 2, 1 } };
112
113 /* Smooth in the direction detected. */
cdef_filter_block_c(uint8_t * dst8,uint16_t * dst16,int dstride,const uint16_t * in,int pri_strength,int sec_strength,int dir,int pri_damping,int sec_damping,int bsize,int coeff_shift)114 void 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,
117 int coeff_shift) {
118 int i, j, k;
119 const int s = CDEF_BSTRIDE;
120 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];
122 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++) {
124 int16_t sum = 0;
125 int16_t y;
126 int16_t x = in[i * s + j];
127 int max = x;
128 int min = x;
129 for (k = 0; k < 2; k++) {
130 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);
134 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);
138 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]];
142 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);
150 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 }
155 y = clamp((int16_t)x + ((8 + sum - (sum < 0)) >> 4), min, max);
156 if (dst8)
157 dst8[i * dstride + j] = (uint8_t)y;
158 else
159 dst16[i * dstride + j] = (uint16_t)y;
160 }
161 }
162 }
163
164 /* 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. */
adjust_strength(int strength,int32_t var)170 static INLINE int adjust_strength(int strength, int32_t var) {
171 const int i = var >> 6 ? AOMMIN(get_msb(var >> 6), 12) : 0;
172 /* We use the variance of 8x8 blocks to adjust the strength. */
173 return var ? (strength * (4 + i) + 8) >> 4 : 0;
174 }
175
cdef_filter_fb(uint8_t * dst8,uint16_t * dst16,int dstride,uint16_t * in,int xdec,int ydec,int dir[CDEF_NBLOCKS][CDEF_NBLOCKS],int * dirinit,int var[CDEF_NBLOCKS][CDEF_NBLOCKS],int pli,cdef_list * dlist,int cdef_count,int level,int sec_strength,int pri_damping,int sec_damping,int coeff_shift)176 void cdef_filter_fb(uint8_t *dst8, uint16_t *dst16, int dstride, uint16_t *in,
177 int xdec, int ydec, int dir[CDEF_NBLOCKS][CDEF_NBLOCKS],
178 int *dirinit, int var[CDEF_NBLOCKS][CDEF_NBLOCKS], int pli,
179 cdef_list *dlist, int cdef_count, int level,
180 int sec_strength, int pri_damping, int sec_damping,
181 int coeff_shift) {
182 int bi;
183 int bx;
184 int by;
185 int bsize, bsizex, bsizey;
186
187 int pri_strength = level << coeff_shift;
188 sec_strength <<= coeff_shift;
189 sec_damping += coeff_shift - (pli != AOM_PLANE_Y);
190 pri_damping += coeff_shift - (pli != AOM_PLANE_Y);
191 bsize =
192 ydec ? (xdec ? BLOCK_4X4 : BLOCK_8X4) : (xdec ? BLOCK_4X8 : BLOCK_8X8);
193 bsizex = 3 - xdec;
194 bsizey = 3 - ydec;
195 if (dirinit && pri_strength == 0 && sec_strength == 0) {
196 // If we're here, both primary and secondary strengths are 0, and
197 // we still haven't written anything to y[] yet, so we just copy
198 // the input to y[]. This is necessary only for av1_cdef_search()
199 // and only av1_cdef_search() sets dirinit.
200 for (bi = 0; bi < cdef_count; bi++) {
201 by = dlist[bi].by;
202 bx = dlist[bi].bx;
203 int iy, ix;
204 // TODO(stemidts/jmvalin): SIMD optimisations
205 for (iy = 0; iy < 1 << bsizey; iy++)
206 for (ix = 0; ix < 1 << bsizex; ix++)
207 dst16[(bi << (bsizex + bsizey)) + (iy << bsizex) + ix] =
208 in[((by << bsizey) + iy) * CDEF_BSTRIDE + (bx << bsizex) + ix];
209 }
210 return;
211 }
212
213 if (pli == 0) {
214 if (!dirinit || !*dirinit) {
215 for (bi = 0; bi < cdef_count; bi++) {
216 by = dlist[bi].by;
217 bx = dlist[bi].bx;
218 dir[by][bx] = cdef_find_dir(&in[8 * by * CDEF_BSTRIDE + 8 * bx],
219 CDEF_BSTRIDE, &var[by][bx], coeff_shift);
220 }
221 if (dirinit) *dirinit = 1;
222 }
223 }
224 if (pli == 1 && xdec != ydec) {
225 for (bi = 0; bi < cdef_count; bi++) {
226 static const int conv422[8] = { 7, 0, 2, 4, 5, 6, 6, 6 };
227 static const int conv440[8] = { 1, 2, 2, 2, 3, 4, 6, 0 };
228 by = dlist[bi].by;
229 bx = dlist[bi].bx;
230 dir[by][bx] = (xdec ? conv422 : conv440)[dir[by][bx]];
231 }
232 }
233
234 for (bi = 0; bi < cdef_count; bi++) {
235 int t = pri_strength;
236 int s = sec_strength;
237 by = dlist[bi].by;
238 bx = dlist[bi].bx;
239 if (dst8)
240 cdef_filter_block(
241 &dst8[(by << bsizey) * dstride + (bx << bsizex)], NULL, dstride,
242 &in[(by * CDEF_BSTRIDE << bsizey) + (bx << bsizex)],
243 (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
244 pri_damping, sec_damping, bsize, coeff_shift);
245 else
246 cdef_filter_block(
247 NULL,
248 &dst16[dirinit ? bi << (bsizex + bsizey)
249 : (by << bsizey) * dstride + (bx << bsizex)],
250 dirinit ? 1 << bsizex : dstride,
251 &in[(by * CDEF_BSTRIDE << bsizey) + (bx << bsizex)],
252 (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
253 pri_damping, sec_damping, bsize, coeff_shift);
254 }
255 }
256