OLD | NEW |
1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 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 // Author: Jyrki Alakuijala (jyrki@google.com) | 10 // Author: Jyrki Alakuijala (jyrki@google.com) |
11 // | 11 // |
12 // Entropy encoding (Huffman) for webp lossless. | 12 // Entropy encoding (Huffman) for webp lossless. |
13 | 13 |
14 #include <assert.h> | 14 #include <assert.h> |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 #include <string.h> | 16 #include <string.h> |
17 #include "./huffman_encode.h" | 17 #include "./huffman_encode.h" |
18 #include "../utils/utils.h" | 18 #include "../utils/utils.h" |
19 #include "../webp/format_constants.h" | 19 #include "../webp/format_constants.h" |
20 | 20 |
21 // ----------------------------------------------------------------------------- | 21 // ----------------------------------------------------------------------------- |
22 // Util function to optimize the symbol map for RLE coding | 22 // Util function to optimize the symbol map for RLE coding |
23 | 23 |
24 // Heuristics for selecting the stride ranges to collapse. | 24 // Heuristics for selecting the stride ranges to collapse. |
25 static int ValuesShouldBeCollapsedToStrideAverage(int a, int b) { | 25 static int ValuesShouldBeCollapsedToStrideAverage(int a, int b) { |
26 return abs(a - b) < 4; | 26 return abs(a - b) < 4; |
27 } | 27 } |
28 | 28 |
29 // Change the population counts in a way that the consequent | 29 // Change the population counts in a way that the consequent |
30 // Hufmann tree compression, especially its RLE-part, give smaller output. | 30 // Huffman tree compression, especially its RLE-part, give smaller output. |
31 static int OptimizeHuffmanForRle(int length, int* const counts) { | 31 static int OptimizeHuffmanForRle(int length, int* const counts) { |
32 uint8_t* good_for_rle; | 32 uint8_t* good_for_rle; |
33 // 1) Let's make the Huffman code more compatible with rle encoding. | 33 // 1) Let's make the Huffman code more compatible with rle encoding. |
34 int i; | 34 int i; |
35 for (; length >= 0; --length) { | 35 for (; length >= 0; --length) { |
36 if (length == 0) { | 36 if (length == 0) { |
37 return 1; // All zeros. | 37 return 1; // All zeros. |
38 } | 38 } |
39 if (counts[length - 1] != 0) { | 39 if (counts[length - 1] != 0) { |
40 // Now counts[0..length - 1] does not have trailing zeros. | 40 // Now counts[0..length - 1] does not have trailing zeros. |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 return 0; | 431 return 0; |
432 } | 432 } |
433 if (!GenerateOptimalTree(histogram, num_symbols, | 433 if (!GenerateOptimalTree(histogram, num_symbols, |
434 tree_depth_limit, tree->code_lengths)) { | 434 tree_depth_limit, tree->code_lengths)) { |
435 return 0; | 435 return 0; |
436 } | 436 } |
437 // Create the actual bit codes for the bit lengths. | 437 // Create the actual bit codes for the bit lengths. |
438 ConvertBitDepthsToSymbols(tree); | 438 ConvertBitDepthsToSymbols(tree); |
439 return 1; | 439 return 1; |
440 } | 440 } |
OLD | NEW |