1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #define INLINE __inline
11 
12 #include <stdint.h>
13 #include "vp9_treewriter.h"
14 
tree2tok(struct vp9_token * tokens,const vpx_tree_index * tree,int i,int v,int l)15 static void tree2tok(struct vp9_token *tokens, const vpx_tree_index *tree,
16                      int i, int v, int l) {
17   v += v;
18   ++l;
19 
20   do {
21     const vpx_tree_index j = tree[i++];
22     if (j <= 0) {
23       tokens[-j].value = v;
24       tokens[-j].len = l;
25     } else {
26       tree2tok(tokens, tree, j, v, l);
27     }
28   } while (++v & 1);
29 }
30 
eb_vp9_tokens_from_tree(struct vp9_token * tokens,const vpx_tree_index * tree)31 void eb_vp9_tokens_from_tree(struct vp9_token *tokens,
32                           const vpx_tree_index *tree) {
33   tree2tok(tokens, tree, 0, 0, 0);
34 }
35 
convert_distribution(unsigned int i,vpx_tree tree,unsigned int branch_ct[][2],const unsigned int num_events[])36 static unsigned int convert_distribution(unsigned int i, vpx_tree tree,
37                                          unsigned int branch_ct[][2],
38                                          const unsigned int num_events[]) {
39   unsigned int left, right;
40 
41   if (tree[i] <= 0)
42     left = num_events[-tree[i]];
43   else
44     left = convert_distribution(tree[i], tree, branch_ct, num_events);
45 
46   if (tree[i + 1] <= 0)
47     right = num_events[-tree[i + 1]];
48   else
49     right = convert_distribution(tree[i + 1], tree, branch_ct, num_events);
50 
51   branch_ct[i >> 1][0] = left;
52   branch_ct[i >> 1][1] = right;
53   return left + right;
54 }
55 
eb_vp9_tree_probs_from_distribution(vpx_tree tree,unsigned int branch_ct[][2],const unsigned int num_events[])56 void eb_vp9_tree_probs_from_distribution(vpx_tree tree,
57                                       unsigned int branch_ct[/* n-1 */][2],
58                                       const unsigned int num_events[/* n */]) {
59   convert_distribution(0, tree, branch_ct, num_events);
60 }
61