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
24extern "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
208typedef 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.
221typedef int (*VP8CPUInfo)(CPUFeature feature);
222WEBP_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).
239typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
240 int do_two);
241typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
242typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
243extern VP8Idct VP8ITransform;
244extern VP8Fdct VP8FTransform;
245extern VP8Fdct VP8FTransform2; // performs two transforms at a time
246extern VP8WHT VP8FTransformWHT;
247// Predictions
248// *dst is the destination block. *top and *left can be NULL.
249typedef void (*VP8IntraPreds)(uint8_t* dst, const uint8_t* left,
250 const uint8_t* top);
251typedef void (*VP8Intra4Preds)(uint8_t* dst, const uint8_t* top);
252extern VP8Intra4Preds VP8EncPredLuma4;
253extern VP8IntraPreds VP8EncPredLuma16;
254extern VP8IntraPreds VP8EncPredChroma8;
255
256typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
257extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
258typedef 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.
262extern 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].
266typedef void (*VP8MeanMetric)(const uint8_t* ref, uint32_t dc[4]);
267extern VP8MeanMetric VP8Mean16x4;
268
269typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
270extern VP8BlockCopy VP8Copy4x4;
271extern VP8BlockCopy VP8Copy16x8;
272// Quantization
273struct VP8Matrix; // forward declaration
274typedef 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.
277typedef int (*VP8Quantize2Blocks)(int16_t in[32], int16_t out[32],
278 const struct VP8Matrix* const mtx);
279
280extern VP8QuantizeBlock VP8EncQuantizeBlock;
281extern VP8Quantize2Blocks VP8EncQuantize2Blocks;
282
283// specific to 2nd transform:
284typedef int (*VP8QuantizeBlockWHT)(int16_t in[16], int16_t out[16],
285 const struct VP8Matrix* const mtx);
286extern VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT;
287
288extern 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.
292typedef 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;
297typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
298 int start_block, int end_block,
299 VP8Histogram* const histo);
300extern VP8CHisto VP8CollectHistogram;
301// General-purpose util function to help VP8CollectHistogram().
302void VP8SetHistogramData(const int distribution[MAX_COEFF_THRESH + 1],
303 VP8Histogram* const histo);
304
305// must be called before using any of the above
306void VP8EncDspInit(void);
307
308//------------------------------------------------------------------------------
309// cost functions (encoding)
310
311extern const uint16_t VP8EntropyCost[256]; // 8bit fixed-point log(p)
312// approximate cost per level:
313extern const uint16_t VP8LevelFixedCosts[2047 /*MAX_LEVEL*/ + 1];
314extern const uint8_t VP8EncBands[16 + 1];
315
316struct VP8Residual;
317typedef void (*VP8SetResidualCoeffsFunc)(const int16_t* const coeffs,
318 struct VP8Residual* const res);
319extern VP8SetResidualCoeffsFunc VP8SetResidualCoeffs;
320
321// Cost calculation function.
322typedef int (*VP8GetResidualCostFunc)(int ctx0,
323 const struct VP8Residual* const res);
324extern VP8GetResidualCostFunc VP8GetResidualCost;
325
326// must be called before anything using the above
327void VP8EncDspCostInit(void);
328
329//------------------------------------------------------------------------------
330// SSIM / PSNR utils
331
332// struct for accumulating statistical moments
333typedef 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.
341double VP8SSIMFromStats(const VP8DistoStats* const stats);
342double VP8SSIMFromStatsClipped(const VP8DistoStats* const stats);
343
344#define VP8_SSIM_KERNEL 3 // total size of the kernel: 2 * VP8_SSIM_KERNEL + 1
345typedef 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
353typedef double (*VP8SSIMGetFunc)(const uint8_t* src1, int stride1,
354 const uint8_t* src2, int stride2);
355
356extern VP8SSIMGetFunc VP8SSIMGet; // unclipped / unchecked
357extern VP8SSIMGetClippedFunc VP8SSIMGetClipped; // with clipping
358#endif
359
360#if !defined(WEBP_DISABLE_STATS)
361typedef uint32_t (*VP8AccumulateSSEFunc)(const uint8_t* src1,
362 const uint8_t* src2, int len);
363extern VP8AccumulateSSEFunc VP8AccumulateSSE;
364#endif
365
366// must be called before using any of the above directly
367void VP8SSIMDspInit(void);
368
369//------------------------------------------------------------------------------
370// Decoding
371
372typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
373// when doing two transforms, coeffs is actually int16_t[2][16].
374typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
375extern VP8DecIdct2 VP8Transform;
376extern VP8DecIdct VP8TransformAC3;
377extern VP8DecIdct VP8TransformUV;
378extern VP8DecIdct VP8TransformDC;
379extern VP8DecIdct VP8TransformDCUV;
380extern VP8WHT VP8TransformWHT;
381
382// *dst is the destination block, with stride BPS. Boundary samples are
383// assumed accessible when needed.
384typedef void (*VP8PredFunc)(uint8_t* dst);
385extern VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
386extern VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
387extern VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
388
389// clipping tables (for filtering)
390extern const int8_t* const VP8ksclip1; // clips [-1020, 1020] to [-128, 127]
391extern const int8_t* const VP8ksclip2; // clips [-112, 112] to [-16, 15]
392extern const uint8_t* const VP8kclip1; // clips [-255,511] to [0,255]
393extern const uint8_t* const VP8kabs0; // abs(x) for x in [-255,255]
394// must be called first
395void VP8InitClipTables(void);
396
397// simple filter (only for luma)
398typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
399extern VP8SimpleFilterFunc VP8SimpleVFilter16;
400extern VP8SimpleFilterFunc VP8SimpleHFilter16;
401extern VP8SimpleFilterFunc VP8SimpleVFilter16i; // filter 3 inner edges
402extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
403
404// regular filter (on both macroblock edges and inner edges)
405typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
406 int thresh, int ithresh, int hev_t);
407typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
408 int thresh, int ithresh, int hev_t);
409// on outer edge
410extern VP8LumaFilterFunc VP8VFilter16;
411extern VP8LumaFilterFunc VP8HFilter16;
412extern VP8ChromaFilterFunc VP8VFilter8;
413extern VP8ChromaFilterFunc VP8HFilter8;
414
415// on inner edge
416extern VP8LumaFilterFunc VP8VFilter16i; // filtering 3 inner edges altogether
417extern VP8LumaFilterFunc VP8HFilter16i;
418extern VP8ChromaFilterFunc VP8VFilter8i; // filtering u and v altogether
419extern 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)
427extern void (*VP8DitherCombine8x8)(const uint8_t* dither, uint8_t* dst,
428 int dst_stride);
429
430// must be called before anything using the above
431void 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).
440typedef 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
449extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
450
451#endif // FANCY_UPSAMPLING
452
453// Per-row point-sampling methods.
454typedef 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:
458void 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)
464extern 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).
469WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
470
471// YUV444->RGB converters
472typedef void (*WebPYUV444Converter)(const uint8_t* y,
473 const uint8_t* u, const uint8_t* v,
474 uint8_t* dst, int len);
475
476extern WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
477
478// Must be called before using the WebPUpsamplers[] (and for premultiplied
479// colorspaces like rgbA, rgbA4444, etc)
480void WebPInitUpsamplers(void);
481// Must be called before using WebPSamplers[]
482void WebPInitSamplers(void);
483// Must be called before using WebPYUV444Converters[]
484void WebPInitYUV444Converters(void);
485
486//------------------------------------------------------------------------------
487// ARGB -> YUV converters
488
489// Convert ARGB samples to luma Y.
490extern 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.
494extern 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
498extern void (*WebPConvertRGBA32ToUV)(const uint16_t* rgb,
499 uint8_t* u, uint8_t* v, int width);
500
501// Convert RGB or BGR to Y
502extern void (*WebPConvertRGB24ToY)(const uint8_t* rgb, uint8_t* y, int width);
503extern void (*WebPConvertBGR24ToY)(const uint8_t* bgr, uint8_t* y, int width);
504
505// used for plain-C fallback.
506extern void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
507 int src_width, int do_store);
508extern void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
509 uint8_t* u, uint8_t* v, int width);
510
511// utilities for accurate RGB->YUV conversion
512extern uint64_t (*WebPSharpYUVUpdateY)(const uint16_t* src, const uint16_t* ref,
513 uint16_t* dst, int len);
514extern void (*WebPSharpYUVUpdateRGB)(const int16_t* src, const int16_t* ref,
515 int16_t* dst, int len);
516extern 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.
521void WebPInitConvertARGBToYUV(void);
522
523//------------------------------------------------------------------------------
524// Rescaler
525
526struct 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.
531typedef void (*WebPRescalerImportRowFunc)(struct WebPRescaler* const wrk,
532 const uint8_t* src);
533
534extern WebPRescalerImportRowFunc WebPRescalerImportRowExpand;
535extern 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
540typedef void (*WebPRescalerExportRowFunc)(struct WebPRescaler* const wrk);
541extern WebPRescalerExportRowFunc WebPRescalerExportRowExpand;
542extern WebPRescalerExportRowFunc WebPRescalerExportRowShrink;
543
544// Plain-C implementation, as fall-back.
545extern void WebPRescalerImportRowExpand_C(struct WebPRescaler* const wrk,
546 const uint8_t* src);
547extern void WebPRescalerImportRowShrink_C(struct WebPRescaler* const wrk,
548 const uint8_t* src);
549extern void WebPRescalerExportRowExpand_C(struct WebPRescaler* const wrk);
550extern void WebPRescalerExportRowShrink_C(struct WebPRescaler* const wrk);
551
552// Main entry calls:
553extern void WebPRescalerImportRow(struct WebPRescaler* const wrk,
554 const uint8_t* src);
555// Export one row (starting at x_out position) from rescaler.
556extern void WebPRescalerExportRow(struct WebPRescaler* const wrk);
557
558// Must be called first before using the above.
559void 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).
566extern void (*WebPApplyAlphaMultiply)(
567 uint8_t* rgba, int alpha_first, int w, int h, int stride);
568
569// Same, buf specifically for RGBA4444 format
570extern 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.
575extern 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.
581extern 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.
588extern 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).
594extern 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.
600extern void (*WebPMultARGBRow)(uint32_t* const ptr, int width, int inverse);
601
602// Same a WebPMultARGBRow(), but for several rows.
603void 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.
607extern 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.
611void 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.
616void WebPMultRow_C(uint8_t* const ptr, const uint8_t* const alpha,
617 int width, int inverse);
618void 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.
622extern 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.
628extern 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.
632extern int (*WebPHasAlpha8b)(const uint8_t* src, int length);
633// This function returns true if src[4*i] contains a value different from 0xff.
634extern int (*WebPHasAlpha32b)(const uint8_t* src, int length);
635
636// To be called first before using the above.
637void WebPInitAlphaProcessing(void);
638
639//------------------------------------------------------------------------------
640// Filter functions
641
642typedef 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
652typedef 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'.
656typedef 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.
664extern 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).
669extern WebPUnfilterFunc WebPUnfilters[WEBP_FILTER_LAST];
670
671// To be called first before using the above.
672void VP8FiltersInit(void);
673
674#ifdef __cplusplus
675} // extern "C"
676#endif
677
678#endif // WEBP_DSP_DSP_H_
679