blob: eeda5cda7961075bbf480189507563b28340ccc8 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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 Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
Jim Bankoski2b8dc062012-11-29 16:36:10 -080012#ifndef VP9_ENCODER_VP9_TREEWRITER_H_
13#define VP9_ENCODER_VP9_TREEWRITER_H_
John Koleszar0ea50ce2010-05-18 11:58:33 -040014
15/* Trees map alphabets into huffman-like codes suitable for an arithmetic
16 bit coder. Timothy S Murphy 11 October 2004 */
17
John Koleszarfcccbcb2012-11-27 13:59:17 -080018#include "vp9/common/vp9_treecoder.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040019
Jim Bankoskic6787392012-11-28 10:41:40 -080020#include "vp9/encoder/vp9_boolhuff.h" /* for now */
John Koleszar0ea50ce2010-05-18 11:58:33 -040021
Dmitry Kovalevcd5113c2013-05-07 18:19:50 -070022
Dmitry Kovalevef4d9a42013-04-16 16:23:17 -070023#define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8)
John Koleszar0ea50ce2010-05-18 11:58:33 -040024
25/* Approximate length of an encoded bool in 256ths of a bit at given prob */
26
Ronald S. Bultje6a4b1e52012-10-31 14:40:53 -070027#define vp9_cost_zero(x) (vp9_prob_cost[x])
28#define vp9_cost_one(x) vp9_cost_zero(vp9_complement(x))
John Koleszar0ea50ce2010-05-18 11:58:33 -040029
Ronald S. Bultje6a4b1e52012-10-31 14:40:53 -070030#define vp9_cost_bit(x, b) vp9_cost_zero((b) ? vp9_complement(x) : (x))
John Koleszar0ea50ce2010-05-18 11:58:33 -040031
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 Kovalev548b4dd2013-02-22 11:03:14 -080036static INLINE unsigned int cost_branch256(const unsigned int ct[2],
37 vp9_prob p) {
Dmitry Kovalev548b4dd2013-02-22 11:03:14 -080038 return ct[0] * vp9_cost_zero(p) + ct[1] * vp9_cost_one(p);
39}
John Koleszar0ea50ce2010-05-18 11:58:33 -040040
Ronald S. Bultjeaac73df2013-02-06 12:45:28 -080041static INLINE unsigned int cost_branch(const unsigned int ct[2],
42 vp9_prob p) {
Dmitry Kovalev548b4dd2013-02-22 11:03:14 -080043 return cost_branch256(ct, p) >> 8;
John Koleszar0ea50ce2010-05-18 11:58:33 -040044}
45
Deb Mukherjeec6f1bf42012-04-12 09:24:03 -070046
Dmitry Kovalevaea29cd2013-04-30 16:39:07 -070047static INLINE void treed_write(vp9_writer *w,
48 vp9_tree tree, const vp9_prob *probs,
49 int bits, int len) {
Ronald S. Bultje6a4b1e52012-10-31 14:40:53 -070050 vp9_tree_index i = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -040051
John Koleszarc6b90392012-07-13 15:21:29 -070052 do {
Dmitry Kovalevaea29cd2013-04-30 16:39:07 -070053 const int bit = (bits >> --len) & 1;
54 vp9_write(w, bit, probs[i >> 1]);
55 i = tree[i + bit];
56 } while (len);
John Koleszar0ea50ce2010-05-18 11:58:33 -040057}
Ronald S. Bultje6a4b1e52012-10-31 14:40:53 -070058
Dmitry Kovalevaea29cd2013-04-30 16:39:07 -070059static 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 Koleszar0ea50ce2010-05-18 11:58:33 -040063}
64
Dmitry Kovalevaea29cd2013-04-30 16:39:07 -070065static INLINE int treed_cost(vp9_tree tree, const vp9_prob *probs,
66 int bits, int len) {
67 int cost = 0;
Ronald S. Bultje6a4b1e52012-10-31 14:40:53 -070068 vp9_tree_index i = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -040069
John Koleszarc6b90392012-07-13 15:21:29 -070070 do {
Dmitry Kovalevaea29cd2013-04-30 16:39:07 -070071 const int bit = (bits >> --len) & 1;
72 cost += vp9_cost_bit(probs[i >> 1], bit);
73 i = tree[i + bit];
74 } while (len);
John Koleszar0ea50ce2010-05-18 11:58:33 -040075
Dmitry Kovalevaea29cd2013-04-30 16:39:07 -070076 return cost;
John Koleszar0ea50ce2010-05-18 11:58:33 -040077}
Ronald S. Bultje6a4b1e52012-10-31 14:40:53 -070078
Dmitry Kovalevaea29cd2013-04-30 16:39:07 -070079static 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 Koleszar0ea50ce2010-05-18 11:58:33 -040082}
83
Dmitry Kovalevaea29cd2013-04-30 16:39:07 -070084void vp9_cost_tokens(int *costs, const vp9_prob *probs, vp9_tree tree);
85void vp9_cost_tokens_skip(int *costs, const vp9_prob *probs, vp9_tree tree);
Christian Duviviere0a338c2012-06-14 18:14:43 -070086
Ronald S. Bultje4cca47b2012-12-18 15:31:19 -080087#endif // VP9_ENCODER_VP9_TREEWRITER_H_