convolve.c 25.7 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"
Yaowu Xu's avatar
Yaowu Xu committed
19
#include "aom_dsp/aom_dsp_common.h"
20
#include "aom_ports/mem.h"
21

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

Yaowu Xu's avatar
Yaowu Xu committed
26 27 28
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,
29 30
                          const int subpel_x_q4, int x_step_q4,
                          ConvolveParams *conv_params) {
31
  int x, y;
32
  int filter_size = filter_params.taps;
Angie Chiang's avatar
Angie Chiang committed
33
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
34 35 36 37 38
  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
39
      const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
40
          filter_params, x_q4 & SUBPEL_MASK);
41 42
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k) sum += src_x[k] * x_filter[k];
43

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

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

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

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

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

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

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

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,
136 137
                              const int subpel_y_q4, int y_step_q4,
                              ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
138 139
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
140 141
    const int16_t *filter_y =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_y_q4);
142
    if (conv_params->ref == 0) {
143 144 145 146 147 148 149
      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
150 151
    av1_convolve_vert(src, src_stride, dst, dst_stride, w, h, filter_params,
                      subpel_y_q4, y_step_q4, conv_params);
152 153 154
  }
}

Angie Chiang's avatar
Angie Chiang committed
155
#if CONFIG_CONVOLVE_ROUND
Angie Chiang's avatar
Angie Chiang committed
156
void av1_convolve_rounding(const int32_t *src, int src_stride, uint8_t *dst,
Angie Chiang's avatar
Angie Chiang committed
157
                           int dst_stride, int w, int h, int bits) {
Angie Chiang's avatar
Angie Chiang committed
158 159 160 161
  int r, c;
  for (r = 0; r < h; ++r) {
    for (c = 0; c < w; ++c) {
      dst[r * dst_stride + c] =
Angie Chiang's avatar
Angie Chiang committed
162
          clip_pixel(ROUND_POWER_OF_TWO_SIGNED(src[r * src_stride + c], bits));
Angie Chiang's avatar
Angie Chiang committed
163 164 165
    }
  }
}
Angie Chiang's avatar
Angie Chiang committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

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;
  CONV_BUF_TYPE 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;
  (void)conv_params;
  // 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) {
      CONV_BUF_TYPE sum = 0;
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
      }
      im_block[y * im_stride + x] =
          ROUND_POWER_OF_TWO_SIGNED(sum, conv_params->round_0);
    }
  }

  // vertical filter
  CONV_BUF_TYPE *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];
      }
      dst[y * dst_stride + x] +=
          ROUND_POWER_OF_TWO_SIGNED(sum, conv_params->round_1);
    }
  }
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
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
228 229 230 231 232 233 234 235 236 237
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;
238
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
239 240 241 242
  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]);
243

244 245 246 247 248 249
  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);
  }
250 251 252 253 254
#else
  InterpFilterParams filter_params_x =
      av1_get_interp_filter_params(*interp_filter);
  InterpFilterParams filter_params_y =
      av1_get_interp_filter_params(*interp_filter);
255
#endif
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

  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
283 284
}

Angie Chiang's avatar
Angie Chiang committed
285
#endif  // CONFIG_CONVOLVE_ROUND
Angie Chiang's avatar
Angie Chiang committed
286

Yaowu Xu's avatar
Yaowu Xu committed
287 288
void av1_convolve(const uint8_t *src, int src_stride, uint8_t *dst,
                  int dst_stride, int w, int h,
289
#if CONFIG_DUAL_FILTER
James Zern's avatar
James Zern committed
290
                  const InterpFilter *interp_filter,
291
#else
James Zern's avatar
James Zern committed
292
                  const InterpFilter interp_filter,
293
#endif
Yaowu Xu's avatar
Yaowu Xu committed
294
                  const int subpel_x_q4, int x_step_q4, const int subpel_y_q4,
295
                  int y_step_q4, ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
296 297
  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
298 299 300 301 302 303 304 305 306 307
#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
308
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
309 310 311 312 313 314

  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
315
  if (ignore_horiz && ignore_vert) {
316
    convolve_copy(src, src_stride, dst, dst_stride, w, h, conv_params);
Angie Chiang's avatar
Angie Chiang committed
317
  } else if (ignore_vert) {
318
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
319
    filter_params = filter_params_x;
320 321
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
322
    av1_convolve_horiz_facade(src, src_stride, dst, dst_stride, w, h,
323 324
                              filter_params, subpel_x_q4, x_step_q4,
                              conv_params);
Angie Chiang's avatar
Angie Chiang committed
325
  } else if (ignore_horiz) {
326
#if CONFIG_DUAL_FILTER
Angie Chiang's avatar
Angie Chiang committed
327
    filter_params = filter_params_y;
328 329
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
330
    av1_convolve_vert_facade(src, src_stride, dst, dst_stride, w, h,
331 332
                             filter_params, subpel_y_q4, y_step_q4,
                             conv_params);
Angie Chiang's avatar
Angie Chiang committed
333
  } else {
334 335 336 337 338 339
    // 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);
340
    int filter_size;
341
#if CONFIG_DUAL_FILTER
342 343
    if (interp_filter[0 + 2 * conv_params->ref] == MULTITAP_SHARP &&
        interp_filter[1 + 2 * conv_params->ref] == MULTITAP_SHARP) {
344 345 346 347
      // Avoid two directions both using 12-tap filter.
      // This will reduce hardware implementation cost.
      filter_params_y = av1_get_interp_filter_params(EIGHTTAP_SHARP);
    }
348

349 350 351 352
    // we do filter with fewer taps first to reduce hardware implementation
    // complexity
    if (filter_params_y.taps < filter_params_x.taps) {
      int intermediate_width;
353
      int temp_stride = max_intermediate_size;
354 355
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
356
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
357 358 359 360
      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;
361
      assert(intermediate_width <= max_intermediate_size);
Angie Chiang's avatar
Angie Chiang committed
362

363
      assert(filter_params.taps <= MAX_FILTER_TAP);
364

365 366
      av1_convolve_vert_facade(src - (filter_size / 2 - 1), src_stride, temp,
                               temp_stride, intermediate_width, h,
367 368
                               filter_params, subpel_y_q4, y_step_q4,
                               &temp_conv_params);
369

370 371
      filter_params = filter_params_x;
      assert(filter_params.taps <= MAX_FILTER_TAP);
372 373
      av1_convolve_horiz_facade(temp + (filter_size / 2 - 1), temp_stride, dst,
                                dst_stride, w, h, filter_params, subpel_x_q4,
374
                                x_step_q4, conv_params);
375
    } else
376
#endif  // CONFIG_DUAL_FILTER
377 378
    {
      int intermediate_height;
379
      int temp_stride = MAX_SB_SIZE;
380 381
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
382
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
383 384 385 386 387 388 389 390
#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;
391 392
      assert(intermediate_height <= max_intermediate_size);
      (void)max_intermediate_size;
393 394 395

      assert(filter_params.taps <= MAX_FILTER_TAP);

396 397 398
      av1_convolve_horiz_facade(src - src_stride * (filter_size / 2 - 1),
                                src_stride, temp, temp_stride, w,
                                intermediate_height, filter_params, subpel_x_q4,
399
                                x_step_q4, &temp_conv_params);
400 401 402 403 404 405

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

406 407 408
      av1_convolve_vert_facade(
          temp + temp_stride * (filter_size / 2 - 1), temp_stride, dst,
          dst_stride, w, h, filter_params, subpel_y_q4, y_step_q4, conv_params);
409
    }
Angie Chiang's avatar
Angie Chiang committed
410
  }
411 412
}

