Coverage Report

Created: 2025-12-03 07:28

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
32.4k
                             int x_step_qn) {
30
32.4k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
31
1.24M
  for (int y = 0; y < h; ++y) {
32
1.20M
    int x_qn = x0_qn;
33
194M
    for (int x = 0; x < w; ++x) {
34
193M
      const uint8_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
35
193M
      const int x_filter_idx =
36
193M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
37
193M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
38
193M
      const int16_t *const x_filter =
39
193M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
40
193M
      int sum = 0;
41
1.74G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
42
1.54G
        sum += src_x[k] * x_filter[k];
43
193M
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
44
193M
      x_qn += x_step_qn;
45
193M
    }
46
1.20M
    src += src_stride;
47
1.20M
    dst += dst_stride;
48
1.20M
  }
49
32.4k
}
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
129k
                                    int x_step_qn, int bd) {
55
129k
  src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
56
4.90M
  for (int y = 0; y < h; ++y) {
57
4.77M
    int x_qn = x0_qn;
58
302M
    for (int x = 0; x < w; ++x) {
59
297M
      const uint16_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
60
297M
      const int x_filter_idx =
61
297M
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
62
297M
      assert(x_filter_idx <= RS_SUBPEL_MASK);
63
297M
      const int16_t *const x_filter =
64
297M
          &x_filters[x_filter_idx * UPSCALE_NORMATIVE_TAPS];
65
297M
      int sum = 0;
66
2.67G
      for (int k = 0; k < UPSCALE_NORMATIVE_TAPS; ++k)
67
2.37G
        sum += src_x[k] * x_filter[k];
68
297M
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
69
297M
      x_qn += x_step_qn;
70
297M
    }
71
4.77M
    src += src_stride;
72
4.77M
    dst += dst_stride;
73
4.77M
  }
74
129k
}
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
14.6k
                          ConvolveParams *conv_params) {
121
14.6k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
122
14.6k
  int im_h = h + filter_params_y->taps - 1;
123
14.6k
  int im_stride = w;
124
14.6k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
125
14.6k
  const int fo_vert = filter_params_y->taps / 2 - 1;
126
14.6k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
127
14.6k
  const int bd = 8;
128
14.6k
  const int bits =
129
14.6k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
130
131
  // horizontal filter
132
14.6k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
133
14.6k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
134
14.6k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
135
174k
  for (int y = 0; y < im_h; ++y) {
136
1.54M
    for (int x = 0; x < w; ++x) {
137
1.38M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
138
7.12M
      for (int k = 0; k < filter_params_x->taps; ++k) {
139
5.73M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
140
5.73M
      }
141
1.38M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
142
1.38M
      im_block[y * im_stride + x] =
143
1.38M
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
144
1.38M
    }
145
159k
  }
146
147
  // vertical filter
148
14.6k
  int16_t *src_vert = im_block + fo_vert * im_stride;
149
14.6k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
150
14.6k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
151
14.6k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
152
113k
  for (int y = 0; y < h; ++y) {
153
1.15M
    for (int x = 0; x < w; ++x) {
154
1.05M
      int32_t sum = 1 << offset_bits;
155
4.52M
      for (int k = 0; k < filter_params_y->taps; ++k) {
156
3.46M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
157
3.46M
      }
158
1.05M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
159
1.05M
      int16_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
160
1.05M
                    ((1 << (offset_bits - conv_params->round_1)) +
161
1.05M
                     (1 << (offset_bits - conv_params->round_1 - 1)));
162
1.05M
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
163
1.05M
    }
164
98.4k
  }
165
14.6k
}
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
11.8k
                         const int subpel_y_qn) {
171
11.8k
  const int fo_vert = filter_params_y->taps / 2 - 1;
172
173
  // vertical filter
174
11.8k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
175
11.8k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
176
95.8k
  for (int y = 0; y < h; ++y) {
177
1.06M
    for (int x = 0; x < w; ++x) {
178
978k
      int32_t res = 0;
179
3.99M
      for (int k = 0; k < filter_params_y->taps; ++k) {
180
3.01M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
181
3.01M
      }
182
978k
      dst[y * dst_stride + x] =
183
978k
          clip_pixel(ROUND_POWER_OF_TWO(res, FILTER_BITS));
184
978k
    }
185
84.0k
  }
186
11.8k
}
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
9.55k
                         const int subpel_x_qn, ConvolveParams *conv_params) {
192
9.55k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
193
9.55k
  const int bits = FILTER_BITS - conv_params->round_0;
194
195
9.55k
  assert(bits >= 0);
196
9.55k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
197
9.55k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
198
199
  // horizontal filter
200
9.55k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
201
9.55k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
202
203
90.7k
  for (int y = 0; y < h; ++y) {
204
1.33M
    for (int x = 0; x < w; ++x) {
205
1.25M
      int32_t res = 0;
206
4.53M
      for (int k = 0; k < filter_params_x->taps; ++k) {
207
3.28M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
208
3.28M
      }
209
1.25M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
210
1.25M
      dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(res, bits));
211
1.25M
    }
212
81.2k
  }
