Coverage Report

Created: 2026-06-14 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/common/convolve.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 <assert.h>
13
#include <string.h>
14
15
#include "config/aom_dsp_rtcd.h"
16
#include "config/av1_rtcd.h"
17
18
#include "av1/common/av1_common_int.h"
19
#include "av1/common/blockd.h"
20
#include "av1/common/convolve.h"
21
#include "av1/common/filter.h"
22
#include "av1/common/resize.h"
23
#include "aom_dsp/aom_dsp_common.h"
24
#include "aom_ports/mem.h"
25
26
void av1_convolve_horiz_rs_c(const uint8_t *src, int src_stride, uint8_t *dst,
27
                             int dst_stride, int w, int h,
28
                             const int16_t *x_filters, int x0_qn,
29
23.2k
                             int x_step_qn) {
30
23.2k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
31
770k
  for (int y = 0; y < h; ++y) {
32
747k
    int x_qn = x0_qn;
33
191M
    for (int x = 0; x < w; ++x) {
34
190M
      const uint8_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
35
190M
      const int x_filter_idx =
36
190M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
37
190M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
38
190M
      const int16_t *const x_filter =
39
190M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
40
190M
      int sum = 0;
41
1.71G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
42
1.52G
        sum += src_x[k] * x_filter[k];
43
190M
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
44
190M
      x_qn += x_step_qn;
45
190M
    }
46
747k
    src += src_stride;
47
747k
    dst += dst_stride;
48
747k
  }
49
23.2k
}
50
51
#if CONFIG_AV1_HIGHBITDEPTH
52
void av1_highbd_convolve_horiz_rs_c(const uint16_t *src, int src_stride,
53
                                    uint16_t *dst, int dst_stride, int w, int h,
54
                                    const int16_t *x_filters, int x0_qn,
55
61.8k
                                    int x_step_qn, int bd) {
56
61.8k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
57
2.24M
  for (int y = 0; y < h; ++y) {
58
2.18M
    int x_qn = x0_qn;
59
129M
    for (int x = 0; x < w; ++x) {
60
126M
      const uint16_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
61
126M
      const int x_filter_idx =
62
126M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
63
126M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
64
126M
      const int16_t *const x_filter =
65
126M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
66
126M
      int sum = 0;
67
1.14G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
68
1.01G
        sum += src_x[k] * x_filter[k];
69
126M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
70
126M
      x_qn += x_step_qn;
71
126M
    }
72
2.18M
    src += src_stride;
73
2.18M
    dst += dst_stride;
74
2.18M
  }
75
61.8k
}
76
#endif  // CONFIG_AV1_HIGHBITDEPTH
77
78
void av1_convolve_2d_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
79
                          int dst_stride, int w, int h,
80
                          const InterpFilterParams *filter_params_x,
81
                          const InterpFilterParams *filter_params_y,
82
                          const int subpel_x_qn, const int subpel_y_qn,
