OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Image transforms and color space conversion methods for lossless decoder. | 10 // Image transforms and color space conversion methods for lossless decoder. |
11 // | 11 // |
12 // Authors: Vikas Arora (vikaas.arora@gmail.com) | 12 // Authors: Vikas Arora (vikaas.arora@gmail.com) |
13 // Jyrki Alakuijala (jyrki@google.com) | 13 // Jyrki Alakuijala (jyrki@google.com) |
14 | 14 |
15 #ifndef WEBP_DSP_LOSSLESS_H_ | 15 #ifndef WEBP_DSP_LOSSLESS_H_ |
16 #define WEBP_DSP_LOSSLESS_H_ | 16 #define WEBP_DSP_LOSSLESS_H_ |
17 | 17 |
18 #include "../webp/types.h" | 18 #include "../webp/types.h" |
19 #include "../webp/decode.h" | 19 #include "../webp/decode.h" |
20 | 20 |
21 #include "../enc/histogram.h" | 21 #include "../enc/histogram.h" |
22 #include "../utils/utils.h" | 22 #include "../utils/utils.h" |
23 | 23 |
24 #ifdef __cplusplus | 24 #ifdef __cplusplus |
25 extern "C" { | 25 extern "C" { |
26 #endif | 26 #endif |
27 | 27 |
| 28 #ifdef WEBP_EXPERIMENTAL_FEATURES |
| 29 #include "../enc/delta_palettization.h" |
| 30 #endif // WEBP_EXPERIMENTAL_FEATURES |
| 31 |
28 //------------------------------------------------------------------------------ | 32 //------------------------------------------------------------------------------ |
29 // Signatures and generic function-pointers | 33 // Decoding |
30 | 34 |
31 typedef uint32_t (*VP8LPredictorFunc)(uint32_t left, const uint32_t* const top); | 35 typedef uint32_t (*VP8LPredictorFunc)(uint32_t left, const uint32_t* const top); |
32 extern VP8LPredictorFunc VP8LPredictors[16]; | 36 extern VP8LPredictorFunc VP8LPredictors[16]; |
33 | 37 |
34 typedef void (*VP8LProcessBlueAndRedFunc)(uint32_t* argb_data, int num_pixels); | 38 typedef void (*VP8LProcessBlueAndRedFunc)(uint32_t* argb_data, int num_pixels); |
35 extern VP8LProcessBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed; | |
36 extern VP8LProcessBlueAndRedFunc VP8LAddGreenToBlueAndRed; | 39 extern VP8LProcessBlueAndRedFunc VP8LAddGreenToBlueAndRed; |
37 | 40 |
38 typedef struct { | 41 typedef struct { |
39 // Note: the members are uint8_t, so that any negative values are | 42 // Note: the members are uint8_t, so that any negative values are |
40 // automatically converted to "mod 256" values. | 43 // automatically converted to "mod 256" values. |
41 uint8_t green_to_red_; | 44 uint8_t green_to_red_; |
42 uint8_t green_to_blue_; | 45 uint8_t green_to_blue_; |
43 uint8_t red_to_blue_; | 46 uint8_t red_to_blue_; |
44 } VP8LMultipliers; | 47 } VP8LMultipliers; |
45 typedef void (*VP8LTransformColorFunc)(const VP8LMultipliers* const m, | 48 typedef void (*VP8LTransformColorFunc)(const VP8LMultipliers* const m, |
46 uint32_t* argb_data, int num_pixels); | 49 uint32_t* argb_data, int num_pixels); |
47 extern VP8LTransformColorFunc VP8LTransformColor; | |
48 extern VP8LTransformColorFunc VP8LTransformColorInverse; | 50 extern VP8LTransformColorFunc VP8LTransformColorInverse; |
49 | 51 |
| 52 struct VP8LTransform; // Defined in dec/vp8li.h. |
| 53 |
| 54 // Performs inverse transform of data given transform information, start and end |
| 55 // rows. Transform will be applied to rows [row_start, row_end[. |
| 56 // The *in and *out pointers refer to source and destination data respectively |
| 57 // corresponding to the intermediate row (row_start). |
| 58 void VP8LInverseTransform(const struct VP8LTransform* const transform, |
| 59 int row_start, int row_end, |
| 60 const uint32_t* const in, uint32_t* const out); |
| 61 |
| 62 // Color space conversion. |
50 typedef void (*VP8LConvertFunc)(const uint32_t* src, int num_pixels, | 63 typedef void (*VP8LConvertFunc)(const uint32_t* src, int num_pixels, |
51 uint8_t* dst); | 64 uint8_t* dst); |
52 extern VP8LConvertFunc VP8LConvertBGRAToRGB; | 65 extern VP8LConvertFunc VP8LConvertBGRAToRGB; |
53 extern VP8LConvertFunc VP8LConvertBGRAToRGBA; | 66 extern VP8LConvertFunc VP8LConvertBGRAToRGBA; |
54 extern VP8LConvertFunc VP8LConvertBGRAToRGBA4444; | 67 extern VP8LConvertFunc VP8LConvertBGRAToRGBA4444; |
55 extern VP8LConvertFunc VP8LConvertBGRAToRGB565; | 68 extern VP8LConvertFunc VP8LConvertBGRAToRGB565; |
56 extern VP8LConvertFunc VP8LConvertBGRAToBGR; | 69 extern VP8LConvertFunc VP8LConvertBGRAToBGR; |
57 | 70 |
| 71 // Converts from BGRA to other color spaces. |
| 72 void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels, |
| 73 WEBP_CSP_MODE out_colorspace, uint8_t* const rgba); |
| 74 |
| 75 // color mapping related functions. |
| 76 static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) { |
| 77 return (idx >> 8) & 0xff; |
| 78 } |
| 79 |
| 80 static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) { |
| 81 return idx; |
| 82 } |
| 83 |
| 84 static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) { |
| 85 return val; |
| 86 } |
| 87 |
| 88 static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) { |
| 89 return (val >> 8) & 0xff; |
| 90 } |
| 91 |
| 92 typedef void (*VP8LMapARGBFunc)(const uint32_t* src, |
| 93 const uint32_t* const color_map, |
| 94 uint32_t* dst, int y_start, |
| 95 int y_end, int width); |
| 96 typedef void (*VP8LMapAlphaFunc)(const uint8_t* src, |
| 97 const uint32_t* const color_map, |
| 98 uint8_t* dst, int y_start, |
| 99 int y_end, int width); |
| 100 |
| 101 extern VP8LMapARGBFunc VP8LMapColor32b; |
| 102 extern VP8LMapAlphaFunc VP8LMapColor8b; |
| 103 |
| 104 // Similar to the static method ColorIndexInverseTransform() that is part of |
| 105 // lossless.c, but used only for alpha decoding. It takes uint8_t (rather than |
| 106 // uint32_t) arguments for 'src' and 'dst'. |
| 107 void VP8LColorIndexInverseTransformAlpha( |
| 108 const struct VP8LTransform* const transform, int y_start, int y_end, |
| 109 const uint8_t* src, uint8_t* dst); |
| 110 |
58 // Expose some C-only fallback functions | 111 // 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, | 112 void VP8LTransformColorInverse_C(const VP8LMultipliers* const m, |
62 uint32_t* data, int num_pixels); | 113 uint32_t* data, int num_pixels); |
63 | 114 |
64 void VP8LConvertBGRAToRGB_C(const uint32_t* src, int num_pixels, uint8_t* dst); | 115 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); | 116 void VP8LConvertBGRAToRGBA_C(const uint32_t* src, int num_pixels, uint8_t* dst); |
66 void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src, | 117 void VP8LConvertBGRAToRGBA4444_C(const uint32_t* src, |
67 int num_pixels, uint8_t* dst); | 118 int num_pixels, uint8_t* dst); |
68 void VP8LConvertBGRAToRGB565_C(const uint32_t* src, | 119 void VP8LConvertBGRAToRGB565_C(const uint32_t* src, |
69 int num_pixels, uint8_t* dst); | 120 int num_pixels, uint8_t* dst); |
70 void VP8LConvertBGRAToBGR_C(const uint32_t* src, int num_pixels, uint8_t* dst); | 121 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); | 122 void VP8LAddGreenToBlueAndRed_C(uint32_t* data, int num_pixels); |
73 | 123 |
74 // Must be called before calling any of the above methods. | 124 // Must be called before calling any of the above methods. |
75 void VP8LDspInit(void); | 125 void VP8LDspInit(void); |
76 | 126 |
77 //------------------------------------------------------------------------------ | 127 //------------------------------------------------------------------------------ |
| 128 // Encoding |
| 129 |
| 130 extern VP8LProcessBlueAndRedFunc VP8LSubtractGreenFromBlueAndRed; |
| 131 extern VP8LTransformColorFunc VP8LTransformColor; |
| 132 typedef void (*VP8LCollectColorBlueTransformsFunc)( |
| 133 const uint32_t* argb, int stride, |
| 134 int tile_width, int tile_height, |
| 135 int green_to_blue, int red_to_blue, int histo[]); |
| 136 extern VP8LCollectColorBlueTransformsFunc VP8LCollectColorBlueTransforms; |
| 137 |
| 138 typedef void (*VP8LCollectColorRedTransformsFunc)( |
| 139 const uint32_t* argb, int stride, |
| 140 int tile_width, int tile_height, |
| 141 int green_to_red, int histo[]); |
| 142 extern VP8LCollectColorRedTransformsFunc VP8LCollectColorRedTransforms; |
| 143 |
| 144 // Expose some C-only fallback functions |
| 145 void VP8LTransformColor_C(const VP8LMultipliers* const m, |
| 146 uint32_t* data, int num_pixels); |
| 147 void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels); |
| 148 void VP8LCollectColorRedTransforms_C(const uint32_t* argb, int stride, |
| 149 int tile_width, int tile_height, |
| 150 int green_to_red, int histo[]); |
| 151 void VP8LCollectColorBlueTransforms_C(const uint32_t* argb, int stride, |
| 152 int tile_width, int tile_height, |
| 153 int green_to_blue, int red_to_blue, |
| 154 int histo[]); |
| 155 |
| 156 //------------------------------------------------------------------------------ |
78 // Image transforms. | 157 // Image transforms. |
79 | 158 |
80 struct VP8LTransform; // Defined in dec/vp8li.h. | 159 void VP8LResidualImage(int width, int height, int bits, int low_effort, |
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). | |
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); | |
89 | |
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); | |
96 | |
97 void VP8LResidualImage(int width, int height, int bits, | |
98 uint32_t* const argb, uint32_t* const argb_scratch, | 160 uint32_t* const argb, uint32_t* const argb_scratch, |
99 uint32_t* const image); | 161 uint32_t* const image, int exact); |
100 | 162 |
101 void VP8LColorSpaceTransform(int width, int height, int bits, int quality, | 163 void VP8LColorSpaceTransform(int width, int height, int bits, int quality, |
102 uint32_t* const argb, uint32_t* image); | 164 uint32_t* const argb, uint32_t* image); |
103 | 165 |
104 //------------------------------------------------------------------------------ | 166 //------------------------------------------------------------------------------ |
105 // Color space conversion. | |
106 | |
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); | |
110 | |
111 //------------------------------------------------------------------------------ | |
112 // Misc methods. | 167 // Misc methods. |
113 | 168 |
114 // Computes sampled size of 'size' when sampling using 'sampling bits'. | 169 // Computes sampled size of 'size' when sampling using 'sampling bits'. |
115 static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size, | 170 static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size, |
116 uint32_t sampling_bits) { | 171 uint32_t sampling_bits) { |
117 return (size + (1 << sampling_bits) - 1) >> sampling_bits; | 172 return (size + (1 << sampling_bits) - 1) >> sampling_bits; |
118 } | 173 } |
119 | 174 |
120 // ----------------------------------------------------------------------------- | 175 // ----------------------------------------------------------------------------- |
121 // Faster logarithm for integers. Small values use a look-up table. | 176 // Faster logarithm for integers. Small values use a look-up table. |
| 177 |
| 178 // The threshold till approximate version of log_2 can be used. |
| 179 // Practically, we can get rid of the call to log() as the two values match to |
| 180 // very high degree (the ratio of these two is 0.99999x). |
| 181 // Keeping a high threshold for now. |
| 182 #define APPROX_LOG_WITH_CORRECTION_MAX 65536 |
| 183 #define APPROX_LOG_MAX 4096 |
| 184 #define LOG_2_RECIPROCAL 1.44269504088896338700465094007086 |
122 #define LOG_LOOKUP_IDX_MAX 256 | 185 #define LOG_LOOKUP_IDX_MAX 256 |
123 extern const float kLog2Table[LOG_LOOKUP_IDX_MAX]; | 186 extern const float kLog2Table[LOG_LOOKUP_IDX_MAX]; |
124 extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX]; | 187 extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX]; |
125 typedef float (*VP8LFastLog2SlowFunc)(uint32_t v); | 188 typedef float (*VP8LFastLog2SlowFunc)(uint32_t v); |
126 | 189 |
127 extern VP8LFastLog2SlowFunc VP8LFastLog2Slow; | 190 extern VP8LFastLog2SlowFunc VP8LFastLog2Slow; |
128 extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow; | 191 extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow; |
129 | 192 |
130 static WEBP_INLINE float VP8LFastLog2(uint32_t v) { | 193 static WEBP_INLINE float VP8LFastLog2(uint32_t v) { |
131 return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v); | 194 return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v); |
132 } | 195 } |
133 // Fast calculation of v * log2(v) for integer input. | 196 // Fast calculation of v * log2(v) for integer input. |
134 static WEBP_INLINE float VP8LFastSLog2(uint32_t v) { | 197 static WEBP_INLINE float VP8LFastSLog2(uint32_t v) { |
135 return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v); | 198 return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v); |
136 } | 199 } |
137 | 200 |
138 // ----------------------------------------------------------------------------- | 201 // ----------------------------------------------------------------------------- |
139 // Huffman-cost related functions. | 202 // Huffman-cost related functions. |
140 | 203 |
141 typedef double (*VP8LCostFunc)(const uint32_t* population, int length); | 204 typedef double (*VP8LCostFunc)(const uint32_t* population, int length); |
142 typedef double (*VP8LCostCombinedFunc)(const uint32_t* X, const uint32_t* Y, | 205 typedef double (*VP8LCostCombinedFunc)(const uint32_t* X, const uint32_t* Y, |
143 int length); | 206 int length); |
| 207 typedef float (*VP8LCombinedShannonEntropyFunc)(const int X[256], |
| 208 const int Y[256]); |
144 | 209 |
145 extern VP8LCostFunc VP8LExtraCost; | 210 extern VP8LCostFunc VP8LExtraCost; |
146 extern VP8LCostCombinedFunc VP8LExtraCostCombined; | 211 extern VP8LCostCombinedFunc VP8LExtraCostCombined; |
| 212 extern VP8LCombinedShannonEntropyFunc VP8LCombinedShannonEntropy; |
147 | 213 |
148 typedef struct { // small struct to hold counters | 214 typedef struct { // small struct to hold counters |
149 int counts[2]; // index: 0=zero steak, 1=non-zero streak | 215 int counts[2]; // index: 0=zero steak, 1=non-zero streak |
150 int streaks[2][2]; // [zero/non-zero][streak<3 / streak>=3] | 216 int streaks[2][2]; // [zero/non-zero][streak<3 / streak>=3] |
151 } VP8LStreaks; | 217 } VP8LStreaks; |
152 | 218 |
153 typedef VP8LStreaks (*VP8LCostCountFunc)(const uint32_t* population, | |
154 int length); | |
155 typedef VP8LStreaks (*VP8LCostCombinedCountFunc)(const uint32_t* X, | 219 typedef VP8LStreaks (*VP8LCostCombinedCountFunc)(const uint32_t* X, |
156 const uint32_t* Y, int length); | 220 const uint32_t* Y, int length); |
157 | 221 |
158 extern VP8LCostCountFunc VP8LHuffmanCostCount; | |
159 extern VP8LCostCombinedCountFunc VP8LHuffmanCostCombinedCount; | 222 extern VP8LCostCombinedCountFunc VP8LHuffmanCostCombinedCount; |
160 | 223 |
| 224 typedef struct { // small struct to hold bit entropy results |
| 225 double entropy; // entropy |
| 226 uint32_t sum; // sum of the population |
| 227 int nonzeros; // number of non-zero elements in the population |
| 228 uint32_t max_val; // maximum value in the population |
| 229 uint32_t nonzero_code; // index of the last non-zero in the population |
| 230 } VP8LBitEntropy; |
| 231 |
| 232 void VP8LBitEntropyInit(VP8LBitEntropy* const entropy); |
| 233 |
| 234 // Get the combined symbol bit entropy and Huffman cost stats for the |
| 235 // distributions 'X' and 'Y'. Those results can then be refined according to |
| 236 // codec specific heuristics. |
| 237 void VP8LGetCombinedEntropyUnrefined(const uint32_t* const X, |
| 238 const uint32_t* const Y, int length, |
| 239 VP8LBitEntropy* const bit_entropy, |
| 240 VP8LStreaks* const stats); |
| 241 // Get the entropy for the distribution 'X'. |
| 242 void VP8LGetEntropyUnrefined(const uint32_t* const X, int length, |
| 243 VP8LBitEntropy* const bit_entropy, |
| 244 VP8LStreaks* const stats); |
| 245 |
| 246 void VP8LBitsEntropyUnrefined(const uint32_t* const array, int n, |
| 247 VP8LBitEntropy* const entropy); |
| 248 |
| 249 typedef void (*GetEntropyUnrefinedHelperFunc)(uint32_t val, int i, |
| 250 uint32_t* const val_prev, |
| 251 int* const i_prev, |
| 252 VP8LBitEntropy* const bit_entropy, |
| 253 VP8LStreaks* const stats); |
| 254 // Internal function used by VP8LGet*EntropyUnrefined. |
| 255 extern GetEntropyUnrefinedHelperFunc VP8LGetEntropyUnrefinedHelper; |
| 256 |
161 typedef void (*VP8LHistogramAddFunc)(const VP8LHistogram* const a, | 257 typedef void (*VP8LHistogramAddFunc)(const VP8LHistogram* const a, |
162 const VP8LHistogram* const b, | 258 const VP8LHistogram* const b, |
163 VP8LHistogram* const out); | 259 VP8LHistogram* const out); |
164 extern VP8LHistogramAddFunc VP8LHistogramAdd; | 260 extern VP8LHistogramAddFunc VP8LHistogramAdd; |
165 | 261 |
166 // ----------------------------------------------------------------------------- | 262 // ----------------------------------------------------------------------------- |
167 // PrefixEncode() | 263 // PrefixEncode() |
168 | 264 |
169 static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) { | 265 static WEBP_INLINE int VP8LBitsLog2Ceiling(uint32_t n) { |
170 const int log_floor = BitsLog2Floor(n); | 266 const int log_floor = BitsLog2Floor(n); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 const uint32_t alpha_and_green = | 329 const uint32_t alpha_and_green = |
234 0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u); | 330 0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u); |
235 const uint32_t red_and_blue = | 331 const uint32_t red_and_blue = |
236 0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu); | 332 0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu); |
237 return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu); | 333 return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu); |
238 } | 334 } |
239 | 335 |
240 void VP8LBundleColorMap(const uint8_t* const row, int width, | 336 void VP8LBundleColorMap(const uint8_t* const row, int width, |
241 int xbits, uint32_t* const dst); | 337 int xbits, uint32_t* const dst); |
242 | 338 |
| 339 // Must be called before calling any of the above methods. |
| 340 void VP8LEncDspInit(void); |
| 341 |
243 //------------------------------------------------------------------------------ | 342 //------------------------------------------------------------------------------ |
244 | 343 |
245 #ifdef __cplusplus | 344 #ifdef __cplusplus |
246 } // extern "C" | 345 } // extern "C" |
247 #endif | 346 #endif |
248 | 347 |
249 #endif // WEBP_DSP_LOSSLESS_H_ | 348 #endif // WEBP_DSP_LOSSLESS_H_ |
OLD | NEW |