michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #ifndef VP9_ENCODER_VP9_TREEWRITER_H_ michael@0: #define VP9_ENCODER_VP9_TREEWRITER_H_ michael@0: michael@0: /* Trees map alphabets into huffman-like codes suitable for an arithmetic michael@0: bit coder. Timothy S Murphy 11 October 2004 */ michael@0: michael@0: #include "vp9/common/vp9_treecoder.h" michael@0: michael@0: #include "vp9/encoder/vp9_boolhuff.h" /* for now */ michael@0: michael@0: michael@0: #define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8) michael@0: michael@0: /* Approximate length of an encoded bool in 256ths of a bit at given prob */ michael@0: michael@0: #define vp9_cost_zero(x) (vp9_prob_cost[x]) michael@0: #define vp9_cost_one(x) vp9_cost_zero(vp9_complement(x)) michael@0: michael@0: #define vp9_cost_bit(x, b) vp9_cost_zero((b) ? vp9_complement(x) : (x)) michael@0: michael@0: /* VP8BC version is scaled by 2^20 rather than 2^8; see bool_coder.h */ michael@0: michael@0: michael@0: /* Both of these return bits, not scaled bits. */ michael@0: static INLINE unsigned int cost_branch256(const unsigned int ct[2], michael@0: vp9_prob p) { michael@0: return ct[0] * vp9_cost_zero(p) + ct[1] * vp9_cost_one(p); michael@0: } michael@0: michael@0: static INLINE unsigned int cost_branch(const unsigned int ct[2], michael@0: vp9_prob p) { michael@0: return cost_branch256(ct, p) >> 8; michael@0: } michael@0: michael@0: michael@0: static INLINE void treed_write(vp9_writer *w, michael@0: vp9_tree tree, const vp9_prob *probs, michael@0: int bits, int len) { michael@0: vp9_tree_index i = 0; michael@0: michael@0: do { michael@0: const int bit = (bits >> --len) & 1; michael@0: vp9_write(w, bit, probs[i >> 1]); michael@0: i = tree[i + bit]; michael@0: } while (len); michael@0: } michael@0: michael@0: static INLINE void write_token(vp9_writer *w, vp9_tree tree, michael@0: const vp9_prob *probs, michael@0: const struct vp9_token *token) { michael@0: treed_write(w, tree, probs, token->value, token->len); michael@0: } michael@0: michael@0: static INLINE int treed_cost(vp9_tree tree, const vp9_prob *probs, michael@0: int bits, int len) { michael@0: int cost = 0; michael@0: vp9_tree_index i = 0; michael@0: michael@0: do { michael@0: const int bit = (bits >> --len) & 1; michael@0: cost += vp9_cost_bit(probs[i >> 1], bit); michael@0: i = tree[i + bit]; michael@0: } while (len); michael@0: michael@0: return cost; michael@0: } michael@0: michael@0: static INLINE int cost_token(vp9_tree tree, const vp9_prob *probs, michael@0: const struct vp9_token *token) { michael@0: return treed_cost(tree, probs, token->value, token->len); michael@0: } michael@0: michael@0: void vp9_cost_tokens(int *costs, const vp9_prob *probs, vp9_tree tree); michael@0: void vp9_cost_tokens_skip(int *costs, const vp9_prob *probs, vp9_tree tree); michael@0: michael@0: #endif // VP9_ENCODER_VP9_TREEWRITER_H_