| 1 | /* hzip.c */ |
| 2 | /* |
| 3 | | Routines designed to be used as a pilot experiment for compression |
| 4 | | of tablebases. Not really optimized, but they are supposed to work |
| 5 | | --Miguel A. Ballicora |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | This Software is distributed with the following X11 License, |
| 10 | sometimes also known as MIT license. |
| 11 | |
| 12 | Copyright (c) 2010 Miguel A. Ballicora |
| 13 | |
| 14 | Permission is hereby granted, free of charge, to any person |
| 15 | obtaining a copy of this software and associated documentation |
| 16 | files (the "Software"), to deal in the Software without |
| 17 | restriction, including without limitation the rights to use, |
| 18 | copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 19 | copies of the Software, and to permit persons to whom the |
| 20 | Software is furnished to do so, subject to the following |
| 21 | conditions: |
| 22 | |
| 23 | The above copyright notice and this permission notice shall be |
| 24 | included in all copies or substantial portions of the Software. |
| 25 | |
| 26 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 28 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 29 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 30 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 31 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 32 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 33 | OTHER DEALINGS IN THE SOFTWARE. |
| 34 | */ |
| 35 | |
| 36 | #include "hzip.h" |
| 37 | |
| 38 | /*-------------------------------------------------------------------*\ |
| 39 | | |
| 40 | | Huffman coding compression |
| 41 | | |
| 42 | \*-------------------------------------------------------------------*/ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
| 47 | #include <assert.h> |
| 48 | |
| 49 | #define MAXDIVERSITY (256) |
| 50 | #define MAXHEAP (MAXDIVERSITY+1) |
| 51 | #define MAXSTREAM (1<<18) |
| 52 | #define MAXHUFF (2*MAXDIVERSITY) |
| 53 | |
| 54 | typedef int bool_t; |
| 55 | |
| 56 | #define TRUE 1 |
| 57 | #define FALSE 0; |
| 58 | |
| 59 | /* huffman tree */ |
| 60 | struct huff { |
| 61 | int freq; |
| 62 | int value; |
| 63 | int pleft; |
| 64 | int pright; |
| 65 | bool_t isleaf; |
| 66 | }; |
| 67 | |
| 68 | static int huff_end; |
| 69 | static struct huff hufftree[MAXHUFF]; |
| 70 | |
| 71 | /* heap */ |
| 72 | struct element { |
| 73 | int freq; |
| 74 | int huffidx; |
| 75 | }; |
| 76 | |
| 77 | static int heap_end; |
| 78 | static struct element heap[MAXHEAP]; |
| 79 | |
| 80 | unsigned char streambuffer[MAXSTREAM]; |
| 81 | |
| 82 | /* stream */ |
| 83 | struct STREAM { |
| 84 | unsigned long pbit; |
| 85 | unsigned char *x; |
| 86 | }; |
| 87 | |
| 88 | typedef struct STREAM stream_t; |
| 89 | |
| 90 | /* read only */ |
| 91 | struct RO_STREAM { |
| 92 | unsigned long pbit; |
| 93 | const unsigned char *x; |
| 94 | }; |
| 95 | |
| 96 | typedef struct RO_STREAM ro_stream_t; |
| 97 | |
| 98 | /* |
| 99 | | |
| 100 | | VARIABLES |
| 101 | | |
| 102 | \*---------------------------*/ |
| 103 | |
| 104 | static int freq[MAXDIVERSITY]; |
| 105 | static unsigned code_table[MAXDIVERSITY]; |
| 106 | static unsigned size_table[MAXDIVERSITY]; |
| 107 | static stream_t Stream = {0, NULL}; |
| 108 | static ro_stream_t RO_Stream = {0, NULL}; |
| 109 | static const unsigned int VALUEBITS = 8u; |
| 110 | |
| 111 | /*==== PROTOTYPES======================================*/ |
| 112 | |
| 113 | |
| 114 | /* heap */ |
| 115 | |
| 116 | static void freq_init (const unsigned char *in, size_t max); |
| 117 | static void heap_init (void); |
| 118 | static void heap_append (struct element e); |
| 119 | static void heap_sift_up (int x); |
| 120 | static void heap_adjust_down (int top, int last); |
| 121 | |
| 122 | |
| 123 | /* hufftree */ |
| 124 | |
| 125 | static int hufftree_from_freq (void); |
| 126 | static int hufftree_from_heap (void); |
| 127 | static void hufftree_to_codes (int start, int n, unsigned code); |
| 128 | static void hufftree_reset (void); |
| 129 | static int hufftree_frombits (ro_stream_t *stream, bool_t *pok); |
| 130 | static void hufftree_tobits (int thisnode, stream_t *stream); |
| 131 | static unsigned int hufftree_readstream (int root, ro_stream_t *s); |
| 132 | |
| 133 | /* stream */ |
| 134 | |
| 135 | /* read only */ |
| 136 | static void ro_stream_rewind (ro_stream_t *s); |
| 137 | static void ro_stream_init (ro_stream_t *s, const unsigned char *buffer); |
| 138 | static void ro_stream_done (ro_stream_t *s); |
| 139 | |
| 140 | /* read and write */ |
| 141 | static void stream_clear (stream_t *s); |
| 142 | static void stream_init (stream_t *s, unsigned char *buffer); |
| 143 | static void stream_done (stream_t *s); |
| 144 | static size_t stream_len (stream_t *s); |
| 145 | |
| 146 | static void stream_rewind (stream_t *s); |
| 147 | static unsigned int stream_nextbit (ro_stream_t *s); |
| 148 | static unsigned int stream_nextbit_n (ro_stream_t *s, unsigned int width); |
| 149 | static void stream_writebit (stream_t *s, unsigned z); |
| 150 | static void stream_write_n (unsigned code, unsigned width, stream_t *s); |
| 151 | |
| 152 | |
| 153 | static bool_t decode_from_stream (ro_stream_t *stream, size_t n, unsigned char *out); |
| 154 | static void encode_to_stream (const unsigned char *in, size_t inlen, stream_t *stream); |
| 155 | |
| 156 | /*static unsigned int stream_next8 (stream_t *s);*/ |
| 157 | /*static void stream_write8 (stream_t *s, unsigned z);*/ |
| 158 | |
| 159 | /* supporting functions */ |
| 160 | |
| 161 | /* |
| 162 | static void heap_plot (void); |
| 163 | static int fill_block(unsigned char *out); |
| 164 | static char *binstream(unsigned int x, int n); |
| 165 | static void stream_print(stream_t *s, int n); |
| 166 | static void stream_printnext (stream_t *s, int n); |
| 167 | static void stream_dump (stream_t *s, int ori, int n); |
| 168 | static void freq_report (void); |
| 169 | */ |
| 170 | |
| 171 | /*=== ENCODE/DECODE=================================================*/ |
| 172 | |
| 173 | size_t TB_hzip_unused; |
| 174 | |
| 175 | static int |
| 176 | huffman_decode (size_t z, const unsigned char *bz, size_t n, unsigned char *bp) |
| 177 | /* bz:buffer huffman zipped to bp:buffer decoded */ |
| 178 | { |
| 179 | bool_t ok; |
| 180 | TB_hzip_unused = z; /* to silence compiler */ |
| 181 | ro_stream_init (&RO_Stream, bz); |
| 182 | ok = decode_from_stream (&RO_Stream, n, bp); |
| 183 | ro_stream_done (&RO_Stream); |
| 184 | return ok; |
| 185 | } |
| 186 | |
| 187 | static int |
| 188 | huffman_encode (size_t n, const unsigned char *bp, size_t *z, unsigned char *bz) |
| 189 | /* bz:buffer huffman zipped to bp:buffer decoded */ |
| 190 | { |
| 191 | size_t i, zz; |
| 192 | stream_init (&Stream, streambuffer); |
| 193 | encode_to_stream (bp, n, &Stream); |
| 194 | zz = stream_len (&Stream); |
| 195 | for (i = 0; i < zz; i++) { |
| 196 | bz[i] = Stream.x[i]; |
| 197 | } |
| 198 | *z = zz; |
| 199 | stream_done (&Stream); |
| 200 | return TRUE; |
| 201 | } |
| 202 | |
| 203 | extern int |
| 204 | huff_decode |
| 205 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) |
| 206 | { |
| 207 | size_t n = (size_t)in_start[0] |
| 208 | | ((size_t)in_start[1] << 8) |
| 209 | | ((size_t)in_start[2] << 16) |
| 210 | | ((size_t)in_start[3] << 24); |
| 211 | TB_hzip_unused = out_max; |
| 212 | *pout_len = n; |
| 213 | return huffman_decode (in_len-4, in_start+4, n, out_start); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | extern int |
| 218 | huff_encode |
| 219 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) |
| 220 | { |
| 221 | bool_t ok; |
| 222 | size_t hlen = 0; |
| 223 | TB_hzip_unused = out_max; |
| 224 | out_start[0] = (unsigned char) ((in_len ) & 0xffu); |
| 225 | out_start[1] = (unsigned char) ((in_len >> 8) & 0xffu); |
| 226 | out_start[2] = (unsigned char) ((in_len >> 16) & 0xffu); |
| 227 | out_start[3] = (unsigned char) ((in_len >> 24) & 0xffu); |
| 228 | ok = huffman_encode (in_len, in_start, &hlen, out_start+4); |
| 229 | *pout_len = hlen + 4; |
| 230 | return ok; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | static bool_t |
| 235 | decode_from_stream (ro_stream_t *s, size_t n, unsigned char *out) |
| 236 | { |
| 237 | int root; |
| 238 | bool_t ok = TRUE; /* default */ |
| 239 | |
| 240 | hufftree_reset (); |
| 241 | ro_stream_rewind (s); |
| 242 | root = hufftree_frombits (s, &ok); |
| 243 | |
| 244 | if (ok) { |
| 245 | while (n-->0) { |
| 246 | *out++ = (unsigned char) hufftree_readstream (root, s); /* cast to silence compiler */ |
| 247 | } |
| 248 | } |
| 249 | return ok; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static void |
| 254 | encode_to_stream (const unsigned char *in, size_t inlen, stream_t *stream) |
| 255 | { |
| 256 | size_t i; |
| 257 | unsigned x, c, s; |
| 258 | int root; |
| 259 | |
| 260 | stream_clear (&Stream); |
| 261 | stream_rewind(&Stream); |
| 262 | |
| 263 | /* pass to collect frequencies */ |
| 264 | freq_init (in, inlen); |
| 265 | |
| 266 | /* frequency --> heap --> hufftrees */ |
| 267 | root = hufftree_from_freq(); |
| 268 | |
| 269 | /* hufftree --> codes */ |
| 270 | hufftree_to_codes (root, 0, 0); |
| 271 | |
| 272 | /* hufftrees --> stored in bits (stream) */ |
| 273 | hufftree_tobits (root, stream) ; |
| 274 | |
| 275 | /* input + codes --> stored in bits (stream) */ |
| 276 | for (i = 0; i < inlen; i++) { |
| 277 | x = in[i]; |
| 278 | c = code_table[x]; |
| 279 | s = size_table[x]; |
| 280 | stream_write_n (c, s, stream); |
| 281 | } |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | /*=== STREAM =================================================*/ |
| 286 | /* |
| 287 | static char buffer[256]; |
| 288 | */ |
| 289 | /* |
| 290 | static char * |
| 291 | binstream(unsigned int x, int n) |
| 292 | { |
| 293 | char *s = buffer; |
| 294 | int i; |
| 295 | |
| 296 | for (i = 0; i < n; i++) { |
| 297 | if (0!=(x&(1<<i))) { |
| 298 | s[i] = '1'; |
| 299 | } else { |
| 300 | s[i] = '0'; |
| 301 | } |
| 302 | } |
| 303 | s[i] = '\0'; |
| 304 | return buffer; |
| 305 | } |
| 306 | */ |
| 307 | |
| 308 | /* READ ONLY */ |
| 309 | static void ro_stream_rewind (ro_stream_t *s) {s->pbit = 0; return;} |
| 310 | |
| 311 | static void |
| 312 | ro_stream_init (ro_stream_t *s, const unsigned char *buffer) |
| 313 | { |
| 314 | s->x = buffer; |
| 315 | s->pbit = 0; |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | static void |
| 320 | ro_stream_done (ro_stream_t *s) |
| 321 | { |
| 322 | s->x = NULL; |
| 323 | s->pbit = 0; |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* READ AND WRITE */ |
| 329 | static void stream_rewind (stream_t *s) {s->pbit = 0; return;} |
| 330 | |
| 331 | static void |
| 332 | stream_init (stream_t *s, unsigned char *buffer) |
| 333 | { |
| 334 | s->x = buffer; |
| 335 | s->pbit = 0; |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | stream_done (stream_t *s) |
| 341 | { |
| 342 | s->x = NULL; |
| 343 | s->pbit = 0; |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | static void |
| 348 | stream_clear (stream_t *s) |
| 349 | { int i; |
| 350 | for (i = 0; i < MAXSTREAM; i++) { |
| 351 | s->x[i] = 0; |
| 352 | } |
| 353 | s->pbit = 0; |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | static size_t |
| 358 | stream_len (stream_t *s) |
| 359 | { |
| 360 | return 1 + s->pbit/8; |
| 361 | } |
| 362 | |
| 363 | static unsigned int |
| 364 | stream_nextbit (ro_stream_t *s) |
| 365 | { |
| 366 | unsigned long y, byte, bit; |
| 367 | y = s->pbit++; |
| 368 | byte = y / 8; |
| 369 | bit = y & 7; |
| 370 | return 1u & (((unsigned)s->x[byte]) >> bit); |
| 371 | } |
| 372 | |
| 373 | static unsigned int |
| 374 | stream_nextbit_n (ro_stream_t *s, unsigned int width) |
| 375 | { |
| 376 | unsigned i; |
| 377 | unsigned x; |
| 378 | unsigned r = 0; |
| 379 | for (i = 0; i < width; i++) { |
| 380 | x = stream_nextbit (s); |
| 381 | r |= (x << i); |
| 382 | } |
| 383 | return r; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | static unsigned int |
| 388 | stream_next8 (stream_t *s) |
| 389 | { |
| 390 | unsigned a,b,y,byte,bit; |
| 391 | y = s->pbit; |
| 392 | s->pbit += 8; |
| 393 | byte = y / 8; |
| 394 | bit = y & 7; |
| 395 | a = 0xff & s->x[byte]; |
| 396 | b = 0xff & s->x[byte+1]; |
| 397 | return 0xff & ((a >> bit) | (b << (8-bit))); |
| 398 | } |
| 399 | */ |
| 400 | |
| 401 | #if 1 |
| 402 | static void |
| 403 | stream_writebit (stream_t *s, unsigned z) |
| 404 | { |
| 405 | unsigned long y,byte,bit; |
| 406 | y = s->pbit++; |
| 407 | |
| 408 | byte = y / 8; |
| 409 | bit = y & 7; |
| 410 | |
| 411 | /* s->x[byte] &= ~(1u << bit);*/ |
| 412 | s->x[byte] = (unsigned char) (s->x[byte] | ((z&1u) << bit)); /* cast to silence compiler */ |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | #else |
| 417 | static void |
| 418 | stream_writebit (stream_t *s, unsigned z) |
| 419 | { |
| 420 | /* This function will write the next bit, 0 or 1 depending on z, and will clear |
| 421 | | the following bits (when bit == 0) or some future bytes |
| 422 | | Do not use for writing after random access |
| 423 | | It is only useful when this function is use for sequential writing on a |
| 424 | | empty buffer. |
| 425 | */ |
| 426 | unsigned long y, byte, bit; |
| 427 | unsigned char *p; |
| 428 | y = s->pbit++; |
| 429 | |
| 430 | byte = y / 8; |
| 431 | bit = y & 7; |
| 432 | |
| 433 | p = &(s->x[byte]); |
| 434 | |
| 435 | /* hack to clear the byte only when bit == 0, otherwise, it clears future bytes |
| 436 | | This will avoid clearing the whole buffer beforehand or doing |
| 437 | | *p &= (unsigned char)(~(1u << bit)); |
| 438 | */ |
| 439 | p[bit] = 0; |
| 440 | |
| 441 | *p |= (unsigned char)(z&1u) << bit); |
| 442 | |
| 443 | return; |
| 444 | } |
| 445 | #endif |
| 446 | |
| 447 | |
| 448 | /* |
| 449 | static void |
| 450 | stream_write8 (stream_t *s, unsigned z) |
| 451 | { |
| 452 | unsigned a,b,c,y,byte,bit; |
| 453 | y = s->pbit; |
| 454 | s->pbit += 8; |
| 455 | byte = y / 8; |
| 456 | bit = y & 7; |
| 457 | a = 0xff & s->x[byte]; |
| 458 | b = 0xff & s->x[byte+1]; |
| 459 | c = a | (b << 8); |
| 460 | |
| 461 | c &= ~(0xff << bit); |
| 462 | c |= z << bit; |
| 463 | |
| 464 | s->x[byte] = c & 0xff; |
| 465 | s->x[byte+1] = 0xff & (c >> 8); |
| 466 | |
| 467 | return; |
| 468 | } |
| 469 | */ |
| 470 | |
| 471 | static void |
| 472 | stream_write_n (unsigned code, unsigned width, stream_t *s) |
| 473 | { |
| 474 | unsigned i; |
| 475 | for (i = 0; i < width; i++) { |
| 476 | stream_writebit (s, 1u & (code >> i)); |
| 477 | } |
| 478 | return; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | static void |
| 483 | stream_printnext (stream_t *s, int n) |
| 484 | { |
| 485 | int i; unsigned int x; |
| 486 | unsigned long int oldpos = s->pbit; |
| 487 | |
| 488 | printf("\n"); |
| 489 | for (i = 0; i < n; i++) { |
| 490 | if ((i & 7) == 0) |
| 491 | printf ("\n"); |
| 492 | x = stream_next8 (s); |
| 493 | printf ("%s ", binstream(x,8) ); |
| 494 | } |
| 495 | printf("\n"); |
| 496 | |
| 497 | s->pbit = oldpos; |
| 498 | return; |
| 499 | } |
| 500 | */ |
| 501 | |
| 502 | /* |
| 503 | static void |
| 504 | stream_dump (stream_t *s, int ori, int n) |
| 505 | { |
| 506 | int i; unsigned int x; |
| 507 | unsigned long int oldpos = s->pbit; |
| 508 | |
| 509 | s->pbit = ori; |
| 510 | |
| 511 | printf("\n"); |
| 512 | for (i = 0; i < n; i++) { |
| 513 | if ((i & 7) == 0) |
| 514 | printf ("\n"); |
| 515 | x = stream_next8 (s); |
| 516 | printf ("%s ", binstream(x,8) ); |
| 517 | } |
| 518 | printf("\n"); |
| 519 | |
| 520 | s->pbit = oldpos; |
| 521 | return; |
| 522 | } |
| 523 | */ |
| 524 | |
| 525 | /*=== HUFFTREE=================================================*/ |
| 526 | |
| 527 | #define LEFTCODE 0u |
| 528 | #define RIGHTCODE 1u |
| 529 | #define BITLEAF 1 |
| 530 | #define BITNODE 0 |
| 531 | |
| 532 | static void |
| 533 | hufftree_reset (void) |
| 534 | { |
| 535 | struct huff h; |
| 536 | int i; |
| 537 | for (i = 0; i < 2*MAXDIVERSITY; i++) { |
| 538 | h.isleaf = FALSE; |
| 539 | h.value = 0; |
| 540 | h.freq = 0; |
| 541 | h.pleft = 0; |
| 542 | h.pright = 0; |
| 543 | hufftree[i] = h; |
| 544 | } |
| 545 | huff_end = 0; |
| 546 | } |
| 547 | |
| 548 | static int |
| 549 | hufftree_from_heap (void) |
| 550 | { |
| 551 | int top, newidx /*, left, right, lesser */ ; |
| 552 | struct huff h; |
| 553 | |
| 554 | for (;;) |
| 555 | { |
| 556 | if (heap_end == 2) { /* at least top element */ |
| 557 | /* done */ |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | /* work at the top */ |
| 562 | top = 1; |
| 563 | /* |
| 564 | left = 2*top; |
| 565 | right = left + 1; |
| 566 | |
| 567 | lesser = left; |
| 568 | if (right < heap_end && (heap[right].freq < heap[left].freq)) |
| 569 | lesser = right; |
| 570 | */ |
| 571 | /* new huff node */ |
| 572 | newidx = huff_end++; |
| 573 | |
| 574 | h.isleaf = FALSE; |
| 575 | h.value = -1; |
| 576 | h.freq = heap[top].freq; /* will be incremented later when in 'combine' */ |
| 577 | h.pleft = heap[top].huffidx; |
| 578 | h.pright = -1; /* will be attached the next element */ |
| 579 | |
| 580 | |
| 581 | #ifdef TRACE |
| 582 | printf ("\n\nBefore Eliminate Top\n"); |
| 583 | heap_plot(); |
| 584 | #endif |
| 585 | |
| 586 | /* eliminate top */ |
| 587 | heap[top] = heap[--heap_end]; |
| 588 | |
| 589 | /* next 'lesser' element at 'top' */ |
| 590 | heap_adjust_down (1, heap_end-1); |
| 591 | |
| 592 | #ifdef TRACE |
| 593 | printf ("\n\nEliminate Top\n"); |
| 594 | heap_plot (); |
| 595 | #endif |
| 596 | |
| 597 | /* combine */ |
| 598 | h.pright = heap[1].huffidx; |
| 599 | h.freq += heap[1].freq; /* combine frequencies */ |
| 600 | hufftree[newidx] = h; |
| 601 | |
| 602 | heap[1].freq = h.freq; |
| 603 | heap[1].huffidx = newidx; |
| 604 | |
| 605 | /* adjust the combined elements */ |
| 606 | heap_adjust_down (1, heap_end-1); |
| 607 | |
| 608 | #ifdef TRACE |
| 609 | printf ("\n\nAfter Combine\n"); |
| 610 | heap_plot (); |
| 611 | #endif |
| 612 | |
| 613 | } |
| 614 | |
| 615 | return heap[1].huffidx; |
| 616 | } |
| 617 | |
| 618 | |
| 619 | static void |
| 620 | hufftree_to_codes (int start, int n, unsigned code) |
| 621 | { |
| 622 | int x, m; |
| 623 | unsigned c; |
| 624 | int value; |
| 625 | |
| 626 | #ifdef TRACK |
| 627 | if (n == 0) |
| 628 | printf ("\nHufftree to codes\n"); |
| 629 | #endif |
| 630 | |
| 631 | assert (n >= 0); |
| 632 | |
| 633 | x = hufftree[start].pleft; |
| 634 | c = code | (LEFTCODE << n); |
| 635 | m = n + 1; |
| 636 | |
| 637 | /* LEFT */ |
| 638 | if (hufftree[x].isleaf) { |
| 639 | value = hufftree[x].value; |
| 640 | code_table[value] = c; |
| 641 | size_table[value] = (unsigned)m; |
| 642 | |
| 643 | #ifdef TRACK |
| 644 | printf ("value=%c:%d, code=%d \"%s\", size=%d\n", value,value, c, binstream(c,m), m); |
| 645 | #endif |
| 646 | |
| 647 | } else { |
| 648 | hufftree_to_codes(x, m, c); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | /* RIGHT */ |
| 653 | x = hufftree[start].pright; |
| 654 | c = code | (RIGHTCODE << n); |
| 655 | m = n + 1; |
| 656 | |
| 657 | if (hufftree[x].isleaf) { |
| 658 | value = hufftree[x].value; |
| 659 | code_table[value] = c; |
| 660 | size_table[value] = (unsigned)m; |
| 661 | |
| 662 | #ifdef TRACK |
| 663 | printf ("value=%c:%d, code=%d \"%s\", size=%d\n", value,value, c, binstream(c,m), m); |
| 664 | #endif |
| 665 | |
| 666 | } else { |
| 667 | hufftree_to_codes(x, m, c); |
| 668 | } |
| 669 | |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | static int |
| 675 | hufftree_frombits (ro_stream_t *stream, bool_t *pok) |
| 676 | { |
| 677 | unsigned bit; |
| 678 | unsigned value; |
| 679 | int thisnode; |
| 680 | struct huff h; |
| 681 | |
| 682 | if (!*pok) |
| 683 | return 0; |
| 684 | |
| 685 | bit = stream_nextbit(stream); |
| 686 | if (bit == BITLEAF) { |
| 687 | /* leaf */ |
| 688 | value = stream_nextbit_n (stream, VALUEBITS); |
| 689 | thisnode = huff_end++; |
| 690 | h.isleaf = TRUE; |
| 691 | h.value = (int)value; |
| 692 | h.freq = 0; |
| 693 | h.pleft = 0; |
| 694 | h.pright = 0; |
| 695 | |
| 696 | if (thisnode >= MAXHUFF) { |
| 697 | *pok = FALSE; |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | hufftree[thisnode] = h; |
| 702 | |
| 703 | #ifdef TRACK |
| 704 | printf ("Huff leaf, %d=%c\n", value, value); |
| 705 | #endif |
| 706 | |
| 707 | return thisnode; |
| 708 | |
| 709 | } else { |
| 710 | /* node */ |
| 711 | thisnode = huff_end++; |
| 712 | |
| 713 | if (thisnode >= MAXHUFF) { |
| 714 | *pok = FALSE; |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | h.isleaf = FALSE; |
| 719 | h.value = -1; |
| 720 | h.freq = 0; |
| 721 | h.pleft = hufftree_frombits (stream, pok); |
| 722 | h.pright = hufftree_frombits (stream, pok); |
| 723 | hufftree[thisnode] = h; |
| 724 | return thisnode; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | |
| 729 | static void |
| 730 | hufftree_tobits (int thisnode, stream_t *stream) |
| 731 | { |
| 732 | |
| 733 | if (hufftree[thisnode].isleaf) { |
| 734 | |
| 735 | #ifdef TRACK |
| 736 | {int c = hufftree[thisnode].value; printf ("[leaf=1][%c:%d=%s]", c, c, binstream(c,8));} |
| 737 | #endif |
| 738 | |
| 739 | assert (0 <= hufftree[thisnode].value); |
| 740 | |
| 741 | stream_writebit (stream, BITLEAF); |
| 742 | stream_write_n ((unsigned)hufftree[thisnode].value, VALUEBITS, stream); |
| 743 | |
| 744 | } else { |
| 745 | stream_writebit (stream, BITNODE); |
| 746 | |
| 747 | #ifdef TRACK |
| 748 | printf ("[node=0]"); |
| 749 | #endif |
| 750 | |
| 751 | hufftree_tobits (hufftree[thisnode].pleft, stream); |
| 752 | hufftree_tobits (hufftree[thisnode].pright, stream); |
| 753 | |
| 754 | } |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | |
| 759 | static unsigned int |
| 760 | hufftree_readstream (int root, ro_stream_t *s) |
| 761 | { |
| 762 | unsigned bit; |
| 763 | int next; |
| 764 | |
| 765 | bit = stream_nextbit(s); |
| 766 | if (bit == RIGHTCODE) { |
| 767 | /* right */ |
| 768 | next = hufftree[root].pright; |
| 769 | } else { |
| 770 | /*ASSERT (bit == LEFTCODE */ |
| 771 | /* left */ |
| 772 | next = hufftree[root].pleft; |
| 773 | } |
| 774 | |
| 775 | if (hufftree[next].isleaf) { |
| 776 | assert (0 <= hufftree[next].value); |
| 777 | return (unsigned)hufftree[next].value; |
| 778 | } else { |
| 779 | return hufftree_readstream (next, s); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | /*==== HEAP ==========================================*/ |
| 784 | |
| 785 | static void |
| 786 | heap_init (void) |
| 787 | { |
| 788 | heap_end = 1; |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | static void |
| 793 | heap_append (struct element e) |
| 794 | { |
| 795 | /*ASSERT (heap_end < MAXHEAP);*/ |
| 796 | heap[heap_end++] = e; |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | static void |
| 801 | heap_sift_up (int x) |
| 802 | { |
| 803 | struct element t; |
| 804 | int p; |
| 805 | int c = x; |
| 806 | while (c > 1) { |
| 807 | p = c / 2; |
| 808 | if (heap[c].freq < heap[p].freq) { |
| 809 | t = heap[c]; heap[c] = heap[p]; heap[p] = t; |
| 810 | } else { |
| 811 | break; |
| 812 | } |
| 813 | c = p; |
| 814 | } |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | |
| 819 | |
| 820 | static void |
| 821 | heap_adjust_down (int top, int last) |
| 822 | { |
| 823 | struct element t; |
| 824 | int p; |
| 825 | int c; |
| 826 | int left, right; |
| 827 | |
| 828 | if (last == top) { /* at least top element */ |
| 829 | /* done */ |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | /* starts at the top */ |
| 834 | p = top; |
| 835 | |
| 836 | while (p <= last) |
| 837 | { |
| 838 | left = 2*p; |
| 839 | right = left + 1; |
| 840 | |
| 841 | if (left > last) |
| 842 | break; |
| 843 | |
| 844 | if (right <= last && (heap[right].freq < heap[left].freq)) |
| 845 | c = right; |
| 846 | else |
| 847 | c = left; |
| 848 | |
| 849 | if (c > last) |
| 850 | break; |
| 851 | |
| 852 | if (heap[c].freq < heap[p].freq) { |
| 853 | t = heap[c]; heap[c] = heap[p]; heap[p] = t; |
| 854 | } else { |
| 855 | break; |
| 856 | } |
| 857 | p = c; |
| 858 | } |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | |
| 863 | /* |
| 864 | static void |
| 865 | heap_plot (void) |
| 866 | { |
| 867 | unsigned int line, limit, j; |
| 868 | int n = heap_end; |
| 869 | printf("===========================\n"); |
| 870 | line = 1; |
| 871 | j = 1; |
| 872 | while (j < n) { |
| 873 | limit = 1 << line; |
| 874 | while (j < limit && j < n) { |
| 875 | printf("%3d:%c ",heap[j].freq, hufftree[heap[j].huffidx].value); |
| 876 | j++; |
| 877 | } |
| 878 | while (j < limit) { |
| 879 | printf("%3s ","--"); |
| 880 | j++; |
| 881 | } |
| 882 | line++; printf("\n"); |
| 883 | } |
| 884 | printf("===========================\n"); |
| 885 | return; |
| 886 | } |
| 887 | */ |
| 888 | |
| 889 | /* |
| 890 | static void |
| 891 | freq_report (void) |
| 892 | { |
| 893 | int i; |
| 894 | printf ("\nFREQUENCIES\n"); |
| 895 | for (i = 0; i < MAXDIVERSITY; i++) { |
| 896 | if (freq[i] > 0) { |
| 897 | printf ("%c: %2d: %d: %d\n", i, i, freq[i], code_table[i]); |
| 898 | } |
| 899 | } |
| 900 | printf ("\n"); |
| 901 | return; |
| 902 | } |
| 903 | */ |
| 904 | |
| 905 | static void |
| 906 | freq_init (const unsigned char *in, size_t max) |
| 907 | { |
| 908 | size_t i; |
| 909 | |
| 910 | /* clean up frequencies */ |
| 911 | for (i = 0; i < MAXDIVERSITY; i++) { |
| 912 | freq [i] = 0; |
| 913 | code_table[i] = 0; |
| 914 | size_table[i] = 0; |
| 915 | } |
| 916 | |
| 917 | /* build frequencies */ |
| 918 | for (i = 0; i < max; i++) { |
| 919 | freq [in[i]]++; |
| 920 | } |
| 921 | |
| 922 | #ifdef TRACK |
| 923 | freq_report(); |
| 924 | #endif |
| 925 | |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | static int |
| 930 | hufftree_from_freq (void) |
| 931 | { |
| 932 | int i; |
| 933 | struct huff h; |
| 934 | struct element e; |
| 935 | int root; |
| 936 | |
| 937 | hufftree_reset (); |
| 938 | |
| 939 | /* build huff tree elements */ |
| 940 | huff_end = 0; |
| 941 | for (i = 0; i < MAXDIVERSITY; i++) { |
| 942 | if (freq[i] > 0) { |
| 943 | h.isleaf = TRUE; |
| 944 | h.value = i; |
| 945 | h.freq = freq[i]; |
| 946 | h.pleft = 0; |
| 947 | h.pright = 0; |
| 948 | hufftree[huff_end++] = h; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /* build heap */ |
| 953 | heap_init(); |
| 954 | for (i = 0; i < huff_end; i++) { |
| 955 | e.freq = hufftree[i].freq; |
| 956 | e.huffidx = i; |
| 957 | heap_append(e); |
| 958 | heap_sift_up(heap_end-1); |
| 959 | } |
| 960 | |
| 961 | #ifdef TRACE |
| 962 | heap_plot (); |
| 963 | #endif |
| 964 | |
| 965 | root = hufftree_from_heap(); |
| 966 | |
| 967 | /*hufftree_to_codes (root, 0, 0);*/ |
| 968 | |
| 969 | return root; |
| 970 | } |
| 971 | |
| 972 | |
| 973 |