/*
 * 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.
 */

#include <string.h>
#include <tuple>

#include "gtest/gtest.h"

#include "config/aom_config.h"
#include "config/aom_dsp_rtcd.h"

#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/aom_filter.h"
#include "aom_mem/aom_mem.h"
#include "aom_ports/aom_timer.h"
#include "aom_ports/mem.h"
#include "av1/common/filter.h"
#include "test/acm_random.h"
#include "test/register_state_check.h"
#include "test/util.h"

namespace {

static const unsigned int kMaxDimension = MAX_SB_SIZE;
static const int kDataAlignment = 16;
static const int kOuterBlockSize = 4 * kMaxDimension;
static const int kInputStride = kOuterBlockSize;
static const int kOutputStride = kOuterBlockSize;
static const int kInputBufferSize = kOuterBlockSize * kOuterBlockSize;
static const int kOutputBufferSize = kOuterBlockSize * kOuterBlockSize;
static const int16_t kInvalidFilter[8] = {};
static const int kNumFilterBanks = SWITCHABLE_FILTERS;
static const int kNumFilters = 16;

using ConvolveFunc = void (*)(const uint8_t *src, ptrdiff_t src_stride,
                              uint8_t *dst, ptrdiff_t dst_stride,
                              const int16_t *filter_x, int filter_x_stride,
                              const int16_t *filter_y, int filter_y_stride,
                              int w, int h);

struct ConvolveFunctions {
  ConvolveFunctions(ConvolveFunc h8, ConvolveFunc v8, int bd)
      : h8_(h8), v8_(v8), use_highbd_(bd) {}

