dsp.h revision 1e7bf8805bd030c19924a5306837ecd72c295751
1// Copyright 2011 Google Inc. All Rights Reserved.
2//
3// This code is licensed under the same terms as WebM:
4//  Software License Agreement:  http://www.webmproject.org/license/software/
5//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8//   Speed-critical functions.
9//
10// Author: Skal (pascal.massimino@gmail.com)
11
12#ifndef WEBP_DSP_DSP_H_
13#define WEBP_DSP_DSP_H_
14
15#include "webp/types.h"
16
17#if defined(__cplusplus) || defined(c_plusplus)
18extern "C" {
19#endif
20
21//------------------------------------------------------------------------------
22// CPU detection
23
24#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
25#define WEBP_MSC_SSE2  // Visual C++ SSE2 targets
26#endif
27
28#if defined(__SSE2__) || defined(WEBP_MSC_SSE2)
29#define WEBP_USE_SSE2
30#endif
31
32#if defined(__ANDROID__) && defined(__ARM_ARCH_7A__)
33#define WEBP_ANDROID_NEON  // Android targets that might support NEON
34#endif
35
36#if defined(__ARM_NEON__)
37#define WEBP_USE_NEON
38#endif
39
40typedef enum {
41  kSSE2,
42  kSSE3,
43  kNEON
44} CPUFeature;
45// returns true if the CPU supports the feature.
46typedef int (*VP8CPUInfo)(CPUFeature feature);
47extern VP8CPUInfo VP8GetCPUInfo;
48
49//------------------------------------------------------------------------------
50// Encoding
51
52// Transforms
53// VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
54//          will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
55typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
56                        int do_two);
57typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
58typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
59extern VP8Idct VP8ITransform;
60extern VP8Fdct VP8FTransform;
61extern VP8WHT VP8ITransformWHT;
62extern VP8WHT VP8FTransformWHT;
63// Predictions
64// *dst is the destination block. *top and *left can be NULL.
65typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
66                              const uint8_t* top);
67typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
68extern VP8Intra4Preds VP8EncPredLuma4;
69extern VP8IntraPreds VP8EncPredLuma16;
70extern VP8IntraPreds VP8EncPredChroma8;
71
72typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
73extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
74typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
75                          const uint16_t* const weights);
76extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
77
78typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
79extern VP8BlockCopy VP8Copy4x4;
80// Quantization
81struct VP8Matrix;   // forward declaration
82typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
83                                int n, const struct VP8Matrix* const mtx);
84extern VP8QuantizeBlock VP8EncQuantizeBlock;
85
86// Collect histogram for susceptibility calculation and accumulate in histo[].
87struct VP8Histogram;
88typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
89                          int start_block, int end_block,
90                          struct VP8Histogram* const histo);
91extern const int VP8DspScan[16 + 4 + 4];
92extern VP8CHisto VP8CollectHistogram;
93
94void VP8EncDspInit(void);   // must be called before using any of the above
95
96//------------------------------------------------------------------------------
97// Decoding
98
99typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
100// when doing two transforms, coeffs is actually int16_t[2][16].
101typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
102extern VP8DecIdct2 VP8Transform;
103extern VP8DecIdct VP8TransformUV;
104extern VP8DecIdct VP8TransformDC;
105extern VP8DecIdct VP8TransformDCUV;
106extern VP8WHT VP8TransformWHT;
107
108// *dst is the destination block, with stride BPS. Boundary samples are
109// assumed accessible when needed.
110typedef void (*VP8PredFunc)(uint8_t* dst);
111extern const VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
112extern const VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
113extern const VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
114
115// simple filter (only for luma)
116typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
117extern VP8SimpleFilterFunc VP8SimpleVFilter16;
118extern VP8SimpleFilterFunc VP8SimpleHFilter16;
119extern VP8SimpleFilterFunc VP8SimpleVFilter16i;  // filter 3 inner edges
120extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
121
122// regular filter (on both macroblock edges and inner edges)
123typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
124                                  int thresh, int ithresh, int hev_t);
125typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
126                                    int thresh, int ithresh, int hev_t);
127// on outer edge
128extern VP8LumaFilterFunc VP8VFilter16;
129extern VP8LumaFilterFunc VP8HFilter16;
130extern VP8ChromaFilterFunc VP8VFilter8;
131extern VP8ChromaFilterFunc VP8HFilter8;
132
133// on inner edge
134extern VP8LumaFilterFunc VP8VFilter16i;   // filtering 3 inner edges altogether
135extern VP8LumaFilterFunc VP8HFilter16i;
136extern VP8ChromaFilterFunc VP8VFilter8i;  // filtering u and v altogether
137extern VP8ChromaFilterFunc VP8HFilter8i;
138
139// must be called before anything using the above
140void VP8DspInit(void);
141
142//------------------------------------------------------------------------------
143// WebP I/O
144
145#define FANCY_UPSAMPLING   // undefined to remove fancy upsampling support
146
147typedef void (*WebPUpsampleLinePairFunc)(
148    const uint8_t* top_y, const uint8_t* bottom_y,
149    const uint8_t* top_u, const uint8_t* top_v,
150    const uint8_t* cur_u, const uint8_t* cur_v,
151    uint8_t* top_dst, uint8_t* bottom_dst, int len);
152
153#ifdef FANCY_UPSAMPLING
154
155// Fancy upsampling functions to convert YUV to RGB(A) modes
156extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
157
158// Initializes SSE2 version of the fancy upsamplers.
159void WebPInitUpsamplersSSE2(void);
160
161// NEON version
162void WebPInitUpsamplersNEON(void);
163
164#endif    // FANCY_UPSAMPLING
165
166// Point-sampling methods.
167typedef void (*WebPSampleLinePairFunc)(
168    const uint8_t* top_y, const uint8_t* bottom_y,
169    const uint8_t* u, const uint8_t* v,
170    uint8_t* top_dst, uint8_t* bottom_dst, int len);
171
172extern const WebPSampleLinePairFunc WebPSamplers[/* MODE_LAST */];
173
174// General function for converting two lines of ARGB or RGBA.
175// 'alpha_is_last' should be true if 0xff000000 is stored in memory as
176// as 0x00, 0x00, 0x00, 0xff (little endian).
177WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
178
179// YUV444->RGB converters
180typedef void (*WebPYUV444Converter)(const uint8_t* y,
181                                    const uint8_t* u, const uint8_t* v,
182                                    uint8_t* dst, int len);
183
184extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
185
186// Main function to be called
187void WebPInitUpsamplers(void);
188
189//------------------------------------------------------------------------------
190// Pre-multiply planes with alpha values
191
192// Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
193// alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
194extern void (*WebPApplyAlphaMultiply)(
195    uint8_t* rgba, int alpha_first, int w, int h, int stride);
196
197// Same, buf specifically for RGBA4444 format
198extern void (*WebPApplyAlphaMultiply4444)(
199    uint8_t* rgba4444, int w, int h, int stride);
200
201// To be called first before using the above.
202void WebPInitPremultiply(void);
203
204void WebPInitPremultiplySSE2(void);   // should not be called directly.
205void WebPInitPremultiplyNEON(void);
206
207//------------------------------------------------------------------------------
208
209#if defined(__cplusplus) || defined(c_plusplus)
210}    // extern "C"
211#endif
212
213#endif  /* WEBP_DSP_DSP_H_ */
214