Coverage Report

Created: 2025-07-16 07:53

/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.6k
                             int x_step_qn) {
30
24.6k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
31
952k
  for (int y = 0; y < h; ++y) {
32
927k
    int x_qn = x0_qn;
33
114M
    for (int x = 0; x < w; ++x) {
34
113M
      const uint8_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
35
113M
      const int x_filter_idx =
36
113M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
37
113M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
38
113M
      const int16_t *const x_filter =
39
113M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
40
113M
      int sum = 0;
41
1.02G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
42
906M
        sum += src_x[k] * x_filter[k];
43
113M
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
44
113M
      x_qn += x_step_qn;
45
113M
    }
46
927k
    src += src_stride;
47
927k
    dst += dst_stride;
48
927k
  }
49
24.6k
}
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
87.4k
                                    int x_step_qn, int bd) {
56
87.4k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
57
3.56M
  for (int y = 0; y < h; ++y) {
58
3.47M
    int x_qn = x0_qn;
59
207M
    for (int x = 0; x < w; ++x) {
60
203M
      const uint16_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
61
203M
      const int x_filter_idx =
62
203M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
63
203M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
64
203M
      const int16_t *const x_filter =
65
203M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
66
203M
      int sum = 0;
67
1.83G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
68
1.62G
        sum += src_x[k] * x_filter[k];
69
203M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
70
203M
      x_qn += x_step_qn;
71
203M
    }
72
3.47M
    src += src_stride;
73
3.47M
    dst += dst_stride;
74
3.47M
  }
75
87.4k
}
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.82k
                          ConvolveParams *conv_params) {
84
7.82k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
85
7.82k
  int im_h = h + filter_params_y->taps - 1;
86
7.82k
  int im_stride = w;
87
7.82k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
88
7.82k
  const int fo_vert = filter_params_y->taps / 2 - 1;
89
7.82k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
90
7.82k
  const int bd = 8;
91
7.82k
  const int bits =
92
7.82k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
93
94
  // horizontal filter
95
7.82k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
96
7.82k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
97
7.82k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
98
110k
  for (int y = 0; y < im_h; ++y) {
99
664k
    for (int x = 0; x < w; ++x) {
100
562k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
101
5.06M
      for (int k = 0; k < filter_params_x->taps; ++k) {
102
4.50M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
103
4.50M
      }
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
562k
      assert(filter_params_x->taps > 8 ||
110
562k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
111
562k
      im_block[y * im_stride + x] =
112
562k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
113
562k
    }
114
102k
  }
115
116
  // vertical filter
117
7.82k
  int16_t *src_vert = im_block + fo_vert * im_stride;
118
7.82k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
119
7.82k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
120
7.82k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
121
55.2k
  for (int y = 0; y < h; ++y) {
122
323k
    for (int x = 0; x < w; ++x) {
123
275k
      int32_t sum = 1 << offset_bits;
124
2.48M
      for (int k = 0; k < filter_params_y->taps; ++k) {
125
2.20M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
126
2.20M
      }
127
275k
      assert(filter_params_y->taps > 8 ||
128
275k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
129
275k
      int16_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
130
275k
                    ((1 << (offset_bits - conv_params->round_1)) +
131
275k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
132
275k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
133
275k
    }
134
47.4k
  }
135
7.82k
}
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
6.00k
                         const int subpel_y_qn) {
141
6.00k
  const int fo_vert = filter_params_y->taps / 2 - 1;
142
143
  // vertical filter
144
6.00k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
145
6.00k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
146
42.8k
  for (int y = 0; y < h; ++y) {
147
276k
    for (int x = 0; x < w; ++x) {
148
240k
      int32_t res = 0;
149
2.16M
      for (int k = 0; k < filter_params_y->taps; ++k) {
150
1.92M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
151
1.92M
      }
152
240k
      dst[y * dst_stride + x] =
153
240k
          clip_pixel(ROUND_POWER_OF_TWO(res, FILTER_BITS));
154
240k
    }
155
36.8k
  }
156
6.00k
}
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.87k
                         const int subpel_x_qn, ConvolveParams *conv_params) {
162
7.87k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
163
7.87k
  const int bits = FILTER_BITS - conv_params->round_0;
164
165
7.87k
  assert(bits >= 0);
166
7.87k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
167
7.87k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
168
169
  // horizontal filter
170
7.87k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
171
7.87k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
172
173
51.9k
  for (int y = 0; y < h; ++y) {
174
283k
    for (int x = 0; x < w; ++x) {
175
239k
      int32_t res = 0;
176
2.15M
      for (int k = 0; k < filter_params_x->taps; ++k) {
177
1.91M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
178
1.91M
      }
179
239k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
180
239k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
181
239k
    }
182
44.1k
  }
183
7.87k
}
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
3.05k
                                  ConvolveParams *conv_params) {
198
3.05k
  assert(subpel_x_qn == 8);
199
3.05k
  assert(subpel_y_qn == 8);
200
3.05k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
201
3.05k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
202
3.05k
  (void)filter_params_x;
203
3.05k
  (void)subpel_x_qn;
204
3.05k
  (void)filter_params_y;
205
3.05k
  (void)subpel_y_qn;
206
3.05k
  (void)conv_params;
207
208
3.05k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
209
3.05k
  int im_h = h + 1;
210
3.05k
  int im_stride = w;
211
3.05k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
212
3.05k
  const int bd = 8;
213
214
  // horizontal filter
215
  // explicitly operate for subpel_x_qn = 8.
216
3.05k
  int16_t *im = im_block;
217
28.4k
  for (int y = 0; y < im_h; ++y) {
218
298k
    for (int x = 0; x < w; ++x) {
219
272k
      const int32_t sum = (1 << bd) + src[x] + src[x + 1];
220
272k
      assert(0 <= sum && sum < (1 << (bd + 2)));
221
272k
      im[x] = sum;
222
272k
    }
223
25.3k
    src += src_stride;
224
25.3k
    im += im_stride;
225
25.3k
  }
226
227
  // vertical filter
228
  // explicitly operate for subpel_y_qn = 8.
229
3.05k
  int16_t *src_vert = im_block;
230
25.3k
  for (int y = 0; y < h; ++y) {
231
270k
    for (int x = 0; x < w; ++x) {
232
248k
      const int32_t sum =
233
248k
          (1 << (bd + 2)) + src_vert[x] + src_vert[im_stride + x];
234
248k
      assert(0 <= sum && sum < (1 << (bd + 4)));
235
248k
      const int16_t res =
236
248k
          ROUND_POWER_OF_TWO(sum, 2) - ((1 << bd) + (1 << (bd - 1)));
237
248k
      dst[x] = clip_pixel(res);
238
248k
    }
239
22.3k
    src_vert += im_stride;
240
22.3k
    dst += dst_stride;
241
22.3k
  }
242
3.05k
}
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.20k
                                 const int subpel_y_qn) {
250
3.20k
  assert(subpel_y_qn == 8);
251
3.20k
  assert(filter_params_y->taps == 2);
252
3.20k
  (void)filter_params_y;
253
3.20k
  (void)subpel_y_qn;
254
255
  // vertical filter
256
  // explicitly operate for subpel_y_qn = 8.
257
25.8k
  for (int y = 0; y < h; ++y) {
258
299k
    for (int x = 0; x < w; ++x) {
259
277k
      const int32_t res = src[x] + src[src_stride + x];
260
277k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
261
277k
    }
262
22.6k
    src += src_stride;
263
22.6k
    dst += dst_stride;
264
22.6k
  }
265
3.20k
}
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.47k
                                 ConvolveParams *conv_params) {
274
3.47k
  assert(subpel_x_qn == 8);
275
3.47k
  assert(filter_params_x->taps == 2);
276
3.47k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
277
3.47k
  (void)filter_params_x;
278
3.47k
  (void)subpel_x_qn;
279
3.47k
  (void)conv_params;
280
281
  // horizontal filter
282
  // explicitly operate for subpel_x_qn = 8.
283
37.0k
  for (int y = 0; y < h; ++y) {
284
776k
    for (int x = 0; x < w; ++x) {
285
742k
      const int32_t res = src[x] + src[x + 1];
286
742k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
287
742k
    }
288
33.6k
    src += src_stride;
289
33.6k
    dst += dst_stride;
290
33.6k
  }
291
3.47k
}
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.27k
                                ConvolveParams *conv_params) {
299
2.27k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
300
2.27k
  int dst16_stride = conv_params->dst_stride;
301
2.27k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
302
2.27k
  int im_h = h + filter_params_y->taps - 1;
303
2.27k
  int im_stride = w;
304
2.27k
  const int fo_vert = filter_params_y->taps / 2 - 1;
305
2.27k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
306
2.27k
  const int bd = 8;
307
2.27k
  const int round_bits =
308
2.27k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
309
310
  // horizontal filter
311
2.27k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
312
2.27k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
313
2.27k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
314
39.1k
  for (int y = 0; y < im_h; ++y) {
315
348k
    for (int x = 0; x < w; ++x) {
316
311k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
317
2.80M
      for (int k = 0; k < filter_params_x->taps; ++k) {
318
2.49M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
319
2.49M
      }
320
311k
      assert(filter_params_x->taps > 8 ||
321
311k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
322
311k
      im_block[y * im_stride + x] =
323
311k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
324
311k
    }
325
36.8k
  }
326
327
  // vertical filter
328
2.27k
  int16_t *src_vert = im_block + fo_vert * im_stride;
329
2.27k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
330
2.27k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
331
2.27k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
332
23.2k
  for (int y = 0; y < h; ++y) {
333
206k
    for (int x = 0; x < w; ++x) {
334
185k
      int32_t sum = 1 << offset_bits;
335
1.66M
      for (int k = 0; k < filter_params_y->taps; ++k) {
336
1.48M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
337
1.48M
      }
338
185k
      assert(filter_params_y->taps > 8 ||
339
185k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
340
185k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
341
185k
      if (conv_params->do_average) {
342
100k
        int32_t tmp = dst16[y * dst16_stride + x];
343
100k
        if (conv_params->use_dist_wtd_comp_avg) {
344
37.6k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
345
37.6k
          tmp = tmp >> DIST_PRECISION_BITS;
346
62.8k
        } else {
347
62.8k
          tmp += res;
348
62.8k
          tmp = tmp >> 1;
349
62.8k
        }
350
100k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
351
100k
               (1 << (offset_bits - conv_params->round_1 - 1));
352
100k
        dst[y * dst_stride + x] =
353
100k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
354
100k
      } else {
355
84.9k
        dst16[y * dst16_stride + x] = res;
356
84.9k
      }
357
185k
    }
358
20.9k
  }
359
2.27k
}
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.26k
                               ConvolveParams *conv_params) {
366
2.26k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
367
2.26k
  int dst16_stride = conv_params->dst_stride;
368
2.26k
  const int fo_vert = filter_params_y->taps / 2 - 1;
369
2.26k
  const int bits = FILTER_BITS - conv_params->round_0;
370
2.26k
  const int bd = 8;
371
2.26k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
372
2.26k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
373
2.26k
                           (1 << (offset_bits - conv_params->round_1 - 1));
374
2.26k
  const int round_bits =
375
2.26k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
376
377
  // vertical filter
378
2.26k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
379
2.26k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
380
24.3k
  for (int y = 0; y < h; ++y) {
381
213k
    for (int x = 0; x < w; ++x) {
382
191k
      int32_t res = 0;
383
1.72M
      for (int k = 0; k < filter_params_y->taps; ++k) {
384
1.53M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
385
1.53M
      }
386
191k
      res *= (1 << bits);
387
191k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
388
389
191k
      if (conv_params->do_average) {
390
62.7k
        int32_t tmp = dst16[y * dst16_stride + x];
391
62.7k
        if (conv_params->use_dist_wtd_comp_avg) {
392
7.80k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
393
7.80k
          tmp = tmp >> DIST_PRECISION_BITS;
394
54.9k
        } else {
395
54.9k
          tmp += res;
396
54.9k
          tmp = tmp >> 1;
397
54.9k
        }
398
62.7k
        tmp -= round_offset;
399
62.7k
        dst[y * dst_stride + x] =
400
62.7k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
401
128k
      } else {
402
128k
        dst16[y * dst16_stride + x] = res;
403
128k
      }
404
191k
    }
405
22.0k
  }
406
2.26k
}
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.21k
                               ConvolveParams *conv_params) {
413
1.21k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
414
1.21k
  int dst16_stride = conv_params->dst_stride;
415
1.21k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
416
1.21k
  const int bits = FILTER_BITS - conv_params->round_1;
417
1.21k
  const int bd = 8;
418
1.21k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
419
1.21k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
420
1.21k
                           (1 << (offset_bits - conv_params->round_1 - 1));
421
1.21k
  const int round_bits =
422
1.21k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
423
424
  // horizontal filter
425
1.21k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
426
1.21k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
427
12.7k
  for (int y = 0; y < h; ++y) {
428
123k
    for (int x = 0; x < w; ++x) {
429
111k
      int32_t res = 0;
430
1.00M
      for (int k = 0; k < filter_params_x->taps; ++k) {
431
892k
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
432
892k
      }
433
111k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
434
111k
      res += round_offset;
435
436
111k
      if (conv_params->do_average) {
437
41.4k
        int32_t tmp = dst16[y * dst16_stride + x];
438
41.4k
        if (conv_params->use_dist_wtd_comp_avg) {
439
17.6k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
440
17.6k
          tmp = tmp >> DIST_PRECISION_BITS;
441
23.8k
        } else {
442
23.8k
          tmp += res;
443
23.8k
          tmp = tmp >> 1;
444
23.8k
        }
445
41.4k
        tmp -= round_offset;
446
41.4k
        dst[y * dst_stride + x] =
447
41.4k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
448
70.1k
      } else {
449
70.1k
        dst16[y * dst16_stride + x] = res;
450
70.1k
      }
451
111k
    }
452
11.5k
  }
453
1.21k
}
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.88k
                                     ConvolveParams *conv_params) {
458
4.88k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
459
4.88k
  int dst16_stride = conv_params->dst_stride;
460
4.88k
  const int bits =
461
4.88k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
462
4.88k
  const int bd = 8;
463
4.88k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
464
4.88k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
465
4.88k
                           (1 << (offset_bits - conv_params->round_1 - 1));
466
467
52.4k
  for (int y = 0; y < h; ++y) {
468
537k
    for (int x = 0; x < w; ++x) {
469
489k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
470
489k
      res += round_offset;
471
472
489k
      if (conv_params->do_average) {
473
103k
        int32_t tmp = dst16[y * dst16_stride + x];
474
103k
        if (conv_params->use_dist_wtd_comp_avg) {
475
17.0k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
476
17.0k
          tmp = tmp >> DIST_PRECISION_BITS;
477
86.4k
        } else {
478
86.4k
          tmp += res;
479
86.4k
          tmp = tmp >> 1;
480
86.4k
        }
481
103k
        tmp -= round_offset;
482
103k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
483
386k
      } else {
484
386k
        dst16[y * dst16_stride + x] = res;
485
386k
      }
486
489k
    }
487
47.5k
  }
488
4.88k
}
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
10.1k
                             ConvolveParams *conv_params) {
497
10.1k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
498
10.1k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
499
10.1k
             filter_params_y->taps;
500
10.1k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
501
10.1k
  const int dst16_stride = conv_params->dst_stride;
502
10.1k
  const int bits =
503
10.1k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
504
10.1k
  assert(bits >= 0);
505
10.1k
  int im_stride = w;
506
10.1k
  const int fo_vert = filter_params_y->taps / 2 - 1;
507
10.1k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
508
10.1k
  const int bd = 8;
509
510
  // horizontal filter
511
10.1k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
512
153k
  for (int y = 0; y < im_h; ++y) {
513
143k
    int x_qn = subpel_x_qn;
514
1.37M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
515
1.23M
      const uint8_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
516
1.23M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
517
1.23M
      assert(x_filter_idx < SUBPEL_SHIFTS);
518
1.23M
      const int16_t *x_filter =
519
1.23M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
520
1.23M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
521
11.0M
      for (int k = 0; k < filter_params_x->taps; ++k) {
522
9.86M
        sum += x_filter[k] * src_x[k - fo_horiz];
523
9.86M
      }
524
1.23M
      assert(filter_params_x->taps > 8 ||
525
1.23M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
526
1.23M
      im_block[y * im_stride + x] =
527
1.23M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
528
1.23M
    }
529
143k
    src_horiz += src_stride;
530
143k
  }
531
532
  // vertical filter
533
10.1k
  int16_t *src_vert = im_block + fo_vert * im_stride;
534
10.1k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
535
72.7k
  for (int x = 0; x < w; ++x) {
536
62.5k
    int y_qn = subpel_y_qn;
537
840k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
538
778k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
539
778k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
540
778k
      assert(y_filter_idx < SUBPEL_SHIFTS);
541
778k
      const int16_t *y_filter =
542
778k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
543
778k
      int32_t sum = 1 << offset_bits;
544
7.00M
      for (int k = 0; k < filter_params_y->taps; ++k) {
545
6.22M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
546
6.22M
      }
547
778k
      assert(filter_params_y->taps > 8 ||
548
778k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
549
778k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
550
778k
      if (conv_params->is_compound) {
551
231k
        if (conv_params->do_average) {
552
165k
          int32_t tmp = dst16[y * dst16_stride + x];
553
165k
          if (conv_params->use_dist_wtd_comp_avg) {
554
107k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
555
107k
            tmp = tmp >> DIST_PRECISION_BITS;
556
107k
          } else {
557
57.4k
            tmp += res;
558
57.4k
            tmp = tmp >> 1;
559
57.4k
          }
560
          /* Subtract round offset and convolve round */
561
165k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
562
165k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
563
165k
          dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
564
165k
        } else {
565
66.1k
          dst16[y * dst16_stride + x] = res;
566
66.1k
        }
567
546k
      } else {
568
        /* Subtract round offset and convolve round */
569
546k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
570
546k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
571
546k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
572
546k
      }
573
778k
    }
574
62.5k
    src_vert++;
575
62.5k
  }
576
10.1k
}
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
10.1k
    ConvolveParams *conv_params) {
584
10.1k
  if (conv_params->is_compound) {
585
1.98k
    assert(conv_params->dst != NULL);
586
1.98k
  }
587
10.1k
  av1_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h, filter_params_x,
588
10.1k
                        filter_params_y, subpel_x_qn, x_step_qn, subpel_y_qn,
589
10.1k
                        y_step_qn, conv_params);
