Reads up to 4 symbols directly and applies predefined histograms.
(int alphabetSizeMax, int alphabetSizeLimit,
int[] tableGroup, int tableIdx, State s)
| 540 | * Reads up to 4 symbols directly and applies predefined histograms. |
| 541 | */ |
| 542 | private static int readSimpleHuffmanCode(int alphabetSizeMax, int alphabetSizeLimit, |
| 543 | int[] tableGroup, int tableIdx, State s) { |
| 544 | // TODO(eustas): Avoid allocation? |
| 545 | final int[] codeLengths = new int[alphabetSizeLimit]; |
| 546 | final int[] symbols = new int[4]; |
| 547 | |
| 548 | final int maxBits = 1 + log2floor(alphabetSizeMax - 1); |
| 549 | |
| 550 | final int numSymbols = BitReader.readFewBits(s, 2) + 1; |
| 551 | for (int i = 0; i < numSymbols; ++i) { |
| 552 | BitReader.fillBitWindow(s); |
| 553 | final int symbol = BitReader.readFewBits(s, maxBits); |
| 554 | if (symbol >= alphabetSizeLimit) { |
| 555 | return Utils.makeError(s, BROTLI_ERROR_SYMBOL_OUT_OF_RANGE); |
| 556 | } |
| 557 | symbols[i] = symbol; |
| 558 | } |
| 559 | final int result = checkDupes(s, symbols, numSymbols); |
| 560 | if (result < BROTLI_OK) { |
| 561 | return result; |
| 562 | } |
| 563 | |
| 564 | int histogramId = numSymbols; |
| 565 | if (numSymbols == 4) { |
| 566 | histogramId += BitReader.readFewBits(s, 1); |
| 567 | } |
| 568 | |
| 569 | switch (histogramId) { |
| 570 | case 1: |
| 571 | codeLengths[symbols[0]] = 1; |
| 572 | break; |
| 573 | |
| 574 | case 2: |
| 575 | codeLengths[symbols[0]] = 1; |
| 576 | codeLengths[symbols[1]] = 1; |
| 577 | break; |
| 578 | |
| 579 | case 3: |
| 580 | codeLengths[symbols[0]] = 1; |
| 581 | codeLengths[symbols[1]] = 2; |
| 582 | codeLengths[symbols[2]] = 2; |
| 583 | break; |
| 584 | |
| 585 | case 4: // uniform 4-symbol histogram |
| 586 | codeLengths[symbols[0]] = 2; |
| 587 | codeLengths[symbols[1]] = 2; |
| 588 | codeLengths[symbols[2]] = 2; |
| 589 | codeLengths[symbols[3]] = 2; |
| 590 | break; |
| 591 | |
| 592 | case 5: // prioritized 4-symbol histogram |
| 593 | codeLengths[symbols[0]] = 1; |
| 594 | codeLengths[symbols[1]] = 2; |
| 595 | codeLengths[symbols[2]] = 3; |
| 596 | codeLengths[symbols[3]] = 3; |
| 597 | break; |
| 598 | |
| 599 | default: |
no test coverage detected