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 VP9_ENCODER_VP9_TREEWRITER_H_ 13 #define VP9_ENCODER_VP9_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 "vp9/common/vp9_treecoder.h" 19 20 #include "vp9/encoder/vp9_boolhuff.h" /* for now */ 21 22 23 #define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8) 24 25 /* Approximate length of an encoded bool in 256ths of a bit at given prob */ 26 27 #define vp9_cost_zero(x) (vp9_prob_cost[x]) 28 #define vp9_cost_one(x) vp9_cost_zero(vp9_complement(x)) 29 30 #define vp9_cost_bit(x, b) vp9_cost_zero((b) ? vp9_complement(x) : (x)) 31 32 /* VP8BC version is scaled by 2^20 rather than 2^8; see bool_coder.h */ 33 34 35 /* Both of these return bits, not scaled bits. */ 36 static INLINE unsigned int cost_branch256(const unsigned int ct[2], 37 vp9_prob p) { 38 return ct[0] * vp9_cost_zero(p) + ct[1] * vp9_cost_one(p); 39 } 40 41 static INLINE unsigned int cost_branch(const unsigned int ct[2], 42 vp9_prob p) { 43 return cost_branch256(ct, p) >> 8; 44 } 45 46 47 static INLINE void treed_write(vp9_writer *w, 48 vp9_tree tree, const vp9_prob *probs, 49 int bits, int len) { 50 vp9_tree_index i = 0; 51 52 do { 53 const int bit = (bits >> --len) & 1; 54 vp9_write(w, bit, probs[i >> 1]); 55 i = tree[i + bit]; 56 } while (len); 57 } 58 59 static INLINE void write_token(vp9_writer *w, vp9_tree tree, 60 const vp9_prob *probs, 61 const struct vp9_token *token) { 62 treed_write(w, tree, probs, token->value, token->len); 63 } 64 65 static INLINE int treed_cost(vp9_tree tree, const vp9_prob *probs, 66 int bits, int len) { 67 int cost = 0; 68 vp9_tree_index i = 0; 69 70 do { 71 const int bit = (bits >> --len) & 1; 72 cost += vp9_cost_bit(probs[i >> 1], bit); 73 i = tree[i + bit]; 74 } while (len); 75 76 return cost; 77 } 78 79 static INLINE int cost_token(vp9_tree tree, const vp9_prob *probs, 80 const struct vp9_token *token) { 81 return treed_cost(tree, probs, token->value, token->len); 82 } 83 84 void vp9_cost_tokens(int *costs, const vp9_prob *probs, vp9_tree tree); 85 void vp9_cost_tokens_skip(int *costs, const vp9_prob *probs, vp9_tree tree); 86 87 #endif // VP9_ENCODER_VP9_TREEWRITER_H_ 88