Coverage Report

Created: 2025-09-08 07:52

/src/libjxl/lib/jxl/huffman_table.cc
Line
Count
Source (jump to first uncovered line)
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 <cstring> /* for memcpy */
9
#include <vector>
10
11
#include "lib/jxl/ans_params.h"
12
#include "lib/jxl/dec_huffman.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
93.2k
static inline int GetNextKey(int key, int len) {
19
93.2k
  int step = 1u << (len - 1);
20
176k
  while (key & step) {
21
83.3k
    step >>= 1;
22
83.3k
  }
23
93.2k
  return (key & (step - 1)) + step;
24
93.2k
}
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
93.2k
                                  HuffmanCode code) {
30
659k
  do {
31
659k
    end -= step;
32
659k
    table[end] = code;
33
659k
  } while (end > 0);
34
93.2k
}
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
911
                                      int root_bits) {
41
911
  size_t left = 1u << (len - root_bits);
42
1.21k
  while (len < PREFIX_MAX_BITS) {
43
1.17k
    if (left <= count[len]) break;
44
299
    left -= count[len];
45
299
    ++len;
46
299
    left <<= 1;
47
299
  }
48
911
  return len - root_bits;
49
911
}
50
51
uint32_t BuildHuffmanTable(HuffmanCode* root_table, int root_bits,
52
                           const uint8_t* const code_lengths,
53
9.84k
                           size_t code_lengths_size, uint16_t* count) {
54
9.84k
  HuffmanCode code;   /* current table entry */
55
9.84k
  HuffmanCode* table; /* next available space in table */
56
9.84k
  size_t len;         /* current code length */
57
9.84k
  size_t symbol;      /* symbol index in original or sorted table */
58
9.84k
  int key;            /* reversed prefix code */
59
9.84k
  int step;           /* step size to replicate values in current table */
60
9.84k
  int low;            /* low bits for current root entry */
61
9.84k
  int mask;           /* mask for low bits */
62
9.84k
  size_t table_bits;  /* key length of current table */
63
9.84k
  int table_size;     /* size of current table */
64
9.84k
  int total_size;     /* sum of root table size and 2nd level table sizes */
65
  /* offsets in sorted table for each length */
66
9.84k
  uint16_t offset[PREFIX_MAX_BITS + 1];
67
9.84k
  size_t max_length = 1;
68
69
9.84k
  if (code_lengths_size > 1u << PREFIX_MAX_BITS) return 0;
70
71
  /* symbols sorted by code length */
72
9.84k
  std::vector<uint16_t> sorted_storage(code_lengths_size);
73
9.84k
  uint16_t* sorted = sorted_storage.data();
74
75
  /* generate offsets into sorted symbol table by code length */
76
9.84k
  {
77
9.84k
    uint16_t sum = 0;
78
157k
    for (len = 1; len <= PREFIX_MAX_BITS; len++) {
79
147k
      offset[len] = sum;
80
147k
      if (count[len]) {
81
38.6k
        sum = static_cast<uint16_t>(sum + count[len]);
82
38.6k
        max_length = len;
83
38.6k
      }
84
147k
    }
85
9.84k
  }
86
87
  /* sort symbols by length, by symbol order within each length */
88
1.49M
  for (symbol = 0; symbol < code_lengths_size; symbol++) {
89
1.48M
    if (code_lengths[symbol] != 0) {
90
93.2k
      sorted[offset[code_lengths[symbol]]++] = symbol;
91
93.2k
    }
92
1.48M
  }
93
94
9.84k
  table = root_table;
95
9.84k
  table_bits = root_bits;
96
9.84k
  table_size = 1u << table_bits;
97
9.84k
  total_size = table_size;
98
99
  /* special case code with only one value */
100
9.84k
  if (offset[PREFIX_MAX_BITS] == 1) {
101
0
    code.bits = 0;
102
0
    code.value = static_cast<uint16_t>(sorted[0]);
103
0
    for (key = 0; key < total_size; ++key) {
104
0
      table[key] = code;
105
0
    }
106
0
    return total_size;
107
0
  }
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
9.84k
  if (table_bits > max_length) {
113
7.67k
    table_bits = max_length;
114
7.67k
    table_size = 1u << table_bits;
115
7.67k
  }
116
9.84k
  key = 0;
117
9.84k
  symbol = 0;
118
9.84k
  code.bits = 1;
119
9.84k
  step = 2;
120
45.4k
  do {
121
134k
    for (; count[code.bits] != 0; --count[code.bits]) {
122
88.8k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
123
88.8k
      ReplicateValue(&table[key], step, table_size, code);
124
88.8k
      key = GetNextKey(key, code.bits);
125
88.8k
    }
126
45.4k
    step <<= 1;
127
45.4k
  } 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
28.3k
  while (total_size != table_size) {
132
18.4k
    memcpy(&table[table_size], &table[0], table_size * sizeof(table[0]));
133
18.4k
    table_size <<= 1;
134
18.4k
  }
135
136
  /* fill in 2nd level tables and add pointers to root table */
137
9.84k
  mask = total_size - 1;
138
9.84k
  low = -1;
139
10.3k
  for (len = root_bits + 1, step = 2; len <= max_length; ++len, step <<= 1) {
140
4.85k
    for (; count[len] != 0; --count[len]) {
141
4.37k
      if ((key & mask) != low) {
142
911
        table += table_size;
143
911
        table_bits = NextTableBitSize(count, len, root_bits);
144
911
        table_size = 1u << table_bits;
145
911
        total_size += table_size;
146
911
        low = key & mask;
147
911
        root_table[low].bits = static_cast<uint8_t>(table_bits + root_bits);
148
911
        root_table[low].value =
149
911
            static_cast<uint16_t>((table - root_table) - low);
150
911
      }
151
4.37k
      code.bits = static_cast<uint8_t>(len - root_bits);
152
4.37k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
153
4.37k
      ReplicateValue(&table[key >> root_bits], step, table_size, code);
154
4.37k
      key = GetNextKey(key, len);
155
4.37k
    }
156
482
  }
157
158
9.84k
  return total_size;
159
9.84k
}
160
161
}  // namespace jxl