loopfilter_filters.c 14.7 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10
 */
#include <stdlib.h>
11
#include "vpx_config.h"
John Koleszar's avatar
John Koleszar committed
12 13 14 15 16
#include "loopfilter.h"
#include "onyxc_int.h"

typedef unsigned char uc;

John Koleszar's avatar
John Koleszar committed
17 18 19 20
static __inline signed char vp8_signed_char_clamp(int t) {
  t = (t < -128 ? -128 : t);
  t = (t > 127 ? 127 : t);
  return (signed char) t;
John Koleszar's avatar
John Koleszar committed
21 22 23
}


24
/* should we apply any filter at all ( 11111111 yes, 00000000 no) */
Attila Nagy's avatar
Attila Nagy committed
25
static __inline signed char vp8_filter_mask(uc limit, uc blimit,
John Koleszar's avatar
John Koleszar committed
26 27 28 29 30 31 32 33 34 35 36 37
                                            uc p3, uc p2, uc p1, uc p0,
                                            uc q0, uc q1, uc q2, uc q3) {
  signed char mask = 0;
  mask |= (abs(p3 - p2) > limit) * -1;
  mask |= (abs(p2 - p1) > limit) * -1;
  mask |= (abs(p1 - p0) > limit) * -1;
  mask |= (abs(q1 - q0) > limit) * -1;
  mask |= (abs(q2 - q1) > limit) * -1;
  mask |= (abs(q3 - q2) > limit) * -1;
  mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2  > blimit) * -1;
  mask = ~mask;
  return mask;
John Koleszar's avatar
John Koleszar committed
38 39
}

40
/* is there high variance internal edge ( 11111111 yes, 00000000 no) */
John Koleszar's avatar
John Koleszar committed
41 42 43 44 45
static __inline signed char vp8_hevmask(uc thresh, uc p1, uc p0, uc q0, uc q1) {
  signed char hev = 0;
  hev  |= (abs(p1 - p0) > thresh) * -1;
  hev  |= (abs(q1 - q0) > thresh) * -1;
  return hev;
John Koleszar's avatar
John Koleszar committed
46 47
}

Attila Nagy's avatar
Attila Nagy committed
48
static __inline void vp8_filter(signed char mask, uc hev, uc *op1,
John Koleszar's avatar
John Koleszar committed
49
                                uc *op0, uc *oq0, uc *oq1)
John Koleszar's avatar
John Koleszar committed
50 51

{
John Koleszar's avatar
John Koleszar committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  signed char ps0, qs0;
  signed char ps1, qs1;
  signed char vp8_filter, Filter1, Filter2;
  signed char u;

  ps1 = (signed char) * op1 ^ 0x80;
  ps0 = (signed char) * op0 ^ 0x80;
  qs0 = (signed char) * oq0 ^ 0x80;
  qs1 = (signed char) * oq1 ^ 0x80;

  /* add outer taps if we have high edge variance */
  vp8_filter = vp8_signed_char_clamp(ps1 - qs1);
  vp8_filter &= hev;

  /* inner taps */
  vp8_filter = vp8_signed_char_clamp(vp8_filter + 3 * (qs0 - ps0));
  vp8_filter &= mask;

  /* save bottom 3 bits so that we round one side +4 and the other +3
   * if it equals 4 we'll set to adjust by -1 to account for the fact
   * we'd round 3 the other way
   */
  Filter1 = vp8_signed_char_clamp(vp8_filter + 4);
  Filter2 = vp8_signed_char_clamp(vp8_filter + 3);
  Filter1 >>= 3;
  Filter2 >>= 3;
  u = vp8_signed_char_clamp(qs0 - Filter1);
  *oq0 = u ^ 0x80;
  u = vp8_signed_char_clamp(ps0 + Filter2);
  *op0 = u ^ 0x80;
  vp8_filter = Filter1;

  /* outer tap adjustments */
  vp8_filter += 1;
  vp8_filter >>= 1;
  vp8_filter &= ~hev;

  u = vp8_signed_char_clamp(qs1 - vp8_filter);
  *oq1 = u ^ 0x80;
  u = vp8_signed_char_clamp(ps1 + vp8_filter);
  *op1 = u ^ 0x80;

}
95

