Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #ifndef VP9_COMMON_VP9_TREECODER_H_ |
michael@0 | 12 | #define VP9_COMMON_VP9_TREECODER_H_ |
michael@0 | 13 | |
michael@0 | 14 | #include "./vpx_config.h" |
michael@0 | 15 | #include "vpx/vpx_integer.h" |
michael@0 | 16 | #include "vp9/common/vp9_common.h" |
michael@0 | 17 | |
michael@0 | 18 | typedef uint8_t vp9_prob; |
michael@0 | 19 | |
michael@0 | 20 | #define vp9_prob_half ((vp9_prob) 128) |
michael@0 | 21 | |
michael@0 | 22 | typedef int8_t vp9_tree_index; |
michael@0 | 23 | |
michael@0 | 24 | #define TREE_SIZE(leaf_count) (2 * (leaf_count) - 2) |
michael@0 | 25 | |
michael@0 | 26 | #define vp9_complement(x) (255 - x) |
michael@0 | 27 | |
michael@0 | 28 | /* We build coding trees compactly in arrays. |
michael@0 | 29 | Each node of the tree is a pair of vp9_tree_indices. |
michael@0 | 30 | Array index often references a corresponding probability table. |
michael@0 | 31 | Index <= 0 means done encoding/decoding and value = -Index, |
michael@0 | 32 | Index > 0 means need another bit, specification at index. |
michael@0 | 33 | Nonnegative indices are always even; processing begins at node 0. */ |
michael@0 | 34 | |
michael@0 | 35 | typedef const vp9_tree_index vp9_tree[]; |
michael@0 | 36 | |
michael@0 | 37 | struct vp9_token { |
michael@0 | 38 | int value; |
michael@0 | 39 | int len; |
michael@0 | 40 | }; |
michael@0 | 41 | |
michael@0 | 42 | /* Construct encoding array from tree. */ |
michael@0 | 43 | |
michael@0 | 44 | void vp9_tokens_from_tree(struct vp9_token*, vp9_tree); |
michael@0 | 45 | |
michael@0 | 46 | /* Convert array of token occurrence counts into a table of probabilities |
michael@0 | 47 | for the associated binary encoding tree. Also writes count of branches |
michael@0 | 48 | taken for each node on the tree; this facilitiates decisions as to |
michael@0 | 49 | probability updates. */ |
michael@0 | 50 | |
michael@0 | 51 | void vp9_tree_probs_from_distribution(vp9_tree tree, |
michael@0 | 52 | unsigned int branch_ct[ /* n - 1 */ ][2], |
michael@0 | 53 | const unsigned int num_events[ /* n */ ]); |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | static INLINE vp9_prob clip_prob(int p) { |
michael@0 | 57 | return (p > 255) ? 255u : (p < 1) ? 1u : p; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | // int64 is not needed for normal frame level calculations. |
michael@0 | 61 | // However when outputing entropy stats accumulated over many frames |
michael@0 | 62 | // or even clips we can overflow int math. |
michael@0 | 63 | #ifdef ENTROPY_STATS |
michael@0 | 64 | static INLINE vp9_prob get_prob(int num, int den) { |
michael@0 | 65 | return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den); |
michael@0 | 66 | } |
michael@0 | 67 | #else |
michael@0 | 68 | static INLINE vp9_prob get_prob(int num, int den) { |
michael@0 | 69 | return (den == 0) ? 128u : clip_prob((num * 256 + (den >> 1)) / den); |
michael@0 | 70 | } |
michael@0 | 71 | #endif |
michael@0 | 72 | |
michael@0 | 73 | static INLINE vp9_prob get_binary_prob(int n0, int n1) { |
michael@0 | 74 | return get_prob(n0, n0 + n1); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | /* this function assumes prob1 and prob2 are already within [1,255] range */ |
michael@0 | 78 | static INLINE vp9_prob weighted_prob(int prob1, int prob2, int factor) { |
michael@0 | 79 | return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | static INLINE vp9_prob merge_probs(vp9_prob pre_prob, |
michael@0 | 83 | const unsigned int ct[2], |
michael@0 | 84 | unsigned int count_sat, |
michael@0 | 85 | unsigned int max_update_factor) { |
michael@0 | 86 | const vp9_prob prob = get_binary_prob(ct[0], ct[1]); |
michael@0 | 87 | const unsigned int count = MIN(ct[0] + ct[1], count_sat); |
michael@0 | 88 | const unsigned int factor = max_update_factor * count / count_sat; |
michael@0 | 89 | return weighted_prob(pre_prob, prob, factor); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | static unsigned int tree_merge_probs_impl(unsigned int i, |
michael@0 | 93 | const vp9_tree_index *tree, |
michael@0 | 94 | const vp9_prob *pre_probs, |
michael@0 | 95 | const unsigned int *counts, |
michael@0 | 96 | unsigned int count_sat, |
michael@0 | 97 | unsigned int max_update_factor, |
michael@0 | 98 | vp9_prob *probs) { |
michael@0 | 99 | const int l = tree[i]; |
michael@0 | 100 | const unsigned int left_count = (l <= 0) |
michael@0 | 101 | ? counts[-l] |
michael@0 | 102 | : tree_merge_probs_impl(l, tree, pre_probs, counts, |
michael@0 | 103 | count_sat, max_update_factor, probs); |
michael@0 | 104 | const int r = tree[i + 1]; |
michael@0 | 105 | const unsigned int right_count = (r <= 0) |
michael@0 | 106 | ? counts[-r] |
michael@0 | 107 | : tree_merge_probs_impl(r, tree, pre_probs, counts, |
michael@0 | 108 | count_sat, max_update_factor, probs); |
michael@0 | 109 | const unsigned int ct[2] = { left_count, right_count }; |
michael@0 | 110 | probs[i >> 1] = merge_probs(pre_probs[i >> 1], ct, |
michael@0 | 111 | count_sat, max_update_factor); |
michael@0 | 112 | return left_count + right_count; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | static void tree_merge_probs(const vp9_tree_index *tree, |
michael@0 | 116 | const vp9_prob *pre_probs, |
michael@0 | 117 | const unsigned int *counts, |
michael@0 | 118 | unsigned int count_sat, |
michael@0 | 119 | unsigned int max_update_factor, vp9_prob *probs) { |
michael@0 | 120 | tree_merge_probs_impl(0, tree, pre_probs, counts, |
michael@0 | 121 | count_sat, max_update_factor, probs); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | |
michael@0 | 125 | #endif // VP9_COMMON_VP9_TREECODER_H_ |