• 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 
15 #ifndef WEBP_DSP_LOSSLESS_H_
16 #define WEBP_DSP_LOSSLESS_H_
17 
18 #include "../webp/types.h"
19 #include "../webp/decode.h"
20 
21 #include "../enc/histogram.h"
22 #include "../utils/utils.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 //------------------------------------------------------------------------------
29 // Signatures and generic function-pointers
30 
31 typedef uint32_t (*VP8LPredictorFunc)(uint32_t left, const uint32_t* const top);
32 extern VP8LPredictorFunc VP8LPredictors[16];
33 
34 typedef void (*VP8LProcessBlueAndRedFunc)(uint32_t* argb_data, int num_pixels);
35 extern VP8LProcessBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed;
36 extern VP8LProcessBlueAndRedFunc VP8LAddGreenToBlueAndRed;
37 
38 typedef struct {
39   // Note: the members are uint8_t, so that any negative values are
40   // automatically converted to "mod 256" values.
41   uint8_t green_to_red_;
42   uint8_t green_to_blue_;
43   uint8_t red_to_blue_;
44 } VP8LMultipliers;
45 typedef void (*VP8LTransformColorFunc)(const VP8LMultipliers* const m,
46                                        uint32_t* argb_data, int num_pixels);
47 extern VP8LTransformColorFunc VP8LTransformColor;
48 extern VP8LTransformColorFunc VP8LTransformColorInverse;
49 
50 typedef void (*VP8LConvertFunc)(const uint32_t* src, int num_pixels,
51                                 uint8_t* dst);
52 extern VP8LConvertFunc VP8LConvertBGRAToRGB;
53 extern VP8LConvertFunc VP8LConvertBGRAToRGBA;
54 extern VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
55 extern VP8LConvertFunc VP8LConvertBGRAToRGB565;
56 extern VP8LConvertFunc VP8LConvertBGRAToBGR;
57 
58 // Expose some C-only fallback functions
59 extern void VP8LTransformColor_C(const VP8LMultipliers* const m,
60                                  uint32_t* data, int num_pixels);
61 extern void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
62                                         uint32_t* data, int num_pixels);
63 
64 extern void VP8LConvertBGRAToRGB_C(const uint32_t* src,
65                                    int num_pixels, uint8_t* dst);
66 extern void VP8LConvertBGRAToRGBA_C(const uint32_t* src,
67                                     int num_pixels, uint8_t* dst);
68 extern void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src,
69                                         int num_pixels, uint8_t* dst);
70 extern void VP8LConvertBGRAToRGB565_C(const uint32_t* src,
71                                       int num_pixels, uint8_t* dst);
72 extern void VP8LConvertBGRAToBGR_C(const uint32_t* src,
73                                    int num_pixels, uint8_t* dst);
74 extern void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data,
75                                               int num_pixels);
76 extern void VP8LAddGreenToBlueAndRed_C(uint32_t* data, int num_pixels);
77 
78 // Must be called before calling any of the above methods.
79 void VP8LDspInit(void);
80 
81 //------------------------------------------------------------------------------
82 // Image transforms.
83 
84 struct VP8LTransform;  // Defined in dec/vp8li.h.
85 
86 // Performs inverse transform of data given transform information, start and end
87 // rows. Transform will be applied to rows [row_start, row_end[.
88 // The *in and *out pointers refer to source and destination data respectively
89 // corresponding to the intermediate row (row_start).
90 void VP8LInverseTransform(const struct VP8LTransform* const transform,
91                           int row_start, int row_end,
92                           const uint32_t* const in, uint32_t* const out);
93 
94 // Similar to the static method ColorIndexInverseTransform() that is part of
95 // lossless.c, but used only for alpha decoding. It takes uint8_t (rather than
96 // uint32_t) arguments for 'src' and 'dst'.
97 void VP8LColorIndexInverseTransformAlpha(
98     const struct VP8LTransform* const transform, int y_start, int y_end,
99     const uint8_t* src, uint8_t* dst);
100 
101 void VP8LResidualImage(int width, int height, int bits,
102                        uint32_t* const argb, uint32_t* const argb_scratch,
103                        uint32_t* const image);
104 
105 void VP8LColorSpaceTransform(int width, int height, int bits, int quality,
106                              uint32_t* const argb, uint32_t* image);
107 
108 //------------------------------------------------------------------------------
109 // Color space conversion.
110 
111 // Converts from BGRA to other color spaces.
112 void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
113                          WEBP_CSP_MODE out_colorspace, uint8_t* const rgba);
114 
115 //------------------------------------------------------------------------------
116 // Misc methods.
117 
118 // Computes sampled size of 'size' when sampling using 'sampling bits'.
VP8LSubSampleSize(uint32_t size,uint32_t sampling_bits)119 static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,
120                                               uint32_t sampling_bits) {
121   return (size + (1 << sampling_bits) - 1) >> sampling_bits;
122 }
123 
124 // -----------------------------------------------------------------------------
125 // Faster logarithm for integers. Small values use a look-up table.
126 #define LOG_LOOKUP_IDX_MAX 256
127 extern const float kLog2Table[LOG_LOOKUP_IDX_MAX];
128 extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX];
129 typedef float (*VP8LFastLog2SlowFunc)(uint32_t v);
130 
131 extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;
132 extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow;
133 
VP8LFastLog2(uint32_t v)134 static WEBP_INLINE float VP8LFastLog2(uint32_t v) {
135   return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);
136 }
137 // Fast calculation of v * log2(v) for integer input.
VP8LFastSLog2(uint32_t v)138 static WEBP_INLINE float VP8LFastSLog2(uint32_t v) {
139   return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);
140 }
141 
142 // -----------------------------------------------------------------------------
143 // Huffman-cost related functions.
144 
145 typedef double (*VP8LCostFunc)(const uint32_t* population, int length);
146 typedef double (*VP8LCostCombinedFunc)(const uint32_t* X, const uint32_t* Y,
147                                        int length);
148 
149 extern VP8LCostFunc VP8LExtraCost;
150 extern VP8LCostCombinedFunc VP8LExtraCostCombined;
151 
152 typedef struct {        // small struct to hold counters
153   int counts[2];        // index: 0=zero steak, 1=non-zero streak
154   int streaks[2][2];    // [zero/non-zero][streak<3 / streak>=3]
155 } VP8LStreaks;
156 
157 typedef VP8LStreaks (*VP8LCostCountFunc)(const uint32_t* population,
158                                          int length);
159 typedef VP8LStreaks (*VP8LCostCombinedCountFunc)(const uint32_t* X,
160                                                  const uint32_t* Y, int length);
161 
162 extern VP8LCostCountFunc VP8LHuffmanCostCount;
163 extern VP8LCostCombinedCountFunc VP8LHuffmanCostCombinedCount;
164 
165 typedef void (*VP8LHistogramAddFunc)(const VP8LHistogram* const a,
166                                      const VP8LHistogram* const b,
167                                      VP8LHistogram* const out);
168 extern VP8LHistogramAddFunc VP8LHistogramAdd;
169 
170 // -----------------------------------------------------------------------------
171 // PrefixEncode()
172 
VP8LBitsLog2Ceiling(uint32_t n)173 static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) {
174   const int log_floor = BitsLog2Floor(n);
175   if (n == (n & ~(n - 1)))  // zero or a power of two.
176     return log_floor;
177   else
178     return log_floor + 1;
179 }
180 
181 // Splitting of distance and length codes into prefixes and
182 // extra bits. The prefixes are encoded with an entropy code
183 // while the extra bits are stored just as normal bits.
VP8LPrefixEncodeBitsNoLUT(int distance,int * const code,int * const extra_bits)184 static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,
185                                                   int* const extra_bits) {
186   const int highest_bit = BitsLog2Floor(--distance);
187   const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
188   *extra_bits = highest_bit - 1;
189   *code = 2 * highest_bit + second_highest_bit;
190 }
191 
VP8LPrefixEncodeNoLUT(int distance,int * const code,int * const extra_bits,int * const extra_bits_value)192 static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,
193                                               int* const extra_bits,
194                                               int* const extra_bits_value) {
195   const int highest_bit = BitsLog2Floor(--distance);
196   const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
197   *extra_bits = highest_bit - 1;
198   *extra_bits_value = distance & ((1 << *extra_bits) - 1);
199   *code = 2 * highest_bit + second_highest_bit;
200 }
201 
202 #define PREFIX_LOOKUP_IDX_MAX   512
203 typedef struct {
204   int8_t code_;
205   int8_t extra_bits_;
206 } VP8LPrefixCode;
207 
208 // These tables are derived using VP8LPrefixEncodeNoLUT.
209 extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];
210 extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];
VP8LPrefixEncodeBits(int distance,int * const code,int * const extra_bits)211 static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,
212                                              int* const extra_bits) {
213   if (distance < PREFIX_LOOKUP_IDX_MAX) {
214     const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
215     *code = prefix_code.code_;
216     *extra_bits = prefix_code.extra_bits_;
217   } else {
218     VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);
219   }
220 }
221 
VP8LPrefixEncode(int distance,int * const code,int * const extra_bits,int * const extra_bits_value)222 static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,
223                                          int* const extra_bits,
224                                          int* const extra_bits_value) {
225   if (distance < PREFIX_LOOKUP_IDX_MAX) {
226     const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
227     *code = prefix_code.code_;
228     *extra_bits = prefix_code.extra_bits_;
229     *extra_bits_value = kPrefixEncodeExtraBitsValue[distance];
230   } else {
231     VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);
232   }
233 }
234 
235 // In-place difference of each component with mod 256.
VP8LSubPixels(uint32_t a,uint32_t b)236 static WEBP_INLINE uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {
237   const uint32_t alpha_and_green =
238       0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);
239   const uint32_t red_and_blue =
240       0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);
241   return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
242 }
243 
244 void VP8LBundleColorMap(const uint8_t* const row, int width,
245                         int xbits, uint32_t* const dst);
246 
247 //------------------------------------------------------------------------------
248 
249 #ifdef __cplusplus
250 }    // extern "C"
251 #endif
252 
253 #endif  // WEBP_DSP_LOSSLESS_H_
254