blob: 65a2a885b3499ba9b3378072dc788a152ca39626 [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2010 Google Inc. All Rights Reserved.
Eric Hassold9aea6422011-01-04 17:22:46 -08002//
Vikas Arora0406ce12013-08-09 15:57:12 -07003// 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.
Eric Hassold9aea6422011-01-04 17:22:46 -08008// -----------------------------------------------------------------------------
9//
Vikas Aroraa2415722012-08-09 16:18:58 -070010// Speed-critical decoding functions.
Eric Hassold9aea6422011-01-04 17:22:46 -080011//
12// Author: Skal (pascal.massimino@gmail.com)
13
Vikas Aroraa2415722012-08-09 16:18:58 -070014#include "./dsp.h"
15#include "../dec/vp8i.h"
Eric Hassold9aea6422011-01-04 17:22:46 -080016
Vikas Aroraa2415722012-08-09 16:18:58 -070017//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -080018
Vikas Aroraa2415722012-08-09 16:18:58 -070019static WEBP_INLINE uint8_t clip_8b(int v) {
Eric Hassold9aea6422011-01-04 17:22:46 -080020 return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
21}
22
Vikas Aroraa2415722012-08-09 16:18:58 -070023//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -080024// Transforms (Paragraph 14.4)
25
26#define STORE(x, y, v) \
27 dst[x + y * BPS] = clip_8b(dst[x + y * BPS] + ((v) >> 3))
28
Vikas Arora8b720222014-01-02 16:48:02 -080029#define STORE2(y, dc, d, c) do { \
30 const int DC = (dc); \
31 STORE(0, y, DC + (d)); \
32 STORE(1, y, DC + (c)); \
33 STORE(2, y, DC - (c)); \
34 STORE(3, y, DC - (d)); \
35} while (0)
36
Eric Hassold9aea6422011-01-04 17:22:46 -080037static const int kC1 = 20091 + (1 << 16);
38static const int kC2 = 35468;
39#define MUL(a, b) (((a) * (b)) >> 16)
40
Vikas Arora46672792011-07-13 16:37:55 +053041static void TransformOne(const int16_t* in, uint8_t* dst) {
Eric Hassold9aea6422011-01-04 17:22:46 -080042 int C[4 * 4], *tmp;
43 int i;
44 tmp = C;
45 for (i = 0; i < 4; ++i) { // vertical pass
46 const int a = in[0] + in[8]; // [-4096, 4094]
47 const int b = in[0] - in[8]; // [-4095, 4095]
48 const int c = MUL(in[4], kC2) - MUL(in[12], kC1); // [-3783, 3783]
49 const int d = MUL(in[4], kC1) + MUL(in[12], kC2); // [-3785, 3781]
50 tmp[0] = a + d; // [-7881, 7875]
51 tmp[1] = b + c; // [-7878, 7878]
52 tmp[2] = b - c; // [-7878, 7878]
53 tmp[3] = a - d; // [-7877, 7879]
54 tmp += 4;
55 in++;
56 }
57 // Each pass is expanding the dynamic range by ~3.85 (upper bound).
58 // The exact value is (2. + (kC1 + kC2) / 65536).
59 // After the second pass, maximum interval is [-3794, 3794], assuming
60 // an input in [-2048, 2047] interval. We then need to add a dst value
61 // in the [0, 255] range.
62 // In the worst case scenario, the input to clip_8b() can be as large as
63 // [-60713, 60968].
64 tmp = C;
65 for (i = 0; i < 4; ++i) { // horizontal pass
66 const int dc = tmp[0] + 4;
67 const int a = dc + tmp[8];
68 const int b = dc - tmp[8];
69 const int c = MUL(tmp[4], kC2) - MUL(tmp[12], kC1);
70 const int d = MUL(tmp[4], kC1) + MUL(tmp[12], kC2);
71 STORE(0, 0, a + d);
72 STORE(1, 0, b + c);
73 STORE(2, 0, b - c);
74 STORE(3, 0, a - d);
75 tmp++;
76 dst += BPS;
77 }
78}
Vikas Arora8b720222014-01-02 16:48:02 -080079
80// Simplified transform when only in[0], in[1] and in[4] are non-zero
81static void TransformAC3(const int16_t* in, uint8_t* dst) {
82 const int a = in[0] + 4;
83 const int c4 = MUL(in[4], kC2);
84 const int d4 = MUL(in[4], kC1);
85 const int c1 = MUL(in[1], kC2);
86 const int d1 = MUL(in[1], kC1);
87 STORE2(0, a + d4, d1, c1);
88 STORE2(1, a + c4, d1, c1);
89 STORE2(2, a - c4, d1, c1);
90 STORE2(3, a - d4, d1, c1);
91}
Eric Hassold9aea6422011-01-04 17:22:46 -080092#undef MUL
Vikas Arora8b720222014-01-02 16:48:02 -080093#undef STORE2
Eric Hassold9aea6422011-01-04 17:22:46 -080094
Vikas Arora46672792011-07-13 16:37:55 +053095static void TransformTwo(const int16_t* in, uint8_t* dst, int do_two) {
96 TransformOne(in, dst);
97 if (do_two) {
98 TransformOne(in + 16, dst + 4);
99 }
100}
101
Eric Hassold9aea6422011-01-04 17:22:46 -0800102static void TransformUV(const int16_t* in, uint8_t* dst) {
Vikas Arora46672792011-07-13 16:37:55 +0530103 VP8Transform(in + 0 * 16, dst, 1);
104 VP8Transform(in + 2 * 16, dst + 4 * BPS, 1);
Eric Hassold9aea6422011-01-04 17:22:46 -0800105}
106
107static void TransformDC(const int16_t *in, uint8_t* dst) {
108 const int DC = in[0] + 4;
109 int i, j;
110 for (j = 0; j < 4; ++j) {
111 for (i = 0; i < 4; ++i) {
112 STORE(i, j, DC);
113 }
114 }
115}
116
117static void TransformDCUV(const int16_t* in, uint8_t* dst) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700118 if (in[0 * 16]) VP8TransformDC(in + 0 * 16, dst);
119 if (in[1 * 16]) VP8TransformDC(in + 1 * 16, dst + 4);
120 if (in[2 * 16]) VP8TransformDC(in + 2 * 16, dst + 4 * BPS);
121 if (in[3 * 16]) VP8TransformDC(in + 3 * 16, dst + 4 * BPS + 4);
Eric Hassold9aea6422011-01-04 17:22:46 -0800122}
123
124#undef STORE
125
Vikas Aroraa2415722012-08-09 16:18:58 -0700126//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800127// Paragraph 14.3
128
129static void TransformWHT(const int16_t* in, int16_t* out) {
130 int tmp[16];
131 int i;
132 for (i = 0; i < 4; ++i) {
133 const int a0 = in[0 + i] + in[12 + i];
134 const int a1 = in[4 + i] + in[ 8 + i];
135 const int a2 = in[4 + i] - in[ 8 + i];
136 const int a3 = in[0 + i] - in[12 + i];
137 tmp[0 + i] = a0 + a1;
138 tmp[8 + i] = a0 - a1;
139 tmp[4 + i] = a3 + a2;
140 tmp[12 + i] = a3 - a2;
141 }
142 for (i = 0; i < 4; ++i) {
143 const int dc = tmp[0 + i * 4] + 3; // w/ rounder
144 const int a0 = dc + tmp[3 + i * 4];
145 const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
146 const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
147 const int a3 = dc - tmp[3 + i * 4];
148 out[ 0] = (a0 + a1) >> 3;
149 out[16] = (a3 + a2) >> 3;
150 out[32] = (a0 - a1) >> 3;
151 out[48] = (a3 - a2) >> 3;
152 out += 64;
153 }
154}
155
Vikas Aroraaf51b942014-08-28 10:51:12 -0700156void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
Eric Hassold9aea6422011-01-04 17:22:46 -0800157
Vikas Aroraa2415722012-08-09 16:18:58 -0700158//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800159// Intra predictions
160
Vikas Aroraa2415722012-08-09 16:18:58 -0700161#define DST(x, y) dst[(x) + (y) * BPS]
Eric Hassold9aea6422011-01-04 17:22:46 -0800162
Vikas Aroraa2415722012-08-09 16:18:58 -0700163static WEBP_INLINE void TrueMotion(uint8_t *dst, int size) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800164 const uint8_t* top = dst - BPS;
Vikas Aroraaf51b942014-08-28 10:51:12 -0700165 const uint8_t* const clip0 = VP8kclip1 - top[-1];
Eric Hassold9aea6422011-01-04 17:22:46 -0800166 int y;
167 for (y = 0; y < size; ++y) {
168 const uint8_t* const clip = clip0 + dst[-1];
169 int x;
170 for (x = 0; x < size; ++x) {
171 dst[x] = clip[top[x]];
172 }
173 dst += BPS;
174 }
175}
176static void TM4(uint8_t *dst) { TrueMotion(dst, 4); }
177static void TM8uv(uint8_t *dst) { TrueMotion(dst, 8); }
178static void TM16(uint8_t *dst) { TrueMotion(dst, 16); }
179
Vikas Aroraa2415722012-08-09 16:18:58 -0700180//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800181// 16x16
182
183static void VE16(uint8_t *dst) { // vertical
184 int j;
185 for (j = 0; j < 16; ++j) {
186 memcpy(dst + j * BPS, dst - BPS, 16);
187 }
188}
189
190static void HE16(uint8_t *dst) { // horizontal
191 int j;
192 for (j = 16; j > 0; --j) {
193 memset(dst, dst[-1], 16);
194 dst += BPS;
195 }
196}
197
Vikas Aroraa2415722012-08-09 16:18:58 -0700198static WEBP_INLINE void Put16(int v, uint8_t* dst) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800199 int j;
200 for (j = 0; j < 16; ++j) {
201 memset(dst + j * BPS, v, 16);
202 }
203}
204
205static void DC16(uint8_t *dst) { // DC
206 int DC = 16;
207 int j;
208 for (j = 0; j < 16; ++j) {
209 DC += dst[-1 + j * BPS] + dst[j - BPS];
210 }
211 Put16(DC >> 5, dst);
212}
213
214static void DC16NoTop(uint8_t *dst) { // DC with top samples not available
215 int DC = 8;
216 int j;
217 for (j = 0; j < 16; ++j) {
218 DC += dst[-1 + j * BPS];
219 }
220 Put16(DC >> 4, dst);
221}
222
223static void DC16NoLeft(uint8_t *dst) { // DC with left samples not available
224 int DC = 8;
225 int i;
226 for (i = 0; i < 16; ++i) {
227 DC += dst[i - BPS];
228 }
229 Put16(DC >> 4, dst);
230}
231
232static void DC16NoTopLeft(uint8_t *dst) { // DC with no top and left samples
233 Put16(0x80, dst);
234}
235
Vikas Aroraa2415722012-08-09 16:18:58 -0700236//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800237// 4x4
238
239#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
240#define AVG2(a, b) (((a) + (b) + 1) >> 1)
241
242static void VE4(uint8_t *dst) { // vertical
243 const uint8_t* top = dst - BPS;
244 const uint8_t vals[4] = {
245 AVG3(top[-1], top[0], top[1]),
246 AVG3(top[ 0], top[1], top[2]),
247 AVG3(top[ 1], top[2], top[3]),
248 AVG3(top[ 2], top[3], top[4])
249 };
250 int i;
251 for (i = 0; i < 4; ++i) {
252 memcpy(dst + i * BPS, vals, sizeof(vals));
253 }
254}
255
256static void HE4(uint8_t *dst) { // horizontal
257 const int A = dst[-1 - BPS];
258 const int B = dst[-1];
259 const int C = dst[-1 + BPS];
260 const int D = dst[-1 + 2 * BPS];
261 const int E = dst[-1 + 3 * BPS];
262 *(uint32_t*)(dst + 0 * BPS) = 0x01010101U * AVG3(A, B, C);
263 *(uint32_t*)(dst + 1 * BPS) = 0x01010101U * AVG3(B, C, D);
264 *(uint32_t*)(dst + 2 * BPS) = 0x01010101U * AVG3(C, D, E);
265 *(uint32_t*)(dst + 3 * BPS) = 0x01010101U * AVG3(D, E, E);
266}
267
268static void DC4(uint8_t *dst) { // DC
269 uint32_t dc = 4;
270 int i;
271 for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
272 dc >>= 3;
273 for (i = 0; i < 4; ++i) memset(dst + i * BPS, dc, 4);
274}
275
276static void RD4(uint8_t *dst) { // Down-right
277 const int I = dst[-1 + 0 * BPS];
278 const int J = dst[-1 + 1 * BPS];
279 const int K = dst[-1 + 2 * BPS];
280 const int L = dst[-1 + 3 * BPS];
281 const int X = dst[-1 - BPS];
282 const int A = dst[0 - BPS];
283 const int B = dst[1 - BPS];
284 const int C = dst[2 - BPS];
285 const int D = dst[3 - BPS];
Vikas Aroraa2415722012-08-09 16:18:58 -0700286 DST(0, 3) = AVG3(J, K, L);
287 DST(0, 2) = DST(1, 3) = AVG3(I, J, K);
288 DST(0, 1) = DST(1, 2) = DST(2, 3) = AVG3(X, I, J);
289 DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I);
290 DST(1, 0) = DST(2, 1) = DST(3, 2) = AVG3(B, A, X);
291 DST(2, 0) = DST(3, 1) = AVG3(C, B, A);
292 DST(3, 0) = AVG3(D, C, B);
Eric Hassold9aea6422011-01-04 17:22:46 -0800293}
294
295static void LD4(uint8_t *dst) { // Down-Left
296 const int A = dst[0 - BPS];
297 const int B = dst[1 - BPS];
298 const int C = dst[2 - BPS];
299 const int D = dst[3 - BPS];
300 const int E = dst[4 - BPS];
301 const int F = dst[5 - BPS];
302 const int G = dst[6 - BPS];
303 const int H = dst[7 - BPS];
Vikas Aroraa2415722012-08-09 16:18:58 -0700304 DST(0, 0) = AVG3(A, B, C);
305 DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
306 DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
307 DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
308 DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
309 DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
310 DST(3, 3) = AVG3(G, H, H);
Eric Hassold9aea6422011-01-04 17:22:46 -0800311}
312
313static void VR4(uint8_t *dst) { // Vertical-Right
314 const int I = dst[-1 + 0 * BPS];
315 const int J = dst[-1 + 1 * BPS];
316 const int K = dst[-1 + 2 * BPS];
317 const int X = dst[-1 - BPS];
318 const int A = dst[0 - BPS];
319 const int B = dst[1 - BPS];
320 const int C = dst[2 - BPS];
321 const int D = dst[3 - BPS];
Vikas Aroraa2415722012-08-09 16:18:58 -0700322 DST(0, 0) = DST(1, 2) = AVG2(X, A);
323 DST(1, 0) = DST(2, 2) = AVG2(A, B);
324 DST(2, 0) = DST(3, 2) = AVG2(B, C);
325 DST(3, 0) = AVG2(C, D);
Eric Hassold9aea6422011-01-04 17:22:46 -0800326
Vikas Aroraa2415722012-08-09 16:18:58 -0700327 DST(0, 3) = AVG3(K, J, I);
328 DST(0, 2) = AVG3(J, I, X);
329 DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
330 DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
331 DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
332 DST(3, 1) = AVG3(B, C, D);
Eric Hassold9aea6422011-01-04 17:22:46 -0800333}
334
335static void VL4(uint8_t *dst) { // Vertical-Left
336 const int A = dst[0 - BPS];
337 const int B = dst[1 - BPS];
338 const int C = dst[2 - BPS];
339 const int D = dst[3 - BPS];
340 const int E = dst[4 - BPS];
341 const int F = dst[5 - BPS];
342 const int G = dst[6 - BPS];
343 const int H = dst[7 - BPS];
Vikas Aroraa2415722012-08-09 16:18:58 -0700344 DST(0, 0) = AVG2(A, B);
345 DST(1, 0) = DST(0, 2) = AVG2(B, C);
346 DST(2, 0) = DST(1, 2) = AVG2(C, D);
347 DST(3, 0) = DST(2, 2) = AVG2(D, E);
Eric Hassold9aea6422011-01-04 17:22:46 -0800348
Vikas Aroraa2415722012-08-09 16:18:58 -0700349 DST(0, 1) = AVG3(A, B, C);
350 DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
351 DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
352 DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
353 DST(3, 2) = AVG3(E, F, G);
354 DST(3, 3) = AVG3(F, G, H);
Eric Hassold9aea6422011-01-04 17:22:46 -0800355}
356
357static void HU4(uint8_t *dst) { // Horizontal-Up
358 const int I = dst[-1 + 0 * BPS];
359 const int J = dst[-1 + 1 * BPS];
360 const int K = dst[-1 + 2 * BPS];
361 const int L = dst[-1 + 3 * BPS];
Vikas Aroraa2415722012-08-09 16:18:58 -0700362 DST(0, 0) = AVG2(I, J);
363 DST(2, 0) = DST(0, 1) = AVG2(J, K);
364 DST(2, 1) = DST(0, 2) = AVG2(K, L);
365 DST(1, 0) = AVG3(I, J, K);
366 DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
367 DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
368 DST(3, 2) = DST(2, 2) =
369 DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
Eric Hassold9aea6422011-01-04 17:22:46 -0800370}
371
372static void HD4(uint8_t *dst) { // Horizontal-Down
373 const int I = dst[-1 + 0 * BPS];
374 const int J = dst[-1 + 1 * BPS];
375 const int K = dst[-1 + 2 * BPS];
376 const int L = dst[-1 + 3 * BPS];
377 const int X = dst[-1 - BPS];
378 const int A = dst[0 - BPS];
379 const int B = dst[1 - BPS];
380 const int C = dst[2 - BPS];
381
Vikas Aroraa2415722012-08-09 16:18:58 -0700382 DST(0, 0) = DST(2, 1) = AVG2(I, X);
383 DST(0, 1) = DST(2, 2) = AVG2(J, I);
384 DST(0, 2) = DST(2, 3) = AVG2(K, J);
385 DST(0, 3) = AVG2(L, K);
Eric Hassold9aea6422011-01-04 17:22:46 -0800386
Vikas Aroraa2415722012-08-09 16:18:58 -0700387 DST(3, 0) = AVG3(A, B, C);
388 DST(2, 0) = AVG3(X, A, B);
389 DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
390 DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
391 DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
392 DST(1, 3) = AVG3(L, K, J);
Eric Hassold9aea6422011-01-04 17:22:46 -0800393}
394
Vikas Aroraa2415722012-08-09 16:18:58 -0700395#undef DST
Eric Hassold9aea6422011-01-04 17:22:46 -0800396#undef AVG3
397#undef AVG2
398
Vikas Aroraa2415722012-08-09 16:18:58 -0700399//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800400// Chroma
401
402static void VE8uv(uint8_t *dst) { // vertical
403 int j;
404 for (j = 0; j < 8; ++j) {
405 memcpy(dst + j * BPS, dst - BPS, 8);
406 }
407}
408
409static void HE8uv(uint8_t *dst) { // horizontal
410 int j;
411 for (j = 0; j < 8; ++j) {
412 memset(dst, dst[-1], 8);
413 dst += BPS;
414 }
415}
416
417// helper for chroma-DC predictions
Vikas Arora1e7bf882013-03-13 16:43:18 -0700418static WEBP_INLINE void Put8x8uv(uint8_t value, uint8_t* dst) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800419 int j;
420 for (j = 0; j < 8; ++j) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700421 memset(dst + j * BPS, value, 8);
Eric Hassold9aea6422011-01-04 17:22:46 -0800422 }
423}
424
425static void DC8uv(uint8_t *dst) { // DC
426 int dc0 = 8;
427 int i;
428 for (i = 0; i < 8; ++i) {
429 dc0 += dst[i - BPS] + dst[-1 + i * BPS];
430 }
Vikas Arora1e7bf882013-03-13 16:43:18 -0700431 Put8x8uv(dc0 >> 4, dst);
Eric Hassold9aea6422011-01-04 17:22:46 -0800432}
433
434static void DC8uvNoLeft(uint8_t *dst) { // DC with no left samples
435 int dc0 = 4;
436 int i;
437 for (i = 0; i < 8; ++i) {
438 dc0 += dst[i - BPS];
439 }
Vikas Arora1e7bf882013-03-13 16:43:18 -0700440 Put8x8uv(dc0 >> 3, dst);
Eric Hassold9aea6422011-01-04 17:22:46 -0800441}
442
443static void DC8uvNoTop(uint8_t *dst) { // DC with no top samples
444 int dc0 = 4;
445 int i;
446 for (i = 0; i < 8; ++i) {
447 dc0 += dst[-1 + i * BPS];
448 }
Vikas Arora1e7bf882013-03-13 16:43:18 -0700449 Put8x8uv(dc0 >> 3, dst);
Eric Hassold9aea6422011-01-04 17:22:46 -0800450}
451
452static void DC8uvNoTopLeft(uint8_t *dst) { // DC with nothing
Vikas Arora1e7bf882013-03-13 16:43:18 -0700453 Put8x8uv(0x80, dst);
Eric Hassold9aea6422011-01-04 17:22:46 -0800454}
455
Vikas Aroraa2415722012-08-09 16:18:58 -0700456//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800457// default C implementations
458
Vikas Aroraa2415722012-08-09 16:18:58 -0700459const VP8PredFunc VP8PredLuma4[NUM_BMODES] = {
Eric Hassold9aea6422011-01-04 17:22:46 -0800460 DC4, TM4, VE4, HE4, RD4, VR4, LD4, VL4, HD4, HU4
461};
462
Vikas Aroraa2415722012-08-09 16:18:58 -0700463const VP8PredFunc VP8PredLuma16[NUM_B_DC_MODES] = {
Eric Hassold9aea6422011-01-04 17:22:46 -0800464 DC16, TM16, VE16, HE16,
465 DC16NoTop, DC16NoLeft, DC16NoTopLeft
466};
467
Vikas Aroraa2415722012-08-09 16:18:58 -0700468const VP8PredFunc VP8PredChroma8[NUM_B_DC_MODES] = {
Eric Hassold9aea6422011-01-04 17:22:46 -0800469 DC8uv, TM8uv, VE8uv, HE8uv,
470 DC8uvNoTop, DC8uvNoLeft, DC8uvNoTopLeft
471};
472
Vikas Aroraa2415722012-08-09 16:18:58 -0700473//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800474// Edge filtering functions
475
476// 4 pixels in, 2 pixels out
Vikas Aroraa2415722012-08-09 16:18:58 -0700477static WEBP_INLINE void do_filter2(uint8_t* p, int step) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800478 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
Vikas Aroraaf51b942014-08-28 10:51:12 -0700479 const int a = 3 * (q0 - p0) + VP8ksclip1[p1 - q1]; // in [-893,892]
480 const int a1 = VP8ksclip2[(a + 4) >> 3]; // in [-16,15]
481 const int a2 = VP8ksclip2[(a + 3) >> 3];
482 p[-step] = VP8kclip1[p0 + a2];
483 p[ 0] = VP8kclip1[q0 - a1];
Eric Hassold9aea6422011-01-04 17:22:46 -0800484}
485
486// 4 pixels in, 4 pixels out
Vikas Aroraa2415722012-08-09 16:18:58 -0700487static WEBP_INLINE void do_filter4(uint8_t* p, int step) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800488 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
489 const int a = 3 * (q0 - p0);
Vikas Aroraaf51b942014-08-28 10:51:12 -0700490 const int a1 = VP8ksclip2[(a + 4) >> 3];
491 const int a2 = VP8ksclip2[(a + 3) >> 3];
Eric Hassold9aea6422011-01-04 17:22:46 -0800492 const int a3 = (a1 + 1) >> 1;
Vikas Aroraaf51b942014-08-28 10:51:12 -0700493 p[-2*step] = VP8kclip1[p1 + a3];
494 p[- step] = VP8kclip1[p0 + a2];
495 p[ 0] = VP8kclip1[q0 - a1];
496 p[ step] = VP8kclip1[q1 - a3];
Eric Hassold9aea6422011-01-04 17:22:46 -0800497}
498
499// 6 pixels in, 6 pixels out
Vikas Aroraa2415722012-08-09 16:18:58 -0700500static WEBP_INLINE void do_filter6(uint8_t* p, int step) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800501 const int p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
502 const int q0 = p[0], q1 = p[step], q2 = p[2*step];
Vikas Aroraaf51b942014-08-28 10:51:12 -0700503 const int a = VP8ksclip1[3 * (q0 - p0) + VP8ksclip1[p1 - q1]];
504 // a is in [-128,127], a1 in [-27,27], a2 in [-18,18] and a3 in [-9,9]
Eric Hassold9aea6422011-01-04 17:22:46 -0800505 const int a1 = (27 * a + 63) >> 7; // eq. to ((3 * a + 7) * 9) >> 7
506 const int a2 = (18 * a + 63) >> 7; // eq. to ((2 * a + 7) * 9) >> 7
507 const int a3 = (9 * a + 63) >> 7; // eq. to ((1 * a + 7) * 9) >> 7
Vikas Aroraaf51b942014-08-28 10:51:12 -0700508 p[-3*step] = VP8kclip1[p2 + a3];
509 p[-2*step] = VP8kclip1[p1 + a2];
510 p[- step] = VP8kclip1[p0 + a1];
511 p[ 0] = VP8kclip1[q0 - a1];
512 p[ step] = VP8kclip1[q1 - a2];
513 p[ 2*step] = VP8kclip1[q2 - a3];
Eric Hassold9aea6422011-01-04 17:22:46 -0800514}
515
Vikas Aroraa2415722012-08-09 16:18:58 -0700516static WEBP_INLINE int hev(const uint8_t* p, int step, int thresh) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800517 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
Vikas Aroraaf51b942014-08-28 10:51:12 -0700518 return (VP8kabs0[p1 - p0] > thresh) || (VP8kabs0[q1 - q0] > thresh);
Eric Hassold9aea6422011-01-04 17:22:46 -0800519}
520
Vikas Aroraaf51b942014-08-28 10:51:12 -0700521static WEBP_INLINE int needs_filter(const uint8_t* p, int step, int t) {
522 const int p1 = p[-2 * step], p0 = p[-step], q0 = p[0], q1 = p[step];
523 return ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) <= t);
Eric Hassold9aea6422011-01-04 17:22:46 -0800524}
525
Vikas Aroraa2415722012-08-09 16:18:58 -0700526static WEBP_INLINE int needs_filter2(const uint8_t* p,
527 int step, int t, int it) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700528 const int p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step];
529 const int p0 = p[-step], q0 = p[0];
530 const int q1 = p[step], q2 = p[2 * step], q3 = p[3 * step];
531 if ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) > t) return 0;
532 return VP8kabs0[p3 - p2] <= it && VP8kabs0[p2 - p1] <= it &&
533 VP8kabs0[p1 - p0] <= it && VP8kabs0[q3 - q2] <= it &&
534 VP8kabs0[q2 - q1] <= it && VP8kabs0[q1 - q0] <= it;
Eric Hassold9aea6422011-01-04 17:22:46 -0800535}
536
Vikas Aroraa2415722012-08-09 16:18:58 -0700537//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800538// Simple In-loop filtering (Paragraph 15.2)
539
540static void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
541 int i;
Vikas Aroraaf51b942014-08-28 10:51:12 -0700542 const int thresh2 = 2 * thresh + 1;
Eric Hassold9aea6422011-01-04 17:22:46 -0800543 for (i = 0; i < 16; ++i) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700544 if (needs_filter(p + i, stride, thresh2)) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800545 do_filter2(p + i, stride);
546 }
547 }
548}
549
550static void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
551 int i;
Vikas Aroraaf51b942014-08-28 10:51:12 -0700552 const int thresh2 = 2 * thresh + 1;
Eric Hassold9aea6422011-01-04 17:22:46 -0800553 for (i = 0; i < 16; ++i) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700554 if (needs_filter(p + i * stride, 1, thresh2)) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800555 do_filter2(p + i * stride, 1);
556 }
557 }
558}
559
560static void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
561 int k;
562 for (k = 3; k > 0; --k) {
563 p += 4 * stride;
564 SimpleVFilter16(p, stride, thresh);
565 }
566}
567
568static void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
569 int k;
570 for (k = 3; k > 0; --k) {
571 p += 4;
572 SimpleHFilter16(p, stride, thresh);
573 }
574}
575
Vikas Aroraa2415722012-08-09 16:18:58 -0700576//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800577// Complex In-loop filtering (Paragraph 15.3)
578
Vikas Aroraa2415722012-08-09 16:18:58 -0700579static WEBP_INLINE void FilterLoop26(uint8_t* p,
580 int hstride, int vstride, int size,
581 int thresh, int ithresh, int hev_thresh) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700582 const int thresh2 = 2 * thresh + 1;
Eric Hassold9aea6422011-01-04 17:22:46 -0800583 while (size-- > 0) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700584 if (needs_filter2(p, hstride, thresh2, ithresh)) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800585 if (hev(p, hstride, hev_thresh)) {
586 do_filter2(p, hstride);
587 } else {
588 do_filter6(p, hstride);
589 }
590 }
591 p += vstride;
592 }
593}
594
Vikas Aroraa2415722012-08-09 16:18:58 -0700595static WEBP_INLINE void FilterLoop24(uint8_t* p,
596 int hstride, int vstride, int size,
597 int thresh, int ithresh, int hev_thresh) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700598 const int thresh2 = 2 * thresh + 1;
Eric Hassold9aea6422011-01-04 17:22:46 -0800599 while (size-- > 0) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700600 if (needs_filter2(p, hstride, thresh2, ithresh)) {
Eric Hassold9aea6422011-01-04 17:22:46 -0800601 if (hev(p, hstride, hev_thresh)) {
602 do_filter2(p, hstride);
603 } else {
604 do_filter4(p, hstride);
605 }
606 }
607 p += vstride;
608 }
609}
610
611// on macroblock edges
612static void VFilter16(uint8_t* p, int stride,
613 int thresh, int ithresh, int hev_thresh) {
614 FilterLoop26(p, stride, 1, 16, thresh, ithresh, hev_thresh);
615}
616
617static void HFilter16(uint8_t* p, int stride,
618 int thresh, int ithresh, int hev_thresh) {
619 FilterLoop26(p, 1, stride, 16, thresh, ithresh, hev_thresh);
620}
621
622// on three inner edges
623static void VFilter16i(uint8_t* p, int stride,
624 int thresh, int ithresh, int hev_thresh) {
625 int k;
626 for (k = 3; k > 0; --k) {
627 p += 4 * stride;
628 FilterLoop24(p, stride, 1, 16, thresh, ithresh, hev_thresh);
629 }
630}
631
632static void HFilter16i(uint8_t* p, int stride,
633 int thresh, int ithresh, int hev_thresh) {
634 int k;
635 for (k = 3; k > 0; --k) {
636 p += 4;
637 FilterLoop24(p, 1, stride, 16, thresh, ithresh, hev_thresh);
638 }
639}
640
641// 8-pixels wide variant, for chroma filtering
642static void VFilter8(uint8_t* u, uint8_t* v, int stride,
643 int thresh, int ithresh, int hev_thresh) {
644 FilterLoop26(u, stride, 1, 8, thresh, ithresh, hev_thresh);
645 FilterLoop26(v, stride, 1, 8, thresh, ithresh, hev_thresh);
646}
647
648static void HFilter8(uint8_t* u, uint8_t* v, int stride,
649 int thresh, int ithresh, int hev_thresh) {
650 FilterLoop26(u, 1, stride, 8, thresh, ithresh, hev_thresh);
651 FilterLoop26(v, 1, stride, 8, thresh, ithresh, hev_thresh);
652}
653
654static void VFilter8i(uint8_t* u, uint8_t* v, int stride,
655 int thresh, int ithresh, int hev_thresh) {
656 FilterLoop24(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
657 FilterLoop24(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
658}
659
660static void HFilter8i(uint8_t* u, uint8_t* v, int stride,
661 int thresh, int ithresh, int hev_thresh) {
662 FilterLoop24(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
663 FilterLoop24(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
664}
665
Vikas Aroraa2415722012-08-09 16:18:58 -0700666//------------------------------------------------------------------------------
Eric Hassold9aea6422011-01-04 17:22:46 -0800667
Vikas Aroraa2415722012-08-09 16:18:58 -0700668VP8DecIdct2 VP8Transform;
Vikas Arora8b720222014-01-02 16:48:02 -0800669VP8DecIdct VP8TransformAC3;
Vikas Aroraa2415722012-08-09 16:18:58 -0700670VP8DecIdct VP8TransformUV;
671VP8DecIdct VP8TransformDC;
672VP8DecIdct VP8TransformDCUV;
Eric Hassold9aea6422011-01-04 17:22:46 -0800673
Vikas Aroraa2415722012-08-09 16:18:58 -0700674VP8LumaFilterFunc VP8VFilter16;
675VP8LumaFilterFunc VP8HFilter16;
676VP8ChromaFilterFunc VP8VFilter8;
677VP8ChromaFilterFunc VP8HFilter8;
678VP8LumaFilterFunc VP8VFilter16i;
679VP8LumaFilterFunc VP8HFilter16i;
680VP8ChromaFilterFunc VP8VFilter8i;
681VP8ChromaFilterFunc VP8HFilter8i;
682VP8SimpleFilterFunc VP8SimpleVFilter16;
683VP8SimpleFilterFunc VP8SimpleHFilter16;
684VP8SimpleFilterFunc VP8SimpleVFilter16i;
685VP8SimpleFilterFunc VP8SimpleHFilter16i;
Vikas Arora46672792011-07-13 16:37:55 +0530686
687extern void VP8DspInitSSE2(void);
Vikas Aroraa2415722012-08-09 16:18:58 -0700688extern void VP8DspInitNEON(void);
Vikas Aroraaf51b942014-08-28 10:51:12 -0700689extern void VP8DspInitMIPS32(void);
Eric Hassold9aea6422011-01-04 17:22:46 -0800690
Vikas Arora03d5e342011-06-02 23:59:44 +0530691void VP8DspInit(void) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700692 VP8InitClipTables();
Vikas Aroraa2415722012-08-09 16:18:58 -0700693
Vikas Aroraaf51b942014-08-28 10:51:12 -0700694 VP8TransformWHT = TransformWHT;
Vikas Aroraa2415722012-08-09 16:18:58 -0700695 VP8Transform = TransformTwo;
696 VP8TransformUV = TransformUV;
697 VP8TransformDC = TransformDC;
698 VP8TransformDCUV = TransformDCUV;
Vikas Arora8b720222014-01-02 16:48:02 -0800699 VP8TransformAC3 = TransformAC3;
Vikas Aroraa2415722012-08-09 16:18:58 -0700700
701 VP8VFilter16 = VFilter16;
702 VP8HFilter16 = HFilter16;
703 VP8VFilter8 = VFilter8;
704 VP8HFilter8 = HFilter8;
705 VP8VFilter16i = VFilter16i;
706 VP8HFilter16i = HFilter16i;
707 VP8VFilter8i = VFilter8i;
708 VP8HFilter8i = HFilter8i;
709 VP8SimpleVFilter16 = SimpleVFilter16;
710 VP8SimpleHFilter16 = SimpleHFilter16;
711 VP8SimpleVFilter16i = SimpleVFilter16i;
712 VP8SimpleHFilter16i = SimpleHFilter16i;
713
Vikas Arora46672792011-07-13 16:37:55 +0530714 // If defined, use CPUInfo() to overwrite some pointers with faster versions.
Vikas Aroraaf51b942014-08-28 10:51:12 -0700715 if (VP8GetCPUInfo != NULL) {
Vikas Aroraa2415722012-08-09 16:18:58 -0700716#if defined(WEBP_USE_SSE2)
717 if (VP8GetCPUInfo(kSSE2)) {
Vikas Arora46672792011-07-13 16:37:55 +0530718 VP8DspInitSSE2();
Vikas Aroraa2415722012-08-09 16:18:58 -0700719 }
720#elif defined(WEBP_USE_NEON)
721 if (VP8GetCPUInfo(kNEON)) {
722 VP8DspInitNEON();
723 }
Vikas Aroraaf51b942014-08-28 10:51:12 -0700724#elif defined(WEBP_USE_MIPS32)
725 if (VP8GetCPUInfo(kMIPS32)) {
726 VP8DspInitMIPS32();
727 }
Vikas Arora46672792011-07-13 16:37:55 +0530728#endif
Vikas Arora46672792011-07-13 16:37:55 +0530729 }
Eric Hassold9aea6422011-01-04 17:22:46 -0800730}
731