OLD | NEW |
(Empty) | |
| 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 |
| 11 |
| 12 #include "vpx_config.h" |
| 13 |
| 14 #if defined(CONFIG_DEBUG) && CONFIG_DEBUG |
| 15 #include <assert.h> |
| 16 #endif |
| 17 #include <stdio.h> |
| 18 |
| 19 #include "vp9/common/vp9_treecoder.h" |
| 20 |
| 21 static void tree2tok( |
| 22 struct vp9_token_struct *const p, |
| 23 vp9_tree t, |
| 24 int i, |
| 25 int v, |
| 26 int L |
| 27 ) { |
| 28 v += v; |
| 29 ++L; |
| 30 |
| 31 do { |
| 32 const vp9_tree_index j = t[i++]; |
| 33 |
| 34 if (j <= 0) { |
| 35 p[-j].value = v; |
| 36 p[-j].Len = L; |
| 37 } else |
| 38 tree2tok(p, t, j, v, L); |
| 39 } while (++v & 1); |
| 40 } |
| 41 |
| 42 void vp9_tokens_from_tree(struct vp9_token_struct *p, vp9_tree t) { |
| 43 tree2tok(p, t, 0, 0, 0); |
| 44 } |
| 45 |
| 46 void vp9_tokens_from_tree_offset(struct vp9_token_struct *p, vp9_tree t, |
| 47 int offset) { |
| 48 tree2tok(p - offset, t, 0, 0, 0); |
| 49 } |
| 50 |
| 51 static void branch_counts( |
| 52 int n, /* n = size of alphabet */ |
| 53 vp9_token tok [ /* n */ ], |
| 54 vp9_tree tree, |
| 55 unsigned int branch_ct [ /* n-1 */ ] [2], |
| 56 const unsigned int num_events[ /* n */ ] |
| 57 ) { |
| 58 const int tree_len = n - 1; |
| 59 int t = 0; |
| 60 |
| 61 #if CONFIG_DEBUG |
| 62 assert(tree_len); |
| 63 #endif |
| 64 |
| 65 do { |
| 66 branch_ct[t][0] = branch_ct[t][1] = 0; |
| 67 } while (++t < tree_len); |
| 68 |
| 69 t = 0; |
| 70 |
| 71 do { |
| 72 int L = tok[t].Len; |
| 73 const int enc = tok[t].value; |
| 74 const unsigned int ct = num_events[t]; |
| 75 |
| 76 vp9_tree_index i = 0; |
| 77 |
| 78 do { |
| 79 const int b = (enc >> --L) & 1; |
| 80 const int j = i >> 1; |
| 81 #if CONFIG_DEBUG |
| 82 assert(j < tree_len && 0 <= L); |
| 83 #endif |
| 84 |
| 85 branch_ct [j] [b] += ct; |
| 86 i = tree[ i + b]; |
| 87 } while (i > 0); |
| 88 |
| 89 #if CONFIG_DEBUG |
| 90 assert(!L); |
| 91 #endif |
| 92 } while (++t < n); |
| 93 |
| 94 } |
| 95 |
| 96 |
| 97 void vp9_tree_probs_from_distribution( |
| 98 int n, /* n = size of alphabet */ |
| 99 vp9_token tok [ /* n */ ], |
| 100 vp9_tree tree, |
| 101 vp9_prob probs [ /* n-1 */ ], |
| 102 unsigned int branch_ct [ /* n-1 */ ] [2], |
| 103 const unsigned int num_events[ /* n */ ], |
| 104 unsigned int Pfac, |
| 105 int rd |
| 106 ) { |
| 107 const int tree_len = n - 1; |
| 108 int t = 0; |
| 109 |
| 110 branch_counts(n, tok, tree, branch_ct, num_events); |
| 111 |
| 112 do { |
| 113 const unsigned int *const c = branch_ct[t]; |
| 114 const unsigned int tot = c[0] + c[1]; |
| 115 |
| 116 #if CONFIG_DEBUG |
| 117 assert(tot < (1 << 24)); /* no overflow below */ |
| 118 #endif |
| 119 |
| 120 if (tot) { |
| 121 const unsigned int p = ((c[0] * Pfac) + (rd ? tot >> 1 : 0)) / tot; |
| 122 probs[t] = p < 256 ? (p ? p : 1) : 255; /* agree w/old version for now */ |
| 123 } else |
| 124 probs[t] = vp9_prob_half; |
| 125 } while (++t < tree_len); |
| 126 } |
| 127 |
| 128 vp9_prob vp9_bin_prob_from_distribution(const unsigned int counts[2]) { |
| 129 int tot_count = counts[0] + counts[1]; |
| 130 vp9_prob prob; |
| 131 if (tot_count) { |
| 132 prob = (counts[0] * 255 + (tot_count >> 1)) / tot_count; |
| 133 prob += !prob; |
| 134 } else { |
| 135 prob = 128; |
| 136 } |
| 137 return prob; |
| 138 } |
OLD | NEW |