prob.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * 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.
  9. */
  10. #ifndef VPX_DSP_PROB_H_
  11. #define VPX_DSP_PROB_H_
  12. #include "./vpx_config.h"
  13. #include "./vpx_dsp_common.h"
  14. #include "vpx_ports/mem.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. typedef uint8_t vpx_prob;
  19. #define MAX_PROB 255
  20. #define vpx_prob_half ((vpx_prob) 128)
  21. typedef int8_t vpx_tree_index;
  22. #define TREE_SIZE(leaf_count) (2 * (leaf_count) - 2)
  23. #define vpx_complement(x) (255 - x)
  24. #define MODE_MV_COUNT_SAT 20
  25. /* We build coding trees compactly in arrays.
  26. Each node of the tree is a pair of vpx_tree_indices.
  27. Array index often references a corresponding probability table.
  28. Index <= 0 means done encoding/decoding and value = -Index,
  29. Index > 0 means need another bit, specification at index.
  30. Nonnegative indices are always even; processing begins at node 0. */
  31. typedef const vpx_tree_index vpx_tree[];
  32. static INLINE vpx_prob clip_prob(int p) {
  33. return (p > 255) ? 255 : (p < 1) ? 1 : p;
  34. }
  35. static INLINE vpx_prob get_prob(int num, int den) {
  36. return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den);
  37. }
  38. static INLINE vpx_prob get_binary_prob(int n0, int n1) {
  39. return get_prob(n0, n0 + n1);
  40. }
  41. /* This function assumes prob1 and prob2 are already within [1,255] range. */
  42. static INLINE vpx_prob weighted_prob(int prob1, int prob2, int factor) {
  43. return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8);
  44. }
  45. static INLINE vpx_prob merge_probs(vpx_prob pre_prob,
  46. const unsigned int ct[2],
  47. unsigned int count_sat,
  48. unsigned int max_update_factor) {
  49. const vpx_prob prob = get_binary_prob(ct[0], ct[1]);
  50. const unsigned int count = VPXMIN(ct[0] + ct[1], count_sat);
  51. const unsigned int factor = max_update_factor * count / count_sat;
  52. return weighted_prob(pre_prob, prob, factor);
  53. }
  54. // MODE_MV_MAX_UPDATE_FACTOR (128) * count / MODE_MV_COUNT_SAT;
  55. static const int count_to_update_factor[MODE_MV_COUNT_SAT + 1] = {
  56. 0, 6, 12, 19, 25, 32, 38, 44, 51, 57, 64,
  57. 70, 76, 83, 89, 96, 102, 108, 115, 121, 128
  58. };
  59. static INLINE vpx_prob mode_mv_merge_probs(vpx_prob pre_prob,
  60. const unsigned int ct[2]) {
  61. const unsigned int den = ct[0] + ct[1];
  62. if (den == 0) {
  63. return pre_prob;
  64. } else {
  65. const unsigned int count = VPXMIN(den, MODE_MV_COUNT_SAT);
  66. const unsigned int factor = count_to_update_factor[count];
  67. const vpx_prob prob =
  68. clip_prob(((int64_t)(ct[0]) * 256 + (den >> 1)) / den);
  69. return weighted_prob(pre_prob, prob, factor);
  70. }
  71. }
  72. void vpx_tree_merge_probs(const vpx_tree_index *tree, const vpx_prob *pre_probs,
  73. const unsigned int *counts, vpx_prob *probs);
  74. DECLARE_ALIGNED(16, extern const uint8_t, vpx_norm[256]);
  75. #ifdef __cplusplus
  76. } // extern "C"
  77. #endif
  78. #endif // VPX_DSP_PROB_H_