blob: 08be9375ca17e356cc205d6cf3b9414084ba6e03 [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
Vikas Arora0406ce12013-08-09 15:57:12 -07003// 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.
Vikas Aroraa2415722012-08-09 16:18:58 -07008// -----------------------------------------------------------------------------
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
Vikas Aroraaf51b942014-08-28 10:51:12 -070021#include "../enc/histogram.h"
22#include "../utils/utils.h"
23
Vikas Arora8b720222014-01-02 16:48:02 -080024#ifdef __cplusplus
Vikas Aroraa2415722012-08-09 16:18:58 -070025extern "C" {
26#endif
27
28//------------------------------------------------------------------------------
Vikas Aroraaf51b942014-08-28 10:51:12 -070029// Signatures and generic function-pointers
Vikas Arora8b720222014-01-02 16:48:02 -080030
Vikas Aroraaf51b942014-08-28 10:51:12 -070031typedef uint32_t (*VP8LPredictorFunc)(uint32_t left, const uint32_t* const top);
32extern VP8LPredictorFunc VP8LPredictors[16];
Vikas Arora8b720222014-01-02 16:48:02 -080033
Vikas Aroraaf51b942014-08-28 10:51:12 -070034typedef void (*VP8LProcessBlueAndRedFunc)(uint32_t* argb_data, int num_pixels);
35extern VP8LProcessBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed;
36extern VP8LProcessBlueAndRedFunc VP8LAddGreenToBlueAndRed;
37
38typedef struct {
39 // Note: the members are uint8_t, so that any negative values are
40 // automatically converted to "mod 256" values.
41 uint8_t green_to_red_;
42 uint8_t green_to_blue_;
43 uint8_t red_to_blue_;
44} VP8LMultipliers;
45typedef void (*VP8LTransformColorFunc)(const VP8LMultipliers* const m,
46 uint32_t* argb_data, int num_pixels);
47extern VP8LTransformColorFunc VP8LTransformColor;
48extern VP8LTransformColorFunc VP8LTransformColorInverse;
49
50typedef void (*VP8LConvertFunc)(const uint32_t* src, int num_pixels,
51 uint8_t* dst);
52extern VP8LConvertFunc VP8LConvertBGRAToRGB;
53extern VP8LConvertFunc VP8LConvertBGRAToRGBA;
54extern VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
55extern VP8LConvertFunc VP8LConvertBGRAToRGB565;
56extern VP8LConvertFunc VP8LConvertBGRAToBGR;
57
58// Expose some C-only fallback functions
Vikas Arora1b4db032015-01-28 14:08:58 -080059void VP8LTransformColor_C(const VP8LMultipliers* const m,
60 uint32_t* data, int num_pixels);
61void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
Vikas Aroraaf51b942014-08-28 10:51:12 -070062 uint32_t* data, int num_pixels);
Vikas Aroraaf51b942014-08-28 10:51:12 -070063
Vikas Arora1b4db032015-01-28 14:08:58 -080064void VP8LConvertBGRAToRGB_C(const uint32_t* src, int num_pixels, uint8_t* dst);
65void VP8LConvertBGRAToRGBA_C(const uint32_t* src, int num_pixels, uint8_t* dst);
66void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src,
67 int num_pixels, uint8_t* dst);
68void VP8LConvertBGRAToRGB565_C(const uint32_t* src,
69 int num_pixels, uint8_t* dst);
70void VP8LConvertBGRAToBGR_C(const uint32_t* src, int num_pixels, uint8_t* dst);
71void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels);
72void VP8LAddGreenToBlueAndRed_C(uint32_t* data, int num_pixels);
Vikas Arora8b720222014-01-02 16:48:02 -080073
74// Must be called before calling any of the above methods.
75void VP8LDspInit(void);
76
77//------------------------------------------------------------------------------
Vikas Aroraa2415722012-08-09 16:18:58 -070078// Image transforms.
79
80struct VP8LTransform; // Defined in dec/vp8li.h.
81
82// Performs inverse transform of data given transform information, start and end
83// rows. Transform will be applied to rows [row_start, row_end[.
84// The *in and *out pointers refer to source and destination data respectively
85// corresponding to the intermediate row (row_start).
86void VP8LInverseTransform(const struct VP8LTransform* const transform,
87 int row_start, int row_end,
88 const uint32_t* const in, uint32_t* const out);
89
Vikas Arora0406ce12013-08-09 15:57:12 -070090// Similar to the static method ColorIndexInverseTransform() that is part of
91// lossless.c, but used only for alpha decoding. It takes uint8_t (rather than
92// uint32_t) arguments for 'src' and 'dst'.
93void VP8LColorIndexInverseTransformAlpha(
94 const struct VP8LTransform* const transform, int y_start, int y_end,
95 const uint8_t* src, uint8_t* dst);
96
Vikas Aroraa2415722012-08-09 16:18:58 -070097void VP8LResidualImage(int width, int height, int bits,
98 uint32_t* const argb, uint32_t* const argb_scratch,
99 uint32_t* const image);
100
Vikas Aroraaf51b942014-08-28 10:51:12 -0700101void VP8LColorSpaceTransform(int width, int height, int bits, int quality,
Vikas Aroraa2415722012-08-09 16:18:58 -0700102 uint32_t* const argb, uint32_t* image);
103
104//------------------------------------------------------------------------------
105// Color space conversion.
106
107// Converts from BGRA to other color spaces.
108void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
109 WEBP_CSP_MODE out_colorspace, uint8_t* const rgba);
110
111//------------------------------------------------------------------------------
112// Misc methods.
113
114// Computes sampled size of 'size' when sampling using 'sampling bits'.
115static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,
116 uint32_t sampling_bits) {
117 return (size + (1 << sampling_bits) - 1) >> sampling_bits;
118}
119
Vikas Aroraaf51b942014-08-28 10:51:12 -0700120// -----------------------------------------------------------------------------
Vikas Arora1e7bf882013-03-13 16:43:18 -0700121// Faster logarithm for integers. Small values use a look-up table.
122#define LOG_LOOKUP_IDX_MAX 256
123extern const float kLog2Table[LOG_LOOKUP_IDX_MAX];
124extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX];
Vikas Aroraaf51b942014-08-28 10:51:12 -0700125typedef float (*VP8LFastLog2SlowFunc)(uint32_t v);
126
127extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;
128extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow;
129
130static WEBP_INLINE float VP8LFastLog2(uint32_t v) {
Vikas Arora1e7bf882013-03-13 16:43:18 -0700131 return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);
132}
Vikas Aroraa2415722012-08-09 16:18:58 -0700133// Fast calculation of v * log2(v) for integer input.
Vikas Aroraaf51b942014-08-28 10:51:12 -0700134static WEBP_INLINE float VP8LFastSLog2(uint32_t v) {
Vikas Arora1e7bf882013-03-13 16:43:18 -0700135 return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);
136}
137
Vikas Arora8b720222014-01-02 16:48:02 -0800138// -----------------------------------------------------------------------------
Vikas Aroraaf51b942014-08-28 10:51:12 -0700139// Huffman-cost related functions.
140
141typedef double (*VP8LCostFunc)(const uint32_t* population, int length);
142typedef double (*VP8LCostCombinedFunc)(const uint32_t* X, const uint32_t* Y,
143 int length);
144
145extern VP8LCostFunc VP8LExtraCost;
146extern VP8LCostCombinedFunc VP8LExtraCostCombined;
147
148typedef struct { // small struct to hold counters
149 int counts[2]; // index: 0=zero steak, 1=non-zero streak
150 int streaks[2][2]; // [zero/non-zero][streak<3 / streak>=3]
151} VP8LStreaks;
152
153typedef VP8LStreaks (*VP8LCostCountFunc)(const uint32_t* population,
154 int length);
155typedef VP8LStreaks (*VP8LCostCombinedCountFunc)(const uint32_t* X,
156 const uint32_t* Y, int length);
157
158extern VP8LCostCountFunc VP8LHuffmanCostCount;
159extern VP8LCostCombinedCountFunc VP8LHuffmanCostCombinedCount;
160
161typedef void (*VP8LHistogramAddFunc)(const VP8LHistogram* const a,
162 const VP8LHistogram* const b,
163 VP8LHistogram* const out);
164extern VP8LHistogramAddFunc VP8LHistogramAdd;
165
166// -----------------------------------------------------------------------------
Vikas Arora8b720222014-01-02 16:48:02 -0800167// PrefixEncode()
168
Vikas Arora8b720222014-01-02 16:48:02 -0800169static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) {
170 const int log_floor = BitsLog2Floor(n);
171 if (n == (n & ~(n - 1))) // zero or a power of two.
172 return log_floor;
173 else
174 return log_floor + 1;
175}
176
177// Splitting of distance and length codes into prefixes and
178// extra bits. The prefixes are encoded with an entropy code
179// while the extra bits are stored just as normal bits.
180static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,
181 int* const extra_bits) {
182 const int highest_bit = BitsLog2Floor(--distance);
183 const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
184 *extra_bits = highest_bit - 1;
185 *code = 2 * highest_bit + second_highest_bit;
186}
187
188static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,
189 int* const extra_bits,
190 int* const extra_bits_value) {
191 const int highest_bit = BitsLog2Floor(--distance);
192 const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
193 *extra_bits = highest_bit - 1;
194 *extra_bits_value = distance & ((1 << *extra_bits) - 1);
195 *code = 2 * highest_bit + second_highest_bit;
196}
197
198#define PREFIX_LOOKUP_IDX_MAX 512
199typedef struct {
200 int8_t code_;
201 int8_t extra_bits_;
202} VP8LPrefixCode;
203
204// These tables are derived using VP8LPrefixEncodeNoLUT.
205extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];
206extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];
207static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,
208 int* const extra_bits) {
209 if (distance < PREFIX_LOOKUP_IDX_MAX) {
210 const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
211 *code = prefix_code.code_;
212 *extra_bits = prefix_code.extra_bits_;
213 } else {
214 VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);
215 }
216}
217
218static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,
219 int* const extra_bits,
220 int* const extra_bits_value) {
221 if (distance < PREFIX_LOOKUP_IDX_MAX) {
222 const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
223 *code = prefix_code.code_;
224 *extra_bits = prefix_code.extra_bits_;
225 *extra_bits_value = kPrefixEncodeExtraBitsValue[distance];
226 } else {
227 VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);
228 }
229}
Vikas Aroraa2415722012-08-09 16:18:58 -0700230
231// In-place difference of each component with mod 256.
232static WEBP_INLINE uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {
233 const uint32_t alpha_and_green =
234 0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);
235 const uint32_t red_and_blue =
236 0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);
237 return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
238}
239
Vikas Arora0406ce12013-08-09 15:57:12 -0700240void VP8LBundleColorMap(const uint8_t* const row, int width,
241 int xbits, uint32_t* const dst);
242
Vikas Aroraa2415722012-08-09 16:18:58 -0700243//------------------------------------------------------------------------------
244
Vikas Arora8b720222014-01-02 16:48:02 -0800245#ifdef __cplusplus
Vikas Aroraa2415722012-08-09 16:18:58 -0700246} // extern "C"
247#endif
248
249#endif // WEBP_DSP_LOSSLESS_H_