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

11
12
#ifndef VP9_COMMON_VP9_TREECODER_H_
#define VP9_COMMON_VP9_TREECODER_H_
John Koleszar's avatar
John Koleszar committed
13

14
#include "./vpx_config.h"
15
16
#include "vpx/vpx_integer.h"

17
typedef uint8_t vp9_prob;
John Koleszar's avatar
John Koleszar committed
18

19
#define vp9_prob_half ((vp9_prob) 128)
John Koleszar's avatar
John Koleszar committed
20

21
typedef int8_t vp9_tree_index;
John Koleszar's avatar
John Koleszar committed
22

23
#define vp9_complement(x) (255 - x)
John Koleszar's avatar
John Koleszar committed
24
25

/* We build coding trees compactly in arrays.
26
   Each node of the tree is a pair of vp9_tree_indices.
John Koleszar's avatar
John Koleszar committed
27
28
29
30
31
   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. */

32
typedef const vp9_tree_index vp9_tree[], *vp9_tree_p;
John Koleszar's avatar
John Koleszar committed
33

34
struct vp9_token {
John Koleszar's avatar
John Koleszar committed
35
  int value;
36
37
  int len;
};
John Koleszar's avatar
John Koleszar committed
38
39
40

/* Construct encoding array from tree. */

41
42
void vp9_tokens_from_tree(struct vp9_token*, vp9_tree);
void vp9_tokens_from_tree_offset(struct vp9_token*, vp9_tree, int offset);
John Koleszar's avatar
John Koleszar committed
43
44
45
46
47
48

/* Convert array of token occurrence counts into a table of probabilities
   for the associated binary encoding tree.  Also writes count of branches
   taken for each node on the tree; this facilitiates decisions as to
   probability updates. */

49
void vp9_tree_probs_from_distribution(vp9_tree tree,
50
51
                                      vp9_prob probs[ /* n - 1 */ ],
                                      unsigned int branch_ct[ /* n - 1 */ ][2],
52
53
                                      const unsigned int num_events[ /* n */ ],
                                      unsigned int tok0_offset);
John Koleszar's avatar
John Koleszar committed
54

55
static INLINE vp9_prob clip_prob(int p) {
56
57
58
  return (p > 255) ? 255u : (p < 1) ? 1u : p;
}

59
60
61
62
63
64
65
66
// int64 is not needed for normal frame level calculations.
// However when outputing entropy stats accumulated over many frames
// or even clips we can overflow int math.
#ifdef ENTROPY_STATS
static INLINE vp9_prob get_prob(int num, int den) {
  return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den);
}
#else
67
static INLINE vp9_prob get_prob(int num, int den) {
68
  return (den == 0) ? 128u : clip_prob((num * 256 + (den >> 1)) / den);
69
}
70
#endif
71

72
static INLINE vp9_prob get_binary_prob(int n0, int n1) {
73
74
75
76
  return get_prob(n0, n0 + n1);
}

/* this function assumes prob1 and prob2 are already within [1,255] range */
77
static INLINE vp9_prob weighted_prob(int prob1, int prob2, int factor) {
78
79
  return (prob1 * (256 - factor) + prob2 * factor + 128) >> 8;
}
John Koleszar's avatar
John Koleszar committed
80

81
#endif  // VP9_COMMON_VP9_TREECODER_H_