Coverage Report

Created: 2026-01-17 06:45

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