| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#include <stdlib.h> |
| 100 |
#include <stdio.h> |
| 101 |
#include <string.h> |
| 102 |
|
| 103 |
#include "../include/libchdr/huffman.h" |
| 104 |
#include "../include/libchdr/macros.h" |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
#define MAKE_LOOKUP(code,bits) (((code) << 5) | ((bits) & 0x1f)) |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
struct huffman_decoder* create_huffman_decoder(int numcodes, int maxbits) |
| 125 |
{ |
| 126 |
struct huffman_decoder* decoder = NULL; |
| 127 |
|
| 128 |
|
| 129 |
if (maxbits > 24) |
| 130 |
return NULL; |
| 131 |
|
| 132 |
decoder = (struct huffman_decoder*)malloc(sizeof(struct huffman_decoder)); |
| 133 |
decoder->numcodes = numcodes; |
| 134 |
decoder->maxbits = maxbits; |
| 135 |
decoder->lookup = (lookup_value*)malloc(sizeof(lookup_value) * (1 << maxbits)); |
| 136 |
decoder->huffnode = (struct node_t*)malloc(sizeof(struct node_t) * numcodes); |
| 137 |
decoder->datahisto = NULL; |
| 138 |
decoder->prevdata = 0; |
| 139 |
decoder->rleremaining = 0; |
| 140 |
return decoder; |
| 141 |
} |
| 142 |
|
| 143 |
void delete_huffman_decoder(struct huffman_decoder* decoder) |
| 144 |
{ |
| 145 |
if (decoder != NULL) |
| 146 |
{ |
| 147 |
if (decoder->lookup != NULL) |
| 148 |
free(decoder->lookup); |
| 149 |
if (decoder->huffnode != NULL) |
| 150 |
free(decoder->huffnode); |
| 151 |
free(decoder); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
uint32_t huffman_decode_one(struct huffman_decoder* decoder, struct bitstream* bitbuf) |
| 162 |
{ |
| 163 |
|
| 164 |
uint32_t bits = bitstream_peek(bitbuf, decoder->maxbits); |
| 165 |
|
| 166 |
|
| 167 |
lookup_value lookup = decoder->lookup[bits]; |
| 168 |
bitstream_remove(bitbuf, lookup & 0x1f); |
| 169 |
|
| 170 |
|
| 171 |
return lookup >> 5; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
enum huffman_error huffman_import_tree_rle(struct huffman_decoder* decoder, struct bitstream* bitbuf) |
| 181 |
{ |
| 182 |
int numbits; |
| 183 |
uint32_t curnode; |
| 184 |
enum huffman_error error; |
| 185 |
|
| 186 |
|
| 187 |
if (decoder->maxbits >= 16) |
| 188 |
numbits = 5; |
| 189 |
else if (decoder->maxbits >= 8) |
| 190 |
numbits = 4; |
| 191 |
else |
| 192 |
numbits = 3; |
| 193 |
|
| 194 |
|
| 195 |
for (curnode = 0; curnode < decoder->numcodes; ) |
| 196 |
{ |
| 197 |
|
| 198 |
int nodebits = bitstream_read(bitbuf, numbits); |
| 199 |
if (nodebits != 1) |
| 200 |
decoder->huffnode[curnode++].numbits = nodebits; |
| 201 |
|
| 202 |
|
| 203 |
else |
| 204 |
{ |
| 205 |
|
| 206 |
nodebits = bitstream_read(bitbuf, numbits); |
| 207 |
if (nodebits == 1) |
| 208 |
decoder->huffnode[curnode++].numbits = nodebits; |
| 209 |
|
| 210 |
|
| 211 |
else |
| 212 |
{ |
| 213 |
int repcount = bitstream_read(bitbuf, numbits) + 3; |
| 214 |
if (repcount + curnode > decoder->numcodes) |
| 215 |
return HUFFERR_INVALID_DATA; |
| 216 |
while (repcount--) |
| 217 |
decoder->huffnode[curnode++].numbits = nodebits; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
if (curnode != decoder->numcodes) |
| 224 |
return HUFFERR_INVALID_DATA; |
| 225 |
|
| 226 |
|
| 227 |
error = huffman_assign_canonical_codes(decoder); |
| 228 |
if (error != HUFFERR_NONE) |
| 229 |
return error; |
| 230 |
|
| 231 |
|
| 232 |
error = huffman_build_lookup_table(decoder); |
| 233 |
if (error != HUFFERR_NONE) |
| 234 |
return error; |
| 235 |
|
| 236 |
|
| 237 |
return bitstream_overflow(bitbuf) ? HUFFERR_INPUT_BUFFER_TOO_SMALL : HUFFERR_NONE; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
enum huffman_error huffman_import_tree_huffman(struct huffman_decoder* decoder, struct bitstream* bitbuf) |
| 248 |
{ |
| 249 |
int start; |
| 250 |
int last = 0; |
| 251 |
int count = 0; |
| 252 |
int index; |
| 253 |
uint32_t curcode; |
| 254 |
uint8_t rlefullbits = 0; |
| 255 |
uint32_t temp; |
| 256 |
enum huffman_error error; |
| 257 |
|
| 258 |
struct huffman_decoder* smallhuff = create_huffman_decoder(24, 6); |
| 259 |
smallhuff->huffnode[0].numbits = bitstream_read(bitbuf, 3); |
| 260 |
start = bitstream_read(bitbuf, 3) + 1; |
| 261 |
for (index = 1; index < 24; index++) |
| 262 |
{ |
| 263 |
if (index < start || count == 7) |
| 264 |
smallhuff->huffnode[index].numbits = 0; |
| 265 |
else |
| 266 |
{ |
| 267 |
count = bitstream_read(bitbuf, 3); |
| 268 |
smallhuff->huffnode[index].numbits = (count == 7) ? 0 : count; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
error = huffman_assign_canonical_codes(smallhuff); |
| 274 |
if (error != HUFFERR_NONE) |
| 275 |
{ |
| 276 |
delete_huffman_decoder(smallhuff); |
| 277 |
return error; |
| 278 |
} |
| 279 |
error = huffman_build_lookup_table(smallhuff); |
| 280 |
if (error != HUFFERR_NONE) |
| 281 |
{ |
| 282 |
delete_huffman_decoder(smallhuff); |
| 283 |
return error; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
temp = decoder->numcodes - 9; |
| 288 |
while (temp != 0) |
| 289 |
temp >>= 1, rlefullbits++; |
| 290 |
|
| 291 |
|
| 292 |
for (curcode = 0; curcode < decoder->numcodes; ) |
| 293 |
{ |
| 294 |
int value = huffman_decode_one(smallhuff, bitbuf); |
| 295 |
if (value != 0) |
| 296 |
decoder->huffnode[curcode++].numbits = last = value - 1; |
| 297 |
else |
| 298 |
{ |
| 299 |
int count = bitstream_read(bitbuf, 3) + 2; |
| 300 |
if (count == 7+2) |
| 301 |
count += bitstream_read(bitbuf, rlefullbits); |
| 302 |
for ( ; count != 0 && curcode < decoder->numcodes; count--) |
| 303 |
decoder->huffnode[curcode++].numbits = last; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
delete_huffman_decoder(smallhuff); |
| 309 |
|
| 310 |
|
| 311 |
if (curcode != decoder->numcodes) |
| 312 |
return HUFFERR_INVALID_DATA; |
| 313 |
|
| 314 |
|
| 315 |
error = huffman_assign_canonical_codes(decoder); |
| 316 |
if (error != HUFFERR_NONE) |
| 317 |
return error; |
| 318 |
|
| 319 |
|
| 320 |
error = huffman_build_lookup_table(decoder); |
| 321 |
if (error != HUFFERR_NONE) |
| 322 |
return error; |
| 323 |
|
| 324 |
|
| 325 |
return bitstream_overflow(bitbuf) ? HUFFERR_INPUT_BUFFER_TOO_SMALL : HUFFERR_NONE; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
enum huffman_error huffman_compute_tree_from_histo(struct huffman_decoder* decoder) |
| 335 |
{ |
| 336 |
uint32_t i; |
| 337 |
uint32_t lowerweight; |
| 338 |
uint32_t upperweight; |
| 339 |
|
| 340 |
uint32_t sdatacount = 0; |
| 341 |
for (i = 0; i < decoder->numcodes; i++) |
| 342 |
sdatacount += decoder->datahisto[i]; |
| 343 |
|
| 344 |
|
| 345 |
lowerweight = 0; |
| 346 |
upperweight = sdatacount * 2; |
| 347 |
while (1) |
| 348 |
{ |
| 349 |
|
| 350 |
uint32_t curweight = (upperweight + lowerweight) / 2; |
| 351 |
int curmaxbits = huffman_build_tree(decoder, sdatacount, curweight); |
| 352 |
|
| 353 |
|
| 354 |
if (curmaxbits <= decoder->maxbits) |
| 355 |
{ |
| 356 |
lowerweight = curweight; |
| 357 |
|
| 358 |
|
| 359 |
if (curweight == sdatacount || (upperweight - lowerweight) <= 1) |
| 360 |
break; |
| 361 |
} |
| 362 |
else |
| 363 |
upperweight = curweight; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
return huffman_assign_canonical_codes(decoder); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
static int huffman_tree_node_compare(const void *item1, const void *item2) |
| 382 |
{ |
| 383 |
const struct node_t *node1 = *(const struct node_t **)item1; |
| 384 |
const struct node_t *node2 = *(const struct node_t **)item2; |
| 385 |
if (node2->weight != node1->weight) |
| 386 |
return node2->weight - node1->weight; |
| 387 |
#if 0 |
| 388 |
if (node2->bits - node1->bits == 0) |
| 389 |
fprintf(stderr, "identical node sort keys, should not happen!\n"); |
| 390 |
#endif |
| 391 |
return (int)node1->bits - (int)node2->bits; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint32_t totalweight) |
| 401 |
{ |
| 402 |
uint32_t curcode; |
| 403 |
int nextalloc; |
| 404 |
int listitems = 0; |
| 405 |
int maxbits = 0; |
| 406 |
|
| 407 |
struct node_t** list = (struct node_t**)malloc(sizeof(struct node_t*) * decoder->numcodes * 2); |
| 408 |
memset(decoder->huffnode, 0, decoder->numcodes * sizeof(decoder->huffnode[0])); |
| 409 |
for (curcode = 0; curcode < decoder->numcodes; curcode++) |
| 410 |
if (decoder->datahisto[curcode] != 0) |
| 411 |
{ |
| 412 |
list[listitems++] = &decoder->huffnode[curcode]; |
| 413 |
decoder->huffnode[curcode].count = decoder->datahisto[curcode]; |
| 414 |
decoder->huffnode[curcode].bits = curcode; |
| 415 |
|
| 416 |
|
| 417 |
decoder->huffnode[curcode].weight = ((uint64_t)decoder->datahisto[curcode]) * ((uint64_t)totalweight) / ((uint64_t)totaldata); |
| 418 |
if (decoder->huffnode[curcode].weight == 0) |
| 419 |
decoder->huffnode[curcode].weight = 1; |
| 420 |
} |
| 421 |
|
| 422 |
#if 0 |
| 423 |
fprintf(stderr, "Pre-sort:\n"); |
| 424 |
for (int i = 0; i < listitems; i++) { |
| 425 |
fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits); |
| 426 |
} |
| 427 |
#endif |
| 428 |
|
| 429 |
|
| 430 |
qsort(&list[0], listitems, sizeof(list[0]), huffman_tree_node_compare); |
| 431 |
|
| 432 |
#if 0 |
| 433 |
fprintf(stderr, "Post-sort:\n"); |
| 434 |
for (int i = 0; i < listitems; i++) { |
| 435 |
fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits); |
| 436 |
} |
| 437 |
fprintf(stderr, "===================\n"); |
| 438 |
#endif |
| 439 |
|
| 440 |
|
| 441 |
nextalloc = decoder->numcodes; |
| 442 |
while (listitems > 1) |
| 443 |
{ |
| 444 |
int curitem; |
| 445 |
|
| 446 |
struct node_t* node1 = &(*list[--listitems]); |
| 447 |
struct node_t* node0 = &(*list[--listitems]); |
| 448 |
|
| 449 |
|
| 450 |
struct node_t* newnode = &decoder->huffnode[nextalloc++]; |
| 451 |
newnode->parent = NULL; |
| 452 |
node0->parent = node1->parent = newnode; |
| 453 |
newnode->weight = node0->weight + node1->weight; |
| 454 |
|
| 455 |
|
| 456 |
for (curitem = 0; curitem < listitems; curitem++) |
| 457 |
if (newnode->weight > list[curitem]->weight) |
| 458 |
{ |
| 459 |
memmove(&list[curitem+1], &list[curitem], (listitems - curitem) * sizeof(list[0])); |
| 460 |
break; |
| 461 |
} |
| 462 |
list[curitem] = newnode; |
| 463 |
listitems++; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
for (curcode = 0; curcode < decoder->numcodes; curcode++) |
| 468 |
{ |
| 469 |
struct node_t *curnode; |
| 470 |
struct node_t* node = &decoder->huffnode[curcode]; |
| 471 |
node->numbits = 0; |
| 472 |
node->bits = 0; |
| 473 |
|
| 474 |
|
| 475 |
if (node->weight > 0) |
| 476 |
{ |
| 477 |
|
| 478 |
for (curnode = node; curnode->parent != NULL; curnode = curnode->parent) |
| 479 |
node->numbits++; |
| 480 |
if (node->numbits == 0) |
| 481 |
node->numbits = 1; |
| 482 |
|
| 483 |
|
| 484 |
maxbits = MAX(maxbits, ((int)node->numbits)); |
| 485 |
} |
| 486 |
} |
| 487 |
return maxbits; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
enum huffman_error huffman_assign_canonical_codes(struct huffman_decoder* decoder) |
| 498 |
{ |
| 499 |
uint32_t curcode; |
| 500 |
int codelen; |
| 501 |
uint32_t curstart = 0; |
| 502 |
|
| 503 |
uint32_t bithisto[33] = { 0 }; |
| 504 |
for (curcode = 0; curcode < decoder->numcodes; curcode++) |
| 505 |
{ |
| 506 |
struct node_t* node = &decoder->huffnode[curcode]; |
| 507 |
if (node->numbits > decoder->maxbits) |
| 508 |
return HUFFERR_INTERNAL_INCONSISTENCY; |
| 509 |
if (node->numbits <= 32) |
| 510 |
bithisto[node->numbits]++; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
for (codelen = 32; codelen > 0; codelen--) |
| 515 |
{ |
| 516 |
uint32_t nextstart = (curstart + bithisto[codelen]) >> 1; |
| 517 |
if (codelen != 1 && nextstart * 2 != (curstart + bithisto[codelen])) |
| 518 |
return HUFFERR_INTERNAL_INCONSISTENCY; |
| 519 |
bithisto[codelen] = curstart; |
| 520 |
curstart = nextstart; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
for (curcode = 0; curcode < decoder->numcodes; curcode++) |
| 525 |
{ |
| 526 |
struct node_t* node = &decoder->huffnode[curcode]; |
| 527 |
if (node->numbits > 0) |
| 528 |
node->bits = bithisto[node->numbits]++; |
| 529 |
} |
| 530 |
return HUFFERR_NONE; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
enum huffman_error huffman_build_lookup_table(struct huffman_decoder* decoder) |
| 540 |
{ |
| 541 |
const lookup_value* lookupend = &decoder->lookup[(1u << decoder->maxbits)]; |
| 542 |
uint32_t curcode; |
| 543 |
|
| 544 |
for (curcode = 0; curcode < decoder->numcodes; curcode++) |
| 545 |
{ |
| 546 |
|
| 547 |
struct node_t* node = &decoder->huffnode[curcode]; |
| 548 |
if (node->numbits > 0) |
| 549 |
{ |
| 550 |
int shift; |
| 551 |
lookup_value *dest; |
| 552 |
lookup_value *destend; |
| 553 |
|
| 554 |
|
| 555 |
lookup_value value = MAKE_LOOKUP(curcode, node->numbits); |
| 556 |
|
| 557 |
|
| 558 |
shift = decoder->maxbits - node->numbits; |
| 559 |
dest = &decoder->lookup[node->bits << shift]; |
| 560 |
destend = &decoder->lookup[((node->bits + 1) << shift) - 1]; |
| 561 |
if (dest >= lookupend || destend >= lookupend || destend < dest) |
| 562 |
return HUFFERR_INTERNAL_INCONSISTENCY; |
| 563 |
while (dest <= destend) |
| 564 |
*dest++ = value; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
return HUFFERR_NONE; |
| 569 |
} |