clpf.c 4.64 KB
Newer Older
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
3
4
5
6
7
8
9
10
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */
Steinar Midtskogen's avatar
Steinar Midtskogen committed
11

12
#include "av1/common/clpf.h"
13
#include "./aom_dsp_rtcd.h"
Steinar Midtskogen's avatar
Steinar Midtskogen committed
14
#include "aom/aom_image.h"
15
#include "aom_dsp/aom_dsp_common.h"
16

17
static int sign(int i) { return i < 0 ? -1 : 1; }
Steinar Midtskogen's avatar
Steinar Midtskogen committed
18

19
static int constrain(int x, int s, unsigned int damping) {
Steinar Midtskogen's avatar
Steinar Midtskogen committed
20
  return sign(x) *
21
22
         AOMMAX(0, abs(x) - AOMMAX(0, abs(x) - s +
                                          (abs(x) >> (damping - get_msb(s)))));
Steinar Midtskogen's avatar
Steinar Midtskogen committed
23
24
25
}

int av1_clpf_sample(int X, int A, int B, int C, int D, int E, int F, int G,
26
27
28
29
30
                    int H, int s, unsigned int dmp) {
  int delta = 1 * constrain(A - X, s, dmp) + 3 * constrain(B - X, s, dmp) +
              1 * constrain(C - X, s, dmp) + 3 * constrain(D - X, s, dmp) +
              3 * constrain(E - X, s, dmp) + 1 * constrain(F - X, s, dmp) +
              3 * constrain(G - X, s, dmp) + 1 * constrain(H - X, s, dmp);
31
32
  return (8 + delta - (delta < 0)) >> 4;
}
33

34
35
36
37
38
39
40
int av1_clpf_hsample(int X, int A, int B, int C, int D, int s,
                     unsigned int dmp) {
  int delta = 1 * constrain(A - X, s, dmp) + 3 * constrain(B - X, s, dmp) +
              3 * constrain(C - X, s, dmp) + 1 * constrain(D - X, s, dmp);
  return (4 + delta - (delta < 0)) >> 3;
}

41
42
43
void aom_clpf_block_c(uint8_t *dst, const uint16_t *src, int dstride,
                      int sstride, int sizex, int sizey, unsigned int strength,
                      unsigned int damping) {
44
  int x, y;
Steinar Midtskogen's avatar
Steinar Midtskogen committed
45

46
47
  for (y = 0; y < sizey; y++) {
    for (x = 0; x < sizex; x++) {
Steinar Midtskogen's avatar
Steinar Midtskogen committed
48
      const int X = src[y * sstride + x];
49
50
51
52
53
54
55
56
      const int A = src[(y - 2) * sstride + x];
      const int B = src[(y - 1) * sstride + x];
      const int C = src[y * sstride + x - 2];
      const int D = src[y * sstride + x - 1];
      const int E = src[y * sstride + x + 1];
      const int F = src[y * sstride + x + 2];
      const int G = src[(y + 1) * sstride + x];
      const int H = src[(y + 2) * sstride + x];
Steinar Midtskogen's avatar
Steinar Midtskogen committed
57
      const int delta =
58
          av1_clpf_sample(X, A, B, C, D, E, F, G, H, strength, damping);
59
      dst[y * dstride + x] = X + delta;
60
61
62
63
    }
  }
}

64
65
66
// Identical to aom_clpf_block_c() apart from "dst".
void aom_clpf_block_hbd_c(uint16_t *dst, const uint16_t *src, int dstride,
                          int sstride, int sizex, int sizey,
67
                          unsigned int strength, unsigned int damping) {
68
  int x, y;
69

70
71
  for (y = 0; y < sizey; y++) {
    for (x = 0; x < sizex; x++) {
Steinar Midtskogen's avatar
Steinar Midtskogen committed
72
      const int X = src[y * sstride + x];
73
74
75
76
77
78
79
80
      const int A = src[(y - 2) * sstride + x];
      const int B = src[(y - 1) * sstride + x];
      const int C = src[y * sstride + x - 2];
      const int D = src[y * sstride + x - 1];
      const int E = src[y * sstride + x + 1];
      const int F = src[y * sstride + x + 2];
      const int G = src[(y + 1) * sstride + x];
      const int H = src[(y + 2) * sstride + x];
Steinar Midtskogen's avatar
Steinar Midtskogen committed
81
      const int delta =
82
          av1_clpf_sample(X, A, B, C, D, E, F, G, H, strength, damping);
83
84
85
86
      dst[y * dstride + x] = X + delta;
    }
  }
}
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Vertically restricted filter
void aom_clpf_hblock_c(uint8_t *dst, const uint16_t *src, int dstride,
                       int sstride, int sizex, int sizey, unsigned int strength,
                       unsigned int damping) {
  int x, y;

  for (y = 0; y < sizey; y++) {
    for (x = 0; x < sizex; x++) {
      const int X = src[y * sstride + x];
      const int A = src[y * sstride + x - 2];
      const int B = src[y * sstride + x - 1];
      const int C = src[y * sstride + x + 1];
      const int D = src[y * sstride + x + 2];
      const int delta = av1_clpf_hsample(X, A, B, C, D, strength, damping);
      dst[y * dstride + x] = X + delta;
    }
  }
}

void aom_clpf_hblock_hbd_c(uint16_t *dst, const uint16_t *src, int dstride,
                           int sstride, int sizex, int sizey,
109
110
111
                           unsigned int strength, unsigned int damping) {
  int x, y;

112
113
  for (y = 0; y < sizey; y++) {
    for (x = 0; x < sizex; x++) {
114
115
116
117
118
119
120
121
122
123
      const int X = src[y * sstride + x];
      const int A = src[y * sstride + x - 2];
      const int B = src[y * sstride + x - 1];
      const int C = src[y * sstride + x + 1];
      const int D = src[y * sstride + x + 2];
      const int delta = av1_clpf_hsample(X, A, B, C, D, strength, damping);
      dst[y * dstride + x] = X + delta;
    }
  }
}