Coverage Report

Created: 2026-07-25 07:03

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
19.1k
                             int x_step_qn) {
30
19.1k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
31
547k
  for (int y = 0; y < h; ++y) {
32
528k
    int x_qn = x0_qn;
33
56.3M
    for (int x = 0; x < w; ++x) {
34
55.7M
      const uint8_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
35
55.7M
      const int x_filter_idx =
36
55.7M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
37
55.7M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
38
55.7M
      const int16_t *const x_filter =
39
55.7M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
40
55.7M
      int sum = 0;
41
502M
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
42
446M
        sum += src_x[k] * x_filter[k];
43
55.7M
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
44
55.7M
      x_qn += x_step_qn;
45
55.7M
    }
46
528k
    src += src_stride;
47
528k
    dst += dst_stride;
48
528k
  }
49
19.1k
}
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
47.2k
                                    int x_step_qn, int bd) {
56
47.2k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
57
1.68M
  for (int y = 0; y < h; ++y) {
58
1.63M
    int x_qn = x0_qn;
59
77.3M
    for (int x = 0; x < w; ++x) {
60
75.7M
      const uint16_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
61
75.7M
      const int x_filter_idx =
62
75.7M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
63
75.7M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
64
75.7M
      const int16_t *const x_filter =
65
75.7M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
66
75.7M
      int sum = 0;
67
681M
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
68
605M
        sum += src_x[k] * x_filter[k];
69
75.7M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
70
75.7M
      x_qn += x_step_qn;
71
75.7M
    }
72
1.63M
    src += src_stride;
73
1.63M
    dst += dst_stride;
74
1.63M
  }
75
47.2k
}
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.90k
                          ConvolveParams *conv_params) {
84
7.90k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
85
7.90k
  int im_h = h + filter_params_y->taps - 1;
86
7.90k
  int im_stride = w;
87
7.90k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
88
7.90k
  const int fo_vert = filter_params_y->taps / 2 - 1;
89
7.90k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
90
7.90k
  const int bd = 8;
91
7.90k
  const int bits =
92
7.90k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
93
94
  // horizontal filter
95
7.90k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
96
7.90k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
97
7.90k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
98
108k
  for (int y = 0; y < im_h; ++y) {
99
644k
    for (int x = 0; x < w; ++x) {
100
543k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
101
4.89M
      for (int k = 0; k < filter_params_x->taps; ++k) {
102
4.34M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
103
4.34M
      }
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
543k
      assert(filter_params_x->taps > 8 ||
110
543k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
111
543k
      im_block[y * im_stride + x] =
112
543k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
113
543k
    }
114
100k
  }
115
116
  // vertical filter
117
7.90k
  int16_t *src_vert = im_block + fo_vert * im_stride;
118
7.90k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
119
7.90k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
120
7.90k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
121
53.4k
  for (int y = 0; y < h; ++y) {
122
301k
    for (int x = 0; x < w; ++x) {
123
256k
      int32_t sum = 1 << offset_bits;
124
2.30M
      for (int k = 0; k < filter_params_y->taps; ++k) {
125
2.05M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
126
2.05M
      }
127
256k
      assert(filter_params_y->taps > 8 ||
128
256k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
129
256k
      int16_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
130
256k
                    ((1 << (offset_bits - conv_params->round_1)) +
131
256k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
132
256k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
133
256k
    }
134
45.5k
  }
135
7.90k
}
136
137
void av1_convolve_y_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
138
                         int dst_stride, int w, int h,
139
                         const InterpFilterParams *filter_params_y,
140
4.54k
                         const int subpel_y_qn) {
141
4.54k
  const int fo_vert = filter_params_y->taps / 2 - 1;
142
143
  // vertical filter
144
4.54k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
145
4.54k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
146
31.7k
  for (int y = 0; y < h; ++y) {
147
199k
    for (int x = 0; x < w; ++x) {
148
171k
      int32_t res = 0;
149
1.54M
      for (int k = 0; k < filter_params_y->taps; ++k) {
150
1.37M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
151
1.37M
      }
152
171k
      dst[y * dst_stride + x] =
153
171k
          clip_pixel(ROUND_POWER_OF_TWO(res, FILTER_BITS));
154
171k
    }
155
27.2k
  }
156
4.54k
}
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
5.98k
                         const int subpel_x_qn, ConvolveParams *conv_params) {
162
5.98k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
163
5.98k
  const int bits = FILTER_BITS - conv_params->round_0;
164
165
5.98k
  assert(bits >= 0);
166
5.98k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
167
5.98k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
168
169
  // horizontal filter
170
5.98k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
171
5.98k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
172
173
40.1k
  for (int y = 0; y < h; ++y) {
174
237k
    for (int x = 0; x < w; ++x) {
175
203k
      int32_t res = 0;
176
1.83M
      for (int k = 0; k < filter_params_x->taps; ++k) {
177
1.63M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
178
1.63M
      }
179
203k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
180
203k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
181
203k
    }
182
34.1k
  }
183
5.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
1.46k
                                  ConvolveParams *conv_params) {
198
1.46k
  assert(subpel_x_qn == 8);
199
1.46k
  assert(subpel_y_qn == 8);
200
1.46k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
201
1.46k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
202
1.46k
  (void)filter_params_x;
203
1.46k
  (void)subpel_x_qn;
204
1.46k
  (void)filter_params_y;
205
1.46k
  (void)subpel_y_qn;
206
1.46k
  (void)conv_params;
207
208
1.46k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
209
1.46k
  int im_h = h + 1;
210
1.46k
  int im_stride = w;
211
1.46k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
212
1.46k
  const int bd = 8;
213
214
  // horizontal filter
215
  // explicitly operate for subpel_x_qn = 8.
216
1.46k
  int16_t *im = im_block;
217
13.3k
  for (int y = 0; y < im_h; ++y) {
218
129k
    for (int x = 0; x < w; ++x) {
219
117k
      const int32_t sum = (1 << bd) + src[x] + src[x + 1];
220
117k
      assert(0 <= sum && sum < (1 << (bd + 2)));
221
117k
      im[x] = sum;
222
117k
    }
223
11.8k
    src += src_stride;
224
11.8k
    im += im_stride;
225
11.8k
  }
226
227
  // vertical filter
228
  // explicitly operate for subpel_y_qn = 8.
229
1.46k
  int16_t *src_vert = im_block;
230
11.8k
  for (int y = 0; y < h; ++y) {
231
117k
    for (int x = 0; x < w; ++x) {
232
106k
      const int32_t sum =
233
106k
          (1 << (bd + 2)) + src_vert[x] + src_vert[im_stride + x];
234
106k
      assert(0 <= sum && sum < (1 << (bd + 4)));
235
106k
      const int16_t res =
236
106k
          ROUND_POWER_OF_TWO(sum, 2) - ((1 << bd) + (1 << (bd - 1)));
237
106k
      dst[x] = clip_pixel(res);
238
106k
    }
239
10.4k
    src_vert += im_stride;
240
10.4k
    dst += dst_stride;
241
10.4k
  }
242
1.46k
}
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
1.74k
                                 const int subpel_y_qn) {
250
1.74k
  assert(subpel_y_qn == 8);
251
1.74k
  assert(filter_params_y->taps == 2);
252
1.74k
  (void)filter_params_y;
253
1.74k
  (void)subpel_y_qn;
254
255
  // vertical filter
256
  // explicitly operate for subpel_y_qn = 8.
257
14.1k
  for (int y = 0; y < h; ++y) {
258
171k
    for (int x = 0; x < w; ++x) {
259
158k
      const int32_t res = src[x] + src[src_stride + x];
260
158k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
261
158k
    }
262
12.4k
    src += src_stride;
263
12.4k
    dst += dst_stride;
264
12.4k
  }
265
1.74k
}
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
1.72k
                                 ConvolveParams *conv_params) {
274
1.72k
  assert(subpel_x_qn == 8);
275
1.72k
  assert(filter_params_x->taps == 2);
276
1.72k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
277
1.72k
  (void)filter_params_x;
278
1.72k
  (void)subpel_x_qn;
279
1.72k
  (void)conv_params;
280
281
  // horizontal filter
282
  // explicitly operate for subpel_x_qn = 8.
283
16.2k
  for (int y = 0; y < h; ++y) {
284
227k
    for (int x = 0; x < w; ++x) {
285
213k
      const int32_t res = src[x] + src[x + 1];
286
213k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
287
213k
    }
288
14.4k
    src += src_stride;
289
14.4k
    dst += dst_stride;
290
14.4k
  }
291
1.72k
}
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
3.09k
                                ConvolveParams *conv_params) {
299
3.09k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
300
3.09k
  int dst16_stride = conv_params->dst_stride;
301
3.09k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
302
3.09k
  int im_h = h + filter_params_y->taps - 1;
303
3.09k
  int im_stride = w;
304
3.09k
  const int fo_vert = filter_params_y->taps / 2 - 1;
305
3.09k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
306
3.09k
  const int bd = 8;
307
3.09k
  const int round_bits =
308
3.09k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
309
310
  // horizontal filter
311
3.09k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
312
3.09k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
313
3.09k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
314
50.6k
  for (int y = 0; y < im_h; ++y) {
315
426k
    for (int x = 0; x < w; ++x) {
316
379k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
317
3.41M
      for (int k = 0; k < filter_params_x->taps; ++k) {
318
3.03M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
319
3.03M
      }
320
379k
      assert(filter_params_x->taps > 8 ||
321
379k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
322
379k
      im_block[y * im_stride + x] =
323
379k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
324
379k
    }
325
47.5k
  }
326
327
  // vertical filter
328
3.09k
  int16_t *src_vert = im_block + fo_vert * im_stride;
329
3.09k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
330
3.09k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
331
3.09k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
332
28.9k
  for (int y = 0; y < h; ++y) {
333
233k
    for (int x = 0; x < w; ++x) {
334
207k
      int32_t sum = 1 << offset_bits;
335
1.86M
      for (int k = 0; k < filter_params_y->taps; ++k) {
336
1.66M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
337
1.66M
      }
338
207k
      assert(filter_params_y->taps > 8 ||
339
207k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
340
207k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
341
207k
      if (conv_params->do_average) {
342
124k
        int32_t tmp = dst16[y * dst16_stride + x];
343
124k
        if (conv_params->use_dist_wtd_comp_avg) {
344
51.3k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
345
51.3k
          tmp = tmp >> DIST_PRECISION_BITS;
346
73.0k
        } else {
347
73.0k
          tmp += res;
348
73.0k
          tmp = tmp >> 1;
349
73.0k
        }
350
124k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
351
124k
               (1 << (offset_bits - conv_params->round_1 - 1));
352
124k
        dst[y * dst_stride + x] =
353
124k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
354
124k
      } else {
355
83.2k
        dst16[y * dst16_stride + x] = res;
356
83.2k
      }
357
207k
    }
358
25.8k
  }
359
3.09k
}
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.87k
                               ConvolveParams *conv_params) {
366
2.87k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
367
2.87k
  int dst16_stride = conv_params->dst_stride;
368
2.87k
  const int fo_vert = filter_params_y->taps / 2 - 1;
369
2.87k
  const int bits = FILTER_BITS - conv_params->round_0;
370
2.87k
  const int bd = 8;
371
2.87k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
372
2.87k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
373
2.87k
                           (1 << (offset_bits - conv_params->round_1 - 1));
374
2.87k
  const int round_bits =
375
2.87k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
376
377
  // vertical filter
378
2.87k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
379
2.87k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
380
29.9k
  for (int y = 0; y < h; ++y) {
381
244k
    for (int x = 0; x < w; ++x) {
382
217k
      int32_t res = 0;
383
1.95M
      for (int k = 0; k < filter_params_y->taps; ++k) {
384
1.73M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
385
1.73M
      }
386
217k
      res *= (1 << bits);
387
217k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
388
389
217k
      if (conv_params->do_average) {
390
83.4k
        int32_t tmp = dst16[y * dst16_stride + x];
391
83.4k
        if (conv_params->use_dist_wtd_comp_avg) {
392
3.71k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
393
3.71k
          tmp = tmp >> DIST_PRECISION_BITS;
394
79.7k
        } else {
395
79.7k
          tmp += res;
396
79.7k
          tmp = tmp >> 1;
397
79.7k
        }
398
83.4k
        tmp -= round_offset;
399
83.4k
        dst[y * dst_stride + x] =
400
83.4k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
401
133k
      } else {
402
133k
        dst16[y * dst16_stride + x] = res;
403
133k
      }
404
217k
    }
405
27.0k
  }
406
2.87k
}
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.29k
                               ConvolveParams *conv_params) {
413
1.29k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
414
1.29k
  int dst16_stride = conv_params->dst_stride;
415
1.29k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
416
1.29k
  const int bits = FILTER_BITS - conv_params->round_1;
417
1.29k
  const int bd = 8;
418
1.29k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
419
1.29k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
420
1.29k
                           (1 << (offset_bits - conv_params->round_1 - 1));
421
1.29k
  const int round_bits =
422
1.29k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
423
424
  // horizontal filter
425
1.29k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
426
1.29k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
427
13.2k
  for (int y = 0; y < h; ++y) {
428
119k
    for (int x = 0; x < w; ++x) {
429
107k
      int32_t res = 0;
430
969k
      for (int k = 0; k < filter_params_x->taps; ++k) {
431
861k
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
432
861k
      }
433
107k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
434
107k
      res += round_offset;
435
436
107k
      if (conv_params->do_average) {
437
23.6k
        int32_t tmp = dst16[y * dst16_stride + x];
438
23.6k
        if (conv_params->use_dist_wtd_comp_avg) {
439
10.9k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
440
10.9k
          tmp = tmp >> DIST_PRECISION_BITS;
441
12.7k
        } else {
442
12.7k
          tmp += res;
443
12.7k
          tmp = tmp >> 1;
444
12.7k
        }
445
23.6k
        tmp -= round_offset;
446
23.6k
        dst[y * dst_stride + x] =
447
23.6k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
448
84.0k
      } else {
449
84.0k
        dst16[y * dst16_stride + x] = res;
450
84.0k
      }
451
107k
    }
452
11.9k
  }
453
1.29k
}
454
455
void av1_dist_wtd_convolve_2d_copy_c(const uint8_t *src, int src_stride,
456
                                     uint8_t *dst, int dst_stride, int w, int h,
457
5.26k
                                     ConvolveParams *conv_params) {
458
5.26k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
459
5.26k
  int dst16_stride = conv_params->dst_stride;
460
5.26k
  const int bits =
461
5.26k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
462
5.26k
  const int bd = 8;
463
5.26k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
464
5.26k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
465
5.26k
                           (1 << (offset_bits - conv_params->round_1 - 1));
466
467
55.7k
  for (int y = 0; y < h; ++y) {
468
594k
    for (int x = 0; x < w; ++x) {
469
544k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
470
544k
      res += round_offset;
471
472
544k
      if (conv_params->do_average) {
473
109k
        int32_t tmp = dst16[y * dst16_stride + x];
474
109k
        if (conv_params->use_dist_wtd_comp_avg) {
475
22.4k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
476
22.4k
          tmp = tmp >> DIST_PRECISION_BITS;
477
87.0k
        } else {
478
87.0k
          tmp += res;
479
87.0k
          tmp = tmp >> 1;
480
87.0k
        }
481
109k
        tmp -= round_offset;
482
109k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
483
435k
      } else {
484
435k
        dst16[y * dst16_stride + x] = res;
485
435k
      }
486
544k
    }
487
50.5k
  }
488
5.26k
}
489
490
void av1_convolve_2d_scale_c(const uint8_t *src, int src_stride, uint8_t *dst,
491
                             int dst_stride, int w, int h,
492
                             const InterpFilterParams *filter_params_x,
493
                             const InterpFilterParams *filter_params_y,
494
                             const int subpel_x_qn, const int x_step_qn,
495
                             const int subpel_y_qn, const int y_step_qn,
496
10.0k
                             ConvolveParams *conv_params) {
497
10.0k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
498
10.0k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
499
10.0k
             filter_params_y->taps;
500
10.0k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
501
10.0k
  const int dst16_stride = conv_params->dst_stride;
502
10.0k
  const int bits =
503
10.0k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
504
10.0k
  assert(bits >= 0);
505
10.0k
  int im_stride = w;
506
10.0k
  const int fo_vert = filter_params_y->taps / 2 - 1;
507
10.0k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
508
10.0k
  const int bd = 8;
509
510
  // horizontal filter
511
10.0k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
512
144k
  for (int y = 0; y < im_h; ++y) {
513
134k
    int x_qn = subpel_x_qn;
514
1.22M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
515
1.08M
      const uint8_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
516
1.08M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
517
1.08M
      assert(x_filter_idx < SUBPEL_SHIFTS);
518
1.08M
      const int16_t *x_filter =
519
1.08M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
520
1.08M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
521
9.78M
      for (int k = 0; k < filter_params_x->taps; ++k) {
522
8.69M
        sum += x_filter[k] * src_x[k - fo_horiz];
523
8.69M
      }
524
1.08M
      assert(filter_params_x->taps > 8 ||
525
1.08M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
526
1.08M
      im_block[y * im_stride + x] =
527
1.08M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
528
1.08M
    }
529
134k
    src_horiz += src_stride;
530
134k
  }
531
532
  // vertical filter
533
10.0k
  int16_t *src_vert = im_block + fo_vert * im_stride;
534
10.0k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
535
69.5k
  for (int x = 0; x < w; ++x) {
536
59.4k
    int y_qn = subpel_y_qn;
537
753k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
538
694k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
539
694k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
540
694k
      assert(y_filter_idx < SUBPEL_SHIFTS);
541
694k
      const int16_t *y_filter =
542
694k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
543
694k
      int32_t sum = 1 << offset_bits;
544
6.24M
      for (int k = 0; k < filter_params_y->taps; ++k) {
545
5.55M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
546
5.55M
      }
547
694k
      assert(filter_params_y->taps > 8 ||
548
694k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
549
694k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
550
694k
      if (conv_params->is_compound) {
551
196k
        if (conv_params->do_average) {
552
159k
          int32_t tmp = dst16[y * dst16_stride + x];
553
159k
          if (conv_params->use_dist_wtd_comp_avg) {
554
91.5k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
555
91.5k
            tmp = tmp >> DIST_PRECISION_BITS;
556
91.5k
          } else {
557
67.8k
            tmp += res;
558
67.8k
            tmp = tmp >> 1;
559
67.8k
          }
560
          /* Subtract round offset and convolve round */
561
159k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
562
159k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
563
159k
          dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
564
159k
        } else {
565
37.5k
          dst16[y * dst16_stride + x] = res;
566
37.5k
        }
567
497k
      } else {
568
        /* Subtract round offset and convolve round */
569
497k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
570
497k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
571
497k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
572
497k
      }
573
694k
    }
574
59.4k
    src_vert++;
575
59.4k
  }
576
10.0k
}
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.0k
    ConvolveParams *conv_params) {
584
10.0k
  if (conv_params->is_compound) {
585
1.99k
    assert(conv_params->dst != NULL);
586
1.99k
  }
587
10.0k
  av1_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h, filter_params_x,
588
10.0k
                        filter_params_y, subpel_x_qn, x_step_qn, subpel_y_qn,
589
10.0k
                        y_step_qn, conv_params);
