convolve.c 59.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

12
#include <assert.h>
Angie Chiang's avatar
Angie Chiang committed
13
#include <string.h>
14

15
#include "./aom_dsp_rtcd.h"
Yaowu Xu's avatar
Yaowu Xu committed
16
#include "./av1_rtcd.h"
17
#include "av1/common/convolve.h"
18
#include "av1/common/filter.h"
Angie Chiang's avatar
Angie Chiang committed
19
#include "av1/common/onyxc_int.h"
Yaowu Xu's avatar
Yaowu Xu committed
20
#include "aom_dsp/aom_dsp_common.h"
21
#include "aom_ports/mem.h"
22

23 24
#define MAX_BLOCK_WIDTH (MAX_SB_SIZE)
#define MAX_BLOCK_HEIGHT (MAX_SB_SIZE)
25 26
#define MAX_STEP (32)

Yaowu Xu's avatar
Yaowu Xu committed
27 28 29
void av1_convolve_horiz_c(const uint8_t *src, int src_stride, uint8_t *dst,
                          int dst_stride, int w, int h,
                          const InterpFilterParams filter_params,
30 31
                          const int subpel_x_q4, int x_step_q4,
                          ConvolveParams *conv_params) {
32
  int x, y;
33
  int filter_size = filter_params.taps;
Angie Chiang's avatar
Angie Chiang committed
34
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
35 36 37 38 39
  src -= filter_size / 2 - 1;
  for (y = 0; y < h; ++y) {
    int x_q4 = subpel_x_q4;
    for (x = 0; x < w; ++x) {
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
Yaowu Xu's avatar
Yaowu Xu committed
40
      const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
41
          filter_params, x_q4 & SUBPEL_MASK);
42 43
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k) sum += src_x[k] * x_filter[k];
44

Angie Chiang's avatar
Angie Chiang committed
45
      sum = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
46
      if (conv_params->do_average)
Angie Chiang's avatar
Angie Chiang committed
47 48 49
        dst[x] = ROUND_POWER_OF_TWO(dst[x] + sum, 1);
      else
        dst[x] = sum;
50

51 52 53 54 55 56 57
      x_q4 += x_step_q4;
    }
    src += src_stride;
    dst += dst_stride;
  }
}

Fergus Simpson's avatar
Fergus Simpson committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
void av1_convolve_horiz_scale(const uint8_t *src, int src_stride, uint8_t *dst,
                              int dst_stride, int w, int h,
                              const InterpFilterParams filter_params,
                              const int subpel_x_qn, int x_step_qn,
                              ConvolveParams *conv_params) {
  int x, y;
  int filter_size = filter_params.taps;
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  src -= filter_size / 2 - 1;
  for (y = 0; y < h; ++y) {
    int x_qn = subpel_x_qn;
    for (x = 0; x < w; ++x) {
      const uint8_t *const src_x = &src[x_qn >> SCALE_SUBPEL_BITS];
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
      assert(x_filter_idx < SUBPEL_SHIFTS);
      const int16_t *x_filter =
          av1_get_interp_filter_subpel_kernel(filter_params, x_filter_idx);
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k) sum += src_x[k] * x_filter[k];

      sum = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
      if (conv_params->do_average)
        dst[x] = ROUND_POWER_OF_TWO(dst[x] + sum, 1);
      else
        dst[x] = sum;

      x_qn += x_step_qn;
    }
    src += src_stride;
    dst += dst_stride;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
91 92 93
void av1_convolve_vert_c(const uint8_t *src, int src_stride, uint8_t *dst,
                         int dst_stride, int w, int h,
                         const InterpFilterParams filter_params,
94 95
                         const int subpel_y_q4, int y_step_q4,
                         ConvolveParams *conv_params) {
96
  int x, y;
97
  int filter_size = filter_params.taps;
Angie Chiang's avatar
Angie Chiang committed
98
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
99 100 101 102 103
  src -= src_stride * (filter_size / 2 - 1);
  for (x = 0; x < w; ++x) {
    int y_q4 = subpel_y_q4;
    for (y = 0; y < h; ++y) {
      const uint8_t *const src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
Yaowu Xu's avatar
Yaowu Xu committed
104
      const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
105
          filter_params, y_q4 & SUBPEL_MASK);
106 107 108
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k)
        sum += src_y[k * src_stride] * y_filter[k];
109

Angie Chiang's avatar
Angie Chiang committed
110
      sum = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
111
      if (conv_params->do_average)
Angie Chiang's avatar
Angie Chiang committed
112 113 114
        dst[y * dst_stride] = ROUND_POWER_OF_TWO(dst[y * dst_stride] + sum, 1);
      else
        dst[y * dst_stride] = sum;
115

116 117 118 119 120 121 122
      y_q4 += y_step_q4;
    }
    ++src;
    ++dst;
  }
}

Fergus Simpson's avatar
Fergus Simpson committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
void av1_convolve_vert_scale(const uint8_t *src, int src_stride, uint8_t *dst,
                             int dst_stride, int w, int h,
                             const InterpFilterParams filter_params,
                             const int subpel_y_qn, int y_step_qn,
                             ConvolveParams *conv_params) {
  int x, y;
  int filter_size = filter_params.taps;
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  src -= src_stride * (filter_size / 2 - 1);
  for (x = 0; x < w; ++x) {
    int y_qn = subpel_y_qn;
    for (y = 0; y < h; ++y) {
      const uint8_t *const src_y =
          &src[(y_qn >> SCALE_SUBPEL_BITS) * src_stride];
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
      assert(y_filter_idx < SUBPEL_SHIFTS);
      const int16_t *y_filter =
          av1_get_interp_filter_subpel_kernel(filter_params, y_filter_idx);
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k)
        sum += src_y[k * src_stride] * y_filter[k];

      sum = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
      if (conv_params->do_average)
        dst[y * dst_stride] = ROUND_POWER_OF_TWO(dst[y * dst_stride] + sum, 1);
      else
        dst[y * dst_stride] = sum;

      y_qn += y_step_qn;
    }
    ++src;
    ++dst;
  }
}

Angie Chiang's avatar
Angie Chiang committed
158
static void convolve_copy(const uint8_t *src, int src_stride, uint8_t *dst,
159 160
                          int dst_stride, int w, int h,
                          ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
161
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
162
  if (conv_params->do_average == 0) {
Angie Chiang's avatar
Angie Chiang committed
163
    int r;
Angie Chiang's avatar
Angie Chiang committed
164
    for (r = 0; r < h; ++r) {
Angie Chiang's avatar
Angie Chiang committed
165
      memcpy(dst, src, w);
Angie Chiang's avatar
Angie Chiang committed
166 167 168 169 170 171
      src += src_stride;
      dst += dst_stride;
    }
  } else {
    int r, c;
    for (r = 0; r < h; ++r) {
172
      for (c = 0; c < w; ++c) {
Angie Chiang's avatar
Angie Chiang committed
173
        dst[c] = clip_pixel(ROUND_POWER_OF_TWO(dst[c] + src[c], 1));
174
      }
Angie Chiang's avatar
Angie Chiang committed
175 176 177 178 179 180
      src += src_stride;
      dst += dst_stride;
    }
  }
}

