convolve.c 39.4 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;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
58 59 60
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,
61 62
                         const int subpel_y_q4, int y_step_q4,
                         ConvolveParams *conv_params) {
63
  int x, y;
64
  int filter_size = filter_params.taps;
Angie Chiang's avatar
Angie Chiang committed
65
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
66 67 68 69 70
  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
71
      const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
72
          filter_params, y_q4 & SUBPEL_MASK);
73 74 75
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k)
        sum += src_y[k * src_stride] * y_filter[k];
76

Angie Chiang's avatar
Angie Chiang committed
77
      sum = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
78
      if (conv_params->do_average)
Angie Chiang's avatar
Angie Chiang committed
79 80 81
        dst[y * dst_stride] = ROUND_POWER_OF_TWO(dst[y * dst_stride] + sum, 1);
      else
        dst[y * dst_stride] = sum;
82

83 84 85 86 87 88 89
      y_q4 += y_step_q4;
    }
    ++src;
    ++dst;
  }
}

Angie Chiang's avatar
Angie Chiang committed
90
static void convolve_copy(const uint8_t *src, int src_stride, uint8_t *dst,
91 92
                          int dst_stride, int w, int h,
                          ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
93
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
94
  if (conv_params->do_average == 0) {
Angie Chiang's avatar
Angie Chiang committed
95
    int r;
Angie Chiang's avatar
Angie Chiang committed
96
    for (r = 0; r < h; ++r) {
Angie Chiang's avatar
Angie Chiang committed
97
      memcpy(dst, src, w);
Angie Chiang's avatar
Angie Chiang committed
98 99 100 101 102 103
      src += src_stride;
      dst += dst_stride;
    }
  } else {
    int r, c;
    for (r = 0; r < h; ++r) {
104
      for (c = 0; c < w; ++c) {
Angie Chiang's avatar
Angie Chiang committed
105
        dst[c] = clip_pixel(ROUND_POWER_OF_TWO(dst[c] + src[c], 1));
106
      }
Angie Chiang's avatar
Angie Chiang committed
107 108 109 110 111 112
      src += src_stride;
      dst += dst_stride;
    }
  }
}

113 114 115
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,
116 117
                               const int subpel_x_q4, int x_step_q4,
                               ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
118 119
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
120 121
    const int16_t *filter_x =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_x_q4);
122
    if (conv_params->do_average == 0)
123 124 125 126 127 128
      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
129 130
    av1_convolve_horiz(src, src_stride, dst, dst_stride, w, h, filter_params,
                       subpel_x_q4, x_step_q4, conv_params);
131 132 133
  }
}

134 135 136 137 138 139 140 141 142
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);
143
    if (conv_params->do_average == 0)
144 145 146 147 148 149 150 151 152 153 154
      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);
  }
}

155 156 157
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,
158 159
                              const int subpel_y_q4, int y_step_q4,
                              ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
160 161
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
162 163
    const int16_t *filter_y =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_y_q4);
164
    if (conv_params->do_average == 0) {
165 166 167 168 169 170 171
      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
172 173
    av1_convolve_vert(src, src_stride, dst, dst_stride, w, h, filter_params,
                      subpel_y_q4, y_step_q4, conv_params);
174 175 176
  }
}

177 178 179 180 181 182 183 184 185
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);
186
    if (conv_params->do_average == 0) {
187 188 189 190 191 192 193 194 195 196 197 198
      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);
  }
}

Angie Chiang's avatar
Angie Chiang committed
199
#if CONFIG_CONVOLVE_ROUND
Angie Chiang's avatar
Angie Chiang committed
200
void av1_convolve_rounding(const int32_t *src, int src_stride, uint8_t *dst,
Angie Chiang's avatar
Angie Chiang committed
201
                           int dst_stride, int w, int h, int bits) {
Angie Chiang's avatar
Angie Chiang committed
202 203 204 205
  int r, c;
  for (r = 0; r < h; ++r) {
    for (c = 0; c < w; ++c) {
      dst[r * dst_stride + c] =
206
          clip_pixel(ROUND_POWER_OF_TWO(src[r * src_stride + c], bits));
Angie Chiang's avatar
Angie Chiang committed
207 208 209
    }
  }
}
Angie Chiang's avatar
Angie Chiang committed
210