83
7.24k
                          ConvolveParams *conv_params) {
84
7.24k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
85
7.24k
  int im_h = h + filter_params_y->taps - 1;
86
7.24k
  int im_stride = w;
87
7.24k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
88
7.24k
  const int fo_vert = filter_params_y->taps / 2 - 1;
89
7.24k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
90
7.24k
  const int bd = 8;
91
7.24k
  const int bits =
92
7.24k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
93
94
  // horizontal filter
95
7.24k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
96
7.24k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
97
7.24k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
98
100k
  for (int y = 0; y < im_h; ++y) {
99
596k
    for (int x = 0; x < w; ++x) {
100
503k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
101
4.52M
      for (int k = 0; k < filter_params_x->taps; ++k) {
102
4.02M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
103
4.02M
      }
104
105
      // TODO(aomedia:3393): for 12-tap filter, in extreme cases, the result can
106
      // be beyond the following range. For better prediction, a clamping can be
107
      // added for 12 tap filter to ensure the horizontal filtering result is
108
      // within 16 bit. The same applies to the vertical filtering.
109
503k
      assert(filter_params_x->taps > 8 ||
110
503k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
111
503k
      im_block[y * im_stride + x] =
112
503k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
113
503k
    }
114
92.8k
  }
115
116
  // vertical filter
117
7.24k
  int16_t *src_vert = im_block + fo_vert * im_stride;
118
7.24k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
119
7.24k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
120
7.24k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
121
49.3k
  for (int y = 0; y < h; ++y) {
122
280k
    for (int x = 0; x < w; ++x) {
123
238k
      int32_t sum = 1 << offset_bits;
124
2.14M
      for (int k = 0; k < filter_params_y->taps; ++k) {
125
1.90M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
126
1.90M
      }
127
238k
      assert(filter_params_y->taps > 8 ||
128
238k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
129
238k
      int16_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
130
238k
                    ((1 << (offset_bits - conv_params->round_1)) +
131
238k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
132
238k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
133
238k
    }
134
42.1k
  }
135
7.24k
}
136
137
void av1_convolve_y_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
138
                         int dst_stride, int w, int h,
139
                         const InterpFilterParams *filter_params_y,
140
4.32k
                         const int subpel_y_qn) {
141
4.32k
  const int fo_vert = filter_params_y->taps / 2 - 1;
142
143
  // vertical filter
144
4.32k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
145
4.32k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
146
30.2k
  for (int y = 0; y < h; ++y) {
147
191k
    for (int x = 0; x < w; ++x) {
148
165k
      int32_t res = 0;
149
1.49M
      for (int k = 0; k < filter_params_y->taps; ++k) {
150
1.32M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
151
1.32M
      }
152
165k
      dst[y * dst_stride + x] =
153
165k
          clip_pixel(ROUND_POWER_OF_TWO(res, FILTER_BITS));
154
165k
    }
155
25.9k
  }
156
4.32k
}
157
158
void av1_convolve_x_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
159
                         int dst_stride, int w, int h,
160
                         const InterpFilterParams *filter_params_x,
161
6.20k
                         const int subpel_x_qn, ConvolveParams *conv_params) {
162
6.20k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
163
6.20k
  const int bits = FILTER_BITS - conv_params->round_0;
164
165
6.20k
  assert(bits >= 0);
166
6.20k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
167
6.20k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
168
169
  // horizontal filter
170
6.20k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
171
6.20k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
172
173
41.5k
  for (int y = 0; y < h; ++y) {
174
248k
    for (int x = 0; x < w; ++x) {
175
213k
      int32_t res = 0;
176
1.91M
      for (int k = 0; k < filter_params_x->taps; ++k) {
177
1.70M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
178
1.70M
      }
179
213k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
180
213k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
181
213k
    }
182
35.3k
  }
183
6.20k
}
184
185
// This function is exactly the same as av1_convolve_2d_sr_c, and is an
186
// optimized version for intrabc. Use the following 2-tap filter:
187
// DECLARE_ALIGNED(256, static const int16_t,
188
//                 av1_intrabc_bilinear_filter[2 * SUBPEL_SHIFTS]) = {
189
//   128, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190
//   64,  64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191
// };
192
void av1_convolve_2d_sr_intrabc_c(const uint8_t *src, int src_stride,
193
                                  uint8_t *dst, int dst_stride, int w, int h,
194
                                  const InterpFilterParams *filter_params_x,
195
                                  const InterpFilterParams *filter_params_y,
196
                                  const int subpel_x_qn, const int subpel_y_qn,
197
2.88k
                                  ConvolveParams *conv_params) {
198
2.88k
  assert(subpel_x_qn == 8);
199
2.88k
  assert(subpel_y_qn == 8);
200
2.88k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
201
2.88k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
202
2.88k
  (void)filter_params_x;
203
2.88k
  (void)subpel_x_qn;
204
2.88k
  (void)filter_params_y;
205
2.88k
  (void)subpel_y_qn;
206
2.88k
  (void)conv_params;
207
208
2.88k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
209
2.88k
  int im_h = h + 1;
210
2.88k
  int im_stride = w;
211
2.88k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
212
2.88k
  const int bd = 8;
213
214
  // horizontal filter
215
  // explicitly operate for subpel_x_qn = 8.
216
2.88k
  int16_t *im = im_block;
217
26.3k
  for (int y = 0; y < im_h; ++y) {
218
261k
    for (int x = 0; x < w; ++x) {
219
238k
      const int32_t sum = (1 << bd) + src[x] + src[x + 1];
220
238k
      assert(0 <= sum && sum < (1 << (bd + 2)));
221
238k
      im[x] = sum;
222
238k
    }
223
23.4k
    src += src_stride;
224
23.4k
    im += im_stride;
225
23.4k
  }
226
227
  // vertical filter
228
  // explicitly operate for subpel_y_qn = 8.
229
2.88k
  int16_t *src_vert = im_block;
230
23.4k
  for (int y = 0; y < h; ++y) {
231
236k
    for (int x = 0; x < w; ++x) {
232
215k
      const int32_t sum =
233
215k
          (1 << (bd + 2)) + src_vert[x] + src_vert[im_stride + x];
234
215k
      assert(0 <= sum && sum < (1 << (bd + 4)));
235
215k
      const int16_t res =
236
215k
          ROUND_POWER_OF_TWO(sum, 2) - ((1 << bd) + (1 << (bd - 1)));
237
215k
      dst[x] = clip_pixel(res);
238
215k
    }
239
20.5k
    src_vert += im_stride;
240
20.5k
    dst += dst_stride;
241
20.5k
  }
242
2.88k
}
243
244
// This function is exactly the same as av1_convolve_y_sr_c, and is an
245
// optimized version for intrabc.
246
void av1_convolve_y_sr_intrabc_c(const uint8_t *src, int src_stride,
247
                                 uint8_t *dst, int dst_stride, int w, int h,
248
                                 const InterpFilterParams *filter_params_y,
249
3.33k
                                 const int subpel_y_qn) {
250
3.33k
  assert(subpel_y_qn == 8);
251
3.33k
  assert(filter_params_y->taps == 2);
252
3.33k
  (void)filter_params_y;
253
3.33k
  (void)subpel_y_qn;
254
255
  // vertical filter
256
  // explicitly operate for subpel_y_qn = 8.
257
27.9k
  for (int y = 0; y < h; ++y) {
258
342k
    for (int x = 0; x < w; ++x) {
259
318k
      const int32_t res = src[x] + src[src_stride + x];
260
318k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
261
318k
    }
262
24.6k
    src += src_stride;
263
24.6k
    dst += dst_stride;
264
24.6k
  }
265
3.33k
}
266
267
// This function is exactly the same as av1_convolve_x_sr_c, and is an
268
// optimized version for intrabc.
269
void av1_convolve_x_sr_intrabc_c(const uint8_t *src, int src_stride,
270
                                 uint8_t *dst, int dst_stride, int w, int h,
271
                                 const InterpFilterParams *filter_params_x,
272
                                 const int subpel_x_qn,
273
3.49k
                                 ConvolveParams *conv_params) {
274
3.49k
  assert(subpel_x_qn == 8);
275
3.49k
  assert(filter_params_x->taps == 2);
276
3.49k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
277
3.49k
  (void)filter_params_x;
278
3.49k
  (void)subpel_x_qn;
279
3.49k
  (void)conv_params;
280
281
  // horizontal filter
282
  // explicitly operate for subpel_x_qn = 8.
283
33.0k
  for (int y = 0; y < h; ++y) {
284
504k
    for (int x = 0; x < w; ++x) {
285
475k
      const int32_t res = src[x] + src[x + 1];
286
475k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
287
475k
    }
288
29.5k
    src += src_stride;
289
29.5k
    dst += dst_stride;
290
29.5k
  }
291
3.49k
}
292
293
void av1_dist_wtd_convolve_2d_c(const uint8_t *src, int src_stride,
294
                                uint8_t *dst, int dst_stride, int w, int h,
295
                                const InterpFilterParams *filter_params_x,
296
                                const InterpFilterParams *filter_params_y,
297
                                const int subpel_x_qn, const int subpel_y_qn,
298
2.92k
                                ConvolveParams *conv_params) {
299
2.92k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
300
2.92k
  int dst16_stride = conv_params->dst_stride;
301
2.92k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
302
2.92k
  int im_h = h + filter_params_y->taps - 1;
303
2.92k
  int im_stride = w;
304
2.92k
  const int fo_vert = filter_params_y->taps / 2 - 1;
305
2.92k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
306
2.92k
  const int bd = 8;
307
2.92k
  const int round_bits =
308
2.92k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
309
310
  // horizontal filter
311
2.92k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
312
2.92k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
313
2.92k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
314
47.9k
  for (int y = 0; y < im_h; ++y) {
315
404k
    for (int x = 0; x < w; ++x) {
316
358k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
317
3.23M
      for (int k = 0; k < filter_params_x->taps; ++k) {
318
2.87M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
319
2.87M
      }
320
358k
      assert(filter_params_x->taps > 8 ||
321
358k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
322
358k
      im_block[y * im_stride + x] =
323
358k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
324
358k
    }
325
45.0k
  }
326
327
  // vertical filter
328
2.92k
  int16_t *src_vert = im_block + fo_vert * im_stride;
329
2.92k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
330
2.92k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
331
2.92k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
332
27.4k
  for (int y = 0; y < h; ++y) {
333
221k
    for (int x = 0; x < w; ++x) {
334
196k
      int32_t sum = 1 << offset_bits;
335
1.76M
      for (int k = 0; k < filter_params_y->taps; ++k) {
336
1.57M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
337
1.57M
      }
338
196k
      assert(filter_params_y->taps > 8 ||
339
196k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
340
196k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
341
196k
      if (conv_params->do_average) {
342
119k
        int32_t tmp = dst16[y * dst16_stride + x];
343
119k
        if (conv_params->use_dist_wtd_comp_avg) {
344
48.0k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
345
48.0k
          tmp = tmp >> DIST_PRECISION_BITS;
346
71.0k
        } else {
347
71.0k
          tmp += res;
348
71.0k
          tmp = tmp >> 1;
349
71.0k
        }
350
119k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
351
119k
               (1 << (offset_bits - conv_params->round_1 - 1));
352
119k
        dst[y * dst_stride + x] =
353
119k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
354
119k
      } else {
355
77.6k
        dst16[y * dst16_stride + x] = res;
356
77.6k
      }
357
196k
    }
358
24.5k
  }
359
2.92k
}
360
361
void av1_dist_wtd_convolve_y_c(const uint8_t *src, int src_stride, uint8_t *dst,
362
                               int dst_stride, int w, int h,
363
                               const InterpFilterParams *filter_params_y,
364
                               const int subpel_y_qn,
365
2.78k
                               ConvolveParams *conv_params) {
366
2.78k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
367
2.78k
  int dst16_stride = conv_params->dst_stride;
368
2.78k
  const int fo_vert = filter_params_y->taps / 2 - 1;
369
2.78k
  const int bits = FILTER_BITS - conv_params->round_0;
370
2.78k
  const int bd = 8;
371
2.78k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
372
2.78k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
373
2.78k
                           (1 << (offset_bits - conv_params->round_1 - 1));
374
2.78k
  const int round_bits =
375
2.78k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
376
377
  // vertical filter
378
2.78k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
379
2.78k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
380
28.8k
  for (int y = 0; y < h; ++y) {
381
235k
    for (int x = 0; x < w; ++x) {
382
209k
      int32_t res = 0;
383
1.88M
      for (int k = 0; k < filter_params_y->taps; ++k) {
384
1.67M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
385
1.67M
      }
386
209k
      res *= (1 << bits);
387
209k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
388
389
209k
      if (conv_params->do_average) {
390
82.2k
        int32_t tmp = dst16[y * dst16_stride + x];
391
82.2k
        if (conv_params->use_dist_wtd_comp_avg) {
392
3.20k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
393
3.20k
          tmp = tmp >> DIST_PRECISION_BITS;
394
79.0k
        } else {
395
79.0k
          tmp += res;
396
79.0k
          tmp = tmp >> 1;
397
79.0k
        }
398
82.2k
        tmp -= round_offset;
399
82.2k
        dst[y * dst_stride + x] =
400
82.2k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
401
127k
      } else {
402
127k
        dst16[y * dst16_stride + x] = res;
403
127k
      }
404
209k
    }
405
26.0k
  }
406
2.78k
}
407
408
void av1_dist_wtd_convolve_x_c(const uint8_t *src, int src_stride, uint8_t *dst,
409
                               int dst_stride, int w, int h,
410
                               const InterpFilterParams *filter_params_x,
411
                               const int subpel_x_qn,
412
1.24k
                               ConvolveParams *conv_params) {
413
1.24k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
414
1.24k
  int dst16_stride = conv_params->dst_stride;
415
1.24k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
416
1.24k
  const int bits = FILTER_BITS - conv_params->round_1;
417
1.24k
  const int bd = 8;
418
1.24k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
419
1.24k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
420
1.24k
                           (1 << (offset_bits - conv_params->round_1 - 1));
421
1.24k
  const int round_bits =
422
1.24k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
423
424
  // horizontal filter
425
1.24k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
426
1.24k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
427
12.5k
  for (int y = 0; y < h; ++y) {
428
111k
    for (int x = 0; x < w; ++x) {
429
99.8k
      int32_t res = 0;
430
898k
      for (int k = 0; k < filter_params_x->taps; ++k) {
431
798k
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
432
798k
      }
433
99.8k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
434
99.8k
      res += round_offset;
435
436
99.8k
      if (conv_params->do_average) {
437
20.9k
        int32_t tmp = dst16[y * dst16_stride + x];
438
20.9k
        if (conv_params->use_dist_wtd_comp_avg) {
439
8.57k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
440
8.57k
          tmp = tmp >> DIST_PRECISION_BITS;
441
12.3k
        } else {
442
12.3k
          tmp += res;
443
12.3k
          tmp = tmp >> 1;
444
12.3k
        }
445
20.9k
        tmp -= round_offset;
446
20.9k
        dst[y * dst_stride + x] =
447
20.9k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
448
78.8k
      } else {
449
78.8k
        dst16[y * dst16_stride + x] = res;
450
78.8k
      }
451
99.8k
    }
452
11.3k
  }
453
1.24k
}
454
455
void av1_dist_wtd_convolve_2d_copy_c(const uint8_t *src, int src_stride,
456
                                     uint8_t *dst, int dst_stride, int w, int h,
457
5.26k
                                     ConvolveParams *conv_params) {
458
5.26k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
459
5.26k
  int dst16_stride = conv_params->dst_stride;
460
5.26k
  const int bits =
461
5.26k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
462
5.26k
  const int bd = 8;
463
5.26k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
464
5.26k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
465
5.26k
                           (1 << (offset_bits - conv_params->round_1 - 1));
466
467
54.9k
  for (int y = 0; y < h; ++y) {
468
545k
    for (int x = 0; x < w; ++x) {
469
495k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
470
495k
      res += round_offset;
471
472
495k
      if (conv_params->do_average) {
473
92.0k
        int32_t tmp = dst16[y * dst16_stride + x];
474
92.0k
        if (conv_params->use_dist_wtd_comp_avg) {
475
4.22k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
476
4.22k
          tmp = tmp >> DIST_PRECISION_BITS;
477
87.7k
        } else {
478
87.7k
          tmp += res;
479
87.7k
          tmp = tmp >> 1;
480
87.7k
        }
481
92.0k
        tmp -= round_offset;
482
92.0k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
483
403k
      } else {
484
403k
        dst16[y * dst16_stride + x] = res;
485
403k
      }
486
495k
    }
487
49.6k
  }
488
5.26k
}
489
490
void av1_convolve_2d_scale_c(const uint8_t *src, int src_stride, uint8_t *dst,
491
                             int dst_stride, int w, int h,
492
                             const InterpFilterParams *filter_params_x,
493
                             const InterpFilterParams *filter_params_y,
494
                             const int subpel_x_qn, const int x_step_qn,
495
                             const int subpel_y_qn, const int y_step_qn,
496
9.99k
                             ConvolveParams *conv_params) {
497
9.99k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
498
9.99k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
499
9.99k
             filter_params_y->taps;
500
9.99k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
501
9.99k
  const int dst16_stride = conv_params->dst_stride;
502
9.99k
  const int bits =
503
9.99k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
504
9.99k
  assert(bits >= 0);
505
9.99k
  int im_stride = w;
506
9.99k
  const int fo_vert = filter_params_y->taps / 2 - 1;
507
9.99k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
508
9.99k
  const int bd = 8;
509
510
  // horizontal filter
511
9.99k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
512
145k
  for (int y = 0; y < im_h; ++y) {
513
135k
    int x_qn = subpel_x_qn;
514
1.24M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
515
1.10M
      const uint8_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
516
1.10M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
517
1.10M
      assert(x_filter_idx < SUBPEL_SHIFTS);
518
1.10M
      const int16_t *x_filter =
519
1.10M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
520
1.10M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
521
9.95M
      for (int k = 0; k < filter_params_x->taps; ++k) {
522
8.84M
        sum += x_filter[k] * src_x[k - fo_horiz];
523
8.84M
      }
524
1.10M
      assert(filter_params_x->taps > 8 ||
525
1.10M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
526
1.10M
      im_block[y * im_stride + x] =
527
1.10M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
528
1.10M
    }
529
135k
    src_horiz += src_stride;
530
135k
  }
531
532
  // vertical filter
533
9.99k
  int16_t *src_vert = im_block + fo_vert * im_stride;
534
9.99k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
535
70.3k
  for (int x = 0; x < w; ++x) {
536
60.3k
    int y_qn = subpel_y_qn;
537
761k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
538
701k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
539
701k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
540
701k
      assert(y_filter_idx < SUBPEL_SHIFTS);
541
701k
      const int16_t *y_filter =
542
701k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
543
701k
      int32_t sum = 1 << offset_bits;
544
6.31M
      for (int k = 0; k < filter_params_y->taps; ++k) {
545
5.60M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
546
5.60M
      }
547
701k
      assert(filter_params_y->taps > 8 ||
548
701k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
549
701k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
550
701k
      if (conv_params->is_compound) {
551
198k
        if (conv_params->do_average) {
552
159k
          int32_t tmp = dst16[y * dst16_stride + x];
553
159k
          if (conv_params->use_dist_wtd_comp_avg) {
554
91.3k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
555
91.3k
            tmp = tmp >> DIST_PRECISION_BITS;
556
91.3k
          } else {
557
68.5k
            tmp += res;
558
68.5k
            tmp = tmp >> 1;
559
68.5k
          }
560
          /* Subtract round offset and convolve round */
561
159k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
562
159k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
563
159k
          dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
564
159k
        } else {
565
38.8k
          dst16[y * dst16_stride + x] = res;
566
38.8k
        }
567
502k
      } else {
568
        /* Subtract round offset and convolve round */
569
502k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
570
502k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
571
502k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
572
502k
      }
573
701k
    }
574
60.3k
    src_vert++;
575
60.3k
  }
576
9.99k
}
577
578
static void convolve_2d_scale_wrapper(
579
    const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int w,
580
    int h, const InterpFilterParams *filter_params_x,
581
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
582
    const int x_step_qn, const int subpel_y_qn, const int y_step_qn,
583
9.99k
    ConvolveParams *conv_params) {
584
9.99k
  if (conv_params->is_compound) {
585
2.02k
    assert(conv_params->dst != NULL);
586
2.02k
  }
587
9.99k
  av1_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h, filter_params_x,
588
9.99k
                        filter_params_y, subpel_x_qn, x_step_qn, subpel_y_qn,
589
9.99k
                        y_step_qn, conv_params);
590
9.99k
}
591
592
static void convolve_2d_facade_compound(
593
    const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int w,
594
    int h, const InterpFilterParams *filter_params_x,
595
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
596
12.2k
    const int subpel_y_qn, ConvolveParams *conv_params) {
597
12.2k
  const bool need_x = subpel_x_qn != 0;
598
12.2k
  const bool need_y = subpel_y_qn != 0;
599
12.2k
  if (!need_x && !need_y) {
600
5.26k
    av1_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
601
5.26k
                                  conv_params);
602
6.95k
  } else if (need_x && !need_y) {
603
1.24k
    av1_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
604
1.24k
                            filter_params_x, subpel_x_qn, conv_params);
605
5.71k
  } else if (!need_x && need_y) {
606
2.78k
    av1_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
607
2.78k
                            filter_params_y, subpel_y_qn, conv_params);
608
2.92k
  } else {
609
2.92k
    assert(need_y && need_x);
610
2.92k
    av1_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
611
2.92k
                             filter_params_x, filter_params_y, subpel_x_qn,
612
2.92k
                             subpel_y_qn, conv_params);