590
10.0k
}
591
592
static void convolve_2d_facade_compound(
593
    const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int w,
594
    int h, const InterpFilterParams *filter_params_x,
595
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
596
12.5k
    const int subpel_y_qn, ConvolveParams *conv_params) {
597
12.5k
  const bool need_x = subpel_x_qn != 0;
598
12.5k
  const bool need_y = subpel_y_qn != 0;
599
12.5k
  if (!need_x && !need_y) {
600
5.26k
    av1_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
601
5.26k
                                  conv_params);
602
7.25k
  } else if (need_x && !need_y) {
603
1.29k
    av1_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
604
1.29k
                            filter_params_x, subpel_x_qn, conv_params);
605
5.96k
  } else if (!need_x && need_y) {
606
2.87k
    av1_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
607
2.87k
                            filter_params_y, subpel_y_qn, conv_params);
608
3.09k
  } else {
609
3.09k
    assert(need_y && need_x);
610
3.09k
    av1_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
611
3.09k
                             filter_params_x, filter_params_y, subpel_x_qn,
612
3.09k
                             subpel_y_qn, conv_params);
613
3.09k
  }
614
12.5k
}
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
166k
    const int subpel_y_qn, ConvolveParams *conv_params) {
621
166k
  const bool need_x = subpel_x_qn != 0;
622
166k
  const bool need_y = subpel_y_qn != 0;
623
166k
  if (!need_x && !need_y) {
624
147k
    aom_convolve_copy(src, src_stride, dst, dst_stride, w, h);
625
147k
  } else if (need_x && !need_y) {
626
5.98k
    av1_convolve_x_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
627
5.98k
                      subpel_x_qn, conv_params);
628
12.4k
  } else if (!need_x && need_y) {
629
4.54k
    av1_convolve_y_sr(src, src_stride, dst, dst_stride, w, h, filter_params_y,
630
4.54k
                      subpel_y_qn);
631
7.90k
  } else {
632
7.90k
    assert(need_x && need_y);
633
7.90k
    av1_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
634
7.90k
                       filter_params_y, subpel_x_qn, subpel_y_qn, conv_params);