590
10.1k
}
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
10.6k
    const int subpel_y_qn, ConvolveParams *conv_params) {
597
10.6k
  const bool need_x = subpel_x_qn != 0;
598
10.6k
  const bool need_y = subpel_y_qn != 0;
599
10.6k
  if (!need_x && !need_y) {
600
4.88k
    av1_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
601
4.88k
                                  conv_params);
602
5.75k
  } else if (need_x && !need_y) {
603
1.21k
    av1_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
604
1.21k
                            filter_params_x, subpel_x_qn, conv_params);
605
4.53k
  } else if (!need_x && need_y) {
606
2.26k
    av1_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
607
2.26k
                            filter_params_y, subpel_y_qn, conv_params);
608
2.27k
  } else {
609
2.27k
    assert(need_y && need_x);
610
2.27k
    av1_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
611
2.27k
                             filter_params_x, filter_params_y, subpel_x_qn,
612
2.27k
                             subpel_y_qn, conv_params);
613
2.27k
  }
614
10.6k
}
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
182k
    const int subpel_y_qn, ConvolveParams *conv_params) {
621
182k
  const bool need_x = subpel_x_qn != 0;
622
182k
  const bool need_y = subpel_y_qn != 0;
623
182k
  if (!need_x && !need_y) {
624
160k
    aom_convolve_copy(src, src_stride, dst, dst_stride, w, h);
625
160k
  } else if (need_x && !need_y) {
626
7.87k
    av1_convolve_x_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
627
7.87k
                      subpel_x_qn, conv_params);
628
13.8k
  } else if (!need_x && need_y) {
629
6.00k
    av1_convolve_y_sr(src, src_stride, dst, dst_stride, w, h, filter_params_y,
630
6.00k
                      subpel_y_qn);
631
7.82k
  } else {
632
7.82k
    assert(need_x && need_y);
633
7.82k
    av1_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
634
7.82k
                       filter_params_y, subpel_x_qn, subpel_y_qn, conv_params);