213
9.55k
}
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
7.30k
                                ConvolveParams *conv_params) {
221
7.30k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
222
7.30k
  int dst16_stride = conv_params->dst_stride;
223
7.30k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
224
7.30k
  int im_h = h + filter_params_y->taps - 1;
225
7.30k
  int im_stride = w;
226
7.30k
  const int fo_vert = filter_params_y->taps / 2 - 1;
227
7.30k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
228
7.30k
  const int bd = 8;
229
7.30k
  const int round_bits =
230
7.30k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
231
232
  // horizontal filter
233
7.30k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
234
7.30k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
235
7.30k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
236
120k
  for (int y = 0; y < im_h; ++y) {
237
992k
    for (int x = 0; x < w; ++x) {
238
879k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
239
7.91M
      for (int k = 0; k < filter_params_x->taps; ++k) {
240
7.03M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
241
7.03M
      }
242
879k
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
243
879k
      im_block[y * im_stride + x] =
244
879k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
245
879k
    }
246
113k
  }
247
248
  // vertical filter
249
7.30k
  int16_t *src_vert = im_block + fo_vert * im_stride;
250
7.30k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
251
7.30k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
252
7.30k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
253
69.3k
  for (int y = 0; y < h; ++y) {
254
550k
    for (int x = 0; x < w; ++x) {
255
488k
      int32_t sum = 1 << offset_bits;
256
4.39M
      for (int k = 0; k < filter_params_y->taps; ++k) {
257
3.91M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
258
3.91M
      }
259
488k
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
260
488k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
261
488k
      if (conv_params->do_average) {
262
209k
        int32_t tmp = dst16[y * dst16_stride + x];
263
209k
        if (conv_params->use_dist_wtd_comp_avg) {
264
70.7k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
265
70.7k
          tmp = tmp >> DIST_PRECISION_BITS;
266
138k
        } else {
267
138k
          tmp += res;
268
138k
          tmp = tmp >> 1;
269
138k
        }
270
209k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
271
209k
               (1 << (offset_bits - conv_params->round_1 - 1));
272
209k
        dst[y * dst_stride + x] =
273
209k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
274
279k
      } else {
275
279k
        dst16[y * dst16_stride + x] = res;
276
279k
      }
277
488k
    }
278
62.0k
  }
279
7.30k
}
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
6.34k
                               ConvolveParams *conv_params) {
286
6.34k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
287
6.34k
  int dst16_stride = conv_params->dst_stride;
288
6.34k
  const int fo_vert = filter_params_y->taps / 2 - 1;
289
6.34k
  const int bits = FILTER_BITS - conv_params->round_0;
290
6.34k
  const int bd = 8;
291
6.34k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
292
6.34k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
293
6.34k
                           (1 << (offset_bits - conv_params->round_1 - 1));
294
6.34k
  const int round_bits =
295
6.34k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
296
297
  // vertical filter
298
6.34k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
299
6.34k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
300
64.0k
  for (int y = 0; y < h; ++y) {
301
513k
    for (int x = 0; x < w; ++x) {
302
455k
      int32_t res = 0;
303
4.10M
      for (int k = 0; k < filter_params_y->taps; ++k) {
304
3.64M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
305
3.64M
      }
306
455k
      res *= (1 << bits);
307
455k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
308
309
455k
      if (conv_params->do_average) {
310
124k
        int32_t tmp = dst16[y * dst16_stride + x];
311
124k
        if (conv_params->use_dist_wtd_comp_avg) {
312
28.9k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
313
28.9k
          tmp = tmp >> DIST_PRECISION_BITS;
314
95.8k
        } else {
315
95.8k
          tmp += res;
316
95.8k
          tmp = tmp >> 1;
317
95.8k
        }
318
124k
        tmp -= round_offset;
319
124k
        dst[y * dst_stride + x] =
320
124k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
321
330k
      } else {
322
330k
        dst16[y * dst16_stride + x] = res;
323
330k
      }
324
455k
    }
325
57.6k
  }
326
6.34k
}
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
2.65k
                               ConvolveParams *conv_params) {
333
2.65k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
334
2.65k
  int dst16_stride = conv_params->dst_stride;
335
2.65k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
336
2.65k
  const int bits = FILTER_BITS - conv_params->round_1;
337
2.65k
  const int bd = 8;
338
2.65k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
339
2.65k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
340
2.65k
                           (1 << (offset_bits - conv_params->round_1 - 1));
341
2.65k
  const int round_bits =
342
2.65k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
343
344
  // horizontal filter
345
2.65k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
346
2.65k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
347
30.1k
  for (int y = 0; y < h; ++y) {
348
251k
    for (int x = 0; x < w; ++x) {
349
224k
      int32_t res = 0;
350
2.01M
      for (int k = 0; k < filter_params_x->taps; ++k) {
351
1.79M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
352
1.79M
      }
353
224k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
354
224k
      res += round_offset;
355
356
224k
      if (conv_params->do_average) {
357
65.6k
        int32_t tmp = dst16[y * dst16_stride + x];
358
65.6k
        if (conv_params->use_dist_wtd_comp_avg) {
359
26.1k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
360
26.1k
          tmp = tmp >> DIST_PRECISION_BITS;
361
39.4k
        } else {
362
39.4k
          tmp += res;
363
39.4k
          tmp = tmp >> 1;
364
39.4k
        }
365
65.6k
        tmp -= round_offset;
366
65.6k
        dst[y * dst_stride + x] =
367
65.6k
            clip_pixel(ROUND_POWER_OF_TWO(tmp, round_bits));
368
158k
      } else {
369
158k
        dst16[y * dst16_stride + x] = res;
370
158k
      }
371
224k
    }
372
27.5k
  }
373
2.65k
}
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
7.33k
                                     ConvolveParams *conv_params) {
378
7.33k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
379
7.33k
  int dst16_stride = conv_params->dst_stride;
380
7.33k
  const int bits =
381
7.33k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
382
7.33k
  const int bd = 8;
383
7.33k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
384
7.33k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
385
7.33k
                           (1 << (offset_bits - conv_params->round_1 - 1));
386
387
70.9k
  for (int y = 0; y < h; ++y) {
388
570k
    for (int x = 0; x < w; ++x) {
389
506k
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
390
506k
      res += round_offset;
391
392
506k
      if (conv_params->do_average) {
393
167k
        int32_t tmp = dst16[y * dst16_stride + x];
394
167k
        if (conv_params->use_dist_wtd_comp_avg) {
395
33.7k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
396
33.7k
          tmp = tmp >> DIST_PRECISION_BITS;
397
133k
        } else {
398
133k
          tmp += res;
399
133k
          tmp = tmp >> 1;
400
133k
        }
401
167k
        tmp -= round_offset;
402
167k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
403
338k
      } else {
404
338k
        dst16[y * dst16_stride + x] = res;
405
338k
      }
406
506k
    }
407
63.5k
  }
408
7.33k
}
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
7.55k
                             ConvolveParams *conv_params) {
417
7.55k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
418
7.55k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
419
7.55k
             filter_params_y->taps;
420
7.55k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
421
7.55k
  const int dst16_stride = conv_params->dst_stride;
422
7.55k
  const int bits =
423
7.55k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
424
7.55k
  assert(bits >= 0);
425
7.55k
  int im_stride = w;
426
7.55k
  const int fo_vert = filter_params_y->taps / 2 - 1;
427
7.55k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
428
7.55k
  const int bd = 8;
429
430
  // horizontal filter
431
7.55k
  const uint8_t *src_horiz = src - fo_vert * src_stride;
432
112k
  for (int y = 0; y < im_h; ++y) {
433
104k
    int x_qn = subpel_x_qn;
434
862k
    for (int x = 0; x < w; ++x, x_qn += x_step_qn) {
435
758k
      const uint8_t *const src_x = &src_horiz[(x_qn >> SCALE_SUBPEL_BITS)];
436
758k
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
437
758k
      assert(x_filter_idx < SUBPEL_SHIFTS);
438
758k
      const int16_t *x_filter =
439
758k
          av1_get_interp_filter_subpel_kernel(filter_params_x, x_filter_idx);
440
758k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
441
6.82M
      for (int k = 0; k < filter_params_x->taps; ++k) {
442
6.06M
        sum += x_filter[k] * src_x[k - fo_horiz];
443
6.06M
      }
444
758k
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
445
758k
      im_block[y * im_stride + x] =
446
758k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
447
758k
    }
448
104k
    src_horiz += src_stride;
449
104k
  }
450
451
  // vertical filter
452
7.55k
  int16_t *src_vert = im_block + fo_vert * im_stride;
453
7.55k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
454
57.6k
  for (int x = 0; x < w; ++x) {
455
50.0k
    int y_qn = subpel_y_qn;
456
449k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
457
399k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
458
399k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
459
399k
      assert(y_filter_idx < SUBPEL_SHIFTS);
460
399k
      const int16_t *y_filter =
461
399k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
462
399k
      int32_t sum = 1 << offset_bits;
463
3.59M
      for (int k = 0; k < filter_params_y->taps; ++k) {
464
3.19M
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
465
3.19M
      }
466
399k
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
467
399k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
468
399k
      if (conv_params->is_compound) {
469
131k
        if (conv_params->do_average) {
470
50.7k
          int32_t tmp = dst16[y * dst16_stride + x];
471
50.7k
          if (conv_params->use_dist_wtd_comp_avg) {
472
11.9k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
473
11.9k
            tmp = tmp >> DIST_PRECISION_BITS;
474
38.7k
          } else {
475
38.7k
            tmp += res;
476
38.7k
            tmp = tmp >> 1;
477
38.7k
          }
478
          /* Subtract round offset and convolve round */
479
50.7k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
480
50.7k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
481
50.7k
          dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
482
80.8k
        } else {
483
80.8k
          dst16[y * dst16_stride + x] = res;
484
80.8k
        }
485
267k
      } else {
486
        /* Subtract round offset and convolve round */
487
267k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
488
267k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
489
267k
        dst[y * dst_stride + x] = clip_pixel(ROUND_POWER_OF_TWO(tmp, bits));
490
267k
      }
491
399k
    }
492
50.0k
    src_vert++;
493
50.0k
  }
