Coverage Report

Created: 2026-04-01 07:49

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