MCPcopy Create free account
hub / github.com/CoderLine/alphaTab / flushBlock

Method flushBlock

packages/alphatab/src/zip/DeflaterHuffman.ts:579–636  ·  view source on GitHub ↗
(stored: Uint8Array, storedOffset: number, storedLength: number, lastBlock: boolean)

Source from the content-addressed store, hash-verified

577 }
578
579 public flushBlock(stored: Uint8Array, storedOffset: number, storedLength: number, lastBlock: boolean) {
580 this._literalTree.freqs[DeflaterHuffman._eofSymbol]++;
581
582 // Build trees
583 this._literalTree.buildTree();
584 this._distTree.buildTree();
585
586 // Calculate bitlen frequency
587 this._literalTree.calcBLFreq(this._blTree);
588 this._distTree.calcBLFreq(this._blTree);
589
590 // Build bitlen tree
591 this._blTree.buildTree();
592
593 let blTreeCodes = 4;
594 for (let i = 18; i > blTreeCodes; i--) {
595 if (this._blTree.length![DeflaterHuffman._blOrder[i]] > 0) {
596 blTreeCodes = i + 1;
597 }
598 }
599 let optLen =
600 14 +
601 blTreeCodes * 3 +
602 this._blTree.getEncodedLength() +
603 this._literalTree.getEncodedLength() +
604 this._distTree.getEncodedLength() +
605 this._extraBits;
606
607 let staticLen = this._extraBits;
608 for (let i = 0; i < DeflaterHuffman._literalNum; i++) {
609 staticLen += this._literalTree.freqs[i] * DeflaterHuffman._staticLLength[i];
610 }
611 for (let i = 0; i < DeflaterHuffman._distNum; i++) {
612 staticLen += this._distTree.freqs[i] * DeflaterHuffman._staticDLength[i];
613 }
614 if (optLen >= staticLen) {
615 // Force static trees
616 optLen = staticLen;
617 }
618
619 if (storedOffset >= 0 && storedLength + 4 < optLen >> 3) {
620 // Store Block
621 this.flushStoredBlock(stored, storedOffset, storedLength, lastBlock);
622 } else if (optLen === staticLen) {
623 // Encode with static tree
624 this.pending.writeBits((DeflaterHuffman.staticTrees << 1) + (lastBlock ? 1 : 0), 3);
625 this._literalTree.setStaticCodes(DeflaterHuffman._staticLCodes, DeflaterHuffman._staticLLength);
626 this._distTree.setStaticCodes(DeflaterHuffman._staticDCodes, DeflaterHuffman._staticDLength);
627 this.compressBlock();
628 this.reset();
629 } else {
630 // Encode with dynamic tree
631 this.pending.writeBits((DeflaterHuffman.dynTrees << 1) + (lastBlock ? 1 : 0), 3);
632 this.sendAllTrees(blTreeCodes);
633 this.compressBlock();
634 this.reset();
635 }
636 }

Callers 1

_deflateSlowMethod · 0.80

Calls 9

flushStoredBlockMethod · 0.95
compressBlockMethod · 0.95
resetMethod · 0.95
sendAllTreesMethod · 0.95
buildTreeMethod · 0.80
calcBLFreqMethod · 0.80
getEncodedLengthMethod · 0.80
writeBitsMethod · 0.80
setStaticCodesMethod · 0.80

Tested by

no test coverage detected