494
7.55k
}
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
7.55k
    ConvolveParams *conv_params) {
502
7.55k
  if (conv_params->is_compound) {
503
1.58k
    assert(conv_params->dst != NULL);
504
1.58k
  }
505
7.55k
  av1_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h, filter_params_x,
506
7.55k
                        filter_params_y, subpel_x_qn, x_step_qn, subpel_y_qn,
507
7.55k
                        y_step_qn, conv_params);
508
7.55k
}
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
23.6k
    const int subpel_y_qn, ConvolveParams *conv_params) {
515
23.6k
  const bool need_x = subpel_x_qn != 0;
516
23.6k
  const bool need_y = subpel_y_qn != 0;
517
23.6k
  if (!need_x && !need_y) {
518
7.33k
    av1_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
519
7.33k
                                  conv_params);
520
16.3k
  } else if (need_x && !need_y) {
521
2.65k
    av1_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
522
2.65k
                            filter_params_x, subpel_x_qn, conv_params);
523
13.6k
  } else if (!need_x && need_y) {
524
6.34k
    av1_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
525
6.34k
                            filter_params_y, subpel_y_qn, conv_params);
526
7.30k
  } else {
527
7.30k
    assert(need_y && need_x);
528
7.30k
    av1_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
529
7.30k
                             filter_params_x, filter_params_y, subpel_x_qn,
530
7.30k
                             subpel_y_qn, conv_params);
531
7.30k
  }
532
23.6k
}
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
63.8k
    const int subpel_y_qn, ConvolveParams *conv_params) {
539
63.8k
  const bool need_x = subpel_x_qn != 0;
540
63.8k
  const bool need_y = subpel_y_qn != 0;
541
63.8k
  if (!need_x && !need_y) {
542
48.2k
    aom_convolve_copy(src, src_stride, dst, dst_stride, w, h);
543
48.2k
  } else if (need_x && !need_y) {
544
2.64k
    av1_convolve_x_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
545
2.64k
                      subpel_x_qn, conv_params);
546
12.9k
  } else if (!need_x && need_y) {
547
5.21k
    av1_convolve_y_sr(src, src_stride, dst, dst_stride, w, h, filter_params_y,
548
5.21k
                      subpel_y_qn);
549
7.71k
  } else {
550
7.71k
    assert(need_x && need_y);
551
7.71k
    av1_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h, filter_params_x,
552
7.71k
                       filter_params_y, subpel_x_qn, subpel_y_qn, conv_params);
