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/dsp/cpu.h"
22#include "src/webp/types.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#define BPS 32 // this is the common stride for enc/dec
29
30//------------------------------------------------------------------------------
31// WEBP_RESTRICT
32
33// Declares a pointer with the restrict type qualifier if available.
34// This allows code to hint to the compiler that only this pointer references a
35// particular object or memory region within the scope of the block in which it
36// is declared. This may allow for improved optimizations due to the lack of
37// pointer aliasing. See also:
38// https://en.cppreference.com/w/c/language/restrict
39#if defined(__GNUC__)
40#define WEBP_RESTRICT __restrict__
41#elif defined(_MSC_VER)
42#define WEBP_RESTRICT __restrict
43#else
44#define WEBP_RESTRICT
45#endif
46
47
48//------------------------------------------------------------------------------
49// Init stub generator
50
51// Defines an init function stub to ensure each module exposes a symbol,
52// avoiding a compiler warning.
53#define WEBP_DSP_INIT_STUB(func) \
54 extern void func(void); \
55 void func(void) {}
56
57//------------------------------------------------------------------------------
58// Encoding
59
60// Transforms
61// VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
62// will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
63typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
64 int do_two);
65typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
66typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
67extern VP8Idct VP8ITransform;
68extern VP8Fdct VP8FTransform;
69extern VP8Fdct VP8FTransform2; // performs two transforms at a time
70extern VP8WHT VP8FTransformWHT;
71// Predictions
72// *dst is the destination block. *top and *left can be NULL.
73typedef void (*VP8IntraPreds)(uint8_t* dst, const uint8_t* left,
74 const uint8_t* top);
75typedef void (*VP8Intra4Preds)(uint8_t* dst, const uint8_t* top);
76extern VP8Intra4Preds VP8EncPredLuma4;
77extern VP8IntraPreds VP8EncPredLuma16;
78extern VP8IntraPreds VP8EncPredChroma8;
79
80typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
81extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
82typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
83 const uint16_t* const weights);
84// The weights for VP8TDisto4x4 and VP8TDisto16x16 contain a row-major
85// 4 by 4 symmetric matrix.
86extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
87
88// Compute the average (DC) of four 4x4 blocks.
89// Each sub-4x4 block #i sum is stored in dc[i].
90typedef void (*VP8MeanMetric)(const uint8_t* ref, uint32_t dc[4]);
91extern VP8MeanMetric VP8Mean16x4;
92
93typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
94extern VP8BlockCopy VP8Copy4x4;
95extern VP8BlockCopy VP8Copy16x8;
96// Quantization
97struct VP8Matrix; // forward declaration
98typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
99 const struct VP8Matrix* const mtx);
100// Same as VP8QuantizeBlock, but quantizes two consecutive blocks.
101typedef int (*VP8Quantize2Blocks)(int16_t in[32], int16_t out[32],
102 const struct VP8Matrix* const mtx);
103
104extern VP8QuantizeBlock VP8EncQuantizeBlock;
105extern VP8Quantize2Blocks VP8EncQuantize2Blocks;
106
107// specific to 2nd transform:
108typedef int (*VP8QuantizeBlockWHT)(int16_t in[16], int16_t out[16],
109 const struct VP8Matrix* const mtx);
110extern VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT;
111
112extern const int VP8DspScan[16 + 4 + 4];
113
114// Collect histogram for susceptibility calculation.
115#define MAX_COEFF_THRESH 31 // size of histogram used by CollectHistogram.
116typedef struct {
117 // We only need to store max_value and last_non_zero, not the distribution.
118 int max_value;
119 int last_non_zero;
120} VP8Histogram;
121typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
122 int start_block, int end_block,
123 VP8Histogram* const histo);
124extern VP8CHisto VP8CollectHistogram;
125// General-purpose util function to help VP8CollectHistogram().
126void VP8SetHistogramData(const int distribution[MAX_COEFF_THRESH + 1],
127 VP8Histogram* const histo);
128
129// must be called before using any of the above
130void VP8EncDspInit(void);
131
132//------------------------------------------------------------------------------
133// cost functions (encoding)
134
135extern const uint16_t VP8EntropyCost[256]; // 8bit fixed-point log(p)
136// approximate cost per level:
137extern const uint16_t VP8LevelFixedCosts[2047 /*MAX_LEVEL*/ + 1];
138extern const uint8_t VP8EncBands[16 + 1];
139
140struct VP8Residual;
141typedef void (*VP8SetResidualCoeffsFunc)(const int16_t* const coeffs,
142 struct VP8Residual* const res);
143extern VP8SetResidualCoeffsFunc VP8SetResidualCoeffs;
144
145// Cost calculation function.
146typedef int (*VP8GetResidualCostFunc)(int ctx0,
147 const struct VP8Residual* const res);
148extern VP8GetResidualCostFunc VP8GetResidualCost;
149
150// must be called before anything using the above
151void VP8EncDspCostInit(void);
152
153//------------------------------------------------------------------------------
154// SSIM / PSNR utils
155
156// struct for accumulating statistical moments
157typedef struct {
158 uint32_t w; // sum(w_i) : sum of weights
159 uint32_t xm, ym; // sum(w_i * x_i), sum(w_i * y_i)
160 uint32_t xxm, xym, yym; // sum(w_i * x_i * x_i), etc.
161} VP8DistoStats;
162
163// Compute the final SSIM value
164// The non-clipped version assumes stats->w = (2 * VP8_SSIM_KERNEL + 1)^2.
165double VP8SSIMFromStats(const VP8DistoStats* const stats);
166double VP8SSIMFromStatsClipped(const VP8DistoStats* const stats);
167
168#define VP8_SSIM_KERNEL 3 // total size of the kernel: 2 * VP8_SSIM_KERNEL + 1
169typedef double (*VP8SSIMGetClippedFunc)(const uint8_t* src1, int stride1,
170 const uint8_t* src2, int stride2,
171 int xo, int yo, // center position
172 int W, int H); // plane dimension
173
174#if !defined(WEBP_REDUCE_SIZE)
175// This version is called with the guarantee that you can load 8 bytes and
176// 8 rows at offset src1 and src2
177typedef double (*VP8SSIMGetFunc)(const uint8_t* src1, int stride1,
178 const uint8_t* src2, int stride2);
179
180extern VP8SSIMGetFunc VP8SSIMGet; // unclipped / unchecked
181extern VP8SSIMGetClippedFunc VP8SSIMGetClipped; // with clipping
182#endif
183
184#if !defined(WEBP_DISABLE_STATS)
185typedef uint32_t (*VP8AccumulateSSEFunc)(const uint8_t* src1,
186 const uint8_t* src2, int len);
187extern VP8AccumulateSSEFunc VP8AccumulateSSE;
188#endif
189
190// must be called before using any of the above directly
191void VP8SSIMDspInit(void);
192
193//------------------------------------------------------------------------------
194// Decoding
195
196typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
197// when doing two transforms, coeffs is actually int16_t[2][16].
198typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
199extern VP8DecIdct2 VP8Transform;
200extern VP8DecIdct VP8TransformAC3;
201extern VP8DecIdct VP8TransformUV;
202extern VP8DecIdct VP8TransformDC;
203extern VP8DecIdct VP8TransformDCUV;
204extern VP8WHT VP8TransformWHT;
205
206// *dst is the destination block, with stride BPS. Boundary samples are
207// assumed accessible when needed.
208typedef void (*VP8PredFunc)(uint8_t* dst);
209extern VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
210extern VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
211extern VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
212
213// clipping tables (for filtering)
214extern const int8_t* const VP8ksclip1; // clips [-1020, 1020] to [-128, 127]
215extern const int8_t* const VP8ksclip2; // clips [-112, 112] to [-16, 15]
216extern const uint8_t* const VP8kclip1; // clips [-255,511] to [0,255]
217extern const uint8_t* const VP8kabs0; // abs(x) for x in [-255,255]
218// must be called first
219void VP8InitClipTables(void);
220
221// simple filter (only for luma)
222typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
223extern VP8SimpleFilterFunc VP8SimpleVFilter16;
224extern VP8SimpleFilterFunc VP8SimpleHFilter16;
225extern VP8SimpleFilterFunc VP8SimpleVFilter16i; // filter 3 inner edges
226extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
227
228// regular filter (on both macroblock edges and inner edges)
229typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
230 int thresh, int ithresh, int hev_t);
231typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
232 int thresh, int ithresh, int hev_t);
233// on outer edge
234extern VP8LumaFilterFunc VP8VFilter16;
235extern VP8LumaFilterFunc VP8HFilter16;
236extern VP8ChromaFilterFunc VP8VFilter8;
237extern VP8ChromaFilterFunc VP8HFilter8;
238
239// on inner edge
240extern VP8LumaFilterFunc VP8VFilter16i; // filtering 3 inner edges altogether
241extern VP8LumaFilterFunc VP8HFilter16i;
242extern VP8ChromaFilterFunc VP8VFilter8i; // filtering u and v altogether
243extern VP8ChromaFilterFunc VP8HFilter8i;
244
245// Dithering. Combines dithering values (centered around 128) with dst[],
246// according to: dst[] = clip(dst[] + (((dither[]-128) + 8) >> 4)
247#define VP8_DITHER_DESCALE 4
248#define VP8_DITHER_DESCALE_ROUNDER (1 << (VP8_DITHER_DESCALE - 1))
249#define VP8_DITHER_AMP_BITS 7
250#define VP8_DITHER_AMP_CENTER (1 << VP8_DITHER_AMP_BITS)
251extern void (*VP8DitherCombine8x8)(const uint8_t* dither, uint8_t* dst,
252 int dst_stride);
253
254// must be called before anything using the above
255void VP8DspInit(void);
256
257//------------------------------------------------------------------------------
258// WebP I/O
259
260#define FANCY_UPSAMPLING // undefined to remove fancy upsampling support
261
262// Convert a pair of y/u/v lines together to the output rgb/a colorspace.
263// bottom_y can be NULL if only one line of output is needed (at top/bottom).
264typedef void (*WebPUpsampleLinePairFunc)(
265 const uint8_t* top_y, const uint8_t* bottom_y,
266 const uint8_t* top_u, const uint8_t* top_v,
267 const uint8_t* cur_u, const uint8_t* cur_v,
268 uint8_t* top_dst, uint8_t* bottom_dst, int len);
269
270#ifdef FANCY_UPSAMPLING
271
272// Fancy upsampling functions to convert YUV to RGB(A) modes
273extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
274
275#endif // FANCY_UPSAMPLING
276
277// Per-row point-sampling methods.
278typedef void (*WebPSamplerRowFunc)(const uint8_t* y,
279 const uint8_t* u, const uint8_t* v,
280 uint8_t* dst, int len);
281// Generic function to apply 'WebPSamplerRowFunc' to the whole plane:
282void WebPSamplerProcessPlane(const uint8_t* y, int y_stride,
283 const uint8_t* u, const uint8_t* v, int uv_stride,
284 uint8_t* dst, int dst_stride,
285 int width, int height, WebPSamplerRowFunc func);
286
287// Sampling functions to convert rows of YUV to RGB(A)
288extern WebPSamplerRowFunc WebPSamplers[/* MODE_LAST */];
289
290// General function for converting two lines of ARGB or RGBA.
291// 'alpha_is_last' should be true if 0xff000000 is stored in memory as
292// as 0x00, 0x00, 0x00, 0xff (little endian).
293WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
294
295// YUV444->RGB converters
296typedef void (*WebPYUV444Converter)(const uint8_t* y,
297 const uint8_t* u, const uint8_t* v,
298 uint8_t* dst, int len);
299
300extern WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
301
302// Must be called before using the WebPUpsamplers[] (and for premultiplied
303// colorspaces like rgbA, rgbA4444, etc)
304void WebPInitUpsamplers(void);
305// Must be called before using WebPSamplers[]
306void WebPInitSamplers(void);
307// Must be called before using WebPYUV444Converters[]
308void WebPInitYUV444Converters(void);
309
310//------------------------------------------------------------------------------
311// ARGB -> YUV converters
312
313// Convert ARGB samples to luma Y.
314extern void (*WebPConvertARGBToY)(const uint32_t* argb, uint8_t* y, int width);
315// Convert ARGB samples to U/V with downsampling. do_store should be '1' for
316// even lines and '0' for odd ones. 'src_width' is the original width, not
317// the U/V one.
318extern void (*WebPConvertARGBToUV)(const uint32_t* argb, uint8_t* u, uint8_t* v,
319 int src_width, int do_store);
320
321// Convert a row of accumulated (four-values) of rgba32 toward U/V
322extern void (*WebPConvertRGBA32ToUV)(const uint16_t* rgb,
323 uint8_t* u, uint8_t* v, int width);
324
325// Convert RGB or BGR to Y
326extern void (*WebPConvertRGB24ToY)(const uint8_t* rgb, uint8_t* y, int width);
327extern void (*WebPConvertBGR24ToY)(const uint8_t* bgr, uint8_t* y, int width);
328
329// used for plain-C fallback.
330extern void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
331 int src_width, int do_store);
332extern void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
333 uint8_t* u, uint8_t* v, int width);
334
335// Must be called before using the above.
336void WebPInitConvertARGBToYUV(void);
337
338//------------------------------------------------------------------------------
339// Rescaler
340
341struct WebPRescaler;
342
343// Import a row of data and save its contribution in the rescaler.
344// 'channel' denotes the channel number to be imported. 'Expand' corresponds to
345// the wrk->x_expand case. Otherwise, 'Shrink' is to be used.
346typedef void (*WebPRescalerImportRowFunc)(struct WebPRescaler* const wrk,
347 const uint8_t* src);
348
349extern WebPRescalerImportRowFunc WebPRescalerImportRowExpand;
350extern WebPRescalerImportRowFunc WebPRescalerImportRowShrink;
351
352// Export one row (starting at x_out position) from rescaler.
353// 'Expand' corresponds to the wrk->y_expand case.
354// Otherwise 'Shrink' is to be used
355typedef void (*WebPRescalerExportRowFunc)(struct WebPRescaler* const wrk);
356extern WebPRescalerExportRowFunc WebPRescalerExportRowExpand;
357extern WebPRescalerExportRowFunc WebPRescalerExportRowShrink;
358
359// Plain-C implementation, as fall-back.
360extern void WebPRescalerImportRowExpand_C(struct WebPRescaler* const wrk,
361 const uint8_t* src);
362extern void WebPRescalerImportRowShrink_C(struct WebPRescaler* const wrk,
363 const uint8_t* src);
364extern void WebPRescalerExportRowExpand_C(struct WebPRescaler* const wrk);
365extern void WebPRescalerExportRowShrink_C(struct WebPRescaler* const wrk);
366
367// Main entry calls:
368extern void WebPRescalerImportRow(struct WebPRescaler* const wrk,
369 const uint8_t* src);
370// Export one row (starting at x_out position) from rescaler.
371extern void WebPRescalerExportRow(struct WebPRescaler* const wrk);
372
373// Must be called first before using the above.
374void WebPRescalerDspInit(void);
375
376//------------------------------------------------------------------------------
377// Utilities for processing transparent channel.
378
379// Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
380// alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
381extern void (*WebPApplyAlphaMultiply)(
382 uint8_t* rgba, int alpha_first, int w, int h, int stride);
383
384// Same, buf specifically for RGBA4444 format
385extern void (*WebPApplyAlphaMultiply4444)(
386 uint8_t* rgba4444, int w, int h, int stride);
387
388// Dispatch the values from alpha[] plane to the ARGB destination 'dst'.
389// Returns true if alpha[] plane has non-trivial values different from 0xff.
390extern int (*WebPDispatchAlpha)(const uint8_t* WEBP_RESTRICT alpha,
391 int alpha_stride, int width, int height,
392 uint8_t* WEBP_RESTRICT dst, int dst_stride);
393
394// Transfer packed 8b alpha[] values to green channel in dst[], zero'ing the
395// A/R/B values. 'dst_stride' is the stride for dst[] in uint32_t units.
396extern void (*WebPDispatchAlphaToGreen)(const uint8_t* WEBP_RESTRICT alpha,
397 int alpha_stride, int width, int height,
398 uint32_t* WEBP_RESTRICT dst,
399 int dst_stride);
400
401// Extract the alpha values from 32b values in argb[] and pack them into alpha[]
402// (this is the opposite of WebPDispatchAlpha).
403// Returns true if there's only trivial 0xff alpha values.
404extern int (*WebPExtractAlpha)(const uint8_t* WEBP_RESTRICT argb,
405 int argb_stride, int width, int height,
406 uint8_t* WEBP_RESTRICT alpha,
407 int alpha_stride);
408
409// Extract the green values from 32b values in argb[] and pack them into alpha[]
410// (this is the opposite of WebPDispatchAlphaToGreen).
411extern void (*WebPExtractGreen)(const uint32_t* WEBP_RESTRICT argb,
412 uint8_t* WEBP_RESTRICT alpha, int size);
413
414// Pre-Multiply operation transforms x into x * A / 255 (where x=Y,R,G or B).
415// Un-Multiply operation transforms x into x * 255 / A.
416
417// Pre-Multiply or Un-Multiply (if 'inverse' is true) argb values in a row.
418extern void (*WebPMultARGBRow)(uint32_t* const ptr, int width, int inverse);
419
420// Same a WebPMultARGBRow(), but for several rows.
421void WebPMultARGBRows(uint8_t* ptr, int stride, int width, int num_rows,
422 int inverse);
423
424// Same for a row of single values, with side alpha values.
425extern void (*WebPMultRow)(uint8_t* WEBP_RESTRICT const ptr,
426 const uint8_t* WEBP_RESTRICT const alpha,
427 int width, int inverse);
428
429// Same a WebPMultRow(), but for several 'num_rows' rows.
430void WebPMultRows(uint8_t* WEBP_RESTRICT ptr, int stride,
431 const uint8_t* WEBP_RESTRICT alpha, int alpha_stride,
432 int width, int num_rows, int inverse);
433
434// Plain-C versions, used as fallback by some implementations.
435void WebPMultRow_C(uint8_t* WEBP_RESTRICT const ptr,
436 const uint8_t* WEBP_RESTRICT const alpha,
437 int width, int inverse);
438void WebPMultARGBRow_C(uint32_t* const ptr, int width, int inverse);
439
440#ifdef WORDS_BIGENDIAN
441// ARGB packing function: a/r/g/b input is rgba or bgra order.
442extern void (*WebPPackARGB)(const uint8_t* WEBP_RESTRICT a,
443 const uint8_t* WEBP_RESTRICT r,
444 const uint8_t* WEBP_RESTRICT g,
445 const uint8_t* WEBP_RESTRICT b,
446 int len, uint32_t* WEBP_RESTRICT out);
447#endif
448
449// RGB packing function. 'step' can be 3 or 4. r/g/b input is rgb or bgr order.
450extern void (*WebPPackRGB)(const uint8_t* WEBP_RESTRICT r,
451 const uint8_t* WEBP_RESTRICT g,
452 const uint8_t* WEBP_RESTRICT b,
453 int len, int step, uint32_t* WEBP_RESTRICT out);
454
455// This function returns true if src[i] contains a value different from 0xff.
456extern int (*WebPHasAlpha8b)(const uint8_t* src, int length);
457// This function returns true if src[4*i] contains a value different from 0xff.
458extern int (*WebPHasAlpha32b)(const uint8_t* src, int length);
459// replaces transparent values in src[] by 'color'.
460extern void (*WebPAlphaReplace)(uint32_t* src, int length, uint32_t color);
461
462// To be called first before using the above.
463void WebPInitAlphaProcessing(void);
464
465//------------------------------------------------------------------------------
466// Filter functions
467
468typedef enum { // Filter types.
469 WEBP_FILTER_NONE = 0,
470 WEBP_FILTER_HORIZONTAL,
471 WEBP_FILTER_VERTICAL,
472 WEBP_FILTER_GRADIENT,
473 WEBP_FILTER_LAST = WEBP_FILTER_GRADIENT + 1, // end marker
474 WEBP_FILTER_BEST, // meta-types
475 WEBP_FILTER_FAST
476} WEBP_FILTER_TYPE;
477
478typedef void (*WebPFilterFunc)(const uint8_t* in, int width, int height,
479 int stride, uint8_t* out);
480// In-place un-filtering.
481// Warning! 'prev_line' pointer can be equal to 'cur_line' or 'preds'.
482typedef void (*WebPUnfilterFunc)(const uint8_t* prev_line, const uint8_t* preds,
483 uint8_t* cur_line, int width);
484
485// Filter the given data using the given predictor.
486// 'in' corresponds to a 2-dimensional pixel array of size (stride * height)
487// in raster order.
488// 'stride' is number of bytes per scan line (with possible padding).
489// 'out' should be pre-allocated.
490extern WebPFilterFunc WebPFilters[WEBP_FILTER_LAST];
491
492// In-place reconstruct the original data from the given filtered data.
493// The reconstruction will be done for 'num_rows' rows starting from 'row'
494// (assuming rows upto 'row - 1' are already reconstructed).
495extern WebPUnfilterFunc WebPUnfilters[WEBP_FILTER_LAST];
496
497// To be called first before using the above.
498void VP8FiltersInit(void);
499
500#ifdef __cplusplus
501} // extern "C"
502#endif
503
504#endif // WEBP_DSP_DSP_H_
505