Coverage Report

Created: 2026-04-01 07:24

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
24.8k
                             int x_step_qn) {
30
24.8k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
31
927k
  for (int y = 0; y < h; ++y) {
32
902k
    int x_qn = x0_qn;
33
295M
    for (int x = 0; x < w; ++x) {
34
294M
      const uint8_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
35
294M
      const int x_filter_idx =
36
294M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
37
294M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
38
294M
      const int16_t *const x_filter =
39
294M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
40
294M
      int sum = 0;
41
2.65G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
42
2.35G
        sum += src_x[k] * x_filter[k];
43
294M
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
44
294M
      x_qn += x_step_qn;
45
294M
    }
46
902k
    src += src_stride;
47
902k
    dst += dst_stride;
48
902k
  }
49
24.8k
}
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
66.8k
                                    int x_step_qn, int bd) {
56
66.8k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
57
2.53M
  for (int y = 0; y < h; ++y) {
58
2.47M
    int x_qn = x0_qn;
59
146M
    for (int x = 0; x < w; ++x) {
60
143M
      const uint16_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
61
143M
      const int x_filter_idx =
62
143M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
63
143M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
64
143M
      const int16_t *const x_filter =
65
143M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
66
143M
      int sum = 0;
67
1.29G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
68
1.15G
        sum += src_x[k] * x_filter[k];
69
143M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
70
143M
      x_qn += x_step_qn;
71
143M
    }
72
2.47M
    src += src_stride;
73
2.47M
    dst += dst_stride;
74
2.47M
  }
75
66.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
8.28k
                          ConvolveParams *conv_params) {
84
8.28k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
85
8.28k
  int im_h = h + filter_params_y->taps - 1;
86
8.28k
  int im_stride = w;
87
8.28k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
88
8.28k
  const int fo_vert = filter_params_y->taps / 2 - 1;
89
8.28k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
90
8.28k
  const int bd = 8;
91
8.28k
  const int bits =
92
8.28k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
93
94
  // horizontal filter
95
8.28k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
96
8.28k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
97
8.28k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
98
115k
  for (int y = 0; y < im_h; ++y) {
99
694k
    for (int x = 0; x < w; ++x) {
100
587k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
101
5.28M
      for (int k = 0; k < filter_params_x->taps; ++k) {
102
4.70M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
103
4.70M
      }
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
587k
      assert(filter_params_x->taps > 8 ||
110
587k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
111
587k
      im_block[y * im_stride + x] =
112
587k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
113
587k
    }
114
106k
  }
115
116
  // vertical filter
117
8.28k
  int16_t *src_vert = im_block + fo_vert * im_stride;
118
8.28k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
119
8.28k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
120
8.28k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
121
57.0k
  for (int y = 0; y < h; ++y) {
122
329k
    for (int x = 0; x < w; ++x) {
123
280k
      int32_t sum = 1 << offset_bits;
124
2.52M
      for (int k = 0; k < filter_params_y->taps; ++k) {
125
2.24M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
126
2.24M
      }
127
280k
      assert(filter_params_y->taps > 8 ||
128
280k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
129
280k
      int16_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
130
280k
                    ((1 << (offset_bits - conv_params->round_1)) +
131
280k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
132
280k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
133
280k
    }
134
48.7k
  }
135
8.28k
}
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.60k
                         const int subpel_y_qn) {
141
4.60k
  const int fo_vert = filter_params_y->taps / 2 - 1;
142
143
  // vertical filter
144
4.60k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
145
4.60k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
146
32.2k
  for (int y = 0; y < h; ++y) {
147
201k
    for (int x = 0; x < w; ++x) {
148
174k
      int32_t res = 0;
149
1.56M
      for (int k = 0; k < filter_params_y->taps; ++k) {
150
1.39M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
151
1.39M
      }
152
174k
      dst[y * dst_stride + x] =
153
174k
          clip_pixel(ROUND_POWER_OF_TWO(res, FILTER_BITS));
154
174k
    }
155
27.6k
  }
156
4.60k
}
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
7.03k
                         const int subpel_x_qn, ConvolveParams *conv_params) {
162
7.03k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
163
7.03k
  const int bits = FILTER_BITS - conv_params->round_0;
164
165
7.03k
  assert(bits >= 0);
166
7.03k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
167
7.03k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
168
169
  // horizontal filter
170
7.03k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
171
7.03k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
172
173
47.3k
  for (int y = 0; y < h; ++y) {
174
275k
    for (int x = 0; x < w; ++x) {
175
234k
      int32_t res = 0;
176
2.11M
      for (int k = 0; k < filter_params_x->taps; ++k) {
177
1.87M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
178
1.87M
      }
179
234k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
180
234k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
181
234k
    }
182
40.3k
  }
183
7.03k
}
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.61k
                                  ConvolveParams *conv_params) {
198
2.61k
  assert(subpel_x_qn == 8);
199
2.61k
  assert(subpel_y_qn == 8);
200
2.61k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
201
2.61k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
202
2.61k
  (void)filter_params_x;
203
2.61k
  (void)subpel_x_qn;
204
2.61k
  (void)filter_params_y;
205
2.61k
  (void)subpel_y_qn;
206
2.61k
  (void)conv_params;
207
208
2.61k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
209
2.61k
  int im_h = h + 1;
210
2.61k
  int im_stride = w;
211
2.61k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
212
2.61k
  const int bd = 8;
213
214
  // horizontal filter
215
  // explicitly operate for subpel_x_qn = 8.
216
2.61k
  int16_t *im = im_block;
217
24.1k
  for (int y = 0; y < im_h; ++y) {
218
230k
    for (int x = 0; x < w; ++x) {
219
209k
      const int32_t sum = (1 << bd) + src[x] + src[x + 1];
220
209k
      assert(0 <= sum && sum < (1 << (bd + 2)));
221
209k
      im[x] = sum;
222
209k
    }
223
21.5k
    src += src_stride;
224
21.5k
    im += im_stride;
225
21.5k
  }
226
227
  // vertical filter
228
  // explicitly operate for subpel_y_qn = 8.
229
2.61k
  int16_t *src_vert = im_block;
230
21.5k
  for (int y = 0; y < h; ++y) {
231
207k
    for (int x = 0; x < w; ++x) {
232
188k
      const int32_t sum =
233
188k
          (1 << (bd + 2)) + src_vert[x] + src_vert[im_stride + x];
234
188k
      assert(0 <= sum && sum < (1 << (bd + 4)));
235
188k
      const int16_t res =
236
188k
          ROUND_POWER_OF_TWO(sum, 2) - ((1 << bd) + (1 << (bd - 1)));
237
188k
      dst[x] = clip_pixel(res);
238
188k
    }
239
18.9k
    src_vert += im_stride;
240
18.9k
    dst += dst_stride;
241
18.9k
  }
242
2.61k
}
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.01k
                                 const int subpel_y_qn) {
250
3.01k
  assert(subpel_y_qn == 8);
251
3.01k
  assert(filter_params_y->taps == 2);
252
3.01k
  (void)filter_params_y;
253
3.01k
  (void)subpel_y_qn;
254
255
  // vertical filter
256
  // explicitly operate for subpel_y_qn = 8.
257
24.3k
  for (int y = 0; y < h; ++y) {
258
284k
    for (int x = 0; x < w; ++x) {
259
262k
      const int32_t res = src[x] + src[src_stride + x];
260
262k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
261
262k
    }
262
21.3k
    src += src_stride;
263
21.3k
    dst += dst_stride;
264
21.3k
  }
265
3.01k
}
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.63k
                                 ConvolveParams *conv_params) {
274
3.63k
  assert(subpel_x_qn == 8);
275
3.63k
  assert(filter_params_x->taps == 2);
276
3.63k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
277
3.63k
  (void)filter_params_x;
278
3.63k
  (void)subpel_x_qn;
279
3.63k
  (void)conv_params;
280
281
  // horizontal filter
282
  // explicitly operate for subpel_x_qn = 8.
283
31.1k
  for (int y = 0; y < h; ++y) {
284
359k
    for (int x = 0; x < w; ++x) {
285
332k
      const int32_t res = src[x] + src[x + 1];
286
332k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
287
332k
    }
288
27.4k
    src += src_stride;
289
27.4k
    dst += dst_stride;
290
27.4k
  }
291
3.63k
}
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.49k
                                ConvolveParams *conv_params) {
299
2.49k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
300
2.49k
  int dst16_stride = conv_params->dst_stride;
301
2.49k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
302
2.49k
  int im_h = h + filter_params_y->taps - 1;
303
2.49k
  int im_stride = w;
304
2.49k
  const int fo_vert = filter_params_y->taps / 2 - 1;
305
2.49k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
306
2.49k
  const int bd = 8;
307
2.49k
  const int round_bits =
308
2.49k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
309
310
  // horizontal filter
311
2.49k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
312
2.49k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
313
2.49k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
314
40.5k
  for (int y = 0; y < im_h; ++y) {
315
337k
    for (int x = 0; x < w; ++x) {
316
299k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
317
2.69M
      for (int k = 0; k < filter_params_x->taps; ++k) {
318
2.39M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
319
2.39M
      }
320
299k
      assert(filter_params_x->taps > 8 ||
321
299k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
322
299k
      im_block[y * im_stride + x] =
323
299k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
324
299k
    }
325
38.0k
  }
326
327
  // vertical filter
328
2.49k
  int16_t *src_vert = im_block + fo_vert * im_stride;
329
2.49k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
330
2.49k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
331
2.49k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
332
23.0k
  for (int y = 0; y < h; ++y) {
333
183k
    for (int x = 0; x < w; ++x) {
334
163k
      int32_t sum = 1 << offset_bits;
335
1.46M
      for (int k = 0; k < filter_params_y->taps; ++k) {
336
1.30M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
337
1.30M
      }
338
163k
      assert(filter_params_y->taps > 8 ||
339
163k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
340
163k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
341
163k
      if (conv_params->do_average) {
342
105k
        int32_t tmp = dst16[y * dst16_stride + x];
343
105k
        if (conv_params->use_dist_wtd_comp_avg) {
344
42.8k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
345
42.8k
          tmp = tmp >> DIST_PRECISION_BITS;
346
62.3k
        } else {
347
62.3k
          tmp += res;
348
62.3k
          tmp = tmp >> 1;
349
62.3k
        }
350
105k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
351
105k
               (1 << (offset_bits - conv_params->round_1 - 1));
352
105k
        dst[y * dst_stride + x] =
353
105k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
354
105k
      } else {
355
57.8k
        dst16[y * dst16_stride + x] = res;
356
57.8k
      }
357
163k
    }
358
20.5k
  }
359
2.49k
}
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.67k
                               ConvolveParams *conv_params) {
366
2.67k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
367
2.67k
  int dst16_stride = conv_params->dst_stride;
368
2.67k
  const int fo_vert = filter_params_y->taps / 2 - 1;
369
2.67k
  const int bits = FILTER_BITS - conv_params->round_0;
370
2.67k
  const int bd = 8;
371
2.67k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
372
2.67k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
373
2.67k
                           (1 << (offset_bits - conv_params->round_1 - 1));
374
2.67k
  const int round_bits =
375
2.67k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
376
377
  // vertical filter
378
2.67k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
379
2.67k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
380
27.2k
  for (int y = 0; y < h; ++y) {
381
220k
    for (int x = 0; x < w; ++x) {
382
196k
      int32_t res = 0;
383
1.76M
      for (int k = 0; k < filter_params_y->taps; ++k) {
384
1.57M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
385
1.57M
      }
386
196k
      res *= (1 << bits);
387
196k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
388
389
196k
      if (conv_params->do_average) {
390
77.7k
        int32_t tmp = dst16[y * dst16_stride + x];
391
77.7k
        if (conv_params->use_dist_wtd_comp_avg) {
392
3.77k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
393
3.77k
          tmp = tmp >> DIST_PRECISION_BITS;
394
73.9k
        } else {
395
73.9k
          tmp += res;
396
73.9k
          tmp = tmp >> 1;
397
73.9k
        }
398
77.7k
        tmp -= round_offset;
399
77.7k
        dst[y * dst_stride + x] =
400
77.7k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
401
118k
      } else {
402
118k
        dst16[y * dst16_stride + x] = res;
403
118k
      }
404
196k
    }
405
24.5k
  }
406
2.67k
}
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.1k
  for (int y = 0; y < h; ++y) {
428
115k
    for (int x = 0; x < w; ++x) {
429
104k
      int32_t res = 0;
430
938k
      for (int k = 0; k < filter_params_x->taps; ++k) {
431
834k
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
432
834k
      }
433
104k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
434
104k
      res += round_offset;
435
436
104k
      if (conv_params->do_average) {
437
22.0k
        int32_t tmp = dst16[y * dst16_stride + x];
438
22.0k
        if (conv_params->use_dist_wtd_comp_avg) {
439
9.47k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
440
9.47k
          tmp = tmp >> DIST_PRECISION_BITS;
441
12.5k
        } else {
442
12.5k
          tmp += res;
443
12.5k
          tmp = tmp >> 1;
444
12.5k
        }
445
22.0k
        tmp -= round_offset;
446
22.0k
        dst[y * dst_stride + x] =
447
22.0k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
448
82.2k
      } else {
449
82.2k
        dst16[y * dst16_stride + x] = res;
450
82.2k
      }
451
104k
    }
452
10.9k
  }
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
4.96k
                                     ConvolveParams *conv_params) {
458
4.96k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
459
4.96k
  int dst16_stride = conv_params->dst_stride;
460
4.96k
  const int bits =
461
4.96k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
462
4.96k
  const int bd = 8;
463
4.96k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
464
4.96k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
465
4.96k
                           (1 << (offset_bits - conv_params->round_1 - 1));
466
467
51.4k
  for (int y = 0; y < h; ++y) {
468
509k
    for (int x = 0; x < w; ++x) {
469
462k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
470
462k
      res += round_offset;
471
472
462k
      if (conv_params->do_average) {
473
86.5k
        int32_t tmp = dst16[y * dst16_stride + x];
474
86.5k
        if (conv_params->use_dist_wtd_comp_avg) {
475
8.76k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
476
8.76k
          tmp = tmp >> DIST_PRECISION_BITS;
477
77.7k
        } else {
478
77.7k
          tmp += res;
479
77.7k
          tmp = tmp >> 1;
480
77.7k
        }
481
86.5k
        tmp -= round_offset;
482
86.5k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
483
376k
      } else {
484
376k
        dst16[y * dst16_stride + x] = res;
485
376k
      }
486
462k
    }
487
46.5k
  }
488
4.96k
}
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.73k
                             ConvolveParams *conv_params) {
497
9.73k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
498
9.73k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
499
9.73k
             filter_params_y->taps;
500
9.73k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
501
9.73k
  const int dst16_stride = conv_params->dst_stride;
502
9.73k
  const int bits =
503
9.73k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
504
9.73k
  assert(bits >= 0);
505
9.73k
  int im_stride = w;
506
9.73k
  const int fo_vert = filter_params_y->taps / 2 - 1;
507
9.73k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
508
9.73k
  const int bd = 8;
509
510
  // horizontal filter
511
9.73k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
512
143k
  for (int y = 0; y < im_h; ++y) {
513
133k
    int x_qn = subpel_x_qn;
514
1.26M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
515
1.13M
      const uint8_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
516
1.13M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
517
1.13M
      assert(x_filter_idx < SUBPEL_SHIFTS);
518
1.13M
      const int16_t *x_filter =
519
1.13M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
520
1.13M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
521
10.2M
      for (int k = 0; k < filter_params_x->taps; ++k) {
522
9.07M
        sum += x_filter[k] * src_x[k - fo_horiz];
523
9.07M
      }
524
1.13M
      assert(filter_params_x->taps > 8 ||
525
1.13M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
526
1.13M
      im_block[y * im_stride + x] =
527
1.13M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
528
1.13M
    }
529
133k
    src_horiz += src_stride;
530
133k
  }
531
532
  // vertical filter
533
9.73k
  int16_t *src_vert = im_block + fo_vert * im_stride;
534
9.73k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
535
69.7k
  for (int x = 0; x < w; ++x) {
536
60.0k
    int y_qn = subpel_y_qn;
537
776k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
538
716k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
539
716k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
540
716k
      assert(y_filter_idx < SUBPEL_SHIFTS);
541
716k
      const int16_t *y_filter =
542
716k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
543
716k
      int32_t sum = 1 << offset_bits;
544
6.44M
      for (int k = 0; k < filter_params_y->taps; ++k) {
545
5.73M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
546
5.73M
      }
547
716k
      assert(filter_params_y->taps > 8 ||
548
716k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
549
716k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
550
716k
      if (conv_params->is_compound) {
551
202k
        if (conv_params->do_average) {
552
162k
          int32_t tmp = dst16[y * dst16_stride + x];
553
162k
          if (conv_params->use_dist_wtd_comp_avg) {
554
114k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
555
114k
            tmp = tmp >> DIST_PRECISION_BITS;
556
114k
          } else {
557
48.7k
            tmp += res;
558
48.7k
            tmp = tmp >> 1;
559
48.7k
          }
560
          /* Subtract round offset and convolve round */
561
162k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
562
162k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
563
162k
          dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
564
162k
        } else {
565
40.0k
          dst16[y * dst16_stride + x] = res;
566
40.0k
        }
567
513k
      } else {
568
        /* Subtract round offset and convolve round */
569
513k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
570
513k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
571
513k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
572
513k
      }
573
716k
    }
574
60.0k
    src_vert++;
575
60.0k
  }
576
9.73k
}
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.73k
    ConvolveParams *conv_params) {
584
9.73k
  if (conv_params->is_compound) {
585
2.07k
    assert(conv_params->dst != NULL);
586
2.07k
  }
587
9.73k
  av1_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h, filter_params_x,
588
9.73k
                        filter_params_y, subpel_x_qn, x_step_qn, subpel_y_qn,
589
9.73k
                        y_step_qn, conv_params);
