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 #ifndef VPX_VP8_ENCODER_TREEWRITER_H_
12 #define VPX_VP8_ENCODER_TREEWRITER_H_
13
14 /* Trees map alphabets into huffman-like codes suitable for an arithmetic
15 bit coder. Timothy S Murphy 11 October 2004 */
16
17 #include <stdint.h>
18
19 #include "./vpx_config.h"
20 #include "vp8/common/treecoder.h"
21
22 #include "boolhuff.h" /* for now */
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 typedef BOOL_CODER vp8_writer;
29
30 #define vp8_write vp8_encode_bool
31 #define vp8_write_literal vp8_encode_value
32 #define vp8_write_bit(W, V) vp8_write(W, V, vp8_prob_half)
33
34 #define vp8bc_write vp8bc_write_bool
35 #define vp8bc_write_literal vp8bc_write_bits
36 #define vp8bc_write_bit(W, V) vp8bc_write_bits(W, V, 1)
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 /* Both of these return bits, not scaled bits. */
48
vp8_cost_branch(const unsigned int ct[2],vp8_prob p)49 static INLINE unsigned int vp8_cost_branch(const unsigned int ct[2],
50 vp8_prob p) {
51 /* Imitate existing calculation */
52
53 return (unsigned int)(((((uint64_t)ct[0]) * vp8_cost_zero(p)) +
54 (((uint64_t)ct[1]) * vp8_cost_one(p))) >>
55 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(vp8_writer *const w, vp8_tree t,
62 const vp8_prob *const p, int v,
63 int n) { /* number of bits in v, assumed nonzero */
64 vp8_tree_index i = 0;
65
66 do {
67 const int b = (v >> --n) & 1;
68 vp8_write(w, b, p[i >> 1]);
69 i = t[i + b];
70 } while (n);
71 }
vp8_write_token(vp8_writer * const w,vp8_tree t,const vp8_prob * const p,vp8_token * const x)72 static INLINE void vp8_write_token(vp8_writer *const w, vp8_tree t,
73 const vp8_prob *const p,
74 vp8_token *const x) {
75 vp8_treed_write(w, t, p, x->value, x->Len);
76 }
77
vp8_treed_cost(vp8_tree t,const vp8_prob * const p,int v,int n)78 static int vp8_treed_cost(vp8_tree t, const vp8_prob *const p, int v,
79 int n) { /* number of bits in v, assumed nonzero */
80 int c = 0;
81 vp8_tree_index i = 0;
82
83 do {
84 const int b = (v >> --n) & 1;
85 c += vp8_cost_bit(p[i >> 1], b);
86 i = t[i + b];
87 } while (n);
88
89 return c;
90 }
vp8_cost_token(vp8_tree t,const vp8_prob * const p,vp8_token * const x)91 static INLINE int vp8_cost_token(vp8_tree t, const vp8_prob *const p,
92 vp8_token *const x) {
93 return vp8_treed_cost(t, p, x->value, x->Len);
94 }
95
96 /* Fill array of costs for all possible token values. */
97
98 void vp8_cost_tokens(int *c, const vp8_prob *, vp8_tree);
99
100 void vp8_cost_tokens2(int *c, const vp8_prob *, vp8_tree, int);
101
102 #ifdef __cplusplus
103 } // extern "C"
104 #endif
105
106 #endif // VPX_VP8_ENCODER_TREEWRITER_H_
107