Support multi-q-category coeff cdf table optimization

Record the index of the selected set of coefficient cdf tables.
Do stats collection and cdf optimization separately for each
category.

Change-Id: If27ddec6c77a0189bbaf7244f666c845d1bc9592
diff --git a/av1/common/entropy.c b/av1/common/entropy.c
index 814fbf0..2850d51 100644
--- a/av1/common/entropy.c
+++ b/av1/common/entropy.c
@@ -560,6 +560,10 @@
 
 void av1_default_coef_probs(AV1_COMMON *cm) {
   const int index = get_q_ctx(cm->base_qindex);
+#if CONFIG_ENTROPY_STATS
+  cm->coef_cdf_category = index;
+#endif
+
   av1_copy(cm->fc->txb_skip_cdf, av1_default_txb_skip_cdfs[index]);
   av1_copy(cm->fc->eob_extra_cdf, av1_default_eob_extra_cdfs[index]);
   av1_copy(cm->fc->dc_sign_cdf, av1_default_dc_sign_cdfs[index]);
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 054f75e..831e9fa 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -192,26 +192,27 @@
                                 [SWITCHABLE_FILTERS];
 
 #if CONFIG_ENTROPY_STATS
-  unsigned int txb_skip[TX_SIZES][TXB_SKIP_CONTEXTS][2];
-  unsigned int eob_extra[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][2];
+  unsigned int txb_skip[TOKEN_CDF_Q_CTXS][TX_SIZES][TXB_SKIP_CONTEXTS][2];
+  unsigned int eob_extra[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
+                        [EOB_COEF_CONTEXTS][2];
   unsigned int dc_sign[PLANE_TYPES][DC_SIGN_CONTEXTS][2];
   unsigned int coeff_lps[TX_SIZES][PLANE_TYPES][BR_CDF_SIZE - 1][LEVEL_CONTEXTS]
                         [2];
-#endif  // CONFIG_ENTROPY_STATS
   unsigned int eob_flag[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS][2];
-  unsigned int eob_multi16[PLANE_TYPES][2][5];
-  unsigned int eob_multi32[PLANE_TYPES][2][6];
-  unsigned int eob_multi64[PLANE_TYPES][2][7];
-  unsigned int eob_multi128[PLANE_TYPES][2][8];
-  unsigned int eob_multi256[PLANE_TYPES][2][9];
-  unsigned int eob_multi512[PLANE_TYPES][2][10];
-  unsigned int eob_multi1024[PLANE_TYPES][2][11];
-  unsigned int coeff_lps_multi[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS]
-                              [BR_CDF_SIZE];
-  unsigned int coeff_base_multi[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS]
-                               [NUM_BASE_LEVELS + 2];
-  unsigned int coeff_base_eob_multi[TX_SIZES][PLANE_TYPES]
+  unsigned int eob_multi16[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][5];
+  unsigned int eob_multi32[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][6];
+  unsigned int eob_multi64[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][7];
+  unsigned int eob_multi128[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][8];
+  unsigned int eob_multi256[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][9];
+  unsigned int eob_multi512[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][10];
+  unsigned int eob_multi1024[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][11];
+  unsigned int coeff_lps_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
+                              [LEVEL_CONTEXTS][BR_CDF_SIZE];
+  unsigned int coeff_base_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
+                               [SIG_COEF_CONTEXTS][NUM_BASE_LEVELS + 2];
+  unsigned int coeff_base_eob_multi[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES]
                                    [SIG_COEF_CONTEXTS_EOB][NUM_BASE_LEVELS + 1];
+#endif  // CONFIG_ENTROPY_STATS
 
   unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
   unsigned int zeromv_mode[GLOBALMV_MODE_CONTEXTS][2];
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index e3d6988..a15fd14 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -339,6 +339,10 @@
   /* profile settings */
   TX_MODE tx_mode;
 
+#if CONFIG_ENTROPY_STATS
+  int coef_cdf_category;
+#endif
+
   int base_qindex;
   int y_dc_delta_q;
   int u_dc_delta_q;
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 9da6984..1b9117a 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -133,10 +133,16 @@
   return error;
 }
 
-void av1_update_eob_context(int eob, int seg_eob, TX_SIZE tx_size,
+#if CONFIG_ENTROPY_STATS
+void av1_update_eob_context(int cdf_idx, int eob, int seg_eob, TX_SIZE tx_size,
                             TX_TYPE tx_type, PLANE_TYPE plane,
                             FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts,
                             uint8_t allow_update_cdf) {
+#else
+void av1_update_eob_context(int eob, int seg_eob, TX_SIZE tx_size,
+                            TX_TYPE tx_type, PLANE_TYPE plane,
+                            FRAME_CONTEXT *ec_ctx, uint8_t allow_update_cdf) {
+#endif
   int eob_extra, dummy;
   const int eob_pt = get_eob_pos_token(eob, &eob_extra);
   const int max_eob_pt = get_eob_pos_token(seg_eob, &dummy);
@@ -148,36 +154,48 @@
 
   switch (eob_multi_size) {
     case 0:
-      ++counts->eob_multi16[plane][eob_multi_ctx][eob_pt - 1];
+#if CONFIG_ENTROPY_STATS
+      ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
+#endif
       if (allow_update_cdf)
         update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5);
       break;
     case 1:
-      ++counts->eob_multi32[plane][eob_multi_ctx][eob_pt - 1];
+#if CONFIG_ENTROPY_STATS
+      ++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
+#endif
       if (allow_update_cdf)
         update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6);
       break;
     case 2:
-      ++counts->eob_multi64[plane][eob_multi_ctx][eob_pt - 1];
+#if CONFIG_ENTROPY_STATS
+      ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
+#endif
       if (allow_update_cdf)
         update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7);
       break;
     case 3:
-      ++counts->eob_multi128[plane][eob_multi_ctx][eob_pt - 1];
+#if CONFIG_ENTROPY_STATS
+      ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
+#endif
       if (allow_update_cdf) {
         update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1,
                    8);
       }
       break;
     case 4:
-      ++counts->eob_multi256[plane][eob_multi_ctx][eob_pt - 1];
+#if CONFIG_ENTROPY_STATS
+      ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
+#endif
       if (allow_update_cdf) {
         update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1,
                    9);
       }
       break;
     case 5:
-      ++counts->eob_multi512[plane][eob_multi_ctx][eob_pt - 1];
+#if CONFIG_ENTROPY_STATS
+      ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
+#endif
       if (allow_update_cdf) {
         update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1,
                    10);
@@ -185,7 +203,9 @@
       break;
     case 6:
     default:
-      ++counts->eob_multi1024[plane][eob_multi_ctx][eob_pt - 1];
+#if CONFIG_ENTROPY_STATS
+      ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
+#endif
       if (allow_update_cdf) {
         update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1,
                    11);
@@ -197,7 +217,7 @@
     int eob_shift = k_eob_offset_bits[eob_pt] - 1;
     int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
 #if CONFIG_ENTROPY_STATS
-    counts->eob_extra[txs_ctx][plane][eob_pt][bit]++;
+    counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++;
 #endif  // CONFIG_ENTROPY_STATS
     if (allow_update_cdf)
       update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_pt], bit, 2);
@@ -1319,10 +1339,13 @@
   TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size);
   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
   DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
+#if CONFIG_ENTROPY_STATS
+  int cdf_idx = cm->coef_cdf_category;
+#endif  // CONFIG_ENTROPY_STATS
 
   memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
 #if CONFIG_ENTROPY_STATS
-  ++td->counts->txb_skip[txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0];
+  ++td->counts->txb_skip[cdf_idx][txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0];
 #endif  // CONFIG_ENTROPY_STATS
   if (allow_update_cdf) {
     update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx], eob == 0,
@@ -1340,8 +1363,13 @@
   av1_txb_init_levels(tcoeff, width, height, levels);
   av1_update_tx_type_count(cm, xd, blk_row, blk_col, plane, mbmi->sb_type,
                            tx_size, td->counts, allow_update_cdf);