635
7.90k
  }
636
166k
}
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
193k
                            ConvolveParams *conv_params) {
644
193k
  (void)x_step_q4;
645
193k
  (void)y_step_q4;
646
193k
  (void)dst;
647
193k
  (void)dst_stride;
648
649
193k
  const InterpFilterParams *filter_params_x = interp_filters[0];
650
193k
  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
193k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
655
135k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
656
135k
    assert(!scaled);
657
135k
    if (subpel_x_qn && subpel_y_qn) {
658
1.46k
      av1_convolve_2d_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
659
1.46k
                                 filter_params_x, filter_params_y, subpel_x_qn,
660
1.46k
                                 subpel_y_qn, conv_params);
661
1.46k
      return;
662
134k
    } else if (subpel_x_qn) {
663
1.72k
      av1_convolve_x_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
664
1.72k
                                filter_params_x, subpel_x_qn, conv_params);
665
1.72k
      return;
666
132k
    } else if (subpel_y_qn) {
667
1.74k
      av1_convolve_y_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
668
1.74k
                                filter_params_y, subpel_y_qn);
669
1.74k
      return;
670
1.74k
    }
671
135k
  }
672
673
188k
  if (scaled) {
674
10.0k
    convolve_2d_scale_wrapper(src, src_stride, dst, dst_stride, w, h,
675
10.0k
                              filter_params_x, filter_params_y, subpel_x_qn,
676
10.0k
                              x_step_q4, subpel_y_qn, y_step_q4, conv_params);
677
178k
  } else if (conv_params->is_compound) {
678
12.5k
    convolve_2d_facade_compound(src, src_stride, dst, dst_stride, w, h,
679
12.5k
                                filter_params_x, filter_params_y, subpel_x_qn,
680
12.5k
                                subpel_y_qn, conv_params);
681
166k
  } else {
682
166k
    convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
683
166k
                              filter_params_x, filter_params_y, subpel_x_qn,
684
166k
                              subpel_y_qn, conv_params);
