Coverage Report

Created: 2025-06-13 06:48

/src/libwebp/src/utils/huffman_utils.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2012 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Utilities for building and looking up Huffman trees.
11
//
12
// Author: Urvang Joshi (urvang@google.com)
13
14
#include <assert.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "src/utils/huffman_utils.h"
19
#include "src/utils/utils.h"
20
#include "src/webp/format_constants.h"
21
#include "src/webp/types.h"
22
23
// Huffman data read via DecodeImageStream is represented in two (red and green)
24
// bytes.
25
#define MAX_HTREE_GROUPS    0x10000
26
27
0
HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups) {
28
0
  HTreeGroup* const htree_groups =
29
0
      (HTreeGroup*)WebPSafeMalloc(num_htree_groups, sizeof(*htree_groups));
30
0
  if (htree_groups == NULL) {
31
0
    return NULL;
32
0
  }
33
0
  assert(num_htree_groups <= MAX_HTREE_GROUPS);
34
0
  return htree_groups;
35
0
}
36
37
0
void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups) {
38
0
  if (htree_groups != NULL) {
39
0
    WebPSafeFree(htree_groups);
40
0
  }
41
0
}
42
43
// Returns reverse(reverse(key, len) + 1, len), where reverse(key, len) is the
44
// bit-wise reversal of the len least significant bits of key.
45
0
static WEBP_INLINE uint32_t GetNextKey(uint32_t key, int len) {
46
0
  uint32_t step = 1 << (len - 1);
47
0
  while (key & step) {
48
0
    step >>= 1;
49
0
  }
50
0
  return step ? (key & (step - 1)) + step : key;
51
0
}
52
53
// Stores code in table[0], table[step], table[2*step], ..., table[end].
54
// Assumes that end is an integer multiple of step.
55
static WEBP_INLINE void ReplicateValue(HuffmanCode* table,
56
                                       int step, int end,
57
0
                                       HuffmanCode code) {
58
0
  assert(end % step == 0);
59
0
  do {
60
0
    end -= step;
61
0
    table[end] = code;
62
0
  } while (end > 0);
63
0
}
64
65
// Returns the table width of the next 2nd level table. count is the histogram
66
// of bit lengths for the remaining symbols, len is the code length of the next
67
// processed symbol
68
static WEBP_INLINE int NextTableBitSize(const int* const count,
69
0
                                        int len, int root_bits) {
70
0
  int left = 1 << (len - root_bits);
71
0
  while (len < MAX_ALLOWED_CODE_LENGTH) {
72
0
    left -= count[len];
73
0
    if (left <= 0) break;
74
0
    ++len;
75
0
    left <<= 1;
76
0
  }
77
0
  return len - root_bits;
78
0
}
79
80
// sorted[code_lengths_size] is a pre-allocated array for sorting symbols
81
// by code length.
82
static int BuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
83
                             const int code_lengths[], int code_lengths_size,