+#if CONFIG_ENTROPY_STATS
+  av1_update_eob_context(cdf_idx, eob, seg_eob, tx_size, tx_type, plane_type,
+                         ec_ctx, td->counts, allow_update_cdf);
+#else
   av1_update_eob_context(eob, seg_eob, tx_size, tx_type, plane_type, ec_ctx,
-                         td->counts, allow_update_cdf);
+                         allow_update_cdf);
+#endif
   av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_type, coeff_contexts);
 
   for (c = eob - 1; c >= 0; --c) {
@@ -1366,11 +1394,13 @@
     {
       if (c == eob - 1) {
         assert(coeff_ctx < 4);
-        ++td->counts->coeff_base_eob_multi[txsize_ctx][plane_type][coeff_ctx]
-                                          [AOMMIN(level, 3) - 1];
+#if CONFIG_ENTROPY_STATS
+        ++td->counts->coeff_base_eob_multi[cdf_idx][txsize_ctx][plane_type]
+                                          [coeff_ctx][AOMMIN(level, 3) - 1];
       } else {
-        ++td->counts->coeff_base_multi[txsize_ctx][plane_type][coeff_ctx]
-                                      [AOMMIN(level, 3)];
+        ++td->counts->coeff_base_multi[cdf_idx][txsize_ctx][plane_type]
+                                      [coeff_ctx][AOMMIN(level, 3)];
+#endif
       }
     }
     if (level > NUM_BASE_LEVELS) {
@@ -1390,8 +1420,10 @@
 #endif  // CONFIG_ENTROPY_STATS
           if (lps == k) break;
         }
-        ++td->counts->coeff_lps_multi[AOMMIN(txsize_ctx, TX_32X32)][plane_type]
-                                     [br_ctx][k];
+#if CONFIG_ENTROPY_STATS
+        ++td->counts->coeff_lps_multi[cdf_idx][AOMMIN(txsize_ctx, TX_32X32)]
+                                     [plane_type][br_ctx][k];
+#endif
         if (k < BR_CDF_SIZE - 1) break;
       }
     }
diff --git a/tools/aom_entropy_optimizer.c b/tools/aom_entropy_optimizer.c
index 9fb1a42..f58137f 100644
--- a/tools/aom_entropy_optimizer.c
+++ b/tools/aom_entropy_optimizer.c
@@ -414,102 +414,116 @@
                      "static const aom_cdf_prob "
                      "default_filter_intra_cdfs[BLOCK_SIZES_ALL][CDF_SIZE(2)]");
 
-  cts_each_dim[0] = TX_SIZES;
-  cts_each_dim[1] = TXB_SKIP_CONTEXTS;
-  cts_each_dim[2] = 2;
-  optimize_cdf_table(&fc.txb_skip[0][0][0], probsfile, 3, cts_each_dim,
-                     "static const aom_cdf_prob "
-                     "default_nz_map_cdf[TX_SIZES][PLANE_TYPES][SIG_COEF_"
-                     "CONTEXTS][CDF_SIZE(2)]");
-
-  cts_each_dim[0] = TX_SIZES;
-  cts_each_dim[1] = PLANE_TYPES;
-  cts_each_dim[2] = EOB_COEF_CONTEXTS;
+  /* transform coding */
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = TX_SIZES;
+  cts_each_dim[2] = TXB_SKIP_CONTEXTS;
   cts_each_dim[3] = 2;
+  optimize_cdf_table(&fc.txb_skip[0][0][0][0], probsfile, 4, cts_each_dim,
+                     "static const aom_cdf_prob "
+                     "av1_default_txb_skip_cdfs[TOKEN_CDF_Q_CTXS][TX_SIZES]"
+                     "[TXB_SKIP_CONTEXTS][CDF_SIZE(2)]");
+
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = TX_SIZES;
+  cts_each_dim[2] = PLANE_TYPES;
+  cts_each_dim[3] = EOB_COEF_CONTEXTS;
+  cts_each_dim[4] = 2;
   optimize_cdf_table(
-      &fc.eob_extra[0][0][0][0], probsfile, 4, cts_each_dim,
-      "static const aom_cdf_prob "
-      "default_eob_extra_cdf[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS]"
+      &fc.eob_extra[0][0][0][0][0], probsfile, 5, cts_each_dim,
+      "static const aom_cdf_prob av1_default_eob_extra_cdfs "
+      "[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS]"
       "[CDF_SIZE(2)]");
 