590
9.73k
}
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
11.3k
    const int subpel_y_qn, ConvolveParams *conv_params) {
597
11.3k
  const bool need_x = subpel_x_qn != 0;
598
11.3k
  const bool need_y = subpel_y_qn != 0;
599
11.3k
  if (!need_x && !need_y) {
600
4.96k
    av1_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
601
4.96k
                                  conv_params);
602
6.40k
  } 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.16k
  } else if (!need_x && need_y) {
606
2.67k
    av1_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
607
2.67k
                            filter_params_y, subpel_y_qn, conv_params);
608
2.67k
  } else {
609
2.49k
    assert(need_y && need_x);
610
2.49k
    av1_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
611
2.49k
                             filter_params_x, filter_params_y, subpel_x_qn,
612
2.49k
                             subpel_y_qn, conv_params);
613
2.49k
  }
614
11.3k
}
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
176k
    const int subpel_y_qn, ConvolveParams *conv_params) {
621
176k
  const bool need_x = subpel_x_qn != 0;
622
176k
  const bool need_y = subpel_y_qn != 0;
623
176k
  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
7.03k
    av1_convolve_x_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
627
7.03k
                      subpel_x_qn, conv_params);
628
12.8k
  } else if (!need_x && need_y) {
629
4.60k
    av1_convolve_y_sr(src, src_stride, dst, dst_stride, w, h, filter_params_y,
630
4.60k
                      subpel_y_qn);
631
8.28k
  } else {
632
8.28k
    assert(need_x && need_y);
633
8.28k
    av1_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
634
8.28k
                       filter_params_y, subpel_x_qn, subpel_y_qn, conv_params);
