/src/aom/av1/common/cdef_block.c
Line | Count | Source (jump to first uncovered line) |
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 | | This is Cdef_Directions (section 7.15.3) with 2 padding entries at the |
21 | | beginning and end of the table. The cdef direction range is [0, 7] and the |
22 | | first index is offset +/-2. This removes the need to constrain the first |
23 | | index to the same range using e.g., & 7. |
24 | | */ |
25 | | DECLARE_ALIGNED(16, const int, cdef_directions_padded[12][2]) = { |
26 | | /* Padding: cdef_directions[6] */ |
27 | | { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 0 }, |
28 | | /* Padding: cdef_directions[7] */ |
29 | | { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE - 1 }, |
30 | | |
31 | | /* Begin cdef_directions */ |
32 | | { -1 * CDEF_BSTRIDE + 1, -2 * CDEF_BSTRIDE + 2 }, |
33 | | { 0 * CDEF_BSTRIDE + 1, -1 * CDEF_BSTRIDE + 2 }, |
34 | | { 0 * CDEF_BSTRIDE + 1, 0 * CDEF_BSTRIDE + 2 }, |
35 | | { 0 * CDEF_BSTRIDE + 1, 1 * CDEF_BSTRIDE + 2 }, |
36 | | { 1 * CDEF_BSTRIDE + 1, 2 * CDEF_BSTRIDE + 2 }, |
37 | | { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 1 }, |
38 | | { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 0 }, |
39 | | { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE - 1 }, |
40 | | /* End cdef_directions */ |
41 | | |
42 | | /* Padding: cdef_directions[0] */ |
43 | | { -1 * CDEF_BSTRIDE + 1, -2 * CDEF_BSTRIDE + 2 }, |
44 | | /* Padding: cdef_directions[1] */ |
45 | | { 0 * CDEF_BSTRIDE + 1, -1 * CDEF_BSTRIDE + 2 }, |
46 | | }; |
47 | | |
48 | | const int (*const cdef_directions)[2] = cdef_directions_padded + 2; |
49 | | |
50 | | /* Detect direction. 0 means 45-degree up-right, 2 is horizontal, and so on. |
51 | | The search minimizes the weighted variance along all the lines in a |
52 | | particular direction, i.e. the squared error between the input and a |
53 | | "predicted" block where each pixel is replaced by the average along a line |
54 | | in a particular direction. Since each direction have the same sum(x^2) term, |
55 | | that term is never computed. See Section 2, step 2, of: |
56 | | http://jmvalin.ca/notes/intra_paint.pdf */ |
57 | | int cdef_find_dir_c(const uint16_t *img, int stride, int32_t *var, |
58 | 0 | int coeff_shift) { |
59 | 0 | int i; |
60 | 0 | int32_t cost[8] = { 0 }; |
61 | 0 | int partial[8][15] = { { 0 } }; |
62 | 0 | int32_t best_cost = 0; |
63 | 0 | int best_dir = 0; |
64 | | /* Instead of dividing by n between 2 and 8, we multiply by 3*5*7*8/n. |
65 | | The output is then 840 times larger, but we don't care for finding |
66 | | the max. */ |
67 | 0 | static const int div_table[] = { 0, 840, 420, 280, 210, 168, 140, 120, 105 }; |
68 | 0 | for (i = 0; i < 8; i++) { |
69 | 0 | int j; |
70 | 0 | for (j = 0; j < 8; j++) { |
71 | 0 | int x; |
72 | | /* We subtract 128 here to reduce the maximum range of the squared |
73 | | partial sums. */ |
74 | 0 | x = (img[i * stride + j] >> coeff_shift) - 128; |
75 | 0 | partial[0][i + j] += x; |
76 | 0 | partial[1][i + j / 2] += x; |
77 | 0 | partial[2][i] += x; |
78 | 0 | partial[3][3 + i - j / 2] += x; |
79 | 0 | partial[4][7 + i - j] += x; |
80 | 0 | partial[5][3 - i / 2 + j] += x; |
81 | 0 | partial[6][j] += x; |
82 | 0 | partial[7][i / 2 + j] += x; |
83 | 0 | } |
84 | 0 | } |
85 | 0 | for (i = 0; i < 8; i++) { |
86 | 0 | cost[2] += partial[2][i] * partial[2][i]; |
87 | 0 | cost[6] += partial[6][i] * partial[6][i]; |
88 | 0 | } |
89 | 0 | cost[2] *= div_table[8]; |
90 | 0 | cost[6] *= div_table[8]; |
91 | 0 | for (i = 0; i < 7; i++) { |
92 | 0 | cost[0] += (partial[0][i] * partial[0][i] + |
93 | 0 | partial[0][14 - i] * partial[0][14 - i]) * |
94 | 0 | div_table[i + 1]; |
95 | 0 | cost[4] += (partial[4][i] * partial[4][i] + |
96 | 0 | partial[4][14 - i] * partial[4][14 - i]) * |
97 | 0 | div_table[i + 1]; |
98 | 0 | } |
99 | 0 | cost[0] += partial[0][7] * partial[0][7] * div_table[8]; |
100 | 0 | cost[4] += partial[4][7] * partial[4][7] * div_table[8]; |
101 | 0 | for (i = 1; i < 8; i += 2) { |
102 | 0 | int j; |
103 | 0 | for (j = 0; j < 4 + 1; j++) { |
104 | 0 | cost[i] += partial[i][3 + j] * partial[i][3 + j]; |
105 | 0 | } |
106 | 0 | cost[i] *= div_table[8]; |
107 | 0 | for (j = 0; j < 4 - 1; j++) { |
108 | 0 | cost[i] += (partial[i][j] * partial[i][j] + |
109 | 0 | partial[i][10 - j] * partial[i][10 - j]) * |
110 | 0 | div_table[2 * j + 2]; |
111 | 0 | } |
112 | 0 | } |
113 | 0 | for (i = 0; i < 8; i++) { |
114 | 0 | if (cost[i] > best_cost) { |
115 | 0 | best_cost = cost[i]; |
116 | 0 | best_dir = i; |
117 | 0 | } |
118 | 0 | } |
119 | | /* Difference between the optimal variance and the variance along the |
120 | | orthogonal direction. Again, the sum(x^2) terms cancel out. */ |
121 | 0 | *var = best_cost - cost[(best_dir + 4) & 7]; |
122 | | /* We'd normally divide by 840, but dividing by 1024 is close enough |
123 | | for what we're going to do with this. */ |
124 | 0 | *var >>= 10; |
125 | 0 | return best_dir; |
126 | 0 | } |
127 | | |
128 | | const int cdef_pri_taps[2][2] = { { 4, 2 }, { 3, 3 } }; |
129 | | const int cdef_sec_taps[2] = { 2, 1 }; |
130 | | |
131 | | /* Smooth in the direction detected. */ |
132 | | static void cdef_filter_block_internal(uint8_t *dst8, uint16_t *dst16, |
133 | | int dstride, const uint16_t *in, |
134 | | int pri_strength, int sec_strength, |
135 | | int dir, int pri_damping, |
136 | | int sec_damping, int bsize, |
137 | 0 | int coeff_shift) { |
138 | 0 | int i, j, k; |
139 | 0 | const int s = CDEF_BSTRIDE; |
140 | 0 | const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1]; |
141 | 0 | const int *sec_taps = cdef_sec_taps; |
142 | 0 | for (i = 0; i < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_4X8); i++) { |
143 | 0 | for (j = 0; j < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_8X4); j++) { |
144 | 0 | int16_t sum = 0; |
145 | 0 | int16_t y; |
146 | 0 | int16_t x = in[i * s + j]; |
147 | 0 | int max = x; |
148 | 0 | int min = x; |
149 | 0 | for (k = 0; k < 2; k++) { |
150 | 0 | int16_t p0 = in[i * s + j + cdef_directions[dir][k]]; |
151 | 0 | int16_t p1 = in[i * s + j - cdef_directions[dir][k]]; |
152 | 0 | sum += pri_taps[k] * constrain(p0 - x, pri_strength, pri_damping); |
153 | 0 | sum += pri_taps[k] * constrain(p1 - x, pri_strength, pri_damping); |
154 | 0 | if (p0 != CDEF_VERY_LARGE) max = AOMMAX(p0, max); |
155 | 0 | if (p1 != CDEF_VERY_LARGE) max = AOMMAX(p1, max); |
156 | 0 | min = AOMMIN(p0, min); |
157 | 0 | min = AOMMIN(p1, min); |
158 | 0 | int16_t s0 = in[i * s + j + cdef_directions[dir + 2][k]]; |
159 | 0 | int16_t s1 = in[i * s + j - cdef_directions[dir + 2][k]]; |
160 | 0 | int16_t s2 = in[i * s + j + cdef_directions[dir - 2][k]]; |
161 | 0 | int16_t s3 = in[i * s + j - cdef_directions[dir - 2][k]]; |
162 | 0 | if (s0 != CDEF_VERY_LARGE) max = AOMMAX(s0, max); |
163 | 0 | if (s1 != CDEF_VERY_LARGE) max = AOMMAX(s1, max); |
164 | 0 | if (s2 != CDEF_VERY_LARGE) max = AOMMAX(s2, max); |
165 | 0 | if (s3 != CDEF_VERY_LARGE) max = AOMMAX(s3, max); |
166 | 0 | min = AOMMIN(s0, min); |
167 | 0 | min = AOMMIN(s1, min); |
168 | 0 | min = AOMMIN(s2, min); |
169 | 0 | min = AOMMIN(s3, min); |
170 | 0 | sum += sec_taps[k] * constrain(s0 - x, sec_strength, sec_damping); |
171 | 0 | sum += sec_taps[k] * constrain(s1 - x, sec_strength, sec_damping); |
172 | 0 | sum += sec_taps[k] * constrain(s2 - x, sec_strength, sec_damping); |
173 | 0 | sum += sec_taps[k] * constrain(s3 - x, sec_strength, sec_damping); |
174 | 0 | } |
175 | 0 | y = clamp((int16_t)x + ((8 + sum - (sum < 0)) >> 4), min, max); |
176 | 0 | if (dst8) |
177 | 0 | dst8[i * dstride + j] = (uint8_t)y; |
178 | 0 | else |
179 | 0 | dst16[i * dstride + j] = (uint16_t)y; |
180 | 0 | } |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | void cdef_filter_block_c(void *dst8, int dstride, const uint16_t *in, |
185 | | int pri_strength, int sec_strength, int dir, |
186 | | int pri_damping, int sec_damping, int bsize, |
187 | 0 | int coeff_shift) { |
188 | 0 | cdef_filter_block_internal((uint8_t *)dst8, NULL, dstride, in, pri_strength, |
189 | 0 | sec_strength, dir, pri_damping, sec_damping, bsize, |
190 | 0 | coeff_shift); |
191 | 0 | } |
192 | | |
193 | | void cdef_filter_block_highbd_c(void *dst16, int dstride, const uint16_t *in, |
194 | | int pri_strength, int sec_strength, int dir, |
195 | | int pri_damping, int sec_damping, int bsize, |
196 | 0 | int coeff_shift) { |
197 | 0 | cdef_filter_block_internal(NULL, (uint16_t *)dst16, dstride, in, pri_strength, |
198 | 0 | sec_strength, dir, pri_damping, sec_damping, bsize, |
199 | 0 | coeff_shift); |
200 | 0 | } |
201 | | |
202 | | /* Compute the primary filter strength for an 8x8 block based on the |
203 | | directional variance difference. A high variance difference means |
204 | | that we have a highly directional pattern (e.g. a high contrast |
205 | | edge), so we can apply more deringing. A low variance means that we |
206 | | either have a low contrast edge, or a non-directional texture, so |
207 | | we want to be careful not to blur. */ |
208 | 0 | static INLINE int adjust_strength(int strength, int32_t var) { |
209 | 0 | const int i = var >> 6 ? AOMMIN(get_msb(var >> 6), 12) : 0; |
210 | | /* We use the variance of 8x8 blocks to adjust the strength. */ |
211 | 0 | return var ? (strength * (4 + i) + 8) >> 4 : 0; |
212 | 0 | } |
213 | | |
214 | | void av1_cdef_filter_fb(uint8_t *dst8, uint16_t *dst16, int dstride, |
215 | | uint16_t *in, int xdec, int ydec, |
216 | | int dir[CDEF_NBLOCKS][CDEF_NBLOCKS], int *dirinit, |
217 | | int var[CDEF_NBLOCKS][CDEF_NBLOCKS], int pli, |
218 | | cdef_list *dlist, int cdef_count, int level, |
219 | 0 | int sec_strength, int damping, int coeff_shift) { |
220 | 0 | int bi; |
221 | 0 | int bx; |
222 | 0 | int by; |
223 | 0 | const int pri_strength = level << coeff_shift; |
224 | 0 | sec_strength <<= coeff_shift; |
225 | 0 | damping += coeff_shift - (pli != AOM_PLANE_Y); |
226 | 0 | const int bw_log2 = 3 - xdec; |
227 | 0 | const int bh_log2 = 3 - ydec; |
228 | 0 | if (dirinit && pri_strength == 0 && sec_strength == 0) { |
229 | | // If we're here, both primary and secondary strengths are 0, and |
230 | | // we still haven't written anything to y[] yet, so we just copy |
231 | | // the input to y[]. This is necessary only for av1_cdef_search() |
232 | | // and only av1_cdef_search() sets dirinit. |
233 | 0 | for (bi = 0; bi < cdef_count; bi++) { |
234 | 0 | by = dlist[bi].by; |
235 | 0 | bx = dlist[bi].bx; |
236 | | // TODO(stemidts/jmvalin): SIMD optimisations |
237 | 0 | for (int iy = 0; iy < 1 << bh_log2; iy++) { |
238 | 0 | memcpy(&dst16[(bi << (bw_log2 + bh_log2)) + (iy << bw_log2)], |
239 | 0 | &in[((by << bh_log2) + iy) * CDEF_BSTRIDE + (bx << bw_log2)], |
240 | 0 | ((size_t)1 << bw_log2) * sizeof(*dst16)); |
241 | 0 | } |
242 | 0 | } |
243 | 0 | return; |
244 | 0 | } |
245 | | |
246 | 0 | if (pli == 0) { |
247 | 0 | if (!dirinit || !*dirinit) { |
248 | 0 | for (bi = 0; bi < cdef_count; bi++) { |
249 | 0 | by = dlist[bi].by; |
250 | 0 | bx = dlist[bi].bx; |
251 | 0 | dir[by][bx] = cdef_find_dir(&in[8 * by * CDEF_BSTRIDE + 8 * bx], |
252 | 0 | CDEF_BSTRIDE, &var[by][bx], coeff_shift); |
253 | 0 | } |
254 | 0 | if (dirinit) *dirinit = 1; |
255 | 0 | } |
256 | 0 | } |
257 | 0 | if (pli == 1 && xdec != ydec) { |
258 | 0 | for (bi = 0; bi < cdef_count; bi++) { |
259 | 0 | static const int conv422[8] = { 7, 0, 2, 4, 5, 6, 6, 6 }; |
260 | 0 | static const int conv440[8] = { 1, 2, 2, 2, 3, 4, 6, 0 }; |
261 | 0 | by = dlist[bi].by; |
262 | 0 | bx = dlist[bi].bx; |
263 | 0 | dir[by][bx] = (xdec ? conv422 : conv440)[dir[by][bx]]; |
264 | 0 | } |
265 | 0 | } |
266 | |
|
267 | 0 | const int bsize = |
268 | 0 | ydec ? (xdec ? BLOCK_4X4 : BLOCK_8X4) : (xdec ? BLOCK_4X8 : BLOCK_8X8); |
269 | 0 | const int t = pri_strength; |
270 | 0 | const int s = sec_strength; |
271 | 0 | for (bi = 0; bi < cdef_count; bi++) { |
272 | 0 | by = dlist[bi].by; |
273 | 0 | bx = dlist[bi].bx; |
274 | 0 | if (dst16) { |
275 | 0 | cdef_filter_block_highbd( |
276 | 0 | &dst16[dirinit ? bi << (bw_log2 + bh_log2) |
277 | 0 | : (by << bh_log2) * dstride + (bx << bw_log2)], |
278 | 0 | dirinit ? 1 << bw_log2 : dstride, |
279 | 0 | &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)], |
280 | 0 | (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0, |
281 | 0 | damping, damping, bsize, coeff_shift); |
282 | 0 | } else { |
283 | 0 | cdef_filter_block( |
284 | 0 | &dst8[(by << bh_log2) * dstride + (bx << bw_log2)], dstride, |
285 | 0 | &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)], |
286 | 0 | (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0, |
287 | 0 | damping, damping, bsize, coeff_shift); |
288 | 0 | } |
289 | 0 | } |
290 | 0 | } |