diff --git a/av1/encoder/treewriter.c b/av1/encoder/treewriter.c
index 50be72413bc6d60d94146ebd9e672ff8804bf65c..742ffca23798e31923fd314bdce68140e1ba2525 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 9a4cb86cb24d6561fd96f5503b8bade94ba144de..e16b43fcd85dbba5642fe448fd1013b7148ba8b5 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,