553
7.71k
  }
554
63.8k
}
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
115k
                            ConvolveParams *conv_params) {
562
115k
  (void)x_step_q4;
563
115k
  (void)y_step_q4;
564
115k
  (void)dst;
565
115k
  (void)dst_stride;
566
567
115k
  const InterpFilterParams *filter_params_x = interp_filters[0];
568
115k
  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
115k
  if (filter_params_x->taps == 2 || filter_params_y->taps == 2) {
574
51.8k
    assert(filter_params_x->taps == 2 && filter_params_y->taps == 2);
575
51.8k
    assert(!scaled);
576
51.8k
    if (subpel_x_qn && subpel_y_qn) {
577
6.89k
      av1_convolve_2d_sr_c(src, src_stride, dst, dst_stride, w, h,
578
6.89k
                           filter_params_x, filter_params_y, subpel_x_qn,
579
6.89k
                           subpel_y_qn, conv_params);
580
6.89k
      return;
581
44.9k
    } else if (subpel_x_qn) {
582
6.91k
      av1_convolve_x_sr_c(src, src_stride, dst, dst_stride, w, h,
583
6.91k
                          filter_params_x, subpel_x_qn, conv_params);
584
6.91k
      return;
585
38.0k
    } else if (subpel_y_qn) {
586
6.60k
      av1_convolve_y_sr_c(src, src_stride, dst, dst_stride, w, h,
587
6.60k
                          filter_params_y, subpel_y_qn);
588
6.60k
      return;
589
6.60k
    }
590
51.8k
  }
591
592
95.0k
  if (scaled) {
593
7.55k
    convolve_2d_scale_wrapper(src, src_stride, dst, dst_stride, w, h,
594
7.55k
                              filter_params_x, filter_params_y, subpel_x_qn,
595
7.55k
                              x_step_q4, subpel_y_qn, y_step_q4, conv_params);
596
87.4k
  } else if (conv_params->is_compound) {
597
23.6k
    convolve_2d_facade_compound(src, src_stride, dst, dst_stride, w, h,
598
23.6k
                                filter_params_x, filter_params_y, subpel_x_qn,
599
23.6k
                                subpel_y_qn, conv_params);
600
63.8k
  } else {
601
63.8k
    convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
602
63.8k
                              filter_params_x, filter_params_y, subpel_x_qn,
603
63.8k
                              subpel_y_qn, conv_params);
604
63.8k
  }
605
95.0k
}
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
34.0k
                                ConvolveParams *conv_params, int bd) {
613
34.0k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
614
34.0k
  const int bits = FILTER_BITS - conv_params->round_0;
615
616
34.0k
  assert(bits >= 0);
617
34.0k
  assert((FILTER_BITS - conv_params->round_1) >= 0 ||
618
34.0k
         ((conv_params->round_0 + conv_params->round_1) == 2 * FILTER_BITS));
619
620
  // horizontal filter
621
34.0k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
622
34.0k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
623
271k
  for (int y = 0; y < h; ++y) {
624
2.70M
    for (int x = 0; x < w; ++x) {
625
2.46M
      int32_t res = 0;
626
10.6M
      for (int k = 0; k < filter_params_x->taps; ++k) {
627
8.19M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
628
8.19M
      }
629
2.46M
      res = ROUND_POWER_OF_TWO(res, conv_params->round_0);
630
2.46M
      dst[y * dst_stride + x] =
631
2.46M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
632
2.46M
    }
633
237k
  }
634
34.0k
}
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
26.3k
                                const int subpel_y_qn, int bd) {
640
26.3k
  const int fo_vert = filter_params_y->taps / 2 - 1;
641
  // vertical filter
642
26.3k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
643
26.3k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
644
194k
  for (int y = 0; y < h; ++y) {
645
1.60M
    for (int x = 0; x < w; ++x) {
646
1.43M
      int32_t res = 0;
647
5.43M
      for (int k = 0; k < filter_params_y->taps; ++k) {
648
4.00M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
649
4.00M
      }
650
1.43M
      dst[y * dst_stride + x] =
651
1.43M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, FILTER_BITS), bd);
652
1.43M
    }
653
168k
  }
654
26.3k
}
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
30.4k
                                 ConvolveParams *conv_params, int bd) {
662
30.4k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
663
30.4k
  int im_h = h + filter_params_y->taps - 1;
664
30.4k
  int im_stride = w;
665
30.4k
  assert(w <= MAX_SB_SIZE && h <= MAX_SB_SIZE);
666
30.4k
  const int fo_vert = filter_params_y->taps / 2 - 1;
667
30.4k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
668
30.4k
  const int bits =
669
30.4k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
670
30.4k
  assert(bits >= 0);
671
672
  // horizontal filter
673
30.4k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
674
30.4k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
675
30.4k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
676
293k
  for (int y = 0; y < im_h; ++y) {
677
2.44M
    for (int x = 0; x < w; ++x) {
678
2.18M
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
679
9.62M
      for (int k = 0; k < filter_params_x->taps; ++k) {
680
7.44M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
681
7.44M
      }
682
2.18M
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
683
2.18M
      im_block[y * im_stride + x] =
684
2.18M
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
685
2.18M
    }
686
263k
  }
687
688
  // vertical filter
689
30.4k
  int16_t *src_vert = im_block + fo_vert * im_stride;
690
30.4k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
691
30.4k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
692
30.4k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
693
228k
  for (int y = 0; y < h; ++y) {
694
1.94M
    for (int x = 0; x < w; ++x) {
695
1.74M
      int32_t sum = 1 << offset_bits;
696
6.82M
      for (int k = 0; k < filter_params_y->taps; ++k) {
697
5.07M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
698
5.07M
      }
699
1.74M
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
700
1.74M
      int32_t res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
701
1.74M
                    ((1 << (offset_bits - conv_params->round_1)) +
702
1.74M
                     (1 << (offset_bits - conv_params->round_1 - 1)));
703
1.74M
      dst[y * dst_stride + x] =
704
1.74M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(res, bits), bd);
705
1.74M
    }
706
198k
  }
