Account for elided extrabits during rate cost calculation.

Fixes some rd-debug mismatches coding cat6 tokens with tx size < 32x32.
For these tokens the high extrabits are elided during tokenization and
detokenization, but the rd cost was computed with the old tables from
VP9 where these high extrabits are always coded.

Change-Id: I4a9a6ea822ff821e1932c351d43a57bdb4d6d466
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index eaec9b8..3a45f47 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -846,10 +846,10 @@
       const int bit_string_length = extra_bits->len;  // Length of extra bits to
       // be written excluding
       // the sign bit.
-      int skip_bits =
-          (extra_bits->base_val == CAT6_MIN_VAL)
-              ? TX_SIZES - 1 - txsize_sqr_up_map[tx_size] + (12 - bit_depth)
-              : 0;
+      int skip_bits = (extra_bits->base_val == CAT6_MIN_VAL)
+                          ? (int)sizeof(av1_cat6_prob) -
+                                av1_get_cat6_extrabits_size(tx_size, bit_depth)
+                          : 0;
 
       if (bit_string_length > 0) {
         const unsigned char *pb = extra_bits->prob;
@@ -943,10 +943,10 @@
       const int bit_string_length = extra_bits->len;  // Length of extra bits to
                                                       // be written excluding
                                                       // the sign bit.
-      int skip_bits =
-          (extra_bits->base_val == CAT6_MIN_VAL)
-              ? TX_SIZES - 1 - txsize_sqr_up_map[tx_size] + (12 - bit_depth)
-              : 0;
+      int skip_bits = (extra_bits->base_val == CAT6_MIN_VAL)
+                          ? (int)sizeof(av1_cat6_prob) -
+                                av1_get_cat6_extrabits_size(tx_size, bit_depth)
+                          : 0;
       if (bit_string_length > 0) {
         const unsigned char *pb = extra_bits->prob;
         const int value = bit_string >> 1;
diff --git a/av1/encoder/encodemb.c b/av1/encoder/encodemb.c
index 38051cc..c2c68a5 100644
--- a/av1/encoder/encodemb.c
+++ b/av1/encoder/encodemb.c
@@ -153,9 +153,9 @@
                                        : band_translate[eob - 1];
   int pt, i, final_eob;
 #if CONFIG_AOM_HIGHBITDEPTH
-  const int *cat6_high_cost = av1_get_high_cost_table(xd->bd);
+  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
 #else
-  const int *cat6_high_cost = av1_get_high_cost_table(8);
+  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, 8);
 #endif
   unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
       mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref];
@@ -182,7 +182,7 @@
 
   for (i = 0; i < eob; i++) {
     const int rc = scan[i];
-    tokens[i][0].rate = av1_get_token_cost(qcoeff[rc], &t0, cat6_high_cost);
+    tokens[i][0].rate = av1_get_token_cost(qcoeff[rc], &t0, cat6_bits);
     tokens[i][0].token = t0;
     token_cache[rc] = av1_pt_energy_class[t0];
   }
@@ -302,7 +302,7 @@
         t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
         base_bits = 0;
       } else {
-        base_bits = av1_get_token_cost(x, &t0, cat6_high_cost);
+        base_bits = av1_get_token_cost(x, &t0, cat6_bits);
         t1 = t0;
       }
 
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 0aaaf68..cd126e7 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -1120,9 +1120,9 @@
 #endif  // CONFIG_NEW_TOKENSET
 
 #if CONFIG_AOM_HIGHBITDEPTH
-  const int *cat6_high_cost = av1_get_high_cost_table(xd->bd);
+  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
 #else
-  const int *cat6_high_cost = av1_get_high_cost_table(8);
+  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, 8);
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
 #if !CONFIG_VAR_TX && !CONFIG_SUPERTX
@@ -1145,7 +1145,7 @@
       // dc token
       int v = qcoeff[0];
       int16_t prev_t;
-      cost = av1_get_token_cost(v, &prev_t, cat6_high_cost);
+      cost = av1_get_token_cost(v, &prev_t, cat6_bits);
 #if CONFIG_NEW_TOKENSET
       cost += (*token_costs)[!prev_t][pt][prev_t];
 #else
