Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Krishna Rapaka | 7319db5 | 2021-09-28 20:35:29 -0700 | [diff] [blame] | 2 | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Vibhoothi | 41c6dd7 | 2021-10-12 18:48:26 +0000 | [diff] [blame] | 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/. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 13 | #ifndef AOM_AV1_ENCODER_COST_H_ |
| 14 | #define AOM_AV1_ENCODER_COST_H_ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 15 | |
| 16 | #include "aom_dsp/prob.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 17 | #include "aom/aom_integer.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 18 | |
| 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
Hui Su | c1cd519 | 2018-01-24 11:05:06 -0800 | [diff] [blame] | 23 | extern const uint16_t av1_prob_cost[128]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 24 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 25 | // The factor to scale from cost in bits to cost in av1_prob_cost units. |
| 26 | #define AV1_PROB_COST_SHIFT 9 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 27 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 28 | // Cost of coding an n bit literal, using 128 (i.e. 50%) probability |
| 29 | // for each bit. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 30 | #define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 31 | |
Nathan E. Egge | 50f1911 | 2017-06-27 21:07:57 -0400 | [diff] [blame] | 32 | // Calculate the cost of a symbol with probability p15 / 2^15 |
| 33 | static INLINE int av1_cost_symbol(aom_cdf_prob p15) { |
Yunqing Wang | 85923a6 | 2019-01-18 18:48:42 -0800 | [diff] [blame] | 34 | // p15 can be out of range [1, CDF_PROB_TOP - 1]. Clamping it, so that the |
| 35 | // following cost calculation works correctly. Otherwise, if p15 = |
| 36 | // CDF_PROB_TOP, shift would be -1, and "p15 << shift" would be wrong. |
| 37 | p15 = (aom_cdf_prob)clamp(p15, 1, CDF_PROB_TOP - 1); |
Nathan E. Egge | 50f1911 | 2017-06-27 21:07:57 -0400 | [diff] [blame] | 38 | assert(0 < p15 && p15 < CDF_PROB_TOP); |
| 39 | const int shift = CDF_PROB_BITS - 1 - get_msb(p15); |
Hui Su | c1cd519 | 2018-01-24 11:05:06 -0800 | [diff] [blame] | 40 | const int prob = get_prob(p15 << shift, CDF_PROB_TOP); |
| 41 | assert(prob >= 128); |
| 42 | return av1_prob_cost[prob - 128] + av1_cost_literal(shift); |
Nathan E. Egge | 50f1911 | 2017-06-27 21:07:57 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Nathan E. Egge | 50f1911 | 2017-06-27 21:07:57 -0400 | [diff] [blame] | 45 | void av1_cost_tokens_from_cdf(int *costs, const aom_cdf_prob *cdf, |
| 46 | const int *inv_map); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 47 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 48 | #ifdef __cplusplus |
| 49 | } // extern "C" |
| 50 | #endif |
| 51 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 52 | #endif // AOM_AV1_ENCODER_COST_H_ |