613
2.92k
  }
614
12.2k
}
615
616
static void convolve_2d_facade_single(
617
    const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int w,
618
    int h, const InterpFilterParams *filter_params_x,
619
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
620
173k
    const int subpel_y_qn, ConvolveParams *conv_params) {
621
173k
  const bool need_x = subpel_x_qn != 0;
622
173k
  const bool need_y = subpel_y_qn != 0;
623
173k
  if (!need_x && !need_y) {
624
156k
    aom_convolve_copy(src, src_stride, dst, dst_stride, w, h);
625
156k
  } else if (need_x && !need_y) {
626
6.20k
    av1_convolve_x_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
627
6.20k
                      subpel_x_qn, conv_params);
628
11.5k
  } else if (!need_x && need_y) {
629
4.32k
    av1_convolve_y_sr(src, src_stride, dst, dst_stride, w, h, filter_params_y,
630
4.32k
                      subpel_y_qn);
631
7.24k
  } else {
632
7.24k
    assert(need_x && need_y);
633
7.24k
    av1_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
634
7.24k
                       filter_params_y, subpel_x_qn, subpel_y_qn, conv_params);
635
7.24k
  }
636
173k
}
637
638
void av1_convolve_2d_facade(const uint8_t *src, int src_stride, uint8_t *dst,
639
                            int dst_stride, int w, int h,
640
                            const InterpFilterParams *interp_filters[2],
641
                            const int subpel_x_qn, int x_step_q4,
642
                            const int subpel_y_qn, int y_step_q4, int scaled,
643
205k
                            ConvolveParams *conv_params) {
644
205k
  (void)x_step_q4;
645
205k
  (void)y_step_q4;
646
205k
  (void)dst;
647
205k
  (void)dst_stride;
648
649
205k
  const InterpFilterParams *filter_params_x = interp_filters[0];
650
205k
  const InterpFilterParams *filter_params_y = interp_filters[1];
651
652
  // TODO(jingning, yunqing): Add SIMD support to 2-tap filter case.
653
  // 2-tap filter indicates that it is for IntraBC.
654
205k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
655
148k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
656
148k
    assert(!scaled);
657
148k
    if (subpel_x_qn && subpel_y_qn) {
658
2.88k
      av1_convolve_2d_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
659
2.88k
                                 filter_params_x, filter_params_y, subpel_x_qn,
660
2.88k
                                 subpel_y_qn, conv_params);
661
2.88k
      return;
662
146k
    } else if (subpel_x_qn) {
663
3.49k
      av1_convolve_x_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
664
3.49k
                                filter_params_x, subpel_x_qn, conv_params);
