Coverage Report

Created: 2026-09-14 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/common/convolve.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include <assert.h>
13
#include <string.h>
14
15
#include "config/aom_dsp_rtcd.h"
16
#include "config/av1_rtcd.h"
17
18
#include "av1/common/av1_common_int.h"
19
#include "av1/common/blockd.h"
20
#include "av1/common/convolve.h"
21
#include "av1/common/filter.h"
22
#include "av1/common/resize.h"
23
#include "aom_dsp/aom_dsp_common.h"
24
#include "aom_ports/mem.h"
25
26
void av1_convolve_horiz_rs_c(const uint8_t *src, int src_stride, uint8_t *dst,
27
                             int dst_stride, int w, int h,
28
                             const int16_t *x_filters, int x0_qn,
29
24.1k
                             int x_step_qn) {
30
24.1k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
31
903k
  for (int y = 0; y < h; ++y) {
32
879k
    int x_qn = x0_qn;
33
307M
    for (int x = 0; x < w; ++x) {
34
306M
      const uint8_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
35
306M
      const int x_filter_idx =
36
306M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
37
306M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
38
306M
      const int16_t *const x_filter =
39
306M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
40
306M
      int sum = 0;
41
2.75G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
42
2.45G
        sum += src_x[k] * x_filter[k];
43
306M
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
44
306M
      x_qn += x_step_qn;
45
306M
    }
46
879k
    src += src_stride;
47
879k
    dst += dst_stride;
48
879k
  }
49
24.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
65.1k
                                    int x_step_qn, int bd) {
56
65.1k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
57
2.38M
  for (int y = 0; y < h; ++y) {
58
2.31M
    int x_qn = x0_qn;
59
132M
    for (int x = 0; x < w; ++x) {
60
130M
      const uint16_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
61
130M
      const int x_filter_idx =
62
130M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
63
130M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
64
130M
      const int16_t *const x_filter =
65
130M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
66
130M
      int sum = 0;
67
1.17G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
68
1.04G
        sum += src_x[k] * x_filter[k];
69
130M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
70
130M
      x_qn += x_step_qn;
71
130M
    }
72
2.31M
    src += src_stride;
73
2.31M
    dst += dst_stride;
74
2.31M
  }
75
65.1k
}
76
#endif  // CONFIG_AV1_HIGHBITDEPTH
77
78
void av1_convolve_2d_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
79
                          int dst_stride, int w, int h,
80
                          const InterpFilterParams *filter_params_x,
81
                          const InterpFilterParams *filter_params_y,
82
                          const int subpel_x_qn, const int subpel_y_qn,
83
8.46k
                          ConvolveParams *conv_params) {
84
8.46k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
85
8.46k
  int im_h = h + filter_params_y->taps - 1;
86
8.46k
  int im_stride = w;
87
8.46k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
88
8.46k
  const int fo_vert = filter_params_y->taps / 2 - 1;
89
8.46k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
90
8.46k
  const int bd = 8;
91
8.46k
  const int bits =
92
8.46k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
93
94
  // horizontal filter
95
8.46k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
96
8.46k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
97
8.46k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
98
116k
  for (int y = 0; y < im_h; ++y) {
99
697k
    for (int x = 0; x < w; ++x) {
100
589k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
101
5.30M
      for (int k = 0; k < filter_params_x->taps; ++k) {
102
4.71M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
103
4.71M
      }
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
589k
      assert(filter_params_x->taps > 8 ||
110
589k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
111
589k
      im_block[y * im_stride + x] =
112
589k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
113
589k
    }
114
108k
  }
115
116
  // vertical filter
117
8.46k
  int16_t *src_vert = im_block + fo_vert * im_stride;
118
8.46k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
119
8.46k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
120
8.46k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
121
57.2k
  for (int y = 0; y < h; ++y) {
122
326k
    for (int x = 0; x < w; ++x) {
123
277k
      int32_t sum = 1 << offset_bits;
124
2.49M
      for (int k = 0; k < filter_params_y->taps; ++k) {
125
2.22M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
126
2.22M
      }
127
277k
      assert(filter_params_y->taps > 8 ||
128
277k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
129
277k
      int16_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
130
277k
                    ((1 << (offset_bits - conv_params->round_1)) +
131
277k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
132
277k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
133
277k
    }
134
48.8k
  }
135
8.46k
}
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
5.03k
                         const int subpel_y_qn) {
141
5.03k
  const int fo_vert = filter_params_y->taps / 2 - 1;
142
143
  // vertical filter
144
5.03k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
145
5.03k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
146
34.8k
  for (int y = 0; y < h; ++y) {
147
217k
    for (int x = 0; x < w; ++x) {
148
187k
      int32_t res = 0;
149
1.68M
      for (int k = 0; k < filter_params_y->taps; ++k) {
150
1.49M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
151
1.49M
      }
152
187k
      dst[y * dst_stride + x] =
153
187k
          clip_pixel(ROUND_POWER_OF_TWO(res, FILTER_BITS));
154
187k
    }
155
29.8k
  }
156
5.03k
}
157
158
void av1_convolve_x_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
159
                         int dst_stride, int w, int h,
160
                         const InterpFilterParams *filter_params_x,
161
6.45k
                         const int subpel_x_qn, ConvolveParams *conv_params) {
162
6.45k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
163
6.45k
  const int bits = FILTER_BITS - conv_params->round_0;
164
165
6.45k
  assert(bits >= 0);
166
6.45k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
167
6.45k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
168
169
  // horizontal filter
170
6.45k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
171
6.45k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
172
173
43.0k
  for (int y = 0; y < h; ++y) {
174
247k
    for (int x = 0; x < w; ++x) {
175
211k
      int32_t res = 0;
176
1.90M
      for (int k = 0; k < filter_params_x->taps; ++k) {
177
1.68M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
178
1.68M
      }
179
211k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
180
211k
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
181
211k
    }
182
36.5k
  }
183
6.45k
}
184
185
// This function is exactly the same as av1_convolve_2d_sr_c, and is an
186
// optimized version for intrabc. Use the following 2-tap filter:
187
// DECLARE_ALIGNED(256, static const int16_t,
188
//                 av1_intrabc_bilinear_filter[2 * SUBPEL_SHIFTS]) = {
189
//   128, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190
//   64,  64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191
// };
192
void av1_convolve_2d_sr_intrabc_c(const uint8_t *src, int src_stride,
193
                                  uint8_t *dst, int dst_stride, int w, int h,
194
                                  const InterpFilterParams *filter_params_x,
195
                                  const InterpFilterParams *filter_params_y,
196
                                  const int subpel_x_qn, const int subpel_y_qn,
197
2.40k
                                  ConvolveParams *conv_params) {
198
2.40k
  assert(subpel_x_qn == 8);
199
2.40k
  assert(subpel_y_qn == 8);
200
2.40k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
201
2.40k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
202
2.40k
  (void)filter_params_x;
203
2.40k
  (void)subpel_x_qn;
204
2.40k
  (void)filter_params_y;
205
2.40k
  (void)subpel_y_qn;
206
2.40k
  (void)conv_params;
207
208
2.40k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
209
2.40k
  int im_h = h + 1;
210
2.40k
  int im_stride = w;
211
2.40k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
212
2.40k
  const int bd = 8;
213
214
  // horizontal filter
215
  // explicitly operate for subpel_x_qn = 8.
216
2.40k
  int16_t *im = im_block;
217
21.1k
  for (int y = 0; y < im_h; ++y) {
218
197k
    for (int x = 0; x < w; ++x) {
219
178k
      const int32_t sum = (1 << bd) + src[x] + src[x + 1];
220
178k
      assert(0 <= sum && sum < (1 << (bd + 2)));
221
178k
      im[x] = sum;
222
178k
    }
223
18.7k
    src += src_stride;
224
18.7k
    im += im_stride;
225
18.7k
  }
226
227
  // vertical filter
228
  // explicitly operate for subpel_y_qn = 8.
229
2.40k
  int16_t *src_vert = im_block;
230
18.7k
  for (int y = 0; y < h; ++y) {
231
175k
    for (int x = 0; x < w; ++x) {
232
159k
      const int32_t sum =
233
159k
          (1 << (bd + 2)) + src_vert[x] + src_vert[im_stride + x];
234
159k
      assert(0 <= sum && sum < (1 << (bd + 4)));
235
159k
      const int16_t res =
236
159k
          ROUND_POWER_OF_TWO(sum, 2) - ((1 << bd) + (1 << (bd - 1)));
237
159k
      dst[x] = clip_pixel(res);
238
159k
    }
239
16.3k
    src_vert += im_stride;
240
16.3k
    dst += dst_stride;
241
16.3k
  }
242
2.40k
}
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
2.76k
                                 const int subpel_y_qn) {
250
2.76k
  assert(subpel_y_qn == 8);
251
2.76k
  assert(filter_params_y->taps == 2);
252
2.76k
  (void)filter_params_y;
253
2.76k
  (void)subpel_y_qn;
254
255
  // vertical filter
256
  // explicitly operate for subpel_y_qn = 8.
257
21.7k
  for (int y = 0; y < h; ++y) {
258
251k
    for (int x = 0; x < w; ++x) {
259
232k
      const int32_t res = src[x] + src[src_stride + x];
260
232k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
261
232k
    }
262
19.0k
    src += src_stride;
263
19.0k
    dst += dst_stride;
264
19.0k
  }
265
2.76k
}
266
267
// This function is exactly the same as av1_convolve_x_sr_c, and is an
268
// optimized version for intrabc.
269
void av1_convolve_x_sr_intrabc_c(const uint8_t *src, int src_stride,
270
                                 uint8_t *dst, int dst_stride, int w, int h,
271
                                 const InterpFilterParams *filter_params_x,
272
                                 const int subpel_x_qn,
273
3.12k
                                 ConvolveParams *conv_params) {
274
3.12k
  assert(subpel_x_qn == 8);
275
3.12k
  assert(filter_params_x->taps == 2);
276
3.12k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
277
3.12k
  (void)filter_params_x;
278
3.12k
  (void)subpel_x_qn;
279
3.12k
  (void)conv_params;
280
281
  // horizontal filter
282
  // explicitly operate for subpel_x_qn = 8.
283
27.4k
  for (int y = 0; y < h; ++y) {
284
337k
    for (int x = 0; x < w; ++x) {
285
313k
      const int32_t res = src[x] + src[x + 1];
286
313k
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(res, 1));
287
313k
    }
288
24.2k
    src += src_stride;
289
24.2k
    dst += dst_stride;
290
24.2k
  }
