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