John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 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 |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
Jim Bankoski | 2b8dc06 | 2012-11-29 16:36:10 -0800 | [diff] [blame] | 12 | #ifndef VP9_ENCODER_VP9_TREEWRITER_H_ |
| 13 | #define VP9_ENCODER_VP9_TREEWRITER_H_ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 14 | |
| 15 | /* Trees map alphabets into huffman-like codes suitable for an arithmetic |
| 16 | bit coder. Timothy S Murphy 11 October 2004 */ |
| 17 | |
John Koleszar | fcccbcb | 2012-11-27 13:59:17 -0800 | [diff] [blame] | 18 | #include "vp9/common/vp9_treecoder.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 19 | |
Jim Bankoski | c678739 | 2012-11-28 10:41:40 -0800 | [diff] [blame] | 20 | #include "vp9/encoder/vp9_boolhuff.h" /* for now */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 21 | |
Dmitry Kovalev | cd5113c | 2013-05-07 18:19:50 -0700 | [diff] [blame] | 22 | |
Dmitry Kovalev | ef4d9a4 | 2013-04-16 16:23:17 -0700 | [diff] [blame] | 23 | #define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 24 | |
| 25 | /* Approximate length of an encoded bool in 256ths of a bit at given prob */ |
| 26 | |
Ronald S. Bultje | 6a4b1e5 | 2012-10-31 14:40:53 -0700 | [diff] [blame] | 27 | #define vp9_cost_zero(x) (vp9_prob_cost[x]) |
| 28 | #define vp9_cost_one(x) vp9_cost_zero(vp9_complement(x)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 29 | |
Ronald S. Bultje | 6a4b1e5 | 2012-10-31 14:40:53 -0700 | [diff] [blame] | 30 | #define vp9_cost_bit(x, b) vp9_cost_zero((b) ? vp9_complement(x) : (x)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 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. */ |
Dmitry Kovalev | 548b4dd | 2013-02-22 11:03:14 -0800 | [diff] [blame] | 36 | static INLINE unsigned int cost_branch256(const unsigned int ct[2], |
| 37 | vp9_prob p) { |
Dmitry Kovalev | 548b4dd | 2013-02-22 11:03:14 -0800 | [diff] [blame] | 38 | return ct[0] * vp9_cost_zero(p) + ct[1] * vp9_cost_one(p); |
| 39 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 40 | |
Ronald S. Bultje | aac73df | 2013-02-06 12:45:28 -0800 | [diff] [blame] | 41 | static INLINE unsigned int cost_branch(const unsigned int ct[2], |
| 42 | vp9_prob p) { |
Dmitry Kovalev | 548b4dd | 2013-02-22 11:03:14 -0800 | [diff] [blame] | 43 | return cost_branch256(ct, p) >> 8; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Deb Mukherjee | c6f1bf4 | 2012-04-12 09:24:03 -0700 | [diff] [blame] | 46 | |
Dmitry Kovalev | aea29cd | 2013-04-30 16:39:07 -0700 | [diff] [blame] | 47 | static INLINE void treed_write(vp9_writer *w, |
| 48 | vp9_tree tree, const vp9_prob *probs, |
| 49 | int bits, int len) { |
Ronald S. Bultje | 6a4b1e5 | 2012-10-31 14:40:53 -0700 | [diff] [blame] | 50 | vp9_tree_index i = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 51 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 52 | do { |
Dmitry Kovalev | aea29cd | 2013-04-30 16:39:07 -0700 | [diff] [blame] | 53 | const int bit = (bits >> --len) & 1; |
| 54 | vp9_write(w, bit, probs[i >> 1]); |
| 55 | i = tree[i + bit]; |
| 56 | } while (len); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 57 | } |
Ronald S. Bultje | 6a4b1e5 | 2012-10-31 14:40:53 -0700 | [diff] [blame] | 58 | |
Dmitry Kovalev | aea29cd | 2013-04-30 16:39:07 -0700 | [diff] [blame] | 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); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Dmitry Kovalev | aea29cd | 2013-04-30 16:39:07 -0700 | [diff] [blame] | 65 | static INLINE int treed_cost(vp9_tree tree, const vp9_prob *probs, |
| 66 | int bits, int len) { |
| 67 | int cost = 0; |
Ronald S. Bultje | 6a4b1e5 | 2012-10-31 14:40:53 -0700 | [diff] [blame] | 68 | vp9_tree_index i = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 69 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 70 | do { |
Dmitry Kovalev | aea29cd | 2013-04-30 16:39:07 -0700 | [diff] [blame] | 71 | const int bit = (bits >> --len) & 1; |
| 72 | cost += vp9_cost_bit(probs[i >> 1], bit); |
| 73 | i = tree[i + bit]; |
| 74 | } while (len); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 75 | |
Dmitry Kovalev | aea29cd | 2013-04-30 16:39:07 -0700 | [diff] [blame] | 76 | return cost; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 77 | } |
Ronald S. Bultje | 6a4b1e5 | 2012-10-31 14:40:53 -0700 | [diff] [blame] | 78 | |
Dmitry Kovalev | aea29cd | 2013-04-30 16:39:07 -0700 | [diff] [blame] | 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); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Dmitry Kovalev | aea29cd | 2013-04-30 16:39:07 -0700 | [diff] [blame] | 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); |
Christian Duvivier | e0a338c | 2012-06-14 18:14:43 -0700 | [diff] [blame] | 86 | |
Ronald S. Bultje | 4cca47b | 2012-12-18 15:31:19 -0800 | [diff] [blame] | 87 | #endif // VP9_ENCODER_VP9_TREEWRITER_H_ |