291
3.12k
}
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.27k
                                ConvolveParams *conv_params) {
299
3.27k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
300
3.27k
  int dst16_stride = conv_params->dst_stride;
301
3.27k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
302
3.27k
  int im_h = h + filter_params_y->taps - 1;
303
3.27k
  int im_stride = w;
304
3.27k
  const int fo_vert = filter_params_y->taps / 2 - 1;
305
3.27k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
306
3.27k
  const int bd = 8;
307
3.27k
  const int round_bits =
308
3.27k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
309
310
  // horizontal filter
311
3.27k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
312
3.27k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
313
3.27k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
314
53.8k
  for (int y = 0; y < im_h; ++y) {
315
456k
    for (int x = 0; x < w; ++x) {
316
406k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
317
3.65M
      for (int k = 0; k < filter_params_x->taps; ++k) {
318
3.24M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
319
3.24M
      }
320
406k
      assert(filter_params_x->taps > 8 ||
321
406k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
322
406k
      im_block[y * im_stride + x] =
323
406k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
324
406k
    }
325
50.6k
  }
326
327
  // vertical filter
328
3.27k
  int16_t *src_vert = im_block + fo_vert * im_stride;
329
3.27k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
330
3.27k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
331
3.27k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
332
30.9k
  for (int y = 0; y < h; ++y) {
333
251k
    for (int x = 0; x < w; ++x) {
334
224k
      int32_t sum = 1 << offset_bits;
335
2.01M
      for (int k = 0; k < filter_params_y->taps; ++k) {
336
1.79M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
337
1.79M
      }
338
224k
      assert(filter_params_y->taps > 8 ||
339
224k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
340
224k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
341
224k
      if (conv_params->do_average) {
342
132k
        int32_t tmp = dst16[y * dst16_stride + x];
343
132k
        if (conv_params->use_dist_wtd_comp_avg) {
344
54.4k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
345
54.4k
          tmp = tmp >> DIST_PRECISION_BITS;
346
77.9k
        } else {
347
77.9k
          tmp += res;
348
77.9k
          tmp = tmp >> 1;
349
77.9k
        }
350
132k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
351
132k
               (1 << (offset_bits - conv_params->round_1 - 1));
352
132k
        dst[y * dst_stride + x] =
353
132k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
354
132k
      } else {
355
91.8k
        dst16[y * dst16_stride + x] = res;
356
91.8k
      }
357
224k
    }
358
27.6k
  }
359
3.27k
}
360
361
void av1_dist_wtd_convolve_y_c(const uint8_t *src, int src_stride, uint8_t *dst,
362
                               int dst_stride, int w, int h,
363
                               const InterpFilterParams *filter_params_y,
364
                               const int subpel_y_qn,
365
3.05k
                               ConvolveParams *conv_params) {
366
3.05k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
367
3.05k
  int dst16_stride = conv_params->dst_stride;
368
3.05k
  const int fo_vert = filter_params_y->taps / 2 - 1;
369
3.05k
  const int bits = FILTER_BITS - conv_params->round_0;
370
3.05k
  const int bd = 8;
371
3.05k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
372
3.05k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
373
3.05k
                           (1 << (offset_bits - conv_params->round_1 - 1));
374
3.05k
  const int round_bits =
375
3.05k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
376
377
  // vertical filter
378
3.05k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
379
3.05k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
380
31.7k
  for (int y = 0; y < h; ++y) {
381
259k
    for (int x = 0; x < w; ++x) {
382
231k
      int32_t res = 0;
383
2.08M
      for (int k = 0; k < filter_params_y->taps; ++k) {
384
1.84M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
385
1.84M
      }
386
231k
      res *= (1 << bits);
387
231k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
388
389
231k
      if (conv_params->do_average) {
390
90.6k
        int32_t tmp = dst16[y * dst16_stride + x];
391
90.6k
        if (conv_params->use_dist_wtd_comp_avg) {
392
3.84k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
393
3.84k
          tmp = tmp >> DIST_PRECISION_BITS;
394
86.8k
        } else {
395
86.8k
          tmp += res;
396
86.8k
          tmp = tmp >> 1;
397
86.8k
        }
398
90.6k
        tmp -= round_offset;
399
90.6k
        dst[y * dst_stride + x] =
400
90.6k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
401
140k
      } else {
402
140k
        dst16[y * dst16_stride + x] = res;
403
140k
      }
404
231k
    }
405
28.6k
  }
406
3.05k
}
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.37k
                               ConvolveParams *conv_params) {
413
1.37k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
414
1.37k
  int dst16_stride = conv_params->dst_stride;
415
1.37k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
416
1.37k
  const int bits = FILTER_BITS - conv_params->round_1;
417
1.37k
  const int bd = 8;
418
1.37k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
419
1.37k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
420
1.37k
                           (1 << (offset_bits - conv_params->round_1 - 1));
421
1.37k
  const int round_bits =
422
1.37k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
423
424
  // horizontal filter
425
1.37k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
426
1.37k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
427
14.0k
  for (int y = 0; y < h; ++y) {
428
127k
    for (int x = 0; x < w; ++x) {
429
114k
      int32_t res = 0;
430
1.02M
      for (int k = 0; k < filter_params_x->taps; ++k) {
431
914k
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
432
914k
      }
433
114k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
434
114k
      res += round_offset;
435
436
114k
      if (conv_params->do_average) {
437
25.1k
        int32_t tmp = dst16[y * dst16_stride + x];
438
25.1k
        if (conv_params->use_dist_wtd_comp_avg) {
439
11.0k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
440
11.0k
          tmp = tmp >> DIST_PRECISION_BITS;
441
14.1k
        } else {
442
14.1k
          tmp += res;
443
14.1k
          tmp = tmp >> 1;
444
14.1k
        }
445
25.1k
        tmp -= round_offset;
446
25.1k
        dst[y * dst_stride + x] =
447
25.1k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
448
89.2k
      } else {
449
89.2k
        dst16[y * dst16_stride + x] = res;
450
89.2k
      }
451
114k
    }
452
12.7k
  }
453
1.37k
}
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
6.02k
                                     ConvolveParams *conv_params) {
458
6.02k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
459
6.02k
  int dst16_stride = conv_params->dst_stride;
460
6.02k
  const int bits =
461
6.02k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
462
6.02k
  const int bd = 8;
463
6.02k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
464
6.02k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
465
6.02k
                           (1 << (offset_bits - conv_params->round_1 - 1));
466
467
63.0k
  for (int y = 0; y < h; ++y) {
468
664k
    for (int x = 0; x < w; ++x) {
469
607k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
470
607k
      res += round_offset;
471
472
607k
      if (conv_params->do_average) {
473
128k
        int32_t tmp = dst16[y * dst16_stride + x];
474
128k
        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
106k
        } else {
478
106k
          tmp += res;
479
106k
          tmp = tmp >> 1;
480
106k
        }
481
128k
        tmp -= round_offset;
482
128k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
483
479k
      } else {
484
479k
        dst16[y * dst16_stride + x] = res;
485
479k
      }
486
607k
    }
487
57.0k
  }
