Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 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 | */ |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 11 | |
| 12 | #include <math.h> |
| 13 | #include <stdlib.h> |
| 14 | |
Tom Finegan | 44702c8 | 2018-05-22 13:00:39 -0700 | [diff] [blame] | 15 | #include "config/aom_dsp_rtcd.h" |
| 16 | #include "config/av1_rtcd.h" |
| 17 | |
Tom Finegan | dd3e2a5 | 2018-05-23 14:33:09 -0700 | [diff] [blame] | 18 | #include "av1/common/cdef.h" |
Scott LaVarnway | 0890faa | 2021-12-14 15:35:25 -0500 | [diff] [blame] | 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 }, |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 30 | |
Scott LaVarnway | 0890faa | 2021-12-14 15:35:25 -0500 | [diff] [blame] | 31 | /* Begin cdef_directions */ |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 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 }, |
Scott LaVarnway | 0890faa | 2021-12-14 15:35:25 -0500 | [diff] [blame] | 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 }, |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 46 | }; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 47 | |
Scott LaVarnway | 0890faa | 2021-12-14 15:35:25 -0500 | [diff] [blame] | 48 | const int (*const cdef_directions)[2] = cdef_directions_padded + 2; |
| 49 | |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 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 */ |
Steinar Midtskogen | 94de0aa | 2017-08-02 10:30:12 +0200 | [diff] [blame] | 57 | int cdef_find_dir_c(const uint16_t *img, int stride, int32_t *var, |
| 58 | int coeff_shift) { |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 59 | int i; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 60 | int32_t cost[8] = { 0 }; |
| 61 | int partial[8][15] = { { 0 } }; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 62 | int32_t best_cost = 0; |
| 63 | 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. */ |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 67 | static const int div_table[] = { 0, 840, 420, 280, 210, 168, 140, 120, 105 }; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 68 | for (i = 0; i < 8; i++) { |
| 69 | int j; |
| 70 | for (j = 0; j < 8; j++) { |
| 71 | int x; |
| 72 | /* We subtract 128 here to reduce the maximum range of the squared |
| 73 | partial sums. */ |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 74 | x = (img[i * stride + j] >> coeff_shift) - 128; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 75 | partial[0][i + j] += x; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 76 | partial[1][i + j / 2] += x; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 77 | partial[2][i] += x; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 78 | partial[3][3 + i - j / 2] += x; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 79 | partial[4][7 + i - j] += x; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 80 | partial[5][3 - i / 2 + j] += x; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 81 | partial[6][j] += x; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 82 | partial[7][i / 2 + j] += x; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | for (i = 0; i < 8; i++) { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 86 | cost[2] += partial[2][i] * partial[2][i]; |
| 87 | cost[6] += partial[6][i] * partial[6][i]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 88 | } |
| 89 | cost[2] *= div_table[8]; |
| 90 | cost[6] *= div_table[8]; |
| 91 | for (i = 0; i < 7; i++) { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 92 | cost[0] += (partial[0][i] * partial[0][i] + |
| 93 | partial[0][14 - i] * partial[0][14 - i]) * |
| 94 | div_table[i + 1]; |
| 95 | cost[4] += (partial[4][i] * partial[4][i] + |
| 96 | partial[4][14 - i] * partial[4][14 - i]) * |
| 97 | div_table[i + 1]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 98 | } |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 99 | cost[0] += partial[0][7] * partial[0][7] * div_table[8]; |
| 100 | cost[4] += partial[4][7] * partial[4][7] * div_table[8]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 101 | for (i = 1; i < 8; i += 2) { |
| 102 | int j; |
| 103 | for (j = 0; j < 4 + 1; j++) { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 104 | cost[i] += partial[i][3 + j] * partial[i][3 + j]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 105 | } |
| 106 | cost[i] *= div_table[8]; |
| 107 | for (j = 0; j < 4 - 1; j++) { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 108 | cost[i] += (partial[i][j] * partial[i][j] + |
| 109 | partial[i][10 - j] * partial[i][10 - j]) * |
| 110 | div_table[2 * j + 2]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | for (i = 0; i < 8; i++) { |
| 114 | if (cost[i] > best_cost) { |
| 115 | best_cost = cost[i]; |
| 116 | best_dir = i; |
| 117 | } |
| 118 | } |
| 119 | /* Difference between the optimal variance and the variance along the |
| 120 | orthogonal direction. Again, the sum(x^2) terms cancel out. */ |
| 121 | *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 | *var >>= 10; |
| 125 | return best_dir; |
| 126 | } |
| 127 | |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 128 | const int cdef_pri_taps[2][2] = { { 4, 2 }, { 3, 3 } }; |
Johann | ecfb315 | 2019-10-10 10:15:17 -0700 | [diff] [blame] | 129 | const int cdef_sec_taps[2] = { 2, 1 }; |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 130 | |
| 131 | /* Smooth in the direction detected. */ |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 132 | static void cdef_filter_block_internal( |
| 133 | uint8_t *dst8, uint16_t *dst16, int dstride, const uint16_t *in, |
| 134 | int pri_strength, int sec_strength, int dir, int pri_damping, |
| 135 | int sec_damping, int coeff_shift, int block_width, int block_height, |
| 136 | int enable_primary, int enable_secondary) { |
| 137 | const int clipping_required = (enable_primary && enable_secondary); |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 138 | int i, j, k; |
| 139 | const int s = CDEF_BSTRIDE; |
Steinar Midtskogen | bc75305 | 2017-10-23 12:32:10 +0200 | [diff] [blame] | 140 | const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1]; |
Johann | ecfb315 | 2019-10-10 10:15:17 -0700 | [diff] [blame] | 141 | const int *sec_taps = cdef_sec_taps; |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 142 | for (i = 0; i < block_height; i++) { |
| 143 | for (j = 0; j < block_width; j++) { |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 144 | int16_t sum = 0; |
| 145 | int16_t y; |
| 146 | int16_t x = in[i * s + j]; |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 147 | int max = x; |
| 148 | int min = x; |
Steinar Midtskogen | 612eb2e | 2018-01-02 15:50:50 +0100 | [diff] [blame] | 149 | for (k = 0; k < 2; k++) { |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 150 | if (enable_primary) { |
| 151 | int16_t p0 = in[i * s + j + cdef_directions[dir][k]]; |
| 152 | int16_t p1 = in[i * s + j - cdef_directions[dir][k]]; |
| 153 | sum += pri_taps[k] * constrain(p0 - x, pri_strength, pri_damping); |
| 154 | sum += pri_taps[k] * constrain(p1 - x, pri_strength, pri_damping); |
| 155 | if (clipping_required) { |
| 156 | if (p0 != CDEF_VERY_LARGE) max = AOMMAX(p0, max); |
| 157 | if (p1 != CDEF_VERY_LARGE) max = AOMMAX(p1, max); |
| 158 | min = AOMMIN(p0, min); |
| 159 | min = AOMMIN(p1, min); |
| 160 | } |
Scott LaVarnway | 8734a61 | 2022-01-21 11:25:24 -0500 | [diff] [blame] | 161 | } |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 162 | if (enable_secondary) { |
| 163 | int16_t s0 = in[i * s + j + cdef_directions[dir + 2][k]]; |
| 164 | int16_t s1 = in[i * s + j - cdef_directions[dir + 2][k]]; |
| 165 | int16_t s2 = in[i * s + j + cdef_directions[dir - 2][k]]; |
| 166 | int16_t s3 = in[i * s + j - cdef_directions[dir - 2][k]]; |
| 167 | if (clipping_required) { |
| 168 | if (s0 != CDEF_VERY_LARGE) max = AOMMAX(s0, max); |
| 169 | if (s1 != CDEF_VERY_LARGE) max = AOMMAX(s1, max); |
| 170 | if (s2 != CDEF_VERY_LARGE) max = AOMMAX(s2, max); |
| 171 | if (s3 != CDEF_VERY_LARGE) max = AOMMAX(s3, max); |
| 172 | min = AOMMIN(s0, min); |
| 173 | min = AOMMIN(s1, min); |
| 174 | min = AOMMIN(s2, min); |
| 175 | min = AOMMIN(s3, min); |
| 176 | } |
| 177 | sum += sec_taps[k] * constrain(s0 - x, sec_strength, sec_damping); |
| 178 | sum += sec_taps[k] * constrain(s1 - x, sec_strength, sec_damping); |
| 179 | sum += sec_taps[k] * constrain(s2 - x, sec_strength, sec_damping); |
| 180 | sum += sec_taps[k] * constrain(s3 - x, sec_strength, sec_damping); |
Scott LaVarnway | 8734a61 | 2022-01-21 11:25:24 -0500 | [diff] [blame] | 181 | } |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 182 | } |
Scott LaVarnway | 8734a61 | 2022-01-21 11:25:24 -0500 | [diff] [blame] | 183 | y = ((int16_t)x + ((8 + sum - (sum < 0)) >> 4)); |
| 184 | if (clipping_required) { |
| 185 | y = clamp(y, min, max); |
| 186 | } |
| 187 | |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 188 | if (dst8) |
| 189 | dst8[i * dstride + j] = (uint8_t)y; |
| 190 | else |
| 191 | dst16[i * dstride + j] = (uint16_t)y; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 196 | void cdef_filter_8_0_c(void *dst8, int dstride, const uint16_t *in, |
| 197 | int pri_strength, int sec_strength, int dir, |
| 198 | int pri_damping, int sec_damping, int coeff_shift, |
| 199 | int block_width, int block_height) { |
Scott LaVarnway | 584988f | 2021-12-17 11:47:50 -0500 | [diff] [blame] | 200 | cdef_filter_block_internal((uint8_t *)dst8, NULL, dstride, in, pri_strength, |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 201 | sec_strength, dir, pri_damping, sec_damping, |
| 202 | coeff_shift, block_width, block_height, |
| 203 | /*enable_primary=*/1, /*enable_secondary=*/1); |
Scott LaVarnway | 584988f | 2021-12-17 11:47:50 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 206 | void cdef_filter_8_1_c(void *dst8, int dstride, const uint16_t *in, |
| 207 | int pri_strength, int sec_strength, int dir, |
| 208 | int pri_damping, int sec_damping, int coeff_shift, |
| 209 | int block_width, int block_height) { |
| 210 | cdef_filter_block_internal((uint8_t *)dst8, NULL, dstride, in, pri_strength, |
| 211 | sec_strength, dir, pri_damping, sec_damping, |
| 212 | coeff_shift, block_width, block_height, |
| 213 | /*enable_primary=*/1, /*enable_secondary=*/0); |
| 214 | } |
| 215 | |
| 216 | void cdef_filter_8_2_c(void *dst8, int dstride, const uint16_t *in, |
| 217 | int pri_strength, int sec_strength, int dir, |
| 218 | int pri_damping, int sec_damping, int coeff_shift, |
| 219 | int block_width, int block_height) { |
| 220 | cdef_filter_block_internal((uint8_t *)dst8, NULL, dstride, in, pri_strength, |
| 221 | sec_strength, dir, pri_damping, sec_damping, |
| 222 | coeff_shift, block_width, block_height, |
| 223 | /*enable_primary=*/0, /*enable_secondary=*/1); |
| 224 | } |
| 225 | |
| 226 | void cdef_filter_8_3_c(void *dst8, int dstride, const uint16_t *in, |
| 227 | int pri_strength, int sec_strength, int dir, |
| 228 | int pri_damping, int sec_damping, int coeff_shift, |
| 229 | int block_width, int block_height) { |
| 230 | cdef_filter_block_internal((uint8_t *)dst8, NULL, dstride, in, pri_strength, |
| 231 | sec_strength, dir, pri_damping, sec_damping, |
| 232 | coeff_shift, block_width, block_height, |
| 233 | /*enable_primary=*/0, /*enable_secondary=*/0); |
| 234 | } |
| 235 | |
| 236 | void cdef_filter_16_0_c(void *dst16, int dstride, const uint16_t *in, |
| 237 | int pri_strength, int sec_strength, int dir, |
| 238 | int pri_damping, int sec_damping, int coeff_shift, |
| 239 | int block_width, int block_height) { |
Scott LaVarnway | 584988f | 2021-12-17 11:47:50 -0500 | [diff] [blame] | 240 | cdef_filter_block_internal(NULL, (uint16_t *)dst16, dstride, in, pri_strength, |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 241 | sec_strength, dir, pri_damping, sec_damping, |
| 242 | coeff_shift, block_width, block_height, |
| 243 | /*enable_primary=*/1, /*enable_secondary=*/1); |
| 244 | } |
| 245 | |
| 246 | void cdef_filter_16_1_c(void *dst16, int dstride, const uint16_t *in, |
| 247 | int pri_strength, int sec_strength, int dir, |
| 248 | int pri_damping, int sec_damping, int coeff_shift, |
| 249 | int block_width, int block_height) { |
| 250 | cdef_filter_block_internal(NULL, (uint16_t *)dst16, dstride, in, pri_strength, |
| 251 | sec_strength, dir, pri_damping, sec_damping, |
| 252 | coeff_shift, block_width, block_height, |
| 253 | /*enable_primary=*/1, /*enable_secondary=*/0); |
| 254 | } |
| 255 | |
| 256 | void cdef_filter_16_2_c(void *dst16, int dstride, const uint16_t *in, |
| 257 | int pri_strength, int sec_strength, int dir, |
| 258 | int pri_damping, int sec_damping, int coeff_shift, |
| 259 | int block_width, int block_height) { |
| 260 | cdef_filter_block_internal(NULL, (uint16_t *)dst16, dstride, in, pri_strength, |
| 261 | sec_strength, dir, pri_damping, sec_damping, |
| 262 | coeff_shift, block_width, block_height, |
| 263 | /*enable_primary=*/0, /*enable_secondary=*/1); |
| 264 | } |
| 265 | |
| 266 | void cdef_filter_16_3_c(void *dst16, int dstride, const uint16_t *in, |
| 267 | int pri_strength, int sec_strength, int dir, |
| 268 | int pri_damping, int sec_damping, int coeff_shift, |
| 269 | int block_width, int block_height) { |
| 270 | cdef_filter_block_internal(NULL, (uint16_t *)dst16, dstride, in, pri_strength, |
| 271 | sec_strength, dir, pri_damping, sec_damping, |
| 272 | coeff_shift, block_width, block_height, |
| 273 | /*enable_primary=*/0, /*enable_secondary=*/0); |
Scott LaVarnway | 584988f | 2021-12-17 11:47:50 -0500 | [diff] [blame] | 274 | } |
Scott LaVarnway | 584988f | 2021-12-17 11:47:50 -0500 | [diff] [blame] | 275 | |
Steinar Midtskogen | 94de0aa | 2017-08-02 10:30:12 +0200 | [diff] [blame] | 276 | /* Compute the primary filter strength for an 8x8 block based on the |
| 277 | directional variance difference. A high variance difference means |
| 278 | that we have a highly directional pattern (e.g. a high contrast |
| 279 | edge), so we can apply more deringing. A low variance means that we |
| 280 | either have a low contrast edge, or a non-directional texture, so |
| 281 | we want to be careful not to blur. */ |
| 282 | static INLINE int adjust_strength(int strength, int32_t var) { |
Steinar Midtskogen | fade463 | 2017-04-14 20:29:05 +0200 | [diff] [blame] | 283 | const int i = var >> 6 ? AOMMIN(get_msb(var >> 6), 12) : 0; |
Steinar Midtskogen | 94de0aa | 2017-08-02 10:30:12 +0200 | [diff] [blame] | 284 | /* We use the variance of 8x8 blocks to adjust the strength. */ |
| 285 | return var ? (strength * (4 + i) + 8) >> 4 : 0; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Hui Su | 584ba48 | 2019-06-19 11:48:05 -0700 | [diff] [blame] | 288 | void av1_cdef_filter_fb(uint8_t *dst8, uint16_t *dst16, int dstride, |
| 289 | uint16_t *in, int xdec, int ydec, |
| 290 | int dir[CDEF_NBLOCKS][CDEF_NBLOCKS], int *dirinit, |
| 291 | int var[CDEF_NBLOCKS][CDEF_NBLOCKS], int pli, |
| 292 | cdef_list *dlist, int cdef_count, int level, |
| 293 | int sec_strength, int damping, int coeff_shift) { |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 294 | int bi; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 295 | int bx; |
| 296 | int by; |
Hui Su | 3af4d13 | 2019-06-19 16:03:54 -0700 | [diff] [blame] | 297 | const int pri_strength = level << coeff_shift; |
Steinar Midtskogen | 830d2ac | 2017-10-21 08:07:19 +0200 | [diff] [blame] | 298 | sec_strength <<= coeff_shift; |
Hui Su | 584ba48 | 2019-06-19 11:48:05 -0700 | [diff] [blame] | 299 | damping += coeff_shift - (pli != AOM_PLANE_Y); |
Hui Su | 3af4d13 | 2019-06-19 16:03:54 -0700 | [diff] [blame] | 300 | const int bw_log2 = 3 - xdec; |
| 301 | const int bh_log2 = 3 - ydec; |
Steinar Midtskogen | 8322ff0 | 2017-12-20 15:39:52 +0100 | [diff] [blame] | 302 | if (dirinit && pri_strength == 0 && sec_strength == 0) { |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 303 | // If we're here, both primary and secondary strengths are 0, and |
| 304 | // we still haven't written anything to y[] yet, so we just copy |
| 305 | // the input to y[]. This is necessary only for av1_cdef_search() |
| 306 | // and only av1_cdef_search() sets dirinit. |
| 307 | for (bi = 0; bi < cdef_count; bi++) { |
| 308 | by = dlist[bi].by; |
| 309 | bx = dlist[bi].bx; |
Steinar Midtskogen | 95a2f86 | 2017-04-07 09:24:02 +0200 | [diff] [blame] | 310 | // TODO(stemidts/jmvalin): SIMD optimisations |
Hui Su | 3af4d13 | 2019-06-19 16:03:54 -0700 | [diff] [blame] | 311 | for (int iy = 0; iy < 1 << bh_log2; iy++) { |
| 312 | memcpy(&dst16[(bi << (bw_log2 + bh_log2)) + (iy << bw_log2)], |
| 313 | &in[((by << bh_log2) + iy) * CDEF_BSTRIDE + (bx << bw_log2)], |
Fyodor Kyslov | e08fbd2 | 2019-06-28 10:49:19 -0700 | [diff] [blame] | 314 | ((size_t)1 << bw_log2) * sizeof(*dst16)); |
Hui Su | 3af4d13 | 2019-06-19 16:03:54 -0700 | [diff] [blame] | 315 | } |
Steinar Midtskogen | 95a2f86 | 2017-04-07 09:24:02 +0200 | [diff] [blame] | 316 | } |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 317 | return; |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 318 | } |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 319 | |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 320 | if (pli == 0) { |
| 321 | if (!dirinit || !*dirinit) { |
| 322 | for (bi = 0; bi < cdef_count; bi++) { |
| 323 | by = dlist[bi].by; |
| 324 | bx = dlist[bi].bx; |
| 325 | dir[by][bx] = cdef_find_dir(&in[8 * by * CDEF_BSTRIDE + 8 * bx], |
| 326 | CDEF_BSTRIDE, &var[by][bx], coeff_shift); |
| 327 | } |
| 328 | if (dirinit) *dirinit = 1; |
| 329 | } |
| 330 | } |
Steinar Midtskogen | ab6c9c7 | 2017-11-30 13:38:26 +0100 | [diff] [blame] | 331 | if (pli == 1 && xdec != ydec) { |
| 332 | for (bi = 0; bi < cdef_count; bi++) { |
| 333 | static const int conv422[8] = { 7, 0, 2, 4, 5, 6, 6, 6 }; |
| 334 | static const int conv440[8] = { 1, 2, 2, 2, 3, 4, 6, 0 }; |
| 335 | by = dlist[bi].by; |
| 336 | bx = dlist[bi].bx; |
| 337 | dir[by][bx] = (xdec ? conv422 : conv440)[dir[by][bx]]; |
| 338 | } |
| 339 | } |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 340 | |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 341 | if (dst8) { |
| 342 | const int block_width = 8 >> xdec; |
| 343 | const int block_height = 8 >> ydec; |
| 344 | /* |
| 345 | * strength_index == 0 : enable_primary = 1, enable_secondary = 1 |
| 346 | * strength_index == 1 : enable_primary = 1, enable_secondary = 0 |
| 347 | * strength_index == 2 : enable_primary = 0, enable_secondary = 1 |
| 348 | * strength_index == 3 : enable_primary = 0, enable_secondary = 0 |
| 349 | */ |
| 350 | const cdef_filter_block_func cdef_filter_fn[4] = { |
| 351 | cdef_filter_8_0, cdef_filter_8_1, cdef_filter_8_2, cdef_filter_8_3 |
| 352 | }; |
| 353 | |
| 354 | for (bi = 0; bi < cdef_count; bi++) { |
| 355 | by = dlist[bi].by; |
| 356 | bx = dlist[bi].bx; |
| 357 | const int t = |
| 358 | (pli ? pri_strength : adjust_strength(pri_strength, var[by][bx])); |
| 359 | const int strength_index = (sec_strength == 0) | ((t == 0) << 1); |
| 360 | |
| 361 | cdef_filter_fn[strength_index]( |
| 362 | &dst8[(by << bh_log2) * dstride + (bx << bw_log2)], dstride, |
| 363 | &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)], t, |
| 364 | sec_strength, pri_strength ? dir[by][bx] : 0, damping, damping, |
| 365 | coeff_shift, block_width, block_height); |
| 366 | } |
| 367 | } else { |
| 368 | const int block_width = 8 >> xdec; |
| 369 | const int block_height = 8 >> ydec; |
| 370 | /* |
| 371 | * strength_index == 0 : enable_primary = 1, enable_secondary = 1 |
| 372 | * strength_index == 1 : enable_primary = 1, enable_secondary = 0 |
| 373 | * strength_index == 2 : enable_primary = 0, enable_secondary = 1 |
| 374 | * strength_index == 3 : enable_primary = 0, enable_secondary = 0 |
| 375 | */ |
| 376 | const cdef_filter_block_func cdef_filter_fn[4] = { |
| 377 | cdef_filter_16_0, cdef_filter_16_1, cdef_filter_16_2, cdef_filter_16_3 |
| 378 | }; |
| 379 | |
| 380 | for (bi = 0; bi < cdef_count; bi++) { |
| 381 | by = dlist[bi].by; |
| 382 | bx = dlist[bi].bx; |
| 383 | const int t = |
| 384 | (pli ? pri_strength : adjust_strength(pri_strength, var[by][bx])); |
| 385 | const int strength_index = (sec_strength == 0) | ((t == 0) << 1); |
| 386 | |
| 387 | cdef_filter_fn[strength_index]( |
Hui Su | 3af4d13 | 2019-06-19 16:03:54 -0700 | [diff] [blame] | 388 | &dst16[dirinit ? bi << (bw_log2 + bh_log2) |
| 389 | : (by << bh_log2) * dstride + (bx << bw_log2)], |
| 390 | dirinit ? 1 << bw_log2 : dstride, |
Scott LaVarnway | 78a7788 | 2022-01-28 08:54:08 -0500 | [diff] [blame^] | 391 | &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)], t, |
| 392 | sec_strength, pri_strength ? dir[by][bx] : 0, damping, damping, |
| 393 | coeff_shift, block_width, block_height); |
Hui Su | 3af4d13 | 2019-06-19 16:03:54 -0700 | [diff] [blame] | 394 | } |
Steinar Midtskogen | 5978212 | 2017-07-20 08:49:43 +0200 | [diff] [blame] | 395 | } |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 396 | } |