635
7.82k
  }
636
182k
}
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
212k
                            ConvolveParams *conv_params) {
644
212k
  (void)x_step_q4;
645
212k
  (void)y_step_q4;
646
212k
  (void)dst;
647
212k
  (void)dst_stride;
648
649
212k
  const InterpFilterParams *filter_params_x = interp_filters[0];
650
212k
  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
212k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
655
150k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
656
150k
    assert(!scaled);
657
150k
    if (subpel_x_qn && subpel_y_qn) {
658
3.05k
      av1_convolve_2d_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
659
3.05k
                                 filter_params_x, filter_params_y, subpel_x_qn,
660
3.05k
                                 subpel_y_qn, conv_params);
661
3.05k
      return;
662
147k
    } else if (subpel_x_qn) {
663
3.47k
      av1_convolve_x_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
664
3.47k
                                filter_params_x, subpel_x_qn, conv_params);
665
3.47k
      return;
666
144k
    } else if (subpel_y_qn) {
667
3.20k
      av1_convolve_y_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
668
3.20k
                                filter_params_y, subpel_y_qn);
669
3.20k
      return;
670
3.20k
    }
671
150k
  }
672
673
202k
  if (scaled) {
674
10.1k
    convolve_2d_scale_wrapper(src, src_stride, dst, dst_stride, w, h,
675
10.1k
                              filter_params_x, filter_params_y, subpel_x_qn,
676
10.1k
                              x_step_q4, subpel_y_qn, y_step_q4, conv_params);
677
192k
  } else if (conv_params->is_compound) {
678
10.6k
    convolve_2d_facade_compound(src, src_stride, dst, dst_stride, w, h,
679
10.6k
                                filter_params_x, filter_params_y, subpel_x_qn,
680
10.6k
                                subpel_y_qn, conv_params);
681
182k
  } else {
682
182k
    convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
683
182k
                              filter_params_x, filter_params_y, subpel_x_qn,
684
182k
                              subpel_y_qn, conv_params);
