Do entropy coding on eob_extra

Change-Id: Ia67beabb85eedd5da5eb69f434dde71f66f08006
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index c534679..15636d5 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -168,6 +168,10 @@
                          [CDF_SIZE(2)];
   aom_cdf_prob eob_flag_cdf[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS]
                            [CDF_SIZE(2)];
+#if CONFIG_EOB_FIRST
+  aom_cdf_prob eob_extra_cdf[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS]
+                            [CDF_SIZE(2)];
+#endif
   aom_cdf_prob dc_sign_cdf[PLANE_TYPES][DC_SIGN_CONTEXTS][CDF_SIZE(2)];
   aom_cdf_prob coeff_base_cdf[TX_SIZES][PLANE_TYPES][NUM_BASE_LEVELS]
                              [COEFF_BASE_CONTEXTS][CDF_SIZE(2)];
diff --git a/av1/common/txb_common.c b/av1/common/txb_common.c
index 216cb12..d7c0328 100644
--- a/av1/common/txb_common.c
+++ b/av1/common/txb_common.c
@@ -149,6 +149,14 @@
         fc->eob_flag_cdf[tx_size][plane][ctx][1] = AOM_ICDF(32768);
         fc->eob_flag_cdf[tx_size][plane][ctx][2] = 0;
       }
+
+#if CONFIG_EOB_FIRST
+      for (ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx) {
+        fc->eob_extra_cdf[tx_size][plane][ctx][0] = AOM_ICDF(128 * 145);
+        fc->eob_flag_cdf[tx_size][plane][ctx][1] = AOM_ICDF(32768);
+        fc->eob_flag_cdf[tx_size][plane][ctx][2] = 0;
+      }
+#endif
     }
   }
 
diff --git a/av1/decoder/decodetxb.c b/av1/decoder/decodetxb.c
index 76e5066..f4f7328 100644
--- a/av1/decoder/decodetxb.c
+++ b/av1/decoder/decodetxb.c
@@ -370,9 +370,17 @@
 
   // printf("Dec: ");
   if (k_eob_offset_bits[eob_pt] > 0) {
-    for (int i = 0; i < k_eob_offset_bits[eob_pt]; i++) {
-      int eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
-      int bit = av1_read_record_bit(counts, r, ACCT_STR);
+    int eob_shift = k_eob_offset_bits[eob_pt] - 1;
+    int bit = av1_read_record_bin(
+        counts, r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_pt], 2,
+        ACCT_STR);
+    if (bit) {
+      eob_extra += (1 << eob_shift);
+    }
+
+    for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) {
+      eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
+      bit = av1_read_record_bit(counts, r, ACCT_STR);
       if (bit) {
         eob_extra += (1 << eob_shift);
       }
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 12c5ed2..30a1ef8 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -60,6 +60,9 @@
   int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
   int nz_map_cost[SIG_COEF_CONTEXTS][2];
   int eob_cost[EOB_COEF_CONTEXTS][2];
+#if CONFIG_EOB_FIRST
+  int eob_extra_cost[EOB_COEF_CONTEXTS][2];
+#endif
   int dc_sign_cost[DC_SIGN_CONTEXTS][2];
   int base_cost[NUM_BASE_LEVELS][COEFF_BASE_CONTEXTS][2];
 #if BR_NODE
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index cb05d43..24b1a3e 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -152,6 +152,14 @@
       break;
     }
   }
+  if (k_eob_offset_bits[eob_pt] > 0) {
+    int eob_shift = k_eob_offset_bits[eob_pt] - 1;
+    int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
+// TODO(angiebird): update count as well
+#if LV_MAP_PROB
+    update_cdf(ec_ctx->eob_extra_cdf[txsize][plane][eob_pt], bit, 2);
+#endif
+  }
 }
 
 static int get_eob_cost(int eob, int seg_eob,
@@ -171,9 +179,12 @@
     }
   }
   if (k_eob_offset_bits[eob_pt] > 0) {
-    for (int i = 0; i < k_eob_offset_bits[eob_pt]; i++) {
-      int eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
-      int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
+    int eob_shift = k_eob_offset_bits[eob_pt] - 1;
+    int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
+    eob_cost += txb_costs->eob_extra_cost[eob_pt][bit];
+    for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) {
+      eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
+      bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
       eob_cost += av1_cost_bit(128, bit);
     }
   }
@@ -475,9 +486,13 @@
   }
 
   if (k_eob_offset_bits[eob_pt] > 0) {
-    for (int i = 0; i < k_eob_offset_bits[eob_pt]; i++) {
-      int eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
-      int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
+    int eob_shift = k_eob_offset_bits[eob_pt] - 1;
+    int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
+    aom_write_bin(w, bit, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_pt],
+                  2);
+    for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) {
+      eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
+      bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
       aom_write_bit(w, bit);
       // printf("%d ", bit);
     }
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 8c69a44..9da59fd 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -608,6 +608,12 @@
         av1_cost_tokens_from_cdf(pcost->eob_cost[ctx],
                                  fc->eob_flag_cdf[tx_size][plane][ctx], NULL);
 
+#if CONFIG_EOB_FIRST
+      for (int ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx)
+        av1_cost_tokens_from_cdf(pcost->eob_extra_cost[ctx],
+                                 fc->eob_extra_cdf[tx_size][plane][ctx], NULL);
+#endif
+
       for (int ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
         av1_cost_tokens_from_cdf(pcost->dc_sign_cost[ctx],
                                  fc->dc_sign_cdf[plane][ctx], NULL);