OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // This code is licensed under the same terms as WebM: | 3 // This code is licensed under the same terms as WebM: |
4 // Software License Agreement: http://www.webmproject.org/license/software/ | 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ | 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
6 // ----------------------------------------------------------------------------- | 6 // ----------------------------------------------------------------------------- |
7 // | 7 // |
8 // Author: Jyrki Alakuijala (jyrki@google.com) | 8 // Author: Jyrki Alakuijala (jyrki@google.com) |
9 // | 9 // |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 #define PIX_OR_COPY_CODES_MAX \ | 28 #define PIX_OR_COPY_CODES_MAX \ |
29 (NUM_LITERAL_CODES + NUM_LENGTH_CODES + (1 << MAX_COLOR_CACHE_BITS)) | 29 (NUM_LITERAL_CODES + NUM_LENGTH_CODES + (1 << MAX_COLOR_CACHE_BITS)) |
30 | 30 |
31 // ----------------------------------------------------------------------------- | 31 // ----------------------------------------------------------------------------- |
32 // PrefixEncode() | 32 // PrefixEncode() |
33 | 33 |
34 // use GNU builtins where available. | 34 // use GNU builtins where available. |
35 #if defined(__GNUC__) && \ | 35 #if defined(__GNUC__) && \ |
36 ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) | 36 ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) |
37 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | 37 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { |
38 return n == 0 ? -1 : 31 ^ __builtin_clz(n); | 38 assert(n != 0); |
| 39 return 31 ^ __builtin_clz(n); |
39 } | 40 } |
40 #elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) | 41 #elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) |
41 #include <intrin.h> | 42 #include <intrin.h> |
42 #pragma intrinsic(_BitScanReverse) | 43 #pragma intrinsic(_BitScanReverse) |
43 | 44 |
44 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | 45 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { |
45 unsigned long first_set_bit; | 46 unsigned long first_set_bit; |
46 return _BitScanReverse(&first_set_bit, n) ? first_set_bit : -1; | 47 assert(n != 0); |
| 48 _BitScanReverse(&first_set_bit, n); |
| 49 return first_set_bit; |
47 } | 50 } |
48 #else | 51 #else |
| 52 // Returns (int)floor(log2(n)). n must be > 0. |
49 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | 53 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { |
50 int log = 0; | 54 int log = 0; |
51 uint32_t value = n; | 55 uint32_t value = n; |
52 int i; | 56 int i; |
53 | 57 |
54 if (value == 0) return -1; | 58 assert(n != 0); |
55 for (i = 4; i >= 0; --i) { | 59 for (i = 4; i >= 0; --i) { |
56 const int shift = (1 << i); | 60 const int shift = (1 << i); |
57 const uint32_t x = value >> shift; | 61 const uint32_t x = value >> shift; |
58 if (x != 0) { | 62 if (x != 0) { |
59 value = x; | 63 value = x; |
60 log += shift; | 64 log += shift; |
61 } | 65 } |
62 } | 66 } |
63 return log; | 67 return log; |
64 } | 68 } |
65 #endif | 69 #endif |
66 | 70 |
67 static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) { | 71 static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) { |
68 const int floor = BitsLog2Floor(n); | 72 const int log_floor = BitsLog2Floor(n); |
69 if (n == (n & ~(n - 1))) // zero or a power of two. | 73 if (n == (n & ~(n - 1))) // zero or a power of two. |
70 return floor; | 74 return log_floor; |
71 else | 75 else |
72 return floor + 1; | 76 return log_floor + 1; |
73 } | 77 } |
74 | 78 |
75 // Splitting of distance and length codes into prefixes and | 79 // Splitting of distance and length codes into prefixes and |
76 // extra bits. The prefixes are encoded with an entropy code | 80 // extra bits. The prefixes are encoded with an entropy code |
77 // while the extra bits are stored just as normal bits. | 81 // while the extra bits are stored just as normal bits. |
78 static WEBP_INLINE void PrefixEncode(int distance, int* const code, | 82 static WEBP_INLINE void PrefixEncode(int distance, int* const code, |
79 int* const extra_bits_count, | 83 int* const extra_bits_count, |
80 int* const extra_bits_value) { | 84 int* const extra_bits_value) { |
81 // Collect the two most significant bits where the highest bit is 1. | 85 if (distance > 2) { // Collect the two most significant bits. |
82 const int highest_bit = BitsLog2Floor(--distance); | 86 const int highest_bit = BitsLog2Floor(--distance); |
83 // & 0x3f is to make behavior well defined when highest_bit | 87 const int second_highest_bit = (distance >> (highest_bit - 1)) & 1; |
84 // does not exist or is the least significant bit. | 88 *extra_bits_count = highest_bit - 1; |
85 const int second_highest_bit = | 89 *extra_bits_value = distance & ((1 << *extra_bits_count) - 1); |
86 (distance >> ((highest_bit - 1) & 0x3f)) & 1; | 90 *code = 2 * highest_bit + second_highest_bit; |
87 *extra_bits_count = (highest_bit > 0) ? (highest_bit - 1) : 0; | 91 } else { |
88 *extra_bits_value = distance & ((1 << *extra_bits_count) - 1); | 92 *extra_bits_count = 0; |
89 *code = (highest_bit > 0) ? (2 * highest_bit + second_highest_bit) | 93 *extra_bits_value = 0; |
90 : (highest_bit == 0) ? 1 : 0; | 94 *code = (distance == 2) ? 1 : 0; |
| 95 } |
91 } | 96 } |
92 | 97 |
93 // ----------------------------------------------------------------------------- | 98 // ----------------------------------------------------------------------------- |
94 // PixOrCopy | 99 // PixOrCopy |
95 | 100 |
96 enum Mode { | 101 enum Mode { |
97 kLiteral, | 102 kLiteral, |
98 kCacheIdx, | 103 kCacheIdx, |
99 kCopy, | 104 kCopy, |
100 kNone | 105 kNone |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 // Produce an estimate for a good color cache size for the image. | 208 // Produce an estimate for a good color cache size for the image. |
204 int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb, | 209 int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb, |
205 int xsize, int ysize, | 210 int xsize, int ysize, |
206 int* const best_cache_bits); | 211 int* const best_cache_bits); |
207 | 212 |
208 #if defined(__cplusplus) || defined(c_plusplus) | 213 #if defined(__cplusplus) || defined(c_plusplus) |
209 } | 214 } |
210 #endif | 215 #endif |
211 | 216 |
212 #endif // WEBP_ENC_BACKWARD_REFERENCES_H_ | 217 #endif // WEBP_ENC_BACKWARD_REFERENCES_H_ |
OLD | NEW |