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