=========================================================================== * Compute the optimal bit lengths for a tree and update the total bit length * for the current block. * IN assertion: the fields freq and dad are set, heap[heap_max] and * above are the tree nodes sorted by increasing frequency. * OUT assertions: the field len is set to the optimal bit length, the * array bl_c
| 536 | * not null. |
| 537 | */ |
| 538 | local void gen_bitlen(deflate_state *s, tree_desc *desc) { |
| 539 | ct_data *tree = desc->dyn_tree; |
| 540 | int max_code = desc->max_code; |
| 541 | const ct_data *stree = desc->stat_desc->static_tree; |
| 542 | const intf *extra = desc->stat_desc->extra_bits; |
| 543 | int base = desc->stat_desc->extra_base; |
| 544 | int max_length = desc->stat_desc->max_length; |
| 545 | int h; /* heap index */ |
| 546 | int n, m; /* iterate over the tree elements */ |
| 547 | int bits; /* bit length */ |
| 548 | int xbits; /* extra bits */ |
| 549 | ush f; /* frequency */ |
| 550 | int overflow = 0; /* number of elements with bit length too large */ |
| 551 | |
| 552 | for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; |
| 553 | |
| 554 | /* In a first pass, compute the optimal bit lengths (which may |
| 555 | * overflow in the case of the bit length tree). |
| 556 | */ |
| 557 | tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ |
| 558 | |
| 559 | for (h = s->heap_max + 1; h < HEAP_SIZE; h++) { |
| 560 | n = s->heap[h]; |
| 561 | bits = tree[tree[n].Dad].Len + 1; |
| 562 | if (bits > max_length) bits = max_length, overflow++; |
| 563 | tree[n].Len = (ush)bits; |
| 564 | /* We overwrite tree[n].Dad which is no longer needed */ |
| 565 | |
| 566 | if (n > max_code) continue; /* not a leaf node */ |
| 567 | |
| 568 | s->bl_count[bits]++; |
| 569 | xbits = 0; |
| 570 | if (n >= base) xbits = extra[n - base]; |
| 571 | f = tree[n].Freq; |
| 572 | s->opt_len += (ulg)f * (unsigned)(bits + xbits); |
| 573 | if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits); |
| 574 | } |
| 575 | if (overflow == 0) return; |
| 576 | |
| 577 | Tracev((stderr,"\nbit length overflow\n")); |
| 578 | /* This happens for example on obj2 and pic of the Calgary corpus */ |
| 579 | |
| 580 | /* Find the first bit length which could increase: */ |
| 581 | do { |
| 582 | bits = max_length - 1; |
| 583 | while (s->bl_count[bits] == 0) bits--; |
| 584 | s->bl_count[bits]--; /* move one leaf down the tree */ |
| 585 | s->bl_count[bits + 1] += 2; /* move one overflow item as its brother */ |
| 586 | s->bl_count[max_length]--; |
| 587 | /* The brother of the overflow item also moves one step up, |
| 588 | * but this does not affect bl_count[max_length] |
| 589 | */ |
| 590 | overflow -= 2; |
| 591 | } while (overflow > 0); |
| 592 | |
| 593 | /* Now recompute all bit lengths, scanning in increasing frequency. |
| 594 | * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all |
| 595 | * lengths instead of fixing only the wrong ones. This idea is taken |