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 | */ |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 11 | #ifdef HAVE_CONFIG_H |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 12 | #include "config.h" |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 13 | #endif |
| 14 | |
Jean-Marc Valin | a8ce2c9 | 2016-10-07 18:10:19 -0400 | [diff] [blame] | 15 | // clang-format off |
| 16 | |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <math.h> |
| 19 | #include "dering.h" |
Michael Bebenita | 7227b65 | 2016-10-06 14:27:34 -0700 | [diff] [blame] | 20 | #include "./av1_rtcd.h" |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 21 | |
| 22 | /* Generated from gen_filter_tables.c. */ |
| 23 | const int OD_DIRECTION_OFFSETS_TABLE[8][3] = { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 24 | { -1 * OD_FILT_BSTRIDE + 1, -2 * OD_FILT_BSTRIDE + 2, |
| 25 | -3 * OD_FILT_BSTRIDE + 3 }, |
| 26 | { 0 * OD_FILT_BSTRIDE + 1, -1 * OD_FILT_BSTRIDE + 2, |
| 27 | -1 * OD_FILT_BSTRIDE + 3 }, |
| 28 | { 0 * OD_FILT_BSTRIDE + 1, 0 * OD_FILT_BSTRIDE + 2, 0 * OD_FILT_BSTRIDE + 3 }, |
| 29 | { 0 * OD_FILT_BSTRIDE + 1, 1 * OD_FILT_BSTRIDE + 2, 1 * OD_FILT_BSTRIDE + 3 }, |
| 30 | { 1 * OD_FILT_BSTRIDE + 1, 2 * OD_FILT_BSTRIDE + 2, 3 * OD_FILT_BSTRIDE + 3 }, |
| 31 | { 1 * OD_FILT_BSTRIDE + 0, 2 * OD_FILT_BSTRIDE + 1, 3 * OD_FILT_BSTRIDE + 1 }, |
| 32 | { 1 * OD_FILT_BSTRIDE + 0, 2 * OD_FILT_BSTRIDE + 0, 3 * OD_FILT_BSTRIDE + 0 }, |
| 33 | { 1 * OD_FILT_BSTRIDE + 0, 2 * OD_FILT_BSTRIDE - 1, 3 * OD_FILT_BSTRIDE - 1 }, |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 36 | /* Detect direction. 0 means 45-degree up-right, 2 is horizontal, and so on. |
| 37 | The search minimizes the weighted variance along all the lines in a |
| 38 | particular direction, i.e. the squared error between the input and a |
| 39 | "predicted" block where each pixel is replaced by the average along a line |
| 40 | in a particular direction. Since each direction have the same sum(x^2) term, |
| 41 | that term is never computed. See Section 2, step 2, of: |
| 42 | http://jmvalin.ca/notes/intra_paint.pdf */ |
Michael Bebenita | 7227b65 | 2016-10-06 14:27:34 -0700 | [diff] [blame] | 43 | int od_dir_find8_c(const od_dering_in *img, int stride, int32_t *var, |
| 44 | int coeff_shift) { |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 45 | int i; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 46 | int32_t cost[8] = { 0 }; |
| 47 | int partial[8][15] = { { 0 } }; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 48 | int32_t best_cost = 0; |
| 49 | int best_dir = 0; |
| 50 | /* Instead of dividing by n between 2 and 8, we multiply by 3*5*7*8/n. |
| 51 | The output is then 840 times larger, but we don't care for finding |
| 52 | the max. */ |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 53 | 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] | 54 | for (i = 0; i < 8; i++) { |
| 55 | int j; |
| 56 | for (j = 0; j < 8; j++) { |
| 57 | int x; |
| 58 | /* We subtract 128 here to reduce the maximum range of the squared |
| 59 | partial sums. */ |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 60 | x = (img[i * stride + j] >> coeff_shift) - 128; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 61 | partial[0][i + j] += x; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 62 | partial[1][i + j / 2] += x; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 63 | partial[2][i] += x; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 64 | partial[3][3 + i - j / 2] += x; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 65 | partial[4][7 + i - j] += x; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 66 | partial[5][3 - i / 2 + j] += x; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 67 | partial[6][j] += x; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 68 | partial[7][i / 2 + j] += x; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | for (i = 0; i < 8; i++) { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 72 | cost[2] += partial[2][i] * partial[2][i]; |
| 73 | cost[6] += partial[6][i] * partial[6][i]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 74 | } |
| 75 | cost[2] *= div_table[8]; |
| 76 | cost[6] *= div_table[8]; |
| 77 | for (i = 0; i < 7; i++) { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 78 | cost[0] += (partial[0][i] * partial[0][i] + |
| 79 | partial[0][14 - i] * partial[0][14 - i]) * |
| 80 | div_table[i + 1]; |
| 81 | cost[4] += (partial[4][i] * partial[4][i] + |
| 82 | partial[4][14 - i] * partial[4][14 - i]) * |
| 83 | div_table[i + 1]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 84 | } |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 85 | cost[0] += partial[0][7] * partial[0][7] * div_table[8]; |
| 86 | cost[4] += partial[4][7] * partial[4][7] * div_table[8]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 87 | for (i = 1; i < 8; i += 2) { |
| 88 | int j; |
| 89 | for (j = 0; j < 4 + 1; j++) { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 90 | cost[i] += partial[i][3 + j] * partial[i][3 + j]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 91 | } |
| 92 | cost[i] *= div_table[8]; |
| 93 | for (j = 0; j < 4 - 1; j++) { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 94 | cost[i] += (partial[i][j] * partial[i][j] + |
| 95 | partial[i][10 - j] * partial[i][10 - j]) * |
| 96 | div_table[2 * j + 2]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | for (i = 0; i < 8; i++) { |
| 100 | if (cost[i] > best_cost) { |
| 101 | best_cost = cost[i]; |
| 102 | best_dir = i; |
| 103 | } |
| 104 | } |
| 105 | /* Difference between the optimal variance and the variance along the |
| 106 | orthogonal direction. Again, the sum(x^2) terms cancel out. */ |
| 107 | *var = best_cost - cost[(best_dir + 4) & 7]; |
| 108 | /* We'd normally divide by 840, but dividing by 1024 is close enough |
| 109 | for what we're going to do with this. */ |
| 110 | *var >>= 10; |
| 111 | return best_dir; |
| 112 | } |
| 113 | |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 114 | /* Smooth in the direction detected. */ |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 115 | int od_filter_dering_direction_8x8_c(int16_t *y, int ystride, const int16_t *in, |
| 116 | int threshold, int dir) { |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 117 | int i; |
| 118 | int j; |
| 119 | int k; |
Yaowu Xu | 9c323bc | 2016-09-01 11:35:16 -0700 | [diff] [blame] | 120 | static const int taps[3] = { 3, 2, 1 }; |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 121 | int total_abs = 0; |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 122 | for (i = 0; i < 8; i++) { |
| 123 | for (j = 0; j < 8; j++) { |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 124 | int16_t sum; |
| 125 | int16_t xx; |
| 126 | int16_t yy; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 127 | xx = in[i * OD_FILT_BSTRIDE + j]; |
| 128 | sum = 0; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 129 | for (k = 0; k < 3; k++) { |
| 130 | int16_t p0; |
| 131 | int16_t p1; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 132 | p0 = in[i * OD_FILT_BSTRIDE + j + OD_DIRECTION_OFFSETS_TABLE[dir][k]] - |
| 133 | xx; |
| 134 | p1 = in[i * OD_FILT_BSTRIDE + j - OD_DIRECTION_OFFSETS_TABLE[dir][k]] - |
| 135 | xx; |
| 136 | if (abs(p0) < threshold) sum += taps[k] * p0; |
| 137 | if (abs(p1) < threshold) sum += taps[k] * p1; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 138 | } |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 139 | sum = (sum + 8) >> 4; |
| 140 | total_abs += abs(sum); |
| 141 | yy = xx + sum; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 142 | y[i * ystride + j] = yy; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 145 | return (total_abs + 8) >> 4; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 148 | /* Smooth in the direction detected. */ |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 149 | int od_filter_dering_direction_4x4_c(int16_t *y, int ystride, const int16_t *in, |
| 150 | int threshold, int dir) { |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 151 | int i; |
| 152 | int j; |
| 153 | int k; |
| 154 | static const int taps[2] = { 4, 1 }; |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 155 | int total_abs = 0; |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 156 | for (i = 0; i < 4; i++) { |
| 157 | for (j = 0; j < 4; j++) { |
| 158 | int16_t sum; |
| 159 | int16_t xx; |
| 160 | int16_t yy; |
| 161 | xx = in[i * OD_FILT_BSTRIDE + j]; |
| 162 | sum = 0; |
| 163 | for (k = 0; k < 2; k++) { |
| 164 | int16_t p0; |
| 165 | int16_t p1; |
| 166 | p0 = in[i * OD_FILT_BSTRIDE + j + OD_DIRECTION_OFFSETS_TABLE[dir][k]] - |
| 167 | xx; |
| 168 | p1 = in[i * OD_FILT_BSTRIDE + j - OD_DIRECTION_OFFSETS_TABLE[dir][k]] - |
| 169 | xx; |
| 170 | if (abs(p0) < threshold) sum += taps[k] * p0; |
| 171 | if (abs(p1) < threshold) sum += taps[k] * p1; |
| 172 | } |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 173 | sum = (sum + 8) >> 4; |
| 174 | total_abs += abs(sum); |
| 175 | yy = xx + sum; |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 176 | y[i * ystride + j] = yy; |
| 177 | } |
| 178 | } |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 179 | return (total_abs + 2) >> 2; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Smooth in the direction orthogonal to what was detected. */ |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 183 | void od_filter_dering_orthogonal_8x8_c(int16_t *y, int ystride, |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 184 | const int16_t *in, int threshold, |
| 185 | int dir) { |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 186 | int i; |
| 187 | int j; |
| 188 | int offset; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 189 | if (dir > 0 && dir < 4) |
| 190 | offset = OD_FILT_BSTRIDE; |
| 191 | else |
| 192 | offset = 1; |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 193 | for (i = 0; i < 8; i++) { |
| 194 | for (j = 0; j < 8; j++) { |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 195 | int16_t yy; |
| 196 | int16_t sum; |
| 197 | int16_t p; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 198 | yy = in[i * OD_FILT_BSTRIDE + j]; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 199 | sum = 0; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 200 | p = in[i * OD_FILT_BSTRIDE + j + offset] - yy; |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 201 | if (abs(p) < threshold) sum += p; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 202 | p = in[i * OD_FILT_BSTRIDE + j - offset] - yy; |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 203 | if (abs(p) < threshold) sum += p; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 204 | p = in[i * OD_FILT_BSTRIDE + j + 2 * offset] - yy; |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 205 | if (abs(p) < threshold) sum += p; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 206 | p = in[i * OD_FILT_BSTRIDE + j - 2 * offset] - yy; |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 207 | if (abs(p) < threshold) sum += p; |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 208 | y[i * ystride + j] = yy + ((3 * sum + 8) >> 4); |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 213 | /* Smooth in the direction orthogonal to what was detected. */ |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 214 | void od_filter_dering_orthogonal_4x4_c(int16_t *y, int ystride, |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 215 | const int16_t *in, int threshold, |
| 216 | int dir) { |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 217 | int i; |
| 218 | int j; |
| 219 | int offset; |
| 220 | if (dir > 0 && dir < 4) |
| 221 | offset = OD_FILT_BSTRIDE; |
| 222 | else |
| 223 | offset = 1; |
| 224 | for (i = 0; i < 4; i++) { |
| 225 | for (j = 0; j < 4; j++) { |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 226 | int16_t yy; |
| 227 | int16_t sum; |
| 228 | int16_t p; |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 229 | yy = in[i * OD_FILT_BSTRIDE + j]; |
| 230 | sum = 0; |
| 231 | p = in[i * OD_FILT_BSTRIDE + j + offset] - yy; |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 232 | if (abs(p) < threshold) sum += p; |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 233 | p = in[i * OD_FILT_BSTRIDE + j - offset] - yy; |
Jean-Marc Valin | 4713d8d | 2016-09-16 11:06:50 -0400 | [diff] [blame] | 234 | if (abs(p) < threshold) sum += p; |
Jean-Marc Valin | ea64c34 | 2016-09-15 16:23:12 -0400 | [diff] [blame] | 235 | y[i * ystride + j] = yy + ((5 * sum + 8) >> 4); |
| 236 | } |
| 237 | } |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* This table approximates x^0.16 with the index being log2(x). It is clamped |
| 241 | to [-.5, 3]. The table is computed as: |
| 242 | round(256*min(3, max(.5, 1.08*(sqrt(2)*2.^([0:17]+8)/256/256).^.16))) */ |
| 243 | static const int16_t OD_THRESH_TABLE_Q8[18] = { |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 244 | 128, 134, 150, 168, 188, 210, 234, 262, 292, |
| 245 | 327, 365, 408, 455, 509, 569, 635, 710, 768, |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
Jean-Marc Valin | a8ce2c9 | 2016-10-07 18:10:19 -0400 | [diff] [blame] | 248 | /* Compute deringing filter threshold for an 8x8 block based on the |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 249 | directional variance difference. A high variance difference means that we |
| 250 | have a highly directional pattern (e.g. a high contrast edge), so we can |
| 251 | apply more deringing. A low variance means that we either have a low |
| 252 | contrast edge, or a non-directional texture, so we want to be careful not |
| 253 | to blur. */ |
Jean-Marc Valin | a8ce2c9 | 2016-10-07 18:10:19 -0400 | [diff] [blame] | 254 | static INLINE int od_adjust_thresh(int threshold, int32_t var) { |
| 255 | int v1; |
| 256 | /* We use the variance of 8x8 blocks to adjust the threshold. */ |
| 257 | v1 = OD_MINI(32767, var >> 6); |
| 258 | return (threshold * OD_THRESH_TABLE_Q8[OD_ILOG(v1)] + 128) >> 8; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Jean-Marc Valin | cf23aef | 2016-10-11 17:47:36 -0400 | [diff] [blame] | 261 | static INLINE void copy_8x8_16bit(int16_t *dst, int dstride, int16_t *src, int sstride) { |
| 262 | int i, j; |
| 263 | for (i = 0; i < 8; i++) |
| 264 | for (j = 0; j < 8; j++) |
| 265 | dst[i * dstride + j] = src[i * sstride + j]; |
| 266 | } |
| 267 | |
| 268 | static INLINE void copy_4x4_16bit(int16_t *dst, int dstride, int16_t *src, int sstride) { |
| 269 | int i, j; |
| 270 | for (i = 0; i < 4; i++) |
| 271 | for (j = 0; j < 4; j++) |
| 272 | dst[i * dstride + j] = src[i * sstride + j]; |
| 273 | } |
| 274 | |
| 275 | /* TODO: Optimize this function for SSE. */ |
Jean-Marc Valin | 82c65fc | 2016-10-12 18:09:48 -0400 | [diff] [blame] | 276 | void copy_blocks_16bit(int16_t *dst, int dstride, int16_t *src, |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame^] | 277 | dering_list *dlist, int dering_count, int bsize) |
Jean-Marc Valin | cf23aef | 2016-10-11 17:47:36 -0400 | [diff] [blame] | 278 | { |
| 279 | int bi, bx, by; |
| 280 | if (bsize == 3) { |
| 281 | for (bi = 0; bi < dering_count; bi++) { |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame^] | 282 | by = dlist[bi].by; |
| 283 | bx = dlist[bi].bx; |
Jean-Marc Valin | cf23aef | 2016-10-11 17:47:36 -0400 | [diff] [blame] | 284 | copy_8x8_16bit(&dst[(by << 3) * dstride + (bx << 3)], |
| 285 | dstride, |
Jean-Marc Valin | 82c65fc | 2016-10-12 18:09:48 -0400 | [diff] [blame] | 286 | &src[bi << 2*bsize], 1 << bsize); |
Jean-Marc Valin | cf23aef | 2016-10-11 17:47:36 -0400 | [diff] [blame] | 287 | } |
| 288 | } else { |
| 289 | for (bi = 0; bi < dering_count; bi++) { |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame^] | 290 | by = dlist[bi].by; |
| 291 | bx = dlist[bi].bx; |
Jean-Marc Valin | cf23aef | 2016-10-11 17:47:36 -0400 | [diff] [blame] | 292 | copy_4x4_16bit(&dst[(by << 2) * dstride + (bx << 2)], |
| 293 | dstride, |
Jean-Marc Valin | 82c65fc | 2016-10-12 18:09:48 -0400 | [diff] [blame] | 294 | &src[bi << 2*bsize], 1 << bsize); |
Jean-Marc Valin | cf23aef | 2016-10-11 17:47:36 -0400 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Jean-Marc Valin | ca1eb5d | 2016-10-13 15:24:39 -0400 | [diff] [blame] | 299 | void od_dering(int16_t *y, int16_t *in, int xdec, |
Yaowu Xu | 3e90f84 | 2016-11-02 08:22:02 -0700 | [diff] [blame] | 300 | int dir[OD_DERING_NBLOCKS][OD_DERING_NBLOCKS], int pli, |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame^] | 301 | dering_list *dlist, int dering_count, int threshold, |
Jean-Marc Valin | 2c616e6 | 2016-08-23 02:45:43 -0400 | [diff] [blame] | 302 | int coeff_shift) { |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 303 | int bi; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 304 | int bx; |
| 305 | int by; |
Yaowu Xu | 3e90f84 | 2016-11-02 08:22:02 -0700 | [diff] [blame] | 306 | int bsize; |
Jean-Marc Valin | a8ce2c9 | 2016-10-07 18:10:19 -0400 | [diff] [blame] | 307 | int filter2_thresh[OD_DERING_NBLOCKS][OD_DERING_NBLOCKS]; |
Michael Bebenita | 7227b65 | 2016-10-06 14:27:34 -0700 | [diff] [blame] | 308 | od_filter_dering_direction_func filter_dering_direction[OD_DERINGSIZES] = { |
Yaowu Xu | 3e90f84 | 2016-11-02 08:22:02 -0700 | [diff] [blame] | 309 | od_filter_dering_direction_4x4, od_filter_dering_direction_8x8 |
Michael Bebenita | 7227b65 | 2016-10-06 14:27:34 -0700 | [diff] [blame] | 310 | }; |
| 311 | od_filter_dering_orthogonal_func filter_dering_orthogonal[OD_DERINGSIZES] = { |
Yaowu Xu | 3e90f84 | 2016-11-02 08:22:02 -0700 | [diff] [blame] | 312 | od_filter_dering_orthogonal_4x4, od_filter_dering_orthogonal_8x8 |
Michael Bebenita | 7227b65 | 2016-10-06 14:27:34 -0700 | [diff] [blame] | 313 | }; |
Yaowu Xu | 3e90f84 | 2016-11-02 08:22:02 -0700 | [diff] [blame] | 314 | bsize = 3 - xdec; |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 315 | if (pli == 0) { |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 316 | for (bi = 0; bi < dering_count; bi++) { |
Jean-Marc Valin | 643902d | 2016-10-12 17:45:07 -0400 | [diff] [blame] | 317 | int32_t var; |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame^] | 318 | by = dlist[bi].by; |
| 319 | bx = dlist[bi].bx; |
Jean-Marc Valin | eab77ea | 2016-10-13 14:18:49 -0400 | [diff] [blame] | 320 | dir[by][bx] = od_dir_find8(&in[8 * by * OD_FILT_BSTRIDE + 8 * bx], OD_FILT_BSTRIDE, |
Jean-Marc Valin | 643902d | 2016-10-12 17:45:07 -0400 | [diff] [blame] | 321 | &var, coeff_shift); |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 322 | /* Deringing orthogonal to the direction uses a tighter threshold |
| 323 | because we want to be conservative. We've presumably already |
| 324 | achieved some deringing, so the amount of change is expected |
| 325 | to be low. Also, since we might be filtering across an edge, we |
| 326 | want to make sure not to blur it. That being said, we might want |
| 327 | to be a little bit more aggressive on pure horizontal/vertical |
| 328 | since the ringing there tends to be directional, so it doesn't |
| 329 | get removed by the directional filtering. */ |
| 330 | filter2_thresh[by][bx] = (filter_dering_direction[bsize - OD_LOG_BSIZE0])( |
Jean-Marc Valin | 82c65fc | 2016-10-12 18:09:48 -0400 | [diff] [blame] | 331 | &y[bi << 2*bsize], 1 << bsize, |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 332 | &in[(by * OD_FILT_BSTRIDE << bsize) + (bx << bsize)], |
Jean-Marc Valin | 643902d | 2016-10-12 17:45:07 -0400 | [diff] [blame] | 333 | od_adjust_thresh(threshold, var), dir[by][bx]); |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 334 | } |
clang-format | 21a0c2c | 2016-08-18 15:10:22 -0700 | [diff] [blame] | 335 | } else { |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 336 | for (bi = 0; bi < dering_count; bi++) { |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame^] | 337 | by = dlist[bi].by; |
| 338 | bx = dlist[bi].bx; |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 339 | filter2_thresh[by][bx] = (filter_dering_direction[bsize - OD_LOG_BSIZE0])( |
Jean-Marc Valin | 82c65fc | 2016-10-12 18:09:48 -0400 | [diff] [blame] | 340 | &y[bi << 2*bsize], 1 << bsize, |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 341 | &in[(by * OD_FILT_BSTRIDE << bsize) + (bx << bsize)], threshold, |
| 342 | dir[by][bx]); |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame^] | 345 | copy_blocks_16bit(in, OD_FILT_BSTRIDE, y, dlist, dering_count, |
Jean-Marc Valin | cf23aef | 2016-10-11 17:47:36 -0400 | [diff] [blame] | 346 | bsize); |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 347 | for (bi = 0; bi < dering_count; bi++) { |
Jean-Marc Valin | e046503 | 2016-10-18 15:56:37 -0400 | [diff] [blame^] | 348 | by = dlist[bi].by; |
| 349 | bx = dlist[bi].bx; |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 350 | if (filter2_thresh[by][bx] == 0) continue; |
| 351 | (filter_dering_orthogonal[bsize - OD_LOG_BSIZE0])( |
Jean-Marc Valin | 82c65fc | 2016-10-12 18:09:48 -0400 | [diff] [blame] | 352 | &y[bi << 2*bsize], 1 << bsize, |
Jean-Marc Valin | 3e44bcc | 2016-10-11 16:53:59 -0400 | [diff] [blame] | 353 | &in[(by * OD_FILT_BSTRIDE << bsize) + (bx << bsize)], filter2_thresh[by][bx], |
| 354 | dir[by][bx]); |
Yaowu Xu | 253c001 | 2016-08-15 10:27:19 -0700 | [diff] [blame] | 355 | } |
| 356 | } |