  ConvolveFunc h8_;
  ConvolveFunc v8_;
  int use_highbd_;  // 0 if high bitdepth not used, else the actual bit depth.
};

using ConvolveParam = std::tuple<int, int, const ConvolveFunctions *>;

#define ALL_SIZES_64(convolve_fn)                                         \
  make_tuple(4, 4, &convolve_fn), make_tuple(8, 4, &convolve_fn),         \
      make_tuple(4, 8, &convolve_fn), make_tuple(8, 8, &convolve_fn),     \
      make_tuple(16, 8, &convolve_fn), make_tuple(8, 16, &convolve_fn),   \
      make_tuple(16, 16, &convolve_fn), make_tuple(32, 16, &convolve_fn), \
      make_tuple(16, 32, &convolve_fn), make_tuple(32, 32, &convolve_fn), \
      make_tuple(64, 32, &convolve_fn), make_tuple(32, 64, &convolve_fn), \
      make_tuple(64, 64, &convolve_fn)

#define ALL_SIZES(convolve_fn)                                          \
  make_tuple(128, 64, &convolve_fn), make_tuple(64, 128, &convolve_fn), \
      make_tuple(128, 128, &convolve_fn), ALL_SIZES_64(convolve_fn)

// Reference 8-tap subpixel filter, slightly modified to fit into this test.
#define AV1_FILTER_WEIGHT 128
#define AV1_FILTER_SHIFT 7
uint8_t clip_pixel(int x) { return x < 0 ? 0 : x > 255 ? 255 : x; }

void filter_block2d_8_c(const uint8_t *src_ptr, unsigned int src_stride,
                        const int16_t *HFilter, const int16_t *VFilter,
                        uint8_t *dst_ptr, unsigned int dst_stride,
                        unsigned int output_width, unsigned int output_height) {
  // Between passes, we use an intermediate buffer whose height is extended to
  // have enough horizontally filtered values as input for the vertical pass.
  // This buffer is allocated to be big enough for the largest block type we
  // support.
  const int kInterp_Extend = 4;
  const unsigned int intermediate_height =
      (kInterp_Extend - 1) + output_height + kInterp_Extend;
  unsigned int i, j;

  assert(intermediate_height > 7);

  // Size of intermediate_buffer is max_intermediate_height * filter_max_width,
  // where max_intermediate_height = (kInterp_Extend - 1) + filter_max_height
  //                                 + kInterp_Extend
  //                               = 3 + 16 + 4
  //                               = 23
  // and filter_max_width          = 16
  //
  uint8_t intermediate_buffer[(kMaxDimension + 8) * kMaxDimension];
  const int intermediate_next_stride =
      1 - static_cast<int>(intermediate_height * output_width);

  // Horizontal pass (src -> transposed intermediate).
  uint8_t *output_ptr = intermediate_buffer;
  const int src_next_row_stride = src_stride - output_width;
  src_ptr -= (kInterp_Extend - 1) * src_stride + (kInterp_Extend - 1);
  for (i = 0; i < intermediate_height; ++i) {
    for (j = 0; j < output_width; ++j) {
      // Apply filter...
      const int temp = (src_ptr[0] * HFilter[0]) + (src_ptr[1] * HFilter[1]) +
                       (src_ptr[2] * HFilter[2]) + (src_ptr[3] * HFilter[3]) +
                       (src_ptr[4] * HFilter[4]) + (src_ptr[5] * HFilter[5]) +
                       (src_ptr[6] * HFilter[6]) + (src_ptr[7] * HFilter[7]) +
                       (AV1_FILTER_WEIGHT >> 1);  // Rounding

      // Normalize back to 0-255...
      *output_ptr = clip_pixel(temp >> AV1_FILTER_SHIFT);
      ++src_ptr;
      output_ptr += intermediate_height;
    }
    src_ptr += src_next_row_stride;
    output_ptr += intermediate_next_stride;
  }

  // Vertical pass (transposed intermediate -> dst).
  src_ptr = intermediate_buffer;
  const int dst_next_row_stride = dst_stride - output_width;
  for (i = 0; i < output_height; ++i) {
    for (j = 0; j < output_width; ++j) {
      // Apply filter...
      const int temp = (src_ptr[0] * VFilter[0]) + (src_ptr[1] * VFilter[1]) +
                       (src_ptr[2] * VFilter[2]) + (src_ptr[3] * VFilter[3]) +
                       (src_ptr[4] * VFilter[4]) + (src_ptr[5] * VFilter[5]) +
                       (src_ptr[6] * VFilter[6]) + (src_ptr[7] * VFilter[7]) +
                       (AV1_FILTER_WEIGHT >> 1);  // Rounding

      // Normalize back to 0-255...
      *dst_ptr++ = clip_pixel(temp >> AV1_FILTER_SHIFT);
      src_ptr += intermediate_height;
    }
    src_ptr += intermediate_next_stride;
    dst_ptr += dst_next_row_stride;
  }
}

void block2d_average_c(uint8_t *src, unsigned int src_stride,
                       uint8_t *output_ptr, unsigned int output_stride,
                       unsigned int output_width, unsigned int output_height) {
  unsigned int i, j;
  for (i = 0; i < output_height; ++i) {
    for (j = 0; j < output_width; ++j) {
      output_ptr[j] = (output_ptr[j] + src[i * src_stride + j] + 1) >> 1;
    }
    output_ptr += output_stride;
  }
}

void filter_average_block2d_8_c(const uint8_t *src_ptr,
                                const unsigned int src_stride,
                                const int16_t *HFilter, const int16_t *VFilter,
                                uint8_t *dst_ptr, unsigned int dst_stride,
                                unsigned int output_width,
                                unsigned int output_height) {
  uint8_t tmp[kMaxDimension * kMaxDimension];

  assert(output_width <= kMaxDimension);
  assert(output_height <= kMaxDimension);
  filter_block2d_8_c(src_ptr, src_stride, HFilter, VFilter, tmp, kMaxDimension,
                     output_width, output_height);
  block2d_average_c(tmp, kMaxDimension, dst_ptr, dst_stride, output_width,
                    output_height);
}

void highbd_filter_block2d_8_c(const uint16_t *src_ptr,
                               const unsigned int src_stride,
                               const int16_t *HFilter, const int16_t *VFilter,
                               uint16_t *dst_ptr, unsigned int dst_stride,
                               unsigned int output_width,
                               unsigned int output_height, int bd) {
  // Between passes, we use an intermediate buffer whose height is extended to
  // have enough horizontally filtered values as input for the vertical pass.
  // This buffer is allocated to be big enough for the largest block type we
  // support.
  const int kInterp_Extend = 4;
  const unsigned int intermediate_height =
      (kInterp_Extend - 1) + output_height + kInterp_Extend;

  /* Size of intermediate_buffer is max_intermediate_height * filter_max_width,
   * where max_intermediate_height = (kInterp_Extend - 1) + filter_max_height
   *                                 + kInterp_Extend
   *                               = 3 + 16 + 4
   *                               = 23
   * and filter_max_width = 16
   */
  uint16_t intermediate_buffer[(kMaxDimension + 8) * kMaxDimension] = { 0 };
  const int intermediate_next_stride =
      1 - static_cast<int>(intermediate_height * output_width);

  // Horizontal pass (src -> transposed intermediate).
  {
    uint16_t *output_ptr = intermediate_buffer;
    const int src_next_row_stride = src_stride - output_width;
    unsigned int i, j;
    src_ptr -= (kInterp_Extend - 1) * src_stride + (kInterp_Extend - 1);
    for (i = 0; i < intermediate_height; ++i) {
      for (j = 0; j < output_width; ++j) {
        // Apply filter...
        const int temp = (src_ptr[0] * HFilter[0]) + (src_ptr[1] * HFilter[1]) +
                         (src_ptr[2] * HFilter[2]) + (src_ptr[3] * HFilter[3]) +
                         (src_ptr[4] * HFilter[4]) + (src_ptr[5] * HFilter[5]) +
                         (src_ptr[6] * HFilter[6]) + (src_ptr[7] * HFilter[7]) +
                         (AV1_FILTER_WEIGHT >> 1);  // Rounding

        // Normalize back to 0-255...
        *output_ptr = clip_pixel_highbd(temp >> AV1_FILTER_SHIFT, bd);
        ++src_ptr;
        output_ptr += intermediate_height;
      }
      src_ptr += src_next_row_stride;
      output_ptr += intermediate_next_stride;
    }
  }

  // Vertical pass (transposed intermediate -> dst).
  {
    const uint16_t *interm_ptr = intermediate_buffer;
    const int dst_next_row_stride = dst_stride - output_width;
    unsigned int i, j;
    for (i = 0; i < output_height; ++i) {
      for (j = 0; j < output_width; ++j) {
        // Apply filter...
        const int temp =
            (interm_ptr[0] * VFilter[0]) + (interm_ptr[1] * VFilter[1]) +
            (interm_ptr[2] * VFilter[2]) + (interm_ptr[3] * VFilter[3]) +
            (interm_ptr[4] * VFilter[4]) + (interm_ptr[5] * VFilter[5]) +
            (interm_ptr[6] * VFilter[6]) + (interm_ptr[7] * VFilter[7]) +
            (AV1_FILTER_WEIGHT >> 1);  // Rounding

        // Normalize back to 0-255...
        *dst_ptr++ = clip_pixel_highbd(temp >> AV1_FILTER_SHIFT, bd);
        interm_ptr += intermediate_height;
      }
      interm_ptr += intermediate_next_stride;
      dst_ptr += dst_next_row_stride;
    }
  }
}

void highbd_block2d_average_c(uint16_t *src, unsigned int src_stride,
                              uint16_t *output_ptr, unsigned int output_stride,
                              unsigned int output_width,
                              unsigned int output_height) {
  unsigned int i, j;
  for (i = 0; i < output_height; ++i) {
    for (j = 0; j < output_width; ++j) {
      output_ptr[j] = (output_ptr[j] + src[i * src_stride + j] + 1) >> 1;
    }
    output_ptr += output_stride;
  }
}

void highbd_filter_average_block2d_8_c(
    const uint16_t *src_ptr, unsigned int src_stride, const int16_t *HFilter,
    const int16_t *VFilter, uint16_t *dst_ptr, unsigned int dst_stride,
    unsigned int output_width, unsigned int output_height, int bd) {
  uint16_t tmp[kMaxDimension * kMaxDimension];

  assert(output_width <= kMaxDimension);
  assert(output_height <= kMaxDimension);
  highbd_filter_block2d_8_c(src_ptr, src_stride, HFilter, VFilter, tmp,
                            kMaxDimension, output_width, output_height, bd);
  highbd_block2d_average_c(tmp, kMaxDimension, dst_ptr, dst_stride,
                           output_width, output_height);
}

class ConvolveTestBase : public ::testing::TestWithParam<ConvolveParam> {
 public:
  static void SetUpTestSuite() {
    // Force input_ to be unaligned, output to be 16 byte aligned.
    input_ = reinterpret_cast<uint8_t *>(
                 aom_memalign(kDataAlignment, kInputBufferSize + 1)) +
             1;
    ASSERT_NE(input_, nullptr);
    ref8_ = reinterpret_cast<uint8_t *>(
        aom_memalign(kDataAlignment, kOutputStride * kMaxDimension));
    ASSERT_NE(ref8_, nullptr);
    output_ = reinterpret_cast<uint8_t *>(
        aom_memalign(kDataAlignment, kOutputBufferSize));
    ASSERT_NE(output_, nullptr);
    output_ref_ = reinterpret_cast<uint8_t *>(
        aom_memalign(kDataAlignment, kOutputBufferSize));
    ASSERT_NE(output_ref_, nullptr);
    input16_ = reinterpret_cast<uint16_t *>(aom_memalign(
                   kDataAlignment, (kInputBufferSize + 1) * sizeof(uint16_t))) +
               1;
    ASSERT_NE(input16_, nullptr);
    ref16_ = reinterpret_cast<uint16_t *>(aom_memalign(
        kDataAlignment, kOutputStride * kMaxDimension * sizeof(uint16_t)));
    ASSERT_NE(ref16_, nullptr);
    output16_ = reinterpret_cast<uint16_t *>(
        aom_memalign(kDataAlignment, (kOutputBufferSize) * sizeof(uint16_t)));
    ASSERT_NE(output16_, nullptr);
    output16_ref_ = reinterpret_cast<uint16_t *>(
        aom_memalign(kDataAlignment, (kOutputBufferSize) * sizeof(uint16_t)));
    ASSERT_NE(output16_ref_, nullptr);
  }

