cost.h 1.94 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
2
3
4
5
6
7
8
9
10
/*
 *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

Yaowu Xu's avatar
Yaowu Xu committed
11
12
#ifndef AV1_ENCODER_COST_H_
#define AV1_ENCODER_COST_H_
Jingning Han's avatar
Jingning Han committed
13

14
#include "aom_dsp/prob.h"
Yaowu Xu's avatar
Yaowu Xu committed
15
#include "aom/aom_integer.h"
16
#if CONFIG_ANS
Alex Converse's avatar
Alex Converse committed
17
#include "aom_dsp/ans.h"
18
#endif  // CONFIG_ANS
Jingning Han's avatar
Jingning Han committed
19
20
21
22
23

#ifdef __cplusplus
extern "C" {
#endif

Yaowu Xu's avatar
Yaowu Xu committed
24
extern const uint16_t av1_prob_cost[256];
25

Yaowu Xu's avatar
Yaowu Xu committed
26
27
// 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
28

Yaowu Xu's avatar
Yaowu Xu committed
29
#define av1_cost_zero(prob) (av1_prob_cost[prob])
Jingning Han's avatar
Jingning Han committed
30

Yaowu Xu's avatar
Yaowu Xu committed
31
#define av1_cost_one(prob) av1_cost_zero(256 - (prob))
Jingning Han's avatar
Jingning Han committed
32

Yaowu Xu's avatar
Yaowu Xu committed
33
#define av1_cost_bit(prob, bit) av1_cost_zero((bit) ? 256 - (prob) : (prob))
Jingning Han's avatar
Jingning Han committed
34

35
36
// Cost of coding an n bit literal, using 128 (i.e. 50%) probability
// for each bit.
Yaowu Xu's avatar
Yaowu Xu committed
37
#define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT))
38

Jingning Han's avatar
Jingning Han committed
39
static INLINE unsigned int cost_branch256(const unsigned int ct[2],
Yaowu Xu's avatar
Yaowu Xu committed
40
41
                                          aom_prob p) {
  return ct[0] * av1_cost_zero(p) + ct[1] * av1_cost_one(p);
Jingning Han's avatar
Jingning Han committed
42
43
}

Yaowu Xu's avatar
Yaowu Xu committed
44
static INLINE int treed_cost(aom_tree tree, const aom_prob *probs, int bits,
45
                             int len) {
Jingning Han's avatar
Jingning Han committed
46
  int cost = 0;
Yaowu Xu's avatar
Yaowu Xu committed
47
  aom_tree_index i = 0;
Jingning Han's avatar
Jingning Han committed
48
49
50

  do {
    const int bit = (bits >> --len) & 1;
Yaowu Xu's avatar
Yaowu Xu committed
51
    cost += av1_cost_bit(probs[i >> 1], bit);
Jingning Han's avatar
Jingning Han committed
52
53
54
55
56
57
    i = tree[i + bit];
  } while (len);

  return cost;
}

Yaowu Xu's avatar
Yaowu Xu committed
58
59
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);
Jingning Han's avatar
Jingning Han committed
60

Alex Converse's avatar
Alex Converse committed
61
#if CONFIG_ANS
Yaowu Xu's avatar
Yaowu Xu committed
62
void av1_cost_tokens_ans(int *costs, const aom_prob *tree_probs,
63
                         const rans_lut token_cdf, int skip_eob);
Alex Converse's avatar
Alex Converse committed
64
65
#endif

Jingning Han's avatar
Jingning Han committed
66
67
68
69
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
70
#endif  // AV1_ENCODER_COST_H_