convolve.c 71.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/blockd.h"
18
#include "av1/common/convolve.h"
19
#include "av1/common/filter.h"
Angie Chiang's avatar
Angie Chiang committed
20
#include "av1/common/onyxc_int.h"
Yaowu Xu's avatar
Yaowu Xu committed
21
#include "aom_dsp/aom_dsp_common.h"
22
#include "aom_ports/mem.h"
23

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

28
#if CONFIG_FRAME_SUPERRES
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

#define UPSCALE_PROC_UNIT 64  // Source step (roughly)
#define UPSCALE_PROC_UNIT_SCALE (UPSCALE_PROC_UNIT / SCALE_NUMERATOR)

void av1_convolve_horiz_rs_c(const uint8_t *src, int src_stride, uint8_t *dst,
                             int dst_stride, int w, int h,
                             const int16_t *x_filters, int interp_taps,
                             const int x0_qn, const int x_step_qn) {
  int x, y;
  src -= interp_taps / 2 - 1;
  for (y = 0; y < h; ++y) {
    int x_qn = x0_qn;
    for (x = 0; x < w; ++x) {
      const uint8_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
      const int x_filter_idx =
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
      assert(x_filter_idx <= RS_SUBPEL_MASK);
      const int16_t *const x_filter = &x_filters[x_filter_idx * interp_taps];
      int k, sum = 0;
      for (k = 0; k < interp_taps; ++k) sum += src_x[k] * x_filter[k];
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
      x_qn += x_step_qn;
    }
    src += src_stride;
    dst += dst_stride;
  }
}

#if CONFIG_HIGHBITDEPTH
void av1_highbd_convolve_horiz_rs_c(const uint16_t *src, int src_stride,
                                    uint16_t *dst, int dst_stride, int w, int h,
                                    const int16_t *x_filters, int interp_taps,
                                    int x0_qn, int x_step_qn, int bd) {
  int x, y;
  src -= interp_taps / 2 - 1;
  for (y = 0; y < h; ++y) {
    int x_qn = x0_qn;
    for (x = 0; x < w; ++x) {
      const uint16_t *const src_x = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
      const int x_filter_idx =
          (x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS;
      assert(x_filter_idx <= RS_SUBPEL_MASK);
      const int16_t *const x_filter = &x_filters[x_filter_idx * interp_taps];
      int k, sum = 0;
      for (k = 0; k < interp_taps; ++k) sum += src_x[k] * x_filter[k];
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
      x_qn += x_step_qn;
    }
    src += src_stride;
    dst += dst_stride;
  }
}
#endif  // CONFIG_HIGHBITDEPTH
82
#endif  // CONFIG_FRAME_SUPERRES
83

Yaowu Xu's avatar
Yaowu Xu committed
84 85 86
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,
87 88
                          const int subpel_x_q4, int x_step_q4,
                          ConvolveParams *conv_params) {
89
  int x, y;
90
  int filter_size = filter_params.taps;
Angie Chiang's avatar
Angie Chiang committed
91
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
92 93 94 95 96
  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
97
      const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
98
          filter_params, x_q4 & SUBPEL_MASK);
99 100
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k) sum += src_x[k] * x_filter[k];
101

Angie Chiang's avatar
Angie Chiang committed
102
      sum = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
103
      if (conv_params->do_average)
Angie Chiang's avatar
Angie Chiang committed
104 105 106
        dst[x] = ROUND_POWER_OF_TWO(dst[x] + sum, 1);
      else
        dst[x] = sum;
107

108 109 110 111 112 113 114
      x_q4 += x_step_q4;
    }
    src += src_stride;
    dst += dst_stride;
  }
}

Fergus Simpson's avatar
Fergus Simpson committed
115 116 117 118 119 120 121
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;
122
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
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
  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
148 149 150
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,
151 152
                         const int subpel_y_q4, int y_step_q4,
                         ConvolveParams *conv_params) {
153
  int x, y;
154
  int filter_size = filter_params.taps;
Angie Chiang's avatar
Angie Chiang committed
155
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
156 157 158 159 160
  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
161
      const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
162
          filter_params, y_q4 & SUBPEL_MASK);
163 164 165
      int k, sum = 0;
      for (k = 0; k < filter_size; ++k)
        sum += src_y[k * src_stride] * y_filter[k];
