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