Coverage Report

Created: 2026-05-12 06:13

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
7.44k
static inline int GetNextKey(int key, size_t len) {
21
7.44k
  int step = 1u << (len - 1);
22
14.6k
  while (key & step) {
23
7.16k
    step >>= 1;
24
7.16k
  }
25
7.44k
  return (key & (step - 1)) + step;
26
7.44k
}
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
7.44k
                                  HuffmanCode code) {
32
16.4k
  do {
33
16.4k
    end -= step;
34
16.4k
    table[end] = code;
35
16.4k
  } while (end > 0);
36
7.44k
}
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.41k
                                      size_t root_bits) {
43
1.41k
  size_t left = size_t(1) << (len - root_bits);
44
1.46k
  while (len < kMaxHuffmanBits) {
45
1.45k
    if (left <= count[len]) break;
46
45
    left -= count[len];
47
45
    ++len;
48
45
    left <<= 1;
49
45
  }
50
1.41k
  return len - root_bits;
51
1.41k
}
52
53
uint32_t BuildHuffmanTable(HuffmanCode* root_table, size_t root_bits,
54
                           const uint8_t* const code_lengths,
55
313
                           size_t code_lengths_size, uint16_t* count) {
56
313
  HuffmanCode code;    /* current table entry */
57
313
  HuffmanCode* table;  /* next available space in table */
58
313
  size_t len;          /* current code length */
59
313
  size_t symbol;       /* symbol index in original or sorted table */
60
313
  int key;             /* reversed prefix code */
61
313
  int step;            /* step size to replicate values in current table */
62
313
  int low;             /* low bits for current root entry */
63
313
  int mask;            /* mask for low bits */
64
313
  size_t table_bits;   /* key length of current table */
65
313
  int table_size;      /* size of current table */
66
313
  int total_size;      /* sum of root table size and 2nd level table sizes */
67
  /* offsets in sorted table for each length */
68
313
  uint16_t offset[kMaxHuffmanBits + 1];
69
313
  size_t max_length = 1;
70
71
313
  if (code_lengths_size > 1u << kMaxHuffmanBits) return 0;
72
73
  /* symbols sorted by code length */
74
313
  std::vector<uint16_t> sorted_storage(code_lengths_size);
75
313
  uint16_t* sorted = sorted_storage.data();
76
77
  /* generate offsets into sorted symbol table by code length */
78
313
  {
79
313
    uint16_t sum = 0;
80
5.00k
    for (len = 1; len <= kMaxHuffmanBits; len++) {
81
4.69k
      offset[len] = sum;
82
4.69k
      if (count[len]) {
83
834
        sum = static_cast<uint16_t>(sum + count[len]);
84
834
        max_length = len;
85
834
      }
86
4.69k
    }
87
313
  }
88
89
  /* sort symbols by length, by symbol order within each length */
90
16.3k
  for (symbol = 0; symbol < code_lengths_size; symbol++) {
91
16.0k
    if (code_lengths[symbol] != 0) {
92
7.47k
      sorted[offset[code_lengths[symbol]]++] = static_cast<uint16_t>(symbol);
93
7.47k
    }
94
16.0k
  }
95
96
313
  table = root_table;
97
313
  table_bits = root_bits;
98
313
  table_size = 1u << table_bits;
99
313
  total_size = table_size;
100
101
  /* special case code with only one value */
102
313
  if (offset[kMaxHuffmanBits] == 1) {
103
30
    code.bits = 0;
104
30
    code.value = static_cast<uint16_t>(sorted[0]);
105
990
    for (key = 0; key < total_size; ++key) {
106
960
      table[key] = code;
107
960
    }
108
30
    return total_size;
109
30
  }
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
283
  if (table_bits > max_length) {
115
210
    table_bits = max_length;
116
210
    table_size = 1u << table_bits;
117
210
  }
118
283
  key = 0;
119
283
  symbol = 0;
120
283
  code.bits = 1;
121
283
  step = 2;
122
1.13k
  do {
123
4.96k
    for (; count[code.bits] != 0; --count[code.bits]) {
124
3.83k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
125
3.83k
      ReplicateValue(&table[key], step, table_size, code);
126
3.83k
      key = GetNextKey(key, code.bits);
127
3.83k
    }
128
1.13k
    step <<= 1;
129
1.13k
  } 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
894
  while (total_size != table_size) {
134
611
    memcpy(&table[table_size], &table[0], table_size * sizeof(table[0]));
135
611
    table_size <<= 1;
136
611
  }
137
138
  /* fill in 2nd level tables and add pointers to root table */
139
283
  mask = total_size - 1;
140
283
  low = -1;
141
390
  for (len = root_bits + 1, step = 2; len <= max_length; ++len, step <<= 1) {
142
3.72k
    for (; count[len] != 0; --count[len]) {
143
3.61k
      if ((key & mask) != low) {
144
1.41k
        table += table_size;
145
1.41k
        table_bits = NextTableBitSize(count, len, root_bits);
146
1.41k
        table_size = 1u << table_bits;
147
1.41k
        total_size += table_size;
148
1.41k
        low = key & mask;
149
1.41k
        root_table[low].bits = static_cast<uint8_t>(table_bits + root_bits);
150
1.41k
        root_table[low].value =
151
1.41k
            static_cast<uint16_t>((table - root_table) - low);
152
1.41k
      }
153
3.61k
      code.bits = static_cast<uint8_t>(len - root_bits);
154
3.61k
      code.value = static_cast<uint16_t>(sorted[symbol++]);
155
3.61k
      ReplicateValue(&table[key >> root_bits], step, table_size, code);
156
3.61k
      key = GetNextKey(key, len);
157
3.61k
    }
158
107
  }
159
160
283
  return total_size;
161
313
}
162
163
}  // namespace brunsli