/*
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 3-Clause Clear License
 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
 * License was not distributed with this source code in the LICENSE file, you
 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/.  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
 * aomedia.org/license/patent-license/.
 */

#ifndef AVM_AV2_COMMON_CONVOLVE_H_
#define AVM_AV2_COMMON_CONVOLVE_H_
#include "av2/common/filter.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef uint16_t CONV_BUF_TYPE;
typedef struct ConvolveParams {
  int do_average;
  CONV_BUF_TYPE *dst;
  int dst_stride;
  int round_0;
  int round_1;
  int plane;
  int is_compound;
  int fwd_offset;
  int bck_offset;
} ConvolveParams;

typedef struct WienerConvolveParams {
  int round_0;
  int round_1;
} WienerConvolveParams;

#define NONSEP_ROW_ID 0
#define NONSEP_COL_ID 1
#define NONSEP_BUF_POS 2

static INLINE int16_t clip_base(int16_t x, int bit_depth) {
  (void)bit_depth;
  return x;
}

typedef struct NonsepFilterConfig {
  int prec_bits;
  int num_pixels;
  int num_pixels2;
  const int (*config)[3];
  const int (*config2)[3];
  int strict_bounds;
  int subtract_center;

  // Symmetry can be derived from the config but convenient to have
  // explicitly specified
  int asymmetric;   // number of pixels that are asymmetric in config; the value
                    // is always equal to zero since luma filter is symmetric in
                    // AV2
  int asymmetric2;  // number of pixels that are asymmetric in config2
} NonsepFilterConfig;

static INLINE int config2ncoeffs(const NonsepFilterConfig *config, int *ncoeff,
                                 int *ncoeff2) {
  int nc, nc2;
  int symmetric = (config->num_pixels & ~1) - config->asymmetric;
  int symmetric2 = (config->num_pixels2 & ~1) - config->asymmetric2;
  nc = (config->num_pixels & ~1) - (symmetric >> 1);
  nc2 = (config->num_pixels2 & ~1) - (symmetric2 >> 1);
  if (ncoeff) *ncoeff = nc;
  if (ncoeff2) *ncoeff2 = nc2;
  return (nc + nc2);
}

static INLINE int config2ncoeffs_select(const NonsepFilterConfig *config,
                                        const int *select, int *ncoeff,
                                        int *ncoeff2) {
  int nc, nc2;
  int symmetric = (config->num_pixels & ~1) - config->asymmetric;
  int symmetric2 = (config->num_pixels2 & ~1) - config->asymmetric2;
  nc = (config->num_pixels & ~1) - (symmetric >> 1);
  nc2 = (config->num_pixels2 & ~1) - (symmetric2 >> 1);
  int ncs = 0, ncs2 = 0;
  for (int i = 0; i < nc; ++i)
    if (select[i]) ncs++;
  for (int i = nc; i < nc2; ++i)
    if (select[i]) ncs2++;
  if (ncoeff) *ncoeff = ncs;
  if (ncoeff2) *ncoeff2 = ncs2;
  return (ncs + ncs2);
}

// Nonseparable convolution.
void av2_convolve_nonsep_blk4x4_highbd(const uint16_t *dgd, int width,
                                       int height, int stride,
                                       const NonsepFilterConfig *config,
                                       const int16_t *filter, uint16_t *dst,
                                       int dst_stride, int bit_depth);
void av2_convolve_nonsep_blk8x8_highbd(const uint16_t *dgd, int width,
                                       int height, int stride,
                                       const NonsepFilterConfig *config,
                                       const int16_t *filter, uint16_t *dst,
                                       int dst_stride, int bit_depth);

// Nonseparable convolution with dual input planes - used for cross component
// filtering.
void av2_convolve_nonsep_dual_highbd(const uint16_t *dgd, int width, int height,
                                     int stride, const uint16_t *dgd2,
                                     int stride2,
                                     const NonsepFilterConfig *config,
                                     const int16_t *filter, uint16_t *dst,
                                     int dst_stride, int bit_depth);

#define ROUND0_BITS 3
#define COMPOUND_ROUND1_BITS 7
#define WIENER_ROUND0_BITS 3

#define WIENER_CLAMP_LIMIT(r0, bd) (1 << ((bd) + 1 + FILTER_BITS - r0))

struct AV2Common;
struct scale_factors;

