ext_partition_types: Bring into alignment with ec_multisymbol and ec_adapt

ec_multisymbol+ext_partition_types:
base_ext_types@2017-03-15T18:24:38.012Z -> 8565_3_ext_types@2017-03-17T18:29:48.794Z

   PSNR | PSNR Cb | PSNR Cr | PSNR HVS |    SSIM | MS SSIM | CIEDE 2000
-0.0346 | -0.0343 | -0.0341 |  -0.0345 | -0.0345 | -0.0344 |    -0.0345

ec_multisymbol+ec_adapt+ext_partition_types:
base_adapt_ext_types@2017-03-15T18:27:31.704Z -> 8565_3_adapt_ext_types@2017-03-17T18:30:01.108Z

   PSNR | PSNR Cb | PSNR Cr | PSNR HVS |    SSIM | MS SSIM | CIEDE 2000
-0.0422 | -0.0420 |     N/A |  -0.0425 | -0.0423 | -0.0423 |    -0.0420

Change-Id: Ia40f118f0faf546be13e02ec9ed584dd38529a59
diff --git a/av1/common/entropy.c b/av1/common/entropy.c
index b634ee9..ad68d3a 100644
--- a/av1/common/entropy.c
+++ b/av1/common/entropy.c
@@ -5694,11 +5694,7 @@
   AVERAGE_TILE_CDFS(seg.tree_cdf)
   AVERAGE_TILE_CDFS(uv_mode_cdf)
 
-#if CONFIG_EXT_PARTITION_TYPES
-// FIXME
-#else
   AVERAGE_TILE_CDFS(partition_cdf)
-#endif  // CONFIG_EXT_PARTITION_TYPES
 
 #if CONFIG_DELTA_Q
   AVERAGE_TILE_CDFS(delta_q_cdf)
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index ec3dc28..4bc470a 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -1666,6 +1666,34 @@
 };
 #endif  // CONFIG_ALT_INTRA
 
+#if CONFIG_EXT_PARTITION_TYPES
+static const aom_cdf_prob
+    default_partition_cdf[PARTITION_CONTEXTS][CDF_SIZE(EXT_PARTITION_TYPES)] = {
+      // 8x8 -> 4x4 only supports the four legacy partition types
+      { 25472, 28949, 31052, 32768, 0, 0, 0, 0, 0 },
+      { 18816, 22250, 28783, 32768, 0, 0, 0, 0, 0 },
+      { 18944, 26126, 29188, 32768, 0, 0, 0, 0, 0 },
+      { 15488, 22508, 27077, 32768, 0, 0, 0, 0, 0 },
+      { 22272, 23768, 25043, 29996, 30744, 31493, 32130, 32768, 0 },
+      { 11776, 13457, 16315, 28229, 29069, 29910, 31339, 32768, 0 },
+      { 10496, 14802, 16136, 27127, 29280, 31434, 32101, 32768, 0 },
+      { 6784, 8763, 10440, 29110, 30100, 31090, 31929, 32768, 0 },
+      { 22656, 23801, 24702, 30721, 31294, 31867, 32317, 32768, 0 },
+      { 8704, 9926, 12586, 28885, 29496, 30107, 31437, 32768, 0 },
+      { 6656, 10685, 11566, 27857, 29871, 31886, 32327, 32768, 0 },
+      { 2176, 3012, 3690, 31253, 31671, 32090, 32429, 32768, 0 },
+      { 28416, 28705, 28926, 32258, 32402, 32547, 32657, 32768, 0 },
+      { 9216, 9952, 11849, 30134, 30502, 30870, 31819, 32768, 0 },
+      { 7424, 9008, 9528, 30664, 31456, 32248, 32508, 32768, 0 },
+      { 1280, 1710, 2069, 31978, 32193, 32409, 32588, 32768, 0 },
+#if CONFIG_EXT_PARTITION
+      { 28416, 28705, 28926, 32258, 32402, 32547, 32657, 32768, 0 },
+      { 9216, 9952, 11849, 30134, 30502, 30870, 31819, 32768, 0 },
+      { 7424, 9008, 9528, 30664, 31456, 32248, 32508, 32768, 0 },
+      { 1280, 1710, 2069, 31978, 32193, 32409, 32588, 32768, 0 },
+#endif
+    };
+#else
 static const aom_cdf_prob
     default_partition_cdf[PARTITION_CONTEXTS][CDF_SIZE(PARTITION_TYPES)] = {
       { 25472, 28949, 31052, 32768, 0 }, { 18816, 22250, 28783, 32768, 0 },
@@ -1676,7 +1704,12 @@
       { 6656, 14714, 16477, 32768, 0 },  { 2176, 3849, 5205, 32768, 0 },
       { 28416, 28994, 29436, 32768, 0 }, { 9216, 10688, 14483, 32768, 0 },
       { 7424, 10592, 11632, 32768, 0 },  { 1280, 2141, 2859, 32768, 0 },
+#if CONFIG_EXT_PARTITION
+      { 28416, 28994, 29436, 32768, 0 }, { 9216, 10688, 14483, 32768, 0 },
+      { 7424, 10592, 11632, 32768, 0 },  { 1280, 2141, 2859, 32768, 0 },
+#endif
     };
