Coverage Report

Created: 2025-12-31 07:53

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
37.5k
                             int x_step_qn) {
30
37.5k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
31
1.37M
  for (int y = 0; y < h; ++y) {
32
1.33M
    int x_qn = x0_qn;
33
203M
    for (int x = 0; x < w; ++x) {
34
202M
      const uint8_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
35
202M
      const int x_filter_idx =
36
202M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
37
202M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
38
202M
      const int16_t *const x_filter =
39
202M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
40
202M
      int sum = 0;
41
1.82G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
42
1.62G
        sum += src_x[k] * x_filter[k];
43
202M
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
44
202M
      x_qn += x_step_qn;
45
202M
    }
46
1.33M
    src += src_stride;
47
1.33M
    dst += dst_stride;
48
1.33M
  }
49
37.5k
}
50
51
void av1_highbd_convolve_horiz_rs_c(const uint16_t *src, int src_stride,
52
                                    uint16_t *dst, int dst_stride, int w, int h,
53
                                    const int16_t *x_filters, int x0_qn,
54
127k
                                    int x_step_qn, int bd) {
55
127k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
56
4.86M
  for (int y = 0; y < h; ++y) {
57
4.73M
    int x_qn = x0_qn;
58
308M
    for (int x = 0; x < w; ++x) {
59
303M
      const uint16_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
60
303M
      const int x_filter_idx =
61
303M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
62
303M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
63
303M
      const int16_t *const x_filter =
64
303M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
65
303M
      int sum = 0;
66
2.73G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
67
2.43G
        sum += src_x[k] * x_filter[k];
68
303M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
69
303M
      x_qn += x_step_qn;
70
303M
    }
71
4.73M
    src += src_stride;
72
4.73M
    dst += dst_stride;
73
4.73M
  }
74
127k
}
75
76
void av1_convolve_2d_sobel_y_c(const uint8_t *src, int src_stride, double *dst,
77
                               int dst_stride, int w, int h, int dir,
78
0
                               double norm) {
79
0
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
80
0
  DECLARE_ALIGNED(256, static const int16_t, sobel_a[3]) = { 1, 0, -1 };
81
0
  DECLARE_ALIGNED(256, static const int16_t, sobel_b[3]) = { 1, 2, 1 };
82
0
  const int taps = 3;
83
0
  int im_h = h + taps - 1;
84
0
  int im_stride = w;
85
0
  const int fo_vert = 1;
86
0
  const int fo_horiz = 1;
87
88
  // horizontal filter
89
0
  const uint8_t *src_horiz = src - fo_vert * src_stride;
90
0
  const int16_t *x_filter = dir ? sobel_a : sobel_b;
91
0
  for (int y = 0; y < im_h; ++y) {
92
0
    for (int x = 0; x < w; ++x) {
93
0
      int16_t sum = 0;
94
0
      for (int k = 0; k < taps; ++k) {
95
0
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
96
0
      }
97
0
      im_block[y * im_stride + x] = sum;
98
0
    }
99
0
  }
100
101
  // vertical filter
102
0
  int16_t *src_vert = im_block + fo_vert * im_stride;
103
0
  const int16_t *y_filter = dir ? sobel_b : sobel_a;
104
0
  for (int y = 0; y < h; ++y) {
105
0
    for (int x = 0; x < w; ++x) {
106
0
      int16_t sum = 0;
107
0
      for (int k = 0; k < taps; ++k) {
108
0
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
109
0
      }
110
0
      dst[y * dst_stride + x] = sum * norm;
111
0
    }
112
0
  }
113
0
}
114
115
void av1_convolve_2d_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
116
                          int dst_stride, int w, int h,
117
                          const InterpFilterParams *filter_params_x,
118
                          const InterpFilterParams *filter_params_y,
119
                          const int subpel_x_qn, const int subpel_y_qn,
120
16.7k
                          ConvolveParams *conv_params) {
121
16.7k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
122
16.7k
  int im_h = h + filter_params_y->taps - 1;
123
16.7k
  int im_stride = w;
124
16.7k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
125
16.7k
  const int fo_vert = filter_params_y->taps / 2 - 1;
126
16.7k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
127
16.7k
  const int bd = 8;
128
16.7k
  const int bits =
129
16.7k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
130
131
  // horizontal filter
132
16.7k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
133
16.7k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
134
16.7k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
135
200k
  for (int y = 0; y < im_h; ++y) {
136
1.79M
    for (int x = 0; x < w; ++x) {
137
1.61M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
138
8.42M
      for (int k = 0; k < filter_params_x->taps; ++k) {
139
6.81M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
140
6.81M
      }
141
1.61M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
142
1.61M
      im_block[y * im_stride + x] =
143
1.61M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
144
1.61M
    }
145
184k
  }
146
147
  // vertical filter
148
16.7k
  int16_t *src_vert = im_block + fo_vert * im_stride;
149
16.7k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
150
16.7k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
151
16.7k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
152
128k
  for (int y = 0; y < h; ++y) {
153
1.32M
    for (int x = 0; x < w; ++x) {
154
1.21M
      int32_t sum = 1 << offset_bits;
155
5.30M
      for (int k = 0; k < filter_params_y->taps; ++k) {
156
4.09M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
157
4.09M
      }
158
1.21M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
159
1.21M
      int16_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
160
1.21M
                    ((1 << (offset_bits - conv_params->round_1)) +
161
1.21M
                     (1 << (offset_bits - conv_params->round_1 - 1)));
162
1.21M
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
163
1.21M
    }
164
112k
  }
165
16.7k
}
166
167
void av1_convolve_y_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
168
                         int dst_stride, int w, int h,
169
                         const InterpFilterParams *filter_params_y,
