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