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