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