| OLD | NEW |
| 1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 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 | 12 |
| 13 #ifndef WEBP_ENC_BACKWARD_REFERENCES_H_ | 13 #ifndef WEBP_ENC_BACKWARD_REFERENCES_H_ |
| 14 #define WEBP_ENC_BACKWARD_REFERENCES_H_ | 14 #define WEBP_ENC_BACKWARD_REFERENCES_H_ |
| 15 | 15 |
| 16 #include <assert.h> | 16 #include <assert.h> |
| 17 #include <stdlib.h> | 17 #include <stdlib.h> |
| 18 #include "../webp/types.h" | 18 #include "../webp/types.h" |
| 19 #include "../webp/format_constants.h" | 19 #include "../webp/format_constants.h" |
| 20 | 20 |
| 21 #if defined(__cplusplus) || defined(c_plusplus) | 21 #ifdef __cplusplus |
| 22 extern "C" { | 22 extern "C" { |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 // The spec allows 11, we use 9 bits to reduce memory consumption in encoding. | 25 // The spec allows 11, we use 9 bits to reduce memory consumption in encoding. |
| 26 // Having 9 instead of 11 only removes about 0.25 % of compression density. | 26 // Having 9 instead of 11 only removes about 0.25 % of compression density. |
| 27 #define MAX_COLOR_CACHE_BITS 9 | 27 #define MAX_COLOR_CACHE_BITS 9 |
| 28 | 28 |
| 29 // Max ever number of codes we'll use: | 29 // Max ever number of codes we'll use: |
| 30 #define PIX_OR_COPY_CODES_MAX \ | 30 #define PIX_OR_COPY_CODES_MAX \ |
| 31 (NUM_LITERAL_CODES + NUM_LENGTH_CODES + (1 << MAX_COLOR_CACHE_BITS)) | 31 (NUM_LITERAL_CODES + NUM_LENGTH_CODES + (1 << MAX_COLOR_CACHE_BITS)) |
| 32 | 32 |
| 33 // ----------------------------------------------------------------------------- | 33 // ----------------------------------------------------------------------------- |
| 34 // PrefixEncode() | |
| 35 | |
| 36 // use GNU builtins where available. | |
| 37 #if defined(__GNUC__) && \ | |
| 38 ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) | |
| 39 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | |
| 40 assert(n != 0); | |
| 41 return 31 ^ __builtin_clz(n); | |
| 42 } | |
| 43 #elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) | |
| 44 #include <intrin.h> | |
| 45 #pragma intrinsic(_BitScanReverse) | |
| 46 | |
| 47 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | |
| 48 unsigned long first_set_bit; | |
| 49 assert(n != 0); | |
| 50 _BitScanReverse(&first_set_bit, n); | |
| 51 return first_set_bit; | |
| 52 } | |
| 53 #else | |
| 54 // Returns (int)floor(log2(n)). n must be > 0. | |
| 55 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { | |
| 56 int log = 0; | |
| 57 uint32_t value = n; | |
| 58 int i; | |
| 59 | |
| 60 assert(n != 0); | |
| 61 for (i = 4; i >= 0; --i) { | |
| 62 const int shift = (1 << i); | |
| 63 const uint32_t x = value >> shift; | |
| 64 if (x != 0) { | |
| 65 value = x; | |
| 66 log += shift; | |
| 67 } | |
| 68 } | |
| 69 return log; | |
| 70 } | |
| 71 #endif | |
| 72 | |
| 73 static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) { | |
| 74 const int log_floor = BitsLog2Floor(n); | |
| 75 if (n == (n & ~(n - 1))) // zero or a power of two. | |
| 76 return log_floor; | |
| 77 else | |
| 78 return log_floor + 1; | |
| 79 } | |
| 80 | |
| 81 // Splitting of distance and length codes into prefixes and | |
| 82 // extra bits. The prefixes are encoded with an entropy code | |
| 83 // while the extra bits are stored just as normal bits. | |
| 84 static WEBP_INLINE void PrefixEncode(int distance, int* const code, | |
| 85 int* const extra_bits_count, | |
| 86 int* const extra_bits_value) { | |
| 87 if (distance > 2) { // Collect the two most significant bits. | |
| 88 const int highest_bit = BitsLog2Floor(--distance); | |
| 89 const int second_highest_bit = (distance >> (highest_bit - 1)) & 1; | |
| 90 *extra_bits_count = highest_bit - 1; | |
| 91 *extra_bits_value = distance & ((1 << *extra_bits_count) - 1); | |
| 92 *code = 2 * highest_bit + second_highest_bit; | |
| 93 } else { | |
| 94 *extra_bits_count = 0; | |
| 95 *extra_bits_value = 0; | |
| 96 *code = (distance == 2) ? 1 : 0; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 // ----------------------------------------------------------------------------- | |
| 101 // PixOrCopy | 34 // PixOrCopy |
| 102 | 35 |
| 103 enum Mode { | 36 enum Mode { |
| 104 kLiteral, | 37 kLiteral, |
| 105 kCacheIdx, | 38 kCacheIdx, |
| 106 kCopy, | 39 kCopy, |
| 107 kNone | 40 kNone |
| 108 }; | 41 }; |
| 109 | 42 |
| 110 typedef struct { | 43 typedef struct { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 int VP8LGetBackwardReferences(int width, int height, | 138 int VP8LGetBackwardReferences(int width, int height, |
| 206 const uint32_t* const argb, | 139 const uint32_t* const argb, |
| 207 int quality, int cache_bits, int use_2d_locality, | 140 int quality, int cache_bits, int use_2d_locality, |
| 208 VP8LBackwardRefs* const best); | 141 VP8LBackwardRefs* const best); |
| 209 | 142 |
| 210 // Produce an estimate for a good color cache size for the image. | 143 // Produce an estimate for a good color cache size for the image. |
| 211 int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb, | 144 int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb, |
| 212 int xsize, int ysize, | 145 int xsize, int ysize, |
| 213 int* const best_cache_bits); | 146 int* const best_cache_bits); |
| 214 | 147 |
| 215 #if defined(__cplusplus) || defined(c_plusplus) | 148 #ifdef __cplusplus |
| 216 } | 149 } |
| 217 #endif | 150 #endif |
| 218 | 151 |
| 219 #endif // WEBP_ENC_BACKWARD_REFERENCES_H_ | 152 #endif // WEBP_ENC_BACKWARD_REFERENCES_H_ |
| OLD | NEW |