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

#include "config/avm_config.h"

#include "avm/avm_integer.h"
#include "avm_mem/avm_mem.h"
#include "av2/common/av2_common_int.h"
#include "av2/common/entropy.h"
#include "av2/common/entropymode.h"
#include "av2/common/scan.h"
#include "av2/common/txb_common.h"

// Context tables for coefficient coding
#include "av2/common/entropy_inits_coeffs.h"

static int get_q_ctx(int q) {
  if (q <= 90) return 0;
  if (q <= 140) return 1;
  if (q <= 190) return 2;
  return 3;
}

void av2_default_coef_probs(AV2_COMMON *cm) {
  const int index = get_q_ctx(cm->quant_params.base_qindex);
#if CONFIG_ENTROPY_STATS
  cm->coef_cdf_category = index;
#endif

  av2_copy(cm->fc->txb_skip_cdf[0], av2_default_txb_skip_cdfs[index][0]);
  av2_copy(cm->fc->txb_skip_cdf[1], av2_default_txb_skip_cdfs[index][1]);
  av2_copy(cm->fc->v_txb_skip_cdf, av2_default_v_txb_skip_cdfs[index]);
  av2_copy(cm->fc->eob_extra_cdf, av2_default_eob_extra_cdfs[index]);
  av2_copy(cm->fc->dc_sign_cdf, av2_default_dc_sign_cdfs[index]);
  av2_copy(cm->fc->coeff_base_lf_cdf,
           av2_default_coeff_base_lf_multi_cdfs[index]);
  av2_copy(cm->fc->coeff_base_lf_eob_cdf,
           av2_default_coeff_base_lf_eob_multi_cdfs[index]);
  av2_copy(cm->fc->coeff_br_lf_cdf, av2_default_coeff_lps_lf_multi_cdfs[index]);
  av2_copy(cm->fc->coeff_br_cdf, av2_default_coeff_lps_multi_cdfs[index]);
  av2_copy(cm->fc->coeff_base_cdf, av2_default_coeff_base_multi_cdfs[index]);
  av2_copy(cm->fc->idtx_sign_cdf, av2_default_idtx_sign_cdfs[index]);
  av2_copy(cm->fc->coeff_base_cdf_idtx,
           av2_default_coeff_base_multi_cdfs_idtx[index]);
  av2_copy(cm->fc->coeff_br_cdf_idtx,
           av2_default_coeff_lps_multi_cdfs_idtx[index]);
  av2_copy(cm->fc->coeff_base_eob_cdf,
           av2_default_coeff_base_eob_multi_cdfs[index]);
  av2_copy(cm->fc->coeff_base_lf_uv_cdf,
           av2_default_coeff_base_lf_multi_uv_cdfs[index]);
  av2_copy(cm->fc->coeff_base_lf_eob_uv_cdf,
           av2_default_coeff_base_lf_eob_multi_uv_cdfs[index]);
  av2_copy(cm->fc->coeff_br_uv_cdf, av2_default_coeff_lps_multi_uv_cdfs[index]);
  av2_copy(cm->fc->coeff_base_uv_cdf,
           av2_default_coeff_base_multi_uv_cdfs[index]);
  av2_copy(cm->fc->coeff_base_eob_uv_cdf,
           av2_default_coeff_base_eob_multi_uv_cdfs[index]);
  av2_copy(cm->fc->eob_flag_cdf16, av2_default_eob_multi16_cdfs[index]);
  av2_copy(cm->fc->eob_flag_cdf32, av2_default_eob_multi32_cdfs[index]);
  av2_copy(cm->fc->eob_flag_cdf64, av2_default_eob_multi64_cdfs[index]);
  av2_copy(cm->fc->eob_flag_cdf128, av2_default_eob_multi128_cdfs[index]);
  av2_copy(cm->fc->eob_flag_cdf256, av2_default_eob_multi256_cdfs[index]);
  av2_copy(cm->fc->eob_flag_cdf512, av2_default_eob_multi512_cdfs[index]);
  av2_copy(cm->fc->eob_flag_cdf1024, av2_default_eob_multi1024_cdfs[index]);
  av2_copy(cm->fc->coeff_base_ph_cdf, av2_default_coeff_base_ph_cdfs[index]);
  av2_copy(cm->fc->coeff_base_bob_cdf,
           av2_default_coeff_base_bob_multi_cdfs[index]);
}

