Coverage Report

Created: 2026-03-08 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/aom/av1/common/cdef_block.c
Line
Count
Source
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
72.5k
                    int coeff_shift) {
59
72.5k
  int i;
60
72.5k
  int32_t cost[8] = { 0 };
61
72.5k
  int partial[8][15] = { { 0 } };
62
72.5k
  int32_t best_cost = 0;
63
72.5k
  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
72.5k
  static const int div_table[] = { 0, 840, 420, 280, 210, 168, 140, 120, 105 };
68
652k
  for (i = 0; i < 8; i++) {
69
580k
    int j;
70
5.22M
    for (j = 0; j < 8; j++) {
71
4.64M
      int x;
72
      /* We subtract 128 here to reduce the maximum range of the squared
73
         partial sums. */
74
4.64M
      x = (img[i * stride + j] >> coeff_shift) - 128;
75
4.64M
      partial[0][i + j] += x;
76
4.64M
      partial[1][i + j / 2] += x;
77
4.64M
      partial[2][i] += x;
78
4.64M
      partial[3][3 + i - j / 2] += x;
79
4.64M
      partial[4][7 + i - j] += x;
80
4.64M
      partial[5][3 - i / 2 + j] += x;
81
4.64M
      partial[6][j] += x;
82
4.64M
      partial[7][i / 2 + j] += x;
83
4.64M
    }
84
580k
  }
85
652k
  for (i = 0; i < 8; i++) {
86
580k
    cost[2] += partial[2][i] * partial[2][i];
87
580k
    cost[6] += partial[6][i] * partial[6][i];
88
580k
  }
89
72.5k
  cost[2] *= div_table[8];
90
72.5k
  cost[6] *= div_table[8];
91
580k
  for (i = 0; i < 7; i++) {
92
507k
    cost[0] += (partial[0][i] * partial[0][i] +
93
507k
                partial[0][14 - i] * partial[0][14 - i]) *
94
507k
               div_table[i + 1];
95
507k
    cost[4] += (partial[4][i] * partial[4][i] +
96
507k
                partial[4][14 - i] * partial[4][14 - i]) *
97
507k
               div_table[i + 1];
98
507k
  }
99
72.5k
  cost[0] += partial[0][7] * partial[0][7] * div_table[8];
100
72.5k
  cost[4] += partial[4][7] * partial[4][7] * div_table[8];
101
362k
  for (i = 1; i < 8; i += 2) {
102
290k
    int j;
103
1.74M
    for (j = 0; j < 4 + 1; j++) {
104
1.45M
      cost[i] += partial[i][3 + j] * partial[i][3 + j];
105
1.45M
    }
106
290k
    cost[i] *= div_table[8];
107
1.16M
    for (j = 0; j < 4 - 1; j++) {
108
870k
      cost[i] += (partial[i][j] * partial[i][j] +
109
870k
                  partial[i][10 - j] * partial[i][10 - j]) *
110
870k
                 div_table[2 * j + 2];
111
870k
    }
112
290k
  }
113
652k
  for (i = 0; i < 8; i++) {
114
580k
    if (cost[i] > best_cost) {
115
153k
      best_cost = cost[i];
116
153k
      best_dir = i;
117
153k
    }
118
580k
  }
119
  /* Difference between the optimal variance and the variance along the
120
     orthogonal direction. Again, the sum(x^2) terms cancel out. */
121
72.5k
  *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
72.5k
  *var >>= 10;
125
72.5k
  return best_dir;
126
72.5k
}
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
217k
                                       int coeff_shift) {
138
217k
  int i, j, k;
139
217k
  const int s = CDEF_BSTRIDE;
140
217k
  const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
141
217k
  const int *sec_taps = cdef_sec_taps;
142
1.37M
  for (i = 0; i < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_4X8); i++) {
143
8.12M
    for (j = 0; j < 4 << (bsize == BLOCK_8X8 || bsize == BLOCK_8X4); j++) {
144
6.96M
      int16_t sum = 0;
145
6.96M
      int16_t y;
146
6.96M
      int16_t x = in[i * s + j];
147
6.96M
      int max = x;
148
6.96M
      int min = x;
149
20.8M
      for (k = 0; k < 2; k++) {
150
13.9M
        int16_t p0 = in[i * s + j + cdef_directions[dir][k]];
151
13.9M
        int16_t p1 = in[i * s + j - cdef_directions[dir][k]];
152
13.9M
        sum += pri_taps[k] * constrain(p0 - x, pri_strength, pri_damping);
153
13.9M
        sum += pri_taps[k] * constrain(p1 - x, pri_strength, pri_damping);
154
13.9M
        if (p0 != CDEF_VERY_LARGE) max = AOMMAX(p0, max);
155
13.9M
        if (p1 != CDEF_VERY_LARGE) max = AOMMAX(p1, max);
156
13.9M
        min = AOMMIN(p0, min);
157
13.9M
        min = AOMMIN(p1, min);
158
13.9M
        int16_t s0 = in[i * s + j + cdef_directions[dir + 2][k]];
159
13.9M
        int16_t s1 = in[i * s + j - cdef_directions[dir + 2][k]];
160
13.9M
        int16_t s2 = in[i * s + j + cdef_directions[dir - 2][k]];
161
13.9M
        int16_t s3 = in[i * s + j - cdef_directions[dir - 2][k]];
162
13.9M
        if (s0 != CDEF_VERY_LARGE) max = AOMMAX(s0, max);
163
13.9M
        if (s1 != CDEF_VERY_LARGE) max = AOMMAX(s1, max);
164
13.9M
        if (s2 != CDEF_VERY_LARGE) max = AOMMAX(s2, max);
165
13.9M
        if (s3 != CDEF_VERY_LARGE) max = AOMMAX(s3, max);
166
13.9M
        min = AOMMIN(s0, min);
167
13.9M
        min = AOMMIN(s1, min);
168
13.9M
        min = AOMMIN(s2, min);
169
13.9M
        min = AOMMIN(s3, min);
170
13.9M
        sum += sec_taps[k] * constrain(s0 - x, sec_strength, sec_damping);
171
13.9M
        sum += sec_taps[k] * constrain(s1 - x, sec_strength, sec_damping);
172
13.9M
        sum += sec_taps[k] * constrain(s2 - x, sec_strength, sec_damping);
173
13.9M
        sum += sec_taps[k] * constrain(s3 - x, sec_strength, sec_damping);
174
13.9M
      }
175
6.96M
      y = clamp((int16_t)x + ((8 + sum - (sum < 0)) >> 4), min, max);
176
6.96M
      if (dst8)
177
4.77M
        dst8[i * dstride + j] = (uint8_t)y;
178
2.18M
      else
179
2.18M
        dst16[i * dstride + j] = (uint16_t)y;
180
6.96M
    }
181
1.16M
  }