  static void TearDownTestSuite() {
    aom_free(input_ - 1);
    input_ = nullptr;
    aom_free(ref8_);
    ref8_ = nullptr;
    aom_free(output_);
    output_ = nullptr;
    aom_free(output_ref_);
    output_ref_ = nullptr;
    aom_free(input16_ - 1);
    input16_ = nullptr;
    aom_free(ref16_);
    ref16_ = nullptr;
    aom_free(output16_);
    output16_ = nullptr;
    aom_free(output16_ref_);
    output16_ref_ = nullptr;
  }

 protected:
  int Width() const { return GET_PARAM(0); }
  int Height() const { return GET_PARAM(1); }
  int BorderLeft() const {
    const int center = (kOuterBlockSize - Width()) / 2;
    return (center + (kDataAlignment - 1)) & ~(kDataAlignment - 1);
  }
  int BorderTop() const { return (kOuterBlockSize - Height()) / 2; }

  bool IsIndexInBorder(int i) {
    return (i < BorderTop() * kOuterBlockSize ||
            i >= (BorderTop() + Height()) * kOuterBlockSize ||
            i % kOuterBlockSize < BorderLeft() ||
            i % kOuterBlockSize >= (BorderLeft() + Width()));
  }

  void SetUp() override {
    UUT_ = GET_PARAM(2);
    if (UUT_->use_highbd_ != 0)
      mask_ = (1 << UUT_->use_highbd_) - 1;
    else
      mask_ = 255;
    /* Set up guard blocks for an inner block centered in the outer block */
    for (int i = 0; i < kOutputBufferSize; ++i) {
      if (IsIndexInBorder(i)) {
        output_[i] = 255;
        output16_[i] = mask_;
      } else {
        output_[i] = 0;
        output16_[i] = 0;
      }
    }

    ::libaom_test::ACMRandom prng;
    for (int i = 0; i < kInputBufferSize; ++i) {
      if (i & 1) {
        input_[i] = 255;
        input16_[i] = mask_;
      } else {
        input_[i] = prng.Rand8Extremes();
        input16_[i] = prng.Rand16() & mask_;
      }
    }
  }

  void SetConstantInput(int value) {
    memset(input_, value, kInputBufferSize);
    aom_memset16(input16_, value, kInputBufferSize);
  }

  void CopyOutputToRef() {
    memcpy(output_ref_, output_, kOutputBufferSize);
    // Copy 16-bit pixels values. The effective number of bytes is double.
    memcpy(output16_ref_, output16_, sizeof(output16_[0]) * kOutputBufferSize);
  }

  void CheckGuardBlocks() {
    for (int i = 0; i < kOutputBufferSize; ++i) {
      if (IsIndexInBorder(i)) {
        EXPECT_EQ(255, output_[i]);
      }
    }
  }

  uint8_t *input() const {
    const int offset = BorderTop() * kOuterBlockSize + BorderLeft();
    if (UUT_->use_highbd_ == 0) {
      return input_ + offset;
    } else {
      return CONVERT_TO_BYTEPTR(input16_) + offset;
    }
  }

  uint8_t *output() const {
    const int offset = BorderTop() * kOuterBlockSize + BorderLeft();
    if (UUT_->use_highbd_ == 0) {
      return output_ + offset;
    } else {
      return CONVERT_TO_BYTEPTR(output16_) + offset;
    }
  }

  uint8_t *output_ref() const {
    const int offset = BorderTop() * kOuterBlockSize + BorderLeft();
    if (UUT_->use_highbd_ == 0) {
      return output_ref_ + offset;
    } else {
      return CONVERT_TO_BYTEPTR(output16_ref_) + offset;
    }
  }

  uint16_t lookup(uint8_t *list, int index) const {
    if (UUT_->use_highbd_ == 0) {
      return list[index];
    } else {
      return CONVERT_TO_SHORTPTR(list)[index];
    }
  }

  void assign_val(uint8_t *list, int index, uint16_t val) const {
    if (UUT_->use_highbd_ == 0) {
      list[index] = (uint8_t)val;
    } else {
      CONVERT_TO_SHORTPTR(list)[index] = val;
    }
  }