685
182k
  }
686
202k
}
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
9.16k
                                ConvolveParams *conv_params, int bd) {
694
9.16k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
695
9.16k
  const int bits = FILTER_BITS - conv_params->round_0;
696
697
9.16k
  assert(bits >= 0);
698
9.16k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
699
9.16k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
700
701
  // horizontal filter
702
9.16k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
703
9.16k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
704
64.1k
  for (int y = 0; y < h; ++y) {
705
377k
    for (int x = 0; x < w; ++x) {
706
322k
      int32_t res = 0;
707
2.90M
      for (int k = 0; k < filter_params_x->taps; ++k) {
708
2.57M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
709
2.57M
      }
710
322k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
711
322k
      dst[y * dst_stride + x] =
712
322k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
713
322k
    }
714
55.0k
  }
715
9.16k
}
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
5.68k
                                const int subpel_y_qn, int bd) {
721
5.68k
  const int fo_vert = filter_params_y->taps / 2 - 1;
722
  // vertical filter
723
5.68k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
724
5.68k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
725
45.9k
  for (int y = 0; y < h; ++y) {
726
332k
    for (int x = 0; x < w; ++x) {
727
292k
      int32_t res = 0;
728
2.63M
      for (int k = 0; k < filter_params_y->taps; ++k) {
729
2.33M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
730
2.33M
      }
731
292k
      dst[y * dst_stride + x] =
732
292k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, FILTER_BITS), bd);
733
292k
    }
734
40.2k
  }