170
13.4k
                         const int subpel_y_qn) {
171
13.4k
  const int fo_vert = filter_params_y->taps / 2 - 1;
172
173
  // vertical filter
174
13.4k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
175
13.4k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
176
108k
  for (int y = 0; y < h; ++y) {
177
1.16M
    for (int x = 0; x < w; ++x) {
178
1.06M
      int32_t res = 0;
179
4.47M
      for (int k = 0; k < filter_params_y->taps; ++k) {
180
3.40M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
181
3.40M
      }
182
1.06M
      dst[y * dst_stride + x] =
183
1.06M
          clip_pixel(ROUND_POWER_OF_TWO(res, FILTER_BITS));
184
1.06M
    }
185
94.9k
  }
186
13.4k
}
187
188
void av1_convolve_x_sr_c(const uint8_t *src, int src_stride, uint8_t *dst,
189
                         int dst_stride, int w, int h,
190
                         const InterpFilterParams *filter_params_x,
191
10.5k
                         const int subpel_x_qn, ConvolveParams *conv_params) {
192
10.5k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
193
10.5k
  const int bits = FILTER_BITS - conv_params->round_0;
194
195
10.5k
  assert(bits >= 0);
196
10.5k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
197
10.5k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
198
199
  // horizontal filter
200
10.5k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
201
10.5k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
202
203
99.4k
  for (int y = 0; y < h; ++y) {
204
1.49M
    for (int x = 0; x < w; ++x) {
205
1.41M
      int32_t res = 0;
206
5.09M
      for (int k = 0; k < filter_params_x->taps; ++k) {
207
3.68M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
208
3.68M
      }
209
1.41M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
210
1.41M
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
211
1.41M
    }
212
88.8k
  }
213
10.5k
}
214
215
void av1_dist_wtd_convolve_2d_c(const uint8_t *src, int src_stride,
216
                                uint8_t *dst, int dst_stride, int w, int h,
217
                                const InterpFilterParams *filter_params_x,
218
                                const InterpFilterParams *filter_params_y,
219
                                const int subpel_x_qn, const int subpel_y_qn,
220
8.46k
                                ConvolveParams *conv_params) {
221
8.46k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
222
8.46k
  int dst16_stride = conv_params->dst_stride;
223
8.46k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
224
8.46k
  int im_h = h + filter_params_y->taps - 1;
225
8.46k
  int im_stride = w;
226
8.46k
  const int fo_vert = filter_params_y->taps / 2 - 1;
227
8.46k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
228
8.46k
  const int bd = 8;
229
8.46k
  const int round_bits =
230
8.46k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
231
232
  // horizontal filter
233
8.46k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
234
8.46k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
235
8.46k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
236
139k
  for (int y = 0; y < im_h; ++y) {
237
1.14M
    for (int x = 0; x < w; ++x) {
238
1.01M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
239
9.09M
      for (int k = 0; k < filter_params_x->taps; ++k) {
240
8.08M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
241
8.08M
      }
242
1.01M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
243
1.01M
      im_block[y * im_stride + x] =
244
1.01M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
245
1.01M
    }
246
131k
  }
247
248
  // vertical filter
249
8.46k
  int16_t *src_vert = im_block + fo_vert * im_stride;
250
8.46k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
251
8.46k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
252
8.46k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
253
80.4k
  for (int y = 0; y < h; ++y) {
254
633k
    for (int x = 0; x < w; ++x) {
255
561k
      int32_t sum = 1 << offset_bits;
256
5.05M
      for (int k = 0; k < filter_params_y->taps; ++k) {
257
4.49M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
258
4.49M
      }
259
561k
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
260
561k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
261
561k
      if (conv_params->do_average) {
262
234k
        int32_t tmp = dst16[y * dst16_stride + x];
263
234k
        if (conv_params->use_dist_wtd_comp_avg) {
264
76.8k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
265
76.8k
          tmp = tmp >> DIST_PRECISION_BITS;
266
158k
        } else {
267
158k
          tmp += res;
268
158k
          tmp = tmp >> 1;
269
158k
        }
270
234k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
271
234k
               (1 << (offset_bits - conv_params->round_1 - 1));
272
234k
        dst[y * dst_stride + x] =
273
234k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
274
326k
      } else {
275
326k
        dst16[y * dst16_stride + x] = res;
276
326k
      }
277
561k
    }
278
71.9k
  }
279
8.46k
}
280
281
void av1_dist_wtd_convolve_y_c(const uint8_t *src, int src_stride, uint8_t *dst,
282
                               int dst_stride, int w, int h,
283
                               const InterpFilterParams *filter_params_y,
284
                               const int subpel_y_qn,
285
7.33k
                               ConvolveParams *conv_params) {
286
7.33k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
287
7.33k
  int dst16_stride = conv_params->dst_stride;
288
7.33k
  const int fo_vert = filter_params_y->taps / 2 - 1;
289
7.33k
  const int bits = FILTER_BITS - conv_params->round_0;
290
7.33k
  const int bd = 8;
291
7.33k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
292
7.33k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
293
7.33k
                           (1 << (offset_bits - conv_params->round_1 - 1));
294
7.33k
  const int round_bits =
295
7.33k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
296
297
  // vertical filter
298
7.33k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
299
7.33k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
300
74.0k
  for (int y = 0; y < h; ++y) {
301
596k
    for (int x = 0; x < w; ++x) {
302
529k
      int32_t res = 0;
303
4.76M
      for (int k = 0; k < filter_params_y->taps; ++k) {
304
4.23M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
305
4.23M
      }
306
529k
      res *= (1 << bits);
307
529k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
308
309
529k
      if (conv_params->do_average) {
310
152k
        int32_t tmp = dst16[y * dst16_stride + x];
311
152k
        if (conv_params->use_dist_wtd_comp_avg) {
312
35.5k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
313
35.5k
          tmp = tmp >> DIST_PRECISION_BITS;
314
116k
        } else {
315
116k
          tmp += res;
316
116k
          tmp = tmp >> 1;
317
116k
        }
318
152k
        tmp -= round_offset;
319
152k
        dst[y * dst_stride + x] =
320
152k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
321
377k
      } else {
322
377k
        dst16[y * dst16_stride + x] = res;
323
377k
      }
324
529k
    }
325
66.7k
  }
326
7.33k
}
327
328
void av1_dist_wtd_convolve_x_c(const uint8_t *src, int src_stride, uint8_t *dst,
329
                               int dst_stride, int w, int h,
330
                               const InterpFilterParams *filter_params_x,
331
                               const int subpel_x_qn,
332
3.11k
                               ConvolveParams *conv_params) {
333
3.11k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
334
3.11k
  int dst16_stride = conv_params->dst_stride;
335
3.11k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
336
3.11k
  const int bits = FILTER_BITS - conv_params->round_1;
337
3.11k
  const int bd = 8;
338
3.11k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
339
3.11k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
340
3.11k
                           (1 << (offset_bits - conv_params->round_1 - 1));
341
3.11k
  const int round_bits =
342
3.11k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
343
344
  // horizontal filter
345
3.11k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
346
3.11k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
347
35.4k
  for (int y = 0; y < h; ++y) {
348
293k
    for (int x = 0; x < w; ++x) {
349
261k
      int32_t res = 0;
350
2.35M
      for (int k = 0; k < filter_params_x->taps; ++k) {
351
2.09M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
352
2.09M
      }
353
261k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
354
261k
      res += round_offset;
355
356
261k
      if (conv_params->do_average) {
357
75.0k
        int32_t tmp = dst16[y * dst16_stride + x];
358
75.0k
        if (conv_params->use_dist_wtd_comp_avg) {
359
30.7k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
360
30.7k
          tmp = tmp >> DIST_PRECISION_BITS;
361
44.2k
        } else {
362
44.2k
          tmp += res;
363
44.2k
          tmp = tmp >> 1;
364
44.2k
        }
365
75.0k
        tmp -= round_offset;
366
75.0k
        dst[y * dst_stride + x] =
367
75.0k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
368
186k
      } else {
369
186k
        dst16[y * dst16_stride + x] = res;
370
186k
      }
371
261k
    }
372
32.3k
  }
373
3.11k
}
374
375
void av1_dist_wtd_convolve_2d_copy_c(const uint8_t *src, int src_stride,
376
                                     uint8_t *dst, int dst_stride, int w, int h,
377
8.35k
                                     ConvolveParams *conv_params) {
378
8.35k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
379
8.35k
  int dst16_stride = conv_params->dst_stride;
380
8.35k
  const int bits =
381
8.35k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
382
8.35k
  const int bd = 8;
383
8.35k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
384
8.35k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
385
8.35k
                           (1 << (offset_bits - conv_params->round_1 - 1));
386
387
81.2k
  for (int y = 0; y < h; ++y) {
388
659k
    for (int x = 0; x < w; ++x) {
389
586k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
390
586k
      res += round_offset;
391
392
586k
      if (conv_params->do_average) {
393
188k
        int32_t tmp = dst16[y * dst16_stride + x];
394
188k
        if (conv_params->use_dist_wtd_comp_avg) {
395
39.8k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
396
39.8k
          tmp = tmp >> DIST_PRECISION_BITS;
397
148k
        } else {
398
148k
          tmp += res;
399
148k
          tmp = tmp >> 1;
400
148k
        }
401
188k
        tmp -= round_offset;
402
188k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
403
398k
      } else {
404
398k
        dst16[y * dst16_stride + x] = res;
405
398k
      }
406
586k
    }
407
72.8k
  }
408
8.35k
}
409
410
void av1_convolve_2d_scale_c(const uint8_t *src, int src_stride, uint8_t *dst,
411
                             int dst_stride, int w, int h,
412
                             const InterpFilterParams *filter_params_x,
413
                             const InterpFilterParams *filter_params_y,
414
                             const int subpel_x_qn, const int x_step_qn,
415
                             const int subpel_y_qn, const int y_step_qn,
416
8.03k
                             ConvolveParams *conv_params) {
417
8.03k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
418
8.03k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
419
8.03k
             filter_params_y->taps;
420
8.03k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
421
8.03k
  const int dst16_stride = conv_params->dst_stride;
422
8.03k
  const int bits =
423
8.03k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
424
8.03k
  assert(bits >= 0);
425
8.03k
  int im_stride = w;
426
8.03k
  const int fo_vert = filter_params_y->taps / 2 - 1;
427
8.03k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
428
8.03k
  const int bd = 8;
429
430
  // horizontal filter
431
8.03k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
432
119k
  for (int y = 0; y < im_h; ++y) {
433
111k
    int x_qn = subpel_x_qn;
434
937k
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
435
825k
      const uint8_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
436
825k
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
437
825k
      assert(x_filter_idx < SUBPEL_SHIFTS);
438
825k
      const int16_t *x_filter =
439
825k
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
440
825k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
441
7.42M
      for (int k = 0; k < filter_params_x->taps; ++k) {
442
6.60M
        sum += x_filter[k] * src_x[k - fo_horiz];
443
6.60M
      }
444
825k
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
445
825k
      im_block[y * im_stride + x] =
446
825k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
447
825k
    }
448
111k
    src_horiz += src_stride;
449
111k
  }
