prob.h 4.76 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar 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.
John Koleszar's avatar
John Koleszar committed
10 11
 */

Adrian Grange's avatar
Adrian Grange committed
12 13
#ifndef AOM_DSP_PROB_H_
#define AOM_DSP_PROB_H_
John Koleszar's avatar
John Koleszar committed
14

Adrian Grange's avatar
Adrian Grange committed
15 16
#include "./aom_config.h"
#include "./aom_dsp_common.h"
17

Yaowu Xu's avatar
Yaowu Xu committed
18
#include "aom_ports/mem.h"
19

20 21 22 23
#ifdef __cplusplus
extern "C" {
#endif

Adrian Grange's avatar
Adrian Grange committed
24
typedef uint8_t aom_prob;
John Koleszar's avatar
John Koleszar committed
25

26 27 28
// TODO(negge): Rename this aom_prob once we remove vpxbool.
typedef uint16_t aom_cdf_prob;

29 30
#define MAX_PROB 255

Adrian Grange's avatar
Adrian Grange committed
31
#define aom_prob_half ((aom_prob)128)
John Koleszar's avatar
John Koleszar committed
32

Adrian Grange's avatar
Adrian Grange committed
33
typedef int8_t aom_tree_index;
John Koleszar's avatar
John Koleszar committed
34

35
#define TREE_SIZE(leaf_count) (-2 + 2 * (leaf_count))
36

Adrian Grange's avatar
Adrian Grange committed
37
#define aom_complement(x) (255 - x)
John Koleszar's avatar
John Koleszar committed
38

Yaowu Xu's avatar
Yaowu Xu committed
39 40
#define MODE_MV_COUNT_SAT 20

John Koleszar's avatar
John Koleszar committed
41
/* We build coding trees compactly in arrays.
Adrian Grange's avatar
Adrian Grange committed
42
   Each node of the tree is a pair of aom_tree_indices.
John Koleszar's avatar
John Koleszar committed
43 44 45 46 47
   Array index often references a corresponding probability table.
   Index <= 0 means done encoding/decoding and value = -Index,
   Index > 0 means need another bit, specification at index.
   Nonnegative indices are always even;  processing begins at node 0. */

Adrian Grange's avatar
Adrian Grange committed
48
typedef const aom_tree_index aom_tree[];
John Koleszar's avatar
John Koleszar committed
49

Adrian Grange's avatar
Adrian Grange committed
50
static INLINE aom_prob clip_prob(int p) {
51
  return (p > 255) ? 255 : (p < 1) ? 1 : p;
52 53
}

Adrian Grange's avatar
Adrian Grange committed
54
static INLINE aom_prob get_prob(int num, int den) {
55 56
  return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den);
}
57

Adrian Grange's avatar
Adrian Grange committed
58
static INLINE aom_prob get_binary_prob(int n0, int n1) {
59 60 61
  return get_prob(n0, n0 + n1);
}

62
/* This function assumes prob1 and prob2 are already within [1,255] range. */
Adrian Grange's avatar
Adrian Grange committed
63
static INLINE aom_prob weighted_prob(int prob1, int prob2, int factor) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
64
  return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8);
65
}
John Koleszar's avatar
John Koleszar committed
66

Adrian Grange's avatar
Adrian Grange committed
67
static INLINE aom_prob merge_probs(aom_prob pre_prob, const unsigned int ct[2],
68 69
                                   unsigned int count_sat,
                                   unsigned int max_update_factor) {
Adrian Grange's avatar
Adrian Grange committed
70
  const aom_prob prob = get_binary_prob(ct[0], ct[1]);
Adrian Grange's avatar
Adrian Grange committed
71
  const unsigned int count = AOMMIN(ct[0] + ct[1], count_sat);
72 73 74 75
  const unsigned int factor = max_update_factor * count / count_sat;
  return weighted_prob(pre_prob, prob, factor);
}

Yaowu Xu's avatar
Yaowu Xu committed
76 77
// MODE_MV_MAX_UPDATE_FACTOR (128) * count / MODE_MV_COUNT_SAT;
static const int count_to_update_factor[MODE_MV_COUNT_SAT + 1] = {
clang-format's avatar
clang-format committed
78
  0,  6,  12, 19, 25, 32,  38,  44,  51,  57, 64,
Yaowu Xu's avatar
Yaowu Xu committed
79 80 81
  70, 76, 83, 89, 96, 102, 108, 115, 121, 128
};

Adrian Grange's avatar
Adrian Grange committed
82
static INLINE aom_prob mode_mv_merge_probs(aom_prob pre_prob,
Yaowu Xu's avatar
Yaowu Xu committed
83 84 85 86 87
                                           const unsigned int ct[2]) {
  const unsigned int den = ct[0] + ct[1];
  if (den == 0) {
    return pre_prob;
  } else {
Adrian Grange's avatar
Adrian Grange committed
88
    const unsigned int count = AOMMIN(den, MODE_MV_COUNT_SAT);
Yaowu Xu's avatar
Yaowu Xu committed
89
    const unsigned int factor = count_to_update_factor[count];
Adrian Grange's avatar
Adrian Grange committed
90
    const aom_prob prob =
Yaowu Xu's avatar
Yaowu Xu committed
91 92 93 94 95
        clip_prob(((int64_t)(ct[0]) * 256 + (den >> 1)) / den);
    return weighted_prob(pre_prob, prob, factor);
  }
}

Adrian Grange's avatar
Adrian Grange committed
96 97
void aom_tree_merge_probs(const aom_tree_index *tree, const aom_prob *pre_probs,
                          const unsigned int *counts, aom_prob *probs);
98

99 100
#if CONFIG_DAALA_EC
int tree_to_cdf(const aom_tree_index *tree, const aom_prob *probs,
101
                aom_tree_index root, aom_cdf_prob *cdf, aom_tree_index *ind,
102
                int *pth, int *len);
103 104

static INLINE void av1_tree_to_cdf(const aom_tree_index *tree,
105
                                   const aom_prob *probs, aom_cdf_prob *cdf) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119
  aom_tree_index index[16];
  int path[16];
  int dist[16];
  tree_to_cdf(tree, probs, 0, cdf, index, path, dist);
}

#define av1_tree_to_cdf_1D(tree, probs, cdf, u) \
  do {                                          \
    int i;                                      \
    for (i = 0; i < u; i++) {                   \
      av1_tree_to_cdf(tree, probs[i], cdf[i]);  \
    }                                           \
  } while (0)

120
#define av1_tree_to_cdf_2D(tree, probs, cdf, v, u)     \
121 122 123 124 125 126 127 128 129
  do {                                                 \
    int j;                                             \
    int i;                                             \
    for (j = 0; j < v; j++) {                          \
      for (i = 0; i < u; i++) {                        \
        av1_tree_to_cdf(tree, probs[j][i], cdf[j][i]); \
      }                                                \
    }                                                  \
  } while (0)
130 131 132

void av1_indices_from_tree(int *ind, int *inv, int len,
                           const aom_tree_index *tree);
133 134
#endif

Adrian Grange's avatar
Adrian Grange committed
135
DECLARE_ALIGNED(16, extern const uint8_t, aom_norm[256]);
136

137 138 139 140
#ifdef __cplusplus
}  // extern "C"
#endif

Adrian Grange's avatar
Adrian Grange committed
141
#endif  // AOM_DSP_PROB_H_