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