84
0
                             uint16_t sorted[]) {
85
0
  HuffmanCode* table = root_table;  // next available space in table
86
0
  int total_size = 1 << root_bits;  // total size root table + 2nd level table
87
0
  int len;                          // current code length
88
0
  int symbol;                       // symbol index in original or sorted table
89
  // number of codes of each length:
90
0
  int count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
91
  // offsets in sorted table for each length:
92
0
  int offset[MAX_ALLOWED_CODE_LENGTH + 1];
93
94
0
  assert(code_lengths_size != 0);
95
0
  assert(code_lengths != NULL);
96
0
  assert((root_table != NULL && sorted != NULL) ||
97
0
         (root_table == NULL && sorted == NULL));
98
0
  assert(root_bits > 0);
99
100
  // Build histogram of code lengths.
101
0
  for (symbol = 0; symbol < code_lengths_size; ++symbol) {
102
0
    if (code_lengths[symbol] > MAX_ALLOWED_CODE_LENGTH) {
103
0
      return 0;
104
0
    }
105
0
    ++count[code_lengths[symbol]];
106
0
  }
107
108
  // Error, all code lengths are zeros.
109
0
  if (count[0] == code_lengths_size) {
110
0
    return 0;
111
0
  }
112
113
  // Generate offsets into sorted symbol table by code length.
114
0
  offset[1] = 0;
115
0
  for (len = 1; len < MAX_ALLOWED_CODE_LENGTH; ++len) {
116
0
    if (count[len] > (1 << len)) {
117
0
      return 0;
118
0
    }
119
0
    offset[len + 1] = offset[len] + count[len];
120
0
  }
121
122
  // Sort symbols by length, by symbol order within each length.
123
0
  for (symbol = 0; symbol < code_lengths_size; ++symbol) {
124
0
    const int symbol_code_length = code_lengths[symbol];
125
0
    if (code_lengths[symbol] > 0) {
126
0
      if (sorted != NULL) {
127
0
        if(offset[symbol_code_length] >= code_lengths_size) {
128
0
            return 0;
129
0
        }
130
0
        sorted[offset[symbol_code_length]++] = symbol;
131
0
      } else {
132
0
        offset[symbol_code_length]++;
133
0
      }
134
0
    }
135
0
  }
136
137
  // Special case code with only one value.
138
0
  if (offset[MAX_ALLOWED_CODE_LENGTH] == 1) {
139
0
    if (sorted != NULL) {
140
0
      HuffmanCode code;
141
0
      code.bits = 0;
142
0
      code.value = (uint16_t)sorted[0];
143
0
      ReplicateValue(table, 1, total_size, code);
144
0
    }
145
0
    return total_size;
146
0
  }
147
148
0
  {
149
0
    int step;              // step size to replicate values in current table
150
0
    uint32_t low = 0xffffffffu;        // low bits for current root entry
151
0
    uint32_t mask = total_size - 1;    // mask for low bits
152
0
    uint32_t key = 0;      // reversed prefix code
153
0
    int num_nodes = 1;     // number of Huffman tree nodes
154
0
    int num_open = 1;      // number of open branches in current tree level
155
0
    int table_bits = root_bits;        // key length of current table
156
0
    int table_size = 1 << table_bits;  // size of current table
157
0
    symbol = 0;
158
    // Fill in root table.
159
0
    for (len = 1, step = 2; len <= root_bits; ++len, step <<= 1) {
160
0
      num_open <<= 1;
161
0
      num_nodes += num_open;
162
0
      num_open -= count[len];
163
0
      if (num_open < 0) {
164
0
        return 0;
165
0
      }
166
0
      if (root_table == NULL) continue;
167
0
      for (; count[len] > 0; --count[len]) {
168
0
        HuffmanCode code;
169
0
        code.bits = (uint8_t)len;
170
0
        code.value = (uint16_t)sorted[symbol++];
171
0
        ReplicateValue(&table[key], step, table_size, code);
172
0
        key = GetNextKey(key, len);
173
0
      }
174
0
    }
175
176
    // Fill in 2nd level tables and add pointers to root table.
177
0
    for (len = root_bits + 1, step = 2; len <= MAX_ALLOWED_CODE_LENGTH;
178
0
         ++len, step <<= 1) {
179
0
      num_open <<= 1;
180
0
      num_nodes += num_open;
181
0
      num_open -= count[len];
182
0
      if (num_open < 0) {
183
0
        return 0;
184
0
      }
185
0
      for (; count[len] > 0; --count[len]) {
186
0
        HuffmanCode code;
187
0
        if ((key & mask) != low) {
188
0
          if (root_table != NULL) table += table_size;
189
0
          table_bits = NextTableBitSize(count, len, root_bits);
190
0
          table_size = 1 << table_bits;
191
0
          total_size += table_size;
192
0
          low = key & mask;
193
0
          if (root_table != NULL) {
194
0
            root_table[low].bits = (uint8_t)(table_bits + root_bits);
195
0
            root_table[low].value = (uint16_t)((table - root_table) - low);
196
0
          }
197
0
        }
198
0
        if (root_table != NULL) {
199
0
          code.bits = (uint8_t)(len - root_bits);
200
0
          code.value = (uint16_t)sorted[symbol++];
201
0
          ReplicateValue(&table[key >> root_bits], step, table_size, code);
202
0
        }
203
0
        key = GetNextKey(key, len);
204
0
      }
205
0
    }
206
207
    // Check if tree is full.
208
0
    if (num_nodes != 2 * offset[MAX_ALLOWED_CODE_LENGTH] - 1) {
209
0
      return 0;
210
0
    }
211
0
  }
212
213
0
  return total_size;
214
0
}
215
216
// Maximum code_lengths_size is 2328 (reached for 11-bit color_cache_bits).
217
// More commonly, the value is around ~280.
218
#define MAX_CODE_LENGTHS_SIZE \
219
  ((1 << MAX_CACHE_BITS) + NUM_LITERAL_CODES + NUM_LENGTH_CODES)
