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