166

Angie Chiang's avatar
Angie Chiang committed
167
      sum = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
168
      if (conv_params->do_average)
Angie Chiang's avatar
Angie Chiang committed
169 170 171
        dst[y * dst_stride] = ROUND_POWER_OF_TWO(dst[y * dst_stride] + sum, 1);
      else
        dst[y * dst_stride] = sum;
172

173 174 175 176 177 178 179
      y_q4 += y_step_q4;
    }
    ++src;
    ++dst;
  }
}

Fergus Simpson's avatar
Fergus Simpson committed
180 181 182 183 184 185 186
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;
187
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
Fergus Simpson's avatar
Fergus Simpson committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  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
215
static void convolve_copy(const uint8_t *src, int src_stride, uint8_t *dst,
216 217
                          int dst_stride, int w, int h,
                          ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
218
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
219
  if (conv_params->do_average == 0) {
Angie Chiang's avatar
Angie Chiang committed
220
    int r;
Angie Chiang's avatar
Angie Chiang committed
221
    for (r = 0; r < h; ++r) {
Angie Chiang's avatar
Angie Chiang committed
222
      memcpy(dst, src, w);
Angie Chiang's avatar
Angie Chiang committed
223 224 225 226 227 228
      src += src_stride;
      dst += dst_stride;
    }
  } else {
    int r, c;
    for (r = 0; r < h; ++r) {
229
      for (c = 0; c < w; ++c) {
Angie Chiang's avatar
Angie Chiang committed
230
        dst[c] = clip_pixel(ROUND_POWER_OF_TWO(dst[c] + src[c], 1));
231
      }
Angie Chiang's avatar
Angie Chiang committed
232 233 234 235 236 237
      src += src_stride;
      dst += dst_stride;
    }
  }
}

238 239 240
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,
241 242
                               const int subpel_x_q4, int x_step_q4,
                               ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
243 244
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
245 246
    const int16_t *filter_x =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_x_q4);
247
    if (conv_params->do_average == 0)
248 249 250 251 252 253
      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
254 255
    av1_convolve_horiz(src, src_stride, dst, dst_stride, w, h, filter_params,
                       subpel_x_q4, x_step_q4, conv_params);
256 257 258
  }
}

259 260 261 262 263 264 265 266 267
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);
268
    if (conv_params->do_average == 0)
269 270 271 272 273 274 275 276 277 278 279
      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
280 281 282 283 284
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) {
285
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
Fergus Simpson's avatar
Fergus Simpson committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  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);
  }
}

302 303 304
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,
305 306
                              const int subpel_y_q4, int y_step_q4,
                              ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
307 308
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
  if (filter_params.taps == SUBPEL_TAPS) {
309 310
    const int16_t *filter_y =
        av1_get_interp_filter_subpel_kernel(filter_params, subpel_y_q4);
311
    if (conv_params->do_average == 0) {
312 313 314 315 316 317 318
      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
319 320
    av1_convolve_vert(src, src_stride, dst, dst_stride, w, h, filter_params,
                      subpel_y_q4, y_step_q4, conv_params);
321 322 323
  }
}

324 325 326 327 328 329 330 331 332
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);
333
    if (conv_params->do_average == 0) {
334 335 336 337 338 339 340 341 342 343 344 345
      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
346 347 348 349 350
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) {
351
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
Fergus Simpson's avatar
Fergus Simpson committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  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
368
#if CONFIG_CONVOLVE_ROUND
369 370
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
371 372 373 374
  int r, c;
  for (r = 0; r < h; ++r) {
    for (c = 0; c < w; ++c) {
      dst[r * dst_stride + c] =
375
          clip_pixel(ROUND_POWER_OF_TWO(src[r * src_stride + c], bits));
Angie Chiang's avatar
Angie Chiang committed
376 377 378
    }
  }
}
Angie Chiang's avatar
Angie Chiang committed
379

380
#if CONFIG_COMPOUND_ROUND
David Barker's avatar
David Barker committed
381 382 383 384 385 386
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
387
  int x, y, k;
388
  uint8_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
Angie Chiang's avatar
Angie Chiang committed
389 390 391 392
  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;
393

Angie Chiang's avatar
Angie Chiang committed
394 395 396 397 398 399
  // 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) {
400
      int32_t sum = 0;