450
451
  // vertical filter
452
8.03k
  int16_t *src_vert = im_block + fo_vert * im_stride;
453
8.03k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
454
61.5k
  for (int x = 0; x < w; ++x) {
455
53.5k
    int y_qn = subpel_y_qn;
456
495k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
457
442k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
458
442k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
459
442k
      assert(y_filter_idx < SUBPEL_SHIFTS);
460
442k
      const int16_t *y_filter =
461
442k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
462
442k
      int32_t sum = 1 << offset_bits;
463
3.98M
      for (int k = 0; k < filter_params_y->taps; ++k) {
464
3.53M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
465
3.53M
      }
466
442k
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
467
442k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
468
442k
      if (conv_params->is_compound) {
469
149k
        if (conv_params->do_average) {
470
55.1k
          int32_t tmp = dst16[y * dst16_stride + x];
471
55.1k
          if (conv_params->use_dist_wtd_comp_avg) {
472
13.5k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
473
13.5k
            tmp = tmp >> DIST_PRECISION_BITS;
474
41.5k
          } else {
475
41.5k
            tmp += res;
476
41.5k
            tmp = tmp >> 1;
477
41.5k
          }
478
          /* Subtract round offset and convolve round */
479
55.1k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
480
55.1k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
481
55.1k
          dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
482
93.8k
        } else {
483
93.8k
          dst16[y * dst16_stride + x] = res;
484
93.8k
        }
485
293k
      } else {
486
        /* Subtract round offset and convolve round */
487
293k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
488
293k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
489
293k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
490
293k
      }
491
442k
    }
492
53.5k
    src_vert++;
493
53.5k
  }
494
8.03k
}
495
496
static void convolve_2d_scale_wrapper(
497
    const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int w,
498
    int h, const InterpFilterParams *filter_params_x,
499
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
500
    const int x_step_qn, const int subpel_y_qn, const int y_step_qn,
501
8.03k
    ConvolveParams *conv_params) {
502
8.03k
  if (conv_params->is_compound) {
503
1.74k
    assert(conv_params->dst != NULL);
504
1.74k
  }
505
8.03k
  av1_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h, filter_params_x,
506
8.03k
                        filter_params_y, subpel_x_qn, x_step_qn, subpel_y_qn,
507
8.03k
                        y_step_qn, conv_params);
508
8.03k
}
509
510
static void convolve_2d_facade_compound(
511
    const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int w,
512
    int h, const InterpFilterParams *filter_params_x,
513
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
514
27.2k
    const int subpel_y_qn, ConvolveParams *conv_params) {
515
27.2k
  const bool need_x = subpel_x_qn != 0;
516
27.2k
  const bool need_y = subpel_y_qn != 0;
517
27.2k
  if (!need_x && !need_y) {
518
8.35k
    av1_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
519
8.35k
                                  conv_params);
520
18.9k
  } else if (need_x && !need_y) {
521
3.11k
    av1_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
522
3.11k
                            filter_params_x, subpel_x_qn, conv_params);
523
15.8k
  } else if (!need_x && need_y) {
524
7.33k
    av1_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
525
7.33k
                            filter_params_y, subpel_y_qn, conv_params);
526
8.46k
  } else {
527
8.46k
    assert(need_y && need_x);
528
8.46k
    av1_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
529
8.46k
                             filter_params_x, filter_params_y, subpel_x_qn,
530
8.46k
                             subpel_y_qn, conv_params);
531
8.46k
  }