488
6.02k
}
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
13.6k
                             ConvolveParams *conv_params) {
497
13.6k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
498
13.6k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
499
13.6k
             filter_params_y->taps;
500
13.6k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
501
13.6k
  const int dst16_stride = conv_params->dst_stride;
502
13.6k
  const int bits =
503
13.6k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
504
13.6k
  assert(bits >= 0);
505
13.6k
  int im_stride = w;
506
13.6k
  const int fo_vert = filter_params_y->taps / 2 - 1;
507
13.6k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
508
13.6k
  const int bd = 8;
509
510
  // horizontal filter
511
13.6k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
512
192k
  for (int y = 0; y < im_h; ++y) {
513
178k
    int x_qn = subpel_x_qn;
514
1.60M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
515
1.42M
      const uint8_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
516
1.42M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
517
1.42M
      assert(x_filter_idx < SUBPEL_SHIFTS);
518
1.42M
      const int16_t *x_filter =
519
1.42M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
520
1.42M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
521
12.8M
      for (int k = 0; k < filter_params_x->taps; ++k) {
522
11.3M
        sum += x_filter[k] * src_x[k - fo_horiz];
523
11.3M
      }
524
1.42M
      assert(filter_params_x->taps > 8 ||
525
1.42M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
526
1.42M
      im_block[y * im_stride + x] =
527
1.42M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
528
1.42M
    }
529
178k
    src_horiz += src_stride;
530
178k
  }
531
532
  // vertical filter
533
13.6k
  int16_t *src_vert = im_block + fo_vert * im_stride;
534
13.6k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
535
97.0k
  for (int x = 0; x < w; ++x) {
536
83.3k
    int y_qn = subpel_y_qn;
537
1.02M
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
538
940k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
539
940k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
540
940k
      assert(y_filter_idx < SUBPEL_SHIFTS);
541
940k
      const int16_t *y_filter =
542
940k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
543
940k
      int32_t sum = 1 << offset_bits;
544
8.46M
      for (int k = 0; k < filter_params_y->taps; ++k) {
545
7.52M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
546
7.52M
      }
547
940k
      assert(filter_params_y->taps > 8 ||
548
940k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
549
940k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
550
940k
      if (conv_params->is_compound) {
551
295k
        if (conv_params->do_average) {
552
199k
          int32_t tmp = dst16[y * dst16_stride + x];
553
199k
          if (conv_params->use_dist_wtd_comp_avg) {
554
100k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
555
100k
            tmp = tmp >> DIST_PRECISION_BITS;
556
100k
          } else {
557
99.2k
            tmp += res;
558
99.2k
            tmp = tmp >> 1;
559
99.2k
          }
560
          /* Subtract round offset and convolve round */
561
199k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
562
199k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
563
199k
          dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
564
199k
        } else {
565
95.5k
          dst16[y * dst16_stride + x] = res;
566
95.5k
        }
567
645k
      } else {
568
        /* Subtract round offset and convolve round */
569
645k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
570
645k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
571
645k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
572
645k
      }
573
940k
    }
574
83.3k
    src_vert++;
575
83.3k
  }
576
13.6k
}
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
13.6k
    ConvolveParams *conv_params) {
584
13.6k
  if (conv_params->is_compound) {
585
2.92k
    assert(conv_params->dst != NULL);
586
2.92k
  }
587
13.6k
  av1_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h, filter_params_x,
588
13.6k
                        filter_params_y, subpel_x_qn, x_step_qn, subpel_y_qn,
589
13.6k
                        y_step_qn, conv_params);
