Use cdfs for loop-restoration types

BUG=aomedia:907

Change-Id: I30a0290e499d338250343b5f9092bdcabe7d867a
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 6a3df8d..e2db714 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -2492,15 +2492,20 @@
 #endif
 
 #if CONFIG_LOOP_RESTORATION
-const aom_tree_index
-    av1_switchable_restore_tree[TREE_SIZE(RESTORE_SWITCHABLE_TYPES)] = {
-      -RESTORE_NONE, 2, -RESTORE_WIENER, -RESTORE_SGRPROJ,
+static const aom_cdf_prob
+    default_switchable_restore_cdf[CDF_SIZE(RESTORE_SWITCHABLE_TYPES)] = {
+      AOM_ICDF(32 * 128), AOM_ICDF(144 * 128), AOM_ICDF(32768), 0,
     };
 
-static const aom_prob
-    default_switchable_restore_prob[RESTORE_SWITCHABLE_TYPES - 1] = {
-      32, 128,
-    };
+#if CONFIG_NEW_MULTISYMBOL
+static const aom_cdf_prob default_wiener_restore_cdf[CDF_SIZE(2)] = {
+  AOM_ICDF(64 * 128), AOM_ICDF(32768), 0,
+};
+
+static const aom_cdf_prob default_sgrproj_restore_cdf[CDF_SIZE(2)] = {
+  AOM_ICDF(64 * 128), AOM_ICDF(32768), 0,
+};
+#endif  // CONFIG_NEW_MULTISYMBOL
 #endif  // CONFIG_LOOP_RESTORATION
 
 #define NUM_PALETTE_NEIGHBORS 3  // left, top-left and top.
@@ -6644,7 +6649,11 @@
   av1_copy(fc->inter_lgt_prob, default_inter_lgt_prob);
 #endif  // CONFIG_LGT_FROM_PRED
 #if CONFIG_LOOP_RESTORATION
-  av1_copy(fc->switchable_restore_prob, default_switchable_restore_prob);
+  av1_copy(fc->switchable_restore_cdf, default_switchable_restore_cdf);
+#if CONFIG_NEW_MULTISYMBOL
+  av1_copy(fc->wiener_restore_cdf, default_wiener_restore_cdf);
+  av1_copy(fc->sgrproj_restore_cdf, default_sgrproj_restore_cdf);
+#endif  // CONFIG_NEW_MULTISYMBOL
 #endif  // CONFIG_LOOP_RESTORATION
   av1_copy(fc->y_mode_cdf, default_if_y_mode_cdf);
   av1_copy(fc->uv_mode_cdf, default_uv_mode_cdf);
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 8ed6788..fd655cd 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -348,7 +348,11 @@
   aom_cdf_prob filter_intra_mode_cdf[PLANE_TYPES][CDF_SIZE(FILTER_INTRA_MODES)];
 #endif  // CONFIG_FILTER_INTRA
 #if CONFIG_LOOP_RESTORATION
-  aom_prob switchable_restore_prob[RESTORE_SWITCHABLE_TYPES - 1];
+  aom_cdf_prob switchable_restore_cdf[CDF_SIZE(RESTORE_SWITCHABLE_TYPES)];
+#if CONFIG_NEW_MULTISYMBOL
+  aom_cdf_prob wiener_restore_cdf[CDF_SIZE(2)];
+  aom_cdf_prob sgrproj_restore_cdf[CDF_SIZE(2)];
+#endif  // CONFIG_NEW_MULTISYMBOL
 #endif  // CONFIG_LOOP_RESTORATION
   aom_cdf_prob y_mode_cdf[BLOCK_SIZE_GROUPS][CDF_SIZE(INTRA_MODES)];
   aom_cdf_prob uv_mode_cdf[INTRA_MODES][CDF_SIZE(UV_INTRA_MODES)];
@@ -706,9 +710,7 @@
 #endif  // CONFIG_NCOBMC_ADAPT_WEIGHT
 #if CONFIG_LOOP_RESTORATION
 #define RESTORE_NONE_SGRPROJ_PROB 64
-#define RESTORE_NONE_BILATERAL_PROB 16
 #define RESTORE_NONE_WIENER_PROB 64
-#define RESTORE_NONE_DOMAINTXFMRF_PROB 64
 extern const aom_tree_index
     av1_switchable_restore_tree[TREE_SIZE(RESTORE_SWITCHABLE_TYPES)];
 #endif  // CONFIG_LOOP_RESTORATION
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 1855991..9596a69 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -2887,9 +2887,8 @@
   if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
     assert(plane == 0);
     rsi->restoration_type[rtile_idx] =
-        aom_read_tree(r, av1_switchable_restore_tree,
-                      cm->fc->switchable_restore_prob, ACCT_STR);
-
+        aom_read_symbol(r, xd->tile_ctx->switchable_restore_cdf,
+                        RESTORE_SWITCHABLE_TYPES, ACCT_STR);
     if (rsi->restoration_type[rtile_idx] == RESTORE_WIENER) {
       read_wiener_filter(wiener_win, &rsi->wiener_info[rtile_idx], wiener_info,
                          r);
@@ -2897,7 +2896,11 @@
       read_sgrproj_filter(&rsi->sgrproj_info[rtile_idx], sgrproj_info, r);
     }
   } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
