OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 | |
12 #ifndef VP9_COMMON_VP9_TREECODER_H_ | 11 #ifndef VP9_COMMON_VP9_TREECODER_H_ |
13 #define VP9_COMMON_VP9_TREECODER_H_ | 12 #define VP9_COMMON_VP9_TREECODER_H_ |
14 | 13 |
15 typedef unsigned char vp9_prob; | 14 #include "vpx/vpx_integer.h" |
16 | 15 |
17 #define vp9_prob_half ( (vp9_prob) 128) | 16 typedef uint8_t vp9_prob; |
18 | 17 |
19 typedef signed char vp9_tree_index; | 18 #define vp9_prob_half ((vp9_prob) 128) |
20 struct bool_coder_spec; | |
21 | 19 |
22 typedef struct bool_coder_spec bool_coder_spec; | 20 typedef int8_t vp9_tree_index; |
23 typedef struct bool_writer bool_writer; | |
24 typedef struct bool_reader bool_reader; | |
25 | 21 |
26 typedef const bool_coder_spec c_bool_coder_spec; | 22 #define vp9_complement(x) (255 - x) |
27 typedef const bool_writer c_bool_writer; | |
28 typedef const bool_reader c_bool_reader; | |
29 | |
30 | |
31 | |
32 # define vp9_complement( x) (255 - x) | |
33 | |
34 | 23 |
35 /* We build coding trees compactly in arrays. | 24 /* We build coding trees compactly in arrays. |
36 Each node of the tree is a pair of vp9_tree_indices. | 25 Each node of the tree is a pair of vp9_tree_indices. |
37 Array index often references a corresponding probability table. | 26 Array index often references a corresponding probability table. |
38 Index <= 0 means done encoding/decoding and value = -Index, | 27 Index <= 0 means done encoding/decoding and value = -Index, |
39 Index > 0 means need another bit, specification at index. | 28 Index > 0 means need another bit, specification at index. |
40 Nonnegative indices are always even; processing begins at node 0. */ | 29 Nonnegative indices are always even; processing begins at node 0. */ |
41 | 30 |
42 typedef const vp9_tree_index vp9_tree[], *vp9_tree_p; | 31 typedef const vp9_tree_index vp9_tree[], *vp9_tree_p; |
43 | 32 |
44 | |
45 typedef const struct vp9_token_struct { | 33 typedef const struct vp9_token_struct { |
46 int value; | 34 int value; |
47 int Len; | 35 int Len; |
48 } vp9_token; | 36 } vp9_token; |
49 | 37 |
50 /* Construct encoding array from tree. */ | 38 /* Construct encoding array from tree. */ |
51 | 39 |
52 void vp9_tokens_from_tree(struct vp9_token_struct *, vp9_tree); | 40 void vp9_tokens_from_tree(struct vp9_token_struct *, vp9_tree); |
53 void vp9_tokens_from_tree_offset(struct vp9_token_struct *, vp9_tree, | 41 void vp9_tokens_from_tree_offset(struct vp9_token_struct *, vp9_tree, |
54 int offset); | 42 int offset); |
55 | 43 |
56 | |
57 /* Convert array of token occurrence counts into a table of probabilities | 44 /* Convert array of token occurrence counts into a table of probabilities |
58 for the associated binary encoding tree. Also writes count of branches | 45 for the associated binary encoding tree. Also writes count of branches |
59 taken for each node on the tree; this facilitiates decisions as to | 46 taken for each node on the tree; this facilitiates decisions as to |
60 probability updates. */ | 47 probability updates. */ |
61 | 48 |
62 void vp9_tree_probs_from_distribution( | 49 void vp9_tree_probs_from_distribution(int n, /* n = size of alphabet */ |
63 int n, /* n = size of alphabet */ | 50 vp9_token tok[ /* n */ ], |
64 vp9_token tok [ /* n */ ], | 51 vp9_tree tree, |
65 vp9_tree tree, | 52 vp9_prob probs[ /* n - 1 */ ], |
66 vp9_prob probs [ /* n-1 */ ], | 53 unsigned int branch_ct[ /* n - 1 */ ][2], |
67 unsigned int branch_ct [ /* n-1 */ ] [2], | 54 const unsigned int num_events[ /* n */ ]); |
68 const unsigned int num_events[ /* n */ ], | |
69 unsigned int Pfactor, | |
70 int Round | |
71 ); | |
72 | 55 |
73 static __inline int clip_prob(int p) { | 56 static __inline vp9_prob clip_prob(int p) { |
74 if (p > 255) | 57 return (p > 255) ? 255u : (p < 1) ? 1u : p; |
75 return 255; | |
76 else if (p < 1) | |
77 return 1; | |
78 return p; | |
79 } | 58 } |
80 | 59 |
81 vp9_prob vp9_bin_prob_from_distribution(const unsigned int counts[2]); | 60 static __inline vp9_prob get_prob(int num, int den) { |
| 61 return (den == 0) ? 128u : clip_prob((num * 256 + (den >> 1)) / den); |
| 62 } |
82 | 63 |
83 #endif | 64 static __inline vp9_prob get_binary_prob(int n0, int n1) { |
| 65 return get_prob(n0, n0 + n1); |
| 66 } |
| 67 |
| 68 /* this function assumes prob1 and prob2 are already within [1,255] range */ |
| 69 static __inline vp9_prob weighted_prob(int prob1, int prob2, int factor) { |
| 70 return (prob1 * (256 - factor) + prob2 * factor + 128) >> 8; |
| 71 } |
| 72 |
| 73 #endif // VP9_COMMON_VP9_TREECODER_H_ |
OLD | NEW |