@@ -1161,7 +1161,7 @@
         int16_t t;
 
         v = qcoeff[rc];
-        cost += av1_get_token_cost(v, &t, cat6_high_cost);
+        cost += av1_get_token_cost(v, &t, cat6_bits);
 #if CONFIG_NEW_TOKENSET
         cost += (*token_costs)[!t][!prev_t][t];
 #else
@@ -1187,7 +1187,7 @@
 #if !CONFIG_NEW_TOKENSET
       unsigned int(*tok_cost_ptr)[COEFF_CONTEXTS][ENTROPY_TOKENS];
 #endif
-      cost = av1_get_token_cost(v, &tok, cat6_high_cost);
+      cost = av1_get_token_cost(v, &tok, cat6_bits);
 #if CONFIG_NEW_TOKENSET
       cost += (*token_costs)[!tok][pt][tok];
 #else
@@ -1206,7 +1206,7 @@
         const int rc = scan[c];
 
         v = qcoeff[rc];
-        cost += av1_get_token_cost(v, &tok, cat6_high_cost);
+        cost += av1_get_token_cost(v, &tok, cat6_bits);
         pt = get_coef_context(nb, token_cache, c);
 #if CONFIG_NEW_TOKENSET
         cost += (*token_costs)[!tok][pt][tok];
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index af832b4..45814c3 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -126,139 +126,108 @@
   6620, 6632, 6654, 6666, 6677, 6689, 6751, 6763, 6774, 6786, 6808, 6820, 6831,
   6843, 6890, 6902, 6913, 6925, 6947, 6959, 6970, 6982
 };