590
13.6k
}
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
13.7k
    const int subpel_y_qn, ConvolveParams *conv_params) {
597
13.7k
  const bool need_x = subpel_x_qn != 0;
598
13.7k
  const bool need_y = subpel_y_qn != 0;
599
13.7k
  if (!need_x && !need_y) {
600
6.02k
    av1_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
601
6.02k
                                  conv_params);
602
7.71k
  } else if (need_x && !need_y) {
603
1.37k
    av1_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
604
1.37k
                            filter_params_x, subpel_x_qn, conv_params);
605
6.33k
  } else if (!need_x && need_y) {
606
3.05k
    av1_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
607
3.05k
                            filter_params_y, subpel_y_qn, conv_params);
608
3.27k
  } else {
609
3.27k
    assert(need_y && need_x);
610
3.27k
    av1_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
611
3.27k
                             filter_params_x, filter_params_y, subpel_x_qn,
612
3.27k
                             subpel_y_qn, conv_params);
613
3.27k
  }
614
13.7k
}
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
193k
    const int subpel_y_qn, ConvolveParams *conv_params) {
621
193k
  const bool need_x = subpel_x_qn != 0;
622
193k
  const bool need_y = subpel_y_qn != 0;
623
193k
  if (!need_x && !need_y) {
624
173k
    aom_convolve_copy(src, src_stride, dst, dst_stride, w, h);
625
173k
  } else if (need_x && !need_y) {
626
6.45k
    av1_convolve_x_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
627
6.45k
                      subpel_x_qn, conv_params);
628
13.5k
  } else if (!need_x && need_y) {
629
5.03k
    av1_convolve_y_sr(src, src_stride, dst, dst_stride, w, h, filter_params_y,
630
5.03k
                      subpel_y_qn);
631
8.46k
  } else {
632
8.46k
    assert(need_x && need_y);
633
8.46k
    av1_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
634
8.46k
                       filter_params_y, subpel_x_qn, subpel_y_qn, conv_params);
635
8.46k
  }
636
193k
}
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
228k
                            ConvolveParams *conv_params) {
644
228k
  (void)x_step_q4;
645
228k
  (void)y_step_q4;
646
228k
  (void)dst;
647
228k
  (void)dst_stride;
648
649
228k
  const InterpFilterParams *filter_params_x = interp_filters[0];
650
228k
  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
228k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
655
154k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
656
154k
    assert(!scaled);
657
154k
    if (subpel_x_qn && subpel_y_qn) {
658
2.40k
      av1_convolve_2d_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
659
2.40k
                                 filter_params_x, filter_params_y, subpel_x_qn,
660
2.40k
                                 subpel_y_qn, conv_params);
661
2.40k
      return;
662
151k
    } else if (subpel_x_qn) {
663
3.12k
      av1_convolve_x_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
664
3.12k
                                filter_params_x, subpel_x_qn, conv_params);
665
3.12k
      return;
666
148k
    } else if (subpel_y_qn) {
667
2.76k
      av1_convolve_y_sr_intrabc(src, src_stride, dst, dst_stride, w, h,
668
2.76k
                                filter_params_y, subpel_y_qn);
669
2.76k
      return;
670
2.76k
    }
671
154k
  }
672
673
220k
  if (scaled) {
674
13.6k
    convolve_2d_scale_wrapper(src, src_stride, dst, dst_stride, w, h,
675
13.6k
                              filter_params_x, filter_params_y, subpel_x_qn,
676
13.6k
                              x_step_q4, subpel_y_qn, y_step_q4, conv_params);
677
207k
  } else if (conv_params->is_compound) {
678
13.7k
    convolve_2d_facade_compound(src, src_stride, dst, dst_stride, w, h,
679
13.7k
                                filter_params_x, filter_params_y, subpel_x_qn,
680
13.7k
                                subpel_y_qn, conv_params);
681
193k
  } else {
682
193k
    convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
683
193k
                              filter_params_x, filter_params_y, subpel_x_qn,
684
193k
                              subpel_y_qn, conv_params);
685
193k
  }
686
220k
}
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.19k
                                ConvolveParams *conv_params, int bd) {
694
7.19k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
695
7.19k
  const int bits = FILTER_BITS - conv_params->round_0;
696
697
7.19k
  assert(bits >= 0);
698
7.19k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
699
7.19k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
700
701
  // horizontal filter
702
7.19k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
703
7.19k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
704
51.1k
  for (int y = 0; y < h; ++y) {
705
318k
    for (int x = 0; x < w; ++x) {
706
274k
      int32_t res = 0;
707
2.46M
      for (int k = 0; k < filter_params_x->taps; ++k) {
708
2.19M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
709
2.19M
      }
710
274k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
711
274k
      dst[y * dst_stride + x] =
712
274k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
713
274k
    }
714
43.9k
  }
715
7.19k
}
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.76k
                                const int subpel_y_qn, int bd) {
721
4.76k
  const int fo_vert = filter_params_y->taps / 2 - 1;
722
  // vertical filter
723
4.76k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
724
4.76k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
725
38.1k
  for (int y = 0; y < h; ++y) {
726
265k
    for (int x = 0; x < w; ++x) {
727
231k
      int32_t res = 0;
728
2.08M
      for (int k = 0; k < filter_params_y->taps; ++k) {
729
1.85M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
730
1.85M
      }
731
231k
      dst[y * dst_stride + x] =
732
231k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, FILTER_BITS), bd);
733
231k
    }
734
33.4k
  }
