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