635
8.28k
  }
636
176k
}
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
206k
                            ConvolveParams *conv_params) {
644
206k
  (void)x_step_q4;
645
206k
  (void)y_step_q4;
646
206k
  (void)dst;
647
206k
  (void)dst_stride;
648
649
206k
  const InterpFilterParams *filter_params_x = interp_filters[0];
650
206k
  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
206k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
655
149k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
656
149k
    assert(!scaled);
657
149k
    if (subpel_x_qn && subpel_y_qn) {
658
2.61k
      av1_convolve_2d_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
659
2.61k
                                 filter_params_x, filter_params_y, subpel_x_qn,
660
2.61k
                                 subpel_y_qn, conv_params);
661
2.61k
      return;
662
147k
    } else if (subpel_x_qn) {
663
3.63k
      av1_convolve_x_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
664
3.63k
                                filter_params_x, subpel_x_qn, conv_params);
665
3.63k
      return;
666
143k
    } else if (subpel_y_qn) {
667
3.01k
      av1_convolve_y_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
668
3.01k
                                filter_params_y, subpel_y_qn);
669
3.01k
      return;
670
3.01k
    }
671
149k
  }
672
673
197k
  if (scaled) {
674
9.73k
    convolve_2d_scale_wrapper(src, src_stride, dst, dst_stride, w, h,
675
9.73k
                              filter_params_x, filter_params_y, subpel_x_qn,
676
9.73k
                              x_step_q4, subpel_y_qn, y_step_q4, conv_params);
677
187k
  } else if (conv_params->is_compound) {
678
11.3k
    convolve_2d_facade_compound(src, src_stride, dst, dst_stride, w, h,
679
11.3k
                                filter_params_x, filter_params_y, subpel_x_qn,
680
11.3k
                                subpel_y_qn, conv_params);
681
176k
  } else {
682
176k
    convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
683
176k
                              filter_params_x, filter_params_y, subpel_x_qn,
684
176k
                              subpel_y_qn, conv_params);