665
3.49k
      return;
666
142k
    } else if (subpel_y_qn) {
667
3.33k
      av1_convolve_y_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
668
3.33k
                                filter_params_y, subpel_y_qn);
669
3.33k
      return;
670
3.33k
    }
671
148k
  }
672
673
196k
  if (scaled) {
674
9.99k
    convolve_2d_scale_wrapper(src, src_stride, dst, dst_stride, w, h,
675
9.99k
                              filter_params_x, filter_params_y, subpel_x_qn,
676
9.99k
                              x_step_q4, subpel_y_qn, y_step_q4, conv_params);
677
186k
  } else if (conv_params->is_compound) {
678
12.2k
    convolve_2d_facade_compound(src, src_stride, dst, dst_stride, w, h,
679
12.2k
                                filter_params_x, filter_params_y, subpel_x_qn,
680
12.2k
                                subpel_y_qn, conv_params);
681
173k
  } else {
682
173k
    convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
683
173k
                              filter_params_x, filter_params_y, subpel_x_qn,
684
173k
                              subpel_y_qn, conv_params);
685
173k
  }
686
196k
}
687
688
#if CONFIG_AV1_HIGHBITDEPTH
689
void av1_highbd_convolve_x_sr_c(const uint16_t *src, int src_stride,
690
                                uint16_t *dst, int dst_stride, int w, int h,
691
                                const InterpFilterParams *filter_params_x,
692
                                const int subpel_x_qn,
693
6.75k
                                ConvolveParams *conv_params, int bd) {
694
6.75k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
695
6.75k
  const int bits = FILTER_BITS - conv_params->round_0;
696
697
6.75k
  assert(bits >= 0);
698
6.75k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
699
6.75k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
700
701
  // horizontal filter
702
6.75k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
703
6.75k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
704
48.8k
  for (int y = 0; y < h; ++y) {
705
312k
    for (int x = 0; x < w; ++x) {
706
270k
      int32_t res = 0;
707
2.43M
      for (int k = 0; k < filter_params_x->taps; ++k) {
708
2.16M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
709
2.16M
      }
710
270k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
711
270k
      dst[y * dst_stride + x] =
712
270k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
713
270k
    }
714
42.1k
  }
715
6.75k
}
716
717
void av1_highbd_convolve_y_sr_c(const uint16_t *src, int src_stride,
718
                                uint16_t *dst, int dst_stride, int w, int h,
719
                                const InterpFilterParams *filter_params_y,
720
4.53k
                                const int subpel_y_qn, int bd) {
721
4.53k
  const int fo_vert = filter_params_y->taps / 2 - 1;
722
  // vertical filter
723
4.53k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
724
4.53k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
725
35.8k
  for (int y = 0; y < h; ++y) {
726
251k
    for (int x = 0; x < w; ++x) {
727
220k
      int32_t res = 0;
728
1.98M
      for (int k = 0; k < filter_params_y->taps; ++k) {
729
1.76M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
730
1.76M
      }
731
220k
      dst[y * dst_stride + x] =
732
220k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, FILTER_BITS), bd);
733
220k
    }
734
31.3k
  }