532
27.2k
}
533
534
static void convolve_2d_facade_single(
535
    const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int w,
536
    int h, const InterpFilterParams *filter_params_x,
537
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
538
70.1k
    const int subpel_y_qn, ConvolveParams *conv_params) {
539
70.1k
  const bool need_x = subpel_x_qn != 0;
540
70.1k
  const bool need_y = subpel_y_qn != 0;
541
70.1k
  if (!need_x && !need_y) {
542
51.7k
    aom_convolve_copy(src, src_stride, dst, dst_stride, w, h);
543
51.7k
  } else if (need_x && !need_y) {
544
2.98k
    av1_convolve_x_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
545
2.98k
                      subpel_x_qn, conv_params);
546
15.4k
  } else if (!need_x && need_y) {
547
6.21k
    av1_convolve_y_sr(src, src_stride, dst, dst_stride, w, h, filter_params_y,
548
6.21k
                      subpel_y_qn);
549
9.20k
  } else {
550
9.20k
    assert(need_x && need_y);
551
9.20k
    av1_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
552
9.20k
                       filter_params_y, subpel_x_qn, subpel_y_qn, conv_params);
553
9.20k
  }
554
70.1k
}
555
556
void av1_convolve_2d_facade(const uint8_t *src, int src_stride, uint8_t *dst,
557
                            int dst_stride, int w, int h,
558
                            const InterpFilterParams *interp_filters[2],
559
                            const int subpel_x_qn, int x_step_q4,
560
                            const int subpel_y_qn, int y_step_q4, int scaled,
561
127k
                            ConvolveParams *conv_params) {
562
127k
  (void)x_step_q4;
563
127k
  (void)y_step_q4;
564
127k
  (void)dst;
565
127k
  (void)dst_stride;
566
567
127k
  const InterpFilterParams *filter_params_x = interp_filters[0];
568
127k
  const InterpFilterParams *filter_params_y = interp_filters[1];
569
570
  // TODO(jingning, yunqing): Add SIMD support to 2-tap filter case.
571
  // Do we have SIMD support to 4-tap case?
572
  // 2-tap filter indicates that it is for IntraBC.
573
127k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
574
55.6k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
575
55.6k
    assert(!scaled);
576
55.6k
    if (subpel_x_qn && subpel_y_qn) {
577
7.51k
      av1_convolve_2d_sr_c(src, src_stride, dst, dst_stride, w, h,
578
7.51k
                           filter_params_x, filter_params_y, subpel_x_qn,
579
7.51k
                           subpel_y_qn, conv_params);
580
7.51k
      return;
581
48.1k
    } else if (subpel_x_qn) {
582
7.61k
      av1_convolve_x_sr_c(src, src_stride, dst, dst_stride, w, h,
583
7.61k
                          filter_params_x, subpel_x_qn, conv_params);
584
7.61k
      return;
585
40.5k
    } else if (subpel_y_qn) {
586
7.24k
      av1_convolve_y_sr_c(src, src_stride, dst, dst_stride, w, h,
587
7.24k
                          filter_params_y, subpel_y_qn);
588
7.24k
      return;
589
7.24k
    }
590
55.6k
  }
591
592
105k
  if (scaled) {
593
8.03k
    convolve_2d_scale_wrapper(src, src_stride, dst, dst_stride, w, h,
594
8.03k
                              filter_params_x, filter_params_y, subpel_x_qn,
595
8.03k
                              x_step_q4, subpel_y_qn, y_step_q4, conv_params);
596
97.4k
  } else if (conv_params->is_compound) {
597
27.2k
    convolve_2d_facade_compound(src, src_stride, dst, dst_stride, w, h,
598
27.2k
                                filter_params_x, filter_params_y, subpel_x_qn,
599
27.2k
                                subpel_y_qn, conv_params);
600
70.1k
  } else {
601
70.1k
    convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
602
70.1k
                              filter_params_x, filter_params_y, subpel_x_qn,
603
70.1k
                              subpel_y_qn, conv_params);
604
70.1k
  }
605
105k
}
606
607
#if CONFIG_AV1_HIGHBITDEPTH
608
void av1_highbd_convolve_x_sr_c(const uint16_t *src, int src_stride,
609
                                uint16_t *dst, int dst_stride, int w, int h,
610
                                const InterpFilterParams *filter_params_x,
611
                                const int subpel_x_qn,
612
38.1k
                                ConvolveParams *conv_params, int bd) {
613
38.1k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
614
38.1k
  const int bits = FILTER_BITS - conv_params->round_0;
615
616
38.1k
  assert(bits >= 0);
617
38.1k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
618
38.1k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
619
620
  // horizontal filter
621
38.1k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
622
38.1k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
623
305k
  for (int y = 0; y < h; ++y) {
624
3.07M
    for (int x = 0; x < w; ++x) {
625
2.81M
      int32_t res = 0;
626
11.6M
      for (int k = 0; k < filter_params_x->taps; ++k) {
627
8.88M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
628
8.88M
      }
629
2.81M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
630
2.81M
      dst[y * dst_stride + x] =
631
2.81M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
632
2.81M
    }
633
267k
  }
634
38.1k
}
635
636
void av1_highbd_convolve_y_sr_c(const uint16_t *src, int src_stride,
637
                                uint16_t *dst, int dst_stride, int w, int h,
638
                                const InterpFilterParams *filter_params_y,
639
30.2k
                                const int subpel_y_qn, int bd) {
640
30.2k
  const int fo_vert = filter_params_y->taps / 2 - 1;
641
  // vertical filter
642
30.2k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
643
30.2k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
644
225k
  for (int y = 0; y < h; ++y) {
645
1.86M
    for (int x = 0; x < w; ++x) {
646
1.66M
      int32_t res = 0;
647
6.17M
      for (int k = 0; k < filter_params_y->taps; ++k) {
648
4.50M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
649
4.50M
      }
650
1.66M
      dst[y * dst_stride + x] =
651
1.66M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, FILTER_BITS), bd);
652
1.66M
    }
653
194k
  }