735
4.76k
}
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.87k
                                 ConvolveParams *conv_params, int bd) {
743
8.87k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
744
8.87k
  int im_h = h + filter_params_y->taps - 1;
745
8.87k
  int im_stride = w;
746
8.87k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
747
8.87k
  const int fo_vert = filter_params_y->taps / 2 - 1;
748
8.87k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
749
8.87k
  const int bits =
750
8.87k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
751
8.87k
  assert(bits >= 0);
752
753
  // horizontal filter
754
8.87k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
755
8.87k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
756
8.87k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
757
126k
  for (int y = 0; y < im_h; ++y) {
758
869k
    for (int x = 0; x < w; ++x) {
759
752k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
760
6.77M
      for (int k = 0; k < filter_params_x->taps; ++k) {
761
6.02M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
762
6.02M
      }
763
752k
      assert(filter_params_x->taps > 8 ||
764
752k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
765
752k
      im_block[y * im_stride + x] =
766
752k
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
767
752k
    }
768
117k
  }
769
770
  // vertical filter
771
8.87k
  int16_t *src_vert = im_block + fo_vert * im_stride;
772
8.87k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
773
8.87k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
774
8.87k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
775
63.9k
  for (int y = 0; y < h; ++y) {
776
451k
    for (int x = 0; x < w; ++x) {
777
396k
      int32_t sum = 1 << offset_bits;
778
3.56M
      for (int k = 0; k < filter_params_y->taps; ++k) {
779
3.16M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
780
3.16M
      }
781
396k
      assert(filter_params_y->taps > 8 ||
782
396k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
783
396k
      int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
784
396k
                    ((1 << (offset_bits - conv_params->round_1)) +
785
396k
                     (1 << (offset_bits - conv_params->round_1 - 1)));
786
396k
      dst[y * dst_stride + x] =
787
396k
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
788
396k
    }
789
55.1k
  }
790
8.87k
}
791
792
// This function is exactly the same as av1_highbd_convolve_2d_sr_c, and is an
793
// optimized version for intrabc. Use the following 2-tap filter:
794
// DECLARE_ALIGNED(256, static const int16_t,
795
//                 av1_intrabc_bilinear_filter[2 * SUBPEL_SHIFTS]) = {
796
//   128, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
797
//   64,  64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
798
// };
799
void av1_highbd_convolve_2d_sr_intrabc_c(
800
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
801
    int h, const InterpFilterParams *filter_params_x,
802
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
803
22.2k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
804
22.2k
  const int bits =
805
22.2k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
806
22.2k
  assert(bits >= 0);
807
22.2k
  assert(subpel_x_qn == 8);
808
22.2k
  assert(subpel_y_qn == 8);
809
22.2k
  assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
810
22.2k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
811
22.2k
  (void)filter_params_x;
812
22.2k
  (void)subpel_x_qn;
813
22.2k
  (void)filter_params_y;
814
22.2k
  (void)subpel_y_qn;
815
22.2k
  (void)conv_params;
816
817
22.2k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
818
22.2k
  int im_h = h + 1;
819
22.2k
  int im_stride = w;
820
22.2k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
821
822
  // horizontal filter
823
  // explicitly operate for subpel_x_qn = 8.
824
22.2k
  int16_t *im = im_block;
825
190k
  for (int y = 0; y < im_h; ++y) {
826
1.67M
    for (int x = 0; x < w; ++x) {
827
1.50M
      int32_t sum = (1 << (bd + FILTER_BITS - 1)) + 64 * (src[x] + src[x + 1]);
828
1.50M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
829
1.50M
      sum = ROUND_POWER_OF_TWO(sum, conv_params->round_0);
830
1.50M
      im[x] = sum;
831
1.50M
    }
832
167k
    src += src_stride;
833
167k
    im += im_stride;
834
167k
  }
835
836
  // vertical filter
837
  // explicitly operate for subpel_y_qn = 8.
838
22.2k
  int16_t *src_vert = im_block;
839
22.2k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
840
167k
  for (int y = 0; y < h; ++y) {
841
1.48M
    for (int x = 0; x < w; ++x) {
842
1.34M
      const int32_t sum =
843
1.34M
          (1 << offset_bits) + 64 * (src_vert[x] + src_vert[im_stride + x]);
844
1.34M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
845
1.34M
      const int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
846
1.34M
                          ((1 << (offset_bits - conv_params->round_1)) +
847
1.34M
                           (1 << (offset_bits - conv_params->round_1 - 1)));
848
849
1.34M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
850
1.34M
    }
851
145k
    src_vert += im_stride;
852
145k
    dst += dst_stride;
853
145k
  }
854
22.2k
}
855
856
// This function is exactly the same as av1_highbd_convolve_y_sr_c, and is an
857
// optimized version for intrabc.
858
void av1_highbd_convolve_y_sr_intrabc_c(
859
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
860
    int h, const InterpFilterParams *filter_params_y, const int subpel_y_qn,
861
19.6k
    int bd) {
862
19.6k
  assert(subpel_y_qn == 8);
863
19.6k
  assert(filter_params_y->taps == 2);
864
19.6k
  (void)filter_params_y;
865
19.6k
  (void)subpel_y_qn;
866
867
  // vertical filter
868
  // explicitly operate for subpel_y_qn = 8.
869
144k
  for (int y = 0; y < h; ++y) {
870
1.17M
    for (int x = 0; x < w; ++x) {
871
1.04M
      const int32_t res = src[x] + src[src_stride + x];
872
1.04M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, 1), bd);
873
1.04M
    }
874
125k
    src += src_stride;
875
125k
    dst += dst_stride;
876
125k
  }
877
19.6k
}
878
879
// This function is exactly the same as av1_highbd_convolve_x_sr_c, and is an
880
// optimized version for intrabc.
881
void av1_highbd_convolve_x_sr_intrabc_c(
882
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
883
    int h, const InterpFilterParams *filter_params_x, const int subpel_x_qn,
884
20.2k
    ConvolveParams *conv_params, int bd) {
885
20.2k
  const int bits = FILTER_BITS - conv_params->round_0;
886
20.2k
  assert(bits >= 0);
887
20.2k
  assert(subpel_x_qn == 8);
888
20.2k
  assert(filter_params_x->taps == 2);
889
20.2k
  assert((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS);
890
20.2k
  (void)filter_params_x;
891
20.2k
  (void)subpel_x_qn;
892
893
  // horizontal filter
894
  // explicitly operate for subpel_x_qn = 8.
895
150k
  for (int y = 0; y < h; ++y) {
896
1.38M
    for (int x = 0; x < w; ++x) {
897
1.25M
      int32_t res = 64 * (src[x] + src[x + 1]);
898
1.25M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
899
1.25M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
900
1.25M
    }
901
130k
    src += src_stride;
902
130k
    dst += dst_stride;
903
130k
  }
904
20.2k
}
905
906
void av1_highbd_dist_wtd_convolve_2d_c(
907
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
908
    int h, const InterpFilterParams *filter_params_x,
909
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
910
4.91k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
911
4.91k
  int x, y, k;
912
4.91k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
913
4.91k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
914
4.91k
  int dst16_stride = conv_params->dst_stride;
915
4.91k
  int im_h = h + filter_params_y->taps - 1;
916
4.91k
  int im_stride = w;
917
4.91k
  const int fo_vert = filter_params_y->taps / 2 - 1;
918
4.91k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
919
4.91k
  const int round_bits =
920
4.91k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
921
4.91k
  assert(round_bits >= 0);
922
923
  // horizontal filter
924
4.91k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
925
4.91k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
926
4.91k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
927
85.3k
  for (y = 0; y < im_h; ++y) {
928
742k
    for (x = 0; x < w; ++x) {
929
662k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
930
5.95M
      for (k = 0; k < filter_params_x->taps; ++k) {
931
5.29M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
932
5.29M
      }
933
662k
      assert(filter_params_x->taps > 8 ||
934
662k
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
935
662k
      (void)bd;
936
662k
      im_block[y * im_stride + x] =
937
662k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
938
662k
    }
939
80.4k
  }
940
941
  // vertical filter
942
4.91k
  int16_t *src_vert = im_block + fo_vert * im_stride;
943
4.91k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
944
4.91k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
945
4.91k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
946
50.9k
  for (y = 0; y < h; ++y) {
947
432k
    for (x = 0; x < w; ++x) {
948
386k
      int32_t sum = 1 << offset_bits;
949
3.48M
      for (k = 0; k < filter_params_y->taps; ++k) {
950
3.09M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
951
3.09M
      }
952
386k
      assert(filter_params_y->taps > 8 ||
953
386k
             (0 <= sum && sum < (1 << (offset_bits + 2))));
954
386k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
955
386k
      if (conv_params->do_average) {
956
130k
        int32_t tmp = dst16[y * dst16_stride + x];
957
130k
        if (conv_params->use_dist_wtd_comp_avg) {
958
13.6k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
959
13.6k
          tmp = tmp >> DIST_PRECISION_BITS;
960
116k
        } else {
961
116k
          tmp += res;
962
116k
          tmp = tmp >> 1;
963
116k
        }
964
130k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
965
130k
               (1 << (offset_bits - conv_params->round_1 - 1));
966
130k
        dst[y * dst_stride + x] =
967
130k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
968
256k
      } else {
969
256k
        dst16[y * dst16_stride + x] = res;
970
256k
      }
971
386k
    }
972
45.9k
  }
973
4.91k
}
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
4.33k
                                      ConvolveParams *conv_params, int bd) {
981
4.33k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
982
4.33k
  int dst16_stride = conv_params->dst_stride;
983
4.33k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
984
4.33k
  const int bits = FILTER_BITS - conv_params->round_1;
985
4.33k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
986
4.33k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
987
4.33k
                           (1 << (offset_bits - conv_params->round_1 - 1));
988
4.33k
  const int round_bits =
989
4.33k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
990
4.33k
  assert(round_bits >= 0);
991
4.33k
  assert(bits >= 0);
992
  // horizontal filter
993
4.33k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
994
4.33k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
995
53.2k
  for (int y = 0; y < h; ++y) {
996
526k
    for (int x = 0; x < w; ++x) {
997
477k
      int32_t res = 0;
998
4.29M
      for (int k = 0; k < filter_params_x->taps; ++k) {
999
3.82M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
1000
3.82M
      }
1001
477k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
1002
477k
      res += round_offset;
1003
1004
477k
      if (conv_params->do_average) {
1005
351k
        int32_t tmp = dst16[y * dst16_stride + x];
1006
351k
        if (conv_params->use_dist_wtd_comp_avg) {
1007
10.8k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1008
10.8k
          tmp = tmp >> DIST_PRECISION_BITS;
1009
340k
        } else {
1010
340k
          tmp += res;
1011
340k
          tmp = tmp >> 1;
1012
340k
        }
1013
351k
        tmp -= round_offset;
1014
351k
        dst[y * dst_stride + x] =
1015
351k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1016
351k
      } else {
1017
126k
        dst16[y * dst16_stride + x] = res;
1018
126k
      }
1019
477k
    }
1020
48.8k
  }
1021
4.33k
}
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.32k
                                      ConvolveParams *conv_params, int bd) {
1029
3.32k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1030
3.32k
  int dst16_stride = conv_params->dst_stride;
1031
3.32k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1032
3.32k
  const int bits = FILTER_BITS - conv_params->round_0;
1033
3.32k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1034
3.32k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1035
3.32k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1036
3.32k
  const int round_bits =
1037
3.32k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
1038
3.32k
  assert(round_bits >= 0);
1039
3.32k
  assert(bits >= 0);
1040
  // vertical filter
1041
3.32k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
1042
3.32k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
1043
38.7k
  for (int y = 0; y < h; ++y) {
1044
396k
    for (int x = 0; x < w; ++x) {
1045
360k
      int32_t res = 0;
1046
3.24M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1047
2.88M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
1048
2.88M
      }
1049
360k
      res *= (1 << bits);
1050
360k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
1051
1052
360k
      if (conv_params->do_average) {
1053
102k
        int32_t tmp = dst16[y * dst16_stride + x];
1054
102k
        if (conv_params->use_dist_wtd_comp_avg) {
1055
10.8k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1056
10.8k
          tmp = tmp >> DIST_PRECISION_BITS;
1057
91.7k
        } else {
1058
91.7k
          tmp += res;
1059
91.7k
          tmp = tmp >> 1;
1060
91.7k
        }
1061
102k
        tmp -= round_offset;
1062
102k
        dst[y * dst_stride + x] =
1063
102k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
1064
257k
      } else {
1065
257k
        dst16[y * dst16_stride + x] = res;
1066
257k
      }
1067
360k
    }
1068
35.4k
  }
