Palette: modify the context slightly

Use the number of pixels in a block as context, rather than the bsize
itself. The rectangular blocks therefore share the same context, e.g.
BLOCK_8X16 and BLOCK_16X8.

The number of contexts is reduced from 10 to 7.
Almost no coding performance changes.

Change-Id: Ib3241194580c2b93ad0e953957cdc9188393d055
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 4c664d2..e63bce1 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -1139,37 +1139,31 @@
 
 // TODO(huisu): tune these cdfs
 const aom_cdf_prob
-    default_palette_y_size_cdf[PALETTE_BLOCK_SIZES][CDF_SIZE(PALETTE_SIZES)] = {
+    default_palette_y_size_cdf[PALATTE_BSIZE_CTXS][CDF_SIZE(PALETTE_SIZES)] = {
       { AOM_CDF7(12288, 19408, 24627, 26662, 28499, 30667) },
       { AOM_CDF7(2815, 4570, 9416, 10875, 13782, 19863) },
-      { AOM_CDF7(3839, 5986, 11949, 13413, 16286, 21823) },
       { AOM_CDF7(12032, 14948, 22187, 23138, 24756, 27635) },
       { AOM_CDF7(14847, 20167, 25433, 26751, 28278, 30119) },
-      { AOM_CDF7(14336, 20240, 24840, 26079, 27908, 30034) },
       { AOM_CDF7(18816, 25574, 29030, 29877, 30656, 31506) },
       { AOM_CDF7(23039, 27333, 30220, 30708, 31070, 31826) },
-      { AOM_CDF7(13696, 18911, 23620, 25371, 29821, 31617) },
       { AOM_CDF7(12543, 20838, 27455, 28762, 29763, 31546) },
     };
 
-const aom_cdf_prob default_palette_uv_size_cdf[PALETTE_BLOCK_SIZES][CDF_SIZE(
-    PALETTE_SIZES)] = {
-  { AOM_CDF7(20480, 29888, 32453, 32715, 32751, 32766) },
-  { AOM_CDF7(11135, 23641, 31056, 31998, 32496, 32668) },
-  { AOM_CDF7(9216, 23108, 30806, 31871, 32414, 32637) },
-  { AOM_CDF7(9984, 21999, 29192, 30645, 31640, 32402) },
-  { AOM_CDF7(7552, 16614, 24880, 27283, 29254, 31203) },
-  { AOM_CDF7(9600, 20279, 27548, 29261, 30494, 31631) },
-  { AOM_CDF7(11391, 18656, 23727, 26058, 27788, 30278) },
-  { AOM_CDF7(8576, 13585, 17632, 20884, 23948, 27152) },
-  { AOM_CDF7(15360, 24200, 26978, 30846, 31409, 32545) },
-  { AOM_CDF7(9216, 14276, 19043, 22689, 25799, 28712) },
-};
+const aom_cdf_prob
+    default_palette_uv_size_cdf[PALATTE_BSIZE_CTXS][CDF_SIZE(PALETTE_SIZES)] = {
+      { AOM_CDF7(20480, 29888, 32453, 32715, 32751, 32766) },
+      { AOM_CDF7(11135, 23641, 31056, 31998, 32496, 32668) },
+      { AOM_CDF7(9984, 21999, 29192, 30645, 31640, 32402) },
+      { AOM_CDF7(7552, 16614, 24880, 27283, 29254, 31203) },
+      { AOM_CDF7(11391, 18656, 23727, 26058, 27788, 30278) },
+      { AOM_CDF7(8576, 13585, 17632, 20884, 23948, 27152) },
+      { AOM_CDF7(9216, 14276, 19043, 22689, 25799, 28712) },
+    };
 
 // When palette mode is enabled, following probability tables indicate the
 // probabilities to code the "is_palette" bit (i.e. the bit that indicates
 // if this block uses palette mode or DC_PRED mode).
-const aom_cdf_prob default_palette_y_mode_cdf[PALETTE_BLOCK_SIZES]
+const aom_cdf_prob default_palette_y_mode_cdf[PALATTE_BSIZE_CTXS]
                                              [PALETTE_Y_MODE_CONTEXTS]
                                              [CDF_SIZE(2)] = {
                                                { { AOM_CDF2(128 * 240) },
@@ -1193,15 +1187,6 @@
                                                { { AOM_CDF2(128 * 240) },
                                                  { AOM_CDF2(128 * 180) },
                                                  { AOM_CDF2(128 * 100) } },
-                                               { { AOM_CDF2(128 * 240) },
-                                                 { AOM_CDF2(128 * 180) },
-                                                 { AOM_CDF2(128 * 100) } },
-                                               { { AOM_CDF2(128 * 240) },
-                                                 { AOM_CDF2(128 * 180) },
-                                                 { AOM_CDF2(128 * 100) } },
-                                               { { AOM_CDF2(128 * 240) },
-                                                 { AOM_CDF2(128 * 180) },
-                                                 { AOM_CDF2(128 * 100) } },
                                              };
 
 const aom_cdf_prob
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 7665883..fb52ac3 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -148,16 +148,15 @@
 #endif  // CONFIG_EXT_WARPED_MOTION
   aom_cdf_prob obmc_cdf[BLOCK_SIZES_ALL][CDF_SIZE(2)];
   aom_prob comp_inter_prob[COMP_INTER_CONTEXTS];
-  aom_cdf_prob palette_y_size_cdf[PALETTE_BLOCK_SIZES][CDF_SIZE(PALETTE_SIZES)];
-  aom_cdf_prob palette_uv_size_cdf[PALETTE_BLOCK_SIZES]
-                                  [CDF_SIZE(PALETTE_SIZES)];
+  aom_cdf_prob palette_y_size_cdf[PALATTE_BSIZE_CTXS][CDF_SIZE(PALETTE_SIZES)];
+  aom_cdf_prob palette_uv_size_cdf[PALATTE_BSIZE_CTXS][CDF_SIZE(PALETTE_SIZES)];
   aom_cdf_prob palette_y_color_index_cdf[PALETTE_SIZES]
                                         [PALETTE_COLOR_INDEX_CONTEXTS]
                                         [CDF_SIZE(PALETTE_COLORS)];
   aom_cdf_prob palette_uv_color_index_cdf[PALETTE_SIZES]
                                          [PALETTE_COLOR_INDEX_CONTEXTS]
                                          [CDF_SIZE(PALETTE_COLORS)];
-  aom_cdf_prob palette_y_mode_cdf[PALETTE_BLOCK_SIZES][PALETTE_Y_MODE_CONTEXTS]
+  aom_cdf_prob palette_y_mode_cdf[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS]
                                  [CDF_SIZE(2)];
   aom_cdf_prob palette_uv_mode_cdf[PALETTE_UV_MODE_CONTEXTS][CDF_SIZE(2)];
   aom_cdf_prob comp_inter_cdf[COMP_INTER_CONTEXTS][CDF_SIZE(2)];
diff --git a/av1/common/enums.h b/av1/common/enums.h
index 04f4b30..a9b2060 100644
--- a/av1/common/enums.h
+++ b/av1/common/enums.h
@@ -70,15 +70,19 @@
 #endif
 
 #define MIN_PALETTE_BSIZE BLOCK_8X8
-#define MAX_PALETTE_BSIZE BLOCK_64X64
 #define MAX_PALETTE_SQUARE (64 * 64)
+
 // Maximum number of colors in a palette.
 #define PALETTE_MAX_SIZE 8
 // Minimum number of colors in a palette.
 #define PALETTE_MIN_SIZE 2
-// Palette mode is available for block sizes between MIN_PALETTE_BSIZE and
-// MAX_PALETTE_BSIZE.
-#define PALETTE_BLOCK_SIZES (MAX_PALETTE_BSIZE - MIN_PALETTE_BSIZE + 1)
+// Map the number of pixels in a block size to a context
+//   64(BLOCK_8X8)              -> 0
+//  128(BLOCK_16X8, BLOCK_16X8) -> 1
+//  256(BLOCK_16X16)            -> 2
+//  ...
+// 4096(BLOCK_64X64)            -> 6
+#define PALATTE_BSIZE_CTXS 7
 
 #if CONFIG_FRAME_MARKER
 #define FRAME_OFFSET_BITS 5
diff --git a/av1/common/pred_common.h b/av1/common/pred_common.h
index f781987..010f1d4 100644
--- a/av1/common/pred_common.h
+++ b/av1/common/pred_common.h
@@ -184,6 +184,10 @@
 int av1_get_palette_cache(const MACROBLOCKD *const xd, int plane,
                           uint16_t *cache);
 
+static INLINE int av1_get_palette_bsize_ctx(BLOCK_SIZE bsize) {
+  return num_pels_log2_lookup[bsize] - num_pels_log2_lookup[MIN_PALETTE_BSIZE];
+}
+
 int av1_get_intra_inter_context(const MACROBLOCKD *xd);
 
 int av1_get_reference_mode_context(const AV1_COMMON *cm, const MACROBLOCKD *xd);
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index ed17569..f7e86c7 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -822,7 +822,7 @@
   const BLOCK_SIZE bsize = mbmi->sb_type;
   assert(av1_allow_palette(cm->allow_screen_content_tools, bsize));
   PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
-  const int block_palette_idx = bsize - BLOCK_8X8;
+  const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
 
   if (mbmi->mode == DC_PRED) {
     int palette_y_mode_ctx = 0;
@@ -835,13 +835,11 @@
           (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
     }
     const int modev = aom_read_symbol(
-        r,
-        xd->tile_ctx->palette_y_mode_cdf[block_palette_idx][palette_y_mode_ctx],
-        2, ACCT_STR);
+        r, xd->tile_ctx->palette_y_mode_cdf[bsize_ctx][palette_y_mode_ctx], 2,
+        ACCT_STR);
     if (modev) {
       pmi->palette_size[0] =
-          aom_read_symbol(r,
-                          xd->tile_ctx->palette_y_size_cdf[block_palette_idx],
+          aom_read_symbol(r, xd->tile_ctx->palette_y_size_cdf[bsize_ctx],
                           PALETTE_SIZES, ACCT_STR) +
           2;
       read_palette_colors_y(xd, cm->bit_depth, pmi, r);
@@ -853,8 +851,7 @@
         r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2, ACCT_STR);
     if (modev) {
       pmi->palette_size[1] =
-          aom_read_symbol(r,
-                          xd->tile_ctx->palette_uv_size_cdf[block_palette_idx],
+          aom_read_symbol(r, xd->tile_ctx->palette_uv_size_cdf[bsize_ctx],
                           PALETTE_SIZES, ACCT_STR) +
           2;
       read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 11545ed..db7fd99 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1023,7 +1023,7 @@
   const BLOCK_SIZE bsize = mbmi->sb_type;
   assert(av1_allow_palette(cm->allow_screen_content_tools, bsize));
   const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
-  const int block_palette_idx = bsize - BLOCK_8X8;
+  const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
 
   if (mbmi->mode == DC_PRED) {
     const int n = pmi->palette_size[0];
@@ -1038,11 +1038,10 @@
     }
     aom_write_symbol(
         w, n > 0,
-        xd->tile_ctx->palette_y_mode_cdf[block_palette_idx][palette_y_mode_ctx],
-        2);
+        xd->tile_ctx->palette_y_mode_cdf[bsize_ctx][palette_y_mode_ctx], 2);
     if (n > 0) {
       aom_write_symbol(w, n - PALETTE_MIN_SIZE,
-                       xd->tile_ctx->palette_y_size_cdf[block_palette_idx],
+                       xd->tile_ctx->palette_y_size_cdf[bsize_ctx],
                        PALETTE_SIZES);
       write_palette_colors_y(xd, pmi, cm->bit_depth, w);
     }
@@ -1060,7 +1059,7 @@
                      xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2);
     if (n > 0) {
       aom_write_symbol(w, n - PALETTE_MIN_SIZE,
-                       xd->tile_ctx->palette_uv_size_cdf[block_palette_idx],
+                       xd->tile_ctx->palette_uv_size_cdf[bsize_ctx],
                        PALETTE_SIZES);
       write_palette_colors_uv(xd, pmi, cm->bit_depth, w);
     }
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 4489913..1e83ad6 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -305,13 +305,13 @@
 #else
   int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
 #endif  // CONFIG_EXT_PARTITION_TYPES
-  int palette_y_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
-  int palette_uv_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
+  int palette_y_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
+  int palette_uv_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
   int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
                           [PALETTE_COLORS];
   int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
                            [PALETTE_COLORS];
-  int palette_y_mode_cost[PALETTE_BLOCK_SIZES][PALETTE_Y_MODE_CONTEXTS][2];
+  int palette_y_mode_cost[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
   int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
 #if CONFIG_CFL
   // The rate associated with each alpha codeword
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 635a992..a00b4bd 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -4220,7 +4220,7 @@
   const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
 
   assert(bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST);
-  const int block_palette_idx = bsize - BLOCK_8X8;
+  const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
 
   if (mbmi->mode == DC_PRED) {
     const int n = pmi->palette_size[0];
@@ -4233,8 +4233,7 @@
       palette_y_mode_ctx +=
           (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
     }
-    update_cdf(fc->palette_y_mode_cdf[block_palette_idx][palette_y_mode_ctx],
-               n > 0, 2);
+    update_cdf(fc->palette_y_mode_cdf[bsize_ctx][palette_y_mode_ctx], n > 0, 2);
   }
 
   if (mbmi->uv_mode == UV_DC_PRED) {
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 710df4f..601d1b6 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -130,7 +130,7 @@
     av1_cost_tokens_from_cdf(x->switchable_interp_costs[i],
                              fc->switchable_interp_cdf[i], NULL);
 
-  for (i = 0; i < PALETTE_BLOCK_SIZES; ++i) {
+  for (i = 0; i < PALATTE_BSIZE_CTXS; ++i) {
     av1_cost_tokens_from_cdf(x->palette_y_size_cost[i],
                              fc->palette_y_size_cdf[i], NULL);
     av1_cost_tokens_from_cdf(x->palette_uv_size_cost[i],
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 5e62b17..5e93d53 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -2855,11 +2855,11 @@
                            &cols);
   av1_calc_indices(data, centroids, color_map, rows * cols, k, 1);
   extend_palette_color_map(color_map, cols, rows, block_width, block_height);
+  const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
   int palette_mode_cost =
-      dc_mode_cost +
-      x->palette_y_size_cost[bsize - BLOCK_8X8][k - PALETTE_MIN_SIZE] +
+      dc_mode_cost + x->palette_y_size_cost[bsize_ctx][k - PALETTE_MIN_SIZE] +
       write_uniform_cost(k, color_map[0]) +
-      x->palette_y_mode_cost[bsize - BLOCK_8X8][palette_ctx][1];
+      x->palette_y_mode_cost[bsize_ctx][palette_ctx][1];
   palette_mode_cost += av1_palette_color_cost_y(pmi, color_cache, n_cache,
                                                 cpi->common.bit_depth);
   palette_mode_cost +=
@@ -3491,8 +3491,8 @@
           tx_size_cost(&cpi->common, x, bsize, mbmi->tx_size);
     }
     if (try_palette && mbmi->mode == DC_PRED) {
-      this_rate +=
-          x->palette_y_mode_cost[bsize - BLOCK_8X8][palette_y_mode_ctx][0];
+      const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
+      this_rate += x->palette_y_mode_cost[bsize_ctx][palette_y_mode_ctx][0];
     }
 #if CONFIG_FILTER_INTRA
     if (mbmi->mode == DC_PRED && av1_filter_intra_allowed_txsize(mbmi->tx_size))
@@ -5251,11 +5251,11 @@
 
       super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd);
       if (tokenonly_rd_stats.rate == INT_MAX) continue;
-      this_rate =
-          tokenonly_rd_stats.rate + dc_mode_cost +
-          x->palette_uv_size_cost[bsize - BLOCK_8X8][n - PALETTE_MIN_SIZE] +
-          write_uniform_cost(n, color_map[0]) +
-          x->palette_uv_mode_cost[pmi->palette_size[0] > 0][1];
+      const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
+      this_rate = tokenonly_rd_stats.rate + dc_mode_cost +
+                  x->palette_uv_size_cost[bsize_ctx][n - PALETTE_MIN_SIZE] +
+                  write_uniform_cost(n, color_map[0]) +
+                  x->palette_uv_mode_cost[pmi->palette_size[0] > 0][1];
       this_rate += av1_palette_color_cost_uv(pmi, color_cache, n_cache,
                                              cpi->common.bit_depth);
       this_rate += av1_cost_color_map(x, 1, bsize, mbmi->tx_size, PALETTE_MAP);
@@ -9888,7 +9888,8 @@
         rate2 += rate_uv + x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode];
 
       if (try_palette && mbmi->mode == DC_PRED) {
-        rate2 += x->palette_y_mode_cost[bsize - BLOCK_8X8][palette_ctx][0];
+        const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
+        rate2 += x->palette_y_mode_cost[bsize_ctx][palette_ctx][0];
       }
 
       if (!xd->lossless[mbmi->segment_id] && block_signals_txsize(bsize)) {