-  cts_each_dim[0] = PLANE_TYPES;
-  cts_each_dim[1] = 2;
-  cts_each_dim[2] = 5;
-  optimize_cdf_table(&fc.eob_multi16[0][0][0], probsfile, 3, cts_each_dim,
-                     "static const aom_cdf_prob "
-                     "default_eob_multi16[PLANE_TYPES][2][CDF_SIZE(5)]");
-
-  cts_each_dim[0] = PLANE_TYPES;
-  cts_each_dim[1] = 2;
-  cts_each_dim[2] = 6;
-  optimize_cdf_table(&fc.eob_multi32[0][0][0], probsfile, 3, cts_each_dim,
-                     "static const aom_cdf_prob "
-                     "default_eob_multi32[PLANE_TYPES][2][CDF_SIZE(6)]");
-
-  cts_each_dim[0] = PLANE_TYPES;
-  cts_each_dim[1] = 2;
-  cts_each_dim[2] = 7;
-  optimize_cdf_table(&fc.eob_multi64[0][0][0], probsfile, 3, cts_each_dim,
-                     "static const aom_cdf_prob "
-                     "default_eob_multi64[PLANE_TYPES][2][CDF_SIZE(7)]");
-
-  cts_each_dim[0] = PLANE_TYPES;
-  cts_each_dim[1] = 2;
-  cts_each_dim[2] = 8;
-  optimize_cdf_table(&fc.eob_multi128[0][0][0], probsfile, 3, cts_each_dim,
-                     "static const aom_cdf_prob "
-                     "default_eob_multi128[PLANE_TYPES][2][CDF_SIZE(8)]");
-
-  cts_each_dim[0] = PLANE_TYPES;
-  cts_each_dim[1] = 2;
-  cts_each_dim[2] = 9;
-  optimize_cdf_table(&fc.eob_multi256[0][0][0], probsfile, 3, cts_each_dim,
-                     "static const aom_cdf_prob "
-                     "default_eob_multi256[PLANE_TYPES][2][CDF_SIZE(9)]");
-
-  cts_each_dim[0] = PLANE_TYPES;
-  cts_each_dim[1] = 2;
-  cts_each_dim[2] = 10;
-  optimize_cdf_table(&fc.eob_multi512[0][0][0], probsfile, 3, cts_each_dim,
-                     "static const aom_cdf_prob "
-                     "default_eob_multi512[PLANE_TYPES][2][CDF_SIZE(10)]");
-
-  cts_each_dim[0] = PLANE_TYPES;
-  cts_each_dim[1] = 2;
-  cts_each_dim[2] = 11;
-  optimize_cdf_table(&fc.eob_multi1024[0][0][0], probsfile, 3, cts_each_dim,
-                     "static const aom_cdf_prob "
-                     "default_eob_multi1024[PLANE_TYPES][2][CDF_SIZE(11)]");
-
-  cts_each_dim[0] = TX_SIZES;
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
   cts_each_dim[1] = PLANE_TYPES;