static AVM_INLINE void reset_cdf_symbol_counter(avm_cdf_prob *cdf_ptr,
                                                int num_cdfs, int cdf_stride,
                                                int nsymbs) {
  for (int i = 0; i < num_cdfs; i++) {
    cdf_ptr[i * cdf_stride + nsymbs] =
        (cdf_ptr[i * cdf_stride + nsymbs] * 3) >> 2;
  }
}

#define RESET_CDF_COUNTER(cname, nsymbs) \
  RESET_CDF_COUNTER_STRIDE(cname, nsymbs, CDF_SIZE(nsymbs))

#define RESET_CDF_COUNTER_STRIDE(cname, nsymbs, cdf_stride)          \
  do {                                                               \
    avm_cdf_prob *cdf_ptr = (avm_cdf_prob *)cname;                   \
    int array_size = (int)sizeof(cname) / sizeof(avm_cdf_prob);      \
    int num_cdfs = array_size / cdf_stride;                          \
    reset_cdf_symbol_counter(cdf_ptr, num_cdfs, cdf_stride, nsymbs); \
  } while (0)

static AVM_INLINE void reset_nmv_counter(nmv_context *nmv) {
  RESET_CDF_COUNTER(nmv->joint_shell_set_cdf, 2);
  for (int prec = 0; prec < NUM_MV_PRECISIONS; prec++) {
    const int num_mv_class = get_default_num_shell_class(prec);
    int num_mv_class_0, num_mv_class_1;
    split_num_shell_class(num_mv_class, &num_mv_class_0, &num_mv_class_1);
    RESET_CDF_COUNTER(nmv->joint_shell_class_cdf_0[prec], num_mv_class_0);
    if (prec == MV_PRECISION_ONE_EIGHTH_PEL) {
      RESET_CDF_COUNTER(nmv->joint_shell_class_cdf_1[prec], num_mv_class_1 - 1);
      RESET_CDF_COUNTER(nmv->joint_shell_last_two_classes_cdf, 2);
    } else {
      RESET_CDF_COUNTER(nmv->joint_shell_class_cdf_1[prec], num_mv_class_1);
    }
  }
  RESET_CDF_COUNTER(nmv->shell_offset_low_class_cdf, 2);
  RESET_CDF_COUNTER(nmv->shell_offset_class2_cdf, 2);
  for (int i = 0; i < NUM_CTX_CLASS_OFFSETS; i++) {
    RESET_CDF_COUNTER(nmv->shell_offset_other_class_cdf[i], 2);
  }
  RESET_CDF_COUNTER(nmv->col_mv_greater_flags_cdf, 2);
  RESET_CDF_COUNTER(nmv->col_mv_index_cdf, 2);

  RESET_CDF_COUNTER(nmv->amvd_joints_cdf, 4);
  for (int i = 0; i < 2; i++) {
    RESET_CDF_COUNTER(nmv->comps[i].amvd_indices_cdf, MAX_AMVD_INDEX);
  }
}

