1 // Copyright 2012 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Image transforms and color space conversion methods for lossless decoder.
12 // Authors: Vikas Arora (vikaas.arora@gmail.com)
13 // Jyrki Alakuijala (jyrki@google.com)
15 #ifndef WEBP_DSP_LOSSLESS_H_
16 #define WEBP_DSP_LOSSLESS_H_
18 #include "../webp/types.h"
19 #include "../webp/decode.h"
21 #include "../enc/histogram.h"
22 #include "../utils/utils.h"
28 //------------------------------------------------------------------------------
29 // Signatures and generic function-pointers
31 typedef uint32_t (*VP8LPredictorFunc
)(uint32_t left
, const uint32_t* const top
);
32 extern VP8LPredictorFunc VP8LPredictors
[16];
34 typedef void (*VP8LProcessBlueAndRedFunc
)(uint32_t* argb_data
, int num_pixels
);
35 extern VP8LProcessBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed
;
36 extern VP8LProcessBlueAndRedFunc VP8LAddGreenToBlueAndRed
;
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_
;
45 typedef void (*VP8LTransformColorFunc
)(const VP8LMultipliers
* const m
,
46 uint32_t* argb_data
, int num_pixels
);
47 extern VP8LTransformColorFunc VP8LTransformColor
;
48 extern VP8LTransformColorFunc VP8LTransformColorInverse
;
50 typedef void (*VP8LConvertFunc
)(const uint32_t* src
, int num_pixels
,
52 extern VP8LConvertFunc VP8LConvertBGRAToRGB
;
53 extern VP8LConvertFunc VP8LConvertBGRAToRGBA
;
54 extern VP8LConvertFunc VP8LConvertBGRAToRGBA4444
;
55 extern VP8LConvertFunc VP8LConvertBGRAToRGB565
;
56 extern VP8LConvertFunc VP8LConvertBGRAToBGR
;
58 // Expose some C-only fallback functions
59 void VP8LTransformColor_C(const VP8LMultipliers
* const m
,
60 uint32_t* data
, int num_pixels
);
61 void VP8LTransformColorInverse_C(const VP8LMultipliers
* const m
,
62 uint32_t* data
, int num_pixels
);
64 void VP8LConvertBGRAToRGB_C(const uint32_t* src
, int num_pixels
, uint8_t* dst
);
65 void VP8LConvertBGRAToRGBA_C(const uint32_t* src
, int num_pixels
, uint8_t* dst
);
66 void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src
,
67 int num_pixels
, uint8_t* dst
);
68 void VP8LConvertBGRAToRGB565_C(const uint32_t* src
,
69 int num_pixels
, uint8_t* dst
);
70 void VP8LConvertBGRAToBGR_C(const uint32_t* src
, int num_pixels
, uint8_t* dst
);
71 void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data
, int num_pixels
);
72 void VP8LAddGreenToBlueAndRed_C(uint32_t* data
, int num_pixels
);
74 // Must be called before calling any of the above methods.
75 void VP8LDspInit(void);
77 //------------------------------------------------------------------------------
80 struct VP8LTransform
; // Defined in dec/vp8li.h.
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).
86 void VP8LInverseTransform(const struct VP8LTransform
* const transform
,
87 int row_start
, int row_end
,
88 const uint32_t* const in
, uint32_t* const out
);
90 // 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'.
93 void VP8LColorIndexInverseTransformAlpha(
94 const struct VP8LTransform
* const transform
, int y_start
, int y_end
,
95 const uint8_t* src
, uint8_t* dst
);
97 void VP8LResidualImage(int width
, int height
, int bits
,
98 uint32_t* const argb
, uint32_t* const argb_scratch
,
99 uint32_t* const image
);
101 void VP8LColorSpaceTransform(int width
, int height
, int bits
, int quality
,
102 uint32_t* const argb
, uint32_t* image
);
104 //------------------------------------------------------------------------------
105 // Color space conversion.
107 // Converts from BGRA to other color spaces.
108 void VP8LConvertFromBGRA(const uint32_t* const in_data
, int num_pixels
,
109 WEBP_CSP_MODE out_colorspace
, uint8_t* const rgba
);
111 //------------------------------------------------------------------------------
114 // Computes sampled size of 'size' when sampling using 'sampling bits'.
115 static WEBP_INLINE
uint32_t VP8LSubSampleSize(uint32_t size
,
116 uint32_t sampling_bits
) {
117 return (size
+ (1 << sampling_bits
) - 1) >> sampling_bits
;
120 // -----------------------------------------------------------------------------
121 // Faster logarithm for integers. Small values use a look-up table.
122 #define LOG_LOOKUP_IDX_MAX 256
123 extern const float kLog2Table
[LOG_LOOKUP_IDX_MAX
];
124 extern const float kSLog2Table
[LOG_LOOKUP_IDX_MAX
];
125 typedef float (*VP8LFastLog2SlowFunc
)(uint32_t v
);
127 extern VP8LFastLog2SlowFunc VP8LFastLog2Slow
;
128 extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow
;
130 static WEBP_INLINE
float VP8LFastLog2(uint32_t v
) {
131 return (v
< LOG_LOOKUP_IDX_MAX
) ? kLog2Table
[v
] : VP8LFastLog2Slow(v
);
133 // Fast calculation of v * log2(v) for integer input.
134 static WEBP_INLINE
float VP8LFastSLog2(uint32_t v
) {
135 return (v
< LOG_LOOKUP_IDX_MAX
) ? kSLog2Table
[v
] : VP8LFastSLog2Slow(v
);
138 // -----------------------------------------------------------------------------
139 // Huffman-cost related functions.
141 typedef double (*VP8LCostFunc
)(const uint32_t* population
, int length
);
142 typedef double (*VP8LCostCombinedFunc
)(const uint32_t* X
, const uint32_t* Y
,
145 extern VP8LCostFunc VP8LExtraCost
;
146 extern VP8LCostCombinedFunc VP8LExtraCostCombined
;
148 typedef 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]
153 typedef VP8LStreaks (*VP8LCostCountFunc
)(const uint32_t* population
,
155 typedef VP8LStreaks (*VP8LCostCombinedCountFunc
)(const uint32_t* X
,
156 const uint32_t* Y
, int length
);
158 extern VP8LCostCountFunc VP8LHuffmanCostCount
;
159 extern VP8LCostCombinedCountFunc VP8LHuffmanCostCombinedCount
;
161 typedef void (*VP8LHistogramAddFunc
)(const VP8LHistogram
* const a
,
162 const VP8LHistogram
* const b
,
163 VP8LHistogram
* const out
);
164 extern VP8LHistogramAddFunc VP8LHistogramAdd
;
166 // -----------------------------------------------------------------------------
169 static 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.
174 return log_floor
+ 1;
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.
180 static 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
;
188 static 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
;
198 #define PREFIX_LOOKUP_IDX_MAX 512
204 // These tables are derived using VP8LPrefixEncodeNoLUT.
205 extern const VP8LPrefixCode kPrefixEncodeCode
[PREFIX_LOOKUP_IDX_MAX
];
206 extern const uint8_t kPrefixEncodeExtraBitsValue
[PREFIX_LOOKUP_IDX_MAX
];
207 static 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_
;
214 VP8LPrefixEncodeBitsNoLUT(distance
, code
, extra_bits
);
218 static 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
];
227 VP8LPrefixEncodeNoLUT(distance
, code
, extra_bits
, extra_bits_value
);
231 // In-place difference of each component with mod 256.
232 static 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
);
240 void VP8LBundleColorMap(const uint8_t* const row
, int width
,
241 int xbits
, uint32_t* const dst
);
243 //------------------------------------------------------------------------------
249 #endif // WEBP_DSP_LOSSLESS_H_