Coverage Report

Created: 2025-11-16 07:22

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