• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "../webp/types.h"
20 
21 #include "../utils/utils.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 //------------------------------------------------------------------------------
28 // Decoding
29 
30 // color mapping related functions.
VP8GetARGBIndex(uint32_t idx)31 static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) {
32   return (idx >> 8) & 0xff;
33 }
34 
VP8GetAlphaIndex(uint8_t idx)35 static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) {
36   return idx;
37 }
38 
VP8GetARGBValue(uint32_t val)39 static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) {
40   return val;
41 }
42 
VP8GetAlphaValue(uint32_t val)43 static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) {
44   return (val >> 8) & 0xff;
45 }
46 
47 //------------------------------------------------------------------------------
48 // Misc methods.
49 
50 // Computes sampled size of 'size' when sampling using 'sampling bits'.
VP8LSubSampleSize(uint32_t size,uint32_t sampling_bits)51 static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,
52                                               uint32_t sampling_bits) {
53   return (size + (1 << sampling_bits) - 1) >> sampling_bits;
54 }
55 
56 // Converts near lossless quality into max number of bits shaved off.
VP8LNearLosslessBits(int near_lossless_quality)57 static WEBP_INLINE int VP8LNearLosslessBits(int near_lossless_quality) {
58   //    100 -> 0
59   // 80..99 -> 1
60   // 60..79 -> 2
61   // 40..59 -> 3
62   // 20..39 -> 4
63   //  0..19 -> 5
64   return 5 - near_lossless_quality / 20;
65 }
66 
67 // -----------------------------------------------------------------------------
68 // Faster logarithm for integers. Small values use a look-up table.
69 
70 // The threshold till approximate version of log_2 can be used.
71 // Practically, we can get rid of the call to log() as the two values match to
72 // very high degree (the ratio of these two is 0.99999x).
73 // Keeping a high threshold for now.
74 #define APPROX_LOG_WITH_CORRECTION_MAX  65536
75 #define APPROX_LOG_MAX                   4096
76 #define LOG_2_RECIPROCAL 1.44269504088896338700465094007086
77 #define LOG_LOOKUP_IDX_MAX 256
78 extern const float kLog2Table[LOG_LOOKUP_IDX_MAX];
79 extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX];
80 typedef float (*VP8LFastLog2SlowFunc)(uint32_t v);
81 
82 extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;
83 extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow;
84 
VP8LFastLog2(uint32_t v)85 static WEBP_INLINE float VP8LFastLog2(uint32_t v) {
86   return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);
87 }
88 // Fast calculation of v * log2(v) for integer input.
VP8LFastSLog2(uint32_t v)89 static WEBP_INLINE float VP8LFastSLog2(uint32_t v) {
90   return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);
91 }
92 
93 // -----------------------------------------------------------------------------
94 // PrefixEncode()
95 
VP8LBitsLog2Ceiling(uint32_t n)96 static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) {
97   const int log_floor = BitsLog2Floor(n);
98   if (n == (n & ~(n - 1))) {  // zero or a power of two.
99     return log_floor;
100   }
101   return log_floor + 1;
102 }
103 
104 // Splitting of distance and length codes into prefixes and
105 // extra bits. The prefixes are encoded with an entropy code
106 // while the extra bits are stored just as normal bits.
VP8LPrefixEncodeBitsNoLUT(int distance,int * const code,int * const extra_bits)107 static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,
108                                                   int* const extra_bits) {
109   const int highest_bit = BitsLog2Floor(--distance);
110   const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
111   *extra_bits = highest_bit - 1;
112   *code = 2 * highest_bit + second_highest_bit;
113 }
114 
VP8LPrefixEncodeNoLUT(int distance,int * const code,int * const extra_bits,int * const extra_bits_value)115 static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,
116                                               int* const extra_bits,
117                                               int* const extra_bits_value) {
118   const int highest_bit = BitsLog2Floor(--distance);
119   const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
120   *extra_bits = highest_bit - 1;
121   *extra_bits_value = distance & ((1 << *extra_bits) - 1);
122   *code = 2 * highest_bit + second_highest_bit;
123 }
124 
125 #define PREFIX_LOOKUP_IDX_MAX   512
126 typedef struct {
127   int8_t code_;
128   int8_t extra_bits_;
129 } VP8LPrefixCode;
130 
131 // These tables are derived using VP8LPrefixEncodeNoLUT.
132 extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];
133 extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];
VP8LPrefixEncodeBits(int distance,int * const code,int * const extra_bits)134 static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,
135                                              int* const extra_bits) {
136   if (distance < PREFIX_LOOKUP_IDX_MAX) {
137     const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
138     *code = prefix_code.code_;
139     *extra_bits = prefix_code.extra_bits_;
140   } else {
141     VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);
142   }
143 }
144 
VP8LPrefixEncode(int distance,int * const code,int * const extra_bits,int * const extra_bits_value)145 static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,
146                                          int* const extra_bits,
147                                          int* const extra_bits_value) {
148   if (distance < PREFIX_LOOKUP_IDX_MAX) {
149     const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
150     *code = prefix_code.code_;
151     *extra_bits = prefix_code.extra_bits_;
152     *extra_bits_value = kPrefixEncodeExtraBitsValue[distance];
153   } else {
154     VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);
155   }
156 }
157 
158 // Sum of each component, mod 256.
159 static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
VP8LAddPixels(uint32_t a,uint32_t b)160 uint32_t VP8LAddPixels(uint32_t a, uint32_t b) {
161   const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u);
162   const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu);
163   return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
164 }
165 
166 // Difference of each component, mod 256.
167 static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
VP8LSubPixels(uint32_t a,uint32_t b)168 uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {
169   const uint32_t alpha_and_green =
170       0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);
171   const uint32_t red_and_blue =
172       0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);
173   return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
174 }
175 
176 //------------------------------------------------------------------------------
177 // Transform-related functions use din both encoding and decoding.
178 
179 // Macros used to create a batch predictor that iteratively uses a
180 // one-pixel predictor.
181 
182 // The predictor is added to the output pixel (which
183 // is therefore considered as a residual) to get the final prediction.
184 #define GENERATE_PREDICTOR_ADD(PREDICTOR, PREDICTOR_ADD)             \
185 static void PREDICTOR_ADD(const uint32_t* in, const uint32_t* upper, \
186                           int num_pixels, uint32_t* out) {           \
187   int x;                                                             \
188   for (x = 0; x < num_pixels; ++x) {                                 \
189     const uint32_t pred = (PREDICTOR)(out[x - 1], upper + x);        \
190     out[x] = VP8LAddPixels(in[x], pred);                             \
191   }                                                                  \
192 }
193 
194 // It subtracts the prediction from the input pixel and stores the residual
195 // in the output pixel.
196 #define GENERATE_PREDICTOR_SUB(PREDICTOR, PREDICTOR_SUB)             \
197 static void PREDICTOR_SUB(const uint32_t* in, const uint32_t* upper, \
198                           int num_pixels, uint32_t* out) {           \
199   int x;                                                             \
200   for (x = 0; x < num_pixels; ++x) {                                 \
201     const uint32_t pred = (PREDICTOR)(in[x - 1], upper + x);         \
202     out[x] = VP8LSubPixels(in[x], pred);                             \
203   }                                                                  \
204 }
205 
206 #ifdef __cplusplus
207 }    // extern "C"
208 #endif
209 
210 #endif  // WEBP_DSP_LOSSLESS_COMMON_H_
211