void av2_reset_cdf_symbol_counters(FRAME_CONTEXT *fc) {
  RESET_CDF_COUNTER(fc->txb_skip_cdf, 2);
  RESET_CDF_COUNTER(fc->v_txb_skip_cdf, 2);
  RESET_CDF_COUNTER(fc->eob_extra_cdf, 2);
  RESET_CDF_COUNTER(fc->dc_sign_cdf, 2);
  RESET_CDF_COUNTER(fc->eob_flag_cdf16, EOB_MAX_SYMS - 6);
  RESET_CDF_COUNTER(fc->eob_flag_cdf32, EOB_MAX_SYMS - 5);
  RESET_CDF_COUNTER(fc->eob_flag_cdf64, EOB_MAX_SYMS - 4);
  RESET_CDF_COUNTER(fc->eob_flag_cdf128, EOB_MAX_SYMS - 3);
  RESET_CDF_COUNTER(fc->eob_flag_cdf256, EOB_MAX_SYMS - 3);
  RESET_CDF_COUNTER(fc->eob_flag_cdf512, EOB_MAX_SYMS - 3);
  RESET_CDF_COUNTER(fc->eob_flag_cdf1024, EOB_MAX_SYMS - 3);
  RESET_CDF_COUNTER(fc->coeff_base_eob_cdf, 3);
  RESET_CDF_COUNTER(fc->coeff_base_bob_cdf, 3);
  RESET_CDF_COUNTER(fc->coeff_base_lf_cdf, LF_BASE_SYMBOLS);
  RESET_CDF_COUNTER(fc->coeff_base_lf_eob_cdf, LF_BASE_SYMBOLS - 1);
  RESET_CDF_COUNTER(fc->coeff_br_lf_cdf, BR_CDF_SIZE);
  RESET_CDF_COUNTER(fc->coeff_base_lf_uv_cdf, LF_BASE_SYMBOLS);
  RESET_CDF_COUNTER(fc->coeff_base_lf_eob_uv_cdf, LF_BASE_SYMBOLS - 1);
  RESET_CDF_COUNTER(fc->coeff_base_uv_cdf, 4);
  RESET_CDF_COUNTER(fc->coeff_base_eob_uv_cdf, 3);
  RESET_CDF_COUNTER(fc->coeff_br_uv_cdf, BR_CDF_SIZE);
  RESET_CDF_COUNTER(fc->coeff_base_cdf, 4);
  RESET_CDF_COUNTER(fc->idtx_sign_cdf, 2);
  RESET_CDF_COUNTER(fc->coeff_base_cdf_idtx, 4);
  RESET_CDF_COUNTER(fc->coeff_br_cdf_idtx, BR_CDF_SIZE);
  RESET_CDF_COUNTER(fc->coeff_br_cdf, BR_CDF_SIZE);
  RESET_CDF_COUNTER(fc->inter_single_mode_cdf, INTER_SINGLE_MODES);
  RESET_CDF_COUNTER(fc->inter_warp_mode_cdf, 2);
  RESET_CDF_COUNTER(fc->is_warpmv_or_warp_newmv_cdf, 2);
  RESET_CDF_COUNTER(fc->drl_cdf[0], 2);
  RESET_CDF_COUNTER(fc->drl_cdf[1], 2);
  RESET_CDF_COUNTER(fc->drl_cdf[2], 2);
  RESET_CDF_COUNTER(fc->skip_drl_cdf, 2);
  RESET_CDF_COUNTER(fc->tip_drl_cdf, 2);
  RESET_CDF_COUNTER(fc->use_optflow_cdf, 2);
  RESET_CDF_COUNTER(fc->inter_compound_mode_is_joint_cdf, NUM_OPTIONS_IS_JOINT);
  RESET_CDF_COUNTER(fc->inter_compound_mode_non_joint_type_cdf,
                    NUM_OPTIONS_NON_JOINT_TYPE);

  RESET_CDF_COUNTER(fc->inter_compound_mode_same_refs_cdf,
                    INTER_COMPOUND_SAME_REFS_TYPES);
  RESET_CDF_COUNTER(fc->amvd_mode_cdf, 2);
  RESET_CDF_COUNTER(fc->cwp_idx_cdf, 2);
  RESET_CDF_COUNTER(fc->jmvd_scale_mode_cdf, JOINT_NEWMV_SCALE_FACTOR_CNT);
  RESET_CDF_COUNTER(fc->jmvd_amvd_scale_mode_cdf, JOINT_AMVD_SCALE_FACTOR_CNT);
  RESET_CDF_COUNTER(fc->compound_type_cdf, MASKED_COMPOUND_TYPES);
  RESET_CDF_COUNTER(fc->wedge_quad_cdf, WEDGE_QUADS);
  RESET_CDF_COUNTER(fc->wedge_angle_cdf, QUAD_WEDGE_ANGLES);
  RESET_CDF_COUNTER(fc->wedge_dist_cdf, NUM_WEDGE_DIST);
  RESET_CDF_COUNTER(fc->wedge_dist_cdf2, NUM_WEDGE_DIST - 1);
  RESET_CDF_COUNTER(fc->interintra_cdf, 2);
  RESET_CDF_COUNTER(fc->wedge_interintra_cdf, 2);
  RESET_CDF_COUNTER(fc->interintra_mode_cdf, INTERINTRA_MODES);

  RESET_CDF_COUNTER(fc->warp_interintra_cdf, 2);
  RESET_CDF_COUNTER(fc->refinemv_flag_cdf, REFINEMV_NUM_MODES);

  RESET_CDF_COUNTER(fc->warp_causal_cdf, 2);
  RESET_CDF_COUNTER(fc->warp_ref_idx_cdf[0], 2);
  RESET_CDF_COUNTER(fc->warp_ref_idx_cdf[1], 2);
  RESET_CDF_COUNTER(fc->warp_ref_idx_cdf[2], 2);
  RESET_CDF_COUNTER(fc->warpmv_with_mvd_flag_cdf, 2);
  RESET_CDF_COUNTER(fc->warp_precision_idx_cdf, NUM_WARP_PRECISION_MODES);

  RESET_CDF_COUNTER(fc->warp_delta_param_cdf, WARP_DELTA_NUMSYMBOLS_LOW);
  RESET_CDF_COUNTER(fc->warp_delta_param_high_cdf, WARP_DELTA_NUMSYMBOLS_HIGH);
  RESET_CDF_COUNTER(fc->warp_param_sign_cdf, 2);
  RESET_CDF_COUNTER(fc->warp_extend_cdf, 2);
  RESET_CDF_COUNTER(fc->bawp_cdf[0], 2);
  RESET_CDF_COUNTER(fc->bawp_cdf[1], 2);
  RESET_CDF_COUNTER(fc->explicit_bawp_cdf, 2);
  RESET_CDF_COUNTER(fc->explicit_bawp_scale_cdf, EXPLICIT_BAWP_SCALE_CNT);
  RESET_CDF_COUNTER(fc->tip_cdf, 2);
  RESET_CDF_COUNTER(fc->tip_pred_mode_cdf, TIP_PRED_MODES);
  RESET_CDF_COUNTER(fc->palette_y_size_cdf, PALETTE_SIZES);
  RESET_CDF_COUNTER(fc->palette_uv_size_cdf, PALETTE_SIZES);
  RESET_CDF_COUNTER(fc->identity_row_cdf_y, 3);
  RESET_CDF_COUNTER(fc->identity_row_cdf_uv, 3);
  for (int j = 0; j < PALETTE_SIZES; j++) {
    int nsymbs = j + PALETTE_MIN_SIZE;
    RESET_CDF_COUNTER_STRIDE(fc->palette_y_color_index_cdf[j], nsymbs,
                             CDF_SIZE(PALETTE_COLORS));
  }
  RESET_CDF_COUNTER(fc->palette_y_mode_cdf, 2);
  RESET_CDF_COUNTER(fc->palette_uv_mode_cdf, 2);
  RESET_CDF_COUNTER(fc->comp_inter_cdf, 2);
  RESET_CDF_COUNTER(fc->single_ref_cdf, 2);
  RESET_CDF_COUNTER(fc->comp_ref0_cdf, 2);
  RESET_CDF_COUNTER(fc->comp_ref1_cdf, 2);
  RESET_CDF_COUNTER(fc->txfm_do_partition_cdf, 2);
  RESET_CDF_COUNTER(fc->txfm_2or3_way_partition_type_cdf, 2);
  RESET_CDF_COUNTER(fc->txfm_4way_partition_type_cdf, TX_PARTITION_TYPE_NUM);
  RESET_CDF_COUNTER(fc->lossless_tx_size_cdf, 2);
  RESET_CDF_COUNTER(fc->lossless_inter_tx_type_cdf, 2);
  RESET_CDF_COUNTER(fc->comp_group_idx_cdf, 2);
  RESET_CDF_COUNTER(fc->skip_mode_cdfs, 2);
  RESET_CDF_COUNTER(fc->bru_mode_cdf, 3);
  RESET_CDF_COUNTER(fc->intra_inter_cdf, 2);
  RESET_CDF_COUNTER(fc->skip_txfm_cdfs, 2);
  reset_nmv_counter(&fc->nmvc);
  reset_nmv_counter(&fc->ndvc);
  RESET_CDF_COUNTER(fc->intrabc_cdf, 2);
  RESET_CDF_COUNTER(fc->intrabc_mode_cdf, 2);
  RESET_CDF_COUNTER(fc->intrabc_bv_precision_cdf, NUM_ALLOWED_BV_PRECISIONS);
  RESET_CDF_COUNTER(fc->morph_pred_cdf, 2);
  RESET_CDF_COUNTER(fc->seg.pred_cdf, 2);
  RESET_CDF_COUNTER(fc->seg.tree_cdf, MAX_SEGMENTS_8);
  RESET_CDF_COUNTER(fc->seg.tree_cdf1, MAX_SEGMENTS_8);
  RESET_CDF_COUNTER(fc->seg.spatial_pred_seg_cdf, MAX_SEGMENTS_8);
  RESET_CDF_COUNTER(fc->seg.spatial_pred_seg_cdf1, MAX_SEGMENTS_8);
  RESET_CDF_COUNTER(fc->seg.seg_id_ext_flag_cdf, 2);
  RESET_CDF_COUNTER(fc->mrl_index_cdf, MRL_LINE_NUMBER);
  RESET_CDF_COUNTER(fc->multi_line_mrl_cdf, 2);
  RESET_CDF_COUNTER(fc->fsc_mode_cdf, FSC_MODES);

  // CDF for dpcm related symbols
  RESET_CDF_COUNTER(fc->dpcm_cdf, 2);
  RESET_CDF_COUNTER(fc->dpcm_vert_horz_cdf, 2);
  RESET_CDF_COUNTER(fc->dpcm_uv_cdf, 2);
  RESET_CDF_COUNTER(fc->dpcm_uv_vert_horz_cdf, 2);

  RESET_CDF_COUNTER(fc->cfl_mhccp_cdf, CFL_MHCCP_SWITCH_NUM);
  RESET_CDF_COUNTER(fc->cfl_index_cdf, CFL_TYPE_COUNT - 1);
  RESET_CDF_COUNTER(fc->filter_dir_cdf, MHCCP_MODE_NUM);
  RESET_CDF_COUNTER(fc->intra_dip_cdf, 2);
  RESET_CDF_COUNTER(fc->intra_dip_mode_n6_cdf, 6);
  RESET_CDF_COUNTER(fc->switchable_flex_restore_cdf, 2);
  for (int plane = 0; plane < CCSO_PLANES; plane++) {
    for (int ctx = 0; ctx < CCSO_CONTEXT; ctx++) {
      RESET_CDF_COUNTER(fc->ccso_cdf[plane][ctx], 2);
    }
  }
  RESET_CDF_COUNTER(fc->cdef_strength_index0_cdf, 2);
  for (int j = 0; j < CDEF_STRENGTHS_NUM - 1; j++) {
    RESET_CDF_COUNTER_STRIDE(fc->cdef_cdf[j], j + 2,
                             CDF_SIZE(CDEF_STRENGTHS_NUM));
  }
  RESET_CDF_COUNTER(fc->gdf_cdf, 2);
  RESET_CDF_COUNTER(fc->wienerns_restore_cdf, 2);
  RESET_CDF_COUNTER(fc->wienerns_length_cdf, 2);
  RESET_CDF_COUNTER(fc->wienerns_uv_sym_cdf, 2);
  RESET_CDF_COUNTER(fc->wienerns_4part_cdf, 4);
  RESET_CDF_COUNTER(fc->pc_wiener_restore_cdf, 2);
  RESET_CDF_COUNTER(fc->y_mode_set_cdf, INTRA_MODE_SETS);
  RESET_CDF_COUNTER(fc->y_mode_idx_cdf, LUMA_INTRA_MODE_INDEX_COUNT);
  RESET_CDF_COUNTER(fc->y_mode_idx_offset_cdf, LUMA_INTRA_MODE_OFFSET_COUNT);
  RESET_CDF_COUNTER(fc->uv_mode_cdf, CHROMA_INTRA_MODE_INDEX_COUNT);
  RESET_CDF_COUNTER(fc->cfl_cdf, 2);

  for (int i = 0; i < INTER_SDP_BSIZE_GROUP; i++) {
    RESET_CDF_COUNTER(fc->region_type_cdf[i], REGION_TYPES);
  }

  for (int plane_index = 0; plane_index < PARTITION_STRUCTURE_NUM;
       plane_index++) {
    for (int i = 0; i < PARTITION_CONTEXTS; i++) {
      RESET_CDF_COUNTER(fc->do_split_cdf[plane_index][i], 2);
    }
  }
  for (int plane_index = 0; plane_index < PARTITION_STRUCTURE_NUM;
       plane_index++) {
    for (int i = 0; i < SQUARE_SPLIT_CONTEXTS; i++) {
      RESET_CDF_COUNTER(fc->do_square_split_cdf[plane_index][i], 2);
    }
  }
  for (int plane_index = 0; plane_index < PARTITION_STRUCTURE_NUM;
       plane_index++) {
    for (int i = 0; i < PARTITION_CONTEXTS; i++) {
      RESET_CDF_COUNTER(fc->rect_type_cdf[plane_index][i], 2);
    }
  }
  for (int plane_index = 0; plane_index < PARTITION_STRUCTURE_NUM;
       plane_index++) {
    for (RECT_PART_TYPE rect = 0; rect < NUM_RECT_CONTEXTS; rect++) {
      for (int i = 0; i < PARTITION_CONTEXTS; i++) {
        RESET_CDF_COUNTER(fc->do_ext_partition_cdf[plane_index][rect][i], 2);
        RESET_CDF_COUNTER(
            fc->do_uneven_4way_partition_cdf[plane_index][rect][i], 2);
      }
    }
  }
  RESET_CDF_COUNTER(fc->switchable_interp_cdf, SWITCHABLE_FILTERS);
  RESET_CDF_COUNTER(fc->delta_q_cdf, DELTA_Q_PROBS + 1);

  RESET_CDF_COUNTER_STRIDE(fc->intra_ext_tx_cdf[1], INTRA_TX_SET1,
                           CDF_SIZE(TX_TYPES));
  RESET_CDF_COUNTER_STRIDE(fc->intra_ext_tx_cdf[2], INTRA_TX_SET2,
                           CDF_SIZE(TX_TYPES));
  RESET_CDF_COUNTER_STRIDE(fc->inter_ext_tx_cdf[1], INTER_TX_SET1,
                           CDF_SIZE(TX_TYPES));
  RESET_CDF_COUNTER_STRIDE(fc->inter_ext_tx_cdf[2], INTER_TX_SET2,
                           CDF_SIZE(TX_TYPES));
  RESET_CDF_COUNTER_STRIDE(fc->inter_ext_tx_cdf[3], INTER_TX_SET3,
                           CDF_SIZE(TX_TYPES));
  RESET_CDF_COUNTER_STRIDE(fc->inter_ext_tx_cdf[4], INTER_TX_SET4,
                           CDF_SIZE(TX_TYPES));
  RESET_CDF_COUNTER(fc->inter_tx_type_set, 2);
  RESET_CDF_COUNTER(fc->inter_tx_type_idx, INTER_TX_TYPE_INDEX_COUNT);
  RESET_CDF_COUNTER(fc->inter_tx_type_offset_1, INTER_TX_TYPE_OFFSET1_COUNT);
  RESET_CDF_COUNTER(fc->inter_tx_type_offset_2, INTER_TX_TYPE_OFFSET2_COUNT);
  RESET_CDF_COUNTER(fc->inter_ext_tx_short_side_cdf, 4);
  RESET_CDF_COUNTER(fc->intra_ext_tx_short_side_cdf, 4);
  RESET_CDF_COUNTER(fc->tx_ext_32_cdf, 2);
  RESET_CDF_COUNTER(fc->cfl_sign_cdf, CFL_JOINT_SIGNS);
  RESET_CDF_COUNTER(fc->cfl_alpha_cdf, CFL_ALPHABET_SIZE);

  RESET_CDF_COUNTER_STRIDE(fc->stx_cdf, STX_TYPES, CDF_SIZE(STX_TYPES));
  RESET_CDF_COUNTER(fc->most_probable_stx_set_cdf, IST_SET_SIZE);
  RESET_CDF_COUNTER(fc->most_probable_stx_set_cdf_ADST_ADST,
                    IST_REDUCED_SET_SIZE);
  for (int p = 0; p < NUM_MV_PREC_MPP_CONTEXT; ++p) {
    RESET_CDF_COUNTER(fc->pb_mv_mpp_flag_cdf[p], 2);
  }

  for (int p = MV_PRECISION_HALF_PEL; p < NUM_MV_PRECISIONS; ++p) {
    int num_precisions = MAX_NUM_OF_SUPPORTED_PRECISIONS;
    for (int j = 0; j < MV_PREC_DOWN_CONTEXTS; ++j) {
      RESET_CDF_COUNTER_STRIDE(
          fc->pb_mv_precision_cdf[j][p - MV_PRECISION_HALF_PEL],
          num_precisions - 1, CDF_SIZE(FLEX_MV_COSTS_SIZE));
    }
  }

  RESET_CDF_COUNTER(fc->coeff_base_ph_cdf, NUM_BASE_LEVELS + 2);
  RESET_CDF_COUNTER(fc->cctx_type_cdf, CCTX_TYPES);
}
