| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#include "src/utils/huffman_utils.h" |
| 18 |
#include "src/utils/utils.h" |
| 19 |
#include "src/webp/format_constants.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#define MAX_HTREE_GROUPS 0x10000 |
| 24 |
|
| 25 |
HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups) { |
| 26 |
HTreeGroup* const htree_groups = |
| 27 |
(HTreeGroup*)WebPSafeMalloc(num_htree_groups, sizeof(*htree_groups)); |
| 28 |
if (htree_groups == NULL) { |
| 29 |
return NULL; |
| 30 |
} |
| 31 |
assert(num_htree_groups <= MAX_HTREE_GROUPS); |
| 32 |
return htree_groups; |
| 33 |
} |
| 34 |
|
| 35 |
void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups) { |
| 36 |
if (htree_groups != NULL) { |
| 37 |
WebPSafeFree(htree_groups); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static WEBP_INLINE uint32_t GetNextKey(uint32_t key, int len) { |
| 44 |
uint32_t step = 1 << (len - 1); |
| 45 |
while (key & step) { |
| 46 |
step >>= 1; |
| 47 |
} |
| 48 |
return step ? (key & (step - 1)) + step : key; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static WEBP_INLINE void ReplicateValue(HuffmanCode* table, |
| 54 |
int step, int end, |
| 55 |
HuffmanCode code) { |
| 56 |
assert(end % step == 0); |
| 57 |
do { |
| 58 |
end -= step; |
| 59 |
table[end] = code; |
| 60 |
} while (end > 0); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static WEBP_INLINE int NextTableBitSize(const int* const count, |
| 67 |
int len, int root_bits) { |
| 68 |
int left = 1 << (len - root_bits); |
| 69 |
while (len < MAX_ALLOWED_CODE_LENGTH) { |
| 70 |
left -= count[len]; |
| 71 |
if (left <= 0) break; |
| 72 |
++len; |
| 73 |
left <<= 1; |
| 74 |
} |
| 75 |
return len - root_bits; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static int BuildHuffmanTable(HuffmanCode* const root_table, int root_bits, |
| 81 |
const int code_lengths[], int code_lengths_size, |
| 82 |
uint16_t sorted[]) { |
| 83 |
HuffmanCode* table = root_table; |
| 84 |
int total_size = 1 << root_bits; |
| 85 |
int len; |
| 86 |
int symbol; |
| 87 |
|
| 88 |
int count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 }; |
| 89 |
|
| 90 |
int offset[MAX_ALLOWED_CODE_LENGTH + 1]; |
| 91 |
|
| 92 |
assert(code_lengths_size != 0); |
| 93 |
assert(code_lengths != NULL); |
| 94 |
assert((root_table != NULL && sorted != NULL) || |
| 95 |
(root_table == NULL && sorted == NULL)); |
| 96 |
assert(root_bits > 0); |
| 97 |
|
| 98 |
|
| 99 |
for (symbol = 0; symbol < code_lengths_size; ++symbol) { |
| 100 |
if (code_lengths[symbol] > MAX_ALLOWED_CODE_LENGTH) { |
| 101 |
return 0; |
| 102 |
} |
| 103 |
++count[code_lengths[symbol]]; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
if (count[0] == code_lengths_size) { |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
offset[1] = 0; |
| 113 |
for (len = 1; len < MAX_ALLOWED_CODE_LENGTH; ++len) { |
| 114 |
if (count[len] > (1 << len)) { |
| 115 |
return 0; |
| 116 |
} |
| 117 |
offset[len + 1] = offset[len] + count[len]; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
for (symbol = 0; symbol < code_lengths_size; ++symbol) { |
| 122 |
const int symbol_code_length = code_lengths[symbol]; |
| 123 |
if (code_lengths[symbol] > 0) { |
| 124 |
if (sorted != NULL) { |
| 125 |
if(offset[symbol_code_length] >= code_lengths_size) { |
| 126 |
return 0; |
| 127 |
} |
| 128 |
sorted[offset[symbol_code_length]++] = symbol; |
| 129 |
} else { |
| 130 |
offset[symbol_code_length]++; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
if (offset[MAX_ALLOWED_CODE_LENGTH] == 1) { |
| 137 |
if (sorted != NULL) { |
| 138 |
HuffmanCode code; |
| 139 |
code.bits = 0; |
| 140 |
code.value = (uint16_t)sorted[0]; |
| 141 |
ReplicateValue(table, 1, total_size, code); |
| 142 |
} |
| 143 |
return total_size; |
| 144 |
} |
| 145 |
|
| 146 |
{ |
| 147 |
int step; |
| 148 |
uint32_t low = 0xffffffffu; |
| 149 |
uint32_t mask = total_size - 1; |
| 150 |
uint32_t key = 0; |
| 151 |
int num_nodes = 1; |
| 152 |
int num_open = 1; |
| 153 |
int table_bits = root_bits; |
| 154 |
int table_size = 1 << table_bits; |
| 155 |
symbol = 0; |
| 156 |
|
| 157 |
for (len = 1, step = 2; len <= root_bits; ++len, step <<= 1) { |
| 158 |
num_open <<= 1; |
| 159 |
num_nodes += num_open; |
| 160 |
num_open -= count[len]; |
| 161 |
if (num_open < 0) { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
if (root_table == NULL) continue; |
| 165 |
for (; count[len] > 0; --count[len]) { |
| 166 |
HuffmanCode code; |
| 167 |
code.bits = (uint8_t)len; |
| 168 |
code.value = (uint16_t)sorted[symbol++]; |
| 169 |
ReplicateValue(&table[key], step, table_size, code); |
| 170 |
key = GetNextKey(key, len); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
for (len = root_bits + 1, step = 2; len <= MAX_ALLOWED_CODE_LENGTH; |
| 176 |
++len, step <<= 1) { |
| 177 |
num_open <<= 1; |
| 178 |
num_nodes += num_open; |
| 179 |
num_open -= count[len]; |
| 180 |
if (num_open < 0) { |
| 181 |
return 0; |
| 182 |
} |
| 183 |
for (; count[len] > 0; --count[len]) { |
| 184 |
HuffmanCode code; |
| 185 |
if ((key & mask) != low) { |
| 186 |
if (root_table != NULL) table += table_size; |
| 187 |
table_bits = NextTableBitSize(count, len, root_bits); |
| 188 |
table_size = 1 << table_bits; |
| 189 |
total_size += table_size; |
| 190 |
low = key & mask; |
| 191 |
if (root_table != NULL) { |
| 192 |
root_table[low].bits = (uint8_t)(table_bits + root_bits); |
| 193 |
root_table[low].value = (uint16_t)((table - root_table) - low); |
| 194 |
} |
| 195 |
} |
| 196 |
if (root_table != NULL) { |
| 197 |
code.bits = (uint8_t)(len - root_bits); |
| 198 |
code.value = (uint16_t)sorted[symbol++]; |
| 199 |
ReplicateValue(&table[key >> root_bits], step, table_size, code); |
| 200 |
} |
| 201 |
key = GetNextKey(key, len); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
if (num_nodes != 2 * offset[MAX_ALLOWED_CODE_LENGTH] - 1) { |
| 207 |
return 0; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return total_size; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
#define MAX_CODE_LENGTHS_SIZE \ |
| 217 |
((1 << MAX_CACHE_BITS) + NUM_LITERAL_CODES + NUM_LENGTH_CODES) |
| 218 |
|
| 219 |
#define SORTED_SIZE_CUTOFF 512 |
| 220 |
int VP8LBuildHuffmanTable(HuffmanTables* const root_table, int root_bits, |
| 221 |
const int code_lengths[], int code_lengths_size) { |
| 222 |
const int total_size = |
| 223 |
BuildHuffmanTable(NULL, root_bits, code_lengths, code_lengths_size, NULL); |
| 224 |
assert(code_lengths_size <= MAX_CODE_LENGTHS_SIZE); |
| 225 |
if (total_size == 0 || root_table == NULL) return total_size; |
| 226 |
|
| 227 |
if (root_table->curr_segment->curr_table + total_size >= |
| 228 |
root_table->curr_segment->start + root_table->curr_segment->size) { |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
const int segment_size = root_table->curr_segment->size; |
| 233 |
struct HuffmanTablesSegment* next = |
| 234 |
(HuffmanTablesSegment*)WebPSafeMalloc(1, sizeof(*next)); |
| 235 |
if (next == NULL) return 0; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
next->size = total_size > segment_size ? total_size : segment_size; |
| 241 |
next->start = |
| 242 |
(HuffmanCode*)WebPSafeMalloc(next->size, sizeof(*next->start)); |
| 243 |
if (next->start == NULL) { |
| 244 |
WebPSafeFree(next); |
| 245 |
return 0; |
| 246 |
} |
| 247 |
next->curr_table = next->start; |
| 248 |
next->next = NULL; |
| 249 |
|
| 250 |
root_table->curr_segment->next = next; |
| 251 |
root_table->curr_segment = next; |
| 252 |
} |
| 253 |
if (code_lengths_size <= SORTED_SIZE_CUTOFF) { |
| 254 |
|
| 255 |
uint16_t sorted[SORTED_SIZE_CUTOFF]; |
| 256 |
BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits, |
| 257 |
code_lengths, code_lengths_size, sorted); |
| 258 |
} else { |
| 259 |
uint16_t* const sorted = |
| 260 |
(uint16_t*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted)); |
| 261 |
if (sorted == NULL) return 0; |
| 262 |
BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits, |
| 263 |
code_lengths, code_lengths_size, sorted); |
| 264 |
WebPSafeFree(sorted); |
| 265 |
} |
| 266 |
return total_size; |
| 267 |
} |
| 268 |
|
| 269 |
int VP8LHuffmanTablesAllocate(int size, HuffmanTables* huffman_tables) { |
| 270 |
|
| 271 |
HuffmanTablesSegment* const root = &huffman_tables->root; |
| 272 |
huffman_tables->curr_segment = root; |
| 273 |
root->next = NULL; |
| 274 |
|
| 275 |
root->start = (HuffmanCode*)WebPSafeMalloc(size, sizeof(*root->start)); |
| 276 |
if (root->start == NULL) return 0; |
| 277 |
root->curr_table = root->start; |
| 278 |
root->size = size; |
| 279 |
return 1; |
| 280 |
} |
| 281 |
|
| 282 |
void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables) { |
| 283 |
HuffmanTablesSegment *current, *next; |
| 284 |
if (huffman_tables == NULL) return; |
| 285 |
|
| 286 |
current = &huffman_tables->root; |
| 287 |
next = current->next; |
| 288 |
WebPSafeFree(current->start); |
| 289 |
current->start = NULL; |
| 290 |
current->next = NULL; |
| 291 |
current = next; |
| 292 |
|
| 293 |
while (current != NULL) { |
| 294 |
next = current->next; |
| 295 |
WebPSafeFree(current->start); |
| 296 |
WebPSafeFree(current); |
| 297 |
current = next; |
| 298 |
} |
| 299 |
} |