735
4.53k
}
736
737
void av1_highbd_convolve_2d_sr_c(const uint16_t *src, int src_stride,
738
                                 uint16_t *dst, int dst_stride, int w, int h,
739
                                 const InterpFilterParams *filter_params_x,
740
                                 const InterpFilterParams *filter_params_y,
741
                                 const int subpel_x_qn, const int subpel_y_qn,
742
8.06k
                                 ConvolveParams *conv_params, int bd) {
743
8.06k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
744
8.06k
  int im_h = h + filter_params_y->taps - 1;
745
8.06k
  int im_stride = w;
746
8.06k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
747
8.06k
  const int fo_vert = filter_params_y->taps / 2 - 1;
748
8.06k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
749
8.06k
  const int bits =
750
8.06k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
751
8.06k
  assert(bits >= 0);
752
753
  // horizontal filter
754
8.06k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
755
8.06k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
756
8.06k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
757
114k
  for (int y = 0; y < im_h; ++y) {
758
722k
    for (int x = 0; x < w; ++x) {
759
616k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
760
5.55M
      for (int k = 0; k < filter_params_x->taps; ++k) {
761
4.93M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
762
4.93M
      }
763
616k
      assert(filter_params_x->taps > 8 ||
764
616k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
765
616k
      im_block[y * im_stride + x] =
766
616k
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
767
616k
    }
768
106k
  }
769
770
  // vertical filter
771
8.06k
  int16_t *src_vert = im_block + fo_vert * im_stride;
772
8.06k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
773
8.06k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
774
8.06k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
775
57.7k
  for (int y = 0; y < h; ++y) {
776
353k
    for (int x = 0; x < w; ++x) {
777
303k
      int32_t sum = 1 << offset_bits;
778
2.73M
      for (int k = 0; k < filter_params_y->taps; ++k) {
779
2.42M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
780
2.42M
      }
781
303k
      assert(filter_params_y->taps > 8 ||
782
303k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
783
303k
      int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
784
303k
                    ((1 << (offset_bits - conv_params->round_1)) +
785
303k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
786
303k
      dst[y * dst_stride + x] =
787
303k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
788
303k
    }
789
49.6k
  }
790
8.06k
}
791
792
// This function is exactly the same as av1_highbd_convolve_2d_sr_c, and is an
793
// optimized version for intrabc. Use the following 2-tap filter:
794
// DECLARE_ALIGNED(256, static const int16_t,
795
//                 av1_intrabc_bilinear_filter[2 * SUBPEL_SHIFTS]) = {
796
//   128, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
797
//   64,  64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
798
// };
799
void av1_highbd_convolve_2d_sr_intrabc_c(
800
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
801
    int h, const InterpFilterParams *filter_params_x,
802
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
803
20.2k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
804
20.2k
  const int bits =
805
20.2k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
806
20.2k
  assert(bits >= 0);
807
20.2k
  assert(subpel_x_qn == 8);
808
20.2k
  assert(subpel_y_qn == 8);
809
20.2k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
810
20.2k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
811
20.2k
  (void)filter_params_x;
812
20.2k
  (void)subpel_x_qn;
813
20.2k
  (void)filter_params_y;
814
20.2k
  (void)subpel_y_qn;
815
20.2k
  (void)conv_params;
816
817
20.2k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
818
20.2k
  int im_h = h + 1;
819
20.2k
  int im_stride = w;
820
20.2k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
821
822
  // horizontal filter
823
  // explicitly operate for subpel_x_qn = 8.
824
20.2k
  int16_t *im = im_block;
825
172k
  for (int y = 0; y < im_h; ++y) {
826
1.47M
    for (int x = 0; x < w; ++x) {
827
1.32M
      int32_t sum = (1 << (bd + FILTER_BITS - 1)) + 64 * (src[x] + src[x + 1]);
828
1.32M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
829
1.32M
      sum = ROUND_POWER_OF_TWO(sum, conv_params->round_0);
830
1.32M
      im[x] = sum;
831
1.32M
    }
832
152k
    src += src_stride;
833
152k
    im += im_stride;
834
152k
  }
835
836
  // vertical filter
837
  // explicitly operate for subpel_y_qn = 8.
838
20.2k
  int16_t *src_vert = im_block;
839
20.2k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
840
152k
  for (int y = 0; y < h; ++y) {
841
1.30M
    for (int x = 0; x < w; ++x) {
842
1.17M
      const int32_t sum =
843
1.17M
          (1 << offset_bits) + 64 * (src_vert[x] + src_vert[im_stride + x]);
844
1.17M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
845
1.17M
      const int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
846
1.17M
                          ((1 << (offset_bits - conv_params->round_1)) +
847
1.17M
                           (1 << (offset_bits - conv_params->round_1 - 1)));
848
849
1.17M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
850
1.17M
    }
851
132k
    src_vert += im_stride;
852
132k
    dst += dst_stride;
853
132k
  }
854
20.2k
}
855
856
// This function is exactly the same as av1_highbd_convolve_y_sr_c, and is an
857
// optimized version for intrabc.
858
void av1_highbd_convolve_y_sr_intrabc_c(
859
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
860
    int h, const InterpFilterParams *filter_params_y, const int subpel_y_qn,
861
18.2k
    int bd) {
862
18.2k
  assert(subpel_y_qn == 8);
863
18.2k
  assert(filter_params_y->taps == 2);
864
18.2k
  (void)filter_params_y;
865
18.2k
  (void)subpel_y_qn;
866
867
  // vertical filter
868
  // explicitly operate for subpel_y_qn = 8.
869
133k
  for (int y = 0; y < h; ++y) {
870
1.08M
    for (int x = 0; x < w; ++x) {
871
971k
      const int32_t res = src[x] + src[src_stride + x];
872
971k
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, 1), bd);
873
971k
    }
874
115k
    src += src_stride;
875
115k
    dst += dst_stride;
876
115k
  }
877
18.2k
}
878
879
// This function is exactly the same as av1_highbd_convolve_x_sr_c, and is an
880
// optimized version for intrabc.
881
void av1_highbd_convolve_x_sr_intrabc_c(
882
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
883
    int h, const InterpFilterParams *filter_params_x, const int subpel_x_qn,
884
18.4k
    ConvolveParams *conv_params, int bd) {
885
18.4k
  const int bits = FILTER_BITS - conv_params->round_0;
886
18.4k
  assert(bits >= 0);
887
18.4k
  assert(subpel_x_qn == 8);
888
18.4k
  assert(filter_params_x->taps == 2);
889
18.4k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
890
18.4k
  (void)filter_params_x;
891
18.4k
  (void)subpel_x_qn;
892
893
  // horizontal filter
894
  // explicitly operate for subpel_x_qn = 8.
895
136k
  for (int y = 0; y < h; ++y) {
896
1.29M
    for (int x = 0; x < w; ++x) {
897
1.17M
      int32_t res = 64 * (src[x] + src[x + 1]);
898
1.17M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
899
1.17M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
900
1.17M
    }
901
118k
    src += src_stride;
902
118k
    dst += dst_stride;
903
118k
  }
904
18.4k
}
905
906
void av1_highbd_dist_wtd_convolve_2d_c(
907
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
908
    int h, const InterpFilterParams *filter_params_x,
909
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
910
4.52k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
911
4.52k
  int x, y, k;
912
4.52k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
913
4.52k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
914
4.52k
  int dst16_stride = conv_params->dst_stride;
915
4.52k
  int im_h = h + filter_params_y->taps - 1;
916
4.52k
  int im_stride = w;
917
4.52k
  const int fo_vert = filter_params_y->taps / 2 - 1;
918
4.52k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
919
4.52k
  const int round_bits =
920
4.52k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
921
4.52k
  assert(round_bits >= 0);
922
923
  // horizontal filter
924
4.52k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
925
4.52k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
926
4.52k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
927
78.6k
  for (y = 0; y < im_h; ++y) {
928
685k
    for (x = 0; x < w; ++x) {
929
611k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
930
5.50M
      for (k = 0; k < filter_params_x->taps; ++k) {
931
4.89M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
932
4.89M
      }
933
611k
      assert(filter_params_x->taps > 8 ||
934
611k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
935
611k
      (void)bd;
936
611k
      im_block[y * im_stride + x] =
937
611k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
938
611k
    }
939
74.1k
  }
940
941
  // vertical filter
942
4.52k
  int16_t *src_vert = im_block + fo_vert * im_stride;
943
4.52k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
944
4.52k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
945
4.52k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
946
47.0k
  for (y = 0; y < h; ++y) {
947
400k
    for (x = 0; x < w; ++x) {
948
357k
      int32_t sum = 1 << offset_bits;
949
3.22M
      for (k = 0; k < filter_params_y->taps; ++k) {
950
2.86M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
951
2.86M
      }
952
357k
      assert(filter_params_y->taps > 8 ||
953
357k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
954
357k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
955
357k
      if (conv_params->do_average) {
956
117k
        int32_t tmp = dst16[y * dst16_stride + x];
957
117k
        if (conv_params->use_dist_wtd_comp_avg) {
958
12.2k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
959
12.2k
          tmp = tmp >> DIST_PRECISION_BITS;
960
105k
        } else {
961
105k
          tmp += res;
962
105k
          tmp = tmp >> 1;
963
105k
        }
964
117k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
965
117k
               (1 << (offset_bits - conv_params->round_1 - 1));
966
117k
        dst[y * dst_stride + x] =
967
117k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
968
240k
      } else {
969
240k
        dst16[y * dst16_stride + x] = res;
970
240k
      }
971
357k
    }
972
42.4k
  }
973
4.52k
}
974
975
void av1_highbd_dist_wtd_convolve_x_c(const uint16_t *src, int src_stride,
976
                                      uint16_t *dst, int dst_stride, int w,
977
                                      int h,
978
                                      const InterpFilterParams *filter_params_x,
979
                                      const int subpel_x_qn,
980
3.55k
                                      ConvolveParams *conv_params, int bd) {
981
3.55k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
982
3.55k
  int dst16_stride = conv_params->dst_stride;
983
3.55k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
984
3.55k
  const int bits = FILTER_BITS - conv_params->round_1;
985
3.55k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
986
3.55k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
987
3.55k
                           (1 << (offset_bits - conv_params->round_1 - 1));
988
3.55k
  const int round_bits =
989
3.55k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
990
3.55k
  assert(round_bits >= 0);
991
3.55k
  assert(bits >= 0);
992
  // horizontal filter
993
3.55k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
994
3.55k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
995
42.9k
  for (int y = 0; y < h; ++y) {
996
419k
    for (int x = 0; x < w; ++x) {
997
380k
      int32_t res = 0;
998
3.42M
      for (int k = 0; k < filter_params_x->taps; ++k) {
999
3.04M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
1000
3.04M
      }
1001
380k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
1002
380k
      res += round_offset;
1003
1004
380k
      if (conv_params->do_average) {
1005
260k
        int32_t tmp = dst16[y * dst16_stride + x];
1006
260k
        if (conv_params->use_dist_wtd_comp_avg) {
1007
10.0k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1008
10.0k
          tmp = tmp >> DIST_PRECISION_BITS;
1009
250k
        } else {
1010
250k
          tmp += res;
1011
250k
          tmp = tmp >> 1;
1012
250k
        }
1013
260k
        tmp -= round_offset;
1014
260k
        dst[y * dst_stride + x] =
1015
260k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1016
260k
      } else {
1017
119k
        dst16[y * dst16_stride + x] = res;
1018
119k
      }
1019
380k
    }
1020
39.4k
  }
1021
3.55k
}
1022
1023
void av1_highbd_dist_wtd_convolve_y_c(const uint16_t *src, int src_stride,
1024
                                      uint16_t *dst, int dst_stride, int w,
1025
                                      int h,
1026
                                      const InterpFilterParams *filter_params_y,
1027
                                      const int subpel_y_qn,
1028
3.02k
                                      ConvolveParams *conv_params, int bd) {
1029
3.02k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1030
3.02k
  int dst16_stride = conv_params->dst_stride;
1031
3.02k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1032
3.02k
  const int bits = FILTER_BITS - conv_params->round_0;
1033
3.02k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1034
3.02k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1035
3.02k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1036
3.02k
  const int round_bits =
1037
3.02k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
1038
3.02k
  assert(round_bits >= 0);
1039
3.02k
  assert(bits >= 0);
1040
  // vertical filter
1041
3.02k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
1042
3.02k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
1043
35.2k
  for (int y = 0; y < h; ++y) {
1044
365k
    for (int x = 0; x < w; ++x) {
1045
333k
      int32_t res = 0;
1046
3.00M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1047
2.66M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
1048
2.66M
      }
1049
333k
      res *= (1 << bits);
1050
333k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
1051
1052
333k
      if (conv_params->do_average) {
1053
94.4k
        int32_t tmp = dst16[y * dst16_stride + x];
1054
94.4k
        if (conv_params->use_dist_wtd_comp_avg) {
1055
10.4k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1056
10.4k
          tmp = tmp >> DIST_PRECISION_BITS;
1057
84.0k
        } else {
1058
84.0k
          tmp += res;
1059
84.0k
          tmp = tmp >> 1;
1060
84.0k
        }
1061
94.4k
        tmp -= round_offset;
1062
94.4k
        dst[y * dst_stride + x] =
1063
94.4k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1064
238k
      } else {
1065
238k
        dst16[y * dst16_stride + x] = res;
1066
238k
      }
1067
333k
    }
1068
32.2k
  }
1069
3.02k
}
1070
1071
void av1_highbd_dist_wtd_convolve_2d_copy_c(const uint16_t *src, int src_stride,
1072
                                            uint16_t *dst, int dst_stride,
1073
                                            int w, int h,
1074
                                            ConvolveParams *conv_params,
1075
13.3k
                                            int bd) {
1076
13.3k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1077
13.3k
  int dst16_stride = conv_params->dst_stride;
1078
13.3k
  const int bits =
1079
13.3k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
1080
13.3k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1081
13.3k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1082
13.3k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1083
13.3k
  assert(bits >= 0);
1084
1085
132k
  for (int y = 0; y < h; ++y) {
1086
1.21M
    for (int x = 0; x < w; ++x) {
1087
1.09M
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
1088
1.09M
      res += round_offset;
1089
1.09M
      if (conv_params->do_average) {
1090
309k
        int32_t tmp = dst16[y * dst16_stride + x];
1091
309k
        if (conv_params->use_dist_wtd_comp_avg) {
1092
211k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1093
211k
          tmp = tmp >> DIST_PRECISION_BITS;
1094
211k
        } else {
1095
98.8k
          tmp += res;
1096
98.8k
          tmp = tmp >> 1;
1097
98.8k
        }
1098
309k
        tmp -= round_offset;
1099
309k
        dst[y * dst_stride + x] =
1100
309k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1101
784k
      } else {
1102
784k
        dst16[y * dst16_stride + x] = res;
1103
784k
      }
1104
1.09M
    }
1105
118k
  }
1106
13.3k
}
1107
1108
void av1_highbd_convolve_2d_scale_c(const uint16_t *src, int src_stride,
1109
                                    uint16_t *dst, int dst_stride, int w, int h,
1110
                                    const InterpFilterParams *filter_params_x,
1111
                                    const InterpFilterParams *filter_params_y,
1112
                                    const int subpel_x_qn, const int x_step_qn,
1113
                                    const int subpel_y_qn, const int y_step_qn,
1114
7.18k
                                    ConvolveParams *conv_params, int bd) {
1115
7.18k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
1116
7.18k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
1117
7.18k
             filter_params_y->taps;
1118
7.18k
  int im_stride = w;
1119
7.18k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1120
7.18k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
1121
7.18k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1122
7.18k
  const int dst16_stride = conv_params->dst_stride;
1123
7.18k
  const int bits =
1124
7.18k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
1125
7.18k
  assert(bits >= 0);
1126
  // horizontal filter
1127
7.18k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
1128
114k
  for (int y = 0; y < im_h; ++y) {
1129
107k
    int x_qn = subpel_x_qn;
1130
1.44M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
1131
1.33M
      const uint16_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
1132
1.33M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1133
1.33M
      assert(x_filter_idx < SUBPEL_SHIFTS);
1134
1.33M
      const int16_t *x_filter =
1135
1.33M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
1136
1.33M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
1137
12.0M
      for (int k = 0; k < filter_params_x->taps; ++k) {
1138
10.6M
        sum += x_filter[k] * src_x[k - fo_horiz];
1139
10.6M
      }
1140
1.33M
      assert(filter_params_x->taps > 8 ||
1141
1.33M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
1142
1.33M
      im_block[y * im_stride + x] =
1143
1.33M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
1144
1.33M
    }
1145
107k
    src_horiz += src_stride;
1146
107k
  }
1147
1148
  // vertical filter
1149
7.18k
  int16_t *src_vert = im_block + fo_vert * im_stride;
1150
7.18k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1151
64.8k
  for (int x = 0; x < w; ++x) {
1152
57.6k
    int y_qn = subpel_y_qn;
1153
999k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
1154
941k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
1155
941k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1156
941k
      assert(y_filter_idx < SUBPEL_SHIFTS);
1157
941k
      const int16_t *y_filter =
1158
941k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
1159
941k
      int32_t sum = 1 << offset_bits;
1160
8.47M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1161
7.53M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
1162
7.53M
      }
1163
941k
      assert(filter_params_y->taps > 8 ||
1164
941k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
1165
941k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
1166
941k
      if (conv_params->is_compound) {
1167
203k
        if (conv_params->do_average) {
1168
82.7k
          int32_t tmp = dst16[y * dst16_stride + x];
1169
82.7k
          if (conv_params->use_dist_wtd_comp_avg) {
1170
53.2k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1171
53.2k
            tmp = tmp >> DIST_PRECISION_BITS;
1172
53.2k
          } else {
1173
29.5k
            tmp += res;
1174
29.5k
            tmp = tmp >> 1;
1175
29.5k
          }
1176
          /* Subtract round offset and convolve round */
1177
82.7k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
1178
82.7k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
1179
82.7k
          dst[y * dst_stride + x] =
1180
82.7k
              clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1181
120k
        } else {
1182
120k
          dst16[y * dst16_stride + x] = res;
1183
120k
        }
1184
738k
      } else {
1185
        /* Subtract round offset and convolve round */
1186
738k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
1187
738k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
1188
738k
        dst[y * dst_stride + x] =
1189
738k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1190
738k
      }
1191
941k
    }
1192
57.6k
    src_vert++;
1193
57.6k
  }
1194
7.18k
}
1195
1196
static void highbd_convolve_2d_facade_compound(
1197
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride,
1198
    const int w, const int h, const InterpFilterParams *filter_params_x,
1199
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
1200
24.4k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1201
24.4k
  const bool need_x = subpel_x_qn != 0;
1202
24.4k
  const bool need_y = subpel_y_qn != 0;
1203
24.4k
  if (!need_x && !need_y) {
1204
13.3k
    av1_highbd_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
1205
13.3k
                                         conv_params, bd);
1206
13.3k
  } else if (need_x && !need_y) {
1207
3.55k
    av1_highbd_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
1208
3.55k
                                   filter_params_x, subpel_x_qn, conv_params,
1209
3.55k
                                   bd);
1210
7.55k
  } else if (!need_x && need_y) {
1211
3.02k
    av1_highbd_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
1212
3.02k
                                   filter_params_y, subpel_y_qn, conv_params,
1213
3.02k
                                   bd);