211
#if CONFIG_COMPOUND_ROUND
Angie Chiang's avatar
Angie Chiang committed
212 213 214 215 216 217
void av1_convolve_2d(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) {
  int x, y, k;
218
  uint8_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
Angie Chiang's avatar
Angie Chiang committed
219 220 221 222
  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;
223

Angie Chiang's avatar
Angie Chiang committed
224 225 226 227 228 229
  // 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) {
230
      int32_t sum = 0;
Angie Chiang's avatar
Angie Chiang committed
231 232 233
      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
234
      im_block[y * im_stride + x] =
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
          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
254
#else
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/* 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.
*/
void av1_convolve_2d(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) {
  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
285
      im_block[y * im_stride + x] =
286
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
Angie Chiang's avatar
Angie Chiang committed
287 288 289 290
    }
  }

  // vertical filter
291
  int32_t *src_vert = im_block + fo_vert * im_stride;
Angie Chiang's avatar
Angie Chiang committed
292 293
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_y, subpel_y_q4 & SUBPEL_MASK);
294
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
Angie Chiang's avatar
Angie Chiang committed
295 296
  for (y = 0; y < h; ++y) {
    for (x = 0; x < w; ++x) {
297
      CONV_BUF_TYPE sum = 1 << offset_bits;
Angie Chiang's avatar
Angie Chiang committed
298 299 300
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
      }
301 302 303 304 305
      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
306 307 308
    }
  }
}
309
#endif
Angie Chiang's avatar
Angie Chiang committed
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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
329 330 331 332 333 334 335 336 337 338
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;
339
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
340 341 342 343
  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]);
344

345
#if USE_EXTRA_FILTER
346 347 348 349 350 351
  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);
  }
352
#endif  // USE_EXTRA_FILTER
353 354 355 356 357
#else
  InterpFilterParams filter_params_x =
      av1_get_interp_filter_params(*interp_filter);
  InterpFilterParams filter_params_y =
      av1_get_interp_filter_params(*interp_filter);
358
#endif
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

  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
386 387
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
#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];
}

void av1_highbd_convolve_rounding(const int32_t *src, int src_stride,
                                  uint8_t *dst8, int dst_stride, int w, int h,
                                  int bits, int bd) {
  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(
405
          ROUND_POWER_OF_TWO(src[r * src_stride + c], bits), bd);
406 407 408 409
    }
  }
}

410
#if CONFIG_COMPOUND_ROUND
411 412 413 414 415 416 417
void av1_highbd_convolve_2d(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) {
  int x, y, k;
418
  uint16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
419 420 421 422
  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;
423

424 425 426 427 428 429
  // 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) {
430
      int32_t sum = 0;
431 432 433
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
      }
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
      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;
    }
  }
}
454
#else
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
void av1_highbd_convolve_2d(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) {
  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)));
479 480
      (void)bd;
      im_block[y * im_stride + x] =
481
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
482 483 484 485
    }
  }

  // vertical filter
486 487
  int32_t *src_vert = im_block + fo_vert * im_stride;
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
488 489 490 491
  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) {
492
      CONV_BUF_TYPE sum = 1 << offset_bits;
493 494 495
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
      }
496 497 498 499 500
      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;
501 502 503
    }
  }
}
504
#endif
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

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]);

522
#if USE_EXTRA_FILTER
523 524 525 526 527 528
  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);
  }
529
#endif
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
#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
568
#endif  // CONFIG_CONVOLVE_ROUND
Angie Chiang's avatar
Angie Chiang committed
569

570 571 572 573 574 575 576 577
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,
578
#if CONFIG_DUAL_FILTER
579
                            const InterpFilter *interp_filter,
580
#else
581
                            const InterpFilter interp_filter,