685
176k
  }
686
197k
}
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
7.66k
                                ConvolveParams *conv_params, int bd) {
694
7.66k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
695
7.66k
  const int bits = FILTER_BITS - conv_params->round_0;
696
697
7.66k
  assert(bits >= 0);
698
7.66k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
699
7.66k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
700
701
  // horizontal filter
702
7.66k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
703
7.66k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
704
54.3k
  for (int y = 0; y < h; ++y) {
705
326k
    for (int x = 0; x < w; ++x) {
706
279k
      int32_t res = 0;
707
2.51M
      for (int k = 0; k < filter_params_x->taps; ++k) {
708
2.23M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
709
2.23M
      }
710
279k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
711
279k
      dst[y * dst_stride + x] =
712
279k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
713
279k
    }
714
46.6k
  }
715
7.66k
}
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.80k
                                const int subpel_y_qn, int bd) {
721
4.80k
  const int fo_vert = filter_params_y->taps / 2 - 1;
722
  // vertical filter
723
4.80k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
724
4.80k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
725
39.1k
  for (int y = 0; y < h; ++y) {
726
283k
    for (int x = 0; x < w; ++x) {
727
249k
      int32_t res = 0;
728
2.24M
      for (int k = 0; k < filter_params_y->taps; ++k) {
729
1.99M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
730
1.99M
      }
731
249k
      dst[y * dst_stride + x] =
732
249k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, FILTER_BITS), bd);
733
249k
    }
734
34.3k
  }
