Home | History | Annotate | Download | only in dsp
      1 // Copyright 2011 Google Inc. All Rights Reserved.
      2 //
      3 // This code is licensed under the same terms as WebM:
      4 //  Software License Agreement:  http://www.webmproject.org/license/software/
      5 //  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
      6 // -----------------------------------------------------------------------------
      7 //
      8 //   Speed-critical functions.
      9 //
     10 // Author: Skal (pascal.massimino (at) gmail.com)
     11 
     12 #ifndef WEBP_DSP_DSP_H_
     13 #define WEBP_DSP_DSP_H_
     14 
     15 #include "webp/types.h"
     16 
     17 #if defined(__cplusplus) || defined(c_plusplus)
     18 extern "C" {
     19 #endif
     20 
     21 //------------------------------------------------------------------------------
     22 // CPU detection
     23 
     24 #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
     25 #define WEBP_MSC_SSE2  // Visual C++ SSE2 targets
     26 #endif
     27 
     28 #if defined(__SSE2__) || defined(WEBP_MSC_SSE2)
     29 #define WEBP_USE_SSE2
     30 #endif
     31 
     32 #if defined(__ANDROID__) && defined(__ARM_ARCH_7A__)
     33 #define WEBP_ANDROID_NEON  // Android targets that might support NEON
     34 #endif
     35 
     36 #if defined(__ARM_NEON__)
     37 #define WEBP_USE_NEON
     38 #endif
     39 
     40 typedef enum {
     41   kSSE2,
     42   kSSE3,
     43   kNEON
     44 } CPUFeature;
     45 // returns true if the CPU supports the feature.
     46 typedef int (*VP8CPUInfo)(CPUFeature feature);
     47 extern VP8CPUInfo VP8GetCPUInfo;
     48 
     49 //------------------------------------------------------------------------------
     50 // Encoding
     51 
     52 // Transforms
     53 // VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
     54 //          will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
     55 typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
     56                         int do_two);
     57 typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
     58 typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
     59 extern VP8Idct VP8ITransform;
     60 extern VP8Fdct VP8FTransform;
     61 extern VP8WHT VP8ITransformWHT;
     62 extern VP8WHT VP8FTransformWHT;
     63 // Predictions
     64 // *dst is the destination block. *top and *left can be NULL.
     65 typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
     66                               const uint8_t* top);
     67 typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
     68 extern VP8Intra4Preds VP8EncPredLuma4;
     69 extern VP8IntraPreds VP8EncPredLuma16;
     70 extern VP8IntraPreds VP8EncPredChroma8;
     71 
     72 typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
     73 extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
     74 typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
     75                           const uint16_t* const weights);
     76 extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
     77 
     78 typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
     79 extern VP8BlockCopy VP8Copy4x4;
     80 // Quantization
     81 struct VP8Matrix;   // forward declaration
     82 typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
     83                                 int n, const struct VP8Matrix* const mtx);
     84 extern VP8QuantizeBlock VP8EncQuantizeBlock;
     85 
     86 // Collect histogram for susceptibility calculation and accumulate in histo[].
     87 struct VP8Histogram;
     88 typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
     89                           int start_block, int end_block,
     90                           struct VP8Histogram* const histo);
     91 extern const int VP8DspScan[16 + 4 + 4];
     92 extern VP8CHisto VP8CollectHistogram;
     93 
     94 void VP8EncDspInit(void);   // must be called before using any of the above
     95 
     96 //------------------------------------------------------------------------------
     97 // Decoding
     98 
     99 typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
    100 // when doing two transforms, coeffs is actually int16_t[2][16].
    101 typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
    102 extern VP8DecIdct2 VP8Transform;
    103 extern VP8DecIdct VP8TransformUV;
    104 extern VP8DecIdct VP8TransformDC;
    105 extern VP8DecIdct VP8TransformDCUV;
    106 extern VP8WHT VP8TransformWHT;
    107 
    108 // *dst is the destination block, with stride BPS. Boundary samples are
    109 // assumed accessible when needed.
    110 typedef void (*VP8PredFunc)(uint8_t* dst);
    111 extern const VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
    112 extern const VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
    113 extern const VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
    114 
    115 // simple filter (only for luma)
    116 typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
    117 extern VP8SimpleFilterFunc VP8SimpleVFilter16;
    118 extern VP8SimpleFilterFunc VP8SimpleHFilter16;
    119 extern VP8SimpleFilterFunc VP8SimpleVFilter16i;  // filter 3 inner edges
    120 extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
    121 
    122 // regular filter (on both macroblock edges and inner edges)
    123 typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
    124                                   int thresh, int ithresh, int hev_t);
    125 typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
    126                                     int thresh, int ithresh, int hev_t);
    127 // on outer edge
    128 extern VP8LumaFilterFunc VP8VFilter16;
    129 extern VP8LumaFilterFunc VP8HFilter16;
    130 extern VP8ChromaFilterFunc VP8VFilter8;
    131 extern VP8ChromaFilterFunc VP8HFilter8;
    132 
    133 // on inner edge
    134 extern VP8LumaFilterFunc VP8VFilter16i;   // filtering 3 inner edges altogether
    135 extern VP8LumaFilterFunc VP8HFilter16i;
    136 extern VP8ChromaFilterFunc VP8VFilter8i;  // filtering u and v altogether
    137 extern VP8ChromaFilterFunc VP8HFilter8i;
    138 
    139 // must be called before anything using the above
    140 void VP8DspInit(void);
    141 
    142 //------------------------------------------------------------------------------
    143 // WebP I/O
    144 
    145 #define FANCY_UPSAMPLING   // undefined to remove fancy upsampling support
    146 
    147 typedef void (*WebPUpsampleLinePairFunc)(
    148     const uint8_t* top_y, const uint8_t* bottom_y,
    149     const uint8_t* top_u, const uint8_t* top_v,
    150     const uint8_t* cur_u, const uint8_t* cur_v,
    151     uint8_t* top_dst, uint8_t* bottom_dst, int len);
    152 
    153 #ifdef FANCY_UPSAMPLING
    154 
    155 // Fancy upsampling functions to convert YUV to RGB(A) modes
    156 extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
    157 
    158 // Initializes SSE2 version of the fancy upsamplers.
    159 void WebPInitUpsamplersSSE2(void);
    160 
    161 // NEON version
    162 void WebPInitUpsamplersNEON(void);
    163 
    164 #endif    // FANCY_UPSAMPLING
    165 
    166 // Point-sampling methods.
    167 typedef void (*WebPSampleLinePairFunc)(
    168     const uint8_t* top_y, const uint8_t* bottom_y,
    169     const uint8_t* u, const uint8_t* v,
    170     uint8_t* top_dst, uint8_t* bottom_dst, int len);
    171 
    172 extern const WebPSampleLinePairFunc WebPSamplers[/* MODE_LAST */];
    173 
    174 // General function for converting two lines of ARGB or RGBA.
    175 // 'alpha_is_last' should be true if 0xff000000 is stored in memory as
    176 // as 0x00, 0x00, 0x00, 0xff (little endian).
    177 WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
    178 
    179 // YUV444->RGB converters
    180 typedef void (*WebPYUV444Converter)(const uint8_t* y,
    181                                     const uint8_t* u, const uint8_t* v,
    182                                     uint8_t* dst, int len);
    183 
    184 extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
    185 
    186 // Main function to be called
    187 void WebPInitUpsamplers(void);
    188 
    189 //------------------------------------------------------------------------------
    190 // Pre-multiply planes with alpha values
    191 
    192 // Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
    193 // alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
    194 extern void (*WebPApplyAlphaMultiply)(
    195     uint8_t* rgba, int alpha_first, int w, int h, int stride);
    196 
    197 // Same, buf specifically for RGBA4444 format
    198 extern void (*WebPApplyAlphaMultiply4444)(
    199     uint8_t* rgba4444, int w, int h, int stride);
    200 
    201 // To be called first before using the above.
    202 void WebPInitPremultiply(void);
    203 
    204 void WebPInitPremultiplySSE2(void);   // should not be called directly.
    205 void WebPInitPremultiplyNEON(void);
    206 
    207 //------------------------------------------------------------------------------
    208 
    209 #if defined(__cplusplus) || defined(c_plusplus)
    210 }    // extern "C"
    211 #endif
    212 
    213 #endif  /* WEBP_DSP_DSP_H_ */
    214