Coverage Report

Created: 2025-07-23 08:18

/src/aom/av1/common/cdef_block.c
Line
Count
Source (jump to first uncovered line)
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
 */
11
12
#include <math.h>
13
#include <stdlib.h>
14
15
#include "config/aom_dsp_rtcd.h"
16
#include "config/av1_rtcd.h"
17
18
#include "av1/common/cdef.h"
19
/*
20
This is Cdef_Directions (section 7.15.3) with 2 padding entries at the
21
beginning and end of the table. The cdef direction range is [0, 7] and the
22
first index is offset +/-2. This removes the need to constrain the first
23
index to the same range using e.g., & 7.
24
*/
25
DECLARE_ALIGNED(16, const int, cdef_directions_padded[12][2]) = {
26
  /* Padding: cdef_directions[6] */
27
  { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 0 },
28
  /* Padding: cdef_directions[7] */
29
  { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE - 1 },
30
31
  /* Begin cdef_directions */
32
  { -1 * CDEF_BSTRIDE + 1, -2 * CDEF_BSTRIDE + 2 },
33
  { 0 * CDEF_BSTRIDE + 1, -1 * CDEF_BSTRIDE + 2 },
34
  { 0 * CDEF_BSTRIDE + 1, 0 * CDEF_BSTRIDE + 2 },
35
  { 0 * CDEF_BSTRIDE + 1, 1 * CDEF_BSTRIDE + 2 },
36
  { 1 * CDEF_BSTRIDE + 1, 2 * CDEF_BSTRIDE + 2 },
37
  { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 1 },
38
  { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE + 0 },
39
  { 1 * CDEF_BSTRIDE + 0, 2 * CDEF_BSTRIDE - 1 },
40
  /* End cdef_directions */
41
42
  /* Padding: cdef_directions[0] */
43
  { -1 * CDEF_BSTRIDE + 1, -2 * CDEF_BSTRIDE + 2 },
44
  /* Padding: cdef_directions[1] */
45
  { 0 * CDEF_BSTRIDE + 1, -1 * CDEF_BSTRIDE + 2 },
46
};
47
48
const int (*const cdef_directions)[2] = cdef_directions_padded + 2;
49
50
/* Detect direction. 0 means 45-degree up-right, 2 is horizontal, and so on.
51
   The search minimizes the weighted variance along all the lines in a
52
   particular direction, i.e. the squared error between the input and a
53
   "predicted" block where each pixel is replaced by the average along a line
54
   in a particular direction. Since each direction have the same sum(x^2) term,
55
   that term is never computed. See Section 2, step 2, of:
56
   http://jmvalin.ca/notes/intra_paint.pdf */
