Make var-tx work with the re-designed cb4x4

Resolve tokenization and bit-stream packing issues in var-tx
when the re-designed cb4x4 is enabled.

Change-Id: I651f776104679eb7d4d0681f428490f668dc806a
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 5c5599f..e958d41 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2333,9 +2333,14 @@
       const struct macroblockd_plane *const pd = &xd->plane[plane];
       BLOCK_SIZE bsize = mbmi->sb_type;
 #if CONFIG_CB4X4
+#if CONFIG_CHROMA_2X2
       const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
 #else
       const BLOCK_SIZE plane_bsize =
+          AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
+#endif
+#else
+      const BLOCK_SIZE plane_bsize =
           get_plane_block_size(AOMMAX(bsize, BLOCK_8X8), pd);
 #endif
 
@@ -2374,6 +2379,9 @@
 #endif  // CONFIG_RD_DEBUG
       } else {
         TX_SIZE tx = get_tx_size(plane, xd);
+#if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
+        tx = AOMMAX(TX_4X4, tx);
+#endif
         const int bkw = tx_size_wide_unit[tx];
         const int bkh = tx_size_high_unit[tx];
         for (row = 0; row < num_4x4_h; row += bkh) {
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index 44b09e8..f14410e 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -710,8 +710,24 @@
     *t = t_backup;
 
   for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
+#if CONFIG_CB4X4
+    if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col)) {
+#if !CONFIG_PVQ
+      if (!dry_run) {
+        (*t)->token = EOSB_TOKEN;
+        (*t)++;
+      }
+#endif
+      continue;
+    }
+#endif
     const struct macroblockd_plane *const pd = &xd->plane[plane];
+#if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
+    const BLOCK_SIZE plane_bsize =
+        AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
+#else
     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
+#endif
     const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
     const int mi_height = block_size_high[plane_bsize] >> tx_size_wide_log2[0];
     const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];