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 "src/webp/config.h"
     19 #endif
     20 
     21 #include "src/webp/types.h"
     22 
     23 #ifdef __cplusplus
     24 extern "C" {
     25 #endif
     26 
     27 #define BPS 32   // this is the common stride for enc/dec
     28 
     29 //------------------------------------------------------------------------------
     30 // CPU detection
     31 
     32 #if defined(__GNUC__)
     33 # define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
     34 # define LOCAL_GCC_PREREQ(maj, min) \
     35     (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
     36 #else
     37 # define LOCAL_GCC_VERSION 0
     38 # define LOCAL_GCC_PREREQ(maj, min) 0
     39 #endif
     40 
     41 #if defined(__clang__)
     42 # define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__)
     43 # define LOCAL_CLANG_PREREQ(maj, min) \
     44     (LOCAL_CLANG_VERSION >= (((maj) << 8) | (min)))
     45 #else
     46 # define LOCAL_CLANG_VERSION 0
     47 # define LOCAL_CLANG_PREREQ(maj, min) 0
     48 #endif
     49 
     50 #ifndef __has_builtin
     51 # define __has_builtin(x) 0
     52 #endif
     53 
     54 // for now, none of the optimizations below are available in emscripten
     55 #if !defined(EMSCRIPTEN)
     56 
     57 #if defined(_MSC_VER) && _MSC_VER > 1310 && \
     58     (defined(_M_X64) || defined(_M_IX86))
     59 #define WEBP_MSC_SSE2  // Visual C++ SSE2 targets
     60 #endif
     61 
     62 #if defined(_MSC_VER) && _MSC_VER >= 1500 && \
     63     (defined(_M_X64) || defined(_M_IX86))
     64 #define WEBP_MSC_SSE41  // Visual C++ SSE4.1 targets
     65 #endif
     66 
     67 // WEBP_HAVE_* are used to indicate the presence of the instruction set in dsp
     68 // files without intrinsics, allowing the corresponding Init() to be called.
     69 // Files containing intrinsics will need to be built targeting the instruction
     70 // set so should succeed on one of the earlier tests.
     71 #if defined(__SSE2__) || defined(WEBP_MSC_SSE2) || defined(WEBP_HAVE_SSE2)
     72 #define WEBP_USE_SSE2
     73 #endif
     74 
     75 #if defined(__SSE4_1__) || defined(WEBP_MSC_SSE41) || defined(WEBP_HAVE_SSE41)
     76 #define WEBP_USE_SSE41
     77 #endif
     78 
     79 #if defined(__AVX2__) || defined(WEBP_HAVE_AVX2)
     80 #define WEBP_USE_AVX2
     81 #endif
     82 
     83 // The intrinsics currently cause compiler errors with arm-nacl-gcc and the
     84 // inline assembly would need to be modified for use with Native Client.
     85 #if (defined(__ARM_NEON__) || \
     86      defined(__aarch64__) || defined(WEBP_HAVE_NEON)) && \
     87     !defined(__native_client__)
     88 #define WEBP_USE_NEON
     89 #endif
     90 
     91 #if !defined(WEBP_USE_NEON) && defined(__ANDROID__) && \
     92     defined(__ARM_ARCH_7A__) && defined(HAVE_CPU_FEATURES_H)
     93 #define WEBP_ANDROID_NEON  // Android targets that may have NEON
     94 #define WEBP_USE_NEON
     95 #endif
     96 
     97 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
     98 #define WEBP_USE_NEON
     99 #define WEBP_USE_INTRINSICS
    100 #endif
    101 
    102 #if defined(__mips__) && !defined(__mips64) && \
    103     defined(__mips_isa_rev) && (__mips_isa_rev >= 1) && (__mips_isa_rev < 6)
    104 #define WEBP_USE_MIPS32
    105 #if (__mips_isa_rev >= 2)
    106 #define WEBP_USE_MIPS32_R2
    107 #if defined(__mips_dspr2) || (defined(__mips_dsp_rev) && __mips_dsp_rev >= 2)
    108 #define WEBP_USE_MIPS_DSP_R2
    109 #endif
    110 #endif
    111 #endif
    112 
    113 #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5)
    114 #define WEBP_USE_MSA
    115 #endif
    116 
    117 #endif  /* EMSCRIPTEN */
    118 
    119 #ifndef WEBP_DSP_OMIT_C_CODE
    120 #define WEBP_DSP_OMIT_C_CODE 1
    121 #endif
    122 
    123 #if (defined(__aarch64__) || defined(__ARM_NEON__)) && WEBP_DSP_OMIT_C_CODE
    124 #define WEBP_NEON_OMIT_C_CODE 1
    125 #else
    126 #define WEBP_NEON_OMIT_C_CODE 0
    127 #endif
    128 
    129 #if !(LOCAL_CLANG_PREREQ(3,8) || LOCAL_GCC_PREREQ(4,8) || defined(__aarch64__))
    130 #define WEBP_NEON_WORK_AROUND_GCC 1
    131 #else
    132 #define WEBP_NEON_WORK_AROUND_GCC 0
    133 #endif
    134 
    135 // This macro prevents thread_sanitizer from reporting known concurrent writes.
    136 #define WEBP_TSAN_IGNORE_FUNCTION
    137 #if defined(__has_feature)
    138 #if __has_feature(thread_sanitizer)
    139 #undef WEBP_TSAN_IGNORE_FUNCTION
    140 #define WEBP_TSAN_IGNORE_FUNCTION __attribute__((no_sanitize_thread))
    141 #endif
    142 #endif
    143 
    144 #define WEBP_UBSAN_IGNORE_UNDEF
    145 #define WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW
    146 #if defined(__clang__) && defined(__has_attribute)
    147 #if __has_attribute(no_sanitize)
    148 // This macro prevents the undefined behavior sanitizer from reporting
    149 // failures. This is only meant to silence unaligned loads on platforms that
    150 // are known to support them.
    151 #undef WEBP_UBSAN_IGNORE_UNDEF
    152 #define WEBP_UBSAN_IGNORE_UNDEF \
    153   __attribute__((no_sanitize("undefined")))
    154 
    155 // This macro prevents the undefined behavior sanitizer from reporting
    156 // failures related to unsigned integer overflows. This is only meant to
    157 // silence cases where this well defined behavior is expected.
    158 #undef WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW
    159 #define WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW \
    160   __attribute__((no_sanitize("unsigned-integer-overflow")))
    161 #endif
    162 #endif
    163 
    164 // Regularize the definition of WEBP_SWAP_16BIT_CSP (backward compatibility)
    165 #if !defined(WEBP_SWAP_16BIT_CSP)
    166 #define WEBP_SWAP_16BIT_CSP 0
    167 #endif
    168 
    169 typedef enum {
    170   kSSE2,
    171   kSSE3,
    172   kSlowSSSE3,  // special feature for slow SSSE3 architectures
    173   kSSE4_1,
    174   kAVX,
    175   kAVX2,
    176   kNEON,
    177   kMIPS32,
    178   kMIPSdspR2,
    179   kMSA
    180 } CPUFeature;
    181 // returns true if the CPU supports the feature.
    182 typedef int (*VP8CPUInfo)(CPUFeature feature);
    183 WEBP_EXTERN VP8CPUInfo VP8GetCPUInfo;
    184 
    185 //------------------------------------------------------------------------------
    186 // Init stub generator
    187 
    188 // Defines an init function stub to ensure each module exposes a symbol,
    189 // avoiding a compiler warning.
    190 #define WEBP_DSP_INIT_STUB(func) \
    191   extern void func(void); \
    192   WEBP_TSAN_IGNORE_FUNCTION void func(void) {}
    193 
    194 //------------------------------------------------------------------------------
    195 // Encoding
    196 
    197 // Transforms
    198 // VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
    199 //          will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
    200 typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
    201                         int do_two);
    202 typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
    203 typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
    204 extern VP8Idct VP8ITransform;
    205 extern VP8Fdct VP8FTransform;
    206 extern VP8Fdct VP8FTransform2;   // performs two transforms at a time
    207 extern VP8WHT VP8FTransformWHT;
    208 // Predictions
    209 // *dst is the destination block. *top and *left can be NULL.
    210 typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
    211                               const uint8_t* top);
    212 typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
    213 extern VP8Intra4Preds VP8EncPredLuma4;
    214 extern VP8IntraPreds VP8EncPredLuma16;
    215 extern VP8IntraPreds VP8EncPredChroma8;
    216 
    217 typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
    218 extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
    219 typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
    220                           const uint16_t* const weights);
    221 // The weights for VP8TDisto4x4 and VP8TDisto16x16 contain a row-major
    222 // 4 by 4 symmetric matrix.
    223 extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
    224 
    225 // Compute the average (DC) of four 4x4 blocks.
    226 // Each sub-4x4 block #i sum is stored in dc[i].
    227 typedef void (*VP8MeanMetric)(const uint8_t* ref, uint32_t dc[4]);
    228 extern VP8MeanMetric VP8Mean16x4;
    229 
    230 typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
    231 extern VP8BlockCopy VP8Copy4x4;
    232 extern VP8BlockCopy VP8Copy16x8;
    233 // Quantization
    234 struct VP8Matrix;   // forward declaration
    235 typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
    236                                 const struct VP8Matrix* const mtx);
    237 // Same as VP8QuantizeBlock, but quantizes two consecutive blocks.
    238 typedef int (*VP8Quantize2Blocks)(int16_t in[32], int16_t out[32],
    239                                   const struct VP8Matrix* const mtx);
    240 
    241 extern VP8QuantizeBlock VP8EncQuantizeBlock;
    242 extern VP8Quantize2Blocks VP8EncQuantize2Blocks;
    243 
    244 // specific to 2nd transform:
    245 typedef int (*VP8QuantizeBlockWHT)(int16_t in[16], int16_t out[16],
    246                                    const struct VP8Matrix* const mtx);
    247 extern VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT;
    248 
    249 extern const int VP8DspScan[16 + 4 + 4];
    250 
    251 // Collect histogram for susceptibility calculation.
    252 #define MAX_COEFF_THRESH   31   // size of histogram used by CollectHistogram.
    253 typedef struct {
    254   // We only need to store max_value and last_non_zero, not the distribution.
    255   int max_value;
    256   int last_non_zero;
    257 } VP8Histogram;
    258 typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
    259                           int start_block, int end_block,
    260                           VP8Histogram* const histo);
    261 extern VP8CHisto VP8CollectHistogram;
    262 // General-purpose util function to help VP8CollectHistogram().
    263 void VP8SetHistogramData(const int distribution[MAX_COEFF_THRESH + 1],
    264                          VP8Histogram* const histo);
    265 
    266 // must be called before using any of the above
    267 void VP8EncDspInit(void);
    268 
    269 //------------------------------------------------------------------------------
    270 // cost functions (encoding)
    271 
    272 extern const uint16_t VP8EntropyCost[256];        // 8bit fixed-point log(p)
    273 // approximate cost per level:
    274 extern const uint16_t VP8LevelFixedCosts[2047 /*MAX_LEVEL*/ + 1];
    275 extern const uint8_t VP8EncBands[16 + 1];
    276 
    277 struct VP8Residual;
    278 typedef void (*VP8SetResidualCoeffsFunc)(const int16_t* const coeffs,
    279                                          struct VP8Residual* const res);
    280 extern VP8SetResidualCoeffsFunc VP8SetResidualCoeffs;
    281 
    282 // Cost calculation function.
    283 typedef int (*VP8GetResidualCostFunc)(int ctx0,
    284                                       const struct VP8Residual* const res);
    285 extern VP8GetResidualCostFunc VP8GetResidualCost;
    286 
    287 // must be called before anything using the above
    288 void VP8EncDspCostInit(void);
    289 
    290 //------------------------------------------------------------------------------
    291 // SSIM / PSNR utils
    292 
    293 // struct for accumulating statistical moments
    294 typedef struct {
    295   uint32_t w;              // sum(w_i) : sum of weights
    296   uint32_t xm, ym;         // sum(w_i * x_i), sum(w_i * y_i)
    297   uint32_t xxm, xym, yym;  // sum(w_i * x_i * x_i), etc.
    298 } VP8DistoStats;
    299 
    300 // Compute the final SSIM value
    301 // The non-clipped version assumes stats->w = (2 * VP8_SSIM_KERNEL + 1)^2.
    302 double VP8SSIMFromStats(const VP8DistoStats* const stats);
    303 double VP8SSIMFromStatsClipped(const VP8DistoStats* const stats);
    304 
    305 #define VP8_SSIM_KERNEL 3   // total size of the kernel: 2 * VP8_SSIM_KERNEL + 1
    306 typedef double (*VP8SSIMGetClippedFunc)(const uint8_t* src1, int stride1,
    307                                         const uint8_t* src2, int stride2,
    308                                         int xo, int yo,  // center position
    309                                         int W, int H);   // plane dimension
    310 
    311 #if !defined(WEBP_REDUCE_SIZE)
    312 // This version is called with the guarantee that you can load 8 bytes and
    313 // 8 rows at offset src1 and src2
    314 typedef double (*VP8SSIMGetFunc)(const uint8_t* src1, int stride1,
    315                                  const uint8_t* src2, int stride2);
    316 
    317 extern VP8SSIMGetFunc VP8SSIMGet;         // unclipped / unchecked
    318 extern VP8SSIMGetClippedFunc VP8SSIMGetClipped;   // with clipping
    319 #endif
    320 
    321 #if !defined(WEBP_DISABLE_STATS)
    322 typedef uint32_t (*VP8AccumulateSSEFunc)(const uint8_t* src1,
    323                                          const uint8_t* src2, int len);
    324 extern VP8AccumulateSSEFunc VP8AccumulateSSE;
    325 #endif
    326 
    327 // must be called before using any of the above directly
    328 void VP8SSIMDspInit(void);
    329 
    330 //------------------------------------------------------------------------------
    331 // Decoding
    332 
    333 typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
    334 // when doing two transforms, coeffs is actually int16_t[2][16].
    335 typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
    336 extern VP8DecIdct2 VP8Transform;
    337 extern VP8DecIdct VP8TransformAC3;
    338 extern VP8DecIdct VP8TransformUV;
    339 extern VP8DecIdct VP8TransformDC;
    340 extern VP8DecIdct VP8TransformDCUV;
    341 extern VP8WHT VP8TransformWHT;
    342 
    343 // *dst is the destination block, with stride BPS. Boundary samples are
    344 // assumed accessible when needed.
    345 typedef void (*VP8PredFunc)(uint8_t* dst);
    346 extern VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
    347 extern VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
    348 extern VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
    349 
    350 // clipping tables (for filtering)
    351 extern const int8_t* const VP8ksclip1;  // clips [-1020, 1020] to [-128, 127]
    352 extern const int8_t* const VP8ksclip2;  // clips [-112, 112] to [-16, 15]
    353 extern const uint8_t* const VP8kclip1;  // clips [-255,511] to [0,255]
    354 extern const uint8_t* const VP8kabs0;   // abs(x) for x in [-255,255]
    355 // must be called first
    356 void VP8InitClipTables(void);
    357 
    358 // simple filter (only for luma)
    359 typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
    360 extern VP8SimpleFilterFunc VP8SimpleVFilter16;
    361 extern VP8SimpleFilterFunc VP8SimpleHFilter16;
    362 extern VP8SimpleFilterFunc VP8SimpleVFilter16i;  // filter 3 inner edges
    363 extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
    364 
    365 // regular filter (on both macroblock edges and inner edges)
    366 typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
    367                                   int thresh, int ithresh, int hev_t);
    368 typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
    369                                     int thresh, int ithresh, int hev_t);
    370 // on outer edge
    371 extern VP8LumaFilterFunc VP8VFilter16;
    372 extern VP8LumaFilterFunc VP8HFilter16;
    373 extern VP8ChromaFilterFunc VP8VFilter8;
    374 extern VP8ChromaFilterFunc VP8HFilter8;
    375 
    376 // on inner edge
    377 extern VP8LumaFilterFunc VP8VFilter16i;   // filtering 3 inner edges altogether
    378 extern VP8LumaFilterFunc VP8HFilter16i;
    379 extern VP8ChromaFilterFunc VP8VFilter8i;  // filtering u and v altogether
    380 extern VP8ChromaFilterFunc VP8HFilter8i;
    381 
    382 // Dithering. Combines dithering values (centered around 128) with dst[],
    383 // according to: dst[] = clip(dst[] + (((dither[]-128) + 8) >> 4)
    384 #define VP8_DITHER_DESCALE 4
    385 #define VP8_DITHER_DESCALE_ROUNDER (1 << (VP8_DITHER_DESCALE - 1))
    386 #define VP8_DITHER_AMP_BITS 7
    387 #define VP8_DITHER_AMP_CENTER (1 << VP8_DITHER_AMP_BITS)
    388 extern void (*VP8DitherCombine8x8)(const uint8_t* dither, uint8_t* dst,
    389                                    int dst_stride);
    390 
    391 // must be called before anything using the above
    392 void VP8DspInit(void);
    393 
    394 //------------------------------------------------------------------------------
    395 // WebP I/O
    396 
    397 #define FANCY_UPSAMPLING   // undefined to remove fancy upsampling support
    398 
    399 // Convert a pair of y/u/v lines together to the output rgb/a colorspace.
    400 // bottom_y can be NULL if only one line of output is needed (at top/bottom).
    401 typedef void (*WebPUpsampleLinePairFunc)(
    402     const uint8_t* top_y, const uint8_t* bottom_y,
    403     const uint8_t* top_u, const uint8_t* top_v,
    404     const uint8_t* cur_u, const uint8_t* cur_v,
    405     uint8_t* top_dst, uint8_t* bottom_dst, int len);
    406 
    407 #ifdef FANCY_UPSAMPLING
    408 
    409 // Fancy upsampling functions to convert YUV to RGB(A) modes
    410 extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
    411 
    412 #endif    // FANCY_UPSAMPLING
    413 
    414 // Per-row point-sampling methods.
    415 typedef void (*WebPSamplerRowFunc)(const uint8_t* y,
    416                                    const uint8_t* u, const uint8_t* v,
    417                                    uint8_t* dst, int len);
    418 // Generic function to apply 'WebPSamplerRowFunc' to the whole plane:
    419 void WebPSamplerProcessPlane(const uint8_t* y, int y_stride,
    420                              const uint8_t* u, const uint8_t* v, int uv_stride,
    421                              uint8_t* dst, int dst_stride,
    422                              int width, int height, WebPSamplerRowFunc func);
    423 
    424 // Sampling functions to convert rows of YUV to RGB(A)
    425 extern WebPSamplerRowFunc WebPSamplers[/* MODE_LAST */];
    426 
    427 // General function for converting two lines of ARGB or RGBA.
    428 // 'alpha_is_last' should be true if 0xff000000 is stored in memory as
    429 // as 0x00, 0x00, 0x00, 0xff (little endian).
    430 WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
    431 
    432 // YUV444->RGB converters
    433 typedef void (*WebPYUV444Converter)(const uint8_t* y,
    434                                     const uint8_t* u, const uint8_t* v,
    435                                     uint8_t* dst, int len);
    436 
    437 extern WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
    438 
    439 // Must be called before using the WebPUpsamplers[] (and for premultiplied
    440 // colorspaces like rgbA, rgbA4444, etc)
    441 void WebPInitUpsamplers(void);
    442 // Must be called before using WebPSamplers[]
    443 void WebPInitSamplers(void);
    444 // Must be called before using WebPYUV444Converters[]
    445 void WebPInitYUV444Converters(void);
    446 
    447 //------------------------------------------------------------------------------
    448 // ARGB -> YUV converters
    449 
    450 // Convert ARGB samples to luma Y.
    451 extern void (*WebPConvertARGBToY)(const uint32_t* argb, uint8_t* y, int width);
    452 // Convert ARGB samples to U/V with downsampling. do_store should be '1' for
    453 // even lines and '0' for odd ones. 'src_width' is the original width, not
    454 // the U/V one.
    455 extern void (*WebPConvertARGBToUV)(const uint32_t* argb, uint8_t* u, uint8_t* v,
    456                                    int src_width, int do_store);
    457 
    458 // Convert a row of accumulated (four-values) of rgba32 toward U/V
    459 extern void (*WebPConvertRGBA32ToUV)(const uint16_t* rgb,
    460                                      uint8_t* u, uint8_t* v, int width);
    461 
    462 // Convert RGB or BGR to Y
    463 extern void (*WebPConvertRGB24ToY)(const uint8_t* rgb, uint8_t* y, int width);
    464 extern void (*WebPConvertBGR24ToY)(const uint8_t* bgr, uint8_t* y, int width);
    465 
    466 // used for plain-C fallback.
    467 extern void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
    468                                   int src_width, int do_store);
    469 extern void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
    470                                     uint8_t* u, uint8_t* v, int width);
    471 
    472 // utilities for accurate RGB->YUV conversion
    473 extern uint64_t (*WebPSharpYUVUpdateY)(const uint16_t* src, const uint16_t* ref,
    474                                        uint16_t* dst, int len);
    475 extern void (*WebPSharpYUVUpdateRGB)(const int16_t* src, const int16_t* ref,
    476                                      int16_t* dst, int len);
    477 extern void (*WebPSharpYUVFilterRow)(const int16_t* A, const int16_t* B,
    478                                      int len,
    479                                      const uint16_t* best_y, uint16_t* out);
    480 
    481 // Must be called before using the above.
    482 void WebPInitConvertARGBToYUV(void);
    483 
    484 //------------------------------------------------------------------------------
    485 // Rescaler
    486 
    487 struct WebPRescaler;
    488 
    489 // Import a row of data and save its contribution in the rescaler.
    490 // 'channel' denotes the channel number to be imported. 'Expand' corresponds to
    491 // the wrk->x_expand case. Otherwise, 'Shrink' is to be used.
    492 typedef void (*WebPRescalerImportRowFunc)(struct WebPRescaler* const wrk,
    493                                           const uint8_t* src);
    494 
    495 extern WebPRescalerImportRowFunc WebPRescalerImportRowExpand;
    496 extern WebPRescalerImportRowFunc WebPRescalerImportRowShrink;
    497 
    498 // Export one row (starting at x_out position) from rescaler.
    499 // 'Expand' corresponds to the wrk->y_expand case.
    500 // Otherwise 'Shrink' is to be used
    501 typedef void (*WebPRescalerExportRowFunc)(struct WebPRescaler* const wrk);
    502 extern WebPRescalerExportRowFunc WebPRescalerExportRowExpand;
    503 extern WebPRescalerExportRowFunc WebPRescalerExportRowShrink;
    504 
    505 // Plain-C implementation, as fall-back.
    506 extern void WebPRescalerImportRowExpand_C(struct WebPRescaler* const wrk,
    507                                           const uint8_t* src);
    508 extern void WebPRescalerImportRowShrink_C(struct WebPRescaler* const wrk,
    509                                           const uint8_t* src);
    510 extern void WebPRescalerExportRowExpand_C(struct WebPRescaler* const wrk);
    511 extern void WebPRescalerExportRowShrink_C(struct WebPRescaler* const wrk);
    512 
    513 // Main entry calls:
    514 extern void WebPRescalerImportRow(struct WebPRescaler* const wrk,
    515                                   const uint8_t* src);
    516 // Export one row (starting at x_out position) from rescaler.
    517 extern void WebPRescalerExportRow(struct WebPRescaler* const wrk);
    518 
    519 // Must be called first before using the above.
    520 void WebPRescalerDspInit(void);
    521 
    522 //------------------------------------------------------------------------------
    523 // Utilities for processing transparent channel.
    524 
    525 // Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
    526 // alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
    527 extern void (*WebPApplyAlphaMultiply)(
    528     uint8_t* rgba, int alpha_first, int w, int h, int stride);
    529 
    530 // Same, buf specifically for RGBA4444 format
    531 extern void (*WebPApplyAlphaMultiply4444)(
    532     uint8_t* rgba4444, int w, int h, int stride);
    533 
    534 // Dispatch the values from alpha[] plane to the ARGB destination 'dst'.
    535 // Returns true if alpha[] plane has non-trivial values different from 0xff.
    536 extern int (*WebPDispatchAlpha)(const uint8_t* alpha, int alpha_stride,
    537                                 int width, int height,
    538                                 uint8_t* dst, int dst_stride);
    539 
    540 // Transfer packed 8b alpha[] values to green channel in dst[], zero'ing the
    541 // A/R/B values. 'dst_stride' is the stride for dst[] in uint32_t units.
    542 extern void (*WebPDispatchAlphaToGreen)(const uint8_t* alpha, int alpha_stride,
    543                                         int width, int height,
    544                                         uint32_t* dst, int dst_stride);
    545 
    546 // Extract the alpha values from 32b values in argb[] and pack them into alpha[]
    547 // (this is the opposite of WebPDispatchAlpha).
    548 // Returns true if there's only trivial 0xff alpha values.
    549 extern int (*WebPExtractAlpha)(const uint8_t* argb, int argb_stride,
    550                                int width, int height,
    551                                uint8_t* alpha, int alpha_stride);
    552 
    553 // Extract the green values from 32b values in argb[] and pack them into alpha[]
    554 // (this is the opposite of WebPDispatchAlphaToGreen).
    555 extern void (*WebPExtractGreen)(const uint32_t* argb, uint8_t* alpha, int size);
    556 
    557 // Pre-Multiply operation transforms x into x * A / 255  (where x=Y,R,G or B).
    558 // Un-Multiply operation transforms x into x * 255 / A.
    559 
    560 // Pre-Multiply or Un-Multiply (if 'inverse' is true) argb values in a row.
    561 extern void (*WebPMultARGBRow)(uint32_t* const ptr, int width, int inverse);
    562 
    563 // Same a WebPMultARGBRow(), but for several rows.
    564 void WebPMultARGBRows(uint8_t* ptr, int stride, int width, int num_rows,
    565                       int inverse);
    566 
    567 // Same for a row of single values, with side alpha values.
    568 extern void (*WebPMultRow)(uint8_t* const ptr, const uint8_t* const alpha,
    569                            int width, int inverse);
    570 
    571 // Same a WebPMultRow(), but for several 'num_rows' rows.
    572 void WebPMultRows(uint8_t* ptr, int stride,
    573                   const uint8_t* alpha, int alpha_stride,
    574                   int width, int num_rows, int inverse);
    575 
    576 // Plain-C versions, used as fallback by some implementations.
    577 void WebPMultRow_C(uint8_t* const ptr, const uint8_t* const alpha,
    578                    int width, int inverse);
    579 void WebPMultARGBRow_C(uint32_t* const ptr, int width, int inverse);
    580 
    581 // RGB packing function. 'step' can be 3 or 4. r/g/b input is rgb or bgr order.
    582 extern void (*WebPPackRGB)(const uint8_t* r, const uint8_t* g, const uint8_t* b,
    583                            int len, int step, uint32_t* out);
    584 
    585 // This function returns true if src[i] contains a value different from 0xff.
    586 extern int (*WebPHasAlpha8b)(const uint8_t* src, int length);
    587 // This function returns true if src[4*i] contains a value different from 0xff.
    588 extern int (*WebPHasAlpha32b)(const uint8_t* src, int length);
    589 
    590 // To be called first before using the above.
    591 void WebPInitAlphaProcessing(void);
    592 
    593 //------------------------------------------------------------------------------
    594 // Filter functions
    595 
    596 typedef enum {     // Filter types.
    597   WEBP_FILTER_NONE = 0,
    598   WEBP_FILTER_HORIZONTAL,
    599   WEBP_FILTER_VERTICAL,
    600   WEBP_FILTER_GRADIENT,
    601   WEBP_FILTER_LAST = WEBP_FILTER_GRADIENT + 1,  // end marker
    602   WEBP_FILTER_BEST,    // meta-types
    603   WEBP_FILTER_FAST
    604 } WEBP_FILTER_TYPE;
    605 
    606 typedef void (*WebPFilterFunc)(const uint8_t* in, int width, int height,
    607                                int stride, uint8_t* out);
    608 // In-place un-filtering.
    609 // Warning! 'prev_line' pointer can be equal to 'cur_line' or 'preds'.
    610 typedef void (*WebPUnfilterFunc)(const uint8_t* prev_line, const uint8_t* preds,
    611                                  uint8_t* cur_line, int width);
    612 
    613 // Filter the given data using the given predictor.
    614 // 'in' corresponds to a 2-dimensional pixel array of size (stride * height)
    615 // in raster order.
    616 // 'stride' is number of bytes per scan line (with possible padding).
    617 // 'out' should be pre-allocated.
    618 extern WebPFilterFunc WebPFilters[WEBP_FILTER_LAST];
    619 
    620 // In-place reconstruct the original data from the given filtered data.
    621 // The reconstruction will be done for 'num_rows' rows starting from 'row'
    622 // (assuming rows upto 'row - 1' are already reconstructed).
    623 extern WebPUnfilterFunc WebPUnfilters[WEBP_FILTER_LAST];
    624 
    625 // To be called first before using the above.
    626 void VP8FiltersInit(void);
    627 
    628 #ifdef __cplusplus
    629 }    // extern "C"
    630 #endif
    631 
    632 #endif  /* WEBP_DSP_DSP_H_ */
    633