57
int cdef_find_dir_c(const uint16_t *img, int stride, int32_t *var,
58
438k
                    int coeff_shift) {
59
438k
  int i;
60
438k
  int32_t cost[8] = { 0 };
61
438k
  int partial[8][15] = { { 0 } };
62
438k
  int32_t best_cost = 0;
63
438k
  int best_dir = 0;
64
  /* Instead of dividing by n between 2 and 8, we multiply by 3*5*7*8/n.
65
     The output is then 840 times larger, but we don't care for finding
66
     the max. */
67
438k
  static const int div_table[] = { 0, 840, 420, 280, 210, 168, 140, 120, 105 };
68
3.94M
  for (i = 0; i < 8; i++) {
69
3.50M
    int j;
70
31.5M
    for (j = 0; j < 8; j++) {
71
28.0M
      int x;
72
      /* We subtract 128 here to reduce the maximum range of the squared
73
         partial sums. */
74
28.0M
      x = (img[i * stride + j] >> coeff_shift) - 128;
75
28.0M
      partial[0][i + j] += x;
76
28.0M
      partial[1][i + j / 2] += x;
77
28.0M
      partial[2][i] += x;
78
28.0M
      partial[3][3 + i - j / 2] += x;
79
28.0M
      partial[4][7 + i - j] += x;
80
28.0M
      partial[5][3 - i / 2 + j] += x;
81
28.0M
      partial[6][j] += x;
82
28.0M
      partial[7][i / 2 + j] += x;
83
28.0M
    }
84
3.50M
  }
85
3.94M
  for (i = 0; i < 8; i++) {
86
3.50M
    cost[2] += partial[2][i] * partial[2][i];
87
3.50M
    cost[6] += partial[6][i] * partial[6][i];
88
3.50M
  }
89
438k
  cost[2] *= div_table[8];
90
438k
  cost[6] *= div_table[8];
91
3.50M
  for (i = 0; i < 7; i++) {
92
3.07M
    cost[0] += (partial[0][i] * partial[0][i] +
93
3.07M
                partial[0][14 - i] * partial[0][14 - i]) *
94
3.07M
               div_table[i + 1];
95
3.07M
    cost[4] += (partial[4][i] * partial[4][i] +
96
3.07M
                partial[4][14 - i] * partial[4][14 - i]) *
97
3.07M
               div_table[i + 1];
98
3.07M
  }
99
438k
  cost[0] += partial[0][7] * partial[0][7] * div_table[8];
100
438k
  cost[4] += partial[4][7] * partial[4][7] * div_table[8];
101
2.19M
  for (i = 1; i < 8; i += 2) {
102
1.75M
    int j;
103
10.5M
    for (j = 0; j < 4 + 1; j++) {
104
8.77M
      cost[i] += partial[i][3 + j] * partial[i][3 + j];
105
8.77M
    }
106
1.75M
    cost[i] *= div_table[8];
107
7.01M
    for (j = 0; j < 4 - 1; j++) {
108
5.26M
      cost[i] += (partial[i][j] * partial[i][j] +
109
5.26M
                  partial[i][10 - j] * partial[i][10 - j]) *
110
5.26M
                 div_table[2 * j + 2];
111
5.26M
    }
112
1.75M
  }
113
3.94M
  for (i = 0; i < 8; i++) {
114
3.50M
    if (cost[i] > best_cost) {
115
886k
      best_cost = cost[i];
116
886k
      best_dir = i;
117
886k
    }
118
3.50M
  }
119
  /* Difference between the optimal variance and the variance along the
120
     orthogonal direction. Again, the sum(x^2) terms cancel out. */
121
438k
  *var = best_cost - cost[(best_dir + 4) & 7];
122
  /* We'd normally divide by 840, but dividing by 1024 is close enough
123
     for what we're going to do with this. */
124
438k
  *var >>= 10;
125
438k
  return best_dir;
126
438k
}
127
128
const int cdef_pri_taps[2][2] = { { 4, 2 }, { 3, 3 } };
129
const int cdef_sec_taps[2] = { 2, 1 };
130
131
/* Smooth in the direction detected. */
132
static void cdef_filter_block_internal(uint8_t *dst8, uint16_t *dst16,
133
                                       int dstride, const uint16_t *in,
134
                                       int pri_strength, int sec_strength,
135
                                       int dir, int pri_damping,
136
                                       int sec_damping, int bsize,
137
1.31M
                                       int coeff_shift) {
138
1.31M
  int i, j, k;
139
1.31M
  const int s = CDEF_BSTRIDE;
140
1.31M
  const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
141
1.31M
  const int *sec_taps = cdef_sec_taps;
142
11.0M
  for (i = 0; i < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_4X8); i++) {
143
84.5M
    for (j = 0; j < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_8X4); j++) {
144
74.8M
      int16_t sum = 0;
145
74.8M
      int16_t y;
146
74.8M
      int16_t x = in[i * s + j];
147
74.8M
      int max = x;
148
74.8M
      int min = x;
149
224M
      for (k = 0; k < 2; k++) {
150
149M
        int16_t p0 = in[i * s + j + cdef_directions[dir][k]];
151
149M
        int16_t p1 = in[i * s + j - cdef_directions[dir][k]];
152
149M
        sum += pri_taps[k] * constrain(p0 - x, pri_strength, pri_damping);
153
149M
        sum += pri_taps[k] * constrain(p1 - x, pri_strength, pri_damping);
154
149M
        if (p0 != CDEF_VERY_LARGE) max = AOMMAX(p0, max);
155
149M
        if (p1 != CDEF_VERY_LARGE) max = AOMMAX(p1, max);
156
149M
        min = AOMMIN(p0, min);
157
149M
        min = AOMMIN(p1, min);
158
149M
        int16_t s0 = in[i * s + j + cdef_directions[dir + 2][k]];
159
149M
        int16_t s1 = in[i * s + j - cdef_directions[dir + 2][k]];
160
149M
        int16_t s2 = in[i * s + j + cdef_directions[dir - 2][k]];
161
149M
        int16_t s3 = in[i * s + j - cdef_directions[dir - 2][k]];
162
149M
        if (s0 != CDEF_VERY_LARGE) max = AOMMAX(s0, max);
163
149M
        if (s1 != CDEF_VERY_LARGE) max = AOMMAX(s1, max);
164
149M
        if (s2 != CDEF_VERY_LARGE) max = AOMMAX(s2, max);
165
149M
        if (s3 != CDEF_VERY_LARGE) max = AOMMAX(s3, max);
166
149M
        min = AOMMIN(s0, min);
167
149M
        min = AOMMIN(s1, min);
168
149M
        min = AOMMIN(s2, min);
169
149M
        min = AOMMIN(s3, min);
170
149M
        sum += sec_taps[k] * constrain(s0 - x, sec_strength, sec_damping);
171
149M
        sum += sec_taps[k] * constrain(s1 - x, sec_strength, sec_damping);
172
149M
        sum += sec_taps[k] * constrain(s2 - x, sec_strength, sec_damping);
173
149M
        sum += sec_taps[k] * constrain(s3 - x, sec_strength, sec_damping);
174
149M
      }
175
74.8M
      y = clamp((int16_t)x + ((8 + sum - (sum < 0)) >> 4), min, max);
176
74.8M
      if (dst8)
177
42.4M
        dst8[i * dstride + j] = (uint8_t)y;
178
32.3M
      else
179
32.3M
        dst16[i * dstride + j] = (uint16_t)y;
180
74.8M
    }