1214
4.52k
  } else {
1215
4.52k
    assert(need_x && need_y);
1216
4.52k
    av1_highbd_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
1217
4.52k
                                    filter_params_x, filter_params_y,
1218
4.52k
                                    subpel_x_qn, subpel_y_qn, conv_params, bd);
1219
4.52k
  }
1220
24.4k
}
1221
1222
static void highbd_convolve_2d_facade_single(
1223
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride,
1224
    const int w, const int h, const InterpFilterParams *filter_params_x,
1225
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
1226
105k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1227
105k
  const bool need_x = subpel_x_qn != 0;
1228
105k
  const bool need_y = subpel_y_qn != 0;
1229
1230
105k
  if (!need_x && !need_y) {
1231
86.2k
    aom_highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h);
1232
86.2k
  } else if (need_x && !need_y) {
1233
6.75k
    av1_highbd_convolve_x_sr(src, src_stride, dst, dst_stride, w, h,
1234
6.75k
                             filter_params_x, subpel_x_qn, conv_params, bd);
1235
12.5k
  } else if (!need_x && need_y) {
1236
4.53k
    av1_highbd_convolve_y_sr(src, src_stride, dst, dst_stride, w, h,
1237
4.53k
                             filter_params_y, subpel_y_qn, bd);
1238
8.06k
  } else {
1239
8.06k
    assert(need_x && need_y);
1240
8.06k
    av1_highbd_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h,
1241
8.06k
                              filter_params_x, filter_params_y, subpel_x_qn,
1242
8.06k
                              subpel_y_qn, conv_params, bd);