735
5.68k
}
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.44k
                                 ConvolveParams *conv_params, int bd) {
743
8.44k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
744
8.44k
  int im_h = h + filter_params_y->taps - 1;
745
8.44k
  int im_stride = w;
746
8.44k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
747
8.44k
  const int fo_vert = filter_params_y->taps / 2 - 1;
748
8.44k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
749
8.44k
  const int bits =
750
8.44k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
751
8.44k
  assert(bits >= 0);
752
753
  // horizontal filter
754
8.44k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
755
8.44k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
756
8.44k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
757
120k
  for (int y = 0; y < im_h; ++y) {
758
785k
    for (int x = 0; x < w; ++x) {
759
672k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
760
6.05M
      for (int k = 0; k < filter_params_x->taps; ++k) {
761
5.38M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
762
5.38M
      }
763
672k
      assert(filter_params_x->taps > 8 ||
764
672k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
765
672k
      im_block[y * im_stride + x] =
766
672k
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
767
672k
    }
768
112k
  }
769
770
  // vertical filter
771
8.44k
  int16_t *src_vert = im_block + fo_vert * im_stride;
772
8.44k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
773
8.44k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
774
8.44k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
775
61.7k
  for (int y = 0; y < h; ++y) {
776
390k
    for (int x = 0; x < w; ++x) {
777
337k
      int32_t sum = 1 << offset_bits;
778
3.03M
      for (int k = 0; k < filter_params_y->taps; ++k) {
779
2.69M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
780
2.69M
      }
781
337k
      assert(filter_params_y->taps > 8 ||
782
337k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
783
337k
      int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
784
337k
                    ((1 << (offset_bits - conv_params->round_1)) +
785
337k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
786
337k
      dst[y * dst_stride + x] =
787
337k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
788
337k
    }
789
53.3k
  }
790
8.44k
}
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.2k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
804
22.2k
  const int bits =
805
22.2k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
806
22.2k
  assert(bits >= 0);
807
22.2k
  assert(subpel_x_qn == 8);
808
22.2k
  assert(subpel_y_qn == 8);
809
22.2k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
810
22.2k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
811
22.2k
  (void)filter_params_x;
812
22.2k
  (void)subpel_x_qn;
813
22.2k
  (void)filter_params_y;
814
22.2k
  (void)subpel_y_qn;
815
22.2k
  (void)conv_params;
816
817
22.2k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
818
22.2k
  int im_h = h + 1;
819
22.2k
  int im_stride = w;
820
22.2k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
821
822
  // horizontal filter
823
  // explicitly operate for subpel_x_qn = 8.
824
22.2k
  int16_t *im = im_block;
825
190k
  for (int y = 0; y < im_h; ++y) {
826
1.74M
    for (int x = 0; x < w; ++x) {
827
1.57M
      int32_t sum = (1 << (bd + FILTER_BITS - 1)) + 64 * (src[x] + src[x + 1]);
828
1.57M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
829
1.57M
      sum = ROUND_POWER_OF_TWO(sum, conv_params->round_0);
830
1.57M
      im[x] = sum;
831
1.57M
    }
832
168k
    src += src_stride;
833
168k
    im += im_stride;
834
168k
  }
835
836
  // vertical filter
837
  // explicitly operate for subpel_y_qn = 8.
838
22.2k
  int16_t *src_vert = im_block;
839
22.2k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
840
168k
  for (int y = 0; y < h; ++y) {
841
1.55M
    for (int x = 0; x < w; ++x) {
842
1.40M
      const int32_t sum =
843
1.40M
          (1 << offset_bits) + 64 * (src_vert[x] + src_vert[im_stride + x]);
844
1.40M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
845
1.40M
      const int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
846
1.40M
                          ((1 << (offset_bits - conv_params->round_1)) +
847
1.40M
                           (1 << (offset_bits - conv_params->round_1 - 1)));
848
849
1.40M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
850
1.40M
    }
851
146k
    src_vert += im_stride;
852
146k
    dst += dst_stride;
853
146k
  }
854
22.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
20.3k
    int bd) {
862
20.3k
  assert(subpel_y_qn == 8);
863
20.3k
  assert(filter_params_y->taps == 2);
864
20.3k
  (void)filter_params_y;
865
20.3k
  (void)subpel_y_qn;
866
867
  // vertical filter
868
  // explicitly operate for subpel_y_qn = 8.
869
147k
  for (int y = 0; y < h; ++y) {
870
1.20M
    for (int x = 0; x < w; ++x) {
871
1.07M
      const int32_t res = src[x] + src[src_stride + x];
872
1.07M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, 1), bd);
873
1.07M
    }
874
127k
    src += src_stride;
875
127k
    dst += dst_stride;
876
127k
  }
877
20.3k
}
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
21.2k
    ConvolveParams *conv_params, int bd) {
885
21.2k
  const int bits = FILTER_BITS - conv_params->round_0;
886
21.2k
  assert(bits >= 0);
887
21.2k
  assert(subpel_x_qn == 8);
888
21.2k
  assert(filter_params_x->taps == 2);
889
21.2k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
890
21.2k
  (void)filter_params_x;
891
21.2k
  (void)subpel_x_qn;
892
893
  // horizontal filter
894
  // explicitly operate for subpel_x_qn = 8.
895
164k
  for (int y = 0; y < h; ++y) {
896
1.78M
    for (int x = 0; x < w; ++x) {
897
1.64M
      int32_t res = 64 * (src[x] + src[x + 1]);
898
1.64M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
899
1.64M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
900
1.64M
    }
901
143k
    src += src_stride;
902
143k
    dst += dst_stride;
903
143k
  }
904
21.2k
}
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
76.2k
  for (y = 0; y < im_h; ++y) {
928
682k
    for (x = 0; x < w; ++x) {
929
610k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
930
5.49M
      for (k = 0; k < filter_params_x->taps; ++k) {
931
4.88M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
932
4.88M
      }
933
610k
      assert(filter_params_x->taps > 8 ||
934
610k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
935
610k
      (void)bd;
936
610k
      im_block[y * im_stride + x] =
937
610k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
938
610k
    }
939
71.8k
  }
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.8k
  for (y = 0; y < h; ++y) {
947
406k
    for (x = 0; x < w; ++x) {
948
364k
      int32_t sum = 1 << offset_bits;
949
3.28M
      for (k = 0; k < filter_params_y->taps; ++k) {
950
2.91M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
951
2.91M
      }
952
364k
      assert(filter_params_y->taps > 8 ||
953
364k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
954
364k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
955
364k
      if (conv_params->do_average) {
956
110k
        int32_t tmp = dst16[y * dst16_stride + x];
957
110k
        if (conv_params->use_dist_wtd_comp_avg) {
958
6.33k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
959
6.33k
          tmp = tmp >> DIST_PRECISION_BITS;
960
104k
        } else {
961
104k
          tmp += res;
962
104k
          tmp = tmp >> 1;
963
104k
        }
964
110k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
965
110k
               (1 << (offset_bits - conv_params->round_1 - 1));
966
110k
        dst[y * dst_stride + x] =
967
110k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
968
253k
      } else {
969
253k
        dst16[y * dst16_stride + x] = res;
970
253k
      }
971
364k
    }
972
41.5k
  }
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.63k
                                      ConvolveParams *conv_params, int bd) {
981
2.63k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
982
2.63k
  int dst16_stride = conv_params->dst_stride;
983
2.63k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
984
2.63k
  const int bits = FILTER_BITS - conv_params->round_1;
985
2.63k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
986
2.63k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
987
2.63k
                           (1 << (offset_bits - conv_params->round_1 - 1));
988
2.63k
  const int round_bits =
989
2.63k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
990
2.63k
  assert(round_bits >= 0);
991
2.63k
  assert(bits >= 0);
992
  // horizontal filter
993
2.63k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
994
2.63k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
995
32.8k
  for (int y = 0; y < h; ++y) {
996
334k
    for (int x = 0; x < w; ++x) {
997
304k
      int32_t res = 0;
998
2.73M
      for (int k = 0; k < filter_params_x->taps; ++k) {
999
2.43M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
1000
2.43M
      }
1001
304k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
1002
304k
      res += round_offset;
1003
1004
304k
      if (conv_params->do_average) {
1005
186k
        int32_t tmp = dst16[y * dst16_stride + x];
1006
186k
        if (conv_params->use_dist_wtd_comp_avg) {
1007
5.05k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1008
5.05k
          tmp = tmp >> DIST_PRECISION_BITS;
1009
181k
        } else {
1010
181k
          tmp += res;
1011
181k
          tmp = tmp >> 1;
1012
181k
        }
1013
186k
        tmp -= round_offset;
1014
186k
        dst[y * dst_stride + x] =
1015
186k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1016
186k
      } else {
1017
117k
        dst16[y * dst16_stride + x] = res;
1018
117k
      }
1019
304k
    }
1020
30.1k
  }
1021
2.63k
}
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.03k
                                      ConvolveParams *conv_params, int bd) {
1029
3.03k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1030
3.03k
  int dst16_stride = conv_params->dst_stride;
1031
3.03k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1032
3.03k
  const int bits = FILTER_BITS - conv_params->round_0;
1033
3.03k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1034
3.03k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1035
3.03k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1036
3.03k
  const int round_bits =
1037
3.03k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
1038
3.03k
  assert(round_bits >= 0);
1039
3.03k
  assert(bits >= 0);
1040
  // vertical filter
1041
3.03k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
1042
3.03k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
1043
35.5k
  for (int y = 0; y < h; ++y) {
1044
328k
    for (int x = 0; x < w; ++x) {
1045
295k
      int32_t res = 0;
1046
2.66M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1047
2.36M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
1048
2.36M
      }
1049
295k
      res *= (1 << bits);
1050
295k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
1051
1052
295k
      if (conv_params->do_average) {
1053
92.4k
        int32_t tmp = dst16[y * dst16_stride + x];
1054
92.4k
        if (conv_params->use_dist_wtd_comp_avg) {
1055
13.5k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1056
13.5k
          tmp = tmp >> DIST_PRECISION_BITS;
1057
78.9k
        } else {
1058
78.9k
          tmp += res;
1059
78.9k
          tmp = tmp >> 1;
1060
78.9k
        }
1061
92.4k
        tmp -= round_offset;
1062
92.4k
        dst[y * dst_stride + x] =
1063
92.4k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1064
203k
      } else {
1065
203k
        dst16[y * dst16_stride + x] = res;
1066
203k
      }
1067
295k
    }
1068
32.5k
  }
