convolve.c 14.3 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

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

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

Yaowu Xu's avatar
Yaowu Xu committed
26 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,
                          const int subpel_x_q4, int x_step_q4, int avg) {
30
  int x, y;
31
  int filter_size = filter_params.taps;
32 33 34 35 36
  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
37
      const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
38
          filter_params, x_q4 & SUBPEL_MASK);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
      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(ROUND_POWER_OF_TWO(sum, FILTER_BITS)), 1);
      } else {
        dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
      }
      x_q4 += x_step_q4;
    }
    src += src_stride;
    dst += dst_stride;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
54 55 56 57
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,
                         const int subpel_y_q4, int y_step_q4, int avg) {
58
  int x, y;
59
  int filter_size = filter_params.taps;
60 61 62 63 64 65
  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
66
      const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
67
          filter_params, y_q4 & SUBPEL_MASK);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      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(ROUND_POWER_OF_TWO(sum, FILTER_BITS)),
            1);
      } else {
        dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
      }
      y_q4 += y_step_q4;
    }
    ++src;
    ++dst;
  }
}

Angie Chiang's avatar
Angie Chiang committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
static void convolve_copy(const uint8_t *src, int src_stride, uint8_t *dst,
                          int dst_stride, int w, int h, int avg) {
  if (avg == 0) {
    int r;
    for (r = 0; r < h; ++r) {
      memcpy(dst, src, w);
      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(ROUND_POWER_OF_TWO(dst[c] + src[c], 1));
      }
      src += src_stride;
      dst += dst_stride;
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
107 108
void av1_convolve(const uint8_t *src, int src_stride, uint8_t *dst,
                  int dst_stride, int w, int h,
109
#if CONFIG_DUAL_FILTER
James Zern's avatar
James Zern committed
110
                  const InterpFilter *interp_filter,
111
#else
James Zern's avatar
James Zern committed
112
                  const InterpFilter interp_filter,
113
#endif
Yaowu Xu's avatar
Yaowu Xu committed
114 115
                  const int subpel_x_q4, int x_step_q4, const int subpel_y_q4,
                  int y_step_q4, int ref_idx) {
Angie Chiang's avatar
Angie Chiang committed
116 117
  int ignore_horiz = x_step_q4 == 16 && subpel_x_q4 == 0;
  int ignore_vert = y_step_q4 == 16 && subpel_y_q4 == 0;
118 119 120 121 122 123

  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
124
  if (ignore_horiz && ignore_vert) {
125
    convolve_copy(src, src_stride, dst, dst_stride, w, h, ref_idx);
Angie Chiang's avatar
Angie Chiang committed
126
  } else if (ignore_vert) {
127 128
#if CONFIG_DUAL_FILTER
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
129
        av1_get_interp_filter_params(interp_filter[1 + 2 * ref_idx]);
130 131
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
132
        av1_get_interp_filter_params(interp_filter);
133 134
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
Yaowu Xu's avatar
Yaowu Xu committed
135 136
    av1_convolve_horiz(src, src_stride, dst, dst_stride, w, h, filter_params,
                       subpel_x_q4, x_step_q4, ref_idx);
Angie Chiang's avatar
Angie Chiang committed
137
  } else if (ignore_horiz) {
138 139
#if CONFIG_DUAL_FILTER
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
140
        av1_get_interp_filter_params(interp_filter[2 * ref_idx]);
141 142
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
143
        av1_get_interp_filter_params(interp_filter);
144 145
#endif
    assert(filter_params.taps <= MAX_FILTER_TAP);
Yaowu Xu's avatar
Yaowu Xu committed
146 147
    av1_convolve_vert(src, src_stride, dst, dst_stride, w, h, filter_params,
                      subpel_y_q4, y_step_q4, ref_idx);
Angie Chiang's avatar
Angie Chiang committed
148
  } else {
149 150 151
    // temp's size is set to (maximum possible intermediate height or width) *
    // MAX_SB_SIZE
    uint8_t temp[((((MAX_SB_SIZE - 1) * MAX_STEP + 15) >> SUBPEL_BITS) +
152
                  MAX_FILTER_TAP) *
153 154 155
                 MAX_SB_SIZE];
    int filter_size;
    InterpFilterParams filter_params;
156
#if CONFIG_DUAL_FILTER
157
    InterpFilterParams filter_params_x =
Yaowu Xu's avatar
Yaowu Xu committed
158
        av1_get_interp_filter_params(interp_filter[1 + 2 * ref_idx]);
159
    InterpFilterParams filter_params_y =
Yaowu Xu's avatar
Yaowu Xu committed
160
        av1_get_interp_filter_params(interp_filter[0 + 2 * ref_idx]);
161 162 163 164 165 166
    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);
    }
167
#endif
168

169 170 171 172 173 174 175 176 177
#if CONFIG_DUAL_FILTER
    // we do filter with fewer taps first to reduce hardware implementation
    // complexity
    if (filter_params_y.taps < filter_params_x.taps) {
      int intermediate_width;
      int temp_stride;
#if CONFIG_DUAL_FILTER
      filter_params = filter_params_y;
      filter_size = filter_params_x.taps;
178
#else
179 180
      filter_params = av1_get_interp_filter_params(interp_filter);
      filter_size = filter_params.taps;
181
#endif
182 183 184
      intermediate_width =
          (((w - 1) * x_step_q4 + subpel_x_q4) >> SUBPEL_BITS) + filter_size;
      temp_stride = intermediate_width;
Angie Chiang's avatar
Angie Chiang committed
185

186
      assert(filter_params.taps <= MAX_FILTER_TAP);
187

188 189 190
      av1_convolve_vert(src - (filter_size / 2 - 1), src_stride, temp,
                        temp_stride, intermediate_width, h, filter_params,
                        subpel_y_q4, y_step_q4, 0);
191 192

#if CONFIG_DUAL_FILTER
193
      filter_params = filter_params_x;
194
#else
195
      filter_params = av1_get_interp_filter_params(interp_filter);
196
#endif
197
      assert(filter_params.taps <= MAX_FILTER_TAP);
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
      av1_convolve_horiz(temp + (filter_size / 2 - 1), temp_stride, dst,
                         dst_stride, w, h, filter_params, subpel_x_q4,
                         x_step_q4, ref_idx);
    } else
#endif
    {
      int intermediate_height;
      int temp_stride = w;
#if CONFIG_DUAL_FILTER
      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;
#endif
      intermediate_height =
          (((h - 1) * y_step_q4 + subpel_y_q4) >> SUBPEL_BITS) + filter_size;

      assert(filter_params.taps <= MAX_FILTER_TAP);

      av1_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, 0);

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

      av1_convolve_vert(temp + temp_stride * (filter_size / 2 - 1), temp_stride,
                        dst, dst_stride, w, h, filter_params, subpel_y_q4,
                        y_step_q4, ref_idx);
    }
Angie Chiang's avatar
Angie Chiang committed
234
  }
