blob: ad829c888f2656c5c58c7486a4bb7859ed5d20d2 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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 AOM_DSP_PROB_H_
13#define AOM_DSP_PROB_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
16#include "./aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
18#include "aom_ports/mem.h"
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
Yaowu Xuf883b422016-08-30 14:01:10 -070024typedef uint8_t aom_prob;
Yaowu Xuc27fc142016-08-22 16:08:15 -070025
Alex Conversea1ac9722016-10-12 15:59:58 -070026// TODO(negge): Rename this aom_prob once we remove vpxbool.
27typedef uint16_t aom_cdf_prob;
28
Yaowu Xuc27fc142016-08-22 16:08:15 -070029#define MAX_PROB 255
30
Yaowu Xuf883b422016-08-30 14:01:10 -070031#define aom_prob_half ((aom_prob)128)
Yaowu Xuc27fc142016-08-22 16:08:15 -070032
Yaowu Xuf883b422016-08-30 14:01:10 -070033typedef int8_t aom_tree_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -070034
35#define TREE_SIZE(leaf_count) (2 * (leaf_count)-2)
36
Yaowu Xuf883b422016-08-30 14:01:10 -070037#define aom_complement(x) (255 - x)
Yaowu Xuc27fc142016-08-22 16:08:15 -070038
39#define MODE_MV_COUNT_SAT 20
40
41/* We build coding trees compactly in arrays.
Yaowu Xuf883b422016-08-30 14:01:10 -070042 Each node of the tree is a pair of aom_tree_indices.
Yaowu Xuc27fc142016-08-22 16:08:15 -070043 Array index often references a corresponding probability table.
44 Index <= 0 means done encoding/decoding and value = -Index,
45 Index > 0 means need another bit, specification at index.
46 Nonnegative indices are always even; processing begins at node 0. */
47
Yaowu Xuf883b422016-08-30 14:01:10 -070048typedef const aom_tree_index aom_tree[];
Yaowu Xuc27fc142016-08-22 16:08:15 -070049
Yaowu Xuf883b422016-08-30 14:01:10 -070050static INLINE aom_prob clip_prob(int p) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070051 return (p > 255) ? 255 : (p < 1) ? 1 : p;
52}
53
Yaowu Xuf883b422016-08-30 14:01:10 -070054static INLINE aom_prob get_prob(int num, int den) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070055 return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den);
56}
57
Yaowu Xuf883b422016-08-30 14:01:10 -070058static INLINE aom_prob get_binary_prob(int n0, int n1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070059 return get_prob(n0, n0 + n1);
60}
61
62/* This function assumes prob1 and prob2 are already within [1,255] range. */
Yaowu Xuf883b422016-08-30 14:01:10 -070063static INLINE aom_prob weighted_prob(int prob1, int prob2, int factor) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070064 return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8);
65}
66
Yaowu Xuf883b422016-08-30 14:01:10 -070067static INLINE aom_prob merge_probs(aom_prob pre_prob, const unsigned int ct[2],
Yaowu Xuc27fc142016-08-22 16:08:15 -070068 unsigned int count_sat,
69 unsigned int max_update_factor) {
Yaowu Xuf883b422016-08-30 14:01:10 -070070 const aom_prob prob = get_binary_prob(ct[0], ct[1]);
71 const unsigned int count = AOMMIN(ct[0] + ct[1], count_sat);
Yaowu Xuc27fc142016-08-22 16:08:15 -070072 const unsigned int factor = max_update_factor * count / count_sat;
73 return weighted_prob(pre_prob, prob, factor);
74}
75
76// MODE_MV_MAX_UPDATE_FACTOR (128) * count / MODE_MV_COUNT_SAT;
77static const int count_to_update_factor[MODE_MV_COUNT_SAT + 1] = {
78 0, 6, 12, 19, 25, 32, 38, 44, 51, 57, 64,
79 70, 76, 83, 89, 96, 102, 108, 115, 121, 128
80};
81
Yaowu Xuf883b422016-08-30 14:01:10 -070082static INLINE aom_prob mode_mv_merge_probs(aom_prob pre_prob,
Yaowu Xuc27fc142016-08-22 16:08:15 -070083 const unsigned int ct[2]) {
84 const unsigned int den = ct[0] + ct[1];
85 if (den == 0) {
86 return pre_prob;
87 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -070088 const unsigned int count = AOMMIN(den, MODE_MV_COUNT_SAT);
Yaowu Xuc27fc142016-08-22 16:08:15 -070089 const unsigned int factor = count_to_update_factor[count];
Yaowu Xuf883b422016-08-30 14:01:10 -070090 const aom_prob prob =
Yaowu Xuc27fc142016-08-22 16:08:15 -070091 clip_prob(((int64_t)(ct[0]) * 256 + (den >> 1)) / den);
92 return weighted_prob(pre_prob, prob, factor);
93 }
94}
95
Yaowu Xuf883b422016-08-30 14:01:10 -070096void aom_tree_merge_probs(const aom_tree_index *tree, const aom_prob *pre_probs,
97 const unsigned int *counts, aom_prob *probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -070098
Nathan E. Egge43acafd2016-03-06 13:41:53 -050099#if CONFIG_DAALA_EC
100int tree_to_cdf(const aom_tree_index *tree, const aom_prob *probs,
101 aom_tree_index root, uint16_t *cdf, aom_tree_index *ind,
102 int *pth, int *len);
103#endif
104
Yaowu Xuf883b422016-08-30 14:01:10 -0700105DECLARE_ALIGNED(16, extern const uint8_t, aom_norm[256]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700106
107#ifdef __cplusplus
108} // extern "C"
109#endif
110
Yaowu Xuf883b422016-08-30 14:01:10 -0700111#endif // AOM_DSP_PROB_H_