582
#endif
583 584 585 586 587
                            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) {
Angie Chiang's avatar
Angie Chiang committed
588 589
  int ignore_horiz = x_step_q4 == 16 && subpel_x_q4 == 0;
  int ignore_vert = y_step_q4 == 16 && subpel_y_q4 == 0;
Angie Chiang's avatar
Angie Chiang committed
590 591 592 593 594 595 596 597 598 599
#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
600
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
601 602 603 604 605 606

  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
607
  if (ignore_horiz && ignore_vert) {
608
    convolve_copy(src, src_stride, dst, dst_stride, w, h, conv_params);
Angie Chiang's avatar
Angie Chiang committed
609
  } else if (ignore_vert) {
610
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
611
    filter_params = filter_params_x;
612 613
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
614 615
    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
616
  } else if (ignore_horiz) {
617
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
618
    filter_params = filter_params_y;
619 620
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
621 622
    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
623
  } else {
624 625 626 627 628 629
    // 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);
630
    int filter_size;
631
#if CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
632 633
    if (interp_filter[0 + 2 * conv_params->ref] == MULTITAP_SHARP &&
        interp_filter[1 + 2 * conv_params->ref] == MULTITAP_SHARP) {
634 635 636 637
      // Avoid two directions both using 12-tap filter.
      // This will reduce hardware implementation cost.
      filter_params_y = av1_get_interp_filter_params(EIGHTTAP_SHARP);
    }
638

639 640 641 642
    // we do filter with fewer taps first to reduce hardware implementation
    // complexity
    if (filter_params_y.taps < filter_params_x.taps) {
      int intermediate_width;
643
      int temp_stride = max_intermediate_size;
644 645
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
646
      temp_conv_params.do_average = 0;
647
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
648 649 650 651
      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;
652
      assert(intermediate_width <= max_intermediate_size);
Angie Chiang's avatar
Angie Chiang committed
653

654
      assert(filter_params.taps <= MAX_FILTER_TAP);
655

656 657 658
      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);
659

660 661
      filter_params = filter_params_x;
      assert(filter_params.taps <= MAX_FILTER_TAP);
662 663
      convolve_horiz(temp + (filter_size / 2 - 1), temp_stride, dst, dst_stride,
                     w, h, filter_params, subpel_x_q4, x_step_q4, conv_params);
664
    } else
665
#endif  // CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
666 667
    {
      int intermediate_height;
668
      int temp_stride = MAX_SB_SIZE;
669 670
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
671
      temp_conv_params.do_average = 0;
672
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
673 674 675 676 677 678 679 680
#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;
681 682
      assert(intermediate_height <= max_intermediate_size);
      (void)max_intermediate_size;
683 684 685

      assert(filter_params.taps <= MAX_FILTER_TAP);

686 687 688
      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);
689 690 691 692 693 694

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

695 696 697
      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);
698
    }
Angie Chiang's avatar
Angie Chiang committed
699
  }
700 701
}

702 703 704 705 706 707 708 709 710 711 712 713 714 715
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);
}

716 717 718 719 720 721 722 723 724
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) {
725 726 727
  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);
728 729
}

Angie Chiang's avatar
Angie Chiang committed
730
void av1_lowbd_convolve_init_c(void) {
Angie Chiang's avatar
Angie Chiang committed
731 732 733 734
  // A placeholder for SIMD initialization
  return;
}

Angie Chiang's avatar
Angie Chiang committed
735 736 737 738 739 740
void av1_highbd_convolve_init_c(void) {
  // A placeholder for SIMD initialization
  return;
}

void av1_convolve_init(AV1_COMMON *cm) {
741
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
742 743 744 745 746 747 748 749 750 751 752
  if (cm->use_highbitdepth)
    av1_highbd_convolve_init();
  else
    av1_lowbd_convolve_init();
#else
  (void)cm;
  av1_lowbd_convolve_init();
#endif
  return;
}

753
#if CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
754 755 756 757 758
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) {
759
  int x, y;
760
  int filter_size = filter_params.taps;
761 762 763 764 765
  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
766
      const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
767
          filter_params, x_q4 & SUBPEL_MASK);
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
      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;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
784 785 786 787 788
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) {
789
  int x, y;
790
  int filter_size = filter_params.taps;
791 792 793 794 795 796
  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
797
      const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
798
          filter_params, y_q4 & SUBPEL_MASK);
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
      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;
  }
}

