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.
17 #include "treecoder.h"
20 struct vp8_token_struct
*const p
,
32 const vp8_tree_index j
= t
[i
++];
40 tree2tok(p
, t
, j
, v
, L
);
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
,
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 */ ],
60 unsigned int branch_ct
[ /* n-1 */ ] [2],
61 const unsigned int num_events
[ /* n */ ]
64 const int tree_len
= n
- 1;
73 branch_ct
[t
][0] = branch_ct
[t
][1] = 0;
75 while (++t
< tree_len
);
82 const int enc
= tok
[t
].value
;
83 const unsigned int ct
= num_events
[t
];
89 const int b
= (enc
>> --L
) & 1;
92 assert(j
< tree_len
&& 0 <= L
);
95 branch_ct
[j
] [b
] += ct
;
109 void vp8_tree_probs_from_distribution(
110 int n
, /* n = size of alphabet */
111 vp8_token tok
[ /* n */ ],
113 vp8_prob probs
[ /* n-1 */ ],
114 unsigned int branch_ct
[ /* n-1 */ ] [2],
115 const unsigned int num_events
[ /* n */ ],
120 const int tree_len
= n
- 1;
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];
131 assert(tot
< (1 << 24)); /* no overflow below */
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 */
140 probs
[t
] = vp8_prob_half
;
142 while (++t
< tree_len
);