Coverage Report

Created: 2026-10-28 06:29

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 <brunsli/types.h>
10
11
#include <cstring> /* for memcpy */
12
#include <vector>
13
14
#include "../common/constants.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
22.6k
static inline int GetNextKey(int key, size_t len) {
21
22.6k
  int step = 1u << (len - 1);
22
44.1k
  while (key & step) {
23
21.4k
    step >>= 1;
24
21.4k
  }
25
22.6k
  return (key & (step - 1)) + step;
26
22.6k
}
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
22.6k
                                  HuffmanCode code) {
32
57.9k
  do {
33
57.9k
    end -= step;
34
57.9k
    table[end] = code;
35
57.9k
  } while (end > 0);
36
22.6k
}
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
2.62k
                                      size_t root_bits) {
43
2.62k
  size_t left = size_t(1) << (len - root_bits);
44
2.79k
  while (len < kMaxHuffmanBits) {
45
2.76k
    if (left <= count[len]) break;
46
171
    left -= count[len];
47
171
    ++len;
48
171
    left <<= 1;
49
171
  }
50
2.62k
  return len - root_bits;
51
2.62k
}
52
53
uint32_t BuildHuffmanTable(HuffmanCode* root_table, size_t root_bits,
54
                           const uint8_t* const code_lengths,
55
1.30k
                           size_t code_lengths_size, uint16_t* count) {
56
1.30k
  HuffmanCode code;   /* current table entry */
57
1.30k
  HuffmanCode* table; /* next available space in table */
58
1.30k
  size_t len;         /* current code length */
59
1.30k
  size_t symbol;      /* symbol index in original or sorted table */
60
1.30k
  int key;            /* reversed prefix code */
61
1.30k
  int step;           /* step size to replicate values in current table */
62
1.30k
  int low;            /* low bits for current root entry */
63
1.30k
  int mask;           /* mask for low bits */
64
1.30k
  size_t table_bits;  /* key length of current table */
65
1.30k
  int table_size;     /* size of current table */
66
1.30k
  int total_size;     /* sum of root table size and 2nd level table sizes */
67
  /* offsets in sorted table for each length */
68
1.30k
  uint16_t offset[kMaxHuffmanBits + 1];
69
1.30k
  size_t max_length = 1;
70
71
1.30k
  if (code_lengths_size > 1u << kMaxHuffmanBits) return 0;
72
73
  /* symbols sorted by code length */
74
1.30k
  std::vector<uint16_t> sorted_storage(code_lengths_size);
75
1.30k
  uint16_t* sorted = sorted_storage.data();
76
77
  /* generate offsets into sorted symbol table by code length */
78
1.30k
  {
79
1.30k
    uint16_t sum = 0;
80
20.8k
    for (len = 1; len <= kMaxHuffmanBits; len++) {
81
19.5k
      offset[len] = sum;
82
19.5k
      if (count[len]) {
83
3.34k
        sum = static_cast<uint16_t>(sum + count[len]);
84
3.34k
        max_length = len;
85
3.34k
      }
86
19.5k
    }
87
1.30k
  }
88
89
  /* sort symbols by length, by symbol order within each length */
90
56.9k
  for (symbol = 0; symbol < code_lengths_size; symbol++) {
91
55.6k
    if (code_lengths[symbol] != 0) {
92
22.7k
      sorted[offset[code_lengths[symbol]]++] = static_cast<uint16_t>(symbol);
93
22.7k
    }
94
55.6k
  }
95
96
1.30k
  table = root_table;
97
1.30k
  table_bits = root_bits;
98
1.30k
  table_size = 1u << table_bits;
99
1.30k
  total_size = table_size;
100
101
  /* special case code with only one value */
102
1.30k
  if (offset[kMaxHuffmanBits] == 1) {
103
102
    code.bits = 0;
104
102
    code.value = static_cast<uint16_t>(sorted[0]);
105
3.36k
    for (key = 0; key < total_size; ++key) {
106
3.26k
      table[key] = code;
107
3.26k
    }
108
102
    return total_size;
109
102
  }
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
1.20k
  if (table_bits > max_length) {
115
977
    table_bits = max_length;
116
977
    table_size = 1u << table_bits;
117
977
  }
118
1.20k
  key = 0;
119
1.20k
  symbol = 0;
120
1.20k
  code.bits = 1;
121
1.20k
  step = 2;
122
4.35k
  do {
123
18.0k
    for (; count[code.bits] != 0; --count[code.bits]) {
124
13.7k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
125
13.7k
      ReplicateValue(&table[key], step, table_size, code);
126
13.7k
      key = GetNextKey(key, code.bits);
127
13.7k
    }
128
4.35k
    step <<= 1;
129
4.35k
  } 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
4.35k
  while (total_size != table_size) {
134
3.15k
    memcpy(&table[table_size], &table[0], table_size * sizeof(table[0]));
135
3.15k
    table_size <<= 1;
136
3.15k
  }
137
138
  /* fill in 2nd level tables and add pointers to root table */
139
1.20k
  mask = total_size - 1;
140
1.20k
  low = -1;
141
1.60k
  for (len = root_bits + 1, step = 2; len <= max_length; ++len, step <<= 1) {
142
9.34k
    for (; count[len] != 0; --count[len]) {
143
8.93k
      if ((key & mask) != low) {
144
2.62k
        table += table_size;
145
2.62k
        table_bits = NextTableBitSize(count, len, root_bits);
146
2.62k
        table_size = 1u << table_bits;
147
2.62k
        total_size += table_size;
148
2.62k
        low = key & mask;
149
2.62k
        root_table[low].bits = static_cast<uint8_t>(table_bits + root_bits);
150
2.62k
        root_table[low].value =
151
2.62k
            static_cast<uint16_t>((table - root_table) - low);
152
2.62k
      }
153
8.93k
      code.bits = static_cast<uint8_t>(len - root_bits);
154
8.93k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
155
8.93k
      ReplicateValue(&table[key >> root_bits], step, table_size, code);
156
8.93k
      key = GetNextKey(key, len);
157
8.93k
    }
158
406
  }
159
160
1.20k
  return total_size;
161
1.30k
}
162
163
}  // namespace brunsli