John Koleszar's avatar
John Koleszar committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
void vp8_loop_filter_horizontal_edge_c
(
  unsigned char *s,
  int p, /* pitch */
  const unsigned char *blimit,
  const unsigned char *limit,
  const unsigned char *thresh,
  int count
) {
  int  hev = 0; /* high edge variance */
  signed char mask = 0;
  int i = 0;

  /* loop filter designed to work using chars so that we can make maximum use
   * of 8 bit simd instructions.
   */
  do {
    mask = vp8_filter_mask(limit[0], blimit[0],
                           s[-4 * p], s[-3 * p], s[-2 * p], s[-1 * p],
                           s[0 * p], s[1 * p], s[2 * p], s[3 * p]);

    hev = vp8_hevmask(thresh[0], s[-2 * p], s[-1 * p], s[0 * p], s[1 * p]);

    vp8_filter(mask, hev, s - 2 * p, s - 1 * p, s, s + 1 * p);

    ++s;
  } while (++i < count * 8);
}

void vp8_loop_filter_vertical_edge_c
(
  unsigned char *s,
  int p,
  const unsigned char *blimit,
  const unsigned char *limit,
  const unsigned char *thresh,
  int count
) {
  int  hev = 0; /* high edge variance */
  signed char mask = 0;
  int i = 0;

  /* loop filter designed to work using chars so that we can make maximum use
   * of 8 bit simd instructions.
   */
  do {
    mask = vp8_filter_mask(limit[0], blimit[0],
                           s[-4], s[-3], s[-2], s[-1],
                           s[0], s[1], s[2], s[3]);

    hev = vp8_hevmask(thresh[0], s[-2], s[-1], s[0], s[1]);

    vp8_filter(mask, hev, s - 2, s - 1, s, s + 1);

    s += p;
  } while (++i < count * 8);
}
static __inline signed char vp8_flatmask(uc thresh,
                                         uc p4, uc p3, uc p2, uc p1, uc p0,
                                         uc q0, uc q1, uc q2, uc q3, uc q4) {
  signed char flat = 0;
  flat |= (abs(p1 - p0) > 1) * -1;
  flat |= (abs(q1 - q0) > 1) * -1;
  flat |= (abs(p0 - p2) > 1) * -1;
  flat |= (abs(q0 - q2) > 1) * -1;
  flat |= (abs(p3 - p0) > 1) * -1;
  flat |= (abs(q3 - q0) > 1) * -1;
  flat |= (abs(p4 - p0) > 1) * -1;
  flat |= (abs(q4 - q0) > 1) * -1;
  flat = ~flat;
  return flat;
}

