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__) && defined(__ARM_NEON__)
     33 #define WEBP_ANDROID_NEON  // Android targets that might support NEON
     34 #endif
     35 
     36 #if defined(__ARM_NEON__) || defined(WEBP_ANDROID_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 int VP8GetAlpha(const int histo[]);
     53 
     54 // Transforms
     55 // VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
     56 //          will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
     57 typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
     58                         int do_two);
     59 typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
     60 typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
     61 extern VP8Idct VP8ITransform;
     62 extern VP8Fdct VP8FTransform;
     63 extern VP8WHT VP8ITransformWHT;
     64 extern VP8WHT VP8FTransformWHT;
     65 // Predictions
     66 // *dst is the destination block. *top and *left can be NULL.
     67 typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
     68                               const uint8_t* top);
     69 typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
     70 extern VP8Intra4Preds VP8EncPredLuma4;
     71 extern VP8IntraPreds VP8EncPredLuma16;
     72 extern VP8IntraPreds VP8EncPredChroma8;
     73 
     74 typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
     75 extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
     76 typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
     77                           const uint16_t* const weights);
     78 extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
     79 
     80 typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
     81 extern VP8BlockCopy VP8Copy4x4;
     82 // Quantization
     83 struct VP8Matrix;   // forward declaration
     84 typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
     85                                 int n, const struct VP8Matrix* const mtx);
     86 extern VP8QuantizeBlock VP8EncQuantizeBlock;
     87 
     88 // Compute susceptibility based on DCT-coeff histograms:
     89 // the higher, the "easier" the macroblock is to compress.
     90 typedef int (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
     91                          int start_block, int end_block);
     92 extern const int VP8DspScan[16 + 4 + 4];
     93 extern VP8CHisto VP8CollectHistogram;
     94 
     95 void VP8EncDspInit(void);   // must be called before using any of the above
     96 
     97 //------------------------------------------------------------------------------
     98 // Decoding
     99 
    100 typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
    101 // when doing two transforms, coeffs is actually int16_t[2][16].
    102 typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
    103 extern VP8DecIdct2 VP8Transform;
    104 extern VP8DecIdct VP8TransformUV;
    105 extern VP8DecIdct VP8TransformDC;
    106 extern VP8DecIdct VP8TransformDCUV;
    107 extern void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
    108 
    109 // *dst is the destination block, with stride BPS. Boundary samples are
    110 // assumed accessible when needed.
    111 typedef void (*VP8PredFunc)(uint8_t* dst);
    112 extern const VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
    113 extern const VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
    114 extern const VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
    115 
    116 // simple filter (only for luma)
    117 typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
    118 extern VP8SimpleFilterFunc VP8SimpleVFilter16;
    119 extern VP8SimpleFilterFunc VP8SimpleHFilter16;
    120 extern VP8SimpleFilterFunc VP8SimpleVFilter16i;  // filter 3 inner edges
    121 extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
    122 
    123 // regular filter (on both macroblock edges and inner edges)
    124 typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
    125                                   int thresh, int ithresh, int hev_t);
    126 typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
    127                                     int thresh, int ithresh, int hev_t);
    128 // on outer edge
    129 extern VP8LumaFilterFunc VP8VFilter16;
    130 extern VP8LumaFilterFunc VP8HFilter16;
    131 extern VP8ChromaFilterFunc VP8VFilter8;
    132 extern VP8ChromaFilterFunc VP8HFilter8;
    133 
    134 // on inner edge
    135 extern VP8LumaFilterFunc VP8VFilter16i;   // filtering 3 inner edges altogether
    136 extern VP8LumaFilterFunc VP8HFilter16i;
    137 extern VP8ChromaFilterFunc VP8VFilter8i;  // filtering u and v altogether
    138 extern VP8ChromaFilterFunc VP8HFilter8i;
    139 
    140 // must be called before anything using the above
    141 void VP8DspInit(void);
    142 
    143 //------------------------------------------------------------------------------
    144 // WebP I/O
    145 
    146 #define FANCY_UPSAMPLING   // undefined to remove fancy upsampling support
    147 
    148 typedef void (*WebPUpsampleLinePairFunc)(
    149     const uint8_t* top_y, const uint8_t* bottom_y,
    150     const uint8_t* top_u, const uint8_t* top_v,
    151     const uint8_t* cur_u, const uint8_t* cur_v,
    152     uint8_t* top_dst, uint8_t* bottom_dst, int len);
    153 
    154 #ifdef FANCY_UPSAMPLING
    155 
    156 // Fancy upsampling functions to convert YUV to RGB(A) modes
    157 extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
    158 
    159 // Initializes SSE2 version of the fancy upsamplers.
    160 void WebPInitUpsamplersSSE2(void);
    161 
    162 #endif    // FANCY_UPSAMPLING
    163 
    164 // Point-sampling methods.
    165 typedef void (*WebPSampleLinePairFunc)(
    166     const uint8_t* top_y, const uint8_t* bottom_y,
    167     const uint8_t* u, const uint8_t* v,
    168     uint8_t* top_dst, uint8_t* bottom_dst, int len);
    169 
    170 extern const WebPSampleLinePairFunc WebPSamplers[/* MODE_LAST */];
    171 
    172 // General function for converting two lines of ARGB or RGBA.
    173 // 'alpha_is_last' should be true if 0xff000000 is stored in memory as
    174 // as 0x00, 0x00, 0x00, 0xff (little endian).
    175 WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
    176 
    177 // YUV444->RGB converters
    178 typedef void (*WebPYUV444Converter)(const uint8_t* y,
    179                                     const uint8_t* u, const uint8_t* v,
    180                                     uint8_t* dst, int len);
    181 
    182 extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
    183 
    184 // Main function to be called
    185 void WebPInitUpsamplers(void);
    186 
    187 //------------------------------------------------------------------------------
    188 // Pre-multiply planes with alpha values
    189 
    190 // Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
    191 // alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
    192 extern void (*WebPApplyAlphaMultiply)(
    193     uint8_t* rgba, int alpha_first, int w, int h, int stride);
    194 
    195 // Same, buf specifically for RGBA4444 format
    196 extern void (*WebPApplyAlphaMultiply4444)(
    197     uint8_t* rgba4444, int w, int h, int stride);
    198 
    199 // To be called first before using the above.
    200 void WebPInitPremultiply(void);
    201 
    202 void WebPInitPremultiplySSE2(void);   // should not be called directly.
    203 
    204 //------------------------------------------------------------------------------
    205 
    206 #if defined(__cplusplus) || defined(c_plusplus)
    207 }    // extern "C"
    208 #endif
    209 
    210 #endif  /* WEBP_DSP_DSP_H_ */
    211