  void wrapper_filter_average_block2d_8_c(
      const uint8_t *src_ptr, unsigned int src_stride, const int16_t *HFilter,
      const int16_t *VFilter, uint8_t *dst_ptr, unsigned int dst_stride,
      unsigned int output_width, unsigned int output_height) {
    if (UUT_->use_highbd_ == 0) {
      filter_average_block2d_8_c(src_ptr, src_stride, HFilter, VFilter, dst_ptr,
                                 dst_stride, output_width, output_height);
    } else {
      highbd_filter_average_block2d_8_c(
          CONVERT_TO_SHORTPTR(src_ptr), src_stride, HFilter, VFilter,
          CONVERT_TO_SHORTPTR(dst_ptr), dst_stride, output_width, output_height,
          UUT_->use_highbd_);
    }
  }

  void wrapper_filter_block2d_8_c(
      const uint8_t *src_ptr, unsigned int src_stride, const int16_t *HFilter,
      const int16_t *VFilter, uint8_t *dst_ptr, unsigned int dst_stride,
      unsigned int output_width, unsigned int output_height) {
    if (UUT_->use_highbd_ == 0) {
      filter_block2d_8_c(src_ptr, src_stride, HFilter, VFilter, dst_ptr,
                         dst_stride, output_width, output_height);
    } else {
      highbd_filter_block2d_8_c(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
                                HFilter, VFilter, CONVERT_TO_SHORTPTR(dst_ptr),
                                dst_stride, output_width, output_height,
                                UUT_->use_highbd_);
    }
  }

  void MatchesReferenceSubpixelFilter() {
    uint8_t *const in = input();
    uint8_t *const out = output();
    uint8_t *ref;
    if (UUT_->use_highbd_ == 0) {
      ref = ref8_;
    } else {
      ref = CONVERT_TO_BYTEPTR(ref16_);
    }
    int subpel_search;
    for (subpel_search = USE_2_TAPS; subpel_search <= USE_8_TAPS;
         ++subpel_search) {
      for (int filter_bank = 0; filter_bank < kNumFilterBanks; ++filter_bank) {
        const InterpFilter filter = (InterpFilter)filter_bank;
        const InterpKernel *filters =
            (const InterpKernel *)av1_get_interp_filter_kernel(filter,
                                                               subpel_search);
        for (int filter_x = 0; filter_x < kNumFilters; ++filter_x) {
          for (int filter_y = 0; filter_y < kNumFilters; ++filter_y) {
            wrapper_filter_block2d_8_c(in, kInputStride, filters[filter_x],
                                       filters[filter_y], ref, kOutputStride,
                                       Width(), Height());

            if (filter_x && filter_y)
              continue;
            else if (filter_y)
              UUT_->v8_(in, kInputStride, out, kOutputStride, kInvalidFilter,
                        16, filters[filter_y], 16, Width(), Height());
            else if (filter_x)
              API_REGISTER_STATE_CHECK(UUT_->h8_(
                  in, kInputStride, out, kOutputStride, filters[filter_x], 16,
                  kInvalidFilter, 16, Width(), Height()));
            else
              continue;

            CheckGuardBlocks();

            for (int y = 0; y < Height(); ++y)
              for (int x = 0; x < Width(); ++x)
                ASSERT_EQ(lookup(ref, y * kOutputStride + x),
                          lookup(out, y * kOutputStride + x))
                    << "mismatch at (" << x << "," << y << "), "
                    << "filters (" << filter_bank << "," << filter_x << ","
                    << filter_y << ")";
          }
        }
      }
    }
  }

  void FilterExtremes() {
    uint8_t *const in = input();
    uint8_t *const out = output();
    uint8_t *ref;
    if (UUT_->use_highbd_ == 0) {
      ref = ref8_;
    } else {
      ref = CONVERT_TO_BYTEPTR(ref16_);
    }

    // Populate ref and out with some random data
    ::libaom_test::ACMRandom prng;
    for (int y = 0; y < Height(); ++y) {
      for (int x = 0; x < Width(); ++x) {
        uint16_t r;
        if (UUT_->use_highbd_ == 0 || UUT_->use_highbd_ == 8) {
          r = prng.Rand8Extremes();
        } else {
          r = prng.Rand16() & mask_;
        }
        assign_val(out, y * kOutputStride + x, r);
        assign_val(ref, y * kOutputStride + x, r);
      }
    }

    for (int axis = 0; axis < 2; axis++) {
      int seed_val = 0;
      while (seed_val < 256) {
        for (int y = 0; y < 8; ++y) {
          for (int x = 0; x < 8; ++x) {
            assign_val(in, y * kOutputStride + x - SUBPEL_TAPS / 2 + 1,
                       ((seed_val >> (axis ? y : x)) & 1) * mask_);
            if (axis) seed_val++;
          }
          if (axis)
            seed_val -= 8;
          else
            seed_val++;
        }
        if (axis) seed_val += 8;
        int subpel_search;
        for (subpel_search = USE_2_TAPS; subpel_search <= USE_8_TAPS;
             ++subpel_search) {
          for (int filter_bank = 0; filter_bank < kNumFilterBanks;
               ++filter_bank) {
            const InterpFilter filter = (InterpFilter)filter_bank;
            const InterpKernel *filters =
                (const InterpKernel *)av1_get_interp_filter_kernel(
                    filter, subpel_search);
            for (int filter_x = 0; filter_x < kNumFilters; ++filter_x) {
              for (int filter_y = 0; filter_y < kNumFilters; ++filter_y) {
                wrapper_filter_block2d_8_c(in, kInputStride, filters[filter_x],
                                           filters[filter_y], ref,
                                           kOutputStride, Width(), Height());
                if (filter_x && filter_y)
                  continue;
                else if (filter_y)
                  API_REGISTER_STATE_CHECK(UUT_->v8_(
                      in, kInputStride, out, kOutputStride, kInvalidFilter, 16,
                      filters[filter_y], 16, Width(), Height()));
                else if (filter_x)
                  API_REGISTER_STATE_CHECK(UUT_->h8_(
                      in, kInputStride, out, kOutputStride, filters[filter_x],
                      16, kInvalidFilter, 16, Width(), Height()));
                else
                  continue;

                for (int y = 0; y < Height(); ++y)
                  for (int x = 0; x < Width(); ++x)
                    ASSERT_EQ(lookup(ref, y * kOutputStride + x),
                              lookup(out, y * kOutputStride + x))
                        << "mismatch at (" << x << "," << y << "), "
                        << "filters (" << filter_bank << "," << filter_x << ","
                        << filter_y << ")";
              }
            }
          }
        }
      }
    }
  }