707
30.4k
}
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
2.54k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
714
2.54k
  int x, y, k;
715
2.54k
  int16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
716
2.54k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
717
2.54k
  int dst16_stride = conv_params->dst_stride;
718
2.54k
  int im_h = h + filter_params_y->taps - 1;
719
2.54k
  int im_stride = w;
720
2.54k
  const int fo_vert = filter_params_y->taps / 2 - 1;
721
2.54k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
722
2.54k
  const int round_bits =
723
2.54k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
724
2.54k
  assert(round_bits >= 0);
725
726
  // horizontal filter
727
2.54k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
728
2.54k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
729
2.54k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
730
43.0k
  for (y = 0; y < im_h; ++y) {
731
403k
    for (x = 0; x < w; ++x) {
732
362k
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
733
3.26M
      for (k = 0; k < filter_params_x->taps; ++k) {
734
2.90M
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
735
2.90M
      }
736
362k
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
737
362k
      (void)bd;
738
362k
      im_block[y * im_stride + x] =
739
362k
          (int16_t)ROUND_POWER_OF_TWO(sum, conv_params->round_0);
740
362k
    }
741
40.5k
  }
742
743
  // vertical filter
744
2.54k
  int16_t *src_vert = im_block + fo_vert * im_stride;
745
2.54k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
746
2.54k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
747
2.54k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
748
25.3k
  for (y = 0; y < h; ++y) {
749
233k
    for (x = 0; x < w; ++x) {
750
211k
      int32_t sum = 1 << offset_bits;
751
1.89M
      for (k = 0; k < filter_params_y->taps; ++k) {
752
1.68M
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
753
1.68M
      }
754
211k
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
755
211k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
756
211k
      if (conv_params->do_average) {
757
79.9k
        int32_t tmp = dst16[y * dst16_stride + x];
758
79.9k
        if (conv_params->use_dist_wtd_comp_avg) {
759
17.3k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
760
17.3k
          tmp = tmp >> DIST_PRECISION_BITS;
761
62.5k
        } else {
762
62.5k
          tmp += res;
763
62.5k
          tmp = tmp >> 1;
764
62.5k
        }
765
79.9k
        tmp -= (1 << (offset_bits - conv_params->round_1)) +
766
79.9k
               (1 << (offset_bits - conv_params->round_1 - 1));
767
79.9k
        dst[y * dst_stride + x] =
768
79.9k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
769
131k
      } else {
770
131k
        dst16[y * dst16_stride + x] = res;
771
131k
      }
772
211k
    }
773
22.7k
  }
774
2.54k
}
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.04k
                                      ConvolveParams *conv_params, int bd) {
782
6.04k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
783
6.04k
  int dst16_stride = conv_params->dst_stride;
784
6.04k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
785
6.04k
  const int bits = FILTER_BITS - conv_params->round_1;
786
6.04k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
787
6.04k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
788
6.04k
                           (1 << (offset_bits - conv_params->round_1 - 1));
789
6.04k
  const int round_bits =
790
6.04k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
791
6.04k
  assert(round_bits >= 0);
792
6.04k
  assert(bits >= 0);
793
  // horizontal filter
794
6.04k
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
795
6.04k
      filter_params_x, subpel_x_qn & SUBPEL_MASK);
796
64.5k
  for (int y = 0; y < h; ++y) {
797
511k
    for (int x = 0; x < w; ++x) {
798
453k
      int32_t res = 0;
799
4.08M
      for (int k = 0; k < filter_params_x->taps; ++k) {
800
3.62M
        res += x_filter[k] * src[y * src_stride + x - fo_horiz + k];
801
3.62M
      }
802
453k
      res = (1 << bits) * ROUND_POWER_OF_TWO(res, conv_params->round_0);
803
453k
      res += round_offset;
804
805
453k
      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
194k
        dst16[y * dst16_stride + x] = res;
819
194k
      }
820
453k
    }
821
58.4k
  }
822
6.04k
}
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
1.47k
                                      ConvolveParams *conv_params, int bd) {
830
1.47k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
831
1.47k
  int dst16_stride = conv_params->dst_stride;
832
1.47k
  const int fo_vert = filter_params_y->taps / 2 - 1;
833
1.47k
  const int bits = FILTER_BITS - conv_params->round_0;
834
1.47k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
835
1.47k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
836
1.47k
                           (1 << (offset_bits - conv_params->round_1 - 1));
837
1.47k
  const int round_bits =
838
1.47k
      2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
839
1.47k
  assert(round_bits >= 0);
840
1.47k
  assert(bits >= 0);
841
  // vertical filter
842
1.47k
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
843
1.47k
      filter_params_y, subpel_y_qn & SUBPEL_MASK);
844
15.5k
  for (int y = 0; y < h; ++y) {
845
149k
    for (int x = 0; x < w; ++x) {
846
135k
      int32_t res = 0;
847
1.22M
      for (int k = 0; k < filter_params_y->taps; ++k) {
848
1.08M
        res += y_filter[k] * src[(y - fo_vert + k) * src_stride + x];
849
1.08M
      }
850
135k
      res *= (1 << bits);
851
135k
      res = ROUND_POWER_OF_TWO(res, conv_params->round_1) + round_offset;
852
853
135k
      if (conv_params->do_average) {
854
71.0k
        int32_t tmp = dst16[y * dst16_stride + x];
855
71.0k
        if (conv_params->use_dist_wtd_comp_avg) {
856
20.2k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
857
20.2k
          tmp = tmp >> DIST_PRECISION_BITS;
858
50.8k
        } else {
859
50.8k
          tmp += res;
860
50.8k
          tmp = tmp >> 1;
861
50.8k
        }
862
71.0k
        tmp -= round_offset;
863
71.0k
        dst[y * dst_stride + x] =
864
71.0k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, round_bits), bd);
865
71.0k
      } else {
866
64.6k
        dst16[y * dst16_stride + x] = res;
867
64.6k
      }