181 182 183
void av1_convolve_horiz_facade(const uint8_t *src, int src_stride, uint8_t *dst,
                               int dst_stride, int w, int h,
                               const InterpFilterParams filter_params,
184 185
                               const int subpel_x_q4, int x_step_q4,
                               ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
186 187
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
188 189
    const int16_t *filter_x =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_x_q4);
190
    if (conv_params->do_average == 0)
191 192 193 194 195 196
      aom_convolve8_horiz(src, src_stride, dst, dst_stride, filter_x, x_step_q4,
                          NULL, -1, w, h);
    else
      aom_convolve8_avg_horiz(src, src_stride, dst, dst_stride, filter_x,
                              x_step_q4, NULL, -1, w, h);
  } else {
Angie Chiang's avatar
Angie Chiang committed
197 198
    av1_convolve_horiz(src, src_stride, dst, dst_stride, w, h, filter_params,
                       subpel_x_q4, x_step_q4, conv_params);
199 200 201
  }
}

202 203 204 205 206 207 208 209 210
void av1_convolve_horiz_facade_c(const uint8_t *src, int src_stride,
                                 uint8_t *dst, int dst_stride, int w, int h,
                                 const InterpFilterParams filter_params,
                                 const int subpel_x_q4, int x_step_q4,
                                 ConvolveParams *conv_params) {
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
    const int16_t *filter_x =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_x_q4);
211
    if (conv_params->do_average == 0)
212 213 214 215 216 217 218 219 220 221 222
      aom_convolve8_horiz_c(src, src_stride, dst, dst_stride, filter_x,
                            x_step_q4, NULL, -1, w, h);
    else
      aom_convolve8_avg_horiz_c(src, src_stride, dst, dst_stride, filter_x,
                                x_step_q4, NULL, -1, w, h);
  } else {
    av1_convolve_horiz_c(src, src_stride, dst, dst_stride, w, h, filter_params,
                         subpel_x_q4, x_step_q4, conv_params);
  }
}

Fergus Simpson's avatar
Fergus Simpson committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
void av1_convolve_horiz_facade_scale(const uint8_t *src, int src_stride,
                                     uint8_t *dst, int dst_stride, int w, int h,
                                     const InterpFilterParams filter_params,
                                     const int subpel_x_qn, int x_step_qn,
                                     ConvolveParams *conv_params) {
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
    const int16_t *filter_x = av1_get_interp_filter_subpel_kernel(
        filter_params, subpel_x_qn >> SCALE_EXTRA_BITS);
    if (conv_params->do_average == 0)
      aom_convolve8_horiz_scale(src, src_stride, dst, dst_stride, filter_x,
                                subpel_x_qn, x_step_qn, NULL, 0, -1, w, h);
    else
      aom_convolve8_avg_horiz_scale(src, src_stride, dst, dst_stride, filter_x,
                                    subpel_x_qn, x_step_qn, NULL, 0, -1, w, h);
  } else {
    av1_convolve_horiz_scale(src, src_stride, dst, dst_stride, w, h,
                             filter_params, subpel_x_qn, x_step_qn,
                             conv_params);
  }
}

245 246 247
void av1_convolve_vert_facade(const uint8_t *src, int src_stride, uint8_t *dst,
                              int dst_stride, int w, int h,
                              const InterpFilterParams filter_params,
248 249
                              const int subpel_y_q4, int y_step_q4,
                              ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
250 251
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
252 253
    const int16_t *filter_y =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_y_q4);
254
    if (conv_params->do_average == 0) {
255 256 257 258 259 260 261
      aom_convolve8_vert(src, src_stride, dst, dst_stride, NULL, -1, filter_y,
                         y_step_q4, w, h);
    } else {
      aom_convolve8_avg_vert(src, src_stride, dst, dst_stride, NULL, -1,
                             filter_y, y_step_q4, w, h);
    }
  } else {
Angie Chiang's avatar
Angie Chiang committed
262 263
    av1_convolve_vert(src, src_stride, dst, dst_stride, w, h, filter_params,
                      subpel_y_q4, y_step_q4, conv_params);
264 265 266
  }
}

267 268 269 270 271 272 273 274 275
void av1_convolve_vert_facade_c(const uint8_t *src, int src_stride,
                                uint8_t *dst, int dst_stride, int w, int h,
                                const InterpFilterParams filter_params,
                                const int subpel_y_q4, int y_step_q4,
                                ConvolveParams *conv_params) {
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
    const int16_t *filter_y =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_y_q4);
276
    if (conv_params->do_average == 0) {
277 278 279 280 281 282 283 284 285 286 287 288
      aom_convolve8_vert_c(src, src_stride, dst, dst_stride, NULL, -1, filter_y,
                           y_step_q4, w, h);
    } else {
      aom_convolve8_avg_vert_c(src, src_stride, dst, dst_stride, NULL, -1,
                               filter_y, y_step_q4, w, h);
    }
  } else {
    av1_convolve_vert_c(src, src_stride, dst, dst_stride, w, h, filter_params,
                        subpel_y_q4, y_step_q4, conv_params);
  }
}

Fergus Simpson's avatar
Fergus Simpson committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
void av1_convolve_vert_facade_scale(const uint8_t *src, int src_stride,
                                    uint8_t *dst, int dst_stride, int w, int h,
                                    const InterpFilterParams filter_params,
                                    const int subpel_y_qn, int y_step_qn,
                                    ConvolveParams *conv_params) {
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
    const int16_t *filter_y = av1_get_interp_filter_subpel_kernel(
        filter_params, subpel_y_qn >> SCALE_EXTRA_BITS);
    if (conv_params->do_average == 0) {
      aom_convolve8_vert_scale(src, src_stride, dst, dst_stride, NULL, 0, -1,
                               filter_y, subpel_y_qn, y_step_qn, w, h);
    } else {
      aom_convolve8_avg_vert_scale(src, src_stride, dst, dst_stride, NULL, 0,
                                   -1, filter_y, subpel_y_qn, y_step_qn, w, h);
    }
  } else {
    av1_convolve_vert_scale(src, src_stride, dst, dst_stride, w, h,
                            filter_params, subpel_y_qn, y_step_qn, conv_params);
  }
}

