1 /*
2  *  Copyright (c) 2013 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 #ifndef VP9_COMMON_VP9_PROB_H_
12 #define VP9_COMMON_VP9_PROB_H_
13 
14 #include "./vpx_config.h"
15 
16 #include "vpx_ports/mem.h"
17 
18 #include "vp9/common/vp9_common.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 typedef uint8_t vp9_prob;
25 
26 #define MAX_PROB 255
27 
28 #define vp9_prob_half ((vp9_prob) 128)
29 
30 typedef int8_t vp9_tree_index;
31 
32 #define TREE_SIZE(leaf_count) (2 * (leaf_count) - 2)
33 
34 #define vp9_complement(x) (255 - x)
35 
36 #define MODE_MV_COUNT_SAT 20
37 
38 /* We build coding trees compactly in arrays.
39    Each node of the tree is a pair of vp9_tree_indices.
40    Array index often references a corresponding probability table.
41    Index <= 0 means done encoding/decoding and value = -Index,
42    Index > 0 means need another bit, specification at index.
43    Nonnegative indices are always even;  processing begins at node 0. */
44 
45 typedef const vp9_tree_index vp9_tree[];
46 
clip_prob(int p)47 static INLINE vp9_prob clip_prob(int p) {
48   return (p > 255) ? 255 : (p < 1) ? 1 : p;
49 }
50 
get_prob(int num,int den)51 static INLINE vp9_prob get_prob(int num, int den) {
52   return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den);
53 }
54 
get_binary_prob(int n0,int n1)55 static INLINE vp9_prob get_binary_prob(int n0, int n1) {
56   return get_prob(n0, n0 + n1);
57 }
58 
59 /* This function assumes prob1 and prob2 are already within [1,255] range. */
weighted_prob(int prob1,int prob2,int factor)60 static INLINE vp9_prob weighted_prob(int prob1, int prob2, int factor) {
61   return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8);
62 }
63 
merge_probs(vp9_prob pre_prob,const unsigned int ct[2],unsigned int count_sat,unsigned int max_update_factor)64 static INLINE vp9_prob merge_probs(vp9_prob pre_prob,
65                                    const unsigned int ct[2],
66                                    unsigned int count_sat,
67                                    unsigned int max_update_factor) {
68   const vp9_prob prob = get_binary_prob(ct[0], ct[1]);
69   const unsigned int count = MIN(ct[0] + ct[1], count_sat);
70   const unsigned int factor = max_update_factor * count / count_sat;
71   return weighted_prob(pre_prob, prob, factor);
72 }
73 
74 // MODE_MV_MAX_UPDATE_FACTOR (128) * count / MODE_MV_COUNT_SAT;
75 static const int count_to_update_factor[MODE_MV_COUNT_SAT + 1] = {
76   0, 6, 12, 19, 25, 32, 38, 44, 51, 57, 64,
77   70, 76, 83, 89, 96, 102, 108, 115, 121, 128
78 };
79 
mode_mv_merge_probs(vp9_prob pre_prob,const unsigned int ct[2])80 static INLINE vp9_prob mode_mv_merge_probs(vp9_prob pre_prob,
81                                            const unsigned int ct[2]) {
82   const unsigned int den = ct[0] + ct[1];
83   if (den == 0) {
84     return pre_prob;
85   } else {
86     const unsigned int count = MIN(den, MODE_MV_COUNT_SAT);
87     const unsigned int factor = count_to_update_factor[count];
88     const vp9_prob prob =
89         clip_prob(((int64_t)(ct[0]) * 256 + (den >> 1)) / den);
90     return weighted_prob(pre_prob, prob, factor);
91   }
92 }
93 
94 void vp9_tree_merge_probs(const vp9_tree_index *tree, const vp9_prob *pre_probs,
95                           const unsigned int *counts, vp9_prob *probs);
96 
97 
98 DECLARE_ALIGNED(16, extern const uint8_t, vp9_norm[256]);
99 
100 #ifdef __cplusplus
101 }  // extern "C"
102 #endif
103 
104 #endif  // VP9_COMMON_VP9_PROB_H_
105