static __inline void vp8_mbfilter(signed char mask, uc hev, uc flat,
                                  uc *op4, uc *op3, uc *op2, uc *op1, uc *op0,
                                  uc *oq0, uc *oq1, uc *oq2, uc *oq3, uc *oq4) {
  /* use a 7 tap filter [1, 1, 1, 2, 1, 1, 1] for flat line */
  if (flat && mask) {
    unsigned char p0, q0;
    unsigned char p1, q1;
    unsigned char p2, q2;
    unsigned char p3, q3;
    unsigned char p4, q4;

    p4 = *op4;
    p3 = *op3;
    p2 = *op2;
    p1 = *op1;
    p0 = *op0;
    q0 = *oq0;
    q1 = *oq1;
    q2 = *oq2;
    q3 = *oq3;
    q4 = *oq4;

    *op2 = (p4 + p4 + p3 + p2 + p2 + p1 + p0 + q0 + 4) >> 3;
    *op1 = (p4 + p3 + p2 + p1 + p1 + p0 + q0 + q1 + 4) >> 3;
    *op0 = (p3 + p2 + p1 + p0 + p0 + q0 + q1 + q2 + 4) >> 3;
    *oq0 = (p2 + p1 + p0 + q0 + q0 + q1 + q2 + q3 + 4) >> 3;
    *oq1 = (p1 + p0 + q0 + q1 + q1 + q2 + q3 + q4 + 4) >> 3;
    *oq2 = (p0 + q0 + q1 + q2 + q2 + q3 + q4 + q4 + 4) >> 3;
  } else {
John Koleszar's avatar
John Koleszar committed
198 199 200 201 202 203 204 205 206 207
    signed char ps0, qs0;
    signed char ps1, qs1;
    signed char vp8_filter, Filter1, Filter2;
    signed char u;

    ps1 = (signed char) * op1 ^ 0x80;
    ps0 = (signed char) * op0 ^ 0x80;
    qs0 = (signed char) * oq0 ^ 0x80;
    qs1 = (signed char) * oq1 ^ 0x80;

208
    /* add outer taps if we have high edge variance */
John Koleszar's avatar
John Koleszar committed
209 210 211
    vp8_filter = vp8_signed_char_clamp(ps1 - qs1);
    vp8_filter &= hev;

212
    /* inner taps */
John Koleszar's avatar
John Koleszar committed
213 214 215 216 217 218 219
    vp8_filter = vp8_signed_char_clamp(vp8_filter + 3 * (qs0 - ps0));
    vp8_filter &= mask;

    Filter1 = vp8_signed_char_clamp(vp8_filter + 4);
    Filter2 = vp8_signed_char_clamp(vp8_filter + 3);
    Filter1 >>= 3;
    Filter2 >>= 3;
220

John Koleszar's avatar
John Koleszar committed
221 222 223 224 225 226
    u = vp8_signed_char_clamp(qs0 - Filter1);
    *oq0 = u ^ 0x80;
    u = vp8_signed_char_clamp(ps0 + Filter2);
    *op0 = u ^ 0x80;
    vp8_filter = Filter1;

227
    /* outer tap adjustments */
John Koleszar's avatar
John Koleszar committed
228 229 230 231 232 233 234 235
    vp8_filter += 1;
    vp8_filter >>= 1;
    vp8_filter &= ~hev;

    u = vp8_signed_char_clamp(qs1 - vp8_filter);
    *oq1 = u ^ 0x80;
    u = vp8_signed_char_clamp(ps1 + vp8_filter);
    *op1 = u ^ 0x80;
John Koleszar's avatar
John Koleszar committed
236
  }
237
}
John Koleszar's avatar
John Koleszar committed
238 239
void vp8_mbloop_filter_horizontal_edge_c
(
John Koleszar's avatar
John Koleszar committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  unsigned char *s,
  int p,
  const unsigned char *blimit,
  const unsigned char *limit,
  const unsigned char *thresh,
  int count
) {
  signed char hev = 0; /* high edge variance */
  signed char mask = 0;
  signed char flat = 0;
  int i = 0;

  /* loop filter designed to work using chars so that we can make maximum use
   * of 8 bit simd instructions.
   */
  do {

    mask = vp8_filter_mask(limit[0], blimit[0],
                           s[-4 * p], s[-3 * p], s[-2 * p], s[-1 * p],
                           s[ 0 * p], s[ 1 * p], s[ 2 * p], s[ 3 * p]);

    hev = vp8_hevmask(thresh[0], s[-2 * p], s[-1 * p], s[0 * p], s[1 * p]);

    flat = vp8_flatmask(thresh[0],
                        s[-5 * p], s[-4 * p], s[-3 * p], s[-2 * p], s[-1 * p],
                        s[ 0 * p], s[ 1 * p], s[ 2 * p], s[ 3 * p], s[ 4 * p]);
    vp8_mbfilter(mask, hev, flat,
                 s - 5 * p, s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p,
                 s,       s + 1 * p, s + 2 * p, s + 3 * p, s + 4 * p);

    ++s;
  } while (++i < count * 8);
John Koleszar's avatar
John Koleszar committed
272 273 274 275 276

}

void vp8_mbloop_filter_vertical_edge_c
(
John Koleszar's avatar
John Koleszar committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  unsigned char *s,
  int p,
  const unsigned char *blimit,
  const unsigned char *limit,
  const unsigned char *thresh,
  int count
) {
  signed char hev = 0; /* high edge variance */
  signed char mask = 0;
  signed char flat = 0;
  int i = 0;

  do {

    mask = vp8_filter_mask(limit[0], blimit[0],
                           s[-4], s[-3], s[-2], s[-1],
                           s[0], s[1], s[2], s[3]);

    hev = vp8_hevmask(thresh[0], s[-2], s[-1], s[0], s[1]);
    flat = vp8_flatmask(thresh[0],
                        s[-5], s[-4], s[-3], s[-2], s[-1],
                        s[ 0], s[ 1], s[ 2], s[ 3], s[ 4]);
    vp8_mbfilter(mask, hev, flat,
                 s - 5, s - 4, s - 3, s - 2, s - 1,
                 s,     s + 1, s + 2, s + 3, s + 4);
    s += p;
  } while (++i < count * 8);
John Koleszar's avatar
John Koleszar committed
304 305 306

}