735
4.80k
}
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.18k
                                 ConvolveParams *conv_params, int bd) {
743
8.18k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
744
8.18k
  int im_h = h + filter_params_y->taps - 1;
745
8.18k
  int im_stride = w;
746
8.18k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
747
8.18k
  const int fo_vert = filter_params_y->taps / 2 - 1;
748
8.18k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
749
8.18k
  const int bits =
750
8.18k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
751
8.18k
  assert(bits >= 0);
752
753
  // horizontal filter
754
8.18k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
755
8.18k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
756
8.18k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
757
115k
  for (int y = 0; y < im_h; ++y) {
758
730k
    for (int x = 0; x < w; ++x) {
759
622k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
760
5.60M
      for (int k = 0; k < filter_params_x->taps; ++k) {
761
4.97M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
762
4.97M
      }
763
622k
      assert(filter_params_x->taps > 8 ||
764
622k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
765
622k
      im_block[y * im_stride + x] =
766
622k
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
767
622k
    }
768
107k
  }
769
770
  // vertical filter
771
8.18k
  int16_t *src_vert = im_block + fo_vert * im_stride;
772
8.18k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
773
8.18k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
774
8.18k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
775
58.6k
  for (int y = 0; y < h; ++y) {
776
355k
    for (int x = 0; x < w; ++x) {
777
304k
      int32_t sum = 1 << offset_bits;
778
2.74M
      for (int k = 0; k < filter_params_y->taps; ++k) {
779
2.43M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
780
2.43M
      }
781
304k
      assert(filter_params_y->taps > 8 ||
782
304k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
783
304k
      int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
784
304k
                    ((1 << (offset_bits - conv_params->round_1)) +
785
304k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
786
304k
      dst[y * dst_stride + x] =
787
304k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
788
304k
    }
789
50.5k
  }
790
8.18k
}
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
22.0k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
804
22.0k
  const int bits =
805
22.0k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
806
22.0k
  assert(bits >= 0);
807
22.0k
  assert(subpel_x_qn == 8);
808
22.0k
  assert(subpel_y_qn == 8);
809
22.0k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
810
22.0k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
811
22.0k
  (void)filter_params_x;
812
22.0k
  (void)subpel_x_qn;
813
22.0k
  (void)filter_params_y;
814
22.0k
  (void)subpel_y_qn;
815
22.0k
  (void)conv_params;
816
817
22.0k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
818
22.0k
  int im_h = h + 1;
819
22.0k
  int im_stride = w;
820
22.0k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
821
822
  // horizontal filter
823
  // explicitly operate for subpel_x_qn = 8.
824
22.0k
  int16_t *im = im_block;
825
187k
  for (int y = 0; y < im_h; ++y) {
826
1.62M
    for (int x = 0; x < w; ++x) {
827
1.46M
      int32_t sum = (1 << (bd + FILTER_BITS - 1)) + 64 * (src[x] + src[x + 1]);
828
1.46M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
829
1.46M
      sum = ROUND_POWER_OF_TWO(sum, conv_params->round_0);
830
1.46M
      im[x] = sum;
831
1.46M
    }
832
165k
    src += src_stride;
833
165k
    im += im_stride;
834
165k
  }
835
836
  // vertical filter
837
  // explicitly operate for subpel_y_qn = 8.
838
22.0k
  int16_t *src_vert = im_block;
839
22.0k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
840
165k
  for (int y = 0; y < h; ++y) {
841
1.44M
    for (int x = 0; x < w; ++x) {
842
1.29M
      const int32_t sum =
843
1.29M
          (1 << offset_bits) + 64 * (src_vert[x] + src_vert[im_stride + x]);
844
1.29M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
845
1.29M
      const int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
846
1.29M
                          ((1 << (offset_bits - conv_params->round_1)) +
847
1.29M
                           (1 << (offset_bits - conv_params->round_1 - 1)));
848
849
1.29M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
850
1.29M
    }
851
143k
    src_vert += im_stride;
852
143k
    dst += dst_stride;
853
143k
  }
854
22.0k
}
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
19.7k
    int bd) {
862
19.7k
  assert(subpel_y_qn == 8);
863
19.7k
  assert(filter_params_y->taps == 2);
864
19.7k
  (void)filter_params_y;
865
19.7k
  (void)subpel_y_qn;
866
867
  // vertical filter
868
  // explicitly operate for subpel_y_qn = 8.
869
144k
  for (int y = 0; y < h; ++y) {
870
1.16M
    for (int x = 0; x < w; ++x) {
871
1.03M
      const int32_t res = src[x] + src[src_stride + x];
872
1.03M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, 1), bd);
873
1.03M
    }
874
124k
    src += src_stride;
875
124k
    dst += dst_stride;
876
124k
  }
877
19.7k
}
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
20.3k
    ConvolveParams *conv_params, int bd) {
885
20.3k
  const int bits = FILTER_BITS - conv_params->round_0;
886
20.3k
  assert(bits >= 0);
887
20.3k
  assert(subpel_x_qn == 8);
888
20.3k
  assert(filter_params_x->taps == 2);
889
20.3k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
890
20.3k
  (void)filter_params_x;
891
20.3k
  (void)subpel_x_qn;
892
893
  // horizontal filter
894
  // explicitly operate for subpel_x_qn = 8.
895
152k
  for (int y = 0; y < h; ++y) {
896
1.48M
    for (int x = 0; x < w; ++x) {
897
1.35M
      int32_t res = 64 * (src[x] + src[x + 1]);
898
1.35M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
899
1.35M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
900
1.35M
    }
901
132k
    src += src_stride;
902
132k
    dst += dst_stride;
903
132k
  }
904
20.3k
}
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.33k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
911
4.33k
  int x, y, k;
912
4.33k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
913
4.33k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
914
4.33k
  int dst16_stride = conv_params->dst_stride;
915
4.33k
  int im_h = h + filter_params_y->taps - 1;
916
4.33k
  int im_stride = w;
917
4.33k
  const int fo_vert = filter_params_y->taps / 2 - 1;
918
4.33k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
919
4.33k
  const int round_bits =
920
4.33k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
921
4.33k
  assert(round_bits >= 0);
922
923
  // horizontal filter
924
4.33k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
925
4.33k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
926
4.33k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
927
75.5k
  for (y = 0; y < im_h; ++y) {
928
669k
    for (x = 0; x < w; ++x) {
929
598k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
930
5.38M
      for (k = 0; k < filter_params_x->taps; ++k) {
931
4.78M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
932
4.78M
      }
933
598k
      assert(filter_params_x->taps > 8 ||
934
598k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
935
598k
      (void)bd;
936
598k
      im_block[y * im_stride + x] =
937
598k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
938
598k
    }
939
71.2k
  }
940
941
  // vertical filter
942
4.33k
  int16_t *src_vert = im_block + fo_vert * im_stride;
943
4.33k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
944
4.33k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
945
4.33k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
946
45.2k
  for (y = 0; y < h; ++y) {
947
392k
    for (x = 0; x < w; ++x) {
948
351k
      int32_t sum = 1 << offset_bits;
949
3.16M
      for (k = 0; k < filter_params_y->taps; ++k) {
950
2.81M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
951
2.81M
      }
952
351k
      assert(filter_params_y->taps > 8 ||
953
351k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
954
351k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
955
351k
      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
10.4k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
959
10.4k
          tmp = tmp >> DIST_PRECISION_BITS;
960
106k
        } else {
961
106k
          tmp += res;
962
106k
          tmp = tmp >> 1;
963
106k
        }
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
234k
      } else {
969
234k
        dst16[y * dst16_stride + x] = res;
970
234k
      }
971
351k
    }
972
40.9k
  }
