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