lossless_common.h (8435B)
1 // Copyright 2012 Google Inc. All Rights Reserved. 2 // 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 5 // tree. An additional intellectual property rights grant can be found 6 // in the file PATENTS. All contributing project authors may 7 // be found in the AUTHORS file in the root of the source tree. 8 // ----------------------------------------------------------------------------- 9 // 10 // Image transforms and color space conversion methods for lossless decoder. 11 // 12 // Authors: Vikas Arora (vikaas.arora@gmail.com) 13 // Jyrki Alakuijala (jyrki@google.com) 14 // Vincent Rabaud (vrabaud@google.com) 15 16 #ifndef WEBP_DSP_LOSSLESS_COMMON_H_ 17 #define WEBP_DSP_LOSSLESS_COMMON_H_ 18 19 #include <assert.h> 20 #include <stddef.h> 21 22 #include "src/dsp/cpu.h" 23 #include "src/utils/utils.h" 24 #include "src/webp/types.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 //------------------------------------------------------------------------------ 31 // Decoding 32 33 // color mapping related functions. 34 static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) { 35 return (idx >> 8) & 0xff; 36 } 37 38 static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) { 39 return idx; 40 } 41 42 static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) { 43 return val; 44 } 45 46 static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) { 47 return (val >> 8) & 0xff; 48 } 49 50 //------------------------------------------------------------------------------ 51 // Misc methods. 52 53 // Computes sampled size of 'size' when sampling using 'sampling bits'. 54 static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size, 55 uint32_t sampling_bits) { 56 return (size + (1 << sampling_bits) - 1) >> sampling_bits; 57 } 58 59 // Converts near lossless quality into max number of bits shaved off. 60 static WEBP_INLINE int VP8LNearLosslessBits(int near_lossless_quality) { 61 // 100 -> 0 62 // 80..99 -> 1 63 // 60..79 -> 2 64 // 40..59 -> 3 65 // 20..39 -> 4 66 // 0..19 -> 5 67 return 5 - near_lossless_quality / 20; 68 } 69 70 // ----------------------------------------------------------------------------- 71 // Faster logarithm for integers. Small values use a look-up table. 72 73 // The threshold till approximate version of log_2 can be used. 74 // Practically, we can get rid of the call to log() as the two values match to 75 // very high degree (the ratio of these two is 0.99999x). 76 // Keeping a high threshold for now. 77 #define APPROX_LOG_WITH_CORRECTION_MAX 65536 78 #define APPROX_LOG_MAX 4096 79 // VP8LFastLog2 and VP8LFastSLog2 are used on elements from image histograms. 80 // The histogram values cannot exceed the maximum number of pixels, which 81 // is (1 << 14) * (1 << 14). Therefore S * log(S) < (1 << 33). 82 // No more than 32 bits of precision should be chosen. 83 // To match the original float implementation, 23 bits of precision are used. 84 #define LOG_2_PRECISION_BITS 23 85 #define LOG_2_RECIPROCAL 1.44269504088896338700465094007086 86 // LOG_2_RECIPROCAL * (1 << LOG_2_PRECISION_BITS) 87 #define LOG_2_RECIPROCAL_FIXED_DOUBLE 12102203.161561485379934310913085937500 88 #define LOG_2_RECIPROCAL_FIXED ((uint64_t)12102203) 89 #define LOG_LOOKUP_IDX_MAX 256 90 extern const uint32_t kLog2Table[LOG_LOOKUP_IDX_MAX]; 91 extern const uint64_t kSLog2Table[LOG_LOOKUP_IDX_MAX]; 92 typedef uint32_t (*VP8LFastLog2SlowFunc)(uint32_t v); 93 typedef uint64_t (*VP8LFastSLog2SlowFunc)(uint32_t v); 94 95 extern VP8LFastLog2SlowFunc VP8LFastLog2Slow; 96 extern VP8LFastSLog2SlowFunc VP8LFastSLog2Slow; 97 98 static WEBP_INLINE uint32_t VP8LFastLog2(uint32_t v) { 99 return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v); 100 } 101 // Fast calculation of v * log2(v) for integer input. 102 static WEBP_INLINE uint64_t VP8LFastSLog2(uint32_t v) { 103 return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v); 104 } 105 106 static WEBP_INLINE uint64_t RightShiftRound(uint64_t v, uint32_t shift) { 107 return (v + (1ull << shift >> 1)) >> shift; 108 } 109 110 static WEBP_INLINE int64_t DivRound(int64_t a, int64_t b) { 111 return ((a < 0) == (b < 0)) ? ((a + b / 2) / b) : ((a - b / 2) / b); 112 } 113 114 #define WEBP_INT64_MAX ((int64_t)((1ull << 63) - 1)) 115 #define WEBP_UINT64_MAX (~0ull) 116 117 // ----------------------------------------------------------------------------- 118 // PrefixEncode() 119 120 // Splitting of distance and length codes into prefixes and 121 // extra bits. The prefixes are encoded with an entropy code 122 // while the extra bits are stored just as normal bits. 123 static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code, 124 int* const extra_bits) { 125 const int highest_bit = BitsLog2Floor(--distance); 126 const int second_highest_bit = (distance >> (highest_bit - 1)) & 1; 127 *extra_bits = highest_bit - 1; 128 *code = 2 * highest_bit + second_highest_bit; 129 } 130 131 static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code, 132 int* const extra_bits, 133 int* const extra_bits_value) { 134 const int highest_bit = BitsLog2Floor(--distance); 135 const int second_highest_bit = (distance >> (highest_bit - 1)) & 1; 136 *extra_bits = highest_bit - 1; 137 *extra_bits_value = distance & ((1 << *extra_bits) - 1); 138 *code = 2 * highest_bit + second_highest_bit; 139 } 140 141 #define PREFIX_LOOKUP_IDX_MAX 512 142 typedef struct { 143 int8_t code; 144 int8_t extra_bits; 145 } VP8LPrefixCode; 146 147 // These tables are derived using VP8LPrefixEncodeNoLUT. 148 extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX]; 149 extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX]; 150 static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code, 151 int* const extra_bits) { 152 if (distance < PREFIX_LOOKUP_IDX_MAX) { 153 const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance]; 154 *code = prefix_code.code; 155 *extra_bits = prefix_code.extra_bits; 156 } else { 157 VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits); 158 } 159 } 160 161 static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code, 162 int* const extra_bits, 163 int* const extra_bits_value) { 164 if (distance < PREFIX_LOOKUP_IDX_MAX) { 165 const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance]; 166 *code = prefix_code.code; 167 *extra_bits = prefix_code.extra_bits; 168 *extra_bits_value = kPrefixEncodeExtraBitsValue[distance]; 169 } else { 170 VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value); 171 } 172 } 173 174 // Sum of each component, mod 256. 175 static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE 176 uint32_t VP8LAddPixels(uint32_t a, uint32_t b) { 177 const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u); 178 const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu); 179 return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu); 180 } 181 182 // Difference of each component, mod 256. 183 static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE 184 uint32_t VP8LSubPixels(uint32_t a, uint32_t b) { 185 const uint32_t alpha_and_green = 186 0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u); 187 const uint32_t red_and_blue = 188 0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu); 189 return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu); 190 } 191 192 //------------------------------------------------------------------------------ 193 // Transform-related functions used in both encoding and decoding. 194 195 // Macros used to create a batch predictor that iteratively uses a 196 // one-pixel predictor. 197 198 // The predictor is added to the output pixel (which 199 // is therefore considered as a residual) to get the final prediction. 200 #define GENERATE_PREDICTOR_ADD(PREDICTOR, PREDICTOR_ADD) \ 201 static void PREDICTOR_ADD(const uint32_t* in, const uint32_t* upper, \ 202 int num_pixels, uint32_t* WEBP_RESTRICT out) { \ 203 int x; \ 204 assert(upper != NULL); \ 205 for (x = 0; x < num_pixels; ++x) { \ 206 const uint32_t pred = (PREDICTOR)(&out[x - 1], upper + x); \ 207 out[x] = VP8LAddPixels(in[x], pred); \ 208 } \ 209 } 210 211 #ifdef __cplusplus 212 } // extern "C" 213 #endif 214 215 #endif // WEBP_DSP_LOSSLESS_COMMON_H_