307
/* should we apply any filter at all ( 11111111 yes, 00000000 no) */
308 309
static __inline signed char vp8_simple_filter_mask(uc blimit,
                                                   uc p1, uc p0,
John Koleszar's avatar
John Koleszar committed
310 311 312 313 314 315 316
                                                   uc q0, uc q1) {
  /* Why does this cause problems for win32?
   * error C2143: syntax error : missing ';' before 'type'
   *  (void) limit;
   */
  signed char mask = (abs(p0 - q0) * 2 + abs(p1 - q1) / 2  <= blimit) * -1;
  return mask;
John Koleszar's avatar
John Koleszar committed
317 318
}

319 320
static __inline void vp8_simple_filter(signed char mask,
                                       uc *op1, uc *op0,
John Koleszar's avatar
John Koleszar committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
                                       uc *oq0, uc *oq1) {
  signed char vp8_filter, Filter1, Filter2;
  signed char p1 = (signed char) * op1 ^ 0x80;
  signed char p0 = (signed char) * op0 ^ 0x80;
  signed char q0 = (signed char) * oq0 ^ 0x80;
  signed char q1 = (signed char) * oq1 ^ 0x80;
  signed char u;

  vp8_filter = vp8_signed_char_clamp(p1 - q1);
  vp8_filter = vp8_signed_char_clamp(vp8_filter + 3 * (q0 - p0));
  vp8_filter &= mask;

  /* save bottom 3 bits so that we round one side +4 and the other +3 */
  Filter1 = vp8_signed_char_clamp(vp8_filter + 4);
  Filter1 >>= 3;
  u = vp8_signed_char_clamp(q0 - Filter1);
  *oq0  = u ^ 0x80;

  Filter2 = vp8_signed_char_clamp(vp8_filter + 3);
  Filter2 >>= 3;
  u = vp8_signed_char_clamp(p0 + Filter2);
  *op0 = u ^ 0x80;
John Koleszar's avatar
John Koleszar committed
343 344 345 346
}

void vp8_loop_filter_simple_horizontal_edge_c
(
John Koleszar's avatar
John Koleszar committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  unsigned char *s,
  int p,
  const unsigned char *blimit
) {
  signed char mask = 0;
  int i = 0;

  do {
    mask = vp8_simple_filter_mask(blimit[0],
                                  s[-2 * p], s[-1 * p],
                                  s[0 * p], s[1 * p]);
    vp8_simple_filter(mask,
                      s - 2 * p, s - 1 * p,
                      s, s + 1 * p);
    ++s;
  } while (++i < 16);
John Koleszar's avatar
John Koleszar committed
363 364 365 366
}

void vp8_loop_filter_simple_vertical_edge_c
(
John Koleszar's avatar
John Koleszar committed
367 368 369 370 371 372 373 374 375 376 377 378
  unsigned char *s,
  int p,
  const unsigned char *blimit
) {
  signed char mask = 0;
  int i = 0;

  do {
    mask = vp8_simple_filter_mask(blimit[0], s[-2], s[-1], s[0], s[1]);
    vp8_simple_filter(mask, s - 2, s - 1, s, s + 1);
    s += p;
  } while (++i < 16);
John Koleszar's avatar
John Koleszar committed
379 380

}
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

