Coverage Report

Created: 2025-11-16 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/ans_common.cc
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#include "lib/jxl/ans_common.h"
7
8
#include <cstddef>
9
#include <cstdint>
10
#include <vector>
11
12
#include "lib/jxl/ans_params.h"
13
#include "lib/jxl/base/compiler_specific.h"
14
#include "lib/jxl/base/status.h"
15
16
namespace jxl {
17
18
// First, all trailing non-occurring symbols are removed from the distribution;
19
// if this leaves the distribution empty, a placeholder symbol with max weight
20
// is  added. This ensures that the resulting distribution sums to total table
21
// size. Then, `entry_size` is chosen to be the largest power of two so that
22
// `table_size` = ANS_TAB_SIZE/`entry_size` is at least as big as the
23
// distribution size.
24
// Note that each entry will only ever contain two different symbols, and
25
// consecutive ranges of offsets, which allows us to use a compact
26
// representation.
27
// Each entry is initialized with only the (symbol=i, offset) pairs; then
28
// positions for which the entry overflows (i.e. distribution[i] > entry_size)
29
// or is not full are computed, and put into a stack in increasing order.
30
// Missing symbols in the distribution are padded with 0 (because `table_size`
31
// >= number of symbols). The `cutoff` value for each entry is initialized to
32
// the number of occupied slots in that entry (i.e. `distributions[i]`). While
33
// the overflowing-symbol stack is not empty (which implies that the
34
// underflowing-symbol stack also is not), the top overfull and underfull
35
// positions are popped from the stack; the empty slots in the underfull entry
36
// are then filled with as many slots as needed from the overfull entry; such
37
// slots are placed after the slots in the overfull entry, and `offsets[1]` is
38
// computed accordingly. The formerly underfull entry is thus now neither
39
// underfull nor overfull, and represents exactly two symbols. The overfull
40
// entry might be either overfull or underfull, and is pushed into the
41
// corresponding stack.
42
Status InitAliasTable(std::vector<int32_t> distribution, uint32_t log_range,
43
                      size_t log_alpha_size,
44
505k
                      AliasTable::Entry* JXL_RESTRICT a) {
45
505k
  const uint32_t range = 1 << log_range;
46
505k
  const size_t table_size = 1 << log_alpha_size;
47
505k
  JXL_ENSURE(table_size <= range);
48
1.06M
  while (!distribution.empty() && distribution.back() == 0) {
49
561k
    distribution.pop_back();
50
561k
  }
51
  // Ensure that a valid table is always returned, even for an empty
52
  // alphabet. Otherwise, a specially-crafted stream might crash the
53
  // decoder.
54
505k
  if (distribution.empty()) {
55
0
    distribution.emplace_back(range);
56
0
  }
57
505k
  JXL_ENSURE(distribution.size() <= table_size);
58
505k
  const uint32_t entry_size = range >> log_alpha_size;  // this is exact
59
505k
  int single_symbol = -1;
60
505k
  int sum = 0;
61
  // Special case for single-symbol distributions, that ensures that the state
62
  // does not change when decoding from such a distribution. Note that, since we
63
  // hardcode offset0 == 0, it is not straightforward (if at all possible) to
64
  // fix the general case to produce this result.
65
8.52M
  for (size_t sym = 0; sym < distribution.size(); sym++) {
66
8.01M
    int32_t v = distribution[sym];
67
8.01M
    sum += v;
68
8.01M
    if (v == ANS_TAB_SIZE) {
69
69.3k
      JXL_ENSURE(single_symbol == -1);
70
69.3k
      single_symbol = sym;
71
69.3k
    }
72
8.01M
  }
73
505k
  JXL_ENSURE(static_cast<uint32_t>(sum) == range);
74
505k
  if (single_symbol != -1) {
75
69.3k
    uint8_t sym = single_symbol;
76
69.3k
    JXL_ENSURE(single_symbol == sym);
77
7.39M
    for (size_t i = 0; i < table_size; i++) {
78
7.32M
      a[i].right_value = sym;
79
7.32M
      a[i].cutoff = 0;
80
7.32M
      a[i].offsets1 = entry_size * i;
81
7.32M
      a[i].freq0 = 0;
82
7.32M
      a[i].freq1_xor_freq0 = ANS_TAB_SIZE;
83
7.32M
    }
84
69.3k
    return true;
85
69.3k
  }
86
87
436k
  std::vector<uint32_t> underfull_posn;
88
436k
  std::vector<uint32_t> overfull_posn;
89
436k
  std::vector<uint32_t> cutoffs(1 << log_alpha_size);
90
  // Initialize entries.
91
8.24M
  for (size_t i = 0; i < distribution.size(); i++) {
92
7.80M
    cutoffs[i] = distribution[i];
93
7.80M
    if (cutoffs[i] > entry_size) {
94
2.80M
      overfull_posn.push_back(i);
95
4.99M
    } else if (cutoffs[i] < entry_size) {
96
4.46M
      underfull_posn.push_back(i);
97
4.46M
    }
98
7.80M
  }
99
27.5M
  for (uint32_t i = distribution.size(); i < table_size; i++) {
100
27.1M
    cutoffs[i] = 0;
101
27.1M
    underfull_posn.push_back(i);
102
27.1M
  }
103
  // Reassign overflow/underflow values.
104
32.9M
  while (!overfull_posn.empty()) {
105
32.4M
    uint32_t overfull_i = overfull_posn.back();
106
32.4M
    overfull_posn.pop_back();
107
32.4M
    JXL_ENSURE(!underfull_posn.empty());
108
32.4M
    uint32_t underfull_i = underfull_posn.back();
109
32.4M
    underfull_posn.pop_back();
110
32.4M
    uint32_t underfull_by = entry_size - cutoffs[underfull_i];
111
32.4M
    cutoffs[overfull_i] -= underfull_by;
112
    // overfull positions have their original symbols
113
32.4M
    a[underfull_i].right_value = overfull_i;
114
32.4M
    a[underfull_i].offsets1 = cutoffs[overfull_i];
115
    // Slots in the right part of entry underfull_i were taken from the end
116
    // of the symbols in entry overfull_i.
117
32.4M
    if (cutoffs[overfull_i] < entry_size) {
118
849k
      underfull_posn.push_back(overfull_i);
119
31.6M
    } else if (cutoffs[overfull_i] > entry_size) {
120
29.6M
      overfull_posn.push_back(overfull_i);
121
29.6M
    }
122
32.4M
  }
123
35.3M
  for (uint32_t i = 0; i < table_size; i++) {
124
    // cutoffs[i] is properly initialized but the clang-analyzer doesn't infer
125
    // it since it is partially initialized across two for-loops.
126
    // NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult)
127
34.9M
    if (cutoffs[i] == entry_size) {
128
2.49M
      a[i].right_value = i;
129
2.49M
      a[i].offsets1 = 0;
130
2.49M
      a[i].cutoff = 0;
131
32.4M
    } else {
132
      // Note that, if cutoff is not equal to entry_size,
133
      // a[i].offsets1 was initialized with (overfull cutoff) -
134
      // (entry_size - a[i].cutoff). Thus, subtracting
135
      // a[i].cutoff cannot make it negative.
136
32.4M
      a[i].offsets1 -= cutoffs[i];
137
32.4M
      a[i].cutoff = cutoffs[i];
138
32.4M
    }
139
34.9M
    const size_t freq0 = i < distribution.size() ? distribution[i] : 0;
140
34.9M
    const size_t i1 = a[i].right_value;
141
34.9M
    const size_t freq1 = i1 < distribution.size() ? distribution[i1] : 0;
142
34.9M
    a[i].freq0 = static_cast<uint16_t>(freq0);
143
34.9M
    a[i].freq1_xor_freq0 = static_cast<uint16_t>(freq1 ^ freq0);
144
34.9M
  }
145
436k
  return true;
146
436k
}
147
148
}  // namespace jxl