868
135k
    }
869
14.0k
  }
870
1.47k
}
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.0k
                                            int bd) {
877
25.0k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
878
25.0k
  int dst16_stride = conv_params->dst_stride;
879
25.0k
  const int bits =
880
25.0k
      FILTER_BITS * 2 - conv_params->round_1 - conv_params->round_0;
881
25.0k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
882
25.0k
  const int round_offset = (1 << (offset_bits - conv_params->round_1)) +
883
25.0k
                           (1 << (offset_bits - conv_params->round_1 - 1));
884
25.0k
  assert(bits >= 0);
885
886
244k
  for (int y = 0; y < h; ++y) {
887
2.04M
    for (int x = 0; x < w; ++x) {
888
1.82M
      CONV_BUF_TYPE res = src[y * src_stride + x] << bits;
889
1.82M
      res += round_offset;
890
1.82M
      if (conv_params->do_average) {
891
677k
        int32_t tmp = dst16[y * dst16_stride + x];
892
677k
        if (conv_params->use_dist_wtd_comp_avg) {
893
266k
          tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
894
266k
          tmp = tmp >> DIST_PRECISION_BITS;
895
410k
        } else {
896
410k
          tmp += res;
897
410k
          tmp = tmp >> 1;
898
410k
        }
899
677k
        tmp -= round_offset;
900
677k
        dst[y * dst_stride + x] =
901
677k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
902
1.14M
      } else {
903
1.14M
        dst16[y * dst16_stride + x] = res;
904
1.14M
      }
905
1.82M
    }
906
219k
  }
907
25.0k
}
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.52k
                                    ConvolveParams *conv_params, int bd) {
916
7.52k
  int16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
917
7.52k
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
918
7.52k
             filter_params_y->taps;
919
7.52k
  int im_stride = w;
920
7.52k
  const int fo_vert = filter_params_y->taps / 2 - 1;
921
7.52k
  const int fo_horiz = filter_params_x->taps / 2 - 1;
922
7.52k
  CONV_BUF_TYPE *dst16 = conv_params->dst;
923
7.52k
  const int dst16_stride = conv_params->dst_stride;
924
7.52k
  const int bits =
925
7.52k
      FILTER_BITS * 2 - conv_params->round_0 - conv_params->round_1;
926
7.52k
  assert(bits >= 0);
927
  // horizontal filter
928
7.52k
  const uint16_t *src_horiz = src - fo_vert * src_stride;
929
119k
  for (int y = 0; y < im_h; ++y) {
930
111k
    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
111k
    src_horiz += src_stride;
946
111k
  }
947
948
  // vertical filter
949
7.52k
  int16_t *src_vert = im_block + fo_vert * im_stride;
950
7.52k
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
951
65.3k
  for (int x = 0; x < w; ++x) {
952
57.8k
    int y_qn = subpel_y_qn;
953
556k
    for (int y = 0; y < h; ++y, y_qn += y_step_qn) {
954
498k
      const int16_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_stride];
955
498k
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
956
498k
      assert(y_filter_idx < SUBPEL_SHIFTS);
957
498k
      const int16_t *y_filter =
958
498k
          av1_get_interp_filter_subpel_kernel(filter_params_y, y_filter_idx);
959
498k
      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
498k
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
964
498k
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
965
498k
      if (conv_params->is_compound) {
966
77.0k
        if (conv_params->do_average) {
967
31.8k
          int32_t tmp = dst16[y * dst16_stride + x];
968
31.8k
          if (conv_params->use_dist_wtd_comp_avg) {
969
12.6k
            tmp = tmp * conv_params->fwd_offset + res * conv_params->bck_offset;
970
12.6k
            tmp = tmp >> DIST_PRECISION_BITS;
971
19.2k
          } else {
972
19.2k
            tmp += res;
973
19.2k
            tmp = tmp >> 1;
974
19.2k
          }
975
          /* Subtract round offset and convolve round */
976
31.8k
          tmp = tmp - ((1 << (offset_bits - conv_params->round_1)) +
977
31.8k
                       (1 << (offset_bits - conv_params->round_1 - 1)));
978
31.8k
          dst[y * dst_stride + x] =
979
31.8k
              clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
980
45.2k
        } else {
981
45.2k
          dst16[y * dst16_stride + x] = res;
982
45.2k
        }
983
421k
      } else {
984
        /* Subtract round offset and convolve round */
985
421k
        int32_t tmp = res - ((1 << (offset_bits - conv_params->round_1)) +
986
421k
                             (1 << (offset_bits - conv_params->round_1 - 1)));
987
421k
        dst[y * dst_stride + x] =
988
421k
            clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp, bits), bd);
989
421k
      }
990
498k
    }
991
57.8k
    src_vert++;
992
57.8k
  }
993
7.52k
}
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
35.1k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1000
35.1k
  const bool need_x = subpel_x_qn != 0;
1001
35.1k
  const bool need_y = subpel_y_qn != 0;