1069
3.32k
}
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
16.5k
                                            int bd) {
1076
16.5k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1077
16.5k
  int dst16_stride = conv_params->dst_stride;
1078
16.5k
  const int bits =
1079
16.5k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
1080
16.5k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1081
16.5k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
1082
16.5k
                           (1 << (offset_bits - conv_params->round_1 - 1));
1083
16.5k
  assert(bits >= 0);
1084
1085
164k
  for (int y = 0; y < h; ++y) {
1086
1.51M
    for (int x = 0; x < w; ++x) {
1087
1.36M
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
1088
1.36M
      res += round_offset;
1089
1.36M
      if (conv_params->do_average) {
1090
392k
        int32_t tmp = dst16[y * dst16_stride + x];
1091
392k
        if (conv_params->use_dist_wtd_comp_avg) {
1092
286k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1093
286k
          tmp = tmp >> DIST_PRECISION_BITS;
1094
286k
        } else {
1095
106k
          tmp += res;
1096
106k
          tmp = tmp >> 1;
1097
106k
        }
1098
392k
        tmp -= round_offset;
1099
392k
        dst[y * dst_stride + x] =
1100
392k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1101
970k
      } else {
1102
970k
        dst16[y * dst16_stride + x] = res;
1103
970k
      }
1104
1.36M
    }
1105
147k
  }
1106
16.5k
}
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.93k
                                    ConvolveParams *conv_params, int bd) {
1115
9.93k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
1116
9.93k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
1117
9.93k
             filter_params_y->taps;
1118
9.93k
  int im_stride = w;
1119
9.93k
  const int fo_vert = filter_params_y->taps / 2 - 1;
1120
9.93k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
1121
9.93k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
1122
9.93k
  const int dst16_stride = conv_params->dst_stride;
1123
9.93k
  const int bits =
1124
9.93k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
1125
9.93k
  assert(bits >= 0);
1126
  // horizontal filter
1127
9.93k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
1128
152k
  for (int y = 0; y < im_h; ++y) {
1129
143k
    int x_qn = subpel_x_qn;
1130
1.79M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
1131
1.65M
      const uint16_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
1132
1.65M
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1133
1.65M
      assert(x_filter_idx < SUBPEL_SHIFTS);
1134
1.65M
      const int16_t *x_filter =
1135
1.65M
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
1136
1.65M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
1137
14.9M
      for (int k = 0; k < filter_params_x->taps; ++k) {
1138
13.2M
        sum += x_filter[k] * src_x[k - fo_horiz];
1139
13.2M
      }
1140
1.65M
      assert(filter_params_x->taps > 8 ||
1141
1.65M
             (0 <= sum && sum < (1 << (bd + FILTER_BITS + 1))));
1142
1.65M
      im_block[y * im_stride + x] =
1143
1.65M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
1144
1.65M
    }
1145
143k
    src_horiz += src_stride;
1146
143k
  }
