blob: e6063200577d82a27df25821c8a439d8ed59a9dd [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xubde4ac82016-11-28 15:26:06 -08002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xubde4ac82016-11-28 15:26:06 -08004 * 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 Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AV1_ENCODER_COST_H_
13#define AV1_ENCODER_COST_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include "aom_dsp/prob.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
18#ifdef __cplusplus
19extern "C" {
20#endif
21
Yaowu Xuf883b422016-08-30 14:01:10 -070022extern const uint16_t av1_prob_cost[256];
Yaowu Xuc27fc142016-08-22 16:08:15 -070023
Yaowu Xuf883b422016-08-30 14:01:10 -070024// The factor to scale from cost in bits to cost in av1_prob_cost units.
25#define AV1_PROB_COST_SHIFT 9
Yaowu Xuc27fc142016-08-22 16:08:15 -070026
Yaowu Xuf883b422016-08-30 14:01:10 -070027#define av1_cost_zero(prob) (av1_prob_cost[prob])
Yaowu Xuc27fc142016-08-22 16:08:15 -070028
Yaowu Xuf883b422016-08-30 14:01:10 -070029#define av1_cost_one(prob) av1_cost_zero(256 - (prob))
Yaowu Xuc27fc142016-08-22 16:08:15 -070030
Yaowu Xuf883b422016-08-30 14:01:10 -070031#define av1_cost_bit(prob, bit) av1_cost_zero((bit) ? 256 - (prob) : (prob))
Yaowu Xuc27fc142016-08-22 16:08:15 -070032
33// Cost of coding an n bit literal, using 128 (i.e. 50%) probability
34// for each bit.
Yaowu Xuf883b422016-08-30 14:01:10 -070035#define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT))
Yaowu Xuc27fc142016-08-22 16:08:15 -070036
Nathan E. Egge50f19112017-06-27 21:07:57 -040037// Calculate the cost of a symbol with probability p15 / 2^15
38static 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 Xuc27fc142016-08-22 16:08:15 -070045static INLINE unsigned int cost_branch256(const unsigned int ct[2],
Yaowu Xuf883b422016-08-30 14:01:10 -070046 aom_prob p) {
47 return ct[0] * av1_cost_zero(p) + ct[1] * av1_cost_one(p);
Yaowu Xuc27fc142016-08-22 16:08:15 -070048}
49
Yaowu Xuf883b422016-08-30 14:01:10 -070050static INLINE int treed_cost(aom_tree tree, const aom_prob *probs, int bits,
Yaowu Xuc27fc142016-08-22 16:08:15 -070051 int len) {
52 int cost = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070053 aom_tree_index i = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -070054
55 do {
56 const int bit = (bits >> --len) & 1;
Yaowu Xuf883b422016-08-30 14:01:10 -070057 cost += av1_cost_bit(probs[i >> 1], bit);
Yaowu Xuc27fc142016-08-22 16:08:15 -070058 i = tree[i + bit];
59 } while (len);
60
61 return cost;
62}
63
Yaowu Xuf883b422016-08-30 14:01:10 -070064void av1_cost_tokens(int *costs, const aom_prob *probs, aom_tree tree);
65void av1_cost_tokens_skip(int *costs, const aom_prob *probs, aom_tree tree);
Nathan E. Egge50f19112017-06-27 21:07:57 -040066void av1_cost_tokens_from_cdf(int *costs, const aom_cdf_prob *cdf,
67 const int *inv_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -070068
Yaowu Xuc27fc142016-08-22 16:08:15 -070069#ifdef __cplusplus
70} // extern "C"
71#endif
72
Yaowu Xuf883b422016-08-30 14:01:10 -070073#endif // AV1_ENCODER_COST_H_