654
30.2k
}
655
656
void av1_highbd_convolve_2d_sr_c(const uint16_t *src, int src_stride,
657
                                 uint16_t *dst, int dst_stride, int w, int h,
658
                                 const InterpFilterParams *filter_params_x,
659
                                 const InterpFilterParams *filter_params_y,
660
                                 const int subpel_x_qn, const int subpel_y_qn,
661
35.0k
                                 ConvolveParams *conv_params, int bd) {
662
35.0k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
663
35.0k
  int im_h = h + filter_params_y->taps - 1;
664
35.0k
  int im_stride = w;
665
35.0k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
666
35.0k
  const int fo_vert = filter_params_y->taps / 2 - 1;
667
35.0k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
668
35.0k
  const int bits =
669
35.0k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
670
35.0k
  assert(bits >= 0);
671
672
  // horizontal filter
673
35.0k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
674
35.0k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
675
35.0k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
676
338k
  for (int y = 0; y < im_h; ++y) {
677
2.91M
    for (int x = 0; x < w; ++x) {
678
2.60M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
679
11.0M
      for (int k = 0; k < filter_params_x->taps; ++k) {
680
8.40M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
681
8.40M
      }
682
2.60M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
683
2.60M
      im_block[y * im_stride + x] =
684
2.60M
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
685
2.60M
    }
686
303k
  }
687
688
  // vertical filter
689
35.0k
  int16_t *src_vert = im_block + fo_vert * im_stride;
690
35.0k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
691
35.0k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
692
35.0k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
693
266k
  for (int y = 0; y < h; ++y) {
694
2.36M
    for (int x = 0; x < w; ++x) {
695
2.12M
      int32_t sum = 1 << offset_bits;
696
8.01M
      for (int k = 0; k < filter_params_y->taps; ++k) {
697
5.88M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
698
5.88M
      }
699
2.12M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
700
2.12M
      int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
701
2.12M
                    ((1 << (offset_bits - conv_params->round_1)) +
702
2.12M
                     (1 << (offset_bits - conv_params->round_1 - 1)));
703
2.12M
      dst[y * dst_stride + x] =
704
2.12M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
705
2.12M
    }
706
231k
  }
707
35.0k
}
708
709
void av1_highbd_dist_wtd_convolve_2d_c(
710
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
711
    int h, const InterpFilterParams *filter_params_x,
712
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
713
3.55k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
714
3.55k
  int x, y, k;
715
3.55k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
716
3.55k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
717
3.55k
  int dst16_stride = conv_params->dst_stride;
718
3.55k
  int im_h = h + filter_params_y->taps - 1;
719
3.55k
  int im_stride = w;
720
3.55k
  const int fo_vert = filter_params_y->taps / 2 - 1;
721
3.55k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
722
3.55k
  const int round_bits =
723
3.55k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
724
3.55k
  assert(round_bits >= 0);
725
726
  // horizontal filter
727
3.55k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
728
3.55k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
729
3.55k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
730
60.6k
  for (y = 0; y < im_h; ++y) {
731
562k
    for (x = 0; x < w; ++x) {
732
505k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
733
4.54M
      for (k = 0; k < filter_params_x->taps; ++k) {
734
4.04M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
735
4.04M
      }
736
505k
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
737
505k
      (void)bd;
738
505k
      im_block[y * im_stride + x] =
739
505k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
740
505k
    }
741
57.0k
  }
742
743
  // vertical filter
744
3.55k
  int16_t *src_vert = im_block + fo_vert * im_stride;
745
3.55k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
746
3.55k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
747
3.55k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
748
35.7k
  for (y = 0; y < h; ++y) {
749
325k
    for (x = 0; x < w; ++x) {
750
293k
      int32_t sum = 1 << offset_bits;
751
2.64M
      for (k = 0; k < filter_params_y->taps; ++k) {
752
2.35M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
753
2.35M
      }
754
293k
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
755
293k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
756
293k
      if (conv_params->do_average) {
757
110k
        int32_t tmp = dst16[y * dst16_stride + x];
758
110k
        if (conv_params->use_dist_wtd_comp_avg) {
759
21.2k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
760
21.2k
          tmp = tmp >> DIST_PRECISION_BITS;
761
89.3k
        } else {
762
89.3k
          tmp += res;
763
89.3k
          tmp = tmp >> 1;
764
89.3k
        }
765
110k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
766
110k
               (1 << (offset_bits - conv_params->round_1 - 1));
767
110k
        dst[y * dst_stride + x] =
768
110k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
769
183k
      } else {
770
183k
        dst16[y * dst16_stride + x] = res;
771
183k
      }
772
293k
    }
773
32.1k
  }
774
3.55k
}
775
776
void av1_highbd_dist_wtd_convolve_x_c(const uint16_t *src, int src_stride,
777
                                      uint16_t *dst, int dst_stride, int w,
778
                                      int h,
779
                                      const InterpFilterParams *filter_params_x,
780
                                      const int subpel_x_qn,
781
6.28k
                                      ConvolveParams *conv_params, int bd) {
782
6.28k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
783
6.28k
  int dst16_stride = conv_params->dst_stride;
784
6.28k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
785
6.28k
  const int bits = FILTER_BITS - conv_params->round_1;
786
6.28k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
787
6.28k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
788
6.28k
                           (1 << (offset_bits - conv_params->round_1 - 1));
789
6.28k
  const int round_bits =
790
6.28k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
791
6.28k
  assert(round_bits >= 0);
792
6.28k
  assert(bits >= 0);
793
  // horizontal filter
794
6.28k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
795
6.28k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
796
68.0k
  for (int y = 0; y < h; ++y) {
797
537k
    for (int x = 0; x < w; ++x) {
798
475k
      int32_t res = 0;
799
4.27M
      for (int k = 0; k < filter_params_x->taps; ++k) {
800
3.80M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
801
3.80M
      }
802
475k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
803
475k
      res += round_offset;
804
805
475k
      if (conv_params->do_average) {
806
258k
        int32_t tmp = dst16[y * dst16_stride + x];
807
258k
        if (conv_params->use_dist_wtd_comp_avg) {
808
36.0k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
809
36.0k
          tmp = tmp >> DIST_PRECISION_BITS;
810
222k
        } else {
811
222k
          tmp += res;
812
222k
          tmp = tmp >> 1;
813
222k
        }
814
258k
        tmp -= round_offset;
815
258k
        dst[y * dst_stride + x] =
816
258k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
817
258k
      } else {
818
216k
        dst16[y * dst16_stride + x] = res;
819
216k
      }
820
475k
    }
821
61.7k
  }
822
6.28k
}
823
824
void av1_highbd_dist_wtd_convolve_y_c(const uint16_t *src, int src_stride,
825
                                      uint16_t *dst, int dst_stride, int w,
826
                                      int h,
827
                                      const InterpFilterParams *filter_params_y,
828
                                      const int subpel_y_qn,
829
2.11k
                                      ConvolveParams *conv_params, int bd) {
830
2.11k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
831
2.11k
  int dst16_stride = conv_params->dst_stride;
832
2.11k
  const int fo_vert = filter_params_y->taps / 2 - 1;
833
2.11k
  const int bits = FILTER_BITS - conv_params->round_0;
834
2.11k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
835
2.11k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
836
2.11k
                           (1 << (offset_bits - conv_params->round_1 - 1));
837
2.11k
  const int round_bits =
838
2.11k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
839
2.11k
  assert(round_bits >= 0);
840
2.11k
  assert(bits >= 0);
841
  // vertical filter
842
2.11k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
843
2.11k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
844
22.6k
  for (int y = 0; y < h; ++y) {
845
206k
    for (int x = 0; x < w; ++x) {
846
185k
      int32_t res = 0;
847
1.67M
      for (int k = 0; k < filter_params_y->taps; ++k) {
848
1.48M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
849
1.48M
      }
850
185k
      res *= (1 << bits);
851
185k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
852
853
185k
      if (conv_params->do_average) {
854
90.3k
        int32_t tmp = dst16[y * dst16_stride + x];
855
90.3k
        if (conv_params->use_dist_wtd_comp_avg) {
856
22.2k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
857
22.2k
          tmp = tmp >> DIST_PRECISION_BITS;
858
68.0k
        } else {
859
68.0k
          tmp += res;
860
68.0k
          tmp = tmp >> 1;
861
68.0k
        }
862
90.3k
        tmp -= round_offset;
863
90.3k
        dst[y * dst_stride + x] =
864
90.3k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
865
95.2k
      } else {
866
95.2k
        dst16[y * dst16_stride + x] = res;
867
95.2k
      }
868
185k
    }
869
20.5k
  }
870
2.11k
}
871
872
void av1_highbd_dist_wtd_convolve_2d_copy_c(const uint16_t *src, int src_stride,
873
                                            uint16_t *dst, int dst_stride,
874
                                            int w, int h,
875
                                            ConvolveParams *conv_params,
876
25.7k
                                            int bd) {
877
25.7k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
878
25.7k
  int dst16_stride = conv_params->dst_stride;
879
25.7k
  const int bits =
880
25.7k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
881
25.7k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
882
25.7k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
883
25.7k
                           (1 << (offset_bits - conv_params->round_1 - 1));
884
25.7k
  assert(bits >= 0);
885
886
251k
  for (int y = 0; y < h; ++y) {
887
2.09M
    for (int x = 0; x < w; ++x) {
888
1.87M
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
889
1.87M
      res += round_offset;
890
1.87M
      if (conv_params->do_average) {
891
684k
        int32_t tmp = dst16[y * dst16_stride + x];
892
684k
        if (conv_params->use_dist_wtd_comp_avg) {
893
262k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
894
262k
          tmp = tmp >> DIST_PRECISION_BITS;
895
422k
        } else {
896
422k
          tmp += res;
897
422k
          tmp = tmp >> 1;
898
422k
        }
899
684k
        tmp -= round_offset;
900
684k
        dst[y * dst_stride + x] =
901
684k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
902
1.18M
      } else {
903
1.18M
        dst16[y * dst16_stride + x] = res;
904
1.18M
      }
905
1.87M
    }
906
225k
  }
907
25.7k
}
908
909
void av1_highbd_convolve_2d_scale_c(const uint16_t *src, int src_stride,
910
                                    uint16_t *dst, int dst_stride, int w, int h,
911
                                    const InterpFilterParams *filter_params_x,
912
                                    const InterpFilterParams *filter_params_y,
913
                                    const int subpel_x_qn, const int x_step_qn,
914
                                    const int subpel_y_qn, const int y_step_qn,
915
7.58k
                                    ConvolveParams *conv_params, int bd) {
916
7.58k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
917
7.58k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
918
7.58k
             filter_params_y->taps;
919
7.58k
  int im_stride = w;
920
7.58k
  const int fo_vert = filter_params_y->taps / 2 - 1;
921
7.58k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
922
7.58k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
923
7.58k
  const int dst16_stride = conv_params->dst_stride;
924
7.58k
  const int bits =
925
7.58k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
926
7.58k
  assert(bits >= 0);
927
  // horizontal filter
928
7.58k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
929
119k
  for (int y = 0; y < im_h; ++y) {
930
112k
    int x_qn = subpel_x_qn;
931
1.05M
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
932
939k
      const uint16_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
933
939k
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
934
939k
      assert(x_filter_idx < SUBPEL_SHIFTS);
935
939k
      const int16_t *x_filter =
936
939k
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
937
939k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
938
8.45M
      for (int k = 0; k < filter_params_x->taps; ++k) {
939
7.51M
        sum += x_filter[k] * src_x[k - fo_horiz];
940
7.51M
      }
941
939k
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
942
939k
      im_block[y * im_stride + x] =
943
939k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
944
939k
    }
945
112k
    src_horiz += src_stride;
946
112k
  }