181
9.74M
  }
182
1.31M
}
183
184
void cdef_filter_block_c(void *dst8, int dstride, const uint16_t *in,
185
                         int pri_strength, int sec_strength, int dir,
186
                         int pri_damping, int sec_damping, int bsize,
187
689k
                         int coeff_shift) {
188
689k
  cdef_filter_block_internal((uint8_t *)dst8, NULL, dstride, in, pri_strength,
189
689k
                             sec_strength, dir, pri_damping, sec_damping, bsize,
190
689k
                             coeff_shift);
191
689k
}
192
193
void cdef_filter_block_highbd_c(void *dst16, int dstride, const uint16_t *in,
194
                                int pri_strength, int sec_strength, int dir,
195
                                int pri_damping, int sec_damping, int bsize,
196
622k
                                int coeff_shift) {
197
622k
  cdef_filter_block_internal(NULL, (uint16_t *)dst16, dstride, in, pri_strength,
198
622k
                             sec_strength, dir, pri_damping, sec_damping, bsize,
199
622k
                             coeff_shift);
200
622k
}
201
202
/* Compute the primary filter strength for an 8x8 block based on the
203
   directional variance difference. A high variance difference means
204
   that we have a highly directional pattern (e.g. a high contrast
205
   edge), so we can apply more deringing. A low variance means that we
206
   either have a low contrast edge, or a non-directional texture, so
207
   we want to be careful not to blur. */
