1 /* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "vpx_config.h" 12 #include "vp9/common/vp9_common.h" 13 #include "vp9/common/vp9_loopfilter.h" 14 #include "vp9/common/vp9_onyxc_int.h" 15 16 static INLINE int8_t signed_char_clamp(int t) { 17 return (int8_t)clamp(t, -128, 127); 18 } 19 20 // should we apply any filter at all: 11111111 yes, 00000000 no 21 static INLINE int8_t filter_mask(uint8_t limit, uint8_t blimit, 22 uint8_t p3, uint8_t p2, 23 uint8_t p1, uint8_t p0, 24 uint8_t q0, uint8_t q1, 25 uint8_t q2, uint8_t q3) { 26 int8_t mask = 0; 27 mask |= (abs(p3 - p2) > limit) * -1; 28 mask |= (abs(p2 - p1) > limit) * -1; 29 mask |= (abs(p1 - p0) > limit) * -1; 30 mask |= (abs(q1 - q0) > limit) * -1; 31 mask |= (abs(q2 - q1) > limit) * -1; 32 mask |= (abs(q3 - q2) > limit) * -1; 33 mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit) * -1; 34 return ~mask; 35 } 36 37 static INLINE int8_t flat_mask4(uint8_t thresh, 38 uint8_t p3, uint8_t p2, 39 uint8_t p1, uint8_t p0, 40 uint8_t q0, uint8_t q1, 41 uint8_t q2, uint8_t q3) { 42 int8_t mask = 0; 43 mask |= (abs(p1 - p0) > thresh) * -1; 44 mask |= (abs(q1 - q0) > thresh) * -1; 45 mask |= (abs(p2 - p0) > thresh) * -1; 46 mask |= (abs(q2 - q0) > thresh) * -1; 47 mask |= (abs(p3 - p0) > thresh) * -1; 48 mask |= (abs(q3 - q0) > thresh) * -1; 49 return ~mask; 50 } 51 52 static INLINE int8_t flat_mask5(uint8_t thresh, 53 uint8_t p4, uint8_t p3, 54 uint8_t p2, uint8_t p1, 55 uint8_t p0, uint8_t q0, 56 uint8_t q1, uint8_t q2, 57 uint8_t q3, uint8_t q4) { 58 int8_t mask = ~flat_mask4(thresh, p3, p2, p1, p0, q0, q1, q2, q3); 59 mask |= (abs(p4 - p0) > thresh) * -1; 60 mask |= (abs(q4 - q0) > thresh) * -1; 61 return ~mask; 62 } 63 64 // is there high edge variance internal edge: 11111111 yes, 00000000 no 65 static INLINE int8_t hev_mask(uint8_t thresh, uint8_t p1, uint8_t p0, 66 uint8_t q0, uint8_t q1) { 67 int8_t hev = 0; 68 hev |= (abs(p1 - p0) > thresh) * -1; 69 hev |= (abs(q1 - q0) > thresh) * -1; 70 return hev; 71 } 72 73 static INLINE void filter4(int8_t mask, uint8_t hev, uint8_t *op1, 74 uint8_t *op0, uint8_t *oq0, uint8_t *oq1) { 75 int8_t filter1, filter2; 76 77 const int8_t ps1 = (int8_t) *op1 ^ 0x80; 78 const int8_t ps0 = (int8_t) *op0 ^ 0x80; 79 const int8_t qs0 = (int8_t) *oq0 ^ 0x80; 80 const int8_t qs1 = (int8_t) *oq1 ^ 0x80; 81 82 // add outer taps if we have high edge variance 83 int8_t filter = signed_char_clamp(ps1 - qs1) & hev; 84 85 // inner taps 86 filter = signed_char_clamp(filter + 3 * (qs0 - ps0)) & mask; 87 88 // save bottom 3 bits so that we round one side +4 and the other +3 89 // if it equals 4 we'll set to adjust by -1 to account for the fact 90 // we'd round 3 the other way 91 filter1 = signed_char_clamp(filter + 4) >> 3; 92 filter2 = signed_char_clamp(filter + 3) >> 3; 93 94 *oq0 = signed_char_clamp(qs0 - filter1) ^ 0x80; 95 *op0 = signed_char_clamp(ps0 + filter2) ^ 0x80; 96 97 // outer tap adjustments 98 filter = ROUND_POWER_OF_TWO(filter1, 1) & ~hev; 99 100 *oq1 = signed_char_clamp(qs1 - filter) ^ 0x80; 101 *op1 = signed_char_clamp(ps1 + filter) ^ 0x80; 102 } 103 104 void vp9_loop_filter_horizontal_edge_c(uint8_t *s, int p /* pitch */, 105 const uint8_t *blimit, 106 const uint8_t *limit, 107 const uint8_t *thresh, 108 int count) { 109 int i; 110 111 // loop filter designed to work using chars so that we can make maximum use 112 // of 8 bit simd instructions. 113 for (i = 0; i < 8 * count; ++i) { 114 const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p]; 115 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p]; 116 const int8_t mask = filter_mask(*limit, *blimit, 117 p3, p2, p1, p0, q0, q1, q2, q3); 118 const int8_t hev = hev_mask(*thresh, p1, p0, q0, q1); 119 filter4(mask, hev, s - 2 * p, s - 1 * p, s, s + 1 * p); 120 ++s; 121 } 122 } 123 124 void vp9_loop_filter_vertical_edge_c(uint8_t *s, int pitch, 125 const uint8_t *blimit, 126 const uint8_t *limit, 127 const uint8_t *thresh, 128 int count) { 129 int i; 130 131 // loop filter designed to work using chars so that we can make maximum use 132 // of 8 bit simd instructions. 133 for (i = 0; i < 8 * count; ++i) { 134 const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1]; 135 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3]; 136 const int8_t mask = filter_mask(*limit, *blimit, 137 p3, p2, p1, p0, q0, q1, q2, q3); 138 const int8_t hev = hev_mask(*thresh, p1, p0, q0, q1); 139 filter4(mask, hev, s - 2, s - 1, s, s + 1); 140 s += pitch; 141 } 142 } 143 144 static INLINE void filter8(int8_t mask, uint8_t hev, uint8_t flat, 145 uint8_t *op3, uint8_t *op2, 146 uint8_t *op1, uint8_t *op0, 147 uint8_t *oq0, uint8_t *oq1, 148 uint8_t *oq2, uint8_t *oq3) { 149 if (flat && mask) { 150 const uint8_t p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0; 151 const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3; 152 153 // 7-tap filter [1, 1, 1, 2, 1, 1, 1] 154 *op2 = ROUND_POWER_OF_TWO(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3); 155 *op1 = ROUND_POWER_OF_TWO(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3); 156 *op0 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3); 157 *oq0 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3); 158 *oq1 = ROUND_POWER_OF_TWO(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3); 159 *oq2 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3); 160 } else { 161 filter4(mask, hev, op1, op0, oq0, oq1); 162 } 163 } 164 165 void vp9_mbloop_filter_horizontal_edge_c(uint8_t *s, int p, 166 const uint8_t *blimit, 167 const uint8_t *limit, 168 const uint8_t *thresh, 169 int count) { 170 int i; 171 172 // loop filter designed to work using chars so that we can make maximum use 173 // of 8 bit simd instructions. 174 for (i = 0; i < 8 * count; ++i) { 175 const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p]; 176 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p]; 177 178 const int8_t mask = filter_mask(*limit, *blimit, 179 p3, p2, p1, p0, q0, q1, q2, q3); 180 const int8_t hev = hev_mask(*thresh, p1, p0, q0, q1); 181 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3); 182 filter8(mask, hev, flat, s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p, 183 s, s + 1 * p, s + 2 * p, s + 3 * p); 184 ++s; 185 } 186 } 187 188 void vp9_mbloop_filter_vertical_edge_c(uint8_t *s, int pitch, 189 const uint8_t *blimit, 190 const uint8_t *limit, 191 const uint8_t *thresh, 192 int count) { 193 int i; 194 195 for (i = 0; i < 8 * count; ++i) { 196 const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1]; 197 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3]; 198 const int8_t mask = filter_mask(*limit, *blimit, 199 p3, p2, p1, p0, q0, q1, q2, q3); 200 const int8_t hev = hev_mask(thresh[0], p1, p0, q0, q1); 201 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3); 202 filter8(mask, hev, flat, s - 4, s - 3, s - 2, s - 1, 203 s, s + 1, s + 2, s + 3); 204 s += pitch; 205 } 206 } 207 208 static INLINE void filter16(int8_t mask, uint8_t hev, 209 uint8_t flat, uint8_t flat2, 210 uint8_t *op7, uint8_t *op6, 211 uint8_t *op5, uint8_t *op4, 212 uint8_t *op3, uint8_t *op2, 213 uint8_t *op1, uint8_t *op0, 214 uint8_t *oq0, uint8_t *oq1, 215 uint8_t *oq2, uint8_t *oq3, 216 uint8_t *oq4, uint8_t *oq5, 217 uint8_t *oq6, uint8_t *oq7) { 218 if (flat2 && flat && mask) { 219 const uint8_t p7 = *op7, p6 = *op6, p5 = *op5, p4 = *op4, 220 p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0; 221 222 const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3, 223 q4 = *oq4, q5 = *oq5, q6 = *oq6, q7 = *oq7; 224 225 // 15-tap filter [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1] 226 *op6 = ROUND_POWER_OF_TWO(p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 + 227 q0, 4); 228 *op5 = ROUND_POWER_OF_TWO(p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 + 229 q0 + q1, 4); 230 *op4 = ROUND_POWER_OF_TWO(p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 + 231 q0 + q1 + q2, 4); 232 *op3 = ROUND_POWER_OF_TWO(p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 + 233 q0 + q1 + q2 + q3, 4); 234 *op2 = ROUND_POWER_OF_TWO(p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 + 235 q0 + q1 + q2 + q3 + q4, 4); 236 *op1 = ROUND_POWER_OF_TWO(p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 + 237 q0 + q1 + q2 + q3 + q4 + q5, 4); 238 *op0 = ROUND_POWER_OF_TWO(p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 + 239 q0 + q1 + q2 + q3 + q4 + q5 + q6, 4); 240 *oq0 = ROUND_POWER_OF_TWO(p6 + p5 + p4 + p3 + p2 + p1 + p0 + 241 q0 * 2 + q1 + q2 + q3 + q4 + q5 + q6 + q7, 4); 242 *oq1 = ROUND_POWER_OF_TWO(p5 + p4 + p3 + p2 + p1 + p0 + 243 q0 + q1 * 2 + q2 + q3 + q4 + q5 + q6 + q7 * 2, 4); 244 *oq2 = ROUND_POWER_OF_TWO(p4 + p3 + p2 + p1 + p0 + 245 q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3, 4); 246 *oq3 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + p0 + 247 q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4, 4); 248 *oq4 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 249 q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5, 4); 250 *oq5 = ROUND_POWER_OF_TWO(p1 + p0 + 251 q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6, 4); 252 *oq6 = ROUND_POWER_OF_TWO(p0 + 253 q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7, 4); 254 } else { 255 filter8(mask, hev, flat, op3, op2, op1, op0, oq0, oq1, oq2, oq3); 256 } 257 } 258 259 void vp9_mb_lpf_horizontal_edge_w_c(uint8_t *s, int p, 260 const uint8_t *blimit, 261 const uint8_t *limit, 262 const uint8_t *thresh, 263 int count) { 264 int i; 265 266 // loop filter designed to work using chars so that we can make maximum use 267 // of 8 bit simd instructions. 268 for (i = 0; i < 8 * count; ++i) { 269 const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p]; 270 const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p]; 271 const int8_t mask = filter_mask(*limit, *blimit, 272 p3, p2, p1, p0, q0, q1, q2, q3); 273 const int8_t hev = hev_mask(*thresh, p1, p0, q0, q1); 274 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3); 275 const int8_t flat2 = flat_mask5(1, 276 s[-8 * p], s[-7 * p], s[-6 * p], s[-5 * p], p0, 277 q0, s[4 * p], s[5 * p], s[6 * p], s[7 * p]); 278 279 filter16(mask, hev, flat, flat2, 280 s - 8 * p, s - 7 * p, s - 6 * p, s - 5 * p, 281 s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p, 282 s, s + 1 * p, s + 2 * p, s + 3 * p, 283 s + 4 * p, s + 5 * p, s + 6 * p, s + 7 * p); 284 ++s; 285 } 286 } 287 288 void vp9_mb_lpf_vertical_edge_w_c(uint8_t *s, int p, 289 const uint8_t *blimit, 290 const uint8_t *limit, 291 const uint8_t *thresh) { 292 int i; 293 294 for (i = 0; i < 8; ++i) { 295 const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1]; 296 const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3]; 297 const int8_t mask = filter_mask(*limit, *blimit, 298 p3, p2, p1, p0, q0, q1, q2, q3); 299 const int8_t hev = hev_mask(*thresh, p1, p0, q0, q1); 300 const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3); 301 const int8_t flat2 = flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0, 302 q0, s[4], s[5], s[6], s[7]); 303 304 filter16(mask, hev, flat, flat2, 305 s - 8, s - 7, s - 6, s - 5, s - 4, s - 3, s - 2, s - 1, 306 s, s + 1, s + 2, s + 3, s + 4, s + 5, s + 6, s + 7); 307 s += p; 308 } 309 } 310