235 236
}

Angie Chiang's avatar
Angie Chiang committed
237 238 239 240 241
void av1_convolve_init_c(void) {
  // A placeholder for SIMD initialization
  return;
}

Yaowu Xu's avatar
Yaowu Xu committed
242 243 244 245 246 247
#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) {
248
  int x, y;
249
  int filter_size = filter_params.taps;
250 251 252 253 254
  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
255
      const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
256
          filter_params, x_q4 & SUBPEL_MASK);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
      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
273 274 275 276 277
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) {
278
  int x, y;
279
  int filter_size = filter_params.taps;
280 281 282 283 284 285
  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
286
      const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
clang-format's avatar
clang-format committed
287
          filter_params, y_q4 & SUBPEL_MASK);
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
      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
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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;
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
329 330
void av1_highbd_convolve(const uint8_t *src8, int src_stride, uint8_t *dst8,
                         int dst_stride, int w, int h,
331
#if CONFIG_DUAL_FILTER
James Zern's avatar
James Zern committed
332
                         const InterpFilter *interp_filter,
333
#else
James Zern's avatar
James Zern committed
334
                         const InterpFilter interp_filter,
335
#endif
Yaowu Xu's avatar
Yaowu Xu committed
336 337 338
                         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
339 340 341 342
  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;
343 344 345 346 347 348

  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
349
  if (ignore_horiz && ignore_vert) {
350
    highbd_convolve_copy(src, src_stride, dst, dst_stride, w, h, ref_idx, bd);
Angie Chiang's avatar
Angie Chiang committed
351
  } else if (ignore_vert) {
352 353
#if CONFIG_DUAL_FILTER
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
354
        av1_get_interp_filter_params(interp_filter[1 + 2 * ref_idx]);
355 356
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
357
        av1_get_interp_filter_params(interp_filter);
358
#endif
Yaowu Xu's avatar
Yaowu Xu committed
359 360 361
    av1_highbd_convolve_horiz(src, src_stride, dst, dst_stride, w, h,
                              filter_params, subpel_x_q4, x_step_q4, ref_idx,
                              bd);
Angie Chiang's avatar
Angie Chiang committed
362
  } else if (ignore_horiz) {
363 364
#if CONFIG_DUAL_FILTER
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
365
        av1_get_interp_filter_params(interp_filter[0 + 2 * ref_idx]);
366 367
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
368
        av1_get_interp_filter_params(interp_filter);
369
#endif
Yaowu Xu's avatar
Yaowu Xu committed
370 371 372
    av1_highbd_convolve_vert(src, src_stride, dst, dst_stride, w, h,
                             filter_params, subpel_y_q4, y_step_q4, ref_idx,
                             bd);
Angie Chiang's avatar
Angie Chiang committed
373 374 375 376 377 378 379 380
  } else {
    // temp's size is set to (maximum possible intermediate_height) *
    // MAX_BLOCK_WIDTH
    uint16_t temp[((((MAX_BLOCK_HEIGHT - 1) * MAX_STEP + 15) >> SUBPEL_BITS) +
                   MAX_FILTER_TAP) *
                  MAX_BLOCK_WIDTH];
    int temp_stride = MAX_BLOCK_WIDTH;

381
#if CONFIG_DUAL_FILTER
382
    InterpFilterParams filter_params_x =
Yaowu Xu's avatar
Yaowu Xu committed
383
        av1_get_interp_filter_params(interp_filter[1 + 2 * ref_idx]);
384
    InterpFilterParams filter_params_y =
Yaowu Xu's avatar
Yaowu Xu committed
385
        av1_get_interp_filter_params(interp_filter[0 + 2 * ref_idx]);
386 387
    InterpFilterParams filter_params = filter_params_x;
    int filter_size = filter_params_y.taps;
388 389
#else
    InterpFilterParams filter_params =
Yaowu Xu's avatar
Yaowu Xu committed
390
        av1_get_interp_filter_params(interp_filter);
391
    int filter_size = filter_params.taps;
392
#endif
393

Angie Chiang's avatar
Angie Chiang committed
394 395 396
    int intermediate_height =
        (((h - 1) * y_step_q4 + subpel_y_q4) >> SUBPEL_BITS) + filter_size;

Yaowu Xu's avatar
Yaowu Xu committed
397
    av1_highbd_convolve_horiz(
clang-format's avatar
clang-format committed
398 399
        src - src_stride * (filter_size / 2 - 1), src_stride, temp, temp_stride,
        w, intermediate_height, filter_params, subpel_x_q4, x_step_q4, 0, bd);
400 401

#if CONFIG_DUAL_FILTER
402
    filter_params = filter_params_y;
403 404 405 406
#endif
    filter_size = filter_params.taps;
    assert(filter_params.taps <= MAX_FILTER_TAP);

Yaowu Xu's avatar
Yaowu Xu committed
407 408 409
    av1_highbd_convolve_vert(temp + temp_stride * (filter_size / 2 - 1),
                             temp_stride, dst, dst_stride, w, h, filter_params,
                             subpel_y_q4, y_step_q4, ref_idx, bd);
Angie Chiang's avatar
Angie Chiang committed
410
  }
411
}
Yaowu Xu's avatar
Yaowu Xu committed
412
#endif  // CONFIG_AOM_HIGHBITDEPTH