685
166k
  }
686
188k
}
687
688
#if CONFIG_AV1_HIGHBITDEPTH
689
void av1_highbd_convolve_x_sr_c(const uint16_t *src, int src_stride,
690
                                uint16_t *dst, int dst_stride, int w, int h,
691
                                const InterpFilterParams *filter_params_x,
692
                                const int subpel_x_qn,
693
7.11k
                                ConvolveParams *conv_params, int bd) {
694
7.11k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
695
7.11k
  const int bits = FILTER_BITS - conv_params->round_0;
696
697
7.11k
  assert(bits >= 0);
698
7.11k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
699
7.11k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
700
701
  // horizontal filter
702
7.11k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
703
7.11k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
704
50.9k
  for (int y = 0; y < h; ++y) {
705
320k
    for (int x = 0; x < w; ++x) {
706
276k
      int32_t res = 0;
707
2.48M
      for (int k = 0; k < filter_params_x->taps; ++k) {
708
2.21M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
709
2.21M
      }
710
276k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
711
276k
      dst[y * dst_stride + x] =
712
276k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
713
276k
    }
714
43.8k
  }
715
7.11k
}
716
717
void av1_highbd_convolve_y_sr_c(const uint16_t *src, int src_stride,
718
                                uint16_t *dst, int dst_stride, int w, int h,
719
                                const InterpFilterParams *filter_params_y,
720
4.56k
                                const int subpel_y_qn, int bd) {
721
4.56k
  const int fo_vert = filter_params_y->taps / 2 - 1;
722
  // vertical filter
723
4.56k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
724
4.56k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
725
36.6k
  for (int y = 0; y < h; ++y) {
726
257k
    for (int x = 0; x < w; ++x) {
727
225k
      int32_t res = 0;
728
2.03M
      for (int k = 0; k < filter_params_y->taps; ++k) {
729
1.80M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
730
1.80M
      }
731
225k
      dst[y * dst_stride + x] =
732
225k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, FILTER_BITS), bd);
733
225k
    }
734
32.1k
  }