-const int av1_cat6_high_cost[64] = {
-  88,    2251,  2727,  4890,  3148,  5311,  5787,  7950,  3666,  5829,  6305,
-  8468,  6726,  8889,  9365,  11528, 3666,  5829,  6305,  8468,  6726,  8889,
-  9365,  11528, 7244,  9407,  9883,  12046, 10304, 12467, 12943, 15106, 3666,
-  5829,  6305,  8468,  6726,  8889,  9365,  11528, 7244,  9407,  9883,  12046,
-  10304, 12467, 12943, 15106, 7244,  9407,  9883,  12046, 10304, 12467, 12943,
-  15106, 10822, 12985, 13461, 15624, 13882, 16045, 16521, 18684
-};
-
-#if CONFIG_AOM_HIGHBITDEPTH
-const int av1_cat6_high10_high_cost[256] = {
-  94,    2257,  2733,  4896,  3154,  5317,  5793,  7956,  3672,  5835,  6311,
-  8474,  6732,  8895,  9371,  11534, 3672,  5835,  6311,  8474,  6732,  8895,
-  9371,  11534, 7250,  9413,  9889,  12052, 10310, 12473, 12949, 15112, 3672,
-  5835,  6311,  8474,  6732,  8895,  9371,  11534, 7250,  9413,  9889,  12052,
-  10310, 12473, 12949, 15112, 7250,  9413,  9889,  12052, 10310, 12473, 12949,
-  15112, 10828, 12991, 13467, 15630, 13888, 16051, 16527, 18690, 4187,  6350,
-  6826,  8989,  7247,  9410,  9886,  12049, 7765,  9928,  10404, 12567, 10825,
-  12988, 13464, 15627, 7765,  9928,  10404, 12567, 10825, 12988, 13464, 15627,
-  11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 7765,  9928,  10404,
-  12567, 10825, 12988, 13464, 15627, 11343, 13506, 13982, 16145, 14403, 16566,
-  17042, 19205, 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 14921,
-  17084, 17560, 19723, 17981, 20144, 20620, 22783, 4187,  6350,  6826,  8989,
-  7247,  9410,  9886,  12049, 7765,  9928,  10404, 12567, 10825, 12988, 13464,
-  15627, 7765,  9928,  10404, 12567, 10825, 12988, 13464, 15627, 11343, 13506,
-  13982, 16145, 14403, 16566, 17042, 19205, 7765,  9928,  10404, 12567, 10825,
-  12988, 13464, 15627, 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205,
-  11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 14921, 17084, 17560,
-  19723, 17981, 20144, 20620, 22783, 8280,  10443, 10919, 13082, 11340, 13503,
-  13979, 16142, 11858, 14021, 14497, 16660, 14918, 17081, 17557, 19720, 11858,
-  14021, 14497, 16660, 14918, 17081, 17557, 19720, 15436, 17599, 18075, 20238,
-  18496, 20659, 21135, 23298, 11858, 14021, 14497, 16660, 14918, 17081, 17557,
-  19720, 15436, 17599, 18075, 20238, 18496, 20659, 21135, 23298, 15436, 17599,
-  18075, 20238, 18496, 20659, 21135, 23298, 19014, 21177, 21653, 23816, 22074,
-  24237, 24713, 26876
-};
-const int av1_cat6_high12_high_cost[1024] = {
+const int av1_cat6_high_cost[CAT6_HIGH_COST_ENTRIES] = {
   100,   2263,  2739,  4902,  3160,  5323,  5799,  7962,  3678,  5841,  6317,
   8480,  6738,  8901,  9377,  11540, 3678,  5841,  6317,  8480,  6738,  8901,
   9377,  11540, 7256,  9419,  9895,  12058, 10316, 12479, 12955, 15118, 3678,
   5841,  6317,  8480,  6738,  8901,  9377,  11540, 7256,  9419,  9895,  12058,
   10316, 12479, 12955, 15118, 7256,  9419,  9895,  12058, 10316, 12479, 12955,
-  15118, 10834, 12997, 13473, 15636, 13894, 16057, 16533, 18696, 4193,  6356,
+  15118, 10834, 12997, 13473, 15636, 13894, 16057, 16533, 18696,
+#if CONFIG_AOM_HIGHBITDEPTH
+  4193,  6356,  6832,  8995,  7253,  9416,  9892,  12055, 7771,  9934,  10410,
+  12573, 10831, 12994, 13470, 15633, 7771,  9934,  10410, 12573, 10831, 12994,
+  13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771,
+  9934,  10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151,
+  14409, 16572, 17048, 19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048,
+  19211, 14927, 17090, 17566, 19729, 17987, 20150, 20626, 22789, 4193,  6356,
   6832,  8995,  7253,  9416,  9892,  12055, 7771,  9934,  10410, 12573, 10831,
   12994, 13470, 15633, 7771,  9934,  10410, 12573, 10831, 12994, 13470, 15633,
   11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771,  9934,  10410,
   12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
   17048, 19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927,
-  17090, 17566, 19729, 17987, 20150, 20626, 22789, 4193,  6356,  6832,  8995,
-  7253,  9416,  9892,  12055, 7771,  9934,  10410, 12573, 10831, 12994, 13470,
-  15633, 7771,  9934,  10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512,
-  13988, 16151, 14409, 16572, 17048, 19211, 7771,  9934,  10410, 12573, 10831,
-  12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211,
-  11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566,
-  19729, 17987, 20150, 20626, 22789, 8286,  10449, 10925, 13088, 11346, 13509,
-  13985, 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864,
-  14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244,
-  18502, 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
-  19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605,
-  18081, 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080,
-  24243, 24719, 26882, 4193,  6356,  6832,  8995,  7253,  9416,  9892,  12055,
-  7771,  9934,  10410, 12573, 10831, 12994, 13470, 15633, 7771,  9934,  10410,
-  12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
-  17048, 19211, 7771,  9934,  10410, 12573, 10831, 12994, 13470, 15633, 11349,
-  13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349, 13512, 13988, 16151,
-  14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729, 17987, 20150, 20626,
-  22789, 8286,  10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027,
+  17090, 17566, 19729, 17987, 20150, 20626, 22789, 8286,  10449, 10925, 13088,
+  11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
+  19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605,
+  18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924,
+  17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
+  15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659,
+  23822, 22080, 24243, 24719, 26882, 4193,  6356,  6832,  8995,  7253,  9416,
+  9892,  12055, 7771,  9934,  10410, 12573, 10831, 12994, 13470, 15633, 7771,
+  9934,  10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151,
+  14409, 16572, 17048, 19211, 7771,  9934,  10410, 12573, 10831, 12994, 13470,
+  15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349, 13512,
+  13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729, 17987,
+  20150, 20626, 22789, 8286,  10449, 10925, 13088, 11346, 13509, 13985, 16148,
+  11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503,
+  16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665,
+  21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442,
+  17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244,
+  18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719,
+  26882, 8286,  10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027,
   14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924,
   17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
   11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081,
   20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665,
-  21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 8286,
-  10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666,
-  14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
-  19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027,
-  14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
-  20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
-  19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542, 15018,
-  17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180,
-  21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535,
-  21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759,
-  19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
-  27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276,
-  25752, 27915, 26173, 28336, 28812, 30975, 4193,  6356,  6832,  8995,  7253,
-  9416,  9892,  12055, 7771,  9934,  10410, 12573, 10831, 12994, 13470, 15633,
-  7771,  9934,  10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988,
-  16151, 14409, 16572, 17048, 19211, 7771,  9934,  10410, 12573, 10831, 12994,
-  13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349,
-  13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729,
-  17987, 20150, 20626, 22789, 8286,  10449, 10925, 13088, 11346, 13509, 13985,
+  21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379,
+  14542, 15018, 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759,
+  19017, 21180, 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
+  23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120,
+  18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595,
+  24758, 25234, 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397,
+  23113, 25276, 25752, 27915, 26173, 28336, 28812, 30975, 4193,  6356,  6832,
+  8995,  7253,  9416,  9892,  12055, 7771,  9934,  10410, 12573, 10831, 12994,
+  13470, 15633, 7771,  9934,  10410, 12573, 10831, 12994, 13470, 15633, 11349,
+  13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771,  9934,  10410, 12573,
+  10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048,
+  19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090,
+  17566, 19729, 17987, 20150, 20626, 22789, 8286,  10449, 10925, 13088, 11346,
+  13509, 13985, 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
+  11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081,
+  20244, 18502, 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087,
+  17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442,
+  17605, 18081, 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822,
+  22080, 24243, 24719, 26882, 8286,  10449, 10925, 13088, 11346, 13509, 13985,
   16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027,
   14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
   20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
   15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081,
   20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243,
-  24719, 26882, 8286,  10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864,
-  14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666,
-  14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141,
-  23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605,
-  18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502,
-  20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882,
-  12379, 14542, 15018, 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596,
-  20759, 19017, 21180, 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180,
-  21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957,
-  18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337,
-  22595, 24758, 25234, 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
-  27397, 23113, 25276, 25752, 27915, 26173, 28336, 28812, 30975, 8286,  10449,
-  10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666, 14924,
-  17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
-  15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027, 14503,
-  16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665,
-  21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 19020,
-  21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542, 15018, 17181,
+  24719, 26882, 12379, 14542, 15018, 17181, 15439, 17602, 18078, 20241, 15957,
+  18120, 18596, 20759, 19017, 21180, 21656, 23819, 15957, 18120, 18596, 20759,
+  19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
+  27397, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698,
+  22174, 24337, 22595, 24758, 25234, 27397, 19535, 21698, 22174, 24337, 22595,
+  24758, 25234, 27397, 23113, 25276, 25752, 27915, 26173, 28336, 28812, 30975,
+  8286,  10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503,
+  16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087,
+  17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864,
+  14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244,
+  18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141,
+  23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542,
+  15018, 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017,
+  21180, 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819,
+  19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596,
+  20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758,
+  25234, 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113,
+  25276, 25752, 27915, 26173, 28336, 28812, 30975, 12379, 14542, 15018, 17181,
   15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
   23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698,
   22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017,
   21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397,
   19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752,
-  27915, 26173, 28336, 28812, 30975, 12379, 14542, 15018, 17181, 15439, 17602,
-  18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 15957,
-  18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337,
-  22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
-  23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 19535, 21698,
-  22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752, 27915, 26173,
-  28336, 28812, 30975, 16472, 18635, 19111, 21274, 19532, 21695, 22171, 24334,
-  20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 20050, 22213, 22689,
-  24852, 23110, 25273, 25749, 27912, 23628, 25791, 26267, 28430, 26688, 28851,
-  29327, 31490, 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 23628,
-  25791, 26267, 28430, 26688, 28851, 29327, 31490, 23628, 25791, 26267, 28430,
-  26688, 28851, 29327, 31490, 27206, 29369, 29845, 32008, 30266, 32429, 32905,
-  35068
-};
+  27915, 26173, 28336, 28812, 30975, 16472, 18635, 19111, 21274, 19532, 21695,
+  22171, 24334, 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 20050,
+  22213, 22689, 24852, 23110, 25273, 25749, 27912, 23628, 25791, 26267, 28430,
+  26688, 28851, 29327, 31490, 20050, 22213, 22689, 24852, 23110, 25273, 25749,
+  27912, 23628, 25791, 26267, 28430, 26688, 28851, 29327, 31490, 23628, 25791,
+  26267, 28430, 26688, 28851, 29327, 31490, 27206, 29369, 29845, 32008, 30266,
+  32429, 32905, 35068
 #endif
+};
+
+const uint8_t av1_cat6_skipped_bits_discount[8] = {
+  0, 3, 6, 9, 12, 18, 24, 30
+};
 
 const av1_extra_bit av1_extra_bits[ENTROPY_TOKENS] = {
   { 0, 0, 0, zero_cost },                         // ZERO_TOKEN
diff --git a/av1/encoder/tokenize.h b/av1/encoder/tokenize.h
index 54b355d..d851d71 100644
--- a/av1/encoder/tokenize.h
+++ b/av1/encoder/tokenize.h
@@ -98,29 +98,13 @@
 extern const TOKENVALUE *av1_dct_cat_lt_10_value_tokens;
 extern const int *av1_dct_cat_lt_10_value_cost;
 extern const int16_t av1_cat6_low_cost[256];
-extern const int av1_cat6_high_cost[64];
-extern const int av1_cat6_high10_high_cost[256];
-extern const int av1_cat6_high12_high_cost[1024];
-static INLINE int av1_get_cost(int16_t token, EXTRABIT extrabits,
-                               const int *cat6_high_table) {
-  if (token != CATEGORY6_TOKEN)
-    return av1_extra_bits[token].cost[extrabits >> 1];
-  return av1_cat6_low_cost[(extrabits >> 1) & 0xff] +
-         cat6_high_table[extrabits >> 9];
-}
-
 #if CONFIG_AOM_HIGHBITDEPTH
-static INLINE const int *av1_get_high_cost_table(int bit_depth) {
-  return bit_depth == 8 ? av1_cat6_high_cost
-                        : (bit_depth == 10 ? av1_cat6_high10_high_cost
-                                           : av1_cat6_high12_high_cost);
-}
+#define CAT6_HIGH_COST_ENTRIES 1024
 #else
-static INLINE const int *av1_get_high_cost_table(int bit_depth) {
-  (void)bit_depth;
-  return av1_cat6_high_cost;
-}
-#endif  // CONFIG_AOM_HIGHBITDEPTH
+#define CAT6_HIGH_COST_ENTRIES 64
+#endif
+extern const int av1_cat6_high_cost[CAT6_HIGH_COST_ENTRIES];
+extern const uint8_t av1_cat6_skipped_bits_discount[8];
 
 static INLINE void av1_get_token_extra(int v, int16_t *token, EXTRABIT *extra) {
   if (v >= CAT6_MIN_VAL || v <= -CAT6_MIN_VAL) {
@@ -139,14 +123,14 @@
   return av1_dct_cat_lt_10_value_tokens[v].token;
 }
 
-static INLINE int av1_get_token_cost(int v, int16_t *token,
-                                     const int *cat6_high_table) {
+static INLINE int av1_get_token_cost(int v, int16_t *token, int cat6_bits) {
   if (v >= CAT6_MIN_VAL || v <= -CAT6_MIN_VAL) {
     EXTRABIT extrabits;
     *token = CATEGORY6_TOKEN;
     extrabits = abs(v) - CAT6_MIN_VAL;
     return av1_cat6_low_cost[extrabits & 0xff] +
-           cat6_high_table[extrabits >> 8];
+           av1_cat6_high_cost[extrabits >> 8] -
+           av1_cat6_skipped_bits_discount[18 - cat6_bits];
   }
   *token = av1_dct_cat_lt_10_value_tokens[v].token;
   return av1_dct_cat_lt_10_value_cost[v];