Angie Chiang's avatar
Angie Chiang committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
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;
    }
  }
}

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
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);
  }
}

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);
871

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
  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);
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
888 889
void av1_highbd_convolve(const uint8_t *src8, int src_stride, uint8_t *dst8,
                         int dst_stride, int w, int h,
890
#if CONFIG_DUAL_FILTER
James Zern's avatar
James Zern committed
891
                         const InterpFilter *interp_filter,
892
#else
James Zern's avatar
James Zern committed
893
                         const InterpFilter interp_filter,
894
#endif
Yaowu Xu's avatar
Yaowu Xu committed
895 896 897
                         const int subpel_x_q4, int x_step_q4,
                         const int subpel_y_q4, int y_step_q4, int ref_idx,
                         int bd) {
Angie Chiang's avatar
Angie Chiang committed
898 899 900 901
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
  int ignore_horiz = x_step_q4 == 16 && subpel_x_q4 == 0;
  int ignore_vert = y_step_q4 == 16 && subpel_y_q4 == 0;
902 903 904 905 906 907

  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
908
  if (ignore_horiz && ignore_vert) {
909
    highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h, ref_idx, bd);
Angie Chiang's avatar
Angie Chiang committed
910
  } else if (ignore_vert) {
911 912
#if CONFIG_DUAL_FILTER
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
913
        av1_get_interp_filter_params(interp_filter[1 + 2 * ref_idx]);
914 915
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
916
        av1_get_interp_filter_params(interp_filter);
917
#endif
918 919 920
    av1_highbd_convolve_horiz_facade(src8, src_stride, dst8, dst_stride, w, h,
                                     filter_params, subpel_x_q4, x_step_q4,
                                     ref_idx, bd);
Angie Chiang's avatar
Angie Chiang committed
921
  } else if (ignore_horiz) {
922 923
#if CONFIG_DUAL_FILTER
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
924
        av1_get_interp_filter_params(interp_filter[0 + 2 * ref_idx]);
925 926
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
927
        av1_get_interp_filter_params(interp_filter);
928
#endif
929 930 931
    av1_highbd_convolve_vert_facade(src8, src_stride, dst8, dst_stride, w, h,
                                    filter_params, subpel_y_q4, y_step_q4,
                                    ref_idx, bd);
Angie Chiang's avatar
Angie Chiang committed
932
  } else {
933 934 935 936 937
    // 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, uint16_t,
                    temp[((MAX_SB_SIZE * 2 + 16) + 16) * MAX_SB_SIZE]);
Yaowu Xu's avatar
Yaowu Xu committed
938
    uint8_t *temp8 = CONVERT_TO_BYTEPTR(temp);
939 940 941
    int max_intermediate_size = ((MAX_SB_SIZE * 2 + 16) + 16);
    int filter_size;
    InterpFilterParams filter_params;
942
#if CONFIG_DUAL_FILTER
Yaowu Xu's avatar
Yaowu Xu committed
943 944 945 946
    InterpFilterParams filter_params_x =
        av1_get_interp_filter_params(interp_filter[1 + 2 * ref_idx]);
    InterpFilterParams filter_params_y =
        av1_get_interp_filter_params(interp_filter[0 + 2 * ref_idx]);
947 948 949
#endif

#if CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
950 951 952 953 954 955
    if (interp_filter[0 + 2 * ref_idx] == MULTITAP_SHARP &&
        interp_filter[1 + 2 * ref_idx] == 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);
    }
956 957 958 959 960 961 962 963 964 965
    if (filter_params_y.taps < filter_params_x.taps) {
      int intermediate_width;
      int temp_stride = max_intermediate_size;
      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;
      assert(intermediate_width <= max_intermediate_size);

      assert(filter_params.taps <= MAX_FILTER_TAP);
Angie Chiang's avatar
Angie Chiang committed
966

967 968 969
      av1_highbd_convolve_vert_facade(
          src8 - (filter_size / 2 - 1), src_stride, temp8, temp_stride,
          intermediate_width, h, filter_params, subpel_y_q4, y_step_q4, 0, bd);
970

971 972 973 974 975 976 977
      filter_params = filter_params_x;
      assert(filter_params.taps <= MAX_FILTER_TAP);

      av1_highbd_convolve_horiz_facade(
          temp8 + (filter_size / 2 - 1), temp_stride, dst8, dst_stride, w, h,
          filter_params, subpel_x_q4, x_step_q4, ref_idx, bd);
    } else
978
#endif  // CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
979 980 981
    {
      int intermediate_height;
      int temp_stride = MAX_SB_SIZE;
982
#if CONFIG_DUAL_FILTER
983 984 985 986 987
      filter_params = filter_params_x;
      filter_size = filter_params_y.taps;
#else
      filter_params = av1_get_interp_filter_params(interp_filter);
      filter_size = filter_params.taps;
988
#endif
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
      intermediate_height =
          (((h - 1) * y_step_q4 + subpel_y_q4) >> SUBPEL_BITS) + filter_size;
      assert(intermediate_height <= max_intermediate_size);
      (void)max_intermediate_size;

      av1_highbd_convolve_horiz_facade(
          src8 - src_stride * (filter_size / 2 - 1), src_stride, temp8,
          temp_stride, w, intermediate_height, filter_params, subpel_x_q4,
          x_step_q4, 0, bd);

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

1005 1006 1007 1008
      av1_highbd_convolve_vert_facade(
          temp8 + temp_stride * (filter_size / 2 - 1), temp_stride, dst8,
          dst_stride, w, h, filter_params, subpel_y_q4, y_step_q4, ref_idx, bd);
    }
Angie Chiang's avatar
Angie Chiang committed
1009
  }
1010
}
1011
#endif  // CONFIG_HIGHBITDEPTH