NEW_MULTISYMBOL: adapt comp_ref and bwd_ref.

Change-Id: I711cd173af501ba955e889d1e2205125615a99fd
diff --git a/av1/common/entropy.c b/av1/common/entropy.c
index 18ff63f..115a3db 100644
--- a/av1/common/entropy.c
+++ b/av1/common/entropy.c
@@ -5692,9 +5692,10 @@
 
 #if CONFIG_NEW_MULTISYMBOL
   AVERAGE_TILE_CDFS(comp_inter_cdf)
+#if CONFIG_EXT_REFS
+  AVERAGE_TILE_CDFS(comp_bwdref_cdf)
 #endif
-
-// FIXME: comp_ref_cdf and comp_bwd_ref not defined
+#endif
 
 #if CONFIG_NEW_MULTISYMBOL
   AVERAGE_TILE_CDFS(single_ref_cdf)
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 9177e9a..a8af114 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -1428,10 +1428,49 @@
 static const aom_prob default_comp_bwdref_p[REF_CONTEXTS][BWD_REFS - 1] = {
   { 16 }, { 74 }, { 142 }, { 170 }, { 247 }
 };
+#if CONFIG_NEW_MULTISYMBOL
+static const aom_cdf_prob
+    default_comp_ref_cdf[REF_CONTEXTS][FWD_REFS - 1][CDF_SIZE(2)] = {
+      // TODO(zoeliu): To adjust the initial prob values.
+      { { AOM_ICDF(33 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(16 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(16 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(77 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(74 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(74 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(142 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(142 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(142 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(172 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(170 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(170 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(238 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(247 * 128), AOM_ICDF(32768), 0 },
+        { AOM_ICDF(247 * 128), AOM_ICDF(32768), 0 } }
+    };
+static const aom_cdf_prob
+    default_comp_bwdref_cdf[REF_CONTEXTS][BWD_REFS - 1][CDF_SIZE(2)] = {
+      { { AOM_ICDF(16 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(74 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(142 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(170 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(247 * 128), AOM_ICDF(32768), 0 } }
+    };
+#endif  // CONFIG_NEW_MULTISYMBOL
 #else
 static const aom_prob default_comp_ref_p[REF_CONTEXTS][COMP_REFS - 1] = {
   { 50 }, { 126 }, { 123 }, { 221 }, { 226 }
 };
+#if CONFIG_NEW_MULTISYMBOL
+static const aom_cdf_prob
+    default_comp_ref_cdf[REF_CONTEXTS][COMP_REFS - 1][CDF_SIZE(2)] = {
+      { { AOM_ICDF(50 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(126 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(123 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(221 * 128), AOM_ICDF(32768), 0 } },
+      { { AOM_ICDF(226 * 128), AOM_ICDF(32768), 0 } }
+    };
+#endif  // CONFIG_NEW_MULTISYMBOL
 #endif  // CONFIG_EXT_REFS
 
 static const aom_prob default_single_ref_p[REF_CONTEXTS][SINGLE_REFS - 1] = {
@@ -4605,6 +4644,9 @@
   av1_copy(fc->uni_comp_ref_prob, default_uni_comp_ref_p);
 #endif  // CONFIG_EXT_COMP_REFS
   av1_copy(fc->comp_ref_prob, default_comp_ref_p);
+#if CONFIG_NEW_MULTISYMBOL
+  av1_copy(fc->comp_ref_cdf, default_comp_ref_cdf);
+#endif
 #if CONFIG_LV_MAP
   av1_copy(fc->txb_skip, default_txb_skip);
   av1_copy(fc->nz_map, default_nz_map);
@@ -4615,6 +4657,9 @@
 #endif
 #if CONFIG_EXT_REFS
   av1_copy(fc->comp_bwdref_prob, default_comp_bwdref_p);
+#if CONFIG_NEW_MULTISYMBOL
+  av1_copy(fc->comp_bwdref_cdf, default_comp_bwdref_cdf);
+#endif
 #endif  // CONFIG_EXT_REFS
   av1_copy(fc->single_ref_prob, default_single_ref_p);
 #if CONFIG_NEW_MULTISYMBOL
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index efe2693..06b70c2 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -234,6 +234,14 @@
 #else
   aom_prob comp_ref_prob[REF_CONTEXTS][COMP_REFS - 1];
 #endif  // CONFIG_EXT_REFS
+#if CONFIG_NEW_MULTISYMBOL
+#if CONFIG_EXT_REFS
+  aom_cdf_prob comp_ref_cdf[REF_CONTEXTS][FWD_REFS - 1][CDF_SIZE(2)];
+  aom_cdf_prob comp_bwdref_cdf[REF_CONTEXTS][BWD_REFS - 1][CDF_SIZE(2)];
+#else
+  aom_cdf_prob comp_ref_cdf[REF_CONTEXTS][COMP_REFS - 1][CDF_SIZE(2)];
+#endif  // CONFIG_EXT_REFS
+#endif
 #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
   aom_prob comp_inter_mode_prob[COMP_INTER_MODE_CONTEXTS];
 #endif  // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
diff --git a/av1/common/pred_common.h b/av1/common/pred_common.h
index 9a32815..2b17b84 100644
--- a/av1/common/pred_common.h
+++ b/av1/common/pred_common.h
@@ -140,6 +140,14 @@
 int av1_get_pred_context_comp_ref_p(const AV1_COMMON *cm,
                                     const MACROBLOCKD *xd);
 
+#if CONFIG_NEW_MULTISYMBOL
+static INLINE aom_cdf_prob *av1_get_pred_cdf_comp_ref_p(const AV1_COMMON *cm,
+                                                        const MACROBLOCKD *xd) {
+  const int pred_context = av1_get_pred_context_comp_ref_p(cm, xd);
+  return xd->tile_ctx->comp_ref_cdf[pred_context][0];
+}
+#endif
+
 static INLINE aom_prob av1_get_pred_prob_comp_ref_p(const AV1_COMMON *cm,
                                                     const MACROBLOCKD *xd) {
   const int pred_context = av1_get_pred_context_comp_ref_p(cm, xd);
@@ -150,6 +158,14 @@
 int av1_get_pred_context_comp_ref_p1(const AV1_COMMON *cm,
                                      const MACROBLOCKD *xd);
 
+#if CONFIG_NEW_MULTISYMBOL
+static INLINE aom_cdf_prob *av1_get_pred_cdf_comp_ref_p1(
+    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
+  const int pred_context = av1_get_pred_context_comp_ref_p1(cm, xd);
+  return xd->tile_ctx->comp_ref_cdf[pred_context][1];
+}
+#endif
+
 static INLINE aom_prob av1_get_pred_prob_comp_ref_p1(const AV1_COMMON *cm,
                                                      const MACROBLOCKD *xd) {
   const int pred_context = av1_get_pred_context_comp_ref_p1(cm, xd);
@@ -159,6 +175,14 @@
 int av1_get_pred_context_comp_ref_p2(const AV1_COMMON *cm,
                                      const MACROBLOCKD *xd);
 
+#if CONFIG_NEW_MULTISYMBOL
+static INLINE aom_cdf_prob *av1_get_pred_cdf_comp_ref_p2(
+    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
+  const int pred_context = av1_get_pred_context_comp_ref_p2(cm, xd);
+  return xd->tile_ctx->comp_ref_cdf[pred_context][2];
+}
+#endif
+
 static INLINE aom_prob av1_get_pred_prob_comp_ref_p2(const AV1_COMMON *cm,
                                                      const MACROBLOCKD *xd) {
   const int pred_context = av1_get_pred_context_comp_ref_p2(cm, xd);
@@ -168,6 +192,13 @@
 int av1_get_pred_context_comp_bwdref_p(const AV1_COMMON *cm,
                                        const MACROBLOCKD *xd);
 
+#if CONFIG_NEW_MULTISYMBOL
+static INLINE aom_cdf_prob *av1_get_pred_cdf_comp_bwdref_p(
+    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
+  const int pred_context = av1_get_pred_context_comp_bwdref_p(cm, xd);
+  return xd->tile_ctx->comp_bwdref_cdf[pred_context][0];
+}
+#endif  // CONFIG_NEW_MULTISYMBOL
 static INLINE aom_prob av1_get_pred_prob_comp_bwdref_p(const AV1_COMMON *cm,
                                                        const MACROBLOCKD *xd) {
   const int pred_context = av1_get_pred_context_comp_bwdref_p(cm, xd);
@@ -214,31 +245,38 @@
 
 #if CONFIG_NEW_MULTISYMBOL
 static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p1(
-    const MACROBLOCKD *xd) {
+    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
+  (void)cm;
   return xd->tile_ctx
       ->single_ref_cdf[av1_get_pred_context_single_ref_p1(xd)][0];
 }
 static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p2(
-    const MACROBLOCKD *xd) {
+    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
+  (void)cm;
   return xd->tile_ctx
       ->single_ref_cdf[av1_get_pred_context_single_ref_p2(xd)][1];
 }
+#if CONFIG_EXT_REFS
 static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p3(
-    const MACROBLOCKD *xd) {
+    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
+  (void)cm;
   return xd->tile_ctx
       ->single_ref_cdf[av1_get_pred_context_single_ref_p3(xd)][2];
 }
 static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p4(
-    const MACROBLOCKD *xd) {
+    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
+  (void)cm;
   return xd->tile_ctx
       ->single_ref_cdf[av1_get_pred_context_single_ref_p4(xd)][3];
 }
 static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p5(
-    const MACROBLOCKD *xd) {
+    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
+  (void)cm;
   return xd->tile_ctx
       ->single_ref_cdf[av1_get_pred_context_single_ref_p5(xd)][4];
 }
-#endif
+#endif  // CONFIG_EXT_REFS
+#endif  // CONFIG_NEW_MULTISYMBOL
 
 #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
 int av1_get_inter_mode_context(const MACROBLOCKD *xd);
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 0940327..d12eb54 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -272,8 +272,12 @@
 }
 
 static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
+#if CONFIG_NEW_MULTISYMBOL && !CONFIG_EXT_COMP_REFS
+  (void)r;
+#else
   FRAME_CONTEXT *const fc = cm->fc;
-  int i, j;
+  int i;
+#endif
 
 #if !CONFIG_NEW_MULTISYMBOL
   if (cm->reference_mode == REFERENCE_MODE_SELECT)
@@ -282,6 +286,7 @@
 
   if (cm->reference_mode != COMPOUND_REFERENCE) {
     for (i = 0; i < REF_CONTEXTS; ++i) {
+      int j;
       for (j = 0; j < (SINGLE_REFS - 1); ++j) {
         av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
       }
@@ -294,12 +299,16 @@
     for (i = 0; i < COMP_REF_TYPE_CONTEXTS; ++i)
       av1_diff_update_prob(r, &fc->comp_ref_type_prob[i], ACCT_STR);
 
-    for (i = 0; i < UNI_COMP_REF_CONTEXTS; ++i)
+    for (i = 0; i < UNI_COMP_REF_CONTEXTS; ++i) {
+      int j;
       for (j = 0; j < (UNIDIR_COMP_REFS - 1); ++j)
         av1_diff_update_prob(r, &fc->uni_comp_ref_prob[i][j], ACCT_STR);
+    }
 #endif  // CONFIG_EXT_COMP_REFS
 
+#if !CONFIG_NEW_MULTISYMBOL
     for (i = 0; i < REF_CONTEXTS; ++i) {
+      int j;
 #if CONFIG_EXT_REFS
       for (j = 0; j < (FWD_REFS - 1); ++j)
         av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
@@ -310,6 +319,7 @@
         av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
 #endif  // CONFIG_EXT_REFS
     }
+#endif  // CONFIG_NEW_MULTISYMBOL
   }
 }
 
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 476f8f1..1e289a4 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -1311,7 +1311,7 @@
 
 #if CONFIG_NEW_MULTISYMBOL
 #define READ_REF_BIT(pname) \
-  aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
+  aom_read_symbol(r, av1_get_pred_cdf_##pname(cm, xd), 2, ACCT_STR)
 #else
 #define READ_REF_BIT(pname) \
   aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
@@ -1339,7 +1339,9 @@
 static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                             aom_reader *r, int segment_id,
                             MV_REFERENCE_FRAME ref_frame[2]) {
+#if CONFIG_EXT_COMP_REFS
   FRAME_CONTEXT *const fc = cm->fc;
+#endif
   FRAME_COUNTS *counts = xd->counts;
 
   if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
@@ -1402,11 +1404,11 @@
       int bit;
       // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
       if (L_OR_L2(cm) && L3_OR_G(cm))
-        bit = aom_read(r, fc->comp_ref_prob[ctx][0], ACCT_STR);
+        bit = READ_REF_BIT(comp_ref_p);
       else
         bit = L3_OR_G(cm);
 #else   // !CONFIG_VAR_REFS
-      const int bit = aom_read(r, fc->comp_ref_prob[ctx][0], ACCT_STR);
+      const int bit = READ_REF_BIT(comp_ref_p);
 #endif  // CONFIG_VAR_REFS
       if (counts) ++counts->comp_ref[ctx][0][bit];
 
@@ -1418,11 +1420,11 @@
         int bit1;
         // Test need to explicitly code (L) vs (L2) branch node in tree
         if (L_AND_L2(cm))
-          bit1 = aom_read(r, fc->comp_ref_prob[ctx1][1], ACCT_STR);
+          bit1 = READ_REF_BIT(comp_ref_p1);
         else
           bit1 = LAST_IS_VALID(cm);
 #else   // !CONFIG_VAR_REFS
-        const int bit1 = aom_read(r, fc->comp_ref_prob[ctx1][1], ACCT_STR);
+        const int bit1 = READ_REF_BIT(comp_ref_p1);
 #endif  // CONFIG_VAR_REFS
         if (counts) ++counts->comp_ref[ctx1][1][bit1];
         ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
@@ -1432,11 +1434,11 @@
         int bit2;
         // Test need to explicitly code (L3) vs (G) branch node in tree
         if (L3_AND_G(cm))
-          bit2 = aom_read(r, fc->comp_ref_prob[ctx2][2], ACCT_STR);
+          bit2 = READ_REF_BIT(comp_ref_p2);
         else
           bit2 = GOLDEN_IS_VALID(cm);
 #else   // !CONFIG_VAR_REFS
-        const int bit2 = aom_read(r, fc->comp_ref_prob[ctx2][2], ACCT_STR);
+        const int bit2 = READ_REF_BIT(comp_ref_p2);
 #endif  // CONFIG_VAR_REFS
         if (counts) ++counts->comp_ref[ctx2][2][bit2];
         ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
@@ -1448,12 +1450,11 @@
       int bit_bwd;
       // Test need to explicitly code (BWD) vs (ALT) branch node in tree
       if (BWD_AND_ALT(cm))
-        bit_bwd = aom_read(r, fc->comp_bwdref_prob[ctx_bwd][0], ACCT_STR);
+        bit_bwd = READ_REF_BIT(comp_bwdref_p);
       else
         bit_bwd = ALTREF_IS_VALID(cm);
-#else   // !CONFIG_VAR_REFS
-      const int bit_bwd =
-          aom_read(r, fc->comp_bwdref_prob[ctx_bwd][0], ACCT_STR);
+#else  // !CONFIG_VAR_REFS
+      const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
 #endif  // CONFIG_VAR_REFS
       if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
       ref_frame[idx] = cm->comp_bwd_ref[bit_bwd];
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index c665d2f..ea52826 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1248,7 +1248,7 @@
 
 #if CONFIG_NEW_MULTISYMBOL
 #define WRITE_REF_BIT(bname, pname) \
-  aom_write_symbol(w, bname, av1_get_pred_cdf_##pname(xd), 2)
+  aom_write_symbol(w, bname, av1_get_pred_cdf_##pname(cm, xd), 2)
 #else
 #define WRITE_REF_BIT(bname, pname) \
   aom_write(w, bname, av1_get_pred_prob_##pname(cm, xd))
@@ -1315,7 +1315,7 @@
       // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
       if (L_OR_L2(cm) && L3_OR_G(cm))
 #endif  // CONFIG_VAR_REFS
-        aom_write(w, bit, av1_get_pred_prob_comp_ref_p(cm, xd));
+        WRITE_REF_BIT(bit, comp_ref_p);
 
       if (!bit) {
 #if CONFIG_VAR_REFS
@@ -1323,7 +1323,7 @@
         if (L_AND_L2(cm)) {
 #endif  // CONFIG_VAR_REFS
           const int bit1 = mbmi->ref_frame[0] == LAST_FRAME;
-          aom_write(w, bit1, av1_get_pred_prob_comp_ref_p1(cm, xd));
+          WRITE_REF_BIT(bit1, comp_ref_p1);
 #if CONFIG_VAR_REFS
         }
 #endif  // CONFIG_VAR_REFS
@@ -1333,7 +1333,7 @@
         if (L3_AND_G(cm)) {
 #endif  // CONFIG_VAR_REFS
           const int bit2 = mbmi->ref_frame[0] == GOLDEN_FRAME;
-          aom_write(w, bit2, av1_get_pred_prob_comp_ref_p2(cm, xd));
+          WRITE_REF_BIT(bit2, comp_ref_p2);
 #if CONFIG_VAR_REFS
         }
 #endif  // CONFIG_VAR_REFS
@@ -1344,14 +1344,14 @@
       if (BWD_AND_ALT(cm)) {
 #endif  // CONFIG_VAR_REFS
         const int bit_bwd = mbmi->ref_frame[1] == ALTREF_FRAME;
-        aom_write(w, bit_bwd, av1_get_pred_prob_comp_bwdref_p(cm, xd));
+        WRITE_REF_BIT(bit_bwd, comp_bwdref_p);
 #if CONFIG_VAR_REFS
       }
 #endif  // CONFIG_VAR_REFS
 
 #else   // !CONFIG_EXT_REFS
       const int bit = mbmi->ref_frame[0] == GOLDEN_FRAME;
-      aom_write(w, bit, av1_get_pred_prob_comp_ref_p(cm, xd));
+      WRITE_REF_BIT(bit, comp_ref_p);
 #endif  // CONFIG_EXT_REFS
     } else {
 #if CONFIG_EXT_REFS
@@ -4960,7 +4960,10 @@
   FRAME_CONTEXT *const fc = cm->fc;
   FRAME_COUNTS *counts = cpi->td.counts;
   aom_writer *header_bc;
-  int i, j;
+  int i;
+#if !CONFIG_NEW_MULTISYMBOL
+  int j;
+#endif
 
 #if CONFIG_TILE_GROUPS
   const int probwt = cm->num_tg;
@@ -5182,6 +5185,7 @@
                                     counts->uni_comp_ref[i][j], probwt);
 #endif  // CONFIG_EXT_COMP_REFS
 
+#if !CONFIG_NEW_MULTISYMBOL
       for (i = 0; i < REF_CONTEXTS; i++) {
 #if CONFIG_EXT_REFS
         for (j = 0; j < (FWD_REFS - 1); j++) {
@@ -5199,6 +5203,7 @@
         }
 #endif  // CONFIG_EXT_REFS
       }
+#endif  // CONFIG_NEW_MULTISYMBOL
     }
 
 #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF