prob.h 3.3 KB

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