prob.h 3.28 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
 */

12 13
#ifndef VPX_DSP_PROB_H_
#define VPX_DSP_PROB_H_
John Koleszar's avatar
John Koleszar committed
14

15
#include "./vpx_config.h"
Yaowu Xu's avatar
Yaowu Xu committed
16
#include "./vpx_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

Yaowu Xu's avatar
Yaowu Xu committed
24
typedef uint8_t vpx_prob;
John Koleszar's avatar
John Koleszar committed
25

26 27
#define MAX_PROB 255

clang-format's avatar
clang-format committed
28
#define vpx_prob_half ((vpx_prob)128)
John Koleszar's avatar
John Koleszar committed
29

Yaowu Xu's avatar
Yaowu Xu committed
30
typedef int8_t vpx_tree_index;
John Koleszar's avatar
John Koleszar committed
31

32
#define TREE_SIZE(leaf_count) (-2 + 2 * (leaf_count))
33

Yaowu Xu's avatar
Yaowu Xu committed
34
#define vpx_complement(x) (255 - x)
John Koleszar's avatar
John Koleszar committed
35

Yaowu Xu's avatar
Yaowu Xu committed
36 37
#define MODE_MV_COUNT_SAT 20

John Koleszar's avatar
John Koleszar committed
38
/* We build coding trees compactly in arrays.
Yaowu Xu's avatar
Yaowu Xu committed
39
   Each node of the tree is a pair of vpx_tree_indices.
John Koleszar's avatar
John Koleszar committed
40 41 42 43 44
   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. */

Yaowu Xu's avatar
Yaowu Xu committed
45
typedef const vpx_tree_index vpx_tree[];
John Koleszar's avatar
John Koleszar committed
46

Yaowu Xu's avatar
Yaowu Xu committed
47
static INLINE vpx_prob clip_prob(int p) {
48
  return (p > 255) ? 255 : (p < 1) ? 1 : p;
49 50
}

Yaowu Xu's avatar
Yaowu Xu committed
51
static INLINE vpx_prob get_prob(int num, int den) {
52 53
  return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den);
}
54

Yaowu Xu's avatar
Yaowu Xu committed
55
static INLINE vpx_prob get_binary_prob(int n0, int n1) {
56 57 58
  return get_prob(n0, n0 + n1);
}

59
/* This function assumes prob1 and prob2 are already within [1,255] range. */
Yaowu Xu's avatar
Yaowu Xu committed
60
static INLINE vpx_prob weighted_prob(int prob1, int prob2, int factor) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
61
  return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8);
62
}
John Koleszar's avatar
John Koleszar committed
63

clang-format's avatar
clang-format committed
64
static INLINE vpx_prob merge_probs(vpx_prob pre_prob, const unsigned int ct[2],
65 66
                                   unsigned int count_sat,
                                   unsigned int max_update_factor) {
Yaowu Xu's avatar
Yaowu Xu committed
67
  const vpx_prob prob = get_binary_prob(ct[0], ct[1]);
68
  const unsigned int count = VPXMIN(ct[0] + ct[1], count_sat);
69 70 71 72
  const unsigned int factor = max_update_factor * count / count_sat;
  return weighted_prob(pre_prob, prob, factor);
}

Yaowu Xu's avatar
Yaowu Xu committed
73 74
// 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
75
  0,  6,  12, 19, 25, 32,  38,  44,  51,  57, 64,
Yaowu Xu's avatar
Yaowu Xu committed
76 77 78
  70, 76, 83, 89, 96, 102, 108, 115, 121, 128
};

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

Yaowu Xu's avatar
Yaowu Xu committed
93
void vpx_tree_merge_probs(const vpx_tree_index *tree, const vpx_prob *pre_probs,
Yaowu Xu's avatar
Yaowu Xu committed
94
                          const unsigned int *counts, vpx_prob *probs);
95

Yaowu Xu's avatar
Yaowu Xu committed
96
DECLARE_ALIGNED(16, extern const uint8_t, vpx_norm[256]);
97

98 99 100 101
#ifdef __cplusplus
}  // extern "C"
#endif

102
#endif  // VPX_DSP_PROB_H_