Angie Chiang's avatar
Angie Chiang committed
401 402 403
      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
404
      im_block[y * im_stride + x] =
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
          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);
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
#if CONFIG_JNT_COMP
      if (conv_params->bck_offset == -1) {
        if (conv_params->do_average)
          dst[y * dst_stride + x] += res;
        else
          dst[y * dst_stride + x] = res;
      } else {
        if (conv_params->do_average == 0) {
          dst[y * dst_stride + x] = res * conv_params->fwd_offset;
        } else {
          dst[y * dst_stride + x] += res * conv_params->bck_offset;

          dst[y * dst_stride + x] >>= (DIST_PRECISION_BITS - 1);
        }
      }
#else
436 437 438 439
      if (conv_params->do_average)
        dst[y * dst_stride + x] += res;
      else
        dst[y * dst_stride + x] = res;
440
#endif  // CONFIG_JNT_COMP
441 442 443
    }
  }
}
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

void av1_convolve_2d_scale_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_qn, const int x_step_qn,
                             const int subpel_y_qn, const int y_step_qn,
                             ConvolveParams *conv_params) {
  int x, y, k;
  uint8_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
             filter_params_y->taps;
  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 uint8_t *src_horiz = src - fo_vert * src_stride;
  for (y = 0; y < im_h; ++y) {
    int x_qn = subpel_x_qn;
    for (x = 0; x < w; ++x, x_qn += x_step_qn) {
      const uint8_t *const src_x = &src_horiz[(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, x_filter_idx);
      int sum = 0;
      for (k = 0; k < filter_params_x->taps; ++k)
        sum += x_filter[k] * src_x[k - fo_horiz];
      im_block[y * im_stride + x] =
          clip_pixel(ROUND_POWER_OF_TWO(sum, conv_params->round_0));
    }
    src_horiz += src_stride;
  }

  // vertical filter
  const uint8_t *src_vert = im_block + fo_vert * im_stride;
  for (x = 0; x < w; ++x) {
    int y_qn = subpel_y_qn;
    for (y = 0; y < h; ++y, y_qn += y_step_qn) {
      const uint8_t *const src_y =
          &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_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, y_filter_idx);
      CONV_BUF_TYPE sum = 0;
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
      }
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
#if CONFIG_JNT_COMP
      if (conv_params->bck_offset == -1) {
        if (conv_params->do_average)
          dst[y * dst_stride + x] += res;
        else
          dst[y * dst_stride + x] = res;
      } else {
        if (conv_params->do_average == 0) {
          dst[y * dst_stride + x] = res * conv_params->fwd_offset;
        } else {
          dst[y * dst_stride + x] += res * conv_params->bck_offset;

          dst[y * dst_stride + x] >>= (DIST_PRECISION_BITS - 1);
        }
      }
#else
511 512 513 514
      if (conv_params->do_average)
        dst[y * dst_stride + x] += res;
      else
        dst[y * dst_stride + x] = res;
515
#endif  // CONFIG_JNT_COMP
516 517 518 519 520
    }
    src_vert++;
  }
}

Angie Chiang's avatar
Angie Chiang committed
521
#else
522