947
948
  // vertical filter
949
7.58k
  int16_t *src_vert = im_block + fo_vert * im_stride;
950
7.58k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
951
65.1k
  for (int x = 0; x < w; ++x) {
952
57.5k
    int y_qn = subpel_y_qn;
953
556k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
954
499k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
955
499k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
956
499k
      assert(y_filter_idx < SUBPEL_SHIFTS);
957
499k
      const int16_t *y_filter =
958
499k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
959
499k
      int32_t sum = 1 << offset_bits;
960
4.49M
      for (int k = 0; k < filter_params_y->taps; ++k) {
961
3.99M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
962
3.99M
      }
963
499k
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
964
499k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
965
499k
      if (conv_params->is_compound) {
966
76.1k
        if (conv_params->do_average) {
967
31.9k
          int32_t tmp = dst16[y * dst16_stride + x];
968
31.9k
          if (conv_params->use_dist_wtd_comp_avg) {
969
11.4k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
970
11.4k
            tmp = tmp >> DIST_PRECISION_BITS;
971
20.4k
          } else {
972
20.4k
            tmp += res;
973
20.4k
            tmp = tmp >> 1;
974
20.4k
          }
975
          /* Subtract round offset and convolve round */
976
31.9k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
977
31.9k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
978
31.9k
          dst[y * dst_stride + x] =
979
31.9k
              clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
980
44.2k
        } else {
981
44.2k
          dst16[y * dst16_stride + x] = res;
982
44.2k
        }
983
423k
      } else {
984
        /* Subtract round offset and convolve round */
985
423k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
986
423k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
987
423k
        dst[y * dst_stride + x] =
988
423k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
989
423k
      }
990
499k
    }
991
57.5k
    src_vert++;
992
57.5k
  }
993
7.58k
}
994
995
static void highbd_convolve_2d_facade_compound(
996
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride,
997
    const int w, const int h, const InterpFilterParams *filter_params_x,
998
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
999
37.6k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1000
37.6k
  const bool need_x = subpel_x_qn != 0;
1001
37.6k
  const bool need_y = subpel_y_qn != 0;
1002
37.6k
  if (!need_x && !need_y) {
1003
25.7k
    av1_highbd_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
1004
25.7k
                                         conv_params, bd);
1005
25.7k
  } else if (need_x && !need_y) {
1006
6.28k
    av1_highbd_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
1007
6.28k
                                   filter_params_x, subpel_x_qn, conv_params,
1008
6.28k
                                   bd);
1009
6.28k
  } else if (!need_x && need_y) {
1010
2.11k
    av1_highbd_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
1011
2.11k
                                   filter_params_y, subpel_y_qn, conv_params,
1012
2.11k
                                   bd);