  void SpeedTest() {
    uint8_t *const in = input();
    uint8_t *const out = output();
    uint8_t *ref;
    if (UUT_->use_highbd_ == 0) {
      ref = ref8_;
    } else {
      ref = CONVERT_TO_BYTEPTR(ref16_);
    }

    // Populate ref and out with some random data
    ::libaom_test::ACMRandom prng;
    for (int y = 0; y < Height(); ++y) {
      for (int x = 0; x < Width(); ++x) {
        uint16_t r;
        if (UUT_->use_highbd_ == 0 || UUT_->use_highbd_ == 8) {
          r = prng.Rand8Extremes();
        } else {
          r = prng.Rand16() & mask_;
        }
        assign_val(out, y * kOutputStride + x, r);
        assign_val(ref, y * kOutputStride + x, r);
      }
    }

    InterpFilter filter = (InterpFilter)1;
    const InterpKernel *filters =
        (const InterpKernel *)av1_get_interp_filter_kernel(filter, USE_8_TAPS);
    wrapper_filter_average_block2d_8_c(in, kInputStride, filters[1], filters[1],
                                       out, kOutputStride, Width(), Height());

    aom_usec_timer timer;
    int tests_num = 1000;

    aom_usec_timer_start(&timer);
    while (tests_num > 0) {
      for (int filter_bank = 0; filter_bank < kNumFilterBanks; ++filter_bank) {
        filter = (InterpFilter)filter_bank;
        filters = (const InterpKernel *)av1_get_interp_filter_kernel(
            filter, USE_8_TAPS);
        for (int filter_x = 0; filter_x < kNumFilters; ++filter_x) {
          for (int filter_y = 0; filter_y < kNumFilters; ++filter_y) {
            if (filter_x && filter_y) continue;
            if (filter_y)
              API_REGISTER_STATE_CHECK(UUT_->v8_(
                  in, kInputStride, out, kOutputStride, kInvalidFilter, 16,
                  filters[filter_y], 16, Width(), Height()));
            else if (filter_x)
              API_REGISTER_STATE_CHECK(UUT_->h8_(
                  in, kInputStride, out, kOutputStride, filters[filter_x], 16,
                  kInvalidFilter, 16, Width(), Height()));
          }
        }
      }
      tests_num--;
    }
    aom_usec_timer_mark(&timer);

    const int elapsed_time =
        static_cast<int>(aom_usec_timer_elapsed(&timer) / 1000);
    printf("%dx%d (bitdepth %d) time: %5d ms\n", Width(), Height(),
           UUT_->use_highbd_, elapsed_time);
  }

  const ConvolveFunctions *UUT_;
  static uint8_t *input_;
  static uint8_t *ref8_;
  static uint8_t *output_;
  static uint8_t *output_ref_;
  static uint16_t *input16_;
  static uint16_t *ref16_;
  static uint16_t *output16_;
  static uint16_t *output16_ref_;
  int mask_;
};

uint8_t *ConvolveTestBase::input_ = nullptr;
uint8_t *ConvolveTestBase::ref8_ = nullptr;
uint8_t *ConvolveTestBase::output_ = nullptr;
uint8_t *ConvolveTestBase::output_ref_ = nullptr;
uint16_t *ConvolveTestBase::input16_ = nullptr;
uint16_t *ConvolveTestBase::ref16_ = nullptr;
uint16_t *ConvolveTestBase::output16_ = nullptr;
uint16_t *ConvolveTestBase::output16_ref_ = nullptr;

using LowbdConvolveTest = ConvolveTestBase;

TEST_P(LowbdConvolveTest, GuardBlocks) { CheckGuardBlocks(); }

void FiltersWontSaturateWhenAddedPairwise() {
  int subpel_search;
  for (subpel_search = USE_2_TAPS; subpel_search <= USE_8_TAPS;
       ++subpel_search) {
    for (int filter_bank = 0; filter_bank < kNumFilterBanks; ++filter_bank) {
      const InterpFilter filter = (InterpFilter)filter_bank;
      const InterpKernel *filters =
          (const InterpKernel *)av1_get_interp_filter_kernel(filter,
                                                             subpel_search);
      for (int i = 0; i < kNumFilters; i++) {
        const int p0 = filters[i][0] + filters[i][1];
        const int p1 = filters[i][2] + filters[i][3];
        const int p2 = filters[i][4] + filters[i][5];
        const int p3 = filters[i][6] + filters[i][7];
        EXPECT_LE(p0, 128);
        EXPECT_LE(p1, 128);
        EXPECT_LE(p2, 128);
        EXPECT_LE(p3, 128);
        EXPECT_LE(p0 + p3, 128);
        EXPECT_LE(p0 + p3 + p1, 128);
        EXPECT_LE(p0 + p3 + p1 + p2, 128);
        EXPECT_EQ(p0 + p1 + p2 + p3, 128);
      }
    }
  }
}

TEST(LowbdConvolveTest, FiltersWontSaturateWhenAddedPairwise) {
  FiltersWontSaturateWhenAddedPairwise();
}

TEST_P(LowbdConvolveTest, MatchesReferenceSubpixelFilter) {
  MatchesReferenceSubpixelFilter();
}

TEST_P(LowbdConvolveTest, FilterExtremes) { FilterExtremes(); }

TEST_P(LowbdConvolveTest, DISABLED_Speed) { SpeedTest(); }

using std::make_tuple;

// WRAP macro is only used for high bitdepth build.
#if CONFIG_AV1_HIGHBITDEPTH
#define WRAP(func, bd)                                                       \
  static void wrap_##func##_##bd(                                            \
      const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,                \
      ptrdiff_t dst_stride, const int16_t *filter_x, int filter_x_stride,    \
      const int16_t *filter_y, int filter_y_stride, int w, int h) {          \
    aom_highbd_##func(src, src_stride, dst, dst_stride, filter_x,            \
                      filter_x_stride, filter_y, filter_y_stride, w, h, bd); \
  }