1069
3.03k
}
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.8k
                                            int bd) {
1076
10.8k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1077
10.8k
  int dst16_stride = conv_params->dst_stride;
1078
10.8k
  const int bits =
1079
10.8k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
1080
10.8k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1081
10.8k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1082
10.8k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1083
10.8k
  assert(bits >= 0);
1084
1085
109k
  for (int y = 0; y < h; ++y) {
1086
1.02M
    for (int x = 0; x < w; ++x) {
1087
929k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
1088
929k
      res += round_offset;
1089
929k
      if (conv_params->do_average) {
1090
277k
        int32_t tmp = dst16[y * dst16_stride + x];
1091
277k
        if (conv_params->use_dist_wtd_comp_avg) {
1092
162k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1093
162k
          tmp = tmp >> DIST_PRECISION_BITS;
1094
162k
        } else {
1095
114k
          tmp += res;
1096
114k
          tmp = tmp >> 1;
1097
114k
        }
1098
277k
        tmp -= round_offset;
1099
277k
        dst[y * dst_stride + x] =
1100
277k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1101
652k
      } else {
1102
652k
        dst16[y * dst16_stride + x] = res;
1103
652k
      }
1104
929k
    }
1105
98.7k
  }
1106
10.8k
}
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
8.25k
                                    ConvolveParams *conv_params, int bd) {
1115
8.25k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
1116
8.25k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
1117
8.25k
             filter_params_y->taps;
1118
8.25k
  int im_stride = w;
1119
8.25k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1120
8.25k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
1121
8.25k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1122
8.25k
  const int dst16_stride = conv_params->dst_stride;
1123
8.25k
  const int bits =
1124
8.25k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
1125
8.25k
  assert(bits >= 0);
1126
  // horizontal filter
1127
8.25k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
1128
124k
  for (int y = 0; y < im_h; ++y) {
1129
116k
    int x_qn = subpel_x_qn;
1130
1.37M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
1131
1.26M
      const uint16_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
1132
1.26M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1133
1.26M
      assert(x_filter_idx < SUBPEL_SHIFTS);
1134
1.26M
      const int16_t *x_filter =
1135
1.26M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
1136
1.26M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
1137
11.3M
      for (int k = 0; k < filter_params_x->taps; ++k) {
1138
10.0M
        sum += x_filter[k] * src_x[k - fo_horiz];
1139
10.0M
      }
1140
1.26M
      assert(filter_params_x->taps > 8 ||
1141
1.26M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
1142
1.26M
      im_block[y * im_stride + x] =
1143
1.26M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
1144
1.26M
    }
1145
116k
    src_horiz += src_stride;
1146
116k
  }
1147
1148
  // vertical filter
1149
8.25k
  int16_t *src_vert = im_block + fo_vert * im_stride;
1150
8.25k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1151
65.0k
  for (int x = 0; x < w; ++x) {
1152
56.7k
    int y_qn = subpel_y_qn;
1153
912k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
1154
855k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
1155
855k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1156
855k
      assert(y_filter_idx < SUBPEL_SHIFTS);
1157
855k
      const int16_t *y_filter =
1158
855k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
1159
855k
      int32_t sum = 1 << offset_bits;
1160
7.69M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1161
6.84M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
1162
6.84M
      }
1163
855k
      assert(filter_params_y->taps > 8 ||
1164
855k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
1165
855k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
1166
855k
      if (conv_params->is_compound) {
1167
226k
        if (conv_params->do_average) {
1168
118k
          int32_t tmp = dst16[y * dst16_stride + x];
1169
118k
          if (conv_params->use_dist_wtd_comp_avg) {
1170
58.3k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1171
58.3k
            tmp = tmp >> DIST_PRECISION_BITS;
1172
60.3k
          } else {
1173
60.3k
            tmp += res;
1174
60.3k
            tmp = tmp >> 1;
1175
60.3k
          }
1176
          /* Subtract round offset and convolve round */
1177
118k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
1178
118k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
1179
118k
          dst[y * dst_stride + x] =
1180
118k
              clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1181
118k
        } else {
1182
107k
          dst16[y * dst16_stride + x] = res;
1183
107k
        }
1184
629k
      } else {
1185
        /* Subtract round offset and convolve round */
1186
629k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
1187
629k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
1188
629k
        dst[y * dst_stride + x] =
1189
629k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1190
629k
      }
1191
855k
    }
1192
56.7k
    src_vert++;
1193
56.7k
  }
