Coverage Report

Created: 2025-11-16 07:20

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