[NORMATIVE]Pack txfm coding info closer

This commit re-organizes the block coding operations. It codes
the prediction information including palette token coding first.
The transform block partition, type, and the coefficients are
handled next. The filter_intra info is processed after the txfm
block size has been coded due to its dependency on the tx size.

The tx size coding functions for key frame and intra block in
inter frame are unified.

BUG=aomedia:1113

Change-Id: I18e68aebaf0a63d35206146f0393a1113ac5f3cb
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index fc10945..004e4b7 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1129,26 +1129,6 @@
 #endif  // CONFIG_EXT_SKIP
     write_is_inter(cm, xd, mbmi->segment_id, w, is_inter);
 
-  if (cm->tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
-      !(is_inter && skip) && !xd->lossless[segment_id]) {
-    if (is_inter) {  // This implies skip flag is 0.
-      const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, bsize, 0);
-      const int bh = tx_size_high_unit[max_tx_size];
-      const int bw = tx_size_wide_unit[max_tx_size];
-      const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
-      const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
-      int idx, idy;
-      for (idy = 0; idy < height; idy += bh)
-        for (idx = 0; idx < width; idx += bw)
-          write_tx_size_vartx(cm, xd, mbmi, max_tx_size, 0, idy, idx, w);
-    } else {
-      write_selected_tx_size(cm, xd, w);
-      set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, skip, xd);
-    }
-  } else {
-    set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, skip, xd);
-  }
-
 #if CONFIG_EXT_SKIP
   if (mbmi->skip_mode) return;
 #endif  // CONFIG_EXT_SKIP
@@ -1181,9 +1161,6 @@
 
     if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
       write_palette_mode_info(cm, xd, mi, mi_row, mi_col, w);
-#if CONFIG_FILTER_INTRA
-    write_filter_intra_mode_info(xd, mbmi, w);
-#endif  // CONFIG_FILTER_INTRA
   } else {
     int16_t mode_ctx;
 
@@ -1354,7 +1331,8 @@
 
 static void write_intrabc_info(AV1_COMMON *cm, MACROBLOCKD *xd,
                                const MB_MODE_INFO_EXT *mbmi_ext,
-                               int enable_tx_size, aom_writer *w) {
+                               aom_writer *w) {
+  (void)cm;
   const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
   int use_intrabc = is_intrabc_block(mbmi);
   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
@@ -1362,22 +1340,6 @@
   if (use_intrabc) {
     assert(mbmi->mode == DC_PRED);
     assert(mbmi->uv_mode == UV_DC_PRED);
-    if ((enable_tx_size && !mbmi->skip)) {
-      const BLOCK_SIZE bsize = mbmi->sb_type;
-      const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, bsize, 0);
-      const int bh = tx_size_high_unit[max_tx_size];
-      const int bw = tx_size_wide_unit[max_tx_size];
-      const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
-      const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
-      int idx, idy;
-      for (idy = 0; idy < height; idy += bh) {
-        for (idx = 0; idx < width; idx += bw) {
-          write_tx_size_vartx(cm, xd, mbmi, max_tx_size, 0, idy, idx, w);
-        }
-      }
-    } else {
-      set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
-    }
     int_mv dv_ref = mbmi_ext->ref_mvs[INTRA_FRAME][0];
     av1_encode_dv(w, &mbmi->mv[0].as_mv, &dv_ref.as_mv, &ec_ctx->ndvc);
 #if !CONFIG_TXK_SEL
@@ -1452,19 +1414,11 @@
     }
   }
 
-  int enable_tx_size = cm->tx_mode == TX_MODE_SELECT &&
-                       block_signals_txsize(bsize) &&
-                       !xd->lossless[mbmi->segment_id];
-
   if (av1_allow_intrabc(cm)) {
-    write_intrabc_info(cm, xd, mbmi_ext, enable_tx_size, w);
+    write_intrabc_info(cm, xd, mbmi_ext, w);
     if (is_intrabc_block(mbmi)) return;
   }
 
