/src/avm/av2/common/cost.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 3-Clause Clear License |
5 | | * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
6 | | * License was not distributed with this source code in the LICENSE file, you |
7 | | * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the |
8 | | * Alliance for Open Media Patent License 1.0 was not distributed with this |
9 | | * source code in the PATENTS file, you can obtain it at |
10 | | * aomedia.org/license/patent-license/. |
11 | | */ |
12 | | |
13 | | #ifndef AVM_AV2_COMMON_COST_H_ |
14 | | #define AVM_AV2_COMMON_COST_H_ |
15 | | |
16 | | #include "avm_dsp/prob.h" |
17 | | #include "avm/avm_integer.h" |
18 | | |
19 | | #ifdef __cplusplus |
20 | | extern "C" { |
21 | | #endif |
22 | | |
23 | | extern const uint16_t av2_prob_cost[128]; |
24 | | |
25 | | // The factor to scale from cost in bits to cost in av2_prob_cost units. |
26 | | #define AV2_PROB_COST_SHIFT 9 |
27 | | |
28 | | // Cost of coding an n bit literal, using 128 (i.e. 50%) probability |
29 | | // for each bit. |
30 | | #define av2_cost_literal(n) ((n) * (1 << AV2_PROB_COST_SHIFT)) |
31 | | |
32 | | // Calculate the cost of a symbol with probability p15 / 2^15 |
33 | 0 | static INLINE int av2_cost_symbol(avm_cdf_prob p15) { |
34 | 0 | // p15 can be out of range [1, CDF_PROB_TOP - 1]. Clamping it, so that the |
35 | 0 | // following cost calculation works correctly. Otherwise, if p15 = |
36 | 0 | // CDF_PROB_TOP, shift would be -1, and "p15 << shift" would be wrong. |
37 | 0 | p15 = (avm_cdf_prob)clamp(p15, 1, CDF_PROB_TOP - 1); |
38 | 0 | assert(0 < p15 && p15 < CDF_PROB_TOP); |
39 | 0 | const int shift = CDF_PROB_BITS - 1 - get_msb(p15); |
40 | 0 | const int prob = get_prob(p15 << shift, CDF_PROB_TOP); |
41 | 0 | assert(prob >= 128); |
42 | 0 | return av2_prob_cost[prob - 128] + av2_cost_literal(shift); |
43 | 0 | } |
44 | | |
45 | | void av2_cost_tokens_from_cdf(int *costs, const avm_cdf_prob *cdf, |
46 | | const int nsym, const int *inv_map); |
47 | | |
48 | | #ifdef __cplusplus |
49 | | } // extern "C" |
50 | | #endif |
51 | | |
52 | | #endif // AVM_AV2_COMMON_COST_H_ |