dsp.h revision af51b94a435132e9014c324e25fb686b3d07a8c8
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 "config.h"
19#endif
20
21#include "webp/types.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27//------------------------------------------------------------------------------
28// CPU detection
29
30#if defined(__GNUC__)
31# define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
32# define LOCAL_GCC_PREREQ(maj, min) \
33    (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
34#else
35# define LOCAL_GCC_PREREQ(maj, min) 0
36#endif
37
38#if defined(_MSC_VER) && _MSC_VER > 1310 && \
39    (defined(_M_X64) || defined(_M_IX86))
40#define WEBP_MSC_SSE2  // Visual C++ SSE2 targets
41#endif
42
43// WEBP_HAVE_* are used to indicate the presence of the instruction set in dsp
44// files without intrinsics, allowing the corresponding Init() to be called.
45// Files containing intrinsics will need to be built targeting the instruction
46// set so should succeed on one of the earlier tests.
47#if defined(__SSE2__) || defined(WEBP_MSC_SSE2) || defined(WEBP_HAVE_SSE2)
48#define WEBP_USE_SSE2
49#endif
50
51#if defined(__AVX2__) || defined(WEBP_HAVE_AVX2)
52#define WEBP_USE_AVX2
53#endif
54
55#if defined(__ANDROID__) && defined(__ARM_ARCH_7A__)
56#define WEBP_ANDROID_NEON  // Android targets that might support NEON
57#endif
58
59#if defined(__ARM_NEON__) || defined(__aarch64__)
60#define WEBP_USE_NEON
61#endif
62
63#if defined(__mips__) && !defined(__mips64)
64#define WEBP_USE_MIPS32
65#endif
66
67typedef enum {
68  kSSE2,
69  kSSE3,
70  kAVX,
71  kAVX2,
72  kNEON,
73  kMIPS32
74} CPUFeature;
75// returns true if the CPU supports the feature.
76typedef int (*VP8CPUInfo)(CPUFeature feature);
77extern VP8CPUInfo VP8GetCPUInfo;
78
79//------------------------------------------------------------------------------
80// Encoding
81
82// Transforms
83// VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
84//          will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
85typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
86                        int do_two);
87typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
88typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
89extern VP8Idct VP8ITransform;
90extern VP8Fdct VP8FTransform;
91extern VP8WHT VP8FTransformWHT;
92// Predictions
93// *dst is the destination block. *top and *left can be NULL.
94typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
95                              const uint8_t* top);
96typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
97extern VP8Intra4Preds VP8EncPredLuma4;
98extern VP8IntraPreds VP8EncPredLuma16;
99extern VP8IntraPreds VP8EncPredChroma8;
100
101typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
102extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
103typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
104                          const uint16_t* const weights);
105extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
106
107typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
108extern VP8BlockCopy VP8Copy4x4;
109// Quantization
110struct VP8Matrix;   // forward declaration
111typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
112                                const struct VP8Matrix* const mtx);
113extern VP8QuantizeBlock VP8EncQuantizeBlock;
114
115// specific to 2nd transform:
116typedef int (*VP8QuantizeBlockWHT)(int16_t in[16], int16_t out[16],
117                                   const struct VP8Matrix* const mtx);
118extern VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT;
119
120// Collect histogram for susceptibility calculation and accumulate in histo[].
121struct VP8Histogram;
122typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
123                          int start_block, int end_block,
124                          struct VP8Histogram* const histo);
125extern const int VP8DspScan[16 + 4 + 4];
126extern VP8CHisto VP8CollectHistogram;
127
128void VP8EncDspInit(void);   // must be called before using any of the above
129
130//------------------------------------------------------------------------------
131// Decoding
132
133typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
134// when doing two transforms, coeffs is actually int16_t[2][16].
135typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
136extern VP8DecIdct2 VP8Transform;
137extern VP8DecIdct VP8TransformAC3;
138extern VP8DecIdct VP8TransformUV;
139extern VP8DecIdct VP8TransformDC;
140extern VP8DecIdct VP8TransformDCUV;
141extern VP8WHT VP8TransformWHT;
142
143// *dst is the destination block, with stride BPS. Boundary samples are
144// assumed accessible when needed.
145typedef void (*VP8PredFunc)(uint8_t* dst);
146extern const VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
147extern const VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
148extern const VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
149
150// clipping tables (for filtering)
151extern const int8_t* const VP8ksclip1;  // clips [-1020, 1020] to [-128, 127]
152extern const int8_t* const VP8ksclip2;  // clips [-112, 112] to [-16, 15]
153extern const uint8_t* const VP8kclip1;  // clips [-255,511] to [0,255]
154extern const uint8_t* const VP8kabs0;   // abs(x) for x in [-255,255]
155void VP8InitClipTables(void);           // must be called first
156
157// simple filter (only for luma)
158typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
159extern VP8SimpleFilterFunc VP8SimpleVFilter16;
160extern VP8SimpleFilterFunc VP8SimpleHFilter16;
161extern VP8SimpleFilterFunc VP8SimpleVFilter16i;  // filter 3 inner edges
162extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
163
164// regular filter (on both macroblock edges and inner edges)
165typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
166                                  int thresh, int ithresh, int hev_t);
167typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
168                                    int thresh, int ithresh, int hev_t);
169// on outer edge
170extern VP8LumaFilterFunc VP8VFilter16;
171extern VP8LumaFilterFunc VP8HFilter16;
172extern VP8ChromaFilterFunc VP8VFilter8;
173extern VP8ChromaFilterFunc VP8HFilter8;
174
175// on inner edge
176extern VP8LumaFilterFunc VP8VFilter16i;   // filtering 3 inner edges altogether
177extern VP8LumaFilterFunc VP8HFilter16i;
178extern VP8ChromaFilterFunc VP8VFilter8i;  // filtering u and v altogether
179extern VP8ChromaFilterFunc VP8HFilter8i;
180
181// must be called before anything using the above
182void VP8DspInit(void);
183
184//------------------------------------------------------------------------------
185// WebP I/O
186
187#define FANCY_UPSAMPLING   // undefined to remove fancy upsampling support
188
189// Convert a pair of y/u/v lines together to the output rgb/a colorspace.
190// bottom_y can be NULL if only one line of output is needed (at top/bottom).
191typedef void (*WebPUpsampleLinePairFunc)(
192    const uint8_t* top_y, const uint8_t* bottom_y,
193    const uint8_t* top_u, const uint8_t* top_v,
194    const uint8_t* cur_u, const uint8_t* cur_v,
195    uint8_t* top_dst, uint8_t* bottom_dst, int len);
196
197#ifdef FANCY_UPSAMPLING
198
199// Fancy upsampling functions to convert YUV to RGB(A) modes
200extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
201
202#endif    // FANCY_UPSAMPLING
203
204// Per-row point-sampling methods.
205typedef void (*WebPSamplerRowFunc)(const uint8_t* y,
206                                   const uint8_t* u, const uint8_t* v,
207                                   uint8_t* dst, int len);
208// Generic function to apply 'WebPSamplerRowFunc' to the whole plane:
209void WebPSamplerProcessPlane(const uint8_t* y, int y_stride,
210                             const uint8_t* u, const uint8_t* v, int uv_stride,
211                             uint8_t* dst, int dst_stride,
212                             int width, int height, WebPSamplerRowFunc func);
213
214// Sampling functions to convert rows of YUV to RGB(A)
215extern WebPSamplerRowFunc WebPSamplers[/* MODE_LAST */];
216
217// General function for converting two lines of ARGB or RGBA.
218// 'alpha_is_last' should be true if 0xff000000 is stored in memory as
219// as 0x00, 0x00, 0x00, 0xff (little endian).
220WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
221
222// YUV444->RGB converters
223typedef void (*WebPYUV444Converter)(const uint8_t* y,
224                                    const uint8_t* u, const uint8_t* v,
225                                    uint8_t* dst, int len);
226
227extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
228
229// Must be called before using the WebPUpsamplers[] (and for premultiplied
230// colorspaces like rgbA, rgbA4444, etc)
231void WebPInitUpsamplers(void);
232// Must be called before using WebPSamplers[]
233void WebPInitSamplers(void);
234
235//------------------------------------------------------------------------------
236// Utilities for processing transparent channel.
237
238// Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
239// alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
240extern void (*WebPApplyAlphaMultiply)(
241    uint8_t* rgba, int alpha_first, int w, int h, int stride);
242
243// Same, buf specifically for RGBA4444 format
244extern void (*WebPApplyAlphaMultiply4444)(
245    uint8_t* rgba4444, int w, int h, int stride);
246
247// Pre-Multiply operation transforms x into x * A / 255  (where x=Y,R,G or B).
248// Un-Multiply operation transforms x into x * 255 / A.
249
250// Pre-Multiply or Un-Multiply (if 'inverse' is true) argb values in a row.
251extern void (*WebPMultARGBRow)(uint32_t* const ptr, int width, int inverse);
252
253// Same a WebPMultARGBRow(), but for several rows.
254void WebPMultARGBRows(uint8_t* ptr, int stride, int width, int num_rows,
255                      int inverse);
256
257// Same for a row of single values, with side alpha values.
258extern void (*WebPMultRow)(uint8_t* const ptr, const uint8_t* const alpha,
259                           int width, int inverse);
260
261// Same a WebPMultRow(), but for several 'num_rows' rows.
262void WebPMultRows(uint8_t* ptr, int stride,
263                  const uint8_t* alpha, int alpha_stride,
264                  int width, int num_rows, int inverse);
265
266// To be called first before using the above.
267void WebPInitAlphaProcessing(void);
268
269#ifdef __cplusplus
270}    // extern "C"
271#endif
272
273#endif  /* WEBP_DSP_DSP_H_ */
274