1147
1148
  // vertical filter
1149
9.93k
  int16_t *src_vert = im_block + fo_vert * im_stride;
1150
9.93k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
1151
85.5k
  for (int x = 0; x < w; ++x) {
1152
75.6k
    int y_qn = subpel_y_qn;
1153
1.22M
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
1154
1.14M
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
1155
1.14M
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
1156
1.14M
      assert(y_filter_idx < SUBPEL_SHIFTS);
1157
1.14M
      const int16_t *y_filter =
1158
1.14M
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
1159
1.14M
      int32_t sum = 1 << offset_bits;
1160
10.3M
      for (int k = 0; k < filter_params_y->taps; ++k) {
1161
9.15M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
1162
9.15M
      }
1163
1.14M
      assert(filter_params_y->taps > 8 ||
1164
1.14M
             (0 <= sum && sum < (1 << (offset_bits + 2))));
1165
1.14M
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
1166
1.14M
      if (conv_params->is_compound) {
1167
314k
        if (conv_params->do_average) {
1168
100k
          int32_t tmp = dst16[y * dst16_stride + x];
1169
100k
          if (conv_params->use_dist_wtd_comp_avg) {
1170
54.4k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
1171
54.4k
            tmp = tmp >> DIST_PRECISION_BITS;
1172
54.4k
          } else {
1173
45.7k
            tmp += res;
1174
45.7k
            tmp = tmp >> 1;
1175
45.7k
          }
1176
          /* Subtract round offset and convolve round */
1177
100k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
1178
100k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
1179
100k
          dst[y * dst_stride + x] =
1180
100k
              clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1181
214k
        } else {
1182
214k
          dst16[y * dst16_stride + x] = res;
1183
214k
        }
1184
829k
      } else {
1185
        /* Subtract round offset and convolve round */
1186
829k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
1187
829k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
1188
829k
        dst[y * dst_stride + x] =
1189
829k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
1190
829k
      }
1191
1.14M
    }
1192
75.6k
    src_vert++;
1193
75.6k
  }
1194
9.93k
}
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
29.0k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1201
29.0k
  const bool need_x = subpel_x_qn != 0;
1202
29.0k
  const bool need_y = subpel_y_qn != 0;
1203
29.0k
  if (!need_x && !need_y) {
1204
16.5k
    av1_highbd_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
1205
16.5k
                                         conv_params, bd);
1206
16.5k
  } else if (need_x && !need_y) {
1207
4.33k
    av1_highbd_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
1208
4.33k
                                   filter_params_x, subpel_x_qn, conv_params,
1209
4.33k
                                   bd);
1210
8.24k
  } else if (!need_x && need_y) {
1211
3.32k
    av1_highbd_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
1212
3.32k
                                   filter_params_y, subpel_y_qn, conv_params,
1213
3.32k
                                   bd);
1214
4.91k
  } else {
1215
4.91k
    assert(need_x && need_y);
1216
4.91k
    av1_highbd_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
1217
4.91k
                                    filter_params_x, filter_params_y,
1218
4.91k
                                    subpel_x_qn, subpel_y_qn, conv_params, bd);
1219
4.91k
  }
1220
29.0k
}
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
117k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1227
117k
  const bool need_x = subpel_x_qn != 0;
1228
117k
  const bool need_y = subpel_y_qn != 0;
1229
1230
117k
  if (!need_x && !need_y) {
1231
96.6k
    aom_highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h);
1232
96.6k
  } else if (need_x && !need_y) {
1233
7.19k
    av1_highbd_convolve_x_sr(src, src_stride, dst, dst_stride, w, h,
1234
7.19k
                             filter_params_x, subpel_x_qn, conv_params, bd);
1235
13.6k
  } else if (!need_x && need_y) {
1236
4.76k
    av1_highbd_convolve_y_sr(src, src_stride, dst, dst_stride, w, h,
1237
4.76k
                             filter_params_y, subpel_y_qn, bd);
1238
8.87k
  } else {
1239
8.87k
    assert(need_x && need_y);
1240
8.87k
    av1_highbd_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h,
1241
8.87k
                              filter_params_x, filter_params_y, subpel_x_qn,
1242
8.87k
                              subpel_y_qn, conv_params, bd);
1243
8.87k
  }
1244
117k
}
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
218k
                                   int bd) {
1253
218k
  (void)x_step_q4;
1254
218k
  (void)y_step_q4;
1255
218k
  (void)dst_stride;
1256
218k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1257
1258
218k
  const InterpFilterParams *filter_params_x = interp_filters[0];
1259
218k
  const InterpFilterParams *filter_params_y = interp_filters[1];
1260
1261
218k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1262
  // 2-tap filter indicates that it is for IntraBC.
1263
218k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
1264
140k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
1265
140k
    assert(!scaled);
1266
140k
    if (subpel_x_qn && subpel_y_qn) {
1267
22.2k
      av1_highbd_convolve_2d_sr_intrabc_c(
1268
22.2k
          src, src_stride, dst, dst_stride, w, h, filter_params_x,
1269
22.2k
          filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1270
22.2k
      return;
1271
118k
    } else if (subpel_x_qn) {
1272
20.2k
      av1_highbd_convolve_x_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1273
20.2k
                                         filter_params_x, subpel_x_qn,
1274
20.2k
                                         conv_params, bd);
1275
20.2k
      return;
1276
97.9k
    } else if (subpel_y_qn) {
1277
19.6k
      av1_highbd_convolve_y_sr_intrabc_c(src, src_stride, dst, dst_stride, w, h,
1278
19.6k
                                         filter_params_y, subpel_y_qn, bd);
1279
19.6k
      return;
1280
19.6k
    }
1281
140k
  }
1282
1283
156k
  if (scaled) {
1284
9.93k
    if (conv_params->is_compound) {
1285
2.03k
      assert(conv_params->dst != NULL);
1286
2.03k
    }
1287
9.93k
    av1_highbd_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h,
1288
9.93k
                                 filter_params_x, filter_params_y, subpel_x_qn,
1289
9.93k
                                 x_step_q4, subpel_y_qn, y_step_q4, conv_params,
1290
9.93k
                                 bd);
1291
146k
  } else if (conv_params->is_compound) {
1292
29.0k
    highbd_convolve_2d_facade_compound(
1293
29.0k
        src, src_stride, dst, dst_stride, w, h, filter_params_x,
1294
29.0k
        filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1295
117k
  } else {
1296
117k
    highbd_convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
1297
117k
                                     filter_params_x, filter_params_y,
1298
117k
                                     subpel_x_qn, subpel_y_qn, conv_params, bd);
1299
117k
  }
1300
156k
}
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
81.5M
static inline int horz_scalar_product(const uint8_t *a, const int16_t *b) {
1319
81.5M
  int sum = 0;
1320
733M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1321
81.5M
  return sum;
1322
81.5M
}
1323
1324
#if CONFIG_AV1_HIGHBITDEPTH
1325
static inline int highbd_horz_scalar_product(const uint16_t *a,
1326
28.3M
                                             const int16_t *b) {
1327
28.3M
  int sum = 0;
1328
254M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1329
28.3M
  return sum;
1330
28.3M
}
1331
#endif
1332
1333
static inline int highbd_vert_scalar_product(const uint16_t *a,
1334
                                             ptrdiff_t a_stride,
1335
99.8M
                                             const int16_t *b) {
1336
99.8M
  int sum = 0;
1337
880M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
1338
99.8M
  return sum;
1339
99.8M
}
1340
1341
81.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
81.2k
  return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
