From 4531b85a252265e299602e15e35696420dd76e01 Mon Sep 17 00:00:00 2001
From: "Nathan E. Egge" <negge@mozilla.com>
Date: Sun, 19 Jun 2016 14:38:04 -0400
Subject: [PATCH] Add code to compute in-order mappings for tokens.

Add av1_indices_from_tree() function that computes a forward and inverse
 mapping of the tree leaf-node symbols to their in-order traversal.
This is necessary because many of the aom_tree binary trees have their
 leaf nodes out of order (e.g., an in-order traversal of a tree with n
 nodes does not start at symbol 0 and go to symbol n - 1), but the CDFs
 created by tree_to_cdf() are indexed in-order.

Change-Id: Icd0dbed4c171a67c9e84a634106c4fdb5b1b3488
---
 av1/encoder/treewriter.c | 16 ++++++++++++++++
 av1/encoder/treewriter.h |  2 ++
 2 files changed, 18 insertions(+)

diff --git a/av1/encoder/treewriter.c b/av1/encoder/treewriter.c
index 50be72413b..742ffca237 100644
--- a/av1/encoder/treewriter.c
+++ b/av1/encoder/treewriter.c
@@ -32,6 +32,22 @@ void av1_tokens_from_tree(struct av1_token *tokens,
   tree2tok(tokens, tree, 0, 0, 0);
 }
 
+/* This code assumes that tree contains as unique leaf nodes the integer values
+    0 to len - 1 and produces the forward and inverse mapping tables in ind[]
+    and inv[] respectively. */
+void av1_indices_from_tree(int *ind, int *inv, int len,
+                           const aom_tree_index *tree) {
+  int i;
+  int index;
+  for (i = index = 0; i < TREE_SIZE(len); i++) {
+    const aom_tree_index j = tree[i];
+    if (j <= 0) {
+      inv[index] = -j;
+      ind[-j] = index++;
+    }
+  }
+}
+
 static unsigned int convert_distribution(unsigned int i, aom_tree tree,
                                          unsigned int branch_ct[][2],
                                          const unsigned int num_events[]) {
diff --git a/av1/encoder/treewriter.h b/av1/encoder/treewriter.h
index 9a4cb86cb2..e16b43fcd8 100644
--- a/av1/encoder/treewriter.h
+++ b/av1/encoder/treewriter.h
@@ -28,6 +28,8 @@ struct av1_token {
 };
 
 void av1_tokens_from_tree(struct av1_token *, const aom_tree_index *);
+void av1_indices_from_tree(int *ind, int *inv, int len,
+                           const aom_tree_index *tree);
 
 static INLINE void av1_write_token(aom_writer *w, const aom_tree_index *tree,
                                    const aom_prob *probs,
-- 
GitLab