Coverage Report

Created: 2026-05-24 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/huffman_table.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/huffman_table.h"
7
8
#include <cstdint>
9
#include <cstring> /* for memcpy */
10
#include <vector>
11
12
#include "lib/jxl/ans_params.h"
13
14
namespace jxl {
15
16
/* Returns reverse(reverse(key, len) + 1, len), where reverse(key, len) is the
17
   bit-wise reversal of the len least significant bits of key. */
18
2.37M
static inline int GetNextKey(int key, int len) {
19
2.37M
  int step = 1u << (len - 1);
20
4.72M
  while (key & step) {
21
2.35M
    step >>= 1;
22
2.35M
  }
23
2.37M
  return (key & (step - 1)) + step;
24
2.37M
}
25
26
/* Stores code in table[0], table[step], table[2*step], ..., table[end] */
27
/* Assumes that end is an integer multiple of step */
28
static inline void ReplicateValue(HuffmanCode* table, int step, int end,
29
2.37M
                                  HuffmanCode code) {
30
2.58M
  do {
31
2.58M
    end -= step;
32
2.58M
    table[end] = code;
33
2.58M
  } while (end > 0);
34
2.37M
}
35
36
/* Returns the table width of the next 2nd level table. count is the histogram
37
   of bit lengths for the remaining symbols, len is the code length of the next
38
   processed symbol */
39
static inline size_t NextTableBitSize(const uint16_t* const count, size_t len,
40
116k
                                      int root_bits) {
41
116k
  size_t left = 1u << (len - root_bits);
42
116k
  while (len < PREFIX_MAX_BITS) {
43
114k
    if (left <= count[len]) break;
44
663
    left -= count[len];
45
663
    ++len;
46
663
    left <<= 1;
47
663
  }
48
116k
  return len - root_bits;
49
116k
}
50
51
uint32_t BuildHuffmanTable(HuffmanCode* root_table, int root_bits,
52
                           const uint8_t* const code_lengths,
53
22.3k
                           size_t code_lengths_size, uint16_t* count) {
54
22.3k
  HuffmanCode code;   /* current table entry */
55
22.3k
  HuffmanCode* table; /* next available space in table */
56
22.3k
  size_t len;         /* current code length */
57
22.3k
  size_t symbol;      /* symbol index in original or sorted table */
58
22.3k
  int key;            /* reversed prefix code */
59
22.3k
  int step;           /* step size to replicate values in current table */
60
22.3k
  int low;            /* low bits for current root entry */
61
22.3k
  int mask;           /* mask for low bits */
62
22.3k
  size_t table_bits;  /* key length of current table */
63
22.3k
  int table_size;     /* size of current table */
64
22.3k
  int total_size;     /* sum of root table size and 2nd level table sizes */
65
  /* offsets in sorted table for each length */
66
22.3k
  uint16_t offset[PREFIX_MAX_BITS + 1];
67
22.3k
  size_t max_length = 1;
68
69
22.3k
  if (code_lengths_size > 1u << PREFIX_MAX_BITS) return 0;
70
71
  /* symbols sorted by code length */
72
22.3k
  std::vector<uint16_t> sorted_storage(code_lengths_size);
73
22.3k
  uint16_t* sorted = sorted_storage.data();
74
75
  /* generate offsets into sorted symbol table by code length */
76
22.3k
  {
77
22.3k
    uint16_t sum = 0;
78
358k
    for (len = 1; len <= PREFIX_MAX_BITS; len++) {
79
335k
      offset[len] = sum;
80
335k
      if (count[len]) {
81
52.8k
        sum = static_cast<uint16_t>(sum + count[len]);
82
52.8k
        max_length = len;
83
52.8k
      }
84
335k
    }
85
22.3k
  }
86
87
  /* sort symbols by length, by symbol order within each length */
88
11.5M
  for (symbol = 0; symbol < code_lengths_size; symbol++) {
89
11.4M
    if (code_lengths[symbol] != 0) {
90
2.37M
      sorted[offset[code_lengths[symbol]]++] = symbol;
91
2.37M
    }
92
11.4M
  }
93
94
22.3k
  table = root_table;
95
22.3k
  table_bits = root_bits;
96
22.3k
  table_size = 1u << table_bits;
97
22.3k
  total_size = table_size;
98
99
  /* special case code with only one value */
100
22.3k
  if (offset[PREFIX_MAX_BITS] == 1) {
101
3.63k
    code.bits = 0;
102
3.63k
    code.value = static_cast<uint16_t>(sorted[0]);
103
120k
    for (key = 0; key < total_size; ++key) {
104
116k
      table[key] = code;
105
116k
    }
106
3.63k
    return total_size;
107
3.63k
  }
108
109
  /* fill in root table */
110
  /* let's reduce the table size to a smaller size if possible, and */
111
  /* create the repetitions by memcpy if possible in the coming loop */
112
18.7k
  if (table_bits > max_length) {
113
17.5k
    table_bits = max_length;
114
17.5k
    table_size = 1u << table_bits;
115
17.5k
  }
116
18.7k
  key = 0;
117
18.7k
  symbol = 0;
118
18.7k
  code.bits = 1;
119
18.7k
  step = 2;
120
70.0k
  do {
121
235k
    for (; count[code.bits] != 0; --count[code.bits]) {
122
165k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
123
165k
      ReplicateValue(&table[key], step, table_size, code);
124
165k
      key = GetNextKey(key, code.bits);
125
165k
    }
126
70.0k
    step <<= 1;
127
70.0k
  } while (++code.bits <= table_bits);
128
129
  /* if root_bits != table_bits we only created one fraction of the */
130
  /* table, and we need to replicate it now. */
131
72.5k
  while (total_size != table_size) {
132
53.7k
    memcpy(&table[table_size], &table[0], table_size * sizeof(table[0]));
133
53.7k
    table_size <<= 1;
134
53.7k
  }
135
136
  /* fill in 2nd level tables and add pointers to root table */
137
18.7k
  mask = total_size - 1;
138
18.7k
  low = -1;
139
20.8k
  for (len = root_bits + 1, step = 2; len <= max_length; ++len, step <<= 1) {
140
2.20M
    for (; count[len] != 0; --count[len]) {
141
2.20M
      if ((key & mask) != low) {
142
116k
        table += table_size;
143
116k
        table_bits = NextTableBitSize(count, len, root_bits);
144
116k
        table_size = 1u << table_bits;
145
116k
        total_size += table_size;
146
116k
        low = key & mask;
147
116k
        root_table[low].bits = static_cast<uint8_t>(table_bits + root_bits);
148
116k
        root_table[low].value =
149
116k
            static_cast<uint16_t>((table - root_table) - low);
150
116k
      }
151
2.20M
      code.bits = static_cast<uint8_t>(len - root_bits);
152
2.20M
      code.value = static_cast<uint16_t>(sorted[symbol++]);
153
2.20M
      ReplicateValue(&table[key >> root_bits], step, table_size, code);
154
2.20M
      key = GetNextKey(key, len);
155
2.20M
    }
156
2.13k
  }
157
158
18.7k
  return total_size;
159
22.3k
}
160
161
}  // namespace jxl