735
4.56k
}
736
737
void av1_highbd_convolve_2d_sr_c(const uint16_t *src, int src_stride,
738
                                 uint16_t *dst, int dst_stride, int w, int h,
739
                                 const InterpFilterParams *filter_params_x,
740
                                 const InterpFilterParams *filter_params_y,
741
                                 const int subpel_x_qn, const int subpel_y_qn,
742
8.18k
                                 ConvolveParams *conv_params, int bd) {
743
8.18k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
744
8.18k
  int im_h = h + filter_params_y->taps - 1;
745
8.18k
  int im_stride = w;
746
8.18k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
747
8.18k
  const int fo_vert = filter_params_y->taps / 2 - 1;
748
8.18k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
749
8.18k
  const int bits =
750
8.18k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
751
8.18k
  assert(bits >= 0);
752
753
  // horizontal filter
754
8.18k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
755
8.18k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
756
8.18k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
757
116k
  for (int y = 0; y < im_h; ++y) {
758
736k
    for (int x = 0; x < w; ++x) {
759
628k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
760
5.65M
      for (int k = 0; k < filter_params_x->taps; ++k) {
761
5.02M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
762
5.02M
      }
763
628k
      assert(filter_params_x->taps > 8 ||
764
628k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
765
628k
      im_block[y * im_stride + x] =
766
628k
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
767
628k
    }
768
107k
  }
769
770
  // vertical filter
771
8.18k
  int16_t *src_vert = im_block + fo_vert * im_stride;
772
8.18k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
773
8.18k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
774
8.18k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
775
58.7k
  for (int y = 0; y < h; ++y) {
776
360k
    for (int x = 0; x < w; ++x) {
777
310k
      int32_t sum = 1 << offset_bits;
778
2.79M
      for (int k = 0; k < filter_params_y->taps; ++k) {
779
2.48M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
780
2.48M
      }
781
310k
      assert(filter_params_y->taps > 8 ||
782
310k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
783
310k
      int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
784
310k
                    ((1 << (offset_bits - conv_params->round_1)) +
785
310k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
786
310k
      dst[y * dst_stride + x] =
787
310k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
788
310k
    }
789
50.5k
  }
790
8.18k
}
791
792
// This function is exactly the same as av1_highbd_convolve_2d_sr_c, and is an
793
// optimized version for intrabc. Use the following 2-tap filter:
794
// DECLARE_ALIGNED(256, static const int16_t,
795
//                 av1_intrabc_bilinear_filter[2 * SUBPEL_SHIFTS]) = {
796
//   128, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
797
//   64,  64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
798
// };
799
void av1_highbd_convolve_2d_sr_intrabc_c(
800
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
801
    int h, const InterpFilterParams *filter_params_x,
802
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
803
21.0k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
804
21.0k
  const int bits =
805
21.0k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
806
21.0k
  assert(bits >= 0);
807
21.0k
  assert(subpel_x_qn == 8);
808
21.0k
  assert(subpel_y_qn == 8);
809
21.0k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
810
21.0k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
811
21.0k
  (void)filter_params_x;
812
21.0k
  (void)subpel_x_qn;
813
21.0k
  (void)filter_params_y;
814
21.0k
  (void)subpel_y_qn;
815
21.0k
  (void)conv_params;
816
817
21.0k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
818
21.0k
  int im_h = h + 1;
819
21.0k
  int im_stride = w;
820
21.0k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
821
822
  // horizontal filter
823
  // explicitly operate for subpel_x_qn = 8.
824
21.0k
  int16_t *im = im_block;
825
178k
  for (int y = 0; y < im_h; ++y) {
826
1.52M
    for (int x = 0; x < w; ++x) {
827
1.36M
      int32_t sum = (1 << (bd + FILTER_BITS - 1)) + 64 * (src[x] + src[x + 1]);
828
1.36M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
829
1.36M
      sum = ROUND_POWER_OF_TWO(sum, conv_params->round_0);
830
1.36M
      im[x] = sum;
831
1.36M
    }
832
157k
    src += src_stride;
833
157k
    im += im_stride;
834
157k
  }
835
836
  // vertical filter
837
  // explicitly operate for subpel_y_qn = 8.
838
21.0k
  int16_t *src_vert = im_block;
839
21.0k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
840
157k
  for (int y = 0; y < h; ++y) {
841
1.34M
    for (int x = 0; x < w; ++x) {
842
1.20M
      const int32_t sum =
843
1.20M
          (1 << offset_bits) + 64 * (src_vert[x] + src_vert[im_stride + x]);
844
1.20M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
845
1.20M
      const int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
846
1.20M
                          ((1 << (offset_bits - conv_params->round_1)) +
847
1.20M
                           (1 << (offset_bits - conv_params->round_1 - 1)));
848
849
1.20M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
850
1.20M
    }
851
136k
    src_vert += im_stride;
852
136k
    dst += dst_stride;
853
136k
  }
854
21.0k
}
855
856
// This function is exactly the same as av1_highbd_convolve_y_sr_c, and is an
857
// optimized version for intrabc.
858
void av1_highbd_convolve_y_sr_intrabc_c(
859
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
860
    int h, const InterpFilterParams *filter_params_y, const int subpel_y_qn,
861
18.7k
    int bd) {
862
18.7k
  assert(subpel_y_qn == 8);
863
18.7k
  assert(filter_params_y->taps == 2);
864
18.7k
  (void)filter_params_y;
865
18.7k
  (void)subpel_y_qn;
866
867
  // vertical filter
868
  // explicitly operate for subpel_y_qn = 8.
869
137k
  for (int y = 0; y < h; ++y) {
870
1.10M
    for (int x = 0; x < w; ++x) {
871
982k
      const int32_t res = src[x] + src[src_stride + x];
872
982k
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, 1), bd);
873
982k
    }
874
118k
    src += src_stride;
875
118k
    dst += dst_stride;
876
118k
  }
877
18.7k
}
878
879
// This function is exactly the same as av1_highbd_convolve_x_sr_c, and is an
880
// optimized version for intrabc.
881
void av1_highbd_convolve_x_sr_intrabc_c(
882
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
883
    int h, const InterpFilterParams *filter_params_x, const int subpel_x_qn,
884
19.1k
    ConvolveParams *conv_params, int bd) {
885
19.1k
  const int bits = FILTER_BITS - conv_params->round_0;
886
19.1k
  assert(bits >= 0);
887
19.1k
  assert(subpel_x_qn == 8);
888
19.1k
  assert(filter_params_x->taps == 2);
889
19.1k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
890
19.1k
  (void)filter_params_x;
891
19.1k
  (void)subpel_x_qn;
892
893
  // horizontal filter
894
  // explicitly operate for subpel_x_qn = 8.
895
141k
  for (int y = 0; y < h; ++y) {
896
1.26M
    for (int x = 0; x < w; ++x) {
897
1.14M
      int32_t res = 64 * (src[x] + src[x + 1]);
898
1.14M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
899
1.14M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
900
1.14M
    }
901
122k
    src += src_stride;
902
122k
    dst += dst_stride;
903
122k
  }
904
19.1k
}
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.42k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
911
4.42k
  int x, y, k;
912
4.42k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
913
4.42k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
914
4.42k
  int dst16_stride = conv_params->dst_stride;
915
4.42k
  int im_h = h + filter_params_y->taps - 1;
916
4.42k
  int im_stride = w;
917
4.42k
  const int fo_vert = filter_params_y->taps / 2 - 1;
918
4.42k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
919
4.42k
  const int round_bits =
920
4.42k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
921
4.42k
  assert(round_bits >= 0);
922
923
  // horizontal filter
924
4.42k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
925
4.42k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
926
4.42k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
927
76.7k
  for (y = 0; y < im_h; ++y) {
928
668k
    for (x = 0; x < w; ++x) {
929
596k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
930
5.36M
      for (k = 0; k < filter_params_x->taps; ++k) {
931
4.77M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
932
4.77M
      }
933
596k
      assert(filter_params_x->taps > 8 ||
934
596k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
935
596k
      (void)bd;
936
596k
      im_block[y * im_stride + x] =
937
596k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
938
596k
    }
939
72.3k
  }
940
941
  // vertical filter
942
4.42k
  int16_t *src_vert = im_block + fo_vert * im_stride;
943
4.42k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
944
4.42k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
945
4.42k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
946
45.7k
  for (y = 0; y < h; ++y) {
947
389k
    for (x = 0; x < w; ++x) {
948
348k
      int32_t sum = 1 << offset_bits;
949
3.13M
      for (k = 0; k < filter_params_y->taps; ++k) {
950
2.78M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
951
2.78M
      }
952
348k
      assert(filter_params_y->taps > 8 ||
953
348k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
954
348k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
955
348k
      if (conv_params->do_average) {
956
114k
        int32_t tmp = dst16[y * dst16_stride + x];
957
114k
        if (conv_params->use_dist_wtd_comp_avg) {
958
11.5k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
959
11.5k
          tmp = tmp >> DIST_PRECISION_BITS;
960
102k
        } else {
961
102k
          tmp += res;
962
102k
          tmp = tmp >> 1;
963
102k
        }
964
114k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
965
114k
               (1 << (offset_bits - conv_params->round_1 - 1));
966
114k
        dst[y * dst_stride + x] =
967
114k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
968
234k
      } else {
969
234k
        dst16[y * dst16_stride + x] = res;
970
234k
      }
971
348k
    }
972
41.3k
  }
973
4.42k
}
974
975
void av1_highbd_dist_wtd_convolve_x_c(const uint16_t *src, int src_stride,
976
                                      uint16_t *dst, int dst_stride, int w,
977
                                      int h,
978
                                      const InterpFilterParams *filter_params_x,
979
                                      const int subpel_x_qn,
980
3.95k
                                      ConvolveParams *conv_params, int bd) {
981
3.95k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
982
3.95k
  int dst16_stride = conv_params->dst_stride;
983
3.95k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
984
3.95k
  const int bits = FILTER_BITS - conv_params->round_1;
985
3.95k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
986
3.95k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
987
3.95k
                           (1 << (offset_bits - conv_params->round_1 - 1));
988
3.95k
  const int round_bits =
989
3.95k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
990
3.95k
  assert(round_bits >= 0);
991
3.95k
  assert(bits >= 0);
992
  // horizontal filter
993
3.95k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
994
3.95k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
995
48.5k
  for (int y = 0; y < h; ++y) {
996
482k
    for (int x = 0; x < w; ++x) {
997
438k
      int32_t res = 0;
998
3.94M
      for (int k = 0; k < filter_params_x->taps; ++k) {
999
3.50M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
1000
3.50M
      }
1001
438k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
1002
438k
      res += round_offset;
1003
1004
438k
      if (conv_params->do_average) {
1005
320k
        int32_t tmp = dst16[y * dst16_stride + x];
1006
320k
        if (conv_params->use_dist_wtd_comp_avg) {
1007
9.85k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1008
9.85k
          tmp = tmp >> DIST_PRECISION_BITS;
1009
310k
        } else {
1010
310k
          tmp += res;
1011
310k
          tmp = tmp >> 1;
1012
310k
        }
1013
320k
        tmp -= round_offset;
1014
320k
        dst[y * dst_stride + x] =
1015
320k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1016
320k
      } else {
1017
117k
        dst16[y * dst16_stride + x] = res;
1018
117k
      }
1019
438k
    }
1020
44.6k
  }
1021
3.95k
}
1022
1023
void av1_highbd_dist_wtd_convolve_y_c(const uint16_t *src, int src_stride,
1024
                                      uint16_t *dst, int dst_stride, int w,
1025
                                      int h,
1026
                                      const InterpFilterParams *filter_params_y,
1027
                                      const int subpel_y_qn,
1028
2.97k
                                      ConvolveParams *conv_params, int bd) {
1029
2.97k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1030
2.97k
  int dst16_stride = conv_params->dst_stride;
1031
2.97k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1032
2.97k
  const int bits = FILTER_BITS - conv_params->round_0;
1033
2.97k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1034
2.97k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1035
2.97k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1036
2.97k
  const int round_bits =
1037
2.97k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
1038
2.97k
  assert(round_bits >= 0);
1039
2.97k
  assert(bits >= 0);
1040
  // vertical filter
1041
2.97k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
1042
2.97k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
1043
34.7k
  for (int y = 0; y < h; ++y) {
1044
362k
    for (int x = 0; x < w; ++x) {
1045
330k
      int32_t res = 0;
1046
2.97M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1047
2.64M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
1048
2.64M
      }
1049
330k
      res *= (1 << bits);
1050
330k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
1051
1052
330k
      if (conv_params->do_average) {
1053
93.8k
        int32_t tmp = dst16[y * dst16_stride + x];
1054
93.8k
        if (conv_params->use_dist_wtd_comp_avg) {
1055
9.92k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1056
9.92k
          tmp = tmp >> DIST_PRECISION_BITS;
1057
83.9k
        } else {
1058
83.9k
          tmp += res;
1059
83.9k
          tmp = tmp >> 1;
1060
83.9k
        }
1061
93.8k
        tmp -= round_offset;
1062
93.8k
        dst[y * dst_stride + x] =
1063
93.8k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1064
236k
      } else {
1065
236k
        dst16[y * dst16_stride + x] = res;
1066
236k
      }
1067
330k
    }
1068
31.8k
  }
1069
2.97k
}
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
14.9k
                                            int bd) {
1076
14.9k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1077
14.9k
  int dst16_stride = conv_params->dst_stride;
1078
14.9k
  const int bits =
1079
14.9k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
1080
14.9k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1081
14.9k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1082
14.9k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1083
14.9k
  assert(bits >= 0);
1084
1085
148k
  for (int y = 0; y < h; ++y) {
1086
1.37M
    for (int x = 0; x < w; ++x) {
1087
1.24M
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
1088
1.24M
      res += round_offset;
1089
1.24M
      if (conv_params->do_average) {
1090
356k
        int32_t tmp = dst16[y * dst16_stride + x];
1091
356k
        if (conv_params->use_dist_wtd_comp_avg) {
1092
262k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1093
262k
          tmp = tmp >> DIST_PRECISION_BITS;
1094
262k
        } else {
1095
93.7k
          tmp += res;
1096
93.7k
          tmp = tmp >> 1;
1097
93.7k
        }
1098
356k
        tmp -= round_offset;
1099
356k
        dst[y * dst_stride + x] =
1100
356k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1101
884k
      } else {
1102
884k
        dst16[y * dst16_stride + x] = res;
1103
884k
      }
1104
1.24M
    }
1105
133k
  }
1106
14.9k
}
1107
1108
void av1_highbd_convolve_2d_scale_c(const uint16_t *src, int src_stride,
1109
                                    uint16_t *dst, int dst_stride, int w, int h,
1110
                                    const InterpFilterParams *filter_params_x,
1111
                                    const InterpFilterParams *filter_params_y,
1112
                                    const int subpel_x_qn, const int x_step_qn,
1113
                                    const int subpel_y_qn, const int y_step_qn,
1114
6.98k
                                    ConvolveParams *conv_params, int bd) {
1115
6.98k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
1116
6.98k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
1117
6.98k
             filter_params_y->taps;
1118
6.98k
  int im_stride = w;
1119
6.98k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1120
6.98k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
1121
6.98k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1122
6.98k
  const int dst16_stride = conv_params->dst_stride;
1123
6.98k
  const int bits =
1124
6.98k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
1125
6.98k
  assert(bits >= 0);
1126
  // horizontal filter
1127
6.98k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
1128
112k
  for (int y = 0; y < im_h; ++y) {
1129
105k
    int x_qn = subpel_x_qn;
1130
1.49M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
1131
1.38M
      const uint16_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
1132
1.38M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1133
1.38M
      assert(x_filter_idx < SUBPEL_SHIFTS);
1134
1.38M
      const int16_t *x_filter =
1135
1.38M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
1136
1.38M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
1137
12.4M
      for (int k = 0; k < filter_params_x->taps; ++k) {
1138
11.1M
        sum += x_filter[k] * src_x[k - fo_horiz];
1139
11.1M
      }
1140
1.38M
      assert(filter_params_x->taps > 8 ||
1141
1.38M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
1142
1.38M
      im_block[y * im_stride + x] =
1143
1.38M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
1144
1.38M
    }
1145
105k
    src_horiz += src_stride;
1146
105k
  }
1147
1148
  // vertical filter
1149
6.98k
  int16_t *src_vert = im_block + fo_vert * im_stride;
1150
6.98k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1151
64.9k
  for (int x = 0; x < w; ++x) {
1152
57.9k
    int y_qn = subpel_y_qn;
1153
1.05M
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
1154
993k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
1155
993k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1156
993k
      assert(y_filter_idx < SUBPEL_SHIFTS);
1157
993k
      const int16_t *y_filter =
1158
993k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
1159
993k
      int32_t sum = 1 << offset_bits;
1160
8.94M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1161
7.94M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
1162
7.94M
      }
1163
993k
      assert(filter_params_y->taps > 8 ||
1164
993k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
1165
993k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
1166
993k
      if (conv_params->is_compound) {
1167
262k
        if (conv_params->do_average) {
1168
82.5k
          int32_t tmp = dst16[y * dst16_stride + x];
1169
82.5k
          if (conv_params->use_dist_wtd_comp_avg) {
1170
52.0k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1171
52.0k
            tmp = tmp >> DIST_PRECISION_BITS;
1172
52.0k
          } else {
1173
30.5k
            tmp += res;
1174
30.5k
            tmp = tmp >> 1;
1175
30.5k
          }
1176
          /* Subtract round offset and convolve round */
1177
82.5k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
1178
82.5k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
1179
82.5k
          dst[y * dst_stride + x] =
1180
82.5k
              clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1181
179k
        } else {
1182
179k
          dst16[y * dst16_stride + x] = res;
1183
179k
        }
1184
731k
      } else {
1185
        /* Subtract round offset and convolve round */
1186
731k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
1187
731k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
1188
731k
        dst[y * dst_stride + x] =
1189
731k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1190
731k
      }
1191
993k
    }
1192
57.9k
    src_vert++;
1193
57.9k
  }