Angie Chiang's avatar
Angie Chiang committed
311
#if CONFIG_CONVOLVE_ROUND
312 313
void av1_convolve_rounding_c(const int32_t *src, int src_stride, uint8_t *dst,
                             int dst_stride, int w, int h, int bits) {
Angie Chiang's avatar
Angie Chiang committed
314 315 316 317
  int r, c;
  for (r = 0; r < h; ++r) {
    for (c = 0; c < w; ++c) {
      dst[r * dst_stride + c] =
318
          clip_pixel(ROUND_POWER_OF_TWO(src[r * src_stride + c], bits));
Angie Chiang's avatar
Angie Chiang committed
319 320 321
    }
  }
}
Angie Chiang's avatar
Angie Chiang committed
322

323
#if CONFIG_COMPOUND_ROUND
David Barker's avatar
David Barker committed
324 325 326 327 328 329
void av1_convolve_2d_c(const uint8_t *src, int src_stride, CONV_BUF_TYPE *dst,
                       int dst_stride, int w, int h,
                       InterpFilterParams *filter_params_x,
                       InterpFilterParams *filter_params_y,
                       const int subpel_x_q4, const int subpel_y_q4,
                       ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
330
  int x, y, k;
331
  uint8_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
Angie Chiang's avatar
Angie Chiang committed
332 333 334 335
  int im_h = h + filter_params_y->taps - 1;
  int im_stride = w;
  const int fo_vert = filter_params_y->taps / 2 - 1;
  const int fo_horiz = filter_params_x->taps / 2 - 1;
336

Angie Chiang's avatar
Angie Chiang committed
337 338 339 340 341 342
  // horizontal filter
  const uint8_t *src_horiz = src - fo_vert * src_stride;
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_x, subpel_x_q4 & SUBPEL_MASK);
  for (y = 0; y < im_h; ++y) {
    for (x = 0; x < w; ++x) {
343
      int32_t sum = 0;
Angie Chiang's avatar
Angie Chiang committed
344 345 346
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
      }
Angie Chiang's avatar
Angie Chiang committed
347
      im_block[y * im_stride + x] =
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
          clip_pixel(ROUND_POWER_OF_TWO(sum, conv_params->round_0));
    }
  }

  // vertical filter
  uint8_t *src_vert = im_block + fo_vert * im_stride;
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_y, subpel_y_q4 & SUBPEL_MASK);
  for (y = 0; y < h; ++y) {
    for (x = 0; x < w; ++x) {
      CONV_BUF_TYPE sum = 0;
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
      }
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
      dst[y * dst_stride + x] += res;
    }
  }
}
Angie Chiang's avatar
Angie Chiang committed
367
#else
368 369 370 371 372 373
/* When convolve-round is enabled and compound-round is disabled, we use a
   high-precision convolve filter.
   Note: For notes on hardware implementations, including the required
   bit widths for various intermediate values, see the comments above
   av1_warp_affine_c.
*/
David Barker's avatar
David Barker committed
374 375 376 377 378 379
void av1_convolve_2d_c(const uint8_t *src, int src_stride, CONV_BUF_TYPE *dst,
                       int dst_stride, int w, int h,
                       InterpFilterParams *filter_params_x,
                       InterpFilterParams *filter_params_y,
                       const int subpel_x_q4, const int subpel_y_q4,
                       ConvolveParams *conv_params) {
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  int x, y, k;
  int32_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
  int im_h = h + filter_params_y->taps - 1;
  int im_stride = w;
  const int fo_vert = filter_params_y->taps / 2 - 1;
  const int fo_horiz = filter_params_x->taps / 2 - 1;
  const int bd = 8;

  // horizontal filter
  const uint8_t *src_horiz = src - fo_vert * src_stride;
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_x, subpel_x_q4 & SUBPEL_MASK);
  for (y = 0; y < im_h; ++y) {
    for (x = 0; x < w; ++x) {
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
      }
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
Angie Chiang's avatar
Angie Chiang committed
399
      im_block[y * im_stride + x] =
400
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
Angie Chiang's avatar
Angie Chiang committed
401 402 403 404
    }
  }

  // vertical filter
405
  int32_t *src_vert = im_block + fo_vert * im_stride;
Angie Chiang's avatar
Angie Chiang committed
406 407
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_y, subpel_y_q4 & SUBPEL_MASK);
408
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
Angie Chiang's avatar
Angie Chiang committed
409 410
  for (y = 0; y < h; ++y) {
    for (x = 0; x < w; ++x) {
411
      CONV_BUF_TYPE sum = 1 << offset_bits;
Angie Chiang's avatar
Angie Chiang committed
412 413 414
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
      }
415 416 417 418 419
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
                          ((1 << (offset_bits - conv_params->round_1)) +
                           (1 << (offset_bits - conv_params->round_1 - 1)));
      dst[y * dst_stride + x] += res;
Angie Chiang's avatar
Angie Chiang committed
420 421 422
    }
  }
}
423
#endif
Angie Chiang's avatar
Angie Chiang committed
424

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
static INLINE void transpose_uint8(uint8_t *dst, int dst_stride,
                                   const uint8_t *src, int src_stride, int w,
                                   int h) {
  int r, c;
  for (r = 0; r < h; ++r)
    for (c = 0; c < w; ++c)
      dst[c * (dst_stride) + r] = src[r * (src_stride) + c];
}

static INLINE void transpose_int32(int32_t *dst, int dst_stride,
                                   const int32_t *src, int src_stride, int w,
                                   int h) {
  int r, c;
  for (r = 0; r < h; ++r)
    for (c = 0; c < w; ++c)
      dst[c * (dst_stride) + r] = src[r * (src_stride) + c];
}