+#if CONFIG_NEW_MULTISYMBOL
+    if (aom_read_symbol(r, xd->tile_ctx->wiener_restore_cdf, 2, ACCT_STR)) {
+#else
     if (aom_read(r, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
+#endif  // CONFIG_NEW_MULTISYMBOL
       rsi->restoration_type[rtile_idx] = RESTORE_WIENER;
       read_wiener_filter(wiener_win, &rsi->wiener_info[rtile_idx], wiener_info,
                          r);
@@ -2905,7 +2908,11 @@
       rsi->restoration_type[rtile_idx] = RESTORE_NONE;
     }
   } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
+#if CONFIG_NEW_MULTISYMBOL
+    if (aom_read_symbol(r, xd->tile_ctx->sgrproj_restore_cdf, 2, ACCT_STR)) {
+#else
     if (aom_read(r, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
+#endif  // CONFIG_NEW_MULTISYMBOL
       rsi->restoration_type[rtile_idx] = RESTORE_SGRPROJ;
       read_sgrproj_filter(&rsi->sgrproj_info[rtile_idx], sgrproj_info, r);
     } else {
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 0c72d89..bfd588a 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -91,7 +91,6 @@
 static struct av1_token compound_type_encodings[COMPOUND_TYPES];
 #endif  // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
 #if CONFIG_LOOP_RESTORATION
-static struct av1_token switchable_restore_encodings[RESTORE_SWITCHABLE_TYPES];
 static void loop_restoration_write_sb_coeffs(const AV1_COMMON *const cm,
                                              MACROBLOCKD *xd,
                                              aom_writer *const w, int plane,
@@ -128,10 +127,6 @@
 #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
   av1_tokens_from_tree(compound_type_encodings, av1_compound_type_tree);
 #endif  // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
-#if CONFIG_LOOP_RESTORATION
-  av1_tokens_from_tree(switchable_restore_encodings,
-                       av1_switchable_restore_tree);
-#endif  // CONFIG_LOOP_RESTORATION
 }
 
 static void write_intra_mode_kf(const AV1_COMMON *cm, FRAME_CONTEXT *frame_ctx,
@@ -3471,9 +3466,9 @@
 
   if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
     assert(plane == 0);
-    av1_write_token(
-        w, av1_switchable_restore_tree, cm->fc->switchable_restore_prob,
-        &switchable_restore_encodings[rsi->restoration_type[rtile_idx]]);
+    aom_write_symbol(w, rsi->restoration_type[rtile_idx],
+                     xd->tile_ctx->switchable_restore_cdf,
+                     RESTORE_SWITCHABLE_TYPES);
     if (rsi->restoration_type[rtile_idx] == RESTORE_WIENER) {
       write_wiener_filter(wiener_win, &rsi->wiener_info[rtile_idx], wiener_info,
                           w);
@@ -3481,21 +3476,30 @@
       write_sgrproj_filter(&rsi->sgrproj_info[rtile_idx], sgrproj_info, w);
     }
   } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
+#if CONFIG_NEW_MULTISYMBOL
+    aom_write_symbol(w, rsi->restoration_type[rtile_idx] != RESTORE_NONE,
+                     xd->tile_ctx->wiener_restore_cdf, 2);
+#else
     aom_write(w, rsi->restoration_type[rtile_idx] != RESTORE_NONE,
               RESTORE_NONE_WIENER_PROB);
+#endif  // CONFIG_NEW_MULTISYMBOL
     if (rsi->restoration_type[rtile_idx] != RESTORE_NONE) {
       write_wiener_filter(wiener_win, &rsi->wiener_info[rtile_idx], wiener_info,
                           w);
     }
   } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
+#if CONFIG_NEW_MULTISYMBOL
+    aom_write_symbol(w, rsi->restoration_type[rtile_idx] != RESTORE_NONE,
+                     xd->tile_ctx->sgrproj_restore_cdf, 2);
+#else
     aom_write(w, rsi->restoration_type[rtile_idx] != RESTORE_NONE,
               RESTORE_NONE_SGRPROJ_PROB);
+#endif  // CONFIG_NEW_MULTISYMBOL
     if (rsi->restoration_type[rtile_idx] != RESTORE_NONE) {
       write_sgrproj_filter(&rsi->sgrproj_info[rtile_idx], sgrproj_info, w);
     }
   }
 }
-
 #endif  // CONFIG_LOOP_RESTORATION
 
 static void encode_loopfilter(AV1_COMMON *cm, struct aom_write_bit_buffer *wb) {
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index d79736f..f3af5bb 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -304,6 +304,8 @@
 #endif  // CONFIG_EXT_INTRA
 #if CONFIG_LOOP_RESTORATION
   int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
+  int wiener_restore_cost[2];
+  int sgrproj_restore_cost[2];
 #endif  // CONFIG_LOOP_RESTORATION
 #if CONFIG_INTRABC
   int intrabc_cost[2];
diff --git a/av1/encoder/pickrst.c b/av1/encoder/pickrst.c
index a2262b6..aa400f8 100644
--- a/av1/encoder/pickrst.c
+++ b/av1/encoder/pickrst.c
@@ -598,7 +598,7 @@
                            limits->h_end - limits->h_start, limits->v_start,
                            limits->v_end - limits->v_start, (1 << ctxt->plane));
   // #bits when a tile is not restored
-  int bits = av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, 0);
+  int bits = x->sgrproj_restore_cost[0];
   double cost_norestore = RDCOST_DBL(x->rdmult, (bits >> 4), err);
   ctxt->best_tile_cost[rtile_idx] = INT64_MAX;
 
