From 9fdf2e2e958f8e8790776cf8fc8eaf08b819d599 Mon Sep 17 00:00:00 2001
From: Hui Su <huisu@google.com>
Date: Mon, 22 Jan 2018 14:48:06 -0800
Subject: [PATCH] Remove more code about probability based entropy coding

Change-Id: Ie0bc1dd68f7a5d81e49da0ae6f855e572e12aa10
---
 aom_dsp/prob.h                | 23 +---------
 av1/common/seg_common.c       |  4 --
 av1/common/seg_common.h       |  2 -
 tools/aom_entropy_optimizer.c | 82 ++++++++---------------------------
 4 files changed, 19 insertions(+), 92 deletions(-)

diff --git a/aom_dsp/prob.h b/aom_dsp/prob.h
index c717c9a255..cf8cabb18b 100644
--- a/aom_dsp/prob.h
+++ b/aom_dsp/prob.h
@@ -27,8 +27,6 @@
 extern "C" {
 #endif
 
-typedef uint8_t aom_prob;
-
 // TODO(negge): Rename this aom_prob once we remove vpxbool.
 typedef uint16_t aom_cdf_prob;
 
@@ -625,30 +623,13 @@ typedef uint16_t aom_cdf_prob;
 
 #define BR_NODE 1
 
-#define aom_prob_half ((aom_prob)128)
-
-typedef int8_t aom_tree_index;
-
-#define TREE_SIZE(leaf_count) (-2 + 2 * (leaf_count))
-
-#define MODE_MV_COUNT_SAT 20
-
-/* We build coding trees compactly in arrays.
-   Each node of the tree is a pair of aom_tree_indices.
-   Array index often references a corresponding probability table.
-   Index <= 0 means done encoding/decoding and value = -Index,
-   Index > 0 means need another bit, specification at index.
-   Nonnegative indices are always even;  processing begins at node 0. */
-
-typedef const aom_tree_index aom_tree[];
-
-static INLINE aom_prob get_prob(unsigned int num, unsigned int den) {
+static INLINE uint8_t get_prob(unsigned int num, unsigned int den) {
   assert(den != 0);
   {
     const int p = (int)(((uint64_t)num * 256 + (den >> 1)) / den);
     // (p > 255) ? 255 : (p < 1) ? 1 : p;
     const int clipped_prob = p | ((255 - p) >> 23) | (p == 0);
-    return (aom_prob)clipped_prob;
+    return (uint8_t)clipped_prob;
   }
 }
 
diff --git a/av1/common/seg_common.c b/av1/common/seg_common.c
index f14bc02fef..24d2847f98 100644
--- a/av1/common/seg_common.c
+++ b/av1/common/seg_common.c
@@ -75,8 +75,4 @@ void av1_set_segdata(struct segmentation *seg, int segment_id,
   seg->feature_data[segment_id][feature_id] = seg_data;
 }
 
-const aom_tree_index av1_segment_tree[TREE_SIZE(MAX_SEGMENTS)] = {
-  2, 4, 6, 8, 10, 12, 0, -1, -2, -3, -4, -5, -6, -7
-};
-
 // TBD? Functions to read and write segment data with range / validity checking
diff --git a/av1/common/seg_common.h b/av1/common/seg_common.h
index efcf055d9e..3be785f4fe 100644
--- a/av1/common/seg_common.h
+++ b/av1/common/seg_common.h
@@ -96,8 +96,6 @@ static INLINE int get_segdata(const struct segmentation *seg, int segment_id,
   return seg->feature_data[segment_id][feature_id];
 }
 
-extern const aom_tree_index av1_segment_tree[TREE_SIZE(MAX_SEGMENTS)];
-
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/tools/aom_entropy_optimizer.c b/tools/aom_entropy_optimizer.c
index ac8cc19bad..8019e02dfb 100644
--- a/tools/aom_entropy_optimizer.c
+++ b/tools/aom_entropy_optimizer.c
@@ -28,64 +28,22 @@
 #include "./aom_config.h"
 #include "av1/common/entropymode.h"
 
-const aom_tree_index av1_intra_mode_tree[TREE_SIZE(INTRA_MODES)] = {
-  -DC_PRED,
-  2, /* 0 = DC_NODE */
-  -PAETH_PRED,
-  4, /* 1 = PAETH_NODE */
-  -V_PRED,
-  6, /* 2 = V_NODE */
-  8,
-  12, /* 3 = COM_NODE */
-  -H_PRED,
-  10, /* 4 = H_NODE */
-  -D135_PRED,
-  -D117_PRED, /* 5 = D135_NODE */
-  -D45_PRED,
-  14, /* 6 = D45_NODE */
-  -D63_PRED,
-  16, /* 7 = D63_NODE */
-  -D153_PRED,
-  18, /* 8 = D153_NODE */
-  -D207_PRED,
-  20, /* 9 = D207_NODE */
-  -SMOOTH_PRED,
-  22, /* 10 = SMOOTH_NODE */
-  -SMOOTH_V_PRED,
-  -SMOOTH_H_PRED /* 11 = SMOOTH_V_NODE */
-};
-
 #define SPACES_PER_TAB 2
 
 typedef unsigned int aom_count_type;
 // A log file recording parsed counts
 static FILE *logfile;  // TODO(yuec): make it a command line option
 
-static INLINE aom_prob get_binary_prob_new(unsigned int n0, unsigned int n1) {
+static INLINE uint8_t get_binary_prob_new(unsigned int n0, unsigned int n1) {
   // The "+1" will prevent this function from generating extreme probability
   // when both n0 and n1 are small
   const unsigned int den = n0 + 1 + n1 + 1;
   return get_prob(n0 + 1, den);
 }
 
-// Optimized probabilities will be stored in probs[].
-static unsigned int optimize_tree_probs(const aom_tree_index *tree,
-                                        unsigned int idx,
-                                        const unsigned int *counts,
-                                        aom_prob *probs) {
-  const int l = tree[idx];
-  const unsigned int left_count =
-      (l <= 0) ? counts[-l] : optimize_tree_probs(tree, l, counts, probs);
-  const int r = tree[idx + 1];
-  const unsigned int right_count =
-      (r <= 0) ? counts[-r] : optimize_tree_probs(tree, r, counts, probs);
-  probs[idx >> 1] = get_binary_prob_new(left_count, right_count);
-  return left_count + right_count;
-}
-
 static int parse_stats(aom_count_type **ct_ptr, FILE *const probsfile, int tabs,
                        int dim_of_cts, int *cts_each_dim,
-                       const aom_tree_index *tree, int flatten_last_dim) {
+                       int flatten_last_dim) {
   if (dim_of_cts < 1) {
     fprintf(stderr, "The dimension of a counts vector should be at least 1!\n");
     return 1;
@@ -93,7 +51,7 @@ static int parse_stats(aom_count_type **ct_ptr, FILE *const probsfile, int tabs,
   if (dim_of_cts == 1) {
     const int total_modes = cts_each_dim[0];
     aom_count_type *counts1d = *ct_ptr;
-    aom_prob *probs = aom_malloc(sizeof(*probs) * (total_modes - 1));
+    uint8_t *probs = aom_malloc(sizeof(*probs) * (total_modes - 1));
 
     if (probs == NULL) {
       fprintf(stderr, "Allocating prob array failed!\n");
@@ -101,12 +59,8 @@ static int parse_stats(aom_count_type **ct_ptr, FILE *const probsfile, int tabs,
     }
 
     (*ct_ptr) += total_modes;
-    if (tree != NULL) {
-      optimize_tree_probs(tree, 0, counts1d, probs);
-    } else {
-      assert(total_modes == 2);
-      probs[0] = get_binary_prob_new(counts1d[0], counts1d[1]);
-    }
+    assert(total_modes == 2);
+    probs[0] = get_binary_prob_new(counts1d[0], counts1d[1]);
     if (tabs > 0) fprintf(probsfile, "%*c", tabs * SPACES_PER_TAB, ' ');
     for (int k = 0; k < total_modes - 1; ++k) {
       if (k == total_modes - 2)
@@ -141,7 +95,7 @@ static int parse_stats(aom_count_type **ct_ptr, FILE *const probsfile, int tabs,
         tabs_next_level = tabs + 1;
       }
       if (parse_stats(ct_ptr, probsfile, tabs_next_level, dim_of_cts - 1,
-                      cts_each_dim + 1, tree, flatten_last_dim)) {
+                      cts_each_dim + 1, flatten_last_dim)) {
         return 1;
       }
       if (dim_of_cts == 2 || (dim_of_cts == 3 && flatten_last_dim)) {
@@ -167,7 +121,6 @@ static int parse_stats(aom_count_type **ct_ptr, FILE *const probsfile, int tabs,
 //   probsfile: output file
 //   dim_of_cts: number of dimensions of counts array
 //   cts_each_dim: an array storing size of each dimension of counts array
-//   tree: binary tree for a multi-symbol syntax, or NULL for a binary one
 //   flatten_last_dim: for a binary syntax, if flatten_last_dim is 0, probs in
 //                     different contexts will be written separately, e.g.,
 //                     {{p1}, {p2}, ...};
@@ -177,15 +130,14 @@ static int parse_stats(aom_count_type **ct_ptr, FILE *const probsfile, int tabs,
 //   prefix: declaration header for the entropy table
 static void optimize_entropy_table(aom_count_type *counts,
                                    FILE *const probsfile, int dim_of_cts,
-                                   int *cts_each_dim,
-                                   const aom_tree_index *tree,
-                                   int flatten_last_dim, char *prefix) {
+                                   int *cts_each_dim, int flatten_last_dim,
+                                   char *prefix) {
   aom_count_type *ct_ptr = counts;
 
   assert(!flatten_last_dim || cts_each_dim[dim_of_cts - 1] == 2);
 
   fprintf(probsfile, "%s = {\n", prefix);
-  if (parse_stats(&ct_ptr, probsfile, 1, dim_of_cts, cts_each_dim, tree,
+  if (parse_stats(&ct_ptr, probsfile, 1, dim_of_cts, cts_each_dim,
                   flatten_last_dim)) {
     fprintf(probsfile, "Optimizer failed!\n");
   }
@@ -402,7 +354,7 @@ int main(int argc, const char **argv) {
   cts_each_dim[0] = NEWMV_MODE_CONTEXTS;
   cts_each_dim[1] = 2;
   optimize_entropy_table(
-      &fc.newmv_mode[0][0], probsfile, 2, cts_each_dim, NULL, 1,
+      &fc.newmv_mode[0][0], probsfile, 2, cts_each_dim, 1,
       "static const aom_prob default_newmv_prob[NEWMV_MODE_CONTEXTS]");
   optimize_cdf_table(&fc.newmv_mode[0][0], probsfile, 2, cts_each_dim,
                      "static const aom_cdf_prob "
@@ -411,7 +363,7 @@ int main(int argc, const char **argv) {
   cts_each_dim[0] = GLOBALMV_MODE_CONTEXTS;
   cts_each_dim[1] = 2;
   optimize_entropy_table(
-      &fc.zeromv_mode[0][0], probsfile, 2, cts_each_dim, NULL, 1,
+      &fc.zeromv_mode[0][0], probsfile, 2, cts_each_dim, 1,
       "static const aom_prob default_zeromv_prob[GLOBALMV_MODE_CONTEXTS]");
   optimize_cdf_table(&fc.zeromv_mode[0][0], probsfile, 2, cts_each_dim,
                      "static const aom_cdf_prob "
@@ -420,7 +372,7 @@ int main(int argc, const char **argv) {
   cts_each_dim[0] = REFMV_MODE_CONTEXTS;
   cts_each_dim[1] = 2;
   optimize_entropy_table(
-      &fc.refmv_mode[0][0], probsfile, 2, cts_each_dim, NULL, 1,
+      &fc.refmv_mode[0][0], probsfile, 2, cts_each_dim, 1,
       "static const aom_prob default_refmv_prob[REFMV_MODE_CONTEXTS]");
   optimize_cdf_table(&fc.refmv_mode[0][0], probsfile, 2, cts_each_dim,
                      "static const aom_cdf_prob "
@@ -587,7 +539,7 @@ int main(int argc, const char **argv) {
   cts_each_dim[1] = TXB_SKIP_CONTEXTS;
   cts_each_dim[2] = 2;
   optimize_entropy_table(
-      &fc.txb_skip[0][0][0], probsfile, 3, cts_each_dim, NULL, 1,
+      &fc.txb_skip[0][0][0], probsfile, 3, cts_each_dim, 1,
       "static const aom_prob "
       "default_txk_skip[TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS]");
   optimize_cdf_table(&fc.txb_skip[0][0][0], probsfile, 3, cts_each_dim,
@@ -600,7 +552,7 @@ int main(int argc, const char **argv) {
   cts_each_dim[2] = EOB_COEF_CONTEXTS;
   cts_each_dim[3] = 2;
   optimize_entropy_table(
-      &fc.eob_flag[0][0][0][0], probsfile, 4, cts_each_dim, NULL, 1,
+      &fc.eob_flag[0][0][0][0], probsfile, 4, cts_each_dim, 1,
       "static const aom_prob "
       "default_eob_flag[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS]");
 
@@ -609,7 +561,7 @@ int main(int argc, const char **argv) {
   cts_each_dim[2] = EOB_COEF_CONTEXTS;
   cts_each_dim[3] = 2;
   optimize_entropy_table(
-      &fc.eob_extra[0][0][0][0], probsfile, 4, cts_each_dim, NULL, 1,
+      &fc.eob_extra[0][0][0][0], probsfile, 4, cts_each_dim, 1,
       "static const aom_prob "
       "default_eob_extra[TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS]");
 
@@ -668,7 +620,7 @@ int main(int argc, const char **argv) {
   cts_each_dim[3] = LEVEL_CONTEXTS;
   cts_each_dim[4] = 2;
   optimize_entropy_table(&fc.coeff_lps[0][0][0][0][0], probsfile, 5,
-                         cts_each_dim, NULL, 1,
+                         cts_each_dim, 1,
                          "static const aom_prob "
                          "default_coeff_lps[TX_SIZES][PLANE_TYPES][BR_CDF_SIZE-"
                          "1][LEVEL_CONTEXTS]");
@@ -709,7 +661,7 @@ int main(int argc, const char **argv) {
   cts_each_dim[0] = SKIP_MODE_CONTEXTS;
   cts_each_dim[1] = 2;
   optimize_entropy_table(
-      &fc.skip_mode[0][0], probsfile, 2, cts_each_dim, NULL, 1,
+      &fc.skip_mode[0][0], probsfile, 2, cts_each_dim, 1,
       "static const aom_prob default_skip_mode_probs[SKIP_MODE_CONTEXTS]");
   optimize_cdf_table(&fc.skip_mode[0][0], probsfile, 2, cts_each_dim,
                      "static const aom_cdf_prob "
-- 
GitLab