523 524 525 526 527 528
/* 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
529 530 531 532 533 534
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) {
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
  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
554
      im_block[y * im_stride + x] =
555
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
Angie Chiang's avatar
Angie Chiang committed
556 557 558 559
    }
  }

  // vertical filter
560
  int32_t *src_vert = im_block + fo_vert * im_stride;
Angie Chiang's avatar
Angie Chiang committed
561 562
  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
      *filter_params_y, subpel_y_q4 & SUBPEL_MASK);
563
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
Angie Chiang's avatar
Angie Chiang committed
564 565
  for (y = 0; y < h; ++y) {
    for (x = 0; x < w; ++x) {
566
      CONV_BUF_TYPE sum = 1 << offset_bits;
Angie Chiang's avatar
Angie Chiang committed
567 568 569
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
      }
570 571 572 573
      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)));
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
#if CONFIG_JNT_COMP
      if (conv_params->fwd_offset == -1) {
        if (conv_params->do_average)
          dst[y * dst_stride + x] += res;
        else
          dst[y * dst_stride + x] = res;
      } else {
        if (conv_params->do_average) {
          dst[y * dst_stride + x] += res * conv_params->bck_offset;

          dst[y * dst_stride + x] >>= (DIST_PRECISION_BITS - 1);
        } else {
          dst[y * dst_stride + x] = res * conv_params->fwd_offset;
        }
      }
#else
590 591 592 593
      if (conv_params->do_average)
        dst[y * dst_stride + x] += res;
      else
        dst[y * dst_stride + x] = res;
594
#endif  // CONFIG_JNT_COMP
Angie Chiang's avatar
Angie Chiang committed
595 596 597
    }
  }
}
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

void av1_convolve_2d_scale_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_qn, const int x_step_qn,
                             const int subpel_y_qn, const int y_step_qn,
                             ConvolveParams *conv_params) {
  int x, y, k;
  int32_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
             filter_params_y->taps;
  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;
  for (y = 0; y < im_h; ++y) {
    int x_qn = subpel_x_qn;
    for (x = 0; x < w; ++x, x_qn += x_step_qn) {
      const uint8_t *const src_x = &src_horiz[(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, x_filter_idx);
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_x[k - fo_horiz];
      }
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
      im_block[y * im_stride + x] =
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
    }
    src_horiz += src_stride;
  }

  // vertical filter
  int32_t *src_vert = im_block + fo_vert * im_stride;
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
  for (x = 0; x < w; ++x) {
    int y_qn = subpel_y_qn;
    for (y = 0; y < h; ++y, y_qn += y_step_qn) {
      const int32_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_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, y_filter_idx);
      CONV_BUF_TYPE sum = 1 << offset_bits;
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
      }
      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)));
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
#if CONFIG_JNT_COMP
      if (conv_params->fwd_offset == -1) {
        if (conv_params->do_average)
          dst[y * dst_stride + x] += res;
        else
          dst[y * dst_stride + x] = res;
      } else {
        if (conv_params->do_average) {
          dst[y * dst_stride + x] += res * conv_params->bck_offset;

          dst[y * dst_stride + x] >>= (DIST_PRECISION_BITS - 1);
        } else {
          dst[y * dst_stride + x] = res * conv_params->fwd_offset;
        }
      }
#else
671 672 673 674
      if (conv_params->do_average)
        dst[y * dst_stride + x] += res;
      else
        dst[y * dst_stride + x] = res;
675
#endif  // CONFIG_JNT_COMP
676 677 678 679 680
    }
    src_vert++;
  }
}
#endif  // CONFIG_COMPOUND_ROUND
Angie Chiang's avatar
Angie Chiang committed
681 682 683

void av1_convolve_2d_facade(const uint8_t *src, int src_stride, uint8_t *dst,
                            int dst_stride, int w, int h,
684 685 686
                            InterpFilters interp_filters, const int subpel_x_q4,
                            int x_step_q4, const int subpel_y_q4, int y_step_q4,
                            int scaled, ConvolveParams *conv_params) {
Angie Chiang's avatar
Angie Chiang committed
687 688 689 690
  (void)x_step_q4;
  (void)y_step_q4;
  (void)dst;
  (void)dst_stride;
691 692

  InterpFilterParams filter_params_x, filter_params_y;
Zhijie Yang's avatar
Zhijie Yang committed
693 694 695 696
#if CONFIG_SHORT_FILTER
  av1_get_convolve_filter_params(interp_filters, 1, &filter_params_x,
                                 &filter_params_y, w, h);
#else
697 698
  av1_get_convolve_filter_params(interp_filters, 1, &filter_params_x,
                                 &filter_params_y);
Zhijie Yang's avatar
Zhijie Yang committed
699
#endif
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

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

716 717 718 719 720 721 722 723 724 725 726 727 728
// horizontal and vertical parameters are swapped because of the transpose
#if CONFIG_JNT_COMP
    if (scaled)
      av1_convolve_2d_scale_c(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,
                              y_step_q4, subpel_x_q4, x_step_q4, conv_params);
    else
      av1_convolve_2d_c(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);
#else
729 730 731 732 733 734 735 736 737 738
    if (scaled)
      av1_convolve_2d_scale(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,
                            y_step_q4, subpel_x_q4, x_step_q4, conv_params);
    else
      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);
739
#endif  // CONFIG_JNT_COMP
740 741 742
    transpose_int32(conv_params->dst, conv_params->dst_stride, tr_dst,
                    tr_dst_stride, h, w);
  } else {
743 744 745 746 747 748 749 750 751 752 753 754
#if CONFIG_JNT_COMP
    if (scaled)
      av1_convolve_2d_scale_c(src, src_stride, conv_params->dst,
                              conv_params->dst_stride, w, h, &filter_params_x,
                              &filter_params_y, subpel_x_q4, x_step_q4,
                              subpel_y_q4, y_step_q4, conv_params);
    else
      av1_convolve_2d_c(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);
#else
755 756 757 758 759 760 761 762 763
    if (scaled)
      av1_convolve_2d_scale(src, src_stride, conv_params->dst,
                            conv_params->dst_stride, w, h, &filter_params_x,
                            &filter_params_y, subpel_x_q4, x_step_q4,
                            subpel_y_q4, y_step_q4, conv_params);
    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);
764
#endif  // CONFIG_JNT_COMP
765
  }
Angie Chiang's avatar
Angie Chiang committed
766 767
}

768
#if CONFIG_HIGHBITDEPTH
769 770 771
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) {
772 773 774 775 776
  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(
777
          ROUND_POWER_OF_TWO(src[r * src_stride + c], bits), bd);
778 779 780 781
    }
  }
}

782
#if CONFIG_COMPOUND_ROUND
David Barker's avatar
David Barker committed
783 784 785 786 787 788
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) {
789
  int x, y, k;
790
  uint16_t im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
791 792 793 794
  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;
795

796 797 798 799 800 801
  // 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) {
802
      int32_t sum = 0;
803 804 805
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
      }
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
      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);
822 823 824 825
      if (conv_params->do_average)
        dst[y * dst_stride + x] += res;
      else
        dst[y * dst_stride + x] = res;
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

void av1_highbd_convolve_2d_scale_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_qn, const int x_step_qn,
                                    const int subpel_y_qn, const int y_step_qn,
                                    ConvolveParams *conv_params, int bd) {
  int x, y, k;
  uint16_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
             filter_params_y->taps;
  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)bd;

  // horizontal filter
  const uint16_t *src_horiz = src - fo_vert * src_stride;
  for (y = 0; y < im_h; ++y) {
    int x_qn = subpel_x_qn;
    for (x = 0; x < w; ++x, x_qn += x_step_qn) {
      const uint16_t *const src_x = &src_horiz[(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, x_filter_idx);
      int sum = 0;
      for (k = 0; k < filter_params_x->taps; ++k)
        sum += x_filter[k] * src_x[k - fo_horiz];
      im_block[y * im_stride + x] =
          clip_pixel(ROUND_POWER_OF_TWO(sum, conv_params->round_0));
    }
    src_horiz += src_stride;
  }

  // vertical filter
  uint16_t *src_vert = im_block + fo_vert * im_stride;
  for (x = 0; x < w; ++x) {
    int y_qn = subpel_y_qn;
    for (y = 0; y < h; ++y, y_qn += y_step_qn) {
      const uint16_t *const src_y =
          &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_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, y_filter_idx);
      CONV_BUF_TYPE sum = 0;
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
      }
      CONV_BUF_TYPE res = ROUND_POWER_OF_TWO(sum, conv_params->round_1);
881 882 883 884
      if (conv_params->do_average)
        dst[y * dst_stride + x] += res;
      else
        dst[y * dst_stride + x] = res;
885 886 887 888 889
    }
    src_vert++;
  }
}

890
#else
891

David Barker's avatar
David Barker committed
892 893 894 895 896 897
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) {
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
  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)));
916 917
      (void)bd;
      im_block[y * im_stride + x] =
918
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
919 920 921 922
    }
  }

  // vertical filter
923 924
  int32_t *src_vert = im_block + fo_vert * im_stride;
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
925 926 927 928
  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) {
929
      CONV_BUF_TYPE sum = 1 << offset_bits;
930 931 932
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
      }
933 934 935 936
      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)));
937 938 939 940
      if (conv_params->do_average)
        dst[y * dst_stride + x] += res;
      else
        dst[y * dst_stride + x] = res;
941 942 943
    }
  }
}
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

void av1_highbd_convolve_2d_scale_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_qn, const int x_step_qn,
                                    const int subpel_y_qn, const int y_step_qn,
                                    ConvolveParams *conv_params, int bd) {
  int x, y, k;
  int32_t im_block[(2 * MAX_SB_SIZE + MAX_FILTER_TAP) * MAX_SB_SIZE];
  int im_h = (((h - 1) * y_step_qn + subpel_y_qn) >> SCALE_SUBPEL_BITS) +
             filter_params_y->taps;
  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;
  for (y = 0; y < im_h; ++y) {
    int x_qn = subpel_x_qn;
    for (x = 0; x < w; ++x, x_qn += x_step_qn) {
      const uint16_t *const src_x = &src_horiz[(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, x_filter_idx);
      int32_t sum = (1 << (bd + FILTER_BITS - 1));
      for (k = 0; k < filter_params_x->taps; ++k) {
        sum += x_filter[k] * src_x[k - fo_horiz];
      }
      assert(0 <= sum && sum < (1 << (bd + FILTER_BITS + 1)));
      im_block[y * im_stride + x] =
          ROUND_POWER_OF_TWO(sum, conv_params->round_0);
    }
    src_horiz += src_stride;
  }

  // vertical filter
  int32_t *src_vert = im_block + fo_vert * im_stride;
  const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
  for (x = 0; x < w; ++x) {
    int y_qn = subpel_y_qn;
    for (y = 0; y < h; ++y, y_qn += y_step_qn) {
      const int32_t *src_y = &src_vert[(y_qn >> SCALE_SUBPEL_BITS) * im_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, y_filter_idx);
      CONV_BUF_TYPE sum = 1 << offset_bits;
      for (k = 0; k < filter_params_y->taps; ++k) {
        sum += y_filter[k] * src_y[(k - fo_vert) * im_stride];
      }
      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)));
1000 1001 1002 1003
      if (conv_params->do_average)
        dst[y * dst_stride + x] += res;
      else
        dst[y * dst_stride + x] = res;
1004 1005 1006 1007 1008
    }
    src_vert++;
  }
}
#endif  // CONFIG_COMPOUND_ROUND
1009 1010 1011

void av1_highbd_convolve_2d_facade(const uint8_t *src8, int src_stride,
                                   uint8_t *dst, int dst_stride, int w, int h,
1012
                                   InterpFilters interp_filters,
1013 1014
                                   const int subpel_x_q4, int x_step_q4,
                                   const int subpel_y_q4, int y_step_q4,
1015 1016
                                   int scaled, ConvolveParams *conv_params,
                                   int bd) {
1017 1018 1019 1020
  (void)x_step_q4;
  (void)y_step_q4;
  (void)dst;
  (void)dst_stride;
1021 1022

  InterpFilterParams filter_params_x, filter_params_y;
Zhijie Yang's avatar
Zhijie Yang committed
1023 1024 1025 1026
#if CONFIG_SHORT_FILTER
  av1_get_convolve_filter_params(interp_filters, 1, &filter_params_x,
                                 &filter_params_y, w, h);
#else
1027 1028
  av1_get_convolve_filter_params(interp_filters, 1, &filter_params_x,
                                 &filter_params_y);
Zhijie Yang's avatar
Zhijie Yang committed
1029
#endif
1030

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  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
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    if (scaled)
      av1_highbd_convolve_2d_scale(
          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,
          y_step_q4, subpel_x_q4, x_step_q4, conv_params, bd);
    else
      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);
1058 1059 1060
    transpose_int32(conv_params->dst, conv_params->dst_stride, tr_dst,
                    tr_dst_stride, h, w);
  } else {
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
    if (scaled)
      av1_highbd_convolve_2d_scale(
          src, src_stride, conv_params->dst, conv_params->dst_stride, w, h,
          &filter_params_x, &filter_params_y, subpel_x_q4, x_step_q4,
          subpel_y_q4, y_step_q4, conv_params, bd);
    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);
  }
}
1073 1074
#endif  // CONFIG_HIGHBITDEPTH

Angie Chiang's avatar
Angie Chiang committed
1075
#endif  // CONFIG_CONVOLVE_ROUND
Angie Chiang's avatar
Angie Chiang committed
1076

1077 1078 1079 1080 1081 1082 1083 1084
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,
1085
                            const InterpFilters interp_filters,
1086 1087 1088 1089 1090
                            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
1091 1092
  int ignore_horiz = x_step_q4 == SUBPEL_SHIFTS && subpel_x_q4 == 0;
  int ignore_vert = y_step_q4 == SUBPEL_SHIFTS && subpel_y_q4 == 0;
1093 1094

  InterpFilterParams filter_params_x, filter_params_y;
Zhijie Yang's avatar
Zhijie Yang committed
1095 1096 1097 1098
#if CONFIG_SHORT_FILTER
  av1_get_convolve_filter_params(interp_filters, 0, &filter_params_x,
                                 &filter_params_y, w, h);
#else
1099 1100
  av1_get_convolve_filter_params(interp_filters, 0, &filter_params_x,
                                 &filter_params_y);
Zhijie Yang's avatar
Zhijie Yang committed
1101
#endif
1102

Angie Chiang's avatar
Angie Chiang committed
1103
  assert(conv_params->round == CONVOLVE_OPT_ROUND);
1104 1105 1106 1107 1108 1109

  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
1110
  if (ignore_horiz && ignore_vert) {
1111
    convolve_copy(src, src_stride, dst, dst_stride, w, h, conv_params);
Angie Chiang's avatar
Angie Chiang committed
1112
  } else if (ignore_vert) {
1113 1114
    assert(filter_params_x.taps <= MAX_FILTER_TAP);
    convolve_horiz(src, src_stride, dst, dst_stride, w, h, filter_params_x,
1115
                   subpel_x_q4, x_step_q4, conv_params);
Angie Chiang's avatar
Angie Chiang committed
1116
  } else if (ignore_horiz) {
1117 1118
    assert(filter_params_y.taps <= MAX_FILTER_TAP);
    convolve_vert(src, src_stride, dst, dst_stride, w, h, filter_params_y,
1119
                  subpel_y_q4, y_step_q4, conv_params);
Angie Chiang's avatar
Angie Chiang committed
1120
  } else {
1121 1122 1123 1124 1125 1126
    // 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);
1127
    int filter_size;
1128
#if CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
1129
    av1_convolve_filter_params_fixup_1212(&filter_params_x, &filter_params_y);
1130

1131 1132 1133 1134
    // we do filter with fewer taps first to reduce hardware implementation
    // complexity
    if (filter_params_y.taps < filter_params_x.taps) {
      int intermediate_width;
1135
      int temp_stride = max_intermediate_size;
1136 1137
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
1138
      temp_conv_params.do_average = 0;
1139
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
1140 1141 1142
      filter_size = filter_params_x.taps;
      intermediate_width =
          (((w - 1) * x_step_q4 + subpel_x_q4) >> SUBPEL_BITS) + filter_size;
1143
      assert(intermediate_width <= max_intermediate_size);
Angie Chiang's avatar
Angie Chiang committed
1144

1145
      assert(filter_params_y.taps <= MAX_FILTER_TAP);
1146

1147
      convolve_vert(src - (filter_size / 2 - 1), src_stride, temp, temp_stride,
1148
                    intermediate_width, h, filter_params_y, subpel_y_q4,
1149
                    y_step_q4, &temp_conv_params);
1150

1151
      assert(filter_params_x.taps <= MAX_FILTER_TAP);
1152
      convolve_horiz(temp + (filter_size / 2 - 1), temp_stride, dst, dst_stride,
1153 1154
                     w, h, filter_params_x, subpel_x_q4, x_step_q4,
                     conv_params);
1155
    } else
1156
#endif  // CONFIG_DUAL_FILTER && USE_EXTRA_FILTER
1157 1158
    {
      int intermediate_height;
1159
      int temp_stride = MAX_SB_SIZE;
1160 1161
      ConvolveParams temp_conv_params;
      temp_conv_params.ref = 0;
1162
      temp_conv_params.do_average = 0;
1163
      temp_conv_params.round = CONVOLVE_OPT_ROUND;
1164 1165 1166
      filter_size = filter_params_y.taps;
      intermediate_height =
          (((h - 1) * y_step_q4 + subpel_y_q4) >> SUBPEL_BITS) + filter_size;
1167 1168
      assert(intermediate_height <= max_intermediate_size);
      (void)max_intermediate_size;
1169

1170
      assert(filter_params_x.taps <= MAX_FILTER_TAP);
1171