Coverage Report

Created: 2025-08-12 07:37

/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 <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
4.28M
static inline int GetNextKey(int key, int len) {
19
4.28M
  int step = 1u << (len - 1);
20
8.55M
  while (key & step) {
21
4.27M
    step >>= 1;
22
4.27M
  }
23
4.28M
  return (key & (step - 1)) + step;
24
4.28M
}
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
4.28M
                                  HuffmanCode code) {
30
4.43M
  do {
31
4.43M
    end -= step;
32
4.43M
    table[end] = code;
33
4.43M
  } while (end > 0);
34
4.28M
}
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
136k
                                      int root_bits) {
41
136k
  size_t left = 1u << (len - root_bits);
42
141k
  while (len < PREFIX_MAX_BITS) {
43
138k
    if (left <= count[len]) break;
44
4.24k
    left -= count[len];
45
4.24k
    ++len;
46
4.24k
    left <<= 1;
47
4.24k
  }
48
136k
  return len - root_bits;
49
136k
}
50
51
uint32_t BuildHuffmanTable(HuffmanCode* root_table, int root_bits,
52
                           const uint8_t* const code_lengths,
53
10.4k
                           size_t code_lengths_size, uint16_t* count) {
54
10.4k
  HuffmanCode code;   /* current table entry */
55
10.4k
  HuffmanCode* table; /* next available space in table */
56
10.4k
  size_t len;         /* current code length */
57
10.4k
  size_t symbol;      /* symbol index in original or sorted table */
58
10.4k
  int key;            /* reversed prefix code */
59
10.4k
  int step;           /* step size to replicate values in current table */
60
10.4k
  int low;            /* low bits for current root entry */
61
10.4k
  int mask;           /* mask for low bits */
62
10.4k
  size_t table_bits;  /* key length of current table */
63
10.4k
  int table_size;     /* size of current table */
64
10.4k
  int total_size;     /* sum of root table size and 2nd level table sizes */
65
  /* offsets in sorted table for each length */
66
10.4k
  uint16_t offset[PREFIX_MAX_BITS + 1];
67
10.4k
  size_t max_length = 1;
68
69
10.4k
  if (code_lengths_size > 1u << PREFIX_MAX_BITS) return 0;
70
71
  /* symbols sorted by code length */
72
10.4k
  std::vector<uint16_t> sorted_storage(code_lengths_size);
73
10.4k
  uint16_t* sorted = sorted_storage.data();
74
75
  /* generate offsets into sorted symbol table by code length */
76
10.4k
  {
77
10.4k
    uint16_t sum = 0;
78
167k
    for (len = 1; len <= PREFIX_MAX_BITS; len++) {
79
157k
      offset[len] = sum;
80
157k
      if (count[len]) {
81
24.7k
        sum = static_cast<uint16_t>(sum + count[len]);
82
24.7k
        max_length = len;
83
24.7k
      }
84
157k
    }
85
10.4k
  }
86
87
  /* sort symbols by length, by symbol order within each length */
88
48.1M
  for (symbol = 0; symbol < code_lengths_size; symbol++) {
89
48.1M
    if (code_lengths[symbol] != 0) {
90
4.28M
      sorted[offset[code_lengths[symbol]]++] = symbol;
91
4.28M
    }
92
48.1M
  }
93
94
10.4k
  table = root_table;
95
10.4k
  table_bits = root_bits;
96
10.4k
  table_size = 1u << table_bits;
97
10.4k
  total_size = table_size;
98
99
  /* special case code with only one value */
100
10.4k
  if (offset[PREFIX_MAX_BITS] == 1) {
101
2.25k
    code.bits = 0;
102
2.25k
    code.value = static_cast<uint16_t>(sorted[0]);
103
74.4k
    for (key = 0; key < total_size; ++key) {
104
72.1k
      table[key] = code;
105
72.1k
    }
106
2.25k
    return total_size;
107
2.25k
  }
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
8.21k
  if (table_bits > max_length) {
113
6.18k
    table_bits = max_length;
114
6.18k
    table_size = 1u << table_bits;
115
6.18k
  }
116
8.21k
  key = 0;
117
8.21k
  symbol = 0;
118
8.21k
  code.bits = 1;
119
8.21k
  step = 2;
120
39.0k
  do {
121
340k
    for (; count[code.bits] != 0; --count[code.bits]) {
122
300k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
123
300k
      ReplicateValue(&table[key], step, table_size, code);
124
300k
      key = GetNextKey(key, code.bits);
125
300k
    }
126
39.0k
    step <<= 1;
127
39.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
23.5k
  while (total_size != table_size) {
132
15.3k
    memcpy(&table[table_size], &table[0], table_size * sizeof(table[0]));
133
15.3k
    table_size <<= 1;
134
15.3k
  }
135
136
  /* fill in 2nd level tables and add pointers to root table */
137
8.21k
  mask = total_size - 1;
138
8.21k
  low = -1;
139
13.9k
  for (len = root_bits + 1, step = 2; len <= max_length; ++len, step <<= 1) {
140
3.98M
    for (; count[len] != 0; --count[len]) {
141
3.98M
      if ((key & mask) != low) {
142
136k
        table += table_size;
143
136k
        table_bits = NextTableBitSize(count, len, root_bits);
144
136k
        table_size = 1u << table_bits;
145
136k
        total_size += table_size;
146
136k
        low = key & mask;
147
136k
        root_table[low].bits = static_cast<uint8_t>(table_bits + root_bits);
148
136k
        root_table[low].value =
149
136k
            static_cast<uint16_t>((table - root_table) - low);
150
136k
      }
151
3.98M
      code.bits = static_cast<uint8_t>(len - root_bits);
152
3.98M
      code.value = static_cast<uint16_t>(sorted[symbol++]);
153
3.98M
      ReplicateValue(&table[key >> root_bits], step, table_size, code);
154
3.98M
      key = GetNextKey(key, len);
155
3.98M
    }
156
5.70k
  }
157
158
8.21k
  return total_size;
159
10.4k
}
160
161
}  // namespace jxl