1002
35.1k
  if (!need_x && !need_y) {
1003
25.0k
    av1_highbd_dist_wtd_convolve_2d_copy(src, src_stride, dst, dst_stride, w, h,
1004
25.0k
                                         conv_params, bd);
1005
25.0k
  } else if (need_x && !need_y) {
1006
6.04k
    av1_highbd_dist_wtd_convolve_x(src, src_stride, dst, dst_stride, w, h,
1007
6.04k
                                   filter_params_x, subpel_x_qn, conv_params,
1008
6.04k
                                   bd);
1009
6.04k
  } else if (!need_x && need_y) {
1010
1.47k
    av1_highbd_dist_wtd_convolve_y(src, src_stride, dst, dst_stride, w, h,
1011
1.47k
                                   filter_params_y, subpel_y_qn, conv_params,
1012
1.47k
                                   bd);
1013
2.54k
  } else {
1014
2.54k
    assert(need_x && need_y);
1015
2.54k
    av1_highbd_dist_wtd_convolve_2d(src, src_stride, dst, dst_stride, w, h,
1016
2.54k
                                    filter_params_x, filter_params_y,
1017
2.54k
                                    subpel_x_qn, subpel_y_qn, conv_params, bd);
1018
2.54k
  }
1019
35.1k
}
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
239k
    const int subpel_y_qn, ConvolveParams *conv_params, int bd) {
1026
239k
  const bool need_x = subpel_x_qn != 0;
1027
239k
  const bool need_y = subpel_y_qn != 0;
1028
1029
239k
  if (!need_x && !need_y) {
1030
148k
    aom_highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h);
1031
148k
  } else if (need_x && !need_y) {
1032
34.0k
    av1_highbd_convolve_x_sr(src, src_stride, dst, dst_stride, w, h,
1033
34.0k
                             filter_params_x, subpel_x_qn, conv_params, bd);
1034
56.7k
  } else if (!need_x && need_y) {
1035
26.3k
    av1_highbd_convolve_y_sr(src, src_stride, dst, dst_stride, w, h,
1036
26.3k
                             filter_params_y, subpel_y_qn, bd);
1037
30.4k
  } else {
1038
30.4k
    assert(need_x && need_y);
1039
30.4k
    av1_highbd_convolve_2d_sr(src, src_stride, dst, dst_stride, w, h,
1040
30.4k
                              filter_params_x, filter_params_y, subpel_x_qn,
1041
30.4k
                              subpel_y_qn, conv_params, bd);
1042
30.4k
  }
1043
239k
}
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
281k
                                   int bd) {
1052
281k
  (void)x_step_q4;
1053
281k
  (void)y_step_q4;
1054
281k
  (void)dst_stride;
1055
281k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1056
1057
281k
  const int need_filter_params_x = (subpel_x_qn != 0) | scaled;
1058
281k
  const int need_filter_params_y = (subpel_y_qn != 0) | scaled;
1059
281k
  const InterpFilterParams *filter_params_x =
1060
281k
      need_filter_params_x ? interp_filters[0] : NULL;
1061
281k
  const InterpFilterParams *filter_params_y =
1062
281k
      need_filter_params_y ? interp_filters[1] : NULL;
1063
1064
281k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1065
281k
  if (scaled) {
1066
7.52k
    if (conv_params->is_compound) {
1067
702
      assert(conv_params->dst != NULL);
1068
702
    }
1069
7.52k
    av1_highbd_convolve_2d_scale(src, src_stride, dst, dst_stride, w, h,
1070
7.52k
                                 filter_params_x, filter_params_y, subpel_x_qn,
1071
7.52k
                                 x_step_q4, subpel_y_qn, y_step_q4, conv_params,
1072
7.52k
                                 bd);
1073
274k
  } else if (conv_params->is_compound) {
1074
35.1k
    highbd_convolve_2d_facade_compound(
1075
35.1k
        src, src_stride, dst, dst_stride, w, h, filter_params_x,
1076
35.1k
        filter_params_y, subpel_x_qn, subpel_y_qn, conv_params, bd);
1077
239k
  } else {
1078
239k
    highbd_convolve_2d_facade_single(src, src_stride, dst, dst_stride, w, h,
1079
239k
                                     filter_params_x, filter_params_y,
1080
239k
                                     subpel_x_qn, subpel_y_qn, conv_params, bd);
1081
239k
  }
1082
281k
}
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
53.0M
static INLINE int horz_scalar_product(const uint8_t *a, const int16_t *b) {
1100
53.0M
  int sum = 0;
1101
477M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1102
53.0M
  return sum;
1103
53.0M
}
1104
1105
#if CONFIG_AV1_HIGHBITDEPTH
1106
static INLINE int highbd_horz_scalar_product(const uint16_t *a,
1107
102M
                                             const int16_t *b) {
1108
102M
  int sum = 0;
1109
926M
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
1110
102M
  return sum;
1111
102M
}
1112
#endif
1113
1114
static INLINE int highbd_vert_scalar_product(const uint16_t *a,
1115
                                             ptrdiff_t a_stride,
1116
138M
                                             const int16_t *b) {
1117
138M
  int sum = 0;
1118
1.24G
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
1119
138M
  return sum;
1120
138M
}
1121
1122
110k
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
110k
  return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
1126
110k
}
1127
1128
110k
static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
1129
110k
  return (int)((const InterpKernel *)(intptr_t)f - base);
1130
110k
}
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
19.6k
                                       int round0_bits) {
1137
19.6k
  const int bd = 8;
1138
19.6k
  src -= SUBPEL_TAPS / 2 - 1;
1139
1.12M
  for (int y = 0; y < h; ++y) {
1140
1.10M
    int x_q4 = x0_q4;
1141
54.1M
    for (int x = 0; x < w; ++x) {
1142
53.0M
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1143
53.0M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1144
53.0M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1145
53.0M
                           (1 << (bd + FILTER_BITS - 1));
1146
53.0M
      const int sum = horz_scalar_product(src_x, x_filter) + rounding;
1147
53.0M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1148
53.0M
                               WIENER_CLAMP_LIMIT(round0_bits, bd) - 1);
1149
53.0M
      x_q4 += x_step_q4;
1150
53.0M
    }
1151
1.10M
    src += src_stride;
1152
1.10M
    dst += dst_stride;
1153
1.10M
  }
