Merge "vp8_rd_pick_best_mbsegmentation code restructure"
[libvpx.git] / vp8 / common / treecoder.c
blobd80c64bdfa1b079bbbc14efbee3c33024e864d2b
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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 */
12 #if CONFIG_DEBUG
13 #include <assert.h>
14 #endif
15 #include <stdio.h>
17 #include "treecoder.h"
19 static void tree2tok(
20 struct vp8_token_struct *const p,
21 vp8_tree t,
22 int i,
23 int v,
24 int L
27 v += v;
28 ++L;
32 const vp8_tree_index j = t[i++];
34 if (j <= 0)
36 p[-j].value = v;
37 p[-j].Len = L;
39 else
40 tree2tok(p, t, j, v, L);
42 while (++v & 1);
45 void vp8_tokens_from_tree(struct vp8_token_struct *p, vp8_tree t)
47 tree2tok(p, t, 0, 0, 0);
50 void vp8_tokens_from_tree_offset(struct vp8_token_struct *p, vp8_tree t,
51 int offset)
53 tree2tok(p - offset, t, 0, 0, 0);
56 static void branch_counts(
57 int n, /* n = size of alphabet */
58 vp8_token tok [ /* n */ ],
59 vp8_tree tree,
60 unsigned int branch_ct [ /* n-1 */ ] [2],
61 const unsigned int num_events[ /* n */ ]
64 const int tree_len = n - 1;
65 int t = 0;
67 #if CONFIG_DEBUG
68 assert(tree_len);
69 #endif
73 branch_ct[t][0] = branch_ct[t][1] = 0;
75 while (++t < tree_len);
77 t = 0;
81 int L = tok[t].Len;
82 const int enc = tok[t].value;
83 const unsigned int ct = num_events[t];
85 vp8_tree_index i = 0;
89 const int b = (enc >> --L) & 1;
90 const int j = i >> 1;
91 #if CONFIG_DEBUG
92 assert(j < tree_len && 0 <= L);
93 #endif
95 branch_ct [j] [b] += ct;
96 i = tree[ i + b];
98 while (i > 0);
100 #if CONFIG_DEBUG
101 assert(!L);
102 #endif
104 while (++t < n);
109 void vp8_tree_probs_from_distribution(
110 int n, /* n = size of alphabet */
111 vp8_token tok [ /* n */ ],
112 vp8_tree tree,
113 vp8_prob probs [ /* n-1 */ ],
114 unsigned int branch_ct [ /* n-1 */ ] [2],
115 const unsigned int num_events[ /* n */ ],
116 unsigned int Pfac,
117 int rd
120 const int tree_len = n - 1;
121 int t = 0;
123 branch_counts(n, tok, tree, branch_ct, num_events);
127 const unsigned int *const c = branch_ct[t];
128 const unsigned int tot = c[0] + c[1];
130 #if CONFIG_DEBUG
131 assert(tot < (1 << 24)); /* no overflow below */
132 #endif
134 if (tot)
136 const unsigned int p = ((c[0] * Pfac) + (rd ? tot >> 1 : 0)) / tot;
137 probs[t] = p < 256 ? (p ? p : 1) : 255; /* agree w/old version for now */
139 else
140 probs[t] = vp8_prob_half;
142 while (++t < tree_len);