Angie Chiang's avatar
Angie Chiang committed
443 444 445 446 447 448 449 450 451 452
void av1_convolve_2d_facade(const uint8_t *src, int src_stride, uint8_t *dst,
                            int dst_stride, int w, int h,
                            const InterpFilter *interp_filter,
                            const int subpel_x_q4, int x_step_q4,
                            const int subpel_y_q4, int y_step_q4,
                            ConvolveParams *conv_params) {
  (void)x_step_q4;
  (void)y_step_q4;
  (void)dst;
  (void)dst_stride;
453
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
454 455 456 457
  InterpFilterParams filter_params_x =
      av1_get_interp_filter_params(interp_filter[1 + 2 * conv_params->ref]);
  InterpFilterParams filter_params_y =
      av1_get_interp_filter_params(interp_filter[0 + 2 * conv_params->ref]);
458

459
#if USE_EXTRA_FILTER
460 461 462 463 464 465
  if (filter_params_x.interp_filter == MULTITAP_SHARP &&
      filter_params_y.interp_filter == MULTITAP_SHARP) {
    // Avoid two directions both using 12-tap filter.
    // This will reduce hardware implementation cost.
    filter_params_y = av1_get_interp_filter_params(EIGHTTAP_SHARP);
  }
466
#endif  // USE_EXTRA_FILTER
467 468 469 470 471
#else
  InterpFilterParams filter_params_x =
      av1_get_interp_filter_params(*interp_filter);
  InterpFilterParams filter_params_y =
      av1_get_interp_filter_params(*interp_filter);
472
#endif
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

  if (filter_params_y.taps < filter_params_x.taps) {
    uint8_t tr_src[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) *
                   (MAX_SB_SIZE + MAX_FILTER_TAP - 1)];
    int tr_src_stride = MAX_SB_SIZE + MAX_FILTER_TAP - 1;
    CONV_BUF_TYPE tr_dst[MAX_SB_SIZE * MAX_SB_SIZE];
    int tr_dst_stride = MAX_SB_SIZE;
    int fo_vert = filter_params_y.taps / 2 - 1;
    int fo_horiz = filter_params_x.taps / 2 - 1;

    transpose_uint8(tr_src, tr_src_stride,
                    src - fo_vert * src_stride - fo_horiz, src_stride,
                    w + filter_params_x.taps - 1, h + filter_params_y.taps - 1);
    transpose_int32(tr_dst, tr_dst_stride, conv_params->dst,
                    conv_params->dst_stride, w, h);

    // horizontal and vertical parameters are swapped because of the transpose
    av1_convolve_2d(tr_src + fo_horiz * tr_src_stride + fo_vert, tr_src_stride,
                    tr_dst, tr_dst_stride, h, w, &filter_params_y,
                    &filter_params_x, subpel_y_q4, subpel_x_q4, conv_params);
    transpose_int32(conv_params->dst, conv_params->dst_stride, tr_dst,
                    tr_dst_stride, h, w);
  } else {
    av1_convolve_2d(src, src_stride, conv_params->dst, conv_params->dst_stride,
                    w, h, &filter_params_x, &filter_params_y, subpel_x_q4,
                    subpel_y_q4, conv_params);
  }
Angie Chiang's avatar
Angie Chiang committed
500 501
}

502 503 504 505 506 507 508 509 510
#if CONFIG_HIGHBITDEPTH
static INLINE void transpose_uint16(uint16_t *dst, int dst_stride,
                                    const uint16_t *src, int src_stride, int w,
                                    int h) {
  int r, c;
  for (r = 0; r < h; ++r)
    for (c = 0; c < w; ++c) dst[c * dst_stride + r] = src[r * src_stride + c];
}

511 512 513
void av1_highbd_convolve_rounding_c(const int32_t *src, int src_stride,
                                    uint8_t *dst8, int dst_stride, int w, int h,
                                    int bits, int bd) {
514 515 516 517 518
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
  int r, c;
  for (r = 0; r < h; ++r) {
    for (c = 0; c < w; ++c) {
      dst[r * dst_stride + c] = clip_pixel_highbd(
519
          ROUND_POWER_OF_TWO(src[r * src_stride + c], bits), bd);
520 521 522 523
    }
  }
}

524
#if CONFIG_COMPOUND_ROUND
David Barker's avatar
David Barker committed
525 526 527 528 529 530
void av1_highbd_convolve_2d_c(const uint16_t *src, int src_stride,
                              CONV_BUF_TYPE *dst, int dst_stride, int w, int h,
                              InterpFilterParams *filter_params_x,
                              InterpFilterParams *filter_params_y,
                              const int subpel_x_q4, const int subpel_y_q4,
                              ConvolveParams *conv_params, int bd) {
531
  int x, y, k;
532
  uint16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
533 534 535 536
  int im_h = h + filter_params_y->taps - 1;
  int im_stride = w;
  const int fo_vert = filter_params_y->taps / 2 - 1;
  const int fo_horiz = filter_params_x->taps / 2 - 1;
537

538 539 540 541 542 543
  // horizontal filter
  const uint16_t *src_horiz = src - fo_vert * src_stride;
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_x, subpel_x_q4 & SUBPEL_MASK);
  for (y = 0; y < im_h; ++y) {
    for (x = 0; x < w; ++x) {
544
      int32_t sum = 0;
545 546 547
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
      }
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
      im_block[y * im_stride + x] =
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, conv_params->round_0), bd);
    }
  }

  // vertical filter
  uint16_t *src_vert = im_block + fo_vert * im_stride;
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_y, subpel_y_q4 & SUBPEL_MASK);
  for (y = 0; y < h; ++y) {
    for (x = 0; x < w; ++x) {
      CONV_BUF_TYPE sum = 0;
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
      }
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
      dst[y * dst_stride + x] += res;
    }
  }
}
568
#else
David Barker's avatar
David Barker committed
569 570 571 572 573 574
void av1_highbd_convolve_2d_c(const uint16_t *src, int src_stride,
                              CONV_BUF_TYPE *dst, int dst_stride, int w, int h,
                              InterpFilterParams *filter_params_x,
                              InterpFilterParams *filter_params_y,
                              const int subpel_x_q4, const int subpel_y_q4,
                              ConvolveParams *conv_params, int bd) {
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
  int x, y, k;
  int32_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
  int im_h = h + filter_params_y->taps - 1;
  int im_stride = w;
  const int fo_vert = filter_params_y->taps / 2 - 1;
  const int fo_horiz = filter_params_x->taps / 2 - 1;

  // horizontal filter
  const uint16_t *src_horiz = src - fo_vert * src_stride;
  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_x, subpel_x_q4 & SUBPEL_MASK);
  for (y = 0; y < im_h; ++y) {
    for (x = 0; x < w; ++x) {
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
      }
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
593 594
      (void)bd;
      im_block[y * im_stride + x] =
595
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
596 597 598 599
    }
  }

  // vertical filter
600 601
  int32_t *src_vert = im_block + fo_vert * im_stride;
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
602 603 604 605
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_y, subpel_y_q4 & SUBPEL_MASK);
  for (y = 0; y < h; ++y) {
    for (x = 0; x < w; ++x) {
606
      CONV_BUF_TYPE sum = 1 << offset_bits;
607 608 609
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
      }
610 611 612 613 614
      assert(0 <= sum && sum < (1 << (offset_bits + 2)));
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1) -
                          ((1 << (offset_bits - conv_params->round_1)) +
                           (1 << (offset_bits - conv_params->round_1 - 1)));
      dst[y * dst_stride + x] += res;
615 616 617
    }
  }
}
618
#endif
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

