deblock.c 6.38 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
Johann's avatar
Johann committed
10
#include <assert.h>
11
#include <stdlib.h>
12
#include "./vpx_dsp_rtcd.h"
13
#include "vpx/vpx_integer.h"
14

15
const int16_t vpx_rv[] = {
clang-format's avatar
clang-format committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  8,  5,  2,  2,  8,  12, 4,  9,  8,  3,  0,  3,  9,  0,  0,  0,  8,  3,  14,
  4,  10, 1,  11, 14, 1,  14, 9,  6,  12, 11, 8,  6,  10, 0,  0,  8,  9,  0,
  3,  14, 8,  11, 13, 4,  2,  9,  0,  3,  9,  6,  1,  2,  3,  14, 13, 1,  8,
  2,  9,  7,  3,  3,  1,  13, 13, 6,  6,  5,  2,  7,  11, 9,  11, 8,  7,  3,
  2,  0,  13, 13, 14, 4,  12, 5,  12, 10, 8,  10, 13, 10, 4,  14, 4,  10, 0,
  8,  11, 1,  13, 7,  7,  14, 6,  14, 13, 2,  13, 5,  4,  4,  0,  10, 0,  5,
  13, 2,  12, 7,  11, 13, 8,  0,  4,  10, 7,  2,  7,  2,  2,  5,  3,  4,  7,
  3,  3,  14, 14, 5,  9,  13, 3,  14, 3,  6,  3,  0,  11, 8,  13, 1,  13, 1,
  12, 0,  10, 9,  7,  6,  2,  8,  5,  2,  13, 7,  1,  13, 14, 7,  6,  7,  9,
  6,  10, 11, 7,  8,  7,  5,  14, 8,  4,  4,  0,  8,  7,  10, 0,  8,  14, 11,
  3,  12, 5,  7,  14, 3,  14, 5,  2,  6,  11, 12, 12, 8,  0,  11, 13, 1,  2,
  0,  5,  10, 14, 7,  8,  0,  4,  11, 0,  8,  0,  3,  10, 5,  8,  0,  11, 6,
  7,  8,  10, 7,  13, 9,  2,  5,  1,  5,  10, 2,  4,  3,  5,  6,  10, 8,  9,
  4,  11, 14, 0,  10, 0,  5,  13, 2,  12, 7,  11, 13, 8,  0,  4,  10, 7,  2,
  7,  2,  2,  5,  3,  4,  7,  3,  3,  14, 14, 5,  9,  13, 3,  14, 3,  6,  3,
  0,  11, 8,  13, 1,  13, 1,  12, 0,  10, 9,  7,  6,  2,  8,  5,  2,  13, 7,
  1,  13, 14, 7,  6,  7,  9,  6,  10, 11, 7,  8,  7,  5,  14, 8,  4,  4,  0,
  8,  7,  10, 0,  8,  14, 11, 3,  12, 5,  7,  14, 3,  14, 5,  2,  6,  11, 12,
  12, 8,  0,  11, 13, 1,  2,  0,  5,  10, 14, 7,  8,  0,  4,  11, 0,  8,  0,
  3,  10, 5,  8,  0,  11, 6,  7,  8,  10, 7,  13, 9,  2,  5,  1,  5,  10, 2,
  4,  3,  5,  6,  10, 8,  9,  4,  11, 14, 3,  8,  3,  7,  8,  5,  11, 4,  12,
  3,  11, 9,  14, 8,  14, 13, 4,  3,  1,  2,  14, 6,  5,  4,  4,  11, 4,  6,
  2,  1,  5,  8,  8,  12, 13, 5,  14, 10, 12, 13, 0,  9,  5,  5,  11, 10, 13,
  9,  10, 13,
};
41 42 43 44 45 46 47 48 49 50 51 52