Angie Chiang's avatar
Angie Chiang committed
413 414 415 416 417
void av1_convolve_init_c(void) {
  // A placeholder for SIMD initialization
  return;
}

Yaowu Xu's avatar
Yaowu Xu committed
418 419 420 421 422 423
#if CONFIG_AOM_HIGHBITDEPTH
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) {
424
  int x, y;
425
  int filter_size = filter_params.taps;
426 427 428 429 430
  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
431
      const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
432
          filter_params, x_q4 & SUBPEL_MASK);
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
      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
449 450 451 452 453
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) {
454
  int x, y;
455
  int filter_size = filter_params.taps;
456 457 458 459 460 461
  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
462
      const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
463
          filter_params, y_q4 & SUBPEL_MASK);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
      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
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
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;
    }
  }
}

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
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);
536

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  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
553 554
void av1_highbd_convolve(const uint8_t *src8, int src_stride, uint8_t *dst8,
                         int dst_stride, int w, int h,
555
#if CONFIG_DUAL_FILTER
James Zern's avatar
James Zern committed
556
                         const InterpFilter *interp_filter,
557
#else
James Zern's avatar
James Zern committed
558
                         const InterpFilter interp_filter,
559
#endif
Yaowu Xu's avatar
Yaowu Xu committed
560 561 562
                         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
563 564 565 566
  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;
567 568 569 570 571 572

  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
573
  if (ignore_horiz && ignore_vert) {
574
    highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h, ref_idx, bd);
Angie Chiang's avatar
Angie Chiang committed
575
  } else if (ignore_vert) {
576 577
#if CONFIG_DUAL_FILTER
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
578
        av1_get_interp_filter_params(interp_filter[1 + 2 * ref_idx]);
579 580
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
581
        av1_get_interp_filter_params(interp_filter);
582
#endif
583 584 585
    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
586
  } else if (ignore_horiz) {
587 588
#if CONFIG_DUAL_FILTER
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
589
        av1_get_interp_filter_params(interp_filter[0 + 2 * ref_idx]);
590 591
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
592
        av1_get_interp_filter_params(interp_filter);
593
#endif
594 595 596
    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
597
  } else {
598 599 600 601 602
    // 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
603
    uint8_t *temp8 = CONVERT_TO_BYTEPTR(temp);
604 605 606
    int max_intermediate_size = ((MAX_SB_SIZE * 2 + 16) + 16);
    int filter_size;
    InterpFilterParams filter_params;
607
#if CONFIG_DUAL_FILTER
Yaowu Xu's avatar
Yaowu Xu committed
608 609 610 611
    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]);
612 613 614 615 616 617
    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);
    }
618
#endif
619

620 621 622 623 624 625 626 627 628 629 630
#if CONFIG_DUAL_FILTER
    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
631

632 633 634
      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);
635

636 637 638 639 640 641 642 643 644 645 646
      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
#endif  // CONFIG_DUAL_FILTER
    {
      int intermediate_height;
      int temp_stride = MAX_SB_SIZE;
647
#if CONFIG_DUAL_FILTER
648 649 650 651 652
      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;
653
#endif
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
      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);
669

670 671 672 673
      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
674
  }
675
}
Yaowu Xu's avatar
Yaowu Xu committed
676
#endif  // CONFIG_AOM_HIGHBITDEPTH