Compute tx_type_cost in optimize_txb

Change-Id: I3387ee20382d3b9b65f33e4e55577fc23d032ad8
diff --git a/av1/encoder/encodemb.c b/av1/encoder/encodemb.c
index 006107b..a2c2c75 100644
--- a/av1/encoder/encodemb.c
+++ b/av1/encoder/encodemb.c
@@ -102,11 +102,14 @@
   MACROBLOCKD *const xd = &mb->e_mbd;
   struct macroblock_plane *const p = &mb->plane[plane];
   const int eob = p->eobs[block];
-  if (eob == 0 || !mb->optimize || xd->lossless[xd->mi[0]->mbmi.segment_id])
-    return eob;
-
   TXB_CTX txb_ctx;
   get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
+
+  if (eob == 0 || !mb->optimize || xd->lossless[xd->mi[0]->mbmi.segment_id]) {
+    *rate_cost = av1_cost_skip_txb(mb, &txb_ctx, plane, tx_size);
+    return eob;
+  }
+
   return av1_optimize_txb(cpi, mb, plane, blk_row, blk_col, block, tx_size,
                           &txb_ctx, fast_mode, rate_cost);
 }
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 2d73b66..7447c77 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -906,6 +906,11 @@
   *rate_cost = zero_blk_rd_cost <= prev_eob_rd_cost
                    ? zero_blk_rate
                    : accu_rate + non_zero_blk_rate;
+
+  if (txb_info->eob > 0) {
+    *rate_cost += txb_info->tx_type_cost;
+  }
+
   return update;
 }
 
@@ -1224,11 +1229,13 @@
           ? pd->seg_iqmatrix[mbmi->segment_id][qm_tx_size]
           : cm->giqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size];
   assert(width == (1 << bwl));
+  const int tx_type_cost =
+      av1_tx_type_cost(cm, x, xd, mbmi->sb_type, plane, tx_size, tx_type);
   TxbInfo txb_info = {
-    qcoeff,   levels,  dqcoeff,    tcoeff,  dequant, shift,
-    tx_size,  txs_ctx, tx_type,    bwl,     width,   height,
-    eob,      seg_eob, scan_order, txb_ctx, rdmult,  &cm->coeff_ctx_table,
-    iqmatrix,
+    qcoeff,   levels,       dqcoeff,    tcoeff,  dequant, shift,
+    tx_size,  txs_ctx,      tx_type,    bwl,     width,   height,
+    eob,      seg_eob,      scan_order, txb_ctx, rdmult,  &cm->coeff_ctx_table,
+    iqmatrix, tx_type_cost,
   };
 
   // Hash based trellis (hbt) speed feature: avoid expensive optimize_txb calls
diff --git a/av1/encoder/encodetxb.h b/av1/encoder/encodetxb.h
index 2fcdc03..6ad5fec 100644
--- a/av1/encoder/encodetxb.h
+++ b/av1/encoder/encodetxb.h
@@ -43,6 +43,7 @@
   int64_t rdmult;
   const LV_MAP_CTX_TABLE *coeff_ctx_table;
   const qm_val_t *iqmatrix;
+  int tx_type_cost;
 } TxbInfo;
 
 void av1_alloc_txb_buf(AV1_COMP *cpi);
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 29a715e..20e232f 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -1966,14 +1966,6 @@
                       tx_size, AV1_XFORM_QUANT_FP);
       av1_optimize_b(cpi, x, plane, blk_row, blk_col, block, plane_bsize,
                      tx_size, a, l, 1, &rate_cost);
-      const int eob = x->plane[plane].eobs[block];
-      if (eob)
-        rate_cost +=
-            av1_tx_type_cost(cm, x, xd, mbmi->sb_type, plane, tx_size, tx_type);
-      else
-        rate_cost =
-            av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block, tx_size,
-                            scan_order, a, l, use_fast_coef_costing);
     }
     av1_dist_block(cpi, x, plane, plane_bsize, block, blk_row, blk_col, tx_size,
                    &this_rd_stats.dist, &this_rd_stats.sse,
diff --git a/av1/encoder/rdopt.h b/av1/encoder/rdopt.h
index a31d396..288b9ea 100644
--- a/av1/encoder/rdopt.h
+++ b/av1/encoder/rdopt.h
@@ -78,6 +78,15 @@
                      int bsh, int visible_w, int visible_h, int qindex);
 #endif
 
+static INLINE int av1_cost_skip_txb(MACROBLOCK *x, TXB_CTX *txb_ctx, int plane,
+                                    TX_SIZE tx_size) {
+  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
+  const PLANE_TYPE plane_type = get_plane_type(plane);
+  const LV_MAP_COEFF_COST *const coeff_costs =
+      &x->coeff_costs[txs_ctx][plane_type];
+  return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
+}
+
 static INLINE int av1_cost_coeffs(const struct AV1_COMP *const cpi,
                                   MACROBLOCK *x, int plane, int blk_row,
                                   int blk_col, int block, TX_SIZE tx_size,
@@ -105,11 +114,7 @@
     cost = av1_cost_coeffs_txb(cm, x, plane, blk_row, blk_col, block, tx_size,
                                &txb_ctx);
   } else {
-    const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
-    const PLANE_TYPE plane_type = get_plane_type(plane);
-    const LV_MAP_COEFF_COST *const coeff_costs =
-        &x->coeff_costs[txs_ctx][plane_type];
-    cost = coeff_costs->txb_skip_cost[txb_ctx.txb_skip_ctx][1];
+    cost = av1_cost_skip_txb(x, &txb_ctx, plane, tx_size);
   }
 #if TXCOEFF_COST_TIMER
   AV1_COMMON *tmp_cm = (AV1_COMMON *)&cpi->common;