cost.h 2.26 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11
 */

12 13
#ifndef AV1_ENCODER_COST_H_
#define AV1_ENCODER_COST_H_
Jingning Han's avatar
Jingning Han committed
14

15
#include "aom_dsp/prob.h"
16
#include "aom/aom_integer.h"
Jingning Han's avatar
Jingning Han committed
17 18 19 20 21

#ifdef __cplusplus
extern "C" {
#endif

22
extern const uint16_t av1_prob_cost[256];
23

24 25
// The factor to scale from cost in bits to cost in av1_prob_cost units.
#define AV1_PROB_COST_SHIFT 9
Jingning Han's avatar
Jingning Han committed
26

27
#define av1_cost_zero(prob) (av1_prob_cost[prob])
Jingning Han's avatar
Jingning Han committed
28

29
#define av1_cost_one(prob) av1_cost_zero(256 - (prob))
Jingning Han's avatar
Jingning Han committed
30

31
#define av1_cost_bit(prob, bit) av1_cost_zero((bit) ? 256 - (prob) : (prob))
Jingning Han's avatar
Jingning Han committed
32

33 34
// Cost of coding an n bit literal, using 128 (i.e. 50%) probability
// for each bit.
35
#define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT))
36

37 38 39 40 41 42 43 44
// Calculate the cost of a symbol with probability p15 / 2^15
static INLINE int av1_cost_symbol(aom_cdf_prob p15) {
  assert(0 < p15 && p15 < CDF_PROB_TOP);
  const int shift = CDF_PROB_BITS - 1 - get_msb(p15);
  return av1_cost_zero(get_prob(p15 << shift, CDF_PROB_TOP)) +
         av1_cost_literal(shift);
}

Jingning Han's avatar
Jingning Han committed
45
static INLINE unsigned int cost_branch256(const unsigned int ct[2],
46 47
                                          aom_prob p) {
  return ct[0] * av1_cost_zero(p) + ct[1] * av1_cost_one(p);
Jingning Han's avatar
Jingning Han committed
48 49
}

50
static INLINE int treed_cost(aom_tree tree, const aom_prob *probs, int bits,
51
                             int len) {
Jingning Han's avatar
Jingning Han committed
52
  int cost = 0;
53
  aom_tree_index i = 0;
Jingning Han's avatar
Jingning Han committed
54 55 56

  do {
    const int bit = (bits >> --len) & 1;
57
    cost += av1_cost_bit(probs[i >> 1], bit);
Jingning Han's avatar
Jingning Han committed
58 59 60 61 62 63
    i = tree[i + bit];
  } while (len);

  return cost;
}

64 65
void av1_cost_tokens(int *costs, const aom_prob *probs, aom_tree tree);
void av1_cost_tokens_skip(int *costs, const aom_prob *probs, aom_tree tree);
66 67
void av1_cost_tokens_from_cdf(int *costs, const aom_cdf_prob *cdf,
                              const int *inv_map);
Jingning Han's avatar
Jingning Han committed
68 69 70 71 72

#ifdef __cplusplus
}  // extern "C"
#endif

73
#endif  // AV1_ENCODER_COST_H_