1194
6.98k
}
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
26.2k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1201
26.2k
  const bool need_x = subpel_x_qn != 0;
1202
26.2k
  const bool need_y = subpel_y_qn != 0;
1203
26.2k
  if (!need_x && !need_y) {
1204
14.9k
    av1_highbd_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
1205
14.9k
                                         conv_params, bd);
1206
14.9k
  } else if (need_x && !need_y) {
1207
3.95k
    av1_highbd_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
1208
3.95k
                                   filter_params_x, subpel_x_qn, conv_params,
1209
3.95k
                                   bd);
1210
7.39k
  } else if (!need_x && need_y) {
1211
2.97k
    av1_highbd_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
1212
2.97k
                                   filter_params_y, subpel_y_qn, conv_params,
1213
2.97k
                                   bd);
1214
4.42k
  } else {
1215
4.42k
    assert(need_x && need_y);
1216
4.42k
    av1_highbd_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
1217
4.42k
                                    filter_params_x, filter_params_y,
1218
4.42k
                                    subpel_x_qn, subpel_y_qn, conv_params, bd);
1219
4.42k
  }
1220
26.2k
}
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
106k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1227
106k
  const bool need_x = subpel_x_qn != 0;
1228
106k
  const bool need_y = subpel_y_qn != 0;