void av1_highbd_convolve_2d_facade(const uint8_t *src8, int src_stride,
                                   uint8_t *dst, int dst_stride, int w, int h,
                                   const InterpFilter *interp_filter,
                                   const int subpel_x_q4, int x_step_q4,
                                   const int subpel_y_q4, int y_step_q4,
                                   ConvolveParams *conv_params, int bd) {
  (void)x_step_q4;
  (void)y_step_q4;
  (void)dst;
  (void)dst_stride;
#if CONFIG_DUAL_FILTER
  InterpFilterParams filter_params_x =
      av1_get_interp_filter_params(interp_filter[1 + 2 * conv_params->ref]);
  InterpFilterParams filter_params_y =
      av1_get_interp_filter_params(interp_filter[0 + 2 * conv_params->ref]);

636
#if USE_EXTRA_FILTER
637 638 639 640 641 642
  if (filter_params_x.interp_filter == MULTITAP_SHARP &&
      filter_params_y.interp_filter == MULTITAP_SHARP) {
    // Avoid two directions both using 12-tap filter.
    // This will reduce hardware implementation cost.
    filter_params_y = av1_get_interp_filter_params(EIGHTTAP_SHARP);
  }
643
#endif
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
#else
  InterpFilterParams filter_params_x =
      av1_get_interp_filter_params(*interp_filter);
  InterpFilterParams filter_params_y =
      av1_get_interp_filter_params(*interp_filter);
#endif
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  if (filter_params_y.taps < filter_params_x.taps) {
    uint16_t tr_src[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) *
                    (MAX_SB_SIZE + MAX_FILTER_TAP - 1)];
    int tr_src_stride = MAX_SB_SIZE + MAX_FILTER_TAP - 1;
    CONV_BUF_TYPE tr_dst[MAX_SB_SIZE * MAX_SB_SIZE];
    int tr_dst_stride = MAX_SB_SIZE;
    int fo_vert = filter_params_y.taps / 2 - 1;
    int fo_horiz = filter_params_x.taps / 2 - 1;

    transpose_uint16(
        tr_src, tr_src_stride, src - fo_vert * src_stride - fo_horiz,
        src_stride, w + filter_params_x.taps - 1, h + filter_params_y.taps - 1);
    transpose_int32(tr_dst, tr_dst_stride, conv_params->dst,
                    conv_params->dst_stride, w, h);

    // horizontal and vertical parameters are swapped because of the transpose
    av1_highbd_convolve_2d(tr_src + fo_horiz * tr_src_stride + fo_vert,
                           tr_src_stride, tr_dst, tr_dst_stride, h, w,
                           &filter_params_y, &filter_params_x, subpel_y_q4,
                           subpel_x_q4, conv_params, bd);
    transpose_int32(conv_params->dst, conv_params->dst_stride, tr_dst,
                    tr_dst_stride, h, w);
  } else {
    av1_highbd_convolve_2d(src, src_stride, conv_params->dst,
                           conv_params->dst_stride, w, h, &filter_params_x,
                           &filter_params_y, subpel_x_q4, subpel_y_q4,
                           conv_params, bd);
  }
}
#endif  // CONFIG_HIGHBITDEPTH

Angie Chiang's avatar
Angie Chiang committed
682
#endif  // CONFIG_CONVOLVE_ROUND
Angie Chiang's avatar
Angie Chiang committed
683

684 685 686 687 688 689 690 691
typedef void (*ConvolveFunc)(const uint8_t *src, int src_stride, uint8_t *dst,
                             int dst_stride, int w, int h,
                             const InterpFilterParams filter_params,
                             const int subpel_q4, int step_q4,
                             ConvolveParams *conv_params);