-  cts_each_dim[2] = LEVEL_CONTEXTS;
-  cts_each_dim[3] = BR_CDF_SIZE;
-  optimize_cdf_table(&fc.coeff_lps_multi[0][0][0][0], probsfile, 4,
+  cts_each_dim[2] = 2;
+  cts_each_dim[3] = 5;
+  optimize_cdf_table(&fc.eob_multi16[0][0][0][0], probsfile, 4, cts_each_dim,
+                     "static const aom_cdf_prob av1_default_eob_multi16_cdfs"
+                     "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(5)]");
+
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = PLANE_TYPES;
+  cts_each_dim[2] = 2;
+  cts_each_dim[3] = 6;
+  optimize_cdf_table(&fc.eob_multi32[0][0][0][0], probsfile, 4, cts_each_dim,
+                     "static const aom_cdf_prob av1_default_eob_multi32_cdfs"
+                     "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(6)]");
+
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = PLANE_TYPES;
+  cts_each_dim[2] = 2;
+  cts_each_dim[3] = 7;
+  optimize_cdf_table(&fc.eob_multi64[0][0][0][0], probsfile, 4, cts_each_dim,
+                     "static const aom_cdf_prob av1_default_eob_multi64_cdfs"
+                     "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(7)]");
+
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = PLANE_TYPES;
+  cts_each_dim[2] = 2;
+  cts_each_dim[3] = 8;
+  optimize_cdf_table(&fc.eob_multi128[0][0][0][0], probsfile, 4, cts_each_dim,
+                     "static const aom_cdf_prob av1_default_eob_multi128_cdfs"
+                     "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(8)]");
+
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = PLANE_TYPES;
+  cts_each_dim[2] = 2;
+  cts_each_dim[3] = 9;
+  optimize_cdf_table(&fc.eob_multi256[0][0][0][0], probsfile, 4, cts_each_dim,
+                     "static const aom_cdf_prob av1_default_eob_multi256_cdfs"
+                     "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(9)]");
+
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = PLANE_TYPES;
+  cts_each_dim[2] = 2;
+  cts_each_dim[3] = 10;
+  optimize_cdf_table(&fc.eob_multi512[0][0][0][0], probsfile, 4, cts_each_dim,
+                     "static const aom_cdf_prob av1_default_eob_multi512_cdfs"
+                     "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(10)]");
+
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = PLANE_TYPES;
+  cts_each_dim[2] = 2;
+  cts_each_dim[3] = 11;
+  optimize_cdf_table(&fc.eob_multi1024[0][0][0][0], probsfile, 4, cts_each_dim,
+                     "static const aom_cdf_prob av1_default_eob_multi1024_cdfs"
+                     "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(11)]");
+
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = TX_SIZES;
+  cts_each_dim[2] = PLANE_TYPES;
+  cts_each_dim[3] = LEVEL_CONTEXTS;
+  cts_each_dim[4] = BR_CDF_SIZE;
+  optimize_cdf_table(&fc.coeff_lps_multi[0][0][0][0][0], probsfile, 5,
                      cts_each_dim,
                      "static const aom_cdf_prob "
-                     "default_coeff_lps_multi[TX_SIZES][PLANE_TYPES][LEVEL_"
-                     "CONTEXTS][CDF_SIZE(BR_CDF_SIZE)]");
+                     "av1_default_coeff_lps_multi_cdfs[TOKEN_CDF_Q_CTXS]"
+                     "[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS]"
+                     "[CDF_SIZE(BR_CDF_SIZE)]");
 
-  cts_each_dim[0] = TX_SIZES;
-  cts_each_dim[1] = PLANE_TYPES;
-  cts_each_dim[2] = SIG_COEF_CONTEXTS_2D + SIG_COEF_CONTEXTS_1D;
-  cts_each_dim[3] = 4;
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = TX_SIZES;
+  cts_each_dim[2] = PLANE_TYPES;
+  cts_each_dim[3] = SIG_COEF_CONTEXTS;
+  cts_each_dim[4] = NUM_BASE_LEVELS + 2;
   optimize_cdf_table(
-      &fc.coeff_base_multi[0][0][0][0], probsfile, 4, cts_each_dim,
-      "static const aom_cdf_prob "
-      "default_coeff_base_multi[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS]"
-      "[CDF_SIZE(NUM_BASE_LEVELS+2)]");
+      &fc.coeff_base_multi[0][0][0][0][0], probsfile, 5, cts_each_dim,
+      "static const aom_cdf_prob av1_default_coeff_base_multi_cdfs"
+      "[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS]"
+      "[CDF_SIZE(NUM_BASE_LEVELS + 2)]");
 
-  cts_each_dim[0] = TX_SIZES;
-  cts_each_dim[1] = PLANE_TYPES;
-  cts_each_dim[2] = SIG_COEF_CONTEXTS_EOB;
-  cts_each_dim[3] = 3;
+  cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
+  cts_each_dim[1] = TX_SIZES;
+  cts_each_dim[2] = PLANE_TYPES;
+  cts_each_dim[3] = SIG_COEF_CONTEXTS_EOB;
+  cts_each_dim[4] = NUM_BASE_LEVELS + 1;
   optimize_cdf_table(
-      &fc.coeff_base_eob_multi[0][0][0][0], probsfile, 4, cts_each_dim,
-      "static const aom_cdf_prob "
-      "default_coeff_base_eob_multi[TX_SIZES][PLANE_TYPES][SIG_COEF_"
-      "CONTEXTS_EOB][CDF_SIZE(NUM_BASE_LEVELS+1)]");
+      &fc.coeff_base_eob_multi[0][0][0][0][0], probsfile, 5, cts_each_dim,
+      "static const aom_cdf_prob av1_default_coeff_base_eob_multi_cdfs"
+      "[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS_EOB]"
+      "[CDF_SIZE(NUM_BASE_LEVELS + 1)]");
 
   /* Skip mode flag */
   cts_each_dim[0] = SKIP_MODE_CONTEXTS;