1229
1230
106k
  if (!need_x && !need_y) {
1231
86.4k
    aom_highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h);
1232
86.4k
  } else if (need_x && !need_y) {
1233
7.11k
    av1_highbd_convolve_x_sr(src, src_stride, dst, dst_stride, w, h,
1234
7.11k
                             filter_params_x, subpel_x_qn, conv_params, bd);
1235
12.7k
  } else if (!need_x && need_y) {
1236
4.56k
    av1_highbd_convolve_y_sr(src, src_stride, dst, dst_stride, w, h,
1237
4.56k
                             filter_params_y, subpel_y_qn, bd);
1238
8.18k
  } else {
1239
8.18k
    assert(need_x && need_y);
1240
8.18k
    av1_highbd_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h,
1241
8.18k
                              filter_params_x, filter_params_y, subpel_x_qn,
1242
8.18k
                              subpel_y_qn, conv_params, bd);
1243
8.18k
  }
1244
106k
}
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
198k
                                   int bd) {
1253
198k
  (void)x_step_q4;
1254
198k
  (void)y_step_q4;
1255
198k
  (void)dst_stride;
1256
198k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1257
1258
198k
  const InterpFilterParams *filter_params_x = interp_filters[0];
1259
198k
  const InterpFilterParams *filter_params_y = interp_filters[1];
1260
1261
198k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1262
  // 2-tap filter indicates that it is for IntraBC.
1263
198k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
1264
129k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
1265
129k
    assert(!scaled);
1266
129k
    if (subpel_x_qn && subpel_y_qn) {
1267
21.0k
      av1_highbd_convolve_2d_sr_intrabc_c(
1268
21.0k
          src, src_stride, dst, dst_stride, w, h, filter_params_x,
1269
21.0k
          filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1270
21.0k
      return;
1271
108k
    } else if (subpel_x_qn) {
1272
19.1k
      av1_highbd_convolve_x_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1273
19.1k
                                         filter_params_x, subpel_x_qn,
1274
19.1k
                                         conv_params, bd);
1275
19.1k
      return;
1276
89.6k
    } else if (subpel_y_qn) {
1277
18.7k
      av1_highbd_convolve_y_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1278
18.7k
                                         filter_params_y, subpel_y_qn, bd);
1279
18.7k
      return;
1280
18.7k
    }
1281
129k
  }
1282
1283
139k
  if (scaled) {
1284
6.98k
    if (conv_params->is_compound) {
1285
1.49k
      assert(conv_params->dst != NULL);
1286
1.49k
    }
1287
6.98k
    av1_highbd_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h,
1288
6.98k
                                 filter_params_x, filter_params_y, subpel_x_qn,
1289
6.98k
                                 x_step_q4, subpel_y_qn, y_step_q4, conv_params,
1290
6.98k
                                 bd);
1291
132k
  } else if (conv_params->is_compound) {
1292
26.2k
    highbd_convolve_2d_facade_compound(
1293
26.2k
        src, src_stride, dst, dst_stride, w, h, filter_params_x,
1294
26.2k
        filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1295
106k
  } else {
1296
106k
    highbd_convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
1297
106k
                                     filter_params_x, filter_params_y,
1298
106k
                                     subpel_x_qn, subpel_y_qn, conv_params, bd);
1299
106k
  }