-  if (enable_tx_size) write_selected_tx_size(cm, xd, w);
-
-  set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
-
   write_intra_mode_kf(ec_ctx, mi, above_mi, left_mi, mode, w);
 
   const int use_angle_delta = av1_use_angle_delta(bsize);
@@ -1492,9 +1446,6 @@
 
   if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
     write_palette_mode_info(cm, xd, mi, mi_row, mi_col, w);
-#if CONFIG_FILTER_INTRA
-  write_filter_intra_mode_info(xd, mbmi, w);
-#endif  // CONFIG_FILTER_INTRA
 
 #if !CONFIG_TXK_SEL
   av1_write_tx_type(cm, xd, w);
@@ -1727,23 +1678,6 @@
 #endif  // CONFIG_DEPENDENT_HORZTILES
                  cm->mi_rows, cm->mi_cols);
 
-  for (plane = 0; plane < AOMMIN(2, num_planes); ++plane) {
-    const uint8_t palette_size_plane =
-        mbmi->palette_mode_info.palette_size[plane];
-#if CONFIG_EXT_SKIP
-    assert(!mbmi->skip_mode || !palette_size_plane);
-#endif  // CONFIG_EXT_SKIP
-    if (palette_size_plane > 0) {
-      assert(mbmi->use_intrabc == 0);
-      assert(av1_allow_palette(cm->allow_screen_content_tools, mbmi->sb_type));
-      int rows, cols;
-      av1_get_block_dimensions(mbmi->sb_type, plane, xd, NULL, NULL, &rows,
-                               &cols);
-      assert(*tok < tok_end);
-      pack_map_tokens(w, tok, palette_size_plane, rows * cols);
-    }
-  }
-
   if (!mbmi->skip) {
     if (!is_inter_block(mbmi))
       av1_write_coeffs_mb(cm, x, mi_row, mi_col, w, mbmi->sb_type);
@@ -1799,6 +1733,54 @@
                           int mi_col) {
   write_mbmi_b(cpi, tile, w, mi_row, mi_col);
 
+  AV1_COMMON *cm = &cpi->common;
+  MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
+  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
+  for (int plane = 0; plane < AOMMIN(2, av1_num_planes(cm)); ++plane) {
+    const uint8_t palette_size_plane =
+        mbmi->palette_mode_info.palette_size[plane];
+#if CONFIG_EXT_SKIP
+    assert(!mbmi->skip_mode || !palette_size_plane);
+#endif  // CONFIG_EXT_SKIP
+    if (palette_size_plane > 0) {
+      assert(mbmi->use_intrabc == 0);
+      assert(av1_allow_palette(cm->allow_screen_content_tools, mbmi->sb_type));
+      int rows, cols;
+      av1_get_block_dimensions(mbmi->sb_type, plane, xd, NULL, NULL, &rows,
+                               &cols);
+      assert(*tok < tok_end);
+      pack_map_tokens(w, tok, palette_size_plane, rows * cols);
+    }
+  }
+
+  BLOCK_SIZE bsize = mbmi->sb_type;
+  int is_inter_tx = is_inter_block(mbmi) || is_intrabc_block(mbmi);
+  int skip = mbmi->skip;
+  int segment_id = mbmi->segment_id;
+  if (cm->tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
+      !(is_inter_tx && skip) && !xd->lossless[segment_id]) {
+    if (is_inter_tx) {  // This implies skip flag is 0.
+      const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, bsize, 0);
+      const int txbh = tx_size_high_unit[max_tx_size];
+      const int txbw = tx_size_wide_unit[max_tx_size];
+      const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
+      const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
+      int idx, idy;
+      for (idy = 0; idy < height; idy += txbh)
+        for (idx = 0; idx < width; idx += txbw)
+          write_tx_size_vartx(cm, xd, mbmi, max_tx_size, 0, idy, idx, w);
+    } else {
+      write_selected_tx_size(cm, xd, w);
+      set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, skip, xd);
+    }
+  } else {
+    set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, skip, xd);
+  }
+
+#if CONFIG_FILTER_INTRA
+  if (!is_inter_tx) write_filter_intra_mode_info(xd, mbmi, w);
+#endif  // CONFIG_FILTER_INTRA
+
   write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
 }