1013
3.55k
  } else {
1014
3.55k
    assert(need_x && need_y);
1015
3.55k
    av1_highbd_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
1016
3.55k
                                    filter_params_x, filter_params_y,
1017
3.55k
                                    subpel_x_qn, subpel_y_qn, conv_params, bd);
1018
3.55k
  }
1019
37.6k
}
1020
1021
static void highbd_convolve_2d_facade_single(
1022
    const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride,
1023
    const int w, const int h, const InterpFilterParams *filter_params_x,
1024
    const InterpFilterParams *filter_params_y, const int subpel_x_qn,
1025
268k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1026
268k
  const bool need_x = subpel_x_qn != 0;
1027
268k
  const bool need_y = subpel_y_qn != 0;
1028
1029
268k
  if (!need_x && !need_y) {
1030
165k
    aom_highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h);
1031
165k
  } else if (need_x && !need_y) {
1032
38.1k
    av1_highbd_convolve_x_sr(src, src_stride, dst, dst_stride, w, h,
1033
38.1k
                             filter_params_x, subpel_x_qn, conv_params, bd);
1034
65.2k
  } else if (!need_x && need_y) {
1035
30.2k
    av1_highbd_convolve_y_sr(src, src_stride, dst, dst_stride, w, h,
1036
30.2k
                             filter_params_y, subpel_y_qn, bd);
1037
35.0k
  } else {
1038
35.0k
    assert(need_x && need_y);
1039
35.0k
    av1_highbd_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h,
1040
35.0k
                              filter_params_x, filter_params_y, subpel_x_qn,
1041
35.0k
                              subpel_y_qn, conv_params, bd);
1042
35.0k
  }
1043
268k
}
1044
1045
void av1_highbd_convolve_2d_facade(const uint8_t *src8, int src_stride,
1046
                                   uint8_t *dst8, int dst_stride, int w, int h,
1047
                                   const InterpFilterParams *interp_filters[2],
1048
                                   const int subpel_x_qn, int x_step_q4,
1049
                                   const int subpel_y_qn, int y_step_q4,
1050
                                   int scaled, ConvolveParams *conv_params,
1051
313k
                                   int bd) {
1052
313k
  (void)x_step_q4;
1053
313k
  (void)y_step_q4;
1054
313k
  (void)dst_stride;
1055
313k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1056
1057
313k
  const int need_filter_params_x = (subpel_x_qn != 0) | scaled;
1058
313k
  const int need_filter_params_y = (subpel_y_qn != 0) | scaled;
1059
313k
  const InterpFilterParams *filter_params_x =
1060
313k
      need_filter_params_x ? interp_filters[0] : NULL;
1061
313k
  const InterpFilterParams *filter_params_y =
1062
313k
      need_filter_params_y ? interp_filters[1] : NULL;
1063
1064
313k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1065
313k
  if (scaled) {
1066
7.58k
    if (conv_params->is_compound) {
1067
635
      assert(conv_params->dst != NULL);
1068
635
    }
1069
7.58k
    av1_highbd_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h,
1070
7.58k
                                 filter_params_x, filter_params_y, subpel_x_qn,
1071
7.58k
                                 x_step_q4, subpel_y_qn, y_step_q4, conv_params,
1072
7.58k
                                 bd);
1073
306k
  } else if (conv_params->is_compound) {
1074
37.6k
    highbd_convolve_2d_facade_compound(
1075
37.6k
        src, src_stride, dst, dst_stride, w, h, filter_params_x,
1076
37.6k
        filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1077
268k
  } else {
1078
268k
    highbd_convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
1079
268k
                                     filter_params_x, filter_params_y,
1080
268k
                                     subpel_x_qn, subpel_y_qn, conv_params, bd);
1081
268k
  }
1082
313k
}
1083
#endif  // CONFIG_AV1_HIGHBITDEPTH
1084
1085
// Note: Fixed size intermediate buffers, place limits on parameters
1086
// of some functions. 2d filtering proceeds in 2 steps:
1087
//   (1) Interpolate horizontally into an intermediate buffer, temp.
1088
//   (2) Interpolate temp vertically to derive the sub-pixel result.
1089
// Deriving the maximum number of rows in the temp buffer (135):
1090
// --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
1091
// --Largest block size is 128x128 pixels.
1092
// --128 rows in the downscaled frame span a distance of (128 - 1) * 32 in the
1093
//   original frame (in 1/16th pixel units).
1094
// --Must round-up because block may be located at sub-pixel position.
1095
// --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
1096
// --((128 - 1) * 32 + 15) >> 4 + 8 = 263.
1097
#define WIENER_MAX_EXT_SIZE 263
1098
1099
65.1M
static INLINE int horz_scalar_product(const uint8_t *a, const int16_t *b) {
1100
65.1M
  int sum = 0;
1101
586M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1102
65.1M
  return sum;
1103
65.1M
}
1104
1105
#if CONFIG_AV1_HIGHBITDEPTH
1106
static INLINE int highbd_horz_scalar_product(const uint16_t *a,
1107
104M
                                             const int16_t *b) {
1108
104M
  int sum = 0;
1109
938M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1110
104M
  return sum;
1111
104M
}
1112
#endif
1113
1114
static INLINE int highbd_vert_scalar_product(const uint16_t *a,
1115
                                             ptrdiff_t a_stride,
1116
150M
                                             const int16_t *b) {
1117
150M
  int sum = 0;
1118
1.35G
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
1119
150M
  return sum;
1120
150M
}
1121
1122
119k
static const InterpKernel *get_filter_base(const int16_t *filter) {
1123
  // NOTE: This assumes that the filter table is 256-byte aligned.
1124
  // TODO(agrange) Modify to make independent of table alignment.
1125
119k
  return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
1126
119k
}
1127
1128
119k
static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
1129
119k
  return (int)((const InterpKernel *)(intptr_t)f - base);
1130
119k
}
1131
1132
static void convolve_add_src_horiz_hip(const uint8_t *src, ptrdiff_t src_stride,
1133
                                       uint16_t *dst, ptrdiff_t dst_stride,
1134
                                       const InterpKernel *x_filters, int x0_q4,
1135
                                       int x_step_q4, int w, int h,
1136
23.4k
                                       int round0_bits) {
1137
23.4k
  const int bd = 8;
1138
23.4k
  src -= SUBPEL_TAPS / 2 - 1;
1139
1.34M
  for (int y = 0; y < h; ++y) {
1140
1.32M
    int x_q4 = x0_q4;
1141
66.4M
    for (int x = 0; x < w; ++x) {
1142
65.1M
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1143
65.1M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1144
65.1M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1145
65.1M
                           (1 << (bd + FILTER_BITS - 1));
1146
65.1M
      const int sum = horz_scalar_product(src_x, x_filter) + rounding;
1147
65.1M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1148
65.1M
                               WIENER_CLAMP_LIMIT(round0_bits, bd) - 1);
1149
65.1M
      x_q4 += x_step_q4;
1150
65.1M
    }
1151
1.32M
    src += src_stride;
1152
1.32M
    dst += dst_stride;
1153
1.32M
  }
