dsp.h revision b6dbce6bfeaabde2a7b581c4c6888d532d32f3ac
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__) || defined(WEBP_ANDROID_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
52int VP8GetAlpha(const int histo[]);
53
54// Transforms
55// VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
56//          will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
57typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
58                        int do_two);
59typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
60typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
61extern VP8Idct VP8ITransform;
62extern VP8Fdct VP8FTransform;
63extern VP8WHT VP8ITransformWHT;
64extern VP8WHT VP8FTransformWHT;
65// Predictions
66// *dst is the destination block. *top and *left can be NULL.
67typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
68                              const uint8_t* top);
69typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
70extern VP8Intra4Preds VP8EncPredLuma4;
71extern VP8IntraPreds VP8EncPredLuma16;
72extern VP8IntraPreds VP8EncPredChroma8;
73
74typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
75extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
76typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
77                          const uint16_t* const weights);
78extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
79
80typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
81extern VP8BlockCopy VP8Copy4x4;
82// Quantization
83struct VP8Matrix;   // forward declaration
84typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
85                                int n, const struct VP8Matrix* const mtx);
86extern VP8QuantizeBlock VP8EncQuantizeBlock;
87
88// Compute susceptibility based on DCT-coeff histograms:
89// the higher, the "easier" the macroblock is to compress.
90typedef int (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
91                         int start_block, int end_block);
92extern const int VP8DspScan[16 + 4 + 4];
93extern VP8CHisto VP8CollectHistogram;
94
95void VP8EncDspInit(void);   // must be called before using any of the above
96
97//------------------------------------------------------------------------------
98// Decoding
99
100typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
101// when doing two transforms, coeffs is actually int16_t[2][16].
102typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
103extern VP8DecIdct2 VP8Transform;
104extern VP8DecIdct VP8TransformUV;
105extern VP8DecIdct VP8TransformDC;
106extern VP8DecIdct VP8TransformDCUV;
107extern void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
108
109// *dst is the destination block, with stride BPS. Boundary samples are
110// assumed accessible when needed.
111typedef void (*VP8PredFunc)(uint8_t* dst);
112extern const VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
113extern const VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
114extern const VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
115
116// simple filter (only for luma)
117typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
118extern VP8SimpleFilterFunc VP8SimpleVFilter16;
119extern VP8SimpleFilterFunc VP8SimpleHFilter16;
120extern VP8SimpleFilterFunc VP8SimpleVFilter16i;  // filter 3 inner edges
121extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
122
123// regular filter (on both macroblock edges and inner edges)
124typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
125                                  int thresh, int ithresh, int hev_t);
126typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
127                                    int thresh, int ithresh, int hev_t);
128// on outer edge
129extern VP8LumaFilterFunc VP8VFilter16;
130extern VP8LumaFilterFunc VP8HFilter16;
131extern VP8ChromaFilterFunc VP8VFilter8;
132extern VP8ChromaFilterFunc VP8HFilter8;
133
134// on inner edge
135extern VP8LumaFilterFunc VP8VFilter16i;   // filtering 3 inner edges altogether
136extern VP8LumaFilterFunc VP8HFilter16i;
137extern VP8ChromaFilterFunc VP8VFilter8i;  // filtering u and v altogether
138extern VP8ChromaFilterFunc VP8HFilter8i;
139
140// must be called before anything using the above
141void VP8DspInit(void);
142
143//------------------------------------------------------------------------------
144// WebP I/O
145
146#define FANCY_UPSAMPLING   // undefined to remove fancy upsampling support
147
148typedef void (*WebPUpsampleLinePairFunc)(
149    const uint8_t* top_y, const uint8_t* bottom_y,
150    const uint8_t* top_u, const uint8_t* top_v,
151    const uint8_t* cur_u, const uint8_t* cur_v,
152    uint8_t* top_dst, uint8_t* bottom_dst, int len);
153
154#ifdef FANCY_UPSAMPLING
155
156// Fancy upsampling functions to convert YUV to RGB(A) modes
157extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
158
159// Initializes SSE2 version of the fancy upsamplers.
160void WebPInitUpsamplersSSE2(void);
161
162#endif    // FANCY_UPSAMPLING
163
164// Point-sampling methods.
165typedef void (*WebPSampleLinePairFunc)(
166    const uint8_t* top_y, const uint8_t* bottom_y,
167    const uint8_t* u, const uint8_t* v,
168    uint8_t* top_dst, uint8_t* bottom_dst, int len);
169
170extern const WebPSampleLinePairFunc WebPSamplers[/* MODE_LAST */];
171
172// General function for converting two lines of ARGB or RGBA.
173// 'alpha_is_last' should be true if 0xff000000 is stored in memory as
174// as 0x00, 0x00, 0x00, 0xff (little endian).
175WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
176
177// YUV444->RGB converters
178typedef void (*WebPYUV444Converter)(const uint8_t* y,
179                                    const uint8_t* u, const uint8_t* v,
180                                    uint8_t* dst, int len);
181
182extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
183
184// Main function to be called
185void WebPInitUpsamplers(void);
186
187//------------------------------------------------------------------------------
188// Pre-multiply planes with alpha values
189
190// Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
191// alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
192extern void (*WebPApplyAlphaMultiply)(
193    uint8_t* rgba, int alpha_first, int w, int h, int stride);
194
195// Same, buf specifically for RGBA4444 format
196extern void (*WebPApplyAlphaMultiply4444)(
197    uint8_t* rgba4444, int w, int h, int stride);
198
199// To be called first before using the above.
200void WebPInitPremultiply(void);
201
202void WebPInitPremultiplySSE2(void);   // should not be called directly.
203
204//------------------------------------------------------------------------------
205
206#if defined(__cplusplus) || defined(c_plusplus)
207}    // extern "C"
208#endif
209
210#endif  /* WEBP_DSP_DSP_H_ */
211