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