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