1154
19.6k
}
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
19.6k
                                      int round1_bits) {
1161
19.6k
  const int bd = 8;
1162
19.6k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1163
1164
921k
  for (int x = 0; x < w; ++x) {
1165
901k
    int y_q4 = y0_q4;
1166
48.5M
    for (int y = 0; y < h; ++y) {
1167
47.6M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1168
47.6M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1169
47.6M
      const int rounding =
1170
47.6M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1171
47.6M
          (1 << (bd + round1_bits - 1));
1172
47.6M
      const int sum =
1173
47.6M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1174
47.6M
      dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, round1_bits));
1175
47.6M
      y_q4 += y_step_q4;
1176
47.6M
    }
1177
901k
    ++src;
1178
901k
    ++dst;
1179
901k
  }
1180
19.6k
}
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
19.6k
                                   const ConvolveParams *conv_params) {
1188
19.6k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1189
19.6k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1190
1191
19.6k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1192
19.6k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1193
1194
19.6k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1195
19.6k
  const int intermediate_height =
1196
19.6k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS - 1;
1197
19.6k
  memset(temp + (intermediate_height * MAX_SB_SIZE), 0, MAX_SB_SIZE);
1198
1199
19.6k
  assert(w <= MAX_SB_SIZE);
1200
19.6k
  assert(h <= MAX_SB_SIZE);
1201
19.6k
  assert(y_step_q4 <= 32);
1202
19.6k
  assert(x_step_q4 <= 32);
1203
1204
19.6k
  convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1205
19.6k
                             src_stride, temp, MAX_SB_SIZE, filters_x, x0_q4,
1206
19.6k
                             x_step_q4, w, intermediate_height,
1207
19.6k
                             conv_params->round_0);
1208
19.6k
  convolve_add_src_vert_hip(temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1),
1209
19.6k
                            MAX_SB_SIZE, dst, dst_stride, filters_y, y0_q4,
1210
19.6k
                            y_step_q4, w, h, conv_params->round_1);
1211
19.6k
}
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
35.4k
    int x_step_q4, int w, int h, int round0_bits, int bd) {
1218
35.4k
  const int extraprec_clamp_limit = WIENER_CLAMP_LIMIT(round0_bits, bd);
1219
35.4k
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1220
35.4k
  src -= SUBPEL_TAPS / 2 - 1;
1221
2.17M
  for (int y = 0; y < h; ++y) {
1222
2.14M
    int x_q4 = x0_q4;
1223
105M
    for (int x = 0; x < w; ++x) {
1224
102M
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
1225
102M
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
1226
102M
      const int rounding = ((int)src_x[SUBPEL_TAPS / 2 - 1] << FILTER_BITS) +
1227
102M
                           (1 << (bd + FILTER_BITS - 1));
1228
102M
      const int sum = highbd_horz_scalar_product(src_x, x_filter) + rounding;
1229
102M
      dst[x] = (uint16_t)clamp(ROUND_POWER_OF_TWO(sum, round0_bits), 0,
1230
102M
                               extraprec_clamp_limit - 1);
1231
102M
      x_q4 += x_step_q4;
1232
102M
    }
1233
2.14M
    src += src_stride;
1234
2.14M
    dst += dst_stride;
1235
2.14M
  }
1236
35.4k
}
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
35.4k
    int y_step_q4, int w, int h, int round1_bits, int bd) {
1242
35.4k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1243
35.4k
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1244
1.73M
  for (int x = 0; x < w; ++x) {
1245
1.69M
    int y_q4 = y0_q4;
1246
92.7M
    for (int y = 0; y < h; ++y) {
1247
91.0M
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1248
91.0M
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
1249
91.0M
      const int rounding =
1250
91.0M
          ((int)src_y[(SUBPEL_TAPS / 2 - 1) * src_stride] << FILTER_BITS) -
1251
91.0M
          (1 << (bd + round1_bits - 1));
1252
91.0M
      const int sum =
1253
91.0M
          highbd_vert_scalar_product(src_y, src_stride, y_filter) + rounding;
1254
91.0M
      dst[y * dst_stride] =
1255
91.0M
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, round1_bits), bd);
1256
91.0M
      y_q4 += y_step_q4;
1257
91.0M
    }
1258
1.69M
    ++src;
1259
1.69M
    ++dst;
1260
1.69M
  }
1261
35.4k
}
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
35.4k
    const ConvolveParams *conv_params, int bd) {
1268
35.4k
  const InterpKernel *const filters_x = get_filter_base(filter_x);
1269
35.4k
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
1270
1271
35.4k
  const InterpKernel *const filters_y = get_filter_base(filter_y);
1272
35.4k
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
1273
1274
35.4k
  uint16_t temp[WIENER_MAX_EXT_SIZE * MAX_SB_SIZE];
1275
35.4k
  const int intermediate_height =
1276
35.4k
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
1277
1278
35.4k
  assert(w <= MAX_SB_SIZE);
1279
35.4k
  assert(h <= MAX_SB_SIZE);
1280
35.4k
  assert(y_step_q4 <= 32);
1281
35.4k
  assert(x_step_q4 <= 32);
1282
35.4k
  assert(bd + FILTER_BITS - conv_params->round_0 + 2 <= 16);
1283
1284
35.4k
  highbd_convolve_add_src_horiz_hip(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1285
35.4k
                                    src_stride, temp, MAX_SB_SIZE, filters_x,
1286
35.4k
                                    x0_q4, x_step_q4, w, intermediate_height,
1287
35.4k
                                    conv_params->round_0, bd);
1288
35.4k
  highbd_convolve_add_src_vert_hip(
1289
35.4k
      temp + MAX_SB_SIZE * (SUBPEL_TAPS / 2 - 1), MAX_SB_SIZE, dst, dst_stride,
1290
35.4k
      filters_y, y0_q4, y_step_q4, w, h, conv_params->round_1, bd);
1291
35.4k
}
1292
#endif  // CONFIG_AV1_HIGHBITDEPTH