Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | #ifndef AV1_ENCODER_COST_H_ |
| 13 | #define AV1_ENCODER_COST_H_ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
| 15 | #include "aom_dsp/prob.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 16 | #include "aom/aom_integer.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 22 | extern const uint16_t av1_prob_cost[256]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 23 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 24 | // The factor to scale from cost in bits to cost in av1_prob_cost units. |
| 25 | #define AV1_PROB_COST_SHIFT 9 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 26 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 27 | #define av1_cost_zero(prob) (av1_prob_cost[prob]) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 28 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 29 | #define av1_cost_one(prob) av1_cost_zero(256 - (prob)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 30 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 31 | #define av1_cost_bit(prob, bit) av1_cost_zero((bit) ? 256 - (prob) : (prob)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 32 | |
| 33 | // Cost of coding an n bit literal, using 128 (i.e. 50%) probability |
| 34 | // for each bit. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 35 | #define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 36 | |
Nathan E. Egge | 50f1911 | 2017-06-27 21:07:57 -0400 | [diff] [blame] | 37 | // Calculate the cost of a symbol with probability p15 / 2^15 |
| 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 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 45 | static INLINE unsigned int cost_branch256(const unsigned int ct[2], |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 46 | aom_prob p) { |
| 47 | return ct[0] * av1_cost_zero(p) + ct[1] * av1_cost_one(p); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 50 | static INLINE int treed_cost(aom_tree tree, const aom_prob *probs, int bits, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 51 | int len) { |
| 52 | int cost = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 53 | aom_tree_index i = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 54 | |
| 55 | do { |
| 56 | const int bit = (bits >> --len) & 1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 57 | cost += av1_cost_bit(probs[i >> 1], bit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 58 | i = tree[i + bit]; |
| 59 | } while (len); |
| 60 | |
| 61 | return cost; |
| 62 | } |
| 63 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 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); |
Nathan E. Egge | 50f1911 | 2017-06-27 21:07:57 -0400 | [diff] [blame] | 66 | void av1_cost_tokens_from_cdf(int *costs, const aom_cdf_prob *cdf, |
| 67 | const int *inv_map); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 68 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 69 | #ifdef __cplusplus |
| 70 | } // extern "C" |
| 71 | #endif |
| 72 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 73 | #endif // AV1_ENCODER_COST_H_ |