1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #ifndef AV1_ENCODER_COST_H_
13 #define AV1_ENCODER_COST_H_
14 
15 #include "aom_dsp/prob.h"
16 #include "aom/aom_integer.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 extern const uint16_t av1_prob_cost[256];
23 
24 // The factor to scale from cost in bits to cost in av1_prob_cost units.
25 #define AV1_PROB_COST_SHIFT 9
26 
27 #define av1_cost_zero(prob) (av1_prob_cost[prob])
28 
29 #define av1_cost_one(prob) av1_cost_zero(256 - (prob))
30 
31 #define av1_cost_bit(prob, bit) av1_cost_zero((bit) ? 256 - (prob) : (prob))
32 
33 // Cost of coding an n bit literal, using 128 (i.e. 50%) probability
34 // for each bit.
35 #define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT))
36 
37 // Calculate the cost of a symbol with probability p15 / 2^15
av1_cost_symbol(aom_cdf_prob p15)38 static INLINE int av1_cost_symbol(aom_cdf_prob p15) {
39   assert(0 < p15 && p15 < CDF_PROB_TOP);
40   const int shift = CDF_PROB_BITS - 1 - get_msb(p15);
41   return av1_cost_zero(get_prob(p15 << shift, CDF_PROB_TOP)) +
42          av1_cost_literal(shift);
43 }
44 
cost_branch256(const unsigned int ct[2],aom_prob p)45 static INLINE unsigned int cost_branch256(const unsigned int ct[2],
46                                           aom_prob p) {
47   return ct[0] * av1_cost_zero(p) + ct[1] * av1_cost_one(p);
48 }
49 
treed_cost(aom_tree tree,const aom_prob * probs,int bits,int len)50 static INLINE int treed_cost(aom_tree tree, const aom_prob *probs, int bits,
51                              int len) {
52   int cost = 0;
53   aom_tree_index i = 0;
54 
55   do {
56     const int bit = (bits >> --len) & 1;
57     cost += av1_cost_bit(probs[i >> 1], bit);
58     i = tree[i + bit];
59   } while (len);
60 
61   return cost;
62 }
63 
64 void av1_cost_tokens(int *costs, const aom_prob *probs, aom_tree tree);
65 void av1_cost_tokens_skip(int *costs, const aom_prob *probs, aom_tree tree);
66 void av1_cost_tokens_from_cdf(int *costs, const aom_cdf_prob *cdf,
67                               const int *inv_map);
68 
69 #ifdef __cplusplus
70 }  // extern "C"
71 #endif
72 
73 #endif  // AV1_ENCODER_COST_H_
74