• 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 "src/webp/types.h"
19 #include "src/webp/decode.h"
20 
21 #include "src/dsp/dsp.h"
22 #include "src/enc/histogram_enc.h"
23 #include "src/utils/utils.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 //------------------------------------------------------------------------------
30 // Decoding
31 
32 typedef uint32_t (*VP8LPredictorFunc)(const uint32_t* const left,
33                                       const uint32_t* const top);
34 extern VP8LPredictorFunc VP8LPredictors[16];
35 
36 uint32_t VP8LPredictor2_C(const uint32_t* const left,
37                           const uint32_t* const top);
38 uint32_t VP8LPredictor3_C(const uint32_t* const left,
39                           const uint32_t* const top);
40 uint32_t VP8LPredictor4_C(const uint32_t* const left,
41                           const uint32_t* const top);
42 uint32_t VP8LPredictor5_C(const uint32_t* const left,
43                           const uint32_t* const top);
44 uint32_t VP8LPredictor6_C(const uint32_t* const left,
45                           const uint32_t* const top);
46 uint32_t VP8LPredictor7_C(const uint32_t* const left,
47                           const uint32_t* const top);
48 uint32_t VP8LPredictor8_C(const uint32_t* const left,
49                           const uint32_t* const top);
50 uint32_t VP8LPredictor9_C(const uint32_t* const left,
51                           const uint32_t* const top);
52 uint32_t VP8LPredictor10_C(const uint32_t* const left,
53                            const uint32_t* const top);
54 uint32_t VP8LPredictor11_C(const uint32_t* const left,
55                            const uint32_t* const top);
56 uint32_t VP8LPredictor12_C(const uint32_t* const left,
57                            const uint32_t* const top);
58 uint32_t VP8LPredictor13_C(const uint32_t* const left,
59                            const uint32_t* const top);
60 
61 // These Add/Sub function expects upper[-1] and out[-1] to be readable.
62 typedef void (*VP8LPredictorAddSubFunc)(const uint32_t* in,
63                                         const uint32_t* upper, int num_pixels,
64                                         uint32_t* WEBP_RESTRICT out);
65 extern VP8LPredictorAddSubFunc VP8LPredictorsAdd[16];
66 extern VP8LPredictorAddSubFunc VP8LPredictorsAdd_C[16];
67 
68 typedef void (*VP8LProcessDecBlueAndRedFunc)(const uint32_t* src,
69                                              int num_pixels, uint32_t* dst);
70 extern VP8LProcessDecBlueAndRedFunc VP8LAddGreenToBlueAndRed;
71 
72 typedef struct {
73   // Note: the members are uint8_t, so that any negative values are
74   // automatically converted to "mod 256" values.
75   uint8_t green_to_red_;
76   uint8_t green_to_blue_;
77   uint8_t red_to_blue_;
78 } VP8LMultipliers;
79 typedef void (*VP8LTransformColorInverseFunc)(const VP8LMultipliers* const m,
80                                               const uint32_t* src,
81                                               int num_pixels, uint32_t* dst);
82 extern VP8LTransformColorInverseFunc VP8LTransformColorInverse;
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 // Color space conversion.
95 typedef void (*VP8LConvertFunc)(const uint32_t* WEBP_RESTRICT src,
96                                 int num_pixels, uint8_t* WEBP_RESTRICT dst);
97 extern VP8LConvertFunc VP8LConvertBGRAToRGB;
98 extern VP8LConvertFunc VP8LConvertBGRAToRGBA;
99 extern VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
100 extern VP8LConvertFunc VP8LConvertBGRAToRGB565;
101 extern VP8LConvertFunc VP8LConvertBGRAToBGR;
102 
103 // Converts from BGRA to other color spaces.
104 void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
105                          WEBP_CSP_MODE out_colorspace, uint8_t* const rgba);
106 
107 typedef void (*VP8LMapARGBFunc)(const uint32_t* src,
108                                 const uint32_t* const color_map,
109                                 uint32_t* dst, int y_start,
110                                 int y_end, int width);
111 typedef void (*VP8LMapAlphaFunc)(const uint8_t* src,
112                                  const uint32_t* const color_map,
113                                  uint8_t* dst, int y_start,
114                                  int y_end, int width);
115 
116 extern VP8LMapARGBFunc VP8LMapColor32b;
117 extern VP8LMapAlphaFunc VP8LMapColor8b;
118 
119 // Similar to the static method ColorIndexInverseTransform() that is part of
120 // lossless.c, but used only for alpha decoding. It takes uint8_t (rather than
121 // uint32_t) arguments for 'src' and 'dst'.
122 void VP8LColorIndexInverseTransformAlpha(
123     const struct VP8LTransform* const transform, int y_start, int y_end,
124     const uint8_t* src, uint8_t* dst);
125 
126 // Expose some C-only fallback functions
127 void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
128                                  const uint32_t* src, int num_pixels,
129                                  uint32_t* dst);
130 
131 void VP8LConvertBGRAToRGB_C(const uint32_t* WEBP_RESTRICT src, int num_pixels,
132                             uint8_t* WEBP_RESTRICT dst);
133 void VP8LConvertBGRAToRGBA_C(const uint32_t* WEBP_RESTRICT src, int num_pixels,
134                              uint8_t* WEBP_RESTRICT dst);
135 void VP8LConvertBGRAToRGBA4444_C(const uint32_t* WEBP_RESTRICT src,
136                                  int num_pixels, uint8_t* WEBP_RESTRICT dst);
137 void VP8LConvertBGRAToRGB565_C(const uint32_t* WEBP_RESTRICT src,
138                                int num_pixels, uint8_t* WEBP_RESTRICT dst);
139 void VP8LConvertBGRAToBGR_C(const uint32_t* WEBP_RESTRICT src, int num_pixels,
140                             uint8_t* WEBP_RESTRICT dst);
141 void VP8LAddGreenToBlueAndRed_C(const uint32_t* src, int num_pixels,
142                                 uint32_t* dst);
143 
144 // Must be called before calling any of the above methods.
145 void VP8LDspInit(void);
146 
147 //------------------------------------------------------------------------------
148 // Encoding
149 
150 typedef void (*VP8LProcessEncBlueAndRedFunc)(uint32_t* dst, int num_pixels);
151 extern VP8LProcessEncBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed;
152 typedef void (*VP8LTransformColorFunc)(
153     const VP8LMultipliers* WEBP_RESTRICT const m, uint32_t* WEBP_RESTRICT dst,
154     int num_pixels);
155 extern VP8LTransformColorFunc VP8LTransformColor;
156 typedef void (*VP8LCollectColorBlueTransformsFunc)(
157     const uint32_t* WEBP_RESTRICT argb, int stride,
158     int tile_width, int tile_height,
159     int green_to_blue, int red_to_blue, uint32_t histo[]);
160 extern VP8LCollectColorBlueTransformsFunc VP8LCollectColorBlueTransforms;
161 
162 typedef void (*VP8LCollectColorRedTransformsFunc)(
163     const uint32_t* WEBP_RESTRICT argb, int stride,
164     int tile_width, int tile_height,
165     int green_to_red, uint32_t histo[]);
166 extern VP8LCollectColorRedTransformsFunc VP8LCollectColorRedTransforms;
167 
168 // Expose some C-only fallback functions
169 void VP8LTransformColor_C(const VP8LMultipliers* WEBP_RESTRICT const m,
170                           uint32_t* WEBP_RESTRICT data, int num_pixels);
171 void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels);
172 void VP8LCollectColorRedTransforms_C(const uint32_t* WEBP_RESTRICT argb,
173                                      int stride,
174                                      int tile_width, int tile_height,
175                                      int green_to_red, uint32_t histo[]);
176 void VP8LCollectColorBlueTransforms_C(const uint32_t* WEBP_RESTRICT argb,
177                                       int stride,
178                                       int tile_width, int tile_height,
179                                       int green_to_blue, int red_to_blue,
180                                       uint32_t histo[]);
181 
182 extern VP8LPredictorAddSubFunc VP8LPredictorsSub[16];
183 extern VP8LPredictorAddSubFunc VP8LPredictorsSub_C[16];
184 
185 // -----------------------------------------------------------------------------
186 // Huffman-cost related functions.
187 
188 typedef uint32_t (*VP8LCostFunc)(const uint32_t* population, int length);
189 typedef uint32_t (*VP8LCostCombinedFunc)(const uint32_t* WEBP_RESTRICT X,
190                                          const uint32_t* WEBP_RESTRICT Y,
191                                          int length);
192 typedef uint64_t (*VP8LCombinedShannonEntropyFunc)(const uint32_t X[256],
193                                                    const uint32_t Y[256]);
194 typedef uint64_t (*VP8LShannonEntropyFunc)(const uint32_t* X, int length);
195 
196 extern VP8LCostFunc VP8LExtraCost;
197 extern VP8LCostCombinedFunc VP8LExtraCostCombined;
198 extern VP8LCombinedShannonEntropyFunc VP8LCombinedShannonEntropy;
199 extern VP8LShannonEntropyFunc VP8LShannonEntropy;
200 
201 typedef struct {        // small struct to hold counters
202   int counts[2];        // index: 0=zero streak, 1=non-zero streak
203   int streaks[2][2];    // [zero/non-zero][streak<3 / streak>=3]
204 } VP8LStreaks;
205 
206 typedef struct {            // small struct to hold bit entropy results
207   uint64_t entropy;         // entropy
208   uint32_t sum;             // sum of the population
209   int nonzeros;             // number of non-zero elements in the population
210   uint32_t max_val;         // maximum value in the population
211   uint32_t nonzero_code;    // index of the last non-zero in the population
212 } VP8LBitEntropy;
213 
214 void VP8LBitEntropyInit(VP8LBitEntropy* const entropy);
215 
216 // Get the combined symbol bit entropy and Huffman cost stats for the
217 // distributions 'X' and 'Y'. Those results can then be refined according to
218 // codec specific heuristics.
219 typedef void (*VP8LGetCombinedEntropyUnrefinedFunc)(
220     const uint32_t X[], const uint32_t Y[], int length,
221     VP8LBitEntropy* WEBP_RESTRICT const bit_entropy,
222     VP8LStreaks* WEBP_RESTRICT const stats);
223 extern VP8LGetCombinedEntropyUnrefinedFunc VP8LGetCombinedEntropyUnrefined;
224 
225 // Get the entropy for the distribution 'X'.
226 typedef void (*VP8LGetEntropyUnrefinedFunc)(
227     const uint32_t X[], int length,
228     VP8LBitEntropy* WEBP_RESTRICT const bit_entropy,
229     VP8LStreaks* WEBP_RESTRICT const stats);
230 extern VP8LGetEntropyUnrefinedFunc VP8LGetEntropyUnrefined;
231 
232 void VP8LBitsEntropyUnrefined(const uint32_t* WEBP_RESTRICT const array, int n,
233                               VP8LBitEntropy* WEBP_RESTRICT const entropy);
234 
235 typedef void (*VP8LAddVectorFunc)(const uint32_t* WEBP_RESTRICT a,
236                                   const uint32_t* WEBP_RESTRICT b,
237                                   uint32_t* WEBP_RESTRICT out, int size);
238 extern VP8LAddVectorFunc VP8LAddVector;
239 typedef void (*VP8LAddVectorEqFunc)(const uint32_t* WEBP_RESTRICT a,
240                                     uint32_t* WEBP_RESTRICT out, int size);
241 extern VP8LAddVectorEqFunc VP8LAddVectorEq;
242 void VP8LHistogramAdd(const VP8LHistogram* WEBP_RESTRICT const a,
243                       const VP8LHistogram* WEBP_RESTRICT const b,
244                       VP8LHistogram* WEBP_RESTRICT const out);
245 
246 // -----------------------------------------------------------------------------
247 // PrefixEncode()
248 
249 typedef int (*VP8LVectorMismatchFunc)(const uint32_t* const array1,
250                                       const uint32_t* const array2, int length);
251 // Returns the first index where array1 and array2 are different.
252 extern VP8LVectorMismatchFunc VP8LVectorMismatch;
253 
254 typedef void (*VP8LBundleColorMapFunc)(const uint8_t* WEBP_RESTRICT const row,
255                                        int width, int xbits,
256                                        uint32_t* WEBP_RESTRICT dst);
257 extern VP8LBundleColorMapFunc VP8LBundleColorMap;
258 void VP8LBundleColorMap_C(const uint8_t* WEBP_RESTRICT const row,
259                           int width, int xbits, uint32_t* WEBP_RESTRICT dst);
260 
261 // Must be called before calling any of the above methods.
262 void VP8LEncDspInit(void);
263 
264 //------------------------------------------------------------------------------
265 
266 #ifdef __cplusplus
267 }    // extern "C"
268 #endif
269 
270 #endif  // WEBP_DSP_LOSSLESS_H_
271