Coverage Report

Created: 2026-06-10 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brunsli/c/dec/huffman_table.cc
Line
Count
Source
1
// Copyright (c) Google LLC 2019
2
//
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file or at
5
// https://opensource.org/licenses/MIT.
6
7
#include "./huffman_table.h"
8
9
#include <cstring> /* for memcpy */
10
#include <vector>
11
12
#include "../common/constants.h"
13
#include <brunsli/types.h>
14
#include "./huffman_decode.h"
15
16
namespace brunsli {
17
18
/* Returns reverse(reverse(key, len) + 1, len), where reverse(key, len) is the
19
   bit-wise reversal of the len least significant bits of key. */
20
11.8k
static inline int GetNextKey(int key, size_t len) {
21
11.8k
  int step = 1u << (len - 1);
22
23.2k
  while (key & step) {
23
11.3k
    step >>= 1;
24
11.3k
  }
25
11.8k
  return (key & (step - 1)) + step;
26
11.8k
}
27
28
/* Stores code in table[0], table[step], table[2*step], ..., table[end] */
29
/* Assumes that end is an integer multiple of step */
30
static inline void ReplicateValue(HuffmanCode* table, int step, int end,
31
11.8k
                                  HuffmanCode code) {
32
27.2k
  do {
33
27.2k
    end -= step;
34
27.2k
    table[end] = code;
35
27.2k
  } while (end > 0);
36
11.8k
}
37
38
/* Returns the table width of the next 2nd level table. count is the histogram
39
   of bit lengths for the remaining symbols, len is the code length of the next
40
   processed symbol */
41
static inline size_t NextTableBitSize(const uint16_t* const count, size_t len,
42
1.70k
                                      size_t root_bits) {
43
1.70k
  size_t left = size_t(1) << (len - root_bits);
44
1.78k
  while (len < kMaxHuffmanBits) {
45
1.77k
    if (left <= count[len]) break;
46
83
    left -= count[len];
47
83
    ++len;
48
83
    left <<= 1;
49
83
  }
50
1.70k
  return len - root_bits;
51
1.70k
}
52
53
uint32_t BuildHuffmanTable(HuffmanCode* root_table, size_t root_bits,
54
                           const uint8_t* const code_lengths,
55
462
                           size_t code_lengths_size, uint16_t* count) {
56
462
  HuffmanCode code;    /* current table entry */
57
462
  HuffmanCode* table;  /* next available space in table */
58
462
  size_t len;          /* current code length */
59
462
  size_t symbol;       /* symbol index in original or sorted table */
60
462
  int key;             /* reversed prefix code */
61
462
  int step;            /* step size to replicate values in current table */
62
462
  int low;             /* low bits for current root entry */
63
462
  int mask;            /* mask for low bits */
64
462
  size_t table_bits;   /* key length of current table */
65
462
  int table_size;      /* size of current table */
66
462
  int total_size;      /* sum of root table size and 2nd level table sizes */
67
  /* offsets in sorted table for each length */
68
462
  uint16_t offset[kMaxHuffmanBits + 1];
69
462
  size_t max_length = 1;
70
71
462
  if (code_lengths_size > 1u << kMaxHuffmanBits) return 0;
72
73
  /* symbols sorted by code length */
74
462
  std::vector<uint16_t> sorted_storage(code_lengths_size);
75
462
  uint16_t* sorted = sorted_storage.data();
76
77
  /* generate offsets into sorted symbol table by code length */
78
462
  {
79
462
    uint16_t sum = 0;
80
7.39k
    for (len = 1; len <= kMaxHuffmanBits; len++) {
81
6.93k
      offset[len] = sum;
82
6.93k
      if (count[len]) {
83
1.20k
        sum = static_cast<uint16_t>(sum + count[len]);
84
1.20k
        max_length = len;
85
1.20k
      }
86
6.93k
    }
87
462
  }
88
89
  /* sort symbols by length, by symbol order within each length */
90
24.3k
  for (symbol = 0; symbol < code_lengths_size; symbol++) {
91
23.8k
    if (code_lengths[symbol] != 0) {
92
11.8k
      sorted[offset[code_lengths[symbol]]++] = static_cast<uint16_t>(symbol);
93
11.8k
    }
94
23.8k
  }
95
96
462
  table = root_table;
97
462
  table_bits = root_bits;
98
462
  table_size = 1u << table_bits;
99
462
  total_size = table_size;
100
101
  /* special case code with only one value */
102
462
  if (offset[kMaxHuffmanBits] == 1) {
103
56
    code.bits = 0;
104
56
    code.value = static_cast<uint16_t>(sorted[0]);
105
1.84k
    for (key = 0; key < total_size; ++key) {
106
1.79k
      table[key] = code;
107
1.79k
    }
108
56
    return total_size;
109
56
  }
110
111
  /* fill in root table */
112
  /* let's reduce the table size to a smaller size if possible, and */
113
  /* create the repetitions by memcpy if possible in the coming loop */
114
406
  if (table_bits > max_length) {
115
285
    table_bits = max_length;
116
285
    table_size = 1u << table_bits;
117
285
  }
118
406
  key = 0;
119
406
  symbol = 0;
120
406
  code.bits = 1;
121
406
  step = 2;
122
1.70k
  do {
123
8.67k
    for (; count[code.bits] != 0; --count[code.bits]) {
124
6.96k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
125
6.96k
      ReplicateValue(&table[key], step, table_size, code);
126
6.96k
      key = GetNextKey(key, code.bits);
127
6.96k
    }
128
1.70k
    step <<= 1;
129
1.70k
  } while (++code.bits <= table_bits);
130
131
  /* if root_bits != table_bits we only created one fraction of the */
132
  /* table, and we need to replicate it now. */
133
1.25k
  while (total_size != table_size) {
134
845
    memcpy(&table[table_size], &table[0], table_size * sizeof(table[0]));
135
845
    table_size <<= 1;
136
845
  }
137
138
  /* fill in 2nd level tables and add pointers to root table */
139
406
  mask = total_size - 1;
140
406
  low = -1;
141
579
  for (len = root_bits + 1, step = 2; len <= max_length; ++len, step <<= 1) {
142
5.01k
    for (; count[len] != 0; --count[len]) {
143
4.83k
      if ((key & mask) != low) {
144
1.70k
        table += table_size;
145
1.70k
        table_bits = NextTableBitSize(count, len, root_bits);
146
1.70k
        table_size = 1u << table_bits;
147
1.70k
        total_size += table_size;
148
1.70k
        low = key & mask;
149
1.70k
        root_table[low].bits = static_cast<uint8_t>(table_bits + root_bits);
150
1.70k
        root_table[low].value =
151
1.70k
            static_cast<uint16_t>((table - root_table) - low);
152
1.70k
      }
153
4.83k
      code.bits = static_cast<uint8_t>(len - root_bits);
154
4.83k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
155
4.83k
      ReplicateValue(&table[key >> root_bits], step, table_size, code);
156
4.83k
      key = GetNextKey(key, len);
157
4.83k
    }
158
173
  }
159
160
406
  return total_size;
161
462
}
162
163
}  // namespace brunsli