2
0

treecoder.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include <assert.h>
  11. #include <stdio.h>
  12. #include "vp8/common/treecoder.h"
  13. #include "vpx/vpx_integer.h"
  14. static void tree2tok(struct vp8_token_struct *const p, vp8_tree t, int i, int v,
  15. int L) {
  16. v += v;
  17. ++L;
  18. do {
  19. const vp8_tree_index j = t[i++];
  20. if (j <= 0) {
  21. p[-j].value = v;
  22. p[-j].Len = L;
  23. } else {
  24. tree2tok(p, t, j, v, L);
  25. }
  26. } while (++v & 1);
  27. }
  28. void vp8_tokens_from_tree(struct vp8_token_struct *p, vp8_tree t) {
  29. tree2tok(p, t, 0, 0, 0);
  30. }
  31. void vp8_tokens_from_tree_offset(struct vp8_token_struct *p, vp8_tree t,
  32. int offset) {
  33. tree2tok(p - offset, t, 0, 0, 0);
  34. }
  35. static void branch_counts(int n, /* n = size of alphabet */
  36. vp8_token tok[/* n */], vp8_tree tree,
  37. unsigned int branch_ct[/* n-1 */][2],
  38. const unsigned int num_events[/* n */]) {
  39. const int tree_len = n - 1;
  40. int t = 0;
  41. assert(tree_len);
  42. do {
  43. branch_ct[t][0] = branch_ct[t][1] = 0;
  44. } while (++t < tree_len);
  45. t = 0;
  46. do {
  47. int L = tok[t].Len;
  48. const int enc = tok[t].value;
  49. const unsigned int ct = num_events[t];
  50. vp8_tree_index i = 0;
  51. do {
  52. const int b = (enc >> --L) & 1;
  53. const int j = i >> 1;
  54. assert(j < tree_len && 0 <= L);
  55. branch_ct[j][b] += ct;
  56. i = tree[i + b];
  57. } while (i > 0);
  58. assert(!L);
  59. } while (++t < n);
  60. }
  61. void vp8_tree_probs_from_distribution(int n, /* n = size of alphabet */
  62. vp8_token tok[/* n */], vp8_tree tree,
  63. vp8_prob probs[/* n-1 */],
  64. unsigned int branch_ct[/* n-1 */][2],
  65. const unsigned int num_events[/* n */],
  66. unsigned int Pfactor, int Round) {
  67. const int tree_len = n - 1;
  68. int t = 0;
  69. branch_counts(n, tok, tree, branch_ct, num_events);
  70. do {
  71. const unsigned int *const c = branch_ct[t];
  72. const unsigned int tot = c[0] + c[1];
  73. if (tot) {
  74. const unsigned int p =
  75. (unsigned int)(((uint64_t)c[0] * Pfactor) + (Round ? tot >> 1 : 0)) /
  76. tot;
  77. probs[t] = p < 256 ? (p ? p : 1) : 255; /* agree w/old version for now */
  78. } else {
  79. probs[t] = vp8_prob_half;
  80. }
  81. } while (++t < tree_len);
  82. }