#if HAVE_SSE2 && AOM_ARCH_X86_64
WRAP(convolve8_horiz_sse2, 8)
WRAP(convolve8_vert_sse2, 8)
WRAP(convolve8_horiz_sse2, 10)
WRAP(convolve8_vert_sse2, 10)
WRAP(convolve8_horiz_sse2, 12)
WRAP(convolve8_vert_sse2, 12)
#endif  // HAVE_SSE2 && AOM_ARCH_X86_64

WRAP(convolve8_horiz_c, 8)
WRAP(convolve8_vert_c, 8)
WRAP(convolve8_horiz_c, 10)
WRAP(convolve8_vert_c, 10)
WRAP(convolve8_horiz_c, 12)
WRAP(convolve8_vert_c, 12)

#if HAVE_AVX2
WRAP(convolve8_horiz_avx2, 8)
WRAP(convolve8_vert_avx2, 8)

WRAP(convolve8_horiz_avx2, 10)
WRAP(convolve8_vert_avx2, 10)

WRAP(convolve8_horiz_avx2, 12)
WRAP(convolve8_vert_avx2, 12)
#endif  // HAVE_AVX2

#if HAVE_NEON
WRAP(convolve8_horiz_neon, 8)
WRAP(convolve8_vert_neon, 8)

WRAP(convolve8_horiz_neon, 10)
WRAP(convolve8_vert_neon, 10)

WRAP(convolve8_horiz_neon, 12)
WRAP(convolve8_vert_neon, 12)
#endif  // HAVE_NEON

#if HAVE_SVE
WRAP(convolve8_horiz_sve, 8)
WRAP(convolve8_vert_sve, 8)

WRAP(convolve8_horiz_sve, 10)
WRAP(convolve8_vert_sve, 10)

WRAP(convolve8_horiz_sve, 12)
WRAP(convolve8_vert_sve, 12)
#endif  // HAVE_SVE
#endif  // CONFIG_AV1_HIGHBITDEPTH

#undef WRAP

#if CONFIG_AV1_HIGHBITDEPTH

using HighbdConvolveTest = ConvolveTestBase;

TEST_P(HighbdConvolveTest, GuardBlocks) { CheckGuardBlocks(); }

TEST(HighbdConvolveTest, FiltersWontSaturateWhenAddedPairwise) {
  FiltersWontSaturateWhenAddedPairwise();
}

TEST_P(HighbdConvolveTest, MatchesReferenceSubpixelFilter) {
  MatchesReferenceSubpixelFilter();
}

TEST_P(HighbdConvolveTest, FilterExtremes) { FilterExtremes(); }

TEST_P(HighbdConvolveTest, DISABLED_Speed) { SpeedTest(); }

const ConvolveFunctions wrap_convolve8_c(wrap_convolve8_horiz_c_8,
                                         wrap_convolve8_vert_c_8, 8);
const ConvolveFunctions wrap_convolve10_c(wrap_convolve8_horiz_c_10,
                                          wrap_convolve8_vert_c_10, 10);
const ConvolveFunctions wrap_convolve12_c(wrap_convolve8_horiz_c_12,
                                          wrap_convolve8_vert_c_12, 12);
const ConvolveParam kArrayHighbdConvolve_c[] = { ALL_SIZES(wrap_convolve8_c),
                                                 ALL_SIZES(wrap_convolve10_c),
                                                 ALL_SIZES(wrap_convolve12_c) };

INSTANTIATE_TEST_SUITE_P(C, HighbdConvolveTest,
                         ::testing::ValuesIn(kArrayHighbdConvolve_c));
#endif  // CONFIG_AV1_HIGHBITDEPTH

const ConvolveFunctions convolve8_c(aom_convolve8_horiz_c, aom_convolve8_vert_c,
                                    0);
const ConvolveParam kArrayConvolve_c[] = { ALL_SIZES(convolve8_c) };

INSTANTIATE_TEST_SUITE_P(C, LowbdConvolveTest,
                         ::testing::ValuesIn(kArrayConvolve_c));

#if HAVE_SSE2 && AOM_ARCH_X86_64
#if CONFIG_AV1_HIGHBITDEPTH
const ConvolveFunctions wrap_convolve8_sse2(wrap_convolve8_horiz_sse2_8,
                                            wrap_convolve8_vert_sse2_8, 8);
const ConvolveFunctions wrap_convolve10_sse2(wrap_convolve8_horiz_sse2_10,
                                             wrap_convolve8_vert_sse2_10, 10);
const ConvolveFunctions wrap_convolve12_sse2(wrap_convolve8_horiz_sse2_12,
                                             wrap_convolve8_vert_sse2_12, 12);
const ConvolveParam kArrayHighbdConvolve_sse2[] = {
  ALL_SIZES(wrap_convolve8_sse2), ALL_SIZES(wrap_convolve10_sse2),
  ALL_SIZES(wrap_convolve12_sse2)
};

INSTANTIATE_TEST_SUITE_P(SSE2, HighbdConvolveTest,
                         ::testing::ValuesIn(kArrayHighbdConvolve_sse2));
#endif
#endif

#if HAVE_SSSE3
const ConvolveFunctions convolve8_ssse3(aom_convolve8_horiz_ssse3,
                                        aom_convolve8_vert_ssse3, 0);

const ConvolveParam kArrayConvolve8_ssse3[] = { ALL_SIZES(convolve8_ssse3) };

INSTANTIATE_TEST_SUITE_P(SSSE3, LowbdConvolveTest,
                         ::testing::ValuesIn(kArrayConvolve8_ssse3));
#endif

#if HAVE_AVX2
#if CONFIG_AV1_HIGHBITDEPTH
const ConvolveFunctions wrap_convolve8_avx2(wrap_convolve8_horiz_avx2_8,
                                            wrap_convolve8_vert_avx2_8, 8);
const ConvolveFunctions wrap_convolve10_avx2(wrap_convolve8_horiz_avx2_10,
                                             wrap_convolve8_vert_avx2_10, 10);
const ConvolveFunctions wrap_convolve12_avx2(wrap_convolve8_horiz_avx2_12,
                                             wrap_convolve8_vert_avx2_12, 12);
