treecoder.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2010 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. #if CONFIG_DEBUG
  11. #include <assert.h>
  12. #endif
  13. #include <stdio.h>
  14. #include "treecoder.h"
  15. static void tree2tok(
  16. struct vp8_token_struct *const p,
  17. vp8_tree t,
  18. int i,
  19. int v,
  20. int L
  21. )
  22. {
  23. v += v;
  24. ++L;
  25. do
  26. {
  27. const vp8_tree_index j = t[i++];
  28. if (j <= 0)
  29. {
  30. p[-j].value = v;
  31. p[-j].Len = L;
  32. }
  33. else
  34. tree2tok(p, t, j, v, L);
  35. }
  36. while (++v & 1);
  37. }
  38. void vp8_tokens_from_tree(struct vp8_token_struct *p, vp8_tree t)
  39. {
  40. tree2tok(p, t, 0, 0, 0);
  41. }
  42. void vp8_tokens_from_tree_offset(struct vp8_token_struct *p, vp8_tree t,
  43. int offset)
  44. {
  45. tree2tok(p - offset, t, 0, 0, 0);
  46. }
  47. static void branch_counts(
  48. int n, /* n = size of alphabet */
  49. vp8_token tok [ /* n */ ],
  50. vp8_tree tree,
  51. unsigned int branch_ct [ /* n-1 */ ] [2],
  52. const unsigned int num_events[ /* n */ ]
  53. )
  54. {
  55. const int tree_len = n - 1;
  56. int t = 0;
  57. #if CONFIG_DEBUG
  58. assert(tree_len);
  59. #endif
  60. do
  61. {
  62. branch_ct[t][0] = branch_ct[t][1] = 0;
  63. }
  64. while (++t < tree_len);
  65. t = 0;
  66. do
  67. {
  68. int L = tok[t].Len;
  69. const int enc = tok[t].value;
  70. const unsigned int ct = num_events[t];
  71. vp8_tree_index i = 0;
  72. do
  73. {
  74. const int b = (enc >> --L) & 1;
  75. const int j = i >> 1;
  76. #if CONFIG_DEBUG
  77. assert(j < tree_len && 0 <= L);
  78. #endif
  79. branch_ct [j] [b] += ct;
  80. i = tree[ i + b];
  81. }
  82. while (i > 0);
  83. #if CONFIG_DEBUG
  84. assert(!L);
  85. #endif
  86. }
  87. while (++t < n);
  88. }
  89. void vp8_tree_probs_from_distribution(
  90. int n, /* n = size of alphabet */
  91. vp8_token tok [ /* n */ ],
  92. vp8_tree tree,
  93. vp8_prob probs [ /* n-1 */ ],
  94. unsigned int branch_ct [ /* n-1 */ ] [2],
  95. const unsigned int num_events[ /* n */ ],
  96. unsigned int Pfac,
  97. int rd
  98. )
  99. {
  100. const int tree_len = n - 1;
  101. int t = 0;
  102. branch_counts(n, tok, tree, branch_ct, num_events);
  103. do
  104. {
  105. const unsigned int *const c = branch_ct[t];
  106. const unsigned int tot = c[0] + c[1];
  107. #if CONFIG_DEBUG
  108. assert(tot < (1 << 24)); /* no overflow below */
  109. #endif
  110. if (tot)
  111. {
  112. const unsigned int p = ((c[0] * Pfac) + (rd ? tot >> 1 : 0)) / tot;
  113. probs[t] = p < 256 ? (p ? p : 1) : 255; /* agree w/old version for now */
  114. }
  115. else
  116. probs[t] = vp8_prob_half;
  117. }
  118. while (++t < tree_len);
  119. }