973
4.33k
}
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
2.76k
                                      ConvolveParams *conv_params, int bd) {
981
2.76k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
982
2.76k
  int dst16_stride = conv_params->dst_stride;
983
2.76k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
984
2.76k
  const int bits = FILTER_BITS - conv_params->round_1;
985
2.76k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
986
2.76k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
987
2.76k
                           (1 << (offset_bits - conv_params->round_1 - 1));
988
2.76k
  const int round_bits =
989
2.76k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
990
2.76k
  assert(round_bits >= 0);
991
2.76k
  assert(bits >= 0);
992
  // horizontal filter
993
2.76k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
994
2.76k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
995
33.4k
  for (int y = 0; y < h; ++y) {
996
330k
    for (int x = 0; x < w; ++x) {
997
299k
      int32_t res = 0;
998
2.69M
      for (int k = 0; k < filter_params_x->taps; ++k) {
999
2.39M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
1000
2.39M
      }
1001
299k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
1002
299k
      res += round_offset;
1003
1004
299k
      if (conv_params->do_average) {
1005
189k
        int32_t tmp = dst16[y * dst16_stride + x];
1006
189k
        if (conv_params->use_dist_wtd_comp_avg) {
1007
7.16k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1008
7.16k
          tmp = tmp >> DIST_PRECISION_BITS;
1009
181k
        } else {
1010
181k
          tmp += res;
1011
181k
          tmp = tmp >> 1;
1012
181k
        }
1013
189k
        tmp -= round_offset;
1014
189k
        dst[y * dst_stride + x] =
1015
189k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1016
189k
      } else {
1017
110k
        dst16[y * dst16_stride + x] = res;
1018
110k
      }
1019
299k
    }
1020
30.6k
  }
1021
2.76k
}
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
2.69k
                                      ConvolveParams *conv_params, int bd) {
1029
2.69k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1030
2.69k
  int dst16_stride = conv_params->dst_stride;
1031
2.69k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1032
2.69k
  const int bits = FILTER_BITS - conv_params->round_0;
1033
2.69k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1034
2.69k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1035
2.69k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1036
2.69k
  const int round_bits =
1037
2.69k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
1038
2.69k
  assert(round_bits >= 0);
1039
2.69k
  assert(bits >= 0);
1040
  // vertical filter
1041
2.69k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
1042
2.69k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
1043
31.6k
  for (int y = 0; y < h; ++y) {
1044
334k
    for (int x = 0; x < w; ++x) {
1045
305k
      int32_t res = 0;
1046
2.75M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1047
2.44M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
1048
2.44M
      }
1049
305k
      res *= (1 << bits);
1050
305k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
1051
1052
305k
      if (conv_params->do_average) {
1053
87.9k
        int32_t tmp = dst16[y * dst16_stride + x];
1054
87.9k
        if (conv_params->use_dist_wtd_comp_avg) {
1055
9.79k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1056
9.79k
          tmp = tmp >> DIST_PRECISION_BITS;
1057
78.1k
        } else {
1058
78.1k
          tmp += res;
1059
78.1k
          tmp = tmp >> 1;
1060
78.1k
        }
1061
87.9k
        tmp -= round_offset;
1062
87.9k
        dst[y * dst_stride + x] =
1063
87.9k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1064
217k
      } else {
1065
217k
        dst16[y * dst16_stride + x] = res;
1066
217k
      }
1067
305k
    }
1068
28.9k
  }
1069
2.69k
}
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
10.9k
                                            int bd) {
1076
10.9k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1077
10.9k
  int dst16_stride = conv_params->dst_stride;
1078
10.9k
  const int bits =
1079
10.9k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
1080
10.9k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1081
10.9k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1082
10.9k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1083
10.9k
  assert(bits >= 0);
1084
1085
108k
  for (int y = 0; y < h; ++y) {
1086
1.00M
    for (int x = 0; x < w; ++x) {
1087
908k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
1088
908k
      res += round_offset;
1089
908k
      if (conv_params->do_average) {
1090
253k
        int32_t tmp = dst16[y * dst16_stride + x];
1091
253k
        if (conv_params->use_dist_wtd_comp_avg) {
1092
157k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1093
157k
          tmp = tmp >> DIST_PRECISION_BITS;
1094
157k
        } else {
1095
95.8k
          tmp += res;
1096
95.8k
          tmp = tmp >> 1;
1097
95.8k
        }
1098
253k
        tmp -= round_offset;
1099
253k
        dst[y * dst_stride + x] =
1100
253k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1101
655k
      } else {
1102
655k
        dst16[y * dst16_stride + x] = res;
1103
655k
      }
1104
908k
    }
1105
97.8k
  }
1106
10.9k
}
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.31k
                                    ConvolveParams *conv_params, int bd) {
1115
7.31k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
1116
7.31k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
1117
7.31k
             filter_params_y->taps;
1118
7.31k
  int im_stride = w;
1119
7.31k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1120
7.31k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
1121
7.31k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1122
7.31k
  const int dst16_stride = conv_params->dst_stride;
1123
7.31k
  const int bits =
1124
7.31k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
1125
7.31k
  assert(bits >= 0);
1126
  // horizontal filter
1127
7.31k
  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.50M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
1131
1.39M
      const uint16_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
1132
1.39M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1133
1.39M
      assert(x_filter_idx < SUBPEL_SHIFTS);
1134
1.39M
      const int16_t *x_filter =
1135
1.39M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
1136
1.39M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
1137
12.5M
      for (int k = 0; k < filter_params_x->taps; ++k) {
1138
11.1M
        sum += x_filter[k] * src_x[k - fo_horiz];
1139
11.1M
      }
1140
1.39M
      assert(filter_params_x->taps > 8 ||
1141
1.39M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
1142
1.39M
      im_block[y * im_stride + x] =
1143
1.39M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
1144
1.39M
    }
1145
107k
    src_horiz += src_stride;
1146
107k
  }
1147
1148
  // vertical filter
1149
7.31k
  int16_t *src_vert = im_block + fo_vert * im_stride;
1150
7.31k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1151
64.9k
  for (int x = 0; x < w; ++x) {
1152
57.6k
    int y_qn = subpel_y_qn;
1153
1.06M
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
1154
1.00M
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
1155
1.00M
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1156
1.00M
      assert(y_filter_idx < SUBPEL_SHIFTS);
1157
1.00M
      const int16_t *y_filter =
1158
1.00M
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
1159
1.00M
      int32_t sum = 1 << offset_bits;
1160
9.04M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1161
8.04M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
1162
8.04M
      }
1163
1.00M
      assert(filter_params_y->taps > 8 ||
1164
1.00M
             (0 <= sum && sum < (1 << (offset_bits + 2))));
