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 AOM_AV1_ENCODER_COST_H_
13 #define AOM_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[128];
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 // Cost of coding an n bit literal, using 128 (i.e. 50%) probability
28 // for each bit.
29 #define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT))
30
31 // Calculate the cost of a symbol with probability p15 / 2^15
av1_cost_symbol(aom_cdf_prob p15)32 static INLINE int av1_cost_symbol(aom_cdf_prob p15) {
33 // p15 can be out of range [1, CDF_PROB_TOP - 1]. Clamping it, so that the
34 // following cost calculation works correctly. Otherwise, if p15 =
35 // CDF_PROB_TOP, shift would be -1, and "p15 << shift" would be wrong.
36 p15 = (aom_cdf_prob)clamp(p15, 1, CDF_PROB_TOP - 1);
37 assert(0 < p15 && p15 < CDF_PROB_TOP);
38 const int shift = CDF_PROB_BITS - 1 - get_msb(p15);
39 const int prob = get_prob(p15 << shift, CDF_PROB_TOP);
40 assert(prob >= 128);
41 return av1_prob_cost[prob - 128] + av1_cost_literal(shift);
42 }
43
44 void av1_cost_tokens_from_cdf(int *costs, const aom_cdf_prob *cdf,
45 const int *inv_map);
46
47 #ifdef __cplusplus
48 } // extern "C"
49 #endif
50
51 #endif // AOM_AV1_ENCODER_COST_H_
52