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