1154
23.4k
}
1155
1156
static void convolve_add_src_vert_hip(const uint16_t *src, ptrdiff_t src_stride,
1157
                                      uint8_t *dst, ptrdiff_t dst_stride,
1158
                                      const InterpKernel *y_filters, int y0_q4,
1159
                                      int y_step_q4, int w, int h,
1160
23.4k
                                      int round1_bits) {
1161
23.4k
  const int bd = 8;
1162
23.4k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1163
1164
1.12M
  for (int x = 0; x < w; ++x) {
1165
1.10M
    int y_q4 = y0_q4;
1166
59.6M
    for (int y = 0; y < h; ++y) {
1167
58.5M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1168
58.5M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1169
58.5M
      const int rounding =
1170
58.5M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1171
58.5M
          (1 << (bd + round1_bits - 1));
1172
58.5M
      const int sum =
1173
58.5M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1174
58.5M
      dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, round1_bits));
1175
58.5M
      y_q4 += y_step_q4;
1176
58.5M
    }
1177
1.10M
    ++src;
1178
1.10M
    ++dst;
1179
1.10M
  }
1180
23.4k
}
1181
1182
void av1_wiener_convolve_add_src_c(const uint8_t *src, ptrdiff_t src_stride,
1183
                                   uint8_t *dst, ptrdiff_t dst_stride,
1184
                                   const int16_t *filter_x, int x_step_q4,
1185
                                   const int16_t *filter_y, int y_step_q4,
1186
                                   int w, int h,
1187
23.4k
                                   const ConvolveParams *conv_params) {
1188
23.4k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1189
23.4k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1190
1191
23.4k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1192
23.4k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1193
1194
23.4k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1195
23.4k
  const int intermediate_height =
1196
23.4k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS - 1;
1197
23.4k
  memset(temp + (intermediate_height * MAX_SB_SIZE), 0, MAX_SB_SIZE);
1198
1199
23.4k
  assert(w <= MAX_SB_SIZE);
1200
23.4k
  assert(h <= MAX_SB_SIZE);
1201
23.4k
  assert(y_step_q4 <= 32);
1202
23.4k
  assert(x_step_q4 <= 32);
1203
1204
23.4k
  convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1205
23.4k
                             src_stride, temp, MAX_SB_SIZE, filters_x, x0_q4,
1206
23.4k
                             x_step_q4, w, intermediate_height,
1207
23.4k
                             conv_params->round_0);
1208
23.4k
  convolve_add_src_vert_hip(temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1),
1209
23.4k
                            MAX_SB_SIZE, dst, dst_stride, filters_y, y0_q4,
1210
23.4k
                            y_step_q4, w, h, conv_params->round_1);
1211
23.4k
}
1212
1213
#if CONFIG_AV1_HIGHBITDEPTH
1214
static void highbd_convolve_add_src_horiz_hip(
1215
    const uint8_t *src8, ptrdiff_t src_stride, uint16_t *dst,
1216
    ptrdiff_t dst_stride, const InterpKernel *x_filters, int x0_q4,
1217
36.0k
    int x_step_q4, int w, int h, int round0_bits, int bd) {
1218
36.0k
  const int extraprec_clamp_limit = WIENER_CLAMP_LIMIT(round0_bits, bd);
1219
36.0k
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1220
36.0k
  src -= SUBPEL_TAPS / 2 - 1;
1221
2.21M
  for (int y = 0; y < h; ++y) {
1222
2.18M
    int x_q4 = x0_q4;
1223
106M
    for (int x = 0; x < w; ++x) {
1224
104M
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1225
104M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1226
104M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1227
104M
                           (1 << (bd + FILTER_BITS - 1));
1228
104M
      const int sum = highbd_horz_scalar_product(src_x, x_filter) + rounding;
1229
104M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1230
104M
                               extraprec_clamp_limit - 1);
1231
104M
      x_q4 += x_step_q4;
1232
104M
    }
1233
2.18M
    src += src_stride;
1234
2.18M
    dst += dst_stride;
1235
2.18M
  }
1236
36.0k
}
1237
1238
static void highbd_convolve_add_src_vert_hip(
1239
    const uint16_t *src, ptrdiff_t src_stride, uint8_t *dst8,
1240
    ptrdiff_t dst_stride, const InterpKernel *y_filters, int y0_q4,
1241
36.0k
    int y_step_q4, int w, int h, int round1_bits, int bd) {
1242
36.0k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1243
36.0k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1244
1.75M
  for (int x = 0; x < w; ++x) {
1245
1.72M
    int y_q4 = y0_q4;
1246
93.9M
    for (int y = 0; y < h; ++y) {
1247
92.2M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1248
92.2M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1249
92.2M
      const int rounding =
1250
92.2M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1251
92.2M
          (1 << (bd + round1_bits - 1));
1252
92.2M
      const int sum =
1253
92.2M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1254
92.2M
      dst[y * dst_stride] =
1255
92.2M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, round1_bits), bd);
1256
92.2M
      y_q4 += y_step_q4;
1257
92.2M
    }
1258
1.72M
    ++src;
1259
1.72M
    ++dst;
1260
1.72M
  }
1261
36.0k
}
1262
1263
void av1_highbd_wiener_convolve_add_src_c(
1264
    const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
1265
    ptrdiff_t dst_stride, const int16_t *filter_x, int x_step_q4,
1266
    const int16_t *filter_y, int y_step_q4, int w, int h,
1267
36.0k
    const ConvolveParams *conv_params, int bd) {
1268
36.0k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1269
36.0k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1270
1271
36.0k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1272
36.0k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1273
1274
36.0k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1275
36.0k
  const int intermediate_height =
1276
36.0k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
1277
1278
36.0k
  assert(w <= MAX_SB_SIZE);
1279
36.0k
  assert(h <= MAX_SB_SIZE);
1280
36.0k
  assert(y_step_q4 <= 32);
1281
36.0k
  assert(x_step_q4 <= 32);
1282
36.0k
  assert(bd + FILTER_BITS - conv_params->round_0 + 2 <= 16);
1283
1284
36.0k
  highbd_convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1285
36.0k
                                    src_stride, temp, MAX_SB_SIZE, filters_x,
1286
36.0k
                                    x0_q4, x_step_q4, w, intermediate_height,
1287
36.0k
                                    conv_params->round_0, bd);
1288
36.0k
  highbd_convolve_add_src_vert_hip(
1289
36.0k
      temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1), MAX_SB_SIZE, dst, dst_stride,
1290
36.0k
      filters_y, y0_q4, y_step_q4, w, h, conv_params->round_1, bd);
1291
36.0k
}
1292
#endif  // CONFIG_AV1_HIGHBITDEPTH