/*
 * 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_ENTROPY_H_
#define AVM_AV2_COMMON_ENTROPY_H_

#include "config/avm_config.h"

#include "avm/avm_integer.h"
#include "avm_dsp/prob.h"

#include "av2/common/common.h"
#include "av2/common/common_data.h"
#include "av2/common/enums.h"

#ifdef __cplusplus
extern "C" {
#endif

#define TOKEN_CDF_Q_CTXS 4

#define CROSS_COMPONENT_CONTEXTS 3
#define V_TXB_SKIP_CONTEXTS 12
#define V_TXB_SKIP_CONTEXT_OFFSET 6

#define TXB_SKIP_PRED_MODE_CTXS 2
#define TXB_SKIP_CONTEXTS 10

#define FSC_TX_SIZE_CONTEXTS 3
#define IDTX_SIGN_CONTEXTS 9
#define IDTX_SIG_COEF_CONTEXTS 7
#define IDTX_LEVEL_CONTEXTS 7

#define EOB_COEF_CONTEXTS 9
#define SIG_COEF_CONTEXTS_BOB 3

#define EOB_MAX_SYMS 11

#define COEFF_BASE_PH_CONTEXTS 5
#define COEFF_BR_PH_CONTEXTS 7

// Number of coefficient coding chroma contexts for the low-frequency region
// for 2D and 1D transforms
#define LF_SIG_COEF_CONTEXTS_2D_UV 8
#define LF_SIG_COEF_CONTEXTS_1D_UV 4
#define LF_SIG_COEF_CONTEXTS_UV \
  (LF_SIG_COEF_CONTEXTS_2D_UV + LF_SIG_COEF_CONTEXTS_1D_UV)
#define LF_LEVEL_CONTEXTS_UV 8
// Number of coefficient coding chroma contexts for the default region
// for 2D and 1D transforms
#define SIG_COEF_CONTEXTS_UV 12  // base range contexts
#define LEVEL_CONTEXTS_UV 4      // low range contexts

// Add an additional dimension to base coeff CDFs
#define TCQ_CTXS 2

// Number of coefficient coding contexts for the low-frequency region
// for 2D and 1D transforms
#define LF_SIG_COEF_CONTEXTS_2D 21
#define LF_SIG_COEF_CONTEXTS_1D 12
#define LF_SIG_COEF_CONTEXTS (LF_SIG_COEF_CONTEXTS_2D + LF_SIG_COEF_CONTEXTS_1D)
#define LF_LEVEL_CONTEXTS 14  // low-range contexts

// Number of coefficient coding contexts for the higher-frequency default region
// for 2D and 1D transforms
#define SIG_COEF_CONTEXTS_2D 20
#define SIG_COEF_CONTEXTS SIG_COEF_CONTEXTS_2D  // base range contexts
#define LEVEL_CONTEXTS 7                        // low range contexts

#define SIG_COEF_CONTEXTS_EOB 4  // context count for the EOB coefficient

// Number of symbols for base range coding in low-frequency region
#define LF_BASE_SYMBOLS 6
#define LF_NUM_BASE_LEVELS (LF_BASE_SYMBOLS - 2)
#define LF_MAX_BASE_BR_RANGE (COEFF_BASE_RANGE + LF_NUM_BASE_LEVELS + 1)

// Limits to determine the low-frequency region for coefficient coding.
#define LF_2D_LIM 4     // row + column limit
#define LF_2D_LIM_UV 1  // row + column limit for chroma
#define LF_RC_LIM 2     // row or column limit
#define LF_RC_LIM_UV 1  // row or column limit for chroma

#define DC_SIGN_GROUPS 2
#define DC_SIGN_CONTEXTS 3

#define NUM_BASE_LEVELS 2

#define BR_CDF_SIZE (4)
#define COEFF_BASE_RANGE (1 * (BR_CDF_SIZE - 1))

#define COEFF_CONTEXT_BITS 3
#define COEFF_CONTEXT_MASK ((1 << COEFF_CONTEXT_BITS) - 1)
#define MAX_BASE_BR_RANGE (COEFF_BASE_RANGE + NUM_BASE_LEVELS + 1)

enum {
  TX_CLASS_2D = 0,
  TX_CLASS_HORIZ = 1,
  TX_CLASS_VERT = 2,
  TX_CLASSES,
} UENUM1BYTE(TX_CLASS);

/* Coefficients are predicted via a 3-dimensional probability table indexed on
 * REF_TYPES, COEF_BANDS and COEF_CONTEXTS. */
#define REF_TYPES 2  // intra=0, inter=1

struct AV2Common;
struct frame_contexts;
void av2_reset_cdf_symbol_counters(struct frame_contexts *fc);
void av2_default_coef_probs(struct AV2Common *cm);

struct frame_contexts;

typedef char ENTROPY_CONTEXT;

static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
                                           ENTROPY_CONTEXT b) {
  return (a != 0) + (b != 0);
}

static INLINE ENTROPY_CONTEXT get_entropy_context_1d(const ENTROPY_CONTEXT *ctx,
                                                     int size) {
  switch (size) {
    case 4: return ctx[0] != 0;
    case 8: return ctx[0] != 0 || ctx[1] != 0;
    case 16: return ctx[0] != 0 || ctx[1] != 0 || ctx[2] != 0 || ctx[3] != 0;
    case 32:
      return !!(*(const uint16_t *)ctx | *(const uint16_t *)(ctx + 2) |
                *(const uint16_t *)(ctx + 4) | *(const uint16_t *)(ctx + 6));
    case 64: return !!(*(const uint64_t *)ctx | *(const uint64_t *)(ctx + 8));
    default: assert(0 && "Invalid transform 1d size."); break;
  }

  return 0;
}

static INLINE int get_entropy_context(TX_SIZE tx_size, const ENTROPY_CONTEXT *a,
                                      const ENTROPY_CONTEXT *l) {
  assert(tx_size < TX_SIZES_ALL);
  const int txw = tx_size_wide[tx_size];
  const int txh = tx_size_high[tx_size];
  ENTROPY_CONTEXT above_ec = 0, left_ec = 0;

  above_ec = get_entropy_context_1d(a, txw);
  left_ec = get_entropy_context_1d(l, txh);
  return combine_entropy_contexts(above_ec, left_ec);
}

static INLINE TX_SIZE get_txsize_entropy_ctx(TX_SIZE txsize) {
  return (TX_SIZE)((txsize_sqr_map[txsize] + txsize_sqr_up_map[txsize] + 1) >>
                   1);
}

#define EOB_PLANE_CTXS 3

static INLINE int get_eob_plane_ctx(int plane, int is_inter) {
  return plane ? 2 : is_inter;
}

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

#endif  // AVM_AV2_COMMON_ENTROPY_H_