1345
81.2k
}
1346
1347
81.2k
static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
1348
81.2k
  return (int)((const InterpKernel *)(intptr_t)f - base);
1349
81.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
26.7k
                                       int round0_bits) {
1356
26.7k
  const int bd = 8;
1357
26.7k
  src -= SUBPEL_TAPS / 2 - 1;
1358
1.40M
  for (int y = 0; y < h; ++y) {
1359
1.37M
    int x_q4 = x0_q4;
1360
82.9M
    for (int x = 0; x < w; ++x) {
1361
81.5M
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1362
81.5M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1363
81.5M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1364
81.5M
                           (1 << (bd + FILTER_BITS - 1));
1365
81.5M
      const int sum = horz_scalar_product(src_x, x_filter) + rounding;
1366
81.5M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1367
81.5M
                               WIENER_CLAMP_LIMIT(round0_bits, bd) - 1);
1368
81.5M
      x_q4 += x_step_q4;
1369
81.5M
    }
1370
1.37M
    src += src_stride;
1371
1.37M
    dst += dst_stride;
1372
1.37M
  }
1373
26.7k
}
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
26.7k
                                      int round1_bits) {
1380
26.7k
  const int bd = 8;
1381
26.7k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1382
1383
1.54M
  for (int x = 0; x < w; ++x) {
1384
1.51M
    int y_q4 = y0_q4;
1385
74.0M
    for (int y = 0; y < h; ++y) {
1386
72.5M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1387
72.5M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1388
72.5M
      const int rounding =
1389
72.5M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1390
72.5M
          (1 << (bd + round1_bits - 1));
1391
72.5M
      const int sum =
1392
72.5M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1393
72.5M
      dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, round1_bits));
1394
72.5M
      y_q4 += y_step_q4;
1395
72.5M
    }
1396
1.51M
    ++src;
1397
1.51M
    ++dst;
1398
1.51M
  }
1399
26.7k
}
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
26.7k
                                   const WienerConvolveParams *conv_params) {
1407
26.7k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1408
26.7k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1409
1410
26.7k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1411
26.7k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1412
1413
26.7k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1414
26.7k
  const int intermediate_height =
1415
26.7k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS - 1;
1416
26.7k
  memset(temp + (intermediate_height * MAX_SB_SIZE), 0, MAX_SB_SIZE);
1417
1418
26.7k
  assert(w <= MAX_SB_SIZE);
1419
26.7k
  assert(h <= MAX_SB_SIZE);
1420
26.7k
  assert(y_step_q4 <= 32);
1421
26.7k
  assert(x_step_q4 <= 32);
1422
1423
26.7k
  convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1424
26.7k
                             src_stride, temp, MAX_SB_SIZE, filters_x, x0_q4,
1425
26.7k
                             x_step_q4, w, intermediate_height,
1426
26.7k
                             conv_params->round_0);
1427
26.7k
  convolve_add_src_vert_hip(temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1),
1428
26.7k
                            MAX_SB_SIZE, dst, dst_stride, filters_y, y0_q4,
1429
26.7k
                            y_step_q4, w, h, conv_params->round_1);
1430
26.7k
}
1431
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1432
1433
#if CONFIG_AV1_HIGHBITDEPTH
1434
#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1435
static void highbd_convolve_add_src_horiz_hip(
1436
    const uint8_t *src8, ptrdiff_t src_stride, uint16_t *dst,
1437
    ptrdiff_t dst_stride, const InterpKernel *x_filters, int x0_q4,
1438
13.8k
    int x_step_q4, int w, int h, int round0_bits, int bd) {
1439
13.8k
  const int extraprec_clamp_limit = WIENER_CLAMP_LIMIT(round0_bits, bd);
1440
13.8k
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1441
13.8k
  src -= SUBPEL_TAPS / 2 - 1;
1442
649k
  for (int y = 0; y < h; ++y) {
1443
635k
    int x_q4 = x0_q4;
1444
28.9M
    for (int x = 0; x < w; ++x) {
1445
28.3M
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1446
28.3M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1447
28.3M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1448
28.3M
                           (1 << (bd + FILTER_BITS - 1));
1449
28.3M
      const int sum = highbd_horz_scalar_product(src_x, x_filter) + rounding;
1450
28.3M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1451
28.3M
                               extraprec_clamp_limit - 1);
1452
28.3M
      x_q4 += x_step_q4;
1453
28.3M
    }
1454
635k
    src += src_stride;
1455
635k
    dst += dst_stride;
1456
635k
  }
1457
13.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
13.8k
    int y_step_q4, int w, int h, int round1_bits, int bd) {
1463
13.8k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1464
13.8k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1465
582k
  for (int x = 0; x < w; ++x) {
1466
568k
    int y_q4 = y0_q4;
1467
28.8M
    for (int y = 0; y < h; ++y) {
1468
28.3M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1469
28.3M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1470
28.3M
      const int rounding =
1471
28.3M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1472
28.3M
          (1 << (bd + round1_bits - 1));
1473
28.3M
      const int sum =
1474
28.3M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1475
28.3M
      dst[y * dst_stride] =
1476
28.3M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, round1_bits), bd);
1477
28.3M
      y_q4 += y_step_q4;
1478
28.3M
    }
1479
568k
    ++src;
1480
568k
    ++dst;
1481
568k
  }
1482
13.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
13.8k
    const WienerConvolveParams *conv_params, int bd) {
1489
13.8k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1490
13.8k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1491
1492
13.8k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1493
13.8k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1494
1495
13.8k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1496
13.8k
  const int intermediate_height =
1497
13.8k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
1498
1499
13.8k
  assert(w <= MAX_SB_SIZE);
1500
13.8k
  assert(h <= MAX_SB_SIZE);
1501
13.8k
  assert(y_step_q4 <= 32);
1502
13.8k
  assert(x_step_q4 <= 32);
1503
13.8k
  assert(bd + FILTER_BITS - conv_params->round_0 + 2 <= 16);
1504
1505
13.8k
  highbd_convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1506
13.8k
                                    src_stride, temp, MAX_SB_SIZE, filters_x,
1507
13.8k
                                    x0_q4, x_step_q4, w, intermediate_height,
1508
13.8k
                                    conv_params->round_0, bd);
1509
13.8k
  highbd_convolve_add_src_vert_hip(
1510
13.8k
      temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1), MAX_SB_SIZE, dst, dst_stride,
1511
13.8k
      filters_y, y0_q4, y_step_q4, w, h, conv_params->round_1, bd);
1512
13.8k
}
1513
#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
1514
#endif  // CONFIG_AV1_HIGHBITDEPTH