1 // Copyright 2011 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 // Speed-critical functions. 11 // 12 // Author: Skal (pascal.massimino@gmail.com) 13 14 #ifndef WEBP_DSP_DSP_H_ 15 #define WEBP_DSP_DSP_H_ 16 17 #ifdef HAVE_CONFIG_H 18 #include "../webp/config.h" 19 #endif 20 21 #include "../webp/types.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 //------------------------------------------------------------------------------ 28 // CPU detection 29 30 #if defined(__GNUC__) 31 # define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__) 32 # define LOCAL_GCC_PREREQ(maj, min) \ 33 (LOCAL_GCC_VERSION >= (((maj) << 8) | (min))) 34 #else 35 # define LOCAL_GCC_VERSION 0 36 # define LOCAL_GCC_PREREQ(maj, min) 0 37 #endif 38 39 #ifdef __clang__ 40 # define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__) 41 # define LOCAL_CLANG_PREREQ(maj, min) \ 42 (LOCAL_CLANG_VERSION >= (((maj) << 8) | (min))) 43 #else 44 # define LOCAL_CLANG_VERSION 0 45 # define LOCAL_CLANG_PREREQ(maj, min) 0 46 #endif // __clang__ 47 48 #if defined(_MSC_VER) && _MSC_VER > 1310 && \ 49 (defined(_M_X64) || defined(_M_IX86)) 50 #define WEBP_MSC_SSE2 // Visual C++ SSE2 targets 51 #endif 52 53 // WEBP_HAVE_* are used to indicate the presence of the instruction set in dsp 54 // files without intrinsics, allowing the corresponding Init() to be called. 55 // Files containing intrinsics will need to be built targeting the instruction 56 // set so should succeed on one of the earlier tests. 57 #if defined(__SSE2__) || defined(WEBP_MSC_SSE2) || defined(WEBP_HAVE_SSE2) 58 #define WEBP_USE_SSE2 59 #endif 60 61 #if defined(__AVX2__) || defined(WEBP_HAVE_AVX2) 62 #define WEBP_USE_AVX2 63 #endif 64 65 #if defined(__ANDROID__) && defined(__ARM_ARCH_7A__) 66 #define WEBP_ANDROID_NEON // Android targets that might support NEON 67 #endif 68 69 // The intrinsics currently cause compiler errors with arm-nacl-gcc and the 70 // inline assembly would need to be modified for use with Native Client. 71 #if (defined(__ARM_NEON__) || defined(__aarch64__)) \ 72 && !defined(__native_client__) 73 #define WEBP_USE_NEON 74 #endif 75 76 #if defined(__mips__) && !defined(__mips64) && (__mips_isa_rev < 6) 77 #define WEBP_USE_MIPS32 78 #if (__mips_isa_rev >= 2) 79 #define WEBP_USE_MIPS32_R2 80 #endif 81 #endif 82 83 typedef enum { 84 kSSE2, 85 kSSE3, 86 kAVX, 87 kAVX2, 88 kNEON, 89 kMIPS32 90 } CPUFeature; 91 // returns true if the CPU supports the feature. 92 typedef int (*VP8CPUInfo)(CPUFeature feature); 93 extern VP8CPUInfo VP8GetCPUInfo; 94 95 //------------------------------------------------------------------------------ 96 // Encoding 97 98 // Transforms 99 // VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms 100 // will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4). 101 typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst, 102 int do_two); 103 typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out); 104 typedef void (*VP8WHT)(const int16_t* in, int16_t* out); 105 extern VP8Idct VP8ITransform; 106 extern VP8Fdct VP8FTransform; 107 extern VP8WHT VP8FTransformWHT; 108 // Predictions 109 // *dst is the destination block. *top and *left can be NULL. 110 typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left, 111 const uint8_t* top); 112 typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top); 113 extern VP8Intra4Preds VP8EncPredLuma4; 114 extern VP8IntraPreds VP8EncPredLuma16; 115 extern VP8IntraPreds VP8EncPredChroma8; 116 117 typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref); 118 extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4; 119 typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref, 120 const uint16_t* const weights); 121 extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16; 122 123 typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst); 124 extern VP8BlockCopy VP8Copy4x4; 125 // Quantization 126 struct VP8Matrix; // forward declaration 127 typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16], 128 const struct VP8Matrix* const mtx); 129 extern VP8QuantizeBlock VP8EncQuantizeBlock; 130 131 // specific to 2nd transform: 132 typedef int (*VP8QuantizeBlockWHT)(int16_t in[16], int16_t out[16], 133 const struct VP8Matrix* const mtx); 134 extern VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT; 135 136 // Collect histogram for susceptibility calculation and accumulate in histo[]. 137 struct VP8Histogram; 138 typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred, 139 int start_block, int end_block, 140 struct VP8Histogram* const histo); 141 extern const int VP8DspScan[16 + 4 + 4]; 142 extern VP8CHisto VP8CollectHistogram; 143 144 void VP8EncDspInit(void); // must be called before using any of the above 145 146 //------------------------------------------------------------------------------ 147 // Decoding 148 149 typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst); 150 // when doing two transforms, coeffs is actually int16_t[2][16]. 151 typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two); 152 extern VP8DecIdct2 VP8Transform; 153 extern VP8DecIdct VP8TransformAC3; 154 extern VP8DecIdct VP8TransformUV; 155 extern VP8DecIdct VP8TransformDC; 156 extern VP8DecIdct VP8TransformDCUV; 157 extern VP8WHT VP8TransformWHT; 158 159 // *dst is the destination block, with stride BPS. Boundary samples are 160 // assumed accessible when needed. 161 typedef void (*VP8PredFunc)(uint8_t* dst); 162 extern const VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */]; 163 extern const VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */]; 164 extern const VP8PredFunc VP8PredLuma4[/* NUM_BMODES */]; 165 166 // clipping tables (for filtering) 167 extern const int8_t* const VP8ksclip1; // clips [-1020, 1020] to [-128, 127] 168 extern const int8_t* const VP8ksclip2; // clips [-112, 112] to [-16, 15] 169 extern const uint8_t* const VP8kclip1; // clips [-255,511] to [0,255] 170 extern const uint8_t* const VP8kabs0; // abs(x) for x in [-255,255] 171 void VP8InitClipTables(void); // must be called first 172 173 // simple filter (only for luma) 174 typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh); 175 extern VP8SimpleFilterFunc VP8SimpleVFilter16; 176 extern VP8SimpleFilterFunc VP8SimpleHFilter16; 177 extern VP8SimpleFilterFunc VP8SimpleVFilter16i; // filter 3 inner edges 178 extern VP8SimpleFilterFunc VP8SimpleHFilter16i; 179 180 // regular filter (on both macroblock edges and inner edges) 181 typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride, 182 int thresh, int ithresh, int hev_t); 183 typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride, 184 int thresh, int ithresh, int hev_t); 185 // on outer edge 186 extern VP8LumaFilterFunc VP8VFilter16; 187 extern VP8LumaFilterFunc VP8HFilter16; 188 extern VP8ChromaFilterFunc VP8VFilter8; 189 extern VP8ChromaFilterFunc VP8HFilter8; 190 191 // on inner edge 192 extern VP8LumaFilterFunc VP8VFilter16i; // filtering 3 inner edges altogether 193 extern VP8LumaFilterFunc VP8HFilter16i; 194 extern VP8ChromaFilterFunc VP8VFilter8i; // filtering u and v altogether 195 extern VP8ChromaFilterFunc VP8HFilter8i; 196 197 // must be called before anything using the above 198 void VP8DspInit(void); 199 200 //------------------------------------------------------------------------------ 201 // WebP I/O 202 203 #define FANCY_UPSAMPLING // undefined to remove fancy upsampling support 204 205 // Convert a pair of y/u/v lines together to the output rgb/a colorspace. 206 // bottom_y can be NULL if only one line of output is needed (at top/bottom). 207 typedef void (*WebPUpsampleLinePairFunc)( 208 const uint8_t* top_y, const uint8_t* bottom_y, 209 const uint8_t* top_u, const uint8_t* top_v, 210 const uint8_t* cur_u, const uint8_t* cur_v, 211 uint8_t* top_dst, uint8_t* bottom_dst, int len); 212 213 #ifdef FANCY_UPSAMPLING 214 215 // Fancy upsampling functions to convert YUV to RGB(A) modes 216 extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */]; 217 218 #endif // FANCY_UPSAMPLING 219 220 // Per-row point-sampling methods. 221 typedef void (*WebPSamplerRowFunc)(const uint8_t* y, 222 const uint8_t* u, const uint8_t* v, 223 uint8_t* dst, int len); 224 // Generic function to apply 'WebPSamplerRowFunc' to the whole plane: 225 void WebPSamplerProcessPlane(const uint8_t* y, int y_stride, 226 const uint8_t* u, const uint8_t* v, int uv_stride, 227 uint8_t* dst, int dst_stride, 228 int width, int height, WebPSamplerRowFunc func); 229 230 // Sampling functions to convert rows of YUV to RGB(A) 231 extern WebPSamplerRowFunc WebPSamplers[/* MODE_LAST */]; 232 233 // General function for converting two lines of ARGB or RGBA. 234 // 'alpha_is_last' should be true if 0xff000000 is stored in memory as 235 // as 0x00, 0x00, 0x00, 0xff (little endian). 236 WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last); 237 238 // YUV444->RGB converters 239 typedef void (*WebPYUV444Converter)(const uint8_t* y, 240 const uint8_t* u, const uint8_t* v, 241 uint8_t* dst, int len); 242 243 extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */]; 244 245 // Must be called before using the WebPUpsamplers[] (and for premultiplied 246 // colorspaces like rgbA, rgbA4444, etc) 247 void WebPInitUpsamplers(void); 248 // Must be called before using WebPSamplers[] 249 void WebPInitSamplers(void); 250 251 //------------------------------------------------------------------------------ 252 // Utilities for processing transparent channel. 253 254 // Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h. 255 // alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last). 256 extern void (*WebPApplyAlphaMultiply)( 257 uint8_t* rgba, int alpha_first, int w, int h, int stride); 258 259 // Same, buf specifically for RGBA4444 format 260 extern void (*WebPApplyAlphaMultiply4444)( 261 uint8_t* rgba4444, int w, int h, int stride); 262 263 // Extract the alpha values from 32b values in argb[] and pack them into alpha[] 264 // (this is the opposite of WebPDispatchAlpha). 265 // Returns true if there's only trivial 0xff alpha values. 266 extern int (*WebPExtractAlpha)(const uint8_t* argb, int argb_stride, 267 int width, int height, 268 uint8_t* alpha, int alpha_stride); 269 270 // Pre-Multiply operation transforms x into x * A / 255 (where x=Y,R,G or B). 271 // Un-Multiply operation transforms x into x * 255 / A. 272 273 // Pre-Multiply or Un-Multiply (if 'inverse' is true) argb values in a row. 274 extern void (*WebPMultARGBRow)(uint32_t* const ptr, int width, int inverse); 275 276 // Same a WebPMultARGBRow(), but for several rows. 277 void WebPMultARGBRows(uint8_t* ptr, int stride, int width, int num_rows, 278 int inverse); 279 280 // Same for a row of single values, with side alpha values. 281 extern void (*WebPMultRow)(uint8_t* const ptr, const uint8_t* const alpha, 282 int width, int inverse); 283 284 // Same a WebPMultRow(), but for several 'num_rows' rows. 285 void WebPMultRows(uint8_t* ptr, int stride, 286 const uint8_t* alpha, int alpha_stride, 287 int width, int num_rows, int inverse); 288 289 // To be called first before using the above. 290 void WebPInitAlphaProcessing(void); 291 292 #ifdef __cplusplus 293 } // extern "C" 294 #endif 295 296 #endif /* WEBP_DSP_DSP_H_ */ 297