OLD | NEW |
1 // Copyright 2015 Google Inc. All Rights Reserved. | 1 // Copyright 2015 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
4 // that can be found in the COPYING file in the root of the source | 4 // that can be found in the COPYING file in the root of the source |
5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Image transform methods for lossless encoder. | 10 // Image transform methods for lossless encoder. |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 log_2 += (double)correction / orig_v; | 375 log_2 += (double)correction / orig_v; |
376 } | 376 } |
377 return (float)log_2; | 377 return (float)log_2; |
378 } else { | 378 } else { |
379 return (float)(LOG_2_RECIPROCAL * log((double)v)); | 379 return (float)(LOG_2_RECIPROCAL * log((double)v)); |
380 } | 380 } |
381 } | 381 } |
382 | 382 |
383 // Mostly used to reduce code size + readability | 383 // Mostly used to reduce code size + readability |
384 static WEBP_INLINE int GetMin(int a, int b) { return (a > b) ? b : a; } | 384 static WEBP_INLINE int GetMin(int a, int b) { return (a > b) ? b : a; } |
| 385 static WEBP_INLINE int GetMax(int a, int b) { return (a < b) ? b : a; } |
385 | 386 |
386 //------------------------------------------------------------------------------ | 387 //------------------------------------------------------------------------------ |
387 // Methods to calculate Entropy (Shannon). | 388 // Methods to calculate Entropy (Shannon). |
388 | 389 |
389 static float PredictionCostSpatial(const int counts[256], int weight_0, | 390 static float PredictionCostSpatial(const int counts[256], int weight_0, |
390 double exp_val) { | 391 double exp_val) { |
391 const int significant_symbols = 256 >> 4; | 392 const int significant_symbols = 256 >> 4; |
392 const double exp_decay_factor = 0.6; | 393 const double exp_decay_factor = 0.6; |
393 double bits = weight_0 * counts[0]; | 394 double bits = weight_0 * counts[0]; |
394 int i; | 395 int i; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 const uint32_t* upper_row) { | 545 const uint32_t* upper_row) { |
545 if (y == 0) { | 546 if (y == 0) { |
546 return (x == 0) ? ARGB_BLACK : current_row[x - 1]; // Left. | 547 return (x == 0) ? ARGB_BLACK : current_row[x - 1]; // Left. |
547 } else if (x == 0) { | 548 } else if (x == 0) { |
548 return upper_row[x]; // Top. | 549 return upper_row[x]; // Top. |
549 } else { | 550 } else { |
550 return pred_func(current_row[x - 1], upper_row + x); | 551 return pred_func(current_row[x - 1], upper_row + x); |
551 } | 552 } |
552 } | 553 } |
553 | 554 |
| 555 static int MaxDiffBetweenPixels(uint32_t p1, uint32_t p2) { |
| 556 const int diff_a = abs((int)(p1 >> 24) - (int)(p2 >> 24)); |
| 557 const int diff_r = abs((int)((p1 >> 16) & 0xff) - (int)((p2 >> 16) & 0xff)); |
| 558 const int diff_g = abs((int)((p1 >> 8) & 0xff) - (int)((p2 >> 8) & 0xff)); |
| 559 const int diff_b = abs((int)(p1 & 0xff) - (int)(p2 & 0xff)); |
| 560 return GetMax(GetMax(diff_a, diff_r), GetMax(diff_g, diff_b)); |
| 561 } |
| 562 |
| 563 static int MaxDiffAroundPixel(uint32_t current, uint32_t up, uint32_t down, |
| 564 uint32_t left, uint32_t right) { |
| 565 const int diff_up = MaxDiffBetweenPixels(current, up); |
| 566 const int diff_down = MaxDiffBetweenPixels(current, down); |
| 567 const int diff_left = MaxDiffBetweenPixels(current, left); |
| 568 const int diff_right = MaxDiffBetweenPixels(current, right); |
| 569 return GetMax(GetMax(diff_up, diff_down), GetMax(diff_left, diff_right)); |
| 570 } |
| 571 |
| 572 static uint32_t AddGreenToBlueAndRed(uint32_t argb) { |
| 573 const uint32_t green = (argb >> 8) & 0xff; |
| 574 uint32_t red_blue = argb & 0x00ff00ffu; |
| 575 red_blue += (green << 16) | green; |
| 576 red_blue &= 0x00ff00ffu; |
| 577 return (argb & 0xff00ff00u) | red_blue; |
| 578 } |
| 579 |
| 580 static void MaxDiffsForRow(int width, int stride, const uint32_t* const argb, |
| 581 uint8_t* const max_diffs, int used_subtract_green) { |
| 582 uint32_t current, up, down, left, right; |
| 583 int x; |
| 584 if (width <= 2) return; |
| 585 current = argb[0]; |
| 586 right = argb[1]; |
| 587 if (used_subtract_green) { |
| 588 current = AddGreenToBlueAndRed(current); |
| 589 right = AddGreenToBlueAndRed(right); |
| 590 } |
| 591 // max_diffs[0] and max_diffs[width - 1] are never used. |
| 592 for (x = 1; x < width - 1; ++x) { |
| 593 up = argb[-stride + x]; |
| 594 down = argb[stride + x]; |
| 595 left = current; |
| 596 current = right; |
| 597 right = argb[x + 1]; |
| 598 if (used_subtract_green) { |
| 599 up = AddGreenToBlueAndRed(up); |
| 600 down = AddGreenToBlueAndRed(down); |
| 601 right = AddGreenToBlueAndRed(right); |
| 602 } |
| 603 max_diffs[x] = MaxDiffAroundPixel(current, up, down, left, right); |
| 604 } |
| 605 } |
| 606 |
| 607 // Quantize the difference between the actual component value and its prediction |
| 608 // to a multiple of quantization, working modulo 256, taking care not to cross |
| 609 // a boundary (inclusive upper limit). |
| 610 static uint8_t NearLosslessComponent(uint8_t value, uint8_t predict, |
| 611 uint8_t boundary, int quantization) { |
| 612 const int residual = (value - predict) & 0xff; |
| 613 const int boundary_residual = (boundary - predict) & 0xff; |
| 614 const int lower = residual & ~(quantization - 1); |
| 615 const int upper = lower + quantization; |
| 616 // Resolve ties towards a value closer to the prediction (i.e. towards lower |
| 617 // if value comes after prediction and towards upper otherwise). |
| 618 const int bias = ((boundary - value) & 0xff) < boundary_residual; |
| 619 if (residual - lower < upper - residual + bias) { |
| 620 // lower is closer to residual than upper. |
| 621 if (residual > boundary_residual && lower <= boundary_residual) { |
| 622 // Halve quantization step to avoid crossing boundary. This midpoint is |
| 623 // on the same side of boundary as residual because midpoint >= residual |
| 624 // (since lower is closer than upper) and residual is above the boundary. |
| 625 return lower + (quantization >> 1); |
| 626 } |
| 627 return lower; |
| 628 } else { |
| 629 // upper is closer to residual than lower. |
| 630 if (residual <= boundary_residual && upper > boundary_residual) { |
| 631 // Halve quantization step to avoid crossing boundary. This midpoint is |
| 632 // on the same side of boundary as residual because midpoint <= residual |
| 633 // (since upper is closer than lower) and residual is below the boundary. |
| 634 return lower + (quantization >> 1); |
| 635 } |
| 636 return upper & 0xff; |
| 637 } |
| 638 } |
| 639 |
| 640 // Quantize every component of the difference between the actual pixel value and |
| 641 // its prediction to a multiple of a quantization (a power of 2, not larger than |
| 642 // max_quantization which is a power of 2, smaller than max_diff). Take care if |
| 643 // value and predict have undergone subtract green, which means that red and |
| 644 // blue are represented as offsets from green. |
| 645 static uint32_t NearLossless(uint32_t value, uint32_t predict, |
| 646 int max_quantization, int max_diff, |
| 647 int used_subtract_green) { |
| 648 int quantization; |
| 649 uint8_t new_green = 0; |
| 650 uint8_t green_diff = 0; |
| 651 uint8_t a, r, g, b; |
| 652 if (max_diff <= 2) { |
| 653 return VP8LSubPixels(value, predict); |
| 654 } |
| 655 quantization = max_quantization; |
| 656 while (quantization >= max_diff) { |
| 657 quantization >>= 1; |
| 658 } |
| 659 if ((value >> 24) == 0 || (value >> 24) == 0xff) { |
| 660 // Preserve transparency of fully transparent or fully opaque pixels. |
| 661 a = ((value >> 24) - (predict >> 24)) & 0xff; |
| 662 } else { |
| 663 a = NearLosslessComponent(value >> 24, predict >> 24, 0xff, quantization); |
| 664 } |
| 665 g = NearLosslessComponent((value >> 8) & 0xff, (predict >> 8) & 0xff, 0xff, |
| 666 quantization); |
| 667 if (used_subtract_green) { |
| 668 // The green offset will be added to red and blue components during decoding |
| 669 // to obtain the actual red and blue values. |
| 670 new_green = ((predict >> 8) + g) & 0xff; |
| 671 // The amount by which green has been adjusted during quantization. It is |
| 672 // subtracted from red and blue for compensation, to avoid accumulating two |
| 673 // quantization errors in them. |
| 674 green_diff = (new_green - (value >> 8)) & 0xff; |
| 675 } |
| 676 r = NearLosslessComponent(((value >> 16) - green_diff) & 0xff, |
| 677 (predict >> 16) & 0xff, 0xff - new_green, |
| 678 quantization); |
| 679 b = NearLosslessComponent((value - green_diff) & 0xff, predict & 0xff, |
| 680 0xff - new_green, quantization); |
| 681 return ((uint32_t)a << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; |
| 682 } |
| 683 |
| 684 // Returns the difference between the pixel and its prediction. In case of a |
| 685 // lossy encoding, updates the source image to avoid propagating the deviation |
| 686 // further to pixels which depend on the current pixel for their predictions. |
| 687 static WEBP_INLINE uint32_t GetResidual(int width, int height, |
| 688 uint32_t* const upper_row, |
| 689 uint32_t* const current_row, |
| 690 const uint8_t* const max_diffs, |
| 691 int mode, VP8LPredictorFunc pred_func, |
| 692 int x, int y, int max_quantization, |
| 693 int exact, int used_subtract_green) { |
| 694 const uint32_t predict = Predict(pred_func, x, y, current_row, upper_row); |
| 695 uint32_t residual; |
| 696 if (max_quantization == 1 || mode == 0 || y == 0 || y == height - 1 || |
| 697 x == 0 || x == width - 1) { |
| 698 residual = VP8LSubPixels(current_row[x], predict); |
| 699 } else { |
| 700 residual = NearLossless(current_row[x], predict, max_quantization, |
| 701 max_diffs[x], used_subtract_green); |
| 702 // Update the source image. |
| 703 current_row[x] = VP8LAddPixels(predict, residual); |
| 704 // x is never 0 here so we do not need to update upper_row like below. |
| 705 } |
| 706 if (!exact && (current_row[x] & kMaskAlpha) == 0) { |
| 707 // If alpha is 0, cleanup RGB. We can choose the RGB values of the residual |
| 708 // for best compression. The prediction of alpha itself can be non-zero and |
| 709 // must be kept though. We choose RGB of the residual to be 0. |
| 710 residual &= kMaskAlpha; |
| 711 // Update the source image. |
| 712 current_row[x] = predict & ~kMaskAlpha; |
| 713 // The prediction for the rightmost pixel in a row uses the leftmost pixel |
| 714 // in that row as its top-right context pixel. Hence if we change the |
| 715 // leftmost pixel of current_row, the corresponding change must be applied |
| 716 // to upper_row as well where top-right context is being read from. |
| 717 if (x == 0 && y != 0) upper_row[width] = current_row[0]; |
| 718 } |
| 719 return residual; |
| 720 } |
| 721 |
554 // Returns best predictor and updates the accumulated histogram. | 722 // Returns best predictor and updates the accumulated histogram. |
| 723 // If max_quantization > 1, assumes that near lossless processing will be |
| 724 // applied, quantizing residuals to multiples of quantization levels up to |
| 725 // max_quantization (the actual quantization level depends on smoothness near |
| 726 // the given pixel). |
555 static int GetBestPredictorForTile(int width, int height, | 727 static int GetBestPredictorForTile(int width, int height, |
556 int tile_x, int tile_y, int bits, | 728 int tile_x, int tile_y, int bits, |
557 int accumulated[4][256], | 729 int accumulated[4][256], |
558 const uint32_t* const argb_scratch, | 730 uint32_t* const argb_scratch, |
559 int exact) { | 731 const uint32_t* const argb, |
| 732 int max_quantization, |
| 733 int exact, int used_subtract_green) { |
560 const int kNumPredModes = 14; | 734 const int kNumPredModes = 14; |
561 const int col_start = tile_x << bits; | 735 const int start_x = tile_x << bits; |
562 const int row_start = tile_y << bits; | 736 const int start_y = tile_y << bits; |
563 const int tile_size = 1 << bits; | 737 const int tile_size = 1 << bits; |
564 const int max_y = GetMin(tile_size, height - row_start); | 738 const int max_y = GetMin(tile_size, height - start_y); |
565 const int max_x = GetMin(tile_size, width - col_start); | 739 const int max_x = GetMin(tile_size, width - start_x); |
| 740 // Whether there exist columns just outside the tile. |
| 741 const int have_left = (start_x > 0); |
| 742 const int have_right = (max_x < width - start_x); |
| 743 // Position and size of the strip covering the tile and adjacent columns if |
| 744 // they exist. |
| 745 const int context_start_x = start_x - have_left; |
| 746 const int context_width = max_x + have_left + have_right; |
| 747 // The width of upper_row and current_row is one pixel larger than image width |
| 748 // to allow the top right pixel to point to the leftmost pixel of the next row |
| 749 // when at the right edge. |
| 750 uint32_t* upper_row = argb_scratch; |
| 751 uint32_t* current_row = upper_row + width + 1; |
| 752 uint8_t* const max_diffs = (uint8_t*)(current_row + width + 1); |
566 float best_diff = MAX_DIFF_COST; | 753 float best_diff = MAX_DIFF_COST; |
567 int best_mode = 0; | 754 int best_mode = 0; |
568 int mode; | 755 int mode; |
569 int histo_stack_1[4][256]; | 756 int histo_stack_1[4][256]; |
570 int histo_stack_2[4][256]; | 757 int histo_stack_2[4][256]; |
571 // Need pointers to be able to swap arrays. | 758 // Need pointers to be able to swap arrays. |
572 int (*histo_argb)[256] = histo_stack_1; | 759 int (*histo_argb)[256] = histo_stack_1; |
573 int (*best_histo)[256] = histo_stack_2; | 760 int (*best_histo)[256] = histo_stack_2; |
| 761 int i, j; |
574 | 762 |
575 int i, j; | |
576 for (mode = 0; mode < kNumPredModes; ++mode) { | 763 for (mode = 0; mode < kNumPredModes; ++mode) { |
577 const uint32_t* current_row = argb_scratch; | |
578 const VP8LPredictorFunc pred_func = VP8LPredictors[mode]; | 764 const VP8LPredictorFunc pred_func = VP8LPredictors[mode]; |
579 float cur_diff; | 765 float cur_diff; |
580 int y; | 766 int relative_y; |
581 memset(histo_argb, 0, sizeof(histo_stack_1)); | 767 memset(histo_argb, 0, sizeof(histo_stack_1)); |
582 for (y = 0; y < max_y; ++y) { | 768 if (start_y > 0) { |
583 int x; | 769 // Read the row above the tile which will become the first upper_row. |
584 const int row = row_start + y; | 770 // Include a pixel to the left if it exists; include a pixel to the right |
585 const uint32_t* const upper_row = current_row; | 771 // in all cases (wrapping to the leftmost pixel of the next row if it does |
586 current_row = upper_row + width; | 772 // not exist). |
587 for (x = 0; x < max_x; ++x) { | 773 memcpy(current_row + context_start_x, |
588 const int col = col_start + x; | 774 argb + (start_y - 1) * width + context_start_x, |
589 const uint32_t predict = | 775 sizeof(*argb) * (max_x + have_left + 1)); |
590 Predict(pred_func, col, row, current_row, upper_row); | 776 } |
591 uint32_t residual = VP8LSubPixels(current_row[col], predict); | 777 for (relative_y = 0; relative_y < max_y; ++relative_y) { |
592 if (!exact && (current_row[col] & kMaskAlpha) == 0) { | 778 const int y = start_y + relative_y; |
593 residual &= kMaskAlpha; // See CopyTileWithPrediction. | 779 int relative_x; |
594 } | 780 uint32_t* tmp = upper_row; |
595 UpdateHisto(histo_argb, residual); | 781 upper_row = current_row; |
| 782 current_row = tmp; |
| 783 // Read current_row. Include a pixel to the left if it exists; include a |
| 784 // pixel to the right in all cases except at the bottom right corner of |
| 785 // the image (wrapping to the leftmost pixel of the next row if it does |
| 786 // not exist in the current row). |
| 787 memcpy(current_row + context_start_x, |
| 788 argb + y * width + context_start_x, |
| 789 sizeof(*argb) * (max_x + have_left + (y + 1 < height))); |
| 790 if (max_quantization > 1 && y >= 1 && y + 1 < height) { |
| 791 MaxDiffsForRow(context_width, width, argb + y * width + context_start_x, |
| 792 max_diffs + context_start_x, used_subtract_green); |
| 793 } |
| 794 |
| 795 for (relative_x = 0; relative_x < max_x; ++relative_x) { |
| 796 const int x = start_x + relative_x; |
| 797 UpdateHisto(histo_argb, |
| 798 GetResidual(width, height, upper_row, current_row, |
| 799 max_diffs, mode, pred_func, x, y, |
| 800 max_quantization, exact, used_subtract_green)); |
596 } | 801 } |
597 } | 802 } |
598 cur_diff = PredictionCostSpatialHistogram( | 803 cur_diff = PredictionCostSpatialHistogram( |
599 (const int (*)[256])accumulated, (const int (*)[256])histo_argb); | 804 (const int (*)[256])accumulated, (const int (*)[256])histo_argb); |
600 if (cur_diff < best_diff) { | 805 if (cur_diff < best_diff) { |
601 int (*tmp)[256] = histo_argb; | 806 int (*tmp)[256] = histo_argb; |
602 histo_argb = best_histo; | 807 histo_argb = best_histo; |
603 best_histo = tmp; | 808 best_histo = tmp; |
604 best_diff = cur_diff; | 809 best_diff = cur_diff; |
605 best_mode = mode; | 810 best_mode = mode; |
606 } | 811 } |
607 } | 812 } |
608 | 813 |
609 for (i = 0; i < 4; i++) { | 814 for (i = 0; i < 4; i++) { |
610 for (j = 0; j < 256; j++) { | 815 for (j = 0; j < 256; j++) { |
611 accumulated[i][j] += best_histo[i][j]; | 816 accumulated[i][j] += best_histo[i][j]; |
612 } | 817 } |
613 } | 818 } |
614 | 819 |
615 return best_mode; | 820 return best_mode; |
616 } | 821 } |
617 | 822 |
| 823 // Converts pixels of the image to residuals with respect to predictions. |
| 824 // If max_quantization > 1, applies near lossless processing, quantizing |
| 825 // residuals to multiples of quantization levels up to max_quantization |
| 826 // (the actual quantization level depends on smoothness near the given pixel). |
618 static void CopyImageWithPrediction(int width, int height, | 827 static void CopyImageWithPrediction(int width, int height, |
619 int bits, uint32_t* const modes, | 828 int bits, uint32_t* const modes, |
620 uint32_t* const argb_scratch, | 829 uint32_t* const argb_scratch, |
621 uint32_t* const argb, | 830 uint32_t* const argb, |
622 int low_effort, int exact) { | 831 int low_effort, int max_quantization, |
| 832 int exact, int used_subtract_green) { |
623 const int tiles_per_row = VP8LSubSampleSize(width, bits); | 833 const int tiles_per_row = VP8LSubSampleSize(width, bits); |
624 const int mask = (1 << bits) - 1; | 834 const int mask = (1 << bits) - 1; |
625 // The row size is one pixel longer to allow the top right pixel to point to | 835 // The width of upper_row and current_row is one pixel larger than image width |
626 // the leftmost pixel of the next row when at the right edge. | 836 // to allow the top right pixel to point to the leftmost pixel of the next row |
627 uint32_t* current_row = argb_scratch; | 837 // when at the right edge. |
628 uint32_t* upper_row = argb_scratch + width + 1; | 838 uint32_t* upper_row = argb_scratch; |
| 839 uint32_t* current_row = upper_row + width + 1; |
| 840 uint8_t* current_max_diffs = (uint8_t*)(current_row + width + 1); |
| 841 uint8_t* lower_max_diffs = current_max_diffs + width; |
629 int y; | 842 int y; |
630 VP8LPredictorFunc pred_func = | 843 int mode = 0; |
631 low_effort ? VP8LPredictors[kPredLowEffort] : NULL; | 844 VP8LPredictorFunc pred_func = NULL; |
632 | 845 |
633 for (y = 0; y < height; ++y) { | 846 for (y = 0; y < height; ++y) { |
634 int x; | 847 int x; |
635 uint32_t* tmp = upper_row; | 848 uint32_t* const tmp32 = upper_row; |
636 upper_row = current_row; | 849 upper_row = current_row; |
637 current_row = tmp; | 850 current_row = tmp32; |
638 memcpy(current_row, argb + y * width, sizeof(*current_row) * width); | 851 memcpy(current_row, argb + y * width, |
639 current_row[width] = (y + 1 < height) ? argb[(y + 1) * width] : ARGB_BLACK; | 852 sizeof(*argb) * (width + (y + 1 < height))); |
640 | 853 |
641 if (low_effort) { | 854 if (low_effort) { |
642 for (x = 0; x < width; ++x) { | 855 for (x = 0; x < width; ++x) { |
643 const uint32_t predict = | 856 const uint32_t predict = Predict(VP8LPredictors[kPredLowEffort], x, y, |
644 Predict(pred_func, x, y, current_row, upper_row); | 857 current_row, upper_row); |
645 argb[y * width + x] = VP8LSubPixels(current_row[x], predict); | 858 argb[y * width + x] = VP8LSubPixels(current_row[x], predict); |
646 } | 859 } |
647 } else { | 860 } else { |
| 861 if (max_quantization > 1) { |
| 862 // Compute max_diffs for the lower row now, because that needs the |
| 863 // contents of argb for the current row, which we will overwrite with |
| 864 // residuals before proceeding with the next row. |
| 865 uint8_t* const tmp8 = current_max_diffs; |
| 866 current_max_diffs = lower_max_diffs; |
| 867 lower_max_diffs = tmp8; |
| 868 if (y + 2 < height) { |
| 869 MaxDiffsForRow(width, width, argb + (y + 1) * width, lower_max_diffs, |
| 870 used_subtract_green); |
| 871 } |
| 872 } |
648 for (x = 0; x < width; ++x) { | 873 for (x = 0; x < width; ++x) { |
649 uint32_t predict, residual; | |
650 if ((x & mask) == 0) { | 874 if ((x & mask) == 0) { |
651 const int mode = | 875 mode = (modes[(y >> bits) * tiles_per_row + (x >> bits)] >> 8) & 0xff; |
652 (modes[(y >> bits) * tiles_per_row + (x >> bits)] >> 8) & 0xff; | |
653 pred_func = VP8LPredictors[mode]; | 876 pred_func = VP8LPredictors[mode]; |
654 } | 877 } |
655 predict = Predict(pred_func, x, y, current_row, upper_row); | 878 argb[y * width + x] = GetResidual( |
656 residual = VP8LSubPixels(current_row[x], predict); | 879 width, height, upper_row, current_row, current_max_diffs, mode, |
657 if (!exact && (current_row[x] & kMaskAlpha) == 0) { | 880 pred_func, x, y, max_quantization, exact, used_subtract_green); |
658 // If alpha is 0, cleanup RGB. We can choose the RGB values of the | |
659 // residual for best compression. The prediction of alpha itself can | |
660 // be non-zero and must be kept though. We choose RGB of the residual | |
661 // to be 0. | |
662 residual &= kMaskAlpha; | |
663 // Update input image so that next predictions use correct RGB value. | |
664 current_row[x] = predict & ~kMaskAlpha; | |
665 if (x == 0 && y != 0) upper_row[width] = current_row[x]; | |
666 } | |
667 argb[y * width + x] = residual; | |
668 } | 881 } |
669 } | 882 } |
670 } | 883 } |
671 } | 884 } |
672 | 885 |
| 886 // Finds the best predictor for each tile, and converts the image to residuals |
| 887 // with respect to predictions. If near_lossless_quality < 100, applies |
| 888 // near lossless processing, shaving off more bits of residuals for lower |
| 889 // qualities. |
673 void VP8LResidualImage(int width, int height, int bits, int low_effort, | 890 void VP8LResidualImage(int width, int height, int bits, int low_effort, |
674 uint32_t* const argb, uint32_t* const argb_scratch, | 891 uint32_t* const argb, uint32_t* const argb_scratch, |
675 uint32_t* const image, int exact) { | 892 uint32_t* const image, int near_lossless_quality, |
676 const int max_tile_size = 1 << bits; | 893 int exact, int used_subtract_green) { |
677 const int tiles_per_row = VP8LSubSampleSize(width, bits); | 894 const int tiles_per_row = VP8LSubSampleSize(width, bits); |
678 const int tiles_per_col = VP8LSubSampleSize(height, bits); | 895 const int tiles_per_col = VP8LSubSampleSize(height, bits); |
679 uint32_t* const upper_row = argb_scratch; | |
680 uint32_t* const current_tile_rows = argb_scratch + width; | |
681 int tile_y; | 896 int tile_y; |
682 int histo[4][256]; | 897 int histo[4][256]; |
| 898 const int max_quantization = 1 << VP8LNearLosslessBits(near_lossless_quality); |
683 if (low_effort) { | 899 if (low_effort) { |
684 int i; | 900 int i; |
685 for (i = 0; i < tiles_per_row * tiles_per_col; ++i) { | 901 for (i = 0; i < tiles_per_row * tiles_per_col; ++i) { |
686 image[i] = ARGB_BLACK | (kPredLowEffort << 8); | 902 image[i] = ARGB_BLACK | (kPredLowEffort << 8); |
687 } | 903 } |
688 } else { | 904 } else { |
689 memset(histo, 0, sizeof(histo)); | 905 memset(histo, 0, sizeof(histo)); |
690 for (tile_y = 0; tile_y < tiles_per_col; ++tile_y) { | 906 for (tile_y = 0; tile_y < tiles_per_col; ++tile_y) { |
691 const int tile_y_offset = tile_y * max_tile_size; | |
692 const int this_tile_height = | |
693 (tile_y < tiles_per_col - 1) ? max_tile_size : height - tile_y_offset; | |
694 int tile_x; | 907 int tile_x; |
695 if (tile_y > 0) { | |
696 memcpy(upper_row, current_tile_rows + (max_tile_size - 1) * width, | |
697 width * sizeof(*upper_row)); | |
698 } | |
699 memcpy(current_tile_rows, &argb[tile_y_offset * width], | |
700 this_tile_height * width * sizeof(*current_tile_rows)); | |
701 for (tile_x = 0; tile_x < tiles_per_row; ++tile_x) { | 908 for (tile_x = 0; tile_x < tiles_per_row; ++tile_x) { |
702 const int pred = GetBestPredictorForTile(width, height, tile_x, tile_y, | 909 const int pred = GetBestPredictorForTile(width, height, tile_x, tile_y, |
703 bits, (int (*)[256])histo, argb_scratch, exact); | 910 bits, histo, argb_scratch, argb, max_quantization, exact, |
| 911 used_subtract_green); |
704 image[tile_y * tiles_per_row + tile_x] = ARGB_BLACK | (pred << 8); | 912 image[tile_y * tiles_per_row + tile_x] = ARGB_BLACK | (pred << 8); |
705 } | 913 } |
706 } | 914 } |
707 } | 915 } |
708 | 916 |
709 CopyImageWithPrediction(width, height, bits, | 917 CopyImageWithPrediction(width, height, bits, image, argb_scratch, argb, |
710 image, argb_scratch, argb, low_effort, exact); | 918 low_effort, max_quantization, exact, |
| 919 used_subtract_green); |
711 } | 920 } |
712 | 921 |
713 void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels) { | 922 void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels) { |
714 int i; | 923 int i; |
715 for (i = 0; i < num_pixels; ++i) { | 924 for (i = 0; i < num_pixels; ++i) { |
716 const uint32_t argb = argb_data[i]; | 925 const uint32_t argb = argb_data[i]; |
717 const uint32_t green = (argb >> 8) & 0xff; | 926 const uint32_t green = (argb >> 8) & 0xff; |
718 const uint32_t new_r = (((argb >> 16) & 0xff) - green) & 0xff; | 927 const uint32_t new_r = (((argb >> 16) & 0xff) - green) & 0xff; |
719 const uint32_t new_b = ((argb & 0xff) - green) & 0xff; | 928 const uint32_t new_b = ((argb & 0xff) - green) & 0xff; |
720 argb_data[i] = (argb & 0xff00ff00) | (new_r << 16) | new_b; | 929 argb_data[i] = (argb & 0xff00ff00) | (new_r << 16) | new_b; |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1046 } | 1255 } |
1047 ++accumulated_red_histo[(pix >> 16) & 0xff]; | 1256 ++accumulated_red_histo[(pix >> 16) & 0xff]; |
1048 ++accumulated_blue_histo[(pix >> 0) & 0xff]; | 1257 ++accumulated_blue_histo[(pix >> 0) & 0xff]; |
1049 } | 1258 } |
1050 } | 1259 } |
1051 } | 1260 } |
1052 } | 1261 } |
1053 } | 1262 } |
1054 | 1263 |
1055 //------------------------------------------------------------------------------ | 1264 //------------------------------------------------------------------------------ |
| 1265 |
| 1266 static int VectorMismatch(const uint32_t* const array1, |
| 1267 const uint32_t* const array2, int length) { |
| 1268 int match_len = 0; |
| 1269 |
| 1270 while (match_len < length && array1[match_len] == array2[match_len]) { |
| 1271 ++match_len; |
| 1272 } |
| 1273 return match_len; |
| 1274 } |
| 1275 |
1056 // Bundles multiple (1, 2, 4 or 8) pixels into a single pixel. | 1276 // Bundles multiple (1, 2, 4 or 8) pixels into a single pixel. |
1057 void VP8LBundleColorMap(const uint8_t* const row, int width, | 1277 void VP8LBundleColorMap(const uint8_t* const row, int width, |
1058 int xbits, uint32_t* const dst) { | 1278 int xbits, uint32_t* const dst) { |
1059 int x; | 1279 int x; |
1060 if (xbits > 0) { | 1280 if (xbits > 0) { |
1061 const int bit_depth = 1 << (3 - xbits); | 1281 const int bit_depth = 1 << (3 - xbits); |
1062 const int mask = (1 << xbits) - 1; | 1282 const int mask = (1 << xbits) - 1; |
1063 uint32_t code = 0xff000000; | 1283 uint32_t code = 0xff000000; |
1064 for (x = 0; x < width; ++x) { | 1284 for (x = 0; x < width; ++x) { |
1065 const int xsub = x & mask; | 1285 const int xsub = x & mask; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1142 VP8LFastLog2SlowFunc VP8LFastSLog2Slow; | 1362 VP8LFastLog2SlowFunc VP8LFastSLog2Slow; |
1143 | 1363 |
1144 VP8LCostFunc VP8LExtraCost; | 1364 VP8LCostFunc VP8LExtraCost; |
1145 VP8LCostCombinedFunc VP8LExtraCostCombined; | 1365 VP8LCostCombinedFunc VP8LExtraCostCombined; |
1146 VP8LCombinedShannonEntropyFunc VP8LCombinedShannonEntropy; | 1366 VP8LCombinedShannonEntropyFunc VP8LCombinedShannonEntropy; |
1147 | 1367 |
1148 GetEntropyUnrefinedHelperFunc VP8LGetEntropyUnrefinedHelper; | 1368 GetEntropyUnrefinedHelperFunc VP8LGetEntropyUnrefinedHelper; |
1149 | 1369 |
1150 VP8LHistogramAddFunc VP8LHistogramAdd; | 1370 VP8LHistogramAddFunc VP8LHistogramAdd; |
1151 | 1371 |
| 1372 VP8LVectorMismatchFunc VP8LVectorMismatch; |
| 1373 |
1152 extern void VP8LEncDspInitSSE2(void); | 1374 extern void VP8LEncDspInitSSE2(void); |
1153 extern void VP8LEncDspInitSSE41(void); | 1375 extern void VP8LEncDspInitSSE41(void); |
1154 extern void VP8LEncDspInitNEON(void); | 1376 extern void VP8LEncDspInitNEON(void); |
1155 extern void VP8LEncDspInitMIPS32(void); | 1377 extern void VP8LEncDspInitMIPS32(void); |
1156 extern void VP8LEncDspInitMIPSdspR2(void); | 1378 extern void VP8LEncDspInitMIPSdspR2(void); |
1157 | 1379 |
1158 static volatile VP8CPUInfo lossless_enc_last_cpuinfo_used = | 1380 static volatile VP8CPUInfo lossless_enc_last_cpuinfo_used = |
1159 (VP8CPUInfo)&lossless_enc_last_cpuinfo_used; | 1381 (VP8CPUInfo)&lossless_enc_last_cpuinfo_used; |
1160 | 1382 |
1161 WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInit(void) { | 1383 WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInit(void) { |
(...skipping 12 matching lines...) Expand all Loading... |
1174 VP8LFastSLog2Slow = FastSLog2Slow; | 1396 VP8LFastSLog2Slow = FastSLog2Slow; |
1175 | 1397 |
1176 VP8LExtraCost = ExtraCost; | 1398 VP8LExtraCost = ExtraCost; |
1177 VP8LExtraCostCombined = ExtraCostCombined; | 1399 VP8LExtraCostCombined = ExtraCostCombined; |
1178 VP8LCombinedShannonEntropy = CombinedShannonEntropy; | 1400 VP8LCombinedShannonEntropy = CombinedShannonEntropy; |
1179 | 1401 |
1180 VP8LGetEntropyUnrefinedHelper = GetEntropyUnrefinedHelper; | 1402 VP8LGetEntropyUnrefinedHelper = GetEntropyUnrefinedHelper; |
1181 | 1403 |
1182 VP8LHistogramAdd = HistogramAdd; | 1404 VP8LHistogramAdd = HistogramAdd; |
1183 | 1405 |
| 1406 VP8LVectorMismatch = VectorMismatch; |
| 1407 |
1184 // If defined, use CPUInfo() to overwrite some pointers with faster versions. | 1408 // If defined, use CPUInfo() to overwrite some pointers with faster versions. |
1185 if (VP8GetCPUInfo != NULL) { | 1409 if (VP8GetCPUInfo != NULL) { |
1186 #if defined(WEBP_USE_SSE2) | 1410 #if defined(WEBP_USE_SSE2) |
1187 if (VP8GetCPUInfo(kSSE2)) { | 1411 if (VP8GetCPUInfo(kSSE2)) { |
1188 VP8LEncDspInitSSE2(); | 1412 VP8LEncDspInitSSE2(); |
1189 #if defined(WEBP_USE_SSE41) | 1413 #if defined(WEBP_USE_SSE41) |
1190 if (VP8GetCPUInfo(kSSE4_1)) { | 1414 if (VP8GetCPUInfo(kSSE4_1)) { |
1191 VP8LEncDspInitSSE41(); | 1415 VP8LEncDspInitSSE41(); |
1192 } | 1416 } |
1193 #endif | 1417 #endif |
(...skipping 12 matching lines...) Expand all Loading... |
1206 #if defined(WEBP_USE_MIPS_DSP_R2) | 1430 #if defined(WEBP_USE_MIPS_DSP_R2) |
1207 if (VP8GetCPUInfo(kMIPSdspR2)) { | 1431 if (VP8GetCPUInfo(kMIPSdspR2)) { |
1208 VP8LEncDspInitMIPSdspR2(); | 1432 VP8LEncDspInitMIPSdspR2(); |
1209 } | 1433 } |
1210 #endif | 1434 #endif |
1211 } | 1435 } |
1212 lossless_enc_last_cpuinfo_used = VP8GetCPUInfo; | 1436 lossless_enc_last_cpuinfo_used = VP8GetCPUInfo; |
1213 } | 1437 } |
1214 | 1438 |
1215 //------------------------------------------------------------------------------ | 1439 //------------------------------------------------------------------------------ |
OLD | NEW |