@@ -627,7 +627,7 @@
   bits =
       count_sgrproj_bits(&plane_rsi->sgrproj_info[rtile_idx], ref_sgrproj_info)
       << AV1_PROB_COST_SHIFT;
-  bits += av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, 1);
+  bits += x->sgrproj_restore_cost[1];
   double cost_sgrproj = RDCOST_DBL(x->rdmult, (bits >> 4), err);
   if (cost_sgrproj >= cost_norestore) {
     ctxt->type[rtile_idx] = RESTORE_NONE;
@@ -645,6 +645,7 @@
                              RestorationInfo *info, RestorationType *type,
                              int64_t *best_tile_cost,
                              YV12_BUFFER_CONFIG *dst_frame) {
+  const MACROBLOCK *const x = &cpi->td.mb;
   struct rest_search_ctxt ctxt;
   const int nrtiles =
       init_rest_search_ctxt(src, cpi, partial_frame, plane, info, type,
@@ -686,8 +687,7 @@
   int bits = frame_level_restore_bits[plane_rsi->frame_restoration_type]
              << AV1_PROB_COST_SHIFT;
   for (int rtile_idx = 0; rtile_idx < nrtiles; ++rtile_idx) {
-    bits += av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB,
-                         type[rtile_idx] != RESTORE_NONE);
+    bits += x->sgrproj_restore_cost[type[rtile_idx] != RESTORE_NONE];
     plane_rsi->sgrproj_info[rtile_idx] = sgrproj_info[rtile_idx];
     if (type[rtile_idx] == RESTORE_SGRPROJ) {
       bits += count_sgrproj_bits(&plane_rsi->sgrproj_info[rtile_idx],
@@ -1191,7 +1191,7 @@
                            limits->h_end - limits->h_start, limits->v_start,
                            limits->v_end - limits->v_start, (1 << ctxt->plane));
   // #bits when a tile is not restored
-  int bits = av1_cost_bit(RESTORE_NONE_WIENER_PROB, 0);
+  int bits = x->wiener_restore_cost[0];
   double cost_norestore = RDCOST_DBL(x->rdmult, (bits >> 4), err);
   ctxt->best_tile_cost[rtile_idx] = INT64_MAX;
 
@@ -1242,7 +1242,7 @@
   }
   bits = count_wiener_bits(wiener_win, rtile_wiener_info, ref_wiener_info)
          << AV1_PROB_COST_SHIFT;
-  bits += av1_cost_bit(RESTORE_NONE_WIENER_PROB, 1);
+  bits += x->wiener_restore_cost[1];
   double cost_wiener = RDCOST_DBL(x->rdmult, (bits >> 4), err);
   if (cost_wiener >= cost_norestore) {
     ctxt->type[rtile_idx] = RESTORE_NONE;
@@ -1258,6 +1258,7 @@
                             int partial_frame, int plane, RestorationInfo *info,
                             RestorationType *type, int64_t *best_tile_cost,
                             YV12_BUFFER_CONFIG *dst_frame) {
+  const MACROBLOCK *const x = &cpi->td.mb;
   struct rest_search_ctxt ctxt;
   const int nrtiles =
       init_rest_search_ctxt(src, cpi, partial_frame, plane, info, type,
@@ -1305,8 +1306,7 @@
       (plane == AOM_PLANE_Y) ? WIENER_WIN : WIENER_WIN_CHROMA;
 
   for (int tile_idx = 0; tile_idx < nrtiles; ++tile_idx) {
-    bits +=
-        av1_cost_bit(RESTORE_NONE_WIENER_PROB, type[tile_idx] != RESTORE_NONE);
+    bits += x->wiener_restore_cost[type[tile_idx] != RESTORE_NONE];
     plane_rsi->wiener_info[tile_idx] = wiener_info[tile_idx];
 
     if (type[tile_idx] == RESTORE_WIENER) {
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 573c1f6..0ed3518 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -271,8 +271,19 @@
 #endif  // CONFIG_INTRA_INTERP
 #endif  // CONFIG_EXT_INTRA
 #if CONFIG_LOOP_RESTORATION
-  av1_cost_tokens(x->switchable_restore_cost, fc->switchable_restore_prob,
-                  av1_switchable_restore_tree);
+  av1_cost_tokens_from_cdf(x->switchable_restore_cost,
+                           fc->switchable_restore_cdf, NULL);
+#if CONFIG_NEW_MULTISYMBOL
+  av1_cost_tokens_from_cdf(x->wiener_restore_cost, fc->wiener_restore_cdf,
+                           NULL);
+  av1_cost_tokens_from_cdf(x->sgrproj_restore_cost, fc->sgrproj_restore_cdf,
+                           NULL);
+#else
+  x->wiener_restore_cost[0] = av1_cost_bit(RESTORE_NONE_WIENER_PROB, 0);
+  x->wiener_restore_cost[1] = av1_cost_bit(RESTORE_NONE_WIENER_PROB, 1);
+  x->sgrproj_restore_cost[0] = av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, 0);
+  x->sgrproj_restore_cost[1] = av1_cost_bit(RESTORE_NONE_SGRPROJ_PROB, 1);
+#endif  // CONFIG_NEW_MULTISYMBOL
 #endif  // CONFIG_LOOP_RESTORATION
 #if CONFIG_INTRABC
   av1_cost_tokens_from_cdf(x->intrabc_cost, fc->intrabc_cdf, NULL);