1194
8.25k
}
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.8k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1201
20.8k
  const bool need_x = subpel_x_qn != 0;
1202
20.8k
  const bool need_y = subpel_y_qn != 0;
1203
20.8k
  if (!need_x && !need_y) {
1204
10.8k
    av1_highbd_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
1205
10.8k
                                         conv_params, bd);
1206
10.8k
  } else if (need_x && !need_y) {
1207
2.63k
    av1_highbd_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
1208
2.63k
                                   filter_params_x, subpel_x_qn, conv_params,
1209
2.63k
                                   bd);
1210
7.36k
  } else if (!need_x && need_y) {
1211
3.03k
    av1_highbd_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
1212
3.03k
                                   filter_params_y, subpel_y_qn, conv_params,
1213
3.03k
                                   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.8k
}
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
116k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1227
116k
  const bool need_x = subpel_x_qn != 0;
1228
116k
  const bool need_y = subpel_y_qn != 0;
1229
1230
116k
  if (!need_x && !need_y) {
1231
93.2k
    aom_highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h);
1232
93.2k
  } else if (need_x && !need_y) {
1233
9.16k
    av1_highbd_convolve_x_sr(src, src_stride, dst, dst_stride, w, h,
1234
9.16k
                             filter_params_x, subpel_x_qn, conv_params, bd);
1235
14.1k
  } else if (!need_x && need_y) {
1236
5.68k
    av1_highbd_convolve_y_sr(src, src_stride, dst, dst_stride, w, h,
1237
5.68k
                             filter_params_y, subpel_y_qn, bd);
1238
8.44k
  } else {
1239
8.44k
    assert(need_x && need_y);
1240
8.44k
    av1_highbd_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h,
1241
8.44k
                              filter_params_x, filter_params_y, subpel_x_qn,
1242
8.44k
                              subpel_y_qn, conv_params, bd);
1243
8.44k
  }
1244
116k
}
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
209k
                                   int bd) {
1253
209k
  (void)x_step_q4;
1254
209k
  (void)y_step_q4;
1255
209k
  (void)dst_stride;
1256
209k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1257
1258
209k
  const InterpFilterParams *filter_params_x = interp_filters[0];
1259
209k
  const InterpFilterParams *filter_params_y = interp_filters[1];
1260
1261
209k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1262
  // 2-tap filter indicates that it is for IntraBC.
1263
209k
  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.2k
      av1_highbd_convolve_2d_sr_intrabc_c(
1268
22.2k
          src, src_stride, dst, dst_stride, w, h, filter_params_x,
1269
22.2k
          filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1270
22.2k
      return;
1271
118k
    } else if (subpel_x_qn) {
1272
21.2k
      av1_highbd_convolve_x_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1273
21.2k
                                         filter_params_x, subpel_x_qn,
1274
21.2k
                                         conv_params, bd);
1275
21.2k
      return;
1276
97.1k
    } else if (subpel_y_qn) {
1277
20.3k
      av1_highbd_convolve_y_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1278
20.3k
                                         filter_params_y, subpel_y_qn, bd);
1279
20.3k
      return;
1280
20.3k
    }
1281
140k
  }
1282
1283
145k
  if (scaled) {
1284
8.25k
    if (conv_params->is_compound) {
1285
1.81k
      assert(conv_params->dst != NULL);
1286
1.81k
    }
1287
8.25k
    av1_highbd_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h,
1288
8.25k
                                 filter_params_x, filter_params_y, subpel_x_qn,
1289
8.25k
                                 x_step_q4, subpel_y_qn, y_step_q4, conv_params,
1290
8.25k
                                 bd);
1291
137k
  } else if (conv_params->is_compound) {
1292
20.8k
    highbd_convolve_2d_facade_compound(
1293
20.8k
        src, src_stride, dst, dst_stride, w, h, filter_params_x,
1294
20.8k
        filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1295
116k
  } else {
1296
116k
    highbd_convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
1297
116k
                                     filter_params_x, filter_params_y,
1298
116k
                                     subpel_x_qn, subpel_y_qn, conv_params, bd);
1299
116k
  }
1300
145k
}
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
9.17M
static inline int horz_scalar_product(const uint8_t *a, const int16_t *b) {
1319
9.17M
  int sum = 0;
1320
82.5M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1321
9.17M
  return sum;
1322
9.17M
}
1323
1324
#if CONFIG_AV1_HIGHBITDEPTH
1325
static inline int highbd_horz_scalar_product(const uint16_t *a,
1326
39.9M
                                             const int16_t *b) {
1327
39.9M
  int sum = 0;
1328
359M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1329
39.9M
  return sum;
1330
39.9M
}
1331
#endif
1332
1333
static inline int highbd_vert_scalar_product(const uint16_t *a,
1334
                                             ptrdiff_t a_stride,
1335
47.0M
                                             const int16_t *b) {
1336
47.0M
  int sum = 0;
1337
419M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
1338
47.0M
  return sum;
1339
47.0M
}
1340
1341
48.3k
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
48.3k
  return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