182
217k
}
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
149k
                         int coeff_shift) {
188
149k
  cdef_filter_block_internal((uint8_t *)dst8, NULL, dstride, in, pri_strength,
189
149k
                             sec_strength, dir, pri_damping, sec_damping, bsize,
190
149k
                             coeff_shift);
191
149k
}
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
68.4k
                                int coeff_shift) {
197
68.4k
  cdef_filter_block_internal(NULL, (uint16_t *)dst16, dstride, in, pri_strength,
198
68.4k
                             sec_strength, dir, pri_damping, sec_damping, bsize,
199
68.4k
                             coeff_shift);
200
68.4k
}
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
72.5k
static INLINE int adjust_strength(int strength, int32_t var) {
209
72.5k
  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
72.5k
  return var ? (strength * (4 + i) + 8) >> 4 : 0;
212
72.5k
}
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
12.2k
                        int sec_strength, int damping, int coeff_shift) {
220
12.2k
  int bi;
221
12.2k
  int bx;
222
12.2k
  int by;
223
12.2k
  const int pri_strength = level << coeff_shift;
224
12.2k
  sec_strength <<= coeff_shift;
225
12.2k
  damping += coeff_shift - (pli != AOM_PLANE_Y);
226
12.2k
  const int bw_log2 = 3 - xdec;
227
12.2k
  const int bh_log2 = 3 - ydec;
228
12.2k
  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
12.2k
  if (pli == 0) {
247
4.06k
    if (!dirinit || !*dirinit) {
248
76.5k
      for (bi = 0; bi < cdef_count; bi++) {
249
72.5k
        by = dlist[bi].by;
250
72.5k
        bx = dlist[bi].bx;
251
72.5k
        dir[by][bx] = cdef_find_dir(&in[8 * by * CDEF_BSTRIDE + 8 * bx],
252
72.5k
                                    CDEF_BSTRIDE, &var[by][bx], coeff_shift);
253
72.5k
      }
254
4.06k
      if (dirinit) *dirinit = 1;
255
4.06k
    }
256
4.06k
  }
257
12.2k
  if (pli == 1 && xdec != ydec) {
258
0
    for (bi = 0; bi < cdef_count; bi++) {
259
0
      static const int conv422[8] = { 7, 0, 2, 4, 5, 6, 6, 6 };
260
0
      static const int conv440[8] = { 1, 2, 2, 2, 3, 4, 6, 0 };
261
0
      by = dlist[bi].by;
262
0
      bx = dlist[bi].bx;
263
0
      dir[by][bx] = (xdec ? conv422 : conv440)[dir[by][bx]];
264
0
    }
265
0
  }
266
267
12.2k
  const int bsize =
268
12.2k
      ydec ? (xdec ? BLOCK_4X4 : BLOCK_8X4) : (xdec ? BLOCK_4X8 : BLOCK_8X8);
269
12.2k
  const int t = pri_strength;
270
12.2k
  const int s = sec_strength;
271
229k
  for (bi = 0; bi < cdef_count; bi++) {
272
217k
    by = dlist[bi].by;
273
217k
    bx = dlist[bi].bx;
274
217k
    if (dst16) {
275
68.4k
      cdef_filter_block_highbd(
276
68.4k
          &dst16[dirinit ? bi << (bw_log2 + bh_log2)
277
68.4k
                         : (by << bh_log2) * dstride + (bx << bw_log2)],
278
68.4k
          dirinit ? 1 << bw_log2 : dstride,
279
68.4k
          &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)],
280
68.4k
          (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
281
68.4k
          damping, damping, bsize, coeff_shift);
282
149k
    } else {
283
149k
      cdef_filter_block(
284
149k
          &dst8[(by << bh_log2) * dstride + (bx << bw_log2)], dstride,
285
149k
          &in[(by * CDEF_BSTRIDE << bh_log2) + (bx << bw_log2)],
286
149k
          (pli ? t : adjust_strength(t, var[by][bx])), s, t ? dir[by][bx] : 0,
287
149k
          damping, damping, bsize, coeff_shift);
288
149k
    }
289
217k
  }
290
12.2k
}