1165
1.00M
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
1166
1.00M
      if (conv_params->is_compound) {
1167
197k
        if (conv_params->do_average) {
1168
76.9k
          int32_t tmp = dst16[y * dst16_stride + x];
1169
76.9k
          if (conv_params->use_dist_wtd_comp_avg) {
1170
47.2k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1171
47.2k
            tmp = tmp >> DIST_PRECISION_BITS;
1172
47.2k
          } else {
1173
29.7k
            tmp += res;
1174
29.7k
            tmp = tmp >> 1;
1175
29.7k
          }
1176
          /* Subtract round offset and convolve round */
1177
76.9k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
1178
76.9k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
1179
76.9k
          dst[y * dst_stride + x] =
1180
76.9k
              clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1181
120k
        } else {
1182
120k
          dst16[y * dst16_stride + x] = res;
1183
120k
        }
1184
807k
      } else {
1185
        /* Subtract round offset and convolve round */
1186
807k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
1187
807k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
1188
807k
        dst[y * dst_stride + x] =
1189
807k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1190
807k
      }
1191
1.00M
    }
1192
57.6k
    src_vert++;
1193
57.6k
  }
1194
7.31k
}
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
20.7k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1201
20.7k
  const bool need_x = subpel_x_qn != 0;
1202
20.7k
  const bool need_y = subpel_y_qn != 0;
1203
20.7k
  if (!need_x && !need_y) {
1204
10.9k
    av1_highbd_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
1205
10.9k
                                         conv_params, bd);
1206
10.9k
  } else if (need_x && !need_y) {
1207
2.76k
    av1_highbd_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
1208
2.76k
                                   filter_params_x, subpel_x_qn, conv_params,
1209
2.76k
                                   bd);
1210
7.03k
  } else if (!need_x && need_y) {
1211
2.69k
    av1_highbd_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
1212
2.69k
                                   filter_params_y, subpel_y_qn, conv_params,
1213
2.69k
                                   bd);
1214
4.33k
  } else {
1215
4.33k
    assert(need_x && need_y);
1216
4.33k
    av1_highbd_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
1217
4.33k
                                    filter_params_x, filter_params_y,
1218
4.33k
                                    subpel_x_qn, subpel_y_qn, conv_params, bd);
1219
4.33k
  }
1220
20.7k
}
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
114k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1227
114k
  const bool need_x = subpel_x_qn != 0;
1228
114k
  const bool need_y = subpel_y_qn != 0;
1229
1230
114k
  if (!need_x && !need_y) {
1231
93.7k
    aom_highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h);
1232
93.7k
  } else if (need_x && !need_y) {
1233
7.66k
    av1_highbd_convolve_x_sr(src, src_stride, dst, dst_stride, w, h,
1234
7.66k
                             filter_params_x, subpel_x_qn, conv_params, bd);
1235
12.9k
  } else if (!need_x && need_y) {
1236
4.80k
    av1_highbd_convolve_y_sr(src, src_stride, dst, dst_stride, w, h,
1237
4.80k
                             filter_params_y, subpel_y_qn, bd);
1238
8.18k
  } else {
1239
8.18k
    assert(need_x && need_y);
1240
8.18k
    av1_highbd_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h,
1241
8.18k
                              filter_params_x, filter_params_y, subpel_x_qn,
1242
8.18k
                              subpel_y_qn, conv_params, bd);
1243
8.18k
  }
1244
114k
}
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
204k
                                   int bd) {
1253
204k
  (void)x_step_q4;
1254
204k
  (void)y_step_q4;
1255
204k
  (void)dst_stride;
1256
204k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1257
1258
204k
  const InterpFilterParams *filter_params_x = interp_filters[0];
1259
204k
  const InterpFilterParams *filter_params_y = interp_filters[1];
1260
1261
204k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1262
  // 2-tap filter indicates that it is for IntraBC.
1263
204k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
1264
140k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
1265
140k
    assert(!scaled);
1266
140k
    if (subpel_x_qn && subpel_y_qn) {
1267
22.0k
      av1_highbd_convolve_2d_sr_intrabc_c(
1268
22.0k
          src, src_stride, dst, dst_stride, w, h, filter_params_x,
1269
22.0k
          filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1270
22.0k
      return;
1271
118k
    } else if (subpel_x_qn) {
1272
20.3k
      av1_highbd_convolve_x_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1273
20.3k
                                         filter_params_x, subpel_x_qn,
1274
20.3k
                                         conv_params, bd);
1275
20.3k
      return;
1276
97.7k
    } else if (subpel_y_qn) {
1277
19.7k
      av1_highbd_convolve_y_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1278
19.7k
                                         filter_params_y, subpel_y_qn, bd);
1279
19.7k
      return;
1280
19.7k
    }
1281
140k
  }
1282
1283
142k
  if (scaled) {
1284
7.31k
    if (conv_params->is_compound) {
1285
1.33k
      assert(conv_params->dst != NULL);
1286
1.33k
    }
1287
7.31k
    av1_highbd_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h,
1288
7.31k
                                 filter_params_x, filter_params_y, subpel_x_qn,
1289
7.31k
                                 x_step_q4, subpel_y_qn, y_step_q4, conv_params,
1290
7.31k
                                 bd);
1291
135k
  } else if (conv_params->is_compound) {
1292
20.7k
    highbd_convolve_2d_facade_compound(
1293
20.7k
        src, src_stride, dst, dst_stride, w, h, filter_params_x,
1294
20.7k
        filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1295
114k
  } else {
1296
114k
    highbd_convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
1297
114k
                                     filter_params_x, filter_params_y,
1298
114k
                                     subpel_x_qn, subpel_y_qn, conv_params, bd);
1299
114k
  }
1300
142k
}
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
24.5M
static inline int horz_scalar_product(const uint8_t *a, const int16_t *b) {
1319
24.5M
  int sum = 0;
1320
220M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1321
24.5M
  return sum;
1322
24.5M
}
1323
1324
#if CONFIG_AV1_HIGHBITDEPTH
1325
static inline int highbd_horz_scalar_product(const uint16_t *a,
1326
29.1M
                                             const int16_t *b) {
1327
29.1M
  int sum = 0;
1328
262M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1329
29.1M
  return sum;
1330
29.1M
}
1331
#endif
1332
1333
static inline int highbd_vert_scalar_product(const uint16_t *a,
1334
                                             ptrdiff_t a_stride,
1335
52.7M
                                             const int16_t *b) {
1336
52.7M
  int sum = 0;
1337
477M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
1338
52.7M
  return sum;
1339
52.7M
}
1340
1341
55.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
55.9k
  return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