1345
48.3k
}
1346
1347
48.3k
static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
1348
48.3k
  return (int)((const InterpKernel *)(intptr_t)f - base);
1349
48.3k
}
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
6.01k
                                       int round0_bits) {
1356
6.01k
  const int bd = 8;
1357
6.01k
  src -= SUBPEL_TAPS / 2 - 1;
1358
204k
  for (int y = 0; y < h; ++y) {
1359
198k
    int x_q4 = x0_q4;
1360
9.37M
    for (int x = 0; x < w; ++x) {
1361
9.17M
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1362
9.17M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1363
9.17M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1364
9.17M
                           (1 << (bd + FILTER_BITS - 1));
1365
9.17M
      const int sum = horz_scalar_product(src_x, x_filter) + rounding;
1366
9.17M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1367
9.17M
                               WIENER_CLAMP_LIMIT(round0_bits, bd) - 1);
1368
9.17M
      x_q4 += x_step_q4;
1369
9.17M
    }
1370
198k
    src += src_stride;
1371
198k
    dst += dst_stride;
1372
198k
  }
1373
6.01k
}
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
6.00k
                                      int round1_bits) {
1380
6.00k
  const int bd = 8;
1381
6.00k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1382
1383
242k
  for (int x = 0; x < w; ++x) {
1384
236k
    int y_q4 = y0_q4;
1385
9.83M
    for (int y = 0; y < h; ++y) {
1386
9.59M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1387
9.59M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1388
9.59M
      const int rounding =
1389
9.59M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1390
9.59M
          (1 << (bd + round1_bits - 1));
1391
9.59M
      const int sum =
1392
9.59M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1393
9.59M
      dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, round1_bits));
1394
9.59M
      y_q4 += y_step_q4;
1395
9.59M
    }
1396
236k
    ++src;
1397
236k
    ++dst;
1398
236k
  }
1399
6.00k
}
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
6.01k
                                   const WienerConvolveParams *conv_params) {
1407
6.01k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1408
6.01k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1409
1410
6.01k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1411
6.01k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1412
1413
6.01k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1414
6.01k
  const int intermediate_height =
1415
6.01k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS - 1;
1416
6.01k
  memset(temp + (intermediate_height * MAX_SB_SIZE), 0, MAX_SB_SIZE);
1417
1418
6.01k
  assert(w <= MAX_SB_SIZE);
1419
6.01k
  assert(h <= MAX_SB_SIZE);
1420
6.01k
  assert(y_step_q4 <= 32);
1421
6.01k
  assert(x_step_q4 <= 32);
1422
1423
6.01k
  convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1424
6.01k
                             src_stride, temp, MAX_SB_SIZE, filters_x, x0_q4,
1425
6.01k
                             x_step_q4, w, intermediate_height,
1426
6.01k
                             conv_params->round_0);
1427
6.01k
  convolve_add_src_vert_hip(temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1),
1428
6.01k
                            MAX_SB_SIZE, dst, dst_stride, filters_y, y0_q4,
1429
6.01k
                            y_step_q4, w, h, conv_params->round_1);
1430
6.01k
}
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
18.1k
    int x_step_q4, int w, int h, int round0_bits, int bd) {
1439
18.1k
  const int extraprec_clamp_limit = WIENER_CLAMP_LIMIT(round0_bits, bd);
1440
18.1k
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1441
18.1k
  src -= SUBPEL_TAPS / 2 - 1;
1442
899k
  for (int y = 0; y < h; ++y) {
1443
881k
    int x_q4 = x0_q4;
1444
40.8M
    for (int x = 0; x < w; ++x) {
1445
39.9M
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1446
39.9M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1447
39.9M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1448
39.9M
                           (1 << (bd + FILTER_BITS - 1));
1449
39.9M
      const int sum = highbd_horz_scalar_product(src_x, x_filter) + rounding;
1450
39.9M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1451
39.9M
                               extraprec_clamp_limit - 1);
1452
39.9M
      x_q4 += x_step_q4;
1453
39.9M
    }
1454
881k
    src += src_stride;
1455
881k
    dst += dst_stride;
1456
881k
  }
1457
18.1k
}
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
18.1k
    int y_step_q4, int w, int h, int round1_bits, int bd) {
1463
18.1k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1464
18.1k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1465
799k
  for (int x = 0; x < w; ++x) {
1466
780k
    int y_q4 = y0_q4;
1467
40.2M
    for (int y = 0; y < h; ++y) {
1468
39.4M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1469
39.4M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1470
39.4M
      const int rounding =
1471
39.4M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1472
39.4M
          (1 << (bd + round1_bits - 1));
1473
39.4M
      const int sum =
1474
39.4M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1475
39.4M
      dst[y * dst_stride] =
1476
39.4M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, round1_bits), bd);
1477
39.4M
      y_q4 += y_step_q4;
1478
39.4M
    }
1479
780k
    ++src;
1480
780k
    ++dst;
1481
780k
  }
1482
18.1k
}
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
18.1k
    const WienerConvolveParams *conv_params, int bd) {
1489
18.1k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1490
18.1k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1491
1492
18.1k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1493
18.1k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1494
1495
18.1k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1496
18.1k
  const int intermediate_height =
1497
18.1k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
1498
1499
18.1k
  assert(w <= MAX_SB_SIZE);
1500
18.1k
  assert(h <= MAX_SB_SIZE);
1501
18.1k
  assert(y_step_q4 <= 32);
1502
18.1k
  assert(x_step_q4 <= 32);
1503
18.1k
  assert(bd + FILTER_BITS - conv_params->round_0 + 2 <= 16);
1504
1505
18.1k
  highbd_convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1506
18.1k
                                    src_stride, temp, MAX_SB_SIZE, filters_x,
1507
18.1k
                                    x0_q4, x_step_q4, w, intermediate_height,
1508
18.1k
                                    conv_params->round_0, bd);
1509
18.1k
  highbd_convolve_add_src_vert_hip(
1510
18.1k
      temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1), MAX_SB_SIZE, dst, dst_stride,
1511
18.1k
      filters_y, y0_q4, y_step_q4, w, h, conv_params->round_1, bd);
1512
18.1k
}
1513
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1514
#endif  // CONFIG_AV1_HIGHBITDEPTH