const ConvolveParam kArray_HighbdConvolve8_avx2[] = {
  ALL_SIZES_64(wrap_convolve8_avx2), ALL_SIZES_64(wrap_convolve10_avx2),
  ALL_SIZES_64(wrap_convolve12_avx2)
};

INSTANTIATE_TEST_SUITE_P(AVX2, HighbdConvolveTest,
                         ::testing::ValuesIn(kArray_HighbdConvolve8_avx2));
#endif
const ConvolveFunctions convolve8_avx2(aom_convolve8_horiz_avx2,
                                       aom_convolve8_vert_avx2, 0);
const ConvolveParam kArray_Convolve8_avx2[] = { ALL_SIZES(convolve8_avx2) };

INSTANTIATE_TEST_SUITE_P(AVX2, LowbdConvolveTest,
                         ::testing::ValuesIn(kArray_Convolve8_avx2));
#endif  // HAVE_AVX2

#if HAVE_NEON
#if CONFIG_AV1_HIGHBITDEPTH
const ConvolveFunctions wrap_convolve8_neon(wrap_convolve8_horiz_neon_8,
                                            wrap_convolve8_vert_neon_8, 8);
const ConvolveFunctions wrap_convolve10_neon(wrap_convolve8_horiz_neon_10,
                                             wrap_convolve8_vert_neon_10, 10);
const ConvolveFunctions wrap_convolve12_neon(wrap_convolve8_horiz_neon_12,
                                             wrap_convolve8_vert_neon_12, 12);
const ConvolveParam kArray_HighbdConvolve8_neon[] = {
  ALL_SIZES_64(wrap_convolve8_neon), ALL_SIZES_64(wrap_convolve10_neon),
  ALL_SIZES_64(wrap_convolve12_neon)
};

INSTANTIATE_TEST_SUITE_P(NEON, HighbdConvolveTest,
                         ::testing::ValuesIn(kArray_HighbdConvolve8_neon));
#endif
const ConvolveFunctions convolve8_neon(aom_convolve8_horiz_neon,
                                       aom_convolve8_vert_neon, 0);
const ConvolveParam kArray_Convolve8_neon[] = { ALL_SIZES(convolve8_neon) };

INSTANTIATE_TEST_SUITE_P(NEON, LowbdConvolveTest,
                         ::testing::ValuesIn(kArray_Convolve8_neon));
#endif  // HAVE_NEON

#if HAVE_NEON_DOTPROD
const ConvolveFunctions convolve8_neon_dotprod(aom_convolve8_horiz_neon_dotprod,
                                               aom_convolve8_vert_neon_dotprod,
                                               0);
const ConvolveParam kArray_Convolve8_neon_dotprod[] = { ALL_SIZES(
    convolve8_neon_dotprod) };

INSTANTIATE_TEST_SUITE_P(NEON_DOTPROD, LowbdConvolveTest,
                         ::testing::ValuesIn(kArray_Convolve8_neon_dotprod));
#endif  // HAVE_NEON_DOTPROD

#if HAVE_NEON_I8MM
const ConvolveFunctions convolve8_neon_i8mm(aom_convolve8_horiz_neon_i8mm,
                                            aom_convolve8_vert_neon_i8mm, 0);
const ConvolveParam kArray_Convolve8_neon_i8mm[] = { ALL_SIZES(
    convolve8_neon_i8mm) };

INSTANTIATE_TEST_SUITE_P(NEON_I8MM, LowbdConvolveTest,
                         ::testing::ValuesIn(kArray_Convolve8_neon_i8mm));
#endif  // HAVE_NEON_I8MM

#if HAVE_SVE
#if CONFIG_AV1_HIGHBITDEPTH
const ConvolveFunctions wrap_convolve8_sve(wrap_convolve8_horiz_sve_8,
                                           wrap_convolve8_vert_sve_8, 8);
const ConvolveFunctions wrap_convolve10_sve(wrap_convolve8_horiz_sve_10,
                                            wrap_convolve8_vert_sve_10, 10);
const ConvolveFunctions wrap_convolve12_sve(wrap_convolve8_horiz_sve_12,
                                            wrap_convolve8_vert_sve_12, 12);
const ConvolveParam kArray_HighbdConvolve8_sve[] = {
  ALL_SIZES_64(wrap_convolve8_sve), ALL_SIZES_64(wrap_convolve10_sve),
  ALL_SIZES_64(wrap_convolve12_sve)
};

INSTANTIATE_TEST_SUITE_P(SVE, HighbdConvolveTest,
                         ::testing::ValuesIn(kArray_HighbdConvolve8_sve));
#endif
#endif  // HAVE_SVE

using ConvolveScale2DFunc = void (*)(const uint8_t *src, ptrdiff_t src_stride,
                                     uint8_t *dst, ptrdiff_t dst_stride,
                                     const InterpKernel *filter, int x0_q4,
                                     int x_step_q4, int y0_q4, int y_step_q4,
                                     int w, int h);

using ConvolveScale2DParam = std::tuple<int, int, ConvolveScale2DFunc>;