1300
139k
}
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
13.3M
static inline int horz_scalar_product(const uint8_t *a, const int16_t *b) {
1319
13.3M
  int sum = 0;
1320
120M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1321
13.3M
  return sum;
1322
13.3M
}
1323
1324
#if CONFIG_AV1_HIGHBITDEPTH
1325
static inline int highbd_horz_scalar_product(const uint16_t *a,
1326
18.0M
                                             const int16_t *b) {
1327
18.0M
  int sum = 0;
1328
162M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1329
18.0M
  return sum;
1330
18.0M
}
1331
#endif
1332
1333
static inline int highbd_vert_scalar_product(const uint16_t *a,
1334
                                             ptrdiff_t a_stride,
1335
29.8M
                                             const int16_t *b) {
1336
29.8M
  int sum = 0;
1337
270M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
1338
29.8M
  return sum;
1339
29.8M
}
1340
1341
34.4k
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
34.4k
  return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
1345
34.4k
}
1346
1347
34.4k
static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
1348
34.4k
  return (int)((const InterpKernel *)(intptr_t)f - base);
1349
34.4k
}
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
7.55k
                                       int round0_bits) {
1356
7.55k
  const int bd = 8;
1357
7.55k
  src -= SUBPEL_TAPS / 2 - 1;
1358
253k
  for (int y = 0; y < h; ++y) {
1359
245k
    int x_q4 = x0_q4;
1360
13.5M
    for (int x = 0; x < w; ++x) {
1361
13.3M
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1362
13.3M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1363
13.3M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1364
13.3M
                           (1 << (bd + FILTER_BITS - 1));
1365
13.3M
      const int sum = horz_scalar_product(src_x, x_filter) + rounding;
1366
13.3M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1367
13.3M
                               WIENER_CLAMP_LIMIT(round0_bits, bd) - 1);
1368
13.3M
      x_q4 += x_step_q4;
1369
13.3M
    }
1370
245k
    src += src_stride;
1371
245k
    dst += dst_stride;
1372
245k
  }
1373
7.55k
}
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
7.54k
                                      int round1_bits) {
1380
7.54k
  const int bd = 8;
1381
7.54k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1382
1383
352k
  for (int x = 0; x < w; ++x) {
1384
344k
    int y_q4 = y0_q4;
1385
12.7M
    for (int y = 0; y < h; ++y) {
1386
12.4M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1387
12.4M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1388
12.4M
      const int rounding =
1389
12.4M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1390
12.4M
          (1 << (bd + round1_bits - 1));
1391
12.4M
      const int sum =
1392
12.4M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1393
12.4M
      dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, round1_bits));
1394
12.4M
      y_q4 += y_step_q4;
1395
12.4M
    }
1396
344k
    ++src;
1397
344k
    ++dst;
1398
344k
  }
1399
7.54k
}
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
7.55k
                                   const WienerConvolveParams *conv_params) {
1407
7.55k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1408
7.55k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1409
1410
7.55k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1411
7.55k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1412
1413
7.55k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1414
7.55k
  const int intermediate_height =
1415
7.55k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS - 1;
1416
7.55k
  memset(temp + (intermediate_height * MAX_SB_SIZE), 0, MAX_SB_SIZE);
1417
1418
7.55k
  assert(w <= MAX_SB_SIZE);
1419
7.55k
  assert(h <= MAX_SB_SIZE);
1420
7.55k
  assert(y_step_q4 <= 32);
1421
7.55k
  assert(x_step_q4 <= 32);
1422
1423
7.55k
  convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1424
7.55k
                             src_stride, temp, MAX_SB_SIZE, filters_x, x0_q4,
1425
7.55k
                             x_step_q4, w, intermediate_height,
1426
7.55k
                             conv_params->round_0);
1427
7.55k
  convolve_add_src_vert_hip(temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1),
1428
7.55k
                            MAX_SB_SIZE, dst, dst_stride, filters_y, y0_q4,
1429
7.55k
                            y_step_q4, w, h, conv_params->round_1);
1430
7.55k
}
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
9.69k
    int x_step_q4, int w, int h, int round0_bits, int bd) {
1439
9.69k
  const int extraprec_clamp_limit = WIENER_CLAMP_LIMIT(round0_bits, bd);
1440
9.69k
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1441
9.69k
  src -= SUBPEL_TAPS / 2 - 1;
1442
426k
  for (int y = 0; y < h; ++y) {
1443
417k
    int x_q4 = x0_q4;
1444
18.4M
    for (int x = 0; x < w; ++x) {
1445
18.0M
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1446
18.0M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1447
18.0M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1448
18.0M
                           (1 << (bd + FILTER_BITS - 1));
1449
18.0M
      const int sum = highbd_horz_scalar_product(src_x, x_filter) + rounding;
1450
18.0M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1451
18.0M
                               extraprec_clamp_limit - 1);
1452
18.0M
      x_q4 += x_step_q4;
1453
18.0M
    }
1454
417k
    src += src_stride;
1455
417k
    dst += dst_stride;
1456
417k
  }
1457
9.69k
}
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
9.69k
    int y_step_q4, int w, int h, int round1_bits, int bd) {
1463
9.69k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1464
9.69k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1465
392k
  for (int x = 0; x < w; ++x) {
1466
383k
    int y_q4 = y0_q4;
1467
18.6M
    for (int y = 0; y < h; ++y) {
1468
18.2M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1469
18.2M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1470
18.2M
      const int rounding =
1471
18.2M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1472
18.2M
          (1 << (bd + round1_bits - 1));
1473
18.2M
      const int sum =
1474
18.2M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1475
18.2M
      dst[y * dst_stride] =
1476
18.2M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, round1_bits), bd);
1477
18.2M
      y_q4 += y_step_q4;
1478
18.2M
    }
1479
383k
    ++src;
1480
383k
    ++dst;
1481
383k
  }
1482
9.69k
}
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
9.68k
    const WienerConvolveParams *conv_params, int bd) {
1489
9.68k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1490
9.68k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1491
1492
9.68k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1493
9.68k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1494
1495
9.68k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1496
9.68k
  const int intermediate_height =
1497
9.68k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
1498
1499
9.68k
  assert(w <= MAX_SB_SIZE);
1500
9.68k
  assert(h <= MAX_SB_SIZE);
1501
9.68k
  assert(y_step_q4 <= 32);
1502
9.68k
  assert(x_step_q4 <= 32);
1503
9.68k
  assert(bd + FILTER_BITS - conv_params->round_0 + 2 <= 16);
1504
1505
9.68k
  highbd_convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1506
9.68k
                                    src_stride, temp, MAX_SB_SIZE, filters_x,
1507
9.68k
                                    x0_q4, x_step_q4, w, intermediate_height,
1508
9.68k
                                    conv_params->round_0, bd);
1509
9.68k
  highbd_convolve_add_src_vert_hip(
1510
9.68k
      temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1), MAX_SB_SIZE, dst, dst_stride,
1511
9.68k
      filters_y, y0_q4, y_step_q4, w, h, conv_params->round_1, bd);
1512
9.68k
}
1513
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1514
#endif  // CONFIG_AV1_HIGHBITDEPTH