dsp.h revision a2415724fb3466168b2af5b08bd94ba732c0e753
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2011 Google Inc. All Rights Reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This code is licensed under the same terms as WebM:
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  Software License Agreement:  http://www.webmproject.org/license/software/
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// -----------------------------------------------------------------------------
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//   Speed-critical functions.
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)//
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Author: Skal (pascal.massimino@gmail.com)
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef WEBP_DSP_DSP_H_
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define WEBP_DSP_DSP_H_
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "webp/types.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(__cplusplus) || defined(c_plusplus)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CPU detection
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define WEBP_MSC_SSE2  // Visual C++ SSE2 targets
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if defined(__SSE2__) || defined(WEBP_MSC_SSE2)
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define WEBP_USE_SSE2
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//#if defined(__ANDROID__) && defined(__ARM_ARCH_7A__)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//#define WEBP_ANDROID_NEON  // Android targets that might support NEON
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//#endif
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if defined(__ARM_NEON__) || defined(WEBP_ANDROID_NEON)
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define WEBP_USE_NEON
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef enum {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  kSSE2,
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  kSSE3,
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  kNEON
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} CPUFeature;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// returns true if the CPU supports the feature.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int (*VP8CPUInfo)(CPUFeature feature);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8CPUInfo VP8GetCPUInfo;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Encoding
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Transforms
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//          will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        int do_two);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8Idct VP8ITransform;
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)extern VP8Fdct VP8FTransform;
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)extern VP8WHT VP8ITransformWHT;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8WHT VP8FTransformWHT;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Predictions
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// *dst is the destination block. *top and *left can be NULL.
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const uint8_t* top);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8Intra4Preds VP8EncPredLuma4;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8IntraPreds VP8EncPredLuma16;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8IntraPreds VP8EncPredChroma8;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const uint16_t* const weights);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8BlockCopy VP8Copy4x4;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Quantization
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct VP8Matrix;   // forward declaration
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                int n, const struct VP8Matrix* const mtx);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern VP8QuantizeBlock VP8EncQuantizeBlock;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 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 void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
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#endif    // FANCY_UPSAMPLING
162
163// Point-sampling methods.
164typedef void (*WebPSampleLinePairFunc)(
165    const uint8_t* top_y, const uint8_t* bottom_y,
166    const uint8_t* u, const uint8_t* v,
167    uint8_t* top_dst, uint8_t* bottom_dst, int len);
168
169extern const WebPSampleLinePairFunc WebPSamplers[/* MODE_LAST */];
170
171// General function for converting two lines of ARGB or RGBA.
172// 'alpha_is_last' should be true if 0xff000000 is stored in memory as
173// as 0x00, 0x00, 0x00, 0xff (little endian).
174WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
175
176// YUV444->RGB converters
177typedef void (*WebPYUV444Converter)(const uint8_t* y,
178                                    const uint8_t* u, const uint8_t* v,
179                                    uint8_t* dst, int len);
180
181extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
182
183// Main function to be called
184void WebPInitUpsamplers(void);
185
186//------------------------------------------------------------------------------
187// Pre-multiply planes with alpha values
188
189// Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
190// alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
191extern void (*WebPApplyAlphaMultiply)(
192    uint8_t* rgba, int alpha_first, int w, int h, int stride);
193
194// Same, buf specifically for RGBA4444 format
195extern void (*WebPApplyAlphaMultiply4444)(
196    uint8_t* rgba4444, int w, int h, int stride);
197
198// To be called first before using the above.
199void WebPInitPremultiply(void);
200
201void WebPInitPremultiplySSE2(void);   // should not be called directly.
202
203//------------------------------------------------------------------------------
204
205#if defined(__cplusplus) || defined(c_plusplus)
206}    // extern "C"
207#endif
208
209#endif  /* WEBP_DSP_DSP_H_ */
210