208
438k
static INLINE int adjust_strength(int strength, int32_t var) {
209
438k
  const int i = var >> 6 ? AOMMIN(get_msb(var >> 6), 12) : 0;
210
  /* We use the variance of 8x8 blocks to adjust the strength. */
211
438k
  return var ? (strength * (4 + i) + 8) >> 4 : 0;
212
438k
}
213
214
void av1_cdef_filter_fb(uint8_t *dst8, uint16_t *dst16, int dstride,
215
                        uint16_t *in, int xdec, int ydec,
216
                        int dir[CDEF_NBLOCKS][CDEF_NBLOCKS], int *dirinit,
217
                        int var[CDEF_NBLOCKS][CDEF_NBLOCKS], int pli,
218
                        cdef_list *dlist, int cdef_count, int level,
219
28.1k
                        int sec_strength, int damping, int coeff_shift) {
220
28.1k
  int bi;
221
28.1k
  int bx;
222
28.1k
  int by;
223
28.1k
  const int pri_strength = level << coeff_shift;
224
28.1k
  sec_strength <<= coeff_shift;
225
28.1k
  damping += coeff_shift - (pli != AOM_PLANE_Y);
226
28.1k
  const int bw_log2 = 3 - xdec;
227
28.1k
  const int bh_log2 = 3 - ydec;
228
28.1k
  if (dirinit && pri_strength == 0 && sec_strength == 0) {
229
    // If we're here, both primary and secondary strengths are 0, and
230
    // we still haven't written anything to y[] yet, so we just copy
231
    // the input to y[]. This is necessary only for av1_cdef_search()
232
    // and only av1_cdef_search() sets dirinit.
233
0
    for (bi = 0; bi < cdef_count; bi++) {
234
0
      by = dlist[bi].by;
235
0
      bx = dlist[bi].bx;
236
      // TODO(stemidts/jmvalin): SIMD optimisations
237
0
      for (int iy = 0; iy < 1 << bh_log2; iy++) {
238
0
        memcpy(&dst16[(bi << (bw_log2 + bh_log2)) + (iy << bw_log2)],
239
0
               &in[((by << bh_log2) + iy) * CDEF_BSTRIDE + (bx << bw_log2)],
240
0
               ((size_t)1 << bw_log2) * sizeof(*dst16));
241
0
      }
242
0
    }
243
0
    return;
244
0
  }
245
246
28.1k
  if (pli == 0) {
247
9.49k
    if (!dirinit || !*dirinit) {
248
448k
      for (bi = 0; bi < cdef_count; bi++) {
249
438k
        by = dlist[bi].by;
250
438k
        bx = dlist[bi].bx;
251
438k
        dir[by][bx] = cdef_find_dir(&in[8 * by * CDEF_BSTRIDE + 8 * bx],
252
438k
                                    CDEF_BSTRIDE, &var[by][bx], coeff_shift);
253
438k
      }
254
9.49k
      if (dirinit) *dirinit = 1;
255
9.49k
    }
256
9.49k
  }
257
28.1k
  if (pli == 1 && xdec != ydec) {
258
2.28k
    for (bi = 0; bi < cdef_count; bi++) {
259
2.21k
      static const int conv422[8] = { 7, 0, 2, 4, 5, 6, 6, 6 };
260
2.21k
      static const int conv440[8] = { 1, 2, 2, 2, 3, 4, 6, 0 };
261
2.21k
      by = dlist[bi].by;
262
2.21k
      bx = dlist[bi].bx;
263
2.21k
      dir[by][bx] = (xdec ? conv422 : conv440)[dir[by][bx]];
264
2.21k
    }
265
70
  }
266
267
28.1k
  const int bsize =
268
28.1k
      ydec ? (xdec ? BLOCK_4X4 : BLOCK_8X4) : (xdec ? BLOCK_4X8 : BLOCK_8X8);
269
28.1k
  const int t = pri_strength;
270
28.1k
  const int s = sec_strength;
271
1.34M
  for (bi = 0; bi < cdef_count; bi++) {
272
1.31M
    by = dlist[bi].by;
273
1.31M
    bx = dlist[bi].bx;
274
1.31M
    if (dst16) {
275
622k
      cdef_filter_block_highbd(
276
622k
          &dst16[dirinit ? bi << (bw_log2 + bh_log2)
277
622k
                         : (by << bh_log2) * dstride + (bx << bw_log2)],
278
622k
          dirinit ? 1 << bw_log2 : dstride,
279
622k
          &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)],
280
622k
          (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
281
622k
          damping, damping, bsize, coeff_shift);
282
689k
    } else {
283
689k
      cdef_filter_block(
284
689k
          &dst8[(by << bh_log2) * dstride + (bx << bw_log2)], dstride,
285
689k
          &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)],
286
689k
          (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
287
689k
          damping, damping, bsize, coeff_shift);
288
689k
    }
289
1.31M
  }
290
28.1k
}