+#endif
 
 static const aom_cdf_prob
     default_inter_mode_cdf[INTER_MODE_CONTEXTS][CDF_SIZE(INTER_MODES)] = {
@@ -2280,7 +2313,29 @@
     av1_tree_to_cdf(av1_intra_mode_tree, fc->uv_mode_prob[i],
                     fc->uv_mode_cdf[i]);
 #if CONFIG_EXT_PARTITION_TYPES
-// FIXME
+  for (i = 0; i < PARTITION_PLOFFSET; ++i)
+    av1_tree_to_cdf(av1_partition_tree, fc->partition_prob[i],
+                    fc->partition_cdf[i]);
+  // Logical index (enum value) to inorder index (tree_to_cdf order)
+  aom_cdf_prob inorder_partition_cdf[CDF_SIZE(EXT_PARTITION_TYPES)] = {};
+  // TODO(aconverse): Generate this dynamically. The assumptions that
+  // av1_indices_from_tree() makes don't hold for this tree.
+  static const uint8_t av1_ext_partition_index_map[EXT_PARTITION_TYPES] = {
+    0, 1, 4, 7, 2, 3, 5, 6,
+  };
+  for (; i < PARTITION_CONTEXTS; ++i) {
+    av1_tree_to_cdf(av1_ext_partition_tree, fc->partition_prob[i],
+                    inorder_partition_cdf);
+    aom_cdf_prob cum_prob = 0;
+    for (j = 0; j < EXT_PARTITION_TYPES; ++j) {
+      int inorder_idx = av1_ext_partition_index_map[j];
+      aom_cdf_prob prob =
+          inorder_partition_cdf[inorder_idx] -
+          (inorder_idx > 0 ? inorder_partition_cdf[inorder_idx - 1] : 0);
+      fc->partition_cdf[i][j] = (cum_prob += prob);
+    }
+    assert(cum_prob == CDF_PROB_TOP);
+  }
 #else
   for (i = 0; i < PARTITION_CONTEXTS; ++i)
     av1_tree_to_cdf(av1_partition_tree, fc->partition_prob[i],
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 23cc2cb..902b5fb 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -245,7 +245,11 @@
 #if CONFIG_EC_MULTISYMBOL
   aom_cdf_prob y_mode_cdf[BLOCK_SIZE_GROUPS][CDF_SIZE(INTRA_MODES)];
   aom_cdf_prob uv_mode_cdf[INTRA_MODES][CDF_SIZE(INTRA_MODES)];
+#if CONFIG_EXT_PARTITION_TYPES
+  aom_cdf_prob partition_cdf[PARTITION_CONTEXTS][CDF_SIZE(EXT_PARTITION_TYPES)];
+#else
   aom_cdf_prob partition_cdf[PARTITION_CONTEXTS][CDF_SIZE(PARTITION_TYPES)];
+#endif
   aom_cdf_prob switchable_interp_cdf[SWITCHABLE_FILTER_CONTEXTS]
                                     [CDF_SIZE(SWITCHABLE_FILTERS)];
   aom_cdf_prob inter_mode_cdf[INTER_MODE_CONTEXTS][CDF_SIZE(INTER_MODES)];
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index fc7dcde..7866380 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1940,8 +1940,13 @@
       p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
 #endif
     else
+#if CONFIG_EC_MULTISYMBOL
+      p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
+                                          EXT_PARTITION_TYPES, ACCT_STR);
+#else
       p = (PARTITION_TYPE)aom_read_tree(r, av1_ext_partition_tree, probs,
                                         ACCT_STR);
+#endif
 #else
 #if CONFIG_EC_MULTISYMBOL
     p = (PARTITION_TYPE)aom_read_symbol(r, ec_ctx->partition_cdf[ctx],
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index f71201b..1c944e4 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -59,7 +59,7 @@
 
 static struct av1_token intra_mode_encodings[INTRA_MODES];
 static struct av1_token switchable_interp_encodings[SWITCHABLE_FILTERS];
-#if CONFIG_EXT_PARTITION_TYPES
+#if CONFIG_EXT_PARTITION_TYPES && !CONFIG_EC_MULTISYMBOL
 static const struct av1_token ext_partition_encodings[EXT_PARTITION_TYPES] = {
   { 0, 1 },  { 4, 3 },  { 12, 4 }, { 7, 3 },
   { 10, 4 }, { 11, 4 }, { 26, 5 }, { 27, 5 }
@@ -2499,8 +2499,12 @@
       av1_write_token(w, av1_partition_tree, probs, &partition_encodings[p]);
 #endif
     else
+#if CONFIG_EC_MULTISYMBOL
+      aom_write_symbol(w, p, ec_ctx->partition_cdf[ctx], EXT_PARTITION_TYPES);
+#else
       av1_write_token(w, av1_ext_partition_tree, probs,
                       &ext_partition_encodings[p]);
+#endif  // CONFIG_EC_MULTISYMBOL
 #else
 #if CONFIG_EC_MULTISYMBOL
     aom_write_symbol(w, p, ec_ctx->partition_cdf[ctx], PARTITION_TYPES);