static void convolve_helper(const uint8_t *src, int src_stride, uint8_t *dst,
                            int dst_stride, int w, int h,
692
#if CONFIG_DUAL_FILTER
693
                            const InterpFilter *interp_filter,
694
#else
695
                            const InterpFilter interp_filter,
696
#endif
697 698 699 700 701
                            const int subpel_x_q4, int x_step_q4,
                            const int subpel_y_q4, int y_step_q4,
                            ConvolveParams *conv_params,
                            ConvolveFunc convolve_horiz,
                            ConvolveFunc convolve_vert) {
Fergus Simpson's avatar
Fergus Simpson committed
702 703
  int ignore_horiz = x_step_q4 == SUBPEL_SHIFTS && subpel_x_q4 == 0;
  int ignore_vert = y_step_q4 == SUBPEL_SHIFTS && subpel_y_q4 == 0;
Angie Chiang's avatar
Angie Chiang committed
704 705 706 707 708 709 710 711 712 713
#if CONFIG_DUAL_FILTER
  InterpFilterParams filter_params_x =
      av1_get_interp_filter_params(interp_filter[1 + 2 * conv_params->ref]);
  InterpFilterParams filter_params_y =
      av1_get_interp_filter_params(interp_filter[0 + 2 * conv_params->ref]);
  InterpFilterParams filter_params;
#else
  InterpFilterParams filter_params =
      av1_get_interp_filter_params(interp_filter);
#endif
Angie Chiang's avatar
Angie Chiang committed
714
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
715 716 717 718 719 720

  assert(w <= MAX_BLOCK_WIDTH);
  assert(h <= MAX_BLOCK_HEIGHT);
  assert(y_step_q4 <= MAX_STEP);
  assert(x_step_q4 <= MAX_STEP);

Angie Chiang's avatar
Angie Chiang committed
721
  if (ignore_horiz && ignore_vert) {
722
    convolve_copy(src, src_stride, dst, dst_stride, w, h, conv_params);
Angie Chiang's avatar
Angie Chiang committed
723
  } else if (ignore_vert) {
724
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
725
    filter_params = filter_params_x;
726 727
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
728 729
    convolve_horiz(src, src_stride, dst, dst_stride, w, h, filter_params,
                   subpel_x_q4, x_step_q4, conv_params);
Angie Chiang's avatar
Angie Chiang committed
730
  } else if (ignore_horiz) {
731
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
732
    filter_params = filter_params_y;
733 734
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
735 736
    convolve_vert(src, src_stride, dst, dst_stride, w, h, filter_params,
                  subpel_y_q4, y_step_q4, conv_params);
Angie Chiang's avatar
Angie Chiang committed
737
  } else {
738 739 740 741 742 743
    // temp's size is set to a 256 aligned value to facilitate SIMD
    // implementation. The value is greater than (maximum possible intermediate
    // height or width) * MAX_SB_SIZE
    DECLARE_ALIGNED(16, uint8_t,
                    temp[((MAX_SB_SIZE * 2 + 16) + 16) * MAX_SB_SIZE]);
    int max_intermediate_size = ((MAX_SB_SIZE * 2 + 16) + 16);
744
    int filter_size;
745
#if CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
746 747
    if (interp_filter[0 + 2 * conv_params->ref] == MULTITAP_SHARP &&
        interp_filter[1 + 2 * conv_params->ref] == MULTITAP_SHARP) {
748 749 750 751
      // Avoid two directions both using 12-tap filter.
      // This will reduce hardware implementation cost.
      filter_params_y = av1_get_interp_filter_params(EIGHTTAP_SHARP);
    }
752

753 754 755 756
    // we do filter with fewer taps first to reduce hardware implementation
    // complexity
    if (filter_params_y.taps < filter_params_x.taps) {
      int intermediate_width;
757
      int temp_stride = max_intermediate_size;
758 759
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
760
      temp_conv_params.do_average = 0;
761
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
762 763 764 765
      filter_params = filter_params_y;
      filter_size = filter_params_x.taps;
      intermediate_width =
          (((w - 1) * x_step_q4 + subpel_x_q4) >> SUBPEL_BITS) + filter_size;
766
      assert(intermediate_width <= max_intermediate_size);
Angie Chiang's avatar
Angie Chiang committed
767

768
      assert(filter_params.taps <= MAX_FILTER_TAP);
769

770 771 772
      convolve_vert(src - (filter_size / 2 - 1), src_stride, temp, temp_stride,
                    intermediate_width, h, filter_params, subpel_y_q4,
                    y_step_q4, &temp_conv_params);
773

774 775
      filter_params = filter_params_x;
      assert(filter_params.taps <= MAX_FILTER_TAP);
776 777
      convolve_horiz(temp + (filter_size / 2 - 1), temp_stride, dst, dst_stride,
                     w, h, filter_params, subpel_x_q4, x_step_q4, conv_params);
778
    } else
779
#endif  // CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
780 781
    {
      int intermediate_height;
782
      int temp_stride = MAX_SB_SIZE;
783 784
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
785
      temp_conv_params.do_average = 0;
786
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
787 788 789 790 791 792 793 794
#if CONFIG_DUAL_FILTER
      filter_params = filter_params_x;
      filter_size = filter_params_y.taps;
#else
      filter_size = filter_params.taps;
#endif
      intermediate_height =
          (((h - 1) * y_step_q4 + subpel_y_q4) >> SUBPEL_BITS) + filter_size;
795 796
      assert(intermediate_height <= max_intermediate_size);
      (void)max_intermediate_size;
797 798 799

      assert(filter_params.taps <= MAX_FILTER_TAP);

800 801 802
      convolve_horiz(src - src_stride * (filter_size / 2 - 1), src_stride, temp,
                     temp_stride, w, intermediate_height, filter_params,
                     subpel_x_q4, x_step_q4, &temp_conv_params);
803 804 805 806 807 808

#if CONFIG_DUAL_FILTER
      filter_params = filter_params_y;
#endif
      assert(filter_params.taps <= MAX_FILTER_TAP);

809 810 811
      convolve_vert(temp + temp_stride * (filter_size / 2 - 1), temp_stride,
                    dst, dst_stride, w, h, filter_params, subpel_y_q4,
                    y_step_q4, conv_params);
812
    }
Angie Chiang's avatar
Angie Chiang committed
813
  }
814 815
}