1345
55.9k
}
1346
1347
55.9k
static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
1348
55.9k
  return (int)((const InterpKernel *)(intptr_t)f - base);
1349
55.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.4k
                                       int round0_bits) {
1356
13.4k
  const int bd = 8;
1357
13.4k
  src -= SUBPEL_TAPS / 2 - 1;
1358
448k
  for (int y = 0; y < h; ++y) {
1359
435k
    int x_q4 = x0_q4;
1360
24.9M
    for (int x = 0; x < w; ++x) {
1361
24.5M
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1362
24.5M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1363
24.5M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1364
24.5M
                           (1 << (bd + FILTER_BITS - 1));
1365
24.5M
      const int sum = horz_scalar_product(src_x, x_filter) + rounding;
1366
24.5M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1367
24.5M
                               WIENER_CLAMP_LIMIT(round0_bits, bd) - 1);
1368
24.5M
      x_q4 += x_step_q4;
1369
24.5M
    }
1370
435k
    src += src_stride;
1371
435k
    dst += dst_stride;
1372
435k
  }
1373
13.4k
}
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.4k
                                      int round1_bits) {
1380
13.4k
  const int bd = 8;
1381
13.4k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1382
1383
678k
  for (int x = 0; x < w; ++x) {
1384
665k
    int y_q4 = y0_q4;
1385
27.3M
    for (int y = 0; y < h; ++y) {
1386
26.7M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1387
26.7M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1388
26.7M
      const int rounding =
1389
26.7M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1390
26.7M
          (1 << (bd + round1_bits - 1));
1391
26.7M
      const int sum =
1392
26.7M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1393
26.7M
      dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, round1_bits));
1394
26.7M
      y_q4 += y_step_q4;
1395
26.7M
    }
1396
665k
    ++src;
1397
665k
    ++dst;
1398
665k
  }
1399
13.4k
}
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.4k
                                   const WienerConvolveParams *conv_params) {
1407
13.4k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1408
13.4k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1409
1410
13.4k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1411
13.4k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1412
1413
13.4k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1414
13.4k
  const int intermediate_height =
1415
13.4k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS - 1;
1416
13.4k
  memset(temp + (intermediate_height * MAX_SB_SIZE), 0, MAX_SB_SIZE);
1417
1418
13.4k
  assert(w <= MAX_SB_SIZE);
1419
13.4k
  assert(h <= MAX_SB_SIZE);
1420
13.4k
  assert(y_step_q4 <= 32);
1421
13.4k
  assert(x_step_q4 <= 32);
1422
1423
13.4k
  convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1424
13.4k
                             src_stride, temp, MAX_SB_SIZE, filters_x, x0_q4,
1425
13.4k
                             x_step_q4, w, intermediate_height,
1426
13.4k
                             conv_params->round_0);
1427
13.4k
  convolve_add_src_vert_hip(temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1),
1428
13.4k
                            MAX_SB_SIZE, dst, dst_stride, filters_y, y0_q4,
1429
13.4k
                            y_step_q4, w, h, conv_params->round_1);
1430
13.4k
}
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
14.4k
    int x_step_q4, int w, int h, int round0_bits, int bd) {
1439
14.4k
  const int extraprec_clamp_limit = WIENER_CLAMP_LIMIT(round0_bits, bd);
1440
14.4k
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1441
14.4k
  src -= SUBPEL_TAPS / 2 - 1;
1442
653k
  for (int y = 0; y < h; ++y) {
1443
639k
    int x_q4 = x0_q4;
1444
29.8M
    for (int x = 0; x < w; ++x) {
1445
29.2M
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1446
29.2M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1447
29.2M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1448
29.2M
                           (1 << (bd + FILTER_BITS - 1));
1449
29.2M
      const int sum = highbd_horz_scalar_product(src_x, x_filter) + rounding;
1450
29.2M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1451
29.2M
                               extraprec_clamp_limit - 1);
1452
29.2M
      x_q4 += x_step_q4;
1453
29.2M
    }
1454
639k
    src += src_stride;
1455
639k
    dst += dst_stride;
1456
639k
  }
1457
14.4k
}
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
14.4k
    int y_step_q4, int w, int h, int round1_bits, int bd) {
1463
14.4k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1464
14.4k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1465
615k
  for (int x = 0; x < w; ++x) {
1466
600k
    int y_q4 = y0_q4;
1467
28.2M
    for (int y = 0; y < h; ++y) {
1468
27.6M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1469
27.6M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1470
27.6M
      const int rounding =
1471
27.6M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1472
27.6M
          (1 << (bd + round1_bits - 1));
1473
27.6M
      const int sum =
1474
27.6M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1475
27.6M
      dst[y * dst_stride] =
1476
27.6M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, round1_bits), bd);
1477
27.6M
      y_q4 += y_step_q4;
1478
27.6M
    }
1479
600k
    ++src;
1480
600k
    ++dst;
1481
600k
  }
1482
14.4k
}
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
14.4k
    const WienerConvolveParams *conv_params, int bd) {
1489
14.4k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1490
14.4k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1491
1492
14.4k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1493
14.4k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1494
1495
14.4k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1496
14.4k
  const int intermediate_height =
1497
14.4k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
1498
1499
14.4k
  assert(w <= MAX_SB_SIZE);
1500
14.4k
  assert(h <= MAX_SB_SIZE);
1501
14.4k
  assert(y_step_q4 <= 32);
1502
14.4k
  assert(x_step_q4 <= 32);
1503
14.4k
  assert(bd + FILTER_BITS - conv_params->round_0 + 2 <= 16);
1504
1505
14.4k
  highbd_convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1506
14.4k
                                    src_stride, temp, MAX_SB_SIZE, filters_x,
1507
14.4k
                                    x0_q4, x_step_q4, w, intermediate_height,
1508
14.4k
                                    conv_params->round_0, bd);
1509
14.4k
  highbd_convolve_add_src_vert_hip(
1510
14.4k
      temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1), MAX_SB_SIZE, dst, dst_stride,
1511
14.4k
      filters_y, y0_q4, y_step_q4, w, h, conv_params->round_1, bd);
1512
14.4k
}
1513
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1514
#endif  // CONFIG_AV1_HIGHBITDEPTH