class ConvolveScale2DTest
    : public ::testing::TestWithParam<ConvolveScale2DParam> {
 public:
  int Width() const { return GET_PARAM(0); }
  int Height() const { return GET_PARAM(1); }
  int BorderLeft() const {
    const int center = (kOuterBlockSize - Width()) / 2;
    return (center + (kDataAlignment - 1)) & ~(kDataAlignment - 1);
  }
  int BorderTop() const { return (kOuterBlockSize - Height()) / 2; }

  bool IsIndexInBorder(int i) {
    return (i < BorderTop() * kOuterBlockSize ||
            i >= (BorderTop() + Height()) * kOuterBlockSize ||
            i % kOuterBlockSize < BorderLeft() ||
            i % kOuterBlockSize >= (BorderLeft() + Width()));
  }

  void SetUp() override {
    // Force input_ to be unaligned, output to be 16 byte aligned.
    input_ = reinterpret_cast<uint8_t *>(
                 aom_memalign(kDataAlignment, kInputBufferSize + 1)) +
             1;
    output_ = reinterpret_cast<uint8_t *>(
        aom_memalign(kDataAlignment, kOutputBufferSize));
    output_ref_ = reinterpret_cast<uint8_t *>(
        aom_memalign(kDataAlignment, kOutputBufferSize));

    ASSERT_NE(input_, nullptr);
    ASSERT_NE(output_, nullptr);
    ASSERT_NE(output_ref_, nullptr);

    test_func_ = GET_PARAM(2);
    /* Set up guard blocks for an inner block centered in the outer block */
    for (int i = 0; i < kOutputBufferSize; ++i) {
      if (IsIndexInBorder(i)) {
        output_[i] = 255;
      } else {
        output_[i] = 0;
      }
    }

    ::libaom_test::ACMRandom prng;
    for (int i = 0; i < kInputBufferSize; ++i) {
      if (i & 1) {
        input_[i] = 255;
      } else {
        input_[i] = prng.Rand8Extremes();
      }
    }
  }

  void TearDown() override {
    aom_free(input_ - 1);
    input_ = nullptr;
    aom_free(output_);
    output_ = nullptr;
    aom_free(output_ref_);
    output_ref_ = nullptr;
  }

  void SetConstantInput(int value) { memset(input_, value, kInputBufferSize); }

  void CopyOutputToRef() { memcpy(output_ref_, output_, kOutputBufferSize); }

  void CheckGuardBlocks() {
    for (int i = 0; i < kOutputBufferSize; ++i) {
      if (IsIndexInBorder(i)) {
        EXPECT_EQ(255, output_[i]);
      }
    }
  }

  uint8_t *input() const {
    const int offset = BorderTop() * kOuterBlockSize + BorderLeft();
    return input_ + offset;
  }

  uint8_t *output() const {
    const int offset = BorderTop() * kOuterBlockSize + BorderLeft();
    return output_ + offset;
  }

  uint8_t *output_ref() const {
    const int offset = BorderTop() * kOuterBlockSize + BorderLeft();
    return output_ref_ + offset;
  }

  uint16_t lookup(uint8_t *list, int index) const { return list[index]; }

  void assign_val(uint8_t *list, int index, uint16_t val) const {
    list[index] = (uint8_t)val;
  }

  ConvolveScale2DFunc test_func_;
  uint8_t *input_;
  uint8_t *output_;
  uint8_t *output_ref_;
};

TEST_P(ConvolveScale2DTest, DISABLED_Speed) {
  const uint8_t *const in = input();
  uint8_t *const out = output();
  const InterpKernel *const filter =
      (const InterpKernel *)av1_get_interp_filter_kernel(EIGHTTAP_REGULAR,
                                                         USE_8_TAPS);
  const int kNumTests = 10000;
  const int width = Width();
  const int height = Height();
  const int frac = 8;
  const int step = 16;
  aom_usec_timer timer;

  aom_usec_timer_start(&timer);
  for (int n = 0; n < kNumTests; ++n) {
    test_func_(in, kInputStride, out, kOutputStride, filter, frac, step, frac,
               step, width, height);
  }
  aom_usec_timer_mark(&timer);

  const int elapsed_time = static_cast<int>(aom_usec_timer_elapsed(&timer));
  printf("convolve_scale_2d_%dx%d_%d: %d us\n", width, height, 8, elapsed_time);
}

TEST_P(ConvolveScale2DTest, Correctness) {
  uint8_t *const in = input();
  uint8_t *const out = output();
  uint8_t ref[kOutputStride * kMaxDimension];

  ::libaom_test::ACMRandom prng;
  for (int y = 0; y < Height(); ++y) {
    for (int x = 0; x < Width(); ++x) {
      const uint16_t r = prng.Rand8Extremes();
      assign_val(in, y * kInputStride + x, r);
    }
  }

  for (int subpel_search = USE_2_TAPS; subpel_search <= USE_8_TAPS;
       ++subpel_search) {
    for (int filter_bank = 0; filter_bank < kNumFilterBanks; ++filter_bank) {
      const InterpFilter filter = static_cast<InterpFilter>(filter_bank);
      const InterpKernel *filters =
          (const InterpKernel *)av1_get_interp_filter_kernel(filter,
                                                             subpel_search);
      for (int frac = 0; frac < 16; ++frac) {
        for (int step = 1; step <= 32; ++step) {
          aom_scaled_2d_c(in, kInputStride, ref, kOutputStride, filters, frac,
                          step, frac, step, Width(), Height());
          API_REGISTER_STATE_CHECK(
              test_func_(in, kInputStride, out, kOutputStride, filters, frac,
                         step, frac, step, Width(), Height()));

          CheckGuardBlocks();

          for (int y = 0; y < Height(); ++y) {
            for (int x = 0; x < Width(); ++x) {
              ASSERT_EQ(lookup(ref, y * kOutputStride + x),
                        lookup(out, y * kOutputStride + x))
                  << "x == " << x << ", y == " << y << ", frac == " << frac
                  << ", step == " << step;
            }
          }
        }
      }
    }
  }
}

INSTANTIATE_TEST_SUITE_P(C, ConvolveScale2DTest,
                         ::testing::Values(ALL_SIZES_64(aom_scaled_2d_c)));

#if HAVE_NEON
INSTANTIATE_TEST_SUITE_P(NEON, ConvolveScale2DTest,
                         ::testing::Values(ALL_SIZES_64(aom_scaled_2d_neon)));
#endif  // HAVE_NEON

#if HAVE_NEON_DOTPROD
INSTANTIATE_TEST_SUITE_P(
    NEON_DOTPROD, ConvolveScale2DTest,
    ::testing::Values(ALL_SIZES_64(aom_scaled_2d_neon_dotprod)));
#endif  // HAVE_NEON_DOTPROD

#if HAVE_NEON_I8MM
INSTANTIATE_TEST_SUITE_P(
    NEON_I8MM, ConvolveScale2DTest,
    ::testing::Values(ALL_SIZES_64(aom_scaled_2d_neon_i8mm)));
#endif  // HAVE_NEON_I8MM

#if HAVE_SSSE3
INSTANTIATE_TEST_SUITE_P(SSSE3, ConvolveScale2DTest,
                         ::testing::Values(ALL_SIZES_64(aom_scaled_2d_ssse3)));
#endif  // HAVE_SSSE3

}  // namespace