Fergus Simpson's avatar
Fergus Simpson committed
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
static void convolve_scale_helper(const uint8_t *src, int src_stride,
                                  uint8_t *dst, int dst_stride, int w, int h,
#if CONFIG_DUAL_FILTER
                                  const InterpFilter *interp_filter,
#else
                                  const InterpFilter interp_filter,
#endif
                                  const int subpel_x_qn, int x_step_qn,
                                  const int subpel_y_qn, int y_step_qn,
                                  ConvolveParams *conv_params,
                                  ConvolveFunc convolve_horiz,
                                  ConvolveFunc convolve_vert) {
  int ignore_horiz = x_step_qn == SCALE_SUBPEL_SHIFTS && subpel_x_qn == 0;
  int ignore_vert = y_step_qn == SCALE_SUBPEL_SHIFTS && subpel_y_qn == 0;
#if CONFIG_DUAL_FILTER
  InterpFilterParams filter_params_x =
      av1_get_interp_filter_params(interp_filter[1 + 2 * conv_params->ref]);
  InterpFilterParams filter_params_y =
      av1_get_interp_filter_params(interp_filter[0 + 2 * conv_params->ref]);
  InterpFilterParams filter_params;
#else
  InterpFilterParams filter_params =
      av1_get_interp_filter_params(interp_filter);
#endif
  assert(conv_params->round == CONVOLVE_OPT_ROUND);

  assert(w <= MAX_BLOCK_WIDTH);
  assert(h <= MAX_BLOCK_HEIGHT);
  assert(y_step_qn <= (MAX_STEP << SCALE_EXTRA_BITS));
  assert(x_step_qn <= (MAX_STEP << SCALE_EXTRA_BITS));

  if (ignore_horiz && ignore_vert) {
    convolve_copy(src, src_stride, dst, dst_stride, w, h, conv_params);
  } else if (ignore_vert) {
#if CONFIG_DUAL_FILTER
    filter_params = filter_params_x;
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
    convolve_horiz(src, src_stride, dst, dst_stride, w, h, filter_params,
                   subpel_x_qn, x_step_qn, conv_params);
  } else if (ignore_horiz) {
#if CONFIG_DUAL_FILTER
    filter_params = filter_params_y;
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
    convolve_vert(src, src_stride, dst, dst_stride, w, h, filter_params,
                  subpel_y_qn, y_step_qn, conv_params);
  } else {
    // temp's size is set to a 256 aligned value to facilitate SIMD
    // implementation. The value is greater than (maximum possible intermediate
    // height or width) * MAX_SB_SIZE
    DECLARE_ALIGNED(16, uint8_t,
                    temp[((MAX_SB_SIZE * 2 + 16) + 16) * MAX_SB_SIZE]);
    int max_intermediate_size = ((MAX_SB_SIZE * 2 + 16) + 16);
    int filter_size;
#if CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
    if (interp_filter[0 + 2 * conv_params->ref] == MULTITAP_SHARP &&
        interp_filter[1 + 2 * conv_params->ref] == MULTITAP_SHARP) {
      // Avoid two directions both using 12-tap filter.
      // This will reduce hardware implementation cost.
      filter_params_y = av1_get_interp_filter_params(EIGHTTAP_SHARP);
    }

    // we do filter with fewer taps first to reduce hardware implementation
    // complexity
    if (filter_params_y.taps < filter_params_x.taps) {
      int intermediate_width;
      int temp_stride = max_intermediate_size;
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
      temp_conv_params.do_average = 0;
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
      filter_params = filter_params_y;
      filter_size = filter_params_x.taps;
      intermediate_width =
          (((w - 1) * x_step_qn + subpel_x_qn) >> SCALE_SUBPEL_BITS) +
          filter_size;
      assert(intermediate_width <= max_intermediate_size);

      assert(filter_params.taps <= MAX_FILTER_TAP);

      convolve_vert(src - (filter_size / 2 - 1), src_stride, temp, temp_stride,
                    intermediate_width, h, filter_params, subpel_y_qn,
                    y_step_qn, &temp_conv_params);

      filter_params = filter_params_x;
      assert(filter_params.taps <= MAX_FILTER_TAP);
      convolve_horiz(temp + (filter_size / 2 - 1), temp_stride, dst, dst_stride,
                     w, h, filter_params, subpel_x_qn, x_step_qn, conv_params);
    } else {
#endif  // CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
      int intermediate_height;
      int temp_stride = MAX_SB_SIZE;
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
      temp_conv_params.do_average = 0;
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
#if CONFIG_DUAL_FILTER
      filter_params = filter_params_x;
      filter_size = filter_params_y.taps;
#else
    filter_size = filter_params.taps;
#endif
      intermediate_height =
          (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
          filter_size;
      assert(intermediate_height <= max_intermediate_size);
      (void)max_intermediate_size;

      assert(filter_params.taps <= MAX_FILTER_TAP);

      convolve_horiz(src - src_stride * (filter_size / 2 - 1), src_stride, temp,
                     temp_stride, w, intermediate_height, filter_params,
                     subpel_x_qn, x_step_qn, &temp_conv_params);

#if CONFIG_DUAL_FILTER
      filter_params = filter_params_y;
#endif
      assert(filter_params.taps <= MAX_FILTER_TAP);

      convolve_vert(temp + temp_stride * (filter_size / 2 - 1), temp_stride,
                    dst, dst_stride, w, h, filter_params, subpel_y_qn,
                    y_step_qn, conv_params);
#if CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
    }
#endif  // CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
  }
}

945 946 947 948 949 950 951 952 953 954 955 956 957 958
void av1_convolve(const uint8_t *src, int src_stride, uint8_t *dst,
                  int dst_stride, int w, int h,
#if CONFIG_DUAL_FILTER
                  const InterpFilter *interp_filter,
#else
                  const InterpFilter interp_filter,
#endif
                  const int subpel_x_q4, int x_step_q4, const int subpel_y_q4,
                  int y_step_q4, ConvolveParams *conv_params) {
  convolve_helper(src, src_stride, dst, dst_stride, w, h, interp_filter,
                  subpel_x_q4, x_step_q4, subpel_y_q4, y_step_q4, conv_params,
                  av1_convolve_horiz_facade, av1_convolve_vert_facade);
}

959 960 961 962 963 964 965 966 967
void av1_convolve_c(const uint8_t *src, int src_stride, uint8_t *dst,
                    int dst_stride, int w, int h,
#if CONFIG_DUAL_FILTER
                    const InterpFilter *interp_filter,
#else
                    const InterpFilter interp_filter,
#endif
                    const int subpel_x_q4, int x_step_q4, const int subpel_y_q4,
                    int y_step_q4, ConvolveParams *conv_params) {
968 969 970
  convolve_helper(src, src_stride, dst, dst_stride, w, h, interp_filter,
                  subpel_x_q4, x_step_q4, subpel_y_q4, y_step_q4, conv_params,
                  av1_convolve_horiz_facade_c, av1_convolve_vert_facade_c);
971 972
}

Fergus Simpson's avatar
Fergus Simpson committed
973 974 975 976 977 978 979
void av1_convolve_scale(const uint8_t *src, int src_stride, uint8_t *dst,
                        int dst_stride, int w, int h,
#if CONFIG_DUAL_FILTER
                        const InterpFilter *interp_filter,
#else
                        const InterpFilter interp_filter,
#endif
980 981
                        const int subpel_x_qn, int x_step_qn,
                        const int subpel_y_qn, int y_step_qn,
Fergus Simpson's avatar
Fergus Simpson committed
982 983
                        ConvolveParams *conv_params) {
  convolve_scale_helper(src, src_stride, dst, dst_stride, w, h, interp_filter,
984
                        subpel_x_qn, x_step_qn, subpel_y_qn, y_step_qn,
Fergus Simpson's avatar
Fergus Simpson committed
985 986 987 988
                        conv_params, av1_convolve_horiz_facade_scale,
                        av1_convolve_vert_facade_scale);
}

Angie Chiang's avatar
Angie Chiang committed
989
void av1_lowbd_convolve_init_c(void) {
Angie Chiang's avatar
Angie Chiang committed
990 991 992 993
  // A placeholder for SIMD initialization
  return;
}

Angie Chiang's avatar
Angie Chiang committed
994 995 996 997 998 999
void av1_highbd_convolve_init_c(void) {
  // A placeholder for SIMD initialization
  return;
}

void av1_convolve_init(AV1_COMMON *cm) {
1000
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
  if (cm->use_highbitdepth)
    av1_highbd_convolve_init();
  else
    av1_lowbd_convolve_init();
#else
  (void)cm;
  av1_lowbd_convolve_init();
#endif
  return;
}

1012
#if CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
1013 1014 1015 1016 1017
void av1_highbd_convolve_horiz_c(const uint16_t *src, int src_stride,
                                 uint16_t *dst, int dst_stride, int w, int h,
                                 const InterpFilterParams filter_params,
                                 const int subpel_x_q4, int x_step_q4, int avg,
                                 int bd) {
1018
  int x, y;
1019
  int filter_size = filter_params.taps;
1020 1021 1022 1023 1024
  src -= filter_size / 2 - 1;
  for (y = 0; y < h; ++y) {
    int x_q4 = subpel_x_q4;
    for (x = 0; x < w; ++x) {
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
Yaowu Xu's avatar
Yaowu Xu committed
1025
      const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
1026
          filter_params, x_q4 & SUBPEL_MASK);
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k) sum += src_x[k] * x_filter[k];
      if (avg)
        dst[x] = ROUND_POWER_OF_TWO(
            dst[x] +
                clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd),
            1);
      else
        dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
      x_q4 += x_step_q4;
    }
    src += src_stride;
    dst += dst_stride;
  }
}

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
void av1_highbd_convolve_horiz_scale(const uint16_t *src, int src_stride,
                                     uint16_t *dst, int dst_stride, int w,
                                     int h,
                                     const InterpFilterParams filter_params,
                                     const int subpel_x_qn, int x_step_qn,
                                     int avg, int bd) {
  int x, y;
  int filter_size = filter_params.taps;
  src -= filter_size / 2 - 1;
  for (y = 0; y < h; ++y) {
    int x_qn = subpel_x_qn;
    for (x = 0; x < w; ++x) {
      const uint16_t *const src_x = &src[x_qn >> SCALE_SUBPEL_BITS];
      const int x_filter_idx = (x_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
      assert(x_filter_idx < SUBPEL_SHIFTS);
      const int16_t *x_filter =
          av1_get_interp_filter_subpel_kernel(filter_params, x_filter_idx);
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k) sum += src_x[k] * x_filter[k];
      if (avg)
        dst[x] = ROUND_POWER_OF_TWO(
            dst[x] +
                clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd),
            1);
      else
        dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
      x_qn += x_step_qn;
    }
    src += src_stride;
    dst += dst_stride;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
1076 1077 1078 1079 1080
void av1_highbd_convolve_vert_c(const uint16_t *src, int src_stride,
                                uint16_t *dst, int dst_stride, int w, int h,
                                const InterpFilterParams filter_params,
                                const int subpel_y_q4, int y_step_q4, int avg,
                                int bd) {
1081
  int x, y;
1082
  int filter_size = filter_params.taps;
1083 1084 1085 1086 1087 1088
  src -= src_stride * (filter_size / 2 - 1);

  for (x = 0; x < w; ++x) {
    int y_q4 = subpel_y_q4;
    for (y = 0; y < h; ++y) {
      const uint16_t *const src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
Yaowu Xu's avatar
Yaowu Xu committed
1089
      const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
1090
          filter_params, y_q4 & SUBPEL_MASK);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k)
        sum += src_y[k * src_stride] * y_filter[k];
      if (avg) {
        dst[y * dst_stride] = ROUND_POWER_OF_TWO(
            dst[y * dst_stride] +
                clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd),
            1);
      } else {
        dst[y * dst_stride] =
            clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
      }
      y_q4 += y_step_q4;
    }
    ++src;
    ++dst;
  }
}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
void av1_highbd_convolve_vert_scale(const uint16_t *src, int src_stride,
                                    uint16_t *dst, int dst_stride, int w, int h,
                                    const InterpFilterParams filter_params,
                                    const int subpel_y_qn, int y_step_qn,
                                    int avg, int bd) {
  int x, y;
  int filter_size = filter_params.taps;
  src -= src_stride * (filter_size / 2 - 1);

  for (x = 0; x < w; ++x) {
    int y_qn = subpel_y_qn;
    for (y = 0; y < h; ++y) {
      const uint16_t *const src_y =
          &src[(y_qn >> SCALE_SUBPEL_BITS) * src_stride];
      const int y_filter_idx = (y_qn & SCALE_SUBPEL_MASK) >> SCALE_EXTRA_BITS;
      assert(y_filter_idx < SUBPEL_SHIFTS);
      const int16_t *y_filter =
          av1_get_interp_filter_subpel_kernel(filter_params, y_filter_idx);
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k)
        sum += src_y[k * src_stride] * y_filter[k];
      if (avg) {
        dst[y * dst_stride] = ROUND_POWER_OF_TWO(
            dst[y * dst_stride] +
                clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd),
            1);
      } else {
        dst[y * dst_stride] =
            clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
      }
      y_qn += y_step_qn;
    }
    ++src;
    ++dst;
  }
}

