OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 /* We build coding trees compactly in arrays. | 28 /* We build coding trees compactly in arrays. |
29 Each node of the tree is a pair of vp9_tree_indices. | 29 Each node of the tree is a pair of vp9_tree_indices. |
30 Array index often references a corresponding probability table. | 30 Array index often references a corresponding probability table. |
31 Index <= 0 means done encoding/decoding and value = -Index, | 31 Index <= 0 means done encoding/decoding and value = -Index, |
32 Index > 0 means need another bit, specification at index. | 32 Index > 0 means need another bit, specification at index. |
33 Nonnegative indices are always even; processing begins at node 0. */ | 33 Nonnegative indices are always even; processing begins at node 0. */ |
34 | 34 |
35 typedef const vp9_tree_index vp9_tree[]; | 35 typedef const vp9_tree_index vp9_tree[]; |
36 | 36 |
37 struct vp9_token { | |
38 int value; | |
39 int len; | |
40 }; | |
41 | |
42 /* Construct encoding array from tree. */ | |
43 | |
44 void vp9_tokens_from_tree(struct vp9_token*, vp9_tree); | |
45 void vp9_tokens_from_tree_offset(struct vp9_token*, vp9_tree, int offset); | |
46 | |
47 /* Convert array of token occurrence counts into a table of probabilities | 37 /* Convert array of token occurrence counts into a table of probabilities |
48 for the associated binary encoding tree. Also writes count of branches | 38 for the associated binary encoding tree. Also writes count of branches |
49 taken for each node on the tree; this facilitiates decisions as to | 39 taken for each node on the tree; this facilitiates decisions as to |
50 probability updates. */ | 40 probability updates. */ |
51 | 41 |
52 void vp9_tree_probs_from_distribution(vp9_tree tree, | |
53 vp9_prob probs[ /* n - 1 */ ], | |
54 unsigned int branch_ct[ /* n - 1 */ ][2], | |
55 const unsigned int num_events[ /* n */ ], | |
56 unsigned int tok0_offset); | |
57 | |
58 static INLINE vp9_prob clip_prob(int p) { | 42 static INLINE vp9_prob clip_prob(int p) { |
59 return (p > 255) ? 255u : (p < 1) ? 1u : p; | 43 return (p > 255) ? 255u : (p < 1) ? 1u : p; |
60 } | 44 } |
61 | 45 |
62 // int64 is not needed for normal frame level calculations. | 46 // int64 is not needed for normal frame level calculations. |
63 // However when outputing entropy stats accumulated over many frames | 47 // However when outputing entropy stats accumulated over many frames |
64 // or even clips we can overflow int math. | 48 // or even clips we can overflow int math. |
65 #ifdef ENTROPY_STATS | 49 #ifdef ENTROPY_STATS |
66 static INLINE vp9_prob get_prob(int num, int den) { | 50 static INLINE vp9_prob get_prob(int num, int den) { |
67 return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den); | 51 return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den); |
68 } | 52 } |
69 #else | 53 #else |
70 static INLINE vp9_prob get_prob(int num, int den) { | 54 static INLINE vp9_prob get_prob(int num, int den) { |
71 return (den == 0) ? 128u : clip_prob((num * 256 + (den >> 1)) / den); | 55 return (den == 0) ? 128u : clip_prob((num * 256 + (den >> 1)) / den); |
72 } | 56 } |
73 #endif | 57 #endif |
74 | 58 |
75 static INLINE vp9_prob get_binary_prob(int n0, int n1) { | 59 static INLINE vp9_prob get_binary_prob(int n0, int n1) { |
76 return get_prob(n0, n0 + n1); | 60 return get_prob(n0, n0 + n1); |
77 } | 61 } |
78 | 62 |
79 /* this function assumes prob1 and prob2 are already within [1,255] range */ | 63 /* this function assumes prob1 and prob2 are already within [1,255] range */ |
80 static INLINE vp9_prob weighted_prob(int prob1, int prob2, int factor) { | 64 static INLINE vp9_prob weighted_prob(int prob1, int prob2, int factor) { |
81 return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8); | 65 return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8); |
82 } | 66 } |
83 | 67 |
84 static INLINE vp9_prob merge_probs(vp9_prob pre_prob, vp9_prob prob, | 68 static INLINE vp9_prob merge_probs(vp9_prob pre_prob, |
85 const unsigned int ct[2], | 69 const unsigned int ct[2], |
86 unsigned int count_sat, | 70 unsigned int count_sat, |
87 unsigned int max_update_factor) { | 71 unsigned int max_update_factor) { |
| 72 const vp9_prob prob = get_binary_prob(ct[0], ct[1]); |
88 const unsigned int count = MIN(ct[0] + ct[1], count_sat); | 73 const unsigned int count = MIN(ct[0] + ct[1], count_sat); |
89 const unsigned int factor = max_update_factor * count / count_sat; | 74 const unsigned int factor = max_update_factor * count / count_sat; |
90 return weighted_prob(pre_prob, prob, factor); | 75 return weighted_prob(pre_prob, prob, factor); |
91 } | 76 } |
92 | 77 |
93 static INLINE vp9_prob merge_probs2(vp9_prob pre_prob, | 78 static unsigned int tree_merge_probs_impl(unsigned int i, |
94 const unsigned int ct[2], | 79 const vp9_tree_index *tree, |
95 unsigned int count_sat, | 80 const vp9_prob *pre_probs, |
96 unsigned int max_update_factor) { | 81 const unsigned int *counts, |
97 return merge_probs(pre_prob, get_binary_prob(ct[0], ct[1]), ct, count_sat, | 82 unsigned int count_sat, |
98 max_update_factor); | 83 unsigned int max_update_factor, |
| 84 vp9_prob *probs) { |
| 85 const int l = tree[i]; |
| 86 const unsigned int left_count = (l <= 0) |
| 87 ? counts[-l] |
| 88 : tree_merge_probs_impl(l, tree, pre_probs, counts, |
| 89 count_sat, max_update_factor, probs); |
| 90 const int r = tree[i + 1]; |
| 91 const unsigned int right_count = (r <= 0) |
| 92 ? counts[-r] |
| 93 : tree_merge_probs_impl(r, tree, pre_probs, counts, |
| 94 count_sat, max_update_factor, probs); |
| 95 const unsigned int ct[2] = { left_count, right_count }; |
| 96 probs[i >> 1] = merge_probs(pre_probs[i >> 1], ct, |
| 97 count_sat, max_update_factor); |
| 98 return left_count + right_count; |
| 99 } |
| 100 |
| 101 static void tree_merge_probs(const vp9_tree_index *tree, |
| 102 const vp9_prob *pre_probs, |
| 103 const unsigned int *counts, |
| 104 unsigned int count_sat, |
| 105 unsigned int max_update_factor, vp9_prob *probs) { |
| 106 tree_merge_probs_impl(0, tree, pre_probs, counts, |
| 107 count_sat, max_update_factor, probs); |
99 } | 108 } |
100 | 109 |
101 | 110 |
102 #endif // VP9_COMMON_VP9_TREECODER_H_ | 111 #endif // VP9_COMMON_VP9_TREECODER_H_ |
OLD | NEW |