1243
8.06k
  }
1244
105k
}
1245
1246
void av1_highbd_convolve_2d_facade(const uint8_t *src8, int src_stride,
1247
                                   uint8_t *dst8, int dst_stride, int w, int h,
1248
                                   const InterpFilterParams *interp_filters[2],
1249
                                   const int subpel_x_qn, int x_step_q4,
1250
                                   const int subpel_y_qn, int y_step_q4,
1251
                                   int scaled, ConvolveParams *conv_params,
1252
194k
                                   int bd) {
1253
194k
  (void)x_step_q4;
1254
194k
  (void)y_step_q4;
1255
194k
  (void)dst_stride;
1256
194k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1257
1258
194k
  const InterpFilterParams *filter_params_x = interp_filters[0];
1259
194k
  const InterpFilterParams *filter_params_y = interp_filters[1];
1260
1261
194k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1262
  // 2-tap filter indicates that it is for IntraBC.
1263
194k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
1264
128k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
1265
128k
    assert(!scaled);
1266
128k
    if (subpel_x_qn && subpel_y_qn) {
1267
20.2k
      av1_highbd_convolve_2d_sr_intrabc_c(
1268
20.2k
          src, src_stride, dst, dst_stride, w, h, filter_params_x,
1269
20.2k
          filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1270
20.2k
      return;
1271
108k
    } else if (subpel_x_qn) {
1272
18.4k
      av1_highbd_convolve_x_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1273
18.4k
                                         filter_params_x, subpel_x_qn,
1274
18.4k
                                         conv_params, bd);
1275
18.4k
      return;
1276
89.5k
    } else if (subpel_y_qn) {
1277
18.2k
      av1_highbd_convolve_y_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1278
18.2k
                                         filter_params_y, subpel_y_qn, bd);
1279
18.2k
      return;
1280
18.2k
    }
1281
128k
  }
1282
1283
137k
  if (scaled) {
1284
7.18k
    if (conv_params->is_compound) {
1285
1.42k
      assert(conv_params->dst != NULL);
1286
1.42k
    }
1287
7.18k
    av1_highbd_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h,
1288
7.18k
                                 filter_params_x, filter_params_y, subpel_x_qn,
1289
7.18k
                                 x_step_q4, subpel_y_qn, y_step_q4, conv_params,
1290
7.18k
                                 bd);
1291
130k
  } else if (conv_params->is_compound) {
1292
24.4k
    highbd_convolve_2d_facade_compound(
1293
24.4k
        src, src_stride, dst, dst_stride, w, h, filter_params_x,
1294
24.4k
        filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1295
105k
  } else {
1296
105k
    highbd_convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
1297
105k
                                     filter_params_x, filter_params_y,
1298
105k
                                     subpel_x_qn, subpel_y_qn, conv_params, bd);
1299
105k
  }
1300
137k
}
1301
#endif  // CONFIG_AV1_HIGHBITDEPTH
1302
1303
// Note: Fixed size intermediate buffers, place limits on parameters
1304
// of some functions. 2d filtering proceeds in 2 steps:
1305
//   (1) Interpolate horizontally into an intermediate buffer, temp.
1306
//   (2) Interpolate temp vertically to derive the sub-pixel result.
1307
// Deriving the maximum number of rows in the temp buffer (135):
1308
// --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
1309
// --Largest block size is 128x128 pixels.
1310
// --128 rows in the downscaled frame span a distance of (128 - 1) * 32 in the
1311
//   original frame (in 1/16th pixel units).
1312
// --Must round-up because block may be located at sub-pixel position.
1313
// --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
1314
// --((128 - 1) * 32 + 15) >> 4 + 8 = 263.
1315
#define WIENER_MAX_EXT_SIZE 263
1316
1317
#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1318
30.4M
static inline int horz_scalar_product(const uint8_t *a, const int16_t *b) {
1319
30.4M
  int sum = 0;
1320
274M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1321
30.4M
  return sum;
1322
30.4M
}
1323
1324
#if CONFIG_AV1_HIGHBITDEPTH
1325
static inline int highbd_horz_scalar_product(const uint16_t *a,
1326
28.9M
                                             const int16_t *b) {
1327
28.9M
  int sum = 0;
1328
260M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1329
28.9M
  return sum;
1330
28.9M
}
1331
#endif
1332
1333
static inline int highbd_vert_scalar_product(const uint16_t *a,
1334
                                             ptrdiff_t a_stride,
1335
57.3M
                                             const int16_t *b) {
1336
57.3M
  int sum = 0;
1337
504M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
1338
57.3M
  return sum;
1339
57.3M
}
1340
1341
53.9k
static const InterpKernel *get_filter_base(const int16_t *filter) {
1342
  // NOTE: This assumes that the filter table is 256-byte aligned.
1343
  // TODO(agrange) Modify to make independent of table alignment.
1344
53.9k
  return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
1345
53.9k
}
1346
1347
53.9k
static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
1348
53.9k
  return (int)((const InterpKernel *)(intptr_t)f - base);