/* Vertical MB Filtering */
void vp8_loop_filter_mbv_c(unsigned char *y_ptr, unsigned char *u_ptr,
                           unsigned char *v_ptr, int y_stride, int uv_stride,
                           struct loop_filter_info *lfi) {
  vp8_mbloop_filter_vertical_edge_c(y_ptr, y_stride, lfi->mblim, lfi->lim, lfi->hev_thr, 2);

  if (u_ptr)
    vp8_mbloop_filter_vertical_edge_c(u_ptr, uv_stride, lfi->mblim, lfi->lim, lfi->hev_thr, 1);

  if (v_ptr)
    vp8_mbloop_filter_vertical_edge_c(v_ptr, uv_stride, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
}

/* Vertical B Filtering */
void vp8_loop_filter_bv_c(unsigned char *y_ptr, unsigned char *u_ptr,
                          unsigned char *v_ptr, int y_stride, int uv_stride,
                          struct loop_filter_info *lfi) {
  vp8_loop_filter_vertical_edge_c(y_ptr + 4, y_stride, lfi->blim, lfi->lim, lfi->hev_thr, 2);
  vp8_loop_filter_vertical_edge_c(y_ptr + 8, y_stride, lfi->blim, lfi->lim, lfi->hev_thr, 2);
  vp8_loop_filter_vertical_edge_c(y_ptr + 12, y_stride, lfi->blim, lfi->lim, lfi->hev_thr, 2);

  if (u_ptr)
    vp8_loop_filter_vertical_edge_c(u_ptr + 4, uv_stride, lfi->blim, lfi->lim, lfi->hev_thr, 1);

  if (v_ptr)
    vp8_loop_filter_vertical_edge_c(v_ptr + 4, uv_stride, lfi->blim, lfi->lim, lfi->hev_thr, 1);
}

/* Horizontal MB filtering */
void vp8_loop_filter_mbh_c(unsigned char *y_ptr, unsigned char *u_ptr,
                           unsigned char *v_ptr, int y_stride, int uv_stride,
                           struct loop_filter_info *lfi) {
  vp8_mbloop_filter_horizontal_edge_c(y_ptr, y_stride, lfi->mblim, lfi->lim, lfi->hev_thr, 2);

  if (u_ptr)
    vp8_mbloop_filter_horizontal_edge_c(u_ptr, uv_stride, lfi->mblim, lfi->lim, lfi->hev_thr, 1);

  if (v_ptr)
    vp8_mbloop_filter_horizontal_edge_c(v_ptr, uv_stride, lfi->mblim, lfi->lim, lfi->hev_thr, 1);
}

/* Horizontal B Filtering */
void vp8_loop_filter_bh_c(unsigned char *y_ptr, unsigned char *u_ptr,
                          unsigned char *v_ptr, int y_stride, int uv_stride,
                          struct loop_filter_info *lfi) {
  vp8_loop_filter_horizontal_edge_c(y_ptr + 4 * y_stride, y_stride, lfi->blim, lfi->lim, lfi->hev_thr, 2);
  vp8_loop_filter_horizontal_edge_c(y_ptr + 8 * y_stride, y_stride, lfi->blim, lfi->lim, lfi->hev_thr, 2);
  vp8_loop_filter_horizontal_edge_c(y_ptr + 12 * y_stride, y_stride, lfi->blim, lfi->lim, lfi->hev_thr, 2);

  if (u_ptr)
    vp8_loop_filter_horizontal_edge_c(u_ptr + 4 * uv_stride, uv_stride, lfi->blim, lfi->lim, lfi->hev_thr, 1);

  if (v_ptr)
    vp8_loop_filter_horizontal_edge_c(v_ptr + 4 * uv_stride, uv_stride, lfi->blim, lfi->lim, lfi->hev_thr, 1);
}

void vp8_loop_filter_bh8x8_c(unsigned char *y_ptr, unsigned char *u_ptr,
                             unsigned char *v_ptr, int y_stride, int uv_stride,
                             struct loop_filter_info *lfi) {
  vp8_mbloop_filter_horizontal_edge_c(
    y_ptr + 8 * y_stride, y_stride, lfi->blim, lfi->lim, lfi->hev_thr, 2);
}

void vp8_loop_filter_bhs_c(unsigned char *y_ptr, int y_stride,
                           const unsigned char *blimit) {
  vp8_loop_filter_simple_horizontal_edge_c(y_ptr + 4 * y_stride, y_stride, blimit);
  vp8_loop_filter_simple_horizontal_edge_c(y_ptr + 8 * y_stride, y_stride, blimit);
  vp8_loop_filter_simple_horizontal_edge_c(y_ptr + 12 * y_stride, y_stride, blimit);
}

void vp8_loop_filter_bv8x8_c(unsigned char *y_ptr, unsigned char *u_ptr,
                             unsigned char *v_ptr, int y_stride, int uv_stride,
                             struct loop_filter_info *lfi) {
  vp8_mbloop_filter_vertical_edge_c(
    y_ptr + 8, y_stride, lfi->blim, lfi->lim, lfi->hev_thr, 2);
}

void vp8_loop_filter_bvs_c(unsigned char *y_ptr, int y_stride,
                           const unsigned char *blimit) {
  vp8_loop_filter_simple_vertical_edge_c(y_ptr + 4, y_stride, blimit);
  vp8_loop_filter_simple_vertical_edge_c(y_ptr + 8, y_stride, blimit);
  vp8_loop_filter_simple_vertical_edge_c(y_ptr + 12, y_stride, blimit);
}