1 /*
2  *  Copyright (c) 2010 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 
12 #ifndef VP8_ENCODER_TREEWRITER_H_
13 #define VP8_ENCODER_TREEWRITER_H_
14 
15 /* Trees map alphabets into huffman-like codes suitable for an arithmetic
16    bit coder.  Timothy S Murphy  11 October 2004 */
17 
18 #include "./vpx_config.h"
19 #include "vp8/common/treecoder.h"
20 
21 #include "boolhuff.h"       /* for now */
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef BOOL_CODER vp8_writer;
28 
29 #define vp8_write vp8_encode_bool
30 #define vp8_write_literal vp8_encode_value
31 #define vp8_write_bit( W, V) vp8_write( W, V, vp8_prob_half)
32 
33 #define vp8bc_write vp8bc_write_bool
34 #define vp8bc_write_literal vp8bc_write_bits
35 #define vp8bc_write_bit( W, V) vp8bc_write_bits( W, V, 1)
36 
37 
38 /* Approximate length of an encoded bool in 256ths of a bit at given prob */
39 
40 #define vp8_cost_zero( x) ( vp8_prob_cost[x])
41 #define vp8_cost_one( x)  vp8_cost_zero( vp8_complement(x))
42 
43 #define vp8_cost_bit( x, b) vp8_cost_zero( (b)?  vp8_complement(x) : (x) )
44 
45 /* VP8BC version is scaled by 2^20 rather than 2^8; see bool_coder.h */
46 
47 
48 /* Both of these return bits, not scaled bits. */
49 
vp8_cost_branch(const unsigned int ct[2],vp8_prob p)50 static INLINE unsigned int vp8_cost_branch(const unsigned int ct[2], vp8_prob p)
51 {
52     /* Imitate existing calculation */
53 
54     return ((ct[0] * vp8_cost_zero(p))
55             + (ct[1] * vp8_cost_one(p))) >> 8;
56 }
57 
58 /* Small functions to write explicit values and tokens, as well as
59    estimate their lengths. */
60 
vp8_treed_write(vp8_writer * const w,vp8_tree t,const vp8_prob * const p,int v,int n)61 static void vp8_treed_write
62 (
63     vp8_writer *const w,
64     vp8_tree t,
65     const vp8_prob *const p,
66     int v,
67     int n               /* number of bits in v, assumed nonzero */
68 )
69 {
70     vp8_tree_index i = 0;
71 
72     do
73     {
74         const int b = (v >> --n) & 1;
75         vp8_write(w, b, p[i>>1]);
76         i = t[i+b];
77     }
78     while (n);
79 }
vp8_write_token(vp8_writer * const w,vp8_tree t,const vp8_prob * const p,vp8_token * const x)80 static INLINE void vp8_write_token
81 (
82     vp8_writer *const w,
83     vp8_tree t,
84     const vp8_prob *const p,
85     vp8_token *const x
86 )
87 {
88     vp8_treed_write(w, t, p, x->value, x->Len);
89 }
90 
vp8_treed_cost(vp8_tree t,const vp8_prob * const p,int v,int n)91 static int vp8_treed_cost(
92     vp8_tree t,
93     const vp8_prob *const p,
94     int v,
95     int n               /* number of bits in v, assumed nonzero */
96 )
97 {
98     int c = 0;
99     vp8_tree_index i = 0;
100 
101     do
102     {
103         const int b = (v >> --n) & 1;
104         c += vp8_cost_bit(p[i>>1], b);
105         i = t[i+b];
106     }
107     while (n);
108 
109     return c;
110 }
vp8_cost_token(vp8_tree t,const vp8_prob * const p,vp8_token * const x)111 static INLINE int vp8_cost_token
112 (
113     vp8_tree t,
114     const vp8_prob *const p,
115     vp8_token *const x
116 )
117 {
118     return vp8_treed_cost(t, p, x->value, x->Len);
119 }
120 
121 /* Fill array of costs for all possible token values. */
122 
123 void vp8_cost_tokens(
124     int *Costs, const vp8_prob *, vp8_tree
125 );
126 
127 void vp8_cost_tokens2(
128     int *Costs, const vp8_prob *, vp8_tree, int
129 );
130 
131 #ifdef __cplusplus
132 }  // extern "C"
133 #endif
134 
135 #endif  // VP8_ENCODER_TREEWRITER_H_
136