void vpx_post_proc_down_and_across_mb_row_c(unsigned char *src_ptr,
                                            unsigned char *dst_ptr,
                                            int src_pixels_per_line,
                                            int dst_pixels_per_line, int cols,
                                            unsigned char *f, int size) {
  unsigned char *p_src, *p_dst;
  int row;
  int col;
  unsigned char v;
  unsigned char d[4];

Johann's avatar
Johann committed
53 54 55
  assert(size >= 8);
  assert(cols >= 8);

56 57 58 59 60 61 62 63 64 65 66 67 68
  for (row = 0; row < size; row++) {
    /* post_proc_down for one row */
    p_src = src_ptr;
    p_dst = dst_ptr;

    for (col = 0; col < cols; col++) {
      unsigned char p_above2 = p_src[col - 2 * src_pixels_per_line];
      unsigned char p_above1 = p_src[col - src_pixels_per_line];
      unsigned char p_below1 = p_src[col + src_pixels_per_line];
      unsigned char p_below2 = p_src[col + 2 * src_pixels_per_line];

      v = p_src[col];

clang-format's avatar
clang-format committed
69 70
      if ((abs(v - p_above2) < f[col]) && (abs(v - p_above1) < f[col]) &&
          (abs(v - p_below1) < f[col]) && (abs(v - p_below2) < f[col])) {
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        unsigned char k1, k2, k3;
        k1 = (p_above2 + p_above1 + 1) >> 1;
        k2 = (p_below2 + p_below1 + 1) >> 1;
        k3 = (k1 + k2 + 1) >> 1;
        v = (k3 + v + 1) >> 1;
      }

      p_dst[col] = v;
    }

    /* now post_proc_across */
    p_src = dst_ptr;
    p_dst = dst_ptr;

    p_src[-2] = p_src[-1] = p_src[0];
    p_src[cols] = p_src[cols + 1] = p_src[cols - 1];

    for (col = 0; col < cols; col++) {
      v = p_src[col];

clang-format's avatar
clang-format committed
91 92 93 94
      if ((abs(v - p_src[col - 2]) < f[col]) &&
          (abs(v - p_src[col - 1]) < f[col]) &&
          (abs(v - p_src[col + 1]) < f[col]) &&
          (abs(v - p_src[col + 2]) < f[col])) {
95 96 97 98 99 100 101 102 103
        unsigned char k1, k2, k3;
        k1 = (p_src[col - 2] + p_src[col - 1] + 1) >> 1;
        k2 = (p_src[col + 2] + p_src[col + 1] + 1) >> 1;
        k3 = (k1 + k2 + 1) >> 1;
        v = (k3 + v + 1) >> 1;
      }

      d[col & 3] = v;

clang-format's avatar
clang-format committed
104
      if (col >= 2) p_dst[col - 2] = d[(col - 2) & 3];
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    }

    /* handle the last two pixels */
    p_dst[col - 2] = d[(col - 2) & 3];
    p_dst[col - 1] = d[(col - 1) & 3];

    /* next row */
    src_ptr += src_pixels_per_line;
    dst_ptr += dst_pixels_per_line;
  }
}

void vpx_mbpost_proc_across_ip_c(unsigned char *src, int pitch, int rows,
                                 int cols, int flimit) {
  int r, c, i;

  unsigned char *s = src;
  unsigned char d[16];

  for (r = 0; r < rows; r++) {
125
    int sumsq = 16;
126 127
    int sum = 0;

clang-format's avatar
clang-format committed
128
    for (i = -8; i < 0; i++) s[i] = s[0];
129 130 131 132

    /* 17 avoids valgrind warning - we buffer values in c in d
     * and only write them when we've read 8 ahead...
     */
clang-format's avatar
clang-format committed
133
    for (i = 0; i < 17; i++) s[i + cols] = s[cols - 1];
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

    for (i = -8; i <= 6; i++) {
      sumsq += s[i] * s[i];
      sum += s[i];
      d[i + 8] = 0;
    }

    for (c = 0; c < cols + 8; c++) {
      int x = s[c + 7] - s[c - 8];
      int y = s[c + 7] + s[c - 8];

      sum += x;
      sumsq += x * y;

      d[c & 15] = s[c];

      if (sumsq * 15 - sum * sum < flimit) {
        d[c & 15] = (8 + sum + s[c]) >> 4;
      }

      s[c - 8] = d[(c - 8) & 15];
    }

    s += pitch;
  }
}

void vpx_mbpost_proc_down_c(unsigned char *dst, int pitch, int rows, int cols,
                            int flimit) {
  int r, c, i;

  for (c = 0; c < cols; c++) {
    unsigned char *s = &dst[c];
    int sumsq = 0;
    int sum = 0;
    unsigned char d[16];

clang-format's avatar
clang-format committed
171
    for (i = -8; i < 0; i++) s[i * pitch] = s[0];
172 173 174 175

    /* 17 avoids valgrind warning - we buffer values in c in d
     * and only write them when we've read 8 ahead...
     */
clang-format's avatar
clang-format committed
176
    for (i = 0; i < 17; i++) s[(i + rows) * pitch] = s[(rows - 1) * pitch];
177 178 179 180 181 182 183 184 185 186 187 188

    for (i = -8; i <= 6; i++) {
      sumsq += s[i * pitch] * s[i * pitch];
      sum += s[i * pitch];
    }

    for (r = 0; r < rows + 8; r++) {
      sumsq += s[7 * pitch] * s[7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
      sum += s[7 * pitch] - s[-8 * pitch];
      d[r & 15] = s[0];

      if (sumsq * 15 - sum * sum < flimit) {
189
        d[r & 15] = (vpx_rv[(r & 127) + (c & 7)] + sum + s[0]) >> 4;
190
      }
clang-format's avatar
clang-format committed
191
      if (r >= 8) s[-8 * pitch] = d[(r - 8) & 15];
192 193 194 195
      s += pitch;
    }
  }
}