xref: /aosp_15_r20/external/libaom/av1/encoder/cost.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_ENCODER_COST_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_COST_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/prob.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
19*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
20*77c1e3ccSAndroid Build Coastguard Worker #endif
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker extern const uint16_t av1_prob_cost[128];
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker // The factor to scale from cost in bits to cost in av1_prob_cost units.
25*77c1e3ccSAndroid Build Coastguard Worker #define AV1_PROB_COST_SHIFT 9
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker // Cost of coding an n bit literal, using 128 (i.e. 50%) probability
28*77c1e3ccSAndroid Build Coastguard Worker // for each bit.
29*77c1e3ccSAndroid Build Coastguard Worker #define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT))
30*77c1e3ccSAndroid Build Coastguard Worker 
31*77c1e3ccSAndroid Build Coastguard Worker // Calculate the cost of a symbol with probability p15 / 2^15
av1_cost_symbol(aom_cdf_prob p15)32*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_cost_symbol(aom_cdf_prob p15) {
33*77c1e3ccSAndroid Build Coastguard Worker   // p15 can be out of range [1, CDF_PROB_TOP - 1]. Clamping it, so that the
34*77c1e3ccSAndroid Build Coastguard Worker   // following cost calculation works correctly. Otherwise, if p15 =
35*77c1e3ccSAndroid Build Coastguard Worker   // CDF_PROB_TOP, shift would be -1, and "p15 << shift" would be wrong.
36*77c1e3ccSAndroid Build Coastguard Worker   p15 = (aom_cdf_prob)clamp(p15, 1, CDF_PROB_TOP - 1);
37*77c1e3ccSAndroid Build Coastguard Worker   assert(0 < p15 && p15 < CDF_PROB_TOP);
38*77c1e3ccSAndroid Build Coastguard Worker   const int shift = CDF_PROB_BITS - 1 - get_msb(p15);
39*77c1e3ccSAndroid Build Coastguard Worker   const int prob = get_prob(p15 << shift, CDF_PROB_TOP);
40*77c1e3ccSAndroid Build Coastguard Worker   assert(prob >= 128);
41*77c1e3ccSAndroid Build Coastguard Worker   return av1_prob_cost[prob - 128] + av1_cost_literal(shift);
42*77c1e3ccSAndroid Build Coastguard Worker }
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker void av1_cost_tokens_from_cdf(int *costs, const aom_cdf_prob *cdf,
45*77c1e3ccSAndroid Build Coastguard Worker                               const int *inv_map);
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
48*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
49*77c1e3ccSAndroid Build Coastguard Worker #endif
50*77c1e3ccSAndroid Build Coastguard Worker 
51*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_ENCODER_COST_H_
52