220
// Cut-off value for switching between heap and stack allocation.
221
0
#define SORTED_SIZE_CUTOFF 512
222
int VP8LBuildHuffmanTable(HuffmanTables* const root_table, int root_bits,
223
0
                          const int code_lengths[], int code_lengths_size) {
224
0
  const int total_size =
225
0
      BuildHuffmanTable(NULL, root_bits, code_lengths, code_lengths_size, NULL);
226
0
  assert(code_lengths_size <= MAX_CODE_LENGTHS_SIZE);
227
0
  if (total_size == 0 || root_table == NULL) return total_size;
228
229
0
  if (root_table->curr_segment->curr_table + total_size >=
230
0
      root_table->curr_segment->start + root_table->curr_segment->size) {
231
    // If 'root_table' does not have enough memory, allocate a new segment.
232
    // The available part of root_table->curr_segment is left unused because we
233
    // need a contiguous buffer.
234
0
    const int segment_size = root_table->curr_segment->size;
235
0
    struct HuffmanTablesSegment* next =
236
0
        (HuffmanTablesSegment*)WebPSafeMalloc(1, sizeof(*next));
237
0
    if (next == NULL) return 0;
238
    // Fill the new segment.
239
    // We need at least 'total_size' but if that value is small, it is better to
240
    // allocate a big chunk to prevent more allocations later. 'segment_size' is
241
    // therefore chosen (any other arbitrary value could be chosen).
242
0
    next->size = total_size > segment_size ? total_size : segment_size;
243
0
    next->start =
244
0
        (HuffmanCode*)WebPSafeMalloc(next->size, sizeof(*next->start));
245
0
    if (next->start == NULL) {
246
0
      WebPSafeFree(next);
247
0
      return 0;
248
0
    }
249
0
    next->curr_table = next->start;
250
0
    next->next = NULL;
251
    // Point to the new segment.
252
0
    root_table->curr_segment->next = next;
253
0
    root_table->curr_segment = next;
254
0
  }
255
0
  if (code_lengths_size <= SORTED_SIZE_CUTOFF) {
256
    // use local stack-allocated array.
257
0
    uint16_t sorted[SORTED_SIZE_CUTOFF];
258
0
    BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
259
0
                      code_lengths, code_lengths_size, sorted);
260
0
  } else {  // rare case. Use heap allocation.
261
0
    uint16_t* const sorted =
262
0
        (uint16_t*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted));
263
0
    if (sorted == NULL) return 0;
264
0
    BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
265
0
                      code_lengths, code_lengths_size, sorted);
266
0
    WebPSafeFree(sorted);
267
0
  }
268
0
  return total_size;
269
0
}
270
271
0
int VP8LHuffmanTablesAllocate(int size, HuffmanTables* huffman_tables) {
272
  // Have 'segment' point to the first segment for now, 'root'.
273
0
  HuffmanTablesSegment* const root = &huffman_tables->root;
274
0
  huffman_tables->curr_segment = root;
275
0
  root->next = NULL;
276
  // Allocate root.
277
0
  root->start = (HuffmanCode*)WebPSafeMalloc(size, sizeof(*root->start));
278
0
  if (root->start == NULL) return 0;
279
0
  root->curr_table = root->start;
280
0
  root->size = size;
281
0
  return 1;
282
0
}
283
284
0
void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables) {
285
0
  HuffmanTablesSegment *current, *next;
286
0
  if (huffman_tables == NULL) return;
287
  // Free the root node.
288
0
  current = &huffman_tables->root;
289
0
  next = current->next;
290
0
  WebPSafeFree(current->start);
291
0
  current->start = NULL;
292
0
  current->next = NULL;
293
0
  current = next;
294
  // Free the following nodes.
295
0
  while (current != NULL) {
296
0
    next = current->next;
297
0
    WebPSafeFree(current->start);
298
0
    WebPSafeFree(current);
299
0
    current = next;
300
0
  }
301
0
}