1349
53.9k
}
1350
1351
static void convolve_add_src_horiz_hip(const uint8_t *src, ptrdiff_t src_stride,
1352
                                       uint16_t *dst, ptrdiff_t dst_stride,
1353
                                       const InterpKernel *x_filters, int x0_q4,
1354
                                       int x_step_q4, int w, int h,
1355
13.6k
                                       int round0_bits) {
1356
13.6k
  const int bd = 8;
1357
13.6k
  src -= SUBPEL_TAPS / 2 - 1;
1358
545k
  for (int y = 0; y < h; ++y) {
1359
531k
    int x_q4 = x0_q4;
1360
31.0M
    for (int x = 0; x < w; ++x) {
1361
30.4M
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1362
30.4M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1363
30.4M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1364
30.4M
                           (1 << (bd + FILTER_BITS - 1));
1365
30.4M
      const int sum = horz_scalar_product(src_x, x_filter) + rounding;
1366
30.4M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1367
30.4M
                               WIENER_CLAMP_LIMIT(round0_bits, bd) - 1);
1368
30.4M
      x_q4 += x_step_q4;
1369
30.4M
    }
1370
531k
    src += src_stride;
1371
531k
    dst += dst_stride;
1372
531k
  }
1373
13.6k
}
1374
1375
static void convolve_add_src_vert_hip(const uint16_t *src, ptrdiff_t src_stride,
1376
                                      uint8_t *dst, ptrdiff_t dst_stride,
1377
                                      const InterpKernel *y_filters, int y0_q4,
1378
                                      int y_step_q4, int w, int h,
1379
13.6k
                                      int round1_bits) {
1380
13.6k
  const int bd = 8;
1381
13.6k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1382
1383
732k
  for (int x = 0; x < w; ++x) {
1384
718k
    int y_q4 = y0_q4;
1385
30.0M
    for (int y = 0; y < h; ++y) {
1386
29.3M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1387
29.3M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1388
29.3M
      const int rounding =
1389
29.3M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1390
29.3M
          (1 << (bd + round1_bits - 1));
1391
29.3M
      const int sum =
1392
29.3M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1393
29.3M
      dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, round1_bits));
1394
29.3M
      y_q4 += y_step_q4;
1395
29.3M
    }
1396
718k
    ++src;
1397
718k
    ++dst;
1398
718k
  }
1399
13.6k
}
1400
1401
void av1_wiener_convolve_add_src_c(const uint8_t *src, ptrdiff_t src_stride,
1402
                                   uint8_t *dst, ptrdiff_t dst_stride,
1403
                                   const int16_t *filter_x, int x_step_q4,
1404
                                   const int16_t *filter_y, int y_step_q4,
1405
                                   int w, int h,
1406
13.6k
                                   const WienerConvolveParams *conv_params) {
1407
13.6k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1408
13.6k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1409
1410
13.6k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1411
13.6k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1412
1413
13.6k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1414
13.6k
  const int intermediate_height =
1415
13.6k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS - 1;
1416
13.6k
  memset(temp + (intermediate_height * MAX_SB_SIZE), 0, MAX_SB_SIZE);
1417
1418
13.6k
  assert(w <= MAX_SB_SIZE);
1419
13.6k
  assert(h <= MAX_SB_SIZE);
1420
13.6k
  assert(y_step_q4 <= 32);
1421
13.6k
  assert(x_step_q4 <= 32);
1422
1423
13.6k
  convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1424
13.6k
                             src_stride, temp, MAX_SB_SIZE, filters_x, x0_q4,
1425
13.6k
                             x_step_q4, w, intermediate_height,
1426
13.6k
                             conv_params->round_0);
1427
13.6k
  convolve_add_src_vert_hip(temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1),
1428
13.6k
                            MAX_SB_SIZE, dst, dst_stride, filters_y, y0_q4,
1429
13.6k
                            y_step_q4, w, h, conv_params->round_1);
1430
13.6k
}
1431
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1432
1433
#if CONFIG_AV1_HIGHBITDEPTH
1434
#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1435
static void highbd_convolve_add_src_horiz_hip(
1436
    const uint8_t *src8, ptrdiff_t src_stride, uint16_t *dst,
1437
    ptrdiff_t dst_stride, const InterpKernel *x_filters, int x0_q4,
1438
13.2k
    int x_step_q4, int w, int h, int round0_bits, int bd) {
1439
13.2k
  const int extraprec_clamp_limit = WIENER_CLAMP_LIMIT(round0_bits, bd);
1440
13.2k
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1441
13.2k
  src -= SUBPEL_TAPS / 2 - 1;
1442
653k
  for (int y = 0; y < h; ++y) {
1443
639k
    int x_q4 = x0_q4;
1444
29.6M
    for (int x = 0; x < w; ++x) {
1445
28.9M
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1446
28.9M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1447
28.9M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1448
28.9M
                           (1 << (bd + FILTER_BITS - 1));
1449
28.9M
      const int sum = highbd_horz_scalar_product(src_x, x_filter) + rounding;
1450
28.9M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1451
28.9M
                               extraprec_clamp_limit - 1);
1452
28.9M
      x_q4 += x_step_q4;
1453
28.9M
    }
1454
639k
    src += src_stride;
1455
639k
    dst += dst_stride;
1456
639k
  }
1457
13.2k
}
1458
1459
static void highbd_convolve_add_src_vert_hip(
1460
    const uint16_t *src, ptrdiff_t src_stride, uint8_t *dst8,
1461
    ptrdiff_t dst_stride, const InterpKernel *y_filters, int y0_q4,
1462
13.2k
    int y_step_q4, int w, int h, int round1_bits, int bd) {
1463
13.2k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1464
13.2k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1465
578k
  for (int x = 0; x < w; ++x) {
1466
564k
    int y_q4 = y0_q4;
1467
29.3M
    for (int y = 0; y < h; ++y) {
1468
28.7M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1469
28.7M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1470
28.7M
      const int rounding =
1471
28.7M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1472
28.7M
          (1 << (bd + round1_bits - 1));
1473
28.7M
      const int sum =
1474
28.7M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1475
28.7M
      dst[y * dst_stride] =
1476
28.7M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, round1_bits), bd);
1477
28.7M
      y_q4 += y_step_q4;
1478
28.7M
    }
1479
564k
    ++src;
1480
564k
    ++dst;
1481
564k
  }
1482
13.2k
}
1483
1484
void av1_highbd_wiener_convolve_add_src_c(
1485
    const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
1486
    ptrdiff_t dst_stride, const int16_t *filter_x, int x_step_q4,
1487
    const int16_t *filter_y, int y_step_q4, int w, int h,
1488
13.2k
    const WienerConvolveParams *conv_params, int bd) {
1489
13.2k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1490
13.2k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1491
1492
13.2k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1493
13.2k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1494
1495
13.2k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1496
13.2k
  const int intermediate_height =
1497
13.2k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
1498
1499
13.2k
  assert(w <= MAX_SB_SIZE);
1500
13.2k
  assert(h <= MAX_SB_SIZE);
1501
13.2k
  assert(y_step_q4 <= 32);
1502
13.2k
  assert(x_step_q4 <= 32);
1503
13.2k
  assert(bd + FILTER_BITS - conv_params->round_0 + 2 <= 16);
1504
1505
13.2k
  highbd_convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1506
13.2k
                                    src_stride, temp, MAX_SB_SIZE, filters_x,
1507
13.2k
                                    x0_q4, x_step_q4, w, intermediate_height,
1508
13.2k
                                    conv_params->round_0, bd);
1509
13.2k
  highbd_convolve_add_src_vert_hip(
1510
13.2k
      temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1), MAX_SB_SIZE, dst, dst_stride,
1511
13.2k
      filters_y, y0_q4, y_step_q4, w, h, conv_params->round_1, bd);
1512
13.2k
}
1513
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1514
#endif  // CONFIG_AV1_HIGHBITDEPTH