static INLINE int is_uneven_wtd_comp_avg(const ConvolveParams *params) {
  return params->do_average &&
         (params->fwd_offset != (1 << (DIST_PRECISION_BITS - 1)) ||
          params->bck_offset != (1 << (DIST_PRECISION_BITS - 1)));
}

static INLINE void init_conv_params(ConvolveParams *params) {
  memset(params, 0, sizeof(*params));
  params->fwd_offset = 1 << (DIST_PRECISION_BITS - 1);
  params->bck_offset = 1 << (DIST_PRECISION_BITS - 1);
}

static INLINE ConvolveParams get_conv_params_no_round(int cmp_index, int plane,
                                                      CONV_BUF_TYPE *dst,
                                                      int dst_stride,
                                                      int is_compound, int bd) {
  ConvolveParams conv_params;
  assert(IMPLIES(cmp_index, is_compound));

  init_conv_params(&conv_params);
  conv_params.is_compound = is_compound;
  conv_params.round_0 = ROUND0_BITS;
  conv_params.round_1 = is_compound ? COMPOUND_ROUND1_BITS
                                    : 2 * FILTER_BITS - conv_params.round_0;
  const int intbufrange = bd + FILTER_BITS - conv_params.round_0 + 2;
  assert(IMPLIES(bd < 12, intbufrange <= 16));
  if (intbufrange > 16) {
    conv_params.round_0 += intbufrange - 16;
    if (!is_compound) conv_params.round_1 -= intbufrange - 16;
  }
  // TODO(yunqing): The following dst should only be valid while
  // is_compound = 1;
  conv_params.dst = dst;
  conv_params.dst_stride = dst_stride;
  conv_params.plane = plane;

  // By default, set do average to 1 if this is the second single prediction
  // in a compound mode.
  conv_params.do_average = cmp_index;
  return conv_params;
}

static INLINE ConvolveParams get_conv_params(int do_average, int plane,
                                             int bd) {
  return get_conv_params_no_round(do_average, plane, NULL, 0, 0, bd);
}

static INLINE WienerConvolveParams get_conv_params_wiener(int bd) {
  WienerConvolveParams conv_params;
  conv_params.round_0 = WIENER_ROUND0_BITS;
  conv_params.round_1 = 2 * FILTER_BITS - conv_params.round_0;
  const int intbufrange = bd + FILTER_BITS - conv_params.round_0 + 2;
  assert(IMPLIES(bd < 12, intbufrange <= 16));
  if (intbufrange > 16) {
    conv_params.round_0 += intbufrange - 16;
    conv_params.round_1 -= intbufrange - 16;
  }
  return conv_params;
}

void av2_highbd_convolve_2d_facade(const uint16_t *src8, int src_stride,
                                   uint16_t *dst, int dst_stride, int w, int h,
                                   const InterpFilterParams *interp_filters[2],
                                   const int subpel_x_qn, int x_step_q4,
                                   const int subpel_y_qn, int y_step_q4,
                                   int scaled, ConvolveParams *conv_params,
                                   int bd, int is_intrabc);

#ifdef __cplusplus
}  // extern "C"
#endif

// Updates the line buffers holding sums of features that in turn enable
// box-filtering of features. Accomplishes the first step of the update by
// subtracting the contribution of the out-of-scope line.
void prepare_feature_sum_bufs_c(int *feature_sum_buffers[],
                                int16_t *feature_line_buffers[],
                                int feature_length, int buffer_row,
                                int col_begin, int col_end, int buffer_col);

// Updates the line buffers holding sums of features that in turn enable
// box-filtering of features. Accomplishes the second step of the update by
// adding the contribution of the newly in-scope line.
void update_feature_sum_bufs_c(int *feature_sum_buffers[],
                               int16_t *feature_line_buffers[],
                               int feature_length, int buffer_row,
                               int col_begin, int col_end, int buffer_col);

// Calculates horizontal/vertical/diagonal/anti-diagonal gradients over a line
// and stores the results in associated line buffers. See CWG-C016 contribution
// for details.
void calc_gradient_in_various_directions_c(int16_t *feature_line_buffers[],
                                           int row, int buffer_row,
                                           const uint16_t *dgd, int dgd_stride,
                                           int width, int col_begin,
                                           int col_end, int feature_length,
                                           int buffer_col);

#endif  // AVM_AV2_COMMON_CONVOLVE_H_