Angie Chiang's avatar
Angie Chiang committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
static void highbd_convolve_copy(const uint16_t *src, int src_stride,
                                 uint16_t *dst, int dst_stride, int w, int h,
                                 int avg, int bd) {
  if (avg == 0) {
    int r;
    for (r = 0; r < h; ++r) {
      memcpy(dst, src, w * sizeof(*src));
      src += src_stride;
      dst += dst_stride;
    }
  } else {
    int r, c;
    for (r = 0; r < h; ++r) {
      for (c = 0; c < w; ++c) {
        dst[c] = clip_pixel_highbd(ROUND_POWER_OF_TWO(dst[c] + src[c], 1), bd);
      }
      src += src_stride;
      dst += dst_stride;
    }
  }
}

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
void av1_highbd_convolve_horiz_facade(const uint8_t *src8, int src_stride,
                                      uint8_t *dst8, int dst_stride, int w,
                                      int h,
                                      const InterpFilterParams filter_params,
                                      const int subpel_x_q4, int x_step_q4,
                                      int avg, int bd) {
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
  if (filter_params.taps == SUBPEL_TAPS) {
    const int16_t *filter_x =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_x_q4);
    if (avg == 0)
      aom_highbd_convolve8_horiz(src8, src_stride, dst8, dst_stride, filter_x,
                                 x_step_q4, NULL, -1, w, h, bd);
    else
      aom_highbd_convolve8_avg_horiz(src8, src_stride, dst8, dst_stride,
                                     filter_x, x_step_q4, NULL, -1, w, h, bd);
  } else {
    av1_highbd_convolve_horiz(src, src_stride, dst, dst_stride, w, h,
                              filter_params, subpel_x_q4, x_step_q4, avg, bd);
  }
}

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
void av1_highbd_convolve_horiz_facade_scale(
    const uint8_t *src8, int src_stride, uint8_t *dst8, int dst_stride, int w,
    int h, const InterpFilterParams filter_params, const int subpel_x_qn,
    int x_step_qn, int avg, int bd) {
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
  // TODO(debargha): Add special functions for filter_params.taps == SUBPEL_TAPS
  // as in the function above.
  av1_highbd_convolve_horiz_scale(src, src_stride, dst, dst_stride, w, h,
                                  filter_params, subpel_x_qn, x_step_qn, avg,
                                  bd);
}

1205 1206 1207 1208 1209 1210 1211 1212
void av1_highbd_convolve_vert_facade(const uint8_t *src8, int src_stride,
                                     uint8_t *dst8, int dst_stride, int w,
                                     int h,
                                     const InterpFilterParams filter_params,
                                     const int subpel_y_q4, int y_step_q4,
                                     int avg, int bd) {
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
1213

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
  if (filter_params.taps == SUBPEL_TAPS) {
    const int16_t *filter_y =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_y_q4);
    if (avg == 0) {
      aom_highbd_convolve8_vert(src8, src_stride, dst8, dst_stride, NULL, -1,
                                filter_y, y_step_q4, w, h, bd);
    } else {
      aom_highbd_convolve8_avg_vert(src8, src_stride, dst8, dst_stride, NULL,
                                    -1, filter_y, y_step_q4, w, h, bd);
    }
  } else {
    av1_highbd_convolve_vert(src, src_stride, dst, dst_stride, w, h,
                             filter_params, subpel_y_q4, y_step_q4, avg, bd);
  }
}

1230 1231